Coverage Report

Created: 2022-02-19 20:27

/src/php-src/Zend/zend_compile.c
Line
Count
Source (jump to first uncovered line)
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_attributes.h"
24
#include "zend_compile.h"
25
#include "zend_constants.h"
26
#include "zend_llist.h"
27
#include "zend_API.h"
28
#include "zend_exceptions.h"
29
#include "zend_interfaces.h"
30
#include "zend_virtual_cwd.h"
31
#include "zend_multibyte.h"
32
#include "zend_language_scanner.h"
33
#include "zend_inheritance.h"
34
#include "zend_vm.h"
35
36
16.9M
#define SET_NODE(target, src) do { \
37
16.9M
    target ## _type = (src)->op_type; \
38
16.9M
    if ((src)->op_type == IS_CONST) { \
39
6.73M
      target.constant = zend_add_literal(&(src)->u.constant); \
40
10.2M
    } else { \
41
10.2M
      target = (src)->u.op; \
42
10.2M
    } \
43
16.9M
  } while (0)
44
45
7.56M
#define GET_NODE(target, src) do { \
46
7.56M
    (target)->op_type = src ## _type; \
47
7.56M
    if ((target)->op_type == IS_CONST) { \
48
0
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
49
7.56M
    } else { \
50
7.56M
      (target)->u.op = src; \
51
7.56M
    } \
52
7.56M
  } while (0)
53
54
34.8M
#define FC(member) (CG(file_context).member)
55
56
typedef struct _zend_loop_var {
57
  zend_uchar opcode;
58
  zend_uchar var_type;
59
  uint32_t   var_num;
60
  uint32_t   try_catch_offset;
61
} zend_loop_var;
62
63
4.29M
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
64
4.29M
  if (count == 0) {
65
    /* Even if no cache slots are desired, the VM handler may still want to acquire
66
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
67
     * calculation is still legal and ubsan does not complain. */
68
66.1k
    return 0;
69
66.1k
  }
70
71
4.22M
  zend_op_array *op_array = CG(active_op_array);
72
4.22M
  uint32_t ret = op_array->cache_size;
73
4.22M
  op_array->cache_size += count * sizeof(void*);
74
4.22M
  return ret;
75
4.22M
}
76
77
3.29M
static inline uint32_t zend_alloc_cache_slot(void) {
78
3.29M
  return zend_alloc_cache_slots(1);
79
3.29M
}
80
81
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
82
ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename);
83
84
#ifndef ZTS
85
ZEND_API zend_compiler_globals compiler_globals;
86
ZEND_API zend_executor_globals executor_globals;
87
#endif
88
89
static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
90
static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
91
92
static void init_op(zend_op *op)
93
19.1M
{
94
19.1M
  MAKE_NOP(op);
95
19.1M
  op->extended_value = 0;
96
19.1M
  op->lineno = CG(zend_lineno);
97
19.1M
}
98
99
static zend_always_inline uint32_t get_next_op_number(void)
100
6.42M
{
101
6.42M
  return CG(active_op_array)->last;
102
6.42M
}
103
104
static zend_op *get_next_op(void)
105
18.3M
{
106
18.3M
  zend_op_array *op_array = CG(active_op_array);
107
18.3M
  uint32_t next_op_num = op_array->last++;
108
18.3M
  zend_op *next_op;
109
110
18.3M
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
111
45.6k
    CG(context).opcodes_size *= 4;
112
45.6k
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
113
45.6k
  }
114
115
18.3M
  next_op = &(op_array->opcodes[next_op_num]);
116
117
18.3M
  init_op(next_op);
118
119
18.3M
  return next_op;
120
18.3M
}
121
122
static zend_brk_cont_element *get_next_brk_cont_element(void)
123
115k
{
124
115k
  CG(context).last_brk_cont++;
125
115k
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
126
115k
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
127
115k
}
128
129
static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
130
9.78k
{
131
9.78k
  zend_property_info *property_info = Z_PTR_P(zv);
132
133
9.78k
  zend_string_release(property_info->name);
134
9.78k
  zend_type_release(property_info->type, /* persistent */ 1);
135
9.78k
  free(property_info);
136
9.78k
}
137
/* }}} */
138
139
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
140
91.4k
{
141
91.4k
  zend_string *filename = CG(active_op_array)->filename;
142
91.4k
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
143
91.4k
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
144
91.4k
  return zend_new_interned_string(result);
145
91.4k
}
146
/* }}} */
147
148
static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
149
618k
{
150
618k
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
151
618k
  if (ns_separator != NULL) {
152
24.3k
    *result = ns_separator + 1;
153
24.3k
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
154
24.3k
    return 1;
155
24.3k
  }
156
157
593k
  return 0;
158
593k
}
159
/* }}} */
160
161
struct reserved_class_name {
162
  const char *name;
163
  size_t len;
164
};
165
static const struct reserved_class_name reserved_class_names[] = {
166
  {ZEND_STRL("bool")},
167
  {ZEND_STRL("false")},
168
  {ZEND_STRL("float")},
169
  {ZEND_STRL("int")},
170
  {ZEND_STRL("null")},
171
  {ZEND_STRL("parent")},
172
  {ZEND_STRL("self")},
173
  {ZEND_STRL("static")},
174
  {ZEND_STRL("string")},
175
  {ZEND_STRL("true")},
176
  {ZEND_STRL("void")},
177
  {ZEND_STRL("iterable")},
178
  {ZEND_STRL("object")},
179
  {ZEND_STRL("mixed")},
180
  {NULL, 0}
181
};
182
183
static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
184
219k
{
185
219k
  const struct reserved_class_name *reserved = reserved_class_names;
186
187
219k
  const char *uqname = ZSTR_VAL(name);
188
219k
  size_t uqname_len = ZSTR_LEN(name);
189
219k
  zend_get_unqualified_name(name, &uqname, &uqname_len);
190
191
3.29M
  for (; reserved->name; ++reserved) {
192
3.07M
    if (uqname_len == reserved->len
193
241k
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
194
361
    ) {
195
361
      return 1;
196
361
    }
197
3.07M
  }
198
199
219k
  return 0;
200
219k
}
201
/* }}} */
202
203
void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
204
215k
{
205
215k
  if (zend_is_reserved_class_name(name)) {
206
266
    zend_error_noreturn(E_COMPILE_ERROR,
207
266
      "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
208
266
  }
209
215k
}
210
/* }}} */
211
212
typedef struct _builtin_type_info {
213
  const char* name;
214
  const size_t name_len;
215
  const zend_uchar type;
216
} builtin_type_info;
217
218
static const builtin_type_info builtin_types[] = {
219
  {ZEND_STRL("null"), IS_NULL},
220
  {ZEND_STRL("false"), IS_FALSE},
221
  {ZEND_STRL("int"), IS_LONG},
222
  {ZEND_STRL("float"), IS_DOUBLE},
223
  {ZEND_STRL("string"), IS_STRING},
224
  {ZEND_STRL("bool"), _IS_BOOL},
225
  {ZEND_STRL("void"), IS_VOID},
226
  {ZEND_STRL("iterable"), IS_ITERABLE},
227
  {ZEND_STRL("object"), IS_OBJECT},
228
  {ZEND_STRL("mixed"), IS_MIXED},
229
  {NULL, 0, IS_UNDEF}
230
};
231
232
typedef struct {
233
  const char *name;
234
  size_t name_len;
235
  const char *correct_name;
236
} confusable_type_info;
237
238
static const confusable_type_info confusable_types[] = {
239
  {ZEND_STRL("boolean"), "bool"},
240
  {ZEND_STRL("integer"), "int"},
241
  {ZEND_STRL("double"), "float"},
242
  {ZEND_STRL("resource"), NULL},
243
  {NULL, 0, NULL},
244
};
245
246
static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
247
148k
{
248
148k
  const builtin_type_info *info = &builtin_types[0];
249
250
1.06M
  for (; info->name; ++info) {
251
1.00M
    if (ZSTR_LEN(name) == info->name_len
252
158k
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
253
92.7k
    ) {
254
92.7k
      return info->type;
255
92.7k
    }
256
1.00M
  }
257
258
55.3k
  return 0;
259
148k
}
260
/* }}} */
261
262
static zend_always_inline zend_bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
263
53.8k
{
264
53.8k
  const confusable_type_info *info = confusable_types;
265
266
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
267
   * as a scalar type, while "Integer" is likely a class type. */
268
265k
  for (; info->name; ++info) {
269
212k
    if (ZSTR_LEN(name) == info->name_len
270
8.43k
      && memcmp(ZSTR_VAL(name), info->name, info->name_len) == 0
271
1.33k
    ) {
272
1.33k
      *correct_name = info->correct_name;
273
1.33k
      return 1;
274
1.33k
    }
275
212k
  }
276
277
52.4k
  return 0;
278
53.8k
}
279
/* }}} */
280
281
1.33k
static zend_bool zend_is_not_imported(zend_string *name) {
282
  /* Assuming "name" is unqualified here. */
283
1.33k
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
284
1.33k
}
285
286
void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
287
978k
{
288
978k
  *prev_context = CG(context);
289
978k
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
290
978k
  CG(context).vars_size = 0;
291
978k
  CG(context).literals_size = 0;
292
978k
  CG(context).fast_call_var = -1;
293
978k
  CG(context).try_catch_offset = -1;
294
978k
  CG(context).current_brk_cont = -1;
295
978k
  CG(context).last_brk_cont = 0;
296
978k
  CG(context).brk_cont_array = NULL;
297
978k
  CG(context).labels = NULL;
298
978k
}
299
/* }}} */
300
301
void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
302
963k
{
303
963k
  if (CG(context).brk_cont_array) {
304
64.7k
    efree(CG(context).brk_cont_array);
305
64.7k
    CG(context).brk_cont_array = NULL;
306
64.7k
  }
307
963k
  if (CG(context).labels) {
308
10.1k
    zend_hash_destroy(CG(context).labels);
309
10.1k
    FREE_HASHTABLE(CG(context).labels);
310
10.1k
    CG(context).labels = NULL;
311
10.1k
  }
312
963k
  CG(context) = *prev_context;
313
963k
}
314
/* }}} */
315
316
static void zend_reset_import_tables(void) /* {{{ */
317
593k
{
318
593k
  if (FC(imports)) {
319
2.75k
    zend_hash_destroy(FC(imports));
320
2.75k
    efree(FC(imports));
321
2.75k
    FC(imports) = NULL;
322
2.75k
  }
323
324
593k
  if (FC(imports_function)) {
325
467
    zend_hash_destroy(FC(imports_function));
326
467
    efree(FC(imports_function));
327
467
    FC(imports_function) = NULL;
328
467
  }
329
330
593k
  if (FC(imports_const)) {
331
450
    zend_hash_destroy(FC(imports_const));
332
450
    efree(FC(imports_const));
333
450
    FC(imports_const) = NULL;
334
450
  }
335
593k
}
336
/* }}} */
337
338
583k
static void zend_end_namespace(void) /* {{{ */ {
339
583k
  FC(in_namespace) = 0;
340
583k
  zend_reset_import_tables();
341
583k
  if (FC(current_namespace)) {
342
7.35k
    zend_string_release_ex(FC(current_namespace), 0);
343
7.35k
    FC(current_namespace) = NULL;
344
7.35k
  }
345
583k
}
346
/* }}} */
347
348
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
349
589k
{
350
589k
  *prev_context = CG(file_context);
351
589k
  FC(imports) = NULL;
352
589k
  FC(imports_function) = NULL;
353
589k
  FC(imports_const) = NULL;
354
589k
  FC(current_namespace) = NULL;
355
589k
  FC(in_namespace) = 0;
356
589k
  FC(has_bracketed_namespaces) = 0;
357
589k
  FC(declarables).ticks = 0;
358
589k
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
359
589k
}
360
/* }}} */
361
362
void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
363
578k
{
364
578k
  zend_end_namespace();
365
578k
  zend_hash_destroy(&FC(seen_symbols));
366
578k
  CG(file_context) = *prev_context;
367
578k
}
368
/* }}} */
369
370
void zend_init_compiler_data_structures(void) /* {{{ */
371
653k
{
372
653k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
373
653k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
374
653k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
375
653k
  CG(active_class_entry) = NULL;
376
653k
  CG(in_compilation) = 0;
377
653k
  CG(skip_shebang) = 0;
378
379
653k
  CG(encoding_declared) = 0;
380
653k
  CG(memoized_exprs) = NULL;
381
653k
  CG(memoize_mode) = 0;
382
653k
}
383
/* }}} */
384
385
360k
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
386
360k
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
387
360k
  if (zv) {
388
33.9k
    Z_LVAL_P(zv) |= kind;
389
326k
  } else {
390
326k
    zval tmp;
391
326k
    ZVAL_LONG(&tmp, kind);
392
326k
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
393
326k
  }
394
360k
}
395
396
6.21k
static zend_bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
397
6.21k
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
398
6.21k
  return zv && (Z_LVAL_P(zv) & kind) != 0;
399
6.21k
}
400
401
ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
402
664k
{
403
404
664k
  zend_file_handle_dtor(fh);
405
664k
}
406
/* }}} */
407
408
void init_compiler(void) /* {{{ */
409
653k
{
410
653k
  CG(arena) = zend_arena_create(64 * 1024);
411
653k
  CG(active_op_array) = NULL;
412
653k
  memset(&CG(context), 0, sizeof(CG(context)));
413
653k
  zend_init_compiler_data_structures();
414
653k
  zend_init_rsrc_list();
415
653k
  zend_hash_init(&CG(filenames_table), 8, NULL, ZVAL_PTR_DTOR, 0);
416
653k
  zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
417
653k
  CG(unclean_shutdown) = 0;
418
419
653k
  CG(delayed_variance_obligations) = NULL;
420
653k
  CG(delayed_autoloads) = NULL;
421
653k
}
422
/* }}} */
423
424
void shutdown_compiler(void) /* {{{ */
425
652k
{
426
652k
  zend_stack_destroy(&CG(loop_var_stack));
427
652k
  zend_stack_destroy(&CG(delayed_oplines_stack));
428
652k
  zend_stack_destroy(&CG(short_circuiting_opnums));
429
652k
  zend_hash_destroy(&CG(filenames_table));
430
652k
  zend_arena_destroy(CG(arena));
431
432
652k
  if (CG(delayed_variance_obligations)) {
433
1.37k
    zend_hash_destroy(CG(delayed_variance_obligations));
434
1.37k
    FREE_HASHTABLE(CG(delayed_variance_obligations));
435
1.37k
    CG(delayed_variance_obligations) = NULL;
436
1.37k
  }
437
652k
  if (CG(delayed_autoloads)) {
438
1.15k
    zend_hash_destroy(CG(delayed_autoloads));
439
1.15k
    FREE_HASHTABLE(CG(delayed_autoloads));
440
1.15k
    CG(delayed_autoloads) = NULL;
441
1.15k
  }
442
652k
}
443
/* }}} */
444
445
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
446
698k
{
447
698k
  zval *p, rv;
448
449
698k
  if ((p = zend_hash_find(&CG(filenames_table), new_compiled_filename))) {
450
27.6k
    ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
451
27.6k
    CG(compiled_filename) = Z_STR_P(p);
452
27.6k
    return Z_STR_P(p);
453
27.6k
  }
454
455
670k
  new_compiled_filename = zend_new_interned_string(zend_string_copy(new_compiled_filename));
456
670k
  ZVAL_STR(&rv, new_compiled_filename);
457
670k
  zend_hash_add_new(&CG(filenames_table), new_compiled_filename, &rv);
458
459
670k
  CG(compiled_filename) = new_compiled_filename;
460
670k
  return new_compiled_filename;
461
670k
}
462
/* }}} */
463
464
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
465
832k
{
466
832k
  CG(compiled_filename) = original_compiled_filename;
467
832k
}
468
/* }}} */
469
470
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
471
2.21M
{
472
2.21M
  return CG(compiled_filename);
473
2.21M
}
474
/* }}} */
475
476
ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
477
204k
{
478
204k
  return CG(zend_lineno);
479
204k
}
480
/* }}} */
481
482
ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
483
2.46M
{
484
2.46M
  return CG(in_compilation);
485
2.46M
}
486
/* }}} */
487
488
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
489
7.76M
{
490
7.76M
  return (uint32_t)CG(active_op_array)->T++;
491
7.76M
}
492
/* }}} */
493
494
4.96M
static int lookup_cv(zend_string *name) /* {{{ */{
495
4.96M
  zend_op_array *op_array = CG(active_op_array);
496
4.96M
  int i = 0;
497
4.96M
  zend_ulong hash_value = zend_string_hash_val(name);
498
499
13.2M
  while (i < op_array->last_var) {
500
11.1M
    if (ZSTR_H(op_array->vars[i]) == hash_value
501
2.91M
     && zend_string_equals(op_array->vars[i], name)) {
502
2.91M
      return EX_NUM_TO_VAR(i);
503
2.91M
    }
504
8.24M
    i++;
505
8.24M
  }
506
2.05M
  i = op_array->last_var;
507
2.05M
  op_array->last_var++;
508
2.05M
  if (op_array->last_var > CG(context).vars_size) {
509
688k
    CG(context).vars_size += 16; /* FIXME */
510
688k
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
511
688k
  }
512
513
2.05M
  op_array->vars[i] = zend_string_copy(name);
514
2.05M
  return EX_NUM_TO_VAR(i);
515
4.96M
}
516
/* }}} */
517
518
static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
519
13.7M
{
520
13.7M
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
521
13.7M
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
522
13.7M
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
523
13.7M
    Z_TYPE_FLAGS_P(zv) = 0;
524
13.7M
  }
525
13.7M
  return Z_STR_P(zv);
526
13.7M
}
527
528
/* Common part of zend_add_literal and zend_append_individual_literal */
529
static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
530
11.0M
{
531
11.0M
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
532
11.0M
  if (Z_TYPE_P(zv) == IS_STRING) {
533
8.58M
    zval_make_interned_string(zv);
534
8.58M
  }
535
11.0M
  ZVAL_COPY_VALUE(lit, zv);
536
11.0M
  Z_EXTRA_P(lit) = 0;
537
11.0M
}
538
/* }}} */
539
540
/* Is used while compiling a function, using the context to keep track
541
   of an approximate size to avoid to relocate to often.
542
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
543
static int zend_add_literal(zval *zv) /* {{{ */
544
11.0M
{
545
11.0M
  zend_op_array *op_array = CG(active_op_array);
546
11.0M
  int i = op_array->last_literal;
547
11.0M
  op_array->last_literal++;
548
11.0M
  if (i >= CG(context).literals_size) {
549
2.46M
    while (i >= CG(context).literals_size) {
550
1.23M
      CG(context).literals_size += 16; /* FIXME */
551
1.23M
    }
552
1.23M
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
553
1.23M
  }
554
11.0M
  zend_insert_literal(op_array, zv, i);
555
11.0M
  return i;
556
11.0M
}
557
/* }}} */
558
559
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
560
4.21M
{
561
4.21M
  int ret;
562
4.21M
  zval zv;
563
4.21M
  ZVAL_STR(&zv, *str);
564
4.21M
  ret = zend_add_literal(&zv);
565
4.21M
  *str = Z_STR(zv);
566
4.21M
  return ret;
567
4.21M
}
568
/* }}} */
569
570
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
571
872k
{
572
  /* Original name */
573
872k
  int ret = zend_add_literal_string(&name);
574
575
  /* Lowercased name */
576
872k
  zend_string *lc_name = zend_string_tolower(name);
577
872k
  zend_add_literal_string(&lc_name);
578
579
872k
  return ret;
580
872k
}
581
/* }}} */
582
583
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
584
15.5k
{
585
15.5k
  const char *unqualified_name;
586
15.5k
  size_t unqualified_name_len;
587
588
  /* Original name */
589
15.5k
  int ret = zend_add_literal_string(&name);
590
591
  /* Lowercased name */
592
15.5k
  zend_string *lc_name = zend_string_tolower(name);
593
15.5k
  zend_add_literal_string(&lc_name);
594
595
  /* Lowercased unqualfied name */
596
15.5k
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
597
15.5k
    lc_name = zend_string_alloc(unqualified_name_len, 0);
598
15.5k
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
599
15.5k
    zend_add_literal_string(&lc_name);
600
15.5k
  }
601
602
15.5k
  return ret;
603
15.5k
}
604
/* }}} */
605
606
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
607
1.00M
{
608
  /* Original name */
609
1.00M
  int ret = zend_add_literal_string(&name);
610
611
  /* Lowercased name */
612
1.00M
  zend_string *lc_name = zend_string_tolower(name);
613
1.00M
  zend_add_literal_string(&lc_name);
614
615
1.00M
  return ret;
616
1.00M
}
617
/* }}} */
618
619
static int zend_add_const_name_literal(zend_string *name, zend_bool unqualified) /* {{{ */
620
145k
{
621
145k
  zend_string *tmp_name;
622
623
145k
  int ret = zend_add_literal_string(&name);
624
625
145k
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
626
145k
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
627
145k
  if (after_ns) {
628
3.54k
    after_ns += 1;
629
3.54k
    ns_len = after_ns - ZSTR_VAL(name) - 1;
630
3.54k
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
631
632
    /* lowercased namespace name & original constant name */
633
3.54k
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
634
3.54k
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
635
3.54k
    zend_add_literal_string(&tmp_name);
636
637
3.54k
    if (!unqualified) {
638
1.89k
      return ret;
639
1.89k
    }
640
142k
  } else {
641
142k
    after_ns = ZSTR_VAL(name);
642
142k
  }
643
644
  /* original unqualified constant name */
645
144k
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
646
144k
  zend_add_literal_string(&tmp_name);
647
648
144k
  return ret;
649
145k
}
650
/* }}} */
651
652
137k
#define LITERAL_STR(op, str) do { \
653
137k
    zval _c; \
654
137k
    ZVAL_STR(&_c, str); \
655
137k
    op.constant = zend_add_literal(&_c); \
656
137k
  } while (0)
657
658
void zend_stop_lexing(void)
659
650
{
660
650
  if (LANG_SCNG(on_event)) {
661
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
662
0
  }
663
664
650
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
665
650
}
666
667
static inline void zend_begin_loop(
668
    zend_uchar free_opcode, const znode *loop_var, zend_bool is_switch) /* {{{ */
669
115k
{
670
115k
  zend_brk_cont_element *brk_cont_element;
671
115k
  int parent = CG(context).current_brk_cont;
672
115k
  zend_loop_var info = {0};
673
674
115k
  CG(context).current_brk_cont = CG(context).last_brk_cont;
675
115k
  brk_cont_element = get_next_brk_cont_element();
676
115k
  brk_cont_element->parent = parent;
677
115k
  brk_cont_element->is_switch = is_switch;
678
679
115k
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
680
69.3k
    uint32_t start = get_next_op_number();
681
682
69.3k
    info.opcode = free_opcode;
683
69.3k
    info.var_type = loop_var->op_type;
684
69.3k
    info.var_num = loop_var->u.op.var;
685
69.3k
    brk_cont_element->start = start;
686
45.6k
  } else {
687
45.6k
    info.opcode = ZEND_NOP;
688
    /* The start field is used to free temporary variables in case of exceptions.
689
     * We won't try to free something of we don't have loop variable.  */
690
45.6k
    brk_cont_element->start = -1;
691
45.6k
  }
692
693
115k
  zend_stack_push(&CG(loop_var_stack), &info);
694
115k
}
695
/* }}} */
696
697
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
698
114k
{
699
114k
  uint32_t end = get_next_op_number();
700
114k
  zend_brk_cont_element *brk_cont_element
701
114k
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
702
114k
  brk_cont_element->cont = cont_addr;
703
114k
  brk_cont_element->brk = end;
704
114k
  CG(context).current_brk_cont = brk_cont_element->parent;
705
706
114k
  zend_stack_del_top(&CG(loop_var_stack));
707
114k
}
708
/* }}} */
709
710
void zend_do_free(znode *op1) /* {{{ */
711
3.84M
{
712
3.84M
  if (op1->op_type == IS_TMP_VAR) {
713
1.52M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
714
715
1.72M
    while (opline->opcode == ZEND_END_SILENCE ||
716
1.72M
           opline->opcode == ZEND_OP_DATA) {
717
200k
      opline--;
718
200k
    }
719
720
1.52M
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
721
1.51M
      switch (opline->opcode) {
722
2.21k
        case ZEND_BOOL:
723
14.0k
        case ZEND_BOOL_NOT:
724
          /* boolean resuls don't have to be freed */
725
14.0k
          return;
726
85
        case ZEND_POST_INC_STATIC_PROP:
727
85
        case ZEND_POST_DEC_STATIC_PROP:
728
1.76k
        case ZEND_POST_INC_OBJ:
729
2.59k
        case ZEND_POST_DEC_OBJ:
730
52.0k
        case ZEND_POST_INC:
731
54.5k
        case ZEND_POST_DEC:
732
          /* convert $i++ to ++$i */
733
54.5k
          opline->opcode -= 2;
734
54.5k
          opline->result_type = IS_UNUSED;
735
54.5k
          return;
736
1.09M
        case ZEND_ASSIGN:
737
1.17M
        case ZEND_ASSIGN_DIM:
738
1.26M
        case ZEND_ASSIGN_OBJ:
739
1.27M
        case ZEND_ASSIGN_STATIC_PROP:
740
1.29M
        case ZEND_ASSIGN_OP:
741
1.30M
        case ZEND_ASSIGN_DIM_OP:
742
1.31M
        case ZEND_ASSIGN_OBJ_OP:
743
1.31M
        case ZEND_ASSIGN_STATIC_PROP_OP:
744
1.31M
        case ZEND_PRE_INC_STATIC_PROP:
745
1.31M
        case ZEND_PRE_DEC_STATIC_PROP:
746
1.31M
        case ZEND_PRE_INC_OBJ:
747
1.31M
        case ZEND_PRE_DEC_OBJ:
748
1.31M
        case ZEND_PRE_INC:
749
1.32M
        case ZEND_PRE_DEC:
750
1.32M
          opline->result_type = IS_UNUSED;
751
1.32M
          return;
752
140k
      }
753
140k
    }
754
755
140k
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
756
2.31M
  } else if (op1->op_type == IS_VAR) {
757
2.14M
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
758
2.15M
    while (opline->opcode == ZEND_END_SILENCE ||
759
2.15M
        opline->opcode == ZEND_EXT_FCALL_END ||
760
2.15M
        opline->opcode == ZEND_OP_DATA) {
761
15.4k
      opline--;
762
15.4k
    }
763
2.14M
    if (opline->result_type == IS_VAR
764
2.13M
      && opline->result.var == op1->u.op.var) {
765
2.13M
      if (opline->opcode == ZEND_FETCH_THIS) {
766
0
        opline->opcode = ZEND_NOP;
767
0
        opline->result_type = IS_UNUSED;
768
2.13M
      } else {
769
2.13M
        opline->result_type = IS_UNUSED;
770
2.13M
      }
771
12.3k
    } else {
772
33.8k
      while (opline >= CG(active_op_array)->opcodes) {
773
33.8k
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
774
31.2k
                     opline->opcode == ZEND_FETCH_LIST_W) &&
775
3.90k
            opline->op1_type == IS_VAR &&
776
3.90k
            opline->op1.var == op1->u.op.var) {
777
3.41k
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
778
3.41k
          return;
779
3.41k
        }
780
30.4k
        if (opline->result_type == IS_VAR
781
10.0k
          && opline->result.var == op1->u.op.var) {
782
8.90k
          if (opline->opcode == ZEND_NEW) {
783
8.90k
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
784
8.90k
          }
785
8.90k
          break;
786
8.90k
        }
787
21.5k
        opline--;
788
21.5k
      }
789
12.3k
    }
790
171k
  } else if (op1->op_type == IS_CONST) {
791
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
792
     * free the zend_array structure, so references to it from outside the op array
793
     * become invalid. GC would cause such a reference in the root buffer. */
794
167k
    zval_ptr_dtor_nogc(&op1->u.constant);
795
167k
  }
796
3.84M
}
797
/* }}} */
798
799
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
800
77
{
801
77
  uint32_t new_flags = flags | new_flag;
802
77
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
803
20
    zend_throw_exception(zend_ce_compile_error,
804
20
      "Multiple abstract modifiers are not allowed", 0);
805
20
    return 0;
806
20
  }
807
57
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
808
22
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
809
22
    return 0;
810
22
  }
811
35
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
812
35
    zend_throw_exception(zend_ce_compile_error,
813
35
      "Cannot use the final modifier on an abstract class", 0);
814
35
    return 0;
815
35
  }
816
0
  return new_flags;
817
0
}
818
/* }}} */
819
820
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
821
35.6k
{
822
35.6k
  uint32_t new_flags = flags | new_flag;
823
35.6k
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
824
97
    zend_throw_exception(zend_ce_compile_error,
825
97
      "Multiple access type modifiers are not allowed", 0);
826
97
    return 0;
827
97
  }
828
35.5k
  if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
829
20
    zend_throw_exception(zend_ce_compile_error, "Multiple abstract modifiers are not allowed", 0);
830
20
    return 0;
831
20
  }
832
35.4k
  if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
833
19
    zend_throw_exception(zend_ce_compile_error, "Multiple static modifiers are not allowed", 0);
834
19
    return 0;
835
19
  }
836
35.4k
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
837
39
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
838
39
    return 0;
839
39
  }
840
35.4k
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
841
20
    zend_throw_exception(zend_ce_compile_error,
842
20
      "Cannot use the final modifier on an abstract class member", 0);
843
20
    return 0;
844
20
  }
845
35.4k
  return new_flags;
846
35.4k
}
847
/* }}} */
848
849
23.2k
ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name) {
850
23.2k
  return zend_string_concat3(
851
23.2k
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
852
23.2k
    "::", sizeof("::") - 1,
853
23.2k
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
854
23.2k
}
855
856
51.5k
zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
857
51.5k
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
858
51.5k
}
859
860
4.27M
zend_string *zend_prefix_with_ns(zend_string *name) {
861
4.27M
  if (FC(current_namespace)) {
862
45.5k
    zend_string *ns = FC(current_namespace);
863
45.5k
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
864
4.22M
  } else {
865
4.22M
    return zend_string_copy(name);
866
4.22M
  }
867
4.27M
}
868
869
zend_string *zend_resolve_non_class_name(
870
  zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
871
  zend_bool case_sensitive, HashTable *current_import_sub
872
2.77M
) {
873
2.77M
  char *compound;
874
2.77M
  *is_fully_qualified = 0;
875
876
2.77M
  if (ZSTR_VAL(name)[0] == '\\') {
877
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
878
59
    *is_fully_qualified = 1;
879
59
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
880
59
  }
881
882
2.77M
  if (type == ZEND_NAME_FQ) {
883
36.8k
    *is_fully_qualified = 1;
884
36.8k
    return zend_string_copy(name);
885
36.8k
  }
886
887
2.73M
  if (type == ZEND_NAME_RELATIVE) {
888
1.24k
    *is_fully_qualified = 1;
889
1.24k
    return zend_prefix_with_ns(name);
890
1.24k
  }
891
892
2.73M
  if (current_import_sub) {
893
    /* If an unqualified name is a function/const alias, replace it. */
894
2.51k
    zend_string *import_name;
895
2.51k
    if (case_sensitive) {
896
864
      import_name = zend_hash_find_ptr(current_import_sub, name);
897
1.65k
    } else {
898
1.65k
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
899
1.65k
    }
900
901
2.51k
    if (import_name) {
902
1.30k
      *is_fully_qualified = 1;
903
1.30k
      return zend_string_copy(import_name);
904
1.30k
    }
905
2.73M
  }
906
907
2.73M
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
908
2.73M
  if (compound) {
909
5.66k
    *is_fully_qualified = 1;
910
5.66k
  }
911
912
2.73M
  if (compound && FC(imports)) {
913
    /* If the first part of a qualified name is an alias, substitute it. */
914
1.64k
    size_t len = compound - ZSTR_VAL(name);
915
1.64k
    zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
916
917
1.64k
    if (import_name) {
918
287
      return zend_concat_names(
919
287
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
920
287
    }
921
2.73M
  }
922
923
2.73M
  return zend_prefix_with_ns(name);
924
2.73M
}
925
/* }}} */
926
927
zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
928
2.19M
{
929
2.19M
  return zend_resolve_non_class_name(
930
2.19M
    name, type, is_fully_qualified, 0, FC(imports_function));
931
2.19M
}
932
/* }}} */
933
934
582k
zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
935
582k
  return zend_resolve_non_class_name(
936
582k
    name, type, is_fully_qualified, 1, FC(imports_const));
937
582k
}
938
/* }}} */
939
940
zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
941
1.22M
{
942
1.22M
  char *compound;
943
944
1.22M
  if (type == ZEND_NAME_RELATIVE) {
945
1.31k
    return zend_prefix_with_ns(name);
946
1.31k
  }
947
948
1.22M
  if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
949
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
950
43.4k
    if (ZSTR_VAL(name)[0] == '\\') {
951
66
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
952
43.4k
    } else {
953
43.4k
      zend_string_addref(name);
954
43.4k
    }
955
    /* Ensure that \self, \parent and \static are not used */
956
43.4k
    if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
957
19
      zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
958
19
    }
959
43.4k
    return name;
960
43.4k
  }
961
962
1.18M
  if (FC(imports)) {
963
33.0k
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
964
33.0k
    if (compound) {
965
      /* If the first part of a qualified name is an alias, substitute it. */
966
5.17k
      size_t len = compound - ZSTR_VAL(name);
967
5.17k
      zend_string *import_name =
968
5.17k
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
969
970
5.17k
      if (import_name) {
971
4.03k
        return zend_concat_names(
972
4.03k
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
973
4.03k
      }
974
27.8k
    } else {
975
      /* If an unqualified name is an alias, replace it. */
976
27.8k
      zend_string *import_name
977
27.8k
        = zend_hash_find_ptr_lc(FC(imports), name);
978
979
27.8k
      if (import_name) {
980
8.29k
        return zend_string_copy(import_name);
981
8.29k
      }
982
1.17M
    }
983
33.0k
  }
984
985
  /* If not fully qualified and not an alias, prepend the current namespace */
986
1.17M
  return zend_prefix_with_ns(name);
987
1.17M
}
988
/* }}} */
989
990
zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
991
1.16M
{
992
1.16M
  zval *class_name = zend_ast_get_zval(ast);
993
1.16M
  if (Z_TYPE_P(class_name) != IS_STRING) {
994
22
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
995
22
  }
996
1.16M
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
997
1.16M
}
998
/* }}} */
999
1000
static void label_ptr_dtor(zval *zv) /* {{{ */
1001
10.6k
{
1002
10.6k
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1003
10.6k
}
1004
/* }}} */
1005
1006
5.41k
static void str_dtor(zval *zv)  /* {{{ */ {
1007
5.41k
  zend_string_release_ex(Z_STR_P(zv), 0);
1008
5.41k
}
1009
/* }}} */
1010
1011
static zend_bool zend_is_call(zend_ast *ast);
1012
1013
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1014
387k
{
1015
387k
  zend_op_array *op_array = CG(active_op_array);
1016
387k
  uint32_t try_catch_offset = op_array->last_try_catch++;
1017
387k
  zend_try_catch_element *elem;
1018
1019
387k
  op_array->try_catch_array = safe_erealloc(
1020
387k
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1021
1022
387k
  elem = &op_array->try_catch_array[try_catch_offset];
1023
387k
  elem->try_op = try_op;
1024
387k
  elem->catch_op = 0;
1025
387k
  elem->finally_op = 0;
1026
387k
  elem->finally_end = 0;
1027
1028
387k
  return try_catch_offset;
1029
387k
}
1030
/* }}} */
1031
1032
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1033
9.28k
{
1034
9.28k
  if (function->type == ZEND_USER_FUNCTION) {
1035
9.28k
    zend_op_array *op_array = &function->op_array;
1036
1037
9.28k
    if (op_array->refcount) {
1038
9.28k
      (*op_array->refcount)++;
1039
9.28k
    }
1040
9.28k
    if (op_array->static_variables
1041
152
      && !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
1042
152
      GC_ADDREF(op_array->static_variables);
1043
152
    }
1044
1045
9.28k
    if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
1046
0
      ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_PRELOADED);
1047
0
      ZEND_MAP_PTR_NEW(op_array->run_time_cache);
1048
0
      ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
1049
9.28k
    } else {
1050
9.28k
      ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &op_array->static_variables);
1051
9.28k
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
1052
9.28k
      ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
1053
9.28k
    }
1054
9.28k
  }
1055
1056
9.28k
  if (function->common.function_name) {
1057
9.28k
    zend_string_addref(function->common.function_name);
1058
9.28k
  }
1059
9.28k
}
1060
/* }}} */
1061
1062
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(zend_string *lcname, zend_op_array *op_array, zend_bool compile_time) /* {{{ */
1063
130
{
1064
122
  zval *zv = zend_hash_find_ex(compile_time ? CG(function_table) : EG(function_table), lcname, 1);
1065
122
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1066
130
  zend_function *old_function;
1067
1068
130
  ZEND_ASSERT(zv != NULL);
1069
130
  old_function = (zend_function*)Z_PTR_P(zv);
1070
130
  if (old_function->type == ZEND_USER_FUNCTION
1071
107
    && old_function->op_array.last > 0) {
1072
107
    zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
1073
103
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1074
107
          ZSTR_VAL(old_function->op_array.filename),
1075
107
          old_function->op_array.opcodes[0].lineno);
1076
23
  } else {
1077
23
    zend_error_noreturn(error_level, "Cannot redeclare %s()",
1078
23
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1079
23
  }
1080
130
}
1081
1082
ZEND_API zend_result do_bind_function(zval *lcname) /* {{{ */
1083
208
{
1084
208
  zend_function *function;
1085
208
  zval *rtd_key, *zv;
1086
1087
208
  rtd_key = lcname + 1;
1088
208
  zv = zend_hash_find_ex(EG(function_table), Z_STR_P(rtd_key), 1);
1089
208
  if (UNEXPECTED(!zv)) {
1090
4
    do_bind_function_error(Z_STR_P(lcname), NULL, 0);
1091
0
    return FAILURE;
1092
204
  }
1093
204
  function = (zend_function*)Z_PTR_P(zv);
1094
204
  zv = zend_hash_set_bucket_key(EG(function_table), (Bucket*)zv, Z_STR_P(lcname));
1095
204
  if (UNEXPECTED(!zv)) {
1096
4
    do_bind_function_error(Z_STR_P(lcname), &function->op_array, 0);
1097
0
    return FAILURE;
1098
200
  }
1099
200
  return SUCCESS;
1100
200
}
1101
/* }}} */
1102
1103
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1104
30.8k
{
1105
30.8k
  zend_class_entry *ce;
1106
30.8k
  zval *rtd_key, *zv;
1107
1108
30.8k
  rtd_key = lcname + 1;
1109
1110
30.8k
  zv = zend_hash_find_ex(EG(class_table), Z_STR_P(rtd_key), 1);
1111
1112
30.8k
  if (UNEXPECTED(!zv)) {
1113
84
    ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1114
84
    if (ce) {
1115
84
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
1116
0
      return FAILURE;
1117
0
    } else {
1118
0
      do {
1119
0
        ZEND_ASSERT(EG(current_execute_data)->func->op_array.fn_flags & ZEND_ACC_PRELOADED);
1120
0
        if (zend_preload_autoload
1121
0
          && zend_preload_autoload(EG(current_execute_data)->func->op_array.filename) == SUCCESS) {
1122
0
          zv = zend_hash_find_ex(EG(class_table), Z_STR_P(rtd_key), 1);
1123
0
          if (EXPECTED(zv != NULL)) {
1124
0
            break;
1125
0
          }
1126
0
        }
1127
0
        zend_error_noreturn(E_ERROR, "Class %s wasn't preloaded", Z_STRVAL_P(lcname));
1128
0
        return FAILURE;
1129
0
      } while (0);
1130
0
    }
1131
84
  }
1132
1133
  /* Register the derived class */
1134
30.7k
  ce = (zend_class_entry*)Z_PTR_P(zv);
1135
30.7k
  zv = zend_hash_set_bucket_key(EG(class_table), (Bucket*)zv, Z_STR_P(lcname));
1136
30.7k
  if (UNEXPECTED(!zv)) {
1137
270
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
1138
0
    return FAILURE;
1139
30.4k
  }
1140
1141
30.4k
  if (zend_do_link_class(ce, lc_parent_name) == FAILURE) {
1142
    /* Reload bucket pointer, the hash table may have been reallocated */
1143
4.77k
    zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1144
4.77k
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(rtd_key));
1145
4.77k
    return FAILURE;
1146
4.77k
  }
1147
1148
25.7k
  return SUCCESS;
1149
25.7k
}
1150
/* }}} */
1151
1152
67.9k
static zend_string *add_type_string(zend_string *type, zend_string *new_type) {
1153
67.9k
  zend_string *result;
1154
67.9k
  if (type == NULL) {
1155
47.5k
    return zend_string_copy(new_type);
1156
47.5k
  }
1157
1158
20.3k
  result = zend_string_concat3(
1159
20.3k
    ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1160
20.3k
  zend_string_release(type);
1161
20.3k
  return result;
1162
20.3k
}
1163
1164
14.4k
static zend_string *resolve_class_name(zend_string *name, zend_class_entry *scope) {
1165
14.4k
  if (scope) {
1166
6.47k
    if (zend_string_equals_literal_ci(name, "self")) {
1167
240
      name = scope->name;
1168
6.23k
    } else if (zend_string_equals_literal_ci(name, "parent") && scope->parent) {
1169
102
      name = scope->parent->name;
1170
102
    }
1171
6.47k
  }
1172
14.4k
  return name;
1173
14.4k
}
1174
1175
65.6k
zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope) {
1176
65.6k
  zend_string *str = NULL;
1177
1178
65.6k
  if (ZEND_TYPE_HAS_LIST(type)) {
1179
2.12k
    zend_type *list_type;
1180
7.25k
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1181
5.12k
      if (ZEND_TYPE_HAS_CE(*list_type)) {
1182
83
        str = add_type_string(str, ZEND_TYPE_CE(*list_type)->name);
1183
5.04k
      } else {
1184
5.04k
        str = add_type_string(str, resolve_class_name(ZEND_TYPE_NAME(*list_type), scope));
1185
5.04k
      }
1186
5.12k
    } ZEND_TYPE_LIST_FOREACH_END();
1187
63.5k
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1188
9.38k
    str = zend_string_copy(resolve_class_name(ZEND_TYPE_NAME(type), scope));
1189
54.1k
  } else if (ZEND_TYPE_HAS_CE(type)) {
1190
8.75k
    str = zend_string_copy(ZEND_TYPE_CE(type)->name);
1191
8.75k
  }
1192
1193
65.6k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1194
1195
65.6k
  if (type_mask == MAY_BE_ANY) {
1196
411
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED));
1197
1198
411
    return str;
1199
411
  }
1200
65.2k
  if (type_mask & MAY_BE_STATIC) {
1201
551
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1202
551
    if (scope) {
1203
420
      zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1204
420
      if (called_scope) {
1205
299
        name = called_scope->name;
1206
299
      }
1207
420
    }
1208
551
    str = add_type_string(str, name);
1209
551
  }
1210
65.2k
  if (type_mask & MAY_BE_CALLABLE) {
1211
198
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE));
1212
198
  }
1213
65.2k
  if (type_mask & MAY_BE_ITERABLE) {
1214
2.52k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ITERABLE));
1215
2.52k
  }
1216
65.2k
  if (type_mask & MAY_BE_OBJECT) {
1217
1.13k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT));
1218
1.13k
  }
1219
65.2k
  if (type_mask & MAY_BE_ARRAY) {
1220
2.46k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY));
1221
2.46k
  }
1222
65.2k
  if (type_mask & MAY_BE_STRING) {
1223
7.54k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING));
1224
7.54k
  }
1225
65.2k
  if (type_mask & MAY_BE_LONG) {
1226
29.7k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT));
1227
29.7k
  }
1228
65.2k
  if (type_mask & MAY_BE_DOUBLE) {
1229
13.2k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT));
1230
13.2k
  }
1231
65.2k
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1232
2.64k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL));
1233
62.6k
  } else if (type_mask & MAY_BE_FALSE) {
1234
2.00k
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE));
1235
2.00k
  }
1236
65.2k
  if (type_mask & MAY_BE_VOID) {
1237
200
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID));
1238
200
  }
1239
1240
65.2k
  if (type_mask & MAY_BE_NULL) {
1241
9.90k
    zend_bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1242
9.90k
    if (!is_union) {
1243
9.78k
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1244
9.78k
      zend_string_release(str);
1245
9.78k
      return nullable_str;
1246
9.78k
    }
1247
1248
113
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE));
1249
113
  }
1250
55.4k
  return str;
1251
65.2k
}
1252
1253
46.1k
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1254
46.1k
  return zend_type_to_string_resolved(type, NULL);
1255
46.1k
}
1256
1257
1.18k
static zend_bool is_generator_compatible_class_type(zend_string *name) {
1258
1.18k
  return zend_string_equals_literal_ci(name, "Traversable")
1259
1.12k
    || zend_string_equals_literal_ci(name, "Iterator")
1260
972
    || zend_string_equals_literal_ci(name, "Generator");
1261
1.18k
}
1262
1263
static void zend_mark_function_as_generator() /* {{{ */
1264
61.2k
{
1265
61.2k
  if (!CG(active_op_array)->function_name) {
1266
27
    zend_error_noreturn(E_COMPILE_ERROR,
1267
27
      "The \"yield\" expression can only be used inside a function");
1268
27
  }
1269
1270
61.1k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1271
1.35k
    zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1272
1.35k
    zend_bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & (MAY_BE_ITERABLE | MAY_BE_OBJECT)) != 0;
1273
1.35k
    if (!valid_type) {
1274
1.05k
      zend_type *single_type;
1275
2.23k
      ZEND_TYPE_FOREACH(return_type, single_type) {
1276
1.18k
        if (ZEND_TYPE_HAS_NAME(*single_type)
1277
1.18k
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1278
915
          valid_type = 1;
1279
915
          break;
1280
915
        }
1281
1.18k
      } ZEND_TYPE_FOREACH_END();
1282
1.05k
    }
1283
1284
568
    if (!valid_type) {
1285
141
      zend_string *str = zend_type_to_string(return_type);
1286
141
      zend_error_noreturn(E_COMPILE_ERROR,
1287
141
        "Generator return type must be a supertype of Generator, %s given",
1288
141
        ZSTR_VAL(str));
1289
141
    }
1290
60.2k
  }
1291
1292
60.2k
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1293
60.2k
}
1294
/* }}} */
1295
1296
ZEND_API uint32_t zend_build_delayed_early_binding_list(const zend_op_array *op_array) /* {{{ */
1297
0
{
1298
0
  if (op_array->fn_flags & ZEND_ACC_EARLY_BINDING) {
1299
0
    uint32_t  first_early_binding_opline = (uint32_t)-1;
1300
0
    uint32_t *prev_opline_num = &first_early_binding_opline;
1301
0
    zend_op  *opline = op_array->opcodes;
1302
0
    zend_op  *end = opline + op_array->last;
1303
1304
0
    while (opline < end) {
1305
0
      if (opline->opcode == ZEND_DECLARE_CLASS_DELAYED) {
1306
0
        *prev_opline_num = opline - op_array->opcodes;
1307
0
        prev_opline_num = &opline->result.opline_num;
1308
0
      }
1309
0
      ++opline;
1310
0
    }
1311
0
    *prev_opline_num = -1;
1312
0
    return first_early_binding_opline;
1313
0
  }
1314
0
  return (uint32_t)-1;
1315
0
}
1316
/* }}} */
1317
1318
ZEND_API void zend_do_delayed_early_binding(zend_op_array *op_array, uint32_t first_early_binding_opline) /* {{{ */
1319
0
{
1320
0
  if (first_early_binding_opline != (uint32_t)-1) {
1321
0
    zend_bool orig_in_compilation = CG(in_compilation);
1322
0
    uint32_t opline_num = first_early_binding_opline;
1323
0
    void **run_time_cache;
1324
1325
0
    if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
1326
0
      void *ptr;
1327
1328
0
      ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
1329
0
      ptr = emalloc(op_array->cache_size + sizeof(void*));
1330
0
      ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
1331
0
      ptr = (char*)ptr + sizeof(void*);
1332
0
      ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
1333
0
      memset(ptr, 0, op_array->cache_size);
1334
0
    }
1335
0
    run_time_cache = RUN_TIME_CACHE(op_array);
1336
1337
0
    CG(in_compilation) = 1;
1338
0
    while (opline_num != (uint32_t)-1) {
1339
0
      const zend_op *opline = &op_array->opcodes[opline_num];
1340
0
      zval *lcname = RT_CONSTANT(opline, opline->op1);
1341
0
      zval *zv = zend_hash_find_ex(EG(class_table), Z_STR_P(lcname + 1), 1);
1342
1343
0
      if (zv) {
1344
0
        zend_class_entry *ce = Z_CE_P(zv);
1345
0
        zend_string *lc_parent_name = Z_STR_P(RT_CONSTANT(opline, opline->op2));
1346
0
        zend_class_entry *parent_ce = zend_hash_find_ex_ptr(EG(class_table), lc_parent_name, 1);
1347
1348
0
        if (parent_ce) {
1349
0
          if (zend_try_early_bind(ce, parent_ce, Z_STR_P(lcname), zv)) {
1350
            /* Store in run-time cache */
1351
0
            ((void**)((char*)run_time_cache + opline->extended_value))[0] = ce;
1352
0
          }
1353
0
        }
1354
0
      }
1355
0
      opline_num = op_array->opcodes[opline_num].result.opline_num;
1356
0
    }
1357
0
    CG(in_compilation) = orig_in_compilation;
1358
0
  }
1359
0
}
1360
/* }}} */
1361
1362
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1363
111k
{
1364
111k
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1365
111k
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1366
1367
111k
  ZSTR_VAL(prop_name)[0] = '\0';
1368
111k
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1369
111k
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1370
111k
  return prop_name;
1371
111k
}
1372
/* }}} */
1373
1374
static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
1375
29.5k
{
1376
29.5k
  size_t len = 0;
1377
176k
  while (*s++ && maxlen--) len++;
1378
29.5k
  return len;
1379
29.5k
}
1380
/* }}} */
1381
1382
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) /* {{{ */
1383
78.1k
{
1384
78.1k
  size_t class_name_len;
1385
78.1k
  size_t anonclass_src_len;
1386
1387
78.1k
  *class_name = NULL;
1388
1389
78.1k
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1390
63.3k
    *prop_name = ZSTR_VAL(name);
1391
63.3k
    if (prop_len) {
1392
1.44k
      *prop_len = ZSTR_LEN(name);
1393
1.44k
    }
1394
63.3k
    return SUCCESS;
1395
63.3k
  }
1396
14.8k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1397
25
    zend_error(E_NOTICE, "Illegal member variable name");
1398
25
    *prop_name = ZSTR_VAL(name);
1399
25
    if (prop_len) {
1400
19
      *prop_len = ZSTR_LEN(name);
1401
19
    }
1402
25
    return FAILURE;
1403
25
  }
1404
1405
14.8k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1406
14.8k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1407
98
    zend_error(E_NOTICE, "Corrupt member variable name");
1408
98
    *prop_name = ZSTR_VAL(name);
1409
98
    if (prop_len) {
1410
33
      *prop_len = ZSTR_LEN(name);
1411
33
    }
1412
98
    return FAILURE;
1413
98
  }
1414
1415
14.7k
  *class_name = ZSTR_VAL(name) + 1;
1416
14.7k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1417
14.7k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1418
193
    class_name_len += anonclass_src_len + 1;
1419
193
  }
1420
14.7k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1421
14.7k
  if (prop_len) {
1422
11.6k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1423
11.6k
  }
1424
14.7k
  return SUCCESS;
1425
14.7k
}
1426
/* }}} */
1427
1428
234k
static zend_bool can_ct_eval_const(zend_constant *c) {
1429
234k
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1430
0
    return 0;
1431
0
  }
1432
234k
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1433
234k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1434
194k
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1435
194k
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1436
194k
    return 1;
1437
194k
  }
1438
40.5k
  if (Z_TYPE(c->value) < IS_OBJECT
1439
40.5k
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1440
31
    return 1;
1441
31
  }
1442
40.5k
  return 0;
1443
40.5k
}
1444
1445
static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
1446
581k
{
1447
581k
  zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
1448
581k
  if (c && can_ct_eval_const(c)) {
1449
194k
    ZVAL_COPY_OR_DUP(zv, &c->value);
1450
194k
    return 1;
1451
194k
  }
1452
1453
387k
  {
1454
    /* Substitute true, false and null (including unqualified usage in namespaces) */
1455
387k
    const char *lookup_name = ZSTR_VAL(name);
1456
387k
    size_t lookup_len = ZSTR_LEN(name);
1457
1458
387k
    if (!is_fully_qualified) {
1459
379k
      zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1460
379k
    }
1461
1462
387k
    if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1463
182k
      ZVAL_COPY_VALUE(zv, &c->value);
1464
182k
      return 1;
1465
182k
    }
1466
1467
204k
    return 0;
1468
204k
  }
1469
204k
}
1470
/* }}} */
1471
1472
static inline zend_bool zend_is_scope_known() /* {{{ */
1473
55.1k
{
1474
55.1k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1475
    /* Closures can be rebound to a different scope */
1476
1.76k
    return 0;
1477
1.76k
  }
1478
1479
53.3k
  if (!CG(active_class_entry)) {
1480
    /* The scope is known if we're in a free function (no scope), but not if we're in
1481
     * a file/eval (which inherits including/eval'ing scope). */
1482
872
    return CG(active_op_array)->function_name != NULL;
1483
872
  }
1484
1485
  /* For traits self etc refers to the using class, not the trait itself */
1486
52.5k
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1487
52.5k
}
1488
/* }}} */
1489
1490
static inline zend_bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
1491
57.1k
{
1492
57.1k
  if (!CG(active_class_entry)) {
1493
44.6k
    return 0;
1494
44.6k
  }
1495
12.5k
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1496
5.28k
    return 1;
1497
5.28k
  }
1498
7.24k
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1499
5.22k
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1500
7.24k
}
1501
/* }}} */
1502
1503
uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
1504
1.29M
{
1505
1.29M
  if (zend_string_equals_literal_ci(name, "self")) {
1506
28.9k
    return ZEND_FETCH_CLASS_SELF;
1507
1.26M
  } else if (zend_string_equals_literal_ci(name, "parent")) {
1508
10.0k
    return ZEND_FETCH_CLASS_PARENT;
1509
1.25M
  } else if (zend_string_equals_literal_ci(name, "static")) {
1510
16.0k
    return ZEND_FETCH_CLASS_STATIC;
1511
1.24M
  } else {
1512
1.24M
    return ZEND_FETCH_CLASS_DEFAULT;
1513
1.24M
  }
1514
1.29M
}
1515
/* }}} */
1516
1517
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1518
508k
{
1519
  /* Fully qualified names are always default refs */
1520
508k
  if (name_ast->attr == ZEND_NAME_FQ) {
1521
20.7k
    return ZEND_FETCH_CLASS_DEFAULT;
1522
20.7k
  }
1523
1524
487k
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1525
487k
}
1526
/* }}} */
1527
1528
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1529
65.0k
{
1530
65.0k
  zend_string *class_name = zend_ast_get_str(ast);
1531
65.0k
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1532
192
    zend_error_noreturn(E_COMPILE_ERROR,
1533
192
      "Cannot use '%s' as %s, as it is reserved",
1534
192
      ZSTR_VAL(class_name), type);
1535
192
  }
1536
64.8k
  return zend_resolve_class_name(class_name, ast->attr);
1537
64.8k
}
1538
1539
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1540
54.8k
{
1541
54.8k
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1542
30.7k
    zend_class_entry *ce = CG(active_class_entry);
1543
30.7k
    if (!ce) {
1544
59
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1545
59
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1546
21
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1547
30.7k
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1548
40
      zend_error_noreturn(E_COMPILE_ERROR,
1549
40
        "Cannot use \"parent\" when current class scope has no parent");
1550
40
    }
1551
30.7k
  }
1552
54.8k
}
1553
/* }}} */
1554
1555
static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1556
29.3k
{
1557
29.3k
  uint32_t fetch_type;
1558
29.3k
  zval *class_name;
1559
1560
29.3k
  if (class_ast->kind != ZEND_AST_ZVAL) {
1561
680
    return 0;
1562
680
  }
1563
1564
28.6k
  class_name = zend_ast_get_zval(class_ast);
1565
1566
28.6k
  if (Z_TYPE_P(class_name) != IS_STRING) {
1567
19
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1568
19
  }
1569
1570
28.6k
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1571
28.6k
  zend_ensure_valid_class_fetch_type(fetch_type);
1572
1573
28.6k
  switch (fetch_type) {
1574
2.87k
    case ZEND_FETCH_CLASS_SELF:
1575
2.87k
      if (CG(active_class_entry) && zend_is_scope_known()) {
1576
2.13k
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1577
2.13k
        return 1;
1578
2.13k
      }
1579
737
      return 0;
1580
81
    case ZEND_FETCH_CLASS_PARENT:
1581
81
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1582
39
          && zend_is_scope_known()) {
1583
39
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1584
39
        return 1;
1585
39
      }
1586
42
      return 0;
1587
6.71k
    case ZEND_FETCH_CLASS_STATIC:
1588
6.71k
      return 0;
1589
18.9k
    case ZEND_FETCH_CLASS_DEFAULT:
1590
18.9k
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1591
18.9k
      return 1;
1592
0
    EMPTY_SWITCH_DEFAULT_CASE()
1593
28.6k
  }
1594
28.6k
}
1595
/* }}} */
1596
1597
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1598
static zend_bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_entry *scope)
1599
35.9k
{
1600
35.9k
  if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PUBLIC) {
1601
34.6k
    return 1;
1602
1.30k
  } else if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PRIVATE) {
1603
1.30k
    return c->ce == scope;
1604
2
  } else {
1605
2
    zend_class_entry *ce = c->ce;
1606
2
    while (1) {
1607
2
      if (ce == scope) {
1608
0
        return 1;
1609
0
      }
1610
2
      if (!ce->parent) {
1611
2
        break;
1612
2
      }
1613
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1614
0
        ce = ce->parent;
1615
0
      } else {
1616
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1617
0
        if (!ce) {
1618
0
          break;
1619
0
        }
1620
0
      }
1621
0
    }
1622
    /* Reverse case cannot be true during compilation */
1623
2
    return 0;
1624
2
  }
1625
35.9k
}
1626
1627
static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1628
57.1k
{
1629
57.1k
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1630
57.1k
  zend_class_constant *cc;
1631
57.1k
  zval *c;
1632
1633
57.1k
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1634
5.53k
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1635
51.6k
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1636
37.2k
    zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1637
37.2k
    if (ce) {
1638
34.7k
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1639
2.47k
    } else {
1640
2.47k
      return 0;
1641
2.47k
    }
1642
14.3k
  } else {
1643
14.3k
    return 0;
1644
14.3k
  }
1645
1646
40.3k
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1647
1.24k
    return 0;
1648
1.24k
  }
1649
1650
39.0k
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1651
3.17k
    return 0;
1652
3.17k
  }
1653
1654
35.9k
  c = &cc->value;
1655
1656
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1657
35.9k
  if (Z_TYPE_P(c) < IS_OBJECT) {
1658
34.7k
    ZVAL_COPY_OR_DUP(zv, c);
1659
34.7k
    return 1;
1660
34.7k
  }
1661
1662
1.20k
  return 0;
1663
1.20k
}
1664
/* }}} */
1665
1666
static void zend_add_to_list(void *result, void *item) /* {{{ */
1667
3.13k
{
1668
3.13k
  void** list = *(void**)result;
1669
3.13k
  size_t n = 0;
1670
1671
3.13k
  if (list) {
1672
5.67k
    while (list[n]) {
1673
4.25k
      n++;
1674
4.25k
    }
1675
1.42k
  }
1676
1677
3.13k
  list = erealloc(list, sizeof(void*) * (n+2));
1678
1679
3.13k
  list[n]   = item;
1680
3.13k
  list[n+1] = NULL;
1681
1682
3.13k
  *(void**)result = list;
1683
3.13k
}
1684
/* }}} */
1685
1686
void zend_do_extended_stmt(void) /* {{{ */
1687
425k
{
1688
425k
  zend_op *opline;
1689
1690
425k
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1691
425k
    return;
1692
425k
  }
1693
1694
0
  opline = get_next_op();
1695
1696
0
  opline->opcode = ZEND_EXT_STMT;
1697
0
}
1698
/* }}} */
1699
1700
void zend_do_extended_fcall_begin(void) /* {{{ */
1701
3.22M
{
1702
3.22M
  zend_op *opline;
1703
1704
3.22M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1705
3.22M
    return;
1706
3.22M
  }
1707
1708
0
  opline = get_next_op();
1709
1710
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1711
0
}
1712
/* }}} */
1713
1714
void zend_do_extended_fcall_end(void) /* {{{ */
1715
3.22M
{
1716
3.22M
  zend_op *opline;
1717
1718
3.22M
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1719
3.22M
    return;
1720
3.22M
  }
1721
1722
0
  opline = get_next_op();
1723
1724
0
  opline->opcode = ZEND_EXT_FCALL_END;
1725
0
}
1726
/* }}} */
1727
1728
0
zend_bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1729
0
  zend_auto_global *auto_global;
1730
1731
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1732
0
    if (auto_global->armed) {
1733
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1734
0
    }
1735
0
    return 1;
1736
0
  }
1737
0
  return 0;
1738
0
}
1739
/* }}} */
1740
1741
zend_bool zend_is_auto_global(zend_string *name) /* {{{ */
1742
4.59M
{
1743
4.59M
  zend_auto_global *auto_global;
1744
1745
4.59M
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1746
24.4k
    if (auto_global->armed) {
1747
4.39k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1748
4.39k
    }
1749
24.4k
    return 1;
1750
24.4k
  }
1751
4.57M
  return 0;
1752
4.57M
}
1753
/* }}} */
1754
1755
zend_result zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1756
39.1k
{
1757
39.1k
  zend_auto_global auto_global;
1758
39.1k
  zend_result retval;
1759
1760
39.1k
  auto_global.name = name;
1761
39.1k
  auto_global.auto_global_callback = auto_global_callback;
1762
39.1k
  auto_global.jit = jit;
1763
1764
39.1k
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
1765
1766
39.1k
  return retval;
1767
39.1k
}
1768
/* }}} */
1769
1770
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
1771
653k
{
1772
653k
  zend_auto_global *auto_global;
1773
1774
11.1M
  ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
1775
5.23M
    if (auto_global->jit) {
1776
2.61M
      auto_global->armed = 1;
1777
2.61M
    } else if (auto_global->auto_global_callback) {
1778
2.61M
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1779
0
    } else {
1780
0
      auto_global->armed = 0;
1781
0
    }
1782
5.23M
  } ZEND_HASH_FOREACH_END();
1783
653k
}
1784
/* }}} */
1785
1786
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
1787
49.3M
{
1788
49.3M
  zval zv;
1789
49.3M
  int ret;
1790
1791
49.3M
  if (CG(increment_lineno)) {
1792
59.6k
    CG(zend_lineno)++;
1793
59.6k
    CG(increment_lineno) = 0;
1794
59.6k
  }
1795
1796
49.3M
  ret = lex_scan(&zv, elem);
1797
49.3M
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
1798
49.3M
  return ret;
1799
1800
49.3M
}
1801
/* }}} */
1802
1803
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
1804
741k
{
1805
741k
  zend_bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
1806
1807
741k
  ce->refcount = 1;
1808
741k
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
1809
1810
741k
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
1811
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
1812
0
  }
1813
1814
741k
  ce->default_properties_table = NULL;
1815
741k
  ce->default_static_members_table = NULL;
1816
741k
  zend_hash_init(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes);
1817
741k
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
1818
741k
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
1819
1820
741k
  if (ce->type == ZEND_INTERNAL_CLASS) {
1821
557k
    ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
1822
183k
  } else {
1823
183k
    ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
1824
183k
    ce->info.user.doc_comment = NULL;
1825
183k
  }
1826
1827
741k
  ce->default_properties_count = 0;
1828
741k
  ce->default_static_members_count = 0;
1829
741k
  ce->properties_info_table = NULL;
1830
741k
  ce->attributes = NULL;
1831
1832
741k
  if (nullify_handlers) {
1833
183k
    ce->constructor = NULL;
1834
183k
    ce->destructor = NULL;
1835
183k
    ce->clone = NULL;
1836
183k
    ce->__get = NULL;
1837
183k
    ce->__set = NULL;
1838
183k
    ce->__unset = NULL;
1839
183k
    ce->__isset = NULL;
1840
183k
    ce->__call = NULL;
1841
183k
    ce->__callstatic = NULL;
1842
183k
    ce->__tostring = NULL;
1843
183k
    ce->__serialize = NULL;
1844
183k
    ce->__unserialize = NULL;
1845
183k
    ce->__debugInfo = NULL;
1846
183k
    ce->create_object = NULL;
1847
183k
    ce->get_iterator = NULL;
1848
183k
    ce->iterator_funcs_ptr = NULL;
1849
183k
    ce->get_static_method = NULL;
1850
183k
    ce->parent = NULL;
1851
183k
    ce->parent_name = NULL;
1852
183k
    ce->num_interfaces = 0;
1853
183k
    ce->interfaces = NULL;
1854
183k
    ce->num_traits = 0;
1855
183k
    ce->trait_names = NULL;
1856
183k
    ce->trait_aliases = NULL;
1857
183k
    ce->trait_precedences = NULL;
1858
183k
    ce->serialize = NULL;
1859
183k
    ce->unserialize = NULL;
1860
183k
    if (ce->type == ZEND_INTERNAL_CLASS) {
1861
0
      ce->info.internal.module = NULL;
1862
0
      ce->info.internal.builtin_functions = NULL;
1863
0
    }
1864
183k
  }
1865
741k
}
1866
/* }}} */
1867
1868
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
1869
0
{
1870
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
1871
0
}
1872
/* }}} */
1873
1874
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
1875
0
{
1876
0
  zval *left_zv = zend_ast_get_zval(left_ast);
1877
0
  zend_string *left = Z_STR_P(left_zv);
1878
0
  zend_string *right = zend_ast_get_str(right_ast);
1879
1880
0
  zend_string *result;
1881
0
  size_t left_len = ZSTR_LEN(left);
1882
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
1883
1884
0
  result = zend_string_extend(left, len, 0);
1885
0
  ZSTR_VAL(result)[left_len] = '\\';
1886
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
1887
0
  ZSTR_VAL(result)[len] = '\0';
1888
0
  zend_string_release_ex(right, 0);
1889
1890
0
  ZVAL_STR(left_zv, result);
1891
0
  return left_ast;
1892
0
}
1893
/* }}} */
1894
1895
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
1896
6.68k
{
1897
6.68k
  zval *zv = zend_ast_get_zval(ast);
1898
6.68k
  if (Z_TYPE_P(zv) == IS_LONG) {
1899
4.50k
    if (Z_LVAL_P(zv) == 0) {
1900
1.05k
      ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
1901
3.45k
    } else {
1902
3.45k
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
1903
3.45k
      Z_LVAL_P(zv) *= -1;
1904
3.45k
    }
1905
2.17k
  } else if (Z_TYPE_P(zv) == IS_STRING) {
1906
2.17k
    size_t orig_len = Z_STRLEN_P(zv);
1907
2.17k
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
1908
2.17k
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
1909
2.17k
    Z_STRVAL_P(zv)[0] = '-';
1910
0
  } else {
1911
0
    ZEND_UNREACHABLE();
1912
0
  }
1913
6.68k
  return ast;
1914
6.68k
}
1915
/* }}} */
1916
1917
void zend_verify_namespace(void) /* {{{ */
1918
3.15M
{
1919
3.15M
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
1920
146
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
1921
146
  }
1922
3.15M
}
1923
/* }}} */
1924
1925
/* {{{ zend_dirname
1926
   Returns directory name component of path */
1927
ZEND_API size_t zend_dirname(char *path, size_t len)
1928
3.56k
{
1929
3.56k
  register char *end = path + len - 1;
1930
3.56k
  unsigned int len_adjust = 0;
1931
1932
#ifdef ZEND_WIN32
1933
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
1934
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
1935
   */
1936
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
1937
    /* Skip over the drive spec (if any) so as not to change */
1938
    path += 2;
1939
    len_adjust += 2;
1940
    if (2 == len) {
1941
      /* Return "c:" on Win32 for dirname("c:").
1942
       * It would be more consistent to return "c:."
1943
       * but that would require making the string *longer*.
1944
       */
1945
      return len;
1946
    }
1947
  }
1948
#endif
1949
1950
3.56k
  if (len == 0) {
1951
    /* Illegal use of this function */
1952
0
    return 0;
1953
0
  }
1954
1955
  /* Strip trailing slashes */
1956
3.56k
  while (end >= path && IS_SLASH_P(end)) {
1957
0
    end--;
1958
0
  }
1959
3.56k
  if (end < path) {
1960
    /* The path only contained slashes */
1961
0
    path[0] = DEFAULT_SLASH;
1962
0
    path[1] = '\0';
1963
0
    return 1 + len_adjust;
1964
0
  }
1965
1966
  /* Strip filename */
1967
39.5k
  while (end >= path && !IS_SLASH_P(end)) {
1968
35.9k
    end--;
1969
35.9k
  }
1970
3.56k
  if (end < path) {
1971
    /* No slash found, therefore return '.' */
1972
0
    path[0] = '.';
1973
0
    path[1] = '\0';
1974
0
    return 1 + len_adjust;
1975
0
  }
1976
1977
  /* Strip slashes which came before the file name */
1978
7.12k
  while (end >= path && IS_SLASH_P(end)) {
1979
3.56k
    end--;
1980
3.56k
  }
1981
3.56k
  if (end < path) {
1982
3.56k
    path[0] = DEFAULT_SLASH;
1983
3.56k
    path[1] = '\0';
1984
3.56k
    return 1 + len_adjust;
1985
3.56k
  }
1986
0
  *(end+1) = '\0';
1987
1988
0
  return (size_t)(end + 1 - path) + len_adjust;
1989
0
}
1990
/* }}} */
1991
1992
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
1993
843k
{
1994
843k
  zend_uchar factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
1995
1996
843k
  switch (type) {
1997
262k
    case BP_VAR_R:
1998
262k
      opline->result_type = IS_TMP_VAR;
1999
262k
      result->op_type = IS_TMP_VAR;
2000
262k
      return;
2001
332k
    case BP_VAR_W:
2002
332k
      opline->opcode += 1 * factor;
2003
332k
      return;
2004
51.8k
    case BP_VAR_RW:
2005
51.8k
      opline->opcode += 2 * factor;
2006
51.8k
      return;
2007
138k
    case BP_VAR_IS:
2008
138k
      opline->result_type = IS_TMP_VAR;
2009
138k
      result->op_type = IS_TMP_VAR;
2010
138k
      opline->opcode += 3 * factor;
2011
138k
      return;
2012
16.1k
    case BP_VAR_FUNC_ARG:
2013
16.1k
      opline->opcode += 4 * factor;
2014
16.1k
      return;
2015
41.7k
    case BP_VAR_UNSET:
2016
41.7k
      opline->opcode += 5 * factor;
2017
41.7k
      return;
2018
0
    EMPTY_SWITCH_DEFAULT_CASE()
2019
843k
  }
2020
843k
}
2021
/* }}} */
2022
2023
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2024
4.83M
{
2025
4.83M
  opline->result_type = IS_VAR;
2026
4.83M
  opline->result.var = get_temporary_variable();
2027
4.83M
  GET_NODE(result, opline->result);
2028
4.83M
}
2029
/* }}} */
2030
2031
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2032
2.70M
{
2033
2.70M
  opline->result_type = IS_TMP_VAR;
2034
2.70M
  opline->result.var = get_temporary_variable();
2035
2.70M
  GET_NODE(result, opline->result);
2036
2.70M
}
2037
/* }}} */
2038
2039
static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2040
13.6M
{
2041
13.6M
  zend_op *opline = get_next_op();
2042
13.6M
  opline->opcode = opcode;
2043
2044
13.6M
  if (op1 != NULL) {
2045
7.00M
    SET_NODE(opline->op1, op1);
2046
7.00M
  }
2047
2048
13.6M
  if (op2 != NULL) {
2049
2.23M
    SET_NODE(opline->op2, op2);
2050
2.23M
  }
2051
2052
13.6M
  if (result) {
2053
4.08M
    zend_make_var_result(result, opline);
2054
4.08M
  }
2055
13.6M
  return opline;
2056
13.6M
}
2057
/* }}} */
2058
2059
static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2060
2.61M
{
2061
2.61M
  zend_op *opline = get_next_op();
2062
2.61M
  opline->opcode = opcode;
2063
2064
2.61M
  if (op1 != NULL) {
2065
2.34M
    SET_NODE(opline->op1, op1);
2066
2.34M
  }
2067
2068
2.61M
  if (op2 != NULL) {
2069
1.98M
    SET_NODE(opline->op2, op2);
2070
1.98M
  }
2071
2072
2.61M
  if (result) {
2073
2.57M
    zend_make_tmp_result(result, opline);
2074
2.57M
  }
2075
2076
2.61M
  return opline;
2077
2.61M
}
2078
/* }}} */
2079
2080
static void zend_emit_tick(void) /* {{{ */
2081
727
{
2082
727
  zend_op *opline;
2083
2084
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2085
727
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2086
113
    return;
2087
113
  }
2088
2089
614
  opline = get_next_op();
2090
2091
614
  opline->opcode = ZEND_TICKS;
2092
614
  opline->extended_value = FC(declarables).ticks;
2093
614
}
2094
/* }}} */
2095
2096
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2097
259k
{
2098
259k
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2099
259k
}
2100
/* }}} */
2101
2102
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2103
556k
{
2104
556k
  uint32_t opnum = get_next_op_number();
2105
556k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2106
556k
  opline->op1.opline_num = opnum_target;
2107
556k
  return opnum;
2108
556k
}
2109
/* }}} */
2110
2111
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2112
260k
{
2113
260k
  switch (opline->opcode) {
2114
13.0k
    case ZEND_IS_IDENTICAL:
2115
14.3k
    case ZEND_IS_NOT_IDENTICAL:
2116
35.1k
    case ZEND_IS_EQUAL:
2117
39.6k
    case ZEND_IS_NOT_EQUAL:
2118
63.8k
    case ZEND_IS_SMALLER:
2119
78.3k
    case ZEND_IS_SMALLER_OR_EQUAL:
2120
79.1k
    case ZEND_CASE:
2121
80.2k
    case ZEND_CASE_STRICT:
2122
80.3k
    case ZEND_ISSET_ISEMPTY_CV:
2123
80.7k
    case ZEND_ISSET_ISEMPTY_VAR:
2124
86.1k
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2125
88.5k
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2126
88.9k
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2127
89.6k
    case ZEND_INSTANCEOF:
2128
99.9k
    case ZEND_TYPE_CHECK:
2129
100k
    case ZEND_DEFINED:
2130
100k
    case ZEND_IN_ARRAY:
2131
100k
    case ZEND_ARRAY_KEY_EXISTS:
2132
100k
      return 1;
2133
160k
    default:
2134
160k
      return 0;
2135
260k
  }
2136
260k
}
2137
/* }}} */
2138
2139
static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2140
148k
{
2141
148k
  uint32_t opnum = get_next_op_number();
2142
148k
  zend_op *opline;
2143
2144
148k
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2145
126k
    opline = CG(active_op_array)->opcodes + opnum - 1;
2146
126k
    if (opline->result_type == IS_TMP_VAR
2147
125k
     && opline->result.var == cond->u.op.var
2148
125k
     && zend_is_smart_branch(opline)) {
2149
99.5k
      if (opcode == ZEND_JMPZ) {
2150
49.7k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2151
49.7k
      } else {
2152
49.7k
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2153
49.7k
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2154
49.7k
      }
2155
99.5k
    }
2156
126k
  }
2157
148k
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2158
148k
  opline->op2.opline_num = opnum_target;
2159
148k
  return opnum;
2160
148k
}
2161
/* }}} */
2162
2163
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2164
652k
{
2165
652k
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2166
652k
  switch (opline->opcode) {
2167
493k
    case ZEND_JMP:
2168
493k
      opline->op1.opline_num = opnum_target;
2169
493k
      break;
2170
90.6k
    case ZEND_JMPZ:
2171
103k
    case ZEND_JMPNZ:
2172
121k
    case ZEND_JMPZ_EX:
2173
126k
    case ZEND_JMPNZ_EX:
2174
131k
    case ZEND_JMP_SET:
2175
159k
    case ZEND_COALESCE:
2176
159k
    case ZEND_JMP_NULL:
2177
159k
      opline->op2.opline_num = opnum_target;
2178
159k
      break;
2179
0
    EMPTY_SWITCH_DEFAULT_CASE()
2180
652k
  }
2181
652k
}
2182
/* }}} */
2183
2184
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2185
649k
{
2186
649k
  zend_update_jump_target(opnum_jump, get_next_op_number());
2187
649k
}
2188
/* }}} */
2189
2190
static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2191
775k
{
2192
775k
  zend_op tmp_opline;
2193
2194
775k
  init_op(&tmp_opline);
2195
2196
775k
  tmp_opline.opcode = opcode;
2197
775k
  if (op1 != NULL) {
2198
775k
    SET_NODE(tmp_opline.op1, op1);
2199
775k
  }
2200
775k
  if (op2 != NULL) {
2201
735k
    SET_NODE(tmp_opline.op2, op2);
2202
735k
  }
2203
775k
  if (result) {
2204
730k
    zend_make_var_result(result, &tmp_opline);
2205
730k
  }
2206
2207
775k
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2208
775k
  return zend_stack_top(&CG(delayed_oplines_stack));
2209
775k
}
2210
/* }}} */
2211
2212
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2213
1.85M
{
2214
1.85M
  return zend_stack_count(&CG(delayed_oplines_stack));
2215
1.85M
}
2216
/* }}} */
2217
2218
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2219
1.85M
{
2220
1.85M
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2221
1.85M
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2222
2223
1.85M
  ZEND_ASSERT(count >= offset);
2224
2.63M
  for (i = offset; i < count; ++i) {
2225
775k
    opline = get_next_op();
2226
775k
    memcpy(opline, &oplines[i], sizeof(zend_op));
2227
775k
  }
2228
1.85M
  CG(delayed_oplines_stack).top = offset;
2229
1.85M
  return opline;
2230
1.85M
}
2231
/* }}} */
2232
2233
static zend_bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2234
18.1M
{
2235
18.1M
  switch (ast_kind) {
2236
332k
    case ZEND_AST_DIM:
2237
586k
    case ZEND_AST_PROP:
2238
592k
    case ZEND_AST_NULLSAFE_PROP:
2239
639k
    case ZEND_AST_STATIC_PROP:
2240
1.28M
    case ZEND_AST_METHOD_CALL:
2241
1.30M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2242
1.37M
    case ZEND_AST_STATIC_CALL:
2243
1.37M
      return 1;
2244
16.7M
    default:
2245
16.7M
      return 0;
2246
18.1M
  }
2247
18.1M
}
2248
2249
static zend_bool zend_ast_is_short_circuited(const zend_ast *ast)
2250
3.34M
{
2251
3.34M
  switch (ast->kind) {
2252
229k
    case ZEND_AST_DIM:
2253
476k
    case ZEND_AST_PROP:
2254
512k
    case ZEND_AST_STATIC_PROP:
2255
520k
    case ZEND_AST_METHOD_CALL:
2256
521k
    case ZEND_AST_STATIC_CALL:
2257
521k
      return zend_ast_is_short_circuited(ast->child[0]);
2258
1.06k
    case ZEND_AST_NULLSAFE_PROP:
2259
1.46k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2260
1.46k
      return 1;
2261
2.82M
    default:
2262
2.82M
      return 0;
2263
3.34M
  }
2264
3.34M
}
2265
2266
/* Mark nodes that are an inner part of a short-circuiting chain.
2267
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2268
 * We do this to avoid passing down an argument in various compile functions. */
2269
2270
1.50M
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2271
2272
1.28M
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2273
1.28M
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2274
208k
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2275
208k
  }
2276
1.28M
}
2277
2278
static uint32_t zend_short_circuiting_checkpoint()
2279
16.8M
{
2280
16.8M
  return zend_stack_count(&CG(short_circuiting_opnums));
2281
16.8M
}
2282
2283
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, zend_ast *ast)
2284
16.8M
{
2285
16.8M
  zend_bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2286
15.7M
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2287
16.8M
  if (!is_short_circuited) {
2288
15.5M
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2289
15.5M
      && "Short circuiting stack should be empty");
2290
15.5M
    return;
2291
15.5M
  }
2292
2293
1.29M
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2294
    /* Outer-most node will commit. */
2295
52.9k
    return;
2296
52.9k
  }
2297
2298
1.25M
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2299
15.6k
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2300
15.6k
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2301
15.6k
    opline->op2.opline_num = get_next_op_number();
2302
15.6k
    SET_NODE(opline->result, result);
2303
15.6k
    opline->extended_value =
2304
1.30k
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2305
14.3k
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2306
13.1k
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2307
15.6k
    zend_stack_del_top(&CG(short_circuiting_opnums));
2308
15.6k
  }
2309
1.23M
}
2310
2311
static void zend_emit_jmp_null(znode *obj_node)
2312
15.6k
{
2313
15.6k
  uint32_t jmp_null_opnum = get_next_op_number();
2314
15.6k
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2315
15.6k
  if (opline->op1_type == IS_CONST) {
2316
1.04k
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2317
1.04k
  }
2318
15.6k
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2319
15.6k
}
2320
2321
12.0M
#define ZEND_MEMOIZE_NONE 0
2322
100k
#define ZEND_MEMOIZE_COMPILE 1
2323
52.5k
#define ZEND_MEMOIZE_FETCH 2
2324
2325
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2326
47.8k
{
2327
47.8k
  int memoize_mode = CG(memoize_mode);
2328
47.8k
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2329
24.1k
    znode memoized_result;
2330
2331
    /* Go through normal compilation */
2332
24.1k
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2333
24.1k
    zend_compile_expr(result, expr);
2334
24.1k
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2335
2336
24.1k
    if (result->op_type == IS_VAR) {
2337
11.2k
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2338
12.9k
    } else if (result->op_type == IS_TMP_VAR) {
2339
481
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2340
12.4k
    } else {
2341
12.4k
      if (result->op_type == IS_CONST) {
2342
5.13k
        Z_TRY_ADDREF(result->u.constant);
2343
5.13k
      }
2344
12.4k
      memoized_result = *result;
2345
12.4k
    }
2346
2347
24.1k
    zend_hash_index_update_mem(
2348
24.1k
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2349
23.6k
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2350
23.6k
    znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2351
23.6k
    *result = *memoized_result;
2352
23.6k
    if (result->op_type == IS_CONST) {
2353
5.07k
      Z_TRY_ADDREF(result->u.constant);
2354
5.07k
    }
2355
0
  } else {
2356
0
    ZEND_UNREACHABLE();
2357
0
  }
2358
47.8k
}
2359
/* }}} */
2360
2361
95.4k
static size_t zend_type_get_num_classes(zend_type type) {
2362
95.4k
  if (!ZEND_TYPE_HAS_CLASS(type)) {
2363
66.1k
    return 0;
2364
66.1k
  }
2365
29.3k
  if (ZEND_TYPE_HAS_LIST(type)) {
2366
8.35k
    return ZEND_TYPE_LIST(type)->num_types;
2367
8.35k
  }
2368
20.9k
  return 1;
2369
20.9k
}
2370
2371
static void zend_emit_return_type_check(
2372
    znode *expr, zend_arg_info *return_info, zend_bool implicit) /* {{{ */
2373
43.1k
{
2374
43.1k
  zend_type type = return_info->type;
2375
43.1k
  if (ZEND_TYPE_IS_SET(type)) {
2376
43.1k
    zend_op *opline;
2377
2378
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2379
43.1k
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2380
2.27k
      if (expr) {
2381
58
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2382
19
          zend_error_noreturn(E_COMPILE_ERROR,
2383
19
            "A void function must not return a value "
2384
19
            "(did you mean \"return;\" instead of \"return null;\"?)");
2385
39
        } else {
2386
39
          zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
2387
39
        }
2388
2.21k
      }
2389
      /* we don't need run-time check */
2390
2.21k
      return;
2391
2.21k
    }
2392
2393
40.8k
    if (!expr && !implicit) {
2394
38
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2395
19
        zend_error_noreturn(E_COMPILE_ERROR,
2396
19
          "A function with return type must return a value "
2397
19
          "(did you mean \"return null;\" instead of \"return;\"?)");
2398
19
      } else {
2399
19
        zend_error_noreturn(E_COMPILE_ERROR,
2400
19
          "A function with return type must return a value");
2401
19
      }
2402
40.8k
    }
2403
2404
40.8k
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2405
      /* we don't need run-time check for mixed return type */
2406
57
      return;
2407
57
    }
2408
2409
40.7k
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2410
      /* we don't need run-time check */
2411
5.68k
      return;
2412
5.68k
    }
2413
2414
35.0k
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2415
35.0k
    if (expr && expr->op_type == IS_CONST) {
2416
459
      opline->result_type = expr->op_type = IS_TMP_VAR;
2417
459
      opline->result.var = expr->u.op.var = get_temporary_variable();
2418
459
    }
2419
2420
35.0k
    opline->op2.num = zend_alloc_cache_slots(zend_type_get_num_classes(return_info->type));
2421
35.0k
  }
2422
43.1k
}
2423
/* }}} */
2424
2425
void zend_emit_final_return(bool return_one) /* {{{ */
2426
964k
{
2427
964k
  znode zn;
2428
964k
  zend_op *ret;
2429
964k
  zend_bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2430
2431
964k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2432
33.3k
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2433
32.7k
    zend_emit_return_type_check(NULL, CG(active_op_array)->arg_info - 1, 1);
2434
32.7k
  }
2435
2436
964k
  zn.op_type = IS_CONST;
2437
964k
  if (return_one) {
2438
548k
    ZVAL_LONG(&zn.u.constant, 1);
2439
415k
  } else {
2440
415k
    ZVAL_NULL(&zn.u.constant);
2441
415k
  }
2442
2443
951k
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2444
964k
  ret->extended_value = -1;
2445
964k
}
2446
/* }}} */
2447
2448
static inline zend_bool zend_is_variable(zend_ast *ast) /* {{{ */
2449
2.86M
{
2450
2.86M
  return ast->kind == ZEND_AST_VAR
2451
1.70M
    || ast->kind == ZEND_AST_DIM
2452
1.56M
    || ast->kind == ZEND_AST_PROP
2453
1.49M
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2454
1.49M
    || ast->kind == ZEND_AST_STATIC_PROP;
2455
2.86M
}
2456
/* }}} */
2457
2458
static inline zend_bool zend_is_call(zend_ast *ast) /* {{{ */
2459
3.65M
{
2460
3.65M
  return ast->kind == ZEND_AST_CALL
2461
3.16M
    || ast->kind == ZEND_AST_METHOD_CALL
2462
2.94M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2463
2.93M
    || ast->kind == ZEND_AST_STATIC_CALL;
2464
3.65M
}
2465
/* }}} */
2466
2467
static inline zend_bool zend_is_variable_or_call(zend_ast *ast) /* {{{ */
2468
66.8k
{
2469
66.8k
  return zend_is_variable(ast) || zend_is_call(ast);
2470
66.8k
}
2471
/* }}} */
2472
2473
static inline zend_bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
2474
888
{
2475
888
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2476
727
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2477
727
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2478
888
}
2479
/* }}} */
2480
2481
static inline zend_bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
2482
65.7k
{
2483
65.7k
  while (
2484
69.3k
    ast->kind == ZEND_AST_DIM
2485
66.2k
    || ast->kind == ZEND_AST_PROP
2486
3.65k
  ) {
2487
3.65k
    ast = ast->child[0];
2488
3.65k
  }
2489
2490
65.7k
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2491
65.7k
}
2492
/* }}} */
2493
2494
static inline zend_bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2495
387k
{
2496
387k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2497
0
    return 0;
2498
0
  }
2499
2500
387k
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2501
387k
}
2502
/* }}} */
2503
2504
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2505
16.3k
{
2506
16.3k
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2507
13.1k
    zend_ulong index;
2508
2509
13.1k
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2510
583
      zval_ptr_dtor(&node->u.constant);
2511
583
      ZVAL_LONG(&node->u.constant, index);
2512
583
    }
2513
13.1k
  }
2514
16.3k
}
2515
/* }}} */
2516
2517
static inline void zend_handle_numeric_dim(zend_op *opline, znode *dim_node) /* {{{ */
2518
233k
{
2519
233k
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2520
83.1k
    zend_ulong index;
2521
2522
83.1k
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2523
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2524
       * See bug #63217
2525
       */
2526
8.51k
      int c = zend_add_literal(&dim_node->u.constant);
2527
8.51k
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2528
8.51k
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2529
8.51k
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2530
8.51k
      return;
2531
8.51k
    }
2532
83.1k
  }
2533
233k
}
2534
/* }}} */
2535
2536
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2537
51.5k
{
2538
51.5k
  if (class_node->op_type == IS_CONST) {
2539
37.4k
    opline->op1_type = IS_CONST;
2540
37.4k
    opline->op1.constant = zend_add_class_name_literal(
2541
37.4k
      Z_STR(class_node->u.constant));
2542
14.1k
  } else {
2543
14.1k
    SET_NODE(opline->op1, class_node);
2544
14.1k
  }
2545
51.5k
}
2546
/* }}} */
2547
2548
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2549
646k
{
2550
646k
  uint32_t fetch_type;
2551
2552
646k
  if (name_ast->kind != ZEND_AST_ZVAL) {
2553
6.38k
    znode name_node;
2554
2555
6.38k
    zend_compile_expr(&name_node, name_ast);
2556
2557
6.38k
    if (name_node.op_type == IS_CONST) {
2558
101
      zend_string *name;
2559
2560
101
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2561
19
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2562
19
      }
2563
2564
82
      name = Z_STR(name_node.u.constant);
2565
82
      fetch_type = zend_get_class_fetch_type(name);
2566
2567
82
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2568
82
        result->op_type = IS_CONST;
2569
82
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2570
0
      } else {
2571
0
        zend_ensure_valid_class_fetch_type(fetch_type);
2572
0
        result->op_type = IS_UNUSED;
2573
0
        result->u.op.num = fetch_type | fetch_flags;
2574
0
      }
2575
2576
82
      zend_string_release_ex(name, 0);
2577
6.28k
    } else {
2578
6.28k
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2579
6.28k
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2580
6.28k
    }
2581
6.36k
    return;
2582
639k
  }
2583
2584
  /* Fully qualified names are always default refs */
2585
639k
  if (name_ast->attr == ZEND_NAME_FQ) {
2586
17.8k
    result->op_type = IS_CONST;
2587
17.8k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2588
17.8k
    return;
2589
17.8k
  }
2590
2591
621k
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2592
621k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2593
598k
    result->op_type = IS_CONST;
2594
598k
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2595
23.8k
  } else {
2596
23.8k
    zend_ensure_valid_class_fetch_type(fetch_type);
2597
23.8k
    result->op_type = IS_UNUSED;
2598
23.8k
    result->u.op.num = fetch_type | fetch_flags;
2599
23.8k
  }
2600
621k
}
2601
/* }}} */
2602
2603
static zend_result zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
2604
4.24M
{
2605
4.24M
  zend_ast *name_ast = ast->child[0];
2606
4.24M
  if (name_ast->kind == ZEND_AST_ZVAL) {
2607
4.19M
    zval *zv = zend_ast_get_zval(name_ast);
2608
4.19M
    zend_string *name;
2609
2610
4.19M
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2611
4.19M
      name = zval_make_interned_string(zv);
2612
592
    } else {
2613
592
      name = zend_new_interned_string(zval_get_string_func(zv));
2614
592
    }
2615
2616
4.19M
    if (zend_is_auto_global(name)) {
2617
12.1k
      return FAILURE;
2618
12.1k
    }
2619
2620
4.18M
    result->op_type = IS_CV;
2621
4.18M
    result->u.op.var = lookup_cv(name);
2622
2623
4.18M
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2624
592
      zend_string_release_ex(name, 0);
2625
592
    }
2626
2627
4.18M
    return SUCCESS;
2628
4.18M
  }
2629
2630
43.5k
  return FAILURE;
2631
43.5k
}
2632
/* }}} */
2633
2634
static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2635
56.7k
{
2636
56.7k
  zend_ast *name_ast = ast->child[0];
2637
56.7k
  znode name_node;
2638
56.7k
  zend_op *opline;
2639
2640
56.7k
  zend_compile_expr(&name_node, name_ast);
2641
56.7k
  if (name_node.op_type == IS_CONST) {
2642
15.1k
    convert_to_string(&name_node.u.constant);
2643
15.1k
  }
2644
2645
56.7k
  if (delayed) {
2646
22.2k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2647
34.5k
  } else {
2648
34.5k
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2649
34.5k
  }
2650
2651
56.7k
  if (name_node.op_type == IS_CONST &&
2652
15.1k
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2653
2654
12.0k
    opline->extended_value = ZEND_FETCH_GLOBAL;
2655
44.7k
  } else {
2656
44.7k
    opline->extended_value = ZEND_FETCH_LOCAL;
2657
44.7k
  }
2658
2659
56.7k
  zend_adjust_for_fetch_type(opline, result, type);
2660
56.7k
  return opline;
2661
56.7k
}
2662
/* }}} */
2663
2664
static zend_bool is_this_fetch(zend_ast *ast) /* {{{ */
2665
6.45M
{
2666
6.45M
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2667
6.09M
    zval *name = zend_ast_get_zval(ast->child[0]);
2668
6.09M
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
2669
6.09M
  }
2670
2671
365k
  return 0;
2672
365k
}
2673
/* }}} */
2674
2675
static zend_bool this_guaranteed_exists() /* {{{ */
2676
102k
{
2677
102k
  zend_op_array *op_array = CG(active_op_array);
2678
  /* Instance methods always have a $this.
2679
   * This also includes closures that have a scope and use $this. */
2680
102k
  return op_array->scope != NULL
2681
100k
    && (op_array->fn_flags & ZEND_ACC_STATIC) == 0;
2682
102k
}
2683
/* }}} */
2684
2685
static zend_op *zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2686
3.56M
{
2687
3.56M
  if (is_this_fetch(ast)) {
2688
13.2k
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2689
13.2k
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
2690
12.5k
      opline->result_type = IS_TMP_VAR;
2691
12.5k
      result->op_type = IS_TMP_VAR;
2692
12.5k
    }
2693
13.2k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
2694
13.2k
    return opline;
2695
3.54M
  } else if (zend_try_compile_cv(result, ast) == FAILURE) {
2696
50.2k
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
2697
50.2k
  }
2698
3.49M
  return NULL;
2699
3.49M
}
2700
/* }}} */
2701
2702
static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
2703
656k
{
2704
656k
  if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
2705
8.56k
    if (node->op_type == IS_VAR) {
2706
8.54k
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
2707
8.54k
      opline->result_type = IS_VAR;
2708
8.54k
      opline->result.var = opline->op1.var;
2709
19
    } else {
2710
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
2711
19
    }
2712
8.56k
  }
2713
656k
}
2714
/* }}} */
2715
2716
zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, zend_bool by_ref);
2717
void zend_compile_assign(znode *result, zend_ast *ast);
2718
2719
static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
2720
23.9k
{
2721
23.9k
  znode dummy_node;
2722
23.9k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
2723
23.9k
    zend_ast_create_znode(value_node));
2724
23.9k
  zend_compile_expr(&dummy_node, assign_ast);
2725
23.9k
  zend_do_free(&dummy_node);
2726
23.9k
}
2727
/* }}} */
2728
2729
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2730
384k
{
2731
384k
  if (ast->attr == ZEND_DIM_ALTERNATIVE_SYNTAX) {
2732
56
    zend_error(E_COMPILE_ERROR, "Array and string offset access syntax with curly braces is no longer supported");
2733
56
  }
2734
384k
  zend_ast *var_ast = ast->child[0];
2735
384k
  zend_ast *dim_ast = ast->child[1];
2736
384k
  zend_op *opline;
2737
2738
384k
  znode var_node, dim_node;
2739
2740
384k
  zend_short_circuiting_mark_inner(var_ast);
2741
384k
  opline = zend_delayed_compile_var(&var_node, var_ast, type, 0);
2742
384k
  if (opline && type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
2743
16.2k
    opline->extended_value |= ZEND_FETCH_DIM_WRITE;
2744
16.2k
  }
2745
2746
384k
  zend_separate_if_call_and_write(&var_node, var_ast, type);
2747
2748
384k
  if (dim_ast == NULL) {
2749
29.5k
    if (type == BP_VAR_R || type == BP_VAR_IS) {
2750
254
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
2751
254
    }
2752
29.2k
    if (type == BP_VAR_UNSET) {
2753
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
2754
19
    }
2755
29.2k
    dim_node.op_type = IS_UNUSED;
2756
355k
  } else {
2757
355k
    zend_compile_expr(&dim_node, dim_ast);
2758
355k
  }
2759
2760
384k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
2761
384k
  zend_adjust_for_fetch_type(opline, result, type);
2762
2763
384k
  if (dim_node.op_type == IS_CONST) {
2764
221k
    zend_handle_numeric_dim(opline, &dim_node);
2765
221k
  }
2766
384k
  return opline;
2767
384k
}
2768
/* }}} */
2769
2770
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2771
236k
{
2772
236k
  uint32_t offset = zend_delayed_compile_begin();
2773
236k
  zend_delayed_compile_dim(result, ast, type);
2774
236k
  return zend_delayed_compile_end(offset);
2775
236k
}
2776
/* }}} */
2777
2778
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2779
351k
{
2780
351k
  zend_ast *obj_ast = ast->child[0];
2781
351k
  zend_ast *prop_ast = ast->child[1];
2782
2783
351k
  znode obj_node, prop_node;
2784
351k
  zend_op *opline;
2785
351k
  zend_bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
2786
2787
351k
  if (is_this_fetch(obj_ast)) {
2788
92.3k
    if (this_guaranteed_exists()) {
2789
90.4k
      obj_node.op_type = IS_UNUSED;
2790
1.87k
    } else {
2791
1.87k
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
2792
1.87k
    }
2793
92.3k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
2794
2795
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
2796
     * check for a nullsafe access. */
2797
258k
  } else {
2798
258k
    zend_short_circuiting_mark_inner(obj_ast);
2799
258k
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, 0);
2800
258k
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
2801
258k
    if (nullsafe) {
2802
4.29k
      zend_emit_jmp_null(&obj_node);
2803
4.29k
    }
2804
258k
  }
2805
2806
351k
  zend_compile_expr(&prop_node, prop_ast);
2807
2808
351k
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
2809
351k
  if (opline->op2_type == IS_CONST) {
2810
288k
    convert_to_string(CT_CONSTANT(opline->op2));
2811
288k
    opline->extended_value = zend_alloc_cache_slots(3);
2812
288k
  }
2813
2814
351k
  zend_adjust_for_fetch_type(opline, result, type);
2815
2816
351k
  return opline;
2817
351k
}
2818
/* }}} */
2819
2820
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
2821
192k
{
2822
192k
  uint32_t offset = zend_delayed_compile_begin();
2823
192k
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
2824
192k
  if (by_ref) { /* shared with cache_slot */
2825
28.1k
    opline->extended_value |= ZEND_FETCH_REF;
2826
28.1k
  }
2827
192k
  return zend_delayed_compile_end(offset);
2828
192k
}
2829
/* }}} */
2830
2831
zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
2832
51.9k
{
2833
51.9k
  zend_ast *class_ast = ast->child[0];
2834
51.9k
  zend_ast *prop_ast = ast->child[1];
2835
2836
51.9k
  znode class_node, prop_node;
2837
51.9k
  zend_op *opline;
2838
2839
51.9k
  zend_short_circuiting_mark_inner(class_ast);
2840
51.9k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
2841
2842
51.9k
  zend_compile_expr(&prop_node, prop_ast);
2843
2844
51.9k
  if (delayed) {
2845
17.8k
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
2846
34.0k
  } else {
2847
34.0k
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
2848
34.0k
  }
2849
51.9k
  if (opline->op1_type == IS_CONST) {
2850
40.6k
    convert_to_string(CT_CONSTANT(opline->op1));
2851
40.6k
    opline->extended_value = zend_alloc_cache_slots(3);
2852
40.6k
  }
2853
51.9k
  if (class_node.op_type == IS_CONST) {
2854
39.8k
    opline->op2_type = IS_CONST;
2855
39.8k
    opline->op2.constant = zend_add_class_name_literal(
2856
39.8k
      Z_STR(class_node.u.constant));
2857
39.8k
    if (opline->op1_type != IS_CONST) {
2858
10.8k
      opline->extended_value = zend_alloc_cache_slot();
2859
10.8k
    }
2860
12.0k
  } else {
2861
12.0k
    SET_NODE(opline->op2, &class_node);
2862
12.0k
  }
2863
2864
51.9k
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
2865
3.30k
    opline->extended_value |= ZEND_FETCH_REF;
2866
3.30k
  }
2867
2868
51.9k
  zend_adjust_for_fetch_type(opline, result, type);
2869
51.9k
  return opline;
2870
51.9k
}
2871
/* }}} */
2872
2873
14.6k
static void zend_verify_list_assign_target(zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
2874
14.6k
  if (var_ast->kind == ZEND_AST_ARRAY) {
2875
933
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
2876
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
2877
19
    }
2878
914
    if (array_style != var_ast->attr) {
2879
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
2880
19
    }
2881
13.7k
  } else if (!zend_can_write_to_variable(var_ast)) {
2882
86
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
2883
86
  }
2884
14.6k
}
2885
/* }}} */
2886
2887
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node);
2888
2889
/* Propagate refs used on leaf elements to the surrounding list() structures. */
2890
8.55k
static zend_bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
2891
8.55k
  zend_ast_list *list = zend_ast_get_list(ast);
2892
8.55k
  zend_bool has_refs = 0;
2893
8.55k
  uint32_t i;
2894
2895
24.3k
  for (i = 0; i < list->children; ++i) {
2896
15.7k
    zend_ast *elem_ast = list->child[i];
2897
2898
15.7k
    if (elem_ast) {
2899
14.9k
      zend_ast *var_ast = elem_ast->child[0];
2900
14.9k
      if (var_ast->kind == ZEND_AST_ARRAY) {
2901
943
        elem_ast->attr = zend_propagate_list_refs(var_ast);
2902
943
      }
2903
14.9k
      has_refs |= elem_ast->attr;
2904
14.9k
    }
2905
15.7k
  }
2906
2907
8.55k
  return has_refs;
2908
8.55k
}
2909
/* }}} */
2910
2911
static void zend_compile_list_assign(
2912
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
2913
8.45k
{
2914
8.45k
  zend_ast_list *list = zend_ast_get_list(ast);
2915
8.45k
  uint32_t i;
2916
8.45k
  zend_bool has_elems = 0;
2917
8.45k
  zend_bool is_keyed =
2918
8.45k
    list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
2919
2920
8.45k
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
2921
47
    zval_make_interned_string(&expr_node->u.constant);
2922
47
  }
2923
2924
23.8k
  for (i = 0; i < list->children; ++i) {
2925
15.4k
    zend_ast *elem_ast = list->child[i];
2926
15.4k
    zend_ast *var_ast, *key_ast;
2927
15.4k
    znode fetch_result, dim_node;
2928
15.4k
    zend_op *opline;
2929
2930
15.4k
    if (elem_ast == NULL) {
2931
698
      if (is_keyed) {
2932
21
        zend_error(E_COMPILE_ERROR,
2933
21
          "Cannot use empty array entries in keyed array assignment");
2934
677
      } else {
2935
677
        continue;
2936
677
      }
2937
14.7k
    }
2938
2939
14.7k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
2940
20
      zend_error(E_COMPILE_ERROR,
2941
20
          "Spread operator is not supported in assignments");
2942
20
    }
2943
2944
14.7k
    var_ast = elem_ast->child[0];
2945
14.7k
    key_ast = elem_ast->child[1];
2946
14.7k
    has_elems = 1;
2947
2948
14.7k
    if (is_keyed) {
2949
6.70k
      if (key_ast == NULL) {
2950
31
        zend_error(E_COMPILE_ERROR,
2951
31
          "Cannot mix keyed and unkeyed array entries in assignments");
2952
31
      }
2953
2954
6.70k
      zend_compile_expr(&dim_node, key_ast);
2955
8.04k
    } else {
2956
8.04k
      if (key_ast != NULL) {
2957
30
        zend_error(E_COMPILE_ERROR,
2958
30
          "Cannot mix keyed and unkeyed array entries in assignments");
2959
30
      }
2960
2961
8.04k
      dim_node.op_type = IS_CONST;
2962
8.04k
      ZVAL_LONG(&dim_node.u.constant, i);
2963
8.04k
    }
2964
2965
14.7k
    if (expr_node->op_type == IS_CONST) {
2966
2.85k
      Z_TRY_ADDREF(expr_node->u.constant);
2967
2.85k
    }
2968
2969
14.7k
    zend_verify_list_assign_target(var_ast, array_style);
2970
2971
14.7k
    opline = zend_emit_op(&fetch_result,
2972
12.9k
      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);
2973
2974
14.7k
    if (dim_node.op_type == IS_CONST) {
2975
11.9k
      zend_handle_numeric_dim(opline, &dim_node);
2976
11.9k
    }
2977
2978
14.7k
    if (var_ast->kind == ZEND_AST_ARRAY) {
2979
895
      if (elem_ast->attr) {
2980
240
        zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
2981
240
      }
2982
895
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
2983
13.8k
    } else if (elem_ast->attr) {
2984
1.56k
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
2985
12.2k
    } else {
2986
12.2k
      zend_emit_assign_znode(var_ast, &fetch_result);
2987
12.2k
    }
2988
14.7k
  }
2989
2990
8.45k
  if (has_elems == 0) {
2991
51
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
2992
51
  }
2993
2994
8.40k
  if (result) {
2995
5.78k
    *result = *expr_node;
2996
2.61k
  } else {
2997
2.61k
    zend_do_free(expr_node);
2998
2.61k
  }
2999
8.40k
}
3000
/* }}} */
3001
3002
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3003
1.61M
{
3004
1.61M
  if (ast->kind == ZEND_AST_CALL) {
3005
580
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3006
580
  }
3007
1.61M
  if (
3008
1.61M
    ast->kind == ZEND_AST_METHOD_CALL
3009
1.61M
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3010
1.61M
    || ast->kind == ZEND_AST_STATIC_CALL
3011
101
  ) {
3012
101
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3013
101
  }
3014
1.61M
  if (zend_ast_is_short_circuited(ast)) {
3015
168
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3016
168
  }
3017
1.61M
}
3018
/* }}} */
3019
3020
/* Detects $a... = $a pattern */
3021
zend_bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
3022
100k
{
3023
100k
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3024
77.5k
    return 0;
3025
77.5k
  }
3026
3027
52.7k
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3028
29.7k
    var_ast = var_ast->child[0];
3029
29.7k
  }
3030
3031
23.0k
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3032
1.76k
    return 0;
3033
1.76k
  }
3034
3035
21.3k
  {
3036
21.3k
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3037
21.3k
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3038
21.3k
    zend_bool result = zend_string_equals(name1, name2);
3039
21.3k
    zend_string_release_ex(name1, 0);
3040
21.3k
    zend_string_release_ex(name2, 0);
3041
21.3k
    return result;
3042
21.3k
  }
3043
21.3k
}
3044
/* }}} */
3045
3046
void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3047
1.33M
{
3048
1.33M
  zend_ast *var_ast = ast->child[0];
3049
1.33M
  zend_ast *expr_ast = ast->child[1];
3050
3051
1.33M
  znode var_node, expr_node;
3052
1.33M
  zend_op *opline;
3053
1.33M
  uint32_t offset;
3054
1.33M
  if (is_this_fetch(var_ast)) {
3055
39
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3056
39
  }
3057
3058
1.33M
  zend_ensure_writable_variable(var_ast);
3059
3060
1.33M
  switch (var_ast->kind) {
3061
1.12M
    case ZEND_AST_VAR:
3062
1.12M
      offset = zend_delayed_compile_begin();
3063
1.12M
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, 0);
3064
1.12M
      zend_compile_expr(&expr_node, expr_ast);
3065
1.12M
      zend_delayed_compile_end(offset);
3066
1.12M
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3067
1.12M
      return;
3068
6.93k
    case ZEND_AST_STATIC_PROP:
3069
6.93k
      offset = zend_delayed_compile_begin();
3070
6.93k
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, 0);
3071
6.93k
      zend_compile_expr(&expr_node, expr_ast);
3072
3073
6.93k
      opline = zend_delayed_compile_end(offset);
3074
6.93k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3075
6.93k
      opline->result_type = IS_TMP_VAR;
3076
6.93k
      result->op_type = IS_TMP_VAR;
3077
3078
6.93k
      zend_emit_op_data(&expr_node);
3079
6.93k
      return;
3080
100k
    case ZEND_AST_DIM:
3081
100k
      offset = zend_delayed_compile_begin();
3082
100k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
3083
3084
100k
      if (zend_is_assign_to_self(var_ast, expr_ast)
3085
1.11k
       && !is_this_fetch(expr_ast)) {
3086
        /* $a[0] = $a should evaluate the right $a first */
3087
906
        znode cv_node;
3088
3089
906
        if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
3090
0
          zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
3091
906
        } else {
3092
906
          zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3093
906
        }
3094
99.8k
      } else {
3095
99.8k
        zend_compile_expr(&expr_node, expr_ast);
3096
99.8k
      }
3097
3098
100k
      opline = zend_delayed_compile_end(offset);
3099
100k
      opline->opcode = ZEND_ASSIGN_DIM;
3100
100k
      opline->result_type = IS_TMP_VAR;
3101
100k
      result->op_type = IS_TMP_VAR;
3102
3103
100k
      opline = zend_emit_op_data(&expr_node);
3104
100k
      return;
3105
97.6k
    case ZEND_AST_PROP:
3106
97.6k
    case ZEND_AST_NULLSAFE_PROP:
3107
97.6k
      offset = zend_delayed_compile_begin();
3108
97.6k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3109
97.6k
      zend_compile_expr(&expr_node, expr_ast);
3110
3111
97.6k
      opline = zend_delayed_compile_end(offset);
3112
97.6k
      opline->opcode = ZEND_ASSIGN_OBJ;
3113
97.6k
      opline->result_type = IS_TMP_VAR;
3114
97.6k
      result->op_type = IS_TMP_VAR;
3115
3116
97.6k
      zend_emit_op_data(&expr_node);
3117
97.6k
      return;
3118
6.08k
    case ZEND_AST_ARRAY:
3119
6.08k
      if (zend_propagate_list_refs(var_ast)) {
3120
1.15k
        if (!zend_is_variable_or_call(expr_ast)) {
3121
50
          zend_error_noreturn(E_COMPILE_ERROR,
3122
50
            "Cannot assign reference to non referencable value");
3123
50
        }
3124
3125
1.10k
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
3126
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3127
         * self-assignments, this forces the RHS to evaluate first. */
3128
1.10k
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3129
4.92k
      } else {
3130
4.92k
        if (expr_ast->kind == ZEND_AST_VAR) {
3131
          /* list($a, $b) = $a should evaluate the right $a first */
3132
2.45k
          znode cv_node;
3133
3134
2.45k
          if (zend_try_compile_cv(&cv_node, expr_ast) == FAILURE) {
3135
16
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
3136
2.43k
          } else {
3137
2.43k
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3138
2.43k
          }
3139
2.47k
        } else {
3140
2.47k
          zend_compile_expr(&expr_node, expr_ast);
3141
2.47k
        }
3142
4.92k
      }
3143
3144
6.03k
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3145
6.03k
      return;
3146
0
    EMPTY_SWITCH_DEFAULT_CASE();
3147
1.33M
  }
3148
1.33M
}
3149
/* }}} */
3150
3151
void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3152
40.7k
{
3153
40.7k
  zend_ast *target_ast = ast->child[0];
3154
40.7k
  zend_ast *source_ast = ast->child[1];
3155
3156
40.7k
  znode target_node, source_node;
3157
40.7k
  zend_op *opline;
3158
40.7k
  uint32_t offset, flags;
3159
3160
40.7k
  if (is_this_fetch(target_ast)) {
3161
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3162
19
  }
3163
40.7k
  zend_ensure_writable_variable(target_ast);
3164
40.7k
  if (zend_ast_is_short_circuited(source_ast)) {
3165
40
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
3166
40
  }
3167
3168
40.6k
  offset = zend_delayed_compile_begin();
3169
40.6k
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, 1);
3170
40.6k
  zend_compile_var(&source_node, source_ast, BP_VAR_W, 1);
3171
3172
40.6k
  if ((target_ast->kind != ZEND_AST_VAR
3173
23.0k
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3174
18.8k
   && source_node.op_type != IS_CV) {
3175
    /* Both LHS and RHS expressions may modify the same data structure,
3176
     * and the modification during RHS evaluation may dangle the pointer
3177
     * to the result of the LHS evaluation.
3178
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3179
     * See: Bug #71539
3180
     */
3181
13.0k
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3182
13.0k
  }
3183
3184
40.6k
  opline = zend_delayed_compile_end(offset);
3185
3186
40.6k
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3187
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3188
19
  }
3189
3190
40.6k
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3191
3192
40.6k
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3193
12.4k
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3194
12.4k
    opline->extended_value &= ~ZEND_FETCH_REF;
3195
12.4k
    opline->extended_value |= flags;
3196
12.4k
    zend_emit_op_data(&source_node);
3197
12.4k
    if (result != NULL) {
3198
12.4k
      *result = target_node;
3199
12.4k
    }
3200
28.2k
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3201
828
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3202
828
    opline->extended_value &= ~ZEND_FETCH_REF;
3203
828
    opline->extended_value |= flags;
3204
828
    zend_emit_op_data(&source_node);
3205
828
    if (result != NULL) {
3206
828
      *result = target_node;
3207
828
    }
3208
27.4k
  } else {
3209
27.4k
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3210
27.4k
    opline->extended_value = flags;
3211
27.4k
  }
3212
40.6k
}
3213
/* }}} */
3214
3215
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
3216
1.77k
{
3217
1.77k
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3218
1.77k
    zend_ast_create_znode(value_node));
3219
1.77k
  zend_compile_expr(NULL, assign_ast);
3220
1.77k
}
3221
/* }}} */
3222
3223
void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3224
53.5k
{
3225
53.5k
  zend_ast *var_ast = ast->child[0];
3226
53.5k
  zend_ast *expr_ast = ast->child[1];
3227
53.5k
  uint32_t opcode = ast->attr;
3228
3229
53.5k
  znode var_node, expr_node;
3230
53.5k
  zend_op *opline;
3231
53.5k
  uint32_t offset, cache_slot;
3232
3233
53.5k
  zend_ensure_writable_variable(var_ast);
3234
3235
53.5k
  switch (var_ast->kind) {
3236
35.4k
    case ZEND_AST_VAR:
3237
35.4k
      offset = zend_delayed_compile_begin();
3238
35.4k
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
3239
35.4k
      zend_compile_expr(&expr_node, expr_ast);
3240
35.4k
      zend_delayed_compile_end(offset);
3241
35.4k
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3242
35.4k
      opline->extended_value = opcode;
3243
35.4k
      return;
3244
1.88k
    case ZEND_AST_STATIC_PROP:
3245
1.88k
      offset = zend_delayed_compile_begin();
3246
1.88k
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, 0);
3247
1.88k
      zend_compile_expr(&expr_node, expr_ast);
3248
3249
1.88k
      opline = zend_delayed_compile_end(offset);
3250
1.88k
      cache_slot = opline->extended_value;
3251
1.88k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3252
1.88k
      opline->extended_value = opcode;
3253
1.88k
      opline->result_type = IS_TMP_VAR;
3254
1.88k
      result->op_type = IS_TMP_VAR;
3255
3256
1.88k
      opline = zend_emit_op_data(&expr_node);
3257
1.88k
      opline->extended_value = cache_slot;
3258
1.88k
      return;
3259
8.91k
    case ZEND_AST_DIM:
3260
8.91k
      offset = zend_delayed_compile_begin();
3261
8.91k
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
3262
8.91k
      zend_compile_expr(&expr_node, expr_ast);
3263
3264
8.91k
      opline = zend_delayed_compile_end(offset);
3265
8.91k
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3266
8.91k
      opline->extended_value = opcode;
3267
8.91k
      opline->result_type = IS_TMP_VAR;
3268
8.91k
      result->op_type = IS_TMP_VAR;
3269
3270
8.91k
      zend_emit_op_data(&expr_node);
3271
8.91k
      return;
3272
7.24k
    case ZEND_AST_PROP:
3273
7.24k
    case ZEND_AST_NULLSAFE_PROP:
3274
7.24k
      offset = zend_delayed_compile_begin();
3275
7.24k
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3276
7.24k
      zend_compile_expr(&expr_node, expr_ast);
3277
3278
7.24k
      opline = zend_delayed_compile_end(offset);
3279
7.24k
      cache_slot = opline->extended_value;
3280
7.24k
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3281
7.24k
      opline->extended_value = opcode;
3282
7.24k
      opline->result_type = IS_TMP_VAR;
3283
7.24k
      result->op_type = IS_TMP_VAR;
3284
3285
7.24k
      opline = zend_emit_op_data(&expr_node);
3286
7.24k
      opline->extended_value = cache_slot;
3287
7.24k
      return;
3288
0
    EMPTY_SWITCH_DEFAULT_CASE()
3289
53.5k
  }
3290
53.5k
}
3291
/* }}} */
3292
3293
55.1k
static uint32_t zend_get_arg_num(zend_function *fn, zend_string *arg_name) {
3294
  // TODO: Caching?
3295
55.1k
  if (fn->type == ZEND_USER_FUNCTION) {
3296
252k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3297
250k
      zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3298
250k
      if (zend_string_equals(arg_info->name, arg_name)) {
3299
41.0k
        return i + 1;
3300
41.0k
      }
3301
250k
    }
3302
12.0k
  } else {
3303
26.6k
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3304
20.2k
      zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
3305
20.2k
      size_t len = strlen(arg_info->name);
3306
20.2k
      if (len == ZSTR_LEN(arg_name) && !memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
3307
5.66k
        return i + 1;
3308
5.66k
      }
3309
20.2k
    }
3310
12.0k
  }
3311
3312
  /* Either an invalid argument name, or collected into a variadic argument. */
3313
8.43k
  return (uint32_t) -1;
3314
55.1k
}
3315
3316
uint32_t zend_compile_args(
3317
    zend_ast *ast, zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3318
3.20M
{
3319
3.20M
  zend_ast_list *args = zend_ast_get_list(ast);
3320
3.20M
  uint32_t i;
3321
3.20M
  zend_bool uses_arg_unpack = 0;
3322
3.20M
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3323
3324
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3325
   * May not actually use named argument passing. */
3326
3.20M
  zend_bool uses_named_args = 0;
3327
  /* Whether there may be any undef arguments due to the use of named arguments. */
3328
3.20M
  zend_bool may_have_undef = 0;
3329
  /* Whether there may be any extra named arguments collected into a variadic. */
3330
3.20M
  *may_have_extra_named_args = 0;
3331
3332
6.44M
  for (i = 0; i < args->children; ++i) {
3333
3.24M
    zend_ast *arg = args->child[i];
3334
3.24M
    zend_string *arg_name = NULL;
3335
3.24M
    uint32_t arg_num = i + 1;
3336
3337
3.24M
    znode arg_node;
3338
3.24M
    zend_op *opline;
3339
3.24M
    zend_uchar opcode;
3340
3341
3.24M
    if (arg->kind == ZEND_AST_UNPACK) {
3342
9.51k
      if (uses_named_args) {
3343
19
        zend_error_noreturn(E_COMPILE_ERROR,
3344
19
          "Cannot combine named arguments and argument unpacking");
3345
19
      }
3346
3347
9.49k
      uses_arg_unpack = 1;
3348
9.49k
      fbc = NULL;
3349
3350
9.49k
      zend_compile_expr(&arg_node, arg->child[0]);
3351
9.49k
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3352
9.49k
      opline->op2.num = arg_count;
3353
9.49k
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3354
3355
      /* Unpack may contain named arguments. */
3356
9.49k
      may_have_undef = 1;
3357
9.49k
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3358
9.49k
        *may_have_extra_named_args = 1;
3359
9.49k
      }
3360
9.49k
      continue;
3361
9.49k
    }
3362
3363
3.23M
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3364
96.6k
      if (uses_arg_unpack) {
3365
19
        zend_error_noreturn(E_COMPILE_ERROR,
3366
19
          "Cannot combine named arguments and argument unpacking");
3367
19
      }
3368
3369
96.6k
      uses_named_args = 1;
3370
96.6k
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3371
96.6k
      arg = arg->child[1];
3372
3373
96.6k
      if (fbc) {
3374
55.1k
        arg_num = zend_get_arg_num(fbc, arg_name);
3375
55.1k
        if (arg_num == arg_count + 1 && !may_have_undef) {
3376
          /* Using named arguments, but passing in order. */
3377
12.9k
          arg_name = NULL;
3378
12.9k
          arg_count++;
3379
42.2k
        } else {
3380
          // TODO: We could track which arguments were passed, even if out of order.
3381
42.2k
          may_have_undef = 1;
3382
42.2k
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3383
7.32k
            *may_have_extra_named_args = 1;
3384
7.32k
          }
3385
42.2k
        }
3386
41.4k
      } else {
3387
41.4k
        arg_num = (uint32_t) -1;
3388
41.4k
        may_have_undef = 1;
3389
41.4k
        *may_have_extra_named_args = 1;
3390
41.4k
      }
3391
3.14M
    } else {
3392
3.14M
      if (uses_arg_unpack) {
3393
19
        zend_error_noreturn(E_COMPILE_ERROR,
3394
19
          "Cannot use positional argument after argument unpacking");
3395
19
      }
3396
3397
3.14M
      if (uses_named_args) {
3398
216
        zend_error_noreturn(E_COMPILE_ERROR,
3399
216
          "Cannot use positional argument after named argument");
3400
216
      }
3401
3402
3.14M
      arg_count++;
3403
3.14M
    }
3404
3405
3.23M
    if (zend_is_call(arg)) {
3406
711k
      zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
3407
711k
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3408
        /* Function call was converted into builtin instruction */
3409
23.5k
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3410
569
          opcode = ZEND_SEND_VAL_EX;
3411
22.9k
        } else {
3412
22.9k
          opcode = ZEND_SEND_VAL;
3413
22.9k
        }
3414
687k
      } else {
3415
687k
        if (fbc && arg_num != (uint32_t) -1) {
3416
485k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3417
15.3k
            opcode = ZEND_SEND_VAR_NO_REF;
3418
470k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3419
41
            opcode = ZEND_SEND_VAL;
3420
470k
          } else {
3421
470k
            opcode = ZEND_SEND_VAR;
3422
470k
          }
3423
202k
        } else {
3424
202k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3425
202k
        }
3426
687k
      }
3427
2.52M
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3428
1.07M
      if (fbc && arg_num != (uint32_t) -1) {
3429
499k
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3430
47.6k
          zend_compile_var(&arg_node, arg, BP_VAR_W, 1);
3431
47.6k
          opcode = ZEND_SEND_REF;
3432
452k
        } else {
3433
452k
          zend_compile_var(&arg_node, arg, BP_VAR_R, 0);
3434
452k
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3435
452k
        }
3436
575k
      } else {
3437
575k
        do {
3438
575k
          if (arg->kind == ZEND_AST_VAR) {
3439
561k
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3440
561k
            if (is_this_fetch(arg)) {
3441
1.39k
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3442
1.39k
              opcode = ZEND_SEND_VAR_EX;
3443
1.39k
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3444
1.39k
              break;
3445
559k
            } else if (zend_try_compile_cv(&arg_node, arg) == SUCCESS) {
3446
558k
              opcode = ZEND_SEND_VAR_EX;
3447
558k
              break;
3448
558k
            }
3449
15.3k
          }
3450
15.3k
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3451
15.3k
          if (arg_name) {
3452
4.19k
            opline->op2_type = IS_CONST;
3453
4.19k
            zend_string_addref(arg_name);
3454
4.19k
            opline->op2.constant = zend_add_literal_string(&arg_name);
3455
4.19k
            opline->result.num = zend_alloc_cache_slots(2);
3456
11.1k
          } else {
3457
11.1k
            opline->op2.num = arg_num;
3458
11.1k
          }
3459
15.3k
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, 1);
3460
15.3k
          opcode = ZEND_SEND_FUNC_ARG;
3461
15.3k
        } while (0);
3462
575k
      }
3463
1.45M
    } else {
3464
1.45M
      zend_compile_expr(&arg_node, arg);
3465
1.45M
      if (arg_node.op_type == IS_VAR) {
3466
        /* pass ++$a or something similar */
3467
41.4k
        if (fbc && arg_num != (uint32_t) -1) {
3468
30.6k
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3469
90
            opcode = ZEND_SEND_VAR_NO_REF;
3470
30.5k
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3471
0
            opcode = ZEND_SEND_VAL;
3472
30.5k
          } else {
3473
30.5k
            opcode = ZEND_SEND_VAR;
3474
30.5k
          }
3475
10.8k
        } else {
3476
10.8k
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3477
10.8k
        }
3478
1.40M
      } else if (arg_node.op_type == IS_CV) {
3479
0
        if (fbc && arg_num != (uint32_t) -1) {
3480
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3481
0
            opcode = ZEND_SEND_REF;
3482
0
          } else {
3483
0
            opcode = ZEND_SEND_VAR;
3484
0
          }
3485
0
        } else {
3486
0
          opcode = ZEND_SEND_VAR_EX;
3487
0
        }
3488
1.40M
      } else {
3489
        /* Delay "Only variables can be passed by reference" error to execution */
3490
1.40M
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3491
1.16M
          opcode = ZEND_SEND_VAL;
3492
248k
        } else {
3493
248k
          opcode = ZEND_SEND_VAL_EX;
3494
248k
        }
3495
1.40M
      }
3496
1.45M
    }
3497
3498
3.23M
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3499
3.23M
    if (arg_name) {
3500
83.6k
      opline->op2_type = IS_CONST;
3501
83.6k
      zend_string_addref(arg_name);
3502
83.6k
      opline->op2.constant = zend_add_literal_string(&arg_name);
3503
83.6k
      opline->result.num = zend_alloc_cache_slots(2);
3504
3.15M
    } else {
3505
3.15M
      opline->op2.opline_num = arg_num;
3506
3.15M
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3507
3.15M
    }
3508
3.23M
  }
3509
3510
3.20M
  if (may_have_undef) {
3511
45.6k
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3512
45.6k
  }
3513
3514
3.20M
  return arg_count;
3515
3.20M
}
3516
/* }}} */
3517
3518
ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc) /* {{{ */
3519
3.20M
{
3520
3.20M
  if (fbc) {
3521
1.71M
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3522
1.35M
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3523
1.34M
        if (!(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED))) {
3524
1.34M
          return ZEND_DO_ICALL;
3525
0
        } else {
3526
0
          return ZEND_DO_FCALL_BY_NAME;
3527
0
        }
3528
360k
      }
3529
360k
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3530
360k
      if (zend_execute_ex == execute_ex && !(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
3531
0
        return ZEND_DO_UCALL;
3532
0
      }
3533
1.49M
    }
3534
1.49M
  } else if (zend_execute_ex == execute_ex &&
3535
0
             !zend_execute_internal &&
3536
0
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3537
0
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3538
0
    return ZEND_DO_FCALL_BY_NAME;
3539
0
  }
3540
1.85M
  return ZEND_DO_FCALL;
3541
1.85M
}
3542
/* }}} */
3543
3544
void zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
3545
3.20M
{
3546
3.20M
  zend_op *opline;
3547
3.20M
  uint32_t opnum_init = get_next_op_number() - 1;
3548
3.20M
  uint32_t arg_count;
3549
3.20M
  bool may_have_extra_named_args;
3550
3551
3.20M
  arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3552
3553
3.20M
  zend_do_extended_fcall_begin();
3554
3555
3.20M
  opline = &CG(active_op_array)->opcodes[opnum_init];
3556
3.20M
  opline->extended_value = arg_count;
3557
3558
3.20M
  if (opline->opcode == ZEND_INIT_FCALL) {
3559
1.69M
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3560
1.69M
  }
3561
3562
3.20M
  opline = zend_emit_op(result, zend_get_call_op(opline, fbc), NULL, NULL);
3563
3.20M
  if (may_have_extra_named_args) {
3564
32.3k
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
3565
32.3k
  }
3566
3.20M
  zend_do_extended_fcall_end();
3567
3.20M
}
3568
/* }}} */
3569
3570
zend_bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
3571
2.19M
{
3572
2.19M
  zend_string *orig_name = zend_ast_get_str(name_ast);
3573
2.19M
  zend_bool is_fully_qualified;
3574
3575
2.19M
  name_node->op_type = IS_CONST;
3576
2.19M
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
3577
2.19M
    orig_name, name_ast->attr, &is_fully_qualified));
3578
3579
2.19M
  return !is_fully_qualified && FC(current_namespace);
3580
2.19M
}
3581
/* }}} */
3582
3583
void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
3584
15.2k
{
3585
15.2k
  zend_op *opline = get_next_op();
3586
15.2k
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
3587
15.2k
  opline->op2_type = IS_CONST;
3588
15.2k
  opline->op2.constant = zend_add_ns_func_name_literal(
3589
15.2k
    Z_STR(name_node->u.constant));
3590
15.2k
  opline->result.num = zend_alloc_cache_slot();
3591
3592
15.2k
  zend_compile_call_common(result, args_ast, NULL);
3593
15.2k
}
3594
/* }}} */
3595
3596
void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
3597
447k
{
3598
447k
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
3599
402k
    const char *colon;
3600
402k
    zend_string *str = Z_STR(name_node->u.constant);
3601
402k
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
3602
87
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
3603
87
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
3604
87
      zend_op *opline = get_next_op();
3605
3606
87
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
3607
87
      opline->op1_type = IS_CONST;
3608
87
      opline->op1.constant = zend_add_class_name_literal(class);
3609
87
      opline->op2_type = IS_CONST;
3610
87
      opline->op2.constant = zend_add_func_name_literal(method);
3611
      /* 2 slots, for class and method */
3612
87
      opline->result.num = zend_alloc_cache_slots(2);
3613
87
      zval_ptr_dtor(&name_node->u.constant);
3614
402k
    } else {
3615
402k
      zend_op *opline = get_next_op();
3616
3617
402k
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
3618
402k
      opline->op2_type = IS_CONST;
3619
402k
      opline->op2.constant = zend_add_func_name_literal(str);
3620
402k
      opline->result.num = zend_alloc_cache_slot();
3621
402k
    }
3622
45.1k
  } else {
3623
45.1k
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
3624
45.1k
  }
3625
3626
447k
  zend_compile_call_common(result, args_ast, NULL);
3627
447k
}
3628
/* }}} */
3629
3630
static inline zend_bool zend_args_contain_unpack_or_named(zend_ast_list *args) /* {{{ */
3631
1.39M
{
3632
1.39M
  uint32_t i;
3633
3.00M
  for (i = 0; i < args->children; ++i) {
3634
1.62M
    zend_ast *arg = args->child[i];
3635
1.62M
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
3636
9.06k
      return 1;
3637
9.06k
    }
3638
1.62M
  }
3639
1.38M
  return 0;
3640
1.39M
}
3641
/* }}} */
3642
3643
zend_result zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
3644
9.97k
{
3645
9.97k
  znode arg_node;
3646
3647
9.97k
  if (args->children != 1) {
3648
23
    return FAILURE;
3649
23
  }
3650
3651
9.95k
  zend_compile_expr(&arg_node, args->child[0]);
3652
9.95k
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
3653
106
    result->op_type = IS_CONST;
3654
106
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
3655
106
    zval_ptr_dtor_str(&arg_node.u.constant);
3656
9.84k
  } else {
3657
9.84k
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
3658
9.84k
  }
3659
9.95k
  return SUCCESS;
3660
9.95k
}
3661
/* }}} */
3662
3663
zend_result zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
3664
22.2k
{
3665
22.2k
  znode arg_node;
3666
22.2k
  zend_op *opline;
3667
3668
22.2k
  if (args->children != 1) {
3669
135
    return FAILURE;
3670
135
  }
3671
3672
22.1k
  zend_compile_expr(&arg_node, args->child[0]);
3673
22.1k
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
3674
22.1k
  if (type != _IS_BOOL) {
3675
21.6k
    opline->extended_value = (1 << type);
3676
428
  } else {
3677
428
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
3678
428
  }
3679
22.1k
  return SUCCESS;
3680
22.1k
}
3681
/* }}} */
3682
3683
static zend_result zend_compile_func_is_scalar(znode *result, zend_ast_list *args) /* {{{ */
3684
423
{
3685
423
  znode arg_node;
3686
423
  zend_op *opline;
3687
3688
423
  if (args->children != 1) {
3689
16
    return FAILURE;
3690
16
  }
3691
3692
407
  zend_compile_expr(&arg_node, args->child[0]);
3693
407
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
3694
407
    opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
3695
407
  return SUCCESS;
3696
407
}
3697
3698
zend_result zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
3699
2.04k
{
3700
2.04k
  znode arg_node;
3701
2.04k
  zend_op *opline;
3702
3703
2.04k
  if (args->children != 1) {
3704
59
    return FAILURE;
3705
59
  }
3706
3707
1.98k
  zend_compile_expr(&arg_node, args->child[0]);
3708
1.98k
  if (type == _IS_BOOL) {
3709
403
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
3710
1.58k
  } else {
3711
1.58k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
3712
1.58k
    opline->extended_value = type;
3713
1.58k
  }
3714
1.98k
  return SUCCESS;
3715
1.98k
}
3716
/* }}} */
3717
3718
zend_result zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
3719
1.52k
{
3720
1.52k
  zend_string *name;
3721
1.52k
  zend_op *opline;
3722
3723
1.52k
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
3724
480
    return FAILURE;
3725
480
  }
3726
3727
1.04k
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
3728
1.04k
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
3729
631
    zend_string_release_ex(name, 0);
3730
631
    return FAILURE;
3731
631
  }
3732
3733
409
  if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
3734
1
    zend_string_release_ex(name, 0);
3735
1
    zval_ptr_dtor(&result->u.constant);
3736
1
    ZVAL_TRUE(&result->u.constant);
3737
1
    result->op_type = IS_CONST;
3738
1
    return SUCCESS;
3739
1
  }
3740
3741
408
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
3742
408
  opline->op1_type = IS_CONST;
3743
408
  LITERAL_STR(opline->op1, name);
3744
408
  opline->extended_value = zend_alloc_cache_slot();
3745
3746
408
  return SUCCESS;
3747
408
}
3748
/* }}} */
3749
3750
zend_result zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
3751
2.65k
{
3752
3753
2.65k
  if (args->children == 1 &&
3754
2.64k
      args->child[0]->kind == ZEND_AST_ZVAL &&
3755
2.23k
      Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
3756
3757
2.22k
    zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
3758
3759
2.22k
    result->op_type = IS_CONST;
3760
2.22k
    ZVAL_CHAR(&result->u.constant, c);
3761
2.22k
    return SUCCESS;
3762
434
  } else {
3763
434
    return FAILURE;
3764
434
  }
3765
2.65k
}
3766
/* }}} */
3767
3768
zend_result zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
3769
542
{
3770
542
  if (args->children == 1 &&
3771
538
      args->child[0]->kind == ZEND_AST_ZVAL &&
3772
90
      Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
3773
3774
1
    result->op_type = IS_CONST;
3775
1
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
3776
1
    return SUCCESS;
3777
541
  } else {
3778
541
    return FAILURE;
3779
541
  }
3780
542
}
3781
/* }}} */
3782
3783
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
3784
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
3785
 * directly or indirectly recursive function calls. */
3786
1.78M
static zend_bool fbc_is_finalized(zend_function *fbc) {
3787
1.78M
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
3788
1.78M
}
3789
3790
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
3791
9.88k
{
3792
9.88k
  zend_string *name, *lcname;
3793
9.88k
  zend_function *fbc;
3794
9.88k
  zend_op *opline;
3795
3796
9.88k
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
3797
5.36k
    return FAILURE;
3798
5.36k
  }
3799
3800
4.51k
  name = zend_ast_get_str(name_ast);
3801
4.51k
  lcname = zend_string_tolower(name);
3802
3803
4.51k
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
3804
4.51k
  if (!fbc || !fbc_is_finalized(fbc)
3805
3.55k
   || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
3806
3.55k
   || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
3807
3.55k
   || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
3808
965
  ) {
3809
965
    zend_string_release_ex(lcname, 0);
3810
965
    return FAILURE;
3811
965
  }
3812
3813
3.55k
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
3814
3.55k
  opline->extended_value = num_args;
3815
3.55k
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
3816
3.55k
  opline->op2_type = IS_CONST;
3817
3.55k
  LITERAL_STR(opline->op2, lcname);
3818
3.55k
  opline->result.num = zend_alloc_cache_slot();
3819
3820
3.55k
  return SUCCESS;
3821
3.55k
}
3822
/* }}} */
3823
3824
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
3825
9.88k
{
3826
9.88k
  zend_op *opline;
3827
9.88k
  znode name_node;
3828
3829
9.88k
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
3830
3.55k
    return;
3831
3.55k
  }
3832
3833
6.33k
  zend_compile_expr(&name_node, name_ast);
3834
3835
6.33k
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
3836
6.33k
  opline->op1_type = IS_CONST;
3837
6.33k
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
3838
6.33k
  opline->extended_value = num_args;
3839
6.33k
}
3840
/* }}} */
3841
3842
/* cufa = call_user_func_array */
3843
zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
3844
5.22k
{
3845
5.22k
  znode arg_node;
3846
3847
5.22k
  if (args->children != 2) {
3848
81
    return FAILURE;
3849
81
  }
3850
3851
5.14k
  zend_compile_init_user_func(args->child[0], 0, lcname);
3852
5.14k
  if (args->child[1]->kind == ZEND_AST_CALL
3853
224
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
3854
219
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
3855
219
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
3856
219
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
3857
219
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
3858
219
    zend_bool is_fully_qualified;
3859
219
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
3860
3861
219
    if (zend_string_equals_literal_ci(name, "array_slice")
3862
0
     && list->children == 3
3863
0
     && list->child[1]->kind == ZEND_AST_ZVAL) {
3864
0
      zval *zv = zend_ast_get_zval(list->child[1]);
3865
3866
0
      if (Z_TYPE_P(zv) == IS_LONG
3867
0
       && Z_LVAL_P(zv) >= 0
3868
0
       && Z_LVAL_P(zv) <= 0x7fffffff) {
3869
0
        zend_op *opline;
3870
0
        znode len_node;
3871
3872
0
        zend_compile_expr(&arg_node, list->child[0]);
3873
0
        zend_compile_expr(&len_node, list->child[2]);
3874
0
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
3875
0
        opline->extended_value = Z_LVAL_P(zv);
3876
0
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3877
0
        zend_string_release_ex(name, 0);
3878
0
        return SUCCESS;
3879
0
      }
3880
219
    }
3881
219
    zend_string_release_ex(name, 0);
3882
219
  }
3883
5.14k
  zend_compile_expr(&arg_node, args->child[1]);
3884
5.14k
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
3885
5.14k
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3886
5.14k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3887
3888
5.14k
  return SUCCESS;
3889
5.14k
}
3890
/* }}} */
3891
3892
/* cuf = call_user_func */
3893
zend_result zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
3894
4.77k
{
3895
4.77k
  uint32_t i;
3896
3897
4.77k
  if (args->children < 1) {
3898
32
    return FAILURE;
3899
32
  }
3900
3901
4.74k
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
3902
18.3k
  for (i = 1; i < args->children; ++i) {
3903
13.5k
    zend_ast *arg_ast = args->child[i];
3904
13.5k
    znode arg_node;
3905
13.5k
    zend_op *opline;
3906
3907
13.5k
    zend_compile_expr(&arg_node, arg_ast);
3908
3909
13.5k
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
3910
13.5k
    opline->op2.num = i;
3911
13.5k
    opline->result.var = EX_NUM_TO_VAR(i - 1);
3912
13.5k
  }
3913
4.74k
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3914
3915
4.74k
  return SUCCESS;
3916
4.74k
}
3917
/* }}} */
3918
3919
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
3920
35.1k
{
3921
35.1k
  if (EG(assertions) >= 0) {
3922
35.1k
    znode name_node;
3923
35.1k
    zend_op *opline;
3924
35.1k
    uint32_t check_op_number = get_next_op_number();
3925
3926
35.1k
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
3927
3928
35.1k
    if (fbc && fbc_is_finalized(fbc)) {
3929
34.8k
      name_node.op_type = IS_CONST;
3930
34.8k
      ZVAL_STR_COPY(&name_node.u.constant, name);
3931
3932
34.8k
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3933
307
    } else {
3934
307
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
3935
307
      opline->op2_type = IS_CONST;
3936
307
      opline->op2.constant = zend_add_ns_func_name_literal(name);
3937
307
    }
3938
35.1k
    opline->result.num = zend_alloc_cache_slot();
3939
3940
35.1k
    if (args->children == 1 &&
3941
27.4k
        (args->child[0]->kind != ZEND_AST_ZVAL ||
3942
27.4k
         Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
3943
      /* add "assert(condition) as assertion message */
3944
27.4k
      zend_ast_list_add((zend_ast*)args,
3945
27.4k
        zend_ast_create_zval_from_str(
3946
27.4k
          zend_ast_export("assert(", args->child[0], ")")));
3947
27.4k
    }
3948
3949
35.1k
    zend_compile_call_common(result, (zend_ast*)args, fbc);
3950
3951
35.1k
    opline = &CG(active_op_array)->opcodes[check_op_number];
3952
35.1k
    opline->op2.opline_num = get_next_op_number();
3953
35.1k
    SET_NODE(opline->result, result);
3954
0
  } else {
3955
0
    if (!fbc) {
3956
0
      zend_string_release_ex(name, 0);
3957
0
    }
3958
0
    result->op_type = IS_CONST;
3959
0
    ZVAL_TRUE(&result->u.constant);
3960
0
  }
3961
35.1k
}
3962
/* }}} */
3963
3964
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
3965
1.59k
{
3966
1.59k
  zend_bool strict = 0;
3967
1.59k
  znode array, needly;
3968
1.59k
  zend_op *opline;
3969
3970
1.59k
  if (args->children == 3) {
3971
379
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
3972
0
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
3973
379
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
3974
323
      zval value;
3975
323
      zend_ast *name_ast = args->child[2]->child[0];
3976
323
      zend_bool is_fully_qualified;
3977
323
      zend_string *resolved_name = zend_resolve_const_name(
3978
323
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
3979
3980
323
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
3981
26
        zend_string_release_ex(resolved_name, 0);
3982
26
        return FAILURE;
3983
26
      }
3984
3985
297
      zend_string_release_ex(resolved_name, 0);
3986
297
      strict = zend_is_true(&value);
3987
297
      zval_ptr_dtor(&value);
3988
56
    } else {
3989
56
      return FAILURE;
3990
56
    }
3991
1.21k
  } else if (args->children != 2) {
3992
64
    return FAILURE;
3993
64
  }
3994
3995
1.45k
  if (args->child[1]->kind != ZEND_AST_ARRAY
3996
1.32k
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
3997
1.32k
    return FAILURE;
3998
1.32k
  }
3999
4000
124
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4001
124
    zend_bool ok = 1;
4002
124
    zval *val, tmp;
4003
124
    HashTable *src = Z_ARRVAL(array.u.constant);
4004
124
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4005
4006
124
    ZVAL_TRUE(&tmp);
4007
4008
124
    if (strict) {
4009
30
      ZEND_HASH_FOREACH_VAL(src, val) {
4010
10
        if (Z_TYPE_P(val) == IS_STRING) {
4011
10
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4012
0
        } else if (Z_TYPE_P(val) == IS_LONG) {
4013
0
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4014
0
        } else {
4015
0
          zend_array_destroy(dst);
4016
0
          ok = 0;
4017
0
          break;
4018
0
        }
4019
10
      } ZEND_HASH_FOREACH_END();
4020
114
    } else {
4021
722
      ZEND_HASH_FOREACH_VAL(src, val) {
4022
304
        if (Z_TYPE_P(val) != IS_STRING
4023
301
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4024
4
          zend_array_destroy(dst);
4025
4
          ok = 0;
4026
4
          break;
4027
4
        }
4028
300
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4029
300
      } ZEND_HASH_FOREACH_END();
4030
114
    }
4031
4032
124
    zend_array_destroy(src);
4033
124
    if (!ok) {
4034
4
      return FAILURE;
4035
4
    }
4036
120
    Z_ARRVAL(array.u.constant) = dst;
4037
120
  }
4038
120
  array.op_type = IS_CONST;
4039
4040
120
  zend_compile_expr(&needly, args->child[0]);
4041
4042
120
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4043
120
  opline->extended_value = strict;
4044
4045
120
  return SUCCESS;
4046
124
}
4047
/* }}} */
4048
4049
zend_result zend_compile_func_count(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4050
11.8k
{
4051
11.8k
  znode arg_node;
4052
11.8k
  zend_op *opline;
4053
4054
11.8k
  if (args->children != 1) {
4055
41
    return FAILURE;
4056
41
  }
4057
4058
11.7k
  zend_compile_expr(&arg_node, args->child[0]);
4059
11.7k
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4060
11.7k
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4061
4062
11.7k
  return SUCCESS;
4063
11.7k
}
4064
/* }}} */
4065
4066
zend_result zend_compile_func_get_class(znode *result, zend_ast_list *args) /* {{{ */
4067
8.08k
{
4068
8.08k
  if (args->children == 0) {
4069
388
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4070
7.69k
  } else {
4071
7.69k
    znode arg_node;
4072
4073
7.69k
    if (args->children != 1) {
4074
18
      return FAILURE;
4075
18
    }
4076
4077
7.67k
    zend_compile_expr(&arg_node, args->child[0]);
4078
7.67k
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4079
7.67k
  }
4080
8.06k
  return SUCCESS;
4081
8.08k
}
4082
/* }}} */
4083
4084
zend_result zend_compile_func_get_called_class(znode *result, zend_ast_list *args) /* {{{ */
4085
912
{
4086
912
  if (args->children != 0) {
4087
378
    return FAILURE;
4088
378
  }
4089
4090
534
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4091
534
  return SUCCESS;
4092
534
}
4093
/* }}} */
4094
4095
zend_result zend_compile_func_gettype(znode *result, zend_ast_list *args) /* {{{ */
4096
4.25k
{
4097
4.25k
  znode arg_node;
4098
4099
4.25k
  if (args->children != 1) {
4100
38
    return FAILURE;
4101
38
  }
4102
4103
4.21k
  zend_compile_expr(&arg_node, args->child[0]);
4104
4.21k
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4105
4.21k
  return SUCCESS;
4106
4.21k
}
4107
/* }}} */
4108
4109
zend_result zend_compile_func_num_args(znode *result, zend_ast_list *args) /* {{{ */
4110
1.53k
{
4111
1.53k
  if (CG(active_op_array)->function_name && args->children == 0) {
4112
1.09k
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4113
1.09k
    return SUCCESS;
4114
440
  } else {
4115
440
    return FAILURE;
4116
440
  }
4117
1.53k
}
4118
/* }}} */
4119
4120
zend_result zend_compile_func_get_args(znode *result, zend_ast_list *args) /* {{{ */
4121
1.69k
{
4122
1.69k
  if (CG(active_op_array)->function_name && args->children == 0) {
4123
1.19k
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4124
1.19k
    return SUCCESS;
4125
505
  } else {
4126
505
    return FAILURE;
4127
505
  }
4128
1.69k
}
4129
/* }}} */
4130
4131
zend_result zend_compile_func_array_key_exists(znode *result, zend_ast_list *args) /* {{{ */
4132
2.38k
{
4133
2.38k
  znode subject, needle;
4134
4135
2.38k
  if (args->children != 2) {
4136
67
    return FAILURE;
4137
67
  }
4138
4139
2.31k
  zend_compile_expr(&needle, args->child[0]);
4140
2.31k
  zend_compile_expr(&subject, args->child[1]);
4141
4142
2.31k
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4143
2.31k
  return SUCCESS;
4144
2.31k
}
4145
/* }}} */
4146
4147
zend_result zend_compile_func_array_slice(znode *result, zend_ast_list *args) /* {{{ */
4148
2.02k
{
4149
2.02k
  if (CG(active_op_array)->function_name
4150
1.94k
   && args->children == 2
4151
1.29k
   && args->child[0]->kind == ZEND_AST_CALL
4152
415
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4153
411
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4154
411
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4155
411
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4156
4157
380
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4158
380
    zend_bool is_fully_qualified;
4159
380
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4160
380
    zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4161
380
    zval *zv = zend_ast_get_zval(args->child[1]);
4162
380
    znode first;
4163
4164
380
    if (zend_string_equals_literal_ci(name, "func_get_args")
4165
266
     && list->children == 0
4166
259
     && Z_TYPE_P(zv) == IS_LONG
4167
256
     && Z_LVAL_P(zv) >= 0) {
4168
256
      first.op_type = IS_CONST;
4169
256
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4170
256
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4171
256
      zend_string_release_ex(name, 0);
4172
256
      return SUCCESS;
4173
256
    }
4174
124
    zend_string_release_ex(name, 0);
4175
124
  }
4176
1.76k
  return FAILURE;
4177
2.02k
}
4178
/* }}} */
4179
4180
zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
4181
1.74M
{
4182
1.74M
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
4183
0
    return FAILURE;
4184
0
  }
4185
4186
1.74M
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
4187
    /* If the function is part of disabled_functions, it may be redeclared as a userland
4188
     * function with a different implementation. Don't use the VM builtin in that case. */
4189
349k
    return FAILURE;
4190
349k
  }
4191
4192
1.39M
  if (zend_args_contain_unpack_or_named(args)) {
4193
9.06k
    return FAILURE;
4194
9.06k
  }
4195
4196
1.38M
  if (zend_string_equals_literal(lcname, "strlen")) {
4197
9.97k
    return zend_compile_func_strlen(result, args);
4198
1.37M
  } else if (zend_string_equals_literal(lcname, "is_null")) {
4199
721
    return zend_compile_func_typecheck(result, args, IS_NULL);
4200
1.37M
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
4201
432
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
4202
1.37M
  } else if (zend_string_equals_literal(lcname, "is_long")
4203
1.37M
    || zend_string_equals_literal(lcname, "is_int")
4204
1.36M
    || zend_string_equals_literal(lcname, "is_integer")
4205
3.11k
  ) {
4206
3.11k
    return zend_compile_func_typecheck(result, args, IS_LONG);
4207
1.36M
  } else if (zend_string_equals_literal(lcname, "is_float")
4208
1.36M
    || zend_string_equals_literal(lcname, "is_double")
4209
12.6k
  ) {
4210
12.6k
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
4211
1.35M
  } else if (zend_string_equals_literal(lcname, "is_string")) {
4212
60
    return zend_compile_func_typecheck(result, args, IS_STRING);
4213
1.35M
  } else if (zend_string_equals_literal(lcname, "is_array")) {
4214
2.13k
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
4215
1.35M
  } else if (zend_string_equals_literal(lcname, "is_object")) {
4216
3.08k
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
4217
1.35M
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
4218
43
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
4219
1.35M
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
4220
423
    return zend_compile_func_is_scalar(result, args);
4221
1.34M
  } else if (zend_string_equals_literal(lcname, "boolval")) {
4222
411
    return zend_compile_func_cast(result, args, _IS_BOOL);
4223
1.34M
  } else if (zend_string_equals_literal(lcname, "intval")) {
4224
1.38k
    return zend_compile_func_cast(result, args, IS_LONG);
4225
1.34M
  } else if (zend_string_equals_literal(lcname, "floatval")
4226
1.34M
    || zend_string_equals_literal(lcname, "doubleval")
4227
119
  ) {
4228
119
    return zend_compile_func_cast(result, args, IS_DOUBLE);
4229
1.34M
  } else if (zend_string_equals_literal(lcname, "strval")) {
4230
127
    return zend_compile_func_cast(result, args, IS_STRING);
4231
1.34M
  } else if (zend_string_equals_literal(lcname, "defined")) {
4232
1.52k
    return zend_compile_func_defined(result, args);
4233
1.34M
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
4234
2.65k
    return zend_compile_func_chr(result, args);
4235
1.34M
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
4236
542
    return zend_compile_func_ord(result, args);
4237
1.34M
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
4238
5.22k
    return zend_compile_func_cufa(result, args, lcname);
4239
1.33M
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
4240
4.77k
    return zend_compile_func_cuf(result, args, lcname);
4241
1.33M
  } else if (zend_string_equals_literal(lcname, "in_array")) {
4242
1.59k
    return zend_compile_func_in_array(result, args);
4243
1.33M
  } else if (zend_string_equals_literal(lcname, "count")
4244
1.31M
      || zend_string_equals_literal(lcname, "sizeof")) {
4245
11.8k
    return zend_compile_func_count(result, args, lcname);
4246
1.31M
  } else if (zend_string_equals_literal(lcname, "get_class")) {
4247
8.08k
    return zend_compile_func_get_class(result, args);
4248
1.31M
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
4249
912
    return zend_compile_func_get_called_class(result, args);
4250
1.31M
  } else if (zend_string_equals_literal(lcname, "gettype")) {
4251
4.25k
    return zend_compile_func_gettype(result, args);
4252
1.30M
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
4253
1.53k
    return zend_compile_func_num_args(result, args);
4254
1.30M
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
4255
1.69k
    return zend_compile_func_get_args(result, args);
4256
1.30M
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
4257
2.02k
    return zend_compile_func_array_slice(result, args);
4258
1.30M
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
4259
2.38k
    return zend_compile_func_array_key_exists(result, args);
4260
1.29M
  } else {
4261
1.29M
    return FAILURE;
4262
1.29M
  }
4263
1.38M
}
4264
/* }}} */
4265
4266
void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
4267
2.23M
{
4268
2.23M
  zend_ast *name_ast = ast->child[0];
4269
2.23M
  zend_ast *args_ast = ast->child[1];
4270
4271
2.23M
  znode name_node;
4272
4273
2.23M
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4274
45.2k
    zend_compile_expr(&name_node, name_ast);
4275
45.2k
    zend_compile_dynamic_call(result, &name_node, args_ast);
4276
45.2k
    return;
4277
45.2k
  }
4278
4279
2.19M
  {
4280
2.19M
    zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
4281
2.19M
    if (runtime_resolution) {
4282
15.5k
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
4283
307
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
4284
15.2k
      } else {
4285
15.2k
        zend_compile_ns_call(result, &name_node, args_ast);
4286
15.2k
      }
4287
15.5k
      return;
4288
15.5k
    }
4289
2.17M
  }
4290
4291
2.17M
  {
4292
2.17M
    zval *name = &name_node.u.constant;
4293
2.17M
    zend_string *lcname;
4294
2.17M
    zend_function *fbc;
4295
2.17M
    zend_op *opline;
4296
4297
2.17M
    lcname = zend_string_tolower(Z_STR_P(name));
4298
2.17M
    fbc = zend_hash_find_ptr(CG(function_table), lcname);
4299
4300
    /* Special assert() handling should apply independently of compiler flags. */
4301
2.17M
    if (fbc && zend_string_equals_literal(lcname, "assert")) {
4302
34.8k
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
4303
34.8k
      zend_string_release(lcname);
4304
34.8k
      zval_ptr_dtor(&name_node.u.constant);
4305
34.8k
      return;
4306
34.8k
    }
4307
4308
2.14M
    if (!fbc || !fbc_is_finalized(fbc)
4309
1.74M
     || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
4310
1.74M
     || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
4311
1.74M
     || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) && fbc->op_array.filename != CG(active_op_array)->filename)
4312
402k
    ) {
4313
402k
      zend_string_release_ex(lcname, 0);
4314
402k
      zend_compile_dynamic_call(result, &name_node, args_ast);
4315
402k
      return;
4316
402k
    }
4317
4318
1.74M
    if (zend_try_compile_special_func(result, lcname,
4319
1.74M
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
4320
76.5k
    ) {
4321
76.5k
      zend_string_release_ex(lcname, 0);
4322
76.5k
      zval_ptr_dtor(&name_node.u.constant);
4323
76.5k
      return;
4324
76.5k
    }
4325
4326
1.66M
    zval_ptr_dtor(&name_node.u.constant);
4327
1.66M
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
4328
4329
1.66M
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4330
1.66M
    opline->result.num = zend_alloc_cache_slot();
4331
4332
1.66M
    zend_compile_call_common(result, args_ast, fbc);
4333
1.66M
  }
4334
1.66M
}
4335
/* }}} */
4336
4337
void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
4338
442k
{
4339
442k
  zend_ast *obj_ast = ast->child[0];
4340
442k
  zend_ast *method_ast = ast->child[1];
4341
442k
  zend_ast *args_ast = ast->child[2];
4342
4343
442k
  znode obj_node, method_node;
4344
442k
  zend_op *opline;
4345
442k
  zend_function *fbc = NULL;
4346
442k
  zend_bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
4347
4348
442k
  if (is_this_fetch(obj_ast)) {
4349
10.0k
    if (this_guaranteed_exists()) {
4350
10.0k
      obj_node.op_type = IS_UNUSED;
4351
75
    } else {
4352
75
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
4353
75
    }
4354
10.0k
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
4355
4356
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
4357
     * check for a nullsafe access. */
4358
432k
  } else {
4359
432k
    zend_short_circuiting_mark_inner(obj_ast);
4360
432k
    zend_compile_expr(&obj_node, obj_ast);
4361
432k
    if (nullsafe) {
4362
11.3k
      zend_emit_jmp_null(&obj_node);
4363
11.3k
    }
4364
432k
  }
4365
4366
442k
  zend_compile_expr(&method_node, method_ast);
4367
442k
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
4368
4369
442k
  if (method_node.op_type == IS_CONST) {
4370
438k
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
4371
44
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
4372
44
    }
4373
4374
438k
    opline->op2_type = IS_CONST;
4375
438k
    opline->op2.constant = zend_add_func_name_literal(
4376
438k
      Z_STR(method_node.u.constant));
4377
438k
    opline->result.num = zend_alloc_cache_slots(2);
4378
4.22k
  } else {
4379
4.22k
    SET_NODE(opline->op2, &method_node);
4380
4.22k
  }
4381
4382
  /* Check if this calls a known method on $this */
4383
442k
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
4384
9.59k
      CG(active_class_entry) && zend_is_scope_known()) {
4385
9.46k
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
4386
9.46k
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
4387
4388
    /* We only know the exact method that is being called if it is either private or final.
4389
     * Otherwise an overriding method in a child class may be called. */
4390
9.46k
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
4391
6.67k
      fbc = NULL;
4392
6.67k
    }
4393
9.46k
  }
4394
4395
442k
  zend_compile_call_common(result, args_ast, fbc);
4396
442k
}
4397
/* }}} */
4398
4399
static zend_bool zend_is_constructor(zend_string *name) /* {{{ */
4400
34.6k
{
4401
34.6k
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
4402
34.6k
}
4403
/* }}} */
4404
4405
static zend_function *zend_get_compatible_func_or_null(zend_class_entry *ce, zend_string *lcname) /* {{{ */
4406
17.0k
{
4407
17.0k
  zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
4408
17.0k
  if (!fbc || (fbc->common.fn_flags & ZEND_ACC_PUBLIC) || ce == CG(active_class_entry)) {
4409
16.0k
    return fbc;
4410
16.0k
  }
4411
4412
1.08k
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
4413
581
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
4414
581
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
4415
254
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
4416
0
    return fbc;
4417
0
  }
4418
4419
1.08k
  return NULL;
4420
1.08k
}
4421
/* }}} */
4422
4423
void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
4424
36.9k
{
4425
36.9k
  zend_ast *class_ast = ast->child[0];
4426
36.9k
  zend_ast *method_ast = ast->child[1];
4427
36.9k
  zend_ast *args_ast = ast->child[2];
4428
4429
36.9k
  znode class_node, method_node;
4430
36.9k
  zend_op *opline;
4431
36.9k
  zend_function *fbc = NULL;
4432
4433
36.9k
  zend_short_circuiting_mark_inner(class_ast);
4434
36.9k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
4435
4436
36.9k
  zend_compile_expr(&method_node, method_ast);
4437
4438
36.9k
  if (method_node.op_type == IS_CONST) {
4439
33.6k
    zval *name = &method_node.u.constant;
4440
33.6k
    if (Z_TYPE_P(name) != IS_STRING) {
4441
5
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
4442
5
    }
4443
33.6k
    if (zend_is_constructor(Z_STR_P(name))) {
4444
1.51k
      zval_ptr_dtor(name);
4445
1.51k
      method_node.op_type = IS_UNUSED;
4446
1.51k
    }
4447
33.6k
  }
4448
4449
36.8k
  opline = get_next_op();
4450
36.8k
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4451
4452
36.8k
  zend_set_class_name_op1(opline, &class_node);
4453
4454
36.8k
  if (method_node.op_type == IS_CONST) {
4455
32.1k
    opline->op2_type = IS_CONST;
4456
32.1k
    opline->op2.constant = zend_add_func_name_literal(
4457
32.1k
      Z_STR(method_node.u.constant));
4458
32.1k
    opline->result.num = zend_alloc_cache_slots(2);
4459
4.73k
  } else {
4460
4.73k
    if (opline->op1_type == IS_CONST) {
4461
813
      opline->result.num = zend_alloc_cache_slot();
4462
813
    }
4463
4.73k
    SET_NODE(opline->op2, &method_node);
4464
4.73k
  }
4465
4466
  /* Check if we already know which method we're calling */
4467
36.8k
  if (opline->op2_type == IS_CONST) {
4468
32.1k
    zend_class_entry *ce = NULL;
4469
32.1k
    if (opline->op1_type == IS_CONST) {
4470
23.4k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
4471
23.4k
      ce = zend_hash_find_ptr(CG(class_table), lcname);
4472
23.4k
      if (!ce && CG(active_class_entry)
4473
1.66k
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
4474
952
        ce = CG(active_class_entry);
4475
952
      }
4476
8.72k
    } else if (opline->op1_type == IS_UNUSED
4477
7.30k
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
4478
991
        && zend_is_scope_known()) {
4479
877
      ce = CG(active_class_entry);
4480
877
    }
4481
32.1k
    if (ce) {
4482
17.0k
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
4483
17.0k
      fbc = zend_get_compatible_func_or_null(ce, lcname);
4484
17.0k
    }
4485
32.1k
  }
4486
4487
36.8k
  zend_compile_call_common(result, args_ast, fbc);
4488
36.8k
}
4489
/* }}} */
4490
4491
void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel);
4492
4493
void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
4494
562k
{
4495
562k
  zend_ast *class_ast = ast->child[0];
4496
562k
  zend_ast *args_ast = ast->child[1];
4497
4498
562k
  znode class_node, ctor_result;
4499
562k
  zend_op *opline;
4500
4501
562k
  if (class_ast->kind == ZEND_AST_CLASS) {
4502
    /* anon class declaration */
4503
22.2k
     zend_compile_class_decl(&class_node, class_ast, 0);
4504
539k
  } else {
4505
539k
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
4506
539k
  }
4507
4508
562k
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
4509
4510
562k
  if (class_node.op_type == IS_CONST) {
4511
536k
    opline->op1_type = IS_CONST;
4512
536k
    opline->op1.constant = zend_add_class_name_literal(
4513
536k
      Z_STR(class_node.u.constant));
4514
536k
    opline->op2.num = zend_alloc_cache_slot();
4515
25.9k
  } else {
4516
25.9k
    SET_NODE(opline->op1, &class_node);
4517
25.9k
  }
4518
4519
562k
  zend_compile_call_common(&ctor_result, args_ast, NULL);
4520
562k
  zend_do_free(&ctor_result);
4521
562k
}
4522
/* }}} */
4523
4524
void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
4525
3.55k
{
4526
3.55k
  zend_ast *obj_ast = ast->child[0];
4527
4528
3.55k
  znode obj_node;
4529
3.55k
  zend_compile_expr(&obj_node, obj_ast);
4530
4531
3.55k
  zend_emit_op_tmp(result, ZEND_CLONE, &obj_node, NULL);
4532
3.55k
}
4533
/* }}} */
4534
4535
void zend_compile_global_var(zend_ast *ast) /* {{{ */
4536
6.36k
{
4537
6.36k
  zend_ast *var_ast = ast->child[0];
4538
6.36k
  zend_ast *name_ast = var_ast->child[0];
4539
4540
6.36k
  znode name_node, result;
4541
4542
6.36k
  zend_compile_expr(&name_node, name_ast);
4543
6.36k
  if (name_node.op_type == IS_CONST) {
4544
6.19k
    convert_to_string(&name_node.u.constant);
4545
6.19k
  }
4546
4547
6.36k
  if (is_this_fetch(var_ast)) {
4548
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
4549
6.35k
  } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
4550
6.17k
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
4551
6.17k
    opline->extended_value = zend_alloc_cache_slot();
4552
178
  } else {
4553
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
4554
     * to not free the name_node operand, so it can be reused in the following
4555
     * ASSIGN_REF, which then frees it. */
4556
178
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
4557
178
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
4558
4559
178
    if (name_node.op_type == IS_CONST) {
4560
2
      zend_string_addref(Z_STR(name_node.u.constant));
4561
2
    }
4562
4563
178
    zend_emit_assign_ref_znode(
4564
178
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
4565
178
      &result
4566
178
    );
4567
178
  }
4568
6.36k
}
4569
/* }}} */
4570
4571
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
4572
25.7k
{
4573
25.7k
  zend_op *opline;
4574
25.7k
  if (!CG(active_op_array)->static_variables) {
4575
3.98k
    if (CG(active_op_array)->scope) {
4576
1.17k
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
4577
1.17k
    }
4578
3.98k
    CG(active_op_array)->static_variables = zend_new_array(8);
4579
3.98k
  }
4580
4581
25.7k
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
4582
4583
25.7k
  if (zend_string_equals_literal(var_name, "this")) {
4584
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
4585
19
  }
4586
4587
25.7k
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
4588
25.7k
  opline->op1_type = IS_CV;
4589
25.7k
  opline->op1.var = lookup_cv(var_name);
4590
25.7k
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
4591
25.7k
}
4592
/* }}} */
4593
4594
void zend_compile_static_var(zend_ast *ast) /* {{{ */
4595
5.06k
{
4596
5.06k
  zend_ast *var_ast = ast->child[0];
4597
5.06k
  zend_ast *value_ast = ast->child[1];
4598
5.06k
  zval value_zv;
4599
4600
5.06k
  if (value_ast) {
4601
3.76k
    zend_const_expr_to_zval(&value_zv, value_ast);
4602
1.30k
  } else {
4603
1.30k
    ZVAL_NULL(&value_zv);
4604
1.30k
  }
4605
4606
5.06k
  zend_compile_static_var_common(zend_ast_get_str(var_ast), &value_zv, ZEND_BIND_REF);
4607
5.06k
}
4608
/* }}} */
4609
4610
void zend_compile_unset(zend_ast *ast) /* {{{ */
4611
61.9k
{
4612
61.9k
  zend_ast *var_ast = ast->child[0];
4613
61.9k
  znode var_node;
4614
61.9k
  zend_op *opline;
4615
4616
61.9k
  zend_ensure_writable_variable(var_ast);
4617
4618
61.9k
  switch (var_ast->kind) {
4619
26.9k
    case ZEND_AST_VAR:
4620
26.9k
      if (is_this_fetch(var_ast)) {
4621
38
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
4622
26.9k
      } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
4623
25.0k
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
4624
1.87k
      } else {
4625
1.87k
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
4626
1.87k
        opline->opcode = ZEND_UNSET_VAR;
4627
1.87k
      }
4628
26.9k
      return;
4629
18.9k
    case ZEND_AST_DIM:
4630
18.9k
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET);
4631
18.9k
      opline->opcode = ZEND_UNSET_DIM;
4632
18.9k
      return;
4633
15.6k
    case ZEND_AST_PROP:
4634
15.6k
    case ZEND_AST_NULLSAFE_PROP:
4635
15.6k
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, 0);
4636
15.6k
      opline->opcode = ZEND_UNSET_OBJ;
4637
15.6k
      return;
4638
218
    case ZEND_AST_STATIC_PROP:
4639
218
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, 0, 0);
4640
218
      opline->opcode = ZEND_UNSET_STATIC_PROP;
4641
218
      return;
4642
0
    EMPTY_SWITCH_DEFAULT_CASE()
4643
61.9k
  }
4644
61.9k
}
4645
/* }}} */
4646
4647
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
4648
180k
{
4649
180k
  zend_loop_var *base;
4650
180k
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
4651
4652
180k
  if (!loop_var) {
4653
18.5k
    return 1;
4654
18.5k
  }
4655
162k
  base = zend_stack_base(&CG(loop_var_stack));
4656
169k
  for (; loop_var >= base; loop_var--) {
4657
169k
    if (loop_var->opcode == ZEND_FAST_CALL) {
4658
1.97k
      zend_op *opline = get_next_op();
4659
4660
1.97k
      opline->opcode = ZEND_FAST_CALL;
4661
1.97k
      opline->result_type = IS_TMP_VAR;
4662
1.97k
      opline->result.var = loop_var->var_num;
4663
1.97k
      if (return_value) {
4664
997
        SET_NODE(opline->op2, return_value);
4665
997
      }
4666
1.97k
      opline->op1.num = loop_var->try_catch_offset;
4667
167k
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
4668
844
      zend_op *opline = get_next_op();
4669
844
      opline->opcode = ZEND_DISCARD_EXCEPTION;
4670
844
      opline->op1_type = IS_TMP_VAR;
4671
844
      opline->op1.var = loop_var->var_num;
4672
166k
    } else if (loop_var->opcode == ZEND_RETURN) {
4673
      /* Stack separator */
4674
148k
      break;
4675
17.3k
    } else if (depth <= 1) {
4676
12.8k
      return 1;
4677
4.54k
    } else if (loop_var->opcode == ZEND_NOP) {
4678
      /* Loop doesn't have freeable variable */
4679
2.40k
      depth--;
4680
2.13k
    } else {
4681
2.13k
      zend_op *opline;
4682
4683
2.13k
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
4684
2.13k
      opline = get_next_op();
4685
2.13k
      opline->opcode = loop_var->opcode;
4686
2.13k
      opline->op1_type = loop_var->var_type;
4687
2.13k
      opline->op1.var = loop_var->var_num;
4688
2.13k
      opline->extended_value = ZEND_FREE_ON_RETURN;
4689
2.13k
      depth--;
4690
2.13k
      }
4691
169k
  }
4692
149k
  return (depth == 0);
4693
162k
}
4694
/* }}} */
4695
4696
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
4697
168k
{
4698
168k
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
4699
168k
}
4700
/* }}} */
4701
4702
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
4703
186
{
4704
186
  zend_loop_var *base;
4705
186
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
4706
4707
186
  if (!loop_var) {
4708
0
    return 0;
4709
0
  }
4710
186
  base = zend_stack_base(&CG(loop_var_stack));
4711
212
  for (; loop_var >= base; loop_var--) {
4712
212
    if (loop_var->opcode == ZEND_FAST_CALL) {
4713
160
      return 1;
4714
52
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
4715
26
    } else if (loop_var->opcode == ZEND_RETURN) {
4716
      /* Stack separator */
4717
26
      return 0;
4718
0
    } else if (depth <= 1) {
4719
0
      return 0;
4720
0
    } else {
4721
0
      depth--;
4722
0
      }
4723
212
  }
4724
0
  return 0;
4725
186
}
4726
/* }}} */
4727
4728
static bool zend_has_finally(void) /* {{{ */
4729
186
{
4730
186
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
4731
186
}
4732
/* }}} */
4733
4734
void zend_compile_return(zend_ast *ast) /* {{{ */
4735
166k
{
4736
166k
  zend_ast *expr_ast = ast->child[0];
4737
166k
  zend_bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
4738
166k
  zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
4739
4740
166k
  znode expr_node;
4741
166k
  zend_op *opline;
4742
4743
166k
  if (is_generator) {
4744
    /* For generators the by-ref flag refers to yields, not returns */
4745
2.45k
    by_ref = 0;
4746
2.45k
  }
4747
4748
166k
  if (!expr_ast) {
4749
1.98k
    expr_node.op_type = IS_CONST;
4750
1.98k
    ZVAL_NULL(&expr_node.u.constant);
4751
164k
  } else if (by_ref && zend_is_variable(expr_ast)) {
4752
10.3k
    if (zend_ast_is_short_circuited(expr_ast)) {
4753
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
4754
19
    }
4755
4756
10.2k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
4757
154k
  } else {
4758
154k
    zend_compile_expr(&expr_node, expr_ast);
4759
154k
  }
4760
4761
166k
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
4762
1.97k
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
4763
186
   && zend_has_finally()) {
4764
    /* Copy return value into temporary VAR to avoid modification in finally code */
4765
160
    if (by_ref) {
4766
77
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
4767
83
    } else {
4768
83
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
4769
83
    }
4770
160
  }
4771
4772
  /* Generator return types are handled separately */
4773
166k
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
4774
10.4k
    zend_emit_return_type_check(
4775
10.4k
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
4776
10.4k
  }
4777
4778
166k
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
4779
4780
153k
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
4781
166k
    &expr_node, NULL);
4782
4783
166k
  if (by_ref && expr_ast) {
4784
13.1k
    if (zend_is_call(expr_ast)) {
4785
956
      opline->extended_value = ZEND_RETURNS_FUNCTION;
4786
12.2k
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
4787
1.91k
      opline->extended_value = ZEND_RETURNS_VALUE;
4788
1.91k
    }
4789
13.1k
  }
4790
166k
}
4791
/* }}} */
4792
4793
void zend_compile_echo(zend_ast *ast) /* {{{ */
4794
713k
{
4795
713k
  zend_op *opline;
4796
713k
  zend_ast *expr_ast = ast->child[0];
4797
4798
713k
  znode expr_node;
4799
713k
  zend_compile_expr(&expr_node, expr_ast);
4800
4801
713k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
4802
713k
  opline->extended_value = 0;
4803
713k
}
4804
/* }}} */
4805
4806
void zend_compile_throw(znode *result, zend_ast *ast) /* {{{ */
4807
28.9k
{
4808
28.9k
  zend_ast *expr_ast = ast->child[0];
4809
4810
28.9k
  znode expr_node;
4811
28.9k
  zend_compile_expr(&expr_node, expr_ast);
4812
4813
28.9k
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
4814
28.9k
  if (result) {
4815
    /* Mark this as an "expression throw" for opcache. */
4816
890
    opline->extended_value = ZEND_THROW_IS_EXPR;
4817
890
    result->op_type = IS_CONST;
4818
890
    ZVAL_BOOL(&result->u.constant, 1);
4819
890
  }
4820
28.9k
}
4821
/* }}} */
4822
4823
void zend_compile_break_continue(zend_ast *ast) /* {{{ */
4824
13.0k
{
4825
13.0k
  zend_ast *depth_ast = ast->child[0];
4826
4827
13.0k
  zend_op *opline;
4828
13.0k
  zend_long depth;
4829
4830
13.0k
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
4831
4832
13.0k
  if (depth_ast) {
4833
1.81k
    zval *depth_zv;
4834
1.81k
    if (depth_ast->kind != ZEND_AST_ZVAL) {
4835
23
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
4836
23
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4837
23
    }
4838
4839
1.79k
    depth_zv = zend_ast_get_zval(depth_ast);
4840
1.79k
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
4841
39
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
4842
39
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4843
39
    }
4844
4845
1.75k
    depth = Z_LVAL_P(depth_zv);
4846
11.1k
  } else {
4847
11.1k
    depth = 1;
4848
11.1k
  }
4849
4850
12.9k
  if (CG(context).current_brk_cont == -1) {
4851
39
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
4852
39
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4853
12.9k
  } else {
4854
12.9k
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
4855
104
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
4856
53
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
4857
104
        depth, depth == 1 ? "" : "s");
4858
104
    }
4859
12.8k
  }
4860
4861
12.8k
  if (ast->kind == ZEND_AST_CONTINUE) {
4862
5.67k
    int d, cur = CG(context).current_brk_cont;
4863
6.16k
    for (d = depth - 1; d > 0; d--) {
4864
489
      cur = CG(context).brk_cont_array[cur].parent;
4865
489
      ZEND_ASSERT(cur != -1);
4866
489
    }
4867
4868
5.67k
    if (CG(context).brk_cont_array[cur].is_switch) {
4869
365
      if (depth == 1) {
4870
250
        zend_error(E_WARNING,
4871
250
          "\"continue\" targeting switch is equivalent to \"break\". " \
4872
250
          "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4873
250
          depth + 1);
4874
115
      } else {
4875
115
        zend_error(E_WARNING,
4876
115
          "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
4877
115
          "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
4878
115
          depth, depth, depth + 1);
4879
115
      }
4880
365
    }
4881
5.67k
  }
4882
4883
7.13k
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
4884
12.8k
  opline->op1.num = CG(context).current_brk_cont;
4885
12.8k
  opline->op2.num = depth;
4886
12.8k
}
4887
/* }}} */
4888
4889
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
4890
1.71k
{
4891
1.71k
  zend_label *dest;
4892
1.71k
  int current, remove_oplines = opline->op1.num;
4893
1.71k
  zval *label;
4894
1.71k
  uint32_t opnum = opline - op_array->opcodes;
4895
4896
1.71k
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
4897
1.71k
  if (CG(context).labels == NULL ||
4898
1.68k
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
4899
92
  ) {
4900
92
    CG(in_compilation) = 1;
4901
92
    CG(active_op_array) = op_array;
4902
92
    CG(zend_lineno) = opline->lineno;
4903
92
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
4904
92
  }
4905
4906
1.61k
  zval_ptr_dtor_str(label);
4907
1.61k
  ZVAL_NULL(label);
4908
4909
1.61k
  current = opline->extended_value;
4910
2.13k
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
4911
597
    if (current == -1) {
4912
82
      CG(in_compilation) = 1;
4913
82
      CG(active_op_array) = op_array;
4914
82
      CG(zend_lineno) = opline->lineno;
4915
82
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
4916
82
    }
4917
515
    if (CG(context).brk_cont_array[current].start >= 0) {
4918
222
      remove_oplines--;
4919
222
    }
4920
515
  }
4921
4922
2.22k
  for (current = 0; current < op_array->last_try_catch; ++current) {
4923
916
    zend_try_catch_element *elem = &op_array->try_catch_array[current];
4924
916
    if (elem->try_op > opnum) {
4925
232
      break;
4926
232
    }
4927
684
    if (elem->finally_op && opnum < elem->finally_op - 1
4928
246
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
4929
147
    ) {
4930
147
      remove_oplines--;
4931
147
    }
4932
684
  }
4933
4934
1.53k
  opline->opcode = ZEND_JMP;
4935
1.53k
  opline->op1.opline_num = dest->opline_num;
4936
1.53k
  opline->extended_value = 0;
4937
1.53k
  SET_UNUSED(opline->op1);
4938
1.53k
  SET_UNUSED(opline->op2);
4939
1.53k
  SET_UNUSED(opline->result);
4940
4941
1.53k
  ZEND_ASSERT(remove_oplines >= 0);
4942
1.82k
  while (remove_oplines--) {
4943
291
    opline--;
4944
291
    MAKE_NOP(opline);
4945
291
    ZEND_VM_SET_OPCODE_HANDLER(opline);
4946
291
  }
4947
1.53k
}
4948
/* }}} */
4949
4950
void zend_compile_goto(zend_ast *ast) /* {{{ */
4951
1.78k
{
4952
1.78k
  zend_ast *label_ast = ast->child[0];
4953
1.78k
  znode label_node;
4954
1.78k
  zend_op *opline;
4955
1.78k
  uint32_t opnum_start = get_next_op_number();
4956
4957
1.78k
  zend_compile_expr(&label_node, label_ast);
4958
4959
  /* Label resolution and unwinding adjustments happen in pass two. */
4960
1.78k
  zend_handle_loops_and_finally(NULL);
4961
1.78k
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
4962
1.78k
  opline->op1.num = get_next_op_number() - opnum_start - 1;
4963
1.78k
  opline->extended_value = CG(context).current_brk_cont;
4964
1.78k
}
4965
/* }}} */
4966
4967
void zend_compile_label(zend_ast *ast) /* {{{ */
4968
11.1k
{
4969
11.1k
  zend_string *label = zend_ast_get_str(ast->child[0]);
4970
11.1k
  zend_label dest;
4971
4972
11.1k
  if (!CG(context).labels) {
4973
10.4k
    ALLOC_HASHTABLE(CG(context).labels);
4974
10.4k
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
4975
10.4k
  }
4976
4977
11.1k
  dest.brk_cont = CG(context).current_brk_cont;
4978
11.1k
  dest.opline_num = get_next_op_number();
4979
4980
11.1k
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
4981
48
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
4982
48
  }
4983
11.1k
}
4984
/* }}} */
4985
4986
void zend_compile_while(zend_ast *ast) /* {{{ */
4987
3.59k
{
4988
3.59k
  zend_ast *cond_ast = ast->child[0];
4989
3.59k
  zend_ast *stmt_ast = ast->child[1];
4990
3.59k
  znode cond_node;
4991
3.59k
  uint32_t opnum_start, opnum_jmp, opnum_cond;
4992
4993
3.59k
  opnum_jmp = zend_emit_jump(0);
4994
4995
3.59k
  zend_begin_loop(ZEND_NOP, NULL, 0);
4996
4997
3.59k
  opnum_start = get_next_op_number();
4998
3.59k
  zend_compile_stmt(stmt_ast);
4999
5000
3.59k
  opnum_cond = get_next_op_number();
5001
3.59k
  zend_update_jump_target(opnum_jmp, opnum_cond);
5002
3.59k
  zend_compile_expr(&cond_node, cond_ast);
5003
5004
3.59k
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
5005
5006
3.59k
  zend_end_loop(opnum_cond, NULL);
5007
3.59k
}
5008
/* }}} */
5009
5010
void zend_compile_do_while(zend_ast *ast) /* {{{ */
5011
996
{
5012
996
  zend_ast *stmt_ast = ast->child[0];
5013
996
  zend_ast *cond_ast = ast->child[1];
5014
5015
996
  znode cond_node;
5016
996
  uint32_t opnum_start, opnum_cond;
5017
5018
996
  zend_begin_loop(ZEND_NOP, NULL, 0);
5019
5020
996
  opnum_start = get_next_op_number();
5021
996
  zend_compile_stmt(stmt_ast);
5022
5023
996
  opnum_cond = get_next_op_number();
5024
996
  zend_compile_expr(&cond_node, cond_ast);
5025
5026
996
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
5027
5028
996
  zend_end_loop(opnum_cond, NULL);
5029
996
}
5030
/* }}} */
5031
5032
void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
5033
118k
{
5034
118k
  zend_ast_list *list;
5035
118k
  uint32_t i;
5036
5037
118k
  result->op_type = IS_CONST;
5038
118k
  ZVAL_TRUE(&result->u.constant);
5039
5040
118k
  if (!ast) {
5041
374
    return;
5042
374
  }
5043
5044
118k
  list = zend_ast_get_list(ast);
5045
238k
  for (i = 0; i < list->children; ++i) {
5046
120k
    zend_ast *expr_ast = list->child[i];
5047
5048
120k
    zend_do_free(result);
5049
120k
    zend_compile_expr(result, expr_ast);
5050
120k
  }
5051
118k
}
5052
/* }}} */
5053
5054
void zend_compile_for(zend_ast *ast) /* {{{ */
5055
39.6k
{
5056
39.6k
  zend_ast *init_ast = ast->child[0];
5057
39.6k
  zend_ast *cond_ast = ast->child[1];
5058
39.6k
  zend_ast *loop_ast = ast->child[2];
5059
39.6k
  zend_ast *stmt_ast = ast->child[3];
5060
5061
39.6k
  znode result;
5062
39.6k
  uint32_t opnum_start, opnum_jmp, opnum_loop;
5063
5064
39.6k
  zend_compile_expr_list(&result, init_ast);
5065
39.6k
  zend_do_free(&result);
5066
5067
39.6k
  opnum_jmp = zend_emit_jump(0);
5068
5069
39.6k
  zend_begin_loop(ZEND_NOP, NULL, 0);
5070
5071
39.6k
  opnum_start = get_next_op_number();
5072
39.6k
  zend_compile_stmt(stmt_ast);
5073
5074
39.6k
  opnum_loop = get_next_op_number();
5075
39.6k
  zend_compile_expr_list(&result, loop_ast);
5076
39.6k
  zend_do_free(&result);
5077
5078
39.6k
  zend_update_jump_target_to_next(opnum_jmp);
5079
39.6k
  zend_compile_expr_list(&result, cond_ast);
5080
39.6k
  zend_do_extended_stmt();
5081
5082
39.6k
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
5083
5084
39.6k
  zend_end_loop(opnum_loop, NULL);
5085
39.6k
}
5086
/* }}} */
5087
5088
void zend_compile_foreach(zend_ast *ast) /* {{{ */
5089
68.5k
{
5090
68.5k
  zend_ast *expr_ast = ast->child[0];
5091
68.5k
  zend_ast *value_ast = ast->child[1];
5092
68.5k
  zend_ast *key_ast = ast->child[2];
5093
68.5k
  zend_ast *stmt_ast = ast->child[3];
5094
68.5k
  zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
5095
68.5k
  zend_bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
5096
5097
68.5k
  znode expr_node, reset_node, value_node, key_node;
5098
68.5k
  zend_op *opline;
5099
68.5k
  uint32_t opnum_reset, opnum_fetch;
5100
5101
68.5k
  if (key_ast) {
5102
11.1k
    if (key_ast->kind == ZEND_AST_REF) {
5103
19
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
5104
19
    }
5105
11.1k
    if (key_ast->kind == ZEND_AST_ARRAY) {
5106
27
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
5107
27
    }
5108
68.5k
  }
5109
5110
68.5k
  if (by_ref) {
5111
13.3k
    value_ast = value_ast->child[0];
5112
13.3k
  }
5113
5114
68.5k
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
5115
49
    by_ref = 1;
5116
49
  }
5117
5118
68.5k
  if (by_ref && is_variable) {
5119
10.5k
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, 1);
5120
58.0k
  } else {
5121
58.0k
    zend_compile_expr(&expr_node, expr_ast);
5122
58.0k
  }
5123
5124
68.5k
  if (by_ref) {
5125
13.3k
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
5126
13.3k
  }
5127
5128
68.5k
  opnum_reset = get_next_op_number();
5129
55.1k
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
5130
5131
68.5k
  zend_begin_loop(ZEND_FE_FREE, &reset_node, 0);
5132
5133
68.5k
  opnum_fetch = get_next_op_number();
5134
55.1k
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
5135
5136
68.5k
  if (is_this_fetch(value_ast)) {
5137
38
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
5138
68.5k
  } else if (value_ast->kind == ZEND_AST_VAR &&
5139
66.3k
    zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
5140
66.1k
    SET_NODE(opline->op2, &value_node);
5141
2.39k
  } else {
5142
2.39k
    opline->op2_type = IS_VAR;
5143
2.39k
    opline->op2.var = get_temporary_variable();
5144
2.39k
    GET_NODE(&value_node, opline->op2);
5145
2.39k
    if (value_ast->kind == ZEND_AST_ARRAY) {
5146
1.52k
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
5147
871
    } else if (by_ref) {
5148
28
      zend_emit_assign_ref_znode(value_ast, &value_node);
5149
843
    } else {
5150
843
      zend_emit_assign_znode(value_ast, &value_node);
5151
843
    }
5152
2.39k
  }
5153
5154
68.5k
  if (key_ast) {
5155
11.1k
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
5156
11.1k
    zend_make_tmp_result(&key_node, opline);
5157
11.1k
    zend_emit_assign_znode(key_ast, &key_node);
5158
11.1k
  }
5159
5160
68.5k
  zend_compile_stmt(stmt_ast);
5161
5162
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
5163
   * better to use the end line, but this information is not available
5164
   * currently. */
5165
68.5k
  CG(zend_lineno) = ast->lineno;
5166
68.5k
  zend_emit_jump(opnum_fetch);
5167
5168
68.5k
  opline = &CG(active_op_array)->opcodes[opnum_reset];
5169
68.5k
  opline->op2.opline_num = get_next_op_number();
5170
5171
68.5k
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
5172
68.5k
  opline->extended_value = get_next_op_number();
5173
5174
68.5k
  zend_end_loop(opnum_fetch, &reset_node);
5175
5176
68.5k
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
5177
68.5k
}
5178
/* }}} */
5179
5180
void zend_compile_if(zend_ast *ast) /* {{{ */
5181
76.5k
{
5182
76.5k
  zend_ast_list *list = zend_ast_get_list(ast);
5183
76.5k
  uint32_t i;
5184
76.5k
  uint32_t *jmp_opnums = NULL;
5185
5186
76.5k
  if (list->children > 1) {
5187
13.9k
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
5188
13.9k
  }
5189
5190
167k
  for (i = 0; i < list->children; ++i) {
5191
90.5k
    zend_ast *elem_ast = list->child[i];
5192
90.5k
    zend_ast *cond_ast = elem_ast->child[0];
5193
90.5k
    zend_ast *stmt_ast = elem_ast->child[1];
5194
5195
90.5k
    if (cond_ast) {
5196
76.5k
      znode cond_node;
5197
76.5k
      uint32_t opnum_jmpz;
5198
76.5k
      zend_compile_expr(&cond_node, cond_ast);
5199
76.5k
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
5200
5201
76.5k
      zend_compile_stmt(stmt_ast);
5202
5203
76.5k
      if (i != list->children - 1) {
5204
13.9k
        jmp_opnums[i] = zend_emit_jump(0);
5205
13.9k
      }
5206
76.5k
      zend_update_jump_target_to_next(opnum_jmpz);
5207
13.9k
    } else {
5208
      /* "else" can only occur as last element. */
5209
13.9k
      ZEND_ASSERT(i == list->children - 1);
5210
13.9k
      zend_compile_stmt(stmt_ast);
5211
13.9k
    }
5212
90.5k
  }
5213
5214
76.5k
  if (list->children > 1) {
5215
27.9k
    for (i = 0; i < list->children - 1; ++i) {
5216
13.9k
      zend_update_jump_target_to_next(jmp_opnums[i]);
5217
13.9k
    }
5218
13.9k
    efree(jmp_opnums);
5219
13.9k
  }
5220
76.5k
}
5221
/* }}} */
5222
5223
2.31k
static zend_uchar determine_switch_jumptable_type(zend_ast_list *cases) {
5224
2.31k
  uint32_t i;
5225
2.31k
  zend_uchar common_type = IS_UNDEF;
5226
5.68k
  for (i = 0; i < cases->children; i++) {
5227
3.93k
    zend_ast *case_ast = cases->child[i];
5228
3.93k
    zend_ast **cond_ast = &case_ast->child[0];
5229
3.93k
    zval *cond_zv;
5230
3.93k
    if (!case_ast->child[0]) {
5231
      /* Skip default clause */
5232
551
      continue;
5233
551
    }
5234
5235
3.38k
    zend_eval_const_expr(cond_ast);
5236
3.38k
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
5237
      /* Non-constant case */
5238
82
      return IS_UNDEF;
5239
82
    }
5240
5241
3.30k
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
5242
3.30k
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
5243
      /* We only optimize switched on integers and strings */
5244
132
      return IS_UNDEF;
5245
132
    }
5246
5247
3.16k
    if (common_type == IS_UNDEF) {
5248
2.03k
      common_type = Z_TYPE_P(cond_zv);
5249
1.13k
    } else if (common_type != Z_TYPE_P(cond_zv)) {
5250
      /* Non-uniform case types */
5251
6
      return IS_UNDEF;
5252
6
    }
5253
5254
3.16k
    if (Z_TYPE_P(cond_zv) == IS_STRING
5255
1.50k
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
5256
      /* Numeric strings cannot be compared with a simple hash lookup */
5257
339
      return IS_UNDEF;
5258
339
    }
5259
3.16k
  }
5260
5261
1.75k
  return common_type;
5262
2.31k
}
5263
5264
1.65k
static zend_bool should_use_jumptable(zend_ast_list *cases, zend_uchar jumptable_type) {
5265
1.65k
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
5266
0
    return 0;
5267
0
  }
5268
5269
  /* Thresholds are chosen based on when the average switch time for equidistributed
5270
   * input becomes smaller when using the jumptable optimization. */
5271
1.65k
  if (jumptable_type == IS_LONG) {
5272
745
    return cases->children >= 5;
5273
909
  } else {
5274
909
    ZEND_ASSERT(jumptable_type == IS_STRING);
5275
909
    return cases->children >= 2;
5276
909
  }
5277
1.65k
}
5278
5279
void zend_compile_switch(zend_ast *ast) /* {{{ */
5280
2.31k
{
5281
2.31k
  zend_ast *expr_ast = ast->child[0];
5282
2.31k
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
5283
5284
2.31k
  uint32_t i;
5285
2.31k
  zend_bool has_default_case = 0;
5286
5287
2.31k
  znode expr_node, case_node;
5288
2.31k
  zend_op *opline;
5289
2.31k
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
5290
2.31k
  zend_uchar jumptable_type;
5291
2.31k
  HashTable *jumptable = NULL;
5292
5293
2.31k
  zend_compile_expr(&expr_node, expr_ast);
5294
5295
2.31k
  zend_begin_loop(ZEND_FREE, &expr_node, 1);
5296
5297
2.31k
  case_node.op_type = IS_TMP_VAR;
5298
2.31k
  case_node.u.op.var = get_temporary_variable();
5299
5300
2.31k
  jumptable_type = determine_switch_jumptable_type(cases);
5301
2.31k
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
5302
441
    znode jumptable_op;
5303
5304
441
    ALLOC_HASHTABLE(jumptable);
5305
441
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
5306
441
    jumptable_op.op_type = IS_CONST;
5307
441
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
5308
5309
441
    opline = zend_emit_op(NULL,
5310
441
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
5311
441
      &expr_node, &jumptable_op);
5312
441
    if (opline->op1_type == IS_CONST) {
5313
36
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
5314
36
    }
5315
441
    opnum_switch = opline - CG(active_op_array)->opcodes;
5316
441
  }
5317
5318
2.31k
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
5319
7.55k
  for (i = 0; i < cases->children; ++i) {
5320
5.31k
    zend_ast *case_ast = cases->child[i];
5321
5.31k
    zend_ast *cond_ast = case_ast->child[0];
5322
5.31k
    znode cond_node;
5323
5324
5.31k
    if (!cond_ast) {
5325
801
      if (has_default_case) {
5326
74
        CG(zend_lineno) = case_ast->lineno;
5327
74
        zend_error_noreturn(E_COMPILE_ERROR,
5328
74
          "Switch statements may only contain one default clause");
5329
74
      }
5330
727
      has_default_case = 1;
5331
727
      continue;
5332
727
    }
5333
5334
4.51k
    zend_compile_expr(&cond_node, cond_ast);
5335
5336
4.51k
    if (expr_node.op_type == IS_CONST
5337
311
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
5338
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
5339
4.51k
    } else if (expr_node.op_type == IS_CONST
5340
311
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
5341
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
5342
4.51k
    } else {
5343
4.51k
      opline = zend_emit_op(NULL,
5344
4.51k
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
5345
4.51k
        &expr_node, &cond_node);
5346
4.51k
      SET_NODE(opline->result, &case_node);
5347
4.51k
      if (opline->op1_type == IS_CONST) {
5348
311
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
5349
311
      }
5350
5351
4.51k
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
5352
4.51k
    }
5353
4.51k
  }
5354
5355
2.23k
  opnum_default_jmp = zend_emit_jump(0);
5356
5357
7.18k
  for (i = 0; i < cases->children; ++i) {
5358
4.94k
    zend_ast *case_ast = cases->child[i];
5359
4.94k
    zend_ast *cond_ast = case_ast->child[0];
5360
4.94k
    zend_ast *stmt_ast = case_ast->child[1];
5361
5362
4.94k
    if (cond_ast) {
5363
4.29k
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
5364
5365
4.29k
      if (jumptable) {
5366
606
        zval *cond_zv = zend_ast_get_zval(cond_ast);
5367
606
        zval jmp_target;
5368
606
        ZVAL_LONG(&jmp_target, get_next_op_number());
5369
5370
606
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
5371
606
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
5372
14
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
5373
592
        } else {
5374
592
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
5375
592
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
5376
592
        }
5377
606
      }
5378
653
    } else {
5379
653
      zend_update_jump_target_to_next(opnum_default_jmp);
5380
5381
653
      if (jumptable) {
5382
280
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
5383
280
        opline = &CG(active_op_array)->opcodes[opnum_switch];
5384
280
        opline->extended_value = get_next_op_number();
5385
280
      }
5386
653
    }
5387
5388
4.94k
    zend_compile_stmt(stmt_ast);
5389
4.94k
  }
5390
5391
2.23k
  if (!has_default_case) {
5392
1.52k
    zend_update_jump_target_to_next(opnum_default_jmp);
5393
5394
1.52k
    if (jumptable) {
5395
121
      opline = &CG(active_op_array)->opcodes[opnum_switch];
5396
121
      opline->extended_value = get_next_op_number();
5397
121
    }
5398
1.52k
  }
5399
5400
2.23k
  zend_end_loop(get_next_op_number(), &expr_node);
5401
5402
2.23k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
5403
875
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5404
875
    opline->extended_value = ZEND_FREE_SWITCH;
5405
1.36k
  } else if (expr_node.op_type == IS_CONST) {
5406
129
    zval_ptr_dtor_nogc(&expr_node.u.constant);
5407
129
  }
5408
5409
2.23k
  efree(jmpnz_opnums);
5410
2.23k
}
5411
/* }}} */
5412
5413
static uint32_t count_match_conds(zend_ast_list *arms)
5414
4.38k
{
5415
4.38k
  uint32_t num_conds = 0;
5416
5417
18.6k
  for (uint32_t i = 0; i < arms->children; i++) {
5418
14.2k
    zend_ast *arm_ast = arms->child[i];
5419
14.2k
    if (arm_ast->child[0] == NULL) {
5420
2.27k
      continue;
5421
2.27k
    }
5422
5423
11.9k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
5424
11.9k
    num_conds += conds->children;
5425
11.9k
  }
5426
5427
4.38k
  return num_conds;
5428
4.38k
}
5429
5430
4.38k
static zend_bool can_match_use_jumptable(zend_ast_list *arms) {
5431
10.8k
  for (uint32_t i = 0; i < arms->children; i++) {
5432
8.36k
    zend_ast *arm_ast = arms->child[i];
5433
8.36k
    if (!arm_ast->child[0]) {
5434
      /* Skip default arm */
5435
1.18k
      continue;
5436
1.18k
    }
5437
5438
7.17k
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
5439
20.7k
    for (uint32_t j = 0; j < conds->children; j++) {
5440
15.4k
      zend_ast **cond_ast = &conds->child[j];
5441
5442
15.4k
      zend_eval_const_expr(cond_ast);
5443
15.4k
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
5444
1.18k
        return 0;
5445
1.18k
      }
5446
5447
14.2k
      zval *cond_zv = zend_ast_get_zval(*cond_ast);
5448
14.2k
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
5449
679
        return 0;
5450
679
      }
5451
14.2k
    }
5452
7.17k
  }
5453
5454
2.51k
  return 1;
5455
4.38k
}
5456
5457
void zend_compile_match(znode *result, zend_ast *ast)
5458
4.38k
{
5459
4.38k
  zend_ast *expr_ast = ast->child[0];
5460
4.38k
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
5461
4.38k
  zend_bool has_default_arm = 0;
5462
4.38k
  uint32_t opnum_match = (uint32_t)-1;
5463
5464
4.38k
  znode expr_node;
5465
4.38k
  zend_compile_expr(&expr_node, expr_ast);
5466
5467
4.38k
  znode case_node;
5468
4.38k
  case_node.op_type = IS_TMP_VAR;
5469
4.38k
  case_node.u.op.var = get_temporary_variable();
5470
5471
4.38k
  uint32_t num_conds = count_match_conds(arms);
5472
4.38k
  zend_uchar can_use_jumptable = can_match_use_jumptable(arms);
5473
4.38k
  zend_bool uses_jumptable = can_use_jumptable && num_conds >= 2;
5474
4.38k
  HashTable *jumptable = NULL;
5475
4.38k
  uint32_t *jmpnz_opnums = NULL;
5476
5477
18.5k
  for (uint32_t i = 0; i < arms->children; ++i) {
5478
14.2k
    zend_ast *arm_ast = arms->child[i];
5479
5480
14.2k
    if (!arm_ast->child[0]) {
5481
2.27k
      if (has_default_arm) {
5482
33
        CG(zend_lineno) = arm_ast->lineno;
5483
33
        zend_error_noreturn(E_COMPILE_ERROR,
5484
33
          "Match expressions may only contain one default arm");
5485
33
      }
5486
2.23k
      has_default_arm = 1;
5487
2.23k
    }
5488
14.2k
  }
5489
5490
4.35k
  if (uses_jumptable) {
5491
2.36k
    znode jumptable_op;
5492
5493
2.36k
    ALLOC_HASHTABLE(jumptable);
5494
2.36k
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
5495
2.36k
    jumptable_op.op_type = IS_CONST;
5496
2.36k
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
5497
5498
2.36k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
5499
2.36k
    if (opline->op1_type == IS_CONST) {
5500
1.76k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
5501
1.76k
    }
5502
2.36k
    opnum_match = opline - CG(active_op_array)->opcodes;
5503
1.98k
  } else {
5504
1.98k
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
5505
1.98k
    uint32_t cond_count = 0;
5506
10.6k
    for (uint32_t i = 0; i < arms->children; ++i) {
5507
8.62k
      zend_ast *arm_ast = arms->child[i];
5508
5509
8.62k
      if (!arm_ast->child[0]) {
5510
1.11k
        continue;
5511
1.11k
      }
5512
5513
7.51k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
5514
16.3k
      for (uint32_t j = 0; j < conds->children; j++) {
5515
8.87k
        zend_ast *cond_ast = conds->child[j];
5516
5517
8.87k
        znode cond_node;
5518
8.87k
        zend_compile_expr(&cond_node, cond_ast);
5519
5520
8.87k
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
5521
8.87k
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
5522
8.87k
        SET_NODE(opline->result, &case_node);
5523
8.87k
        if (opline->op1_type == IS_CONST) {
5524
5.40k
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
5525
5.40k
        }
5526
5527
8.87k
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
5528
5529
8.87k
        cond_count++;
5530
8.87k
      }
5531
7.51k
    }
5532
1.98k
  }
5533
5534
4.35k
  uint32_t opnum_default_jmp = 0;
5535
4.35k
  if (!uses_jumptable) {
5536
1.97k
    opnum_default_jmp = zend_emit_jump(0);
5537
1.97k
  }
5538
5539
4.35k
  zend_bool is_first_case = 1;
5540
4.35k
  uint32_t cond_count = 0;
5541
4.35k
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
5542
5543
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
5544
  // for the arm result is freed even though it has not been initialized yet.
5545
4.35k
  if (!has_default_arm) {
5546
2.14k
    if (!uses_jumptable) {
5547
868
      zend_update_jump_target_to_next(opnum_default_jmp);
5548
868
    }
5549
5550
2.14k
    if (jumptable) {
5551
1.27k
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
5552
1.27k
      opline->extended_value = get_next_op_number();
5553
1.27k
    }
5554
5555
2.14k
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
5556
2.14k
    if (opline->op1_type == IS_CONST) {
5557
1.41k
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
5558
1.41k
    }
5559
2.14k
  }
5560
5561
18.4k
  for (uint32_t i = 0; i < arms->children; ++i) {
5562
14.0k
    zend_ast *arm_ast = arms->child[i];
5563
14.0k
    zend_ast *body_ast = arm_ast->child[1];
5564
5565
14.0k
    if (arm_ast->child[0] != NULL) {
5566
11.8k
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
5567
5568
32.7k
      for (uint32_t j = 0; j < conds->children; j++) {
5569
20.8k
        zend_ast *cond_ast = conds->child[j];
5570
5571
20.8k
        if (jmpnz_opnums != NULL) {
5572
8.85k
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
5573
8.85k
        }
5574
5575
20.8k
        if (jumptable) {
5576
12.0k
          zval *cond_zv = zend_ast_get_zval(cond_ast);
5577
12.0k
          zval jmp_target;
5578
12.0k
          ZVAL_LONG(&jmp_target, get_next_op_number());
5579
5580
12.0k
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
5581
10.9k
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
5582
1.05k
          } else {
5583
1.05k
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
5584
1.05k
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
5585
1.05k
          }
5586
12.0k
        }
5587
5588
20.8k
        cond_count++;
5589
20.8k
      }
5590
2.20k
    } else {
5591
2.20k
      if (!uses_jumptable) {
5592
1.11k
        zend_update_jump_target_to_next(opnum_default_jmp);
5593
1.11k
      }
5594
5595
2.20k
      if (jumptable) {
5596
1.09k
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
5597
1.09k
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
5598
1.09k
        opline->extended_value = get_next_op_number();
5599
1.09k
      }
5600
2.20k
    }
5601
5602
14.0k
    znode body_node;
5603
14.0k
    zend_compile_expr(&body_node, body_ast);
5604
5605
14.0k
    if (is_first_case) {
5606
4.31k
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
5607
4.31k
      is_first_case = 0;
5608
9.75k
    } else {
5609
9.75k
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
5610
9.75k
      SET_NODE(opline_qm_assign->result, result);
5611
9.75k
    }
5612
5613
14.0k
    jmp_end_opnums[i] = zend_emit_jump(0);
5614
14.0k
  }
5615
5616
  // Initialize result in case there is no arm
5617
4.35k
  if (arms->children == 0) {
5618
36
    result->op_type = IS_CONST;
5619
36
    ZVAL_NULL(&result->u.constant);
5620
36
  }
5621
5622
18.4k
  for (uint32_t i = 0; i < arms->children; ++i) {
5623
14.0k
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
5624
14.0k
  }
5625
5626
4.35k
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
5627
242
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5628
242
    opline->extended_value = ZEND_FREE_SWITCH;
5629
4.10k
  } else if (expr_node.op_type == IS_CONST) {
5630
3.02k
    zval_ptr_dtor_nogc(&expr_node.u.constant);
5631
3.02k
  }
5632
5633
4.35k
  if (jmpnz_opnums != NULL) {
5634
1.97k
    efree(jmpnz_opnums);
5635
1.97k
  }
5636
4.35k
  efree(jmp_end_opnums);
5637
4.35k
}
5638
5639
void zend_compile_try(zend_ast *ast) /* {{{ */
5640
387k
{
5641
387k
  zend_ast *try_ast = ast->child[0];
5642
387k
  zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
5643
387k
  zend_ast *finally_ast = ast->child[2];
5644
5645
387k
  uint32_t i, j;
5646
387k
  zend_op *opline;
5647
387k
  uint32_t try_catch_offset;
5648
387k
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
5649
387k
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
5650
387k
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
5651
5652
387k
  if (catches->children == 0 && !finally_ast) {
5653
97
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
5654
97
  }
5655
5656
  /* label: try { } must not be equal to try { label: } */
5657
387k
  if (CG(context).labels) {
5658
1.13k
    zend_label *label;
5659
1.13k
    ZEND_HASH_REVERSE_FOREACH_PTR(CG(context).labels, label) {
5660
1.13k
      if (label->opline_num == get_next_op_number()) {
5661
140
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
5662
140
      }
5663
1.13k
      break;
5664
1.13k
    } ZEND_HASH_FOREACH_END();
5665
1.13k
  }
5666
5667
387k
  try_catch_offset = zend_add_try_element(get_next_op_number());
5668
5669
387k
  if (finally_ast) {
5670
5.15k
    zend_loop_var fast_call;
5671
5.15k
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
5672
3.65k
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
5673
3.65k
    }
5674
5.15k
    CG(context).fast_call_var = get_temporary_variable();
5675
5676
    /* Push FAST_CALL on unwind stack */
5677
5.15k
    fast_call.opcode = ZEND_FAST_CALL;
5678
5.15k
    fast_call.var_type = IS_TMP_VAR;
5679
5.15k
    fast_call.var_num = CG(context).fast_call_var;
5680
5.15k
    fast_call.try_catch_offset = try_catch_offset;
5681
5.15k
    zend_stack_push(&CG(loop_var_stack), &fast_call);
5682
5.15k
  }
5683
5684
387k
  CG(context).try_catch_offset = try_catch_offset;
5685
5686
387k
  zend_compile_stmt(try_ast);
5687
5688
387k
  if (catches->children != 0) {
5689
383k
    jmp_opnums[0] = zend_emit_jump(0);
5690
383k
  }
5691
5692
771k
  for (i = 0; i < catches->children; ++i) {
5693
384k
    zend_ast *catch_ast = catches->child[i];
5694
384k
    zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
5695
384k
    zend_ast *var_ast = catch_ast->child[1];
5696
384k
    zend_ast *stmt_ast = catch_ast->child[2];
5697
374k
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
5698
384k
    zend_bool is_last_catch = (i + 1 == catches->children);
5699
5700
384k
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
5701
384k
    uint32_t opnum_catch = (uint32_t)-1;
5702
5703
384k
    CG(zend_lineno) = catch_ast->lineno;
5704
5705
771k
    for (j = 0; j < classes->children; j++) {
5706
387k
      zend_ast *class_ast = classes->child[j];
5707
387k
      zend_bool is_last_class = (j + 1 == classes->children);
5708
5709
387k
      if (!zend_is_const_default_class_ref(class_ast)) {
5710
19
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
5711
19
      }
5712
5713
387k
      opnum_catch = get_next_op_number();
5714
387k
      if (i == 0 && j == 0) {
5715
383k
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
5716
383k
      }
5717
5718
387k
      opline = get_next_op();
5719
387k
      opline->opcode = ZEND_CATCH;
5720
387k
      opline->op1_type = IS_CONST;
5721
387k
      opline->op1.constant = zend_add_class_name_literal(
5722
387k
          zend_resolve_class_name_ast(class_ast));
5723
387k
      opline->extended_value = zend_alloc_cache_slot();
5724
5725
387k
      if (var_name && zend_string_equals_literal(var_name, "this")) {
5726
38
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
5727
38
      }
5728
5729
387k
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
5730
378k
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
5731
5732
387k
      if (is_last_catch && is_last_class) {
5733
383k
        opline->extended_value |= ZEND_LAST_CATCH;
5734
383k
      }
5735
5736
387k
      if (!is_last_class) {
5737
3.49k
        jmp_multicatch[j] = zend_emit_jump(0);
5738
3.49k
        opline = &CG(active_op_array)->opcodes[opnum_catch];
5739
3.49k
        opline->op2.opline_num = get_next_op_number();
5740
3.49k
      }
5741
387k
    }
5742
5743
387k
    for (j = 0; j < classes->children - 1; j++) {
5744
3.49k
      zend_update_jump_target_to_next(jmp_multicatch[j]);
5745
3.49k
    }
5746
5747
384k
    efree(jmp_multicatch);
5748
5749
384k
    zend_compile_stmt(stmt_ast);
5750
5751
384k
    if (!is_last_catch) {
5752
769
      jmp_opnums[i + 1] = zend_emit_jump(0);
5753
769
    }
5754
5755
384k
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
5756
384k
    opline = &CG(active_op_array)->opcodes[opnum_catch];
5757
384k
    if (!is_last_catch) {
5758
769
      opline->op2.opline_num = get_next_op_number();
5759
769
    }
5760
384k
  }
5761
5762
771k
  for (i = 0; i < catches->children; ++i) {
5763
384k
    zend_update_jump_target_to_next(jmp_opnums[i]);
5764
384k
  }
5765
5766
387k
  if (finally_ast) {
5767
5.13k
    zend_loop_var discard_exception;
5768
5.13k
    uint32_t opnum_jmp = get_next_op_number() + 1;
5769
5770
    /* Pop FAST_CALL from unwind stack */
5771
5.13k
    zend_stack_del_top(&CG(loop_var_stack));
5772
5773
    /* Push DISCARD_EXCEPTION on unwind stack */
5774
5.13k
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
5775
5.13k
    discard_exception.var_type = IS_TMP_VAR;
5776
5.13k
    discard_exception.var_num = CG(context).fast_call_var;
5777
5.13k
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
5778
5779
5.13k
    CG(zend_lineno) = finally_ast->lineno;
5780
5781
5.13k
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
5782
5.13k
    opline->op1.num = try_catch_offset;
5783
5.13k
    opline->result_type = IS_TMP_VAR;
5784
5.13k
    opline->result.var = CG(context).fast_call_var;
5785
5786
5.13k
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
5787
5788
5.13k
    zend_compile_stmt(finally_ast);
5789
5790
5.13k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
5791
5.13k
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
5792
5.13k
      = get_next_op_number();
5793
5794
5.13k
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
5795
5.13k
    opline->op1_type = IS_TMP_VAR;
5796
5.13k
    opline->op1.var = CG(context).fast_call_var;
5797
5.13k
    opline->op2.num = orig_try_catch_offset;
5798
5799
5.13k
    zend_update_jump_target_to_next(opnum_jmp);
5800
5801
5.13k
    CG(context).fast_call_var = orig_fast_call_var;
5802
5803
    /* Pop DISCARD_EXCEPTION from unwind stack */
5804
5.13k
    zend_stack_del_top(&CG(loop_var_stack));
5805
5.13k
  }
5806
5807
387k
  CG(context).try_catch_offset = orig_try_catch_offset;
5808
5809
387k
  efree(jmp_opnums);
5810
387k
}
5811
/* }}} */
5812
5813
/* Encoding declarations must already be handled during parsing */
5814
zend_bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
5815
5.19k
{
5816
5.19k
  zend_ast_list *declares = zend_ast_get_list(ast);
5817
5.19k
  uint32_t i;
5818
11.6k
  for (i = 0; i < declares->children; ++i) {
5819
6.54k
    zend_ast *declare_ast = declares->child[i];
5820
6.54k
    zend_ast *name_ast = declare_ast->child[0];
5821
6.54k
    zend_ast *value_ast = declare_ast->child[1];
5822
6.54k
    zend_string *name = zend_ast_get_str(name_ast);
5823
5824
6.54k
    if (zend_string_equals_literal_ci(name, "encoding")) {
5825
646
      if (value_ast->kind != ZEND_AST_ZVAL) {
5826
124
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
5827
124
        return 0;
5828
124
      }
5829
5830
522
      if (CG(multibyte)) {
5831
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
5832
5833
0
        const zend_encoding *new_encoding, *old_encoding;
5834
0
        zend_encoding_filter old_input_filter;
5835
5836
0
        CG(encoding_declared) = 1;
5837
5838
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
5839
0
        if (!new_encoding) {
5840
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
5841
0
        } else {
5842
0
          old_input_filter = LANG_SCNG(input_filter);
5843
0
          old_encoding = LANG_SCNG(script_encoding);
5844
0
          zend_multibyte_set_filter(new_encoding);
5845
5846
          /* need to re-scan if input filter changed */
5847
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
5848
0
             (old_input_filter && new_encoding != old_encoding)) {
5849
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
5850
0
          }
5851
0
        }
5852
5853
0
        zend_string_release_ex(encoding_name, 0);
5854
522
      } else {
5855
522
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
5856
522
          "Zend multibyte feature is turned off by settings");
5857
522
      }
5858
522
    }
5859
6.54k
  }
5860
5861
5.06k
  return 1;
5862
5.19k
}
5863
/* }}} */
5864
5865
static zend_result zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
5866
1.22k
{
5867
1.22k
  uint32_t i = 0;
5868
1.22k
  zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
5869
5870
  /* Check to see if this declare is preceded only by declare statements */
5871
1.31k
  while (i < file_ast->children) {
5872
1.31k
    if (file_ast->child[i] == ast) {
5873
1.10k
      return SUCCESS;
5874
205
    } else if (file_ast->child[i] == NULL) {
5875
      /* Empty statements are not allowed prior to a declare */
5876
2
      return FAILURE;
5877
203
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
5878
      /* declares can only be preceded by other declares */
5879
118
      return FAILURE;
5880
118
    }
5881
85
    i++;
5882
85
  }
5883
0
  return FAILURE;
5884
1.22k
}
5885
/* }}} */
5886
5887
void zend_compile_declare(zend_ast *ast) /* {{{ */
5888
1.82k
{
5889
1.82k
  zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
5890
1.82k
  zend_ast *stmt_ast = ast->child[1];
5891
1.82k
  zend_declarables orig_declarables = FC(declarables);
5892
1.82k
  uint32_t i;
5893
5894
3.44k
  for (i = 0; i < declares->children; ++i) {
5895
1.83k
    zend_ast *declare_ast = declares->child[i];
5896
1.83k
    zend_ast *name_ast = declare_ast->child[0];
5897
1.83k
    zend_ast *value_ast = declare_ast->child[1];
5898
1.83k
    zend_string *name = zend_ast_get_str(name_ast);
5899
5900
1.83k
    if (value_ast->kind != ZEND_AST_ZVAL) {
5901
72
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
5902
72
    }
5903
5904
1.76k
    if (zend_string_equals_literal_ci(name, "ticks")) {
5905
282
      zval value_zv;
5906
282
      zend_const_expr_to_zval(&value_zv, value_ast);
5907
282
      FC(declarables).ticks = zval_get_long(&value_zv);
5908
282
      zval_ptr_dtor_nogc(&value_zv);
5909
1.48k
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
5910
5911
173
      if (FAILURE == zend_declare_is_first_statement(ast)) {
5912
43
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
5913
43
          "the very first statement in the script");
5914
43
      }
5915
1.30k
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
5916
1.05k
      zval value_zv;
5917
5918
1.05k
      if (FAILURE == zend_declare_is_first_statement(ast)) {
5919
77
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
5920
77
          "the very first statement in the script");
5921
77
      }
5922
5923
978
      if (ast->child[1] != NULL) {
5924
19
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
5925
19
          "use block mode");
5926
19
      }
5927
5928
959
      zend_const_expr_to_zval(&value_zv, value_ast);
5929
5930
959
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
5931
8
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
5932
8
      }
5933
5934
951
      if (Z_LVAL(value_zv) == 1) {
5935
895
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
5936
895
      }
5937
5938
252
    } else {
5939
252
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
5940
252
    }
5941
1.76k
  }
5942
5943
1.60k
  if (stmt_ast) {
5944
117
    zend_compile_stmt(stmt_ast);
5945
5946
117
    FC(declarables) = orig_declarables;
5947
117
  }
5948
1.60k
}
5949
/* }}} */
5950
5951
void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
5952
1.76M
{
5953
1.76M
  zend_ast_list *list = zend_ast_get_list(ast);
5954
1.76M
  uint32_t i;
5955
4.19M
  for (i = 0; i < list->children; ++i) {
5956
2.42M
    zend_compile_stmt(list->child[i]);
5957
2.42M
  }
5958
1.76M
}
5959
/* }}} */
5960
5961
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
5962
7.45M
{
5963
7.45M
  uint32_t i, n;
5964
5965
7.45M
  func->common.arg_flags[0] = 0;
5966
7.45M
  func->common.arg_flags[1] = 0;
5967
7.45M
  func->common.arg_flags[2] = 0;
5968
7.45M
  if (func->common.arg_info) {
5969
7.45M
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
5970
7.45M
    i = 0;
5971
16.2M
    while (i < n) {
5972
8.77M
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
5973
8.77M
      i++;
5974
8.77M
    }
5975
7.45M
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
5976
25.1k
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
5977
273k
      while (i < MAX_ARG_FLAG_NUM) {
5978
248k
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
5979
248k
        i++;
5980
248k
      }
5981
25.1k
    }
5982
7.45M
  }
5983
7.45M
}
5984
/* }}} */
5985
5986
static zend_type zend_compile_single_typename(zend_ast *ast)
5987
167k
{
5988
167k
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
5989
167k
  if (ast->kind == ZEND_AST_TYPE) {
5990
18.9k
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
5991
19
      zend_error_noreturn(E_COMPILE_ERROR,
5992
19
        "Cannot use \"static\" when no class scope is active");
5993
19
    }
5994
18.8k
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
5995
148k
  } else {
5996
148k
    zend_string *class_name = zend_ast_get_str(ast);
5997
148k
    zend_uchar type_code = zend_lookup_builtin_type_by_name(class_name);
5998
5999
148k
    if (type_code != 0) {
6000
92.7k
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
6001
59
        zend_error_noreturn(E_COMPILE_ERROR,
6002
59
          "Type declaration '%s' must be unqualified",
6003
59
          ZSTR_VAL(zend_string_tolower(class_name)));
6004
59
      }
6005
92.7k
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
6006
55.3k
    } else {
6007
55.3k
      const char *correct_name;
6008
55.3k
      zend_string *orig_name = zend_ast_get_str(ast);
6009
55.3k
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
6010
55.3k
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
6011
52.9k
        class_name = zend_resolve_class_name_ast(ast);
6012
52.9k
        zend_assert_valid_class_name(class_name);
6013
2.39k
      } else {
6014
2.39k
        zend_ensure_valid_class_fetch_type(fetch_type);
6015
2.39k
        zend_string_addref(class_name);
6016
2.39k
      }
6017
6018
55.3k
      if (ast->attr == ZEND_NAME_NOT_FQ
6019
53.8k
          && zend_is_confusable_type(orig_name, &correct_name)
6020
1.33k
          && zend_is_not_imported(orig_name)) {
6021
1.18k
        const char *extra =
6022
1.18k
          FC(current_namespace) ? " or import the class with \"use\"" : "";
6023
1.18k
        if (correct_name) {
6024
973
          zend_error(E_COMPILE_WARNING,
6025
973
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
6026
973
            "Write \"\\%s\"%s to suppress this warning",
6027
973
            ZSTR_VAL(orig_name), correct_name, ZSTR_VAL(class_name), extra);
6028
215
        } else {
6029
215
          zend_error(E_COMPILE_WARNING,
6030
215
            "\"%s\" is not a supported builtin type "
6031
215
            "and will be interpreted as a class name. "
6032
215
            "Write \"\\%s\"%s to suppress this warning",
6033
215
            ZSTR_VAL(orig_name), ZSTR_VAL(class_name), extra);
6034
215
        }
6035
1.18k
      }
6036
6037
55.3k
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, 0, 0);
6038
55.3k
    }
6039
148k
  }
6040
167k
}
6041
6042
5.52k
static zend_bool zend_type_contains_traversable(zend_type type) {
6043
5.52k
  zend_type *single_type;
6044
11.2k
  ZEND_TYPE_FOREACH(type, single_type) {
6045
5.67k
    if (ZEND_TYPE_HAS_NAME(*single_type)
6046
1.52k
        && zend_string_equals_literal_ci(ZEND_TYPE_NAME(*single_type), "Traversable")) {
6047
52
      return 1;
6048
52
    }
6049
5.67k
  } ZEND_TYPE_FOREACH_END();
6050
5.47k
  return 0;
6051
5.52k
}
6052
6053
// TODO: Ideally we'd canonicalize "iterable" into "array|Traversable" and essentially
6054
// treat it as a built-in type alias.
6055
static zend_type zend_compile_typename(
6056
    zend_ast *ast, zend_bool force_allow_null, zend_bool use_arena) /* {{{ */
6057
132k
{
6058
132k
  zend_bool allow_null = force_allow_null;
6059
132k
  zend_ast_attr orig_ast_attr = ast->attr;
6060
132k
  zend_type type = ZEND_TYPE_INIT_NONE(0);
6061
132k
  if (ast->attr & ZEND_TYPE_NULLABLE) {
6062
10.5k
    allow_null = 1;
6063
10.5k
    ast->attr &= ~ZEND_TYPE_NULLABLE;
6064
10.5k
  }
6065
6066
132k
  if (ast->kind == ZEND_AST_TYPE_UNION) {
6067
25.7k
    zend_ast_list *list = zend_ast_get_list(ast);
6068
86.3k
    for (uint32_t i = 0; i < list->children; i++) {
6069
60.7k
      zend_ast *type_ast = list->child[i];
6070
60.7k
      zend_type single_type = zend_compile_single_typename(type_ast);
6071
60.7k
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
6072
6073
60.7k
      if (single_type_mask == MAY_BE_ANY) {
6074
57
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
6075
57
      }
6076
6077
60.6k
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
6078
60.6k
      if (type_mask_overlap) {
6079
40
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
6080
40
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
6081
40
        zend_error_noreturn(E_COMPILE_ERROR,
6082
40
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
6083
40
      }
6084
60.6k
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
6085
60.6k
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
6086
6087
60.6k
      if (ZEND_TYPE_HAS_CLASS(single_type)) {
6088
28.0k
        if (!ZEND_TYPE_HAS_CLASS(type)) {
6089
          /* The first class type can be stored directly as the type ptr payload. */
6090
15.6k
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
6091
15.6k
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
6092
12.3k
        } else {
6093
12.3k
          zend_type_list *list;
6094
12.3k
          if (ZEND_TYPE_HAS_LIST(type)) {
6095
            /* Add name to existing name list. */
6096
1.70k
            zend_type_list *old_list = ZEND_TYPE_LIST(type);
6097
1.70k
            if (use_arena) {
6098
              // TODO: Add a zend_arena_realloc API?
6099
402
              list = zend_arena_alloc(
6100
402
                &CG(arena), ZEND_TYPE_LIST_SIZE(old_list->num_types + 1));
6101
402
              memcpy(list, old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types));
6102
1.30k
            } else {
6103
1.30k
              list = erealloc(old_list, ZEND_TYPE_LIST_SIZE(old_list->num_types + 1));
6104
1.30k
            }
6105
10.6k
          } else {
6106
            /* Switch from single name to name list. */
6107
10.6k
            size_t size = ZEND_TYPE_LIST_SIZE(2);
6108
8.24k
            list = use_arena ? zend_arena_alloc(&CG(arena), size) : emalloc(size);
6109
10.6k
            list->num_types = 1;
6110
10.6k
            list->types[0] = type;
6111
10.6k
            ZEND_TYPE_FULL_MASK(list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
6112
10.6k
          }
6113
6114
12.3k
          list->types[list->num_types++] = single_type;
6115
12.3k
          ZEND_TYPE_SET_LIST(type, list);
6116
12.3k
          if (use_arena) {
6117
2.80k
            ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
6118
2.80k
          }
6119
6120
          /* Check for trivially redundant class types */
6121
26.4k
          for (size_t i = 0; i < list->num_types - 1; i++) {
6122
14.2k
            if (zend_string_equals_ci(
6123
107
                ZEND_TYPE_NAME(list->types[i]), ZEND_TYPE_NAME(single_type))) {
6124
107
              zend_string *single_type_str = zend_type_to_string(single_type);
6125
107
              zend_error_noreturn(E_COMPILE_ERROR,
6126
107
                "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
6127
107
            }
6128
14.2k
          }
6129
12.3k
        }
6130
28.0k
      }
6131
60.6k
    }
6132
106k
  } else {
6133
106k
    type = zend_compile_single_typename(ast);
6134
106k
  }
6135
6136
131k
  if (allow_null) {
6137
11.3k
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
6138
11.3k
  }
6139
6140
131k
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
6141
131k
  if ((type_mask & (MAY_BE_ARRAY|MAY_BE_ITERABLE)) == (MAY_BE_ARRAY|MAY_BE_ITERABLE)) {
6142
20
    zend_string *type_str = zend_type_to_string(type);
6143
20
    zend_error_noreturn(E_COMPILE_ERROR,
6144
20
      "Type %s contains both iterable and array, which is redundant", ZSTR_VAL(type_str));
6145
20
  }
6146
6147
131k
  if ((type_mask & MAY_BE_ITERABLE) && zend_type_contains_traversable(type)) {
6148
52
    zend_string *type_str = zend_type_to_string(type);
6149
52
    zend_error_noreturn(E_COMPILE_ERROR,
6150
52
      "Type %s contains both iterable and Traversable, which is redundant",
6151
52
      ZSTR_VAL(type_str));
6152
52
  }
6153
6154
131k
  if (type_mask == MAY_BE_ANY && (orig_ast_attr & ZEND_TYPE_NULLABLE)) {
6155
77
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
6156
77
  }
6157
6158
131k
  if ((type_mask & MAY_BE_OBJECT) && (ZEND_TYPE_HAS_CLASS(type) || (type_mask & MAY_BE_STATIC))) {
6159
41
    zend_string *type_str = zend_type_to_string(type);
6160
41
    zend_error_noreturn(E_COMPILE_ERROR,
6161
41
      "Type %s contains both object and a class type, which is redundant",
6162
41
      ZSTR_VAL(type_str));
6163
41
  }
6164
6165
131k
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_HAS_CLASS(type) || type_mask != MAY_BE_VOID)) {
6166
38
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
6167
38
  }
6168
6169
131k
  if ((type_mask & (MAY_BE_NULL|MAY_BE_FALSE))
6170
19.2k
      && !ZEND_TYPE_HAS_CLASS(type) && !(type_mask & ~(MAY_BE_NULL|MAY_BE_FALSE))) {
6171
76
    if (type_mask == MAY_BE_NULL) {
6172
38
      zend_error_noreturn(E_COMPILE_ERROR, "Null can not be used as a standalone type");
6173
38
    } else {
6174
38
      zend_error_noreturn(E_COMPILE_ERROR, "False can not be used as a standalone type");
6175
38
    }
6176
131k
  }
6177
6178
131k
  ast->attr = orig_ast_attr;
6179
131k
  return type;
6180
131k
}
6181
/* }}} */
6182
6183
/* May convert value from int to float. */
6184
static zend_bool zend_is_valid_default_value(zend_type type, zval *value)
6185
6.83k
{
6186
6.83k
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
6187
6.83k
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
6188
5.99k
    return 1;
6189
5.99k
  }
6190
837
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
6191
    /* Integers are allowed as initializers for floating-point values. */
6192
101
    convert_to_double(value);
6193
101
    return 1;
6194
101
  }
6195
736
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_ITERABLE) && Z_TYPE_P(value) == IS_ARRAY) {
6196
418
    return 1;
6197
418
  }
6198
318
  return 0;
6199
318
}
6200
6201
static void zend_compile_attributes(HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target) /* {{{ */
6202
20.0k
{
6203
20.0k
  zend_attribute *attr;
6204
20.0k
  zend_internal_attribute *config;
6205
6206
20.0k
  zend_ast_list *list = zend_ast_get_list(ast);
6207
20.0k
  uint32_t i, j;
6208
6209
20.0k
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
6210
6211
48.2k
  for (i = 0; i < list->children; i++) {
6212
28.2k
    ZEND_ASSERT(list->child[i]->kind == ZEND_AST_ATTRIBUTE);
6213
6214
28.2k
    zend_ast *el = list->child[i];
6215
28.2k
    zend_string *name = zend_resolve_class_name_ast(el->child[0]);
6216
14.1k
    zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
6217
6218
14.1k
    attr = zend_add_attribute(attributes, 0, offset, name, args ? args->children : 0);
6219
28.2k
    zend_string_release(name);
6220
6221
    /* Populate arguments */
6222
28.2k
    if (args) {
6223
14.1k
      ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
6224
6225
14.1k
      zend_bool uses_named_args = 0;
6226
32.8k
      for (j = 0; j < args->children; j++) {
6227
18.7k
        zend_ast *arg_ast = args->child[j];
6228
6229
18.7k
        if (arg_ast->kind == ZEND_AST_UNPACK) {
6230
19
          zend_error_noreturn(E_COMPILE_ERROR,
6231
19
            "Cannot use unpacking in attribute argument list");
6232
19
        }
6233
6234
18.7k
        if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
6235
398
          attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
6236
398
          arg_ast = arg_ast->child[1];
6237
398
          uses_named_args = 1;
6238
6239
618
          for (uint32_t k = 0; k < j; k++) {
6240
240
            if (attr->args[k].name &&
6241
49
                zend_string_equals(attr->args[k].name, attr->args[j].name)) {
6242
20
              zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
6243
20
                ZSTR_VAL(attr->args[j].name));
6244
20
            }
6245
240
          }
6246
18.3k
        } else if (uses_named_args) {
6247
45
          zend_error_noreturn(E_COMPILE_ERROR,
6248
45
            "Cannot use positional argument after named argument");
6249
45
        }
6250
6251
18.6k
        zend_const_expr_to_zval(&attr->args[j].value, arg_ast);
6252
18.6k
      }
6253
14.1k
    }
6254
28.2k
  }
6255
6256
  /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
6257
80.4k
  ZEND_HASH_FOREACH_PTR(*attributes, attr) {
6258
30.2k
    if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
6259
28.6k
      continue;
6260
28.6k
    }
6261
6262
1.60k
    if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
6263
39
      zend_string *location = zend_get_attribute_target_names(target);
6264
39
      zend_string *allowed = zend_get_attribute_target_names(config->flags);
6265
6266
39
      zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
6267
39
        ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
6268
39
      );
6269
39
    }
6270
6271
1.56k
    if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
6272
1.56k
      if (zend_is_attribute_repeated(*attributes, attr)) {
6273
20
        zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
6274
20
      }
6275
1.54k
    }
6276
6277
1.54k
    if (config->validator != NULL) {
6278
1.54k
      config->validator(attr, target, CG(active_class_entry));
6279
1.54k
    }
6280
1.54k
  } ZEND_HASH_FOREACH_END();
6281
19.9k
}
6282
/* }}} */
6283
6284
void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
6285
389k
{
6286
389k
  zend_ast_list *list = zend_ast_get_list(ast);
6287
389k
  uint32_t i;
6288
389k
  zend_op_array *op_array = CG(active_op_array);
6289
389k
  zend_arg_info *arg_infos;
6290
389k
  zend_string *optional_param = NULL;
6291
6292
389k
  if (return_type_ast || fallback_return_type) {
6293
    /* Use op_array->arg_info[-1] for return type */
6294
34.4k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
6295
34.4k
    arg_infos->name = NULL;
6296
34.4k
    if (return_type_ast) {
6297
27.6k
      arg_infos->type = zend_compile_typename(
6298
27.6k
        return_type_ast, /* force_allow_null */ 0, /* use_arena */ 0);
6299
27.6k
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
6300
27.6k
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0);
6301
6.87k
    } else {
6302
6.87k
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
6303
6.87k
    }
6304
34.4k
    arg_infos++;
6305
34.4k
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
6306
354k
  } else {
6307
354k
    if (list->children == 0) {
6308
155k
      return;
6309
155k
    }
6310
198k
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
6311
198k
  }
6312
6313
591k
  for (i = 0; i < list->children; ++i) {
6314
358k
    zend_ast *param_ast = list->child[i];
6315
358k
    zend_ast *type_ast = param_ast->child[0];
6316
358k
    zend_ast *var_ast = param_ast->child[1];
6317
358k
    zend_ast *default_ast = param_ast->child[2];
6318
358k
    zend_ast *attributes_ast = param_ast->child[3];
6319
358k
    zend_ast *doc_comment_ast = param_ast->child[4];
6320
358k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
6321
358k
    zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
6322
358k
    zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
6323
358k
    uint32_t visibility =
6324
358k
      param_ast->attr & (ZEND_ACC_PUBLIC|ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE);
6325
6326
358k
    znode var_node, default_node;
6327
358k
    zend_uchar opcode;
6328
358k
    zend_op *opline;
6329
358k
    zend_arg_info *arg_info;
6330
6331
358k
    zend_ast_ref *attributes_copy = NULL;
6332
6333
358k
    if (visibility && attributes_ast) {
6334
183
      attributes_copy = zend_ast_copy(attributes_ast);
6335
183
    }
6336
6337
358k
    if (zend_is_auto_global(name)) {
6338
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
6339
0
        ZSTR_VAL(name));
6340
0
    }
6341
6342
358k
    var_node.op_type = IS_CV;
6343
358k
    var_node.u.op.var = lookup_cv(name);
6344
6345
358k
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
6346
59
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
6347
59
        ZSTR_VAL(name));
6348
358k
    } else if (zend_string_equals_literal(name, "this")) {
6349
57
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
6350
57
    }
6351
6352
358k
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
6353
20
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
6354
20
    }
6355
6356
358k
    if (is_variadic) {
6357
3.28k
      opcode = ZEND_RECV_VARIADIC;
6358
3.28k
      default_node.op_type = IS_UNUSED;
6359
3.28k
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
6360
6361
3.28k
      if (default_ast) {
6362
19
        zend_error_noreturn(E_COMPILE_ERROR,
6363
19
          "Variadic parameter cannot have a default value");
6364
19
      }
6365
354k
    } else if (default_ast) {
6366
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
6367
59.7k
      uint32_t cops = CG(compiler_options);
6368
59.7k
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
6369
59.7k
      opcode = ZEND_RECV_INIT;
6370
59.7k
      default_node.op_type = IS_CONST;
6371
59.7k
      zend_const_expr_to_zval(&default_node.u.constant, default_ast);
6372
59.7k
      CG(compiler_options) = cops;
6373
6374
59.7k
      if (!optional_param) {
6375
        /* Ignore parameters of the form "Type $param = null".
6376
         * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
6377
14.6k
        zend_bool is_implicit_nullable =
6378
14.6k
          type_ast && Z_TYPE(default_node.u.constant) == IS_NULL;
6379
14.6k
        if (!is_implicit_nullable) {
6380
13.8k
          optional_param = name;
6381
13.8k
        }
6382
14.6k
      }
6383
295k
    } else {
6384
295k
      opcode = ZEND_RECV;
6385
295k
      default_node.op_type = IS_UNUSED;
6386
295k
      op_array->required_num_args = i + 1;
6387
295k
      if (optional_param) {
6388
210
        zend_error(E_DEPRECATED, "Required parameter $%s follows optional parameter $%s",
6389
210
          ZSTR_VAL(name), ZSTR_VAL(optional_param));
6390
210
      }
6391
295k
    }
6392
6393
357k
    arg_info = &arg_infos[i];
6394
357k
    arg_info->name = zend_string_copy(name);
6395
357k
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
6396
6397
357k
    if (attributes_ast) {
6398
1.73k
      zend_compile_attributes(&op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER);
6399
1.73k
    }
6400
6401
357k
    if (type_ast) {
6402
58.1k
      uint32_t default_type = default_ast ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
6403
60.7k
      zend_bool force_nullable = default_type == IS_NULL && !visibility;
6404
6405
60.7k
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
6406
60.7k
      arg_info->type = zend_compile_typename(type_ast, force_nullable, /* use_arena */ 0);
6407
6408
60.7k
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
6409
19
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
6410
19
      }
6411
6412
60.7k
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
6413
1.23k
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
6414
179
        zend_string *type_str = zend_type_to_string(arg_info->type);
6415
179
        zend_error_noreturn(E_COMPILE_ERROR,
6416
179
          "Cannot use %s as default value for parameter $%s of type %s",
6417
179
          zend_get_type_by_const(default_type),
6418
179
          ZSTR_VAL(name), ZSTR_VAL(type_str));
6419
179
      }
6420
357k
    }
6421
6422
357k
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
6423
357k
    SET_NODE(opline->result, &var_node);
6424
357k
    opline->op1.num = i + 1;
6425
6426
357k
    if (type_ast) {
6427
      /* Allocate cache slot to speed-up run-time class resolution */
6428
60.4k
      opline->extended_value =
6429
60.4k
        zend_alloc_cache_slots(zend_type_get_num_classes(arg_info->type));
6430
60.4k
    }
6431
6432
357k
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic)
6433
356k
      | (visibility ? _ZEND_IS_PROMOTED_BIT : 0);
6434
357k
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
6435
357k
    if (opcode == ZEND_RECV) {
6436
294k
      opline->op2.num = type_ast ?
6437
237k
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
6438
294k
    }
6439
6440
357k
    if (visibility) {
6441
1.00k
      zend_op_array *op_array = CG(active_op_array);
6442
1.00k
      zend_class_entry *scope = op_array->scope;
6443
1.00k
      zend_bool is_ctor =
6444
1.00k
        scope && zend_is_constructor(op_array->function_name);
6445
1.00k
      if (!is_ctor) {
6446
39
        zend_error_noreturn(E_COMPILE_ERROR,
6447
39
          "Cannot declare promoted property outside a constructor");
6448
39
      }
6449
968
      if ((op_array->fn_flags & ZEND_ACC_ABSTRACT)
6450
930
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
6451
38
        zend_error_noreturn(E_COMPILE_ERROR,
6452
38
          "Cannot declare promoted property in an abstract constructor");
6453
38
      }
6454
930
      if (is_variadic) {
6455
19
        zend_error_noreturn(E_COMPILE_ERROR,
6456
19
          "Cannot declare variadic promoted property");
6457
19
      }
6458
911
      if (zend_hash_exists(&scope->properties_info, name)) {
6459
19
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
6460
19
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
6461
19
      }
6462
892
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
6463
19
        zend_string *str = zend_type_to_string(arg_info->type);
6464
19
        zend_error_noreturn(E_COMPILE_ERROR,
6465
19
          "Property %s::$%s cannot have type %s",
6466
19
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
6467
19
      }
6468
6469
      /* Recompile the type, as it has different memory management requirements. */
6470
873
      zend_type type = ZEND_TYPE_INIT_NONE(0);
6471
873
      if (type_ast) {
6472
520
        type = zend_compile_typename(type_ast, /* force_allow_null */ 0, /* use_arena */ 1);
6473
520
      }
6474
6475
      /* Don't give the property an explicit default value. For typed properties this means
6476
       * uninitialized, for untyped properties it means an implicit null default value. */
6477
873
      zval default_value;
6478
873
      if (ZEND_TYPE_IS_SET(type)) {
6479
520
        ZVAL_UNDEF(&default_value);
6480
353
      } else {
6481
353
        ZVAL_NULL(&default_value);
6482
353
      }
6483
6484
873
      zend_string *doc_comment =
6485
0
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
6486
873
      zend_property_info *prop = zend_declare_typed_property(
6487
873
        scope, name, &default_value, visibility | ZEND_ACC_PROMOTED, doc_comment, type);
6488
873
      if (attributes_ast) {
6489
183
        zend_compile_attributes(
6490
183
          &prop->attributes, GC_AST(attributes_copy), 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
6491
183
        zend_ast_ref_destroy(attributes_copy);
6492
183
      }
6493
873
    }
6494
357k
  }
6495
6496
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
6497
232k
  op_array->num_args = list->children;
6498
232k
  op_array->arg_info = arg_infos;
6499
6500
  /* Don't count the variadic argument */
6501
232k
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
6502
3.22k
    op_array->num_args--;
6503
3.22k
  }
6504
232k
  zend_set_function_arg_flags((zend_function*)op_array);
6505
6506
589k
  for (i = 0; i < list->children; i++) {
6507
356k
    zend_ast *param_ast = list->child[i];
6508
356k
    zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
6509
356k
    uint32_t visibility =
6510
356k
      param_ast->attr & (ZEND_ACC_PUBLIC|ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE);
6511
356k
    if (!visibility) {
6512
355k
      continue;
6513
355k
    }
6514
6515
    /* Emit $this->prop = $prop for promoted properties. */
6516
797
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
6517
797
    znode name_node, value_node;
6518
797
    name_node.op_type = IS_CONST;
6519
797
    ZVAL_STR_COPY(&name_node.u.constant, name);
6520
797
    value_node.op_type = IS_CV;
6521
797
    value_node.u.op.var = lookup_cv(name);
6522
6523
797
    zend_op *opline = zend_emit_op(NULL,
6524
778
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
6525
797
    opline->extended_value = zend_alloc_cache_slots(3);
6526
797
    zend_emit_op_data(&value_node);
6527
797
  }
6528
232k
}
6529
/* }}} */
6530
6531
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
6532
7.40k
{
6533
7.40k
  zend_ast_list *list = zend_ast_get_list(uses_ast);
6534
7.40k
  uint32_t i;
6535
6536
7.40k
  if (!list->children) {
6537
0
    return;
6538
0
  }
6539
6540
7.40k
  if (!op_array->static_variables) {
6541
7.40k
    op_array->static_variables = zend_new_array(8);
6542
7.40k
  }
6543
6544
18.1k
  for (i = 0; i < list->children; ++i) {
6545
10.8k
    zend_ast *var_name_ast = list->child[i];
6546
10.8k
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
6547
10.8k
    uint32_t mode = var_name_ast->attr;
6548
10.8k
    zend_op *opline;
6549
10.8k
    zval *value;
6550
6551
10.8k
    if (zend_string_equals_literal(var_name, "this")) {
6552
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
6553
19
    }
6554
6555
10.8k
    if (zend_is_auto_global(var_name)) {
6556
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
6557
19
    }
6558
6559
10.7k
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
6560
10.7k
    if (!value) {
6561
19
      zend_error_noreturn(E_COMPILE_ERROR,
6562
19
        "Cannot use variable $%s twice", ZSTR_VAL(var_name));
6563
19
    }
6564
6565
10.7k
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
6566
6567
10.7k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
6568
10.7k
    opline->op2_type = IS_CV;
6569
10.7k
    opline->op2.var = lookup_cv(var_name);
6570
10.7k
    opline->extended_value =
6571
10.7k
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
6572
10.7k
  }
6573
7.40k
}
6574
/* }}} */
6575
6576
typedef struct {
6577
  HashTable uses;
6578
  zend_bool varvars_used;
6579
} closure_info;
6580
6581
105k
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
6582
105k
  if (!ast) {
6583
1.14k
    return;
6584
1.14k
  }
6585
6586
104k
  if (ast->kind == ZEND_AST_VAR) {
6587
14.9k
    zend_ast *name_ast = ast->child[0];
6588
14.9k
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
6589
14.3k
      zend_string *name = zend_ast_get_str(name_ast);
6590
14.3k
      if (zend_is_auto_global(name)) {
6591
        /* These is no need to explicitly import auto-globals. */
6592
48
        return;
6593
48
      }
6594
6595
14.3k
      if (zend_string_equals_literal(name, "this")) {
6596
        /* $this does not need to be explicitly imported. */
6597
222
        return;
6598
222
      }
6599
6600
14.1k
      zend_hash_add_empty_element(&info->uses, name);
6601
623
    } else {
6602
623
      info->varvars_used = 1;
6603
623
      find_implicit_binds_recursively(info, name_ast);
6604
623
    }
6605
89.2k
  } else if (zend_ast_is_list(ast)) {
6606
22.8k
    zend_ast_list *list = zend_ast_get_list(ast);
6607
22.8k
    uint32_t i;
6608
35.8k
    for (i = 0; i < list->children; i++) {
6609
12.9k
      find_implicit_binds_recursively(info, list->child[i]);
6610
12.9k
    }
6611
66.3k
  } else if (ast->kind == ZEND_AST_CLOSURE) {
6612
    /* For normal closures add the use() list. */
6613
115
    zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
6614
115
    zend_ast *uses_ast = closure_ast->child[1];
6615
115
    if (uses_ast) {
6616
115
      zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
6617
115
      uint32_t i;
6618
230
      for (i = 0; i < uses_list->children; i++) {
6619
115
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
6620
115
      }
6621
115
    }
6622
66.2k
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
6623
    /* For arrow functions recursively check the expression. */
6624
211
    zend_ast_decl *closure_ast = (zend_ast_decl *) ast;
6625
211
    find_implicit_binds_recursively(info, closure_ast->child[2]);
6626
66.0k
  } else if (!zend_ast_is_special(ast)) {
6627
40.1k
    uint32_t i, children = zend_ast_get_num_children(ast);
6628
115k
    for (i = 0; i < children; i++) {
6629
75.7k
      find_implicit_binds_recursively(info, ast->child[i]);
6630
75.7k
    }
6631
40.1k
  }
6632
104k
}
6633
6634
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
6635
15.8k
{
6636
15.8k
  zend_ast_list *param_list = zend_ast_get_list(params_ast);
6637
15.8k
  uint32_t i;
6638
6639
15.8k
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
6640
6641
15.8k
  find_implicit_binds_recursively(info, stmt_ast);
6642
6643
  /* Remove variables that are parameters */
6644
20.1k
  for (i = 0; i < param_list->children; i++) {
6645
4.34k
    zend_ast *param_ast = param_list->child[i];
6646
4.34k
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
6647
4.34k
  }
6648
15.8k
}
6649
6650
static void compile_implicit_lexical_binds(
6651
    closure_info *info, znode *closure, zend_op_array *op_array)
6652
15.8k
{
6653
15.8k
  zend_string *var_name;
6654
15.8k
  zend_op *opline;
6655
6656
  /* TODO We might want to use a special binding mode if varvars_used is set. */
6657
15.8k
  if (zend_hash_num_elements(&info->uses) == 0) {
6658
5.97k
    return;
6659
5.97k
  }
6660
6661
9.85k
  if (!op_array->static_variables) {
6662
9.85k
    op_array->static_variables = zend_new_array(8);
6663
9.85k
  }
6664
6665
30.2k
  ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
6666
30.2k
    zval *value = zend_hash_add(
6667
10.1k
      op_array->static_variables, var_name, &EG(uninitialized_zval));
6668
30.2k
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
6669
6670
10.1k
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
6671
10.1k
    opline->op2_type = IS_CV;
6672
30.2k
    opline->op2.var = lookup_cv(var_name);
6673
10.1k
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
6674
10.1k
  ZEND_HASH_FOREACH_END();
6675
9.85k
}
6676
6677
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
6678
7.34k
{
6679
7.34k
  zend_op_array *op_array = CG(active_op_array);
6680
7.34k
  zend_ast_list *list = zend_ast_get_list(ast);
6681
7.34k
  uint32_t i;
6682
6683
17.9k
  for (i = 0; i < list->children; ++i) {
6684
10.6k
    zend_ast *var_ast = list->child[i];
6685
10.6k
    zend_string *var_name = zend_ast_get_str(var_ast);
6686
10.6k
    zval zv;
6687
10.6k
    ZVAL_NULL(&zv);
6688
6689
10.6k
    {
6690
10.6k
      int i;
6691
23.4k
      for (i = 0; i < op_array->last_var; i++) {
6692
12.8k
        if (zend_string_equals(op_array->vars[i], var_name)) {
6693
99
          zend_error_noreturn(E_COMPILE_ERROR,
6694
99
            "Cannot use lexical variable $%s as a parameter name", ZSTR_VAL(var_name));
6695
99
        }
6696
12.8k
      }
6697
10.6k
    }
6698
6699
10.5k
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
6700
6701
7.88k
    zend_compile_static_var_common(var_name, &zv, var_ast->attr ? ZEND_BIND_REF : 0);
6702
10.5k
  }
6703
7.34k
}
6704
/* }}} */
6705
6706
static void zend_compile_implicit_closure_uses(closure_info *info)
6707
15.8k
{
6708
15.8k
  zend_string *var_name;
6709
36.1k
  ZEND_HASH_FOREACH_STR_KEY(&info->uses, var_name)
6710
36.1k
    zval zv;
6711
10.1k
    ZVAL_NULL(&zv);
6712
10.1k
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
6713
10.1k
  ZEND_HASH_FOREACH_END();
6714
15.8k
}
6715
6716
7.22k
static void add_stringable_interface(zend_class_entry *ce) {
6717
7.23k
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
6718
27
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
6719
      /* Interface already explicitly implemented */
6720
22
      return;
6721
22
    }
6722
27
  }
6723
6724
7.20k
  ce->num_interfaces++;
6725
7.20k
  ce->interface_names =
6726
7.20k
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
6727
  // TODO: Add known interned strings instead?
6728
7.20k
  ce->interface_names[ce->num_interfaces - 1].name =
6729
7.20k
    zend_string_init("Stringable", sizeof("Stringable") - 1, 0);
6730
7.20k
  ce->interface_names[ce->num_interfaces - 1].lc_name =
6731
7.20k
    zend_string_init("stringable", sizeof("stringable") - 1, 0);
6732
7.20k
}
6733
6734
zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
6735
220k
{
6736
220k
  zend_class_entry *ce = CG(active_class_entry);
6737
220k
  zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
6738
220k
  uint32_t fn_flags = op_array->fn_flags;
6739
6740
220k
  zend_string *lcname;
6741
6742
220k
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
6743
0
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
6744
0
  }
6745
6746
220k
  if (in_interface) {
6747
2.48k
    if (!(fn_flags & ZEND_ACC_PUBLIC) || (fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_ABSTRACT))) {
6748
38
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
6749
38
        "%s::%s() must be omitted", ZSTR_VAL(ce->name), ZSTR_VAL(name));
6750
38
    }
6751
2.44k
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
6752
2.44k
  }
6753
6754
220k
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
6755
4.44k
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
6756
19
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
6757
19
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
6758
19
    }
6759
6760
4.42k
    if (has_body) {
6761
17
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
6762
17
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
6763
17
    }
6764
6765
4.40k
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
6766
216k
  } else if (!has_body) {
6767
20
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
6768
20
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
6769
20
  }
6770
6771
220k
  op_array->scope = ce;
6772
220k
  op_array->function_name = zend_string_copy(name);
6773
6774
220k
  lcname = zend_string_tolower(name);
6775
220k
  lcname = zend_new_interned_string(lcname);
6776
6777
220k
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
6778
105
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
6779
105
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
6780
105
  }
6781
6782
220k
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
6783
220k
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
6784
7.22k
    add_stringable_interface(ce);
6785
7.22k
  }
6786
6787
220k
  return lcname;
6788
220k
}
6789
/* }}} */
6790
6791
static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl, zend_bool toplevel) /* {{{ */
6792
168k
{
6793
168k
  zend_string *unqualified_name, *name, *lcname, *key;
6794
168k
  zend_op *opline;
6795
6796
168k
  unqualified_name = decl->name;
6797
168k
  op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
6798
168k
  lcname = zend_string_tolower(name);
6799
6800
168k
  if (FC(imports_function)) {
6801
152
    zend_string *import_name =
6802
152
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
6803
152
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
6804
38
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
6805
38
        "because the name is already in use", ZSTR_VAL(name));
6806
38
    }
6807
168k
  }
6808
6809
168k
  if (zend_string_equals_literal(lcname, "__autoload")) {
6810
0
    zend_error_noreturn(E_COMPILE_ERROR,
6811
0
      "__autoload() is no longer supported, use spl_autoload_register() instead");
6812
0
  }
6813
6814
168k
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
6815
19
    zend_error(E_COMPILE_ERROR,
6816
19
      "Defining a custom assert() function is not allowed, "
6817
19
      "as the function has special semantics");
6818
19
  }
6819
6820
168k
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
6821
168k
  if (toplevel) {
6822
107k
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
6823
122
      do_bind_function_error(lcname, op_array, 1);
6824
122
    }
6825
107k
    zend_string_release_ex(lcname, 0);
6826
107k
    return;
6827
107k
  }
6828
6829
  /* Generate RTD keys until we find one that isn't in use yet. */
6830
60.9k
  key = NULL;
6831
60.9k
  do {
6832
60.9k
    zend_tmp_string_release(key);
6833
60.9k
    key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
6834
60.9k
  } while (!zend_hash_add_ptr(CG(function_table), key, op_array));
6835
6836
60.9k
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
6837
60.6k
    opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
6838
60.6k
    opline->extended_value = zend_alloc_cache_slot();
6839
60.6k
    opline->op1_type = IS_CONST;
6840
60.6k
    LITERAL_STR(opline->op1, key);
6841
298
  } else {
6842
298
    opline = get_next_op();
6843
298
    opline->opcode = ZEND_DECLARE_FUNCTION;
6844
298
    opline->op1_type = IS_CONST;
6845
298
    LITERAL_STR(opline->op1, zend_string_copy(lcname));
6846
    /* RTD key is placed after lcname literal in op1 */
6847
298
    zend_add_literal_string(&key);
6848
298
  }
6849
60.9k
  zend_string_release_ex(lcname, 0);
6850
60.9k
}
6851
/* }}} */
6852
6853
void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* {{{ */
6854
389k
{
6855
389k
  zend_ast_decl *decl = (zend_ast_decl *) ast;
6856
389k
  zend_ast *params_ast = decl->child[0];
6857
389k
  zend_ast *uses_ast = decl->child[1];
6858
389k
  zend_ast *stmt_ast = decl->child[2];
6859
389k
  zend_ast *return_type_ast = decl->child[3];
6860
389k
  zend_bool is_method = decl->kind == ZEND_AST_METHOD;
6861
389k
  zend_string *method_lcname;
6862
6863
389k
  zend_class_entry *orig_class_entry = CG(active_class_entry);
6864
389k
  zend_op_array *orig_op_array = CG(active_op_array);
6865
389k
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
6866
389k
  zend_oparray_context orig_oparray_context;
6867
389k
  closure_info info;
6868
389k
  memset(&info, 0, sizeof(closure_info));
6869
6870
389k
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
6871
6872
389k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
6873
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
6874
0
    ZEND_MAP_PTR_NEW(op_array->run_time_cache);
6875
0
    ZEND_MAP_PTR_NEW(op_array->static_variables_ptr);
6876
389k
  } else {
6877
389k
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
6878
389k
    ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
6879
389k
  }
6880
6881
389k
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
6882
389k
  op_array->fn_flags |= decl->flags;
6883
389k
  op_array->line_start = decl->start_lineno;
6884
389k
  op_array->line_end = decl->end_lineno;
6885
389k
  if (decl->doc_comment) {
6886
192
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
6887
192
  }
6888
6889
389k
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
6890
60.6k
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
6891
60.6k
  }
6892
6893
389k
  if (is_method) {
6894
220k
    zend_bool has_body = stmt_ast != NULL;
6895
220k
    method_lcname = zend_begin_method_decl(op_array, decl->name, has_body);
6896
168k
  } else {
6897
168k
    zend_begin_func_decl(result, op_array, decl, toplevel);
6898
168k
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
6899
15.8k
      find_implicit_binds(&info, params_ast, stmt_ast);
6900
15.8k
      compile_implicit_lexical_binds(&info, result, op_array);
6901
152k
    } else if (uses_ast) {
6902
7.40k
      zend_compile_closure_binding(result, op_array, uses_ast);
6903
7.40k
    }
6904
168k
  }
6905
6906
389k
  CG(active_op_array) = op_array;
6907
6908
389k
  if (decl->child[4]) {
6909
9.12k
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
6910
6911
9.12k
    if (is_method) {
6912
1.73k
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
6913
1.73k
    }
6914
6915
9.12k
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target);
6916
9.12k
  }
6917
6918
  /* Do not leak the class scope into free standing functions, even if they are dynamically
6919
   * defined inside a class method. This is necessary for correct handling of magic constants.
6920
   * For example __CLASS__ should always be "" inside a free standing function. */
6921
389k
  if (decl->kind == ZEND_AST_FUNC_DECL) {
6922
107k
    CG(active_class_entry) = NULL;
6923
107k
  }
6924
6925
389k
  if (toplevel) {
6926
107k
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
6927
107k
  }
6928
6929
389k
  zend_oparray_context_begin(&orig_oparray_context);
6930
6931
389k
  if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) {
6932
0
    zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
6933
0
    opline_ext->lineno = decl->start_lineno;
6934
0
  }
6935
6936
389k
  {
6937
    /* Push a separator to the loop variable stack */
6938
389k
    zend_loop_var dummy_var;
6939
389k
    dummy_var.opcode = ZEND_RETURN;
6940
6941
389k
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
6942
389k
  }
6943
6944
389k
  zend_compile_params(params_ast, return_type_ast,
6945
389k
    is_method && zend_string_equals_literal(method_lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
6946
389k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
6947
22.1k
    zend_mark_function_as_generator();
6948
22.1k
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
6949
22.1k
  }
6950
389k
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
6951
15.8k
    zend_compile_implicit_closure_uses(&info);
6952
15.8k
    zend_hash_destroy(&info.uses);
6953
373k
  } else if (uses_ast) {
6954
7.34k
    zend_compile_closure_uses(uses_ast);
6955
7.34k
  }
6956
389k
  zend_compile_stmt(stmt_ast);
6957
6958
389k
  if (is_method) {
6959
220k
    CG(zend_lineno) = decl->start_lineno;
6960
220k
    zend_check_magic_method_implementation(
6961
220k
      CG(active_class_entry), (zend_function *) op_array, method_lcname, E_COMPILE_ERROR);
6962
220k
    zend_string_release_ex(method_lcname, 0);
6963
220k
  }
6964
6965
  /* put the implicit return on the really last line */
6966
389k
  CG(zend_lineno) = decl->end_lineno;
6967
6968
389k
  zend_do_extended_stmt();
6969
389k
  zend_emit_final_return(0);
6970
6971
389k
  pass_two(CG(active_op_array));
6972
389k
  zend_oparray_context_end(&orig_oparray_context);
6973
6974
  /* Pop the loop variable stack separator */
6975
389k
  zend_stack_del_top(&CG(loop_var_stack));
6976
6977
389k
  CG(active_op_array) = orig_op_array;
6978
389k
  CG(active_class_entry) = orig_class_entry;
6979
389k
}
6980
/* }}} */
6981
6982
void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
6983
105k
{
6984
105k
  zend_ast_list *list = zend_ast_get_list(ast);
6985
105k
  zend_class_entry *ce = CG(active_class_entry);
6986
105k
  uint32_t i, children = list->children;
6987
6988
105k
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
6989
2
    zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
6990
2
  }
6991
6992
105k
  if (flags & ZEND_ACC_ABSTRACT) {
6993
19
    zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
6994
19
  }
6995
6996
214k
  for (i = 0; i < children; ++i) {
6997
108k
    zend_property_info *info;
6998
108k
    zend_ast *prop_ast = list->child[i];
6999
108k
    zend_ast *name_ast = prop_ast->child[0];
7000
108k
    zend_ast *value_ast = prop_ast->child[1];
7001
108k
    zend_ast *doc_comment_ast = prop_ast->child[2];
7002
108k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
7003
108k
    zend_string *doc_comment = NULL;
7004
108k
    zval value_zv;
7005
108k
    zend_type type = ZEND_TYPE_INIT_NONE(0);
7006
7007
108k
    if (type_ast) {
7008
43.2k
      type = zend_compile_typename(type_ast, /* force_allow_null */ 0, /* use_arena */ 1);
7009
7010
43.2k
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_CALLABLE)) {
7011
57
        zend_string *str = zend_type_to_string(type);
7012
57
        zend_error_noreturn(E_COMPILE_ERROR,
7013
57
          "Property %s::$%s cannot have type %s",
7014
57
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
7015
57
      }
7016
108k
    }
7017
7018
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
7019
108k
    if (doc_comment_ast) {
7020
1.23k
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
7021
1.23k
    }
7022
7023
108k
    if (flags & ZEND_ACC_FINAL) {
7024
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
7025
19
        "the final modifier is allowed only for methods and classes",
7026
19
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
7027
19
    }
7028
7029
108k
    if (zend_hash_exists(&ce->properties_info, name)) {
7030
33
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
7031
33
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
7032
33
    }
7033
7034
108k
    if (value_ast) {
7035
36.3k
      zend_const_expr_to_zval(&value_zv, value_ast);
7036
7037
36.3k
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
7038
5.60k
          && !zend_is_valid_default_value(type, &value_zv)) {
7039
139
        zend_string *str = zend_type_to_string(type);
7040
139
        if (Z_TYPE(value_zv) == IS_NULL) {
7041
38
          zend_error_noreturn(E_COMPILE_ERROR,
7042
38
            "Default value for property of type %s may not be null. "
7043
38
            "Use the nullable type ?%s to allow null default value",
7044
38
            ZSTR_VAL(str), ZSTR_VAL(str));
7045
101
        } else {
7046
101
          zend_error_noreturn(E_COMPILE_ERROR,
7047
101
            "Cannot use %s as default value for property %s::$%s of type %s",
7048
101
            zend_zval_type_name(&value_zv),
7049
101
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
7050
101
        }
7051
72.1k
      }
7052
72.1k
    } else if (!ZEND_TYPE_IS_SET(type)) {
7053
34.9k
      ZVAL_NULL(&value_zv);
7054
37.2k
    } else {
7055
37.2k
      ZVAL_UNDEF(&value_zv);
7056
37.2k
    }
7057
7058
108k
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
7059
7060
108k
    if (attr_ast) {
7061
2.06k
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY);
7062
2.06k
    }
7063
108k
  }
7064
105k
}
7065
/* }}} */
7066
7067
void zend_compile_prop_group(zend_ast *ast) /* {{{ */
7068
105k
{
7069
105k
  zend_ast *type_ast = ast->child[0];
7070
105k
  zend_ast *prop_ast = ast->child[1];
7071
105k
  zend_ast *attr_ast = ast->child[2];
7072
7073
105k
  if (attr_ast && zend_ast_get_list(prop_ast)->children > 1) {
7074
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of properties");
7075
0
    return;
7076
105k
  }
7077
7078
105k
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
7079
105k
}
7080
/* }}} */
7081
7082
static void zend_check_const_and_trait_alias_attr(uint32_t attr, const char* entity) /* {{{ */
7083
2.54k
{
7084
2.54k
  if (attr & ZEND_ACC_STATIC) {
7085
21
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as %s modifier", entity);
7086
2.52k
  } else if (attr & ZEND_ACC_ABSTRACT) {
7087
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as %s modifier", entity);
7088
2.50k
  } else if (attr & ZEND_ACC_FINAL) {
7089
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as %s modifier", entity);
7090
19
  }
7091
2.54k
}
7092
/* }}} */
7093
7094
void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
7095
49.8k
{
7096
49.8k
  zend_ast_list *list = zend_ast_get_list(ast);
7097
49.8k
  zend_class_entry *ce = CG(active_class_entry);
7098
49.8k
  uint32_t i, children = list->children;
7099
7100
49.8k
  if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
7101
19
    zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
7102
0
    return;
7103
49.8k
  }
7104
7105
99.8k
  for (i = 0; i < children; ++i) {
7106
49.9k
    zend_class_constant *c;
7107
49.9k
    zend_ast *const_ast = list->child[i];
7108
49.9k
    zend_ast *name_ast = const_ast->child[0];
7109
49.9k
    zend_ast *value_ast = const_ast->child[1];
7110
49.9k
    zend_ast *doc_comment_ast = const_ast->child[2];
7111
49.9k
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
7112
91
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
7113
49.9k
    zval value_zv;
7114
7115
49.9k
    if (UNEXPECTED(flags & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
7116
2
      zend_check_const_and_trait_alias_attr(flags, "constant");
7117
2
    }
7118
7119
49.9k
    zend_const_expr_to_zval(&value_zv, value_ast);
7120
49.9k
    c = zend_declare_class_constant_ex(ce, name, &value_zv, flags, doc_comment);
7121
7122
49.9k
    if (attr_ast) {
7123
933
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
7124
933
    }
7125
49.9k
  }
7126
49.8k
}
7127
/* }}} */
7128
7129
void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
7130
49.8k
{
7131
49.8k
  zend_ast *const_ast = ast->child[0];
7132
49.8k
  zend_ast *attr_ast = ast->child[1];
7133
7134
49.8k
  if (attr_ast && zend_ast_get_list(const_ast)->children > 1) {
7135
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of constants");
7136
7137
0
    return;
7138
49.8k
  }
7139
7140
49.8k
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast);
7141
49.8k
}
7142
/* }}} */
7143
7144
static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
7145
3.17k
{
7146
3.17k
  zend_ast *class_ast = ast->child[0];
7147
3.17k
  zend_ast *method_ast = ast->child[1];
7148
7149
3.17k
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
7150
7151
3.17k
  if (class_ast) {
7152
2.02k
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
7153
1.14k
  } else {
7154
1.14k
    method_ref->class_name = NULL;
7155
1.14k
  }
7156
3.17k
}
7157
/* }}} */
7158
7159
static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
7160
683
{
7161
683
  zend_ast *method_ref_ast = ast->child[0];
7162
683
  zend_ast *insteadof_ast = ast->child[1];
7163
683
  zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
7164
683
  uint32_t i;
7165
7166
683
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
7167
683
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
7168
683
  precedence->num_excludes = insteadof_list->children;
7169
7170
1.39k
  for (i = 0; i < insteadof_list->children; ++i) {
7171
713
    zend_ast *name_ast = insteadof_list->child[i];
7172
713
    precedence->exclude_class_names[i] =
7173
713
      zend_resolve_const_class_name_reference(name_ast, "trait name");
7174
713
  }
7175
7176
683
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
7177
683
}
7178
/* }}} */
7179
7180
static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
7181
2.54k
{
7182
2.54k
  zend_ast *method_ref_ast = ast->child[0];
7183
2.54k
  zend_ast *alias_ast = ast->child[1];
7184
2.54k
  uint32_t modifiers = ast->attr;
7185
7186
2.54k
  zend_trait_alias *alias;
7187
7188
2.54k
  zend_check_const_and_trait_alias_attr(modifiers, "method");
7189
7190
2.54k
  alias = emalloc(sizeof(zend_trait_alias));
7191
2.54k
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
7192
2.54k
  alias->modifiers = modifiers;
7193
7194
2.54k
  if (alias_ast) {
7195
2.13k
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
7196
407
  } else {
7197
407
    alias->alias = NULL;
7198
407
  }
7199
7200
2.54k
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
7201
2.54k
}
7202
/* }}} */
7203
7204
void zend_compile_use_trait(zend_ast *ast) /* {{{ */
7205
8.48k
{
7206
8.48k
  zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
7207
1.82k
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
7208
8.48k
  zend_class_entry *ce = CG(active_class_entry);
7209
8.48k
  uint32_t i;
7210
7211
8.48k
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
7212
7213
18.1k
  for (i = 0; i < traits->children; ++i) {
7214
9.68k
    zend_ast *trait_ast = traits->child[i];
7215
7216
9.68k
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7217
19
      zend_string *name = zend_ast_get_str(trait_ast);
7218
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
7219
19
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
7220
19
    }
7221
7222
9.66k
    ce->trait_names[ce->num_traits].name =
7223
9.66k
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
7224
9.66k
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
7225
9.66k
    ce->num_traits++;
7226
9.66k
  }
7227
7228
8.47k
  if (!adaptations) {
7229
6.62k
    return;
7230
6.62k
  }
7231
7232
4.97k
  for (i = 0; i < adaptations->children; ++i) {
7233
3.22k
    zend_ast *adaptation_ast = adaptations->child[i];
7234
3.22k
    switch (adaptation_ast->kind) {
7235
683
      case ZEND_AST_TRAIT_PRECEDENCE:
7236
683
        zend_compile_trait_precedence(adaptation_ast);
7237
683
        break;
7238
2.54k
      case ZEND_AST_TRAIT_ALIAS:
7239
2.54k
        zend_compile_trait_alias(adaptation_ast);
7240
2.54k
        break;
7241
0
      EMPTY_SWITCH_DEFAULT_CASE()
7242
3.22k
    }
7243
3.22k
  }
7244
1.84k
}
7245
/* }}} */
7246
7247
void zend_compile_implements(zend_ast *ast) /* {{{ */
7248
12.8k
{
7249
12.8k
  zend_ast_list *list = zend_ast_get_list(ast);
7250
12.8k
  zend_class_entry *ce = CG(active_class_entry);
7251
12.8k
  zend_class_name *interface_names;
7252
12.8k
  uint32_t i;
7253
7254
12.8k
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
7255
7256
26.3k
  for (i = 0; i < list->children; ++i) {
7257
13.4k
    zend_ast *class_ast = list->child[i];
7258
13.4k
    interface_names[i].name =
7259
13.4k
      zend_resolve_const_class_name_reference(class_ast, "interface name");
7260
13.4k
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
7261
13.4k
  }
7262
7263
12.8k
  ce->num_interfaces = list->children;
7264
12.8k
  ce->interface_names = interface_names;
7265
12.8k
}
7266
/* }}} */
7267
7268
static zend_string *zend_generate_anon_class_name(zend_ast_decl *decl)
7269
22.2k
{
7270
22.2k
  zend_string *filename = CG(active_op_array)->filename;
7271
22.2k
  uint32_t start_lineno = decl->start_lineno;
7272
7273
  /* Use parent or first interface as prefix. */
7274
22.2k
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
7275
22.2k
  if (decl->child[0]) {
7276
337
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
7277
21.8k
  } else if (decl->child[1]) {
7278
2.89k
    zend_ast_list *list = zend_ast_get_list(decl->child[1]);
7279
2.89k
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
7280
2.89k
  }
7281
7282
22.2k
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
7283
22.2k
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
7284
22.2k
  zend_string_release(prefix);
7285
22.2k
  return zend_new_interned_string(result);
7286
22.2k
}
7287
7288
void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /* {{{ */
7289
183k
{
7290
183k
  zend_ast_decl *decl = (zend_ast_decl *) ast;
7291
183k
  zend_ast *extends_ast = decl->child[0];
7292
183k
  zend_ast *implements_ast = decl->child[1];
7293
183k
  zend_ast *stmt_ast = decl->child[2];
7294
183k
  zend_string *name, *lcname;
7295
183k
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
7296
183k
  zend_op *opline;
7297
7298
183k
  zend_class_entry *original_ce = CG(active_class_entry);
7299
7300
183k
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
7301
161k
    zend_string *unqualified_name = decl->name;
7302
7303
161k
    if (CG(active_class_entry)) {
7304
19
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
7305
19
    }
7306
7307
161k
    zend_assert_valid_class_name(unqualified_name);
7308
161k
    name = zend_prefix_with_ns(unqualified_name);
7309
161k
    name = zend_new_interned_string(name);
7310
161k
    lcname = zend_string_tolower(name);
7311
7312
161k
    if (FC(imports)) {
7313
3.19k
      zend_string *import_name =
7314
3.19k
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
7315
3.19k
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
7316
38
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
7317
38
            "because the name is already in use", ZSTR_VAL(name));
7318
38
      }
7319
161k
    }
7320
7321
161k
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
7322
22.2k
  } else {
7323
    /* Find an anon class name that is not in use yet. */
7324
22.2k
    name = NULL;
7325
22.2k
    lcname = NULL;
7326
22.2k
    do {
7327
22.2k
      zend_tmp_string_release(name);
7328
22.2k
      zend_tmp_string_release(lcname);
7329
22.2k
      name = zend_generate_anon_class_name(decl);
7330
22.2k
      lcname = zend_string_tolower(name);
7331
22.2k
    } while (zend_hash_exists(CG(class_table), lcname));
7332
22.2k
  }
7333
183k
  lcname = zend_new_interned_string(lcname);
7334
7335
183k
  ce->type = ZEND_USER_CLASS;
7336
183k
  ce->name = name;
7337
183k
  zend_initialize_class_data(ce, 1);
7338
7339
183k
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
7340
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
7341
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
7342
0
  }
7343
7344
183k
  ce->ce_flags |= decl->flags;
7345
183k
  ce->info.user.filename = zend_get_compiled_filename();
7346
183k
  ce->info.user.line_start = decl->start_lineno;
7347
183k
  ce->info.user.line_end = decl->end_lineno;
7348
7349
183k
  if (decl->doc_comment) {
7350
489
    ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
7351
489
  }
7352
7353
183k
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
7354
    /* Serialization is not supported for anonymous classes */
7355
22.2k
    ce->serialize = zend_class_serialize_deny;
7356
22.2k
    ce->unserialize = zend_class_unserialize_deny;
7357
22.2k
  }
7358
7359
183k
  if (extends_ast) {
7360
35.8k
    ce->parent_name =
7361
35.8k
      zend_resolve_const_class_name_reference(extends_ast, "class name");
7362
35.8k
  }
7363
7364
183k
  CG(active_class_entry) = ce;
7365
7366
183k
  if (decl->child[4]) {
7367
6.03k
    zend_compile_attributes(&ce->attributes, decl->child[4], 0, ZEND_ATTRIBUTE_TARGET_CLASS);
7368
6.03k
  }
7369
7370
183k
  if (implements_ast) {
7371
12.8k
    zend_compile_implements(implements_ast);
7372
12.8k
  }
7373
7374
183k
  zend_compile_stmt(stmt_ast);
7375
7376
  /* Reset lineno for final opcodes and errors */
7377
183k
  CG(zend_lineno) = ast->lineno;
7378
7379
183k
  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) {
7380
19
    zend_verify_abstract_class(ce);
7381
19
  }
7382
7383
183k
  CG(active_class_entry) = original_ce;
7384
7385
183k
  if (toplevel) {
7386
154k
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
7387
154k
  }
7388
7389
183k
  if (toplevel
7390
    /* We currently don't early-bind classes that implement interfaces or use traits */
7391
154k
   && !ce->num_interfaces && !ce->num_traits
7392
132k
   && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD)) {
7393
132k
    if (extends_ast) {
7394
31.1k
      zend_class_entry *parent_ce = zend_lookup_class_ex(
7395
31.1k
        ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
7396
7397
31.1k
      if (parent_ce
7398
27.9k
       && ((parent_ce->type != ZEND_INTERNAL_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES))
7399
27.9k
       && ((parent_ce->type != ZEND_USER_CLASS) || !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) || (parent_ce->info.user.filename == ce->info.user.filename))) {
7400
7401
27.9k
        CG(zend_lineno) = decl->end_lineno;
7402
27.9k
        if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
7403
25.0k
          CG(zend_lineno) = ast->lineno;
7404
25.0k
          zend_string_release(lcname);
7405
25.0k
          return;
7406
25.0k
        }
7407
2.89k
        CG(zend_lineno) = ast->lineno;
7408
2.89k
      }
7409
101k
    } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
7410
101k
      zend_string_release(lcname);
7411
101k
      zend_build_properties_info_table(ce);
7412
101k
      ce->ce_flags |= ZEND_ACC_LINKED;
7413
101k
      return;
7414
101k
    }
7415
57.5k
  }
7416
7417
57.5k
  opline = get_next_op();
7418
7419
57.5k
  if (ce->parent_name) {
7420
    /* Lowercased parent name */
7421
8.87k
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
7422
8.87k
    opline->op2_type = IS_CONST;
7423
8.87k
    LITERAL_STR(opline->op2, lc_parent_name);
7424
8.87k
  }
7425
7426
57.5k
  opline->op1_type = IS_CONST;
7427
57.5k
  LITERAL_STR(opline->op1, lcname);
7428
7429
57.5k
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
7430
22.1k
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
7431
22.1k
    opline->extended_value = zend_alloc_cache_slot();
7432
22.1k
    zend_make_var_result(result, opline);
7433
22.1k
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
7434
      /* We checked above that the class name is not used. This really shouldn't happen. */
7435
0
      zend_error_noreturn(E_ERROR,
7436
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
7437
0
    }
7438
35.3k
  } else {
7439
    /* Generate RTD keys until we find one that isn't in use yet. */
7440
35.3k
    zend_string *key = NULL;
7441
35.3k
    do {
7442
35.3k
      zend_tmp_string_release(key);
7443
35.3k
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
7444
35.3k
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
7445
7446
    /* RTD key is placed after lcname literal in op1 */
7447
35.3k
    zend_add_literal_string(&key);
7448
7449
35.3k
    opline->opcode = ZEND_DECLARE_CLASS;
7450
35.3k
    if (extends_ast && toplevel
7451
6.83k
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
7452
        /* We currently don't early-bind classes that implement interfaces or use traits */
7453
0
       && !ce->num_interfaces && !ce->num_traits
7454
0
    ) {
7455
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
7456
0
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
7457
0
      opline->extended_value = zend_alloc_cache_slot();
7458
0
      opline->result_type = IS_UNUSED;
7459
0
      opline->result.opline_num = -1;
7460
0
    }
7461
35.3k
  }
7462
57.5k
}
7463
/* }}} */
7464
7465
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
7466
6.11k
{
7467
6.11k
  switch (type) {
7468
4.40k
    case ZEND_SYMBOL_CLASS:
7469
4.40k
      if (!FC(imports)) {
7470
3.21k
        FC(imports) = emalloc(sizeof(HashTable));
7471
3.21k
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
7472
3.21k
      }
7473
4.40k
      return FC(imports);
7474
886
    case ZEND_SYMBOL_FUNCTION:
7475
886
      if (!FC(imports_function)) {
7476
588
        FC(imports_function) = emalloc(sizeof(HashTable));
7477
588
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
7478
588
      }
7479
886
      return FC(imports_function);
7480
818
    case ZEND_SYMBOL_CONST:
7481
818
      if (!FC(imports_const)) {
7482
554
        FC(imports_const) = emalloc(sizeof(HashTable));
7483
554
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
7484
554
      }
7485
818
      return FC(imports_const);
7486
0
    EMPTY_SWITCH_DEFAULT_CASE()
7487
6.11k
  }
7488
7489
0
  return NULL;
7490
6.11k
}
7491
/* }}} */
7492
7493
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
7494
219
{
7495
219
  switch (type) {
7496
100
    case ZEND_SYMBOL_CLASS:
7497
100
      return "";
7498
81
    case ZEND_SYMBOL_FUNCTION:
7499
81
      return " function";
7500
38
    case ZEND_SYMBOL_CONST:
7501
38
      return " const";
7502
0
    EMPTY_SWITCH_DEFAULT_CASE()
7503
219
  }
7504
7505
0
  return " unknown";
7506
219
}
7507
/* }}} */
7508
7509
static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
7510
142
{
7511
142
  if (zend_string_equals_ci(old_name, check_name)) {
7512
23
    return;
7513
23
  }
7514
7515
119
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
7516
119
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
7517
119
}
7518
/* }}} */
7519
7520
void zend_compile_use(zend_ast *ast) /* {{{ */
7521
6.11k
{
7522
6.11k
  zend_ast_list *list = zend_ast_get_list(ast);
7523
6.11k
  uint32_t i;
7524
6.11k
  zend_string *current_ns = FC(current_namespace);
7525
6.11k
  uint32_t type = ast->attr;
7526
6.11k
  HashTable *current_import = zend_get_import_ht(type);
7527
6.11k
  zend_bool case_sensitive = type == ZEND_SYMBOL_CONST;
7528
7529
12.2k
  for (i = 0; i < list->children; ++i) {
7530
6.30k
    zend_ast *use_ast = list->child[i];
7531
6.30k
    zend_ast *old_name_ast = use_ast->child[0];
7532
6.30k
    zend_ast *new_name_ast = use_ast->child[1];
7533
6.30k
    zend_string *old_name = zend_ast_get_str(old_name_ast);
7534
6.30k
    zend_string *new_name, *lookup_name;
7535
7536
6.30k
    if (new_name_ast) {
7537
3.13k
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
7538
3.17k
    } else {
7539
3.17k
      const char *unqualified_name;
7540
3.17k
      size_t unqualified_name_len;
7541
3.17k
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
7542
        /* The form "use A\B" is equivalent to "use A\B as B" */
7543
2.49k
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
7544
683
      } else {
7545
683
        new_name = zend_string_copy(old_name);
7546
7547
683
        if (!current_ns) {
7548
158
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
7549
158
            "has no effect", ZSTR_VAL(new_name));
7550
158
        }
7551
683
      }
7552
3.17k
    }
7553
7554
6.30k
    if (case_sensitive) {
7555
875
      lookup_name = zend_string_copy(new_name);
7556
5.43k
    } else {
7557
5.43k
      lookup_name = zend_string_tolower(new_name);
7558
5.43k
    }
7559
7560
6.30k
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
7561
95
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
7562
95
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
7563
95
    }
7564
7565
6.21k
    if (current_ns) {
7566
2.94k
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
7567
2.94k
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
7568
2.94k
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
7569
2.94k
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
7570
7571
2.94k
      if (zend_have_seen_symbol(ns_name, type)) {
7572
42
        zend_check_already_in_use(type, old_name, new_name, ns_name);
7573
42
      }
7574
7575
2.94k
      zend_string_efree(ns_name);
7576
3.26k
    } else if (zend_have_seen_symbol(lookup_name, type)) {
7577
100
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
7578
100
    }
7579
7580
6.21k
    zend_string_addref(old_name);
7581
6.21k
    old_name = zend_new_interned_string(old_name);
7582
6.21k
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
7583
100
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
7584
100
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
7585
100
    }
7586
7587
6.11k
    zend_string_release_ex(lookup_name, 0);
7588
6.11k
    zend_string_release_ex(new_name, 0);
7589
6.11k
  }
7590
6.11k
}
7591
/* }}} */
7592
7593
void zend_compile_group_use(zend_ast *ast) /* {{{ */
7594
613
{
7595
613
  uint32_t i;
7596
613
  zend_string *ns = zend_ast_get_str(ast->child[0]);
7597
613
  zend_ast_list *list = zend_ast_get_list(ast->child[1]);
7598
7599
2.25k
  for (i = 0; i < list->children; i++) {
7600
1.64k
    zend_ast *inline_use, *use = list->child[i];
7601
1.64k
    zval *name_zval = zend_ast_get_zval(use->child[0]);
7602
1.64k
    zend_string *name = Z_STR_P(name_zval);
7603
1.64k
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
7604
1.64k
    zend_string_release_ex(name, 0);
7605
1.64k
    ZVAL_STR(name_zval, compound_ns);
7606
1.64k
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
7607
1.19k
    inline_use->attr = ast->attr ? ast->attr : use->attr;
7608
1.64k
    zend_compile_use(inline_use);
7609
1.64k
  }
7610
613
}
7611
/* }}} */
7612
7613
void zend_compile_const_decl(zend_ast *ast) /* {{{ */
7614
30.8k
{
7615
30.8k
  zend_ast_list *list = zend_ast_get_list(ast);
7616
30.8k
  uint32_t i;
7617
61.6k
  for (i = 0; i < list->children; ++i) {
7618
30.9k
    zend_ast *const_ast = list->child[i];
7619
30.9k
    zend_ast *name_ast = const_ast->child[0];
7620
30.9k
    zend_ast *value_ast = const_ast->child[1];
7621
30.9k
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
7622
7623
30.9k
    zend_string *name;
7624
30.9k
    znode name_node, value_node;
7625
30.9k
    zval *value_zv = &value_node.u.constant;
7626
7627
30.9k
    value_node.op_type = IS_CONST;
7628
30.9k
    zend_const_expr_to_zval(value_zv, value_ast);
7629
7630
30.9k
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
7631
19
      zend_error_noreturn(E_COMPILE_ERROR,
7632
19
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
7633
19
    }
7634
7635
30.8k
    name = zend_prefix_with_ns(unqualified_name);
7636
30.8k
    name = zend_new_interned_string(name);
7637
7638
30.8k
    if (FC(imports_const)) {
7639
147
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
7640
147
      if (import_name && !zend_string_equals(import_name, name)) {
7641
38
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
7642
38
          "the name is already in use", ZSTR_VAL(name));
7643
38
      }
7644
30.8k
    }
7645
7646
30.8k
    name_node.op_type = IS_CONST;
7647
30.8k
    ZVAL_STR(&name_node.u.constant, name);
7648
7649
30.8k
    zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
7650
7651
30.8k
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
7652
30.8k
  }
7653
30.8k
}
7654
/* }}}*/
7655
7656
void zend_compile_namespace(zend_ast *ast) /* {{{ */
7657
10.2k
{
7658
10.2k
  zend_ast *name_ast = ast->child[0];
7659
10.2k
  zend_ast *stmt_ast = ast->child[1];
7660
10.2k
  zend_string *name;
7661
10.2k
  zend_bool with_bracket = stmt_ast != NULL;
7662
7663
  /* handle mixed syntax declaration or nested namespaces */
7664
10.2k
  if (!FC(has_bracketed_namespaces)) {
7665
7.06k
    if (FC(current_namespace)) {
7666
      /* previous namespace declarations were unbracketed */
7667
150
      if (with_bracket) {
7668
19
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
7669
19
          "with unbracketed namespace declarations");
7670
19
      }
7671
3.15k
    }
7672
3.15k
  } else {
7673
    /* previous namespace declarations were bracketed */
7674
3.15k
    if (!with_bracket) {
7675
37
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
7676
37
        "with unbracketed namespace declarations");
7677
3.11k
    } else if (FC(current_namespace) || FC(in_namespace)) {
7678
22
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
7679
22
    }
7680
10.1k
  }
7681
7682
10.1k
  if (((!with_bracket && !FC(current_namespace))
7683
6.91k
     || (with_bracket && !FC(has_bracketed_namespaces))) && CG(active_op_array)->last > 0
7684
151
  ) {
7685
    /* ignore ZEND_EXT_STMT and ZEND_TICKS */
7686
151
    uint32_t num = CG(active_op_array)->last;
7687
257
    while (num > 0 &&
7688
169
           (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
7689
169
            CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
7690
106
      --num;
7691
106
    }
7692
151
    if (num > 0) {
7693
63
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
7694
63
        "the very first statement or after any declare call in the script");
7695
63
    }
7696
10.0k
  }
7697
7698
10.0k
  if (FC(current_namespace)) {
7699
131
    zend_string_release_ex(FC(current_namespace), 0);
7700
131
  }
7701
7702
10.0k
  if (name_ast) {
7703
7.89k
    name = zend_ast_get_str(name_ast);
7704
7705
7.89k
    if (zend_string_equals_literal_ci(name, "namespace")) {
7706
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
7707
19
    }
7708
7709
7.87k
    FC(current_namespace) = zend_string_copy(name);
7710
2.18k
  } else {
7711
2.18k
    FC(current_namespace) = NULL;
7712
2.18k
  }
7713
7714
10.0k
  zend_reset_import_tables();
7715
7716
10.0k
  FC(in_namespace) = 1;
7717
10.0k
  if (with_bracket) {
7718
5.97k
    FC(has_bracketed_namespaces) = 1;
7719
5.97k
  }
7720
7721
10.0k
  if (stmt_ast) {
7722
5.97k
    zend_compile_top_stmt(stmt_ast);
7723
5.97k
    zend_end_namespace();
7724
5.97k
  }
7725
10.0k
}
7726
/* }}} */
7727
7728
void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
7729
630
{
7730
630
  zend_ast *offset_ast = ast->child[0];
7731
630
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
7732
7733
630
  zend_string *filename, *name;
7734
630
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
7735
7736
630
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
7737
0
    zend_error_noreturn(E_COMPILE_ERROR,
7738
0
      "__HALT_COMPILER() can only be used from the outermost scope");
7739
0
  }
7740
7741
630
  filename = zend_get_compiled_filename();
7742
630
  name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
7743
630
    ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
7744
7745
630
  zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, CONST_CS, 0);
7746
630
  zend_string_release_ex(name, 0);
7747
630
}
7748
/* }}} */
7749
7750
static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
7751
35.7k
{
7752
35.7k
  zend_op_array *op_array = CG(active_op_array);
7753
35.7k
  zend_class_entry *ce = CG(active_class_entry);
7754
7755
35.7k
  switch (ast->attr) {
7756
1.54k
    case T_LINE:
7757
1.54k
      ZVAL_LONG(zv, ast->lineno);
7758
1.54k
      break;
7759
3.52k
    case T_FILE:
7760
3.52k
      ZVAL_STR_COPY(zv, CG(compiled_filename));
7761
3.52k
      break;
7762
3.47k
    case T_DIR:
7763
3.47k
    {
7764
3.47k
      zend_string *filename = CG(compiled_filename);
7765
3.47k
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
7766
#ifdef ZEND_WIN32
7767
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
7768
#else
7769
3.47k
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
7770
3.47k
#endif
7771
7772
3.47k
      if (strcmp(ZSTR_VAL(dirname), ".") == 0) {
7773
0
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
7774
0
#if HAVE_GETCWD
7775
0
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
7776
#elif HAVE_GETWD
7777
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
7778
#endif
7779
0
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
7780
0
      }
7781
7782
3.47k
      ZVAL_STR(zv, dirname);
7783
3.47k
      break;
7784
0
    }
7785
7.59k
    case T_FUNC_C:
7786
7.59k
      if (op_array && op_array->function_name) {
7787
7.34k
        ZVAL_STR_COPY(zv, op_array->function_name);
7788
252
      } else {
7789
252
        ZVAL_EMPTY_STRING(zv);
7790
252
      }
7791
7.59k
      break;
7792
15.1k
    case T_METHOD_C:
7793
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
7794
       * this as not being inside a function. */
7795
15.1k
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
7796
59
        op_array = NULL;
7797
59
      }
7798
15.1k
      if (op_array && op_array->function_name) {
7799
14.7k
        if (op_array->scope) {
7800
14.4k
          ZVAL_NEW_STR(zv,
7801
14.4k
            zend_create_member_string(op_array->scope->name, op_array->function_name));
7802
293
        } else {
7803
293
          ZVAL_STR_COPY(zv, op_array->function_name);
7804
293
        }
7805
349
      } else {
7806
349
        ZVAL_EMPTY_STRING(zv);
7807
349
      }
7808
15.1k
      break;
7809
2.85k
    case T_CLASS_C:
7810
2.85k
      if (ce) {
7811
2.54k
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
7812
583
          return 0;
7813
1.96k
        } else {
7814
1.96k
          ZVAL_STR_COPY(zv, ce->name);
7815
1.96k
        }
7816
312
      } else {
7817
312
        ZVAL_EMPTY_STRING(zv);
7818
312
      }
7819
2.27k
      break;
7820
340
    case T_TRAIT_C:
7821
340
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
7822
19
        ZVAL_STR_COPY(zv, ce->name);
7823
321
      } else {
7824
321
        ZVAL_EMPTY_STRING(zv);
7825
321
      }
7826
340
      break;
7827
1.29k
    case T_NS_C:
7828
1.29k
      if (FC(current_namespace)) {
7829
687
        ZVAL_STR_COPY(zv, FC(current_namespace));
7830
611
      } else {
7831
611
        ZVAL_EMPTY_STRING(zv);
7832
611
      }
7833
1.29k
      break;
7834
0
    EMPTY_SWITCH_DEFAULT_CASE()
7835
35.7k
  }
7836
7837
35.1k
  return 1;
7838
35.7k
}
7839
/* }}} */
7840
7841
ZEND_API zend_bool zend_binary_op_produces_error(uint32_t opcode, zval *op1, zval *op2) /* {{{ */
7842
245k
{
7843
245k
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
7844
    /* Array to string warning. */
7845
58.6k
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
7846
58.6k
  }
7847
7848
187k
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
7849
26.6k
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
7850
18.4k
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
7851
    /* Only the numeric operations throw errors. */
7852
10.5k
    return 0;
7853
10.5k
  }
7854
7855
176k
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
7856
6.31k
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
7857
      /* Adding two arrays is allowed. */
7858
995
      return 0;
7859
995
    }
7860
7861
    /* Numeric operators throw when one of the operands is an array. */
7862
5.32k
    return 1;
7863
5.32k
  }
7864
7865
  /* While basic arithmetic operators always produce numeric string errors,
7866
   * bitwise operators don't produce errors if both operands are strings */
7867
170k
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
7868
7.86k
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
7869
354
    return 0;
7870
354
  }
7871
7872
169k
  if (Z_TYPE_P(op1) == IS_STRING
7873
20.4k
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
7874
12.7k
    return 1;
7875
12.7k
  }
7876
7877
157k
  if (Z_TYPE_P(op2) == IS_STRING
7878
12.1k
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
7879
6.94k
    return 1;
7880
6.94k
  }
7881
7882
150k
  if ((opcode == ZEND_MOD && zval_get_long(op2) == 0)
7883
150k
      || (opcode == ZEND_DIV && zval_get_double(op2) == 0.0)) {
7884
    /* Division by zero throws an error. */
7885
1.27k
    return 1;
7886
1.27k
  }
7887
148k
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
7888
    /* Shift by negative number throws an error. */
7889
114
    return 1;
7890
114
  }
7891
7892
148k
  return 0;
7893
148k
}
7894
/* }}} */
7895
7896
static inline zend_bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
7897
245k
{
7898
245k
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
7899
28.8k
    return 0;
7900
28.8k
  }
7901
7902
217k
  binary_op_type fn = get_binary_op(opcode);
7903
217k
  fn(result, op1, op2);
7904
217k
  return 1;
7905
217k
}
7906
/* }}} */
7907
7908
static inline void zend_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
7909
9.72k
{
7910
9.72k
  unary_op_type fn = get_unary_op(opcode);
7911
9.72k
  fn(result, op);
7912
9.72k
}
7913
/* }}} */
7914
7915
static inline zend_bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
7916
47.9k
{
7917
47.9k
  zval left;
7918
47.9k
  ZVAL_LONG(&left, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
7919
47.9k
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, &left, op);
7920
47.9k
}
7921
/* }}} */
7922
7923
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
7924
2.01k
{
7925
2.01k
  binary_op_type fn = kind == ZEND_AST_GREATER
7926
1.71k
    ? is_smaller_function : is_smaller_or_equal_function;
7927
2.01k
  fn(result, op2, op1);
7928
2.01k
}
7929
/* }}} */
7930
7931
static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
7932
327k
{
7933
327k
  zend_ast_list *list = zend_ast_get_list(ast);
7934
327k
  zend_ast *last_elem_ast = NULL;
7935
327k
  uint32_t i;
7936
327k
  zend_bool is_constant = 1;
7937
7938
327k
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
7939
0
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
7940
0
  }
7941
7942
  /* First ensure that *all* child nodes are constant and by-val */
7943
1.08M
  for (i = 0; i < list->children; ++i) {
7944
761k
    zend_ast *elem_ast = list->child[i];
7945
7946
761k
    if (elem_ast == NULL) {
7947
      /* Report error at line of last non-empty element */
7948
138
      if (last_elem_ast) {
7949
107
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
7950
107
      }
7951
138
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
7952
138
    }
7953
7954
761k
    if (elem_ast->kind != ZEND_AST_UNPACK) {
7955
757k
      zend_eval_const_expr(&elem_ast->child[0]);
7956
757k
      zend_eval_const_expr(&elem_ast->child[1]);
7957
7958
757k
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
7959
570k
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
7960
189k
      ) {
7961
189k
        is_constant = 0;
7962
189k
      }
7963
3.96k
    } else {
7964
3.96k
      zend_eval_const_expr(&elem_ast->child[0]);
7965
7966
3.96k
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
7967
3.05k
        is_constant = 0;
7968
3.05k
      }
7969
3.96k
    }
7970
7971
761k
    last_elem_ast = elem_ast;
7972
761k
  }
7973
7974
327k
  if (!is_constant) {
7975
129k
    return 0;
7976
129k
  }
7977
7978
197k
  if (!list->children) {
7979
64.3k
    ZVAL_EMPTY_ARRAY(result);
7980
64.3k
    return 1;
7981
64.3k
  }
7982
7983
133k
  array_init_size(result, list->children);
7984
516k
  for (i = 0; i < list->children; ++i) {
7985
383k
    zend_ast *elem_ast = list->child[i];
7986
383k
    zend_ast *value_ast = elem_ast->child[0];
7987
383k
    zend_ast *key_ast;
7988
7989
383k
    zval *value = zend_ast_get_zval(value_ast);
7990
383k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
7991
752
      if (Z_TYPE_P(value) == IS_ARRAY) {
7992
733
        HashTable *ht = Z_ARRVAL_P(value);
7993
733
        zval *val;
7994
733
        zend_string *key;
7995
7996
3.08k
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
7997
1.17k
          if (key) {
7998
19
            zend_error_noreturn(E_COMPILE_ERROR, "Cannot unpack array with string keys");
7999
19
          }
8000
1.15k
          if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
8001
58
            zval_ptr_dtor(result);
8002
58
            return 0;
8003
58
          }
8004
1.10k
          Z_TRY_ADDREF_P(val);
8005
1.10k
        } ZEND_HASH_FOREACH_END();
8006
8007
656
        continue;
8008
19
      } else {
8009
19
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked");
8010
19
      }
8011
382k
    }
8012
8013
382k
    Z_TRY_ADDREF_P(value);
8014
8015
382k
    key_ast = elem_ast->child[1];
8016
382k
    if (key_ast) {
8017
82.7k
      zval *key = zend_ast_get_zval(key_ast);
8018
82.7k
      switch (Z_TYPE_P(key)) {
8019
16.1k
        case IS_LONG:
8020
16.1k
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
8021
16.1k
          break;
8022
66.4k
        case IS_STRING:
8023
66.4k
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
8024
66.4k
          break;
8025
69
        case IS_DOUBLE:
8026
69
          zend_hash_index_update(Z_ARRVAL_P(result),
8027
69
            zend_dval_to_lval(Z_DVAL_P(key)), value);
8028
69
          break;
8029
109
        case IS_FALSE:
8030
109
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
8031
109
          break;
8032
57
        case IS_TRUE:
8033
57
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
8034
57
          break;
8035
15
        case IS_NULL:
8036
15
          zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
8037
15
          break;
8038
11
        default:
8039
11
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
8040
0
          break;
8041
299k
      }
8042
299k
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
8043
0
      zval_ptr_dtor_nogc(value);
8044
0
      zval_ptr_dtor(result);
8045
0
      return 0;
8046
0
    }
8047
382k
  }
8048
8049
133k
  return 1;
8050
133k
}
8051
/* }}} */
8052
8053
void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
8054
895k
{
8055
895k
  zend_ast *left_ast = ast->child[0];
8056
895k
  zend_ast *right_ast = ast->child[1];
8057
895k
  uint32_t opcode = ast->attr;
8058
8059
895k
  znode left_node, right_node;
8060
8061
895k
  zend_compile_expr(&left_node, left_ast);
8062
895k
  zend_compile_expr(&right_node, right_ast);
8063
8064
895k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
8065
177k
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
8066
177k
        &left_node.u.constant, &right_node.u.constant)
8067
153k
    ) {
8068
153k
      result->op_type = IS_CONST;
8069
153k
      zval_ptr_dtor(&left_node.u.constant);
8070
153k
      zval_ptr_dtor(&right_node.u.constant);
8071
153k
      return;
8072
153k
    }
8073
741k
  }
8074
8075
741k
  do {
8076
741k
    if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
8077
61.2k
      if (left_node.op_type == IS_CONST) {
8078
2.05k
        if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
8079
33
          opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
8080
33
          zend_emit_op_tmp(result, opcode, &right_node, NULL);
8081
33
          break;
8082
2.02k
        } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
8083
35
          opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
8084
35
          zend_emit_op_tmp(result, opcode, &right_node, NULL);
8085
35
          break;
8086
35
        }
8087
59.1k
      } else if (right_node.op_type == IS_CONST) {
8088
43.1k
        if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
8089
2.33k
          opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
8090
2.33k
          zend_emit_op_tmp(result, opcode, &left_node, NULL);
8091
2.33k
          break;
8092
40.7k
        } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
8093
1.31k
          opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
8094
1.31k
          zend_emit_op_tmp(result, opcode, &left_node, NULL);
8095
1.31k
          break;
8096
1.31k
        }
8097
680k
      }
8098
680k
    } else if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
8099
      /* 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) */
8100
23.1k
      if (left_node.op_type == IS_CONST) {
8101
558
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
8102
505
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
8103
505
          opline->extended_value =
8104
505
            (opcode == ZEND_IS_IDENTICAL) ?
8105
23
              (1 << Z_TYPE(left_node.u.constant)) :
8106
482
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
8107
505
          return;
8108
505
        }
8109
22.5k
      } else if (right_node.op_type == IS_CONST) {
8110
17.6k
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
8111
3.73k
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
8112
3.73k
          opline->extended_value =
8113
3.73k
            (opcode == ZEND_IS_IDENTICAL) ?
8114
481
              (1 << Z_TYPE(right_node.u.constant)) :
8115
3.24k
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
8116
3.73k
          return;
8117
3.73k
        }
8118
657k
      }
8119
657k
    } else if (opcode == ZEND_CONCAT) {
8120
      /* convert constant operands to strings at compile-time */
8121
125k
      if (left_node.op_type == IS_CONST) {
8122
23.5k
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
8123
1.30k
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
8124
22.2k
        } else {
8125
22.2k
          convert_to_string(&left_node.u.constant);
8126
22.2k
        }
8127
23.5k
      }
8128
125k
      if (right_node.op_type == IS_CONST) {
8129
80.3k
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
8130
249
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
8131
80.1k
        } else {
8132
80.1k
          convert_to_string(&right_node.u.constant);
8133
80.1k
        }
8134
80.3k
      }
8135
125k
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
8136
0
        opcode = ZEND_FAST_CONCAT;
8137
0
      }
8138
125k
    }
8139
733k
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
8140
733k
  } while (0);
8141
741k
}
8142
/* }}} */
8143
8144
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
8145
 * evaluation order. */
8146
void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
8147
32.0k
{
8148
32.0k
  zend_ast *left_ast = ast->child[0];
8149
32.0k
  zend_ast *right_ast = ast->child[1];
8150
32.0k
  znode left_node, right_node;
8151
8152
32.0k
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
8153
8154
32.0k
  zend_compile_expr(&left_node, left_ast);
8155
32.0k
  zend_compile_expr(&right_node, right_ast);
8156
8157
32.0k
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
8158
1.41k
    result->op_type = IS_CONST;
8159
1.41k
    zend_ct_eval_greater(&result->u.constant, ast->kind,
8160
1.41k
      &left_node.u.constant, &right_node.u.constant);
8161
1.41k
    zval_ptr_dtor(&left_node.u.constant);
8162
1.41k
    zval_ptr_dtor(&right_node.u.constant);
8163
1.41k
    return;
8164
1.41k
  }
8165
8166
30.6k
  zend_emit_op_tmp(result,
8167
22.1k
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
8168
30.6k
    &right_node, &left_node);
8169
30.6k
}
8170
/* }}} */
8171
8172
void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
8173
51.0k
{
8174
51.0k
  zend_ast *expr_ast = ast->child[0];
8175
51.0k
  uint32_t opcode = ast->attr;
8176
8177
51.0k
  znode expr_node;
8178
51.0k
  zend_compile_expr(&expr_node, expr_ast);
8179
8180
51.0k
  if (expr_node.op_type == IS_CONST) {
8181
8.40k
    result->op_type = IS_CONST;
8182
8.40k
    zend_ct_eval_unary_op(&result->u.constant, opcode,
8183
8.40k
      &expr_node.u.constant);
8184
8.40k
    zval_ptr_dtor(&expr_node.u.constant);
8185
8.40k
    return;
8186
8.40k
  }
8187
8188
42.6k
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
8189
42.6k
}
8190
/* }}} */
8191
8192
void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
8193
59.8k
{
8194
59.8k
  zend_ast *expr_ast = ast->child[0];
8195
59.8k
  znode expr_node;
8196
59.8k
  znode lefthand_node;
8197
8198
59.8k
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
8199
8200
59.8k
  zend_compile_expr(&expr_node, expr_ast);
8201
8202
59.8k
  if (expr_node.op_type == IS_CONST
8203
38.9k
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
8204
36.7k
    result->op_type = IS_CONST;
8205
36.7k
    zval_ptr_dtor(&expr_node.u.constant);
8206
36.7k
    return;
8207
36.7k
  }
8208
8209
23.0k
  lefthand_node.op_type = IS_CONST;
8210
23.0k
  ZVAL_LONG(&lefthand_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
8211
23.0k
  zend_emit_op_tmp(result, ZEND_MUL, &lefthand_node, &expr_node);
8212
23.0k
}
8213
/* }}} */
8214
8215
void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
8216
35.6k
{
8217
35.6k
  zend_ast *left_ast = ast->child[0];
8218
35.6k
  zend_ast *right_ast = ast->child[1];
8219
8220
35.6k
  znode left_node, right_node;
8221
35.6k
  zend_op *opline_jmpz, *opline_bool;
8222
35.6k
  uint32_t opnum_jmpz;
8223
8224
35.6k
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
8225
8226
35.6k
  zend_compile_expr(&left_node, left_ast);
8227
8228
35.6k
  if (left_node.op_type == IS_CONST) {
8229
12.5k
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
8230
11.3k
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
8231
11.3k
      result->op_type = IS_CONST;
8232
11.3k
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
8233
1.25k
    } else {
8234
1.25k
      zend_compile_expr(&right_node, right_ast);
8235
8236
1.25k
      if (right_node.op_type == IS_CONST) {
8237
703
        result->op_type = IS_CONST;
8238
703
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
8239
8240
703
        zval_ptr_dtor(&right_node.u.constant);
8241
549
      } else {
8242
549
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
8243
549
      }
8244
1.25k
    }
8245
8246
12.5k
    zval_ptr_dtor(&left_node.u.constant);
8247
12.5k
    return;
8248
12.5k
  }
8249
8250
23.0k
  opnum_jmpz = get_next_op_number();
8251
17.4k
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
8252
23.0k
    &left_node, NULL);
8253
8254
23.0k
  if (left_node.op_type == IS_TMP_VAR) {
8255
20.5k
    SET_NODE(opline_jmpz->result, &left_node);
8256
20.5k
    GET_NODE(result, opline_jmpz->result);
8257
2.53k
  } else {
8258
2.53k
    zend_make_tmp_result(result, opline_jmpz);
8259
2.53k
  }
8260
8261
23.0k
  zend_compile_expr(&right_node, right_ast);
8262
8263
23.0k
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
8264
23.0k
  SET_NODE(opline_bool->result, result);
8265
8266
23.0k
  zend_update_jump_target_to_next(opnum_jmpz);
8267
23.0k
}
8268
/* }}} */
8269
8270
void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
8271
70.8k
{
8272
70.8k
  zend_ast *var_ast = ast->child[0];
8273
70.8k
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
8274
8275
70.8k
  zend_ensure_writable_variable(var_ast);
8276
8277
70.8k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
8278
10.7k
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, 0);
8279
6.60k
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
8280
10.7k
    zend_make_tmp_result(result, opline);
8281
60.1k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
8282
643
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, 0, 0);
8283
460
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
8284
643
    zend_make_tmp_result(result, opline);
8285
59.5k
  } else {
8286
59.5k
    znode var_node;
8287
59.5k
    zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
8288
54.1k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
8289
59.5k
      &var_node, NULL);
8290
59.5k
  }
8291
70.8k
}
8292
/* }}} */
8293
8294
void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
8295
21.1k
{
8296
21.1k
  zend_ast *var_ast = ast->child[0];
8297
21.1k
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
8298
8299
21.1k
  zend_ensure_writable_variable(var_ast);
8300
8301
21.1k
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
8302
9.32k
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, 0);
8303
5.69k
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
8304
9.32k
    opline->result_type = IS_TMP_VAR;
8305
9.32k
    result->op_type = IS_TMP_VAR;
8306
11.8k
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
8307
120
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, 0, 0);
8308
120
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
8309
120
    opline->result_type = IS_TMP_VAR;
8310
120
    result->op_type = IS_TMP_VAR;
8311
11.7k
  } else {
8312
11.7k
    znode var_node;
8313
11.7k
    zend_compile_var(&var_node, var_ast, BP_VAR_RW, 0);
8314
7.16k
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
8315
11.7k
      &var_node, NULL);
8316
11.7k
  }
8317
21.1k
}
8318
/* }}} */
8319
8320
void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
8321
28.7k
{
8322
28.7k
  zend_ast *expr_ast = ast->child[0];
8323
28.7k
  znode expr_node;
8324
28.7k
  zend_op *opline;
8325
8326
28.7k
  zend_compile_expr(&expr_node, expr_ast);
8327
8328
28.7k
  if (ast->attr == _IS_BOOL) {
8329
41
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
8330
28.7k
  } else if (ast->attr == IS_NULL) {
8331
30
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
8332
28.7k
  } else {
8333
28.7k
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
8334
28.7k
    opline->extended_value = ast->attr;
8335
28.7k
  }
8336
28.7k
}
8337
/* }}} */
8338
8339
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
8340
4.36k
{
8341
4.36k
  zend_ast *cond_ast = ast->child[0];
8342
4.36k
  zend_ast *false_ast = ast->child[2];
8343
8344
4.36k
  znode cond_node, false_node;
8345
4.36k
  zend_op *opline_qm_assign;
8346
4.36k
  uint32_t opnum_jmp_set;
8347
8348
4.36k
  ZEND_ASSERT(ast->child[1] == NULL);
8349
8350
4.36k
  zend_compile_expr(&cond_node, cond_ast);
8351
8352
4.36k
  opnum_jmp_set = get_next_op_number();
8353
4.36k
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
8354
8355
4.36k
  zend_compile_expr(&false_node, false_ast);
8356
8357
4.36k
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
8358
4.36k
  SET_NODE(opline_qm_assign->result, result);
8359
8360
4.36k
  zend_update_jump_target_to_next(opnum_jmp_set);
8361
4.36k
}
8362
/* }}} */
8363
8364
void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
8365
18.5k
{
8366
18.5k
  zend_ast *cond_ast = ast->child[0];
8367
18.5k
  zend_ast *true_ast = ast->child[1];
8368
18.5k
  zend_ast *false_ast = ast->child[2];
8369
8370
18.5k
  znode cond_node, true_node, false_node;
8371
18.5k
  zend_op *opline_qm_assign2;
8372
18.5k
  uint32_t opnum_jmpz, opnum_jmp;
8373
8374
18.5k
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
8375
1.22k
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
8376
402
    if (cond_ast->child[1]) {
8377
39
      if (true_ast) {
8378
19
        zend_error(E_COMPILE_ERROR,
8379
19
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
8380
19
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
8381
20
      } else {
8382
20
        zend_error(E_COMPILE_ERROR,
8383
20
          "Unparenthesized `a ? b : c ?: d` is not supported. "
8384
20
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
8385
20
      }
8386
363
    } else {
8387
363
      if (true_ast) {
8388
31
        zend_error(E_COMPILE_ERROR,
8389
31
          "Unparenthesized `a ?: b ? c : d` is not supported. "
8390
31
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
8391
332
      } else {
8392
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
8393
         * as a ?: (b ?: c). */
8394
332
      }
8395
363
    }
8396
402
  }
8397
8398
18.5k
  if (!true_ast) {
8399
4.36k
    zend_compile_shorthand_conditional(result, ast);
8400
4.36k
    return;
8401
4.36k
  }
8402
8403
14.2k
  zend_compile_expr(&cond_node, cond_ast);
8404
8405
14.2k
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
8406
8407
14.2k
  zend_compile_expr(&true_node, true_ast);
8408
8409
14.2k
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
8410
8411
14.2k
  opnum_jmp = zend_emit_jump(0);
8412
8413
14.2k
  zend_update_jump_target_to_next(opnum_jmpz);
8414
8415
14.2k
  zend_compile_expr(&false_node, false_ast);
8416
8417
14.2k
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
8418
14.2k
  SET_NODE(opline_qm_assign2->result, result);
8419
8420
14.2k
  zend_update_jump_target_to_next(opnum_jmp);
8421
14.2k
}
8422
/* }}} */
8423
8424
void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
8425
3.52k
{
8426
3.52k
  zend_ast *expr_ast = ast->child[0];
8427
3.52k
  zend_ast *default_ast = ast->child[1];
8428
8429
3.52k
  znode expr_node, default_node;
8430
3.52k
  zend_op *opline;
8431
3.52k
  uint32_t opnum;
8432
8433
3.52k
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, 0);
8434
8435
3.52k
  opnum = get_next_op_number();
8436
3.52k
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
8437
8438
3.52k
  zend_compile_expr(&default_node, default_ast);
8439
8440
3.52k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
8441
3.52k
  SET_NODE(opline->result, result);
8442
8443
3.52k
  opline = &CG(active_op_array)->opcodes[opnum];
8444
3.52k
  opline->op2.opline_num = get_next_op_number();
8445
3.52k
}
8446
/* }}} */
8447
8448
23.6k
static void znode_dtor(zval *zv) {
8449
23.6k
  znode *node = Z_PTR_P(zv);
8450
23.6k
  if (node->op_type == IS_CONST) {
8451
5.07k
    zval_ptr_dtor_nogc(&node->u.constant);
8452
5.07k
  }
8453
23.6k
  efree(node);
8454
23.6k
}
8455
8456
void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
8457
28.8k
{
8458
28.8k
  zend_ast *var_ast = ast->child[0];
8459
28.8k
  zend_ast *default_ast = ast->child[1];
8460
8461
28.8k
  znode var_node_is, var_node_w, default_node, assign_node, *node;
8462
28.8k
  zend_op *opline;
8463
28.8k
  uint32_t coalesce_opnum;
8464
28.8k
  zend_bool need_frees = 0;
8465
8466
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
8467
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
8468
28.8k
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
8469
28.8k
  int orig_memoize_mode = CG(memoize_mode);
8470
8471
28.8k
  zend_ensure_writable_variable(var_ast);
8472
28.8k
  if (is_this_fetch(var_ast)) {
8473
19
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
8474
19
  }
8475
8476
28.8k
  ALLOC_HASHTABLE(CG(memoized_exprs));
8477
28.8k
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
8478
8479
28.8k
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
8480
28.8k
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, 0);
8481
8482
28.8k
  coalesce_opnum = get_next_op_number();
8483
28.8k
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
8484
8485
28.8k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
8486
28.8k
  zend_compile_expr(&default_node, default_ast);
8487
8488
28.8k
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
8489
28.8k
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, 0);
8490
8491
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
8492
28.8k
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
8493
28.8k
  switch (var_ast->kind) {
8494
5.82k
    case ZEND_AST_VAR:
8495
5.82k
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
8496
5.82k
      break;
8497
6.32k
    case ZEND_AST_STATIC_PROP:
8498
6.32k
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
8499
6.32k
      opline->result_type = IS_TMP_VAR;
8500
6.32k
      var_node_w.op_type = IS_TMP_VAR;
8501
6.32k
      zend_emit_op_data(&default_node);
8502
6.32k
      assign_node = var_node_w;
8503
6.32k
      break;
8504
9.50k
    case ZEND_AST_DIM:
8505
9.50k
      opline->opcode = ZEND_ASSIGN_DIM;
8506
9.50k
      opline->result_type = IS_TMP_VAR;
8507
9.50k
      var_node_w.op_type = IS_TMP_VAR;
8508
9.50k
      zend_emit_op_data(&default_node);
8509
9.50k
      assign_node = var_node_w;
8510
9.50k
      break;
8511
6.89k
    case ZEND_AST_PROP:
8512
6.89k
    case ZEND_AST_NULLSAFE_PROP:
8513
6.89k
      opline->opcode = ZEND_ASSIGN_OBJ;
8514
6.89k
      opline->result_type = IS_TMP_VAR;
8515
6.89k
      var_node_w.op_type = IS_TMP_VAR;
8516
6.89k
      zend_emit_op_data(&default_node);
8517
6.89k
      assign_node = var_node_w;
8518
6.89k
      break;
8519
0
    EMPTY_SWITCH_DEFAULT_CASE();
8520
28.8k
  }
8521
8522
28.5k
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
8523
28.5k
  SET_NODE(opline->result, result);
8524
8525
75.1k
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
8526
23.2k
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
8527
11.0k
      need_frees = 1;
8528
11.0k
      break;
8529
11.0k
    }
8530
23.2k
  } ZEND_HASH_FOREACH_END();
8531
8532
  /* Free DUPed expressions if there are any */
8533
28.5k
  if (need_frees) {
8534
11.0k
    uint32_t jump_opnum = zend_emit_jump(0);
8535
11.0k
    zend_update_jump_target_to_next(coalesce_opnum);
8536
33.9k
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
8537
11.4k
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
8538
11.4k
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
8539
11.4k
      }
8540
11.4k
    } ZEND_HASH_FOREACH_END();
8541
11.0k
    zend_update_jump_target_to_next(jump_opnum);
8542
17.5k
  } else {
8543
17.5k
    zend_update_jump_target_to_next(coalesce_opnum);
8544
17.5k
  }
8545
8546
28.5k
  zend_hash_destroy(CG(memoized_exprs));
8547
28.5k
  FREE_HASHTABLE(CG(memoized_exprs));
8548
28.5k
  CG(memoized_exprs) = orig_memoized_exprs;
8549
28.5k
  CG(memoize_mode) = orig_memoize_mode;
8550
28.5k
}
8551
/* }}} */
8552
8553
void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
8554
41.9k
{
8555
41.9k
  zend_op *opline;
8556
41.9k
  zend_ast *expr_ast = ast->child[0];
8557
8558
41.9k
  znode expr_node;
8559
41.9k
  zend_compile_expr(&expr_node, expr_ast);
8560
8561
41.9k
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
8562
41.9k
  opline->extended_value = 1;
8563
8564
41.9k
  result->op_type = IS_CONST;
8565
41.9k
  ZVAL_LONG(&result->u.constant, 1);
8566
41.9k
}
8567
/* }}} */
8568
8569
void zend_compile_exit(znode *result, zend_ast *ast) /* {{{ */
8570
3.47k
{
8571
3.47k
  zend_ast *expr_ast = ast->child[0];
8572
8573
3.47k
  if (expr_ast) {
8574
1.86k
    znode expr_node;
8575
1.86k
    zend_compile_expr(&expr_node, expr_ast);
8576
1.86k
    zend_emit_op(NULL, ZEND_EXIT, &expr_node, NULL);
8577
1.61k
  } else {
8578
1.61k
    zend_emit_op(NULL, ZEND_EXIT, NULL, NULL);
8579
1.61k
  }
8580
8581
3.47k
  result->op_type = IS_CONST;
8582
3.47k
  ZVAL_BOOL(&result->u.constant, 1);
8583
3.47k
}
8584
/* }}} */
8585
8586
void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
8587
31.8k
{
8588
31.8k
  zend_ast *value_ast = ast->child[0];
8589
31.8k
  zend_ast *key_ast = ast->child[1];
8590
8591
31.8k
  znode value_node, key_node;
8592
31.8k
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
8593
31.8k
  zend_op *opline;
8594
31.8k
  zend_bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
8595
8596
31.8k
  zend_mark_function_as_generator();
8597
8598
31.8k
  if (key_ast) {
8599
9.61k
    zend_compile_expr(&key_node, key_ast);
8600
9.61k
    key_node_ptr = &key_node;
8601
9.61k
  }
8602
8603
31.8k
  if (value_ast) {
8604
26.4k
    if (returns_by_ref && zend_is_variable(value_ast)) {
8605
1.10k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
8606
25.3k
    } else {
8607
25.3k
      zend_compile_expr(&value_node, value_ast);
8608
25.3k
    }
8609
26.4k
    value_node_ptr = &value_node;
8610
26.4k
  }
8611
8612
31.8k
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
8613
8614
31.8k
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
8615
39
    opline->extended_value = ZEND_RETURNS_FUNCTION;
8616
39
  }
8617
31.8k
}
8618
/* }}} */
8619
8620
void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
8621
7.23k
{
8622
7.23k
  zend_ast *expr_ast = ast->child[0];
8623
7.23k
  znode expr_node;
8624
8625
7.23k
  zend_mark_function_as_generator();
8626
8627
7.23k
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
8628
20
    zend_error_noreturn(E_COMPILE_ERROR,
8629
20
      "Cannot use \"yield from\" inside a by-reference generator");
8630
20
  }
8631
8632
7.21k
  zend_compile_expr(&expr_node, expr_ast);
8633
7.21k
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
8634
7.21k
}
8635
/* }}} */
8636
8637
void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
8638
2.81k
{
8639
2.81k
  zend_ast *obj_ast = ast->child[0];
8640
2.81k
  zend_ast *class_ast = ast->child[1];
8641
8642
2.81k
  znode obj_node, class_node;
8643
2.81k
  zend_op *opline;
8644
8645
2.81k
  zend_compile_expr(&obj_node, obj_ast);
8646
2.81k
  if (obj_node.op_type == IS_CONST) {
8647
41
    zend_do_free(&obj_node);
8648
41
    result->op_type = IS_CONST;
8649
41
    ZVAL_FALSE(&result->u.constant);
8650
41
    return;
8651
41
  }
8652
8653
2.77k
  zend_compile_class_ref(&class_node, class_ast,
8654
2.77k
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION);
8655
8656
2.77k
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
8657
8658
2.77k
  if (class_node.op_type == IS_CONST) {
8659
2.57k
    opline->op2_type = IS_CONST;
8660
2.57k
    opline->op2.constant = zend_add_class_name_literal(
8661
2.57k
      Z_STR(class_node.u.constant));
8662
2.57k
    opline->extended_value = zend_alloc_cache_slot();
8663
196
  } else {
8664
196
    SET_NODE(opline->op2, &class_node);
8665
196
  }
8666
2.77k
}
8667
/* }}} */
8668
8669
void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
8670
26.5k
{
8671
26.5k
  zend_ast *expr_ast = ast->child[0];
8672
26.5k
  znode expr_node;
8673
26.5k
  zend_op *opline;
8674
8675
26.5k
  zend_do_extended_fcall_begin();
8676
26.5k
  zend_compile_expr(&expr_node, expr_ast);
8677
8678
26.5k
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
8679
26.5k
  opline->extended_value = ast->attr;
8680
8681
26.5k
  zend_do_extended_fcall_end();
8682
26.5k
}
8683
/* }}} */
8684
8685
void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
8686
123k
{
8687
123k
  zend_ast *var_ast = ast->child[0];
8688
8689
123k
  znode var_node;
8690
123k
  zend_op *opline = NULL;
8691
8692
123k
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
8693
8694
123k
  if (!zend_is_variable(var_ast)) {
8695
984
    if (ast->kind == ZEND_AST_EMPTY) {
8696
      /* empty(expr) can be transformed to !expr */
8697
790
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
8698
790
      zend_compile_expr(result, not_ast);
8699
790
      return;
8700
194
    } else {
8701
194
      zend_error_noreturn(E_COMPILE_ERROR,
8702
194
        "Cannot use isset() on the result of an expression "
8703
194
        "(you can use \"null !== expression\" instead)");
8704
194
    }
8705
122k
  }
8706
8707
122k
  zend_short_circuiting_mark_inner(var_ast);
8708
122k
  switch (var_ast->kind) {
8709
31.5k
    case ZEND_AST_VAR:
8710
31.5k
      if (is_this_fetch(var_ast)) {
8711
724
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
8712
724
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
8713
30.8k
      } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
8714
29.1k
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
8715
1.66k
      } else {
8716
1.66k
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
8717
1.66k
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
8718
1.66k
      }
8719
31.5k
      break;
8720
68.1k
    case ZEND_AST_DIM:
8721
68.1k
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS);
8722
68.1k
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
8723
68.1k
      break;
8724
20.5k
    case ZEND_AST_PROP:
8725
21.5k
    case ZEND_AST_NULLSAFE_PROP:
8726
21.5k
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, 0);
8727
21.5k
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
8728
21.5k
      break;
8729
1.08k
    case ZEND_AST_STATIC_PROP:
8730
1.08k
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, 0, 0);
8731
1.08k
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
8732
1.08k
      break;
8733
0
    EMPTY_SWITCH_DEFAULT_CASE()
8734
122k
  }
8735
8736
122k
  result->op_type = opline->result_type = IS_TMP_VAR;
8737
122k
  if (!(ast->kind == ZEND_AST_ISSET)) {
8738
44.3k
    opline->extended_value |= ZEND_ISEMPTY;
8739
44.3k
  }
8740
122k
}
8741
/* }}} */
8742
8743
void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
8744
36.4k
{
8745
36.4k
  zend_ast *expr_ast = ast->child[0];
8746
36.4k
  znode silence_node;
8747
8748
36.4k
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
8749
8750
36.4k
  if (expr_ast->kind == ZEND_AST_VAR) {
8751
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
8752
     * happen outside the silenced section. */
8753
2.92k
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
8754
33.5k
  } else {
8755
33.5k
    zend_compile_expr(result, expr_ast);
8756
33.5k
  }
8757
8758
36.4k
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
8759
36.4k
}
8760
/* }}} */
8761
8762
void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
8763
247
{
8764
247
  zend_ast *expr_ast = ast->child[0];
8765
8766
247
  zval fn_name;
8767
247
  zend_ast *name_ast, *args_ast, *call_ast;
8768
8769
247
  ZVAL_STRING(&fn_name, "shell_exec");
8770
247
  name_ast = zend_ast_create_zval(&fn_name);
8771
247
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
8772
247
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
8773
8774
247
  zend_compile_expr(result, call_ast);
8775
8776
247
  zval_ptr_dtor(&fn_name);
8777
247
}
8778
/* }}} */
8779
8780
void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
8781
213k
{
8782
213k
  zend_ast_list *list = zend_ast_get_list(ast);
8783
213k
  zend_op *opline;
8784
213k
  uint32_t i, opnum_init = -1;
8785
213k
  zend_bool packed = 1;
8786
8787
213k
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
8788
146k
    result->op_type = IS_CONST;
8789
146k
    return;
8790
146k
  }
8791
8792
  /* Empty arrays are handled at compile-time */
8793
66.2k
  ZEND_ASSERT(list->children > 0);
8794
8795
281k
  for (i = 0; i < list->children; ++i) {
8796
215k
    zend_ast *elem_ast = list->child[i];
8797
215k
    zend_ast *value_ast, *key_ast;
8798
215k
    zend_bool by_ref;
8799
215k
    znode value_node, key_node, *key_node_ptr = NULL;
8800
8801
215k
    if (elem_ast == NULL) {
8802
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
8803
0
    }
8804
8805
215k
    value_ast = elem_ast->child[0];
8806
8807
215k
    if (elem_ast->kind == ZEND_AST_UNPACK) {
8808
2.60k
      zend_compile_expr(&value_node, value_ast);
8809
2.60k
      if (i == 0) {
8810
1.29k
        opnum_init = get_next_op_number();
8811
1.29k
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
8812
1.29k
      }
8813
2.60k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
8814
2.60k
      SET_NODE(opline->result, result);
8815
2.60k
      continue;
8816
2.60k
    }
8817
8818
212k
    key_ast = elem_ast->child[1];
8819
212k
    by_ref = elem_ast->attr;
8820
8821
212k
    if (key_ast) {
8822
16.3k
      zend_compile_expr(&key_node, key_ast);
8823
16.3k
      zend_handle_numeric_op(&key_node);
8824
16.3k
      key_node_ptr = &key_node;
8825
16.3k
    }
8826
8827
212k
    if (by_ref) {
8828
3.39k
      zend_ensure_writable_variable(value_ast);
8829
3.39k
      zend_compile_var(&value_node, value_ast, BP_VAR_W, 1);
8830
209k
    } else {
8831
209k
      zend_compile_expr(&value_node, value_ast);
8832
209k
    }
8833
8834
212k
    if (i == 0) {
8835
64.7k
      opnum_init = get_next_op_number();
8836
64.7k
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
8837
64.7k
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
8838
147k
    } else {
8839
147k
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
8840
147k
        &value_node, key_node_ptr);
8841
147k
      SET_NODE(opline->result, result);
8842
147k
    }
8843
212k
    opline->extended_value |= by_ref;
8844
8845
212k
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
8846
12.5k
      packed = 0;
8847
12.5k
    }
8848
212k
  }
8849
8850
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
8851
66.2k
  if (!packed) {
8852
6.01k
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
8853
6.01k
    opline = &CG(active_op_array)->opcodes[opnum_init];
8854
6.01k
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
8855
6.01k
  }
8856
66.2k
}
8857
/* }}} */
8858
8859
void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
8860
423k
{
8861
423k
  zend_ast *name_ast = ast->child[0];
8862
8863
423k
  zend_op *opline;
8864
8865
423k
  zend_bool is_fully_qualified;
8866
423k
  zend_string *orig_name = zend_ast_get_str(name_ast);
8867
423k
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
8868
8869
423k
  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__"))) {
8870
1.32k
    zend_ast *last = CG(ast);
8871
8872
2.65k
    while (last && last->kind == ZEND_AST_STMT_LIST) {
8873
1.33k
      zend_ast_list *list = zend_ast_get_list(last);
8874
1.33k
      if (list->children == 0) {
8875
1
        break;
8876
1
      }
8877
1.33k
      last = list->child[list->children-1];
8878
1.33k
    }
8879
1.32k
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
8880
834
      result->op_type = IS_CONST;
8881
834
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
8882
834
      zend_string_release_ex(resolved_name, 0);
8883
834
      return;
8884
834
    }
8885
422k
  }
8886
8887
422k
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
8888
276k
    result->op_type = IS_CONST;
8889
276k
    zend_string_release_ex(resolved_name, 0);
8890
276k
    return;
8891
276k
  }
8892
8893
145k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
8894
145k
  opline->op2_type = IS_CONST;
8895
8896
145k
  if (is_fully_qualified || !FC(current_namespace)) {
8897
144k
    opline->op2.constant = zend_add_const_name_literal(
8898
144k
      resolved_name, 0);
8899
1.65k
  } else {
8900
1.65k
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
8901
1.65k
    opline->op2.constant = zend_add_const_name_literal(
8902
1.65k
      resolved_name, 1);
8903
1.65k
  }
8904
145k
  opline->extended_value = zend_alloc_cache_slot();
8905
145k
}
8906
/* }}} */
8907
8908
void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
8909
45.1k
{
8910
45.1k
  zend_ast *class_ast = ast->child[0];
8911
45.1k
  zend_ast *const_ast = ast->child[1];
8912
8913
45.1k
  znode class_node, const_node;
8914
45.1k
  zend_op *opline;
8915
8916
45.1k
  zend_eval_const_expr(&ast->child[0]);
8917
45.1k
  zend_eval_const_expr(&ast->child[1]);
8918
8919
45.1k
  class_ast = ast->child[0];
8920
45.1k
  const_ast = ast->child[1];
8921
8922
45.1k
  if (class_ast->kind == ZEND_AST_ZVAL) {
8923
44.7k
    zend_string *resolved_name;
8924
8925
44.7k
    resolved_name = zend_resolve_class_name_ast(class_ast);
8926
44.7k
    if (const_ast->kind == ZEND_AST_ZVAL && zend_try_ct_eval_class_const(&result->u.constant, resolved_name, zend_ast_get_str(const_ast))) {
8927
30.4k
      result->op_type = IS_CONST;
8928
30.4k
      zend_string_release_ex(resolved_name, 0);
8929
30.4k
      return;
8930
30.4k
    }
8931
14.3k
    zend_string_release_ex(resolved_name, 0);
8932
14.3k
  }
8933
8934
14.7k
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
8935
8936
14.7k
  zend_compile_expr(&const_node, const_ast);
8937
8938
14.7k
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
8939
8940
14.7k
  zend_set_class_name_op1(opline, &class_node);
8941
8942
14.7k
  opline->extended_value = zend_alloc_cache_slots(2);
8943
14.7k
}
8944
/* }}} */
8945
8946
void zend_compile_class_name(znode *result, zend_ast *ast) /* {{{ */
8947
21.5k
{
8948
21.5k
  zend_ast *class_ast = ast->child[0];
8949
8950
21.5k
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
8951
14.9k
    result->op_type = IS_CONST;
8952
14.9k
    return;
8953
14.9k
  }
8954
8955
6.56k
  if (class_ast->kind == ZEND_AST_ZVAL) {
8956
5.90k
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
8957
5.90k
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
8958
659
  } else {
8959
659
    znode expr_node;
8960
659
    zend_compile_expr(&expr_node, class_ast);
8961
659
    if (expr_node.op_type == IS_CONST) {
8962
      /* Unlikely case that happen if class_ast is constant folded.
8963
       * Handle it here, to avoid needing a CONST specialization in the VM. */
8964
19
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on value of type %s",
8965
19
        zend_zval_type_name(&expr_node.u.constant));
8966
19
    }
8967
8968
640
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
8969
640
  }
8970
6.56k
}
8971
/* }}} */
8972
8973
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
8974
224k
{
8975
224k
  if (num == 0) {
8976
66.1k
    result->op_type = IS_TMP_VAR;
8977
66.1k
    result->u.op.var = -1;
8978
66.1k
    opline->opcode = ZEND_ROPE_INIT;
8979
158k
  } else {
8980
158k
    opline->opcode = ZEND_ROPE_ADD;
8981
158k
    SET_NODE(opline->op1, result);
8982
158k
  }
8983
224k
  SET_NODE(opline->op2, elem_node);
8984
224k
  SET_NODE(opline->result, result);
8985
224k
  opline->extended_value = num;
8986
224k
  return opline;
8987
224k
}
8988
/* }}} */
8989
8990
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
8991
175k
{
8992
175k
  zend_op *opline = get_next_op();
8993
8994
175k
  if (num == 0) {
8995
39.6k
    result->op_type = IS_TMP_VAR;
8996
39.6k
    result->u.op.var = -1;
8997
39.6k
    opline->opcode = ZEND_ROPE_INIT;
8998
135k
  } else {
8999
135k
    opline->opcode = ZEND_ROPE_ADD;
9000
135k
    SET_NODE(opline->op1, result);
9001
135k
  }
9002
175k
  SET_NODE(opline->op2, elem_node);
9003
175k
  SET_NODE(opline->result, result);
9004
175k
  opline->extended_value = num;
9005
175k
  return opline;
9006
175k
}
9007
/* }}} */
9008
9009
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
9010
105k
{
9011
105k
  uint32_t i, j;
9012
105k
  uint32_t rope_init_lineno = -1;
9013
105k
  zend_op *opline = NULL, *init_opline;
9014
105k
  znode elem_node, last_const_node;
9015
105k
  zend_ast_list *list = zend_ast_get_list(ast);
9016
105k
  uint32_t reserved_op_number = -1;
9017
9018
105k
  ZEND_ASSERT(list->children > 0);
9019
9020
105k
  j = 0;
9021
105k
  last_const_node.op_type = IS_UNUSED;
9022
506k
  for (i = 0; i < list->children; i++) {
9023
400k
    zend_compile_expr(&elem_node, list->child[i]);
9024
9025
400k
    if (elem_node.op_type == IS_CONST) {
9026
225k
      convert_to_string(&elem_node.u.constant);
9027
9028
225k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
9029
929
        zval_ptr_dtor(&elem_node.u.constant);
9030
224k
      } else if (last_const_node.op_type == IS_CONST) {
9031
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
9032
0
        zval_ptr_dtor(&elem_node.u.constant);
9033
224k
      } else {
9034
224k
        last_const_node.op_type = IS_CONST;
9035
224k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
9036
        /* Reserve place for ZEND_ROPE_ADD instruction */
9037
224k
        reserved_op_number = get_next_op_number();
9038
224k
        opline = get_next_op();
9039
224k
        opline->opcode = ZEND_NOP;
9040
224k
      }
9041
225k
      continue;
9042
175k
    } else {
9043
175k
      if (j == 0) {
9044
105k
        if (last_const_node.op_type == IS_CONST) {
9045
66.1k
          rope_init_lineno = reserved_op_number;
9046
39.6k
        } else {
9047
39.6k
          rope_init_lineno = get_next_op_number();
9048
39.6k
        }
9049
105k
      }
9050
175k
      if (last_const_node.op_type == IS_CONST) {
9051
132k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
9052
132k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
9053
132k
        last_const_node.op_type = IS_UNUSED;
9054
132k
      }
9055
175k
      opline = zend_compile_rope_add(result, j++, &elem_node);
9056
175k
    }
9057
400k
  }
9058
9059
105k
  if (j == 0) {
9060
0
    result->op_type = IS_CONST;
9061
0
    if (last_const_node.op_type == IS_CONST) {
9062
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
9063
0
    } else {
9064
0
      ZVAL_EMPTY_STRING(&result->u.constant);
9065
      /* empty string */
9066
0
    }
9067
0
    CG(active_op_array)->last = reserved_op_number - 1;
9068
0
    return;
9069
105k
  } else if (last_const_node.op_type == IS_CONST) {
9070
91.9k
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
9071
91.9k
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
9072
91.9k
  }
9073
105k
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
9074
105k
  if (j == 1) {
9075
6.11k
    if (opline->op2_type == IS_CONST) {
9076
0
      GET_NODE(result, opline->op2);
9077
0
      MAKE_NOP(opline);
9078
6.11k
    } else {
9079
6.11k
      opline->opcode = ZEND_CAST;
9080
6.11k
      opline->extended_value = IS_STRING;
9081
6.11k
      opline->op1_type = opline->op2_type;
9082
6.11k
      opline->op1 = opline->op2;
9083
6.11k
      SET_UNUSED(opline->op2);
9084
6.11k
      zend_make_tmp_result(result, opline);
9085
6.11k
    }
9086
99.6k
  } else if (j == 2) {
9087
27.1k
    opline->opcode = ZEND_FAST_CONCAT;
9088
27.1k
    opline->extended_value = 0;
9089
27.1k
    opline->op1_type = init_opline->op2_type;
9090
27.1k
    opline->op1 = init_opline->op2;
9091
27.1k
    zend_make_tmp_result(result, opline);
9092
27.1k
    MAKE_NOP(init_opline);
9093
72.5k
  } else {
9094
72.5k
    uint32_t var;
9095
9096
72.5k
    init_opline->extended_value = j;
9097
72.5k
    opline->opcode = ZEND_ROPE_END;
9098
72.5k
    zend_make_tmp_result(result, opline);
9099
72.5k
    var = opline->op1.var = get_temporary_variable();
9100
9101
    /* Allocates the necessary number of zval slots to keep the rope */
9102
72.5k
    i = ((j * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
9103
202k
    while (i > 1) {
9104
129k
      get_temporary_variable();
9105
129k
      i--;
9106
129k
    }
9107
9108
    /* Update all the previous opcodes to use the same variable */
9109
368k
    while (opline != init_opline) {
9110
295k
      opline--;
9111
295k
      if (opline->opcode == ZEND_ROPE_ADD &&
9112
194k
          opline->result.var == (uint32_t)-1) {
9113
194k
        opline->op1.var = var;
9114
194k
        opline->result.var = var;
9115
101k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
9116
72.6k
                 opline->result.var == (uint32_t)-1) {
9117
72.5k
        opline->result.var = var;
9118
72.5k
      }
9119
295k
    }
9120
72.5k
  }
9121
105k
}
9122
/* }}} */
9123
9124
void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
9125
33.8k
{
9126
33.8k
  zend_op *opline;
9127
9128
33.8k
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
9129
33.4k
    result->op_type = IS_CONST;
9130
33.4k
    return;
9131
33.4k
  }
9132
9133
345
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
9134
345
              CG(active_class_entry) &&
9135
345
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
9136
9137
345
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
9138
345
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
9139
345
}
9140
/* }}} */
9141
9142
zend_bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
9143
82.6k
{
9144
82.6k
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
9145
78.3k
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
9146
76.6k
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
9147
73.3k
    || kind == ZEND_AST_UNARY_OP
9148
72.5k
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
9149
70.1k
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
9150
53.6k
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
9151
40.4k
    || kind == ZEND_AST_UNPACK
9152
39.9k
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
9153
4.88k
    || kind == ZEND_AST_CLASS_NAME
9154
4.40k
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE;
9155
82.6k
}
9156
/* }}} */
9157
9158
void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
9159
7.68k
{
9160
7.68k
  zend_ast *ast = *ast_ptr;
9161
7.68k
  zend_ast *class_ast = ast->child[0];
9162
7.68k
  zend_ast *const_ast = ast->child[1];
9163
7.68k
  zend_string *class_name;
9164
7.68k
  zend_string *const_name = zend_ast_get_str(const_ast);
9165
7.68k
  zend_string *name;
9166
7.68k
  int fetch_type;
9167
9168
7.68k
  if (class_ast->kind != ZEND_AST_ZVAL) {
9169
63
    zend_error_noreturn(E_COMPILE_ERROR,
9170
63
      "Dynamic class names are not allowed in compile-time class constant references");
9171
63
  }
9172
9173
7.62k
  class_name = zend_ast_get_str(class_ast);
9174
7.62k
  fetch_type = zend_get_class_fetch_type(class_name);
9175
9176
7.62k
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
9177
19
    zend_error_noreturn(E_COMPILE_ERROR,
9178
19
      "\"static::\" is not allowed in compile-time constants");
9179
19
  }
9180
9181
7.60k
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
9182
3.07k
    class_name = zend_resolve_class_name_ast(class_ast);
9183
4.52k
  } else {
9184
4.52k
    zend_string_addref(class_name);
9185
4.52k
  }
9186
9187
7.60k
  name = zend_create_member_string(class_name, const_name);
9188
9189
7.60k
  zend_ast_destroy(ast);
9190
7.60k
  zend_string_release_ex(class_name, 0);
9191
9192
7.60k
  *ast_ptr = zend_ast_create_constant(name, fetch_type | ZEND_FETCH_CLASS_EXCEPTION);
9193
7.60k
}
9194
/* }}} */
9195
9196
void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
9197
479
{
9198
479
  zend_ast *ast = *ast_ptr;
9199
479
  zend_ast *class_ast = ast->child[0];
9200
479
  if (class_ast->kind != ZEND_AST_ZVAL) {
9201
24
    zend_error_noreturn(E_COMPILE_ERROR,
9202
24
      "(expression)::class cannot be used in constant expressions");
9203
24
  }
9204
9205
455
  zend_string *class_name = zend_ast_get_str(class_ast);
9206
455
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
9207
9208
455
  switch (fetch_type) {
9209
415
    case ZEND_FETCH_CLASS_SELF:
9210
417
    case ZEND_FETCH_CLASS_PARENT:
9211
      /* For the const-eval representation store the fetch type instead of the name. */
9212
417
      zend_string_release(class_name);
9213
417
      ast->child[0] = NULL;
9214
417
      ast->attr = fetch_type;
9215
417
      return;
9216
38
    case ZEND_FETCH_CLASS_STATIC:
9217
38
      zend_error_noreturn(E_COMPILE_ERROR,
9218
38
        "static::class cannot be used for compile-time class name resolution");
9219
0
      return;
9220
0
    EMPTY_SWITCH_DEFAULT_CASE()
9221
455
  }
9222
455
}
9223
9224
void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
9225
27.3k
{
9226
27.3k
  zend_ast *ast = *ast_ptr;
9227
27.3k
  zend_ast *name_ast = ast->child[0];
9228
27.3k
  zend_string *orig_name = zend_ast_get_str(name_ast);
9229
27.3k
  zend_bool is_fully_qualified;
9230
27.3k
  zval result;
9231
27.3k
  zend_string *resolved_name;
9232
9233
27.3k
  resolved_name = zend_resolve_const_name(
9234
27.3k
    orig_name, name_ast->attr, &is_fully_qualified);
9235
9236
27.3k
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
9237
0
    zend_string_release_ex(resolved_name, 0);
9238
0
    zend_ast_destroy(ast);
9239
0
    *ast_ptr = zend_ast_create_zval(&result);
9240
0
    return;
9241
0
  }
9242
9243
27.3k
  zend_ast_destroy(ast);
9244
27.3k
  *ast_ptr = zend_ast_create_constant(resolved_name,
9245
27.3k
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
9246
27.3k
}
9247
/* }}} */
9248
9249
void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
9250
238
{
9251
238
  zend_ast *ast = *ast_ptr;
9252
9253
  /* Other cases already resolved by constant folding */
9254
238
  ZEND_ASSERT(ast->attr == T_CLASS_C);
9255
9256
238
  zend_ast_destroy(ast);
9257
238
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
9258
238
}
9259
/* }}} */
9260
9261
void zend_compile_const_expr(zend_ast **ast_ptr) /* {{{ */
9262
294k
{
9263
294k
  zend_ast *ast = *ast_ptr;
9264
294k
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
9265
211k
    return;
9266
211k
  }
9267
9268
82.6k
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
9269
300
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
9270
300
  }
9271
9272
82.3k
  switch (ast->kind) {
9273
7.68k
    case ZEND_AST_CLASS_CONST:
9274
7.68k
      zend_compile_const_expr_class_const(ast_ptr);
9275
7.68k
      break;
9276
479
    case ZEND_AST_CLASS_NAME:
9277
479
      zend_compile_const_expr_class_name(ast_ptr);
9278
479
      break;
9279
27.3k
    case ZEND_AST_CONST:
9280
27.3k
      zend_compile_const_expr_const(ast_ptr);
9281
27.3k
      break;
9282
238
    case ZEND_AST_MAGIC_CONST:
9283
238
      zend_compile_const_expr_magic_const(ast_ptr);
9284
238
      break;
9285
46.6k
    default:
9286
46.6k
      zend_ast_apply(ast, zend_compile_const_expr);
9287
46.6k
      break;
9288
82.3k
  }
9289
82.3k
}
9290
/* }}} */
9291
9292
void zend_const_expr_to_zval(zval *result, zend_ast *ast) /* {{{ */
9293
200k
{
9294
200k
  zend_ast *orig_ast = ast;
9295
200k
  zend_eval_const_expr(&ast);
9296
200k
  zend_compile_const_expr(&ast);
9297
200k
  if (ast->kind == ZEND_AST_ZVAL) {
9298
166k
    ZVAL_COPY_VALUE(result, zend_ast_get_zval(ast));
9299
33.7k
  } else {
9300
33.7k
    ZVAL_AST(result, zend_ast_copy(ast));
9301
    /* destroy the ast here, it might have been replaced */
9302
33.7k
    zend_ast_destroy(ast);
9303
33.7k
  }
9304
9305
  /* Kill this branch of the original AST, as it was already destroyed.
9306
   * It would be nice to find a better solution to this problem in the
9307
   * future. */
9308
200k
  orig_ast->kind = 0;
9309
200k
}
9310
/* }}} */
9311
9312
/* Same as compile_stmt, but with early binding */
9313
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
9314
4.52M
{
9315
4.52M
  if (!ast) {
9316
489k
    return;
9317
489k
  }
9318
9319
4.03M
  if (ast->kind == ZEND_AST_STMT_LIST) {
9320
865k
    zend_ast_list *list = zend_ast_get_list(ast);
9321
865k
    uint32_t i;
9322
4.79M
    for (i = 0; i < list->children; ++i) {
9323
3.93M
      zend_compile_top_stmt(list->child[i]);
9324
3.93M
    }
9325
865k
    return;
9326
865k
  }
9327
9328
3.17M
  if (ast->kind == ZEND_AST_FUNC_DECL) {
9329
107k
    CG(zend_lineno) = ast->lineno;
9330
107k
    zend_compile_func_decl(NULL, ast, 1);
9331
107k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
9332
3.06M
  } else if (ast->kind == ZEND_AST_CLASS) {
9333
157k
    CG(zend_lineno) = ast->lineno;
9334
157k
    zend_compile_class_decl(NULL, ast, 1);
9335
157k
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
9336
2.90M
  } else {
9337
2.90M
    zend_compile_stmt(ast);
9338
2.90M
  }
9339
3.17M
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
9340
3.15M
    zend_verify_namespace();
9341
3.15M
  }
9342
3.17M
}
9343
/* }}} */
9344
9345
void zend_compile_stmt(zend_ast *ast) /* {{{ */
9346
6.88M
{
9347
6.88M
  if (!ast) {
9348
35.8k
    return;
9349
35.8k
  }
9350
9351
6.85M
  CG(zend_lineno) = ast->lineno;
9352
9353
6.85M
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
9354
0
    zend_do_extended_stmt();
9355
0
  }
9356
9357
6.85M
  switch (ast->kind) {
9358
1.76M
    case ZEND_AST_STMT_LIST:
9359
1.76M
      zend_compile_stmt_list(ast);
9360
1.76M
      break;
9361
6.36k
    case ZEND_AST_GLOBAL:
9362
6.36k
      zend_compile_global_var(ast);
9363
6.36k
      break;
9364
5.06k
    case ZEND_AST_STATIC:
9365
5.06k
      zend_compile_static_var(ast);
9366
5.06k
      break;
9367
61.9k
    case ZEND_AST_UNSET:
9368
61.9k
      zend_compile_unset(ast);
9369
61.9k
      break;
9370
166k
    case ZEND_AST_RETURN:
9371
166k
      zend_compile_return(ast);
9372
166k
      break;
9373
713k
    case ZEND_AST_ECHO:
9374
713k
      zend_compile_echo(ast);
9375
713k
      break;
9376
7.28k
    case ZEND_AST_BREAK:
9377
13.0k
    case ZEND_AST_CONTINUE:
9378
13.0k
      zend_compile_break_continue(ast);
9379
13.0k
      break;
9380
1.78k
    case ZEND_AST_GOTO:
9381
1.78k
      zend_compile_goto(ast);
9382
1.78k
      break;
9383
11.1k
    case ZEND_AST_LABEL:
9384
11.1k
      zend_compile_label(ast);
9385
11.1k
      break;
9386
3.59k
    case ZEND_AST_WHILE:
9387
3.59k
      zend_compile_while(ast);
9388
3.59k
      break;
9389
996
    case ZEND_AST_DO_WHILE:
9390
996
      zend_compile_do_while(ast);
9391
996
      break;
9392
39.6k
    case ZEND_AST_FOR:
9393
39.6k
      zend_compile_for(ast);
9394
39.6k
      break;
9395
68.5k
    case ZEND_AST_FOREACH:
9396
68.5k
      zend_compile_foreach(ast);
9397
68.5k
      break;
9398
76.5k
    case ZEND_AST_IF:
9399
76.5k
      zend_compile_if(ast);
9400
76.5k
      break;
9401
2.31k
    case ZEND_AST_SWITCH:
9402
2.31k
      zend_compile_switch(ast);
9403
2.31k
      break;
9404
387k
    case ZEND_AST_TRY:
9405
387k
      zend_compile_try(ast);
9406
387k
      break;
9407
1.82k
    case ZEND_AST_DECLARE:
9408
1.82k
      zend_compile_declare(ast);
9409
1.82k
      break;
9410
279
    case ZEND_AST_FUNC_DECL:
9411
221k
    case ZEND_AST_METHOD:
9412
221k
      zend_compile_func_decl(NULL, ast, 0);
9413
221k
      break;
9414
105k
    case ZEND_AST_PROP_GROUP:
9415
105k
      zend_compile_prop_group(ast);
9416
105k
      break;
9417
49.8k
    case ZEND_AST_CLASS_CONST_GROUP:
9418
49.8k
      zend_compile_class_const_group(ast);
9419
49.8k
      break;
9420
8.48k
    case ZEND_AST_USE_TRAIT:
9421
8.48k
      zend_compile_use_trait(ast);
9422
8.48k
      break;
9423
4.40k
    case ZEND_AST_CLASS:
9424
4.40k
      zend_compile_class_decl(NULL, ast, 0);
9425
4.40k
      break;
9426
613
    case ZEND_AST_GROUP_USE:
9427
613
      zend_compile_group_use(ast);
9428
613
      break;
9429
4.46k
    case ZEND_AST_USE:
9430
4.46k
      zend_compile_use(ast);
9431
4.46k
      break;
9432
30.8k
    case ZEND_AST_CONST_DECL:
9433
30.8k
      zend_compile_const_decl(ast);
9434
30.8k
      break;
9435
10.2k
    case ZEND_AST_NAMESPACE:
9436
10.2k
      zend_compile_namespace(ast);
9437
10.2k
      break;
9438
630
    case ZEND_AST_HALT_COMPILER:
9439
630
      zend_compile_halt_compiler(ast);
9440
630
      break;
9441
28.0k
    case ZEND_AST_THROW:
9442
28.0k
      zend_compile_expr(NULL, ast);
9443
28.0k
      break;
9444
3.05M
    default:
9445
3.05M
    {
9446
3.05M
      znode result;
9447
3.05M
      zend_compile_expr(&result, ast);
9448
3.05M
      zend_do_free(&result);
9449
3.05M
    }
9450
6.85M
  }
9451
9452
6.83M
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
9453
727
    zend_emit_tick();
9454
727
  }
9455
6.83M
}
9456
/* }}} */
9457
9458
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
9459
12.0M
{
9460
  /* CG(zend_lineno) = ast->lineno; */
9461
12.0M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
9462
9463
12.0M
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
9464
47.8k
    zend_compile_memoized_expr(result, ast);
9465
47.8k
    return;
9466
47.8k
  }
9467
9468
11.9M
  switch (ast->kind) {
9469
4.03M
    case ZEND_AST_ZVAL:
9470
4.03M
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
9471
4.03M
      result->op_type = IS_CONST;
9472
4.03M
      return;
9473
24.0k
    case ZEND_AST_ZNODE:
9474
24.0k
      *result = *zend_ast_get_znode(ast);
9475
24.0k
      return;
9476
1.31M
    case ZEND_AST_VAR:
9477
1.38M
    case ZEND_AST_DIM:
9478
1.44M
    case ZEND_AST_PROP:
9479
1.45M
    case ZEND_AST_NULLSAFE_PROP:
9480
1.45M
    case ZEND_AST_STATIC_PROP:
9481
3.19M
    case ZEND_AST_CALL:
9482
3.39M
    case ZEND_AST_METHOD_CALL:
9483
3.40M
    case ZEND_AST_NULLSAFE_METHOD_CALL:
9484
3.43M
    case ZEND_AST_STATIC_CALL:
9485
3.43M
      zend_compile_var(result, ast, BP_VAR_R, 0);
9486
3.43M
      return;
9487
1.33M
    case ZEND_AST_ASSIGN:
9488
1.33M
      zend_compile_assign(result, ast);
9489
1.33M
      return;
9490
40.7k
    case ZEND_AST_ASSIGN_REF:
9491
40.7k
      zend_compile_assign_ref(result, ast);
9492
40.7k
      return;
9493
562k
    case ZEND_AST_NEW:
9494
562k
      zend_compile_new(result, ast);
9495
562k
      return;
9496
3.55k
    case ZEND_AST_CLONE:
9497
3.55k
      zend_compile_clone(result, ast);
9498
3.55k
      return;
9499
53.5k
    case ZEND_AST_ASSIGN_OP:
9500
53.5k
      zend_compile_compound_assign(result, ast);
9501
53.5k
      return;
9502
895k
    case ZEND_AST_BINARY_OP:
9503
895k
      zend_compile_binary_op(result, ast);
9504
895k
      return;
9505
23.5k
    case ZEND_AST_GREATER:
9506
32.0k
    case ZEND_AST_GREATER_EQUAL:
9507
32.0k
      zend_compile_greater(result, ast);
9508
32.0k
      return;
9509
51.0k
    case ZEND_AST_UNARY_OP:
9510
51.0k
      zend_compile_unary_op(result, ast);
9511
51.0k
      return;
9512
14.4k
    case ZEND_AST_UNARY_PLUS:
9513
59.8k
    case ZEND_AST_UNARY_MINUS:
9514
59.8k
      zend_compile_unary_pm(result, ast);
9515
59.8k
      return;
9516
29.3k
    case ZEND_AST_AND:
9517
35.6k
    case ZEND_AST_OR:
9518
35.6k
      zend_compile_short_circuiting(result, ast);
9519
35.6k
      return;
9520
60.9k
    case ZEND_AST_POST_INC:
9521
70.8k
    case ZEND_AST_POST_DEC:
9522
70.8k
      zend_compile_post_incdec(result, ast);
9523
70.8k
      return;
9524
13.0k
    case ZEND_AST_PRE_INC:
9525
21.1k
    case ZEND_AST_PRE_DEC:
9526
21.1k
      zend_compile_pre_incdec(result, ast);
9527
21.1k
      return;
9528
28.7k
    case ZEND_AST_CAST:
9529
28.7k
      zend_compile_cast(result, ast);
9530
28.7k
      return;
9531
18.5k
    case ZEND_AST_CONDITIONAL:
9532
18.5k
      zend_compile_conditional(result, ast);
9533
18.5k
      return;
9534
3.52k
    case ZEND_AST_COALESCE:
9535
3.52k
      zend_compile_coalesce(result, ast);
9536
3.52k
      return;
9537
28.8k
    case ZEND_AST_ASSIGN_COALESCE:
9538
28.8k
      zend_compile_assign_coalesce(result, ast);
9539
28.8k
      return;
9540
41.9k
    case ZEND_AST_PRINT:
9541
41.9k
      zend_compile_print(result, ast);
9542
41.9k
      return;
9543
3.47k
    case ZEND_AST_EXIT:
9544
3.47k
      zend_compile_exit(result, ast);
9545
3.47k
      return;
9546
31.8k
    case ZEND_AST_YIELD:
9547
31.8k
      zend_compile_yield(result, ast);
9548
31.8k
      return;
9549
7.23k
    case ZEND_AST_YIELD_FROM:
9550
7.23k
      zend_compile_yield_from(result, ast);
9551
7.23k
      return;
9552
2.81k
    case ZEND_AST_INSTANCEOF:
9553
2.81k
      zend_compile_instanceof(result, ast);
9554
2.81k
      return;
9555
26.5k
    case ZEND_AST_INCLUDE_OR_EVAL:
9556
26.5k
      zend_compile_include_or_eval(result, ast);
9557
26.5k
      return;
9558
78.1k
    case ZEND_AST_ISSET:
9559
123k
    case ZEND_AST_EMPTY:
9560
123k
      zend_compile_isset_or_empty(result, ast);
9561
123k
      return;
9562
36.4k
    case ZEND_AST_SILENCE:
9563
36.4k
      zend_compile_silence(result, ast);
9564
36.4k
      return;
9565
247
    case ZEND_AST_SHELL_EXEC:
9566
247
      zend_compile_shell_exec(result, ast);
9567
247
      return;
9568
213k
    case ZEND_AST_ARRAY:
9569
213k
      zend_compile_array(result, ast);
9570
213k
      return;
9571
423k
    case ZEND_AST_CONST:
9572
423k
      zend_compile_const(result, ast);
9573
423k
      return;
9574
45.1k
    case ZEND_AST_CLASS_CONST:
9575
45.1k
      zend_compile_class_const(result, ast);
9576
45.1k
      return;
9577
21.5k
    case ZEND_AST_CLASS_NAME:
9578
21.5k
      zend_compile_class_name(result, ast);
9579
21.5k
      return;
9580
105k
    case ZEND_AST_ENCAPS_LIST:
9581
105k
      zend_compile_encaps_list(result, ast);
9582
105k
      return;
9583
33.8k
    case ZEND_AST_MAGIC_CONST:
9584
33.8k
      zend_compile_magic_const(result, ast);
9585
33.8k
      return;
9586
44.8k
    case ZEND_AST_CLOSURE:
9587
60.6k
    case ZEND_AST_ARROW_FUNC:
9588
60.6k
      zend_compile_func_decl(result, ast, 0);
9589
60.6k
      return;
9590
28.9k
    case ZEND_AST_THROW:
9591
28.9k
      zend_compile_throw(result, ast);
9592
28.9k
      return;
9593
4.38k
    case ZEND_AST_MATCH:
9594
4.38k
      zend_compile_match(result, ast);
9595
4.38k
      return;
9596
0
    default:
9597
0
      ZEND_ASSERT(0 /* not supported */);
9598
11.9M
  }
9599
11.9M
}
9600
/* }}} */
9601
9602
void zend_compile_expr(znode *result, zend_ast *ast)
9603
12.0M
{
9604
12.0M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
9605
12.0M
  zend_compile_expr_inner(result, ast);
9606
12.0M
  zend_short_circuiting_commit(checkpoint, result, ast);
9607
12.0M
}
9608
9609
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
9610
4.89M
{
9611
4.89M
  CG(zend_lineno) = zend_ast_get_lineno(ast);
9612
9613
4.89M
  switch (ast->kind) {
9614
1.84M
    case ZEND_AST_VAR:
9615
1.84M
      return zend_compile_simple_var(result, ast, type, 0);
9616
149k
    case ZEND_AST_DIM:
9617
149k
      return zend_compile_dim(result, ast, type);
9618
133k
    case ZEND_AST_PROP:
9619
135k
    case ZEND_AST_NULLSAFE_PROP:
9620
135k
      return zend_compile_prop(result, ast, type, by_ref);
9621
31.9k
    case ZEND_AST_STATIC_PROP:
9622
31.9k
      return zend_compile_static_prop(result, ast, type, by_ref, 0);
9623
2.23M
    case ZEND_AST_CALL:
9624
2.23M
      zend_compile_call(result, ast, type);
9625
2.23M
      return NULL;
9626
431k
    case ZEND_AST_METHOD_CALL:
9627
442k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
9628
442k
      zend_compile_method_call(result, ast, type);
9629
442k
      return NULL;
9630
36.9k
    case ZEND_AST_STATIC_CALL:
9631
36.9k
      zend_compile_static_call(result, ast, type);
9632
36.9k
      return NULL;
9633
1.76k
    case ZEND_AST_ZNODE:
9634
1.76k
      *result = *zend_ast_get_znode(ast);
9635
1.76k
      return NULL;
9636
9.29k
    default:
9637
9.29k
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
9638
426
        zend_error_noreturn(E_COMPILE_ERROR,
9639
426
          "Cannot use temporary expression in write context");
9640
426
      }
9641
9642
8.87k
      zend_compile_expr(result, ast);
9643
8.87k
      return NULL;
9644
4.89M
  }
9645
4.89M
}
9646
9647
zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
9648
4.89M
{
9649
4.89M
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
9650
4.89M
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
9651
4.89M
  zend_short_circuiting_commit(checkpoint, result, ast);
9652
4.89M
  return opcode;
9653
4.89M
}
9654
9655
zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, zend_bool by_ref) /* {{{ */
9656
1.85M
{
9657
1.85M
  switch (ast->kind) {
9658
1.71M
    case ZEND_AST_VAR:
9659
1.71M
      return zend_compile_simple_var(result, ast, type, 1);
9660
38.4k
    case ZEND_AST_DIM:
9661
38.4k
      return zend_delayed_compile_dim(result, ast, type);
9662
51.8k
    case ZEND_AST_PROP:
9663
53.6k
    case ZEND_AST_NULLSAFE_PROP:
9664
53.6k
    {
9665
53.6k
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
9666
53.6k
      if (by_ref) {
9667
12.4k
        opline->extended_value |= ZEND_FETCH_REF;
9668
12.4k
      }
9669
53.6k
      return opline;
9670
51.8k
    }
9671
17.8k
    case ZEND_AST_STATIC_PROP:
9672
17.8k
      return zend_compile_static_prop(result, ast, type, by_ref, 1);
9673
32.8k
    default:
9674
32.8k
      return zend_compile_var(result, ast, type, 0);
9675
1.85M
  }
9676
1.85M
}
9677
/* }}} */
9678
9679
void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
9680
1.99M
{
9681
1.99M
  zend_ast *ast = *ast_ptr;
9682
1.99M
  zval result;
9683
9684
1.99M
  if (!ast) {
9685
649k
    return;
9686
649k
  }
9687
9688
1.34M
  switch (ast->kind) {
9689
28.8k
    case ZEND_AST_BINARY_OP:
9690
28.8k
      zend_eval_const_expr(&ast->child[0]);
9691
28.8k
      zend_eval_const_expr(&ast->child[1]);
9692
28.8k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
9693
8.61k
        return;
9694
8.61k
      }
9695
9696
20.2k
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
9697
20.2k
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
9698
2.15k
      ) {
9699
2.15k
        return;
9700
2.15k
      }
9701
18.1k
      break;
9702
1.71k
    case ZEND_AST_GREATER:
9703
3.45k
    case ZEND_AST_GREATER_EQUAL:
9704
3.45k
      zend_eval_const_expr(&ast->child[0]);
9705
3.45k
      zend_eval_const_expr(&ast->child[1]);
9706
3.45k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
9707
2.85k
        return;
9708
2.85k
      }
9709
9710
599
      zend_ct_eval_greater(&result, ast->kind,
9711
599
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
9712
599
      break;
9713
2.26k
    case ZEND_AST_AND:
9714
4.13k
    case ZEND_AST_OR:
9715
4.13k
    {
9716
4.13k
      zend_bool child0_is_true, child1_is_true;
9717
4.13k
      zend_eval_const_expr(&ast->child[0]);
9718
4.13k
      zend_eval_const_expr(&ast->child[1]);
9719
4.13k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
9720
3.20k
        return;
9721
3.20k
      }
9722
9723
939
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
9724
939
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
9725
319
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
9726
319
        break;
9727
319
      }
9728
9729
620
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
9730
51
        return;
9731
51
      }
9732
9733
569
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
9734
569
      if (ast->kind == ZEND_AST_OR) {
9735
176
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
9736
393
      } else {
9737
393
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
9738
393
      }
9739
569
      break;
9740
569
    }
9741
2.71k
    case ZEND_AST_UNARY_OP:
9742
2.71k
      zend_eval_const_expr(&ast->child[0]);
9743
2.71k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
9744
1.38k
        return;
9745
1.38k
      }
9746
9747
1.33k
      zend_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]));
9748
1.33k
      break;
9749
3.11k
    case ZEND_AST_UNARY_PLUS:
9750
11.9k
    case ZEND_AST_UNARY_MINUS:
9751
11.9k
      zend_eval_const_expr(&ast->child[0]);
9752
11.9k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
9753
2.93k
        return;
9754
2.93k
      }
9755
9756
8.99k
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
9757
262
        return;
9758
262
      }
9759
8.73k
      break;
9760
7.57k
    case ZEND_AST_COALESCE:
9761
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
9762
7.57k
      if (ast->child[0]->kind == ZEND_AST_DIM) {
9763
3.69k
        ast->child[0]->attr |= ZEND_DIM_IS;
9764
3.69k
      }
9765
7.57k
      zend_eval_const_expr(&ast->child[0]);
9766
9767
7.57k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
9768
        /* ensure everything was compile-time evaluated at least once */
9769
3.88k
        zend_eval_const_expr(&ast->child[1]);
9770
3.88k
        return;
9771
3.88k
      }
9772
9773
3.69k
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
9774
3.09k
        zend_eval_const_expr(&ast->child[1]);
9775
3.09k
        *ast_ptr = ast->child[1];
9776
3.09k
        ast->child[1] = NULL;
9777
3.09k
        zend_ast_destroy(ast);
9778
593
      } else {
9779
593
        *ast_ptr = ast->child[0];
9780
593
        ast->child[0] = NULL;
9781
593
        zend_ast_destroy(ast);
9782
593
      }
9783
3.69k
      return;
9784
4.36k
    case ZEND_AST_CONDITIONAL:
9785
4.36k
    {
9786
4.36k
      zend_ast **child, *child_ast;
9787
4.36k
      zend_eval_const_expr(&ast->child[0]);
9788
4.36k
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
9789
        /* ensure everything was compile-time evaluated at least once */
9790
3.22k
        if (ast->child[1]) {
9791
1.60k
          zend_eval_const_expr(&ast->child[1]);
9792
1.60k
        }
9793
3.22k
        zend_eval_const_expr(&ast->child[2]);
9794
3.22k
        return;
9795
3.22k
      }
9796
9797
1.13k
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
9798
1.13k
      if (*child == NULL) {
9799
269
        child--;
9800
269
      }
9801
1.13k
      child_ast = *child;
9802
1.13k
      *child = NULL;
9803
1.13k
      zend_ast_destroy(ast);
9804
1.13k
      *ast_ptr = child_ast;
9805
1.13k
      zend_eval_const_expr(ast_ptr);
9806
1.13k
      return;
9807
1.13k
    }
9808
15.2k
    case ZEND_AST_DIM:
9809
15.2k
    {
9810
      /* constant expression should be always read context ... */
9811
15.2k
      zval *container, *dim;
9812
9813
15.2k
      if (ast->child[1] == NULL) {
9814
58
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
9815
58
      }
9816
9817
15.1k
      if (ast->attr & ZEND_DIM_ALTERNATIVE_SYNTAX) {
9818
20
        ast->attr &= ~ZEND_DIM_ALTERNATIVE_SYNTAX; /* remove flag to avoid duplicate warning */
9819
20
        zend_error(E_COMPILE_ERROR, "Array and string offset access syntax with curly braces is no longer supported");
9820
20
      }
9821
9822
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
9823
15.1k
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
9824
6.37k
        ast->child[0]->attr |= ZEND_DIM_IS;
9825
6.37k
      }
9826
9827
15.1k
      zend_eval_const_expr(&ast->child[0]);
9828
15.1k
      zend_eval_const_expr(&ast->child[1]);
9829
15.1k
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
9830
14.4k
        return;
9831
14.4k
      }
9832
9833
704
      container = zend_ast_get_zval(ast->child[0]);
9834
704
      dim = zend_ast_get_zval(ast->child[1]);
9835
9836
704
      if (Z_TYPE_P(container) == IS_ARRAY) {
9837
559
        zval *el;
9838
559
        if (Z_TYPE_P(dim) == IS_LONG) {
9839
345
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
9840
345
          if (el) {
9841
286
            ZVAL_COPY(&result, el);
9842
59
          } else {
9843
59
            return;
9844
59
          }
9845
214
        } else if (Z_TYPE_P(dim) == IS_STRING) {
9846
194
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
9847
194
          if (el) {
9848
161
            ZVAL_COPY(&result, el);
9849
33
          } else {
9850
33
            return;
9851
33
          }
9852
20
        } else {
9853
20
          return; /* warning... handle at runtime */
9854
20
        }
9855
145
      } else if (Z_TYPE_P(container) == IS_STRING) {
9856
89
        zend_long offset;
9857
89
        zend_uchar c;
9858
89
        if (Z_TYPE_P(dim) == IS_LONG) {
9859
79
          offset = Z_LVAL_P(dim);
9860
10
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
9861
10
          return;
9862
10
        }
9863
79
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
9864
36
          return;
9865
36
        }
9866
43
        c = (zend_uchar) Z_STRVAL_P(container)[offset];
9867
43
        ZVAL_CHAR(&result, c);
9868
56
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
9869
29
        ZVAL_NULL(&result);
9870
27
      } else {
9871
27
        return;
9872
27
      }
9873
519
      break;
9874
519
    }
9875
113k
    case ZEND_AST_ARRAY:
9876
113k
      if (!zend_try_ct_eval_array(&result, ast)) {
9877
63.2k
        return;
9878
63.2k
      }
9879
50.7k
      break;
9880
1.92k
    case ZEND_AST_MAGIC_CONST:
9881
1.92k
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
9882
238
        return;
9883
238
      }
9884
1.68k
      break;
9885
130k
    case ZEND_AST_CONST:
9886
130k
    {
9887
130k
      zend_ast *name_ast = ast->child[0];
9888
130k
      zend_bool is_fully_qualified;
9889
130k
      zend_string *resolved_name = zend_resolve_const_name(
9890
130k
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
9891
9892
130k
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
9893
31.0k
        zend_string_release_ex(resolved_name, 0);
9894
31.0k
        return;
9895
31.0k
      }
9896
9897
99.8k
      zend_string_release_ex(resolved_name, 0);
9898
99.8k
      break;
9899
99.8k
    }
9900
12.5k
    case ZEND_AST_CLASS_CONST:
9901
12.5k
    {
9902
12.5k
      zend_ast *class_ast;
9903
12.5k
      zend_ast *name_ast;
9904
12.5k
      zend_string *resolved_name;
9905
9906
12.5k
      zend_eval_const_expr(&ast->child[0]);
9907
12.5k
      zend_eval_const_expr(&ast->child[1]);
9908
9909
12.5k
      class_ast = ast->child[0];
9910
12.5k
      name_ast = ast->child[1];
9911
9912
12.5k
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
9913
95
        return;
9914
95
      }
9915
9916
12.4k
      resolved_name = zend_resolve_class_name_ast(class_ast);
9917
9918
12.4k
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
9919
8.16k
        zend_string_release_ex(resolved_name, 0);
9920
8.16k
        return;
9921
8.16k
      }
9922
9923
4.29k
      zend_string_release_ex(resolved_name, 0);
9924
4.29k
      break;
9925
4.29k
    }
9926
7.81k
    case ZEND_AST_CLASS_NAME:
9927
7.81k
    {
9928
7.81k
      zend_ast *class_ast = ast->child[0];
9929
7.81k
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
9930
1.62k
        return;
9931
1.62k
      }
9932
6.18k
      break;
9933
6.18k
    }
9934
1.00M
    default:
9935
1.00M
      return;
9936
192k
  }
9937
9938
192k
  zend_ast_destroy(ast);
9939
192k
  *ast_ptr = zend_ast_create_zval(&result);
9940
192k
}
9941
/* }}} */