Coverage Report

Created: 2025-12-14 06:06

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
0
#define SET_NODE(target, src) do { \
44
0
    target ## _type = (src)->op_type; \
45
0
    if ((src)->op_type == IS_CONST) { \
46
0
      target.constant = zend_add_literal(&(src)->u.constant); \
47
0
    } else { \
48
0
      target = (src)->u.op; \
49
0
    } \
50
0
  } while (0)
51
52
0
#define GET_NODE(target, src) do { \
53
0
    (target)->op_type = src ## _type; \
54
0
    if ((target)->op_type == IS_CONST) { \
55
0
      ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
56
0
    } else { \
57
0
      (target)->u.op = src; \
58
0
    } \
59
0
  } while (0)
60
61
0
#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
0
static inline uint32_t zend_alloc_cache_slots(unsigned count) {
71
0
  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
0
  zend_op_array *op_array = CG(active_op_array);
79
0
  uint32_t ret = op_array->cache_size;
80
0
  op_array->cache_size += count * sizeof(void*);
81
0
  return ret;
82
0
}
83
84
0
static inline uint32_t zend_alloc_cache_slot(void) {
85
0
  return zend_alloc_cache_slots(1);
86
0
}
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
0
{
121
0
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
122
0
    zend_stack_limit_error();
123
0
  }
124
0
}
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
0
{
133
0
  MAKE_NOP(op);
134
0
  op->extended_value = 0;
135
0
  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
0
}
145
146
static zend_always_inline uint32_t get_next_op_number(void)
147
0
{
148
0
  return CG(active_op_array)->last;
149
0
}
150
151
static zend_op *get_next_op(void)
152
0
{
153
0
  zend_op_array *op_array = CG(active_op_array);
154
0
  uint32_t next_op_num = op_array->last++;
155
0
  zend_op *next_op;
156
157
0
  if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) {
158
0
    CG(context).opcodes_size *= 4;
159
0
    op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op));
160
0
  }
161
162
0
  next_op = &(op_array->opcodes[next_op_num]);
163
164
0
  init_op(next_op);
165
166
0
  return next_op;
167
0
}
168
169
static zend_brk_cont_element *get_next_brk_cont_element(void)
170
0
{
171
0
  CG(context).last_brk_cont++;
172
0
  CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont);
173
0
  return &CG(context).brk_cont_array[CG(context).last_brk_cont-1];
174
0
}
175
176
static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */
177
0
{
178
0
  zend_string *filename = CG(active_op_array)->filename;
179
0
  zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32,
180
0
    '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
181
0
  return zend_new_interned_string(result);
182
0
}
183
/* }}} */
184
185
static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
186
0
{
187
0
  const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
188
0
  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
0
  return 0;
195
0
}
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
0
{
227
0
  const struct reserved_class_name *reserved = reserved_class_names;
228
229
0
  const char *uqname = ZSTR_VAL(name);
230
0
  size_t uqname_len = ZSTR_LEN(name);
231
0
  zend_get_unqualified_name(name, &uqname, &uqname_len);
232
233
0
  for (; reserved->name; ++reserved) {
234
0
    if (uqname_len == reserved->len
235
0
      && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
236
0
    ) {
237
0
      return 1;
238
0
    }
239
0
  }
240
241
0
  return 0;
242
0
}
243
/* }}} */
244
245
void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */
246
0
{
247
0
  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
0
  if (zend_string_equals_literal(name, "_")) {
252
0
    zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type);
253
0
  }
254
0
}
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
0
{
295
0
  const builtin_type_info *info = &builtin_types[0];
296
297
0
  for (; info->name; ++info) {
298
0
    if (ZSTR_LEN(name) == info->name_len
299
0
      && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
300
0
    ) {
301
0
      return info->type;
302
0
    }
303
0
  }
304
305
0
  return 0;
306
0
}
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
0
{
333
0
  *prev_context = CG(context);
334
0
  CG(context).prev = CG(context).op_array ? prev_context : NULL;
335
0
  CG(context).op_array = op_array;
336
0
  CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
337
0
  CG(context).vars_size = 0;
338
0
  CG(context).literals_size = 0;
339
0
  CG(context).fast_call_var = -1;
340
0
  CG(context).try_catch_offset = -1;
341
0
  CG(context).current_brk_cont = -1;
342
0
  CG(context).last_brk_cont = 0;
343
0
  CG(context).has_assigned_to_http_response_header = false;
344
0
  CG(context).brk_cont_array = NULL;
345
0
  CG(context).labels = NULL;
346
0
  CG(context).in_jmp_frameless_branch = false;
347
0
  CG(context).active_property_info_name = NULL;
348
0
  CG(context).active_property_hook_kind = (zend_property_hook_kind)-1;
349
0
}
350
/* }}} */
351
352
void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */
353
0
{
354
0
  if (CG(context).brk_cont_array) {
355
0
    efree(CG(context).brk_cont_array);
356
0
    CG(context).brk_cont_array = NULL;
357
0
  }
358
0
  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
0
  CG(context) = *prev_context;
364
0
}
365
/* }}} */
366
367
static void zend_reset_import_tables(void) /* {{{ */
368
0
{
369
0
  if (FC(imports)) {
370
0
    zend_hash_destroy(FC(imports));
371
0
    efree(FC(imports));
372
0
    FC(imports) = NULL;
373
0
  }
374
375
0
  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
0
  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
0
  zend_hash_clean(&FC(seen_symbols));
388
0
}
389
/* }}} */
390
391
0
static void zend_end_namespace(void) /* {{{ */ {
392
0
  FC(in_namespace) = 0;
393
0
  zend_reset_import_tables();
394
0
  if (FC(current_namespace)) {
395
0
    zend_string_release_ex(FC(current_namespace), 0);
396
0
    FC(current_namespace) = NULL;
397
0
  }
398
0
}
399
/* }}} */
400
401
void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
402
0
{
403
0
  *prev_context = CG(file_context);
404
0
  FC(imports) = NULL;
405
0
  FC(imports_function) = NULL;
406
0
  FC(imports_const) = NULL;
407
0
  FC(current_namespace) = NULL;
408
0
  FC(in_namespace) = 0;
409
0
  FC(has_bracketed_namespaces) = 0;
410
0
  FC(declarables).ticks = 0;
411
0
  zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0);
412
0
}
413
/* }}} */
414
415
void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */
416
0
{
417
0
  zend_end_namespace();
418
0
  zend_hash_destroy(&FC(seen_symbols));
419
0
  CG(file_context) = *prev_context;
420
0
}
421
/* }}} */
422
423
void zend_init_compiler_data_structures(void) /* {{{ */
424
50.4k
{
425
50.4k
  zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
426
50.4k
  zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
427
50.4k
  zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t));
428
50.4k
  CG(active_class_entry) = NULL;
429
50.4k
  CG(in_compilation) = 0;
430
50.4k
  CG(skip_shebang) = 0;
431
432
50.4k
  CG(encoding_declared) = 0;
433
50.4k
  CG(memoized_exprs) = NULL;
434
50.4k
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
435
50.4k
}
436
/* }}} */
437
438
0
static void zend_register_seen_symbol(zend_string *name, uint32_t kind) {
439
0
  zval *zv = zend_hash_find(&FC(seen_symbols), name);
440
0
  if (zv) {
441
0
    Z_LVAL_P(zv) |= kind;
442
0
  } else {
443
0
    zval tmp;
444
0
    ZVAL_LONG(&tmp, kind);
445
0
    zend_hash_add_new(&FC(seen_symbols), name, &tmp);
446
0
  }
447
0
}
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
50.4k
{
456
50.4k
  CG(arena) = zend_arena_create(64 * 1024);
457
50.4k
  CG(active_op_array) = NULL;
458
50.4k
  memset(&CG(context), 0, sizeof(CG(context)));
459
50.4k
  zend_init_compiler_data_structures();
460
50.4k
  zend_init_rsrc_list();
461
50.4k
  zend_stream_init();
462
50.4k
  CG(unclean_shutdown) = 0;
463
464
50.4k
  CG(delayed_variance_obligations) = NULL;
465
50.4k
  CG(delayed_autoloads) = NULL;
466
50.4k
  CG(unlinked_uses) = NULL;
467
50.4k
  CG(current_linking_class) = NULL;
468
50.4k
}
469
/* }}} */
470
471
void shutdown_compiler(void) /* {{{ */
472
50.4k
{
473
  /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */
474
50.4k
  zend_restore_compiled_filename(NULL);
475
476
50.4k
  zend_stack_destroy(&CG(loop_var_stack));
477
50.4k
  zend_stack_destroy(&CG(delayed_oplines_stack));
478
50.4k
  zend_stack_destroy(&CG(short_circuiting_opnums));
479
480
50.4k
  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
50.4k
  if (CG(delayed_autoloads)) {
486
0
    zend_hash_destroy(CG(delayed_autoloads));
487
0
    FREE_HASHTABLE(CG(delayed_autoloads));
488
0
    CG(delayed_autoloads) = NULL;
489
0
  }
490
50.4k
  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
50.4k
  CG(current_linking_class) = NULL;
496
50.4k
}
497
/* }}} */
498
499
ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
500
0
{
501
0
  CG(compiled_filename) = zend_string_copy(new_compiled_filename);
502
0
  return new_compiled_filename;
503
0
}
504
/* }}} */
505
506
ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
507
50.4k
{
508
50.4k
  if (CG(compiled_filename)) {
509
0
    zend_string_release(CG(compiled_filename));
510
0
    CG(compiled_filename) = NULL;
511
0
  }
512
50.4k
  CG(compiled_filename) = original_compiled_filename;
513
50.4k
}
514
/* }}} */
515
516
ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
517
0
{
518
0
  return CG(compiled_filename);
519
0
}
520
/* }}} */
521
522
ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */
523
0
{
524
0
  return CG(zend_lineno);
525
0
}
526
/* }}} */
527
528
ZEND_API bool zend_is_compiling(void) /* {{{ */
529
737k
{
530
737k
  return CG(in_compilation);
531
737k
}
532
/* }}} */
533
534
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
535
0
{
536
0
  return (uint32_t)CG(active_op_array)->T++;
537
0
}
538
/* }}} */
539
540
0
static uint32_t lookup_cv(zend_string *name) /* {{{ */{
541
0
  zend_op_array *op_array = CG(active_op_array);
542
0
  int i = 0;
543
0
  zend_ulong hash_value = zend_string_hash_val(name);
544
545
0
  while (i < op_array->last_var) {
546
0
    if (ZSTR_H(op_array->vars[i]) == hash_value
547
0
     && zend_string_equals(op_array->vars[i], name)) {
548
0
      return EX_NUM_TO_VAR(i);
549
0
    }
550
0
    i++;
551
0
  }
552
0
  i = op_array->last_var;
553
0
  op_array->last_var++;
554
0
  if (op_array->last_var > CG(context).vars_size) {
555
0
    CG(context).vars_size += 16; /* FIXME */
556
0
    op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
557
0
  }
558
559
0
  op_array->vars[i] = zend_string_copy(name);
560
0
  return EX_NUM_TO_VAR(i);
561
0
}
562
/* }}} */
563
564
zend_string *zval_make_interned_string(zval *zv)
565
32
{
566
32
  ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
567
32
  Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
568
32
  if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
569
32
    Z_TYPE_FLAGS_P(zv) = 0;
570
32
  }
571
32
  return Z_STR_P(zv);
572
32
}
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
0
{
577
0
  zval *lit = CT_CONSTANT_EX(op_array, literal_position);
578
0
  if (Z_TYPE_P(zv) == IS_STRING) {
579
0
    zval_make_interned_string(zv);
580
0
  }
581
0
  ZVAL_COPY_VALUE(lit, zv);
582
0
  Z_EXTRA_P(lit) = 0;
583
0
}
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
0
{
591
0
  zend_op_array *op_array = CG(active_op_array);
592
0
  uint32_t i = op_array->last_literal;
593
0
  op_array->last_literal++;
594
0
  if (i >= CG(context).literals_size) {
595
0
    while (i >= CG(context).literals_size) {
596
0
      CG(context).literals_size += 16; /* FIXME */
597
0
    }
598
0
    op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
599
0
  }
600
0
  zend_insert_literal(op_array, zv, i);
601
0
  return i;
602
0
}
603
/* }}} */
604
605
static inline int zend_add_literal_string(zend_string **str) /* {{{ */
606
0
{
607
0
  int ret;
608
0
  zval zv;
609
0
  ZVAL_STR(&zv, *str);
610
0
  ret = zend_add_literal(&zv);
611
0
  *str = Z_STR(zv);
612
0
  return ret;
613
0
}
614
/* }}} */
615
616
static int zend_add_func_name_literal(zend_string *name) /* {{{ */
617
0
{
618
  /* Original name */
619
0
  int ret = zend_add_literal_string(&name);
620
621
  /* Lowercased name */
622
0
  zend_string *lc_name = zend_string_tolower(name);
623
0
  zend_add_literal_string(&lc_name);
624
625
0
  return ret;
626
0
}
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
0
{
654
  /* Original name */
655
0
  int ret = zend_add_literal_string(&name);
656
657
  /* Lowercased name */
658
0
  zend_string *lc_name = zend_string_tolower(name);
659
0
  zend_add_literal_string(&lc_name);
660
661
0
  return ret;
662
0
}
663
/* }}} */
664
665
static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */
666
0
{
667
0
  zend_string *tmp_name;
668
669
0
  int ret = zend_add_literal_string(&name);
670
671
0
  size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
672
0
  const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
673
0
  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
0
  } else {
687
0
    after_ns = ZSTR_VAL(name);
688
0
  }
689
690
  /* original unqualified constant name */
691
0
  tmp_name = zend_string_init(after_ns, after_ns_len, 0);
692
0
  zend_add_literal_string(&tmp_name);
693
694
0
  return ret;
695
0
}
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
0
{
716
0
  zend_brk_cont_element *brk_cont_element;
717
0
  int parent = CG(context).current_brk_cont;
718
0
  zend_loop_var info = {0};
719
720
0
  CG(context).current_brk_cont = CG(context).last_brk_cont;
721
0
  brk_cont_element = get_next_brk_cont_element();
722
0
  brk_cont_element->parent = parent;
723
0
  brk_cont_element->is_switch = is_switch;
724
725
0
  if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
726
0
    uint32_t start = get_next_op_number();
727
728
0
    info.opcode = free_opcode;
729
0
    info.var_type = loop_var->op_type;
730
0
    info.var_num = loop_var->u.op.var;
731
0
    brk_cont_element->start = start;
732
0
  } else {
733
0
    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
0
    brk_cont_element->start = -1;
737
0
  }
738
739
0
  zend_stack_push(&CG(loop_var_stack), &info);
740
0
}
741
/* }}} */
742
743
static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
744
0
{
745
0
  uint32_t end = get_next_op_number();
746
0
  zend_brk_cont_element *brk_cont_element
747
0
    = &CG(context).brk_cont_array[CG(context).current_brk_cont];
748
0
  brk_cont_element->cont = cont_addr;
749
0
  brk_cont_element->brk = end;
750
0
  CG(context).current_brk_cont = brk_cont_element->parent;
751
752
0
  zend_stack_del_top(&CG(loop_var_stack));
753
0
}
754
/* }}} */
755
756
static void zend_do_free(znode *op1) /* {{{ */
757
0
{
758
0
  if (op1->op_type == IS_TMP_VAR) {
759
0
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
760
761
0
    while (opline->opcode == ZEND_END_SILENCE ||
762
0
           opline->opcode == ZEND_OP_DATA) {
763
0
      opline--;
764
0
    }
765
766
0
    if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
767
0
      switch (opline->opcode) {
768
0
        case ZEND_BOOL:
769
0
        case ZEND_BOOL_NOT:
770
          /* boolean results don't have to be freed */
771
0
          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
0
        case ZEND_POST_INC:
777
0
        case ZEND_POST_DEC:
778
          /* convert $i++ to ++$i */
779
0
          opline->opcode -= 2;
780
0
          SET_UNUSED(opline->result);
781
0
          return;
782
0
        case ZEND_ASSIGN:
783
0
        case ZEND_ASSIGN_DIM:
784
0
        case ZEND_ASSIGN_OBJ:
785
0
        case ZEND_ASSIGN_STATIC_PROP:
786
0
        case ZEND_ASSIGN_OP:
787
0
        case ZEND_ASSIGN_DIM_OP:
788
0
        case ZEND_ASSIGN_OBJ_OP:
789
0
        case ZEND_ASSIGN_STATIC_PROP_OP:
790
0
        case ZEND_PRE_INC_STATIC_PROP:
791
0
        case ZEND_PRE_DEC_STATIC_PROP:
792
0
        case ZEND_PRE_INC_OBJ:
793
0
        case ZEND_PRE_DEC_OBJ:
794
0
        case ZEND_PRE_INC:
795
0
        case ZEND_PRE_DEC:
796
0
          SET_UNUSED(opline->result);
797
0
          return;
798
0
      }
799
0
    }
800
801
0
    zend_emit_op(NULL, ZEND_FREE, op1, NULL);
802
0
  } else if (op1->op_type == IS_VAR) {
803
0
    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
804
0
    while (opline->opcode == ZEND_END_SILENCE ||
805
0
        opline->opcode == ZEND_EXT_FCALL_END ||
806
0
        opline->opcode == ZEND_OP_DATA) {
807
0
      opline--;
808
0
    }
809
0
    if (opline->result_type == IS_VAR
810
0
      && opline->result.var == op1->u.op.var) {
811
0
      if (opline->opcode == ZEND_FETCH_THIS) {
812
0
        opline->opcode = ZEND_NOP;
813
0
      }
814
0
      if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) {
815
0
        SET_UNUSED(opline->result);
816
0
      } else {
817
        /* Frameless calls usually use the return value, so always emit a free. This should be
818
         * faster than checking RETURN_VALUE_USED inside the handler. */
819
        // FIXME: We may actually look at the function signature to determine whether a free
820
        // is necessary.
821
0
        zend_emit_op(NULL, ZEND_FREE, op1, NULL);
822
0
      }
823
0
    } else {
824
0
      while (opline >= CG(active_op_array)->opcodes) {
825
0
        if ((opline->opcode == ZEND_FETCH_LIST_R ||
826
0
             opline->opcode == ZEND_FETCH_LIST_W ||
827
0
             opline->opcode == ZEND_EXT_STMT) &&
828
0
            opline->op1_type == IS_VAR &&
829
0
            opline->op1.var == op1->u.op.var) {
830
0
          zend_emit_op(NULL, ZEND_FREE, op1, NULL);
831
0
          return;
832
0
        }
833
0
        if (opline->result_type == IS_VAR
834
0
          && opline->result.var == op1->u.op.var) {
835
0
          if (opline->opcode == ZEND_NEW) {
836
0
            zend_emit_op(NULL, ZEND_FREE, op1, NULL);
837
0
          }
838
0
          break;
839
0
        }
840
0
        opline--;
841
0
      }
842
0
    }
843
0
  } else if (op1->op_type == IS_CONST) {
844
    /* Destroy value without using GC: When opcache moves arrays into SHM it will
845
     * free the zend_array structure, so references to it from outside the op array
846
     * become invalid. GC would cause such a reference in the root buffer. */
847
0
    zval_ptr_dtor_nogc(&op1->u.constant);
848
0
  }
849
0
}
850
/* }}} */
851
852
853
static const char *zend_modifier_token_to_string(uint32_t token)
854
0
{
855
0
  switch (token) {
856
0
    case T_PUBLIC:
857
0
      return "public";
858
0
    case T_PROTECTED:
859
0
      return "protected";
860
0
    case T_PRIVATE:
861
0
      return "private";
862
0
    case T_STATIC:
863
0
      return "static";
864
0
    case T_FINAL:
865
0
      return "final";
866
0
    case T_READONLY:
867
0
      return "readonly";
868
0
    case T_ABSTRACT:
869
0
      return "abstract";
870
0
    case T_PUBLIC_SET:
871
0
      return "public(set)";
872
0
    case T_PROTECTED_SET:
873
0
      return "protected(set)";
874
0
    case T_PRIVATE_SET:
875
0
      return "private(set)";
876
0
    EMPTY_SWITCH_DEFAULT_CASE()
877
0
  }
878
0
}
879
880
uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token)
881
0
{
882
0
  switch (token) {
883
0
    case T_PUBLIC:
884
0
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
885
0
        return ZEND_ACC_PUBLIC;
886
0
      }
887
0
      break;
888
0
    case T_PROTECTED:
889
0
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
890
0
        return ZEND_ACC_PROTECTED;
891
0
      }
892
0
      break;
893
0
    case T_PRIVATE:
894
0
      if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
895
0
        return ZEND_ACC_PRIVATE;
896
0
      }
897
0
      break;
898
0
    case T_READONLY:
899
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
900
0
        return ZEND_ACC_READONLY;
901
0
      }
902
0
      break;
903
0
    case T_ABSTRACT:
904
0
      if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) {
905
0
        return ZEND_ACC_ABSTRACT;
906
0
      }
907
0
      break;
908
0
    case T_FINAL:
909
0
      return ZEND_ACC_FINAL;
910
0
    case T_STATIC:
911
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) {
912
0
        return ZEND_ACC_STATIC;
913
0
      }
914
0
      break;
915
0
    case T_PUBLIC_SET:
916
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
917
0
        return ZEND_ACC_PUBLIC_SET;
918
0
      }
919
0
      break;
920
0
    case T_PROTECTED_SET:
921
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
922
0
        return ZEND_ACC_PROTECTED_SET;
923
0
      }
924
0
      break;
925
0
    case T_PRIVATE_SET:
926
0
      if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
927
0
        return ZEND_ACC_PRIVATE_SET;
928
0
      }
929
0
      break;
930
0
  }
931
932
0
  char *member;
933
0
  if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
934
0
    member = "property";
935
0
  } else if (target == ZEND_MODIFIER_TARGET_METHOD) {
936
0
    member = "method";
937
0
  } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) {
938
0
    member = "class constant";
939
0
  } else if (target == ZEND_MODIFIER_TARGET_CPP) {
940
0
    member = "parameter";
941
0
  } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) {
942
0
    member = "property hook";
943
0
  } else {
944
0
    ZEND_UNREACHABLE();
945
0
  }
946
947
0
  zend_throw_exception_ex(zend_ce_compile_error, 0,
948
0
    "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member);
949
0
  return 0;
950
0
}
951
952
uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers)
953
0
{
954
0
  uint32_t flags = 0;
955
0
  const zend_ast_list *modifier_list = zend_ast_get_list(modifiers);
956
957
0
  for (uint32_t i = 0; i < modifier_list->children; i++) {
958
0
    uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i]));
959
0
    uint32_t new_flag = zend_modifier_token_to_flag(target, token);
960
0
    if (!new_flag) {
961
0
      return 0;
962
0
    }
963
    /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */
964
0
    bool duplicate_flag = (flags & new_flag);
965
0
    flags = zend_add_member_modifier(flags, new_flag, target);
966
0
    if (!flags) {
967
0
      return 0;
968
0
    }
969
0
    if (duplicate_flag) {
970
0
      zend_throw_exception_ex(zend_ce_compile_error, 0,
971
0
        "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token));
972
0
      return 0;
973
0
    }
974
0
  }
975
976
0
  return flags;
977
0
}
978
979
uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
980
0
{
981
0
  uint32_t new_flags = flags | new_flag;
982
0
  if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
983
0
    zend_throw_exception(zend_ce_compile_error,
984
0
      "Multiple abstract modifiers are not allowed", 0);
985
0
    return 0;
986
0
  }
987
0
  if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
988
0
    zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0);
989
0
    return 0;
990
0
  }
991
0
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
992
0
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
993
0
    return 0;
994
0
  }
995
0
  if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
996
0
    zend_throw_exception(zend_ce_compile_error,
997
0
      "Cannot use the final modifier on an abstract class", 0);
998
0
    return 0;
999
0
  }
1000
0
  return new_flags;
1001
0
}
1002
/* }}} */
1003
1004
uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
1005
0
{
1006
0
  uint32_t new_flags = flags | new_flag;
1007
0
  if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
1008
0
    zend_throw_exception(zend_ce_compile_error,
1009
0
      "Cannot use the abstract modifier on an anonymous class", 0);
1010
0
    return 0;
1011
0
  }
1012
0
  if (new_flag & ZEND_ACC_FINAL) {
1013
0
    zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0);
1014
0
    return 0;
1015
0
  }
1016
0
  if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) {
1017
0
    zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
1018
0
    return 0;
1019
0
  }
1020
0
  return new_flags;
1021
0
}
1022
1023
uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */
1024
0
{
1025
0
  uint32_t new_flags = flags | new_flag;
1026
0
  if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
1027
0
    zend_throw_exception(zend_ce_compile_error,
1028
0
      "Multiple access type modifiers are not allowed", 0);
1029
0
    return 0;
1030
0
  }
1031
0
  if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
1032
0
    if (target == ZEND_MODIFIER_TARGET_METHOD) {
1033
0
      zend_throw_exception(zend_ce_compile_error,
1034
0
        "Cannot use the final modifier on an abstract method", 0);
1035
0
      return 0;
1036
0
    }
1037
0
    if (target == ZEND_MODIFIER_TARGET_PROPERTY) {
1038
0
      zend_throw_exception(zend_ce_compile_error,
1039
0
        "Cannot use the final modifier on an abstract property", 0);
1040
0
      return 0;
1041
0
    }
1042
0
  }
1043
0
  if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) {
1044
0
    if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) {
1045
0
      zend_throw_exception(zend_ce_compile_error,
1046
0
        "Multiple access type modifiers are not allowed", 0);
1047
0
      return 0;
1048
0
    }
1049
0
  }
1050
0
  return new_flags;
1051
0
}
1052
/* }}} */
1053
1054
0
ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) {
1055
0
  return zend_string_concat3(
1056
0
    ZSTR_VAL(class_name), ZSTR_LEN(class_name),
1057
0
    "::", sizeof("::") - 1,
1058
0
    ZSTR_VAL(member_name), ZSTR_LEN(member_name));
1059
0
}
1060
1061
0
static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) {
1062
0
  return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len);
1063
0
}
1064
1065
0
static zend_string *zend_prefix_with_ns(zend_string *name) {
1066
0
  if (FC(current_namespace)) {
1067
0
    const zend_string *ns = FC(current_namespace);
1068
0
    return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
1069
0
  } else {
1070
0
    return zend_string_copy(name);
1071
0
  }
1072
0
}
1073
1074
static zend_string *zend_resolve_non_class_name(
1075
  zend_string *name, uint32_t type, bool *is_fully_qualified,
1076
  bool case_sensitive, const HashTable *current_import_sub
1077
0
) {
1078
0
  const char *compound;
1079
0
  *is_fully_qualified = false;
1080
1081
0
  if (ZSTR_VAL(name)[0] == '\\') {
1082
    /* Remove \ prefix (only relevant if this is a string rather than a label) */
1083
0
    *is_fully_qualified = true;
1084
0
    return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1085
0
  }
1086
1087
0
  if (type == ZEND_NAME_FQ) {
1088
0
    *is_fully_qualified = true;
1089
0
    return zend_string_copy(name);
1090
0
  }
1091
1092
0
  if (type == ZEND_NAME_RELATIVE) {
1093
0
    *is_fully_qualified = true;
1094
0
    return zend_prefix_with_ns(name);
1095
0
  }
1096
1097
0
  if (current_import_sub) {
1098
    /* If an unqualified name is a function/const alias, replace it. */
1099
0
    zend_string *import_name;
1100
0
    if (case_sensitive) {
1101
0
      import_name = zend_hash_find_ptr(current_import_sub, name);
1102
0
    } else {
1103
0
      import_name = zend_hash_find_ptr_lc(current_import_sub, name);
1104
0
    }
1105
1106
0
    if (import_name) {
1107
0
      *is_fully_qualified = true;
1108
0
      return zend_string_copy(import_name);
1109
0
    }
1110
0
  }
1111
1112
0
  compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1113
0
  if (compound) {
1114
0
    *is_fully_qualified = true;
1115
0
  }
1116
1117
0
  if (compound && FC(imports)) {
1118
    /* If the first part of a qualified name is an alias, substitute it. */
1119
0
    size_t len = compound - ZSTR_VAL(name);
1120
0
    const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1121
1122
0
    if (import_name) {
1123
0
      return zend_concat_names(
1124
0
        ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1125
0
    }
1126
0
  }
1127
1128
0
  return zend_prefix_with_ns(name);
1129
0
}
1130
/* }}} */
1131
1132
static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1133
0
{
1134
0
  return zend_resolve_non_class_name(
1135
0
    name, type, is_fully_qualified, false, FC(imports_function));
1136
0
}
1137
1138
static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified)
1139
0
{
1140
0
  return zend_resolve_non_class_name(
1141
0
    name, type, is_fully_qualified, true, FC(imports_const));
1142
0
}
1143
1144
static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
1145
0
{
1146
0
  const char *compound;
1147
1148
0
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1149
0
    if (type == ZEND_NAME_FQ) {
1150
0
      zend_error_noreturn(E_COMPILE_ERROR,
1151
0
        "'\\%s' is an invalid class name", ZSTR_VAL(name));
1152
0
    }
1153
0
    if (type == ZEND_NAME_RELATIVE) {
1154
0
      zend_error_noreturn(E_COMPILE_ERROR,
1155
0
        "'namespace\\%s' is an invalid class name", ZSTR_VAL(name));
1156
0
    }
1157
0
    ZEND_ASSERT(type == ZEND_NAME_NOT_FQ);
1158
0
    return zend_string_copy(name);
1159
0
  }
1160
1161
0
  if (type == ZEND_NAME_RELATIVE) {
1162
0
    return zend_prefix_with_ns(name);
1163
0
  }
1164
1165
0
  if (type == ZEND_NAME_FQ) {
1166
0
    if (ZSTR_VAL(name)[0] == '\\') {
1167
      /* Remove \ prefix (only relevant if this is a string rather than a label) */
1168
0
      name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
1169
0
      if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
1170
0
        zend_error_noreturn(E_COMPILE_ERROR,
1171
0
          "'\\%s' is an invalid class name", ZSTR_VAL(name));
1172
0
      }
1173
0
      return name;
1174
0
    }
1175
1176
0
    return zend_string_copy(name);
1177
0
  }
1178
1179
0
  if (FC(imports)) {
1180
0
    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
1181
0
    if (compound) {
1182
      /* If the first part of a qualified name is an alias, substitute it. */
1183
0
      size_t len = compound - ZSTR_VAL(name);
1184
0
      const zend_string *import_name =
1185
0
        zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
1186
1187
0
      if (import_name) {
1188
0
        return zend_concat_names(
1189
0
          ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
1190
0
      }
1191
0
    } else {
1192
      /* If an unqualified name is an alias, replace it. */
1193
0
      zend_string *import_name
1194
0
        = zend_hash_find_ptr_lc(FC(imports), name);
1195
1196
0
      if (import_name) {
1197
0
        return zend_string_copy(import_name);
1198
0
      }
1199
0
    }
1200
0
  }
1201
1202
  /* If not fully qualified and not an alias, prepend the current namespace */
1203
0
  return zend_prefix_with_ns(name);
1204
0
}
1205
/* }}} */
1206
1207
static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
1208
0
{
1209
0
  const zval *class_name = zend_ast_get_zval(ast);
1210
0
  if (Z_TYPE_P(class_name) != IS_STRING) {
1211
0
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1212
0
  }
1213
0
  return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1214
0
}
1215
/* }}} */
1216
1217
static void label_ptr_dtor(zval *zv) /* {{{ */
1218
0
{
1219
0
  efree_size(Z_PTR_P(zv), sizeof(zend_label));
1220
0
}
1221
/* }}} */
1222
1223
0
static void str_dtor(zval *zv)  /* {{{ */ {
1224
0
  zend_string_release_ex(Z_STR_P(zv), 0);
1225
0
}
1226
/* }}} */
1227
1228
static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1229
0
{
1230
0
  zend_op_array *op_array = CG(active_op_array);
1231
0
  uint32_t try_catch_offset = op_array->last_try_catch++;
1232
0
  zend_try_catch_element *elem;
1233
1234
0
  op_array->try_catch_array = safe_erealloc(
1235
0
    op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1236
1237
0
  elem = &op_array->try_catch_array[try_catch_offset];
1238
0
  elem->try_op = try_op;
1239
0
  elem->catch_op = 0;
1240
0
  elem->finally_op = 0;
1241
0
  elem->finally_end = 0;
1242
1243
0
  return try_catch_offset;
1244
0
}
1245
/* }}} */
1246
1247
ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1248
0
{
1249
0
  if (function->type == ZEND_USER_FUNCTION) {
1250
0
    zend_op_array *op_array = &function->op_array;
1251
0
    if (op_array->refcount) {
1252
0
      (*op_array->refcount)++;
1253
0
    }
1254
1255
0
    ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL);
1256
0
    ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL);
1257
0
  }
1258
1259
0
  if (function->common.function_name) {
1260
0
    zend_string_addref(function->common.function_name);
1261
0
  }
1262
0
}
1263
/* }}} */
1264
1265
static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */
1266
0
{
1267
0
  const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname);
1268
0
  int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1269
0
  const zend_function *old_function;
1270
1271
0
  ZEND_ASSERT(zv != NULL);
1272
0
  old_function = Z_PTR_P(zv);
1273
0
  if (old_function->type == ZEND_USER_FUNCTION
1274
0
    && old_function->op_array.last > 0) {
1275
0
    zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)",
1276
0
          op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name),
1277
0
          ZSTR_VAL(old_function->op_array.filename),
1278
0
          old_function->op_array.line_start);
1279
0
  } else {
1280
0
    zend_error_noreturn(error_level, "Cannot redeclare function %s()",
1281
0
      op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name));
1282
0
  }
1283
0
}
1284
1285
ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */
1286
0
{
1287
0
  zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func);
1288
0
  if (UNEXPECTED(!added_func)) {
1289
0
    do_bind_function_error(Z_STR_P(lcname), &func->op_array, false);
1290
0
    return FAILURE;
1291
0
  }
1292
1293
0
  if (func->op_array.refcount) {
1294
0
    ++*func->op_array.refcount;
1295
0
  }
1296
0
  if (func->common.function_name) {
1297
0
    zend_string_addref(func->common.function_name);
1298
0
  }
1299
0
  zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname));
1300
0
  return SUCCESS;
1301
0
}
1302
/* }}} */
1303
1304
ZEND_API zend_class_entry *zend_bind_class_in_slot(
1305
    zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name)
1306
0
{
1307
0
  zend_class_entry *ce = Z_PTR_P(class_table_slot);
1308
0
  bool is_preloaded =
1309
0
    (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD);
1310
0
  bool success;
1311
0
  if (EXPECTED(!is_preloaded)) {
1312
0
    success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL;
1313
0
  } else {
1314
    /* If preloading is used, don't replace the existing bucket, add a new one. */
1315
0
    success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL;
1316
0
  }
1317
0
  if (UNEXPECTED(!success)) {
1318
0
    zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1319
0
    ZEND_ASSERT(old_class);
1320
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, old_class);
1321
0
    return NULL;
1322
0
  }
1323
1324
0
  if (ce->ce_flags & ZEND_ACC_LINKED) {
1325
0
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1326
0
    return ce;
1327
0
  }
1328
1329
0
  ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname));
1330
0
  if (ce) {
1331
0
    zend_observer_class_linked_notify(ce, Z_STR_P(lcname));
1332
0
    return ce;
1333
0
  }
1334
1335
0
  if (!is_preloaded) {
1336
    /* Reload bucket pointer, the hash table may have been reallocated */
1337
0
    zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname));
1338
0
    zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1));
1339
0
  } else {
1340
0
    zend_hash_del(EG(class_table), Z_STR_P(lcname));
1341
0
  }
1342
0
  return NULL;
1343
0
}
1344
1345
ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */
1346
0
{
1347
0
  zval *rtd_key, *zv;
1348
1349
0
  rtd_key = lcname + 1;
1350
1351
0
  zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key));
1352
1353
0
  if (UNEXPECTED(!zv)) {
1354
0
    const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname));
1355
0
    ZEND_ASSERT(ce);
1356
0
    zend_class_redeclaration_error(E_COMPILE_ERROR, ce);
1357
0
    return FAILURE;
1358
0
  }
1359
1360
  /* Register the derived class */
1361
0
  return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE;
1362
0
}
1363
/* }}} */
1364
1365
4
static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) {
1366
4
  zend_string *result;
1367
4
  if (type == NULL) {
1368
4
    return zend_string_copy(new_type);
1369
4
  }
1370
1371
0
  if (is_intersection) {
1372
0
    result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type),
1373
0
      "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1374
0
    zend_string_release(type);
1375
0
  } else {
1376
0
    result = zend_string_concat3(
1377
0
      ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type));
1378
0
    zend_string_release(type);
1379
0
  }
1380
0
  return result;
1381
4
}
1382
1383
13
static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) {
1384
13
  if (scope) {
1385
0
    if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1386
0
      name = scope->name;
1387
0
    } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) {
1388
0
      name = scope->parent->name;
1389
0
    }
1390
0
  }
1391
1392
  /* The resolved name for anonymous classes contains null bytes. Cut off everything after the
1393
   * null byte here, to avoid larger parts of the type being omitted by printing code later. */
1394
13
  size_t len = strlen(ZSTR_VAL(name));
1395
13
  if (len != ZSTR_LEN(name)) {
1396
0
    return zend_string_init(ZSTR_VAL(name), len, 0);
1397
0
  }
1398
13
  return zend_string_copy(name);
1399
13
}
1400
1401
static zend_string *add_intersection_type(zend_string *str,
1402
  const zend_type_list *intersection_type_list, zend_class_entry *scope,
1403
  bool is_bracketed)
1404
0
{
1405
0
  const zend_type *single_type;
1406
0
  zend_string *intersection_str = NULL;
1407
1408
0
  ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
1409
0
    ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type));
1410
0
    ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type));
1411
1412
0
    intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true);
1413
0
  } ZEND_TYPE_LIST_FOREACH_END();
1414
1415
0
  ZEND_ASSERT(intersection_str);
1416
1417
0
  if (is_bracketed) {
1418
0
    zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1);
1419
0
    zend_string_release(intersection_str);
1420
0
    intersection_str = result;
1421
0
  }
1422
0
  str = add_type_string(str, intersection_str, /* is_intersection */ false);
1423
0
  zend_string_release(intersection_str);
1424
0
  return str;
1425
0
}
1426
1427
17
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
1428
17
  zend_string *str = NULL;
1429
1430
  /* Pure intersection type */
1431
17
  if (ZEND_TYPE_IS_INTERSECTION(type)) {
1432
0
    ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type));
1433
0
    str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
1434
17
  } else if (ZEND_TYPE_HAS_LIST(type)) {
1435
    /* A union type might not be a list */
1436
0
    const zend_type *list_type;
1437
0
    ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
1438
0
      if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
1439
0
        str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
1440
0
        continue;
1441
0
      }
1442
0
      ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type));
1443
0
      ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type));
1444
1445
0
      zend_string *name = ZEND_TYPE_NAME(*list_type);
1446
0
      zend_string *resolved = resolve_class_name(name, scope);
1447
0
      str = add_type_string(str, resolved, /* is_intersection */ false);
1448
0
      zend_string_release(resolved);
1449
0
    } ZEND_TYPE_LIST_FOREACH_END();
1450
17
  } else if (ZEND_TYPE_HAS_NAME(type)) {
1451
13
    str = resolve_class_name(ZEND_TYPE_NAME(type), scope);
1452
13
  }
1453
1454
17
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
1455
1456
17
  if (type_mask == MAY_BE_ANY) {
1457
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false);
1458
1459
0
    return str;
1460
0
  }
1461
17
  if (type_mask & MAY_BE_STATIC) {
1462
0
    zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC);
1463
    // During compilation of eval'd code the called scope refers to the scope calling the eval
1464
0
    if (scope && !zend_is_compiling()) {
1465
0
      const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
1466
0
      if (called_scope) {
1467
0
        name = called_scope->name;
1468
0
      }
1469
0
    }
1470
0
    str = add_type_string(str, name, /* is_intersection */ false);
1471
0
  }
1472
17
  if (type_mask & MAY_BE_CALLABLE) {
1473
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false);
1474
0
  }
1475
17
  if (type_mask & MAY_BE_OBJECT) {
1476
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false);
1477
0
  }
1478
17
  if (type_mask & MAY_BE_ARRAY) {
1479
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false);
1480
0
  }
1481
17
  if (type_mask & MAY_BE_STRING) {
1482
4
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false);
1483
4
  }
1484
17
  if (type_mask & MAY_BE_LONG) {
1485
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false);
1486
0
  }
1487
17
  if (type_mask & MAY_BE_DOUBLE) {
1488
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false);
1489
0
  }
1490
17
  if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) {
1491
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false);
1492
17
  } else if (type_mask & MAY_BE_FALSE) {
1493
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false);
1494
17
  } else if (type_mask & MAY_BE_TRUE) {
1495
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false);
1496
0
  }
1497
17
  if (type_mask & MAY_BE_VOID) {
1498
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false);
1499
0
  }
1500
17
  if (type_mask & MAY_BE_NEVER) {
1501
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false);
1502
0
  }
1503
1504
17
  if (type_mask & MAY_BE_NULL) {
1505
13
    bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL;
1506
13
    bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL;
1507
13
    if (!is_union && !has_intersection) {
1508
13
      zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str));
1509
13
      zend_string_release(str);
1510
13
      return nullable_str;
1511
13
    }
1512
1513
0
    str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false);
1514
0
  }
1515
4
  return str;
1516
17
}
1517
1518
17
ZEND_API zend_string *zend_type_to_string(zend_type type) {
1519
17
  return zend_type_to_string_resolved(type, NULL);
1520
17
}
1521
1522
0
static bool is_generator_compatible_class_type(const zend_string *name) {
1523
0
  return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
1524
0
    || zend_string_equals_literal_ci(name, "Iterator")
1525
0
    || zend_string_equals_literal_ci(name, "Generator");
1526
0
}
1527
1528
static void zend_mark_function_as_generator(void) /* {{{ */
1529
0
{
1530
0
  if (!CG(active_op_array)->function_name) {
1531
0
    zend_error_noreturn(E_COMPILE_ERROR,
1532
0
      "The \"yield\" expression can only be used inside a function");
1533
0
  }
1534
1535
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1536
0
    const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
1537
0
    bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
1538
0
    if (!valid_type) {
1539
0
      const zend_type *single_type;
1540
0
      ZEND_TYPE_FOREACH(return_type, single_type) {
1541
0
        if (ZEND_TYPE_HAS_NAME(*single_type)
1542
0
            && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
1543
0
          valid_type = true;
1544
0
          break;
1545
0
        }
1546
0
      } ZEND_TYPE_FOREACH_END();
1547
0
    }
1548
1549
0
    if (!valid_type) {
1550
0
      zend_string *str = zend_type_to_string(return_type);
1551
0
      zend_error_noreturn(E_COMPILE_ERROR,
1552
0
        "Generator return type must be a supertype of Generator, %s given",
1553
0
        ZSTR_VAL(str));
1554
0
    }
1555
0
  }
1556
1557
0
  CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1558
0
}
1559
/* }}} */
1560
1561
ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */
1562
32
{
1563
32
  size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1564
32
  zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1565
1566
32
  ZSTR_VAL(prop_name)[0] = '\0';
1567
32
  memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1568
32
  memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1569
32
  return prop_name;
1570
32
}
1571
/* }}} */
1572
1573
ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1574
772k
{
1575
772k
  size_t class_name_len;
1576
772k
  size_t anonclass_src_len;
1577
1578
772k
  *class_name = NULL;
1579
1580
772k
  if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1581
674k
    *prop_name = ZSTR_VAL(name);
1582
674k
    if (prop_len) {
1583
465k
      *prop_len = ZSTR_LEN(name);
1584
465k
    }
1585
674k
    return SUCCESS;
1586
674k
  }
1587
98.2k
  if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1588
1.48k
    zend_error(E_NOTICE, "Illegal member variable name");
1589
1.48k
    *prop_name = ZSTR_VAL(name);
1590
1.48k
    if (prop_len) {
1591
1.48k
      *prop_len = ZSTR_LEN(name);
1592
1.48k
    }
1593
1.48k
    return FAILURE;
1594
1.48k
  }
1595
1596
96.7k
  class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1597
96.7k
  if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1598
134
    zend_error(E_NOTICE, "Corrupt member variable name");
1599
134
    *prop_name = ZSTR_VAL(name);
1600
134
    if (prop_len) {
1601
134
      *prop_len = ZSTR_LEN(name);
1602
134
    }
1603
134
    return FAILURE;
1604
134
  }
1605
1606
96.6k
  *class_name = ZSTR_VAL(name) + 1;
1607
96.6k
  anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1608
96.6k
  if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1609
24.5k
    class_name_len += anonclass_src_len + 1;
1610
24.5k
  }
1611
96.6k
  *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1612
96.6k
  if (prop_len) {
1613
57.7k
    *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1614
57.7k
  }
1615
96.6k
  return SUCCESS;
1616
96.7k
}
1617
/* }}} */
1618
1619
static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks)
1620
0
{
1621
0
  if (zend_hash_num_elements(array) > *max_checks) {
1622
0
    return false;
1623
0
  }
1624
0
  *max_checks -= zend_hash_num_elements(array);
1625
1626
0
  zval *element;
1627
0
  ZEND_HASH_FOREACH_VAL(array, element) {
1628
0
    if (Z_TYPE_P(element) < IS_ARRAY) {
1629
0
      continue;
1630
0
    } else if (Z_TYPE_P(element) == IS_ARRAY) {
1631
0
      if (!array_is_const_ex(array, max_checks)) {
1632
0
        return false;
1633
0
      }
1634
0
    } else {
1635
0
      return false;
1636
0
    }
1637
0
  } ZEND_HASH_FOREACH_END();
1638
1639
0
  return true;
1640
0
}
1641
1642
static bool array_is_const(const zend_array *array)
1643
0
{
1644
0
  uint32_t max_checks = 50;
1645
0
  return array_is_const_ex(array, &max_checks);
1646
0
}
1647
1648
0
static bool can_ct_eval_const(const zend_constant *c) {
1649
0
  if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
1650
0
    return 0;
1651
0
  }
1652
0
  if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)
1653
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION)
1654
0
      && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE)
1655
0
        && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) {
1656
0
    return 1;
1657
0
  }
1658
0
  if (Z_TYPE(c->value) < IS_ARRAY
1659
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1660
0
    return 1;
1661
0
  } else if (Z_TYPE(c->value) == IS_ARRAY
1662
0
      && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)
1663
0
      && array_is_const(Z_ARR(c->value))) {
1664
0
    return 1;
1665
0
  }
1666
0
  return 0;
1667
0
}
1668
1669
static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */
1670
0
{
1671
  /* Substitute true, false and null (including unqualified usage in namespaces)
1672
   * before looking up the possibly namespaced name. */
1673
0
  const char *lookup_name = ZSTR_VAL(name);
1674
0
  size_t lookup_len = ZSTR_LEN(name);
1675
1676
0
  if (!is_fully_qualified) {
1677
0
    zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1678
0
  }
1679
1680
0
  zend_constant *c;
1681
0
  if ((c = zend_get_special_const(lookup_name, lookup_len))) {
1682
0
    ZVAL_COPY_VALUE(zv, &c->value);
1683
0
    return 1;
1684
0
  }
1685
0
  c = zend_hash_find_ptr(EG(zend_constants), name);
1686
0
  if (c && can_ct_eval_const(c)) {
1687
0
    ZVAL_COPY_OR_DUP(zv, &c->value);
1688
0
    return 1;
1689
0
  }
1690
0
  return 0;
1691
0
}
1692
/* }}} */
1693
1694
static inline bool zend_is_scope_known(void) /* {{{ */
1695
0
{
1696
0
  if (!CG(active_op_array)) {
1697
    /* This can only happen when evaluating a default value string. */
1698
0
    return 0;
1699
0
  }
1700
1701
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1702
    /* Closures can be rebound to a different scope */
1703
0
    return 0;
1704
0
  }
1705
1706
0
  if (!CG(active_class_entry)) {
1707
    /* The scope is known if we're in a free function (no scope), but not if we're in
1708
     * a file/eval (which inherits including/eval'ing scope). */
1709
0
    return CG(active_op_array)->function_name != NULL;
1710
0
  }
1711
1712
  /* For traits self etc refers to the using class, not the trait itself */
1713
0
  return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1714
0
}
1715
/* }}} */
1716
1717
static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */
1718
0
{
1719
0
  if (!CG(active_class_entry)) {
1720
0
    return 0;
1721
0
  }
1722
0
  if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1723
0
    return 1;
1724
0
  }
1725
0
  return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1726
0
    && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1727
0
}
1728
/* }}} */
1729
1730
uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */
1731
0
{
1732
0
  if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) {
1733
0
    return ZEND_FETCH_CLASS_SELF;
1734
0
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) {
1735
0
    return ZEND_FETCH_CLASS_PARENT;
1736
0
  } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) {
1737
0
    return ZEND_FETCH_CLASS_STATIC;
1738
0
  } else {
1739
0
    return ZEND_FETCH_CLASS_DEFAULT;
1740
0
  }
1741
0
}
1742
/* }}} */
1743
1744
static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1745
0
{
1746
  /* Fully qualified names are always default refs */
1747
0
  if (name_ast->attr == ZEND_NAME_FQ) {
1748
0
    return ZEND_FETCH_CLASS_DEFAULT;
1749
0
  }
1750
1751
0
  return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1752
0
}
1753
/* }}} */
1754
1755
static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type)
1756
0
{
1757
0
  zend_string *class_name = zend_ast_get_str(ast);
1758
0
  if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) {
1759
0
    zend_error_noreturn(E_COMPILE_ERROR,
1760
0
      "Cannot use \"%s\" as %s, as it is reserved",
1761
0
      ZSTR_VAL(class_name), type);
1762
0
  }
1763
0
  return zend_resolve_class_name(class_name, ast->attr);
1764
0
}
1765
1766
static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1767
0
{
1768
0
  if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) {
1769
0
    zend_class_entry *ce = CG(active_class_entry);
1770
0
    if (!ce) {
1771
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1772
0
        fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1773
0
        fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1774
0
    } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) {
1775
0
      zend_error_noreturn(E_COMPILE_ERROR,
1776
0
        "Cannot use \"parent\" when current class scope has no parent");
1777
0
    }
1778
0
  }
1779
0
}
1780
/* }}} */
1781
1782
static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */
1783
0
{
1784
0
  uint32_t fetch_type;
1785
0
  const zval *class_name;
1786
1787
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
1788
0
    return 0;
1789
0
  }
1790
1791
0
  class_name = zend_ast_get_zval(class_ast);
1792
1793
0
  if (Z_TYPE_P(class_name) != IS_STRING) {
1794
0
    zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
1795
0
  }
1796
1797
0
  fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name));
1798
0
  zend_ensure_valid_class_fetch_type(fetch_type);
1799
1800
0
  switch (fetch_type) {
1801
0
    case ZEND_FETCH_CLASS_SELF:
1802
0
      if (CG(active_class_entry) && zend_is_scope_known()) {
1803
0
        ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1804
0
        return 1;
1805
0
      }
1806
0
      return 0;
1807
0
    case ZEND_FETCH_CLASS_PARENT:
1808
0
      if (CG(active_class_entry) && CG(active_class_entry)->parent_name
1809
0
          && zend_is_scope_known()) {
1810
0
        ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name);
1811
0
        return 1;
1812
0
      }
1813
0
      return 0;
1814
0
    case ZEND_FETCH_CLASS_STATIC:
1815
0
      return 0;
1816
0
    case ZEND_FETCH_CLASS_DEFAULT:
1817
0
      ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1818
0
      return 1;
1819
0
    EMPTY_SWITCH_DEFAULT_CASE()
1820
0
  }
1821
0
}
1822
/* }}} */
1823
1824
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
1825
static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope)
1826
0
{
1827
0
  if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1828
0
    return 0;
1829
0
  } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1830
    /* This condition is only met on directly accessing trait constants,
1831
     * because the ce is replaced to the class entry of the composing class
1832
     * on binding. */
1833
0
    return 0;
1834
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) {
1835
0
    return 1;
1836
0
  } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) {
1837
0
    return c->ce == scope;
1838
0
  } else {
1839
0
    zend_class_entry *ce = c->ce;
1840
0
    while (1) {
1841
0
      if (ce == scope) {
1842
0
        return 1;
1843
0
      }
1844
0
      if (!ce->parent) {
1845
0
        break;
1846
0
      }
1847
0
      if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
1848
0
        ce = ce->parent;
1849
0
      } else {
1850
0
        ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name);
1851
0
        if (!ce) {
1852
0
          break;
1853
0
        }
1854
0
      }
1855
0
    }
1856
    /* Reverse case cannot be true during compilation */
1857
0
    return 0;
1858
0
  }
1859
0
}
1860
1861
static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1862
0
{
1863
0
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1864
0
  zend_class_constant *cc;
1865
0
  zval *c;
1866
1867
0
  if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1868
0
    cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1869
0
  } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1870
0
    const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name);
1871
0
    if (ce) {
1872
0
      cc = zend_hash_find_ptr(&ce->constants_table, name);
1873
0
    } else {
1874
0
      return 0;
1875
0
    }
1876
0
  } else {
1877
0
    return 0;
1878
0
  }
1879
1880
0
  if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1881
0
    return 0;
1882
0
  }
1883
1884
0
  if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) {
1885
0
    return 0;
1886
0
  }
1887
1888
0
  c = &cc->value;
1889
1890
  /* Substitute case-sensitive (or lowercase) persistent class constants */
1891
0
  if (Z_TYPE_P(c) < IS_ARRAY) {
1892
0
    ZVAL_COPY_OR_DUP(zv, c);
1893
0
    return 1;
1894
0
  } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) {
1895
0
    ZVAL_COPY_OR_DUP(zv, c);
1896
0
    return 1;
1897
0
  }
1898
1899
0
  return 0;
1900
0
}
1901
/* }}} */
1902
1903
static void zend_add_to_list(void *result, void *item) /* {{{ */
1904
0
{
1905
0
  void** list = *(void**)result;
1906
0
  size_t n = 0;
1907
1908
0
  if (list) {
1909
0
    while (list[n]) {
1910
0
      n++;
1911
0
    }
1912
0
  }
1913
1914
0
  list = erealloc(list, sizeof(void*) * (n+2));
1915
1916
0
  list[n]   = item;
1917
0
  list[n+1] = NULL;
1918
1919
0
  *(void**)result = list;
1920
0
}
1921
/* }}} */
1922
1923
static void zend_do_extended_stmt(znode* result) /* {{{ */
1924
0
{
1925
0
  zend_op *opline;
1926
1927
0
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) {
1928
0
    return;
1929
0
  }
1930
1931
0
  opline = get_next_op();
1932
1933
0
  opline->opcode = ZEND_EXT_STMT;
1934
0
  if (result) {
1935
0
    SET_NODE(opline->op1, result);
1936
0
  }
1937
0
}
1938
/* }}} */
1939
1940
static void zend_do_extended_fcall_begin(void) /* {{{ */
1941
0
{
1942
0
  zend_op *opline;
1943
1944
0
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1945
0
    return;
1946
0
  }
1947
1948
0
  opline = get_next_op();
1949
1950
0
  opline->opcode = ZEND_EXT_FCALL_BEGIN;
1951
0
}
1952
/* }}} */
1953
1954
static void zend_do_extended_fcall_end(void) /* {{{ */
1955
0
{
1956
0
  zend_op *opline;
1957
1958
0
  if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) {
1959
0
    return;
1960
0
  }
1961
1962
0
  opline = get_next_op();
1963
1964
0
  opline->opcode = ZEND_EXT_FCALL_END;
1965
0
}
1966
/* }}} */
1967
1968
0
ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ {
1969
0
  zend_auto_global *auto_global;
1970
1971
0
  if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1972
0
    if (auto_global->armed) {
1973
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1974
0
    }
1975
0
    return 1;
1976
0
  }
1977
0
  return 0;
1978
0
}
1979
/* }}} */
1980
1981
ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */
1982
0
{
1983
0
  zend_auto_global *auto_global;
1984
1985
0
  if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1986
0
    if (auto_global->armed) {
1987
0
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1988
0
    }
1989
0
    return 1;
1990
0
  }
1991
0
  return 0;
1992
0
}
1993
/* }}} */
1994
1995
ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1996
16
{
1997
16
  zend_auto_global auto_global;
1998
16
  zend_result retval;
1999
2000
16
  auto_global.name = name;
2001
16
  auto_global.auto_global_callback = auto_global_callback;
2002
16
  auto_global.jit = jit;
2003
2004
16
  retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
2005
2006
16
  return retval;
2007
16
}
2008
/* }}} */
2009
2010
ZEND_API void zend_activate_auto_globals(void) /* {{{ */
2011
50.4k
{
2012
50.4k
  zend_auto_global *auto_global;
2013
2014
907k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2015
907k
    auto_global->armed = auto_global->jit || auto_global->auto_global_callback;
2016
907k
  } ZEND_HASH_FOREACH_END();
2017
2018
907k
  ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) {
2019
907k
    if (auto_global->armed && !auto_global->jit) {
2020
201k
      auto_global->armed = auto_global->auto_global_callback(auto_global->name);
2021
201k
    }
2022
907k
  } ZEND_HASH_FOREACH_END();
2023
50.4k
}
2024
/* }}} */
2025
2026
int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
2027
0
{
2028
0
  zval zv;
2029
0
  int ret;
2030
2031
0
  if (CG(increment_lineno)) {
2032
0
    CG(zend_lineno)++;
2033
0
    CG(increment_lineno) = 0;
2034
0
  }
2035
2036
0
  ret = lex_scan(&zv, elem);
2037
0
  ZEND_ASSERT(!EG(exception) || ret == T_ERROR);
2038
0
  return ret;
2039
2040
0
}
2041
/* }}} */
2042
2043
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */
2044
330
{
2045
330
  bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
2046
2047
330
  ce->refcount = 1;
2048
330
  ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
2049
330
  ce->ce_flags2 = 0;
2050
2051
330
  if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
2052
0
    ce->ce_flags |= ZEND_ACC_USE_GUARDS;
2053
0
  }
2054
2055
330
  ce->default_properties_table = NULL;
2056
330
  ce->default_static_members_table = NULL;
2057
330
  zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes);
2058
330
  zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes);
2059
330
  zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes);
2060
2061
330
  ce->doc_comment = NULL;
2062
2063
330
  ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
2064
330
  ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
2065
2066
330
  ce->default_object_handlers = &std_object_handlers;
2067
330
  ce->default_properties_count = 0;
2068
330
  ce->default_static_members_count = 0;
2069
330
  ce->properties_info_table = NULL;
2070
330
  ce->attributes = NULL;
2071
330
  ce->enum_backing_type = IS_UNDEF;
2072
330
  ce->backed_enum_table = NULL;
2073
2074
330
  if (nullify_handlers) {
2075
0
    ce->constructor = NULL;
2076
0
    ce->destructor = NULL;
2077
0
    ce->clone = NULL;
2078
0
    ce->__get = NULL;
2079
0
    ce->__set = NULL;
2080
0
    ce->__unset = NULL;
2081
0
    ce->__isset = NULL;
2082
0
    ce->__call = NULL;
2083
0
    ce->__callstatic = NULL;
2084
0
    ce->__tostring = NULL;
2085
0
    ce->__serialize = NULL;
2086
0
    ce->__unserialize = NULL;
2087
0
    ce->__debugInfo = NULL;
2088
0
    ce->create_object = NULL;
2089
0
    ce->get_iterator = NULL;
2090
0
    ce->iterator_funcs_ptr = NULL;
2091
0
    ce->arrayaccess_funcs_ptr = NULL;
2092
0
    ce->get_static_method = NULL;
2093
0
    ce->parent = NULL;
2094
0
    ce->parent_name = NULL;
2095
0
    ce->num_interfaces = 0;
2096
0
    ce->interfaces = NULL;
2097
0
    ce->num_traits = 0;
2098
0
    ce->num_hooked_props = 0;
2099
0
    ce->num_hooked_prop_variance_checks = 0;
2100
0
    ce->trait_names = NULL;
2101
0
    ce->trait_aliases = NULL;
2102
0
    ce->trait_precedences = NULL;
2103
0
    ce->serialize = NULL;
2104
0
    ce->unserialize = NULL;
2105
0
    if (ce->type == ZEND_INTERNAL_CLASS) {
2106
0
      ce->info.internal.module = NULL;
2107
0
      ce->info.internal.builtin_functions = NULL;
2108
0
    }
2109
0
  }
2110
330
}
2111
/* }}} */
2112
2113
ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
2114
0
{
2115
0
  return op_array->vars[EX_VAR_TO_NUM(var)];
2116
0
}
2117
/* }}} */
2118
2119
zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
2120
0
{
2121
0
  zval *left_zv = zend_ast_get_zval(left_ast);
2122
0
  zend_string *left = Z_STR_P(left_zv);
2123
0
  zend_string *right = zend_ast_get_str(right_ast);
2124
2125
0
  zend_string *result;
2126
0
  size_t left_len = ZSTR_LEN(left);
2127
0
  size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
2128
2129
0
  result = zend_string_extend(left, len, 0);
2130
0
  ZSTR_VAL(result)[left_len] = '\\';
2131
0
  memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
2132
0
  ZSTR_VAL(result)[len] = '\0';
2133
0
  zend_string_release_ex(right, 0);
2134
2135
0
  ZVAL_STR(left_zv, result);
2136
0
  return left_ast;
2137
0
}
2138
/* }}} */
2139
2140
zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
2141
0
{
2142
0
  zval *zv = zend_ast_get_zval(ast);
2143
0
  if (Z_TYPE_P(zv) == IS_LONG) {
2144
0
    if (Z_LVAL_P(zv) == 0) {
2145
0
      ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0));
2146
0
    } else {
2147
0
      ZEND_ASSERT(Z_LVAL_P(zv) > 0);
2148
0
      Z_LVAL_P(zv) *= -1;
2149
0
    }
2150
0
  } else if (Z_TYPE_P(zv) == IS_STRING) {
2151
0
    size_t orig_len = Z_STRLEN_P(zv);
2152
0
    Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
2153
0
    memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
2154
0
    Z_STRVAL_P(zv)[0] = '-';
2155
0
  } else {
2156
0
    ZEND_UNREACHABLE();
2157
0
  }
2158
0
  return ast;
2159
0
}
2160
/* }}} */
2161
2162
static void zend_verify_namespace(void) /* {{{ */
2163
0
{
2164
0
  if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
2165
0
    zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
2166
0
  }
2167
0
}
2168
/* }}} */
2169
2170
/* {{{ zend_dirname
2171
   Returns directory name component of path */
2172
ZEND_API size_t zend_dirname(char *path, size_t len)
2173
0
{
2174
0
  char *end = path + len - 1;
2175
0
  unsigned int len_adjust = 0;
2176
2177
#ifdef ZEND_WIN32
2178
  /* Note that on Win32 CWD is per drive (heritage from CP/M).
2179
   * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
2180
   */
2181
  if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
2182
    /* Skip over the drive spec (if any) so as not to change */
2183
    path += 2;
2184
    len_adjust += 2;
2185
    if (2 == len) {
2186
      /* Return "c:" on Win32 for dirname("c:").
2187
       * It would be more consistent to return "c:."
2188
       * but that would require making the string *longer*.
2189
       */
2190
      return len;
2191
    }
2192
  }
2193
#endif
2194
2195
0
  if (len == 0) {
2196
    /* Illegal use of this function */
2197
0
    return 0;
2198
0
  }
2199
2200
  /* Strip trailing slashes */
2201
0
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2202
0
    end--;
2203
0
  }
2204
0
  if (end < path) {
2205
    /* The path only contained slashes */
2206
0
    path[0] = DEFAULT_SLASH;
2207
0
    path[1] = '\0';
2208
0
    return 1 + len_adjust;
2209
0
  }
2210
2211
  /* Strip filename */
2212
0
  while (end >= path && !IS_SLASH_P_EX(end, end == path)) {
2213
0
    end--;
2214
0
  }
2215
0
  if (end < path) {
2216
    /* No slash found, therefore return '.' */
2217
0
    path[0] = '.';
2218
0
    path[1] = '\0';
2219
0
    return 1 + len_adjust;
2220
0
  }
2221
2222
  /* Strip slashes which came before the file name */
2223
0
  while (end >= path && IS_SLASH_P_EX(end, end == path)) {
2224
0
    end--;
2225
0
  }
2226
0
  if (end < path) {
2227
0
    path[0] = DEFAULT_SLASH;
2228
0
    path[1] = '\0';
2229
0
    return 1 + len_adjust;
2230
0
  }
2231
0
  *(end+1) = '\0';
2232
2233
0
  return (size_t)(end + 1 - path) + len_adjust;
2234
0
}
2235
/* }}} */
2236
2237
static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */
2238
0
{
2239
0
  uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
2240
2241
0
  switch (type) {
2242
0
    case BP_VAR_R:
2243
0
      opline->result_type = IS_TMP_VAR;
2244
0
      result->op_type = IS_TMP_VAR;
2245
0
      return;
2246
0
    case BP_VAR_W:
2247
0
      opline->opcode += 1 * factor;
2248
0
      return;
2249
0
    case BP_VAR_RW:
2250
0
      opline->opcode += 2 * factor;
2251
0
      return;
2252
0
    case BP_VAR_IS:
2253
0
      opline->result_type = IS_TMP_VAR;
2254
0
      result->op_type = IS_TMP_VAR;
2255
0
      opline->opcode += 3 * factor;
2256
0
      return;
2257
0
    case BP_VAR_FUNC_ARG:
2258
0
      opline->opcode += 4 * factor;
2259
0
      return;
2260
0
    case BP_VAR_UNSET:
2261
0
      opline->opcode += 5 * factor;
2262
0
      return;
2263
0
    EMPTY_SWITCH_DEFAULT_CASE()
2264
0
  }
2265
0
}
2266
/* }}} */
2267
2268
static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
2269
0
{
2270
0
  opline->result_type = IS_VAR;
2271
0
  opline->result.var = get_temporary_variable();
2272
0
  GET_NODE(result, opline->result);
2273
0
}
2274
/* }}} */
2275
2276
static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2277
0
{
2278
0
  opline->result_type = IS_TMP_VAR;
2279
0
  opline->result.var = get_temporary_variable();
2280
0
  GET_NODE(result, opline->result);
2281
0
}
2282
/* }}} */
2283
2284
static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2285
0
{
2286
0
  zend_op *opline = get_next_op();
2287
0
  opline->opcode = opcode;
2288
2289
0
  if (op1 != NULL) {
2290
0
    SET_NODE(opline->op1, op1);
2291
0
  }
2292
2293
0
  if (op2 != NULL) {
2294
0
    SET_NODE(opline->op2, op2);
2295
0
  }
2296
2297
0
  if (result) {
2298
0
    zend_make_var_result(result, opline);
2299
0
  }
2300
0
  return opline;
2301
0
}
2302
/* }}} */
2303
2304
static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2305
0
{
2306
0
  zend_op *opline = get_next_op();
2307
0
  opline->opcode = opcode;
2308
2309
0
  if (op1 != NULL) {
2310
0
    SET_NODE(opline->op1, op1);
2311
0
  }
2312
2313
0
  if (op2 != NULL) {
2314
0
    SET_NODE(opline->op2, op2);
2315
0
  }
2316
2317
0
  if (result) {
2318
0
    zend_make_tmp_result(result, opline);
2319
0
  }
2320
2321
0
  return opline;
2322
0
}
2323
/* }}} */
2324
2325
static void zend_emit_tick(void) /* {{{ */
2326
0
{
2327
0
  zend_op *opline;
2328
2329
  /* This prevents a double TICK generated by the parser statement of "declare()" */
2330
0
  if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2331
0
    return;
2332
0
  }
2333
2334
0
  opline = get_next_op();
2335
2336
0
  opline->opcode = ZEND_TICKS;
2337
0
  opline->extended_value = FC(declarables).ticks;
2338
0
}
2339
/* }}} */
2340
2341
static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2342
0
{
2343
0
  return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2344
0
}
2345
/* }}} */
2346
2347
static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2348
0
{
2349
0
  uint32_t opnum = get_next_op_number();
2350
0
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2351
0
  opline->op1.opline_num = opnum_target;
2352
0
  return opnum;
2353
0
}
2354
/* }}} */
2355
2356
ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */
2357
0
{
2358
0
  switch (opline->opcode) {
2359
0
    case ZEND_IS_IDENTICAL:
2360
0
    case ZEND_IS_NOT_IDENTICAL:
2361
0
    case ZEND_IS_EQUAL:
2362
0
    case ZEND_IS_NOT_EQUAL:
2363
0
    case ZEND_IS_SMALLER:
2364
0
    case ZEND_IS_SMALLER_OR_EQUAL:
2365
0
    case ZEND_CASE:
2366
0
    case ZEND_CASE_STRICT:
2367
0
    case ZEND_ISSET_ISEMPTY_CV:
2368
0
    case ZEND_ISSET_ISEMPTY_VAR:
2369
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2370
0
    case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2371
0
    case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2372
0
    case ZEND_INSTANCEOF:
2373
0
    case ZEND_TYPE_CHECK:
2374
0
    case ZEND_DEFINED:
2375
0
    case ZEND_IN_ARRAY:
2376
0
    case ZEND_ARRAY_KEY_EXISTS:
2377
0
      return 1;
2378
0
    default:
2379
0
      return 0;
2380
0
  }
2381
0
}
2382
/* }}} */
2383
2384
static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2385
0
{
2386
0
  uint32_t opnum = get_next_op_number();
2387
0
  zend_op *opline;
2388
2389
0
  if (cond->op_type == IS_TMP_VAR && opnum > 0) {
2390
0
    opline = CG(active_op_array)->opcodes + opnum - 1;
2391
0
    if (opline->result_type == IS_TMP_VAR
2392
0
     && opline->result.var == cond->u.op.var
2393
0
     && zend_is_smart_branch(opline)) {
2394
0
      if (opcode == ZEND_JMPZ) {
2395
0
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ;
2396
0
      } else {
2397
0
        ZEND_ASSERT(opcode == ZEND_JMPNZ);
2398
0
        opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ;
2399
0
      }
2400
0
    }
2401
0
  }
2402
0
  opline = zend_emit_op(NULL, opcode, cond, NULL);
2403
0
  opline->op2.opline_num = opnum_target;
2404
0
  return opnum;
2405
0
}
2406
/* }}} */
2407
2408
static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2409
0
{
2410
0
  zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2411
0
  switch (opline->opcode) {
2412
0
    case ZEND_JMP:
2413
0
      opline->op1.opline_num = opnum_target;
2414
0
      break;
2415
0
    case ZEND_JMPZ:
2416
0
    case ZEND_JMPNZ:
2417
0
    case ZEND_JMPZ_EX:
2418
0
    case ZEND_JMPNZ_EX:
2419
0
    case ZEND_JMP_SET:
2420
0
    case ZEND_COALESCE:
2421
0
    case ZEND_JMP_NULL:
2422
0
    case ZEND_BIND_INIT_STATIC_OR_JMP:
2423
0
    case ZEND_JMP_FRAMELESS:
2424
0
      opline->op2.opline_num = opnum_target;
2425
0
      break;
2426
0
    EMPTY_SWITCH_DEFAULT_CASE()
2427
0
  }
2428
0
}
2429
/* }}} */
2430
2431
static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2432
0
{
2433
0
  zend_update_jump_target(opnum_jump, get_next_op_number());
2434
0
}
2435
/* }}} */
2436
2437
static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */
2438
0
{
2439
0
  zend_op tmp_opline;
2440
2441
0
  init_op(&tmp_opline);
2442
2443
0
  tmp_opline.opcode = opcode;
2444
0
  if (op1 != NULL) {
2445
0
    SET_NODE(tmp_opline.op1, op1);
2446
0
  }
2447
0
  if (op2 != NULL) {
2448
0
    SET_NODE(tmp_opline.op2, op2);
2449
0
  }
2450
0
  if (result) {
2451
0
    zend_make_var_result(result, &tmp_opline);
2452
0
  }
2453
2454
0
  zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2455
0
  return zend_stack_top(&CG(delayed_oplines_stack));
2456
0
}
2457
/* }}} */
2458
2459
static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2460
0
{
2461
0
  return zend_stack_count(&CG(delayed_oplines_stack));
2462
0
}
2463
/* }}} */
2464
2465
static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2466
0
{
2467
0
  zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2468
0
  uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2469
2470
0
  ZEND_ASSERT(count >= offset);
2471
0
  for (i = offset; i < count; ++i) {
2472
0
    if (EXPECTED(oplines[i].opcode != ZEND_NOP)) {
2473
0
      opline = get_next_op();
2474
0
      memcpy(opline, &oplines[i], sizeof(zend_op));
2475
0
    } else {
2476
0
      opline = CG(active_op_array)->opcodes + oplines[i].extended_value;
2477
0
    }
2478
0
  }
2479
2480
0
  CG(delayed_oplines_stack).top = offset;
2481
0
  return opline;
2482
0
}
2483
/* }}} */
2484
2485
static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind)
2486
0
{
2487
0
  switch (ast_kind) {
2488
0
    case ZEND_AST_DIM:
2489
0
    case ZEND_AST_PROP:
2490
0
    case ZEND_AST_NULLSAFE_PROP:
2491
0
    case ZEND_AST_STATIC_PROP:
2492
0
    case ZEND_AST_METHOD_CALL:
2493
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2494
0
    case ZEND_AST_STATIC_CALL:
2495
0
      return 1;
2496
0
    default:
2497
0
      return 0;
2498
0
  }
2499
0
}
2500
2501
static bool zend_ast_is_short_circuited(const zend_ast *ast)
2502
0
{
2503
0
  switch (ast->kind) {
2504
0
    case ZEND_AST_DIM:
2505
0
    case ZEND_AST_PROP:
2506
0
    case ZEND_AST_STATIC_PROP:
2507
0
    case ZEND_AST_METHOD_CALL:
2508
0
    case ZEND_AST_STATIC_CALL:
2509
0
      return zend_ast_is_short_circuited(ast->child[0]);
2510
0
    case ZEND_AST_NULLSAFE_PROP:
2511
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
2512
0
      return 1;
2513
0
    default:
2514
0
      return 0;
2515
0
  }
2516
0
}
2517
2518
static void zend_assert_not_short_circuited(const zend_ast *ast)
2519
0
{
2520
0
  if (zend_ast_is_short_circuited(ast)) {
2521
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain");
2522
0
  }
2523
0
}
2524
2525
/* Mark nodes that are an inner part of a short-circuiting chain.
2526
 * We should not perform a "commit" on them, as it will be performed by the outer-most node.
2527
 * We do this to avoid passing down an argument in various compile functions. */
2528
2529
0
#define ZEND_SHORT_CIRCUITING_INNER 0x8000
2530
2531
0
static void zend_short_circuiting_mark_inner(zend_ast *ast) {
2532
0
  if (zend_ast_kind_is_short_circuited(ast->kind)) {
2533
0
    ast->attr |= ZEND_SHORT_CIRCUITING_INNER;
2534
0
  }
2535
0
}
2536
2537
static uint32_t zend_short_circuiting_checkpoint(void)
2538
0
{
2539
0
  return zend_stack_count(&CG(short_circuiting_opnums));
2540
0
}
2541
2542
static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast)
2543
0
{
2544
0
  bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind)
2545
0
    || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY;
2546
0
  if (!is_short_circuited) {
2547
0
    ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint
2548
0
      && "Short circuiting stack should be empty");
2549
0
    return;
2550
0
  }
2551
2552
0
  if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) {
2553
    /* Outer-most node will commit. */
2554
0
    return;
2555
0
  }
2556
2557
0
  while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) {
2558
0
    uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums));
2559
0
    zend_op *opline = &CG(active_op_array)->opcodes[opnum];
2560
0
    opline->op2.opline_num = get_next_op_number();
2561
0
    SET_NODE(opline->result, result);
2562
0
    opline->extended_value |=
2563
0
      ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET :
2564
0
      ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY :
2565
0
                                    ZEND_SHORT_CIRCUITING_CHAIN_EXPR;
2566
0
    zend_stack_del_top(&CG(short_circuiting_opnums));
2567
0
  }
2568
0
}
2569
2570
static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type)
2571
0
{
2572
0
  uint32_t jmp_null_opnum = get_next_op_number();
2573
0
  zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL);
2574
0
  if (opline->op1_type == IS_CONST) {
2575
0
    Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
2576
0
  }
2577
0
  if (bp_type == BP_VAR_IS) {
2578
0
    opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS;
2579
0
  }
2580
0
  zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum);
2581
0
}
2582
2583
static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
2584
0
{
2585
0
  const zend_memoize_mode memoize_mode = CG(memoize_mode);
2586
0
  if (memoize_mode == ZEND_MEMOIZE_COMPILE) {
2587
0
    znode memoized_result;
2588
2589
    /* Go through normal compilation */
2590
0
    CG(memoize_mode) = ZEND_MEMOIZE_NONE;
2591
0
    zend_compile_expr(result, expr);
2592
0
    CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
2593
2594
0
    if (result->op_type == IS_VAR) {
2595
0
      zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL);
2596
0
    } else if (result->op_type == IS_TMP_VAR) {
2597
0
      zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL);
2598
0
    } else {
2599
0
      if (result->op_type == IS_CONST) {
2600
0
        Z_TRY_ADDREF(result->u.constant);
2601
0
      }
2602
0
      memoized_result = *result;
2603
0
    }
2604
2605
0
    zend_hash_index_update_mem(
2606
0
      CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode));
2607
0
  } else if (memoize_mode == ZEND_MEMOIZE_FETCH) {
2608
0
    const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr);
2609
0
    *result = *memoized_result;
2610
0
    if (result->op_type == IS_CONST) {
2611
0
      Z_TRY_ADDREF(result->u.constant);
2612
0
    }
2613
0
  } else {
2614
0
    ZEND_UNREACHABLE();
2615
0
  }
2616
0
}
2617
/* }}} */
2618
2619
static void zend_emit_return_type_check(
2620
    znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */
2621
0
{
2622
0
  zend_type type = return_info->type;
2623
0
  if (ZEND_TYPE_IS_SET(type)) {
2624
0
    zend_op *opline;
2625
2626
    /* `return ...;` is illegal in a void function (but `return;` isn't) */
2627
0
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) {
2628
0
      if (expr) {
2629
0
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2630
0
          zend_error_noreturn(E_COMPILE_ERROR,
2631
0
            "A void %s must not return a value "
2632
0
            "(did you mean \"return;\" instead of \"return null;\"?)",
2633
0
            CG(active_class_entry) != NULL ? "method" : "function");
2634
0
        } else {
2635
0
          zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value",
2636
0
          CG(active_class_entry) != NULL ? "method" : "function");
2637
0
        }
2638
0
      }
2639
      /* we don't need run-time check */
2640
0
      return;
2641
0
    }
2642
2643
    /* `return` is illegal in a never-returning function */
2644
0
    if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) {
2645
      /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */
2646
0
      ZEND_ASSERT(!implicit);
2647
0
      zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return",
2648
0
        CG(active_class_entry) != NULL ? "method" : "function");
2649
0
      return;
2650
0
    }
2651
2652
0
    if (!expr && !implicit) {
2653
0
      if (ZEND_TYPE_ALLOW_NULL(type)) {
2654
0
        zend_error_noreturn(E_COMPILE_ERROR,
2655
0
          "A %s with return type must return a value "
2656
0
          "(did you mean \"return null;\" instead of \"return;\"?)",
2657
0
          CG(active_class_entry) != NULL ? "method" : "function");
2658
0
      } else {
2659
0
        zend_error_noreturn(E_COMPILE_ERROR,
2660
0
          "A %s with return type must return a value",
2661
0
          CG(active_class_entry) != NULL ? "method" : "function");
2662
0
      }
2663
0
    }
2664
2665
0
    if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) {
2666
      /* we don't need run-time check for mixed return type */
2667
0
      return;
2668
0
    }
2669
2670
0
    if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) {
2671
      /* we don't need run-time check */
2672
0
      return;
2673
0
    }
2674
2675
0
    opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2676
0
    if (expr && expr->op_type == IS_CONST) {
2677
0
      opline->result_type = expr->op_type = IS_TMP_VAR;
2678
0
      opline->result.var = expr->u.op.var = get_temporary_variable();
2679
0
    }
2680
0
  }
2681
0
}
2682
/* }}} */
2683
2684
void zend_emit_final_return(bool return_one) /* {{{ */
2685
0
{
2686
0
  znode zn;
2687
0
  zend_op *ret;
2688
0
  bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2689
2690
0
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)
2691
0
      && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2692
0
    zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
2693
2694
0
    if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
2695
0
      zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL);
2696
0
      return;
2697
0
    }
2698
2699
0
    zend_emit_return_type_check(NULL, return_info, true);
2700
0
  }
2701
2702
0
  zn.op_type = IS_CONST;
2703
0
  if (return_one) {
2704
0
    ZVAL_LONG(&zn.u.constant, 1);
2705
0
  } else {
2706
0
    ZVAL_NULL(&zn.u.constant);
2707
0
  }
2708
2709
0
  ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2710
0
  ret->extended_value = -1;
2711
0
}
2712
/* }}} */
2713
2714
static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */
2715
0
{
2716
0
  return ast->kind == ZEND_AST_VAR
2717
0
    || ast->kind == ZEND_AST_DIM
2718
0
    || ast->kind == ZEND_AST_PROP
2719
0
    || ast->kind == ZEND_AST_NULLSAFE_PROP
2720
0
    || ast->kind == ZEND_AST_STATIC_PROP;
2721
0
}
2722
/* }}} */
2723
2724
static inline bool zend_is_call(const zend_ast *ast) /* {{{ */
2725
0
{
2726
0
  return ast->kind == ZEND_AST_CALL
2727
0
    || ast->kind == ZEND_AST_METHOD_CALL
2728
0
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
2729
0
    || ast->kind == ZEND_AST_STATIC_CALL
2730
0
    || ast->kind == ZEND_AST_PIPE;
2731
0
}
2732
/* }}} */
2733
2734
static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */
2735
0
{
2736
0
  return zend_is_variable(ast) || zend_is_call(ast);
2737
0
}
2738
/* }}} */
2739
2740
static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */
2741
0
{
2742
0
  return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2743
0
    || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP
2744
0
    || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2745
0
}
2746
/* }}} */
2747
2748
static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */
2749
0
{
2750
0
  while (
2751
0
    ast->kind == ZEND_AST_DIM
2752
0
    || ast->kind == ZEND_AST_PROP
2753
0
  ) {
2754
0
    ast = ast->child[0];
2755
0
  }
2756
2757
0
  return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast);
2758
0
}
2759
/* }}} */
2760
2761
static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2762
0
{
2763
0
  if (name_ast->kind != ZEND_AST_ZVAL) {
2764
0
    return 0;
2765
0
  }
2766
2767
0
  return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2768
0
}
2769
/* }}} */
2770
2771
static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2772
0
{
2773
0
  if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2774
0
    zend_ulong index;
2775
2776
0
    if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2777
0
      zval_ptr_dtor(&node->u.constant);
2778
0
      ZVAL_LONG(&node->u.constant, index);
2779
0
    }
2780
0
  }
2781
0
}
2782
/* }}} */
2783
2784
static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */
2785
0
{
2786
0
  if (Z_TYPE(dim_node->u.constant) == IS_STRING) {
2787
0
    zend_ulong index;
2788
2789
0
    if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) {
2790
      /* For numeric indexes we also keep the original value to use by ArrayAccess
2791
       * See bug #63217
2792
       */
2793
0
      int c = zend_add_literal(&dim_node->u.constant);
2794
0
      ZEND_ASSERT(opline->op2.constant + 1 == c);
2795
0
      ZVAL_LONG(CT_CONSTANT(opline->op2), index);
2796
0
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE;
2797
0
      return;
2798
0
    }
2799
0
  }
2800
0
}
2801
/* }}} */
2802
2803
static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2804
0
{
2805
0
  if (class_node->op_type == IS_CONST) {
2806
0
    opline->op1_type = IS_CONST;
2807
0
    opline->op1.constant = zend_add_class_name_literal(
2808
0
      Z_STR(class_node->u.constant));
2809
0
  } else {
2810
0
    SET_NODE(opline->op1, class_node);
2811
0
  }
2812
0
}
2813
/* }}} */
2814
2815
static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2816
0
{
2817
0
  uint32_t fetch_type;
2818
2819
0
  if (name_ast->kind != ZEND_AST_ZVAL) {
2820
0
    znode name_node;
2821
2822
0
    zend_compile_expr(&name_node, name_ast);
2823
2824
0
    if (name_node.op_type == IS_CONST) {
2825
0
      zend_string *name;
2826
2827
0
      if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2828
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2829
0
      }
2830
2831
0
      name = Z_STR(name_node.u.constant);
2832
0
      fetch_type = zend_get_class_fetch_type(name);
2833
2834
0
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2835
0
        result->op_type = IS_CONST;
2836
0
        ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2837
0
      } else {
2838
0
        zend_ensure_valid_class_fetch_type(fetch_type);
2839
0
        result->op_type = IS_UNUSED;
2840
0
        result->u.op.num = fetch_type | fetch_flags;
2841
0
      }
2842
2843
0
      zend_string_release_ex(name, 0);
2844
0
    } else {
2845
0
      zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2846
0
      opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2847
0
    }
2848
0
    return;
2849
0
  }
2850
2851
  /* Fully qualified names are always default refs */
2852
0
  if (name_ast->attr == ZEND_NAME_FQ) {
2853
0
    result->op_type = IS_CONST;
2854
0
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2855
0
    return;
2856
0
  }
2857
2858
0
  fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2859
0
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2860
0
    result->op_type = IS_CONST;
2861
0
    ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2862
0
  } else {
2863
0
    zend_ensure_valid_class_fetch_type(fetch_type);
2864
0
    result->op_type = IS_UNUSED;
2865
0
    result->u.op.num = fetch_type | fetch_flags;
2866
0
  }
2867
0
}
2868
/* }}} */
2869
2870
static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
2871
0
{
2872
0
  zend_ast *name_ast = ast->child[0];
2873
0
  if (name_ast->kind == ZEND_AST_ZVAL) {
2874
0
    zval *zv = zend_ast_get_zval(name_ast);
2875
0
    zend_string *name;
2876
2877
0
    if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
2878
0
      name = zval_make_interned_string(zv);
2879
0
    } else {
2880
0
      name = zend_new_interned_string(zval_get_string_func(zv));
2881
0
    }
2882
2883
0
    if (zend_is_auto_global(name)) {
2884
0
      return FAILURE;
2885
0
    }
2886
2887
0
    if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) {
2888
0
      if (type == BP_VAR_R) {
2889
0
        zend_error(E_DEPRECATED,
2890
0
          "The predefined locally scoped $http_response_header variable is deprecated,"
2891
0
          " call http_get_last_response_headers() instead");
2892
0
      } else if (type == BP_VAR_W) {
2893
0
        CG(context).has_assigned_to_http_response_header = true;
2894
0
      }
2895
0
    }
2896
2897
0
    result->op_type = IS_CV;
2898
0
    result->u.op.var = lookup_cv(name);
2899
2900
0
    if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) {
2901
0
      zend_string_release_ex(name, 0);
2902
0
    }
2903
2904
0
    return SUCCESS;
2905
0
  }
2906
2907
0
  return FAILURE;
2908
0
}
2909
/* }}} */
2910
2911
static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2912
0
{
2913
0
  zend_ast *name_ast = ast->child[0];
2914
0
  znode name_node;
2915
0
  zend_op *opline;
2916
2917
0
  zend_compile_expr(&name_node, name_ast);
2918
0
  if (name_node.op_type == IS_CONST) {
2919
0
    convert_to_string(&name_node.u.constant);
2920
0
  }
2921
2922
0
  if (delayed) {
2923
0
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2924
0
  } else {
2925
0
    opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2926
0
  }
2927
2928
0
  if (name_node.op_type == IS_CONST &&
2929
0
      zend_is_auto_global(Z_STR(name_node.u.constant))) {
2930
2931
0
    opline->extended_value = ZEND_FETCH_GLOBAL;
2932
0
  } else {
2933
    // TODO: Have a test case for this?
2934
0
    if (name_node.op_type == IS_CONST
2935
0
      && type == BP_VAR_R
2936
0
      && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) {
2937
0
      zend_error(E_DEPRECATED,
2938
0
        "The predefined locally scoped $http_response_header variable is deprecated,"
2939
0
        " call http_get_last_response_headers() instead");
2940
0
    }
2941
0
    opline->extended_value = ZEND_FETCH_LOCAL;
2942
0
  }
2943
2944
0
  zend_adjust_for_fetch_type(opline, result, type);
2945
0
  return opline;
2946
0
}
2947
/* }}} */
2948
2949
static bool is_this_fetch(const zend_ast *ast) /* {{{ */
2950
0
{
2951
0
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2952
0
    const zval *name = zend_ast_get_zval(ast->child[0]);
2953
0
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS));
2954
0
  }
2955
2956
0
  return 0;
2957
0
}
2958
/* }}} */
2959
2960
static bool is_globals_fetch(const zend_ast *ast)
2961
0
{
2962
0
  if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2963
0
    const zval *name = zend_ast_get_zval(ast->child[0]);
2964
0
    return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS");
2965
0
  }
2966
2967
0
  return 0;
2968
0
}
2969
2970
static bool is_global_var_fetch(const zend_ast *ast)
2971
0
{
2972
0
  return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]);
2973
0
}
2974
2975
static bool this_guaranteed_exists(void) /* {{{ */
2976
0
{
2977
0
  const zend_oparray_context *ctx = &CG(context);
2978
0
  while (ctx) {
2979
    /* Instance methods always have a $this.
2980
     * This also includes closures that have a scope and use $this. */
2981
0
    const zend_op_array *op_array = ctx->op_array;
2982
0
    if (op_array->fn_flags & ZEND_ACC_STATIC) {
2983
0
      return false;
2984
0
    } else if (op_array->scope) {
2985
0
      return true;
2986
0
    } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
2987
0
      return false;
2988
0
    }
2989
0
    ctx = ctx->prev;
2990
0
  }
2991
0
  return false;
2992
0
}
2993
/* }}} */
2994
2995
static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */
2996
0
{
2997
0
  if (is_this_fetch(ast)) {
2998
0
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2999
0
    if ((type == BP_VAR_R) || (type == BP_VAR_IS)) {
3000
0
      opline->result_type = IS_TMP_VAR;
3001
0
      result->op_type = IS_TMP_VAR;
3002
0
    }
3003
0
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3004
0
    return opline;
3005
0
  } else if (is_globals_fetch(ast)) {
3006
0
    zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL);
3007
0
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3008
0
      opline->result_type = IS_TMP_VAR;
3009
0
      result->op_type = IS_TMP_VAR;
3010
0
    }
3011
0
    return opline;
3012
0
  } else if (zend_try_compile_cv(result, ast, type) == FAILURE) {
3013
0
    return zend_compile_simple_var_no_cv(result, ast, type, delayed);
3014
0
  }
3015
0
  return NULL;
3016
0
}
3017
/* }}} */
3018
3019
static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */
3020
0
{
3021
0
  if (type != BP_VAR_R
3022
0
   && type != BP_VAR_IS
3023
   /* Whether a FUNC_ARG is R may only be determined at runtime. */
3024
0
   && type != BP_VAR_FUNC_ARG
3025
0
   && zend_is_call(ast)) {
3026
0
    if (node->op_type == IS_VAR) {
3027
0
      zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
3028
0
      opline->result_type = IS_VAR;
3029
0
      opline->result.var = opline->op1.var;
3030
0
    } else {
3031
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3032
0
    }
3033
0
  }
3034
0
}
3035
/* }}} */
3036
3037
static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3038
0
{
3039
0
  znode dummy_node;
3040
0
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
3041
0
    zend_ast_create_znode(value_node));
3042
0
  zend_compile_expr(&dummy_node, assign_ast);
3043
0
  zend_do_free(&dummy_node);
3044
0
}
3045
/* }}} */
3046
3047
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
3048
0
{
3049
0
  zend_ast *var_ast = ast->child[0];
3050
0
  zend_ast *dim_ast = ast->child[1];
3051
0
  zend_op *opline;
3052
3053
0
  znode var_node, dim_node;
3054
3055
0
  if (is_globals_fetch(var_ast)) {
3056
0
    if (dim_ast == NULL) {
3057
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS");
3058
0
    }
3059
3060
0
    zend_compile_expr(&dim_node, dim_ast);
3061
0
    if (dim_node.op_type == IS_CONST) {
3062
0
      convert_to_string(&dim_node.u.constant);
3063
0
    }
3064
3065
0
    opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL);
3066
0
    opline->extended_value = ZEND_FETCH_GLOBAL;
3067
0
    zend_adjust_for_fetch_type(opline, result, type);
3068
0
    return opline;
3069
0
  } else {
3070
0
    zend_short_circuiting_mark_inner(var_ast);
3071
0
    opline = zend_delayed_compile_var(&var_node, var_ast, type, false);
3072
0
    if (opline) {
3073
0
      if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) {
3074
0
        opline->extended_value |= ZEND_FETCH_DIM_WRITE;
3075
0
      } else if (opline->opcode == ZEND_FETCH_DIM_W
3076
0
          || opline->opcode == ZEND_FETCH_DIM_RW
3077
0
          || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3078
0
          || opline->opcode == ZEND_FETCH_DIM_UNSET) {
3079
0
        opline->extended_value = ZEND_FETCH_DIM_DIM;
3080
0
      }
3081
0
    }
3082
0
  }
3083
3084
0
  zend_separate_if_call_and_write(&var_node, var_ast, type);
3085
3086
0
  if (dim_ast == NULL) {
3087
0
    if (type == BP_VAR_R || type == BP_VAR_IS) {
3088
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
3089
0
    }
3090
0
    if (type == BP_VAR_UNSET) {
3091
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
3092
0
    }
3093
0
    dim_node.op_type = IS_UNUSED;
3094
0
  } else {
3095
0
    zend_compile_expr(&dim_node, dim_ast);
3096
0
  }
3097
3098
0
  opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
3099
0
  zend_adjust_for_fetch_type(opline, result, type);
3100
0
  if (by_ref) {
3101
0
    opline->extended_value = ZEND_FETCH_DIM_REF;
3102
0
  }
3103
3104
0
  if (dim_node.op_type == IS_CONST) {
3105
0
    zend_handle_numeric_dim(opline, &dim_node);
3106
0
  }
3107
0
  return opline;
3108
0
}
3109
3110
static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3111
0
{
3112
0
  uint32_t offset = zend_delayed_compile_begin();
3113
0
  zend_delayed_compile_dim(result, ast, type, by_ref);
3114
0
  return zend_delayed_compile_end(offset);
3115
0
}
3116
/* }}} */
3117
3118
static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3119
0
{
3120
0
  zend_ast *obj_ast = ast->child[0];
3121
0
  zend_ast *prop_ast = ast->child[1];
3122
3123
0
  znode obj_node, prop_node;
3124
0
  zend_op *opline;
3125
0
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP;
3126
3127
0
  if (is_this_fetch(obj_ast)) {
3128
0
    if (this_guaranteed_exists()) {
3129
0
      obj_node.op_type = IS_UNUSED;
3130
0
    } else {
3131
0
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
3132
0
    }
3133
0
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3134
3135
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
3136
     * check for a nullsafe access. */
3137
0
  } else {
3138
0
    zend_short_circuiting_mark_inner(obj_ast);
3139
0
    opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false);
3140
0
    if (opline && (opline->opcode == ZEND_FETCH_DIM_W
3141
0
        || opline->opcode == ZEND_FETCH_DIM_RW
3142
0
        || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG
3143
0
        || opline->opcode == ZEND_FETCH_DIM_UNSET)) {
3144
0
      opline->extended_value = ZEND_FETCH_DIM_OBJ;
3145
0
    }
3146
3147
0
    zend_separate_if_call_and_write(&obj_node, obj_ast, type);
3148
0
    if (nullsafe) {
3149
0
      if (obj_node.op_type == IS_TMP_VAR) {
3150
        /* Flush delayed oplines */
3151
0
        zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
3152
0
        uint32_t var = obj_node.u.op.var;
3153
0
        uint32_t count = zend_stack_count(&CG(delayed_oplines_stack));
3154
0
        uint32_t i = count;
3155
3156
0
        while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) {
3157
0
          i--;
3158
0
          if (oplines[i].op1_type == IS_TMP_VAR) {
3159
0
            var = oplines[i].op1.var;
3160
0
          } else {
3161
0
            break;
3162
0
          }
3163
0
        }
3164
0
        for (; i < count; ++i) {
3165
0
          if (oplines[i].opcode != ZEND_NOP) {
3166
0
            opline = get_next_op();
3167
0
            memcpy(opline, &oplines[i], sizeof(zend_op));
3168
0
            oplines[i].opcode = ZEND_NOP;
3169
0
            oplines[i].extended_value = opline - CG(active_op_array)->opcodes;
3170
0
          }
3171
0
        }
3172
0
      }
3173
0
      zend_emit_jmp_null(&obj_node, type);
3174
0
    }
3175
0
  }
3176
3177
0
  zend_compile_expr(&prop_node, prop_ast);
3178
3179
0
  opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
3180
0
  if (opline->op2_type == IS_CONST) {
3181
0
    convert_to_string(CT_CONSTANT(opline->op2));
3182
0
    zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2)));
3183
0
    opline->extended_value = zend_alloc_cache_slots(3);
3184
0
  }
3185
3186
0
  zend_adjust_for_fetch_type(opline, result, type);
3187
3188
0
  return opline;
3189
0
}
3190
/* }}} */
3191
3192
static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
3193
0
{
3194
0
  uint32_t offset = zend_delayed_compile_begin();
3195
0
  zend_op *opline = zend_delayed_compile_prop(result, ast, type);
3196
0
  if (by_ref) { /* shared with cache_slot */
3197
0
    opline->extended_value |= ZEND_FETCH_REF;
3198
0
  }
3199
0
  return zend_delayed_compile_end(offset);
3200
0
}
3201
/* }}} */
3202
3203
static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */
3204
0
{
3205
0
  zend_ast *class_ast = ast->child[0];
3206
0
  zend_ast *prop_ast = ast->child[1];
3207
3208
0
  znode class_node, prop_node;
3209
0
  zend_op *opline;
3210
3211
0
  zend_short_circuiting_mark_inner(class_ast);
3212
0
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3213
3214
0
  zend_compile_expr(&prop_node, prop_ast);
3215
3216
0
  if (delayed) {
3217
0
    opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3218
0
  } else {
3219
0
    opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
3220
0
  }
3221
0
  if (opline->op1_type == IS_CONST) {
3222
0
    convert_to_string(CT_CONSTANT(opline->op1));
3223
0
    opline->extended_value = zend_alloc_cache_slots(3);
3224
0
  }
3225
0
  if (class_node.op_type == IS_CONST) {
3226
0
    opline->op2_type = IS_CONST;
3227
0
    opline->op2.constant = zend_add_class_name_literal(
3228
0
      Z_STR(class_node.u.constant));
3229
0
    if (opline->op1_type != IS_CONST) {
3230
0
      opline->extended_value = zend_alloc_cache_slot();
3231
0
    }
3232
0
  } else {
3233
0
    SET_NODE(opline->op2, &class_node);
3234
0
  }
3235
3236
0
  if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */
3237
0
    opline->extended_value |= ZEND_FETCH_REF;
3238
0
  }
3239
3240
0
  zend_adjust_for_fetch_type(opline, result, type);
3241
0
  return opline;
3242
0
}
3243
/* }}} */
3244
3245
0
static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ {
3246
0
  if (var_ast->kind == ZEND_AST_ARRAY) {
3247
0
    if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
3248
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
3249
0
    }
3250
0
    if (array_style != var_ast->attr) {
3251
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
3252
0
    }
3253
0
  } else if (!zend_can_write_to_variable(var_ast)) {
3254
0
    zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
3255
0
  }
3256
0
}
3257
/* }}} */
3258
3259
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node);
3260
3261
/* Propagate refs used on leaf elements to the surrounding list() structures. */
3262
0
static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */
3263
0
  const zend_ast_list *list = zend_ast_get_list(ast);
3264
0
  bool has_refs = false;
3265
0
  uint32_t i;
3266
3267
0
  for (i = 0; i < list->children; ++i) {
3268
0
    zend_ast *elem_ast = list->child[i];
3269
3270
0
    if (elem_ast) {
3271
0
      zend_ast *var_ast = elem_ast->child[0];
3272
0
      if (var_ast->kind == ZEND_AST_ARRAY) {
3273
0
        elem_ast->attr = zend_propagate_list_refs(var_ast);
3274
0
      }
3275
0
      has_refs |= elem_ast->attr;
3276
0
    }
3277
0
  }
3278
3279
0
  return has_refs;
3280
0
}
3281
/* }}} */
3282
3283
static bool list_is_keyed(const zend_ast_list *list)
3284
0
{
3285
0
  for (uint32_t i = 0; i < list->children; i++) {
3286
0
    const zend_ast *child = list->child[i];
3287
0
    if (child) {
3288
0
      return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL;
3289
0
    }
3290
0
  }
3291
0
  return false;
3292
0
}
3293
3294
static void zend_compile_list_assign(
3295
    znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style) /* {{{ */
3296
0
{
3297
0
  zend_ast_list *list = zend_ast_get_list(ast);
3298
0
  uint32_t i;
3299
0
  bool has_elems = false;
3300
0
  bool is_keyed = list_is_keyed(list);
3301
3302
0
  if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) {
3303
0
    zval_make_interned_string(&expr_node->u.constant);
3304
0
  }
3305
3306
0
  for (i = 0; i < list->children; ++i) {
3307
0
    zend_ast *elem_ast = list->child[i];
3308
0
    zend_ast *var_ast, *key_ast;
3309
0
    znode fetch_result, dim_node;
3310
0
    zend_op *opline;
3311
3312
0
    if (elem_ast == NULL) {
3313
0
      if (is_keyed) {
3314
0
        zend_error(E_COMPILE_ERROR,
3315
0
          "Cannot use empty array entries in keyed array assignment");
3316
0
      } else {
3317
0
        continue;
3318
0
      }
3319
0
    }
3320
3321
0
    if (elem_ast->kind == ZEND_AST_UNPACK) {
3322
0
      zend_error(E_COMPILE_ERROR,
3323
0
          "Spread operator is not supported in assignments");
3324
0
    }
3325
3326
0
    var_ast = elem_ast->child[0];
3327
0
    key_ast = elem_ast->child[1];
3328
0
    has_elems = true;
3329
3330
0
    if (is_keyed) {
3331
0
      if (key_ast == NULL) {
3332
0
        zend_error(E_COMPILE_ERROR,
3333
0
          "Cannot mix keyed and unkeyed array entries in assignments");
3334
0
      }
3335
3336
0
      zend_compile_expr(&dim_node, key_ast);
3337
0
    } else {
3338
0
      if (key_ast != NULL) {
3339
0
        zend_error(E_COMPILE_ERROR,
3340
0
          "Cannot mix keyed and unkeyed array entries in assignments");
3341
0
      }
3342
3343
0
      dim_node.op_type = IS_CONST;
3344
0
      ZVAL_LONG(&dim_node.u.constant, i);
3345
0
    }
3346
3347
0
    if (expr_node->op_type == IS_CONST) {
3348
0
      Z_TRY_ADDREF(expr_node->u.constant);
3349
0
    }
3350
3351
0
    zend_verify_list_assign_target(var_ast, array_style);
3352
3353
0
    opline = zend_emit_op(&fetch_result,
3354
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);
3355
3356
0
    if (dim_node.op_type == IS_CONST) {
3357
0
      zend_handle_numeric_dim(opline, &dim_node);
3358
0
    }
3359
3360
0
    if (elem_ast->attr) {
3361
0
      zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL);
3362
0
    }
3363
0
    if (var_ast->kind == ZEND_AST_ARRAY) {
3364
0
      zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr);
3365
0
    } else if (elem_ast->attr) {
3366
0
      zend_emit_assign_ref_znode(var_ast, &fetch_result);
3367
0
    } else {
3368
0
      zend_emit_assign_znode(var_ast, &fetch_result);
3369
0
    }
3370
0
  }
3371
3372
0
  if (has_elems == 0) {
3373
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
3374
0
  }
3375
3376
0
  if (result) {
3377
0
    *result = *expr_node;
3378
0
  } else {
3379
0
    zend_do_free(expr_node);
3380
0
  }
3381
0
}
3382
/* }}} */
3383
3384
static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
3385
0
{
3386
0
  if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) {
3387
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
3388
0
  }
3389
0
  if (
3390
0
    ast->kind == ZEND_AST_METHOD_CALL
3391
0
    || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL
3392
0
    || ast->kind == ZEND_AST_STATIC_CALL
3393
0
  ) {
3394
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
3395
0
  }
3396
0
  if (zend_ast_is_short_circuited(ast)) {
3397
0
    zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context");
3398
0
  }
3399
0
  if (is_globals_fetch(ast)) {
3400
0
    zend_error_noreturn(E_COMPILE_ERROR,
3401
0
      "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax");
3402
0
  }
3403
0
}
3404
/* }}} */
3405
3406
/* Detects $a... = $a pattern */
3407
static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */
3408
0
{
3409
0
  if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
3410
0
    return 0;
3411
0
  }
3412
3413
0
  while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
3414
0
    var_ast = var_ast->child[0];
3415
0
  }
3416
3417
0
  if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
3418
0
    return 0;
3419
0
  }
3420
3421
0
  {
3422
0
    zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
3423
0
    zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
3424
0
    bool result = zend_string_equals(name1, name2);
3425
0
    zend_string_release_ex(name1, 0);
3426
0
    zend_string_release_ex(name2, 0);
3427
0
    return result;
3428
0
  }
3429
0
}
3430
/* }}} */
3431
3432
static void zend_compile_expr_with_potential_assign_to_self(
3433
0
    znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) {
3434
0
  if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) {
3435
    /* $a[0] = $a should evaluate the right $a first */
3436
0
    znode cv_node;
3437
3438
0
    if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3439
0
      zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false);
3440
0
    } else {
3441
0
      zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3442
0
    }
3443
0
  } else {
3444
0
    zend_compile_expr(expr_node, expr_ast);
3445
0
  }
3446
0
}
3447
3448
static void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
3449
0
{
3450
0
  zend_ast *var_ast = ast->child[0];
3451
0
  zend_ast *expr_ast = ast->child[1];
3452
3453
0
  znode var_node, expr_node;
3454
0
  zend_op *opline;
3455
0
  uint32_t offset;
3456
0
  if (is_this_fetch(var_ast)) {
3457
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3458
0
  }
3459
3460
0
  zend_ensure_writable_variable(var_ast);
3461
3462
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3463
0
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3464
0
  switch (kind) {
3465
0
    case ZEND_AST_VAR:
3466
0
      offset = zend_delayed_compile_begin();
3467
0
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false);
3468
0
      zend_compile_expr(&expr_node, expr_ast);
3469
0
      zend_delayed_compile_end(offset);
3470
0
      CG(zend_lineno) = zend_ast_get_lineno(var_ast);
3471
0
      zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node);
3472
0
      return;
3473
0
    case ZEND_AST_STATIC_PROP:
3474
0
      offset = zend_delayed_compile_begin();
3475
0
      zend_delayed_compile_var(result, var_ast, BP_VAR_W, false);
3476
0
      zend_compile_expr(&expr_node, expr_ast);
3477
3478
0
      opline = zend_delayed_compile_end(offset);
3479
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
3480
0
      opline->result_type = IS_TMP_VAR;
3481
0
      result->op_type = IS_TMP_VAR;
3482
3483
0
      zend_emit_op_data(&expr_node);
3484
0
      return;
3485
0
    case ZEND_AST_DIM:
3486
0
      offset = zend_delayed_compile_begin();
3487
0
      zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false);
3488
0
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3489
3490
0
      opline = zend_delayed_compile_end(offset);
3491
0
      opline->opcode = ZEND_ASSIGN_DIM;
3492
0
      opline->result_type = IS_TMP_VAR;
3493
0
      result->op_type = IS_TMP_VAR;
3494
3495
0
      opline = zend_emit_op_data(&expr_node);
3496
0
      return;
3497
0
    case ZEND_AST_PROP:
3498
0
    case ZEND_AST_NULLSAFE_PROP:
3499
0
      offset = zend_delayed_compile_begin();
3500
0
      zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3501
0
      zend_compile_expr(&expr_node, expr_ast);
3502
3503
0
      opline = zend_delayed_compile_end(offset);
3504
0
      opline->opcode = ZEND_ASSIGN_OBJ;
3505
0
      opline->result_type = IS_TMP_VAR;
3506
0
      result->op_type = IS_TMP_VAR;
3507
3508
0
      zend_emit_op_data(&expr_node);
3509
0
      return;
3510
0
    case ZEND_AST_ARRAY:
3511
0
      if (zend_propagate_list_refs(var_ast)) {
3512
0
        if (!zend_is_variable_or_call(expr_ast)) {
3513
0
          zend_error_noreturn(E_COMPILE_ERROR,
3514
0
            "Cannot assign reference to non referenceable value");
3515
0
        } else {
3516
0
          zend_assert_not_short_circuited(expr_ast);
3517
0
        }
3518
3519
0
        zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
3520
        /* MAKE_REF is usually not necessary for CVs. However, if there are
3521
         * self-assignments, this forces the RHS to evaluate first. */
3522
0
        zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
3523
0
      } else {
3524
0
        if (expr_ast->kind == ZEND_AST_VAR) {
3525
          /* list($a, $b) = $a should evaluate the right $a first */
3526
0
          znode cv_node;
3527
3528
0
          if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) {
3529
0
            zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false);
3530
0
          } else {
3531
0
            zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL);
3532
0
          }
3533
0
        } else {
3534
0
          zend_compile_expr(&expr_node, expr_ast);
3535
0
        }
3536
0
      }
3537
3538
0
      zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3539
0
      return;
3540
0
    EMPTY_SWITCH_DEFAULT_CASE();
3541
0
  }
3542
0
}
3543
/* }}} */
3544
3545
static void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3546
0
{
3547
0
  zend_ast *target_ast = ast->child[0];
3548
0
  zend_ast *source_ast = ast->child[1];
3549
3550
0
  znode target_node, source_node;
3551
0
  zend_op *opline;
3552
0
  uint32_t offset, flags;
3553
3554
0
  if (is_this_fetch(target_ast)) {
3555
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3556
0
  }
3557
0
  zend_ensure_writable_variable(target_ast);
3558
0
  zend_assert_not_short_circuited(source_ast);
3559
0
  if (is_globals_fetch(source_ast)) {
3560
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS");
3561
0
  }
3562
3563
0
  offset = zend_delayed_compile_begin();
3564
0
  zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true);
3565
0
  zend_compile_var(&source_node, source_ast, BP_VAR_W, true);
3566
3567
0
  if ((target_ast->kind != ZEND_AST_VAR
3568
0
    || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3569
0
   && source_ast->kind != ZEND_AST_ZNODE
3570
0
   && source_node.op_type != IS_CV) {
3571
    /* Both LHS and RHS expressions may modify the same data structure,
3572
     * and the modification during RHS evaluation may dangle the pointer
3573
     * to the result of the LHS evaluation.
3574
     * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3575
     * See: Bug #71539
3576
     */
3577
0
    zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3578
0
  }
3579
3580
0
  opline = zend_delayed_compile_end(offset);
3581
3582
0
  if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3583
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3584
0
  }
3585
3586
0
  flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0;
3587
3588
0
  if (opline && opline->opcode == ZEND_FETCH_OBJ_W) {
3589
0
    opline->opcode = ZEND_ASSIGN_OBJ_REF;
3590
0
    opline->extended_value &= ~ZEND_FETCH_REF;
3591
0
    opline->extended_value |= flags;
3592
0
    zend_emit_op_data(&source_node);
3593
0
    *result = target_node;
3594
0
  } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) {
3595
0
    opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF;
3596
0
    opline->extended_value &= ~ZEND_FETCH_REF;
3597
0
    opline->extended_value |= flags;
3598
0
    zend_emit_op_data(&source_node);
3599
0
    *result = target_node;
3600
0
  } else {
3601
0
    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3602
0
    opline->extended_value = flags;
3603
0
  }
3604
0
}
3605
/* }}} */
3606
3607
static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */
3608
0
{
3609
0
  znode dummy_node;
3610
0
  zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3611
0
    zend_ast_create_znode(value_node));
3612
0
  zend_compile_expr(&dummy_node, assign_ast);
3613
0
  zend_do_free(&dummy_node);
3614
0
}
3615
/* }}} */
3616
3617
static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3618
0
{
3619
0
  zend_ast *var_ast = ast->child[0];
3620
0
  zend_ast *expr_ast = ast->child[1];
3621
0
  uint32_t opcode = ast->attr;
3622
3623
0
  znode var_node, expr_node;
3624
0
  zend_op *opline;
3625
0
  uint32_t offset, cache_slot;
3626
3627
0
  zend_ensure_writable_variable(var_ast);
3628
3629
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
3630
0
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
3631
0
  switch (kind) {
3632
0
    case ZEND_AST_VAR:
3633
0
      offset = zend_delayed_compile_begin();
3634
0
      zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false);
3635
0
      zend_compile_expr(&expr_node, expr_ast);
3636
0
      zend_delayed_compile_end(offset);
3637
0
      opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node);
3638
0
      opline->extended_value = opcode;
3639
0
      return;
3640
0
    case ZEND_AST_STATIC_PROP:
3641
0
      offset = zend_delayed_compile_begin();
3642
0
      zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false);
3643
0
      zend_compile_expr(&expr_node, expr_ast);
3644
3645
0
      opline = zend_delayed_compile_end(offset);
3646
0
      cache_slot = opline->extended_value;
3647
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP;
3648
0
      opline->extended_value = opcode;
3649
0
      opline->result_type = IS_TMP_VAR;
3650
0
      result->op_type = IS_TMP_VAR;
3651
3652
0
      opline = zend_emit_op_data(&expr_node);
3653
0
      opline->extended_value = cache_slot;
3654
0
      return;
3655
0
    case ZEND_AST_DIM:
3656
0
      offset = zend_delayed_compile_begin();
3657
0
      zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false);
3658
0
      zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast);
3659
3660
0
      opline = zend_delayed_compile_end(offset);
3661
0
      opline->opcode = ZEND_ASSIGN_DIM_OP;
3662
0
      opline->extended_value = opcode;
3663
0
      opline->result_type = IS_TMP_VAR;
3664
0
      result->op_type = IS_TMP_VAR;
3665
3666
0
      zend_emit_op_data(&expr_node);
3667
0
      return;
3668
0
    case ZEND_AST_PROP:
3669
0
    case ZEND_AST_NULLSAFE_PROP:
3670
0
      offset = zend_delayed_compile_begin();
3671
0
      zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3672
0
      zend_compile_expr(&expr_node, expr_ast);
3673
3674
0
      opline = zend_delayed_compile_end(offset);
3675
0
      cache_slot = opline->extended_value;
3676
0
      opline->opcode = ZEND_ASSIGN_OBJ_OP;
3677
0
      opline->extended_value = opcode;
3678
0
      opline->result_type = IS_TMP_VAR;
3679
0
      result->op_type = IS_TMP_VAR;
3680
3681
0
      opline = zend_emit_op_data(&expr_node);
3682
0
      opline->extended_value = cache_slot;
3683
0
      return;
3684
0
    EMPTY_SWITCH_DEFAULT_CASE()
3685
0
  }
3686
0
}
3687
/* }}} */
3688
3689
0
static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) {
3690
  // TODO: Caching?
3691
0
  if (fn->type == ZEND_USER_FUNCTION) {
3692
0
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3693
0
      const zend_arg_info *arg_info = &fn->op_array.arg_info[i];
3694
0
      if (zend_string_equals(arg_info->name, arg_name)) {
3695
0
        return i + 1;
3696
0
      }
3697
0
    }
3698
0
  } else {
3699
0
    ZEND_ASSERT(fn->common.num_args == 0 || fn->internal_function.arg_info);
3700
0
    for (uint32_t i = 0; i < fn->common.num_args; i++) {
3701
0
      const zend_internal_arg_info *arg_info = &fn->internal_function.arg_info[i];
3702
0
      size_t len = strlen(arg_info->name);
3703
0
      if (zend_string_equals_cstr(arg_name, arg_info->name, len)) {
3704
0
        return i + 1;
3705
0
      }
3706
0
    }
3707
0
  }
3708
3709
  /* Either an invalid argument name, or collected into a variadic argument. */
3710
0
  return (uint32_t) -1;
3711
0
}
3712
3713
static uint32_t zend_compile_args(
3714
    zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */
3715
0
{
3716
0
  const zend_ast_list *args = zend_ast_get_list(ast);
3717
0
  uint32_t i;
3718
0
  bool uses_arg_unpack = false;
3719
0
  uint32_t arg_count = 0; /* number of arguments not including unpacks */
3720
3721
  /* Whether named arguments are used syntactically, to enforce language level limitations.
3722
   * May not actually use named argument passing. */
3723
0
  bool uses_named_args = false;
3724
  /* Whether there may be any undef arguments due to the use of named arguments. */
3725
0
  bool may_have_undef = false;
3726
  /* Whether there may be any extra named arguments collected into a variadic. */
3727
0
  *may_have_extra_named_args = false;
3728
3729
0
  for (i = 0; i < args->children; ++i) {
3730
0
    zend_ast *arg = args->child[i];
3731
0
    zend_string *arg_name = NULL;
3732
0
    uint32_t arg_num = i + 1;
3733
3734
0
    znode arg_node;
3735
0
    zend_op *opline;
3736
0
    uint8_t opcode;
3737
3738
0
    if (arg->kind == ZEND_AST_UNPACK) {
3739
0
      if (uses_named_args) {
3740
0
        zend_error_noreturn(E_COMPILE_ERROR,
3741
0
          "Cannot use argument unpacking after named arguments");
3742
0
      }
3743
3744
      /* Unpack may contain named arguments. */
3745
0
      may_have_undef = true;
3746
0
      if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3747
0
        *may_have_extra_named_args = true;
3748
0
      }
3749
3750
0
      uses_arg_unpack = true;
3751
0
      fbc = NULL;
3752
3753
0
      zend_compile_expr(&arg_node, arg->child[0]);
3754
0
      opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3755
0
      opline->op2.num = arg_count;
3756
0
      opline->result.var = EX_NUM_TO_VAR(arg_count - 1);
3757
3758
0
      continue;
3759
0
    }
3760
3761
0
    if (arg->kind == ZEND_AST_NAMED_ARG) {
3762
0
      uses_named_args = true;
3763
0
      arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0]));
3764
0
      arg = arg->child[1];
3765
3766
0
      if (fbc && !uses_arg_unpack) {
3767
0
        arg_num = zend_get_arg_num(fbc, arg_name);
3768
0
        if (arg_num == arg_count + 1 && !may_have_undef) {
3769
          /* Using named arguments, but passing in order. */
3770
0
          arg_name = NULL;
3771
0
          arg_count++;
3772
0
        } else {
3773
          // TODO: We could track which arguments were passed, even if out of order.
3774
0
          may_have_undef = true;
3775
0
          if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
3776
0
            *may_have_extra_named_args = true;
3777
0
          }
3778
0
        }
3779
0
      } else {
3780
0
        arg_num = (uint32_t) -1;
3781
0
        may_have_undef = true;
3782
0
        *may_have_extra_named_args = true;
3783
0
      }
3784
0
    } else {
3785
0
      if (uses_arg_unpack) {
3786
0
        zend_error_noreturn(E_COMPILE_ERROR,
3787
0
          "Cannot use positional argument after argument unpacking");
3788
0
      }
3789
3790
0
      if (uses_named_args) {
3791
0
        zend_error_noreturn(E_COMPILE_ERROR,
3792
0
          "Cannot use positional argument after named argument");
3793
0
      }
3794
3795
0
      arg_count++;
3796
0
    }
3797
3798
    /* Treat passing of $GLOBALS the same as passing a call.
3799
     * This will error at runtime if the argument is by-ref. */
3800
0
    if (zend_is_call(arg) || is_globals_fetch(arg)) {
3801
0
      zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3802
0
      if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3803
        /* Function call was converted into builtin instruction */
3804
0
        if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3805
0
          opcode = ZEND_SEND_VAL_EX;
3806
0
        } else {
3807
0
          opcode = ZEND_SEND_VAL;
3808
0
        }
3809
0
      } else {
3810
0
        if (fbc && arg_num != (uint32_t) -1) {
3811
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3812
0
            opcode = ZEND_SEND_VAR_NO_REF;
3813
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3814
            /* For IS_VAR operands, SEND_VAL will pass through the operand without
3815
             * dereferencing, so it will use a by-ref pass if the call returned by-ref
3816
             * and a by-value pass if it returned by-value. */
3817
0
            opcode = ZEND_SEND_VAL;
3818
0
          } else {
3819
0
            opcode = ZEND_SEND_VAR;
3820
0
          }
3821
0
        } else {
3822
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3823
0
        }
3824
0
      }
3825
0
    } else if (zend_is_variable(arg) && !zend_ast_is_short_circuited(arg)) {
3826
0
      if (fbc && arg_num != (uint32_t) -1) {
3827
0
        if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3828
0
          zend_compile_var(&arg_node, arg, BP_VAR_W, true);
3829
0
          opcode = ZEND_SEND_REF;
3830
0
        } else {
3831
0
          zend_compile_var(&arg_node, arg, BP_VAR_R, false);
3832
0
          opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR;
3833
0
        }
3834
0
      } else {
3835
0
        do {
3836
0
          if (arg->kind == ZEND_AST_VAR) {
3837
0
            CG(zend_lineno) = zend_ast_get_lineno(ast);
3838
0
            if (is_this_fetch(arg)) {
3839
0
              zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL);
3840
0
              opcode = ZEND_SEND_VAR_EX;
3841
0
              CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
3842
0
              break;
3843
0
            } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) {
3844
0
              opcode = ZEND_SEND_VAR_EX;
3845
0
              break;
3846
0
            }
3847
0
          }
3848
0
          opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL);
3849
0
          if (arg_name) {
3850
0
            opline->op2_type = IS_CONST;
3851
0
            zend_string_addref(arg_name);
3852
0
            opline->op2.constant = zend_add_literal_string(&arg_name);
3853
0
            opline->result.num = zend_alloc_cache_slots(2);
3854
0
          } else {
3855
0
            opline->op2.num = arg_num;
3856
0
          }
3857
0
          zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true);
3858
0
          opcode = ZEND_SEND_FUNC_ARG;
3859
0
        } while (0);
3860
0
      }
3861
0
    } else {
3862
0
      zend_compile_expr(&arg_node, arg);
3863
0
      if (arg_node.op_type == IS_VAR) {
3864
        /* pass ++$a or something similar */
3865
0
        if (fbc && arg_num != (uint32_t) -1) {
3866
0
          if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3867
0
            opcode = ZEND_SEND_VAR_NO_REF;
3868
0
          } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3869
0
            opcode = ZEND_SEND_VAL;
3870
0
          } else {
3871
0
            opcode = ZEND_SEND_VAR;
3872
0
          }
3873
0
        } else {
3874
0
          opcode = ZEND_SEND_VAR_NO_REF_EX;
3875
0
        }
3876
0
      } else if (arg_node.op_type == IS_CV) {
3877
0
        if (fbc && arg_num != (uint32_t) -1) {
3878
0
          if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3879
0
            opcode = ZEND_SEND_REF;
3880
0
          } else {
3881
0
            opcode = ZEND_SEND_VAR;
3882
0
          }
3883
0
        } else {
3884
0
          opcode = ZEND_SEND_VAR_EX;
3885
0
        }
3886
0
      } else {
3887
        /* Delay "Only variables can be passed by reference" error to execution */
3888
0
        if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3889
0
          opcode = ZEND_SEND_VAL;
3890
0
        } else {
3891
0
          opcode = ZEND_SEND_VAL_EX;
3892
0
        }
3893
0
      }
3894
0
    }
3895
3896
0
    opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3897
0
    if (arg_name) {
3898
0
      opline->op2_type = IS_CONST;
3899
0
      zend_string_addref(arg_name);
3900
0
      opline->op2.constant = zend_add_literal_string(&arg_name);
3901
0
      opline->result.num = zend_alloc_cache_slots(2);
3902
0
    } else {
3903
0
      opline->op2.opline_num = arg_num;
3904
0
      opline->result.var = EX_NUM_TO_VAR(arg_num - 1);
3905
0
    }
3906
0
  }
3907
3908
0
  if (may_have_undef) {
3909
0
    zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
3910
0
  }
3911
3912
0
  return arg_count;
3913
0
}
3914
/* }}} */
3915
3916
ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */
3917
0
{
3918
0
  uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD;
3919
3920
0
  if (fbc && init_op->opcode != ZEND_NEW) {
3921
0
    ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE));
3922
0
    if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) {
3923
0
      if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3924
0
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3925
0
          return ZEND_DO_ICALL;
3926
0
        } else {
3927
0
          return ZEND_DO_FCALL_BY_NAME;
3928
0
        }
3929
0
      }
3930
0
    } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){
3931
0
      if (zend_execute_ex == execute_ex) {
3932
0
        if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) {
3933
0
          return ZEND_DO_UCALL;
3934
0
        } else {
3935
0
          return ZEND_DO_FCALL_BY_NAME;
3936
0
        }
3937
0
      }
3938
0
    }
3939
0
  } else if (zend_execute_ex == execute_ex &&
3940
0
             !zend_execute_internal &&
3941
0
             (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3942
0
              init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3943
0
    return ZEND_DO_FCALL_BY_NAME;
3944
0
  }
3945
0
  return ZEND_DO_FCALL;
3946
0
}
3947
/* }}} */
3948
3949
static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno) /* {{{ */
3950
0
{
3951
0
  zend_op *opline;
3952
0
  uint32_t opnum_init = get_next_op_number() - 1;
3953
3954
0
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
3955
0
    opline = &CG(active_op_array)->opcodes[opnum_init];
3956
0
    opline->extended_value = 0;
3957
    /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */
3958
0
    uint8_t opcode = opline->opcode;
3959
3960
0
    if (opcode == ZEND_NEW) {
3961
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
3962
0
    }
3963
3964
0
    if (opcode == ZEND_INIT_FCALL) {
3965
0
      opline->op1.num = zend_vm_calc_used_stack(0, fbc);
3966
0
    }
3967
3968
0
    zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL);
3969
0
    if (opcode == ZEND_INIT_FCALL
3970
0
     || opcode == ZEND_INIT_FCALL_BY_NAME
3971
0
     || opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
3972
0
      callable_convert_op->extended_value = zend_alloc_cache_slot();
3973
0
    } else {
3974
0
      callable_convert_op->extended_value = (uint32_t)-1;
3975
0
    }
3976
0
    return true;
3977
0
  }
3978
3979
0
  bool may_have_extra_named_args;
3980
0
  uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args);
3981
3982
0
  zend_do_extended_fcall_begin();
3983
3984
0
  opline = &CG(active_op_array)->opcodes[opnum_init];
3985
0
  opline->extended_value = arg_count;
3986
3987
0
  if (opline->opcode == ZEND_INIT_FCALL) {
3988
0
    opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3989
0
  }
3990
3991
0
  uint8_t call_op = zend_get_call_op(
3992
0
    opline,
3993
0
    fbc,
3994
    /* result_used: At this point we do not yet reliably
3995
     * know if the result is used. Deoptimize #[\NoDiscard]
3996
     * calls to be sure. The optimizer will fix this up.
3997
     */
3998
0
    false
3999
0
  );
4000
0
  opline = zend_emit_op(result, call_op, NULL, NULL);
4001
0
  if (may_have_extra_named_args) {
4002
0
    opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4003
0
  }
4004
0
  opline->lineno = lineno;
4005
0
  zend_do_extended_fcall_end();
4006
0
  return false;
4007
0
}
4008
/* }}} */
4009
4010
static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
4011
0
{
4012
0
  zend_string *orig_name = zend_ast_get_str(name_ast);
4013
0
  bool is_fully_qualified;
4014
4015
0
  name_node->op_type = IS_CONST;
4016
0
  ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
4017
0
    orig_name, name_ast->attr, &is_fully_qualified));
4018
4019
0
  return !is_fully_qualified && FC(current_namespace);
4020
0
}
4021
/* }}} */
4022
4023
static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno) /* {{{ */
4024
0
{
4025
0
  if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
4026
0
    const char *colon;
4027
0
    zend_string *str = Z_STR(name_node->u.constant);
4028
0
    if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
4029
0
      zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
4030
0
      zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
4031
0
      zend_op *opline = get_next_op();
4032
4033
0
      opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
4034
0
      opline->op1_type = IS_CONST;
4035
0
      opline->op1.constant = zend_add_class_name_literal(class);
4036
0
      opline->op2_type = IS_CONST;
4037
0
      opline->op2.constant = zend_add_func_name_literal(method);
4038
      /* 2 slots, for class and method */
4039
0
      opline->result.num = zend_alloc_cache_slots(2);
4040
0
      zval_ptr_dtor(&name_node->u.constant);
4041
0
    } else {
4042
0
      zend_op *opline = get_next_op();
4043
4044
0
      opline->opcode = ZEND_INIT_FCALL_BY_NAME;
4045
0
      opline->op2_type = IS_CONST;
4046
0
      opline->op2.constant = zend_add_func_name_literal(str);
4047
0
      opline->result.num = zend_alloc_cache_slot();
4048
0
    }
4049
0
  } else {
4050
0
    zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
4051
0
  }
4052
4053
0
  zend_compile_call_common(result, args_ast, NULL, lineno);
4054
0
}
4055
/* }}} */
4056
4057
static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */
4058
0
{
4059
0
  uint32_t i;
4060
0
  for (i = 0; i < args->children; ++i) {
4061
0
    const zend_ast *arg = args->child[i];
4062
0
    if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) {
4063
0
      return 1;
4064
0
    }
4065
0
  }
4066
0
  return 0;
4067
0
}
4068
/* }}} */
4069
4070
static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */
4071
0
{
4072
0
  znode arg_node;
4073
4074
0
  if (args->children != 1) {
4075
0
    return FAILURE;
4076
0
  }
4077
4078
0
  zend_compile_expr(&arg_node, args->child[0]);
4079
0
  if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
4080
0
    result->op_type = IS_CONST;
4081
0
    ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
4082
0
    zval_ptr_dtor_str(&arg_node.u.constant);
4083
0
  } else {
4084
0
    zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
4085
0
  }
4086
0
  return SUCCESS;
4087
0
}
4088
/* }}} */
4089
4090
static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4091
0
{
4092
0
  znode arg_node;
4093
0
  zend_op *opline;
4094
4095
0
  if (args->children != 1) {
4096
0
    return FAILURE;
4097
0
  }
4098
4099
0
  zend_compile_expr(&arg_node, args->child[0]);
4100
0
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4101
0
  if (type != _IS_BOOL) {
4102
0
    opline->extended_value = (1 << type);
4103
0
  } else {
4104
0
    opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE);
4105
0
  }
4106
0
  return SUCCESS;
4107
0
}
4108
/* }}} */
4109
4110
static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */
4111
0
{
4112
0
  znode arg_node;
4113
0
  zend_op *opline;
4114
4115
0
  if (args->children != 1) {
4116
0
    return FAILURE;
4117
0
  }
4118
4119
0
  zend_compile_expr(&arg_node, args->child[0]);
4120
0
  opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
4121
0
  opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING);
4122
0
  return SUCCESS;
4123
0
}
4124
4125
static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */
4126
0
{
4127
0
  znode arg_node;
4128
0
  zend_op *opline;
4129
4130
0
  if (args->children != 1) {
4131
0
    return FAILURE;
4132
0
  }
4133
4134
0
  zend_compile_expr(&arg_node, args->child[0]);
4135
0
  if (type == _IS_BOOL) {
4136
0
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL);
4137
0
  } else {
4138
0
    opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
4139
0
    opline->extended_value = type;
4140
0
  }
4141
0
  return SUCCESS;
4142
0
}
4143
/* }}} */
4144
4145
static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */
4146
0
{
4147
0
  zend_string *name;
4148
0
  zend_op *opline;
4149
4150
0
  if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
4151
0
    return FAILURE;
4152
0
  }
4153
4154
0
  name = zval_get_string(zend_ast_get_zval(args->child[0]));
4155
0
  if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
4156
0
    zend_string_release_ex(name, 0);
4157
0
    return FAILURE;
4158
0
  }
4159
4160
0
  if (zend_try_ct_eval_const(&result->u.constant, name, false)) {
4161
0
    zend_string_release_ex(name, 0);
4162
0
    zval_ptr_dtor(&result->u.constant);
4163
0
    ZVAL_TRUE(&result->u.constant);
4164
0
    result->op_type = IS_CONST;
4165
0
    return SUCCESS;
4166
0
  }
4167
4168
0
  opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
4169
0
  opline->op1_type = IS_CONST;
4170
0
  LITERAL_STR(opline->op1, name);
4171
0
  opline->extended_value = zend_alloc_cache_slot();
4172
4173
0
  return SUCCESS;
4174
0
}
4175
/* }}} */
4176
4177
static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */
4178
0
{
4179
0
  zval *zint;
4180
0
  if (
4181
0
    args->children == 1
4182
0
    && args->child[0]->kind == ZEND_AST_ZVAL
4183
0
    && (zint = zend_ast_get_zval(args->child[0]))
4184
0
    && Z_TYPE_P(zint) == IS_LONG
4185
0
    && Z_LVAL_P(zint) >= 0
4186
0
    && Z_LVAL_P(zint) <= 255
4187
0
  ) {
4188
0
    result->op_type = IS_CONST;
4189
0
    ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint));
4190
0
    return SUCCESS;
4191
0
  } else {
4192
0
    return FAILURE;
4193
0
  }
4194
0
}
4195
/* }}} */
4196
4197
static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */
4198
0
{
4199
0
  zval *str;
4200
0
  if (
4201
0
    args->children == 1
4202
0
    && args->child[0]->kind == ZEND_AST_ZVAL
4203
0
    && (str = zend_ast_get_zval(args->child[0]))
4204
0
    && Z_TYPE_P(str) == IS_STRING
4205
0
    && Z_STRLEN_P(str) == 1
4206
0
  ) {
4207
0
    result->op_type = IS_CONST;
4208
0
    ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]);
4209
0
    return SUCCESS;
4210
0
  } else {
4211
0
    return FAILURE;
4212
0
  }
4213
0
}
4214
/* }}} */
4215
4216
/* We can only calculate the stack size for functions that have been fully compiled, otherwise
4217
 * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for
4218
 * directly or indirectly recursive function calls. */
4219
0
static bool fbc_is_finalized(const zend_function *fbc) {
4220
0
  return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO);
4221
0
}
4222
4223
static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename)
4224
0
{
4225
0
  if (ce->type == ZEND_INTERNAL_CLASS) {
4226
0
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES;
4227
0
  } else {
4228
0
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4229
0
      && ce->info.user.filename != filename;
4230
0
  }
4231
0
}
4232
4233
static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename)
4234
0
{
4235
0
  if (fbc->type == ZEND_INTERNAL_FUNCTION) {
4236
0
    return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS;
4237
0
  } else {
4238
0
    return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)
4239
0
      || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
4240
0
        && fbc->op_array.filename != filename);
4241
0
  }
4242
0
}
4243
4244
static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
4245
0
{
4246
0
  zend_string *name, *lcname;
4247
0
  zend_function *fbc;
4248
0
  zend_op *opline;
4249
4250
0
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
4251
0
    return FAILURE;
4252
0
  }
4253
4254
0
  name = zend_ast_get_str(name_ast);
4255
0
  lcname = zend_string_tolower(name);
4256
4257
0
  fbc = zend_hash_find_ptr(CG(function_table), lcname);
4258
0
  if (!fbc
4259
0
   || !fbc_is_finalized(fbc)
4260
0
   || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
4261
0
    zend_string_release_ex(lcname, 0);
4262
0
    return FAILURE;
4263
0
  }
4264
4265
0
  opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
4266
0
  opline->extended_value = num_args;
4267
0
  opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
4268
0
  opline->op2_type = IS_CONST;
4269
0
  LITERAL_STR(opline->op2, lcname);
4270
0
  opline->result.num = zend_alloc_cache_slot();
4271
4272
0
  return SUCCESS;
4273
0
}
4274
/* }}} */
4275
4276
static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
4277
0
{
4278
0
  zend_op *opline;
4279
0
  znode name_node;
4280
4281
0
  if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
4282
0
    return;
4283
0
  }
4284
4285
0
  zend_compile_expr(&name_node, name_ast);
4286
4287
0
  opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
4288
0
  opline->op1_type = IS_CONST;
4289
0
  LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
4290
0
  opline->extended_value = num_args;
4291
0
}
4292
/* }}} */
4293
4294
/* cufa = call_user_func_array */
4295
static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
4296
0
{
4297
0
  znode arg_node;
4298
0
  zend_op *opline;
4299
4300
0
  if (args->children != 2) {
4301
0
    return FAILURE;
4302
0
  }
4303
4304
0
  zend_compile_init_user_func(args->child[0], 0, lcname);
4305
0
  if (args->child[1]->kind == ZEND_AST_CALL
4306
0
   && args->child[1]->child[0]->kind == ZEND_AST_ZVAL
4307
0
   && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING
4308
0
   && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) {
4309
0
    zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]);
4310
0
    zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]);
4311
0
    bool is_fully_qualified;
4312
0
    zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified);
4313
4314
0
    if (zend_string_equals_literal_ci(name, "array_slice")
4315
0
       && !zend_args_contain_unpack_or_named(list)
4316
0
     && list->children == 3
4317
0
     && list->child[1]->kind == ZEND_AST_ZVAL) {
4318
0
      zval *zv = zend_ast_get_zval(list->child[1]);
4319
4320
0
      if (Z_TYPE_P(zv) == IS_LONG
4321
0
       && Z_LVAL_P(zv) >= 0
4322
0
       && Z_LVAL_P(zv) <= 0x7fffffff) {
4323
0
        zend_op *opline;
4324
0
        znode len_node;
4325
4326
0
        zend_compile_expr(&arg_node, list->child[0]);
4327
0
        zend_compile_expr(&len_node, list->child[2]);
4328
0
        opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node);
4329
0
        opline->extended_value = Z_LVAL_P(zv);
4330
0
        zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4331
0
        zend_string_release_ex(name, 0);
4332
0
        return SUCCESS;
4333
0
      }
4334
0
    }
4335
0
    zend_string_release_ex(name, 0);
4336
0
  }
4337
0
  zend_compile_expr(&arg_node, args->child[1]);
4338
0
  zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
4339
0
  zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL);
4340
0
  opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4341
0
  opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS;
4342
4343
0
  return SUCCESS;
4344
0
}
4345
/* }}} */
4346
4347
/* cuf = call_user_func */
4348
static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname) /* {{{ */
4349
0
{
4350
0
  uint32_t i;
4351
4352
0
  if (args->children < 1) {
4353
0
    return FAILURE;
4354
0
  }
4355
4356
0
  zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
4357
0
  for (i = 1; i < args->children; ++i) {
4358
0
    zend_ast *arg_ast = args->child[i];
4359
0
    znode arg_node;
4360
0
    zend_op *opline;
4361
4362
0
    zend_compile_expr(&arg_node, arg_ast);
4363
4364
0
    opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
4365
0
    opline->op2.num = i;
4366
0
    opline->result.var = EX_NUM_TO_VAR(i - 1);
4367
0
  }
4368
0
  zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
4369
4370
0
  return SUCCESS;
4371
0
}
4372
/* }}} */
4373
4374
static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno) /* {{{ */
4375
0
{
4376
0
  if (EG(assertions) >= 0) {
4377
0
    znode name_node;
4378
0
    zend_op *opline;
4379
0
    uint32_t check_op_number = get_next_op_number();
4380
4381
0
    zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
4382
4383
0
    if (fbc && fbc_is_finalized(fbc)) {
4384
0
      name_node.op_type = IS_CONST;
4385
0
      ZVAL_STR_COPY(&name_node.u.constant, name);
4386
4387
0
      opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
4388
0
    } else {
4389
0
      opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
4390
0
      opline->op2_type = IS_CONST;
4391
0
      opline->op2.constant = zend_add_ns_func_name_literal(name);
4392
0
    }
4393
0
    opline->result.num = zend_alloc_cache_slot();
4394
4395
0
    if (args->children == 1) {
4396
      /* add "assert(condition) as assertion message */
4397
0
      zend_ast *arg = zend_ast_create_zval_from_str(
4398
0
        zend_ast_export("assert(", args->child[0], ")"));
4399
0
      if (args->child[0]->kind == ZEND_AST_NAMED_ARG) {
4400
        /* If the original argument was named, add the new argument as named as well,
4401
         * as mixing named and positional is not allowed. */
4402
0
        zend_ast *name = zend_ast_create_zval_from_str(
4403
0
          ZSTR_INIT_LITERAL("description", 0));
4404
0
        arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg);
4405
0
      }
4406
0
      args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg);
4407
0
    }
4408
4409
0
    zend_compile_call_common(result, (zend_ast*)args, fbc, lineno);
4410
4411
0
    opline = &CG(active_op_array)->opcodes[check_op_number];
4412
0
    opline->op2.opline_num = get_next_op_number();
4413
0
    SET_NODE(opline->result, result);
4414
0
  } else {
4415
0
    if (!fbc) {
4416
0
      zend_string_release_ex(name, 0);
4417
0
    }
4418
0
    result->op_type = IS_CONST;
4419
0
    ZVAL_TRUE(&result->u.constant);
4420
0
  }
4421
0
}
4422
/* }}} */
4423
4424
static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */
4425
0
{
4426
0
  bool strict = false;
4427
0
  znode array, needly;
4428
0
  zend_op *opline;
4429
4430
0
  if (args->children == 3) {
4431
0
    if (args->child[2]->kind == ZEND_AST_ZVAL) {
4432
0
      strict = zend_is_true(zend_ast_get_zval(args->child[2]));
4433
0
    } else if (args->child[2]->kind == ZEND_AST_CONST) {
4434
0
      zval value;
4435
0
      zend_ast *name_ast = args->child[2]->child[0];
4436
0
      bool is_fully_qualified;
4437
0
      zend_string *resolved_name = zend_resolve_const_name(
4438
0
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
4439
4440
0
      if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) {
4441
0
        zend_string_release_ex(resolved_name, 0);
4442
0
        return FAILURE;
4443
0
      }
4444
4445
0
      zend_string_release_ex(resolved_name, 0);
4446
0
      strict = zend_is_true(&value);
4447
0
      zval_ptr_dtor(&value);
4448
0
    } else {
4449
0
      return FAILURE;
4450
0
    }
4451
0
  } else if (args->children != 2) {
4452
0
    return FAILURE;
4453
0
  }
4454
4455
0
  if (args->child[1]->kind != ZEND_AST_ARRAY
4456
0
   || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) {
4457
0
    return FAILURE;
4458
0
  }
4459
4460
0
  if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) {
4461
0
    bool ok = true;
4462
0
    zval *val, tmp;
4463
0
    HashTable *src = Z_ARRVAL(array.u.constant);
4464
0
    HashTable *dst = zend_new_array(zend_hash_num_elements(src));
4465
4466
0
    ZVAL_TRUE(&tmp);
4467
4468
0
    if (strict) {
4469
0
      ZEND_HASH_FOREACH_VAL(src, val) {
4470
0
        if (Z_TYPE_P(val) == IS_STRING) {
4471
0
          zend_hash_add(dst, Z_STR_P(val), &tmp);
4472
0
        } else if (Z_TYPE_P(val) == IS_LONG) {
4473
0
          zend_hash_index_add(dst, Z_LVAL_P(val), &tmp);
4474
0
        } else {
4475
0
          zend_array_destroy(dst);
4476
0
          ok = false;
4477
0
          break;
4478
0
        }
4479
0
      } ZEND_HASH_FOREACH_END();
4480
0
    } else {
4481
0
      ZEND_HASH_FOREACH_VAL(src, val) {
4482
0
        if (Z_TYPE_P(val) != IS_STRING
4483
0
         || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) {
4484
0
          zend_array_destroy(dst);
4485
0
          ok = false;
4486
0
          break;
4487
0
        }
4488
0
        zend_hash_add(dst, Z_STR_P(val), &tmp);
4489
0
      } ZEND_HASH_FOREACH_END();
4490
0
    }
4491
4492
0
    zend_array_destroy(src);
4493
0
    if (!ok) {
4494
0
      return FAILURE;
4495
0
    }
4496
0
    Z_ARRVAL(array.u.constant) = dst;
4497
0
  }
4498
0
  array.op_type = IS_CONST;
4499
4500
0
  zend_compile_expr(&needly, args->child[0]);
4501
4502
0
  opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array);
4503
0
  opline->extended_value = strict;
4504
4505
0
  return SUCCESS;
4506
0
}
4507
/* }}} */
4508
4509
static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */
4510
0
{
4511
0
  znode arg_node;
4512
0
  zend_op *opline;
4513
4514
0
  if (args->children != 1) {
4515
0
    return FAILURE;
4516
0
  }
4517
4518
0
  zend_compile_expr(&arg_node, args->child[0]);
4519
0
  opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL);
4520
0
  opline->extended_value = zend_string_equals_literal(lcname, "sizeof");
4521
4522
0
  return SUCCESS;
4523
0
}
4524
/* }}} */
4525
4526
static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */
4527
0
{
4528
0
  if (args->children == 0) {
4529
0
    zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL);
4530
0
  } else {
4531
0
    znode arg_node;
4532
4533
0
    if (args->children != 1) {
4534
0
      return FAILURE;
4535
0
    }
4536
4537
0
    zend_compile_expr(&arg_node, args->child[0]);
4538
0
    zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL);
4539
0
  }
4540
0
  return SUCCESS;
4541
0
}
4542
/* }}} */
4543
4544
static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */
4545
0
{
4546
0
  if (args->children != 0) {
4547
0
    return FAILURE;
4548
0
  }
4549
4550
0
  zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL);
4551
0
  return SUCCESS;
4552
0
}
4553
/* }}} */
4554
4555
static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */
4556
0
{
4557
0
  znode arg_node;
4558
4559
0
  if (args->children != 1) {
4560
0
    return FAILURE;
4561
0
  }
4562
4563
0
  zend_compile_expr(&arg_node, args->child[0]);
4564
0
  zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL);
4565
0
  return SUCCESS;
4566
0
}
4567
/* }}} */
4568
4569
static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */
4570
0
{
4571
0
  if (CG(active_op_array)->function_name && args->children == 0) {
4572
0
    zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL);
4573
0
    return SUCCESS;
4574
0
  } else {
4575
0
    return FAILURE;
4576
0
  }
4577
0
}
4578
/* }}} */
4579
4580
static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */
4581
0
{
4582
0
  if (CG(active_op_array)->function_name && args->children == 0) {
4583
0
    zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL);
4584
0
    return SUCCESS;
4585
0
  } else {
4586
0
    return FAILURE;
4587
0
  }
4588
0
}
4589
/* }}} */
4590
4591
static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */
4592
0
{
4593
0
  znode subject, needle;
4594
4595
0
  if (args->children != 2) {
4596
0
    return FAILURE;
4597
0
  }
4598
4599
0
  zend_compile_expr(&needle, args->child[0]);
4600
0
  zend_compile_expr(&subject, args->child[1]);
4601
4602
0
  zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject);
4603
0
  return SUCCESS;
4604
0
}
4605
/* }}} */
4606
4607
static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */
4608
0
{
4609
0
  if (CG(active_op_array)->function_name
4610
0
   && args->children == 2
4611
0
   && args->child[0]->kind == ZEND_AST_CALL
4612
0
   && args->child[0]->child[0]->kind == ZEND_AST_ZVAL
4613
0
   && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING
4614
0
   && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST
4615
0
   && args->child[1]->kind == ZEND_AST_ZVAL) {
4616
4617
0
    zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]);
4618
0
    bool is_fully_qualified;
4619
0
    zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified);
4620
0
    const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]);
4621
0
    const zval *zv = zend_ast_get_zval(args->child[1]);
4622
0
    znode first;
4623
4624
0
    if (zend_string_equals_literal_ci(name, "func_get_args")
4625
0
     && list->children == 0
4626
0
     && Z_TYPE_P(zv) == IS_LONG
4627
0
     && Z_LVAL_P(zv) >= 0) {
4628
0
      first.op_type = IS_CONST;
4629
0
      ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv));
4630
0
      zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL);
4631
0
      zend_string_release_ex(name, 0);
4632
0
      return SUCCESS;
4633
0
    }
4634
0
    zend_string_release_ex(name, 0);
4635
0
  }
4636
0
  return FAILURE;
4637
0
}
4638
/* }}} */
4639
4640
static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler)
4641
0
{
4642
0
  void **handlers = zend_flf_handlers;
4643
0
  void **current = handlers;
4644
0
  while (current) {
4645
0
    if (*current == handler) {
4646
0
      return current - handlers;
4647
0
    }
4648
0
    current++;
4649
0
  }
4650
4651
0
  return (uint32_t)-1;
4652
0
}
4653
4654
static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4655
0
{
4656
0
  if (zend_execute_internal) {
4657
0
    return NULL;
4658
0
  }
4659
4660
0
  if (type != BP_VAR_R) {
4661
0
    return NULL;
4662
0
  }
4663
4664
0
  if (ZEND_USER_CODE(fbc->type)) {
4665
0
    return NULL;
4666
0
  }
4667
4668
0
  const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos;
4669
0
  if (!frameless_function_info) {
4670
0
    return NULL;
4671
0
  }
4672
4673
0
  if (args->children > 3) {
4674
0
    return NULL;
4675
0
  }
4676
4677
0
  while (frameless_function_info->handler) {
4678
0
    if (frameless_function_info->num_args >= args->children
4679
0
     && fbc->common.required_num_args <= args->children
4680
0
     && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC)
4681
0
      || frameless_function_info->num_args == args->children)) {
4682
0
      uint32_t num_args = frameless_function_info->num_args;
4683
0
      uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4684
0
      if (offset == (uint32_t)-1) {
4685
0
        continue;
4686
0
      }
4687
0
      return frameless_function_info;
4688
0
    }
4689
0
    frameless_function_info++;
4690
0
  }
4691
4692
0
  return NULL;
4693
0
}
4694
4695
static uint32_t zend_compile_frameless_icall_ex(znode *result, const zend_ast_list *args, const zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type)
4696
0
{
4697
0
  uint32_t lineno = CG(zend_lineno);
4698
0
  uint32_t num_args = frameless_function_info->num_args;
4699
0
  uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler);
4700
0
  znode arg_zvs[3];
4701
0
  for (uint32_t i = 0; i < num_args; i++) {
4702
0
    if (i < args->children) {
4703
0
      zend_compile_expr(&arg_zvs[i], args->child[i]);
4704
0
    } else {
4705
0
      const zend_internal_arg_info *arg_info = (const zend_internal_arg_info *)&fbc->common.arg_info[i];
4706
0
      arg_zvs[i].op_type = IS_CONST;
4707
0
      if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) {
4708
0
        ZEND_UNREACHABLE();
4709
0
      }
4710
0
    }
4711
0
  }
4712
0
  uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args;
4713
0
  uint32_t opnum = get_next_op_number();
4714
0
  zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL);
4715
0
  opline->extended_value = offset;
4716
0
  opline->lineno = lineno;
4717
0
  if (num_args >= 1) {
4718
0
    SET_NODE(opline->op1, &arg_zvs[0]);
4719
0
  }
4720
0
  if (num_args >= 2) {
4721
0
    SET_NODE(opline->op2, &arg_zvs[1]);
4722
0
  }
4723
0
  if (num_args >= 3) {
4724
0
    zend_emit_op_data(&arg_zvs[2]);
4725
0
  }
4726
0
  return opnum;
4727
0
}
4728
4729
static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type)
4730
0
{
4731
0
  const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type);
4732
0
  if (!frameless_function_info) {
4733
0
    return (uint32_t)-1;
4734
0
  }
4735
4736
0
  return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type);
4737
0
}
4738
4739
static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */
4740
0
{
4741
0
  int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant));
4742
4743
  /* Find frameless function with same name. */
4744
0
  const zend_function *frameless_function = NULL;
4745
0
  if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT
4746
0
   && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast))
4747
   /* Avoid blowing up op count with nested frameless branches. */
4748
0
   && !CG(context).in_jmp_frameless_branch) {
4749
0
    zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2));
4750
0
    frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name);
4751
0
  }
4752
4753
  /* Check whether any frameless handler may actually be used. */
4754
0
  uint32_t jmp_fl_opnum = 0;
4755
0
  const zend_frameless_function_info *frameless_function_info = NULL;
4756
0
  if (frameless_function) {
4757
0
    frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type);
4758
0
    if (frameless_function_info) {
4759
0
      CG(context).in_jmp_frameless_branch = true;
4760
0
      znode op1;
4761
0
      op1.op_type = IS_CONST;
4762
0
      ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1));
4763
0
      jmp_fl_opnum = get_next_op_number();
4764
0
      zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL);
4765
0
    }
4766
0
  }
4767
4768
  /* Compile ns call. */
4769
0
  zend_op *opline = get_next_op();
4770
0
  opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
4771
0
  opline->op2_type = IS_CONST;
4772
0
  opline->op2.constant = name_constants;
4773
0
  opline->result.num = zend_alloc_cache_slot();
4774
0
  zend_compile_call_common(result, args_ast, NULL, lineno);
4775
4776
  /* Compile frameless call. */
4777
0
  if (frameless_function_info) {
4778
0
    CG(zend_lineno) = lineno;
4779
4780
0
    uint32_t jmp_end_opnum = zend_emit_jump(0);
4781
0
    uint32_t jmp_fl_target = get_next_op_number();
4782
4783
0
    uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type);
4784
4785
0
    zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum];
4786
0
    jmp_fl->op2.opline_num = jmp_fl_target;
4787
0
    jmp_fl->extended_value = zend_alloc_cache_slot();
4788
0
    zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum];
4789
0
    SET_NODE(flf_icall->result, result);
4790
0
    zend_update_jump_target_to_next(jmp_end_opnum);
4791
4792
0
    CG(context).in_jmp_frameless_branch = false;
4793
0
  }
4794
0
}
4795
/* }}} */
4796
4797
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node);
4798
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node);
4799
static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline);
4800
4801
static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */
4802
0
{
4803
  /* Bail out if we do not have a format string. */
4804
0
  if (args->children < 1) {
4805
0
    return FAILURE;
4806
0
  }
4807
4808
0
  zend_eval_const_expr(&args->child[0]);
4809
  /* Bail out if the format string is not constant. */
4810
0
  if (args->child[0]->kind != ZEND_AST_ZVAL) {
4811
0
    return FAILURE;
4812
0
  }
4813
4814
0
  zval *format_string = zend_ast_get_zval(args->child[0]);
4815
0
  if (Z_TYPE_P(format_string) != IS_STRING) {
4816
0
    return FAILURE;
4817
0
  }
4818
0
  if (Z_STRLEN_P(format_string) >= 256) {
4819
0
    return FAILURE;
4820
0
  }
4821
4822
0
  char *p;
4823
0
  char *end;
4824
0
  uint32_t placeholder_count;
4825
4826
0
  placeholder_count = 0;
4827
0
  p = Z_STRVAL_P(format_string);
4828
0
  end = p + Z_STRLEN_P(format_string);
4829
4830
0
  for (;;) {
4831
0
    p = memchr(p, '%', end - p);
4832
0
    if (!p) {
4833
0
      break;
4834
0
    }
4835
4836
0
    char *q = p + 1;
4837
0
    if (q == end) {
4838
0
      return FAILURE;
4839
0
    }
4840
4841
0
    switch (*q) {
4842
0
      case 's':
4843
0
      case 'd':
4844
0
        placeholder_count++;
4845
0
        break;
4846
0
      case '%':
4847
0
        break;
4848
0
      default:
4849
0
        return FAILURE;
4850
0
    }
4851
4852
0
    p = q;
4853
0
    p++;
4854
0
  }
4855
4856
  /* Bail out if the number of placeholders does not match the number of values. */
4857
0
  if (placeholder_count != (args->children - 1)) {
4858
0
    return FAILURE;
4859
0
  }
4860
4861
  /* Handle empty format strings. */
4862
0
  if (Z_STRLEN_P(format_string) == 0) {
4863
0
    result->op_type = IS_CONST;
4864
0
    ZVAL_EMPTY_STRING(&result->u.constant);
4865
4866
0
    return SUCCESS;
4867
0
  }
4868
4869
0
  znode *elements = NULL;
4870
4871
0
  if (placeholder_count > 0) {
4872
0
    elements = safe_emalloc(sizeof(*elements), placeholder_count, 0);
4873
0
  }
4874
4875
  /* Compile the value expressions first for error handling that is consistent
4876
   * with a function call: Values that fail to convert to a string may emit errors.
4877
   */
4878
0
  for (uint32_t i = 0; i < placeholder_count; i++) {
4879
0
    zend_compile_expr(elements + i, args->child[1 + i]);
4880
0
  }
4881
4882
0
  uint32_t rope_elements = 0;
4883
0
  uint32_t rope_init_lineno = -1;
4884
0
  zend_op *opline = NULL;
4885
4886
0
  placeholder_count = 0;
4887
0
  p = Z_STRVAL_P(format_string);
4888
0
  end = p + Z_STRLEN_P(format_string);
4889
0
  char *offset = p;
4890
0
  for (;;) {
4891
0
    p = memchr(p, '%', end - p);
4892
0
    if (!p) {
4893
0
      break;
4894
0
    }
4895
4896
0
    char *q = p + 1;
4897
0
    ZEND_ASSERT(q < end);
4898
0
    ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%');
4899
4900
0
    if (*q == '%') {
4901
      /* Optimization to not create a dedicated rope element for the literal '%':
4902
       * Include the first '%' within the "constant" part instead of dropping the
4903
       * full placeholder.
4904
       */
4905
0
      p++;
4906
0
    }
4907
4908
0
    if (p != offset) {
4909
0
      znode const_node;
4910
0
      const_node.op_type = IS_CONST;
4911
0
      ZVAL_STRINGL(&const_node.u.constant, offset, p - offset);
4912
0
      if (rope_elements == 0) {
4913
0
        rope_init_lineno = get_next_op_number();
4914
0
      }
4915
0
      opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4916
0
    }
4917
4918
0
    if (*q != '%') {
4919
0
      switch (*q) {
4920
0
        case 's':
4921
          /* Perform the cast of constants when actually evaluating the corresponding placeholder
4922
           * for correct error reporting.
4923
           */
4924
0
          if (elements[placeholder_count].op_type == IS_CONST) {
4925
0
            if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) {
4926
0
              zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING;
4927
0
            } else {
4928
0
              convert_to_string(&elements[placeholder_count].u.constant);
4929
0
            }
4930
0
          }
4931
0
          break;
4932
0
        case 'd':
4933
0
          zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG;
4934
0
          break;
4935
0
        EMPTY_SWITCH_DEFAULT_CASE();
4936
0
      }
4937
4938
0
      if (rope_elements == 0) {
4939
0
        rope_init_lineno = get_next_op_number();
4940
0
      }
4941
0
      opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]);
4942
4943
0
      placeholder_count++;
4944
0
    }
4945
4946
0
    p = q;
4947
0
    p++;
4948
0
    offset = p;
4949
0
  }
4950
0
  if (end != offset) {
4951
    /* Add the constant part after the last placeholder. */
4952
0
    znode const_node;
4953
0
    const_node.op_type = IS_CONST;
4954
0
    ZVAL_STRINGL(&const_node.u.constant, offset, end - offset);
4955
0
    if (rope_elements == 0) {
4956
0
      rope_init_lineno = get_next_op_number();
4957
0
    }
4958
0
    opline = zend_compile_rope_add(result, rope_elements++, &const_node);
4959
0
  }
4960
0
  ZEND_ASSERT(opline != NULL);
4961
4962
0
  zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
4963
0
  zend_compile_rope_finalize(result, rope_elements, init_opline, opline);
4964
0
  efree(elements);
4965
4966
0
  return SUCCESS;
4967
0
}
4968
4969
static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */
4970
0
{
4971
  /* Special case: printf with a single constant string argument and no format specifiers.
4972
   * In this case, just emit ECHO and return the string length if needed. */
4973
0
  if (args->children == 1) {
4974
0
    zend_eval_const_expr(&args->child[0]);
4975
0
    if (args->child[0]->kind != ZEND_AST_ZVAL) {
4976
0
      return FAILURE;
4977
0
    }
4978
0
    zval *format_string = zend_ast_get_zval(args->child[0]);
4979
0
    if (Z_TYPE_P(format_string) != IS_STRING) {
4980
0
      return FAILURE;
4981
0
    }
4982
    /* Check if there are any format specifiers */
4983
0
    if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) {
4984
      /* No format specifiers - just emit ECHO and return string length */
4985
0
      znode format_node;
4986
0
      zend_compile_expr(&format_node, args->child[0]);
4987
0
      zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL);
4988
4989
      /* Return the string length as a constant if the result is used */
4990
0
      result->op_type = IS_CONST;
4991
0
      ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string));
4992
0
      return SUCCESS;
4993
0
    }
4994
0
  }
4995
4996
  /* Fall back to sprintf optimization for format strings with specifiers */
4997
0
  znode rope_result;
4998
0
  if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) {
4999
0
    return FAILURE;
5000
0
  }
5001
5002
  /* printf() returns the amount of bytes written, so just an ECHO of the
5003
   * resulting sprintf() optimisation might not be enough. At this early
5004
   * stage we can't detect if the result is actually used, so we just emit
5005
   * the opcodes and let them be cleaned up by the dead code elimination
5006
   * pass in the Zend Optimizer if the result of the printf() is in fact
5007
   * unused */
5008
0
  znode copy;
5009
0
  if (rope_result.op_type != IS_CONST) {
5010
    /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */
5011
0
    ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR);
5012
0
    zend_emit_op_tmp(&copy, ZEND_COPY_TMP, &rope_result, NULL);
5013
0
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5014
0
    zend_emit_op_tmp(result, ZEND_STRLEN, &copy, NULL);
5015
0
  } else {
5016
0
    zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL);
5017
0
    result->op_type = IS_CONST;
5018
0
    ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant));
5019
0
  }
5020
5021
0
  return SUCCESS;
5022
0
}
5023
5024
static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args)
5025
0
{
5026
0
  znode arg_node;
5027
5028
0
  if (args->children != 1) {
5029
0
    return FAILURE;
5030
0
  }
5031
5032
0
  zend_compile_expr(&arg_node, args->child[0]);
5033
0
  zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL);
5034
5035
0
  return SUCCESS;
5036
0
}
5037
5038
static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */
5039
0
{
5040
0
  if (zend_string_equals_literal(lcname, "strlen")) {
5041
0
    return zend_compile_func_strlen(result, args);
5042
0
  } else if (zend_string_equals_literal(lcname, "is_null")) {
5043
0
    return zend_compile_func_typecheck(result, args, IS_NULL);
5044
0
  } else if (zend_string_equals_literal(lcname, "is_bool")) {
5045
0
    return zend_compile_func_typecheck(result, args, _IS_BOOL);
5046
0
  } else if (zend_string_equals_literal(lcname, "is_long")
5047
0
    || zend_string_equals_literal(lcname, "is_int")
5048
0
    || zend_string_equals_literal(lcname, "is_integer")
5049
0
  ) {
5050
0
    return zend_compile_func_typecheck(result, args, IS_LONG);
5051
0
  } else if (zend_string_equals_literal(lcname, "is_float")
5052
0
    || zend_string_equals_literal(lcname, "is_double")
5053
0
  ) {
5054
0
    return zend_compile_func_typecheck(result, args, IS_DOUBLE);
5055
0
  } else if (zend_string_equals_literal(lcname, "is_string")) {
5056
0
    return zend_compile_func_typecheck(result, args, IS_STRING);
5057
0
  } else if (zend_string_equals_literal(lcname, "is_array")) {
5058
0
    return zend_compile_func_typecheck(result, args, IS_ARRAY);
5059
0
  } else if (zend_string_equals_literal(lcname, "is_object")) {
5060
0
    return zend_compile_func_typecheck(result, args, IS_OBJECT);
5061
0
  } else if (zend_string_equals_literal(lcname, "is_resource")) {
5062
0
    return zend_compile_func_typecheck(result, args, IS_RESOURCE);
5063
0
  } else if (zend_string_equals_literal(lcname, "is_scalar")) {
5064
0
    return zend_compile_func_is_scalar(result, args);
5065
0
  } else if (zend_string_equals_literal(lcname, "boolval")) {
5066
0
    return zend_compile_func_cast(result, args, _IS_BOOL);
5067
0
  } else if (zend_string_equals_literal(lcname, "intval")) {
5068
0
    return zend_compile_func_cast(result, args, IS_LONG);
5069
0
  } else if (zend_string_equals_literal(lcname, "floatval")
5070
0
    || zend_string_equals_literal(lcname, "doubleval")
5071
0
  ) {
5072
0
    return zend_compile_func_cast(result, args, IS_DOUBLE);
5073
0
  } else if (zend_string_equals_literal(lcname, "strval")) {
5074
0
    return zend_compile_func_cast(result, args, IS_STRING);
5075
0
  } else if (zend_string_equals_literal(lcname, "defined")) {
5076
0
    return zend_compile_func_defined(result, args);
5077
0
  } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
5078
0
    return zend_compile_func_chr(result, args);
5079
0
  } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
5080
0
    return zend_compile_func_ord(result, args);
5081
0
  } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
5082
0
    return zend_compile_func_cufa(result, args, lcname);
5083
0
  } else if (zend_string_equals_literal(lcname, "call_user_func")) {
5084
0
    return zend_compile_func_cuf(result, args, lcname);
5085
0
  } else if (zend_string_equals_literal(lcname, "in_array")) {
5086
0
    return zend_compile_func_in_array(result, args);
5087
0
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT))
5088
0
      || zend_string_equals_literal(lcname, "sizeof")) {
5089
0
    return zend_compile_func_count(result, args, lcname);
5090
0
  } else if (zend_string_equals_literal(lcname, "get_class")) {
5091
0
    return zend_compile_func_get_class(result, args);
5092
0
  } else if (zend_string_equals_literal(lcname, "get_called_class")) {
5093
0
    return zend_compile_func_get_called_class(result, args);
5094
0
  } else if (zend_string_equals_literal(lcname, "gettype")) {
5095
0
    return zend_compile_func_gettype(result, args);
5096
0
  } else if (zend_string_equals_literal(lcname, "func_num_args")) {
5097
0
    return zend_compile_func_num_args(result, args);
5098
0
  } else if (zend_string_equals_literal(lcname, "func_get_args")) {
5099
0
    return zend_compile_func_get_args(result, args);
5100
0
  } else if (zend_string_equals_literal(lcname, "array_slice")) {
5101
0
    return zend_compile_func_array_slice(result, args);
5102
0
  } else if (zend_string_equals_literal(lcname, "array_key_exists")) {
5103
0
    return zend_compile_func_array_key_exists(result, args);
5104
0
  } else if (zend_string_equals_literal(lcname, "sprintf")) {
5105
0
    return zend_compile_func_sprintf(result, args);
5106
0
  } else if (zend_string_equals_literal(lcname, "printf")) {
5107
0
    return zend_compile_func_printf(result, args);
5108
0
  } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) {
5109
0
    return zend_compile_func_clone(result, args);
5110
0
  } else {
5111
0
    return FAILURE;
5112
0
  }
5113
0
}
5114
5115
static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */
5116
0
{
5117
0
  if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
5118
0
    return FAILURE;
5119
0
  }
5120
5121
0
  if (fbc->type != ZEND_INTERNAL_FUNCTION) {
5122
    /* If the function is part of disabled_functions, it may be redeclared as a userland
5123
     * function with a different implementation. Don't use the VM builtin in that case. */
5124
0
    return FAILURE;
5125
0
  }
5126
5127
0
  if (zend_args_contain_unpack_or_named(args)) {
5128
0
    return FAILURE;
5129
0
  }
5130
5131
0
  if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) {
5132
0
    return SUCCESS;
5133
0
  }
5134
5135
0
  return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE;
5136
0
}
5137
5138
0
static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) {
5139
0
  switch (kind) {
5140
0
    case ZEND_PROPERTY_HOOK_GET:
5141
0
      return "get";
5142
0
    case ZEND_PROPERTY_HOOK_SET:
5143
0
      return "set";
5144
0
    EMPTY_SWITCH_DEFAULT_CASE()
5145
0
  }
5146
0
}
5147
5148
static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name)
5149
0
{
5150
0
  if (ZSTR_VAL(prop_name)[0] != '\0') {
5151
0
    return zend_string_copy(prop_name);
5152
0
  } else {
5153
0
    const char *unmangled = zend_get_unmangled_property_name(prop_name);
5154
0
    return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false);
5155
0
  }
5156
0
}
5157
5158
static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type)
5159
0
{
5160
0
  ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL);
5161
5162
0
  const zend_ast *class_ast = ast->child[0];
5163
0
  zend_ast *method_ast = ast->child[1];
5164
5165
  /* Recognize parent::$prop::get() pattern. */
5166
0
  if (class_ast->kind != ZEND_AST_STATIC_PROP
5167
0
   || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP)
5168
0
   || class_ast->child[0]->kind != ZEND_AST_ZVAL
5169
0
   || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING
5170
0
   || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT
5171
0
   || class_ast->child[1]->kind != ZEND_AST_ZVAL
5172
0
   || method_ast->kind != ZEND_AST_ZVAL
5173
0
   || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING
5174
0
   || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get")
5175
0
    && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) {
5176
0
    return false;
5177
0
  }
5178
5179
0
  zend_class_entry *ce = CG(active_class_entry);
5180
0
  if (!ce) {
5181
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active");
5182
0
  }
5183
5184
0
  zend_ast *args_ast = ast->child[2];
5185
0
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
5186
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call");
5187
0
  }
5188
5189
0
  zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]);
5190
0
  zend_string *property_name = zval_get_string(property_hook_name_zv);
5191
0
  zend_string *hook_name = zend_ast_get_str(method_ast);
5192
0
  zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name);
5193
0
  ZEND_ASSERT(hook_kind != (uint32_t)-1);
5194
5195
0
  const zend_string *prop_info_name = CG(context).active_property_info_name;
5196
0
  if (!prop_info_name) {
5197
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook",
5198
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name));
5199
0
  }
5200
5201
0
  const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name);
5202
0
  if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) {
5203
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)",
5204
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name);
5205
0
  }
5206
0
  if (hook_kind != CG(context).active_property_hook_kind) {
5207
0
    zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)",
5208
0
      ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind));
5209
0
  }
5210
5211
0
  zend_op *opline = get_next_op();
5212
0
  opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL;
5213
0
  opline->op1_type = IS_CONST;
5214
0
  opline->op1.constant = zend_add_literal_string(&property_name);
5215
0
  opline->op2.num = hook_kind;
5216
5217
0
  zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast));
5218
5219
0
  return true;
5220
0
}
5221
5222
static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */
5223
0
{
5224
0
  zend_ast *name_ast = ast->child[0];
5225
0
  zend_ast *args_ast = ast->child[1];
5226
0
  bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT;
5227
5228
0
  znode name_node;
5229
5230
0
  if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
5231
0
    zend_compile_expr(&name_node, name_ast);
5232
0
    zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5233
0
    return;
5234
0
  }
5235
5236
0
  {
5237
0
    bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
5238
0
    if (runtime_resolution) {
5239
0
      if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
5240
0
          && !is_callable_convert) {
5241
0
        zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno);
5242
0
      } else {
5243
0
        zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type);
5244
0
      }
5245
0
      return;
5246
0
    }
5247
0
  }
5248
5249
0
  {
5250
0
    const zval *name = &name_node.u.constant;
5251
0
    zend_string *lcname = zend_string_tolower(Z_STR_P(name));
5252
0
    zval *fbc_zv = zend_hash_find(CG(function_table), lcname);
5253
0
    const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL;
5254
0
    zend_op *opline;
5255
5256
    /* Special assert() handling should apply independently of compiler flags. */
5257
0
    if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
5258
0
      zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno);
5259
0
      zend_string_release(lcname);
5260
0
      zval_ptr_dtor(&name_node.u.constant);
5261
0
      return;
5262
0
    }
5263
5264
0
    if (!fbc
5265
0
     || !fbc_is_finalized(fbc)
5266
0
     || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) {
5267
0
      zend_string_release_ex(lcname, 0);
5268
0
      zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno);
5269
0
      return;
5270
0
    }
5271
5272
0
    if (!is_callable_convert &&
5273
0
        zend_try_compile_special_func(result, lcname,
5274
0
        zend_ast_get_list(args_ast), fbc, type) == SUCCESS
5275
0
    ) {
5276
0
      zend_string_release_ex(lcname, 0);
5277
0
      zval_ptr_dtor(&name_node.u.constant);
5278
0
      return;
5279
0
    }
5280
5281
0
    zval_ptr_dtor(&name_node.u.constant);
5282
0
    ZVAL_NEW_STR(&name_node.u.constant, lcname);
5283
5284
0
    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
5285
0
    opline->result.num = zend_alloc_cache_slot();
5286
5287
    /* Store offset to function from symbol table in op2.extra. */
5288
0
    if (fbc->type == ZEND_INTERNAL_FUNCTION) {
5289
0
      const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val));
5290
0
      Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData;
5291
0
    }
5292
5293
0
    zend_compile_call_common(result, args_ast, fbc, ast->lineno);
5294
0
  }
5295
0
}
5296
/* }}} */
5297
5298
static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5299
0
{
5300
0
  zend_ast *obj_ast = ast->child[0];
5301
0
  zend_ast *method_ast = ast->child[1];
5302
0
  zend_ast *args_ast = ast->child[2];
5303
5304
0
  znode obj_node, method_node;
5305
0
  zend_op *opline;
5306
0
  const zend_function *fbc = NULL;
5307
0
  bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL;
5308
0
  uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint();
5309
5310
0
  if (is_this_fetch(obj_ast)) {
5311
0
    if (this_guaranteed_exists()) {
5312
0
      obj_node.op_type = IS_UNUSED;
5313
0
    } else {
5314
0
      zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL);
5315
0
    }
5316
0
    CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
5317
5318
    /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL
5319
     * check for a nullsafe access. */
5320
0
  } else {
5321
0
    zend_short_circuiting_mark_inner(obj_ast);
5322
0
    zend_compile_expr(&obj_node, obj_ast);
5323
0
    if (nullsafe) {
5324
0
      zend_emit_jmp_null(&obj_node, type);
5325
0
    }
5326
0
  }
5327
5328
0
  zend_compile_expr(&method_node, method_ast);
5329
0
  opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
5330
5331
0
  if (method_node.op_type == IS_CONST) {
5332
0
    if (Z_TYPE(method_node.u.constant) != IS_STRING) {
5333
0
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5334
0
    }
5335
5336
0
    opline->op2_type = IS_CONST;
5337
0
    opline->op2.constant = zend_add_func_name_literal(
5338
0
      Z_STR(method_node.u.constant));
5339
0
    opline->result.num = zend_alloc_cache_slots(2);
5340
0
  } else {
5341
0
    SET_NODE(opline->op2, &method_node);
5342
0
  }
5343
5344
  /* Check if this calls a known method on $this */
5345
0
  if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
5346
0
      CG(active_class_entry) && zend_is_scope_known()) {
5347
0
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5348
0
    fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
5349
5350
    /* We only know the exact method that is being called if it is either private or final.
5351
     * Otherwise an overriding method in a child class may be called. */
5352
0
    if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
5353
0
      fbc = NULL;
5354
0
    }
5355
0
  }
5356
5357
0
  if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast))) {
5358
0
    if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) {
5359
0
      zend_error_noreturn(E_COMPILE_ERROR,
5360
0
        "Cannot combine nullsafe operator with Closure creation");
5361
0
    }
5362
0
  }
5363
0
}
5364
/* }}} */
5365
5366
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5367
0
{
5368
0
  return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5369
0
}
5370
/* }}} */
5371
5372
static bool is_func_accessible(const zend_function *fbc)
5373
0
{
5374
0
  if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) {
5375
0
    return true;
5376
0
  }
5377
5378
0
  if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE)
5379
0
    && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED)
5380
0
    && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED))
5381
0
    && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) {
5382
0
    return true;
5383
0
  }
5384
5385
0
  return false;
5386
0
}
5387
5388
static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */
5389
0
{
5390
0
  const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname);
5391
5392
0
  if (!fbc || is_func_accessible(fbc)) {
5393
0
    return fbc;
5394
0
  }
5395
5396
0
  return NULL;
5397
0
}
5398
/* }}} */
5399
5400
static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
5401
0
{
5402
0
  zend_ast *class_ast = ast->child[0];
5403
0
  zend_ast *method_ast = ast->child[1];
5404
0
  zend_ast *args_ast = ast->child[2];
5405
5406
0
  znode class_node, method_node;
5407
0
  zend_op *opline;
5408
0
  const zend_function *fbc = NULL;
5409
5410
0
  if (zend_compile_parent_property_hook_call(result, ast, type)) {
5411
0
    return;
5412
0
  }
5413
5414
0
  zend_short_circuiting_mark_inner(class_ast);
5415
0
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5416
5417
0
  zend_compile_expr(&method_node, method_ast);
5418
5419
0
  if (method_node.op_type == IS_CONST) {
5420
0
    zval *name = &method_node.u.constant;
5421
0
    if (Z_TYPE_P(name) != IS_STRING) {
5422
0
      zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
5423
0
    }
5424
0
    if (zend_is_constructor(Z_STR_P(name))) {
5425
0
      zval_ptr_dtor(name);
5426
0
      method_node.op_type = IS_UNUSED;
5427
0
    }
5428
0
  }
5429
5430
0
  opline = get_next_op();
5431
0
  opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
5432
5433
0
  zend_set_class_name_op1(opline, &class_node);
5434
5435
0
  if (method_node.op_type == IS_CONST) {
5436
0
    opline->op2_type = IS_CONST;
5437
0
    opline->op2.constant = zend_add_func_name_literal(
5438
0
      Z_STR(method_node.u.constant));
5439
0
    opline->result.num = zend_alloc_cache_slots(2);
5440
0
  } else {
5441
0
    if (opline->op1_type == IS_CONST) {
5442
0
      opline->result.num = zend_alloc_cache_slot();
5443
0
    }
5444
0
    SET_NODE(opline->op2, &method_node);
5445
0
  }
5446
5447
  /* Check if we already know which method we're calling */
5448
0
  if (opline->op2_type == IS_CONST) {
5449
0
    zend_class_entry *ce = NULL;
5450
0
    if (opline->op1_type == IS_CONST) {
5451
0
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5452
0
      ce = zend_hash_find_ptr(CG(class_table), lcname);
5453
0
      if (ce) {
5454
0
        if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5455
0
          ce = NULL;
5456
0
        }
5457
0
      } else if (CG(active_class_entry)
5458
0
          && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5459
0
        ce = CG(active_class_entry);
5460
0
      }
5461
0
    } else if (opline->op1_type == IS_UNUSED
5462
0
        && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5463
0
        && zend_is_scope_known()) {
5464
0
      ce = CG(active_class_entry);
5465
0
    }
5466
0
    if (ce) {
5467
0
      zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
5468
0
      fbc = zend_get_compatible_func_or_null(ce, lcname);
5469
0
    }
5470
0
  }
5471
5472
0
  zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast));
5473
0
}
5474
/* }}} */
5475
5476
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel);
5477
5478
static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
5479
0
{
5480
0
  zend_ast *class_ast = ast->child[0];
5481
0
  zend_ast *args_ast = ast->child[1];
5482
5483
0
  znode class_node, ctor_result;
5484
0
  zend_op *opline;
5485
5486
0
  if (class_ast->kind == ZEND_AST_CLASS) {
5487
    /* anon class declaration */
5488
0
    zend_compile_class_decl(&class_node, class_ast, false);
5489
0
  } else {
5490
0
    zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
5491
0
  }
5492
5493
0
  opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
5494
5495
0
  zend_set_class_name_op1(opline, &class_node);
5496
5497
0
  if (opline->op1_type == IS_CONST) {
5498
0
    opline->op2.num = zend_alloc_cache_slot();
5499
0
  }
5500
5501
0
  zend_class_entry *ce = NULL;
5502
0
  if (opline->op1_type == IS_CONST) {
5503
0
    zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
5504
0
    ce = zend_hash_find_ptr(CG(class_table), lcname);
5505
0
    if (ce) {
5506
0
      if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) {
5507
0
        ce = NULL;
5508
0
      }
5509
0
    } else if (CG(active_class_entry)
5510
0
        && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
5511
0
      ce = CG(active_class_entry);
5512
0
    }
5513
0
  } else if (opline->op1_type == IS_UNUSED
5514
0
      && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
5515
0
      && zend_is_scope_known()) {
5516
0
    ce = CG(active_class_entry);
5517
0
  }
5518
5519
5520
0
  const zend_function *fbc = NULL;
5521
0
  if (ce
5522
0
      && ce->default_object_handlers->get_constructor == zend_std_get_constructor
5523
0
      && ce->constructor
5524
0
      && is_func_accessible(ce->constructor)) {
5525
0
    fbc = ce->constructor;
5526
0
  }
5527
5528
0
  zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno);
5529
0
  zend_do_free(&ctor_result);
5530
0
}
5531
/* }}} */
5532
5533
static void zend_compile_global_var(zend_ast *ast) /* {{{ */
5534
0
{
5535
0
  zend_ast *var_ast = ast->child[0];
5536
0
  zend_ast *name_ast = var_ast->child[0];
5537
5538
0
  znode name_node, result;
5539
5540
0
  zend_compile_expr(&name_node, name_ast);
5541
0
  if (name_node.op_type == IS_CONST) {
5542
0
    convert_to_string(&name_node.u.constant);
5543
0
  }
5544
5545
  // TODO(GLOBALS) Forbid "global $GLOBALS"?
5546
0
  if (is_this_fetch(var_ast)) {
5547
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
5548
0
  } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) {
5549
0
    zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
5550
0
    opline->extended_value = zend_alloc_cache_slot();
5551
0
  } else {
5552
    /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
5553
     * to not free the name_node operand, so it can be reused in the following
5554
     * ASSIGN_REF, which then frees it. */
5555
0
    zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
5556
0
    opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
5557
5558
0
    if (name_node.op_type == IS_CONST) {
5559
0
      zend_string_addref(Z_STR(name_node.u.constant));
5560
0
    }
5561
5562
0
    zend_emit_assign_ref_znode(
5563
0
      zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
5564
0
      &result
5565
0
    );
5566
0
  }
5567
0
}
5568
/* }}} */
5569
5570
static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */
5571
0
{
5572
0
  zend_op *opline;
5573
0
  if (!CG(active_op_array)->static_variables) {
5574
0
    if (CG(active_op_array)->scope) {
5575
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5576
0
    }
5577
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5578
0
  }
5579
5580
0
  value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value);
5581
5582
0
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5583
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5584
0
  }
5585
5586
0
  opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL);
5587
0
  opline->op1_type = IS_CV;
5588
0
  opline->op1.var = lookup_cv(var_name);
5589
0
  opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode;
5590
0
}
5591
/* }}} */
5592
5593
static void zend_compile_static_var(zend_ast *ast) /* {{{ */
5594
0
{
5595
0
  zend_ast *var_ast = ast->child[0];
5596
0
  zend_string *var_name = zend_ast_get_str(var_ast);
5597
5598
0
  if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
5599
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
5600
0
  }
5601
5602
0
  if (!CG(active_op_array)->static_variables) {
5603
0
    if (CG(active_op_array)->scope) {
5604
0
      CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
5605
0
    }
5606
0
    CG(active_op_array)->static_variables = zend_new_array(8);
5607
0
  }
5608
5609
0
  if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) {
5610
0
    zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name);
5611
0
  }
5612
5613
0
  zend_eval_const_expr(&ast->child[1]);
5614
0
  zend_ast *value_ast = ast->child[1];
5615
5616
0
  if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) {
5617
0
    zval *value_zv = value_ast
5618
0
      ? zend_ast_get_zval(value_ast)
5619
0
      : &EG(uninitialized_zval);
5620
0
    Z_TRY_ADDREF_P(value_zv);
5621
0
    zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF);
5622
0
  } else {
5623
0
    zend_op *opline;
5624
5625
0
    zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval));
5626
0
    uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData);
5627
5628
0
    uint32_t static_def_jmp_opnum = get_next_op_number();
5629
0
    opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL);
5630
0
    opline->op1_type = IS_CV;
5631
0
    opline->op1.var = lookup_cv(var_name);
5632
0
    opline->extended_value = placeholder_offset;
5633
5634
0
    znode expr;
5635
0
    zend_compile_expr(&expr, value_ast);
5636
5637
0
    opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr);
5638
0
    opline->op1_type = IS_CV;
5639
0
    opline->op1.var = lookup_cv(var_name);
5640
0
    opline->extended_value = placeholder_offset | ZEND_BIND_REF;
5641
5642
0
    zend_update_jump_target_to_next(static_def_jmp_opnum);
5643
0
  }
5644
0
}
5645
/* }}} */
5646
5647
static void zend_compile_unset(const zend_ast *ast) /* {{{ */
5648
0
{
5649
0
  zend_ast *var_ast = ast->child[0];
5650
0
  znode var_node;
5651
0
  zend_op *opline;
5652
5653
0
  zend_ensure_writable_variable(var_ast);
5654
5655
0
  if (is_global_var_fetch(var_ast)) {
5656
0
    if (!var_ast->child[1]) {
5657
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
5658
0
    }
5659
5660
0
    zend_compile_expr(&var_node, var_ast->child[1]);
5661
0
    if (var_node.op_type == IS_CONST) {
5662
0
      convert_to_string(&var_node.u.constant);
5663
0
    }
5664
5665
0
    opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
5666
0
    opline->extended_value = ZEND_FETCH_GLOBAL;
5667
0
    return;
5668
0
  }
5669
5670
0
  switch (var_ast->kind) {
5671
0
    case ZEND_AST_VAR:
5672
0
      if (is_this_fetch(var_ast)) {
5673
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
5674
0
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) {
5675
0
        opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL);
5676
0
      } else {
5677
0
        opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false);
5678
0
        opline->opcode = ZEND_UNSET_VAR;
5679
0
      }
5680
0
      return;
5681
0
    case ZEND_AST_DIM:
5682
0
      opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false);
5683
0
      opline->opcode = ZEND_UNSET_DIM;
5684
0
      return;
5685
0
    case ZEND_AST_PROP:
5686
0
    case ZEND_AST_NULLSAFE_PROP:
5687
0
      opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false);
5688
0
      opline->opcode = ZEND_UNSET_OBJ;
5689
0
      return;
5690
0
    case ZEND_AST_STATIC_PROP:
5691
0
      opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false);
5692
0
      opline->opcode = ZEND_UNSET_STATIC_PROP;
5693
0
      return;
5694
0
    EMPTY_SWITCH_DEFAULT_CASE()
5695
0
  }
5696
0
}
5697
/* }}} */
5698
5699
static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
5700
0
{
5701
0
  const zend_loop_var *base;
5702
0
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5703
5704
0
  if (!loop_var) {
5705
0
    return 1;
5706
0
  }
5707
0
  base = zend_stack_base(&CG(loop_var_stack));
5708
0
  for (; loop_var >= base; loop_var--) {
5709
0
    if (loop_var->opcode == ZEND_FAST_CALL) {
5710
0
      zend_op *opline = get_next_op();
5711
5712
0
      opline->opcode = ZEND_FAST_CALL;
5713
0
      opline->result_type = IS_TMP_VAR;
5714
0
      opline->result.var = loop_var->var_num;
5715
0
      if (return_value) {
5716
0
        SET_NODE(opline->op2, return_value);
5717
0
      }
5718
0
      opline->op1.num = loop_var->try_catch_offset;
5719
0
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5720
0
      zend_op *opline = get_next_op();
5721
0
      opline->opcode = ZEND_DISCARD_EXCEPTION;
5722
0
      opline->op1_type = IS_TMP_VAR;
5723
0
      opline->op1.var = loop_var->var_num;
5724
0
    } else if (loop_var->opcode == ZEND_RETURN) {
5725
      /* Stack separator */
5726
0
      break;
5727
0
    } else if (depth <= 1) {
5728
0
      return 1;
5729
0
    } else if (loop_var->opcode == ZEND_NOP) {
5730
      /* Loop doesn't have freeable variable */
5731
0
      depth--;
5732
0
    } else {
5733
0
      zend_op *opline;
5734
5735
0
      ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
5736
0
      opline = get_next_op();
5737
0
      opline->opcode = loop_var->opcode;
5738
0
      opline->op1_type = loop_var->var_type;
5739
0
      opline->op1.var = loop_var->var_num;
5740
0
      opline->extended_value = ZEND_FREE_ON_RETURN;
5741
0
      depth--;
5742
0
      }
5743
0
  }
5744
0
  return (depth == 0);
5745
0
}
5746
/* }}} */
5747
5748
static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */
5749
0
{
5750
0
  return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
5751
0
}
5752
/* }}} */
5753
5754
static bool zend_has_finally_ex(zend_long depth) /* {{{ */
5755
0
{
5756
0
  const zend_loop_var *base;
5757
0
  zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
5758
5759
0
  if (!loop_var) {
5760
0
    return 0;
5761
0
  }
5762
0
  base = zend_stack_base(&CG(loop_var_stack));
5763
0
  for (; loop_var >= base; loop_var--) {
5764
0
    if (loop_var->opcode == ZEND_FAST_CALL) {
5765
0
      return 1;
5766
0
    } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
5767
0
    } else if (loop_var->opcode == ZEND_RETURN) {
5768
      /* Stack separator */
5769
0
      return 0;
5770
0
    } else if (depth <= 1) {
5771
0
      return 0;
5772
0
    } else {
5773
0
      depth--;
5774
0
      }
5775
0
  }
5776
0
  return 0;
5777
0
}
5778
/* }}} */
5779
5780
static bool zend_has_finally(void) /* {{{ */
5781
0
{
5782
0
  return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
5783
0
}
5784
/* }}} */
5785
5786
static void zend_compile_return(const zend_ast *ast) /* {{{ */
5787
0
{
5788
0
  zend_ast *expr_ast = ast->child[0];
5789
0
  bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
5790
0
  bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5791
5792
0
  znode expr_node;
5793
0
  zend_op *opline;
5794
5795
0
  if (is_generator) {
5796
    /* For generators the by-ref flag refers to yields, not returns */
5797
0
    by_ref = false;
5798
0
  }
5799
5800
0
  if (!expr_ast) {
5801
0
    expr_node.op_type = IS_CONST;
5802
0
    ZVAL_NULL(&expr_node.u.constant);
5803
0
  } else if (by_ref && zend_is_variable(expr_ast)) {
5804
0
    zend_assert_not_short_circuited(expr_ast);
5805
0
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
5806
0
  } else {
5807
0
    zend_compile_expr(&expr_node, expr_ast);
5808
0
  }
5809
5810
0
  if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
5811
0
   && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
5812
0
   && zend_has_finally()) {
5813
    /* Copy return value into temporary VAR to avoid modification in finally code */
5814
0
    if (by_ref) {
5815
0
      zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
5816
0
    } else {
5817
0
      zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
5818
0
    }
5819
0
  }
5820
5821
  /* Generator return types are handled separately */
5822
0
  if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5823
0
    zend_emit_return_type_check(
5824
0
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5825
0
  }
5826
5827
0
  uint32_t opnum_before_finally = get_next_op_number();
5828
5829
0
  zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
5830
5831
  /* Content of reference might have changed in finally, repeat type check. */
5832
0
  if (by_ref
5833
   /* Check if any opcodes were emitted since the last return type check. */
5834
0
   && opnum_before_finally != get_next_op_number()
5835
0
   && !is_generator
5836
0
   && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
5837
0
    zend_emit_return_type_check(
5838
0
      expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false);
5839
0
  }
5840
5841
0
  opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
5842
0
    &expr_node, NULL);
5843
5844
0
  if (by_ref && expr_ast) {
5845
0
    if (zend_is_call(expr_ast)) {
5846
0
      opline->extended_value = ZEND_RETURNS_FUNCTION;
5847
0
    } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) {
5848
0
      opline->extended_value = ZEND_RETURNS_VALUE;
5849
0
    }
5850
0
  }
5851
0
}
5852
/* }}} */
5853
5854
static void zend_compile_void_cast(znode *result, const zend_ast *ast)
5855
0
{
5856
0
  zend_ast *expr_ast = ast->child[0];
5857
0
  znode expr_node;
5858
0
  zend_op *opline;
5859
5860
0
  zend_compile_expr(&expr_node, expr_ast);
5861
5862
0
  switch (expr_node.op_type) {
5863
0
    case IS_TMP_VAR:
5864
0
    case IS_VAR:
5865
0
      opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
5866
0
      opline->extended_value = ZEND_FREE_VOID_CAST;
5867
0
      break;
5868
0
    case IS_CONST:
5869
0
      zend_do_free(&expr_node);
5870
0
      break;
5871
0
  }
5872
0
}
5873
5874
static void zend_compile_echo(const zend_ast *ast) /* {{{ */
5875
0
{
5876
0
  zend_op *opline;
5877
0
  zend_ast *expr_ast = ast->child[0];
5878
5879
0
  znode expr_node;
5880
0
  zend_compile_expr(&expr_node, expr_ast);
5881
5882
0
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
5883
0
  opline->extended_value = 0;
5884
0
}
5885
/* }}} */
5886
5887
static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */
5888
0
{
5889
0
  zend_ast *expr_ast = ast->child[0];
5890
5891
0
  znode expr_node;
5892
0
  zend_compile_expr(&expr_node, expr_ast);
5893
5894
0
  zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
5895
0
  if (result) {
5896
    /* Mark this as an "expression throw" for opcache. */
5897
0
    opline->extended_value = ZEND_THROW_IS_EXPR;
5898
0
    result->op_type = IS_CONST;
5899
0
    ZVAL_TRUE(&result->u.constant);
5900
0
  }
5901
0
}
5902
/* }}} */
5903
5904
static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */
5905
0
{
5906
0
  zend_ast *depth_ast = ast->child[0];
5907
5908
0
  zend_op *opline;
5909
0
  zend_long depth;
5910
5911
0
  ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
5912
5913
0
  if (depth_ast) {
5914
0
    const zval *depth_zv;
5915
0
    if (depth_ast->kind != ZEND_AST_ZVAL) {
5916
0
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand "
5917
0
        "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5918
0
    }
5919
5920
0
    depth_zv = zend_ast_get_zval(depth_ast);
5921
0
    if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
5922
0
      zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers",
5923
0
        ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5924
0
    }
5925
5926
0
    depth = Z_LVAL_P(depth_zv);
5927
0
  } else {
5928
0
    depth = 1;
5929
0
  }
5930
5931
0
  if (CG(context).current_brk_cont == -1) {
5932
0
    zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
5933
0
      ast->kind == ZEND_AST_BREAK ? "break" : "continue");
5934
0
  } else {
5935
0
    if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
5936
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s",
5937
0
        ast->kind == ZEND_AST_BREAK ? "break" : "continue",
5938
0
        depth, depth == 1 ? "" : "s");
5939
0
    }
5940
0
  }
5941
5942
0
  if (ast->kind == ZEND_AST_CONTINUE) {
5943
0
    int d, cur = CG(context).current_brk_cont;
5944
0
    for (d = depth - 1; d > 0; d--) {
5945
0
      cur = CG(context).brk_cont_array[cur].parent;
5946
0
      ZEND_ASSERT(cur != -1);
5947
0
    }
5948
5949
0
    if (CG(context).brk_cont_array[cur].is_switch) {
5950
0
      if (depth == 1) {
5951
0
        if (CG(context).brk_cont_array[cur].parent == -1) {
5952
0
          zend_error(E_WARNING,
5953
0
            "\"continue\" targeting switch is equivalent to \"break\"");
5954
0
        } else {
5955
0
          zend_error(E_WARNING,
5956
0
            "\"continue\" targeting switch is equivalent to \"break\". " \
5957
0
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5958
0
            depth + 1);
5959
0
        }
5960
0
      } else {
5961
0
        if (CG(context).brk_cont_array[cur].parent == -1) {
5962
0
          zend_error(E_WARNING,
5963
0
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
5964
0
            depth, depth);
5965
0
        } else {
5966
0
          zend_error(E_WARNING,
5967
0
            "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
5968
0
            "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
5969
0
            depth, depth, depth + 1);
5970
0
        }
5971
0
      }
5972
0
    }
5973
0
  }
5974
5975
0
  opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
5976
0
  opline->op1.num = CG(context).current_brk_cont;
5977
0
  opline->op2.num = depth;
5978
0
}
5979
/* }}} */
5980
5981
void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
5982
0
{
5983
0
  zend_label *dest;
5984
0
  int remove_oplines = opline->op1.num;
5985
0
  zval *label;
5986
0
  uint32_t opnum = opline - op_array->opcodes;
5987
5988
0
  label = CT_CONSTANT_EX(op_array, opline->op2.constant);
5989
0
  if (CG(context).labels == NULL ||
5990
0
      (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
5991
0
  ) {
5992
0
    CG(in_compilation) = 1;
5993
0
    CG(active_op_array) = op_array;
5994
0
    CG(zend_lineno) = opline->lineno;
5995
0
    zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
5996
0
  }
5997
5998
0
  zval_ptr_dtor_str(label);
5999
0
  ZVAL_NULL(label);
6000
6001
0
  uint32_t current = opline->extended_value;
6002
0
  for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
6003
0
    if (current == -1) {
6004
0
      CG(in_compilation) = 1;
6005
0
      CG(active_op_array) = op_array;
6006
0
      CG(zend_lineno) = opline->lineno;
6007
0
      zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
6008
0
    }
6009
0
    if (CG(context).brk_cont_array[current].start >= 0) {
6010
0
      remove_oplines--;
6011
0
    }
6012
0
  }
6013
6014
0
  for (current = 0; current < op_array->last_try_catch; ++current) {
6015
0
    const zend_try_catch_element *elem = &op_array->try_catch_array[current];
6016
0
    if (elem->try_op > opnum) {
6017
0
      break;
6018
0
    }
6019
0
    if (elem->finally_op && opnum < elem->finally_op - 1
6020
0
      && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
6021
0
    ) {
6022
0
      remove_oplines--;
6023
0
    }
6024
0
  }
6025
6026
0
  opline->opcode = ZEND_JMP;
6027
0
  SET_UNUSED(opline->op1);
6028
0
  SET_UNUSED(opline->op2);
6029
0
  SET_UNUSED(opline->result);
6030
0
  opline->op1.opline_num = dest->opline_num;
6031
0
  opline->extended_value = 0;
6032
6033
0
  ZEND_ASSERT(remove_oplines >= 0);
6034
0
  while (remove_oplines--) {
6035
0
    opline--;
6036
0
    MAKE_NOP(opline);
6037
0
    ZEND_VM_SET_OPCODE_HANDLER(opline);
6038
0
  }
6039
0
}
6040
/* }}} */
6041
6042
static void zend_compile_goto(const zend_ast *ast) /* {{{ */
6043
0
{
6044
0
  zend_ast *label_ast = ast->child[0];
6045
0
  znode label_node;
6046
0
  zend_op *opline;
6047
6048
0
  zend_compile_expr(&label_node, label_ast);
6049
6050
  /* Label resolution and unwinding adjustments happen in pass two. */
6051
0
  uint32_t opnum_start = get_next_op_number();
6052
0
  zend_handle_loops_and_finally(NULL);
6053
0
  opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
6054
0
  opline->op1.num = get_next_op_number() - opnum_start - 1;
6055
0
  opline->extended_value = CG(context).current_brk_cont;
6056
0
}
6057
/* }}} */
6058
6059
static void zend_compile_label(const zend_ast *ast) /* {{{ */
6060
0
{
6061
0
  zend_string *label = zend_ast_get_str(ast->child[0]);
6062
0
  zend_label dest;
6063
6064
0
  if (!CG(context).labels) {
6065
0
    ALLOC_HASHTABLE(CG(context).labels);
6066
0
    zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
6067
0
  }
6068
6069
0
  dest.brk_cont = CG(context).current_brk_cont;
6070
0
  dest.opline_num = get_next_op_number();
6071
6072
0
  if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
6073
0
    zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
6074
0
  }
6075
0
}
6076
/* }}} */
6077
6078
static void zend_compile_while(const zend_ast *ast) /* {{{ */
6079
0
{
6080
0
  zend_ast *cond_ast = ast->child[0];
6081
0
  zend_ast *stmt_ast = ast->child[1];
6082
0
  znode cond_node;
6083
0
  uint32_t opnum_start, opnum_jmp, opnum_cond;
6084
6085
0
  opnum_jmp = zend_emit_jump(0);
6086
6087
0
  zend_begin_loop(ZEND_NOP, NULL, false);
6088
6089
0
  opnum_start = get_next_op_number();
6090
0
  zend_compile_stmt(stmt_ast);
6091
6092
0
  opnum_cond = get_next_op_number();
6093
0
  zend_update_jump_target(opnum_jmp, opnum_cond);
6094
0
  zend_compile_expr(&cond_node, cond_ast);
6095
6096
0
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6097
6098
0
  zend_end_loop(opnum_cond, NULL);
6099
0
}
6100
/* }}} */
6101
6102
static void zend_compile_do_while(const zend_ast *ast) /* {{{ */
6103
0
{
6104
0
  zend_ast *stmt_ast = ast->child[0];
6105
0
  zend_ast *cond_ast = ast->child[1];
6106
6107
0
  znode cond_node;
6108
0
  uint32_t opnum_start, opnum_cond;
6109
6110
0
  zend_begin_loop(ZEND_NOP, NULL, false);
6111
6112
0
  opnum_start = get_next_op_number();
6113
0
  zend_compile_stmt(stmt_ast);
6114
6115
0
  opnum_cond = get_next_op_number();
6116
0
  zend_compile_expr(&cond_node, cond_ast);
6117
6118
0
  zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
6119
6120
0
  zend_end_loop(opnum_cond, NULL);
6121
0
}
6122
/* }}} */
6123
6124
static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */
6125
0
{
6126
0
  const zend_ast_list *list;
6127
0
  uint32_t i;
6128
6129
0
  result->op_type = IS_CONST;
6130
0
  ZVAL_TRUE(&result->u.constant);
6131
6132
0
  if (!ast) {
6133
0
    return;
6134
0
  }
6135
6136
0
  list = zend_ast_get_list(ast);
6137
0
  for (i = 0; i < list->children; ++i) {
6138
0
    zend_ast *expr_ast = list->child[i];
6139
6140
0
    zend_do_free(result);
6141
0
    if (expr_ast->kind == ZEND_AST_CAST_VOID) {
6142
0
      zend_compile_void_cast(NULL, expr_ast);
6143
0
      result->op_type = IS_CONST;
6144
0
      ZVAL_NULL(&result->u.constant);
6145
0
    } else {
6146
0
      zend_compile_expr(result, expr_ast);
6147
0
    }
6148
0
  }
6149
0
}
6150
/* }}} */
6151
6152
static void zend_compile_for(const zend_ast *ast) /* {{{ */
6153
0
{
6154
0
  zend_ast *init_ast = ast->child[0];
6155
0
  zend_ast *cond_ast = ast->child[1];
6156
0
  zend_ast *loop_ast = ast->child[2];
6157
0
  zend_ast *stmt_ast = ast->child[3];
6158
6159
0
  znode result;
6160
0
  uint32_t opnum_start, opnum_jmp, opnum_loop;
6161
6162
0
  zend_compile_for_expr_list(&result, init_ast);
6163
0
  zend_do_free(&result);
6164
6165
0
  opnum_jmp = zend_emit_jump(0);
6166
6167
0
  zend_begin_loop(ZEND_NOP, NULL, false);
6168
6169
0
  opnum_start = get_next_op_number();
6170
0
  zend_compile_stmt(stmt_ast);
6171
6172
0
  opnum_loop = get_next_op_number();
6173
0
  zend_compile_for_expr_list(&result, loop_ast);
6174
0
  zend_do_free(&result);
6175
6176
0
  zend_update_jump_target_to_next(opnum_jmp);
6177
0
  zend_compile_for_expr_list(&result, cond_ast);
6178
0
  zend_do_extended_stmt(NULL);
6179
6180
0
  zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
6181
6182
0
  zend_end_loop(opnum_loop, NULL);
6183
0
}
6184
/* }}} */
6185
6186
static void zend_compile_foreach(zend_ast *ast) /* {{{ */
6187
0
{
6188
0
  zend_ast *expr_ast = ast->child[0];
6189
0
  zend_ast *value_ast = ast->child[1];
6190
0
  zend_ast *key_ast = ast->child[2];
6191
0
  zend_ast *stmt_ast = ast->child[3];
6192
0
  bool by_ref = value_ast->kind == ZEND_AST_REF;
6193
0
  bool is_variable = zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast);
6194
6195
0
  znode expr_node, reset_node, value_node, key_node;
6196
0
  zend_op *opline;
6197
0
  uint32_t opnum_reset, opnum_fetch;
6198
6199
0
  if (key_ast) {
6200
0
    if (key_ast->kind == ZEND_AST_REF) {
6201
0
      zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
6202
0
    }
6203
0
    if (key_ast->kind == ZEND_AST_ARRAY) {
6204
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
6205
0
    }
6206
0
  }
6207
6208
0
  if (by_ref) {
6209
0
    value_ast = value_ast->child[0];
6210
0
  }
6211
6212
0
  if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) {
6213
0
    by_ref = true;
6214
0
  }
6215
6216
0
  if (by_ref && is_variable) {
6217
0
    zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true);
6218
0
  } else {
6219
0
    zend_compile_expr(&expr_node, expr_ast);
6220
0
  }
6221
6222
0
  if (by_ref) {
6223
0
    zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
6224
0
  }
6225
6226
0
  opnum_reset = get_next_op_number();
6227
0
  opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
6228
6229
0
  zend_begin_loop(ZEND_FE_FREE, &reset_node, false);
6230
6231
0
  opnum_fetch = get_next_op_number();
6232
0
  opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
6233
6234
0
  if (is_this_fetch(value_ast)) {
6235
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6236
0
  } else if (value_ast->kind == ZEND_AST_VAR &&
6237
0
    zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) {
6238
0
    SET_NODE(opline->op2, &value_node);
6239
0
  } else {
6240
0
    opline->op2_type = IS_VAR;
6241
0
    opline->op2.var = get_temporary_variable();
6242
0
    GET_NODE(&value_node, opline->op2);
6243
0
    if (value_ast->kind == ZEND_AST_ARRAY) {
6244
0
      zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr);
6245
0
    } else if (by_ref) {
6246
0
      zend_emit_assign_ref_znode(value_ast, &value_node);
6247
0
    } else {
6248
0
      zend_emit_assign_znode(value_ast, &value_node);
6249
0
    }
6250
0
  }
6251
6252
0
  if (key_ast) {
6253
0
    opline = &CG(active_op_array)->opcodes[opnum_fetch];
6254
0
    zend_make_tmp_result(&key_node, opline);
6255
0
    zend_emit_assign_znode(key_ast, &key_node);
6256
0
  }
6257
6258
0
  zend_compile_stmt(stmt_ast);
6259
6260
  /* Place JMP and FE_FREE on the line where foreach starts. It would be
6261
   * better to use the end line, but this information is not available
6262
   * currently. */
6263
0
  CG(zend_lineno) = ast->lineno;
6264
0
  zend_emit_jump(opnum_fetch);
6265
6266
0
  opline = &CG(active_op_array)->opcodes[opnum_reset];
6267
0
  opline->op2.opline_num = get_next_op_number();
6268
6269
0
  opline = &CG(active_op_array)->opcodes[opnum_fetch];
6270
0
  opline->extended_value = get_next_op_number();
6271
6272
0
  zend_end_loop(opnum_fetch, &reset_node);
6273
6274
0
  opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
6275
0
}
6276
/* }}} */
6277
6278
static void zend_compile_if(zend_ast *ast) /* {{{ */
6279
0
{
6280
0
  const zend_ast_list *list = zend_ast_get_list(ast);
6281
0
  uint32_t i;
6282
0
  uint32_t *jmp_opnums = NULL;
6283
6284
0
  if (list->children > 1) {
6285
0
    jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
6286
0
  }
6287
6288
0
  for (i = 0; i < list->children; ++i) {
6289
0
    const zend_ast *elem_ast = list->child[i];
6290
0
    zend_ast *cond_ast = elem_ast->child[0];
6291
0
    zend_ast *stmt_ast = elem_ast->child[1];
6292
6293
0
    if (cond_ast) {
6294
0
      znode cond_node;
6295
0
      uint32_t opnum_jmpz;
6296
6297
0
      if (i > 0) {
6298
0
        CG(zend_lineno) = cond_ast->lineno;
6299
0
        zend_do_extended_stmt(NULL);
6300
0
      }
6301
6302
0
      zend_compile_expr(&cond_node, cond_ast);
6303
0
      opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6304
6305
0
      zend_compile_stmt(stmt_ast);
6306
6307
0
      if (i != list->children - 1) {
6308
        /* Set the lineno of JMP to the position of the if keyword, as we don't want to
6309
         * report the last line in the if branch as covered if it hasn't actually executed. */
6310
0
        CG(zend_lineno) = elem_ast->lineno;
6311
0
        jmp_opnums[i] = zend_emit_jump(0);
6312
0
      }
6313
0
      zend_update_jump_target_to_next(opnum_jmpz);
6314
0
    } else {
6315
      /* "else" can only occur as last element. */
6316
0
      ZEND_ASSERT(i == list->children - 1);
6317
0
      zend_compile_stmt(stmt_ast);
6318
0
    }
6319
0
  }
6320
6321
0
  if (list->children > 1) {
6322
0
    for (i = 0; i < list->children - 1; ++i) {
6323
0
      zend_update_jump_target_to_next(jmp_opnums[i]);
6324
0
    }
6325
0
    efree(jmp_opnums);
6326
0
  }
6327
0
}
6328
/* }}} */
6329
6330
0
static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) {
6331
0
  uint32_t i;
6332
0
  uint8_t common_type = IS_UNDEF;
6333
0
  for (i = 0; i < cases->children; i++) {
6334
0
    zend_ast *case_ast = cases->child[i];
6335
0
    zend_ast **cond_ast = &case_ast->child[0];
6336
0
    const zval *cond_zv;
6337
0
    if (!case_ast->child[0]) {
6338
      /* Skip default clause */
6339
0
      continue;
6340
0
    }
6341
6342
0
    zend_eval_const_expr(cond_ast);
6343
0
    if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6344
      /* Non-constant case */
6345
0
      return IS_UNDEF;
6346
0
    }
6347
6348
0
    cond_zv = zend_ast_get_zval(case_ast->child[0]);
6349
0
    if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6350
      /* We only optimize switched on integers and strings */
6351
0
      return IS_UNDEF;
6352
0
    }
6353
6354
0
    if (common_type == IS_UNDEF) {
6355
0
      common_type = Z_TYPE_P(cond_zv);
6356
0
    } else if (common_type != Z_TYPE_P(cond_zv)) {
6357
      /* Non-uniform case types */
6358
0
      return IS_UNDEF;
6359
0
    }
6360
6361
0
    if (Z_TYPE_P(cond_zv) == IS_STRING
6362
0
        && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) {
6363
      /* Numeric strings cannot be compared with a simple hash lookup */
6364
0
      return IS_UNDEF;
6365
0
    }
6366
0
  }
6367
6368
0
  return common_type;
6369
0
}
6370
6371
0
static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) {
6372
0
  if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) {
6373
0
    return 0;
6374
0
  }
6375
6376
  /* Thresholds are chosen based on when the average switch time for equidistributed
6377
   * input becomes smaller when using the jumptable optimization. */
6378
0
  if (jumptable_type == IS_LONG) {
6379
0
    return cases->children >= 5;
6380
0
  } else {
6381
0
    ZEND_ASSERT(jumptable_type == IS_STRING);
6382
0
    return cases->children >= 2;
6383
0
  }
6384
0
}
6385
6386
static void zend_compile_switch(zend_ast *ast) /* {{{ */
6387
0
{
6388
0
  zend_ast *expr_ast = ast->child[0];
6389
0
  zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
6390
6391
0
  uint32_t i;
6392
0
  bool has_default_case = false;
6393
6394
0
  znode expr_node, case_node;
6395
0
  zend_op *opline;
6396
0
  uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1;
6397
0
  uint8_t jumptable_type;
6398
0
  HashTable *jumptable = NULL;
6399
6400
0
  zend_compile_expr(&expr_node, expr_ast);
6401
6402
0
  zend_begin_loop(ZEND_FREE, &expr_node, true);
6403
6404
0
  case_node.op_type = IS_TMP_VAR;
6405
0
  case_node.u.op.var = get_temporary_variable();
6406
6407
0
  jumptable_type = determine_switch_jumptable_type(cases);
6408
0
  if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) {
6409
0
    znode jumptable_op;
6410
6411
0
    ALLOC_HASHTABLE(jumptable);
6412
0
    zend_hash_init(jumptable, cases->children, NULL, NULL, 0);
6413
0
    jumptable_op.op_type = IS_CONST;
6414
0
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6415
6416
0
    opline = zend_emit_op(NULL,
6417
0
      jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING,
6418
0
      &expr_node, &jumptable_op);
6419
0
    if (opline->op1_type == IS_CONST) {
6420
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6421
0
    }
6422
0
    opnum_switch = opline - CG(active_op_array)->opcodes;
6423
0
  }
6424
6425
0
  jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
6426
0
  for (i = 0; i < cases->children; ++i) {
6427
0
    zend_ast *case_ast = cases->child[i];
6428
0
    zend_ast *cond_ast = case_ast->child[0];
6429
0
    znode cond_node;
6430
6431
0
    if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) {
6432
0
      CG(zend_lineno) = case_ast->lineno;
6433
0
      zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead");
6434
0
    }
6435
6436
0
    if (!cond_ast) {
6437
0
      if (has_default_case) {
6438
0
        CG(zend_lineno) = case_ast->lineno;
6439
0
        zend_error_noreturn(E_COMPILE_ERROR,
6440
0
          "Switch statements may only contain one default clause");
6441
0
      }
6442
0
      has_default_case = true;
6443
0
      continue;
6444
0
    }
6445
6446
0
    zend_compile_expr(&cond_node, cond_ast);
6447
6448
0
    if (expr_node.op_type == IS_CONST
6449
0
      && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
6450
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6451
0
    } else if (expr_node.op_type == IS_CONST
6452
0
      && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
6453
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
6454
0
    } else {
6455
0
      opline = zend_emit_op(NULL,
6456
0
        (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
6457
0
        &expr_node, &cond_node);
6458
0
      SET_NODE(opline->result, &case_node);
6459
0
      if (opline->op1_type == IS_CONST) {
6460
0
        Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6461
0
      }
6462
6463
0
      jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6464
0
    }
6465
0
  }
6466
6467
0
  opnum_default_jmp = zend_emit_jump(0);
6468
6469
0
  for (i = 0; i < cases->children; ++i) {
6470
0
    zend_ast *case_ast = cases->child[i];
6471
0
    zend_ast *cond_ast = case_ast->child[0];
6472
0
    zend_ast *stmt_ast = case_ast->child[1];
6473
6474
0
    if (cond_ast) {
6475
0
      zend_update_jump_target_to_next(jmpnz_opnums[i]);
6476
6477
0
      if (jumptable) {
6478
0
        zval *cond_zv = zend_ast_get_zval(cond_ast);
6479
0
        zval jmp_target;
6480
0
        ZVAL_LONG(&jmp_target, get_next_op_number());
6481
6482
0
        ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type);
6483
0
        if (Z_TYPE_P(cond_zv) == IS_LONG) {
6484
0
          zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6485
0
        } else {
6486
0
          ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6487
0
          zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6488
0
        }
6489
0
      }
6490
0
    } else {
6491
0
      zend_update_jump_target_to_next(opnum_default_jmp);
6492
6493
0
      if (jumptable) {
6494
0
        ZEND_ASSERT(opnum_switch != (uint32_t)-1);
6495
0
        opline = &CG(active_op_array)->opcodes[opnum_switch];
6496
0
        opline->extended_value = get_next_op_number();
6497
0
      }
6498
0
    }
6499
6500
0
    zend_compile_stmt(stmt_ast);
6501
0
  }
6502
6503
0
  if (!has_default_case) {
6504
0
    zend_update_jump_target_to_next(opnum_default_jmp);
6505
6506
0
    if (jumptable) {
6507
0
      opline = &CG(active_op_array)->opcodes[opnum_switch];
6508
0
      opline->extended_value = get_next_op_number();
6509
0
    }
6510
0
  }
6511
6512
0
  zend_end_loop(get_next_op_number(), &expr_node);
6513
6514
0
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6515
0
    opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6516
0
    opline->extended_value = ZEND_FREE_SWITCH;
6517
0
  } else if (expr_node.op_type == IS_CONST) {
6518
0
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6519
0
  }
6520
6521
0
  efree(jmpnz_opnums);
6522
0
}
6523
/* }}} */
6524
6525
static uint32_t count_match_conds(const zend_ast_list *arms)
6526
0
{
6527
0
  uint32_t num_conds = 0;
6528
6529
0
  for (uint32_t i = 0; i < arms->children; i++) {
6530
0
    const zend_ast *arm_ast = arms->child[i];
6531
0
    if (arm_ast->child[0] == NULL) {
6532
0
      continue;
6533
0
    }
6534
6535
0
    const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6536
0
    num_conds += conds->children;
6537
0
  }
6538
6539
0
  return num_conds;
6540
0
}
6541
6542
0
static bool can_match_use_jumptable(const zend_ast_list *arms) {
6543
0
  for (uint32_t i = 0; i < arms->children; i++) {
6544
0
    const zend_ast *arm_ast = arms->child[i];
6545
0
    if (!arm_ast->child[0]) {
6546
      /* Skip default arm */
6547
0
      continue;
6548
0
    }
6549
6550
0
    zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6551
0
    for (uint32_t j = 0; j < conds->children; j++) {
6552
0
      zend_ast **cond_ast = &conds->child[j];
6553
6554
0
      zend_eval_const_expr(cond_ast);
6555
0
      if ((*cond_ast)->kind != ZEND_AST_ZVAL) {
6556
0
        return 0;
6557
0
      }
6558
6559
0
      const zval *cond_zv = zend_ast_get_zval(*cond_ast);
6560
0
      if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) {
6561
0
        return 0;
6562
0
      }
6563
0
    }
6564
0
  }
6565
6566
0
  return 1;
6567
0
}
6568
6569
static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast)
6570
0
{
6571
0
  if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) {
6572
    /* Assert compilation adds a message operand, but this is incompatible with the
6573
     * pipe optimization that uses a temporary znode for the reference elimination.
6574
     * Therefore, disable the optimization for assert.
6575
     * Note that "assert" as a name is always treated as fully qualified. */
6576
0
    return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert");
6577
0
  }
6578
6579
0
  return true;
6580
0
}
6581
6582
static void zend_compile_pipe(znode *result, zend_ast *ast)
6583
0
{
6584
0
  zend_ast *operand_ast = ast->child[0];
6585
0
  zend_ast *callable_ast = ast->child[1];
6586
6587
0
  if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) {
6588
0
    zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized");
6589
0
  }
6590
6591
  /* Compile the left hand side down to a value first. */
6592
0
  znode operand_result;
6593
0
  zend_compile_expr(&operand_result, operand_ast);
6594
6595
  /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references
6596
   * always fail. They will already fail in complex cases like arrays,
6597
   * so those don't need a wrapper. */
6598
0
  znode wrapped_operand_result;
6599
0
  if (operand_result.op_type & (IS_CV|IS_VAR)) {
6600
0
    zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL);
6601
0
  } else {
6602
0
    wrapped_operand_result = operand_result;
6603
0
  }
6604
6605
  /* Turn the operand into a function parameter list. */
6606
0
  zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result));
6607
6608
0
  zend_ast *fcall_ast;
6609
0
  znode callable_result;
6610
6611
  /* Turn $foo |> bar(...) into bar($foo). */
6612
0
  if (callable_ast->kind == ZEND_AST_CALL
6613
0
    && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT
6614
0
    && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) {
6615
0
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6616
0
        callable_ast->child[0], arg_list_ast);
6617
  /* Turn $foo |> bar::baz(...) into bar::baz($foo). */
6618
0
  } else if (callable_ast->kind == ZEND_AST_STATIC_CALL
6619
0
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6620
0
    fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL,
6621
0
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6622
  /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */
6623
0
  } else if (callable_ast->kind == ZEND_AST_METHOD_CALL
6624
0
      && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) {
6625
0
    fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL,
6626
0
      callable_ast->child[0], callable_ast->child[1], arg_list_ast);
6627
  /* Turn $foo |> $expr into ($expr)($foo) */
6628
0
  } else {
6629
0
    zend_compile_expr(&callable_result, callable_ast);
6630
0
    callable_ast = zend_ast_create_znode(&callable_result);
6631
0
    fcall_ast = zend_ast_create(ZEND_AST_CALL,
6632
0
      callable_ast, arg_list_ast);
6633
0
  }
6634
6635
0
  zend_do_extended_stmt(&operand_result);
6636
6637
0
  zend_compile_expr(result, fcall_ast);
6638
0
}
6639
6640
static void zend_compile_match(znode *result, zend_ast *ast)
6641
0
{
6642
0
  zend_ast *expr_ast = ast->child[0];
6643
0
  zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
6644
0
  bool has_default_arm = false;
6645
0
  uint32_t opnum_match = (uint32_t)-1;
6646
6647
0
  znode expr_node;
6648
0
  zend_compile_expr(&expr_node, expr_ast);
6649
6650
0
  znode case_node;
6651
0
  case_node.op_type = IS_TMP_VAR;
6652
0
  case_node.u.op.var = get_temporary_variable();
6653
6654
0
  uint32_t num_conds = count_match_conds(arms);
6655
0
  uint8_t can_use_jumptable = can_match_use_jumptable(arms);
6656
0
  bool uses_jumptable = can_use_jumptable && num_conds >= 2;
6657
0
  HashTable *jumptable = NULL;
6658
0
  uint32_t *jmpnz_opnums = NULL;
6659
6660
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6661
0
    zend_ast *arm_ast = arms->child[i];
6662
6663
0
    if (!arm_ast->child[0]) {
6664
0
      if (has_default_arm) {
6665
0
        CG(zend_lineno) = arm_ast->lineno;
6666
0
        zend_error_noreturn(E_COMPILE_ERROR,
6667
0
          "Match expressions may only contain one default arm");
6668
0
      }
6669
0
      has_default_arm = true;
6670
0
    }
6671
0
  }
6672
6673
0
  if (uses_jumptable) {
6674
0
    znode jumptable_op;
6675
6676
0
    ALLOC_HASHTABLE(jumptable);
6677
0
    zend_hash_init(jumptable, num_conds, NULL, NULL, 0);
6678
0
    jumptable_op.op_type = IS_CONST;
6679
0
    ZVAL_ARR(&jumptable_op.u.constant, jumptable);
6680
6681
0
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op);
6682
0
    if (opline->op1_type == IS_CONST) {
6683
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6684
0
    }
6685
0
    opnum_match = opline - CG(active_op_array)->opcodes;
6686
0
  } else {
6687
0
    jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0);
6688
0
    uint32_t cond_count = 0;
6689
0
    for (uint32_t i = 0; i < arms->children; ++i) {
6690
0
      zend_ast *arm_ast = arms->child[i];
6691
6692
0
      if (!arm_ast->child[0]) {
6693
0
        continue;
6694
0
      }
6695
6696
0
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6697
0
      for (uint32_t j = 0; j < conds->children; j++) {
6698
0
        zend_ast *cond_ast = conds->child[j];
6699
6700
0
        znode cond_node;
6701
0
        zend_compile_expr(&cond_node, cond_ast);
6702
6703
0
        uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL;
6704
0
        zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node);
6705
0
        SET_NODE(opline->result, &case_node);
6706
0
        if (opline->op1_type == IS_CONST) {
6707
0
          Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6708
0
        }
6709
6710
0
        jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
6711
6712
0
        cond_count++;
6713
0
      }
6714
0
    }
6715
0
  }
6716
6717
0
  uint32_t opnum_default_jmp = 0;
6718
0
  if (!uses_jumptable) {
6719
0
    opnum_default_jmp = zend_emit_jump(0);
6720
0
  }
6721
6722
0
  bool is_first_case = true;
6723
0
  uint32_t cond_count = 0;
6724
0
  uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0);
6725
6726
  // The generated default arm is emitted first to avoid live range issues where the tmpvar
6727
  // for the arm result is freed even though it has not been initialized yet.
6728
0
  if (!has_default_arm) {
6729
0
    if (!uses_jumptable) {
6730
0
      zend_update_jump_target_to_next(opnum_default_jmp);
6731
0
    }
6732
6733
0
    if (jumptable) {
6734
0
      zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6735
0
      opline->extended_value = get_next_op_number();
6736
0
    }
6737
6738
0
    zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL);
6739
0
    if (opline->op1_type == IS_CONST) {
6740
0
      Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
6741
0
    }
6742
0
    if (arms->children == 0) {
6743
      /* Mark this as an "expression throw" for opcache. */
6744
0
      opline->extended_value = ZEND_THROW_IS_EXPR;
6745
0
    }
6746
0
  }
6747
6748
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6749
0
    zend_ast *arm_ast = arms->child[i];
6750
0
    zend_ast *body_ast = arm_ast->child[1];
6751
6752
0
    if (arm_ast->child[0] != NULL) {
6753
0
      zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]);
6754
6755
0
      for (uint32_t j = 0; j < conds->children; j++) {
6756
0
        zend_ast *cond_ast = conds->child[j];
6757
6758
0
        if (jmpnz_opnums != NULL) {
6759
0
          zend_update_jump_target_to_next(jmpnz_opnums[cond_count]);
6760
0
        }
6761
6762
0
        if (jumptable) {
6763
0
          zval *cond_zv = zend_ast_get_zval(cond_ast);
6764
0
          zval jmp_target;
6765
0
          ZVAL_LONG(&jmp_target, get_next_op_number());
6766
6767
0
          if (Z_TYPE_P(cond_zv) == IS_LONG) {
6768
0
            zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target);
6769
0
          } else {
6770
0
            ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING);
6771
0
            zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target);
6772
0
          }
6773
0
        }
6774
6775
0
        cond_count++;
6776
0
      }
6777
0
    } else {
6778
0
      if (!uses_jumptable) {
6779
0
        zend_update_jump_target_to_next(opnum_default_jmp);
6780
0
      }
6781
6782
0
      if (jumptable) {
6783
0
        ZEND_ASSERT(opnum_match != (uint32_t)-1);
6784
0
        zend_op *opline = &CG(active_op_array)->opcodes[opnum_match];
6785
0
        opline->extended_value = get_next_op_number();
6786
0
      }
6787
0
    }
6788
6789
0
    znode body_node;
6790
0
    zend_compile_expr(&body_node, body_ast);
6791
6792
0
    if (is_first_case) {
6793
0
      zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
6794
0
      is_first_case = false;
6795
0
    } else {
6796
0
      zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
6797
0
      SET_NODE(opline_qm_assign->result, result);
6798
0
    }
6799
6800
0
    jmp_end_opnums[i] = zend_emit_jump(0);
6801
0
  }
6802
6803
  // Initialize result in case there is no arm
6804
0
  if (arms->children == 0) {
6805
0
    result->op_type = IS_CONST;
6806
0
    ZVAL_NULL(&result->u.constant);
6807
0
  }
6808
6809
0
  for (uint32_t i = 0; i < arms->children; ++i) {
6810
0
    zend_update_jump_target_to_next(jmp_end_opnums[i]);
6811
0
  }
6812
6813
0
  if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
6814
0
    zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
6815
0
    opline->extended_value = ZEND_FREE_SWITCH;
6816
0
  } else if (expr_node.op_type == IS_CONST) {
6817
0
    zval_ptr_dtor_nogc(&expr_node.u.constant);
6818
0
  }
6819
6820
0
  if (jmpnz_opnums != NULL) {
6821
0
    efree(jmpnz_opnums);
6822
0
  }
6823
0
  efree(jmp_end_opnums);
6824
0
}
6825
6826
static void zend_compile_try(const zend_ast *ast) /* {{{ */
6827
0
{
6828
0
  zend_ast *try_ast = ast->child[0];
6829
0
  const zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
6830
0
  zend_ast *finally_ast = ast->child[2];
6831
6832
0
  uint32_t i, j;
6833
0
  zend_op *opline;
6834
0
  uint32_t try_catch_offset;
6835
0
  uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
6836
0
  uint32_t orig_fast_call_var = CG(context).fast_call_var;
6837
0
  uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
6838
6839
0
  if (catches->children == 0 && !finally_ast) {
6840
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
6841
0
  }
6842
6843
  /* label: try { } must not be equal to try { label: } */
6844
0
  if (CG(context).labels) {
6845
0
    zend_label *label;
6846
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) {
6847
0
      if (label->opline_num == get_next_op_number()) {
6848
0
        zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
6849
0
      }
6850
0
      break;
6851
0
    } ZEND_HASH_FOREACH_END();
6852
0
  }
6853
6854
0
  try_catch_offset = zend_add_try_element(get_next_op_number());
6855
6856
0
  if (finally_ast) {
6857
0
    zend_loop_var fast_call;
6858
0
    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
6859
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
6860
0
    }
6861
0
    CG(context).fast_call_var = get_temporary_variable();
6862
6863
    /* Push FAST_CALL on unwind stack */
6864
0
    fast_call.opcode = ZEND_FAST_CALL;
6865
0
    fast_call.var_type = IS_TMP_VAR;
6866
0
    fast_call.var_num = CG(context).fast_call_var;
6867
0
    fast_call.try_catch_offset = try_catch_offset;
6868
0
    zend_stack_push(&CG(loop_var_stack), &fast_call);
6869
0
  }
6870
6871
0
  CG(context).try_catch_offset = try_catch_offset;
6872
6873
0
  zend_compile_stmt(try_ast);
6874
6875
0
  if (catches->children != 0) {
6876
0
    jmp_opnums[0] = zend_emit_jump(0);
6877
0
  }
6878
6879
0
  for (i = 0; i < catches->children; ++i) {
6880
0
    const zend_ast *catch_ast = catches->child[i];
6881
0
    const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
6882
0
    zend_ast *var_ast = catch_ast->child[1];
6883
0
    zend_ast *stmt_ast = catch_ast->child[2];
6884
0
    zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL;
6885
0
    bool is_last_catch = (i + 1 == catches->children);
6886
6887
0
    uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
6888
0
    uint32_t opnum_catch = (uint32_t)-1;
6889
6890
0
    CG(zend_lineno) = catch_ast->lineno;
6891
6892
0
    for (j = 0; j < classes->children; j++) {
6893
0
      zend_ast *class_ast = classes->child[j];
6894
0
      bool is_last_class = (j + 1 == classes->children);
6895
6896
0
      if (!zend_is_const_default_class_ref(class_ast)) {
6897
0
        zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
6898
0
      }
6899
6900
0
      opnum_catch = get_next_op_number();
6901
0
      if (i == 0 && j == 0) {
6902
0
        CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
6903
0
      }
6904
6905
0
      opline = get_next_op();
6906
0
      opline->opcode = ZEND_CATCH;
6907
0
      opline->op1_type = IS_CONST;
6908
0
      opline->op1.constant = zend_add_class_name_literal(
6909
0
          zend_resolve_class_name_ast(class_ast));
6910
0
      opline->extended_value = zend_alloc_cache_slot();
6911
6912
0
      if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
6913
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
6914
0
      }
6915
6916
0
      opline->result_type = var_name ? IS_CV : IS_UNUSED;
6917
0
      opline->result.var = var_name ? lookup_cv(var_name) : -1;
6918
6919
0
      if (is_last_catch && is_last_class) {
6920
0
        opline->extended_value |= ZEND_LAST_CATCH;
6921
0
      }
6922
6923
0
      if (!is_last_class) {
6924
0
        jmp_multicatch[j] = zend_emit_jump(0);
6925
0
        opline = &CG(active_op_array)->opcodes[opnum_catch];
6926
0
        opline->op2.opline_num = get_next_op_number();
6927
0
      }
6928
0
    }
6929
6930
0
    for (j = 0; j < classes->children - 1; j++) {
6931
0
      zend_update_jump_target_to_next(jmp_multicatch[j]);
6932
0
    }
6933
6934
0
    efree(jmp_multicatch);
6935
6936
0
    zend_compile_stmt(stmt_ast);
6937
6938
0
    if (!is_last_catch) {
6939
0
      jmp_opnums[i + 1] = zend_emit_jump(0);
6940
0
    }
6941
6942
0
    ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class");
6943
0
    opline = &CG(active_op_array)->opcodes[opnum_catch];
6944
0
    if (!is_last_catch) {
6945
0
      opline->op2.opline_num = get_next_op_number();
6946
0
    }
6947
0
  }
6948
6949
0
  for (i = 0; i < catches->children; ++i) {
6950
0
    zend_update_jump_target_to_next(jmp_opnums[i]);
6951
0
  }
6952
6953
0
  if (finally_ast) {
6954
0
    zend_loop_var discard_exception;
6955
0
    uint32_t opnum_jmp = get_next_op_number() + 1;
6956
6957
    /* Pop FAST_CALL from unwind stack */
6958
0
    zend_stack_del_top(&CG(loop_var_stack));
6959
6960
    /* Push DISCARD_EXCEPTION on unwind stack */
6961
0
    discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
6962
0
    discard_exception.var_type = IS_TMP_VAR;
6963
0
    discard_exception.var_num = CG(context).fast_call_var;
6964
0
    zend_stack_push(&CG(loop_var_stack), &discard_exception);
6965
6966
0
    CG(zend_lineno) = finally_ast->lineno;
6967
6968
0
    opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
6969
0
    opline->op1.num = try_catch_offset;
6970
0
    opline->result_type = IS_TMP_VAR;
6971
0
    opline->result.var = CG(context).fast_call_var;
6972
6973
0
    zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
6974
6975
0
    zend_compile_stmt(finally_ast);
6976
6977
0
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
6978
0
    CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
6979
0
      = get_next_op_number();
6980
6981
0
    opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
6982
0
    opline->op1_type = IS_TMP_VAR;
6983
0
    opline->op1.var = CG(context).fast_call_var;
6984
0
    opline->op2.num = orig_try_catch_offset;
6985
6986
0
    zend_update_jump_target_to_next(opnum_jmp);
6987
6988
0
    CG(context).fast_call_var = orig_fast_call_var;
6989
6990
    /* Pop DISCARD_EXCEPTION from unwind stack */
6991
0
    zend_stack_del_top(&CG(loop_var_stack));
6992
0
  }
6993
6994
0
  CG(context).try_catch_offset = orig_try_catch_offset;
6995
6996
0
  efree(jmp_opnums);
6997
0
}
6998
/* }}} */
6999
7000
/* Encoding declarations must already be handled during parsing */
7001
bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
7002
0
{
7003
0
  const zend_ast_list *declares = zend_ast_get_list(ast);
7004
0
  uint32_t i;
7005
0
  for (i = 0; i < declares->children; ++i) {
7006
0
    const zend_ast *declare_ast = declares->child[i];
7007
0
    zend_ast *name_ast = declare_ast->child[0];
7008
0
    zend_ast *value_ast = declare_ast->child[1];
7009
0
    const zend_string *name = zend_ast_get_str(name_ast);
7010
7011
0
    if (zend_string_equals_literal_ci(name, "encoding")) {
7012
0
      if (value_ast->kind != ZEND_AST_ZVAL) {
7013
0
        zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0);
7014
0
        return 0;
7015
0
      }
7016
7017
0
      if (CG(multibyte)) {
7018
0
        zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
7019
7020
0
        const zend_encoding *new_encoding, *old_encoding;
7021
0
        zend_encoding_filter old_input_filter;
7022
7023
0
        CG(encoding_declared) = 1;
7024
7025
0
        new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
7026
0
        if (!new_encoding) {
7027
0
          zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
7028
0
        } else {
7029
0
          old_input_filter = LANG_SCNG(input_filter);
7030
0
          old_encoding = LANG_SCNG(script_encoding);
7031
0
          zend_multibyte_set_filter(new_encoding);
7032
7033
          /* need to re-scan if input filter changed */
7034
0
          if (old_input_filter != LANG_SCNG(input_filter) ||
7035
0
             (old_input_filter && new_encoding != old_encoding)) {
7036
0
            zend_multibyte_yyinput_again(old_input_filter, old_encoding);
7037
0
          }
7038
0
        }
7039
7040
0
        zend_string_release_ex(encoding_name, 0);
7041
0
      } else {
7042
0
        zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
7043
0
          "Zend multibyte feature is turned off by settings");
7044
0
      }
7045
0
    }
7046
0
  }
7047
7048
0
  return 1;
7049
0
}
7050
/* }}} */
7051
7052
/* Check whether this is the first statement, not counting declares. */
7053
static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */
7054
0
{
7055
0
  uint32_t i = 0;
7056
0
  const zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
7057
7058
0
  while (i < file_ast->children) {
7059
0
    if (file_ast->child[i] == ast) {
7060
0
      return SUCCESS;
7061
0
    } else if (file_ast->child[i] == NULL) {
7062
0
      if (!allow_nop) {
7063
0
        return FAILURE;
7064
0
      }
7065
0
    } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
7066
0
      return FAILURE;
7067
0
    }
7068
0
    i++;
7069
0
  }
7070
0
  return FAILURE;
7071
0
}
7072
/* }}} */
7073
7074
static void zend_compile_declare(const zend_ast *ast) /* {{{ */
7075
0
{
7076
0
  const zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
7077
0
  zend_ast *stmt_ast = ast->child[1];
7078
0
  zend_declarables orig_declarables = FC(declarables);
7079
0
  uint32_t i;
7080
7081
0
  for (i = 0; i < declares->children; ++i) {
7082
0
    zend_ast *declare_ast = declares->child[i];
7083
0
    zend_ast *name_ast = declare_ast->child[0];
7084
0
    zend_ast **value_ast_ptr = &declare_ast->child[1];
7085
0
    zend_string *name = zend_ast_get_str(name_ast);
7086
7087
0
    if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) {
7088
0
      zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
7089
0
    }
7090
7091
0
    if (zend_string_equals_literal_ci(name, "ticks")) {
7092
0
      zval value_zv;
7093
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7094
0
      FC(declarables).ticks = zval_get_long(&value_zv);
7095
0
      zval_ptr_dtor_nogc(&value_zv);
7096
0
    } else if (zend_string_equals_literal_ci(name, "encoding")) {
7097
7098
0
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) {
7099
0
        zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
7100
0
          "the very first statement in the script");
7101
0
      }
7102
0
    } else if (zend_string_equals_literal_ci(name, "strict_types")) {
7103
0
      zval value_zv;
7104
7105
0
      if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
7106
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
7107
0
          "the very first statement in the script");
7108
0
      }
7109
7110
0
      if (ast->child[1] != NULL) {
7111
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
7112
0
          "use block mode");
7113
0
      }
7114
7115
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
7116
7117
0
      if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
7118
0
        zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
7119
0
      }
7120
7121
0
      if (Z_LVAL(value_zv) == 1) {
7122
0
        CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
7123
0
      }
7124
7125
0
    } else {
7126
0
      zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
7127
0
    }
7128
0
  }
7129
7130
0
  if (stmt_ast) {
7131
0
    zend_compile_stmt(stmt_ast);
7132
7133
0
    FC(declarables) = orig_declarables;
7134
0
  }
7135
0
}
7136
/* }}} */
7137
7138
static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
7139
0
{
7140
0
  const zend_ast_list *list = zend_ast_get_list(ast);
7141
0
  uint32_t i;
7142
0
  for (i = 0; i < list->children; ++i) {
7143
0
    zend_compile_stmt(list->child[i]);
7144
0
  }
7145
0
}
7146
/* }}} */
7147
7148
ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
7149
3.34k
{
7150
3.34k
  uint32_t i, n;
7151
7152
3.34k
  func->common.arg_flags[0] = 0;
7153
3.34k
  func->common.arg_flags[1] = 0;
7154
3.34k
  func->common.arg_flags[2] = 0;
7155
3.34k
  if (func->common.arg_info) {
7156
3.34k
    n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
7157
3.34k
    i = 0;
7158
6.78k
    while (i < n) {
7159
3.44k
      ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i]));
7160
3.44k
      i++;
7161
3.44k
    }
7162
3.34k
    if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) {
7163
8
      uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]);
7164
92
      while (i < MAX_ARG_FLAG_NUM) {
7165
84
        ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
7166
84
        i++;
7167
84
      }
7168
8
    }
7169
3.34k
  }
7170
3.34k
}
7171
/* }}} */
7172
7173
static zend_type zend_compile_single_typename(zend_ast *ast)
7174
0
{
7175
0
  ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE));
7176
0
  if (ast->kind == ZEND_AST_TYPE) {
7177
0
    if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) {
7178
0
      zend_error_noreturn(E_COMPILE_ERROR,
7179
0
        "Cannot use \"static\" when no class scope is active");
7180
0
    }
7181
7182
0
    return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0);
7183
0
  } else {
7184
0
    zend_string *type_name = zend_ast_get_str(ast);
7185
0
    uint8_t type_code = zend_lookup_builtin_type_by_name(type_name);
7186
7187
0
    if (type_code != 0) {
7188
0
      if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
7189
0
        zend_error_noreturn(E_COMPILE_ERROR,
7190
0
          "Type declaration '%s' must be unqualified",
7191
0
          ZSTR_VAL(zend_string_tolower(type_name)));
7192
0
      }
7193
7194
      /* Transform iterable into a type union alias */
7195
0
      if (type_code == IS_ITERABLE) {
7196
        /* Set iterable bit for BC compat during Reflection and string representation of type */
7197
0
        zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE),
7198
0
                  (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT));
7199
0
        return iterable;
7200
0
      }
7201
7202
0
      return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0);
7203
0
    } else {
7204
0
      const char *correct_name;
7205
0
      uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
7206
0
      zend_string *class_name = type_name;
7207
7208
0
      if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
7209
0
        class_name = zend_resolve_class_name_ast(ast);
7210
0
        zend_assert_valid_class_name(class_name, "a type name");
7211
0
      } else {
7212
0
        ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT);
7213
7214
0
        zend_ensure_valid_class_fetch_type(fetch_type);
7215
7216
0
        bool substitute_self_parent = zend_is_scope_known()
7217
0
          && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS);
7218
7219
0
        if (fetch_type == ZEND_FETCH_CLASS_SELF) {
7220
          /* Scope might be unknown for unbound closures and traits */
7221
0
          if (substitute_self_parent) {
7222
0
            class_name = CG(active_class_entry)->name;
7223
0
            ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time");
7224
0
          }
7225
0
        } else {
7226
0
          ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT);
7227
          /* Scope might be unknown for unbound closures and traits */
7228
0
          if (substitute_self_parent) {
7229
0
            class_name = CG(active_class_entry)->parent_name;
7230
0
            ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time");
7231
0
          }
7232
0
        }
7233
0
        zend_string_addref(class_name);
7234
0
      }
7235
7236
0
      if (ast->attr == ZEND_NAME_NOT_FQ
7237
0
          && zend_is_confusable_type(type_name, &correct_name)
7238
0
          && zend_is_not_imported(type_name)) {
7239
0
        const char *extra =
7240
0
          FC(current_namespace) ? " or import the class with \"use\"" : "";
7241
0
        if (correct_name) {
7242
0
          zend_error(E_COMPILE_WARNING,
7243
0
            "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? "
7244
0
            "Write \"\\%s\"%s to suppress this warning",
7245
0
            ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra);
7246
0
        } else {
7247
0
          zend_error(E_COMPILE_WARNING,
7248
0
            "\"%s\" is not a supported builtin type "
7249
0
            "and will be interpreted as a class name. "
7250
0
            "Write \"\\%s\"%s to suppress this warning",
7251
0
            ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra);
7252
0
        }
7253
0
      }
7254
7255
0
      class_name = zend_new_interned_string(class_name);
7256
0
      zend_alloc_ce_cache(class_name);
7257
0
      return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0);
7258
0
    }
7259
0
  }
7260
0
}
7261
7262
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
7263
0
{
7264
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
7265
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
7266
0
  const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type);
7267
0
  const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type);
7268
0
  const zend_type_list *smaller_type_list, *larger_type_list;
7269
0
  bool flipped = false;
7270
7271
0
  if (r_type_list->num_types < l_type_list->num_types) {
7272
0
    smaller_type_list = r_type_list;
7273
0
    larger_type_list = l_type_list;
7274
0
    flipped = true;
7275
0
  } else {
7276
0
    smaller_type_list = l_type_list;
7277
0
    larger_type_list = r_type_list;
7278
0
  }
7279
7280
0
  unsigned int sum = 0;
7281
0
  const zend_type *outer_type;
7282
0
  ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
7283
0
    const zend_type *inner_type;
7284
0
    ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
7285
0
      if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
7286
0
        sum++;
7287
0
        break;
7288
0
      }
7289
0
    ZEND_TYPE_LIST_FOREACH_END();
7290
0
  ZEND_TYPE_LIST_FOREACH_END();
7291
7292
0
  if (sum == smaller_type_list->num_types) {
7293
0
    zend_string *smaller_type_str;
7294
0
    zend_string *larger_type_str;
7295
0
    if (flipped) {
7296
0
      smaller_type_str = zend_type_to_string(right_type);
7297
0
      larger_type_str = zend_type_to_string(left_type);
7298
0
    } else {
7299
0
      smaller_type_str = zend_type_to_string(left_type);
7300
0
      larger_type_str = zend_type_to_string(right_type);
7301
0
    }
7302
0
    if (smaller_type_list->num_types == larger_type_list->num_types) {
7303
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s",
7304
0
        ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str));
7305
0
    } else {
7306
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7307
0
        ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str));
7308
0
    }
7309
0
  }
7310
0
}
7311
7312
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
7313
0
{
7314
0
  ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
7315
0
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));
7316
7317
0
  const zend_type *single_intersection_type = NULL;
7318
0
  ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
7319
0
    if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
7320
0
      zend_string *single_type_str = zend_type_to_string(single_type);
7321
0
      zend_string *complete_type = zend_type_to_string(intersection_type);
7322
0
      zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s",
7323
0
          ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str));
7324
0
    }
7325
0
  ZEND_TYPE_FOREACH_END();
7326
0
}
7327
7328
/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
7329
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
7330
0
{
7331
0
  ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
7332
0
  for (size_t i = 0; i < type_list->num_types - 1; i++) {
7333
0
    if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7334
0
      zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type);
7335
0
      continue;
7336
0
    }
7337
0
    if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) {
7338
0
      zend_string *single_type_str = zend_type_to_string(type);
7339
0
      zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str));
7340
0
    }
7341
0
  }
7342
0
}
7343
7344
static zend_type zend_compile_typename(zend_ast *ast);
7345
7346
static zend_type zend_compile_typename_ex(
7347
    zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */
7348
0
{
7349
0
  bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE;
7350
0
  zend_ast_attr orig_ast_attr = ast->attr;
7351
0
  zend_type type = ZEND_TYPE_INIT_NONE(0);
7352
7353
0
  if (is_marked_nullable) {
7354
0
    ast->attr &= ~ZEND_TYPE_NULLABLE;
7355
0
  }
7356
7357
0
  if (ast->kind == ZEND_AST_TYPE_UNION) {
7358
0
    const zend_ast_list *list = zend_ast_get_list(ast);
7359
0
    zend_type_list *type_list;
7360
0
    bool is_composite = false;
7361
0
    bool has_only_iterable_class = true;
7362
0
    ALLOCA_FLAG(use_heap)
7363
7364
0
    type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap);
7365
0
    type_list->num_types = 0;
7366
7367
0
    for (uint32_t i = 0; i < list->children; i++) {
7368
0
      zend_ast *type_ast = list->child[i];
7369
0
      zend_type single_type;
7370
0
      uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7371
7372
0
      if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7373
0
        has_only_iterable_class = false;
7374
0
        is_composite = true;
7375
        /* The first class type can be stored directly as the type ptr payload. */
7376
0
        if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) {
7377
          /* Switch from single name to name list. */
7378
0
          type_list->num_types = 1;
7379
0
          type_list->types[0] = type;
7380
          /* Clear MAY_BE_* type flags */
7381
0
          ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7382
0
        }
7383
        /* Mark type as list type */
7384
0
        ZEND_TYPE_SET_LIST(type, type_list);
7385
7386
0
        single_type = zend_compile_typename(type_ast);
7387
0
        ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type));
7388
7389
0
        type_list->types[type_list->num_types++] = single_type;
7390
7391
        /* Check for trivially redundant class types */
7392
0
        for (size_t i = 0; i < type_list->num_types - 1; i++) {
7393
0
          if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) {
7394
0
            zend_are_intersection_types_redundant(single_type, type_list->types[i]);
7395
0
            continue;
7396
0
          }
7397
          /* Type from type list is a simple type */
7398
0
          zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]);
7399
0
        }
7400
0
        continue;
7401
0
      }
7402
7403
0
      single_type = zend_compile_single_typename(type_ast);
7404
0
      uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type);
7405
7406
0
      if (single_type_mask == MAY_BE_ANY) {
7407
0
        zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type");
7408
0
      }
7409
0
      if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7410
0
        has_only_iterable_class = false;
7411
0
      }
7412
7413
0
      uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask;
7414
0
      if (type_mask_overlap) {
7415
0
        zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap);
7416
0
        zend_string *overlap_type_str = zend_type_to_string(overlap_type);
7417
0
        zend_error_noreturn(E_COMPILE_ERROR,
7418
0
          "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str));
7419
0
      }
7420
7421
0
      if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE))
7422
0
          || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) {
7423
0
        zend_error_noreturn(E_COMPILE_ERROR,
7424
0
          "Type contains both true and false, bool must be used instead");
7425
0
      }
7426
0
      ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type);
7427
      /* Clear MAY_BE_* type flags */
7428
0
      ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK;
7429
7430
0
      if (ZEND_TYPE_IS_COMPLEX(single_type)) {
7431
0
        if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) {
7432
          /* The first class type can be stored directly as the type ptr payload. */
7433
0
          ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type));
7434
0
          ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT;
7435
0
        } else {
7436
0
          if (type_list->num_types == 0) {
7437
            /* Switch from single name to name list. */
7438
0
            type_list->num_types = 1;
7439
0
            type_list->types[0] = type;
7440
            /* Clear MAY_BE_* type flags */
7441
0
            ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK;
7442
0
            ZEND_TYPE_SET_LIST(type, type_list);
7443
0
          }
7444
7445
0
          type_list->types[type_list->num_types++] = single_type;
7446
7447
          /* Check for trivially redundant class types */
7448
0
          zend_is_type_list_redundant_by_single_type(type_list, single_type);
7449
0
        }
7450
0
      }
7451
0
    }
7452
7453
0
    if (type_list->num_types) {
7454
0
      zend_type_list *list = zend_arena_alloc(
7455
0
        &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types));
7456
0
      memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types));
7457
0
      ZEND_TYPE_SET_LIST(type, list);
7458
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7459
      /* Inform that the type list is a union type */
7460
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7461
0
    }
7462
7463
0
    free_alloca(type_list, use_heap);
7464
7465
0
    uint32_t type_mask = ZEND_TYPE_FULL_MASK(type);
7466
0
    if ((type_mask & MAY_BE_OBJECT) &&
7467
0
        ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) {
7468
0
      zend_string *type_str = zend_type_to_string(type);
7469
0
      zend_error_noreturn(E_COMPILE_ERROR,
7470
0
        "Type %s contains both object and a class type, which is redundant",
7471
0
        ZSTR_VAL(type_str));
7472
0
    }
7473
0
  } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) {
7474
0
    const zend_ast_list *list = zend_ast_get_list(ast);
7475
0
    zend_type_list *type_list;
7476
7477
    /* Allocate the type list directly on the arena as it must be a type
7478
     * list of the same number of elements as the AST list has children */
7479
0
    type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children));
7480
0
    type_list->num_types = 0;
7481
7482
0
    ZEND_ASSERT(list->children > 1);
7483
7484
0
    for (uint32_t i = 0; i < list->children; i++) {
7485
0
      zend_ast *type_ast = list->child[i];
7486
0
      zend_type single_type = zend_compile_single_typename(type_ast);
7487
7488
      /* An intersection of union types cannot exist so invalidate it
7489
       * Currently only can happen with iterable getting canonicalized to Traversable|array */
7490
0
      if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) {
7491
0
        zend_string *standard_type_str = zend_type_to_string(single_type);
7492
0
        zend_error_noreturn(E_COMPILE_ERROR,
7493
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7494
0
        zend_string_release_ex(standard_type_str, false);
7495
0
      }
7496
      /* An intersection of standard types cannot exist so invalidate it */
7497
0
      if (ZEND_TYPE_IS_ONLY_MASK(single_type)) {
7498
0
        zend_string *standard_type_str = zend_type_to_string(single_type);
7499
0
        zend_error_noreturn(E_COMPILE_ERROR,
7500
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str));
7501
0
        zend_string_release_ex(standard_type_str, false);
7502
0
      }
7503
      /* Check for "self" and "parent" too */
7504
0
      if (
7505
0
        zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF))
7506
0
        || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT))
7507
0
      ) {
7508
0
        zend_error_noreturn(E_COMPILE_ERROR,
7509
0
          "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type)));
7510
0
      }
7511
7512
      /* Add type to the type list */
7513
0
      type_list->types[type_list->num_types++] = single_type;
7514
7515
      /* Check for trivially redundant class types */
7516
0
      zend_is_type_list_redundant_by_single_type(type_list, single_type);
7517
0
    }
7518
7519
0
    ZEND_ASSERT(list->children == type_list->num_types);
7520
7521
    /* An implicitly nullable intersection type needs to be converted to a DNF type */
7522
0
    if (force_allow_null) {
7523
0
      zend_type intersection_type = ZEND_TYPE_INIT_NONE(0);
7524
0
      ZEND_TYPE_SET_LIST(intersection_type, type_list);
7525
0
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT;
7526
0
      ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT;
7527
7528
0
      zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1));
7529
0
      dnf_type_list->num_types = 1;
7530
0
      dnf_type_list->types[0] = intersection_type;
7531
0
      ZEND_TYPE_SET_LIST(type, dnf_type_list);
7532
      /* Inform that the type list is a DNF type */
7533
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT;
7534
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7535
0
    } else {
7536
0
      ZEND_TYPE_SET_LIST(type, type_list);
7537
      /* Inform that the type list is an intersection type */
7538
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT;
7539
0
      ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT;
7540
0
    }
7541
0
  } else {
7542
0
    type = zend_compile_single_typename(ast);
7543
0
  }
7544
7545
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
7546
7547
0
  if (type_mask == MAY_BE_ANY && is_marked_nullable) {
7548
0
    zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null");
7549
0
  }
7550
7551
0
  if ((type_mask & MAY_BE_NULL) && is_marked_nullable) {
7552
0
    zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable");
7553
0
  }
7554
7555
0
  if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) {
7556
0
    *forced_allow_null = true;
7557
0
  }
7558
7559
0
  if (is_marked_nullable || force_allow_null) {
7560
0
    ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
7561
0
    type_mask = ZEND_TYPE_PURE_MASK(type);
7562
0
  }
7563
7564
0
  if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) {
7565
0
    zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type");
7566
0
  }
7567
7568
0
  if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) {
7569
0
    zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type");
7570
0
  }
7571
7572
0
  ast->attr = orig_ast_attr;
7573
0
  return type;
7574
0
}
7575
/* }}} */
7576
7577
static zend_type zend_compile_typename(zend_ast *ast)
7578
0
{
7579
0
  bool forced_allow_null;
7580
0
  return zend_compile_typename_ex(ast, false, &forced_allow_null);
7581
0
}
7582
7583
/* May convert value from int to float. */
7584
static bool zend_is_valid_default_value(zend_type type, zval *value)
7585
0
{
7586
0
  ZEND_ASSERT(ZEND_TYPE_IS_SET(type));
7587
0
  if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) {
7588
0
    return 1;
7589
0
  }
7590
0
  if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) {
7591
    /* Integers are allowed as initializers for floating-point values. */
7592
0
    convert_to_double(value);
7593
0
    return 1;
7594
0
  }
7595
0
  return 0;
7596
0
}
7597
7598
static void zend_compile_attributes(
7599
  HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted
7600
0
) /* {{{ */ {
7601
0
  zend_attribute *attr;
7602
0
  zend_internal_attribute *config;
7603
7604
0
  const zend_ast_list *list = zend_ast_get_list(ast);
7605
0
  uint32_t g, i, j;
7606
7607
0
  ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST);
7608
7609
0
  for (g = 0; g < list->children; g++) {
7610
0
    const zend_ast_list *group = zend_ast_get_list(list->child[g]);
7611
7612
0
    ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP);
7613
7614
0
    for (i = 0; i < group->children; i++) {
7615
0
      ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE);
7616
7617
0
      const zend_ast *el = group->child[i];
7618
7619
0
      if (el->child[1] &&
7620
0
          el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) {
7621
0
          zend_error_noreturn(E_COMPILE_ERROR,
7622
0
              "Cannot create Closure as attribute argument");
7623
0
      }
7624
7625
0
      zend_string *name = zend_resolve_class_name_ast(el->child[0]);
7626
0
      zend_string *lcname = zend_string_tolower_ex(name, false);
7627
0
      zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL;
7628
7629
0
      config = zend_internal_attribute_get(lcname);
7630
0
      zend_string_release(lcname);
7631
7632
      /* Exclude internal attributes that do not match on promoted properties. */
7633
0
      if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7634
0
        if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) {
7635
0
          zend_string_release(name);
7636
0
          continue;
7637
0
        }
7638
0
      }
7639
7640
0
      uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES)
7641
0
        ? ZEND_ATTRIBUTE_STRICT_TYPES : 0;
7642
0
      attr = zend_add_attribute(
7643
0
        attributes, name, args ? args->children : 0, flags, offset, el->lineno);
7644
0
      zend_string_release(name);
7645
7646
      /* Populate arguments */
7647
0
      if (args) {
7648
0
        ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST);
7649
7650
0
        bool uses_named_args = false;
7651
0
        for (j = 0; j < args->children; j++) {
7652
0
          zend_ast **arg_ast_ptr = &args->child[j];
7653
0
          zend_ast *arg_ast = *arg_ast_ptr;
7654
7655
0
          if (arg_ast->kind == ZEND_AST_UNPACK) {
7656
0
            zend_error_noreturn(E_COMPILE_ERROR,
7657
0
              "Cannot use unpacking in attribute argument list");
7658
0
          }
7659
7660
0
          if (arg_ast->kind == ZEND_AST_NAMED_ARG) {
7661
0
            attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0]));
7662
0
            arg_ast_ptr = &arg_ast->child[1];
7663
0
            uses_named_args = true;
7664
7665
0
            for (uint32_t k = 0; k < j; k++) {
7666
0
              if (attr->args[k].name &&
7667
0
                  zend_string_equals(attr->args[k].name, attr->args[j].name)) {
7668
0
                zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s",
7669
0
                  ZSTR_VAL(attr->args[j].name));
7670
0
              }
7671
0
            }
7672
0
          } else if (uses_named_args) {
7673
0
            zend_error_noreturn(E_COMPILE_ERROR,
7674
0
              "Cannot use positional argument after named argument");
7675
0
          }
7676
7677
0
          zend_const_expr_to_zval(
7678
0
            &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true);
7679
0
        }
7680
0
      }
7681
0
    }
7682
0
  }
7683
7684
0
  if (*attributes != NULL) {
7685
    /* Allow delaying target validation for forward compatibility. */
7686
0
    const zend_attribute *delayed_target_validation = NULL;
7687
0
    if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) {
7688
0
      ZEND_ASSERT(offset >= 1);
7689
      /* zend_get_parameter_attribute_str will add 1 too */
7690
0
      delayed_target_validation = zend_get_parameter_attribute_str(
7691
0
        *attributes,
7692
0
        "delayedtargetvalidation",
7693
0
        strlen("delayedtargetvalidation"),
7694
0
        offset - 1
7695
0
      );
7696
0
    } else {
7697
0
      delayed_target_validation = zend_get_attribute_str(
7698
0
        *attributes,
7699
0
        "delayedtargetvalidation",
7700
0
        strlen("delayedtargetvalidation")
7701
0
      );
7702
0
    }
7703
    /* Validate attributes in a secondary loop (needed to detect repeated attributes). */
7704
0
    ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) {
7705
0
      if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) {
7706
0
        continue;
7707
0
      }
7708
7709
0
      bool run_validator = true;
7710
0
      if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) {
7711
0
        if (delayed_target_validation == NULL) {
7712
0
          zend_string *location = zend_get_attribute_target_names(target);
7713
0
          zend_string *allowed = zend_get_attribute_target_names(config->flags);
7714
7715
0
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)",
7716
0
            ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed)
7717
0
          );
7718
0
        }
7719
0
        run_validator = false;
7720
0
      }
7721
7722
0
      if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) {
7723
0
        if (zend_is_attribute_repeated(*attributes, attr)) {
7724
0
          zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name));
7725
0
        }
7726
0
      }
7727
7728
      /* Validators are not run if the target is already invalid */
7729
0
      if (run_validator && config->validator != NULL) {
7730
0
        zend_string *error = config->validator(attr, target, CG(active_class_entry));
7731
0
        if (error != NULL) {
7732
0
          if (delayed_target_validation == NULL) {
7733
0
            zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error));
7734
0
            zend_string_efree(error);
7735
0
          } else {
7736
0
            attr->validation_error = error;
7737
0
          }
7738
0
        }
7739
0
      }
7740
0
    } ZEND_HASH_FOREACH_END();
7741
0
  }
7742
0
}
7743
/* }}} */
7744
7745
static void zend_compile_property_hooks(
7746
    zend_property_info *prop_info, zend_string *prop_name,
7747
    zend_ast *prop_type_ast, const zend_ast_list *hooks);
7748
7749
typedef struct {
7750
  const zend_string *property_name;
7751
  bool uses_property;
7752
} find_property_usage_context;
7753
7754
static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */
7755
0
{
7756
0
  zend_ast *ast = *ast_ptr;
7757
0
  find_property_usage_context *context = (find_property_usage_context *) _context;
7758
7759
0
  if (ast == NULL) {
7760
0
    return;
7761
0
  } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
7762
0
    const zend_ast *object_ast = ast->child[0];
7763
0
    zend_ast *property_ast = ast->child[1];
7764
7765
0
    if (object_ast->kind == ZEND_AST_VAR
7766
0
     && object_ast->child[0]->kind == ZEND_AST_ZVAL
7767
0
     && property_ast->kind == ZEND_AST_ZVAL) {
7768
0
      const zval *object = zend_ast_get_zval(object_ast->child[0]);
7769
0
      const zval *property = zend_ast_get_zval(property_ast);
7770
0
      if (Z_TYPE_P(object) == IS_STRING
7771
0
        && Z_TYPE_P(property) == IS_STRING
7772
0
        && zend_string_equals_literal(Z_STR_P(object), "this")
7773
0
        && zend_string_equals(Z_STR_P(property), context->property_name)) {
7774
0
        context->uses_property = true;
7775
        /* No need to look for references in this branch. */
7776
0
        return;
7777
0
      }
7778
0
    }
7779
0
  }
7780
7781
  /* Don't search across function/class boundaries. */
7782
0
  if (!zend_ast_is_special(ast)) {
7783
0
    zend_ast_apply(ast, zend_property_hook_find_property_usage, context);
7784
0
  }
7785
0
}
7786
7787
static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast)
7788
0
{
7789
0
  if (zend_string_equals_literal_ci(hook_name, "set")
7790
0
   && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
7791
0
    return true;
7792
0
  }
7793
7794
0
  find_property_usage_context context = { property_name, false };
7795
0
  zend_property_hook_find_property_usage(&hook_ast, &context);
7796
0
  return context.uses_property;
7797
0
}
7798
7799
static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast, uint32_t flags)
7800
0
{
7801
0
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
7802
0
    return true;
7803
0
  }
7804
0
  if (!hooks_ast) {
7805
0
    return false;
7806
0
  }
7807
7808
0
  bool is_virtual = true;
7809
7810
0
  const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
7811
0
  for (uint32_t i = 0; i < hooks->children; i++) {
7812
0
    const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i];
7813
0
    zend_ast *body = hook->child[2];
7814
0
    if (body && zend_property_hook_uses_property(property_name, hook->name, body)) {
7815
0
      is_virtual = false;
7816
0
    }
7817
0
  }
7818
7819
0
  return is_virtual;
7820
0
}
7821
7822
static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */
7823
0
{
7824
0
  zend_ast_list *list = zend_ast_get_list(ast);
7825
0
  uint32_t i;
7826
0
  zend_op_array *op_array = CG(active_op_array);
7827
0
  zend_arg_info *arg_infos;
7828
7829
0
  if (return_type_ast || fallback_return_type) {
7830
    /* Use op_array->arg_info[-1] for return type */
7831
0
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
7832
0
    arg_infos->name = NULL;
7833
0
    if (return_type_ast) {
7834
0
      arg_infos->type = zend_compile_typename(return_type_ast);
7835
0
      ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS(
7836
0
        (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0);
7837
0
    } else {
7838
0
      arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0);
7839
0
    }
7840
0
    arg_infos++;
7841
0
    op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
7842
7843
0
    if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
7844
0
        && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
7845
0
      zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7846
0
      zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name));
7847
0
      zend_string_release(func_name);
7848
0
    }
7849
0
  } else {
7850
0
    if (list->children == 0) {
7851
0
      return;
7852
0
    }
7853
0
    arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
7854
0
  }
7855
7856
  /* Find last required parameter number for deprecation message. */
7857
0
  uint32_t last_required_param = (uint32_t) -1;
7858
0
  for (i = 0; i < list->children; ++i) {
7859
0
    zend_ast *param_ast = list->child[i];
7860
0
    zend_ast *default_ast_ptr = param_ast->child[2];
7861
0
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7862
0
    if (!default_ast_ptr && !is_variadic) {
7863
0
      last_required_param = i;
7864
0
    }
7865
0
  }
7866
7867
0
  const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL;
7868
0
  for (i = 0; i < list->children; ++i) {
7869
0
    zend_ast *param_ast = list->child[i];
7870
0
    zend_ast *type_ast = param_ast->child[0];
7871
0
    zend_ast *var_ast = param_ast->child[1];
7872
0
    zend_ast **default_ast_ptr = &param_ast->child[2];
7873
0
    zend_ast *attributes_ast = param_ast->child[3];
7874
0
    zend_ast *doc_comment_ast = param_ast->child[4];
7875
0
    zend_ast *hooks_ast = param_ast->child[5];
7876
0
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast));
7877
0
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
7878
0
    bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
7879
0
    uint32_t property_flags = param_ast->attr & promotion_flags;
7880
0
    bool is_promoted = property_flags || hooks_ast;
7881
7882
0
    CG(zend_lineno) = param_ast->lineno;
7883
7884
0
    znode var_node, default_node;
7885
0
    uint8_t opcode;
7886
0
    zend_op *opline;
7887
0
    zend_arg_info *arg_info;
7888
7889
0
    if (zend_is_auto_global(name)) {
7890
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
7891
0
        ZSTR_VAL(name));
7892
0
    }
7893
7894
0
    var_node.op_type = IS_CV;
7895
0
    var_node.u.op.var = lookup_cv(name);
7896
7897
0
    if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
7898
0
      zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
7899
0
        ZSTR_VAL(name));
7900
0
    } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
7901
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
7902
0
    } else if (zend_string_equals_literal(name, "http_response_header")) {
7903
0
      CG(context).has_assigned_to_http_response_header = true;
7904
0
    }
7905
7906
0
    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
7907
0
      zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
7908
0
    }
7909
7910
0
    if (is_variadic) {
7911
0
      opcode = ZEND_RECV_VARIADIC;
7912
0
      default_node.op_type = IS_UNUSED;
7913
0
      op_array->fn_flags |= ZEND_ACC_VARIADIC;
7914
7915
0
      if (*default_ast_ptr) {
7916
0
        zend_error_noreturn(E_COMPILE_ERROR,
7917
0
          "Variadic parameter cannot have a default value");
7918
0
      }
7919
0
    } else if (*default_ast_ptr) {
7920
      /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
7921
0
      uint32_t cops = CG(compiler_options);
7922
0
      CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
7923
0
      opcode = ZEND_RECV_INIT;
7924
0
      default_node.op_type = IS_CONST;
7925
0
      zend_const_expr_to_zval(
7926
0
        &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true);
7927
0
      CG(compiler_options) = cops;
7928
0
    } else {
7929
0
      opcode = ZEND_RECV;
7930
0
      default_node.op_type = IS_UNUSED;
7931
0
      op_array->required_num_args = i + 1;
7932
0
    }
7933
7934
0
    arg_info = &arg_infos[i];
7935
0
    arg_info->name = zend_string_copy(name);
7936
0
    arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0);
7937
7938
0
    if (attributes_ast) {
7939
0
      zend_compile_attributes(
7940
0
        &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER,
7941
0
        is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0
7942
0
      );
7943
0
    }
7944
7945
0
    bool forced_allow_nullable = false;
7946
0
    if (type_ast) {
7947
0
      uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF;
7948
0
      bool force_nullable = default_type == IS_NULL && !is_promoted;
7949
7950
0
      op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
7951
0
      arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable);
7952
0
      if (forced_allow_nullable) {
7953
0
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7954
0
        zend_error(E_DEPRECATED,
7955
0
           "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type "
7956
0
           "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name));
7957
0
        zend_string_release(func_name);
7958
0
      }
7959
7960
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) {
7961
0
        zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
7962
0
      }
7963
7964
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) {
7965
0
        zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type");
7966
0
      }
7967
7968
0
      if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable
7969
0
          && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) {
7970
0
        zend_string *type_str = zend_type_to_string(arg_info->type);
7971
0
        zend_error_noreturn(E_COMPILE_ERROR,
7972
0
          "Cannot use %s as default value for parameter $%s of type %s",
7973
0
          zend_get_type_by_const(default_type),
7974
0
          ZSTR_VAL(name), ZSTR_VAL(type_str));
7975
0
      }
7976
0
    }
7977
0
    if (last_required_param != (uint32_t) -1
7978
0
     && i < last_required_param
7979
0
     && default_node.op_type == IS_CONST) {
7980
      /* Ignore parameters of the form "Type $param = null".
7981
       * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */
7982
0
      if (!forced_allow_nullable) {
7983
0
        zend_string *func_name = get_function_or_method_name((zend_function *) op_array);
7984
0
        zend_ast *required_param_ast = list->child[last_required_param];
7985
0
        zend_error(E_DEPRECATED,
7986
0
          "%s(): Optional parameter $%s declared before required parameter $%s "
7987
0
          "is implicitly treated as a required parameter",
7988
0
          ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1])));
7989
0
        zend_string_release(func_name);
7990
0
      }
7991
7992
      /* Regardless of whether we issue a deprecation, convert this parameter into
7993
       * a required parameter without a default value. This ensures that it cannot be
7994
       * used as an optional parameter even with named parameters. */
7995
0
      opcode = ZEND_RECV;
7996
0
      default_node.op_type = IS_UNUSED;
7997
0
      zval_ptr_dtor(&default_node.u.constant);
7998
0
    }
7999
8000
0
    opline = zend_emit_op(NULL, opcode, NULL, &default_node);
8001
0
    SET_NODE(opline->result, &var_node);
8002
0
    opline->op1.num = i + 1;
8003
8004
0
    uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0)
8005
0
      | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0);
8006
0
    ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
8007
0
    if (opcode == ZEND_RECV) {
8008
0
      opline->op2.num = type_ast ?
8009
0
        ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;
8010
0
    }
8011
8012
0
    if (is_promoted) {
8013
0
      const zend_op_array *active_op_array = CG(active_op_array);
8014
0
      zend_class_entry *scope = active_op_array->scope;
8015
8016
0
      bool is_ctor =
8017
0
        scope && zend_is_constructor(active_op_array->function_name);
8018
0
      if (!is_ctor) {
8019
0
        zend_error_noreturn(E_COMPILE_ERROR,
8020
0
          "Cannot declare promoted property outside a constructor");
8021
0
      }
8022
0
      if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT)
8023
0
          || (scope->ce_flags & ZEND_ACC_INTERFACE)) {
8024
0
        zend_error_noreturn(E_COMPILE_ERROR,
8025
0
          "Cannot declare promoted property in an abstract constructor");
8026
0
      }
8027
0
      if (is_variadic) {
8028
0
        zend_error_noreturn(E_COMPILE_ERROR,
8029
0
          "Cannot declare variadic promoted property");
8030
0
      }
8031
0
      if (zend_hash_exists(&scope->properties_info, name)) {
8032
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
8033
0
          ZSTR_VAL(scope->name), ZSTR_VAL(name));
8034
0
      }
8035
0
      if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) {
8036
0
        zend_string *str = zend_type_to_string(arg_info->type);
8037
0
        zend_error_noreturn(E_COMPILE_ERROR,
8038
0
          "Property %s::$%s cannot have type %s",
8039
0
          ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str));
8040
0
      }
8041
8042
0
      if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) {
8043
0
        property_flags |= ZEND_ACC_READONLY;
8044
0
      }
8045
8046
      /* Recompile the type, as it has different memory management requirements. */
8047
0
      zend_type type = ZEND_TYPE_INIT_NONE(0);
8048
0
      if (type_ast) {
8049
0
        type = zend_compile_typename(type_ast);
8050
0
      }
8051
8052
      /* Don't give the property an explicit default value. For typed properties this means
8053
       * uninitialized, for untyped properties it means an implicit null default value.
8054
       * Properties with hooks get an implicit default value of undefined until inheritance,
8055
       * where it is changed to null only once we know it is not virtual. If we were to set it
8056
       * here, we couldn't verify that a true virtual property must not have an explicit
8057
       * default value. */
8058
0
      zval default_value;
8059
0
      if (ZEND_TYPE_IS_SET(type) || hooks_ast) {
8060
0
        ZVAL_UNDEF(&default_value);
8061
0
      } else {
8062
0
        if (property_flags & ZEND_ACC_READONLY) {
8063
0
          zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
8064
0
            ZSTR_VAL(scope->name), ZSTR_VAL(name));
8065
0
        }
8066
8067
0
        ZVAL_NULL(&default_value);
8068
0
      }
8069
8070
0
      zend_string *doc_comment =
8071
0
        doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
8072
0
      zend_property_info *prop = zend_declare_typed_property(
8073
0
        scope, name, &default_value,
8074
0
        property_flags | (zend_property_is_virtual(scope, name, hooks_ast, property_flags) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED,
8075
0
        doc_comment, type);
8076
0
      if (hooks_ast) {
8077
0
        const zend_ast_list *hooks = zend_ast_get_list(hooks_ast);
8078
0
        zend_compile_property_hooks(prop, name, type_ast, hooks);
8079
0
      }
8080
0
      if (attributes_ast) {
8081
0
        zend_compile_attributes(
8082
0
          &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER);
8083
8084
0
        zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1);
8085
0
        if (override_attribute) {
8086
0
          prop->flags |= ZEND_ACC_OVERRIDE;
8087
0
        }
8088
0
      }
8089
0
    }
8090
0
  }
8091
8092
  /* These are assigned at the end to avoid uninitialized memory in case of an error */
8093
0
  op_array->num_args = list->children;
8094
0
  op_array->arg_info = arg_infos;
8095
8096
  /* Don't count the variadic argument */
8097
0
  if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
8098
0
    op_array->num_args--;
8099
0
  }
8100
0
  zend_set_function_arg_flags((zend_function*)op_array);
8101
8102
0
  for (i = 0; i < list->children; i++) {
8103
0
    zend_ast *param_ast = list->child[i];
8104
0
    zend_ast *hooks_ast = param_ast->child[5];
8105
0
    bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
8106
0
    uint32_t flags = param_ast->attr & promotion_flags;
8107
0
    bool is_promoted = flags || hooks_ast;
8108
0
    if (!is_promoted) {
8109
0
      continue;
8110
0
    }
8111
8112
0
    CG(zend_lineno) = param_ast->lineno;
8113
8114
    /* Emit $this->prop = $prop for promoted properties. */
8115
0
    zend_string *name = zend_ast_get_str(param_ast->child[1]);
8116
0
    znode name_node, value_node;
8117
0
    name_node.op_type = IS_CONST;
8118
0
    ZVAL_STR_COPY(&name_node.u.constant, name);
8119
0
    value_node.op_type = IS_CV;
8120
0
    value_node.u.op.var = lookup_cv(name);
8121
8122
0
    zend_op *opline = zend_emit_op(NULL,
8123
0
      is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node);
8124
0
    opline->extended_value = zend_alloc_cache_slots(3);
8125
0
    zend_emit_op_data(&value_node);
8126
0
  }
8127
0
}
8128
/* }}} */
8129
8130
static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */
8131
0
{
8132
0
  const zend_ast_list *list = zend_ast_get_list(uses_ast);
8133
0
  uint32_t i;
8134
8135
0
  if (!list->children) {
8136
0
    return;
8137
0
  }
8138
8139
0
  if (!op_array->static_variables) {
8140
0
    op_array->static_variables = zend_new_array(8);
8141
0
  }
8142
8143
0
  for (i = 0; i < list->children; ++i) {
8144
0
    zend_ast *var_name_ast = list->child[i];
8145
0
    zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast));
8146
0
    uint32_t mode = var_name_ast->attr;
8147
0
    zend_op *opline;
8148
0
    zval *value;
8149
8150
0
    if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8151
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
8152
0
    }
8153
8154
0
    if (zend_is_auto_global(var_name)) {
8155
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
8156
0
    }
8157
8158
0
    value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval));
8159
0
    if (!value) {
8160
0
      zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8161
0
        "Cannot use variable $%S twice", var_name);
8162
0
    }
8163
8164
0
    CG(zend_lineno) = zend_ast_get_lineno(var_name_ast);
8165
8166
0
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8167
0
    opline->op2_type = IS_CV;
8168
0
    opline->op2.var = lookup_cv(var_name);
8169
0
    opline->extended_value =
8170
0
      (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode;
8171
0
  }
8172
0
}
8173
/* }}} */
8174
8175
typedef struct {
8176
  HashTable uses;
8177
  bool varvars_used;
8178
} closure_info;
8179
8180
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast);
8181
8182
0
static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) {
8183
0
  if (!ast) {
8184
0
    return;
8185
0
  }
8186
8187
0
  if (ast->kind == ZEND_AST_VAR) {
8188
0
    zend_ast *name_ast = ast->child[0];
8189
0
    if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) {
8190
0
      zend_string *name = zend_ast_get_str(name_ast);
8191
0
      if (zend_is_auto_global(name)) {
8192
        /* These is no need to explicitly import auto-globals. */
8193
0
        return;
8194
0
      }
8195
8196
0
      if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) {
8197
        /* $this does not need to be explicitly imported. */
8198
0
        return;
8199
0
      }
8200
8201
0
      zend_hash_add_empty_element(&info->uses, name);
8202
0
    } else {
8203
0
      info->varvars_used = true;
8204
0
      find_implicit_binds_recursively(info, name_ast);
8205
0
    }
8206
0
  } else if (zend_ast_is_list(ast)) {
8207
0
    const zend_ast_list *list = zend_ast_get_list(ast);
8208
0
    uint32_t i;
8209
0
    for (i = 0; i < list->children; i++) {
8210
0
      find_implicit_binds_recursively(info, list->child[i]);
8211
0
    }
8212
0
  } else if (ast->kind == ZEND_AST_CLOSURE) {
8213
    /* For normal closures add the use() list. */
8214
0
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8215
0
    zend_ast *uses_ast = closure_ast->child[1];
8216
0
    if (uses_ast) {
8217
0
      const zend_ast_list *uses_list = zend_ast_get_list(uses_ast);
8218
0
      uint32_t i;
8219
0
      for (i = 0; i < uses_list->children; i++) {
8220
0
        zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i]));
8221
0
      }
8222
0
    }
8223
0
  } else if (ast->kind == ZEND_AST_ARROW_FUNC) {
8224
    /* For arrow functions recursively check the expression. */
8225
0
    const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast;
8226
0
    closure_info inner_info;
8227
0
    find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]);
8228
0
    if (inner_info.varvars_used) {
8229
0
      info->varvars_used = true;
8230
0
    }
8231
0
    if (zend_hash_num_elements(&inner_info.uses)) {
8232
0
      zend_hash_copy(&info->uses, &inner_info.uses, NULL);
8233
0
    }
8234
0
    zend_hash_destroy(&inner_info.uses);
8235
0
  } else if (!zend_ast_is_special(ast)) {
8236
0
    uint32_t i, children = zend_ast_get_num_children(ast);
8237
0
    for (i = 0; i < children; i++) {
8238
0
      find_implicit_binds_recursively(info, ast->child[i]);
8239
0
    }
8240
0
  }
8241
0
}
8242
8243
static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast)
8244
0
{
8245
0
  const zend_ast_list *param_list = zend_ast_get_list(params_ast);
8246
0
  uint32_t i;
8247
8248
0
  zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0);
8249
0
  info->varvars_used = false;
8250
8251
0
  find_implicit_binds_recursively(info, stmt_ast);
8252
8253
  /* Remove variables that are parameters */
8254
0
  for (i = 0; i < param_list->children; i++) {
8255
0
    const zend_ast *param_ast = param_list->child[i];
8256
0
    zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1]));
8257
0
  }
8258
0
}
8259
8260
static void compile_implicit_lexical_binds(
8261
    const closure_info *info, znode *closure, zend_op_array *op_array)
8262
0
{
8263
0
  zend_string *var_name;
8264
0
  zend_op *opline;
8265
8266
  /* TODO We might want to use a special binding mode if varvars_used is set. */
8267
0
  if (zend_hash_num_elements(&info->uses) == 0) {
8268
0
    return;
8269
0
  }
8270
8271
0
  if (!op_array->static_variables) {
8272
0
    op_array->static_variables = zend_new_array(8);
8273
0
  }
8274
8275
0
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8276
0
    zval *value = zend_hash_add(
8277
0
      op_array->static_variables, var_name, &EG(uninitialized_zval));
8278
0
    uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData);
8279
8280
0
    opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
8281
0
    opline->op2_type = IS_CV;
8282
0
    opline->op2.var = lookup_cv(var_name);
8283
0
    opline->extended_value = offset | ZEND_BIND_IMPLICIT;
8284
0
  ZEND_HASH_FOREACH_END();
8285
0
}
8286
8287
static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
8288
0
{
8289
0
  const zend_op_array *op_array = CG(active_op_array);
8290
0
  const zend_ast_list *list = zend_ast_get_list(ast);
8291
0
  uint32_t i;
8292
8293
0
  for (i = 0; i < list->children; ++i) {
8294
0
    uint32_t mode = ZEND_BIND_EXPLICIT;
8295
0
    zend_ast *var_ast = list->child[i];
8296
0
    zend_string *var_name = zend_ast_get_str(var_ast);
8297
0
    zval zv;
8298
0
    ZVAL_NULL(&zv);
8299
8300
0
    {
8301
0
      int i;
8302
0
      for (i = 0; i < op_array->last_var; i++) {
8303
0
        if (zend_string_equals(op_array->vars[i], var_name)) {
8304
0
          zend_error_noreturn_unchecked(E_COMPILE_ERROR,
8305
0
            "Cannot use lexical variable $%S as a parameter name", var_name);
8306
0
        }
8307
0
      }
8308
0
    }
8309
8310
0
    CG(zend_lineno) = zend_ast_get_lineno(var_ast);
8311
8312
0
    if (var_ast->attr) {
8313
0
      mode |= ZEND_BIND_REF;
8314
0
    }
8315
8316
0
    zend_compile_static_var_common(var_name, &zv, mode);
8317
0
  }
8318
0
}
8319
/* }}} */
8320
8321
static void zend_compile_implicit_closure_uses(const closure_info *info)
8322
0
{
8323
0
  zend_string *var_name;
8324
0
  ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name)
8325
0
    zval zv;
8326
0
    ZVAL_NULL(&zv);
8327
0
    zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT);
8328
0
  ZEND_HASH_FOREACH_END();
8329
0
}
8330
8331
0
static void add_stringable_interface(zend_class_entry *ce) {
8332
0
  for (uint32_t i = 0; i < ce->num_interfaces; i++) {
8333
0
    if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) {
8334
      /* Interface already explicitly implemented */
8335
0
      return;
8336
0
    }
8337
0
  }
8338
8339
0
  ce->num_interfaces++;
8340
0
  ce->interface_names =
8341
0
    erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces);
8342
  // TODO: Add known interned strings instead?
8343
0
  ce->interface_names[ce->num_interfaces - 1].name =
8344
0
    ZSTR_INIT_LITERAL("Stringable", 0);
8345
0
  ce->interface_names[ce->num_interfaces - 1].lc_name =
8346
0
    ZSTR_INIT_LITERAL("stringable", 0);
8347
0
}
8348
8349
static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */
8350
0
{
8351
0
  zend_class_entry *ce = CG(active_class_entry);
8352
0
  bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
8353
0
  uint32_t fn_flags = op_array->fn_flags;
8354
8355
0
  zend_string *lcname;
8356
8357
0
  if (fn_flags & ZEND_ACC_READONLY) {
8358
0
    zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier");
8359
0
  }
8360
8361
0
  if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) {
8362
0
    zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
8363
0
  }
8364
8365
0
  if ((fn_flags & ZEND_ACC_ABSTRACT)
8366
0
   && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) {
8367
    // Don't say that the class should be declared abstract if it is
8368
    // anonymous or an enum and can't be abstract
8369
0
    if (ce->ce_flags & ZEND_ACC_ANON_CLASS) {
8370
0
      zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract",
8371
0
        ZSTR_VAL(name));
8372
0
    } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) {
8373
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract",
8374
0
        zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name));
8375
0
    } else {
8376
0
      zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract",
8377
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
8378
0
    }
8379
0
  }
8380
8381
0
  if (in_interface) {
8382
0
    if (!(fn_flags & ZEND_ACC_PUBLIC)) {
8383
0
      zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
8384
0
        "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8385
0
    }
8386
0
    if (fn_flags & ZEND_ACC_FINAL) {
8387
0
      zend_error_noreturn(E_COMPILE_ERROR, "Interface method "
8388
0
        "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8389
0
    }
8390
0
    op_array->fn_flags |= ZEND_ACC_ABSTRACT;
8391
0
  }
8392
8393
0
  if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
8394
0
    if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8395
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
8396
0
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8397
0
    }
8398
8399
0
    if (has_body) {
8400
0
      zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
8401
0
        in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
8402
0
    }
8403
8404
0
    ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8405
0
  } else if (!has_body) {
8406
0
    zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
8407
0
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8408
0
  }
8409
8410
0
  op_array->scope = ce;
8411
0
  op_array->function_name = zend_string_copy(name);
8412
8413
0
  lcname = zend_string_tolower(name);
8414
0
  lcname = zend_new_interned_string(lcname);
8415
8416
0
  if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
8417
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
8418
0
      ZSTR_VAL(ce->name), ZSTR_VAL(name));
8419
0
  }
8420
8421
0
  zend_add_magic_method(ce, (zend_function *) op_array, lcname);
8422
0
  if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)
8423
0
      && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
8424
0
    add_stringable_interface(ce);
8425
0
  }
8426
8427
0
  return lcname;
8428
0
}
8429
/* }}} */
8430
8431
0
static uint32_t zend_add_dynamic_func_def(zend_op_array *def) {
8432
0
  zend_op_array *op_array = CG(active_op_array);
8433
0
  uint32_t def_offset = op_array->num_dynamic_func_defs++;
8434
0
  op_array->dynamic_func_defs = erealloc(
8435
0
    op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *));
8436
0
  op_array->dynamic_func_defs[def_offset] = def;
8437
0
  return def_offset;
8438
0
}
8439
8440
enum func_decl_level {
8441
  FUNC_DECL_LEVEL_TOPLEVEL,
8442
  FUNC_DECL_LEVEL_NESTED,
8443
  FUNC_DECL_LEVEL_CONSTEXPR,
8444
};
8445
8446
static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */
8447
0
{
8448
0
  zend_string *unqualified_name, *name, *lcname;
8449
0
  zend_op *opline;
8450
8451
0
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8452
0
    zend_string *filename = op_array->filename;
8453
0
    uint32_t start_lineno = decl->start_lineno;
8454
8455
0
    zend_string *class = zend_empty_string;
8456
0
    zend_string *separator = zend_empty_string;
8457
0
    zend_string *function = filename;
8458
0
    const char *parens = "";
8459
8460
0
    if (CG(active_op_array) && CG(active_op_array)->function_name) {
8461
0
      if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
8462
        /* If the parent function is a closure, don't redundantly
8463
         * add the classname and parentheses.
8464
         */
8465
0
        function = CG(active_op_array)->function_name;
8466
0
      } else {
8467
0
        function = CG(active_op_array)->function_name;
8468
0
        parens = "()";
8469
8470
0
        if (CG(active_class_entry) && CG(active_class_entry)->name) {
8471
0
          class = CG(active_class_entry)->name;
8472
0
          separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
8473
0
        }
8474
0
      }
8475
0
    }
8476
8477
0
    unqualified_name = zend_strpprintf_unchecked(
8478
0
      0,
8479
0
      "{closure:%S%S%S%s:%" PRIu32 "}",
8480
0
      class,
8481
0
      separator,
8482
0
      function,
8483
0
      parens,
8484
0
      start_lineno
8485
0
    );
8486
8487
0
    op_array->function_name = name = unqualified_name;
8488
0
  } else {
8489
0
    unqualified_name = decl->name;
8490
0
    op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
8491
0
  }
8492
8493
0
  lcname = zend_string_tolower(name);
8494
8495
0
  if (FC(imports_function)) {
8496
0
    const zend_string *import_name =
8497
0
      zend_hash_find_ptr_lc(FC(imports_function), unqualified_name);
8498
0
    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
8499
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)",
8500
0
        ZSTR_VAL(name));
8501
0
    }
8502
0
  }
8503
8504
0
  if (zend_string_equals_literal(lcname, "__autoload")) {
8505
0
    zend_error_noreturn(E_COMPILE_ERROR,
8506
0
      "__autoload() is no longer supported, use spl_autoload_register() instead");
8507
0
  }
8508
8509
0
  if (zend_string_equals_literal_ci(unqualified_name, "assert")) {
8510
0
    zend_error(E_COMPILE_ERROR,
8511
0
      "Defining a custom assert() function is not allowed, "
8512
0
      "as the function has special semantics");
8513
0
  }
8514
8515
0
  zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION);
8516
0
  switch (level) {
8517
0
    case FUNC_DECL_LEVEL_NESTED: {
8518
0
      uint32_t func_ref = zend_add_dynamic_func_def(op_array);
8519
0
      if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
8520
0
        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
8521
0
        opline->op2.num = func_ref;
8522
0
      } else {
8523
0
        opline = get_next_op();
8524
0
        opline->opcode = ZEND_DECLARE_FUNCTION;
8525
0
        opline->op1_type = IS_CONST;
8526
0
        LITERAL_STR(opline->op1, zend_string_copy(lcname));
8527
0
        opline->op2.num = func_ref;
8528
0
      }
8529
0
      break;
8530
0
    }
8531
0
    case FUNC_DECL_LEVEL_CONSTEXPR:
8532
0
    case FUNC_DECL_LEVEL_TOPLEVEL:
8533
      /* Nothing to do. */
8534
0
      break;
8535
0
  }
8536
0
  return lcname;
8537
0
}
8538
/* }}} */
8539
8540
static zend_op_array *zend_compile_func_decl_ex(
8541
  znode *result, zend_ast *ast, enum func_decl_level level,
8542
  zend_string *property_info_name,
8543
  zend_property_hook_kind hook_kind
8544
0
) {
8545
0
  zend_ast_decl *decl = (zend_ast_decl *) ast;
8546
0
  zend_ast *params_ast = decl->child[0];
8547
0
  zend_ast *uses_ast = decl->child[1];
8548
0
  zend_ast *stmt_ast = decl->child[2];
8549
0
  zend_ast *return_type_ast = decl->child[3];
8550
0
  bool is_method = decl->kind == ZEND_AST_METHOD;
8551
0
  zend_string *lcname = NULL;
8552
0
  bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK;
8553
8554
0
  zend_class_entry *orig_class_entry = CG(active_class_entry);
8555
0
  zend_op_array *orig_op_array = CG(active_op_array);
8556
0
  zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
8557
0
  zend_oparray_context orig_oparray_context;
8558
0
  closure_info info;
8559
8560
0
  init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
8561
8562
0
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
8563
0
    op_array->fn_flags |= ZEND_ACC_PRELOADED;
8564
0
  }
8565
8566
0
  op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
8567
0
  op_array->fn_flags |= decl->flags;
8568
0
  op_array->line_start = decl->start_lineno;
8569
0
  op_array->line_end = decl->end_lineno;
8570
0
  if (decl->doc_comment) {
8571
0
    op_array->doc_comment = zend_string_copy(decl->doc_comment);
8572
0
  }
8573
8574
0
  if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
8575
0
    op_array->fn_flags |= ZEND_ACC_CLOSURE;
8576
0
  }
8577
8578
0
  if (is_hook) {
8579
0
    zend_class_entry *ce = CG(active_class_entry);
8580
0
    op_array->scope = ce;
8581
0
    op_array->function_name = zend_string_copy(decl->name);
8582
0
  } else if (is_method) {
8583
0
    bool has_body = stmt_ast != NULL;
8584
0
    lcname = zend_begin_method_decl(op_array, decl->name, has_body);
8585
0
  } else {
8586
0
    lcname = zend_begin_func_decl(result, op_array, decl, level);
8587
0
    if (decl->kind == ZEND_AST_ARROW_FUNC) {
8588
0
      find_implicit_binds(&info, params_ast, stmt_ast);
8589
0
      compile_implicit_lexical_binds(&info, result, op_array);
8590
0
    } else if (uses_ast) {
8591
0
      zend_compile_closure_binding(result, op_array, uses_ast);
8592
0
    }
8593
0
  }
8594
8595
0
  CG(active_op_array) = op_array;
8596
8597
0
  zend_oparray_context_begin(&orig_oparray_context, op_array);
8598
0
  CG(context).active_property_info_name = property_info_name;
8599
0
  CG(context).active_property_hook_kind = hook_kind;
8600
8601
0
  if (decl->child[4]) {
8602
0
    int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
8603
8604
0
    if (is_method || is_hook) {
8605
0
      target = ZEND_ATTRIBUTE_TARGET_METHOD;
8606
0
    }
8607
8608
0
    zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0);
8609
8610
0
    const zend_attribute *override_attribute = zend_get_attribute_str(
8611
0
      op_array->attributes,
8612
0
      "override",
8613
0
      sizeof("override")-1
8614
0
    );
8615
8616
0
    if (override_attribute) {
8617
0
      op_array->fn_flags |= ZEND_ACC_OVERRIDE;
8618
0
    }
8619
8620
0
    const zend_attribute *deprecated_attribute = zend_get_attribute_str(
8621
0
      op_array->attributes,
8622
0
      "deprecated",
8623
0
      sizeof("deprecated")-1
8624
0
    );
8625
8626
0
    if (deprecated_attribute) {
8627
0
      op_array->fn_flags |= ZEND_ACC_DEPRECATED;
8628
0
    }
8629
8630
    // ZEND_ACC_NODISCARD is added via an attribute validator
8631
0
  }
8632
8633
  /* Do not leak the class scope into free standing functions, even if they are dynamically
8634
   * defined inside a class method. This is necessary for correct handling of magic constants.
8635
   * For example __CLASS__ should always be "" inside a free standing function. */
8636
0
  if (decl->kind == ZEND_AST_FUNC_DECL) {
8637
0
    CG(active_class_entry) = NULL;
8638
0
  }
8639
8640
0
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8641
0
    op_array->fn_flags |= ZEND_ACC_TOP_LEVEL;
8642
0
  }
8643
8644
0
  {
8645
    /* Push a separator to the loop variable stack */
8646
0
    zend_loop_var dummy_var;
8647
0
    dummy_var.opcode = ZEND_RETURN;
8648
8649
0
    zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
8650
0
  }
8651
8652
0
  zend_compile_params(params_ast, return_type_ast,
8653
0
    is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
8654
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8655
0
    zend_mark_function_as_generator();
8656
0
    zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
8657
0
  }
8658
0
  if (decl->kind == ZEND_AST_ARROW_FUNC) {
8659
0
    zend_compile_implicit_closure_uses(&info);
8660
0
    zend_hash_destroy(&info.uses);
8661
0
  } else if (uses_ast) {
8662
0
    zend_compile_closure_uses(uses_ast);
8663
0
  }
8664
8665
0
  if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) {
8666
0
    bool needs_return = true;
8667
0
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8668
0
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8669
0
      needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER);
8670
0
    }
8671
0
    if (needs_return) {
8672
0
      stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8673
0
      decl->child[2] = stmt_ast;
8674
0
    }
8675
0
  }
8676
8677
0
  if (op_array->fn_flags & ZEND_ACC_NODISCARD) {
8678
    /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only
8679
     * if the method is not a hook; if it is a hook, then the validator
8680
     * will have returned an error message, even if the error message was
8681
     * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD
8682
     * flag should not have been added. */
8683
0
    ZEND_ASSERT(!is_hook);
8684
8685
0
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
8686
0
      const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1;
8687
0
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) {
8688
0
        zend_error_noreturn(E_COMPILE_ERROR,
8689
0
          "A void %s does not return a value, but #[\\NoDiscard] requires a return value",
8690
0
          CG(active_class_entry) != NULL ? "method" : "function");
8691
0
      }
8692
8693
0
      if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) {
8694
0
        zend_error_noreturn(E_COMPILE_ERROR,
8695
0
          "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value",
8696
0
          CG(active_class_entry) != NULL ? "method" : "function");
8697
0
      }
8698
0
    }
8699
0
  }
8700
8701
0
  zend_compile_stmt(stmt_ast);
8702
8703
0
  if (is_method) {
8704
0
    CG(zend_lineno) = decl->start_lineno;
8705
0
    zend_check_magic_method_implementation(
8706
0
      CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR);
8707
0
  } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8708
    /* Only register the function after a successful compile */
8709
0
    if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) {
8710
0
      CG(zend_lineno) = decl->start_lineno;
8711
0
      do_bind_function_error(lcname, op_array, true);
8712
0
    }
8713
0
  }
8714
8715
  /* put the implicit return on the really last line */
8716
0
  CG(zend_lineno) = decl->end_lineno;
8717
8718
0
  zend_do_extended_stmt(NULL);
8719
0
  zend_emit_final_return(false);
8720
8721
0
  pass_two(CG(active_op_array));
8722
0
  zend_oparray_context_end(&orig_oparray_context);
8723
8724
  /* Pop the loop variable stack separator */
8725
0
  zend_stack_del_top(&CG(loop_var_stack));
8726
8727
0
  if (level == FUNC_DECL_LEVEL_TOPLEVEL) {
8728
0
    zend_observer_function_declared_notify(op_array, lcname);
8729
0
  }
8730
8731
0
  if (lcname != NULL) {
8732
0
    zend_string_release_ex(lcname, 0);
8733
0
  }
8734
8735
0
  CG(active_op_array) = orig_op_array;
8736
0
  CG(active_class_entry) = orig_class_entry;
8737
8738
0
  return op_array;
8739
0
}
8740
8741
static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level)
8742
0
{
8743
0
  return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1);
8744
0
}
8745
8746
0
zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) {
8747
0
  if (zend_string_equals_literal_ci(name, "get")) {
8748
0
    return ZEND_PROPERTY_HOOK_GET;
8749
0
  } else if (zend_string_equals_literal_ci(name, "set")) {
8750
0
    return ZEND_PROPERTY_HOOK_SET;
8751
0
  } else {
8752
0
    return (zend_property_hook_kind)-1;
8753
0
  }
8754
0
}
8755
8756
static void zend_compile_property_hooks(
8757
    zend_property_info *prop_info, zend_string *prop_name,
8758
    zend_ast *prop_type_ast, const zend_ast_list *hooks)
8759
0
{
8760
0
  zend_class_entry *ce = CG(active_class_entry);
8761
8762
0
  if (prop_info->flags & ZEND_ACC_READONLY) {
8763
0
    zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly");
8764
0
  }
8765
8766
0
  if (hooks->children == 0) {
8767
0
    zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty");
8768
0
  }
8769
8770
0
  for (uint32_t i = 0; i < hooks->children; i++) {
8771
0
    zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i];
8772
0
    zend_string *name = hook->name;
8773
0
    zend_ast *stmt_ast = hook->child[2];
8774
0
    zend_ast **return_type_ast_ptr = NULL;
8775
0
    zend_ast **value_type_ast_ptr = NULL;
8776
0
    CG(zend_lineno) = hook->start_lineno;
8777
8778
    /* Non-private hooks are always public. This avoids having to copy the hook when inheriting
8779
     * hooks from protected properties to public ones. */
8780
0
    uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE;
8781
0
    hook->flags |= hook_visibility;
8782
8783
0
    if (prop_info->flags & ZEND_ACC_STATIC) {
8784
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property");
8785
0
    }
8786
0
    if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) {
8787
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private");
8788
0
    }
8789
0
    if ((ce->ce_flags & ZEND_ACC_INTERFACE)
8790
0
     || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) {
8791
0
      hook->flags |= ZEND_ACC_ABSTRACT;
8792
8793
0
      if (stmt_ast) {
8794
0
        zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body");
8795
0
      }
8796
0
      if (hook->flags & ZEND_ACC_PRIVATE) {
8797
0
        zend_error_noreturn(E_COMPILE_ERROR,
8798
0
          "Property hook cannot be both abstract and private");
8799
0
      }
8800
0
      if (hook->flags & ZEND_ACC_FINAL) {
8801
0
        zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final");
8802
0
      }
8803
0
    } else if (!stmt_ast) {
8804
0
      zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body");
8805
0
    }
8806
8807
0
    zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name);
8808
0
    if (hook_kind == (zend_property_hook_kind)-1) {
8809
0
      zend_error_noreturn(E_COMPILE_ERROR,
8810
0
        "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"",
8811
0
        ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8812
0
    }
8813
8814
0
    if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) {
8815
0
      stmt_ast = stmt_ast->child[0];
8816
0
      if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8817
0
        stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast);
8818
0
      } else {
8819
0
        ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET);
8820
0
        stmt_ast = zend_ast_create(ZEND_AST_ASSIGN,
8821
0
          zend_ast_create(ZEND_AST_PROP,
8822
0
            zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))),
8823
0
            zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))),
8824
0
          stmt_ast);
8825
0
      }
8826
0
      stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast);
8827
0
      hook->child[2] = stmt_ast;
8828
0
    }
8829
8830
0
    if (hook_kind == ZEND_PROPERTY_HOOK_GET) {
8831
0
      if (hook->child[0]) {
8832
0
        zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list",
8833
0
          ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8834
0
      }
8835
8836
0
      hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST);
8837
8838
0
      return_type_ast_ptr = &hook->child[3];
8839
0
      *return_type_ast_ptr = prop_type_ast;
8840
0
    } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8841
0
      if (hook->child[0]) {
8842
0
        const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]);
8843
0
        if (param_list->children != 1) {
8844
0
          zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters",
8845
0
            ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8846
0
        }
8847
0
        const zend_ast *value_param_ast = param_list->child[0];
8848
0
        if (value_param_ast->attr & ZEND_PARAM_REF) {
8849
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference",
8850
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8851
0
        }
8852
0
        if (value_param_ast->attr & ZEND_PARAM_VARIADIC) {
8853
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic",
8854
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8855
0
        }
8856
0
        if (value_param_ast->child[2]) {
8857
0
          zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value",
8858
0
            ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
8859
0
        }
8860
0
        if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) {
8861
0
          zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name);
8862
0
        }
8863
0
      } else {
8864
0
        zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE));
8865
0
        zend_ast *param = zend_ast_create(
8866
0
          ZEND_AST_PARAM, prop_type_ast, param_name_ast,
8867
0
          /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL,
8868
0
          /* hooks */ NULL);
8869
0
        value_type_ast_ptr = &param->child[0];
8870
0
        hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
8871
0
      }
8872
0
      zend_ast *return_type = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VOID));
8873
0
      return_type->attr = ZEND_NAME_NOT_FQ;
8874
0
      hook->child[3] = return_type;
8875
0
    } else {
8876
0
      ZEND_UNREACHABLE();
8877
0
    }
8878
8879
0
    hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name));
8880
8881
0
    zend_function *func = (zend_function *) zend_compile_func_decl_ex(
8882
0
      NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind);
8883
8884
0
    func->common.prop_info = prop_info;
8885
8886
0
    if (!prop_info->hooks) {
8887
0
      prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8888
0
      memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
8889
0
    }
8890
8891
0
    if (prop_info->hooks[hook_kind]) {
8892
0
      zend_error_noreturn(E_COMPILE_ERROR,
8893
0
        "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name));
8894
0
    }
8895
0
    prop_info->hooks[hook_kind] = func;
8896
8897
0
    if (hook_kind == ZEND_PROPERTY_HOOK_SET) {
8898
0
      switch (zend_verify_property_hook_variance(prop_info, func)) {
8899
0
        case INHERITANCE_SUCCESS:
8900
0
          break;
8901
0
        case INHERITANCE_UNRESOLVED:
8902
0
          ce->num_hooked_prop_variance_checks++;
8903
0
          break;
8904
0
        case INHERITANCE_ERROR:
8905
0
          zend_hooked_property_variance_error(prop_info);
8906
0
        case INHERITANCE_WARNING:
8907
0
          ZEND_UNREACHABLE();
8908
0
      }
8909
0
    }
8910
8911
0
    zend_string_release(name);
8912
    /* Un-share type ASTs to avoid double-frees of zval nodes. */
8913
0
    if (return_type_ast_ptr) {
8914
0
      *return_type_ast_ptr = NULL;
8915
0
    }
8916
0
    if (value_type_ast_ptr) {
8917
0
      *value_type_ast_ptr = NULL;
8918
0
    }
8919
0
  }
8920
8921
0
  ce->num_hooked_props++;
8922
8923
  /* See zend_link_hooked_object_iter(). */
8924
0
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
8925
0
  if (!ce->get_iterator) {
8926
    /* Will be removed again, in case of Iterator or IteratorAggregate. */
8927
0
    ce->get_iterator = zend_hooked_object_get_iterator;
8928
0
  }
8929
0
#endif
8930
8931
0
  if (!prop_info->ce->parent_name) {
8932
0
    zend_verify_hooked_property(ce, prop_info, prop_name);
8933
0
  }
8934
0
}
8935
8936
static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
8937
0
{
8938
0
  const zend_ast_list *list = zend_ast_get_list(ast);
8939
0
  zend_class_entry *ce = CG(active_class_entry);
8940
0
  uint32_t i, children = list->children;
8941
8942
0
  if (ce->ce_flags & ZEND_ACC_ENUM) {
8943
0
    zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name));
8944
0
  }
8945
8946
0
  if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) {
8947
0
    zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private");
8948
0
  }
8949
8950
0
  if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8951
0
    if (flags & ZEND_ACC_FINAL) {
8952
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final");
8953
0
    }
8954
0
    if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) {
8955
0
      zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private");
8956
0
    }
8957
0
    if (flags & ZEND_ACC_ABSTRACT) {
8958
0
      zend_error_noreturn(E_COMPILE_ERROR,
8959
0
        "Property in interface cannot be explicitly abstract. "
8960
0
        "All interface members are implicitly abstract");
8961
0
    }
8962
0
    flags |= ZEND_ACC_ABSTRACT;
8963
0
  }
8964
8965
0
  for (i = 0; i < children; ++i) {
8966
0
    zend_property_info *info;
8967
0
    zend_ast *prop_ast = list->child[i];
8968
0
    zend_ast *name_ast = prop_ast->child[0];
8969
0
    zend_ast **value_ast_ptr = &prop_ast->child[1];
8970
0
    zend_ast *doc_comment_ast = prop_ast->child[2];
8971
0
    zend_ast *hooks_ast = prop_ast->child[3];
8972
0
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
8973
0
    zend_string *doc_comment = NULL;
8974
0
    zval value_zv;
8975
0
    zend_type type = ZEND_TYPE_INIT_NONE(0);
8976
0
    flags |= zend_property_is_virtual(ce, name, hooks_ast, flags) ? ZEND_ACC_VIRTUAL : 0;
8977
8978
0
    zend_string *old_active_property_info_name = CG(context).active_property_info_name;
8979
0
    CG(context).active_property_info_name = name;
8980
8981
0
    if (!hooks_ast) {
8982
0
      if (ce->ce_flags & ZEND_ACC_INTERFACE) {
8983
0
        zend_error_noreturn(E_COMPILE_ERROR,
8984
0
          "Interfaces may only include hooked properties");
8985
0
      }
8986
0
      if (flags & ZEND_ACC_ABSTRACT) {
8987
0
        zend_error_noreturn(E_COMPILE_ERROR,
8988
0
          "Only hooked properties may be declared abstract");
8989
0
      }
8990
0
    }
8991
0
    if ((flags & ZEND_ACC_ABSTRACT)) {
8992
0
      ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
8993
0
    }
8994
8995
0
    if (type_ast) {
8996
0
      type = zend_compile_typename(type_ast);
8997
8998
0
      if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) {
8999
0
        zend_string *str = zend_type_to_string(type);
9000
0
        zend_error_noreturn(E_COMPILE_ERROR,
9001
0
          "Property %s::$%s cannot have type %s",
9002
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9003
0
      }
9004
0
    }
9005
9006
    /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
9007
0
    if (doc_comment_ast) {
9008
0
      doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9009
0
    }
9010
9011
0
    if (zend_hash_exists(&ce->properties_info, name)) {
9012
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
9013
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name));
9014
0
    }
9015
9016
0
    if (*value_ast_ptr) {
9017
0
      zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9018
9019
0
      if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv)
9020
0
          && !zend_is_valid_default_value(type, &value_zv)) {
9021
0
        zend_string *str = zend_type_to_string(type);
9022
0
        if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) {
9023
0
          ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL;
9024
0
          zend_string *nullable_str = zend_type_to_string(type);
9025
9026
0
          zend_error_noreturn(E_COMPILE_ERROR,
9027
0
            "Default value for property of type %s may not be null. "
9028
0
            "Use the nullable type %s to allow null default value",
9029
0
            ZSTR_VAL(str), ZSTR_VAL(nullable_str));
9030
0
        } else {
9031
0
          zend_error_noreturn(E_COMPILE_ERROR,
9032
0
            "Cannot use %s as default value for property %s::$%s of type %s",
9033
0
            zend_zval_value_name(&value_zv),
9034
0
            ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str));
9035
0
        }
9036
0
      }
9037
0
    } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) {
9038
0
      ZVAL_NULL(&value_zv);
9039
0
    } else {
9040
0
      ZVAL_UNDEF(&value_zv);
9041
0
    }
9042
9043
0
    if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) {
9044
0
      flags |= ZEND_ACC_READONLY;
9045
0
    }
9046
9047
0
    if (flags & ZEND_ACC_READONLY) {
9048
0
      if (!ZEND_TYPE_IS_SET(type)) {
9049
0
        zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type",
9050
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9051
0
      }
9052
0
      if (!Z_ISUNDEF(value_zv)) {
9053
0
        zend_error_noreturn(E_COMPILE_ERROR,
9054
0
          "Readonly property %s::$%s cannot have default value",
9055
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9056
0
      }
9057
0
      if (flags & ZEND_ACC_STATIC) {
9058
0
        zend_error_noreturn(E_COMPILE_ERROR,
9059
0
          "Static property %s::$%s cannot be readonly",
9060
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name));
9061
0
      }
9062
0
    }
9063
9064
0
    info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type);
9065
9066
0
    if (hooks_ast) {
9067
0
      zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast));
9068
0
    }
9069
9070
0
    if (attr_ast) {
9071
0
      zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0);
9072
9073
0
      const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1);
9074
0
      if (override_attribute) {
9075
0
        info->flags |= ZEND_ACC_OVERRIDE;
9076
0
      }
9077
0
    }
9078
9079
0
    CG(context).active_property_info_name = old_active_property_info_name;
9080
0
  }
9081
0
}
9082
/* }}} */
9083
9084
static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */
9085
0
{
9086
0
  zend_ast *type_ast = ast->child[0];
9087
0
  zend_ast *prop_ast = ast->child[1];
9088
0
  zend_ast *attr_ast = ast->child[2];
9089
9090
0
  zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
9091
0
}
9092
/* }}} */
9093
9094
static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */
9095
0
{
9096
0
  if (attr & ZEND_ACC_STATIC) {
9097
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias");
9098
0
  } else if (attr & ZEND_ACC_ABSTRACT) {
9099
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias");
9100
0
  }
9101
0
}
9102
/* }}} */
9103
9104
static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast)
9105
0
{
9106
0
  const zend_ast_list *list = zend_ast_get_list(ast);
9107
0
  zend_class_entry *ce = CG(active_class_entry);
9108
0
  uint32_t i, children = list->children;
9109
9110
0
  for (i = 0; i < children; ++i) {
9111
0
    zend_class_constant *c;
9112
0
    zend_ast *const_ast = list->child[i];
9113
0
    zend_ast *name_ast = const_ast->child[0];
9114
0
    zend_ast **value_ast_ptr = &const_ast->child[1];
9115
0
    zend_ast *doc_comment_ast = const_ast->child[2];
9116
0
    zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast));
9117
0
    zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
9118
0
    zval value_zv;
9119
0
    zend_type type = ZEND_TYPE_INIT_NONE(0);
9120
9121
0
    if (type_ast) {
9122
0
      type = zend_compile_typename(type_ast);
9123
9124
0
      uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9125
9126
0
      if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) {
9127
0
        zend_string *type_str = zend_type_to_string(type);
9128
9129
0
        zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s",
9130
0
          ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9131
0
      }
9132
0
    }
9133
9134
0
    if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) {
9135
0
      zend_error_noreturn(
9136
0
        E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes",
9137
0
        ZSTR_VAL(ce->name), ZSTR_VAL(name)
9138
0
      );
9139
0
    }
9140
9141
0
    zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false);
9142
9143
0
    if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) {
9144
0
      zend_string *type_str = zend_type_to_string(type);
9145
9146
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s",
9147
0
        zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str));
9148
0
    }
9149
9150
0
    c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type);
9151
9152
0
    if (attr_ast) {
9153
0
      zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9154
9155
0
      const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9156
9157
0
      if (deprecated) {
9158
0
        ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9159
        /* For deprecated constants, we need to flag the zval for recursion
9160
         * detection. Make sure the zval is separated out of shm. */
9161
0
        ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
9162
0
        ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
9163
0
      }
9164
0
    }
9165
0
  }
9166
0
}
9167
9168
static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */
9169
0
{
9170
0
  zend_ast *const_ast = ast->child[0];
9171
0
  zend_ast *attr_ast = ast->child[1];
9172
0
  zend_ast *type_ast = ast->child[2];
9173
9174
0
  zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast);
9175
0
}
9176
/* }}} */
9177
9178
static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
9179
0
{
9180
0
  zend_ast *class_ast = ast->child[0];
9181
0
  zend_ast *method_ast = ast->child[1];
9182
9183
0
  method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
9184
9185
0
  if (class_ast) {
9186
0
    method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name");
9187
0
  } else {
9188
0
    method_ref->class_name = NULL;
9189
0
  }
9190
0
}
9191
/* }}} */
9192
9193
static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */
9194
0
{
9195
0
  const zend_ast *method_ref_ast = ast->child[0];
9196
0
  zend_ast *insteadof_ast = ast->child[1];
9197
0
  const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast);
9198
0
  uint32_t i;
9199
9200
0
  zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*));
9201
0
  zend_compile_method_ref(method_ref_ast, &precedence->trait_method);
9202
0
  precedence->num_excludes = insteadof_list->children;
9203
9204
0
  for (i = 0; i < insteadof_list->children; ++i) {
9205
0
    zend_ast *name_ast = insteadof_list->child[i];
9206
0
    precedence->exclude_class_names[i] =
9207
0
      zend_resolve_const_class_name_reference(name_ast, "trait name");
9208
0
  }
9209
9210
0
  zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
9211
0
}
9212
/* }}} */
9213
9214
static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */
9215
0
{
9216
0
  const zend_ast *method_ref_ast = ast->child[0];
9217
0
  zend_ast *alias_ast = ast->child[1];
9218
0
  uint32_t modifiers = ast->attr;
9219
9220
0
  zend_trait_alias *alias;
9221
9222
0
  zend_check_trait_alias_modifiers(modifiers);
9223
9224
0
  alias = emalloc(sizeof(zend_trait_alias));
9225
0
  zend_compile_method_ref(method_ref_ast, &alias->trait_method);
9226
0
  alias->modifiers = modifiers;
9227
9228
0
  if (alias_ast) {
9229
0
    alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
9230
0
  } else {
9231
0
    alias->alias = NULL;
9232
0
  }
9233
9234
0
  zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
9235
0
}
9236
/* }}} */
9237
9238
static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */
9239
0
{
9240
0
  const zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
9241
0
  zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
9242
0
  zend_class_entry *ce = CG(active_class_entry);
9243
0
  uint32_t i;
9244
9245
0
  ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children));
9246
9247
0
  for (i = 0; i < traits->children; ++i) {
9248
0
    zend_ast *trait_ast = traits->child[i];
9249
9250
0
    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
9251
0
      zend_string *name = zend_ast_get_str(trait_ast);
9252
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
9253
0
        "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
9254
0
    }
9255
9256
0
    ce->trait_names[ce->num_traits].name =
9257
0
      zend_resolve_const_class_name_reference(trait_ast, "trait name");
9258
0
    ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name);
9259
0
    ce->num_traits++;
9260
0
  }
9261
9262
0
  if (!adaptations) {
9263
0
    return;
9264
0
  }
9265
9266
0
  for (i = 0; i < adaptations->children; ++i) {
9267
0
    const zend_ast *adaptation_ast = adaptations->child[i];
9268
0
    switch (adaptation_ast->kind) {
9269
0
      case ZEND_AST_TRAIT_PRECEDENCE:
9270
0
        zend_compile_trait_precedence(adaptation_ast);
9271
0
        break;
9272
0
      case ZEND_AST_TRAIT_ALIAS:
9273
0
        zend_compile_trait_alias(adaptation_ast);
9274
0
        break;
9275
0
      EMPTY_SWITCH_DEFAULT_CASE()
9276
0
    }
9277
0
  }
9278
0
}
9279
/* }}} */
9280
9281
static void zend_compile_implements(zend_ast *ast) /* {{{ */
9282
0
{
9283
0
  const zend_ast_list *list = zend_ast_get_list(ast);
9284
0
  zend_class_entry *ce = CG(active_class_entry);
9285
0
  zend_class_name *interface_names;
9286
0
  uint32_t i;
9287
9288
0
  interface_names = emalloc(sizeof(zend_class_name) * list->children);
9289
9290
0
  for (i = 0; i < list->children; ++i) {
9291
0
    zend_ast *class_ast = list->child[i];
9292
0
    interface_names[i].name =
9293
0
      zend_resolve_const_class_name_reference(class_ast, "interface name");
9294
0
    interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
9295
0
  }
9296
9297
0
  ce->num_interfaces = list->children;
9298
0
  ce->interface_names = interface_names;
9299
0
}
9300
/* }}} */
9301
9302
static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl)
9303
0
{
9304
0
  zend_string *filename = CG(active_op_array)->filename;
9305
0
  uint32_t start_lineno = decl->start_lineno;
9306
9307
  /* Use parent or first interface as prefix. */
9308
0
  zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS);
9309
0
  if (decl->child[0]) {
9310
0
    prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name");
9311
0
  } else if (decl->child[1]) {
9312
0
    const zend_ast_list *list = zend_ast_get_list(decl->child[1]);
9313
0
    prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name");
9314
0
  }
9315
9316
0
  zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32,
9317
0
    ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++);
9318
0
  zend_string_release(prefix);
9319
0
  return zend_new_interned_string(result);
9320
0
}
9321
9322
static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast)
9323
0
{
9324
0
  ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM);
9325
0
  zend_type type = zend_compile_typename(enum_backing_type_ast);
9326
0
  uint32_t type_mask = ZEND_TYPE_PURE_MASK(type);
9327
0
  if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) {
9328
0
    zend_string *type_string = zend_type_to_string(type);
9329
0
    zend_error_noreturn(E_COMPILE_ERROR,
9330
0
      "Enum backing type must be int or string, %s given",
9331
0
      ZSTR_VAL(type_string));
9332
0
  }
9333
0
  if (type_mask == MAY_BE_LONG) {
9334
0
    ce->enum_backing_type = IS_LONG;
9335
0
  } else {
9336
0
    ZEND_ASSERT(type_mask == MAY_BE_STRING);
9337
0
    ce->enum_backing_type = IS_STRING;
9338
0
  }
9339
0
  zend_type_release(type, 0);
9340
0
}
9341
9342
static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */
9343
0
{
9344
0
  const zend_ast_decl *decl = (const zend_ast_decl *) ast;
9345
0
  zend_ast *extends_ast = decl->child[0];
9346
0
  zend_ast *implements_ast = decl->child[1];
9347
0
  zend_ast *stmt_ast = decl->child[2];
9348
0
  zend_ast *enum_backing_type_ast = decl->child[4];
9349
0
  zend_string *name, *lcname;
9350
0
  zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
9351
0
  zend_op *opline;
9352
9353
0
  zend_class_entry *original_ce = CG(active_class_entry);
9354
9355
0
  if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
9356
0
    zend_string *unqualified_name = decl->name;
9357
9358
0
    if (CG(active_class_entry)) {
9359
0
      zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
9360
0
    }
9361
9362
0
    const char *type = "a class name";
9363
0
    if (decl->flags & ZEND_ACC_ENUM) {
9364
0
      type = "an enum name";
9365
0
    } else if (decl->flags & ZEND_ACC_INTERFACE) {
9366
0
      type = "an interface name";
9367
0
    } else if (decl->flags & ZEND_ACC_TRAIT) {
9368
0
      type = "a trait name";
9369
0
    }
9370
0
    zend_assert_valid_class_name(unqualified_name, type);
9371
0
    name = zend_prefix_with_ns(unqualified_name);
9372
0
    name = zend_new_interned_string(name);
9373
0
    lcname = zend_string_tolower(name);
9374
9375
0
    if (FC(imports)) {
9376
0
      zend_string *import_name =
9377
0
        zend_hash_find_ptr_lc(FC(imports), unqualified_name);
9378
0
      if (import_name && !zend_string_equals_ci(lcname, import_name)) {
9379
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s "
9380
0
            "(previously declared as local import)", ZSTR_VAL(name));
9381
0
      }
9382
0
    }
9383
9384
0
    zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS);
9385
0
  } else {
9386
    /* Find an anon class name that is not in use yet. */
9387
0
    name = NULL;
9388
0
    lcname = NULL;
9389
0
    do {
9390
0
      zend_tmp_string_release(name);
9391
0
      zend_tmp_string_release(lcname);
9392
0
      name = zend_generate_anon_class_name(decl);
9393
0
      lcname = zend_string_tolower(name);
9394
0
    } while (zend_hash_exists(CG(class_table), lcname));
9395
0
  }
9396
0
  lcname = zend_new_interned_string(lcname);
9397
9398
0
  ce->type = ZEND_USER_CLASS;
9399
0
  ce->name = name;
9400
0
  zend_initialize_class_data(ce, true);
9401
0
  if (!(decl->flags & ZEND_ACC_ANON_CLASS)) {
9402
0
    zend_alloc_ce_cache(ce->name);
9403
0
  }
9404
9405
0
  if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
9406
0
    ce->ce_flags |= ZEND_ACC_PRELOADED;
9407
0
    ZEND_MAP_PTR_NEW(ce->static_members_table);
9408
0
    ZEND_MAP_PTR_NEW(ce->mutable_data);
9409
0
  }
9410
9411
0
  ce->ce_flags |= decl->flags;
9412
0
  ce->info.user.filename = zend_string_copy(zend_get_compiled_filename());
9413
0
  ce->info.user.line_start = decl->start_lineno;
9414
0
  ce->info.user.line_end = decl->end_lineno;
9415
9416
0
  if (decl->doc_comment) {
9417
0
    ce->doc_comment = zend_string_copy(decl->doc_comment);
9418
0
  }
9419
9420
0
  if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
9421
    /* Serialization is not supported for anonymous classes */
9422
0
    ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
9423
0
  }
9424
9425
0
  if (extends_ast) {
9426
0
    ce->parent_name =
9427
0
      zend_resolve_const_class_name_reference(extends_ast, "class name");
9428
0
  }
9429
9430
0
  CG(active_class_entry) = ce;
9431
9432
0
  if (decl->child[3]) {
9433
0
    zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
9434
0
  }
9435
9436
0
  if (implements_ast) {
9437
0
    zend_compile_implements(implements_ast);
9438
0
  }
9439
9440
0
  if (ce->ce_flags & ZEND_ACC_ENUM) {
9441
0
    if (enum_backing_type_ast != NULL) {
9442
0
      zend_compile_enum_backing_type(ce, enum_backing_type_ast);
9443
0
    }
9444
0
    zend_enum_add_interfaces(ce);
9445
0
    zend_enum_register_props(ce);
9446
0
  }
9447
9448
0
  zend_compile_stmt(stmt_ast);
9449
9450
  /* Reset lineno for final opcodes and errors */
9451
0
  CG(zend_lineno) = ast->lineno;
9452
9453
0
  if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
9454
0
    zend_verify_abstract_class(ce);
9455
0
  }
9456
9457
0
  CG(active_class_entry) = original_ce;
9458
9459
0
  if (toplevel) {
9460
0
    ce->ce_flags |= ZEND_ACC_TOP_LEVEL;
9461
0
  }
9462
9463
  /* We currently don't early-bind classes that implement interfaces or use traits */
9464
0
  if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9465
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
9466
   /* See zend_link_hooked_object_iter(). */
9467
   && !ce->num_hooked_props
9468
#endif
9469
0
   && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) {
9470
0
    if (toplevel) {
9471
0
      if (extends_ast) {
9472
0
        zend_class_entry *parent_ce = zend_lookup_class_ex(
9473
0
          ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
9474
9475
0
        if (parent_ce
9476
0
         && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) {
9477
0
          if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) {
9478
0
            zend_string_release(lcname);
9479
0
            return;
9480
0
          }
9481
0
        }
9482
0
      } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) {
9483
0
        zend_string_release(lcname);
9484
0
        zend_build_properties_info_table(ce);
9485
0
        zend_inheritance_check_override(ce);
9486
0
        ce->ce_flags |= ZEND_ACC_LINKED;
9487
0
        zend_observer_class_linked_notify(ce, lcname);
9488
0
        return;
9489
0
      } else {
9490
0
        goto link_unbound;
9491
0
      }
9492
0
    } else if (!extends_ast) {
9493
0
link_unbound:
9494
      /* Link unbound simple class */
9495
0
      zend_build_properties_info_table(ce);
9496
0
      zend_inheritance_check_override(ce);
9497
0
      ce->ce_flags |= ZEND_ACC_LINKED;
9498
0
    }
9499
0
  }
9500
9501
0
  opline = get_next_op();
9502
9503
0
  if (ce->parent_name) {
9504
    /* Lowercased parent name */
9505
0
    zend_string *lc_parent_name = zend_string_tolower(ce->parent_name);
9506
0
    opline->op2_type = IS_CONST;
9507
0
    LITERAL_STR(opline->op2, lc_parent_name);
9508
0
  }
9509
9510
0
  opline->op1_type = IS_CONST;
9511
  /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table.
9512
   * However, by this point another thread may have caused `lcname` to be added in the interned string table.
9513
   * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use
9514
   * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the
9515
   * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using
9516
   * zend_add_literal_string() which gives us the new value. */
9517
0
  opline->op1.constant = zend_add_literal_string(&lcname);
9518
9519
0
  if (decl->flags & ZEND_ACC_ANON_CLASS) {
9520
0
    opline->opcode = ZEND_DECLARE_ANON_CLASS;
9521
0
    opline->extended_value = zend_alloc_cache_slot();
9522
0
    zend_make_var_result(result, opline);
9523
0
    if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) {
9524
      /* We checked above that the class name is not used. This really shouldn't happen. */
9525
0
      zend_error_noreturn(E_ERROR,
9526
0
        "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name));
9527
0
    }
9528
0
  } else {
9529
    /* Generate RTD keys until we find one that isn't in use yet. */
9530
0
    zend_string *key = NULL;
9531
0
    do {
9532
0
      zend_tmp_string_release(key);
9533
0
      key = zend_build_runtime_definition_key(lcname, decl->start_lineno);
9534
0
    } while (!zend_hash_add_ptr(CG(class_table), key, ce));
9535
9536
    /* RTD key is placed after lcname literal in op1 */
9537
0
    zend_add_literal_string(&key);
9538
9539
0
    opline->opcode = ZEND_DECLARE_CLASS;
9540
0
    if (toplevel
9541
0
       && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING)
9542
        /* We currently don't early-bind classes that implement interfaces or use traits */
9543
0
       && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks
9544
0
    ) {
9545
0
      if (!extends_ast) {
9546
        /* Use empty string for classes without parents to avoid new handler, and special
9547
         * handling of zend_early_binding. */
9548
0
        opline->op2_type = IS_CONST;
9549
0
        LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC());
9550
0
      }
9551
0
      CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING;
9552
0
      opline->opcode = ZEND_DECLARE_CLASS_DELAYED;
9553
0
      opline->extended_value = zend_alloc_cache_slot();
9554
0
      opline->result_type = IS_UNUSED;
9555
0
      opline->result.opline_num = -1;
9556
0
    }
9557
0
  }
9558
0
}
9559
/* }}} */
9560
9561
static void zend_compile_enum_case(zend_ast *ast)
9562
0
{
9563
0
  zend_class_entry *enum_class = CG(active_class_entry);
9564
0
  if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) {
9565
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums");
9566
0
  }
9567
9568
0
  zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0]));
9569
0
  zend_string *enum_class_name = enum_class->name;
9570
9571
0
  zval class_name_zval;
9572
0
  ZVAL_STR_COPY(&class_name_zval, enum_class_name);
9573
0
  zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval);
9574
9575
0
  zval case_name_zval;
9576
0
  ZVAL_STR_COPY(&case_name_zval, enum_case_name);
9577
0
  zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval);
9578
9579
0
  zend_ast *case_value_ast = ast->child[1];
9580
  // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval
9581
0
  ast->child[1] = NULL;
9582
0
  if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) {
9583
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value",
9584
0
      ZSTR_VAL(enum_case_name),
9585
0
      ZSTR_VAL(enum_class_name));
9586
0
  } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) {
9587
0
    zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value",
9588
0
      ZSTR_VAL(enum_case_name),
9589
0
      ZSTR_VAL(enum_class_name));
9590
0
  }
9591
9592
0
  zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, class_name_ast, case_name_ast, case_value_ast);
9593
9594
0
  zval value_zv;
9595
0
  zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false);
9596
9597
  /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */
9598
0
  zend_ast *doc_comment_ast = ast->child[2];
9599
0
  zend_string *doc_comment = NULL;
9600
0
  if (doc_comment_ast) {
9601
0
    doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
9602
0
  }
9603
9604
0
  zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment);
9605
0
  ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE;
9606
0
  zend_ast_destroy(const_enum_init_ast);
9607
9608
0
  zend_ast *attr_ast = ast->child[3];
9609
0
  if (attr_ast) {
9610
0
    zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0);
9611
9612
0
    zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1);
9613
9614
0
    if (deprecated) {
9615
0
      ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED;
9616
0
    }
9617
0
  }
9618
0
}
9619
9620
static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
9621
0
{
9622
0
  switch (type) {
9623
0
    case ZEND_SYMBOL_CLASS:
9624
0
      if (!FC(imports)) {
9625
0
        FC(imports) = emalloc(sizeof(HashTable));
9626
0
        zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
9627
0
      }
9628
0
      return FC(imports);
9629
0
    case ZEND_SYMBOL_FUNCTION:
9630
0
      if (!FC(imports_function)) {
9631
0
        FC(imports_function) = emalloc(sizeof(HashTable));
9632
0
        zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
9633
0
      }
9634
0
      return FC(imports_function);
9635
0
    case ZEND_SYMBOL_CONST:
9636
0
      if (!FC(imports_const)) {
9637
0
        FC(imports_const) = emalloc(sizeof(HashTable));
9638
0
        zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
9639
0
      }
9640
0
      return FC(imports_const);
9641
0
    EMPTY_SWITCH_DEFAULT_CASE()
9642
0
  }
9643
9644
0
  return NULL;
9645
0
}
9646
/* }}} */
9647
9648
static char *zend_get_use_type_str(uint32_t type) /* {{{ */
9649
0
{
9650
0
  switch (type) {
9651
0
    case ZEND_SYMBOL_CLASS:
9652
0
      return "";
9653
0
    case ZEND_SYMBOL_FUNCTION:
9654
0
      return " function";
9655
0
    case ZEND_SYMBOL_CONST:
9656
0
      return " const";
9657
0
    EMPTY_SWITCH_DEFAULT_CASE()
9658
0
  }
9659
9660
0
  return " unknown";
9661
0
}
9662
/* }}} */
9663
9664
static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */
9665
0
{
9666
0
  if (zend_string_equals_ci(old_name, check_name)) {
9667
0
    return;
9668
0
  }
9669
9670
0
  zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9671
0
    "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9672
0
}
9673
/* }}} */
9674
9675
static void zend_compile_use(zend_ast *ast) /* {{{ */
9676
0
{
9677
0
  const zend_ast_list *list = zend_ast_get_list(ast);
9678
0
  uint32_t i;
9679
0
  zend_string *current_ns = FC(current_namespace);
9680
0
  uint32_t type = ast->attr;
9681
0
  HashTable *current_import = zend_get_import_ht(type);
9682
0
  bool case_sensitive = type == ZEND_SYMBOL_CONST;
9683
9684
0
  for (i = 0; i < list->children; ++i) {
9685
0
    const zend_ast *use_ast = list->child[i];
9686
0
    zend_ast *old_name_ast = use_ast->child[0];
9687
0
    zend_ast *new_name_ast = use_ast->child[1];
9688
0
    zend_string *old_name = zend_ast_get_str(old_name_ast);
9689
0
    zend_string *new_name, *lookup_name;
9690
9691
0
    if (new_name_ast) {
9692
0
      new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
9693
0
    } else {
9694
0
      const char *unqualified_name;
9695
0
      size_t unqualified_name_len;
9696
0
      if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
9697
        /* The form "use A\B" is equivalent to "use A\B as B" */
9698
0
        new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
9699
0
      } else {
9700
0
        new_name = zend_string_copy(old_name);
9701
9702
0
        if (!current_ns) {
9703
0
          zend_error(E_WARNING, "The use statement with non-compound name '%s' "
9704
0
            "has no effect", ZSTR_VAL(new_name));
9705
0
        }
9706
0
      }
9707
0
    }
9708
9709
0
    if (case_sensitive) {
9710
0
      lookup_name = zend_string_copy(new_name);
9711
0
    } else {
9712
0
      lookup_name = zend_string_tolower(new_name);
9713
0
    }
9714
9715
0
    if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) {
9716
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
9717
0
        "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
9718
0
    }
9719
9720
0
    if (current_ns) {
9721
0
      zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
9722
0
      zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
9723
0
      ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
9724
0
      memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
9725
9726
0
      if (zend_have_seen_symbol(ns_name, type)) {
9727
0
        zend_check_already_in_use(type, old_name, new_name, ns_name);
9728
0
      }
9729
9730
0
      zend_string_efree(ns_name);
9731
0
    } else if (zend_have_seen_symbol(lookup_name, type)) {
9732
0
      zend_check_already_in_use(type, old_name, new_name, lookup_name);
9733
0
    }
9734
9735
0
    zend_string_addref(old_name);
9736
0
    old_name = zend_new_interned_string(old_name);
9737
0
    if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
9738
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
9739
0
        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
9740
0
    }
9741
9742
0
    zend_string_release_ex(lookup_name, 0);
9743
0
    zend_string_release_ex(new_name, 0);
9744
0
  }
9745
0
}
9746
/* }}} */
9747
9748
static void zend_compile_group_use(const zend_ast *ast) /* {{{ */
9749
0
{
9750
0
  uint32_t i;
9751
0
  const zend_string *ns = zend_ast_get_str(ast->child[0]);
9752
0
  const zend_ast_list *list = zend_ast_get_list(ast->child[1]);
9753
9754
0
  for (i = 0; i < list->children; i++) {
9755
0
    zend_ast *inline_use, *use = list->child[i];
9756
0
    zval *name_zval = zend_ast_get_zval(use->child[0]);
9757
0
    zend_string *name = Z_STR_P(name_zval);
9758
0
    zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
9759
0
    zend_string_release_ex(name, 0);
9760
0
    ZVAL_STR(name_zval, compound_ns);
9761
0
    inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
9762
0
    inline_use->attr = ast->attr ? ast->attr : use->attr;
9763
0
    zend_compile_use(inline_use);
9764
0
  }
9765
0
}
9766
/* }}} */
9767
9768
static void zend_compile_const_decl(zend_ast *ast) /* {{{ */
9769
0
{
9770
0
  zend_ast_list *list = zend_ast_get_list(ast);
9771
0
  uint32_t i;
9772
0
  zend_ast *attributes_ast = NULL;
9773
0
  zend_op *last_op = NULL;
9774
0
  for (i = 0; i < list->children; ++i) {
9775
0
    zend_ast *const_ast = list->child[i];
9776
0
    if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) {
9777
0
      ZEND_ASSERT(i == list->children - 1);
9778
0
      attributes_ast = const_ast;
9779
0
      continue;
9780
0
    }
9781
0
    ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM);
9782
0
    zend_ast *name_ast = const_ast->child[0];
9783
0
    zend_ast **value_ast_ptr = &const_ast->child[1];
9784
0
    zend_string *unqualified_name = zend_ast_get_str(name_ast);
9785
9786
0
    zend_string *name;
9787
0
    znode name_node, value_node;
9788
0
    zval *value_zv = &value_node.u.constant;
9789
9790
0
    value_node.op_type = IS_CONST;
9791
0
    zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true);
9792
9793
0
    if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
9794
0
      zend_error_noreturn(E_COMPILE_ERROR,
9795
0
        "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
9796
0
    }
9797
9798
0
    name = zend_prefix_with_ns(unqualified_name);
9799
0
    name = zend_new_interned_string(name);
9800
9801
0
    if (FC(imports_const)) {
9802
0
      zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
9803
0
      if (import_name && !zend_string_equals(import_name, name)) {
9804
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
9805
0
          "the name is already in use", ZSTR_VAL(name));
9806
0
      }
9807
0
    }
9808
9809
0
    name_node.op_type = IS_CONST;
9810
0
    ZVAL_STR(&name_node.u.constant, name);
9811
9812
0
    last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
9813
9814
0
    zend_register_seen_symbol(name, ZEND_SYMBOL_CONST);
9815
0
  }
9816
0
  if (attributes_ast == NULL) {
9817
0
    return;
9818
0
  }
9819
  /* Validate: attributes can only be applied to one constant at a time
9820
   * Since we store the AST for the attributes in the list of children,
9821
   * there should be exactly 2 children. */
9822
0
  if (list->children > 2) {
9823
0
    zend_error_noreturn(
9824
0
      E_COMPILE_ERROR,
9825
0
      "Cannot apply attributes to multiple constants at once"
9826
0
    );
9827
0
  }
9828
9829
0
  HashTable *attributes = NULL;
9830
0
  zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0);
9831
9832
0
  ZEND_ASSERT(last_op != NULL);
9833
0
  last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST;
9834
0
  znode attribs_node;
9835
0
  attribs_node.op_type = IS_CONST;
9836
0
  ZVAL_PTR(&attribs_node.u.constant, attributes);
9837
0
  zend_emit_op_data(&attribs_node);
9838
0
  CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS;
9839
0
}
9840
/* }}}*/
9841
9842
static void zend_compile_namespace(const zend_ast *ast) /* {{{ */
9843
0
{
9844
0
  zend_ast *name_ast = ast->child[0];
9845
0
  zend_ast *stmt_ast = ast->child[1];
9846
0
  zend_string *name;
9847
0
  bool with_bracket = stmt_ast != NULL;
9848
9849
  /* handle mixed syntax declaration or nested namespaces */
9850
0
  if (!FC(has_bracketed_namespaces)) {
9851
0
    if (FC(current_namespace)) {
9852
      /* previous namespace declarations were unbracketed */
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
      }
9857
0
    }
9858
0
  } else {
9859
    /* previous namespace declarations were bracketed */
9860
0
    if (!with_bracket) {
9861
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
9862
0
        "with unbracketed namespace declarations");
9863
0
    } else if (FC(current_namespace) || FC(in_namespace)) {
9864
0
      zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
9865
0
    }
9866
0
  }
9867
9868
0
  bool is_first_namespace = (!with_bracket && !FC(current_namespace))
9869
0
    || (with_bracket && !FC(has_bracketed_namespaces));
9870
0
  if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) {
9871
0
    zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
9872
0
      "the very first statement or after any declare call in the script");
9873
0
  }
9874
9875
0
  if (FC(current_namespace)) {
9876
0
    zend_string_release_ex(FC(current_namespace), 0);
9877
0
  }
9878
9879
0
  if (name_ast) {
9880
0
    name = zend_ast_get_str(name_ast);
9881
9882
0
    if (zend_string_equals_literal_ci(name, "namespace")) {
9883
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
9884
0
    }
9885
9886
0
    FC(current_namespace) = zend_string_copy(name);
9887
0
  } else {
9888
0
    FC(current_namespace) = NULL;
9889
0
  }
9890
9891
0
  zend_reset_import_tables();
9892
9893
0
  FC(in_namespace) = 1;
9894
0
  if (with_bracket) {
9895
0
    FC(has_bracketed_namespaces) = 1;
9896
0
  }
9897
9898
0
  if (stmt_ast) {
9899
0
    zend_compile_top_stmt(stmt_ast);
9900
0
    zend_end_namespace();
9901
0
  }
9902
0
}
9903
/* }}} */
9904
9905
static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */
9906
0
{
9907
0
  zend_ast *offset_ast = ast->child[0];
9908
0
  zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
9909
9910
0
  const char const_name[] = "__COMPILER_HALT_OFFSET__";
9911
9912
0
  if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
9913
0
    zend_error_noreturn(E_COMPILE_ERROR,
9914
0
      "__HALT_COMPILER() can only be used from the outermost scope");
9915
0
  }
9916
9917
0
  const zend_string *filename = zend_get_compiled_filename();
9918
0
  zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
9919
0
    ZSTR_VAL(filename), ZSTR_LEN(filename), false);
9920
9921
  /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in
9922
   * case this file was already included. */
9923
0
  if (!zend_hash_find(EG(zend_constants), name)) {
9924
0
    zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0);
9925
0
  }
9926
0
  zend_string_release_ex(name, 0);
9927
0
}
9928
/* }}} */
9929
9930
static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */
9931
0
{
9932
0
  const zend_op_array *op_array = CG(active_op_array);
9933
0
  const zend_class_entry *ce = CG(active_class_entry);
9934
9935
0
  switch (ast->attr) {
9936
0
    case T_LINE:
9937
0
      ZVAL_LONG(zv, ast->lineno);
9938
0
      break;
9939
0
    case T_FILE:
9940
0
      ZVAL_STR_COPY(zv, CG(compiled_filename));
9941
0
      break;
9942
0
    case T_DIR:
9943
0
    {
9944
0
      const zend_string *filename = CG(compiled_filename);
9945
0
      zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
9946
#ifdef ZEND_WIN32
9947
      ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9948
#else
9949
0
      ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
9950
0
#endif
9951
9952
0
      if (zend_string_equals_literal(dirname, ".")) {
9953
0
        dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
9954
0
#ifdef HAVE_GETCWD
9955
0
        ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
9956
#elif defined(HAVE_GETWD)
9957
        ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
9958
#endif
9959
0
        ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
9960
0
      }
9961
9962
0
      ZVAL_STR(zv, dirname);
9963
0
      break;
9964
0
    }
9965
0
    case T_FUNC_C:
9966
0
      if (op_array && op_array->function_name) {
9967
0
        ZVAL_STR_COPY(zv, op_array->function_name);
9968
0
      } else {
9969
0
        ZVAL_EMPTY_STRING(zv);
9970
0
      }
9971
0
      break;
9972
0
    case T_PROPERTY_C: {
9973
0
      zend_string *prop_info_name = CG(context).active_property_info_name;
9974
0
      if (prop_info_name) {
9975
0
        ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name));
9976
0
      } else {
9977
0
        ZVAL_EMPTY_STRING(zv);
9978
0
      }
9979
0
      break;
9980
0
    }
9981
0
    case T_METHOD_C:
9982
      /* Detect whether we are directly inside a class (e.g. a class constant) and treat
9983
       * this as not being inside a function. */
9984
0
      if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) {
9985
0
        op_array = NULL;
9986
0
      }
9987
0
      if (op_array && op_array->function_name) {
9988
0
        if (op_array->scope) {
9989
0
          ZVAL_NEW_STR(zv,
9990
0
            zend_create_member_string(op_array->scope->name, op_array->function_name));
9991
0
        } else {
9992
0
          ZVAL_STR_COPY(zv, op_array->function_name);
9993
0
        }
9994
0
      } else {
9995
0
        ZVAL_EMPTY_STRING(zv);
9996
0
      }
9997
0
      break;
9998
0
    case T_CLASS_C:
9999
0
      if (ce) {
10000
0
        if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10001
0
          return 0;
10002
0
        } else {
10003
0
          ZVAL_STR_COPY(zv, ce->name);
10004
0
        }
10005
0
      } else {
10006
0
        ZVAL_EMPTY_STRING(zv);
10007
0
      }
10008
0
      break;
10009
0
    case T_TRAIT_C:
10010
0
      if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
10011
0
        ZVAL_STR_COPY(zv, ce->name);
10012
0
      } else {
10013
0
        ZVAL_EMPTY_STRING(zv);
10014
0
      }
10015
0
      break;
10016
0
    case T_NS_C:
10017
0
      if (FC(current_namespace)) {
10018
0
        ZVAL_STR_COPY(zv, FC(current_namespace));
10019
0
      } else {
10020
0
        ZVAL_EMPTY_STRING(zv);
10021
0
      }
10022
0
      break;
10023
0
    EMPTY_SWITCH_DEFAULT_CASE()
10024
0
  }
10025
10026
0
  return 1;
10027
0
}
10028
/* }}} */
10029
10030
ZEND_API bool zend_is_op_long_compatible(const zval *op)
10031
0
{
10032
0
  if (Z_TYPE_P(op) == IS_ARRAY) {
10033
0
    return false;
10034
0
  }
10035
10036
0
  if (Z_TYPE_P(op) == IS_DOUBLE
10037
0
    && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) {
10038
0
    return false;
10039
0
  }
10040
10041
0
  if (Z_TYPE_P(op) == IS_STRING) {
10042
0
    double dval = 0;
10043
0
    uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval);
10044
0
    if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) {
10045
0
      return false;
10046
0
    }
10047
0
  }
10048
10049
0
  return true;
10050
0
}
10051
10052
ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */
10053
0
{
10054
0
  if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) {
10055
    /* Array to string warning. */
10056
0
    return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY;
10057
0
  }
10058
10059
0
  if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
10060
0
               || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
10061
0
               || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
10062
    /* Only the numeric operations throw errors. */
10063
0
    return 0;
10064
0
  }
10065
10066
0
  if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) {
10067
0
    if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) {
10068
      /* Adding two arrays is allowed. */
10069
0
      return 0;
10070
0
    }
10071
10072
    /* Numeric operators throw when one of the operands is an array. */
10073
0
    return 1;
10074
0
  }
10075
10076
  /* While basic arithmetic operators always produce numeric string errors,
10077
   * bitwise operators don't produce errors if both operands are strings */
10078
0
  if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
10079
0
    && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
10080
0
    return 0;
10081
0
  }
10082
10083
0
  if (Z_TYPE_P(op1) == IS_STRING
10084
0
    && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
10085
0
    return 1;
10086
0
  }
10087
10088
0
  if (Z_TYPE_P(op2) == IS_STRING
10089
0
    && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
10090
0
    return 1;
10091
0
  }
10092
10093
  /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */
10094
0
  if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR
10095
0
      || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) {
10096
0
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) {
10097
0
      return 1;
10098
0
    }
10099
0
  }
10100
10101
0
  if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) {
10102
    /* Division by zero throws an error. */
10103
0
    return 1;
10104
0
  }
10105
10106
  /* Mod is an operation that will cast float/float-strings to integers which might
10107
     produce float to int incompatible errors, and also cannot be divided by 0 */
10108
0
  if (opcode == ZEND_MOD) {
10109
0
    if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) {
10110
0
      return 1;
10111
0
    }
10112
0
  }
10113
10114
0
  if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) {
10115
    /* 0 ** (<0) throws a division by zero error. */
10116
0
    return 1;
10117
0
  }
10118
0
  if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) {
10119
    /* Shift by negative number throws an error. */
10120
0
    return 1;
10121
0
  }
10122
10123
0
  return 0;
10124
0
}
10125
/* }}} */
10126
10127
static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
10128
0
{
10129
0
  if (zend_binary_op_produces_error(opcode, op1, op2)) {
10130
0
    return 0;
10131
0
  }
10132
10133
0
  const binary_op_type fn = get_binary_op(opcode);
10134
0
  fn(result, op1, op2);
10135
0
  return 1;
10136
0
}
10137
/* }}} */
10138
10139
ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op)
10140
0
{
10141
0
  if (opcode == ZEND_BW_NOT) {
10142
    /* BW_NOT on string does not convert the string into an integer. */
10143
0
    if (Z_TYPE_P(op) == IS_STRING) {
10144
0
      return 0;
10145
0
    }
10146
0
    return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op);
10147
0
  }
10148
  /* Can happen when called from zend_optimizer_eval_unary_op() */
10149
0
  if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) {
10150
    /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */
10151
0
    return Z_TYPE_P(op) == IS_DOUBLE;
10152
0
  }
10153
10154
0
  return 0;
10155
0
}
10156
10157
static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
10158
0
{
10159
0
  if (zend_unary_op_produces_error(opcode, op)) {
10160
0
    return 0;
10161
0
  }
10162
10163
0
  const unary_op_type fn = get_unary_op(opcode);
10164
0
  fn(result, op);
10165
0
  return 1;
10166
0
}
10167
/* }}} */
10168
10169
static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
10170
0
{
10171
0
  zval right;
10172
0
  ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10173
0
  return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right);
10174
0
}
10175
/* }}} */
10176
10177
static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
10178
0
{
10179
0
  const binary_op_type fn = kind == ZEND_AST_GREATER
10180
0
    ? is_smaller_function : is_smaller_or_equal_function;
10181
0
  fn(result, op2, op1);
10182
0
}
10183
/* }}} */
10184
10185
static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
10186
0
{
10187
0
  const zend_ast_list *list = zend_ast_get_list(ast);
10188
0
  zend_ast *last_elem_ast = NULL;
10189
0
  uint32_t i;
10190
0
  bool is_constant = true;
10191
10192
0
  if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
10193
0
    zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
10194
0
  }
10195
10196
  /* First ensure that *all* child nodes are constant and by-val */
10197
0
  for (i = 0; i < list->children; ++i) {
10198
0
    zend_ast *elem_ast = list->child[i];
10199
10200
0
    if (elem_ast == NULL) {
10201
      /* Report error at line of last non-empty element */
10202
0
      if (last_elem_ast) {
10203
0
        CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast);
10204
0
      }
10205
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
10206
0
    }
10207
10208
0
    if (elem_ast->kind != ZEND_AST_UNPACK) {
10209
0
      zend_eval_const_expr(&elem_ast->child[0]);
10210
0
      zend_eval_const_expr(&elem_ast->child[1]);
10211
10212
0
      if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
10213
0
        || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
10214
0
      ) {
10215
0
        is_constant = false;
10216
0
      }
10217
0
    } else {
10218
0
      zend_eval_const_expr(&elem_ast->child[0]);
10219
10220
0
      if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) {
10221
0
        is_constant = false;
10222
0
      }
10223
0
    }
10224
10225
0
    last_elem_ast = elem_ast;
10226
0
  }
10227
10228
0
  if (!is_constant) {
10229
0
    return 0;
10230
0
  }
10231
10232
0
  if (!list->children) {
10233
0
    ZVAL_EMPTY_ARRAY(result);
10234
0
    return 1;
10235
0
  }
10236
10237
0
  array_init_size(result, list->children);
10238
0
  for (i = 0; i < list->children; ++i) {
10239
0
    const zend_ast *elem_ast = list->child[i];
10240
0
    zend_ast *value_ast = elem_ast->child[0];
10241
0
    zend_ast *key_ast;
10242
10243
0
    zval *value = zend_ast_get_zval(value_ast);
10244
0
    if (elem_ast->kind == ZEND_AST_UNPACK) {
10245
0
      if (Z_TYPE_P(value) == IS_ARRAY) {
10246
0
        const HashTable *ht = Z_ARRVAL_P(value);
10247
0
        zval *val;
10248
0
        zend_string *key;
10249
10250
0
        ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
10251
0
          if (key) {
10252
0
            zend_hash_update(Z_ARRVAL_P(result), key, val);
10253
0
          } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) {
10254
0
            zval_ptr_dtor(result);
10255
0
            return 0;
10256
0
          }
10257
0
          Z_TRY_ADDREF_P(val);
10258
0
        } ZEND_HASH_FOREACH_END();
10259
10260
0
        continue;
10261
0
      } else {
10262
0
        zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value));
10263
0
      }
10264
0
    }
10265
10266
0
    Z_TRY_ADDREF_P(value);
10267
10268
0
    key_ast = elem_ast->child[1];
10269
0
    if (key_ast) {
10270
0
      const zval *key = zend_ast_get_zval(key_ast);
10271
0
      switch (Z_TYPE_P(key)) {
10272
0
        case IS_LONG:
10273
0
          zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
10274
0
          break;
10275
0
        case IS_STRING:
10276
0
          zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
10277
0
          break;
10278
0
        case IS_DOUBLE: {
10279
0
          zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key));
10280
          /* Incompatible float will generate an error, leave this to run-time */
10281
0
          if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
10282
0
            goto fail;
10283
0
          }
10284
0
          zend_hash_index_update(Z_ARRVAL_P(result), lval, value);
10285
0
          break;
10286
0
        }
10287
0
        case IS_FALSE:
10288
0
          zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
10289
0
          break;
10290
0
        case IS_TRUE:
10291
0
          zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
10292
0
          break;
10293
0
        case IS_NULL:
10294
          /* Null key will generate a warning at run-time. */
10295
0
          goto fail;
10296
0
        default:
10297
0
          zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
10298
0
          break;
10299
0
      }
10300
0
    } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
10301
0
fail:
10302
0
      zval_ptr_dtor_nogc(value);
10303
0
      zval_ptr_dtor(result);
10304
0
      return 0;
10305
0
    }
10306
0
  }
10307
10308
0
  return 1;
10309
0
}
10310
/* }}} */
10311
10312
static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
10313
0
{
10314
0
  zend_ast *left_ast = ast->child[0];
10315
0
  zend_ast *right_ast = ast->child[1];
10316
0
  uint32_t opcode = ast->attr;
10317
10318
0
  znode left_node, right_node;
10319
10320
0
  zend_compile_expr(&left_node, left_ast);
10321
0
  zend_compile_expr(&right_node, right_ast);
10322
10323
0
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10324
0
    if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
10325
0
        &left_node.u.constant, &right_node.u.constant)
10326
0
    ) {
10327
0
      result->op_type = IS_CONST;
10328
0
      zval_ptr_dtor(&left_node.u.constant);
10329
0
      zval_ptr_dtor(&right_node.u.constant);
10330
0
      return;
10331
0
    }
10332
0
  }
10333
10334
0
  do {
10335
0
    if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) {
10336
      /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */
10337
0
      if (left_node.op_type == IS_CONST) {
10338
0
        if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) {
10339
0
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL);
10340
0
          opline->extended_value =
10341
0
            (opcode == ZEND_IS_IDENTICAL) ?
10342
0
              (1 << Z_TYPE(left_node.u.constant)) :
10343
0
              (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant)));
10344
0
          return;
10345
0
        }
10346
0
      } else if (right_node.op_type == IS_CONST) {
10347
0
        if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) {
10348
0
          zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL);
10349
0
          opline->extended_value =
10350
0
            (opcode == ZEND_IS_IDENTICAL) ?
10351
0
              (1 << Z_TYPE(right_node.u.constant)) :
10352
0
              (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant)));
10353
0
          return;
10354
0
        }
10355
0
      }
10356
0
    } else if (opcode == ZEND_CONCAT) {
10357
      /* convert constant operands to strings at compile-time */
10358
0
      if (left_node.op_type == IS_CONST) {
10359
0
        if (Z_TYPE(left_node.u.constant) == IS_ARRAY) {
10360
0
          zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING;
10361
0
        } else {
10362
0
          convert_to_string(&left_node.u.constant);
10363
0
        }
10364
0
      }
10365
0
      if (right_node.op_type == IS_CONST) {
10366
0
        if (Z_TYPE(right_node.u.constant) == IS_ARRAY) {
10367
0
          zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING;
10368
0
        } else {
10369
0
          convert_to_string(&right_node.u.constant);
10370
0
        }
10371
0
      }
10372
0
      if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10373
0
        opcode = ZEND_FAST_CONCAT;
10374
0
      }
10375
0
    }
10376
0
    zend_emit_op_tmp(result, opcode, &left_node, &right_node);
10377
0
  } while (0);
10378
0
}
10379
/* }}} */
10380
10381
/* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
10382
 * evaluation order. */
10383
static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
10384
0
{
10385
0
  zend_ast *left_ast = ast->child[0];
10386
0
  zend_ast *right_ast = ast->child[1];
10387
0
  znode left_node, right_node;
10388
10389
0
  ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
10390
10391
0
  zend_compile_expr(&left_node, left_ast);
10392
0
  zend_compile_expr(&right_node, right_ast);
10393
10394
0
  if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
10395
0
    result->op_type = IS_CONST;
10396
0
    zend_ct_eval_greater(&result->u.constant, ast->kind,
10397
0
      &left_node.u.constant, &right_node.u.constant);
10398
0
    zval_ptr_dtor(&left_node.u.constant);
10399
0
    zval_ptr_dtor(&right_node.u.constant);
10400
0
    return;
10401
0
  }
10402
10403
0
  zend_emit_op_tmp(result,
10404
0
    ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
10405
0
    &right_node, &left_node);
10406
0
}
10407
/* }}} */
10408
10409
static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */
10410
0
{
10411
0
  zend_ast *expr_ast = ast->child[0];
10412
0
  uint32_t opcode = ast->attr;
10413
10414
0
  znode expr_node;
10415
0
  zend_compile_expr(&expr_node, expr_ast);
10416
10417
0
  if (expr_node.op_type == IS_CONST
10418
0
      && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) {
10419
0
    result->op_type = IS_CONST;
10420
0
    zval_ptr_dtor(&expr_node.u.constant);
10421
0
    return;
10422
0
  }
10423
10424
0
  zend_emit_op_tmp(result, opcode, &expr_node, NULL);
10425
0
}
10426
/* }}} */
10427
10428
static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
10429
0
{
10430
0
  zend_ast *expr_ast = ast->child[0];
10431
0
  znode expr_node, right_node;
10432
10433
0
  ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
10434
10435
0
  zend_compile_expr(&expr_node, expr_ast);
10436
10437
0
  if (expr_node.op_type == IS_CONST
10438
0
    && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
10439
0
    result->op_type = IS_CONST;
10440
0
    zval_ptr_dtor(&expr_node.u.constant);
10441
0
    return;
10442
0
  }
10443
10444
0
  right_node.op_type = IS_CONST;
10445
0
  ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
10446
0
  zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node);
10447
0
}
10448
/* }}} */
10449
10450
static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
10451
0
{
10452
0
  zend_ast *left_ast = ast->child[0];
10453
0
  zend_ast *right_ast = ast->child[1];
10454
10455
0
  znode left_node, right_node;
10456
0
  zend_op *opline_jmpz, *opline_bool;
10457
0
  uint32_t opnum_jmpz;
10458
10459
0
  ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
10460
10461
0
  zend_compile_expr(&left_node, left_ast);
10462
10463
0
  if (left_node.op_type == IS_CONST) {
10464
0
    if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
10465
0
     || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
10466
0
      result->op_type = IS_CONST;
10467
0
      ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
10468
0
    } else {
10469
0
      zend_compile_expr(&right_node, right_ast);
10470
10471
0
      if (right_node.op_type == IS_CONST) {
10472
0
        result->op_type = IS_CONST;
10473
0
        ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
10474
10475
0
        zval_ptr_dtor(&right_node.u.constant);
10476
0
      } else {
10477
0
        zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
10478
0
      }
10479
0
    }
10480
10481
0
    zval_ptr_dtor(&left_node.u.constant);
10482
0
    return;
10483
0
  }
10484
10485
0
  opnum_jmpz = get_next_op_number();
10486
0
  opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
10487
0
    &left_node, NULL);
10488
10489
0
  if (left_node.op_type == IS_TMP_VAR) {
10490
0
    SET_NODE(opline_jmpz->result, &left_node);
10491
0
    GET_NODE(result, opline_jmpz->result);
10492
0
  } else {
10493
0
    zend_make_tmp_result(result, opline_jmpz);
10494
0
  }
10495
10496
0
  zend_compile_expr(&right_node, right_ast);
10497
10498
0
  opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
10499
0
  SET_NODE(opline_bool->result, result);
10500
10501
0
  zend_update_jump_target_to_next(opnum_jmpz);
10502
0
}
10503
/* }}} */
10504
10505
static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */
10506
0
{
10507
0
  zend_ast *var_ast = ast->child[0];
10508
0
  ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
10509
10510
0
  zend_ensure_writable_variable(var_ast);
10511
10512
0
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10513
0
    zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false);
10514
0
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
10515
0
    zend_make_tmp_result(result, opline);
10516
0
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10517
0
    zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false);
10518
0
    opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP;
10519
0
    zend_make_tmp_result(result, opline);
10520
0
  } else {
10521
0
    znode var_node;
10522
0
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10523
0
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10524
0
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10525
0
    }
10526
0
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
10527
0
      &var_node, NULL);
10528
0
  }
10529
0
}
10530
/* }}} */
10531
10532
static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */
10533
0
{
10534
0
  zend_ast *var_ast = ast->child[0];
10535
0
  ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
10536
10537
0
  zend_ensure_writable_variable(var_ast);
10538
10539
0
  if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) {
10540
0
    zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false);
10541
0
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
10542
0
    opline->result_type = IS_TMP_VAR;
10543
0
    result->op_type = IS_TMP_VAR;
10544
0
  } else if (var_ast->kind == ZEND_AST_STATIC_PROP) {
10545
0
    zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false);
10546
0
    opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP;
10547
0
    opline->result_type = IS_TMP_VAR;
10548
0
    result->op_type = IS_TMP_VAR;
10549
0
  } else {
10550
0
    znode var_node;
10551
0
    zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false);
10552
0
    if (opline && opline->opcode == ZEND_FETCH_DIM_RW) {
10553
0
      opline->extended_value = ZEND_FETCH_DIM_INCDEC;
10554
0
    }
10555
0
    zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
10556
0
      &var_node, NULL);
10557
0
  }
10558
0
}
10559
/* }}} */
10560
10561
static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */
10562
0
{
10563
0
  zend_ast *expr_ast = ast->child[0];
10564
0
  znode expr_node;
10565
0
  zend_op *opline;
10566
10567
0
  zend_compile_expr(&expr_node, expr_ast);
10568
10569
0
  if (ast->attr == _IS_BOOL) {
10570
0
    opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL);
10571
0
  } else if (ast->attr == IS_NULL) {
10572
0
    zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported");
10573
0
  } else {
10574
0
    opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
10575
0
    opline->extended_value = ast->attr;
10576
0
  }
10577
0
}
10578
/* }}} */
10579
10580
static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
10581
0
{
10582
0
  zend_ast *cond_ast = ast->child[0];
10583
0
  zend_ast *false_ast = ast->child[2];
10584
10585
0
  znode cond_node, false_node;
10586
0
  zend_op *opline_qm_assign;
10587
0
  uint32_t opnum_jmp_set;
10588
10589
0
  ZEND_ASSERT(ast->child[1] == NULL);
10590
10591
0
  zend_compile_expr(&cond_node, cond_ast);
10592
10593
0
  opnum_jmp_set = get_next_op_number();
10594
0
  zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
10595
10596
0
  zend_compile_expr(&false_node, false_ast);
10597
10598
0
  opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10599
0
  SET_NODE(opline_qm_assign->result, result);
10600
10601
0
  zend_update_jump_target_to_next(opnum_jmp_set);
10602
0
}
10603
/* }}} */
10604
10605
static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
10606
0
{
10607
0
  zend_ast *cond_ast = ast->child[0];
10608
0
  zend_ast *true_ast = ast->child[1];
10609
0
  zend_ast *false_ast = ast->child[2];
10610
10611
0
  znode cond_node, true_node, false_node;
10612
0
  zend_op *opline_qm_assign2;
10613
0
  uint32_t opnum_jmpz, opnum_jmp;
10614
10615
0
  if (cond_ast->kind == ZEND_AST_CONDITIONAL
10616
0
      && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) {
10617
0
    if (cond_ast->child[1]) {
10618
0
      if (true_ast) {
10619
0
        zend_error(E_COMPILE_ERROR,
10620
0
          "Unparenthesized `a ? b : c ? d : e` is not supported. "
10621
0
          "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`");
10622
0
      } else {
10623
0
        zend_error(E_COMPILE_ERROR,
10624
0
          "Unparenthesized `a ? b : c ?: d` is not supported. "
10625
0
          "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`");
10626
0
      }
10627
0
    } else {
10628
0
      if (true_ast) {
10629
0
        zend_error(E_COMPILE_ERROR,
10630
0
          "Unparenthesized `a ?: b ? c : d` is not supported. "
10631
0
          "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`");
10632
0
      } else {
10633
        /* This case is harmless:  (a ?: b) ?: c always produces the same result
10634
         * as a ?: (b ?: c). */
10635
0
      }
10636
0
    }
10637
0
  }
10638
10639
0
  if (!true_ast) {
10640
0
    zend_compile_shorthand_conditional(result, ast);
10641
0
    return;
10642
0
  }
10643
10644
0
  zend_compile_expr(&cond_node, cond_ast);
10645
10646
0
  opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
10647
10648
0
  zend_compile_expr(&true_node, true_ast);
10649
10650
0
  zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
10651
10652
0
  opnum_jmp = zend_emit_jump(0);
10653
10654
0
  zend_update_jump_target_to_next(opnum_jmpz);
10655
10656
0
  zend_compile_expr(&false_node, false_ast);
10657
10658
0
  opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
10659
0
  SET_NODE(opline_qm_assign2->result, result);
10660
10661
0
  zend_update_jump_target_to_next(opnum_jmp);
10662
0
}
10663
/* }}} */
10664
10665
static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
10666
0
{
10667
0
  zend_ast *expr_ast = ast->child[0];
10668
0
  zend_ast *default_ast = ast->child[1];
10669
10670
0
  znode expr_node, default_node;
10671
0
  zend_op *opline;
10672
0
  uint32_t opnum;
10673
10674
0
  zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false);
10675
10676
0
  opnum = get_next_op_number();
10677
0
  zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
10678
10679
0
  zend_compile_expr(&default_node, default_ast);
10680
10681
0
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
10682
0
  SET_NODE(opline->result, result);
10683
10684
0
  opline = &CG(active_op_array)->opcodes[opnum];
10685
0
  opline->op2.opline_num = get_next_op_number();
10686
0
}
10687
/* }}} */
10688
10689
0
static void znode_dtor(zval *zv) {
10690
0
  znode *node = Z_PTR_P(zv);
10691
0
  if (node->op_type == IS_CONST) {
10692
0
    zval_ptr_dtor_nogc(&node->u.constant);
10693
0
  }
10694
0
  efree(node);
10695
0
}
10696
10697
static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */
10698
0
{
10699
0
  zend_ast *var_ast = ast->child[0];
10700
0
  zend_ast *default_ast = ast->child[1];
10701
10702
0
  znode var_node_is, var_node_w, default_node, assign_node, *node;
10703
0
  zend_op *opline;
10704
0
  uint32_t coalesce_opnum;
10705
0
  bool need_frees = false;
10706
10707
  /* Remember expressions compiled during the initial BP_VAR_IS lookup,
10708
   * to avoid double-evaluation when we compile again with BP_VAR_W. */
10709
0
  HashTable *orig_memoized_exprs = CG(memoized_exprs);
10710
0
  const zend_memoize_mode orig_memoize_mode = CG(memoize_mode);
10711
10712
0
  zend_ensure_writable_variable(var_ast);
10713
0
  if (is_this_fetch(var_ast)) {
10714
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
10715
0
  }
10716
10717
0
  ALLOC_HASHTABLE(CG(memoized_exprs));
10718
0
  zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0);
10719
10720
0
  CG(memoize_mode) = ZEND_MEMOIZE_COMPILE;
10721
0
  zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false);
10722
10723
0
  coalesce_opnum = get_next_op_number();
10724
0
  zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL);
10725
10726
0
  CG(memoize_mode) = ZEND_MEMOIZE_NONE;
10727
0
  if (var_ast->kind == ZEND_AST_DIM) {
10728
0
    zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast);
10729
0
  } else {
10730
0
    zend_compile_expr(&default_node, default_ast);
10731
0
  }
10732
10733
0
  CG(memoize_mode) = ZEND_MEMOIZE_FETCH;
10734
0
  zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false);
10735
10736
  /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */
10737
0
  opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
10738
  /* Treat $GLOBALS['x'] assignment like assignment to variable. */
10739
0
  zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind;
10740
0
  switch (kind) {
10741
0
    case ZEND_AST_VAR:
10742
0
      zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node);
10743
0
      break;
10744
0
    case ZEND_AST_STATIC_PROP:
10745
0
      opline->opcode = ZEND_ASSIGN_STATIC_PROP;
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_DIM:
10752
0
      opline->opcode = ZEND_ASSIGN_DIM;
10753
0
      opline->result_type = IS_TMP_VAR;
10754
0
      var_node_w.op_type = IS_TMP_VAR;
10755
0
      zend_emit_op_data(&default_node);
10756
0
      assign_node = var_node_w;
10757
0
      break;
10758
0
    case ZEND_AST_PROP:
10759
0
    case ZEND_AST_NULLSAFE_PROP:
10760
0
      opline->opcode = ZEND_ASSIGN_OBJ;
10761
0
      opline->result_type = IS_TMP_VAR;
10762
0
      var_node_w.op_type = IS_TMP_VAR;
10763
0
      zend_emit_op_data(&default_node);
10764
0
      assign_node = var_node_w;
10765
0
      break;
10766
0
    EMPTY_SWITCH_DEFAULT_CASE();
10767
0
  }
10768
10769
0
  opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL);
10770
0
  SET_NODE(opline->result, result);
10771
10772
0
  ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10773
0
    if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10774
0
      need_frees = true;
10775
0
      break;
10776
0
    }
10777
0
  } ZEND_HASH_FOREACH_END();
10778
10779
  /* Free DUPed expressions if there are any */
10780
0
  if (need_frees) {
10781
0
    uint32_t jump_opnum = zend_emit_jump(0);
10782
0
    zend_update_jump_target_to_next(coalesce_opnum);
10783
0
    ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) {
10784
0
      if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) {
10785
0
        zend_emit_op(NULL, ZEND_FREE, node, NULL);
10786
0
      }
10787
0
    } ZEND_HASH_FOREACH_END();
10788
0
    zend_update_jump_target_to_next(jump_opnum);
10789
0
  } else {
10790
0
    zend_update_jump_target_to_next(coalesce_opnum);
10791
0
  }
10792
10793
0
  zend_hash_destroy(CG(memoized_exprs));
10794
0
  FREE_HASHTABLE(CG(memoized_exprs));
10795
0
  CG(memoized_exprs) = orig_memoized_exprs;
10796
0
  CG(memoize_mode) = orig_memoize_mode;
10797
0
}
10798
/* }}} */
10799
10800
static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */
10801
0
{
10802
0
  zend_op *opline;
10803
0
  zend_ast *expr_ast = ast->child[0];
10804
10805
0
  znode expr_node;
10806
0
  zend_compile_expr(&expr_node, expr_ast);
10807
10808
0
  opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
10809
0
  opline->extended_value = 1;
10810
10811
0
  result->op_type = IS_CONST;
10812
0
  ZVAL_LONG(&result->u.constant, 1);
10813
0
}
10814
/* }}} */
10815
10816
static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
10817
0
{
10818
0
  zend_ast *value_ast = ast->child[0];
10819
0
  zend_ast *key_ast = ast->child[1];
10820
10821
0
  znode value_node, key_node;
10822
0
  znode *value_node_ptr = NULL, *key_node_ptr = NULL;
10823
0
  zend_op *opline;
10824
0
  bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
10825
10826
0
  zend_mark_function_as_generator();
10827
10828
0
  if (key_ast) {
10829
0
    zend_compile_expr(&key_node, key_ast);
10830
0
    key_node_ptr = &key_node;
10831
0
  }
10832
10833
0
  if (value_ast) {
10834
0
    if (returns_by_ref && zend_is_variable(value_ast)) {
10835
0
      zend_assert_not_short_circuited(value_ast);
10836
0
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
10837
0
    } else {
10838
0
      zend_compile_expr(&value_node, value_ast);
10839
0
    }
10840
0
    value_node_ptr = &value_node;
10841
0
  }
10842
10843
0
  opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
10844
10845
0
  if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
10846
0
    opline->extended_value = ZEND_RETURNS_FUNCTION;
10847
0
  }
10848
0
}
10849
/* }}} */
10850
10851
static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */
10852
0
{
10853
0
  zend_ast *expr_ast = ast->child[0];
10854
0
  znode expr_node;
10855
10856
0
  zend_mark_function_as_generator();
10857
10858
0
  if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
10859
0
    zend_error_noreturn(E_COMPILE_ERROR,
10860
0
      "Cannot use \"yield from\" inside a by-reference generator");
10861
0
  }
10862
10863
0
  zend_compile_expr(&expr_node, expr_ast);
10864
0
  zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
10865
0
}
10866
/* }}} */
10867
10868
static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
10869
0
{
10870
0
  zend_ast *obj_ast = ast->child[0];
10871
0
  zend_ast *class_ast = ast->child[1];
10872
10873
0
  znode obj_node, class_node;
10874
0
  zend_op *opline;
10875
10876
0
  zend_compile_expr(&obj_node, obj_ast);
10877
0
  if (obj_node.op_type == IS_CONST) {
10878
0
    zend_do_free(&obj_node);
10879
0
    result->op_type = IS_CONST;
10880
0
    ZVAL_FALSE(&result->u.constant);
10881
0
    return;
10882
0
  }
10883
10884
0
  zend_compile_class_ref(&class_node, class_ast,
10885
0
    ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT);
10886
10887
0
  opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
10888
10889
0
  if (class_node.op_type == IS_CONST) {
10890
0
    opline->op2_type = IS_CONST;
10891
0
    opline->op2.constant = zend_add_class_name_literal(
10892
0
      Z_STR(class_node.u.constant));
10893
0
    opline->extended_value = zend_alloc_cache_slot();
10894
0
  } else {
10895
0
    SET_NODE(opline->op2, &class_node);
10896
0
  }
10897
0
}
10898
/* }}} */
10899
10900
static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */
10901
0
{
10902
0
  zend_ast *expr_ast = ast->child[0];
10903
0
  znode expr_node;
10904
0
  zend_op *opline;
10905
10906
0
  zend_do_extended_fcall_begin();
10907
0
  zend_compile_expr(&expr_node, expr_ast);
10908
10909
0
  opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
10910
0
  opline->extended_value = ast->attr;
10911
10912
0
  zend_do_extended_fcall_end();
10913
0
}
10914
/* }}} */
10915
10916
static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */
10917
0
{
10918
0
  zend_ast *var_ast = ast->child[0];
10919
10920
0
  znode var_node;
10921
0
  zend_op *opline = NULL;
10922
10923
0
  ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
10924
10925
0
  if (!zend_is_variable(var_ast)) {
10926
0
    if (ast->kind == ZEND_AST_EMPTY) {
10927
      /* empty(expr) can be transformed to !expr */
10928
0
      zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
10929
0
      zend_compile_expr(result, not_ast);
10930
0
      return;
10931
0
    } else {
10932
0
      zend_error_noreturn(E_COMPILE_ERROR,
10933
0
        "Cannot use isset() on the result of an expression "
10934
0
        "(you can use \"null !== expression\" instead)");
10935
0
    }
10936
0
  }
10937
10938
0
  if (is_globals_fetch(var_ast)) {
10939
0
    result->op_type = IS_CONST;
10940
0
    ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET);
10941
0
    return;
10942
0
  }
10943
10944
0
  if (is_global_var_fetch(var_ast)) {
10945
0
    if (!var_ast->child[1]) {
10946
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
10947
0
    }
10948
10949
0
    zend_compile_expr(&var_node, var_ast->child[1]);
10950
0
    if (var_node.op_type == IS_CONST) {
10951
0
      convert_to_string(&var_node.u.constant);
10952
0
    }
10953
10954
0
    opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
10955
0
    opline->extended_value =
10956
0
      ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0);
10957
0
    return;
10958
0
  }
10959
10960
0
  zend_short_circuiting_mark_inner(var_ast);
10961
0
  switch (var_ast->kind) {
10962
0
    case ZEND_AST_VAR:
10963
0
      if (is_this_fetch(var_ast)) {
10964
0
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
10965
0
        CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS;
10966
0
      } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) {
10967
0
        opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL);
10968
0
      } else {
10969
0
        opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false);
10970
0
        opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
10971
0
      }
10972
0
      break;
10973
0
    case ZEND_AST_DIM:
10974
0
      opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false);
10975
0
      opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
10976
0
      break;
10977
0
    case ZEND_AST_PROP:
10978
0
    case ZEND_AST_NULLSAFE_PROP:
10979
0
      opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false);
10980
0
      opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
10981
0
      break;
10982
0
    case ZEND_AST_STATIC_PROP:
10983
0
      opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false);
10984
0
      opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
10985
0
      break;
10986
0
    EMPTY_SWITCH_DEFAULT_CASE()
10987
0
  }
10988
10989
0
  result->op_type = opline->result_type = IS_TMP_VAR;
10990
0
  if (!(ast->kind == ZEND_AST_ISSET)) {
10991
0
    opline->extended_value |= ZEND_ISEMPTY;
10992
0
  }
10993
0
}
10994
/* }}} */
10995
10996
static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */
10997
0
{
10998
0
  zend_ast *expr_ast = ast->child[0];
10999
0
  znode silence_node;
11000
11001
0
  zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
11002
11003
0
  if (expr_ast->kind == ZEND_AST_VAR) {
11004
    /* For @$var we need to force a FETCH instruction, otherwise the CV access will
11005
     * happen outside the silenced section. */
11006
0
    zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false );
11007
0
  } else {
11008
0
    zend_compile_expr(result, expr_ast);
11009
0
  }
11010
11011
0
  zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
11012
0
}
11013
/* }}} */
11014
11015
static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */
11016
0
{
11017
0
  zend_ast *expr_ast = ast->child[0];
11018
11019
0
  zval fn_name;
11020
0
  zend_ast *name_ast, *args_ast, *call_ast;
11021
11022
0
  zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead");
11023
11024
0
  ZVAL_STRING(&fn_name, "shell_exec");
11025
0
  name_ast = zend_ast_create_zval(&fn_name);
11026
0
  args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
11027
0
  call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
11028
11029
0
  zend_compile_expr(result, call_ast);
11030
11031
0
  zval_ptr_dtor(&fn_name);
11032
0
}
11033
/* }}} */
11034
11035
static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
11036
0
{
11037
0
  zend_ast_list *list = zend_ast_get_list(ast);
11038
0
  zend_op *opline;
11039
0
  uint32_t i, opnum_init = -1;
11040
0
  bool packed = true;
11041
11042
0
  if (zend_try_ct_eval_array(&result->u.constant, ast)) {
11043
0
    result->op_type = IS_CONST;
11044
0
    return;
11045
0
  }
11046
11047
  /* Empty arrays are handled at compile-time */
11048
0
  ZEND_ASSERT(list->children > 0);
11049
11050
0
  for (i = 0; i < list->children; ++i) {
11051
0
    zend_ast *elem_ast = list->child[i];
11052
0
    zend_ast *value_ast, *key_ast;
11053
0
    bool by_ref;
11054
0
    znode value_node, key_node, *key_node_ptr = NULL;
11055
11056
0
    if (elem_ast == NULL) {
11057
0
      zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
11058
0
    }
11059
11060
0
    value_ast = elem_ast->child[0];
11061
11062
0
    if (elem_ast->kind == ZEND_AST_UNPACK) {
11063
0
      zend_compile_expr(&value_node, value_ast);
11064
0
      if (i == 0) {
11065
0
        opnum_init = get_next_op_number();
11066
0
        opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
11067
0
      }
11068
0
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL);
11069
0
      SET_NODE(opline->result, result);
11070
0
      continue;
11071
0
    }
11072
11073
0
    key_ast = elem_ast->child[1];
11074
0
    by_ref = elem_ast->attr;
11075
11076
0
    if (key_ast) {
11077
0
      zend_compile_expr(&key_node, key_ast);
11078
0
      zend_handle_numeric_op(&key_node);
11079
0
      key_node_ptr = &key_node;
11080
0
    }
11081
11082
0
    if (by_ref) {
11083
0
      zend_ensure_writable_variable(value_ast);
11084
0
      zend_compile_var(&value_node, value_ast, BP_VAR_W, true);
11085
0
    } else {
11086
0
      zend_compile_expr(&value_node, value_ast);
11087
0
    }
11088
11089
0
    if (i == 0) {
11090
0
      opnum_init = get_next_op_number();
11091
0
      opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
11092
0
      opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
11093
0
    } else {
11094
0
      opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
11095
0
        &value_node, key_node_ptr);
11096
0
      SET_NODE(opline->result, result);
11097
0
    }
11098
0
    opline->extended_value |= by_ref;
11099
11100
0
    if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
11101
0
      packed = false;
11102
0
    }
11103
0
  }
11104
11105
  /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
11106
0
  if (!packed) {
11107
0
    ZEND_ASSERT(opnum_init != (uint32_t)-1);
11108
0
    opline = &CG(active_op_array)->opcodes[opnum_init];
11109
0
    opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
11110
0
  }
11111
0
}
11112
/* }}} */
11113
11114
static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */
11115
0
{
11116
0
  zend_ast *name_ast = ast->child[0];
11117
11118
0
  zend_op *opline;
11119
11120
0
  bool is_fully_qualified;
11121
0
  zend_string *orig_name = zend_ast_get_str(name_ast);
11122
0
  zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
11123
11124
0
  if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) {
11125
0
    zend_ast *last = CG(ast);
11126
11127
0
    while (last && last->kind == ZEND_AST_STMT_LIST) {
11128
0
      const zend_ast_list *list = zend_ast_get_list(last);
11129
0
      if (list->children == 0) {
11130
0
        break;
11131
0
      }
11132
0
      last = list->child[list->children-1];
11133
0
    }
11134
0
    if (last && last->kind == ZEND_AST_HALT_COMPILER) {
11135
0
      result->op_type = IS_CONST;
11136
0
      ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
11137
0
      zend_string_release_ex(resolved_name, 0);
11138
0
      return;
11139
0
    }
11140
0
  }
11141
11142
0
  if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
11143
0
    result->op_type = IS_CONST;
11144
0
    zend_string_release_ex(resolved_name, 0);
11145
0
    return;
11146
0
  }
11147
11148
0
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
11149
0
  opline->op2_type = IS_CONST;
11150
11151
0
  if (is_fully_qualified || !FC(current_namespace)) {
11152
0
    opline->op1.num = 0;
11153
0
    opline->op2.constant = zend_add_const_name_literal(
11154
0
      resolved_name, false);
11155
0
  } else {
11156
0
    opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE;
11157
0
    opline->op2.constant = zend_add_const_name_literal(
11158
0
      resolved_name, true);
11159
0
  }
11160
0
  opline->extended_value = zend_alloc_cache_slot();
11161
0
}
11162
/* }}} */
11163
11164
static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
11165
0
{
11166
0
  zend_ast *class_ast;
11167
0
  zend_ast *const_ast;
11168
0
  znode class_node, const_node;
11169
0
  zend_op *opline;
11170
11171
0
  zend_eval_const_expr(&ast->child[0]);
11172
0
  zend_eval_const_expr(&ast->child[1]);
11173
11174
0
  class_ast = ast->child[0];
11175
0
  const_ast = ast->child[1];
11176
11177
0
  if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) {
11178
0
    zval *const_zv = zend_ast_get_zval(const_ast);
11179
0
    if (Z_TYPE_P(const_zv) == IS_STRING) {
11180
0
      zend_string *const_str = Z_STR_P(const_zv);
11181
0
      zend_string *resolved_name = zend_resolve_class_name_ast(class_ast);
11182
0
      if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) {
11183
0
        result->op_type = IS_CONST;
11184
0
        zend_string_release_ex(resolved_name, 0);
11185
0
        return;
11186
0
      }
11187
0
      zend_string_release_ex(resolved_name, 0);
11188
0
    }
11189
0
  }
11190
11191
0
  zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
11192
11193
0
  zend_compile_expr(&const_node, const_ast);
11194
11195
0
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
11196
11197
0
  zend_set_class_name_op1(opline, &class_node);
11198
11199
0
  if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) {
11200
0
    opline->extended_value = zend_alloc_cache_slots(2);
11201
0
  }
11202
0
}
11203
/* }}} */
11204
11205
static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */
11206
0
{
11207
0
  zend_ast *class_ast = ast->child[0];
11208
11209
0
  if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) {
11210
0
    result->op_type = IS_CONST;
11211
0
    return;
11212
0
  }
11213
11214
0
  if (class_ast->kind == ZEND_AST_ZVAL) {
11215
0
    zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11216
0
    opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
11217
0
  } else {
11218
0
    znode expr_node;
11219
0
    zend_compile_expr(&expr_node, class_ast);
11220
0
    if (expr_node.op_type == IS_CONST) {
11221
      /* Unlikely case that happen if class_ast is constant folded.
11222
       * Handle it here, to avoid needing a CONST specialization in the VM. */
11223
0
      zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s",
11224
0
        zend_zval_value_name(&expr_node.u.constant));
11225
0
    }
11226
11227
0
    zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL);
11228
0
  }
11229
0
}
11230
/* }}} */
11231
11232
static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */
11233
0
{
11234
0
  if (num == 0) {
11235
0
    result->op_type = IS_TMP_VAR;
11236
0
    result->u.op.var = -1;
11237
0
    opline->opcode = ZEND_ROPE_INIT;
11238
0
  } else {
11239
0
    opline->opcode = ZEND_ROPE_ADD;
11240
0
    SET_NODE(opline->op1, result);
11241
0
  }
11242
0
  SET_NODE(opline->op2, elem_node);
11243
0
  SET_NODE(opline->result, result);
11244
0
  opline->extended_value = num;
11245
0
  return opline;
11246
0
}
11247
/* }}} */
11248
11249
static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
11250
0
{
11251
0
  zend_op *opline = get_next_op();
11252
11253
0
  if (num == 0) {
11254
0
    result->op_type = IS_TMP_VAR;
11255
0
    result->u.op.var = -1;
11256
0
    opline->opcode = ZEND_ROPE_INIT;
11257
0
  } else {
11258
0
    opline->opcode = ZEND_ROPE_ADD;
11259
0
    SET_NODE(opline->op1, result);
11260
0
  }
11261
0
  SET_NODE(opline->op2, elem_node);
11262
0
  SET_NODE(opline->result, result);
11263
0
  opline->extended_value = num;
11264
0
  return opline;
11265
0
}
11266
/* }}} */
11267
11268
static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline)
11269
0
{
11270
0
  if (rope_elements == 1) {
11271
0
    if (opline->op2_type == IS_CONST) {
11272
0
      GET_NODE(result, opline->op2);
11273
0
      ZVAL_UNDEF(CT_CONSTANT(opline->op2));
11274
0
      SET_UNUSED(opline->op2);
11275
0
      MAKE_NOP(opline);
11276
0
    } else {
11277
0
      opline->opcode = ZEND_CAST;
11278
0
      opline->extended_value = IS_STRING;
11279
0
      opline->op1_type = opline->op2_type;
11280
0
      opline->op1 = opline->op2;
11281
0
      SET_UNUSED(opline->op2);
11282
0
      zend_make_tmp_result(result, opline);
11283
0
    }
11284
0
  } else if (rope_elements == 2) {
11285
0
    opline->opcode = ZEND_FAST_CONCAT;
11286
0
    opline->extended_value = 0;
11287
0
    opline->op1_type = init_opline->op2_type;
11288
0
    opline->op1 = init_opline->op2;
11289
0
    zend_make_tmp_result(result, opline);
11290
0
    MAKE_NOP(init_opline);
11291
0
  } else {
11292
0
    uint32_t var;
11293
11294
0
    init_opline->extended_value = rope_elements;
11295
0
    opline->opcode = ZEND_ROPE_END;
11296
0
    zend_make_tmp_result(result, opline);
11297
0
    var = opline->op1.var = get_temporary_variable();
11298
11299
    /* Allocates the necessary number of zval slots to keep the rope */
11300
0
    uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
11301
0
    while (i > 1) {
11302
0
      get_temporary_variable();
11303
0
      i--;
11304
0
    }
11305
11306
    /* Update all the previous opcodes to use the same variable */
11307
0
    while (opline != init_opline) {
11308
0
      opline--;
11309
0
      if (opline->opcode == ZEND_ROPE_ADD &&
11310
0
          opline->result.var == (uint32_t)-1) {
11311
0
        opline->op1.var = var;
11312
0
        opline->result.var = var;
11313
0
      } else if (opline->opcode == ZEND_ROPE_INIT &&
11314
0
                 opline->result.var == (uint32_t)-1) {
11315
0
        opline->result.var = var;
11316
0
      }
11317
0
    }
11318
0
  }
11319
0
}
11320
11321
static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
11322
0
{
11323
0
  uint32_t i, j;
11324
0
  uint32_t rope_init_lineno = -1;
11325
0
  zend_op *opline = NULL, *init_opline;
11326
0
  znode elem_node, last_const_node;
11327
0
  zend_ast_list *list = zend_ast_get_list(ast);
11328
0
  uint32_t reserved_op_number = -1;
11329
11330
0
  ZEND_ASSERT(list->children > 0);
11331
11332
0
  j = 0;
11333
0
  last_const_node.op_type = IS_UNUSED;
11334
0
  for (i = 0; i < list->children; i++) {
11335
0
    zend_ast *encaps_var = list->child[i];
11336
11337
0
    if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11338
0
      if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) {
11339
0
        zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead");
11340
0
      } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) {
11341
0
        zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead");
11342
0
      }
11343
0
    }
11344
11345
0
    zend_compile_expr(&elem_node, encaps_var);
11346
11347
0
    if (elem_node.op_type == IS_CONST) {
11348
0
      convert_to_string(&elem_node.u.constant);
11349
11350
0
      if (Z_STRLEN(elem_node.u.constant) == 0) {
11351
0
        zval_ptr_dtor(&elem_node.u.constant);
11352
0
      } else if (last_const_node.op_type == IS_CONST) {
11353
0
        concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
11354
0
        zval_ptr_dtor(&elem_node.u.constant);
11355
0
      } else {
11356
0
        last_const_node.op_type = IS_CONST;
11357
0
        ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
11358
        /* Reserve place for ZEND_ROPE_ADD instruction */
11359
0
        reserved_op_number = get_next_op_number();
11360
0
        opline = get_next_op();
11361
0
        opline->opcode = ZEND_NOP;
11362
0
      }
11363
0
      continue;
11364
0
    } else {
11365
0
      if (j == 0) {
11366
0
        if (last_const_node.op_type == IS_CONST) {
11367
0
          rope_init_lineno = reserved_op_number;
11368
0
        } else {
11369
0
          rope_init_lineno = get_next_op_number();
11370
0
        }
11371
0
      }
11372
0
      if (last_const_node.op_type == IS_CONST) {
11373
0
        opline = &CG(active_op_array)->opcodes[reserved_op_number];
11374
0
        zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11375
0
        last_const_node.op_type = IS_UNUSED;
11376
0
      }
11377
0
      opline = zend_compile_rope_add(result, j++, &elem_node);
11378
0
    }
11379
0
  }
11380
11381
0
  if (j == 0) {
11382
0
    result->op_type = IS_CONST;
11383
0
    if (last_const_node.op_type == IS_CONST) {
11384
0
      ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
11385
0
    } else {
11386
0
      ZVAL_EMPTY_STRING(&result->u.constant);
11387
      /* empty string */
11388
0
    }
11389
0
    CG(active_op_array)->last = reserved_op_number - 1;
11390
0
    return;
11391
0
  } else if (last_const_node.op_type == IS_CONST) {
11392
0
    opline = &CG(active_op_array)->opcodes[reserved_op_number];
11393
0
    opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node);
11394
0
  }
11395
0
  init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
11396
0
  zend_compile_rope_finalize(result, j, init_opline, opline);
11397
0
}
11398
/* }}} */
11399
11400
static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */
11401
0
{
11402
0
  zend_op *opline;
11403
11404
0
  if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
11405
0
    result->op_type = IS_CONST;
11406
0
    return;
11407
0
  }
11408
11409
0
  ZEND_ASSERT(ast->attr == T_CLASS_C &&
11410
0
              CG(active_class_entry) &&
11411
0
              (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
11412
11413
0
  opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
11414
0
  opline->op1.num = ZEND_FETCH_CLASS_SELF;
11415
0
}
11416
/* }}} */
11417
11418
static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
11419
0
{
11420
0
  return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
11421
0
    || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
11422
0
    || kind == ZEND_AST_AND || kind == ZEND_AST_OR
11423
0
    || kind == ZEND_AST_UNARY_OP
11424
0
    || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
11425
0
    || kind == ZEND_AST_CAST
11426
0
    || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
11427
0
    || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
11428
0
    || kind == ZEND_AST_UNPACK
11429
0
    || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
11430
0
    || kind == ZEND_AST_CLASS_NAME
11431
0
    || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE
11432
0
    || kind == ZEND_AST_CONST_ENUM_INIT
11433
0
    || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST
11434
0
    || kind == ZEND_AST_NAMED_ARG
11435
0
    || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP
11436
0
    || kind == ZEND_AST_CLOSURE
11437
0
    || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT;
11438
0
}
11439
/* }}} */
11440
11441
static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
11442
0
{
11443
0
  zend_ast *ast = *ast_ptr;
11444
0
  zend_ast *class_ast = ast->child[0];
11445
0
  zend_string *class_name;
11446
0
  int fetch_type;
11447
11448
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11449
0
    zend_error_noreturn(E_COMPILE_ERROR,
11450
0
      "Dynamic class names are not allowed in compile-time class constant references");
11451
0
  }
11452
0
  if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) {
11453
0
    zend_throw_error(NULL, "Class name must be a valid object or a string");
11454
0
  }
11455
11456
0
  class_name = zend_ast_get_str(class_ast);
11457
0
  fetch_type = zend_get_class_fetch_type(class_name);
11458
11459
0
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11460
0
    zend_error_noreturn(E_COMPILE_ERROR,
11461
0
      "\"static::\" is not allowed in compile-time constants");
11462
0
  }
11463
11464
0
  if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
11465
0
    zend_string *tmp = zend_resolve_class_name_ast(class_ast);
11466
11467
0
    zend_string_release_ex(class_name, 0);
11468
0
    if (tmp != class_name) {
11469
0
      zval *zv = zend_ast_get_zval(class_ast);
11470
0
      ZVAL_STR(zv, tmp);
11471
0
      class_ast->attr = ZEND_NAME_FQ;
11472
0
    }
11473
0
  }
11474
11475
0
  ast->attr |= ZEND_FETCH_CLASS_EXCEPTION;
11476
0
}
11477
/* }}} */
11478
11479
static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */
11480
0
{
11481
0
  zend_ast *ast = *ast_ptr;
11482
0
  zend_ast *class_ast = ast->child[0];
11483
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11484
0
    zend_error_noreturn(E_COMPILE_ERROR,
11485
0
      "(expression)::class cannot be used in constant expressions");
11486
0
  }
11487
11488
0
  zend_string *class_name = zend_ast_get_str(class_ast);
11489
0
  uint32_t fetch_type = zend_get_class_fetch_type(class_name);
11490
11491
0
  switch (fetch_type) {
11492
0
    case ZEND_FETCH_CLASS_SELF:
11493
0
    case ZEND_FETCH_CLASS_PARENT:
11494
      /* For the const-eval representation store the fetch type instead of the name. */
11495
0
      zend_string_release(class_name);
11496
0
      ast->child[0] = NULL;
11497
0
      ast->attr = fetch_type;
11498
0
      return;
11499
0
    case ZEND_FETCH_CLASS_STATIC:
11500
0
      zend_error_noreturn(E_COMPILE_ERROR,
11501
0
        "static::class cannot be used for compile-time class name resolution");
11502
0
      return;
11503
0
    EMPTY_SWITCH_DEFAULT_CASE()
11504
0
  }
11505
0
}
11506
11507
static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
11508
0
{
11509
0
  zend_ast *ast = *ast_ptr;
11510
0
  zend_ast *name_ast = ast->child[0];
11511
0
  zend_string *orig_name = zend_ast_get_str(name_ast);
11512
0
  bool is_fully_qualified;
11513
0
  zval result;
11514
0
  zend_string *resolved_name;
11515
11516
0
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11517
11518
0
  resolved_name = zend_resolve_const_name(
11519
0
    orig_name, name_ast->attr, &is_fully_qualified);
11520
11521
0
  if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
11522
0
    zend_string_release_ex(resolved_name, 0);
11523
0
    zend_ast_destroy(ast);
11524
0
    *ast_ptr = zend_ast_create_zval(&result);
11525
0
    return;
11526
0
  }
11527
11528
0
  zend_ast_destroy(ast);
11529
0
  *ast_ptr = zend_ast_create_constant(resolved_name,
11530
0
    !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0);
11531
0
}
11532
/* }}} */
11533
11534
static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
11535
0
{
11536
0
  zend_ast *ast = *ast_ptr;
11537
11538
  /* Other cases already resolved by constant folding */
11539
0
  ZEND_ASSERT(ast->attr == T_CLASS_C);
11540
11541
0
  zend_ast_destroy(ast);
11542
0
  *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS);
11543
0
}
11544
/* }}} */
11545
11546
static void zend_compile_const_expr_class_reference(zend_ast *class_ast)
11547
0
{
11548
0
  if (class_ast->kind == ZEND_AST_CLASS) {
11549
0
    zend_error_noreturn(E_COMPILE_ERROR,
11550
0
      "Cannot use anonymous class in constant expression");
11551
0
  }
11552
0
  if (class_ast->kind != ZEND_AST_ZVAL) {
11553
0
    zend_error_noreturn(E_COMPILE_ERROR,
11554
0
      "Cannot use dynamic class name in constant expression");
11555
0
  }
11556
11557
0
  zend_string *class_name = zend_resolve_class_name_ast(class_ast);
11558
0
  int fetch_type = zend_get_class_fetch_type(class_name);
11559
0
  if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
11560
0
    zend_error_noreturn(E_COMPILE_ERROR,
11561
0
      "\"static\" is not allowed in compile-time constants");
11562
0
  }
11563
11564
0
  zval *class_ast_zv = zend_ast_get_zval(class_ast);
11565
0
  zval_ptr_dtor_nogc(class_ast_zv);
11566
0
  ZVAL_STR(class_ast_zv, class_name);
11567
0
  class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT;
11568
0
}
11569
11570
static void zend_compile_const_expr_new(zend_ast **ast_ptr)
11571
0
{
11572
0
  zend_ast *class_ast = (*ast_ptr)->child[0];
11573
0
  zend_compile_const_expr_class_reference(class_ast);
11574
11575
0
  const zend_ast *args_ast = (*ast_ptr)->child[1];
11576
0
  if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) {
11577
0
    zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression");
11578
0
  }
11579
0
}
11580
11581
static void zend_compile_const_expr_closure(zend_ast **ast_ptr)
11582
0
{
11583
0
  zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr;
11584
0
  const zend_ast *uses_ast = closure_ast->child[1];
11585
0
  if (!(closure_ast->flags & ZEND_ACC_STATIC)) {
11586
0
    zend_error_noreturn(E_COMPILE_ERROR,
11587
0
      "Closures in constant expressions must be static");
11588
0
  }
11589
0
  if (uses_ast) {
11590
0
    zend_error_noreturn(E_COMPILE_ERROR,
11591
0
      "Cannot use(...) variables in constant expression");
11592
0
  }
11593
11594
0
  znode node;
11595
0
  zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR);
11596
11597
0
  zend_ast_destroy(*ast_ptr);
11598
0
  *ast_ptr = zend_ast_create_op_array(op);
11599
0
}
11600
11601
static void zend_compile_const_expr_fcc(zend_ast **ast_ptr)
11602
0
{
11603
0
  zend_ast **args_ast;
11604
0
  switch ((*ast_ptr)->kind) {
11605
0
    case ZEND_AST_CALL:
11606
0
      args_ast = &(*ast_ptr)->child[1];
11607
0
      break;
11608
0
    case ZEND_AST_STATIC_CALL:
11609
0
      args_ast = &(*ast_ptr)->child[2];
11610
0
      break;
11611
0
    EMPTY_SWITCH_DEFAULT_CASE();
11612
0
  }
11613
0
  if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) {
11614
0
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11615
0
  }
11616
0
  ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr);
11617
11618
0
  switch ((*ast_ptr)->kind) {
11619
0
    case ZEND_AST_CALL: {
11620
0
      zend_ast *name_ast = (*ast_ptr)->child[0];
11621
0
      if (name_ast->kind != ZEND_AST_ZVAL) {
11622
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression");
11623
0
      }
11624
0
      zval *name_ast_zv = zend_ast_get_zval(name_ast);
11625
0
      if (Z_TYPE_P(name_ast_zv) != IS_STRING) {
11626
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name");
11627
0
      }
11628
0
      bool is_fully_qualified;
11629
0
      zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified);
11630
0
      zval_ptr_dtor_nogc(name_ast_zv);
11631
0
      ZVAL_STR(name_ast_zv, name);
11632
0
      if (is_fully_qualified) {
11633
0
        name_ast->attr = ZEND_NAME_FQ;
11634
0
      }
11635
0
      break;
11636
0
    }
11637
0
    case ZEND_AST_STATIC_CALL: {
11638
0
      zend_ast *class_ast = (*ast_ptr)->child[0];
11639
0
      zend_compile_const_expr_class_reference(class_ast);
11640
0
      zend_ast *method_ast = (*ast_ptr)->child[1];
11641
0
      if (method_ast->kind != ZEND_AST_ZVAL) {
11642
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression");
11643
0
      }
11644
0
      if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) {
11645
0
        zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name");
11646
0
      }
11647
0
      break;
11648
0
    }
11649
0
    EMPTY_SWITCH_DEFAULT_CASE();
11650
0
  }
11651
0
}
11652
11653
static void zend_compile_const_expr_args(zend_ast **ast_ptr)
11654
0
{
11655
0
  zend_ast_list *list = zend_ast_get_list(*ast_ptr);
11656
0
  bool uses_named_args = false;
11657
0
  for (uint32_t i = 0; i < list->children; i++) {
11658
0
    const zend_ast *arg = list->child[i];
11659
0
    if (arg->kind == ZEND_AST_UNPACK) {
11660
0
      zend_error_noreturn(E_COMPILE_ERROR,
11661
0
        "Argument unpacking in constant expressions is not supported");
11662
0
    }
11663
0
    if (arg->kind == ZEND_AST_NAMED_ARG) {
11664
0
      uses_named_args = true;
11665
0
    } else if (uses_named_args) {
11666
0
      zend_error_noreturn(E_COMPILE_ERROR,
11667
0
        "Cannot use positional argument after named argument");
11668
0
    }
11669
0
  }
11670
0
  if (uses_named_args) {
11671
0
    list->attr = 1;
11672
0
  }
11673
0
}
11674
11675
typedef struct {
11676
  /* Whether the value of this expression may differ on each evaluation. */
11677
  bool allow_dynamic;
11678
} const_expr_context;
11679
11680
static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
11681
0
{
11682
0
  const const_expr_context *ctx = context;
11683
0
  zend_ast *ast = *ast_ptr;
11684
0
  if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
11685
0
    return;
11686
0
  }
11687
11688
0
  if (!zend_is_allowed_in_const_expr(ast->kind)) {
11689
0
    zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
11690
0
  }
11691
11692
0
  switch (ast->kind) {
11693
0
    case ZEND_AST_CLASS_CONST:
11694
0
      zend_compile_const_expr_class_const(ast_ptr);
11695
0
      break;
11696
0
    case ZEND_AST_CLASS_NAME:
11697
0
      zend_compile_const_expr_class_name(ast_ptr);
11698
0
      break;
11699
0
    case ZEND_AST_CONST:
11700
0
      zend_compile_const_expr_const(ast_ptr);
11701
0
      break;
11702
0
    case ZEND_AST_MAGIC_CONST:
11703
0
      zend_compile_const_expr_magic_const(ast_ptr);
11704
0
      break;
11705
0
    case ZEND_AST_CAST:
11706
0
      if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11707
0
        zend_error_noreturn(E_COMPILE_ERROR,
11708
0
          "Object casts are not supported in this context");
11709
0
      }
11710
0
      break;
11711
0
    case ZEND_AST_NEW:
11712
0
      if (!ctx->allow_dynamic) {
11713
0
        zend_error_noreturn(E_COMPILE_ERROR,
11714
0
          "New expressions are not supported in this context");
11715
0
      }
11716
0
      zend_compile_const_expr_new(ast_ptr);
11717
0
      break;
11718
0
    case ZEND_AST_ARG_LIST:
11719
0
      zend_compile_const_expr_args(ast_ptr);
11720
0
      break;
11721
0
    case ZEND_AST_CLOSURE:
11722
0
      zend_compile_const_expr_closure(ast_ptr);
11723
      /* Return, because we do not want to traverse the children. */
11724
0
      return;
11725
0
    case ZEND_AST_CALL:
11726
0
    case ZEND_AST_STATIC_CALL:
11727
0
      zend_compile_const_expr_fcc(ast_ptr);
11728
0
      break;
11729
0
  }
11730
11731
0
  zend_ast_apply(ast, zend_compile_const_expr, context);
11732
0
}
11733
/* }}} */
11734
11735
void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */
11736
0
{
11737
0
  const_expr_context context;
11738
0
  context.allow_dynamic = allow_dynamic;
11739
11740
0
  zend_eval_const_expr(ast_ptr);
11741
0
  zend_compile_const_expr(ast_ptr, &context);
11742
0
  if ((*ast_ptr)->kind != ZEND_AST_ZVAL) {
11743
    /* Replace with compiled AST zval representation. */
11744
0
    zval ast_zv;
11745
0
    ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr));
11746
0
    zend_ast_destroy(*ast_ptr);
11747
0
    *ast_ptr = zend_ast_create_zval(&ast_zv);
11748
0
  }
11749
0
  ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr));
11750
0
}
11751
/* }}} */
11752
11753
/* Same as compile_stmt, but with early binding */
11754
void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
11755
0
{
11756
0
  if (!ast) {
11757
0
    return;
11758
0
  }
11759
11760
0
  if (ast->kind == ZEND_AST_STMT_LIST) {
11761
0
    const zend_ast_list *list = zend_ast_get_list(ast);
11762
0
    uint32_t i;
11763
0
    for (i = 0; i < list->children; ++i) {
11764
0
      zend_compile_top_stmt(list->child[i]);
11765
0
    }
11766
0
    return;
11767
0
  }
11768
11769
0
  if (ast->kind == ZEND_AST_FUNC_DECL) {
11770
0
    CG(zend_lineno) = ast->lineno;
11771
0
    zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL);
11772
0
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11773
0
  } else if (ast->kind == ZEND_AST_CLASS) {
11774
0
    CG(zend_lineno) = ast->lineno;
11775
0
    zend_compile_class_decl(NULL, ast, true);
11776
0
    CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
11777
0
  } else {
11778
0
    zend_compile_stmt(ast);
11779
0
  }
11780
0
  if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
11781
0
    zend_verify_namespace();
11782
0
  }
11783
0
}
11784
/* }}} */
11785
11786
static void zend_compile_stmt(zend_ast *ast) /* {{{ */
11787
0
{
11788
0
  if (!ast) {
11789
0
    return;
11790
0
  }
11791
11792
0
  CG(zend_lineno) = ast->lineno;
11793
11794
0
  if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) {
11795
0
    zend_do_extended_stmt(NULL);
11796
0
  }
11797
11798
0
  switch (ast->kind) {
11799
0
    case ZEND_AST_STMT_LIST:
11800
0
      zend_compile_stmt_list(ast);
11801
0
      break;
11802
0
    case ZEND_AST_GLOBAL:
11803
0
      zend_compile_global_var(ast);
11804
0
      break;
11805
0
    case ZEND_AST_STATIC:
11806
0
      zend_compile_static_var(ast);
11807
0
      break;
11808
0
    case ZEND_AST_UNSET:
11809
0
      zend_compile_unset(ast);
11810
0
      break;
11811
0
    case ZEND_AST_RETURN:
11812
0
      zend_compile_return(ast);
11813
0
      break;
11814
0
    case ZEND_AST_ECHO:
11815
0
      zend_compile_echo(ast);
11816
0
      break;
11817
0
    case ZEND_AST_BREAK:
11818
0
    case ZEND_AST_CONTINUE:
11819
0
      zend_compile_break_continue(ast);
11820
0
      break;
11821
0
    case ZEND_AST_GOTO:
11822
0
      zend_compile_goto(ast);
11823
0
      break;
11824
0
    case ZEND_AST_LABEL:
11825
0
      zend_compile_label(ast);
11826
0
      break;
11827
0
    case ZEND_AST_WHILE:
11828
0
      zend_compile_while(ast);
11829
0
      break;
11830
0
    case ZEND_AST_DO_WHILE:
11831
0
      zend_compile_do_while(ast);
11832
0
      break;
11833
0
    case ZEND_AST_FOR:
11834
0
      zend_compile_for(ast);
11835
0
      break;
11836
0
    case ZEND_AST_FOREACH:
11837
0
      zend_compile_foreach(ast);
11838
0
      break;
11839
0
    case ZEND_AST_IF:
11840
0
      zend_compile_if(ast);
11841
0
      break;
11842
0
    case ZEND_AST_SWITCH:
11843
0
      zend_compile_switch(ast);
11844
0
      break;
11845
0
    case ZEND_AST_TRY:
11846
0
      zend_compile_try(ast);
11847
0
      break;
11848
0
    case ZEND_AST_DECLARE:
11849
0
      zend_compile_declare(ast);
11850
0
      break;
11851
0
    case ZEND_AST_FUNC_DECL:
11852
0
    case ZEND_AST_METHOD:
11853
0
      zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED);
11854
0
      break;
11855
0
    case ZEND_AST_ENUM_CASE:
11856
0
      zend_compile_enum_case(ast);
11857
0
      break;
11858
0
    case ZEND_AST_PROP_GROUP:
11859
0
      zend_compile_prop_group(ast);
11860
0
      break;
11861
0
    case ZEND_AST_CLASS_CONST_GROUP:
11862
0
      zend_compile_class_const_group(ast);
11863
0
      break;
11864
0
    case ZEND_AST_USE_TRAIT:
11865
0
      zend_compile_use_trait(ast);
11866
0
      break;
11867
0
    case ZEND_AST_CLASS:
11868
0
      zend_compile_class_decl(NULL, ast, false);
11869
0
      break;
11870
0
    case ZEND_AST_GROUP_USE:
11871
0
      zend_compile_group_use(ast);
11872
0
      break;
11873
0
    case ZEND_AST_USE:
11874
0
      zend_compile_use(ast);
11875
0
      break;
11876
0
    case ZEND_AST_CONST_DECL:
11877
0
      zend_compile_const_decl(ast);
11878
0
      break;
11879
0
    case ZEND_AST_NAMESPACE:
11880
0
      zend_compile_namespace(ast);
11881
0
      break;
11882
0
    case ZEND_AST_HALT_COMPILER:
11883
0
      zend_compile_halt_compiler(ast);
11884
0
      break;
11885
0
    case ZEND_AST_THROW:
11886
0
      zend_compile_expr(NULL, ast);
11887
0
      break;
11888
0
    case ZEND_AST_CAST_VOID:
11889
0
      zend_compile_void_cast(NULL, ast);
11890
0
      break;
11891
0
    default:
11892
0
    {
11893
0
      znode result;
11894
0
      zend_compile_expr(&result, ast);
11895
0
      zend_do_free(&result);
11896
0
    }
11897
0
  }
11898
11899
0
  if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
11900
0
    zend_emit_tick();
11901
0
  }
11902
0
}
11903
/* }}} */
11904
11905
static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */
11906
0
{
11907
  /* CG(zend_lineno) = ast->lineno; */
11908
0
  CG(zend_lineno) = zend_ast_get_lineno(ast);
11909
11910
0
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
11911
0
    zend_compile_memoized_expr(result, ast);
11912
0
    return;
11913
0
  }
11914
11915
0
  switch (ast->kind) {
11916
0
    case ZEND_AST_ZVAL:
11917
0
      ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
11918
0
      result->op_type = IS_CONST;
11919
0
      return;
11920
0
    case ZEND_AST_ZNODE:
11921
0
      *result = *zend_ast_get_znode(ast);
11922
0
      return;
11923
0
    case ZEND_AST_VAR:
11924
0
    case ZEND_AST_DIM:
11925
0
    case ZEND_AST_PROP:
11926
0
    case ZEND_AST_NULLSAFE_PROP:
11927
0
    case ZEND_AST_STATIC_PROP:
11928
0
    case ZEND_AST_CALL:
11929
0
    case ZEND_AST_METHOD_CALL:
11930
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
11931
0
    case ZEND_AST_STATIC_CALL:
11932
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
11933
0
      zend_compile_var(result, ast, BP_VAR_R, false);
11934
0
      return;
11935
0
    case ZEND_AST_ASSIGN:
11936
0
      zend_compile_assign(result, ast);
11937
0
      return;
11938
0
    case ZEND_AST_ASSIGN_REF:
11939
0
      zend_compile_assign_ref(result, ast);
11940
0
      return;
11941
0
    case ZEND_AST_NEW:
11942
0
      zend_compile_new(result, ast);
11943
0
      return;
11944
0
    case ZEND_AST_ASSIGN_OP:
11945
0
      zend_compile_compound_assign(result, ast);
11946
0
      return;
11947
0
    case ZEND_AST_BINARY_OP:
11948
0
      zend_compile_binary_op(result, ast);
11949
0
      return;
11950
0
    case ZEND_AST_GREATER:
11951
0
    case ZEND_AST_GREATER_EQUAL:
11952
0
      zend_compile_greater(result, ast);
11953
0
      return;
11954
0
    case ZEND_AST_UNARY_OP:
11955
0
      zend_compile_unary_op(result, ast);
11956
0
      return;
11957
0
    case ZEND_AST_UNARY_PLUS:
11958
0
    case ZEND_AST_UNARY_MINUS:
11959
0
      zend_compile_unary_pm(result, ast);
11960
0
      return;
11961
0
    case ZEND_AST_AND:
11962
0
    case ZEND_AST_OR:
11963
0
      zend_compile_short_circuiting(result, ast);
11964
0
      return;
11965
0
    case ZEND_AST_POST_INC:
11966
0
    case ZEND_AST_POST_DEC:
11967
0
      zend_compile_post_incdec(result, ast);
11968
0
      return;
11969
0
    case ZEND_AST_PRE_INC:
11970
0
    case ZEND_AST_PRE_DEC:
11971
0
      zend_compile_pre_incdec(result, ast);
11972
0
      return;
11973
0
    case ZEND_AST_CAST:
11974
0
      zend_compile_cast(result, ast);
11975
0
      return;
11976
0
    case ZEND_AST_CONDITIONAL:
11977
0
      zend_compile_conditional(result, ast);
11978
0
      return;
11979
0
    case ZEND_AST_COALESCE:
11980
0
      zend_compile_coalesce(result, ast);
11981
0
      return;
11982
0
    case ZEND_AST_ASSIGN_COALESCE:
11983
0
      zend_compile_assign_coalesce(result, ast);
11984
0
      return;
11985
0
    case ZEND_AST_PRINT:
11986
0
      zend_compile_print(result, ast);
11987
0
      return;
11988
0
    case ZEND_AST_YIELD:
11989
0
      zend_compile_yield(result, ast);
11990
0
      return;
11991
0
    case ZEND_AST_YIELD_FROM:
11992
0
      zend_compile_yield_from(result, ast);
11993
0
      return;
11994
0
    case ZEND_AST_INSTANCEOF:
11995
0
      zend_compile_instanceof(result, ast);
11996
0
      return;
11997
0
    case ZEND_AST_INCLUDE_OR_EVAL:
11998
0
      zend_compile_include_or_eval(result, ast);
11999
0
      return;
12000
0
    case ZEND_AST_ISSET:
12001
0
    case ZEND_AST_EMPTY:
12002
0
      zend_compile_isset_or_empty(result, ast);
12003
0
      return;
12004
0
    case ZEND_AST_SILENCE:
12005
0
      zend_compile_silence(result, ast);
12006
0
      return;
12007
0
    case ZEND_AST_SHELL_EXEC:
12008
0
      zend_compile_shell_exec(result, ast);
12009
0
      return;
12010
0
    case ZEND_AST_ARRAY:
12011
0
      zend_compile_array(result, ast);
12012
0
      return;
12013
0
    case ZEND_AST_CONST:
12014
0
      zend_compile_const(result, ast);
12015
0
      return;
12016
0
    case ZEND_AST_CLASS_CONST:
12017
0
      zend_compile_class_const(result, ast);
12018
0
      return;
12019
0
    case ZEND_AST_CLASS_NAME:
12020
0
      zend_compile_class_name(result, ast);
12021
0
      return;
12022
0
    case ZEND_AST_ENCAPS_LIST:
12023
0
      zend_compile_encaps_list(result, ast);
12024
0
      return;
12025
0
    case ZEND_AST_MAGIC_CONST:
12026
0
      zend_compile_magic_const(result, ast);
12027
0
      return;
12028
0
    case ZEND_AST_CLOSURE:
12029
0
    case ZEND_AST_ARROW_FUNC:
12030
0
      zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED);
12031
0
      return;
12032
0
    case ZEND_AST_THROW:
12033
0
      zend_compile_throw(result, ast);
12034
0
      return;
12035
0
    case ZEND_AST_MATCH:
12036
0
      zend_compile_match(result, ast);
12037
0
      return;
12038
0
    case ZEND_AST_PIPE:
12039
0
      zend_compile_pipe(result, ast);
12040
0
      return;
12041
0
    default:
12042
0
      ZEND_ASSERT(0 /* not supported */);
12043
0
  }
12044
0
}
12045
/* }}} */
12046
12047
static void zend_compile_expr(znode *result, zend_ast *ast)
12048
0
{
12049
0
  zend_check_stack_limit();
12050
12051
0
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12052
0
  zend_compile_expr_inner(result, ast);
12053
0
  zend_short_circuiting_commit(checkpoint, result, ast);
12054
0
}
12055
12056
static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref)
12057
0
{
12058
0
  CG(zend_lineno) = zend_ast_get_lineno(ast);
12059
12060
0
  if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) {
12061
0
    switch (ast->kind) {
12062
0
      case ZEND_AST_CALL:
12063
0
      case ZEND_AST_METHOD_CALL:
12064
0
      case ZEND_AST_NULLSAFE_METHOD_CALL:
12065
0
      case ZEND_AST_STATIC_CALL:
12066
0
        zend_compile_memoized_expr(result, ast);
12067
        /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */
12068
0
        return NULL;
12069
0
    }
12070
0
  }
12071
12072
0
  switch (ast->kind) {
12073
0
    case ZEND_AST_VAR:
12074
0
      return zend_compile_simple_var(result, ast, type, false);
12075
0
    case ZEND_AST_DIM:
12076
0
      return zend_compile_dim(result, ast, type, by_ref);
12077
0
    case ZEND_AST_PROP:
12078
0
    case ZEND_AST_NULLSAFE_PROP:
12079
0
      return zend_compile_prop(result, ast, type, by_ref);
12080
0
    case ZEND_AST_STATIC_PROP:
12081
0
      return zend_compile_static_prop(result, ast, type, by_ref, false);
12082
0
    case ZEND_AST_CALL:
12083
0
      zend_compile_call(result, ast, type);
12084
0
      return NULL;
12085
0
    case ZEND_AST_PARENT_PROPERTY_HOOK_CALL:
12086
0
      zend_compile_parent_property_hook_call(result, ast, type);
12087
0
      return NULL;
12088
0
    case ZEND_AST_METHOD_CALL:
12089
0
    case ZEND_AST_NULLSAFE_METHOD_CALL:
12090
0
      zend_compile_method_call(result, ast, type);
12091
0
      return NULL;
12092
0
    case ZEND_AST_STATIC_CALL:
12093
0
      zend_compile_static_call(result, ast, type);
12094
0
      return NULL;
12095
0
    case ZEND_AST_ZNODE:
12096
0
      *result = *zend_ast_get_znode(ast);
12097
0
      return NULL;
12098
0
    default:
12099
0
      if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
12100
0
        zend_error_noreturn(E_COMPILE_ERROR,
12101
0
          "Cannot use temporary expression in write context");
12102
0
      }
12103
12104
0
      zend_compile_expr(result, ast);
12105
0
      return NULL;
12106
0
  }
12107
0
}
12108
12109
static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12110
0
{
12111
0
  zend_check_stack_limit();
12112
12113
0
  uint32_t checkpoint = zend_short_circuiting_checkpoint();
12114
0
  zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref);
12115
0
  zend_short_circuiting_commit(checkpoint, result, ast);
12116
0
  return opcode;
12117
0
}
12118
12119
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */
12120
0
{
12121
0
  zend_check_stack_limit();
12122
12123
0
  switch (ast->kind) {
12124
0
    case ZEND_AST_VAR:
12125
0
      return zend_compile_simple_var(result, ast, type, true);
12126
0
    case ZEND_AST_DIM:
12127
0
      return zend_delayed_compile_dim(result, ast, type, by_ref);
12128
0
    case ZEND_AST_PROP:
12129
0
    case ZEND_AST_NULLSAFE_PROP:
12130
0
    {
12131
0
      zend_op *opline = zend_delayed_compile_prop(result, ast, type);
12132
0
      if (by_ref) {
12133
0
        opline->extended_value |= ZEND_FETCH_REF;
12134
0
      }
12135
0
      return opline;
12136
0
    }
12137
0
    case ZEND_AST_STATIC_PROP:
12138
0
      return zend_compile_static_prop(result, ast, type, by_ref, true);
12139
0
    default:
12140
0
      return zend_compile_var(result, ast, type, false);
12141
0
  }
12142
0
}
12143
/* }}} */
12144
12145
bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1)
12146
0
{
12147
  /* NAN warns when casting */
12148
0
  if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) {
12149
0
    return false;
12150
0
  }
12151
0
  switch (type) {
12152
0
    case _IS_BOOL:
12153
0
      ZVAL_BOOL(result, zend_is_true(op1));
12154
0
      return true;
12155
0
    case IS_LONG:
12156
0
      if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) {
12157
0
        return false;
12158
0
      }
12159
0
      ZVAL_LONG(result, zval_get_long(op1));
12160
0
      return true;
12161
0
    case IS_DOUBLE:
12162
0
      ZVAL_DOUBLE(result, zval_get_double(op1));
12163
0
      return true;
12164
0
    case IS_STRING:
12165
      /* Conversion from double to string takes into account run-time
12166
         'precision' setting and cannot be evaluated at compile-time */
12167
0
      if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) {
12168
0
        ZVAL_STR(result, zval_get_string(op1));
12169
0
        return true;
12170
0
      }
12171
0
      break;
12172
0
    case IS_ARRAY:
12173
0
      ZVAL_COPY(result, op1);
12174
0
      convert_to_array(result);
12175
0
      return true;
12176
0
  }
12177
0
  return false;
12178
0
}
12179
12180
static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
12181
0
{
12182
0
  zend_ast *ast = *ast_ptr;
12183
0
  zval result;
12184
12185
0
  if (!ast) {
12186
0
    return;
12187
0
  }
12188
12189
0
  zend_check_stack_limit();
12190
12191
0
  switch (ast->kind) {
12192
0
    case ZEND_AST_BINARY_OP:
12193
0
      zend_eval_const_expr(&ast->child[0]);
12194
0
      zend_eval_const_expr(&ast->child[1]);
12195
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12196
0
        return;
12197
0
      }
12198
12199
0
      if (!zend_try_ct_eval_binary_op(&result, ast->attr,
12200
0
          zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
12201
0
      ) {
12202
0
        return;
12203
0
      }
12204
0
      break;
12205
0
    case ZEND_AST_GREATER:
12206
0
    case ZEND_AST_GREATER_EQUAL:
12207
0
      zend_eval_const_expr(&ast->child[0]);
12208
0
      zend_eval_const_expr(&ast->child[1]);
12209
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12210
0
        return;
12211
0
      }
12212
12213
0
      zend_ct_eval_greater(&result, ast->kind,
12214
0
        zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
12215
0
      break;
12216
0
    case ZEND_AST_AND:
12217
0
    case ZEND_AST_OR:
12218
0
    {
12219
0
      bool child0_is_true, child1_is_true;
12220
0
      zend_eval_const_expr(&ast->child[0]);
12221
0
      zend_eval_const_expr(&ast->child[1]);
12222
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12223
0
        return;
12224
0
      }
12225
12226
0
      child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0]));
12227
0
      if (child0_is_true == (ast->kind == ZEND_AST_OR)) {
12228
0
        ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
12229
0
        break;
12230
0
      }
12231
12232
0
      if (ast->child[1]->kind != ZEND_AST_ZVAL) {
12233
0
        return;
12234
0
      }
12235
12236
0
      child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1]));
12237
0
      if (ast->kind == ZEND_AST_OR) {
12238
0
        ZVAL_BOOL(&result, child0_is_true || child1_is_true);
12239
0
      } else {
12240
0
        ZVAL_BOOL(&result, child0_is_true && child1_is_true);
12241
0
      }
12242
0
      break;
12243
0
    }
12244
0
    case ZEND_AST_UNARY_OP:
12245
0
      zend_eval_const_expr(&ast->child[0]);
12246
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12247
0
        return;
12248
0
      }
12249
12250
0
      if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12251
0
        return;
12252
0
      }
12253
0
      break;
12254
0
    case ZEND_AST_UNARY_PLUS:
12255
0
    case ZEND_AST_UNARY_MINUS:
12256
0
      zend_eval_const_expr(&ast->child[0]);
12257
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12258
0
        return;
12259
0
      }
12260
12261
0
      if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
12262
0
        return;
12263
0
      }
12264
0
      break;
12265
0
    case ZEND_AST_COALESCE:
12266
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12267
0
      if (ast->child[0]->kind == ZEND_AST_DIM) {
12268
0
        ast->child[0]->attr |= ZEND_DIM_IS;
12269
0
      }
12270
0
      zend_eval_const_expr(&ast->child[0]);
12271
12272
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12273
        /* ensure everything was compile-time evaluated at least once */
12274
0
        zend_eval_const_expr(&ast->child[1]);
12275
0
        return;
12276
0
      }
12277
12278
0
      if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
12279
0
        zend_eval_const_expr(&ast->child[1]);
12280
0
        *ast_ptr = ast->child[1];
12281
0
        ast->child[1] = NULL;
12282
0
        zend_ast_destroy(ast);
12283
0
      } else {
12284
0
        *ast_ptr = ast->child[0];
12285
0
        ast->child[0] = NULL;
12286
0
        zend_ast_destroy(ast);
12287
0
      }
12288
0
      return;
12289
0
    case ZEND_AST_CONDITIONAL:
12290
0
    {
12291
0
      zend_ast **child, *child_ast;
12292
0
      zend_eval_const_expr(&ast->child[0]);
12293
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL) {
12294
        /* ensure everything was compile-time evaluated at least once */
12295
0
        if (ast->child[1]) {
12296
0
          zend_eval_const_expr(&ast->child[1]);
12297
0
        }
12298
0
        zend_eval_const_expr(&ast->child[2]);
12299
0
        return;
12300
0
      }
12301
12302
0
      child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
12303
0
      if (*child == NULL) {
12304
0
        child--;
12305
0
      }
12306
0
      child_ast = *child;
12307
0
      *child = NULL;
12308
0
      zend_ast_destroy(ast);
12309
0
      *ast_ptr = child_ast;
12310
0
      zend_eval_const_expr(ast_ptr);
12311
0
      return;
12312
0
    }
12313
0
    case ZEND_AST_DIM:
12314
0
    {
12315
      /* constant expression should be always read context ... */
12316
0
      const zval *container, *dim;
12317
12318
0
      if (ast->child[1] == NULL) {
12319
0
        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
12320
0
      }
12321
12322
      /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
12323
0
      if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) {
12324
0
        ast->child[0]->attr |= ZEND_DIM_IS;
12325
0
      }
12326
12327
0
      zend_eval_const_expr(&ast->child[0]);
12328
0
      zend_eval_const_expr(&ast->child[1]);
12329
0
      if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
12330
0
        return;
12331
0
      }
12332
12333
0
      container = zend_ast_get_zval(ast->child[0]);
12334
0
      dim = zend_ast_get_zval(ast->child[1]);
12335
12336
0
      if (Z_TYPE_P(container) == IS_ARRAY) {
12337
0
        zval *el;
12338
0
        if (Z_TYPE_P(dim) == IS_LONG) {
12339
0
          el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
12340
0
          if (el) {
12341
0
            ZVAL_COPY(&result, el);
12342
0
          } else {
12343
0
            return;
12344
0
          }
12345
0
        } else if (Z_TYPE_P(dim) == IS_STRING) {
12346
0
          el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
12347
0
          if (el) {
12348
0
            ZVAL_COPY(&result, el);
12349
0
          } else {
12350
0
            return;
12351
0
          }
12352
0
        } else {
12353
0
          return; /* warning... handle at runtime */
12354
0
        }
12355
0
      } else if (Z_TYPE_P(container) == IS_STRING) {
12356
0
        zend_long offset;
12357
0
        uint8_t c;
12358
0
        if (Z_TYPE_P(dim) == IS_LONG) {
12359
0
          offset = Z_LVAL_P(dim);
12360
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) {
12361
0
          return;
12362
0
        }
12363
0
        if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12364
0
          return;
12365
0
        }
12366
0
        c = (uint8_t) Z_STRVAL_P(container)[offset];
12367
0
        ZVAL_CHAR(&result, c);
12368
0
      } else if (Z_TYPE_P(container) <= IS_FALSE) {
12369
0
        return; /* warning... handle at runtime */
12370
0
      } else {
12371
0
        return;
12372
0
      }
12373
0
      break;
12374
0
    }
12375
0
    case ZEND_AST_ARRAY:
12376
0
      if (!zend_try_ct_eval_array(&result, ast)) {
12377
0
        return;
12378
0
      }
12379
0
      break;
12380
0
    case ZEND_AST_MAGIC_CONST:
12381
0
      if (!zend_try_ct_eval_magic_const(&result, ast)) {
12382
0
        return;
12383
0
      }
12384
0
      break;
12385
0
    case ZEND_AST_CONST:
12386
0
    {
12387
0
      zend_ast *name_ast = ast->child[0];
12388
0
      bool is_fully_qualified;
12389
0
      zend_string *resolved_name = zend_resolve_const_name(
12390
0
        zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
12391
12392
0
      if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
12393
0
        zend_string_release_ex(resolved_name, 0);
12394
0
        return;
12395
0
      }
12396
12397
0
      zend_string_release_ex(resolved_name, 0);
12398
0
      break;
12399
0
    }
12400
0
    case ZEND_AST_CLASS_CONST:
12401
0
    {
12402
0
      zend_ast *class_ast;
12403
0
      zend_ast *name_ast;
12404
0
      zend_string *resolved_name;
12405
12406
0
      zend_eval_const_expr(&ast->child[0]);
12407
0
      zend_eval_const_expr(&ast->child[1]);
12408
12409
0
      if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL
12410
0
        || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) {
12411
0
        return;
12412
0
      }
12413
12414
0
      class_ast = ast->child[0];
12415
0
      name_ast = ast->child[1];
12416
12417
0
      if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
12418
0
        return;
12419
0
      }
12420
12421
0
      resolved_name = zend_resolve_class_name_ast(class_ast);
12422
0
      if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
12423
0
        zend_string_release_ex(resolved_name, 0);
12424
0
        return;
12425
0
      }
12426
12427
0
      zend_string_release_ex(resolved_name, 0);
12428
0
      break;
12429
0
    }
12430
0
    case ZEND_AST_CLASS_NAME:
12431
0
    {
12432
0
      zend_ast *class_ast = ast->child[0];
12433
0
      if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) {
12434
0
        return;
12435
0
      }
12436
0
      break;
12437
0
    }
12438
    // TODO: We should probably use zend_ast_apply to recursively walk nodes without
12439
    // special handling. It is required that all nodes that are part of a const expr
12440
    // are visited. Probably we should be distinguishing evaluation of const expr and
12441
    // normal exprs here.
12442
0
    case ZEND_AST_ARG_LIST:
12443
0
    {
12444
0
      zend_ast_list *list = zend_ast_get_list(ast);
12445
0
      for (uint32_t i = 0; i < list->children; i++) {
12446
0
        zend_eval_const_expr(&list->child[i]);
12447
0
      }
12448
0
      return;
12449
0
    }
12450
0
    case ZEND_AST_NEW:
12451
0
      zend_eval_const_expr(&ast->child[0]);
12452
0
      zend_eval_const_expr(&ast->child[1]);
12453
0
      return;
12454
0
    case ZEND_AST_NAMED_ARG:
12455
0
      zend_eval_const_expr(&ast->child[1]);
12456
0
      return;
12457
0
    case ZEND_AST_CONST_ENUM_INIT:
12458
0
      zend_eval_const_expr(&ast->child[2]);
12459
0
      return;
12460
0
    case ZEND_AST_PROP:
12461
0
    case ZEND_AST_NULLSAFE_PROP:
12462
0
      zend_eval_const_expr(&ast->child[0]);
12463
0
      zend_eval_const_expr(&ast->child[1]);
12464
0
      return;
12465
0
    case ZEND_AST_CAST:
12466
0
      zend_eval_const_expr(&ast->child[0]);
12467
0
      if (ast->child[0]->kind == ZEND_AST_ZVAL
12468
0
       && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) {
12469
0
        break;
12470
0
      }
12471
0
      return;
12472
0
    default:
12473
0
      return;
12474
0
  }
12475
12476
0
  zend_ast_destroy(ast);
12477
0
  *ast_ptr = zend_ast_create_zval(&result);
12478
0
}
12479
/* }}} */