Coverage Report

Created: 2025-12-14 06:10

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
48.8M
#define SET_NODE(target, src) do { \
44
48.8M
    target ## _type = (src)->op_type; \
45
48.8M
    if ((src)->op_type == IS_CONST) { \
46
5.92M
      target.constant = zend_add_literal(&(src)->u.constant); \
47
42.9M
    } else { \
48
42.9M
      target = (src)->u.op; \
49
42.9M
    } \
50
48.8M
  } while (0)
51
52
36.4M
#define GET_NODE(target, src) do { \
53
36.4M
    (target)->op_type = src ## _type; \
54
36.4M
    if ((target)->op_type == IS_CONST) { \
55
571
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
36.4M
    } else { \
57
36.4M
      (target)->u.op = src; \
58
36.4M
    } \
59
36.4M
  } while (0)
60
61
76.7M
#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
13.3M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
13.3M
  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
13.3M
  zend_op_array *op_array = CG(active_op_array);
79
13.3M
  uint32_t ret = op_array->cache_size;
80
13.3M
  op_array->cache_size += count * sizeof(void*);
81
13.3M
  return ret;
82
13.3M
}
83
84
12.9M
static inline uint32_t zend_alloc_cache_slot(void) {
85
12.9M
  return zend_alloc_cache_slots(1);
86
12.9M
}
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
55.2M
{
121
55.2M
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
55.2M
}
125
#else /* ZEND_CHECK_STACK_LIMIT */
126
static void zend_check_stack_limit(void)
127
{
128
}
129
#endif /* ZEND_CHECK_STACK_LIMIT */
130
131
static void init_op(zend_op *op)
132
73.4M
{
133
73.4M
  MAKE_NOP(op);
134
73.4M
  op->extended_value = 0;
135
73.4M
  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
73.4M
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
12.3M
{
148
12.3M
  return CG(active_op_array)->last;
149
12.3M
}
150
151
static zend_op *get_next_op(void)
152
73.0M
{
153
73.0M
  zend_op_array *op_array = CG(active_op_array);
154
73.0M
  uint32_t next_op_num = op_array->last++;
155
73.0M
  zend_op *next_op;
156
157
73.0M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
618k
    CG(context).opcodes_size *= 4;
159
618k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
618k
  }
161
162
73.0M
  next_op = &(op_array->opcodes[next_op_num]);
163
164
73.0M
  init_op(next_op);
165
166
73.0M
  return next_op;
167
73.0M
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
88.7k
{
171
88.7k
  CG(context).last_brk_cont++;
172
88.7k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
88.7k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
88.7k
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
103k
{
178
103k
  zend_string *filename = CG(active_op_array)->filename;
179
103k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
103k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
103k
  return zend_new_interned_string(result);
182
103k
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
12.7M
{
187
12.7M
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
12.7M
  if (ns_separator != NULL) {
189
12.2M
    *result = ns_separator + 1;
190
12.2M
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
12.2M
    return 1;
192
12.2M
  }
193
194
484k
  return 0;
195
12.7M
}
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
488k
{
227
488k
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
488k
  const char *uqname = ZSTR_VAL(name);
230
488k
  size_t uqname_len = ZSTR_LEN(name);
231
488k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
8.78M
  for (; reserved->name; ++reserved) {
234
8.29M
    if (uqname_len == reserved->len
235
269k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
8.29M
    ) {
237
134
      return 1;
238
134
    }
239
8.29M
  }
240
241
487k
  return 0;
242
488k
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
486k
{
247
486k
  if (zend_is_reserved_class_name(name)) {
248
101
    zend_error_noreturn(E_COMPILE_ERROR,
249
101
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
101
  }
251
486k
  if (zend_string_equals_literal(name, "_")) {
252
931
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
931
  }
254
486k
}
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
552k
{
295
552k
  const builtin_type_info *info = &builtin_types[0];
296
297
6.52M
  for (; info->name; ++info) {
298
6.15M
    if (ZSTR_LEN(name) == info->name_len
299
362k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
6.15M
    ) {
301
182k
      return info->type;
302
182k
    }
303
6.15M
  }
304
305
369k
  return 0;
306
552k
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
369k
{
311
369k
  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
1.71M
  for (; info->name; ++info) {
316
1.39M
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
43.9k
      *correct_name = info->correct_name;
318
43.9k
      return 1;
319
43.9k
    }
320
1.39M
  }
321
322
325k
  return 0;
323
369k
}
324
/* }}} */
325
326
43.9k
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
43.9k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
43.9k
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
1.53M
{
333
1.53M
  *prev_context = CG(context);
334
1.53M
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
1.53M
  CG(context).op_array = op_array;
336
1.53M
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
1.53M
  CG(context).vars_size = 0;
338
1.53M
  CG(context).literals_size = 0;
339
1.53M
  CG(context).fast_call_var = -1;
340
1.53M
  CG(context).try_catch_offset = -1;
341
1.53M
  CG(context).current_brk_cont = -1;
342
1.53M
  CG(context).last_brk_cont = 0;
343
1.53M
  CG(context).has_assigned_to_http_response_header = false;
344
1.53M
  CG(context).brk_cont_array = NULL;
345
1.53M
  CG(context).labels = NULL;
346
1.53M
  CG(context).in_jmp_frameless_branch = false;
347
1.53M
  CG(context).active_property_info_name = NULL;
348
1.53M
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
1.53M
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
1.52M
{
354
1.52M
  if (CG(context).brk_cont_array) {
355
43.3k
    efree(CG(context).brk_cont_array);
356
43.3k
    CG(context).brk_cont_array = NULL;
357
43.3k
  }
358
1.52M
  if (CG(context).labels) {
359
3.92k
    zend_hash_destroy(CG(context).labels);
360
3.92k
    FREE_HASHTABLE(CG(context).labels);
361
3.92k
    CG(context).labels = NULL;
362
3.92k
  }
363
1.52M
  CG(context) = *prev_context;
364
1.52M
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
101k
{
369
101k
  if (FC(imports)) {
370
834
    zend_hash_destroy(FC(imports));
371
834
    efree(FC(imports));
372
834
    FC(imports) = NULL;
373
834
  }
374
375
101k
  if (FC(imports_function)) {
376
417
    zend_hash_destroy(FC(imports_function));
377
417
    efree(FC(imports_function));
378
417
    FC(imports_function) = NULL;
379
417
  }
380
381
101k
  if (FC(imports_const)) {
382
212
    zend_hash_destroy(FC(imports_const));
383
212
    efree(FC(imports_const));
384
212
    FC(imports_const) = NULL;
385
212
  }
386
387
101k
  zend_hash_clean(&FC(seen_symbols));
388
101k
}
389
/* }}} */
390
391
95.3k
static void zend_end_namespace(void) /* {{{ */ {
392
95.3k
  FC(in_namespace) = 0;
393
95.3k
  zend_reset_import_tables();
394
95.3k
  if (FC(current_namespace)) {
395
3.75k
    zend_string_release_ex(FC(current_namespace), 0);
396
3.75k
    FC(current_namespace) = NULL;
397
3.75k
  }
398
95.3k
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
100k
{
403
100k
  *prev_context = CG(file_context);
404
100k
  FC(imports) = NULL;
405
100k
  FC(imports_function) = NULL;
406
100k
  FC(imports_const) = NULL;
407
100k
  FC(current_namespace) = NULL;
408
100k
  FC(in_namespace) = 0;
409
100k
  FC(has_bracketed_namespaces) = 0;
410
100k
  FC(declarables).ticks = 0;
411
100k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
100k
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
93.4k
{
417
93.4k
  zend_end_namespace();
418
93.4k
  zend_hash_destroy(&FC(seen_symbols));
419
93.4k
  CG(file_context) = *prev_context;
420
93.4k
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
313k
{
425
313k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
313k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
313k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
313k
  CG(active_class_entry) = NULL;
429
313k
  CG(in_compilation) = 0;
430
313k
  CG(skip_shebang) = 0;
431
432
313k
  CG(encoding_declared) = 0;
433
313k
  CG(memoized_exprs) = NULL;
434
313k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
313k
}
436
/* }}} */
437
438
1.52M
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
1.52M
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
1.52M
  if (zv) {
441
1.44M
    Z_LVAL_P(zv) |= kind;
442
1.44M
  } else {
443
84.7k
    zval tmp;
444
84.7k
    ZVAL_LONG(&tmp, kind);
445
84.7k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
84.7k
  }
447
1.52M
}
448
449
2.33k
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
2.33k
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
2.33k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
2.33k
}
453
454
void init_compiler(void) /* {{{ */
455
306k
{
456
306k
  CG(arena) = zend_arena_create(64 * 1024);
457
306k
  CG(active_op_array) = NULL;
458
306k
  memset(&CG(context), 0, sizeof(CG(context)));
459
306k
  zend_init_compiler_data_structures();
460
306k
  zend_init_rsrc_list();
461
306k
  zend_stream_init();
462
306k
  CG(unclean_shutdown) = 0;
463
464
306k
  CG(delayed_variance_obligations) = NULL;
465
306k
  CG(delayed_autoloads) = NULL;
466
306k
  CG(unlinked_uses) = NULL;
467
306k
  CG(current_linking_class) = NULL;
468
306k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
313k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
313k
  zend_restore_compiled_filename(NULL);
475
476
313k
  zend_stack_destroy(&CG(loop_var_stack));
477
313k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
313k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
313k
  if (CG(delayed_variance_obligations)) {
481
266
    zend_hash_destroy(CG(delayed_variance_obligations));
482
266
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
266
    CG(delayed_variance_obligations) = NULL;
484
266
  }
485
313k
  if (CG(delayed_autoloads)) {
486
271
    zend_hash_destroy(CG(delayed_autoloads));
487
271
    FREE_HASHTABLE(CG(delayed_autoloads));
488
271
    CG(delayed_autoloads) = NULL;
489
271
  }
490
313k
  if (CG(unlinked_uses)) {
491
234
    zend_hash_destroy(CG(unlinked_uses));
492
234
    FREE_HASHTABLE(CG(unlinked_uses));
493
234
    CG(unlinked_uses) = NULL;
494
234
  }
495
313k
  CG(current_linking_class) = NULL;
496
313k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
168k
{
501
168k
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
168k
  return new_compiled_filename;
503
168k
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
663k
{
508
663k
  if (CG(compiled_filename)) {
509
168k
    zend_string_release(CG(compiled_filename));
510
168k
    CG(compiled_filename) = NULL;
511
168k
  }
512
663k
  CG(compiled_filename) = original_compiled_filename;
513
663k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
2.12M
{
518
2.12M
  return CG(compiled_filename);
519
2.12M
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
275k
{
524
275k
  return CG(zend_lineno);
525
275k
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
3.24M
{
530
3.24M
  return CG(in_compilation);
531
3.24M
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
36.7M
{
536
36.7M
  return (uint32_t)CG(active_op_array)->T++;
537
36.7M
}
538
/* }}} */
539
540
2.73M
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.73M
  zend_op_array *op_array = CG(active_op_array);
542
2.73M
  int i = 0;
543
2.73M
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
8.37M
  while (i < op_array->last_var) {
546
6.34M
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
703k
     && zend_string_equals(op_array->vars[i], name)) {
548
701k
      return EX_NUM_TO_VAR(i);
549
701k
    }
550
5.64M
    i++;
551
5.64M
  }
552
2.03M
  i = op_array->last_var;
553
2.03M
  op_array->last_var++;
554
2.03M
  if (op_array->last_var > CG(context).vars_size) {
555
1.42M
    CG(context).vars_size += 16; /* FIXME */
556
1.42M
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
1.42M
  }
558
559
2.03M
  op_array->vars[i] = zend_string_copy(name);
560
2.03M
  return EX_NUM_TO_VAR(i);
561
2.73M
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
43.5M
{
566
43.5M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
43.5M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
43.5M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
5.31M
    Z_TYPE_FLAGS_P(zv) = 0;
570
5.31M
  }
571
43.5M
  return Z_STR_P(zv);
572
43.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
42.9M
{
577
42.9M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
42.9M
  if (Z_TYPE_P(zv) == IS_STRING) {
579
40.8M
    zval_make_interned_string(zv);
580
40.8M
  }
581
42.9M
  ZVAL_COPY_VALUE(lit, zv);
582
42.9M
  Z_EXTRA_P(lit) = 0;
583
42.9M
}
584
/* }}} */
585
586
/* Is used while compiling a function, using the context to keep track
587
   of an approximate size to avoid to relocate to often.
588
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
589
static int zend_add_literal(zval *zv) /* {{{ */
590
42.9M
{
591
42.9M
  zend_op_array *op_array = CG(active_op_array);
592
42.9M
  uint32_t i = op_array->last_literal;
593
42.9M
  op_array->last_literal++;
594
42.9M
  if (i >= CG(context).literals_size) {
595
6.67M
    while (i >= CG(context).literals_size) {
596
3.33M
      CG(context).literals_size += 16; /* FIXME */
597
3.33M
    }
598
3.33M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
3.33M
  }
600
42.9M
  zend_insert_literal(op_array, zv, i);
601
42.9M
  return i;
602
42.9M
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
37.0M
{
607
37.0M
  int ret;
608
37.0M
  zval zv;
609
37.0M
  ZVAL_STR(&zv, *str);
610
37.0M
  ret = zend_add_literal(&zv);
611
37.0M
  *str = Z_STR(zv);
612
37.0M
  return ret;
613
37.0M
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
173k
{
618
  /* Original name */
619
173k
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
173k
  zend_string *lc_name = zend_string_tolower(name);
623
173k
  zend_add_literal_string(&lc_name);
624
625
173k
  return ret;
626
173k
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
3.36M
{
631
3.36M
  const char *unqualified_name;
632
3.36M
  size_t unqualified_name_len;
633
634
  /* Original name */
635
3.36M
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
3.36M
  zend_string *lc_name = zend_string_tolower(name);
639
3.36M
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
3.36M
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
3.36M
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
3.36M
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
3.36M
    zend_add_literal_string(&lc_name);
646
3.36M
  }
647
648
3.36M
  return ret;
649
3.36M
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
231k
{
654
  /* Original name */
655
231k
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
231k
  zend_string *lc_name = zend_string_tolower(name);
659
231k
  zend_add_literal_string(&lc_name);
660
661
231k
  return ret;
662
231k
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
8.68M
{
667
8.68M
  zend_string *tmp_name;
668
669
8.68M
  int ret = zend_add_literal_string(&name);
670
671
8.68M
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
8.68M
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
8.68M
  if (after_ns) {
674
8.46M
    after_ns += 1;
675
8.46M
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
8.46M
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
8.46M
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
8.46M
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
8.46M
    zend_add_literal_string(&tmp_name);
682
683
8.46M
    if (!unqualified) {
684
3.38k
      return ret;
685
3.38k
    }
686
8.46M
  } else {
687
216k
    after_ns = ZSTR_VAL(name);
688
216k
  }
689
690
  /* original unqualified constant name */
691
8.68M
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
8.68M
  zend_add_literal_string(&tmp_name);
693
694
8.68M
  return ret;
695
8.68M
}
696
/* }}} */
697
698
49.7k
#define LITERAL_STR(op, str) do { \
699
49.7k
    zval _c; \
700
49.7k
    ZVAL_STR(&_c, str); \
701
49.7k
    op.constant = zend_add_literal(&_c); \
702
49.7k
  } while (0)
703
704
void zend_stop_lexing(void)
705
67
{
706
67
  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
67
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
67
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
88.7k
{
716
88.7k
  zend_brk_cont_element *brk_cont_element;
717
88.7k
  int parent = CG(context).current_brk_cont;
718
88.7k
  zend_loop_var info = {0};
719
720
88.7k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
88.7k
  brk_cont_element = get_next_brk_cont_element();
722
88.7k
  brk_cont_element->parent = parent;
723
88.7k
  brk_cont_element->is_switch = is_switch;
724
725
88.7k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
34.6k
    uint32_t start = get_next_op_number();
727
728
34.6k
    info.opcode = free_opcode;
729
34.6k
    info.var_type = loop_var->op_type;
730
34.6k
    info.var_num = loop_var->u.op.var;
731
34.6k
    brk_cont_element->start = start;
732
54.0k
  } else {
733
54.0k
    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
54.0k
    brk_cont_element->start = -1;
737
54.0k
  }
738
739
88.7k
  zend_stack_push(&CG(loop_var_stack), &info);
740
88.7k
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
88.4k
{
745
88.4k
  uint32_t end = get_next_op_number();
746
88.4k
  zend_brk_cont_element *brk_cont_element
747
88.4k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
88.4k
  brk_cont_element->cont = cont_addr;
749
88.4k
  brk_cont_element->brk = end;
750
88.4k
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
88.4k
  zend_stack_del_top(&CG(loop_var_stack));
753
88.4k
}
754
/* }}} */
755
756
static void zend_do_free(znode *op1) /* {{{ */
757
3.73M
{
758
3.73M
  if (op1->op_type == IS_TMP_VAR) {
759
1.55M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
760
761
1.59M
    while (opline->opcode == ZEND_END_SILENCE ||
762
1.57M
           opline->opcode == ZEND_OP_DATA) {
763
39.7k
      opline--;
764
39.7k
    }
765
766
1.55M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
767
1.54M
      switch (opline->opcode) {
768
1.85k
        case ZEND_BOOL:
769
8.45k
        case ZEND_BOOL_NOT:
770
          /* boolean results don't have to be freed */
771
8.45k
          return;
772
85
        case ZEND_POST_INC_STATIC_PROP:
773
284
        case ZEND_POST_DEC_STATIC_PROP:
774
592
        case ZEND_POST_INC_OBJ:
775
716
        case ZEND_POST_DEC_OBJ:
776
5.40k
        case ZEND_POST_INC:
777
6.06k
        case ZEND_POST_DEC:
778
          /* convert $i++ to ++$i */
779
6.06k
          opline->opcode -= 2;
780
6.06k
          SET_UNUSED(opline->result);
781
6.06k
          return;
782
122k
        case ZEND_ASSIGN:
783
131k
        case ZEND_ASSIGN_DIM:
784
142k
        case ZEND_ASSIGN_OBJ:
785
144k
        case ZEND_ASSIGN_STATIC_PROP:
786
184k
        case ZEND_ASSIGN_OP:
787
186k
        case ZEND_ASSIGN_DIM_OP:
788
187k
        case ZEND_ASSIGN_OBJ_OP:
789
188k
        case ZEND_ASSIGN_STATIC_PROP_OP:
790
188k
        case ZEND_PRE_INC_STATIC_PROP:
791
188k
        case ZEND_PRE_DEC_STATIC_PROP:
792
188k
        case ZEND_PRE_INC_OBJ:
793
189k
        case ZEND_PRE_DEC_OBJ:
794
189k
        case ZEND_PRE_INC:
795
190k
        case ZEND_PRE_DEC:
796
190k
          SET_UNUSED(opline->result);
797
190k
          return;
798
1.54M
      }
799
1.54M
    }
800
801
1.34M
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
802
2.18M
  } else if (op1->op_type == IS_VAR) {
803
2.10M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
804
2.21M
    while (opline->opcode == ZEND_END_SILENCE ||
805
2.21M
        opline->opcode == ZEND_EXT_FCALL_END ||
806
2.21M
        opline->opcode == ZEND_OP_DATA) {
807
107k
      opline--;
808
107k
    }
809
2.10M
    if (opline->result_type == IS_VAR
810
2.08M
      && opline->result.var == op1->u.op.var) {
811
2.08M
      if (opline->opcode == ZEND_FETCH_THIS) {
812
0
        opline->opcode = ZEND_NOP;
813
0
      }
814
2.08M
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
815
1.49M
        SET_UNUSED(opline->result);
816
1.49M
      } 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
        // FIXME: We may actually look at the function signature to determine whether a free
820
        // is necessary.
821
587k
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
587k
      }
823
2.08M
    } else {
824
96.8k
      while (opline >= CG(active_op_array)->opcodes) {
825
96.8k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
826
94.1k
             opline->opcode == ZEND_FETCH_LIST_W ||
827
92.5k
             opline->opcode == ZEND_EXT_STMT) &&
828
4.28k
            opline->op1_type == IS_VAR &&
829
4.28k
            opline->op1.var == op1->u.op.var) {
830
3.73k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
831
3.73k
          return;
832
3.73k
        }
833
93.1k
        if (opline->result_type == IS_VAR
834
32.3k
          && opline->result.var == op1->u.op.var) {
835
19.9k
          if (opline->opcode == ZEND_NEW) {
836
19.9k
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
837
19.9k
          }
838
19.9k
          break;
839
19.9k
        }
840
73.2k
        opline--;
841
73.2k
      }
842
23.6k
    }
843
2.10M
  } else if (op1->op_type == IS_CONST) {
844
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
845
     * free the zend_array structure, so references to it from outside the op array
846
     * become invalid. GC would cause such a reference in the root buffer. */
847
75.4k
    zval_ptr_dtor_nogc(&op1->u.constant);
848
75.4k
  }
849
3.73M
}
850
/* }}} */
851
852
853
static const char *zend_modifier_token_to_string(uint32_t token)
854
113
{
855
113
  switch (token) {
856
7
    case T_PUBLIC:
857
7
      return "public";
858
7
    case T_PROTECTED:
859
7
      return "protected";
860
1
    case T_PRIVATE:
861
1
      return "private";
862
25
    case T_STATIC:
863
25
      return "static";
864
22
    case T_FINAL:
865
22
      return "final";
866
29
    case T_READONLY:
867
29
      return "readonly";
868
13
    case T_ABSTRACT:
869
13
      return "abstract";
870
1
    case T_PUBLIC_SET:
871
1
      return "public(set)";
872
1
    case T_PROTECTED_SET:
873
1
      return "protected(set)";
874
7
    case T_PRIVATE_SET:
875
7
      return "private(set)";
876
113
    EMPTY_SWITCH_DEFAULT_CASE()
877
113
  }
878
113
}
879
880
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
881
61.8k
{
882
61.8k
  switch (token) {
883
39.2k
    case T_PUBLIC:
884
39.2k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
885
39.2k
        return ZEND_ACC_PUBLIC;
886
39.2k
      }
887
7
      break;
888
2.53k
    case T_PROTECTED:
889
2.53k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
890
2.53k
        return ZEND_ACC_PROTECTED;
891
2.53k
      }
892
7
      break;
893
4.83k
    case T_PRIVATE:
894
4.83k
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
895
4.83k
        return ZEND_ACC_PRIVATE;
896
4.83k
      }
897
1
      break;
898
1.12k
    case T_READONLY:
899
1.12k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
900
1.10k
        return ZEND_ACC_READONLY;
901
1.10k
      }
902
20
      break;
903
1.13k
    case T_ABSTRACT:
904
1.13k
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
905
1.12k
        return ZEND_ACC_ABSTRACT;
906
1.12k
      }
907
3
      break;
908
1.67k
    case T_FINAL:
909
1.67k
      return ZEND_ACC_FINAL;
910
9.93k
    case T_STATIC:
911
9.93k
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
912
9.92k
        return ZEND_ACC_STATIC;
913
9.92k
      }
914
13
      break;
915
456
    case T_PUBLIC_SET:
916
456
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
917
455
        return ZEND_ACC_PUBLIC_SET;
918
455
      }
919
1
      break;
920
198
    case T_PROTECTED_SET:
921
198
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
922
197
        return ZEND_ACC_PROTECTED_SET;
923
197
      }
924
1
      break;
925
728
    case T_PRIVATE_SET:
926
728
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
927
721
        return ZEND_ACC_PRIVATE_SET;
928
721
      }
929
7
      break;
930
61.8k
  }
931
932
60
  char *member;
933
60
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
934
0
    member = "property";
935
60
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
936
17
    member = "method";
937
43
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
938
7
    member = "class constant";
939
36
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
940
8
    member = "parameter";
941
28
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
942
28
    member = "property hook";
943
28
  } else {
944
0
    ZEND_UNREACHABLE();
945
0
  }
946
947
60
  zend_throw_exception_ex(zend_ce_compile_error, 0,
948
60
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
949
60
  return 0;
950
60
}
951
952
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
953
52.0k
{
954
52.0k
  uint32_t flags = 0;
955
52.0k
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
956
957
112k
  for (uint32_t i = 0; i < modifier_list->children; i++) {
958
60.6k
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
959
60.6k
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
960
60.6k
    if (!new_flag) {
961
52
      return 0;
962
52
    }
963
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
964
60.6k
    bool duplicate_flag = (flags & new_flag);
965
60.6k
    flags = zend_add_member_modifier(flags, new_flag, target);
966
60.6k
    if (!flags) {
967
65
      return 0;
968
65
    }
969
60.5k
    if (duplicate_flag) {
970
53
      zend_throw_exception_ex(zend_ce_compile_error, 0,
971
53
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
972
53
      return 0;
973
53
    }
974
60.5k
  }
975
976
51.8k
  return flags;
977
52.0k
}
978
979
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
980
185
{
981
185
  uint32_t new_flags = flags | new_flag;
982
185
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
983
2
    zend_throw_exception(zend_ce_compile_error,
984
2
      "Multiple abstract modifiers are not allowed", 0);
985
2
    return 0;
986
2
  }
987
183
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
988
10
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
989
10
    return 0;
990
10
  }
991
173
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
992
8
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
993
8
    return 0;
994
8
  }
995
165
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
996
7
    zend_throw_exception(zend_ce_compile_error,
997
7
      "Cannot use the final modifier on an abstract class", 0);
998
7
    return 0;
999
7
  }
1000
158
  return new_flags;
1001
165
}
1002
/* }}} */
1003
1004
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1005
93
{
1006
93
  uint32_t new_flags = flags | new_flag;
1007
93
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1008
7
    zend_throw_exception(zend_ce_compile_error,
1009
7
      "Cannot use the abstract modifier on an anonymous class", 0);
1010
7
    return 0;
1011
7
  }
1012
86
  if (new_flag & ZEND_ACC_FINAL) {
1013
7
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1014
7
    return 0;
1015
7
  }
1016
79
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1017
7
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1018
7
    return 0;
1019
7
  }
1020
72
  return new_flags;
1021
79
}
1022
1023
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1024
60.6k
{
1025
60.6k
  uint32_t new_flags = flags | new_flag;
1026
60.6k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1027
36
    zend_throw_exception(zend_ce_compile_error,
1028
36
      "Multiple access type modifiers are not allowed", 0);
1029
36
    return 0;
1030
36
  }
1031
60.5k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1032
14
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1033
7
      zend_throw_exception(zend_ce_compile_error,
1034
7
        "Cannot use the final modifier on an abstract method", 0);
1035
7
      return 0;
1036
7
    }
1037
7
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1038
7
      zend_throw_exception(zend_ce_compile_error,
1039
7
        "Cannot use the final modifier on an abstract property", 0);
1040
7
      return 0;
1041
7
    }
1042
7
  }
1043
60.5k
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1044
31.6k
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1045
15
      zend_throw_exception(zend_ce_compile_error,
1046
15
        "Multiple access type modifiers are not allowed", 0);
1047
15
      return 0;
1048
15
    }
1049
31.6k
  }
1050
60.5k
  return new_flags;
1051
60.5k
}
1052
/* }}} */
1053
1054
21.1k
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1055
21.1k
  return zend_string_concat3(
1056
21.1k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1057
21.1k
    "::", sizeof("::") - 1,
1058
21.1k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1059
21.1k
}
1060
1061
15.1M
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1062
15.1M
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1063
15.1M
}
1064
1065
15.9M
static zend_string *zend_prefix_with_ns(zend_string *name) {
1066
15.9M
  if (FC(current_namespace)) {
1067
15.1M
    const zend_string *ns = FC(current_namespace);
1068
15.1M
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1069
15.1M
  } else {
1070
789k
    return zend_string_copy(name);
1071
789k
  }
1072
15.9M
}
1073
1074
static zend_string *zend_resolve_non_class_name(
1075
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1076
  bool case_sensitive, const HashTable *current_import_sub
1077
12.4M
) {
1078
12.4M
  const char *compound;
1079
12.4M
  *is_fully_qualified = false;
1080
1081
12.4M
  if (ZSTR_VAL(name)[0] == '\\') {
1082
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1083
305
    *is_fully_qualified = true;
1084
305
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1085
305
  }
1086
1087
12.4M
  if (type == ZEND_NAME_FQ) {
1088
38.6k
    *is_fully_qualified = true;
1089
38.6k
    return zend_string_copy(name);
1090
38.6k
  }
1091
1092
12.4M
  if (type == ZEND_NAME_RELATIVE) {
1093
1.42k
    *is_fully_qualified = true;
1094
1.42k
    return zend_prefix_with_ns(name);
1095
1.42k
  }
1096
1097
12.4M
  if (current_import_sub) {
1098
    /* If an unqualified name is a function/const alias, replace it. */
1099
1.72k
    zend_string *import_name;
1100
1.72k
    if (case_sensitive) {
1101
1.21k
      import_name = zend_hash_find_ptr(current_import_sub, name);
1102
1.21k
    } else {
1103
503
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1104
503
    }
1105
1106
1.72k
    if (import_name) {
1107
629
      *is_fully_qualified = true;
1108
629
      return zend_string_copy(import_name);
1109
629
    }
1110
1.72k
  }
1111
1112
12.4M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1113
12.4M
  if (compound) {
1114
7.39k
    *is_fully_qualified = true;
1115
7.39k
  }
1116
1117
12.4M
  if (compound && FC(imports)) {
1118
    /* If the first part of a qualified name is an alias, substitute it. */
1119
2.46k
    size_t len = compound - ZSTR_VAL(name);
1120
2.46k
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1121
1122
2.46k
    if (import_name) {
1123
481
      return zend_concat_names(
1124
481
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1125
481
    }
1126
2.46k
  }
1127
1128
12.4M
  return zend_prefix_with_ns(name);
1129
12.4M
}
1130
/* }}} */
1131
1132
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1133
3.59M
{
1134
3.59M
  return zend_resolve_non_class_name(
1135
3.59M
    name, type, is_fully_qualified, false, FC(imports_function));
1136
3.59M
}
1137
1138
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1139
8.87M
{
1140
8.87M
  return zend_resolve_non_class_name(
1141
8.87M
    name, type, is_fully_qualified, true, FC(imports_const));
1142
8.87M
}
1143
1144
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1145
3.41M
{
1146
3.41M
  const char *compound;
1147
1148
3.41M
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1149
3.26k
    if (type == ZEND_NAME_FQ) {
1150
7
      zend_error_noreturn(E_COMPILE_ERROR,
1151
7
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1152
7
    }
1153
3.25k
    if (type == ZEND_NAME_RELATIVE) {
1154
0
      zend_error_noreturn(E_COMPILE_ERROR,
1155
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1156
0
    }
1157
3.25k
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1158
3.25k
    return zend_string_copy(name);
1159
3.25k
  }
1160
1161
3.41M
  if (type == ZEND_NAME_RELATIVE) {
1162
296
    return zend_prefix_with_ns(name);
1163
296
  }
1164
1165
3.41M
  if (type == ZEND_NAME_FQ) {
1166
14.2k
    if (ZSTR_VAL(name)[0] == '\\') {
1167
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1168
128
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1169
128
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1170
0
        zend_error_noreturn(E_COMPILE_ERROR,
1171
0
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1172
0
      }
1173
128
      return name;
1174
128
    }
1175
1176
14.1k
    return zend_string_copy(name);
1177
14.2k
  }
1178
1179
3.40M
  if (FC(imports)) {
1180
4.03k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1181
4.03k
    if (compound) {
1182
      /* If the first part of a qualified name is an alias, substitute it. */
1183
1.18k
      size_t len = compound - ZSTR_VAL(name);
1184
1.18k
      const zend_string *import_name =
1185
1.18k
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1186
1187
1.18k
      if (import_name) {
1188
425
        return zend_concat_names(
1189
425
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1190
425
      }
1191
2.84k
    } else {
1192
      /* If an unqualified name is an alias, replace it. */
1193
2.84k
      zend_string *import_name
1194
2.84k
        = zend_hash_find_ptr_lc(FC(imports), name);
1195
1196
2.84k
      if (import_name) {
1197
1.22k
        return zend_string_copy(import_name);
1198
1.22k
      }
1199
2.84k
    }
1200
4.03k
  }
1201
1202
  /* If not fully qualified and not an alias, prepend the current namespace */
1203
3.39M
  return zend_prefix_with_ns(name);
1204
3.40M
}
1205
/* }}} */
1206
1207
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1208
3.34M
{
1209
3.34M
  const zval *class_name = zend_ast_get_zval(ast);
1210
3.34M
  if (Z_TYPE_P(class_name) != IS_STRING) {
1211
40
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1212
40
  }
1213
3.34M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1214
3.34M
}
1215
/* }}} */
1216
1217
static void label_ptr_dtor(zval *zv) /* {{{ */
1218
5.06k
{
1219
5.06k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1220
5.06k
}
1221
/* }}} */
1222
1223
2.04k
static void str_dtor(zval *zv)  /* {{{ */ {
1224
2.04k
  zend_string_release_ex(Z_STR_P(zv), 0);
1225
2.04k
}
1226
/* }}} */
1227
1228
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1229
48.7k
{
1230
48.7k
  zend_op_array *op_array = CG(active_op_array);
1231
48.7k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1232
48.7k
  zend_try_catch_element *elem;
1233
1234
48.7k
  op_array->try_catch_array = safe_erealloc(
1235
48.7k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1236
1237
48.7k
  elem = &op_array->try_catch_array[try_catch_offset];
1238
48.7k
  elem->try_op = try_op;
1239
48.7k
  elem->catch_op = 0;
1240
48.7k
  elem->finally_op = 0;
1241
48.7k
  elem->finally_end = 0;
1242
1243
48.7k
  return try_catch_offset;
1244
48.7k
}
1245
/* }}} */
1246
1247
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1248
1.69k
{
1249
1.69k
  if (function->type == ZEND_USER_FUNCTION) {
1250
1.69k
    zend_op_array *op_array = &function->op_array;
1251
1.69k
    if (op_array->refcount) {
1252
167
      (*op_array->refcount)++;
1253
167
    }
1254
1255
1.69k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1256
1.69k
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1257
1.69k
  }
1258
1259
1.69k
  if (function->common.function_name) {
1260
1.69k
    zend_string_addref(function->common.function_name);
1261
1.69k
  }
1262
1.69k
}
1263
/* }}} */
1264
1265
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) /* {{{ */
1266
115
{
1267
115
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1268
115
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1269
115
  const zend_function *old_function;
1270
1271
115
  ZEND_ASSERT(zv != NULL);
1272
115
  old_function = Z_PTR_P(zv);
1273
115
  if (old_function->type == ZEND_USER_FUNCTION
1274
108
    && old_function->op_array.last > 0) {
1275
108
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1276
108
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1277
108
          ZSTR_VAL(old_function->op_array.filename),
1278
108
          old_function->op_array.line_start);
1279
108
  } else {
1280
7
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1281
7
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1282
7
  }
1283
115
}
1284
1285
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1286
104
{
1287
104
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1288
104
  if (UNEXPECTED(!added_func)) {
1289
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1290
0
    return FAILURE;
1291
0
  }
1292
1293
104
  if (func->op_array.refcount) {
1294
21
    ++*func->op_array.refcount;
1295
21
  }
1296
104
  if (func->common.function_name) {
1297
104
    zend_string_addref(func->common.function_name);
1298
104
  }
1299
104
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1300
104
  return SUCCESS;
1301
104
}
1302
/* }}} */
1303
1304
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1305
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1306
6.99k
{
1307
6.99k
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1308
6.99k
  bool is_preloaded =
1309
6.99k
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1310
6.99k
  bool success;
1311
6.99k
  if (EXPECTED(!is_preloaded)) {
1312
6.99k
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1313
6.99k
  } else {
1314
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1315
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1316
0
  }
1317
6.99k
  if (UNEXPECTED(!success)) {
1318
162
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1319
162
    ZEND_ASSERT(old_class);
1320
162
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1321
162
    return NULL;
1322
162
  }
1323
1324
6.82k
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1325
250
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1326
250
    return ce;
1327
250
  }
1328
1329
6.57k
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1330
6.57k
  if (ce) {
1331
5.13k
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1332
5.13k
    return ce;
1333
5.13k
  }
1334
1335
1.44k
  if (!is_preloaded) {
1336
    /* Reload bucket pointer, the hash table may have been reallocated */
1337
286
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1338
286
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1339
1.15k
  } else {
1340
1.15k
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1341
1.15k
  }
1342
1.44k
  return NULL;
1343
6.57k
}
1344
1345
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1346
6.35k
{
1347
6.35k
  zval *rtd_key, *zv;
1348
1349
6.35k
  rtd_key = lcname + 1;
1350
1351
6.35k
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1352
1353
6.35k
  if (UNEXPECTED(!zv)) {
1354
12
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1355
12
    ZEND_ASSERT(ce);
1356
12
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1357
12
    return FAILURE;
1358
12
  }
1359
1360
  /* Register the derived class */
1361
6.33k
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1362
6.35k
}
1363
/* }}} */
1364
1365
21.2k
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1366
21.2k
  zend_string *result;
1367
21.2k
  if (type == NULL) {
1368
14.0k
    return zend_string_copy(new_type);
1369
14.0k
  }
1370
1371
7.23k
  if (is_intersection) {
1372
3.93k
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1373
3.93k
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1374
3.93k
    zend_string_release(type);
1375
3.93k
  } else {
1376
3.29k
    result = zend_string_concat3(
1377
3.29k
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1378
3.29k
    zend_string_release(type);
1379
3.29k
  }
1380
7.23k
  return result;
1381
21.2k
}
1382
1383
3.69k
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1384
3.69k
  if (scope) {
1385
2.42k
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1386
27
      name = scope->name;
1387
2.39k
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1388
0
      name = scope->parent->name;
1389
0
    }
1390
2.42k
  }
1391
1392
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1393
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1394
3.69k
  size_t len = strlen(ZSTR_VAL(name));
1395
3.69k
  if (len != ZSTR_LEN(name)) {
1396
10
    return zend_string_init(ZSTR_VAL(name), len, 0);
1397
10
  }
1398
3.68k
  return zend_string_copy(name);
1399
3.69k
}
1400
1401
static zend_string *add_intersection_type(zend_string *str,
1402
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1403
  bool is_bracketed)
1404
2.06k
{
1405
2.06k
  const zend_type *single_type;
1406
2.06k
  zend_string *intersection_str = NULL;
1407
1408
8.07k
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1409
8.07k
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1410
8.07k
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1411
1412
6.00k
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1413
6.00k
  } ZEND_TYPE_LIST_FOREACH_END();
1414
1415
2.06k
  ZEND_ASSERT(intersection_str);
1416
1417
2.06k
  if (is_bracketed) {
1418
1.58k
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1419
1.58k
    zend_string_release(intersection_str);
1420
1.58k
    intersection_str = result;
1421
1.58k
  }
1422
2.06k
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1423
2.06k
  zend_string_release(intersection_str);
1424
2.06k
  return str;
1425
2.06k
}
1426
1427
14.4k
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1428
14.4k
  zend_string *str = NULL;
1429
1430
  /* Pure intersection type */
1431
14.4k
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1432
484
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1433
484
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1434
14.0k
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1435
    /* A union type might not be a list */
1436
613
    const zend_type *list_type;
1437
3.39k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1438
3.39k
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1439
1.58k
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1440
1.58k
        continue;
1441
1.58k
      }
1442
1.20k
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1443
1.20k
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1444
1445
1.20k
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1446
1.20k
      zend_string *resolved = resolve_class_name(name, scope);
1447
1.20k
      str = add_type_string(str, resolved, /* is_intersection */ false);
1448
1.20k
      zend_string_release(resolved);
1449
1.20k
    } ZEND_TYPE_LIST_FOREACH_END();
1450
13.3k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1451
2.49k
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1452
2.49k
  }
1453
1454
14.4k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1455
1456
14.4k
  if (type_mask == MAY_BE_ANY) {
1457
125
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1458
1459
125
    return str;
1460
125
  }
1461
14.3k
  if (type_mask & MAY_BE_STATIC) {
1462
114
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1463
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1464
114
    if (scope && !zend_is_compiling()) {
1465
36
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1466
36
      if (called_scope) {
1467
15
        name = called_scope->name;
1468
15
      }
1469
36
    }
1470
114
    str = add_type_string(str, name, /* is_intersection */ false);
1471
114
  }
1472
14.3k
  if (type_mask & MAY_BE_CALLABLE) {
1473
61
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1474
61
  }
1475
14.3k
  if (type_mask & MAY_BE_OBJECT) {
1476
210
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1477
210
  }
1478
14.3k
  if (type_mask & MAY_BE_ARRAY) {
1479
1.08k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1480
1.08k
  }
1481
14.3k
  if (type_mask & MAY_BE_STRING) {
1482
5.78k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1483
5.78k
  }
1484
14.3k
  if (type_mask & MAY_BE_LONG) {
1485
3.44k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1486
3.44k
  }
1487
14.3k
  if (type_mask & MAY_BE_DOUBLE) {
1488
257
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1489
257
  }
1490
14.3k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1491
441
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1492
13.9k
  } else if (type_mask & MAY_BE_FALSE) {
1493
113
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1494
13.8k
  } else if (type_mask & MAY_BE_TRUE) {
1495
48
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1496
48
  }
1497
14.3k
  if (type_mask & MAY_BE_VOID) {
1498
132
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1499
132
  }
1500
14.3k
  if (type_mask & MAY_BE_NEVER) {
1501
22
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1502
22
  }
1503
1504
14.3k
  if (type_mask & MAY_BE_NULL) {
1505
1.47k
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1506
1.47k
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1507
1.47k
    if (!is_union && !has_intersection) {
1508
1.29k
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1509
1.29k
      zend_string_release(str);
1510
1.29k
      return nullable_str;
1511
1.29k
    }
1512
1513
180
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1514
180
  }
1515
13.0k
  return str;
1516
14.3k
}
1517
1518
8.71k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1519
8.71k
  return zend_type_to_string_resolved(type, NULL);
1520
8.71k
}
1521
1522
1.21k
static bool is_generator_compatible_class_type(const zend_string *name) {
1523
1.21k
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1524
1.03k
    || zend_string_equals_literal_ci(name, "Iterator")
1525
764
    || zend_string_equals_literal_ci(name, "Generator");
1526
1.21k
}
1527
1528
static void zend_mark_function_as_generator(void) /* {{{ */
1529
35.8k
{
1530
35.8k
  if (!CG(active_op_array)->function_name) {
1531
84
    zend_error_noreturn(E_COMPILE_ERROR,
1532
84
      "The \"yield\" expression can only be used inside a function");
1533
84
  }
1534
1535
35.7k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1536
821
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1537
821
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1538
821
    if (!valid_type) {
1539
789
      const zend_type *single_type;
1540
2.01k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1541
2.01k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1542
1.21k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1543
746
          valid_type = true;
1544
746
          break;
1545
746
        }
1546
2.01k
      } ZEND_TYPE_FOREACH_END();
1547
789
    }
1548
1549
821
    if (!valid_type) {
1550
43
      zend_string *str = zend_type_to_string(return_type);
1551
43
      zend_error_noreturn(E_COMPILE_ERROR,
1552
43
        "Generator return type must be a supertype of Generator, %s given",
1553
43
        ZSTR_VAL(str));
1554
43
    }
1555
821
  }
1556
1557
35.6k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1558
35.6k
}
1559
/* }}} */
1560
1561
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1562
68.4k
{
1563
68.4k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1564
68.4k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1565
1566
68.4k
  ZSTR_VAL(prop_name)[0] = '\0';
1567
68.4k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1568
68.4k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1569
68.4k
  return prop_name;
1570
68.4k
}
1571
/* }}} */
1572
1573
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) /* {{{ */
1574
1.19M
{
1575
1.19M
  size_t class_name_len;
1576
1.19M
  size_t anonclass_src_len;
1577
1578
1.19M
  *class_name = NULL;
1579
1580
1.19M
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1581
1.08M
    *prop_name = ZSTR_VAL(name);
1582
1.08M
    if (prop_len) {
1583
737k
      *prop_len = ZSTR_LEN(name);
1584
737k
    }
1585
1.08M
    return SUCCESS;
1586
1.08M
  }
1587
116k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1588
1.52k
    zend_error(E_NOTICE, "Illegal member variable name");
1589
1.52k
    *prop_name = ZSTR_VAL(name);
1590
1.52k
    if (prop_len) {
1591
1.49k
      *prop_len = ZSTR_LEN(name);
1592
1.49k
    }
1593
1.52k
    return FAILURE;
1594
1.52k
  }
1595
1596
115k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1597
115k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1598
543
    zend_error(E_NOTICE, "Corrupt member variable name");
1599
543
    *prop_name = ZSTR_VAL(name);
1600
543
    if (prop_len) {
1601
496
      *prop_len = ZSTR_LEN(name);
1602
496
    }
1603
543
    return FAILURE;
1604
543
  }
1605
1606
114k
  *class_name = ZSTR_VAL(name) + 1;
1607
114k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1608
114k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1609
26.5k
    class_name_len += anonclass_src_len + 1;
1610
26.5k
  }
1611
114k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1612
114k
  if (prop_len) {
1613
72.1k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1614
72.1k
  }
1615
114k
  return SUCCESS;
1616
115k
}
1617
/* }}} */
1618
1619
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1620
199
{
1621
199
  if (zend_hash_num_elements(array) > *max_checks) {
1622
3
    return false;
1623
3
  }
1624
196
  *max_checks -= zend_hash_num_elements(array);
1625
1626
196
  zval *element;
1627
550
  ZEND_HASH_FOREACH_VAL(array, element) {
1628
550
    if (Z_TYPE_P(element) < IS_ARRAY) {
1629
27
      continue;
1630
150
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1631
150
      if (!array_is_const_ex(array, max_checks)) {
1632
150
        return false;
1633
150
      }
1634
150
    } else {
1635
0
      return false;
1636
0
    }
1637
550
  } ZEND_HASH_FOREACH_END();
1638
1639
46
  return true;
1640
196
}
1641
1642
static bool array_is_const(const zend_array *array)
1643
49
{
1644
49
  uint32_t max_checks = 50;
1645
49
  return array_is_const_ex(array, &max_checks);
1646
49
}
1647
1648
10.3k
static bool can_ct_eval_const(const zend_constant *c) {
1649
10.3k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1650
87
    return 0;
1651
87
  }
1652
10.2k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1653
10.2k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1654
9.69k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1655
9.69k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1656
9.69k
    return 1;
1657
9.69k
  }
1658
607
  if (Z_TYPE(c->value) < IS_ARRAY
1659
607
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1660
15
    return 1;
1661
592
  } else if (Z_TYPE(c->value) == IS_ARRAY
1662
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1663
0
      && array_is_const(Z_ARR(c->value))) {
1664
0
    return 1;
1665
0
  }
1666
592
  return 0;
1667
607
}
1668
1669
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1670
8.87M
{
1671
  /* Substitute true, false and null (including unqualified usage in namespaces)
1672
   * before looking up the possibly namespaced name. */
1673
8.87M
  const char *lookup_name = ZSTR_VAL(name);
1674
8.87M
  size_t lookup_len = ZSTR_LEN(name);
1675
1676
8.87M
  if (!is_fully_qualified) {
1677
8.86M
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1678
8.86M
  }
1679
1680
8.87M
  zend_constant *c;
1681
8.87M
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1682
51.0k
    ZVAL_COPY_VALUE(zv, &c->value);
1683
51.0k
    return 1;
1684
51.0k
  }
1685
8.82M
  c = zend_hash_find_ptr(EG(zend_constants), name);
1686
8.82M
  if (c && can_ct_eval_const(c)) {
1687
9.70k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1688
9.70k
    return 1;
1689
9.70k
  }
1690
8.81M
  return 0;
1691
8.82M
}
1692
/* }}} */
1693
1694
static inline bool zend_is_scope_known(void) /* {{{ */
1695
82.7k
{
1696
82.7k
  if (!CG(active_op_array)) {
1697
    /* This can only happen when evaluating a default value string. */
1698
0
    return 0;
1699
0
  }
1700
1701
82.7k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1702
    /* Closures can be rebound to a different scope */
1703
70.1k
    return 0;
1704
70.1k
  }
1705
1706
12.5k
  if (!CG(active_class_entry)) {
1707
    /* The scope is known if we're in a free function (no scope), but not if we're in
1708
     * a file/eval (which inherits including/eval'ing scope). */
1709
1.88k
    return CG(active_op_array)->function_name != NULL;
1710
1.88k
  }
1711
1712
  /* For traits self etc refers to the using class, not the trait itself */
1713
10.6k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1714
12.5k
}
1715
/* }}} */
1716
1717
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1718
78.0k
{
1719
78.0k
  if (!CG(active_class_entry)) {
1720
72.9k
    return 0;
1721
72.9k
  }
1722
5.10k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1723
1.26k
    return 1;
1724
1.26k
  }
1725
3.84k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1726
2.85k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1727
5.10k
}
1728
/* }}} */
1729
1730
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1731
4.19M
{
1732
4.19M
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1733
44.1k
    return ZEND_FETCH_CLASS_SELF;
1734
4.15M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1735
6.74k
    return ZEND_FETCH_CLASS_PARENT;
1736
4.14M
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1737
1.84k
    return ZEND_FETCH_CLASS_STATIC;
1738
4.14M
  } else {
1739
4.14M
    return ZEND_FETCH_CLASS_DEFAULT;
1740
4.14M
  }
1741
4.19M
}
1742
/* }}} */
1743
1744
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1745
484k
{
1746
  /* Fully qualified names are always default refs */
1747
484k
  if (name_ast->attr == ZEND_NAME_FQ) {
1748
3.46k
    return ZEND_FETCH_CLASS_DEFAULT;
1749
3.46k
  }
1750
1751
481k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1752
484k
}
1753
/* }}} */
1754
1755
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1756
67.6k
{
1757
67.6k
  zend_string *class_name = zend_ast_get_str(ast);
1758
67.6k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1759
22
    zend_error_noreturn(E_COMPILE_ERROR,
1760
22
      "Cannot use \"%s\" as %s, as it is reserved",
1761
22
      ZSTR_VAL(class_name), type);
1762
22
  }
1763
67.6k
  return zend_resolve_class_name(class_name, ast->attr);
1764
67.6k
}
1765
1766
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1767
46.4k
{
1768
46.4k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1769
4.27k
    zend_class_entry *ce = CG(active_class_entry);
1770
4.27k
    if (!ce) {
1771
26
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1772
26
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1773
26
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1774
4.24k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1775
27
      zend_error_noreturn(E_COMPILE_ERROR,
1776
27
        "Cannot use \"parent\" when current class scope has no parent");
1777
27
    }
1778
4.27k
  }
1779
46.4k
}
1780
/* }}} */
1781
1782
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1783
9.08k
{
1784
9.08k
  uint32_t fetch_type;
1785
9.08k
  const zval *class_name;
1786
1787
9.08k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1788
2.15k
    return 0;
1789
2.15k
  }
1790
1791
6.93k
  class_name = zend_ast_get_zval(class_ast);
1792
1793
6.93k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1794
7
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1795
7
  }
1796
1797
6.92k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1798
6.92k
  zend_ensure_valid_class_fetch_type(fetch_type);
1799
1800
6.92k
  switch (fetch_type) {
1801
1.34k
    case ZEND_FETCH_CLASS_SELF:
1802
1.34k
      if (CG(active_class_entry) && zend_is_scope_known()) {
1803
369
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1804
369
        return 1;
1805
369
      }
1806
972
      return 0;
1807
1.31k
    case ZEND_FETCH_CLASS_PARENT:
1808
1.31k
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1809
271
          && zend_is_scope_known()) {
1810
271
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1811
271
        return 1;
1812
271
      }
1813
1.03k
      return 0;
1814
348
    case ZEND_FETCH_CLASS_STATIC:
1815
348
      return 0;
1816
3.91k
    case ZEND_FETCH_CLASS_DEFAULT:
1817
3.91k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1818
3.91k
      return 1;
1819
6.92k
    EMPTY_SWITCH_DEFAULT_CASE()
1820
6.92k
  }
1821
6.92k
}
1822
/* }}} */
1823
1824
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1825
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1826
652
{
1827
652
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1828
13
    return 0;
1829
639
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1830
    /* This condition is only met on directly accessing trait constants,
1831
     * because the ce is replaced to the class entry of the composing class
1832
     * on binding. */
1833
1
    return 0;
1834
638
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1835
434
    return 1;
1836
434
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1837
203
    return c->ce == scope;
1838
203
  } else {
1839
1
    zend_class_entry *ce = c->ce;
1840
1
    while (1) {
1841
1
      if (ce == scope) {
1842
0
        return 1;
1843
0
      }
1844
1
      if (!ce->parent) {
1845
1
        break;
1846
1
      }
1847
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1848
0
        ce = ce->parent;
1849
0
      } else {
1850
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1851
0
        if (!ce) {
1852
0
          break;
1853
0
        }
1854
0
      }
1855
0
    }
1856
    /* Reverse case cannot be true during compilation */
1857
1
    return 0;
1858
1
  }
1859
652
}
1860
1861
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1862
78.0k
{
1863
78.0k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1864
78.0k
  zend_class_constant *cc;
1865
78.0k
  zval *c;
1866
1867
78.0k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1868
1.39k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1869
76.6k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1870
560
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1871
560
    if (ce) {
1872
76
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1873
484
    } else {
1874
484
      return 0;
1875
484
    }
1876
76.0k
  } else {
1877
76.0k
    return 0;
1878
76.0k
  }
1879
1880
1.47k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1881
520
    return 0;
1882
520
  }
1883
1884
952
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1885
315
    return 0;
1886
315
  }
1887
1888
637
  c = &cc->value;
1889
1890
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1891
637
  if (Z_TYPE_P(c) < IS_ARRAY) {
1892
404
    ZVAL_COPY_OR_DUP(zv, c);
1893
404
    return 1;
1894
404
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1895
46
    ZVAL_COPY_OR_DUP(zv, c);
1896
46
    return 1;
1897
46
  }
1898
1899
187
  return 0;
1900
637
}
1901
/* }}} */
1902
1903
static void zend_add_to_list(void *result, void *item) /* {{{ */
1904
62.2k
{
1905
62.2k
  void** list = *(void**)result;
1906
62.2k
  size_t n = 0;
1907
1908
62.2k
  if (list) {
1909
207k
    while (list[n]) {
1910
164k
      n++;
1911
164k
    }
1912
42.1k
  }
1913
1914
62.2k
  list = erealloc(list, sizeof(void*) * (n+2));
1915
1916
62.2k
  list[n]   = item;
1917
62.2k
  list[n+1] = NULL;
1918
1919
62.2k
  *(void**)result = list;
1920
62.2k
}
1921
/* }}} */
1922
1923
static void zend_do_extended_stmt(znode* result) /* {{{ */
1924
1.66M
{
1925
1.66M
  zend_op *opline;
1926
1927
1.66M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1928
1.66M
    return;
1929
1.66M
  }
1930
1931
0
  opline = get_next_op();
1932
1933
0
  opline->opcode = ZEND_EXT_STMT;
1934
0
  if (result) {
1935
0
    SET_NODE(opline->op1, result);
1936
0
  }
1937
0
}
1938
/* }}} */
1939
1940
static void zend_do_extended_fcall_begin(void) /* {{{ */
1941
4.00M
{
1942
4.00M
  zend_op *opline;
1943
1944
4.00M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1945
4.00M
    return;
1946
4.00M
  }
1947
1948
0
  opline = get_next_op();
1949
1950
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1951
0
}
1952
/* }}} */
1953
1954
static void zend_do_extended_fcall_end(void) /* {{{ */
1955
4.00M
{
1956
4.00M
  zend_op *opline;
1957
1958
4.00M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1959
4.00M
    return;
1960
4.00M
  }
1961
1962
0
  opline = get_next_op();
1963
1964
0
  opline->opcode = ZEND_EXT_FCALL_END;
1965
0
}
1966
/* }}} */
1967
1968
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1969
0
  zend_auto_global *auto_global;
1970
1971
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1972
0
    if (auto_global->armed) {
1973
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1974
0
    }
1975
0
    return 1;
1976
0
  }
1977
0
  return 0;
1978
0
}
1979
/* }}} */
1980
1981
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1982
2.91M
{
1983
2.91M
  zend_auto_global *auto_global;
1984
1985
2.91M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1986
4.80k
    if (auto_global->armed) {
1987
204
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1988
204
    }
1989
4.80k
    return 1;
1990
4.80k
  }
1991
2.90M
  return 0;
1992
2.91M
}
1993
/* }}} */
1994
1995
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1996
128
{
1997
128
  zend_auto_global auto_global;
1998
128
  zend_result retval;
1999
2000
128
  auto_global.name = name;
2001
128
  auto_global.auto_global_callback = auto_global_callback;
2002
128
  auto_global.jit = jit;
2003
2004
128
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2005
2006
128
  return retval;
2007
128
}
2008
/* }}} */
2009
2010
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2011
306k
{
2012
306k
  zend_auto_global *auto_global;
2013
2014
5.52M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2015
5.52M
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2016
5.52M
  } ZEND_HASH_FOREACH_END();
2017
2018
5.52M
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2019
5.52M
    if (auto_global->armed && !auto_global->jit) {
2020
1.22M
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2021
1.22M
    }
2022
5.52M
  } ZEND_HASH_FOREACH_END();
2023
306k
}
2024
/* }}} */
2025
2026
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2027
11.3M
{
2028
11.3M
  zval zv;
2029
11.3M
  int ret;
2030
2031
11.3M
  if (CG(increment_lineno)) {
2032
14.2k
    CG(zend_lineno)++;
2033
14.2k
    CG(increment_lineno) = 0;
2034
14.2k
  }
2035
2036
11.3M
  ret = lex_scan(&zv, elem);
2037
11.3M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2038
11.3M
  return ret;
2039
2040
11.3M
}
2041
/* }}} */
2042
2043
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2044
136k
{
2045
136k
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2046
2047
136k
  ce->refcount = 1;
2048
136k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2049
136k
  ce->ce_flags2 = 0;
2050
2051
136k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2052
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2053
0
  }
2054
2055
136k
  ce->default_properties_table = NULL;
2056
136k
  ce->default_static_members_table = NULL;
2057
136k
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2058
136k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2059
136k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2060
2061
136k
  ce->doc_comment = NULL;
2062
2063
136k
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2064
136k
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2065
2066
136k
  ce->default_object_handlers = &std_object_handlers;
2067
136k
  ce->default_properties_count = 0;
2068
136k
  ce->default_static_members_count = 0;
2069
136k
  ce->properties_info_table = NULL;
2070
136k
  ce->attributes = NULL;
2071
136k
  ce->enum_backing_type = IS_UNDEF;
2072
136k
  ce->backed_enum_table = NULL;
2073
2074
136k
  if (nullify_handlers) {
2075
133k
    ce->constructor = NULL;
2076
133k
    ce->destructor = NULL;
2077
133k
    ce->clone = NULL;
2078
133k
    ce->__get = NULL;
2079
133k
    ce->__set = NULL;
2080
133k
    ce->__unset = NULL;
2081
133k
    ce->__isset = NULL;
2082
133k
    ce->__call = NULL;
2083
133k
    ce->__callstatic = NULL;
2084
133k
    ce->__tostring = NULL;
2085
133k
    ce->__serialize = NULL;
2086
133k
    ce->__unserialize = NULL;
2087
133k
    ce->__debugInfo = NULL;
2088
133k
    ce->create_object = NULL;
2089
133k
    ce->get_iterator = NULL;
2090
133k
    ce->iterator_funcs_ptr = NULL;
2091
133k
    ce->arrayaccess_funcs_ptr = NULL;
2092
133k
    ce->get_static_method = NULL;
2093
133k
    ce->parent = NULL;
2094
133k
    ce->parent_name = NULL;
2095
133k
    ce->num_interfaces = 0;
2096
133k
    ce->interfaces = NULL;
2097
133k
    ce->num_traits = 0;
2098
133k
    ce->num_hooked_props = 0;
2099
133k
    ce->num_hooked_prop_variance_checks = 0;
2100
133k
    ce->trait_names = NULL;
2101
133k
    ce->trait_aliases = NULL;
2102
133k
    ce->trait_precedences = NULL;
2103
133k
    ce->serialize = NULL;
2104
133k
    ce->unserialize = NULL;
2105
133k
    if (ce->type == ZEND_INTERNAL_CLASS) {
2106
0
      ce->info.internal.module = NULL;
2107
0
      ce->info.internal.builtin_functions = NULL;
2108
0
    }
2109
133k
  }
2110
136k
}
2111
/* }}} */
2112
2113
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2114
0
{
2115
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2116
0
}
2117
/* }}} */
2118
2119
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2120
0
{
2121
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2122
0
  zend_string *left = Z_STR_P(left_zv);
2123
0
  zend_string *right = zend_ast_get_str(right_ast);
2124
2125
0
  zend_string *result;
2126
0
  size_t left_len = ZSTR_LEN(left);
2127
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2128
2129
0
  result = zend_string_extend(left, len, 0);
2130
0
  ZSTR_VAL(result)[left_len] = '\\';
2131
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2132
0
  ZSTR_VAL(result)[len] = '\0';
2133
0
  zend_string_release_ex(right, 0);
2134
2135
0
  ZVAL_STR(left_zv, result);
2136
0
  return left_ast;
2137
0
}
2138
/* }}} */
2139
2140
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2141
1.76k
{
2142
1.76k
  zval *zv = zend_ast_get_zval(ast);
2143
1.76k
  if (Z_TYPE_P(zv) == IS_LONG) {
2144
1.44k
    if (Z_LVAL_P(zv) == 0) {
2145
644
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2146
802
    } else {
2147
802
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2148
802
      Z_LVAL_P(zv) *= -1;
2149
802
    }
2150
1.44k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2151
320
    size_t orig_len = Z_STRLEN_P(zv);
2152
320
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2153
320
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2154
320
    Z_STRVAL_P(zv)[0] = '-';
2155
320
  } else {
2156
0
    ZEND_UNREACHABLE();
2157
0
  }
2158
1.76k
  return ast;
2159
1.76k
}
2160
/* }}} */
2161
2162
static void zend_verify_namespace(void) /* {{{ */
2163
515k
{
2164
515k
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2165
52
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2166
52
  }
2167
515k
}
2168
/* }}} */
2169
2170
/* {{{ zend_dirname
2171
   Returns directory name component of path */
2172
ZEND_API size_t zend_dirname(char *path, size_t len)
2173
1.34k
{
2174
1.34k
  char *end = path + len - 1;
2175
1.34k
  unsigned int len_adjust = 0;
2176
2177
#ifdef ZEND_WIN32
2178
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2179
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2180
   */
2181
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2182
    /* Skip over the drive spec (if any) so as not to change */
2183
    path += 2;
2184
    len_adjust += 2;
2185
    if (2 == len) {
2186
      /* Return "c:" on Win32 for dirname("c:").
2187
       * It would be more consistent to return "c:."
2188
       * but that would require making the string *longer*.
2189
       */
2190
      return len;
2191
    }
2192
  }
2193
#endif
2194
2195
1.34k
  if (len == 0) {
2196
    /* Illegal use of this function */
2197
0
    return 0;
2198
0
  }
2199
2200
  /* Strip trailing slashes */
2201
1.34k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2202
0
    end--;
2203
0
  }
2204
1.34k
  if (end < path) {
2205
    /* The path only contained slashes */
2206
0
    path[0] = DEFAULT_SLASH;
2207
0
    path[1] = '\0';
2208
0
    return 1 + len_adjust;
2209
0
  }
2210
2211
  /* Strip filename */
2212
14.8k
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2213
13.4k
    end--;
2214
13.4k
  }
2215
1.34k
  if (end < path) {
2216
    /* No slash found, therefore return '.' */
2217
710
    path[0] = '.';
2218
710
    path[1] = '\0';
2219
710
    return 1 + len_adjust;
2220
710
  }
2221
2222
  /* Strip slashes which came before the file name */
2223
1.26k
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2224
630
    end--;
2225
630
  }
2226
630
  if (end < path) {
2227
5
    path[0] = DEFAULT_SLASH;
2228
5
    path[1] = '\0';
2229
5
    return 1 + len_adjust;
2230
5
  }
2231
625
  *(end+1) = '\0';
2232
2233
625
  return (size_t)(end + 1 - path) + len_adjust;
2234
630
}
2235
/* }}} */
2236
2237
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2238
682k
{
2239
682k
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2240
2241
682k
  switch (type) {
2242
391k
    case BP_VAR_R:
2243
391k
      opline->result_type = IS_TMP_VAR;
2244
391k
      result->op_type = IS_TMP_VAR;
2245
391k
      return;
2246
143k
    case BP_VAR_W:
2247
143k
      opline->opcode += 1 * factor;
2248
143k
      return;
2249
15.0k
    case BP_VAR_RW:
2250
15.0k
      opline->opcode += 2 * factor;
2251
15.0k
      return;
2252
109k
    case BP_VAR_IS:
2253
109k
      opline->result_type = IS_TMP_VAR;
2254
109k
      result->op_type = IS_TMP_VAR;
2255
109k
      opline->opcode += 3 * factor;
2256
109k
      return;
2257
15.4k
    case BP_VAR_FUNC_ARG:
2258
15.4k
      opline->opcode += 4 * factor;
2259
15.4k
      return;
2260
7.02k
    case BP_VAR_UNSET:
2261
7.02k
      opline->opcode += 5 * factor;
2262
7.02k
      return;
2263
682k
    EMPTY_SWITCH_DEFAULT_CASE()
2264
682k
  }
2265
682k
}
2266
/* }}} */
2267
2268
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2269
4.87M
{
2270
4.87M
  opline->result_type = IS_VAR;
2271
4.87M
  opline->result.var = get_temporary_variable();
2272
4.87M
  GET_NODE(result, opline->result);
2273
4.87M
}
2274
/* }}} */
2275
2276
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2277
31.5M
{
2278
31.5M
  opline->result_type = IS_TMP_VAR;
2279
31.5M
  opline->result.var = get_temporary_variable();
2280
31.5M
  GET_NODE(result, opline->result);
2281
31.5M
}
2282
/* }}} */
2283
2284
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2285
34.5M
{
2286
34.5M
  zend_op *opline = get_next_op();
2287
34.5M
  opline->opcode = opcode;
2288
2289
34.5M
  if (op1 != NULL) {
2290
27.6M
    SET_NODE(opline->op1, op1);
2291
27.6M
  }
2292
2293
34.5M
  if (op2 != NULL) {
2294
2.04M
    SET_NODE(opline->op2, op2);
2295
2.04M
  }
2296
2297
34.5M
  if (result) {
2298
4.46M
    zend_make_var_result(result, opline);
2299
4.46M
  }
2300
34.5M
  return opline;
2301
34.5M
}
2302
/* }}} */
2303
2304
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2305
33.9M
{
2306
33.9M
  zend_op *opline = get_next_op();
2307
33.9M
  opline->opcode = opcode;
2308
2309
33.9M
  if (op1 != NULL) {
2310
8.38M
    SET_NODE(opline->op1, op1);
2311
8.38M
  }
2312
2313
33.9M
  if (op2 != NULL) {
2314
3.17M
    SET_NODE(opline->op2, op2);
2315
3.17M
  }
2316
2317
33.9M
  if (result) {
2318
31.4M
    zend_make_tmp_result(result, opline);
2319
31.4M
  }
2320
2321
33.9M
  return opline;
2322
33.9M
}
2323
/* }}} */
2324
2325
static void zend_emit_tick(void) /* {{{ */
2326
58.7k
{
2327
58.7k
  zend_op *opline;
2328
2329
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2330
58.7k
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2331
3.36k
    return;
2332
3.36k
  }
2333
2334
55.3k
  opline = get_next_op();
2335
2336
55.3k
  opline->opcode = ZEND_TICKS;
2337
55.3k
  opline->extended_value = FC(declarables).ticks;
2338
55.3k
}
2339
/* }}} */
2340
2341
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2342
159k
{
2343
159k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2344
159k
}
2345
/* }}} */
2346
2347
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2348
764k
{
2349
764k
  uint32_t opnum = get_next_op_number();
2350
764k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2351
764k
  opline->op1.opline_num = opnum_target;
2352
764k
  return opnum;
2353
764k
}
2354
/* }}} */
2355
2356
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2357
199k
{
2358
199k
  switch (opline->opcode) {
2359
4.75k
    case ZEND_IS_IDENTICAL:
2360
5.17k
    case ZEND_IS_NOT_IDENTICAL:
2361
106k
    case ZEND_IS_EQUAL:
2362
107k
    case ZEND_IS_NOT_EQUAL:
2363
116k
    case ZEND_IS_SMALLER:
2364
117k
    case ZEND_IS_SMALLER_OR_EQUAL:
2365
153k
    case ZEND_CASE:
2366
154k
    case ZEND_CASE_STRICT:
2367
154k
    case ZEND_ISSET_ISEMPTY_CV:
2368
154k
    case ZEND_ISSET_ISEMPTY_VAR:
2369
155k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2370
155k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2371
155k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2372
155k
    case ZEND_INSTANCEOF:
2373
156k
    case ZEND_TYPE_CHECK:
2374
156k
    case ZEND_DEFINED:
2375
156k
    case ZEND_IN_ARRAY:
2376
156k
    case ZEND_ARRAY_KEY_EXISTS:
2377
156k
      return 1;
2378
42.7k
    default:
2379
42.7k
      return 0;
2380
199k
  }
2381
199k
}
2382
/* }}} */
2383
2384
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2385
190k
{
2386
190k
  uint32_t opnum = get_next_op_number();
2387
190k
  zend_op *opline;
2388
2389
190k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2390
170k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2391
170k
    if (opline->result_type == IS_TMP_VAR
2392
167k
     && opline->result.var == cond->u.op.var
2393
167k
     && zend_is_smart_branch(opline)) {
2394
156k
      if (opcode == ZEND_JMPZ) {
2395
20.3k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2396
136k
      } else {
2397
136k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2398
136k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2399
136k
      }
2400
156k
    }
2401
170k
  }
2402
190k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2403
190k
  opline->op2.opline_num = opnum_target;
2404
190k
  return opnum;
2405
190k
}
2406
/* }}} */
2407
2408
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2409
959k
{
2410
959k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2411
959k
  switch (opline->opcode) {
2412
752k
    case ZEND_JMP:
2413
752k
      opline->op1.opline_num = opnum_target;
2414
752k
      break;
2415
38.5k
    case ZEND_JMPZ:
2416
170k
    case ZEND_JMPNZ:
2417
177k
    case ZEND_JMPZ_EX:
2418
182k
    case ZEND_JMPNZ_EX:
2419
188k
    case ZEND_JMP_SET:
2420
206k
    case ZEND_COALESCE:
2421
206k
    case ZEND_JMP_NULL:
2422
207k
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2423
207k
    case ZEND_JMP_FRAMELESS:
2424
207k
      opline->op2.opline_num = opnum_target;
2425
207k
      break;
2426
959k
    EMPTY_SWITCH_DEFAULT_CASE()
2427
959k
  }
2428
959k
}
2429
/* }}} */
2430
2431
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2432
953k
{
2433
953k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2434
953k
}
2435
/* }}} */
2436
2437
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2438
415k
{
2439
415k
  zend_op tmp_opline;
2440
2441
415k
  init_op(&tmp_opline);
2442
2443
415k
  tmp_opline.opcode = opcode;
2444
415k
  if (op1 != NULL) {
2445
415k
    SET_NODE(tmp_opline.op1, op1);
2446
415k
  }
2447
415k
  if (op2 != NULL) {
2448
391k
    SET_NODE(tmp_opline.op2, op2);
2449
391k
  }
2450
415k
  if (result) {
2451
410k
    zend_make_var_result(result, &tmp_opline);
2452
410k
  }
2453
2454
415k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2455
415k
  return zend_stack_top(&CG(delayed_oplines_stack));
2456
415k
}
2457
/* }}} */
2458
2459
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2460
609k
{
2461
609k
  return zend_stack_count(&CG(delayed_oplines_stack));
2462
609k
}
2463
/* }}} */
2464
2465
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2466
607k
{
2467
607k
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2468
607k
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2469
2470
607k
  ZEND_ASSERT(count >= offset);
2471
1.02M
  for (i = offset; i < count; ++i) {
2472
414k
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2473
407k
      opline = get_next_op();
2474
407k
      memcpy(opline, &oplines[i], sizeof(zend_op));
2475
407k
    } else {
2476
7.47k
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2477
7.47k
    }
2478
414k
  }
2479
2480
607k
  CG(delayed_oplines_stack).top = offset;
2481
607k
  return opline;
2482
607k
}
2483
/* }}} */
2484
2485
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2486
47.8M
{
2487
47.8M
  switch (ast_kind) {
2488
290k
    case ZEND_AST_DIM:
2489
370k
    case ZEND_AST_PROP:
2490
471k
    case ZEND_AST_NULLSAFE_PROP:
2491
498k
    case ZEND_AST_STATIC_PROP:
2492
611k
    case ZEND_AST_METHOD_CALL:
2493
626k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2494
739k
    case ZEND_AST_STATIC_CALL:
2495
739k
      return 1;
2496
47.0M
    default:
2497
47.0M
      return 0;
2498
47.8M
  }
2499
47.8M
}
2500
2501
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2502
829k
{
2503
829k
  switch (ast->kind) {
2504
138k
    case ZEND_AST_DIM:
2505
171k
    case ZEND_AST_PROP:
2506
183k
    case ZEND_AST_STATIC_PROP:
2507
185k
    case ZEND_AST_METHOD_CALL:
2508
188k
    case ZEND_AST_STATIC_CALL:
2509
188k
      return zend_ast_is_short_circuited(ast->child[0]);
2510
585
    case ZEND_AST_NULLSAFE_PROP:
2511
719
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2512
719
      return 1;
2513
640k
    default:
2514
640k
      return 0;
2515
829k
  }
2516
829k
}
2517
2518
static void zend_assert_not_short_circuited(const zend_ast *ast)
2519
11.6k
{
2520
11.6k
  if (zend_ast_is_short_circuited(ast)) {
2521
48
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2522
48
  }
2523
11.6k
}
2524
2525
/* Mark nodes that are an inner part of a short-circuiting chain.
2526
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2527
 * We do this to avoid passing down an argument in various compile functions. */
2528
2529
752k
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2530
2531
527k
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2532
527k
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2533
233k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2534
233k
  }
2535
527k
}
2536
2537
static uint32_t zend_short_circuiting_checkpoint(void)
2538
47.3M
{
2539
47.3M
  return zend_stack_count(&CG(short_circuiting_opnums));
2540
47.3M
}
2541
2542
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2543
47.2M
{
2544
47.2M
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2545
46.7M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2546
47.2M
  if (!is_short_circuited) {
2547
46.7M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2548
46.7M
      && "Short circuiting stack should be empty");
2549
46.7M
    return;
2550
46.7M
  }
2551
2552
518k
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2553
    /* Outer-most node will commit. */
2554
66.4k
    return;
2555
66.4k
  }
2556
2557
509k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2558
58.0k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2559
58.0k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2560
58.0k
    opline->op2.opline_num = get_next_op_number();
2561
58.0k
    SET_NODE(opline->result, result);
2562
58.0k
    opline->extended_value |=
2563
58.0k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2564
58.0k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2565
57.6k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2566
58.0k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2567
58.0k
  }
2568
451k
}
2569
2570
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2571
58.0k
{
2572
58.0k
  uint32_t jmp_null_opnum = get_next_op_number();
2573
58.0k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2574
58.0k
  if (opline->op1_type == IS_CONST) {
2575
981
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2576
981
  }
2577
58.0k
  if (bp_type == BP_VAR_IS) {
2578
1.24k
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2579
1.24k
  }
2580
58.0k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2581
58.0k
}
2582
2583
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2584
200k
{
2585
200k
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2586
200k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2587
101k
    znode memoized_result;
2588
2589
    /* Go through normal compilation */
2590
101k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2591
101k
    zend_compile_expr(result, expr);
2592
101k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2593
2594
101k
    if (result->op_type == IS_VAR) {
2595
2.00k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
99.4k
    } else if (result->op_type == IS_TMP_VAR) {
2597
91.9k
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2598
91.9k
    } else {
2599
7.50k
      if (result->op_type == IS_CONST) {
2600
5.89k
        Z_TRY_ADDREF(result->u.constant);
2601
5.89k
      }
2602
7.50k
      memoized_result = *result;
2603
7.50k
    }
2604
2605
101k
    zend_hash_index_update_mem(
2606
101k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2607
101k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2608
99.5k
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2609
99.5k
    *result = *memoized_result;
2610
99.5k
    if (result->op_type == IS_CONST) {
2611
4.68k
      Z_TRY_ADDREF(result->u.constant);
2612
4.68k
    }
2613
99.5k
  } else {
2614
0
    ZEND_UNREACHABLE();
2615
0
  }
2616
200k
}
2617
/* }}} */
2618
2619
static void zend_emit_return_type_check(
2620
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2621
36.8k
{
2622
36.8k
  zend_type type = return_info->type;
2623
36.8k
  if (ZEND_TYPE_IS_SET(type)) {
2624
36.8k
    zend_op *opline;
2625
2626
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2627
36.8k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2628
4.11k
      if (expr) {
2629
20
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2630
7
          zend_error_noreturn(E_COMPILE_ERROR,
2631
7
            "A void %s must not return a value "
2632
7
            "(did you mean \"return;\" instead of \"return null;\"?)",
2633
7
            CG(active_class_entry) != NULL ? "method" : "function");
2634
13
        } else {
2635
13
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2636
13
          CG(active_class_entry) != NULL ? "method" : "function");
2637
13
        }
2638
20
      }
2639
      /* we don't need run-time check */
2640
4.09k
      return;
2641
4.11k
    }
2642
2643
    /* `return` is illegal in a never-returning function */
2644
32.7k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2645
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2646
13
      ZEND_ASSERT(!implicit);
2647
13
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2648
13
        CG(active_class_entry) != NULL ? "method" : "function");
2649
0
      return;
2650
13
    }
2651
2652
32.7k
    if (!expr && !implicit) {
2653
14
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2654
7
        zend_error_noreturn(E_COMPILE_ERROR,
2655
7
          "A %s with return type must return a value "
2656
7
          "(did you mean \"return null;\" instead of \"return;\"?)",
2657
7
          CG(active_class_entry) != NULL ? "method" : "function");
2658
7
      } else {
2659
7
        zend_error_noreturn(E_COMPILE_ERROR,
2660
7
          "A %s with return type must return a value",
2661
7
          CG(active_class_entry) != NULL ? "method" : "function");
2662
7
      }
2663
14
    }
2664
2665
32.7k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2666
      /* we don't need run-time check for mixed return type */
2667
534
      return;
2668
534
    }
2669
2670
32.2k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2671
      /* we don't need run-time check */
2672
1.83k
      return;
2673
1.83k
    }
2674
2675
30.3k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2676
30.3k
    if (expr && expr->op_type == IS_CONST) {
2677
538
      opline->result_type = expr->op_type = IS_TMP_VAR;
2678
538
      opline->result.var = expr->u.op.var = get_temporary_variable();
2679
538
    }
2680
30.3k
  }
2681
36.8k
}
2682
/* }}} */
2683
2684
void zend_emit_final_return(bool return_one) /* {{{ */
2685
1.52M
{
2686
1.52M
  znode zn;
2687
1.52M
  zend_op *ret;
2688
1.52M
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2689
2690
1.52M
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2691
31.9k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2692
31.6k
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2693
2694
31.6k
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2695
1.00k
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2696
1.00k
      return;
2697
1.00k
    }
2698
2699
30.6k
    zend_emit_return_type_check(NULL, return_info, true);
2700
30.6k
  }
2701
2702
1.52M
  zn.op_type = IS_CONST;
2703
1.52M
  if (return_one) {
2704
90.5k
    ZVAL_LONG(&zn.u.constant, 1);
2705
1.43M
  } else {
2706
1.43M
    ZVAL_NULL(&zn.u.constant);
2707
1.43M
  }
2708
2709
1.52M
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2710
1.52M
  ret->extended_value = -1;
2711
1.52M
}
2712
/* }}} */
2713
2714
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2715
5.23M
{
2716
5.23M
  return ast->kind == ZEND_AST_VAR
2717
5.08M
    || ast->kind == ZEND_AST_DIM
2718
5.05M
    || ast->kind == ZEND_AST_PROP
2719
5.04M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2720
5.04M
    || ast->kind == ZEND_AST_STATIC_PROP;
2721
5.23M
}
2722
/* }}} */
2723
2724
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2725
5.47M
{
2726
5.47M
  return ast->kind == ZEND_AST_CALL
2727
5.32M
    || ast->kind == ZEND_AST_METHOD_CALL
2728
5.30M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2729
5.30M
    || ast->kind == ZEND_AST_STATIC_CALL
2730
5.30M
    || ast->kind == ZEND_AST_PIPE;
2731
5.47M
}
2732
/* }}} */
2733
2734
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2735
27.1k
{
2736
27.1k
  return zend_is_variable(ast) || zend_is_call(ast);
2737
27.1k
}
2738
/* }}} */
2739
2740
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2741
86.0k
{
2742
86.0k
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2743
63.0k
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2744
60.6k
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2745
86.0k
}
2746
/* }}} */
2747
2748
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2749
25.6k
{
2750
25.6k
  while (
2751
30.1k
    ast->kind == ZEND_AST_DIM
2752
27.5k
    || ast->kind == ZEND_AST_PROP
2753
25.6k
  ) {
2754
4.48k
    ast = ast->child[0];
2755
4.48k
  }
2756
2757
25.6k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2758
25.6k
}
2759
/* }}} */
2760
2761
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2762
47.3k
{
2763
47.3k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2764
0
    return 0;
2765
0
  }
2766
2767
47.3k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2768
47.3k
}
2769
/* }}} */
2770
2771
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2772
9.18k
{
2773
9.18k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2774
5.72k
    zend_ulong index;
2775
2776
5.72k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2777
509
      zval_ptr_dtor(&node->u.constant);
2778
509
      ZVAL_LONG(&node->u.constant, index);
2779
509
    }
2780
5.72k
  }
2781
9.18k
}
2782
/* }}} */
2783
2784
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2785
60.5k
{
2786
60.5k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2787
17.5k
    zend_ulong index;
2788
2789
17.5k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2790
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2791
       * See bug #63217
2792
       */
2793
5.19k
      int c = zend_add_literal(&dim_node->u.constant);
2794
5.19k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2795
5.19k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2796
5.19k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2797
5.19k
      return;
2798
5.19k
    }
2799
17.5k
  }
2800
60.5k
}
2801
/* }}} */
2802
2803
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2804
219k
{
2805
219k
  if (class_node->op_type == IS_CONST) {
2806
170k
    opline->op1_type = IS_CONST;
2807
170k
    opline->op1.constant = zend_add_class_name_literal(
2808
170k
      Z_STR(class_node->u.constant));
2809
170k
  } else {
2810
49.2k
    SET_NODE(opline->op1, class_node);
2811
49.2k
  }
2812
219k
}
2813
/* }}} */
2814
2815
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2816
240k
{
2817
240k
  uint32_t fetch_type;
2818
2819
240k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2820
34.3k
    znode name_node;
2821
2822
34.3k
    zend_compile_expr(&name_node, name_ast);
2823
2824
34.3k
    if (name_node.op_type == IS_CONST) {
2825
2.14k
      zend_string *name;
2826
2827
2.14k
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2828
9
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2829
9
      }
2830
2831
2.13k
      name = Z_STR(name_node.u.constant);
2832
2.13k
      fetch_type = zend_get_class_fetch_type(name);
2833
2834
2.13k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2835
1.39k
        result->op_type = IS_CONST;
2836
1.39k
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2837
1.39k
      } else {
2838
740
        zend_ensure_valid_class_fetch_type(fetch_type);
2839
740
        result->op_type = IS_UNUSED;
2840
740
        result->u.op.num = fetch_type | fetch_flags;
2841
740
      }
2842
2843
2.13k
      zend_string_release_ex(name, 0);
2844
32.1k
    } else {
2845
32.1k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2846
32.1k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2847
32.1k
    }
2848
34.3k
    return;
2849
34.3k
  }
2850
2851
  /* Fully qualified names are always default refs */
2852
205k
  if (name_ast->attr == ZEND_NAME_FQ) {
2853
4.26k
    result->op_type = IS_CONST;
2854
4.26k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2855
4.26k
    return;
2856
4.26k
  }
2857
2858
201k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2859
201k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2860
178k
    result->op_type = IS_CONST;
2861
178k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2862
178k
  } else {
2863
23.6k
    zend_ensure_valid_class_fetch_type(fetch_type);
2864
23.6k
    result->op_type = IS_UNUSED;
2865
23.6k
    result->u.op.num = fetch_type | fetch_flags;
2866
23.6k
  }
2867
201k
}
2868
/* }}} */
2869
2870
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2871
1.19M
{
2872
1.19M
  zend_ast *name_ast = ast->child[0];
2873
1.19M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2874
1.10M
    zval *zv = zend_ast_get_zval(name_ast);
2875
1.10M
    zend_string *name;
2876
2877
1.10M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2878
1.09M
      name = zval_make_interned_string(zv);
2879
1.09M
    } else {
2880
6.77k
      name = zend_new_interned_string(zval_get_string_func(zv));
2881
6.77k
    }
2882
2883
1.10M
    if (zend_is_auto_global(name)) {
2884
871
      return FAILURE;
2885
871
    }
2886
2887
1.10M
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2888
195
      if (type == BP_VAR_R) {
2889
88
        zend_error(E_DEPRECATED,
2890
88
          "The predefined locally scoped $http_response_header variable is deprecated,"
2891
88
          " call http_get_last_response_headers() instead");
2892
107
      } else if (type == BP_VAR_W) {
2893
19
        CG(context).has_assigned_to_http_response_header = true;
2894
19
      }
2895
195
    }
2896
2897
1.10M
    result->op_type = IS_CV;
2898
1.10M
    result->u.op.var = lookup_cv(name);
2899
2900
1.10M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2901
6.77k
      zend_string_release_ex(name, 0);
2902
6.77k
    }
2903
2904
1.10M
    return SUCCESS;
2905
1.10M
  }
2906
2907
91.5k
  return FAILURE;
2908
1.19M
}
2909
/* }}} */
2910
2911
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2912
267k
{
2913
267k
  zend_ast *name_ast = ast->child[0];
2914
267k
  znode name_node;
2915
267k
  zend_op *opline;
2916
2917
267k
  zend_compile_expr(&name_node, name_ast);
2918
267k
  if (name_node.op_type == IS_CONST) {
2919
178k
    convert_to_string(&name_node.u.constant);
2920
178k
  }
2921
2922
267k
  if (delayed) {
2923
14.1k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2924
253k
  } else {
2925
253k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2926
253k
  }
2927
2928
267k
  if (name_node.op_type == IS_CONST &&
2929
178k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2930
2931
852
    opline->extended_value = ZEND_FETCH_GLOBAL;
2932
266k
  } else {
2933
    // TODO: Have a test case for this?
2934
266k
    if (name_node.op_type == IS_CONST
2935
177k
      && type == BP_VAR_R
2936
177k
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2937
1
      zend_error(E_DEPRECATED,
2938
1
        "The predefined locally scoped $http_response_header variable is deprecated,"
2939
1
        " call http_get_last_response_headers() instead");
2940
1
    }
2941
266k
    opline->extended_value = ZEND_FETCH_LOCAL;
2942
266k
  }
2943
2944
267k
  zend_adjust_for_fetch_type(opline, result, type);
2945
267k
  return opline;
2946
267k
}
2947
/* }}} */
2948
2949
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2950
1.67M
{
2951
1.67M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2952
1.46M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2953
1.46M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2954
1.46M
  }
2955
2956
213k
  return 0;
2957
1.67M
}
2958
/* }}} */
2959
2960
static bool is_globals_fetch(const zend_ast *ast)
2961
7.11M
{
2962
7.11M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2963
1.65M
    const zval *name = zend_ast_get_zval(ast->child[0]);
2964
1.65M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2965
1.65M
  }
2966
2967
5.46M
  return 0;
2968
7.11M
}
2969
2970
static bool is_global_var_fetch(const zend_ast *ast)
2971
470k
{
2972
470k
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2973
470k
}
2974
2975
static bool this_guaranteed_exists(void) /* {{{ */
2976
12.4k
{
2977
12.4k
  const zend_oparray_context *ctx = &CG(context);
2978
17.8k
  while (ctx) {
2979
    /* Instance methods always have a $this.
2980
     * This also includes closures that have a scope and use $this. */
2981
17.8k
    const zend_op_array *op_array = ctx->op_array;
2982
17.8k
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2983
45
      return false;
2984
17.8k
    } else if (op_array->scope) {
2985
10.5k
      return true;
2986
10.5k
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2987
1.89k
      return false;
2988
1.89k
    }
2989
5.41k
    ctx = ctx->prev;
2990
5.41k
  }
2991
0
  return false;
2992
12.4k
}
2993
/* }}} */
2994
2995
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2996
1.15M
{
2997
1.15M
  if (is_this_fetch(ast)) {
2998
2.96k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2999
2.96k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3000
2.76k
      opline->result_type = IS_TMP_VAR;
3001
2.76k
      result->op_type = IS_TMP_VAR;
3002
2.76k
    }
3003
2.96k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3004
2.96k
    return opline;
3005
1.14M
  } else if (is_globals_fetch(ast)) {
3006
2.49k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3007
2.49k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3008
2.42k
      opline->result_type = IS_TMP_VAR;
3009
2.42k
      result->op_type = IS_TMP_VAR;
3010
2.42k
    }
3011
2.49k
    return opline;
3012
1.14M
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3013
89.6k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3014
89.6k
  }
3015
1.05M
  return NULL;
3016
1.15M
}
3017
/* }}} */
3018
3019
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3020
382k
{
3021
382k
  if (type != BP_VAR_R
3022
256k
   && type != BP_VAR_IS
3023
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3024
153k
   && type != BP_VAR_FUNC_ARG
3025
140k
   && zend_is_call(ast)) {
3026
3.04k
    if (node->op_type == IS_VAR) {
3027
3.02k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3028
3.02k
      opline->result_type = IS_VAR;
3029
3.02k
      opline->result.var = opline->op1.var;
3030
3.02k
    } else {
3031
21
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3032
21
    }
3033
3.04k
  }
3034
382k
}
3035
/* }}} */
3036
3037
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3038
8.94k
{
3039
8.94k
  znode dummy_node;
3040
8.94k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3041
8.94k
    zend_ast_create_znode(value_node));
3042
8.94k
  zend_compile_expr(&dummy_node, assign_ast);
3043
8.94k
  zend_do_free(&dummy_node);
3044
8.94k
}
3045
/* }}} */
3046
3047
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3048
273k
{
3049
273k
  zend_ast *var_ast = ast->child[0];
3050
273k
  zend_ast *dim_ast = ast->child[1];
3051
273k
  zend_op *opline;
3052
3053
273k
  znode var_node, dim_node;
3054
3055
273k
  if (is_globals_fetch(var_ast)) {
3056
2.56k
    if (dim_ast == NULL) {
3057
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3058
10
    }
3059
3060
2.55k
    zend_compile_expr(&dim_node, dim_ast);
3061
2.55k
    if (dim_node.op_type == IS_CONST) {
3062
2.03k
      convert_to_string(&dim_node.u.constant);
3063
2.03k
    }
3064
3065
2.55k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3066
2.55k
    opline->extended_value = ZEND_FETCH_GLOBAL;
3067
2.55k
    zend_adjust_for_fetch_type(opline, result, type);
3068
2.55k
    return opline;
3069
270k
  } else {
3070
270k
    zend_short_circuiting_mark_inner(var_ast);
3071
270k
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3072
270k
    if (opline) {
3073
174k
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3074
2.01k
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3075
172k
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3076
95.2k
          || opline->opcode == ZEND_FETCH_DIM_RW
3077
93.7k
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3078
93.1k
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3079
79.9k
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3080
79.9k
      }
3081
174k
    }
3082
270k
  }
3083
3084
270k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3085
3086
270k
  if (dim_ast == NULL) {
3087
17.8k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3088
307
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3089
307
    }
3090
17.4k
    if (type == BP_VAR_UNSET) {
3091
10
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3092
10
    }
3093
17.4k
    dim_node.op_type = IS_UNUSED;
3094
253k
  } else {
3095
253k
    zend_compile_expr(&dim_node, dim_ast);
3096
253k
  }
3097
3098
270k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3099
270k
  zend_adjust_for_fetch_type(opline, result, type);
3100
270k
  if (by_ref) {
3101
12.3k
    opline->extended_value = ZEND_FETCH_DIM_REF;
3102
12.3k
  }
3103
3104
270k
  if (dim_node.op_type == IS_CONST) {
3105
51.2k
    zend_handle_numeric_dim(opline, &dim_node);
3106
51.2k
  }
3107
270k
  return opline;
3108
270k
}
3109
3110
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3111
88.1k
{
3112
88.1k
  uint32_t offset = zend_delayed_compile_begin();
3113
88.1k
  zend_delayed_compile_dim(result, ast, type, by_ref);
3114
88.1k
  return zend_delayed_compile_end(offset);
3115
88.1k
}
3116
/* }}} */
3117
3118
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3119
121k
{
3120
121k
  zend_ast *obj_ast = ast->child[0];
3121
121k
  zend_ast *prop_ast = ast->child[1];
3122
3123
121k
  znode obj_node, prop_node;
3124
121k
  zend_op *opline;
3125
121k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3126
3127
121k
  if (is_this_fetch(obj_ast)) {
3128
10.1k
    if (this_guaranteed_exists()) {
3129
8.40k
      obj_node.op_type = IS_UNUSED;
3130
8.40k
    } else {
3131
1.78k
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3132
1.78k
    }
3133
10.1k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3134
3135
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3136
     * check for a nullsafe access. */
3137
111k
  } else {
3138
111k
    zend_short_circuiting_mark_inner(obj_ast);
3139
111k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3140
111k
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3141
22.7k
        || opline->opcode == ZEND_FETCH_DIM_RW
3142
22.3k
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3143
21.8k
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3144
3.80k
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3145
3.80k
    }
3146
3147
111k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3148
111k
    if (nullsafe) {
3149
51.7k
      if (obj_node.op_type == IS_TMP_VAR) {
3150
        /* Flush delayed oplines */
3151
9.21k
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3152
9.21k
        uint32_t var = obj_node.u.op.var;
3153
9.21k
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3154
9.21k
        uint32_t i = count;
3155
3156
38.1k
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3157
31.2k
          i--;
3158
31.2k
          if (oplines[i].op1_type == IS_TMP_VAR) {
3159
28.9k
            var = oplines[i].op1.var;
3160
28.9k
          } else {
3161
2.25k
            break;
3162
2.25k
          }
3163
31.2k
        }
3164
40.4k
        for (; i < count; ++i) {
3165
31.2k
          if (oplines[i].opcode != ZEND_NOP) {
3166
7.48k
            opline = get_next_op();
3167
7.48k
            memcpy(opline, &oplines[i], sizeof(zend_op));
3168
7.48k
            oplines[i].opcode = ZEND_NOP;
3169
7.48k
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3170
7.48k
          }
3171
31.2k
        }
3172
9.21k
      }
3173
51.7k
      zend_emit_jmp_null(&obj_node, type);
3174
51.7k
    }
3175
111k
  }
3176
3177
121k
  zend_compile_expr(&prop_node, prop_ast);
3178
3179
121k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3180
121k
  if (opline->op2_type == IS_CONST) {
3181
118k
    convert_to_string(CT_CONSTANT(opline->op2));
3182
118k
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3183
118k
    opline->extended_value = zend_alloc_cache_slots(3);
3184
118k
  }
3185
3186
121k
  zend_adjust_for_fetch_type(opline, result, type);
3187
3188
121k
  return opline;
3189
121k
}
3190
/* }}} */
3191
3192
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3193
85.6k
{
3194
85.6k
  uint32_t offset = zend_delayed_compile_begin();
3195
85.6k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3196
85.6k
  if (by_ref) { /* shared with cache_slot */
3197
3.11k
    opline->extended_value |= ZEND_FETCH_REF;
3198
3.11k
  }
3199
85.6k
  return zend_delayed_compile_end(offset);
3200
85.6k
}
3201
/* }}} */
3202
3203
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3204
21.1k
{
3205
21.1k
  zend_ast *class_ast = ast->child[0];
3206
21.1k
  zend_ast *prop_ast = ast->child[1];
3207
3208
21.1k
  znode class_node, prop_node;
3209
21.1k
  zend_op *opline;
3210
3211
21.1k
  zend_short_circuiting_mark_inner(class_ast);
3212
21.1k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3213
3214
21.1k
  zend_compile_expr(&prop_node, prop_ast);
3215
3216
21.1k
  if (delayed) {
3217
7.19k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3218
13.9k
  } else {
3219
13.9k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3220
13.9k
  }
3221
21.1k
  if (opline->op1_type == IS_CONST) {
3222
19.3k
    convert_to_string(CT_CONSTANT(opline->op1));
3223
19.3k
    opline->extended_value = zend_alloc_cache_slots(3);
3224
19.3k
  }
3225
21.1k
  if (class_node.op_type == IS_CONST) {
3226
12.5k
    opline->op2_type = IS_CONST;
3227
12.5k
    opline->op2.constant = zend_add_class_name_literal(
3228
12.5k
      Z_STR(class_node.u.constant));
3229
12.5k
    if (opline->op1_type != IS_CONST) {
3230
1.28k
      opline->extended_value = zend_alloc_cache_slot();
3231
1.28k
    }
3232
12.5k
  } else {
3233
8.63k
    SET_NODE(opline->op2, &class_node);
3234
8.63k
  }
3235
3236
21.1k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3237
2.08k
    opline->extended_value |= ZEND_FETCH_REF;
3238
2.08k
  }
3239
3240
21.1k
  zend_adjust_for_fetch_type(opline, result, type);
3241
21.1k
  return opline;
3242
21.1k
}
3243
/* }}} */
3244
3245
9.53k
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3246
9.53k
  if (var_ast->kind == ZEND_AST_ARRAY) {
3247
484
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3248
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3249
7
    }
3250
477
    if (array_style != var_ast->attr) {
3251
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3252
7
    }
3253
9.05k
  } else if (!zend_can_write_to_variable(var_ast)) {
3254
104
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3255
104
  }
3256
9.53k
}
3257
/* }}} */
3258
3259
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3260
3261
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3262
5.82k
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3263
5.82k
  const zend_ast_list *list = zend_ast_get_list(ast);
3264
5.82k
  bool has_refs = false;
3265
5.82k
  uint32_t i;
3266
3267
46.0k
  for (i = 0; i < list->children; ++i) {
3268
40.2k
    zend_ast *elem_ast = list->child[i];
3269
3270
40.2k
    if (elem_ast) {
3271
10.6k
      zend_ast *var_ast = elem_ast->child[0];
3272
10.6k
      if (var_ast->kind == ZEND_AST_ARRAY) {
3273
493
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3274
493
      }
3275
10.6k
      has_refs |= elem_ast->attr;
3276
10.6k
    }
3277
40.2k
  }
3278
3279
5.82k
  return has_refs;
3280
5.82k
}
3281
/* }}} */
3282
3283
static bool list_is_keyed(const zend_ast_list *list)
3284
5.33k
{
3285
8.42k
  for (uint32_t i = 0; i < list->children; i++) {
3286
8.37k
    const zend_ast *child = list->child[i];
3287
8.37k
    if (child) {
3288
5.29k
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3289
5.29k
    }
3290
8.37k
  }
3291
45
  return false;
3292
5.33k
}
3293
3294
static void zend_compile_list_assign(
3295
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3296
5.33k
{
3297
5.33k
  zend_ast_list *list = zend_ast_get_list(ast);
3298
5.33k
  uint32_t i;
3299
5.33k
  bool has_elems = false;
3300
5.33k
  bool is_keyed = list_is_keyed(list);
3301
3302
5.33k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3303
239
    zval_make_interned_string(&expr_node->u.constant);
3304
239
  }
3305
3306
18.1k
  for (i = 0; i < list->children; ++i) {
3307
12.7k
    zend_ast *elem_ast = list->child[i];
3308
12.7k
    zend_ast *var_ast, *key_ast;
3309
12.7k
    znode fetch_result, dim_node;
3310
12.7k
    zend_op *opline;
3311
3312
12.7k
    if (elem_ast == NULL) {
3313
3.19k
      if (is_keyed) {
3314
17
        zend_error(E_COMPILE_ERROR,
3315
17
          "Cannot use empty array entries in keyed array assignment");
3316
3.17k
      } else {
3317
3.17k
        continue;
3318
3.17k
      }
3319
3.19k
    }
3320
3321
9.58k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3322
16
      zend_error(E_COMPILE_ERROR,
3323
16
          "Spread operator is not supported in assignments");
3324
16
    }
3325
3326
9.58k
    var_ast = elem_ast->child[0];
3327
9.58k
    key_ast = elem_ast->child[1];
3328
9.58k
    has_elems = true;
3329
3330
9.58k
    if (is_keyed) {
3331
1.12k
      if (key_ast == NULL) {
3332
7
        zend_error(E_COMPILE_ERROR,
3333
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3334
7
      }
3335
3336
1.12k
      zend_compile_expr(&dim_node, key_ast);
3337
8.46k
    } else {
3338
8.46k
      if (key_ast != NULL) {
3339
7
        zend_error(E_COMPILE_ERROR,
3340
7
          "Cannot mix keyed and unkeyed array entries in assignments");
3341
7
      }
3342
3343
8.46k
      dim_node.op_type = IS_CONST;
3344
8.46k
      ZVAL_LONG(&dim_node.u.constant, i);
3345
8.46k
    }
3346
3347
9.58k
    if (expr_node->op_type == IS_CONST) {
3348
1.54k
      Z_TRY_ADDREF(expr_node->u.constant);
3349
1.54k
    }
3350
3351
9.58k
    zend_verify_list_assign_target(var_ast, array_style);
3352
3353
9.58k
    opline = zend_emit_op(&fetch_result,
3354
9.58k
      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);
3355
3356
9.58k
    if (dim_node.op_type == IS_CONST) {
3357
9.26k
      zend_handle_numeric_dim(opline, &dim_node);
3358
9.26k
    }
3359
3360
9.58k
    if (elem_ast->attr) {
3361
1.94k
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3362
1.94k
    }
3363
9.58k
    if (var_ast->kind == ZEND_AST_ARRAY) {
3364
470
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3365
9.11k
    } else if (elem_ast->attr) {
3366
1.84k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3367
7.27k
    } else {
3368
7.27k
      zend_emit_assign_znode(var_ast, &fetch_result);
3369
7.27k
    }
3370
9.58k
  }
3371
3372
5.33k
  if (has_elems == 0) {
3373
45
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3374
45
  }
3375
3376
5.29k
  if (result) {
3377
4.30k
    *result = *expr_node;
3378
4.30k
  } else {
3379
989
    zend_do_free(expr_node);
3380
989
  }
3381
5.29k
}
3382
/* }}} */
3383
3384
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3385
482k
{
3386
482k
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3387
85
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3388
85
  }
3389
482k
  if (
3390
482k
    ast->kind == ZEND_AST_METHOD_CALL
3391
482k
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3392
482k
    || ast->kind == ZEND_AST_STATIC_CALL
3393
482k
  ) {
3394
19
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3395
19
  }
3396
482k
  if (zend_ast_is_short_circuited(ast)) {
3397
27
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3398
27
  }
3399
482k
  if (is_globals_fetch(ast)) {
3400
34
    zend_error_noreturn(E_COMPILE_ERROR,
3401
34
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3402
34
  }
3403
482k
}
3404
/* }}} */
3405
3406
/* Detects $a... = $a pattern */
3407
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3408
32.1k
{
3409
32.1k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3410
27.1k
    return 0;
3411
27.1k
  }
3412
3413
11.2k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3414
6.27k
    var_ast = var_ast->child[0];
3415
6.27k
  }
3416
3417
4.93k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3418
851
    return 0;
3419
851
  }
3420
3421
4.08k
  {
3422
4.08k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3423
4.08k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3424
4.08k
    bool result = zend_string_equals(name1, name2);
3425
4.08k
    zend_string_release_ex(name1, 0);
3426
4.08k
    zend_string_release_ex(name2, 0);
3427
4.08k
    return result;
3428
4.93k
  }
3429
4.93k
}
3430
/* }}} */
3431
3432
static void zend_compile_expr_with_potential_assign_to_self(
3433
32.1k
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3434
32.1k
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3435
    /* $a[0] = $a should evaluate the right $a first */
3436
1.13k
    znode cv_node;
3437
3438
1.13k
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3439
52
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3440
1.08k
    } else {
3441
1.08k
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3442
1.08k
    }
3443
30.9k
  } else {
3444
30.9k
    zend_compile_expr(expr_node, expr_ast);
3445
30.9k
  }
3446
32.1k
}
3447
3448
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3449
262k
{
3450
262k
  zend_ast *var_ast = ast->child[0];
3451
262k
  zend_ast *expr_ast = ast->child[1];
3452
3453
262k
  znode var_node, expr_node;
3454
262k
  zend_op *opline;
3455
262k
  uint32_t offset;
3456
262k
  if (is_this_fetch(var_ast)) {
3457
13
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3458
13
  }
3459
3460
262k
  zend_ensure_writable_variable(var_ast);
3461
3462
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3463
262k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3464
262k
  switch (kind) {
3465
229k
    case ZEND_AST_VAR:
3466
229k
      offset = zend_delayed_compile_begin();
3467
229k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3468
229k
      zend_compile_expr(&expr_node, expr_ast);
3469
229k
      zend_delayed_compile_end(offset);
3470
229k
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3471
229k
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3472
229k
      return;
3473
2.86k
    case ZEND_AST_STATIC_PROP:
3474
2.86k
      offset = zend_delayed_compile_begin();
3475
2.86k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3476
2.86k
      zend_compile_expr(&expr_node, expr_ast);
3477
3478
2.86k
      opline = zend_delayed_compile_end(offset);
3479
2.86k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3480
2.86k
      opline->result_type = IS_TMP_VAR;
3481
2.86k
      result->op_type = IS_TMP_VAR;
3482
3483
2.86k
      zend_emit_op_data(&expr_node);
3484
2.86k
      return;
3485
13.2k
    case ZEND_AST_DIM:
3486
13.2k
      offset = zend_delayed_compile_begin();
3487
13.2k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3488
13.2k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3489
3490
13.2k
      opline = zend_delayed_compile_end(offset);
3491
13.2k
      opline->opcode = ZEND_ASSIGN_DIM;
3492
13.2k
      opline->result_type = IS_TMP_VAR;
3493
13.2k
      result->op_type = IS_TMP_VAR;
3494
3495
13.2k
      opline = zend_emit_op_data(&expr_node);
3496
13.2k
      return;
3497
11.8k
    case ZEND_AST_PROP:
3498
11.8k
    case ZEND_AST_NULLSAFE_PROP:
3499
11.8k
      offset = zend_delayed_compile_begin();
3500
11.8k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3501
11.8k
      zend_compile_expr(&expr_node, expr_ast);
3502
3503
11.8k
      opline = zend_delayed_compile_end(offset);
3504
11.8k
      opline->opcode = ZEND_ASSIGN_OBJ;
3505
11.8k
      opline->result_type = IS_TMP_VAR;
3506
11.8k
      result->op_type = IS_TMP_VAR;
3507
3508
11.8k
      zend_emit_op_data(&expr_node);
3509
11.8k
      return;
3510
4.98k
    case ZEND_AST_ARRAY:
3511
4.98k
      if (zend_propagate_list_refs(var_ast)) {
3512
1.56k
        if (!zend_is_variable_or_call(expr_ast)) {
3513
23
          zend_error_noreturn(E_COMPILE_ERROR,
3514
23
            "Cannot assign reference to non referenceable value");
3515
1.53k
        } else {
3516
1.53k
          zend_assert_not_short_circuited(expr_ast);
3517
1.53k
        }
3518
3519
1.53k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3520
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3521
         * self-assignments, this forces the RHS to evaluate first. */
3522
1.53k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3523
3.42k
      } else {
3524
3.42k
        if (expr_ast->kind == ZEND_AST_VAR) {
3525
          /* list($a, $b) = $a should evaluate the right $a first */
3526
617
          znode cv_node;
3527
3528
617
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3529
301
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3530
316
          } else {
3531
316
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3532
316
          }
3533
2.81k
        } else {
3534
2.81k
          zend_compile_expr(&expr_node, expr_ast);
3535
2.81k
        }
3536
3.42k
      }
3537
3538
4.96k
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3539
4.96k
      return;
3540
0
    EMPTY_SWITCH_DEFAULT_CASE();
3541
262k
  }
3542
262k
}
3543
/* }}} */
3544
3545
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3546
7.87k
{
3547
7.87k
  zend_ast *target_ast = ast->child[0];
3548
7.87k
  zend_ast *source_ast = ast->child[1];
3549
3550
7.87k
  znode target_node, source_node;
3551
7.87k
  zend_op *opline;
3552
7.87k
  uint32_t offset, flags;
3553
3554
7.87k
  if (is_this_fetch(target_ast)) {
3555
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3556
7
  }
3557
7.87k
  zend_ensure_writable_variable(target_ast);
3558
7.87k
  zend_assert_not_short_circuited(source_ast);
3559
7.87k
  if (is_globals_fetch(source_ast)) {
3560
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3561
7
  }
3562
3563
7.86k
  offset = zend_delayed_compile_begin();
3564
7.86k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3565
7.86k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3566
3567
7.86k
  if ((target_ast->kind != ZEND_AST_VAR
3568
5.65k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3569
3.23k
   && source_ast->kind != ZEND_AST_ZNODE
3570
2.09k
   && source_node.op_type != IS_CV) {
3571
    /* Both LHS and RHS expressions may modify the same data structure,
3572
     * and the modification during RHS evaluation may dangle the pointer
3573
     * to the result of the LHS evaluation.
3574
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3575
     * See: Bug #71539
3576
     */
3577
999
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3578
999
  }
3579
3580
7.86k
  opline = zend_delayed_compile_end(offset);
3581
3582
7.86k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3583
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3584
7
  }
3585
3586
7.85k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3587
3588
7.85k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3589
860
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3590
860
    opline->extended_value &= ~ZEND_FETCH_REF;
3591
860
    opline->extended_value |= flags;
3592
860
    zend_emit_op_data(&source_node);
3593
860
    *result = target_node;
3594
6.99k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3595
342
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3596
342
    opline->extended_value &= ~ZEND_FETCH_REF;
3597
342
    opline->extended_value |= flags;
3598
342
    zend_emit_op_data(&source_node);
3599
342
    *result = target_node;
3600
6.65k
  } else {
3601
6.65k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3602
6.65k
    opline->extended_value = flags;
3603
6.65k
  }
3604
7.85k
}
3605
/* }}} */
3606
3607
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3608
2.62k
{
3609
2.62k
  znode dummy_node;
3610
2.62k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3611
2.62k
    zend_ast_create_znode(value_node));
3612
2.62k
  zend_compile_expr(&dummy_node, assign_ast);
3613
2.62k
  zend_do_free(&dummy_node);
3614
2.62k
}
3615
/* }}} */
3616
3617
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3618
169k
{
3619
169k
  zend_ast *var_ast = ast->child[0];
3620
169k
  zend_ast *expr_ast = ast->child[1];
3621
169k
  uint32_t opcode = ast->attr;
3622
3623
169k
  znode var_node, expr_node;
3624
169k
  zend_op *opline;
3625
169k
  uint32_t offset, cache_slot;
3626
3627
169k
  zend_ensure_writable_variable(var_ast);
3628
3629
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3630
169k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3631
169k
  switch (kind) {
3632
163k
    case ZEND_AST_VAR:
3633
163k
      offset = zend_delayed_compile_begin();
3634
163k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3635
163k
      zend_compile_expr(&expr_node, expr_ast);
3636
163k
      zend_delayed_compile_end(offset);
3637
163k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3638
163k
      opline->extended_value = opcode;
3639
163k
      return;
3640
752
    case ZEND_AST_STATIC_PROP:
3641
752
      offset = zend_delayed_compile_begin();
3642
752
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3643
752
      zend_compile_expr(&expr_node, expr_ast);
3644
3645
752
      opline = zend_delayed_compile_end(offset);
3646
752
      cache_slot = opline->extended_value;
3647
752
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3648
752
      opline->extended_value = opcode;
3649
752
      opline->result_type = IS_TMP_VAR;
3650
752
      result->op_type = IS_TMP_VAR;
3651
3652
752
      opline = zend_emit_op_data(&expr_node);
3653
752
      opline->extended_value = cache_slot;
3654
752
      return;
3655
3.46k
    case ZEND_AST_DIM:
3656
3.46k
      offset = zend_delayed_compile_begin();
3657
3.46k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3658
3.46k
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3659
3660
3.46k
      opline = zend_delayed_compile_end(offset);
3661
3.46k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3662
3.46k
      opline->extended_value = opcode;
3663
3.46k
      opline->result_type = IS_TMP_VAR;
3664
3.46k
      result->op_type = IS_TMP_VAR;
3665
3666
3.46k
      zend_emit_op_data(&expr_node);
3667
3.46k
      return;
3668
1.66k
    case ZEND_AST_PROP:
3669
1.66k
    case ZEND_AST_NULLSAFE_PROP:
3670
1.66k
      offset = zend_delayed_compile_begin();
3671
1.66k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3672
1.66k
      zend_compile_expr(&expr_node, expr_ast);
3673
3674
1.66k
      opline = zend_delayed_compile_end(offset);
3675
1.66k
      cache_slot = opline->extended_value;
3676
1.66k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3677
1.66k
      opline->extended_value = opcode;
3678
1.66k
      opline->result_type = IS_TMP_VAR;
3679
1.66k
      result->op_type = IS_TMP_VAR;
3680
3681
1.66k
      opline = zend_emit_op_data(&expr_node);
3682
1.66k
      opline->extended_value = cache_slot;
3683
1.66k
      return;
3684
169k
    EMPTY_SWITCH_DEFAULT_CASE()
3685
169k
  }
3686
169k
}
3687
/* }}} */
3688
3689
5.85k
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3690
  // TODO: Caching?
3691
5.85k
  if (fn->type == ZEND_USER_FUNCTION) {
3692
10.2k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3693
9.38k
      const zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3694
9.38k
      if (zend_string_equals(arg_info->name, arg_name)) {
3695
1.66k
        return i + 1;
3696
1.66k
      }
3697
9.38k
    }
3698
3.28k
  } else {
3699
3.28k
    ZEND_ASSERT(fn->common.num_args == 0 || fn->internal_function.arg_info);
3700
11.2k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3701
8.50k
      const zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
3702
8.50k
      size_t len = strlen(arg_info->name);
3703
8.50k
      if (zend_string_equals_cstr(arg_name, arg_info->name, len)) {
3704
572
        return i + 1;
3705
572
      }
3706
8.50k
    }
3707
3.28k
  }
3708
3709
  /* Either an invalid argument name, or collected into a variadic argument. */
3710
3.61k
  return (uint32_t) -1;
3711
5.85k
}
3712
3713
static uint32_t zend_compile_args(
3714
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3715
3.99M
{
3716
3.99M
  const zend_ast_list *args = zend_ast_get_list(ast);
3717
3.99M
  uint32_t i;
3718
3.99M
  bool uses_arg_unpack = false;
3719
3.99M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3720
3721
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3722
   * May not actually use named argument passing. */
3723
3.99M
  bool uses_named_args = false;
3724
  /* Whether there may be any undef arguments due to the use of named arguments. */
3725
3.99M
  bool may_have_undef = false;
3726
  /* Whether there may be any extra named arguments collected into a variadic. */
3727
3.99M
  *may_have_extra_named_args = false;
3728
3729
9.31M
  for (i = 0; i < args->children; ++i) {
3730
5.31M
    zend_ast *arg = args->child[i];
3731
5.31M
    zend_string *arg_name = NULL;
3732
5.31M
    uint32_t arg_num = i + 1;
3733
3734
5.31M
    znode arg_node;
3735
5.31M
    zend_op *opline;
3736
5.31M
    uint8_t opcode;
3737
3738
5.31M
    if (arg->kind == ZEND_AST_UNPACK) {
3739
2.03k
      if (uses_named_args) {
3740
8
        zend_error_noreturn(E_COMPILE_ERROR,
3741
8
          "Cannot use argument unpacking after named arguments");
3742
8
      }
3743
3744
      /* Unpack may contain named arguments. */
3745
2.03k
      may_have_undef = true;
3746
2.03k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3747
1.58k
        *may_have_extra_named_args = true;
3748
1.58k
      }
3749
3750
2.03k
      uses_arg_unpack = true;
3751
2.03k
      fbc = NULL;
3752
3753
2.03k
      zend_compile_expr(&arg_node, arg->child[0]);
3754
2.03k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3755
2.03k
      opline->op2.num = arg_count;
3756
2.03k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3757
3758
2.03k
      continue;
3759
2.03k
    }
3760
3761
5.31M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3762
62.0k
      uses_named_args = true;
3763
62.0k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3764
62.0k
      arg = arg->child[1];
3765
3766
62.0k
      if (fbc && !uses_arg_unpack) {
3767
5.85k
        arg_num = zend_get_arg_num(fbc, arg_name);
3768
5.85k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3769
          /* Using named arguments, but passing in order. */
3770
657
          arg_name = NULL;
3771
657
          arg_count++;
3772
5.19k
        } else {
3773
          // TODO: We could track which arguments were passed, even if out of order.
3774
5.19k
          may_have_undef = true;
3775
5.19k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3776
409
            *may_have_extra_named_args = true;
3777
409
          }
3778
5.19k
        }
3779
56.2k
      } else {
3780
56.2k
        arg_num = (uint32_t) -1;
3781
56.2k
        may_have_undef = true;
3782
56.2k
        *may_have_extra_named_args = true;
3783
56.2k
      }
3784
5.25M
    } else {
3785
5.25M
      if (uses_arg_unpack) {
3786
23
        zend_error_noreturn(E_COMPILE_ERROR,
3787
23
          "Cannot use positional argument after argument unpacking");
3788
23
      }
3789
3790
5.25M
      if (uses_named_args) {
3791
69
        zend_error_noreturn(E_COMPILE_ERROR,
3792
69
          "Cannot use positional argument after named argument");
3793
69
      }
3794
3795
5.25M
      arg_count++;
3796
5.25M
    }
3797
3798
    /* Treat passing of $GLOBALS the same as passing a call.
3799
     * This will error at runtime if the argument is by-ref. */
3800
5.31M
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3801
165k
      zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3802
165k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3803
        /* Function call was converted into builtin instruction */
3804
4.39k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3805
1.82k
          opcode = ZEND_SEND_VAL_EX;
3806
2.57k
        } else {
3807
2.57k
          opcode = ZEND_SEND_VAL;
3808
2.57k
        }
3809
161k
      } else {
3810
161k
        if (fbc && arg_num != (uint32_t) -1) {
3811
42.1k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3812
580
            opcode = ZEND_SEND_VAR_NO_REF;
3813
41.5k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3814
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3815
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3816
             * and a by-value pass if it returned by-value. */
3817
90
            opcode = ZEND_SEND_VAL;
3818
41.4k
          } else {
3819
41.4k
            opcode = ZEND_SEND_VAR;
3820
41.4k
          }
3821
118k
        } else {
3822
118k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3823
118k
        }
3824
161k
      }
3825
5.14M
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3826
120k
      if (fbc && arg_num != (uint32_t) -1) {
3827
90.2k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3828
2.52k
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3829
2.52k
          opcode = ZEND_SEND_REF;
3830
87.7k
        } else {
3831
87.7k
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3832
87.7k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3833
87.7k
        }
3834
90.2k
      } else {
3835
30.6k
        do {
3836
30.6k
          if (arg->kind == ZEND_AST_VAR) {
3837
16.8k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3838
16.8k
            if (is_this_fetch(arg)) {
3839
1.35k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3840
1.35k
              opcode = ZEND_SEND_VAR_EX;
3841
1.35k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3842
1.35k
              break;
3843
15.5k
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3844
15.2k
              opcode = ZEND_SEND_VAR_EX;
3845
15.2k
              break;
3846
15.2k
            }
3847
16.8k
          }
3848
14.0k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3849
14.0k
          if (arg_name) {
3850
9.28k
            opline->op2_type = IS_CONST;
3851
9.28k
            zend_string_addref(arg_name);
3852
9.28k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3853
9.28k
            opline->result.num = zend_alloc_cache_slots(2);
3854
9.28k
          } else {
3855
4.74k
            opline->op2.num = arg_num;
3856
4.74k
          }
3857
14.0k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3858
14.0k
          opcode = ZEND_SEND_FUNC_ARG;
3859
14.0k
        } while (0);
3860
30.6k
      }
3861
5.02M
    } else {
3862
5.02M
      zend_compile_expr(&arg_node, arg);
3863
5.02M
      if (arg_node.op_type == IS_VAR) {
3864
        /* pass ++$a or something similar */
3865
612k
        if (fbc && arg_num != (uint32_t) -1) {
3866
4.10k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3867
22
            opcode = ZEND_SEND_VAR_NO_REF;
3868
4.08k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3869
68
            opcode = ZEND_SEND_VAL;
3870
4.01k
          } else {
3871
4.01k
            opcode = ZEND_SEND_VAR;
3872
4.01k
          }
3873
608k
        } else {
3874
608k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3875
608k
        }
3876
4.41M
      } else if (arg_node.op_type == IS_CV) {
3877
0
        if (fbc && arg_num != (uint32_t) -1) {
3878
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3879
0
            opcode = ZEND_SEND_REF;
3880
0
          } else {
3881
0
            opcode = ZEND_SEND_VAR;
3882
0
          }
3883
0
        } else {
3884
0
          opcode = ZEND_SEND_VAR_EX;
3885
0
        }
3886
4.41M
      } else {
3887
        /* Delay "Only variables can be passed by reference" error to execution */
3888
4.41M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3889
130k
          opcode = ZEND_SEND_VAL;
3890
4.28M
        } else {
3891
4.28M
          opcode = ZEND_SEND_VAL_EX;
3892
4.28M
        }
3893
4.41M
      }
3894
5.02M
    }
3895
3896
5.31M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3897
5.31M
    if (arg_name) {
3898
61.4k
      opline->op2_type = IS_CONST;
3899
61.4k
      zend_string_addref(arg_name);
3900
61.4k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3901
61.4k
      opline->result.num = zend_alloc_cache_slots(2);
3902
5.25M
    } else {
3903
5.25M
      opline->op2.opline_num = arg_num;
3904
5.25M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3905
5.25M
    }
3906
5.31M
  }
3907
3908
3.99M
  if (may_have_undef) {
3909
53.1k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3910
53.1k
  }
3911
3912
3.99M
  return arg_count;
3913
3.99M
}
3914
/* }}} */
3915
3916
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3917
4.10M
{
3918
4.10M
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3919
3920
4.10M
  if (fbc && init_op->opcode != ZEND_NEW) {
3921
259k
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3922
259k
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3923
211k
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3924
25.6k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3925
25.6k
          return ZEND_DO_ICALL;
3926
25.6k
        } else {
3927
10
          return ZEND_DO_FCALL_BY_NAME;
3928
10
        }
3929
25.6k
      }
3930
211k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3931
48.1k
      if (zend_execute_ex == execute_ex) {
3932
23.3k
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3933
23.1k
          return ZEND_DO_UCALL;
3934
23.1k
        } else {
3935
235
          return ZEND_DO_FCALL_BY_NAME;
3936
235
        }
3937
23.3k
      }
3938
48.1k
    }
3939
3.84M
  } else if (zend_execute_ex == execute_ex &&
3940
3.76M
             !zend_execute_internal &&
3941
3.71M
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3942
3.66M
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3943
3.40M
    return ZEND_DO_FCALL_BY_NAME;
3944
3.40M
  }
3945
656k
  return ZEND_DO_FCALL;
3946
4.10M
}
3947
/* }}} */
3948
3949
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno) /* {{{ */
3950
4.00M
{
3951
4.00M
  zend_op *opline;
3952
4.00M
  uint32_t opnum_init = get_next_op_number() - 1;
3953
3954
4.00M
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3955
2.95k
    opline = &CG(active_op_array)->opcodes[opnum_init];
3956
2.95k
    opline->extended_value = 0;
3957
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
3958
2.95k
    uint8_t opcode = opline->opcode;
3959
3960
2.95k
    if (opcode == ZEND_NEW) {
3961
18
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3962
18
    }
3963
3964
2.93k
    if (opcode == ZEND_INIT_FCALL) {
3965
684
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3966
684
    }
3967
3968
2.93k
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3969
2.93k
    if (opcode == ZEND_INIT_FCALL
3970
2.25k
     || opcode == ZEND_INIT_FCALL_BY_NAME
3971
2.40k
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
3972
2.40k
      callable_convert_op->extended_value = zend_alloc_cache_slot();
3973
2.40k
    } else {
3974
534
      callable_convert_op->extended_value = (uint32_t)-1;
3975
534
    }
3976
2.93k
    return true;
3977
2.95k
  }
3978
3979
4.00M
  bool may_have_extra_named_args;
3980
3.99M
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3981
3982
3.99M
  zend_do_extended_fcall_begin();
3983
3984
3.99M
  opline = &CG(active_op_array)->opcodes[opnum_init];
3985
3.99M
  opline->extended_value = arg_count;
3986
3987
3.99M
  if (opline->opcode == ZEND_INIT_FCALL) {
3988
140k
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3989
140k
  }
3990
3991
3.99M
  uint8_t call_op = zend_get_call_op(
3992
3.99M
    opline,
3993
3.99M
    fbc,
3994
    /* result_used: At this point we do not yet reliably
3995
     * know if the result is used. Deoptimize #[\NoDiscard]
3996
     * calls to be sure. The optimizer will fix this up.
3997
     */
3998
3.99M
    false
3999
3.99M
  );
4000
3.99M
  opline = zend_emit_op(result, call_op, NULL, NULL);
4001
3.99M
  if (may_have_extra_named_args) {
4002
50.8k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4003
50.8k
  }
4004
3.99M
  opline->lineno = lineno;
4005
3.99M
  zend_do_extended_fcall_end();
4006
3.99M
  return false;
4007
4.00M
}
4008
/* }}} */
4009
4010
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4011
3.59M
{
4012
3.59M
  zend_string *orig_name = zend_ast_get_str(name_ast);
4013
3.59M
  bool is_fully_qualified;
4014
4015
3.59M
  name_node->op_type = IS_CONST;
4016
3.59M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4017
3.59M
    orig_name, name_ast->attr, &is_fully_qualified));
4018
4019
3.59M
  return !is_fully_qualified && FC(current_namespace);
4020
3.59M
}
4021
/* }}} */
4022
4023
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
4024
294k
{
4025
294k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4026
59.9k
    const char *colon;
4027
59.9k
    zend_string *str = Z_STR(name_node->u.constant);
4028
59.9k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4029
413
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4030
413
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4031
413
      zend_op *opline = get_next_op();
4032
4033
413
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4034
413
      opline->op1_type = IS_CONST;
4035
413
      opline->op1.constant = zend_add_class_name_literal(class);
4036
413
      opline->op2_type = IS_CONST;
4037
413
      opline->op2.constant = zend_add_func_name_literal(method);
4038
      /* 2 slots, for class and method */
4039
413
      opline->result.num = zend_alloc_cache_slots(2);
4040
413
      zval_ptr_dtor(&name_node->u.constant);
4041
59.5k
    } else {
4042
59.5k
      zend_op *opline = get_next_op();
4043
4044
59.5k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4045
59.5k
      opline->op2_type = IS_CONST;
4046
59.5k
      opline->op2.constant = zend_add_func_name_literal(str);
4047
59.5k
      opline->result.num = zend_alloc_cache_slot();
4048
59.5k
    }
4049
234k
  } else {
4050
234k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4051
234k
  }
4052
4053
294k
  zend_compile_call_common(result, args_ast, NULL, lineno);
4054
294k
}
4055
/* }}} */
4056
4057
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4058
3.48M
{
4059
3.48M
  uint32_t i;
4060
8.32M
  for (i = 0; i < args->children; ++i) {
4061
4.88M
    const zend_ast *arg = args->child[i];
4062
4.88M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4063
47.8k
      return 1;
4064
47.8k
    }
4065
4.88M
  }
4066
3.43M
  return 0;
4067
3.48M
}
4068
/* }}} */
4069
4070
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4071
893
{
4072
893
  znode arg_node;
4073
4074
893
  if (args->children != 1) {
4075
212
    return FAILURE;
4076
212
  }
4077
4078
681
  zend_compile_expr(&arg_node, args->child[0]);
4079
681
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4080
175
    result->op_type = IS_CONST;
4081
175
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4082
175
    zval_ptr_dtor_str(&arg_node.u.constant);
4083
506
  } else {
4084
506
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4085
506
  }
4086
681
  return SUCCESS;
4087
893
}
4088
/* }}} */
4089
4090
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4091
1.59k
{
4092
1.59k
  znode arg_node;
4093
1.59k
  zend_op *opline;
4094
4095
1.59k
  if (args->children != 1) {
4096
535
    return FAILURE;
4097
535
  }
4098
4099
1.05k
  zend_compile_expr(&arg_node, args->child[0]);
4100
1.05k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4101
1.05k
  if (type != _IS_BOOL) {
4102
1.03k
    opline->extended_value = (1 << type);
4103
1.03k
  } else {
4104
23
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4105
23
  }
4106
1.05k
  return SUCCESS;
4107
1.59k
}
4108
/* }}} */
4109
4110
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4111
88
{
4112
88
  znode arg_node;
4113
88
  zend_op *opline;
4114
4115
88
  if (args->children != 1) {
4116
10
    return FAILURE;
4117
10
  }
4118
4119
78
  zend_compile_expr(&arg_node, args->child[0]);
4120
78
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4121
78
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4122
78
  return SUCCESS;
4123
88
}
4124
4125
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4126
1.12k
{
4127
1.12k
  znode arg_node;
4128
1.12k
  zend_op *opline;
4129
4130
1.12k
  if (args->children != 1) {
4131
160
    return FAILURE;
4132
160
  }
4133
4134
964
  zend_compile_expr(&arg_node, args->child[0]);
4135
964
  if (type == _IS_BOOL) {
4136
265
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4137
699
  } else {
4138
699
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4139
699
    opline->extended_value = type;
4140
699
  }
4141
964
  return SUCCESS;
4142
1.12k
}
4143
/* }}} */
4144
4145
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4146
1.86k
{
4147
1.86k
  zend_string *name;
4148
1.86k
  zend_op *opline;
4149
4150
1.86k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4151
173
    return FAILURE;
4152
173
  }
4153
4154
1.69k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4155
1.69k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4156
226
    zend_string_release_ex(name, 0);
4157
226
    return FAILURE;
4158
226
  }
4159
4160
1.46k
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4161
72
    zend_string_release_ex(name, 0);
4162
72
    zval_ptr_dtor(&result->u.constant);
4163
72
    ZVAL_TRUE(&result->u.constant);
4164
72
    result->op_type = IS_CONST;
4165
72
    return SUCCESS;
4166
72
  }
4167
4168
1.39k
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4169
1.39k
  opline->op1_type = IS_CONST;
4170
1.39k
  LITERAL_STR(opline->op1, name);
4171
1.39k
  opline->extended_value = zend_alloc_cache_slot();
4172
4173
1.39k
  return SUCCESS;
4174
1.46k
}
4175
/* }}} */
4176
4177
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4178
1.06k
{
4179
1.06k
  zval *zint;
4180
1.06k
  if (
4181
1.06k
    args->children == 1
4182
963
    && args->child[0]->kind == ZEND_AST_ZVAL
4183
751
    && (zint = zend_ast_get_zval(args->child[0]))
4184
751
    && Z_TYPE_P(zint) == IS_LONG
4185
651
    && Z_LVAL_P(zint) >= 0
4186
651
    && Z_LVAL_P(zint) <= 255
4187
1.06k
  ) {
4188
296
    result->op_type = IS_CONST;
4189
296
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4190
296
    return SUCCESS;
4191
771
  } else {
4192
771
    return FAILURE;
4193
771
  }
4194
1.06k
}
4195
/* }}} */
4196
4197
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4198
648
{
4199
648
  zval *str;
4200
648
  if (
4201
648
    args->children == 1
4202
423
    && args->child[0]->kind == ZEND_AST_ZVAL
4203
323
    && (str = zend_ast_get_zval(args->child[0]))
4204
323
    && Z_TYPE_P(str) == IS_STRING
4205
211
    && Z_STRLEN_P(str) == 1
4206
648
  ) {
4207
12
    result->op_type = IS_CONST;
4208
12
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4209
12
    return SUCCESS;
4210
636
  } else {
4211
636
    return FAILURE;
4212
636
  }
4213
648
}
4214
/* }}} */
4215
4216
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4217
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4218
 * directly or indirectly recursive function calls. */
4219
173k
static bool fbc_is_finalized(const zend_function *fbc) {
4220
173k
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4221
173k
}
4222
4223
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4224
73.7k
{
4225
73.7k
  if (ce->type == ZEND_INTERNAL_CLASS) {
4226
51.7k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4227
51.7k
  } else {
4228
21.9k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4229
15.9k
      && ce->info.user.filename != filename;
4230
21.9k
  }
4231
73.7k
}
4232
4233
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4234
153k
{
4235
153k
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4236
130k
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4237
130k
  } else {
4238
22.9k
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4239
22.9k
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4240
20.3k
        && fbc->op_array.filename != filename);
4241
22.9k
  }
4242
153k
}
4243
4244
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4245
10.6k
{
4246
10.6k
  zend_string *name, *lcname;
4247
10.6k
  zend_function *fbc;
4248
10.6k
  zend_op *opline;
4249
4250
10.6k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4251
2.52k
    return FAILURE;
4252
2.52k
  }
4253
4254
8.15k
  name = zend_ast_get_str(name_ast);
4255
8.15k
  lcname = zend_string_tolower(name);
4256
4257
8.15k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4258
8.15k
  if (!fbc
4259
837
   || !fbc_is_finalized(fbc)
4260
7.31k
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4261
7.31k
    zend_string_release_ex(lcname, 0);
4262
7.31k
    return FAILURE;
4263
7.31k
  }
4264
4265
837
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4266
837
  opline->extended_value = num_args;
4267
837
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4268
837
  opline->op2_type = IS_CONST;
4269
837
  LITERAL_STR(opline->op2, lcname);
4270
837
  opline->result.num = zend_alloc_cache_slot();
4271
4272
837
  return SUCCESS;
4273
8.15k
}
4274
/* }}} */
4275
4276
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4277
10.6k
{
4278
10.6k
  zend_op *opline;
4279
10.6k
  znode name_node;
4280
4281
10.6k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4282
837
    return;
4283
837
  }
4284
4285
9.84k
  zend_compile_expr(&name_node, name_ast);
4286
4287
9.84k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4288
9.84k
  opline->op1_type = IS_CONST;
4289
9.84k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4290
9.84k
  opline->extended_value = num_args;
4291
9.84k
}
4292
/* }}} */
4293
4294
/* cufa = call_user_func_array */
4295
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4296
2.21k
{
4297
2.21k
  znode arg_node;
4298
2.21k
  zend_op *opline;
4299
4300
2.21k
  if (args->children != 2) {
4301
89
    return FAILURE;
4302
89
  }
4303
4304
2.12k
  zend_compile_init_user_func(args->child[0], 0, lcname);
4305
2.12k
  if (args->child[1]->kind == ZEND_AST_CALL
4306
1.55k
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4307
1.47k
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4308
1.34k
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4309
1.33k
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4310
1.33k
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4311
1.33k
    bool is_fully_qualified;
4312
1.33k
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4313
4314
1.33k
    if (zend_string_equals_literal_ci(name, "array_slice")
4315
912
       && !zend_args_contain_unpack_or_named(list)
4316
760
     && list->children == 3
4317
477
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4318
454
      zval *zv = zend_ast_get_zval(list->child[1]);
4319
4320
454
      if (Z_TYPE_P(zv) == IS_LONG
4321
319
       && Z_LVAL_P(zv) >= 0
4322
319
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4323
129
        zend_op *opline;
4324
129
        znode len_node;
4325
4326
129
        zend_compile_expr(&arg_node, list->child[0]);
4327
129
        zend_compile_expr(&len_node, list->child[2]);
4328
129
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4329
129
        opline->extended_value = Z_LVAL_P(zv);
4330
129
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4331
129
        zend_string_release_ex(name, 0);
4332
129
        return SUCCESS;
4333
129
      }
4334
454
    }
4335
1.20k
    zend_string_release_ex(name, 0);
4336
1.20k
  }
4337
1.99k
  zend_compile_expr(&arg_node, args->child[1]);
4338
1.99k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4339
1.99k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4340
1.99k
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4341
1.99k
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4342
4343
1.99k
  return SUCCESS;
4344
2.12k
}
4345
/* }}} */
4346
4347
/* cuf = call_user_func */
4348
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname) /* {{{ */
4349
8.61k
{
4350
8.61k
  uint32_t i;
4351
4352
8.61k
  if (args->children < 1) {
4353
66
    return FAILURE;
4354
66
  }
4355
4356
8.55k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4357
11.4k
  for (i = 1; i < args->children; ++i) {
4358
2.92k
    zend_ast *arg_ast = args->child[i];
4359
2.92k
    znode arg_node;
4360
2.92k
    zend_op *opline;
4361
4362
2.92k
    zend_compile_expr(&arg_node, arg_ast);
4363
4364
2.92k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4365
2.92k
    opline->op2.num = i;
4366
2.92k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4367
2.92k
  }
4368
8.55k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4369
4370
8.55k
  return SUCCESS;
4371
8.61k
}
4372
/* }}} */
4373
4374
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno) /* {{{ */
4375
22.7k
{
4376
22.7k
  if (EG(assertions) >= 0) {
4377
22.7k
    znode name_node;
4378
22.7k
    zend_op *opline;
4379
22.7k
    uint32_t check_op_number = get_next_op_number();
4380
4381
22.7k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4382
4383
22.7k
    if (fbc && fbc_is_finalized(fbc)) {
4384
20.3k
      name_node.op_type = IS_CONST;
4385
20.3k
      ZVAL_STR_COPY(&name_node.u.constant, name);
4386
4387
20.3k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4388
20.3k
    } else {
4389
2.32k
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4390
2.32k
      opline->op2_type = IS_CONST;
4391
2.32k
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4392
2.32k
    }
4393
22.7k
    opline->result.num = zend_alloc_cache_slot();
4394
4395
22.7k
    if (args->children == 1) {
4396
      /* add "assert(condition) as assertion message */
4397
20.5k
      zend_ast *arg = zend_ast_create_zval_from_str(
4398
20.5k
        zend_ast_export("assert(", args->child[0], ")"));
4399
20.5k
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4400
        /* If the original argument was named, add the new argument as named as well,
4401
         * as mixing named and positional is not allowed. */
4402
231
        zend_ast *name = zend_ast_create_zval_from_str(
4403
231
          ZSTR_INIT_LITERAL("description", 0));
4404
231
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4405
231
      }
4406
20.5k
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4407
20.5k
    }
4408
4409
22.7k
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4410
4411
22.7k
    opline = &CG(active_op_array)->opcodes[check_op_number];
4412
22.7k
    opline->op2.opline_num = get_next_op_number();
4413
22.7k
    SET_NODE(opline->result, result);
4414
22.7k
  } else {
4415
0
    if (!fbc) {
4416
0
      zend_string_release_ex(name, 0);
4417
0
    }
4418
0
    result->op_type = IS_CONST;
4419
0
    ZVAL_TRUE(&result->u.constant);
4420
0
  }
4421
22.7k
}
4422
/* }}} */
4423
4424
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4425
3.25k
{
4426
3.25k
  bool strict = false;
4427
3.25k
  znode array, needly;
4428
3.25k
  zend_op *opline;
4429
4430
3.25k
  if (args->children == 3) {
4431
1.27k
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4432
663
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4433
663
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4434
537
      zval value;
4435
537
      zend_ast *name_ast = args->child[2]->child[0];
4436
537
      bool is_fully_qualified;
4437
537
      zend_string *resolved_name = zend_resolve_const_name(
4438
537
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4439
4440
537
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4441
354
        zend_string_release_ex(resolved_name, 0);
4442
354
        return FAILURE;
4443
354
      }
4444
4445
183
      zend_string_release_ex(resolved_name, 0);
4446
183
      strict = zend_is_true(&value);
4447
183
      zval_ptr_dtor(&value);
4448
183
    } else {
4449
72
      return FAILURE;
4450
72
    }
4451
1.97k
  } else if (args->children != 2) {
4452
322
    return FAILURE;
4453
322
  }
4454
4455
2.50k
  if (args->child[1]->kind != ZEND_AST_ARRAY
4456
1.70k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4457
874
    return FAILURE;
4458
874
  }
4459
4460
1.62k
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4461
1.46k
    bool ok = true;
4462
1.46k
    zval *val, tmp;
4463
1.46k
    HashTable *src = Z_ARRVAL(array.u.constant);
4464
1.46k
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4465
4466
1.46k
    ZVAL_TRUE(&tmp);
4467
4468
1.46k
    if (strict) {
4469
4.18k
      ZEND_HASH_FOREACH_VAL(src, val) {
4470
4.18k
        if (Z_TYPE_P(val) == IS_STRING) {
4471
20
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4472
1.20k
        } else if (Z_TYPE_P(val) == IS_LONG) {
4473
1.14k
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4474
1.14k
        } else {
4475
59
          zend_array_destroy(dst);
4476
59
          ok = false;
4477
59
          break;
4478
59
        }
4479
4.18k
      } ZEND_HASH_FOREACH_END();
4480
826
    } else {
4481
4.24k
      ZEND_HASH_FOREACH_VAL(src, val) {
4482
4.24k
        if (Z_TYPE_P(val) != IS_STRING
4483
848
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4484
606
          zend_array_destroy(dst);
4485
606
          ok = false;
4486
606
          break;
4487
606
        }
4488
703
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4489
703
      } ZEND_HASH_FOREACH_END();
4490
826
    }
4491
4492
1.46k
    zend_array_destroy(src);
4493
1.46k
    if (!ok) {
4494
665
      return FAILURE;
4495
665
    }
4496
798
    Z_ARRVAL(array.u.constant) = dst;
4497
798
  }
4498
963
  array.op_type = IS_CONST;
4499
4500
963
  zend_compile_expr(&needly, args->child[0]);
4501
4502
963
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4503
963
  opline->extended_value = strict;
4504
4505
963
  return SUCCESS;
4506
1.62k
}
4507
/* }}} */
4508
4509
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4510
996
{
4511
996
  znode arg_node;
4512
996
  zend_op *opline;
4513
4514
996
  if (args->children != 1) {
4515
70
    return FAILURE;
4516
70
  }
4517
4518
926
  zend_compile_expr(&arg_node, args->child[0]);
4519
926
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4520
926
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4521
4522
926
  return SUCCESS;
4523
996
}
4524
/* }}} */
4525
4526
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4527
1.10k
{
4528
1.10k
  if (args->children == 0) {
4529
38
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4530
1.06k
  } else {
4531
1.06k
    znode arg_node;
4532
4533
1.06k
    if (args->children != 1) {
4534
66
      return FAILURE;
4535
66
    }
4536
4537
1.00k
    zend_compile_expr(&arg_node, args->child[0]);
4538
1.00k
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4539
1.00k
  }
4540
1.03k
  return SUCCESS;
4541
1.10k
}
4542
/* }}} */
4543
4544
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4545
305
{
4546
305
  if (args->children != 0) {
4547
74
    return FAILURE;
4548
74
  }
4549
4550
231
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4551
231
  return SUCCESS;
4552
305
}
4553
/* }}} */
4554
4555
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4556
799
{
4557
799
  znode arg_node;
4558
4559
799
  if (args->children != 1) {
4560
22
    return FAILURE;
4561
22
  }
4562
4563
777
  zend_compile_expr(&arg_node, args->child[0]);
4564
777
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4565
777
  return SUCCESS;
4566
799
}
4567
/* }}} */
4568
4569
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4570
178
{
4571
178
  if (CG(active_op_array)->function_name && args->children == 0) {
4572
85
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4573
85
    return SUCCESS;
4574
93
  } else {
4575
93
    return FAILURE;
4576
93
  }
4577
178
}
4578
/* }}} */
4579
4580
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4581
577
{
4582
577
  if (CG(active_op_array)->function_name && args->children == 0) {
4583
497
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4584
497
    return SUCCESS;
4585
497
  } else {
4586
80
    return FAILURE;
4587
80
  }
4588
577
}
4589
/* }}} */
4590
4591
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4592
260
{
4593
260
  znode subject, needle;
4594
4595
260
  if (args->children != 2) {
4596
73
    return FAILURE;
4597
73
  }
4598
4599
187
  zend_compile_expr(&needle, args->child[0]);
4600
187
  zend_compile_expr(&subject, args->child[1]);
4601
4602
187
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4603
187
  return SUCCESS;
4604
260
}
4605
/* }}} */
4606
4607
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4608
1.38k
{
4609
1.38k
  if (CG(active_op_array)->function_name
4610
632
   && args->children == 2
4611
522
   && args->child[0]->kind == ZEND_AST_CALL
4612
491
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4613
403
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4614
403
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4615
403
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4616
4617
311
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4618
311
    bool is_fully_qualified;
4619
311
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4620
311
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4621
311
    const zval *zv = zend_ast_get_zval(args->child[1]);
4622
311
    znode first;
4623
4624
311
    if (zend_string_equals_literal_ci(name, "func_get_args")
4625
190
     && list->children == 0
4626
170
     && Z_TYPE_P(zv) == IS_LONG
4627
106
     && Z_LVAL_P(zv) >= 0) {
4628
106
      first.op_type = IS_CONST;
4629
106
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4630
106
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4631
106
      zend_string_release_ex(name, 0);
4632
106
      return SUCCESS;
4633
106
    }
4634
205
    zend_string_release_ex(name, 0);
4635
205
  }
4636
1.28k
  return FAILURE;
4637
1.38k
}
4638
/* }}} */
4639
4640
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4641
1.18M
{
4642
1.18M
  void **handlers = zend_flf_handlers;
4643
1.18M
  void **current = handlers;
4644
9.66M
  while (current) {
4645
9.66M
    if (*current == handler) {
4646
1.18M
      return current - handlers;
4647
1.18M
    }
4648
8.48M
    current++;
4649
8.48M
  }
4650
4651
0
  return (uint32_t)-1;
4652
1.18M
}
4653
4654
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4655
881k
{
4656
881k
  if (zend_execute_internal) {
4657
93.8k
    return NULL;
4658
93.8k
  }
4659
4660
788k
  if (type != BP_VAR_R) {
4661
1.86k
    return NULL;
4662
1.86k
  }
4663
4664
786k
  if (ZEND_USER_CODE(fbc->type)) {
4665
10
    return NULL;
4666
10
  }
4667
4668
786k
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4669
786k
  if (!frameless_function_info) {
4670
17.3k
    return NULL;
4671
17.3k
  }
4672
4673
768k
  if (args->children > 3) {
4674
431
    return NULL;
4675
431
  }
4676
4677
1.22M
  while (frameless_function_info->handler) {
4678
1.04M
    if (frameless_function_info->num_args >= args->children
4679
943k
     && fbc->common.required_num_args <= args->children
4680
592k
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4681
591k
      || frameless_function_info->num_args == args->children)) {
4682
591k
      uint32_t num_args = frameless_function_info->num_args;
4683
591k
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4684
591k
      if (offset == (uint32_t)-1) {
4685
0
        continue;
4686
0
      }
4687
591k
      return frameless_function_info;
4688
591k
    }
4689
458k
    frameless_function_info++;
4690
458k
  }
4691
4692
177k
  return NULL;
4693
768k
}
4694
4695
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)
4696
590k
{
4697
590k
  uint32_t lineno = CG(zend_lineno);
4698
590k
  uint32_t num_args = frameless_function_info->num_args;
4699
590k
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4700
590k
  znode arg_zvs[3];
4701
1.87M
  for (uint32_t i = 0; i < num_args; i++) {
4702
1.28M
    if (i < args->children) {
4703
1.28M
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4704
1.28M
    } else {
4705
0
      const zend_internal_arg_info *arg_info = (const zend_internal_arg_info *)&fbc->common.arg_info[i];
4706
0
      arg_zvs[i].op_type = IS_CONST;
4707
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4708
0
        ZEND_UNREACHABLE();
4709
0
      }
4710
0
    }
4711
1.28M
  }
4712
590k
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4713
590k
  uint32_t opnum = get_next_op_number();
4714
590k
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4715
590k
  opline->extended_value = offset;
4716
590k
  opline->lineno = lineno;
4717
590k
  if (num_args >= 1) {
4718
590k
    SET_NODE(opline->op1, &arg_zvs[0]);
4719
590k
  }
4720
590k
  if (num_args >= 2) {
4721
590k
    SET_NODE(opline->op2, &arg_zvs[1]);
4722
590k
  }
4723
590k
  if (num_args >= 3) {
4724
105k
    zend_emit_op_data(&arg_zvs[2]);
4725
105k
  }
4726
590k
  return opnum;
4727
590k
}
4728
4729
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4730
103k
{
4731
103k
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4732
103k
  if (!frameless_function_info) {
4733
101k
    return (uint32_t)-1;
4734
101k
  }
4735
4736
1.99k
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4737
103k
}
4738
4739
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4740
3.35M
{
4741
3.35M
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4742
4743
  /* Find frameless function with same name. */
4744
3.35M
  const zend_function *frameless_function = NULL;
4745
3.35M
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4746
3.35M
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4747
   /* Avoid blowing up op count with nested frameless branches. */
4748
3.31M
   && !CG(context).in_jmp_frameless_branch) {
4749
2.13M
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4750
2.13M
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4751
2.13M
  }
4752
4753
  /* Check whether any frameless handler may actually be used. */
4754
3.35M
  uint32_t jmp_fl_opnum = 0;
4755
3.35M
  const zend_frameless_function_info *frameless_function_info = NULL;
4756
3.35M
  if (frameless_function) {
4757
778k
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4758
778k
    if (frameless_function_info) {
4759
589k
      CG(context).in_jmp_frameless_branch = true;
4760
589k
      znode op1;
4761
589k
      op1.op_type = IS_CONST;
4762
589k
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4763
589k
      jmp_fl_opnum = get_next_op_number();
4764
589k
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4765
589k
    }
4766
778k
  }
4767
4768
  /* Compile ns call. */
4769
3.35M
  zend_op *opline = get_next_op();
4770
3.35M
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4771
3.35M
  opline->op2_type = IS_CONST;
4772
3.35M
  opline->op2.constant = name_constants;
4773
3.35M
  opline->result.num = zend_alloc_cache_slot();
4774
3.35M
  zend_compile_call_common(result, args_ast, NULL, lineno);
4775
4776
  /* Compile frameless call. */
4777
3.35M
  if (frameless_function_info) {
4778
588k
    CG(zend_lineno) = lineno;
4779
4780
588k
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4781
588k
    uint32_t jmp_fl_target = get_next_op_number();
4782
4783
588k
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4784
4785
588k
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4786
588k
    jmp_fl->op2.opline_num = jmp_fl_target;
4787
588k
    jmp_fl->extended_value = zend_alloc_cache_slot();
4788
588k
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4789
588k
    SET_NODE(flf_icall->result, result);
4790
588k
    zend_update_jump_target_to_next(jmp_end_opnum);
4791
4792
588k
    CG(context).in_jmp_frameless_branch = false;
4793
588k
  }
4794
3.35M
}
4795
/* }}} */
4796
4797
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4798
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4799
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4800
4801
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4802
4.60k
{
4803
  /* Bail out if we do not have a format string. */
4804
4.60k
  if (args->children < 1) {
4805
316
    return FAILURE;
4806
316
  }
4807
4808
4.28k
  zend_eval_const_expr(&args->child[0]);
4809
  /* Bail out if the format string is not constant. */
4810
4.28k
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4811
313
    return FAILURE;
4812
313
  }
4813
4814
3.97k
  zval *format_string = zend_ast_get_zval(args->child[0]);
4815
3.97k
  if (Z_TYPE_P(format_string) != IS_STRING) {
4816
205
    return FAILURE;
4817
205
  }
4818
3.76k
  if (Z_STRLEN_P(format_string) >= 256) {
4819
29
    return FAILURE;
4820
29
  }
4821
4822
3.74k
  char *p;
4823
3.74k
  char *end;
4824
3.74k
  uint32_t placeholder_count;
4825
4826
3.74k
  placeholder_count = 0;
4827
3.74k
  p = Z_STRVAL_P(format_string);
4828
3.74k
  end = p + Z_STRLEN_P(format_string);
4829
4830
9.20k
  for (;;) {
4831
9.20k
    p = memchr(p, '%', end - p);
4832
9.20k
    if (!p) {
4833
3.41k
      break;
4834
3.41k
    }
4835
4836
5.78k
    char *q = p + 1;
4837
5.78k
    if (q == end) {
4838
84
      return FAILURE;
4839
84
    }
4840
4841
5.70k
    switch (*q) {
4842
3.43k
      case 's':
4843
3.97k
      case 'd':
4844
3.97k
        placeholder_count++;
4845
3.97k
        break;
4846
1.49k
      case '%':
4847
1.49k
        break;
4848
242
      default:
4849
242
        return FAILURE;
4850
5.70k
    }
4851
4852
5.46k
    p = q;
4853
5.46k
    p++;
4854
5.46k
  }
4855
4856
  /* Bail out if the number of placeholders does not match the number of values. */
4857
3.41k
  if (placeholder_count != (args->children - 1)) {
4858
191
    return FAILURE;
4859
191
  }
4860
4861
  /* Handle empty format strings. */
4862
3.22k
  if (Z_STRLEN_P(format_string) == 0) {
4863
68
    result->op_type = IS_CONST;
4864
68
    ZVAL_EMPTY_STRING(&result->u.constant);
4865
4866
68
    return SUCCESS;
4867
68
  }
4868
4869
3.15k
  znode *elements = NULL;
4870
4871
3.15k
  if (placeholder_count > 0) {
4872
2.59k
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4873
2.59k
  }
4874
4875
  /* Compile the value expressions first for error handling that is consistent
4876
   * with a function call: Values that fail to convert to a string may emit errors.
4877
   */
4878
6.76k
  for (uint32_t i = 0; i < placeholder_count; i++) {
4879
3.60k
    zend_compile_expr(elements + i, args->child[1 + i]);
4880
3.60k
  }
4881
4882
3.15k
  uint32_t rope_elements = 0;
4883
3.15k
  uint32_t rope_init_lineno = -1;
4884
3.15k
  zend_op *opline = NULL;
4885
4886
3.15k
  placeholder_count = 0;
4887
3.15k
  p = Z_STRVAL_P(format_string);
4888
3.15k
  end = p + Z_STRLEN_P(format_string);
4889
3.15k
  char *offset = p;
4890
7.69k
  for (;;) {
4891
7.69k
    p = memchr(p, '%', end - p);
4892
7.69k
    if (!p) {
4893
3.15k
      break;
4894
3.15k
    }
4895
4896
4.54k
    char *q = p + 1;
4897
4.54k
    ZEND_ASSERT(q < end);
4898
4.54k
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4899
4900
4.54k
    if (*q == '%') {
4901
      /* Optimization to not create a dedicated rope element for the literal '%':
4902
       * Include the first '%' within the "constant" part instead of dropping the
4903
       * full placeholder.
4904
       */
4905
933
      p++;
4906
933
    }
4907
4908
4.54k
    if (p != offset) {
4909
3.31k
      znode const_node;
4910
3.31k
      const_node.op_type = IS_CONST;
4911
3.31k
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4912
3.31k
      if (rope_elements == 0) {
4913
1.79k
        rope_init_lineno = get_next_op_number();
4914
1.79k
      }
4915
3.31k
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4916
3.31k
    }
4917
4918
4.54k
    if (*q != '%') {
4919
3.60k
      switch (*q) {
4920
3.14k
        case 's':
4921
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4922
           * for correct error reporting.
4923
           */
4924
3.14k
          if (elements[placeholder_count].op_type == IS_CONST) {
4925
599
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4926
119
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4927
480
            } else {
4928
480
              convert_to_string(&elements[placeholder_count].u.constant);
4929
480
            }
4930
599
          }
4931
3.14k
          break;
4932
462
        case 'd':
4933
462
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4934
462
          break;
4935
0
        EMPTY_SWITCH_DEFAULT_CASE();
4936
3.60k
      }
4937
4938
3.60k
      if (rope_elements == 0) {
4939
969
        rope_init_lineno = get_next_op_number();
4940
969
      }
4941
3.60k
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4942
4943
3.60k
      placeholder_count++;
4944
3.60k
    }
4945
4946
4.54k
    p = q;
4947
4.54k
    p++;
4948
4.54k
    offset = p;
4949
4.54k
  }
4950
3.15k
  if (end != offset) {
4951
    /* Add the constant part after the last placeholder. */
4952
2.68k
    znode const_node;
4953
2.68k
    const_node.op_type = IS_CONST;
4954
2.68k
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4955
2.68k
    if (rope_elements == 0) {
4956
391
      rope_init_lineno = get_next_op_number();
4957
391
    }
4958
2.68k
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4959
2.68k
  }
4960
3.15k
  ZEND_ASSERT(opline != NULL);
4961
4962
3.15k
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4963
3.15k
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4964
3.15k
  efree(elements);
4965
4966
3.15k
  return SUCCESS;
4967
3.15k
}
4968
4969
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
4970
3.38k
{
4971
  /* Special case: printf with a single constant string argument and no format specifiers.
4972
   * In this case, just emit ECHO and return the string length if needed. */
4973
3.38k
  if (args->children == 1) {
4974
1.37k
    zend_eval_const_expr(&args->child[0]);
4975
1.37k
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
4976
772
      return FAILURE;
4977
772
    }
4978
600
    zval *format_string = zend_ast_get_zval(args->child[0]);
4979
600
    if (Z_TYPE_P(format_string) != IS_STRING) {
4980
66
      return FAILURE;
4981
66
    }
4982
    /* Check if there are any format specifiers */
4983
534
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
4984
      /* No format specifiers - just emit ECHO and return string length */
4985
365
      znode format_node;
4986
365
      zend_compile_expr(&format_node, args->child[0]);
4987
365
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
4988
4989
      /* Return the string length as a constant if the result is used */
4990
365
      result->op_type = IS_CONST;
4991
365
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
4992
365
      return SUCCESS;
4993
365
    }
4994
534
  }
4995
4996
  /* Fall back to sprintf optimization for format strings with specifiers */
4997
2.17k
  znode rope_result;
4998
2.17k
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
4999
184
    return FAILURE;
5000
184
  }
5001
5002
  /* printf() returns the amount of bytes written, so just an ECHO of the
5003
   * resulting sprintf() optimisation might not be enough. At this early
5004
   * stage we can't detect if the result is actually used, so we just emit
5005
   * the opcodes and let them be cleaned up by the dead code elimination
5006
   * pass in the Zend Optimizer if the result of the printf() is in fact
5007
   * unused */
5008
1.99k
  znode copy;
5009
1.99k
  if (rope_result.op_type != IS_CONST) {
5010
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5011
1.91k
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5012
1.91k
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5013
1.91k
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5014
1.91k
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5015
1.91k
  } else {
5016
75
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5017
75
    result->op_type = IS_CONST;
5018
75
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5019
75
  }
5020
5021
1.99k
  return SUCCESS;
5022
1.99k
}
5023
5024
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5025
1.69k
{
5026
1.69k
  znode arg_node;
5027
5028
1.69k
  if (args->children != 1) {
5029
312
    return FAILURE;
5030
312
  }
5031
5032
1.38k
  zend_compile_expr(&arg_node, args->child[0]);
5033
1.38k
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5034
5035
1.38k
  return SUCCESS;
5036
1.69k
}
5037
5038
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
5039
128k
{
5040
128k
  if (zend_string_equals_literal(lcname, "strlen")) {
5041
893
    return zend_compile_func_strlen(result, args);
5042
127k
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5043
276
    return zend_compile_func_typecheck(result, args, IS_NULL);
5044
126k
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5045
23
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5046
126k
  } else if (zend_string_equals_literal(lcname, "is_long")
5047
126k
    || zend_string_equals_literal(lcname, "is_int")
5048
126k
    || zend_string_equals_literal(lcname, "is_integer")
5049
126k
  ) {
5050
354
    return zend_compile_func_typecheck(result, args, IS_LONG);
5051
126k
  } else if (zend_string_equals_literal(lcname, "is_float")
5052
126k
    || zend_string_equals_literal(lcname, "is_double")
5053
126k
  ) {
5054
541
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5055
126k
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5056
22
    return zend_compile_func_typecheck(result, args, IS_STRING);
5057
125k
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5058
245
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5059
125k
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5060
82
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5061
125k
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5062
48
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5063
125k
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5064
88
    return zend_compile_func_is_scalar(result, args);
5065
125k
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5066
265
    return zend_compile_func_cast(result, args, _IS_BOOL);
5067
125k
  } else if (zend_string_equals_literal(lcname, "intval")) {
5068
148
    return zend_compile_func_cast(result, args, IS_LONG);
5069
125k
  } else if (zend_string_equals_literal(lcname, "floatval")
5070
124k
    || zend_string_equals_literal(lcname, "doubleval")
5071
125k
  ) {
5072
543
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5073
124k
  } else if (zend_string_equals_literal(lcname, "strval")) {
5074
168
    return zend_compile_func_cast(result, args, IS_STRING);
5075
124k
  } else if (zend_string_equals_literal(lcname, "defined")) {
5076
1.86k
    return zend_compile_func_defined(result, args);
5077
122k
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5078
1.06k
    return zend_compile_func_chr(result, args);
5079
121k
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5080
648
    return zend_compile_func_ord(result, args);
5081
120k
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5082
2.21k
    return zend_compile_func_cufa(result, args, lcname);
5083
118k
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5084
8.61k
    return zend_compile_func_cuf(result, args, lcname);
5085
109k
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5086
3.25k
    return zend_compile_func_in_array(result, args);
5087
106k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5088
105k
      || zend_string_equals_literal(lcname, "sizeof")) {
5089
996
    return zend_compile_func_count(result, args, lcname);
5090
105k
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5091
1.10k
    return zend_compile_func_get_class(result, args);
5092
104k
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5093
305
    return zend_compile_func_get_called_class(result, args);
5094
104k
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5095
799
    return zend_compile_func_gettype(result, args);
5096
103k
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5097
178
    return zend_compile_func_num_args(result, args);
5098
103k
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5099
577
    return zend_compile_func_get_args(result, args);
5100
102k
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5101
1.38k
    return zend_compile_func_array_slice(result, args);
5102
101k
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5103
260
    return zend_compile_func_array_key_exists(result, args);
5104
101k
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5105
2.42k
    return zend_compile_func_sprintf(result, args);
5106
98.6k
  } else if (zend_string_equals_literal(lcname, "printf")) {
5107
3.38k
    return zend_compile_func_printf(result, args);
5108
95.3k
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5109
1.69k
    return zend_compile_func_clone(result, args);
5110
93.6k
  } else {
5111
93.6k
    return FAILURE;
5112
93.6k
  }
5113
128k
}
5114
5115
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
5116
151k
{
5117
151k
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5118
0
    return FAILURE;
5119
0
  }
5120
5121
151k
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5122
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5123
     * function with a different implementation. Don't use the VM builtin in that case. */
5124
22.2k
    return FAILURE;
5125
22.2k
  }
5126
5127
129k
  if (zend_args_contain_unpack_or_named(args)) {
5128
1.30k
    return FAILURE;
5129
1.30k
  }
5130
5131
128k
  if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
5132
25.0k
    return SUCCESS;
5133
25.0k
  }
5134
5135
103k
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5136
128k
}
5137
5138
7
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5139
7
  switch (kind) {
5140
0
    case ZEND_PROPERTY_HOOK_GET:
5141
0
      return "get";
5142
7
    case ZEND_PROPERTY_HOOK_SET:
5143
7
      return "set";
5144
7
    EMPTY_SWITCH_DEFAULT_CASE()
5145
7
  }
5146
7
}
5147
5148
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5149
657
{
5150
657
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5151
455
    return zend_string_copy(prop_name);
5152
455
  } else {
5153
202
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5154
202
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5155
202
  }
5156
657
}
5157
5158
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5159
54.2k
{
5160
54.2k
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5161
5162
54.2k
  const zend_ast *class_ast = ast->child[0];
5163
54.2k
  zend_ast *method_ast = ast->child[1];
5164
5165
  /* Recognize parent::$prop::get() pattern. */
5166
54.2k
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5167
2.16k
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5168
2.06k
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5169
1.85k
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5170
1.85k
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5171
1.32k
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5172
1.07k
   || method_ast->kind != ZEND_AST_ZVAL
5173
1.00k
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5174
1.00k
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5175
53.4k
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5176
53.4k
    return false;
5177
53.4k
  }
5178
5179
793
  zend_class_entry *ce = CG(active_class_entry);
5180
793
  if (!ce) {
5181
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5182
7
  }
5183
5184
786
  zend_ast *args_ast = ast->child[2];
5185
786
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5186
6
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5187
6
  }
5188
5189
780
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5190
780
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5191
780
  zend_string *hook_name = zend_ast_get_str(method_ast);
5192
780
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5193
780
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5194
5195
780
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5196
780
  if (!prop_info_name) {
5197
7
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5198
7
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5199
7
  }
5200
5201
773
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5202
773
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5203
36
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5204
36
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5205
36
  }
5206
737
  if (hook_kind != CG(context).active_property_hook_kind) {
5207
7
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5208
7
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5209
7
  }
5210
5211
730
  zend_op *opline = get_next_op();
5212
730
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5213
730
  opline->op1_type = IS_CONST;
5214
730
  opline->op1.constant = zend_add_literal_string(&property_name);
5215
730
  opline->op2.num = hook_kind;
5216
5217
730
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast));
5218
5219
730
  return true;
5220
737
}
5221
5222
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5223
3.82M
{
5224
3.82M
  zend_ast *name_ast = ast->child[0];
5225
3.82M
  zend_ast *args_ast = ast->child[1];
5226
3.82M
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5227
5228
3.82M
  znode name_node;
5229
5230
3.82M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5231
236k
    zend_compile_expr(&name_node, name_ast);
5232
236k
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5233
236k
    return;
5234
236k
  }
5235
5236
3.59M
  {
5237
3.59M
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5238
3.59M
    if (runtime_resolution) {
5239
3.36M
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5240
3.00k
          && !is_callable_convert) {
5241
2.32k
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5242
3.35M
      } else {
5243
3.35M
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5244
3.35M
      }
5245
3.36M
      return;
5246
3.36M
    }
5247
3.59M
  }
5248
5249
230k
  {
5250
230k
    const zval *name = &name_node.u.constant;
5251
230k
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5252
230k
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5253
230k
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5254
230k
    zend_op *opline;
5255
5256
    /* Special assert() handling should apply independently of compiler flags. */
5257
230k
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5258
20.3k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5259
20.3k
      zend_string_release(lcname);
5260
20.3k
      zval_ptr_dtor(&name_node.u.constant);
5261
20.3k
      return;
5262
20.3k
    }
5263
5264
209k
    if (!fbc
5265
152k
     || !fbc_is_finalized(fbc)
5266
152k
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5267
57.6k
      zend_string_release_ex(lcname, 0);
5268
57.6k
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5269
57.6k
      return;
5270
57.6k
    }
5271
5272
152k
    if (!is_callable_convert &&
5273
151k
        zend_try_compile_special_func(result, lcname,
5274
151k
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5275
152k
    ) {
5276
26.9k
      zend_string_release_ex(lcname, 0);
5277
26.9k
      zval_ptr_dtor(&name_node.u.constant);
5278
26.9k
      return;
5279
26.9k
    }
5280
5281
125k
    zval_ptr_dtor(&name_node.u.constant);
5282
125k
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5283
5284
125k
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5285
125k
    opline->result.num = zend_alloc_cache_slot();
5286
5287
    /* Store offset to function from symbol table in op2.extra. */
5288
125k
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5289
102k
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5290
102k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5291
102k
    }
5292
5293
125k
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5294
125k
  }
5295
125k
}
5296
/* }}} */
5297
5298
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5299
63.5k
{
5300
63.5k
  zend_ast *obj_ast = ast->child[0];
5301
63.5k
  zend_ast *method_ast = ast->child[1];
5302
63.5k
  zend_ast *args_ast = ast->child[2];
5303
5304
63.5k
  znode obj_node, method_node;
5305
63.5k
  zend_op *opline;
5306
63.5k
  const zend_function *fbc = NULL;
5307
63.5k
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5308
63.5k
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5309
5310
63.5k
  if (is_this_fetch(obj_ast)) {
5311
2.28k
    if (this_guaranteed_exists()) {
5312
2.12k
      obj_node.op_type = IS_UNUSED;
5313
2.12k
    } else {
5314
152
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5315
152
    }
5316
2.28k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5317
5318
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5319
     * check for a nullsafe access. */
5320
61.2k
  } else {
5321
61.2k
    zend_short_circuiting_mark_inner(obj_ast);
5322
61.2k
    zend_compile_expr(&obj_node, obj_ast);
5323
61.2k
    if (nullsafe) {
5324
6.31k
      zend_emit_jmp_null(&obj_node, type);
5325
6.31k
    }
5326
61.2k
  }
5327
5328
63.5k
  zend_compile_expr(&method_node, method_ast);
5329
63.5k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5330
5331
63.5k
  if (method_node.op_type == IS_CONST) {
5332
62.1k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5333
9
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5334
9
    }
5335
5336
62.1k
    opline->op2_type = IS_CONST;
5337
62.1k
    opline->op2.constant = zend_add_func_name_literal(
5338
62.1k
      Z_STR(method_node.u.constant));
5339
62.1k
    opline->result.num = zend_alloc_cache_slots(2);
5340
62.1k
  } else {
5341
1.42k
    SET_NODE(opline->op2, &method_node);
5342
1.42k
  }
5343
5344
  /* Check if this calls a known method on $this */
5345
63.5k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5346
2.07k
      CG(active_class_entry) && zend_is_scope_known()) {
5347
2.00k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5348
2.00k
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5349
5350
    /* We only know the exact method that is being called if it is either private or final.
5351
     * Otherwise an overriding method in a child class may be called. */
5352
2.00k
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5353
288
      fbc = NULL;
5354
288
    }
5355
2.00k
  }
5356
5357
63.5k
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5358
315
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5359
16
      zend_error_noreturn(E_COMPILE_ERROR,
5360
16
        "Cannot combine nullsafe operator with Closure creation");
5361
16
    }
5362
315
  }
5363
63.5k
}
5364
/* }}} */
5365
5366
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5367
52.7k
{
5368
52.7k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5369
52.7k
}
5370
/* }}} */
5371
5372
static bool is_func_accessible(const zend_function *fbc)
5373
51.3k
{
5374
51.3k
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5375
51.0k
    return true;
5376
51.0k
  }
5377
5378
278
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5379
147
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5380
147
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5381
24
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5382
0
    return true;
5383
0
  }
5384
5385
278
  return false;
5386
278
}
5387
5388
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5389
6.11k
{
5390
6.11k
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5391
5392
6.11k
  if (!fbc || is_func_accessible(fbc)) {
5393
5.84k
    return fbc;
5394
5.84k
  }
5395
5396
270
  return NULL;
5397
6.11k
}
5398
/* }}} */
5399
5400
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5401
54.2k
{
5402
54.2k
  zend_ast *class_ast = ast->child[0];
5403
54.2k
  zend_ast *method_ast = ast->child[1];
5404
54.2k
  zend_ast *args_ast = ast->child[2];
5405
5406
54.2k
  znode class_node, method_node;
5407
54.2k
  zend_op *opline;
5408
54.2k
  const zend_function *fbc = NULL;
5409
5410
54.2k
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5411
730
    return;
5412
730
  }
5413
5414
53.5k
  zend_short_circuiting_mark_inner(class_ast);
5415
53.5k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5416
5417
53.5k
  zend_compile_expr(&method_node, method_ast);
5418
5419
53.5k
  if (method_node.op_type == IS_CONST) {
5420
51.6k
    zval *name = &method_node.u.constant;
5421
51.6k
    if (Z_TYPE_P(name) != IS_STRING) {
5422
3
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5423
3
    }
5424
51.5k
    if (zend_is_constructor(Z_STR_P(name))) {
5425
217
      zval_ptr_dtor(name);
5426
217
      method_node.op_type = IS_UNUSED;
5427
217
    }
5428
51.5k
  }
5429
5430
53.5k
  opline = get_next_op();
5431
53.5k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5432
5433
53.5k
  zend_set_class_name_op1(opline, &class_node);
5434
5435
53.5k
  if (method_node.op_type == IS_CONST) {
5436
51.3k
    opline->op2_type = IS_CONST;
5437
51.3k
    opline->op2.constant = zend_add_func_name_literal(
5438
51.3k
      Z_STR(method_node.u.constant));
5439
51.3k
    opline->result.num = zend_alloc_cache_slots(2);
5440
51.3k
  } else {
5441
2.12k
    if (opline->op1_type == IS_CONST) {
5442
480
      opline->result.num = zend_alloc_cache_slot();
5443
480
    }
5444
2.12k
    SET_NODE(opline->op2, &method_node);
5445
2.12k
  }
5446
5447
  /* Check if we already know which method we're calling */
5448
53.5k
  if (opline->op2_type == IS_CONST) {
5449
51.3k
    zend_class_entry *ce = NULL;
5450
51.3k
    if (opline->op1_type == IS_CONST) {
5451
24.0k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5452
24.0k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5453
24.0k
      if (ce) {
5454
5.09k
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5455
0
          ce = NULL;
5456
0
        }
5457
18.9k
      } else if (CG(active_class_entry)
5458
1.17k
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5459
585
        ce = CG(active_class_entry);
5460
585
      }
5461
27.3k
    } else if (opline->op1_type == IS_UNUSED
5462
20.4k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5463
19.8k
        && zend_is_scope_known()) {
5464
439
      ce = CG(active_class_entry);
5465
439
    }
5466
51.3k
    if (ce) {
5467
6.11k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5468
6.11k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5469
6.11k
    }
5470
51.3k
  }
5471
5472
53.5k
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5473
53.5k
}
5474
/* }}} */
5475
5476
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5477
5478
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5479
82.6k
{
5480
82.6k
  zend_ast *class_ast = ast->child[0];
5481
82.6k
  zend_ast *args_ast = ast->child[1];
5482
5483
82.6k
  znode class_node, ctor_result;
5484
82.6k
  zend_op *opline;
5485
5486
82.6k
  if (class_ast->kind == ZEND_AST_CLASS) {
5487
    /* anon class declaration */
5488
1.94k
    zend_compile_class_decl(&class_node, class_ast, false);
5489
80.6k
  } else {
5490
80.6k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5491
80.6k
  }
5492
5493
82.6k
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5494
5495
82.6k
  zend_set_class_name_op1(opline, &class_node);
5496
5497
82.6k
  if (opline->op1_type == IS_CONST) {
5498
78.9k
    opline->op2.num = zend_alloc_cache_slot();
5499
78.9k
  }
5500
5501
82.6k
  zend_class_entry *ce = NULL;
5502
82.6k
  if (opline->op1_type == IS_CONST) {
5503
78.9k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5504
78.9k
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5505
78.9k
    if (ce) {
5506
59.2k
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5507
0
        ce = NULL;
5508
0
      }
5509
59.2k
    } else if (CG(active_class_entry)
5510
1.22k
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5511
409
      ce = CG(active_class_entry);
5512
409
    }
5513
78.9k
  } else if (opline->op1_type == IS_UNUSED
5514
246
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5515
114
      && zend_is_scope_known()) {
5516
94
    ce = CG(active_class_entry);
5517
94
  }
5518
5519
5520
82.6k
  const zend_function *fbc = NULL;
5521
82.6k
  if (ce
5522
59.7k
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5523
59.7k
      && ce->constructor
5524
46.6k
      && is_func_accessible(ce->constructor)) {
5525
46.6k
    fbc = ce->constructor;
5526
46.6k
  }
5527
5528
82.6k
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno);
5529
82.6k
  zend_do_free(&ctor_result);
5530
82.6k
}
5531
/* }}} */
5532
5533
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5534
2.17k
{
5535
2.17k
  zend_ast *var_ast = ast->child[0];
5536
2.17k
  zend_ast *name_ast = var_ast->child[0];
5537
5538
2.17k
  znode name_node, result;
5539
5540
2.17k
  zend_compile_expr(&name_node, name_ast);
5541
2.17k
  if (name_node.op_type == IS_CONST) {
5542
1.75k
    convert_to_string(&name_node.u.constant);
5543
1.75k
  }
5544
5545
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5546
2.17k
  if (is_this_fetch(var_ast)) {
5547
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5548
2.16k
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5549
1.59k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5550
1.59k
    opline->extended_value = zend_alloc_cache_slot();
5551
1.59k
  } else {
5552
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5553
     * to not free the name_node operand, so it can be reused in the following
5554
     * ASSIGN_REF, which then frees it. */
5555
575
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5556
575
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5557
5558
575
    if (name_node.op_type == IS_CONST) {
5559
158
      zend_string_addref(Z_STR(name_node.u.constant));
5560
158
    }
5561
5562
575
    zend_emit_assign_ref_znode(
5563
575
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5564
575
      &result
5565
575
    );
5566
575
  }
5567
2.17k
}
5568
/* }}} */
5569
5570
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5571
77.3k
{
5572
77.3k
  zend_op *opline;
5573
77.3k
  if (!CG(active_op_array)->static_variables) {
5574
0
    if (CG(active_op_array)->scope) {
5575
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5576
0
    }
5577
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5578
0
  }
5579
5580
77.3k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5581
5582
77.3k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5583
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5584
0
  }
5585
5586
77.3k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5587
77.3k
  opline->op1_type = IS_CV;
5588
77.3k
  opline->op1.var = lookup_cv(var_name);
5589
77.3k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5590
77.3k
}
5591
/* }}} */
5592
5593
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5594
1.38k
{
5595
1.38k
  zend_ast *var_ast = ast->child[0];
5596
1.38k
  zend_string *var_name = zend_ast_get_str(var_ast);
5597
5598
1.38k
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5599
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5600
7
  }
5601
5602
1.38k
  if (!CG(active_op_array)->static_variables) {
5603
1.00k
    if (CG(active_op_array)->scope) {
5604
270
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5605
270
    }
5606
1.00k
    CG(active_op_array)->static_variables = zend_new_array(8);
5607
1.00k
  }
5608
5609
1.38k
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5610
19
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5611
19
  }
5612
5613
1.36k
  zend_eval_const_expr(&ast->child[1]);
5614
1.36k
  zend_ast *value_ast = ast->child[1];
5615
5616
1.36k
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5617
939
    zval *value_zv = value_ast
5618
939
      ? zend_ast_get_zval(value_ast)
5619
939
      : &EG(uninitialized_zval);
5620
939
    Z_TRY_ADDREF_P(value_zv);
5621
939
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5622
939
  } else {
5623
424
    zend_op *opline;
5624
5625
424
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5626
424
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5627
5628
424
    uint32_t static_def_jmp_opnum = get_next_op_number();
5629
424
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5630
424
    opline->op1_type = IS_CV;
5631
424
    opline->op1.var = lookup_cv(var_name);
5632
424
    opline->extended_value = placeholder_offset;
5633
5634
424
    znode expr;
5635
424
    zend_compile_expr(&expr, value_ast);
5636
5637
424
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5638
424
    opline->op1_type = IS_CV;
5639
424
    opline->op1.var = lookup_cv(var_name);
5640
424
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5641
5642
424
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5643
424
  }
5644
1.36k
}
5645
/* }}} */
5646
5647
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5648
7.90k
{
5649
7.90k
  zend_ast *var_ast = ast->child[0];
5650
7.90k
  znode var_node;
5651
7.90k
  zend_op *opline;
5652
5653
7.90k
  zend_ensure_writable_variable(var_ast);
5654
5655
7.90k
  if (is_global_var_fetch(var_ast)) {
5656
880
    if (!var_ast->child[1]) {
5657
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5658
7
    }
5659
5660
873
    zend_compile_expr(&var_node, var_ast->child[1]);
5661
873
    if (var_node.op_type == IS_CONST) {
5662
510
      convert_to_string(&var_node.u.constant);
5663
510
    }
5664
5665
873
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5666
873
    opline->extended_value = ZEND_FETCH_GLOBAL;
5667
873
    return;
5668
880
  }
5669
5670
7.02k
  switch (var_ast->kind) {
5671
3.51k
    case ZEND_AST_VAR:
5672
3.51k
      if (is_this_fetch(var_ast)) {
5673
10
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5674
3.50k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5675
3.15k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5676
3.15k
      } else {
5677
353
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5678
353
        opline->opcode = ZEND_UNSET_VAR;
5679
353
      }
5680
3.50k
      return;
5681
3.50k
    case ZEND_AST_DIM:
5682
1.98k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5683
1.98k
      opline->opcode = ZEND_UNSET_DIM;
5684
1.98k
      return;
5685
1.47k
    case ZEND_AST_PROP:
5686
1.47k
    case ZEND_AST_NULLSAFE_PROP:
5687
1.47k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5688
1.47k
      opline->opcode = ZEND_UNSET_OBJ;
5689
1.47k
      return;
5690
29
    case ZEND_AST_STATIC_PROP:
5691
29
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5692
29
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5693
29
      return;
5694
7.02k
    EMPTY_SWITCH_DEFAULT_CASE()
5695
7.02k
  }
5696
7.02k
}
5697
/* }}} */
5698
5699
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5700
47.8k
{
5701
47.8k
  const zend_loop_var *base;
5702
47.8k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5703
5704
47.8k
  if (!loop_var) {
5705
1.31k
    return 1;
5706
1.31k
  }
5707
46.5k
  base = zend_stack_base(&CG(loop_var_stack));
5708
53.8k
  for (; loop_var >= base; loop_var--) {
5709
51.9k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5710
2.82k
      zend_op *opline = get_next_op();
5711
5712
2.82k
      opline->opcode = ZEND_FAST_CALL;
5713
2.82k
      opline->result_type = IS_TMP_VAR;
5714
2.82k
      opline->result.var = loop_var->var_num;
5715
2.82k
      if (return_value) {
5716
1.66k
        SET_NODE(opline->op2, return_value);
5717
1.66k
      }
5718
2.82k
      opline->op1.num = loop_var->try_catch_offset;
5719
49.1k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5720
2.27k
      zend_op *opline = get_next_op();
5721
2.27k
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5722
2.27k
      opline->op1_type = IS_TMP_VAR;
5723
2.27k
      opline->op1.var = loop_var->var_num;
5724
46.9k
    } else if (loop_var->opcode == ZEND_RETURN) {
5725
      /* Stack separator */
5726
43.1k
      break;
5727
43.1k
    } else if (depth <= 1) {
5728
1.50k
      return 1;
5729
2.25k
    } else if (loop_var->opcode == ZEND_NOP) {
5730
      /* Loop doesn't have freeable variable */
5731
1.39k
      depth--;
5732
1.39k
    } else {
5733
861
      zend_op *opline;
5734
5735
861
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5736
861
      opline = get_next_op();
5737
861
      opline->opcode = loop_var->opcode;
5738
861
      opline->op1_type = loop_var->var_type;
5739
861
      opline->op1.var = loop_var->var_num;
5740
861
      opline->extended_value = ZEND_FREE_ON_RETURN;
5741
861
      depth--;
5742
861
      }
5743
51.9k
  }
5744
45.0k
  return (depth == 0);
5745
46.5k
}
5746
/* }}} */
5747
5748
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5749
46.2k
{
5750
46.2k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5751
46.2k
}
5752
/* }}} */
5753
5754
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5755
2.24k
{
5756
2.24k
  const zend_loop_var *base;
5757
2.24k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5758
5759
2.24k
  if (!loop_var) {
5760
298
    return 0;
5761
298
  }
5762
1.94k
  base = zend_stack_base(&CG(loop_var_stack));
5763
3.81k
  for (; loop_var >= base; loop_var--) {
5764
3.50k
    if (loop_var->opcode == ZEND_FAST_CALL) {
5765
532
      return 1;
5766
2.97k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5767
1.52k
    } else if (loop_var->opcode == ZEND_RETURN) {
5768
      /* Stack separator */
5769
1.09k
      return 0;
5770
1.09k
    } else if (depth <= 1) {
5771
0
      return 0;
5772
432
    } else {
5773
432
      depth--;
5774
432
      }
5775
3.50k
  }
5776
315
  return 0;
5777
1.94k
}
5778
/* }}} */
5779
5780
static bool zend_has_finally(void) /* {{{ */
5781
2.24k
{
5782
2.24k
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5783
2.24k
}
5784
/* }}} */
5785
5786
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5787
45.0k
{
5788
45.0k
  zend_ast *expr_ast = ast->child[0];
5789
45.0k
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5790
45.0k
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5791
5792
45.0k
  znode expr_node;
5793
45.0k
  zend_op *opline;
5794
5795
45.0k
  if (is_generator) {
5796
    /* For generators the by-ref flag refers to yields, not returns */
5797
6.25k
    by_ref = false;
5798
6.25k
  }
5799
5800
45.0k
  if (!expr_ast) {
5801
866
    expr_node.op_type = IS_CONST;
5802
866
    ZVAL_NULL(&expr_node.u.constant);
5803
44.2k
  } else if (by_ref && zend_is_variable(expr_ast)) {
5804
2.06k
    zend_assert_not_short_circuited(expr_ast);
5805
2.06k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5806
42.1k
  } else {
5807
42.1k
    zend_compile_expr(&expr_node, expr_ast);
5808
42.1k
  }
5809
5810
45.0k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5811
2.81k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5812
2.24k
   && zend_has_finally()) {
5813
    /* Copy return value into temporary VAR to avoid modification in finally code */
5814
532
    if (by_ref) {
5815
133
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5816
399
    } else {
5817
399
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5818
399
    }
5819
532
  }
5820
5821
  /* Generator return types are handled separately */
5822
45.0k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5823
6.22k
    zend_emit_return_type_check(
5824
6.22k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5825
6.22k
  }
5826
5827
45.0k
  uint32_t opnum_before_finally = get_next_op_number();
5828
5829
45.0k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5830
5831
  /* Content of reference might have changed in finally, repeat type check. */
5832
45.0k
  if (by_ref
5833
   /* Check if any opcodes were emitted since the last return type check. */
5834
4.19k
   && opnum_before_finally != get_next_op_number()
5835
1.37k
   && !is_generator
5836
1.37k
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5837
5
    zend_emit_return_type_check(
5838
5
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5839
5
  }
5840
5841
45.0k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5842
45.0k
    &expr_node, NULL);
5843
5844
45.0k
  if (by_ref && expr_ast) {
5845
3.88k
    if (zend_is_call(expr_ast)) {
5846
750
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5847
3.13k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5848
1.07k
      opline->extended_value = ZEND_RETURNS_VALUE;
5849
1.07k
    }
5850
3.88k
  }
5851
45.0k
}
5852
/* }}} */
5853
5854
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
5855
817
{
5856
817
  zend_ast *expr_ast = ast->child[0];
5857
817
  znode expr_node;
5858
817
  zend_op *opline;
5859
5860
817
  zend_compile_expr(&expr_node, expr_ast);
5861
5862
817
  switch (expr_node.op_type) {
5863
118
    case IS_TMP_VAR:
5864
263
    case IS_VAR:
5865
263
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5866
263
      opline->extended_value = ZEND_FREE_VOID_CAST;
5867
263
      break;
5868
544
    case IS_CONST:
5869
544
      zend_do_free(&expr_node);
5870
544
      break;
5871
817
  }
5872
817
}
5873
5874
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
5875
2.06M
{
5876
2.06M
  zend_op *opline;
5877
2.06M
  zend_ast *expr_ast = ast->child[0];
5878
5879
2.06M
  znode expr_node;
5880
2.06M
  zend_compile_expr(&expr_node, expr_ast);
5881
5882
2.06M
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5883
2.06M
  opline->extended_value = 0;
5884
2.06M
}
5885
/* }}} */
5886
5887
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
5888
3.86k
{
5889
3.86k
  zend_ast *expr_ast = ast->child[0];
5890
5891
3.86k
  znode expr_node;
5892
3.86k
  zend_compile_expr(&expr_node, expr_ast);
5893
5894
3.86k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5895
3.86k
  if (result) {
5896
    /* Mark this as an "expression throw" for opcache. */
5897
848
    opline->extended_value = ZEND_THROW_IS_EXPR;
5898
848
    result->op_type = IS_CONST;
5899
848
    ZVAL_TRUE(&result->u.constant);
5900
848
  }
5901
3.86k
}
5902
/* }}} */
5903
5904
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
5905
1.68k
{
5906
1.68k
  zend_ast *depth_ast = ast->child[0];
5907
5908
1.68k
  zend_op *opline;
5909
1.68k
  zend_long depth;
5910
5911
1.68k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5912
5913
1.68k
  if (depth_ast) {
5914
438
    const zval *depth_zv;
5915
438
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5916
25
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5917
25
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5918
25
    }
5919
5920
413
    depth_zv = zend_ast_get_zval(depth_ast);
5921
413
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5922
10
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5923
10
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5924
10
    }
5925
5926
403
    depth = Z_LVAL_P(depth_zv);
5927
1.24k
  } else {
5928
1.24k
    depth = 1;
5929
1.24k
  }
5930
5931
1.64k
  if (CG(context).current_brk_cont == -1) {
5932
46
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5933
46
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5934
1.60k
  } else {
5935
1.60k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5936
100
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5937
100
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5938
100
        depth, depth == 1 ? "" : "s");
5939
100
    }
5940
1.60k
  }
5941
5942
1.50k
  if (ast->kind == ZEND_AST_CONTINUE) {
5943
716
    int d, cur = CG(context).current_brk_cont;
5944
851
    for (d = depth - 1; d > 0; d--) {
5945
135
      cur = CG(context).brk_cont_array[cur].parent;
5946
135
      ZEND_ASSERT(cur != -1);
5947
135
    }
5948
5949
716
    if (CG(context).brk_cont_array[cur].is_switch) {
5950
121
      if (depth == 1) {
5951
28
        if (CG(context).brk_cont_array[cur].parent == -1) {
5952
19
          zend_error(E_WARNING,
5953
19
            "\"continue\" targeting switch is equivalent to \"break\"");
5954
19
        } else {
5955
9
          zend_error(E_WARNING,
5956
9
            "\"continue\" targeting switch is equivalent to \"break\". " \
5957
9
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5958
9
            depth + 1);
5959
9
        }
5960
93
      } else {
5961
93
        if (CG(context).brk_cont_array[cur].parent == -1) {
5962
42
          zend_error(E_WARNING,
5963
42
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5964
42
            depth, depth);
5965
51
        } else {
5966
51
          zend_error(E_WARNING,
5967
51
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5968
51
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5969
51
            depth, depth, depth + 1);
5970
51
        }
5971
93
      }
5972
121
    }
5973
716
  }
5974
5975
1.50k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5976
1.50k
  opline->op1.num = CG(context).current_brk_cont;
5977
1.50k
  opline->op2.num = depth;
5978
1.50k
}
5979
/* }}} */
5980
5981
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5982
1.12k
{
5983
1.12k
  zend_label *dest;
5984
1.12k
  int remove_oplines = opline->op1.num;
5985
1.12k
  zval *label;
5986
1.12k
  uint32_t opnum = opline - op_array->opcodes;
5987
5988
1.12k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5989
1.12k
  if (CG(context).labels == NULL ||
5990
1.10k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5991
1.12k
  ) {
5992
71
    CG(in_compilation) = 1;
5993
71
    CG(active_op_array) = op_array;
5994
71
    CG(zend_lineno) = opline->lineno;
5995
71
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5996
71
  }
5997
5998
1.05k
  zval_ptr_dtor_str(label);
5999
1.05k
  ZVAL_NULL(label);
6000
6001
1.05k
  uint32_t current = opline->extended_value;
6002
1.65k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6003
618
    if (current == -1) {
6004
16
      CG(in_compilation) = 1;
6005
16
      CG(active_op_array) = op_array;
6006
16
      CG(zend_lineno) = opline->lineno;
6007
16
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6008
16
    }
6009
602
    if (CG(context).brk_cont_array[current].start >= 0) {
6010
422
      remove_oplines--;
6011
422
    }
6012
602
  }
6013
6014
2.26k
  for (current = 0; current < op_array->last_try_catch; ++current) {
6015
1.43k
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6016
1.43k
    if (elem->try_op > opnum) {
6017
199
      break;
6018
199
    }
6019
1.23k
    if (elem->finally_op && opnum < elem->finally_op - 1
6020
787
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6021
1.23k
    ) {
6022
413
      remove_oplines--;
6023
413
    }
6024
1.23k
  }
6025
6026
1.03k
  opline->opcode = ZEND_JMP;
6027
1.03k
  SET_UNUSED(opline->op1);
6028
1.03k
  SET_UNUSED(opline->op2);
6029
1.03k
  SET_UNUSED(opline->result);
6030
1.03k
  opline->op1.opline_num = dest->opline_num;
6031
1.03k
  opline->extended_value = 0;
6032
6033
1.03k
  ZEND_ASSERT(remove_oplines >= 0);
6034
1.47k
  while (remove_oplines--) {
6035
440
    opline--;
6036
440
    MAKE_NOP(opline);
6037
440
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6038
440
  }
6039
1.03k
}
6040
/* }}} */
6041
6042
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6043
1.32k
{
6044
1.32k
  zend_ast *label_ast = ast->child[0];
6045
1.32k
  znode label_node;
6046
1.32k
  zend_op *opline;
6047
6048
1.32k
  zend_compile_expr(&label_node, label_ast);
6049
6050
  /* Label resolution and unwinding adjustments happen in pass two. */
6051
1.32k
  uint32_t opnum_start = get_next_op_number();
6052
1.32k
  zend_handle_loops_and_finally(NULL);
6053
1.32k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6054
1.32k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6055
1.32k
  opline->extended_value = CG(context).current_brk_cont;
6056
1.32k
}
6057
/* }}} */
6058
6059
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6060
5.47k
{
6061
5.47k
  zend_string *label = zend_ast_get_str(ast->child[0]);
6062
5.47k
  zend_label dest;
6063
6064
5.47k
  if (!CG(context).labels) {
6065
4.11k
    ALLOC_HASHTABLE(CG(context).labels);
6066
4.11k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6067
4.11k
  }
6068
6069
5.47k
  dest.brk_cont = CG(context).current_brk_cont;
6070
5.47k
  dest.opline_num = get_next_op_number();
6071
6072
5.47k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6073
74
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6074
74
  }
6075
5.47k
}
6076
/* }}} */
6077
6078
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6079
6.47k
{
6080
6.47k
  zend_ast *cond_ast = ast->child[0];
6081
6.47k
  zend_ast *stmt_ast = ast->child[1];
6082
6.47k
  znode cond_node;
6083
6.47k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6084
6085
6.47k
  opnum_jmp = zend_emit_jump(0);
6086
6087
6.47k
  zend_begin_loop(ZEND_NOP, NULL, false);
6088
6089
6.47k
  opnum_start = get_next_op_number();
6090
6.47k
  zend_compile_stmt(stmt_ast);
6091
6092
6.47k
  opnum_cond = get_next_op_number();
6093
6.47k
  zend_update_jump_target(opnum_jmp, opnum_cond);
6094
6.47k
  zend_compile_expr(&cond_node, cond_ast);
6095
6096
6.47k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6097
6098
6.47k
  zend_end_loop(opnum_cond, NULL);
6099
6.47k
}
6100
/* }}} */
6101
6102
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6103
1.11k
{
6104
1.11k
  zend_ast *stmt_ast = ast->child[0];
6105
1.11k
  zend_ast *cond_ast = ast->child[1];
6106
6107
1.11k
  znode cond_node;
6108
1.11k
  uint32_t opnum_start, opnum_cond;
6109
6110
1.11k
  zend_begin_loop(ZEND_NOP, NULL, false);
6111
6112
1.11k
  opnum_start = get_next_op_number();
6113
1.11k
  zend_compile_stmt(stmt_ast);
6114
6115
1.11k
  opnum_cond = get_next_op_number();
6116
1.11k
  zend_compile_expr(&cond_node, cond_ast);
6117
6118
1.11k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6119
6120
1.11k
  zend_end_loop(opnum_cond, NULL);
6121
1.11k
}
6122
/* }}} */
6123
6124
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6125
35.3k
{
6126
35.3k
  const zend_ast_list *list;
6127
35.3k
  uint32_t i;
6128
6129
35.3k
  result->op_type = IS_CONST;
6130
35.3k
  ZVAL_TRUE(&result->u.constant);
6131
6132
35.3k
  if (!ast) {
6133
8.75k
    return;
6134
8.75k
  }
6135
6136
26.6k
  list = zend_ast_get_list(ast);
6137
55.3k
  for (i = 0; i < list->children; ++i) {
6138
28.7k
    zend_ast *expr_ast = list->child[i];
6139
6140
28.7k
    zend_do_free(result);
6141
28.7k
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6142
88
      zend_compile_void_cast(NULL, expr_ast);
6143
88
      result->op_type = IS_CONST;
6144
88
      ZVAL_NULL(&result->u.constant);
6145
28.6k
    } else {
6146
28.6k
      zend_compile_expr(result, expr_ast);
6147
28.6k
    }
6148
28.7k
  }
6149
26.6k
}
6150
/* }}} */
6151
6152
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6153
11.8k
{
6154
11.8k
  zend_ast *init_ast = ast->child[0];
6155
11.8k
  zend_ast *cond_ast = ast->child[1];
6156
11.8k
  zend_ast *loop_ast = ast->child[2];
6157
11.8k
  zend_ast *stmt_ast = ast->child[3];
6158
6159
11.8k
  znode result;
6160
11.8k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6161
6162
11.8k
  zend_compile_for_expr_list(&result, init_ast);
6163
11.8k
  zend_do_free(&result);
6164
6165
11.8k
  opnum_jmp = zend_emit_jump(0);
6166
6167
11.8k
  zend_begin_loop(ZEND_NOP, NULL, false);
6168
6169
11.8k
  opnum_start = get_next_op_number();
6170
11.8k
  zend_compile_stmt(stmt_ast);
6171
6172
11.8k
  opnum_loop = get_next_op_number();
6173
11.8k
  zend_compile_for_expr_list(&result, loop_ast);
6174
11.8k
  zend_do_free(&result);
6175
6176
11.8k
  zend_update_jump_target_to_next(opnum_jmp);
6177
11.8k
  zend_compile_for_expr_list(&result, cond_ast);
6178
11.8k
  zend_do_extended_stmt(NULL);
6179
6180
11.8k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6181
6182
11.8k
  zend_end_loop(opnum_loop, NULL);
6183
11.8k
}
6184
/* }}} */
6185
6186
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6187
21.6k
{
6188
21.6k
  zend_ast *expr_ast = ast->child[0];
6189
21.6k
  zend_ast *value_ast = ast->child[1];
6190
21.6k
  zend_ast *key_ast = ast->child[2];
6191
21.6k
  zend_ast *stmt_ast = ast->child[3];
6192
21.6k
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6193
21.6k
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6194
6195
21.6k
  znode expr_node, reset_node, value_node, key_node;
6196
21.6k
  zend_op *opline;
6197
21.6k
  uint32_t opnum_reset, opnum_fetch;
6198
6199
21.6k
  if (key_ast) {
6200
1.53k
    if (key_ast->kind == ZEND_AST_REF) {
6201
9
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6202
9
    }
6203
1.52k
    if (key_ast->kind == ZEND_AST_ARRAY) {
6204
15
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6205
15
    }
6206
1.52k
  }
6207
6208
21.5k
  if (by_ref) {
6209
1.22k
    value_ast = value_ast->child[0];
6210
1.22k
  }
6211
6212
21.5k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6213
142
    by_ref = true;
6214
142
  }
6215
6216
21.5k
  if (by_ref && is_variable) {
6217
894
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6218
20.6k
  } else {
6219
20.6k
    zend_compile_expr(&expr_node, expr_ast);
6220
20.6k
  }
6221
6222
21.5k
  if (by_ref) {
6223
1.36k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6224
1.36k
  }
6225
6226
21.5k
  opnum_reset = get_next_op_number();
6227
21.5k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6228
6229
21.5k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6230
6231
21.5k
  opnum_fetch = get_next_op_number();
6232
21.5k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6233
6234
21.5k
  if (is_this_fetch(value_ast)) {
6235
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6236
21.5k
  } else if (value_ast->kind == ZEND_AST_VAR &&
6237
20.9k
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6238
20.6k
    SET_NODE(opline->op2, &value_node);
6239
20.6k
  } else {
6240
901
    opline->op2_type = IS_VAR;
6241
901
    opline->op2.var = get_temporary_variable();
6242
901
    GET_NODE(&value_node, opline->op2);
6243
901
    if (value_ast->kind == ZEND_AST_ARRAY) {
6244
341
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6245
560
    } else if (by_ref) {
6246
205
      zend_emit_assign_ref_znode(value_ast, &value_node);
6247
355
    } else {
6248
355
      zend_emit_assign_znode(value_ast, &value_node);
6249
355
    }
6250
901
  }
6251
6252
21.5k
  if (key_ast) {
6253
1.50k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6254
1.50k
    zend_make_tmp_result(&key_node, opline);
6255
1.50k
    zend_emit_assign_znode(key_ast, &key_node);
6256
1.50k
  }
6257
6258
21.5k
  zend_compile_stmt(stmt_ast);
6259
6260
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6261
   * better to use the end line, but this information is not available
6262
   * currently. */
6263
21.5k
  CG(zend_lineno) = ast->lineno;
6264
21.5k
  zend_emit_jump(opnum_fetch);
6265
6266
21.5k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6267
21.5k
  opline->op2.opline_num = get_next_op_number();
6268
6269
21.5k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6270
21.5k
  opline->extended_value = get_next_op_number();
6271
6272
21.5k
  zend_end_loop(opnum_fetch, &reset_node);
6273
6274
21.5k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6275
21.5k
}
6276
/* }}} */
6277
6278
static void zend_compile_if(zend_ast *ast) /* {{{ */
6279
25.1k
{
6280
25.1k
  const zend_ast_list *list = zend_ast_get_list(ast);
6281
25.1k
  uint32_t i;
6282
25.1k
  uint32_t *jmp_opnums = NULL;
6283
6284
25.1k
  if (list->children > 1) {
6285
6.26k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6286
6.26k
  }
6287
6288
60.1k
  for (i = 0; i < list->children; ++i) {
6289
34.9k
    const zend_ast *elem_ast = list->child[i];
6290
34.9k
    zend_ast *cond_ast = elem_ast->child[0];
6291
34.9k
    zend_ast *stmt_ast = elem_ast->child[1];
6292
6293
34.9k
    if (cond_ast) {
6294
29.1k
      znode cond_node;
6295
29.1k
      uint32_t opnum_jmpz;
6296
6297
29.1k
      if (i > 0) {
6298
3.98k
        CG(zend_lineno) = cond_ast->lineno;
6299
3.98k
        zend_do_extended_stmt(NULL);
6300
3.98k
      }
6301
6302
29.1k
      zend_compile_expr(&cond_node, cond_ast);
6303
29.1k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6304
6305
29.1k
      zend_compile_stmt(stmt_ast);
6306
6307
29.1k
      if (i != list->children - 1) {
6308
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6309
         * report the last line in the if branch as covered if it hasn't actually executed. */
6310
9.80k
        CG(zend_lineno) = elem_ast->lineno;
6311
9.80k
        jmp_opnums[i] = zend_emit_jump(0);
6312
9.80k
      }
6313
29.1k
      zend_update_jump_target_to_next(opnum_jmpz);
6314
29.1k
    } else {
6315
      /* "else" can only occur as last element. */
6316
5.81k
      ZEND_ASSERT(i == list->children - 1);
6317
5.81k
      zend_compile_stmt(stmt_ast);
6318
5.81k
    }
6319
34.9k
  }
6320
6321
25.1k
  if (list->children > 1) {
6322
16.0k
    for (i = 0; i < list->children - 1; ++i) {
6323
9.77k
      zend_update_jump_target_to_next(jmp_opnums[i]);
6324
9.77k
    }
6325
6.24k
    efree(jmp_opnums);
6326
6.24k
  }
6327
25.1k
}
6328
/* }}} */
6329
6330
47.6k
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6331
47.6k
  uint32_t i;
6332
47.6k
  uint8_t common_type = IS_UNDEF;
6333
148k
  for (i = 0; i < cases->children; i++) {
6334
126k
    zend_ast *case_ast = cases->child[i];
6335
126k
    zend_ast **cond_ast = &case_ast->child[0];
6336
126k
    const zval *cond_zv;
6337
126k
    if (!case_ast->child[0]) {
6338
      /* Skip default clause */
6339
214
      continue;
6340
214
    }
6341
6342
126k
    zend_eval_const_expr(cond_ast);
6343
126k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6344
      /* Non-constant case */
6345
835
      return IS_UNDEF;
6346
835
    }
6347
6348
125k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6349
125k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6350
      /* We only optimize switched on integers and strings */
6351
24.0k
      return IS_UNDEF;
6352
24.0k
    }
6353
6354
101k
    if (common_type == IS_UNDEF) {
6355
46.6k
      common_type = Z_TYPE_P(cond_zv);
6356
54.6k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6357
      /* Non-uniform case types */
6358
197
      return IS_UNDEF;
6359
197
    }
6360
6361
101k
    if (Z_TYPE_P(cond_zv) == IS_STRING
6362
99.1k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6363
      /* Numeric strings cannot be compared with a simple hash lookup */
6364
122
      return IS_UNDEF;
6365
122
    }
6366
101k
  }
6367
6368
22.4k
  return common_type;
6369
47.6k
}
6370
6371
22.2k
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6372
22.2k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6373
0
    return 0;
6374
0
  }
6375
6376
  /* Thresholds are chosen based on when the average switch time for equidistributed
6377
   * input becomes smaller when using the jumptable optimization. */
6378
22.2k
  if (jumptable_type == IS_LONG) {
6379
328
    return cases->children >= 5;
6380
21.8k
  } else {
6381
21.8k
    ZEND_ASSERT(jumptable_type == IS_STRING);
6382
21.8k
    return cases->children >= 2;
6383
21.8k
  }
6384
22.2k
}
6385
6386
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6387
47.6k
{
6388
47.6k
  zend_ast *expr_ast = ast->child[0];
6389
47.6k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6390
6391
47.6k
  uint32_t i;
6392
47.6k
  bool has_default_case = false;
6393
6394
47.6k
  znode expr_node, case_node;
6395
47.6k
  zend_op *opline;
6396
47.6k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6397
47.6k
  uint8_t jumptable_type;
6398
47.6k
  HashTable *jumptable = NULL;
6399
6400
47.6k
  zend_compile_expr(&expr_node, expr_ast);
6401
6402
47.6k
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6403
6404
47.6k
  case_node.op_type = IS_TMP_VAR;
6405
47.6k
  case_node.u.op.var = get_temporary_variable();
6406
6407
47.6k
  jumptable_type = determine_switch_jumptable_type(cases);
6408
47.6k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6409
20.3k
    znode jumptable_op;
6410
6411
20.3k
    ALLOC_HASHTABLE(jumptable);
6412
20.3k
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6413
20.3k
    jumptable_op.op_type = IS_CONST;
6414
20.3k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6415
6416
20.3k
    opline = zend_emit_op(NULL,
6417
20.3k
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6418
20.3k
      &expr_node, &jumptable_op);
6419
20.3k
    if (opline->op1_type == IS_CONST) {
6420
19.5k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6421
19.5k
    }
6422
20.3k
    opnum_switch = opline - CG(active_op_array)->opcodes;
6423
20.3k
  }
6424
6425
47.6k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6426
177k
  for (i = 0; i < cases->children; ++i) {
6427
129k
    zend_ast *case_ast = cases->child[i];
6428
129k
    zend_ast *cond_ast = case_ast->child[0];
6429
129k
    znode cond_node;
6430
6431
129k
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6432
404
      CG(zend_lineno) = case_ast->lineno;
6433
404
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6434
404
    }
6435
6436
129k
    if (!cond_ast) {
6437
214
      if (has_default_case) {
6438
16
        CG(zend_lineno) = case_ast->lineno;
6439
16
        zend_error_noreturn(E_COMPILE_ERROR,
6440
16
          "Switch statements may only contain one default clause");
6441
16
      }
6442
198
      has_default_case = true;
6443
198
      continue;
6444
214
    }
6445
6446
129k
    zend_compile_expr(&cond_node, cond_ast);
6447
6448
129k
    if (expr_node.op_type == IS_CONST
6449
93.3k
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6450
2.70k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6451
126k
    } else if (expr_node.op_type == IS_CONST
6452
90.6k
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6453
1.68k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6454
124k
    } else {
6455
124k
      opline = zend_emit_op(NULL,
6456
124k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6457
124k
        &expr_node, &cond_node);
6458
124k
      SET_NODE(opline->result, &case_node);
6459
124k
      if (opline->op1_type == IS_CONST) {
6460
89.0k
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6461
89.0k
      }
6462
6463
124k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6464
124k
    }
6465
129k
  }
6466
6467
47.6k
  opnum_default_jmp = zend_emit_jump(0);
6468
6469
176k
  for (i = 0; i < cases->children; ++i) {
6470
128k
    zend_ast *case_ast = cases->child[i];
6471
128k
    zend_ast *cond_ast = case_ast->child[0];
6472
128k
    zend_ast *stmt_ast = case_ast->child[1];
6473
6474
128k
    if (cond_ast) {
6475
128k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6476
6477
128k
      if (jumptable) {
6478
51.8k
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6479
51.8k
        zval jmp_target;
6480
51.8k
        ZVAL_LONG(&jmp_target, get_next_op_number());
6481
6482
51.8k
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6483
51.8k
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6484
1.02k
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6485
50.7k
        } else {
6486
50.7k
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6487
50.7k
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6488
50.7k
        }
6489
51.8k
      }
6490
128k
    } else {
6491
182
      zend_update_jump_target_to_next(opnum_default_jmp);
6492
6493
182
      if (jumptable) {
6494
43
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6495
43
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6496
43
        opline->extended_value = get_next_op_number();
6497
43
      }
6498
182
    }
6499
6500
128k
    zend_compile_stmt(stmt_ast);
6501
128k
  }
6502
6503
47.6k
  if (!has_default_case) {
6504
47.4k
    zend_update_jump_target_to_next(opnum_default_jmp);
6505
6506
47.4k
    if (jumptable) {
6507
20.2k
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6508
20.2k
      opline->extended_value = get_next_op_number();
6509
20.2k
    }
6510
47.4k
  }
6511
6512
47.6k
  zend_end_loop(get_next_op_number(), &expr_node);
6513
6514
47.6k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6515
13.0k
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6516
13.0k
    opline->extended_value = ZEND_FREE_SWITCH;
6517
34.6k
  } else if (expr_node.op_type == IS_CONST) {
6518
34.2k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6519
34.2k
  }
6520
6521
47.6k
  efree(jmpnz_opnums);
6522
47.6k
}
6523
/* }}} */
6524
6525
static uint32_t count_match_conds(const zend_ast_list *arms)
6526
3.25k
{
6527
3.25k
  uint32_t num_conds = 0;
6528
6529
9.93k
  for (uint32_t i = 0; i < arms->children; i++) {
6530
6.67k
    const zend_ast *arm_ast = arms->child[i];
6531
6.67k
    if (arm_ast->child[0] == NULL) {
6532
691
      continue;
6533
691
    }
6534
6535
5.98k
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6536
5.98k
    num_conds += conds->children;
6537
5.98k
  }
6538
6539
3.25k
  return num_conds;
6540
3.25k
}
6541
6542
3.25k
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6543
7.36k
  for (uint32_t i = 0; i < arms->children; i++) {
6544
5.58k
    const zend_ast *arm_ast = arms->child[i];
6545
5.58k
    if (!arm_ast->child[0]) {
6546
      /* Skip default arm */
6547
503
      continue;
6548
503
    }
6549
6550
5.08k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6551
12.3k
    for (uint32_t j = 0; j < conds->children; j++) {
6552
8.78k
      zend_ast **cond_ast = &conds->child[j];
6553
6554
8.78k
      zend_eval_const_expr(cond_ast);
6555
8.78k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6556
1.24k
        return 0;
6557
1.24k
      }
6558
6559
7.53k
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6560
7.53k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6561
241
        return 0;
6562
241
      }
6563
7.53k
    }
6564
5.08k
  }
6565
6566
1.77k
  return 1;
6567
3.25k
}
6568
6569
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6570
283
{
6571
283
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6572
    /* Assert compilation adds a message operand, but this is incompatible with the
6573
     * pipe optimization that uses a temporary znode for the reference elimination.
6574
     * Therefore, disable the optimization for assert.
6575
     * Note that "assert" as a name is always treated as fully qualified. */
6576
214
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6577
214
  }
6578
6579
69
  return true;
6580
283
}
6581
6582
static void zend_compile_pipe(znode *result, zend_ast *ast)
6583
214k
{
6584
214k
  zend_ast *operand_ast = ast->child[0];
6585
214k
  zend_ast *callable_ast = ast->child[1];
6586
6587
214k
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6588
9
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6589
9
  }
6590
6591
  /* Compile the left hand side down to a value first. */
6592
214k
  znode operand_result;
6593
214k
  zend_compile_expr(&operand_result, operand_ast);
6594
6595
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6596
   * always fail. They will already fail in complex cases like arrays,
6597
   * so those don't need a wrapper. */
6598
214k
  znode wrapped_operand_result;
6599
214k
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6600
173k
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6601
173k
  } else {
6602
40.9k
    wrapped_operand_result = operand_result;
6603
40.9k
  }
6604
6605
  /* Turn the operand into a function parameter list. */
6606
214k
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6607
6608
214k
  zend_ast *fcall_ast;
6609
214k
  znode callable_result;
6610
6611
  /* Turn $foo |> bar(...) into bar($foo). */
6612
214k
  if (callable_ast->kind == ZEND_AST_CALL
6613
508
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6614
283
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6615
255
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6616
255
        callable_ast->child[0], arg_list_ast);
6617
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6618
213k
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6619
1.28k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6620
267
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6621
267
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6622
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6623
213k
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6624
1.01k
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6625
682
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6626
682
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6627
  /* Turn $foo |> $expr into ($expr)($foo) */
6628
212k
  } else {
6629
212k
    zend_compile_expr(&callable_result, callable_ast);
6630
212k
    callable_ast = zend_ast_create_znode(&callable_result);
6631
212k
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6632
212k
      callable_ast, arg_list_ast);
6633
212k
  }
6634
6635
214k
  zend_do_extended_stmt(&operand_result);
6636
6637
214k
  zend_compile_expr(result, fcall_ast);
6638
214k
}
6639
6640
static void zend_compile_match(znode *result, zend_ast *ast)
6641
3.25k
{
6642
3.25k
  zend_ast *expr_ast = ast->child[0];
6643
3.25k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6644
3.25k
  bool has_default_arm = false;
6645
3.25k
  uint32_t opnum_match = (uint32_t)-1;
6646
6647
3.25k
  znode expr_node;
6648
3.25k
  zend_compile_expr(&expr_node, expr_ast);
6649
6650
3.25k
  znode case_node;
6651
3.25k
  case_node.op_type = IS_TMP_VAR;
6652
3.25k
  case_node.u.op.var = get_temporary_variable();
6653
6654
3.25k
  uint32_t num_conds = count_match_conds(arms);
6655
3.25k
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6656
3.25k
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6657
3.25k
  HashTable *jumptable = NULL;
6658
3.25k
  uint32_t *jmpnz_opnums = NULL;
6659
6660
9.90k
  for (uint32_t i = 0; i < arms->children; ++i) {
6661
6.65k
    zend_ast *arm_ast = arms->child[i];
6662
6663
6.65k
    if (!arm_ast->child[0]) {
6664
676
      if (has_default_arm) {
6665
9
        CG(zend_lineno) = arm_ast->lineno;
6666
9
        zend_error_noreturn(E_COMPILE_ERROR,
6667
9
          "Match expressions may only contain one default arm");
6668
9
      }
6669
667
      has_default_arm = true;
6670
667
    }
6671
6.65k
  }
6672
6673
3.25k
  if (uses_jumptable) {
6674
1.25k
    znode jumptable_op;
6675
6676
1.25k
    ALLOC_HASHTABLE(jumptable);
6677
1.25k
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6678
1.25k
    jumptable_op.op_type = IS_CONST;
6679
1.25k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6680
6681
1.25k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6682
1.25k
    if (opline->op1_type == IS_CONST) {
6683
565
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6684
565
    }
6685
1.25k
    opnum_match = opline - CG(active_op_array)->opcodes;
6686
1.99k
  } else {
6687
1.99k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6688
1.99k
    uint32_t cond_count = 0;
6689
6.32k
    for (uint32_t i = 0; i < arms->children; ++i) {
6690
4.33k
      zend_ast *arm_ast = arms->child[i];
6691
6692
4.33k
      if (!arm_ast->child[0]) {
6693
421
        continue;
6694
421
      }
6695
6696
3.90k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6697
9.70k
      for (uint32_t j = 0; j < conds->children; j++) {
6698
5.79k
        zend_ast *cond_ast = conds->child[j];
6699
6700
5.79k
        znode cond_node;
6701
5.79k
        zend_compile_expr(&cond_node, cond_ast);
6702
6703
5.79k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6704
5.79k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6705
5.79k
        SET_NODE(opline->result, &case_node);
6706
5.79k
        if (opline->op1_type == IS_CONST) {
6707
2.15k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6708
2.15k
        }
6709
6710
5.79k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6711
6712
5.79k
        cond_count++;
6713
5.79k
      }
6714
3.90k
    }
6715
1.99k
  }
6716
6717
3.25k
  uint32_t opnum_default_jmp = 0;
6718
3.25k
  if (!uses_jumptable) {
6719
1.99k
    opnum_default_jmp = zend_emit_jump(0);
6720
1.99k
  }
6721
6722
3.25k
  bool is_first_case = true;
6723
3.25k
  uint32_t cond_count = 0;
6724
3.25k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6725
6726
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6727
  // for the arm result is freed even though it has not been initialized yet.
6728
3.25k
  if (!has_default_arm) {
6729
2.59k
    if (!uses_jumptable) {
6730
1.57k
      zend_update_jump_target_to_next(opnum_default_jmp);
6731
1.57k
    }
6732
6733
2.59k
    if (jumptable) {
6734
1.01k
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6735
1.01k
      opline->extended_value = get_next_op_number();
6736
1.01k
    }
6737
6738
2.59k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6739
2.59k
    if (opline->op1_type == IS_CONST) {
6740
808
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6741
808
    }
6742
2.59k
    if (arms->children == 0) {
6743
      /* Mark this as an "expression throw" for opcache. */
6744
228
      opline->extended_value = ZEND_THROW_IS_EXPR;
6745
228
    }
6746
2.59k
  }
6747
6748
9.87k
  for (uint32_t i = 0; i < arms->children; ++i) {
6749
6.62k
    zend_ast *arm_ast = arms->child[i];
6750
6.62k
    zend_ast *body_ast = arm_ast->child[1];
6751
6752
6.62k
    if (arm_ast->child[0] != NULL) {
6753
5.96k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6754
6755
16.8k
      for (uint32_t j = 0; j < conds->children; j++) {
6756
10.8k
        zend_ast *cond_ast = conds->child[j];
6757
6758
10.8k
        if (jmpnz_opnums != NULL) {
6759
5.79k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6760
5.79k
        }
6761
6762
10.8k
        if (jumptable) {
6763
5.06k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6764
5.06k
          zval jmp_target;
6765
5.06k
          ZVAL_LONG(&jmp_target, get_next_op_number());
6766
6767
5.06k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6768
4.47k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6769
4.47k
          } else {
6770
590
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6771
590
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6772
590
          }
6773
5.06k
        }
6774
6775
10.8k
        cond_count++;
6776
10.8k
      }
6777
5.96k
    } else {
6778
658
      if (!uses_jumptable) {
6779
421
        zend_update_jump_target_to_next(opnum_default_jmp);
6780
421
      }
6781
6782
658
      if (jumptable) {
6783
237
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6784
237
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6785
237
        opline->extended_value = get_next_op_number();
6786
237
      }
6787
658
    }
6788
6789
6.62k
    znode body_node;
6790
6.62k
    zend_compile_expr(&body_node, body_ast);
6791
6792
6.62k
    if (is_first_case) {
6793
3.02k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6794
3.02k
      is_first_case = false;
6795
3.60k
    } else {
6796
3.60k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6797
3.60k
      SET_NODE(opline_qm_assign->result, result);
6798
3.60k
    }
6799
6800
6.62k
    jmp_end_opnums[i] = zend_emit_jump(0);
6801
6.62k
  }
6802
6803
  // Initialize result in case there is no arm
6804
3.25k
  if (arms->children == 0) {
6805
228
    result->op_type = IS_CONST;
6806
228
    ZVAL_NULL(&result->u.constant);
6807
228
  }
6808
6809
9.87k
  for (uint32_t i = 0; i < arms->children; ++i) {
6810
6.62k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6811
6.62k
  }
6812
6813
3.25k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6814
1.58k
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6815
1.58k
    opline->extended_value = ZEND_FREE_SWITCH;
6816
1.66k
  } else if (expr_node.op_type == IS_CONST) {
6817
1.15k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6818
1.15k
  }
6819
6820
3.25k
  if (jmpnz_opnums != NULL) {
6821
1.99k
    efree(jmpnz_opnums);
6822
1.99k
  }
6823
3.25k
  efree(jmp_end_opnums);
6824
3.25k
}
6825
6826
static void zend_compile_try(const zend_ast *ast) /* {{{ */
6827
48.8k
{
6828
48.8k
  zend_ast *try_ast = ast->child[0];
6829
48.8k
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6830
48.8k
  zend_ast *finally_ast = ast->child[2];
6831
6832
48.8k
  uint32_t i, j;
6833
48.8k
  zend_op *opline;
6834
48.8k
  uint32_t try_catch_offset;
6835
48.8k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6836
48.8k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6837
48.8k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6838
6839
48.8k
  if (catches->children == 0 && !finally_ast) {
6840
68
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6841
68
  }
6842
6843
  /* label: try { } must not be equal to try { label: } */
6844
48.7k
  if (CG(context).labels) {
6845
404
    zend_label *label;
6846
404
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6847
404
      if (label->opline_num == get_next_op_number()) {
6848
102
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6849
102
      }
6850
404
      break;
6851
1.21k
    } ZEND_HASH_FOREACH_END();
6852
404
  }
6853
6854
48.7k
  try_catch_offset = zend_add_try_element(get_next_op_number());
6855
6856
48.7k
  if (finally_ast) {
6857
10.6k
    zend_loop_var fast_call;
6858
10.6k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6859
2.23k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6860
2.23k
    }
6861
10.6k
    CG(context).fast_call_var = get_temporary_variable();
6862
6863
    /* Push FAST_CALL on unwind stack */
6864
10.6k
    fast_call.opcode = ZEND_FAST_CALL;
6865
10.6k
    fast_call.var_type = IS_TMP_VAR;
6866
10.6k
    fast_call.var_num = CG(context).fast_call_var;
6867
10.6k
    fast_call.try_catch_offset = try_catch_offset;
6868
10.6k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6869
10.6k
  }
6870
6871
48.7k
  CG(context).try_catch_offset = try_catch_offset;
6872
6873
48.7k
  zend_compile_stmt(try_ast);
6874
6875
48.7k
  if (catches->children != 0) {
6876
38.2k
    jmp_opnums[0] = zend_emit_jump(0);
6877
38.2k
  }
6878
6879
92.4k
  for (i = 0; i < catches->children; ++i) {
6880
43.6k
    const zend_ast *catch_ast = catches->child[i];
6881
43.6k
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6882
43.6k
    zend_ast *var_ast = catch_ast->child[1];
6883
43.6k
    zend_ast *stmt_ast = catch_ast->child[2];
6884
43.6k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6885
43.6k
    bool is_last_catch = (i + 1 == catches->children);
6886
6887
43.6k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6888
43.6k
    uint32_t opnum_catch = (uint32_t)-1;
6889
6890
43.6k
    CG(zend_lineno) = catch_ast->lineno;
6891
6892
90.9k
    for (j = 0; j < classes->children; j++) {
6893
47.3k
      zend_ast *class_ast = classes->child[j];
6894
47.3k
      bool is_last_class = (j + 1 == classes->children);
6895
6896
47.3k
      if (!zend_is_const_default_class_ref(class_ast)) {
6897
7
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6898
7
      }
6899
6900
47.3k
      opnum_catch = get_next_op_number();
6901
47.3k
      if (i == 0 && j == 0) {
6902
38.2k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6903
38.2k
      }
6904
6905
47.3k
      opline = get_next_op();
6906
47.3k
      opline->opcode = ZEND_CATCH;
6907
47.3k
      opline->op1_type = IS_CONST;
6908
47.3k
      opline->op1.constant = zend_add_class_name_literal(
6909
47.3k
          zend_resolve_class_name_ast(class_ast));
6910
47.3k
      opline->extended_value = zend_alloc_cache_slot();
6911
6912
47.3k
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6913
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6914
7
      }
6915
6916
47.3k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6917
47.3k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6918
6919
47.3k
      if (is_last_catch && is_last_class) {
6920
38.2k
        opline->extended_value |= ZEND_LAST_CATCH;
6921
38.2k
      }
6922
6923
47.3k
      if (!is_last_class) {
6924
3.70k
        jmp_multicatch[j] = zend_emit_jump(0);
6925
3.70k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6926
3.70k
        opline->op2.opline_num = get_next_op_number();
6927
3.70k
      }
6928
47.3k
    }
6929
6930
47.3k
    for (j = 0; j < classes->children - 1; j++) {
6931
3.70k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6932
3.70k
    }
6933
6934
43.6k
    efree(jmp_multicatch);
6935
6936
43.6k
    zend_compile_stmt(stmt_ast);
6937
6938
43.6k
    if (!is_last_catch) {
6939
5.35k
      jmp_opnums[i + 1] = zend_emit_jump(0);
6940
5.35k
    }
6941
6942
43.6k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6943
43.6k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6944
43.6k
    if (!is_last_catch) {
6945
5.35k
      opline->op2.opline_num = get_next_op_number();
6946
5.35k
    }
6947
43.6k
  }
6948
6949
92.3k
  for (i = 0; i < catches->children; ++i) {
6950
43.6k
    zend_update_jump_target_to_next(jmp_opnums[i]);
6951
43.6k
  }
6952
6953
48.7k
  if (finally_ast) {
6954
10.6k
    zend_loop_var discard_exception;
6955
10.6k
    uint32_t opnum_jmp = get_next_op_number() + 1;
6956
6957
    /* Pop FAST_CALL from unwind stack */
6958
10.6k
    zend_stack_del_top(&CG(loop_var_stack));
6959
6960
    /* Push DISCARD_EXCEPTION on unwind stack */
6961
10.6k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6962
10.6k
    discard_exception.var_type = IS_TMP_VAR;
6963
10.6k
    discard_exception.var_num = CG(context).fast_call_var;
6964
10.6k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6965
6966
10.6k
    CG(zend_lineno) = finally_ast->lineno;
6967
6968
10.6k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6969
10.6k
    opline->op1.num = try_catch_offset;
6970
10.6k
    opline->result_type = IS_TMP_VAR;
6971
10.6k
    opline->result.var = CG(context).fast_call_var;
6972
6973
10.6k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6974
6975
10.6k
    zend_compile_stmt(finally_ast);
6976
6977
10.6k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6978
10.6k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6979
10.6k
      = get_next_op_number();
6980
6981
10.6k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6982
10.6k
    opline->op1_type = IS_TMP_VAR;
6983
10.6k
    opline->op1.var = CG(context).fast_call_var;
6984
10.6k
    opline->op2.num = orig_try_catch_offset;
6985
6986
10.6k
    zend_update_jump_target_to_next(opnum_jmp);
6987
6988
10.6k
    CG(context).fast_call_var = orig_fast_call_var;
6989
6990
    /* Pop DISCARD_EXCEPTION from unwind stack */
6991
10.6k
    zend_stack_del_top(&CG(loop_var_stack));
6992
10.6k
  }
6993
6994
48.7k
  CG(context).try_catch_offset = orig_try_catch_offset;
6995
6996
48.7k
  efree(jmp_opnums);
6997
48.7k
}
6998
/* }}} */
6999
7000
/* Encoding declarations must already be handled during parsing */
7001
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7002
2.99k
{
7003
2.99k
  const zend_ast_list *declares = zend_ast_get_list(ast);
7004
2.99k
  uint32_t i;
7005
7.89k
  for (i = 0; i < declares->children; ++i) {
7006
4.91k
    const zend_ast *declare_ast = declares->child[i];
7007
4.91k
    zend_ast *name_ast = declare_ast->child[0];
7008
4.91k
    zend_ast *value_ast = declare_ast->child[1];
7009
4.91k
    const zend_string *name = zend_ast_get_str(name_ast);
7010
7011
4.91k
    if (zend_string_equals_literal_ci(name, "encoding")) {
7012
203
      if (value_ast->kind != ZEND_AST_ZVAL) {
7013
20
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7014
20
        return 0;
7015
20
      }
7016
7017
183
      if (CG(multibyte)) {
7018
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7019
7020
0
        const zend_encoding *new_encoding, *old_encoding;
7021
0
        zend_encoding_filter old_input_filter;
7022
7023
0
        CG(encoding_declared) = 1;
7024
7025
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7026
0
        if (!new_encoding) {
7027
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7028
0
        } else {
7029
0
          old_input_filter = LANG_SCNG(input_filter);
7030
0
          old_encoding = LANG_SCNG(script_encoding);
7031
0
          zend_multibyte_set_filter(new_encoding);
7032
7033
          /* need to re-scan if input filter changed */
7034
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7035
0
             (old_input_filter && new_encoding != old_encoding)) {
7036
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7037
0
          }
7038
0
        }
7039
7040
0
        zend_string_release_ex(encoding_name, 0);
7041
183
      } else {
7042
183
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7043
183
          "Zend multibyte feature is turned off by settings");
7044
183
      }
7045
183
    }
7046
4.91k
  }
7047
7048
2.97k
  return 1;
7049
2.99k
}
7050
/* }}} */
7051
7052
/* Check whether this is the first statement, not counting declares. */
7053
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7054
4.10k
{
7055
4.10k
  uint32_t i = 0;
7056
4.10k
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7057
7058
15.9k
  while (i < file_ast->children) {
7059
15.9k
    if (file_ast->child[i] == ast) {
7060
4.03k
      return SUCCESS;
7061
11.8k
    } else if (file_ast->child[i] == NULL) {
7062
291
      if (!allow_nop) {
7063
0
        return FAILURE;
7064
0
      }
7065
11.5k
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7066
68
      return FAILURE;
7067
68
    }
7068
11.7k
    i++;
7069
11.7k
  }
7070
1
  return FAILURE;
7071
4.10k
}
7072
/* }}} */
7073
7074
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7075
6.89k
{
7076
6.89k
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7077
6.89k
  zend_ast *stmt_ast = ast->child[1];
7078
6.89k
  zend_declarables orig_declarables = FC(declarables);
7079
6.89k
  uint32_t i;
7080
7081
23.8k
  for (i = 0; i < declares->children; ++i) {
7082
17.1k
    zend_ast *declare_ast = declares->child[i];
7083
17.1k
    zend_ast *name_ast = declare_ast->child[0];
7084
17.1k
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7085
17.1k
    zend_string *name = zend_ast_get_str(name_ast);
7086
7087
17.1k
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7088
41
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7089
41
    }
7090
7091
17.0k
    if (zend_string_equals_literal_ci(name, "ticks")) {
7092
8.10k
      zval value_zv;
7093
8.10k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7094
8.10k
      FC(declarables).ticks = zval_get_long(&value_zv);
7095
8.10k
      zval_ptr_dtor_nogc(&value_zv);
7096
8.99k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7097
7098
148
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7099
8
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7100
8
          "the very first statement in the script");
7101
8
      }
7102
8.84k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7103
583
      zval value_zv;
7104
7105
583
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7106
18
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7107
18
          "the very first statement in the script");
7108
18
      }
7109
7110
565
      if (ast->child[1] != NULL) {
7111
7
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7112
7
          "use block mode");
7113
7
      }
7114
7115
558
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7116
7117
558
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7118
66
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7119
66
      }
7120
7121
492
      if (Z_LVAL(value_zv) == 1) {
7122
343
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7123
343
      }
7124
7125
8.26k
    } else {
7126
8.26k
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7127
8.26k
    }
7128
17.0k
  }
7129
7130
6.75k
  if (stmt_ast) {
7131
651
    zend_compile_stmt(stmt_ast);
7132
7133
651
    FC(declarables) = orig_declarables;
7134
651
  }
7135
6.75k
}
7136
/* }}} */
7137
7138
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7139
1.88M
{
7140
1.88M
  const zend_ast_list *list = zend_ast_get_list(ast);
7141
1.88M
  uint32_t i;
7142
8.11M
  for (i = 0; i < list->children; ++i) {
7143
6.23M
    zend_compile_stmt(list->child[i]);
7144
6.23M
  }
7145
1.88M
}
7146
/* }}} */
7147
7148
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7149
1.39M
{
7150
1.39M
  uint32_t i, n;
7151
7152
1.39M
  func->common.arg_flags[0] = 0;
7153
1.39M
  func->common.arg_flags[1] = 0;
7154
1.39M
  func->common.arg_flags[2] = 0;
7155
1.39M
  if (func->common.arg_info) {
7156
1.39M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7157
1.39M
    i = 0;
7158
2.85M
    while (i < n) {
7159
1.46M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7160
1.46M
      i++;
7161
1.46M
    }
7162
1.39M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7163
1.10k
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7164
14.2k
      while (i < MAX_ARG_FLAG_NUM) {
7165
13.1k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7166
13.1k
        i++;
7167
13.1k
      }
7168
1.10k
    }
7169
1.39M
  }
7170
1.39M
}
7171
/* }}} */
7172
7173
static zend_type zend_compile_single_typename(zend_ast *ast)
7174
557k
{
7175
557k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7176
557k
  if (ast->kind == ZEND_AST_TYPE) {
7177
4.49k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7178
10
      zend_error_noreturn(E_COMPILE_ERROR,
7179
10
        "Cannot use \"static\" when no class scope is active");
7180
10
    }
7181
7182
4.48k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7183
552k
  } else {
7184
552k
    zend_string *type_name = zend_ast_get_str(ast);
7185
552k
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7186
7187
552k
    if (type_code != 0) {
7188
182k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7189
7
        zend_error_noreturn(E_COMPILE_ERROR,
7190
7
          "Type declaration '%s' must be unqualified",
7191
7
          ZSTR_VAL(zend_string_tolower(type_name)));
7192
7
      }
7193
7194
      /* Transform iterable into a type union alias */
7195
182k
      if (type_code == IS_ITERABLE) {
7196
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7197
153k
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7198
153k
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7199
153k
        return iterable;
7200
153k
      }
7201
7202
28.8k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7203
369k
    } else {
7204
369k
      const char *correct_name;
7205
369k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7206
369k
      zend_string *class_name = type_name;
7207
7208
369k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7209
354k
        class_name = zend_resolve_class_name_ast(ast);
7210
354k
        zend_assert_valid_class_name(class_name, "a type name");
7211
354k
      } else {
7212
15.1k
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7213
7214
15.1k
        zend_ensure_valid_class_fetch_type(fetch_type);
7215
7216
15.1k
        bool substitute_self_parent = zend_is_scope_known()
7217
497
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7218
7219
15.1k
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7220
          /* Scope might be unknown for unbound closures and traits */
7221
14.8k
          if (substitute_self_parent) {
7222
352
            class_name = CG(active_class_entry)->name;
7223
352
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7224
352
          }
7225
14.8k
        } else {
7226
231
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7227
          /* Scope might be unknown for unbound closures and traits */
7228
231
          if (substitute_self_parent) {
7229
132
            class_name = CG(active_class_entry)->parent_name;
7230
132
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7231
132
          }
7232
210
        }
7233
15.1k
        zend_string_addref(class_name);
7234
15.0k
      }
7235
7236
369k
      if (ast->attr == ZEND_NAME_NOT_FQ
7237
369k
          && zend_is_confusable_type(type_name, &correct_name)
7238
43.9k
          && zend_is_not_imported(type_name)) {
7239
43.9k
        const char *extra =
7240
43.9k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7241
43.9k
        if (correct_name) {
7242
43.6k
          zend_error(E_COMPILE_WARNING,
7243
43.6k
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7244
43.6k
            "Write \"\\%s\"%s to suppress this warning",
7245
43.6k
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7246
43.6k
        } else {
7247
260
          zend_error(E_COMPILE_WARNING,
7248
260
            "\"%s\" is not a supported builtin type "
7249
260
            "and will be interpreted as a class name. "
7250
260
            "Write \"\\%s\"%s to suppress this warning",
7251
260
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7252
260
        }
7253
43.9k
      }
7254
7255
369k
      class_name = zend_new_interned_string(class_name);
7256
369k
      zend_alloc_ce_cache(class_name);
7257
369k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7258
369k
    }
7259
552k
  }
7260
557k
}
7261
7262
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7263
14.6k
{
7264
14.6k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7265
14.6k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7266
14.6k
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7267
14.6k
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7268
14.6k
  const zend_type_list *smaller_type_list, *larger_type_list;
7269
14.6k
  bool flipped = false;
7270
7271
14.6k
  if (r_type_list->num_types < l_type_list->num_types) {
7272
4.75k
    smaller_type_list = r_type_list;
7273
4.75k
    larger_type_list = l_type_list;
7274
4.75k
    flipped = true;
7275
9.87k
  } else {
7276
9.87k
    smaller_type_list = l_type_list;
7277
9.87k
    larger_type_list = r_type_list;
7278
9.87k
  }
7279
7280
14.6k
  unsigned int sum = 0;
7281
14.6k
  const zend_type *outer_type;
7282
57.1k
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7283
57.1k
    const zend_type *inner_type;
7284
196k
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7285
196k
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7286
14.7k
        sum++;
7287
14.7k
        break;
7288
14.7k
      }
7289
196k
    ZEND_TYPE_LIST_FOREACH_END();
7290
57.1k
  ZEND_TYPE_LIST_FOREACH_END();
7291
7292
14.6k
  if (sum == smaller_type_list->num_types) {
7293
35
    zend_string *smaller_type_str;
7294
35
    zend_string *larger_type_str;
7295
35
    if (flipped) {
7296
10
      smaller_type_str = zend_type_to_string(right_type);
7297
10
      larger_type_str = zend_type_to_string(left_type);
7298
25
    } else {
7299
25
      smaller_type_str = zend_type_to_string(left_type);
7300
25
      larger_type_str = zend_type_to_string(right_type);
7301
25
    }
7302
35
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7303
18
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7304
18
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7305
18
    } else {
7306
17
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7307
17
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7308
17
    }
7309
35
  }
7310
14.6k
}
7311
7312
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7313
18.8k
{
7314
18.8k
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7315
18.8k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7316
7317
18.8k
  const zend_type *single_intersection_type = NULL;
7318
67.2k
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7319
67.2k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7320
10
      zend_string *single_type_str = zend_type_to_string(single_type);
7321
10
      zend_string *complete_type = zend_type_to_string(intersection_type);
7322
10
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7323
10
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7324
10
    }
7325
67.2k
  ZEND_TYPE_FOREACH_END();
7326
18.8k
}
7327
7328
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7329
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7330
70.5k
{
7331
70.5k
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7332
136k
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7333
66.3k
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7334
14.6k
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7335
14.6k
      continue;
7336
14.6k
    }
7337
51.6k
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7338
61
      zend_string *single_type_str = zend_type_to_string(type);
7339
61
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7340
61
    }
7341
51.6k
  }
7342
70.5k
}
7343
7344
static zend_type zend_compile_typename(zend_ast *ast);
7345
7346
static zend_type zend_compile_typename_ex(
7347
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7348
513k
{
7349
513k
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7350
513k
  zend_ast_attr orig_ast_attr = ast->attr;
7351
513k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7352
7353
513k
  if (is_marked_nullable) {
7354
5.08k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7355
5.08k
  }
7356
7357
513k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7358
28.6k
    const zend_ast_list *list = zend_ast_get_list(ast);
7359
28.6k
    zend_type_list *type_list;
7360
28.6k
    bool is_composite = false;
7361
28.6k
    bool has_only_iterable_class = true;
7362
28.6k
    ALLOCA_FLAG(use_heap)
7363
7364
28.6k
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7365
28.6k
    type_list->num_types = 0;
7366
7367
91.1k
    for (uint32_t i = 0; i < list->children; i++) {
7368
62.5k
      zend_ast *type_ast = list->child[i];
7369
62.5k
      zend_type single_type;
7370
62.5k
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7371
7372
62.5k
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7373
13.9k
        has_only_iterable_class = false;
7374
13.9k
        is_composite = true;
7375
        /* The first class type can be stored directly as the type ptr payload. */
7376
13.9k
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7377
          /* Switch from single name to name list. */
7378
319
          type_list->num_types = 1;
7379
319
          type_list->types[0] = type;
7380
          /* Clear MAY_BE_* type flags */
7381
319
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7382
319
        }
7383
        /* Mark type as list type */
7384
13.9k
        ZEND_TYPE_SET_LIST(type, type_list);
7385
7386
13.9k
        single_type = zend_compile_typename(type_ast);
7387
13.9k
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7388
7389
13.9k
        type_list->types[type_list->num_types++] = single_type;
7390
7391
        /* Check for trivially redundant class types */
7392
32.7k
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7393
18.8k
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7394
14.6k
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7395
14.6k
            continue;
7396
14.6k
          }
7397
          /* Type from type list is a simple type */
7398
4.20k
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7399
4.20k
        }
7400
13.9k
        continue;
7401
13.9k
      }
7402
7403
48.5k
      single_type = zend_compile_single_typename(type_ast);
7404
48.5k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7405
7406
48.5k
      if (single_type_mask == MAY_BE_ANY) {
7407
7
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7408
7
      }
7409
48.5k
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7410
42.5k
        has_only_iterable_class = false;
7411
42.5k
      }
7412
7413
48.5k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7414
48.5k
      if (type_mask_overlap) {
7415
42
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7416
42
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7417
42
        zend_error_noreturn(E_COMPILE_ERROR,
7418
42
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7419
42
      }
7420
7421
48.5k
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7422
48.5k
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7423
14
        zend_error_noreturn(E_COMPILE_ERROR,
7424
14
          "Type contains both true and false, bool must be used instead");
7425
14
      }
7426
48.4k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7427
      /* Clear MAY_BE_* type flags */
7428
48.4k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7429
7430
48.4k
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7431
43.3k
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7432
          /* The first class type can be stored directly as the type ptr payload. */
7433
16.2k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7434
16.2k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7435
27.0k
        } else {
7436
27.0k
          if (type_list->num_types == 0) {
7437
            /* Switch from single name to name list. */
7438
14.4k
            type_list->num_types = 1;
7439
14.4k
            type_list->types[0] = type;
7440
            /* Clear MAY_BE_* type flags */
7441
14.4k
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7442
14.4k
            ZEND_TYPE_SET_LIST(type, type_list);
7443
14.4k
          }
7444
7445
27.0k
          type_list->types[type_list->num_types++] = single_type;
7446
7447
          /* Check for trivially redundant class types */
7448
27.0k
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7449
27.0k
        }
7450
43.3k
      }
7451
48.4k
    }
7452
7453
28.6k
    if (type_list->num_types) {
7454
25.7k
      zend_type_list *list = zend_arena_alloc(
7455
25.7k
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7456
25.7k
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7457
25.7k
      ZEND_TYPE_SET_LIST(type, list);
7458
25.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7459
      /* Inform that the type list is a union type */
7460
25.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7461
25.7k
    }
7462
7463
28.6k
    free_alloca(type_list, use_heap);
7464
7465
28.6k
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7466
28.6k
    if ((type_mask & MAY_BE_OBJECT) &&
7467
861
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7468
39
      zend_string *type_str = zend_type_to_string(type);
7469
39
      zend_error_noreturn(E_COMPILE_ERROR,
7470
39
        "Type %s contains both object and a class type, which is redundant",
7471
39
        ZSTR_VAL(type_str));
7472
39
    }
7473
484k
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7474
19.8k
    const zend_ast_list *list = zend_ast_get_list(ast);
7475
19.8k
    zend_type_list *type_list;
7476
7477
    /* Allocate the type list directly on the arena as it must be a type
7478
     * list of the same number of elements as the AST list has children */
7479
19.8k
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7480
19.8k
    type_list->num_types = 0;
7481
7482
19.8k
    ZEND_ASSERT(list->children > 1);
7483
7484
63.3k
    for (uint32_t i = 0; i < list->children; i++) {
7485
43.5k
      zend_ast *type_ast = list->child[i];
7486
43.5k
      zend_type single_type = zend_compile_single_typename(type_ast);
7487
7488
      /* An intersection of union types cannot exist so invalidate it
7489
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7490
43.5k
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7491
7
        zend_string *standard_type_str = zend_type_to_string(single_type);
7492
7
        zend_error_noreturn(E_COMPILE_ERROR,
7493
7
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7494
0
        zend_string_release_ex(standard_type_str, false);
7495
0
      }
7496
      /* An intersection of standard types cannot exist so invalidate it */
7497
43.5k
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7498
85
        zend_string *standard_type_str = zend_type_to_string(single_type);
7499
85
        zend_error_noreturn(E_COMPILE_ERROR,
7500
85
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7501
0
        zend_string_release_ex(standard_type_str, false);
7502
0
      }
7503
      /* Check for "self" and "parent" too */
7504
43.4k
      if (
7505
43.4k
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7506
43.4k
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7507
43.4k
      ) {
7508
14
        zend_error_noreturn(E_COMPILE_ERROR,
7509
14
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7510
14
      }
7511
7512
      /* Add type to the type list */
7513
43.4k
      type_list->types[type_list->num_types++] = single_type;
7514
7515
      /* Check for trivially redundant class types */
7516
43.4k
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7517
43.4k
    }
7518
7519
19.7k
    ZEND_ASSERT(list->children == type_list->num_types);
7520
7521
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7522
19.7k
    if (force_allow_null) {
7523
3.95k
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7524
3.95k
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7525
3.95k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7526
3.95k
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7527
7528
3.95k
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7529
3.95k
      dnf_type_list->num_types = 1;
7530
3.95k
      dnf_type_list->types[0] = intersection_type;
7531
3.95k
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7532
      /* Inform that the type list is a DNF type */
7533
3.95k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7534
3.95k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7535
15.7k
    } else {
7536
15.7k
      ZEND_TYPE_SET_LIST(type, type_list);
7537
      /* Inform that the type list is an intersection type */
7538
15.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7539
15.7k
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7540
15.7k
    }
7541
464k
  } else {
7542
464k
    type = zend_compile_single_typename(ast);
7543
464k
  }
7544
7545
513k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7546
7547
513k
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7548
22
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7549
22
  }
7550
7551
513k
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7552
7
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7553
7
  }
7554
7555
513k
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7556
4.98k
    *forced_allow_null = true;
7557
4.98k
  }
7558
7559
513k
  if (is_marked_nullable || force_allow_null) {
7560
10.1k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7561
10.1k
    type_mask = ZEND_TYPE_PURE_MASK(type);
7562
10.1k
  }
7563
7564
513k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7565
14
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7566
14
  }
7567
7568
513k
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7569
8
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7570
8
  }
7571
7572
513k
  ast->attr = orig_ast_attr;
7573
513k
  return type;
7574
513k
}
7575
/* }}} */
7576
7577
static zend_type zend_compile_typename(zend_ast *ast)
7578
83.2k
{
7579
83.2k
  bool forced_allow_null;
7580
83.2k
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7581
83.2k
}
7582
7583
/* May convert value from int to float. */
7584
static bool zend_is_valid_default_value(zend_type type, zval *value)
7585
3.71k
{
7586
3.71k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7587
3.71k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7588
2.21k
    return 1;
7589
2.21k
  }
7590
1.50k
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7591
    /* Integers are allowed as initializers for floating-point values. */
7592
1.32k
    convert_to_double(value);
7593
1.32k
    return 1;
7594
1.32k
  }
7595
174
  return 0;
7596
1.50k
}
7597
7598
static void zend_compile_attributes(
7599
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7600
1.38M
) /* {{{ */ {
7601
1.38M
  zend_attribute *attr;
7602
1.38M
  zend_internal_attribute *config;
7603
7604
1.38M
  const zend_ast_list *list = zend_ast_get_list(ast);
7605
1.38M
  uint32_t g, i, j;
7606
7607
1.38M
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7608
7609
2.77M
  for (g = 0; g < list->children; g++) {
7610
1.39M
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7611
7612
1.39M
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7613
7614
4.07M
    for (i = 0; i < group->children; i++) {
7615
2.67M
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7616
7617
2.67M
      const zend_ast *el = group->child[i];
7618
7619
2.67M
      if (el->child[1] &&
7620
84.8k
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7621
14
          zend_error_noreturn(E_COMPILE_ERROR,
7622
14
              "Cannot create Closure as attribute argument");
7623
14
      }
7624
7625
2.67M
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7626
2.67M
      zend_string *lcname = zend_string_tolower_ex(name, false);
7627
2.67M
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7628
7629
2.67M
      config = zend_internal_attribute_get(lcname);
7630
2.67M
      zend_string_release(lcname);
7631
7632
      /* Exclude internal attributes that do not match on promoted properties. */
7633
2.67M
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7634
518
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7635
10
          zend_string_release(name);
7636
10
          continue;
7637
10
        }
7638
518
      }
7639
7640
2.67M
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7641
2.67M
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7642
2.67M
      attr = zend_add_attribute(
7643
2.67M
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7644
2.67M
      zend_string_release(name);
7645
7646
      /* Populate arguments */
7647
2.67M
      if (args) {
7648
84.8k
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7649
7650
84.8k
        bool uses_named_args = false;
7651
200k
        for (j = 0; j < args->children; j++) {
7652
115k
          zend_ast **arg_ast_ptr = &args->child[j];
7653
115k
          zend_ast *arg_ast = *arg_ast_ptr;
7654
7655
115k
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7656
7
            zend_error_noreturn(E_COMPILE_ERROR,
7657
7
              "Cannot use unpacking in attribute argument list");
7658
7
          }
7659
7660
115k
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7661
21.8k
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7662
21.8k
            arg_ast_ptr = &arg_ast->child[1];
7663
21.8k
            uses_named_args = true;
7664
7665
119k
            for (uint32_t k = 0; k < j; k++) {
7666
97.3k
              if (attr->args[k].name &&
7667
94.7k
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7668
9
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7669
9
                  ZSTR_VAL(attr->args[j].name));
7670
9
              }
7671
97.3k
            }
7672
93.7k
          } else if (uses_named_args) {
7673
42
            zend_error_noreturn(E_COMPILE_ERROR,
7674
42
              "Cannot use positional argument after named argument");
7675
42
          }
7676
7677
115k
          zend_const_expr_to_zval(
7678
115k
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7679
115k
        }
7680
84.8k
      }
7681
2.67M
    }
7682
1.39M
  }
7683
7684
1.38M
  if (*attributes != NULL) {
7685
    /* Allow delaying target validation for forward compatibility. */
7686
1.38M
    const zend_attribute *delayed_target_validation = NULL;
7687
1.38M
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7688
80.8k
      ZEND_ASSERT(offset >= 1);
7689
      /* zend_get_parameter_attribute_str will add 1 too */
7690
80.8k
      delayed_target_validation = zend_get_parameter_attribute_str(
7691
80.8k
        *attributes,
7692
80.8k
        "delayedtargetvalidation",
7693
80.8k
        strlen("delayedtargetvalidation"),
7694
80.8k
        offset - 1
7695
80.8k
      );
7696
1.30M
    } else {
7697
1.30M
      delayed_target_validation = zend_get_attribute_str(
7698
1.30M
        *attributes,
7699
1.30M
        "delayedtargetvalidation",
7700
1.30M
        strlen("delayedtargetvalidation")
7701
1.30M
      );
7702
1.30M
    }
7703
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7704
10.6M
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7705
10.6M
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7706
3.92M
        continue;
7707
3.92M
      }
7708
7709
10.6M
      bool run_validator = true;
7710
4.04k
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7711
294
        if (delayed_target_validation == NULL) {
7712
48
          zend_string *location = zend_get_attribute_target_names(target);
7713
48
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7714
7715
48
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7716
48
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7717
48
          );
7718
48
        }
7719
246
        run_validator = false;
7720
246
      }
7721
7722
3.99k
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7723
3.99k
        if (zend_is_attribute_repeated(*attributes, attr)) {
7724
28
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7725
28
        }
7726
3.99k
      }
7727
7728
      /* Validators are not run if the target is already invalid */
7729
3.96k
      if (run_validator && config->validator != NULL) {
7730
2.16k
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7731
2.16k
        if (error != NULL) {
7732
313
          if (delayed_target_validation == NULL) {
7733
100
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7734
0
            zend_string_efree(error);
7735
213
          } else {
7736
213
            attr->validation_error = error;
7737
213
          }
7738
313
        }
7739
2.16k
      }
7740
3.96k
    } ZEND_HASH_FOREACH_END();
7741
1.38M
  }
7742
1.38M
}
7743
/* }}} */
7744
7745
static void zend_compile_property_hooks(
7746
    zend_property_info *prop_info, zend_string *prop_name,
7747
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7748
7749
typedef struct {
7750
  const zend_string *property_name;
7751
  bool uses_property;
7752
} find_property_usage_context;
7753
7754
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7755
34.8k
{
7756
34.8k
  zend_ast *ast = *ast_ptr;
7757
34.8k
  find_property_usage_context *context = (find_property_usage_context *) _context;
7758
7759
34.8k
  if (ast == NULL) {
7760
417
    return;
7761
34.3k
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7762
2.90k
    const zend_ast *object_ast = ast->child[0];
7763
2.90k
    zend_ast *property_ast = ast->child[1];
7764
7765
2.90k
    if (object_ast->kind == ZEND_AST_VAR
7766
2.66k
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7767
2.54k
     && property_ast->kind == ZEND_AST_ZVAL) {
7768
2.48k
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7769
2.48k
      const zval *property = zend_ast_get_zval(property_ast);
7770
2.48k
      if (Z_TYPE_P(object) == IS_STRING
7771
2.45k
        && Z_TYPE_P(property) == IS_STRING
7772
2.45k
        && zend_string_equals_literal(Z_STR_P(object), "this")
7773
1.97k
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7774
659
        context->uses_property = true;
7775
        /* No need to look for references in this branch. */
7776
659
        return;
7777
659
      }
7778
2.48k
    }
7779
2.90k
  }
7780
7781
  /* Don't search across function/class boundaries. */
7782
33.7k
  if (!zend_ast_is_special(ast)) {
7783
21.1k
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7784
21.1k
  }
7785
33.7k
}
7786
7787
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7788
5.80k
{
7789
5.80k
  if (zend_string_equals_literal_ci(hook_name, "set")
7790
1.95k
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7791
512
    return true;
7792
512
  }
7793
7794
5.28k
  find_property_usage_context context = { property_name, false };
7795
5.28k
  zend_property_hook_find_property_usage(&hook_ast, &context);
7796
5.28k
  return context.uses_property;
7797
5.80k
}
7798
7799
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7800
46.5k
{
7801
46.5k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7802
269
    return true;
7803
269
  }
7804
46.2k
  if (!hooks_ast) {
7805
42.4k
    return false;
7806
42.4k
  }
7807
7808
46.2k
  bool is_virtual = true;
7809
7810
3.81k
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7811
10.2k
  for (uint32_t i = 0; i < hooks->children; i++) {
7812
6.39k
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
7813
6.39k
    zend_ast *body = hook->child[2];
7814
6.39k
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7815
1.14k
      is_virtual = false;
7816
1.14k
    }
7817
6.39k
  }
7818
7819
3.81k
  return is_virtual;
7820
46.2k
}
7821
7822
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7823
1.43M
{
7824
1.43M
  zend_ast_list *list = zend_ast_get_list(ast);
7825
1.43M
  uint32_t i;
7826
1.43M
  zend_op_array *op_array = CG(active_op_array);
7827
1.43M
  zend_arg_info *arg_infos;
7828
7829
1.43M
  if (return_type_ast || fallback_return_type) {
7830
    /* Use op_array->arg_info[-1] for return type */
7831
32.5k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7832
32.5k
    arg_infos->name = NULL;
7833
32.5k
    if (return_type_ast) {
7834
31.6k
      arg_infos->type = zend_compile_typename(return_type_ast);
7835
31.6k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7836
31.6k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7837
31.6k
    } else {
7838
884
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7839
884
    }
7840
32.5k
    arg_infos++;
7841
32.5k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7842
7843
32.5k
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7844
4.04k
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7845
120
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7846
120
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7847
120
      zend_string_release(func_name);
7848
120
    }
7849
1.40M
  } else {
7850
1.40M
    if (list->children == 0) {
7851
69.8k
      return;
7852
69.8k
    }
7853
1.33M
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7854
1.33M
  }
7855
7856
  /* Find last required parameter number for deprecation message. */
7857
1.36M
  uint32_t last_required_param = (uint32_t) -1;
7858
2.80M
  for (i = 0; i < list->children; ++i) {
7859
1.43M
    zend_ast *param_ast = list->child[i];
7860
1.43M
    zend_ast *default_ast_ptr = param_ast->child[2];
7861
1.43M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7862
1.43M
    if (!default_ast_ptr && !is_variadic) {
7863
1.42M
      last_required_param = i;
7864
1.42M
    }
7865
1.43M
  }
7866
7867
1.36M
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
7868
2.80M
  for (i = 0; i < list->children; ++i) {
7869
1.43M
    zend_ast *param_ast = list->child[i];
7870
1.43M
    zend_ast *type_ast = param_ast->child[0];
7871
1.43M
    zend_ast *var_ast = param_ast->child[1];
7872
1.43M
    zend_ast **default_ast_ptr = &param_ast->child[2];
7873
1.43M
    zend_ast *attributes_ast = param_ast->child[3];
7874
1.43M
    zend_ast *doc_comment_ast = param_ast->child[4];
7875
1.43M
    zend_ast *hooks_ast = param_ast->child[5];
7876
1.43M
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7877
1.43M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7878
1.43M
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7879
1.43M
    uint32_t property_flags = param_ast->attr & promotion_flags;
7880
1.43M
    bool is_promoted = property_flags || hooks_ast;
7881
7882
1.43M
    CG(zend_lineno) = param_ast->lineno;
7883
7884
1.43M
    znode var_node, default_node;
7885
1.43M
    uint8_t opcode;
7886
1.43M
    zend_op *opline;
7887
1.43M
    zend_arg_info *arg_info;
7888
7889
1.43M
    if (zend_is_auto_global(name)) {
7890
2
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7891
2
        ZSTR_VAL(name));
7892
2
    }
7893
7894
1.43M
    var_node.op_type = IS_CV;
7895
1.43M
    var_node.u.op.var = lookup_cv(name);
7896
7897
1.43M
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7898
31
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7899
31
        ZSTR_VAL(name));
7900
1.43M
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7901
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7902
1.43M
    } else if (zend_string_equals_literal(name, "http_response_header")) {
7903
71
      CG(context).has_assigned_to_http_response_header = true;
7904
71
    }
7905
7906
1.43M
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7907
8
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7908
8
    }
7909
7910
1.43M
    if (is_variadic) {
7911
1.59k
      opcode = ZEND_RECV_VARIADIC;
7912
1.59k
      default_node.op_type = IS_UNUSED;
7913
1.59k
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7914
7915
1.59k
      if (*default_ast_ptr) {
7916
7
        zend_error_noreturn(E_COMPILE_ERROR,
7917
7
          "Variadic parameter cannot have a default value");
7918
7
      }
7919
1.43M
    } else if (*default_ast_ptr) {
7920
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7921
13.6k
      uint32_t cops = CG(compiler_options);
7922
13.6k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7923
13.6k
      opcode = ZEND_RECV_INIT;
7924
13.6k
      default_node.op_type = IS_CONST;
7925
13.6k
      zend_const_expr_to_zval(
7926
13.6k
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7927
13.6k
      CG(compiler_options) = cops;
7928
1.42M
    } else {
7929
1.42M
      opcode = ZEND_RECV;
7930
1.42M
      default_node.op_type = IS_UNUSED;
7931
1.42M
      op_array->required_num_args = i + 1;
7932
1.42M
    }
7933
7934
1.43M
    arg_info = &arg_infos[i];
7935
1.43M
    arg_info->name = zend_string_copy(name);
7936
1.43M
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7937
7938
1.43M
    if (attributes_ast) {
7939
80.8k
      zend_compile_attributes(
7940
80.8k
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7941
80.8k
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7942
80.8k
      );
7943
80.8k
    }
7944
7945
1.43M
    bool forced_allow_nullable = false;
7946
1.43M
    if (type_ast) {
7947
430k
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7948
430k
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7949
7950
430k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7951
430k
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7952
430k
      if (forced_allow_nullable) {
7953
4.98k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7954
4.98k
        zend_error(E_DEPRECATED,
7955
4.98k
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7956
4.98k
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7957
4.98k
        zend_string_release(func_name);
7958
4.98k
      }
7959
7960
430k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7961
8
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7962
8
      }
7963
7964
430k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7965
8
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7966
8
      }
7967
7968
430k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7969
1.56k
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7970
44
        zend_string *type_str = zend_type_to_string(arg_info->type);
7971
44
        zend_error_noreturn(E_COMPILE_ERROR,
7972
44
          "Cannot use %s as default value for parameter $%s of type %s",
7973
44
          zend_get_type_by_const(default_type),
7974
44
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7975
44
      }
7976
430k
    }
7977
1.43M
    if (last_required_param != (uint32_t) -1
7978
1.42M
     && i < last_required_param
7979
95.5k
     && default_node.op_type == IS_CONST) {
7980
      /* Ignore parameters of the form "Type $param = null".
7981
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7982
6.07k
      if (!forced_allow_nullable) {
7983
1.69k
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7984
1.69k
        zend_ast *required_param_ast = list->child[last_required_param];
7985
1.69k
        zend_error(E_DEPRECATED,
7986
1.69k
          "%s(): Optional parameter $%s declared before required parameter $%s "
7987
1.69k
          "is implicitly treated as a required parameter",
7988
1.69k
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7989
1.69k
        zend_string_release(func_name);
7990
1.69k
      }
7991
7992
      /* Regardless of whether we issue a deprecation, convert this parameter into
7993
       * a required parameter without a default value. This ensures that it cannot be
7994
       * used as an optional parameter even with named parameters. */
7995
6.07k
      opcode = ZEND_RECV;
7996
6.07k
      default_node.op_type = IS_UNUSED;
7997
6.07k
      zval_ptr_dtor(&default_node.u.constant);
7998
6.07k
    }
7999
8000
1.43M
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8001
1.43M
    SET_NODE(opline->result, &var_node);
8002
1.43M
    opline->op1.num = i + 1;
8003
8004
1.43M
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8005
1.43M
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8006
1.43M
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8007
1.43M
    if (opcode == ZEND_RECV) {
8008
1.42M
      opline->op2.num = type_ast ?
8009
1.00M
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8010
1.42M
    }
8011
8012
1.43M
    if (is_promoted) {
8013
1.06k
      const zend_op_array *active_op_array = CG(active_op_array);
8014
1.06k
      zend_class_entry *scope = active_op_array->scope;
8015
8016
1.06k
      bool is_ctor =
8017
1.06k
        scope && zend_is_constructor(active_op_array->function_name);
8018
1.06k
      if (!is_ctor) {
8019
40
        zend_error_noreturn(E_COMPILE_ERROR,
8020
40
          "Cannot declare promoted property outside a constructor");
8021
40
      }
8022
1.02k
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8023
1.01k
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8024
7
        zend_error_noreturn(E_COMPILE_ERROR,
8025
7
          "Cannot declare promoted property in an abstract constructor");
8026
7
      }
8027
1.01k
      if (is_variadic) {
8028
7
        zend_error_noreturn(E_COMPILE_ERROR,
8029
7
          "Cannot declare variadic promoted property");
8030
7
      }
8031
1.01k
      if (zend_hash_exists(&scope->properties_info, name)) {
8032
7
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8033
7
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8034
7
      }
8035
1.00k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8036
7
        zend_string *str = zend_type_to_string(arg_info->type);
8037
7
        zend_error_noreturn(E_COMPILE_ERROR,
8038
7
          "Property %s::$%s cannot have type %s",
8039
7
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8040
7
      }
8041
8042
998
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8043
25
        property_flags |= ZEND_ACC_READONLY;
8044
25
      }
8045
8046
      /* Recompile the type, as it has different memory management requirements. */
8047
998
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8048
998
      if (type_ast) {
8049
776
        type = zend_compile_typename(type_ast);
8050
776
      }
8051
8052
      /* Don't give the property an explicit default value. For typed properties this means
8053
       * uninitialized, for untyped properties it means an implicit null default value.
8054
       * Properties with hooks get an implicit default value of undefined until inheritance,
8055
       * where it is changed to null only once we know it is not virtual. If we were to set it
8056
       * here, we couldn't verify that a true virtual property must not have an explicit
8057
       * default value. */
8058
998
      zval default_value;
8059
998
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8060
810
        ZVAL_UNDEF(&default_value);
8061
810
      } else {
8062
188
        if (property_flags & ZEND_ACC_READONLY) {
8063
14
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8064
14
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8065
14
        }
8066
8067
174
        ZVAL_NULL(&default_value);
8068
174
      }
8069
8070
984
      zend_string *doc_comment =
8071
984
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8072
984
      zend_property_info *prop = zend_declare_typed_property(
8073
984
        scope, name, &default_value,
8074
984
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8075
984
        doc_comment, type);
8076
984
      if (hooks_ast) {
8077
89
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8078
89
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8079
89
      }
8080
984
      if (attributes_ast) {
8081
68
        zend_compile_attributes(
8082
68
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8083
8084
68
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8085
68
        if (override_attribute) {
8086
9
          prop->flags |= ZEND_ACC_OVERRIDE;
8087
9
        }
8088
68
      }
8089
984
    }
8090
1.43M
  }
8091
8092
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8093
1.36M
  op_array->num_args = list->children;
8094
1.36M
  op_array->arg_info = arg_infos;
8095
8096
  /* Don't count the variadic argument */
8097
1.36M
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8098
1.56k
    op_array->num_args--;
8099
1.56k
  }
8100
1.36M
  zend_set_function_arg_flags((zend_function*)op_array);
8101
8102
2.80M
  for (i = 0; i < list->children; i++) {
8103
1.43M
    zend_ast *param_ast = list->child[i];
8104
1.43M
    zend_ast *hooks_ast = param_ast->child[5];
8105
1.43M
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8106
1.43M
    uint32_t flags = param_ast->attr & promotion_flags;
8107
1.43M
    bool is_promoted = flags || hooks_ast;
8108
1.43M
    if (!is_promoted) {
8109
1.43M
      continue;
8110
1.43M
    }
8111
8112
961
    CG(zend_lineno) = param_ast->lineno;
8113
8114
    /* Emit $this->prop = $prop for promoted properties. */
8115
961
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8116
961
    znode name_node, value_node;
8117
961
    name_node.op_type = IS_CONST;
8118
961
    ZVAL_STR_COPY(&name_node.u.constant, name);
8119
961
    value_node.op_type = IS_CV;
8120
961
    value_node.u.op.var = lookup_cv(name);
8121
8122
961
    zend_op *opline = zend_emit_op(NULL,
8123
961
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8124
961
    opline->extended_value = zend_alloc_cache_slots(3);
8125
961
    zend_emit_op_data(&value_node);
8126
961
  }
8127
1.36M
}
8128
/* }}} */
8129
8130
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8131
1.75k
{
8132
1.75k
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8133
1.75k
  uint32_t i;
8134
8135
1.75k
  if (!list->children) {
8136
0
    return;
8137
0
  }
8138
8139
1.75k
  if (!op_array->static_variables) {
8140
1.75k
    op_array->static_variables = zend_new_array(8);
8141
1.75k
  }
8142
8143
6.39k
  for (i = 0; i < list->children; ++i) {
8144
4.67k
    zend_ast *var_name_ast = list->child[i];
8145
4.67k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8146
4.67k
    uint32_t mode = var_name_ast->attr;
8147
4.67k
    zend_op *opline;
8148
4.67k
    zval *value;
8149
8150
4.67k
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8151
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8152
7
    }
8153
8154
4.66k
    if (zend_is_auto_global(var_name)) {
8155
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8156
7
    }
8157
8158
4.65k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8159
4.65k
    if (!value) {
8160
16
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8161
16
        "Cannot use variable $%S twice", var_name);
8162
16
    }
8163
8164
4.64k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8165
8166
4.64k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8167
4.64k
    opline->op2_type = IS_CV;
8168
4.64k
    opline->op2.var = lookup_cv(var_name);
8169
4.64k
    opline->extended_value =
8170
4.64k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8171
4.64k
  }
8172
1.75k
}
8173
/* }}} */
8174
8175
typedef struct {
8176
  HashTable uses;
8177
  bool varvars_used;
8178
} closure_info;
8179
8180
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8181
8182
1.48M
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8183
1.48M
  if (!ast) {
8184
10.6k
    return;
8185
10.6k
  }
8186
8187
1.47M
  if (ast->kind == ZEND_AST_VAR) {
8188
190k
    zend_ast *name_ast = ast->child[0];
8189
190k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8190
187k
      zend_string *name = zend_ast_get_str(name_ast);
8191
187k
      if (zend_is_auto_global(name)) {
8192
        /* These is no need to explicitly import auto-globals. */
8193
2.97k
        return;
8194
2.97k
      }
8195
8196
185k
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8197
        /* $this does not need to be explicitly imported. */
8198
6.79k
        return;
8199
6.79k
      }
8200
8201
178k
      zend_hash_add_empty_element(&info->uses, name);
8202
178k
    } else {
8203
2.60k
      info->varvars_used = true;
8204
2.60k
      find_implicit_binds_recursively(info, name_ast);
8205
2.60k
    }
8206
1.28M
  } else if (zend_ast_is_list(ast)) {
8207
46.5k
    const zend_ast_list *list = zend_ast_get_list(ast);
8208
46.5k
    uint32_t i;
8209
360k
    for (i = 0; i < list->children; i++) {
8210
313k
      find_implicit_binds_recursively(info, list->child[i]);
8211
313k
    }
8212
1.23M
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8213
    /* For normal closures add the use() list. */
8214
419
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8215
419
    zend_ast *uses_ast = closure_ast->child[1];
8216
419
    if (uses_ast) {
8217
336
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8218
336
      uint32_t i;
8219
2.29k
      for (i = 0; i < uses_list->children; i++) {
8220
1.95k
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8221
1.95k
      }
8222
336
    }
8223
1.23M
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8224
    /* For arrow functions recursively check the expression. */
8225
436k
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8226
436k
    closure_info inner_info;
8227
436k
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8228
436k
    if (inner_info.varvars_used) {
8229
5.94k
      info->varvars_used = true;
8230
5.94k
    }
8231
436k
    if (zend_hash_num_elements(&inner_info.uses)) {
8232
398k
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8233
398k
    }
8234
436k
    zend_hash_destroy(&inner_info.uses);
8235
798k
  } else if (!zend_ast_is_special(ast)) {
8236
562k
    uint32_t i, children = zend_ast_get_num_children(ast);
8237
1.26M
    for (i = 0; i < children; i++) {
8238
703k
      find_implicit_binds_recursively(info, ast->child[i]);
8239
703k
    }
8240
562k
  }
8241
1.47M
}
8242
8243
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8244
463k
{
8245
463k
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8246
463k
  uint32_t i;
8247
8248
463k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8249
463k
  info->varvars_used = false;
8250
8251
463k
  find_implicit_binds_recursively(info, stmt_ast);
8252
8253
  /* Remove variables that are parameters */
8254
545k
  for (i = 0; i < param_list->children; i++) {
8255
81.3k
    const zend_ast *param_ast = param_list->child[i];
8256
81.3k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8257
81.3k
  }
8258
463k
}
8259
8260
static void compile_implicit_lexical_binds(
8261
    const closure_info *info, znode *closure, zend_op_array *op_array)
8262
27.3k
{
8263
27.3k
  zend_string *var_name;
8264
27.3k
  zend_op *opline;
8265
8266
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8267
27.3k
  if (zend_hash_num_elements(&info->uses) == 0) {
8268
17.3k
    return;
8269
17.3k
  }
8270
8271
9.96k
  if (!op_array->static_variables) {
8272
9.96k
    op_array->static_variables = zend_new_array(8);
8273
9.96k
  }
8274
8275
164k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8276
164k
    zval *value = zend_hash_add(
8277
164k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8278
164k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8279
8280
164k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8281
164k
    opline->op2_type = IS_CV;
8282
164k
    opline->op2.var = lookup_cv(var_name);
8283
164k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8284
164k
  ZEND_HASH_FOREACH_END();
8285
9.96k
}
8286
8287
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8288
1.72k
{
8289
1.72k
  const zend_op_array *op_array = CG(active_op_array);
8290
1.72k
  const zend_ast_list *list = zend_ast_get_list(ast);
8291
1.72k
  uint32_t i;
8292
8293
6.29k
  for (i = 0; i < list->children; ++i) {
8294
4.58k
    uint32_t mode = ZEND_BIND_EXPLICIT;
8295
4.58k
    zend_ast *var_ast = list->child[i];
8296
4.58k
    zend_string *var_name = zend_ast_get_str(var_ast);
8297
4.58k
    zval zv;
8298
4.58k
    ZVAL_NULL(&zv);
8299
8300
4.58k
    {
8301
4.58k
      int i;
8302
14.1k
      for (i = 0; i < op_array->last_var; i++) {
8303
9.57k
        if (zend_string_equals(op_array->vars[i], var_name)) {
8304
8
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8305
8
            "Cannot use lexical variable $%S as a parameter name", var_name);
8306
8
        }
8307
9.57k
      }
8308
4.58k
    }
8309
8310
4.57k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8311
8312
4.57k
    if (var_ast->attr) {
8313
1.23k
      mode |= ZEND_BIND_REF;
8314
1.23k
    }
8315
8316
4.57k
    zend_compile_static_var_common(var_name, &zv, mode);
8317
4.57k
  }
8318
1.72k
}
8319
/* }}} */
8320
8321
static void zend_compile_implicit_closure_uses(const closure_info *info)
8322
27.3k
{
8323
27.3k
  zend_string *var_name;
8324
199k
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8325
199k
    zval zv;
8326
199k
    ZVAL_NULL(&zv);
8327
199k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8328
199k
  ZEND_HASH_FOREACH_END();
8329
27.3k
}
8330
8331
1.48k
static void add_stringable_interface(zend_class_entry *ce) {
8332
2.76k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8333
1.29k
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8334
      /* Interface already explicitly implemented */
8335
23
      return;
8336
23
    }
8337
1.29k
  }
8338
8339
1.46k
  ce->num_interfaces++;
8340
1.46k
  ce->interface_names =
8341
1.46k
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8342
  // TODO: Add known interned strings instead?
8343
1.46k
  ce->interface_names[ce->num_interfaces - 1].name =
8344
1.46k
    ZSTR_INIT_LITERAL("Stringable", 0);
8345
1.46k
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8346
1.46k
    ZSTR_INIT_LITERAL("stringable", 0);
8347
1.46k
}
8348
8349
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8350
39.8k
{
8351
39.8k
  zend_class_entry *ce = CG(active_class_entry);
8352
39.8k
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8353
39.8k
  uint32_t fn_flags = op_array->fn_flags;
8354
8355
39.8k
  zend_string *lcname;
8356
8357
39.8k
  if (fn_flags & ZEND_ACC_READONLY) {
8358
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8359
0
  }
8360
8361
39.8k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8362
82
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8363
82
  }
8364
8365
39.8k
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8366
529
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8367
    // Don't say that the class should be declared abstract if it is
8368
    // anonymous or an enum and can't be abstract
8369
38
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8370
7
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8371
7
        ZSTR_VAL(name));
8372
31
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8373
19
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8374
19
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8375
19
    } else {
8376
12
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8377
12
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8378
12
    }
8379
38
  }
8380
8381
39.8k
  if (in_interface) {
8382
811
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8383
1
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8384
1
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8385
1
    }
8386
810
    if (fn_flags & ZEND_ACC_FINAL) {
8387
7
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8388
7
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8389
7
    }
8390
803
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8391
803
  }
8392
8393
39.8k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8394
1.29k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8395
7
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8396
7
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8397
7
    }
8398
8399
1.28k
    if (has_body) {
8400
8
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8401
8
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8402
8
    }
8403
8404
1.27k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8405
38.5k
  } else if (!has_body) {
8406
9
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8407
9
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8408
9
  }
8409
8410
39.7k
  op_array->scope = ce;
8411
39.7k
  op_array->function_name = zend_string_copy(name);
8412
8413
39.7k
  lcname = zend_string_tolower(name);
8414
39.7k
  lcname = zend_new_interned_string(lcname);
8415
8416
39.7k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8417
31
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8418
31
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8419
31
  }
8420
8421
39.7k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8422
39.7k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8423
1.50k
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8424
1.48k
    add_stringable_interface(ce);
8425
1.48k
  }
8426
8427
39.7k
  return lcname;
8428
39.7k
}
8429
/* }}} */
8430
8431
1.37M
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8432
1.37M
  zend_op_array *op_array = CG(active_op_array);
8433
1.37M
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8434
1.37M
  op_array->dynamic_func_defs = erealloc(
8435
1.37M
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8436
1.37M
  op_array->dynamic_func_defs[def_offset] = def;
8437
1.37M
  return def_offset;
8438
1.37M
}
8439
8440
enum func_decl_level {
8441
  FUNC_DECL_LEVEL_TOPLEVEL,
8442
  FUNC_DECL_LEVEL_NESTED,
8443
  FUNC_DECL_LEVEL_CONSTEXPR,
8444
};
8445
8446
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8447
1.38M
{
8448
1.38M
  zend_string *unqualified_name, *name, *lcname;
8449
1.38M
  zend_op *opline;
8450
8451
1.38M
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8452
1.36M
    zend_string *filename = op_array->filename;
8453
1.36M
    uint32_t start_lineno = decl->start_lineno;
8454
8455
1.36M
    zend_string *class = zend_empty_string;
8456
1.36M
    zend_string *separator = zend_empty_string;
8457
1.36M
    zend_string *function = filename;
8458
1.36M
    const char *parens = "";
8459
8460
1.36M
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8461
1.35M
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8462
        /* If the parent function is a closure, don't redundantly
8463
         * add the classname and parentheses.
8464
         */
8465
1.34M
        function = CG(active_op_array)->function_name;
8466
1.34M
      } else {
8467
4.15k
        function = CG(active_op_array)->function_name;
8468
4.15k
        parens = "()";
8469
8470
4.15k
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8471
3.66k
          class = CG(active_class_entry)->name;
8472
3.66k
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8473
3.66k
        }
8474
4.15k
      }
8475
1.35M
    }
8476
8477
1.36M
    unqualified_name = zend_strpprintf_unchecked(
8478
1.36M
      0,
8479
1.36M
      "{closure:%S%S%S%s:%" PRIu32 "}",
8480
1.36M
      class,
8481
1.36M
      separator,
8482
1.36M
      function,
8483
1.36M
      parens,
8484
1.36M
      start_lineno
8485
1.36M
    );
8486
8487
1.36M
    op_array->function_name = name = unqualified_name;
8488
1.36M
  } else {
8489
24.3k
    unqualified_name = decl->name;
8490
24.3k
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8491
24.3k
  }
8492
8493
1.38M
  lcname = zend_string_tolower(name);
8494
8495
1.38M
  if (FC(imports_function)) {
8496
43
    const zend_string *import_name =
8497
43
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8498
43
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8499
14
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8500
14
        ZSTR_VAL(name));
8501
14
    }
8502
43
  }
8503
8504
1.38M
  if (zend_string_equals_literal(lcname, "__autoload")) {
8505
1
    zend_error_noreturn(E_COMPILE_ERROR,
8506
1
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8507
1
  }
8508
8509
1.38M
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8510
7
    zend_error(E_COMPILE_ERROR,
8511
7
      "Defining a custom assert() function is not allowed, "
8512
7
      "as the function has special semantics");
8513
7
  }
8514
8515
1.38M
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8516
1.38M
  switch (level) {
8517
1.37M
    case FUNC_DECL_LEVEL_NESTED: {
8518
1.37M
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8519
1.37M
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8520
1.36M
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8521
1.36M
        opline->op2.num = func_ref;
8522
1.36M
      } else {
8523
8.49k
        opline = get_next_op();
8524
8.49k
        opline->opcode = ZEND_DECLARE_FUNCTION;
8525
8.49k
        opline->op1_type = IS_CONST;
8526
8.49k
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8527
8.49k
        opline->op2.num = func_ref;
8528
8.49k
      }
8529
1.37M
      break;
8530
0
    }
8531
90
    case FUNC_DECL_LEVEL_CONSTEXPR:
8532
15.8k
    case FUNC_DECL_LEVEL_TOPLEVEL:
8533
      /* Nothing to do. */
8534
15.8k
      break;
8535
1.38M
  }
8536
1.38M
  return lcname;
8537
1.38M
}
8538
/* }}} */
8539
8540
static zend_op_array *zend_compile_func_decl_ex(
8541
  znode *result, zend_ast *ast, enum func_decl_level level,
8542
  zend_string *property_info_name,
8543
  zend_property_hook_kind hook_kind
8544
1.43M
) {
8545
1.43M
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8546
1.43M
  zend_ast *params_ast = decl->child[0];
8547
1.43M
  zend_ast *uses_ast = decl->child[1];
8548
1.43M
  zend_ast *stmt_ast = decl->child[2];
8549
1.43M
  zend_ast *return_type_ast = decl->child[3];
8550
1.43M
  bool is_method = decl->kind == ZEND_AST_METHOD;
8551
1.43M
  zend_string *lcname = NULL;
8552
1.43M
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8553
8554
1.43M
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8555
1.43M
  zend_op_array *orig_op_array = CG(active_op_array);
8556
1.43M
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8557
1.43M
  zend_oparray_context orig_oparray_context;
8558
1.43M
  closure_info info;
8559
8560
1.43M
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8561
8562
1.43M
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8563
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8564
0
  }
8565
8566
1.43M
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8567
1.43M
  op_array->fn_flags |= decl->flags;
8568
1.43M
  op_array->line_start = decl->start_lineno;
8569
1.43M
  op_array->line_end = decl->end_lineno;
8570
1.43M
  if (decl->doc_comment) {
8571
804
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8572
804
  }
8573
8574
1.43M
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8575
1.36M
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8576
1.36M
  }
8577
8578
1.43M
  if (is_hook) {
8579
4.67k
    zend_class_entry *ce = CG(active_class_entry);
8580
4.67k
    op_array->scope = ce;
8581
4.67k
    op_array->function_name = zend_string_copy(decl->name);
8582
1.42M
  } else if (is_method) {
8583
39.8k
    bool has_body = stmt_ast != NULL;
8584
39.8k
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8585
1.38M
  } else {
8586
1.38M
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8587
1.38M
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8588
27.3k
      find_implicit_binds(&info, params_ast, stmt_ast);
8589
27.3k
      compile_implicit_lexical_binds(&info, result, op_array);
8590
1.36M
    } else if (uses_ast) {
8591
1.75k
      zend_compile_closure_binding(result, op_array, uses_ast);
8592
1.75k
    }
8593
1.38M
  }
8594
8595
1.43M
  CG(active_op_array) = op_array;
8596
8597
1.43M
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8598
1.43M
  CG(context).active_property_info_name = property_info_name;
8599
1.43M
  CG(context).active_property_hook_kind = hook_kind;
8600
8601
1.43M
  if (decl->child[4]) {
8602
1.30M
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8603
8604
1.30M
    if (is_method || is_hook) {
8605
1.27k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8606
1.27k
    }
8607
8608
1.30M
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8609
8610
1.30M
    const zend_attribute *override_attribute = zend_get_attribute_str(
8611
1.30M
      op_array->attributes,
8612
1.30M
      "override",
8613
1.30M
      sizeof("override")-1
8614
1.30M
    );
8615
8616
1.30M
    if (override_attribute) {
8617
677
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8618
677
    }
8619
8620
1.30M
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8621
1.30M
      op_array->attributes,
8622
1.30M
      "deprecated",
8623
1.30M
      sizeof("deprecated")-1
8624
1.30M
    );
8625
8626
1.30M
    if (deprecated_attribute) {
8627
260
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8628
260
    }
8629
8630
    // ZEND_ACC_NODISCARD is added via an attribute validator
8631
1.30M
  }
8632
8633
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8634
   * defined inside a class method. This is necessary for correct handling of magic constants.
8635
   * For example __CLASS__ should always be "" inside a free standing function. */
8636
1.43M
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8637
24.2k
    CG(active_class_entry) = NULL;
8638
24.2k
  }
8639
8640
1.43M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8641
15.7k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8642
15.7k
  }
8643
8644
1.43M
  {
8645
    /* Push a separator to the loop variable stack */
8646
1.43M
    zend_loop_var dummy_var;
8647
1.43M
    dummy_var.opcode = ZEND_RETURN;
8648
8649
1.43M
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8650
1.43M
  }
8651
8652
1.43M
  zend_compile_params(params_ast, return_type_ast,
8653
1.43M
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8654
1.43M
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8655
15.3k
    zend_mark_function_as_generator();
8656
15.3k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8657
15.3k
  }
8658
1.43M
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8659
27.3k
    zend_compile_implicit_closure_uses(&info);
8660
27.3k
    zend_hash_destroy(&info.uses);
8661
1.40M
  } else if (uses_ast) {
8662
1.72k
    zend_compile_closure_uses(uses_ast);
8663
1.72k
  }
8664
8665
1.43M
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8666
13.4k
    bool needs_return = true;
8667
13.4k
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8668
1.18k
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8669
1.18k
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8670
1.18k
    }
8671
13.4k
    if (needs_return) {
8672
13.0k
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8673
13.0k
      decl->child[2] = stmt_ast;
8674
13.0k
    }
8675
13.4k
  }
8676
8677
1.43M
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8678
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8679
     * if the method is not a hook; if it is a hook, then the validator
8680
     * will have returned an error message, even if the error message was
8681
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8682
     * flag should not have been added. */
8683
605
    ZEND_ASSERT(!is_hook);
8684
8685
605
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8686
550
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8687
550
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8688
7
        zend_error_noreturn(E_COMPILE_ERROR,
8689
7
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8690
7
          CG(active_class_entry) != NULL ? "method" : "function");
8691
7
      }
8692
8693
543
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8694
7
        zend_error_noreturn(E_COMPILE_ERROR,
8695
7
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8696
7
          CG(active_class_entry) != NULL ? "method" : "function");
8697
7
      }
8698
543
    }
8699
605
  }
8700
8701
1.43M
  zend_compile_stmt(stmt_ast);
8702
8703
1.43M
  if (is_method) {
8704
39.4k
    CG(zend_lineno) = decl->start_lineno;
8705
39.4k
    zend_check_magic_method_implementation(
8706
39.4k
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8707
1.39M
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8708
    /* Only register the function after a successful compile */
8709
15.1k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8710
115
      CG(zend_lineno) = decl->start_lineno;
8711
115
      do_bind_function_error(lcname, op_array, true);
8712
115
    }
8713
15.1k
  }
8714
8715
  /* put the implicit return on the really last line */
8716
1.43M
  CG(zend_lineno) = decl->end_lineno;
8717
8718
1.43M
  zend_do_extended_stmt(NULL);
8719
1.43M
  zend_emit_final_return(false);
8720
8721
1.43M
  pass_two(CG(active_op_array));
8722
1.43M
  zend_oparray_context_end(&orig_oparray_context);
8723
8724
  /* Pop the loop variable stack separator */
8725
1.43M
  zend_stack_del_top(&CG(loop_var_stack));
8726
8727
1.43M
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8728
15.0k
    zend_observer_function_declared_notify(op_array, lcname);
8729
15.0k
  }
8730
8731
1.43M
  if (lcname != NULL) {
8732
1.42M
    zend_string_release_ex(lcname, 0);
8733
1.42M
  }
8734
8735
1.43M
  CG(active_op_array) = orig_op_array;
8736
1.43M
  CG(active_class_entry) = orig_class_entry;
8737
8738
1.43M
  return op_array;
8739
1.43M
}
8740
8741
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8742
1.42M
{
8743
1.42M
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8744
1.42M
}
8745
8746
5.65k
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8747
5.65k
  if (zend_string_equals_literal_ci(name, "get")) {
8748
3.67k
    return ZEND_PROPERTY_HOOK_GET;
8749
3.67k
  } else if (zend_string_equals_literal_ci(name, "set")) {
8750
1.81k
    return ZEND_PROPERTY_HOOK_SET;
8751
1.81k
  } else {
8752
163
    return (zend_property_hook_kind)-1;
8753
163
  }
8754
5.65k
}
8755
8756
static void zend_compile_property_hooks(
8757
    zend_property_info *prop_info, zend_string *prop_name,
8758
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8759
4.06k
{
8760
4.06k
  zend_class_entry *ce = CG(active_class_entry);
8761
8762
4.06k
  if (prop_info->flags & ZEND_ACC_READONLY) {
8763
19
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8764
19
  }
8765
8766
4.04k
  if (hooks->children == 0) {
8767
17
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8768
17
  }
8769
8770
8.66k
  for (uint32_t i = 0; i < hooks->children; i++) {
8771
4.95k
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8772
4.95k
    zend_string *name = hook->name;
8773
4.95k
    zend_ast *stmt_ast = hook->child[2];
8774
4.95k
    zend_ast **return_type_ast_ptr = NULL;
8775
4.95k
    zend_ast **value_type_ast_ptr = NULL;
8776
4.95k
    CG(zend_lineno) = hook->start_lineno;
8777
8778
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8779
     * hooks from protected properties to public ones. */
8780
4.95k
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8781
4.95k
    hook->flags |= hook_visibility;
8782
8783
4.95k
    if (prop_info->flags & ZEND_ACC_STATIC) {
8784
27
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8785
27
    }
8786
4.92k
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8787
7
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8788
7
    }
8789
4.91k
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8790
4.65k
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8791
705
      hook->flags |= ZEND_ACC_ABSTRACT;
8792
8793
705
      if (stmt_ast) {
8794
11
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8795
11
      }
8796
694
      if (hook->flags & ZEND_ACC_PRIVATE) {
8797
7
        zend_error_noreturn(E_COMPILE_ERROR,
8798
7
          "Property hook cannot be both abstract and private");
8799
7
      }
8800
687
      if (hook->flags & ZEND_ACC_FINAL) {
8801
9
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8802
9
      }
8803
4.21k
    } else if (!stmt_ast) {
8804
18
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8805
18
    }
8806
8807
4.87k
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8808
4.87k
    if (hook_kind == (zend_property_hook_kind)-1) {
8809
163
      zend_error_noreturn(E_COMPILE_ERROR,
8810
163
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8811
163
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8812
163
    }
8813
8814
4.70k
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8815
1.78k
      stmt_ast = stmt_ast->child[0];
8816
1.78k
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8817
1.46k
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8818
1.46k
      } else {
8819
317
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8820
317
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8821
317
          zend_ast_create(ZEND_AST_PROP,
8822
317
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8823
317
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8824
317
          stmt_ast);
8825
317
      }
8826
1.78k
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8827
1.78k
      hook->child[2] = stmt_ast;
8828
1.78k
    }
8829
8830
4.70k
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8831
2.96k
      if (hook->child[0]) {
8832
7
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8833
7
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8834
7
      }
8835
8836
2.96k
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8837
8838
2.96k
      return_type_ast_ptr = &hook->child[3];
8839
2.96k
      *return_type_ast_ptr = prop_type_ast;
8840
2.96k
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8841
1.74k
      if (hook->child[0]) {
8842
149
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8843
149
        if (param_list->children != 1) {
8844
1
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8845
1
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8846
1
        }
8847
148
        const zend_ast *value_param_ast = param_list->child[0];
8848
148
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8849
7
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8850
7
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8851
7
        }
8852
141
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8853
7
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8854
7
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8855
7
        }
8856
134
        if (value_param_ast->child[2]) {
8857
7
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8858
7
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8859
7
        }
8860
127
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8861
7
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8862
7
        }
8863
1.59k
      } else {
8864
1.59k
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8865
1.59k
        zend_ast *param = zend_ast_create(
8866
1.59k
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8867
1.59k
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8868
1.59k
          /* hooks */ NULL);
8869
1.59k
        value_type_ast_ptr = &param->child[0];
8870
1.59k
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8871
1.59k
      }
8872
1.71k
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8873
1.71k
      return_type->attr = ZEND_NAME_NOT_FQ;
8874
1.71k
      hook->child[3] = return_type;
8875
1.71k
    } else {
8876
0
      ZEND_UNREACHABLE();
8877
0
    }
8878
8879
4.67k
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8880
8881
4.67k
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8882
4.67k
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8883
8884
4.67k
    func->common.prop_info = prop_info;
8885
8886
4.67k
    if (!prop_info->hooks) {
8887
3.68k
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8888
3.68k
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8889
3.68k
    }
8890
8891
4.67k
    if (prop_info->hooks[hook_kind]) {
8892
29
      zend_error_noreturn(E_COMPILE_ERROR,
8893
29
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8894
29
    }
8895
4.64k
    prop_info->hooks[hook_kind] = func;
8896
8897
4.64k
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8898
1.66k
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8899
1.62k
        case INHERITANCE_SUCCESS:
8900
1.62k
          break;
8901
28
        case INHERITANCE_UNRESOLVED:
8902
28
          ce->num_hooked_prop_variance_checks++;
8903
28
          break;
8904
8
        case INHERITANCE_ERROR:
8905
8
          zend_hooked_property_variance_error(prop_info);
8906
0
        case INHERITANCE_WARNING:
8907
0
          ZEND_UNREACHABLE();
8908
1.66k
      }
8909
1.66k
    }
8910
8911
4.63k
    zend_string_release(name);
8912
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8913
4.63k
    if (return_type_ast_ptr) {
8914
2.90k
      *return_type_ast_ptr = NULL;
8915
2.90k
    }
8916
4.63k
    if (value_type_ast_ptr) {
8917
1.54k
      *value_type_ast_ptr = NULL;
8918
1.54k
    }
8919
4.63k
  }
8920
8921
3.71k
  ce->num_hooked_props++;
8922
8923
  /* See zend_link_hooked_object_iter(). */
8924
3.71k
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8925
3.71k
  if (!ce->get_iterator) {
8926
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8927
2.77k
    ce->get_iterator = zend_hooked_object_get_iterator;
8928
2.77k
  }
8929
3.71k
#endif
8930
8931
3.71k
  if (!prop_info->ce->parent_name) {
8932
2.22k
    zend_verify_hooked_property(ce, prop_info, prop_name);
8933
2.22k
  }
8934
3.71k
}
8935
8936
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8937
45.3k
{
8938
45.3k
  const zend_ast_list *list = zend_ast_get_list(ast);
8939
45.3k
  zend_class_entry *ce = CG(active_class_entry);
8940
45.3k
  uint32_t i, children = list->children;
8941
8942
45.3k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8943
17
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8944
17
  }
8945
8946
45.3k
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8947
7
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8948
7
  }
8949
8950
45.3k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8951
290
    if (flags & ZEND_ACC_FINAL) {
8952
7
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8953
7
    }
8954
283
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8955
7
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8956
7
    }
8957
276
    if (flags & ZEND_ACC_ABSTRACT) {
8958
7
      zend_error_noreturn(E_COMPILE_ERROR,
8959
7
        "Property in interface cannot be explicitly abstract. "
8960
7
        "All interface members are implicitly abstract");
8961
7
    }
8962
269
    flags |= ZEND_ACC_ABSTRACT;
8963
269
  }
8964
8965
90.6k
  for (i = 0; i < children; ++i) {
8966
45.5k
    zend_property_info *info;
8967
45.5k
    zend_ast *prop_ast = list->child[i];
8968
45.5k
    zend_ast *name_ast = prop_ast->child[0];
8969
45.5k
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8970
45.5k
    zend_ast *doc_comment_ast = prop_ast->child[2];
8971
45.5k
    zend_ast *hooks_ast = prop_ast->child[3];
8972
45.5k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8973
45.5k
    zend_string *doc_comment = NULL;
8974
45.5k
    zval value_zv;
8975
45.5k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8976
45.5k
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8977
8978
45.5k
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8979
45.5k
    CG(context).active_property_info_name = name;
8980
8981
45.5k
    if (!hooks_ast) {
8982
41.5k
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8983
2
        zend_error_noreturn(E_COMPILE_ERROR,
8984
2
          "Interfaces may only include hooked properties");
8985
2
      }
8986
41.5k
      if (flags & ZEND_ACC_ABSTRACT) {
8987
7
        zend_error_noreturn(E_COMPILE_ERROR,
8988
7
          "Only hooked properties may be declared abstract");
8989
7
      }
8990
41.5k
    }
8991
45.5k
    if ((flags & ZEND_ACC_ABSTRACT)) {
8992
728
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8993
728
    }
8994
8995
45.5k
    if (type_ast) {
8996
32.2k
      type = zend_compile_typename(type_ast);
8997
8998
32.2k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8999
7
        zend_string *str = zend_type_to_string(type);
9000
7
        zend_error_noreturn(E_COMPILE_ERROR,
9001
7
          "Property %s::$%s cannot have type %s",
9002
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9003
7
      }
9004
32.2k
    }
9005
9006
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9007
45.5k
    if (doc_comment_ast) {
9008
384
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9009
384
    }
9010
9011
45.5k
    if (zend_hash_exists(&ce->properties_info, name)) {
9012
58
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9013
58
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9014
58
    }
9015
9016
45.4k
    if (*value_ast_ptr) {
9017
9.11k
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9018
9019
9.11k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9020
1.47k
          && !zend_is_valid_default_value(type, &value_zv)) {
9021
99
        zend_string *str = zend_type_to_string(type);
9022
99
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9023
25
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9024
25
          zend_string *nullable_str = zend_type_to_string(type);
9025
9026
25
          zend_error_noreturn(E_COMPILE_ERROR,
9027
25
            "Default value for property of type %s may not be null. "
9028
25
            "Use the nullable type %s to allow null default value",
9029
25
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9030
74
        } else {
9031
74
          zend_error_noreturn(E_COMPILE_ERROR,
9032
74
            "Cannot use %s as default value for property %s::$%s of type %s",
9033
74
            zend_zval_value_name(&value_zv),
9034
74
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9035
74
        }
9036
99
      }
9037
36.3k
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9038
3.79k
      ZVAL_NULL(&value_zv);
9039
32.5k
    } else {
9040
32.5k
      ZVAL_UNDEF(&value_zv);
9041
32.5k
    }
9042
9043
45.3k
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9044
268
      flags |= ZEND_ACC_READONLY;
9045
268
    }
9046
9047
45.3k
    if (flags & ZEND_ACC_READONLY) {
9048
760
      if (!ZEND_TYPE_IS_SET(type)) {
9049
18
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9050
18
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9051
18
      }
9052
742
      if (!Z_ISUNDEF(value_zv)) {
9053
7
        zend_error_noreturn(E_COMPILE_ERROR,
9054
7
          "Readonly property %s::$%s cannot have default value",
9055
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9056
7
      }
9057
735
      if (flags & ZEND_ACC_STATIC) {
9058
13
        zend_error_noreturn(E_COMPILE_ERROR,
9059
13
          "Static property %s::$%s cannot be readonly",
9060
13
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9061
13
      }
9062
735
    }
9063
9064
45.3k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9065
9066
45.3k
    if (hooks_ast) {
9067
3.97k
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9068
3.97k
    }
9069
9070
45.3k
    if (attr_ast) {
9071
383
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9072
9073
383
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9074
383
      if (override_attribute) {
9075
91
        info->flags |= ZEND_ACC_OVERRIDE;
9076
91
      }
9077
383
    }
9078
9079
45.3k
    CG(context).active_property_info_name = old_active_property_info_name;
9080
45.3k
  }
9081
45.3k
}
9082
/* }}} */
9083
9084
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9085
45.3k
{
9086
45.3k
  zend_ast *type_ast = ast->child[0];
9087
45.3k
  zend_ast *prop_ast = ast->child[1];
9088
45.3k
  zend_ast *attr_ast = ast->child[2];
9089
9090
45.3k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9091
45.3k
}
9092
/* }}} */
9093
9094
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9095
61.7k
{
9096
61.7k
  if (attr & ZEND_ACC_STATIC) {
9097
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9098
61.7k
  } else if (attr & ZEND_ACC_ABSTRACT) {
9099
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9100
7
  }
9101
61.7k
}
9102
/* }}} */
9103
9104
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9105
14.5k
{
9106
14.5k
  const zend_ast_list *list = zend_ast_get_list(ast);
9107
14.5k
  zend_class_entry *ce = CG(active_class_entry);
9108
14.5k
  uint32_t i, children = list->children;
9109
9110
29.2k
  for (i = 0; i < children; ++i) {
9111
14.6k
    zend_class_constant *c;
9112
14.6k
    zend_ast *const_ast = list->child[i];
9113
14.6k
    zend_ast *name_ast = const_ast->child[0];
9114
14.6k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9115
14.6k
    zend_ast *doc_comment_ast = const_ast->child[2];
9116
14.6k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9117
14.6k
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9118
14.6k
    zval value_zv;
9119
14.6k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9120
9121
14.6k
    if (type_ast) {
9122
3.66k
      type = zend_compile_typename(type_ast);
9123
9124
3.66k
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9125
9126
3.66k
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9127
7
        zend_string *type_str = zend_type_to_string(type);
9128
9129
7
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9130
7
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9131
7
      }
9132
3.66k
    }
9133
9134
14.6k
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9135
7
      zend_error_noreturn(
9136
7
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9137
7
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9138
7
      );
9139
7
    }
9140
9141
14.6k
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9142
9143
14.6k
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9144
31
      zend_string *type_str = zend_type_to_string(type);
9145
9146
31
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9147
31
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9148
31
    }
9149
9150
14.6k
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9151
9152
14.6k
    if (attr_ast) {
9153
175
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9154
9155
175
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9156
9157
175
      if (deprecated) {
9158
73
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9159
        /* For deprecated constants, we need to flag the zval for recursion
9160
         * detection. Make sure the zval is separated out of shm. */
9161
73
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9162
73
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9163
73
      }
9164
175
    }
9165
14.6k
  }
9166
14.5k
}
9167
9168
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9169
14.5k
{
9170
14.5k
  zend_ast *const_ast = ast->child[0];
9171
14.5k
  zend_ast *attr_ast = ast->child[1];
9172
14.5k
  zend_ast *type_ast = ast->child[2];
9173
9174
14.5k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9175
14.5k
}
9176
/* }}} */
9177
9178
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9179
62.2k
{
9180
62.2k
  zend_ast *class_ast = ast->child[0];
9181
62.2k
  zend_ast *method_ast = ast->child[1];
9182
9183
62.2k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9184
9185
62.2k
  if (class_ast) {
9186
2.06k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9187
60.2k
  } else {
9188
60.2k
    method_ref->class_name = NULL;
9189
60.2k
  }
9190
62.2k
}
9191
/* }}} */
9192
9193
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9194
545
{
9195
545
  const zend_ast *method_ref_ast = ast->child[0];
9196
545
  zend_ast *insteadof_ast = ast->child[1];
9197
545
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9198
545
  uint32_t i;
9199
9200
545
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9201
545
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9202
545
  precedence->num_excludes = insteadof_list->children;
9203
9204
1.68k
  for (i = 0; i < insteadof_list->children; ++i) {
9205
1.14k
    zend_ast *name_ast = insteadof_list->child[i];
9206
1.14k
    precedence->exclude_class_names[i] =
9207
1.14k
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9208
1.14k
  }
9209
9210
545
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9211
545
}
9212
/* }}} */
9213
9214
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9215
61.7k
{
9216
61.7k
  const zend_ast *method_ref_ast = ast->child[0];
9217
61.7k
  zend_ast *alias_ast = ast->child[1];
9218
61.7k
  uint32_t modifiers = ast->attr;
9219
9220
61.7k
  zend_trait_alias *alias;
9221
9222
61.7k
  zend_check_trait_alias_modifiers(modifiers);
9223
9224
61.7k
  alias = emalloc(sizeof(zend_trait_alias));
9225
61.7k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9226
61.7k
  alias->modifiers = modifiers;
9227
9228
61.7k
  if (alias_ast) {
9229
61.2k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9230
61.2k
  } else {
9231
495
    alias->alias = NULL;
9232
495
  }
9233
9234
61.7k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9235
61.7k
}
9236
/* }}} */
9237
9238
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9239
22.4k
{
9240
22.4k
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9241
22.4k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9242
22.4k
  zend_class_entry *ce = CG(active_class_entry);
9243
22.4k
  uint32_t i;
9244
9245
22.4k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9246
9247
46.4k
  for (i = 0; i < traits->children; ++i) {
9248
23.9k
    zend_ast *trait_ast = traits->child[i];
9249
9250
23.9k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9251
7
      zend_string *name = zend_ast_get_str(trait_ast);
9252
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9253
7
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9254
7
    }
9255
9256
23.9k
    ce->trait_names[ce->num_traits].name =
9257
23.9k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9258
23.9k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9259
23.9k
    ce->num_traits++;
9260
23.9k
  }
9261
9262
22.4k
  if (!adaptations) {
9263
2.41k
    return;
9264
2.41k
  }
9265
9266
82.3k
  for (i = 0; i < adaptations->children; ++i) {
9267
62.3k
    const zend_ast *adaptation_ast = adaptations->child[i];
9268
62.3k
    switch (adaptation_ast->kind) {
9269
545
      case ZEND_AST_TRAIT_PRECEDENCE:
9270
545
        zend_compile_trait_precedence(adaptation_ast);
9271
545
        break;
9272
61.7k
      case ZEND_AST_TRAIT_ALIAS:
9273
61.7k
        zend_compile_trait_alias(adaptation_ast);
9274
61.7k
        break;
9275
62.3k
      EMPTY_SWITCH_DEFAULT_CASE()
9276
62.3k
    }
9277
62.3k
  }
9278
20.0k
}
9279
/* }}} */
9280
9281
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9282
3.98k
{
9283
3.98k
  const zend_ast_list *list = zend_ast_get_list(ast);
9284
3.98k
  zend_class_entry *ce = CG(active_class_entry);
9285
3.98k
  zend_class_name *interface_names;
9286
3.98k
  uint32_t i;
9287
9288
3.98k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9289
9290
9.70k
  for (i = 0; i < list->children; ++i) {
9291
5.72k
    zend_ast *class_ast = list->child[i];
9292
5.72k
    interface_names[i].name =
9293
5.72k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9294
5.72k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9295
5.72k
  }
9296
9297
3.98k
  ce->num_interfaces = list->children;
9298
3.98k
  ce->interface_names = interface_names;
9299
3.98k
}
9300
/* }}} */
9301
9302
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9303
1.94k
{
9304
1.94k
  zend_string *filename = CG(active_op_array)->filename;
9305
1.94k
  uint32_t start_lineno = decl->start_lineno;
9306
9307
  /* Use parent or first interface as prefix. */
9308
1.94k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9309
1.94k
  if (decl->child[0]) {
9310
205
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9311
1.74k
  } else if (decl->child[1]) {
9312
529
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9313
529
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9314
529
  }
9315
9316
1.94k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9317
1.94k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9318
1.94k
  zend_string_release(prefix);
9319
1.94k
  return zend_new_interned_string(result);
9320
1.94k
}
9321
9322
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9323
904
{
9324
904
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9325
904
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9326
904
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9327
904
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9328
64
    zend_string *type_string = zend_type_to_string(type);
9329
64
    zend_error_noreturn(E_COMPILE_ERROR,
9330
64
      "Enum backing type must be int or string, %s given",
9331
64
      ZSTR_VAL(type_string));
9332
64
  }
9333
840
  if (type_mask == MAY_BE_LONG) {
9334
548
    ce->enum_backing_type = IS_LONG;
9335
548
  } else {
9336
292
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9337
292
    ce->enum_backing_type = IS_STRING;
9338
291
  }
9339
840
  zend_type_release(type, 0);
9340
839
}
9341
9342
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9343
133k
{
9344
133k
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9345
133k
  zend_ast *extends_ast = decl->child[0];
9346
133k
  zend_ast *implements_ast = decl->child[1];
9347
133k
  zend_ast *stmt_ast = decl->child[2];
9348
133k
  zend_ast *enum_backing_type_ast = decl->child[4];
9349
133k
  zend_string *name, *lcname;
9350
133k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9351
133k
  zend_op *opline;
9352
9353
133k
  zend_class_entry *original_ce = CG(active_class_entry);
9354
9355
133k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9356
131k
    zend_string *unqualified_name = decl->name;
9357
9358
131k
    if (CG(active_class_entry)) {
9359
10
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9360
10
    }
9361
9362
131k
    const char *type = "a class name";
9363
131k
    if (decl->flags & ZEND_ACC_ENUM) {
9364
5.77k
      type = "an enum name";
9365
125k
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9366
7.69k
      type = "an interface name";
9367
118k
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9368
2.95k
      type = "a trait name";
9369
2.95k
    }
9370
131k
    zend_assert_valid_class_name(unqualified_name, type);
9371
131k
    name = zend_prefix_with_ns(unqualified_name);
9372
131k
    name = zend_new_interned_string(name);
9373
131k
    lcname = zend_string_tolower(name);
9374
9375
131k
    if (FC(imports)) {
9376
453
      zend_string *import_name =
9377
453
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9378
453
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9379
14
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9380
14
            "(previously declared as local import)", ZSTR_VAL(name));
9381
14
      }
9382
453
    }
9383
9384
131k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9385
131k
  } else {
9386
    /* Find an anon class name that is not in use yet. */
9387
1.94k
    name = NULL;
9388
1.94k
    lcname = NULL;
9389
1.94k
    do {
9390
1.94k
      zend_tmp_string_release(name);
9391
1.94k
      zend_tmp_string_release(lcname);
9392
1.94k
      name = zend_generate_anon_class_name(decl);
9393
1.94k
      lcname = zend_string_tolower(name);
9394
1.94k
    } while (zend_hash_exists(CG(class_table), lcname));
9395
1.94k
  }
9396
133k
  lcname = zend_new_interned_string(lcname);
9397
9398
133k
  ce->type = ZEND_USER_CLASS;
9399
133k
  ce->name = name;
9400
133k
  zend_initialize_class_data(ce, true);
9401
133k
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9402
131k
    zend_alloc_ce_cache(ce->name);
9403
131k
  }
9404
9405
133k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9406
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9407
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9408
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9409
0
  }
9410
9411
133k
  ce->ce_flags |= decl->flags;
9412
133k
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9413
133k
  ce->info.user.line_start = decl->start_lineno;
9414
133k
  ce->info.user.line_end = decl->end_lineno;
9415
9416
133k
  if (decl->doc_comment) {
9417
97
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9418
97
  }
9419
9420
133k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9421
    /* Serialization is not supported for anonymous classes */
9422
1.94k
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9423
1.94k
  }
9424
9425
133k
  if (extends_ast) {
9426
34.0k
    ce->parent_name =
9427
34.0k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9428
34.0k
  }
9429
9430
133k
  CG(active_class_entry) = ce;
9431
9432
133k
  if (decl->child[3]) {
9433
4.45k
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9434
4.45k
  }
9435
9436
133k
  if (implements_ast) {
9437
3.98k
    zend_compile_implements(implements_ast);
9438
3.98k
  }
9439
9440
133k
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9441
5.74k
    if (enum_backing_type_ast != NULL) {
9442
904
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9443
904
    }
9444
5.74k
    zend_enum_add_interfaces(ce);
9445
5.74k
    zend_enum_register_props(ce);
9446
5.74k
  }
9447
9448
133k
  zend_compile_stmt(stmt_ast);
9449
9450
  /* Reset lineno for final opcodes and errors */
9451
133k
  CG(zend_lineno) = ast->lineno;
9452
9453
133k
  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) {
9454
127
    zend_verify_abstract_class(ce);
9455
127
  }
9456
9457
133k
  CG(active_class_entry) = original_ce;
9458
9459
133k
  if (toplevel) {
9460
43.4k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9461
43.4k
  }
9462
9463
  /* We currently don't early-bind classes that implement interfaces or use traits */
9464
133k
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9465
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9466
   /* See zend_link_hooked_object_iter(). */
9467
   && !ce->num_hooked_props
9468
#endif
9469
99.6k
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9470
99.6k
    if (toplevel) {
9471
34.8k
      if (extends_ast) {
9472
10.3k
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9473
10.3k
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9474
9475
10.3k
        if (parent_ce
9476
9.37k
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9477
9.37k
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9478
5.05k
            zend_string_release(lcname);
9479
5.05k
            return;
9480
5.05k
          }
9481
9.37k
        }
9482
24.5k
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9483
20.2k
        zend_string_release(lcname);
9484
20.2k
        zend_build_properties_info_table(ce);
9485
20.2k
        zend_inheritance_check_override(ce);
9486
20.2k
        ce->ce_flags |= ZEND_ACC_LINKED;
9487
20.2k
        zend_observer_class_linked_notify(ce, lcname);
9488
20.2k
        return;
9489
20.2k
      } else {
9490
4.23k
        goto link_unbound;
9491
4.23k
      }
9492
64.7k
    } else if (!extends_ast) {
9493
45.9k
link_unbound:
9494
      /* Link unbound simple class */
9495
45.9k
      zend_build_properties_info_table(ce);
9496
45.9k
      zend_inheritance_check_override(ce);
9497
45.9k
      ce->ce_flags |= ZEND_ACC_LINKED;
9498
45.9k
    }
9499
99.6k
  }
9500
9501
108k
  opline = get_next_op();
9502
9503
108k
  if (ce->parent_name) {
9504
    /* Lowercased parent name */
9505
27.8k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9506
27.8k
    opline->op2_type = IS_CONST;
9507
27.8k
    LITERAL_STR(opline->op2, lc_parent_name);
9508
27.8k
  }
9509
9510
108k
  opline->op1_type = IS_CONST;
9511
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9512
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9513
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9514
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9515
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9516
   * zend_add_literal_string() which gives us the new value. */
9517
108k
  opline->op1.constant = zend_add_literal_string(&lcname);
9518
9519
108k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9520
1.92k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9521
1.92k
    opline->extended_value = zend_alloc_cache_slot();
9522
1.92k
    zend_make_var_result(result, opline);
9523
1.92k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9524
      /* We checked above that the class name is not used. This really shouldn't happen. */
9525
0
      zend_error_noreturn(E_ERROR,
9526
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9527
0
    }
9528
106k
  } else {
9529
    /* Generate RTD keys until we find one that isn't in use yet. */
9530
106k
    zend_string *key = NULL;
9531
106k
    do {
9532
106k
      zend_tmp_string_release(key);
9533
106k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9534
106k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9535
9536
    /* RTD key is placed after lcname literal in op1 */
9537
106k
    zend_add_literal_string(&key);
9538
9539
106k
    opline->opcode = ZEND_DECLARE_CLASS;
9540
106k
    if (toplevel
9541
17.0k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9542
        /* We currently don't early-bind classes that implement interfaces or use traits */
9543
6.38k
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9544
106k
    ) {
9545
1.91k
      if (!extends_ast) {
9546
        /* Use empty string for classes without parents to avoid new handler, and special
9547
         * handling of zend_early_binding. */
9548
1.34k
        opline->op2_type = IS_CONST;
9549
1.34k
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9550
1.34k
      }
9551
1.91k
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9552
1.91k
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9553
1.91k
      opline->extended_value = zend_alloc_cache_slot();
9554
1.91k
      opline->result_type = IS_UNUSED;
9555
1.91k
      opline->result.opline_num = -1;
9556
1.91k
    }
9557
106k
  }
9558
108k
}
9559
/* }}} */
9560
9561
static void zend_compile_enum_case(zend_ast *ast)
9562
2.53k
{
9563
2.53k
  zend_class_entry *enum_class = CG(active_class_entry);
9564
2.53k
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9565
7
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9566
7
  }
9567
9568
2.53k
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9569
2.53k
  zend_string *enum_class_name = enum_class->name;
9570
9571
2.53k
  zval class_name_zval;
9572
2.53k
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9573
2.53k
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9574
9575
2.53k
  zval case_name_zval;
9576
2.53k
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9577
2.53k
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9578
9579
2.53k
  zend_ast *case_value_ast = ast->child[1];
9580
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9581
2.53k
  ast->child[1] = NULL;
9582
2.53k
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9583
7
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9584
7
      ZSTR_VAL(enum_case_name),
9585
7
      ZSTR_VAL(enum_class_name));
9586
2.52k
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9587
14
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9588
14
      ZSTR_VAL(enum_case_name),
9589
14
      ZSTR_VAL(enum_class_name));
9590
14
  }
9591
9592
2.51k
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9593
9594
2.51k
  zval value_zv;
9595
2.51k
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9596
9597
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9598
2.51k
  zend_ast *doc_comment_ast = ast->child[2];
9599
2.51k
  zend_string *doc_comment = NULL;
9600
2.51k
  if (doc_comment_ast) {
9601
225
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9602
225
  }
9603
9604
2.51k
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9605
2.51k
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9606
2.51k
  zend_ast_destroy(const_enum_init_ast);
9607
9608
2.51k
  zend_ast *attr_ast = ast->child[3];
9609
2.51k
  if (attr_ast) {
9610
143
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9611
9612
143
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9613
9614
143
    if (deprecated) {
9615
29
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9616
29
    }
9617
143
  }
9618
2.51k
}
9619
9620
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9621
2.16k
{
9622
2.16k
  switch (type) {
9623
1.29k
    case ZEND_SYMBOL_CLASS:
9624
1.29k
      if (!FC(imports)) {
9625
979
        FC(imports) = emalloc(sizeof(HashTable));
9626
979
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9627
979
      }
9628
1.29k
      return FC(imports);
9629
546
    case ZEND_SYMBOL_FUNCTION:
9630
546
      if (!FC(imports_function)) {
9631
451
        FC(imports_function) = emalloc(sizeof(HashTable));
9632
451
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9633
451
      }
9634
546
      return FC(imports_function);
9635
326
    case ZEND_SYMBOL_CONST:
9636
326
      if (!FC(imports_const)) {
9637
246
        FC(imports_const) = emalloc(sizeof(HashTable));
9638
246
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9639
246
      }
9640
326
      return FC(imports_const);
9641
2.16k
    EMPTY_SWITCH_DEFAULT_CASE()
9642
2.16k
  }
9643
9644
0
  return NULL;
9645
2.16k
}
9646
/* }}} */
9647
9648
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9649
80
{
9650
80
  switch (type) {
9651
43
    case ZEND_SYMBOL_CLASS:
9652
43
      return "";
9653
20
    case ZEND_SYMBOL_FUNCTION:
9654
20
      return " function";
9655
17
    case ZEND_SYMBOL_CONST:
9656
17
      return " const";
9657
80
    EMPTY_SWITCH_DEFAULT_CASE()
9658
80
  }
9659
9660
0
  return " unknown";
9661
80
}
9662
/* }}} */
9663
9664
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) /* {{{ */
9665
59
{
9666
59
  if (zend_string_equals_ci(old_name, check_name)) {
9667
26
    return;
9668
26
  }
9669
9670
33
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9671
33
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9672
59
}
9673
/* }}} */
9674
9675
static void zend_compile_use(zend_ast *ast) /* {{{ */
9676
2.16k
{
9677
2.16k
  const zend_ast_list *list = zend_ast_get_list(ast);
9678
2.16k
  uint32_t i;
9679
2.16k
  zend_string *current_ns = FC(current_namespace);
9680
2.16k
  uint32_t type = ast->attr;
9681
2.16k
  HashTable *current_import = zend_get_import_ht(type);
9682
2.16k
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9683
9684
4.45k
  for (i = 0; i < list->children; ++i) {
9685
2.37k
    const zend_ast *use_ast = list->child[i];
9686
2.37k
    zend_ast *old_name_ast = use_ast->child[0];
9687
2.37k
    zend_ast *new_name_ast = use_ast->child[1];
9688
2.37k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9689
2.37k
    zend_string *new_name, *lookup_name;
9690
9691
2.37k
    if (new_name_ast) {
9692
478
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9693
1.89k
    } else {
9694
1.89k
      const char *unqualified_name;
9695
1.89k
      size_t unqualified_name_len;
9696
1.89k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9697
        /* The form "use A\B" is equivalent to "use A\B as B" */
9698
882
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9699
1.01k
      } else {
9700
1.01k
        new_name = zend_string_copy(old_name);
9701
9702
1.01k
        if (!current_ns) {
9703
653
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9704
653
            "has no effect", ZSTR_VAL(new_name));
9705
653
        }
9706
1.01k
      }
9707
1.89k
    }
9708
9709
2.37k
    if (case_sensitive) {
9710
344
      lookup_name = zend_string_copy(new_name);
9711
2.02k
    } else {
9712
2.02k
      lookup_name = zend_string_tolower(new_name);
9713
2.02k
    }
9714
9715
2.37k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9716
33
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9717
33
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9718
33
    }
9719
9720
2.33k
    if (current_ns) {
9721
1.10k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9722
1.10k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9723
1.10k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9724
1.10k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9725
9726
1.10k
      if (zend_have_seen_symbol(ns_name, type)) {
9727
30
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9728
30
      }
9729
9730
1.10k
      zend_string_efree(ns_name);
9731
1.23k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9732
29
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9733
29
    }
9734
9735
2.33k
    zend_string_addref(old_name);
9736
2.33k
    old_name = zend_new_interned_string(old_name);
9737
2.33k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9738
47
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9739
47
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9740
47
    }
9741
9742
2.29k
    zend_string_release_ex(lookup_name, 0);
9743
2.29k
    zend_string_release_ex(new_name, 0);
9744
2.29k
  }
9745
2.16k
}
9746
/* }}} */
9747
9748
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9749
231
{
9750
231
  uint32_t i;
9751
231
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9752
231
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9753
9754
845
  for (i = 0; i < list->children; i++) {
9755
614
    zend_ast *inline_use, *use = list->child[i];
9756
614
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9757
614
    zend_string *name = Z_STR_P(name_zval);
9758
614
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9759
614
    zend_string_release_ex(name, 0);
9760
614
    ZVAL_STR(name_zval, compound_ns);
9761
614
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9762
614
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9763
614
    zend_compile_use(inline_use);
9764
614
  }
9765
231
}
9766
/* }}} */
9767
9768
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9769
4.74k
{
9770
4.74k
  zend_ast_list *list = zend_ast_get_list(ast);
9771
4.74k
  uint32_t i;
9772
4.74k
  zend_ast *attributes_ast = NULL;
9773
4.74k
  zend_op *last_op = NULL;
9774
10.8k
  for (i = 0; i < list->children; ++i) {
9775
6.14k
    zend_ast *const_ast = list->child[i];
9776
6.14k
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9777
753
      ZEND_ASSERT(i == list->children - 1);
9778
753
      attributes_ast = const_ast;
9779
753
      continue;
9780
753
    }
9781
5.39k
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9782
5.39k
    zend_ast *name_ast = const_ast->child[0];
9783
5.39k
    zend_ast **value_ast_ptr = &const_ast->child[1];
9784
5.39k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9785
9786
5.39k
    zend_string *name;
9787
5.39k
    znode name_node, value_node;
9788
5.39k
    zval *value_zv = &value_node.u.constant;
9789
9790
5.39k
    value_node.op_type = IS_CONST;
9791
5.39k
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9792
9793
5.39k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9794
7
      zend_error_noreturn(E_COMPILE_ERROR,
9795
7
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9796
7
    }
9797
9798
5.38k
    name = zend_prefix_with_ns(unqualified_name);
9799
5.38k
    name = zend_new_interned_string(name);
9800
9801
5.38k
    if (FC(imports_const)) {
9802
95
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9803
95
      if (import_name && !zend_string_equals(import_name, name)) {
9804
11
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9805
11
          "the name is already in use", ZSTR_VAL(name));
9806
11
      }
9807
95
    }
9808
9809
5.37k
    name_node.op_type = IS_CONST;
9810
5.37k
    ZVAL_STR(&name_node.u.constant, name);
9811
9812
5.37k
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9813
9814
5.37k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9815
5.37k
  }
9816
4.72k
  if (attributes_ast == NULL) {
9817
3.86k
    return;
9818
3.86k
  }
9819
  /* Validate: attributes can only be applied to one constant at a time
9820
   * Since we store the AST for the attributes in the list of children,
9821
   * there should be exactly 2 children. */
9822
861
  if (list->children > 2) {
9823
8
    zend_error_noreturn(
9824
8
      E_COMPILE_ERROR,
9825
8
      "Cannot apply attributes to multiple constants at once"
9826
8
    );
9827
8
  }
9828
9829
853
  HashTable *attributes = NULL;
9830
853
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9831
9832
853
  ZEND_ASSERT(last_op != NULL);
9833
853
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9834
736
  znode attribs_node;
9835
736
  attribs_node.op_type = IS_CONST;
9836
736
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9837
736
  zend_emit_op_data(&attribs_node);
9838
736
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9839
736
}
9840
/* }}}*/
9841
9842
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
9843
5.77k
{
9844
5.77k
  zend_ast *name_ast = ast->child[0];
9845
5.77k
  zend_ast *stmt_ast = ast->child[1];
9846
5.77k
  zend_string *name;
9847
5.77k
  bool with_bracket = stmt_ast != NULL;
9848
9849
  /* handle mixed syntax declaration or nested namespaces */
9850
5.77k
  if (!FC(has_bracketed_namespaces)) {
9851
4.37k
    if (FC(current_namespace)) {
9852
      /* previous namespace declarations were unbracketed */
9853
1.00k
      if (with_bracket) {
9854
6
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9855
6
          "with unbracketed namespace declarations");
9856
6
      }
9857
1.00k
    }
9858
4.37k
  } else {
9859
    /* previous namespace declarations were bracketed */
9860
1.39k
    if (!with_bracket) {
9861
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9862
7
        "with unbracketed namespace declarations");
9863
1.39k
    } else if (FC(current_namespace) || FC(in_namespace)) {
9864
9
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9865
9
    }
9866
1.39k
  }
9867
9868
5.77k
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9869
2.96k
    || (with_bracket && !FC(has_bracketed_namespaces));
9870
5.75k
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
9871
43
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9872
43
      "the very first statement or after any declare call in the script");
9873
43
  }
9874
9875
5.71k
  if (FC(current_namespace)) {
9876
997
    zend_string_release_ex(FC(current_namespace), 0);
9877
997
  }
9878
9879
5.71k
  if (name_ast) {
9880
4.93k
    name = zend_ast_get_str(name_ast);
9881
9882
4.93k
    if (zend_string_equals_literal_ci(name, "namespace")) {
9883
7
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9884
7
    }
9885
9886
4.92k
    FC(current_namespace) = zend_string_copy(name);
9887
4.92k
  } else {
9888
776
    FC(current_namespace) = NULL;
9889
776
  }
9890
9891
5.70k
  zend_reset_import_tables();
9892
9893
5.70k
  FC(in_namespace) = 1;
9894
5.70k
  if (with_bracket) {
9895
1.95k
    FC(has_bracketed_namespaces) = 1;
9896
1.95k
  }
9897
9898
5.70k
  if (stmt_ast) {
9899
1.95k
    zend_compile_top_stmt(stmt_ast);
9900
1.95k
    zend_end_namespace();
9901
1.95k
  }
9902
5.70k
}
9903
/* }}} */
9904
9905
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
9906
61
{
9907
61
  zend_ast *offset_ast = ast->child[0];
9908
61
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9909
9910
61
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9911
9912
61
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9913
0
    zend_error_noreturn(E_COMPILE_ERROR,
9914
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9915
0
  }
9916
9917
61
  const zend_string *filename = zend_get_compiled_filename();
9918
61
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9919
61
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
9920
9921
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9922
   * case this file was already included. */
9923
61
  if (!zend_hash_find(EG(zend_constants), name)) {
9924
61
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9925
61
  }
9926
61
  zend_string_release_ex(name, 0);
9927
61
}
9928
/* }}} */
9929
9930
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
9931
20.8k
{
9932
20.8k
  const zend_op_array *op_array = CG(active_op_array);
9933
20.8k
  const zend_class_entry *ce = CG(active_class_entry);
9934
9935
20.8k
  switch (ast->attr) {
9936
684
    case T_LINE:
9937
684
      ZVAL_LONG(zv, ast->lineno);
9938
684
      break;
9939
5.16k
    case T_FILE:
9940
5.16k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9941
5.16k
      break;
9942
1.24k
    case T_DIR:
9943
1.24k
    {
9944
1.24k
      const zend_string *filename = CG(compiled_filename);
9945
1.24k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9946
#ifdef ZEND_WIN32
9947
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9948
#else
9949
1.24k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9950
1.24k
#endif
9951
9952
1.24k
      if (zend_string_equals_literal(dirname, ".")) {
9953
710
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9954
710
#ifdef HAVE_GETCWD
9955
710
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9956
#elif defined(HAVE_GETWD)
9957
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9958
#endif
9959
710
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9960
710
      }
9961
9962
1.24k
      ZVAL_STR(zv, dirname);
9963
1.24k
      break;
9964
0
    }
9965
1.66k
    case T_FUNC_C:
9966
1.66k
      if (op_array && op_array->function_name) {
9967
1.37k
        ZVAL_STR_COPY(zv, op_array->function_name);
9968
1.37k
      } else {
9969
289
        ZVAL_EMPTY_STRING(zv);
9970
289
      }
9971
1.66k
      break;
9972
689
    case T_PROPERTY_C: {
9973
689
      zend_string *prop_info_name = CG(context).active_property_info_name;
9974
689
      if (prop_info_name) {
9975
340
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9976
349
      } else {
9977
349
        ZVAL_EMPTY_STRING(zv);
9978
349
      }
9979
689
      break;
9980
0
    }
9981
6.48k
    case T_METHOD_C:
9982
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9983
       * this as not being inside a function. */
9984
6.48k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9985
212
        op_array = NULL;
9986
212
      }
9987
6.48k
      if (op_array && op_array->function_name) {
9988
6.03k
        if (op_array->scope) {
9989
5.57k
          ZVAL_NEW_STR(zv,
9990
5.57k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9991
5.57k
        } else {
9992
462
          ZVAL_STR_COPY(zv, op_array->function_name);
9993
462
        }
9994
6.03k
      } else {
9995
457
        ZVAL_EMPTY_STRING(zv);
9996
457
      }
9997
6.48k
      break;
9998
1.79k
    case T_CLASS_C:
9999
1.79k
      if (ce) {
10000
1.01k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10001
413
          return 0;
10002
604
        } else {
10003
604
          ZVAL_STR_COPY(zv, ce->name);
10004
604
        }
10005
1.01k
      } else {
10006
779
        ZVAL_EMPTY_STRING(zv);
10007
779
      }
10008
1.38k
      break;
10009
2.45k
    case T_TRAIT_C:
10010
2.45k
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10011
477
        ZVAL_STR_COPY(zv, ce->name);
10012
1.97k
      } else {
10013
1.97k
        ZVAL_EMPTY_STRING(zv);
10014
1.97k
      }
10015
2.45k
      break;
10016
705
    case T_NS_C:
10017
705
      if (FC(current_namespace)) {
10018
429
        ZVAL_STR_COPY(zv, FC(current_namespace));
10019
429
      } else {
10020
276
        ZVAL_EMPTY_STRING(zv);
10021
276
      }
10022
705
      break;
10023
20.8k
    EMPTY_SWITCH_DEFAULT_CASE()
10024
20.8k
  }
10025
10026
20.4k
  return 1;
10027
20.8k
}
10028
/* }}} */
10029
10030
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10031
86.9k
{
10032
86.9k
  if (Z_TYPE_P(op) == IS_ARRAY) {
10033
308
    return false;
10034
308
  }
10035
10036
86.6k
  if (Z_TYPE_P(op) == IS_DOUBLE
10037
22.1k
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10038
14.6k
    return false;
10039
14.6k
  }
10040
10041
72.0k
  if (Z_TYPE_P(op) == IS_STRING) {
10042
13.2k
    double dval = 0;
10043
13.2k
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10044
13.2k
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10045
6.06k
      return false;
10046
6.06k
    }
10047
13.2k
  }
10048
10049
65.9k
  return true;
10050
72.0k
}
10051
10052
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10053
280k
{
10054
280k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10055
    /* Array to string warning. */
10056
34.3k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10057
34.3k
  }
10058
10059
246k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10060
131k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10061
80.8k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10062
    /* Only the numeric operations throw errors. */
10063
34.1k
    return 0;
10064
34.1k
  }
10065
10066
212k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10067
9.37k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10068
      /* Adding two arrays is allowed. */
10069
7.06k
      return 0;
10070
7.06k
    }
10071
10072
    /* Numeric operators throw when one of the operands is an array. */
10073
2.31k
    return 1;
10074
9.37k
  }
10075
10076
  /* While basic arithmetic operators always produce numeric string errors,
10077
   * bitwise operators don't produce errors if both operands are strings */
10078
202k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10079
46.7k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10080
20.9k
    return 0;
10081
20.9k
  }
10082
10083
181k
  if (Z_TYPE_P(op1) == IS_STRING
10084
52.4k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10085
36.4k
    return 1;
10086
36.4k
  }
10087
10088
145k
  if (Z_TYPE_P(op2) == IS_STRING
10089
18.6k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10090
8.33k
    return 1;
10091
8.33k
  }
10092
10093
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10094
137k
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10095
128k
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10096
30.3k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10097
8.95k
      return 1;
10098
8.95k
    }
10099
30.3k
  }
10100
10101
128k
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10102
    /* Division by zero throws an error. */
10103
1.50k
    return 1;
10104
1.50k
  }
10105
10106
  /* Mod is an operation that will cast float/float-strings to integers which might
10107
     produce float to int incompatible errors, and also cannot be divided by 0 */
10108
126k
  if (opcode == ZEND_MOD) {
10109
15.5k
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10110
12.9k
      return 1;
10111
12.9k
    }
10112
15.5k
  }
10113
10114
113k
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10115
    /* 0 ** (<0) throws a division by zero error. */
10116
51
    return 1;
10117
51
  }
10118
113k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10119
    /* Shift by negative number throws an error. */
10120
1.24k
    return 1;
10121
1.24k
  }
10122
10123
112k
  return 0;
10124
113k
}
10125
/* }}} */
10126
10127
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10128
222k
{
10129
222k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10130
40.4k
    return 0;
10131
40.4k
  }
10132
10133
182k
  const binary_op_type fn = get_binary_op(opcode);
10134
182k
  fn(result, op1, op2);
10135
182k
  return 1;
10136
222k
}
10137
/* }}} */
10138
10139
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10140
95.7k
{
10141
95.7k
  if (opcode == ZEND_BW_NOT) {
10142
    /* BW_NOT on string does not convert the string into an integer. */
10143
7.40k
    if (Z_TYPE_P(op) == IS_STRING) {
10144
1.58k
      return 0;
10145
1.58k
    }
10146
5.82k
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10147
7.40k
  }
10148
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10149
88.3k
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10150
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10151
88.3k
    return Z_TYPE_P(op) == IS_DOUBLE;
10152
88.3k
  }
10153
10154
0
  return 0;
10155
88.3k
}
10156
10157
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10158
94.5k
{
10159
94.5k
  if (zend_unary_op_produces_error(opcode, op)) {
10160
1.51k
    return 0;
10161
1.51k
  }
10162
10163
93.0k
  const unary_op_type fn = get_unary_op(opcode);
10164
93.0k
  fn(result, op);
10165
93.0k
  return 1;
10166
94.5k
}
10167
/* }}} */
10168
10169
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10170
50.3k
{
10171
50.3k
  zval right;
10172
50.3k
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10173
50.3k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10174
50.3k
}
10175
/* }}} */
10176
10177
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10178
11.5k
{
10179
11.5k
  const binary_op_type fn = kind == ZEND_AST_GREATER
10180
11.5k
    ? is_smaller_function : is_smaller_or_equal_function;
10181
11.5k
  fn(result, op2, op1);
10182
11.5k
}
10183
/* }}} */
10184
10185
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10186
2.73M
{
10187
2.73M
  const zend_ast_list *list = zend_ast_get_list(ast);
10188
2.73M
  zend_ast *last_elem_ast = NULL;
10189
2.73M
  uint32_t i;
10190
2.73M
  bool is_constant = true;
10191
10192
2.73M
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10193
7
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10194
7
  }
10195
10196
  /* First ensure that *all* child nodes are constant and by-val */
10197
5.88M
  for (i = 0; i < list->children; ++i) {
10198
3.14M
    zend_ast *elem_ast = list->child[i];
10199
10200
3.14M
    if (elem_ast == NULL) {
10201
      /* Report error at line of last non-empty element */
10202
143
      if (last_elem_ast) {
10203
81
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10204
81
      }
10205
143
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10206
143
    }
10207
10208
3.14M
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10209
3.07M
      zend_eval_const_expr(&elem_ast->child[0]);
10210
3.07M
      zend_eval_const_expr(&elem_ast->child[1]);
10211
10212
3.07M
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10213
495k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10214
3.07M
      ) {
10215
2.58M
        is_constant = false;
10216
2.58M
      }
10217
3.07M
    } else {
10218
79.8k
      zend_eval_const_expr(&elem_ast->child[0]);
10219
10220
79.8k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10221
78.4k
        is_constant = false;
10222
78.4k
      }
10223
79.8k
    }
10224
10225
3.14M
    last_elem_ast = elem_ast;
10226
3.14M
  }
10227
10228
2.73M
  if (!is_constant) {
10229
2.61M
    return 0;
10230
2.61M
  }
10231
10232
120k
  if (!list->children) {
10233
17.8k
    ZVAL_EMPTY_ARRAY(result);
10234
17.8k
    return 1;
10235
17.8k
  }
10236
10237
102k
  array_init_size(result, list->children);
10238
497k
  for (i = 0; i < list->children; ++i) {
10239
398k
    const zend_ast *elem_ast = list->child[i];
10240
398k
    zend_ast *value_ast = elem_ast->child[0];
10241
398k
    zend_ast *key_ast;
10242
10243
398k
    zval *value = zend_ast_get_zval(value_ast);
10244
398k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10245
1.13k
      if (Z_TYPE_P(value) == IS_ARRAY) {
10246
1.12k
        const HashTable *ht = Z_ARRVAL_P(value);
10247
1.12k
        zval *val;
10248
1.12k
        zend_string *key;
10249
10250
3.08k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10251
3.08k
          if (key) {
10252
160
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10253
459
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10254
12
            zval_ptr_dtor(result);
10255
12
            return 0;
10256
12
          }
10257
607
          Z_TRY_ADDREF_P(val);
10258
607
        } ZEND_HASH_FOREACH_END();
10259
10260
1.11k
        continue;
10261
1.12k
      } else {
10262
10
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10263
10
      }
10264
1.13k
    }
10265
10266
397k
    Z_TRY_ADDREF_P(value);
10267
10268
397k
    key_ast = elem_ast->child[1];
10269
397k
    if (key_ast) {
10270
26.3k
      const zval *key = zend_ast_get_zval(key_ast);
10271
26.3k
      switch (Z_TYPE_P(key)) {
10272
8.26k
        case IS_LONG:
10273
8.26k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10274
8.26k
          break;
10275
10.3k
        case IS_STRING:
10276
10.3k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10277
10.3k
          break;
10278
7.08k
        case IS_DOUBLE: {
10279
7.08k
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10280
          /* Incompatible float will generate an error, leave this to run-time */
10281
7.08k
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10282
3.41k
            goto fail;
10283
3.41k
          }
10284
3.67k
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10285
3.67k
          break;
10286
7.08k
        }
10287
478
        case IS_FALSE:
10288
478
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10289
478
          break;
10290
78
        case IS_TRUE:
10291
78
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10292
78
          break;
10293
15
        case IS_NULL:
10294
          /* Null key will generate a warning at run-time. */
10295
15
          goto fail;
10296
4
        default:
10297
4
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10298
0
          break;
10299
26.3k
      }
10300
370k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10301
3.51k
fail:
10302
3.51k
      zval_ptr_dtor_nogc(value);
10303
3.51k
      zval_ptr_dtor(result);
10304
3.51k
      return 0;
10305
87
    }
10306
397k
  }
10307
10308
98.9k
  return 1;
10309
102k
}
10310
/* }}} */
10311
10312
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10313
2.30M
{
10314
2.30M
  zend_ast *left_ast = ast->child[0];
10315
2.30M
  zend_ast *right_ast = ast->child[1];
10316
2.30M
  uint32_t opcode = ast->attr;
10317
10318
2.30M
  znode left_node, right_node;
10319
10320
2.30M
  zend_compile_expr(&left_node, left_ast);
10321
2.30M
  zend_compile_expr(&right_node, right_ast);
10322
10323
2.30M
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10324
137k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10325
137k
        &left_node.u.constant, &right_node.u.constant)
10326
137k
    ) {
10327
122k
      result->op_type = IS_CONST;
10328
122k
      zval_ptr_dtor(&left_node.u.constant);
10329
122k
      zval_ptr_dtor(&right_node.u.constant);
10330
122k
      return;
10331
122k
    }
10332
137k
  }
10333
10334
2.18M
  do {
10335
2.18M
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10336
      /* 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) */
10337
6.13k
      if (left_node.op_type == IS_CONST) {
10338
2.14k
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10339
992
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10340
992
          opline->extended_value =
10341
992
            (opcode == ZEND_IS_IDENTICAL) ?
10342
839
              (1 << Z_TYPE(left_node.u.constant)) :
10343
992
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10344
992
          return;
10345
992
        }
10346
3.99k
      } else if (right_node.op_type == IS_CONST) {
10347
1.78k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10348
466
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10349
466
          opline->extended_value =
10350
466
            (opcode == ZEND_IS_IDENTICAL) ?
10351
238
              (1 << Z_TYPE(right_node.u.constant)) :
10352
466
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10353
466
          return;
10354
466
        }
10355
1.78k
      }
10356
2.17M
    } else if (opcode == ZEND_CONCAT) {
10357
      /* convert constant operands to strings at compile-time */
10358
81.2k
      if (left_node.op_type == IS_CONST) {
10359
15.5k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10360
124
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10361
15.4k
        } else {
10362
15.4k
          convert_to_string(&left_node.u.constant);
10363
15.4k
        }
10364
15.5k
      }
10365
81.2k
      if (right_node.op_type == IS_CONST) {
10366
27.0k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10367
190
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10368
26.8k
        } else {
10369
26.8k
          convert_to_string(&right_node.u.constant);
10370
26.8k
        }
10371
27.0k
      }
10372
81.2k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10373
0
        opcode = ZEND_FAST_CONCAT;
10374
0
      }
10375
81.2k
    }
10376
2.18M
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10377
2.18M
  } while (0);
10378
2.18M
}
10379
/* }}} */
10380
10381
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10382
 * evaluation order. */
10383
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10384
487k
{
10385
487k
  zend_ast *left_ast = ast->child[0];
10386
487k
  zend_ast *right_ast = ast->child[1];
10387
487k
  znode left_node, right_node;
10388
10389
487k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10390
10391
487k
  zend_compile_expr(&left_node, left_ast);
10392
487k
  zend_compile_expr(&right_node, right_ast);
10393
10394
487k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10395
10.1k
    result->op_type = IS_CONST;
10396
10.1k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10397
10.1k
      &left_node.u.constant, &right_node.u.constant);
10398
10.1k
    zval_ptr_dtor(&left_node.u.constant);
10399
10.1k
    zval_ptr_dtor(&right_node.u.constant);
10400
10.1k
    return;
10401
10.1k
  }
10402
10403
477k
  zend_emit_op_tmp(result,
10404
477k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10405
477k
    &right_node, &left_node);
10406
477k
}
10407
/* }}} */
10408
10409
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10410
1.36M
{
10411
1.36M
  zend_ast *expr_ast = ast->child[0];
10412
1.36M
  uint32_t opcode = ast->attr;
10413
10414
1.36M
  znode expr_node;
10415
1.36M
  zend_compile_expr(&expr_node, expr_ast);
10416
10417
1.36M
  if (expr_node.op_type == IS_CONST
10418
90.4k
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10419
89.3k
    result->op_type = IS_CONST;
10420
89.3k
    zval_ptr_dtor(&expr_node.u.constant);
10421
89.3k
    return;
10422
89.3k
  }
10423
10424
1.27M
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10425
1.27M
}
10426
/* }}} */
10427
10428
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10429
58.1k
{
10430
58.1k
  zend_ast *expr_ast = ast->child[0];
10431
58.1k
  znode expr_node, right_node;
10432
10433
58.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10434
10435
58.1k
  zend_compile_expr(&expr_node, expr_ast);
10436
10437
58.1k
  if (expr_node.op_type == IS_CONST
10438
30.0k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10439
27.6k
    result->op_type = IS_CONST;
10440
27.6k
    zval_ptr_dtor(&expr_node.u.constant);
10441
27.6k
    return;
10442
27.6k
  }
10443
10444
30.5k
  right_node.op_type = IS_CONST;
10445
30.5k
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10446
30.5k
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10447
30.5k
}
10448
/* }}} */
10449
10450
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10451
17.5k
{
10452
17.5k
  zend_ast *left_ast = ast->child[0];
10453
17.5k
  zend_ast *right_ast = ast->child[1];
10454
10455
17.5k
  znode left_node, right_node;
10456
17.5k
  zend_op *opline_jmpz, *opline_bool;
10457
17.5k
  uint32_t opnum_jmpz;
10458
10459
17.5k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10460
10461
17.5k
  zend_compile_expr(&left_node, left_ast);
10462
10463
17.5k
  if (left_node.op_type == IS_CONST) {
10464
3.18k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10465
1.95k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10466
1.91k
      result->op_type = IS_CONST;
10467
1.91k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10468
1.91k
    } else {
10469
1.26k
      zend_compile_expr(&right_node, right_ast);
10470
10471
1.26k
      if (right_node.op_type == IS_CONST) {
10472
490
        result->op_type = IS_CONST;
10473
490
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10474
10475
490
        zval_ptr_dtor(&right_node.u.constant);
10476
776
      } else {
10477
776
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10478
776
      }
10479
1.26k
    }
10480
10481
3.18k
    zval_ptr_dtor(&left_node.u.constant);
10482
3.18k
    return;
10483
3.18k
  }
10484
10485
14.4k
  opnum_jmpz = get_next_op_number();
10486
14.4k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10487
14.4k
    &left_node, NULL);
10488
10489
14.4k
  if (left_node.op_type == IS_TMP_VAR) {
10490
12.2k
    SET_NODE(opline_jmpz->result, &left_node);
10491
12.2k
    GET_NODE(result, opline_jmpz->result);
10492
12.2k
  } else {
10493
2.16k
    zend_make_tmp_result(result, opline_jmpz);
10494
2.16k
  }
10495
10496
14.4k
  zend_compile_expr(&right_node, right_ast);
10497
10498
14.4k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10499
14.4k
  SET_NODE(opline_bool->result, result);
10500
10501
14.4k
  zend_update_jump_target_to_next(opnum_jmpz);
10502
14.4k
}
10503
/* }}} */
10504
10505
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10506
11.1k
{
10507
11.1k
  zend_ast *var_ast = ast->child[0];
10508
11.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10509
10510
11.1k
  zend_ensure_writable_variable(var_ast);
10511
10512
11.1k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10513
860
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10514
860
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10515
860
    zend_make_tmp_result(result, opline);
10516
10.2k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10517
1.66k
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10518
1.66k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10519
1.66k
    zend_make_tmp_result(result, opline);
10520
8.57k
  } else {
10521
8.57k
    znode var_node;
10522
8.57k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10523
8.57k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10524
199
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10525
199
    }
10526
8.57k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10527
8.57k
      &var_node, NULL);
10528
8.57k
  }
10529
11.1k
}
10530
/* }}} */
10531
10532
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10533
3.72k
{
10534
3.72k
  zend_ast *var_ast = ast->child[0];
10535
3.72k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10536
10537
3.72k
  zend_ensure_writable_variable(var_ast);
10538
10539
3.72k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10540
921
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10541
921
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10542
921
    opline->result_type = IS_TMP_VAR;
10543
921
    result->op_type = IS_TMP_VAR;
10544
2.80k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10545
259
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10546
259
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10547
259
    opline->result_type = IS_TMP_VAR;
10548
259
    result->op_type = IS_TMP_VAR;
10549
2.54k
  } else {
10550
2.54k
    znode var_node;
10551
2.54k
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10552
2.54k
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10553
122
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10554
122
    }
10555
2.54k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10556
2.54k
      &var_node, NULL);
10557
2.54k
  }
10558
3.72k
}
10559
/* }}} */
10560
10561
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10562
3.78k
{
10563
3.78k
  zend_ast *expr_ast = ast->child[0];
10564
3.78k
  znode expr_node;
10565
3.78k
  zend_op *opline;
10566
10567
3.78k
  zend_compile_expr(&expr_node, expr_ast);
10568
10569
3.78k
  if (ast->attr == _IS_BOOL) {
10570
276
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10571
3.50k
  } else if (ast->attr == IS_NULL) {
10572
19
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10573
3.48k
  } else {
10574
3.48k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10575
3.48k
    opline->extended_value = ast->attr;
10576
3.48k
  }
10577
3.78k
}
10578
/* }}} */
10579
10580
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10581
5.75k
{
10582
5.75k
  zend_ast *cond_ast = ast->child[0];
10583
5.75k
  zend_ast *false_ast = ast->child[2];
10584
10585
5.75k
  znode cond_node, false_node;
10586
5.75k
  zend_op *opline_qm_assign;
10587
5.75k
  uint32_t opnum_jmp_set;
10588
10589
5.75k
  ZEND_ASSERT(ast->child[1] == NULL);
10590
10591
5.75k
  zend_compile_expr(&cond_node, cond_ast);
10592
10593
5.75k
  opnum_jmp_set = get_next_op_number();
10594
5.75k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10595
10596
5.75k
  zend_compile_expr(&false_node, false_ast);
10597
10598
5.75k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10599
5.75k
  SET_NODE(opline_qm_assign->result, result);
10600
10601
5.75k
  zend_update_jump_target_to_next(opnum_jmp_set);
10602
5.75k
}
10603
/* }}} */
10604
10605
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10606
12.5k
{
10607
12.5k
  zend_ast *cond_ast = ast->child[0];
10608
12.5k
  zend_ast *true_ast = ast->child[1];
10609
12.5k
  zend_ast *false_ast = ast->child[2];
10610
10611
12.5k
  znode cond_node, true_node, false_node;
10612
12.5k
  zend_op *opline_qm_assign2;
10613
12.5k
  uint32_t opnum_jmpz, opnum_jmp;
10614
10615
12.5k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10616
3.38k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10617
2.84k
    if (cond_ast->child[1]) {
10618
35
      if (true_ast) {
10619
19
        zend_error(E_COMPILE_ERROR,
10620
19
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10621
19
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10622
19
      } else {
10623
16
        zend_error(E_COMPILE_ERROR,
10624
16
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10625
16
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10626
16
      }
10627
2.80k
    } else {
10628
2.80k
      if (true_ast) {
10629
10
        zend_error(E_COMPILE_ERROR,
10630
10
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10631
10
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10632
2.79k
      } else {
10633
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10634
         * as a ?: (b ?: c). */
10635
2.79k
      }
10636
2.80k
    }
10637
2.84k
  }
10638
10639
12.5k
  if (!true_ast) {
10640
5.75k
    zend_compile_shorthand_conditional(result, ast);
10641
5.75k
    return;
10642
5.75k
  }
10643
10644
6.82k
  zend_compile_expr(&cond_node, cond_ast);
10645
10646
6.82k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10647
10648
6.82k
  zend_compile_expr(&true_node, true_ast);
10649
10650
6.82k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10651
10652
6.82k
  opnum_jmp = zend_emit_jump(0);
10653
10654
6.82k
  zend_update_jump_target_to_next(opnum_jmpz);
10655
10656
6.82k
  zend_compile_expr(&false_node, false_ast);
10657
10658
6.82k
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10659
6.82k
  SET_NODE(opline_qm_assign2->result, result);
10660
10661
6.82k
  zend_update_jump_target_to_next(opnum_jmp);
10662
6.82k
}
10663
/* }}} */
10664
10665
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10666
1.80M
{
10667
1.80M
  zend_ast *expr_ast = ast->child[0];
10668
1.80M
  zend_ast *default_ast = ast->child[1];
10669
10670
1.80M
  znode expr_node, default_node;
10671
1.80M
  zend_op *opline;
10672
1.80M
  uint32_t opnum;
10673
10674
1.80M
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10675
10676
1.80M
  opnum = get_next_op_number();
10677
1.80M
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10678
10679
1.80M
  zend_compile_expr(&default_node, default_ast);
10680
10681
1.80M
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10682
1.80M
  SET_NODE(opline->result, result);
10683
10684
1.80M
  opline = &CG(active_op_array)->opcodes[opnum];
10685
1.80M
  opline->op2.opline_num = get_next_op_number();
10686
1.80M
}
10687
/* }}} */
10688
10689
99.5k
static void znode_dtor(zval *zv) {
10690
99.5k
  znode *node = Z_PTR_P(zv);
10691
99.5k
  if (node->op_type == IS_CONST) {
10692
4.67k
    zval_ptr_dtor_nogc(&node->u.constant);
10693
4.67k
  }
10694
99.5k
  efree(node);
10695
99.5k
}
10696
10697
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10698
18.5k
{
10699
18.5k
  zend_ast *var_ast = ast->child[0];
10700
18.5k
  zend_ast *default_ast = ast->child[1];
10701
10702
18.5k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10703
18.5k
  zend_op *opline;
10704
18.5k
  uint32_t coalesce_opnum;
10705
18.5k
  bool need_frees = false;
10706
10707
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10708
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10709
18.5k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10710
18.5k
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10711
10712
18.5k
  zend_ensure_writable_variable(var_ast);
10713
18.5k
  if (is_this_fetch(var_ast)) {
10714
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10715
7
  }
10716
10717
18.5k
  ALLOC_HASHTABLE(CG(memoized_exprs));
10718
18.5k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10719
10720
18.5k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10721
18.5k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10722
10723
18.5k
  coalesce_opnum = get_next_op_number();
10724
18.5k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10725
10726
18.5k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10727
18.5k
  if (var_ast->kind == ZEND_AST_DIM) {
10728
15.5k
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10729
15.5k
  } else {
10730
2.99k
    zend_compile_expr(&default_node, default_ast);
10731
2.99k
  }
10732
10733
18.5k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10734
18.5k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10735
10736
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10737
18.5k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10738
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10739
18.5k
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10740
18.5k
  switch (kind) {
10741
1.45k
    case ZEND_AST_VAR:
10742
1.45k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10743
1.45k
      break;
10744
614
    case ZEND_AST_STATIC_PROP:
10745
614
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10746
614
      opline->result_type = IS_TMP_VAR;
10747
614
      var_node_w.op_type = IS_TMP_VAR;
10748
614
      zend_emit_op_data(&default_node);
10749
614
      assign_node = var_node_w;
10750
614
      break;
10751
15.2k
    case ZEND_AST_DIM:
10752
15.2k
      opline->opcode = ZEND_ASSIGN_DIM;
10753
15.2k
      opline->result_type = IS_TMP_VAR;
10754
15.2k
      var_node_w.op_type = IS_TMP_VAR;
10755
15.2k
      zend_emit_op_data(&default_node);
10756
15.2k
      assign_node = var_node_w;
10757
15.2k
      break;
10758
816
    case ZEND_AST_PROP:
10759
816
    case ZEND_AST_NULLSAFE_PROP:
10760
816
      opline->opcode = ZEND_ASSIGN_OBJ;
10761
816
      opline->result_type = IS_TMP_VAR;
10762
816
      var_node_w.op_type = IS_TMP_VAR;
10763
816
      zend_emit_op_data(&default_node);
10764
816
      assign_node = var_node_w;
10765
816
      break;
10766
0
    EMPTY_SWITCH_DEFAULT_CASE();
10767
18.5k
  }
10768
10769
18.1k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10770
18.1k
  SET_NODE(opline->result, result);
10771
10772
58.4k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10773
58.4k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10774
15.2k
      need_frees = true;
10775
15.2k
      break;
10776
15.2k
    }
10777
58.4k
  } ZEND_HASH_FOREACH_END();
10778
10779
  /* Free DUPed expressions if there are any */
10780
18.1k
  if (need_frees) {
10781
15.2k
    uint32_t jump_opnum = zend_emit_jump(0);
10782
15.2k
    zend_update_jump_target_to_next(coalesce_opnum);
10783
206k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10784
206k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10785
93.2k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10786
93.2k
      }
10787
206k
    } ZEND_HASH_FOREACH_END();
10788
15.2k
    zend_update_jump_target_to_next(jump_opnum);
10789
15.2k
  } else {
10790
2.95k
    zend_update_jump_target_to_next(coalesce_opnum);
10791
2.95k
  }
10792
10793
18.1k
  zend_hash_destroy(CG(memoized_exprs));
10794
18.1k
  FREE_HASHTABLE(CG(memoized_exprs));
10795
18.1k
  CG(memoized_exprs) = orig_memoized_exprs;
10796
18.1k
  CG(memoize_mode) = orig_memoize_mode;
10797
18.1k
}
10798
/* }}} */
10799
10800
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10801
4.43k
{
10802
4.43k
  zend_op *opline;
10803
4.43k
  zend_ast *expr_ast = ast->child[0];
10804
10805
4.43k
  znode expr_node;
10806
4.43k
  zend_compile_expr(&expr_node, expr_ast);
10807
10808
4.43k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10809
4.43k
  opline->extended_value = 1;
10810
10811
4.43k
  result->op_type = IS_CONST;
10812
4.43k
  ZVAL_LONG(&result->u.constant, 1);
10813
4.43k
}
10814
/* }}} */
10815
10816
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10817
18.8k
{
10818
18.8k
  zend_ast *value_ast = ast->child[0];
10819
18.8k
  zend_ast *key_ast = ast->child[1];
10820
10821
18.8k
  znode value_node, key_node;
10822
18.8k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10823
18.8k
  zend_op *opline;
10824
18.8k
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10825
10826
18.8k
  zend_mark_function_as_generator();
10827
10828
18.8k
  if (key_ast) {
10829
641
    zend_compile_expr(&key_node, key_ast);
10830
641
    key_node_ptr = &key_node;
10831
641
  }
10832
10833
18.8k
  if (value_ast) {
10834
13.6k
    if (returns_by_ref && zend_is_variable(value_ast)) {
10835
161
      zend_assert_not_short_circuited(value_ast);
10836
161
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
10837
13.4k
    } else {
10838
13.4k
      zend_compile_expr(&value_node, value_ast);
10839
13.4k
    }
10840
13.6k
    value_node_ptr = &value_node;
10841
13.6k
  }
10842
10843
18.8k
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10844
10845
18.8k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10846
1.53k
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10847
1.53k
  }
10848
18.8k
}
10849
/* }}} */
10850
10851
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
10852
1.62k
{
10853
1.62k
  zend_ast *expr_ast = ast->child[0];
10854
1.62k
  znode expr_node;
10855
10856
1.62k
  zend_mark_function_as_generator();
10857
10858
1.62k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10859
9
    zend_error_noreturn(E_COMPILE_ERROR,
10860
9
      "Cannot use \"yield from\" inside a by-reference generator");
10861
9
  }
10862
10863
1.61k
  zend_compile_expr(&expr_node, expr_ast);
10864
1.61k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10865
1.61k
}
10866
/* }}} */
10867
10868
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10869
935
{
10870
935
  zend_ast *obj_ast = ast->child[0];
10871
935
  zend_ast *class_ast = ast->child[1];
10872
10873
935
  znode obj_node, class_node;
10874
935
  zend_op *opline;
10875
10876
935
  zend_compile_expr(&obj_node, obj_ast);
10877
935
  if (obj_node.op_type == IS_CONST) {
10878
90
    zend_do_free(&obj_node);
10879
90
    result->op_type = IS_CONST;
10880
90
    ZVAL_FALSE(&result->u.constant);
10881
90
    return;
10882
90
  }
10883
10884
845
  zend_compile_class_ref(&class_node, class_ast,
10885
845
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10886
10887
845
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10888
10889
845
  if (class_node.op_type == IS_CONST) {
10890
417
    opline->op2_type = IS_CONST;
10891
417
    opline->op2.constant = zend_add_class_name_literal(
10892
417
      Z_STR(class_node.u.constant));
10893
417
    opline->extended_value = zend_alloc_cache_slot();
10894
428
  } else {
10895
428
    SET_NODE(opline->op2, &class_node);
10896
428
  }
10897
845
}
10898
/* }}} */
10899
10900
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
10901
9.28k
{
10902
9.28k
  zend_ast *expr_ast = ast->child[0];
10903
9.28k
  znode expr_node;
10904
9.28k
  zend_op *opline;
10905
10906
9.28k
  zend_do_extended_fcall_begin();
10907
9.28k
  zend_compile_expr(&expr_node, expr_ast);
10908
10909
9.28k
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10910
9.28k
  opline->extended_value = ast->attr;
10911
10912
9.28k
  zend_do_extended_fcall_end();
10913
9.28k
}
10914
/* }}} */
10915
10916
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
10917
12.2k
{
10918
12.2k
  zend_ast *var_ast = ast->child[0];
10919
10920
12.2k
  znode var_node;
10921
12.2k
  zend_op *opline = NULL;
10922
10923
12.2k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10924
10925
12.2k
  if (!zend_is_variable(var_ast)) {
10926
202
    if (ast->kind == ZEND_AST_EMPTY) {
10927
      /* empty(expr) can be transformed to !expr */
10928
145
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10929
145
      zend_compile_expr(result, not_ast);
10930
145
      return;
10931
145
    } else {
10932
57
      zend_error_noreturn(E_COMPILE_ERROR,
10933
57
        "Cannot use isset() on the result of an expression "
10934
57
        "(you can use \"null !== expression\" instead)");
10935
57
    }
10936
202
  }
10937
10938
12.0k
  if (is_globals_fetch(var_ast)) {
10939
49
    result->op_type = IS_CONST;
10940
49
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10941
49
    return;
10942
49
  }
10943
10944
11.9k
  if (is_global_var_fetch(var_ast)) {
10945
2.59k
    if (!var_ast->child[1]) {
10946
8
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10947
8
    }
10948
10949
2.58k
    zend_compile_expr(&var_node, var_ast->child[1]);
10950
2.58k
    if (var_node.op_type == IS_CONST) {
10951
1.95k
      convert_to_string(&var_node.u.constant);
10952
1.95k
    }
10953
10954
2.58k
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10955
2.58k
    opline->extended_value =
10956
2.58k
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10957
2.58k
    return;
10958
2.59k
  }
10959
10960
9.35k
  zend_short_circuiting_mark_inner(var_ast);
10961
9.35k
  switch (var_ast->kind) {
10962
3.55k
    case ZEND_AST_VAR:
10963
3.55k
      if (is_this_fetch(var_ast)) {
10964
750
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10965
750
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10966
2.80k
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
10967
1.88k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10968
1.88k
      } else {
10969
921
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
10970
921
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10971
921
      }
10972
3.55k
      break;
10973
4.33k
    case ZEND_AST_DIM:
10974
4.33k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10975
4.33k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10976
4.33k
      break;
10977
809
    case ZEND_AST_PROP:
10978
1.08k
    case ZEND_AST_NULLSAFE_PROP:
10979
1.08k
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
10980
1.08k
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10981
1.08k
      break;
10982
381
    case ZEND_AST_STATIC_PROP:
10983
381
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
10984
381
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10985
381
      break;
10986
9.35k
    EMPTY_SWITCH_DEFAULT_CASE()
10987
9.35k
  }
10988
10989
9.34k
  result->op_type = opline->result_type = IS_TMP_VAR;
10990
9.34k
  if (!(ast->kind == ZEND_AST_ISSET)) {
10991
2.53k
    opline->extended_value |= ZEND_ISEMPTY;
10992
2.53k
  }
10993
9.34k
}
10994
/* }}} */
10995
10996
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
10997
14.7M
{
10998
14.7M
  zend_ast *expr_ast = ast->child[0];
10999
14.7M
  znode silence_node;
11000
11001
14.7M
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11002
11003
14.7M
  if (expr_ast->kind == ZEND_AST_VAR) {
11004
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11005
     * happen outside the silenced section. */
11006
176k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11007
14.6M
  } else {
11008
14.6M
    zend_compile_expr(result, expr_ast);
11009
14.6M
  }
11010
11011
14.7M
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11012
14.7M
}
11013
/* }}} */
11014
11015
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11016
22.0k
{
11017
22.0k
  zend_ast *expr_ast = ast->child[0];
11018
11019
22.0k
  zval fn_name;
11020
22.0k
  zend_ast *name_ast, *args_ast, *call_ast;
11021
11022
22.0k
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11023
11024
22.0k
  ZVAL_STRING(&fn_name, "shell_exec");
11025
22.0k
  name_ast = zend_ast_create_zval(&fn_name);
11026
22.0k
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11027
22.0k
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11028
11029
22.0k
  zend_compile_expr(result, call_ast);
11030
11031
22.0k
  zval_ptr_dtor(&fn_name);
11032
22.0k
}
11033
/* }}} */
11034
11035
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11036
126k
{
11037
126k
  zend_ast_list *list = zend_ast_get_list(ast);
11038
126k
  zend_op *opline;
11039
126k
  uint32_t i, opnum_init = -1;
11040
126k
  bool packed = true;
11041
11042
126k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11043
70.5k
    result->op_type = IS_CONST;
11044
70.5k
    return;
11045
70.5k
  }
11046
11047
  /* Empty arrays are handled at compile-time */
11048
56.3k
  ZEND_ASSERT(list->children > 0);
11049
11050
187k
  for (i = 0; i < list->children; ++i) {
11051
131k
    zend_ast *elem_ast = list->child[i];
11052
131k
    zend_ast *value_ast, *key_ast;
11053
131k
    bool by_ref;
11054
131k
    znode value_node, key_node, *key_node_ptr = NULL;
11055
11056
131k
    if (elem_ast == NULL) {
11057
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11058
0
    }
11059
11060
131k
    value_ast = elem_ast->child[0];
11061
11062
131k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11063
891
      zend_compile_expr(&value_node, value_ast);
11064
891
      if (i == 0) {
11065
721
        opnum_init = get_next_op_number();
11066
721
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11067
721
      }
11068
891
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11069
891
      SET_NODE(opline->result, result);
11070
891
      continue;
11071
891
    }
11072
11073
130k
    key_ast = elem_ast->child[1];
11074
130k
    by_ref = elem_ast->attr;
11075
11076
130k
    if (key_ast) {
11077
9.27k
      zend_compile_expr(&key_node, key_ast);
11078
9.27k
      zend_handle_numeric_op(&key_node);
11079
9.27k
      key_node_ptr = &key_node;
11080
9.27k
    }
11081
11082
130k
    if (by_ref) {
11083
445
      zend_ensure_writable_variable(value_ast);
11084
445
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11085
130k
    } else {
11086
130k
      zend_compile_expr(&value_node, value_ast);
11087
130k
    }
11088
11089
130k
    if (i == 0) {
11090
55.3k
      opnum_init = get_next_op_number();
11091
55.3k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11092
55.3k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11093
75.4k
    } else {
11094
75.4k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11095
75.4k
        &value_node, key_node_ptr);
11096
75.4k
      SET_NODE(opline->result, result);
11097
75.4k
    }
11098
130k
    opline->extended_value |= by_ref;
11099
11100
130k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11101
5.21k
      packed = false;
11102
5.21k
    }
11103
130k
  }
11104
11105
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11106
56.2k
  if (!packed) {
11107
3.55k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11108
3.55k
    opline = &CG(active_op_array)->opcodes[opnum_init];
11109
3.55k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11110
3.55k
  }
11111
56.2k
}
11112
/* }}} */
11113
11114
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11115
8.73M
{
11116
8.73M
  zend_ast *name_ast = ast->child[0];
11117
11118
8.73M
  zend_op *opline;
11119
11120
8.73M
  bool is_fully_qualified;
11121
8.73M
  zend_string *orig_name = zend_ast_get_str(name_ast);
11122
8.73M
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11123
11124
8.73M
  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__"))) {
11125
735
    zend_ast *last = CG(ast);
11126
11127
1.47k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11128
1.09k
      const zend_ast_list *list = zend_ast_get_list(last);
11129
1.09k
      if (list->children == 0) {
11130
363
        break;
11131
363
      }
11132
736
      last = list->child[list->children-1];
11133
736
    }
11134
735
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11135
105
      result->op_type = IS_CONST;
11136
105
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11137
105
      zend_string_release_ex(resolved_name, 0);
11138
105
      return;
11139
105
    }
11140
735
  }
11141
11142
8.73M
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11143
54.9k
    result->op_type = IS_CONST;
11144
54.9k
    zend_string_release_ex(resolved_name, 0);
11145
54.9k
    return;
11146
54.9k
  }
11147
11148
8.68M
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11149
8.68M
  opline->op2_type = IS_CONST;
11150
11151
8.68M
  if (is_fully_qualified || !FC(current_namespace)) {
11152
219k
    opline->op1.num = 0;
11153
219k
    opline->op2.constant = zend_add_const_name_literal(
11154
219k
      resolved_name, false);
11155
8.46M
  } else {
11156
8.46M
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11157
8.46M
    opline->op2.constant = zend_add_const_name_literal(
11158
8.46M
      resolved_name, true);
11159
8.46M
  }
11160
8.68M
  opline->extended_value = zend_alloc_cache_slot();
11161
8.68M
}
11162
/* }}} */
11163
11164
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11165
84.4k
{
11166
84.4k
  zend_ast *class_ast;
11167
84.4k
  zend_ast *const_ast;
11168
84.4k
  znode class_node, const_node;
11169
84.4k
  zend_op *opline;
11170
11171
84.4k
  zend_eval_const_expr(&ast->child[0]);
11172
84.4k
  zend_eval_const_expr(&ast->child[1]);
11173
11174
84.4k
  class_ast = ast->child[0];
11175
84.4k
  const_ast = ast->child[1];
11176
11177
84.4k
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11178
66.6k
    zval *const_zv = zend_ast_get_zval(const_ast);
11179
66.6k
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11180
65.3k
      zend_string *const_str = Z_STR_P(const_zv);
11181
65.3k
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11182
65.3k
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11183
231
        result->op_type = IS_CONST;
11184
231
        zend_string_release_ex(resolved_name, 0);
11185
231
        return;
11186
231
      }
11187
65.1k
      zend_string_release_ex(resolved_name, 0);
11188
65.1k
    }
11189
66.6k
  }
11190
11191
84.2k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11192
11193
84.2k
  zend_compile_expr(&const_node, const_ast);
11194
11195
84.2k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11196
11197
84.2k
  zend_set_class_name_op1(opline, &class_node);
11198
11199
84.2k
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11200
83.5k
    opline->extended_value = zend_alloc_cache_slots(2);
11201
83.5k
  }
11202
84.2k
}
11203
/* }}} */
11204
11205
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11206
5.71k
{
11207
5.71k
  zend_ast *class_ast = ast->child[0];
11208
11209
5.71k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11210
2.97k
    result->op_type = IS_CONST;
11211
2.97k
    return;
11212
2.97k
  }
11213
11214
2.73k
  if (class_ast->kind == ZEND_AST_ZVAL) {
11215
1.28k
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11216
1.28k
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11217
1.45k
  } else {
11218
1.45k
    znode expr_node;
11219
1.45k
    zend_compile_expr(&expr_node, class_ast);
11220
1.45k
    if (expr_node.op_type == IS_CONST) {
11221
      /* Unlikely case that happen if class_ast is constant folded.
11222
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11223
12
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11224
12
        zend_zval_value_name(&expr_node.u.constant));
11225
12
    }
11226
11227
1.44k
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11228
1.44k
  }
11229
2.73k
}
11230
/* }}} */
11231
11232
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11233
241k
{
11234
241k
  if (num == 0) {
11235
28.2k
    result->op_type = IS_TMP_VAR;
11236
28.2k
    result->u.op.var = -1;
11237
28.2k
    opline->opcode = ZEND_ROPE_INIT;
11238
213k
  } else {
11239
213k
    opline->opcode = ZEND_ROPE_ADD;
11240
213k
    SET_NODE(opline->op1, result);
11241
213k
  }
11242
241k
  SET_NODE(opline->op2, elem_node);
11243
241k
  SET_NODE(opline->result, result);
11244
241k
  opline->extended_value = num;
11245
241k
  return opline;
11246
241k
}
11247
/* }}} */
11248
11249
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11250
236k
{
11251
236k
  zend_op *opline = get_next_op();
11252
11253
236k
  if (num == 0) {
11254
12.9k
    result->op_type = IS_TMP_VAR;
11255
12.9k
    result->u.op.var = -1;
11256
12.9k
    opline->opcode = ZEND_ROPE_INIT;
11257
224k
  } else {
11258
224k
    opline->opcode = ZEND_ROPE_ADD;
11259
224k
    SET_NODE(opline->op1, result);
11260
224k
  }
11261
236k
  SET_NODE(opline->op2, elem_node);
11262
236k
  SET_NODE(opline->result, result);
11263
236k
  opline->extended_value = num;
11264
236k
  return opline;
11265
236k
}
11266
/* }}} */
11267
11268
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11269
41.1k
{
11270
41.1k
  if (rope_elements == 1) {
11271
2.73k
    if (opline->op2_type == IS_CONST) {
11272
571
      GET_NODE(result, opline->op2);
11273
571
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11274
571
      SET_UNUSED(opline->op2);
11275
571
      MAKE_NOP(opline);
11276
2.16k
    } else {
11277
2.16k
      opline->opcode = ZEND_CAST;
11278
2.16k
      opline->extended_value = IS_STRING;
11279
2.16k
      opline->op1_type = opline->op2_type;
11280
2.16k
      opline->op1 = opline->op2;
11281
2.16k
      SET_UNUSED(opline->op2);
11282
2.16k
      zend_make_tmp_result(result, opline);
11283
2.16k
    }
11284
38.4k
  } else if (rope_elements == 2) {
11285
7.09k
    opline->opcode = ZEND_FAST_CONCAT;
11286
7.09k
    opline->extended_value = 0;
11287
7.09k
    opline->op1_type = init_opline->op2_type;
11288
7.09k
    opline->op1 = init_opline->op2;
11289
7.09k
    zend_make_tmp_result(result, opline);
11290
7.09k
    MAKE_NOP(init_opline);
11291
31.3k
  } else {
11292
31.3k
    uint32_t var;
11293
11294
31.3k
    init_opline->extended_value = rope_elements;
11295
31.3k
    opline->opcode = ZEND_ROPE_END;
11296
31.3k
    zend_make_tmp_result(result, opline);
11297
31.3k
    var = opline->op1.var = get_temporary_variable();
11298
11299
    /* Allocates the necessary number of zval slots to keep the rope */
11300
31.3k
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11301
245k
    while (i > 1) {
11302
213k
      get_temporary_variable();
11303
213k
      i--;
11304
213k
    }
11305
11306
    /* Update all the previous opcodes to use the same variable */
11307
597k
    while (opline != init_opline) {
11308
565k
      opline--;
11309
565k
      if (opline->opcode == ZEND_ROPE_ADD &&
11310
401k
          opline->result.var == (uint32_t)-1) {
11311
399k
        opline->op1.var = var;
11312
399k
        opline->result.var = var;
11313
399k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11314
31.8k
                 opline->result.var == (uint32_t)-1) {
11315
31.3k
        opline->result.var = var;
11316
31.3k
      }
11317
565k
    }
11318
31.3k
  }
11319
41.1k
}
11320
11321
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11322
38.0k
{
11323
38.0k
  uint32_t i, j;
11324
38.0k
  uint32_t rope_init_lineno = -1;
11325
38.0k
  zend_op *opline = NULL, *init_opline;
11326
38.0k
  znode elem_node, last_const_node;
11327
38.0k
  zend_ast_list *list = zend_ast_get_list(ast);
11328
38.0k
  uint32_t reserved_op_number = -1;
11329
11330
38.0k
  ZEND_ASSERT(list->children > 0);
11331
11332
38.0k
  j = 0;
11333
38.0k
  last_const_node.op_type = IS_UNUSED;
11334
508k
  for (i = 0; i < list->children; i++) {
11335
470k
    zend_ast *encaps_var = list->child[i];
11336
11337
470k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11338
4.19k
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11339
771
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11340
3.42k
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11341
3.42k
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11342
3.42k
      }
11343
4.19k
    }
11344
11345
470k
    zend_compile_expr(&elem_node, encaps_var);
11346
11347
470k
    if (elem_node.op_type == IS_CONST) {
11348
242k
      convert_to_string(&elem_node.u.constant);
11349
11350
242k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11351
919
        zval_ptr_dtor(&elem_node.u.constant);
11352
241k
      } else if (last_const_node.op_type == IS_CONST) {
11353
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11354
0
        zval_ptr_dtor(&elem_node.u.constant);
11355
241k
      } else {
11356
241k
        last_const_node.op_type = IS_CONST;
11357
241k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11358
        /* Reserve place for ZEND_ROPE_ADD instruction */
11359
241k
        reserved_op_number = get_next_op_number();
11360
241k
        opline = get_next_op();
11361
241k
        opline->opcode = ZEND_NOP;
11362
241k
      }
11363
242k
      continue;
11364
242k
    } else {
11365
227k
      if (j == 0) {
11366
38.0k
        if (last_const_node.op_type == IS_CONST) {
11367
28.2k
          rope_init_lineno = reserved_op_number;
11368
28.2k
        } else {
11369
9.82k
          rope_init_lineno = get_next_op_number();
11370
9.82k
        }
11371
38.0k
      }
11372
227k
      if (last_const_node.op_type == IS_CONST) {
11373
208k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11374
208k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11375
208k
        last_const_node.op_type = IS_UNUSED;
11376
208k
      }
11377
227k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11378
227k
    }
11379
470k
  }
11380
11381
38.0k
  if (j == 0) {
11382
0
    result->op_type = IS_CONST;
11383
0
    if (last_const_node.op_type == IS_CONST) {
11384
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11385
0
    } else {
11386
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11387
      /* empty string */
11388
0
    }
11389
0
    CG(active_op_array)->last = reserved_op_number - 1;
11390
0
    return;
11391
38.0k
  } else if (last_const_node.op_type == IS_CONST) {
11392
32.9k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11393
32.9k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11394
32.9k
  }
11395
38.0k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11396
38.0k
  zend_compile_rope_finalize(result, j, init_opline, opline);
11397
38.0k
}
11398
/* }}} */
11399
11400
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11401
19.2k
{
11402
19.2k
  zend_op *opline;
11403
11404
19.2k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11405
19.2k
    result->op_type = IS_CONST;
11406
19.2k
    return;
11407
19.2k
  }
11408
11409
86
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11410
86
              CG(active_class_entry) &&
11411
86
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11412
11413
86
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11414
86
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11415
86
}
11416
/* }}} */
11417
11418
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11419
47.5k
{
11420
47.5k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11421
41.8k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11422
40.8k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11423
40.1k
    || kind == ZEND_AST_UNARY_OP
11424
39.1k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11425
38.2k
    || kind == ZEND_AST_CAST
11426
37.5k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11427
36.0k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11428
32.6k
    || kind == ZEND_AST_UNPACK
11429
32.6k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11430
7.16k
    || kind == ZEND_AST_CLASS_NAME
11431
7.09k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11432
6.55k
    || kind == ZEND_AST_CONST_ENUM_INIT
11433
4.04k
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11434
2.40k
    || kind == ZEND_AST_NAMED_ARG
11435
2.37k
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11436
1.65k
    || kind == ZEND_AST_CLOSURE
11437
1.54k
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11438
47.5k
}
11439
/* }}} */
11440
11441
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11442
3.47k
{
11443
3.47k
  zend_ast *ast = *ast_ptr;
11444
3.47k
  zend_ast *class_ast = ast->child[0];
11445
3.47k
  zend_string *class_name;
11446
3.47k
  int fetch_type;
11447
11448
3.47k
  if (class_ast->kind != ZEND_AST_ZVAL) {
11449
22
    zend_error_noreturn(E_COMPILE_ERROR,
11450
22
      "Dynamic class names are not allowed in compile-time class constant references");
11451
22
  }
11452
3.44k
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11453
11
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11454
11
  }
11455
11456
3.44k
  class_name = zend_ast_get_str(class_ast);
11457
3.44k
  fetch_type = zend_get_class_fetch_type(class_name);
11458
11459
3.44k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11460
7
    zend_error_noreturn(E_COMPILE_ERROR,
11461
7
      "\"static::\" is not allowed in compile-time constants");
11462
7
  }
11463
11464
3.44k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11465
2.57k
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11466
11467
2.57k
    zend_string_release_ex(class_name, 0);
11468
2.57k
    if (tmp != class_name) {
11469
702
      zval *zv = zend_ast_get_zval(class_ast);
11470
702
      ZVAL_STR(zv, tmp);
11471
702
      class_ast->attr = ZEND_NAME_FQ;
11472
702
    }
11473
2.57k
  }
11474
11475
3.44k
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11476
3.44k
}
11477
/* }}} */
11478
11479
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11480
71
{
11481
71
  zend_ast *ast = *ast_ptr;
11482
71
  zend_ast *class_ast = ast->child[0];
11483
71
  if (class_ast->kind != ZEND_AST_ZVAL) {
11484
7
    zend_error_noreturn(E_COMPILE_ERROR,
11485
7
      "(expression)::class cannot be used in constant expressions");
11486
7
  }
11487
11488
64
  zend_string *class_name = zend_ast_get_str(class_ast);
11489
64
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11490
11491
64
  switch (fetch_type) {
11492
56
    case ZEND_FETCH_CLASS_SELF:
11493
57
    case ZEND_FETCH_CLASS_PARENT:
11494
      /* For the const-eval representation store the fetch type instead of the name. */
11495
57
      zend_string_release(class_name);
11496
57
      ast->child[0] = NULL;
11497
57
      ast->attr = fetch_type;
11498
57
      return;
11499
7
    case ZEND_FETCH_CLASS_STATIC:
11500
7
      zend_error_noreturn(E_COMPILE_ERROR,
11501
7
        "static::class cannot be used for compile-time class name resolution");
11502
0
      return;
11503
64
    EMPTY_SWITCH_DEFAULT_CASE()
11504
64
  }
11505
64
}
11506
11507
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11508
21.9k
{
11509
21.9k
  zend_ast *ast = *ast_ptr;
11510
21.9k
  zend_ast *name_ast = ast->child[0];
11511
21.9k
  zend_string *orig_name = zend_ast_get_str(name_ast);
11512
21.9k
  bool is_fully_qualified;
11513
21.9k
  zval result;
11514
21.9k
  zend_string *resolved_name;
11515
11516
21.9k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11517
11518
21.9k
  resolved_name = zend_resolve_const_name(
11519
21.9k
    orig_name, name_ast->attr, &is_fully_qualified);
11520
11521
21.9k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11522
0
    zend_string_release_ex(resolved_name, 0);
11523
0
    zend_ast_destroy(ast);
11524
0
    *ast_ptr = zend_ast_create_zval(&result);
11525
0
    return;
11526
0
  }
11527
11528
21.9k
  zend_ast_destroy(ast);
11529
21.9k
  *ast_ptr = zend_ast_create_constant(resolved_name,
11530
21.9k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11531
21.9k
}
11532
/* }}} */
11533
11534
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11535
327
{
11536
327
  zend_ast *ast = *ast_ptr;
11537
11538
  /* Other cases already resolved by constant folding */
11539
327
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11540
11541
327
  zend_ast_destroy(ast);
11542
327
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11543
327
}
11544
/* }}} */
11545
11546
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11547
964
{
11548
964
  if (class_ast->kind == ZEND_AST_CLASS) {
11549
0
    zend_error_noreturn(E_COMPILE_ERROR,
11550
0
      "Cannot use anonymous class in constant expression");
11551
0
  }
11552
964
  if (class_ast->kind != ZEND_AST_ZVAL) {
11553
9
    zend_error_noreturn(E_COMPILE_ERROR,
11554
9
      "Cannot use dynamic class name in constant expression");
11555
9
  }
11556
11557
955
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11558
955
  int fetch_type = zend_get_class_fetch_type(class_name);
11559
955
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11560
6
    zend_error_noreturn(E_COMPILE_ERROR,
11561
6
      "\"static\" is not allowed in compile-time constants");
11562
6
  }
11563
11564
949
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11565
949
  zval_ptr_dtor_nogc(class_ast_zv);
11566
949
  ZVAL_STR(class_ast_zv, class_name);
11567
949
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11568
949
}
11569
11570
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11571
824
{
11572
824
  zend_ast *class_ast = (*ast_ptr)->child[0];
11573
824
  zend_compile_const_expr_class_reference(class_ast);
11574
11575
824
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11576
824
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11577
7
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11578
7
  }
11579
824
}
11580
11581
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11582
108
{
11583
108
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11584
108
  const zend_ast *uses_ast = closure_ast->child[1];
11585
108
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11586
11
    zend_error_noreturn(E_COMPILE_ERROR,
11587
11
      "Closures in constant expressions must be static");
11588
11
  }
11589
97
  if (uses_ast) {
11590
7
    zend_error_noreturn(E_COMPILE_ERROR,
11591
7
      "Cannot use(...) variables in constant expression");
11592
7
  }
11593
11594
90
  znode node;
11595
90
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11596
11597
90
  zend_ast_destroy(*ast_ptr);
11598
90
  *ast_ptr = zend_ast_create_op_array(op);
11599
90
}
11600
11601
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11602
776
{
11603
776
  zend_ast **args_ast;
11604
776
  switch ((*ast_ptr)->kind) {
11605
635
    case ZEND_AST_CALL:
11606
635
      args_ast = &(*ast_ptr)->child[1];
11607
635
      break;
11608
141
    case ZEND_AST_STATIC_CALL:
11609
141
      args_ast = &(*ast_ptr)->child[2];
11610
141
      break;
11611
0
    EMPTY_SWITCH_DEFAULT_CASE();
11612
776
  }
11613
776
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11614
20
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11615
20
  }
11616
756
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11617
11618
756
  switch ((*ast_ptr)->kind) {
11619
616
    case ZEND_AST_CALL: {
11620
616
      zend_ast *name_ast = (*ast_ptr)->child[0];
11621
616
      if (name_ast->kind != ZEND_AST_ZVAL) {
11622
19
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11623
19
      }
11624
597
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11625
597
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11626
7
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11627
7
      }
11628
597
      bool is_fully_qualified;
11629
590
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11630
590
      zval_ptr_dtor_nogc(name_ast_zv);
11631
590
      ZVAL_STR(name_ast_zv, name);
11632
590
      if (is_fully_qualified) {
11633
114
        name_ast->attr = ZEND_NAME_FQ;
11634
114
      }
11635
590
      break;
11636
597
    }
11637
140
    case ZEND_AST_STATIC_CALL: {
11638
140
      zend_ast *class_ast = (*ast_ptr)->child[0];
11639
140
      zend_compile_const_expr_class_reference(class_ast);
11640
140
      zend_ast *method_ast = (*ast_ptr)->child[1];
11641
140
      if (method_ast->kind != ZEND_AST_ZVAL) {
11642
1
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11643
1
      }
11644
139
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11645
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11646
0
      }
11647
139
      break;
11648
139
    }
11649
139
    EMPTY_SWITCH_DEFAULT_CASE();
11650
756
  }
11651
756
}
11652
11653
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11654
809
{
11655
809
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11656
809
  bool uses_named_args = false;
11657
976
  for (uint32_t i = 0; i < list->children; i++) {
11658
169
    const zend_ast *arg = list->child[i];
11659
169
    if (arg->kind == ZEND_AST_UNPACK) {
11660
1
      zend_error_noreturn(E_COMPILE_ERROR,
11661
1
        "Argument unpacking in constant expressions is not supported");
11662
1
    }
11663
168
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11664
28
      uses_named_args = true;
11665
140
    } else if (uses_named_args) {
11666
1
      zend_error_noreturn(E_COMPILE_ERROR,
11667
1
        "Cannot use positional argument after named argument");
11668
1
    }
11669
168
  }
11670
807
  if (uses_named_args) {
11671
26
    list->attr = 1;
11672
26
  }
11673
807
}
11674
11675
typedef struct {
11676
  /* Whether the value of this expression may differ on each evaluation. */
11677
  bool allow_dynamic;
11678
} const_expr_context;
11679
11680
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11681
238k
{
11682
238k
  const const_expr_context *ctx = context;
11683
238k
  zend_ast *ast = *ast_ptr;
11684
238k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11685
190k
    return;
11686
190k
  }
11687
11688
47.5k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11689
47
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11690
47
  }
11691
11692
47.5k
  switch (ast->kind) {
11693
3.47k
    case ZEND_AST_CLASS_CONST:
11694
3.47k
      zend_compile_const_expr_class_const(ast_ptr);
11695
3.47k
      break;
11696
71
    case ZEND_AST_CLASS_NAME:
11697
71
      zend_compile_const_expr_class_name(ast_ptr);
11698
71
      break;
11699
21.9k
    case ZEND_AST_CONST:
11700
21.9k
      zend_compile_const_expr_const(ast_ptr);
11701
21.9k
      break;
11702
327
    case ZEND_AST_MAGIC_CONST:
11703
327
      zend_compile_const_expr_magic_const(ast_ptr);
11704
327
      break;
11705
688
    case ZEND_AST_CAST:
11706
688
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11707
7
        zend_error_noreturn(E_COMPILE_ERROR,
11708
7
          "Object casts are not supported in this context");
11709
7
      }
11710
681
      break;
11711
830
    case ZEND_AST_NEW:
11712
830
      if (!ctx->allow_dynamic) {
11713
6
        zend_error_noreturn(E_COMPILE_ERROR,
11714
6
          "New expressions are not supported in this context");
11715
6
      }
11716
824
      zend_compile_const_expr_new(ast_ptr);
11717
824
      break;
11718
809
    case ZEND_AST_ARG_LIST:
11719
809
      zend_compile_const_expr_args(ast_ptr);
11720
809
      break;
11721
108
    case ZEND_AST_CLOSURE:
11722
108
      zend_compile_const_expr_closure(ast_ptr);
11723
      /* Return, because we do not want to traverse the children. */
11724
108
      return;
11725
635
    case ZEND_AST_CALL:
11726
776
    case ZEND_AST_STATIC_CALL:
11727
776
      zend_compile_const_expr_fcc(ast_ptr);
11728
776
      break;
11729
47.5k
  }
11730
11731
47.2k
  zend_ast_apply(ast, zend_compile_const_expr, context);
11732
47.2k
}
11733
/* }}} */
11734
11735
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11736
169k
{
11737
169k
  const_expr_context context;
11738
169k
  context.allow_dynamic = allow_dynamic;
11739
11740
169k
  zend_eval_const_expr(ast_ptr);
11741
169k
  zend_compile_const_expr(ast_ptr, &context);
11742
169k
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11743
    /* Replace with compiled AST zval representation. */
11744
22.4k
    zval ast_zv;
11745
22.4k
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11746
22.4k
    zend_ast_destroy(*ast_ptr);
11747
22.4k
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11748
22.4k
  }
11749
169k
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11750
169k
}
11751
/* }}} */
11752
11753
/* Same as compile_stmt, but with early binding */
11754
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11755
757k
{
11756
757k
  if (!ast) {
11757
105k
    return;
11758
105k
  }
11759
11760
652k
  if (ast->kind == ZEND_AST_STMT_LIST) {
11761
125k
    const zend_ast_list *list = zend_ast_get_list(ast);
11762
125k
    uint32_t i;
11763
780k
    for (i = 0; i < list->children; ++i) {
11764
655k
      zend_compile_top_stmt(list->child[i]);
11765
655k
    }
11766
125k
    return;
11767
125k
  }
11768
11769
527k
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11770
15.8k
    CG(zend_lineno) = ast->lineno;
11771
15.8k
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11772
15.8k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11773
511k
  } else if (ast->kind == ZEND_AST_CLASS) {
11774
45.3k
    CG(zend_lineno) = ast->lineno;
11775
45.3k
    zend_compile_class_decl(NULL, ast, true);
11776
45.3k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11777
466k
  } else {
11778
466k
    zend_compile_stmt(ast);
11779
466k
  }
11780
527k
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11781
515k
    zend_verify_namespace();
11782
515k
  }
11783
527k
}
11784
/* }}} */
11785
11786
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11787
8.57M
{
11788
8.57M
  if (!ast) {
11789
563k
    return;
11790
563k
  }
11791
11792
8.00M
  CG(zend_lineno) = ast->lineno;
11793
11794
8.00M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11795
0
    zend_do_extended_stmt(NULL);
11796
0
  }
11797
11798
8.00M
  switch (ast->kind) {
11799
1.88M
    case ZEND_AST_STMT_LIST:
11800
1.88M
      zend_compile_stmt_list(ast);
11801
1.88M
      break;
11802
2.17k
    case ZEND_AST_GLOBAL:
11803
2.17k
      zend_compile_global_var(ast);
11804
2.17k
      break;
11805
1.38k
    case ZEND_AST_STATIC:
11806
1.38k
      zend_compile_static_var(ast);
11807
1.38k
      break;
11808
7.90k
    case ZEND_AST_UNSET:
11809
7.90k
      zend_compile_unset(ast);
11810
7.90k
      break;
11811
45.0k
    case ZEND_AST_RETURN:
11812
45.0k
      zend_compile_return(ast);
11813
45.0k
      break;
11814
2.06M
    case ZEND_AST_ECHO:
11815
2.06M
      zend_compile_echo(ast);
11816
2.06M
      break;
11817
944
    case ZEND_AST_BREAK:
11818
1.68k
    case ZEND_AST_CONTINUE:
11819
1.68k
      zend_compile_break_continue(ast);
11820
1.68k
      break;
11821
1.32k
    case ZEND_AST_GOTO:
11822
1.32k
      zend_compile_goto(ast);
11823
1.32k
      break;
11824
5.47k
    case ZEND_AST_LABEL:
11825
5.47k
      zend_compile_label(ast);
11826
5.47k
      break;
11827
6.47k
    case ZEND_AST_WHILE:
11828
6.47k
      zend_compile_while(ast);
11829
6.47k
      break;
11830
1.11k
    case ZEND_AST_DO_WHILE:
11831
1.11k
      zend_compile_do_while(ast);
11832
1.11k
      break;
11833
11.8k
    case ZEND_AST_FOR:
11834
11.8k
      zend_compile_for(ast);
11835
11.8k
      break;
11836
21.6k
    case ZEND_AST_FOREACH:
11837
21.6k
      zend_compile_foreach(ast);
11838
21.6k
      break;
11839
25.1k
    case ZEND_AST_IF:
11840
25.1k
      zend_compile_if(ast);
11841
25.1k
      break;
11842
47.6k
    case ZEND_AST_SWITCH:
11843
47.6k
      zend_compile_switch(ast);
11844
47.6k
      break;
11845
48.8k
    case ZEND_AST_TRY:
11846
48.8k
      zend_compile_try(ast);
11847
48.8k
      break;
11848
6.89k
    case ZEND_AST_DECLARE:
11849
6.89k
      zend_compile_declare(ast);
11850
6.89k
      break;
11851
8.49k
    case ZEND_AST_FUNC_DECL:
11852
48.3k
    case ZEND_AST_METHOD:
11853
48.3k
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11854
48.3k
      break;
11855
2.53k
    case ZEND_AST_ENUM_CASE:
11856
2.53k
      zend_compile_enum_case(ast);
11857
2.53k
      break;
11858
45.3k
    case ZEND_AST_PROP_GROUP:
11859
45.3k
      zend_compile_prop_group(ast);
11860
45.3k
      break;
11861
14.5k
    case ZEND_AST_CLASS_CONST_GROUP:
11862
14.5k
      zend_compile_class_const_group(ast);
11863
14.5k
      break;
11864
22.4k
    case ZEND_AST_USE_TRAIT:
11865
22.4k
      zend_compile_use_trait(ast);
11866
22.4k
      break;
11867
86.3k
    case ZEND_AST_CLASS:
11868
86.3k
      zend_compile_class_decl(NULL, ast, false);
11869
86.3k
      break;
11870
231
    case ZEND_AST_GROUP_USE:
11871
231
      zend_compile_group_use(ast);
11872
231
      break;
11873
1.55k
    case ZEND_AST_USE:
11874
1.55k
      zend_compile_use(ast);
11875
1.55k
      break;
11876
4.74k
    case ZEND_AST_CONST_DECL:
11877
4.74k
      zend_compile_const_decl(ast);
11878
4.74k
      break;
11879
5.77k
    case ZEND_AST_NAMESPACE:
11880
5.77k
      zend_compile_namespace(ast);
11881
5.77k
      break;
11882
61
    case ZEND_AST_HALT_COMPILER:
11883
61
      zend_compile_halt_compiler(ast);
11884
61
      break;
11885
3.01k
    case ZEND_AST_THROW:
11886
3.01k
      zend_compile_expr(NULL, ast);
11887
3.01k
      break;
11888
729
    case ZEND_AST_CAST_VOID:
11889
729
      zend_compile_void_cast(NULL, ast);
11890
729
      break;
11891
3.59M
    default:
11892
3.59M
    {
11893
3.59M
      znode result;
11894
3.59M
      zend_compile_expr(&result, ast);
11895
3.59M
      zend_do_free(&result);
11896
3.59M
    }
11897
8.00M
  }
11898
11899
7.99M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11900
58.7k
    zend_emit_tick();
11901
58.7k
  }
11902
7.99M
}
11903
/* }}} */
11904
11905
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11906
40.7M
{
11907
  /* CG(zend_lineno) = ast->lineno; */
11908
40.7M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11909
11910
40.7M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11911
197k
    zend_compile_memoized_expr(result, ast);
11912
197k
    return;
11913
197k
  }
11914
11915
40.5M
  switch (ast->kind) {
11916
3.63M
    case ZEND_AST_ZVAL:
11917
3.63M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11918
3.63M
      result->op_type = IS_CONST;
11919
3.63M
      return;
11920
436k
    case ZEND_AST_ZNODE:
11921
436k
      *result = *zend_ast_get_znode(ast);
11922
436k
      return;
11923
512k
    case ZEND_AST_VAR:
11924
547k
    case ZEND_AST_DIM:
11925
572k
    case ZEND_AST_PROP:
11926
619k
    case ZEND_AST_NULLSAFE_PROP:
11927
627k
    case ZEND_AST_STATIC_PROP:
11928
4.29M
    case ZEND_AST_CALL:
11929
4.33M
    case ZEND_AST_METHOD_CALL:
11930
4.34M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11931
4.39M
    case ZEND_AST_STATIC_CALL:
11932
4.39M
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11933
4.39M
      zend_compile_var(result, ast, BP_VAR_R, false);
11934
4.39M
      return;
11935
262k
    case ZEND_AST_ASSIGN:
11936
262k
      zend_compile_assign(result, ast);
11937
262k
      return;
11938
7.87k
    case ZEND_AST_ASSIGN_REF:
11939
7.87k
      zend_compile_assign_ref(result, ast);
11940
7.87k
      return;
11941
82.6k
    case ZEND_AST_NEW:
11942
82.6k
      zend_compile_new(result, ast);
11943
82.6k
      return;
11944
169k
    case ZEND_AST_ASSIGN_OP:
11945
169k
      zend_compile_compound_assign(result, ast);
11946
169k
      return;
11947
2.30M
    case ZEND_AST_BINARY_OP:
11948
2.30M
      zend_compile_binary_op(result, ast);
11949
2.30M
      return;
11950
399k
    case ZEND_AST_GREATER:
11951
487k
    case ZEND_AST_GREATER_EQUAL:
11952
487k
      zend_compile_greater(result, ast);
11953
487k
      return;
11954
1.36M
    case ZEND_AST_UNARY_OP:
11955
1.36M
      zend_compile_unary_op(result, ast);
11956
1.36M
      return;
11957
17.2k
    case ZEND_AST_UNARY_PLUS:
11958
58.1k
    case ZEND_AST_UNARY_MINUS:
11959
58.1k
      zend_compile_unary_pm(result, ast);
11960
58.1k
      return;
11961
11.4k
    case ZEND_AST_AND:
11962
17.5k
    case ZEND_AST_OR:
11963
17.5k
      zend_compile_short_circuiting(result, ast);
11964
17.5k
      return;
11965
6.68k
    case ZEND_AST_POST_INC:
11966
11.1k
    case ZEND_AST_POST_DEC:
11967
11.1k
      zend_compile_post_incdec(result, ast);
11968
11.1k
      return;
11969
2.17k
    case ZEND_AST_PRE_INC:
11970
3.72k
    case ZEND_AST_PRE_DEC:
11971
3.72k
      zend_compile_pre_incdec(result, ast);
11972
3.72k
      return;
11973
3.78k
    case ZEND_AST_CAST:
11974
3.78k
      zend_compile_cast(result, ast);
11975
3.78k
      return;
11976
12.5k
    case ZEND_AST_CONDITIONAL:
11977
12.5k
      zend_compile_conditional(result, ast);
11978
12.5k
      return;
11979
1.80M
    case ZEND_AST_COALESCE:
11980
1.80M
      zend_compile_coalesce(result, ast);
11981
1.80M
      return;
11982
18.5k
    case ZEND_AST_ASSIGN_COALESCE:
11983
18.5k
      zend_compile_assign_coalesce(result, ast);
11984
18.5k
      return;
11985
4.43k
    case ZEND_AST_PRINT:
11986
4.43k
      zend_compile_print(result, ast);
11987
4.43k
      return;
11988
18.8k
    case ZEND_AST_YIELD:
11989
18.8k
      zend_compile_yield(result, ast);
11990
18.8k
      return;
11991
1.62k
    case ZEND_AST_YIELD_FROM:
11992
1.62k
      zend_compile_yield_from(result, ast);
11993
1.62k
      return;
11994
935
    case ZEND_AST_INSTANCEOF:
11995
935
      zend_compile_instanceof(result, ast);
11996
935
      return;
11997
9.28k
    case ZEND_AST_INCLUDE_OR_EVAL:
11998
9.28k
      zend_compile_include_or_eval(result, ast);
11999
9.28k
      return;
12000
9.52k
    case ZEND_AST_ISSET:
12001
12.2k
    case ZEND_AST_EMPTY:
12002
12.2k
      zend_compile_isset_or_empty(result, ast);
12003
12.2k
      return;
12004
14.7M
    case ZEND_AST_SILENCE:
12005
14.7M
      zend_compile_silence(result, ast);
12006
14.7M
      return;
12007
22.0k
    case ZEND_AST_SHELL_EXEC:
12008
22.0k
      zend_compile_shell_exec(result, ast);
12009
22.0k
      return;
12010
126k
    case ZEND_AST_ARRAY:
12011
126k
      zend_compile_array(result, ast);
12012
126k
      return;
12013
8.73M
    case ZEND_AST_CONST:
12014
8.73M
      zend_compile_const(result, ast);
12015
8.73M
      return;
12016
84.4k
    case ZEND_AST_CLASS_CONST:
12017
84.4k
      zend_compile_class_const(result, ast);
12018
84.4k
      return;
12019
5.71k
    case ZEND_AST_CLASS_NAME:
12020
5.71k
      zend_compile_class_name(result, ast);
12021
5.71k
      return;
12022
38.0k
    case ZEND_AST_ENCAPS_LIST:
12023
38.0k
      zend_compile_encaps_list(result, ast);
12024
38.0k
      return;
12025
19.2k
    case ZEND_AST_MAGIC_CONST:
12026
19.2k
      zend_compile_magic_const(result, ast);
12027
19.2k
      return;
12028
1.33M
    case ZEND_AST_CLOSURE:
12029
1.36M
    case ZEND_AST_ARROW_FUNC:
12030
1.36M
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12031
1.36M
      return;
12032
3.86k
    case ZEND_AST_THROW:
12033
3.86k
      zend_compile_throw(result, ast);
12034
3.86k
      return;
12035
3.25k
    case ZEND_AST_MATCH:
12036
3.25k
      zend_compile_match(result, ast);
12037
3.25k
      return;
12038
214k
    case ZEND_AST_PIPE:
12039
214k
      zend_compile_pipe(result, ast);
12040
214k
      return;
12041
0
    default:
12042
0
      ZEND_ASSERT(0 /* not supported */);
12043
40.5M
  }
12044
40.5M
}
12045
/* }}} */
12046
12047
static void zend_compile_expr(znode *result, zend_ast *ast)
12048
40.7M
{
12049
40.7M
  zend_check_stack_limit();
12050
12051
40.7M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12052
40.7M
  zend_compile_expr_inner(result, ast);
12053
40.7M
  zend_short_circuiting_commit(checkpoint, result, ast);
12054
40.7M
}
12055
12056
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12057
6.58M
{
12058
6.58M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12059
12060
6.58M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12061
40.5k
    switch (ast->kind) {
12062
1.06k
      case ZEND_AST_CALL:
12063
1.79k
      case ZEND_AST_METHOD_CALL:
12064
1.79k
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12065
3.54k
      case ZEND_AST_STATIC_CALL:
12066
3.54k
        zend_compile_memoized_expr(result, ast);
12067
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12068
3.54k
        return NULL;
12069
40.5k
    }
12070
40.5k
  }
12071
12072
6.57M
  switch (ast->kind) {
12073
616k
    case ZEND_AST_VAR:
12074
616k
      return zend_compile_simple_var(result, ast, type, false);
12075
81.8k
    case ZEND_AST_DIM:
12076
81.8k
      return zend_compile_dim(result, ast, type, by_ref);
12077
33.5k
    case ZEND_AST_PROP:
12078
81.2k
    case ZEND_AST_NULLSAFE_PROP:
12079
81.2k
      return zend_compile_prop(result, ast, type, by_ref);
12080
11.5k
    case ZEND_AST_STATIC_PROP:
12081
11.5k
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12082
3.82M
    case ZEND_AST_CALL:
12083
3.82M
      zend_compile_call(result, ast, type);
12084
3.82M
      return NULL;
12085
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12086
0
      zend_compile_parent_property_hook_call(result, ast, type);
12087
0
      return NULL;
12088
57.2k
    case ZEND_AST_METHOD_CALL:
12089
63.5k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12090
63.5k
      zend_compile_method_call(result, ast, type);
12091
63.5k
      return NULL;
12092
54.2k
    case ZEND_AST_STATIC_CALL:
12093
54.2k
      zend_compile_static_call(result, ast, type);
12094
54.2k
      return NULL;
12095
2.61k
    case ZEND_AST_ZNODE:
12096
2.61k
      *result = *zend_ast_get_znode(ast);
12097
2.61k
      return NULL;
12098
1.84M
    default:
12099
1.84M
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12100
226
        zend_error_noreturn(E_COMPILE_ERROR,
12101
226
          "Cannot use temporary expression in write context");
12102
226
      }
12103
12104
1.83M
      zend_compile_expr(result, ast);
12105
1.83M
      return NULL;
12106
6.57M
  }
12107
6.57M
}
12108
12109
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12110
6.58M
{
12111
6.58M
  zend_check_stack_limit();
12112
12113
6.58M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12114
6.58M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12115
6.58M
  zend_short_circuiting_commit(checkpoint, result, ast);
12116
6.58M
  return opcode;
12117
6.58M
}
12118
12119
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12120
787k
{
12121
787k
  zend_check_stack_limit();
12122
12123
787k
  switch (ast->kind) {
12124
535k
    case ZEND_AST_VAR:
12125
535k
      return zend_compile_simple_var(result, ast, type, true);
12126
168k
    case ZEND_AST_DIM:
12127
168k
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12128
18.6k
    case ZEND_AST_PROP:
12129
22.3k
    case ZEND_AST_NULLSAFE_PROP:
12130
22.3k
    {
12131
22.3k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12132
22.3k
      if (by_ref) {
12133
860
        opline->extended_value |= ZEND_FETCH_REF;
12134
860
      }
12135
22.3k
      return opline;
12136
18.6k
    }
12137
7.27k
    case ZEND_AST_STATIC_PROP:
12138
7.27k
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12139
53.6k
    default:
12140
53.6k
      return zend_compile_var(result, ast, type, false);
12141
787k
  }
12142
787k
}
12143
/* }}} */
12144
12145
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12146
16.6k
{
12147
  /* NAN warns when casting */
12148
16.6k
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12149
0
    return false;
12150
0
  }
12151
16.6k
  switch (type) {
12152
98
    case _IS_BOOL:
12153
98
      ZVAL_BOOL(result, zend_is_true(op1));
12154
98
      return true;
12155
305
    case IS_LONG:
12156
305
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12157
8
        return false;
12158
8
      }
12159
297
      ZVAL_LONG(result, zval_get_long(op1));
12160
297
      return true;
12161
1.07k
    case IS_DOUBLE:
12162
1.07k
      ZVAL_DOUBLE(result, zval_get_double(op1));
12163
1.07k
      return true;
12164
14.2k
    case IS_STRING:
12165
      /* Conversion from double to string takes into account run-time
12166
         'precision' setting and cannot be evaluated at compile-time */
12167
14.2k
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12168
11.9k
        ZVAL_STR(result, zval_get_string(op1));
12169
11.9k
        return true;
12170
11.9k
      }
12171
2.28k
      break;
12172
2.28k
    case IS_ARRAY:
12173
227
      ZVAL_COPY(result, op1);
12174
227
      convert_to_array(result);
12175
227
      return true;
12176
16.6k
  }
12177
2.93k
  return false;
12178
16.6k
}
12179
12180
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12181
10.1M
{
12182
10.1M
  zend_ast *ast = *ast_ptr;
12183
10.1M
  zval result;
12184
12185
10.1M
  if (!ast) {
12186
2.99M
    return;
12187
2.99M
  }
12188
12189
7.13M
  zend_check_stack_limit();
12190
12191
7.13M
  switch (ast->kind) {
12192
137k
    case ZEND_AST_BINARY_OP:
12193
137k
      zend_eval_const_expr(&ast->child[0]);
12194
137k
      zend_eval_const_expr(&ast->child[1]);
12195
137k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12196
102k
        return;
12197
102k
      }
12198
12199
34.9k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12200
34.9k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12201
34.9k
      ) {
12202
16.7k
        return;
12203
16.7k
      }
12204
18.2k
      break;
12205
18.2k
    case ZEND_AST_GREATER:
12206
6.47k
    case ZEND_AST_GREATER_EQUAL:
12207
6.47k
      zend_eval_const_expr(&ast->child[0]);
12208
6.47k
      zend_eval_const_expr(&ast->child[1]);
12209
6.47k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12210
5.06k
        return;
12211
5.06k
      }
12212
12213
1.41k
      zend_ct_eval_greater(&result, ast->kind,
12214
1.41k
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12215
1.41k
      break;
12216
1.84k
    case ZEND_AST_AND:
12217
4.30k
    case ZEND_AST_OR:
12218
4.30k
    {
12219
4.30k
      bool child0_is_true, child1_is_true;
12220
4.30k
      zend_eval_const_expr(&ast->child[0]);
12221
4.30k
      zend_eval_const_expr(&ast->child[1]);
12222
4.30k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12223
1.58k
        return;
12224
1.58k
      }
12225
12226
2.72k
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12227
2.72k
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12228
278
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12229
278
        break;
12230
278
      }
12231
12232
2.45k
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12233
236
        return;
12234
236
      }
12235
12236
2.21k
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12237
2.21k
      if (ast->kind == ZEND_AST_OR) {
12238
886
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12239
1.32k
      } else {
12240
1.32k
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12241
1.32k
      }
12242
2.21k
      break;
12243
2.45k
    }
12244
7.50k
    case ZEND_AST_UNARY_OP:
12245
7.50k
      zend_eval_const_expr(&ast->child[0]);
12246
7.50k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12247
3.12k
        return;
12248
3.12k
      }
12249
12250
4.38k
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12251
401
        return;
12252
401
      }
12253
3.98k
      break;
12254
3.98k
    case ZEND_AST_UNARY_PLUS:
12255
27.0k
    case ZEND_AST_UNARY_MINUS:
12256
27.0k
      zend_eval_const_expr(&ast->child[0]);
12257
27.0k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12258
6.71k
        return;
12259
6.71k
      }
12260
12261
20.3k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12262
6.50k
        return;
12263
6.50k
      }
12264
13.8k
      break;
12265
43.6k
    case ZEND_AST_COALESCE:
12266
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12267
43.6k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12268
304
        ast->child[0]->attr |= ZEND_DIM_IS;
12269
304
      }
12270
43.6k
      zend_eval_const_expr(&ast->child[0]);
12271
12272
43.6k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12273
        /* ensure everything was compile-time evaluated at least once */
12274
43.5k
        zend_eval_const_expr(&ast->child[1]);
12275
43.5k
        return;
12276
43.5k
      }
12277
12278
162
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12279
100
        zend_eval_const_expr(&ast->child[1]);
12280
100
        *ast_ptr = ast->child[1];
12281
100
        ast->child[1] = NULL;
12282
100
        zend_ast_destroy(ast);
12283
100
      } else {
12284
62
        *ast_ptr = ast->child[0];
12285
62
        ast->child[0] = NULL;
12286
62
        zend_ast_destroy(ast);
12287
62
      }
12288
162
      return;
12289
3.19k
    case ZEND_AST_CONDITIONAL:
12290
3.19k
    {
12291
3.19k
      zend_ast **child, *child_ast;
12292
3.19k
      zend_eval_const_expr(&ast->child[0]);
12293
3.19k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12294
        /* ensure everything was compile-time evaluated at least once */
12295
3.05k
        if (ast->child[1]) {
12296
2.33k
          zend_eval_const_expr(&ast->child[1]);
12297
2.33k
        }
12298
3.05k
        zend_eval_const_expr(&ast->child[2]);
12299
3.05k
        return;
12300
3.05k
      }
12301
12302
144
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12303
144
      if (*child == NULL) {
12304
79
        child--;
12305
79
      }
12306
144
      child_ast = *child;
12307
144
      *child = NULL;
12308
144
      zend_ast_destroy(ast);
12309
144
      *ast_ptr = child_ast;
12310
144
      zend_eval_const_expr(ast_ptr);
12311
144
      return;
12312
3.19k
    }
12313
1.21M
    case ZEND_AST_DIM:
12314
1.21M
    {
12315
      /* constant expression should be always read context ... */
12316
1.21M
      const zval *container, *dim;
12317
12318
1.21M
      if (ast->child[1] == NULL) {
12319
53
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12320
53
      }
12321
12322
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12323
1.21M
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12324
453
        ast->child[0]->attr |= ZEND_DIM_IS;
12325
453
      }
12326
12327
1.21M
      zend_eval_const_expr(&ast->child[0]);
12328
1.21M
      zend_eval_const_expr(&ast->child[1]);
12329
1.21M
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12330
1.19M
        return;
12331
1.19M
      }
12332
12333
12.6k
      container = zend_ast_get_zval(ast->child[0]);
12334
12.6k
      dim = zend_ast_get_zval(ast->child[1]);
12335
12336
12.6k
      if (Z_TYPE_P(container) == IS_ARRAY) {
12337
10.4k
        zval *el;
12338
10.4k
        if (Z_TYPE_P(dim) == IS_LONG) {
12339
966
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12340
966
          if (el) {
12341
645
            ZVAL_COPY(&result, el);
12342
645
          } else {
12343
321
            return;
12344
321
          }
12345
9.46k
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12346
8.48k
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12347
8.48k
          if (el) {
12348
316
            ZVAL_COPY(&result, el);
12349
8.16k
          } else {
12350
8.16k
            return;
12351
8.16k
          }
12352
8.48k
        } else {
12353
989
          return; /* warning... handle at runtime */
12354
989
        }
12355
10.4k
      } else if (Z_TYPE_P(container) == IS_STRING) {
12356
1.93k
        zend_long offset;
12357
1.93k
        uint8_t c;
12358
1.93k
        if (Z_TYPE_P(dim) == IS_LONG) {
12359
1.23k
          offset = Z_LVAL_P(dim);
12360
1.23k
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12361
458
          return;
12362
458
        }
12363
1.47k
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12364
1.26k
          return;
12365
1.26k
        }
12366
208
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12367
208
        ZVAL_CHAR(&result, c);
12368
295
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12369
12
        return; /* warning... handle at runtime */
12370
283
      } else {
12371
283
        return;
12372
283
      }
12373
1.16k
      break;
12374
12.6k
    }
12375
2.60M
    case ZEND_AST_ARRAY:
12376
2.60M
      if (!zend_try_ct_eval_array(&result, ast)) {
12377
2.55M
        return;
12378
2.55M
      }
12379
44.3k
      break;
12380
44.3k
    case ZEND_AST_MAGIC_CONST:
12381
1.59k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12382
327
        return;
12383
327
      }
12384
1.27k
      break;
12385
111k
    case ZEND_AST_CONST:
12386
111k
    {
12387
111k
      zend_ast *name_ast = ast->child[0];
12388
111k
      bool is_fully_qualified;
12389
111k
      zend_string *resolved_name = zend_resolve_const_name(
12390
111k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12391
12392
111k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12393
105k
        zend_string_release_ex(resolved_name, 0);
12394
105k
        return;
12395
105k
      }
12396
12397
5.48k
      zend_string_release_ex(resolved_name, 0);
12398
5.48k
      break;
12399
111k
    }
12400
268k
    case ZEND_AST_CLASS_CONST:
12401
268k
    {
12402
268k
      zend_ast *class_ast;
12403
268k
      zend_ast *name_ast;
12404
268k
      zend_string *resolved_name;
12405
12406
268k
      zend_eval_const_expr(&ast->child[0]);
12407
268k
      zend_eval_const_expr(&ast->child[1]);
12408
12409
268k
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12410
268k
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12411
1.33k
        return;
12412
1.33k
      }
12413
12414
266k
      class_ast = ast->child[0];
12415
266k
      name_ast = ast->child[1];
12416
12417
266k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12418
253k
        return;
12419
253k
      }
12420
12421
12.7k
      resolved_name = zend_resolve_class_name_ast(class_ast);
12422
12.7k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12423
12.4k
        zend_string_release_ex(resolved_name, 0);
12424
12.4k
        return;
12425
12.4k
      }
12426
12427
248
      zend_string_release_ex(resolved_name, 0);
12428
248
      break;
12429
12.7k
    }
12430
3.37k
    case ZEND_AST_CLASS_NAME:
12431
3.37k
    {
12432
3.37k
      zend_ast *class_ast = ast->child[0];
12433
3.37k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12434
1.78k
        return;
12435
1.78k
      }
12436
1.59k
      break;
12437
3.37k
    }
12438
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12439
    // special handling. It is required that all nodes that are part of a const expr
12440
    // are visited. Probably we should be distinguishing evaluation of const expr and
12441
    // normal exprs here.
12442
3.51k
    case ZEND_AST_ARG_LIST:
12443
3.51k
    {
12444
3.51k
      zend_ast_list *list = zend_ast_get_list(ast);
12445
4.32k
      for (uint32_t i = 0; i < list->children; i++) {
12446
806
        zend_eval_const_expr(&list->child[i]);
12447
806
      }
12448
3.51k
      return;
12449
3.37k
    }
12450
3.52k
    case ZEND_AST_NEW:
12451
3.52k
      zend_eval_const_expr(&ast->child[0]);
12452
3.52k
      zend_eval_const_expr(&ast->child[1]);
12453
3.52k
      return;
12454
96
    case ZEND_AST_NAMED_ARG:
12455
96
      zend_eval_const_expr(&ast->child[1]);
12456
96
      return;
12457
2.51k
    case ZEND_AST_CONST_ENUM_INIT:
12458
2.51k
      zend_eval_const_expr(&ast->child[2]);
12459
2.51k
      return;
12460
866
    case ZEND_AST_PROP:
12461
1.41k
    case ZEND_AST_NULLSAFE_PROP:
12462
1.41k
      zend_eval_const_expr(&ast->child[0]);
12463
1.41k
      zend_eval_const_expr(&ast->child[1]);
12464
1.41k
      return;
12465
29.0k
    case ZEND_AST_CAST:
12466
29.0k
      zend_eval_const_expr(&ast->child[0]);
12467
29.0k
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12468
3.55k
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12469
1.60k
        break;
12470
1.60k
      }
12471
27.4k
      return;
12472
2.67M
    default:
12473
2.67M
      return;
12474
7.13M
  }
12475
12476
95.0k
  zend_ast_destroy(ast);
12477
95.0k
  *ast_ptr = zend_ast_create_zval(&result);
12478
95.0k
}
12479
/* }}} */