Coverage Report

Created: 2026-01-18 06:47

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
53.9M
#define SET_NODE(target, src) do { \
44
53.9M
    target ## _type = (src)->op_type; \
45
53.9M
    if ((src)->op_type == IS_CONST) { \
46
5.88M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
48.0M
    } else { \
48
48.0M
      target = (src)->u.op; \
49
48.0M
    } \
50
53.9M
  } while (0)
51
52
41.8M
#define GET_NODE(target, src) do { \
53
41.8M
    (target)->op_type = src ## _type; \
54
41.8M
    if ((target)->op_type == IS_CONST) { \
55
913
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
41.8M
    } else { \
57
41.8M
      (target)->u.op = src; \
58
41.8M
    } \
59
41.8M
  } while (0)
60
61
86.2M
#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
15.5M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
15.5M
  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
15.5M
  zend_op_array *op_array = CG(active_op_array);
79
15.5M
  uint32_t ret = op_array->cache_size;
80
15.5M
  op_array->cache_size += count * sizeof(void*);
81
15.5M
  return ret;
82
15.5M
}
83
84
15.2M
static inline uint32_t zend_alloc_cache_slot(void) {
85
15.2M
  return zend_alloc_cache_slots(1);
86
15.2M
}
87
88
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
89
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
90
91
#ifndef ZTS
92
ZEND_API zend_compiler_globals compiler_globals;
93
ZEND_API zend_executor_globals executor_globals;
94
#endif
95
96
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
97
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
98
static void zend_eval_const_expr(zend_ast **ast_ptr);
99
100
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
102
static void zend_compile_expr(znode *result, zend_ast *ast);
103
static void zend_compile_stmt(zend_ast *ast);
104
static void zend_compile_assign(znode *result, zend_ast *ast);
105
106
#ifdef ZEND_CHECK_STACK_LIMIT
107
zend_never_inline static void zend_stack_limit_error(void)
108
0
{
109
0
  size_t max_stack_size = 0;
110
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
111
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
112
0
  }
113
114
0
  zend_error_noreturn(E_COMPILE_ERROR,
115
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
116
0
    max_stack_size);
117
0
}
118
119
static void zend_check_stack_limit(void)
120
62.0M
{
121
62.0M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
62.0M
}
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
84.2M
{
133
84.2M
  MAKE_NOP(op);
134
84.2M
  op->extended_value = 0;
135
84.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
84.2M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
13.6M
{
148
13.6M
  return CG(active_op_array)->last;
149
13.6M
}
150
151
static zend_op *get_next_op(void)
152
83.8M
{
153
83.8M
  zend_op_array *op_array = CG(active_op_array);
154
83.8M
  uint32_t next_op_num = op_array->last++;
155
83.8M
  zend_op *next_op;
156
157
83.8M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
773k
    CG(context).opcodes_size *= 4;
159
773k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
773k
  }
161
162
83.8M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
83.8M
  init_op(next_op);
165
166
83.8M
  return next_op;
167
83.8M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
44.3k
{
171
44.3k
  CG(context).last_brk_cont++;
172
44.3k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
44.3k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
44.3k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
135k
{
178
135k
  zend_string *filename = CG(active_op_array)->filename;
179
135k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
135k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
135k
  return zend_new_interned_string(result);
182
135k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
15.1M
{
187
15.1M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
15.1M
  if (ns_separator != NULL) {
189
14.7M
    *result = ns_separator + 1;
190
14.7M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
14.7M
    return 1;
192
14.7M
  }
193
194
420k
  return 0;
195
15.1M
}
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
584k
{
227
584k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
584k
  const char *uqname = ZSTR_VAL(name);
230
584k
  size_t uqname_len = ZSTR_LEN(name);
231
584k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
10.5M
  for (; reserved->name; ++reserved) {
234
9.93M
    if (uqname_len == reserved->len
235
271k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
9.93M
    ) {
237
14
      return 1;
238
14
    }
239
9.93M
  }
240
241
584k
  return 0;
242
584k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
583k
{
247
583k
  if (zend_is_reserved_class_name(name)) {
248
5
    zend_error_noreturn(E_COMPILE_ERROR,
249
5
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
5
  }
251
583k
  if (zend_string_equals_literal(name, "_")) {
252
1.53k
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
1.53k
  }
254
583k
}
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
558k
{
295
558k
  const builtin_type_info *info = &builtin_types[0];
296
297
6.86M
  for (; info->name; ++info) {
298
6.40M
    if (ZSTR_LEN(name) == info->name_len
299
303k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
6.40M
    ) {
301
102k
      return info->type;
302
102k
    }
303
6.40M
  }
304
305
456k
  return 0;
306
558k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
455k
{
311
455k
  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
2.16M
  for (; info->name; ++info) {
316
1.74M
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
39.0k
      *correct_name = info->correct_name;
318
39.0k
      return 1;
319
39.0k
    }
320
1.74M
  }
321
322
416k
  return 0;
323
455k
}
324
/* }}} */
325
326
39.0k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
39.0k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
39.0k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
1.78M
{
333
1.78M
  *prev_context = CG(context);
334
1.78M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
1.78M
  CG(context).op_array = op_array;
336
1.78M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
1.78M
  CG(context).vars_size = 0;
338
1.78M
  CG(context).literals_size = 0;
339
1.78M
  CG(context).fast_call_var = -1;
340
1.78M
  CG(context).try_catch_offset = -1;
341
1.78M
  CG(context).current_brk_cont = -1;
342
1.78M
  CG(context).last_brk_cont = 0;
343
1.78M
  CG(context).has_assigned_to_http_response_header = false;
344
1.78M
  CG(context).brk_cont_array = NULL;
345
1.78M
  CG(context).labels = NULL;
346
1.78M
  CG(context).in_jmp_frameless_branch = false;
347
1.78M
  CG(context).active_property_info_name = NULL;
348
1.78M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
1.78M
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
1.77M
{
354
1.77M
  if (CG(context).brk_cont_array) {
355
22.4k
    efree(CG(context).brk_cont_array);
356
22.4k
    CG(context).brk_cont_array = NULL;
357
22.4k
  }
358
1.77M
  if (CG(context).labels) {
359
5.13k
    zend_hash_destroy(CG(context).labels);
360
5.13k
    FREE_HASHTABLE(CG(context).labels);
361
5.13k
    CG(context).labels = NULL;
362
5.13k
  }
363
1.77M
  CG(context) = *prev_context;
364
1.77M
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
29.7k
{
369
29.7k
  if (FC(imports)) {
370
684
    zend_hash_destroy(FC(imports));
371
684
    efree(FC(imports));
372
684
    FC(imports) = NULL;
373
684
  }
374
375
29.7k
  if (FC(imports_function)) {
376
470
    zend_hash_destroy(FC(imports_function));
377
470
    efree(FC(imports_function));
378
470
    FC(imports_function) = NULL;
379
470
  }
380
381
29.7k
  if (FC(imports_const)) {
382
391
    zend_hash_destroy(FC(imports_const));
383
391
    efree(FC(imports_const));
384
391
    FC(imports_const) = NULL;
385
391
  }
386
387
29.7k
  zend_hash_clean(&FC(seen_symbols));
388
29.7k
}
389
/* }}} */
390
391
25.1k
static void zend_end_namespace(void) /* {{{ */ {
392
25.1k
  FC(in_namespace) = 0;
393
25.1k
  zend_reset_import_tables();
394
25.1k
  if (FC(current_namespace)) {
395
2.72k
    zend_string_release_ex(FC(current_namespace), 0);
396
2.72k
    FC(current_namespace) = NULL;
397
2.72k
  }
398
25.1k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
26.7k
{
403
26.7k
  *prev_context = CG(file_context);
404
26.7k
  FC(imports) = NULL;
405
26.7k
  FC(imports_function) = NULL;
406
26.7k
  FC(imports_const) = NULL;
407
26.7k
  FC(current_namespace) = NULL;
408
26.7k
  FC(in_namespace) = 0;
409
26.7k
  FC(has_bracketed_namespaces) = 0;
410
26.7k
  FC(declarables).ticks = 0;
411
26.7k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
26.7k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
23.7k
{
417
23.7k
  zend_end_namespace();
418
23.7k
  zend_hash_destroy(&FC(seen_symbols));
419
23.7k
  CG(file_context) = *prev_context;
420
23.7k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
56.4k
{
425
56.4k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
56.4k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
56.4k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
56.4k
  CG(active_class_entry) = NULL;
429
56.4k
  CG(in_compilation) = 0;
430
56.4k
  CG(skip_shebang) = 0;
431
432
56.4k
  CG(encoding_declared) = 0;
433
56.4k
  CG(memoized_exprs) = NULL;
434
56.4k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
56.4k
}
436
/* }}} */
437
438
1.87M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
1.87M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
1.87M
  if (zv) {
441
1.83M
    Z_LVAL_P(zv) |= kind;
442
1.83M
  } else {
443
36.0k
    zval tmp;
444
36.0k
    ZVAL_LONG(&tmp, kind);
445
36.0k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
36.0k
  }
447
1.87M
}
448
449
2.26k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
2.26k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
2.26k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
2.26k
}
453
454
void init_compiler(void) /* {{{ */
455
53.5k
{
456
53.5k
  CG(arena) = zend_arena_create(64 * 1024);
457
53.5k
  CG(active_op_array) = NULL;
458
53.5k
  memset(&CG(context), 0, sizeof(CG(context)));
459
53.5k
  zend_init_compiler_data_structures();
460
53.5k
  zend_init_rsrc_list();
461
53.5k
  zend_stream_init();
462
53.5k
  CG(unclean_shutdown) = 0;
463
464
53.5k
  CG(delayed_variance_obligations) = NULL;
465
53.5k
  CG(delayed_autoloads) = NULL;
466
53.5k
  CG(unlinked_uses) = NULL;
467
53.5k
  CG(current_linking_class) = NULL;
468
53.5k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
56.4k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
56.4k
  zend_restore_compiled_filename(NULL);
475
476
56.4k
  zend_stack_destroy(&CG(loop_var_stack));
477
56.4k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
56.4k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
56.4k
  if (CG(delayed_variance_obligations)) {
481
1
    zend_hash_destroy(CG(delayed_variance_obligations));
482
1
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
1
    CG(delayed_variance_obligations) = NULL;
484
1
  }
485
56.4k
  if (CG(delayed_autoloads)) {
486
0
    zend_hash_destroy(CG(delayed_autoloads));
487
0
    FREE_HASHTABLE(CG(delayed_autoloads));
488
0
    CG(delayed_autoloads) = NULL;
489
0
  }
490
56.4k
  if (CG(unlinked_uses)) {
491
88
    zend_hash_destroy(CG(unlinked_uses));
492
88
    FREE_HASHTABLE(CG(unlinked_uses));
493
88
    CG(unlinked_uses) = NULL;
494
88
  }
495
56.4k
  CG(current_linking_class) = NULL;
496
56.4k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
53.5k
{
501
53.5k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
53.5k
  return new_compiled_filename;
503
53.5k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
209k
{
508
209k
  if (CG(compiled_filename)) {
509
53.4k
    zend_string_release(CG(compiled_filename));
510
53.4k
    CG(compiled_filename) = NULL;
511
53.4k
  }
512
209k
  CG(compiled_filename) = original_compiled_filename;
513
209k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
2.20M
{
518
2.20M
  return CG(compiled_filename);
519
2.20M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
172k
{
524
172k
  return CG(zend_lineno);
525
172k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
199k
{
530
199k
  return CG(in_compilation);
531
199k
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
41.9M
{
536
41.9M
  return (uint32_t)CG(active_op_array)->T++;
537
41.9M
}
538
/* }}} */
539
540
2.19M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.19M
  zend_op_array *op_array = CG(active_op_array);
542
2.19M
  int i = 0;
543
2.19M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
4.03M
  while (i < op_array->last_var) {
546
2.04M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
203k
     && zend_string_equals(op_array->vars[i], name)) {
548
202k
      return EX_NUM_TO_VAR(i);
549
202k
    }
550
1.84M
    i++;
551
1.84M
  }
552
1.99M
  i = op_array->last_var;
553
1.99M
  op_array->last_var++;
554
1.99M
  if (op_array->last_var > CG(context).vars_size) {
555
1.68M
    CG(context).vars_size += 16; /* FIXME */
556
1.68M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
1.68M
  }
558
559
1.99M
  op_array->vars[i] = zend_string_copy(name);
560
1.99M
  return EX_NUM_TO_VAR(i);
561
2.19M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
49.5M
{
566
49.5M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
49.5M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
49.5M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
4.17M
    Z_TYPE_FLAGS_P(zv) = 0;
570
4.17M
  }
571
49.5M
  return Z_STR_P(zv);
572
49.5M
}
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
49.7M
{
577
49.7M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
49.7M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
47.4M
    zval_make_interned_string(zv);
580
47.4M
  }
581
49.7M
  ZVAL_COPY_VALUE(lit, zv);
582
49.7M
  Z_EXTRA_P(lit) = 0;
583
49.7M
}
584
/* }}} */
585
586
/* Is used while compiling a function, using the context to keep track
587
   of an approximate size to avoid to relocate to often.
588
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
589
static int zend_add_literal(zval *zv) /* {{{ */
590
49.7M
{
591
49.7M
  zend_op_array *op_array = CG(active_op_array);
592
49.7M
  uint32_t i = op_array->last_literal;
593
49.7M
  op_array->last_literal++;
594
49.7M
  if (i >= CG(context).literals_size) {
595
7.75M
    while (i >= CG(context).literals_size) {
596
3.87M
      CG(context).literals_size += 16; /* FIXME */
597
3.87M
    }
598
3.87M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
3.87M
  }
600
49.7M
  zend_insert_literal(op_array, zv, i);
601
49.7M
  return i;
602
49.7M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
43.7M
{
607
43.7M
  int ret;
608
43.7M
  zval zv;
609
43.7M
  ZVAL_STR(&zv, *str);
610
43.7M
  ret = zend_add_literal(&zv);
611
43.7M
  *str = Z_STR(zv);
612
43.7M
  return ret;
613
43.7M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
128k
{
618
  /* Original name */
619
128k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
128k
  zend_string *lc_name = zend_string_tolower(name);
623
128k
  zend_add_literal_string(&lc_name);
624
625
128k
  return ret;
626
128k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
3.94M
{
631
3.94M
  const char *unqualified_name;
632
3.94M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
3.94M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
3.94M
  zend_string *lc_name = zend_string_tolower(name);
639
3.94M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
3.94M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
3.94M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
3.94M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
3.94M
    zend_add_literal_string(&lc_name);
646
3.94M
  }
647
648
3.94M
  return ret;
649
3.94M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
109k
{
654
  /* Original name */
655
109k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
109k
  zend_string *lc_name = zend_string_tolower(name);
659
109k
  zend_add_literal_string(&lc_name);
660
661
109k
  return ret;
662
109k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
10.4M
{
667
10.4M
  zend_string *tmp_name;
668
669
10.4M
  int ret = zend_add_literal_string(&name);
670
671
10.4M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
10.4M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
10.4M
  if (after_ns) {
674
10.2M
    after_ns += 1;
675
10.2M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
10.2M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
10.2M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
10.2M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
10.2M
    zend_add_literal_string(&tmp_name);
682
683
10.2M
    if (!unqualified) {
684
2.81k
      return ret;
685
2.81k
    }
686
10.2M
  } else {
687
157k
    after_ns = ZSTR_VAL(name);
688
157k
  }
689
690
  /* original unqualified constant name */
691
10.4M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
10.4M
  zend_add_literal_string(&tmp_name);
693
694
10.4M
  return ret;
695
10.4M
}
696
/* }}} */
697
698
78.6k
#define LITERAL_STR(op, str) do { \
699
78.6k
    zval _c; \
700
78.6k
    ZVAL_STR(&_c, str); \
701
78.6k
    op.constant = zend_add_literal(&_c); \
702
78.6k
  } while (0)
703
704
void zend_stop_lexing(void)
705
14
{
706
14
  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
14
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
14
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
44.3k
{
716
44.3k
  zend_brk_cont_element *brk_cont_element;
717
44.3k
  int parent = CG(context).current_brk_cont;
718
44.3k
  zend_loop_var info = {0};
719
720
44.3k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
44.3k
  brk_cont_element = get_next_brk_cont_element();
722
44.3k
  brk_cont_element->parent = parent;
723
44.3k
  brk_cont_element->is_switch = is_switch;
724
725
44.3k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
17.2k
    uint32_t start = get_next_op_number();
727
728
17.2k
    info.opcode = free_opcode;
729
17.2k
    info.var_type = loop_var->op_type;
730
17.2k
    info.var_num = loop_var->u.op.var;
731
17.2k
    brk_cont_element->start = start;
732
27.1k
  } else {
733
27.1k
    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
27.1k
    brk_cont_element->start = -1;
737
27.1k
  }
738
739
44.3k
  zend_stack_push(&CG(loop_var_stack), &info);
740
44.3k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
44.1k
{
745
44.1k
  uint32_t end = get_next_op_number();
746
44.1k
  zend_brk_cont_element *brk_cont_element
747
44.1k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
44.1k
  brk_cont_element->cont = cont_addr;
749
44.1k
  brk_cont_element->brk = end;
750
44.1k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
44.1k
  zend_stack_del_top(&CG(loop_var_stack));
753
44.1k
}
754
/* }}} */
755
756
static void zend_do_free(znode *op1) /* {{{ */
757
3.94M
{
758
3.94M
  if (op1->op_type == IS_TMP_VAR) {
759
1.64M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
760
761
1.66M
    while (opline->opcode == ZEND_END_SILENCE ||
762
1.66M
           opline->opcode == ZEND_OP_DATA) {
763
17.7k
      opline--;
764
17.7k
    }
765
766
1.64M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
767
1.63M
      switch (opline->opcode) {
768
647
        case ZEND_BOOL:
769
7.37k
        case ZEND_BOOL_NOT:
770
          /* boolean results don't have to be freed */
771
7.37k
          return;
772
194
        case ZEND_POST_INC_STATIC_PROP:
773
388
        case ZEND_POST_DEC_STATIC_PROP:
774
470
        case ZEND_POST_INC_OBJ:
775
539
        case ZEND_POST_DEC_OBJ:
776
765
        case ZEND_POST_INC:
777
1.07k
        case ZEND_POST_DEC:
778
          /* convert $i++ to ++$i */
779
1.07k
          opline->opcode -= 2;
780
1.07k
          SET_UNUSED(opline->result);
781
1.07k
          return;
782
16.1k
        case ZEND_ASSIGN:
783
19.1k
        case ZEND_ASSIGN_DIM:
784
21.8k
        case ZEND_ASSIGN_OBJ:
785
22.7k
        case ZEND_ASSIGN_STATIC_PROP:
786
23.2k
        case ZEND_ASSIGN_OP:
787
24.5k
        case ZEND_ASSIGN_DIM_OP:
788
24.8k
        case ZEND_ASSIGN_OBJ_OP:
789
25.2k
        case ZEND_ASSIGN_STATIC_PROP_OP:
790
25.3k
        case ZEND_PRE_INC_STATIC_PROP:
791
25.5k
        case ZEND_PRE_DEC_STATIC_PROP:
792
25.5k
        case ZEND_PRE_INC_OBJ:
793
25.7k
        case ZEND_PRE_DEC_OBJ:
794
26.0k
        case ZEND_PRE_INC:
795
26.3k
        case ZEND_PRE_DEC:
796
26.3k
          SET_UNUSED(opline->result);
797
26.3k
          return;
798
1.63M
      }
799
1.63M
    }
800
801
1.61M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
802
2.29M
  } else if (op1->op_type == IS_VAR) {
803
2.24M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
804
2.43M
    while (opline->opcode == ZEND_END_SILENCE ||
805
2.43M
        opline->opcode == ZEND_EXT_FCALL_END ||
806
2.43M
        opline->opcode == ZEND_OP_DATA) {
807
190k
      opline--;
808
190k
    }
809
2.24M
    if (opline->result_type == IS_VAR
810
2.22M
      && opline->result.var == op1->u.op.var) {
811
2.22M
      if (opline->opcode == ZEND_FETCH_THIS) {
812
0
        opline->opcode = ZEND_NOP;
813
0
      }
814
2.22M
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
815
1.46M
        SET_UNUSED(opline->result);
816
1.46M
      } else {
817
        /* Frameless calls usually use the return value, so always emit a free. This should be
818
         * faster than checking RETURN_VALUE_USED inside the handler. */
819
755k
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
820
755k
      }
821
2.22M
    } else {
822
87.1k
      while (opline >= CG(active_op_array)->opcodes) {
823
87.1k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
824
85.7k
             opline->opcode == ZEND_FETCH_LIST_W ||
825
84.9k
             opline->opcode == ZEND_EXT_STMT) &&
826
2.29k
            opline->op1_type == IS_VAR &&
827
2.29k
            opline->op1.var == op1->u.op.var) {
828
1.77k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
829
1.77k
          return;
830
1.77k
        }
831
85.4k
        if (opline->result_type == IS_VAR
832
30.6k
          && opline->result.var == op1->u.op.var) {
833
14.5k
          if (opline->opcode == ZEND_NEW) {
834
14.5k
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
835
14.5k
          }
836
14.5k
          break;
837
14.5k
        }
838
70.8k
        opline--;
839
70.8k
      }
840
16.3k
    }
841
2.24M
  } else if (op1->op_type == IS_CONST) {
842
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
843
     * free the zend_array structure, so references to it from outside the op array
844
     * become invalid. GC would cause such a reference in the root buffer. */
845
55.0k
    zval_ptr_dtor_nogc(&op1->u.constant);
846
55.0k
  }
847
3.94M
}
848
/* }}} */
849
850
851
static const char *zend_modifier_token_to_string(uint32_t token)
852
38
{
853
38
  switch (token) {
854
1
    case T_PUBLIC:
855
1
      return "public";
856
1
    case T_PROTECTED:
857
1
      return "protected";
858
1
    case T_PRIVATE:
859
1
      return "private";
860
8
    case T_STATIC:
861
8
      return "static";
862
7
    case T_FINAL:
863
7
      return "final";
864
8
    case T_READONLY:
865
8
      return "readonly";
866
7
    case T_ABSTRACT:
867
7
      return "abstract";
868
1
    case T_PUBLIC_SET:
869
1
      return "public(set)";
870
1
    case T_PROTECTED_SET:
871
1
      return "protected(set)";
872
3
    case T_PRIVATE_SET:
873
3
      return "private(set)";
874
38
    EMPTY_SWITCH_DEFAULT_CASE()
875
38
  }
876
38
}
877
878
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
879
22.7k
{
880
22.7k
  switch (token) {
881
14.0k
    case T_PUBLIC:
882
14.0k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
883
14.0k
        return ZEND_ACC_PUBLIC;
884
14.0k
      }
885
1
      break;
886
1.04k
    case T_PROTECTED:
887
1.04k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
888
1.04k
        return ZEND_ACC_PROTECTED;
889
1.04k
      }
890
1
      break;
891
1.63k
    case T_PRIVATE:
892
1.63k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
893
1.63k
        return ZEND_ACC_PRIVATE;
894
1.63k
      }
895
1
      break;
896
270
    case T_READONLY:
897
270
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
898
267
        return ZEND_ACC_READONLY;
899
267
      }
900
3
      break;
901
749
    case T_ABSTRACT:
902
749
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
903
748
        return ZEND_ACC_ABSTRACT;
904
748
      }
905
1
      break;
906
1.07k
    case T_FINAL:
907
1.07k
      return ZEND_ACC_FINAL;
908
3.09k
    case T_STATIC:
909
3.09k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
910
3.09k
        return ZEND_ACC_STATIC;
911
3.09k
      }
912
2
      break;
913
362
    case T_PUBLIC_SET:
914
362
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
915
361
        return ZEND_ACC_PUBLIC_SET;
916
361
      }
917
1
      break;
918
139
    case T_PROTECTED_SET:
919
139
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
920
138
        return ZEND_ACC_PROTECTED_SET;
921
138
      }
922
1
      break;
923
333
    case T_PRIVATE_SET:
924
333
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
925
330
        return ZEND_ACC_PRIVATE_SET;
926
330
      }
927
3
      break;
928
22.7k
  }
929
930
14
  char *member;
931
14
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
932
0
    member = "property";
933
14
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
934
4
    member = "method";
935
10
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
936
2
    member = "class constant";
937
8
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
938
2
    member = "parameter";
939
6
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
940
6
    member = "property hook";
941
6
  } else {
942
0
    ZEND_UNREACHABLE();
943
0
  }
944
945
14
  zend_throw_exception_ex(zend_ce_compile_error, 0,
946
14
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
947
14
  return 0;
948
14
}
949
950
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
951
18.8k
{
952
18.8k
  uint32_t flags = 0;
953
18.8k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
954
955
40.8k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
956
22.0k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
957
22.0k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
958
22.0k
    if (!new_flag) {
959
12
      return 0;
960
12
    }
961
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
962
22.0k
    bool duplicate_flag = (flags & new_flag);
963
21.9k
    flags = zend_add_member_modifier(flags, new_flag, target);
964
21.9k
    if (!flags) {
965
18
      return 0;
966
18
    }
967
21.9k
    if (duplicate_flag) {
968
24
      zend_throw_exception_ex(zend_ce_compile_error, 0,
969
24
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
970
24
      return 0;
971
24
    }
972
21.9k
  }
973
974
18.7k
  return flags;
975
18.8k
}
976
977
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
978
141
{
979
141
  uint32_t new_flags = flags | new_flag;
980
141
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
981
2
    zend_throw_exception(zend_ce_compile_error,
982
2
      "Multiple abstract modifiers are not allowed", 0);
983
2
    return 0;
984
2
  }
985
139
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
986
1
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
987
1
    return 0;
988
1
  }
989
138
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
990
2
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
991
2
    return 0;
992
2
  }
993
136
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
994
2
    zend_throw_exception(zend_ce_compile_error,
995
2
      "Cannot use the final modifier on an abstract class", 0);
996
2
    return 0;
997
2
  }
998
134
  return new_flags;
999
136
}
1000
/* }}} */
1001
1002
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1003
70
{
1004
70
  uint32_t new_flags = flags | new_flag;
1005
70
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1006
1
    zend_throw_exception(zend_ce_compile_error,
1007
1
      "Cannot use the abstract modifier on an anonymous class", 0);
1008
1
    return 0;
1009
1
  }
1010
69
  if (new_flag & ZEND_ACC_FINAL) {
1011
1
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1012
1
    return 0;
1013
1
  }
1014
68
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1015
1
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1016
1
    return 0;
1017
1
  }
1018
67
  return new_flags;
1019
68
}
1020
1021
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1022
21.9k
{
1023
21.9k
  uint32_t new_flags = flags | new_flag;
1024
21.9k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1025
6
    zend_throw_exception(zend_ce_compile_error,
1026
6
      "Multiple access type modifiers are not allowed", 0);
1027
6
    return 0;
1028
6
  }
1029
21.9k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1030
2
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1031
1
      zend_throw_exception(zend_ce_compile_error,
1032
1
        "Cannot use the final modifier on an abstract method", 0);
1033
1
      return 0;
1034
1
    }
1035
1
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1036
1
      zend_throw_exception(zend_ce_compile_error,
1037
1
        "Cannot use the final modifier on an abstract property", 0);
1038
1
      return 0;
1039
1
    }
1040
1
  }
1041
21.9k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1042
14.3k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1043
10
      zend_throw_exception(zend_ce_compile_error,
1044
10
        "Multiple access type modifiers are not allowed", 0);
1045
10
      return 0;
1046
10
    }
1047
14.3k
  }
1048
21.9k
  return new_flags;
1049
21.9k
}
1050
/* }}} */
1051
1052
2.01k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1053
2.01k
  return zend_string_concat3(
1054
2.01k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1055
2.01k
    "::", sizeof("::") - 1,
1056
2.01k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1057
2.01k
}
1058
1059
17.8M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1060
17.8M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1061
17.8M
}
1062
1063
18.2M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1064
18.2M
  if (FC(current_namespace)) {
1065
17.8M
    const zend_string *ns = FC(current_namespace);
1066
17.8M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1067
17.8M
  } else {
1068
429k
    return zend_string_copy(name);
1069
429k
  }
1070
18.2M
}
1071
1072
static zend_string *zend_resolve_non_class_name(
1073
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1074
  bool case_sensitive, const HashTable *current_import_sub
1075
14.6M
) {
1076
14.6M
  const char *compound;
1077
14.6M
  *is_fully_qualified = false;
1078
1079
14.6M
  if (ZSTR_VAL(name)[0] == '\\') {
1080
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1081
671
    *is_fully_qualified = true;
1082
671
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1083
671
  }
1084
1085
14.6M
  if (type == ZEND_NAME_FQ) {
1086
59.1k
    *is_fully_qualified = true;
1087
59.1k
    return zend_string_copy(name);
1088
59.1k
  }
1089
1090
14.6M
  if (type == ZEND_NAME_RELATIVE) {
1091
1.13k
    *is_fully_qualified = true;
1092
1.13k
    return zend_prefix_with_ns(name);
1093
1.13k
  }
1094
1095
14.6M
  if (current_import_sub) {
1096
    /* If an unqualified name is a function/const alias, replace it. */
1097
1.87k
    zend_string *import_name;
1098
1.87k
    if (case_sensitive) {
1099
1.45k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1100
1.45k
    } else {
1101
419
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1102
419
    }
1103
1104
1.87k
    if (import_name) {
1105
564
      *is_fully_qualified = true;
1106
564
      return zend_string_copy(import_name);
1107
564
    }
1108
1.87k
  }
1109
1110
14.6M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1111
14.6M
  if (compound) {
1112
6.69k
    *is_fully_qualified = true;
1113
6.69k
  }
1114
1115
14.6M
  if (compound && FC(imports)) {
1116
    /* If the first part of a qualified name is an alias, substitute it. */
1117
2.17k
    size_t len = compound - ZSTR_VAL(name);
1118
2.17k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1119
1120
2.17k
    if (import_name) {
1121
486
      return zend_concat_names(
1122
486
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1123
486
    }
1124
2.17k
  }
1125
1126
14.6M
  return zend_prefix_with_ns(name);
1127
14.6M
}
1128
/* }}} */
1129
1130
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1131
4.07M
{
1132
4.07M
  return zend_resolve_non_class_name(
1133
4.07M
    name, type, is_fully_qualified, false, FC(imports_function));
1134
4.07M
}
1135
1136
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1137
10.6M
{
1138
10.6M
  return zend_resolve_non_class_name(
1139
10.6M
    name, type, is_fully_qualified, true, FC(imports_const));
1140
10.6M
}
1141
1142
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1143
3.49M
{
1144
3.49M
  const char *compound;
1145
1146
3.49M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1147
2.63k
    if (type == ZEND_NAME_FQ) {
1148
1
      zend_error_noreturn(E_COMPILE_ERROR,
1149
1
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1150
1
    }
1151
2.63k
    if (type == ZEND_NAME_RELATIVE) {
1152
0
      zend_error_noreturn(E_COMPILE_ERROR,
1153
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1154
0
    }
1155
2.63k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1156
2.63k
    return zend_string_copy(name);
1157
2.63k
  }
1158
1159
3.49M
  if (type == ZEND_NAME_RELATIVE) {
1160
506
    return zend_prefix_with_ns(name);
1161
506
  }
1162
1163
3.49M
  if (type == ZEND_NAME_FQ) {
1164
10.5k
    if (ZSTR_VAL(name)[0] == '\\') {
1165
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1166
147
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1167
147
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1168
1
        zend_error_noreturn(E_COMPILE_ERROR,
1169
1
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1170
1
      }
1171
146
      return name;
1172
147
    }
1173
1174
10.3k
    return zend_string_copy(name);
1175
10.5k
  }
1176
1177
3.48M
  if (FC(imports)) {
1178
2.61k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1179
2.61k
    if (compound) {
1180
      /* If the first part of a qualified name is an alias, substitute it. */
1181
536
      size_t len = compound - ZSTR_VAL(name);
1182
536
      const zend_string *import_name =
1183
536
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1184
1185
536
      if (import_name) {
1186
66
        return zend_concat_names(
1187
66
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1188
66
      }
1189
2.08k
    } else {
1190
      /* If an unqualified name is an alias, replace it. */
1191
2.08k
      zend_string *import_name
1192
2.08k
        = zend_hash_find_ptr_lc(FC(imports), name);
1193
1194
2.08k
      if (import_name) {
1195
450
        return zend_string_copy(import_name);
1196
450
      }
1197
2.08k
    }
1198
2.61k
  }
1199
1200
  /* If not fully qualified and not an alias, prepend the current namespace */
1201
3.47M
  return zend_prefix_with_ns(name);
1202
3.48M
}
1203
/* }}} */
1204
1205
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1206
3.39M
{
1207
3.39M
  const zval *class_name = zend_ast_get_zval(ast);
1208
3.39M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1209
16
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1210
16
  }
1211
3.39M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1212
3.39M
}
1213
/* }}} */
1214
1215
static void label_ptr_dtor(zval *zv) /* {{{ */
1216
7.17k
{
1217
7.17k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1218
7.17k
}
1219
/* }}} */
1220
1221
2.09k
static void str_dtor(zval *zv)  /* {{{ */ {
1222
2.09k
  zend_string_release_ex(Z_STR_P(zv), 0);
1223
2.09k
}
1224
/* }}} */
1225
1226
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1227
9.39k
{
1228
9.39k
  zend_op_array *op_array = CG(active_op_array);
1229
9.39k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1230
9.39k
  zend_try_catch_element *elem;
1231
1232
9.39k
  op_array->try_catch_array = safe_erealloc(
1233
9.39k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1234
1235
9.39k
  elem = &op_array->try_catch_array[try_catch_offset];
1236
9.39k
  elem->try_op = try_op;
1237
9.39k
  elem->catch_op = 0;
1238
9.39k
  elem->finally_op = 0;
1239
9.39k
  elem->finally_end = 0;
1240
1241
9.39k
  return try_catch_offset;
1242
9.39k
}
1243
/* }}} */
1244
1245
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1246
91
{
1247
91
  if (function->type == ZEND_USER_FUNCTION) {
1248
91
    zend_op_array *op_array = &function->op_array;
1249
91
    if (op_array->refcount) {
1250
91
      (*op_array->refcount)++;
1251
91
    }
1252
1253
91
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1254
91
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1255
91
  }
1256
1257
91
  if (function->common.function_name) {
1258
91
    zend_string_addref(function->common.function_name);
1259
91
  }
1260
91
}
1261
/* }}} */
1262
1263
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) /* {{{ */
1264
38
{
1265
38
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1266
38
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1267
38
  const zend_function *old_function;
1268
1269
38
  ZEND_ASSERT(zv != NULL);
1270
38
  old_function = Z_PTR_P(zv);
1271
38
  if (old_function->type == ZEND_USER_FUNCTION
1272
37
    && old_function->op_array.last > 0) {
1273
37
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1274
37
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1275
37
          ZSTR_VAL(old_function->op_array.filename),
1276
37
          old_function->op_array.line_start);
1277
37
  } else {
1278
1
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1279
1
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1280
1
  }
1281
38
}
1282
1283
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1284
0
{
1285
0
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1286
0
  if (UNEXPECTED(!added_func)) {
1287
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1288
0
    return FAILURE;
1289
0
  }
1290
1291
0
  if (func->op_array.refcount) {
1292
0
    ++*func->op_array.refcount;
1293
0
  }
1294
0
  if (func->common.function_name) {
1295
0
    zend_string_addref(func->common.function_name);
1296
0
  }
1297
0
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1298
0
  return SUCCESS;
1299
0
}
1300
/* }}} */
1301
1302
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1303
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1304
0
{
1305
0
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1306
0
  bool is_preloaded =
1307
0
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1308
0
  bool success;
1309
0
  if (EXPECTED(!is_preloaded)) {
1310
0
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1311
0
  } else {
1312
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1313
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1314
0
  }
1315
0
  if (UNEXPECTED(!success)) {
1316
0
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1317
0
    ZEND_ASSERT(old_class);
1318
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1319
0
    return NULL;
1320
0
  }
1321
1322
0
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1323
0
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1324
0
    return ce;
1325
0
  }
1326
1327
0
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1328
0
  if (ce) {
1329
0
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1330
0
    return ce;
1331
0
  }
1332
1333
0
  if (!is_preloaded) {
1334
    /* Reload bucket pointer, the hash table may have been reallocated */
1335
0
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1336
0
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1337
0
  } else {
1338
0
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1339
0
  }
1340
0
  return NULL;
1341
0
}
1342
1343
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1344
0
{
1345
0
  zval *rtd_key, *zv;
1346
1347
0
  rtd_key = lcname + 1;
1348
1349
0
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1350
1351
0
  if (UNEXPECTED(!zv)) {
1352
0
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1353
0
    ZEND_ASSERT(ce);
1354
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1355
0
    return FAILURE;
1356
0
  }
1357
1358
  /* Register the derived class */
1359
0
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1360
0
}
1361
/* }}} */
1362
1363
10.4k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1364
10.4k
  zend_string *result;
1365
10.4k
  if (type == NULL) {
1366
4.53k
    return zend_string_copy(new_type);
1367
4.53k
  }
1368
1369
5.88k
  if (is_intersection) {
1370
3.56k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1371
3.56k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1372
3.56k
    zend_string_release(type);
1373
3.56k
  } else {
1374
2.31k
    result = zend_string_concat3(
1375
2.31k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1376
2.31k
    zend_string_release(type);
1377
2.31k
  }
1378
5.88k
  return result;
1379
10.4k
}
1380
1381
1.91k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1382
1.91k
  if (scope) {
1383
1.64k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1384
0
      name = scope->name;
1385
1.64k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1386
0
      name = scope->parent->name;
1387
0
    }
1388
1.64k
  }
1389
1390
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1391
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1392
1.91k
  size_t len = strlen(ZSTR_VAL(name));
1393
1.91k
  if (len != ZSTR_LEN(name)) {
1394
0
    return zend_string_init(ZSTR_VAL(name), len, 0);
1395
0
  }
1396
1.91k
  return zend_string_copy(name);
1397
1.91k
}
1398
1399
static zend_string *add_intersection_type(zend_string *str,
1400
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1401
  bool is_bracketed)
1402
1.72k
{
1403
1.72k
  const zend_type *single_type;
1404
1.72k
  zend_string *intersection_str = NULL;
1405
1406
7.01k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1407
7.01k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1408
7.01k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1409
1410
5.29k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1411
5.29k
  } ZEND_TYPE_LIST_FOREACH_END();
1412
1413
1.72k
  ZEND_ASSERT(intersection_str);
1414
1415
1.72k
  if (is_bracketed) {
1416
1.43k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1417
1.43k
    zend_string_release(intersection_str);
1418
1.43k
    intersection_str = result;
1419
1.43k
  }
1420
1.72k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1421
1.72k
  zend_string_release(intersection_str);
1422
1.72k
  return str;
1423
1.72k
}
1424
1425
3.78k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1426
3.78k
  zend_string *str = NULL;
1427
1428
  /* Pure intersection type */
1429
3.78k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1430
294
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1431
294
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1432
3.49k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1433
    /* A union type might not be a list */
1434
309
    const zend_type *list_type;
1435
2.68k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1436
2.68k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1437
1.43k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1438
1.43k
        continue;
1439
1.43k
      }
1440
945
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1441
945
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1442
1443
945
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1444
945
      zend_string *resolved = resolve_class_name(name, scope);
1445
945
      str = add_type_string(str, resolved, /* is_intersection */ false);
1446
945
      zend_string_release(resolved);
1447
945
    } ZEND_TYPE_LIST_FOREACH_END();
1448
3.18k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1449
972
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1450
972
  }
1451
1452
3.78k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1453
1454
3.78k
  if (type_mask == MAY_BE_ANY) {
1455
9
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1456
1457
9
    return str;
1458
9
  }
1459
3.77k
  if (type_mask & MAY_BE_STATIC) {
1460
38
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1461
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1462
38
    if (scope && !zend_is_compiling()) {
1463
0
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1464
0
      if (called_scope) {
1465
0
        name = called_scope->name;
1466
0
      }
1467
0
    }
1468
38
    str = add_type_string(str, name, /* is_intersection */ false);
1469
38
  }
1470
3.77k
  if (type_mask & MAY_BE_CALLABLE) {
1471
13
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1472
13
  }
1473
3.77k
  if (type_mask & MAY_BE_OBJECT) {
1474
9
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1475
9
  }
1476
3.77k
  if (type_mask & MAY_BE_ARRAY) {
1477
393
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1478
393
  }
1479
3.77k
  if (type_mask & MAY_BE_STRING) {
1480
938
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1481
938
  }
1482
3.77k
  if (type_mask & MAY_BE_LONG) {
1483
913
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1484
913
  }
1485
3.77k
  if (type_mask & MAY_BE_DOUBLE) {
1486
11
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1487
11
  }
1488
3.77k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1489
15
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1490
3.76k
  } else if (type_mask & MAY_BE_FALSE) {
1491
5
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1492
3.75k
  } else if (type_mask & MAY_BE_TRUE) {
1493
3
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1494
3
  }
1495
3.77k
  if (type_mask & MAY_BE_VOID) {
1496
13
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1497
13
  }
1498
3.77k
  if (type_mask & MAY_BE_NEVER) {
1499
1
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1500
1
  }
1501
1502
3.77k
  if (type_mask & MAY_BE_NULL) {
1503
1.00k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1504
1.00k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1505
1.00k
    if (!is_union && !has_intersection) {
1506
909
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1507
909
      zend_string_release(str);
1508
909
      return nullable_str;
1509
909
    }
1510
1511
94
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1512
94
  }
1513
2.86k
  return str;
1514
3.77k
}
1515
1516
273
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1517
273
  return zend_type_to_string_resolved(type, NULL);
1518
273
}
1519
1520
2.14k
static bool is_generator_compatible_class_type(const zend_string *name) {
1521
2.14k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1522
1.79k
    || zend_string_equals_literal_ci(name, "Iterator")
1523
1.22k
    || zend_string_equals_literal_ci(name, "Generator");
1524
2.14k
}
1525
1526
static void zend_mark_function_as_generator(void) /* {{{ */
1527
82.4k
{
1528
82.4k
  if (!CG(active_op_array)->function_name) {
1529
78
    zend_error_noreturn(E_COMPILE_ERROR,
1530
78
      "The \"yield\" expression can only be used inside a function");
1531
78
  }
1532
1533
82.3k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1534
1.02k
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1535
1.02k
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1536
1.02k
    if (!valid_type) {
1537
1.01k
      const zend_type *single_type;
1538
3.15k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1539
3.15k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1540
2.14k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1541
984
          valid_type = true;
1542
984
          break;
1543
984
        }
1544
3.15k
      } ZEND_TYPE_FOREACH_END();
1545
1.01k
    }
1546
1547
1.02k
    if (!valid_type) {
1548
27
      zend_string *str = zend_type_to_string(return_type);
1549
27
      zend_error_noreturn(E_COMPILE_ERROR,
1550
27
        "Generator return type must be a supertype of Generator, %s given",
1551
27
        ZSTR_VAL(str));
1552
27
    }
1553
1.02k
  }
1554
1555
82.2k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1556
82.2k
}
1557
/* }}} */
1558
1559
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1560
873
{
1561
873
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1562
873
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1563
1564
873
  ZSTR_VAL(prop_name)[0] = '\0';
1565
873
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1566
873
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1567
873
  return prop_name;
1568
873
}
1569
/* }}} */
1570
1571
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) /* {{{ */
1572
1.09k
{
1573
1.09k
  size_t class_name_len;
1574
1.09k
  size_t anonclass_src_len;
1575
1576
1.09k
  *class_name = NULL;
1577
1578
1.09k
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1579
787
    *prop_name = ZSTR_VAL(name);
1580
787
    if (prop_len) {
1581
0
      *prop_len = ZSTR_LEN(name);
1582
0
    }
1583
787
    return SUCCESS;
1584
787
  }
1585
308
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1586
0
    zend_error(E_NOTICE, "Illegal member variable name");
1587
0
    *prop_name = ZSTR_VAL(name);
1588
0
    if (prop_len) {
1589
0
      *prop_len = ZSTR_LEN(name);
1590
0
    }
1591
0
    return FAILURE;
1592
0
  }
1593
1594
308
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1595
308
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1596
0
    zend_error(E_NOTICE, "Corrupt member variable name");
1597
0
    *prop_name = ZSTR_VAL(name);
1598
0
    if (prop_len) {
1599
0
      *prop_len = ZSTR_LEN(name);
1600
0
    }
1601
0
    return FAILURE;
1602
0
  }
1603
1604
308
  *class_name = ZSTR_VAL(name) + 1;
1605
308
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1606
308
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1607
0
    class_name_len += anonclass_src_len + 1;
1608
0
  }
1609
308
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1610
308
  if (prop_len) {
1611
0
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1612
0
  }
1613
308
  return SUCCESS;
1614
308
}
1615
/* }}} */
1616
1617
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1618
0
{
1619
0
  if (zend_hash_num_elements(array) > *max_checks) {
1620
0
    return false;
1621
0
  }
1622
0
  *max_checks -= zend_hash_num_elements(array);
1623
1624
0
  zval *element;
1625
0
  ZEND_HASH_FOREACH_VAL(array, element) {
1626
0
    if (Z_TYPE_P(element) < IS_ARRAY) {
1627
0
      continue;
1628
0
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1629
0
      if (!array_is_const_ex(array, max_checks)) {
1630
0
        return false;
1631
0
      }
1632
0
    } else {
1633
0
      return false;
1634
0
    }
1635
0
  } ZEND_HASH_FOREACH_END();
1636
1637
0
  return true;
1638
0
}
1639
1640
static bool array_is_const(const zend_array *array)
1641
0
{
1642
0
  uint32_t max_checks = 50;
1643
0
  return array_is_const_ex(array, &max_checks);
1644
0
}
1645
1646
567
static bool can_ct_eval_const(const zend_constant *c) {
1647
567
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1648
110
    return 0;
1649
110
  }
1650
457
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1651
457
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1652
20
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1653
20
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1654
20
    return 1;
1655
20
  }
1656
437
  if (Z_TYPE(c->value) < IS_ARRAY
1657
437
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1658
0
    return 1;
1659
437
  } else if (Z_TYPE(c->value) == IS_ARRAY
1660
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1661
0
      && array_is_const(Z_ARR(c->value))) {
1662
0
    return 1;
1663
0
  }
1664
437
  return 0;
1665
437
}
1666
1667
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1668
10.6M
{
1669
  /* Substitute true, false and null (including unqualified usage in namespaces)
1670
   * before looking up the possibly namespaced name. */
1671
10.6M
  const char *lookup_name = ZSTR_VAL(name);
1672
10.6M
  size_t lookup_len = ZSTR_LEN(name);
1673
1674
10.6M
  if (!is_fully_qualified) {
1675
10.6M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1676
10.6M
  }
1677
1678
10.6M
  zend_constant *c;
1679
10.6M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1680
32.0k
    ZVAL_COPY_VALUE(zv, &c->value);
1681
32.0k
    return 1;
1682
32.0k
  }
1683
10.5M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1684
10.5M
  if (c && can_ct_eval_const(c)) {
1685
20
    ZVAL_COPY_OR_DUP(zv, &c->value);
1686
20
    return 1;
1687
20
  }
1688
10.5M
  return 0;
1689
10.5M
}
1690
/* }}} */
1691
1692
static inline bool zend_is_scope_known(void) /* {{{ */
1693
65.2k
{
1694
65.2k
  if (!CG(active_op_array)) {
1695
    /* This can only happen when evaluating a default value string. */
1696
0
    return 0;
1697
0
  }
1698
1699
65.2k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1700
    /* Closures can be rebound to a different scope */
1701
56.1k
    return 0;
1702
56.1k
  }
1703
1704
9.09k
  if (!CG(active_class_entry)) {
1705
    /* The scope is known if we're in a free function (no scope), but not if we're in
1706
     * a file/eval (which inherits including/eval'ing scope). */
1707
1.83k
    return CG(active_op_array)->function_name != NULL;
1708
1.83k
  }
1709
1710
  /* For traits self etc refers to the using class, not the trait itself */
1711
7.25k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1712
9.09k
}
1713
/* }}} */
1714
1715
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1716
97.6k
{
1717
97.6k
  if (!CG(active_class_entry)) {
1718
93.9k
    return 0;
1719
93.9k
  }
1720
3.70k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1721
825
    return 1;
1722
825
  }
1723
2.88k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1724
2.20k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1725
3.70k
}
1726
/* }}} */
1727
1728
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1729
4.27M
{
1730
4.27M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1731
34.6k
    return ZEND_FETCH_CLASS_SELF;
1732
4.23M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1733
5.67k
    return ZEND_FETCH_CLASS_PARENT;
1734
4.23M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1735
964
    return ZEND_FETCH_CLASS_STATIC;
1736
4.22M
  } else {
1737
4.22M
    return ZEND_FETCH_CLASS_DEFAULT;
1738
4.22M
  }
1739
4.27M
}
1740
/* }}} */
1741
1742
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1743
558k
{
1744
  /* Fully qualified names are always default refs */
1745
558k
  if (name_ast->attr == ZEND_NAME_FQ) {
1746
948
    return ZEND_FETCH_CLASS_DEFAULT;
1747
948
  }
1748
1749
557k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1750
558k
}
1751
/* }}} */
1752
1753
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1754
94.3k
{
1755
94.3k
  zend_string *class_name = zend_ast_get_str(ast);
1756
94.3k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1757
1
    zend_error_noreturn(E_COMPILE_ERROR,
1758
1
      "Cannot use \"%s\" as %s, as it is reserved",
1759
1
      ZSTR_VAL(class_name), type);
1760
1
  }
1761
94.3k
  return zend_resolve_class_name(class_name, ast->attr);
1762
94.3k
}
1763
1764
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1765
34.0k
{
1766
34.0k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1767
2.27k
    zend_class_entry *ce = CG(active_class_entry);
1768
2.27k
    if (!ce) {
1769
6
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1770
6
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1771
6
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1772
2.27k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1773
5
      zend_error_noreturn(E_COMPILE_ERROR,
1774
5
        "Cannot use \"parent\" when current class scope has no parent");
1775
5
    }
1776
2.27k
  }
1777
34.0k
}
1778
/* }}} */
1779
1780
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1781
4.68k
{
1782
4.68k
  uint32_t fetch_type;
1783
4.68k
  const zval *class_name;
1784
1785
4.68k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1786
1.08k
    return 0;
1787
1.08k
  }
1788
1789
3.59k
  class_name = zend_ast_get_zval(class_ast);
1790
1791
3.59k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1792
1
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1793
1
  }
1794
1795
3.59k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1796
3.59k
  zend_ensure_valid_class_fetch_type(fetch_type);
1797
1798
3.59k
  switch (fetch_type) {
1799
1.10k
    case ZEND_FETCH_CLASS_SELF:
1800
1.10k
      if (CG(active_class_entry) && zend_is_scope_known()) {
1801
333
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1802
333
        return 1;
1803
333
      }
1804
775
      return 0;
1805
1.67k
    case ZEND_FETCH_CLASS_PARENT:
1806
1.67k
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1807
395
          && zend_is_scope_known()) {
1808
395
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1809
395
        return 1;
1810
395
      }
1811
1.27k
      return 0;
1812
143
    case ZEND_FETCH_CLASS_STATIC:
1813
143
      return 0;
1814
670
    case ZEND_FETCH_CLASS_DEFAULT:
1815
670
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1816
670
      return 1;
1817
3.59k
    EMPTY_SWITCH_DEFAULT_CASE()
1818
3.59k
  }
1819
3.59k
}
1820
/* }}} */
1821
1822
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1823
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1824
0
{
1825
0
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1826
0
    return 0;
1827
0
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1828
    /* This condition is only met on directly accessing trait constants,
1829
     * because the ce is replaced to the class entry of the composing class
1830
     * on binding. */
1831
0
    return 0;
1832
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1833
0
    return 1;
1834
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1835
0
    return c->ce == scope;
1836
0
  } else {
1837
0
    zend_class_entry *ce = c->ce;
1838
0
    while (1) {
1839
0
      if (ce == scope) {
1840
0
        return 1;
1841
0
      }
1842
0
      if (!ce->parent) {
1843
0
        break;
1844
0
      }
1845
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1846
0
        ce = ce->parent;
1847
0
      } else {
1848
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1849
0
        if (!ce) {
1850
0
          break;
1851
0
        }
1852
0
      }
1853
0
    }
1854
    /* Reverse case cannot be true during compilation */
1855
0
    return 0;
1856
0
  }
1857
0
}
1858
1859
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1860
97.6k
{
1861
97.6k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1862
97.6k
  zend_class_constant *cc;
1863
97.6k
  zval *c;
1864
1865
97.6k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1866
866
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1867
96.7k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1868
642
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1869
642
    if (ce) {
1870
10
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1871
632
    } else {
1872
632
      return 0;
1873
632
    }
1874
96.1k
  } else {
1875
96.1k
    return 0;
1876
96.1k
  }
1877
1878
876
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1879
850
    return 0;
1880
850
  }
1881
1882
26
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1883
26
    return 0;
1884
26
  }
1885
1886
0
  c = &cc->value;
1887
1888
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1889
0
  if (Z_TYPE_P(c) < IS_ARRAY) {
1890
0
    ZVAL_COPY_OR_DUP(zv, c);
1891
0
    return 1;
1892
0
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1893
0
    ZVAL_COPY_OR_DUP(zv, c);
1894
0
    return 1;
1895
0
  }
1896
1897
0
  return 0;
1898
0
}
1899
/* }}} */
1900
1901
static void zend_add_to_list(void *result, void *item) /* {{{ */
1902
143k
{
1903
143k
  void** list = *(void**)result;
1904
143k
  size_t n = 0;
1905
1906
143k
  if (list) {
1907
360k
    while (list[n]) {
1908
263k
      n++;
1909
263k
    }
1910
96.7k
  }
1911
1912
143k
  list = erealloc(list, sizeof(void*) * (n+2));
1913
1914
143k
  list[n]   = item;
1915
143k
  list[n+1] = NULL;
1916
1917
143k
  *(void**)result = list;
1918
143k
}
1919
/* }}} */
1920
1921
static void zend_do_extended_stmt(znode* result) /* {{{ */
1922
1.99M
{
1923
1.99M
  zend_op *opline;
1924
1925
1.99M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1926
1.99M
    return;
1927
1.99M
  }
1928
1929
0
  opline = get_next_op();
1930
1931
0
  opline->opcode = ZEND_EXT_STMT;
1932
0
  if (result) {
1933
0
    SET_NODE(opline->op1, result);
1934
0
  }
1935
0
}
1936
/* }}} */
1937
1938
static void zend_do_extended_fcall_begin(void) /* {{{ */
1939
4.36M
{
1940
4.36M
  zend_op *opline;
1941
1942
4.36M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1943
4.36M
    return;
1944
4.36M
  }
1945
1946
0
  opline = get_next_op();
1947
1948
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1949
0
}
1950
/* }}} */
1951
1952
static void zend_do_extended_fcall_end(void) /* {{{ */
1953
4.36M
{
1954
4.36M
  zend_op *opline;
1955
1956
4.36M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1957
4.36M
    return;
1958
4.36M
  }
1959
1960
0
  opline = get_next_op();
1961
1962
0
  opline->opcode = ZEND_EXT_FCALL_END;
1963
0
}
1964
/* }}} */
1965
1966
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1967
0
  zend_auto_global *auto_global;
1968
1969
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1970
0
    if (auto_global->armed) {
1971
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1972
0
    }
1973
0
    return 1;
1974
0
  }
1975
0
  return 0;
1976
0
}
1977
/* }}} */
1978
1979
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1980
2.27M
{
1981
2.27M
  zend_auto_global *auto_global;
1982
1983
2.27M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1984
4.86k
    if (auto_global->armed) {
1985
87
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1986
87
    }
1987
4.86k
    return 1;
1988
4.86k
  }
1989
2.27M
  return 0;
1990
2.27M
}
1991
/* }}} */
1992
1993
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1994
16
{
1995
16
  zend_auto_global auto_global;
1996
16
  zend_result retval;
1997
1998
16
  auto_global.name = name;
1999
16
  auto_global.auto_global_callback = auto_global_callback;
2000
16
  auto_global.jit = jit;
2001
2002
16
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2003
2004
16
  return retval;
2005
16
}
2006
/* }}} */
2007
2008
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2009
53.5k
{
2010
53.5k
  zend_auto_global *auto_global;
2011
2012
964k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2013
964k
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2014
964k
  } ZEND_HASH_FOREACH_END();
2015
2016
964k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2017
964k
    if (auto_global->armed && !auto_global->jit) {
2018
214k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2019
214k
    }
2020
964k
  } ZEND_HASH_FOREACH_END();
2021
53.5k
}
2022
/* }}} */
2023
2024
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2025
4.49M
{
2026
4.49M
  zval zv;
2027
4.49M
  int ret;
2028
2029
4.49M
  if (CG(increment_lineno)) {
2030
6.00k
    CG(zend_lineno)++;
2031
6.00k
    CG(increment_lineno) = 0;
2032
6.00k
  }
2033
2034
4.49M
  ret = lex_scan(&zv, elem);
2035
4.49M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2036
4.49M
  return ret;
2037
2038
4.49M
}
2039
/* }}} */
2040
2041
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2042
144k
{
2043
144k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2044
2045
144k
  ce->refcount = 1;
2046
144k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2047
144k
  ce->ce_flags2 = 0;
2048
2049
144k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2050
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2051
0
  }
2052
2053
144k
  ce->default_properties_table = NULL;
2054
144k
  ce->default_static_members_table = NULL;
2055
144k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2056
144k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2057
144k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2058
2059
144k
  ce->doc_comment = NULL;
2060
2061
144k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2062
144k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2063
2064
144k
  ce->default_object_handlers = &std_object_handlers;
2065
144k
  ce->default_properties_count = 0;
2066
144k
  ce->default_static_members_count = 0;
2067
144k
  ce->properties_info_table = NULL;
2068
144k
  ce->attributes = NULL;
2069
144k
  ce->enum_backing_type = IS_UNDEF;
2070
144k
  ce->backed_enum_table = NULL;
2071
2072
144k
  if (nullify_handlers) {
2073
144k
    ce->constructor = NULL;
2074
144k
    ce->destructor = NULL;
2075
144k
    ce->clone = NULL;
2076
144k
    ce->__get = NULL;
2077
144k
    ce->__set = NULL;
2078
144k
    ce->__unset = NULL;
2079
144k
    ce->__isset = NULL;
2080
144k
    ce->__call = NULL;
2081
144k
    ce->__callstatic = NULL;
2082
144k
    ce->__tostring = NULL;
2083
144k
    ce->__serialize = NULL;
2084
144k
    ce->__unserialize = NULL;
2085
144k
    ce->__debugInfo = NULL;
2086
144k
    ce->create_object = NULL;
2087
144k
    ce->get_iterator = NULL;
2088
144k
    ce->iterator_funcs_ptr = NULL;
2089
144k
    ce->arrayaccess_funcs_ptr = NULL;
2090
144k
    ce->get_static_method = NULL;
2091
144k
    ce->parent = NULL;
2092
144k
    ce->parent_name = NULL;
2093
144k
    ce->num_interfaces = 0;
2094
144k
    ce->interfaces = NULL;
2095
144k
    ce->num_traits = 0;
2096
144k
    ce->num_hooked_props = 0;
2097
144k
    ce->num_hooked_prop_variance_checks = 0;
2098
144k
    ce->trait_names = NULL;
2099
144k
    ce->trait_aliases = NULL;
2100
144k
    ce->trait_precedences = NULL;
2101
144k
    ce->serialize = NULL;
2102
144k
    ce->unserialize = NULL;
2103
144k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2104
0
      ce->info.internal.module = NULL;
2105
0
      ce->info.internal.builtin_functions = NULL;
2106
0
    }
2107
144k
  }
2108
144k
}
2109
/* }}} */
2110
2111
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2112
0
{
2113
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2114
0
}
2115
/* }}} */
2116
2117
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2118
0
{
2119
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2120
0
  zend_string *left = Z_STR_P(left_zv);
2121
0
  zend_string *right = zend_ast_get_str(right_ast);
2122
2123
0
  zend_string *result;
2124
0
  size_t left_len = ZSTR_LEN(left);
2125
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2126
2127
0
  result = zend_string_extend(left, len, 0);
2128
0
  ZSTR_VAL(result)[left_len] = '\\';
2129
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2130
0
  ZSTR_VAL(result)[len] = '\0';
2131
0
  zend_string_release_ex(right, 0);
2132
2133
0
  ZVAL_STR(left_zv, result);
2134
0
  return left_ast;
2135
0
}
2136
/* }}} */
2137
2138
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2139
1.27k
{
2140
1.27k
  zval *zv = zend_ast_get_zval(ast);
2141
1.27k
  if (Z_TYPE_P(zv) == IS_LONG) {
2142
1.03k
    if (Z_LVAL_P(zv) == 0) {
2143
429
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2144
610
    } else {
2145
610
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2146
610
      Z_LVAL_P(zv) *= -1;
2147
610
    }
2148
1.03k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2149
239
    size_t orig_len = Z_STRLEN_P(zv);
2150
239
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2151
239
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2152
239
    Z_STRVAL_P(zv)[0] = '-';
2153
239
  } else {
2154
0
    ZEND_UNREACHABLE();
2155
0
  }
2156
1.27k
  return ast;
2157
1.27k
}
2158
/* }}} */
2159
2160
static void zend_verify_namespace(void) /* {{{ */
2161
173k
{
2162
173k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2163
23
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2164
23
  }
2165
173k
}
2166
/* }}} */
2167
2168
/* {{{ zend_dirname
2169
   Returns directory name component of path */
2170
ZEND_API size_t zend_dirname(char *path, size_t len)
2171
1.10k
{
2172
1.10k
  char *end = path + len - 1;
2173
1.10k
  unsigned int len_adjust = 0;
2174
2175
#ifdef ZEND_WIN32
2176
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2177
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2178
   */
2179
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2180
    /* Skip over the drive spec (if any) so as not to change */
2181
    path += 2;
2182
    len_adjust += 2;
2183
    if (2 == len) {
2184
      /* Return "c:" on Win32 for dirname("c:").
2185
       * It would be more consistent to return "c:."
2186
       * but that would require making the string *longer*.
2187
       */
2188
      return len;
2189
    }
2190
  }
2191
#endif
2192
2193
1.10k
  if (len == 0) {
2194
    /* Illegal use of this function */
2195
0
    return 0;
2196
0
  }
2197
2198
  /* Strip trailing slashes */
2199
1.10k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2200
0
    end--;
2201
0
  }
2202
1.10k
  if (end < path) {
2203
    /* The path only contained slashes */
2204
0
    path[0] = DEFAULT_SLASH;
2205
0
    path[1] = '\0';
2206
0
    return 1 + len_adjust;
2207
0
  }
2208
2209
  /* Strip filename */
2210
12.1k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2211
11.0k
    end--;
2212
11.0k
  }
2213
1.10k
  if (end < path) {
2214
    /* No slash found, therefore return '.' */
2215
1.10k
    path[0] = '.';
2216
1.10k
    path[1] = '\0';
2217
1.10k
    return 1 + len_adjust;
2218
1.10k
  }
2219
2220
  /* Strip slashes which came before the file name */
2221
0
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2222
0
    end--;
2223
0
  }
2224
0
  if (end < path) {
2225
0
    path[0] = DEFAULT_SLASH;
2226
0
    path[1] = '\0';
2227
0
    return 1 + len_adjust;
2228
0
  }
2229
0
  *(end+1) = '\0';
2230
2231
0
  return (size_t)(end + 1 - path) + len_adjust;
2232
0
}
2233
/* }}} */
2234
2235
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2236
615k
{
2237
615k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2238
2239
615k
  switch (type) {
2240
303k
    case BP_VAR_R:
2241
303k
      opline->result_type = IS_TMP_VAR;
2242
303k
      result->op_type = IS_TMP_VAR;
2243
303k
      return;
2244
138k
    case BP_VAR_W:
2245
138k
      opline->opcode += 1 * factor;
2246
138k
      return;
2247
9.11k
    case BP_VAR_RW:
2248
9.11k
      opline->opcode += 2 * factor;
2249
9.11k
      return;
2250
121k
    case BP_VAR_IS:
2251
121k
      opline->result_type = IS_TMP_VAR;
2252
121k
      result->op_type = IS_TMP_VAR;
2253
121k
      opline->opcode += 3 * factor;
2254
121k
      return;
2255
36.1k
    case BP_VAR_FUNC_ARG:
2256
36.1k
      opline->opcode += 4 * factor;
2257
36.1k
      return;
2258
6.07k
    case BP_VAR_UNSET:
2259
6.07k
      opline->opcode += 5 * factor;
2260
6.07k
      return;
2261
615k
    EMPTY_SWITCH_DEFAULT_CASE()
2262
615k
  }
2263
615k
}
2264
/* }}} */
2265
2266
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2267
5.19M
{
2268
5.19M
  opline->result_type = IS_VAR;
2269
5.19M
  opline->result.var = get_temporary_variable();
2270
5.19M
  GET_NODE(result, opline->result);
2271
5.19M
}
2272
/* }}} */
2273
2274
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2275
36.6M
{
2276
36.6M
  opline->result_type = IS_TMP_VAR;
2277
36.6M
  opline->result.var = get_temporary_variable();
2278
36.6M
  GET_NODE(result, opline->result);
2279
36.6M
}
2280
/* }}} */
2281
2282
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2283
39.2M
{
2284
39.2M
  zend_op *opline = get_next_op();
2285
39.2M
  opline->opcode = opcode;
2286
2287
39.2M
  if (op1 != NULL) {
2288
31.6M
    SET_NODE(opline->op1, op1);
2289
31.6M
  }
2290
2291
39.2M
  if (op2 != NULL) {
2292
2.18M
    SET_NODE(opline->op2, op2);
2293
2.18M
  }
2294
2295
39.2M
  if (result) {
2296
4.78M
    zend_make_var_result(result, opline);
2297
4.78M
  }
2298
39.2M
  return opline;
2299
39.2M
}
2300
/* }}} */
2301
2302
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2303
39.5M
{
2304
39.5M
  zend_op *opline = get_next_op();
2305
39.5M
  opline->opcode = opcode;
2306
2307
39.5M
  if (op1 != NULL) {
2308
9.26M
    SET_NODE(opline->op1, op1);
2309
9.26M
  }
2310
2311
39.5M
  if (op2 != NULL) {
2312
3.12M
    SET_NODE(opline->op2, op2);
2313
3.12M
  }
2314
2315
39.5M
  if (result) {
2316
36.6M
    zend_make_tmp_result(result, opline);
2317
36.6M
  }
2318
2319
39.5M
  return opline;
2320
39.5M
}
2321
/* }}} */
2322
2323
static void zend_emit_tick(void) /* {{{ */
2324
136k
{
2325
136k
  zend_op *opline;
2326
2327
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2328
136k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2329
3.58k
    return;
2330
3.58k
  }
2331
2332
133k
  opline = get_next_op();
2333
2334
133k
  opline->opcode = ZEND_TICKS;
2335
133k
  opline->extended_value = FC(declarables).ticks;
2336
133k
}
2337
/* }}} */
2338
2339
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2340
244k
{
2341
244k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2342
244k
}
2343
/* }}} */
2344
2345
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2346
890k
{
2347
890k
  uint32_t opnum = get_next_op_number();
2348
890k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2349
890k
  opline->op1.opline_num = opnum_target;
2350
890k
  return opnum;
2351
890k
}
2352
/* }}} */
2353
2354
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2355
105k
{
2356
105k
  switch (opline->opcode) {
2357
1.79k
    case ZEND_IS_IDENTICAL:
2358
1.99k
    case ZEND_IS_NOT_IDENTICAL:
2359
37.1k
    case ZEND_IS_EQUAL:
2360
37.3k
    case ZEND_IS_NOT_EQUAL:
2361
62.0k
    case ZEND_IS_SMALLER:
2362
62.7k
    case ZEND_IS_SMALLER_OR_EQUAL:
2363
91.5k
    case ZEND_CASE:
2364
93.6k
    case ZEND_CASE_STRICT:
2365
93.8k
    case ZEND_ISSET_ISEMPTY_CV:
2366
93.8k
    case ZEND_ISSET_ISEMPTY_VAR:
2367
93.8k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2368
93.9k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2369
94.0k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2370
94.2k
    case ZEND_INSTANCEOF:
2371
94.3k
    case ZEND_TYPE_CHECK:
2372
94.3k
    case ZEND_DEFINED:
2373
94.4k
    case ZEND_IN_ARRAY:
2374
94.4k
    case ZEND_ARRAY_KEY_EXISTS:
2375
94.4k
      return 1;
2376
10.6k
    default:
2377
10.6k
      return 0;
2378
105k
  }
2379
105k
}
2380
/* }}} */
2381
2382
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2383
145k
{
2384
145k
  uint32_t opnum = get_next_op_number();
2385
145k
  zend_op *opline;
2386
2387
145k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2388
107k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2389
107k
    if (opline->result_type == IS_TMP_VAR
2390
105k
     && opline->result.var == cond->u.op.var
2391
105k
     && zend_is_smart_branch(opline)) {
2392
94.4k
      if (opcode == ZEND_JMPZ) {
2393
25.8k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2394
68.6k
      } else {
2395
68.6k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2396
68.6k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2397
68.6k
      }
2398
94.4k
    }
2399
107k
  }
2400
145k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2401
145k
  opline->op2.opline_num = opnum_target;
2402
145k
  return opnum;
2403
145k
}
2404
/* }}} */
2405
2406
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2407
1.06M
{
2408
1.06M
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2409
1.06M
  switch (opline->opcode) {
2410
888k
    case ZEND_JMP:
2411
888k
      opline->op1.opline_num = opnum_target;
2412
888k
      break;
2413
62.2k
    case ZEND_JMPZ:
2414
130k
    case ZEND_JMPNZ:
2415
137k
    case ZEND_JMPZ_EX:
2416
138k
    case ZEND_JMPNZ_EX:
2417
142k
    case ZEND_JMP_SET:
2418
177k
    case ZEND_COALESCE:
2419
177k
    case ZEND_JMP_NULL:
2420
177k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2421
177k
    case ZEND_JMP_FRAMELESS:
2422
177k
      opline->op2.opline_num = opnum_target;
2423
177k
      break;
2424
1.06M
    EMPTY_SWITCH_DEFAULT_CASE()
2425
1.06M
  }
2426
1.06M
}
2427
/* }}} */
2428
2429
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2430
1.05M
{
2431
1.05M
  zend_update_jump_target(opnum_jump, get_next_op_number());
2432
1.05M
}
2433
/* }}} */
2434
2435
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2436
419k
{
2437
419k
  zend_op tmp_opline;
2438
2439
419k
  init_op(&tmp_opline);
2440
2441
419k
  tmp_opline.opcode = opcode;
2442
419k
  if (op1 != NULL) {
2443
419k
    SET_NODE(tmp_opline.op1, op1);
2444
419k
  }
2445
419k
  if (op2 != NULL) {
2446
395k
    SET_NODE(tmp_opline.op2, op2);
2447
395k
  }
2448
419k
  if (result) {
2449
417k
    zend_make_var_result(result, &tmp_opline);
2450
417k
  }
2451
2452
419k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2453
419k
  return zend_stack_top(&CG(delayed_oplines_stack));
2454
419k
}
2455
/* }}} */
2456
2457
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2458
259k
{
2459
259k
  return zend_stack_count(&CG(delayed_oplines_stack));
2460
259k
}
2461
/* }}} */
2462
2463
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2464
258k
{
2465
258k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2466
258k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2467
2468
258k
  ZEND_ASSERT(count >= offset);
2469
677k
  for (i = offset; i < count; ++i) {
2470
418k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2471
408k
      opline = get_next_op();
2472
408k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2473
408k
    } else {
2474
10.7k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2475
10.7k
    }
2476
418k
  }
2477
2478
258k
  CG(delayed_oplines_stack).top = offset;
2479
258k
  return opline;
2480
258k
}
2481
/* }}} */
2482
2483
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2484
53.0M
{
2485
53.0M
  switch (ast_kind) {
2486
367k
    case ZEND_AST_DIM:
2487
432k
    case ZEND_AST_PROP:
2488
464k
    case ZEND_AST_NULLSAFE_PROP:
2489
482k
    case ZEND_AST_STATIC_PROP:
2490
596k
    case ZEND_AST_METHOD_CALL:
2491
604k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2492
676k
    case ZEND_AST_STATIC_CALL:
2493
676k
      return 1;
2494
52.3M
    default:
2495
52.3M
      return 0;
2496
53.0M
  }
2497
53.0M
}
2498
2499
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2500
380k
{
2501
380k
  switch (ast->kind) {
2502
157k
    case ZEND_AST_DIM:
2503
173k
    case ZEND_AST_PROP:
2504
180k
    case ZEND_AST_STATIC_PROP:
2505
197k
    case ZEND_AST_METHOD_CALL:
2506
200k
    case ZEND_AST_STATIC_CALL:
2507
200k
      return zend_ast_is_short_circuited(ast->child[0]);
2508
433
    case ZEND_AST_NULLSAFE_PROP:
2509
489
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2510
489
      return 1;
2511
179k
    default:
2512
179k
      return 0;
2513
380k
  }
2514
380k
}
2515
2516
static void zend_assert_not_short_circuited(const zend_ast *ast)
2517
8.37k
{
2518
8.37k
  if (zend_ast_is_short_circuited(ast)) {
2519
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2520
6
  }
2521
8.37k
}
2522
2523
/* Mark nodes that are an inner part of a short-circuiting chain.
2524
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2525
 * We do this to avoid passing down an argument in various compile functions. */
2526
2527
686k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2528
2529
464k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2530
464k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2531
275k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2532
275k
  }
2533
464k
}
2534
2535
static uint32_t zend_short_circuiting_checkpoint(void)
2536
52.5M
{
2537
52.5M
  return zend_stack_count(&CG(short_circuiting_opnums));
2538
52.5M
}
2539
2540
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2541
52.5M
{
2542
52.5M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2543
52.1M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2544
52.5M
  if (!is_short_circuited) {
2545
52.1M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2546
52.1M
      && "Short circuiting stack should be empty");
2547
52.1M
    return;
2548
52.1M
  }
2549
2550
411k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2551
    /* Outer-most node will commit. */
2552
99.2k
    return;
2553
99.2k
  }
2554
2555
334k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2556
22.5k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2557
22.5k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2558
22.5k
    opline->op2.opline_num = get_next_op_number();
2559
22.5k
    SET_NODE(opline->result, result);
2560
22.5k
    opline->extended_value |=
2561
22.5k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2562
22.5k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2563
22.1k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2564
22.5k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2565
22.5k
  }
2566
312k
}
2567
2568
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2569
22.5k
{
2570
22.5k
  uint32_t jmp_null_opnum = get_next_op_number();
2571
22.5k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2572
22.5k
  if (opline->op1_type == IS_CONST) {
2573
728
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2574
728
  }
2575
22.5k
  if (bp_type == BP_VAR_IS) {
2576
1.54k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2577
1.54k
  }
2578
22.5k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2579
22.5k
}
2580
2581
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2582
267k
{
2583
267k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2584
267k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2585
134k
    znode memoized_result;
2586
2587
    /* Go through normal compilation */
2588
134k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2589
134k
    zend_compile_expr(result, expr);
2590
134k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2591
2592
134k
    if (result->op_type == IS_VAR) {
2593
18.2k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2594
116k
    } else if (result->op_type == IS_TMP_VAR) {
2595
108k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
108k
    } else {
2597
7.69k
      if (result->op_type == IS_CONST) {
2598
6.08k
        Z_TRY_ADDREF(result->u.constant);
2599
6.08k
      }
2600
7.69k
      memoized_result = *result;
2601
7.69k
    }
2602
2603
134k
    zend_hash_index_update_mem(
2604
134k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2605
134k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2606
133k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2607
133k
    *result = *memoized_result;
2608
133k
    if (result->op_type == IS_CONST) {
2609
5.24k
      Z_TRY_ADDREF(result->u.constant);
2610
5.24k
    }
2611
133k
  } else {
2612
0
    ZEND_UNREACHABLE();
2613
0
  }
2614
267k
}
2615
/* }}} */
2616
2617
static void zend_emit_return_type_check(
2618
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2619
32.1k
{
2620
32.1k
  zend_type type = return_info->type;
2621
32.1k
  if (ZEND_TYPE_IS_SET(type)) {
2622
32.1k
    zend_op *opline;
2623
2624
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2625
32.1k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2626
2.91k
      if (expr) {
2627
3
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2628
1
          zend_error_noreturn(E_COMPILE_ERROR,
2629
1
            "A void %s must not return a value "
2630
1
            "(did you mean \"return;\" instead of \"return null;\"?)",
2631
1
            CG(active_class_entry) != NULL ? "method" : "function");
2632
2
        } else {
2633
2
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2634
2
          CG(active_class_entry) != NULL ? "method" : "function");
2635
2
        }
2636
3
      }
2637
      /* we don't need run-time check */
2638
2.90k
      return;
2639
2.91k
    }
2640
2641
    /* `return` is illegal in a never-returning function */
2642
29.1k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2643
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2644
1
      ZEND_ASSERT(!implicit);
2645
1
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2646
1
        CG(active_class_entry) != NULL ? "method" : "function");
2647
0
      return;
2648
1
    }
2649
2650
29.1k
    if (!expr && !implicit) {
2651
2
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2652
1
        zend_error_noreturn(E_COMPILE_ERROR,
2653
1
          "A %s with return type must return a value "
2654
1
          "(did you mean \"return null;\" instead of \"return;\"?)",
2655
1
          CG(active_class_entry) != NULL ? "method" : "function");
2656
1
      } else {
2657
1
        zend_error_noreturn(E_COMPILE_ERROR,
2658
1
          "A %s with return type must return a value",
2659
1
          CG(active_class_entry) != NULL ? "method" : "function");
2660
1
      }
2661
2
    }
2662
2663
29.1k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2664
      /* we don't need run-time check for mixed return type */
2665
101
      return;
2666
101
    }
2667
2668
29.0k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2669
      /* we don't need run-time check */
2670
435
      return;
2671
435
    }
2672
2673
28.6k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2674
28.6k
    if (expr && expr->op_type == IS_CONST) {
2675
498
      opline->result_type = expr->op_type = IS_TMP_VAR;
2676
498
      opline->result.var = expr->u.op.var = get_temporary_variable();
2677
498
    }
2678
28.6k
  }
2679
32.1k
}
2680
/* }}} */
2681
2682
void zend_emit_final_return(bool return_one) /* {{{ */
2683
1.77M
{
2684
1.77M
  znode zn;
2685
1.77M
  zend_op *ret;
2686
1.77M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2687
2688
1.77M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2689
29.6k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2690
29.4k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2691
2692
29.4k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2693
741
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2694
741
      return;
2695
741
    }
2696
2697
28.7k
    zend_emit_return_type_check(NULL, return_info, true);
2698
28.7k
  }
2699
2700
1.77M
  zn.op_type = IS_CONST;
2701
1.77M
  if (return_one) {
2702
23.7k
    ZVAL_LONG(&zn.u.constant, 1);
2703
1.75M
  } else {
2704
1.75M
    ZVAL_NULL(&zn.u.constant);
2705
1.75M
  }
2706
2707
1.77M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2708
1.77M
  ret->extended_value = -1;
2709
1.77M
}
2710
/* }}} */
2711
2712
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2713
5.90M
{
2714
5.90M
  return ast->kind == ZEND_AST_VAR
2715
5.88M
    || ast->kind == ZEND_AST_DIM
2716
5.85M
    || ast->kind == ZEND_AST_PROP
2717
5.85M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2718
5.84M
    || ast->kind == ZEND_AST_STATIC_PROP;
2719
5.90M
}
2720
/* }}} */
2721
2722
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2723
6.17M
{
2724
6.17M
  return ast->kind == ZEND_AST_CALL
2725
6.01M
    || ast->kind == ZEND_AST_METHOD_CALL
2726
5.99M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2727
5.99M
    || ast->kind == ZEND_AST_STATIC_CALL
2728
5.99M
    || ast->kind == ZEND_AST_PIPE;
2729
6.17M
}
2730
/* }}} */
2731
2732
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2733
8.35k
{
2734
8.35k
  return zend_is_variable(ast) || zend_is_call(ast);
2735
8.35k
}
2736
/* }}} */
2737
2738
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2739
199k
{
2740
199k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2741
149k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2742
148k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2743
199k
}
2744
/* }}} */
2745
2746
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2747
7.50k
{
2748
7.50k
  while (
2749
14.4k
    ast->kind == ZEND_AST_DIM
2750
11.8k
    || ast->kind == ZEND_AST_PROP
2751
7.50k
  ) {
2752
6.97k
    ast = ast->child[0];
2753
6.97k
  }
2754
2755
7.50k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2756
7.50k
}
2757
/* }}} */
2758
2759
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2760
8.43k
{
2761
8.43k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2762
0
    return 0;
2763
0
  }
2764
2765
8.43k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2766
8.43k
}
2767
/* }}} */
2768
2769
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2770
6.32k
{
2771
6.32k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2772
2.34k
    zend_ulong index;
2773
2774
2.34k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2775
616
      zval_ptr_dtor(&node->u.constant);
2776
616
      ZVAL_LONG(&node->u.constant, index);
2777
616
    }
2778
2.34k
  }
2779
6.32k
}
2780
/* }}} */
2781
2782
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2783
94.1k
{
2784
94.1k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2785
65.8k
    zend_ulong index;
2786
2787
65.8k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2788
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2789
       * See bug #63217
2790
       */
2791
26.5k
      int c = zend_add_literal(&dim_node->u.constant);
2792
26.5k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2793
26.5k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2794
26.5k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2795
26.5k
      return;
2796
26.5k
    }
2797
65.8k
  }
2798
94.1k
}
2799
/* }}} */
2800
2801
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2802
174k
{
2803
174k
  if (class_node->op_type == IS_CONST) {
2804
92.7k
    opline->op1_type = IS_CONST;
2805
92.7k
    opline->op1.constant = zend_add_class_name_literal(
2806
92.7k
      Z_STR(class_node->u.constant));
2807
92.7k
  } else {
2808
82.1k
    SET_NODE(opline->op1, class_node);
2809
82.1k
  }
2810
174k
}
2811
/* }}} */
2812
2813
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2814
187k
{
2815
187k
  uint32_t fetch_type;
2816
2817
187k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2818
74.2k
    znode name_node;
2819
2820
74.2k
    zend_compile_expr(&name_node, name_ast);
2821
2822
74.2k
    if (name_node.op_type == IS_CONST) {
2823
1.38k
      zend_string *name;
2824
2825
1.38k
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2826
4
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2827
4
      }
2828
2829
1.37k
      name = Z_STR(name_node.u.constant);
2830
1.37k
      fetch_type = zend_get_class_fetch_type(name);
2831
2832
1.37k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2833
935
        result->op_type = IS_CONST;
2834
935
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2835
935
      } else {
2836
443
        zend_ensure_valid_class_fetch_type(fetch_type);
2837
443
        result->op_type = IS_UNUSED;
2838
443
        result->u.op.num = fetch_type | fetch_flags;
2839
443
      }
2840
2841
1.37k
      zend_string_release_ex(name, 0);
2842
72.9k
    } else {
2843
72.9k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2844
72.9k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2845
72.9k
    }
2846
74.2k
    return;
2847
74.2k
  }
2848
2849
  /* Fully qualified names are always default refs */
2850
113k
  if (name_ast->attr == ZEND_NAME_FQ) {
2851
3.41k
    result->op_type = IS_CONST;
2852
3.41k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2853
3.41k
    return;
2854
3.41k
  }
2855
2856
110k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2857
110k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2858
96.1k
    result->op_type = IS_CONST;
2859
96.1k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2860
96.1k
  } else {
2861
13.9k
    zend_ensure_valid_class_fetch_type(fetch_type);
2862
13.9k
    result->op_type = IS_UNUSED;
2863
13.9k
    result->u.op.num = fetch_type | fetch_flags;
2864
13.9k
  }
2865
110k
}
2866
/* }}} */
2867
2868
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2869
405k
{
2870
405k
  zend_ast *name_ast = ast->child[0];
2871
405k
  if (name_ast->kind == ZEND_AST_ZVAL) {
2872
308k
    zval *zv = zend_ast_get_zval(name_ast);
2873
308k
    zend_string *name;
2874
2875
308k
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2876
283k
      name = zval_make_interned_string(zv);
2877
283k
    } else {
2878
24.7k
      name = zend_new_interned_string(zval_get_string_func(zv));
2879
24.7k
    }
2880
2881
308k
    if (zend_is_auto_global(name)) {
2882
540
      return FAILURE;
2883
540
    }
2884
2885
308k
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2886
406
      if (type == BP_VAR_R) {
2887
319
        zend_error(E_DEPRECATED,
2888
319
          "The predefined locally scoped $http_response_header variable is deprecated,"
2889
319
          " call http_get_last_response_headers() instead");
2890
319
      } else if (type == BP_VAR_W) {
2891
20
        CG(context).has_assigned_to_http_response_header = true;
2892
20
      }
2893
406
    }
2894
2895
308k
    result->op_type = IS_CV;
2896
308k
    result->u.op.var = lookup_cv(name);
2897
2898
308k
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2899
24.7k
      zend_string_release_ex(name, 0);
2900
24.7k
    }
2901
2902
308k
    return SUCCESS;
2903
308k
  }
2904
2905
97.3k
  return FAILURE;
2906
405k
}
2907
/* }}} */
2908
2909
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2910
203k
{
2911
203k
  zend_ast *name_ast = ast->child[0];
2912
203k
  znode name_node;
2913
203k
  zend_op *opline;
2914
2915
203k
  zend_compile_expr(&name_node, name_ast);
2916
203k
  if (name_node.op_type == IS_CONST) {
2917
110k
    convert_to_string(&name_node.u.constant);
2918
110k
  }
2919
2920
203k
  if (delayed) {
2921
15.6k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2922
188k
  } else {
2923
188k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2924
188k
  }
2925
2926
203k
  if (name_node.op_type == IS_CONST &&
2927
110k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2928
2929
531
    opline->extended_value = ZEND_FETCH_GLOBAL;
2930
203k
  } else {
2931
    // TODO: Have a test case for this?
2932
203k
    if (name_node.op_type == IS_CONST
2933
110k
      && type == BP_VAR_R
2934
108k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2935
162
      zend_error(E_DEPRECATED,
2936
162
        "The predefined locally scoped $http_response_header variable is deprecated,"
2937
162
        " call http_get_last_response_headers() instead");
2938
162
    }
2939
203k
    opline->extended_value = ZEND_FETCH_LOCAL;
2940
203k
  }
2941
2942
203k
  zend_adjust_for_fetch_type(opline, result, type);
2943
203k
  return opline;
2944
203k
}
2945
/* }}} */
2946
2947
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2948
633k
{
2949
633k
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2950
415k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2951
415k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2952
415k
  }
2953
2954
218k
  return 0;
2955
633k
}
2956
/* }}} */
2957
2958
static bool is_globals_fetch(const zend_ast *ast)
2959
6.78M
{
2960
6.78M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2961
438k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2962
438k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2963
438k
  }
2964
2965
6.34M
  return 0;
2966
6.78M
}
2967
2968
static bool is_global_var_fetch(const zend_ast *ast)
2969
136k
{
2970
136k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2971
136k
}
2972
2973
static bool this_guaranteed_exists(void) /* {{{ */
2974
14.0k
{
2975
14.0k
  const zend_oparray_context *ctx = &CG(context);
2976
98.1k
  while (ctx) {
2977
    /* Instance methods always have a $this.
2978
     * This also includes closures that have a scope and use $this. */
2979
98.1k
    const zend_op_array *op_array = ctx->op_array;
2980
98.1k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2981
80
      return false;
2982
98.0k
    } else if (op_array->scope) {
2983
2.17k
      return true;
2984
95.9k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2985
11.8k
      return false;
2986
11.8k
    }
2987
84.0k
    ctx = ctx->prev;
2988
84.0k
  }
2989
0
  return false;
2990
14.0k
}
2991
/* }}} */
2992
2993
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2994
398k
{
2995
398k
  if (is_this_fetch(ast)) {
2996
7.08k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2997
7.08k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
2998
7.01k
      opline->result_type = IS_TMP_VAR;
2999
7.01k
      result->op_type = IS_TMP_VAR;
3000
7.01k
    }
3001
7.08k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3002
7.08k
    return opline;
3003
391k
  } else if (is_globals_fetch(ast)) {
3004
2.47k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3005
2.47k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3006
2.16k
      opline->result_type = IS_TMP_VAR;
3007
2.16k
      result->op_type = IS_TMP_VAR;
3008
2.16k
    }
3009
2.47k
    return opline;
3010
388k
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3011
94.4k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3012
94.4k
  }
3013
294k
  return NULL;
3014
398k
}
3015
/* }}} */
3016
3017
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3018
383k
{
3019
383k
  if (type != BP_VAR_R
3020
282k
   && type != BP_VAR_IS
3021
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3022
166k
   && type != BP_VAR_FUNC_ARG
3023
132k
   && zend_is_call(ast)) {
3024
19.1k
    if (node->op_type == IS_VAR) {
3025
19.1k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3026
19.1k
      opline->result_type = IS_VAR;
3027
19.1k
      opline->result.var = opline->op1.var;
3028
19.1k
    } else {
3029
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3030
8
    }
3031
19.1k
  }
3032
383k
}
3033
/* }}} */
3034
3035
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3036
4.95k
{
3037
4.95k
  znode dummy_node;
3038
4.95k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3039
4.95k
    zend_ast_create_znode(value_node));
3040
4.95k
  zend_compile_expr(&dummy_node, assign_ast);
3041
4.95k
  zend_do_free(&dummy_node);
3042
4.95k
}
3043
/* }}} */
3044
3045
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3046
333k
{
3047
333k
  zend_ast *var_ast = ast->child[0];
3048
333k
  zend_ast *dim_ast = ast->child[1];
3049
333k
  zend_op *opline;
3050
3051
333k
  znode var_node, dim_node;
3052
3053
333k
  if (is_globals_fetch(var_ast)) {
3054
2.56k
    if (dim_ast == NULL) {
3055
3
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3056
3
    }
3057
3058
2.56k
    zend_compile_expr(&dim_node, dim_ast);
3059
2.56k
    if (dim_node.op_type == IS_CONST) {
3060
1.70k
      convert_to_string(&dim_node.u.constant);
3061
1.70k
    }
3062
3063
2.56k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3064
2.56k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3065
2.56k
    zend_adjust_for_fetch_type(opline, result, type);
3066
2.56k
    return opline;
3067
331k
  } else {
3068
331k
    zend_short_circuiting_mark_inner(var_ast);
3069
331k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3070
331k
    if (opline) {
3071
202k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3072
665
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3073
202k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3074
125k
          || opline->opcode == ZEND_FETCH_DIM_RW
3075
124k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3076
113k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3077
88.7k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3078
88.7k
      }
3079
202k
    }
3080
331k
  }
3081
3082
331k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3083
3084
331k
  if (dim_ast == NULL) {
3085
14.5k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3086
267
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3087
267
    }
3088
14.3k
    if (type == BP_VAR_UNSET) {
3089
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3090
2
    }
3091
14.3k
    dim_node.op_type = IS_UNUSED;
3092
316k
  } else {
3093
316k
    zend_compile_expr(&dim_node, dim_ast);
3094
316k
  }
3095
3096
331k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3097
331k
  zend_adjust_for_fetch_type(opline, result, type);
3098
331k
  if (by_ref) {
3099
20.4k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3100
20.4k
  }
3101
3102
331k
  if (dim_node.op_type == IS_CONST) {
3103
89.2k
    zend_handle_numeric_dim(opline, &dim_node);
3104
89.2k
  }
3105
331k
  return opline;
3106
331k
}
3107
3108
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3109
129k
{
3110
129k
  uint32_t offset = zend_delayed_compile_begin();
3111
129k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3112
129k
  return zend_delayed_compile_end(offset);
3113
129k
}
3114
/* }}} */
3115
3116
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3117
64.8k
{
3118
64.8k
  zend_ast *obj_ast = ast->child[0];
3119
64.8k
  zend_ast *prop_ast = ast->child[1];
3120
3121
64.8k
  znode obj_node, prop_node;
3122
64.8k
  zend_op *opline;
3123
64.8k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3124
3125
64.8k
  if (is_this_fetch(obj_ast)) {
3126
12.4k
    if (this_guaranteed_exists()) {
3127
787
      obj_node.op_type = IS_UNUSED;
3128
11.6k
    } else {
3129
11.6k
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3130
11.6k
    }
3131
12.4k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3132
3133
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3134
     * check for a nullsafe access. */
3135
52.4k
  } else {
3136
52.4k
    zend_short_circuiting_mark_inner(obj_ast);
3137
52.4k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3138
52.4k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3139
23.0k
        || opline->opcode == ZEND_FETCH_DIM_RW
3140
22.9k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3141
22.4k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3142
4.20k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3143
4.20k
    }
3144
3145
52.4k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3146
52.4k
    if (nullsafe) {
3147
18.6k
      if (obj_node.op_type == IS_TMP_VAR) {
3148
        /* Flush delayed oplines */
3149
16.2k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3150
16.2k
        uint32_t var = obj_node.u.op.var;
3151
16.2k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3152
16.2k
        uint32_t i = count;
3153
3154
61.3k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3155
47.1k
          i--;
3156
47.1k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3157
45.0k
            var = oplines[i].op1.var;
3158
45.0k
          } else {
3159
2.04k
            break;
3160
2.04k
          }
3161
47.1k
        }
3162
63.3k
        for (; i < count; ++i) {
3163
47.1k
          if (oplines[i].opcode != ZEND_NOP) {
3164
10.7k
            opline = get_next_op();
3165
10.7k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3166
10.7k
            oplines[i].opcode = ZEND_NOP;
3167
10.7k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3168
10.7k
          }
3169
47.1k
        }
3170
16.2k
      }
3171
18.6k
      zend_emit_jmp_null(&obj_node, type);
3172
18.6k
    }
3173
52.4k
  }
3174
3175
64.8k
  zend_compile_expr(&prop_node, prop_ast);
3176
3177
64.8k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3178
64.8k
  if (opline->op2_type == IS_CONST) {
3179
63.5k
    convert_to_string(CT_CONSTANT(opline->op2));
3180
63.5k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3181
63.5k
    opline->extended_value = zend_alloc_cache_slots(3);
3182
63.5k
  }
3183
3184
64.8k
  zend_adjust_for_fetch_type(opline, result, type);
3185
3186
64.8k
  return opline;
3187
64.8k
}
3188
/* }}} */
3189
3190
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3191
39.5k
{
3192
39.5k
  uint32_t offset = zend_delayed_compile_begin();
3193
39.5k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3194
39.5k
  if (by_ref) { /* shared with cache_slot */
3195
1.56k
    opline->extended_value |= ZEND_FETCH_REF;
3196
1.56k
  }
3197
39.5k
  return zend_delayed_compile_end(offset);
3198
39.5k
}
3199
/* }}} */
3200
3201
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3202
13.8k
{
3203
13.8k
  zend_ast *class_ast = ast->child[0];
3204
13.8k
  zend_ast *prop_ast = ast->child[1];
3205
3206
13.8k
  znode class_node, prop_node;
3207
13.8k
  zend_op *opline;
3208
3209
13.8k
  zend_short_circuiting_mark_inner(class_ast);
3210
13.8k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3211
3212
13.8k
  zend_compile_expr(&prop_node, prop_ast);
3213
3214
13.8k
  if (delayed) {
3215
5.55k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3216
8.27k
  } else {
3217
8.27k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3218
8.27k
  }
3219
13.8k
  if (opline->op1_type == IS_CONST) {
3220
12.3k
    convert_to_string(CT_CONSTANT(opline->op1));
3221
12.3k
    opline->extended_value = zend_alloc_cache_slots(3);
3222
12.3k
  }
3223
13.8k
  if (class_node.op_type == IS_CONST) {
3224
7.65k
    opline->op2_type = IS_CONST;
3225
7.65k
    opline->op2.constant = zend_add_class_name_literal(
3226
7.65k
      Z_STR(class_node.u.constant));
3227
7.65k
    if (opline->op1_type != IS_CONST) {
3228
1.12k
      opline->extended_value = zend_alloc_cache_slot();
3229
1.12k
    }
3230
7.65k
  } else {
3231
6.16k
    SET_NODE(opline->op2, &class_node);
3232
6.16k
  }
3233
3234
13.8k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3235
1.10k
    opline->extended_value |= ZEND_FETCH_REF;
3236
1.10k
  }
3237
3238
13.8k
  zend_adjust_for_fetch_type(opline, result, type);
3239
13.8k
  return opline;
3240
13.8k
}
3241
/* }}} */
3242
3243
5.03k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3244
5.03k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3245
246
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3246
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3247
1
    }
3248
245
    if (array_style != var_ast->attr) {
3249
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3250
1
    }
3251
4.78k
  } else if (!zend_can_write_to_variable(var_ast)) {
3252
68
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3253
68
  }
3254
5.03k
}
3255
/* }}} */
3256
3257
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3258
3259
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3260
3.31k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3261
3.31k
  const zend_ast_list *list = zend_ast_get_list(ast);
3262
3.31k
  bool has_refs = false;
3263
3.31k
  uint32_t i;
3264
3265
16.0k
  for (i = 0; i < list->children; ++i) {
3266
12.7k
    zend_ast *elem_ast = list->child[i];
3267
3268
12.7k
    if (elem_ast) {
3269
5.78k
      zend_ast *var_ast = elem_ast->child[0];
3270
5.78k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3271
402
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3272
402
      }
3273
5.78k
      has_refs |= elem_ast->attr;
3274
5.78k
    }
3275
12.7k
  }
3276
3277
3.31k
  return has_refs;
3278
3.31k
}
3279
/* }}} */
3280
3281
static bool list_is_keyed(const zend_ast_list *list)
3282
2.71k
{
3283
3.52k
  for (uint32_t i = 0; i < list->children; i++) {
3284
3.48k
    const zend_ast *child = list->child[i];
3285
3.48k
    if (child) {
3286
2.68k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3287
2.68k
    }
3288
3.48k
  }
3289
32
  return false;
3290
2.71k
}
3291
3292
static void zend_compile_list_assign(
3293
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3294
2.71k
{
3295
2.71k
  zend_ast_list *list = zend_ast_get_list(ast);
3296
2.71k
  uint32_t i;
3297
2.71k
  bool has_elems = false;
3298
2.71k
  bool is_keyed = list_is_keyed(list);
3299
3300
2.71k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3301
349
    zval_make_interned_string(&expr_node->u.constant);
3302
349
  }
3303
3304
8.70k
  for (i = 0; i < list->children; ++i) {
3305
5.99k
    zend_ast *elem_ast = list->child[i];
3306
5.99k
    zend_ast *var_ast, *key_ast;
3307
5.99k
    znode fetch_result, dim_node;
3308
5.99k
    zend_op *opline;
3309
3310
5.99k
    if (elem_ast == NULL) {
3311
955
      if (is_keyed) {
3312
1
        zend_error(E_COMPILE_ERROR,
3313
1
          "Cannot use empty array entries in keyed array assignment");
3314
954
      } else {
3315
954
        continue;
3316
954
      }
3317
955
    }
3318
3319
5.03k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3320
1
      zend_error(E_COMPILE_ERROR,
3321
1
          "Spread operator is not supported in assignments");
3322
1
    }
3323
3324
5.03k
    var_ast = elem_ast->child[0];
3325
5.03k
    key_ast = elem_ast->child[1];
3326
5.03k
    has_elems = true;
3327
3328
5.03k
    if (is_keyed) {
3329
491
      if (key_ast == NULL) {
3330
2
        zend_error(E_COMPILE_ERROR,
3331
2
          "Cannot mix keyed and unkeyed array entries in assignments");
3332
2
      }
3333
3334
491
      zend_compile_expr(&dim_node, key_ast);
3335
4.54k
    } else {
3336
4.54k
      if (key_ast != NULL) {
3337
1
        zend_error(E_COMPILE_ERROR,
3338
1
          "Cannot mix keyed and unkeyed array entries in assignments");
3339
1
      }
3340
3341
4.54k
      dim_node.op_type = IS_CONST;
3342
4.54k
      ZVAL_LONG(&dim_node.u.constant, i);
3343
4.54k
    }
3344
3345
5.03k
    if (expr_node->op_type == IS_CONST) {
3346
1.26k
      Z_TRY_ADDREF(expr_node->u.constant);
3347
1.26k
    }
3348
3349
5.03k
    zend_verify_list_assign_target(var_ast, array_style);
3350
3351
5.03k
    opline = zend_emit_op(&fetch_result,
3352
5.03k
      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);
3353
3354
5.03k
    if (dim_node.op_type == IS_CONST) {
3355
4.94k
      zend_handle_numeric_dim(opline, &dim_node);
3356
4.94k
    }
3357
3358
5.03k
    if (elem_ast->attr) {
3359
963
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3360
963
    }
3361
5.03k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3362
244
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3363
4.79k
    } else if (elem_ast->attr) {
3364
963
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3365
3.83k
    } else {
3366
3.83k
      zend_emit_assign_znode(var_ast, &fetch_result);
3367
3.83k
    }
3368
5.03k
  }
3369
3370
2.71k
  if (has_elems == 0) {
3371
32
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3372
32
  }
3373
3374
2.68k
  if (result) {
3375
2.02k
    *result = *expr_node;
3376
2.02k
  } else {
3377
661
    zend_do_free(expr_node);
3378
661
  }
3379
2.68k
}
3380
/* }}} */
3381
3382
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3383
135k
{
3384
135k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3385
33
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3386
33
  }
3387
135k
  if (
3388
135k
    ast->kind == ZEND_AST_METHOD_CALL
3389
135k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3390
135k
    || ast->kind == ZEND_AST_STATIC_CALL
3391
135k
  ) {
3392
5
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3393
5
  }
3394
135k
  if (zend_ast_is_short_circuited(ast)) {
3395
6
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3396
6
  }
3397
135k
  if (is_globals_fetch(ast)) {
3398
13
    zend_error_noreturn(E_COMPILE_ERROR,
3399
13
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3400
13
  }
3401
135k
}
3402
/* }}} */
3403
3404
/* Detects $a... = $a pattern */
3405
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3406
39.1k
{
3407
39.1k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3408
34.5k
    return 0;
3409
34.5k
  }
3410
3411
9.83k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3412
5.29k
    var_ast = var_ast->child[0];
3413
5.29k
  }
3414
3415
4.54k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3416
1.03k
    return 0;
3417
1.03k
  }
3418
3419
3.51k
  {
3420
3.51k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3421
3.51k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3422
3.51k
    bool result = zend_string_equals(name1, name2);
3423
3.51k
    zend_string_release_ex(name1, 0);
3424
3.51k
    zend_string_release_ex(name2, 0);
3425
3.51k
    return result;
3426
4.54k
  }
3427
4.54k
}
3428
/* }}} */
3429
3430
static void zend_compile_expr_with_potential_assign_to_self(
3431
39.1k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3432
39.1k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3433
    /* $a[0] = $a should evaluate the right $a first */
3434
1.35k
    znode cv_node;
3435
3436
1.35k
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3437
144
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3438
1.21k
    } else {
3439
1.21k
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3440
1.21k
    }
3441
37.7k
  } else {
3442
37.7k
    zend_compile_expr(expr_node, expr_ast);
3443
37.7k
  }
3444
39.1k
}
3445
3446
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3447
80.6k
{
3448
80.6k
  zend_ast *var_ast = ast->child[0];
3449
80.6k
  zend_ast *expr_ast = ast->child[1];
3450
3451
80.6k
  znode var_node, expr_node;
3452
80.6k
  zend_op *opline;
3453
80.6k
  uint32_t offset;
3454
80.6k
  if (is_this_fetch(var_ast)) {
3455
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3456
1
  }
3457
3458
80.6k
  zend_ensure_writable_variable(var_ast);
3459
3460
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3461
80.6k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3462
80.6k
  switch (kind) {
3463
68.3k
    case ZEND_AST_VAR:
3464
68.3k
      offset = zend_delayed_compile_begin();
3465
68.3k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3466
68.3k
      zend_compile_expr(&expr_node, expr_ast);
3467
68.3k
      zend_delayed_compile_end(offset);
3468
68.3k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3469
68.3k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3470
68.3k
      return;
3471
2.13k
    case ZEND_AST_STATIC_PROP:
3472
2.13k
      offset = zend_delayed_compile_begin();
3473
2.13k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3474
2.13k
      zend_compile_expr(&expr_node, expr_ast);
3475
3476
2.13k
      opline = zend_delayed_compile_end(offset);
3477
2.13k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3478
2.13k
      opline->result_type = IS_TMP_VAR;
3479
2.13k
      result->op_type = IS_TMP_VAR;
3480
3481
2.13k
      zend_emit_op_data(&expr_node);
3482
2.13k
      return;
3483
4.56k
    case ZEND_AST_DIM:
3484
4.56k
      offset = zend_delayed_compile_begin();
3485
4.56k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3486
4.56k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3487
3488
4.56k
      opline = zend_delayed_compile_end(offset);
3489
4.56k
      opline->opcode = ZEND_ASSIGN_DIM;
3490
4.56k
      opline->result_type = IS_TMP_VAR;
3491
4.56k
      result->op_type = IS_TMP_VAR;
3492
3493
4.56k
      opline = zend_emit_op_data(&expr_node);
3494
4.56k
      return;
3495
2.94k
    case ZEND_AST_PROP:
3496
2.94k
    case ZEND_AST_NULLSAFE_PROP:
3497
2.94k
      offset = zend_delayed_compile_begin();
3498
2.94k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3499
2.94k
      zend_compile_expr(&expr_node, expr_ast);
3500
3501
2.94k
      opline = zend_delayed_compile_end(offset);
3502
2.94k
      opline->opcode = ZEND_ASSIGN_OBJ;
3503
2.94k
      opline->result_type = IS_TMP_VAR;
3504
2.94k
      result->op_type = IS_TMP_VAR;
3505
3506
2.94k
      zend_emit_op_data(&expr_node);
3507
2.94k
      return;
3508
2.56k
    case ZEND_AST_ARRAY:
3509
2.56k
      if (zend_propagate_list_refs(var_ast)) {
3510
852
        if (!zend_is_variable_or_call(expr_ast)) {
3511
5
          zend_error_noreturn(E_COMPILE_ERROR,
3512
5
            "Cannot assign reference to non referenceable value");
3513
847
        } else {
3514
847
          zend_assert_not_short_circuited(expr_ast);
3515
847
        }
3516
3517
847
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3518
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3519
         * self-assignments, this forces the RHS to evaluate first. */
3520
847
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3521
1.71k
      } else {
3522
1.71k
        if (expr_ast->kind == ZEND_AST_VAR) {
3523
          /* list($a, $b) = $a should evaluate the right $a first */
3524
315
          znode cv_node;
3525
3526
315
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3527
237
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3528
237
          } else {
3529
78
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3530
78
          }
3531
1.40k
        } else {
3532
1.40k
          zend_compile_expr(&expr_node, expr_ast);
3533
1.40k
        }
3534
1.71k
      }
3535
3536
2.56k
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3537
2.56k
      return;
3538
0
    EMPTY_SWITCH_DEFAULT_CASE();
3539
80.6k
  }
3540
80.6k
}
3541
/* }}} */
3542
3543
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3544
5.84k
{
3545
5.84k
  zend_ast *target_ast = ast->child[0];
3546
5.84k
  zend_ast *source_ast = ast->child[1];
3547
3548
5.84k
  znode target_node, source_node;
3549
5.84k
  zend_op *opline;
3550
5.84k
  uint32_t offset, flags;
3551
3552
5.84k
  if (is_this_fetch(target_ast)) {
3553
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3554
1
  }
3555
5.84k
  zend_ensure_writable_variable(target_ast);
3556
5.84k
  zend_assert_not_short_circuited(source_ast);
3557
5.84k
  if (is_globals_fetch(source_ast)) {
3558
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3559
1
  }
3560
3561
5.84k
  offset = zend_delayed_compile_begin();
3562
5.84k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3563
5.84k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3564
3565
5.84k
  if ((target_ast->kind != ZEND_AST_VAR
3566
5.18k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3567
1.76k
   && source_ast->kind != ZEND_AST_ZNODE
3568
569
   && source_node.op_type != IS_CV) {
3569
    /* Both LHS and RHS expressions may modify the same data structure,
3570
     * and the modification during RHS evaluation may dangle the pointer
3571
     * to the result of the LHS evaluation.
3572
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3573
     * See: Bug #71539
3574
     */
3575
213
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3576
213
  }
3577
3578
5.84k
  opline = zend_delayed_compile_end(offset);
3579
3580
5.84k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3581
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3582
1
  }
3583
3584
5.84k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3585
3586
5.84k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3587
357
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3588
357
    opline->extended_value &= ~ZEND_FETCH_REF;
3589
357
    opline->extended_value |= flags;
3590
357
    zend_emit_op_data(&source_node);
3591
357
    *result = target_node;
3592
5.48k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3593
196
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3594
196
    opline->extended_value &= ~ZEND_FETCH_REF;
3595
196
    opline->extended_value |= flags;
3596
196
    zend_emit_op_data(&source_node);
3597
196
    *result = target_node;
3598
5.29k
  } else {
3599
5.29k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3600
5.29k
    opline->extended_value = flags;
3601
5.29k
  }
3602
5.84k
}
3603
/* }}} */
3604
3605
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3606
2.04k
{
3607
2.04k
  znode dummy_node;
3608
2.04k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3609
2.04k
    zend_ast_create_znode(value_node));
3610
2.04k
  zend_compile_expr(&dummy_node, assign_ast);
3611
2.04k
  zend_do_free(&dummy_node);
3612
2.04k
}
3613
/* }}} */
3614
3615
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3616
7.08k
{
3617
7.08k
  zend_ast *var_ast = ast->child[0];
3618
7.08k
  zend_ast *expr_ast = ast->child[1];
3619
7.08k
  uint32_t opcode = ast->attr;
3620
3621
7.08k
  znode var_node, expr_node;
3622
7.08k
  zend_op *opline;
3623
7.08k
  uint32_t offset, cache_slot;
3624
3625
7.08k
  zend_ensure_writable_variable(var_ast);
3626
3627
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3628
7.08k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3629
7.08k
  switch (kind) {
3630
3.43k
    case ZEND_AST_VAR:
3631
3.43k
      offset = zend_delayed_compile_begin();
3632
3.43k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3633
3.43k
      zend_compile_expr(&expr_node, expr_ast);
3634
3.43k
      zend_delayed_compile_end(offset);
3635
3.43k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3636
3.43k
      opline->extended_value = opcode;
3637
3.43k
      return;
3638
608
    case ZEND_AST_STATIC_PROP:
3639
608
      offset = zend_delayed_compile_begin();
3640
608
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3641
608
      zend_compile_expr(&expr_node, expr_ast);
3642
3643
608
      opline = zend_delayed_compile_end(offset);
3644
608
      cache_slot = opline->extended_value;
3645
608
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3646
608
      opline->extended_value = opcode;
3647
608
      opline->result_type = IS_TMP_VAR;
3648
608
      result->op_type = IS_TMP_VAR;
3649
3650
608
      opline = zend_emit_op_data(&expr_node);
3651
608
      opline->extended_value = cache_slot;
3652
608
      return;
3653
2.28k
    case ZEND_AST_DIM:
3654
2.28k
      offset = zend_delayed_compile_begin();
3655
2.28k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3656
2.28k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3657
3658
2.28k
      opline = zend_delayed_compile_end(offset);
3659
2.28k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3660
2.28k
      opline->extended_value = opcode;
3661
2.28k
      opline->result_type = IS_TMP_VAR;
3662
2.28k
      result->op_type = IS_TMP_VAR;
3663
3664
2.28k
      zend_emit_op_data(&expr_node);
3665
2.28k
      return;
3666
752
    case ZEND_AST_PROP:
3667
752
    case ZEND_AST_NULLSAFE_PROP:
3668
752
      offset = zend_delayed_compile_begin();
3669
752
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3670
752
      zend_compile_expr(&expr_node, expr_ast);
3671
3672
752
      opline = zend_delayed_compile_end(offset);
3673
752
      cache_slot = opline->extended_value;
3674
752
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3675
752
      opline->extended_value = opcode;
3676
752
      opline->result_type = IS_TMP_VAR;
3677
752
      result->op_type = IS_TMP_VAR;
3678
3679
752
      opline = zend_emit_op_data(&expr_node);
3680
752
      opline->extended_value = cache_slot;
3681
752
      return;
3682
7.08k
    EMPTY_SWITCH_DEFAULT_CASE()
3683
7.08k
  }
3684
7.08k
}
3685
/* }}} */
3686
3687
3.27k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3688
  // TODO: Caching?
3689
10.8k
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3690
8.11k
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3691
8.11k
    if (zend_string_equals(arg_info->name, arg_name)) {
3692
522
      return i + 1;
3693
522
    }
3694
8.11k
  }
3695
3696
  /* Either an invalid argument name, or collected into a variadic argument. */
3697
2.75k
  return (uint32_t) -1;
3698
3.27k
}
3699
3700
static uint32_t zend_compile_args(
3701
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3702
4.37M
{
3703
4.37M
  const zend_ast_list *args = zend_ast_get_list(ast);
3704
4.37M
  uint32_t i;
3705
4.37M
  bool uses_arg_unpack = false;
3706
4.37M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3707
3708
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3709
   * May not actually use named argument passing. */
3710
4.37M
  bool uses_named_args = false;
3711
  /* Whether there may be any undef arguments due to the use of named arguments. */
3712
4.37M
  bool may_have_undef = false;
3713
  /* Whether there may be any extra named arguments collected into a variadic. */
3714
4.37M
  *may_have_extra_named_args = false;
3715
3716
10.3M
  for (i = 0; i < args->children; ++i) {
3717
6.02M
    zend_ast *arg = args->child[i];
3718
6.02M
    zend_string *arg_name = NULL;
3719
6.02M
    uint32_t arg_num = i + 1;
3720
3721
6.02M
    znode arg_node;
3722
6.02M
    zend_op *opline;
3723
6.02M
    uint8_t opcode;
3724
3725
6.02M
    if (arg->kind == ZEND_AST_UNPACK) {
3726
1.19k
      if (uses_named_args) {
3727
1
        zend_error_noreturn(E_COMPILE_ERROR,
3728
1
          "Cannot use argument unpacking after named arguments");
3729
1
      }
3730
3731
      /* Unpack may contain named arguments. */
3732
1.19k
      may_have_undef = true;
3733
1.19k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3734
908
        *may_have_extra_named_args = true;
3735
908
      }
3736
3737
1.19k
      uses_arg_unpack = true;
3738
1.19k
      fbc = NULL;
3739
3740
1.19k
      zend_compile_expr(&arg_node, arg->child[0]);
3741
1.19k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3742
1.19k
      opline->op2.num = arg_count;
3743
1.19k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3744
3745
1.19k
      continue;
3746
1.19k
    }
3747
3748
6.02M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3749
31.3k
      uses_named_args = true;
3750
31.3k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3751
31.3k
      arg = arg->child[1];
3752
3753
31.3k
      if (fbc && !uses_arg_unpack) {
3754
3.27k
        arg_num = zend_get_arg_num(fbc, arg_name);
3755
3.27k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3756
          /* Using named arguments, but passing in order. */
3757
98
          arg_name = NULL;
3758
98
          arg_count++;
3759
3.18k
        } else {
3760
          // TODO: We could track which arguments were passed, even if out of order.
3761
3.18k
          may_have_undef = true;
3762
3.18k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3763
36
            *may_have_extra_named_args = true;
3764
36
          }
3765
3.18k
        }
3766
28.0k
      } else {
3767
28.0k
        arg_num = (uint32_t) -1;
3768
28.0k
        may_have_undef = true;
3769
28.0k
        *may_have_extra_named_args = true;
3770
28.0k
      }
3771
5.98M
    } else {
3772
5.98M
      if (uses_arg_unpack) {
3773
5
        zend_error_noreturn(E_COMPILE_ERROR,
3774
5
          "Cannot use positional argument after argument unpacking");
3775
5
      }
3776
3777
5.98M
      if (uses_named_args) {
3778
23
        zend_error_noreturn(E_COMPILE_ERROR,
3779
23
          "Cannot use positional argument after named argument");
3780
23
      }
3781
3782
5.98M
      arg_count++;
3783
5.98M
    }
3784
3785
    /* Treat passing of $GLOBALS the same as passing a call.
3786
     * This will error at runtime if the argument is by-ref. */
3787
6.02M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3788
158k
      zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3789
158k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3790
        /* Function call was converted into builtin instruction */
3791
4.70k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3792
3.56k
          opcode = ZEND_SEND_VAL_EX;
3793
3.56k
        } else {
3794
1.14k
          opcode = ZEND_SEND_VAL;
3795
1.14k
        }
3796
153k
      } else {
3797
153k
        if (fbc && arg_num != (uint32_t) -1) {
3798
15.6k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3799
201
            opcode = ZEND_SEND_VAR_NO_REF;
3800
15.4k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3801
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3802
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3803
             * and a by-value pass if it returned by-value. */
3804
81
            opcode = ZEND_SEND_VAL;
3805
15.3k
          } else {
3806
15.3k
            opcode = ZEND_SEND_VAR;
3807
15.3k
          }
3808
137k
        } else {
3809
137k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3810
137k
        }
3811
153k
      }
3812
5.86M
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3813
28.1k
      if (fbc && arg_num != (uint32_t) -1) {
3814
1.44k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3815
115
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3816
115
          opcode = ZEND_SEND_REF;
3817
1.32k
        } else {
3818
1.32k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3819
1.32k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3820
1.32k
        }
3821
26.7k
      } else {
3822
26.7k
        do {
3823
26.7k
          if (arg->kind == ZEND_AST_VAR) {
3824
3.68k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3825
3.68k
            if (is_this_fetch(arg)) {
3826
700
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3827
700
              opcode = ZEND_SEND_VAR_EX;
3828
700
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3829
700
              break;
3830
2.98k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3831
2.67k
              opcode = ZEND_SEND_VAR_EX;
3832
2.67k
              break;
3833
2.67k
            }
3834
3.68k
          }
3835
23.3k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3836
23.3k
          if (arg_name) {
3837
9.03k
            opline->op2_type = IS_CONST;
3838
9.03k
            zend_string_addref(arg_name);
3839
9.03k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3840
9.03k
            opline->result.num = zend_alloc_cache_slots(2);
3841
14.3k
          } else {
3842
14.3k
            opline->op2.num = arg_num;
3843
14.3k
          }
3844
23.3k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3845
23.3k
          opcode = ZEND_SEND_FUNC_ARG;
3846
23.3k
        } while (0);
3847
26.7k
      }
3848
5.83M
    } else {
3849
5.83M
      zend_compile_expr(&arg_node, arg);
3850
5.83M
      if (arg_node.op_type == IS_VAR) {
3851
        /* pass ++$a or something similar */
3852
783k
        if (fbc && arg_num != (uint32_t) -1) {
3853
1.72k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3854
18
            opcode = ZEND_SEND_VAR_NO_REF;
3855
1.70k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3856
201
            opcode = ZEND_SEND_VAL;
3857
1.50k
          } else {
3858
1.50k
            opcode = ZEND_SEND_VAR;
3859
1.50k
          }
3860
781k
        } else {
3861
781k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3862
781k
        }
3863
5.05M
      } else if (arg_node.op_type == IS_CV) {
3864
0
        if (fbc && arg_num != (uint32_t) -1) {
3865
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3866
0
            opcode = ZEND_SEND_REF;
3867
0
          } else {
3868
0
            opcode = ZEND_SEND_VAR;
3869
0
          }
3870
0
        } else {
3871
0
          opcode = ZEND_SEND_VAR_EX;
3872
0
        }
3873
5.05M
      } else {
3874
        /* Delay "Only variables can be passed by reference" error to execution */
3875
5.05M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3876
41.5k
          opcode = ZEND_SEND_VAL;
3877
5.00M
        } else {
3878
5.00M
          opcode = ZEND_SEND_VAL_EX;
3879
5.00M
        }
3880
5.05M
      }
3881
5.83M
    }
3882
3883
6.02M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3884
6.02M
    if (arg_name) {
3885
31.1k
      opline->op2_type = IS_CONST;
3886
31.1k
      zend_string_addref(arg_name);
3887
31.1k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3888
31.1k
      opline->result.num = zend_alloc_cache_slots(2);
3889
5.98M
    } else {
3890
5.98M
      opline->op2.opline_num = arg_num;
3891
5.98M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3892
5.98M
    }
3893
6.02M
  }
3894
3895
4.37M
  if (may_have_undef) {
3896
23.8k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3897
23.8k
  }
3898
3899
4.37M
  return arg_count;
3900
4.37M
}
3901
/* }}} */
3902
3903
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3904
4.36M
{
3905
4.36M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3906
3907
4.36M
  if (fbc && init_op->opcode != ZEND_NEW) {
3908
31.0k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3909
31.0k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3910
29.6k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3911
29.2k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3912
29.1k
          return ZEND_DO_ICALL;
3913
29.1k
        } else {
3914
107
          return ZEND_DO_FCALL_BY_NAME;
3915
107
        }
3916
29.2k
      }
3917
29.6k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3918
1.45k
      if (zend_execute_ex == execute_ex) {
3919
1.45k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3920
1.43k
          return ZEND_DO_UCALL;
3921
1.43k
        } else {
3922
17
          return ZEND_DO_FCALL_BY_NAME;
3923
17
        }
3924
1.45k
      }
3925
1.45k
    }
3926
4.33M
  } else if (zend_execute_ex == execute_ex &&
3927
4.33M
             !zend_execute_internal &&
3928
4.33M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3929
4.26M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3930
4.00M
    return ZEND_DO_FCALL_BY_NAME;
3931
4.00M
  }
3932
327k
  return ZEND_DO_FCALL;
3933
4.36M
}
3934
/* }}} */
3935
3936
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno) /* {{{ */
3937
4.37M
{
3938
4.37M
  zend_op *opline;
3939
4.37M
  uint32_t opnum_init = get_next_op_number() - 1;
3940
3941
4.37M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3942
4.06k
    opline = &CG(active_op_array)->opcodes[opnum_init];
3943
4.06k
    opline->extended_value = 0;
3944
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
3945
4.06k
    uint8_t opcode = opline->opcode;
3946
3947
4.06k
    if (opcode == ZEND_NEW) {
3948
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3949
8
    }
3950
3951
4.05k
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
3952
4.05k
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
3953
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
3954
10
    }
3955
3956
4.04k
    if (opcode == ZEND_INIT_FCALL) {
3957
495
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3958
495
    }
3959
3960
4.04k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3961
4.04k
    if (opcode == ZEND_INIT_FCALL
3962
3.54k
     || opcode == ZEND_INIT_FCALL_BY_NAME
3963
3.62k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
3964
3.62k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
3965
3.62k
    } else {
3966
418
      callable_convert_op->extended_value = (uint32_t)-1;
3967
418
    }
3968
4.04k
    return true;
3969
4.05k
  }
3970
3971
4.37M
  bool may_have_extra_named_args;
3972
4.37M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3973
3974
4.37M
  zend_do_extended_fcall_begin();
3975
3976
4.37M
  opline = &CG(active_op_array)->opcodes[opnum_init];
3977
4.37M
  opline->extended_value = arg_count;
3978
3979
4.37M
  if (opline->opcode == ZEND_INIT_FCALL) {
3980
30.6k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3981
30.6k
  }
3982
3983
4.37M
  uint8_t call_op = zend_get_call_op(
3984
4.37M
    opline,
3985
4.37M
    fbc,
3986
    /* result_used: At this point we do not yet reliably
3987
     * know if the result is used. Deoptimize #[\NoDiscard]
3988
     * calls to be sure. The optimizer will fix this up.
3989
     */
3990
4.37M
    false
3991
4.37M
  );
3992
4.37M
  opline = zend_emit_op(result, call_op, NULL, NULL);
3993
4.37M
  if (may_have_extra_named_args) {
3994
22.3k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
3995
22.3k
  }
3996
4.37M
  opline->lineno = lineno;
3997
4.37M
  zend_do_extended_fcall_end();
3998
4.37M
  return false;
3999
4.37M
}
4000
/* }}} */
4001
4002
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4003
4.07M
{
4004
4.07M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4005
4.07M
  bool is_fully_qualified;
4006
4007
4.07M
  name_node->op_type = IS_CONST;
4008
4.07M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4009
4.07M
    orig_name, name_ast->attr, &is_fully_qualified));
4010
4011
4.07M
  return !is_fully_qualified && FC(current_namespace);
4012
4.07M
}
4013
/* }}} */
4014
4015
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
4016
304k
{
4017
304k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4018
68.6k
    const char *colon;
4019
68.6k
    zend_string *str = Z_STR(name_node->u.constant);
4020
68.6k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4021
455
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4022
455
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4023
455
      zend_op *opline = get_next_op();
4024
4025
455
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4026
455
      opline->op1_type = IS_CONST;
4027
455
      opline->op1.constant = zend_add_class_name_literal(class);
4028
455
      opline->op2_type = IS_CONST;
4029
455
      opline->op2.constant = zend_add_func_name_literal(method);
4030
      /* 2 slots, for class and method */
4031
455
      opline->result.num = zend_alloc_cache_slots(2);
4032
455
      zval_ptr_dtor(&name_node->u.constant);
4033
68.2k
    } else {
4034
68.2k
      zend_op *opline = get_next_op();
4035
4036
68.2k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4037
68.2k
      opline->op2_type = IS_CONST;
4038
68.2k
      opline->op2.constant = zend_add_func_name_literal(str);
4039
68.2k
      opline->result.num = zend_alloc_cache_slot();
4040
68.2k
    }
4041
235k
  } else {
4042
235k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4043
235k
  }
4044
4045
304k
  zend_compile_call_common(result, args_ast, NULL, lineno);
4046
304k
}
4047
/* }}} */
4048
4049
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4050
3.96M
{
4051
3.96M
  uint32_t i;
4052
9.60M
  for (i = 0; i < args->children; ++i) {
4053
5.66M
    const zend_ast *arg = args->child[i];
4054
5.66M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4055
19.7k
      return 1;
4056
19.7k
    }
4057
5.66M
  }
4058
3.94M
  return 0;
4059
3.96M
}
4060
/* }}} */
4061
4062
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4063
930
{
4064
930
  znode arg_node;
4065
4066
930
  if (args->children != 1) {
4067
206
    return FAILURE;
4068
206
  }
4069
4070
724
  zend_compile_expr(&arg_node, args->child[0]);
4071
724
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4072
173
    result->op_type = IS_CONST;
4073
173
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4074
173
    zval_ptr_dtor_str(&arg_node.u.constant);
4075
551
  } else {
4076
551
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4077
551
  }
4078
724
  return SUCCESS;
4079
930
}
4080
/* }}} */
4081
4082
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4083
1.28k
{
4084
1.28k
  znode arg_node;
4085
1.28k
  zend_op *opline;
4086
4087
1.28k
  if (args->children != 1) {
4088
857
    return FAILURE;
4089
857
  }
4090
4091
424
  zend_compile_expr(&arg_node, args->child[0]);
4092
424
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4093
424
  if (type != _IS_BOOL) {
4094
409
    opline->extended_value = (1 << type);
4095
409
  } else {
4096
15
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4097
15
  }
4098
424
  return SUCCESS;
4099
1.28k
}
4100
/* }}} */
4101
4102
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4103
114
{
4104
114
  znode arg_node;
4105
114
  zend_op *opline;
4106
4107
114
  if (args->children != 1) {
4108
13
    return FAILURE;
4109
13
  }
4110
4111
101
  zend_compile_expr(&arg_node, args->child[0]);
4112
101
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4113
101
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4114
101
  return SUCCESS;
4115
114
}
4116
4117
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4118
808
{
4119
808
  znode arg_node;
4120
808
  zend_op *opline;
4121
4122
808
  if (args->children != 1) {
4123
356
    return FAILURE;
4124
356
  }
4125
4126
452
  zend_compile_expr(&arg_node, args->child[0]);
4127
452
  if (type == _IS_BOOL) {
4128
198
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4129
254
  } else {
4130
254
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4131
254
    opline->extended_value = type;
4132
254
  }
4133
452
  return SUCCESS;
4134
808
}
4135
/* }}} */
4136
4137
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4138
2.09k
{
4139
2.09k
  zend_string *name;
4140
2.09k
  zend_op *opline;
4141
4142
2.09k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4143
413
    return FAILURE;
4144
413
  }
4145
4146
1.68k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4147
1.68k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4148
274
    zend_string_release_ex(name, 0);
4149
274
    return FAILURE;
4150
274
  }
4151
4152
1.41k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4153
66
    zend_string_release_ex(name, 0);
4154
66
    zval_ptr_dtor(&result->u.constant);
4155
66
    ZVAL_TRUE(&result->u.constant);
4156
66
    result->op_type = IS_CONST;
4157
66
    return SUCCESS;
4158
66
  }
4159
4160
1.34k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4161
1.34k
  opline->op1_type = IS_CONST;
4162
1.34k
  LITERAL_STR(opline->op1, name);
4163
1.34k
  opline->extended_value = zend_alloc_cache_slot();
4164
4165
1.34k
  return SUCCESS;
4166
1.41k
}
4167
/* }}} */
4168
4169
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4170
1.20k
{
4171
1.20k
  zval *zint;
4172
1.20k
  if (
4173
1.20k
    args->children == 1
4174
987
    && args->child[0]->kind == ZEND_AST_ZVAL
4175
847
    && (zint = zend_ast_get_zval(args->child[0]))
4176
847
    && Z_TYPE_P(zint) == IS_LONG
4177
780
    && Z_LVAL_P(zint) >= 0
4178
780
    && Z_LVAL_P(zint) <= 255
4179
1.20k
  ) {
4180
512
    result->op_type = IS_CONST;
4181
512
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4182
512
    return SUCCESS;
4183
688
  } else {
4184
688
    return FAILURE;
4185
688
  }
4186
1.20k
}
4187
/* }}} */
4188
4189
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4190
704
{
4191
704
  zval *str;
4192
704
  if (
4193
704
    args->children == 1
4194
349
    && args->child[0]->kind == ZEND_AST_ZVAL
4195
283
    && (str = zend_ast_get_zval(args->child[0]))
4196
283
    && Z_TYPE_P(str) == IS_STRING
4197
214
    && Z_STRLEN_P(str) == 1
4198
704
  ) {
4199
18
    result->op_type = IS_CONST;
4200
18
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4201
18
    return SUCCESS;
4202
686
  } else {
4203
686
    return FAILURE;
4204
686
  }
4205
704
}
4206
/* }}} */
4207
4208
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4209
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4210
 * directly or indirectly recursive function calls. */
4211
61.2k
static bool fbc_is_finalized(const zend_function *fbc) {
4212
61.2k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4213
61.2k
}
4214
4215
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4216
8.45k
{
4217
8.45k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4218
1.98k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4219
6.46k
  } else {
4220
6.46k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4221
0
      && ce->info.user.filename != filename;
4222
6.46k
  }
4223
8.45k
}
4224
4225
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4226
40.9k
{
4227
40.9k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4228
39.1k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4229
39.1k
  } else {
4230
1.82k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4231
1.82k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4232
0
        && fbc->op_array.filename != filename);
4233
1.82k
  }
4234
40.9k
}
4235
4236
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4237
7.82k
{
4238
7.82k
  zend_string *name, *lcname;
4239
7.82k
  zend_function *fbc;
4240
7.82k
  zend_op *opline;
4241
4242
7.82k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4243
2.23k
    return FAILURE;
4244
2.23k
  }
4245
4246
5.59k
  name = zend_ast_get_str(name_ast);
4247
5.59k
  lcname = zend_string_tolower(name);
4248
4249
5.59k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4250
5.59k
  if (!fbc
4251
516
   || !fbc_is_finalized(fbc)
4252
5.08k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4253
5.08k
    zend_string_release_ex(lcname, 0);
4254
5.08k
    return FAILURE;
4255
5.08k
  }
4256
4257
516
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4258
516
  opline->extended_value = num_args;
4259
516
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4260
516
  opline->op2_type = IS_CONST;
4261
516
  LITERAL_STR(opline->op2, lcname);
4262
516
  opline->result.num = zend_alloc_cache_slot();
4263
4264
516
  return SUCCESS;
4265
5.59k
}
4266
/* }}} */
4267
4268
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4269
7.82k
{
4270
7.82k
  zend_op *opline;
4271
7.82k
  znode name_node;
4272
4273
7.82k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4274
516
    return;
4275
516
  }
4276
4277
7.31k
  zend_compile_expr(&name_node, name_ast);
4278
4279
7.31k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4280
7.31k
  opline->op1_type = IS_CONST;
4281
7.31k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4282
7.31k
  opline->extended_value = num_args;
4283
7.31k
}
4284
/* }}} */
4285
4286
/* cufa = call_user_func_array */
4287
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4288
2.07k
{
4289
2.07k
  znode arg_node;
4290
2.07k
  zend_op *opline;
4291
4292
2.07k
  if (args->children != 2) {
4293
84
    return FAILURE;
4294
84
  }
4295
4296
1.99k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4297
1.99k
  if (args->child[1]->kind == ZEND_AST_CALL
4298
1.76k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4299
1.69k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4300
1.62k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4301
1.59k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4302
1.59k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4303
1.59k
    bool is_fully_qualified;
4304
1.59k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4305
4306
1.59k
    if (zend_string_equals_literal_ci(name, "array_slice")
4307
1.09k
       && !zend_args_contain_unpack_or_named(list)
4308
957
     && list->children == 3
4309
308
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4310
227
      zval *zv = zend_ast_get_zval(list->child[1]);
4311
4312
227
      if (Z_TYPE_P(zv) == IS_LONG
4313
209
       && Z_LVAL_P(zv) >= 0
4314
209
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4315
94
        zend_op *opline;
4316
94
        znode len_node;
4317
4318
94
        zend_compile_expr(&arg_node, list->child[0]);
4319
94
        zend_compile_expr(&len_node, list->child[2]);
4320
94
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4321
94
        opline->extended_value = Z_LVAL_P(zv);
4322
94
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4323
94
        zend_string_release_ex(name, 0);
4324
94
        return SUCCESS;
4325
94
      }
4326
227
    }
4327
1.49k
    zend_string_release_ex(name, 0);
4328
1.49k
  }
4329
1.89k
  zend_compile_expr(&arg_node, args->child[1]);
4330
1.89k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4331
1.89k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4332
1.89k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4333
1.89k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4334
4335
1.89k
  return SUCCESS;
4336
1.99k
}
4337
/* }}} */
4338
4339
/* cuf = call_user_func */
4340
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname) /* {{{ */
4341
6.03k
{
4342
6.03k
  uint32_t i;
4343
4344
6.03k
  if (args->children < 1) {
4345
196
    return FAILURE;
4346
196
  }
4347
4348
5.83k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4349
6.67k
  for (i = 1; i < args->children; ++i) {
4350
834
    zend_ast *arg_ast = args->child[i];
4351
834
    znode arg_node;
4352
834
    zend_op *opline;
4353
4354
834
    zend_compile_expr(&arg_node, arg_ast);
4355
4356
834
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4357
834
    opline->op2.num = i;
4358
834
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4359
834
  }
4360
5.83k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4361
4362
5.83k
  return SUCCESS;
4363
6.03k
}
4364
/* }}} */
4365
4366
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno) /* {{{ */
4367
35.8k
{
4368
35.8k
  if (EG(assertions) >= 0) {
4369
35.8k
    znode name_node;
4370
35.8k
    zend_op *opline;
4371
35.8k
    uint32_t check_op_number = get_next_op_number();
4372
4373
35.8k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4374
4375
35.8k
    if (fbc && fbc_is_finalized(fbc)) {
4376
20.2k
      name_node.op_type = IS_CONST;
4377
20.2k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4378
4379
20.2k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4380
20.2k
    } else {
4381
15.5k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4382
15.5k
      opline->op2_type = IS_CONST;
4383
15.5k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4384
15.5k
    }
4385
35.8k
    opline->result.num = zend_alloc_cache_slot();
4386
4387
35.8k
    if (args->children == 1) {
4388
      /* add "assert(condition) as assertion message */
4389
20.5k
      zend_ast *arg = zend_ast_create_zval_from_str(
4390
20.5k
        zend_ast_export("assert(", args->child[0], ")"));
4391
20.5k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4392
        /* If the original argument was named, add the new argument as named as well,
4393
         * as mixing named and positional is not allowed. */
4394
294
        zend_ast *name = zend_ast_create_zval_from_str(
4395
294
          ZSTR_INIT_LITERAL("description", 0));
4396
294
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4397
294
      }
4398
20.5k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4399
20.5k
    }
4400
4401
35.8k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4402
4403
35.8k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4404
35.8k
    opline->op2.opline_num = get_next_op_number();
4405
35.8k
    SET_NODE(opline->result, result);
4406
35.8k
  } else {
4407
0
    if (!fbc) {
4408
0
      zend_string_release_ex(name, 0);
4409
0
    }
4410
0
    result->op_type = IS_CONST;
4411
0
    ZVAL_TRUE(&result->u.constant);
4412
0
  }
4413
35.8k
}
4414
/* }}} */
4415
4416
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4417
10.9k
{
4418
10.9k
  bool strict = false;
4419
10.9k
  znode array, needly;
4420
10.9k
  zend_op *opline;
4421
4422
10.9k
  if (args->children == 3) {
4423
9.02k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4424
960
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4425
8.06k
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4426
651
      zval value;
4427
651
      zend_ast *name_ast = args->child[2]->child[0];
4428
651
      bool is_fully_qualified;
4429
651
      zend_string *resolved_name = zend_resolve_const_name(
4430
651
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4431
4432
651
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4433
453
        zend_string_release_ex(resolved_name, 0);
4434
453
        return FAILURE;
4435
453
      }
4436
4437
198
      zend_string_release_ex(resolved_name, 0);
4438
198
      strict = zend_is_true(&value);
4439
198
      zval_ptr_dtor(&value);
4440
7.41k
    } else {
4441
7.41k
      return FAILURE;
4442
7.41k
    }
4443
9.02k
  } else if (args->children != 2) {
4444
362
    return FAILURE;
4445
362
  }
4446
4447
2.76k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4448
2.03k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4449
805
    return FAILURE;
4450
805
  }
4451
4452
1.96k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4453
1.74k
    bool ok = true;
4454
1.74k
    zval *val, tmp;
4455
1.74k
    HashTable *src = Z_ARRVAL(array.u.constant);
4456
1.74k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4457
4458
1.74k
    ZVAL_TRUE(&tmp);
4459
4460
1.74k
    if (strict) {
4461
6.13k
      ZEND_HASH_FOREACH_VAL(src, val) {
4462
6.13k
        if (Z_TYPE_P(val) == IS_STRING) {
4463
20
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4464
1.77k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4465
1.65k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4466
1.65k
        } else {
4467
124
          zend_array_destroy(dst);
4468
124
          ok = false;
4469
124
          break;
4470
124
        }
4471
6.13k
      } ZEND_HASH_FOREACH_END();
4472
930
    } else {
4473
4.60k
      ZEND_HASH_FOREACH_VAL(src, val) {
4474
4.60k
        if (Z_TYPE_P(val) != IS_STRING
4475
1.09k
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4476
591
          zend_array_destroy(dst);
4477
591
          ok = false;
4478
591
          break;
4479
591
        }
4480
916
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4481
916
      } ZEND_HASH_FOREACH_END();
4482
813
    }
4483
4484
1.74k
    zend_array_destroy(src);
4485
1.74k
    if (!ok) {
4486
715
      return FAILURE;
4487
715
    }
4488
1.02k
    Z_ARRVAL(array.u.constant) = dst;
4489
1.02k
  }
4490
1.24k
  array.op_type = IS_CONST;
4491
4492
1.24k
  zend_compile_expr(&needly, args->child[0]);
4493
4494
1.24k
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4495
1.24k
  opline->extended_value = strict;
4496
4497
1.24k
  return SUCCESS;
4498
1.96k
}
4499
/* }}} */
4500
4501
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4502
208
{
4503
208
  znode arg_node;
4504
208
  zend_op *opline;
4505
4506
208
  if (args->children != 1) {
4507
68
    return FAILURE;
4508
68
  }
4509
4510
140
  zend_compile_expr(&arg_node, args->child[0]);
4511
140
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4512
140
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4513
4514
140
  return SUCCESS;
4515
208
}
4516
/* }}} */
4517
4518
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4519
243
{
4520
243
  if (args->children == 0) {
4521
92
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4522
151
  } else {
4523
151
    znode arg_node;
4524
4525
151
    if (args->children != 1) {
4526
66
      return FAILURE;
4527
66
    }
4528
4529
85
    zend_compile_expr(&arg_node, args->child[0]);
4530
85
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4531
85
  }
4532
177
  return SUCCESS;
4533
243
}
4534
/* }}} */
4535
4536
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4537
223
{
4538
223
  if (args->children != 0) {
4539
44
    return FAILURE;
4540
44
  }
4541
4542
179
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4543
179
  return SUCCESS;
4544
223
}
4545
/* }}} */
4546
4547
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4548
103
{
4549
103
  znode arg_node;
4550
4551
103
  if (args->children != 1) {
4552
34
    return FAILURE;
4553
34
  }
4554
4555
69
  zend_compile_expr(&arg_node, args->child[0]);
4556
69
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4557
69
  return SUCCESS;
4558
103
}
4559
/* }}} */
4560
4561
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4562
113
{
4563
113
  if (CG(active_op_array)->function_name && args->children == 0) {
4564
28
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4565
28
    return SUCCESS;
4566
85
  } else {
4567
85
    return FAILURE;
4568
85
  }
4569
113
}
4570
/* }}} */
4571
4572
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4573
703
{
4574
703
  if (CG(active_op_array)->function_name && args->children == 0) {
4575
395
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4576
395
    return SUCCESS;
4577
395
  } else {
4578
308
    return FAILURE;
4579
308
  }
4580
703
}
4581
/* }}} */
4582
4583
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4584
148
{
4585
148
  znode subject, needle;
4586
4587
148
  if (args->children != 2) {
4588
66
    return FAILURE;
4589
66
  }
4590
4591
82
  zend_compile_expr(&needle, args->child[0]);
4592
82
  zend_compile_expr(&subject, args->child[1]);
4593
4594
82
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4595
82
  return SUCCESS;
4596
148
}
4597
/* }}} */
4598
4599
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4600
2.11k
{
4601
2.11k
  if (CG(active_op_array)->function_name
4602
1.13k
   && args->children == 2
4603
606
   && args->child[0]->kind == ZEND_AST_CALL
4604
551
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4605
458
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4606
458
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4607
457
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4608
4609
414
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4610
414
    bool is_fully_qualified;
4611
414
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4612
414
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4613
414
    const zval *zv = zend_ast_get_zval(args->child[1]);
4614
414
    znode first;
4615
4616
414
    if (zend_string_equals_literal_ci(name, "func_get_args")
4617
230
     && list->children == 0
4618
195
     && Z_TYPE_P(zv) == IS_LONG
4619
136
     && Z_LVAL_P(zv) >= 0) {
4620
136
      first.op_type = IS_CONST;
4621
136
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4622
136
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4623
136
      zend_string_release_ex(name, 0);
4624
136
      return SUCCESS;
4625
136
    }
4626
278
    zend_string_release_ex(name, 0);
4627
278
  }
4628
1.98k
  return FAILURE;
4629
2.11k
}
4630
/* }}} */
4631
4632
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4633
1.55M
{
4634
1.55M
  void **handlers = zend_flf_handlers;
4635
1.55M
  void **current = handlers;
4636
14.2M
  while (current) {
4637
14.2M
    if (*current == handler) {
4638
1.55M
      return current - handlers;
4639
1.55M
    }
4640
12.6M
    current++;
4641
12.6M
  }
4642
4643
0
  return (uint32_t)-1;
4644
1.55M
}
4645
4646
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4647
853k
{
4648
853k
  if (zend_execute_internal) {
4649
0
    return NULL;
4650
0
  }
4651
4652
853k
  if (type != BP_VAR_R) {
4653
1.02k
    return NULL;
4654
1.02k
  }
4655
4656
852k
  if (ZEND_USER_CODE(fbc->type)) {
4657
10
    return NULL;
4658
10
  }
4659
4660
852k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4661
852k
  if (!frameless_function_info) {
4662
27.1k
    return NULL;
4663
27.1k
  }
4664
4665
825k
  if (args->children > 3) {
4666
509
    return NULL;
4667
509
  }
4668
4669
1.12M
  while (frameless_function_info->handler) {
4670
1.07M
    if (frameless_function_info->num_args >= args->children
4671
873k
     && fbc->common.required_num_args <= args->children
4672
776k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4673
776k
      || frameless_function_info->num_args == args->children)) {
4674
776k
      uint32_t num_args = frameless_function_info->num_args;
4675
776k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4676
776k
      if (offset == (uint32_t)-1) {
4677
0
        continue;
4678
0
      }
4679
776k
      return frameless_function_info;
4680
776k
    }
4681
294k
    frameless_function_info++;
4682
294k
  }
4683
4684
48.9k
  return NULL;
4685
825k
}
4686
4687
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)
4688
775k
{
4689
775k
  uint32_t lineno = CG(zend_lineno);
4690
775k
  uint32_t num_args = frameless_function_info->num_args;
4691
775k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4692
775k
  znode arg_zvs[3];
4693
2.51M
  for (uint32_t i = 0; i < num_args; i++) {
4694
1.73M
    if (i < args->children) {
4695
1.73M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4696
1.73M
    } else {
4697
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4698
0
      arg_zvs[i].op_type = IS_CONST;
4699
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4700
0
        ZEND_UNREACHABLE();
4701
0
      }
4702
0
    }
4703
1.73M
  }
4704
775k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4705
775k
  uint32_t opnum = get_next_op_number();
4706
775k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4707
775k
  opline->extended_value = offset;
4708
775k
  opline->lineno = lineno;
4709
775k
  if (num_args >= 1) {
4710
775k
    SET_NODE(opline->op1, &arg_zvs[0]);
4711
775k
  }
4712
775k
  if (num_args >= 2) {
4713
764k
    SET_NODE(opline->op2, &arg_zvs[1]);
4714
764k
  }
4715
775k
  if (num_args >= 3) {
4716
197k
    zend_emit_op_data(&arg_zvs[2]);
4717
197k
  }
4718
775k
  return opnum;
4719
775k
}
4720
4721
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4722
21.1k
{
4723
21.1k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4724
21.1k
  if (!frameless_function_info) {
4725
11.7k
    return (uint32_t)-1;
4726
11.7k
  }
4727
4728
9.33k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4729
21.1k
}
4730
4731
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4732
3.93M
{
4733
3.93M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4734
4735
  /* Find frameless function with same name. */
4736
3.93M
  const zend_function *frameless_function = NULL;
4737
3.93M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4738
3.92M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4739
   /* Avoid blowing up op count with nested frameless branches. */
4740
3.90M
   && !CG(context).in_jmp_frameless_branch) {
4741
2.42M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4742
2.42M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4743
2.42M
  }
4744
4745
  /* Check whether any frameless handler may actually be used. */
4746
3.93M
  uint32_t jmp_fl_opnum = 0;
4747
3.93M
  const zend_frameless_function_info *frameless_function_info = NULL;
4748
3.93M
  if (frameless_function) {
4749
832k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4750
832k
    if (frameless_function_info) {
4751
766k
      CG(context).in_jmp_frameless_branch = true;
4752
766k
      znode op1;
4753
766k
      op1.op_type = IS_CONST;
4754
766k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4755
766k
      jmp_fl_opnum = get_next_op_number();
4756
766k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4757
766k
    }
4758
832k
  }
4759
4760
  /* Compile ns call. */
4761
3.93M
  zend_op *opline = get_next_op();
4762
3.93M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4763
3.93M
  opline->op2_type = IS_CONST;
4764
3.93M
  opline->op2.constant = name_constants;
4765
3.93M
  opline->result.num = zend_alloc_cache_slot();
4766
3.93M
  zend_compile_call_common(result, args_ast, NULL, lineno);
4767
4768
  /* Compile frameless call. */
4769
3.93M
  if (frameless_function_info) {
4770
766k
    CG(zend_lineno) = lineno;
4771
4772
766k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4773
766k
    uint32_t jmp_fl_target = get_next_op_number();
4774
4775
766k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4776
4777
766k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4778
766k
    jmp_fl->op2.opline_num = jmp_fl_target;
4779
766k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4780
766k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4781
766k
    SET_NODE(flf_icall->result, result);
4782
766k
    zend_update_jump_target_to_next(jmp_end_opnum);
4783
4784
766k
    CG(context).in_jmp_frameless_branch = false;
4785
766k
  }
4786
3.93M
}
4787
/* }}} */
4788
4789
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4790
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4791
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4792
4793
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4794
2.68k
{
4795
  /* Bail out if we do not have a format string. */
4796
2.68k
  if (args->children < 1) {
4797
235
    return FAILURE;
4798
235
  }
4799
4800
2.44k
  zend_eval_const_expr(&args->child[0]);
4801
  /* Bail out if the format string is not constant. */
4802
2.44k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4803
273
    return FAILURE;
4804
273
  }
4805
4806
2.17k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4807
2.17k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4808
10
    return FAILURE;
4809
10
  }
4810
2.16k
  if (Z_STRLEN_P(format_string) >= 256) {
4811
34
    return FAILURE;
4812
34
  }
4813
4814
2.13k
  char *p;
4815
2.13k
  char *end;
4816
2.13k
  uint32_t placeholder_count;
4817
4818
2.13k
  placeholder_count = 0;
4819
2.13k
  p = Z_STRVAL_P(format_string);
4820
2.13k
  end = p + Z_STRLEN_P(format_string);
4821
4822
5.03k
  for (;;) {
4823
5.03k
    p = memchr(p, '%', end - p);
4824
5.03k
    if (!p) {
4825
1.82k
      break;
4826
1.82k
    }
4827
4828
3.21k
    char *q = p + 1;
4829
3.21k
    if (q == end) {
4830
222
      return FAILURE;
4831
222
    }
4832
4833
2.99k
    switch (*q) {
4834
1.51k
      case 's':
4835
1.69k
      case 'd':
4836
1.69k
        placeholder_count++;
4837
1.69k
        break;
4838
1.21k
      case '%':
4839
1.21k
        break;
4840
90
      default:
4841
90
        return FAILURE;
4842
2.99k
    }
4843
4844
2.90k
    p = q;
4845
2.90k
    p++;
4846
2.90k
  }
4847
4848
  /* Bail out if the number of placeholders does not match the number of values. */
4849
1.82k
  if (placeholder_count != (args->children - 1)) {
4850
138
    return FAILURE;
4851
138
  }
4852
4853
  /* Handle empty format strings. */
4854
1.68k
  if (Z_STRLEN_P(format_string) == 0) {
4855
68
    result->op_type = IS_CONST;
4856
68
    ZVAL_EMPTY_STRING(&result->u.constant);
4857
4858
68
    return SUCCESS;
4859
68
  }
4860
4861
1.61k
  znode *elements = NULL;
4862
4863
1.61k
  if (placeholder_count > 0) {
4864
855
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4865
855
  }
4866
4867
  /* Compile the value expressions first for error handling that is consistent
4868
   * with a function call: Values that fail to convert to a string may emit errors.
4869
   */
4870
2.97k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4871
1.36k
    zend_compile_expr(elements + i, args->child[1 + i]);
4872
1.36k
  }
4873
4874
1.61k
  uint32_t rope_elements = 0;
4875
1.61k
  uint32_t rope_init_lineno = -1;
4876
1.61k
  zend_op *opline = NULL;
4877
4878
1.61k
  placeholder_count = 0;
4879
1.61k
  p = Z_STRVAL_P(format_string);
4880
1.61k
  end = p + Z_STRLEN_P(format_string);
4881
1.61k
  char *offset = p;
4882
3.98k
  for (;;) {
4883
3.98k
    p = memchr(p, '%', end - p);
4884
3.98k
    if (!p) {
4885
1.61k
      break;
4886
1.61k
    }
4887
4888
2.36k
    char *q = p + 1;
4889
2.36k
    ZEND_ASSERT(q < end);
4890
2.36k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4891
4892
2.36k
    if (*q == '%') {
4893
      /* Optimization to not create a dedicated rope element for the literal '%':
4894
       * Include the first '%' within the "constant" part instead of dropping the
4895
       * full placeholder.
4896
       */
4897
1.00k
      p++;
4898
1.00k
    }
4899
4900
2.36k
    if (p != offset) {
4901
1.84k
      znode const_node;
4902
1.84k
      const_node.op_type = IS_CONST;
4903
1.84k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4904
1.84k
      if (rope_elements == 0) {
4905
614
        rope_init_lineno = get_next_op_number();
4906
614
      }
4907
1.84k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4908
1.84k
    }
4909
4910
2.36k
    if (*q != '%') {
4911
1.36k
      switch (*q) {
4912
1.25k
        case 's':
4913
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4914
           * for correct error reporting.
4915
           */
4916
1.25k
          if (elements[placeholder_count].op_type == IS_CONST) {
4917
636
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4918
130
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4919
506
            } else {
4920
506
              convert_to_string(&elements[placeholder_count].u.constant);
4921
506
            }
4922
636
          }
4923
1.25k
          break;
4924
110
        case 'd':
4925
110
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4926
110
          break;
4927
0
        EMPTY_SWITCH_DEFAULT_CASE();
4928
1.36k
      }
4929
4930
1.36k
      if (rope_elements == 0) {
4931
378
        rope_init_lineno = get_next_op_number();
4932
378
      }
4933
1.36k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4934
4935
1.36k
      placeholder_count++;
4936
1.36k
    }
4937
4938
2.36k
    p = q;
4939
2.36k
    p++;
4940
2.36k
    offset = p;
4941
2.36k
  }
4942
1.61k
  if (end != offset) {
4943
    /* Add the constant part after the last placeholder. */
4944
1.02k
    znode const_node;
4945
1.02k
    const_node.op_type = IS_CONST;
4946
1.02k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4947
1.02k
    if (rope_elements == 0) {
4948
622
      rope_init_lineno = get_next_op_number();
4949
622
    }
4950
1.02k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4951
1.02k
  }
4952
1.61k
  ZEND_ASSERT(opline != NULL);
4953
4954
1.61k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4955
1.61k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4956
1.61k
  efree(elements);
4957
4958
1.61k
  return SUCCESS;
4959
1.61k
}
4960
4961
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
4962
1.08k
{
4963
  /* Special case: printf with a single constant string argument and no format specifiers.
4964
   * In this case, just emit ECHO and return the string length if needed. */
4965
1.08k
  if (args->children == 1) {
4966
822
    zend_eval_const_expr(&args->child[0]);
4967
822
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
4968
207
      return FAILURE;
4969
207
    }
4970
615
    zval *format_string = zend_ast_get_zval(args->child[0]);
4971
615
    if (Z_TYPE_P(format_string) != IS_STRING) {
4972
200
      return FAILURE;
4973
200
    }
4974
    /* Check if there are any format specifiers */
4975
415
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
4976
      /* No format specifiers - just emit ECHO and return string length */
4977
68
      znode format_node;
4978
68
      zend_compile_expr(&format_node, args->child[0]);
4979
68
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
4980
4981
      /* Return the string length as a constant if the result is used */
4982
68
      result->op_type = IS_CONST;
4983
68
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
4984
68
      return SUCCESS;
4985
68
    }
4986
415
  }
4987
4988
  /* Fall back to sprintf optimization for format strings with specifiers */
4989
610
  znode rope_result;
4990
610
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
4991
227
    return FAILURE;
4992
227
  }
4993
4994
  /* printf() returns the amount of bytes written, so just an ECHO of the
4995
   * resulting sprintf() optimisation might not be enough. At this early
4996
   * stage we can't detect if the result is actually used, so we just emit
4997
   * the opcodes and let them be cleaned up by the dead code elimination
4998
   * pass in the Zend Optimizer if the result of the printf() is in fact
4999
   * unused */
5000
383
  znode copy;
5001
383
  if (rope_result.op_type != IS_CONST) {
5002
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5003
104
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5004
104
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5005
104
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5006
104
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5007
279
  } else {
5008
279
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5009
279
    result->op_type = IS_CONST;
5010
279
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5011
279
  }
5012
5013
383
  return SUCCESS;
5014
383
}
5015
5016
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5017
1.27k
{
5018
1.27k
  znode arg_node;
5019
5020
1.27k
  if (args->children != 1) {
5021
360
    return FAILURE;
5022
360
  }
5023
5024
919
  zend_compile_expr(&arg_node, args->child[0]);
5025
919
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5026
5027
919
  return SUCCESS;
5028
1.27k
}
5029
5030
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
5031
37.6k
{
5032
37.6k
  if (zend_string_equals_literal(lcname, "strlen")) {
5033
930
    return zend_compile_func_strlen(result, args);
5034
36.7k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5035
462
    return zend_compile_func_typecheck(result, args, IS_NULL);
5036
36.3k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5037
16
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5038
36.2k
  } else if (zend_string_equals_literal(lcname, "is_long")
5039
36.2k
    || zend_string_equals_literal(lcname, "is_int")
5040
36.0k
    || zend_string_equals_literal(lcname, "is_integer")
5041
36.2k
  ) {
5042
460
    return zend_compile_func_typecheck(result, args, IS_LONG);
5043
35.8k
  } else if (zend_string_equals_literal(lcname, "is_float")
5044
35.7k
    || zend_string_equals_literal(lcname, "is_double")
5045
35.8k
  ) {
5046
100
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5047
35.7k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5048
10
    return zend_compile_func_typecheck(result, args, IS_STRING);
5049
35.7k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5050
100
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5051
35.6k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5052
67
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5053
35.5k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5054
66
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5055
35.4k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5056
114
    return zend_compile_func_is_scalar(result, args);
5057
35.3k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5058
198
    return zend_compile_func_cast(result, args, _IS_BOOL);
5059
35.1k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5060
72
    return zend_compile_func_cast(result, args, IS_LONG);
5061
35.1k
  } else if (zend_string_equals_literal(lcname, "floatval")
5062
34.8k
    || zend_string_equals_literal(lcname, "doubleval")
5063
35.1k
  ) {
5064
338
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5065
34.7k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5066
200
    return zend_compile_func_cast(result, args, IS_STRING);
5067
34.5k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5068
2.09k
    return zend_compile_func_defined(result, args);
5069
32.4k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5070
1.20k
    return zend_compile_func_chr(result, args);
5071
31.2k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5072
704
    return zend_compile_func_ord(result, args);
5073
30.5k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5074
2.07k
    return zend_compile_func_cufa(result, args, lcname);
5075
28.4k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5076
6.03k
    return zend_compile_func_cuf(result, args, lcname);
5077
22.4k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5078
10.9k
    return zend_compile_func_in_array(result, args);
5079
11.4k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5080
11.4k
      || zend_string_equals_literal(lcname, "sizeof")) {
5081
208
    return zend_compile_func_count(result, args, lcname);
5082
11.2k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5083
243
    return zend_compile_func_get_class(result, args);
5084
11.0k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5085
223
    return zend_compile_func_get_called_class(result, args);
5086
10.7k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5087
103
    return zend_compile_func_gettype(result, args);
5088
10.6k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5089
113
    return zend_compile_func_num_args(result, args);
5090
10.5k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5091
703
    return zend_compile_func_get_args(result, args);
5092
9.86k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5093
2.11k
    return zend_compile_func_array_slice(result, args);
5094
7.75k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5095
148
    return zend_compile_func_array_key_exists(result, args);
5096
7.60k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5097
2.07k
    return zend_compile_func_sprintf(result, args);
5098
5.53k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5099
1.08k
    return zend_compile_func_printf(result, args);
5100
4.44k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5101
1.27k
    return zend_compile_func_clone(result, args);
5102
3.16k
  } else {
5103
3.16k
    return FAILURE;
5104
3.16k
  }
5105
37.6k
}
5106
5107
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
5108
39.9k
{
5109
39.9k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5110
0
    return FAILURE;
5111
0
  }
5112
5113
39.9k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5114
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5115
     * function with a different implementation. Don't use the VM builtin in that case. */
5116
1.38k
    return FAILURE;
5117
1.38k
  }
5118
5119
38.5k
  if (zend_args_contain_unpack_or_named(args)) {
5120
864
    return FAILURE;
5121
864
  }
5122
5123
37.6k
  if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
5124
16.5k
    return SUCCESS;
5125
16.5k
  }
5126
5127
21.1k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5128
37.6k
}
5129
5130
2
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5131
2
  switch (kind) {
5132
1
    case ZEND_PROPERTY_HOOK_GET:
5133
1
      return "get";
5134
1
    case ZEND_PROPERTY_HOOK_SET:
5135
1
      return "set";
5136
2
    EMPTY_SWITCH_DEFAULT_CASE()
5137
2
  }
5138
2
}
5139
5140
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5141
564
{
5142
564
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5143
276
    return zend_string_copy(prop_name);
5144
288
  } else {
5145
288
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5146
288
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5147
288
  }
5148
564
}
5149
5150
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5151
33.2k
{
5152
33.2k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5153
5154
33.2k
  const zend_ast *class_ast = ast->child[0];
5155
33.2k
  zend_ast *method_ast = ast->child[1];
5156
5157
  /* Recognize parent::$prop::get() pattern. */
5158
33.2k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5159
2.04k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5160
1.95k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5161
1.60k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5162
1.60k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5163
1.12k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5164
990
   || method_ast->kind != ZEND_AST_ZVAL
5165
924
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5166
924
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5167
32.5k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5168
32.5k
    return false;
5169
32.5k
  }
5170
5171
765
  zend_class_entry *ce = CG(active_class_entry);
5172
765
  if (!ce) {
5173
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5174
1
  }
5175
5176
764
  zend_ast *args_ast = ast->child[2];
5177
764
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5178
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5179
1
  }
5180
5181
763
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5182
763
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5183
763
  zend_string *hook_name = zend_ast_get_str(method_ast);
5184
763
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5185
763
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5186
5187
763
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5188
763
  if (!prop_info_name) {
5189
1
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5190
1
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5191
1
  }
5192
5193
762
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5194
762
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5195
22
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5196
22
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5197
22
  }
5198
740
  if (hook_kind != CG(context).active_property_hook_kind) {
5199
2
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5200
2
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5201
2
  }
5202
5203
738
  zend_op *opline = get_next_op();
5204
738
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5205
738
  opline->op1_type = IS_CONST;
5206
738
  opline->op1.constant = zend_add_literal_string(&property_name);
5207
738
  opline->op2.num = hook_kind;
5208
5209
738
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast));
5210
5211
738
  return true;
5212
740
}
5213
5214
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5215
4.31M
{
5216
4.31M
  zend_ast *name_ast = ast->child[0];
5217
4.31M
  zend_ast *args_ast = ast->child[1];
5218
4.31M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5219
5220
4.31M
  znode name_node;
5221
5222
4.31M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5223
235k
    zend_compile_expr(&name_node, name_ast);
5224
235k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5225
235k
    return;
5226
235k
  }
5227
5228
4.07M
  {
5229
4.07M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5230
4.07M
    if (runtime_resolution) {
5231
3.94M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5232
16.0k
          && !is_callable_convert) {
5233
15.5k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5234
3.93M
      } else {
5235
3.93M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5236
3.93M
      }
5237
3.94M
      return;
5238
3.94M
    }
5239
4.07M
  }
5240
5241
129k
  {
5242
129k
    const zval *name = &name_node.u.constant;
5243
129k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5244
129k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5245
129k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5246
129k
    zend_op *opline;
5247
5248
    /* Special assert() handling should apply independently of compiler flags. */
5249
129k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5250
20.2k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5251
20.2k
      zend_string_release(lcname);
5252
20.2k
      zval_ptr_dtor(&name_node.u.constant);
5253
20.2k
      return;
5254
20.2k
    }
5255
5256
109k
    if (!fbc
5257
40.4k
     || !fbc_is_finalized(fbc)
5258
68.6k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5259
68.6k
      zend_string_release_ex(lcname, 0);
5260
68.6k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5261
68.6k
      return;
5262
68.6k
    }
5263
5264
40.4k
    if (!is_callable_convert &&
5265
39.9k
        zend_try_compile_special_func(result, lcname,
5266
39.9k
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5267
40.4k
    ) {
5268
25.9k
      zend_string_release_ex(lcname, 0);
5269
25.9k
      zval_ptr_dtor(&name_node.u.constant);
5270
25.9k
      return;
5271
25.9k
    }
5272
5273
14.5k
    zval_ptr_dtor(&name_node.u.constant);
5274
14.5k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5275
5276
14.5k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5277
14.5k
    opline->result.num = zend_alloc_cache_slot();
5278
5279
    /* Store offset to function from symbol table in op2.extra. */
5280
14.5k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5281
13.1k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5282
13.1k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5283
13.1k
    }
5284
5285
14.5k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5286
14.5k
  }
5287
14.5k
}
5288
/* }}} */
5289
5290
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5291
30.9k
{
5292
30.9k
  zend_ast *obj_ast = ast->child[0];
5293
30.9k
  zend_ast *method_ast = ast->child[1];
5294
30.9k
  zend_ast *args_ast = ast->child[2];
5295
5296
30.9k
  znode obj_node, method_node;
5297
30.9k
  zend_op *opline;
5298
30.9k
  const zend_function *fbc = NULL;
5299
30.9k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5300
30.9k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5301
5302
30.9k
  if (is_this_fetch(obj_ast)) {
5303
1.59k
    if (this_guaranteed_exists()) {
5304
1.38k
      obj_node.op_type = IS_UNUSED;
5305
1.38k
    } else {
5306
203
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5307
203
    }
5308
1.59k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5309
5310
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5311
     * check for a nullsafe access. */
5312
29.3k
  } else {
5313
29.3k
    zend_short_circuiting_mark_inner(obj_ast);
5314
29.3k
    zend_compile_expr(&obj_node, obj_ast);
5315
29.3k
    if (nullsafe) {
5316
3.98k
      zend_emit_jmp_null(&obj_node, type);
5317
3.98k
    }
5318
29.3k
  }
5319
5320
30.9k
  zend_compile_expr(&method_node, method_ast);
5321
30.9k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5322
5323
30.9k
  if (method_node.op_type == IS_CONST) {
5324
29.9k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5325
1
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5326
1
    }
5327
5328
29.9k
    opline->op2_type = IS_CONST;
5329
29.9k
    opline->op2.constant = zend_add_func_name_literal(
5330
29.9k
      Z_STR(method_node.u.constant));
5331
29.9k
    opline->result.num = zend_alloc_cache_slots(2);
5332
29.9k
  } else {
5333
942
    SET_NODE(opline->op2, &method_node);
5334
942
  }
5335
5336
  /* Check if this calls a known method on $this */
5337
30.9k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5338
1.32k
      CG(active_class_entry) && zend_is_scope_known()) {
5339
967
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5340
967
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5341
5342
    /* We only know the exact method that is being called if it is either private or final.
5343
     * Otherwise an overriding method in a child class may be called. */
5344
967
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5345
66
      fbc = NULL;
5346
66
    }
5347
967
  }
5348
5349
30.9k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5350
316
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5351
1
      zend_error_noreturn(E_COMPILE_ERROR,
5352
1
        "Cannot combine nullsafe operator with Closure creation");
5353
1
    }
5354
316
  }
5355
30.9k
}
5356
/* }}} */
5357
5358
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5359
30.6k
{
5360
30.6k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5361
30.6k
}
5362
/* }}} */
5363
5364
static bool is_func_accessible(const zend_function *fbc)
5365
782
{
5366
782
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5367
596
    return true;
5368
596
  }
5369
5370
186
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5371
80
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5372
80
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5373
4
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5374
0
    return true;
5375
0
  }
5376
5377
186
  return false;
5378
186
}
5379
5380
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5381
1.05k
{
5382
1.05k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5383
5384
1.05k
  if (!fbc || is_func_accessible(fbc)) {
5385
873
    return fbc;
5386
873
  }
5387
5388
184
  return NULL;
5389
1.05k
}
5390
/* }}} */
5391
5392
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5393
33.2k
{
5394
33.2k
  zend_ast *class_ast = ast->child[0];
5395
33.2k
  zend_ast *method_ast = ast->child[1];
5396
33.2k
  zend_ast *args_ast = ast->child[2];
5397
5398
33.2k
  znode class_node, method_node;
5399
33.2k
  zend_op *opline;
5400
33.2k
  const zend_function *fbc = NULL;
5401
5402
33.2k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5403
738
    return;
5404
738
  }
5405
5406
32.5k
  zend_short_circuiting_mark_inner(class_ast);
5407
32.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5408
5409
32.5k
  zend_compile_expr(&method_node, method_ast);
5410
5411
32.5k
  if (method_node.op_type == IS_CONST) {
5412
30.3k
    zval *name = &method_node.u.constant;
5413
30.3k
    if (Z_TYPE_P(name) != IS_STRING) {
5414
1
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5415
1
    }
5416
30.3k
    if (zend_is_constructor(Z_STR_P(name))) {
5417
343
      zval_ptr_dtor(name);
5418
343
      method_node.op_type = IS_UNUSED;
5419
343
    }
5420
30.3k
  }
5421
5422
32.5k
  opline = get_next_op();
5423
32.5k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5424
5425
32.5k
  zend_set_class_name_op1(opline, &class_node);
5426
5427
32.5k
  if (method_node.op_type == IS_CONST) {
5428
30.0k
    opline->op2_type = IS_CONST;
5429
30.0k
    opline->op2.constant = zend_add_func_name_literal(
5430
30.0k
      Z_STR(method_node.u.constant));
5431
30.0k
    opline->result.num = zend_alloc_cache_slots(2);
5432
30.0k
  } else {
5433
2.52k
    if (opline->op1_type == IS_CONST) {
5434
532
      opline->result.num = zend_alloc_cache_slot();
5435
532
    }
5436
2.52k
    SET_NODE(opline->op2, &method_node);
5437
2.52k
  }
5438
5439
  /* Check if we already know which method we're calling */
5440
32.5k
  if (opline->op2_type == IS_CONST) {
5441
30.0k
    zend_class_entry *ce = NULL;
5442
30.0k
    if (opline->op1_type == IS_CONST) {
5443
12.4k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5444
12.4k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5445
12.4k
      if (ce) {
5446
832
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5447
0
          ce = NULL;
5448
0
        }
5449
11.6k
      } else if (CG(active_class_entry)
5450
569
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5451
73
        ce = CG(active_class_entry);
5452
73
      }
5453
17.5k
    } else if (opline->op1_type == IS_UNUSED
5454
12.0k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5455
11.7k
        && zend_is_scope_known()) {
5456
152
      ce = CG(active_class_entry);
5457
152
    }
5458
30.0k
    if (ce) {
5459
1.05k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5460
1.05k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5461
1.05k
    }
5462
30.0k
  }
5463
5464
32.5k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5465
32.5k
}
5466
/* }}} */
5467
5468
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5469
5470
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5471
27.6k
{
5472
27.6k
  zend_ast *class_ast = ast->child[0];
5473
27.6k
  zend_ast *args_ast = ast->child[1];
5474
5475
27.6k
  znode class_node, ctor_result;
5476
27.6k
  zend_op *opline;
5477
5478
27.6k
  if (class_ast->kind == ZEND_AST_CLASS) {
5479
    /* anon class declaration */
5480
1.51k
    zend_compile_class_decl(&class_node, class_ast, false);
5481
26.1k
  } else {
5482
26.1k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5483
26.1k
  }
5484
5485
27.6k
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5486
5487
27.6k
  zend_set_class_name_op1(opline, &class_node);
5488
5489
27.6k
  if (opline->op1_type == IS_CONST) {
5490
24.9k
    opline->op2.num = zend_alloc_cache_slot();
5491
24.9k
  }
5492
5493
27.6k
  zend_class_entry *ce = NULL;
5494
27.6k
  if (opline->op1_type == IS_CONST) {
5495
24.9k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5496
24.9k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5497
24.9k
    if (ce) {
5498
500
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5499
0
        ce = NULL;
5500
0
      }
5501
24.4k
    } else if (CG(active_class_entry)
5502
521
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5503
13
      ce = CG(active_class_entry);
5504
13
    }
5505
24.9k
  } else if (opline->op1_type == IS_UNUSED
5506
247
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5507
77
      && zend_is_scope_known()) {
5508
10
    ce = CG(active_class_entry);
5509
10
  }
5510
5511
5512
27.6k
  const zend_function *fbc = NULL;
5513
27.6k
  if (ce
5514
523
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5515
457
      && ce->constructor
5516
214
      && is_func_accessible(ce->constructor)) {
5517
212
    fbc = ce->constructor;
5518
212
  }
5519
5520
27.6k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno);
5521
27.6k
  zend_do_free(&ctor_result);
5522
27.6k
}
5523
/* }}} */
5524
5525
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5526
2.02k
{
5527
2.02k
  zend_ast *var_ast = ast->child[0];
5528
2.02k
  zend_ast *name_ast = var_ast->child[0];
5529
5530
2.02k
  znode name_node, result;
5531
5532
2.02k
  zend_compile_expr(&name_node, name_ast);
5533
2.02k
  if (name_node.op_type == IS_CONST) {
5534
1.45k
    convert_to_string(&name_node.u.constant);
5535
1.45k
  }
5536
5537
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5538
2.02k
  if (is_this_fetch(var_ast)) {
5539
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5540
2.02k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5541
1.23k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5542
1.23k
    opline->extended_value = zend_alloc_cache_slot();
5543
1.23k
  } else {
5544
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5545
     * to not free the name_node operand, so it can be reused in the following
5546
     * ASSIGN_REF, which then frees it. */
5547
788
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5548
788
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5549
5550
788
    if (name_node.op_type == IS_CONST) {
5551
217
      zend_string_addref(Z_STR(name_node.u.constant));
5552
217
    }
5553
5554
788
    zend_emit_assign_ref_znode(
5555
788
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5556
788
      &result
5557
788
    );
5558
788
  }
5559
2.02k
}
5560
/* }}} */
5561
5562
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5563
76.2k
{
5564
76.2k
  zend_op *opline;
5565
76.2k
  if (!CG(active_op_array)->static_variables) {
5566
0
    if (CG(active_op_array)->scope) {
5567
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5568
0
    }
5569
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5570
0
  }
5571
5572
76.2k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5573
5574
76.2k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5575
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5576
0
  }
5577
5578
76.2k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5579
76.2k
  opline->op1_type = IS_CV;
5580
76.2k
  opline->op1.var = lookup_cv(var_name);
5581
76.2k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5582
76.2k
}
5583
/* }}} */
5584
5585
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5586
5.74k
{
5587
5.74k
  zend_ast *var_ast = ast->child[0];
5588
5.74k
  zend_string *var_name = zend_ast_get_str(var_ast);
5589
5590
5.74k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5591
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5592
1
  }
5593
5594
5.74k
  if (!CG(active_op_array)->static_variables) {
5595
4.63k
    if (CG(active_op_array)->scope) {
5596
3.97k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5597
3.97k
    }
5598
4.63k
    CG(active_op_array)->static_variables = zend_new_array(8);
5599
4.63k
  }
5600
5601
5.74k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5602
7
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5603
7
  }
5604
5605
5.73k
  zend_eval_const_expr(&ast->child[1]);
5606
5.73k
  zend_ast *value_ast = ast->child[1];
5607
5608
5.73k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5609
5.49k
    zval *value_zv = value_ast
5610
5.49k
      ? zend_ast_get_zval(value_ast)
5611
5.49k
      : &EG(uninitialized_zval);
5612
5.49k
    Z_TRY_ADDREF_P(value_zv);
5613
5.49k
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5614
5.49k
  } else {
5615
246
    zend_op *opline;
5616
5617
246
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5618
246
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5619
5620
246
    uint32_t static_def_jmp_opnum = get_next_op_number();
5621
246
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5622
246
    opline->op1_type = IS_CV;
5623
246
    opline->op1.var = lookup_cv(var_name);
5624
246
    opline->extended_value = placeholder_offset;
5625
5626
246
    znode expr;
5627
246
    zend_compile_expr(&expr, value_ast);
5628
5629
246
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5630
246
    opline->op1_type = IS_CV;
5631
246
    opline->op1.var = lookup_cv(var_name);
5632
246
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5633
5634
246
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5635
246
  }
5636
5.73k
}
5637
/* }}} */
5638
5639
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5640
3.81k
{
5641
3.81k
  zend_ast *var_ast = ast->child[0];
5642
3.81k
  znode var_node;
5643
3.81k
  zend_op *opline;
5644
5645
3.81k
  zend_ensure_writable_variable(var_ast);
5646
5647
3.81k
  if (is_global_var_fetch(var_ast)) {
5648
505
    if (!var_ast->child[1]) {
5649
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5650
1
    }
5651
5652
504
    zend_compile_expr(&var_node, var_ast->child[1]);
5653
504
    if (var_node.op_type == IS_CONST) {
5654
310
      convert_to_string(&var_node.u.constant);
5655
310
    }
5656
5657
504
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5658
504
    opline->extended_value = ZEND_FETCH_GLOBAL;
5659
504
    return;
5660
505
  }
5661
5662
3.31k
  switch (var_ast->kind) {
5663
1.46k
    case ZEND_AST_VAR:
5664
1.46k
      if (is_this_fetch(var_ast)) {
5665
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5666
1.46k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5667
1.12k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5668
1.12k
      } else {
5669
338
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5670
338
        opline->opcode = ZEND_UNSET_VAR;
5671
338
      }
5672
1.46k
      return;
5673
1.46k
    case ZEND_AST_DIM:
5674
909
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5675
909
      opline->opcode = ZEND_UNSET_DIM;
5676
909
      return;
5677
928
    case ZEND_AST_PROP:
5678
928
    case ZEND_AST_NULLSAFE_PROP:
5679
928
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5680
928
      opline->opcode = ZEND_UNSET_OBJ;
5681
928
      return;
5682
10
    case ZEND_AST_STATIC_PROP:
5683
10
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5684
10
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5685
10
      return;
5686
3.31k
    EMPTY_SWITCH_DEFAULT_CASE()
5687
3.31k
  }
5688
3.31k
}
5689
/* }}} */
5690
5691
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5692
43.4k
{
5693
43.4k
  const zend_loop_var *base;
5694
43.4k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5695
5696
43.4k
  if (!loop_var) {
5697
671
    return 1;
5698
671
  }
5699
42.7k
  base = zend_stack_base(&CG(loop_var_stack));
5700
73.1k
  for (; loop_var >= base; loop_var--) {
5701
71.2k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5702
3.30k
      zend_op *opline = get_next_op();
5703
5704
3.30k
      opline->opcode = ZEND_FAST_CALL;
5705
3.30k
      opline->result_type = IS_TMP_VAR;
5706
3.30k
      opline->result.var = loop_var->var_num;
5707
3.30k
      if (return_value) {
5708
1.78k
        SET_NODE(opline->op2, return_value);
5709
1.78k
      }
5710
3.30k
      opline->op1.num = loop_var->try_catch_offset;
5711
67.9k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5712
24.6k
      zend_op *opline = get_next_op();
5713
24.6k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5714
24.6k
      opline->op1_type = IS_TMP_VAR;
5715
24.6k
      opline->op1.var = loop_var->var_num;
5716
43.2k
    } else if (loop_var->opcode == ZEND_RETURN) {
5717
      /* Stack separator */
5718
39.8k
      break;
5719
39.8k
    } else if (depth <= 1) {
5720
916
      return 1;
5721
2.45k
    } else if (loop_var->opcode == ZEND_NOP) {
5722
      /* Loop doesn't have freeable variable */
5723
1.55k
      depth--;
5724
1.55k
    } else {
5725
898
      zend_op *opline;
5726
5727
898
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5728
898
      opline = get_next_op();
5729
898
      opline->opcode = loop_var->opcode;
5730
898
      opline->op1_type = loop_var->var_type;
5731
898
      opline->op1.var = loop_var->var_num;
5732
898
      opline->extended_value = ZEND_FREE_ON_RETURN;
5733
898
      depth--;
5734
898
      }
5735
71.2k
  }
5736
41.8k
  return (depth == 0);
5737
42.7k
}
5738
/* }}} */
5739
5740
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5741
42.4k
{
5742
42.4k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5743
42.4k
}
5744
/* }}} */
5745
5746
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5747
2.47k
{
5748
2.47k
  const zend_loop_var *base;
5749
2.47k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5750
5751
2.47k
  if (!loop_var) {
5752
293
    return 0;
5753
293
  }
5754
2.18k
  base = zend_stack_base(&CG(loop_var_stack));
5755
4.56k
  for (; loop_var >= base; loop_var--) {
5756
4.41k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5757
663
      return 1;
5758
3.75k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5759
1.88k
    } else if (loop_var->opcode == ZEND_RETURN) {
5760
      /* Stack separator */
5761
1.38k
      return 0;
5762
1.38k
    } else if (depth <= 1) {
5763
0
      return 0;
5764
508
    } else {
5765
508
      depth--;
5766
508
      }
5767
4.41k
  }
5768
143
  return 0;
5769
2.18k
}
5770
/* }}} */
5771
5772
static bool zend_has_finally(void) /* {{{ */
5773
2.47k
{
5774
2.47k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5775
2.47k
}
5776
/* }}} */
5777
5778
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5779
40.7k
{
5780
40.7k
  zend_ast *expr_ast = ast->child[0];
5781
40.7k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5782
40.7k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5783
5784
40.7k
  znode expr_node;
5785
40.7k
  zend_op *opline;
5786
5787
40.7k
  if (is_generator) {
5788
    /* For generators the by-ref flag refers to yields, not returns */
5789
4.48k
    by_ref = false;
5790
4.48k
  }
5791
5792
40.7k
  if (!expr_ast) {
5793
993
    expr_node.op_type = IS_CONST;
5794
993
    ZVAL_NULL(&expr_node.u.constant);
5795
39.7k
  } else if (by_ref && zend_is_variable(expr_ast)) {
5796
1.61k
    zend_assert_not_short_circuited(expr_ast);
5797
1.61k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5798
38.1k
  } else {
5799
38.1k
    zend_compile_expr(&expr_node, expr_ast);
5800
38.1k
  }
5801
5802
40.7k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5803
2.88k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5804
2.47k
   && zend_has_finally()) {
5805
    /* Copy return value into temporary VAR to avoid modification in finally code */
5806
663
    if (by_ref) {
5807
269
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5808
394
    } else {
5809
394
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5810
394
    }
5811
663
  }
5812
5813
  /* Generator return types are handled separately */
5814
40.7k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5815
3.35k
    zend_emit_return_type_check(
5816
3.35k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5817
3.35k
  }
5818
5819
40.7k
  uint32_t opnum_before_finally = get_next_op_number();
5820
5821
40.7k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5822
5823
  /* Content of reference might have changed in finally, repeat type check. */
5824
40.7k
  if (by_ref
5825
   /* Check if any opcodes were emitted since the last return type check. */
5826
4.67k
   && opnum_before_finally != get_next_op_number()
5827
1.92k
   && !is_generator
5828
1.92k
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5829
10
    zend_emit_return_type_check(
5830
10
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5831
10
  }
5832
5833
40.7k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5834
40.7k
    &expr_node, NULL);
5835
5836
40.7k
  if (by_ref && expr_ast) {
5837
3.95k
    if (zend_is_call(expr_ast)) {
5838
1.16k
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5839
2.79k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5840
1.18k
      opline->extended_value = ZEND_RETURNS_VALUE;
5841
1.18k
    }
5842
3.95k
  }
5843
40.7k
}
5844
/* }}} */
5845
5846
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
5847
827
{
5848
827
  zend_ast *expr_ast = ast->child[0];
5849
827
  znode expr_node;
5850
827
  zend_op *opline;
5851
5852
827
  zend_compile_expr(&expr_node, expr_ast);
5853
5854
827
  switch (expr_node.op_type) {
5855
133
    case IS_TMP_VAR:
5856
364
    case IS_VAR:
5857
364
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5858
364
      opline->extended_value = ZEND_FREE_VOID_CAST;
5859
364
      break;
5860
409
    case IS_CONST:
5861
409
      zend_do_free(&expr_node);
5862
409
      break;
5863
827
  }
5864
827
}
5865
5866
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
5867
2.17M
{
5868
2.17M
  zend_op *opline;
5869
2.17M
  zend_ast *expr_ast = ast->child[0];
5870
5871
2.17M
  znode expr_node;
5872
2.17M
  zend_compile_expr(&expr_node, expr_ast);
5873
5874
2.17M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5875
2.17M
  opline->extended_value = 0;
5876
2.17M
}
5877
/* }}} */
5878
5879
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
5880
1.69k
{
5881
1.69k
  zend_ast *expr_ast = ast->child[0];
5882
5883
1.69k
  znode expr_node;
5884
1.69k
  zend_compile_expr(&expr_node, expr_ast);
5885
5886
1.69k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5887
1.69k
  if (result) {
5888
    /* Mark this as an "expression throw" for opcache. */
5889
906
    opline->extended_value = ZEND_THROW_IS_EXPR;
5890
906
    result->op_type = IS_CONST;
5891
906
    ZVAL_TRUE(&result->u.constant);
5892
906
  }
5893
1.69k
}
5894
/* }}} */
5895
5896
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
5897
1.05k
{
5898
1.05k
  zend_ast *depth_ast = ast->child[0];
5899
5900
1.05k
  zend_op *opline;
5901
1.05k
  zend_long depth;
5902
5903
1.05k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5904
5905
1.05k
  if (depth_ast) {
5906
323
    const zval *depth_zv;
5907
323
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5908
19
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5909
19
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5910
19
    }
5911
5912
304
    depth_zv = zend_ast_get_zval(depth_ast);
5913
304
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5914
6
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5915
6
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5916
6
    }
5917
5918
298
    depth = Z_LVAL_P(depth_zv);
5919
735
  } else {
5920
735
    depth = 1;
5921
735
  }
5922
5923
1.03k
  if (CG(context).current_brk_cont == -1) {
5924
43
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5925
43
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5926
990
  } else {
5927
990
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5928
74
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5929
74
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5930
74
        depth, depth == 1 ? "" : "s");
5931
74
    }
5932
990
  }
5933
5934
916
  if (ast->kind == ZEND_AST_CONTINUE) {
5935
772
    int d, cur = CG(context).current_brk_cont;
5936
877
    for (d = depth - 1; d > 0; d--) {
5937
105
      cur = CG(context).brk_cont_array[cur].parent;
5938
105
      ZEND_ASSERT(cur != -1);
5939
105
    }
5940
5941
772
    if (CG(context).brk_cont_array[cur].is_switch) {
5942
280
      if (depth == 1) {
5943
187
        if (CG(context).brk_cont_array[cur].parent == -1) {
5944
115
          zend_error(E_WARNING,
5945
115
            "\"continue\" targeting switch is equivalent to \"break\"");
5946
115
        } else {
5947
72
          zend_error(E_WARNING,
5948
72
            "\"continue\" targeting switch is equivalent to \"break\". " \
5949
72
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5950
72
            depth + 1);
5951
72
        }
5952
187
      } else {
5953
93
        if (CG(context).brk_cont_array[cur].parent == -1) {
5954
53
          zend_error(E_WARNING,
5955
53
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5956
53
            depth, depth);
5957
53
        } else {
5958
40
          zend_error(E_WARNING,
5959
40
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5960
40
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5961
40
            depth, depth, depth + 1);
5962
40
        }
5963
93
      }
5964
280
    }
5965
772
  }
5966
5967
916
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5968
916
  opline->op1.num = CG(context).current_brk_cont;
5969
916
  opline->op2.num = depth;
5970
916
}
5971
/* }}} */
5972
5973
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5974
1.40k
{
5975
1.40k
  zend_label *dest;
5976
1.40k
  int remove_oplines = opline->op1.num;
5977
1.40k
  zval *label;
5978
1.40k
  uint32_t opnum = opline - op_array->opcodes;
5979
5980
1.40k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5981
1.40k
  if (CG(context).labels == NULL ||
5982
1.38k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5983
1.40k
  ) {
5984
46
    CG(in_compilation) = 1;
5985
46
    CG(active_op_array) = op_array;
5986
46
    CG(zend_lineno) = opline->lineno;
5987
46
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5988
46
  }
5989
5990
1.35k
  zval_ptr_dtor_str(label);
5991
1.35k
  ZVAL_NULL(label);
5992
5993
1.35k
  uint32_t current = opline->extended_value;
5994
2.80k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
5995
1.45k
    if (current == -1) {
5996
1
      CG(in_compilation) = 1;
5997
1
      CG(active_op_array) = op_array;
5998
1
      CG(zend_lineno) = opline->lineno;
5999
1
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6000
1
    }
6001
1.45k
    if (CG(context).brk_cont_array[current].start >= 0) {
6002
806
      remove_oplines--;
6003
806
    }
6004
1.45k
  }
6005
6006
2.95k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6007
1.82k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6008
1.82k
    if (elem->try_op > opnum) {
6009
230
      break;
6010
230
    }
6011
1.59k
    if (elem->finally_op && opnum < elem->finally_op - 1
6012
1.08k
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6013
1.59k
    ) {
6014
715
      remove_oplines--;
6015
715
    }
6016
1.59k
  }
6017
6018
1.35k
  opline->opcode = ZEND_JMP;
6019
1.35k
  SET_UNUSED(opline->op1);
6020
1.35k
  SET_UNUSED(opline->op2);
6021
1.35k
  SET_UNUSED(opline->result);
6022
1.35k
  opline->op1.opline_num = dest->opline_num;
6023
1.35k
  opline->extended_value = 0;
6024
6025
1.35k
  ZEND_ASSERT(remove_oplines >= 0);
6026
1.73k
  while (remove_oplines--) {
6027
378
    opline--;
6028
378
    MAKE_NOP(opline);
6029
378
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6030
378
  }
6031
1.35k
}
6032
/* }}} */
6033
6034
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6035
1.75k
{
6036
1.75k
  zend_ast *label_ast = ast->child[0];
6037
1.75k
  znode label_node;
6038
1.75k
  zend_op *opline;
6039
6040
1.75k
  zend_compile_expr(&label_node, label_ast);
6041
6042
  /* Label resolution and unwinding adjustments happen in pass two. */
6043
1.75k
  uint32_t opnum_start = get_next_op_number();
6044
1.75k
  zend_handle_loops_and_finally(NULL);
6045
1.75k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6046
1.75k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6047
1.75k
  opline->extended_value = CG(context).current_brk_cont;
6048
1.75k
}
6049
/* }}} */
6050
6051
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6052
7.43k
{
6053
7.43k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6054
7.43k
  zend_label dest;
6055
6056
7.43k
  if (!CG(context).labels) {
6057
5.23k
    ALLOC_HASHTABLE(CG(context).labels);
6058
5.23k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6059
5.23k
  }
6060
6061
7.43k
  dest.brk_cont = CG(context).current_brk_cont;
6062
7.43k
  dest.opline_num = get_next_op_number();
6063
6064
7.43k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6065
55
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6066
55
  }
6067
7.43k
}
6068
/* }}} */
6069
6070
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6071
6.66k
{
6072
6.66k
  zend_ast *cond_ast = ast->child[0];
6073
6.66k
  zend_ast *stmt_ast = ast->child[1];
6074
6.66k
  znode cond_node;
6075
6.66k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6076
6077
6.66k
  opnum_jmp = zend_emit_jump(0);
6078
6079
6.66k
  zend_begin_loop(ZEND_NOP, NULL, false);
6080
6081
6.66k
  opnum_start = get_next_op_number();
6082
6.66k
  zend_compile_stmt(stmt_ast);
6083
6084
6.66k
  opnum_cond = get_next_op_number();
6085
6.66k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6086
6.66k
  zend_compile_expr(&cond_node, cond_ast);
6087
6088
6.66k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6089
6090
6.66k
  zend_end_loop(opnum_cond, NULL);
6091
6.66k
}
6092
/* }}} */
6093
6094
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6095
1.14k
{
6096
1.14k
  zend_ast *stmt_ast = ast->child[0];
6097
1.14k
  zend_ast *cond_ast = ast->child[1];
6098
6099
1.14k
  znode cond_node;
6100
1.14k
  uint32_t opnum_start, opnum_cond;
6101
6102
1.14k
  zend_begin_loop(ZEND_NOP, NULL, false);
6103
6104
1.14k
  opnum_start = get_next_op_number();
6105
1.14k
  zend_compile_stmt(stmt_ast);
6106
6107
1.14k
  opnum_cond = get_next_op_number();
6108
1.14k
  zend_compile_expr(&cond_node, cond_ast);
6109
6110
1.14k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6111
6112
1.14k
  zend_end_loop(opnum_cond, NULL);
6113
1.14k
}
6114
/* }}} */
6115
6116
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6117
17.6k
{
6118
17.6k
  const zend_ast_list *list;
6119
17.6k
  uint32_t i;
6120
6121
17.6k
  result->op_type = IS_CONST;
6122
17.6k
  ZVAL_TRUE(&result->u.constant);
6123
6124
17.6k
  if (!ast) {
6125
10.7k
    return;
6126
10.7k
  }
6127
6128
6.90k
  list = zend_ast_get_list(ast);
6129
15.9k
  for (i = 0; i < list->children; ++i) {
6130
9.03k
    zend_ast *expr_ast = list->child[i];
6131
6132
9.03k
    zend_do_free(result);
6133
9.03k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6134
224
      zend_compile_void_cast(NULL, expr_ast);
6135
224
      result->op_type = IS_CONST;
6136
224
      ZVAL_NULL(&result->u.constant);
6137
8.80k
    } else {
6138
8.80k
      zend_compile_expr(result, expr_ast);
6139
8.80k
    }
6140
9.03k
  }
6141
6.90k
}
6142
/* }}} */
6143
6144
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6145
5.93k
{
6146
5.93k
  zend_ast *init_ast = ast->child[0];
6147
5.93k
  zend_ast *cond_ast = ast->child[1];
6148
5.93k
  zend_ast *loop_ast = ast->child[2];
6149
5.93k
  zend_ast *stmt_ast = ast->child[3];
6150
6151
5.93k
  znode result;
6152
5.93k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6153
6154
5.93k
  zend_compile_for_expr_list(&result, init_ast);
6155
5.93k
  zend_do_free(&result);
6156
6157
5.93k
  opnum_jmp = zend_emit_jump(0);
6158
6159
5.93k
  zend_begin_loop(ZEND_NOP, NULL, false);
6160
6161
5.93k
  opnum_start = get_next_op_number();
6162
5.93k
  zend_compile_stmt(stmt_ast);
6163
6164
5.93k
  opnum_loop = get_next_op_number();
6165
5.93k
  zend_compile_for_expr_list(&result, loop_ast);
6166
5.93k
  zend_do_free(&result);
6167
6168
5.93k
  zend_update_jump_target_to_next(opnum_jmp);
6169
5.93k
  zend_compile_for_expr_list(&result, cond_ast);
6170
5.93k
  zend_do_extended_stmt(NULL);
6171
6172
5.93k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6173
6174
5.93k
  zend_end_loop(opnum_loop, NULL);
6175
5.93k
}
6176
/* }}} */
6177
6178
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6179
7.18k
{
6180
7.18k
  zend_ast *expr_ast = ast->child[0];
6181
7.18k
  zend_ast *value_ast = ast->child[1];
6182
7.18k
  zend_ast *key_ast = ast->child[2];
6183
7.18k
  zend_ast *stmt_ast = ast->child[3];
6184
7.18k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6185
7.18k
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6186
6187
7.18k
  znode expr_node, reset_node, value_node, key_node;
6188
7.18k
  zend_op *opline;
6189
7.18k
  uint32_t opnum_reset, opnum_fetch;
6190
6191
7.18k
  if (key_ast) {
6192
258
    if (key_ast->kind == ZEND_AST_REF) {
6193
4
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6194
4
    }
6195
254
    if (key_ast->kind == ZEND_AST_ARRAY) {
6196
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6197
9
    }
6198
254
  }
6199
6200
7.16k
  if (by_ref) {
6201
509
    value_ast = value_ast->child[0];
6202
509
  }
6203
6204
7.16k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6205
115
    by_ref = true;
6206
115
  }
6207
6208
7.16k
  if (by_ref && is_variable) {
6209
131
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6210
7.03k
  } else {
6211
7.03k
    zend_compile_expr(&expr_node, expr_ast);
6212
7.03k
  }
6213
6214
7.16k
  if (by_ref) {
6215
624
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6216
624
  }
6217
6218
7.16k
  opnum_reset = get_next_op_number();
6219
7.16k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6220
6221
7.16k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6222
6223
7.16k
  opnum_fetch = get_next_op_number();
6224
7.16k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6225
6226
7.16k
  if (is_this_fetch(value_ast)) {
6227
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6228
7.16k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6229
6.53k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6230
5.57k
    SET_NODE(opline->op2, &value_node);
6231
5.57k
  } else {
6232
1.59k
    opline->op2_type = IS_VAR;
6233
1.59k
    opline->op2.var = get_temporary_variable();
6234
1.59k
    GET_NODE(&value_node, opline->op2);
6235
1.59k
    if (value_ast->kind == ZEND_AST_ARRAY) {
6236
346
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6237
1.25k
    } else if (by_ref) {
6238
294
      zend_emit_assign_ref_znode(value_ast, &value_node);
6239
958
    } else {
6240
958
      zend_emit_assign_znode(value_ast, &value_node);
6241
958
    }
6242
1.59k
  }
6243
6244
7.16k
  if (key_ast) {
6245
240
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6246
240
    zend_make_tmp_result(&key_node, opline);
6247
240
    zend_emit_assign_znode(key_ast, &key_node);
6248
240
  }
6249
6250
7.16k
  zend_compile_stmt(stmt_ast);
6251
6252
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6253
   * better to use the end line, but this information is not available
6254
   * currently. */
6255
7.16k
  CG(zend_lineno) = ast->lineno;
6256
7.16k
  zend_emit_jump(opnum_fetch);
6257
6258
7.16k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6259
7.16k
  opline->op2.opline_num = get_next_op_number();
6260
6261
7.16k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6262
7.16k
  opline->extended_value = get_next_op_number();
6263
6264
7.16k
  zend_end_loop(opnum_fetch, &reset_node);
6265
6266
7.16k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6267
7.16k
}
6268
/* }}} */
6269
6270
static void zend_compile_if(zend_ast *ast) /* {{{ */
6271
42.5k
{
6272
42.5k
  const zend_ast_list *list = zend_ast_get_list(ast);
6273
42.5k
  uint32_t i;
6274
42.5k
  uint32_t *jmp_opnums = NULL;
6275
6276
42.5k
  if (list->children > 1) {
6277
17.7k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6278
17.7k
  }
6279
6280
116k
  for (i = 0; i < list->children; ++i) {
6281
73.6k
    const zend_ast *elem_ast = list->child[i];
6282
73.6k
    zend_ast *cond_ast = elem_ast->child[0];
6283
73.6k
    zend_ast *stmt_ast = elem_ast->child[1];
6284
6285
73.6k
    if (cond_ast) {
6286
56.4k
      znode cond_node;
6287
56.4k
      uint32_t opnum_jmpz;
6288
6289
56.4k
      if (i > 0) {
6290
13.9k
        CG(zend_lineno) = cond_ast->lineno;
6291
13.9k
        zend_do_extended_stmt(NULL);
6292
13.9k
      }
6293
6294
56.4k
      zend_compile_expr(&cond_node, cond_ast);
6295
56.4k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6296
6297
56.4k
      zend_compile_stmt(stmt_ast);
6298
6299
56.4k
      if (i != list->children - 1) {
6300
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6301
         * report the last line in the if branch as covered if it hasn't actually executed. */
6302
31.1k
        CG(zend_lineno) = elem_ast->lineno;
6303
31.1k
        jmp_opnums[i] = zend_emit_jump(0);
6304
31.1k
      }
6305
56.4k
      zend_update_jump_target_to_next(opnum_jmpz);
6306
56.4k
    } else {
6307
      /* "else" can only occur as last element. */
6308
17.2k
      ZEND_ASSERT(i == list->children - 1);
6309
17.2k
      zend_compile_stmt(stmt_ast);
6310
17.2k
    }
6311
73.6k
  }
6312
6313
42.5k
  if (list->children > 1) {
6314
48.8k
    for (i = 0; i < list->children - 1; ++i) {
6315
31.0k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6316
31.0k
    }
6317
17.7k
    efree(jmp_opnums);
6318
17.7k
  }
6319
42.5k
}
6320
/* }}} */
6321
6322
23.4k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6323
23.4k
  uint32_t i;
6324
23.4k
  uint8_t common_type = IS_UNDEF;
6325
73.0k
  for (i = 0; i < cases->children; i++) {
6326
65.2k
    zend_ast *case_ast = cases->child[i];
6327
65.2k
    zend_ast **cond_ast = &case_ast->child[0];
6328
65.2k
    const zval *cond_zv;
6329
65.2k
    if (!case_ast->child[0]) {
6330
      /* Skip default clause */
6331
401
      continue;
6332
401
    }
6333
6334
64.8k
    zend_eval_const_expr(cond_ast);
6335
64.8k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6336
      /* Non-constant case */
6337
943
      return IS_UNDEF;
6338
943
    }
6339
6340
63.8k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6341
63.8k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6342
      /* We only optimize switched on integers and strings */
6343
14.1k
      return IS_UNDEF;
6344
14.1k
    }
6345
6346
49.7k
    if (common_type == IS_UNDEF) {
6347
22.2k
      common_type = Z_TYPE_P(cond_zv);
6348
27.4k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6349
      /* Non-uniform case types */
6350
258
      return IS_UNDEF;
6351
258
    }
6352
6353
49.4k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6354
41.3k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6355
      /* Numeric strings cannot be compared with a simple hash lookup */
6356
258
      return IS_UNDEF;
6357
258
    }
6358
49.4k
  }
6359
6360
7.83k
  return common_type;
6361
23.4k
}
6362
6363
7.21k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6364
7.21k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6365
0
    return 0;
6366
0
  }
6367
6368
  /* Thresholds are chosen based on when the average switch time for equidistributed
6369
   * input becomes smaller when using the jumptable optimization. */
6370
7.21k
  if (jumptable_type == IS_LONG) {
6371
851
    return cases->children >= 5;
6372
6.36k
  } else {
6373
6.36k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6374
6.36k
    return cases->children >= 2;
6375
6.36k
  }
6376
7.21k
}
6377
6378
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6379
23.4k
{
6380
23.4k
  zend_ast *expr_ast = ast->child[0];
6381
23.4k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6382
6383
23.4k
  uint32_t i;
6384
23.4k
  bool has_default_case = false;
6385
6386
23.4k
  znode expr_node, case_node;
6387
23.4k
  zend_op *opline;
6388
23.4k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6389
23.4k
  uint8_t jumptable_type;
6390
23.4k
  HashTable *jumptable = NULL;
6391
6392
23.4k
  zend_compile_expr(&expr_node, expr_ast);
6393
6394
23.4k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6395
6396
23.4k
  case_node.op_type = IS_TMP_VAR;
6397
23.4k
  case_node.u.op.var = get_temporary_variable();
6398
6399
23.4k
  jumptable_type = determine_switch_jumptable_type(cases);
6400
23.4k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6401
6.62k
    znode jumptable_op;
6402
6403
6.62k
    ALLOC_HASHTABLE(jumptable);
6404
6.62k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6405
6.62k
    jumptable_op.op_type = IS_CONST;
6406
6.62k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6407
6408
6.62k
    opline = zend_emit_op(NULL,
6409
6.62k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6410
6.62k
      &expr_node, &jumptable_op);
6411
6.62k
    if (opline->op1_type == IS_CONST) {
6412
6.03k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6413
6.03k
    }
6414
6.62k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6415
6.62k
  }
6416
6417
23.4k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6418
91.7k
  for (i = 0; i < cases->children; ++i) {
6419
68.2k
    zend_ast *case_ast = cases->child[i];
6420
68.2k
    zend_ast *cond_ast = case_ast->child[0];
6421
68.2k
    znode cond_node;
6422
6423
68.2k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6424
2.34k
      CG(zend_lineno) = case_ast->lineno;
6425
2.34k
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6426
2.34k
    }
6427
6428
68.2k
    if (!cond_ast) {
6429
398
      if (has_default_case) {
6430
4
        CG(zend_lineno) = case_ast->lineno;
6431
4
        zend_error_noreturn(E_COMPILE_ERROR,
6432
4
          "Switch statements may only contain one default clause");
6433
4
      }
6434
394
      has_default_case = true;
6435
394
      continue;
6436
398
    }
6437
6438
67.8k
    zend_compile_expr(&cond_node, cond_ast);
6439
6440
67.8k
    if (expr_node.op_type == IS_CONST
6441
38.8k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6442
2.42k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6443
65.4k
    } else if (expr_node.op_type == IS_CONST
6444
36.4k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6445
1.55k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6446
63.9k
    } else {
6447
63.9k
      opline = zend_emit_op(NULL,
6448
63.9k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6449
63.9k
        &expr_node, &cond_node);
6450
63.9k
      SET_NODE(opline->result, &case_node);
6451
63.9k
      if (opline->op1_type == IS_CONST) {
6452
34.8k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6453
34.8k
      }
6454
6455
63.9k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6456
63.9k
    }
6457
67.8k
  }
6458
6459
23.4k
  opnum_default_jmp = zend_emit_jump(0);
6460
6461
90.9k
  for (i = 0; i < cases->children; ++i) {
6462
67.4k
    zend_ast *case_ast = cases->child[i];
6463
67.4k
    zend_ast *cond_ast = case_ast->child[0];
6464
67.4k
    zend_ast *stmt_ast = case_ast->child[1];
6465
6466
67.4k
    if (cond_ast) {
6467
67.1k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6468
6469
67.1k
      if (jumptable) {
6470
19.8k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6471
19.8k
        zval jmp_target;
6472
19.8k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6473
6474
19.8k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6475
19.8k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6476
5.94k
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6477
13.8k
        } else {
6478
13.8k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6479
13.8k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6480
13.8k
        }
6481
19.8k
      }
6482
67.1k
    } else {
6483
390
      zend_update_jump_target_to_next(opnum_default_jmp);
6484
6485
390
      if (jumptable) {
6486
11
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6487
11
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6488
11
        opline->extended_value = get_next_op_number();
6489
11
      }
6490
390
    }
6491
6492
67.4k
    zend_compile_stmt(stmt_ast);
6493
67.4k
  }
6494
6495
23.4k
  if (!has_default_case) {
6496
23.0k
    zend_update_jump_target_to_next(opnum_default_jmp);
6497
6498
23.0k
    if (jumptable) {
6499
6.58k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6500
6.58k
      opline->extended_value = get_next_op_number();
6501
6.58k
    }
6502
23.0k
  }
6503
6504
23.4k
  zend_end_loop(get_next_op_number(), &expr_node);
6505
6506
23.4k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6507
10.0k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6508
10.0k
    opline->extended_value = ZEND_FREE_SWITCH;
6509
13.3k
  } else if (expr_node.op_type == IS_CONST) {
6510
12.6k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6511
12.6k
  }
6512
6513
23.4k
  efree(jmpnz_opnums);
6514
23.4k
}
6515
/* }}} */
6516
6517
static uint32_t count_match_conds(const zend_ast_list *arms)
6518
2.88k
{
6519
2.88k
  uint32_t num_conds = 0;
6520
6521
6.54k
  for (uint32_t i = 0; i < arms->children; i++) {
6522
3.66k
    const zend_ast *arm_ast = arms->child[i];
6523
3.66k
    if (arm_ast->child[0] == NULL) {
6524
563
      continue;
6525
563
    }
6526
6527
3.09k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6528
3.09k
    num_conds += conds->children;
6529
3.09k
  }
6530
6531
2.88k
  return num_conds;
6532
2.88k
}
6533
6534
2.88k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6535
4.82k
  for (uint32_t i = 0; i < arms->children; i++) {
6536
3.25k
    const zend_ast *arm_ast = arms->child[i];
6537
3.25k
    if (!arm_ast->child[0]) {
6538
      /* Skip default arm */
6539
427
      continue;
6540
427
    }
6541
6542
2.82k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6543
7.29k
    for (uint32_t j = 0; j < conds->children; j++) {
6544
5.78k
      zend_ast **cond_ast = &conds->child[j];
6545
6546
5.78k
      zend_eval_const_expr(cond_ast);
6547
5.78k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6548
1.19k
        return 0;
6549
1.19k
      }
6550
6551
4.59k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6552
4.59k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6553
125
        return 0;
6554
125
      }
6555
4.59k
    }
6556
2.82k
  }
6557
6558
1.56k
  return 1;
6559
2.88k
}
6560
6561
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6562
527
{
6563
527
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6564
    /* Assert compilation adds a message operand, but this is incompatible with the
6565
     * pipe optimization that uses a temporary znode for the reference elimination.
6566
     * Therefore, disable the optimization for assert.
6567
     * Note that "assert" as a name is always treated as fully qualified. */
6568
292
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6569
292
  }
6570
6571
235
  return true;
6572
527
}
6573
6574
static void zend_compile_pipe(znode *result, zend_ast *ast)
6575
217k
{
6576
217k
  zend_ast *operand_ast = ast->child[0];
6577
217k
  zend_ast *callable_ast = ast->child[1];
6578
6579
217k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6580
1
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6581
1
  }
6582
6583
  /* Compile the left hand side down to a value first. */
6584
217k
  znode operand_result;
6585
217k
  zend_compile_expr(&operand_result, operand_ast);
6586
6587
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6588
   * always fail. They will already fail in complex cases like arrays,
6589
   * so those don't need a wrapper. */
6590
217k
  znode wrapped_operand_result;
6591
217k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6592
170k
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6593
170k
  } else {
6594
47.5k
    wrapped_operand_result = operand_result;
6595
47.5k
  }
6596
6597
  /* Turn the operand into a function parameter list. */
6598
217k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6599
6600
217k
  zend_ast *fcall_ast;
6601
217k
  znode callable_result;
6602
6603
  /* Turn $foo |> bar(...) into bar($foo). */
6604
217k
  if (callable_ast->kind == ZEND_AST_CALL
6605
842
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6606
527
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6607
510
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6608
510
        callable_ast->child[0], arg_list_ast);
6609
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6610
217k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6611
593
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6612
129
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6613
129
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6614
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6615
216k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6616
1.92k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6617
1.38k
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6618
1.38k
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6619
  /* Turn $foo |> $expr into ($expr)($foo) */
6620
215k
  } else {
6621
215k
    zend_compile_expr(&callable_result, callable_ast);
6622
215k
    callable_ast = zend_ast_create_znode(&callable_result);
6623
215k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6624
215k
      callable_ast, arg_list_ast);
6625
215k
  }
6626
6627
217k
  zend_do_extended_stmt(&operand_result);
6628
6629
217k
  zend_compile_expr(result, fcall_ast);
6630
217k
}
6631
6632
static void zend_compile_match(znode *result, zend_ast *ast)
6633
2.88k
{
6634
2.88k
  zend_ast *expr_ast = ast->child[0];
6635
2.88k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6636
2.88k
  bool has_default_arm = false;
6637
2.88k
  uint32_t opnum_match = (uint32_t)-1;
6638
6639
2.88k
  znode expr_node;
6640
2.88k
  zend_compile_expr(&expr_node, expr_ast);
6641
6642
2.88k
  znode case_node;
6643
2.88k
  case_node.op_type = IS_TMP_VAR;
6644
2.88k
  case_node.u.op.var = get_temporary_variable();
6645
6646
2.88k
  uint32_t num_conds = count_match_conds(arms);
6647
2.88k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6648
2.88k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6649
2.88k
  HashTable *jumptable = NULL;
6650
2.88k
  uint32_t *jmpnz_opnums = NULL;
6651
6652
6.53k
  for (uint32_t i = 0; i < arms->children; ++i) {
6653
3.64k
    zend_ast *arm_ast = arms->child[i];
6654
6655
3.64k
    if (!arm_ast->child[0]) {
6656
549
      if (has_default_arm) {
6657
2
        CG(zend_lineno) = arm_ast->lineno;
6658
2
        zend_error_noreturn(E_COMPILE_ERROR,
6659
2
          "Match expressions may only contain one default arm");
6660
2
      }
6661
547
      has_default_arm = true;
6662
547
    }
6663
3.64k
  }
6664
6665
2.88k
  if (uses_jumptable) {
6666
869
    znode jumptable_op;
6667
6668
869
    ALLOC_HASHTABLE(jumptable);
6669
869
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6670
869
    jumptable_op.op_type = IS_CONST;
6671
869
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6672
6673
869
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6674
869
    if (opline->op1_type == IS_CONST) {
6675
304
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6676
304
    }
6677
869
    opnum_match = opline - CG(active_op_array)->opcodes;
6678
2.01k
  } else {
6679
2.01k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6680
2.01k
    uint32_t cond_count = 0;
6681
4.28k
    for (uint32_t i = 0; i < arms->children; ++i) {
6682
2.27k
      zend_ast *arm_ast = arms->child[i];
6683
6684
2.27k
      if (!arm_ast->child[0]) {
6685
448
        continue;
6686
448
      }
6687
6688
1.82k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6689
5.73k
      for (uint32_t j = 0; j < conds->children; j++) {
6690
3.91k
        zend_ast *cond_ast = conds->child[j];
6691
6692
3.91k
        znode cond_node;
6693
3.91k
        zend_compile_expr(&cond_node, cond_ast);
6694
6695
3.91k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6696
3.91k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6697
3.91k
        SET_NODE(opline->result, &case_node);
6698
3.91k
        if (opline->op1_type == IS_CONST) {
6699
1.75k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6700
1.75k
        }
6701
6702
3.91k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6703
6704
3.91k
        cond_count++;
6705
3.91k
      }
6706
1.82k
    }
6707
2.01k
  }
6708
6709
2.88k
  uint32_t opnum_default_jmp = 0;
6710
2.88k
  if (!uses_jumptable) {
6711
2.01k
    opnum_default_jmp = zend_emit_jump(0);
6712
2.01k
  }
6713
6714
2.88k
  bool is_first_case = true;
6715
2.88k
  uint32_t cond_count = 0;
6716
2.88k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6717
6718
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6719
  // for the arm result is freed even though it has not been initialized yet.
6720
2.88k
  if (!has_default_arm) {
6721
2.33k
    if (!uses_jumptable) {
6722
1.56k
      zend_update_jump_target_to_next(opnum_default_jmp);
6723
1.56k
    }
6724
6725
2.33k
    if (jumptable) {
6726
772
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6727
772
      opline->extended_value = get_next_op_number();
6728
772
    }
6729
6730
2.33k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6731
2.33k
    if (opline->op1_type == IS_CONST) {
6732
425
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6733
425
    }
6734
2.33k
    if (arms->children == 0) {
6735
      /* Mark this as an "expression throw" for opcache. */
6736
334
      opline->extended_value = ZEND_THROW_IS_EXPR;
6737
334
    }
6738
2.33k
  }
6739
6740
6.52k
  for (uint32_t i = 0; i < arms->children; ++i) {
6741
3.64k
    zend_ast *arm_ast = arms->child[i];
6742
3.64k
    zend_ast *body_ast = arm_ast->child[1];
6743
6744
3.64k
    if (arm_ast->child[0] != NULL) {
6745
3.09k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6746
6747
10.4k
      for (uint32_t j = 0; j < conds->children; j++) {
6748
7.31k
        zend_ast *cond_ast = conds->child[j];
6749
6750
7.31k
        if (jmpnz_opnums != NULL) {
6751
3.91k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6752
3.91k
        }
6753
6754
7.31k
        if (jumptable) {
6755
3.40k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6756
3.40k
          zval jmp_target;
6757
3.40k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6758
6759
3.40k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6760
3.06k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6761
3.06k
          } else {
6762
340
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6763
340
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6764
340
          }
6765
3.40k
        }
6766
6767
7.31k
        cond_count++;
6768
7.31k
      }
6769
3.09k
    } else {
6770
545
      if (!uses_jumptable) {
6771
448
        zend_update_jump_target_to_next(opnum_default_jmp);
6772
448
      }
6773
6774
545
      if (jumptable) {
6775
97
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6776
97
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6777
97
        opline->extended_value = get_next_op_number();
6778
97
      }
6779
545
    }
6780
6781
3.64k
    znode body_node;
6782
3.64k
    zend_compile_expr(&body_node, body_ast);
6783
6784
3.64k
    if (is_first_case) {
6785
2.54k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6786
2.54k
      is_first_case = false;
6787
2.54k
    } else {
6788
1.09k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6789
1.09k
      SET_NODE(opline_qm_assign->result, result);
6790
1.09k
    }
6791
6792
3.64k
    jmp_end_opnums[i] = zend_emit_jump(0);
6793
3.64k
  }
6794
6795
  // Initialize result in case there is no arm
6796
2.88k
  if (arms->children == 0) {
6797
334
    result->op_type = IS_CONST;
6798
334
    ZVAL_NULL(&result->u.constant);
6799
334
  }
6800
6801
6.52k
  for (uint32_t i = 0; i < arms->children; ++i) {
6802
3.64k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6803
3.64k
  }
6804
6805
2.88k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6806
2.08k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6807
2.08k
    opline->extended_value = ZEND_FREE_SWITCH;
6808
2.08k
  } else if (expr_node.op_type == IS_CONST) {
6809
773
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6810
773
  }
6811
6812
2.88k
  if (jmpnz_opnums != NULL) {
6813
2.01k
    efree(jmpnz_opnums);
6814
2.01k
  }
6815
2.88k
  efree(jmp_end_opnums);
6816
2.88k
}
6817
6818
static void zend_compile_try(const zend_ast *ast) /* {{{ */
6819
9.41k
{
6820
9.41k
  zend_ast *try_ast = ast->child[0];
6821
9.41k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6822
9.41k
  zend_ast *finally_ast = ast->child[2];
6823
6824
9.41k
  uint32_t i, j;
6825
9.41k
  zend_op *opline;
6826
9.41k
  uint32_t try_catch_offset;
6827
9.41k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6828
9.41k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6829
9.41k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6830
6831
9.41k
  if (catches->children == 0 && !finally_ast) {
6832
25
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6833
25
  }
6834
6835
  /* label: try { } must not be equal to try { label: } */
6836
9.39k
  if (CG(context).labels) {
6837
870
    zend_label *label;
6838
870
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6839
870
      if (label->opline_num == get_next_op_number()) {
6840
419
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6841
419
      }
6842
870
      break;
6843
2.61k
    } ZEND_HASH_FOREACH_END();
6844
870
  }
6845
6846
9.39k
  try_catch_offset = zend_add_try_element(get_next_op_number());
6847
6848
9.39k
  if (finally_ast) {
6849
6.09k
    zend_loop_var fast_call;
6850
6.09k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6851
1.36k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6852
1.36k
    }
6853
6.09k
    CG(context).fast_call_var = get_temporary_variable();
6854
6855
    /* Push FAST_CALL on unwind stack */
6856
6.09k
    fast_call.opcode = ZEND_FAST_CALL;
6857
6.09k
    fast_call.var_type = IS_TMP_VAR;
6858
6.09k
    fast_call.var_num = CG(context).fast_call_var;
6859
6.09k
    fast_call.try_catch_offset = try_catch_offset;
6860
6.09k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6861
6.09k
  }
6862
6863
9.39k
  CG(context).try_catch_offset = try_catch_offset;
6864
6865
9.39k
  zend_compile_stmt(try_ast);
6866
6867
9.39k
  if (catches->children != 0) {
6868
3.29k
    jmp_opnums[0] = zend_emit_jump(0);
6869
3.29k
  }
6870
6871
13.3k
  for (i = 0; i < catches->children; ++i) {
6872
3.92k
    const zend_ast *catch_ast = catches->child[i];
6873
3.92k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6874
3.92k
    zend_ast *var_ast = catch_ast->child[1];
6875
3.92k
    zend_ast *stmt_ast = catch_ast->child[2];
6876
3.92k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6877
3.92k
    bool is_last_catch = (i + 1 == catches->children);
6878
6879
3.92k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6880
3.92k
    uint32_t opnum_catch = (uint32_t)-1;
6881
6882
3.92k
    CG(zend_lineno) = catch_ast->lineno;
6883
6884
12.3k
    for (j = 0; j < classes->children; j++) {
6885
8.43k
      zend_ast *class_ast = classes->child[j];
6886
8.43k
      bool is_last_class = (j + 1 == classes->children);
6887
6888
8.43k
      if (!zend_is_const_default_class_ref(class_ast)) {
6889
1
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6890
1
      }
6891
6892
8.43k
      opnum_catch = get_next_op_number();
6893
8.43k
      if (i == 0 && j == 0) {
6894
3.29k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6895
3.29k
      }
6896
6897
8.43k
      opline = get_next_op();
6898
8.43k
      opline->opcode = ZEND_CATCH;
6899
8.43k
      opline->op1_type = IS_CONST;
6900
8.43k
      opline->op1.constant = zend_add_class_name_literal(
6901
8.43k
          zend_resolve_class_name_ast(class_ast));
6902
8.43k
      opline->extended_value = zend_alloc_cache_slot();
6903
6904
8.43k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6905
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6906
1
      }
6907
6908
8.42k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6909
8.42k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6910
6911
8.42k
      if (is_last_catch && is_last_class) {
6912
3.29k
        opline->extended_value |= ZEND_LAST_CATCH;
6913
3.29k
      }
6914
6915
8.42k
      if (!is_last_class) {
6916
4.50k
        jmp_multicatch[j] = zend_emit_jump(0);
6917
4.50k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6918
4.50k
        opline->op2.opline_num = get_next_op_number();
6919
4.50k
      }
6920
8.42k
    }
6921
6922
8.42k
    for (j = 0; j < classes->children - 1; j++) {
6923
4.50k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6924
4.50k
    }
6925
6926
3.92k
    efree(jmp_multicatch);
6927
6928
3.92k
    zend_compile_stmt(stmt_ast);
6929
6930
3.92k
    if (!is_last_catch) {
6931
630
      jmp_opnums[i + 1] = zend_emit_jump(0);
6932
630
    }
6933
6934
3.92k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6935
3.92k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6936
3.92k
    if (!is_last_catch) {
6937
630
      opline->op2.opline_num = get_next_op_number();
6938
630
    }
6939
3.92k
  }
6940
6941
13.3k
  for (i = 0; i < catches->children; ++i) {
6942
3.92k
    zend_update_jump_target_to_next(jmp_opnums[i]);
6943
3.92k
  }
6944
6945
9.38k
  if (finally_ast) {
6946
6.08k
    zend_loop_var discard_exception;
6947
6.08k
    uint32_t opnum_jmp = get_next_op_number() + 1;
6948
6949
    /* Pop FAST_CALL from unwind stack */
6950
6.08k
    zend_stack_del_top(&CG(loop_var_stack));
6951
6952
    /* Push DISCARD_EXCEPTION on unwind stack */
6953
6.08k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6954
6.08k
    discard_exception.var_type = IS_TMP_VAR;
6955
6.08k
    discard_exception.var_num = CG(context).fast_call_var;
6956
6.08k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6957
6958
6.08k
    CG(zend_lineno) = finally_ast->lineno;
6959
6960
6.08k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6961
6.08k
    opline->op1.num = try_catch_offset;
6962
6.08k
    opline->result_type = IS_TMP_VAR;
6963
6.08k
    opline->result.var = CG(context).fast_call_var;
6964
6965
6.08k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6966
6967
6.08k
    zend_compile_stmt(finally_ast);
6968
6969
6.08k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6970
6.08k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6971
6.08k
      = get_next_op_number();
6972
6973
6.08k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6974
6.08k
    opline->op1_type = IS_TMP_VAR;
6975
6.08k
    opline->op1.var = CG(context).fast_call_var;
6976
6.08k
    opline->op2.num = orig_try_catch_offset;
6977
6978
6.08k
    zend_update_jump_target_to_next(opnum_jmp);
6979
6980
6.08k
    CG(context).fast_call_var = orig_fast_call_var;
6981
6982
    /* Pop DISCARD_EXCEPTION from unwind stack */
6983
6.08k
    zend_stack_del_top(&CG(loop_var_stack));
6984
6.08k
  }
6985
6986
9.38k
  CG(context).try_catch_offset = orig_try_catch_offset;
6987
6988
9.38k
  efree(jmp_opnums);
6989
9.38k
}
6990
/* }}} */
6991
6992
/* Encoding declarations must already be handled during parsing */
6993
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
6994
3.18k
{
6995
3.18k
  const zend_ast_list *declares = zend_ast_get_list(ast);
6996
3.18k
  uint32_t i;
6997
7.91k
  for (i = 0; i < declares->children; ++i) {
6998
4.73k
    const zend_ast *declare_ast = declares->child[i];
6999
4.73k
    zend_ast *name_ast = declare_ast->child[0];
7000
4.73k
    zend_ast *value_ast = declare_ast->child[1];
7001
4.73k
    const zend_string *name = zend_ast_get_str(name_ast);
7002
7003
4.73k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7004
126
      if (value_ast->kind != ZEND_AST_ZVAL) {
7005
1
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7006
1
        return 0;
7007
1
      }
7008
7009
125
      if (CG(multibyte)) {
7010
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7011
7012
0
        const zend_encoding *new_encoding, *old_encoding;
7013
0
        zend_encoding_filter old_input_filter;
7014
7015
0
        CG(encoding_declared) = 1;
7016
7017
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7018
0
        if (!new_encoding) {
7019
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7020
0
        } else {
7021
0
          old_input_filter = LANG_SCNG(input_filter);
7022
0
          old_encoding = LANG_SCNG(script_encoding);
7023
0
          zend_multibyte_set_filter(new_encoding);
7024
7025
          /* need to re-scan if input filter changed */
7026
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7027
0
             (old_input_filter && new_encoding != old_encoding)) {
7028
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7029
0
          }
7030
0
        }
7031
7032
0
        zend_string_release_ex(encoding_name, 0);
7033
125
      } else {
7034
125
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7035
125
          "Zend multibyte feature is turned off by settings");
7036
125
      }
7037
125
    }
7038
4.73k
  }
7039
7040
3.18k
  return 1;
7041
3.18k
}
7042
/* }}} */
7043
7044
/* Check whether this is the first statement, not counting declares. */
7045
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7046
2.63k
{
7047
2.63k
  uint32_t i = 0;
7048
2.63k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7049
7050
13.1k
  while (i < file_ast->children) {
7051
13.1k
    if (file_ast->child[i] == ast) {
7052
2.61k
      return SUCCESS;
7053
10.4k
    } else if (file_ast->child[i] == NULL) {
7054
580
      if (!allow_nop) {
7055
0
        return FAILURE;
7056
0
      }
7057
9.90k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7058
18
      return FAILURE;
7059
18
    }
7060
10.4k
    i++;
7061
10.4k
  }
7062
2
  return FAILURE;
7063
2.63k
}
7064
/* }}} */
7065
7066
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7067
6.66k
{
7068
6.66k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7069
6.66k
  zend_ast *stmt_ast = ast->child[1];
7070
6.66k
  zend_declarables orig_declarables = FC(declarables);
7071
6.66k
  uint32_t i;
7072
7073
23.4k
  for (i = 0; i < declares->children; ++i) {
7074
16.8k
    zend_ast *declare_ast = declares->child[i];
7075
16.8k
    zend_ast *name_ast = declare_ast->child[0];
7076
16.8k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7077
16.8k
    zend_string *name = zend_ast_get_str(name_ast);
7078
7079
16.8k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7080
25
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7081
25
    }
7082
7083
16.8k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7084
9.00k
      zval value_zv;
7085
9.00k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7086
9.00k
      FC(declarables).ticks = zval_get_long(&value_zv);
7087
9.00k
      zval_ptr_dtor_nogc(&value_zv);
7088
9.00k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7089
7090
101
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7091
2
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7092
2
          "the very first statement in the script");
7093
2
      }
7094
7.75k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7095
349
      zval value_zv;
7096
7097
349
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7098
3
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7099
3
          "the very first statement in the script");
7100
3
      }
7101
7102
346
      if (ast->child[1] != NULL) {
7103
1
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7104
1
          "use block mode");
7105
1
      }
7106
7107
345
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7108
7109
345
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7110
58
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7111
58
      }
7112
7113
287
      if (Z_LVAL(value_zv) == 1) {
7114
146
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7115
146
      }
7116
7117
7.40k
    } else {
7118
7.40k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7119
7.40k
    }
7120
16.8k
  }
7121
7122
6.57k
  if (stmt_ast) {
7123
650
    zend_compile_stmt(stmt_ast);
7124
7125
650
    FC(declarables) = orig_declarables;
7126
650
  }
7127
6.57k
}
7128
/* }}} */
7129
7130
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7131
2.01M
{
7132
2.01M
  const zend_ast_list *list = zend_ast_get_list(ast);
7133
2.01M
  uint32_t i;
7134
9.00M
  for (i = 0; i < list->children; ++i) {
7135
6.98M
    zend_compile_stmt(list->child[i]);
7136
6.98M
  }
7137
2.01M
}
7138
/* }}} */
7139
7140
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7141
1.65M
{
7142
1.65M
  uint32_t i, n;
7143
7144
1.65M
  func->common.arg_flags[0] = 0;
7145
1.65M
  func->common.arg_flags[1] = 0;
7146
1.65M
  func->common.arg_flags[2] = 0;
7147
1.65M
  if (func->common.arg_info) {
7148
1.65M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7149
1.65M
    i = 0;
7150
3.39M
    while (i < n) {
7151
1.73M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7152
1.73M
      i++;
7153
1.73M
    }
7154
1.65M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7155
623
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7156
8.08k
      while (i < MAX_ARG_FLAG_NUM) {
7157
7.46k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7158
7.46k
        i++;
7159
7.46k
      }
7160
623
    }
7161
1.65M
  }
7162
1.65M
}
7163
/* }}} */
7164
7165
static zend_type zend_compile_single_typename(zend_ast *ast)
7166
562k
{
7167
562k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7168
562k
  if (ast->kind == ZEND_AST_TYPE) {
7169
3.81k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7170
1
      zend_error_noreturn(E_COMPILE_ERROR,
7171
1
        "Cannot use \"static\" when no class scope is active");
7172
1
    }
7173
7174
3.81k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7175
558k
  } else {
7176
558k
    zend_string *type_name = zend_ast_get_str(ast);
7177
558k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7178
7179
558k
    if (type_code != 0) {
7180
102k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7181
1
        zend_error_noreturn(E_COMPILE_ERROR,
7182
1
          "Type declaration '%s' must be unqualified",
7183
1
          ZSTR_VAL(zend_string_tolower(type_name)));
7184
1
      }
7185
7186
      /* Transform iterable into a type union alias */
7187
102k
      if (type_code == IS_ITERABLE) {
7188
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7189
83.3k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7190
83.3k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7191
83.3k
        return iterable;
7192
83.3k
      }
7193
7194
19.0k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7195
456k
    } else {
7196
456k
      const char *correct_name;
7197
456k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7198
456k
      zend_string *class_name = type_name;
7199
7200
456k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7201
439k
        class_name = zend_resolve_class_name_ast(ast);
7202
439k
        zend_assert_valid_class_name(class_name, "a type name");
7203
439k
      } else {
7204
16.0k
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7205
7206
16.0k
        zend_ensure_valid_class_fetch_type(fetch_type);
7207
7208
16.0k
        bool substitute_self_parent = zend_is_scope_known()
7209
427
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7210
7211
16.0k
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7212
          /* Scope might be unknown for unbound closures and traits */
7213
16.0k
          if (substitute_self_parent) {
7214
408
            class_name = CG(active_class_entry)->name;
7215
408
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7216
408
          }
7217
16.0k
        } else {
7218
42
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7219
          /* Scope might be unknown for unbound closures and traits */
7220
42
          if (substitute_self_parent) {
7221
19
            class_name = CG(active_class_entry)->parent_name;
7222
19
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7223
19
          }
7224
39
        }
7225
16.0k
        zend_string_addref(class_name);
7226
16.0k
      }
7227
7228
456k
      if (ast->attr == ZEND_NAME_NOT_FQ
7229
455k
          && zend_is_confusable_type(type_name, &correct_name)
7230
39.0k
          && zend_is_not_imported(type_name)) {
7231
39.0k
        const char *extra =
7232
39.0k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7233
39.0k
        if (correct_name) {
7234
38.5k
          zend_error(E_COMPILE_WARNING,
7235
38.5k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7236
38.5k
            "Write \"\\%s\"%s to suppress this warning",
7237
38.5k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7238
38.5k
        } else {
7239
458
          zend_error(E_COMPILE_WARNING,
7240
458
            "\"%s\" is not a supported builtin type "
7241
458
            "and will be interpreted as a class name. "
7242
458
            "Write \"\\%s\"%s to suppress this warning",
7243
458
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7244
458
        }
7245
39.0k
      }
7246
7247
456k
      class_name = zend_new_interned_string(class_name);
7248
456k
      zend_alloc_ce_cache(class_name);
7249
456k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7250
456k
    }
7251
558k
  }
7252
562k
}
7253
7254
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7255
14.9k
{
7256
14.9k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7257
14.9k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7258
14.9k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7259
14.9k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7260
14.9k
  const zend_type_list *smaller_type_list, *larger_type_list;
7261
14.9k
  bool flipped = false;
7262
7263
14.9k
  if (r_type_list->num_types < l_type_list->num_types) {
7264
4.89k
    smaller_type_list = r_type_list;
7265
4.89k
    larger_type_list = l_type_list;
7266
4.89k
    flipped = true;
7267
10.0k
  } else {
7268
10.0k
    smaller_type_list = l_type_list;
7269
10.0k
    larger_type_list = r_type_list;
7270
10.0k
  }
7271
7272
14.9k
  unsigned int sum = 0;
7273
14.9k
  const zend_type *outer_type;
7274
57.0k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7275
57.0k
    const zend_type *inner_type;
7276
191k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7277
191k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7278
14.1k
        sum++;
7279
14.1k
        break;
7280
14.1k
      }
7281
191k
    ZEND_TYPE_LIST_FOREACH_END();
7282
57.0k
  ZEND_TYPE_LIST_FOREACH_END();
7283
7284
14.9k
  if (sum == smaller_type_list->num_types) {
7285
23
    zend_string *smaller_type_str;
7286
23
    zend_string *larger_type_str;
7287
23
    if (flipped) {
7288
2
      smaller_type_str = zend_type_to_string(right_type);
7289
2
      larger_type_str = zend_type_to_string(left_type);
7290
21
    } else {
7291
21
      smaller_type_str = zend_type_to_string(left_type);
7292
21
      larger_type_str = zend_type_to_string(right_type);
7293
21
    }
7294
23
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7295
12
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7296
12
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7297
12
    } else {
7298
11
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7299
11
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7300
11
    }
7301
23
  }
7302
14.9k
}
7303
7304
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7305
20.0k
{
7306
20.0k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7307
20.0k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7308
7309
20.0k
  const zend_type *single_intersection_type = NULL;
7310
71.0k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7311
71.0k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7312
4
      zend_string *single_type_str = zend_type_to_string(single_type);
7313
4
      zend_string *complete_type = zend_type_to_string(intersection_type);
7314
4
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7315
4
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7316
4
    }
7317
71.0k
  ZEND_TYPE_FOREACH_END();
7318
20.0k
}
7319
7320
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7321
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7322
110k
{
7323
110k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7324
196k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7325
86.0k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7326
14.6k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7327
14.6k
      continue;
7328
14.6k
    }
7329
71.4k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7330
14
      zend_string *single_type_str = zend_type_to_string(type);
7331
14
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7332
14
    }
7333
71.4k
  }
7334
110k
}
7335
7336
static zend_type zend_compile_typename(zend_ast *ast);
7337
7338
static zend_type zend_compile_typename_ex(
7339
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7340
500k
{
7341
500k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7342
500k
  zend_ast_attr orig_ast_attr = ast->attr;
7343
500k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7344
7345
500k
  if (is_marked_nullable) {
7346
5.11k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7347
5.11k
  }
7348
7349
500k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7350
24.6k
    const zend_ast_list *list = zend_ast_get_list(ast);
7351
24.6k
    zend_type_list *type_list;
7352
24.6k
    bool is_composite = false;
7353
24.6k
    bool has_only_iterable_class = true;
7354
24.6k
    ALLOCA_FLAG(use_heap)
7355
7356
24.6k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7357
24.6k
    type_list->num_types = 0;
7358
7359
78.9k
    for (uint32_t i = 0; i < list->children; i++) {
7360
54.3k
      zend_ast *type_ast = list->child[i];
7361
54.3k
      zend_type single_type;
7362
54.3k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7363
7364
54.3k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7365
14.2k
        has_only_iterable_class = false;
7366
14.2k
        is_composite = true;
7367
        /* The first class type can be stored directly as the type ptr payload. */
7368
14.2k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7369
          /* Switch from single name to name list. */
7370
972
          type_list->num_types = 1;
7371
972
          type_list->types[0] = type;
7372
          /* Clear MAY_BE_* type flags */
7373
972
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7374
972
        }
7375
        /* Mark type as list type */
7376
14.2k
        ZEND_TYPE_SET_LIST(type, type_list);
7377
7378
14.2k
        single_type = zend_compile_typename(type_ast);
7379
14.2k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7380
7381
14.2k
        type_list->types[type_list->num_types++] = single_type;
7382
7383
        /* Check for trivially redundant class types */
7384
34.5k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7385
20.3k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7386
14.9k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7387
14.9k
            continue;
7388
14.9k
          }
7389
          /* Type from type list is a simple type */
7390
5.41k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7391
5.41k
        }
7392
14.2k
        continue;
7393
14.2k
      }
7394
7395
40.1k
      single_type = zend_compile_single_typename(type_ast);
7396
40.1k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7397
7398
40.1k
      if (single_type_mask == MAY_BE_ANY) {
7399
1
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7400
1
      }
7401
40.1k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7402
34.9k
        has_only_iterable_class = false;
7403
34.9k
      }
7404
7405
40.1k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7406
40.1k
      if (type_mask_overlap) {
7407
6
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7408
6
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7409
6
        zend_error_noreturn(E_COMPILE_ERROR,
7410
6
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7411
6
      }
7412
7413
40.1k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7414
40.1k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7415
2
        zend_error_noreturn(E_COMPILE_ERROR,
7416
2
          "Type contains both true and false, bool must be used instead");
7417
2
      }
7418
40.1k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7419
      /* Clear MAY_BE_* type flags */
7420
40.1k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7421
7422
40.1k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7423
35.7k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7424
          /* The first class type can be stored directly as the type ptr payload. */
7425
12.7k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7426
12.7k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7427
22.9k
        } else {
7428
22.9k
          if (type_list->num_types == 0) {
7429
            /* Switch from single name to name list. */
7430
10.6k
            type_list->num_types = 1;
7431
10.6k
            type_list->types[0] = type;
7432
            /* Clear MAY_BE_* type flags */
7433
10.6k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7434
10.6k
            ZEND_TYPE_SET_LIST(type, type_list);
7435
10.6k
          }
7436
7437
22.9k
          type_list->types[type_list->num_types++] = single_type;
7438
7439
          /* Check for trivially redundant class types */
7440
22.9k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7441
22.9k
        }
7442
35.7k
      }
7443
40.1k
    }
7444
7445
24.6k
    if (type_list->num_types) {
7446
22.1k
      zend_type_list *list = zend_arena_alloc(
7447
22.1k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7448
22.1k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7449
22.1k
      ZEND_TYPE_SET_LIST(type, list);
7450
22.1k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7451
      /* Inform that the type list is a union type */
7452
22.1k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7453
22.1k
    }
7454
7455
24.6k
    free_alloca(type_list, use_heap);
7456
7457
24.6k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7458
24.6k
    if ((type_mask & MAY_BE_OBJECT) &&
7459
1.08k
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7460
4
      zend_string *type_str = zend_type_to_string(type);
7461
4
      zend_error_noreturn(E_COMPILE_ERROR,
7462
4
        "Type %s contains both object and a class type, which is redundant",
7463
4
        ZSTR_VAL(type_str));
7464
4
    }
7465
475k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7466
41.3k
    const zend_ast_list *list = zend_ast_get_list(ast);
7467
41.3k
    zend_type_list *type_list;
7468
7469
    /* Allocate the type list directly on the arena as it must be a type
7470
     * list of the same number of elements as the AST list has children */
7471
41.3k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7472
41.3k
    type_list->num_types = 0;
7473
7474
41.3k
    ZEND_ASSERT(list->children > 1);
7475
7476
129k
    for (uint32_t i = 0; i < list->children; i++) {
7477
87.7k
      zend_ast *type_ast = list->child[i];
7478
87.7k
      zend_type single_type = zend_compile_single_typename(type_ast);
7479
7480
      /* An intersection of union types cannot exist so invalidate it
7481
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7482
87.7k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7483
1
        zend_string *standard_type_str = zend_type_to_string(single_type);
7484
1
        zend_error_noreturn(E_COMPILE_ERROR,
7485
1
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7486
0
        zend_string_release_ex(standard_type_str, false);
7487
0
      }
7488
      /* An intersection of standard types cannot exist so invalidate it */
7489
87.7k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7490
9
        zend_string *standard_type_str = zend_type_to_string(single_type);
7491
9
        zend_error_noreturn(E_COMPILE_ERROR,
7492
9
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7493
0
        zend_string_release_ex(standard_type_str, false);
7494
0
      }
7495
      /* Check for "self" and "parent" too */
7496
87.7k
      if (
7497
87.7k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7498
87.7k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7499
87.7k
      ) {
7500
2
        zend_error_noreturn(E_COMPILE_ERROR,
7501
2
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7502
2
      }
7503
7504
      /* Add type to the type list */
7505
87.7k
      type_list->types[type_list->num_types++] = single_type;
7506
7507
      /* Check for trivially redundant class types */
7508
87.7k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7509
87.7k
    }
7510
7511
41.3k
    ZEND_ASSERT(list->children == type_list->num_types);
7512
7513
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7514
41.3k
    if (force_allow_null) {
7515
25.5k
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7516
25.5k
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7517
25.5k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7518
25.5k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7519
7520
25.5k
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7521
25.5k
      dnf_type_list->num_types = 1;
7522
25.5k
      dnf_type_list->types[0] = intersection_type;
7523
25.5k
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7524
      /* Inform that the type list is a DNF type */
7525
25.5k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7526
25.5k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7527
25.5k
    } else {
7528
15.8k
      ZEND_TYPE_SET_LIST(type, type_list);
7529
      /* Inform that the type list is an intersection type */
7530
15.8k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7531
15.8k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7532
15.8k
    }
7533
434k
  } else {
7534
434k
    type = zend_compile_single_typename(ast);
7535
434k
  }
7536
7537
500k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7538
7539
500k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7540
1
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7541
1
  }
7542
7543
500k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7544
1
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7545
1
  }
7546
7547
500k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7548
29.6k
    *forced_allow_null = true;
7549
29.6k
  }
7550
7551
500k
  if (is_marked_nullable || force_allow_null) {
7552
34.9k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7553
34.9k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7554
34.9k
  }
7555
7556
500k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7557
2
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7558
2
  }
7559
7560
500k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7561
2
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7562
2
  }
7563
7564
500k
  ast->attr = orig_ast_attr;
7565
500k
  return type;
7566
500k
}
7567
/* }}} */
7568
7569
static zend_type zend_compile_typename(zend_ast *ast)
7570
74.3k
{
7571
74.3k
  bool forced_allow_null;
7572
74.3k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7573
74.3k
}
7574
7575
/* May convert value from int to float. */
7576
static bool zend_is_valid_default_value(zend_type type, zval *value)
7577
3.93k
{
7578
3.93k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7579
3.93k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7580
1.51k
    return 1;
7581
1.51k
  }
7582
2.41k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7583
    /* Integers are allowed as initializers for floating-point values. */
7584
2.35k
    convert_to_double(value);
7585
2.35k
    return 1;
7586
2.35k
  }
7587
63
  return 0;
7588
2.41k
}
7589
7590
static void zend_compile_attributes(
7591
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7592
1.64M
) /* {{{ */ {
7593
1.64M
  zend_attribute *attr;
7594
1.64M
  zend_internal_attribute *config;
7595
7596
1.64M
  const zend_ast_list *list = zend_ast_get_list(ast);
7597
1.64M
  uint32_t g, i, j;
7598
7599
1.64M
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7600
7601
3.29M
  for (g = 0; g < list->children; g++) {
7602
1.65M
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7603
7604
1.65M
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7605
7606
4.39M
    for (i = 0; i < group->children; i++) {
7607
2.74M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7608
7609
2.74M
      const zend_ast *el = group->child[i];
7610
7611
2.74M
      if (el->child[1] &&
7612
65.3k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7613
3
          zend_error_noreturn(E_COMPILE_ERROR,
7614
3
              "Cannot create Closure as attribute argument");
7615
3
      }
7616
7617
2.74M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7618
2.74M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7619
2.74M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7620
7621
2.74M
      config = zend_internal_attribute_get(lcname);
7622
2.74M
      zend_string_release(lcname);
7623
7624
      /* Exclude internal attributes that do not match on promoted properties. */
7625
2.74M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7626
384
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7627
1
          zend_string_release(name);
7628
1
          continue;
7629
1
        }
7630
384
      }
7631
7632
2.74M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7633
2.74M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7634
2.74M
      attr = zend_add_attribute(
7635
2.74M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7636
2.74M
      zend_string_release(name);
7637
7638
      /* Populate arguments */
7639
2.74M
      if (args) {
7640
65.3k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7641
7642
65.3k
        bool uses_named_args = false;
7643
143k
        for (j = 0; j < args->children; j++) {
7644
78.2k
          zend_ast **arg_ast_ptr = &args->child[j];
7645
78.2k
          zend_ast *arg_ast = *arg_ast_ptr;
7646
7647
78.2k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7648
1
            zend_error_noreturn(E_COMPILE_ERROR,
7649
1
              "Cannot use unpacking in attribute argument list");
7650
1
          }
7651
7652
78.2k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7653
2.19k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7654
2.19k
            arg_ast_ptr = &arg_ast->child[1];
7655
2.19k
            uses_named_args = true;
7656
7657
12.0k
            for (uint32_t k = 0; k < j; k++) {
7658
9.88k
              if (attr->args[k].name &&
7659
7.85k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7660
5
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7661
5
                  ZSTR_VAL(attr->args[j].name));
7662
5
              }
7663
9.88k
            }
7664
76.0k
          } else if (uses_named_args) {
7665
25
            zend_error_noreturn(E_COMPILE_ERROR,
7666
25
              "Cannot use positional argument after named argument");
7667
25
          }
7668
7669
78.2k
          zend_const_expr_to_zval(
7670
78.2k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7671
78.2k
        }
7672
65.3k
      }
7673
2.74M
    }
7674
1.65M
  }
7675
7676
1.64M
  if (*attributes != NULL) {
7677
    /* Allow delaying target validation for forward compatibility. */
7678
1.64M
    const zend_attribute *delayed_target_validation = NULL;
7679
1.64M
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7680
63.5k
      ZEND_ASSERT(offset >= 1);
7681
      /* zend_get_parameter_attribute_str will add 1 too */
7682
63.5k
      delayed_target_validation = zend_get_parameter_attribute_str(
7683
63.5k
        *attributes,
7684
63.5k
        "delayedtargetvalidation",
7685
63.5k
        strlen("delayedtargetvalidation"),
7686
63.5k
        offset - 1
7687
63.5k
      );
7688
1.58M
    } else {
7689
1.58M
      delayed_target_validation = zend_get_attribute_str(
7690
1.58M
        *attributes,
7691
1.58M
        "delayedtargetvalidation",
7692
1.58M
        strlen("delayedtargetvalidation")
7693
1.58M
      );
7694
1.58M
    }
7695
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7696
10.7M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7697
10.7M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7698
3.71M
        continue;
7699
3.71M
      }
7700
7701
10.7M
      bool run_validator = true;
7702
2.93k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7703
72
        if (delayed_target_validation == NULL) {
7704
11
          zend_string *location = zend_get_attribute_target_names(target);
7705
11
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7706
7707
11
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7708
11
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7709
11
          );
7710
11
        }
7711
61
        run_validator = false;
7712
61
      }
7713
7714
2.92k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7715
2.92k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7716
8
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7717
8
        }
7718
2.92k
      }
7719
7720
      /* Validators are not run if the target is already invalid */
7721
2.92k
      if (run_validator && config->validator != NULL) {
7722
1.67k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7723
1.67k
        if (error != NULL) {
7724
280
          if (delayed_target_validation == NULL) {
7725
34
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7726
0
            zend_string_efree(error);
7727
246
          } else {
7728
246
            attr->validation_error = error;
7729
246
          }
7730
280
        }
7731
1.67k
      }
7732
2.92k
    } ZEND_HASH_FOREACH_END();
7733
1.64M
  }
7734
1.64M
}
7735
/* }}} */
7736
7737
static void zend_compile_property_hooks(
7738
    zend_property_info *prop_info, zend_string *prop_name,
7739
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7740
7741
typedef struct {
7742
  const zend_string *property_name;
7743
  bool uses_property;
7744
} find_property_usage_context;
7745
7746
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7747
25.3k
{
7748
25.3k
  zend_ast *ast = *ast_ptr;
7749
25.3k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7750
7751
25.3k
  if (ast == NULL) {
7752
491
    return;
7753
24.8k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7754
2.00k
    const zend_ast *object_ast = ast->child[0];
7755
2.00k
    zend_ast *property_ast = ast->child[1];
7756
7757
2.00k
    if (object_ast->kind == ZEND_AST_VAR
7758
1.51k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7759
1.44k
     && property_ast->kind == ZEND_AST_ZVAL) {
7760
1.18k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7761
1.18k
      const zval *property = zend_ast_get_zval(property_ast);
7762
1.18k
      if (Z_TYPE_P(object) == IS_STRING
7763
1.11k
        && Z_TYPE_P(property) == IS_STRING
7764
1.11k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7765
747
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7766
121
        context->uses_property = true;
7767
        /* No need to look for references in this branch. */
7768
121
        return;
7769
121
      }
7770
1.18k
    }
7771
2.00k
  }
7772
7773
  /* Don't search across function/class boundaries. */
7774
24.7k
  if (!zend_ast_is_special(ast)) {
7775
15.0k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7776
15.0k
  }
7777
24.7k
}
7778
7779
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7780
3.54k
{
7781
3.54k
  if (zend_string_equals_literal_ci(hook_name, "set")
7782
1.09k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7783
376
    return true;
7784
376
  }
7785
7786
3.17k
  find_property_usage_context context = { property_name, false };
7787
3.17k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7788
3.17k
  return context.uses_property;
7789
3.54k
}
7790
7791
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7792
34.5k
{
7793
34.5k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7794
177
    return true;
7795
177
  }
7796
34.4k
  if (!hooks_ast) {
7797
31.8k
    return false;
7798
31.8k
  }
7799
7800
34.4k
  bool is_virtual = true;
7801
7802
2.55k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7803
6.69k
  for (uint32_t i = 0; i < hooks->children; i++) {
7804
4.14k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
7805
4.14k
    zend_ast *body = hook->child[2];
7806
4.14k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7807
437
      is_virtual = false;
7808
437
    }
7809
4.14k
  }
7810
7811
2.55k
  return is_virtual;
7812
34.4k
}
7813
7814
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7815
1.75M
{
7816
1.75M
  zend_ast_list *list = zend_ast_get_list(ast);
7817
1.75M
  uint32_t i;
7818
1.75M
  zend_op_array *op_array = CG(active_op_array);
7819
1.75M
  zend_arg_info *arg_infos;
7820
7821
1.75M
  if (return_type_ast || fallback_return_type) {
7822
    /* Use op_array->arg_info[-1] for return type */
7823
29.8k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7824
29.8k
    arg_infos->name = NULL;
7825
29.8k
    if (return_type_ast) {
7826
29.2k
      arg_infos->type = zend_compile_typename(return_type_ast);
7827
29.2k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7828
29.2k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7829
29.2k
    } else {
7830
558
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7831
558
    }
7832
29.8k
    arg_infos++;
7833
29.8k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7834
7835
29.8k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7836
2.41k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7837
357
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7838
357
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7839
357
      zend_string_release(func_name);
7840
357
    }
7841
1.72M
  } else {
7842
1.72M
    if (list->children == 0) {
7843
102k
      return;
7844
102k
    }
7845
1.62M
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7846
1.62M
  }
7847
7848
  /* Find last required parameter number for deprecation message. */
7849
1.65M
  uint32_t last_required_param = (uint32_t) -1;
7850
3.39M
  for (i = 0; i < list->children; ++i) {
7851
1.73M
    zend_ast *param_ast = list->child[i];
7852
1.73M
    zend_ast *default_ast_ptr = param_ast->child[2];
7853
1.73M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7854
1.73M
    if (!default_ast_ptr && !is_variadic) {
7855
1.69M
      last_required_param = i;
7856
1.69M
    }
7857
1.73M
  }
7858
7859
1.65M
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
7860
3.39M
  for (i = 0; i < list->children; ++i) {
7861
1.73M
    zend_ast *param_ast = list->child[i];
7862
1.73M
    zend_ast *type_ast = param_ast->child[0];
7863
1.73M
    zend_ast *var_ast = param_ast->child[1];
7864
1.73M
    zend_ast **default_ast_ptr = &param_ast->child[2];
7865
1.73M
    zend_ast *attributes_ast = param_ast->child[3];
7866
1.73M
    zend_ast *doc_comment_ast = param_ast->child[4];
7867
1.73M
    zend_ast *hooks_ast = param_ast->child[5];
7868
1.73M
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7869
1.73M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7870
1.73M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7871
1.73M
    uint32_t property_flags = param_ast->attr & promotion_flags;
7872
1.73M
    bool is_promoted = property_flags || hooks_ast;
7873
7874
1.73M
    CG(zend_lineno) = param_ast->lineno;
7875
7876
1.73M
    znode var_node, default_node;
7877
1.73M
    uint8_t opcode;
7878
1.73M
    zend_op *opline;
7879
1.73M
    zend_arg_info *arg_info;
7880
7881
1.73M
    if (zend_is_auto_global(name)) {
7882
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7883
2
        ZSTR_VAL(name));
7884
2
    }
7885
7886
1.73M
    var_node.op_type = IS_CV;
7887
1.73M
    var_node.u.op.var = lookup_cv(name);
7888
7889
1.73M
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7890
19
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7891
19
        ZSTR_VAL(name));
7892
1.73M
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7893
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7894
1.73M
    } else if (zend_string_equals_literal(name, "http_response_header")) {
7895
80
      CG(context).has_assigned_to_http_response_header = true;
7896
80
    }
7897
7898
1.73M
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7899
2
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7900
2
    }
7901
7902
1.73M
    if (is_variadic) {
7903
701
      opcode = ZEND_RECV_VARIADIC;
7904
701
      default_node.op_type = IS_UNUSED;
7905
701
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7906
7907
701
      if (*default_ast_ptr) {
7908
1
        zend_error_noreturn(E_COMPILE_ERROR,
7909
1
          "Variadic parameter cannot have a default value");
7910
1
      }
7911
1.73M
    } else if (*default_ast_ptr) {
7912
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7913
43.5k
      uint32_t cops = CG(compiler_options);
7914
43.5k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7915
43.5k
      opcode = ZEND_RECV_INIT;
7916
43.5k
      default_node.op_type = IS_CONST;
7917
43.5k
      zend_const_expr_to_zval(
7918
43.5k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7919
43.5k
      CG(compiler_options) = cops;
7920
1.69M
    } else {
7921
1.69M
      opcode = ZEND_RECV;
7922
1.69M
      default_node.op_type = IS_UNUSED;
7923
1.69M
      op_array->required_num_args = i + 1;
7924
1.69M
    }
7925
7926
1.73M
    arg_info = &arg_infos[i];
7927
1.73M
    arg_info->name = zend_string_copy(name);
7928
1.73M
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7929
1.73M
    arg_info->default_value = NULL;
7930
7931
1.73M
    if (attributes_ast) {
7932
63.5k
      zend_compile_attributes(
7933
63.5k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7934
63.5k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7935
63.5k
      );
7936
63.5k
    }
7937
7938
1.73M
    bool forced_allow_nullable = false;
7939
1.73M
    if (type_ast) {
7940
426k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7941
426k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7942
7943
426k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7944
426k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7945
426k
      if (forced_allow_nullable) {
7946
29.6k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7947
29.6k
        zend_error(E_DEPRECATED,
7948
29.6k
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7949
29.6k
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7950
29.6k
        zend_string_release(func_name);
7951
29.6k
      }
7952
7953
426k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7954
1
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7955
1
      }
7956
7957
426k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7958
1
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7959
1
      }
7960
7961
426k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7962
1.63k
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7963
9
        zend_string *type_str = zend_type_to_string(arg_info->type);
7964
9
        zend_error_noreturn(E_COMPILE_ERROR,
7965
9
          "Cannot use %s as default value for parameter $%s of type %s",
7966
9
          zend_get_type_by_const(default_type),
7967
9
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7968
9
      }
7969
426k
    }
7970
1.73M
    if (last_required_param != (uint32_t) -1
7971
1.72M
     && i < last_required_param
7972
101k
     && default_node.op_type == IS_CONST) {
7973
      /* Ignore parameters of the form "Type $param = null".
7974
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7975
31.7k
      if (!forced_allow_nullable) {
7976
5.65k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7977
5.65k
        zend_ast *required_param_ast = list->child[last_required_param];
7978
5.65k
        zend_error(E_DEPRECATED,
7979
5.65k
          "%s(): Optional parameter $%s declared before required parameter $%s "
7980
5.65k
          "is implicitly treated as a required parameter",
7981
5.65k
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7982
5.65k
        zend_string_release(func_name);
7983
5.65k
      }
7984
7985
      /* Regardless of whether we issue a deprecation, convert this parameter into
7986
       * a required parameter without a default value. This ensures that it cannot be
7987
       * used as an optional parameter even with named parameters. */
7988
31.7k
      opcode = ZEND_RECV;
7989
31.7k
      default_node.op_type = IS_UNUSED;
7990
31.7k
      zval_ptr_dtor(&default_node.u.constant);
7991
31.7k
    }
7992
7993
1.73M
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
7994
1.73M
    SET_NODE(opline->result, &var_node);
7995
1.73M
    opline->op1.num = i + 1;
7996
7997
1.73M
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
7998
1.73M
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
7999
1.73M
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8000
1.73M
    if (opcode == ZEND_RECV) {
8001
1.72M
      opline->op2.num = type_ast ?
8002
1.30M
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8003
1.72M
    }
8004
8005
1.73M
    if (is_promoted) {
8006
234
      const zend_op_array *active_op_array = CG(active_op_array);
8007
234
      zend_class_entry *scope = active_op_array->scope;
8008
8009
234
      bool is_ctor =
8010
234
        scope && zend_is_constructor(active_op_array->function_name);
8011
234
      if (!is_ctor) {
8012
26
        zend_error_noreturn(E_COMPILE_ERROR,
8013
26
          "Cannot declare promoted property outside a constructor");
8014
26
      }
8015
208
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8016
207
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8017
1
        zend_error_noreturn(E_COMPILE_ERROR,
8018
1
          "Cannot declare promoted property in an abstract constructor");
8019
1
      }
8020
207
      if (is_variadic) {
8021
1
        zend_error_noreturn(E_COMPILE_ERROR,
8022
1
          "Cannot declare variadic promoted property");
8023
1
      }
8024
206
      if (zend_hash_exists(&scope->properties_info, name)) {
8025
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8026
1
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8027
1
      }
8028
205
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8029
1
        zend_string *str = zend_type_to_string(arg_info->type);
8030
1
        zend_error_noreturn(E_COMPILE_ERROR,
8031
1
          "Property %s::$%s cannot have type %s",
8032
1
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8033
1
      }
8034
8035
204
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8036
3
        property_flags |= ZEND_ACC_READONLY;
8037
3
      }
8038
8039
      /* Recompile the type, as it has different memory management requirements. */
8040
204
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8041
204
      if (type_ast) {
8042
135
        type = zend_compile_typename(type_ast);
8043
135
      }
8044
8045
      /* Don't give the property an explicit default value. For typed properties this means
8046
       * uninitialized, for untyped properties it means an implicit null default value.
8047
       * Properties with hooks get an implicit default value of undefined until inheritance,
8048
       * where it is changed to null only once we know it is not virtual. If we were to set it
8049
       * here, we couldn't verify that a true virtual property must not have an explicit
8050
       * default value. */
8051
204
      zval default_value;
8052
204
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8053
183
        ZVAL_UNDEF(&default_value);
8054
183
      } else {
8055
21
        if (property_flags & ZEND_ACC_READONLY) {
8056
2
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8057
2
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8058
2
        }
8059
8060
19
        ZVAL_NULL(&default_value);
8061
19
      }
8062
8063
202
      zend_string *doc_comment =
8064
202
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8065
202
      zend_property_info *prop = zend_declare_typed_property(
8066
202
        scope, name, &default_value,
8067
202
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8068
202
        doc_comment, type);
8069
202
      if (hooks_ast) {
8070
114
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8071
114
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8072
114
      }
8073
202
      if (attributes_ast) {
8074
45
        zend_compile_attributes(
8075
45
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8076
8077
45
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8078
45
        if (override_attribute) {
8079
1
          prop->flags |= ZEND_ACC_OVERRIDE;
8080
1
        }
8081
45
      }
8082
202
    }
8083
1.73M
  }
8084
8085
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8086
1.65M
  op_array->num_args = list->children;
8087
1.65M
  op_array->arg_info = arg_infos;
8088
8089
  /* Don't count the variadic argument */
8090
1.65M
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8091
695
    op_array->num_args--;
8092
695
  }
8093
1.65M
  zend_set_function_arg_flags((zend_function*)op_array);
8094
8095
3.39M
  for (i = 0; i < list->children; i++) {
8096
1.73M
    zend_ast *param_ast = list->child[i];
8097
1.73M
    zend_ast *hooks_ast = param_ast->child[5];
8098
1.73M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8099
1.73M
    uint32_t flags = param_ast->attr & promotion_flags;
8100
1.73M
    bool is_promoted = flags || hooks_ast;
8101
1.73M
    if (!is_promoted) {
8102
1.73M
      continue;
8103
1.73M
    }
8104
8105
201
    CG(zend_lineno) = param_ast->lineno;
8106
8107
    /* Emit $this->prop = $prop for promoted properties. */
8108
201
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8109
201
    znode name_node, value_node;
8110
201
    name_node.op_type = IS_CONST;
8111
201
    ZVAL_STR_COPY(&name_node.u.constant, name);
8112
201
    value_node.op_type = IS_CV;
8113
201
    value_node.u.op.var = lookup_cv(name);
8114
8115
201
    zend_op *opline = zend_emit_op(NULL,
8116
201
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8117
201
    opline->extended_value = zend_alloc_cache_slots(3);
8118
201
    zend_emit_op_data(&value_node);
8119
201
  }
8120
1.65M
}
8121
/* }}} */
8122
8123
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8124
932
{
8125
932
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8126
932
  uint32_t i;
8127
8128
932
  if (!list->children) {
8129
0
    return;
8130
0
  }
8131
8132
932
  if (!op_array->static_variables) {
8133
932
    op_array->static_variables = zend_new_array(8);
8134
932
  }
8135
8136
5.94k
  for (i = 0; i < list->children; ++i) {
8137
5.02k
    zend_ast *var_name_ast = list->child[i];
8138
5.02k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8139
5.02k
    uint32_t mode = var_name_ast->attr;
8140
5.02k
    zend_op *opline;
8141
5.02k
    zval *value;
8142
8143
5.02k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8144
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8145
1
    }
8146
8147
5.02k
    if (zend_is_auto_global(var_name)) {
8148
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8149
1
    }
8150
8151
5.02k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8152
5.02k
    if (!value) {
8153
8
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8154
8
        "Cannot use variable $%S twice", var_name);
8155
8
    }
8156
8157
5.01k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8158
8159
5.01k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8160
5.01k
    opline->op2_type = IS_CV;
8161
5.01k
    opline->op2.var = lookup_cv(var_name);
8162
5.01k
    opline->extended_value =
8163
5.01k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8164
5.01k
  }
8165
932
}
8166
/* }}} */
8167
8168
typedef struct {
8169
  HashTable uses;
8170
  bool varvars_used;
8171
} closure_info;
8172
8173
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8174
8175
1.80M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8176
1.80M
  if (!ast) {
8177
7.54k
    return;
8178
7.54k
  }
8179
8180
1.79M
  if (ast->kind == ZEND_AST_VAR) {
8181
121k
    zend_ast *name_ast = ast->child[0];
8182
121k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8183
117k
      zend_string *name = zend_ast_get_str(name_ast);
8184
117k
      if (zend_is_auto_global(name)) {
8185
        /* These is no need to explicitly import auto-globals. */
8186
3.79k
        return;
8187
3.79k
      }
8188
8189
113k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8190
        /* $this does not need to be explicitly imported. */
8191
14.6k
        return;
8192
14.6k
      }
8193
8194
99.2k
      zend_hash_add_empty_element(&info->uses, name);
8195
99.2k
    } else {
8196
3.44k
      info->varvars_used = true;
8197
3.44k
      find_implicit_binds_recursively(info, name_ast);
8198
3.44k
    }
8199
1.67M
  } else if (zend_ast_is_list(ast)) {
8200
26.4k
    const zend_ast_list *list = zend_ast_get_list(ast);
8201
26.4k
    uint32_t i;
8202
205k
    for (i = 0; i < list->children; i++) {
8203
179k
      find_implicit_binds_recursively(info, list->child[i]);
8204
179k
    }
8205
1.64M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8206
    /* For normal closures add the use() list. */
8207
892
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8208
892
    zend_ast *uses_ast = closure_ast->child[1];
8209
892
    if (uses_ast) {
8210
503
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8211
503
      uint32_t i;
8212
3.68k
      for (i = 0; i < uses_list->children; i++) {
8213
3.18k
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8214
3.18k
      }
8215
503
    }
8216
1.64M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8217
    /* For arrow functions recursively check the expression. */
8218
644k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8219
644k
    closure_info inner_info;
8220
644k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8221
644k
    if (inner_info.varvars_used) {
8222
6.82k
      info->varvars_used = true;
8223
6.82k
    }
8224
644k
    if (zend_hash_num_elements(&inner_info.uses)) {
8225
574k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8226
574k
    }
8227
644k
    zend_hash_destroy(&inner_info.uses);
8228
1.00M
  } else if (!zend_ast_is_special(ast)) {
8229
820k
    uint32_t i, children = zend_ast_get_num_children(ast);
8230
1.75M
    for (i = 0; i < children; i++) {
8231
937k
      find_implicit_binds_recursively(info, ast->child[i]);
8232
937k
    }
8233
820k
  }
8234
1.79M
}
8235
8236
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8237
680k
{
8238
680k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8239
680k
  uint32_t i;
8240
8241
680k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8242
680k
  info->varvars_used = false;
8243
8244
680k
  find_implicit_binds_recursively(info, stmt_ast);
8245
8246
  /* Remove variables that are parameters */
8247
737k
  for (i = 0; i < param_list->children; i++) {
8248
57.4k
    const zend_ast *param_ast = param_list->child[i];
8249
57.4k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8250
57.4k
  }
8251
680k
}
8252
8253
static void compile_implicit_lexical_binds(
8254
    const closure_info *info, znode *closure, zend_op_array *op_array)
8255
35.4k
{
8256
35.4k
  zend_string *var_name;
8257
35.4k
  zend_op *opline;
8258
8259
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8260
35.4k
  if (zend_hash_num_elements(&info->uses) == 0) {
8261
23.9k
    return;
8262
23.9k
  }
8263
8264
11.4k
  if (!op_array->static_variables) {
8265
11.4k
    op_array->static_variables = zend_new_array(8);
8266
11.4k
  }
8267
8268
155k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8269
155k
    zval *value = zend_hash_add(
8270
155k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8271
155k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8272
8273
155k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8274
155k
    opline->op2_type = IS_CV;
8275
155k
    opline->op2.var = lookup_cv(var_name);
8276
155k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8277
155k
  ZEND_HASH_FOREACH_END();
8278
11.4k
}
8279
8280
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8281
922
{
8282
922
  const zend_op_array *op_array = CG(active_op_array);
8283
922
  const zend_ast_list *list = zend_ast_get_list(ast);
8284
922
  uint32_t i;
8285
8286
5.89k
  for (i = 0; i < list->children; ++i) {
8287
4.97k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8288
4.97k
    zend_ast *var_ast = list->child[i];
8289
4.97k
    zend_string *var_name = zend_ast_get_str(var_ast);
8290
4.97k
    zval zv;
8291
4.97k
    ZVAL_NULL(&zv);
8292
8293
4.97k
    {
8294
4.97k
      int i;
8295
19.0k
      for (i = 0; i < op_array->last_var; i++) {
8296
14.0k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8297
1
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8298
1
            "Cannot use lexical variable $%S as a parameter name", var_name);
8299
1
        }
8300
14.0k
      }
8301
4.97k
    }
8302
8303
4.97k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8304
8305
4.97k
    if (var_ast->attr) {
8306
550
      mode |= ZEND_BIND_REF;
8307
550
    }
8308
8309
4.97k
    zend_compile_static_var_common(var_name, &zv, mode);
8310
4.97k
  }
8311
922
}
8312
/* }}} */
8313
8314
static void zend_compile_implicit_closure_uses(const closure_info *info)
8315
35.3k
{
8316
35.3k
  zend_string *var_name;
8317
203k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8318
203k
    zval zv;
8319
203k
    ZVAL_NULL(&zv);
8320
203k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8321
203k
  ZEND_HASH_FOREACH_END();
8322
35.3k
}
8323
8324
685
static void add_stringable_interface(zend_class_entry *ce) {
8325
1.26k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8326
582
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8327
      /* Interface already explicitly implemented */
8328
1
      return;
8329
1
    }
8330
582
  }
8331
8332
684
  ce->num_interfaces++;
8333
684
  ce->interface_names =
8334
684
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8335
  // TODO: Add known interned strings instead?
8336
684
  ce->interface_names[ce->num_interfaces - 1].name =
8337
684
    ZSTR_INIT_LITERAL("Stringable", 0);
8338
684
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8339
684
    ZSTR_INIT_LITERAL("stringable", 0);
8340
684
}
8341
8342
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8343
28.1k
{
8344
28.1k
  zend_class_entry *ce = CG(active_class_entry);
8345
28.1k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8346
28.1k
  uint32_t fn_flags = op_array->fn_flags;
8347
8348
28.1k
  zend_string *lcname;
8349
8350
28.1k
  if (fn_flags & ZEND_ACC_READONLY) {
8351
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8352
0
  }
8353
8354
28.1k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8355
101
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8356
101
  }
8357
8358
28.1k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8359
235
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8360
    // Don't say that the class should be declared abstract if it is
8361
    // anonymous or an enum and can't be abstract
8362
10
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8363
1
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8364
1
        ZSTR_VAL(name));
8365
9
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8366
8
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8367
8
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8368
8
    } else {
8369
1
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8370
1
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8371
1
    }
8372
10
  }
8373
8374
28.1k
  if (in_interface) {
8375
205
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8376
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8377
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8378
1
    }
8379
204
    if (fn_flags & ZEND_ACC_FINAL) {
8380
1
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8381
1
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8382
1
    }
8383
203
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8384
203
  }
8385
8386
28.1k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8387
428
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8388
1
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8389
1
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8390
1
    }
8391
8392
427
    if (has_body) {
8393
3
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8394
3
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8395
3
    }
8396
8397
424
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8398
27.6k
  } else if (!has_body) {
8399
4
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8400
4
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8401
4
  }
8402
8403
28.0k
  op_array->scope = ce;
8404
28.0k
  op_array->function_name = zend_string_copy(name);
8405
8406
28.0k
  lcname = zend_string_tolower(name);
8407
28.0k
  lcname = zend_new_interned_string(lcname);
8408
8409
28.0k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8410
10
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8411
10
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8412
10
  }
8413
8414
28.0k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8415
28.0k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8416
686
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8417
685
    add_stringable_interface(ce);
8418
685
  }
8419
8420
28.0k
  return lcname;
8421
28.0k
}
8422
/* }}} */
8423
8424
1.72M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8425
1.72M
  zend_op_array *op_array = CG(active_op_array);
8426
1.72M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8427
1.72M
  op_array->dynamic_func_defs = erealloc(
8428
1.72M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8429
1.72M
  op_array->dynamic_func_defs[def_offset] = def;
8430
1.72M
  return def_offset;
8431
1.72M
}
8432
8433
enum func_decl_level {
8434
  FUNC_DECL_LEVEL_TOPLEVEL,
8435
  FUNC_DECL_LEVEL_NESTED,
8436
  FUNC_DECL_LEVEL_CONSTEXPR,
8437
};
8438
8439
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8440
1.72M
{
8441
1.72M
  zend_string *unqualified_name, *name, *lcname;
8442
1.72M
  zend_op *opline;
8443
8444
1.72M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8445
1.68M
    zend_string *filename = op_array->filename;
8446
1.68M
    uint32_t start_lineno = decl->start_lineno;
8447
8448
1.68M
    zend_string *class = zend_empty_string;
8449
1.68M
    zend_string *separator = zend_empty_string;
8450
1.68M
    zend_string *function = filename;
8451
1.68M
    const char *parens = "";
8452
8453
1.68M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8454
1.67M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8455
        /* If the parent function is a closure, don't redundantly
8456
         * add the classname and parentheses.
8457
         */
8458
1.67M
        function = CG(active_op_array)->function_name;
8459
1.67M
      } else {
8460
4.67k
        function = CG(active_op_array)->function_name;
8461
4.67k
        parens = "()";
8462
8463
4.67k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8464
4.63k
          class = CG(active_class_entry)->name;
8465
4.63k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8466
4.63k
        }
8467
4.67k
      }
8468
1.67M
    }
8469
8470
1.68M
    unqualified_name = zend_strpprintf_unchecked(
8471
1.68M
      0,
8472
1.68M
      "{closure:%S%S%S%s:%" PRIu32 "}",
8473
1.68M
      class,
8474
1.68M
      separator,
8475
1.68M
      function,
8476
1.68M
      parens,
8477
1.68M
      start_lineno
8478
1.68M
    );
8479
8480
1.68M
    op_array->function_name = name = unqualified_name;
8481
1.68M
  } else {
8482
39.2k
    unqualified_name = decl->name;
8483
39.2k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8484
39.2k
  }
8485
8486
1.72M
  lcname = zend_string_tolower(name);
8487
8488
1.72M
  if (FC(imports_function)) {
8489
69
    const zend_string *import_name =
8490
69
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8491
69
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8492
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8493
2
        ZSTR_VAL(name));
8494
2
    }
8495
69
  }
8496
8497
1.72M
  if (zend_string_equals_literal(lcname, "__autoload")) {
8498
1
    zend_error_noreturn(E_COMPILE_ERROR,
8499
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8500
1
  }
8501
8502
1.72M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8503
1
    zend_error(E_COMPILE_ERROR,
8504
1
      "Defining a custom assert() function is not allowed, "
8505
1
      "as the function has special semantics");
8506
1
  }
8507
8508
1.72M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8509
1.72M
  switch (level) {
8510
1.72M
    case FUNC_DECL_LEVEL_NESTED: {
8511
1.72M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8512
1.72M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8513
1.68M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8514
1.68M
        opline->op2.num = func_ref;
8515
1.68M
      } else {
8516
38.4k
        opline = get_next_op();
8517
38.4k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8518
38.4k
        opline->op1_type = IS_CONST;
8519
38.4k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8520
38.4k
        opline->op2.num = func_ref;
8521
38.4k
      }
8522
1.72M
      break;
8523
0
    }
8524
91
    case FUNC_DECL_LEVEL_CONSTEXPR:
8525
863
    case FUNC_DECL_LEVEL_TOPLEVEL:
8526
      /* Nothing to do. */
8527
863
      break;
8528
1.72M
  }
8529
1.72M
  return lcname;
8530
1.72M
}
8531
/* }}} */
8532
8533
static zend_op_array *zend_compile_func_decl_ex(
8534
  znode *result, zend_ast *ast, enum func_decl_level level,
8535
  zend_string *property_info_name,
8536
  zend_property_hook_kind hook_kind
8537
1.75M
) {
8538
1.75M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8539
1.75M
  zend_ast *params_ast = decl->child[0];
8540
1.75M
  zend_ast *uses_ast = decl->child[1];
8541
1.75M
  zend_ast *stmt_ast = decl->child[2];
8542
1.75M
  zend_ast *return_type_ast = decl->child[3];
8543
1.75M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8544
1.75M
  zend_string *lcname = NULL;
8545
1.75M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8546
8547
1.75M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8548
1.75M
  zend_op_array *orig_op_array = CG(active_op_array);
8549
1.75M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8550
1.75M
  zend_oparray_context orig_oparray_context;
8551
1.75M
  closure_info info;
8552
8553
1.75M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8554
8555
1.75M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8556
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8557
0
  }
8558
8559
1.75M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8560
1.75M
  op_array->fn_flags |= decl->flags;
8561
1.75M
  op_array->line_start = decl->start_lineno;
8562
1.75M
  op_array->line_end = decl->end_lineno;
8563
1.75M
  if (decl->doc_comment) {
8564
562
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8565
562
  }
8566
8567
1.75M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8568
1.68M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8569
1.68M
  }
8570
8571
1.75M
  if (is_hook) {
8572
2.80k
    zend_class_entry *ce = CG(active_class_entry);
8573
2.80k
    op_array->scope = ce;
8574
2.80k
    op_array->function_name = zend_string_copy(decl->name);
8575
1.75M
  } else if (is_method) {
8576
28.1k
    bool has_body = stmt_ast != NULL;
8577
28.1k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8578
1.72M
  } else {
8579
1.72M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8580
1.72M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8581
35.4k
      find_implicit_binds(&info, params_ast, stmt_ast);
8582
35.4k
      compile_implicit_lexical_binds(&info, result, op_array);
8583
1.69M
    } else if (uses_ast) {
8584
932
      zend_compile_closure_binding(result, op_array, uses_ast);
8585
932
    }
8586
1.72M
  }
8587
8588
1.75M
  CG(active_op_array) = op_array;
8589
8590
1.75M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8591
1.75M
  CG(context).active_property_info_name = property_info_name;
8592
1.75M
  CG(context).active_property_hook_kind = hook_kind;
8593
8594
1.75M
  if (decl->child[4]) {
8595
1.58M
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8596
8597
1.58M
    if (is_method || is_hook) {
8598
939
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8599
939
    }
8600
8601
1.58M
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8602
8603
1.58M
    const zend_attribute *override_attribute = zend_get_attribute_str(
8604
1.58M
      op_array->attributes,
8605
1.58M
      "override",
8606
1.58M
      sizeof("override")-1
8607
1.58M
    );
8608
8609
1.58M
    if (override_attribute) {
8610
785
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8611
785
    }
8612
8613
1.58M
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8614
1.58M
      op_array->attributes,
8615
1.58M
      "deprecated",
8616
1.58M
      sizeof("deprecated")-1
8617
1.58M
    );
8618
8619
1.58M
    if (deprecated_attribute) {
8620
28
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8621
28
    }
8622
8623
    // ZEND_ACC_NODISCARD is added via an attribute validator
8624
1.58M
  }
8625
8626
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8627
   * defined inside a class method. This is necessary for correct handling of magic constants.
8628
   * For example __CLASS__ should always be "" inside a free standing function. */
8629
1.75M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8630
39.2k
    CG(active_class_entry) = NULL;
8631
39.2k
  }
8632
8633
1.75M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8634
768
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8635
768
  }
8636
8637
1.75M
  {
8638
    /* Push a separator to the loop variable stack */
8639
1.75M
    zend_loop_var dummy_var;
8640
1.75M
    dummy_var.opcode = ZEND_RETURN;
8641
8642
1.75M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8643
1.75M
  }
8644
8645
1.75M
  zend_compile_params(params_ast, return_type_ast,
8646
1.75M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8647
1.75M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8648
33.2k
    zend_mark_function_as_generator();
8649
33.2k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8650
33.2k
  }
8651
1.75M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8652
35.3k
    zend_compile_implicit_closure_uses(&info);
8653
35.3k
    zend_hash_destroy(&info.uses);
8654
1.72M
  } else if (uses_ast) {
8655
922
    zend_compile_closure_uses(uses_ast);
8656
922
  }
8657
8658
1.75M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8659
14.8k
    bool needs_return = true;
8660
14.8k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8661
1.59k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8662
1.59k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8663
1.59k
    }
8664
14.8k
    if (needs_return) {
8665
14.1k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8666
14.1k
      decl->child[2] = stmt_ast;
8667
14.1k
    }
8668
14.8k
  }
8669
8670
1.75M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8671
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8672
     * if the method is not a hook; if it is a hook, then the validator
8673
     * will have returned an error message, even if the error message was
8674
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8675
     * flag should not have been added. */
8676
1.18k
    ZEND_ASSERT(!is_hook);
8677
8678
1.18k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8679
1.16k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8680
1.16k
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8681
1
        zend_error_noreturn(E_COMPILE_ERROR,
8682
1
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8683
1
          CG(active_class_entry) != NULL ? "method" : "function");
8684
1
      }
8685
8686
1.16k
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8687
1
        zend_error_noreturn(E_COMPILE_ERROR,
8688
1
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8689
1
          CG(active_class_entry) != NULL ? "method" : "function");
8690
1
      }
8691
1.16k
    }
8692
1.18k
  }
8693
8694
1.75M
  zend_compile_stmt(stmt_ast);
8695
8696
1.75M
  if (is_method) {
8697
28.0k
    CG(zend_lineno) = decl->start_lineno;
8698
28.0k
    zend_check_magic_method_implementation(
8699
28.0k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8700
1.72M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8701
    /* Only register the function after a successful compile */
8702
699
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8703
38
      CG(zend_lineno) = decl->start_lineno;
8704
38
      do_bind_function_error(lcname, op_array, true);
8705
38
    }
8706
699
  }
8707
8708
  /* put the implicit return on the really last line */
8709
1.75M
  CG(zend_lineno) = decl->end_lineno;
8710
8711
1.75M
  zend_do_extended_stmt(NULL);
8712
1.75M
  zend_emit_final_return(false);
8713
8714
1.75M
  pass_two(CG(active_op_array));
8715
1.75M
  zend_oparray_context_end(&orig_oparray_context);
8716
8717
  /* Pop the loop variable stack separator */
8718
1.75M
  zend_stack_del_top(&CG(loop_var_stack));
8719
8720
1.75M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8721
660
    zend_observer_function_declared_notify(op_array, lcname);
8722
660
  }
8723
8724
1.75M
  if (lcname != NULL) {
8725
1.75M
    zend_string_release_ex(lcname, 0);
8726
1.75M
  }
8727
8728
1.75M
  CG(active_op_array) = orig_op_array;
8729
1.75M
  CG(active_class_entry) = orig_class_entry;
8730
8731
1.75M
  return op_array;
8732
1.75M
}
8733
8734
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8735
1.75M
{
8736
1.75M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8737
1.75M
}
8738
8739
3.73k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8740
3.73k
  if (zend_string_equals_literal_ci(name, "get")) {
8741
2.56k
    return ZEND_PROPERTY_HOOK_GET;
8742
2.56k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8743
1.01k
    return ZEND_PROPERTY_HOOK_SET;
8744
1.01k
  } else {
8745
159
    return (zend_property_hook_kind)-1;
8746
159
  }
8747
3.73k
}
8748
8749
static void zend_compile_property_hooks(
8750
    zend_property_info *prop_info, zend_string *prop_name,
8751
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8752
2.71k
{
8753
2.71k
  zend_class_entry *ce = CG(active_class_entry);
8754
8755
2.71k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8756
1
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8757
1
  }
8758
8759
2.71k
  if (hooks->children == 0) {
8760
14
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8761
14
  }
8762
8763
5.47k
  for (uint32_t i = 0; i < hooks->children; i++) {
8764
3.03k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8765
3.03k
    zend_string *name = hook->name;
8766
3.03k
    zend_ast *stmt_ast = hook->child[2];
8767
3.03k
    zend_ast **return_type_ast_ptr = NULL;
8768
3.03k
    zend_ast **value_type_ast_ptr = NULL;
8769
3.03k
    CG(zend_lineno) = hook->start_lineno;
8770
8771
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8772
     * hooks from protected properties to public ones. */
8773
3.03k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8774
3.03k
    hook->flags |= hook_visibility;
8775
8776
3.03k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8777
30
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8778
30
    }
8779
3.00k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8780
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8781
1
    }
8782
3.00k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8783
2.86k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8784
491
      hook->flags |= ZEND_ACC_ABSTRACT;
8785
8786
491
      if (stmt_ast) {
8787
10
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8788
10
      }
8789
481
      if (hook->flags & ZEND_ACC_PRIVATE) {
8790
1
        zend_error_noreturn(E_COMPILE_ERROR,
8791
1
          "Property hook cannot be both abstract and private");
8792
1
      }
8793
480
      if (hook->flags & ZEND_ACC_FINAL) {
8794
3
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8795
3
      }
8796
2.51k
    } else if (!stmt_ast) {
8797
20
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8798
20
    }
8799
8800
2.97k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8801
2.97k
    if (hook_kind == (zend_property_hook_kind)-1) {
8802
159
      zend_error_noreturn(E_COMPILE_ERROR,
8803
159
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8804
159
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8805
159
    }
8806
8807
2.81k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8808
1.38k
      stmt_ast = stmt_ast->child[0];
8809
1.38k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8810
1.19k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8811
1.19k
      } else {
8812
196
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8813
196
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8814
196
          zend_ast_create(ZEND_AST_PROP,
8815
196
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8816
196
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8817
196
          stmt_ast);
8818
196
      }
8819
1.38k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8820
1.38k
      hook->child[2] = stmt_ast;
8821
1.38k
    }
8822
8823
2.81k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8824
1.82k
      if (hook->child[0]) {
8825
1
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8826
1
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8827
1
      }
8828
8829
1.82k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8830
8831
1.82k
      return_type_ast_ptr = &hook->child[3];
8832
1.82k
      *return_type_ast_ptr = prop_type_ast;
8833
1.82k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8834
990
      if (hook->child[0]) {
8835
41
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8836
41
        if (param_list->children != 1) {
8837
1
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8838
1
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8839
1
        }
8840
40
        const zend_ast *value_param_ast = param_list->child[0];
8841
40
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8842
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8843
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8844
1
        }
8845
39
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8846
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8847
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8848
1
        }
8849
38
        if (value_param_ast->child[2]) {
8850
1
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8851
1
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8852
1
        }
8853
37
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8854
1
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8855
1
        }
8856
949
      } else {
8857
949
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8858
949
        zend_ast *param = zend_ast_create(
8859
949
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8860
949
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8861
949
          /* hooks */ NULL);
8862
949
        value_type_ast_ptr = &param->child[0];
8863
949
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8864
949
      }
8865
985
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8866
985
      return_type->attr = ZEND_NAME_NOT_FQ;
8867
985
      hook->child[3] = return_type;
8868
985
    } else {
8869
0
      ZEND_UNREACHABLE();
8870
0
    }
8871
8872
2.80k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8873
8874
2.80k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8875
2.80k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8876
8877
2.80k
    func->common.prop_info = prop_info;
8878
8879
2.80k
    if (!prop_info->hooks) {
8880
2.44k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8881
2.44k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8882
2.44k
    }
8883
8884
2.80k
    if (prop_info->hooks[hook_kind]) {
8885
31
      zend_error_noreturn(E_COMPILE_ERROR,
8886
31
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8887
31
    }
8888
2.77k
    prop_info->hooks[hook_kind] = func;
8889
8890
2.77k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8891
944
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8892
909
        case INHERITANCE_SUCCESS:
8893
909
          break;
8894
34
        case INHERITANCE_UNRESOLVED:
8895
34
          ce->num_hooked_prop_variance_checks++;
8896
34
          break;
8897
1
        case INHERITANCE_ERROR:
8898
1
          zend_hooked_property_variance_error(prop_info);
8899
0
        case INHERITANCE_WARNING:
8900
0
          ZEND_UNREACHABLE();
8901
944
      }
8902
944
    }
8903
8904
2.77k
    zend_string_release(name);
8905
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8906
2.77k
    if (return_type_ast_ptr) {
8907
1.79k
      *return_type_ast_ptr = NULL;
8908
1.79k
    }
8909
2.77k
    if (value_type_ast_ptr) {
8910
908
      *value_type_ast_ptr = NULL;
8911
908
    }
8912
2.77k
  }
8913
8914
2.43k
  ce->num_hooked_props++;
8915
8916
  /* See zend_link_hooked_object_iter(). */
8917
2.43k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8918
2.43k
  if (!ce->get_iterator) {
8919
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8920
1.75k
    ce->get_iterator = zend_hooked_object_get_iterator;
8921
1.75k
  }
8922
2.43k
#endif
8923
8924
2.43k
  if (!prop_info->ce->parent_name) {
8925
1.28k
    zend_verify_hooked_property(ce, prop_info, prop_name);
8926
1.28k
  }
8927
2.43k
}
8928
8929
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8930
33.9k
{
8931
33.9k
  const zend_ast_list *list = zend_ast_get_list(ast);
8932
33.9k
  zend_class_entry *ce = CG(active_class_entry);
8933
33.9k
  uint32_t i, children = list->children;
8934
8935
33.9k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8936
1
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8937
1
  }
8938
8939
33.9k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8940
1
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8941
1
  }
8942
8943
33.9k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8944
180
    if (flags & ZEND_ACC_FINAL) {
8945
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8946
1
    }
8947
179
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8948
1
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8949
1
    }
8950
178
    if (flags & ZEND_ACC_ABSTRACT) {
8951
1
      zend_error_noreturn(E_COMPILE_ERROR,
8952
1
        "Property in interface cannot be explicitly abstract. "
8953
1
        "All interface members are implicitly abstract");
8954
1
    }
8955
177
    flags |= ZEND_ACC_ABSTRACT;
8956
177
  }
8957
8958
68.2k
  for (i = 0; i < children; ++i) {
8959
34.3k
    zend_property_info *info;
8960
34.3k
    zend_ast *prop_ast = list->child[i];
8961
34.3k
    zend_ast *name_ast = prop_ast->child[0];
8962
34.3k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8963
34.3k
    zend_ast *doc_comment_ast = prop_ast->child[2];
8964
34.3k
    zend_ast *hooks_ast = prop_ast->child[3];
8965
34.3k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8966
34.3k
    zend_string *doc_comment = NULL;
8967
34.3k
    zval value_zv;
8968
34.3k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8969
34.3k
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8970
8971
34.3k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8972
34.3k
    CG(context).active_property_info_name = name;
8973
8974
34.3k
    if (!hooks_ast) {
8975
31.7k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8976
2
        zend_error_noreturn(E_COMPILE_ERROR,
8977
2
          "Interfaces may only include hooked properties");
8978
2
      }
8979
31.7k
      if (flags & ZEND_ACC_ABSTRACT) {
8980
1
        zend_error_noreturn(E_COMPILE_ERROR,
8981
1
          "Only hooked properties may be declared abstract");
8982
1
      }
8983
31.7k
    }
8984
34.3k
    if ((flags & ZEND_ACC_ABSTRACT)) {
8985
621
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8986
621
    }
8987
8988
34.3k
    if (type_ast) {
8989
25.4k
      type = zend_compile_typename(type_ast);
8990
8991
25.4k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8992
1
        zend_string *str = zend_type_to_string(type);
8993
1
        zend_error_noreturn(E_COMPILE_ERROR,
8994
1
          "Property %s::$%s cannot have type %s",
8995
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
8996
1
      }
8997
25.4k
    }
8998
8999
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9000
34.3k
    if (doc_comment_ast) {
9001
243
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9002
243
    }
9003
9004
34.3k
    if (zend_hash_exists(&ce->properties_info, name)) {
9005
33
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9006
33
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9007
33
    }
9008
9009
34.3k
    if (*value_ast_ptr) {
9010
7.60k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9011
9012
7.60k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9013
1.26k
          && !zend_is_valid_default_value(type, &value_zv)) {
9014
38
        zend_string *str = zend_type_to_string(type);
9015
38
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9016
4
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9017
4
          zend_string *nullable_str = zend_type_to_string(type);
9018
9019
4
          zend_error_noreturn(E_COMPILE_ERROR,
9020
4
            "Default value for property of type %s may not be null. "
9021
4
            "Use the nullable type %s to allow null default value",
9022
4
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9023
34
        } else {
9024
34
          zend_error_noreturn(E_COMPILE_ERROR,
9025
34
            "Cannot use %s as default value for property %s::$%s of type %s",
9026
34
            zend_zval_value_name(&value_zv),
9027
34
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9028
34
        }
9029
38
      }
9030
26.7k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9031
1.66k
      ZVAL_NULL(&value_zv);
9032
25.0k
    } else {
9033
25.0k
      ZVAL_UNDEF(&value_zv);
9034
25.0k
    }
9035
9036
34.3k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9037
50
      flags |= ZEND_ACC_READONLY;
9038
50
    }
9039
9040
34.3k
    if (flags & ZEND_ACC_READONLY) {
9041
150
      if (!ZEND_TYPE_IS_SET(type)) {
9042
8
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9043
8
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9044
8
      }
9045
142
      if (!Z_ISUNDEF(value_zv)) {
9046
1
        zend_error_noreturn(E_COMPILE_ERROR,
9047
1
          "Readonly property %s::$%s cannot have default value",
9048
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9049
1
      }
9050
141
      if (flags & ZEND_ACC_STATIC) {
9051
1
        zend_error_noreturn(E_COMPILE_ERROR,
9052
1
          "Static property %s::$%s cannot be readonly",
9053
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9054
1
      }
9055
141
    }
9056
9057
34.2k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9058
9059
34.2k
    if (hooks_ast) {
9060
2.59k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9061
2.59k
    }
9062
9063
34.2k
    if (attr_ast) {
9064
991
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9065
9066
991
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9067
991
      if (override_attribute) {
9068
12
        info->flags |= ZEND_ACC_OVERRIDE;
9069
12
      }
9070
991
    }
9071
9072
34.2k
    CG(context).active_property_info_name = old_active_property_info_name;
9073
34.2k
  }
9074
33.9k
}
9075
/* }}} */
9076
9077
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9078
33.9k
{
9079
33.9k
  zend_ast *type_ast = ast->child[0];
9080
33.9k
  zend_ast *prop_ast = ast->child[1];
9081
33.9k
  zend_ast *attr_ast = ast->child[2];
9082
9083
33.9k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9084
33.9k
}
9085
/* }}} */
9086
9087
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9088
142k
{
9089
142k
  if (attr & ZEND_ACC_STATIC) {
9090
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9091
142k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9092
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9093
1
  }
9094
142k
}
9095
/* }}} */
9096
9097
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9098
10.7k
{
9099
10.7k
  const zend_ast_list *list = zend_ast_get_list(ast);
9100
10.7k
  zend_class_entry *ce = CG(active_class_entry);
9101
10.7k
  uint32_t i, children = list->children;
9102
9103
21.4k
  for (i = 0; i < children; ++i) {
9104
10.7k
    zend_class_constant *c;
9105
10.7k
    zend_ast *const_ast = list->child[i];
9106
10.7k
    zend_ast *name_ast = const_ast->child[0];
9107
10.7k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9108
10.7k
    zend_ast *doc_comment_ast = const_ast->child[2];
9109
10.7k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9110
10.7k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9111
10.7k
    zval value_zv;
9112
10.7k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9113
9114
10.7k
    if (type_ast) {
9115
4.72k
      type = zend_compile_typename(type_ast);
9116
9117
4.72k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9118
9119
4.72k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9120
1
        zend_string *type_str = zend_type_to_string(type);
9121
9122
1
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9123
1
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9124
1
      }
9125
4.72k
    }
9126
9127
10.7k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9128
1
      zend_error_noreturn(
9129
1
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9130
1
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9131
1
      );
9132
1
    }
9133
9134
10.7k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9135
9136
10.7k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9137
16
      zend_string *type_str = zend_type_to_string(type);
9138
9139
16
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9140
16
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9141
16
    }
9142
9143
10.7k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9144
9145
10.7k
    if (attr_ast) {
9146
37
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9147
9148
37
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9149
9150
37
      if (deprecated) {
9151
15
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9152
        /* For deprecated constants, we need to flag the zval for recursion
9153
         * detection. Make sure the zval is separated out of shm. */
9154
15
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9155
15
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9156
15
      }
9157
37
    }
9158
10.7k
  }
9159
10.7k
}
9160
9161
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9162
10.7k
{
9163
10.7k
  zend_ast *const_ast = ast->child[0];
9164
10.7k
  zend_ast *attr_ast = ast->child[1];
9165
10.7k
  zend_ast *type_ast = ast->child[2];
9166
9167
10.7k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9168
10.7k
}
9169
/* }}} */
9170
9171
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9172
143k
{
9173
143k
  zend_ast *class_ast = ast->child[0];
9174
143k
  zend_ast *method_ast = ast->child[1];
9175
9176
143k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9177
9178
143k
  if (class_ast) {
9179
2.75k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9180
141k
  } else {
9181
141k
    method_ref->class_name = NULL;
9182
141k
  }
9183
143k
}
9184
/* }}} */
9185
9186
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9187
1.46k
{
9188
1.46k
  const zend_ast *method_ref_ast = ast->child[0];
9189
1.46k
  zend_ast *insteadof_ast = ast->child[1];
9190
1.46k
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9191
1.46k
  uint32_t i;
9192
9193
1.46k
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9194
1.46k
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9195
1.46k
  precedence->num_excludes = insteadof_list->children;
9196
9197
3.47k
  for (i = 0; i < insteadof_list->children; ++i) {
9198
2.01k
    zend_ast *name_ast = insteadof_list->child[i];
9199
2.01k
    precedence->exclude_class_names[i] =
9200
2.01k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9201
2.01k
  }
9202
9203
1.46k
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9204
1.46k
}
9205
/* }}} */
9206
9207
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9208
142k
{
9209
142k
  const zend_ast *method_ref_ast = ast->child[0];
9210
142k
  zend_ast *alias_ast = ast->child[1];
9211
142k
  uint32_t modifiers = ast->attr;
9212
9213
142k
  zend_trait_alias *alias;
9214
9215
142k
  zend_check_trait_alias_modifiers(modifiers);
9216
9217
142k
  alias = emalloc(sizeof(zend_trait_alias));
9218
142k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9219
142k
  alias->modifiers = modifiers;
9220
9221
142k
  if (alias_ast) {
9222
142k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9223
142k
  } else {
9224
371
    alias->alias = NULL;
9225
371
  }
9226
9227
142k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9228
142k
}
9229
/* }}} */
9230
9231
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9232
48.3k
{
9233
48.3k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9234
48.3k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9235
48.3k
  zend_class_entry *ce = CG(active_class_entry);
9236
48.3k
  uint32_t i;
9237
9238
48.3k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9239
9240
99.5k
  for (i = 0; i < traits->children; ++i) {
9241
51.1k
    zend_ast *trait_ast = traits->child[i];
9242
9243
51.1k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9244
1
      zend_string *name = zend_ast_get_str(trait_ast);
9245
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9246
1
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9247
1
    }
9248
9249
51.1k
    ce->trait_names[ce->num_traits].name =
9250
51.1k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9251
51.1k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9252
51.1k
    ce->num_traits++;
9253
51.1k
  }
9254
9255
48.3k
  if (!adaptations) {
9256
1.35k
    return;
9257
1.35k
  }
9258
9259
191k
  for (i = 0; i < adaptations->children; ++i) {
9260
143k
    const zend_ast *adaptation_ast = adaptations->child[i];
9261
143k
    switch (adaptation_ast->kind) {
9262
1.46k
      case ZEND_AST_TRAIT_PRECEDENCE:
9263
1.46k
        zend_compile_trait_precedence(adaptation_ast);
9264
1.46k
        break;
9265
142k
      case ZEND_AST_TRAIT_ALIAS:
9266
142k
        zend_compile_trait_alias(adaptation_ast);
9267
142k
        break;
9268
143k
      EMPTY_SWITCH_DEFAULT_CASE()
9269
143k
    }
9270
143k
  }
9271
47.0k
}
9272
/* }}} */
9273
9274
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9275
1.67k
{
9276
1.67k
  const zend_ast_list *list = zend_ast_get_list(ast);
9277
1.67k
  zend_class_entry *ce = CG(active_class_entry);
9278
1.67k
  zend_class_name *interface_names;
9279
1.67k
  uint32_t i;
9280
9281
1.67k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9282
9283
5.07k
  for (i = 0; i < list->children; ++i) {
9284
3.40k
    zend_ast *class_ast = list->child[i];
9285
3.40k
    interface_names[i].name =
9286
3.40k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9287
3.40k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9288
3.40k
  }
9289
9290
1.67k
  ce->num_interfaces = list->children;
9291
1.67k
  ce->interface_names = interface_names;
9292
1.67k
}
9293
/* }}} */
9294
9295
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9296
1.51k
{
9297
1.51k
  zend_string *filename = CG(active_op_array)->filename;
9298
1.51k
  uint32_t start_lineno = decl->start_lineno;
9299
9300
  /* Use parent or first interface as prefix. */
9301
1.51k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9302
1.51k
  if (decl->child[0]) {
9303
141
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9304
1.37k
  } else if (decl->child[1]) {
9305
746
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9306
746
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9307
746
  }
9308
9309
1.51k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9310
1.51k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9311
1.51k
  zend_string_release(prefix);
9312
1.51k
  return zend_new_interned_string(result);
9313
1.51k
}
9314
9315
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9316
515
{
9317
515
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9318
515
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9319
515
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9320
515
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9321
67
    zend_string *type_string = zend_type_to_string(type);
9322
67
    zend_error_noreturn(E_COMPILE_ERROR,
9323
67
      "Enum backing type must be int or string, %s given",
9324
67
      ZSTR_VAL(type_string));
9325
67
  }
9326
448
  if (type_mask == MAY_BE_LONG) {
9327
299
    ce->enum_backing_type = IS_LONG;
9328
299
  } else {
9329
149
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9330
149
    ce->enum_backing_type = IS_STRING;
9331
144
  }
9332
448
  zend_type_release(type, 0);
9333
443
}
9334
9335
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9336
144k
{
9337
144k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9338
144k
  zend_ast *extends_ast = decl->child[0];
9339
144k
  zend_ast *implements_ast = decl->child[1];
9340
144k
  zend_ast *stmt_ast = decl->child[2];
9341
144k
  zend_ast *enum_backing_type_ast = decl->child[4];
9342
144k
  zend_string *name, *lcname;
9343
144k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9344
144k
  zend_op *opline;
9345
9346
144k
  zend_class_entry *original_ce = CG(active_class_entry);
9347
9348
144k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9349
143k
    zend_string *unqualified_name = decl->name;
9350
9351
143k
    if (CG(active_class_entry)) {
9352
1
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9353
1
    }
9354
9355
143k
    const char *type = "a class name";
9356
143k
    if (decl->flags & ZEND_ACC_ENUM) {
9357
5.01k
      type = "an enum name";
9358
138k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9359
6.59k
      type = "an interface name";
9360
131k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9361
1.76k
      type = "a trait name";
9362
1.76k
    }
9363
143k
    zend_assert_valid_class_name(unqualified_name, type);
9364
143k
    name = zend_prefix_with_ns(unqualified_name);
9365
143k
    name = zend_new_interned_string(name);
9366
143k
    lcname = zend_string_tolower(name);
9367
9368
143k
    if (FC(imports)) {
9369
655
      zend_string *import_name =
9370
655
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9371
655
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9372
2
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9373
2
            "(previously declared as local import)", ZSTR_VAL(name));
9374
2
      }
9375
655
    }
9376
9377
143k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9378
143k
  } else {
9379
    /* Find an anon class name that is not in use yet. */
9380
1.51k
    name = NULL;
9381
1.51k
    lcname = NULL;
9382
1.51k
    do {
9383
1.51k
      zend_tmp_string_release(name);
9384
1.51k
      zend_tmp_string_release(lcname);
9385
1.51k
      name = zend_generate_anon_class_name(decl);
9386
1.51k
      lcname = zend_string_tolower(name);
9387
1.51k
    } while (zend_hash_exists(CG(class_table), lcname));
9388
1.51k
  }
9389
144k
  lcname = zend_new_interned_string(lcname);
9390
9391
144k
  ce->type = ZEND_USER_CLASS;
9392
144k
  ce->name = name;
9393
144k
  zend_initialize_class_data(ce, true);
9394
144k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9395
143k
    zend_alloc_ce_cache(ce->name);
9396
143k
  }
9397
9398
144k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9399
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9400
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9401
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9402
0
  }
9403
9404
144k
  ce->ce_flags |= decl->flags;
9405
144k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9406
144k
  ce->info.user.line_start = decl->start_lineno;
9407
144k
  ce->info.user.line_end = decl->end_lineno;
9408
9409
144k
  if (decl->doc_comment) {
9410
66
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9411
66
  }
9412
9413
144k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9414
    /* Serialization is not supported for anonymous classes */
9415
1.51k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9416
1.51k
  }
9417
9418
144k
  if (extends_ast) {
9419
34.1k
    ce->parent_name =
9420
34.1k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9421
34.1k
  }
9422
9423
144k
  CG(active_class_entry) = ce;
9424
9425
144k
  if (decl->child[3]) {
9426
1.43k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9427
1.43k
  }
9428
9429
144k
  if (implements_ast) {
9430
1.67k
    zend_compile_implements(implements_ast);
9431
1.67k
  }
9432
9433
144k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9434
5.00k
    if (enum_backing_type_ast != NULL) {
9435
515
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9436
515
    }
9437
5.00k
    zend_enum_add_interfaces(ce);
9438
5.00k
    zend_enum_register_props(ce);
9439
5.00k
  }
9440
9441
144k
  zend_compile_stmt(stmt_ast);
9442
9443
  /* Reset lineno for final opcodes and errors */
9444
144k
  CG(zend_lineno) = ast->lineno;
9445
9446
144k
  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) {
9447
159
    zend_verify_abstract_class(ce);
9448
159
  }
9449
9450
144k
  CG(active_class_entry) = original_ce;
9451
9452
144k
  if (toplevel) {
9453
19.8k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9454
19.8k
  }
9455
9456
  /* We currently don't early-bind classes that implement interfaces or use traits */
9457
144k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9458
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9459
   /* See zend_link_hooked_object_iter(). */
9460
   && !ce->num_hooked_props
9461
#endif
9462
88.9k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9463
88.9k
    if (toplevel) {
9464
15.2k
      if (extends_ast) {
9465
7.82k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9466
7.82k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9467
9468
7.82k
        if (parent_ce
9469
7.11k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9470
7.11k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9471
2.55k
            zend_string_release(lcname);
9472
2.55k
            return;
9473
2.55k
          }
9474
7.11k
        }
9475
7.82k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9476
3.26k
        zend_string_release(lcname);
9477
3.26k
        zend_build_properties_info_table(ce);
9478
3.26k
        zend_inheritance_check_override(ce);
9479
3.26k
        ce->ce_flags |= ZEND_ACC_LINKED;
9480
3.26k
        zend_observer_class_linked_notify(ce, lcname);
9481
3.26k
        return;
9482
4.12k
      } else {
9483
4.12k
        goto link_unbound;
9484
4.12k
      }
9485
73.6k
    } else if (!extends_ast) {
9486
51.6k
link_unbound:
9487
      /* Link unbound simple class */
9488
51.6k
      zend_build_properties_info_table(ce);
9489
51.6k
      zend_inheritance_check_override(ce);
9490
51.6k
      ce->ce_flags |= ZEND_ACC_LINKED;
9491
51.6k
    }
9492
88.9k
  }
9493
9494
138k
  opline = get_next_op();
9495
9496
138k
  if (ce->parent_name) {
9497
    /* Lowercased parent name */
9498
31.0k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9499
31.0k
    opline->op2_type = IS_CONST;
9500
31.0k
    LITERAL_STR(opline->op2, lc_parent_name);
9501
31.0k
  }
9502
9503
138k
  opline->op1_type = IS_CONST;
9504
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9505
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9506
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9507
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9508
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9509
   * zend_add_literal_string() which gives us the new value. */
9510
138k
  opline->op1.constant = zend_add_literal_string(&lcname);
9511
9512
138k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9513
1.50k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9514
1.50k
    opline->extended_value = zend_alloc_cache_slot();
9515
1.50k
    zend_make_var_result(result, opline);
9516
1.50k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9517
      /* We checked above that the class name is not used. This really shouldn't happen. */
9518
0
      zend_error_noreturn(E_ERROR,
9519
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9520
0
    }
9521
137k
  } else {
9522
    /* Generate RTD keys until we find one that isn't in use yet. */
9523
137k
    zend_string *key = NULL;
9524
137k
    do {
9525
137k
      zend_tmp_string_release(key);
9526
137k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9527
137k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9528
9529
    /* RTD key is placed after lcname literal in op1 */
9530
137k
    zend_add_literal_string(&key);
9531
9532
137k
    opline->opcode = ZEND_DECLARE_CLASS;
9533
137k
    if (toplevel
9534
13.5k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9535
        /* We currently don't early-bind classes that implement interfaces or use traits */
9536
0
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9537
137k
    ) {
9538
0
      if (!extends_ast) {
9539
        /* Use empty string for classes without parents to avoid new handler, and special
9540
         * handling of zend_early_binding. */
9541
0
        opline->op2_type = IS_CONST;
9542
0
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9543
0
      }
9544
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9545
0
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9546
0
      opline->extended_value = zend_alloc_cache_slot();
9547
0
      opline->result_type = IS_UNUSED;
9548
0
      opline->result.opline_num = -1;
9549
0
    }
9550
137k
  }
9551
138k
}
9552
/* }}} */
9553
9554
static void zend_compile_enum_case(zend_ast *ast)
9555
1.23k
{
9556
1.23k
  zend_class_entry *enum_class = CG(active_class_entry);
9557
1.23k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9558
1
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9559
1
  }
9560
9561
1.23k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9562
1.23k
  zend_string *enum_class_name = enum_class->name;
9563
9564
1.23k
  zval class_name_zval;
9565
1.23k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9566
1.23k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9567
9568
1.23k
  zval case_name_zval;
9569
1.23k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9570
1.23k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9571
9572
1.23k
  zend_ast *case_value_ast = ast->child[1];
9573
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9574
1.23k
  ast->child[1] = NULL;
9575
1.23k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9576
1
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9577
1
      ZSTR_VAL(enum_case_name),
9578
1
      ZSTR_VAL(enum_class_name));
9579
1.23k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9580
3
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9581
3
      ZSTR_VAL(enum_case_name),
9582
3
      ZSTR_VAL(enum_class_name));
9583
3
  }
9584
9585
1.22k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9586
9587
1.22k
  zval value_zv;
9588
1.22k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9589
9590
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9591
1.22k
  zend_ast *doc_comment_ast = ast->child[2];
9592
1.22k
  zend_string *doc_comment = NULL;
9593
1.22k
  if (doc_comment_ast) {
9594
280
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9595
280
  }
9596
9597
1.22k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9598
1.22k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9599
1.22k
  zend_ast_destroy(const_enum_init_ast);
9600
9601
1.22k
  zend_ast *attr_ast = ast->child[3];
9602
1.22k
  if (attr_ast) {
9603
115
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9604
9605
115
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9606
9607
115
    if (deprecated) {
9608
29
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9609
29
    }
9610
115
  }
9611
1.22k
}
9612
9613
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9614
1.94k
{
9615
1.94k
  switch (type) {
9616
955
    case ZEND_SYMBOL_CLASS:
9617
955
      if (!FC(imports)) {
9618
729
        FC(imports) = emalloc(sizeof(HashTable));
9619
729
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9620
729
      }
9621
955
      return FC(imports);
9622
518
    case ZEND_SYMBOL_FUNCTION:
9623
518
      if (!FC(imports_function)) {
9624
474
        FC(imports_function) = emalloc(sizeof(HashTable));
9625
474
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9626
474
      }
9627
518
      return FC(imports_function);
9628
469
    case ZEND_SYMBOL_CONST:
9629
469
      if (!FC(imports_const)) {
9630
396
        FC(imports_const) = emalloc(sizeof(HashTable));
9631
396
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9632
396
      }
9633
469
      return FC(imports_const);
9634
1.94k
    EMPTY_SWITCH_DEFAULT_CASE()
9635
1.94k
  }
9636
9637
0
  return NULL;
9638
1.94k
}
9639
/* }}} */
9640
9641
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9642
35
{
9643
35
  switch (type) {
9644
30
    case ZEND_SYMBOL_CLASS:
9645
30
      return "";
9646
2
    case ZEND_SYMBOL_FUNCTION:
9647
2
      return " function";
9648
3
    case ZEND_SYMBOL_CONST:
9649
3
      return " const";
9650
35
    EMPTY_SWITCH_DEFAULT_CASE()
9651
35
  }
9652
9653
0
  return " unknown";
9654
35
}
9655
/* }}} */
9656
9657
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) /* {{{ */
9658
22
{
9659
22
  if (zend_string_equals_ci(old_name, check_name)) {
9660
17
    return;
9661
17
  }
9662
9663
5
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9664
5
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9665
22
}
9666
/* }}} */
9667
9668
static void zend_compile_use(zend_ast *ast) /* {{{ */
9669
1.94k
{
9670
1.94k
  const zend_ast_list *list = zend_ast_get_list(ast);
9671
1.94k
  uint32_t i;
9672
1.94k
  zend_string *current_ns = FC(current_namespace);
9673
1.94k
  uint32_t type = ast->attr;
9674
1.94k
  HashTable *current_import = zend_get_import_ht(type);
9675
1.94k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9676
9677
4.17k
  for (i = 0; i < list->children; ++i) {
9678
2.27k
    const zend_ast *use_ast = list->child[i];
9679
2.27k
    zend_ast *old_name_ast = use_ast->child[0];
9680
2.27k
    zend_ast *new_name_ast = use_ast->child[1];
9681
2.27k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9682
2.27k
    zend_string *new_name, *lookup_name;
9683
9684
2.27k
    if (new_name_ast) {
9685
399
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9686
1.87k
    } else {
9687
1.87k
      const char *unqualified_name;
9688
1.87k
      size_t unqualified_name_len;
9689
1.87k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9690
        /* The form "use A\B" is equivalent to "use A\B as B" */
9691
608
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9692
1.26k
      } else {
9693
1.26k
        new_name = zend_string_copy(old_name);
9694
9695
1.26k
        if (!current_ns) {
9696
903
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9697
903
            "has no effect", ZSTR_VAL(new_name));
9698
903
        }
9699
1.26k
      }
9700
1.87k
    }
9701
9702
2.27k
    if (case_sensitive) {
9703
519
      lookup_name = zend_string_copy(new_name);
9704
1.75k
    } else {
9705
1.75k
      lookup_name = zend_string_tolower(new_name);
9706
1.75k
    }
9707
9708
2.27k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9709
9
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9710
9
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9711
9
    }
9712
9713
2.26k
    if (current_ns) {
9714
982
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9715
982
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9716
982
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9717
982
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9718
9719
982
      if (zend_have_seen_symbol(ns_name, type)) {
9720
14
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9721
14
      }
9722
9723
982
      zend_string_efree(ns_name);
9724
1.28k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9725
8
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9726
8
    }
9727
9728
2.26k
    zend_string_addref(old_name);
9729
2.26k
    old_name = zend_new_interned_string(old_name);
9730
2.26k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9731
30
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9732
30
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9733
30
    }
9734
9735
2.23k
    zend_string_release_ex(lookup_name, 0);
9736
2.23k
    zend_string_release_ex(new_name, 0);
9737
2.23k
  }
9738
1.94k
}
9739
/* }}} */
9740
9741
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9742
269
{
9743
269
  uint32_t i;
9744
269
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9745
269
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9746
9747
942
  for (i = 0; i < list->children; i++) {
9748
673
    zend_ast *inline_use, *use = list->child[i];
9749
673
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9750
673
    zend_string *name = Z_STR_P(name_zval);
9751
673
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9752
673
    zend_string_release_ex(name, 0);
9753
673
    ZVAL_STR(name_zval, compound_ns);
9754
673
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9755
673
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9756
673
    zend_compile_use(inline_use);
9757
673
  }
9758
269
}
9759
/* }}} */
9760
9761
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9762
2.51k
{
9763
2.51k
  zend_ast_list *list = zend_ast_get_list(ast);
9764
2.51k
  uint32_t i;
9765
2.51k
  zend_ast *attributes_ast = NULL;
9766
2.51k
  zend_op *last_op = NULL;
9767
6.17k
  for (i = 0; i < list->children; ++i) {
9768
3.66k
    zend_ast *const_ast = list->child[i];
9769
3.66k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9770
529
      ZEND_ASSERT(i == list->children - 1);
9771
529
      attributes_ast = const_ast;
9772
529
      continue;
9773
529
    }
9774
3.13k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9775
3.13k
    zend_ast *name_ast = const_ast->child[0];
9776
3.13k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9777
3.13k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9778
9779
3.13k
    zend_string *name;
9780
3.13k
    znode name_node, value_node;
9781
3.13k
    zval *value_zv = &value_node.u.constant;
9782
9783
3.13k
    value_node.op_type = IS_CONST;
9784
3.13k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9785
9786
3.13k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9787
1
      zend_error_noreturn(E_COMPILE_ERROR,
9788
1
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9789
1
    }
9790
9791
3.13k
    name = zend_prefix_with_ns(unqualified_name);
9792
3.13k
    name = zend_new_interned_string(name);
9793
9794
3.13k
    if (FC(imports_const)) {
9795
315
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9796
315
      if (import_name && !zend_string_equals(import_name, name)) {
9797
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9798
1
          "the name is already in use", ZSTR_VAL(name));
9799
1
      }
9800
315
    }
9801
9802
3.13k
    name_node.op_type = IS_CONST;
9803
3.13k
    ZVAL_STR(&name_node.u.constant, name);
9804
9805
3.13k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9806
9807
3.13k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9808
3.13k
  }
9809
2.51k
  if (attributes_ast == NULL) {
9810
1.96k
    return;
9811
1.96k
  }
9812
  /* Validate: attributes can only be applied to one constant at a time
9813
   * Since we store the AST for the attributes in the list of children,
9814
   * there should be exactly 2 children. */
9815
549
  if (list->children > 2) {
9816
1
    zend_error_noreturn(
9817
1
      E_COMPILE_ERROR,
9818
1
      "Cannot apply attributes to multiple constants at once"
9819
1
    );
9820
1
  }
9821
9822
548
  HashTable *attributes = NULL;
9823
548
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9824
9825
548
  ZEND_ASSERT(last_op != NULL);
9826
548
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9827
528
  znode attribs_node;
9828
528
  attribs_node.op_type = IS_CONST;
9829
528
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9830
528
  zend_emit_op_data(&attribs_node);
9831
528
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9832
528
}
9833
/* }}}*/
9834
9835
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
9836
4.65k
{
9837
4.65k
  zend_ast *name_ast = ast->child[0];
9838
4.65k
  zend_ast *stmt_ast = ast->child[1];
9839
4.65k
  zend_string *name;
9840
4.65k
  bool with_bracket = stmt_ast != NULL;
9841
9842
  /* handle mixed syntax declaration or nested namespaces */
9843
4.65k
  if (!FC(has_bracketed_namespaces)) {
9844
3.40k
    if (FC(current_namespace)) {
9845
      /* previous namespace declarations were unbracketed */
9846
1.21k
      if (with_bracket) {
9847
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9848
1
          "with unbracketed namespace declarations");
9849
1
      }
9850
1.21k
    }
9851
3.40k
  } else {
9852
    /* previous namespace declarations were bracketed */
9853
1.25k
    if (!with_bracket) {
9854
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9855
2
        "with unbracketed namespace declarations");
9856
1.25k
    } else if (FC(current_namespace) || FC(in_namespace)) {
9857
3
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9858
3
    }
9859
1.25k
  }
9860
9861
4.65k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9862
2.64k
    || (with_bracket && !FC(has_bracketed_namespaces));
9863
4.64k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
9864
15
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9865
15
      "the very first statement or after any declare call in the script");
9866
15
  }
9867
9868
4.63k
  if (FC(current_namespace)) {
9869
1.21k
    zend_string_release_ex(FC(current_namespace), 0);
9870
1.21k
  }
9871
9872
4.63k
  if (name_ast) {
9873
4.01k
    name = zend_ast_get_str(name_ast);
9874
9875
4.01k
    if (zend_string_equals_literal_ci(name, "namespace")) {
9876
1
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9877
1
    }
9878
9879
4.01k
    FC(current_namespace) = zend_string_copy(name);
9880
4.01k
  } else {
9881
622
    FC(current_namespace) = NULL;
9882
622
  }
9883
9884
4.63k
  zend_reset_import_tables();
9885
9886
4.63k
  FC(in_namespace) = 1;
9887
4.63k
  if (with_bracket) {
9888
1.42k
    FC(has_bracketed_namespaces) = 1;
9889
1.42k
  }
9890
9891
4.63k
  if (stmt_ast) {
9892
1.42k
    zend_compile_top_stmt(stmt_ast);
9893
1.42k
    zend_end_namespace();
9894
1.42k
  }
9895
4.63k
}
9896
/* }}} */
9897
9898
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
9899
14
{
9900
14
  zend_ast *offset_ast = ast->child[0];
9901
14
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9902
9903
14
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9904
9905
14
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9906
0
    zend_error_noreturn(E_COMPILE_ERROR,
9907
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9908
0
  }
9909
9910
14
  const zend_string *filename = zend_get_compiled_filename();
9911
14
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9912
14
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
9913
9914
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9915
   * case this file was already included. */
9916
14
  if (!zend_hash_find(EG(zend_constants), name)) {
9917
14
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9918
14
  }
9919
14
  zend_string_release_ex(name, 0);
9920
14
}
9921
/* }}} */
9922
9923
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
9924
18.1k
{
9925
18.1k
  const zend_op_array *op_array = CG(active_op_array);
9926
18.1k
  const zend_class_entry *ce = CG(active_class_entry);
9927
9928
18.1k
  switch (ast->attr) {
9929
641
    case T_LINE:
9930
641
      ZVAL_LONG(zv, ast->lineno);
9931
641
      break;
9932
385
    case T_FILE:
9933
385
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9934
385
      break;
9935
1.10k
    case T_DIR:
9936
1.10k
    {
9937
1.10k
      const zend_string *filename = CG(compiled_filename);
9938
1.10k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9939
#ifdef ZEND_WIN32
9940
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9941
#else
9942
1.10k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9943
1.10k
#endif
9944
9945
1.10k
      if (zend_string_equals_literal(dirname, ".")) {
9946
1.10k
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9947
1.10k
#ifdef HAVE_GETCWD
9948
1.10k
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9949
#elif defined(HAVE_GETWD)
9950
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9951
#endif
9952
1.10k
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9953
1.10k
      }
9954
9955
1.10k
      ZVAL_STR(zv, dirname);
9956
1.10k
      break;
9957
0
    }
9958
5.48k
    case T_FUNC_C:
9959
5.48k
      if (op_array && op_array->function_name) {
9960
5.27k
        ZVAL_STR_COPY(zv, op_array->function_name);
9961
5.27k
      } else {
9962
206
        ZVAL_EMPTY_STRING(zv);
9963
206
      }
9964
5.48k
      break;
9965
562
    case T_PROPERTY_C: {
9966
562
      zend_string *prop_info_name = CG(context).active_property_info_name;
9967
562
      if (prop_info_name) {
9968
368
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9969
368
      } else {
9970
194
        ZVAL_EMPTY_STRING(zv);
9971
194
      }
9972
562
      break;
9973
0
    }
9974
4.05k
    case T_METHOD_C:
9975
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9976
       * this as not being inside a function. */
9977
4.05k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9978
195
        op_array = NULL;
9979
195
      }
9980
4.05k
      if (op_array && op_array->function_name) {
9981
3.63k
        if (op_array->scope) {
9982
1.60k
          ZVAL_NEW_STR(zv,
9983
1.60k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9984
2.03k
        } else {
9985
2.03k
          ZVAL_STR_COPY(zv, op_array->function_name);
9986
2.03k
        }
9987
3.63k
      } else {
9988
425
        ZVAL_EMPTY_STRING(zv);
9989
425
      }
9990
4.05k
      break;
9991
2.66k
    case T_CLASS_C:
9992
2.66k
      if (ce) {
9993
1.95k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
9994
1.55k
          return 0;
9995
1.55k
        } else {
9996
400
          ZVAL_STR_COPY(zv, ce->name);
9997
400
        }
9998
1.95k
      } else {
9999
709
        ZVAL_EMPTY_STRING(zv);
10000
709
      }
10001
1.10k
      break;
10002
2.37k
    case T_TRAIT_C:
10003
2.37k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10004
516
        ZVAL_STR_COPY(zv, ce->name);
10005
1.85k
      } else {
10006
1.85k
        ZVAL_EMPTY_STRING(zv);
10007
1.85k
      }
10008
2.37k
      break;
10009
857
    case T_NS_C:
10010
857
      if (FC(current_namespace)) {
10011
645
        ZVAL_STR_COPY(zv, FC(current_namespace));
10012
645
      } else {
10013
212
        ZVAL_EMPTY_STRING(zv);
10014
212
      }
10015
857
      break;
10016
18.1k
    EMPTY_SWITCH_DEFAULT_CASE()
10017
18.1k
  }
10018
10019
16.5k
  return 1;
10020
18.1k
}
10021
/* }}} */
10022
10023
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10024
62.4k
{
10025
62.4k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10026
2.56k
    return false;
10027
2.56k
  }
10028
10029
59.8k
  if (Z_TYPE_P(op) == IS_DOUBLE
10030
8.42k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10031
2.03k
    return false;
10032
2.03k
  }
10033
10034
57.8k
  if (Z_TYPE_P(op) == IS_STRING) {
10035
13.1k
    double dval = 0;
10036
13.1k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10037
13.1k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10038
4.47k
      return false;
10039
4.47k
    }
10040
13.1k
  }
10041
10042
53.3k
  return true;
10043
57.8k
}
10044
10045
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10046
216k
{
10047
216k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10048
    /* Array to string warning. */
10049
20.0k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10050
20.0k
  }
10051
10052
196k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10053
118k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10054
55.3k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10055
    /* Only the numeric operations throw errors. */
10056
32.1k
    return 0;
10057
32.1k
  }
10058
10059
163k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10060
10.5k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10061
      /* Adding two arrays is allowed. */
10062
5.90k
      return 0;
10063
5.90k
    }
10064
10065
    /* Numeric operators throw when one of the operands is an array. */
10066
4.68k
    return 1;
10067
10.5k
  }
10068
10069
  /* While basic arithmetic operators always produce numeric string errors,
10070
   * bitwise operators don't produce errors if both operands are strings */
10071
153k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10072
23.0k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10073
7.51k
    return 0;
10074
7.51k
  }
10075
10076
145k
  if (Z_TYPE_P(op1) == IS_STRING
10077
22.9k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10078
9.98k
    return 1;
10079
9.98k
  }
10080
10081
135k
  if (Z_TYPE_P(op2) == IS_STRING
10082
40.8k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10083
31.4k
    return 1;
10084
31.4k
  }
10085
10086
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10087
104k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10088
98.4k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10089
19.2k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10090
971
      return 1;
10091
971
    }
10092
19.2k
  }
10093
10094
103k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10095
    /* Division by zero throws an error. */
10096
1.22k
    return 1;
10097
1.22k
  }
10098
10099
  /* Mod is an operation that will cast float/float-strings to integers which might
10100
     produce float to int incompatible errors, and also cannot be divided by 0 */
10101
102k
  if (opcode == ZEND_MOD) {
10102
9.71k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10103
7.91k
      return 1;
10104
7.91k
    }
10105
9.71k
  }
10106
10107
94.3k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10108
    /* 0 ** (<0) throws a division by zero error. */
10109
71
    return 1;
10110
71
  }
10111
94.2k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10112
    /* Shift by negative number throws an error. */
10113
512
    return 1;
10114
512
  }
10115
10116
93.7k
  return 0;
10117
94.2k
}
10118
/* }}} */
10119
10120
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10121
208k
{
10122
208k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10123
58.2k
    return 0;
10124
58.2k
  }
10125
10126
150k
  const binary_op_type fn = get_binary_op(opcode);
10127
150k
  fn(result, op1, op2);
10128
150k
  return 1;
10129
208k
}
10130
/* }}} */
10131
10132
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10133
96.5k
{
10134
96.5k
  if (opcode == ZEND_BW_NOT) {
10135
    /* BW_NOT on string does not convert the string into an integer. */
10136
9.05k
    if (Z_TYPE_P(op) == IS_STRING) {
10137
1.14k
      return 0;
10138
1.14k
    }
10139
7.90k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10140
9.05k
  }
10141
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10142
87.4k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10143
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10144
87.4k
    return Z_TYPE_P(op) == IS_DOUBLE;
10145
87.4k
  }
10146
10147
0
  return 0;
10148
87.4k
}
10149
10150
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10151
96.5k
{
10152
96.5k
  if (zend_unary_op_produces_error(opcode, op)) {
10153
3.38k
    return 0;
10154
3.38k
  }
10155
10156
93.1k
  const unary_op_type fn = get_unary_op(opcode);
10157
93.1k
  fn(result, op);
10158
93.1k
  return 1;
10159
96.5k
}
10160
/* }}} */
10161
10162
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10163
38.0k
{
10164
38.0k
  zval right;
10165
38.0k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10166
38.0k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10167
38.0k
}
10168
/* }}} */
10169
10170
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10171
10.9k
{
10172
10.9k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10173
10.9k
    ? is_smaller_function : is_smaller_or_equal_function;
10174
10.9k
  fn(result, op2, op1);
10175
10.9k
}
10176
/* }}} */
10177
10178
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10179
3.53M
{
10180
3.53M
  const zend_ast_list *list = zend_ast_get_list(ast);
10181
3.53M
  zend_ast *last_elem_ast = NULL;
10182
3.53M
  uint32_t i;
10183
3.53M
  bool is_constant = true;
10184
10185
3.53M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10186
1
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10187
1
  }
10188
10189
  /* First ensure that *all* child nodes are constant and by-val */
10190
7.19M
  for (i = 0; i < list->children; ++i) {
10191
3.66M
    zend_ast *elem_ast = list->child[i];
10192
10193
3.66M
    if (elem_ast == NULL) {
10194
      /* Report error at line of last non-empty element */
10195
118
      if (last_elem_ast) {
10196
50
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10197
50
      }
10198
118
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10199
118
    }
10200
10201
3.66M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10202
3.48M
      zend_eval_const_expr(&elem_ast->child[0]);
10203
3.48M
      zend_eval_const_expr(&elem_ast->child[1]);
10204
10205
3.48M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10206
152k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10207
3.48M
      ) {
10208
3.33M
        is_constant = false;
10209
3.33M
      }
10210
3.48M
    } else {
10211
177k
      zend_eval_const_expr(&elem_ast->child[0]);
10212
10213
177k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10214
175k
        is_constant = false;
10215
175k
      }
10216
177k
    }
10217
10218
3.66M
    last_elem_ast = elem_ast;
10219
3.66M
  }
10220
10221
3.53M
  if (!is_constant) {
10222
3.43M
    return 0;
10223
3.43M
  }
10224
10225
95.0k
  if (!list->children) {
10226
23.8k
    ZVAL_EMPTY_ARRAY(result);
10227
23.8k
    return 1;
10228
23.8k
  }
10229
10230
71.1k
  array_init_size(result, list->children);
10231
183k
  for (i = 0; i < list->children; ++i) {
10232
116k
    const zend_ast *elem_ast = list->child[i];
10233
116k
    zend_ast *value_ast = elem_ast->child[0];
10234
116k
    zend_ast *key_ast;
10235
10236
116k
    zval *value = zend_ast_get_zval(value_ast);
10237
116k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10238
1.35k
      if (Z_TYPE_P(value) == IS_ARRAY) {
10239
1.34k
        const HashTable *ht = Z_ARRVAL_P(value);
10240
1.34k
        zval *val;
10241
1.34k
        zend_string *key;
10242
10243
4.92k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10244
4.92k
          if (key) {
10245
135
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10246
613
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10247
0
            zval_ptr_dtor(result);
10248
0
            return 0;
10249
0
          }
10250
748
          Z_TRY_ADDREF_P(val);
10251
748
        } ZEND_HASH_FOREACH_END();
10252
10253
1.34k
        continue;
10254
1.34k
      } else {
10255
5
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10256
5
      }
10257
1.35k
    }
10258
10259
114k
    Z_TRY_ADDREF_P(value);
10260
10261
114k
    key_ast = elem_ast->child[1];
10262
114k
    if (key_ast) {
10263
20.9k
      const zval *key = zend_ast_get_zval(key_ast);
10264
20.9k
      switch (Z_TYPE_P(key)) {
10265
7.52k
        case IS_LONG:
10266
7.52k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10267
7.52k
          break;
10268
8.59k
        case IS_STRING:
10269
8.59k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10270
8.59k
          break;
10271
4.15k
        case IS_DOUBLE: {
10272
4.15k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10273
          /* Incompatible float will generate an error, leave this to run-time */
10274
4.15k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10275
3.65k
            goto fail;
10276
3.65k
          }
10277
499
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10278
499
          break;
10279
4.15k
        }
10280
533
        case IS_FALSE:
10281
533
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10282
533
          break;
10283
69
        case IS_TRUE:
10284
69
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10285
69
          break;
10286
35
        case IS_NULL:
10287
          /* Null key will generate a warning at run-time. */
10288
35
          goto fail;
10289
3
        default:
10290
3
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10291
0
          break;
10292
20.9k
      }
10293
93.9k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10294
3.79k
fail:
10295
3.79k
      zval_ptr_dtor_nogc(value);
10296
3.79k
      zval_ptr_dtor(result);
10297
3.79k
      return 0;
10298
105
    }
10299
114k
  }
10300
10301
67.3k
  return 1;
10302
71.1k
}
10303
/* }}} */
10304
10305
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10306
2.58M
{
10307
2.58M
  zend_ast *left_ast = ast->child[0];
10308
2.58M
  zend_ast *right_ast = ast->child[1];
10309
2.58M
  uint32_t opcode = ast->attr;
10310
10311
2.58M
  znode left_node, right_node;
10312
10313
2.58M
  zend_compile_expr(&left_node, left_ast);
10314
2.58M
  zend_compile_expr(&right_node, right_ast);
10315
10316
2.58M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10317
124k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10318
124k
        &left_node.u.constant, &right_node.u.constant)
10319
124k
    ) {
10320
110k
      result->op_type = IS_CONST;
10321
110k
      zval_ptr_dtor(&left_node.u.constant);
10322
110k
      zval_ptr_dtor(&right_node.u.constant);
10323
110k
      return;
10324
110k
    }
10325
124k
  }
10326
10327
2.47M
  do {
10328
2.47M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10329
      /* 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) */
10330
3.03k
      if (left_node.op_type == IS_CONST) {
10331
1.62k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10332
702
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10333
702
          opline->extended_value =
10334
702
            (opcode == ZEND_IS_IDENTICAL) ?
10335
564
              (1 << Z_TYPE(left_node.u.constant)) :
10336
702
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10337
702
          return;
10338
702
        }
10339
1.62k
      } else if (right_node.op_type == IS_CONST) {
10340
652
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10341
177
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10342
177
          opline->extended_value =
10343
177
            (opcode == ZEND_IS_IDENTICAL) ?
10344
104
              (1 << Z_TYPE(right_node.u.constant)) :
10345
177
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10346
177
          return;
10347
177
        }
10348
652
      }
10349
2.47M
    } else if (opcode == ZEND_CONCAT) {
10350
      /* convert constant operands to strings at compile-time */
10351
58.2k
      if (left_node.op_type == IS_CONST) {
10352
2.03k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10353
354
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10354
1.68k
        } else {
10355
1.68k
          convert_to_string(&left_node.u.constant);
10356
1.68k
        }
10357
2.03k
      }
10358
58.2k
      if (right_node.op_type == IS_CONST) {
10359
7.25k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10360
387
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10361
6.86k
        } else {
10362
6.86k
          convert_to_string(&right_node.u.constant);
10363
6.86k
        }
10364
7.25k
      }
10365
58.2k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10366
0
        opcode = ZEND_FAST_CONCAT;
10367
0
      }
10368
58.2k
    }
10369
2.47M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10370
2.47M
  } while (0);
10371
2.47M
}
10372
/* }}} */
10373
10374
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10375
 * evaluation order. */
10376
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10377
393k
{
10378
393k
  zend_ast *left_ast = ast->child[0];
10379
393k
  zend_ast *right_ast = ast->child[1];
10380
393k
  znode left_node, right_node;
10381
10382
393k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10383
10384
393k
  zend_compile_expr(&left_node, left_ast);
10385
393k
  zend_compile_expr(&right_node, right_ast);
10386
10387
393k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10388
9.70k
    result->op_type = IS_CONST;
10389
9.70k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10390
9.70k
      &left_node.u.constant, &right_node.u.constant);
10391
9.70k
    zval_ptr_dtor(&left_node.u.constant);
10392
9.70k
    zval_ptr_dtor(&right_node.u.constant);
10393
9.70k
    return;
10394
9.70k
  }
10395
10396
383k
  zend_emit_op_tmp(result,
10397
383k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10398
383k
    &right_node, &left_node);
10399
383k
}
10400
/* }}} */
10401
10402
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10403
1.62M
{
10404
1.62M
  zend_ast *expr_ast = ast->child[0];
10405
1.62M
  uint32_t opcode = ast->attr;
10406
10407
1.62M
  znode expr_node;
10408
1.62M
  zend_compile_expr(&expr_node, expr_ast);
10409
10410
1.62M
  if (expr_node.op_type == IS_CONST
10411
92.3k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10412
89.5k
    result->op_type = IS_CONST;
10413
89.5k
    zval_ptr_dtor(&expr_node.u.constant);
10414
89.5k
    return;
10415
89.5k
  }
10416
10417
1.53M
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10418
1.53M
}
10419
/* }}} */
10420
10421
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10422
94.4k
{
10423
94.4k
  zend_ast *expr_ast = ast->child[0];
10424
94.4k
  znode expr_node, right_node;
10425
10426
94.4k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10427
10428
94.4k
  zend_compile_expr(&expr_node, expr_ast);
10429
10430
94.4k
  if (expr_node.op_type == IS_CONST
10431
27.7k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10432
24.7k
    result->op_type = IS_CONST;
10433
24.7k
    zval_ptr_dtor(&expr_node.u.constant);
10434
24.7k
    return;
10435
24.7k
  }
10436
10437
69.6k
  right_node.op_type = IS_CONST;
10438
69.6k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10439
69.6k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10440
69.6k
}
10441
/* }}} */
10442
10443
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10444
11.1k
{
10445
11.1k
  zend_ast *left_ast = ast->child[0];
10446
11.1k
  zend_ast *right_ast = ast->child[1];
10447
10448
11.1k
  znode left_node, right_node;
10449
11.1k
  zend_op *opline_jmpz, *opline_bool;
10450
11.1k
  uint32_t opnum_jmpz;
10451
10452
11.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10453
10454
11.1k
  zend_compile_expr(&left_node, left_ast);
10455
10456
11.1k
  if (left_node.op_type == IS_CONST) {
10457
2.20k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10458
1.58k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10459
1.05k
      result->op_type = IS_CONST;
10460
1.05k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10461
1.15k
    } else {
10462
1.15k
      zend_compile_expr(&right_node, right_ast);
10463
10464
1.15k
      if (right_node.op_type == IS_CONST) {
10465
409
        result->op_type = IS_CONST;
10466
409
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10467
10468
409
        zval_ptr_dtor(&right_node.u.constant);
10469
746
      } else {
10470
746
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10471
746
      }
10472
1.15k
    }
10473
10474
2.20k
    zval_ptr_dtor(&left_node.u.constant);
10475
2.20k
    return;
10476
2.20k
  }
10477
10478
8.94k
  opnum_jmpz = get_next_op_number();
10479
8.94k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10480
8.94k
    &left_node, NULL);
10481
10482
8.94k
  if (left_node.op_type == IS_TMP_VAR) {
10483
7.44k
    SET_NODE(opline_jmpz->result, &left_node);
10484
7.44k
    GET_NODE(result, opline_jmpz->result);
10485
7.44k
  } else {
10486
1.50k
    zend_make_tmp_result(result, opline_jmpz);
10487
1.50k
  }
10488
10489
8.94k
  zend_compile_expr(&right_node, right_ast);
10490
10491
8.94k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10492
8.94k
  SET_NODE(opline_bool->result, result);
10493
10494
8.94k
  zend_update_jump_target_to_next(opnum_jmpz);
10495
8.94k
}
10496
/* }}} */
10497
10498
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10499
2.16k
{
10500
2.16k
  zend_ast *var_ast = ast->child[0];
10501
2.16k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10502
10503
2.16k
  zend_ensure_writable_variable(var_ast);
10504
10505
2.16k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10506
162
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10507
162
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10508
162
    zend_make_tmp_result(result, opline);
10509
2.00k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10510
1.01k
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10511
1.01k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10512
1.01k
    zend_make_tmp_result(result, opline);
10513
1.01k
  } else {
10514
985
    znode var_node;
10515
985
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10516
985
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10517
35
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10518
35
    }
10519
985
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10520
985
      &var_node, NULL);
10521
985
  }
10522
2.16k
}
10523
/* }}} */
10524
10525
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10526
1.44k
{
10527
1.44k
  zend_ast *var_ast = ast->child[0];
10528
1.44k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10529
10530
1.44k
  zend_ensure_writable_variable(var_ast);
10531
10532
1.44k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10533
287
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10534
287
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10535
287
    opline->result_type = IS_TMP_VAR;
10536
287
    result->op_type = IS_TMP_VAR;
10537
1.16k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10538
228
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10539
228
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10540
228
    opline->result_type = IS_TMP_VAR;
10541
228
    result->op_type = IS_TMP_VAR;
10542
932
  } else {
10543
932
    znode var_node;
10544
932
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10545
932
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10546
28
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10547
28
    }
10548
932
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10549
932
      &var_node, NULL);
10550
932
  }
10551
1.44k
}
10552
/* }}} */
10553
10554
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10555
1.26k
{
10556
1.26k
  zend_ast *expr_ast = ast->child[0];
10557
1.26k
  znode expr_node;
10558
1.26k
  zend_op *opline;
10559
10560
1.26k
  zend_compile_expr(&expr_node, expr_ast);
10561
10562
1.26k
  if (ast->attr == _IS_BOOL) {
10563
214
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10564
1.04k
  } else if (ast->attr == IS_NULL) {
10565
12
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10566
1.03k
  } else {
10567
1.03k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10568
1.03k
    opline->extended_value = ast->attr;
10569
1.03k
  }
10570
1.26k
}
10571
/* }}} */
10572
10573
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10574
4.05k
{
10575
4.05k
  zend_ast *cond_ast = ast->child[0];
10576
4.05k
  zend_ast *false_ast = ast->child[2];
10577
10578
4.05k
  znode cond_node, false_node;
10579
4.05k
  zend_op *opline_qm_assign;
10580
4.05k
  uint32_t opnum_jmp_set;
10581
10582
4.05k
  ZEND_ASSERT(ast->child[1] == NULL);
10583
10584
4.05k
  zend_compile_expr(&cond_node, cond_ast);
10585
10586
4.05k
  opnum_jmp_set = get_next_op_number();
10587
4.05k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10588
10589
4.05k
  zend_compile_expr(&false_node, false_ast);
10590
10591
4.05k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10592
4.05k
  SET_NODE(opline_qm_assign->result, result);
10593
10594
4.05k
  zend_update_jump_target_to_next(opnum_jmp_set);
10595
4.05k
}
10596
/* }}} */
10597
10598
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10599
7.62k
{
10600
7.62k
  zend_ast *cond_ast = ast->child[0];
10601
7.62k
  zend_ast *true_ast = ast->child[1];
10602
7.62k
  zend_ast *false_ast = ast->child[2];
10603
10604
7.62k
  znode cond_node, true_node, false_node;
10605
7.62k
  zend_op *opline_qm_assign2;
10606
7.62k
  uint32_t opnum_jmpz, opnum_jmp;
10607
10608
7.62k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10609
3.15k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10610
2.74k
    if (cond_ast->child[1]) {
10611
20
      if (true_ast) {
10612
8
        zend_error(E_COMPILE_ERROR,
10613
8
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10614
8
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10615
12
      } else {
10616
12
        zend_error(E_COMPILE_ERROR,
10617
12
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10618
12
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10619
12
      }
10620
2.72k
    } else {
10621
2.72k
      if (true_ast) {
10622
3
        zend_error(E_COMPILE_ERROR,
10623
3
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10624
3
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10625
2.71k
      } else {
10626
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10627
         * as a ?: (b ?: c). */
10628
2.71k
      }
10629
2.72k
    }
10630
2.74k
  }
10631
10632
7.62k
  if (!true_ast) {
10633
4.05k
    zend_compile_shorthand_conditional(result, ast);
10634
4.05k
    return;
10635
4.05k
  }
10636
10637
3.57k
  zend_compile_expr(&cond_node, cond_ast);
10638
10639
3.57k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10640
10641
3.57k
  zend_compile_expr(&true_node, true_ast);
10642
10643
3.57k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10644
10645
3.57k
  opnum_jmp = zend_emit_jump(0);
10646
10647
3.57k
  zend_update_jump_target_to_next(opnum_jmpz);
10648
10649
3.57k
  zend_compile_expr(&false_node, false_ast);
10650
10651
3.57k
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10652
3.57k
  SET_NODE(opline_qm_assign2->result, result);
10653
10654
3.57k
  zend_update_jump_target_to_next(opnum_jmp);
10655
3.57k
}
10656
/* }}} */
10657
10658
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10659
2.14M
{
10660
2.14M
  zend_ast *expr_ast = ast->child[0];
10661
2.14M
  zend_ast *default_ast = ast->child[1];
10662
10663
2.14M
  znode expr_node, default_node;
10664
2.14M
  zend_op *opline;
10665
2.14M
  uint32_t opnum;
10666
10667
2.14M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10668
10669
2.14M
  opnum = get_next_op_number();
10670
2.14M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10671
10672
2.14M
  zend_compile_expr(&default_node, default_ast);
10673
10674
2.14M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10675
2.14M
  SET_NODE(opline->result, result);
10676
10677
2.14M
  opline = &CG(active_op_array)->opcodes[opnum];
10678
2.14M
  opline->op2.opline_num = get_next_op_number();
10679
2.14M
}
10680
/* }}} */
10681
10682
133k
static void znode_dtor(zval *zv) {
10683
133k
  znode *node = Z_PTR_P(zv);
10684
133k
  if (node->op_type == IS_CONST) {
10685
5.23k
    zval_ptr_dtor_nogc(&node->u.constant);
10686
5.23k
  }
10687
133k
  efree(node);
10688
133k
}
10689
10690
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10691
34.9k
{
10692
34.9k
  zend_ast *var_ast = ast->child[0];
10693
34.9k
  zend_ast *default_ast = ast->child[1];
10694
10695
34.9k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10696
34.9k
  zend_op *opline;
10697
34.9k
  uint32_t coalesce_opnum;
10698
34.9k
  bool need_frees = false;
10699
10700
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10701
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10702
34.9k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10703
34.9k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10704
10705
34.9k
  zend_ensure_writable_variable(var_ast);
10706
34.9k
  if (is_this_fetch(var_ast)) {
10707
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10708
1
  }
10709
10710
34.9k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10711
34.9k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10712
10713
34.9k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10714
34.9k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10715
10716
34.9k
  coalesce_opnum = get_next_op_number();
10717
34.9k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10718
10719
34.9k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10720
34.9k
  if (var_ast->kind == ZEND_AST_DIM) {
10721
32.2k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10722
32.2k
  } else {
10723
2.61k
    zend_compile_expr(&default_node, default_ast);
10724
2.61k
  }
10725
10726
34.9k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10727
34.9k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10728
10729
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10730
34.9k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10731
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10732
34.9k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10733
34.9k
  switch (kind) {
10734
1.59k
    case ZEND_AST_VAR:
10735
1.59k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10736
1.59k
      break;
10737
472
    case ZEND_AST_STATIC_PROP:
10738
472
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10739
472
      opline->result_type = IS_TMP_VAR;
10740
472
      var_node_w.op_type = IS_TMP_VAR;
10741
472
      zend_emit_op_data(&default_node);
10742
472
      assign_node = var_node_w;
10743
472
      break;
10744
31.9k
    case ZEND_AST_DIM:
10745
31.9k
      opline->opcode = ZEND_ASSIGN_DIM;
10746
31.9k
      opline->result_type = IS_TMP_VAR;
10747
31.9k
      var_node_w.op_type = IS_TMP_VAR;
10748
31.9k
      zend_emit_op_data(&default_node);
10749
31.9k
      assign_node = var_node_w;
10750
31.9k
      break;
10751
707
    case ZEND_AST_PROP:
10752
707
    case ZEND_AST_NULLSAFE_PROP:
10753
707
      opline->opcode = ZEND_ASSIGN_OBJ;
10754
707
      opline->result_type = IS_TMP_VAR;
10755
707
      var_node_w.op_type = IS_TMP_VAR;
10756
707
      zend_emit_op_data(&default_node);
10757
707
      assign_node = var_node_w;
10758
707
      break;
10759
0
    EMPTY_SWITCH_DEFAULT_CASE();
10760
34.9k
  }
10761
10762
34.6k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10763
34.6k
  SET_NODE(opline->result, result);
10764
10765
108k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10766
108k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10767
31.7k
      need_frees = true;
10768
31.7k
      break;
10769
31.7k
    }
10770
108k
  } ZEND_HASH_FOREACH_END();
10771
10772
  /* Free DUPed expressions if there are any */
10773
34.6k
  if (need_frees) {
10774
31.7k
    uint32_t jump_opnum = zend_emit_jump(0);
10775
31.7k
    zend_update_jump_target_to_next(coalesce_opnum);
10776
289k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10777
289k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10778
126k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10779
126k
      }
10780
289k
    } ZEND_HASH_FOREACH_END();
10781
31.7k
    zend_update_jump_target_to_next(jump_opnum);
10782
31.7k
  } else {
10783
2.93k
    zend_update_jump_target_to_next(coalesce_opnum);
10784
2.93k
  }
10785
10786
34.6k
  zend_hash_destroy(CG(memoized_exprs));
10787
34.6k
  FREE_HASHTABLE(CG(memoized_exprs));
10788
34.6k
  CG(memoized_exprs) = orig_memoized_exprs;
10789
34.6k
  CG(memoize_mode) = orig_memoize_mode;
10790
34.6k
}
10791
/* }}} */
10792
10793
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10794
1.35k
{
10795
1.35k
  zend_op *opline;
10796
1.35k
  zend_ast *expr_ast = ast->child[0];
10797
10798
1.35k
  znode expr_node;
10799
1.35k
  zend_compile_expr(&expr_node, expr_ast);
10800
10801
1.35k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10802
1.35k
  opline->extended_value = 1;
10803
10804
1.35k
  result->op_type = IS_CONST;
10805
1.35k
  ZVAL_LONG(&result->u.constant, 1);
10806
1.35k
}
10807
/* }}} */
10808
10809
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10810
47.0k
{
10811
47.0k
  zend_ast *value_ast = ast->child[0];
10812
47.0k
  zend_ast *key_ast = ast->child[1];
10813
10814
47.0k
  znode value_node, key_node;
10815
47.0k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10816
47.0k
  zend_op *opline;
10817
47.0k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10818
10819
47.0k
  zend_mark_function_as_generator();
10820
10821
47.0k
  if (key_ast) {
10822
217
    zend_compile_expr(&key_node, key_ast);
10823
217
    key_node_ptr = &key_node;
10824
217
  }
10825
10826
47.0k
  if (value_ast) {
10827
43.8k
    if (returns_by_ref && zend_is_variable(value_ast)) {
10828
84
      zend_assert_not_short_circuited(value_ast);
10829
84
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
10830
43.8k
    } else {
10831
43.8k
      zend_compile_expr(&value_node, value_ast);
10832
43.8k
    }
10833
43.8k
    value_node_ptr = &value_node;
10834
43.8k
  }
10835
10836
47.0k
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10837
10838
47.0k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10839
1.16k
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10840
1.16k
  }
10841
47.0k
}
10842
/* }}} */
10843
10844
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
10845
2.09k
{
10846
2.09k
  zend_ast *expr_ast = ast->child[0];
10847
2.09k
  znode expr_node;
10848
10849
2.09k
  zend_mark_function_as_generator();
10850
10851
2.09k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10852
1
    zend_error_noreturn(E_COMPILE_ERROR,
10853
1
      "Cannot use \"yield from\" inside a by-reference generator");
10854
1
  }
10855
10856
2.09k
  zend_compile_expr(&expr_node, expr_ast);
10857
2.09k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10858
2.09k
}
10859
/* }}} */
10860
10861
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10862
566
{
10863
566
  zend_ast *obj_ast = ast->child[0];
10864
566
  zend_ast *class_ast = ast->child[1];
10865
10866
566
  znode obj_node, class_node;
10867
566
  zend_op *opline;
10868
10869
566
  zend_compile_expr(&obj_node, obj_ast);
10870
566
  if (obj_node.op_type == IS_CONST) {
10871
102
    zend_do_free(&obj_node);
10872
102
    result->op_type = IS_CONST;
10873
102
    ZVAL_FALSE(&result->u.constant);
10874
102
    return;
10875
102
  }
10876
10877
464
  zend_compile_class_ref(&class_node, class_ast,
10878
464
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10879
10880
464
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10881
10882
464
  if (class_node.op_type == IS_CONST) {
10883
70
    opline->op2_type = IS_CONST;
10884
70
    opline->op2.constant = zend_add_class_name_literal(
10885
70
      Z_STR(class_node.u.constant));
10886
70
    opline->extended_value = zend_alloc_cache_slot();
10887
394
  } else {
10888
394
    SET_NODE(opline->op2, &class_node);
10889
394
  }
10890
464
}
10891
/* }}} */
10892
10893
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
10894
1.23k
{
10895
1.23k
  zend_ast *expr_ast = ast->child[0];
10896
1.23k
  znode expr_node;
10897
1.23k
  zend_op *opline;
10898
10899
1.23k
  zend_do_extended_fcall_begin();
10900
1.23k
  zend_compile_expr(&expr_node, expr_ast);
10901
10902
1.23k
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10903
1.23k
  opline->extended_value = ast->attr;
10904
10905
1.23k
  zend_do_extended_fcall_end();
10906
1.23k
}
10907
/* }}} */
10908
10909
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
10910
10.5k
{
10911
10.5k
  zend_ast *var_ast = ast->child[0];
10912
10913
10.5k
  znode var_node;
10914
10.5k
  zend_op *opline = NULL;
10915
10916
10.5k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10917
10918
10.5k
  if (!zend_is_variable(var_ast)) {
10919
256
    if (ast->kind == ZEND_AST_EMPTY) {
10920
      /* empty(expr) can be transformed to !expr */
10921
225
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10922
225
      zend_compile_expr(result, not_ast);
10923
225
      return;
10924
225
    } else {
10925
31
      zend_error_noreturn(E_COMPILE_ERROR,
10926
31
        "Cannot use isset() on the result of an expression "
10927
31
        "(you can use \"null !== expression\" instead)");
10928
31
    }
10929
256
  }
10930
10931
10.3k
  if (is_globals_fetch(var_ast)) {
10932
413
    result->op_type = IS_CONST;
10933
413
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10934
413
    return;
10935
413
  }
10936
10937
9.90k
  if (is_global_var_fetch(var_ast)) {
10938
4.56k
    if (!var_ast->child[1]) {
10939
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10940
2
    }
10941
10942
4.56k
    zend_compile_expr(&var_node, var_ast->child[1]);
10943
4.56k
    if (var_node.op_type == IS_CONST) {
10944
4.04k
      convert_to_string(&var_node.u.constant);
10945
4.04k
    }
10946
10947
4.56k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10948
4.56k
    opline->extended_value =
10949
4.56k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10950
4.56k
    return;
10951
4.56k
  }
10952
10953
5.33k
  zend_short_circuiting_mark_inner(var_ast);
10954
5.33k
  switch (var_ast->kind) {
10955
2.91k
    case ZEND_AST_VAR:
10956
2.91k
      if (is_this_fetch(var_ast)) {
10957
203
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10958
203
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10959
2.70k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
10960
1.99k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10961
1.99k
      } else {
10962
712
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
10963
712
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10964
712
      }
10965
2.91k
      break;
10966
1.85k
    case ZEND_AST_DIM:
10967
1.85k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10968
1.85k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10969
1.85k
      break;
10970
163
    case ZEND_AST_PROP:
10971
490
    case ZEND_AST_NULLSAFE_PROP:
10972
490
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
10973
490
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10974
490
      break;
10975
73
    case ZEND_AST_STATIC_PROP:
10976
73
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
10977
73
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10978
73
      break;
10979
5.33k
    EMPTY_SWITCH_DEFAULT_CASE()
10980
5.33k
  }
10981
10982
5.32k
  result->op_type = opline->result_type = IS_TMP_VAR;
10983
5.32k
  if (!(ast->kind == ZEND_AST_ISSET)) {
10984
158
    opline->extended_value |= ZEND_ISEMPTY;
10985
158
  }
10986
5.32k
}
10987
/* }}} */
10988
10989
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
10990
17.3M
{
10991
17.3M
  zend_ast *expr_ast = ast->child[0];
10992
17.3M
  znode silence_node;
10993
10994
17.3M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
10995
10996
17.3M
  if (expr_ast->kind == ZEND_AST_VAR) {
10997
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
10998
     * happen outside the silenced section. */
10999
107k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11000
17.2M
  } else {
11001
17.2M
    zend_compile_expr(result, expr_ast);
11002
17.2M
  }
11003
11004
17.3M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11005
17.3M
}
11006
/* }}} */
11007
11008
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11009
31.6k
{
11010
31.6k
  zend_ast *expr_ast = ast->child[0];
11011
11012
31.6k
  zval fn_name;
11013
31.6k
  zend_ast *name_ast, *args_ast, *call_ast;
11014
11015
31.6k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11016
11017
31.6k
  ZVAL_STRING(&fn_name, "shell_exec");
11018
31.6k
  name_ast = zend_ast_create_zval(&fn_name);
11019
31.6k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11020
31.6k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11021
11022
31.6k
  zend_compile_expr(result, call_ast);
11023
11024
31.6k
  zval_ptr_dtor(&fn_name);
11025
31.6k
}
11026
/* }}} */
11027
11028
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11029
111k
{
11030
111k
  zend_ast_list *list = zend_ast_get_list(ast);
11031
111k
  zend_op *opline;
11032
111k
  uint32_t i, opnum_init = -1;
11033
111k
  bool packed = true;
11034
11035
111k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11036
47.1k
    result->op_type = IS_CONST;
11037
47.1k
    return;
11038
47.1k
  }
11039
11040
  /* Empty arrays are handled at compile-time */
11041
64.8k
  ZEND_ASSERT(list->children > 0);
11042
11043
198k
  for (i = 0; i < list->children; ++i) {
11044
133k
    zend_ast *elem_ast = list->child[i];
11045
133k
    zend_ast *value_ast, *key_ast;
11046
133k
    bool by_ref;
11047
133k
    znode value_node, key_node, *key_node_ptr = NULL;
11048
11049
133k
    if (elem_ast == NULL) {
11050
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11051
0
    }
11052
11053
133k
    value_ast = elem_ast->child[0];
11054
11055
133k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11056
1.31k
      zend_compile_expr(&value_node, value_ast);
11057
1.31k
      if (i == 0) {
11058
1.10k
        opnum_init = get_next_op_number();
11059
1.10k
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11060
1.10k
      }
11061
1.31k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11062
1.31k
      SET_NODE(opline->result, result);
11063
1.31k
      continue;
11064
1.31k
    }
11065
11066
132k
    key_ast = elem_ast->child[1];
11067
132k
    by_ref = elem_ast->attr;
11068
11069
132k
    if (key_ast) {
11070
6.32k
      zend_compile_expr(&key_node, key_ast);
11071
6.32k
      zend_handle_numeric_op(&key_node);
11072
6.32k
      key_node_ptr = &key_node;
11073
6.32k
    }
11074
11075
132k
    if (by_ref) {
11076
97
      zend_ensure_writable_variable(value_ast);
11077
97
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11078
132k
    } else {
11079
132k
      zend_compile_expr(&value_node, value_ast);
11080
132k
    }
11081
11082
132k
    if (i == 0) {
11083
63.5k
      opnum_init = get_next_op_number();
11084
63.5k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11085
63.5k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11086
69.0k
    } else {
11087
69.0k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11088
69.0k
        &value_node, key_node_ptr);
11089
69.0k
      SET_NODE(opline->result, result);
11090
69.0k
    }
11091
132k
    opline->extended_value |= by_ref;
11092
11093
132k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11094
1.72k
      packed = false;
11095
1.72k
    }
11096
132k
  }
11097
11098
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11099
64.6k
  if (!packed) {
11100
915
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11101
915
    opline = &CG(active_op_array)->opcodes[opnum_init];
11102
915
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11103
915
  }
11104
64.6k
}
11105
/* }}} */
11106
11107
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11108
10.4M
{
11109
10.4M
  zend_ast *name_ast = ast->child[0];
11110
11111
10.4M
  zend_op *opline;
11112
11113
10.4M
  bool is_fully_qualified;
11114
10.4M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11115
10.4M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11116
11117
10.4M
  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__"))) {
11118
3.64k
    zend_ast *last = CG(ast);
11119
11120
7.30k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11121
3.96k
      const zend_ast_list *list = zend_ast_get_list(last);
11122
3.96k
      if (list->children == 0) {
11123
315
        break;
11124
315
      }
11125
3.65k
      last = list->child[list->children-1];
11126
3.65k
    }
11127
3.64k
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11128
56
      result->op_type = IS_CONST;
11129
56
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11130
56
      zend_string_release_ex(resolved_name, 0);
11131
56
      return;
11132
56
    }
11133
3.64k
  }
11134
11135
10.4M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11136
30.1k
    result->op_type = IS_CONST;
11137
30.1k
    zend_string_release_ex(resolved_name, 0);
11138
30.1k
    return;
11139
30.1k
  }
11140
11141
10.4M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11142
10.4M
  opline->op2_type = IS_CONST;
11143
11144
10.4M
  if (is_fully_qualified || !FC(current_namespace)) {
11145
160k
    opline->op1.num = 0;
11146
160k
    opline->op2.constant = zend_add_const_name_literal(
11147
160k
      resolved_name, false);
11148
10.2M
  } else {
11149
10.2M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11150
10.2M
    opline->op2.constant = zend_add_const_name_literal(
11151
10.2M
      resolved_name, true);
11152
10.2M
  }
11153
10.4M
  opline->extended_value = zend_alloc_cache_slot();
11154
10.4M
}
11155
/* }}} */
11156
11157
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11158
114k
{
11159
114k
  zend_ast *class_ast;
11160
114k
  zend_ast *const_ast;
11161
114k
  znode class_node, const_node;
11162
114k
  zend_op *opline;
11163
11164
114k
  zend_eval_const_expr(&ast->child[0]);
11165
114k
  zend_eval_const_expr(&ast->child[1]);
11166
11167
114k
  class_ast = ast->child[0];
11168
114k
  const_ast = ast->child[1];
11169
11170
114k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11171
54.8k
    zval *const_zv = zend_ast_get_zval(const_ast);
11172
54.8k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11173
54.1k
      zend_string *const_str = Z_STR_P(const_zv);
11174
54.1k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11175
54.1k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11176
0
        result->op_type = IS_CONST;
11177
0
        zend_string_release_ex(resolved_name, 0);
11178
0
        return;
11179
0
      }
11180
54.1k
      zend_string_release_ex(resolved_name, 0);
11181
54.1k
    }
11182
54.8k
  }
11183
11184
114k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11185
11186
114k
  zend_compile_expr(&const_node, const_ast);
11187
11188
114k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11189
11190
114k
  zend_set_class_name_op1(opline, &class_node);
11191
11192
114k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11193
114k
    opline->extended_value = zend_alloc_cache_slots(2);
11194
114k
  }
11195
114k
}
11196
/* }}} */
11197
11198
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11199
2.07k
{
11200
2.07k
  zend_ast *class_ast = ast->child[0];
11201
11202
2.07k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11203
477
    result->op_type = IS_CONST;
11204
477
    return;
11205
477
  }
11206
11207
1.59k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11208
1.15k
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11209
1.15k
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11210
1.15k
  } else {
11211
442
    znode expr_node;
11212
442
    zend_compile_expr(&expr_node, class_ast);
11213
442
    if (expr_node.op_type == IS_CONST) {
11214
      /* Unlikely case that happen if class_ast is constant folded.
11215
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11216
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11217
7
        zend_zval_value_name(&expr_node.u.constant));
11218
7
    }
11219
11220
435
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11221
435
  }
11222
1.59k
}
11223
/* }}} */
11224
11225
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11226
65.7k
{
11227
65.7k
  if (num == 0) {
11228
5.09k
    result->op_type = IS_TMP_VAR;
11229
5.09k
    result->u.op.var = -1;
11230
5.09k
    opline->opcode = ZEND_ROPE_INIT;
11231
60.6k
  } else {
11232
60.6k
    opline->opcode = ZEND_ROPE_ADD;
11233
60.6k
    SET_NODE(opline->op1, result);
11234
60.6k
  }
11235
65.7k
  SET_NODE(opline->op2, elem_node);
11236
65.7k
  SET_NODE(opline->result, result);
11237
65.7k
  opline->extended_value = num;
11238
65.7k
  return opline;
11239
65.7k
}
11240
/* }}} */
11241
11242
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11243
75.7k
{
11244
75.7k
  zend_op *opline = get_next_op();
11245
11246
75.7k
  if (num == 0) {
11247
14.9k
    result->op_type = IS_TMP_VAR;
11248
14.9k
    result->u.op.var = -1;
11249
14.9k
    opline->opcode = ZEND_ROPE_INIT;
11250
60.8k
  } else {
11251
60.8k
    opline->opcode = ZEND_ROPE_ADD;
11252
60.8k
    SET_NODE(opline->op1, result);
11253
60.8k
  }
11254
75.7k
  SET_NODE(opline->op2, elem_node);
11255
75.7k
  SET_NODE(opline->result, result);
11256
75.7k
  opline->extended_value = num;
11257
75.7k
  return opline;
11258
75.7k
}
11259
/* }}} */
11260
11261
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11262
20.0k
{
11263
20.0k
  if (rope_elements == 1) {
11264
2.05k
    if (opline->op2_type == IS_CONST) {
11265
913
      GET_NODE(result, opline->op2);
11266
913
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11267
913
      SET_UNUSED(opline->op2);
11268
913
      MAKE_NOP(opline);
11269
1.14k
    } else {
11270
1.14k
      opline->opcode = ZEND_CAST;
11271
1.14k
      opline->extended_value = IS_STRING;
11272
1.14k
      opline->op1_type = opline->op2_type;
11273
1.14k
      opline->op1 = opline->op2;
11274
1.14k
      SET_UNUSED(opline->op2);
11275
1.14k
      zend_make_tmp_result(result, opline);
11276
1.14k
    }
11277
17.9k
  } else if (rope_elements == 2) {
11278
8.55k
    opline->opcode = ZEND_FAST_CONCAT;
11279
8.55k
    opline->extended_value = 0;
11280
8.55k
    opline->op1_type = init_opline->op2_type;
11281
8.55k
    opline->op1 = init_opline->op2;
11282
8.55k
    zend_make_tmp_result(result, opline);
11283
8.55k
    MAKE_NOP(init_opline);
11284
9.39k
  } else {
11285
9.39k
    uint32_t var;
11286
11287
9.39k
    init_opline->extended_value = rope_elements;
11288
9.39k
    opline->opcode = ZEND_ROPE_END;
11289
9.39k
    zend_make_tmp_result(result, opline);
11290
9.39k
    var = opline->op1.var = get_temporary_variable();
11291
11292
    /* Allocates the necessary number of zval slots to keep the rope */
11293
9.39k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11294
64.7k
    while (i > 1) {
11295
55.3k
      get_temporary_variable();
11296
55.3k
      i--;
11297
55.3k
    }
11298
11299
    /* Update all the previous opcodes to use the same variable */
11300
196k
    while (opline != init_opline) {
11301
187k
      opline--;
11302
187k
      if (opline->opcode == ZEND_ROPE_ADD &&
11303
105k
          opline->result.var == (uint32_t)-1) {
11304
103k
        opline->op1.var = var;
11305
103k
        opline->result.var = var;
11306
103k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11307
9.97k
                 opline->result.var == (uint32_t)-1) {
11308
9.39k
        opline->result.var = var;
11309
9.39k
      }
11310
187k
    }
11311
9.39k
  }
11312
20.0k
}
11313
11314
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11315
18.3k
{
11316
18.3k
  uint32_t i, j;
11317
18.3k
  uint32_t rope_init_lineno = -1;
11318
18.3k
  zend_op *opline = NULL, *init_opline;
11319
18.3k
  znode elem_node, last_const_node;
11320
18.3k
  zend_ast_list *list = zend_ast_get_list(ast);
11321
18.3k
  uint32_t reserved_op_number = -1;
11322
11323
18.3k
  ZEND_ASSERT(list->children > 0);
11324
11325
18.3k
  j = 0;
11326
18.3k
  last_const_node.op_type = IS_UNUSED;
11327
156k
  for (i = 0; i < list->children; i++) {
11328
138k
    zend_ast *encaps_var = list->child[i];
11329
11330
138k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11331
16.6k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11332
779
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11333
15.9k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11334
15.9k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11335
15.9k
      }
11336
16.6k
    }
11337
11338
138k
    zend_compile_expr(&elem_node, encaps_var);
11339
11340
138k
    if (elem_node.op_type == IS_CONST) {
11341
66.4k
      convert_to_string(&elem_node.u.constant);
11342
11343
66.4k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11344
708
        zval_ptr_dtor(&elem_node.u.constant);
11345
65.7k
      } else if (last_const_node.op_type == IS_CONST) {
11346
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11347
0
        zval_ptr_dtor(&elem_node.u.constant);
11348
65.7k
      } else {
11349
65.7k
        last_const_node.op_type = IS_CONST;
11350
65.7k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11351
        /* Reserve place for ZEND_ROPE_ADD instruction */
11352
65.7k
        reserved_op_number = get_next_op_number();
11353
65.7k
        opline = get_next_op();
11354
65.7k
        opline->opcode = ZEND_NOP;
11355
65.7k
      }
11356
66.4k
      continue;
11357
71.5k
    } else {
11358
71.5k
      if (j == 0) {
11359
18.3k
        if (last_const_node.op_type == IS_CONST) {
11360
5.09k
          rope_init_lineno = reserved_op_number;
11361
13.2k
        } else {
11362
13.2k
          rope_init_lineno = get_next_op_number();
11363
13.2k
        }
11364
18.3k
      }
11365
71.5k
      if (last_const_node.op_type == IS_CONST) {
11366
50.9k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11367
50.9k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11368
50.9k
        last_const_node.op_type = IS_UNUSED;
11369
50.9k
      }
11370
71.5k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11371
71.5k
    }
11372
138k
  }
11373
11374
18.3k
  if (j == 0) {
11375
0
    result->op_type = IS_CONST;
11376
0
    if (last_const_node.op_type == IS_CONST) {
11377
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11378
0
    } else {
11379
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11380
      /* empty string */
11381
0
    }
11382
0
    CG(active_op_array)->last = reserved_op_number - 1;
11383
0
    return;
11384
18.3k
  } else if (last_const_node.op_type == IS_CONST) {
11385
14.8k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11386
14.8k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11387
14.8k
  }
11388
18.3k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11389
18.3k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11390
18.3k
}
11391
/* }}} */
11392
11393
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11394
16.7k
{
11395
16.7k
  zend_op *opline;
11396
11397
16.7k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11398
15.5k
    result->op_type = IS_CONST;
11399
15.5k
    return;
11400
15.5k
  }
11401
11402
1.24k
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11403
1.24k
              CG(active_class_entry) &&
11404
1.24k
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11405
11406
1.24k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11407
1.24k
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11408
1.24k
}
11409
/* }}} */
11410
11411
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11412
42.1k
{
11413
42.1k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11414
35.3k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11415
34.4k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11416
34.0k
    || kind == ZEND_AST_UNARY_OP
11417
33.0k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11418
32.4k
    || kind == ZEND_AST_CAST
11419
32.1k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11420
31.2k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11421
29.8k
    || kind == ZEND_AST_UNPACK
11422
29.7k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11423
7.00k
    || kind == ZEND_AST_CLASS_NAME
11424
6.99k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11425
6.48k
    || kind == ZEND_AST_CONST_ENUM_INIT
11426
5.26k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11427
1.32k
    || kind == ZEND_AST_NAMED_ARG
11428
1.29k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11429
735
    || kind == ZEND_AST_CLOSURE
11430
637
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11431
42.1k
}
11432
/* }}} */
11433
11434
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11435
1.72k
{
11436
1.72k
  zend_ast *ast = *ast_ptr;
11437
1.72k
  zend_ast *class_ast = ast->child[0];
11438
1.72k
  zend_string *class_name;
11439
1.72k
  int fetch_type;
11440
11441
1.72k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11442
5
    zend_error_noreturn(E_COMPILE_ERROR,
11443
5
      "Dynamic class names are not allowed in compile-time class constant references");
11444
5
  }
11445
1.71k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11446
2
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11447
2
  }
11448
11449
1.71k
  class_name = zend_ast_get_str(class_ast);
11450
1.71k
  fetch_type = zend_get_class_fetch_type(class_name);
11451
11452
1.71k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11453
1
    zend_error_noreturn(E_COMPILE_ERROR,
11454
1
      "\"static::\" is not allowed in compile-time constants");
11455
1
  }
11456
11457
1.71k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11458
1.34k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11459
11460
1.34k
    zend_string_release_ex(class_name, 0);
11461
1.34k
    if (tmp != class_name) {
11462
346
      zval *zv = zend_ast_get_zval(class_ast);
11463
346
      ZVAL_STR(zv, tmp);
11464
346
      class_ast->attr = ZEND_NAME_FQ;
11465
346
    }
11466
1.34k
  }
11467
11468
1.71k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11469
1.71k
}
11470
/* }}} */
11471
11472
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11473
6
{
11474
6
  zend_ast *ast = *ast_ptr;
11475
6
  zend_ast *class_ast = ast->child[0];
11476
6
  if (class_ast->kind != ZEND_AST_ZVAL) {
11477
1
    zend_error_noreturn(E_COMPILE_ERROR,
11478
1
      "(expression)::class cannot be used in constant expressions");
11479
1
  }
11480
11481
5
  zend_string *class_name = zend_ast_get_str(class_ast);
11482
5
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11483
11484
5
  switch (fetch_type) {
11485
3
    case ZEND_FETCH_CLASS_SELF:
11486
4
    case ZEND_FETCH_CLASS_PARENT:
11487
      /* For the const-eval representation store the fetch type instead of the name. */
11488
4
      zend_string_release(class_name);
11489
4
      ast->child[0] = NULL;
11490
4
      ast->attr = fetch_type;
11491
4
      return;
11492
1
    case ZEND_FETCH_CLASS_STATIC:
11493
1
      zend_error_noreturn(E_COMPILE_ERROR,
11494
1
        "static::class cannot be used for compile-time class name resolution");
11495
0
      return;
11496
5
    EMPTY_SWITCH_DEFAULT_CASE()
11497
5
  }
11498
5
}
11499
11500
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11501
21.0k
{
11502
21.0k
  zend_ast *ast = *ast_ptr;
11503
21.0k
  zend_ast *name_ast = ast->child[0];
11504
21.0k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11505
21.0k
  bool is_fully_qualified;
11506
21.0k
  zval result;
11507
21.0k
  zend_string *resolved_name;
11508
11509
21.0k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11510
11511
21.0k
  resolved_name = zend_resolve_const_name(
11512
21.0k
    orig_name, name_ast->attr, &is_fully_qualified);
11513
11514
21.0k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11515
0
    zend_string_release_ex(resolved_name, 0);
11516
0
    zend_ast_destroy(ast);
11517
0
    *ast_ptr = zend_ast_create_zval(&result);
11518
0
    return;
11519
0
  }
11520
11521
21.0k
  zend_ast_destroy(ast);
11522
21.0k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11523
21.0k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11524
21.0k
}
11525
/* }}} */
11526
11527
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11528
312
{
11529
312
  zend_ast *ast = *ast_ptr;
11530
11531
  /* Other cases already resolved by constant folding */
11532
312
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11533
11534
312
  zend_ast_destroy(ast);
11535
312
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11536
312
}
11537
/* }}} */
11538
11539
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11540
2.01k
{
11541
2.01k
  if (class_ast->kind == ZEND_AST_CLASS) {
11542
1
    zend_error_noreturn(E_COMPILE_ERROR,
11543
1
      "Cannot use anonymous class in constant expression");
11544
1
  }
11545
2.01k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11546
5
    zend_error_noreturn(E_COMPILE_ERROR,
11547
5
      "Cannot use dynamic class name in constant expression");
11548
5
  }
11549
11550
2.00k
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11551
2.00k
  int fetch_type = zend_get_class_fetch_type(class_name);
11552
2.00k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11553
1
    zend_error_noreturn(E_COMPILE_ERROR,
11554
1
      "\"static\" is not allowed in compile-time constants");
11555
1
  }
11556
11557
2.00k
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11558
2.00k
  zval_ptr_dtor_nogc(class_ast_zv);
11559
2.00k
  ZVAL_STR(class_ast_zv, class_name);
11560
2.00k
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11561
2.00k
}
11562
11563
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11564
1.97k
{
11565
1.97k
  zend_ast *class_ast = (*ast_ptr)->child[0];
11566
1.97k
  zend_compile_const_expr_class_reference(class_ast);
11567
11568
1.97k
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11569
1.97k
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11570
1
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11571
1
  }
11572
1.97k
}
11573
11574
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11575
98
{
11576
98
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11577
98
  const zend_ast *uses_ast = closure_ast->child[1];
11578
98
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11579
6
    zend_error_noreturn(E_COMPILE_ERROR,
11580
6
      "Closures in constant expressions must be static");
11581
6
  }
11582
92
  if (uses_ast) {
11583
1
    zend_error_noreturn(E_COMPILE_ERROR,
11584
1
      "Cannot use(...) variables in constant expression");
11585
1
  }
11586
11587
91
  znode node;
11588
91
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11589
11590
91
  zend_ast_destroy(*ast_ptr);
11591
91
  *ast_ptr = zend_ast_create_op_array(op);
11592
91
}
11593
11594
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11595
313
{
11596
313
  zend_ast **args_ast;
11597
313
  switch ((*ast_ptr)->kind) {
11598
267
    case ZEND_AST_CALL:
11599
267
      args_ast = &(*ast_ptr)->child[1];
11600
267
      break;
11601
46
    case ZEND_AST_STATIC_CALL:
11602
46
      args_ast = &(*ast_ptr)->child[2];
11603
46
      break;
11604
0
    EMPTY_SWITCH_DEFAULT_CASE();
11605
313
  }
11606
313
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11607
8
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11608
8
  }
11609
305
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11610
11611
305
  switch ((*ast_ptr)->kind) {
11612
260
    case ZEND_AST_CALL: {
11613
260
      zend_ast *name_ast = (*ast_ptr)->child[0];
11614
260
      if (name_ast->kind != ZEND_AST_ZVAL) {
11615
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11616
1
      }
11617
259
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11618
259
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11619
1
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11620
1
      }
11621
259
      bool is_fully_qualified;
11622
258
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11623
258
      zval_ptr_dtor_nogc(name_ast_zv);
11624
258
      ZVAL_STR(name_ast_zv, name);
11625
258
      if (is_fully_qualified) {
11626
45
        name_ast->attr = ZEND_NAME_FQ;
11627
45
      }
11628
258
      break;
11629
259
    }
11630
45
    case ZEND_AST_STATIC_CALL: {
11631
45
      zend_ast *class_ast = (*ast_ptr)->child[0];
11632
45
      zend_compile_const_expr_class_reference(class_ast);
11633
45
      zend_ast *method_ast = (*ast_ptr)->child[1];
11634
45
      if (method_ast->kind != ZEND_AST_ZVAL) {
11635
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11636
1
      }
11637
44
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11638
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11639
0
      }
11640
44
      break;
11641
44
    }
11642
44
    EMPTY_SWITCH_DEFAULT_CASE();
11643
305
  }
11644
305
}
11645
11646
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11647
1.96k
{
11648
1.96k
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11649
1.96k
  bool uses_named_args = false;
11650
2.90k
  for (uint32_t i = 0; i < list->children; i++) {
11651
947
    const zend_ast *arg = list->child[i];
11652
947
    if (arg->kind == ZEND_AST_UNPACK) {
11653
1
      zend_error_noreturn(E_COMPILE_ERROR,
11654
1
        "Argument unpacking in constant expressions is not supported");
11655
1
    }
11656
946
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11657
31
      uses_named_args = true;
11658
915
    } else if (uses_named_args) {
11659
1
      zend_error_noreturn(E_COMPILE_ERROR,
11660
1
        "Cannot use positional argument after named argument");
11661
1
    }
11662
946
  }
11663
1.96k
  if (uses_named_args) {
11664
29
    list->attr = 1;
11665
29
  }
11666
1.96k
}
11667
11668
typedef struct {
11669
  /* Whether the value of this expression may differ on each evaluation. */
11670
  bool allow_dynamic;
11671
} const_expr_context;
11672
11673
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11674
211k
{
11675
211k
  const const_expr_context *ctx = context;
11676
211k
  zend_ast *ast = *ast_ptr;
11677
211k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11678
169k
    return;
11679
169k
  }
11680
11681
42.1k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11682
24
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11683
24
  }
11684
11685
42.0k
  switch (ast->kind) {
11686
1.72k
    case ZEND_AST_CLASS_CONST:
11687
1.72k
      zend_compile_const_expr_class_const(ast_ptr);
11688
1.72k
      break;
11689
6
    case ZEND_AST_CLASS_NAME:
11690
6
      zend_compile_const_expr_class_name(ast_ptr);
11691
6
      break;
11692
21.0k
    case ZEND_AST_CONST:
11693
21.0k
      zend_compile_const_expr_const(ast_ptr);
11694
21.0k
      break;
11695
312
    case ZEND_AST_MAGIC_CONST:
11696
312
      zend_compile_const_expr_magic_const(ast_ptr);
11697
312
      break;
11698
298
    case ZEND_AST_CAST:
11699
298
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11700
2
        zend_error_noreturn(E_COMPILE_ERROR,
11701
2
          "Object casts are not supported in this context");
11702
2
      }
11703
296
      break;
11704
1.97k
    case ZEND_AST_NEW:
11705
1.97k
      if (!ctx->allow_dynamic) {
11706
1
        zend_error_noreturn(E_COMPILE_ERROR,
11707
1
          "New expressions are not supported in this context");
11708
1
      }
11709
1.97k
      zend_compile_const_expr_new(ast_ptr);
11710
1.97k
      break;
11711
1.96k
    case ZEND_AST_ARG_LIST:
11712
1.96k
      zend_compile_const_expr_args(ast_ptr);
11713
1.96k
      break;
11714
98
    case ZEND_AST_CLOSURE:
11715
98
      zend_compile_const_expr_closure(ast_ptr);
11716
      /* Return, because we do not want to traverse the children. */
11717
98
      return;
11718
267
    case ZEND_AST_CALL:
11719
313
    case ZEND_AST_STATIC_CALL:
11720
313
      zend_compile_const_expr_fcc(ast_ptr);
11721
313
      break;
11722
42.0k
  }
11723
11724
41.9k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11725
41.9k
}
11726
/* }}} */
11727
11728
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11729
153k
{
11730
153k
  const_expr_context context;
11731
153k
  context.allow_dynamic = allow_dynamic;
11732
11733
153k
  zend_eval_const_expr(ast_ptr);
11734
153k
  zend_compile_const_expr(ast_ptr, &context);
11735
153k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11736
    /* Replace with compiled AST zval representation. */
11737
18.1k
    zval ast_zv;
11738
18.1k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11739
18.1k
    zend_ast_destroy(*ast_ptr);
11740
18.1k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11741
18.1k
  }
11742
153k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11743
153k
}
11744
/* }}} */
11745
11746
/* Same as compile_stmt, but with early binding */
11747
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11748
228k
{
11749
228k
  if (!ast) {
11750
14.1k
    return;
11751
14.1k
  }
11752
11753
213k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11754
33.0k
    const zend_ast_list *list = zend_ast_get_list(ast);
11755
33.0k
    uint32_t i;
11756
232k
    for (i = 0; i < list->children; ++i) {
11757
199k
      zend_compile_top_stmt(list->child[i]);
11758
199k
    }
11759
33.0k
    return;
11760
33.0k
  }
11761
11762
180k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11763
776
    CG(zend_lineno) = ast->lineno;
11764
776
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11765
776
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11766
180k
  } else if (ast->kind == ZEND_AST_CLASS) {
11767
20.5k
    CG(zend_lineno) = ast->lineno;
11768
20.5k
    zend_compile_class_decl(NULL, ast, true);
11769
20.5k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11770
159k
  } else {
11771
159k
    zend_compile_stmt(ast);
11772
159k
  }
11773
180k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11774
173k
    zend_verify_namespace();
11775
173k
  }
11776
180k
}
11777
/* }}} */
11778
11779
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11780
9.22M
{
11781
9.22M
  if (!ast) {
11782
681k
    return;
11783
681k
  }
11784
11785
8.54M
  CG(zend_lineno) = ast->lineno;
11786
11787
8.54M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11788
0
    zend_do_extended_stmt(NULL);
11789
0
  }
11790
11791
8.54M
  switch (ast->kind) {
11792
2.01M
    case ZEND_AST_STMT_LIST:
11793
2.01M
      zend_compile_stmt_list(ast);
11794
2.01M
      break;
11795
2.02k
    case ZEND_AST_GLOBAL:
11796
2.02k
      zend_compile_global_var(ast);
11797
2.02k
      break;
11798
5.74k
    case ZEND_AST_STATIC:
11799
5.74k
      zend_compile_static_var(ast);
11800
5.74k
      break;
11801
3.81k
    case ZEND_AST_UNSET:
11802
3.81k
      zend_compile_unset(ast);
11803
3.81k
      break;
11804
40.7k
    case ZEND_AST_RETURN:
11805
40.7k
      zend_compile_return(ast);
11806
40.7k
      break;
11807
2.17M
    case ZEND_AST_ECHO:
11808
2.17M
      zend_compile_echo(ast);
11809
2.17M
      break;
11810
253
    case ZEND_AST_BREAK:
11811
1.05k
    case ZEND_AST_CONTINUE:
11812
1.05k
      zend_compile_break_continue(ast);
11813
1.05k
      break;
11814
1.75k
    case ZEND_AST_GOTO:
11815
1.75k
      zend_compile_goto(ast);
11816
1.75k
      break;
11817
7.43k
    case ZEND_AST_LABEL:
11818
7.43k
      zend_compile_label(ast);
11819
7.43k
      break;
11820
6.66k
    case ZEND_AST_WHILE:
11821
6.66k
      zend_compile_while(ast);
11822
6.66k
      break;
11823
1.14k
    case ZEND_AST_DO_WHILE:
11824
1.14k
      zend_compile_do_while(ast);
11825
1.14k
      break;
11826
5.93k
    case ZEND_AST_FOR:
11827
5.93k
      zend_compile_for(ast);
11828
5.93k
      break;
11829
7.18k
    case ZEND_AST_FOREACH:
11830
7.18k
      zend_compile_foreach(ast);
11831
7.18k
      break;
11832
42.5k
    case ZEND_AST_IF:
11833
42.5k
      zend_compile_if(ast);
11834
42.5k
      break;
11835
23.4k
    case ZEND_AST_SWITCH:
11836
23.4k
      zend_compile_switch(ast);
11837
23.4k
      break;
11838
9.41k
    case ZEND_AST_TRY:
11839
9.41k
      zend_compile_try(ast);
11840
9.41k
      break;
11841
6.66k
    case ZEND_AST_DECLARE:
11842
6.66k
      zend_compile_declare(ast);
11843
6.66k
      break;
11844
38.4k
    case ZEND_AST_FUNC_DECL:
11845
66.5k
    case ZEND_AST_METHOD:
11846
66.5k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11847
66.5k
      break;
11848
1.23k
    case ZEND_AST_ENUM_CASE:
11849
1.23k
      zend_compile_enum_case(ast);
11850
1.23k
      break;
11851
33.9k
    case ZEND_AST_PROP_GROUP:
11852
33.9k
      zend_compile_prop_group(ast);
11853
33.9k
      break;
11854
10.7k
    case ZEND_AST_CLASS_CONST_GROUP:
11855
10.7k
      zend_compile_class_const_group(ast);
11856
10.7k
      break;
11857
48.3k
    case ZEND_AST_USE_TRAIT:
11858
48.3k
      zend_compile_use_trait(ast);
11859
48.3k
      break;
11860
122k
    case ZEND_AST_CLASS:
11861
122k
      zend_compile_class_decl(NULL, ast, false);
11862
122k
      break;
11863
269
    case ZEND_AST_GROUP_USE:
11864
269
      zend_compile_group_use(ast);
11865
269
      break;
11866
1.26k
    case ZEND_AST_USE:
11867
1.26k
      zend_compile_use(ast);
11868
1.26k
      break;
11869
2.51k
    case ZEND_AST_CONST_DECL:
11870
2.51k
      zend_compile_const_decl(ast);
11871
2.51k
      break;
11872
4.65k
    case ZEND_AST_NAMESPACE:
11873
4.65k
      zend_compile_namespace(ast);
11874
4.65k
      break;
11875
14
    case ZEND_AST_HALT_COMPILER:
11876
14
      zend_compile_halt_compiler(ast);
11877
14
      break;
11878
783
    case ZEND_AST_THROW:
11879
783
      zend_compile_expr(NULL, ast);
11880
783
      break;
11881
603
    case ZEND_AST_CAST_VOID:
11882
603
      zend_compile_void_cast(NULL, ast);
11883
603
      break;
11884
3.89M
    default:
11885
3.89M
    {
11886
3.89M
      znode result;
11887
3.89M
      zend_compile_expr(&result, ast);
11888
3.89M
      zend_do_free(&result);
11889
3.89M
    }
11890
8.54M
  }
11891
11892
8.53M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11893
136k
    zend_emit_tick();
11894
136k
  }
11895
8.53M
}
11896
/* }}} */
11897
11898
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11899
45.5M
{
11900
  /* CG(zend_lineno) = ast->lineno; */
11901
45.5M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11902
11903
45.5M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11904
230k
    zend_compile_memoized_expr(result, ast);
11905
230k
    return;
11906
230k
  }
11907
11908
45.3M
  switch (ast->kind) {
11909
3.29M
    case ZEND_AST_ZVAL:
11910
3.29M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11911
3.29M
      result->op_type = IS_CONST;
11912
3.29M
      return;
11913
438k
    case ZEND_AST_ZNODE:
11914
438k
      *result = *zend_ast_get_znode(ast);
11915
438k
      return;
11916
247k
    case ZEND_AST_VAR:
11917
287k
    case ZEND_AST_DIM:
11918
309k
    case ZEND_AST_PROP:
11919
321k
    case ZEND_AST_NULLSAFE_PROP:
11920
326k
    case ZEND_AST_STATIC_PROP:
11921
4.46M
    case ZEND_AST_CALL:
11922
4.48M
    case ZEND_AST_METHOD_CALL:
11923
4.49M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11924
4.52M
    case ZEND_AST_STATIC_CALL:
11925
4.52M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11926
4.52M
      zend_compile_var(result, ast, BP_VAR_R, false);
11927
4.52M
      return;
11928
80.6k
    case ZEND_AST_ASSIGN:
11929
80.6k
      zend_compile_assign(result, ast);
11930
80.6k
      return;
11931
5.84k
    case ZEND_AST_ASSIGN_REF:
11932
5.84k
      zend_compile_assign_ref(result, ast);
11933
5.84k
      return;
11934
27.6k
    case ZEND_AST_NEW:
11935
27.6k
      zend_compile_new(result, ast);
11936
27.6k
      return;
11937
7.08k
    case ZEND_AST_ASSIGN_OP:
11938
7.08k
      zend_compile_compound_assign(result, ast);
11939
7.08k
      return;
11940
2.58M
    case ZEND_AST_BINARY_OP:
11941
2.58M
      zend_compile_binary_op(result, ast);
11942
2.58M
      return;
11943
315k
    case ZEND_AST_GREATER:
11944
393k
    case ZEND_AST_GREATER_EQUAL:
11945
393k
      zend_compile_greater(result, ast);
11946
393k
      return;
11947
1.62M
    case ZEND_AST_UNARY_OP:
11948
1.62M
      zend_compile_unary_op(result, ast);
11949
1.62M
      return;
11950
19.5k
    case ZEND_AST_UNARY_PLUS:
11951
94.4k
    case ZEND_AST_UNARY_MINUS:
11952
94.4k
      zend_compile_unary_pm(result, ast);
11953
94.4k
      return;
11954
8.67k
    case ZEND_AST_AND:
11955
11.1k
    case ZEND_AST_OR:
11956
11.1k
      zend_compile_short_circuiting(result, ast);
11957
11.1k
      return;
11958
728
    case ZEND_AST_POST_INC:
11959
2.16k
    case ZEND_AST_POST_DEC:
11960
2.16k
      zend_compile_post_incdec(result, ast);
11961
2.16k
      return;
11962
665
    case ZEND_AST_PRE_INC:
11963
1.44k
    case ZEND_AST_PRE_DEC:
11964
1.44k
      zend_compile_pre_incdec(result, ast);
11965
1.44k
      return;
11966
1.26k
    case ZEND_AST_CAST:
11967
1.26k
      zend_compile_cast(result, ast);
11968
1.26k
      return;
11969
7.62k
    case ZEND_AST_CONDITIONAL:
11970
7.62k
      zend_compile_conditional(result, ast);
11971
7.62k
      return;
11972
2.14M
    case ZEND_AST_COALESCE:
11973
2.14M
      zend_compile_coalesce(result, ast);
11974
2.14M
      return;
11975
34.9k
    case ZEND_AST_ASSIGN_COALESCE:
11976
34.9k
      zend_compile_assign_coalesce(result, ast);
11977
34.9k
      return;
11978
1.35k
    case ZEND_AST_PRINT:
11979
1.35k
      zend_compile_print(result, ast);
11980
1.35k
      return;
11981
47.0k
    case ZEND_AST_YIELD:
11982
47.0k
      zend_compile_yield(result, ast);
11983
47.0k
      return;
11984
2.09k
    case ZEND_AST_YIELD_FROM:
11985
2.09k
      zend_compile_yield_from(result, ast);
11986
2.09k
      return;
11987
566
    case ZEND_AST_INSTANCEOF:
11988
566
      zend_compile_instanceof(result, ast);
11989
566
      return;
11990
1.23k
    case ZEND_AST_INCLUDE_OR_EVAL:
11991
1.23k
      zend_compile_include_or_eval(result, ast);
11992
1.23k
      return;
11993
10.1k
    case ZEND_AST_ISSET:
11994
10.5k
    case ZEND_AST_EMPTY:
11995
10.5k
      zend_compile_isset_or_empty(result, ast);
11996
10.5k
      return;
11997
17.3M
    case ZEND_AST_SILENCE:
11998
17.3M
      zend_compile_silence(result, ast);
11999
17.3M
      return;
12000
31.6k
    case ZEND_AST_SHELL_EXEC:
12001
31.6k
      zend_compile_shell_exec(result, ast);
12002
31.6k
      return;
12003
111k
    case ZEND_AST_ARRAY:
12004
111k
      zend_compile_array(result, ast);
12005
111k
      return;
12006
10.4M
    case ZEND_AST_CONST:
12007
10.4M
      zend_compile_const(result, ast);
12008
10.4M
      return;
12009
114k
    case ZEND_AST_CLASS_CONST:
12010
114k
      zend_compile_class_const(result, ast);
12011
114k
      return;
12012
2.07k
    case ZEND_AST_CLASS_NAME:
12013
2.07k
      zend_compile_class_name(result, ast);
12014
2.07k
      return;
12015
18.3k
    case ZEND_AST_ENCAPS_LIST:
12016
18.3k
      zend_compile_encaps_list(result, ast);
12017
18.3k
      return;
12018
16.7k
    case ZEND_AST_MAGIC_CONST:
12019
16.7k
      zend_compile_magic_const(result, ast);
12020
16.7k
      return;
12021
1.65M
    case ZEND_AST_CLOSURE:
12022
1.68M
    case ZEND_AST_ARROW_FUNC:
12023
1.68M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12024
1.68M
      return;
12025
1.69k
    case ZEND_AST_THROW:
12026
1.69k
      zend_compile_throw(result, ast);
12027
1.69k
      return;
12028
2.88k
    case ZEND_AST_MATCH:
12029
2.88k
      zend_compile_match(result, ast);
12030
2.88k
      return;
12031
217k
    case ZEND_AST_PIPE:
12032
217k
      zend_compile_pipe(result, ast);
12033
217k
      return;
12034
0
    default:
12035
0
      ZEND_ASSERT(0 /* not supported */);
12036
45.3M
  }
12037
45.3M
}
12038
/* }}} */
12039
12040
static void zend_compile_expr(znode *result, zend_ast *ast)
12041
45.5M
{
12042
45.5M
  zend_check_stack_limit();
12043
12044
45.5M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12045
45.5M
  zend_compile_expr_inner(result, ast);
12046
45.5M
  zend_short_circuiting_commit(checkpoint, result, ast);
12047
45.5M
}
12048
12049
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12050
7.02M
{
12051
7.02M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12052
12053
7.02M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12054
106k
    switch (ast->kind) {
12055
5.97k
      case ZEND_AST_CALL:
12056
34.7k
      case ZEND_AST_METHOD_CALL:
12057
34.7k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12058
36.4k
      case ZEND_AST_STATIC_CALL:
12059
36.4k
        zend_compile_memoized_expr(result, ast);
12060
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12061
36.4k
        return NULL;
12062
106k
    }
12063
106k
  }
12064
12065
6.99M
  switch (ast->kind) {
12066
260k
    case ZEND_AST_VAR:
12067
260k
      return zend_compile_simple_var(result, ast, type, false);
12068
126k
    case ZEND_AST_DIM:
12069
126k
      return zend_compile_dim(result, ast, type, by_ref);
12070
24.6k
    case ZEND_AST_PROP:
12071
37.6k
    case ZEND_AST_NULLSAFE_PROP:
12072
37.6k
      return zend_compile_prop(result, ast, type, by_ref);
12073
6.85k
    case ZEND_AST_STATIC_PROP:
12074
6.85k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12075
4.31M
    case ZEND_AST_CALL:
12076
4.31M
      zend_compile_call(result, ast, type);
12077
4.31M
      return NULL;
12078
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12079
0
      zend_compile_parent_property_hook_call(result, ast, type);
12080
0
      return NULL;
12081
26.9k
    case ZEND_AST_METHOD_CALL:
12082
30.9k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12083
30.9k
      zend_compile_method_call(result, ast, type);
12084
30.9k
      return NULL;
12085
33.2k
    case ZEND_AST_STATIC_CALL:
12086
33.2k
      zend_compile_static_call(result, ast, type);
12087
33.2k
      return NULL;
12088
2.03k
    case ZEND_AST_ZNODE:
12089
2.03k
      *result = *zend_ast_get_znode(ast);
12090
2.03k
      return NULL;
12091
2.18M
    default:
12092
2.18M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12093
107
        zend_error_noreturn(E_COMPILE_ERROR,
12094
107
          "Cannot use temporary expression in write context");
12095
107
      }
12096
12097
2.18M
      zend_compile_expr(result, ast);
12098
2.18M
      return NULL;
12099
6.99M
  }
12100
6.99M
}
12101
12102
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12103
7.02M
{
12104
7.02M
  zend_check_stack_limit();
12105
12106
7.02M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12107
7.02M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12108
7.02M
  zend_short_circuiting_commit(checkpoint, result, ast);
12109
7.02M
  return opcode;
12110
7.02M
}
12111
12112
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12113
464k
{
12114
464k
  zend_check_stack_limit();
12115
12116
464k
  switch (ast->kind) {
12117
137k
    case ZEND_AST_VAR:
12118
137k
      return zend_compile_simple_var(result, ast, type, true);
12119
197k
    case ZEND_AST_DIM:
12120
197k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12121
16.4k
    case ZEND_AST_PROP:
12122
21.6k
    case ZEND_AST_NULLSAFE_PROP:
12123
21.6k
    {
12124
21.6k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12125
21.6k
      if (by_ref) {
12126
358
        opline->extended_value |= ZEND_FETCH_REF;
12127
358
      }
12128
21.6k
      return opline;
12129
16.4k
    }
12130
5.64k
    case ZEND_AST_STATIC_PROP:
12131
5.64k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12132
101k
    default:
12133
101k
      return zend_compile_var(result, ast, type, false);
12134
464k
  }
12135
464k
}
12136
/* }}} */
12137
12138
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12139
2.51k
{
12140
  /* NAN warns when casting */
12141
2.51k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12142
0
    return false;
12143
0
  }
12144
2.51k
  switch (type) {
12145
67
    case _IS_BOOL:
12146
67
      ZVAL_BOOL(result, zend_is_true(op1));
12147
67
      return true;
12148
408
    case IS_LONG:
12149
408
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12150
21
        return false;
12151
21
      }
12152
387
      ZVAL_LONG(result, zval_get_long(op1));
12153
387
      return true;
12154
890
    case IS_DOUBLE:
12155
890
      ZVAL_DOUBLE(result, zval_get_double(op1));
12156
890
      return true;
12157
870
    case IS_STRING:
12158
      /* Conversion from double to string takes into account run-time
12159
         'precision' setting and cannot be evaluated at compile-time */
12160
870
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12161
166
        ZVAL_STR(result, zval_get_string(op1));
12162
166
        return true;
12163
166
      }
12164
704
      break;
12165
704
    case IS_ARRAY:
12166
168
      ZVAL_COPY(result, op1);
12167
168
      convert_to_array(result);
12168
168
      return true;
12169
2.51k
  }
12170
813
  return false;
12171
2.51k
}
12172
12173
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12174
11.8M
{
12175
11.8M
  zend_ast *ast = *ast_ptr;
12176
11.8M
  zval result;
12177
12178
11.8M
  if (!ast) {
12179
2.89M
    return;
12180
2.89M
  }
12181
12182
8.97M
  zend_check_stack_limit();
12183
12184
8.97M
  switch (ast->kind) {
12185
105k
    case ZEND_AST_BINARY_OP:
12186
105k
      zend_eval_const_expr(&ast->child[0]);
12187
105k
      zend_eval_const_expr(&ast->child[1]);
12188
105k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12189
59.4k
        return;
12190
59.4k
      }
12191
12192
45.9k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12193
45.9k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12194
45.9k
      ) {
12195
32.8k
        return;
12196
32.8k
      }
12197
13.1k
      break;
12198
13.1k
    case ZEND_AST_GREATER:
12199
6.04k
    case ZEND_AST_GREATER_EQUAL:
12200
6.04k
      zend_eval_const_expr(&ast->child[0]);
12201
6.04k
      zend_eval_const_expr(&ast->child[1]);
12202
6.04k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12203
4.80k
        return;
12204
4.80k
      }
12205
12206
1.23k
      zend_ct_eval_greater(&result, ast->kind,
12207
1.23k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12208
1.23k
      break;
12209
4.65k
    case ZEND_AST_AND:
12210
5.91k
    case ZEND_AST_OR:
12211
5.91k
    {
12212
5.91k
      bool child0_is_true, child1_is_true;
12213
5.91k
      zend_eval_const_expr(&ast->child[0]);
12214
5.91k
      zend_eval_const_expr(&ast->child[1]);
12215
5.91k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12216
3.47k
        return;
12217
3.47k
      }
12218
12219
2.44k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12220
2.44k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12221
221
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12222
221
        break;
12223
221
      }
12224
12225
2.22k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12226
831
        return;
12227
831
      }
12228
12229
1.39k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12230
1.39k
      if (ast->kind == ZEND_AST_OR) {
12231
437
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12232
953
      } else {
12233
953
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12234
953
      }
12235
1.39k
      break;
12236
2.22k
    }
12237
6.41k
    case ZEND_AST_UNARY_OP:
12238
6.41k
      zend_eval_const_expr(&ast->child[0]);
12239
6.41k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12240
2.24k
        return;
12241
2.24k
      }
12242
12243
4.17k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12244
567
        return;
12245
567
      }
12246
3.60k
      break;
12247
5.10k
    case ZEND_AST_UNARY_PLUS:
12248
16.3k
    case ZEND_AST_UNARY_MINUS:
12249
16.3k
      zend_eval_const_expr(&ast->child[0]);
12250
16.3k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12251
6.04k
        return;
12252
6.04k
      }
12253
12254
10.2k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12255
8.07k
        return;
12256
8.07k
      }
12257
2.22k
      break;
12258
22.2k
    case ZEND_AST_COALESCE:
12259
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12260
22.2k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12261
359
        ast->child[0]->attr |= ZEND_DIM_IS;
12262
359
      }
12263
22.2k
      zend_eval_const_expr(&ast->child[0]);
12264
12265
22.2k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12266
        /* ensure everything was compile-time evaluated at least once */
12267
22.1k
        zend_eval_const_expr(&ast->child[1]);
12268
22.1k
        return;
12269
22.1k
      }
12270
12271
124
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12272
77
        zend_eval_const_expr(&ast->child[1]);
12273
77
        *ast_ptr = ast->child[1];
12274
77
        ast->child[1] = NULL;
12275
77
        zend_ast_destroy(ast);
12276
77
      } else {
12277
47
        *ast_ptr = ast->child[0];
12278
47
        ast->child[0] = NULL;
12279
47
        zend_ast_destroy(ast);
12280
47
      }
12281
124
      return;
12282
2.22k
    case ZEND_AST_CONDITIONAL:
12283
2.22k
    {
12284
2.22k
      zend_ast **child, *child_ast;
12285
2.22k
      zend_eval_const_expr(&ast->child[0]);
12286
2.22k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12287
        /* ensure everything was compile-time evaluated at least once */
12288
2.11k
        if (ast->child[1]) {
12289
1.61k
          zend_eval_const_expr(&ast->child[1]);
12290
1.61k
        }
12291
2.11k
        zend_eval_const_expr(&ast->child[2]);
12292
2.11k
        return;
12293
2.11k
      }
12294
12295
109
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12296
109
      if (*child == NULL) {
12297
72
        child--;
12298
72
      }
12299
109
      child_ast = *child;
12300
109
      *child = NULL;
12301
109
      zend_ast_destroy(ast);
12302
109
      *ast_ptr = child_ast;
12303
109
      zend_eval_const_expr(ast_ptr);
12304
109
      return;
12305
2.22k
    }
12306
1.33M
    case ZEND_AST_DIM:
12307
1.33M
    {
12308
      /* constant expression should be always read context ... */
12309
1.33M
      const zval *container, *dim;
12310
12311
1.33M
      if (ast->child[1] == NULL) {
12312
32
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12313
32
      }
12314
12315
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12316
1.33M
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12317
1.13k
        ast->child[0]->attr |= ZEND_DIM_IS;
12318
1.13k
      }
12319
12320
1.33M
      zend_eval_const_expr(&ast->child[0]);
12321
1.33M
      zend_eval_const_expr(&ast->child[1]);
12322
1.33M
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12323
1.31M
        return;
12324
1.31M
      }
12325
12326
15.0k
      container = zend_ast_get_zval(ast->child[0]);
12327
15.0k
      dim = zend_ast_get_zval(ast->child[1]);
12328
12329
15.0k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12330
9.88k
        zval *el;
12331
9.88k
        if (Z_TYPE_P(dim) == IS_LONG) {
12332
1.26k
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12333
1.26k
          if (el) {
12334
864
            ZVAL_COPY(&result, el);
12335
864
          } else {
12336
396
            return;
12337
396
          }
12338
8.62k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12339
7.96k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12340
7.96k
          if (el) {
12341
351
            ZVAL_COPY(&result, el);
12342
7.61k
          } else {
12343
7.61k
            return;
12344
7.61k
          }
12345
7.96k
        } else {
12346
656
          return; /* warning... handle at runtime */
12347
656
        }
12348
9.88k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12349
4.86k
        zend_long offset;
12350
4.86k
        uint8_t c;
12351
4.86k
        if (Z_TYPE_P(dim) == IS_LONG) {
12352
3.03k
          offset = Z_LVAL_P(dim);
12353
3.03k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12354
1.64k
          return;
12355
1.64k
        }
12356
3.21k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12357
2.86k
          return;
12358
2.86k
        }
12359
346
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12360
346
        ZVAL_CHAR(&result, c);
12361
346
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12362
18
        return; /* warning... handle at runtime */
12363
312
      } else {
12364
312
        return;
12365
312
      }
12366
1.56k
      break;
12367
15.0k
    }
12368
3.41M
    case ZEND_AST_ARRAY:
12369
3.41M
      if (!zend_try_ct_eval_array(&result, ast)) {
12370
3.37M
        return;
12371
3.37M
      }
12372
42.0k
      break;
12373
42.0k
    case ZEND_AST_MAGIC_CONST:
12374
1.33k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12375
312
        return;
12376
312
      }
12377
1.01k
      break;
12378
137k
    case ZEND_AST_CONST:
12379
137k
    {
12380
137k
      zend_ast *name_ast = ast->child[0];
12381
137k
      bool is_fully_qualified;
12382
137k
      zend_string *resolved_name = zend_resolve_const_name(
12383
137k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12384
12385
137k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12386
135k
        zend_string_release_ex(resolved_name, 0);
12387
135k
        return;
12388
135k
      }
12389
12390
1.67k
      zend_string_release_ex(resolved_name, 0);
12391
1.67k
      break;
12392
137k
    }
12393
627k
    case ZEND_AST_CLASS_CONST:
12394
627k
    {
12395
627k
      zend_ast *class_ast;
12396
627k
      zend_ast *name_ast;
12397
627k
      zend_string *resolved_name;
12398
12399
627k
      zend_eval_const_expr(&ast->child[0]);
12400
627k
      zend_eval_const_expr(&ast->child[1]);
12401
12402
627k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12403
627k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12404
1.40k
        return;
12405
1.40k
      }
12406
12407
625k
      class_ast = ast->child[0];
12408
625k
      name_ast = ast->child[1];
12409
12410
625k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12411
582k
        return;
12412
582k
      }
12413
12414
43.4k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12415
43.4k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12416
43.4k
        zend_string_release_ex(resolved_name, 0);
12417
43.4k
        return;
12418
43.4k
      }
12419
12420
6
      zend_string_release_ex(resolved_name, 0);
12421
6
      break;
12422
43.4k
    }
12423
2.60k
    case ZEND_AST_CLASS_NAME:
12424
2.60k
    {
12425
2.60k
      zend_ast *class_ast = ast->child[0];
12426
2.60k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12427
1.68k
        return;
12428
1.68k
      }
12429
923
      break;
12430
2.60k
    }
12431
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12432
    // special handling. It is required that all nodes that are part of a const expr
12433
    // are visited. Probably we should be distinguishing evaluation of const expr and
12434
    // normal exprs here.
12435
2.24k
    case ZEND_AST_ARG_LIST:
12436
2.24k
    {
12437
2.24k
      zend_ast_list *list = zend_ast_get_list(ast);
12438
3.29k
      for (uint32_t i = 0; i < list->children; i++) {
12439
1.04k
        zend_eval_const_expr(&list->child[i]);
12440
1.04k
      }
12441
2.24k
      return;
12442
2.60k
    }
12443
2.24k
    case ZEND_AST_NEW:
12444
2.24k
      zend_eval_const_expr(&ast->child[0]);
12445
2.24k
      zend_eval_const_expr(&ast->child[1]);
12446
2.24k
      return;
12447
39
    case ZEND_AST_NAMED_ARG:
12448
39
      zend_eval_const_expr(&ast->child[1]);
12449
39
      return;
12450
1.22k
    case ZEND_AST_CONST_ENUM_INIT:
12451
1.22k
      zend_eval_const_expr(&ast->child[2]);
12452
1.22k
      return;
12453
613
    case ZEND_AST_PROP:
12454
1.07k
    case ZEND_AST_NULLSAFE_PROP:
12455
1.07k
      zend_eval_const_expr(&ast->child[0]);
12456
1.07k
      zend_eval_const_expr(&ast->child[1]);
12457
1.07k
      return;
12458
16.1k
    case ZEND_AST_CAST:
12459
16.1k
      zend_eval_const_expr(&ast->child[0]);
12460
16.1k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12461
2.51k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12462
1.67k
        break;
12463
1.67k
      }
12464
14.4k
      return;
12465
3.26M
    default:
12466
3.26M
      return;
12467
8.97M
  }
12468
12469
70.2k
  zend_ast_destroy(ast);
12470
70.2k
  *ast_ptr = zend_ast_create_zval(&result);
12471
70.2k
}
12472
/* }}} */