Coverage Report

Created: 2026-01-18 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_compile.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Nikita Popov <nikic@php.net>                                |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include <zend_language_parser.h>
22
#include "zend.h"
23
#include "zend_ast.h"
24
#include "zend_attributes.h"
25
#include "zend_compile.h"
26
#include "zend_constants.h"
27
#include "zend_llist.h"
28
#include "zend_API.h"
29
#include "zend_exceptions.h"
30
#include "zend_interfaces.h"
31
#include "zend_types.h"
32
#include "zend_virtual_cwd.h"
33
#include "zend_multibyte.h"
34
#include "zend_language_scanner.h"
35
#include "zend_inheritance.h"
36
#include "zend_vm.h"
37
#include "zend_enum.h"
38
#include "zend_observer.h"
39
#include "zend_call_stack.h"
40
#include "zend_frameless_function.h"
41
#include "zend_property_hooks.h"
42
43
11.3k
#define SET_NODE(target, src) do { \
44
11.3k
    target ## _type = (src)->op_type; \
45
11.3k
    if ((src)->op_type == IS_CONST) { \
46
2.61k
      target.constant = zend_add_literal(&(src)->u.constant); \
47
8.72k
    } else { \
48
8.72k
      target = (src)->u.op; \
49
8.72k
    } \
50
11.3k
  } while (0)
51
52
2.17k
#define GET_NODE(target, src) do { \
53
2.17k
    (target)->op_type = src ## _type; \
54
2.17k
    if ((target)->op_type == IS_CONST) { \
55
0
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
2.17k
    } else { \
57
2.17k
      (target)->u.op = src; \
58
2.17k
    } \
59
2.17k
  } while (0)
60
61
3.98k
#define FC(member) (CG(file_context).member)
62
63
typedef struct _zend_loop_var {
64
  uint8_t opcode;
65
  uint8_t var_type;
66
  uint32_t   var_num;
67
  uint32_t   try_catch_offset;
68
} zend_loop_var;
69
70
906
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
906
  if (count == 0) {
72
    /* Even if no cache slots are desired, the VM handler may still want to acquire
73
     * CACHE_ADDR() unconditionally. Returning zero makes sure that the address
74
     * calculation is still legal and ubsan does not complain. */
75
0
    return 0;
76
0
  }
77
78
906
  zend_op_array *op_array = CG(active_op_array);
79
906
  uint32_t ret = op_array->cache_size;
80
906
  op_array->cache_size += count * sizeof(void*);
81
906
  return ret;
82
906
}
83
84
392
static inline uint32_t zend_alloc_cache_slot(void) {
85
392
  return zend_alloc_cache_slots(1);
86
392
}
87
88
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
89
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
90
91
#ifndef ZTS
92
ZEND_API zend_compiler_globals compiler_globals;
93
ZEND_API zend_executor_globals executor_globals;
94
#endif
95
96
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2);
97
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast);
98
static void zend_eval_const_expr(zend_ast **ast_ptr);
99
100
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
101
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
102
static void zend_compile_expr(znode *result, zend_ast *ast);
103
static void zend_compile_stmt(zend_ast *ast);
104
static void zend_compile_assign(znode *result, zend_ast *ast);
105
106
#ifdef ZEND_CHECK_STACK_LIMIT
107
zend_never_inline static void zend_stack_limit_error(void)
108
0
{
109
0
  size_t max_stack_size = 0;
110
0
  if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) {
111
0
    max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit));
112
0
  }
113
114
0
  zend_error_noreturn(E_COMPILE_ERROR,
115
0
    "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression",
116
0
    max_stack_size);
117
0
}
118
119
static void zend_check_stack_limit(void)
120
8.44k
{
121
8.44k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
8.44k
}
125
#else /* ZEND_CHECK_STACK_LIMIT */
126
static void zend_check_stack_limit(void)
127
{
128
}
129
#endif /* ZEND_CHECK_STACK_LIMIT */
130
131
static void init_op(zend_op *op)
132
6.41k
{
133
6.41k
  MAKE_NOP(op);
134
6.41k
  op->extended_value = 0;
135
6.41k
  op->lineno = CG(zend_lineno);
136
#ifdef ZEND_VERIFY_TYPE_INFERENCE
137
  op->op1_use_type = 0;
138
  op->op2_use_type = 0;
139
  op->result_use_type = 0;
140
  op->op1_def_type = 0;
141
  op->op2_def_type = 0;
142
  op->result_def_type = 0;
143
#endif
144
6.41k
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
2.89k
{
148
2.89k
  return CG(active_op_array)->last;
149
2.89k
}
150
151
static zend_op *get_next_op(void)
152
5.93k
{
153
5.93k
  zend_op_array *op_array = CG(active_op_array);
154
5.93k
  uint32_t next_op_num = op_array->last++;
155
5.93k
  zend_op *next_op;
156
157
5.93k
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
12
    CG(context).opcodes_size *= 4;
159
12
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
12
  }
161
162
5.93k
  next_op = &(op_array->opcodes[next_op_num]);
163
164
5.93k
  init_op(next_op);
165
166
5.93k
  return next_op;
167
5.93k
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
84
{
171
84
  CG(context).last_brk_cont++;
172
84
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
84
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
84
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
4
{
178
4
  zend_string *filename = CG(active_op_array)->filename;
179
4
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
4
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
4
  return zend_new_interned_string(result);
182
4
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
53
{
187
53
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
53
  if (ns_separator != NULL) {
189
0
    *result = ns_separator + 1;
190
0
    *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
191
0
    return 1;
192
0
  }
193
194
53
  return 0;
195
53
}
196
/* }}} */
197
198
struct reserved_class_name {
199
  const char *name;
200
  size_t len;
201
};
202
static const struct reserved_class_name reserved_class_names[] = {
203
  {ZEND_STRL("bool")},
204
  {ZEND_STRL("false")},
205
  {ZEND_STRL("float")},
206
  {ZEND_STRL("int")},
207
  {ZEND_STRL("null")},
208
  {ZEND_STRL("parent")},
209
  {ZEND_STRL("self")},
210
  {ZEND_STRL("static")},
211
  {ZEND_STRL("string")},
212
  {ZEND_STRL("true")},
213
  {ZEND_STRL("void")},
214
  {ZEND_STRL("never")},
215
  {ZEND_STRL("iterable")},
216
  {ZEND_STRL("object")},
217
  {ZEND_STRL("mixed")},
218
  /* These are not usable as class names because they're proper tokens,
219
   * but they are here for class aliases. */
220
  {ZEND_STRL("array")},
221
  {ZEND_STRL("callable")},
222
  {NULL, 0}
223
};
224
225
static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
226
15
{
227
15
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
15
  const char *uqname = ZSTR_VAL(name);
230
15
  size_t uqname_len = ZSTR_LEN(name);
231
15
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
270
  for (; reserved->name; ++reserved) {
234
255
    if (uqname_len == reserved->len
235
5
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
255
    ) {
237
0
      return 1;
238
0
    }
239
255
  }
240
241
15
  return 0;
242
15
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
15
{
247
15
  if (zend_is_reserved_class_name(name)) {
248
0
    zend_error_noreturn(E_COMPILE_ERROR,
249
0
      "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type);
250
0
  }
251
15
  if (zend_string_equals_literal(name, "_")) {
252
0
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
0
  }
254
15
}
255
/* }}} */
256
257
typedef struct _builtin_type_info {
258
  const char* name;
259
  const size_t name_len;
260
  const uint8_t type;
261
} builtin_type_info;
262
263
static const builtin_type_info builtin_types[] = {
264
  {ZEND_STRL("null"), IS_NULL},
265
  {ZEND_STRL("true"), IS_TRUE},
266
  {ZEND_STRL("false"), IS_FALSE},
267
  {ZEND_STRL("int"), IS_LONG},
268
  {ZEND_STRL("float"), IS_DOUBLE},
269
  {ZEND_STRL("string"), IS_STRING},
270
  {ZEND_STRL("bool"), _IS_BOOL},
271
  {ZEND_STRL("void"), IS_VOID},
272
  {ZEND_STRL("never"), IS_NEVER},
273
  {ZEND_STRL("iterable"), IS_ITERABLE},
274
  {ZEND_STRL("object"), IS_OBJECT},
275
  {ZEND_STRL("mixed"), IS_MIXED},
276
  {NULL, 0, IS_UNDEF}
277
};
278
279
typedef struct {
280
  const char *name;
281
  size_t name_len;
282
  const char *correct_name;
283
} confusable_type_info;
284
285
static const confusable_type_info confusable_types[] = {
286
  {ZEND_STRL("boolean"), "bool"},
287
  {ZEND_STRL("integer"), "int"},
288
  {ZEND_STRL("double"), "float"},
289
  {ZEND_STRL("resource"), NULL},
290
  {NULL, 0, NULL},
291
};
292
293
static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
294
3
{
295
3
  const builtin_type_info *info = &builtin_types[0];
296
297
22
  for (; info->name; ++info) {
298
22
    if (ZSTR_LEN(name) == info->name_len
299
6
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
22
    ) {
301
3
      return info->type;
302
3
    }
303
22
  }
304
305
0
  return 0;
306
3
}
307
/* }}} */
308
309
static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */
310
0
{
311
0
  const confusable_type_info *info = confusable_types;
312
313
  /* Intentionally using case-sensitive comparison here, because "integer" is likely intended
314
   * as a scalar type, while "Integer" is likely a class type. */
315
0
  for (; info->name; ++info) {
316
0
    if (zend_string_equals_cstr(name, info->name, info->name_len)) {
317
0
      *correct_name = info->correct_name;
318
0
      return 1;
319
0
    }
320
0
  }
321
322
0
  return 0;
323
0
}
324
/* }}} */
325
326
0
static bool zend_is_not_imported(zend_string *name) {
327
  /* Assuming "name" is unqualified here. */
328
0
  return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL;
329
0
}
330
331
void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */
332
88
{
333
88
  *prev_context = CG(context);
334
88
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
88
  CG(context).op_array = op_array;
336
88
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
88
  CG(context).vars_size = 0;
338
88
  CG(context).literals_size = 0;
339
88
  CG(context).fast_call_var = -1;
340
88
  CG(context).try_catch_offset = -1;
341
88
  CG(context).current_brk_cont = -1;
342
88
  CG(context).last_brk_cont = 0;
343
88
  CG(context).has_assigned_to_http_response_header = false;
344
88
  CG(context).brk_cont_array = NULL;
345
88
  CG(context).labels = NULL;
346
88
  CG(context).in_jmp_frameless_branch = false;
347
88
  CG(context).active_property_info_name = NULL;
348
88
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
88
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
88
{
354
88
  if (CG(context).brk_cont_array) {
355
19
    efree(CG(context).brk_cont_array);
356
19
    CG(context).brk_cont_array = NULL;
357
19
  }
358
88
  if (CG(context).labels) {
359
0
    zend_hash_destroy(CG(context).labels);
360
0
    FREE_HASHTABLE(CG(context).labels);
361
0
    CG(context).labels = NULL;
362
0
  }
363
88
  CG(context) = *prev_context;
364
88
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
67
{
369
67
  if (FC(imports)) {
370
0
    zend_hash_destroy(FC(imports));
371
0
    efree(FC(imports));
372
0
    FC(imports) = NULL;
373
0
  }
374
375
67
  if (FC(imports_function)) {
376
0
    zend_hash_destroy(FC(imports_function));
377
0
    efree(FC(imports_function));
378
0
    FC(imports_function) = NULL;
379
0
  }
380
381
67
  if (FC(imports_const)) {
382
0
    zend_hash_destroy(FC(imports_const));
383
0
    efree(FC(imports_const));
384
0
    FC(imports_const) = NULL;
385
0
  }
386
387
67
  zend_hash_clean(&FC(seen_symbols));
388
67
}
389
/* }}} */
390
391
67
static void zend_end_namespace(void) /* {{{ */ {
392
67
  FC(in_namespace) = 0;
393
67
  zend_reset_import_tables();
394
67
  if (FC(current_namespace)) {
395
0
    zend_string_release_ex(FC(current_namespace), 0);
396
0
    FC(current_namespace) = NULL;
397
0
  }
398
67
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
67
{
403
67
  *prev_context = CG(file_context);
404
67
  FC(imports) = NULL;
405
67
  FC(imports_function) = NULL;
406
67
  FC(imports_const) = NULL;
407
67
  FC(current_namespace) = NULL;
408
67
  FC(in_namespace) = 0;
409
67
  FC(has_bracketed_namespaces) = 0;
410
67
  FC(declarables).ticks = 0;
411
67
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
67
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
67
{
417
67
  zend_end_namespace();
418
67
  zend_hash_destroy(&FC(seen_symbols));
419
67
  CG(file_context) = *prev_context;
420
67
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
61
{
425
61
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
61
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
61
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
61
  CG(active_class_entry) = NULL;
429
61
  CG(in_compilation) = 0;
430
61
  CG(skip_shebang) = 0;
431
432
61
  CG(encoding_declared) = 0;
433
61
  CG(memoized_exprs) = NULL;
434
61
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
61
}
436
/* }}} */
437
438
24
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
24
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
24
  if (zv) {
441
0
    Z_LVAL_P(zv) |= kind;
442
24
  } else {
443
24
    zval tmp;
444
24
    ZVAL_LONG(&tmp, kind);
445
24
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
24
  }
447
24
}
448
449
0
static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) {
450
0
  const zval *zv = zend_hash_find(&FC(seen_symbols), name);
451
0
  return zv && (Z_LVAL_P(zv) & kind) != 0;
452
0
}
453
454
void init_compiler(void) /* {{{ */
455
61
{
456
61
  CG(arena) = zend_arena_create(64 * 1024);
457
61
  CG(active_op_array) = NULL;
458
61
  memset(&CG(context), 0, sizeof(CG(context)));
459
61
  zend_init_compiler_data_structures();
460
61
  zend_init_rsrc_list();
461
61
  zend_stream_init();
462
61
  CG(unclean_shutdown) = 0;
463
464
61
  CG(delayed_variance_obligations) = NULL;
465
61
  CG(delayed_autoloads) = NULL;
466
61
  CG(unlinked_uses) = NULL;
467
61
  CG(current_linking_class) = NULL;
468
61
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
61
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
61
  zend_restore_compiled_filename(NULL);
475
476
61
  zend_stack_destroy(&CG(loop_var_stack));
477
61
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
61
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
61
  if (CG(delayed_variance_obligations)) {
481
0
    zend_hash_destroy(CG(delayed_variance_obligations));
482
0
    FREE_HASHTABLE(CG(delayed_variance_obligations));
483
0
    CG(delayed_variance_obligations) = NULL;
484
0
  }
485
61
  if (CG(delayed_autoloads)) {
486
0
    zend_hash_destroy(CG(delayed_autoloads));
487
0
    FREE_HASHTABLE(CG(delayed_autoloads));
488
0
    CG(delayed_autoloads) = NULL;
489
0
  }
490
61
  if (CG(unlinked_uses)) {
491
0
    zend_hash_destroy(CG(unlinked_uses));
492
0
    FREE_HASHTABLE(CG(unlinked_uses));
493
0
    CG(unlinked_uses) = NULL;
494
0
  }
495
61
  CG(current_linking_class) = NULL;
496
61
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
71
{
501
71
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
71
  return new_compiled_filename;
503
71
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
140
{
508
140
  if (CG(compiled_filename)) {
509
71
    zend_string_release(CG(compiled_filename));
510
71
    CG(compiled_filename) = NULL;
511
71
  }
512
140
  CG(compiled_filename) = original_compiled_filename;
513
140
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
157
{
518
157
  return CG(compiled_filename);
519
157
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
46
{
524
46
  return CG(zend_lineno);
525
46
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
3.88k
{
530
3.88k
  return CG(in_compilation);
531
3.88k
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
3.29k
{
536
3.29k
  return (uint32_t)CG(active_op_array)->T++;
537
3.29k
}
538
/* }}} */
539
540
2.26k
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
2.26k
  zend_op_array *op_array = CG(active_op_array);
542
2.26k
  int i = 0;
543
2.26k
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
12.4k
  while (i < op_array->last_var) {
546
11.8k
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
1.65k
     && zend_string_equals(op_array->vars[i], name)) {
548
1.65k
      return EX_NUM_TO_VAR(i);
549
1.65k
    }
550
10.1k
    i++;
551
10.1k
  }
552
603
  i = op_array->last_var;
553
603
  op_array->last_var++;
554
603
  if (op_array->last_var > CG(context).vars_size) {
555
72
    CG(context).vars_size += 16; /* FIXME */
556
72
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
72
  }
558
559
603
  op_array->vars[i] = zend_string_copy(name);
560
603
  return EX_NUM_TO_VAR(i);
561
2.26k
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
5.19k
{
566
5.19k
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
5.19k
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
5.19k
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
4.08k
    Z_TYPE_FLAGS_P(zv) = 0;
570
4.08k
  }
571
5.19k
  return Z_STR_P(zv);
572
5.19k
}
573
574
/* Common part of zend_add_literal and zend_append_individual_literal */
575
static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
576
3.28k
{
577
3.28k
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
3.28k
  if (Z_TYPE_P(zv) == IS_STRING) {
579
2.97k
    zval_make_interned_string(zv);
580
2.97k
  }
581
3.28k
  ZVAL_COPY_VALUE(lit, zv);
582
3.28k
  Z_EXTRA_P(lit) = 0;
583
3.28k
}
584
/* }}} */
585
586
/* Is used while compiling a function, using the context to keep track
587
   of an approximate size to avoid to relocate to often.
588
   Literals are truncated to actual size in the second compiler pass (pass_two()). */
589
static int zend_add_literal(zval *zv) /* {{{ */
590
3.28k
{
591
3.28k
  zend_op_array *op_array = CG(active_op_array);
592
3.28k
  uint32_t i = op_array->last_literal;
593
3.28k
  op_array->last_literal++;
594
3.28k
  if (i >= CG(context).literals_size) {
595
474
    while (i >= CG(context).literals_size) {
596
237
      CG(context).literals_size += 16; /* FIXME */
597
237
    }
598
237
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
237
  }
600
3.28k
  zend_insert_literal(op_array, zv, i);
601
3.28k
  return i;
602
3.28k
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
674
{
607
674
  int ret;
608
674
  zval zv;
609
674
  ZVAL_STR(&zv, *str);
610
674
  ret = zend_add_literal(&zv);
611
674
  *str = Z_STR(zv);
612
674
  return ret;
613
674
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
60
{
618
  /* Original name */
619
60
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
60
  zend_string *lc_name = zend_string_tolower(name);
623
60
  zend_add_literal_string(&lc_name);
624
625
60
  return ret;
626
60
}
627
/* }}} */
628
629
static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */
630
0
{
631
0
  const char *unqualified_name;
632
0
  size_t unqualified_name_len;
633
634
  /* Original name */
635
0
  int ret = zend_add_literal_string(&name);
636
637
  /* Lowercased name */
638
0
  zend_string *lc_name = zend_string_tolower(name);
639
0
  zend_add_literal_string(&lc_name);
640
641
  /* Lowercased unqualified name */
642
0
  if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
643
0
    lc_name = zend_string_alloc(unqualified_name_len, 0);
644
0
    zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
645
0
    zend_add_literal_string(&lc_name);
646
0
  }
647
648
0
  return ret;
649
0
}
650
/* }}} */
651
652
static int zend_add_class_name_literal(zend_string *name) /* {{{ */
653
236
{
654
  /* Original name */
655
236
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
236
  zend_string *lc_name = zend_string_tolower(name);
659
236
  zend_add_literal_string(&lc_name);
660
661
236
  return ret;
662
236
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
37
{
667
37
  zend_string *tmp_name;
668
669
37
  int ret = zend_add_literal_string(&name);
670
671
37
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
37
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
37
  if (after_ns) {
674
0
    after_ns += 1;
675
0
    ns_len = after_ns - ZSTR_VAL(name) - 1;
676
0
    after_ns_len = ZSTR_LEN(name) - ns_len - 1;
677
678
    /* lowercased namespace name & original constant name */
679
0
    tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
680
0
    zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
681
0
    zend_add_literal_string(&tmp_name);
682
683
0
    if (!unqualified) {
684
0
      return ret;
685
0
    }
686
37
  } else {
687
37
    after_ns = ZSTR_VAL(name);
688
37
  }
689
690
  /* original unqualified constant name */
691
37
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
37
  zend_add_literal_string(&tmp_name);
693
694
37
  return ret;
695
37
}
696
/* }}} */
697
698
0
#define LITERAL_STR(op, str) do { \
699
0
    zval _c; \
700
0
    ZVAL_STR(&_c, str); \
701
0
    op.constant = zend_add_literal(&_c); \
702
0
  } while (0)
703
704
void zend_stop_lexing(void)
705
0
{
706
0
  if (LANG_SCNG(on_event)) {
707
0
    LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context));
708
0
  }
709
710
0
  LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
711
0
}
712
713
static inline void zend_begin_loop(
714
    uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */
715
84
{
716
84
  zend_brk_cont_element *brk_cont_element;
717
84
  int parent = CG(context).current_brk_cont;
718
84
  zend_loop_var info = {0};
719
720
84
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
84
  brk_cont_element = get_next_brk_cont_element();
722
84
  brk_cont_element->parent = parent;
723
84
  brk_cont_element->is_switch = is_switch;
724
725
84
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
12
    uint32_t start = get_next_op_number();
727
728
12
    info.opcode = free_opcode;
729
12
    info.var_type = loop_var->op_type;
730
12
    info.var_num = loop_var->u.op.var;
731
12
    brk_cont_element->start = start;
732
72
  } else {
733
72
    info.opcode = ZEND_NOP;
734
    /* The start field is used to free temporary variables in case of exceptions.
735
     * We won't try to free something of we don't have loop variable.  */
736
72
    brk_cont_element->start = -1;
737
72
  }
738
739
84
  zend_stack_push(&CG(loop_var_stack), &info);
740
84
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
84
{
745
84
  uint32_t end = get_next_op_number();
746
84
  zend_brk_cont_element *brk_cont_element
747
84
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
84
  brk_cont_element->cont = cont_addr;
749
84
  brk_cont_element->brk = end;
750
84
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
84
  zend_stack_del_top(&CG(loop_var_stack));
753
84
}
754
/* }}} */
755
756
static void zend_do_free(znode *op1) /* {{{ */
757
973
{
758
973
  if (op1->op_type == IS_TMP_VAR) {
759
524
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
760
761
538
    while (opline->opcode == ZEND_END_SILENCE ||
762
538
           opline->opcode == ZEND_OP_DATA) {
763
14
      opline--;
764
14
    }
765
766
524
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
767
524
      switch (opline->opcode) {
768
5
        case ZEND_BOOL:
769
17
        case ZEND_BOOL_NOT:
770
          /* boolean results don't have to be freed */
771
17
          return;
772
0
        case ZEND_POST_INC_STATIC_PROP:
773
0
        case ZEND_POST_DEC_STATIC_PROP:
774
0
        case ZEND_POST_INC_OBJ:
775
0
        case ZEND_POST_DEC_OBJ:
776
31
        case ZEND_POST_INC:
777
31
        case ZEND_POST_DEC:
778
          /* convert $i++ to ++$i */
779
31
          opline->opcode -= 2;
780
31
          SET_UNUSED(opline->result);
781
31
          return;
782
322
        case ZEND_ASSIGN:
783
336
        case ZEND_ASSIGN_DIM:
784
336
        case ZEND_ASSIGN_OBJ:
785
336
        case ZEND_ASSIGN_STATIC_PROP:
786
377
        case ZEND_ASSIGN_OP:
787
377
        case ZEND_ASSIGN_DIM_OP:
788
377
        case ZEND_ASSIGN_OBJ_OP:
789
377
        case ZEND_ASSIGN_STATIC_PROP_OP:
790
377
        case ZEND_PRE_INC_STATIC_PROP:
791
377
        case ZEND_PRE_DEC_STATIC_PROP:
792
377
        case ZEND_PRE_INC_OBJ:
793
377
        case ZEND_PRE_DEC_OBJ:
794
388
        case ZEND_PRE_INC:
795
388
        case ZEND_PRE_DEC:
796
388
          SET_UNUSED(opline->result);
797
388
          return;
798
524
      }
799
524
    }
800
801
88
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
802
449
  } else if (op1->op_type == IS_VAR) {
803
213
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
804
213
    while (opline->opcode == ZEND_END_SILENCE ||
805
213
        opline->opcode == ZEND_EXT_FCALL_END ||
806
213
        opline->opcode == ZEND_OP_DATA) {
807
0
      opline--;
808
0
    }
809
213
    if (opline->result_type == IS_VAR
810
213
      && opline->result.var == op1->u.op.var) {
811
213
      if (opline->opcode == ZEND_FETCH_THIS) {
812
0
        opline->opcode = ZEND_NOP;
813
0
      }
814
213
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
815
213
        SET_UNUSED(opline->result);
816
213
      } else {
817
        /* Frameless calls usually use the return value, so always emit a free. This should be
818
         * faster than checking RETURN_VALUE_USED inside the handler. */
819
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
820
0
      }
821
213
    } else {
822
0
      while (opline >= CG(active_op_array)->opcodes) {
823
0
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
824
0
             opline->opcode == ZEND_FETCH_LIST_W ||
825
0
             opline->opcode == ZEND_EXT_STMT) &&
826
0
            opline->op1_type == IS_VAR &&
827
0
            opline->op1.var == op1->u.op.var) {
828
0
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
829
0
          return;
830
0
        }
831
0
        if (opline->result_type == IS_VAR
832
0
          && opline->result.var == op1->u.op.var) {
833
0
          if (opline->opcode == ZEND_NEW) {
834
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
835
0
          }
836
0
          break;
837
0
        }
838
0
        opline--;
839
0
      }
840
0
    }
841
236
  } else if (op1->op_type == IS_CONST) {
842
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
843
     * free the zend_array structure, so references to it from outside the op array
844
     * become invalid. GC would cause such a reference in the root buffer. */
845
216
    zval_ptr_dtor_nogc(&op1->u.constant);
846
216
  }
847
973
}
848
/* }}} */
849
850
851
static const char *zend_modifier_token_to_string(uint32_t token)
852
0
{
853
0
  switch (token) {
854
0
    case T_PUBLIC:
855
0
      return "public";
856
0
    case T_PROTECTED:
857
0
      return "protected";
858
0
    case T_PRIVATE:
859
0
      return "private";
860
0
    case T_STATIC:
861
0
      return "static";
862
0
    case T_FINAL:
863
0
      return "final";
864
0
    case T_READONLY:
865
0
      return "readonly";
866
0
    case T_ABSTRACT:
867
0
      return "abstract";
868
0
    case T_PUBLIC_SET:
869
0
      return "public(set)";
870
0
    case T_PROTECTED_SET:
871
0
      return "protected(set)";
872
0
    case T_PRIVATE_SET:
873
0
      return "private(set)";
874
0
    EMPTY_SWITCH_DEFAULT_CASE()
875
0
  }
876
0
}
877
878
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
879
12
{
880
12
  switch (token) {
881
12
    case T_PUBLIC:
882
12
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
883
12
        return ZEND_ACC_PUBLIC;
884
12
      }
885
0
      break;
886
0
    case T_PROTECTED:
887
0
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
888
0
        return ZEND_ACC_PROTECTED;
889
0
      }
890
0
      break;
891
0
    case T_PRIVATE:
892
0
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
893
0
        return ZEND_ACC_PRIVATE;
894
0
      }
895
0
      break;
896
0
    case T_READONLY:
897
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
898
0
        return ZEND_ACC_READONLY;
899
0
      }
900
0
      break;
901
0
    case T_ABSTRACT:
902
0
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
903
0
        return ZEND_ACC_ABSTRACT;
904
0
      }
905
0
      break;
906
0
    case T_FINAL:
907
0
      return ZEND_ACC_FINAL;
908
0
    case T_STATIC:
909
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
910
0
        return ZEND_ACC_STATIC;
911
0
      }
912
0
      break;
913
0
    case T_PUBLIC_SET:
914
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
915
0
        return ZEND_ACC_PUBLIC_SET;
916
0
      }
917
0
      break;
918
0
    case T_PROTECTED_SET:
919
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
920
0
        return ZEND_ACC_PROTECTED_SET;
921
0
      }
922
0
      break;
923
0
    case T_PRIVATE_SET:
924
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
925
0
        return ZEND_ACC_PRIVATE_SET;
926
0
      }
927
0
      break;
928
12
  }
929
930
0
  char *member;
931
0
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
932
0
    member = "property";
933
0
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
934
0
    member = "method";
935
0
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
936
0
    member = "class constant";
937
0
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
938
0
    member = "parameter";
939
0
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
940
0
    member = "property hook";
941
0
  } else {
942
0
    ZEND_UNREACHABLE();
943
0
  }
944
945
0
  zend_throw_exception_ex(zend_ce_compile_error, 0,
946
0
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
947
0
  return 0;
948
0
}
949
950
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
951
12
{
952
12
  uint32_t flags = 0;
953
12
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
954
955
24
  for (uint32_t i = 0; i < modifier_list->children; i++) {
956
12
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
957
12
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
958
12
    if (!new_flag) {
959
0
      return 0;
960
0
    }
961
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
962
12
    bool duplicate_flag = (flags & new_flag);
963
12
    flags = zend_add_member_modifier(flags, new_flag, target);
964
12
    if (!flags) {
965
0
      return 0;
966
0
    }
967
12
    if (duplicate_flag) {
968
0
      zend_throw_exception_ex(zend_ce_compile_error, 0,
969
0
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
970
0
      return 0;
971
0
    }
972
12
  }
973
974
12
  return flags;
975
12
}
976
977
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
978
0
{
979
0
  uint32_t new_flags = flags | new_flag;
980
0
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
981
0
    zend_throw_exception(zend_ce_compile_error,
982
0
      "Multiple abstract modifiers are not allowed", 0);
983
0
    return 0;
984
0
  }
985
0
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
986
0
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
987
0
    return 0;
988
0
  }
989
0
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
990
0
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
991
0
    return 0;
992
0
  }
993
0
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
994
0
    zend_throw_exception(zend_ce_compile_error,
995
0
      "Cannot use the final modifier on an abstract class", 0);
996
0
    return 0;
997
0
  }
998
0
  return new_flags;
999
0
}
1000
/* }}} */
1001
1002
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1003
0
{
1004
0
  uint32_t new_flags = flags | new_flag;
1005
0
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1006
0
    zend_throw_exception(zend_ce_compile_error,
1007
0
      "Cannot use the abstract modifier on an anonymous class", 0);
1008
0
    return 0;
1009
0
  }
1010
0
  if (new_flag & ZEND_ACC_FINAL) {
1011
0
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1012
0
    return 0;
1013
0
  }
1014
0
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1015
0
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1016
0
    return 0;
1017
0
  }
1018
0
  return new_flags;
1019
0
}
1020
1021
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1022
12
{
1023
12
  uint32_t new_flags = flags | new_flag;
1024
12
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1025
0
    zend_throw_exception(zend_ce_compile_error,
1026
0
      "Multiple access type modifiers are not allowed", 0);
1027
0
    return 0;
1028
0
  }
1029
12
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1030
0
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1031
0
      zend_throw_exception(zend_ce_compile_error,
1032
0
        "Cannot use the final modifier on an abstract method", 0);
1033
0
      return 0;
1034
0
    }
1035
0
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1036
0
      zend_throw_exception(zend_ce_compile_error,
1037
0
        "Cannot use the final modifier on an abstract property", 0);
1038
0
      return 0;
1039
0
    }
1040
0
  }
1041
12
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1042
0
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1043
0
      zend_throw_exception(zend_ce_compile_error,
1044
0
        "Multiple access type modifiers are not allowed", 0);
1045
0
      return 0;
1046
0
    }
1047
0
  }
1048
12
  return new_flags;
1049
12
}
1050
/* }}} */
1051
1052
4
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1053
4
  return zend_string_concat3(
1054
4
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1055
4
    "::", sizeof("::") - 1,
1056
4
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1057
4
}
1058
1059
0
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1060
0
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1061
0
}
1062
1063
451
static zend_string *zend_prefix_with_ns(zend_string *name) {
1064
451
  if (FC(current_namespace)) {
1065
0
    const zend_string *ns = FC(current_namespace);
1066
0
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1067
451
  } else {
1068
451
    return zend_string_copy(name);
1069
451
  }
1070
451
}
1071
1072
static zend_string *zend_resolve_non_class_name(
1073
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1074
  bool case_sensitive, const HashTable *current_import_sub
1075
187
) {
1076
187
  const char *compound;
1077
187
  *is_fully_qualified = false;
1078
1079
187
  if (ZSTR_VAL(name)[0] == '\\') {
1080
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1081
0
    *is_fully_qualified = true;
1082
0
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1083
0
  }
1084
1085
187
  if (type == ZEND_NAME_FQ) {
1086
0
    *is_fully_qualified = true;
1087
0
    return zend_string_copy(name);
1088
0
  }
1089
1090
187
  if (type == ZEND_NAME_RELATIVE) {
1091
0
    *is_fully_qualified = true;
1092
0
    return zend_prefix_with_ns(name);
1093
0
  }
1094
1095
187
  if (current_import_sub) {
1096
    /* If an unqualified name is a function/const alias, replace it. */
1097
0
    zend_string *import_name;
1098
0
    if (case_sensitive) {
1099
0
      import_name = zend_hash_find_ptr(current_import_sub, name);
1100
0
    } else {
1101
0
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1102
0
    }
1103
1104
0
    if (import_name) {
1105
0
      *is_fully_qualified = true;
1106
0
      return zend_string_copy(import_name);
1107
0
    }
1108
0
  }
1109
1110
187
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1111
187
  if (compound) {
1112
0
    *is_fully_qualified = true;
1113
0
  }
1114
1115
187
  if (compound && FC(imports)) {
1116
    /* If the first part of a qualified name is an alias, substitute it. */
1117
0
    size_t len = compound - ZSTR_VAL(name);
1118
0
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1119
1120
0
    if (import_name) {
1121
0
      return zend_concat_names(
1122
0
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1123
0
    }
1124
0
  }
1125
1126
187
  return zend_prefix_with_ns(name);
1127
187
}
1128
/* }}} */
1129
1130
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1131
149
{
1132
149
  return zend_resolve_non_class_name(
1133
149
    name, type, is_fully_qualified, false, FC(imports_function));
1134
149
}
1135
1136
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1137
38
{
1138
38
  return zend_resolve_non_class_name(
1139
38
    name, type, is_fully_qualified, true, FC(imports_const));
1140
38
}
1141
1142
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1143
241
{
1144
241
  const char *compound;
1145
1146
241
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1147
0
    if (type == ZEND_NAME_FQ) {
1148
0
      zend_error_noreturn(E_COMPILE_ERROR,
1149
0
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1150
0
    }
1151
0
    if (type == ZEND_NAME_RELATIVE) {
1152
0
      zend_error_noreturn(E_COMPILE_ERROR,
1153
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1154
0
    }
1155
0
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1156
0
    return zend_string_copy(name);
1157
0
  }
1158
1159
241
  if (type == ZEND_NAME_RELATIVE) {
1160
0
    return zend_prefix_with_ns(name);
1161
0
  }
1162
1163
241
  if (type == ZEND_NAME_FQ) {
1164
0
    if (ZSTR_VAL(name)[0] == '\\') {
1165
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1166
0
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1167
0
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1168
0
        zend_error_noreturn(E_COMPILE_ERROR,
1169
0
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1170
0
      }
1171
0
      return name;
1172
0
    }
1173
1174
0
    return zend_string_copy(name);
1175
0
  }
1176
1177
241
  if (FC(imports)) {
1178
0
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1179
0
    if (compound) {
1180
      /* If the first part of a qualified name is an alias, substitute it. */
1181
0
      size_t len = compound - ZSTR_VAL(name);
1182
0
      const zend_string *import_name =
1183
0
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1184
1185
0
      if (import_name) {
1186
0
        return zend_concat_names(
1187
0
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1188
0
      }
1189
0
    } else {
1190
      /* If an unqualified name is an alias, replace it. */
1191
0
      zend_string *import_name
1192
0
        = zend_hash_find_ptr_lc(FC(imports), name);
1193
1194
0
      if (import_name) {
1195
0
        return zend_string_copy(import_name);
1196
0
      }
1197
0
    }
1198
0
  }
1199
1200
  /* If not fully qualified and not an alias, prepend the current namespace */
1201
241
  return zend_prefix_with_ns(name);
1202
241
}
1203
/* }}} */
1204
1205
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1206
240
{
1207
240
  const zval *class_name = zend_ast_get_zval(ast);
1208
240
  if (Z_TYPE_P(class_name) != IS_STRING) {
1209
0
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1210
0
  }
1211
240
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1212
240
}
1213
/* }}} */
1214
1215
static void label_ptr_dtor(zval *zv) /* {{{ */
1216
0
{
1217
0
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1218
0
}
1219
/* }}} */
1220
1221
0
static void str_dtor(zval *zv)  /* {{{ */ {
1222
0
  zend_string_release_ex(Z_STR_P(zv), 0);
1223
0
}
1224
/* }}} */
1225
1226
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1227
46
{
1228
46
  zend_op_array *op_array = CG(active_op_array);
1229
46
  uint32_t try_catch_offset = op_array->last_try_catch++;
1230
46
  zend_try_catch_element *elem;
1231
1232
46
  op_array->try_catch_array = safe_erealloc(
1233
46
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1234
1235
46
  elem = &op_array->try_catch_array[try_catch_offset];
1236
46
  elem->try_op = try_op;
1237
46
  elem->catch_op = 0;
1238
46
  elem->finally_op = 0;
1239
46
  elem->finally_end = 0;
1240
1241
46
  return try_catch_offset;
1242
46
}
1243
/* }}} */
1244
1245
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1246
0
{
1247
0
  if (function->type == ZEND_USER_FUNCTION) {
1248
0
    zend_op_array *op_array = &function->op_array;
1249
0
    if (op_array->refcount) {
1250
0
      (*op_array->refcount)++;
1251
0
    }
1252
1253
0
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1254
0
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1255
0
  }
1256
1257
0
  if (function->common.function_name) {
1258
0
    zend_string_addref(function->common.function_name);
1259
0
  }
1260
0
}
1261
/* }}} */
1262
1263
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1264
0
{
1265
0
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1266
0
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1267
0
  const zend_function *old_function;
1268
1269
0
  ZEND_ASSERT(zv != NULL);
1270
0
  old_function = Z_PTR_P(zv);
1271
0
  if (old_function->type == ZEND_USER_FUNCTION
1272
0
    && old_function->op_array.last > 0) {
1273
0
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1274
0
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1275
0
          ZSTR_VAL(old_function->op_array.filename),
1276
0
          old_function->op_array.line_start);
1277
0
  } else {
1278
0
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1279
0
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1280
0
  }
1281
0
}
1282
1283
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1284
0
{
1285
0
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1286
0
  if (UNEXPECTED(!added_func)) {
1287
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1288
0
    return FAILURE;
1289
0
  }
1290
1291
0
  if (func->op_array.refcount) {
1292
0
    ++*func->op_array.refcount;
1293
0
  }
1294
0
  if (func->common.function_name) {
1295
0
    zend_string_addref(func->common.function_name);
1296
0
  }
1297
0
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1298
0
  return SUCCESS;
1299
0
}
1300
/* }}} */
1301
1302
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1303
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1304
4
{
1305
4
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1306
4
  bool is_preloaded =
1307
4
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1308
4
  bool success;
1309
4
  if (EXPECTED(!is_preloaded)) {
1310
4
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1311
4
  } else {
1312
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1313
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1314
0
  }
1315
4
  if (UNEXPECTED(!success)) {
1316
0
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1317
0
    ZEND_ASSERT(old_class);
1318
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1319
0
    return NULL;
1320
0
  }
1321
1322
4
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1323
0
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1324
0
    return ce;
1325
0
  }
1326
1327
4
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1328
4
  if (ce) {
1329
4
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1330
4
    return ce;
1331
4
  }
1332
1333
0
  if (!is_preloaded) {
1334
    /* Reload bucket pointer, the hash table may have been reallocated */
1335
0
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1336
0
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1337
0
  } else {
1338
0
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1339
0
  }
1340
0
  return NULL;
1341
4
}
1342
1343
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1344
4
{
1345
4
  zval *rtd_key, *zv;
1346
1347
4
  rtd_key = lcname + 1;
1348
1349
4
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1350
1351
4
  if (UNEXPECTED(!zv)) {
1352
0
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1353
0
    ZEND_ASSERT(ce);
1354
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1355
0
    return FAILURE;
1356
0
  }
1357
1358
  /* Register the derived class */
1359
4
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1360
4
}
1361
/* }}} */
1362
1363
0
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1364
0
  zend_string *result;
1365
0
  if (type == NULL) {
1366
0
    return zend_string_copy(new_type);
1367
0
  }
1368
1369
0
  if (is_intersection) {
1370
0
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1371
0
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1372
0
    zend_string_release(type);
1373
0
  } else {
1374
0
    result = zend_string_concat3(
1375
0
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1376
0
    zend_string_release(type);
1377
0
  }
1378
0
  return result;
1379
0
}
1380
1381
0
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1382
0
  if (scope) {
1383
0
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1384
0
      name = scope->name;
1385
0
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1386
0
      name = scope->parent->name;
1387
0
    }
1388
0
  }
1389
1390
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1391
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1392
0
  size_t len = strlen(ZSTR_VAL(name));
1393
0
  if (len != ZSTR_LEN(name)) {
1394
0
    return zend_string_init(ZSTR_VAL(name), len, 0);
1395
0
  }
1396
0
  return zend_string_copy(name);
1397
0
}
1398
1399
static zend_string *add_intersection_type(zend_string *str,
1400
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1401
  bool is_bracketed)
1402
0
{
1403
0
  const zend_type *single_type;
1404
0
  zend_string *intersection_str = NULL;
1405
1406
0
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1407
0
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1408
0
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1409
1410
0
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1411
0
  } ZEND_TYPE_LIST_FOREACH_END();
1412
1413
0
  ZEND_ASSERT(intersection_str);
1414
1415
0
  if (is_bracketed) {
1416
0
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1417
0
    zend_string_release(intersection_str);
1418
0
    intersection_str = result;
1419
0
  }
1420
0
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1421
0
  zend_string_release(intersection_str);
1422
0
  return str;
1423
0
}
1424
1425
0
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1426
0
  zend_string *str = NULL;
1427
1428
  /* Pure intersection type */
1429
0
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1430
0
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1431
0
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1432
0
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1433
    /* A union type might not be a list */
1434
0
    const zend_type *list_type;
1435
0
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1436
0
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1437
0
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1438
0
        continue;
1439
0
      }
1440
0
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1441
0
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1442
1443
0
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1444
0
      zend_string *resolved = resolve_class_name(name, scope);
1445
0
      str = add_type_string(str, resolved, /* is_intersection */ false);
1446
0
      zend_string_release(resolved);
1447
0
    } ZEND_TYPE_LIST_FOREACH_END();
1448
0
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1449
0
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1450
0
  }
1451
1452
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1453
1454
0
  if (type_mask == MAY_BE_ANY) {
1455
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1456
1457
0
    return str;
1458
0
  }
1459
0
  if (type_mask & MAY_BE_STATIC) {
1460
0
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1461
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1462
0
    if (scope && !zend_is_compiling()) {
1463
0
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1464
0
      if (called_scope) {
1465
0
        name = called_scope->name;
1466
0
      }
1467
0
    }
1468
0
    str = add_type_string(str, name, /* is_intersection */ false);
1469
0
  }
1470
0
  if (type_mask & MAY_BE_CALLABLE) {
1471
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1472
0
  }
1473
0
  if (type_mask & MAY_BE_OBJECT) {
1474
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1475
0
  }
1476
0
  if (type_mask & MAY_BE_ARRAY) {
1477
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1478
0
  }
1479
0
  if (type_mask & MAY_BE_STRING) {
1480
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1481
0
  }
1482
0
  if (type_mask & MAY_BE_LONG) {
1483
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1484
0
  }
1485
0
  if (type_mask & MAY_BE_DOUBLE) {
1486
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1487
0
  }
1488
0
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1489
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1490
0
  } else if (type_mask & MAY_BE_FALSE) {
1491
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1492
0
  } else if (type_mask & MAY_BE_TRUE) {
1493
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1494
0
  }
1495
0
  if (type_mask & MAY_BE_VOID) {
1496
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1497
0
  }
1498
0
  if (type_mask & MAY_BE_NEVER) {
1499
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1500
0
  }
1501
1502
0
  if (type_mask & MAY_BE_NULL) {
1503
0
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1504
0
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1505
0
    if (!is_union && !has_intersection) {
1506
0
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1507
0
      zend_string_release(str);
1508
0
      return nullable_str;
1509
0
    }
1510
1511
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1512
0
  }
1513
0
  return str;
1514
0
}
1515
1516
0
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1517
0
  return zend_type_to_string_resolved(type, NULL);
1518
0
}
1519
1520
0
static bool is_generator_compatible_class_type(const zend_string *name) {
1521
0
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1522
0
    || zend_string_equals_literal_ci(name, "Iterator")
1523
0
    || zend_string_equals_literal_ci(name, "Generator");
1524
0
}
1525
1526
static void zend_mark_function_as_generator(void) /* {{{ */
1527
0
{
1528
0
  if (!CG(active_op_array)->function_name) {
1529
0
    zend_error_noreturn(E_COMPILE_ERROR,
1530
0
      "The \"yield\" expression can only be used inside a function");
1531
0
  }
1532
1533
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1534
0
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1535
0
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1536
0
    if (!valid_type) {
1537
0
      const zend_type *single_type;
1538
0
      ZEND_TYPE_FOREACH(return_type, single_type) {
1539
0
        if (ZEND_TYPE_HAS_NAME(*single_type)
1540
0
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1541
0
          valid_type = true;
1542
0
          break;
1543
0
        }
1544
0
      } ZEND_TYPE_FOREACH_END();
1545
0
    }
1546
1547
0
    if (!valid_type) {
1548
0
      zend_string *str = zend_type_to_string(return_type);
1549
0
      zend_error_noreturn(E_COMPILE_ERROR,
1550
0
        "Generator return type must be a supertype of Generator, %s given",
1551
0
        ZSTR_VAL(str));
1552
0
    }
1553
0
  }
1554
1555
0
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1556
0
}
1557
/* }}} */
1558
1559
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1560
93
{
1561
93
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1562
93
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1563
1564
93
  ZSTR_VAL(prop_name)[0] = '\0';
1565
93
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1566
93
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1567
93
  return prop_name;
1568
93
}
1569
/* }}} */
1570
1571
ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1572
0
{
1573
0
  size_t class_name_len;
1574
0
  size_t anonclass_src_len;
1575
1576
0
  *class_name = NULL;
1577
1578
0
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1579
0
    *prop_name = ZSTR_VAL(name);
1580
0
    if (prop_len) {
1581
0
      *prop_len = ZSTR_LEN(name);
1582
0
    }
1583
0
    return SUCCESS;
1584
0
  }
1585
0
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1586
0
    zend_error(E_NOTICE, "Illegal member variable name");
1587
0
    *prop_name = ZSTR_VAL(name);
1588
0
    if (prop_len) {
1589
0
      *prop_len = ZSTR_LEN(name);
1590
0
    }
1591
0
    return FAILURE;
1592
0
  }
1593
1594
0
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1595
0
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1596
0
    zend_error(E_NOTICE, "Corrupt member variable name");
1597
0
    *prop_name = ZSTR_VAL(name);
1598
0
    if (prop_len) {
1599
0
      *prop_len = ZSTR_LEN(name);
1600
0
    }
1601
0
    return FAILURE;
1602
0
  }
1603
1604
0
  *class_name = ZSTR_VAL(name) + 1;
1605
0
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1606
0
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1607
0
    class_name_len += anonclass_src_len + 1;
1608
0
  }
1609
0
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1610
0
  if (prop_len) {
1611
0
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1612
0
  }
1613
0
  return SUCCESS;
1614
0
}
1615
/* }}} */
1616
1617
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1618
0
{
1619
0
  if (zend_hash_num_elements(array) > *max_checks) {
1620
0
    return false;
1621
0
  }
1622
0
  *max_checks -= zend_hash_num_elements(array);
1623
1624
0
  zval *element;
1625
0
  ZEND_HASH_FOREACH_VAL(array, element) {
1626
0
    if (Z_TYPE_P(element) < IS_ARRAY) {
1627
0
      continue;
1628
0
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1629
0
      if (!array_is_const_ex(array, max_checks)) {
1630
0
        return false;
1631
0
      }
1632
0
    } else {
1633
0
      return false;
1634
0
    }
1635
0
  } ZEND_HASH_FOREACH_END();
1636
1637
0
  return true;
1638
0
}
1639
1640
static bool array_is_const(const zend_array *array)
1641
0
{
1642
0
  uint32_t max_checks = 50;
1643
0
  return array_is_const_ex(array, &max_checks);
1644
0
}
1645
1646
1
static bool can_ct_eval_const(const zend_constant *c) {
1647
1
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1648
0
    return 0;
1649
0
  }
1650
1
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1651
1
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1652
1
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1653
1
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1654
1
    return 1;
1655
1
  }
1656
0
  if (Z_TYPE(c->value) < IS_ARRAY
1657
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1658
0
    return 1;
1659
0
  } else if (Z_TYPE(c->value) == IS_ARRAY
1660
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1661
0
      && array_is_const(Z_ARR(c->value))) {
1662
0
    return 1;
1663
0
  }
1664
0
  return 0;
1665
0
}
1666
1667
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1668
38
{
1669
  /* Substitute true, false and null (including unqualified usage in namespaces)
1670
   * before looking up the possibly namespaced name. */
1671
38
  const char *lookup_name = ZSTR_VAL(name);
1672
38
  size_t lookup_len = ZSTR_LEN(name);
1673
1674
38
  if (!is_fully_qualified) {
1675
38
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1676
38
  }
1677
1678
38
  zend_constant *c;
1679
38
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1680
0
    ZVAL_COPY_VALUE(zv, &c->value);
1681
0
    return 1;
1682
0
  }
1683
38
  c = zend_hash_find_ptr(EG(zend_constants), name);
1684
38
  if (c && can_ct_eval_const(c)) {
1685
1
    ZVAL_COPY_OR_DUP(zv, &c->value);
1686
1
    return 1;
1687
1
  }
1688
37
  return 0;
1689
38
}
1690
/* }}} */
1691
1692
static inline bool zend_is_scope_known(void) /* {{{ */
1693
0
{
1694
0
  if (!CG(active_op_array)) {
1695
    /* This can only happen when evaluating a default value string. */
1696
0
    return 0;
1697
0
  }
1698
1699
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1700
    /* Closures can be rebound to a different scope */
1701
0
    return 0;
1702
0
  }
1703
1704
0
  if (!CG(active_class_entry)) {
1705
    /* The scope is known if we're in a free function (no scope), but not if we're in
1706
     * a file/eval (which inherits including/eval'ing scope). */
1707
0
    return CG(active_op_array)->function_name != NULL;
1708
0
  }
1709
1710
  /* For traits self etc refers to the using class, not the trait itself */
1711
0
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1712
0
}
1713
/* }}} */
1714
1715
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1716
4
{
1717
4
  if (!CG(active_class_entry)) {
1718
4
    return 0;
1719
4
  }
1720
0
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1721
0
    return 1;
1722
0
  }
1723
0
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1724
0
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1725
0
}
1726
/* }}} */
1727
1728
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1729
482
{
1730
482
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1731
0
    return ZEND_FETCH_CLASS_SELF;
1732
482
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1733
0
    return ZEND_FETCH_CLASS_PARENT;
1734
482
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1735
0
    return ZEND_FETCH_CLASS_STATIC;
1736
482
  } else {
1737
482
    return ZEND_FETCH_CLASS_DEFAULT;
1738
482
  }
1739
482
}
1740
/* }}} */
1741
1742
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1743
78
{
1744
  /* Fully qualified names are always default refs */
1745
78
  if (name_ast->attr == ZEND_NAME_FQ) {
1746
0
    return ZEND_FETCH_CLASS_DEFAULT;
1747
0
  }
1748
1749
78
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1750
78
}
1751
/* }}} */
1752
1753
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1754
1
{
1755
1
  zend_string *class_name = zend_ast_get_str(ast);
1756
1
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1757
0
    zend_error_noreturn(E_COMPILE_ERROR,
1758
0
      "Cannot use \"%s\" as %s, as it is reserved",
1759
0
      ZSTR_VAL(class_name), type);
1760
0
  }
1761
1
  return zend_resolve_class_name(class_name, ast->attr);
1762
1
}
1763
1764
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1765
0
{
1766
0
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1767
0
    zend_class_entry *ce = CG(active_class_entry);
1768
0
    if (!ce) {
1769
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1770
0
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1771
0
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1772
0
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1773
0
      zend_error_noreturn(E_COMPILE_ERROR,
1774
0
        "Cannot use \"parent\" when current class scope has no parent");
1775
0
    }
1776
0
  }
1777
0
}
1778
/* }}} */
1779
1780
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1781
0
{
1782
0
  uint32_t fetch_type;
1783
0
  const zval *class_name;
1784
1785
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
1786
0
    return 0;
1787
0
  }
1788
1789
0
  class_name = zend_ast_get_zval(class_ast);
1790
1791
0
  if (Z_TYPE_P(class_name) != IS_STRING) {
1792
0
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1793
0
  }
1794
1795
0
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1796
0
  zend_ensure_valid_class_fetch_type(fetch_type);
1797
1798
0
  switch (fetch_type) {
1799
0
    case ZEND_FETCH_CLASS_SELF:
1800
0
      if (CG(active_class_entry) && zend_is_scope_known()) {
1801
0
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1802
0
        return 1;
1803
0
      }
1804
0
      return 0;
1805
0
    case ZEND_FETCH_CLASS_PARENT:
1806
0
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1807
0
          && zend_is_scope_known()) {
1808
0
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1809
0
        return 1;
1810
0
      }
1811
0
      return 0;
1812
0
    case ZEND_FETCH_CLASS_STATIC:
1813
0
      return 0;
1814
0
    case ZEND_FETCH_CLASS_DEFAULT:
1815
0
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1816
0
      return 1;
1817
0
    EMPTY_SWITCH_DEFAULT_CASE()
1818
0
  }
1819
0
}
1820
/* }}} */
1821
1822
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1823
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1824
0
{
1825
0
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1826
0
    return 0;
1827
0
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1828
    /* This condition is only met on directly accessing trait constants,
1829
     * because the ce is replaced to the class entry of the composing class
1830
     * on binding. */
1831
0
    return 0;
1832
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1833
0
    return 1;
1834
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1835
0
    return c->ce == scope;
1836
0
  } else {
1837
0
    zend_class_entry *ce = c->ce;
1838
0
    while (1) {
1839
0
      if (ce == scope) {
1840
0
        return 1;
1841
0
      }
1842
0
      if (!ce->parent) {
1843
0
        break;
1844
0
      }
1845
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1846
0
        ce = ce->parent;
1847
0
      } else {
1848
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1849
0
        if (!ce) {
1850
0
          break;
1851
0
        }
1852
0
      }
1853
0
    }
1854
    /* Reverse case cannot be true during compilation */
1855
0
    return 0;
1856
0
  }
1857
0
}
1858
1859
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1860
4
{
1861
4
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1862
4
  zend_class_constant *cc;
1863
4
  zval *c;
1864
1865
4
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1866
0
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1867
4
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1868
0
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1869
0
    if (ce) {
1870
0
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1871
0
    } else {
1872
0
      return 0;
1873
0
    }
1874
4
  } else {
1875
4
    return 0;
1876
4
  }
1877
1878
0
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1879
0
    return 0;
1880
0
  }
1881
1882
0
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1883
0
    return 0;
1884
0
  }
1885
1886
0
  c = &cc->value;
1887
1888
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1889
0
  if (Z_TYPE_P(c) < IS_ARRAY) {
1890
0
    ZVAL_COPY_OR_DUP(zv, c);
1891
0
    return 1;
1892
0
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1893
0
    ZVAL_COPY_OR_DUP(zv, c);
1894
0
    return 1;
1895
0
  }
1896
1897
0
  return 0;
1898
0
}
1899
/* }}} */
1900
1901
static void zend_add_to_list(void *result, void *item) /* {{{ */
1902
0
{
1903
0
  void** list = *(void**)result;
1904
0
  size_t n = 0;
1905
1906
0
  if (list) {
1907
0
    while (list[n]) {
1908
0
      n++;
1909
0
    }
1910
0
  }
1911
1912
0
  list = erealloc(list, sizeof(void*) * (n+2));
1913
1914
0
  list[n]   = item;
1915
0
  list[n+1] = NULL;
1916
1917
0
  *(void**)result = list;
1918
0
}
1919
/* }}} */
1920
1921
static void zend_do_extended_stmt(znode* result) /* {{{ */
1922
93
{
1923
93
  zend_op *opline;
1924
1925
93
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1926
93
    return;
1927
93
  }
1928
1929
0
  opline = get_next_op();
1930
1931
0
  opline->opcode = ZEND_EXT_STMT;
1932
0
  if (result) {
1933
0
    SET_NODE(opline->op1, result);
1934
0
  }
1935
0
}
1936
/* }}} */
1937
1938
static void zend_do_extended_fcall_begin(void) /* {{{ */
1939
359
{
1940
359
  zend_op *opline;
1941
1942
359
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1943
359
    return;
1944
359
  }
1945
1946
0
  opline = get_next_op();
1947
1948
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1949
0
}
1950
/* }}} */
1951
1952
static void zend_do_extended_fcall_end(void) /* {{{ */
1953
359
{
1954
359
  zend_op *opline;
1955
1956
359
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1957
359
    return;
1958
359
  }
1959
1960
0
  opline = get_next_op();
1961
1962
0
  opline->opcode = ZEND_EXT_FCALL_END;
1963
0
}
1964
/* }}} */
1965
1966
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1967
0
  zend_auto_global *auto_global;
1968
1969
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1970
0
    if (auto_global->armed) {
1971
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1972
0
    }
1973
0
    return 1;
1974
0
  }
1975
0
  return 0;
1976
0
}
1977
/* }}} */
1978
1979
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1980
2.49k
{
1981
2.49k
  zend_auto_global *auto_global;
1982
1983
2.49k
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1984
0
    if (auto_global->armed) {
1985
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1986
0
    }
1987
0
    return 1;
1988
0
  }
1989
2.49k
  return 0;
1990
2.49k
}
1991
/* }}} */
1992
1993
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1994
16
{
1995
16
  zend_auto_global auto_global;
1996
16
  zend_result retval;
1997
1998
16
  auto_global.name = name;
1999
16
  auto_global.auto_global_callback = auto_global_callback;
2000
16
  auto_global.jit = jit;
2001
2002
16
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2003
2004
16
  return retval;
2005
16
}
2006
/* }}} */
2007
2008
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2009
61
{
2010
61
  zend_auto_global *auto_global;
2011
2012
1.09k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2013
1.09k
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2014
1.09k
  } ZEND_HASH_FOREACH_END();
2015
2016
1.09k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2017
1.09k
    if (auto_global->armed && !auto_global->jit) {
2018
244
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2019
244
    }
2020
1.09k
  } ZEND_HASH_FOREACH_END();
2021
61
}
2022
/* }}} */
2023
2024
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2025
10.1k
{
2026
10.1k
  zval zv;
2027
10.1k
  int ret;
2028
2029
10.1k
  if (CG(increment_lineno)) {
2030
0
    CG(zend_lineno)++;
2031
0
    CG(increment_lineno) = 0;
2032
0
  }
2033
2034
10.1k
  ret = lex_scan(&zv, elem);
2035
10.1k
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2036
10.1k
  return ret;
2037
2038
10.1k
}
2039
/* }}} */
2040
2041
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2042
345
{
2043
345
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2044
2045
345
  ce->refcount = 1;
2046
345
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2047
345
  ce->ce_flags2 = 0;
2048
2049
345
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2050
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2051
0
  }
2052
2053
345
  ce->default_properties_table = NULL;
2054
345
  ce->default_static_members_table = NULL;
2055
345
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2056
345
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2057
345
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2058
2059
345
  ce->doc_comment = NULL;
2060
2061
345
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2062
345
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2063
2064
345
  ce->default_object_handlers = &std_object_handlers;
2065
345
  ce->default_properties_count = 0;
2066
345
  ce->default_static_members_count = 0;
2067
345
  ce->properties_info_table = NULL;
2068
345
  ce->attributes = NULL;
2069
345
  ce->enum_backing_type = IS_UNDEF;
2070
345
  ce->backed_enum_table = NULL;
2071
2072
345
  if (nullify_handlers) {
2073
15
    ce->constructor = NULL;
2074
15
    ce->destructor = NULL;
2075
15
    ce->clone = NULL;
2076
15
    ce->__get = NULL;
2077
15
    ce->__set = NULL;
2078
15
    ce->__unset = NULL;
2079
15
    ce->__isset = NULL;
2080
15
    ce->__call = NULL;
2081
15
    ce->__callstatic = NULL;
2082
15
    ce->__tostring = NULL;
2083
15
    ce->__serialize = NULL;
2084
15
    ce->__unserialize = NULL;
2085
15
    ce->__debugInfo = NULL;
2086
15
    ce->create_object = NULL;
2087
15
    ce->get_iterator = NULL;
2088
15
    ce->iterator_funcs_ptr = NULL;
2089
15
    ce->arrayaccess_funcs_ptr = NULL;
2090
15
    ce->get_static_method = NULL;
2091
15
    ce->parent = NULL;
2092
15
    ce->parent_name = NULL;
2093
15
    ce->num_interfaces = 0;
2094
15
    ce->interfaces = NULL;
2095
15
    ce->num_traits = 0;
2096
15
    ce->num_hooked_props = 0;
2097
15
    ce->num_hooked_prop_variance_checks = 0;
2098
15
    ce->trait_names = NULL;
2099
15
    ce->trait_aliases = NULL;
2100
15
    ce->trait_precedences = NULL;
2101
15
    ce->serialize = NULL;
2102
15
    ce->unserialize = NULL;
2103
15
    if (ce->type == ZEND_INTERNAL_CLASS) {
2104
0
      ce->info.internal.module = NULL;
2105
0
      ce->info.internal.builtin_functions = NULL;
2106
0
    }
2107
15
  }
2108
345
}
2109
/* }}} */
2110
2111
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2112
0
{
2113
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2114
0
}
2115
/* }}} */
2116
2117
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2118
0
{
2119
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2120
0
  zend_string *left = Z_STR_P(left_zv);
2121
0
  zend_string *right = zend_ast_get_str(right_ast);
2122
2123
0
  zend_string *result;
2124
0
  size_t left_len = ZSTR_LEN(left);
2125
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2126
2127
0
  result = zend_string_extend(left, len, 0);
2128
0
  ZSTR_VAL(result)[left_len] = '\\';
2129
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2130
0
  ZSTR_VAL(result)[len] = '\0';
2131
0
  zend_string_release_ex(right, 0);
2132
2133
0
  ZVAL_STR(left_zv, result);
2134
0
  return left_ast;
2135
0
}
2136
/* }}} */
2137
2138
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2139
0
{
2140
0
  zval *zv = zend_ast_get_zval(ast);
2141
0
  if (Z_TYPE_P(zv) == IS_LONG) {
2142
0
    if (Z_LVAL_P(zv) == 0) {
2143
0
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2144
0
    } else {
2145
0
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2146
0
      Z_LVAL_P(zv) *= -1;
2147
0
    }
2148
0
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2149
0
    size_t orig_len = Z_STRLEN_P(zv);
2150
0
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2151
0
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2152
0
    Z_STRVAL_P(zv)[0] = '-';
2153
0
  } else {
2154
0
    ZEND_UNREACHABLE();
2155
0
  }
2156
0
  return ast;
2157
0
}
2158
/* }}} */
2159
2160
static void zend_verify_namespace(void) /* {{{ */
2161
412
{
2162
412
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2163
0
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2164
0
  }
2165
412
}
2166
/* }}} */
2167
2168
/* {{{ zend_dirname
2169
   Returns directory name component of path */
2170
ZEND_API size_t zend_dirname(char *path, size_t len)
2171
0
{
2172
0
  char *end = path + len - 1;
2173
0
  unsigned int len_adjust = 0;
2174
2175
#ifdef ZEND_WIN32
2176
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2177
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2178
   */
2179
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2180
    /* Skip over the drive spec (if any) so as not to change */
2181
    path += 2;
2182
    len_adjust += 2;
2183
    if (2 == len) {
2184
      /* Return "c:" on Win32 for dirname("c:").
2185
       * It would be more consistent to return "c:."
2186
       * but that would require making the string *longer*.
2187
       */
2188
      return len;
2189
    }
2190
  }
2191
#endif
2192
2193
0
  if (len == 0) {
2194
    /* Illegal use of this function */
2195
0
    return 0;
2196
0
  }
2197
2198
  /* Strip trailing slashes */
2199
0
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2200
0
    end--;
2201
0
  }
2202
0
  if (end < path) {
2203
    /* The path only contained slashes */
2204
0
    path[0] = DEFAULT_SLASH;
2205
0
    path[1] = '\0';
2206
0
    return 1 + len_adjust;
2207
0
  }
2208
2209
  /* Strip filename */
2210
0
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2211
0
    end--;
2212
0
  }
2213
0
  if (end < path) {
2214
    /* No slash found, therefore return '.' */
2215
0
    path[0] = '.';
2216
0
    path[1] = '\0';
2217
0
    return 1 + len_adjust;
2218
0
  }
2219
2220
  /* Strip slashes which came before the file name */
2221
0
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2222
0
    end--;
2223
0
  }
2224
0
  if (end < path) {
2225
0
    path[0] = DEFAULT_SLASH;
2226
0
    path[1] = '\0';
2227
0
    return 1 + len_adjust;
2228
0
  }
2229
0
  *(end+1) = '\0';
2230
2231
0
  return (size_t)(end + 1 - path) + len_adjust;
2232
0
}
2233
/* }}} */
2234
2235
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2236
484
{
2237
484
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2238
2239
484
  switch (type) {
2240
466
    case BP_VAR_R:
2241
466
      opline->result_type = IS_TMP_VAR;
2242
466
      result->op_type = IS_TMP_VAR;
2243
466
      return;
2244
18
    case BP_VAR_W:
2245
18
      opline->opcode += 1 * factor;
2246
18
      return;
2247
0
    case BP_VAR_RW:
2248
0
      opline->opcode += 2 * factor;
2249
0
      return;
2250
0
    case BP_VAR_IS:
2251
0
      opline->result_type = IS_TMP_VAR;
2252
0
      result->op_type = IS_TMP_VAR;
2253
0
      opline->opcode += 3 * factor;
2254
0
      return;
2255
0
    case BP_VAR_FUNC_ARG:
2256
0
      opline->opcode += 4 * factor;
2257
0
      return;
2258
0
    case BP_VAR_UNSET:
2259
0
      opline->opcode += 5 * factor;
2260
0
      return;
2261
484
    EMPTY_SWITCH_DEFAULT_CASE()
2262
484
  }
2263
484
}
2264
/* }}} */
2265
2266
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2267
1.01k
{
2268
1.01k
  opline->result_type = IS_VAR;
2269
1.01k
  opline->result.var = get_temporary_variable();
2270
1.01k
  GET_NODE(result, opline->result);
2271
1.01k
}
2272
/* }}} */
2273
2274
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2275
1.14k
{
2276
1.14k
  opline->result_type = IS_TMP_VAR;
2277
1.14k
  opline->result.var = get_temporary_variable();
2278
1.14k
  GET_NODE(result, opline->result);
2279
1.14k
}
2280
/* }}} */
2281
2282
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2283
1.94k
{
2284
1.94k
  zend_op *opline = get_next_op();
2285
1.94k
  opline->opcode = opcode;
2286
2287
1.94k
  if (op1 != NULL) {
2288
1.14k
    SET_NODE(opline->op1, op1);
2289
1.14k
  }
2290
2291
1.94k
  if (op2 != NULL) {
2292
125
    SET_NODE(opline->op2, op2);
2293
125
  }
2294
2295
1.94k
  if (result) {
2296
530
    zend_make_var_result(result, opline);
2297
530
  }
2298
1.94k
  return opline;
2299
1.94k
}
2300
/* }}} */
2301
2302
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2303
1.13k
{
2304
1.13k
  zend_op *opline = get_next_op();
2305
1.13k
  opline->opcode = opcode;
2306
2307
1.13k
  if (op1 != NULL) {
2308
1.09k
    SET_NODE(opline->op1, op1);
2309
1.09k
  }
2310
2311
1.13k
  if (op2 != NULL) {
2312
904
    SET_NODE(opline->op2, op2);
2313
904
  }
2314
2315
1.13k
  if (result) {
2316
1.09k
    zend_make_tmp_result(result, opline);
2317
1.09k
  }
2318
2319
1.13k
  return opline;
2320
1.13k
}
2321
/* }}} */
2322
2323
static void zend_emit_tick(void) /* {{{ */
2324
0
{
2325
0
  zend_op *opline;
2326
2327
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2328
0
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2329
0
    return;
2330
0
  }
2331
2332
0
  opline = get_next_op();
2333
2334
0
  opline->opcode = ZEND_TICKS;
2335
0
  opline->extended_value = FC(declarables).ticks;
2336
0
}
2337
/* }}} */
2338
2339
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2340
14
{
2341
14
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2342
14
}
2343
/* }}} */
2344
2345
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2346
161
{
2347
161
  uint32_t opnum = get_next_op_number();
2348
161
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2349
161
  opline->op1.opline_num = opnum_target;
2350
161
  return opnum;
2351
161
}
2352
/* }}} */
2353
2354
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2355
76
{
2356
76
  switch (opline->opcode) {
2357
0
    case ZEND_IS_IDENTICAL:
2358
0
    case ZEND_IS_NOT_IDENTICAL:
2359
4
    case ZEND_IS_EQUAL:
2360
4
    case ZEND_IS_NOT_EQUAL:
2361
15
    case ZEND_IS_SMALLER:
2362
76
    case ZEND_IS_SMALLER_OR_EQUAL:
2363
76
    case ZEND_CASE:
2364
76
    case ZEND_CASE_STRICT:
2365
76
    case ZEND_ISSET_ISEMPTY_CV:
2366
76
    case ZEND_ISSET_ISEMPTY_VAR:
2367
76
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2368
76
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2369
76
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2370
76
    case ZEND_INSTANCEOF:
2371
76
    case ZEND_TYPE_CHECK:
2372
76
    case ZEND_DEFINED:
2373
76
    case ZEND_IN_ARRAY:
2374
76
    case ZEND_ARRAY_KEY_EXISTS:
2375
76
      return 1;
2376
0
    default:
2377
0
      return 0;
2378
76
  }
2379
76
}
2380
/* }}} */
2381
2382
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2383
76
{
2384
76
  uint32_t opnum = get_next_op_number();
2385
76
  zend_op *opline;
2386
2387
76
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2388
76
    opline = CG(active_op_array)->opcodes + opnum - 1;
2389
76
    if (opline->result_type == IS_TMP_VAR
2390
76
     && opline->result.var == cond->u.op.var
2391
76
     && zend_is_smart_branch(opline)) {
2392
76
      if (opcode == ZEND_JMPZ) {
2393
4
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2394
72
      } else {
2395
72
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2396
72
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2397
72
      }
2398
76
    }
2399
76
  }
2400
76
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2401
76
  opline->op2.opline_num = opnum_target;
2402
76
  return opnum;
2403
76
}
2404
/* }}} */
2405
2406
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2407
183
{
2408
183
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2409
183
  switch (opline->opcode) {
2410
149
    case ZEND_JMP:
2411
149
      opline->op1.opline_num = opnum_target;
2412
149
      break;
2413
4
    case ZEND_JMPZ:
2414
4
    case ZEND_JMPNZ:
2415
16
    case ZEND_JMPZ_EX:
2416
22
    case ZEND_JMPNZ_EX:
2417
34
    case ZEND_JMP_SET:
2418
34
    case ZEND_COALESCE:
2419
34
    case ZEND_JMP_NULL:
2420
34
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2421
34
    case ZEND_JMP_FRAMELESS:
2422
34
      opline->op2.opline_num = opnum_target;
2423
34
      break;
2424
183
    EMPTY_SWITCH_DEFAULT_CASE()
2425
183
  }
2426
183
}
2427
/* }}} */
2428
2429
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2430
183
{
2431
183
  zend_update_jump_target(opnum_jump, get_next_op_number());
2432
183
}
2433
/* }}} */
2434
2435
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2436
484
{
2437
484
  zend_op tmp_opline;
2438
2439
484
  init_op(&tmp_opline);
2440
2441
484
  tmp_opline.opcode = opcode;
2442
484
  if (op1 != NULL) {
2443
484
    SET_NODE(tmp_opline.op1, op1);
2444
484
  }
2445
484
  if (op2 != NULL) {
2446
480
    SET_NODE(tmp_opline.op2, op2);
2447
480
  }
2448
484
  if (result) {
2449
484
    zend_make_var_result(result, &tmp_opline);
2450
484
  }
2451
2452
484
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2453
484
  return zend_stack_top(&CG(delayed_oplines_stack));
2454
484
}
2455
/* }}} */
2456
2457
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2458
934
{
2459
934
  return zend_stack_count(&CG(delayed_oplines_stack));
2460
934
}
2461
/* }}} */
2462
2463
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2464
934
{
2465
934
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2466
934
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2467
2468
934
  ZEND_ASSERT(count >= offset);
2469
1.41k
  for (i = offset; i < count; ++i) {
2470
484
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2471
484
      opline = get_next_op();
2472
484
      memcpy(opline, &oplines[i], sizeof(zend_op));
2473
484
    } else {
2474
0
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2475
0
    }
2476
484
  }
2477
2478
934
  CG(delayed_oplines_stack).top = offset;
2479
934
  return opline;
2480
934
}
2481
/* }}} */
2482
2483
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2484
7.87k
{
2485
7.87k
  switch (ast_kind) {
2486
3
    case ZEND_AST_DIM:
2487
471
    case ZEND_AST_PROP:
2488
935
    case ZEND_AST_NULLSAFE_PROP:
2489
935
    case ZEND_AST_STATIC_PROP:
2490
995
    case ZEND_AST_METHOD_CALL:
2491
995
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2492
995
    case ZEND_AST_STATIC_CALL:
2493
995
      return 1;
2494
6.88k
    default:
2495
6.88k
      return 0;
2496
7.87k
  }
2497
7.87k
}
2498
2499
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2500
727
{
2501
727
  switch (ast->kind) {
2502
17
    case ZEND_AST_DIM:
2503
17
    case ZEND_AST_PROP:
2504
17
    case ZEND_AST_STATIC_PROP:
2505
17
    case ZEND_AST_METHOD_CALL:
2506
17
    case ZEND_AST_STATIC_CALL:
2507
17
      return zend_ast_is_short_circuited(ast->child[0]);
2508
0
    case ZEND_AST_NULLSAFE_PROP:
2509
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2510
0
      return 1;
2511
710
    default:
2512
710
      return 0;
2513
727
  }
2514
727
}
2515
2516
static void zend_assert_not_short_circuited(const zend_ast *ast)
2517
3
{
2518
3
  if (zend_ast_is_short_circuited(ast)) {
2519
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2520
0
  }
2521
3
}
2522
2523
/* Mark nodes that are an inner part of a short-circuiting chain.
2524
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2525
 * We do this to avoid passing down an argument in various compile functions. */
2526
2527
995
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2528
2529
523
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2530
523
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2531
3
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2532
3
  }
2533
523
}
2534
2535
static uint32_t zend_short_circuiting_checkpoint(void)
2536
7.39k
{
2537
7.39k
  return zend_stack_count(&CG(short_circuiting_opnums));
2538
7.39k
}
2539
2540
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2541
7.35k
{
2542
7.35k
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2543
6.36k
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2544
7.35k
  if (!is_short_circuited) {
2545
6.36k
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2546
6.36k
      && "Short circuiting stack should be empty");
2547
6.36k
    return;
2548
6.36k
  }
2549
2550
992
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2551
    /* Outer-most node will commit. */
2552
0
    return;
2553
0
  }
2554
2555
1.22k
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2556
232
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2557
232
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2558
232
    opline->op2.opline_num = get_next_op_number();
2559
232
    SET_NODE(opline->result, result);
2560
232
    opline->extended_value |=
2561
232
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2562
232
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2563
232
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2564
232
    zend_stack_del_top(&CG(short_circuiting_opnums));
2565
232
  }
2566
992
}
2567
2568
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2569
232
{
2570
232
  uint32_t jmp_null_opnum = get_next_op_number();
2571
232
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2572
232
  if (opline->op1_type == IS_CONST) {
2573
0
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2574
0
  }
2575
232
  if (bp_type == BP_VAR_IS) {
2576
0
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2577
0
  }
2578
232
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2579
232
}
2580
2581
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2582
0
{
2583
0
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2584
0
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2585
0
    znode memoized_result;
2586
2587
    /* Go through normal compilation */
2588
0
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2589
0
    zend_compile_expr(result, expr);
2590
0
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2591
2592
0
    if (result->op_type == IS_VAR) {
2593
0
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2594
0
    } else if (result->op_type == IS_TMP_VAR) {
2595
0
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
0
    } else {
2597
0
      if (result->op_type == IS_CONST) {
2598
0
        Z_TRY_ADDREF(result->u.constant);
2599
0
      }
2600
0
      memoized_result = *result;
2601
0
    }
2602
2603
0
    zend_hash_index_update_mem(
2604
0
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2605
0
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2606
0
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2607
0
    *result = *memoized_result;
2608
0
    if (result->op_type == IS_CONST) {
2609
0
      Z_TRY_ADDREF(result->u.constant);
2610
0
    }
2611
0
  } else {
2612
0
    ZEND_UNREACHABLE();
2613
0
  }
2614
0
}
2615
/* }}} */
2616
2617
static void zend_emit_return_type_check(
2618
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2619
2
{
2620
2
  zend_type type = return_info->type;
2621
2
  if (ZEND_TYPE_IS_SET(type)) {
2622
2
    zend_op *opline;
2623
2624
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2625
2
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2626
0
      if (expr) {
2627
0
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2628
0
          zend_error_noreturn(E_COMPILE_ERROR,
2629
0
            "A void %s must not return a value "
2630
0
            "(did you mean \"return;\" instead of \"return null;\"?)",
2631
0
            CG(active_class_entry) != NULL ? "method" : "function");
2632
0
        } else {
2633
0
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2634
0
          CG(active_class_entry) != NULL ? "method" : "function");
2635
0
        }
2636
0
      }
2637
      /* we don't need run-time check */
2638
0
      return;
2639
0
    }
2640
2641
    /* `return` is illegal in a never-returning function */
2642
2
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2643
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2644
0
      ZEND_ASSERT(!implicit);
2645
0
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2646
0
        CG(active_class_entry) != NULL ? "method" : "function");
2647
0
      return;
2648
0
    }
2649
2650
2
    if (!expr && !implicit) {
2651
0
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2652
0
        zend_error_noreturn(E_COMPILE_ERROR,
2653
0
          "A %s with return type must return a value "
2654
0
          "(did you mean \"return null;\" instead of \"return;\"?)",
2655
0
          CG(active_class_entry) != NULL ? "method" : "function");
2656
0
      } else {
2657
0
        zend_error_noreturn(E_COMPILE_ERROR,
2658
0
          "A %s with return type must return a value",
2659
0
          CG(active_class_entry) != NULL ? "method" : "function");
2660
0
      }
2661
0
    }
2662
2663
2
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2664
      /* we don't need run-time check for mixed return type */
2665
1
      return;
2666
1
    }
2667
2668
1
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2669
      /* we don't need run-time check */
2670
0
      return;
2671
0
    }
2672
2673
1
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2674
1
    if (expr && expr->op_type == IS_CONST) {
2675
0
      opline->result_type = expr->op_type = IS_TMP_VAR;
2676
0
      opline->result.var = expr->u.op.var = get_temporary_variable();
2677
0
    }
2678
1
  }
2679
2
}
2680
/* }}} */
2681
2682
void zend_emit_final_return(bool return_one) /* {{{ */
2683
88
{
2684
88
  znode zn;
2685
88
  zend_op *ret;
2686
88
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2687
2688
88
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2689
1
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2690
1
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2691
2692
1
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2693
0
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2694
0
      return;
2695
0
    }
2696
2697
1
    zend_emit_return_type_check(NULL, return_info, true);
2698
1
  }
2699
2700
88
  zn.op_type = IS_CONST;
2701
88
  if (return_one) {
2702
61
    ZVAL_LONG(&zn.u.constant, 1);
2703
61
  } else {
2704
27
    ZVAL_NULL(&zn.u.constant);
2705
27
  }
2706
2707
88
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2708
88
  ret->extended_value = -1;
2709
88
}
2710
/* }}} */
2711
2712
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2713
275
{
2714
275
  return ast->kind == ZEND_AST_VAR
2715
77
    || ast->kind == ZEND_AST_DIM
2716
77
    || ast->kind == ZEND_AST_PROP
2717
77
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2718
77
    || ast->kind == ZEND_AST_STATIC_PROP;
2719
275
}
2720
/* }}} */
2721
2722
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2723
331
{
2724
331
  return ast->kind == ZEND_AST_CALL
2725
299
    || ast->kind == ZEND_AST_METHOD_CALL
2726
271
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2727
271
    || ast->kind == ZEND_AST_STATIC_CALL
2728
271
    || ast->kind == ZEND_AST_PIPE;
2729
331
}
2730
/* }}} */
2731
2732
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2733
12
{
2734
12
  return zend_is_variable(ast) || zend_is_call(ast);
2735
12
}
2736
/* }}} */
2737
2738
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2739
0
{
2740
0
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2741
0
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2742
0
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2743
0
}
2744
/* }}} */
2745
2746
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2747
12
{
2748
12
  while (
2749
12
    ast->kind == ZEND_AST_DIM
2750
12
    || ast->kind == ZEND_AST_PROP
2751
12
  ) {
2752
0
    ast = ast->child[0];
2753
0
  }
2754
2755
12
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2756
12
}
2757
/* }}} */
2758
2759
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2760
77
{
2761
77
  if (name_ast->kind != ZEND_AST_ZVAL) {
2762
0
    return 0;
2763
0
  }
2764
2765
77
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2766
77
}
2767
/* }}} */
2768
2769
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2770
2
{
2771
2
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2772
1
    zend_ulong index;
2773
2774
1
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2775
0
      zval_ptr_dtor(&node->u.constant);
2776
0
      ZVAL_LONG(&node->u.constant, index);
2777
0
    }
2778
1
  }
2779
2
}
2780
/* }}} */
2781
2782
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2783
0
{
2784
0
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2785
0
    zend_ulong index;
2786
2787
0
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2788
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2789
       * See bug #63217
2790
       */
2791
0
      int c = zend_add_literal(&dim_node->u.constant);
2792
0
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2793
0
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2794
0
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2795
0
      return;
2796
0
    }
2797
0
  }
2798
0
}
2799
/* }}} */
2800
2801
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2802
159
{
2803
159
  if (class_node->op_type == IS_CONST) {
2804
159
    opline->op1_type = IS_CONST;
2805
159
    opline->op1.constant = zend_add_class_name_literal(
2806
159
      Z_STR(class_node->u.constant));
2807
159
  } else {
2808
0
    SET_NODE(opline->op1, class_node);
2809
0
  }
2810
159
}
2811
/* }}} */
2812
2813
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2814
159
{
2815
159
  uint32_t fetch_type;
2816
2817
159
  if (name_ast->kind != ZEND_AST_ZVAL) {
2818
0
    znode name_node;
2819
2820
0
    zend_compile_expr(&name_node, name_ast);
2821
2822
0
    if (name_node.op_type == IS_CONST) {
2823
0
      zend_string *name;
2824
2825
0
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2826
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2827
0
      }
2828
2829
0
      name = Z_STR(name_node.u.constant);
2830
0
      fetch_type = zend_get_class_fetch_type(name);
2831
2832
0
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2833
0
        result->op_type = IS_CONST;
2834
0
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2835
0
      } else {
2836
0
        zend_ensure_valid_class_fetch_type(fetch_type);
2837
0
        result->op_type = IS_UNUSED;
2838
0
        result->u.op.num = fetch_type | fetch_flags;
2839
0
      }
2840
2841
0
      zend_string_release_ex(name, 0);
2842
0
    } else {
2843
0
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2844
0
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2845
0
    }
2846
0
    return;
2847
0
  }
2848
2849
  /* Fully qualified names are always default refs */
2850
159
  if (name_ast->attr == ZEND_NAME_FQ) {
2851
0
    result->op_type = IS_CONST;
2852
0
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2853
0
    return;
2854
0
  }
2855
2856
159
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2857
159
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2858
159
    result->op_type = IS_CONST;
2859
159
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2860
159
  } else {
2861
0
    zend_ensure_valid_class_fetch_type(fetch_type);
2862
0
    result->op_type = IS_UNUSED;
2863
0
    result->u.op.num = fetch_type | fetch_flags;
2864
0
  }
2865
159
}
2866
/* }}} */
2867
2868
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2869
2.10k
{
2870
2.10k
  zend_ast *name_ast = ast->child[0];
2871
2.10k
  if (name_ast->kind == ZEND_AST_ZVAL) {
2872
2.10k
    zval *zv = zend_ast_get_zval(name_ast);
2873
2.10k
    zend_string *name;
2874
2875
2.10k
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2876
2.10k
      name = zval_make_interned_string(zv);
2877
2.10k
    } else {
2878
0
      name = zend_new_interned_string(zval_get_string_func(zv));
2879
0
    }
2880
2881
2.10k
    if (zend_is_auto_global(name)) {
2882
0
      return FAILURE;
2883
0
    }
2884
2885
2.10k
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2886
0
      if (type == BP_VAR_R) {
2887
0
        zend_error(E_DEPRECATED,
2888
0
          "The predefined locally scoped $http_response_header variable is deprecated,"
2889
0
          " call http_get_last_response_headers() instead");
2890
0
      } else if (type == BP_VAR_W) {
2891
0
        CG(context).has_assigned_to_http_response_header = true;
2892
0
      }
2893
0
    }
2894
2895
2.10k
    result->op_type = IS_CV;
2896
2.10k
    result->u.op.var = lookup_cv(name);
2897
2898
2.10k
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2899
0
      zend_string_release_ex(name, 0);
2900
0
    }
2901
2902
2.10k
    return SUCCESS;
2903
2.10k
  }
2904
2905
1
  return FAILURE;
2906
2.10k
}
2907
/* }}} */
2908
2909
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2910
1
{
2911
1
  zend_ast *name_ast = ast->child[0];
2912
1
  znode name_node;
2913
1
  zend_op *opline;
2914
2915
1
  zend_compile_expr(&name_node, name_ast);
2916
1
  if (name_node.op_type == IS_CONST) {
2917
0
    convert_to_string(&name_node.u.constant);
2918
0
  }
2919
2920
1
  if (delayed) {
2921
1
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2922
1
  } else {
2923
0
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2924
0
  }
2925
2926
1
  if (name_node.op_type == IS_CONST &&
2927
0
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2928
2929
0
    opline->extended_value = ZEND_FETCH_GLOBAL;
2930
1
  } else {
2931
    // TODO: Have a test case for this?
2932
1
    if (name_node.op_type == IS_CONST
2933
0
      && type == BP_VAR_R
2934
0
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2935
0
      zend_error(E_DEPRECATED,
2936
0
        "The predefined locally scoped $http_response_header variable is deprecated,"
2937
0
        " call http_get_last_response_headers() instead");
2938
0
    }
2939
1
    opline->extended_value = ZEND_FETCH_LOCAL;
2940
1
  }
2941
2942
1
  zend_adjust_for_fetch_type(opline, result, type);
2943
1
  return opline;
2944
1
}
2945
/* }}} */
2946
2947
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2948
2.99k
{
2949
2.99k
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2950
2.96k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2951
2.96k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2952
2.96k
  }
2953
2954
30
  return 0;
2955
2.99k
}
2956
/* }}} */
2957
2958
static bool is_globals_fetch(const zend_ast *ast)
2959
2.88k
{
2960
2.88k
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2961
2.78k
    const zval *name = zend_ast_get_zval(ast->child[0]);
2962
2.78k
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2963
2.78k
  }
2964
2965
99
  return 0;
2966
2.88k
}
2967
2968
static bool is_global_var_fetch(const zend_ast *ast)
2969
465
{
2970
465
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2971
465
}
2972
2973
static bool this_guaranteed_exists(void) /* {{{ */
2974
1
{
2975
1
  const zend_oparray_context *ctx = &CG(context);
2976
1
  while (ctx) {
2977
    /* Instance methods always have a $this.
2978
     * This also includes closures that have a scope and use $this. */
2979
1
    const zend_op_array *op_array = ctx->op_array;
2980
1
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2981
0
      return false;
2982
1
    } else if (op_array->scope) {
2983
1
      return true;
2984
1
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2985
0
      return false;
2986
0
    }
2987
0
    ctx = ctx->prev;
2988
0
  }
2989
0
  return false;
2990
1
}
2991
/* }}} */
2992
2993
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2994
2.07k
{
2995
2.07k
  if (is_this_fetch(ast)) {
2996
1
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2997
1
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
2998
1
      opline->result_type = IS_TMP_VAR;
2999
1
      result->op_type = IS_TMP_VAR;
3000
1
    }
3001
1
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3002
1
    return opline;
3003
2.07k
  } else if (is_globals_fetch(ast)) {
3004
0
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3005
0
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3006
0
      opline->result_type = IS_TMP_VAR;
3007
0
      result->op_type = IS_TMP_VAR;
3008
0
    }
3009
0
    return opline;
3010
2.07k
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3011
1
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3012
1
  }
3013
2.07k
  return NULL;
3014
2.07k
}
3015
/* }}} */
3016
3017
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3018
479
{
3019
479
  if (type != BP_VAR_R
3020
14
   && type != BP_VAR_IS
3021
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3022
14
   && type != BP_VAR_FUNC_ARG
3023
14
   && zend_is_call(ast)) {
3024
0
    if (node->op_type == IS_VAR) {
3025
0
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3026
0
      opline->result_type = IS_VAR;
3027
0
      opline->result.var = opline->op1.var;
3028
0
    } else {
3029
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3030
0
    }
3031
0
  }
3032
479
}
3033
/* }}} */
3034
3035
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3036
1
{
3037
1
  znode dummy_node;
3038
1
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3039
1
    zend_ast_create_znode(value_node));
3040
1
  zend_compile_expr(&dummy_node, assign_ast);
3041
1
  zend_do_free(&dummy_node);
3042
1
}
3043
/* }}} */
3044
3045
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3046
17
{
3047
17
  zend_ast *var_ast = ast->child[0];
3048
17
  zend_ast *dim_ast = ast->child[1];
3049
17
  zend_op *opline;
3050
3051
17
  znode var_node, dim_node;
3052
3053
17
  if (is_globals_fetch(var_ast)) {
3054
3
    if (dim_ast == NULL) {
3055
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3056
0
    }
3057
3058
3
    zend_compile_expr(&dim_node, dim_ast);
3059
3
    if (dim_node.op_type == IS_CONST) {
3060
3
      convert_to_string(&dim_node.u.constant);
3061
3
    }
3062
3063
3
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3064
3
    opline->extended_value = ZEND_FETCH_GLOBAL;
3065
3
    zend_adjust_for_fetch_type(opline, result, type);
3066
3
    return opline;
3067
14
  } else {
3068
14
    zend_short_circuiting_mark_inner(var_ast);
3069
14
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3070
14
    if (opline) {
3071
3
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3072
0
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3073
3
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3074
3
          || opline->opcode == ZEND_FETCH_DIM_RW
3075
3
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3076
3
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3077
0
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3078
0
      }
3079
3
    }
3080
14
  }
3081
3082
14
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3083
3084
14
  if (dim_ast == NULL) {
3085
3
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3086
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3087
0
    }
3088
3
    if (type == BP_VAR_UNSET) {
3089
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3090
0
    }
3091
3
    dim_node.op_type = IS_UNUSED;
3092
11
  } else {
3093
11
    zend_compile_expr(&dim_node, dim_ast);
3094
11
  }
3095
3096
14
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3097
14
  zend_adjust_for_fetch_type(opline, result, type);
3098
14
  if (by_ref) {
3099
0
    opline->extended_value = ZEND_FETCH_DIM_REF;
3100
0
  }
3101
3102
14
  if (dim_node.op_type == IS_CONST) {
3103
0
    zend_handle_numeric_dim(opline, &dim_node);
3104
0
  }
3105
14
  return opline;
3106
14
}
3107
3108
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3109
0
{
3110
0
  uint32_t offset = zend_delayed_compile_begin();
3111
0
  zend_delayed_compile_dim(result, ast, type, by_ref);
3112
0
  return zend_delayed_compile_end(offset);
3113
0
}
3114
/* }}} */
3115
3116
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3117
466
{
3118
466
  zend_ast *obj_ast = ast->child[0];
3119
466
  zend_ast *prop_ast = ast->child[1];
3120
3121
466
  znode obj_node, prop_node;
3122
466
  zend_op *opline;
3123
466
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3124
3125
466
  if (is_this_fetch(obj_ast)) {
3126
1
    if (this_guaranteed_exists()) {
3127
1
      obj_node.op_type = IS_UNUSED;
3128
1
    } else {
3129
0
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3130
0
    }
3131
1
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3132
3133
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3134
     * check for a nullsafe access. */
3135
465
  } else {
3136
465
    zend_short_circuiting_mark_inner(obj_ast);
3137
465
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3138
465
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3139
0
        || opline->opcode == ZEND_FETCH_DIM_RW
3140
0
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3141
0
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3142
0
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3143
0
    }
3144
3145
465
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3146
465
    if (nullsafe) {
3147
232
      if (obj_node.op_type == IS_TMP_VAR) {
3148
        /* Flush delayed oplines */
3149
0
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3150
0
        uint32_t var = obj_node.u.op.var;
3151
0
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3152
0
        uint32_t i = count;
3153
3154
0
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3155
0
          i--;
3156
0
          if (oplines[i].op1_type == IS_TMP_VAR) {
3157
0
            var = oplines[i].op1.var;
3158
0
          } else {
3159
0
            break;
3160
0
          }
3161
0
        }
3162
0
        for (; i < count; ++i) {
3163
0
          if (oplines[i].opcode != ZEND_NOP) {
3164
0
            opline = get_next_op();
3165
0
            memcpy(opline, &oplines[i], sizeof(zend_op));
3166
0
            oplines[i].opcode = ZEND_NOP;
3167
0
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3168
0
          }
3169
0
        }
3170
0
      }
3171
232
      zend_emit_jmp_null(&obj_node, type);
3172
232
    }
3173
465
  }
3174
3175
466
  zend_compile_expr(&prop_node, prop_ast);
3176
3177
466
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3178
466
  if (opline->op2_type == IS_CONST) {
3179
466
    convert_to_string(CT_CONSTANT(opline->op2));
3180
466
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3181
466
    opline->extended_value = zend_alloc_cache_slots(3);
3182
466
  }
3183
3184
466
  zend_adjust_for_fetch_type(opline, result, type);
3185
3186
466
  return opline;
3187
466
}
3188
/* }}} */
3189
3190
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3191
466
{
3192
466
  uint32_t offset = zend_delayed_compile_begin();
3193
466
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3194
466
  if (by_ref) { /* shared with cache_slot */
3195
0
    opline->extended_value |= ZEND_FETCH_REF;
3196
0
  }
3197
466
  return zend_delayed_compile_end(offset);
3198
466
}
3199
/* }}} */
3200
3201
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3202
0
{
3203
0
  zend_ast *class_ast = ast->child[0];
3204
0
  zend_ast *prop_ast = ast->child[1];
3205
3206
0
  znode class_node, prop_node;
3207
0
  zend_op *opline;
3208
3209
0
  zend_short_circuiting_mark_inner(class_ast);
3210
0
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3211
3212
0
  zend_compile_expr(&prop_node, prop_ast);
3213
3214
0
  if (delayed) {
3215
0
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3216
0
  } else {
3217
0
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3218
0
  }
3219
0
  if (opline->op1_type == IS_CONST) {
3220
0
    convert_to_string(CT_CONSTANT(opline->op1));
3221
0
    opline->extended_value = zend_alloc_cache_slots(3);
3222
0
  }
3223
0
  if (class_node.op_type == IS_CONST) {
3224
0
    opline->op2_type = IS_CONST;
3225
0
    opline->op2.constant = zend_add_class_name_literal(
3226
0
      Z_STR(class_node.u.constant));
3227
0
    if (opline->op1_type != IS_CONST) {
3228
0
      opline->extended_value = zend_alloc_cache_slot();
3229
0
    }
3230
0
  } else {
3231
0
    SET_NODE(opline->op2, &class_node);
3232
0
  }
3233
3234
0
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3235
0
    opline->extended_value |= ZEND_FETCH_REF;
3236
0
  }
3237
3238
0
  zend_adjust_for_fetch_type(opline, result, type);
3239
0
  return opline;
3240
0
}
3241
/* }}} */
3242
3243
0
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3244
0
  if (var_ast->kind == ZEND_AST_ARRAY) {
3245
0
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3246
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3247
0
    }
3248
0
    if (array_style != var_ast->attr) {
3249
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3250
0
    }
3251
0
  } else if (!zend_can_write_to_variable(var_ast)) {
3252
0
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3253
0
  }
3254
0
}
3255
/* }}} */
3256
3257
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3258
3259
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3260
0
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3261
0
  const zend_ast_list *list = zend_ast_get_list(ast);
3262
0
  bool has_refs = false;
3263
0
  uint32_t i;
3264
3265
0
  for (i = 0; i < list->children; ++i) {
3266
0
    zend_ast *elem_ast = list->child[i];
3267
3268
0
    if (elem_ast) {
3269
0
      zend_ast *var_ast = elem_ast->child[0];
3270
0
      if (var_ast->kind == ZEND_AST_ARRAY) {
3271
0
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3272
0
      }
3273
0
      has_refs |= elem_ast->attr;
3274
0
    }
3275
0
  }
3276
3277
0
  return has_refs;
3278
0
}
3279
/* }}} */
3280
3281
static bool list_is_keyed(const zend_ast_list *list)
3282
0
{
3283
0
  for (uint32_t i = 0; i < list->children; i++) {
3284
0
    const zend_ast *child = list->child[i];
3285
0
    if (child) {
3286
0
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3287
0
    }
3288
0
  }
3289
0
  return false;
3290
0
}
3291
3292
static void zend_compile_list_assign(
3293
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3294
0
{
3295
0
  zend_ast_list *list = zend_ast_get_list(ast);
3296
0
  uint32_t i;
3297
0
  bool has_elems = false;
3298
0
  bool is_keyed = list_is_keyed(list);
3299
3300
0
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3301
0
    zval_make_interned_string(&expr_node->u.constant);
3302
0
  }
3303
3304
0
  for (i = 0; i < list->children; ++i) {
3305
0
    zend_ast *elem_ast = list->child[i];
3306
0
    zend_ast *var_ast, *key_ast;
3307
0
    znode fetch_result, dim_node;
3308
0
    zend_op *opline;
3309
3310
0
    if (elem_ast == NULL) {
3311
0
      if (is_keyed) {
3312
0
        zend_error(E_COMPILE_ERROR,
3313
0
          "Cannot use empty array entries in keyed array assignment");
3314
0
      } else {
3315
0
        continue;
3316
0
      }
3317
0
    }
3318
3319
0
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3320
0
      zend_error(E_COMPILE_ERROR,
3321
0
          "Spread operator is not supported in assignments");
3322
0
    }
3323
3324
0
    var_ast = elem_ast->child[0];
3325
0
    key_ast = elem_ast->child[1];
3326
0
    has_elems = true;
3327
3328
0
    if (is_keyed) {
3329
0
      if (key_ast == NULL) {
3330
0
        zend_error(E_COMPILE_ERROR,
3331
0
          "Cannot mix keyed and unkeyed array entries in assignments");
3332
0
      }
3333
3334
0
      zend_compile_expr(&dim_node, key_ast);
3335
0
    } else {
3336
0
      if (key_ast != NULL) {
3337
0
        zend_error(E_COMPILE_ERROR,
3338
0
          "Cannot mix keyed and unkeyed array entries in assignments");
3339
0
      }
3340
3341
0
      dim_node.op_type = IS_CONST;
3342
0
      ZVAL_LONG(&dim_node.u.constant, i);
3343
0
    }
3344
3345
0
    if (expr_node->op_type == IS_CONST) {
3346
0
      Z_TRY_ADDREF(expr_node->u.constant);
3347
0
    }
3348
3349
0
    zend_verify_list_assign_target(var_ast, array_style);
3350
3351
0
    opline = zend_emit_op(&fetch_result,
3352
0
      elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node);
3353
3354
0
    if (dim_node.op_type == IS_CONST) {
3355
0
      zend_handle_numeric_dim(opline, &dim_node);
3356
0
    }
3357
3358
0
    if (elem_ast->attr) {
3359
0
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3360
0
    }
3361
0
    if (var_ast->kind == ZEND_AST_ARRAY) {
3362
0
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3363
0
    } else if (elem_ast->attr) {
3364
0
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3365
0
    } else {
3366
0
      zend_emit_assign_znode(var_ast, &fetch_result);
3367
0
    }
3368
0
  }
3369
3370
0
  if (has_elems == 0) {
3371
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3372
0
  }
3373
3374
0
  if (result) {
3375
0
    *result = *expr_node;
3376
0
  } else {
3377
0
    zend_do_free(expr_node);
3378
0
  }
3379
0
}
3380
/* }}} */
3381
3382
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3383
521
{
3384
521
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3385
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3386
0
  }
3387
521
  if (
3388
521
    ast->kind == ZEND_AST_METHOD_CALL
3389
521
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3390
521
    || ast->kind == ZEND_AST_STATIC_CALL
3391
521
  ) {
3392
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3393
0
  }
3394
521
  if (zend_ast_is_short_circuited(ast)) {
3395
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3396
0
  }
3397
521
  if (is_globals_fetch(ast)) {
3398
0
    zend_error_noreturn(E_COMPILE_ERROR,
3399
0
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3400
0
  }
3401
521
}
3402
/* }}} */
3403
3404
/* Detects $a... = $a pattern */
3405
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3406
14
{
3407
14
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3408
14
    return 0;
3409
14
  }
3410
3411
0
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3412
0
    var_ast = var_ast->child[0];
3413
0
  }
3414
3415
0
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3416
0
    return 0;
3417
0
  }
3418
3419
0
  {
3420
0
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3421
0
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3422
0
    bool result = zend_string_equals(name1, name2);
3423
0
    zend_string_release_ex(name1, 0);
3424
0
    zend_string_release_ex(name2, 0);
3425
0
    return result;
3426
0
  }
3427
0
}
3428
/* }}} */
3429
3430
static void zend_compile_expr_with_potential_assign_to_self(
3431
14
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3432
14
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3433
    /* $a[0] = $a should evaluate the right $a first */
3434
0
    znode cv_node;
3435
3436
0
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3437
0
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3438
0
    } else {
3439
0
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3440
0
    }
3441
14
  } else {
3442
14
    zend_compile_expr(expr_node, expr_ast);
3443
14
  }
3444
14
}
3445
3446
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3447
380
{
3448
380
  zend_ast *var_ast = ast->child[0];
3449
380
  zend_ast *expr_ast = ast->child[1];
3450
3451
380
  znode var_node, expr_node;
3452
380
  zend_op *opline;
3453
380
  uint32_t offset;
3454
380
  if (is_this_fetch(var_ast)) {
3455
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3456
0
  }
3457
3458
380
  zend_ensure_writable_variable(var_ast);
3459
3460
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3461
380
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3462
380
  switch (kind) {
3463
366
    case ZEND_AST_VAR:
3464
366
      offset = zend_delayed_compile_begin();
3465
366
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3466
366
      zend_compile_expr(&expr_node, expr_ast);
3467
366
      zend_delayed_compile_end(offset);
3468
366
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3469
366
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3470
366
      return;
3471
0
    case ZEND_AST_STATIC_PROP:
3472
0
      offset = zend_delayed_compile_begin();
3473
0
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3474
0
      zend_compile_expr(&expr_node, expr_ast);
3475
3476
0
      opline = zend_delayed_compile_end(offset);
3477
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3478
0
      opline->result_type = IS_TMP_VAR;
3479
0
      result->op_type = IS_TMP_VAR;
3480
3481
0
      zend_emit_op_data(&expr_node);
3482
0
      return;
3483
14
    case ZEND_AST_DIM:
3484
14
      offset = zend_delayed_compile_begin();
3485
14
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3486
14
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3487
3488
14
      opline = zend_delayed_compile_end(offset);
3489
14
      opline->opcode = ZEND_ASSIGN_DIM;
3490
14
      opline->result_type = IS_TMP_VAR;
3491
14
      result->op_type = IS_TMP_VAR;
3492
3493
14
      opline = zend_emit_op_data(&expr_node);
3494
14
      return;
3495
0
    case ZEND_AST_PROP:
3496
0
    case ZEND_AST_NULLSAFE_PROP:
3497
0
      offset = zend_delayed_compile_begin();
3498
0
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3499
0
      zend_compile_expr(&expr_node, expr_ast);
3500
3501
0
      opline = zend_delayed_compile_end(offset);
3502
0
      opline->opcode = ZEND_ASSIGN_OBJ;
3503
0
      opline->result_type = IS_TMP_VAR;
3504
0
      result->op_type = IS_TMP_VAR;
3505
3506
0
      zend_emit_op_data(&expr_node);
3507
0
      return;
3508
0
    case ZEND_AST_ARRAY:
3509
0
      if (zend_propagate_list_refs(var_ast)) {
3510
0
        if (!zend_is_variable_or_call(expr_ast)) {
3511
0
          zend_error_noreturn(E_COMPILE_ERROR,
3512
0
            "Cannot assign reference to non referenceable value");
3513
0
        } else {
3514
0
          zend_assert_not_short_circuited(expr_ast);
3515
0
        }
3516
3517
0
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3518
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3519
         * self-assignments, this forces the RHS to evaluate first. */
3520
0
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3521
0
      } else {
3522
0
        if (expr_ast->kind == ZEND_AST_VAR) {
3523
          /* list($a, $b) = $a should evaluate the right $a first */
3524
0
          znode cv_node;
3525
3526
0
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3527
0
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3528
0
          } else {
3529
0
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3530
0
          }
3531
0
        } else {
3532
0
          zend_compile_expr(&expr_node, expr_ast);
3533
0
        }
3534
0
      }
3535
3536
0
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3537
0
      return;
3538
0
    EMPTY_SWITCH_DEFAULT_CASE();
3539
380
  }
3540
380
}
3541
/* }}} */
3542
3543
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3544
3
{
3545
3
  zend_ast *target_ast = ast->child[0];
3546
3
  zend_ast *source_ast = ast->child[1];
3547
3548
3
  znode target_node, source_node;
3549
3
  zend_op *opline;
3550
3
  uint32_t offset, flags;
3551
3552
3
  if (is_this_fetch(target_ast)) {
3553
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3554
0
  }
3555
3
  zend_ensure_writable_variable(target_ast);
3556
3
  zend_assert_not_short_circuited(source_ast);
3557
3
  if (is_globals_fetch(source_ast)) {
3558
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3559
0
  }
3560
3561
3
  offset = zend_delayed_compile_begin();
3562
3
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3563
3
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3564
3565
3
  if ((target_ast->kind != ZEND_AST_VAR
3566
3
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3567
0
   && source_ast->kind != ZEND_AST_ZNODE
3568
0
   && source_node.op_type != IS_CV) {
3569
    /* Both LHS and RHS expressions may modify the same data structure,
3570
     * and the modification during RHS evaluation may dangle the pointer
3571
     * to the result of the LHS evaluation.
3572
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3573
     * See: Bug #71539
3574
     */
3575
0
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3576
0
  }
3577
3578
3
  opline = zend_delayed_compile_end(offset);
3579
3580
3
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3581
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3582
0
  }
3583
3584
3
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3585
3586
3
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3587
0
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3588
0
    opline->extended_value &= ~ZEND_FETCH_REF;
3589
0
    opline->extended_value |= flags;
3590
0
    zend_emit_op_data(&source_node);
3591
0
    *result = target_node;
3592
3
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3593
0
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3594
0
    opline->extended_value &= ~ZEND_FETCH_REF;
3595
0
    opline->extended_value |= flags;
3596
0
    zend_emit_op_data(&source_node);
3597
0
    *result = target_node;
3598
3
  } else {
3599
3
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3600
3
    opline->extended_value = flags;
3601
3
  }
3602
3
}
3603
/* }}} */
3604
3605
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3606
0
{
3607
0
  znode dummy_node;
3608
0
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3609
0
    zend_ast_create_znode(value_node));
3610
0
  zend_compile_expr(&dummy_node, assign_ast);
3611
0
  zend_do_free(&dummy_node);
3612
0
}
3613
/* }}} */
3614
3615
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3616
85
{
3617
85
  zend_ast *var_ast = ast->child[0];
3618
85
  zend_ast *expr_ast = ast->child[1];
3619
85
  uint32_t opcode = ast->attr;
3620
3621
85
  znode var_node, expr_node;
3622
85
  zend_op *opline;
3623
85
  uint32_t offset, cache_slot;
3624
3625
85
  zend_ensure_writable_variable(var_ast);
3626
3627
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3628
85
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3629
85
  switch (kind) {
3630
85
    case ZEND_AST_VAR:
3631
85
      offset = zend_delayed_compile_begin();
3632
85
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3633
85
      zend_compile_expr(&expr_node, expr_ast);
3634
85
      zend_delayed_compile_end(offset);
3635
85
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3636
85
      opline->extended_value = opcode;
3637
85
      return;
3638
0
    case ZEND_AST_STATIC_PROP:
3639
0
      offset = zend_delayed_compile_begin();
3640
0
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3641
0
      zend_compile_expr(&expr_node, expr_ast);
3642
3643
0
      opline = zend_delayed_compile_end(offset);
3644
0
      cache_slot = opline->extended_value;
3645
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3646
0
      opline->extended_value = opcode;
3647
0
      opline->result_type = IS_TMP_VAR;
3648
0
      result->op_type = IS_TMP_VAR;
3649
3650
0
      opline = zend_emit_op_data(&expr_node);
3651
0
      opline->extended_value = cache_slot;
3652
0
      return;
3653
0
    case ZEND_AST_DIM:
3654
0
      offset = zend_delayed_compile_begin();
3655
0
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3656
0
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3657
3658
0
      opline = zend_delayed_compile_end(offset);
3659
0
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3660
0
      opline->extended_value = opcode;
3661
0
      opline->result_type = IS_TMP_VAR;
3662
0
      result->op_type = IS_TMP_VAR;
3663
3664
0
      zend_emit_op_data(&expr_node);
3665
0
      return;
3666
0
    case ZEND_AST_PROP:
3667
0
    case ZEND_AST_NULLSAFE_PROP:
3668
0
      offset = zend_delayed_compile_begin();
3669
0
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3670
0
      zend_compile_expr(&expr_node, expr_ast);
3671
3672
0
      opline = zend_delayed_compile_end(offset);
3673
0
      cache_slot = opline->extended_value;
3674
0
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3675
0
      opline->extended_value = opcode;
3676
0
      opline->result_type = IS_TMP_VAR;
3677
0
      result->op_type = IS_TMP_VAR;
3678
3679
0
      opline = zend_emit_op_data(&expr_node);
3680
0
      opline->extended_value = cache_slot;
3681
0
      return;
3682
85
    EMPTY_SWITCH_DEFAULT_CASE()
3683
85
  }
3684
85
}
3685
/* }}} */
3686
3687
0
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3688
  // TODO: Caching?
3689
0
  for (uint32_t i = 0; i < fn->common.num_args; i++) {
3690
0
    zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3691
0
    if (zend_string_equals(arg_info->name, arg_name)) {
3692
0
      return i + 1;
3693
0
    }
3694
0
  }
3695
3696
  /* Either an invalid argument name, or collected into a variadic argument. */
3697
0
  return (uint32_t) -1;
3698
0
}
3699
3700
static uint32_t zend_compile_args(
3701
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3702
327
{
3703
327
  const zend_ast_list *args = zend_ast_get_list(ast);
3704
327
  uint32_t i;
3705
327
  bool uses_arg_unpack = false;
3706
327
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3707
3708
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3709
   * May not actually use named argument passing. */
3710
327
  bool uses_named_args = false;
3711
  /* Whether there may be any undef arguments due to the use of named arguments. */
3712
327
  bool may_have_undef = false;
3713
  /* Whether there may be any extra named arguments collected into a variadic. */
3714
327
  *may_have_extra_named_args = false;
3715
3716
638
  for (i = 0; i < args->children; ++i) {
3717
311
    zend_ast *arg = args->child[i];
3718
311
    zend_string *arg_name = NULL;
3719
311
    uint32_t arg_num = i + 1;
3720
3721
311
    znode arg_node;
3722
311
    zend_op *opline;
3723
311
    uint8_t opcode;
3724
3725
311
    if (arg->kind == ZEND_AST_UNPACK) {
3726
0
      if (uses_named_args) {
3727
0
        zend_error_noreturn(E_COMPILE_ERROR,
3728
0
          "Cannot use argument unpacking after named arguments");
3729
0
      }
3730
3731
      /* Unpack may contain named arguments. */
3732
0
      may_have_undef = true;
3733
0
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3734
0
        *may_have_extra_named_args = true;
3735
0
      }
3736
3737
0
      uses_arg_unpack = true;
3738
0
      fbc = NULL;
3739
3740
0
      zend_compile_expr(&arg_node, arg->child[0]);
3741
0
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3742
0
      opline->op2.num = arg_count;
3743
0
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3744
3745
0
      continue;
3746
0
    }
3747
3748
311
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3749
0
      uses_named_args = true;
3750
0
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3751
0
      arg = arg->child[1];
3752
3753
0
      if (fbc && !uses_arg_unpack) {
3754
0
        arg_num = zend_get_arg_num(fbc, arg_name);
3755
0
        if (arg_num == arg_count + 1 && !may_have_undef) {
3756
          /* Using named arguments, but passing in order. */
3757
0
          arg_name = NULL;
3758
0
          arg_count++;
3759
0
        } else {
3760
          // TODO: We could track which arguments were passed, even if out of order.
3761
0
          may_have_undef = true;
3762
0
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3763
0
            *may_have_extra_named_args = true;
3764
0
          }
3765
0
        }
3766
0
      } else {
3767
0
        arg_num = (uint32_t) -1;
3768
0
        may_have_undef = true;
3769
0
        *may_have_extra_named_args = true;
3770
0
      }
3771
311
    } else {
3772
311
      if (uses_arg_unpack) {
3773
0
        zend_error_noreturn(E_COMPILE_ERROR,
3774
0
          "Cannot use positional argument after argument unpacking");
3775
0
      }
3776
3777
311
      if (uses_named_args) {
3778
0
        zend_error_noreturn(E_COMPILE_ERROR,
3779
0
          "Cannot use positional argument after named argument");
3780
0
      }
3781
3782
311
      arg_count++;
3783
311
    }
3784
3785
    /* Treat passing of $GLOBALS the same as passing a call.
3786
     * This will error at runtime if the argument is by-ref. */
3787
311
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3788
60
      zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3789
60
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3790
        /* Function call was converted into builtin instruction */
3791
0
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3792
0
          opcode = ZEND_SEND_VAL_EX;
3793
0
        } else {
3794
0
          opcode = ZEND_SEND_VAL;
3795
0
        }
3796
60
      } else {
3797
60
        if (fbc && arg_num != (uint32_t) -1) {
3798
56
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3799
0
            opcode = ZEND_SEND_VAR_NO_REF;
3800
56
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3801
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3802
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3803
             * and a by-value pass if it returned by-value. */
3804
0
            opcode = ZEND_SEND_VAL;
3805
56
          } else {
3806
56
            opcode = ZEND_SEND_VAR;
3807
56
          }
3808
56
        } else {
3809
4
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3810
4
        }
3811
60
      }
3812
251
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3813
174
      if (fbc && arg_num != (uint32_t) -1) {
3814
159
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3815
0
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3816
0
          opcode = ZEND_SEND_REF;
3817
159
        } else {
3818
159
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3819
159
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3820
159
        }
3821
159
      } else {
3822
15
        do {
3823
15
          if (arg->kind == ZEND_AST_VAR) {
3824
15
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3825
15
            if (is_this_fetch(arg)) {
3826
0
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3827
0
              opcode = ZEND_SEND_VAR_EX;
3828
0
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3829
0
              break;
3830
15
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3831
15
              opcode = ZEND_SEND_VAR_EX;
3832
15
              break;
3833
15
            }
3834
15
          }
3835
0
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3836
0
          if (arg_name) {
3837
0
            opline->op2_type = IS_CONST;
3838
0
            zend_string_addref(arg_name);
3839
0
            opline->op2.constant = zend_add_literal_string(&arg_name);
3840
0
            opline->result.num = zend_alloc_cache_slots(2);
3841
0
          } else {
3842
0
            opline->op2.num = arg_num;
3843
0
          }
3844
0
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3845
0
          opcode = ZEND_SEND_FUNC_ARG;
3846
0
        } while (0);
3847
15
      }
3848
174
    } else {
3849
77
      zend_compile_expr(&arg_node, arg);
3850
77
      if (arg_node.op_type == IS_VAR) {
3851
        /* pass ++$a or something similar */
3852
0
        if (fbc && arg_num != (uint32_t) -1) {
3853
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3854
0
            opcode = ZEND_SEND_VAR_NO_REF;
3855
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3856
0
            opcode = ZEND_SEND_VAL;
3857
0
          } else {
3858
0
            opcode = ZEND_SEND_VAR;
3859
0
          }
3860
0
        } else {
3861
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3862
0
        }
3863
77
      } else if (arg_node.op_type == IS_CV) {
3864
0
        if (fbc && arg_num != (uint32_t) -1) {
3865
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3866
0
            opcode = ZEND_SEND_REF;
3867
0
          } else {
3868
0
            opcode = ZEND_SEND_VAR;
3869
0
          }
3870
0
        } else {
3871
0
          opcode = ZEND_SEND_VAR_EX;
3872
0
        }
3873
77
      } else {
3874
        /* Delay "Only variables can be passed by reference" error to execution */
3875
77
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3876
65
          opcode = ZEND_SEND_VAL;
3877
65
        } else {
3878
12
          opcode = ZEND_SEND_VAL_EX;
3879
12
        }
3880
77
      }
3881
77
    }
3882
3883
311
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3884
311
    if (arg_name) {
3885
0
      opline->op2_type = IS_CONST;
3886
0
      zend_string_addref(arg_name);
3887
0
      opline->op2.constant = zend_add_literal_string(&arg_name);
3888
0
      opline->result.num = zend_alloc_cache_slots(2);
3889
311
    } else {
3890
311
      opline->op2.opline_num = arg_num;
3891
311
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3892
311
    }
3893
311
  }
3894
3895
327
  if (may_have_undef) {
3896
0
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3897
0
  }
3898
3899
327
  return arg_count;
3900
327
}
3901
/* }}} */
3902
3903
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3904
434
{
3905
434
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3906
3907
434
  if (fbc && init_op->opcode != ZEND_NEW) {
3908
214
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3909
214
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3910
194
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3911
0
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3912
0
          return ZEND_DO_ICALL;
3913
0
        } else {
3914
0
          return ZEND_DO_FCALL_BY_NAME;
3915
0
        }
3916
0
      }
3917
194
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3918
20
      if (zend_execute_ex == execute_ex) {
3919
0
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3920
0
          return ZEND_DO_UCALL;
3921
0
        } else {
3922
0
          return ZEND_DO_FCALL_BY_NAME;
3923
0
        }
3924
0
      }
3925
20
    }
3926
220
  } else if (zend_execute_ex == execute_ex &&
3927
14
             !zend_execute_internal &&
3928
0
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3929
0
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3930
0
    return ZEND_DO_FCALL_BY_NAME;
3931
0
  }
3932
434
  return ZEND_DO_FCALL;
3933
434
}
3934
/* }}} */
3935
3936
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno) /* {{{ */
3937
327
{
3938
327
  zend_op *opline;
3939
327
  uint32_t opnum_init = get_next_op_number() - 1;
3940
3941
327
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3942
0
    opline = &CG(active_op_array)->opcodes[opnum_init];
3943
0
    opline->extended_value = 0;
3944
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
3945
0
    uint8_t opcode = opline->opcode;
3946
3947
0
    if (opcode == ZEND_NEW) {
3948
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3949
0
    }
3950
3951
0
    zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args);
3952
0
    if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) {
3953
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders");
3954
0
    }
3955
3956
0
    if (opcode == ZEND_INIT_FCALL) {
3957
0
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3958
0
    }
3959
3960
0
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3961
0
    if (opcode == ZEND_INIT_FCALL
3962
0
     || opcode == ZEND_INIT_FCALL_BY_NAME
3963
0
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
3964
0
      callable_convert_op->extended_value = zend_alloc_cache_slot();
3965
0
    } else {
3966
0
      callable_convert_op->extended_value = (uint32_t)-1;
3967
0
    }
3968
0
    return true;
3969
0
  }
3970
3971
327
  bool may_have_extra_named_args;
3972
327
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3973
3974
327
  zend_do_extended_fcall_begin();
3975
3976
327
  opline = &CG(active_op_array)->opcodes[opnum_init];
3977
327
  opline->extended_value = arg_count;
3978
3979
327
  if (opline->opcode == ZEND_INIT_FCALL) {
3980
107
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3981
107
  }
3982
3983
327
  uint8_t call_op = zend_get_call_op(
3984
327
    opline,
3985
327
    fbc,
3986
    /* result_used: At this point we do not yet reliably
3987
     * know if the result is used. Deoptimize #[\NoDiscard]
3988
     * calls to be sure. The optimizer will fix this up.
3989
     */
3990
327
    false
3991
327
  );
3992
327
  opline = zend_emit_op(result, call_op, NULL, NULL);
3993
327
  if (may_have_extra_named_args) {
3994
0
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
3995
0
  }
3996
327
  opline->lineno = lineno;
3997
327
  zend_do_extended_fcall_end();
3998
327
  return false;
3999
327
}
4000
/* }}} */
4001
4002
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4003
149
{
4004
149
  zend_string *orig_name = zend_ast_get_str(name_ast);
4005
149
  bool is_fully_qualified;
4006
4007
149
  name_node->op_type = IS_CONST;
4008
149
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4009
149
    orig_name, name_ast->attr, &is_fully_qualified));
4010
4011
149
  return !is_fully_qualified && FC(current_namespace);
4012
149
}
4013
/* }}} */
4014
4015
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
4016
21
{
4017
21
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4018
16
    const char *colon;
4019
16
    zend_string *str = Z_STR(name_node->u.constant);
4020
16
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4021
0
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4022
0
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4023
0
      zend_op *opline = get_next_op();
4024
4025
0
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4026
0
      opline->op1_type = IS_CONST;
4027
0
      opline->op1.constant = zend_add_class_name_literal(class);
4028
0
      opline->op2_type = IS_CONST;
4029
0
      opline->op2.constant = zend_add_func_name_literal(method);
4030
      /* 2 slots, for class and method */
4031
0
      opline->result.num = zend_alloc_cache_slots(2);
4032
0
      zval_ptr_dtor(&name_node->u.constant);
4033
16
    } else {
4034
16
      zend_op *opline = get_next_op();
4035
4036
16
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4037
16
      opline->op2_type = IS_CONST;
4038
16
      opline->op2.constant = zend_add_func_name_literal(str);
4039
16
      opline->result.num = zend_alloc_cache_slot();
4040
16
    }
4041
16
  } else {
4042
5
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4043
5
  }
4044
4045
21
  zend_compile_call_common(result, args_ast, NULL, lineno);
4046
21
}
4047
/* }}} */
4048
4049
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4050
122
{
4051
122
  uint32_t i;
4052
234
  for (i = 0; i < args->children; ++i) {
4053
112
    const zend_ast *arg = args->child[i];
4054
112
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4055
0
      return 1;
4056
0
    }
4057
112
  }
4058
122
  return 0;
4059
122
}
4060
/* }}} */
4061
4062
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4063
0
{
4064
0
  znode arg_node;
4065
4066
0
  if (args->children != 1) {
4067
0
    return FAILURE;
4068
0
  }
4069
4070
0
  zend_compile_expr(&arg_node, args->child[0]);
4071
0
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4072
0
    result->op_type = IS_CONST;
4073
0
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4074
0
    zval_ptr_dtor_str(&arg_node.u.constant);
4075
0
  } else {
4076
0
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4077
0
  }
4078
0
  return SUCCESS;
4079
0
}
4080
/* }}} */
4081
4082
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4083
0
{
4084
0
  znode arg_node;
4085
0
  zend_op *opline;
4086
4087
0
  if (args->children != 1) {
4088
0
    return FAILURE;
4089
0
  }
4090
4091
0
  zend_compile_expr(&arg_node, args->child[0]);
4092
0
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4093
0
  if (type != _IS_BOOL) {
4094
0
    opline->extended_value = (1 << type);
4095
0
  } else {
4096
0
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4097
0
  }
4098
0
  return SUCCESS;
4099
0
}
4100
/* }}} */
4101
4102
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4103
0
{
4104
0
  znode arg_node;
4105
0
  zend_op *opline;
4106
4107
0
  if (args->children != 1) {
4108
0
    return FAILURE;
4109
0
  }
4110
4111
0
  zend_compile_expr(&arg_node, args->child[0]);
4112
0
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4113
0
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4114
0
  return SUCCESS;
4115
0
}
4116
4117
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4118
25
{
4119
25
  znode arg_node;
4120
25
  zend_op *opline;
4121
4122
25
  if (args->children != 1) {
4123
0
    return FAILURE;
4124
0
  }
4125
4126
25
  zend_compile_expr(&arg_node, args->child[0]);
4127
25
  if (type == _IS_BOOL) {
4128
0
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4129
25
  } else {
4130
25
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4131
25
    opline->extended_value = type;
4132
25
  }
4133
25
  return SUCCESS;
4134
25
}
4135
/* }}} */
4136
4137
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4138
0
{
4139
0
  zend_string *name;
4140
0
  zend_op *opline;
4141
4142
0
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4143
0
    return FAILURE;
4144
0
  }
4145
4146
0
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4147
0
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4148
0
    zend_string_release_ex(name, 0);
4149
0
    return FAILURE;
4150
0
  }
4151
4152
0
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4153
0
    zend_string_release_ex(name, 0);
4154
0
    zval_ptr_dtor(&result->u.constant);
4155
0
    ZVAL_TRUE(&result->u.constant);
4156
0
    result->op_type = IS_CONST;
4157
0
    return SUCCESS;
4158
0
  }
4159
4160
0
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4161
0
  opline->op1_type = IS_CONST;
4162
0
  LITERAL_STR(opline->op1, name);
4163
0
  opline->extended_value = zend_alloc_cache_slot();
4164
4165
0
  return SUCCESS;
4166
0
}
4167
/* }}} */
4168
4169
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4170
0
{
4171
0
  zval *zint;
4172
0
  if (
4173
0
    args->children == 1
4174
0
    && args->child[0]->kind == ZEND_AST_ZVAL
4175
0
    && (zint = zend_ast_get_zval(args->child[0]))
4176
0
    && Z_TYPE_P(zint) == IS_LONG
4177
0
    && Z_LVAL_P(zint) >= 0
4178
0
    && Z_LVAL_P(zint) <= 255
4179
0
  ) {
4180
0
    result->op_type = IS_CONST;
4181
0
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4182
0
    return SUCCESS;
4183
0
  } else {
4184
0
    return FAILURE;
4185
0
  }
4186
0
}
4187
/* }}} */
4188
4189
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4190
0
{
4191
0
  zval *str;
4192
0
  if (
4193
0
    args->children == 1
4194
0
    && args->child[0]->kind == ZEND_AST_ZVAL
4195
0
    && (str = zend_ast_get_zval(args->child[0]))
4196
0
    && Z_TYPE_P(str) == IS_STRING
4197
0
    && Z_STRLEN_P(str) == 1
4198
0
  ) {
4199
0
    result->op_type = IS_CONST;
4200
0
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4201
0
    return SUCCESS;
4202
0
  } else {
4203
0
    return FAILURE;
4204
0
  }
4205
0
}
4206
/* }}} */
4207
4208
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4209
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4210
 * directly or indirectly recursive function calls. */
4211
133
static bool fbc_is_finalized(const zend_function *fbc) {
4212
133
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4213
133
}
4214
4215
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4216
144
{
4217
144
  if (ce->type == ZEND_INTERNAL_CLASS) {
4218
122
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4219
122
  } else {
4220
22
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4221
22
      && ce->info.user.filename != filename;
4222
22
  }
4223
144
}
4224
4225
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4226
132
{
4227
132
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4228
122
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4229
122
  } else {
4230
10
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4231
10
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4232
10
        && fbc->op_array.filename != filename);
4233
10
  }
4234
132
}
4235
4236
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4237
0
{
4238
0
  zend_string *name, *lcname;
4239
0
  zend_function *fbc;
4240
0
  zend_op *opline;
4241
4242
0
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4243
0
    return FAILURE;
4244
0
  }
4245
4246
0
  name = zend_ast_get_str(name_ast);
4247
0
  lcname = zend_string_tolower(name);
4248
4249
0
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4250
0
  if (!fbc
4251
0
   || !fbc_is_finalized(fbc)
4252
0
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4253
0
    zend_string_release_ex(lcname, 0);
4254
0
    return FAILURE;
4255
0
  }
4256
4257
0
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4258
0
  opline->extended_value = num_args;
4259
0
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4260
0
  opline->op2_type = IS_CONST;
4261
0
  LITERAL_STR(opline->op2, lcname);
4262
0
  opline->result.num = zend_alloc_cache_slot();
4263
4264
0
  return SUCCESS;
4265
0
}
4266
/* }}} */
4267
4268
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4269
0
{
4270
0
  zend_op *opline;
4271
0
  znode name_node;
4272
4273
0
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4274
0
    return;
4275
0
  }
4276
4277
0
  zend_compile_expr(&name_node, name_ast);
4278
4279
0
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4280
0
  opline->op1_type = IS_CONST;
4281
0
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4282
0
  opline->extended_value = num_args;
4283
0
}
4284
/* }}} */
4285
4286
/* cufa = call_user_func_array */
4287
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4288
0
{
4289
0
  znode arg_node;
4290
0
  zend_op *opline;
4291
4292
0
  if (args->children != 2) {
4293
0
    return FAILURE;
4294
0
  }
4295
4296
0
  zend_compile_init_user_func(args->child[0], 0, lcname);
4297
0
  if (args->child[1]->kind == ZEND_AST_CALL
4298
0
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4299
0
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4300
0
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4301
0
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4302
0
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4303
0
    bool is_fully_qualified;
4304
0
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4305
4306
0
    if (zend_string_equals_literal_ci(name, "array_slice")
4307
0
       && !zend_args_contain_unpack_or_named(list)
4308
0
     && list->children == 3
4309
0
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4310
0
      zval *zv = zend_ast_get_zval(list->child[1]);
4311
4312
0
      if (Z_TYPE_P(zv) == IS_LONG
4313
0
       && Z_LVAL_P(zv) >= 0
4314
0
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4315
0
        zend_op *opline;
4316
0
        znode len_node;
4317
4318
0
        zend_compile_expr(&arg_node, list->child[0]);
4319
0
        zend_compile_expr(&len_node, list->child[2]);
4320
0
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4321
0
        opline->extended_value = Z_LVAL_P(zv);
4322
0
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4323
0
        zend_string_release_ex(name, 0);
4324
0
        return SUCCESS;
4325
0
      }
4326
0
    }
4327
0
    zend_string_release_ex(name, 0);
4328
0
  }
4329
0
  zend_compile_expr(&arg_node, args->child[1]);
4330
0
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4331
0
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4332
0
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4333
0
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4334
4335
0
  return SUCCESS;
4336
0
}
4337
/* }}} */
4338
4339
/* cuf = call_user_func */
4340
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname) /* {{{ */
4341
0
{
4342
0
  uint32_t i;
4343
4344
0
  if (args->children < 1) {
4345
0
    return FAILURE;
4346
0
  }
4347
4348
0
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4349
0
  for (i = 1; i < args->children; ++i) {
4350
0
    zend_ast *arg_ast = args->child[i];
4351
0
    znode arg_node;
4352
0
    zend_op *opline;
4353
4354
0
    zend_compile_expr(&arg_node, arg_ast);
4355
4356
0
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4357
0
    opline->op2.num = i;
4358
0
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4359
0
  }
4360
0
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4361
4362
0
  return SUCCESS;
4363
0
}
4364
/* }}} */
4365
4366
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno) /* {{{ */
4367
1
{
4368
1
  if (EG(assertions) >= 0) {
4369
1
    znode name_node;
4370
1
    zend_op *opline;
4371
1
    uint32_t check_op_number = get_next_op_number();
4372
4373
1
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4374
4375
1
    if (fbc && fbc_is_finalized(fbc)) {
4376
1
      name_node.op_type = IS_CONST;
4377
1
      ZVAL_STR_COPY(&name_node.u.constant, name);
4378
4379
1
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4380
1
    } else {
4381
0
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4382
0
      opline->op2_type = IS_CONST;
4383
0
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4384
0
    }
4385
1
    opline->result.num = zend_alloc_cache_slot();
4386
4387
1
    if (args->children == 1) {
4388
      /* add "assert(condition) as assertion message */
4389
1
      zend_ast *arg = zend_ast_create_zval_from_str(
4390
1
        zend_ast_export("assert(", args->child[0], ")"));
4391
1
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4392
        /* If the original argument was named, add the new argument as named as well,
4393
         * as mixing named and positional is not allowed. */
4394
0
        zend_ast *name = zend_ast_create_zval_from_str(
4395
0
          ZSTR_INIT_LITERAL("description", 0));
4396
0
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4397
0
      }
4398
1
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4399
1
    }
4400
4401
1
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4402
4403
1
    opline = &CG(active_op_array)->opcodes[check_op_number];
4404
1
    opline->op2.opline_num = get_next_op_number();
4405
1
    SET_NODE(opline->result, result);
4406
1
  } else {
4407
0
    if (!fbc) {
4408
0
      zend_string_release_ex(name, 0);
4409
0
    }
4410
0
    result->op_type = IS_CONST;
4411
0
    ZVAL_TRUE(&result->u.constant);
4412
0
  }
4413
1
}
4414
/* }}} */
4415
4416
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4417
0
{
4418
0
  bool strict = false;
4419
0
  znode array, needly;
4420
0
  zend_op *opline;
4421
4422
0
  if (args->children == 3) {
4423
0
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4424
0
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4425
0
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4426
0
      zval value;
4427
0
      zend_ast *name_ast = args->child[2]->child[0];
4428
0
      bool is_fully_qualified;
4429
0
      zend_string *resolved_name = zend_resolve_const_name(
4430
0
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4431
4432
0
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4433
0
        zend_string_release_ex(resolved_name, 0);
4434
0
        return FAILURE;
4435
0
      }
4436
4437
0
      zend_string_release_ex(resolved_name, 0);
4438
0
      strict = zend_is_true(&value);
4439
0
      zval_ptr_dtor(&value);
4440
0
    } else {
4441
0
      return FAILURE;
4442
0
    }
4443
0
  } else if (args->children != 2) {
4444
0
    return FAILURE;
4445
0
  }
4446
4447
0
  if (args->child[1]->kind != ZEND_AST_ARRAY
4448
0
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4449
0
    return FAILURE;
4450
0
  }
4451
4452
0
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4453
0
    bool ok = true;
4454
0
    zval *val, tmp;
4455
0
    HashTable *src = Z_ARRVAL(array.u.constant);
4456
0
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4457
4458
0
    ZVAL_TRUE(&tmp);
4459
4460
0
    if (strict) {
4461
0
      ZEND_HASH_FOREACH_VAL(src, val) {
4462
0
        if (Z_TYPE_P(val) == IS_STRING) {
4463
0
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4464
0
        } else if (Z_TYPE_P(val) == IS_LONG) {
4465
0
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4466
0
        } else {
4467
0
          zend_array_destroy(dst);
4468
0
          ok = false;
4469
0
          break;
4470
0
        }
4471
0
      } ZEND_HASH_FOREACH_END();
4472
0
    } else {
4473
0
      ZEND_HASH_FOREACH_VAL(src, val) {
4474
0
        if (Z_TYPE_P(val) != IS_STRING
4475
0
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4476
0
          zend_array_destroy(dst);
4477
0
          ok = false;
4478
0
          break;
4479
0
        }
4480
0
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4481
0
      } ZEND_HASH_FOREACH_END();
4482
0
    }
4483
4484
0
    zend_array_destroy(src);
4485
0
    if (!ok) {
4486
0
      return FAILURE;
4487
0
    }
4488
0
    Z_ARRVAL(array.u.constant) = dst;
4489
0
  }
4490
0
  array.op_type = IS_CONST;
4491
4492
0
  zend_compile_expr(&needly, args->child[0]);
4493
4494
0
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4495
0
  opline->extended_value = strict;
4496
4497
0
  return SUCCESS;
4498
0
}
4499
/* }}} */
4500
4501
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4502
0
{
4503
0
  znode arg_node;
4504
0
  zend_op *opline;
4505
4506
0
  if (args->children != 1) {
4507
0
    return FAILURE;
4508
0
  }
4509
4510
0
  zend_compile_expr(&arg_node, args->child[0]);
4511
0
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4512
0
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4513
4514
0
  return SUCCESS;
4515
0
}
4516
/* }}} */
4517
4518
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4519
1
{
4520
1
  if (args->children == 0) {
4521
0
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4522
1
  } else {
4523
1
    znode arg_node;
4524
4525
1
    if (args->children != 1) {
4526
0
      return FAILURE;
4527
0
    }
4528
4529
1
    zend_compile_expr(&arg_node, args->child[0]);
4530
1
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4531
1
  }
4532
1
  return SUCCESS;
4533
1
}
4534
/* }}} */
4535
4536
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4537
0
{
4538
0
  if (args->children != 0) {
4539
0
    return FAILURE;
4540
0
  }
4541
4542
0
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4543
0
  return SUCCESS;
4544
0
}
4545
/* }}} */
4546
4547
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4548
0
{
4549
0
  znode arg_node;
4550
4551
0
  if (args->children != 1) {
4552
0
    return FAILURE;
4553
0
  }
4554
4555
0
  zend_compile_expr(&arg_node, args->child[0]);
4556
0
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4557
0
  return SUCCESS;
4558
0
}
4559
/* }}} */
4560
4561
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4562
0
{
4563
0
  if (CG(active_op_array)->function_name && args->children == 0) {
4564
0
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4565
0
    return SUCCESS;
4566
0
  } else {
4567
0
    return FAILURE;
4568
0
  }
4569
0
}
4570
/* }}} */
4571
4572
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4573
0
{
4574
0
  if (CG(active_op_array)->function_name && args->children == 0) {
4575
0
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4576
0
    return SUCCESS;
4577
0
  } else {
4578
0
    return FAILURE;
4579
0
  }
4580
0
}
4581
/* }}} */
4582
4583
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4584
0
{
4585
0
  znode subject, needle;
4586
4587
0
  if (args->children != 2) {
4588
0
    return FAILURE;
4589
0
  }
4590
4591
0
  zend_compile_expr(&needle, args->child[0]);
4592
0
  zend_compile_expr(&subject, args->child[1]);
4593
4594
0
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4595
0
  return SUCCESS;
4596
0
}
4597
/* }}} */
4598
4599
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4600
0
{
4601
0
  if (CG(active_op_array)->function_name
4602
0
   && args->children == 2
4603
0
   && args->child[0]->kind == ZEND_AST_CALL
4604
0
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4605
0
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4606
0
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4607
0
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4608
4609
0
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4610
0
    bool is_fully_qualified;
4611
0
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4612
0
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4613
0
    const zval *zv = zend_ast_get_zval(args->child[1]);
4614
0
    znode first;
4615
4616
0
    if (zend_string_equals_literal_ci(name, "func_get_args")
4617
0
     && list->children == 0
4618
0
     && Z_TYPE_P(zv) == IS_LONG
4619
0
     && Z_LVAL_P(zv) >= 0) {
4620
0
      first.op_type = IS_CONST;
4621
0
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4622
0
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4623
0
      zend_string_release_ex(name, 0);
4624
0
      return SUCCESS;
4625
0
    }
4626
0
    zend_string_release_ex(name, 0);
4627
0
  }
4628
0
  return FAILURE;
4629
0
}
4630
/* }}} */
4631
4632
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4633
0
{
4634
0
  void **handlers = zend_flf_handlers;
4635
0
  void **current = handlers;
4636
0
  while (current) {
4637
0
    if (*current == handler) {
4638
0
      return current - handlers;
4639
0
    }
4640
0
    current++;
4641
0
  }
4642
4643
0
  return (uint32_t)-1;
4644
0
}
4645
4646
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4647
96
{
4648
96
  if (zend_execute_internal) {
4649
96
    return NULL;
4650
96
  }
4651
4652
0
  if (type != BP_VAR_R) {
4653
0
    return NULL;
4654
0
  }
4655
4656
0
  if (ZEND_USER_CODE(fbc->type)) {
4657
0
    return NULL;
4658
0
  }
4659
4660
0
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4661
0
  if (!frameless_function_info) {
4662
0
    return NULL;
4663
0
  }
4664
4665
0
  if (args->children > 3) {
4666
0
    return NULL;
4667
0
  }
4668
4669
0
  while (frameless_function_info->handler) {
4670
0
    if (frameless_function_info->num_args >= args->children
4671
0
     && fbc->common.required_num_args <= args->children
4672
0
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4673
0
      || frameless_function_info->num_args == args->children)) {
4674
0
      uint32_t num_args = frameless_function_info->num_args;
4675
0
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4676
0
      if (offset == (uint32_t)-1) {
4677
0
        continue;
4678
0
      }
4679
0
      return frameless_function_info;
4680
0
    }
4681
0
    frameless_function_info++;
4682
0
  }
4683
4684
0
  return NULL;
4685
0
}
4686
4687
static uint32_t zend_compile_frameless_icall_ex(znode *result, const zend_ast_list *args, const zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type)
4688
0
{
4689
0
  uint32_t lineno = CG(zend_lineno);
4690
0
  uint32_t num_args = frameless_function_info->num_args;
4691
0
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4692
0
  znode arg_zvs[3];
4693
0
  for (uint32_t i = 0; i < num_args; i++) {
4694
0
    if (i < args->children) {
4695
0
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4696
0
    } else {
4697
0
      const zend_arg_info *arg_info = &fbc->common.arg_info[i];
4698
0
      arg_zvs[i].op_type = IS_CONST;
4699
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4700
0
        ZEND_UNREACHABLE();
4701
0
      }
4702
0
    }
4703
0
  }
4704
0
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4705
0
  uint32_t opnum = get_next_op_number();
4706
0
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4707
0
  opline->extended_value = offset;
4708
0
  opline->lineno = lineno;
4709
0
  if (num_args >= 1) {
4710
0
    SET_NODE(opline->op1, &arg_zvs[0]);
4711
0
  }
4712
0
  if (num_args >= 2) {
4713
0
    SET_NODE(opline->op2, &arg_zvs[1]);
4714
0
  }
4715
0
  if (num_args >= 3) {
4716
0
    zend_emit_op_data(&arg_zvs[2]);
4717
0
  }
4718
0
  return opnum;
4719
0
}
4720
4721
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4722
96
{
4723
96
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4724
96
  if (!frameless_function_info) {
4725
96
    return (uint32_t)-1;
4726
96
  }
4727
4728
0
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4729
96
}
4730
4731
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4732
0
{
4733
0
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4734
4735
  /* Find frameless function with same name. */
4736
0
  const zend_function *frameless_function = NULL;
4737
0
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4738
0
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4739
   /* Avoid blowing up op count with nested frameless branches. */
4740
0
   && !CG(context).in_jmp_frameless_branch) {
4741
0
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4742
0
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4743
0
  }
4744
4745
  /* Check whether any frameless handler may actually be used. */
4746
0
  uint32_t jmp_fl_opnum = 0;
4747
0
  const zend_frameless_function_info *frameless_function_info = NULL;
4748
0
  if (frameless_function) {
4749
0
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4750
0
    if (frameless_function_info) {
4751
0
      CG(context).in_jmp_frameless_branch = true;
4752
0
      znode op1;
4753
0
      op1.op_type = IS_CONST;
4754
0
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4755
0
      jmp_fl_opnum = get_next_op_number();
4756
0
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4757
0
    }
4758
0
  }
4759
4760
  /* Compile ns call. */
4761
0
  zend_op *opline = get_next_op();
4762
0
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4763
0
  opline->op2_type = IS_CONST;
4764
0
  opline->op2.constant = name_constants;
4765
0
  opline->result.num = zend_alloc_cache_slot();
4766
0
  zend_compile_call_common(result, args_ast, NULL, lineno);
4767
4768
  /* Compile frameless call. */
4769
0
  if (frameless_function_info) {
4770
0
    CG(zend_lineno) = lineno;
4771
4772
0
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4773
0
    uint32_t jmp_fl_target = get_next_op_number();
4774
4775
0
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4776
4777
0
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4778
0
    jmp_fl->op2.opline_num = jmp_fl_target;
4779
0
    jmp_fl->extended_value = zend_alloc_cache_slot();
4780
0
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4781
0
    SET_NODE(flf_icall->result, result);
4782
0
    zend_update_jump_target_to_next(jmp_end_opnum);
4783
4784
0
    CG(context).in_jmp_frameless_branch = false;
4785
0
  }
4786
0
}
4787
/* }}} */
4788
4789
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4790
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4791
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4792
4793
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4794
0
{
4795
  /* Bail out if we do not have a format string. */
4796
0
  if (args->children < 1) {
4797
0
    return FAILURE;
4798
0
  }
4799
4800
0
  zend_eval_const_expr(&args->child[0]);
4801
  /* Bail out if the format string is not constant. */
4802
0
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4803
0
    return FAILURE;
4804
0
  }
4805
4806
0
  zval *format_string = zend_ast_get_zval(args->child[0]);
4807
0
  if (Z_TYPE_P(format_string) != IS_STRING) {
4808
0
    return FAILURE;
4809
0
  }
4810
0
  if (Z_STRLEN_P(format_string) >= 256) {
4811
0
    return FAILURE;
4812
0
  }
4813
4814
0
  char *p;
4815
0
  char *end;
4816
0
  uint32_t placeholder_count;
4817
4818
0
  placeholder_count = 0;
4819
0
  p = Z_STRVAL_P(format_string);
4820
0
  end = p + Z_STRLEN_P(format_string);
4821
4822
0
  for (;;) {
4823
0
    p = memchr(p, '%', end - p);
4824
0
    if (!p) {
4825
0
      break;
4826
0
    }
4827
4828
0
    char *q = p + 1;
4829
0
    if (q == end) {
4830
0
      return FAILURE;
4831
0
    }
4832
4833
0
    switch (*q) {
4834
0
      case 's':
4835
0
      case 'd':
4836
0
        placeholder_count++;
4837
0
        break;
4838
0
      case '%':
4839
0
        break;
4840
0
      default:
4841
0
        return FAILURE;
4842
0
    }
4843
4844
0
    p = q;
4845
0
    p++;
4846
0
  }
4847
4848
  /* Bail out if the number of placeholders does not match the number of values. */
4849
0
  if (placeholder_count != (args->children - 1)) {
4850
0
    return FAILURE;
4851
0
  }
4852
4853
  /* Handle empty format strings. */
4854
0
  if (Z_STRLEN_P(format_string) == 0) {
4855
0
    result->op_type = IS_CONST;
4856
0
    ZVAL_EMPTY_STRING(&result->u.constant);
4857
4858
0
    return SUCCESS;
4859
0
  }
4860
4861
0
  znode *elements = NULL;
4862
4863
0
  if (placeholder_count > 0) {
4864
0
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4865
0
  }
4866
4867
  /* Compile the value expressions first for error handling that is consistent
4868
   * with a function call: Values that fail to convert to a string may emit errors.
4869
   */
4870
0
  for (uint32_t i = 0; i < placeholder_count; i++) {
4871
0
    zend_compile_expr(elements + i, args->child[1 + i]);
4872
0
  }
4873
4874
0
  uint32_t rope_elements = 0;
4875
0
  uint32_t rope_init_lineno = -1;
4876
0
  zend_op *opline = NULL;
4877
4878
0
  placeholder_count = 0;
4879
0
  p = Z_STRVAL_P(format_string);
4880
0
  end = p + Z_STRLEN_P(format_string);
4881
0
  char *offset = p;
4882
0
  for (;;) {
4883
0
    p = memchr(p, '%', end - p);
4884
0
    if (!p) {
4885
0
      break;
4886
0
    }
4887
4888
0
    char *q = p + 1;
4889
0
    ZEND_ASSERT(q < end);
4890
0
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4891
4892
0
    if (*q == '%') {
4893
      /* Optimization to not create a dedicated rope element for the literal '%':
4894
       * Include the first '%' within the "constant" part instead of dropping the
4895
       * full placeholder.
4896
       */
4897
0
      p++;
4898
0
    }
4899
4900
0
    if (p != offset) {
4901
0
      znode const_node;
4902
0
      const_node.op_type = IS_CONST;
4903
0
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4904
0
      if (rope_elements == 0) {
4905
0
        rope_init_lineno = get_next_op_number();
4906
0
      }
4907
0
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4908
0
    }
4909
4910
0
    if (*q != '%') {
4911
0
      switch (*q) {
4912
0
        case 's':
4913
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4914
           * for correct error reporting.
4915
           */
4916
0
          if (elements[placeholder_count].op_type == IS_CONST) {
4917
0
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4918
0
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4919
0
            } else {
4920
0
              convert_to_string(&elements[placeholder_count].u.constant);
4921
0
            }
4922
0
          }
4923
0
          break;
4924
0
        case 'd':
4925
0
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4926
0
          break;
4927
0
        EMPTY_SWITCH_DEFAULT_CASE();
4928
0
      }
4929
4930
0
      if (rope_elements == 0) {
4931
0
        rope_init_lineno = get_next_op_number();
4932
0
      }
4933
0
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4934
4935
0
      placeholder_count++;
4936
0
    }
4937
4938
0
    p = q;
4939
0
    p++;
4940
0
    offset = p;
4941
0
  }
4942
0
  if (end != offset) {
4943
    /* Add the constant part after the last placeholder. */
4944
0
    znode const_node;
4945
0
    const_node.op_type = IS_CONST;
4946
0
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4947
0
    if (rope_elements == 0) {
4948
0
      rope_init_lineno = get_next_op_number();
4949
0
    }
4950
0
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4951
0
  }
4952
0
  ZEND_ASSERT(opline != NULL);
4953
4954
0
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4955
0
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4956
0
  efree(elements);
4957
4958
0
  return SUCCESS;
4959
0
}
4960
4961
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
4962
0
{
4963
  /* Special case: printf with a single constant string argument and no format specifiers.
4964
   * In this case, just emit ECHO and return the string length if needed. */
4965
0
  if (args->children == 1) {
4966
0
    zend_eval_const_expr(&args->child[0]);
4967
0
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
4968
0
      return FAILURE;
4969
0
    }
4970
0
    zval *format_string = zend_ast_get_zval(args->child[0]);
4971
0
    if (Z_TYPE_P(format_string) != IS_STRING) {
4972
0
      return FAILURE;
4973
0
    }
4974
    /* Check if there are any format specifiers */
4975
0
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
4976
      /* No format specifiers - just emit ECHO and return string length */
4977
0
      znode format_node;
4978
0
      zend_compile_expr(&format_node, args->child[0]);
4979
0
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
4980
4981
      /* Return the string length as a constant if the result is used */
4982
0
      result->op_type = IS_CONST;
4983
0
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
4984
0
      return SUCCESS;
4985
0
    }
4986
0
  }
4987
4988
  /* Fall back to sprintf optimization for format strings with specifiers */
4989
0
  znode rope_result;
4990
0
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
4991
0
    return FAILURE;
4992
0
  }
4993
4994
  /* printf() returns the amount of bytes written, so just an ECHO of the
4995
   * resulting sprintf() optimisation might not be enough. At this early
4996
   * stage we can't detect if the result is actually used, so we just emit
4997
   * the opcodes and let them be cleaned up by the dead code elimination
4998
   * pass in the Zend Optimizer if the result of the printf() is in fact
4999
   * unused */
5000
0
  znode copy;
5001
0
  if (rope_result.op_type != IS_CONST) {
5002
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5003
0
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5004
0
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5005
0
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5006
0
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5007
0
  } else {
5008
0
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5009
0
    result->op_type = IS_CONST;
5010
0
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5011
0
  }
5012
5013
0
  return SUCCESS;
5014
0
}
5015
5016
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5017
0
{
5018
0
  znode arg_node;
5019
5020
0
  if (args->children != 1) {
5021
0
    return FAILURE;
5022
0
  }
5023
5024
0
  zend_compile_expr(&arg_node, args->child[0]);
5025
0
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5026
5027
0
  return SUCCESS;
5028
0
}
5029
5030
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
5031
122
{
5032
122
  if (zend_string_equals_literal(lcname, "strlen")) {
5033
0
    return zend_compile_func_strlen(result, args);
5034
122
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5035
0
    return zend_compile_func_typecheck(result, args, IS_NULL);
5036
122
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5037
0
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5038
122
  } else if (zend_string_equals_literal(lcname, "is_long")
5039
122
    || zend_string_equals_literal(lcname, "is_int")
5040
122
    || zend_string_equals_literal(lcname, "is_integer")
5041
122
  ) {
5042
0
    return zend_compile_func_typecheck(result, args, IS_LONG);
5043
122
  } else if (zend_string_equals_literal(lcname, "is_float")
5044
122
    || zend_string_equals_literal(lcname, "is_double")
5045
122
  ) {
5046
0
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5047
122
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5048
0
    return zend_compile_func_typecheck(result, args, IS_STRING);
5049
122
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5050
0
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5051
122
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5052
0
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5053
122
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5054
0
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5055
122
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5056
0
    return zend_compile_func_is_scalar(result, args);
5057
122
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5058
0
    return zend_compile_func_cast(result, args, _IS_BOOL);
5059
122
  } else if (zend_string_equals_literal(lcname, "intval")) {
5060
0
    return zend_compile_func_cast(result, args, IS_LONG);
5061
122
  } else if (zend_string_equals_literal(lcname, "floatval")
5062
97
    || zend_string_equals_literal(lcname, "doubleval")
5063
122
  ) {
5064
25
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5065
97
  } else if (zend_string_equals_literal(lcname, "strval")) {
5066
0
    return zend_compile_func_cast(result, args, IS_STRING);
5067
97
  } else if (zend_string_equals_literal(lcname, "defined")) {
5068
0
    return zend_compile_func_defined(result, args);
5069
97
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5070
0
    return zend_compile_func_chr(result, args);
5071
97
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5072
0
    return zend_compile_func_ord(result, args);
5073
97
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5074
0
    return zend_compile_func_cufa(result, args, lcname);
5075
97
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5076
0
    return zend_compile_func_cuf(result, args, lcname);
5077
97
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5078
0
    return zend_compile_func_in_array(result, args);
5079
97
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5080
97
      || zend_string_equals_literal(lcname, "sizeof")) {
5081
0
    return zend_compile_func_count(result, args, lcname);
5082
97
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5083
1
    return zend_compile_func_get_class(result, args);
5084
96
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5085
0
    return zend_compile_func_get_called_class(result, args);
5086
96
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5087
0
    return zend_compile_func_gettype(result, args);
5088
96
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5089
0
    return zend_compile_func_num_args(result, args);
5090
96
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5091
0
    return zend_compile_func_get_args(result, args);
5092
96
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5093
0
    return zend_compile_func_array_slice(result, args);
5094
96
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5095
0
    return zend_compile_func_array_key_exists(result, args);
5096
96
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5097
0
    return zend_compile_func_sprintf(result, args);
5098
96
  } else if (zend_string_equals_literal(lcname, "printf")) {
5099
0
    return zend_compile_func_printf(result, args);
5100
96
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5101
0
    return zend_compile_func_clone(result, args);
5102
96
  } else {
5103
96
    return FAILURE;
5104
96
  }
5105
122
}
5106
5107
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
5108
132
{
5109
132
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5110
0
    return FAILURE;
5111
0
  }
5112
5113
132
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5114
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5115
     * function with a different implementation. Don't use the VM builtin in that case. */
5116
10
    return FAILURE;
5117
10
  }
5118
5119
122
  if (zend_args_contain_unpack_or_named(args)) {
5120
0
    return FAILURE;
5121
0
  }
5122
5123
122
  if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
5124
26
    return SUCCESS;
5125
26
  }
5126
5127
96
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5128
122
}
5129
5130
0
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5131
0
  switch (kind) {
5132
0
    case ZEND_PROPERTY_HOOK_GET:
5133
0
      return "get";
5134
0
    case ZEND_PROPERTY_HOOK_SET:
5135
0
      return "set";
5136
0
    EMPTY_SWITCH_DEFAULT_CASE()
5137
0
  }
5138
0
}
5139
5140
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5141
0
{
5142
0
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5143
0
    return zend_string_copy(prop_name);
5144
0
  } else {
5145
0
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5146
0
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5147
0
  }
5148
0
}
5149
5150
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5151
0
{
5152
0
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5153
5154
0
  const zend_ast *class_ast = ast->child[0];
5155
0
  zend_ast *method_ast = ast->child[1];
5156
5157
  /* Recognize parent::$prop::get() pattern. */
5158
0
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5159
0
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5160
0
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5161
0
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5162
0
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5163
0
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5164
0
   || method_ast->kind != ZEND_AST_ZVAL
5165
0
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5166
0
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5167
0
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5168
0
    return false;
5169
0
  }
5170
5171
0
  zend_class_entry *ce = CG(active_class_entry);
5172
0
  if (!ce) {
5173
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5174
0
  }
5175
5176
0
  zend_ast *args_ast = ast->child[2];
5177
0
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5178
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5179
0
  }
5180
5181
0
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5182
0
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5183
0
  zend_string *hook_name = zend_ast_get_str(method_ast);
5184
0
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5185
0
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5186
5187
0
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5188
0
  if (!prop_info_name) {
5189
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5190
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5191
0
  }
5192
5193
0
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5194
0
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5195
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5196
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5197
0
  }
5198
0
  if (hook_kind != CG(context).active_property_hook_kind) {
5199
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5200
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5201
0
  }
5202
5203
0
  zend_op *opline = get_next_op();
5204
0
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5205
0
  opline->op1_type = IS_CONST;
5206
0
  opline->op1.constant = zend_add_literal_string(&property_name);
5207
0
  opline->op2.num = hook_kind;
5208
5209
0
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast));
5210
5211
0
  return true;
5212
0
}
5213
5214
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5215
154
{
5216
154
  zend_ast *name_ast = ast->child[0];
5217
154
  zend_ast *args_ast = ast->child[1];
5218
154
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5219
5220
154
  znode name_node;
5221
5222
154
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5223
5
    zend_compile_expr(&name_node, name_ast);
5224
5
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5225
5
    return;
5226
5
  }
5227
5228
149
  {
5229
149
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5230
149
    if (runtime_resolution) {
5231
0
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5232
0
          && !is_callable_convert) {
5233
0
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5234
0
      } else {
5235
0
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5236
0
      }
5237
0
      return;
5238
0
    }
5239
149
  }
5240
5241
149
  {
5242
149
    const zval *name = &name_node.u.constant;
5243
149
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5244
149
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5245
149
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5246
149
    zend_op *opline;
5247
5248
    /* Special assert() handling should apply independently of compiler flags. */
5249
149
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5250
1
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5251
1
      zend_string_release(lcname);
5252
1
      zval_ptr_dtor(&name_node.u.constant);
5253
1
      return;
5254
1
    }
5255
5256
148
    if (!fbc
5257
132
     || !fbc_is_finalized(fbc)
5258
132
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5259
16
      zend_string_release_ex(lcname, 0);
5260
16
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5261
16
      return;
5262
16
    }
5263
5264
132
    if (!is_callable_convert &&
5265
132
        zend_try_compile_special_func(result, lcname,
5266
132
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5267
132
    ) {
5268
26
      zend_string_release_ex(lcname, 0);
5269
26
      zval_ptr_dtor(&name_node.u.constant);
5270
26
      return;
5271
26
    }
5272
5273
106
    zval_ptr_dtor(&name_node.u.constant);
5274
106
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5275
5276
106
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5277
106
    opline->result.num = zend_alloc_cache_slot();
5278
5279
    /* Store offset to function from symbol table in op2.extra. */
5280
106
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5281
96
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5282
96
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5283
96
    }
5284
5285
106
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5286
106
  }
5287
106
}
5288
/* }}} */
5289
5290
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5291
44
{
5292
44
  zend_ast *obj_ast = ast->child[0];
5293
44
  zend_ast *method_ast = ast->child[1];
5294
44
  zend_ast *args_ast = ast->child[2];
5295
5296
44
  znode obj_node, method_node;
5297
44
  zend_op *opline;
5298
44
  const zend_function *fbc = NULL;
5299
44
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5300
44
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5301
5302
44
  if (is_this_fetch(obj_ast)) {
5303
0
    if (this_guaranteed_exists()) {
5304
0
      obj_node.op_type = IS_UNUSED;
5305
0
    } else {
5306
0
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5307
0
    }
5308
0
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5309
5310
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5311
     * check for a nullsafe access. */
5312
44
  } else {
5313
44
    zend_short_circuiting_mark_inner(obj_ast);
5314
44
    zend_compile_expr(&obj_node, obj_ast);
5315
44
    if (nullsafe) {
5316
0
      zend_emit_jmp_null(&obj_node, type);
5317
0
    }
5318
44
  }
5319
5320
44
  zend_compile_expr(&method_node, method_ast);
5321
44
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5322
5323
44
  if (method_node.op_type == IS_CONST) {
5324
44
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5325
0
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5326
0
    }
5327
5328
44
    opline->op2_type = IS_CONST;
5329
44
    opline->op2.constant = zend_add_func_name_literal(
5330
44
      Z_STR(method_node.u.constant));
5331
44
    opline->result.num = zend_alloc_cache_slots(2);
5332
44
  } else {
5333
0
    SET_NODE(opline->op2, &method_node);
5334
0
  }
5335
5336
  /* Check if this calls a known method on $this */
5337
44
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5338
0
      CG(active_class_entry) && zend_is_scope_known()) {
5339
0
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5340
0
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5341
5342
    /* We only know the exact method that is being called if it is either private or final.
5343
     * Otherwise an overriding method in a child class may be called. */
5344
0
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5345
0
      fbc = NULL;
5346
0
    }
5347
0
  }
5348
5349
44
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5350
0
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5351
0
      zend_error_noreturn(E_COMPILE_ERROR,
5352
0
        "Cannot combine nullsafe operator with Closure creation");
5353
0
    }
5354
0
  }
5355
44
}
5356
/* }}} */
5357
5358
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5359
0
{
5360
0
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5361
0
}
5362
/* }}} */
5363
5364
static bool is_func_accessible(const zend_function *fbc)
5365
111
{
5366
111
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5367
111
    return true;
5368
111
  }
5369
5370
0
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5371
0
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5372
0
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5373
0
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5374
0
    return true;
5375
0
  }
5376
5377
0
  return false;
5378
0
}
5379
5380
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5381
0
{
5382
0
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5383
5384
0
  if (!fbc || is_func_accessible(fbc)) {
5385
0
    return fbc;
5386
0
  }
5387
5388
0
  return NULL;
5389
0
}
5390
/* }}} */
5391
5392
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5393
0
{
5394
0
  zend_ast *class_ast = ast->child[0];
5395
0
  zend_ast *method_ast = ast->child[1];
5396
0
  zend_ast *args_ast = ast->child[2];
5397
5398
0
  znode class_node, method_node;
5399
0
  zend_op *opline;
5400
0
  const zend_function *fbc = NULL;
5401
5402
0
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5403
0
    return;
5404
0
  }
5405
5406
0
  zend_short_circuiting_mark_inner(class_ast);
5407
0
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5408
5409
0
  zend_compile_expr(&method_node, method_ast);
5410
5411
0
  if (method_node.op_type == IS_CONST) {
5412
0
    zval *name = &method_node.u.constant;
5413
0
    if (Z_TYPE_P(name) != IS_STRING) {
5414
0
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5415
0
    }
5416
0
    if (zend_is_constructor(Z_STR_P(name))) {
5417
0
      zval_ptr_dtor(name);
5418
0
      method_node.op_type = IS_UNUSED;
5419
0
    }
5420
0
  }
5421
5422
0
  opline = get_next_op();
5423
0
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5424
5425
0
  zend_set_class_name_op1(opline, &class_node);
5426
5427
0
  if (method_node.op_type == IS_CONST) {
5428
0
    opline->op2_type = IS_CONST;
5429
0
    opline->op2.constant = zend_add_func_name_literal(
5430
0
      Z_STR(method_node.u.constant));
5431
0
    opline->result.num = zend_alloc_cache_slots(2);
5432
0
  } else {
5433
0
    if (opline->op1_type == IS_CONST) {
5434
0
      opline->result.num = zend_alloc_cache_slot();
5435
0
    }
5436
0
    SET_NODE(opline->op2, &method_node);
5437
0
  }
5438
5439
  /* Check if we already know which method we're calling */
5440
0
  if (opline->op2_type == IS_CONST) {
5441
0
    zend_class_entry *ce = NULL;
5442
0
    if (opline->op1_type == IS_CONST) {
5443
0
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5444
0
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5445
0
      if (ce) {
5446
0
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5447
0
          ce = NULL;
5448
0
        }
5449
0
      } else if (CG(active_class_entry)
5450
0
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5451
0
        ce = CG(active_class_entry);
5452
0
      }
5453
0
    } else if (opline->op1_type == IS_UNUSED
5454
0
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5455
0
        && zend_is_scope_known()) {
5456
0
      ce = CG(active_class_entry);
5457
0
    }
5458
0
    if (ce) {
5459
0
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5460
0
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5461
0
    }
5462
0
  }
5463
5464
0
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5465
0
}
5466
/* }}} */
5467
5468
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5469
5470
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5471
155
{
5472
155
  zend_ast *class_ast = ast->child[0];
5473
155
  zend_ast *args_ast = ast->child[1];
5474
5475
155
  znode class_node, ctor_result;
5476
155
  zend_op *opline;
5477
5478
155
  if (class_ast->kind == ZEND_AST_CLASS) {
5479
    /* anon class declaration */
5480
0
    zend_compile_class_decl(&class_node, class_ast, false);
5481
155
  } else {
5482
155
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5483
155
  }
5484
5485
155
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5486
5487
155
  zend_set_class_name_op1(opline, &class_node);
5488
5489
155
  if (opline->op1_type == IS_CONST) {
5490
155
    opline->op2.num = zend_alloc_cache_slot();
5491
155
  }
5492
5493
155
  zend_class_entry *ce = NULL;
5494
155
  if (opline->op1_type == IS_CONST) {
5495
155
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5496
155
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5497
155
    if (ce) {
5498
144
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5499
0
        ce = NULL;
5500
0
      }
5501
144
    } else if (CG(active_class_entry)
5502
11
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5503
11
      ce = CG(active_class_entry);
5504
11
    }
5505
155
  } else if (opline->op1_type == IS_UNUSED
5506
0
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5507
0
      && zend_is_scope_known()) {
5508
0
    ce = CG(active_class_entry);
5509
0
  }
5510
5511
5512
155
  const zend_function *fbc = NULL;
5513
155
  if (ce
5514
155
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5515
155
      && ce->constructor
5516
111
      && is_func_accessible(ce->constructor)) {
5517
111
    fbc = ce->constructor;
5518
111
  }
5519
5520
155
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno);
5521
155
  zend_do_free(&ctor_result);
5522
155
}
5523
/* }}} */
5524
5525
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5526
0
{
5527
0
  zend_ast *var_ast = ast->child[0];
5528
0
  zend_ast *name_ast = var_ast->child[0];
5529
5530
0
  znode name_node, result;
5531
5532
0
  zend_compile_expr(&name_node, name_ast);
5533
0
  if (name_node.op_type == IS_CONST) {
5534
0
    convert_to_string(&name_node.u.constant);
5535
0
  }
5536
5537
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5538
0
  if (is_this_fetch(var_ast)) {
5539
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5540
0
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5541
0
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5542
0
    opline->extended_value = zend_alloc_cache_slot();
5543
0
  } else {
5544
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5545
     * to not free the name_node operand, so it can be reused in the following
5546
     * ASSIGN_REF, which then frees it. */
5547
0
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5548
0
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5549
5550
0
    if (name_node.op_type == IS_CONST) {
5551
0
      zend_string_addref(Z_STR(name_node.u.constant));
5552
0
    }
5553
5554
0
    zend_emit_assign_ref_znode(
5555
0
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5556
0
      &result
5557
0
    );
5558
0
  }
5559
0
}
5560
/* }}} */
5561
5562
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5563
37
{
5564
37
  zend_op *opline;
5565
37
  if (!CG(active_op_array)->static_variables) {
5566
0
    if (CG(active_op_array)->scope) {
5567
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5568
0
    }
5569
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5570
0
  }
5571
5572
37
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5573
5574
37
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5575
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5576
0
  }
5577
5578
37
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5579
37
  opline->op1_type = IS_CV;
5580
37
  opline->op1.var = lookup_cv(var_name);
5581
37
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5582
37
}
5583
/* }}} */
5584
5585
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5586
0
{
5587
0
  zend_ast *var_ast = ast->child[0];
5588
0
  zend_string *var_name = zend_ast_get_str(var_ast);
5589
5590
0
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5591
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5592
0
  }
5593
5594
0
  if (!CG(active_op_array)->static_variables) {
5595
0
    if (CG(active_op_array)->scope) {
5596
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5597
0
    }
5598
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5599
0
  }
5600
5601
0
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5602
0
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5603
0
  }
5604
5605
0
  zend_eval_const_expr(&ast->child[1]);
5606
0
  zend_ast *value_ast = ast->child[1];
5607
5608
0
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5609
0
    zval *value_zv = value_ast
5610
0
      ? zend_ast_get_zval(value_ast)
5611
0
      : &EG(uninitialized_zval);
5612
0
    Z_TRY_ADDREF_P(value_zv);
5613
0
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5614
0
  } else {
5615
0
    zend_op *opline;
5616
5617
0
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5618
0
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5619
5620
0
    uint32_t static_def_jmp_opnum = get_next_op_number();
5621
0
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5622
0
    opline->op1_type = IS_CV;
5623
0
    opline->op1.var = lookup_cv(var_name);
5624
0
    opline->extended_value = placeholder_offset;
5625
5626
0
    znode expr;
5627
0
    zend_compile_expr(&expr, value_ast);
5628
5629
0
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5630
0
    opline->op1_type = IS_CV;
5631
0
    opline->op1.var = lookup_cv(var_name);
5632
0
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5633
5634
0
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5635
0
  }
5636
0
}
5637
/* }}} */
5638
5639
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5640
0
{
5641
0
  zend_ast *var_ast = ast->child[0];
5642
0
  znode var_node;
5643
0
  zend_op *opline;
5644
5645
0
  zend_ensure_writable_variable(var_ast);
5646
5647
0
  if (is_global_var_fetch(var_ast)) {
5648
0
    if (!var_ast->child[1]) {
5649
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5650
0
    }
5651
5652
0
    zend_compile_expr(&var_node, var_ast->child[1]);
5653
0
    if (var_node.op_type == IS_CONST) {
5654
0
      convert_to_string(&var_node.u.constant);
5655
0
    }
5656
5657
0
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5658
0
    opline->extended_value = ZEND_FETCH_GLOBAL;
5659
0
    return;
5660
0
  }
5661
5662
0
  switch (var_ast->kind) {
5663
0
    case ZEND_AST_VAR:
5664
0
      if (is_this_fetch(var_ast)) {
5665
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5666
0
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5667
0
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5668
0
      } else {
5669
0
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5670
0
        opline->opcode = ZEND_UNSET_VAR;
5671
0
      }
5672
0
      return;
5673
0
    case ZEND_AST_DIM:
5674
0
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5675
0
      opline->opcode = ZEND_UNSET_DIM;
5676
0
      return;
5677
0
    case ZEND_AST_PROP:
5678
0
    case ZEND_AST_NULLSAFE_PROP:
5679
0
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5680
0
      opline->opcode = ZEND_UNSET_OBJ;
5681
0
      return;
5682
0
    case ZEND_AST_STATIC_PROP:
5683
0
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5684
0
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5685
0
      return;
5686
0
    EMPTY_SWITCH_DEFAULT_CASE()
5687
0
  }
5688
0
}
5689
/* }}} */
5690
5691
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5692
2
{
5693
2
  const zend_loop_var *base;
5694
2
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5695
5696
2
  if (!loop_var) {
5697
0
    return 1;
5698
0
  }
5699
2
  base = zend_stack_base(&CG(loop_var_stack));
5700
2
  for (; loop_var >= base; loop_var--) {
5701
2
    if (loop_var->opcode == ZEND_FAST_CALL) {
5702
0
      zend_op *opline = get_next_op();
5703
5704
0
      opline->opcode = ZEND_FAST_CALL;
5705
0
      opline->result_type = IS_TMP_VAR;
5706
0
      opline->result.var = loop_var->var_num;
5707
0
      if (return_value) {
5708
0
        SET_NODE(opline->op2, return_value);
5709
0
      }
5710
0
      opline->op1.num = loop_var->try_catch_offset;
5711
2
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5712
0
      zend_op *opline = get_next_op();
5713
0
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5714
0
      opline->op1_type = IS_TMP_VAR;
5715
0
      opline->op1.var = loop_var->var_num;
5716
2
    } else if (loop_var->opcode == ZEND_RETURN) {
5717
      /* Stack separator */
5718
2
      break;
5719
2
    } else if (depth <= 1) {
5720
0
      return 1;
5721
0
    } else if (loop_var->opcode == ZEND_NOP) {
5722
      /* Loop doesn't have freeable variable */
5723
0
      depth--;
5724
0
    } else {
5725
0
      zend_op *opline;
5726
5727
0
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5728
0
      opline = get_next_op();
5729
0
      opline->opcode = loop_var->opcode;
5730
0
      opline->op1_type = loop_var->var_type;
5731
0
      opline->op1.var = loop_var->var_num;
5732
0
      opline->extended_value = ZEND_FREE_ON_RETURN;
5733
0
      depth--;
5734
0
      }
5735
2
  }
5736
2
  return (depth == 0);
5737
2
}
5738
/* }}} */
5739
5740
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5741
2
{
5742
2
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5743
2
}
5744
/* }}} */
5745
5746
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5747
0
{
5748
0
  const zend_loop_var *base;
5749
0
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5750
5751
0
  if (!loop_var) {
5752
0
    return 0;
5753
0
  }
5754
0
  base = zend_stack_base(&CG(loop_var_stack));
5755
0
  for (; loop_var >= base; loop_var--) {
5756
0
    if (loop_var->opcode == ZEND_FAST_CALL) {
5757
0
      return 1;
5758
0
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5759
0
    } else if (loop_var->opcode == ZEND_RETURN) {
5760
      /* Stack separator */
5761
0
      return 0;
5762
0
    } else if (depth <= 1) {
5763
0
      return 0;
5764
0
    } else {
5765
0
      depth--;
5766
0
      }
5767
0
  }
5768
0
  return 0;
5769
0
}
5770
/* }}} */
5771
5772
static bool zend_has_finally(void) /* {{{ */
5773
0
{
5774
0
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5775
0
}
5776
/* }}} */
5777
5778
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5779
2
{
5780
2
  zend_ast *expr_ast = ast->child[0];
5781
2
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5782
2
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5783
5784
2
  znode expr_node;
5785
2
  zend_op *opline;
5786
5787
2
  if (is_generator) {
5788
    /* For generators the by-ref flag refers to yields, not returns */
5789
0
    by_ref = false;
5790
0
  }
5791
5792
2
  if (!expr_ast) {
5793
0
    expr_node.op_type = IS_CONST;
5794
0
    ZVAL_NULL(&expr_node.u.constant);
5795
2
  } else if (by_ref && zend_is_variable(expr_ast)) {
5796
0
    zend_assert_not_short_circuited(expr_ast);
5797
0
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5798
2
  } else {
5799
2
    zend_compile_expr(&expr_node, expr_ast);
5800
2
  }
5801
5802
2
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5803
0
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5804
0
   && zend_has_finally()) {
5805
    /* Copy return value into temporary VAR to avoid modification in finally code */
5806
0
    if (by_ref) {
5807
0
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5808
0
    } else {
5809
0
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5810
0
    }
5811
0
  }
5812
5813
  /* Generator return types are handled separately */
5814
2
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5815
1
    zend_emit_return_type_check(
5816
1
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5817
1
  }
5818
5819
2
  uint32_t opnum_before_finally = get_next_op_number();
5820
5821
2
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5822
5823
  /* Content of reference might have changed in finally, repeat type check. */
5824
2
  if (by_ref
5825
   /* Check if any opcodes were emitted since the last return type check. */
5826
0
   && opnum_before_finally != get_next_op_number()
5827
0
   && !is_generator
5828
0
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5829
0
    zend_emit_return_type_check(
5830
0
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5831
0
  }
5832
5833
2
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5834
2
    &expr_node, NULL);
5835
5836
2
  if (by_ref && expr_ast) {
5837
0
    if (zend_is_call(expr_ast)) {
5838
0
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5839
0
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5840
0
      opline->extended_value = ZEND_RETURNS_VALUE;
5841
0
    }
5842
0
  }
5843
2
}
5844
/* }}} */
5845
5846
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
5847
0
{
5848
0
  zend_ast *expr_ast = ast->child[0];
5849
0
  znode expr_node;
5850
0
  zend_op *opline;
5851
5852
0
  zend_compile_expr(&expr_node, expr_ast);
5853
5854
0
  switch (expr_node.op_type) {
5855
0
    case IS_TMP_VAR:
5856
0
    case IS_VAR:
5857
0
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5858
0
      opline->extended_value = ZEND_FREE_VOID_CAST;
5859
0
      break;
5860
0
    case IS_CONST:
5861
0
      zend_do_free(&expr_node);
5862
0
      break;
5863
0
  }
5864
0
}
5865
5866
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
5867
136
{
5868
136
  zend_op *opline;
5869
136
  zend_ast *expr_ast = ast->child[0];
5870
5871
136
  znode expr_node;
5872
136
  zend_compile_expr(&expr_node, expr_ast);
5873
5874
136
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5875
136
  opline->extended_value = 0;
5876
136
}
5877
/* }}} */
5878
5879
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
5880
0
{
5881
0
  zend_ast *expr_ast = ast->child[0];
5882
5883
0
  znode expr_node;
5884
0
  zend_compile_expr(&expr_node, expr_ast);
5885
5886
0
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5887
0
  if (result) {
5888
    /* Mark this as an "expression throw" for opcache. */
5889
0
    opline->extended_value = ZEND_THROW_IS_EXPR;
5890
0
    result->op_type = IS_CONST;
5891
0
    ZVAL_TRUE(&result->u.constant);
5892
0
  }
5893
0
}
5894
/* }}} */
5895
5896
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
5897
0
{
5898
0
  zend_ast *depth_ast = ast->child[0];
5899
5900
0
  zend_op *opline;
5901
0
  zend_long depth;
5902
5903
0
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5904
5905
0
  if (depth_ast) {
5906
0
    const zval *depth_zv;
5907
0
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5908
0
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5909
0
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5910
0
    }
5911
5912
0
    depth_zv = zend_ast_get_zval(depth_ast);
5913
0
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5914
0
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5915
0
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5916
0
    }
5917
5918
0
    depth = Z_LVAL_P(depth_zv);
5919
0
  } else {
5920
0
    depth = 1;
5921
0
  }
5922
5923
0
  if (CG(context).current_brk_cont == -1) {
5924
0
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5925
0
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5926
0
  } else {
5927
0
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5928
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5929
0
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5930
0
        depth, depth == 1 ? "" : "s");
5931
0
    }
5932
0
  }
5933
5934
0
  if (ast->kind == ZEND_AST_CONTINUE) {
5935
0
    int d, cur = CG(context).current_brk_cont;
5936
0
    for (d = depth - 1; d > 0; d--) {
5937
0
      cur = CG(context).brk_cont_array[cur].parent;
5938
0
      ZEND_ASSERT(cur != -1);
5939
0
    }
5940
5941
0
    if (CG(context).brk_cont_array[cur].is_switch) {
5942
0
      if (depth == 1) {
5943
0
        if (CG(context).brk_cont_array[cur].parent == -1) {
5944
0
          zend_error(E_WARNING,
5945
0
            "\"continue\" targeting switch is equivalent to \"break\"");
5946
0
        } else {
5947
0
          zend_error(E_WARNING,
5948
0
            "\"continue\" targeting switch is equivalent to \"break\". " \
5949
0
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5950
0
            depth + 1);
5951
0
        }
5952
0
      } else {
5953
0
        if (CG(context).brk_cont_array[cur].parent == -1) {
5954
0
          zend_error(E_WARNING,
5955
0
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5956
0
            depth, depth);
5957
0
        } else {
5958
0
          zend_error(E_WARNING,
5959
0
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5960
0
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5961
0
            depth, depth, depth + 1);
5962
0
        }
5963
0
      }
5964
0
    }
5965
0
  }
5966
5967
0
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5968
0
  opline->op1.num = CG(context).current_brk_cont;
5969
0
  opline->op2.num = depth;
5970
0
}
5971
/* }}} */
5972
5973
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5974
0
{
5975
0
  zend_label *dest;
5976
0
  int remove_oplines = opline->op1.num;
5977
0
  zval *label;
5978
0
  uint32_t opnum = opline - op_array->opcodes;
5979
5980
0
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5981
0
  if (CG(context).labels == NULL ||
5982
0
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5983
0
  ) {
5984
0
    CG(in_compilation) = 1;
5985
0
    CG(active_op_array) = op_array;
5986
0
    CG(zend_lineno) = opline->lineno;
5987
0
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5988
0
  }
5989
5990
0
  zval_ptr_dtor_str(label);
5991
0
  ZVAL_NULL(label);
5992
5993
0
  uint32_t current = opline->extended_value;
5994
0
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
5995
0
    if (current == -1) {
5996
0
      CG(in_compilation) = 1;
5997
0
      CG(active_op_array) = op_array;
5998
0
      CG(zend_lineno) = opline->lineno;
5999
0
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6000
0
    }
6001
0
    if (CG(context).brk_cont_array[current].start >= 0) {
6002
0
      remove_oplines--;
6003
0
    }
6004
0
  }
6005
6006
0
  for (current = 0; current < op_array->last_try_catch; ++current) {
6007
0
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6008
0
    if (elem->try_op > opnum) {
6009
0
      break;
6010
0
    }
6011
0
    if (elem->finally_op && opnum < elem->finally_op - 1
6012
0
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6013
0
    ) {
6014
0
      remove_oplines--;
6015
0
    }
6016
0
  }
6017
6018
0
  opline->opcode = ZEND_JMP;
6019
0
  SET_UNUSED(opline->op1);
6020
0
  SET_UNUSED(opline->op2);
6021
0
  SET_UNUSED(opline->result);
6022
0
  opline->op1.opline_num = dest->opline_num;
6023
0
  opline->extended_value = 0;
6024
6025
0
  ZEND_ASSERT(remove_oplines >= 0);
6026
0
  while (remove_oplines--) {
6027
0
    opline--;
6028
0
    MAKE_NOP(opline);
6029
0
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6030
0
  }
6031
0
}
6032
/* }}} */
6033
6034
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6035
0
{
6036
0
  zend_ast *label_ast = ast->child[0];
6037
0
  znode label_node;
6038
0
  zend_op *opline;
6039
6040
0
  zend_compile_expr(&label_node, label_ast);
6041
6042
  /* Label resolution and unwinding adjustments happen in pass two. */
6043
0
  uint32_t opnum_start = get_next_op_number();
6044
0
  zend_handle_loops_and_finally(NULL);
6045
0
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6046
0
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6047
0
  opline->extended_value = CG(context).current_brk_cont;
6048
0
}
6049
/* }}} */
6050
6051
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6052
0
{
6053
0
  zend_string *label = zend_ast_get_str(ast->child[0]);
6054
0
  zend_label dest;
6055
6056
0
  if (!CG(context).labels) {
6057
0
    ALLOC_HASHTABLE(CG(context).labels);
6058
0
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6059
0
  }
6060
6061
0
  dest.brk_cont = CG(context).current_brk_cont;
6062
0
  dest.opline_num = get_next_op_number();
6063
6064
0
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6065
0
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6066
0
  }
6067
0
}
6068
/* }}} */
6069
6070
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6071
0
{
6072
0
  zend_ast *cond_ast = ast->child[0];
6073
0
  zend_ast *stmt_ast = ast->child[1];
6074
0
  znode cond_node;
6075
0
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6076
6077
0
  opnum_jmp = zend_emit_jump(0);
6078
6079
0
  zend_begin_loop(ZEND_NOP, NULL, false);
6080
6081
0
  opnum_start = get_next_op_number();
6082
0
  zend_compile_stmt(stmt_ast);
6083
6084
0
  opnum_cond = get_next_op_number();
6085
0
  zend_update_jump_target(opnum_jmp, opnum_cond);
6086
0
  zend_compile_expr(&cond_node, cond_ast);
6087
6088
0
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6089
6090
0
  zend_end_loop(opnum_cond, NULL);
6091
0
}
6092
/* }}} */
6093
6094
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6095
0
{
6096
0
  zend_ast *stmt_ast = ast->child[0];
6097
0
  zend_ast *cond_ast = ast->child[1];
6098
6099
0
  znode cond_node;
6100
0
  uint32_t opnum_start, opnum_cond;
6101
6102
0
  zend_begin_loop(ZEND_NOP, NULL, false);
6103
6104
0
  opnum_start = get_next_op_number();
6105
0
  zend_compile_stmt(stmt_ast);
6106
6107
0
  opnum_cond = get_next_op_number();
6108
0
  zend_compile_expr(&cond_node, cond_ast);
6109
6110
0
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6111
6112
0
  zend_end_loop(opnum_cond, NULL);
6113
0
}
6114
/* }}} */
6115
6116
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6117
216
{
6118
216
  const zend_ast_list *list;
6119
216
  uint32_t i;
6120
6121
216
  result->op_type = IS_CONST;
6122
216
  ZVAL_TRUE(&result->u.constant);
6123
6124
216
  if (!ast) {
6125
0
    return;
6126
0
  }
6127
6128
216
  list = zend_ast_get_list(ast);
6129
432
  for (i = 0; i < list->children; ++i) {
6130
216
    zend_ast *expr_ast = list->child[i];
6131
6132
216
    zend_do_free(result);
6133
216
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6134
0
      zend_compile_void_cast(NULL, expr_ast);
6135
0
      result->op_type = IS_CONST;
6136
0
      ZVAL_NULL(&result->u.constant);
6137
216
    } else {
6138
216
      zend_compile_expr(result, expr_ast);
6139
216
    }
6140
216
  }
6141
216
}
6142
/* }}} */
6143
6144
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6145
72
{
6146
72
  zend_ast *init_ast = ast->child[0];
6147
72
  zend_ast *cond_ast = ast->child[1];
6148
72
  zend_ast *loop_ast = ast->child[2];
6149
72
  zend_ast *stmt_ast = ast->child[3];
6150
6151
72
  znode result;
6152
72
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6153
6154
72
  zend_compile_for_expr_list(&result, init_ast);
6155
72
  zend_do_free(&result);
6156
6157
72
  opnum_jmp = zend_emit_jump(0);
6158
6159
72
  zend_begin_loop(ZEND_NOP, NULL, false);
6160
6161
72
  opnum_start = get_next_op_number();
6162
72
  zend_compile_stmt(stmt_ast);
6163
6164
72
  opnum_loop = get_next_op_number();
6165
72
  zend_compile_for_expr_list(&result, loop_ast);
6166
72
  zend_do_free(&result);
6167
6168
72
  zend_update_jump_target_to_next(opnum_jmp);
6169
72
  zend_compile_for_expr_list(&result, cond_ast);
6170
72
  zend_do_extended_stmt(NULL);
6171
6172
72
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6173
6174
72
  zend_end_loop(opnum_loop, NULL);
6175
72
}
6176
/* }}} */
6177
6178
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6179
12
{
6180
12
  zend_ast *expr_ast = ast->child[0];
6181
12
  zend_ast *value_ast = ast->child[1];
6182
12
  zend_ast *key_ast = ast->child[2];
6183
12
  zend_ast *stmt_ast = ast->child[3];
6184
12
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6185
12
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6186
6187
12
  znode expr_node, reset_node, value_node, key_node;
6188
12
  zend_op *opline;
6189
12
  uint32_t opnum_reset, opnum_fetch;
6190
6191
12
  if (key_ast) {
6192
1
    if (key_ast->kind == ZEND_AST_REF) {
6193
0
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6194
0
    }
6195
1
    if (key_ast->kind == ZEND_AST_ARRAY) {
6196
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6197
0
    }
6198
1
  }
6199
6200
12
  if (by_ref) {
6201
0
    value_ast = value_ast->child[0];
6202
0
  }
6203
6204
12
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6205
0
    by_ref = true;
6206
0
  }
6207
6208
12
  if (by_ref && is_variable) {
6209
0
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6210
12
  } else {
6211
12
    zend_compile_expr(&expr_node, expr_ast);
6212
12
  }
6213
6214
12
  if (by_ref) {
6215
0
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6216
0
  }
6217
6218
12
  opnum_reset = get_next_op_number();
6219
12
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6220
6221
12
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6222
6223
12
  opnum_fetch = get_next_op_number();
6224
12
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6225
6226
12
  if (is_this_fetch(value_ast)) {
6227
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6228
12
  } else if (value_ast->kind == ZEND_AST_VAR &&
6229
12
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6230
12
    SET_NODE(opline->op2, &value_node);
6231
12
  } else {
6232
0
    opline->op2_type = IS_VAR;
6233
0
    opline->op2.var = get_temporary_variable();
6234
0
    GET_NODE(&value_node, opline->op2);
6235
0
    if (value_ast->kind == ZEND_AST_ARRAY) {
6236
0
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6237
0
    } else if (by_ref) {
6238
0
      zend_emit_assign_ref_znode(value_ast, &value_node);
6239
0
    } else {
6240
0
      zend_emit_assign_znode(value_ast, &value_node);
6241
0
    }
6242
0
  }
6243
6244
12
  if (key_ast) {
6245
1
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6246
1
    zend_make_tmp_result(&key_node, opline);
6247
1
    zend_emit_assign_znode(key_ast, &key_node);
6248
1
  }
6249
6250
12
  zend_compile_stmt(stmt_ast);
6251
6252
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6253
   * better to use the end line, but this information is not available
6254
   * currently. */
6255
12
  CG(zend_lineno) = ast->lineno;
6256
12
  zend_emit_jump(opnum_fetch);
6257
6258
12
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6259
12
  opline->op2.opline_num = get_next_op_number();
6260
6261
12
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6262
12
  opline->extended_value = get_next_op_number();
6263
6264
12
  zend_end_loop(opnum_fetch, &reset_node);
6265
6266
12
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6267
12
}
6268
/* }}} */
6269
6270
static void zend_compile_if(zend_ast *ast) /* {{{ */
6271
4
{
6272
4
  const zend_ast_list *list = zend_ast_get_list(ast);
6273
4
  uint32_t i;
6274
4
  uint32_t *jmp_opnums = NULL;
6275
6276
4
  if (list->children > 1) {
6277
0
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6278
0
  }
6279
6280
8
  for (i = 0; i < list->children; ++i) {
6281
4
    const zend_ast *elem_ast = list->child[i];
6282
4
    zend_ast *cond_ast = elem_ast->child[0];
6283
4
    zend_ast *stmt_ast = elem_ast->child[1];
6284
6285
4
    if (cond_ast) {
6286
4
      znode cond_node;
6287
4
      uint32_t opnum_jmpz;
6288
6289
4
      if (i > 0) {
6290
0
        CG(zend_lineno) = cond_ast->lineno;
6291
0
        zend_do_extended_stmt(NULL);
6292
0
      }
6293
6294
4
      zend_compile_expr(&cond_node, cond_ast);
6295
4
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6296
6297
4
      zend_compile_stmt(stmt_ast);
6298
6299
4
      if (i != list->children - 1) {
6300
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6301
         * report the last line in the if branch as covered if it hasn't actually executed. */
6302
0
        CG(zend_lineno) = elem_ast->lineno;
6303
0
        jmp_opnums[i] = zend_emit_jump(0);
6304
0
      }
6305
4
      zend_update_jump_target_to_next(opnum_jmpz);
6306
4
    } else {
6307
      /* "else" can only occur as last element. */
6308
0
      ZEND_ASSERT(i == list->children - 1);
6309
0
      zend_compile_stmt(stmt_ast);
6310
0
    }
6311
4
  }
6312
6313
4
  if (list->children > 1) {
6314
0
    for (i = 0; i < list->children - 1; ++i) {
6315
0
      zend_update_jump_target_to_next(jmp_opnums[i]);
6316
0
    }
6317
0
    efree(jmp_opnums);
6318
0
  }
6319
4
}
6320
/* }}} */
6321
6322
0
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6323
0
  uint32_t i;
6324
0
  uint8_t common_type = IS_UNDEF;
6325
0
  for (i = 0; i < cases->children; i++) {
6326
0
    zend_ast *case_ast = cases->child[i];
6327
0
    zend_ast **cond_ast = &case_ast->child[0];
6328
0
    const zval *cond_zv;
6329
0
    if (!case_ast->child[0]) {
6330
      /* Skip default clause */
6331
0
      continue;
6332
0
    }
6333
6334
0
    zend_eval_const_expr(cond_ast);
6335
0
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6336
      /* Non-constant case */
6337
0
      return IS_UNDEF;
6338
0
    }
6339
6340
0
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6341
0
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6342
      /* We only optimize switched on integers and strings */
6343
0
      return IS_UNDEF;
6344
0
    }
6345
6346
0
    if (common_type == IS_UNDEF) {
6347
0
      common_type = Z_TYPE_P(cond_zv);
6348
0
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6349
      /* Non-uniform case types */
6350
0
      return IS_UNDEF;
6351
0
    }
6352
6353
0
    if (Z_TYPE_P(cond_zv) == IS_STRING
6354
0
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6355
      /* Numeric strings cannot be compared with a simple hash lookup */
6356
0
      return IS_UNDEF;
6357
0
    }
6358
0
  }
6359
6360
0
  return common_type;
6361
0
}
6362
6363
0
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6364
0
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6365
0
    return 0;
6366
0
  }
6367
6368
  /* Thresholds are chosen based on when the average switch time for equidistributed
6369
   * input becomes smaller when using the jumptable optimization. */
6370
0
  if (jumptable_type == IS_LONG) {
6371
0
    return cases->children >= 5;
6372
0
  } else {
6373
0
    ZEND_ASSERT(jumptable_type == IS_STRING);
6374
0
    return cases->children >= 2;
6375
0
  }
6376
0
}
6377
6378
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6379
0
{
6380
0
  zend_ast *expr_ast = ast->child[0];
6381
0
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6382
6383
0
  uint32_t i;
6384
0
  bool has_default_case = false;
6385
6386
0
  znode expr_node, case_node;
6387
0
  zend_op *opline;
6388
0
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6389
0
  uint8_t jumptable_type;
6390
0
  HashTable *jumptable = NULL;
6391
6392
0
  zend_compile_expr(&expr_node, expr_ast);
6393
6394
0
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6395
6396
0
  case_node.op_type = IS_TMP_VAR;
6397
0
  case_node.u.op.var = get_temporary_variable();
6398
6399
0
  jumptable_type = determine_switch_jumptable_type(cases);
6400
0
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6401
0
    znode jumptable_op;
6402
6403
0
    ALLOC_HASHTABLE(jumptable);
6404
0
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6405
0
    jumptable_op.op_type = IS_CONST;
6406
0
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6407
6408
0
    opline = zend_emit_op(NULL,
6409
0
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6410
0
      &expr_node, &jumptable_op);
6411
0
    if (opline->op1_type == IS_CONST) {
6412
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6413
0
    }
6414
0
    opnum_switch = opline - CG(active_op_array)->opcodes;
6415
0
  }
6416
6417
0
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6418
0
  for (i = 0; i < cases->children; ++i) {
6419
0
    zend_ast *case_ast = cases->child[i];
6420
0
    zend_ast *cond_ast = case_ast->child[0];
6421
0
    znode cond_node;
6422
6423
0
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6424
0
      CG(zend_lineno) = case_ast->lineno;
6425
0
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6426
0
    }
6427
6428
0
    if (!cond_ast) {
6429
0
      if (has_default_case) {
6430
0
        CG(zend_lineno) = case_ast->lineno;
6431
0
        zend_error_noreturn(E_COMPILE_ERROR,
6432
0
          "Switch statements may only contain one default clause");
6433
0
      }
6434
0
      has_default_case = true;
6435
0
      continue;
6436
0
    }
6437
6438
0
    zend_compile_expr(&cond_node, cond_ast);
6439
6440
0
    if (expr_node.op_type == IS_CONST
6441
0
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6442
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6443
0
    } else if (expr_node.op_type == IS_CONST
6444
0
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6445
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6446
0
    } else {
6447
0
      opline = zend_emit_op(NULL,
6448
0
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6449
0
        &expr_node, &cond_node);
6450
0
      SET_NODE(opline->result, &case_node);
6451
0
      if (opline->op1_type == IS_CONST) {
6452
0
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6453
0
      }
6454
6455
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6456
0
    }
6457
0
  }
6458
6459
0
  opnum_default_jmp = zend_emit_jump(0);
6460
6461
0
  for (i = 0; i < cases->children; ++i) {
6462
0
    zend_ast *case_ast = cases->child[i];
6463
0
    zend_ast *cond_ast = case_ast->child[0];
6464
0
    zend_ast *stmt_ast = case_ast->child[1];
6465
6466
0
    if (cond_ast) {
6467
0
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6468
6469
0
      if (jumptable) {
6470
0
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6471
0
        zval jmp_target;
6472
0
        ZVAL_LONG(&jmp_target, get_next_op_number());
6473
6474
0
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6475
0
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6476
0
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6477
0
        } else {
6478
0
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6479
0
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6480
0
        }
6481
0
      }
6482
0
    } else {
6483
0
      zend_update_jump_target_to_next(opnum_default_jmp);
6484
6485
0
      if (jumptable) {
6486
0
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6487
0
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6488
0
        opline->extended_value = get_next_op_number();
6489
0
      }
6490
0
    }
6491
6492
0
    zend_compile_stmt(stmt_ast);
6493
0
  }
6494
6495
0
  if (!has_default_case) {
6496
0
    zend_update_jump_target_to_next(opnum_default_jmp);
6497
6498
0
    if (jumptable) {
6499
0
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6500
0
      opline->extended_value = get_next_op_number();
6501
0
    }
6502
0
  }
6503
6504
0
  zend_end_loop(get_next_op_number(), &expr_node);
6505
6506
0
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6507
0
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6508
0
    opline->extended_value = ZEND_FREE_SWITCH;
6509
0
  } else if (expr_node.op_type == IS_CONST) {
6510
0
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6511
0
  }
6512
6513
0
  efree(jmpnz_opnums);
6514
0
}
6515
/* }}} */
6516
6517
static uint32_t count_match_conds(const zend_ast_list *arms)
6518
0
{
6519
0
  uint32_t num_conds = 0;
6520
6521
0
  for (uint32_t i = 0; i < arms->children; i++) {
6522
0
    const zend_ast *arm_ast = arms->child[i];
6523
0
    if (arm_ast->child[0] == NULL) {
6524
0
      continue;
6525
0
    }
6526
6527
0
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6528
0
    num_conds += conds->children;
6529
0
  }
6530
6531
0
  return num_conds;
6532
0
}
6533
6534
0
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6535
0
  for (uint32_t i = 0; i < arms->children; i++) {
6536
0
    const zend_ast *arm_ast = arms->child[i];
6537
0
    if (!arm_ast->child[0]) {
6538
      /* Skip default arm */
6539
0
      continue;
6540
0
    }
6541
6542
0
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6543
0
    for (uint32_t j = 0; j < conds->children; j++) {
6544
0
      zend_ast **cond_ast = &conds->child[j];
6545
6546
0
      zend_eval_const_expr(cond_ast);
6547
0
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6548
0
        return 0;
6549
0
      }
6550
6551
0
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6552
0
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6553
0
        return 0;
6554
0
      }
6555
0
    }
6556
0
  }
6557
6558
0
  return 1;
6559
0
}
6560
6561
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6562
0
{
6563
0
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6564
    /* Assert compilation adds a message operand, but this is incompatible with the
6565
     * pipe optimization that uses a temporary znode for the reference elimination.
6566
     * Therefore, disable the optimization for assert.
6567
     * Note that "assert" as a name is always treated as fully qualified. */
6568
0
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6569
0
  }
6570
6571
0
  return true;
6572
0
}
6573
6574
static void zend_compile_pipe(znode *result, zend_ast *ast)
6575
0
{
6576
0
  zend_ast *operand_ast = ast->child[0];
6577
0
  zend_ast *callable_ast = ast->child[1];
6578
6579
0
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6580
0
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6581
0
  }
6582
6583
  /* Compile the left hand side down to a value first. */
6584
0
  znode operand_result;
6585
0
  zend_compile_expr(&operand_result, operand_ast);
6586
6587
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6588
   * always fail. They will already fail in complex cases like arrays,
6589
   * so those don't need a wrapper. */
6590
0
  znode wrapped_operand_result;
6591
0
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6592
0
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6593
0
  } else {
6594
0
    wrapped_operand_result = operand_result;
6595
0
  }
6596
6597
  /* Turn the operand into a function parameter list. */
6598
0
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6599
6600
0
  zend_ast *fcall_ast;
6601
0
  znode callable_result;
6602
6603
  /* Turn $foo |> bar(...) into bar($foo). */
6604
0
  if (callable_ast->kind == ZEND_AST_CALL
6605
0
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6606
0
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6607
0
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6608
0
        callable_ast->child[0], arg_list_ast);
6609
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6610
0
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6611
0
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6612
0
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6613
0
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6614
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6615
0
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6616
0
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6617
0
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6618
0
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6619
  /* Turn $foo |> $expr into ($expr)($foo) */
6620
0
  } else {
6621
0
    zend_compile_expr(&callable_result, callable_ast);
6622
0
    callable_ast = zend_ast_create_znode(&callable_result);
6623
0
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6624
0
      callable_ast, arg_list_ast);
6625
0
  }
6626
6627
0
  zend_do_extended_stmt(&operand_result);
6628
6629
0
  zend_compile_expr(result, fcall_ast);
6630
0
}
6631
6632
static void zend_compile_match(znode *result, zend_ast *ast)
6633
0
{
6634
0
  zend_ast *expr_ast = ast->child[0];
6635
0
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6636
0
  bool has_default_arm = false;
6637
0
  uint32_t opnum_match = (uint32_t)-1;
6638
6639
0
  znode expr_node;
6640
0
  zend_compile_expr(&expr_node, expr_ast);
6641
6642
0
  znode case_node;
6643
0
  case_node.op_type = IS_TMP_VAR;
6644
0
  case_node.u.op.var = get_temporary_variable();
6645
6646
0
  uint32_t num_conds = count_match_conds(arms);
6647
0
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6648
0
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6649
0
  HashTable *jumptable = NULL;
6650
0
  uint32_t *jmpnz_opnums = NULL;
6651
6652
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6653
0
    zend_ast *arm_ast = arms->child[i];
6654
6655
0
    if (!arm_ast->child[0]) {
6656
0
      if (has_default_arm) {
6657
0
        CG(zend_lineno) = arm_ast->lineno;
6658
0
        zend_error_noreturn(E_COMPILE_ERROR,
6659
0
          "Match expressions may only contain one default arm");
6660
0
      }
6661
0
      has_default_arm = true;
6662
0
    }
6663
0
  }
6664
6665
0
  if (uses_jumptable) {
6666
0
    znode jumptable_op;
6667
6668
0
    ALLOC_HASHTABLE(jumptable);
6669
0
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6670
0
    jumptable_op.op_type = IS_CONST;
6671
0
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6672
6673
0
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6674
0
    if (opline->op1_type == IS_CONST) {
6675
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6676
0
    }
6677
0
    opnum_match = opline - CG(active_op_array)->opcodes;
6678
0
  } else {
6679
0
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6680
0
    uint32_t cond_count = 0;
6681
0
    for (uint32_t i = 0; i < arms->children; ++i) {
6682
0
      zend_ast *arm_ast = arms->child[i];
6683
6684
0
      if (!arm_ast->child[0]) {
6685
0
        continue;
6686
0
      }
6687
6688
0
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6689
0
      for (uint32_t j = 0; j < conds->children; j++) {
6690
0
        zend_ast *cond_ast = conds->child[j];
6691
6692
0
        znode cond_node;
6693
0
        zend_compile_expr(&cond_node, cond_ast);
6694
6695
0
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6696
0
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6697
0
        SET_NODE(opline->result, &case_node);
6698
0
        if (opline->op1_type == IS_CONST) {
6699
0
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6700
0
        }
6701
6702
0
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6703
6704
0
        cond_count++;
6705
0
      }
6706
0
    }
6707
0
  }
6708
6709
0
  uint32_t opnum_default_jmp = 0;
6710
0
  if (!uses_jumptable) {
6711
0
    opnum_default_jmp = zend_emit_jump(0);
6712
0
  }
6713
6714
0
  bool is_first_case = true;
6715
0
  uint32_t cond_count = 0;
6716
0
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6717
6718
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6719
  // for the arm result is freed even though it has not been initialized yet.
6720
0
  if (!has_default_arm) {
6721
0
    if (!uses_jumptable) {
6722
0
      zend_update_jump_target_to_next(opnum_default_jmp);
6723
0
    }
6724
6725
0
    if (jumptable) {
6726
0
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6727
0
      opline->extended_value = get_next_op_number();
6728
0
    }
6729
6730
0
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6731
0
    if (opline->op1_type == IS_CONST) {
6732
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6733
0
    }
6734
0
    if (arms->children == 0) {
6735
      /* Mark this as an "expression throw" for opcache. */
6736
0
      opline->extended_value = ZEND_THROW_IS_EXPR;
6737
0
    }
6738
0
  }
6739
6740
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6741
0
    zend_ast *arm_ast = arms->child[i];
6742
0
    zend_ast *body_ast = arm_ast->child[1];
6743
6744
0
    if (arm_ast->child[0] != NULL) {
6745
0
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6746
6747
0
      for (uint32_t j = 0; j < conds->children; j++) {
6748
0
        zend_ast *cond_ast = conds->child[j];
6749
6750
0
        if (jmpnz_opnums != NULL) {
6751
0
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6752
0
        }
6753
6754
0
        if (jumptable) {
6755
0
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6756
0
          zval jmp_target;
6757
0
          ZVAL_LONG(&jmp_target, get_next_op_number());
6758
6759
0
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6760
0
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6761
0
          } else {
6762
0
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6763
0
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6764
0
          }
6765
0
        }
6766
6767
0
        cond_count++;
6768
0
      }
6769
0
    } else {
6770
0
      if (!uses_jumptable) {
6771
0
        zend_update_jump_target_to_next(opnum_default_jmp);
6772
0
      }
6773
6774
0
      if (jumptable) {
6775
0
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6776
0
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6777
0
        opline->extended_value = get_next_op_number();
6778
0
      }
6779
0
    }
6780
6781
0
    znode body_node;
6782
0
    zend_compile_expr(&body_node, body_ast);
6783
6784
0
    if (is_first_case) {
6785
0
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6786
0
      is_first_case = false;
6787
0
    } else {
6788
0
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6789
0
      SET_NODE(opline_qm_assign->result, result);
6790
0
    }
6791
6792
0
    jmp_end_opnums[i] = zend_emit_jump(0);
6793
0
  }
6794
6795
  // Initialize result in case there is no arm
6796
0
  if (arms->children == 0) {
6797
0
    result->op_type = IS_CONST;
6798
0
    ZVAL_NULL(&result->u.constant);
6799
0
  }
6800
6801
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6802
0
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6803
0
  }
6804
6805
0
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6806
0
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6807
0
    opline->extended_value = ZEND_FREE_SWITCH;
6808
0
  } else if (expr_node.op_type == IS_CONST) {
6809
0
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6810
0
  }
6811
6812
0
  if (jmpnz_opnums != NULL) {
6813
0
    efree(jmpnz_opnums);
6814
0
  }
6815
0
  efree(jmp_end_opnums);
6816
0
}
6817
6818
static void zend_compile_try(const zend_ast *ast) /* {{{ */
6819
46
{
6820
46
  zend_ast *try_ast = ast->child[0];
6821
46
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6822
46
  zend_ast *finally_ast = ast->child[2];
6823
6824
46
  uint32_t i, j;
6825
46
  zend_op *opline;
6826
46
  uint32_t try_catch_offset;
6827
46
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6828
46
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6829
46
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6830
6831
46
  if (catches->children == 0 && !finally_ast) {
6832
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6833
0
  }
6834
6835
  /* label: try { } must not be equal to try { label: } */
6836
46
  if (CG(context).labels) {
6837
0
    zend_label *label;
6838
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6839
0
      if (label->opline_num == get_next_op_number()) {
6840
0
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6841
0
      }
6842
0
      break;
6843
0
    } ZEND_HASH_FOREACH_END();
6844
0
  }
6845
6846
46
  try_catch_offset = zend_add_try_element(get_next_op_number());
6847
6848
46
  if (finally_ast) {
6849
0
    zend_loop_var fast_call;
6850
0
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6851
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6852
0
    }
6853
0
    CG(context).fast_call_var = get_temporary_variable();
6854
6855
    /* Push FAST_CALL on unwind stack */
6856
0
    fast_call.opcode = ZEND_FAST_CALL;
6857
0
    fast_call.var_type = IS_TMP_VAR;
6858
0
    fast_call.var_num = CG(context).fast_call_var;
6859
0
    fast_call.try_catch_offset = try_catch_offset;
6860
0
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6861
0
  }
6862
6863
46
  CG(context).try_catch_offset = try_catch_offset;
6864
6865
46
  zend_compile_stmt(try_ast);
6866
6867
46
  if (catches->children != 0) {
6868
46
    jmp_opnums[0] = zend_emit_jump(0);
6869
46
  }
6870
6871
123
  for (i = 0; i < catches->children; ++i) {
6872
77
    const zend_ast *catch_ast = catches->child[i];
6873
77
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6874
77
    zend_ast *var_ast = catch_ast->child[1];
6875
77
    zend_ast *stmt_ast = catch_ast->child[2];
6876
77
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6877
77
    bool is_last_catch = (i + 1 == catches->children);
6878
6879
77
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6880
77
    uint32_t opnum_catch = (uint32_t)-1;
6881
6882
77
    CG(zend_lineno) = catch_ast->lineno;
6883
6884
154
    for (j = 0; j < classes->children; j++) {
6885
77
      zend_ast *class_ast = classes->child[j];
6886
77
      bool is_last_class = (j + 1 == classes->children);
6887
6888
77
      if (!zend_is_const_default_class_ref(class_ast)) {
6889
0
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6890
0
      }
6891
6892
77
      opnum_catch = get_next_op_number();
6893
77
      if (i == 0 && j == 0) {
6894
46
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6895
46
      }
6896
6897
77
      opline = get_next_op();
6898
77
      opline->opcode = ZEND_CATCH;
6899
77
      opline->op1_type = IS_CONST;
6900
77
      opline->op1.constant = zend_add_class_name_literal(
6901
77
          zend_resolve_class_name_ast(class_ast));
6902
77
      opline->extended_value = zend_alloc_cache_slot();
6903
6904
77
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6905
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6906
0
      }
6907
6908
77
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6909
77
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6910
6911
77
      if (is_last_catch && is_last_class) {
6912
46
        opline->extended_value |= ZEND_LAST_CATCH;
6913
46
      }
6914
6915
77
      if (!is_last_class) {
6916
0
        jmp_multicatch[j] = zend_emit_jump(0);
6917
0
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6918
0
        opline->op2.opline_num = get_next_op_number();
6919
0
      }
6920
77
    }
6921
6922
77
    for (j = 0; j < classes->children - 1; j++) {
6923
0
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6924
0
    }
6925
6926
77
    efree(jmp_multicatch);
6927
6928
77
    zend_compile_stmt(stmt_ast);
6929
6930
77
    if (!is_last_catch) {
6931
31
      jmp_opnums[i + 1] = zend_emit_jump(0);
6932
31
    }
6933
6934
77
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6935
77
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6936
77
    if (!is_last_catch) {
6937
31
      opline->op2.opline_num = get_next_op_number();
6938
31
    }
6939
77
  }
6940
6941
123
  for (i = 0; i < catches->children; ++i) {
6942
77
    zend_update_jump_target_to_next(jmp_opnums[i]);
6943
77
  }
6944
6945
46
  if (finally_ast) {
6946
0
    zend_loop_var discard_exception;
6947
0
    uint32_t opnum_jmp = get_next_op_number() + 1;
6948
6949
    /* Pop FAST_CALL from unwind stack */
6950
0
    zend_stack_del_top(&CG(loop_var_stack));
6951
6952
    /* Push DISCARD_EXCEPTION on unwind stack */
6953
0
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6954
0
    discard_exception.var_type = IS_TMP_VAR;
6955
0
    discard_exception.var_num = CG(context).fast_call_var;
6956
0
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6957
6958
0
    CG(zend_lineno) = finally_ast->lineno;
6959
6960
0
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6961
0
    opline->op1.num = try_catch_offset;
6962
0
    opline->result_type = IS_TMP_VAR;
6963
0
    opline->result.var = CG(context).fast_call_var;
6964
6965
0
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6966
6967
0
    zend_compile_stmt(finally_ast);
6968
6969
0
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6970
0
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6971
0
      = get_next_op_number();
6972
6973
0
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6974
0
    opline->op1_type = IS_TMP_VAR;
6975
0
    opline->op1.var = CG(context).fast_call_var;
6976
0
    opline->op2.num = orig_try_catch_offset;
6977
6978
0
    zend_update_jump_target_to_next(opnum_jmp);
6979
6980
0
    CG(context).fast_call_var = orig_fast_call_var;
6981
6982
    /* Pop DISCARD_EXCEPTION from unwind stack */
6983
0
    zend_stack_del_top(&CG(loop_var_stack));
6984
0
  }
6985
6986
46
  CG(context).try_catch_offset = orig_try_catch_offset;
6987
6988
46
  efree(jmp_opnums);
6989
46
}
6990
/* }}} */
6991
6992
/* Encoding declarations must already be handled during parsing */
6993
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
6994
0
{
6995
0
  const zend_ast_list *declares = zend_ast_get_list(ast);
6996
0
  uint32_t i;
6997
0
  for (i = 0; i < declares->children; ++i) {
6998
0
    const zend_ast *declare_ast = declares->child[i];
6999
0
    zend_ast *name_ast = declare_ast->child[0];
7000
0
    zend_ast *value_ast = declare_ast->child[1];
7001
0
    const zend_string *name = zend_ast_get_str(name_ast);
7002
7003
0
    if (zend_string_equals_literal_ci(name, "encoding")) {
7004
0
      if (value_ast->kind != ZEND_AST_ZVAL) {
7005
0
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7006
0
        return 0;
7007
0
      }
7008
7009
0
      if (CG(multibyte)) {
7010
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7011
7012
0
        const zend_encoding *new_encoding, *old_encoding;
7013
0
        zend_encoding_filter old_input_filter;
7014
7015
0
        CG(encoding_declared) = 1;
7016
7017
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7018
0
        if (!new_encoding) {
7019
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7020
0
        } else {
7021
0
          old_input_filter = LANG_SCNG(input_filter);
7022
0
          old_encoding = LANG_SCNG(script_encoding);
7023
0
          zend_multibyte_set_filter(new_encoding);
7024
7025
          /* need to re-scan if input filter changed */
7026
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7027
0
             (old_input_filter && new_encoding != old_encoding)) {
7028
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7029
0
          }
7030
0
        }
7031
7032
0
        zend_string_release_ex(encoding_name, 0);
7033
0
      } else {
7034
0
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7035
0
          "Zend multibyte feature is turned off by settings");
7036
0
      }
7037
0
    }
7038
0
  }
7039
7040
0
  return 1;
7041
0
}
7042
/* }}} */
7043
7044
/* Check whether this is the first statement, not counting declares. */
7045
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7046
0
{
7047
0
  uint32_t i = 0;
7048
0
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7049
7050
0
  while (i < file_ast->children) {
7051
0
    if (file_ast->child[i] == ast) {
7052
0
      return SUCCESS;
7053
0
    } else if (file_ast->child[i] == NULL) {
7054
0
      if (!allow_nop) {
7055
0
        return FAILURE;
7056
0
      }
7057
0
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7058
0
      return FAILURE;
7059
0
    }
7060
0
    i++;
7061
0
  }
7062
0
  return FAILURE;
7063
0
}
7064
/* }}} */
7065
7066
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7067
0
{
7068
0
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7069
0
  zend_ast *stmt_ast = ast->child[1];
7070
0
  zend_declarables orig_declarables = FC(declarables);
7071
0
  uint32_t i;
7072
7073
0
  for (i = 0; i < declares->children; ++i) {
7074
0
    zend_ast *declare_ast = declares->child[i];
7075
0
    zend_ast *name_ast = declare_ast->child[0];
7076
0
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7077
0
    zend_string *name = zend_ast_get_str(name_ast);
7078
7079
0
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7080
0
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7081
0
    }
7082
7083
0
    if (zend_string_equals_literal_ci(name, "ticks")) {
7084
0
      zval value_zv;
7085
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7086
0
      FC(declarables).ticks = zval_get_long(&value_zv);
7087
0
      zval_ptr_dtor_nogc(&value_zv);
7088
0
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7089
7090
0
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7091
0
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7092
0
          "the very first statement in the script");
7093
0
      }
7094
0
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7095
0
      zval value_zv;
7096
7097
0
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7098
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7099
0
          "the very first statement in the script");
7100
0
      }
7101
7102
0
      if (ast->child[1] != NULL) {
7103
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7104
0
          "use block mode");
7105
0
      }
7106
7107
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7108
7109
0
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7110
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7111
0
      }
7112
7113
0
      if (Z_LVAL(value_zv) == 1) {
7114
0
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7115
0
      }
7116
7117
0
    } else {
7118
0
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7119
0
    }
7120
0
  }
7121
7122
0
  if (stmt_ast) {
7123
0
    zend_compile_stmt(stmt_ast);
7124
7125
0
    FC(declarables) = orig_declarables;
7126
0
  }
7127
0
}
7128
/* }}} */
7129
7130
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7131
189
{
7132
189
  const zend_ast_list *list = zend_ast_get_list(ast);
7133
189
  uint32_t i;
7134
489
  for (i = 0; i < list->children; ++i) {
7135
300
    zend_compile_stmt(list->child[i]);
7136
300
  }
7137
189
}
7138
/* }}} */
7139
7140
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7141
3.35k
{
7142
3.35k
  uint32_t i, n;
7143
7144
3.35k
  func->common.arg_flags[0] = 0;
7145
3.35k
  func->common.arg_flags[1] = 0;
7146
3.35k
  func->common.arg_flags[2] = 0;
7147
3.35k
  if (func->common.arg_info) {
7148
3.35k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7149
3.35k
    i = 0;
7150
6.81k
    while (i < n) {
7151
3.46k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7152
3.46k
      i++;
7153
3.46k
    }
7154
3.35k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7155
8
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7156
92
      while (i < MAX_ARG_FLAG_NUM) {
7157
84
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7158
84
        i++;
7159
84
      }
7160
8
    }
7161
3.35k
  }
7162
3.35k
}
7163
/* }}} */
7164
7165
static zend_type zend_compile_single_typename(zend_ast *ast)
7166
3
{
7167
3
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7168
3
  if (ast->kind == ZEND_AST_TYPE) {
7169
0
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7170
0
      zend_error_noreturn(E_COMPILE_ERROR,
7171
0
        "Cannot use \"static\" when no class scope is active");
7172
0
    }
7173
7174
0
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7175
3
  } else {
7176
3
    zend_string *type_name = zend_ast_get_str(ast);
7177
3
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7178
7179
3
    if (type_code != 0) {
7180
3
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7181
0
        zend_error_noreturn(E_COMPILE_ERROR,
7182
0
          "Type declaration '%s' must be unqualified",
7183
0
          ZSTR_VAL(zend_string_tolower(type_name)));
7184
0
      }
7185
7186
      /* Transform iterable into a type union alias */
7187
3
      if (type_code == IS_ITERABLE) {
7188
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7189
0
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7190
0
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7191
0
        return iterable;
7192
0
      }
7193
7194
3
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7195
3
    } else {
7196
0
      const char *correct_name;
7197
0
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7198
0
      zend_string *class_name = type_name;
7199
7200
0
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7201
0
        class_name = zend_resolve_class_name_ast(ast);
7202
0
        zend_assert_valid_class_name(class_name, "a type name");
7203
0
      } else {
7204
0
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7205
7206
0
        zend_ensure_valid_class_fetch_type(fetch_type);
7207
7208
0
        bool substitute_self_parent = zend_is_scope_known()
7209
0
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7210
7211
0
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7212
          /* Scope might be unknown for unbound closures and traits */
7213
0
          if (substitute_self_parent) {
7214
0
            class_name = CG(active_class_entry)->name;
7215
0
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7216
0
          }
7217
0
        } else {
7218
0
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7219
          /* Scope might be unknown for unbound closures and traits */
7220
0
          if (substitute_self_parent) {
7221
0
            class_name = CG(active_class_entry)->parent_name;
7222
0
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7223
0
          }
7224
0
        }
7225
0
        zend_string_addref(class_name);
7226
0
      }
7227
7228
0
      if (ast->attr == ZEND_NAME_NOT_FQ
7229
0
          && zend_is_confusable_type(type_name, &correct_name)
7230
0
          && zend_is_not_imported(type_name)) {
7231
0
        const char *extra =
7232
0
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7233
0
        if (correct_name) {
7234
0
          zend_error(E_COMPILE_WARNING,
7235
0
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7236
0
            "Write \"\\%s\"%s to suppress this warning",
7237
0
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7238
0
        } else {
7239
0
          zend_error(E_COMPILE_WARNING,
7240
0
            "\"%s\" is not a supported builtin type "
7241
0
            "and will be interpreted as a class name. "
7242
0
            "Write \"\\%s\"%s to suppress this warning",
7243
0
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7244
0
        }
7245
0
      }
7246
7247
0
      class_name = zend_new_interned_string(class_name);
7248
0
      zend_alloc_ce_cache(class_name);
7249
0
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7250
0
    }
7251
3
  }
7252
3
}
7253
7254
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7255
0
{
7256
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7257
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7258
0
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7259
0
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7260
0
  const zend_type_list *smaller_type_list, *larger_type_list;
7261
0
  bool flipped = false;
7262
7263
0
  if (r_type_list->num_types < l_type_list->num_types) {
7264
0
    smaller_type_list = r_type_list;
7265
0
    larger_type_list = l_type_list;
7266
0
    flipped = true;
7267
0
  } else {
7268
0
    smaller_type_list = l_type_list;
7269
0
    larger_type_list = r_type_list;
7270
0
  }
7271
7272
0
  unsigned int sum = 0;
7273
0
  const zend_type *outer_type;
7274
0
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7275
0
    const zend_type *inner_type;
7276
0
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7277
0
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7278
0
        sum++;
7279
0
        break;
7280
0
      }
7281
0
    ZEND_TYPE_LIST_FOREACH_END();
7282
0
  ZEND_TYPE_LIST_FOREACH_END();
7283
7284
0
  if (sum == smaller_type_list->num_types) {
7285
0
    zend_string *smaller_type_str;
7286
0
    zend_string *larger_type_str;
7287
0
    if (flipped) {
7288
0
      smaller_type_str = zend_type_to_string(right_type);
7289
0
      larger_type_str = zend_type_to_string(left_type);
7290
0
    } else {
7291
0
      smaller_type_str = zend_type_to_string(left_type);
7292
0
      larger_type_str = zend_type_to_string(right_type);
7293
0
    }
7294
0
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7295
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7296
0
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7297
0
    } else {
7298
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7299
0
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7300
0
    }
7301
0
  }
7302
0
}
7303
7304
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7305
0
{
7306
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7307
0
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7308
7309
0
  const zend_type *single_intersection_type = NULL;
7310
0
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7311
0
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7312
0
      zend_string *single_type_str = zend_type_to_string(single_type);
7313
0
      zend_string *complete_type = zend_type_to_string(intersection_type);
7314
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7315
0
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7316
0
    }
7317
0
  ZEND_TYPE_FOREACH_END();
7318
0
}
7319
7320
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7321
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7322
0
{
7323
0
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7324
0
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7325
0
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7326
0
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7327
0
      continue;
7328
0
    }
7329
0
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7330
0
      zend_string *single_type_str = zend_type_to_string(type);
7331
0
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7332
0
    }
7333
0
  }
7334
0
}
7335
7336
static zend_type zend_compile_typename(zend_ast *ast);
7337
7338
static zend_type zend_compile_typename_ex(
7339
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7340
3
{
7341
3
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7342
3
  zend_ast_attr orig_ast_attr = ast->attr;
7343
3
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7344
7345
3
  if (is_marked_nullable) {
7346
0
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7347
0
  }
7348
7349
3
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7350
0
    const zend_ast_list *list = zend_ast_get_list(ast);
7351
0
    zend_type_list *type_list;
7352
0
    bool is_composite = false;
7353
0
    bool has_only_iterable_class = true;
7354
0
    ALLOCA_FLAG(use_heap)
7355
7356
0
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7357
0
    type_list->num_types = 0;
7358
7359
0
    for (uint32_t i = 0; i < list->children; i++) {
7360
0
      zend_ast *type_ast = list->child[i];
7361
0
      zend_type single_type;
7362
0
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7363
7364
0
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7365
0
        has_only_iterable_class = false;
7366
0
        is_composite = true;
7367
        /* The first class type can be stored directly as the type ptr payload. */
7368
0
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7369
          /* Switch from single name to name list. */
7370
0
          type_list->num_types = 1;
7371
0
          type_list->types[0] = type;
7372
          /* Clear MAY_BE_* type flags */
7373
0
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7374
0
        }
7375
        /* Mark type as list type */
7376
0
        ZEND_TYPE_SET_LIST(type, type_list);
7377
7378
0
        single_type = zend_compile_typename(type_ast);
7379
0
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7380
7381
0
        type_list->types[type_list->num_types++] = single_type;
7382
7383
        /* Check for trivially redundant class types */
7384
0
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7385
0
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7386
0
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7387
0
            continue;
7388
0
          }
7389
          /* Type from type list is a simple type */
7390
0
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7391
0
        }
7392
0
        continue;
7393
0
      }
7394
7395
0
      single_type = zend_compile_single_typename(type_ast);
7396
0
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7397
7398
0
      if (single_type_mask == MAY_BE_ANY) {
7399
0
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7400
0
      }
7401
0
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7402
0
        has_only_iterable_class = false;
7403
0
      }
7404
7405
0
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7406
0
      if (type_mask_overlap) {
7407
0
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7408
0
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7409
0
        zend_error_noreturn(E_COMPILE_ERROR,
7410
0
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7411
0
      }
7412
7413
0
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7414
0
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7415
0
        zend_error_noreturn(E_COMPILE_ERROR,
7416
0
          "Type contains both true and false, bool must be used instead");
7417
0
      }
7418
0
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7419
      /* Clear MAY_BE_* type flags */
7420
0
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7421
7422
0
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7423
0
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7424
          /* The first class type can be stored directly as the type ptr payload. */
7425
0
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7426
0
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7427
0
        } else {
7428
0
          if (type_list->num_types == 0) {
7429
            /* Switch from single name to name list. */
7430
0
            type_list->num_types = 1;
7431
0
            type_list->types[0] = type;
7432
            /* Clear MAY_BE_* type flags */
7433
0
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7434
0
            ZEND_TYPE_SET_LIST(type, type_list);
7435
0
          }
7436
7437
0
          type_list->types[type_list->num_types++] = single_type;
7438
7439
          /* Check for trivially redundant class types */
7440
0
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7441
0
        }
7442
0
      }
7443
0
    }
7444
7445
0
    if (type_list->num_types) {
7446
0
      zend_type_list *list = zend_arena_alloc(
7447
0
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7448
0
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7449
0
      ZEND_TYPE_SET_LIST(type, list);
7450
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7451
      /* Inform that the type list is a union type */
7452
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7453
0
    }
7454
7455
0
    free_alloca(type_list, use_heap);
7456
7457
0
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7458
0
    if ((type_mask & MAY_BE_OBJECT) &&
7459
0
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7460
0
      zend_string *type_str = zend_type_to_string(type);
7461
0
      zend_error_noreturn(E_COMPILE_ERROR,
7462
0
        "Type %s contains both object and a class type, which is redundant",
7463
0
        ZSTR_VAL(type_str));
7464
0
    }
7465
3
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7466
0
    const zend_ast_list *list = zend_ast_get_list(ast);
7467
0
    zend_type_list *type_list;
7468
7469
    /* Allocate the type list directly on the arena as it must be a type
7470
     * list of the same number of elements as the AST list has children */
7471
0
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7472
0
    type_list->num_types = 0;
7473
7474
0
    ZEND_ASSERT(list->children > 1);
7475
7476
0
    for (uint32_t i = 0; i < list->children; i++) {
7477
0
      zend_ast *type_ast = list->child[i];
7478
0
      zend_type single_type = zend_compile_single_typename(type_ast);
7479
7480
      /* An intersection of union types cannot exist so invalidate it
7481
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7482
0
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7483
0
        zend_string *standard_type_str = zend_type_to_string(single_type);
7484
0
        zend_error_noreturn(E_COMPILE_ERROR,
7485
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7486
0
        zend_string_release_ex(standard_type_str, false);
7487
0
      }
7488
      /* An intersection of standard types cannot exist so invalidate it */
7489
0
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7490
0
        zend_string *standard_type_str = zend_type_to_string(single_type);
7491
0
        zend_error_noreturn(E_COMPILE_ERROR,
7492
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7493
0
        zend_string_release_ex(standard_type_str, false);
7494
0
      }
7495
      /* Check for "self" and "parent" too */
7496
0
      if (
7497
0
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7498
0
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7499
0
      ) {
7500
0
        zend_error_noreturn(E_COMPILE_ERROR,
7501
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7502
0
      }
7503
7504
      /* Add type to the type list */
7505
0
      type_list->types[type_list->num_types++] = single_type;
7506
7507
      /* Check for trivially redundant class types */
7508
0
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7509
0
    }
7510
7511
0
    ZEND_ASSERT(list->children == type_list->num_types);
7512
7513
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7514
0
    if (force_allow_null) {
7515
0
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7516
0
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7517
0
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7518
0
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7519
7520
0
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7521
0
      dnf_type_list->num_types = 1;
7522
0
      dnf_type_list->types[0] = intersection_type;
7523
0
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7524
      /* Inform that the type list is a DNF type */
7525
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7526
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7527
0
    } else {
7528
0
      ZEND_TYPE_SET_LIST(type, type_list);
7529
      /* Inform that the type list is an intersection type */
7530
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7531
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7532
0
    }
7533
3
  } else {
7534
3
    type = zend_compile_single_typename(ast);
7535
3
  }
7536
7537
3
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7538
7539
3
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7540
0
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7541
0
  }
7542
7543
3
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7544
0
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7545
0
  }
7546
7547
3
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7548
0
    *forced_allow_null = true;
7549
0
  }
7550
7551
3
  if (is_marked_nullable || force_allow_null) {
7552
0
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7553
0
    type_mask = ZEND_TYPE_PURE_MASK(type);
7554
0
  }
7555
7556
3
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7557
0
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7558
0
  }
7559
7560
3
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7561
0
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7562
0
  }
7563
7564
3
  ast->attr = orig_ast_attr;
7565
3
  return type;
7566
3
}
7567
/* }}} */
7568
7569
static zend_type zend_compile_typename(zend_ast *ast)
7570
3
{
7571
3
  bool forced_allow_null;
7572
3
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7573
3
}
7574
7575
/* May convert value from int to float. */
7576
static bool zend_is_valid_default_value(zend_type type, zval *value)
7577
0
{
7578
0
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7579
0
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7580
0
    return 1;
7581
0
  }
7582
0
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7583
    /* Integers are allowed as initializers for floating-point values. */
7584
0
    convert_to_double(value);
7585
0
    return 1;
7586
0
  }
7587
0
  return 0;
7588
0
}
7589
7590
static void zend_compile_attributes(
7591
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7592
0
) /* {{{ */ {
7593
0
  zend_attribute *attr;
7594
0
  zend_internal_attribute *config;
7595
7596
0
  const zend_ast_list *list = zend_ast_get_list(ast);
7597
0
  uint32_t g, i, j;
7598
7599
0
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7600
7601
0
  for (g = 0; g < list->children; g++) {
7602
0
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7603
7604
0
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7605
7606
0
    for (i = 0; i < group->children; i++) {
7607
0
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7608
7609
0
      const zend_ast *el = group->child[i];
7610
7611
0
      if (el->child[1] &&
7612
0
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7613
0
          zend_error_noreturn(E_COMPILE_ERROR,
7614
0
              "Cannot create Closure as attribute argument");
7615
0
      }
7616
7617
0
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7618
0
      zend_string *lcname = zend_string_tolower_ex(name, false);
7619
0
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7620
7621
0
      config = zend_internal_attribute_get(lcname);
7622
0
      zend_string_release(lcname);
7623
7624
      /* Exclude internal attributes that do not match on promoted properties. */
7625
0
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7626
0
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7627
0
          zend_string_release(name);
7628
0
          continue;
7629
0
        }
7630
0
      }
7631
7632
0
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7633
0
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7634
0
      attr = zend_add_attribute(
7635
0
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7636
0
      zend_string_release(name);
7637
7638
      /* Populate arguments */
7639
0
      if (args) {
7640
0
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7641
7642
0
        bool uses_named_args = false;
7643
0
        for (j = 0; j < args->children; j++) {
7644
0
          zend_ast **arg_ast_ptr = &args->child[j];
7645
0
          zend_ast *arg_ast = *arg_ast_ptr;
7646
7647
0
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7648
0
            zend_error_noreturn(E_COMPILE_ERROR,
7649
0
              "Cannot use unpacking in attribute argument list");
7650
0
          }
7651
7652
0
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7653
0
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7654
0
            arg_ast_ptr = &arg_ast->child[1];
7655
0
            uses_named_args = true;
7656
7657
0
            for (uint32_t k = 0; k < j; k++) {
7658
0
              if (attr->args[k].name &&
7659
0
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7660
0
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7661
0
                  ZSTR_VAL(attr->args[j].name));
7662
0
              }
7663
0
            }
7664
0
          } else if (uses_named_args) {
7665
0
            zend_error_noreturn(E_COMPILE_ERROR,
7666
0
              "Cannot use positional argument after named argument");
7667
0
          }
7668
7669
0
          zend_const_expr_to_zval(
7670
0
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7671
0
        }
7672
0
      }
7673
0
    }
7674
0
  }
7675
7676
0
  if (*attributes != NULL) {
7677
    /* Allow delaying target validation for forward compatibility. */
7678
0
    const zend_attribute *delayed_target_validation = NULL;
7679
0
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7680
0
      ZEND_ASSERT(offset >= 1);
7681
      /* zend_get_parameter_attribute_str will add 1 too */
7682
0
      delayed_target_validation = zend_get_parameter_attribute_str(
7683
0
        *attributes,
7684
0
        "delayedtargetvalidation",
7685
0
        strlen("delayedtargetvalidation"),
7686
0
        offset - 1
7687
0
      );
7688
0
    } else {
7689
0
      delayed_target_validation = zend_get_attribute_str(
7690
0
        *attributes,
7691
0
        "delayedtargetvalidation",
7692
0
        strlen("delayedtargetvalidation")
7693
0
      );
7694
0
    }
7695
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7696
0
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7697
0
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7698
0
        continue;
7699
0
      }
7700
7701
0
      bool run_validator = true;
7702
0
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7703
0
        if (delayed_target_validation == NULL) {
7704
0
          zend_string *location = zend_get_attribute_target_names(target);
7705
0
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7706
7707
0
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7708
0
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7709
0
          );
7710
0
        }
7711
0
        run_validator = false;
7712
0
      }
7713
7714
0
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7715
0
        if (zend_is_attribute_repeated(*attributes, attr)) {
7716
0
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7717
0
        }
7718
0
      }
7719
7720
      /* Validators are not run if the target is already invalid */
7721
0
      if (run_validator && config->validator != NULL) {
7722
0
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7723
0
        if (error != NULL) {
7724
0
          if (delayed_target_validation == NULL) {
7725
0
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7726
0
            zend_string_efree(error);
7727
0
          } else {
7728
0
            attr->validation_error = error;
7729
0
          }
7730
0
        }
7731
0
      }
7732
0
    } ZEND_HASH_FOREACH_END();
7733
0
  }
7734
0
}
7735
/* }}} */
7736
7737
static void zend_compile_property_hooks(
7738
    zend_property_info *prop_info, zend_string *prop_name,
7739
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7740
7741
typedef struct {
7742
  const zend_string *property_name;
7743
  bool uses_property;
7744
} find_property_usage_context;
7745
7746
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7747
0
{
7748
0
  zend_ast *ast = *ast_ptr;
7749
0
  find_property_usage_context *context = (find_property_usage_context *) _context;
7750
7751
0
  if (ast == NULL) {
7752
0
    return;
7753
0
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7754
0
    const zend_ast *object_ast = ast->child[0];
7755
0
    zend_ast *property_ast = ast->child[1];
7756
7757
0
    if (object_ast->kind == ZEND_AST_VAR
7758
0
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7759
0
     && property_ast->kind == ZEND_AST_ZVAL) {
7760
0
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7761
0
      const zval *property = zend_ast_get_zval(property_ast);
7762
0
      if (Z_TYPE_P(object) == IS_STRING
7763
0
        && Z_TYPE_P(property) == IS_STRING
7764
0
        && zend_string_equals_literal(Z_STR_P(object), "this")
7765
0
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7766
0
        context->uses_property = true;
7767
        /* No need to look for references in this branch. */
7768
0
        return;
7769
0
      }
7770
0
    }
7771
0
  }
7772
7773
  /* Don't search across function/class boundaries. */
7774
0
  if (!zend_ast_is_special(ast)) {
7775
0
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7776
0
  }
7777
0
}
7778
7779
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7780
0
{
7781
0
  if (zend_string_equals_literal_ci(hook_name, "set")
7782
0
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7783
0
    return true;
7784
0
  }
7785
7786
0
  find_property_usage_context context = { property_name, false };
7787
0
  zend_property_hook_find_property_usage(&hook_ast, &context);
7788
0
  return context.uses_property;
7789
0
}
7790
7791
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7792
0
{
7793
0
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7794
0
    return true;
7795
0
  }
7796
0
  if (!hooks_ast) {
7797
0
    return false;
7798
0
  }
7799
7800
0
  bool is_virtual = true;
7801
7802
0
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7803
0
  for (uint32_t i = 0; i < hooks->children; i++) {
7804
0
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
7805
0
    zend_ast *body = hook->child[2];
7806
0
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7807
0
      is_virtual = false;
7808
0
    }
7809
0
  }
7810
7811
0
  return is_virtual;
7812
0
}
7813
7814
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7815
21
{
7816
21
  zend_ast_list *list = zend_ast_get_list(ast);
7817
21
  uint32_t i;
7818
21
  zend_op_array *op_array = CG(active_op_array);
7819
21
  zend_arg_info *arg_infos;
7820
7821
21
  if (return_type_ast || fallback_return_type) {
7822
    /* Use op_array->arg_info[-1] for return type */
7823
1
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7824
1
    arg_infos->name = NULL;
7825
1
    if (return_type_ast) {
7826
1
      arg_infos->type = zend_compile_typename(return_type_ast);
7827
1
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7828
1
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7829
1
    } else {
7830
0
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7831
0
    }
7832
1
    arg_infos++;
7833
1
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7834
7835
1
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7836
0
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7837
0
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7838
0
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7839
0
      zend_string_release(func_name);
7840
0
    }
7841
20
  } else {
7842
20
    if (list->children == 0) {
7843
12
      return;
7844
12
    }
7845
8
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7846
8
  }
7847
7848
  /* Find last required parameter number for deprecation message. */
7849
9
  uint32_t last_required_param = (uint32_t) -1;
7850
17
  for (i = 0; i < list->children; ++i) {
7851
8
    zend_ast *param_ast = list->child[i];
7852
8
    zend_ast *default_ast_ptr = param_ast->child[2];
7853
8
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7854
8
    if (!default_ast_ptr && !is_variadic) {
7855
8
      last_required_param = i;
7856
8
    }
7857
8
  }
7858
7859
9
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
7860
17
  for (i = 0; i < list->children; ++i) {
7861
8
    zend_ast *param_ast = list->child[i];
7862
8
    zend_ast *type_ast = param_ast->child[0];
7863
8
    zend_ast *var_ast = param_ast->child[1];
7864
8
    zend_ast **default_ast_ptr = &param_ast->child[2];
7865
8
    zend_ast *attributes_ast = param_ast->child[3];
7866
8
    zend_ast *doc_comment_ast = param_ast->child[4];
7867
8
    zend_ast *hooks_ast = param_ast->child[5];
7868
8
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7869
8
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7870
8
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7871
8
    uint32_t property_flags = param_ast->attr & promotion_flags;
7872
8
    bool is_promoted = property_flags || hooks_ast;
7873
7874
8
    CG(zend_lineno) = param_ast->lineno;
7875
7876
8
    znode var_node, default_node;
7877
8
    uint8_t opcode;
7878
8
    zend_op *opline;
7879
8
    zend_arg_info *arg_info;
7880
7881
8
    if (zend_is_auto_global(name)) {
7882
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7883
0
        ZSTR_VAL(name));
7884
0
    }
7885
7886
8
    var_node.op_type = IS_CV;
7887
8
    var_node.u.op.var = lookup_cv(name);
7888
7889
8
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7890
0
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7891
0
        ZSTR_VAL(name));
7892
8
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7893
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7894
8
    } else if (zend_string_equals_literal(name, "http_response_header")) {
7895
0
      CG(context).has_assigned_to_http_response_header = true;
7896
0
    }
7897
7898
8
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7899
0
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7900
0
    }
7901
7902
8
    if (is_variadic) {
7903
0
      opcode = ZEND_RECV_VARIADIC;
7904
0
      default_node.op_type = IS_UNUSED;
7905
0
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7906
7907
0
      if (*default_ast_ptr) {
7908
0
        zend_error_noreturn(E_COMPILE_ERROR,
7909
0
          "Variadic parameter cannot have a default value");
7910
0
      }
7911
8
    } else if (*default_ast_ptr) {
7912
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7913
0
      uint32_t cops = CG(compiler_options);
7914
0
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7915
0
      opcode = ZEND_RECV_INIT;
7916
0
      default_node.op_type = IS_CONST;
7917
0
      zend_const_expr_to_zval(
7918
0
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7919
0
      CG(compiler_options) = cops;
7920
8
    } else {
7921
8
      opcode = ZEND_RECV;
7922
8
      default_node.op_type = IS_UNUSED;
7923
8
      op_array->required_num_args = i + 1;
7924
8
    }
7925
7926
8
    arg_info = &arg_infos[i];
7927
8
    arg_info->name = zend_string_copy(name);
7928
8
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7929
8
    arg_info->default_value = NULL;
7930
7931
8
    if (attributes_ast) {
7932
0
      zend_compile_attributes(
7933
0
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7934
0
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7935
0
      );
7936
0
    }
7937
7938
8
    bool forced_allow_nullable = false;
7939
8
    if (type_ast) {
7940
0
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7941
0
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7942
7943
0
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7944
0
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7945
0
      if (forced_allow_nullable) {
7946
0
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7947
0
        zend_error(E_DEPRECATED,
7948
0
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7949
0
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7950
0
        zend_string_release(func_name);
7951
0
      }
7952
7953
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7954
0
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7955
0
      }
7956
7957
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7958
0
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7959
0
      }
7960
7961
0
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7962
0
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7963
0
        zend_string *type_str = zend_type_to_string(arg_info->type);
7964
0
        zend_error_noreturn(E_COMPILE_ERROR,
7965
0
          "Cannot use %s as default value for parameter $%s of type %s",
7966
0
          zend_get_type_by_const(default_type),
7967
0
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7968
0
      }
7969
0
    }
7970
8
    if (last_required_param != (uint32_t) -1
7971
8
     && i < last_required_param
7972
0
     && default_node.op_type == IS_CONST) {
7973
      /* Ignore parameters of the form "Type $param = null".
7974
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7975
0
      if (!forced_allow_nullable) {
7976
0
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7977
0
        zend_ast *required_param_ast = list->child[last_required_param];
7978
0
        zend_error(E_DEPRECATED,
7979
0
          "%s(): Optional parameter $%s declared before required parameter $%s "
7980
0
          "is implicitly treated as a required parameter",
7981
0
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7982
0
        zend_string_release(func_name);
7983
0
      }
7984
7985
      /* Regardless of whether we issue a deprecation, convert this parameter into
7986
       * a required parameter without a default value. This ensures that it cannot be
7987
       * used as an optional parameter even with named parameters. */
7988
0
      opcode = ZEND_RECV;
7989
0
      default_node.op_type = IS_UNUSED;
7990
0
      zval_ptr_dtor(&default_node.u.constant);
7991
0
    }
7992
7993
8
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
7994
8
    SET_NODE(opline->result, &var_node);
7995
8
    opline->op1.num = i + 1;
7996
7997
8
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
7998
8
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
7999
8
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8000
8
    if (opcode == ZEND_RECV) {
8001
8
      opline->op2.num = type_ast ?
8002
8
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8003
8
    }
8004
8005
8
    if (is_promoted) {
8006
0
      const zend_op_array *active_op_array = CG(active_op_array);
8007
0
      zend_class_entry *scope = active_op_array->scope;
8008
8009
0
      bool is_ctor =
8010
0
        scope && zend_is_constructor(active_op_array->function_name);
8011
0
      if (!is_ctor) {
8012
0
        zend_error_noreturn(E_COMPILE_ERROR,
8013
0
          "Cannot declare promoted property outside a constructor");
8014
0
      }
8015
0
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8016
0
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8017
0
        zend_error_noreturn(E_COMPILE_ERROR,
8018
0
          "Cannot declare promoted property in an abstract constructor");
8019
0
      }
8020
0
      if (is_variadic) {
8021
0
        zend_error_noreturn(E_COMPILE_ERROR,
8022
0
          "Cannot declare variadic promoted property");
8023
0
      }
8024
0
      if (zend_hash_exists(&scope->properties_info, name)) {
8025
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8026
0
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8027
0
      }
8028
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8029
0
        zend_string *str = zend_type_to_string(arg_info->type);
8030
0
        zend_error_noreturn(E_COMPILE_ERROR,
8031
0
          "Property %s::$%s cannot have type %s",
8032
0
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8033
0
      }
8034
8035
0
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8036
0
        property_flags |= ZEND_ACC_READONLY;
8037
0
      }
8038
8039
      /* Recompile the type, as it has different memory management requirements. */
8040
0
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8041
0
      if (type_ast) {
8042
0
        type = zend_compile_typename(type_ast);
8043
0
      }
8044
8045
      /* Don't give the property an explicit default value. For typed properties this means
8046
       * uninitialized, for untyped properties it means an implicit null default value.
8047
       * Properties with hooks get an implicit default value of undefined until inheritance,
8048
       * where it is changed to null only once we know it is not virtual. If we were to set it
8049
       * here, we couldn't verify that a true virtual property must not have an explicit
8050
       * default value. */
8051
0
      zval default_value;
8052
0
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8053
0
        ZVAL_UNDEF(&default_value);
8054
0
      } else {
8055
0
        if (property_flags & ZEND_ACC_READONLY) {
8056
0
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8057
0
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8058
0
        }
8059
8060
0
        ZVAL_NULL(&default_value);
8061
0
      }
8062
8063
0
      zend_string *doc_comment =
8064
0
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8065
0
      zend_property_info *prop = zend_declare_typed_property(
8066
0
        scope, name, &default_value,
8067
0
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8068
0
        doc_comment, type);
8069
0
      if (hooks_ast) {
8070
0
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8071
0
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8072
0
      }
8073
0
      if (attributes_ast) {
8074
0
        zend_compile_attributes(
8075
0
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8076
8077
0
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8078
0
        if (override_attribute) {
8079
0
          prop->flags |= ZEND_ACC_OVERRIDE;
8080
0
        }
8081
0
      }
8082
0
    }
8083
8
  }
8084
8085
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8086
9
  op_array->num_args = list->children;
8087
9
  op_array->arg_info = arg_infos;
8088
8089
  /* Don't count the variadic argument */
8090
9
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8091
0
    op_array->num_args--;
8092
0
  }
8093
9
  zend_set_function_arg_flags((zend_function*)op_array);
8094
8095
17
  for (i = 0; i < list->children; i++) {
8096
8
    zend_ast *param_ast = list->child[i];
8097
8
    zend_ast *hooks_ast = param_ast->child[5];
8098
8
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8099
8
    uint32_t flags = param_ast->attr & promotion_flags;
8100
8
    bool is_promoted = flags || hooks_ast;
8101
8
    if (!is_promoted) {
8102
8
      continue;
8103
8
    }
8104
8105
0
    CG(zend_lineno) = param_ast->lineno;
8106
8107
    /* Emit $this->prop = $prop for promoted properties. */
8108
0
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8109
0
    znode name_node, value_node;
8110
0
    name_node.op_type = IS_CONST;
8111
0
    ZVAL_STR_COPY(&name_node.u.constant, name);
8112
0
    value_node.op_type = IS_CV;
8113
0
    value_node.u.op.var = lookup_cv(name);
8114
8115
0
    zend_op *opline = zend_emit_op(NULL,
8116
0
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8117
0
    opline->extended_value = zend_alloc_cache_slots(3);
8118
0
    zend_emit_op_data(&value_node);
8119
0
  }
8120
9
}
8121
/* }}} */
8122
8123
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8124
0
{
8125
0
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8126
0
  uint32_t i;
8127
8128
0
  if (!list->children) {
8129
0
    return;
8130
0
  }
8131
8132
0
  if (!op_array->static_variables) {
8133
0
    op_array->static_variables = zend_new_array(8);
8134
0
  }
8135
8136
0
  for (i = 0; i < list->children; ++i) {
8137
0
    zend_ast *var_name_ast = list->child[i];
8138
0
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8139
0
    uint32_t mode = var_name_ast->attr;
8140
0
    zend_op *opline;
8141
0
    zval *value;
8142
8143
0
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8144
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8145
0
    }
8146
8147
0
    if (zend_is_auto_global(var_name)) {
8148
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8149
0
    }
8150
8151
0
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8152
0
    if (!value) {
8153
0
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8154
0
        "Cannot use variable $%S twice", var_name);
8155
0
    }
8156
8157
0
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8158
8159
0
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8160
0
    opline->op2_type = IS_CV;
8161
0
    opline->op2.var = lookup_cv(var_name);
8162
0
    opline->extended_value =
8163
0
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8164
0
  }
8165
0
}
8166
/* }}} */
8167
8168
typedef struct {
8169
  HashTable uses;
8170
  bool varvars_used;
8171
} closure_info;
8172
8173
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8174
8175
1.23k
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8176
1.23k
  if (!ast) {
8177
0
    return;
8178
0
  }
8179
8180
1.23k
  if (ast->kind == ZEND_AST_VAR) {
8181
386
    zend_ast *name_ast = ast->child[0];
8182
386
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8183
386
      zend_string *name = zend_ast_get_str(name_ast);
8184
386
      if (zend_is_auto_global(name)) {
8185
        /* These is no need to explicitly import auto-globals. */
8186
0
        return;
8187
0
      }
8188
8189
386
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8190
        /* $this does not need to be explicitly imported. */
8191
0
        return;
8192
0
      }
8193
8194
386
      zend_hash_add_empty_element(&info->uses, name);
8195
386
    } else {
8196
0
      info->varvars_used = true;
8197
0
      find_implicit_binds_recursively(info, name_ast);
8198
0
    }
8199
848
  } else if (zend_ast_is_list(ast)) {
8200
1
    const zend_ast_list *list = zend_ast_get_list(ast);
8201
1
    uint32_t i;
8202
772
    for (i = 0; i < list->children; i++) {
8203
771
      find_implicit_binds_recursively(info, list->child[i]);
8204
771
    }
8205
847
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8206
    /* For normal closures add the use() list. */
8207
0
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8208
0
    zend_ast *uses_ast = closure_ast->child[1];
8209
0
    if (uses_ast) {
8210
0
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8211
0
      uint32_t i;
8212
0
      for (i = 0; i < uses_list->children; i++) {
8213
0
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8214
0
      }
8215
0
    }
8216
847
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8217
    /* For arrow functions recursively check the expression. */
8218
0
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8219
0
    closure_info inner_info;
8220
0
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8221
0
    if (inner_info.varvars_used) {
8222
0
      info->varvars_used = true;
8223
0
    }
8224
0
    if (zend_hash_num_elements(&inner_info.uses)) {
8225
0
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8226
0
    }
8227
0
    zend_hash_destroy(&inner_info.uses);
8228
847
  } else if (!zend_ast_is_special(ast)) {
8229
231
    uint32_t i, children = zend_ast_get_num_children(ast);
8230
693
    for (i = 0; i < children; i++) {
8231
462
      find_implicit_binds_recursively(info, ast->child[i]);
8232
462
    }
8233
231
  }
8234
1.23k
}
8235
8236
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8237
1
{
8238
1
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8239
1
  uint32_t i;
8240
8241
1
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8242
1
  info->varvars_used = false;
8243
8244
1
  find_implicit_binds_recursively(info, stmt_ast);
8245
8246
  /* Remove variables that are parameters */
8247
1
  for (i = 0; i < param_list->children; i++) {
8248
0
    const zend_ast *param_ast = param_list->child[i];
8249
0
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8250
0
  }
8251
1
}
8252
8253
static void compile_implicit_lexical_binds(
8254
    const closure_info *info, znode *closure, zend_op_array *op_array)
8255
1
{
8256
1
  zend_string *var_name;
8257
1
  zend_op *opline;
8258
8259
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8260
1
  if (zend_hash_num_elements(&info->uses) == 0) {
8261
0
    return;
8262
0
  }
8263
8264
1
  if (!op_array->static_variables) {
8265
1
    op_array->static_variables = zend_new_array(8);
8266
1
  }
8267
8268
76
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8269
76
    zval *value = zend_hash_add(
8270
76
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8271
76
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8272
8273
76
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8274
76
    opline->op2_type = IS_CV;
8275
76
    opline->op2.var = lookup_cv(var_name);
8276
76
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8277
76
  ZEND_HASH_FOREACH_END();
8278
1
}
8279
8280
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8281
0
{
8282
0
  const zend_op_array *op_array = CG(active_op_array);
8283
0
  const zend_ast_list *list = zend_ast_get_list(ast);
8284
0
  uint32_t i;
8285
8286
0
  for (i = 0; i < list->children; ++i) {
8287
0
    uint32_t mode = ZEND_BIND_EXPLICIT;
8288
0
    zend_ast *var_ast = list->child[i];
8289
0
    zend_string *var_name = zend_ast_get_str(var_ast);
8290
0
    zval zv;
8291
0
    ZVAL_NULL(&zv);
8292
8293
0
    {
8294
0
      int i;
8295
0
      for (i = 0; i < op_array->last_var; i++) {
8296
0
        if (zend_string_equals(op_array->vars[i], var_name)) {
8297
0
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8298
0
            "Cannot use lexical variable $%S as a parameter name", var_name);
8299
0
        }
8300
0
      }
8301
0
    }
8302
8303
0
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8304
8305
0
    if (var_ast->attr) {
8306
0
      mode |= ZEND_BIND_REF;
8307
0
    }
8308
8309
0
    zend_compile_static_var_common(var_name, &zv, mode);
8310
0
  }
8311
0
}
8312
/* }}} */
8313
8314
static void zend_compile_implicit_closure_uses(const closure_info *info)
8315
1
{
8316
1
  zend_string *var_name;
8317
76
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8318
76
    zval zv;
8319
76
    ZVAL_NULL(&zv);
8320
76
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8321
76
  ZEND_HASH_FOREACH_END();
8322
1
}
8323
8324
0
static void add_stringable_interface(zend_class_entry *ce) {
8325
0
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8326
0
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8327
      /* Interface already explicitly implemented */
8328
0
      return;
8329
0
    }
8330
0
  }
8331
8332
0
  ce->num_interfaces++;
8333
0
  ce->interface_names =
8334
0
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8335
  // TODO: Add known interned strings instead?
8336
0
  ce->interface_names[ce->num_interfaces - 1].name =
8337
0
    ZSTR_INIT_LITERAL("Stringable", 0);
8338
0
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8339
0
    ZSTR_INIT_LITERAL("stringable", 0);
8340
0
}
8341
8342
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8343
12
{
8344
12
  zend_class_entry *ce = CG(active_class_entry);
8345
12
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8346
12
  uint32_t fn_flags = op_array->fn_flags;
8347
8348
12
  zend_string *lcname;
8349
8350
12
  if (fn_flags & ZEND_ACC_READONLY) {
8351
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8352
0
  }
8353
8354
12
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8355
0
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8356
0
  }
8357
8358
12
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8359
0
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8360
    // Don't say that the class should be declared abstract if it is
8361
    // anonymous or an enum and can't be abstract
8362
0
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8363
0
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8364
0
        ZSTR_VAL(name));
8365
0
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8366
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8367
0
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8368
0
    } else {
8369
0
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8370
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8371
0
    }
8372
0
  }
8373
8374
12
  if (in_interface) {
8375
0
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8376
0
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8377
0
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8378
0
    }
8379
0
    if (fn_flags & ZEND_ACC_FINAL) {
8380
0
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8381
0
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8382
0
    }
8383
0
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8384
0
  }
8385
8386
12
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8387
0
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8388
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8389
0
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8390
0
    }
8391
8392
0
    if (has_body) {
8393
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8394
0
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8395
0
    }
8396
8397
0
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8398
12
  } else if (!has_body) {
8399
0
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8400
0
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8401
0
  }
8402
8403
12
  op_array->scope = ce;
8404
12
  op_array->function_name = zend_string_copy(name);
8405
8406
12
  lcname = zend_string_tolower(name);
8407
12
  lcname = zend_new_interned_string(lcname);
8408
8409
12
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8410
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8411
0
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8412
0
  }
8413
8414
12
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8415
12
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8416
0
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8417
0
    add_stringable_interface(ce);
8418
0
  }
8419
8420
12
  return lcname;
8421
12
}
8422
/* }}} */
8423
8424
1
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8425
1
  zend_op_array *op_array = CG(active_op_array);
8426
1
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8427
1
  op_array->dynamic_func_defs = erealloc(
8428
1
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8429
1
  op_array->dynamic_func_defs[def_offset] = def;
8430
1
  return def_offset;
8431
1
}
8432
8433
enum func_decl_level {
8434
  FUNC_DECL_LEVEL_TOPLEVEL,
8435
  FUNC_DECL_LEVEL_NESTED,
8436
  FUNC_DECL_LEVEL_CONSTEXPR,
8437
};
8438
8439
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8440
9
{
8441
9
  zend_string *unqualified_name, *name, *lcname;
8442
9
  zend_op *opline;
8443
8444
9
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8445
1
    zend_string *filename = op_array->filename;
8446
1
    uint32_t start_lineno = decl->start_lineno;
8447
8448
1
    zend_string *class = zend_empty_string;
8449
1
    zend_string *separator = zend_empty_string;
8450
1
    zend_string *function = filename;
8451
1
    const char *parens = "";
8452
8453
1
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8454
0
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8455
        /* If the parent function is a closure, don't redundantly
8456
         * add the classname and parentheses.
8457
         */
8458
0
        function = CG(active_op_array)->function_name;
8459
0
      } else {
8460
0
        function = CG(active_op_array)->function_name;
8461
0
        parens = "()";
8462
8463
0
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8464
0
          class = CG(active_class_entry)->name;
8465
0
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8466
0
        }
8467
0
      }
8468
0
    }
8469
8470
1
    unqualified_name = zend_strpprintf_unchecked(
8471
1
      0,
8472
1
      "{closure:%S%S%S%s:%" PRIu32 "}",
8473
1
      class,
8474
1
      separator,
8475
1
      function,
8476
1
      parens,
8477
1
      start_lineno
8478
1
    );
8479
8480
1
    op_array->function_name = name = unqualified_name;
8481
8
  } else {
8482
8
    unqualified_name = decl->name;
8483
8
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8484
8
  }
8485
8486
9
  lcname = zend_string_tolower(name);
8487
8488
9
  if (FC(imports_function)) {
8489
0
    const zend_string *import_name =
8490
0
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8491
0
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8492
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8493
0
        ZSTR_VAL(name));
8494
0
    }
8495
0
  }
8496
8497
9
  if (zend_string_equals_literal(lcname, "__autoload")) {
8498
0
    zend_error_noreturn(E_COMPILE_ERROR,
8499
0
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8500
0
  }
8501
8502
9
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8503
0
    zend_error(E_COMPILE_ERROR,
8504
0
      "Defining a custom assert() function is not allowed, "
8505
0
      "as the function has special semantics");
8506
0
  }
8507
8508
9
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8509
9
  switch (level) {
8510
1
    case FUNC_DECL_LEVEL_NESTED: {
8511
1
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8512
1
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8513
1
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8514
1
        opline->op2.num = func_ref;
8515
1
      } else {
8516
0
        opline = get_next_op();
8517
0
        opline->opcode = ZEND_DECLARE_FUNCTION;
8518
0
        opline->op1_type = IS_CONST;
8519
0
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8520
0
        opline->op2.num = func_ref;
8521
0
      }
8522
1
      break;
8523
0
    }
8524
0
    case FUNC_DECL_LEVEL_CONSTEXPR:
8525
8
    case FUNC_DECL_LEVEL_TOPLEVEL:
8526
      /* Nothing to do. */
8527
8
      break;
8528
9
  }
8529
9
  return lcname;
8530
9
}
8531
/* }}} */
8532
8533
static zend_op_array *zend_compile_func_decl_ex(
8534
  znode *result, zend_ast *ast, enum func_decl_level level,
8535
  zend_string *property_info_name,
8536
  zend_property_hook_kind hook_kind
8537
21
) {
8538
21
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8539
21
  zend_ast *params_ast = decl->child[0];
8540
21
  zend_ast *uses_ast = decl->child[1];
8541
21
  zend_ast *stmt_ast = decl->child[2];
8542
21
  zend_ast *return_type_ast = decl->child[3];
8543
21
  bool is_method = decl->kind == ZEND_AST_METHOD;
8544
21
  zend_string *lcname = NULL;
8545
21
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8546
8547
21
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8548
21
  zend_op_array *orig_op_array = CG(active_op_array);
8549
21
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8550
21
  zend_oparray_context orig_oparray_context;
8551
21
  closure_info info;
8552
8553
21
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8554
8555
21
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8556
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8557
0
  }
8558
8559
21
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8560
21
  op_array->fn_flags |= decl->flags;
8561
21
  op_array->line_start = decl->start_lineno;
8562
21
  op_array->line_end = decl->end_lineno;
8563
21
  if (decl->doc_comment) {
8564
0
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8565
0
  }
8566
8567
21
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8568
1
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8569
1
  }
8570
8571
21
  if (is_hook) {
8572
0
    zend_class_entry *ce = CG(active_class_entry);
8573
0
    op_array->scope = ce;
8574
0
    op_array->function_name = zend_string_copy(decl->name);
8575
21
  } else if (is_method) {
8576
12
    bool has_body = stmt_ast != NULL;
8577
12
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8578
12
  } else {
8579
9
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8580
9
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8581
1
      find_implicit_binds(&info, params_ast, stmt_ast);
8582
1
      compile_implicit_lexical_binds(&info, result, op_array);
8583
8
    } else if (uses_ast) {
8584
0
      zend_compile_closure_binding(result, op_array, uses_ast);
8585
0
    }
8586
9
  }
8587
8588
21
  CG(active_op_array) = op_array;
8589
8590
21
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8591
21
  CG(context).active_property_info_name = property_info_name;
8592
21
  CG(context).active_property_hook_kind = hook_kind;
8593
8594
21
  if (decl->child[4]) {
8595
0
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8596
8597
0
    if (is_method || is_hook) {
8598
0
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8599
0
    }
8600
8601
0
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8602
8603
0
    const zend_attribute *override_attribute = zend_get_attribute_str(
8604
0
      op_array->attributes,
8605
0
      "override",
8606
0
      sizeof("override")-1
8607
0
    );
8608
8609
0
    if (override_attribute) {
8610
0
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8611
0
    }
8612
8613
0
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8614
0
      op_array->attributes,
8615
0
      "deprecated",
8616
0
      sizeof("deprecated")-1
8617
0
    );
8618
8619
0
    if (deprecated_attribute) {
8620
0
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8621
0
    }
8622
8623
    // ZEND_ACC_NODISCARD is added via an attribute validator
8624
0
  }
8625
8626
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8627
   * defined inside a class method. This is necessary for correct handling of magic constants.
8628
   * For example __CLASS__ should always be "" inside a free standing function. */
8629
21
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8630
8
    CG(active_class_entry) = NULL;
8631
8
  }
8632
8633
21
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8634
8
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8635
8
  }
8636
8637
21
  {
8638
    /* Push a separator to the loop variable stack */
8639
21
    zend_loop_var dummy_var;
8640
21
    dummy_var.opcode = ZEND_RETURN;
8641
8642
21
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8643
21
  }
8644
8645
21
  zend_compile_params(params_ast, return_type_ast,
8646
21
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8647
21
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8648
0
    zend_mark_function_as_generator();
8649
0
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8650
0
  }
8651
21
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8652
1
    zend_compile_implicit_closure_uses(&info);
8653
1
    zend_hash_destroy(&info.uses);
8654
20
  } else if (uses_ast) {
8655
0
    zend_compile_closure_uses(uses_ast);
8656
0
  }
8657
8658
21
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8659
1
    bool needs_return = true;
8660
1
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8661
0
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8662
0
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8663
0
    }
8664
1
    if (needs_return) {
8665
1
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8666
1
      decl->child[2] = stmt_ast;
8667
1
    }
8668
1
  }
8669
8670
21
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8671
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8672
     * if the method is not a hook; if it is a hook, then the validator
8673
     * will have returned an error message, even if the error message was
8674
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8675
     * flag should not have been added. */
8676
0
    ZEND_ASSERT(!is_hook);
8677
8678
0
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8679
0
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8680
0
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8681
0
        zend_error_noreturn(E_COMPILE_ERROR,
8682
0
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8683
0
          CG(active_class_entry) != NULL ? "method" : "function");
8684
0
      }
8685
8686
0
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8687
0
        zend_error_noreturn(E_COMPILE_ERROR,
8688
0
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8689
0
          CG(active_class_entry) != NULL ? "method" : "function");
8690
0
      }
8691
0
    }
8692
0
  }
8693
8694
21
  zend_compile_stmt(stmt_ast);
8695
8696
21
  if (is_method) {
8697
12
    CG(zend_lineno) = decl->start_lineno;
8698
12
    zend_check_magic_method_implementation(
8699
12
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8700
12
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8701
    /* Only register the function after a successful compile */
8702
8
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8703
0
      CG(zend_lineno) = decl->start_lineno;
8704
0
      do_bind_function_error(lcname, op_array, true);
8705
0
    }
8706
8
  }
8707
8708
  /* put the implicit return on the really last line */
8709
21
  CG(zend_lineno) = decl->end_lineno;
8710
8711
21
  zend_do_extended_stmt(NULL);
8712
21
  zend_emit_final_return(false);
8713
8714
21
  pass_two(CG(active_op_array));
8715
21
  zend_oparray_context_end(&orig_oparray_context);
8716
8717
  /* Pop the loop variable stack separator */
8718
21
  zend_stack_del_top(&CG(loop_var_stack));
8719
8720
21
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8721
8
    zend_observer_function_declared_notify(op_array, lcname);
8722
8
  }
8723
8724
21
  if (lcname != NULL) {
8725
21
    zend_string_release_ex(lcname, 0);
8726
21
  }
8727
8728
21
  CG(active_op_array) = orig_op_array;
8729
21
  CG(active_class_entry) = orig_class_entry;
8730
8731
21
  return op_array;
8732
21
}
8733
8734
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8735
21
{
8736
21
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8737
21
}
8738
8739
0
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8740
0
  if (zend_string_equals_literal_ci(name, "get")) {
8741
0
    return ZEND_PROPERTY_HOOK_GET;
8742
0
  } else if (zend_string_equals_literal_ci(name, "set")) {
8743
0
    return ZEND_PROPERTY_HOOK_SET;
8744
0
  } else {
8745
0
    return (zend_property_hook_kind)-1;
8746
0
  }
8747
0
}
8748
8749
static void zend_compile_property_hooks(
8750
    zend_property_info *prop_info, zend_string *prop_name,
8751
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8752
0
{
8753
0
  zend_class_entry *ce = CG(active_class_entry);
8754
8755
0
  if (prop_info->flags & ZEND_ACC_READONLY) {
8756
0
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8757
0
  }
8758
8759
0
  if (hooks->children == 0) {
8760
0
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8761
0
  }
8762
8763
0
  for (uint32_t i = 0; i < hooks->children; i++) {
8764
0
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8765
0
    zend_string *name = hook->name;
8766
0
    zend_ast *stmt_ast = hook->child[2];
8767
0
    zend_ast **return_type_ast_ptr = NULL;
8768
0
    zend_ast **value_type_ast_ptr = NULL;
8769
0
    CG(zend_lineno) = hook->start_lineno;
8770
8771
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8772
     * hooks from protected properties to public ones. */
8773
0
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8774
0
    hook->flags |= hook_visibility;
8775
8776
0
    if (prop_info->flags & ZEND_ACC_STATIC) {
8777
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8778
0
    }
8779
0
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8780
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8781
0
    }
8782
0
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8783
0
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8784
0
      hook->flags |= ZEND_ACC_ABSTRACT;
8785
8786
0
      if (stmt_ast) {
8787
0
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8788
0
      }
8789
0
      if (hook->flags & ZEND_ACC_PRIVATE) {
8790
0
        zend_error_noreturn(E_COMPILE_ERROR,
8791
0
          "Property hook cannot be both abstract and private");
8792
0
      }
8793
0
      if (hook->flags & ZEND_ACC_FINAL) {
8794
0
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8795
0
      }
8796
0
    } else if (!stmt_ast) {
8797
0
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8798
0
    }
8799
8800
0
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8801
0
    if (hook_kind == (zend_property_hook_kind)-1) {
8802
0
      zend_error_noreturn(E_COMPILE_ERROR,
8803
0
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8804
0
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8805
0
    }
8806
8807
0
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8808
0
      stmt_ast = stmt_ast->child[0];
8809
0
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8810
0
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8811
0
      } else {
8812
0
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8813
0
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8814
0
          zend_ast_create(ZEND_AST_PROP,
8815
0
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8816
0
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8817
0
          stmt_ast);
8818
0
      }
8819
0
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8820
0
      hook->child[2] = stmt_ast;
8821
0
    }
8822
8823
0
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8824
0
      if (hook->child[0]) {
8825
0
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8826
0
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8827
0
      }
8828
8829
0
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8830
8831
0
      return_type_ast_ptr = &hook->child[3];
8832
0
      *return_type_ast_ptr = prop_type_ast;
8833
0
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8834
0
      if (hook->child[0]) {
8835
0
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8836
0
        if (param_list->children != 1) {
8837
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8838
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8839
0
        }
8840
0
        const zend_ast *value_param_ast = param_list->child[0];
8841
0
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8842
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8843
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8844
0
        }
8845
0
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8846
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8847
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8848
0
        }
8849
0
        if (value_param_ast->child[2]) {
8850
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8851
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8852
0
        }
8853
0
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8854
0
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8855
0
        }
8856
0
      } else {
8857
0
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8858
0
        zend_ast *param = zend_ast_create(
8859
0
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8860
0
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8861
0
          /* hooks */ NULL);
8862
0
        value_type_ast_ptr = &param->child[0];
8863
0
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8864
0
      }
8865
0
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8866
0
      return_type->attr = ZEND_NAME_NOT_FQ;
8867
0
      hook->child[3] = return_type;
8868
0
    } else {
8869
0
      ZEND_UNREACHABLE();
8870
0
    }
8871
8872
0
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8873
8874
0
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8875
0
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8876
8877
0
    func->common.prop_info = prop_info;
8878
8879
0
    if (!prop_info->hooks) {
8880
0
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8881
0
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8882
0
    }
8883
8884
0
    if (prop_info->hooks[hook_kind]) {
8885
0
      zend_error_noreturn(E_COMPILE_ERROR,
8886
0
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8887
0
    }
8888
0
    prop_info->hooks[hook_kind] = func;
8889
8890
0
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8891
0
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8892
0
        case INHERITANCE_SUCCESS:
8893
0
          break;
8894
0
        case INHERITANCE_UNRESOLVED:
8895
0
          ce->num_hooked_prop_variance_checks++;
8896
0
          break;
8897
0
        case INHERITANCE_ERROR:
8898
0
          zend_hooked_property_variance_error(prop_info);
8899
0
        case INHERITANCE_WARNING:
8900
0
          ZEND_UNREACHABLE();
8901
0
      }
8902
0
    }
8903
8904
0
    zend_string_release(name);
8905
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8906
0
    if (return_type_ast_ptr) {
8907
0
      *return_type_ast_ptr = NULL;
8908
0
    }
8909
0
    if (value_type_ast_ptr) {
8910
0
      *value_type_ast_ptr = NULL;
8911
0
    }
8912
0
  }
8913
8914
0
  ce->num_hooked_props++;
8915
8916
  /* See zend_link_hooked_object_iter(). */
8917
0
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8918
0
  if (!ce->get_iterator) {
8919
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8920
0
    ce->get_iterator = zend_hooked_object_get_iterator;
8921
0
  }
8922
0
#endif
8923
8924
0
  if (!prop_info->ce->parent_name) {
8925
0
    zend_verify_hooked_property(ce, prop_info, prop_name);
8926
0
  }
8927
0
}
8928
8929
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8930
0
{
8931
0
  const zend_ast_list *list = zend_ast_get_list(ast);
8932
0
  zend_class_entry *ce = CG(active_class_entry);
8933
0
  uint32_t i, children = list->children;
8934
8935
0
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8936
0
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8937
0
  }
8938
8939
0
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8940
0
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8941
0
  }
8942
8943
0
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8944
0
    if (flags & ZEND_ACC_FINAL) {
8945
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8946
0
    }
8947
0
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8948
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8949
0
    }
8950
0
    if (flags & ZEND_ACC_ABSTRACT) {
8951
0
      zend_error_noreturn(E_COMPILE_ERROR,
8952
0
        "Property in interface cannot be explicitly abstract. "
8953
0
        "All interface members are implicitly abstract");
8954
0
    }
8955
0
    flags |= ZEND_ACC_ABSTRACT;
8956
0
  }
8957
8958
0
  for (i = 0; i < children; ++i) {
8959
0
    zend_property_info *info;
8960
0
    zend_ast *prop_ast = list->child[i];
8961
0
    zend_ast *name_ast = prop_ast->child[0];
8962
0
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8963
0
    zend_ast *doc_comment_ast = prop_ast->child[2];
8964
0
    zend_ast *hooks_ast = prop_ast->child[3];
8965
0
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8966
0
    zend_string *doc_comment = NULL;
8967
0
    zval value_zv;
8968
0
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8969
0
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8970
8971
0
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8972
0
    CG(context).active_property_info_name = name;
8973
8974
0
    if (!hooks_ast) {
8975
0
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8976
0
        zend_error_noreturn(E_COMPILE_ERROR,
8977
0
          "Interfaces may only include hooked properties");
8978
0
      }
8979
0
      if (flags & ZEND_ACC_ABSTRACT) {
8980
0
        zend_error_noreturn(E_COMPILE_ERROR,
8981
0
          "Only hooked properties may be declared abstract");
8982
0
      }
8983
0
    }
8984
0
    if ((flags & ZEND_ACC_ABSTRACT)) {
8985
0
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8986
0
    }
8987
8988
0
    if (type_ast) {
8989
0
      type = zend_compile_typename(type_ast);
8990
8991
0
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8992
0
        zend_string *str = zend_type_to_string(type);
8993
0
        zend_error_noreturn(E_COMPILE_ERROR,
8994
0
          "Property %s::$%s cannot have type %s",
8995
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
8996
0
      }
8997
0
    }
8998
8999
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9000
0
    if (doc_comment_ast) {
9001
0
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9002
0
    }
9003
9004
0
    if (zend_hash_exists(&ce->properties_info, name)) {
9005
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9006
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9007
0
    }
9008
9009
0
    if (*value_ast_ptr) {
9010
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9011
9012
0
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9013
0
          && !zend_is_valid_default_value(type, &value_zv)) {
9014
0
        zend_string *str = zend_type_to_string(type);
9015
0
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9016
0
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9017
0
          zend_string *nullable_str = zend_type_to_string(type);
9018
9019
0
          zend_error_noreturn(E_COMPILE_ERROR,
9020
0
            "Default value for property of type %s may not be null. "
9021
0
            "Use the nullable type %s to allow null default value",
9022
0
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9023
0
        } else {
9024
0
          zend_error_noreturn(E_COMPILE_ERROR,
9025
0
            "Cannot use %s as default value for property %s::$%s of type %s",
9026
0
            zend_zval_value_name(&value_zv),
9027
0
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9028
0
        }
9029
0
      }
9030
0
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9031
0
      ZVAL_NULL(&value_zv);
9032
0
    } else {
9033
0
      ZVAL_UNDEF(&value_zv);
9034
0
    }
9035
9036
0
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9037
0
      flags |= ZEND_ACC_READONLY;
9038
0
    }
9039
9040
0
    if (flags & ZEND_ACC_READONLY) {
9041
0
      if (!ZEND_TYPE_IS_SET(type)) {
9042
0
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9043
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9044
0
      }
9045
0
      if (!Z_ISUNDEF(value_zv)) {
9046
0
        zend_error_noreturn(E_COMPILE_ERROR,
9047
0
          "Readonly property %s::$%s cannot have default value",
9048
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9049
0
      }
9050
0
      if (flags & ZEND_ACC_STATIC) {
9051
0
        zend_error_noreturn(E_COMPILE_ERROR,
9052
0
          "Static property %s::$%s cannot be readonly",
9053
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9054
0
      }
9055
0
    }
9056
9057
0
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9058
9059
0
    if (hooks_ast) {
9060
0
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9061
0
    }
9062
9063
0
    if (attr_ast) {
9064
0
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9065
9066
0
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9067
0
      if (override_attribute) {
9068
0
        info->flags |= ZEND_ACC_OVERRIDE;
9069
0
      }
9070
0
    }
9071
9072
0
    CG(context).active_property_info_name = old_active_property_info_name;
9073
0
  }
9074
0
}
9075
/* }}} */
9076
9077
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9078
0
{
9079
0
  zend_ast *type_ast = ast->child[0];
9080
0
  zend_ast *prop_ast = ast->child[1];
9081
0
  zend_ast *attr_ast = ast->child[2];
9082
9083
0
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9084
0
}
9085
/* }}} */
9086
9087
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9088
0
{
9089
0
  if (attr & ZEND_ACC_STATIC) {
9090
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9091
0
  } else if (attr & ZEND_ACC_ABSTRACT) {
9092
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9093
0
  }
9094
0
}
9095
/* }}} */
9096
9097
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9098
0
{
9099
0
  const zend_ast_list *list = zend_ast_get_list(ast);
9100
0
  zend_class_entry *ce = CG(active_class_entry);
9101
0
  uint32_t i, children = list->children;
9102
9103
0
  for (i = 0; i < children; ++i) {
9104
0
    zend_class_constant *c;
9105
0
    zend_ast *const_ast = list->child[i];
9106
0
    zend_ast *name_ast = const_ast->child[0];
9107
0
    zend_ast **value_ast_ptr = &const_ast->child[1];
9108
0
    zend_ast *doc_comment_ast = const_ast->child[2];
9109
0
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9110
0
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9111
0
    zval value_zv;
9112
0
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9113
9114
0
    if (type_ast) {
9115
0
      type = zend_compile_typename(type_ast);
9116
9117
0
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9118
9119
0
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9120
0
        zend_string *type_str = zend_type_to_string(type);
9121
9122
0
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9123
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9124
0
      }
9125
0
    }
9126
9127
0
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9128
0
      zend_error_noreturn(
9129
0
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9130
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9131
0
      );
9132
0
    }
9133
9134
0
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9135
9136
0
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9137
0
      zend_string *type_str = zend_type_to_string(type);
9138
9139
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9140
0
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9141
0
    }
9142
9143
0
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9144
9145
0
    if (attr_ast) {
9146
0
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9147
9148
0
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9149
9150
0
      if (deprecated) {
9151
0
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9152
        /* For deprecated constants, we need to flag the zval for recursion
9153
         * detection. Make sure the zval is separated out of shm. */
9154
0
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9155
0
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9156
0
      }
9157
0
    }
9158
0
  }
9159
0
}
9160
9161
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9162
0
{
9163
0
  zend_ast *const_ast = ast->child[0];
9164
0
  zend_ast *attr_ast = ast->child[1];
9165
0
  zend_ast *type_ast = ast->child[2];
9166
9167
0
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9168
0
}
9169
/* }}} */
9170
9171
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9172
0
{
9173
0
  zend_ast *class_ast = ast->child[0];
9174
0
  zend_ast *method_ast = ast->child[1];
9175
9176
0
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9177
9178
0
  if (class_ast) {
9179
0
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9180
0
  } else {
9181
0
    method_ref->class_name = NULL;
9182
0
  }
9183
0
}
9184
/* }}} */
9185
9186
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9187
0
{
9188
0
  const zend_ast *method_ref_ast = ast->child[0];
9189
0
  zend_ast *insteadof_ast = ast->child[1];
9190
0
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9191
0
  uint32_t i;
9192
9193
0
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9194
0
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9195
0
  precedence->num_excludes = insteadof_list->children;
9196
9197
0
  for (i = 0; i < insteadof_list->children; ++i) {
9198
0
    zend_ast *name_ast = insteadof_list->child[i];
9199
0
    precedence->exclude_class_names[i] =
9200
0
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9201
0
  }
9202
9203
0
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9204
0
}
9205
/* }}} */
9206
9207
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9208
0
{
9209
0
  const zend_ast *method_ref_ast = ast->child[0];
9210
0
  zend_ast *alias_ast = ast->child[1];
9211
0
  uint32_t modifiers = ast->attr;
9212
9213
0
  zend_trait_alias *alias;
9214
9215
0
  zend_check_trait_alias_modifiers(modifiers);
9216
9217
0
  alias = emalloc(sizeof(zend_trait_alias));
9218
0
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9219
0
  alias->modifiers = modifiers;
9220
9221
0
  if (alias_ast) {
9222
0
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9223
0
  } else {
9224
0
    alias->alias = NULL;
9225
0
  }
9226
9227
0
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9228
0
}
9229
/* }}} */
9230
9231
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9232
0
{
9233
0
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9234
0
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9235
0
  zend_class_entry *ce = CG(active_class_entry);
9236
0
  uint32_t i;
9237
9238
0
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9239
9240
0
  for (i = 0; i < traits->children; ++i) {
9241
0
    zend_ast *trait_ast = traits->child[i];
9242
9243
0
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9244
0
      zend_string *name = zend_ast_get_str(trait_ast);
9245
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9246
0
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9247
0
    }
9248
9249
0
    ce->trait_names[ce->num_traits].name =
9250
0
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9251
0
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9252
0
    ce->num_traits++;
9253
0
  }
9254
9255
0
  if (!adaptations) {
9256
0
    return;
9257
0
  }
9258
9259
0
  for (i = 0; i < adaptations->children; ++i) {
9260
0
    const zend_ast *adaptation_ast = adaptations->child[i];
9261
0
    switch (adaptation_ast->kind) {
9262
0
      case ZEND_AST_TRAIT_PRECEDENCE:
9263
0
        zend_compile_trait_precedence(adaptation_ast);
9264
0
        break;
9265
0
      case ZEND_AST_TRAIT_ALIAS:
9266
0
        zend_compile_trait_alias(adaptation_ast);
9267
0
        break;
9268
0
      EMPTY_SWITCH_DEFAULT_CASE()
9269
0
    }
9270
0
  }
9271
0
}
9272
/* }}} */
9273
9274
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9275
1
{
9276
1
  const zend_ast_list *list = zend_ast_get_list(ast);
9277
1
  zend_class_entry *ce = CG(active_class_entry);
9278
1
  zend_class_name *interface_names;
9279
1
  uint32_t i;
9280
9281
1
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9282
9283
2
  for (i = 0; i < list->children; ++i) {
9284
1
    zend_ast *class_ast = list->child[i];
9285
1
    interface_names[i].name =
9286
1
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9287
1
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9288
1
  }
9289
9290
1
  ce->num_interfaces = list->children;
9291
1
  ce->interface_names = interface_names;
9292
1
}
9293
/* }}} */
9294
9295
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9296
0
{
9297
0
  zend_string *filename = CG(active_op_array)->filename;
9298
0
  uint32_t start_lineno = decl->start_lineno;
9299
9300
  /* Use parent or first interface as prefix. */
9301
0
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9302
0
  if (decl->child[0]) {
9303
0
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9304
0
  } else if (decl->child[1]) {
9305
0
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9306
0
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9307
0
  }
9308
9309
0
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9310
0
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9311
0
  zend_string_release(prefix);
9312
0
  return zend_new_interned_string(result);
9313
0
}
9314
9315
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9316
2
{
9317
2
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9318
2
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9319
2
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9320
2
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9321
0
    zend_string *type_string = zend_type_to_string(type);
9322
0
    zend_error_noreturn(E_COMPILE_ERROR,
9323
0
      "Enum backing type must be int or string, %s given",
9324
0
      ZSTR_VAL(type_string));
9325
0
  }
9326
2
  if (type_mask == MAY_BE_LONG) {
9327
1
    ce->enum_backing_type = IS_LONG;
9328
1
  } else {
9329
1
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9330
1
    ce->enum_backing_type = IS_STRING;
9331
1
  }
9332
2
  zend_type_release(type, 0);
9333
2
}
9334
9335
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9336
15
{
9337
15
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9338
15
  zend_ast *extends_ast = decl->child[0];
9339
15
  zend_ast *implements_ast = decl->child[1];
9340
15
  zend_ast *stmt_ast = decl->child[2];
9341
15
  zend_ast *enum_backing_type_ast = decl->child[4];
9342
15
  zend_string *name, *lcname;
9343
15
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9344
15
  zend_op *opline;
9345
9346
15
  zend_class_entry *original_ce = CG(active_class_entry);
9347
9348
15
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9349
15
    zend_string *unqualified_name = decl->name;
9350
9351
15
    if (CG(active_class_entry)) {
9352
0
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9353
0
    }
9354
9355
15
    const char *type = "a class name";
9356
15
    if (decl->flags & ZEND_ACC_ENUM) {
9357
4
      type = "an enum name";
9358
11
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9359
0
      type = "an interface name";
9360
11
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9361
0
      type = "a trait name";
9362
0
    }
9363
15
    zend_assert_valid_class_name(unqualified_name, type);
9364
15
    name = zend_prefix_with_ns(unqualified_name);
9365
15
    name = zend_new_interned_string(name);
9366
15
    lcname = zend_string_tolower(name);
9367
9368
15
    if (FC(imports)) {
9369
0
      zend_string *import_name =
9370
0
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9371
0
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9372
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9373
0
            "(previously declared as local import)", ZSTR_VAL(name));
9374
0
      }
9375
0
    }
9376
9377
15
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9378
15
  } else {
9379
    /* Find an anon class name that is not in use yet. */
9380
0
    name = NULL;
9381
0
    lcname = NULL;
9382
0
    do {
9383
0
      zend_tmp_string_release(name);
9384
0
      zend_tmp_string_release(lcname);
9385
0
      name = zend_generate_anon_class_name(decl);
9386
0
      lcname = zend_string_tolower(name);
9387
0
    } while (zend_hash_exists(CG(class_table), lcname));
9388
0
  }
9389
15
  lcname = zend_new_interned_string(lcname);
9390
9391
15
  ce->type = ZEND_USER_CLASS;
9392
15
  ce->name = name;
9393
15
  zend_initialize_class_data(ce, true);
9394
15
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9395
15
    zend_alloc_ce_cache(ce->name);
9396
15
  }
9397
9398
15
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9399
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9400
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9401
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9402
0
  }
9403
9404
15
  ce->ce_flags |= decl->flags;
9405
15
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9406
15
  ce->info.user.line_start = decl->start_lineno;
9407
15
  ce->info.user.line_end = decl->end_lineno;
9408
9409
15
  if (decl->doc_comment) {
9410
0
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9411
0
  }
9412
9413
15
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9414
    /* Serialization is not supported for anonymous classes */
9415
0
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9416
0
  }
9417
9418
15
  if (extends_ast) {
9419
0
    ce->parent_name =
9420
0
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9421
0
  }
9422
9423
15
  CG(active_class_entry) = ce;
9424
9425
15
  if (decl->child[3]) {
9426
0
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9427
0
  }
9428
9429
15
  if (implements_ast) {
9430
1
    zend_compile_implements(implements_ast);
9431
1
  }
9432
9433
15
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9434
4
    if (enum_backing_type_ast != NULL) {
9435
2
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9436
2
    }
9437
4
    zend_enum_add_interfaces(ce);
9438
4
    zend_enum_register_props(ce);
9439
4
  }
9440
9441
15
  zend_compile_stmt(stmt_ast);
9442
9443
  /* Reset lineno for final opcodes and errors */
9444
15
  CG(zend_lineno) = ast->lineno;
9445
9446
15
  if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
9447
0
    zend_verify_abstract_class(ce);
9448
0
  }
9449
9450
15
  CG(active_class_entry) = original_ce;
9451
9452
15
  if (toplevel) {
9453
15
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9454
15
  }
9455
9456
  /* We currently don't early-bind classes that implement interfaces or use traits */
9457
15
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9458
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9459
   /* See zend_link_hooked_object_iter(). */
9460
   && !ce->num_hooked_props
9461
#endif
9462
11
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9463
11
    if (toplevel) {
9464
11
      if (extends_ast) {
9465
0
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9466
0
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9467
9468
0
        if (parent_ce
9469
0
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9470
0
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9471
0
            zend_string_release(lcname);
9472
0
            return;
9473
0
          }
9474
0
        }
9475
11
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9476
11
        zend_string_release(lcname);
9477
11
        zend_build_properties_info_table(ce);
9478
11
        zend_inheritance_check_override(ce);
9479
11
        ce->ce_flags |= ZEND_ACC_LINKED;
9480
11
        zend_observer_class_linked_notify(ce, lcname);
9481
11
        return;
9482
11
      } else {
9483
0
        goto link_unbound;
9484
0
      }
9485
11
    } else if (!extends_ast) {
9486
0
link_unbound:
9487
      /* Link unbound simple class */
9488
0
      zend_build_properties_info_table(ce);
9489
0
      zend_inheritance_check_override(ce);
9490
0
      ce->ce_flags |= ZEND_ACC_LINKED;
9491
0
    }
9492
11
  }
9493
9494
4
  opline = get_next_op();
9495
9496
4
  if (ce->parent_name) {
9497
    /* Lowercased parent name */
9498
0
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9499
0
    opline->op2_type = IS_CONST;
9500
0
    LITERAL_STR(opline->op2, lc_parent_name);
9501
0
  }
9502
9503
4
  opline->op1_type = IS_CONST;
9504
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9505
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9506
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9507
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9508
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9509
   * zend_add_literal_string() which gives us the new value. */
9510
4
  opline->op1.constant = zend_add_literal_string(&lcname);
9511
9512
4
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9513
0
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9514
0
    opline->extended_value = zend_alloc_cache_slot();
9515
0
    zend_make_var_result(result, opline);
9516
0
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9517
      /* We checked above that the class name is not used. This really shouldn't happen. */
9518
0
      zend_error_noreturn(E_ERROR,
9519
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9520
0
    }
9521
4
  } else {
9522
    /* Generate RTD keys until we find one that isn't in use yet. */
9523
4
    zend_string *key = NULL;
9524
4
    do {
9525
4
      zend_tmp_string_release(key);
9526
4
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9527
4
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9528
9529
    /* RTD key is placed after lcname literal in op1 */
9530
4
    zend_add_literal_string(&key);
9531
9532
4
    opline->opcode = ZEND_DECLARE_CLASS;
9533
4
    if (toplevel
9534
4
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9535
        /* We currently don't early-bind classes that implement interfaces or use traits */
9536
4
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9537
4
    ) {
9538
0
      if (!extends_ast) {
9539
        /* Use empty string for classes without parents to avoid new handler, and special
9540
         * handling of zend_early_binding. */
9541
0
        opline->op2_type = IS_CONST;
9542
0
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9543
0
      }
9544
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9545
0
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9546
0
      opline->extended_value = zend_alloc_cache_slot();
9547
0
      opline->result_type = IS_UNUSED;
9548
0
      opline->result.opline_num = -1;
9549
0
    }
9550
4
  }
9551
4
}
9552
/* }}} */
9553
9554
static void zend_compile_enum_case(zend_ast *ast)
9555
4
{
9556
4
  zend_class_entry *enum_class = CG(active_class_entry);
9557
4
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9558
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9559
0
  }
9560
9561
4
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9562
4
  zend_string *enum_class_name = enum_class->name;
9563
9564
4
  zval class_name_zval;
9565
4
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9566
4
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9567
9568
4
  zval case_name_zval;
9569
4
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9570
4
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9571
9572
4
  zend_ast *case_value_ast = ast->child[1];
9573
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9574
4
  ast->child[1] = NULL;
9575
4
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9576
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9577
0
      ZSTR_VAL(enum_case_name),
9578
0
      ZSTR_VAL(enum_class_name));
9579
4
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9580
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9581
0
      ZSTR_VAL(enum_case_name),
9582
0
      ZSTR_VAL(enum_class_name));
9583
0
  }
9584
9585
4
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9586
9587
4
  zval value_zv;
9588
4
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9589
9590
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9591
4
  zend_ast *doc_comment_ast = ast->child[2];
9592
4
  zend_string *doc_comment = NULL;
9593
4
  if (doc_comment_ast) {
9594
0
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9595
0
  }
9596
9597
4
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9598
4
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9599
4
  zend_ast_destroy(const_enum_init_ast);
9600
9601
4
  zend_ast *attr_ast = ast->child[3];
9602
4
  if (attr_ast) {
9603
0
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9604
9605
0
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9606
9607
0
    if (deprecated) {
9608
0
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9609
0
    }
9610
0
  }
9611
4
}
9612
9613
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9614
0
{
9615
0
  switch (type) {
9616
0
    case ZEND_SYMBOL_CLASS:
9617
0
      if (!FC(imports)) {
9618
0
        FC(imports) = emalloc(sizeof(HashTable));
9619
0
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9620
0
      }
9621
0
      return FC(imports);
9622
0
    case ZEND_SYMBOL_FUNCTION:
9623
0
      if (!FC(imports_function)) {
9624
0
        FC(imports_function) = emalloc(sizeof(HashTable));
9625
0
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9626
0
      }
9627
0
      return FC(imports_function);
9628
0
    case ZEND_SYMBOL_CONST:
9629
0
      if (!FC(imports_const)) {
9630
0
        FC(imports_const) = emalloc(sizeof(HashTable));
9631
0
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9632
0
      }
9633
0
      return FC(imports_const);
9634
0
    EMPTY_SWITCH_DEFAULT_CASE()
9635
0
  }
9636
9637
0
  return NULL;
9638
0
}
9639
/* }}} */
9640
9641
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9642
0
{
9643
0
  switch (type) {
9644
0
    case ZEND_SYMBOL_CLASS:
9645
0
      return "";
9646
0
    case ZEND_SYMBOL_FUNCTION:
9647
0
      return " function";
9648
0
    case ZEND_SYMBOL_CONST:
9649
0
      return " const";
9650
0
    EMPTY_SWITCH_DEFAULT_CASE()
9651
0
  }
9652
9653
0
  return " unknown";
9654
0
}
9655
/* }}} */
9656
9657
static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */
9658
0
{
9659
0
  if (zend_string_equals_ci(old_name, check_name)) {
9660
0
    return;
9661
0
  }
9662
9663
0
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9664
0
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9665
0
}
9666
/* }}} */
9667
9668
static void zend_compile_use(zend_ast *ast) /* {{{ */
9669
0
{
9670
0
  const zend_ast_list *list = zend_ast_get_list(ast);
9671
0
  uint32_t i;
9672
0
  zend_string *current_ns = FC(current_namespace);
9673
0
  uint32_t type = ast->attr;
9674
0
  HashTable *current_import = zend_get_import_ht(type);
9675
0
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9676
9677
0
  for (i = 0; i < list->children; ++i) {
9678
0
    const zend_ast *use_ast = list->child[i];
9679
0
    zend_ast *old_name_ast = use_ast->child[0];
9680
0
    zend_ast *new_name_ast = use_ast->child[1];
9681
0
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9682
0
    zend_string *new_name, *lookup_name;
9683
9684
0
    if (new_name_ast) {
9685
0
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9686
0
    } else {
9687
0
      const char *unqualified_name;
9688
0
      size_t unqualified_name_len;
9689
0
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9690
        /* The form "use A\B" is equivalent to "use A\B as B" */
9691
0
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9692
0
      } else {
9693
0
        new_name = zend_string_copy(old_name);
9694
9695
0
        if (!current_ns) {
9696
0
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9697
0
            "has no effect", ZSTR_VAL(new_name));
9698
0
        }
9699
0
      }
9700
0
    }
9701
9702
0
    if (case_sensitive) {
9703
0
      lookup_name = zend_string_copy(new_name);
9704
0
    } else {
9705
0
      lookup_name = zend_string_tolower(new_name);
9706
0
    }
9707
9708
0
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9709
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9710
0
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9711
0
    }
9712
9713
0
    if (current_ns) {
9714
0
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9715
0
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9716
0
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9717
0
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9718
9719
0
      if (zend_have_seen_symbol(ns_name, type)) {
9720
0
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9721
0
      }
9722
9723
0
      zend_string_efree(ns_name);
9724
0
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9725
0
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9726
0
    }
9727
9728
0
    zend_string_addref(old_name);
9729
0
    old_name = zend_new_interned_string(old_name);
9730
0
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9731
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9732
0
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9733
0
    }
9734
9735
0
    zend_string_release_ex(lookup_name, 0);
9736
0
    zend_string_release_ex(new_name, 0);
9737
0
  }
9738
0
}
9739
/* }}} */
9740
9741
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9742
0
{
9743
0
  uint32_t i;
9744
0
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9745
0
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9746
9747
0
  for (i = 0; i < list->children; i++) {
9748
0
    zend_ast *inline_use, *use = list->child[i];
9749
0
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9750
0
    zend_string *name = Z_STR_P(name_zval);
9751
0
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9752
0
    zend_string_release_ex(name, 0);
9753
0
    ZVAL_STR(name_zval, compound_ns);
9754
0
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9755
0
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9756
0
    zend_compile_use(inline_use);
9757
0
  }
9758
0
}
9759
/* }}} */
9760
9761
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9762
0
{
9763
0
  zend_ast_list *list = zend_ast_get_list(ast);
9764
0
  uint32_t i;
9765
0
  zend_ast *attributes_ast = NULL;
9766
0
  zend_op *last_op = NULL;
9767
0
  for (i = 0; i < list->children; ++i) {
9768
0
    zend_ast *const_ast = list->child[i];
9769
0
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9770
0
      ZEND_ASSERT(i == list->children - 1);
9771
0
      attributes_ast = const_ast;
9772
0
      continue;
9773
0
    }
9774
0
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9775
0
    zend_ast *name_ast = const_ast->child[0];
9776
0
    zend_ast **value_ast_ptr = &const_ast->child[1];
9777
0
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9778
9779
0
    zend_string *name;
9780
0
    znode name_node, value_node;
9781
0
    zval *value_zv = &value_node.u.constant;
9782
9783
0
    value_node.op_type = IS_CONST;
9784
0
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9785
9786
0
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9787
0
      zend_error_noreturn(E_COMPILE_ERROR,
9788
0
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9789
0
    }
9790
9791
0
    name = zend_prefix_with_ns(unqualified_name);
9792
0
    name = zend_new_interned_string(name);
9793
9794
0
    if (FC(imports_const)) {
9795
0
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9796
0
      if (import_name && !zend_string_equals(import_name, name)) {
9797
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9798
0
          "the name is already in use", ZSTR_VAL(name));
9799
0
      }
9800
0
    }
9801
9802
0
    name_node.op_type = IS_CONST;
9803
0
    ZVAL_STR(&name_node.u.constant, name);
9804
9805
0
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9806
9807
0
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9808
0
  }
9809
0
  if (attributes_ast == NULL) {
9810
0
    return;
9811
0
  }
9812
  /* Validate: attributes can only be applied to one constant at a time
9813
   * Since we store the AST for the attributes in the list of children,
9814
   * there should be exactly 2 children. */
9815
0
  if (list->children > 2) {
9816
0
    zend_error_noreturn(
9817
0
      E_COMPILE_ERROR,
9818
0
      "Cannot apply attributes to multiple constants at once"
9819
0
    );
9820
0
  }
9821
9822
0
  HashTable *attributes = NULL;
9823
0
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9824
9825
0
  ZEND_ASSERT(last_op != NULL);
9826
0
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9827
0
  znode attribs_node;
9828
0
  attribs_node.op_type = IS_CONST;
9829
0
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9830
0
  zend_emit_op_data(&attribs_node);
9831
0
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9832
0
}
9833
/* }}}*/
9834
9835
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
9836
0
{
9837
0
  zend_ast *name_ast = ast->child[0];
9838
0
  zend_ast *stmt_ast = ast->child[1];
9839
0
  zend_string *name;
9840
0
  bool with_bracket = stmt_ast != NULL;
9841
9842
  /* handle mixed syntax declaration or nested namespaces */
9843
0
  if (!FC(has_bracketed_namespaces)) {
9844
0
    if (FC(current_namespace)) {
9845
      /* previous namespace declarations were unbracketed */
9846
0
      if (with_bracket) {
9847
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9848
0
          "with unbracketed namespace declarations");
9849
0
      }
9850
0
    }
9851
0
  } else {
9852
    /* previous namespace declarations were bracketed */
9853
0
    if (!with_bracket) {
9854
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9855
0
        "with unbracketed namespace declarations");
9856
0
    } else if (FC(current_namespace) || FC(in_namespace)) {
9857
0
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9858
0
    }
9859
0
  }
9860
9861
0
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9862
0
    || (with_bracket && !FC(has_bracketed_namespaces));
9863
0
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
9864
0
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9865
0
      "the very first statement or after any declare call in the script");
9866
0
  }
9867
9868
0
  if (FC(current_namespace)) {
9869
0
    zend_string_release_ex(FC(current_namespace), 0);
9870
0
  }
9871
9872
0
  if (name_ast) {
9873
0
    name = zend_ast_get_str(name_ast);
9874
9875
0
    if (zend_string_equals_literal_ci(name, "namespace")) {
9876
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9877
0
    }
9878
9879
0
    FC(current_namespace) = zend_string_copy(name);
9880
0
  } else {
9881
0
    FC(current_namespace) = NULL;
9882
0
  }
9883
9884
0
  zend_reset_import_tables();
9885
9886
0
  FC(in_namespace) = 1;
9887
0
  if (with_bracket) {
9888
0
    FC(has_bracketed_namespaces) = 1;
9889
0
  }
9890
9891
0
  if (stmt_ast) {
9892
0
    zend_compile_top_stmt(stmt_ast);
9893
0
    zend_end_namespace();
9894
0
  }
9895
0
}
9896
/* }}} */
9897
9898
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
9899
0
{
9900
0
  zend_ast *offset_ast = ast->child[0];
9901
0
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9902
9903
0
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9904
9905
0
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9906
0
    zend_error_noreturn(E_COMPILE_ERROR,
9907
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9908
0
  }
9909
9910
0
  const zend_string *filename = zend_get_compiled_filename();
9911
0
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9912
0
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
9913
9914
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9915
   * case this file was already included. */
9916
0
  if (!zend_hash_find(EG(zend_constants), name)) {
9917
0
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9918
0
  }
9919
0
  zend_string_release_ex(name, 0);
9920
0
}
9921
/* }}} */
9922
9923
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
9924
26
{
9925
26
  const zend_op_array *op_array = CG(active_op_array);
9926
26
  const zend_class_entry *ce = CG(active_class_entry);
9927
9928
26
  switch (ast->attr) {
9929
0
    case T_LINE:
9930
0
      ZVAL_LONG(zv, ast->lineno);
9931
0
      break;
9932
26
    case T_FILE:
9933
26
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9934
26
      break;
9935
0
    case T_DIR:
9936
0
    {
9937
0
      const zend_string *filename = CG(compiled_filename);
9938
0
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9939
#ifdef ZEND_WIN32
9940
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9941
#else
9942
0
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9943
0
#endif
9944
9945
0
      if (zend_string_equals_literal(dirname, ".")) {
9946
0
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9947
0
#ifdef HAVE_GETCWD
9948
0
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9949
#elif defined(HAVE_GETWD)
9950
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9951
#endif
9952
0
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9953
0
      }
9954
9955
0
      ZVAL_STR(zv, dirname);
9956
0
      break;
9957
0
    }
9958
0
    case T_FUNC_C:
9959
0
      if (op_array && op_array->function_name) {
9960
0
        ZVAL_STR_COPY(zv, op_array->function_name);
9961
0
      } else {
9962
0
        ZVAL_EMPTY_STRING(zv);
9963
0
      }
9964
0
      break;
9965
0
    case T_PROPERTY_C: {
9966
0
      zend_string *prop_info_name = CG(context).active_property_info_name;
9967
0
      if (prop_info_name) {
9968
0
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9969
0
      } else {
9970
0
        ZVAL_EMPTY_STRING(zv);
9971
0
      }
9972
0
      break;
9973
0
    }
9974
0
    case T_METHOD_C:
9975
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9976
       * this as not being inside a function. */
9977
0
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9978
0
        op_array = NULL;
9979
0
      }
9980
0
      if (op_array && op_array->function_name) {
9981
0
        if (op_array->scope) {
9982
0
          ZVAL_NEW_STR(zv,
9983
0
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9984
0
        } else {
9985
0
          ZVAL_STR_COPY(zv, op_array->function_name);
9986
0
        }
9987
0
      } else {
9988
0
        ZVAL_EMPTY_STRING(zv);
9989
0
      }
9990
0
      break;
9991
0
    case T_CLASS_C:
9992
0
      if (ce) {
9993
0
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
9994
0
          return 0;
9995
0
        } else {
9996
0
          ZVAL_STR_COPY(zv, ce->name);
9997
0
        }
9998
0
      } else {
9999
0
        ZVAL_EMPTY_STRING(zv);
10000
0
      }
10001
0
      break;
10002
0
    case T_TRAIT_C:
10003
0
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10004
0
        ZVAL_STR_COPY(zv, ce->name);
10005
0
      } else {
10006
0
        ZVAL_EMPTY_STRING(zv);
10007
0
      }
10008
0
      break;
10009
0
    case T_NS_C:
10010
0
      if (FC(current_namespace)) {
10011
0
        ZVAL_STR_COPY(zv, FC(current_namespace));
10012
0
      } else {
10013
0
        ZVAL_EMPTY_STRING(zv);
10014
0
      }
10015
0
      break;
10016
26
    EMPTY_SWITCH_DEFAULT_CASE()
10017
26
  }
10018
10019
26
  return 1;
10020
26
}
10021
/* }}} */
10022
10023
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10024
12
{
10025
12
  if (Z_TYPE_P(op) == IS_ARRAY) {
10026
0
    return false;
10027
0
  }
10028
10029
12
  if (Z_TYPE_P(op) == IS_DOUBLE
10030
4
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10031
4
    return false;
10032
4
  }
10033
10034
8
  if (Z_TYPE_P(op) == IS_STRING) {
10035
0
    double dval = 0;
10036
0
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10037
0
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10038
0
      return false;
10039
0
    }
10040
0
  }
10041
10042
8
  return true;
10043
8
}
10044
10045
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10046
435
{
10047
435
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10048
    /* Array to string warning. */
10049
28
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10050
28
  }
10051
10052
407
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10053
149
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10054
129
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10055
    /* Only the numeric operations throw errors. */
10056
57
    return 0;
10057
57
  }
10058
10059
350
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10060
0
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10061
      /* Adding two arrays is allowed. */
10062
0
      return 0;
10063
0
    }
10064
10065
    /* Numeric operators throw when one of the operands is an array. */
10066
0
    return 1;
10067
0
  }
10068
10069
  /* While basic arithmetic operators always produce numeric string errors,
10070
   * bitwise operators don't produce errors if both operands are strings */
10071
350
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10072
72
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10073
62
    return 0;
10074
62
  }
10075
10076
288
  if (Z_TYPE_P(op1) == IS_STRING
10077
272
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10078
258
    return 1;
10079
258
  }
10080
10081
30
  if (Z_TYPE_P(op2) == IS_STRING
10082
14
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10083
14
    return 1;
10084
14
  }
10085
10086
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10087
16
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10088
16
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10089
4
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10090
0
      return 1;
10091
0
    }
10092
4
  }
10093
10094
16
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10095
    /* Division by zero throws an error. */
10096
0
    return 1;
10097
0
  }
10098
10099
  /* Mod is an operation that will cast float/float-strings to integers which might
10100
     produce float to int incompatible errors, and also cannot be divided by 0 */
10101
16
  if (opcode == ZEND_MOD) {
10102
4
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10103
4
      return 1;
10104
4
    }
10105
4
  }
10106
10107
12
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10108
    /* 0 ** (<0) throws a division by zero error. */
10109
0
    return 1;
10110
0
  }
10111
12
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10112
    /* Shift by negative number throws an error. */
10113
0
    return 1;
10114
0
  }
10115
10116
12
  return 0;
10117
12
}
10118
/* }}} */
10119
10120
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10121
149
{
10122
149
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10123
73
    return 0;
10124
73
  }
10125
10126
76
  const binary_op_type fn = get_binary_op(opcode);
10127
76
  fn(result, op1, op2);
10128
76
  return 1;
10129
149
}
10130
/* }}} */
10131
10132
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10133
10
{
10134
10
  if (opcode == ZEND_BW_NOT) {
10135
    /* BW_NOT on string does not convert the string into an integer. */
10136
0
    if (Z_TYPE_P(op) == IS_STRING) {
10137
0
      return 0;
10138
0
    }
10139
0
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10140
0
  }
10141
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10142
10
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10143
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10144
10
    return Z_TYPE_P(op) == IS_DOUBLE;
10145
10
  }
10146
10147
0
  return 0;
10148
10
}
10149
10150
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10151
10
{
10152
10
  if (zend_unary_op_produces_error(opcode, op)) {
10153
0
    return 0;
10154
0
  }
10155
10156
10
  const unary_op_type fn = get_unary_op(opcode);
10157
10
  fn(result, op);
10158
10
  return 1;
10159
10
}
10160
/* }}} */
10161
10162
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10163
5
{
10164
5
  zval right;
10165
5
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10166
5
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10167
5
}
10168
/* }}} */
10169
10170
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10171
10
{
10172
10
  const binary_op_type fn = kind == ZEND_AST_GREATER
10173
10
    ? is_smaller_function : is_smaller_or_equal_function;
10174
10
  fn(result, op2, op1);
10175
10
}
10176
/* }}} */
10177
10178
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10179
48
{
10180
48
  const zend_ast_list *list = zend_ast_get_list(ast);
10181
48
  zend_ast *last_elem_ast = NULL;
10182
48
  uint32_t i;
10183
48
  bool is_constant = true;
10184
10185
48
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10186
0
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10187
0
  }
10188
10189
  /* First ensure that *all* child nodes are constant and by-val */
10190
179
  for (i = 0; i < list->children; ++i) {
10191
131
    zend_ast *elem_ast = list->child[i];
10192
10193
131
    if (elem_ast == NULL) {
10194
      /* Report error at line of last non-empty element */
10195
0
      if (last_elem_ast) {
10196
0
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10197
0
      }
10198
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10199
0
    }
10200
10201
131
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10202
131
      zend_eval_const_expr(&elem_ast->child[0]);
10203
131
      zend_eval_const_expr(&elem_ast->child[1]);
10204
10205
131
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10206
127
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10207
131
      ) {
10208
5
        is_constant = false;
10209
5
      }
10210
131
    } else {
10211
0
      zend_eval_const_expr(&elem_ast->child[0]);
10212
10213
0
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10214
0
        is_constant = false;
10215
0
      }
10216
0
    }
10217
10218
131
    last_elem_ast = elem_ast;
10219
131
  }
10220
10221
48
  if (!is_constant) {
10222
1
    return 0;
10223
1
  }
10224
10225
47
  if (!list->children) {
10226
36
    ZVAL_EMPTY_ARRAY(result);
10227
36
    return 1;
10228
36
  }
10229
10230
11
  array_init_size(result, list->children);
10231
136
  for (i = 0; i < list->children; ++i) {
10232
125
    const zend_ast *elem_ast = list->child[i];
10233
125
    zend_ast *value_ast = elem_ast->child[0];
10234
125
    zend_ast *key_ast;
10235
10236
125
    zval *value = zend_ast_get_zval(value_ast);
10237
125
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10238
0
      if (Z_TYPE_P(value) == IS_ARRAY) {
10239
0
        const HashTable *ht = Z_ARRVAL_P(value);
10240
0
        zval *val;
10241
0
        zend_string *key;
10242
10243
0
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10244
0
          if (key) {
10245
0
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10246
0
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10247
0
            zval_ptr_dtor(result);
10248
0
            return 0;
10249
0
          }
10250
0
          Z_TRY_ADDREF_P(val);
10251
0
        } ZEND_HASH_FOREACH_END();
10252
10253
0
        continue;
10254
0
      } else {
10255
0
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10256
0
      }
10257
0
    }
10258
10259
125
    Z_TRY_ADDREF_P(value);
10260
10261
125
    key_ast = elem_ast->child[1];
10262
125
    if (key_ast) {
10263
0
      const zval *key = zend_ast_get_zval(key_ast);
10264
0
      switch (Z_TYPE_P(key)) {
10265
0
        case IS_LONG:
10266
0
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10267
0
          break;
10268
0
        case IS_STRING:
10269
0
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10270
0
          break;
10271
0
        case IS_DOUBLE: {
10272
0
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10273
          /* Incompatible float will generate an error, leave this to run-time */
10274
0
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10275
0
            goto fail;
10276
0
          }
10277
0
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10278
0
          break;
10279
0
        }
10280
0
        case IS_FALSE:
10281
0
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10282
0
          break;
10283
0
        case IS_TRUE:
10284
0
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10285
0
          break;
10286
0
        case IS_NULL:
10287
          /* Null key will generate a warning at run-time. */
10288
0
          goto fail;
10289
0
        default:
10290
0
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10291
0
          break;
10292
0
      }
10293
125
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10294
0
fail:
10295
0
      zval_ptr_dtor_nogc(value);
10296
0
      zval_ptr_dtor(result);
10297
0
      return 0;
10298
0
    }
10299
125
  }
10300
10301
11
  return 1;
10302
11
}
10303
/* }}} */
10304
10305
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10306
503
{
10307
503
  zend_ast *left_ast = ast->child[0];
10308
503
  zend_ast *right_ast = ast->child[1];
10309
503
  uint32_t opcode = ast->attr;
10310
10311
503
  znode left_node, right_node;
10312
10313
503
  zend_compile_expr(&left_node, left_ast);
10314
503
  zend_compile_expr(&right_node, right_ast);
10315
10316
503
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10317
140
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10318
140
        &left_node.u.constant, &right_node.u.constant)
10319
140
    ) {
10320
67
      result->op_type = IS_CONST;
10321
67
      zval_ptr_dtor(&left_node.u.constant);
10322
67
      zval_ptr_dtor(&right_node.u.constant);
10323
67
      return;
10324
67
    }
10325
140
  }
10326
10327
436
  do {
10328
436
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10329
      /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
10330
18
      if (left_node.op_type == IS_CONST) {
10331
0
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10332
0
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10333
0
          opline->extended_value =
10334
0
            (opcode == ZEND_IS_IDENTICAL) ?
10335
0
              (1 << Z_TYPE(left_node.u.constant)) :
10336
0
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10337
0
          return;
10338
0
        }
10339
18
      } else if (right_node.op_type == IS_CONST) {
10340
18
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10341
8
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10342
8
          opline->extended_value =
10343
8
            (opcode == ZEND_IS_IDENTICAL) ?
10344
8
              (1 << Z_TYPE(right_node.u.constant)) :
10345
8
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10346
8
          return;
10347
8
        }
10348
18
      }
10349
418
    } else if (opcode == ZEND_CONCAT) {
10350
      /* convert constant operands to strings at compile-time */
10351
98
      if (left_node.op_type == IS_CONST) {
10352
48
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10353
0
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10354
48
        } else {
10355
48
          convert_to_string(&left_node.u.constant);
10356
48
        }
10357
48
      }
10358
98
      if (right_node.op_type == IS_CONST) {
10359
44
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10360
0
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10361
44
        } else {
10362
44
          convert_to_string(&right_node.u.constant);
10363
44
        }
10364
44
      }
10365
98
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10366
0
        opcode = ZEND_FAST_CONCAT;
10367
0
      }
10368
98
    }
10369
428
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10370
428
  } while (0);
10371
436
}
10372
/* }}} */
10373
10374
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10375
 * evaluation order. */
10376
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10377
27
{
10378
27
  zend_ast *left_ast = ast->child[0];
10379
27
  zend_ast *right_ast = ast->child[1];
10380
27
  znode left_node, right_node;
10381
10382
27
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10383
10384
27
  zend_compile_expr(&left_node, left_ast);
10385
27
  zend_compile_expr(&right_node, right_ast);
10386
10387
27
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10388
10
    result->op_type = IS_CONST;
10389
10
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10390
10
      &left_node.u.constant, &right_node.u.constant);
10391
10
    zval_ptr_dtor(&left_node.u.constant);
10392
10
    zval_ptr_dtor(&right_node.u.constant);
10393
10
    return;
10394
10
  }
10395
10396
17
  zend_emit_op_tmp(result,
10397
17
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10398
17
    &right_node, &left_node);
10399
17
}
10400
/* }}} */
10401
10402
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10403
22
{
10404
22
  zend_ast *expr_ast = ast->child[0];
10405
22
  uint32_t opcode = ast->attr;
10406
10407
22
  znode expr_node;
10408
22
  zend_compile_expr(&expr_node, expr_ast);
10409
10410
22
  if (expr_node.op_type == IS_CONST
10411
10
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10412
10
    result->op_type = IS_CONST;
10413
10
    zval_ptr_dtor(&expr_node.u.constant);
10414
10
    return;
10415
10
  }
10416
10417
12
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10418
12
}
10419
/* }}} */
10420
10421
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10422
4
{
10423
4
  zend_ast *expr_ast = ast->child[0];
10424
4
  znode expr_node, right_node;
10425
10426
4
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10427
10428
4
  zend_compile_expr(&expr_node, expr_ast);
10429
10430
4
  if (expr_node.op_type == IS_CONST
10431
0
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10432
0
    result->op_type = IS_CONST;
10433
0
    zval_ptr_dtor(&expr_node.u.constant);
10434
0
    return;
10435
0
  }
10436
10437
4
  right_node.op_type = IS_CONST;
10438
4
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10439
4
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10440
4
}
10441
/* }}} */
10442
10443
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10444
26
{
10445
26
  zend_ast *left_ast = ast->child[0];
10446
26
  zend_ast *right_ast = ast->child[1];
10447
10448
26
  znode left_node, right_node;
10449
26
  zend_op *opline_jmpz, *opline_bool;
10450
26
  uint32_t opnum_jmpz;
10451
10452
26
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10453
10454
26
  zend_compile_expr(&left_node, left_ast);
10455
10456
26
  if (left_node.op_type == IS_CONST) {
10457
8
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10458
8
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10459
2
      result->op_type = IS_CONST;
10460
2
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10461
6
    } else {
10462
6
      zend_compile_expr(&right_node, right_ast);
10463
10464
6
      if (right_node.op_type == IS_CONST) {
10465
0
        result->op_type = IS_CONST;
10466
0
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10467
10468
0
        zval_ptr_dtor(&right_node.u.constant);
10469
6
      } else {
10470
6
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10471
6
      }
10472
6
    }
10473
10474
8
    zval_ptr_dtor(&left_node.u.constant);
10475
8
    return;
10476
8
  }
10477
10478
18
  opnum_jmpz = get_next_op_number();
10479
18
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10480
18
    &left_node, NULL);
10481
10482
18
  if (left_node.op_type == IS_TMP_VAR) {
10483
18
    SET_NODE(opline_jmpz->result, &left_node);
10484
18
    GET_NODE(result, opline_jmpz->result);
10485
18
  } else {
10486
0
    zend_make_tmp_result(result, opline_jmpz);
10487
0
  }
10488
10489
18
  zend_compile_expr(&right_node, right_ast);
10490
10491
18
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10492
18
  SET_NODE(opline_bool->result, result);
10493
10494
18
  zend_update_jump_target_to_next(opnum_jmpz);
10495
18
}
10496
/* }}} */
10497
10498
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10499
42
{
10500
42
  zend_ast *var_ast = ast->child[0];
10501
42
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10502
10503
42
  zend_ensure_writable_variable(var_ast);
10504
10505
42
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10506
0
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10507
0
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10508
0
    zend_make_tmp_result(result, opline);
10509
42
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10510
0
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10511
0
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10512
0
    zend_make_tmp_result(result, opline);
10513
42
  } else {
10514
42
    znode var_node;
10515
42
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10516
42
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10517
0
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10518
0
    }
10519
42
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10520
42
      &var_node, NULL);
10521
42
  }
10522
42
}
10523
/* }}} */
10524
10525
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10526
11
{
10527
11
  zend_ast *var_ast = ast->child[0];
10528
11
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10529
10530
11
  zend_ensure_writable_variable(var_ast);
10531
10532
11
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10533
0
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10534
0
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10535
0
    opline->result_type = IS_TMP_VAR;
10536
0
    result->op_type = IS_TMP_VAR;
10537
11
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10538
0
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10539
0
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10540
0
    opline->result_type = IS_TMP_VAR;
10541
0
    result->op_type = IS_TMP_VAR;
10542
11
  } else {
10543
11
    znode var_node;
10544
11
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10545
11
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10546
0
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10547
0
    }
10548
11
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10549
11
      &var_node, NULL);
10550
11
  }
10551
11
}
10552
/* }}} */
10553
10554
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10555
0
{
10556
0
  zend_ast *expr_ast = ast->child[0];
10557
0
  znode expr_node;
10558
0
  zend_op *opline;
10559
10560
0
  zend_compile_expr(&expr_node, expr_ast);
10561
10562
0
  if (ast->attr == _IS_BOOL) {
10563
0
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10564
0
  } else if (ast->attr == IS_NULL) {
10565
0
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10566
0
  } else {
10567
0
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10568
0
    opline->extended_value = ast->attr;
10569
0
  }
10570
0
}
10571
/* }}} */
10572
10573
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10574
12
{
10575
12
  zend_ast *cond_ast = ast->child[0];
10576
12
  zend_ast *false_ast = ast->child[2];
10577
10578
12
  znode cond_node, false_node;
10579
12
  zend_op *opline_qm_assign;
10580
12
  uint32_t opnum_jmp_set;
10581
10582
12
  ZEND_ASSERT(ast->child[1] == NULL);
10583
10584
12
  zend_compile_expr(&cond_node, cond_ast);
10585
10586
12
  opnum_jmp_set = get_next_op_number();
10587
12
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10588
10589
12
  zend_compile_expr(&false_node, false_ast);
10590
10591
12
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10592
12
  SET_NODE(opline_qm_assign->result, result);
10593
10594
12
  zend_update_jump_target_to_next(opnum_jmp_set);
10595
12
}
10596
/* }}} */
10597
10598
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10599
12
{
10600
12
  zend_ast *cond_ast = ast->child[0];
10601
12
  zend_ast *true_ast = ast->child[1];
10602
12
  zend_ast *false_ast = ast->child[2];
10603
10604
12
  znode cond_node, true_node, false_node;
10605
12
  zend_op *opline_qm_assign2;
10606
12
  uint32_t opnum_jmpz, opnum_jmp;
10607
10608
12
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10609
0
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10610
0
    if (cond_ast->child[1]) {
10611
0
      if (true_ast) {
10612
0
        zend_error(E_COMPILE_ERROR,
10613
0
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10614
0
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10615
0
      } else {
10616
0
        zend_error(E_COMPILE_ERROR,
10617
0
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10618
0
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10619
0
      }
10620
0
    } else {
10621
0
      if (true_ast) {
10622
0
        zend_error(E_COMPILE_ERROR,
10623
0
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10624
0
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10625
0
      } else {
10626
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10627
         * as a ?: (b ?: c). */
10628
0
      }
10629
0
    }
10630
0
  }
10631
10632
12
  if (!true_ast) {
10633
12
    zend_compile_shorthand_conditional(result, ast);
10634
12
    return;
10635
12
  }
10636
10637
0
  zend_compile_expr(&cond_node, cond_ast);
10638
10639
0
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10640
10641
0
  zend_compile_expr(&true_node, true_ast);
10642
10643
0
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10644
10645
0
  opnum_jmp = zend_emit_jump(0);
10646
10647
0
  zend_update_jump_target_to_next(opnum_jmpz);
10648
10649
0
  zend_compile_expr(&false_node, false_ast);
10650
10651
0
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10652
0
  SET_NODE(opline_qm_assign2->result, result);
10653
10654
0
  zend_update_jump_target_to_next(opnum_jmp);
10655
0
}
10656
/* }}} */
10657
10658
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10659
32
{
10660
32
  zend_ast *expr_ast = ast->child[0];
10661
32
  zend_ast *default_ast = ast->child[1];
10662
10663
32
  znode expr_node, default_node;
10664
32
  zend_op *opline;
10665
32
  uint32_t opnum;
10666
10667
32
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10668
10669
32
  opnum = get_next_op_number();
10670
32
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10671
10672
32
  zend_compile_expr(&default_node, default_ast);
10673
10674
32
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10675
32
  SET_NODE(opline->result, result);
10676
10677
32
  opline = &CG(active_op_array)->opcodes[opnum];
10678
32
  opline->op2.opline_num = get_next_op_number();
10679
32
}
10680
/* }}} */
10681
10682
0
static void znode_dtor(zval *zv) {
10683
0
  znode *node = Z_PTR_P(zv);
10684
0
  if (node->op_type == IS_CONST) {
10685
0
    zval_ptr_dtor_nogc(&node->u.constant);
10686
0
  }
10687
0
  efree(node);
10688
0
}
10689
10690
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10691
0
{
10692
0
  zend_ast *var_ast = ast->child[0];
10693
0
  zend_ast *default_ast = ast->child[1];
10694
10695
0
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10696
0
  zend_op *opline;
10697
0
  uint32_t coalesce_opnum;
10698
0
  bool need_frees = false;
10699
10700
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10701
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10702
0
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10703
0
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10704
10705
0
  zend_ensure_writable_variable(var_ast);
10706
0
  if (is_this_fetch(var_ast)) {
10707
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10708
0
  }
10709
10710
0
  ALLOC_HASHTABLE(CG(memoized_exprs));
10711
0
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10712
10713
0
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10714
0
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10715
10716
0
  coalesce_opnum = get_next_op_number();
10717
0
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10718
10719
0
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10720
0
  if (var_ast->kind == ZEND_AST_DIM) {
10721
0
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10722
0
  } else {
10723
0
    zend_compile_expr(&default_node, default_ast);
10724
0
  }
10725
10726
0
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10727
0
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10728
10729
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10730
0
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10731
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10732
0
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10733
0
  switch (kind) {
10734
0
    case ZEND_AST_VAR:
10735
0
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10736
0
      break;
10737
0
    case ZEND_AST_STATIC_PROP:
10738
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
10739
0
      opline->result_type = IS_TMP_VAR;
10740
0
      var_node_w.op_type = IS_TMP_VAR;
10741
0
      zend_emit_op_data(&default_node);
10742
0
      assign_node = var_node_w;
10743
0
      break;
10744
0
    case ZEND_AST_DIM:
10745
0
      opline->opcode = ZEND_ASSIGN_DIM;
10746
0
      opline->result_type = IS_TMP_VAR;
10747
0
      var_node_w.op_type = IS_TMP_VAR;
10748
0
      zend_emit_op_data(&default_node);
10749
0
      assign_node = var_node_w;
10750
0
      break;
10751
0
    case ZEND_AST_PROP:
10752
0
    case ZEND_AST_NULLSAFE_PROP:
10753
0
      opline->opcode = ZEND_ASSIGN_OBJ;
10754
0
      opline->result_type = IS_TMP_VAR;
10755
0
      var_node_w.op_type = IS_TMP_VAR;
10756
0
      zend_emit_op_data(&default_node);
10757
0
      assign_node = var_node_w;
10758
0
      break;
10759
0
    EMPTY_SWITCH_DEFAULT_CASE();
10760
0
  }
10761
10762
0
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10763
0
  SET_NODE(opline->result, result);
10764
10765
0
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10766
0
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10767
0
      need_frees = true;
10768
0
      break;
10769
0
    }
10770
0
  } ZEND_HASH_FOREACH_END();
10771
10772
  /* Free DUPed expressions if there are any */
10773
0
  if (need_frees) {
10774
0
    uint32_t jump_opnum = zend_emit_jump(0);
10775
0
    zend_update_jump_target_to_next(coalesce_opnum);
10776
0
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10777
0
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10778
0
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10779
0
      }
10780
0
    } ZEND_HASH_FOREACH_END();
10781
0
    zend_update_jump_target_to_next(jump_opnum);
10782
0
  } else {
10783
0
    zend_update_jump_target_to_next(coalesce_opnum);
10784
0
  }
10785
10786
0
  zend_hash_destroy(CG(memoized_exprs));
10787
0
  FREE_HASHTABLE(CG(memoized_exprs));
10788
0
  CG(memoized_exprs) = orig_memoized_exprs;
10789
0
  CG(memoize_mode) = orig_memoize_mode;
10790
0
}
10791
/* }}} */
10792
10793
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10794
0
{
10795
0
  zend_op *opline;
10796
0
  zend_ast *expr_ast = ast->child[0];
10797
10798
0
  znode expr_node;
10799
0
  zend_compile_expr(&expr_node, expr_ast);
10800
10801
0
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10802
0
  opline->extended_value = 1;
10803
10804
0
  result->op_type = IS_CONST;
10805
0
  ZVAL_LONG(&result->u.constant, 1);
10806
0
}
10807
/* }}} */
10808
10809
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10810
0
{
10811
0
  zend_ast *value_ast = ast->child[0];
10812
0
  zend_ast *key_ast = ast->child[1];
10813
10814
0
  znode value_node, key_node;
10815
0
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10816
0
  zend_op *opline;
10817
0
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10818
10819
0
  zend_mark_function_as_generator();
10820
10821
0
  if (key_ast) {
10822
0
    zend_compile_expr(&key_node, key_ast);
10823
0
    key_node_ptr = &key_node;
10824
0
  }
10825
10826
0
  if (value_ast) {
10827
0
    if (returns_by_ref && zend_is_variable(value_ast)) {
10828
0
      zend_assert_not_short_circuited(value_ast);
10829
0
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
10830
0
    } else {
10831
0
      zend_compile_expr(&value_node, value_ast);
10832
0
    }
10833
0
    value_node_ptr = &value_node;
10834
0
  }
10835
10836
0
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10837
10838
0
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10839
0
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10840
0
  }
10841
0
}
10842
/* }}} */
10843
10844
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
10845
0
{
10846
0
  zend_ast *expr_ast = ast->child[0];
10847
0
  znode expr_node;
10848
10849
0
  zend_mark_function_as_generator();
10850
10851
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10852
0
    zend_error_noreturn(E_COMPILE_ERROR,
10853
0
      "Cannot use \"yield from\" inside a by-reference generator");
10854
0
  }
10855
10856
0
  zend_compile_expr(&expr_node, expr_ast);
10857
0
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10858
0
}
10859
/* }}} */
10860
10861
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10862
0
{
10863
0
  zend_ast *obj_ast = ast->child[0];
10864
0
  zend_ast *class_ast = ast->child[1];
10865
10866
0
  znode obj_node, class_node;
10867
0
  zend_op *opline;
10868
10869
0
  zend_compile_expr(&obj_node, obj_ast);
10870
0
  if (obj_node.op_type == IS_CONST) {
10871
0
    zend_do_free(&obj_node);
10872
0
    result->op_type = IS_CONST;
10873
0
    ZVAL_FALSE(&result->u.constant);
10874
0
    return;
10875
0
  }
10876
10877
0
  zend_compile_class_ref(&class_node, class_ast,
10878
0
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10879
10880
0
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10881
10882
0
  if (class_node.op_type == IS_CONST) {
10883
0
    opline->op2_type = IS_CONST;
10884
0
    opline->op2.constant = zend_add_class_name_literal(
10885
0
      Z_STR(class_node.u.constant));
10886
0
    opline->extended_value = zend_alloc_cache_slot();
10887
0
  } else {
10888
0
    SET_NODE(opline->op2, &class_node);
10889
0
  }
10890
0
}
10891
/* }}} */
10892
10893
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
10894
32
{
10895
32
  zend_ast *expr_ast = ast->child[0];
10896
32
  znode expr_node;
10897
32
  zend_op *opline;
10898
10899
32
  zend_do_extended_fcall_begin();
10900
32
  zend_compile_expr(&expr_node, expr_ast);
10901
10902
32
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10903
32
  opline->extended_value = ast->attr;
10904
10905
32
  zend_do_extended_fcall_end();
10906
32
}
10907
/* }}} */
10908
10909
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
10910
0
{
10911
0
  zend_ast *var_ast = ast->child[0];
10912
10913
0
  znode var_node;
10914
0
  zend_op *opline = NULL;
10915
10916
0
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10917
10918
0
  if (!zend_is_variable(var_ast)) {
10919
0
    if (ast->kind == ZEND_AST_EMPTY) {
10920
      /* empty(expr) can be transformed to !expr */
10921
0
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10922
0
      zend_compile_expr(result, not_ast);
10923
0
      return;
10924
0
    } else {
10925
0
      zend_error_noreturn(E_COMPILE_ERROR,
10926
0
        "Cannot use isset() on the result of an expression "
10927
0
        "(you can use \"null !== expression\" instead)");
10928
0
    }
10929
0
  }
10930
10931
0
  if (is_globals_fetch(var_ast)) {
10932
0
    result->op_type = IS_CONST;
10933
0
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10934
0
    return;
10935
0
  }
10936
10937
0
  if (is_global_var_fetch(var_ast)) {
10938
0
    if (!var_ast->child[1]) {
10939
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10940
0
    }
10941
10942
0
    zend_compile_expr(&var_node, var_ast->child[1]);
10943
0
    if (var_node.op_type == IS_CONST) {
10944
0
      convert_to_string(&var_node.u.constant);
10945
0
    }
10946
10947
0
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10948
0
    opline->extended_value =
10949
0
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10950
0
    return;
10951
0
  }
10952
10953
0
  zend_short_circuiting_mark_inner(var_ast);
10954
0
  switch (var_ast->kind) {
10955
0
    case ZEND_AST_VAR:
10956
0
      if (is_this_fetch(var_ast)) {
10957
0
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10958
0
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10959
0
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
10960
0
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10961
0
      } else {
10962
0
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
10963
0
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10964
0
      }
10965
0
      break;
10966
0
    case ZEND_AST_DIM:
10967
0
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10968
0
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10969
0
      break;
10970
0
    case ZEND_AST_PROP:
10971
0
    case ZEND_AST_NULLSAFE_PROP:
10972
0
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
10973
0
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10974
0
      break;
10975
0
    case ZEND_AST_STATIC_PROP:
10976
0
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
10977
0
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10978
0
      break;
10979
0
    EMPTY_SWITCH_DEFAULT_CASE()
10980
0
  }
10981
10982
0
  result->op_type = opline->result_type = IS_TMP_VAR;
10983
0
  if (!(ast->kind == ZEND_AST_ISSET)) {
10984
0
    opline->extended_value |= ZEND_ISEMPTY;
10985
0
  }
10986
0
}
10987
/* }}} */
10988
10989
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
10990
0
{
10991
0
  zend_ast *expr_ast = ast->child[0];
10992
0
  znode silence_node;
10993
10994
0
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
10995
10996
0
  if (expr_ast->kind == ZEND_AST_VAR) {
10997
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
10998
     * happen outside the silenced section. */
10999
0
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11000
0
  } else {
11001
0
    zend_compile_expr(result, expr_ast);
11002
0
  }
11003
11004
0
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11005
0
}
11006
/* }}} */
11007
11008
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11009
0
{
11010
0
  zend_ast *expr_ast = ast->child[0];
11011
11012
0
  zval fn_name;
11013
0
  zend_ast *name_ast, *args_ast, *call_ast;
11014
11015
0
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11016
11017
0
  ZVAL_STRING(&fn_name, "shell_exec");
11018
0
  name_ast = zend_ast_create_zval(&fn_name);
11019
0
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11020
0
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11021
11022
0
  zend_compile_expr(result, call_ast);
11023
11024
0
  zval_ptr_dtor(&fn_name);
11025
0
}
11026
/* }}} */
11027
11028
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11029
48
{
11030
48
  zend_ast_list *list = zend_ast_get_list(ast);
11031
48
  zend_op *opline;
11032
48
  uint32_t i, opnum_init = -1;
11033
48
  bool packed = true;
11034
11035
48
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11036
47
    result->op_type = IS_CONST;
11037
47
    return;
11038
47
  }
11039
11040
  /* Empty arrays are handled at compile-time */
11041
1
  ZEND_ASSERT(list->children > 0);
11042
11043
7
  for (i = 0; i < list->children; ++i) {
11044
6
    zend_ast *elem_ast = list->child[i];
11045
6
    zend_ast *value_ast, *key_ast;
11046
6
    bool by_ref;
11047
6
    znode value_node, key_node, *key_node_ptr = NULL;
11048
11049
6
    if (elem_ast == NULL) {
11050
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11051
0
    }
11052
11053
6
    value_ast = elem_ast->child[0];
11054
11055
6
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11056
0
      zend_compile_expr(&value_node, value_ast);
11057
0
      if (i == 0) {
11058
0
        opnum_init = get_next_op_number();
11059
0
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11060
0
      }
11061
0
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11062
0
      SET_NODE(opline->result, result);
11063
0
      continue;
11064
0
    }
11065
11066
6
    key_ast = elem_ast->child[1];
11067
6
    by_ref = elem_ast->attr;
11068
11069
6
    if (key_ast) {
11070
2
      zend_compile_expr(&key_node, key_ast);
11071
2
      zend_handle_numeric_op(&key_node);
11072
2
      key_node_ptr = &key_node;
11073
2
    }
11074
11075
6
    if (by_ref) {
11076
0
      zend_ensure_writable_variable(value_ast);
11077
0
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11078
6
    } else {
11079
6
      zend_compile_expr(&value_node, value_ast);
11080
6
    }
11081
11082
6
    if (i == 0) {
11083
1
      opnum_init = get_next_op_number();
11084
1
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11085
1
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11086
5
    } else {
11087
5
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11088
5
        &value_node, key_node_ptr);
11089
5
      SET_NODE(opline->result, result);
11090
5
    }
11091
6
    opline->extended_value |= by_ref;
11092
11093
6
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11094
1
      packed = false;
11095
1
    }
11096
6
  }
11097
11098
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11099
1
  if (!packed) {
11100
1
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11101
1
    opline = &CG(active_op_array)->opcodes[opnum_init];
11102
1
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11103
1
  }
11104
1
}
11105
/* }}} */
11106
11107
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11108
38
{
11109
38
  zend_ast *name_ast = ast->child[0];
11110
11111
38
  zend_op *opline;
11112
11113
38
  bool is_fully_qualified;
11114
38
  zend_string *orig_name = zend_ast_get_str(name_ast);
11115
38
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11116
11117
38
  if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
11118
0
    zend_ast *last = CG(ast);
11119
11120
0
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11121
0
      const zend_ast_list *list = zend_ast_get_list(last);
11122
0
      if (list->children == 0) {
11123
0
        break;
11124
0
      }
11125
0
      last = list->child[list->children-1];
11126
0
    }
11127
0
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11128
0
      result->op_type = IS_CONST;
11129
0
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11130
0
      zend_string_release_ex(resolved_name, 0);
11131
0
      return;
11132
0
    }
11133
0
  }
11134
11135
38
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11136
1
    result->op_type = IS_CONST;
11137
1
    zend_string_release_ex(resolved_name, 0);
11138
1
    return;
11139
1
  }
11140
11141
37
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11142
37
  opline->op2_type = IS_CONST;
11143
11144
37
  if (is_fully_qualified || !FC(current_namespace)) {
11145
37
    opline->op1.num = 0;
11146
37
    opline->op2.constant = zend_add_const_name_literal(
11147
37
      resolved_name, false);
11148
37
  } else {
11149
0
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11150
0
    opline->op2.constant = zend_add_const_name_literal(
11151
0
      resolved_name, true);
11152
0
  }
11153
37
  opline->extended_value = zend_alloc_cache_slot();
11154
37
}
11155
/* }}} */
11156
11157
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11158
4
{
11159
4
  zend_ast *class_ast;
11160
4
  zend_ast *const_ast;
11161
4
  znode class_node, const_node;
11162
4
  zend_op *opline;
11163
11164
4
  zend_eval_const_expr(&ast->child[0]);
11165
4
  zend_eval_const_expr(&ast->child[1]);
11166
11167
4
  class_ast = ast->child[0];
11168
4
  const_ast = ast->child[1];
11169
11170
4
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11171
4
    zval *const_zv = zend_ast_get_zval(const_ast);
11172
4
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11173
4
      zend_string *const_str = Z_STR_P(const_zv);
11174
4
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11175
4
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11176
0
        result->op_type = IS_CONST;
11177
0
        zend_string_release_ex(resolved_name, 0);
11178
0
        return;
11179
0
      }
11180
4
      zend_string_release_ex(resolved_name, 0);
11181
4
    }
11182
4
  }
11183
11184
4
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11185
11186
4
  zend_compile_expr(&const_node, const_ast);
11187
11188
4
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11189
11190
4
  zend_set_class_name_op1(opline, &class_node);
11191
11192
4
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11193
4
    opline->extended_value = zend_alloc_cache_slots(2);
11194
4
  }
11195
4
}
11196
/* }}} */
11197
11198
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11199
0
{
11200
0
  zend_ast *class_ast = ast->child[0];
11201
11202
0
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11203
0
    result->op_type = IS_CONST;
11204
0
    return;
11205
0
  }
11206
11207
0
  if (class_ast->kind == ZEND_AST_ZVAL) {
11208
0
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11209
0
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11210
0
  } else {
11211
0
    znode expr_node;
11212
0
    zend_compile_expr(&expr_node, class_ast);
11213
0
    if (expr_node.op_type == IS_CONST) {
11214
      /* Unlikely case that happen if class_ast is constant folded.
11215
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11216
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11217
0
        zend_zval_value_name(&expr_node.u.constant));
11218
0
    }
11219
11220
0
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11221
0
  }
11222
0
}
11223
/* }}} */
11224
11225
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11226
1.14k
{
11227
1.14k
  if (num == 0) {
11228
46
    result->op_type = IS_TMP_VAR;
11229
46
    result->u.op.var = -1;
11230
46
    opline->opcode = ZEND_ROPE_INIT;
11231
1.10k
  } else {
11232
1.10k
    opline->opcode = ZEND_ROPE_ADD;
11233
1.10k
    SET_NODE(opline->op1, result);
11234
1.10k
  }
11235
1.14k
  SET_NODE(opline->op2, elem_node);
11236
1.14k
  SET_NODE(opline->result, result);
11237
1.14k
  opline->extended_value = num;
11238
1.14k
  return opline;
11239
1.14k
}
11240
/* }}} */
11241
11242
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11243
1.12k
{
11244
1.12k
  zend_op *opline = get_next_op();
11245
11246
1.12k
  if (num == 0) {
11247
1
    result->op_type = IS_TMP_VAR;
11248
1
    result->u.op.var = -1;
11249
1
    opline->opcode = ZEND_ROPE_INIT;
11250
1.12k
  } else {
11251
1.12k
    opline->opcode = ZEND_ROPE_ADD;
11252
1.12k
    SET_NODE(opline->op1, result);
11253
1.12k
  }
11254
1.12k
  SET_NODE(opline->op2, elem_node);
11255
1.12k
  SET_NODE(opline->result, result);
11256
1.12k
  opline->extended_value = num;
11257
1.12k
  return opline;
11258
1.12k
}
11259
/* }}} */
11260
11261
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11262
47
{
11263
47
  if (rope_elements == 1) {
11264
0
    if (opline->op2_type == IS_CONST) {
11265
0
      GET_NODE(result, opline->op2);
11266
0
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11267
0
      SET_UNUSED(opline->op2);
11268
0
      MAKE_NOP(opline);
11269
0
    } else {
11270
0
      opline->opcode = ZEND_CAST;
11271
0
      opline->extended_value = IS_STRING;
11272
0
      opline->op1_type = opline->op2_type;
11273
0
      opline->op1 = opline->op2;
11274
0
      SET_UNUSED(opline->op2);
11275
0
      zend_make_tmp_result(result, opline);
11276
0
    }
11277
47
  } else if (rope_elements == 2) {
11278
11
    opline->opcode = ZEND_FAST_CONCAT;
11279
11
    opline->extended_value = 0;
11280
11
    opline->op1_type = init_opline->op2_type;
11281
11
    opline->op1 = init_opline->op2;
11282
11
    zend_make_tmp_result(result, opline);
11283
11
    MAKE_NOP(init_opline);
11284
36
  } else {
11285
36
    uint32_t var;
11286
11287
36
    init_opline->extended_value = rope_elements;
11288
36
    opline->opcode = ZEND_ROPE_END;
11289
36
    zend_make_tmp_result(result, opline);
11290
36
    var = opline->op1.var = get_temporary_variable();
11291
11292
    /* Allocates the necessary number of zval slots to keep the rope */
11293
36
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11294
1.14k
    while (i > 1) {
11295
1.10k
      get_temporary_variable();
11296
1.10k
      i--;
11297
1.10k
    }
11298
11299
    /* Update all the previous opcodes to use the same variable */
11300
2.93k
    while (opline != init_opline) {
11301
2.90k
      opline--;
11302
2.90k
      if (opline->opcode == ZEND_ROPE_ADD &&
11303
2.17k
          opline->result.var == (uint32_t)-1) {
11304
2.17k
        opline->op1.var = var;
11305
2.17k
        opline->result.var = var;
11306
2.17k
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11307
36
                 opline->result.var == (uint32_t)-1) {
11308
36
        opline->result.var = var;
11309
36
      }
11310
2.90k
    }
11311
36
  }
11312
47
}
11313
11314
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11315
47
{
11316
47
  uint32_t i, j;
11317
47
  uint32_t rope_init_lineno = -1;
11318
47
  zend_op *opline = NULL, *init_opline;
11319
47
  znode elem_node, last_const_node;
11320
47
  zend_ast_list *list = zend_ast_get_list(ast);
11321
47
  uint32_t reserved_op_number = -1;
11322
11323
47
  ZEND_ASSERT(list->children > 0);
11324
11325
47
  j = 0;
11326
47
  last_const_node.op_type = IS_UNUSED;
11327
2.31k
  for (i = 0; i < list->children; i++) {
11328
2.27k
    zend_ast *encaps_var = list->child[i];
11329
11330
2.27k
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11331
0
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11332
0
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11333
0
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11334
0
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11335
0
      }
11336
0
    }
11337
11338
2.27k
    zend_compile_expr(&elem_node, encaps_var);
11339
11340
2.27k
    if (elem_node.op_type == IS_CONST) {
11341
1.14k
      convert_to_string(&elem_node.u.constant);
11342
11343
1.14k
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11344
0
        zval_ptr_dtor(&elem_node.u.constant);
11345
1.14k
      } else if (last_const_node.op_type == IS_CONST) {
11346
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11347
0
        zval_ptr_dtor(&elem_node.u.constant);
11348
1.14k
      } else {
11349
1.14k
        last_const_node.op_type = IS_CONST;
11350
1.14k
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11351
        /* Reserve place for ZEND_ROPE_ADD instruction */
11352
1.14k
        reserved_op_number = get_next_op_number();
11353
1.14k
        opline = get_next_op();
11354
1.14k
        opline->opcode = ZEND_NOP;
11355
1.14k
      }
11356
1.14k
      continue;
11357
1.14k
    } else {
11358
1.12k
      if (j == 0) {
11359
47
        if (last_const_node.op_type == IS_CONST) {
11360
46
          rope_init_lineno = reserved_op_number;
11361
46
        } else {
11362
1
          rope_init_lineno = get_next_op_number();
11363
1
        }
11364
47
      }
11365
1.12k
      if (last_const_node.op_type == IS_CONST) {
11366
1.10k
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11367
1.10k
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11368
1.10k
        last_const_node.op_type = IS_UNUSED;
11369
1.10k
      }
11370
1.12k
      opline = zend_compile_rope_add(result, j++, &elem_node);
11371
1.12k
    }
11372
2.27k
  }
11373
11374
47
  if (j == 0) {
11375
0
    result->op_type = IS_CONST;
11376
0
    if (last_const_node.op_type == IS_CONST) {
11377
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11378
0
    } else {
11379
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11380
      /* empty string */
11381
0
    }
11382
0
    CG(active_op_array)->last = reserved_op_number - 1;
11383
0
    return;
11384
47
  } else if (last_const_node.op_type == IS_CONST) {
11385
37
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11386
37
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11387
37
  }
11388
47
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11389
47
  zend_compile_rope_finalize(result, j, init_opline, opline);
11390
47
}
11391
/* }}} */
11392
11393
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11394
26
{
11395
26
  zend_op *opline;
11396
11397
26
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11398
26
    result->op_type = IS_CONST;
11399
26
    return;
11400
26
  }
11401
11402
0
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11403
0
              CG(active_class_entry) &&
11404
0
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11405
11406
0
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11407
0
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11408
0
}
11409
/* }}} */
11410
11411
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11412
4
{
11413
4
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11414
4
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11415
4
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11416
4
    || kind == ZEND_AST_UNARY_OP
11417
4
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11418
4
    || kind == ZEND_AST_CAST
11419
4
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11420
4
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11421
4
    || kind == ZEND_AST_UNPACK
11422
4
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11423
4
    || kind == ZEND_AST_CLASS_NAME
11424
4
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11425
4
    || kind == ZEND_AST_CONST_ENUM_INIT
11426
0
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11427
0
    || kind == ZEND_AST_NAMED_ARG
11428
0
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11429
0
    || kind == ZEND_AST_CLOSURE
11430
0
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11431
4
}
11432
/* }}} */
11433
11434
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11435
0
{
11436
0
  zend_ast *ast = *ast_ptr;
11437
0
  zend_ast *class_ast = ast->child[0];
11438
0
  zend_string *class_name;
11439
0
  int fetch_type;
11440
11441
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11442
0
    zend_error_noreturn(E_COMPILE_ERROR,
11443
0
      "Dynamic class names are not allowed in compile-time class constant references");
11444
0
  }
11445
0
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11446
0
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11447
0
  }
11448
11449
0
  class_name = zend_ast_get_str(class_ast);
11450
0
  fetch_type = zend_get_class_fetch_type(class_name);
11451
11452
0
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11453
0
    zend_error_noreturn(E_COMPILE_ERROR,
11454
0
      "\"static::\" is not allowed in compile-time constants");
11455
0
  }
11456
11457
0
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11458
0
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11459
11460
0
    zend_string_release_ex(class_name, 0);
11461
0
    if (tmp != class_name) {
11462
0
      zval *zv = zend_ast_get_zval(class_ast);
11463
0
      ZVAL_STR(zv, tmp);
11464
0
      class_ast->attr = ZEND_NAME_FQ;
11465
0
    }
11466
0
  }
11467
11468
0
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11469
0
}
11470
/* }}} */
11471
11472
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11473
0
{
11474
0
  zend_ast *ast = *ast_ptr;
11475
0
  zend_ast *class_ast = ast->child[0];
11476
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11477
0
    zend_error_noreturn(E_COMPILE_ERROR,
11478
0
      "(expression)::class cannot be used in constant expressions");
11479
0
  }
11480
11481
0
  zend_string *class_name = zend_ast_get_str(class_ast);
11482
0
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11483
11484
0
  switch (fetch_type) {
11485
0
    case ZEND_FETCH_CLASS_SELF:
11486
0
    case ZEND_FETCH_CLASS_PARENT:
11487
      /* For the const-eval representation store the fetch type instead of the name. */
11488
0
      zend_string_release(class_name);
11489
0
      ast->child[0] = NULL;
11490
0
      ast->attr = fetch_type;
11491
0
      return;
11492
0
    case ZEND_FETCH_CLASS_STATIC:
11493
0
      zend_error_noreturn(E_COMPILE_ERROR,
11494
0
        "static::class cannot be used for compile-time class name resolution");
11495
0
      return;
11496
0
    EMPTY_SWITCH_DEFAULT_CASE()
11497
0
  }
11498
0
}
11499
11500
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11501
0
{
11502
0
  zend_ast *ast = *ast_ptr;
11503
0
  zend_ast *name_ast = ast->child[0];
11504
0
  zend_string *orig_name = zend_ast_get_str(name_ast);
11505
0
  bool is_fully_qualified;
11506
0
  zval result;
11507
0
  zend_string *resolved_name;
11508
11509
0
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11510
11511
0
  resolved_name = zend_resolve_const_name(
11512
0
    orig_name, name_ast->attr, &is_fully_qualified);
11513
11514
0
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11515
0
    zend_string_release_ex(resolved_name, 0);
11516
0
    zend_ast_destroy(ast);
11517
0
    *ast_ptr = zend_ast_create_zval(&result);
11518
0
    return;
11519
0
  }
11520
11521
0
  zend_ast_destroy(ast);
11522
0
  *ast_ptr = zend_ast_create_constant(resolved_name,
11523
0
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11524
0
}
11525
/* }}} */
11526
11527
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11528
0
{
11529
0
  zend_ast *ast = *ast_ptr;
11530
11531
  /* Other cases already resolved by constant folding */
11532
0
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11533
11534
0
  zend_ast_destroy(ast);
11535
0
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11536
0
}
11537
/* }}} */
11538
11539
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11540
0
{
11541
0
  if (class_ast->kind == ZEND_AST_CLASS) {
11542
0
    zend_error_noreturn(E_COMPILE_ERROR,
11543
0
      "Cannot use anonymous class in constant expression");
11544
0
  }
11545
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11546
0
    zend_error_noreturn(E_COMPILE_ERROR,
11547
0
      "Cannot use dynamic class name in constant expression");
11548
0
  }
11549
11550
0
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11551
0
  int fetch_type = zend_get_class_fetch_type(class_name);
11552
0
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11553
0
    zend_error_noreturn(E_COMPILE_ERROR,
11554
0
      "\"static\" is not allowed in compile-time constants");
11555
0
  }
11556
11557
0
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11558
0
  zval_ptr_dtor_nogc(class_ast_zv);
11559
0
  ZVAL_STR(class_ast_zv, class_name);
11560
0
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11561
0
}
11562
11563
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11564
0
{
11565
0
  zend_ast *class_ast = (*ast_ptr)->child[0];
11566
0
  zend_compile_const_expr_class_reference(class_ast);
11567
11568
0
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11569
0
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11570
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11571
0
  }
11572
0
}
11573
11574
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11575
0
{
11576
0
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11577
0
  const zend_ast *uses_ast = closure_ast->child[1];
11578
0
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11579
0
    zend_error_noreturn(E_COMPILE_ERROR,
11580
0
      "Closures in constant expressions must be static");
11581
0
  }
11582
0
  if (uses_ast) {
11583
0
    zend_error_noreturn(E_COMPILE_ERROR,
11584
0
      "Cannot use(...) variables in constant expression");
11585
0
  }
11586
11587
0
  znode node;
11588
0
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11589
11590
0
  zend_ast_destroy(*ast_ptr);
11591
0
  *ast_ptr = zend_ast_create_op_array(op);
11592
0
}
11593
11594
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11595
0
{
11596
0
  zend_ast **args_ast;
11597
0
  switch ((*ast_ptr)->kind) {
11598
0
    case ZEND_AST_CALL:
11599
0
      args_ast = &(*ast_ptr)->child[1];
11600
0
      break;
11601
0
    case ZEND_AST_STATIC_CALL:
11602
0
      args_ast = &(*ast_ptr)->child[2];
11603
0
      break;
11604
0
    EMPTY_SWITCH_DEFAULT_CASE();
11605
0
  }
11606
0
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11607
0
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11608
0
  }
11609
0
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11610
11611
0
  switch ((*ast_ptr)->kind) {
11612
0
    case ZEND_AST_CALL: {
11613
0
      zend_ast *name_ast = (*ast_ptr)->child[0];
11614
0
      if (name_ast->kind != ZEND_AST_ZVAL) {
11615
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11616
0
      }
11617
0
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11618
0
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11619
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11620
0
      }
11621
0
      bool is_fully_qualified;
11622
0
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11623
0
      zval_ptr_dtor_nogc(name_ast_zv);
11624
0
      ZVAL_STR(name_ast_zv, name);
11625
0
      if (is_fully_qualified) {
11626
0
        name_ast->attr = ZEND_NAME_FQ;
11627
0
      }
11628
0
      break;
11629
0
    }
11630
0
    case ZEND_AST_STATIC_CALL: {
11631
0
      zend_ast *class_ast = (*ast_ptr)->child[0];
11632
0
      zend_compile_const_expr_class_reference(class_ast);
11633
0
      zend_ast *method_ast = (*ast_ptr)->child[1];
11634
0
      if (method_ast->kind != ZEND_AST_ZVAL) {
11635
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11636
0
      }
11637
0
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11638
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11639
0
      }
11640
0
      break;
11641
0
    }
11642
0
    EMPTY_SWITCH_DEFAULT_CASE();
11643
0
  }
11644
0
}
11645
11646
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11647
0
{
11648
0
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11649
0
  bool uses_named_args = false;
11650
0
  for (uint32_t i = 0; i < list->children; i++) {
11651
0
    const zend_ast *arg = list->child[i];
11652
0
    if (arg->kind == ZEND_AST_UNPACK) {
11653
0
      zend_error_noreturn(E_COMPILE_ERROR,
11654
0
        "Argument unpacking in constant expressions is not supported");
11655
0
    }
11656
0
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11657
0
      uses_named_args = true;
11658
0
    } else if (uses_named_args) {
11659
0
      zend_error_noreturn(E_COMPILE_ERROR,
11660
0
        "Cannot use positional argument after named argument");
11661
0
    }
11662
0
  }
11663
0
  if (uses_named_args) {
11664
0
    list->attr = 1;
11665
0
  }
11666
0
}
11667
11668
typedef struct {
11669
  /* Whether the value of this expression may differ on each evaluation. */
11670
  bool allow_dynamic;
11671
} const_expr_context;
11672
11673
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11674
16
{
11675
16
  const const_expr_context *ctx = context;
11676
16
  zend_ast *ast = *ast_ptr;
11677
16
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11678
12
    return;
11679
12
  }
11680
11681
4
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11682
0
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11683
0
  }
11684
11685
4
  switch (ast->kind) {
11686
0
    case ZEND_AST_CLASS_CONST:
11687
0
      zend_compile_const_expr_class_const(ast_ptr);
11688
0
      break;
11689
0
    case ZEND_AST_CLASS_NAME:
11690
0
      zend_compile_const_expr_class_name(ast_ptr);
11691
0
      break;
11692
0
    case ZEND_AST_CONST:
11693
0
      zend_compile_const_expr_const(ast_ptr);
11694
0
      break;
11695
0
    case ZEND_AST_MAGIC_CONST:
11696
0
      zend_compile_const_expr_magic_const(ast_ptr);
11697
0
      break;
11698
0
    case ZEND_AST_CAST:
11699
0
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11700
0
        zend_error_noreturn(E_COMPILE_ERROR,
11701
0
          "Object casts are not supported in this context");
11702
0
      }
11703
0
      break;
11704
0
    case ZEND_AST_NEW:
11705
0
      if (!ctx->allow_dynamic) {
11706
0
        zend_error_noreturn(E_COMPILE_ERROR,
11707
0
          "New expressions are not supported in this context");
11708
0
      }
11709
0
      zend_compile_const_expr_new(ast_ptr);
11710
0
      break;
11711
0
    case ZEND_AST_ARG_LIST:
11712
0
      zend_compile_const_expr_args(ast_ptr);
11713
0
      break;
11714
0
    case ZEND_AST_CLOSURE:
11715
0
      zend_compile_const_expr_closure(ast_ptr);
11716
      /* Return, because we do not want to traverse the children. */
11717
0
      return;
11718
0
    case ZEND_AST_CALL:
11719
0
    case ZEND_AST_STATIC_CALL:
11720
0
      zend_compile_const_expr_fcc(ast_ptr);
11721
0
      break;
11722
4
  }
11723
11724
4
  zend_ast_apply(ast, zend_compile_const_expr, context);
11725
4
}
11726
/* }}} */
11727
11728
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11729
4
{
11730
4
  const_expr_context context;
11731
4
  context.allow_dynamic = allow_dynamic;
11732
11733
4
  zend_eval_const_expr(ast_ptr);
11734
4
  zend_compile_const_expr(ast_ptr, &context);
11735
4
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11736
    /* Replace with compiled AST zval representation. */
11737
4
    zval ast_zv;
11738
4
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11739
4
    zend_ast_destroy(*ast_ptr);
11740
4
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11741
4
  }
11742
4
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11743
4
}
11744
/* }}} */
11745
11746
/* Same as compile_stmt, but with early binding */
11747
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11748
556
{
11749
556
  if (!ast) {
11750
70
    return;
11751
70
  }
11752
11753
486
  if (ast->kind == ZEND_AST_STMT_LIST) {
11754
74
    const zend_ast_list *list = zend_ast_get_list(ast);
11755
74
    uint32_t i;
11756
563
    for (i = 0; i < list->children; ++i) {
11757
489
      zend_compile_top_stmt(list->child[i]);
11758
489
    }
11759
74
    return;
11760
74
  }
11761
11762
412
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11763
8
    CG(zend_lineno) = ast->lineno;
11764
8
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11765
8
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11766
404
  } else if (ast->kind == ZEND_AST_CLASS) {
11767
15
    CG(zend_lineno) = ast->lineno;
11768
15
    zend_compile_class_decl(NULL, ast, true);
11769
15
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11770
389
  } else {
11771
389
    zend_compile_stmt(ast);
11772
389
  }
11773
412
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11774
412
    zend_verify_namespace();
11775
412
  }
11776
412
}
11777
/* }}} */
11778
11779
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11780
936
{
11781
936
  if (!ast) {
11782
2
    return;
11783
2
  }
11784
11785
934
  CG(zend_lineno) = ast->lineno;
11786
11787
934
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11788
0
    zend_do_extended_stmt(NULL);
11789
0
  }
11790
11791
934
  switch (ast->kind) {
11792
189
    case ZEND_AST_STMT_LIST:
11793
189
      zend_compile_stmt_list(ast);
11794
189
      break;
11795
0
    case ZEND_AST_GLOBAL:
11796
0
      zend_compile_global_var(ast);
11797
0
      break;
11798
0
    case ZEND_AST_STATIC:
11799
0
      zend_compile_static_var(ast);
11800
0
      break;
11801
0
    case ZEND_AST_UNSET:
11802
0
      zend_compile_unset(ast);
11803
0
      break;
11804
2
    case ZEND_AST_RETURN:
11805
2
      zend_compile_return(ast);
11806
2
      break;
11807
136
    case ZEND_AST_ECHO:
11808
136
      zend_compile_echo(ast);
11809
136
      break;
11810
0
    case ZEND_AST_BREAK:
11811
0
    case ZEND_AST_CONTINUE:
11812
0
      zend_compile_break_continue(ast);
11813
0
      break;
11814
0
    case ZEND_AST_GOTO:
11815
0
      zend_compile_goto(ast);
11816
0
      break;
11817
0
    case ZEND_AST_LABEL:
11818
0
      zend_compile_label(ast);
11819
0
      break;
11820
0
    case ZEND_AST_WHILE:
11821
0
      zend_compile_while(ast);
11822
0
      break;
11823
0
    case ZEND_AST_DO_WHILE:
11824
0
      zend_compile_do_while(ast);
11825
0
      break;
11826
72
    case ZEND_AST_FOR:
11827
72
      zend_compile_for(ast);
11828
72
      break;
11829
12
    case ZEND_AST_FOREACH:
11830
12
      zend_compile_foreach(ast);
11831
12
      break;
11832
4
    case ZEND_AST_IF:
11833
4
      zend_compile_if(ast);
11834
4
      break;
11835
0
    case ZEND_AST_SWITCH:
11836
0
      zend_compile_switch(ast);
11837
0
      break;
11838
46
    case ZEND_AST_TRY:
11839
46
      zend_compile_try(ast);
11840
46
      break;
11841
0
    case ZEND_AST_DECLARE:
11842
0
      zend_compile_declare(ast);
11843
0
      break;
11844
0
    case ZEND_AST_FUNC_DECL:
11845
12
    case ZEND_AST_METHOD:
11846
12
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11847
12
      break;
11848
4
    case ZEND_AST_ENUM_CASE:
11849
4
      zend_compile_enum_case(ast);
11850
4
      break;
11851
0
    case ZEND_AST_PROP_GROUP:
11852
0
      zend_compile_prop_group(ast);
11853
0
      break;
11854
0
    case ZEND_AST_CLASS_CONST_GROUP:
11855
0
      zend_compile_class_const_group(ast);
11856
0
      break;
11857
0
    case ZEND_AST_USE_TRAIT:
11858
0
      zend_compile_use_trait(ast);
11859
0
      break;
11860
0
    case ZEND_AST_CLASS:
11861
0
      zend_compile_class_decl(NULL, ast, false);
11862
0
      break;
11863
0
    case ZEND_AST_GROUP_USE:
11864
0
      zend_compile_group_use(ast);
11865
0
      break;
11866
0
    case ZEND_AST_USE:
11867
0
      zend_compile_use(ast);
11868
0
      break;
11869
0
    case ZEND_AST_CONST_DECL:
11870
0
      zend_compile_const_decl(ast);
11871
0
      break;
11872
0
    case ZEND_AST_NAMESPACE:
11873
0
      zend_compile_namespace(ast);
11874
0
      break;
11875
0
    case ZEND_AST_HALT_COMPILER:
11876
0
      zend_compile_halt_compiler(ast);
11877
0
      break;
11878
0
    case ZEND_AST_THROW:
11879
0
      zend_compile_expr(NULL, ast);
11880
0
      break;
11881
0
    case ZEND_AST_CAST_VOID:
11882
0
      zend_compile_void_cast(NULL, ast);
11883
0
      break;
11884
457
    default:
11885
457
    {
11886
457
      znode result;
11887
457
      zend_compile_expr(&result, ast);
11888
457
      zend_do_free(&result);
11889
457
    }
11890
934
  }
11891
11892
934
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11893
0
    zend_emit_tick();
11894
0
  }
11895
934
}
11896
/* }}} */
11897
11898
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11899
5.51k
{
11900
  /* CG(zend_lineno) = ast->lineno; */
11901
5.51k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11902
11903
5.51k
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11904
0
    zend_compile_memoized_expr(result, ast);
11905
0
    return;
11906
0
  }
11907
11908
5.51k
  switch (ast->kind) {
11909
2.47k
    case ZEND_AST_ZVAL:
11910
2.47k
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11911
2.47k
      result->op_type = IS_CONST;
11912
2.47k
      return;
11913
1
    case ZEND_AST_ZNODE:
11914
1
      *result = *zend_ast_get_znode(ast);
11915
1
      return;
11916
933
    case ZEND_AST_VAR:
11917
933
    case ZEND_AST_DIM:
11918
1.16k
    case ZEND_AST_PROP:
11919
1.39k
    case ZEND_AST_NULLSAFE_PROP:
11920
1.39k
    case ZEND_AST_STATIC_PROP:
11921
1.52k
    case ZEND_AST_CALL:
11922
1.53k
    case ZEND_AST_METHOD_CALL:
11923
1.53k
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11924
1.53k
    case ZEND_AST_STATIC_CALL:
11925
1.53k
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11926
1.53k
      zend_compile_var(result, ast, BP_VAR_R, false);
11927
1.53k
      return;
11928
380
    case ZEND_AST_ASSIGN:
11929
380
      zend_compile_assign(result, ast);
11930
380
      return;
11931
3
    case ZEND_AST_ASSIGN_REF:
11932
3
      zend_compile_assign_ref(result, ast);
11933
3
      return;
11934
155
    case ZEND_AST_NEW:
11935
155
      zend_compile_new(result, ast);
11936
155
      return;
11937
85
    case ZEND_AST_ASSIGN_OP:
11938
85
      zend_compile_compound_assign(result, ast);
11939
85
      return;
11940
503
    case ZEND_AST_BINARY_OP:
11941
503
      zend_compile_binary_op(result, ast);
11942
503
      return;
11943
27
    case ZEND_AST_GREATER:
11944
27
    case ZEND_AST_GREATER_EQUAL:
11945
27
      zend_compile_greater(result, ast);
11946
27
      return;
11947
22
    case ZEND_AST_UNARY_OP:
11948
22
      zend_compile_unary_op(result, ast);
11949
22
      return;
11950
4
    case ZEND_AST_UNARY_PLUS:
11951
4
    case ZEND_AST_UNARY_MINUS:
11952
4
      zend_compile_unary_pm(result, ast);
11953
4
      return;
11954
18
    case ZEND_AST_AND:
11955
26
    case ZEND_AST_OR:
11956
26
      zend_compile_short_circuiting(result, ast);
11957
26
      return;
11958
42
    case ZEND_AST_POST_INC:
11959
42
    case ZEND_AST_POST_DEC:
11960
42
      zend_compile_post_incdec(result, ast);
11961
42
      return;
11962
11
    case ZEND_AST_PRE_INC:
11963
11
    case ZEND_AST_PRE_DEC:
11964
11
      zend_compile_pre_incdec(result, ast);
11965
11
      return;
11966
0
    case ZEND_AST_CAST:
11967
0
      zend_compile_cast(result, ast);
11968
0
      return;
11969
12
    case ZEND_AST_CONDITIONAL:
11970
12
      zend_compile_conditional(result, ast);
11971
12
      return;
11972
32
    case ZEND_AST_COALESCE:
11973
32
      zend_compile_coalesce(result, ast);
11974
32
      return;
11975
0
    case ZEND_AST_ASSIGN_COALESCE:
11976
0
      zend_compile_assign_coalesce(result, ast);
11977
0
      return;
11978
0
    case ZEND_AST_PRINT:
11979
0
      zend_compile_print(result, ast);
11980
0
      return;
11981
0
    case ZEND_AST_YIELD:
11982
0
      zend_compile_yield(result, ast);
11983
0
      return;
11984
0
    case ZEND_AST_YIELD_FROM:
11985
0
      zend_compile_yield_from(result, ast);
11986
0
      return;
11987
0
    case ZEND_AST_INSTANCEOF:
11988
0
      zend_compile_instanceof(result, ast);
11989
0
      return;
11990
32
    case ZEND_AST_INCLUDE_OR_EVAL:
11991
32
      zend_compile_include_or_eval(result, ast);
11992
32
      return;
11993
0
    case ZEND_AST_ISSET:
11994
0
    case ZEND_AST_EMPTY:
11995
0
      zend_compile_isset_or_empty(result, ast);
11996
0
      return;
11997
0
    case ZEND_AST_SILENCE:
11998
0
      zend_compile_silence(result, ast);
11999
0
      return;
12000
0
    case ZEND_AST_SHELL_EXEC:
12001
0
      zend_compile_shell_exec(result, ast);
12002
0
      return;
12003
48
    case ZEND_AST_ARRAY:
12004
48
      zend_compile_array(result, ast);
12005
48
      return;
12006
38
    case ZEND_AST_CONST:
12007
38
      zend_compile_const(result, ast);
12008
38
      return;
12009
4
    case ZEND_AST_CLASS_CONST:
12010
4
      zend_compile_class_const(result, ast);
12011
4
      return;
12012
0
    case ZEND_AST_CLASS_NAME:
12013
0
      zend_compile_class_name(result, ast);
12014
0
      return;
12015
47
    case ZEND_AST_ENCAPS_LIST:
12016
47
      zend_compile_encaps_list(result, ast);
12017
47
      return;
12018
26
    case ZEND_AST_MAGIC_CONST:
12019
26
      zend_compile_magic_const(result, ast);
12020
26
      return;
12021
0
    case ZEND_AST_CLOSURE:
12022
1
    case ZEND_AST_ARROW_FUNC:
12023
1
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12024
1
      return;
12025
0
    case ZEND_AST_THROW:
12026
0
      zend_compile_throw(result, ast);
12027
0
      return;
12028
0
    case ZEND_AST_MATCH:
12029
0
      zend_compile_match(result, ast);
12030
0
      return;
12031
0
    case ZEND_AST_PIPE:
12032
0
      zend_compile_pipe(result, ast);
12033
0
      return;
12034
0
    default:
12035
0
      ZEND_ASSERT(0 /* not supported */);
12036
5.51k
  }
12037
5.51k
}
12038
/* }}} */
12039
12040
static void zend_compile_expr(znode *result, zend_ast *ast)
12041
5.51k
{
12042
5.51k
  zend_check_stack_limit();
12043
12044
5.51k
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12045
5.51k
  zend_compile_expr_inner(result, ast);
12046
5.51k
  zend_short_circuiting_commit(checkpoint, result, ast);
12047
5.51k
}
12048
12049
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12050
1.84k
{
12051
1.84k
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12052
12053
1.84k
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12054
0
    switch (ast->kind) {
12055
0
      case ZEND_AST_CALL:
12056
0
      case ZEND_AST_METHOD_CALL:
12057
0
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12058
0
      case ZEND_AST_STATIC_CALL:
12059
0
        zend_compile_memoized_expr(result, ast);
12060
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12061
0
        return NULL;
12062
0
    }
12063
0
  }
12064
12065
1.84k
  switch (ast->kind) {
12066
1.14k
    case ZEND_AST_VAR:
12067
1.14k
      return zend_compile_simple_var(result, ast, type, false);
12068
0
    case ZEND_AST_DIM:
12069
0
      return zend_compile_dim(result, ast, type, by_ref);
12070
234
    case ZEND_AST_PROP:
12071
466
    case ZEND_AST_NULLSAFE_PROP:
12072
466
      return zend_compile_prop(result, ast, type, by_ref);
12073
0
    case ZEND_AST_STATIC_PROP:
12074
0
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12075
154
    case ZEND_AST_CALL:
12076
154
      zend_compile_call(result, ast, type);
12077
154
      return NULL;
12078
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12079
0
      zend_compile_parent_property_hook_call(result, ast, type);
12080
0
      return NULL;
12081
44
    case ZEND_AST_METHOD_CALL:
12082
44
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12083
44
      zend_compile_method_call(result, ast, type);
12084
44
      return NULL;
12085
0
    case ZEND_AST_STATIC_CALL:
12086
0
      zend_compile_static_call(result, ast, type);
12087
0
      return NULL;
12088
0
    case ZEND_AST_ZNODE:
12089
0
      *result = *zend_ast_get_znode(ast);
12090
0
      return NULL;
12091
32
    default:
12092
32
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12093
0
        zend_error_noreturn(E_COMPILE_ERROR,
12094
0
          "Cannot use temporary expression in write context");
12095
0
      }
12096
12097
32
      zend_compile_expr(result, ast);
12098
32
      return NULL;
12099
1.84k
  }
12100
1.84k
}
12101
12102
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12103
1.84k
{
12104
1.84k
  zend_check_stack_limit();
12105
12106
1.84k
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12107
1.84k
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12108
1.84k
  zend_short_circuiting_commit(checkpoint, result, ast);
12109
1.84k
  return opcode;
12110
1.84k
}
12111
12112
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12113
933
{
12114
933
  zend_check_stack_limit();
12115
12116
933
  switch (ast->kind) {
12117
930
    case ZEND_AST_VAR:
12118
930
      return zend_compile_simple_var(result, ast, type, true);
12119
3
    case ZEND_AST_DIM:
12120
3
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12121
0
    case ZEND_AST_PROP:
12122
0
    case ZEND_AST_NULLSAFE_PROP:
12123
0
    {
12124
0
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12125
0
      if (by_ref) {
12126
0
        opline->extended_value |= ZEND_FETCH_REF;
12127
0
      }
12128
0
      return opline;
12129
0
    }
12130
0
    case ZEND_AST_STATIC_PROP:
12131
0
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12132
0
    default:
12133
0
      return zend_compile_var(result, ast, type, false);
12134
933
  }
12135
933
}
12136
/* }}} */
12137
12138
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12139
6
{
12140
  /* NAN warns when casting */
12141
6
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12142
0
    return false;
12143
0
  }
12144
6
  switch (type) {
12145
0
    case _IS_BOOL:
12146
0
      ZVAL_BOOL(result, zend_is_true(op1));
12147
0
      return true;
12148
0
    case IS_LONG:
12149
0
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12150
0
        return false;
12151
0
      }
12152
0
      ZVAL_LONG(result, zval_get_long(op1));
12153
0
      return true;
12154
0
    case IS_DOUBLE:
12155
0
      ZVAL_DOUBLE(result, zval_get_double(op1));
12156
0
      return true;
12157
6
    case IS_STRING:
12158
      /* Conversion from double to string takes into account run-time
12159
         'precision' setting and cannot be evaluated at compile-time */
12160
6
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12161
6
        ZVAL_STR(result, zval_get_string(op1));
12162
6
        return true;
12163
6
      }
12164
0
      break;
12165
0
    case IS_ARRAY:
12166
0
      ZVAL_COPY(result, op1);
12167
0
      convert_to_array(result);
12168
0
      return true;
12169
6
  }
12170
0
  return false;
12171
6
}
12172
12173
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12174
291
{
12175
291
  zend_ast *ast = *ast_ptr;
12176
291
  zval result;
12177
12178
291
  if (!ast) {
12179
131
    return;
12180
131
  }
12181
12182
160
  zend_check_stack_limit();
12183
12184
160
  switch (ast->kind) {
12185
4
    case ZEND_AST_BINARY_OP:
12186
4
      zend_eval_const_expr(&ast->child[0]);
12187
4
      zend_eval_const_expr(&ast->child[1]);
12188
4
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12189
0
        return;
12190
0
      }
12191
12192
4
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12193
4
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12194
4
      ) {
12195
0
        return;
12196
0
      }
12197
4
      break;
12198
4
    case ZEND_AST_GREATER:
12199
0
    case ZEND_AST_GREATER_EQUAL:
12200
0
      zend_eval_const_expr(&ast->child[0]);
12201
0
      zend_eval_const_expr(&ast->child[1]);
12202
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12203
0
        return;
12204
0
      }
12205
12206
0
      zend_ct_eval_greater(&result, ast->kind,
12207
0
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12208
0
      break;
12209
0
    case ZEND_AST_AND:
12210
0
    case ZEND_AST_OR:
12211
0
    {
12212
0
      bool child0_is_true, child1_is_true;
12213
0
      zend_eval_const_expr(&ast->child[0]);
12214
0
      zend_eval_const_expr(&ast->child[1]);
12215
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12216
0
        return;
12217
0
      }
12218
12219
0
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12220
0
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12221
0
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12222
0
        break;
12223
0
      }
12224
12225
0
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12226
0
        return;
12227
0
      }
12228
12229
0
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12230
0
      if (ast->kind == ZEND_AST_OR) {
12231
0
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12232
0
      } else {
12233
0
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12234
0
      }
12235
0
      break;
12236
0
    }
12237
0
    case ZEND_AST_UNARY_OP:
12238
0
      zend_eval_const_expr(&ast->child[0]);
12239
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12240
0
        return;
12241
0
      }
12242
12243
0
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12244
0
        return;
12245
0
      }
12246
0
      break;
12247
0
    case ZEND_AST_UNARY_PLUS:
12248
5
    case ZEND_AST_UNARY_MINUS:
12249
5
      zend_eval_const_expr(&ast->child[0]);
12250
5
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12251
0
        return;
12252
0
      }
12253
12254
5
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12255
0
        return;
12256
0
      }
12257
5
      break;
12258
5
    case ZEND_AST_COALESCE:
12259
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12260
0
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12261
0
        ast->child[0]->attr |= ZEND_DIM_IS;
12262
0
      }
12263
0
      zend_eval_const_expr(&ast->child[0]);
12264
12265
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12266
        /* ensure everything was compile-time evaluated at least once */
12267
0
        zend_eval_const_expr(&ast->child[1]);
12268
0
        return;
12269
0
      }
12270
12271
0
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12272
0
        zend_eval_const_expr(&ast->child[1]);
12273
0
        *ast_ptr = ast->child[1];
12274
0
        ast->child[1] = NULL;
12275
0
        zend_ast_destroy(ast);
12276
0
      } else {
12277
0
        *ast_ptr = ast->child[0];
12278
0
        ast->child[0] = NULL;
12279
0
        zend_ast_destroy(ast);
12280
0
      }
12281
0
      return;
12282
0
    case ZEND_AST_CONDITIONAL:
12283
0
    {
12284
0
      zend_ast **child, *child_ast;
12285
0
      zend_eval_const_expr(&ast->child[0]);
12286
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12287
        /* ensure everything was compile-time evaluated at least once */
12288
0
        if (ast->child[1]) {
12289
0
          zend_eval_const_expr(&ast->child[1]);
12290
0
        }
12291
0
        zend_eval_const_expr(&ast->child[2]);
12292
0
        return;
12293
0
      }
12294
12295
0
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12296
0
      if (*child == NULL) {
12297
0
        child--;
12298
0
      }
12299
0
      child_ast = *child;
12300
0
      *child = NULL;
12301
0
      zend_ast_destroy(ast);
12302
0
      *ast_ptr = child_ast;
12303
0
      zend_eval_const_expr(ast_ptr);
12304
0
      return;
12305
0
    }
12306
0
    case ZEND_AST_DIM:
12307
0
    {
12308
      /* constant expression should be always read context ... */
12309
0
      const zval *container, *dim;
12310
12311
0
      if (ast->child[1] == NULL) {
12312
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12313
0
      }
12314
12315
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12316
0
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12317
0
        ast->child[0]->attr |= ZEND_DIM_IS;
12318
0
      }
12319
12320
0
      zend_eval_const_expr(&ast->child[0]);
12321
0
      zend_eval_const_expr(&ast->child[1]);
12322
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12323
0
        return;
12324
0
      }
12325
12326
0
      container = zend_ast_get_zval(ast->child[0]);
12327
0
      dim = zend_ast_get_zval(ast->child[1]);
12328
12329
0
      if (Z_TYPE_P(container) == IS_ARRAY) {
12330
0
        zval *el;
12331
0
        if (Z_TYPE_P(dim) == IS_LONG) {
12332
0
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12333
0
          if (el) {
12334
0
            ZVAL_COPY(&result, el);
12335
0
          } else {
12336
0
            return;
12337
0
          }
12338
0
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12339
0
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12340
0
          if (el) {
12341
0
            ZVAL_COPY(&result, el);
12342
0
          } else {
12343
0
            return;
12344
0
          }
12345
0
        } else {
12346
0
          return; /* warning... handle at runtime */
12347
0
        }
12348
0
      } else if (Z_TYPE_P(container) == IS_STRING) {
12349
0
        zend_long offset;
12350
0
        uint8_t c;
12351
0
        if (Z_TYPE_P(dim) == IS_LONG) {
12352
0
          offset = Z_LVAL_P(dim);
12353
0
        } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
12354
0
          return;
12355
0
        }
12356
0
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12357
0
          return;
12358
0
        }
12359
0
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12360
0
        ZVAL_CHAR(&result, c);
12361
0
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12362
0
        return; /* warning... handle at runtime */
12363
0
      } else {
12364
0
        return;
12365
0
      }
12366
0
      break;
12367
0
    }
12368
0
    case ZEND_AST_ARRAY:
12369
0
      if (!zend_try_ct_eval_array(&result, ast)) {
12370
0
        return;
12371
0
      }
12372
0
      break;
12373
0
    case ZEND_AST_MAGIC_CONST:
12374
0
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12375
0
        return;
12376
0
      }
12377
0
      break;
12378
0
    case ZEND_AST_CONST:
12379
0
    {
12380
0
      zend_ast *name_ast = ast->child[0];
12381
0
      bool is_fully_qualified;
12382
0
      zend_string *resolved_name = zend_resolve_const_name(
12383
0
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12384
12385
0
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12386
0
        zend_string_release_ex(resolved_name, 0);
12387
0
        return;
12388
0
      }
12389
12390
0
      zend_string_release_ex(resolved_name, 0);
12391
0
      break;
12392
0
    }
12393
0
    case ZEND_AST_CLASS_CONST:
12394
0
    {
12395
0
      zend_ast *class_ast;
12396
0
      zend_ast *name_ast;
12397
0
      zend_string *resolved_name;
12398
12399
0
      zend_eval_const_expr(&ast->child[0]);
12400
0
      zend_eval_const_expr(&ast->child[1]);
12401
12402
0
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12403
0
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12404
0
        return;
12405
0
      }
12406
12407
0
      class_ast = ast->child[0];
12408
0
      name_ast = ast->child[1];
12409
12410
0
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12411
0
        return;
12412
0
      }
12413
12414
0
      resolved_name = zend_resolve_class_name_ast(class_ast);
12415
0
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12416
0
        zend_string_release_ex(resolved_name, 0);
12417
0
        return;
12418
0
      }
12419
12420
0
      zend_string_release_ex(resolved_name, 0);
12421
0
      break;
12422
0
    }
12423
0
    case ZEND_AST_CLASS_NAME:
12424
0
    {
12425
0
      zend_ast *class_ast = ast->child[0];
12426
0
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12427
0
        return;
12428
0
      }
12429
0
      break;
12430
0
    }
12431
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12432
    // special handling. It is required that all nodes that are part of a const expr
12433
    // are visited. Probably we should be distinguishing evaluation of const expr and
12434
    // normal exprs here.
12435
0
    case ZEND_AST_ARG_LIST:
12436
0
    {
12437
0
      zend_ast_list *list = zend_ast_get_list(ast);
12438
0
      for (uint32_t i = 0; i < list->children; i++) {
12439
0
        zend_eval_const_expr(&list->child[i]);
12440
0
      }
12441
0
      return;
12442
0
    }
12443
0
    case ZEND_AST_NEW:
12444
0
      zend_eval_const_expr(&ast->child[0]);
12445
0
      zend_eval_const_expr(&ast->child[1]);
12446
0
      return;
12447
0
    case ZEND_AST_NAMED_ARG:
12448
0
      zend_eval_const_expr(&ast->child[1]);
12449
0
      return;
12450
4
    case ZEND_AST_CONST_ENUM_INIT:
12451
4
      zend_eval_const_expr(&ast->child[2]);
12452
4
      return;
12453
0
    case ZEND_AST_PROP:
12454
0
    case ZEND_AST_NULLSAFE_PROP:
12455
0
      zend_eval_const_expr(&ast->child[0]);
12456
0
      zend_eval_const_expr(&ast->child[1]);
12457
0
      return;
12458
0
    case ZEND_AST_CAST:
12459
0
      zend_eval_const_expr(&ast->child[0]);
12460
0
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12461
0
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12462
0
        break;
12463
0
      }
12464
0
      return;
12465
147
    default:
12466
147
      return;
12467
160
  }
12468
12469
9
  zend_ast_destroy(ast);
12470
9
  *ast_ptr = zend_ast_create_zval(&result);
12471
9
}
12472
/* }}} */