1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 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@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 | Nikita Popov <nikic@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* $Id$ */
22
23 #include <zend_language_parser.h>
24 #include "zend.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_virtual_cwd.h"
32 #include "zend_multibyte.h"
33 #include "zend_language_scanner.h"
34 #include "zend_inheritance.h"
35 #include "zend_vm.h"
36
37 #define SET_NODE(target, src) do { \
38 target ## _type = (src)->op_type; \
39 if ((src)->op_type == IS_CONST) { \
40 target.constant = zend_add_literal(CG(active_op_array), &(src)->u.constant); \
41 } else { \
42 target = (src)->u.op; \
43 } \
44 } while (0)
45
46 #define GET_NODE(target, src) do { \
47 (target)->op_type = src ## _type; \
48 if ((target)->op_type == IS_CONST) { \
49 ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \
50 } else { \
51 (target)->u.op = src; \
52 } \
53 } while (0)
54
55 #define FC(member) (CG(file_context).member)
56
57 typedef struct _zend_loop_var {
58 zend_uchar opcode;
59 zend_uchar var_type;
60 uint32_t var_num;
61 union {
62 uint32_t try_catch_offset;
63 uint32_t live_range_offset;
64 } u;
65 } zend_loop_var;
66
zend_alloc_cache_slot(uint32_t literal)67 static inline void zend_alloc_cache_slot(uint32_t literal) {
68 zend_op_array *op_array = CG(active_op_array);
69 Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
70 op_array->cache_size += sizeof(void*);
71 }
72
73 #define POLYMORPHIC_CACHE_SLOT_SIZE 2
74
zend_alloc_polymorphic_cache_slot(uint32_t literal)75 static inline void zend_alloc_polymorphic_cache_slot(uint32_t literal) {
76 zend_op_array *op_array = CG(active_op_array);
77 Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
78 op_array->cache_size += POLYMORPHIC_CACHE_SLOT_SIZE * sizeof(void*);
79 }
80
81 ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
82 ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
83
84 #ifndef ZTS
85 ZEND_API zend_compiler_globals compiler_globals;
86 ZEND_API zend_executor_globals executor_globals;
87 #endif
88
89 static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2);
90
zend_destroy_property_info_internal(zval * zv)91 static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
92 {
93 zend_property_info *property_info = Z_PTR_P(zv);
94
95 zend_string_release(property_info->name);
96 free(property_info);
97 }
98 /* }}} */
99
zend_destroy_class_constant_internal(zval * zv)100 static void zend_destroy_class_constant_internal(zval *zv) /* {{{ */
101 {
102 free(Z_PTR_P(zv));
103 }
104 /* }}} */
105
zend_new_interned_string_safe(zend_string * str)106 static zend_string *zend_new_interned_string_safe(zend_string *str) /* {{{ */ {
107 zend_string *interned_str;
108
109 zend_string_addref(str);
110 interned_str = zend_new_interned_string(str);
111 if (str != interned_str) {
112 return interned_str;
113 } else {
114 zend_string_release(str);
115 return str;
116 }
117 }
118 /* }}} */
119
zend_build_runtime_definition_key(zend_string * name,unsigned char * lex_pos)120 static zend_string *zend_build_runtime_definition_key(zend_string *name, unsigned char *lex_pos) /* {{{ */
121 {
122 zend_string *result;
123 char char_pos_buf[32];
124 size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
125 zend_string *filename = CG(active_op_array)->filename;
126
127 /* NULL, name length, filename length, last accepting char position length */
128 result = zend_string_alloc(1 + ZSTR_LEN(name) + ZSTR_LEN(filename) + char_pos_len, 0);
129 sprintf(ZSTR_VAL(result), "%c%s%s%s", '\0', ZSTR_VAL(name), ZSTR_VAL(filename), char_pos_buf);
130 return zend_new_interned_string(result);
131 }
132 /* }}} */
133
zend_get_unqualified_name(const zend_string * name,const char ** result,size_t * result_len)134 static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
135 {
136 const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
137 if (ns_separator != NULL) {
138 *result = ns_separator + 1;
139 *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
140 return 1;
141 }
142
143 return 0;
144 }
145 /* }}} */
146
147 struct reserved_class_name {
148 const char *name;
149 size_t len;
150 };
151 static const struct reserved_class_name reserved_class_names[] = {
152 {ZEND_STRL("bool")},
153 {ZEND_STRL("false")},
154 {ZEND_STRL("float")},
155 {ZEND_STRL("int")},
156 {ZEND_STRL("null")},
157 {ZEND_STRL("parent")},
158 {ZEND_STRL("self")},
159 {ZEND_STRL("static")},
160 {ZEND_STRL("string")},
161 {ZEND_STRL("true")},
162 {ZEND_STRL("void")},
163 {ZEND_STRL("iterable")},
164 {NULL, 0}
165 };
166
zend_is_reserved_class_name(const zend_string * name)167 static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
168 {
169 const struct reserved_class_name *reserved = reserved_class_names;
170
171 const char *uqname = ZSTR_VAL(name);
172 size_t uqname_len = ZSTR_LEN(name);
173 zend_get_unqualified_name(name, &uqname, &uqname_len);
174
175 for (; reserved->name; ++reserved) {
176 if (uqname_len == reserved->len
177 && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
178 ) {
179 return 1;
180 }
181 }
182
183 return 0;
184 }
185 /* }}} */
186
zend_assert_valid_class_name(const zend_string * name)187 ZEND_API void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
188 {
189 if (zend_is_reserved_class_name(name)) {
190 zend_error_noreturn(E_COMPILE_ERROR,
191 "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
192 }
193 }
194 /* }}} */
195
196 typedef struct _builtin_type_info {
197 const char* name;
198 const size_t name_len;
199 const zend_uchar type;
200 } builtin_type_info;
201
202 static const builtin_type_info builtin_types[] = {
203 {ZEND_STRL("int"), IS_LONG},
204 {ZEND_STRL("float"), IS_DOUBLE},
205 {ZEND_STRL("string"), IS_STRING},
206 {ZEND_STRL("bool"), _IS_BOOL},
207 {ZEND_STRL("void"), IS_VOID},
208 {ZEND_STRL("iterable"), IS_ITERABLE},
209 {NULL, 0, IS_UNDEF}
210 };
211
212
zend_lookup_builtin_type_by_name(const zend_string * name)213 static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
214 {
215 const builtin_type_info *info = &builtin_types[0];
216
217 for (; info->name; ++info) {
218 if (ZSTR_LEN(name) == info->name_len
219 && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
220 ) {
221 return info->type;
222 }
223 }
224
225 return 0;
226 }
227 /* }}} */
228
229
zend_oparray_context_begin(zend_oparray_context * prev_context)230 void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
231 {
232 *prev_context = CG(context);
233 CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
234 CG(context).vars_size = 0;
235 CG(context).literals_size = 0;
236 CG(context).backpatch_count = 0;
237 CG(context).in_finally = 0;
238 CG(context).fast_call_var = -1;
239 CG(context).try_catch_offset = -1;
240 CG(context).current_brk_cont = -1;
241 CG(context).last_brk_cont = 0;
242 CG(context).brk_cont_array = NULL;
243 CG(context).labels = NULL;
244 }
245 /* }}} */
246
zend_oparray_context_end(zend_oparray_context * prev_context)247 void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
248 {
249 if (CG(context).brk_cont_array) {
250 efree(CG(context).brk_cont_array);
251 CG(context).brk_cont_array = NULL;
252 }
253 if (CG(context).labels) {
254 zend_hash_destroy(CG(context).labels);
255 FREE_HASHTABLE(CG(context).labels);
256 CG(context).labels = NULL;
257 }
258 CG(context) = *prev_context;
259 }
260 /* }}} */
261
zend_reset_import_tables(void)262 static void zend_reset_import_tables(void) /* {{{ */
263 {
264 if (FC(imports)) {
265 zend_hash_destroy(FC(imports));
266 efree(FC(imports));
267 FC(imports) = NULL;
268 }
269
270 if (FC(imports_function)) {
271 zend_hash_destroy(FC(imports_function));
272 efree(FC(imports_function));
273 FC(imports_function) = NULL;
274 }
275
276 if (FC(imports_const)) {
277 zend_hash_destroy(FC(imports_const));
278 efree(FC(imports_const));
279 FC(imports_const) = NULL;
280 }
281 }
282 /* }}} */
283
zend_end_namespace(void)284 static void zend_end_namespace(void) /* {{{ */ {
285 FC(in_namespace) = 0;
286 zend_reset_import_tables();
287 if (FC(current_namespace)) {
288 zend_string_release(FC(current_namespace));
289 FC(current_namespace) = NULL;
290 }
291 }
292 /* }}} */
293
zend_file_context_begin(zend_file_context * prev_context)294 void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
295 {
296 *prev_context = CG(file_context);
297 FC(imports) = NULL;
298 FC(imports_function) = NULL;
299 FC(imports_const) = NULL;
300 FC(current_namespace) = NULL;
301 FC(in_namespace) = 0;
302 FC(has_bracketed_namespaces) = 0;
303 FC(declarables).ticks = 0;
304 }
305 /* }}} */
306
zend_file_context_end(zend_file_context * prev_context)307 void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
308 {
309 zend_end_namespace();
310 CG(file_context) = *prev_context;
311 }
312 /* }}} */
313
zend_init_compiler_data_structures(void)314 void zend_init_compiler_data_structures(void) /* {{{ */
315 {
316 zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var));
317 zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
318 CG(active_class_entry) = NULL;
319 CG(in_compilation) = 0;
320 CG(start_lineno) = 0;
321 zend_hash_init(&CG(const_filenames), 8, NULL, NULL, 0);
322
323 CG(encoding_declared) = 0;
324 }
325 /* }}} */
326
file_handle_dtor(zend_file_handle * fh)327 ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
328 {
329
330 zend_file_handle_dtor(fh);
331 }
332 /* }}} */
333
init_compiler(void)334 void init_compiler(void) /* {{{ */
335 {
336 CG(arena) = zend_arena_create(64 * 1024);
337 CG(active_op_array) = NULL;
338 memset(&CG(context), 0, sizeof(CG(context)));
339 zend_init_compiler_data_structures();
340 zend_init_rsrc_list();
341 zend_hash_init(&CG(filenames_table), 8, NULL, ZVAL_PTR_DTOR, 0);
342 zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
343 CG(unclean_shutdown) = 0;
344 }
345 /* }}} */
346
shutdown_compiler(void)347 void shutdown_compiler(void) /* {{{ */
348 {
349 zend_stack_destroy(&CG(loop_var_stack));
350 zend_stack_destroy(&CG(delayed_oplines_stack));
351 zend_hash_destroy(&CG(filenames_table));
352 zend_hash_destroy(&CG(const_filenames));
353 zend_arena_destroy(CG(arena));
354 }
355 /* }}} */
356
zend_set_compiled_filename(zend_string * new_compiled_filename)357 ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
358 {
359 zval *p, rv;
360
361 if ((p = zend_hash_find(&CG(filenames_table), new_compiled_filename))) {
362 ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
363 CG(compiled_filename) = Z_STR_P(p);
364 return Z_STR_P(p);
365 }
366
367 ZVAL_STR_COPY(&rv, new_compiled_filename);
368 zend_hash_update(&CG(filenames_table), new_compiled_filename, &rv);
369
370 CG(compiled_filename) = new_compiled_filename;
371 return new_compiled_filename;
372 }
373 /* }}} */
374
zend_restore_compiled_filename(zend_string * original_compiled_filename)375 ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
376 {
377 CG(compiled_filename) = original_compiled_filename;
378 }
379 /* }}} */
380
zend_get_compiled_filename(void)381 ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
382 {
383 return CG(compiled_filename);
384 }
385 /* }}} */
386
zend_get_compiled_lineno(void)387 ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
388 {
389 return CG(zend_lineno);
390 }
391 /* }}} */
392
zend_is_compiling(void)393 ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
394 {
395 return CG(in_compilation);
396 }
397 /* }}} */
398
get_temporary_variable(zend_op_array * op_array)399 static uint32_t get_temporary_variable(zend_op_array *op_array) /* {{{ */
400 {
401 return (uint32_t)op_array->T++;
402 }
403 /* }}} */
404
lookup_cv(zend_op_array * op_array,zend_string * name)405 static int lookup_cv(zend_op_array *op_array, zend_string* name) /* {{{ */{
406 int i = 0;
407 zend_ulong hash_value = zend_string_hash_val(name);
408
409 while (i < op_array->last_var) {
410 if (ZSTR_VAL(op_array->vars[i]) == ZSTR_VAL(name) ||
411 (ZSTR_H(op_array->vars[i]) == hash_value &&
412 ZSTR_LEN(op_array->vars[i]) == ZSTR_LEN(name) &&
413 memcmp(ZSTR_VAL(op_array->vars[i]), ZSTR_VAL(name), ZSTR_LEN(name)) == 0)) {
414 zend_string_release(name);
415 return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
416 }
417 i++;
418 }
419 i = op_array->last_var;
420 op_array->last_var++;
421 if (op_array->last_var > CG(context).vars_size) {
422 CG(context).vars_size += 16; /* FIXME */
423 op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
424 }
425
426 op_array->vars[i] = zend_new_interned_string(name);
427 return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
428 }
429 /* }}} */
430
zend_del_literal(zend_op_array * op_array,int n)431 void zend_del_literal(zend_op_array *op_array, int n) /* {{{ */
432 {
433 zval_dtor(CT_CONSTANT_EX(op_array, n));
434 if (n + 1 == op_array->last_literal) {
435 op_array->last_literal--;
436 } else {
437 ZVAL_UNDEF(CT_CONSTANT_EX(op_array, n));
438 }
439 }
440 /* }}} */
441
442 /* Common part of zend_add_literal and zend_append_individual_literal */
zend_insert_literal(zend_op_array * op_array,zval * zv,int literal_position)443 static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
444 {
445 if (Z_TYPE_P(zv) == IS_STRING || Z_TYPE_P(zv) == IS_CONSTANT) {
446 zend_string_hash_val(Z_STR_P(zv));
447 Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
448 if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
449 Z_TYPE_FLAGS_P(zv) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
450 }
451 }
452 ZVAL_COPY_VALUE(CT_CONSTANT_EX(op_array, literal_position), zv);
453 Z_CACHE_SLOT(op_array->literals[literal_position]) = -1;
454 }
455 /* }}} */
456
457 /* Is used while compiling a function, using the context to keep track
458 of an approximate size to avoid to relocate to often.
459 Literals are truncated to actual size in the second compiler pass (pass_two()). */
zend_add_literal(zend_op_array * op_array,zval * zv)460 int zend_add_literal(zend_op_array *op_array, zval *zv) /* {{{ */
461 {
462 int i = op_array->last_literal;
463 op_array->last_literal++;
464 if (i >= CG(context).literals_size) {
465 while (i >= CG(context).literals_size) {
466 CG(context).literals_size += 16; /* FIXME */
467 }
468 op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
469 }
470 zend_insert_literal(op_array, zv, i);
471 return i;
472 }
473 /* }}} */
474
zend_add_literal_string(zend_op_array * op_array,zend_string ** str)475 static inline int zend_add_literal_string(zend_op_array *op_array, zend_string **str) /* {{{ */
476 {
477 int ret;
478 zval zv;
479 ZVAL_STR(&zv, *str);
480 ret = zend_add_literal(op_array, &zv);
481 *str = Z_STR(zv);
482 return ret;
483 }
484 /* }}} */
485
zend_add_func_name_literal(zend_op_array * op_array,zend_string * name)486 static int zend_add_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
487 {
488 /* Original name */
489 int ret = zend_add_literal_string(op_array, &name);
490
491 /* Lowercased name */
492 zend_string *lc_name = zend_string_tolower(name);
493 zend_add_literal_string(op_array, &lc_name);
494
495 return ret;
496 }
497 /* }}} */
498
zend_add_ns_func_name_literal(zend_op_array * op_array,zend_string * name)499 static int zend_add_ns_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
500 {
501 const char *unqualified_name;
502 size_t unqualified_name_len;
503
504 /* Original name */
505 int ret = zend_add_literal_string(op_array, &name);
506
507 /* Lowercased name */
508 zend_string *lc_name = zend_string_tolower(name);
509 zend_add_literal_string(op_array, &lc_name);
510
511 /* Lowercased unqualfied name */
512 if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
513 lc_name = zend_string_alloc(unqualified_name_len, 0);
514 zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
515 zend_add_literal_string(op_array, &lc_name);
516 }
517
518 return ret;
519 }
520 /* }}} */
521
zend_add_class_name_literal(zend_op_array * op_array,zend_string * name)522 static int zend_add_class_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
523 {
524 /* Original name */
525 int ret = zend_add_literal_string(op_array, &name);
526
527 /* Lowercased name */
528 zend_string *lc_name = zend_string_tolower(name);
529 zend_add_literal_string(op_array, &lc_name);
530
531 zend_alloc_cache_slot(ret);
532
533 return ret;
534 }
535 /* }}} */
536
zend_add_const_name_literal(zend_op_array * op_array,zend_string * name,zend_bool unqualified)537 static int zend_add_const_name_literal(zend_op_array *op_array, zend_string *name, zend_bool unqualified) /* {{{ */
538 {
539 zend_string *tmp_name;
540
541 int ret = zend_add_literal_string(op_array, &name);
542
543 size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
544 const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
545 if (after_ns) {
546 after_ns += 1;
547 ns_len = after_ns - ZSTR_VAL(name) - 1;
548 after_ns_len = ZSTR_LEN(name) - ns_len - 1;
549
550 /* lowercased namespace name & original constant name */
551 tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
552 zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
553 zend_add_literal_string(op_array, &tmp_name);
554
555 /* lowercased namespace name & lowercased constant name */
556 tmp_name = zend_string_tolower(name);
557 zend_add_literal_string(op_array, &tmp_name);
558
559 if (!unqualified) {
560 return ret;
561 }
562 } else {
563 after_ns = ZSTR_VAL(name);
564 }
565
566 /* original unqualified constant name */
567 tmp_name = zend_string_init(after_ns, after_ns_len, 0);
568 zend_add_literal_string(op_array, &tmp_name);
569
570 /* lowercased unqualified constant name */
571 tmp_name = zend_string_alloc(after_ns_len, 0);
572 zend_str_tolower_copy(ZSTR_VAL(tmp_name), after_ns, after_ns_len);
573 zend_add_literal_string(op_array, &tmp_name);
574
575 return ret;
576 }
577 /* }}} */
578
579 #define LITERAL_STR(op, str) do { \
580 zval _c; \
581 ZVAL_STR(&_c, str); \
582 op.constant = zend_add_literal(CG(active_op_array), &_c); \
583 } while (0)
584
zend_stop_lexing(void)585 void zend_stop_lexing(void)
586 {
587 if (LANG_SCNG(on_event)) {
588 LANG_SCNG(on_event)(ON_STOP, END, 0, LANG_SCNG(on_event_context));
589 }
590
591 LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
592 }
593
zend_start_live_range(zend_op_array * op_array,uint32_t start)594 static uint32_t zend_start_live_range(zend_op_array *op_array, uint32_t start) /* {{{ */
595 {
596 zend_live_range *range;
597
598 op_array->last_live_range++;
599 op_array->live_range = erealloc(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
600 range = op_array->live_range + op_array->last_live_range - 1;
601 range->start = start;
602 return op_array->last_live_range - 1;
603 }
604 /* }}} */
605
zend_start_live_range_ex(zend_op_array * op_array,uint32_t start)606 static uint32_t zend_start_live_range_ex(zend_op_array *op_array, uint32_t start) /* {{{ */
607 {
608 if (op_array->last_live_range == 0 ||
609 op_array->live_range[op_array->last_live_range - 1].start <= start) {
610 return zend_start_live_range(op_array, start);
611 } else {
612 /* Live ranges have to be sorted by "start" field */
613 uint32_t n = op_array->last_live_range;
614
615 /* move early ranges to make a room */
616 op_array->last_live_range = n + 1;
617 op_array->live_range = erealloc(op_array->live_range, sizeof(zend_live_range) * op_array->last_live_range);
618 do {
619 op_array->live_range[n] = op_array->live_range[n-1];
620 n--;
621 } while (n != 0 && op_array->live_range[n-1].start > start);
622
623 /* initialize new range */
624 op_array->live_range[n].start = start;
625
626 /* update referens to live-ranges from stack */
627 if (!zend_stack_is_empty(&CG(loop_var_stack))) {
628 zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
629 zend_loop_var *base = zend_stack_base(&CG(loop_var_stack));
630 int check_opcodes = 0;
631
632 for (; loop_var >= base; loop_var--) {
633 if (loop_var->opcode == ZEND_RETURN) {
634 /* Stack separator */
635 break;
636 } else if (loop_var->opcode == ZEND_FREE ||
637 loop_var->opcode == ZEND_FE_FREE) {
638 if (loop_var->u.live_range_offset >= n) {
639 loop_var->u.live_range_offset++;
640 check_opcodes = 1;
641 } else {
642 break;
643 }
644 }
645 }
646
647 /* update previously generated FREE/FE_FREE opcodes */
648 if (check_opcodes) {
649 zend_op *opline = op_array->opcodes + op_array->live_range[n+1].start;
650 zend_op *end = op_array->opcodes + op_array->last;
651
652 while (opline < end) {
653 if ((opline->opcode == ZEND_FREE ||
654 opline->opcode == ZEND_FE_FREE) &&
655 (opline->extended_value & ZEND_FREE_ON_RETURN) &&
656 opline->op2.num >= n) {
657 opline->op2.num++;
658 }
659 opline++;
660 }
661 }
662 }
663 return n;
664 }
665 }
666 /* }}} */
667
zend_end_live_range(zend_op_array * op_array,uint32_t offset,uint32_t end,uint32_t kind,uint32_t var)668 static void zend_end_live_range(zend_op_array *op_array, uint32_t offset, uint32_t end, uint32_t kind, uint32_t var) /* {{{ */
669 {
670 zend_live_range *range = op_array->live_range + offset;
671
672 if (range->start == end && offset == (uint32_t)op_array->last_live_range - 1) {
673 op_array->last_live_range--;
674 } else {
675 range->end = end;
676 range->var = (var * sizeof(zval)) | kind;
677 }
678 }
679 /* }}} */
680
zend_begin_loop(zend_uchar free_opcode,const znode * loop_var)681 static inline void zend_begin_loop(zend_uchar free_opcode, const znode *loop_var) /* {{{ */
682 {
683 zend_brk_cont_element *brk_cont_element;
684 int parent = CG(context).current_brk_cont;
685 zend_loop_var info = {0};
686
687 CG(context).current_brk_cont = CG(context).last_brk_cont;
688 brk_cont_element = get_next_brk_cont_element();
689 brk_cont_element->parent = parent;
690
691 if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) {
692 uint32_t start = get_next_op_number(CG(active_op_array));
693
694 info.opcode = free_opcode;
695 info.var_type = loop_var->op_type;
696 info.var_num = loop_var->u.op.var;
697 info.u.live_range_offset = zend_start_live_range(CG(active_op_array), start);
698 brk_cont_element->start = start;
699 } else {
700 info.opcode = ZEND_NOP;
701 /* The start field is used to free temporary variables in case of exceptions.
702 * We won't try to free something of we don't have loop variable. */
703 brk_cont_element->start = -1;
704 }
705
706 zend_stack_push(&CG(loop_var_stack), &info);
707 }
708 /* }}} */
709
zend_end_loop(int cont_addr,const znode * var_node)710 static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */
711 {
712 uint32_t end = get_next_op_number(CG(active_op_array));
713 zend_brk_cont_element *brk_cont_element
714 = &CG(context).brk_cont_array[CG(context).current_brk_cont];
715 brk_cont_element->cont = cont_addr;
716 brk_cont_element->brk = end;
717 CG(context).current_brk_cont = brk_cont_element->parent;
718
719 if (brk_cont_element->start != -1) {
720 zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
721 zend_end_live_range(CG(active_op_array), loop_var->u.live_range_offset, end,
722 loop_var->opcode == ZEND_FE_FREE ? ZEND_LIVE_LOOP : ZEND_LIVE_TMPVAR,
723 var_node->u.op.var);
724 }
725
726 zend_stack_del_top(&CG(loop_var_stack));
727 }
728 /* }}} */
729
zend_do_free(znode * op1)730 void zend_do_free(znode *op1) /* {{{ */
731 {
732 if (op1->op_type == IS_TMP_VAR) {
733 zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
734
735 while (opline->opcode == ZEND_END_SILENCE) {
736 opline--;
737 }
738
739 if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) {
740 if (opline->opcode == ZEND_BOOL || opline->opcode == ZEND_BOOL_NOT) {
741 return;
742 }
743 }
744
745 zend_emit_op(NULL, ZEND_FREE, op1, NULL);
746 } else if (op1->op_type == IS_VAR) {
747 zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
748 while (opline->opcode == ZEND_END_SILENCE ||
749 opline->opcode == ZEND_EXT_FCALL_END ||
750 opline->opcode == ZEND_OP_DATA) {
751 opline--;
752 }
753 if (opline->result_type == IS_VAR
754 && opline->result.var == op1->u.op.var) {
755 if (opline->opcode == ZEND_FETCH_R ||
756 opline->opcode == ZEND_FETCH_DIM_R ||
757 opline->opcode == ZEND_FETCH_OBJ_R ||
758 opline->opcode == ZEND_FETCH_STATIC_PROP_R) {
759 /* It's very rare and useless case. It's better to use
760 additional FREE opcode and simplify the FETCH handlers
761 their selves */
762 zend_emit_op(NULL, ZEND_FREE, op1, NULL);
763 } else if (opline->opcode == ZEND_FETCH_THIS) {
764 opline->opcode = ZEND_NOP;
765 opline->result_type = IS_UNUSED;
766 } else {
767 opline->result_type = IS_UNUSED;
768 }
769 } else {
770 while (opline >= CG(active_op_array)->opcodes) {
771 if (opline->opcode == ZEND_FETCH_LIST &&
772 opline->op1_type == IS_VAR &&
773 opline->op1.var == op1->u.op.var) {
774 zend_emit_op(NULL, ZEND_FREE, op1, NULL);
775 return;
776 }
777 if (opline->result_type == IS_VAR
778 && opline->result.var == op1->u.op.var) {
779 if (opline->opcode == ZEND_NEW) {
780 zend_emit_op(NULL, ZEND_FREE, op1, NULL);
781 }
782 break;
783 }
784 opline--;
785 }
786 }
787 } else if (op1->op_type == IS_CONST) {
788 /* Destroy value without using GC: When opcache moves arrays into SHM it will
789 * free the zend_array structure, so references to it from outside the op array
790 * become invalid. GC would cause such a reference in the root buffer. */
791 zval_ptr_dtor_nogc(&op1->u.constant);
792 }
793 }
794 /* }}} */
795
zend_add_class_modifier(uint32_t flags,uint32_t new_flag)796 uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
797 {
798 uint32_t new_flags = flags | new_flag;
799 if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
800 zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
801 }
802 if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
803 zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
804 }
805 if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
806 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class");
807 }
808 return new_flags;
809 }
810 /* }}} */
811
zend_add_member_modifier(uint32_t flags,uint32_t new_flag)812 uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
813 {
814 uint32_t new_flags = flags | new_flag;
815 if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
816 zend_error_noreturn(E_COMPILE_ERROR, "Multiple access type modifiers are not allowed");
817 }
818 if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
819 zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
820 }
821 if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
822 zend_error_noreturn(E_COMPILE_ERROR, "Multiple static modifiers are not allowed");
823 }
824 if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
825 zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
826 }
827 if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
828 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class member");
829 }
830 return new_flags;
831 }
832 /* }}} */
833
zend_concat3(char * str1,size_t str1_len,char * str2,size_t str2_len,char * str3,size_t str3_len)834 zend_string *zend_concat3(char *str1, size_t str1_len, char *str2, size_t str2_len, char *str3, size_t str3_len) /* {{{ */
835 {
836 size_t len = str1_len + str2_len + str3_len;
837 zend_string *res = zend_string_alloc(len, 0);
838
839 memcpy(ZSTR_VAL(res), str1, str1_len);
840 memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
841 memcpy(ZSTR_VAL(res) + str1_len + str2_len, str3, str3_len);
842 ZSTR_VAL(res)[len] = '\0';
843
844 return res;
845 }
846
zend_concat_names(char * name1,size_t name1_len,char * name2,size_t name2_len)847 zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
848 return zend_concat3(name1, name1_len, "\\", 1, name2, name2_len);
849 }
850
zend_prefix_with_ns(zend_string * name)851 zend_string *zend_prefix_with_ns(zend_string *name) {
852 if (FC(current_namespace)) {
853 zend_string *ns = FC(current_namespace);
854 return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
855 } else {
856 return zend_string_copy(name);
857 }
858 }
859
zend_hash_find_ptr_lc(HashTable * ht,const char * str,size_t len)860 void *zend_hash_find_ptr_lc(HashTable *ht, const char *str, size_t len) {
861 void *result;
862 zend_string *lcname;
863 ALLOCA_FLAG(use_heap);
864
865 ZSTR_ALLOCA_ALLOC(lcname, len, use_heap);
866 zend_str_tolower_copy(ZSTR_VAL(lcname), str, len);
867 result = zend_hash_find_ptr(ht, lcname);
868 ZSTR_ALLOCA_FREE(lcname, use_heap);
869
870 return result;
871 }
872
zend_resolve_non_class_name(zend_string * name,uint32_t type,zend_bool * is_fully_qualified,zend_bool case_sensitive,HashTable * current_import_sub)873 zend_string *zend_resolve_non_class_name(
874 zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
875 zend_bool case_sensitive, HashTable *current_import_sub
876 ) {
877 char *compound;
878 *is_fully_qualified = 0;
879
880 if (ZSTR_VAL(name)[0] == '\\') {
881 /* Remove \ prefix (only relevant if this is a string rather than a label) */
882 *is_fully_qualified = 1;
883 return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
884 }
885
886 if (type == ZEND_NAME_FQ) {
887 *is_fully_qualified = 1;
888 return zend_string_copy(name);
889 }
890
891 if (type == ZEND_NAME_RELATIVE) {
892 *is_fully_qualified = 1;
893 return zend_prefix_with_ns(name);
894 }
895
896 if (current_import_sub) {
897 /* If an unqualified name is a function/const alias, replace it. */
898 zend_string *import_name;
899 if (case_sensitive) {
900 import_name = zend_hash_find_ptr(current_import_sub, name);
901 } else {
902 import_name = zend_hash_find_ptr_lc(current_import_sub, ZSTR_VAL(name), ZSTR_LEN(name));
903 }
904
905 if (import_name) {
906 *is_fully_qualified = 1;
907 return zend_string_copy(import_name);
908 }
909 }
910
911 compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
912 if (compound) {
913 *is_fully_qualified = 1;
914 }
915
916 if (compound && FC(imports)) {
917 /* If the first part of a qualified name is an alias, substitute it. */
918 size_t len = compound - ZSTR_VAL(name);
919 zend_string *import_name = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
920
921 if (import_name) {
922 return zend_concat_names(
923 ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
924 }
925 }
926
927 return zend_prefix_with_ns(name);
928 }
929 /* }}} */
930
zend_resolve_function_name(zend_string * name,uint32_t type,zend_bool * is_fully_qualified)931 zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
932 {
933 return zend_resolve_non_class_name(
934 name, type, is_fully_qualified, 0, FC(imports_function));
935 }
936 /* }}} */
937
zend_resolve_const_name(zend_string * name,uint32_t type,zend_bool * is_fully_qualified)938 zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
939 return zend_resolve_non_class_name(
940 name, type, is_fully_qualified, 1, FC(imports_const));
941 }
942 /* }}} */
943
zend_resolve_class_name(zend_string * name,uint32_t type)944 zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
945 {
946 char *compound;
947
948 if (type == ZEND_NAME_RELATIVE) {
949 return zend_prefix_with_ns(name);
950 }
951
952 if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
953 /* Remove \ prefix (only relevant if this is a string rather than a label) */
954 if (ZSTR_VAL(name)[0] == '\\') {
955 name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
956 } else {
957 zend_string_addref(name);
958 }
959 /* Ensure that \self, \parent and \static are not used */
960 if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
961 zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
962 }
963 return name;
964 }
965
966 if (FC(imports)) {
967 compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
968 if (compound) {
969 /* If the first part of a qualified name is an alias, substitute it. */
970 size_t len = compound - ZSTR_VAL(name);
971 zend_string *import_name =
972 zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
973
974 if (import_name) {
975 return zend_concat_names(
976 ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
977 }
978 } else {
979 /* If an unqualified name is an alias, replace it. */
980 zend_string *import_name
981 = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name));
982
983 if (import_name) {
984 return zend_string_copy(import_name);
985 }
986 }
987 }
988
989 /* If not fully qualified and not an alias, prepend the current namespace */
990 return zend_prefix_with_ns(name);
991 }
992 /* }}} */
993
zend_resolve_class_name_ast(zend_ast * ast)994 zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
995 {
996 zval *class_name = zend_ast_get_zval(ast);
997 if (Z_TYPE_P(class_name) != IS_STRING) {
998 zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
999 }
1000 return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
1001 }
1002 /* }}} */
1003
label_ptr_dtor(zval * zv)1004 static void label_ptr_dtor(zval *zv) /* {{{ */
1005 {
1006 efree_size(Z_PTR_P(zv), sizeof(zend_label));
1007 }
1008 /* }}} */
1009
str_dtor(zval * zv)1010 static void str_dtor(zval *zv) /* {{{ */ {
1011 zend_string_release(Z_STR_P(zv));
1012 }
1013 /* }}} */
1014
1015 static zend_bool zend_is_call(zend_ast *ast);
1016
zend_add_try_element(uint32_t try_op)1017 static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
1018 {
1019 zend_op_array *op_array = CG(active_op_array);
1020 uint32_t try_catch_offset = op_array->last_try_catch++;
1021 zend_try_catch_element *elem;
1022
1023 op_array->try_catch_array = safe_erealloc(
1024 op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
1025
1026 elem = &op_array->try_catch_array[try_catch_offset];
1027 elem->try_op = try_op;
1028 elem->catch_op = 0;
1029 elem->finally_op = 0;
1030 elem->finally_end = 0;
1031
1032 return try_catch_offset;
1033 }
1034 /* }}} */
1035
function_add_ref(zend_function * function)1036 ZEND_API void function_add_ref(zend_function *function) /* {{{ */
1037 {
1038 if (function->type == ZEND_USER_FUNCTION) {
1039 zend_op_array *op_array = &function->op_array;
1040
1041 if (op_array->refcount) {
1042 (*op_array->refcount)++;
1043 }
1044 if (op_array->static_variables) {
1045 if (!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
1046 GC_REFCOUNT(op_array->static_variables)++;
1047 }
1048 }
1049 op_array->run_time_cache = NULL;
1050 } else if (function->type == ZEND_INTERNAL_FUNCTION) {
1051 if (function->common.function_name) {
1052 zend_string_addref(function->common.function_name);
1053 }
1054 }
1055 }
1056 /* }}} */
1057
do_bind_function(const zend_op_array * op_array,const zend_op * opline,HashTable * function_table,zend_bool compile_time)1058 ZEND_API int do_bind_function(const zend_op_array *op_array, const zend_op *opline, HashTable *function_table, zend_bool compile_time) /* {{{ */
1059 {
1060 zend_function *function, *new_function;
1061 zval *lcname, *rtd_key;
1062
1063 if (compile_time) {
1064 lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
1065 rtd_key = lcname + 1;
1066 } else {
1067 lcname = RT_CONSTANT(op_array, opline->op1);
1068 rtd_key = lcname + 1;
1069 }
1070
1071 function = zend_hash_find_ptr(function_table, Z_STR_P(rtd_key));
1072 new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
1073 memcpy(new_function, function, sizeof(zend_op_array));
1074 if (zend_hash_add_ptr(function_table, Z_STR_P(lcname), new_function) == NULL) {
1075 int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
1076 zend_function *old_function;
1077
1078 if ((old_function = zend_hash_find_ptr(function_table, Z_STR_P(lcname))) != NULL
1079 && old_function->type == ZEND_USER_FUNCTION
1080 && old_function->op_array.last > 0) {
1081 zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
1082 ZSTR_VAL(function->common.function_name),
1083 ZSTR_VAL(old_function->op_array.filename),
1084 old_function->op_array.opcodes[0].lineno);
1085 } else {
1086 zend_error_noreturn(error_level, "Cannot redeclare %s()", ZSTR_VAL(function->common.function_name));
1087 }
1088 return FAILURE;
1089 } else {
1090 if (function->op_array.refcount) {
1091 (*function->op_array.refcount)++;
1092 }
1093 function->op_array.static_variables = NULL; /* NULL out the unbound function */
1094 return SUCCESS;
1095 }
1096 }
1097 /* }}} */
1098
do_bind_class(const zend_op_array * op_array,const zend_op * opline,HashTable * class_table,zend_bool compile_time)1099 ZEND_API zend_class_entry *do_bind_class(const zend_op_array* op_array, const zend_op *opline, HashTable *class_table, zend_bool compile_time) /* {{{ */
1100 {
1101 zend_class_entry *ce;
1102 zval *lcname, *rtd_key;
1103
1104 if (compile_time) {
1105 lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
1106 rtd_key = lcname + 1;
1107 } else {
1108 lcname = RT_CONSTANT(op_array, opline->op1);
1109 rtd_key = lcname + 1;
1110 }
1111 ce = zend_hash_find_ptr(class_table, Z_STR_P(rtd_key));
1112 ZEND_ASSERT(ce);
1113 ce->refcount++;
1114 if (zend_hash_add_ptr(class_table, Z_STR_P(lcname), ce) == NULL) {
1115 ce->refcount--;
1116 if (!compile_time) {
1117 /* If we're in compile time, in practice, it's quite possible
1118 * that we'll never reach this class declaration at runtime,
1119 * so we shut up about it. This allows the if (!defined('FOO')) { return; }
1120 * approach to work.
1121 */
1122 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
1123 }
1124 return NULL;
1125 } else {
1126 if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLEMENT_INTERFACES|ZEND_ACC_IMPLEMENT_TRAITS))) {
1127 zend_verify_abstract_class(ce);
1128 }
1129 return ce;
1130 }
1131 }
1132 /* }}} */
1133
do_bind_inherited_class(const zend_op_array * op_array,const zend_op * opline,HashTable * class_table,zend_class_entry * parent_ce,zend_bool compile_time)1134 ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_class_entry *parent_ce, zend_bool compile_time) /* {{{ */
1135 {
1136 zend_class_entry *ce;
1137 zval *lcname, *rtd_key;
1138
1139 if (compile_time) {
1140 lcname = CT_CONSTANT_EX(op_array, opline->op1.constant);
1141 rtd_key = lcname + 1;
1142 } else {
1143 lcname = RT_CONSTANT(op_array, opline->op1);
1144 rtd_key = lcname + 1;
1145 }
1146
1147 ce = zend_hash_find_ptr(class_table, Z_STR_P(rtd_key));
1148
1149 if (!ce) {
1150 if (!compile_time) {
1151 /* If we're in compile time, in practice, it's quite possible
1152 * that we'll never reach this class declaration at runtime,
1153 * so we shut up about it. This allows the if (!defined('FOO')) { return; }
1154 * approach to work.
1155 */
1156 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s, because the name is already in use", zend_get_object_type(Z_OBJCE_P(lcname)));
1157 }
1158 return NULL;
1159 }
1160
1161 if (zend_hash_exists(class_table, Z_STR_P(lcname))) {
1162 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
1163 }
1164
1165 zend_do_inheritance(ce, parent_ce);
1166
1167 ce->refcount++;
1168
1169 /* Register the derived class */
1170 if (zend_hash_add_ptr(class_table, Z_STR_P(lcname), ce) == NULL) {
1171 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(ce->name));
1172 }
1173 return ce;
1174 }
1175 /* }}} */
1176
zend_do_early_binding(void)1177 void zend_do_early_binding(void) /* {{{ */
1178 {
1179 zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
1180 HashTable *table;
1181
1182 while (opline->opcode == ZEND_TICKS && opline > CG(active_op_array)->opcodes) {
1183 opline--;
1184 }
1185
1186 switch (opline->opcode) {
1187 case ZEND_DECLARE_FUNCTION:
1188 if (do_bind_function(CG(active_op_array), opline, CG(function_table), 1) == FAILURE) {
1189 return;
1190 }
1191 table = CG(function_table);
1192 break;
1193 case ZEND_DECLARE_CLASS:
1194 if (do_bind_class(CG(active_op_array), opline, CG(class_table), 1) == NULL) {
1195 return;
1196 }
1197 table = CG(class_table);
1198 break;
1199 case ZEND_DECLARE_INHERITED_CLASS:
1200 {
1201 zend_op *fetch_class_opline = opline-1;
1202 zval *parent_name;
1203 zend_class_entry *ce;
1204
1205 parent_name = CT_CONSTANT(fetch_class_opline->op2);
1206 if (((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) == NULL) ||
1207 ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES) &&
1208 (ce->type == ZEND_INTERNAL_CLASS))) {
1209 if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
1210 uint32_t *opline_num = &CG(active_op_array)->early_binding;
1211
1212 while (*opline_num != (uint32_t)-1) {
1213 opline_num = &CG(active_op_array)->opcodes[*opline_num].result.opline_num;
1214 }
1215 *opline_num = opline - CG(active_op_array)->opcodes;
1216 opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
1217 opline->result_type = IS_UNUSED;
1218 opline->result.opline_num = -1;
1219 }
1220 return;
1221 }
1222 if (do_bind_inherited_class(CG(active_op_array), opline, CG(class_table), ce, 1) == NULL) {
1223 return;
1224 }
1225 /* clear unnecessary ZEND_FETCH_CLASS opcode */
1226 zend_del_literal(CG(active_op_array), fetch_class_opline->op2.constant);
1227 MAKE_NOP(fetch_class_opline);
1228
1229 table = CG(class_table);
1230 break;
1231 }
1232 case ZEND_VERIFY_ABSTRACT_CLASS:
1233 case ZEND_ADD_INTERFACE:
1234 case ZEND_ADD_TRAIT:
1235 case ZEND_BIND_TRAITS:
1236 /* We currently don't early-bind classes that implement interfaces */
1237 /* Classes with traits are handled exactly the same, no early-bind here */
1238 return;
1239 default:
1240 zend_error_noreturn(E_COMPILE_ERROR, "Invalid binding type");
1241 return;
1242 }
1243
1244 zend_hash_del(table, Z_STR_P(CT_CONSTANT(opline->op1)+1));
1245 zend_del_literal(CG(active_op_array), opline->op1.constant+1);
1246 zend_del_literal(CG(active_op_array), opline->op1.constant);
1247 MAKE_NOP(opline);
1248 }
1249 /* }}} */
1250
zend_mark_function_as_generator()1251 static void zend_mark_function_as_generator() /* {{{ */
1252 {
1253 if (!CG(active_op_array)->function_name) {
1254 zend_error_noreturn(E_COMPILE_ERROR,
1255 "The \"yield\" expression can only be used inside a function");
1256 }
1257
1258 if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
1259 zend_arg_info return_info = CG(active_op_array)->arg_info[-1];
1260
1261 if (return_info.type_hint != IS_ITERABLE) {
1262 const char *msg = "Generators may only declare a return type of Generator, Iterator, Traversable, or iterable, %s is not permitted";
1263
1264 if (!return_info.class_name) {
1265 zend_error_noreturn(E_COMPILE_ERROR, msg, zend_get_type_by_const(return_info.type_hint));
1266 }
1267
1268 if (!zend_string_equals_literal_ci(return_info.class_name, "Traversable")
1269 && !zend_string_equals_literal_ci(return_info.class_name, "Iterator")
1270 && !zend_string_equals_literal_ci(return_info.class_name, "Generator")) {
1271 zend_error_noreturn(E_COMPILE_ERROR, msg, ZSTR_VAL(return_info.class_name));
1272 }
1273 }
1274 }
1275
1276 CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR;
1277 }
1278 /* }}} */
1279
zend_do_delayed_early_binding(const zend_op_array * op_array)1280 ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array) /* {{{ */
1281 {
1282 if (op_array->early_binding != (uint32_t)-1) {
1283 zend_bool orig_in_compilation = CG(in_compilation);
1284 uint32_t opline_num = op_array->early_binding;
1285 zend_class_entry *ce;
1286
1287 CG(in_compilation) = 1;
1288 while (opline_num != (uint32_t)-1) {
1289 zval *parent_name = RT_CONSTANT(op_array, op_array->opcodes[opline_num-1].op2);
1290 if ((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) != NULL) {
1291 do_bind_inherited_class(op_array, &op_array->opcodes[opline_num], EG(class_table), ce, 0);
1292 }
1293 opline_num = op_array->opcodes[opline_num].result.opline_num;
1294 }
1295 CG(in_compilation) = orig_in_compilation;
1296 }
1297 }
1298 /* }}} */
1299
zend_mangle_property_name(const char * src1,size_t src1_length,const char * src2,size_t src2_length,int internal)1300 ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal) /* {{{ */
1301 {
1302 size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1303 zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1304
1305 ZSTR_VAL(prop_name)[0] = '\0';
1306 memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1307 memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1308 return prop_name;
1309 }
1310 /* }}} */
1311
zend_strnlen(const char * s,size_t maxlen)1312 static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
1313 {
1314 size_t len = 0;
1315 while (*s++ && maxlen--) len++;
1316 return len;
1317 }
1318 /* }}} */
1319
zend_unmangle_property_name_ex(const zend_string * name,const char ** class_name,const char ** prop_name,size_t * prop_len)1320 ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1321 {
1322 size_t class_name_len;
1323 size_t anonclass_src_len;
1324
1325 *class_name = NULL;
1326
1327 if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') {
1328 *prop_name = ZSTR_VAL(name);
1329 if (prop_len) {
1330 *prop_len = ZSTR_LEN(name);
1331 }
1332 return SUCCESS;
1333 }
1334 if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1335 zend_error(E_NOTICE, "Illegal member variable name");
1336 *prop_name = ZSTR_VAL(name);
1337 if (prop_len) {
1338 *prop_len = ZSTR_LEN(name);
1339 }
1340 return FAILURE;
1341 }
1342
1343 class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1344 if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1345 zend_error(E_NOTICE, "Corrupt member variable name");
1346 *prop_name = ZSTR_VAL(name);
1347 if (prop_len) {
1348 *prop_len = ZSTR_LEN(name);
1349 }
1350 return FAILURE;
1351 }
1352
1353 *class_name = ZSTR_VAL(name) + 1;
1354 anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2);
1355 if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) {
1356 class_name_len += anonclass_src_len + 1;
1357 }
1358 *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1359 if (prop_len) {
1360 *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1361 }
1362 return SUCCESS;
1363 }
1364 /* }}} */
1365
zend_lookup_reserved_const(const char * name,size_t len)1366 static zend_constant *zend_lookup_reserved_const(const char *name, size_t len) /* {{{ */
1367 {
1368 zend_constant *c = zend_hash_find_ptr_lc(EG(zend_constants), name, len);
1369 if (c && !(c->flags & CONST_CS) && (c->flags & CONST_CT_SUBST)) {
1370 return c;
1371 }
1372 return NULL;
1373 }
1374 /* }}} */
1375
zend_try_ct_eval_const(zval * zv,zend_string * name,zend_bool is_fully_qualified)1376 static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
1377 {
1378 zend_constant *c;
1379
1380 /* Substitute case-sensitive (or lowercase) constants */
1381 c = zend_hash_find_ptr(EG(zend_constants), name);
1382 if (c && (
1383 ((c->flags & CONST_PERSISTENT) && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION))
1384 || (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
1385 )) {
1386 ZVAL_DUP(zv, &c->value);
1387 return 1;
1388 }
1389
1390 {
1391 /* Substitute true, false and null (including unqualified usage in namespaces) */
1392 const char *lookup_name = ZSTR_VAL(name);
1393 size_t lookup_len = ZSTR_LEN(name);
1394
1395 if (!is_fully_qualified) {
1396 zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1397 }
1398
1399 c = zend_lookup_reserved_const(lookup_name, lookup_len);
1400 if (c) {
1401 ZVAL_DUP(zv, &c->value);
1402 return 1;
1403 }
1404 }
1405
1406 return 0;
1407 }
1408 /* }}} */
1409
zend_is_scope_known()1410 static inline zend_bool zend_is_scope_known() /* {{{ */
1411 {
1412 if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1413 /* Closures can be rebound to a different scope */
1414 return 0;
1415 }
1416
1417 if (!CG(active_class_entry)) {
1418 /* The scope is known if we're in a free function (no scope), but not if we're in
1419 * a file/eval (which inherits including/eval'ing scope). */
1420 return CG(active_op_array)->function_name != NULL;
1421 }
1422
1423 /* For traits self etc refers to the using class, not the trait itself */
1424 return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1425 }
1426 /* }}} */
1427
class_name_refers_to_active_ce(zend_string * class_name,uint32_t fetch_type)1428 static inline zend_bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
1429 {
1430 if (!CG(active_class_entry)) {
1431 return 0;
1432 }
1433 if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1434 return 1;
1435 }
1436 return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1437 && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1438 }
1439 /* }}} */
1440
zend_get_class_fetch_type(zend_string * name)1441 uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
1442 {
1443 if (zend_string_equals_literal_ci(name, "self")) {
1444 return ZEND_FETCH_CLASS_SELF;
1445 } else if (zend_string_equals_literal_ci(name, "parent")) {
1446 return ZEND_FETCH_CLASS_PARENT;
1447 } else if (zend_string_equals_literal_ci(name, "static")) {
1448 return ZEND_FETCH_CLASS_STATIC;
1449 } else {
1450 return ZEND_FETCH_CLASS_DEFAULT;
1451 }
1452 }
1453 /* }}} */
1454
zend_get_class_fetch_type_ast(zend_ast * name_ast)1455 static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1456 {
1457 /* Fully qualified names are always default refs */
1458 if (name_ast->attr == ZEND_NAME_FQ) {
1459 return ZEND_FETCH_CLASS_DEFAULT;
1460 }
1461
1462 return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1463 }
1464 /* }}} */
1465
zend_ensure_valid_class_fetch_type(uint32_t fetch_type)1466 static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1467 {
1468 if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && !CG(active_class_entry) && zend_is_scope_known()) {
1469 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1470 fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1471 fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1472 }
1473 }
1474 /* }}} */
1475
zend_try_compile_const_expr_resolve_class_name(zval * zv,zend_ast * class_ast,zend_ast * name_ast,zend_bool constant)1476 static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast, zend_ast *name_ast, zend_bool constant) /* {{{ */
1477 {
1478 uint32_t fetch_type;
1479
1480 if (name_ast->kind != ZEND_AST_ZVAL) {
1481 return 0;
1482 }
1483
1484 if (!zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "class")) {
1485 return 0;
1486 }
1487
1488 if (class_ast->kind != ZEND_AST_ZVAL) {
1489 zend_error_noreturn(E_COMPILE_ERROR,
1490 "Dynamic class names are not allowed in compile-time ::class fetch");
1491 }
1492
1493 fetch_type = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
1494 zend_ensure_valid_class_fetch_type(fetch_type);
1495
1496 switch (fetch_type) {
1497 case ZEND_FETCH_CLASS_SELF:
1498 if (constant || (CG(active_class_entry) && zend_is_scope_known())) {
1499 ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1500 } else {
1501 ZVAL_NULL(zv);
1502 }
1503 return 1;
1504 case ZEND_FETCH_CLASS_STATIC:
1505 case ZEND_FETCH_CLASS_PARENT:
1506 if (constant) {
1507 zend_error_noreturn(E_COMPILE_ERROR,
1508 "%s::class cannot be used for compile-time class name resolution",
1509 fetch_type == ZEND_FETCH_CLASS_STATIC ? "static" : "parent"
1510 );
1511 } else {
1512 ZVAL_NULL(zv);
1513 }
1514 return 1;
1515 case ZEND_FETCH_CLASS_DEFAULT:
1516 ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1517 return 1;
1518 EMPTY_SWITCH_DEFAULT_CASE()
1519 }
1520 }
1521 /* }}} */
1522
zend_try_ct_eval_class_const(zval * zv,zend_string * class_name,zend_string * name)1523 static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1524 {
1525 uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1526 zend_class_constant *cc;
1527 zval *c;
1528
1529 if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1530 cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
1531 } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1532 zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
1533 if (ce) {
1534 cc = zend_hash_find_ptr(&ce->constants_table, name);
1535 } else {
1536 return 0;
1537 }
1538 } else {
1539 return 0;
1540 }
1541
1542 if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1543 return 0;
1544 }
1545
1546 if (!cc || !zend_verify_const_access(cc, CG(active_class_entry))) {
1547 return 0;
1548 }
1549
1550 c = &cc->value;
1551
1552 /* Substitute case-sensitive (or lowercase) persistent class constants */
1553 if (Z_TYPE_P(c) < IS_OBJECT) {
1554 ZVAL_DUP(zv, c);
1555 return 1;
1556 }
1557
1558 return 0;
1559 }
1560 /* }}} */
1561
zend_add_to_list(void * result,void * item)1562 static void zend_add_to_list(void *result, void *item) /* {{{ */
1563 {
1564 void** list = *(void**)result;
1565 size_t n = 0;
1566
1567 if (list) {
1568 while (list[n]) {
1569 n++;
1570 }
1571 }
1572
1573 list = erealloc(list, sizeof(void*) * (n+2));
1574
1575 list[n] = item;
1576 list[n+1] = NULL;
1577
1578 *(void**)result = list;
1579 }
1580 /* }}} */
1581
zend_do_extended_info(void)1582 void zend_do_extended_info(void) /* {{{ */
1583 {
1584 zend_op *opline;
1585
1586 if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1587 return;
1588 }
1589
1590 opline = get_next_op(CG(active_op_array));
1591
1592 opline->opcode = ZEND_EXT_STMT;
1593 SET_UNUSED(opline->op1);
1594 SET_UNUSED(opline->op2);
1595 }
1596 /* }}} */
1597
zend_do_extended_fcall_begin(void)1598 void zend_do_extended_fcall_begin(void) /* {{{ */
1599 {
1600 zend_op *opline;
1601
1602 if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1603 return;
1604 }
1605
1606 opline = get_next_op(CG(active_op_array));
1607
1608 opline->opcode = ZEND_EXT_FCALL_BEGIN;
1609 SET_UNUSED(opline->op1);
1610 SET_UNUSED(opline->op2);
1611 }
1612 /* }}} */
1613
zend_do_extended_fcall_end(void)1614 void zend_do_extended_fcall_end(void) /* {{{ */
1615 {
1616 zend_op *opline;
1617
1618 if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1619 return;
1620 }
1621
1622 opline = get_next_op(CG(active_op_array));
1623
1624 opline->opcode = ZEND_EXT_FCALL_END;
1625 SET_UNUSED(opline->op1);
1626 SET_UNUSED(opline->op2);
1627 }
1628 /* }}} */
1629
zend_is_auto_global_str(char * name,size_t len)1630 zend_bool zend_is_auto_global_str(char *name, size_t len) /* {{{ */ {
1631 zend_auto_global *auto_global;
1632
1633 if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1634 if (auto_global->armed) {
1635 auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1636 }
1637 return 1;
1638 }
1639 return 0;
1640 }
1641 /* }}} */
1642
zend_is_auto_global(zend_string * name)1643 zend_bool zend_is_auto_global(zend_string *name) /* {{{ */
1644 {
1645 zend_auto_global *auto_global;
1646
1647 if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1648 if (auto_global->armed) {
1649 auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1650 }
1651 return 1;
1652 }
1653 return 0;
1654 }
1655 /* }}} */
1656
zend_register_auto_global(zend_string * name,zend_bool jit,zend_auto_global_callback auto_global_callback)1657 int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1658 {
1659 zend_auto_global auto_global;
1660 int retval;
1661
1662 auto_global.name = zend_new_interned_string(name);
1663 auto_global.auto_global_callback = auto_global_callback;
1664 auto_global.jit = jit;
1665
1666 retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
1667
1668 zend_string_release(name);
1669 return retval;
1670 }
1671 /* }}} */
1672
zend_activate_auto_globals(void)1673 ZEND_API void zend_activate_auto_globals(void) /* {{{ */
1674 {
1675 zend_auto_global *auto_global;
1676
1677 ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
1678 if (auto_global->jit) {
1679 auto_global->armed = 1;
1680 } else if (auto_global->auto_global_callback) {
1681 auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1682 } else {
1683 auto_global->armed = 0;
1684 }
1685 } ZEND_HASH_FOREACH_END();
1686 }
1687 /* }}} */
1688
zendlex(zend_parser_stack_elem * elem)1689 int zendlex(zend_parser_stack_elem *elem) /* {{{ */
1690 {
1691 zval zv;
1692 int retval;
1693 uint32_t start_lineno;
1694
1695 if (CG(increment_lineno)) {
1696 CG(zend_lineno)++;
1697 CG(increment_lineno) = 0;
1698 }
1699
1700 again:
1701 ZVAL_UNDEF(&zv);
1702 start_lineno = CG(zend_lineno);
1703 retval = lex_scan(&zv);
1704 if (EG(exception)) {
1705 return T_ERROR;
1706 }
1707
1708 switch (retval) {
1709 case T_COMMENT:
1710 case T_DOC_COMMENT:
1711 case T_OPEN_TAG:
1712 case T_WHITESPACE:
1713 goto again;
1714
1715 case T_CLOSE_TAG:
1716 if (LANG_SCNG(yy_text)[LANG_SCNG(yy_leng)-1] != '>') {
1717 CG(increment_lineno) = 1;
1718 }
1719 retval = ';'; /* implicit ; */
1720 break;
1721 case T_OPEN_TAG_WITH_ECHO:
1722 retval = T_ECHO;
1723 break;
1724 }
1725 if (Z_TYPE(zv) != IS_UNDEF) {
1726 elem->ast = zend_ast_create_zval_with_lineno(&zv, 0, start_lineno);
1727 }
1728
1729 return retval;
1730 }
1731 /* }}} */
1732
zend_initialize_class_data(zend_class_entry * ce,zend_bool nullify_handlers)1733 ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
1734 {
1735 zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
1736
1737 ce->refcount = 1;
1738 ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
1739
1740 if (CG(compiler_options) & ZEND_COMPILE_GUARDS) {
1741 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
1742 }
1743
1744 ce->default_properties_table = NULL;
1745 ce->default_static_members_table = NULL;
1746 zend_hash_init_ex(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes, 0);
1747 zend_hash_init_ex(&ce->constants_table, 8, NULL, (persistent_hashes ? zend_destroy_class_constant_internal : NULL), persistent_hashes, 0);
1748 zend_hash_init_ex(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
1749
1750 if (ce->type == ZEND_INTERNAL_CLASS) {
1751 #ifdef ZTS
1752 int n = zend_hash_num_elements(CG(class_table));
1753
1754 if (CG(static_members_table) && n >= CG(last_static_member)) {
1755 /* Support for run-time declaration: dl() */
1756 CG(last_static_member) = n+1;
1757 CG(static_members_table) = realloc(CG(static_members_table), (n+1)*sizeof(zval*));
1758 CG(static_members_table)[n] = NULL;
1759 }
1760 ce->static_members_table = (zval*)(zend_intptr_t)n;
1761 #else
1762 ce->static_members_table = NULL;
1763 #endif
1764 } else {
1765 ce->static_members_table = ce->default_static_members_table;
1766 ce->info.user.doc_comment = NULL;
1767 }
1768
1769 ce->default_properties_count = 0;
1770 ce->default_static_members_count = 0;
1771
1772 if (nullify_handlers) {
1773 ce->constructor = NULL;
1774 ce->destructor = NULL;
1775 ce->clone = NULL;
1776 ce->__get = NULL;
1777 ce->__set = NULL;
1778 ce->__unset = NULL;
1779 ce->__isset = NULL;
1780 ce->__call = NULL;
1781 ce->__callstatic = NULL;
1782 ce->__tostring = NULL;
1783 ce->create_object = NULL;
1784 ce->get_iterator = NULL;
1785 ce->iterator_funcs.funcs = NULL;
1786 ce->interface_gets_implemented = NULL;
1787 ce->get_static_method = NULL;
1788 ce->parent = NULL;
1789 ce->num_interfaces = 0;
1790 ce->interfaces = NULL;
1791 ce->num_traits = 0;
1792 ce->traits = NULL;
1793 ce->trait_aliases = NULL;
1794 ce->trait_precedences = NULL;
1795 ce->serialize = NULL;
1796 ce->unserialize = NULL;
1797 ce->serialize_func = NULL;
1798 ce->unserialize_func = NULL;
1799 ce->__debugInfo = NULL;
1800 if (ce->type == ZEND_INTERNAL_CLASS) {
1801 ce->info.internal.module = NULL;
1802 ce->info.internal.builtin_functions = NULL;
1803 }
1804 }
1805 }
1806 /* }}} */
1807
zend_get_compiled_variable_name(const zend_op_array * op_array,uint32_t var)1808 ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
1809 {
1810 return op_array->vars[EX_VAR_TO_NUM(var)];
1811 }
1812 /* }}} */
1813
zend_ast_append_str(zend_ast * left_ast,zend_ast * right_ast)1814 zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
1815 {
1816 zval *left_zv = zend_ast_get_zval(left_ast);
1817 zend_string *left = Z_STR_P(left_zv);
1818 zend_string *right = zend_ast_get_str(right_ast);
1819
1820 zend_string *result;
1821 size_t left_len = ZSTR_LEN(left);
1822 size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
1823
1824 result = zend_string_extend(left, len, 0);
1825 ZSTR_VAL(result)[left_len] = '\\';
1826 memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
1827 ZSTR_VAL(result)[len] = '\0';
1828 zend_string_release(right);
1829
1830 ZVAL_STR(left_zv, result);
1831 return left_ast;
1832 }
1833 /* }}} */
1834
zend_negate_num_string(zend_ast * ast)1835 zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */
1836 {
1837 zval *zv = zend_ast_get_zval(ast);
1838 if (Z_TYPE_P(zv) == IS_LONG) {
1839 if (Z_LVAL_P(zv) == 0) {
1840 ZVAL_NEW_STR(zv, zend_string_init("-0", sizeof("-0")-1, 0));
1841 } else {
1842 ZEND_ASSERT(Z_LVAL_P(zv) > 0);
1843 Z_LVAL_P(zv) *= -1;
1844 }
1845 } else if (Z_TYPE_P(zv) == IS_STRING) {
1846 size_t orig_len = Z_STRLEN_P(zv);
1847 Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0);
1848 memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1);
1849 Z_STRVAL_P(zv)[0] = '-';
1850 } else {
1851 ZEND_ASSERT(0);
1852 }
1853 return ast;
1854 }
1855 /* }}} */
1856
zend_verify_namespace(void)1857 void zend_verify_namespace(void) /* {{{ */
1858 {
1859 if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
1860 zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
1861 }
1862 }
1863 /* }}} */
1864
1865 /* {{{ zend_dirname
1866 Returns directory name component of path */
zend_dirname(char * path,size_t len)1867 ZEND_API size_t zend_dirname(char *path, size_t len)
1868 {
1869 register char *end = path + len - 1;
1870 unsigned int len_adjust = 0;
1871
1872 #ifdef ZEND_WIN32
1873 /* Note that on Win32 CWD is per drive (heritage from CP/M).
1874 * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
1875 */
1876 if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
1877 /* Skip over the drive spec (if any) so as not to change */
1878 path += 2;
1879 len_adjust += 2;
1880 if (2 == len) {
1881 /* Return "c:" on Win32 for dirname("c:").
1882 * It would be more consistent to return "c:."
1883 * but that would require making the string *longer*.
1884 */
1885 return len;
1886 }
1887 }
1888 #elif defined(NETWARE)
1889 /*
1890 * Find the first occurrence of : from the left
1891 * move the path pointer to the position just after :
1892 * increment the len_adjust to the length of path till colon character(inclusive)
1893 * If there is no character beyond : simple return len
1894 */
1895 char *colonpos = NULL;
1896 colonpos = strchr(path, ':');
1897 if (colonpos != NULL) {
1898 len_adjust = ((colonpos - path) + 1);
1899 path += len_adjust;
1900 if (len_adjust == len) {
1901 return len;
1902 }
1903 }
1904 #endif
1905
1906 if (len == 0) {
1907 /* Illegal use of this function */
1908 return 0;
1909 }
1910
1911 /* Strip trailing slashes */
1912 while (end >= path && IS_SLASH_P(end)) {
1913 end--;
1914 }
1915 if (end < path) {
1916 /* The path only contained slashes */
1917 path[0] = DEFAULT_SLASH;
1918 path[1] = '\0';
1919 return 1 + len_adjust;
1920 }
1921
1922 /* Strip filename */
1923 while (end >= path && !IS_SLASH_P(end)) {
1924 end--;
1925 }
1926 if (end < path) {
1927 /* No slash found, therefore return '.' */
1928 #ifdef NETWARE
1929 if (len_adjust == 0) {
1930 path[0] = '.';
1931 path[1] = '\0';
1932 return 1; /* only one character */
1933 } else {
1934 path[0] = '\0';
1935 return len_adjust;
1936 }
1937 #else
1938 path[0] = '.';
1939 path[1] = '\0';
1940 return 1 + len_adjust;
1941 #endif
1942 }
1943
1944 /* Strip slashes which came before the file name */
1945 while (end >= path && IS_SLASH_P(end)) {
1946 end--;
1947 }
1948 if (end < path) {
1949 path[0] = DEFAULT_SLASH;
1950 path[1] = '\0';
1951 return 1 + len_adjust;
1952 }
1953 *(end+1) = '\0';
1954
1955 return (size_t)(end + 1 - path) + len_adjust;
1956 }
1957 /* }}} */
1958
zend_adjust_for_fetch_type(zend_op * opline,uint32_t type)1959 static void zend_adjust_for_fetch_type(zend_op *opline, uint32_t type) /* {{{ */
1960 {
1961 zend_uchar factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3;
1962
1963 if (opline->opcode == ZEND_FETCH_THIS) {
1964 return;
1965 }
1966
1967 switch (type & BP_VAR_MASK) {
1968 case BP_VAR_R:
1969 return;
1970 case BP_VAR_W:
1971 opline->opcode += 1 * factor;
1972 return;
1973 case BP_VAR_RW:
1974 opline->opcode += 2 * factor;
1975 return;
1976 case BP_VAR_IS:
1977 opline->opcode += 3 * factor;
1978 return;
1979 case BP_VAR_FUNC_ARG:
1980 opline->opcode += 4 * factor;
1981 opline->extended_value |= type >> BP_VAR_SHIFT;
1982 return;
1983 case BP_VAR_UNSET:
1984 opline->opcode += 5 * factor;
1985 return;
1986 EMPTY_SWITCH_DEFAULT_CASE()
1987 }
1988 }
1989 /* }}} */
1990
zend_make_var_result(znode * result,zend_op * opline)1991 static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
1992 {
1993 opline->result_type = IS_VAR;
1994 opline->result.var = get_temporary_variable(CG(active_op_array));
1995 GET_NODE(result, opline->result);
1996 }
1997 /* }}} */
1998
zend_make_tmp_result(znode * result,zend_op * opline)1999 static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
2000 {
2001 opline->result_type = IS_TMP_VAR;
2002 opline->result.var = get_temporary_variable(CG(active_op_array));
2003 GET_NODE(result, opline->result);
2004 }
2005 /* }}} */
2006
zend_find_live_range(zend_op * opline,zend_uchar type,uint32_t var)2007 static void zend_find_live_range(zend_op *opline, zend_uchar type, uint32_t var) /* {{{ */
2008 {
2009 zend_op *def = opline;
2010
2011 while (def != CG(active_op_array)->opcodes) {
2012 def--;
2013 if (def->result_type == type && def->result.var == var) {
2014 if (def->opcode == ZEND_ADD_ARRAY_ELEMENT ||
2015 def->opcode == ZEND_ROPE_ADD) {
2016 /* not a real definition */
2017 continue;
2018 } else if (def->opcode == ZEND_JMPZ_EX ||
2019 def->opcode == ZEND_JMPNZ_EX ||
2020 def->opcode == ZEND_BOOL ||
2021 def->opcode == ZEND_BOOL_NOT) {
2022 /* result IS_BOOL, it does't have to be destroyed */
2023 break;
2024 } else if (def->opcode == ZEND_DECLARE_CLASS ||
2025 def->opcode == ZEND_DECLARE_INHERITED_CLASS ||
2026 def->opcode == ZEND_DECLARE_INHERITED_CLASS_DELAYED ||
2027 def->opcode == ZEND_DECLARE_ANON_CLASS ||
2028 def->opcode == ZEND_DECLARE_ANON_INHERITED_CLASS) {
2029 /* classes don't have to be destroyed */
2030 break;
2031 } else if (def->opcode == ZEND_FAST_CALL) {
2032 /* fast_calls don't have to be destroyed */
2033 break;
2034 } else if (def->opcode == ZEND_NEW) {
2035 /* Objects created via ZEND_NEW are only fully initialized
2036 * after the DO_FCALL (constructor call) */
2037 def = CG(active_op_array)->opcodes + def->op2.opline_num - 1;
2038 if (def + 1 == opline) {
2039 break;
2040 }
2041 }
2042
2043 zend_end_live_range(CG(active_op_array),
2044 zend_start_live_range_ex(CG(active_op_array),
2045 def + 1 - CG(active_op_array)->opcodes),
2046 opline - CG(active_op_array)->opcodes,
2047 ZEND_LIVE_TMPVAR, var);
2048 break;
2049 }
2050 }
2051 }
2052 /* }}} */
2053
zend_is_def_range(zend_op * opline,zend_uchar type,uint32_t var)2054 static zend_always_inline int zend_is_def_range(zend_op *opline, zend_uchar type, uint32_t var) /* {{{ */
2055 {
2056 while (1) {
2057 if (opline->result_type == type && opline->result.var == var) {
2058 return opline->opcode != ZEND_ADD_ARRAY_ELEMENT &&
2059 opline->opcode != ZEND_ROPE_ADD;
2060 } else if (opline->opcode == ZEND_OP_DATA) {
2061 return (opline-1)->result_type == type &&
2062 (opline-1)->result.var == var;
2063 } else if (opline->opcode == ZEND_END_SILENCE ||
2064 opline->opcode == ZEND_NOP ||
2065 opline->opcode == ZEND_EXT_NOP ||
2066 opline->opcode == ZEND_EXT_STMT ||
2067 opline->opcode == ZEND_EXT_FCALL_BEGIN ||
2068 opline->opcode == ZEND_EXT_FCALL_END ||
2069 opline->opcode == ZEND_TICKS) {
2070 opline--;
2071 } else {
2072 return 0;
2073 }
2074 }
2075 }
2076 /* }}} */
2077
zend_check_live_ranges(zend_op * opline)2078 static void zend_check_live_ranges(zend_op *opline) /* {{{ */
2079 {
2080 if ((opline->op1_type & (IS_VAR|IS_TMP_VAR)) &&
2081 !zend_is_def_range(opline - 1, opline->op1_type, opline->op1.var)) {
2082
2083 if (opline->opcode == ZEND_OP_DATA) {
2084 if (!zend_is_def_range(opline - 2, opline->op1_type, opline->op1.var)) {
2085 zend_find_live_range(opline - 1, opline->op1_type, opline->op1.var);
2086 }
2087 } else if (opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
2088 opline->opcode == ZEND_NEW ||
2089 opline->opcode == ZEND_FETCH_CLASS_CONSTANT ||
2090 opline->opcode == ZEND_ADD_INTERFACE ||
2091 opline->opcode == ZEND_ADD_TRAIT ||
2092 opline->opcode == ZEND_BIND_TRAITS ||
2093 opline->opcode == ZEND_VERIFY_ABSTRACT_CLASS) {
2094 /* classes don't have to be destroyed */
2095 } else if (opline->opcode == ZEND_FAST_RET) {
2096 /* fast_calls don't have to be destroyed */
2097 } else if (opline->opcode == ZEND_CASE ||
2098 opline->opcode == ZEND_FE_FETCH_R ||
2099 opline->opcode == ZEND_FE_FETCH_RW ||
2100 opline->opcode == ZEND_FE_FREE ||
2101 opline->opcode == ZEND_ROPE_ADD ||
2102 opline->opcode == ZEND_ROPE_END ||
2103 opline->opcode == ZEND_END_SILENCE ||
2104 opline->opcode == ZEND_FETCH_LIST ||
2105 opline->opcode == ZEND_VERIFY_RETURN_TYPE ||
2106 opline->opcode == ZEND_BIND_LEXICAL) {
2107 /* these opcodes are handled separately */
2108 } else {
2109 zend_find_live_range(opline, opline->op1_type, opline->op1.var);
2110 }
2111 }
2112
2113 if ((opline->op2_type & (IS_VAR|IS_TMP_VAR)) &&
2114 !zend_is_def_range(opline - 1, opline->op2_type, opline->op2.var)) {
2115
2116 if (opline->opcode == ZEND_OP_DATA) {
2117 if (!zend_is_def_range(opline - 2, opline->op2_type, opline->op2.var)) {
2118 zend_find_live_range(opline-1, opline->op2_type, opline->op2.var);
2119 }
2120 } else if (opline->opcode == ZEND_FETCH_STATIC_PROP_R ||
2121 opline->opcode == ZEND_FETCH_STATIC_PROP_W ||
2122 opline->opcode == ZEND_FETCH_STATIC_PROP_RW ||
2123 opline->opcode == ZEND_FETCH_STATIC_PROP_IS ||
2124 opline->opcode == ZEND_FETCH_STATIC_PROP_FUNC_ARG ||
2125 opline->opcode == ZEND_FETCH_STATIC_PROP_UNSET ||
2126 opline->opcode == ZEND_UNSET_STATIC_PROP ||
2127 opline->opcode == ZEND_ISSET_ISEMPTY_STATIC_PROP ||
2128 opline->opcode == ZEND_INSTANCEOF) {
2129 /* classes don't have to be destroyed */
2130 } else {
2131 zend_find_live_range(opline, opline->op2_type, opline->op2.var);
2132 }
2133 }
2134 }
2135 /* }}} */
2136
zend_emit_op(znode * result,zend_uchar opcode,znode * op1,znode * op2)2137 static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2138 {
2139 zend_op *opline = get_next_op(CG(active_op_array));
2140 opline->opcode = opcode;
2141
2142 if (op1 == NULL) {
2143 SET_UNUSED(opline->op1);
2144 } else {
2145 SET_NODE(opline->op1, op1);
2146 }
2147
2148 if (op2 == NULL) {
2149 SET_UNUSED(opline->op2);
2150 } else {
2151 SET_NODE(opline->op2, op2);
2152 }
2153
2154 zend_check_live_ranges(opline);
2155
2156 if (result) {
2157 zend_make_var_result(result, opline);
2158 }
2159 return opline;
2160 }
2161 /* }}} */
2162
zend_emit_op_tmp(znode * result,zend_uchar opcode,znode * op1,znode * op2)2163 static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2164 {
2165 zend_op *opline = get_next_op(CG(active_op_array));
2166 opline->opcode = opcode;
2167
2168 if (op1 == NULL) {
2169 SET_UNUSED(opline->op1);
2170 } else {
2171 SET_NODE(opline->op1, op1);
2172 }
2173
2174 if (op2 == NULL) {
2175 SET_UNUSED(opline->op2);
2176 } else {
2177 SET_NODE(opline->op2, op2);
2178 }
2179
2180 zend_check_live_ranges(opline);
2181
2182 if (result) {
2183 zend_make_tmp_result(result, opline);
2184 }
2185
2186 return opline;
2187 }
2188 /* }}} */
2189
zend_emit_tick(void)2190 static void zend_emit_tick(void) /* {{{ */
2191 {
2192 zend_op *opline;
2193
2194 /* This prevents a double TICK generated by the parser statement of "declare()" */
2195 if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
2196 return;
2197 }
2198
2199 opline = get_next_op(CG(active_op_array));
2200
2201 opline->opcode = ZEND_TICKS;
2202 SET_UNUSED(opline->op1);
2203 SET_UNUSED(opline->op2);
2204 opline->extended_value = FC(declarables).ticks;
2205 }
2206 /* }}} */
2207
zend_emit_op_data(znode * value)2208 static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
2209 {
2210 return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
2211 }
2212 /* }}} */
2213
zend_emit_jump(uint32_t opnum_target)2214 static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
2215 {
2216 uint32_t opnum = get_next_op_number(CG(active_op_array));
2217 zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
2218 opline->op1.opline_num = opnum_target;
2219 return opnum;
2220 }
2221 /* }}} */
2222
zend_is_smart_branch(zend_op * opline)2223 ZEND_API int zend_is_smart_branch(zend_op *opline) /* {{{ */
2224 {
2225 switch (opline->opcode) {
2226 case ZEND_IS_IDENTICAL:
2227 case ZEND_IS_NOT_IDENTICAL:
2228 case ZEND_IS_EQUAL:
2229 case ZEND_IS_NOT_EQUAL:
2230 case ZEND_IS_SMALLER:
2231 case ZEND_IS_SMALLER_OR_EQUAL:
2232 case ZEND_CASE:
2233 case ZEND_ISSET_ISEMPTY_VAR:
2234 case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2235 case ZEND_ISSET_ISEMPTY_PROP_OBJ:
2236 case ZEND_ISSET_ISEMPTY_STATIC_PROP:
2237 case ZEND_INSTANCEOF:
2238 case ZEND_TYPE_CHECK:
2239 case ZEND_DEFINED:
2240 return 1;
2241 default:
2242 return 0;
2243 }
2244 }
2245 /* }}} */
2246
zend_emit_cond_jump(zend_uchar opcode,znode * cond,uint32_t opnum_target)2247 static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
2248 {
2249 uint32_t opnum = get_next_op_number(CG(active_op_array));
2250 zend_op *opline;
2251
2252 if ((cond->op_type & (IS_CV|IS_CONST))
2253 && opnum > 0
2254 && zend_is_smart_branch(CG(active_op_array)->opcodes + opnum - 1)) {
2255 /* emit extra NOP to avoid incorrect SMART_BRANCH in very rare cases */
2256 zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
2257 opnum = get_next_op_number(CG(active_op_array));
2258 }
2259 opline = zend_emit_op(NULL, opcode, cond, NULL);
2260 opline->op2.opline_num = opnum_target;
2261 return opnum;
2262 }
2263 /* }}} */
2264
zend_update_jump_target(uint32_t opnum_jump,uint32_t opnum_target)2265 static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
2266 {
2267 zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
2268 switch (opline->opcode) {
2269 case ZEND_JMP:
2270 opline->op1.opline_num = opnum_target;
2271 break;
2272 case ZEND_JMPZ:
2273 case ZEND_JMPNZ:
2274 case ZEND_JMPZ_EX:
2275 case ZEND_JMPNZ_EX:
2276 case ZEND_JMP_SET:
2277 opline->op2.opline_num = opnum_target;
2278 break;
2279 EMPTY_SWITCH_DEFAULT_CASE()
2280 }
2281 }
2282 /* }}} */
2283
zend_update_jump_target_to_next(uint32_t opnum_jump)2284 static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
2285 {
2286 zend_update_jump_target(opnum_jump, get_next_op_number(CG(active_op_array)));
2287 }
2288 /* }}} */
2289
zend_delayed_emit_op(znode * result,zend_uchar opcode,znode * op1,znode * op2)2290 static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
2291 {
2292 zend_op tmp_opline;
2293 init_op(&tmp_opline);
2294 tmp_opline.opcode = opcode;
2295 if (op1 == NULL) {
2296 SET_UNUSED(tmp_opline.op1);
2297 } else {
2298 SET_NODE(tmp_opline.op1, op1);
2299 }
2300 if (op2 == NULL) {
2301 SET_UNUSED(tmp_opline.op2);
2302 } else {
2303 SET_NODE(tmp_opline.op2, op2);
2304 }
2305 if (result) {
2306 zend_make_var_result(result, &tmp_opline);
2307 }
2308
2309 zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
2310 return zend_stack_top(&CG(delayed_oplines_stack));
2311 }
2312 /* }}} */
2313
zend_delayed_compile_begin(void)2314 static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
2315 {
2316 return zend_stack_count(&CG(delayed_oplines_stack));
2317 }
2318 /* }}} */
2319
zend_delayed_compile_end(uint32_t offset)2320 static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
2321 {
2322 zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
2323 uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
2324
2325 ZEND_ASSERT(count >= offset);
2326 for (i = offset; i < count; ++i) {
2327 opline = get_next_op(CG(active_op_array));
2328 memcpy(opline, &oplines[i], sizeof(zend_op));
2329 zend_check_live_ranges(opline);
2330 }
2331 CG(delayed_oplines_stack).top = offset;
2332 return opline;
2333 }
2334 /* }}} */
2335
zend_emit_return_type_check(znode * expr,zend_arg_info * return_info,zend_bool implicit)2336 static void zend_emit_return_type_check(
2337 znode *expr, zend_arg_info *return_info, zend_bool implicit) /* {{{ */
2338 {
2339 /* `return ...;` is illegal in a void function (but `return;` isn't) */
2340 if (return_info->type_hint == IS_VOID) {
2341 if (expr) {
2342 if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
2343 zend_error_noreturn(E_COMPILE_ERROR,
2344 "A void function must not return a value "
2345 "(did you mean \"return;\" instead of \"return null;\"?)");
2346 } else {
2347 zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
2348 }
2349 }
2350 /* we don't need run-time check */
2351 return;
2352 }
2353
2354 if (return_info->type_hint != IS_UNDEF) {
2355 zend_op *opline;
2356
2357 if (!expr && !implicit) {
2358 if (return_info->allow_null) {
2359 zend_error_noreturn(E_COMPILE_ERROR,
2360 "A function with return type must return a value "
2361 "(did you mean \"return null;\" instead of \"return;\"?)");
2362 } else {
2363 zend_error_noreturn(E_COMPILE_ERROR,
2364 "A function with return type must return a value");
2365 }
2366 }
2367
2368 if (expr && expr->op_type == IS_CONST) {
2369 if ((return_info->type_hint == Z_TYPE(expr->u.constant))
2370 ||((return_info->type_hint == _IS_BOOL)
2371 && (Z_TYPE(expr->u.constant) == IS_FALSE
2372 || Z_TYPE(expr->u.constant) == IS_TRUE))
2373 || (return_info->allow_null
2374 && Z_TYPE(expr->u.constant) == IS_NULL)) {
2375 /* we don't need run-time check */
2376 return;
2377 }
2378 }
2379
2380 opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
2381 if (expr && expr->op_type == IS_CONST) {
2382 opline->result_type = expr->op_type = IS_TMP_VAR;
2383 opline->result.var = expr->u.op.var = get_temporary_variable(CG(active_op_array));
2384 }
2385 if (return_info->class_name) {
2386 opline->op2.num = CG(active_op_array)->cache_size;
2387 CG(active_op_array)->cache_size += sizeof(void*);
2388 } else {
2389 opline->op2.num = -1;
2390 }
2391 }
2392 }
2393 /* }}} */
2394
zend_emit_final_return(int return_one)2395 void zend_emit_final_return(int return_one) /* {{{ */
2396 {
2397 znode zn;
2398 zend_op *ret;
2399 zend_bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2400
2401 if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE
2402 && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) {
2403 zend_emit_return_type_check(NULL, CG(active_op_array)->arg_info - 1, 1);
2404 }
2405
2406 zn.op_type = IS_CONST;
2407 if (return_one) {
2408 ZVAL_LONG(&zn.u.constant, 1);
2409 } else {
2410 ZVAL_NULL(&zn.u.constant);
2411 }
2412
2413 ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2414 ret->extended_value = -1;
2415 }
2416 /* }}} */
2417
zend_is_variable(zend_ast * ast)2418 static inline zend_bool zend_is_variable(zend_ast *ast) /* {{{ */
2419 {
2420 return ast->kind == ZEND_AST_VAR || ast->kind == ZEND_AST_DIM
2421 || ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_STATIC_PROP
2422 || ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_METHOD_CALL
2423 || ast->kind == ZEND_AST_STATIC_CALL;
2424 }
2425 /* }}} */
2426
zend_is_call(zend_ast * ast)2427 static inline zend_bool zend_is_call(zend_ast *ast) /* {{{ */
2428 {
2429 return ast->kind == ZEND_AST_CALL
2430 || ast->kind == ZEND_AST_METHOD_CALL
2431 || ast->kind == ZEND_AST_STATIC_CALL;
2432 }
2433 /* }}} */
2434
zend_is_unticked_stmt(zend_ast * ast)2435 static inline zend_bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
2436 {
2437 return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL
2438 || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_DECL
2439 || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD;
2440 }
2441 /* }}} */
2442
zend_can_write_to_variable(zend_ast * ast)2443 static inline zend_bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
2444 {
2445 while (ast->kind == ZEND_AST_DIM || ast->kind == ZEND_AST_PROP) {
2446 ast = ast->child[0];
2447 }
2448
2449 return zend_is_variable(ast);
2450 }
2451 /* }}} */
2452
zend_is_const_default_class_ref(zend_ast * name_ast)2453 static inline zend_bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2454 {
2455 if (name_ast->kind != ZEND_AST_ZVAL) {
2456 return 0;
2457 }
2458
2459 return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2460 }
2461 /* }}} */
2462
zend_handle_numeric_op(znode * node)2463 static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2464 {
2465 if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2466 zend_ulong index;
2467
2468 if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2469 zval_ptr_dtor(&node->u.constant);
2470 ZVAL_LONG(&node->u.constant, index);
2471 }
2472 }
2473 }
2474 /* }}} */
2475
zend_set_class_name_op1(zend_op * opline,znode * class_node)2476 static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2477 {
2478 if (class_node->op_type == IS_CONST) {
2479 opline->op1_type = IS_CONST;
2480 opline->op1.constant = zend_add_class_name_literal(
2481 CG(active_op_array), Z_STR(class_node->u.constant));
2482 } else {
2483 SET_NODE(opline->op1, class_node);
2484 }
2485 }
2486 /* }}} */
2487
zend_compile_class_ref(znode * result,zend_ast * name_ast,int throw_exception)2488 static zend_op *zend_compile_class_ref(znode *result, zend_ast *name_ast, int throw_exception) /* {{{ */
2489 {
2490 zend_op *opline;
2491 znode name_node;
2492 zend_compile_expr(&name_node, name_ast);
2493
2494 if (name_node.op_type == IS_CONST) {
2495 zend_string *name;
2496 uint32_t fetch_type;
2497
2498 if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2499 zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2500 }
2501
2502 name = Z_STR(name_node.u.constant);
2503 fetch_type = zend_get_class_fetch_type(name);
2504
2505 opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, NULL);
2506 opline->extended_value = fetch_type | (throw_exception ? ZEND_FETCH_CLASS_EXCEPTION : 0);
2507
2508 if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2509 uint32_t type = name_ast->kind == ZEND_AST_ZVAL ? name_ast->attr : ZEND_NAME_FQ;
2510 opline->op2_type = IS_CONST;
2511 opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
2512 zend_resolve_class_name(name, type));
2513 } else {
2514 zend_ensure_valid_class_fetch_type(fetch_type);
2515 }
2516
2517 zend_string_release(name);
2518 } else {
2519 opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2520 opline->extended_value = ZEND_FETCH_CLASS_DEFAULT | (throw_exception ? ZEND_FETCH_CLASS_EXCEPTION : 0);
2521 }
2522
2523 return opline;
2524 }
2525 /* }}} */
2526
zend_compile_class_ref_ex(znode * result,zend_ast * name_ast,uint32_t fetch_flags)2527 static void zend_compile_class_ref_ex(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */
2528 {
2529 uint32_t fetch_type;
2530
2531 if (name_ast->kind != ZEND_AST_ZVAL) {
2532 znode name_node;
2533
2534 zend_compile_expr(&name_node, name_ast);
2535
2536 if (name_node.op_type == IS_CONST) {
2537 zend_string *name;
2538
2539 if (Z_TYPE(name_node.u.constant) != IS_STRING) {
2540 zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
2541 }
2542
2543 name = Z_STR(name_node.u.constant);
2544 fetch_type = zend_get_class_fetch_type(name);
2545
2546 if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2547 result->op_type = IS_CONST;
2548 ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ));
2549 } else {
2550 zend_ensure_valid_class_fetch_type(fetch_type);
2551 result->op_type = IS_UNUSED;
2552 result->u.op.num = fetch_type | fetch_flags;
2553 }
2554
2555 zend_string_release(name);
2556 } else {
2557 zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2558 opline->extended_value = ZEND_FETCH_CLASS_DEFAULT | fetch_flags;
2559 }
2560 return;
2561 }
2562
2563 /* Fully qualified names are always default refs */
2564 if (name_ast->attr == ZEND_NAME_FQ) {
2565 result->op_type = IS_CONST;
2566 ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2567 return;
2568 }
2569
2570 fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
2571 if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
2572 result->op_type = IS_CONST;
2573 ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
2574 } else {
2575 zend_ensure_valid_class_fetch_type(fetch_type);
2576 result->op_type = IS_UNUSED;
2577 result->u.op.num = fetch_type | fetch_flags;
2578 }
2579 }
2580 /* }}} */
2581
zend_try_compile_cv(znode * result,zend_ast * ast)2582 static int zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
2583 {
2584 zend_ast *name_ast = ast->child[0];
2585 if (name_ast->kind == ZEND_AST_ZVAL) {
2586 zend_string *name = zval_get_string(zend_ast_get_zval(name_ast));
2587
2588 if (zend_is_auto_global(name)) {
2589 zend_string_release(name);
2590 return FAILURE;
2591 }
2592
2593 result->op_type = IS_CV;
2594 result->u.op.var = lookup_cv(CG(active_op_array), name);
2595
2596 /* lookup_cv may be using another zend_string instance */
2597 name = CG(active_op_array)->vars[EX_VAR_TO_NUM(result->u.op.var)];
2598
2599 return SUCCESS;
2600 }
2601
2602 return FAILURE;
2603 }
2604 /* }}} */
2605
zend_compile_simple_var_no_cv(znode * result,zend_ast * ast,uint32_t type,int delayed)2606 static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2607 {
2608 zend_ast *name_ast = ast->child[0];
2609 znode name_node;
2610 zend_op *opline;
2611
2612 zend_compile_expr(&name_node, name_ast);
2613 if (name_node.op_type == IS_CONST) {
2614 convert_to_string(&name_node.u.constant);
2615 }
2616
2617 if (delayed) {
2618 opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2619 } else {
2620 opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2621 }
2622
2623 if (name_node.op_type == IS_CONST &&
2624 zend_is_auto_global(Z_STR(name_node.u.constant))) {
2625
2626 opline->extended_value = ZEND_FETCH_GLOBAL;
2627 } else {
2628 opline->extended_value = ZEND_FETCH_LOCAL;
2629 }
2630
2631 return opline;
2632 }
2633 /* }}} */
2634
is_this_fetch(zend_ast * ast)2635 static zend_bool is_this_fetch(zend_ast *ast) /* {{{ */
2636 {
2637 if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2638 zval *name = zend_ast_get_zval(ast->child[0]);
2639 return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
2640 }
2641
2642 return 0;
2643 }
2644 /* }}} */
2645
zend_compile_simple_var(znode * result,zend_ast * ast,uint32_t type,int delayed)2646 static void zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2647 {
2648 zend_op *opline;
2649
2650 if (is_this_fetch(ast)) {
2651 opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL);
2652 zend_adjust_for_fetch_type(opline, type);
2653 } else if (zend_try_compile_cv(result, ast) == FAILURE) {
2654 zend_op *opline = zend_compile_simple_var_no_cv(result, ast, type, delayed);
2655 zend_adjust_for_fetch_type(opline, type);
2656 }
2657 }
2658 /* }}} */
2659
zend_separate_if_call_and_write(znode * node,zend_ast * ast,uint32_t type)2660 static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
2661 {
2662 if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
2663 if (node->op_type == IS_VAR) {
2664 zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
2665 opline->result_type = IS_VAR;
2666 opline->result.var = opline->op1.var;
2667 } else {
2668 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
2669 }
2670 }
2671 }
2672 /* }}} */
2673
2674 void zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type);
2675 void zend_compile_assign(znode *result, zend_ast *ast);
2676 static void zend_compile_list_assign(znode *result, zend_ast *ast, znode *expr_node, zend_bool old_style);
2677
zend_emit_assign_znode(zend_ast * var_ast,znode * value_node)2678 static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
2679 {
2680 znode dummy_node;
2681 if (var_ast->kind == ZEND_AST_ARRAY) {
2682 zend_compile_list_assign(&dummy_node, var_ast, value_node, var_ast->attr);
2683 } else {
2684 zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
2685 zend_ast_create_znode(value_node));
2686 zend_compile_assign(&dummy_node, assign_ast);
2687 }
2688 zend_do_free(&dummy_node);
2689 }
2690 /* }}} */
2691
zend_delayed_compile_dim(znode * result,zend_ast * ast,uint32_t type)2692 static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2693 {
2694 zend_ast *var_ast = ast->child[0];
2695 zend_ast *dim_ast = ast->child[1];
2696
2697 znode var_node, dim_node;
2698
2699 zend_delayed_compile_var(&var_node, var_ast, type);
2700 zend_separate_if_call_and_write(&var_node, var_ast, type);
2701
2702 if (dim_ast == NULL) {
2703 if (type == BP_VAR_R || type == BP_VAR_IS) {
2704 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
2705 }
2706 if (type == BP_VAR_UNSET) {
2707 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
2708 }
2709 dim_node.op_type = IS_UNUSED;
2710 } else {
2711 zend_compile_expr(&dim_node, dim_ast);
2712 zend_handle_numeric_op(&dim_node);
2713 }
2714
2715 return zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
2716 }
2717 /* }}} */
2718
zend_compile_dim_common(znode * result,zend_ast * ast,uint32_t type)2719 static inline zend_op *zend_compile_dim_common(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2720 {
2721 uint32_t offset = zend_delayed_compile_begin();
2722 zend_delayed_compile_dim(result, ast, type);
2723 return zend_delayed_compile_end(offset);
2724 }
2725 /* }}} */
2726
zend_compile_dim(znode * result,zend_ast * ast,uint32_t type)2727 void zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2728 {
2729 zend_op *opline = zend_compile_dim_common(result, ast, type);
2730 zend_adjust_for_fetch_type(opline, type);
2731 }
2732 /* }}} */
2733
zend_delayed_compile_prop(znode * result,zend_ast * ast,uint32_t type)2734 static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2735 {
2736 zend_ast *obj_ast = ast->child[0];
2737 zend_ast *prop_ast = ast->child[1];
2738
2739 znode obj_node, prop_node;
2740 zend_op *opline;
2741
2742 if (is_this_fetch(obj_ast)) {
2743 obj_node.op_type = IS_UNUSED;
2744 } else {
2745 zend_delayed_compile_var(&obj_node, obj_ast, type);
2746 zend_separate_if_call_and_write(&obj_node, obj_ast, type);
2747 }
2748 zend_compile_expr(&prop_node, prop_ast);
2749
2750 opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
2751 if (opline->op2_type == IS_CONST) {
2752 convert_to_string(CT_CONSTANT(opline->op2));
2753 zend_alloc_polymorphic_cache_slot(opline->op2.constant);
2754 }
2755
2756 return opline;
2757 }
2758 /* }}} */
2759
zend_compile_prop_common(znode * result,zend_ast * ast,uint32_t type)2760 static zend_op *zend_compile_prop_common(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2761 {
2762 uint32_t offset = zend_delayed_compile_begin();
2763 zend_delayed_compile_prop(result, ast, type);
2764 return zend_delayed_compile_end(offset);
2765 }
2766 /* }}} */
2767
zend_compile_prop(znode * result,zend_ast * ast,uint32_t type)2768 void zend_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2769 {
2770 zend_op *opline = zend_compile_prop_common(result, ast, type);
2771 zend_adjust_for_fetch_type(opline, type);
2772 }
2773 /* }}} */
2774
zend_compile_static_prop_common(znode * result,zend_ast * ast,uint32_t type,int delayed)2775 zend_op *zend_compile_static_prop_common(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2776 {
2777 zend_ast *class_ast = ast->child[0];
2778 zend_ast *prop_ast = ast->child[1];
2779
2780 znode class_node, prop_node;
2781 zend_op *opline;
2782
2783 zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
2784
2785 zend_compile_expr(&prop_node, prop_ast);
2786
2787 if (delayed) {
2788 opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
2789 } else {
2790 opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL);
2791 }
2792 if (opline->op1_type == IS_CONST) {
2793 convert_to_string(CT_CONSTANT(opline->op1));
2794 zend_alloc_polymorphic_cache_slot(opline->op1.constant);
2795 }
2796 if (class_node.op_type == IS_CONST) {
2797 opline->op2_type = IS_CONST;
2798 opline->op2.constant = zend_add_class_name_literal(
2799 CG(active_op_array), Z_STR(class_node.u.constant));
2800 } else {
2801 SET_NODE(opline->op2, &class_node);
2802 }
2803
2804 return opline;
2805 }
2806 /* }}} */
2807
zend_compile_static_prop(znode * result,zend_ast * ast,uint32_t type,int delayed)2808 void zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2809 {
2810 zend_op *opline = zend_compile_static_prop_common(result, ast, type, delayed);
2811 zend_adjust_for_fetch_type(opline, type);
2812 }
2813 /* }}} */
2814
zend_verify_list_assign_target(zend_ast * var_ast,zend_bool old_style)2815 static void zend_verify_list_assign_target(zend_ast *var_ast, zend_bool old_style) /* {{{ */ {
2816 if (var_ast->kind == ZEND_AST_ARRAY) {
2817 if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) {
2818 zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead");
2819 }
2820 if (old_style != var_ast->attr) {
2821 zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()");
2822 }
2823 } else if (!zend_can_write_to_variable(var_ast)) {
2824 zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values");
2825 }
2826 }
2827 /* }}} */
2828
zend_compile_list_assign(znode * result,zend_ast * ast,znode * expr_node,zend_bool old_style)2829 static void zend_compile_list_assign(
2830 znode *result, zend_ast *ast, znode *expr_node, zend_bool old_style) /* {{{ */
2831 {
2832 zend_ast_list *list = zend_ast_get_list(ast);
2833 uint32_t i;
2834 zend_bool has_elems = 0;
2835 zend_bool is_keyed =
2836 list->children > 0 && list->child[0] != NULL && list->child[0]->child[1] != NULL;
2837
2838 for (i = 0; i < list->children; ++i) {
2839 zend_ast *elem_ast = list->child[i];
2840 zend_ast *var_ast, *key_ast;
2841 znode fetch_result, dim_node;
2842
2843 if (elem_ast == NULL) {
2844 if (is_keyed) {
2845 zend_error(E_COMPILE_ERROR,
2846 "Cannot use empty array entries in keyed array assignment");
2847 } else {
2848 continue;
2849 }
2850 }
2851
2852 if (elem_ast->attr) {
2853 zend_error(E_COMPILE_ERROR, "[] and list() assignments cannot be by reference");
2854 }
2855
2856 var_ast = elem_ast->child[0];
2857 key_ast = elem_ast->child[1];
2858 has_elems = 1;
2859
2860 if (is_keyed) {
2861 if (key_ast == NULL) {
2862 zend_error(E_COMPILE_ERROR,
2863 "Cannot mix keyed and unkeyed array entries in assignments");
2864 }
2865
2866 zend_compile_expr(&dim_node, key_ast);
2867 } else {
2868 if (key_ast != NULL) {
2869 zend_error(E_COMPILE_ERROR,
2870 "Cannot mix keyed and unkeyed array entries in assignments");
2871 }
2872
2873 dim_node.op_type = IS_CONST;
2874 ZVAL_LONG(&dim_node.u.constant, i);
2875 }
2876
2877 if (expr_node->op_type == IS_CONST) {
2878 Z_TRY_ADDREF(expr_node->u.constant);
2879 }
2880
2881 zend_verify_list_assign_target(var_ast, old_style);
2882
2883 zend_emit_op(&fetch_result, ZEND_FETCH_LIST, expr_node, &dim_node);
2884 zend_emit_assign_znode(var_ast, &fetch_result);
2885 }
2886
2887 if (has_elems == 0) {
2888 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
2889 }
2890
2891 *result = *expr_node;
2892 }
2893 /* }}} */
2894
zend_ensure_writable_variable(const zend_ast * ast)2895 static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
2896 {
2897 if (ast->kind == ZEND_AST_CALL) {
2898 zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
2899 }
2900 if (ast->kind == ZEND_AST_METHOD_CALL || ast->kind == ZEND_AST_STATIC_CALL) {
2901 zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
2902 }
2903 }
2904 /* }}} */
2905
2906 /* Detects $a... = $a pattern */
zend_is_assign_to_self(zend_ast * var_ast,zend_ast * expr_ast)2907 zend_bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
2908 {
2909 if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
2910 return 0;
2911 }
2912
2913 while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
2914 var_ast = var_ast->child[0];
2915 }
2916
2917 if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
2918 return 0;
2919 }
2920
2921 {
2922 zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
2923 zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
2924 zend_bool result = zend_string_equals(name1, name2);
2925 zend_string_release(name1);
2926 zend_string_release(name2);
2927 return result;
2928 }
2929 }
2930 /* }}} */
2931
2932 /* Detects if list($a, $b, $c) contains variable with given name */
zend_list_has_assign_to(zend_ast * list_ast,zend_string * name)2933 zend_bool zend_list_has_assign_to(zend_ast *list_ast, zend_string *name) /* {{{ */
2934 {
2935 zend_ast_list *list = zend_ast_get_list(list_ast);
2936 uint32_t i;
2937 for (i = 0; i < list->children; i++) {
2938 zend_ast *elem_ast = list->child[i];
2939 zend_ast *var_ast;
2940
2941 if (!elem_ast) {
2942 continue;
2943 }
2944 var_ast = elem_ast->child[0];
2945
2946 /* Recursively check nested list()s */
2947 if (var_ast->kind == ZEND_AST_ARRAY && zend_list_has_assign_to(var_ast, name)) {
2948 return 1;
2949 }
2950
2951 if (var_ast->kind == ZEND_AST_VAR && var_ast->child[0]->kind == ZEND_AST_ZVAL) {
2952 zend_string *var_name = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
2953 zend_bool result = zend_string_equals(var_name, name);
2954 zend_string_release(var_name);
2955 if (result) {
2956 return 1;
2957 }
2958 }
2959 }
2960
2961 return 0;
2962 }
2963 /* }}} */
2964
2965 /* Detects patterns like list($a, $b, $c) = $a */
zend_list_has_assign_to_self(zend_ast * list_ast,zend_ast * expr_ast)2966 zend_bool zend_list_has_assign_to_self(zend_ast *list_ast, zend_ast *expr_ast) /* {{{ */
2967 {
2968 /* Only check simple variables on the RHS, as only CVs cause issues with this. */
2969 if (expr_ast->kind == ZEND_AST_VAR && expr_ast->child[0]->kind == ZEND_AST_ZVAL) {
2970 zend_string *name = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
2971 zend_bool result = zend_list_has_assign_to(list_ast, name);
2972 zend_string_release(name);
2973 return result;
2974 }
2975 return 0;
2976 }
2977 /* }}} */
2978
zend_compile_assign(znode * result,zend_ast * ast)2979 void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
2980 {
2981 zend_ast *var_ast = ast->child[0];
2982 zend_ast *expr_ast = ast->child[1];
2983
2984 znode var_node, expr_node;
2985 zend_op *opline;
2986 uint32_t offset;
2987
2988 if (is_this_fetch(var_ast)) {
2989 zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
2990 }
2991
2992 zend_ensure_writable_variable(var_ast);
2993
2994 switch (var_ast->kind) {
2995 case ZEND_AST_VAR:
2996 case ZEND_AST_STATIC_PROP:
2997 offset = zend_delayed_compile_begin();
2998 zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W);
2999 zend_compile_expr(&expr_node, expr_ast);
3000 zend_delayed_compile_end(offset);
3001 zend_emit_op(result, ZEND_ASSIGN, &var_node, &expr_node);
3002 return;
3003 case ZEND_AST_DIM:
3004 offset = zend_delayed_compile_begin();
3005 zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
3006
3007 if (zend_is_assign_to_self(var_ast, expr_ast)
3008 && !is_this_fetch(expr_ast)) {
3009 /* $a[0] = $a should evaluate the right $a first */
3010 zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
3011 } else {
3012 zend_compile_expr(&expr_node, expr_ast);
3013 }
3014
3015 opline = zend_delayed_compile_end(offset);
3016 opline->opcode = ZEND_ASSIGN_DIM;
3017
3018 opline = zend_emit_op_data(&expr_node);
3019 return;
3020 case ZEND_AST_PROP:
3021 offset = zend_delayed_compile_begin();
3022 zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
3023 zend_compile_expr(&expr_node, expr_ast);
3024
3025 opline = zend_delayed_compile_end(offset);
3026 opline->opcode = ZEND_ASSIGN_OBJ;
3027
3028 zend_emit_op_data(&expr_node);
3029 return;
3030 case ZEND_AST_ARRAY:
3031 if (zend_list_has_assign_to_self(var_ast, expr_ast)) {
3032 /* list($a, $b) = $a should evaluate the right $a first */
3033 zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
3034 } else {
3035 zend_compile_expr(&expr_node, expr_ast);
3036 }
3037
3038 zend_compile_list_assign(result, var_ast, &expr_node, var_ast->attr);
3039 return;
3040 EMPTY_SWITCH_DEFAULT_CASE();
3041 }
3042 }
3043 /* }}} */
3044
zend_compile_assign_ref(znode * result,zend_ast * ast)3045 void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
3046 {
3047 zend_ast *target_ast = ast->child[0];
3048 zend_ast *source_ast = ast->child[1];
3049
3050 znode target_node, source_node;
3051 zend_op *opline;
3052 uint32_t offset;
3053
3054 if (is_this_fetch(target_ast)) {
3055 zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
3056 }
3057 zend_ensure_writable_variable(target_ast);
3058
3059 offset = zend_delayed_compile_begin();
3060 zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W);
3061 zend_compile_var(&source_node, source_ast, BP_VAR_W);
3062
3063 if ((target_ast->kind != ZEND_AST_VAR
3064 || target_ast->child[0]->kind != ZEND_AST_ZVAL)
3065 && source_node.op_type != IS_CV) {
3066 /* Both LHS and RHS expressions may modify the same data structure,
3067 * and the modification during RHS evaluation may dangle the pointer
3068 * to the result of the LHS evaluation.
3069 * Use MAKE_REF instruction to replace direct pointer with REFERENCE.
3070 * See: Bug #71539
3071 */
3072 zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL);
3073 }
3074
3075 zend_delayed_compile_end(offset);
3076
3077 if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
3078 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
3079 }
3080
3081 opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
3082
3083 if (zend_is_call(source_ast)) {
3084 opline->extended_value = ZEND_RETURNS_FUNCTION;
3085 }
3086 }
3087 /* }}} */
3088
zend_emit_assign_ref_znode(zend_ast * var_ast,znode * value_node)3089 static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
3090 {
3091 zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
3092 zend_ast_create_znode(value_node));
3093 zend_compile_assign_ref(NULL, assign_ast);
3094 }
3095 /* }}} */
3096
zend_compile_compound_assign(znode * result,zend_ast * ast)3097 void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
3098 {
3099 zend_ast *var_ast = ast->child[0];
3100 zend_ast *expr_ast = ast->child[1];
3101 uint32_t opcode = ast->attr;
3102
3103 znode var_node, expr_node;
3104 zend_op *opline;
3105 uint32_t offset;
3106
3107 zend_ensure_writable_variable(var_ast);
3108
3109 switch (var_ast->kind) {
3110 case ZEND_AST_VAR:
3111 case ZEND_AST_STATIC_PROP:
3112 offset = zend_delayed_compile_begin();
3113 zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW);
3114 zend_compile_expr(&expr_node, expr_ast);
3115 zend_delayed_compile_end(offset);
3116 zend_emit_op(result, opcode, &var_node, &expr_node);
3117 return;
3118 case ZEND_AST_DIM:
3119 offset = zend_delayed_compile_begin();
3120 zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
3121 zend_compile_expr(&expr_node, expr_ast);
3122
3123 opline = zend_delayed_compile_end(offset);
3124 opline->opcode = opcode;
3125 opline->extended_value = ZEND_ASSIGN_DIM;
3126
3127 opline = zend_emit_op_data(&expr_node);
3128 return;
3129 case ZEND_AST_PROP:
3130 offset = zend_delayed_compile_begin();
3131 zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
3132 zend_compile_expr(&expr_node, expr_ast);
3133
3134 opline = zend_delayed_compile_end(offset);
3135 opline->opcode = opcode;
3136 opline->extended_value = ZEND_ASSIGN_OBJ;
3137
3138 zend_emit_op_data(&expr_node);
3139 return;
3140 EMPTY_SWITCH_DEFAULT_CASE()
3141 }
3142 }
3143 /* }}} */
3144
zend_compile_args(zend_ast * ast,zend_function * fbc)3145 uint32_t zend_compile_args(zend_ast *ast, zend_function *fbc) /* {{{ */
3146 {
3147 zend_ast_list *args = zend_ast_get_list(ast);
3148 uint32_t i;
3149 zend_bool uses_arg_unpack = 0;
3150 uint32_t arg_count = 0; /* number of arguments not including unpacks */
3151
3152 for (i = 0; i < args->children; ++i) {
3153 zend_ast *arg = args->child[i];
3154 uint32_t arg_num = i + 1;
3155
3156 znode arg_node;
3157 zend_op *opline;
3158 zend_uchar opcode;
3159
3160 if (arg->kind == ZEND_AST_UNPACK) {
3161 uses_arg_unpack = 1;
3162 fbc = NULL;
3163
3164 zend_compile_expr(&arg_node, arg->child[0]);
3165 opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
3166 opline->op2.num = arg_count;
3167 opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_count);
3168 continue;
3169 }
3170
3171 if (uses_arg_unpack) {
3172 zend_error_noreturn(E_COMPILE_ERROR,
3173 "Cannot use positional argument after argument unpacking");
3174 }
3175
3176 arg_count++;
3177 if (zend_is_variable(arg)) {
3178 if (zend_is_call(arg)) {
3179 zend_compile_var(&arg_node, arg, BP_VAR_R);
3180 if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
3181 /* Function call was converted into builtin instruction */
3182 opcode = ZEND_SEND_VAL;
3183 } else {
3184 if (fbc) {
3185 if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3186 opcode = ZEND_SEND_VAR_NO_REF;
3187 } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3188 opcode = ZEND_SEND_VAL;
3189 } else {
3190 opcode = ZEND_SEND_VAR;
3191 }
3192 } else {
3193 opcode = ZEND_SEND_VAR_NO_REF_EX;
3194 }
3195 }
3196 } else if (fbc) {
3197 if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3198 zend_compile_var(&arg_node, arg, BP_VAR_W);
3199 opcode = ZEND_SEND_REF;
3200 } else {
3201 zend_compile_var(&arg_node, arg, BP_VAR_R);
3202 opcode = ZEND_SEND_VAR;
3203 }
3204 } else {
3205 zend_compile_var(&arg_node, arg,
3206 BP_VAR_FUNC_ARG | (arg_num << BP_VAR_SHIFT));
3207 opcode = ZEND_SEND_VAR_EX;
3208 }
3209 } else {
3210 zend_compile_expr(&arg_node, arg);
3211 if (arg_node.op_type == IS_VAR) {
3212 /* pass ++$a or something similar */
3213 if (fbc) {
3214 if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3215 opcode = ZEND_SEND_VAR_NO_REF;
3216 } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
3217 opcode = ZEND_SEND_VAL;
3218 } else {
3219 opcode = ZEND_SEND_VAR;
3220 }
3221 } else {
3222 opcode = ZEND_SEND_VAR_NO_REF_EX;
3223 }
3224 } else if (arg_node.op_type == IS_CV) {
3225 if (fbc) {
3226 if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
3227 opcode = ZEND_SEND_REF;
3228 } else {
3229 opcode = ZEND_SEND_VAR;
3230 }
3231 } else {
3232 opcode = ZEND_SEND_VAR_EX;
3233 }
3234 } else {
3235 if (fbc) {
3236 opcode = ZEND_SEND_VAL;
3237 if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
3238 zend_error_noreturn(E_COMPILE_ERROR, "Only variables can be passed by reference");
3239 }
3240 } else {
3241 opcode = ZEND_SEND_VAL_EX;
3242 }
3243 }
3244 }
3245
3246 opline = zend_emit_op(NULL, opcode, &arg_node, NULL);
3247 opline->op2.opline_num = arg_num;
3248 opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_num);
3249 }
3250
3251 return arg_count;
3252 }
3253 /* }}} */
3254
zend_get_call_op(const zend_op * init_op,zend_function * fbc)3255 ZEND_API zend_uchar zend_get_call_op(const zend_op *init_op, zend_function *fbc) /* {{{ */
3256 {
3257 if (fbc) {
3258 if (fbc->type == ZEND_INTERNAL_FUNCTION) {
3259 if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) {
3260 if (!(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_RETURN_REFERENCE))) {
3261 return ZEND_DO_ICALL;
3262 } else {
3263 return ZEND_DO_FCALL_BY_NAME;
3264 }
3265 }
3266 } else {
3267 if (zend_execute_ex == execute_ex && !(fbc->common.fn_flags & ZEND_ACC_ABSTRACT)) {
3268 return ZEND_DO_UCALL;
3269 }
3270 }
3271 } else if (zend_execute_ex == execute_ex &&
3272 !zend_execute_internal &&
3273 (init_op->opcode == ZEND_INIT_FCALL_BY_NAME ||
3274 init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) {
3275 return ZEND_DO_FCALL_BY_NAME;
3276 }
3277 return ZEND_DO_FCALL;
3278 }
3279 /* }}} */
3280
zend_compile_call_common(znode * result,zend_ast * args_ast,zend_function * fbc)3281 void zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
3282 {
3283 zend_op *opline;
3284 uint32_t opnum_init = get_next_op_number(CG(active_op_array)) - 1;
3285 uint32_t arg_count;
3286 uint32_t call_flags;
3287
3288 zend_do_extended_fcall_begin();
3289
3290 arg_count = zend_compile_args(args_ast, fbc);
3291
3292 opline = &CG(active_op_array)->opcodes[opnum_init];
3293 opline->extended_value = arg_count;
3294
3295 if (opline->opcode == ZEND_INIT_FCALL) {
3296 opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
3297 }
3298
3299 call_flags = (opline->opcode == ZEND_NEW ? ZEND_CALL_CTOR : 0);
3300 opline = zend_emit_op(result, zend_get_call_op(opline, fbc), NULL, NULL);
3301 opline->op1.num = call_flags;
3302
3303 zend_do_extended_fcall_end();
3304 }
3305 /* }}} */
3306
zend_compile_function_name(znode * name_node,zend_ast * name_ast)3307 zend_bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
3308 {
3309 zend_string *orig_name = zend_ast_get_str(name_ast);
3310 zend_bool is_fully_qualified;
3311
3312 name_node->op_type = IS_CONST;
3313 ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
3314 orig_name, name_ast->attr, &is_fully_qualified));
3315
3316 return !is_fully_qualified && FC(current_namespace);
3317 }
3318 /* }}} */
3319
zend_compile_ns_call(znode * result,znode * name_node,zend_ast * args_ast)3320 void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
3321 {
3322 zend_op *opline = get_next_op(CG(active_op_array));
3323 opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
3324 SET_UNUSED(opline->op1);
3325 opline->op2_type = IS_CONST;
3326 opline->op2.constant = zend_add_ns_func_name_literal(
3327 CG(active_op_array), Z_STR(name_node->u.constant));
3328 zend_alloc_cache_slot(opline->op2.constant);
3329
3330 zend_compile_call_common(result, args_ast, NULL);
3331 }
3332 /* }}} */
3333
zend_compile_dynamic_call(znode * result,znode * name_node,zend_ast * args_ast)3334 void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
3335 {
3336 if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
3337 const char *colon;
3338 zend_string *str = Z_STR(name_node->u.constant);
3339 if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
3340 zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
3341 zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
3342 zend_op *opline = get_next_op(CG(active_op_array));
3343
3344 opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
3345 opline->op1_type = IS_CONST;
3346 opline->op1.constant = zend_add_class_name_literal(CG(active_op_array), class);
3347 opline->op2_type = IS_CONST;
3348 opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), method);
3349 zend_alloc_cache_slot(opline->op2.constant);
3350 zval_ptr_dtor(&name_node->u.constant);
3351 } else {
3352 zend_op *opline = get_next_op(CG(active_op_array));
3353
3354 opline->opcode = ZEND_INIT_FCALL_BY_NAME;
3355 SET_UNUSED(opline->op1);
3356 opline->op2_type = IS_CONST;
3357 opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), str);
3358 zend_alloc_cache_slot(opline->op2.constant);
3359 }
3360 } else {
3361 zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node);
3362 }
3363
3364 zend_compile_call_common(result, args_ast, NULL);
3365 }
3366 /* }}} */
3367
zend_args_contain_unpack(zend_ast_list * args)3368 static zend_bool zend_args_contain_unpack(zend_ast_list *args) /* {{{ */
3369 {
3370 uint32_t i;
3371 for (i = 0; i < args->children; ++i) {
3372 if (args->child[i]->kind == ZEND_AST_UNPACK) {
3373 return 1;
3374 }
3375 }
3376 return 0;
3377 }
3378 /* }}} */
3379
zend_compile_func_strlen(znode * result,zend_ast_list * args)3380 int zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
3381 {
3382 znode arg_node;
3383
3384 if ((CG(compiler_options) & ZEND_COMPILE_NO_BUILTIN_STRLEN)
3385 || args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK
3386 ) {
3387 return FAILURE;
3388 }
3389
3390 zend_compile_expr(&arg_node, args->child[0]);
3391 if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
3392 result->op_type = IS_CONST;
3393 ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
3394 zval_dtor(&arg_node.u.constant);
3395 } else {
3396 zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
3397 }
3398 return SUCCESS;
3399 }
3400 /* }}} */
3401
zend_compile_func_typecheck(znode * result,zend_ast_list * args,uint32_t type)3402 int zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
3403 {
3404 znode arg_node;
3405 zend_op *opline;
3406
3407 if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
3408 return FAILURE;
3409 }
3410
3411 zend_compile_expr(&arg_node, args->child[0]);
3412 opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
3413 opline->extended_value = type;
3414 return SUCCESS;
3415 }
3416 /* }}} */
3417
zend_compile_func_cast(znode * result,zend_ast_list * args,uint32_t type)3418 int zend_compile_func_cast(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
3419 {
3420 znode arg_node;
3421 zend_op *opline;
3422
3423 if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
3424 return FAILURE;
3425 }
3426
3427 zend_compile_expr(&arg_node, args->child[0]);
3428 opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL);
3429 opline->extended_value = type;
3430 return SUCCESS;
3431 }
3432 /* }}} */
3433
zend_compile_func_defined(znode * result,zend_ast_list * args)3434 int zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
3435 {
3436 zend_string *name;
3437 zend_op *opline;
3438
3439 if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
3440 return FAILURE;
3441 }
3442
3443 name = zval_get_string(zend_ast_get_zval(args->child[0]));
3444 if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
3445 zend_string_release(name);
3446 return FAILURE;
3447 }
3448
3449 if (zend_try_ct_eval_const(&result->u.constant, name, 0)) {
3450 zend_string_release(name);
3451 zval_ptr_dtor(&result->u.constant);
3452 ZVAL_TRUE(&result->u.constant);
3453 result->op_type = IS_CONST;
3454 return SUCCESS;
3455 }
3456
3457 opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
3458 opline->op1_type = IS_CONST;
3459 LITERAL_STR(opline->op1, name);
3460 zend_alloc_cache_slot(opline->op1.constant);
3461
3462 /* Lowercase constant name in a separate literal */
3463 {
3464 zval c;
3465 zend_string *lcname = zend_string_tolower(name);
3466 ZVAL_NEW_STR(&c, lcname);
3467 zend_add_literal(CG(active_op_array), &c);
3468 }
3469 return SUCCESS;
3470 }
3471 /* }}} */
3472
zend_compile_func_chr(znode * result,zend_ast_list * args)3473 int zend_compile_func_chr(znode *result, zend_ast_list *args) /* {{{ */
3474 {
3475
3476 if (args->children == 1 &&
3477 args->child[0]->kind == ZEND_AST_ZVAL &&
3478 Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_LONG) {
3479
3480 zend_long c = Z_LVAL_P(zend_ast_get_zval(args->child[0])) & 0xff;
3481
3482 result->op_type = IS_CONST;
3483 if (CG(one_char_string)[c]) {
3484 ZVAL_INTERNED_STR(&result->u.constant, CG(one_char_string)[c]);
3485 } else {
3486 ZVAL_NEW_STR(&result->u.constant, zend_string_alloc(1, 0));
3487 Z_STRVAL_P(&result->u.constant)[0] = (char)c;
3488 Z_STRVAL_P(&result->u.constant)[1] = '\0';
3489 }
3490 return SUCCESS;
3491 } else {
3492 return FAILURE;
3493 }
3494 }
3495 /* }}} */
3496
zend_compile_func_ord(znode * result,zend_ast_list * args)3497 int zend_compile_func_ord(znode *result, zend_ast_list *args) /* {{{ */
3498 {
3499 if (args->children == 1 &&
3500 args->child[0]->kind == ZEND_AST_ZVAL &&
3501 Z_TYPE_P(zend_ast_get_zval(args->child[0])) == IS_STRING) {
3502
3503 result->op_type = IS_CONST;
3504 ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(zend_ast_get_zval(args->child[0]))[0]);
3505 return SUCCESS;
3506 } else {
3507 return FAILURE;
3508 }
3509 }
3510 /* }}} */
3511
3512
zend_try_compile_ct_bound_init_user_func(zend_ast * name_ast,uint32_t num_args)3513 static int zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
3514 {
3515 zend_string *name, *lcname;
3516 zend_function *fbc;
3517 zend_op *opline;
3518
3519 if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
3520 return FAILURE;
3521 }
3522
3523 name = zend_ast_get_str(name_ast);
3524 lcname = zend_string_tolower(name);
3525
3526 fbc = zend_hash_find_ptr(CG(function_table), lcname);
3527 if (!fbc
3528 || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
3529 || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
3530 ) {
3531 zend_string_release(lcname);
3532 return FAILURE;
3533 }
3534
3535 opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
3536 opline->extended_value = num_args;
3537 opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
3538 opline->op2_type = IS_CONST;
3539 LITERAL_STR(opline->op2, lcname);
3540 zend_alloc_cache_slot(opline->op2.constant);
3541
3542 return SUCCESS;
3543 }
3544 /* }}} */
3545
zend_compile_init_user_func(zend_ast * name_ast,uint32_t num_args,zend_string * orig_func_name)3546 static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
3547 {
3548 zend_op *opline;
3549 znode name_node;
3550
3551 if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
3552 return;
3553 }
3554
3555 zend_compile_expr(&name_node, name_ast);
3556
3557 opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
3558 opline->op1_type = IS_CONST;
3559 LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
3560 opline->extended_value = num_args;
3561 }
3562 /* }}} */
3563
3564 /* cufa = call_user_func_array */
zend_compile_func_cufa(znode * result,zend_ast_list * args,zend_string * lcname)3565 int zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
3566 {
3567 znode arg_node;
3568
3569 if (args->children != 2 || zend_args_contain_unpack(args)) {
3570 return FAILURE;
3571 }
3572
3573 zend_compile_init_user_func(args->child[0], 0, lcname);
3574 zend_compile_expr(&arg_node, args->child[1]);
3575 zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
3576 zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3577
3578 return SUCCESS;
3579 }
3580 /* }}} */
3581
3582 /* cuf = call_user_func */
zend_compile_func_cuf(znode * result,zend_ast_list * args,zend_string * lcname)3583 int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
3584 {
3585 uint32_t i;
3586
3587 if (args->children < 1 || zend_args_contain_unpack(args)) {
3588 return FAILURE;
3589 }
3590
3591 zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
3592 for (i = 1; i < args->children; ++i) {
3593 zend_ast *arg_ast = args->child[i];
3594 znode arg_node;
3595 zend_op *opline;
3596
3597 zend_compile_expr(&arg_node, arg_ast);
3598
3599 opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
3600 opline->op2.num = i;
3601 opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, i);
3602 }
3603 zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3604
3605 return SUCCESS;
3606 }
3607 /* }}} */
3608
zend_compile_assert(znode * result,zend_ast_list * args,zend_string * name,zend_function * fbc)3609 static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
3610 {
3611 if (EG(assertions) >= 0) {
3612 znode name_node;
3613 zend_op *opline;
3614 uint32_t check_op_number = get_next_op_number(CG(active_op_array));
3615
3616 zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
3617
3618 if (fbc) {
3619 name_node.op_type = IS_CONST;
3620 ZVAL_STR_COPY(&name_node.u.constant, name);
3621
3622 opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3623 } else {
3624 opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
3625 opline->op2_type = IS_CONST;
3626 opline->op2.constant = zend_add_ns_func_name_literal(
3627 CG(active_op_array), name);
3628 }
3629 zend_alloc_cache_slot(opline->op2.constant);
3630
3631 if (args->children == 1 &&
3632 (args->child[0]->kind != ZEND_AST_ZVAL ||
3633 Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
3634 /* add "assert(condition) as assertion message */
3635 zend_ast_list_add((zend_ast*)args,
3636 zend_ast_create_zval_from_str(
3637 zend_ast_export("assert(", args->child[0], ")")));
3638 }
3639
3640 zend_compile_call_common(result, (zend_ast*)args, fbc);
3641
3642 opline = &CG(active_op_array)->opcodes[check_op_number];
3643 opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3644 SET_NODE(opline->result, result);
3645 } else {
3646 if (!fbc) {
3647 zend_string_release(name);
3648 }
3649 result->op_type = IS_CONST;
3650 ZVAL_TRUE(&result->u.constant);
3651 }
3652
3653 return SUCCESS;
3654 }
3655 /* }}} */
3656
zend_try_compile_special_func(znode * result,zend_string * lcname,zend_ast_list * args,zend_function * fbc,uint32_t type)3657 int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc, uint32_t type) /* {{{ */
3658 {
3659 if (fbc->internal_function.handler == ZEND_FN(display_disabled_function)) {
3660 return FAILURE;
3661 }
3662
3663 if (zend_string_equals_literal(lcname, "assert")) {
3664 return zend_compile_assert(result, args, lcname, fbc);
3665 }
3666
3667 if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) {
3668 return FAILURE;
3669 }
3670
3671 if (zend_string_equals_literal(lcname, "strlen")) {
3672 return zend_compile_func_strlen(result, args);
3673 } else if (zend_string_equals_literal(lcname, "is_null")) {
3674 return zend_compile_func_typecheck(result, args, IS_NULL);
3675 } else if (zend_string_equals_literal(lcname, "is_bool")) {
3676 return zend_compile_func_typecheck(result, args, _IS_BOOL);
3677 } else if (zend_string_equals_literal(lcname, "is_long")
3678 || zend_string_equals_literal(lcname, "is_int")
3679 || zend_string_equals_literal(lcname, "is_integer")
3680 ) {
3681 return zend_compile_func_typecheck(result, args, IS_LONG);
3682 } else if (zend_string_equals_literal(lcname, "is_float")
3683 || zend_string_equals_literal(lcname, "is_double")
3684 || zend_string_equals_literal(lcname, "is_real")
3685 ) {
3686 return zend_compile_func_typecheck(result, args, IS_DOUBLE);
3687 } else if (zend_string_equals_literal(lcname, "is_string")) {
3688 return zend_compile_func_typecheck(result, args, IS_STRING);
3689 } else if (zend_string_equals_literal(lcname, "is_array")) {
3690 return zend_compile_func_typecheck(result, args, IS_ARRAY);
3691 } else if (zend_string_equals_literal(lcname, "is_object")) {
3692 return zend_compile_func_typecheck(result, args, IS_OBJECT);
3693 } else if (zend_string_equals_literal(lcname, "is_resource")) {
3694 return zend_compile_func_typecheck(result, args, IS_RESOURCE);
3695 } else if (zend_string_equals_literal(lcname, "boolval")) {
3696 return zend_compile_func_cast(result, args, _IS_BOOL);
3697 } else if (zend_string_equals_literal(lcname, "intval")) {
3698 return zend_compile_func_cast(result, args, IS_LONG);
3699 } else if (zend_string_equals_literal(lcname, "floatval")
3700 || zend_string_equals_literal(lcname, "doubleval")
3701 ) {
3702 return zend_compile_func_cast(result, args, IS_DOUBLE);
3703 } else if (zend_string_equals_literal(lcname, "strval")) {
3704 return zend_compile_func_cast(result, args, IS_STRING);
3705 } else if (zend_string_equals_literal(lcname, "defined")) {
3706 return zend_compile_func_defined(result, args);
3707 } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) {
3708 return zend_compile_func_chr(result, args);
3709 } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) {
3710 return zend_compile_func_ord(result, args);
3711 } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
3712 return zend_compile_func_cufa(result, args, lcname);
3713 } else if (zend_string_equals_literal(lcname, "call_user_func")) {
3714 return zend_compile_func_cuf(result, args, lcname);
3715 } else {
3716 return FAILURE;
3717 }
3718 }
3719 /* }}} */
3720
zend_compile_call(znode * result,zend_ast * ast,uint32_t type)3721 void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3722 {
3723 zend_ast *name_ast = ast->child[0];
3724 zend_ast *args_ast = ast->child[1];
3725
3726 znode name_node;
3727
3728 if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
3729 zend_compile_expr(&name_node, name_ast);
3730 zend_compile_dynamic_call(result, &name_node, args_ast);
3731 return;
3732 }
3733
3734 {
3735 zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
3736 if (runtime_resolution) {
3737 if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
3738 zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
3739 } else {
3740 zend_compile_ns_call(result, &name_node, args_ast);
3741 }
3742 return;
3743 }
3744 }
3745
3746 {
3747 zval *name = &name_node.u.constant;
3748 zend_string *lcname;
3749 zend_function *fbc;
3750 zend_op *opline;
3751
3752 lcname = zend_string_tolower(Z_STR_P(name));
3753
3754 fbc = zend_hash_find_ptr(CG(function_table), lcname);
3755 if (!fbc
3756 || (fbc->type == ZEND_INTERNAL_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
3757 || (fbc->type == ZEND_USER_FUNCTION && (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS))
3758 ) {
3759 zend_string_release(lcname);
3760 zend_compile_dynamic_call(result, &name_node, args_ast);
3761 return;
3762 }
3763
3764 if (zend_try_compile_special_func(result, lcname,
3765 zend_ast_get_list(args_ast), fbc, type) == SUCCESS
3766 ) {
3767 zend_string_release(lcname);
3768 zval_ptr_dtor(&name_node.u.constant);
3769 return;
3770 }
3771
3772 zval_ptr_dtor(&name_node.u.constant);
3773 ZVAL_NEW_STR(&name_node.u.constant, lcname);
3774
3775 opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3776 zend_alloc_cache_slot(opline->op2.constant);
3777
3778 zend_compile_call_common(result, args_ast, fbc);
3779 }
3780 }
3781 /* }}} */
3782
zend_compile_method_call(znode * result,zend_ast * ast,uint32_t type)3783 void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3784 {
3785 zend_ast *obj_ast = ast->child[0];
3786 zend_ast *method_ast = ast->child[1];
3787 zend_ast *args_ast = ast->child[2];
3788
3789 znode obj_node, method_node;
3790 zend_op *opline;
3791 zend_function *fbc = NULL;
3792
3793 if (is_this_fetch(obj_ast)) {
3794 obj_node.op_type = IS_UNUSED;
3795 } else {
3796 zend_compile_expr(&obj_node, obj_ast);
3797 }
3798
3799 zend_compile_expr(&method_node, method_ast);
3800 opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
3801
3802 if (method_node.op_type == IS_CONST) {
3803 if (Z_TYPE(method_node.u.constant) != IS_STRING) {
3804 zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3805 }
3806
3807 opline->op2_type = IS_CONST;
3808 opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3809 Z_STR(method_node.u.constant));
3810 zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3811 } else {
3812 SET_NODE(opline->op2, &method_node);
3813 }
3814
3815 /* Check if this calls a known method on $this */
3816 if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST &&
3817 CG(active_class_entry) && zend_is_scope_known()) {
3818 zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
3819 fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname);
3820
3821 /* We only know the exact method that is being called if it is either private or final.
3822 * Otherwise an overriding method in a child class may be called. */
3823 if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) {
3824 fbc = NULL;
3825 }
3826 }
3827
3828 zend_compile_call_common(result, args_ast, fbc);
3829 }
3830 /* }}} */
3831
zend_is_constructor(zend_string * name)3832 static zend_bool zend_is_constructor(zend_string *name) /* {{{ */
3833 {
3834 return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
3835 }
3836 /* }}} */
3837
zend_compile_static_call(znode * result,zend_ast * ast,uint32_t type)3838 void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3839 {
3840 zend_ast *class_ast = ast->child[0];
3841 zend_ast *method_ast = ast->child[1];
3842 zend_ast *args_ast = ast->child[2];
3843
3844 znode class_node, method_node;
3845 zend_op *opline;
3846 zend_function *fbc = NULL;
3847
3848 zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3849
3850 zend_compile_expr(&method_node, method_ast);
3851 if (method_node.op_type == IS_CONST) {
3852 zval *name = &method_node.u.constant;
3853 if (Z_TYPE_P(name) != IS_STRING) {
3854 zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3855 }
3856 if (zend_is_constructor(Z_STR_P(name))) {
3857 zval_ptr_dtor(name);
3858 method_node.op_type = IS_UNUSED;
3859 }
3860 }
3861
3862 opline = get_next_op(CG(active_op_array));
3863 opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
3864
3865 zend_set_class_name_op1(opline, &class_node);
3866
3867 if (method_node.op_type == IS_CONST) {
3868 opline->op2_type = IS_CONST;
3869 opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3870 Z_STR(method_node.u.constant));
3871 if (opline->op1_type == IS_CONST) {
3872 zend_alloc_cache_slot(opline->op2.constant);
3873 } else {
3874 zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3875 }
3876 } else {
3877 SET_NODE(opline->op2, &method_node);
3878 }
3879 zend_check_live_ranges(opline);
3880
3881 /* Check if we already know which method we're calling */
3882 if (opline->op2_type == IS_CONST) {
3883 zend_class_entry *ce = NULL;
3884 if (opline->op1_type == IS_CONST) {
3885 zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1);
3886 ce = zend_hash_find_ptr(CG(class_table), lcname);
3887 if (!ce && CG(active_class_entry)
3888 && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) {
3889 ce = CG(active_class_entry);
3890 }
3891 } else if (opline->op1_type == IS_UNUSED
3892 && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
3893 && zend_is_scope_known()) {
3894 ce = CG(active_class_entry);
3895 }
3896 if (ce) {
3897 zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1);
3898 fbc = zend_hash_find_ptr(&ce->function_table, lcname);
3899 }
3900 }
3901
3902 zend_compile_call_common(result, args_ast, fbc);
3903 }
3904 /* }}} */
3905
3906 void zend_compile_class_decl(zend_ast *ast);
3907
zend_compile_new(znode * result,zend_ast * ast)3908 void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
3909 {
3910 zend_ast *class_ast = ast->child[0];
3911 zend_ast *args_ast = ast->child[1];
3912
3913 znode class_node, ctor_result;
3914 zend_op *opline;
3915 uint32_t opnum;
3916
3917 if (class_ast->kind == ZEND_AST_CLASS) {
3918 uint32_t dcl_opnum = get_next_op_number(CG(active_op_array));
3919 zend_compile_class_decl(class_ast);
3920 /* jump over anon class declaration */
3921 opline = &CG(active_op_array)->opcodes[dcl_opnum];
3922 if (opline->opcode == ZEND_FETCH_CLASS) {
3923 opline++;
3924 }
3925 class_node.op_type = opline->result_type;
3926 class_node.u.op.var = opline->result.var;
3927 opline->extended_value = get_next_op_number(CG(active_op_array));
3928 } else {
3929 zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
3930 }
3931
3932 opnum = get_next_op_number(CG(active_op_array));
3933 opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
3934
3935 if (class_node.op_type == IS_CONST) {
3936 opline->op1_type = IS_CONST;
3937 opline->op1.constant = zend_add_class_name_literal(
3938 CG(active_op_array), Z_STR(class_node.u.constant));
3939 } else {
3940 SET_NODE(opline->op1, &class_node);
3941 }
3942
3943 zend_compile_call_common(&ctor_result, args_ast, NULL);
3944 zend_do_free(&ctor_result);
3945
3946 /* We save the position of DO_FCALL for convenience in find_live_range().
3947 * This info is not preserved for runtime. */
3948 opline = &CG(active_op_array)->opcodes[opnum];
3949 opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3950 }
3951 /* }}} */
3952
zend_compile_clone(znode * result,zend_ast * ast)3953 void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
3954 {
3955 zend_ast *obj_ast = ast->child[0];
3956
3957 znode obj_node;
3958 zend_compile_expr(&obj_node, obj_ast);
3959
3960 zend_emit_op_tmp(result, ZEND_CLONE, &obj_node, NULL);
3961 }
3962 /* }}} */
3963
zend_compile_global_var(zend_ast * ast)3964 void zend_compile_global_var(zend_ast *ast) /* {{{ */
3965 {
3966 zend_ast *var_ast = ast->child[0];
3967 zend_ast *name_ast = var_ast->child[0];
3968
3969 znode name_node, result;
3970
3971 zend_compile_expr(&name_node, name_ast);
3972 if (name_node.op_type == IS_CONST) {
3973 convert_to_string(&name_node.u.constant);
3974 }
3975
3976 if (is_this_fetch(var_ast)) {
3977 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable");
3978 } else if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
3979 zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
3980 zend_alloc_cache_slot(opline->op2.constant);
3981 } else {
3982 /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W
3983 * to not free the name_node operand, so it can be reused in the following
3984 * ASSIGN_REF, which then frees it. */
3985 zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
3986 opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
3987
3988 if (name_node.op_type == IS_CONST) {
3989 zend_string_addref(Z_STR(name_node.u.constant));
3990 }
3991
3992 zend_emit_assign_ref_znode(
3993 zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)),
3994 &result
3995 );
3996 }
3997 }
3998 /* }}} */
3999
zend_compile_static_var_common(zend_ast * var_ast,zval * value,zend_bool by_ref)4000 static void zend_compile_static_var_common(zend_ast *var_ast, zval *value, zend_bool by_ref) /* {{{ */
4001 {
4002 znode var_node;
4003 zend_op *opline;
4004
4005 zend_compile_expr(&var_node, var_ast);
4006
4007 if (!CG(active_op_array)->static_variables) {
4008 if (CG(active_op_array)->scope) {
4009 CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
4010 }
4011 ALLOC_HASHTABLE(CG(active_op_array)->static_variables);
4012 zend_hash_init(CG(active_op_array)->static_variables, 8, NULL, ZVAL_PTR_DTOR, 0);
4013 }
4014
4015 if (GC_REFCOUNT(CG(active_op_array)->static_variables) > 1) {
4016 if (!(GC_FLAGS(CG(active_op_array)->static_variables) & IS_ARRAY_IMMUTABLE)) {
4017 GC_REFCOUNT(CG(active_op_array)->static_variables)--;
4018 }
4019 CG(active_op_array)->static_variables = zend_array_dup(CG(active_op_array)->static_variables);
4020 }
4021 zend_hash_update(CG(active_op_array)->static_variables, Z_STR(var_node.u.constant), value);
4022
4023 if (zend_string_equals_literal(Z_STR(var_node.u.constant), "this")) {
4024 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable");
4025 }
4026
4027 opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &var_node);
4028 opline->op1_type = IS_CV;
4029 opline->op1.var = lookup_cv(CG(active_op_array), zend_string_copy(Z_STR(var_node.u.constant)));
4030 opline->extended_value = by_ref;
4031 }
4032 /* }}} */
4033
zend_compile_static_var(zend_ast * ast)4034 void zend_compile_static_var(zend_ast *ast) /* {{{ */
4035 {
4036 zend_ast *var_ast = ast->child[0];
4037 zend_ast *value_ast = ast->child[1];
4038 zval value_zv;
4039
4040 if (value_ast) {
4041 zend_const_expr_to_zval(&value_zv, value_ast);
4042 } else {
4043 ZVAL_NULL(&value_zv);
4044 }
4045
4046 zend_compile_static_var_common(var_ast, &value_zv, 1);
4047 }
4048 /* }}} */
4049
zend_compile_unset(zend_ast * ast)4050 void zend_compile_unset(zend_ast *ast) /* {{{ */
4051 {
4052 zend_ast *var_ast = ast->child[0];
4053 znode var_node;
4054 zend_op *opline;
4055
4056 zend_ensure_writable_variable(var_ast);
4057
4058 switch (var_ast->kind) {
4059 case ZEND_AST_VAR:
4060 if (is_this_fetch(var_ast)) {
4061 zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this");
4062 } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
4063 opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
4064 opline->extended_value = ZEND_FETCH_LOCAL | ZEND_QUICK_SET;
4065 } else {
4066 opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
4067 opline->opcode = ZEND_UNSET_VAR;
4068 }
4069 return;
4070 case ZEND_AST_DIM:
4071 opline = zend_compile_dim_common(NULL, var_ast, BP_VAR_UNSET);
4072 opline->opcode = ZEND_UNSET_DIM;
4073 return;
4074 case ZEND_AST_PROP:
4075 opline = zend_compile_prop_common(NULL, var_ast, BP_VAR_UNSET);
4076 opline->opcode = ZEND_UNSET_OBJ;
4077 return;
4078 case ZEND_AST_STATIC_PROP:
4079 opline = zend_compile_static_prop_common(NULL, var_ast, BP_VAR_UNSET, 0);
4080 opline->opcode = ZEND_UNSET_STATIC_PROP;
4081 return;
4082 EMPTY_SWITCH_DEFAULT_CASE()
4083 }
4084 }
4085 /* }}} */
4086
zend_handle_loops_and_finally_ex(zend_long depth,znode * return_value)4087 static int zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */
4088 {
4089 zend_loop_var *base;
4090 zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
4091
4092 if (!loop_var) {
4093 return 1;
4094 }
4095 base = zend_stack_base(&CG(loop_var_stack));
4096 for (; loop_var >= base; loop_var--) {
4097 if (loop_var->opcode == ZEND_FAST_CALL) {
4098 zend_op *opline = get_next_op(CG(active_op_array));
4099
4100 opline->opcode = ZEND_FAST_CALL;
4101 opline->result_type = IS_TMP_VAR;
4102 opline->result.var = loop_var->var_num;
4103 SET_UNUSED(opline->op1);
4104 if (return_value) {
4105 SET_NODE(opline->op2, return_value);
4106 } else {
4107 SET_UNUSED(opline->op2);
4108 }
4109 opline->op1.num = loop_var->u.try_catch_offset;
4110 } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
4111 zend_op *opline = get_next_op(CG(active_op_array));
4112 opline->opcode = ZEND_DISCARD_EXCEPTION;
4113 opline->op1_type = IS_TMP_VAR;
4114 opline->op1.var = loop_var->var_num;
4115 SET_UNUSED(opline->op2);
4116 } else if (loop_var->opcode == ZEND_RETURN) {
4117 /* Stack separator */
4118 break;
4119 } else if (depth <= 1) {
4120 return 1;
4121 } else if (loop_var->opcode == ZEND_NOP) {
4122 /* Loop doesn't have freeable variable */
4123 depth--;
4124 } else {
4125 zend_op *opline;
4126
4127 ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR));
4128 opline = get_next_op(CG(active_op_array));
4129 opline->opcode = loop_var->opcode;
4130 opline->op1_type = loop_var->var_type;
4131 opline->op1.var = loop_var->var_num;
4132 SET_UNUSED(opline->op2);
4133 opline->op2.num = loop_var->u.live_range_offset;
4134 opline->extended_value = ZEND_FREE_ON_RETURN;
4135 depth--;
4136 }
4137 }
4138 return (depth == 0);
4139 }
4140 /* }}} */
4141
zend_handle_loops_and_finally(znode * return_value)4142 static int zend_handle_loops_and_finally(znode *return_value) /* {{{ */
4143 {
4144 return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value);
4145 }
4146 /* }}} */
4147
zend_has_finally_ex(zend_long depth)4148 static int zend_has_finally_ex(zend_long depth) /* {{{ */
4149 {
4150 zend_loop_var *base;
4151 zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack));
4152
4153 if (!loop_var) {
4154 return 0;
4155 }
4156 base = zend_stack_base(&CG(loop_var_stack));
4157 for (; loop_var >= base; loop_var--) {
4158 if (loop_var->opcode == ZEND_FAST_CALL) {
4159 return 1;
4160 } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) {
4161 } else if (loop_var->opcode == ZEND_RETURN) {
4162 /* Stack separator */
4163 return 0;
4164 } else if (depth <= 1) {
4165 return 0;
4166 } else {
4167 depth--;
4168 }
4169 }
4170 return 0;
4171 }
4172 /* }}} */
4173
zend_has_finally(void)4174 static int zend_has_finally(void) /* {{{ */
4175 {
4176 return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1);
4177 }
4178 /* }}} */
4179
zend_compile_return(zend_ast * ast)4180 void zend_compile_return(zend_ast *ast) /* {{{ */
4181 {
4182 zend_ast *expr_ast = ast->child[0];
4183 zend_bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0;
4184 zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
4185
4186 znode expr_node;
4187 zend_op *opline;
4188
4189 if (is_generator) {
4190 /* For generators the by-ref flag refers to yields, not returns */
4191 by_ref = 0;
4192 }
4193
4194 if (!expr_ast) {
4195 expr_node.op_type = IS_CONST;
4196 ZVAL_NULL(&expr_node.u.constant);
4197 } else if (by_ref && zend_is_variable(expr_ast) && !zend_is_call(expr_ast)) {
4198 zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
4199 } else {
4200 zend_compile_expr(&expr_node, expr_ast);
4201 }
4202
4203 if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
4204 && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
4205 && zend_has_finally()) {
4206 /* Copy return value into temporary VAR to avoid modification in finally code */
4207 if (by_ref) {
4208 zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL);
4209 } else {
4210 zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL);
4211 }
4212 }
4213
4214 /* Generator return types are handled separately */
4215 if (!is_generator && CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
4216 zend_emit_return_type_check(
4217 expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, 0);
4218 }
4219
4220 zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);
4221
4222 opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
4223 &expr_node, NULL);
4224
4225 if (by_ref && expr_ast) {
4226 if (zend_is_call(expr_ast)) {
4227 opline->extended_value = ZEND_RETURNS_FUNCTION;
4228 } else if (!zend_is_variable(expr_ast)) {
4229 opline->extended_value = ZEND_RETURNS_VALUE;
4230 }
4231 }
4232 }
4233 /* }}} */
4234
zend_compile_echo(zend_ast * ast)4235 void zend_compile_echo(zend_ast *ast) /* {{{ */
4236 {
4237 zend_op *opline;
4238 zend_ast *expr_ast = ast->child[0];
4239
4240 znode expr_node;
4241 zend_compile_expr(&expr_node, expr_ast);
4242
4243 opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
4244 opline->extended_value = 0;
4245 }
4246 /* }}} */
4247
zend_compile_throw(zend_ast * ast)4248 void zend_compile_throw(zend_ast *ast) /* {{{ */
4249 {
4250 zend_ast *expr_ast = ast->child[0];
4251
4252 znode expr_node;
4253 zend_compile_expr(&expr_node, expr_ast);
4254
4255 zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
4256 }
4257 /* }}} */
4258
zend_compile_break_continue(zend_ast * ast)4259 void zend_compile_break_continue(zend_ast *ast) /* {{{ */
4260 {
4261 zend_ast *depth_ast = ast->child[0];
4262
4263 zend_op *opline;
4264 int depth;
4265
4266 ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
4267
4268 if (depth_ast) {
4269 zval *depth_zv;
4270 if (depth_ast->kind != ZEND_AST_ZVAL) {
4271 zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-constant operand "
4272 "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4273 }
4274
4275 depth_zv = zend_ast_get_zval(depth_ast);
4276 if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
4277 zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive numbers",
4278 ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4279 }
4280
4281 depth = Z_LVAL_P(depth_zv);
4282 } else {
4283 depth = 1;
4284 }
4285
4286 if (CG(context).current_brk_cont == -1) {
4287 zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
4288 ast->kind == ZEND_AST_BREAK ? "break" : "continue");
4289 } else {
4290 if (!zend_handle_loops_and_finally_ex(depth, NULL)) {
4291 zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' %d level%s",
4292 ast->kind == ZEND_AST_BREAK ? "break" : "continue",
4293 depth, depth == 1 ? "" : "s");
4294 }
4295 }
4296 opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
4297 opline->op1.num = CG(context).current_brk_cont;
4298 opline->op2.num = depth;
4299 }
4300 /* }}} */
4301
zend_resolve_goto_label(zend_op_array * op_array,zend_op * opline)4302 void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */
4303 {
4304 zend_label *dest;
4305 int current, remove_oplines = opline->op1.num;
4306 zval *label;
4307 uint32_t opnum = opline - op_array->opcodes;
4308
4309 label = CT_CONSTANT_EX(op_array, opline->op2.constant);
4310 if (CG(context).labels == NULL ||
4311 (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL
4312 ) {
4313 CG(in_compilation) = 1;
4314 CG(active_op_array) = op_array;
4315 CG(zend_lineno) = opline->lineno;
4316 zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
4317 }
4318
4319 zval_dtor(label);
4320 ZVAL_NULL(label);
4321
4322 current = opline->extended_value;
4323 for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) {
4324 if (current == -1) {
4325 CG(in_compilation) = 1;
4326 CG(active_op_array) = op_array;
4327 CG(zend_lineno) = opline->lineno;
4328 zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
4329 }
4330 if (CG(context).brk_cont_array[current].start >= 0) {
4331 remove_oplines--;
4332 }
4333 }
4334
4335 for (current = 0; current < op_array->last_try_catch; ++current) {
4336 zend_try_catch_element *elem = &op_array->try_catch_array[current];
4337 if (elem->try_op > opnum) {
4338 break;
4339 }
4340 if (elem->finally_op && opnum < elem->finally_op - 1
4341 && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op)
4342 ) {
4343 remove_oplines--;
4344 }
4345 }
4346
4347 opline->opcode = ZEND_JMP;
4348 opline->op1.opline_num = dest->opline_num;
4349 opline->extended_value = 0;
4350 SET_UNUSED(opline->op1);
4351 SET_UNUSED(opline->op2);
4352 SET_UNUSED(opline->result);
4353
4354 ZEND_ASSERT(remove_oplines >= 0);
4355 while (remove_oplines--) {
4356 opline--;
4357 MAKE_NOP(opline);
4358 ZEND_VM_SET_OPCODE_HANDLER(opline);
4359 }
4360 }
4361 /* }}} */
4362
zend_compile_goto(zend_ast * ast)4363 void zend_compile_goto(zend_ast *ast) /* {{{ */
4364 {
4365 zend_ast *label_ast = ast->child[0];
4366 znode label_node;
4367 zend_op *opline;
4368 uint32_t opnum_start = get_next_op_number(CG(active_op_array));
4369
4370 zend_compile_expr(&label_node, label_ast);
4371
4372 /* Label resolution and unwinding adjustments happen in pass two. */
4373 zend_handle_loops_and_finally(NULL);
4374 opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
4375 opline->op1.num = get_next_op_number(CG(active_op_array)) - opnum_start - 1;
4376 opline->extended_value = CG(context).current_brk_cont;
4377 }
4378 /* }}} */
4379
zend_compile_label(zend_ast * ast)4380 void zend_compile_label(zend_ast *ast) /* {{{ */
4381 {
4382 zend_string *label = zend_ast_get_str(ast->child[0]);
4383 zend_label dest;
4384
4385 if (!CG(context).labels) {
4386 ALLOC_HASHTABLE(CG(context).labels);
4387 zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0);
4388 }
4389
4390 dest.brk_cont = CG(context).current_brk_cont;
4391 dest.opline_num = get_next_op_number(CG(active_op_array));
4392
4393 if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
4394 zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
4395 }
4396 }
4397 /* }}} */
4398
zend_compile_while(zend_ast * ast)4399 void zend_compile_while(zend_ast *ast) /* {{{ */
4400 {
4401 zend_ast *cond_ast = ast->child[0];
4402 zend_ast *stmt_ast = ast->child[1];
4403 znode cond_node;
4404 uint32_t opnum_start, opnum_jmp, opnum_cond;
4405
4406 opnum_jmp = zend_emit_jump(0);
4407
4408 zend_begin_loop(ZEND_NOP, NULL);
4409
4410 opnum_start = get_next_op_number(CG(active_op_array));
4411 zend_compile_stmt(stmt_ast);
4412
4413 opnum_cond = get_next_op_number(CG(active_op_array));
4414 zend_update_jump_target(opnum_jmp, opnum_cond);
4415 zend_compile_expr(&cond_node, cond_ast);
4416
4417 zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
4418
4419 zend_end_loop(opnum_cond, NULL);
4420 }
4421 /* }}} */
4422
zend_compile_do_while(zend_ast * ast)4423 void zend_compile_do_while(zend_ast *ast) /* {{{ */
4424 {
4425 zend_ast *stmt_ast = ast->child[0];
4426 zend_ast *cond_ast = ast->child[1];
4427
4428 znode cond_node;
4429 uint32_t opnum_start, opnum_cond;
4430
4431 zend_begin_loop(ZEND_NOP, NULL);
4432
4433 opnum_start = get_next_op_number(CG(active_op_array));
4434 zend_compile_stmt(stmt_ast);
4435
4436 opnum_cond = get_next_op_number(CG(active_op_array));
4437 zend_compile_expr(&cond_node, cond_ast);
4438
4439 zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
4440
4441 zend_end_loop(opnum_cond, NULL);
4442 }
4443 /* }}} */
4444
zend_compile_expr_list(znode * result,zend_ast * ast)4445 void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
4446 {
4447 zend_ast_list *list;
4448 uint32_t i;
4449
4450 result->op_type = IS_CONST;
4451 ZVAL_TRUE(&result->u.constant);
4452
4453 if (!ast) {
4454 return;
4455 }
4456
4457 list = zend_ast_get_list(ast);
4458 for (i = 0; i < list->children; ++i) {
4459 zend_ast *expr_ast = list->child[i];
4460
4461 zend_do_free(result);
4462 zend_compile_expr(result, expr_ast);
4463 }
4464 }
4465 /* }}} */
4466
zend_compile_for(zend_ast * ast)4467 void zend_compile_for(zend_ast *ast) /* {{{ */
4468 {
4469 zend_ast *init_ast = ast->child[0];
4470 zend_ast *cond_ast = ast->child[1];
4471 zend_ast *loop_ast = ast->child[2];
4472 zend_ast *stmt_ast = ast->child[3];
4473
4474 znode result;
4475 uint32_t opnum_start, opnum_jmp, opnum_loop;
4476
4477 zend_compile_expr_list(&result, init_ast);
4478 zend_do_free(&result);
4479
4480 opnum_jmp = zend_emit_jump(0);
4481
4482 zend_begin_loop(ZEND_NOP, NULL);
4483
4484 opnum_start = get_next_op_number(CG(active_op_array));
4485 zend_compile_stmt(stmt_ast);
4486
4487 opnum_loop = get_next_op_number(CG(active_op_array));
4488 zend_compile_expr_list(&result, loop_ast);
4489 zend_do_free(&result);
4490
4491 zend_update_jump_target_to_next(opnum_jmp);
4492 zend_compile_expr_list(&result, cond_ast);
4493 zend_do_extended_info();
4494
4495 zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
4496
4497 zend_end_loop(opnum_loop, NULL);
4498 }
4499 /* }}} */
4500
zend_compile_foreach(zend_ast * ast)4501 void zend_compile_foreach(zend_ast *ast) /* {{{ */
4502 {
4503 zend_ast *expr_ast = ast->child[0];
4504 zend_ast *value_ast = ast->child[1];
4505 zend_ast *key_ast = ast->child[2];
4506 zend_ast *stmt_ast = ast->child[3];
4507 zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
4508 zend_bool is_variable = zend_is_variable(expr_ast) && !zend_is_call(expr_ast)
4509 && zend_can_write_to_variable(expr_ast);
4510
4511 znode expr_node, reset_node, value_node, key_node;
4512 zend_op *opline;
4513 uint32_t opnum_reset, opnum_fetch;
4514
4515 if (key_ast) {
4516 if (key_ast->kind == ZEND_AST_REF) {
4517 zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
4518 }
4519 if (key_ast->kind == ZEND_AST_ARRAY) {
4520 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
4521 }
4522 }
4523
4524 if (by_ref) {
4525 value_ast = value_ast->child[0];
4526 }
4527
4528 if (by_ref && is_variable) {
4529 zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
4530 } else {
4531 zend_compile_expr(&expr_node, expr_ast);
4532 }
4533
4534 if (by_ref) {
4535 zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
4536 }
4537
4538 opnum_reset = get_next_op_number(CG(active_op_array));
4539 opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
4540
4541 zend_begin_loop(ZEND_FE_FREE, &reset_node);
4542
4543 opnum_fetch = get_next_op_number(CG(active_op_array));
4544 opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
4545
4546 if (is_this_fetch(value_ast)) {
4547 zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
4548 } else if (value_ast->kind == ZEND_AST_VAR &&
4549 zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
4550 SET_NODE(opline->op2, &value_node);
4551 } else {
4552 opline->op2_type = IS_VAR;
4553 opline->op2.var = get_temporary_variable(CG(active_op_array));
4554 GET_NODE(&value_node, opline->op2);
4555 if (by_ref) {
4556 zend_emit_assign_ref_znode(value_ast, &value_node);
4557 } else {
4558 zend_emit_assign_znode(value_ast, &value_node);
4559 }
4560 }
4561
4562 if (key_ast) {
4563 opline = &CG(active_op_array)->opcodes[opnum_fetch];
4564 zend_make_tmp_result(&key_node, opline);
4565 zend_emit_assign_znode(key_ast, &key_node);
4566 }
4567
4568 zend_compile_stmt(stmt_ast);
4569
4570 zend_emit_jump(opnum_fetch);
4571
4572 opline = &CG(active_op_array)->opcodes[opnum_reset];
4573 opline->op2.opline_num = get_next_op_number(CG(active_op_array));
4574
4575 opline = &CG(active_op_array)->opcodes[opnum_fetch];
4576 opline->extended_value = get_next_op_number(CG(active_op_array));
4577
4578 zend_end_loop(opnum_fetch, &reset_node);
4579
4580 opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL);
4581 }
4582 /* }}} */
4583
zend_compile_if(zend_ast * ast)4584 void zend_compile_if(zend_ast *ast) /* {{{ */
4585 {
4586 zend_ast_list *list = zend_ast_get_list(ast);
4587 uint32_t i;
4588 uint32_t *jmp_opnums = NULL;
4589
4590 if (list->children > 1) {
4591 jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
4592 }
4593
4594 for (i = 0; i < list->children; ++i) {
4595 zend_ast *elem_ast = list->child[i];
4596 zend_ast *cond_ast = elem_ast->child[0];
4597 zend_ast *stmt_ast = elem_ast->child[1];
4598
4599 znode cond_node;
4600 uint32_t opnum_jmpz;
4601 if (cond_ast) {
4602 zend_compile_expr(&cond_node, cond_ast);
4603 opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
4604 }
4605
4606 zend_compile_stmt(stmt_ast);
4607
4608 if (i != list->children - 1) {
4609 jmp_opnums[i] = zend_emit_jump(0);
4610 }
4611
4612 if (cond_ast) {
4613 zend_update_jump_target_to_next(opnum_jmpz);
4614 }
4615 }
4616
4617 if (list->children > 1) {
4618 for (i = 0; i < list->children - 1; ++i) {
4619 zend_update_jump_target_to_next(jmp_opnums[i]);
4620 }
4621 efree(jmp_opnums);
4622 }
4623 }
4624 /* }}} */
4625
zend_compile_switch(zend_ast * ast)4626 void zend_compile_switch(zend_ast *ast) /* {{{ */
4627 {
4628 zend_ast *expr_ast = ast->child[0];
4629 zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
4630
4631 uint32_t i;
4632 zend_bool has_default_case = 0;
4633
4634 znode expr_node, case_node;
4635 zend_op *opline;
4636 uint32_t *jmpnz_opnums, opnum_default_jmp;
4637
4638 zend_compile_expr(&expr_node, expr_ast);
4639
4640 zend_begin_loop(ZEND_FREE, &expr_node);
4641
4642 case_node.op_type = IS_TMP_VAR;
4643 case_node.u.op.var = get_temporary_variable(CG(active_op_array));
4644
4645 jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
4646 for (i = 0; i < cases->children; ++i) {
4647 zend_ast *case_ast = cases->child[i];
4648 zend_ast *cond_ast = case_ast->child[0];
4649 znode cond_node;
4650
4651 if (!cond_ast) {
4652 if (has_default_case) {
4653 CG(zend_lineno) = case_ast->lineno;
4654 zend_error_noreturn(E_COMPILE_ERROR,
4655 "Switch statements may only contain one default clause");
4656 }
4657 has_default_case = 1;
4658 continue;
4659 }
4660
4661 zend_compile_expr(&cond_node, cond_ast);
4662
4663 if (expr_node.op_type == IS_CONST
4664 && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
4665 jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
4666 } else if (expr_node.op_type == IS_CONST
4667 && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
4668 jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
4669 } else {
4670 opline = zend_emit_op(NULL, ZEND_CASE, &expr_node, &cond_node);
4671 SET_NODE(opline->result, &case_node);
4672 if (opline->op1_type == IS_CONST) {
4673 zval_copy_ctor(CT_CONSTANT(opline->op1));
4674 }
4675
4676 jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
4677 }
4678 }
4679
4680 opnum_default_jmp = zend_emit_jump(0);
4681
4682 for (i = 0; i < cases->children; ++i) {
4683 zend_ast *case_ast = cases->child[i];
4684 zend_ast *cond_ast = case_ast->child[0];
4685 zend_ast *stmt_ast = case_ast->child[1];
4686
4687 if (cond_ast) {
4688 zend_update_jump_target_to_next(jmpnz_opnums[i]);
4689 } else {
4690 zend_update_jump_target_to_next(opnum_default_jmp);
4691 }
4692
4693 zend_compile_stmt(stmt_ast);
4694 }
4695
4696 if (!has_default_case) {
4697 zend_update_jump_target_to_next(opnum_default_jmp);
4698 }
4699
4700 zend_end_loop(get_next_op_number(CG(active_op_array)), &expr_node);
4701
4702 if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) {
4703 /* don't use emit_op() to prevent automatic live-range construction */
4704 opline = get_next_op(CG(active_op_array));
4705 opline->opcode = ZEND_FREE;
4706 SET_NODE(opline->op1, &expr_node);
4707 SET_UNUSED(opline->op2);
4708 } else if (expr_node.op_type == IS_CONST) {
4709 zval_dtor(&expr_node.u.constant);
4710 }
4711
4712 efree(jmpnz_opnums);
4713 }
4714 /* }}} */
4715
zend_compile_try(zend_ast * ast)4716 void zend_compile_try(zend_ast *ast) /* {{{ */
4717 {
4718 zend_ast *try_ast = ast->child[0];
4719 zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
4720 zend_ast *finally_ast = ast->child[2];
4721
4722 uint32_t i, j;
4723 zend_op *opline;
4724 uint32_t try_catch_offset;
4725 uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
4726 uint32_t orig_fast_call_var = CG(context).fast_call_var;
4727 uint32_t orig_try_catch_offset = CG(context).try_catch_offset;
4728
4729 if (catches->children == 0 && !finally_ast) {
4730 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
4731 }
4732
4733 /* label: try { } must not be equal to try { label: } */
4734 if (CG(context).labels) {
4735 zend_label *label;
4736 ZEND_HASH_REVERSE_FOREACH_PTR(CG(context).labels, label) {
4737 if (label->opline_num == get_next_op_number(CG(active_op_array))) {
4738 zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
4739 }
4740 break;
4741 } ZEND_HASH_FOREACH_END();
4742 }
4743
4744 try_catch_offset = zend_add_try_element(get_next_op_number(CG(active_op_array)));
4745
4746 if (finally_ast) {
4747 zend_loop_var fast_call;
4748 if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
4749 CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
4750 }
4751 CG(context).fast_call_var = get_temporary_variable(CG(active_op_array));
4752
4753 /* Push FAST_CALL on unwind stack */
4754 fast_call.opcode = ZEND_FAST_CALL;
4755 fast_call.var_type = IS_TMP_VAR;
4756 fast_call.var_num = CG(context).fast_call_var;
4757 fast_call.u.try_catch_offset = try_catch_offset;
4758 zend_stack_push(&CG(loop_var_stack), &fast_call);
4759 }
4760
4761 CG(context).try_catch_offset = try_catch_offset;
4762
4763 zend_compile_stmt(try_ast);
4764
4765 if (catches->children != 0) {
4766 jmp_opnums[0] = zend_emit_jump(0);
4767 }
4768
4769 for (i = 0; i < catches->children; ++i) {
4770 zend_ast *catch_ast = catches->child[i];
4771 zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]);
4772 zend_ast *var_ast = catch_ast->child[1];
4773 zend_ast *stmt_ast = catch_ast->child[2];
4774 zval *var_name = zend_ast_get_zval(var_ast);
4775 zend_bool is_last_catch = (i + 1 == catches->children);
4776
4777 uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0);
4778 uint32_t opnum_catch;
4779
4780 CG(zend_lineno) = catch_ast->lineno;
4781
4782 for (j = 0; j < classes->children; j++) {
4783
4784 zend_ast *class_ast = classes->child[j];
4785 zend_bool is_last_class = (j + 1 == classes->children);
4786
4787 if (!zend_is_const_default_class_ref(class_ast)) {
4788 zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
4789 }
4790
4791 opnum_catch = get_next_op_number(CG(active_op_array));
4792 if (i == 0 && j == 0) {
4793 CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
4794 }
4795
4796 opline = get_next_op(CG(active_op_array));
4797 opline->opcode = ZEND_CATCH;
4798 opline->op1_type = IS_CONST;
4799 opline->op1.constant = zend_add_class_name_literal(CG(active_op_array),
4800 zend_resolve_class_name_ast(class_ast));
4801
4802 if (zend_string_equals_literal(Z_STR_P(var_name), "this")) {
4803 zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
4804 }
4805
4806 opline->op2_type = IS_CV;
4807 opline->op2.var = lookup_cv(CG(active_op_array), zend_string_copy(Z_STR_P(var_name)));
4808
4809 opline->result.num = is_last_catch && is_last_class;
4810
4811 if (!is_last_class) {
4812 jmp_multicatch[j] = zend_emit_jump(0);
4813 opline = &CG(active_op_array)->opcodes[opnum_catch];
4814 opline->extended_value = get_next_op_number(CG(active_op_array));
4815 }
4816 }
4817
4818 for (j = 0; j < classes->children - 1; j++) {
4819 zend_update_jump_target_to_next(jmp_multicatch[j]);
4820 }
4821
4822 efree(jmp_multicatch);
4823
4824 zend_compile_stmt(stmt_ast);
4825
4826 if (!is_last_catch) {
4827 jmp_opnums[i + 1] = zend_emit_jump(0);
4828 }
4829
4830 opline = &CG(active_op_array)->opcodes[opnum_catch];
4831 if (!is_last_catch) {
4832 opline->extended_value = get_next_op_number(CG(active_op_array));
4833 }
4834 }
4835
4836 for (i = 0; i < catches->children; ++i) {
4837 zend_update_jump_target_to_next(jmp_opnums[i]);
4838 }
4839
4840 if (finally_ast) {
4841 zend_loop_var discard_exception;
4842 uint32_t opnum_jmp = get_next_op_number(CG(active_op_array)) + 1;
4843
4844 /* Pop FAST_CALL from unwind stack */
4845 zend_stack_del_top(&CG(loop_var_stack));
4846
4847 /* Push DISCARD_EXCEPTION on unwind stack */
4848 discard_exception.opcode = ZEND_DISCARD_EXCEPTION;
4849 discard_exception.var_type = IS_TMP_VAR;
4850 discard_exception.var_num = CG(context).fast_call_var;
4851 zend_stack_push(&CG(loop_var_stack), &discard_exception);
4852
4853 CG(zend_lineno) = finally_ast->lineno;
4854
4855 opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
4856 opline->op1.num = try_catch_offset;
4857 opline->result_type = IS_TMP_VAR;
4858 opline->result.var = CG(context).fast_call_var;
4859
4860 zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
4861
4862 CG(context).in_finally++;
4863 zend_compile_stmt(finally_ast);
4864 CG(context).in_finally--;
4865
4866 CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
4867 CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
4868 = get_next_op_number(CG(active_op_array));
4869
4870 opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
4871 opline->op1_type = IS_TMP_VAR;
4872 opline->op1.var = CG(context).fast_call_var;
4873 opline->op2.num = orig_try_catch_offset;
4874
4875 zend_update_jump_target_to_next(opnum_jmp);
4876
4877 CG(context).fast_call_var = orig_fast_call_var;
4878
4879 /* Pop DISCARD_EXCEPTION from unwind stack */
4880 zend_stack_del_top(&CG(loop_var_stack));
4881 }
4882
4883 CG(context).try_catch_offset = orig_try_catch_offset;
4884
4885 efree(jmp_opnums);
4886 }
4887 /* }}} */
4888
4889 /* Encoding declarations must already be handled during parsing */
zend_handle_encoding_declaration(zend_ast * ast)4890 void zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
4891 {
4892 zend_ast_list *declares = zend_ast_get_list(ast);
4893 uint32_t i;
4894 for (i = 0; i < declares->children; ++i) {
4895 zend_ast *declare_ast = declares->child[i];
4896 zend_ast *name_ast = declare_ast->child[0];
4897 zend_ast *value_ast = declare_ast->child[1];
4898 zend_string *name = zend_ast_get_str(name_ast);
4899
4900 if (zend_string_equals_literal_ci(name, "encoding")) {
4901 if (value_ast->kind != ZEND_AST_ZVAL) {
4902 zend_error_noreturn(E_COMPILE_ERROR, "Encoding must be a literal");
4903 }
4904
4905 if (CG(multibyte)) {
4906 zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
4907
4908 const zend_encoding *new_encoding, *old_encoding;
4909 zend_encoding_filter old_input_filter;
4910
4911 CG(encoding_declared) = 1;
4912
4913 new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
4914 if (!new_encoding) {
4915 zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
4916 } else {
4917 old_input_filter = LANG_SCNG(input_filter);
4918 old_encoding = LANG_SCNG(script_encoding);
4919 zend_multibyte_set_filter(new_encoding);
4920
4921 /* need to re-scan if input filter changed */
4922 if (old_input_filter != LANG_SCNG(input_filter) ||
4923 (old_input_filter && new_encoding != old_encoding)) {
4924 zend_multibyte_yyinput_again(old_input_filter, old_encoding);
4925 }
4926 }
4927
4928 zend_string_release(encoding_name);
4929 } else {
4930 zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
4931 "Zend multibyte feature is turned off by settings");
4932 }
4933 }
4934 }
4935 }
4936 /* }}} */
4937
zend_declare_is_first_statement(zend_ast * ast)4938 static int zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
4939 {
4940 uint32_t i = 0;
4941 zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
4942
4943 /* Check to see if this declare is preceeded only by declare statements */
4944 while (i < file_ast->children) {
4945 if (file_ast->child[i] == ast) {
4946 return SUCCESS;
4947 } else if (file_ast->child[i] == NULL) {
4948 /* Empty statements are not allowed prior to a declare */
4949 return FAILURE;
4950 } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
4951 /* declares can only be preceeded by other declares */
4952 return FAILURE;
4953 }
4954 i++;
4955 }
4956 return FAILURE;
4957 }
4958 /* }}} */
4959
zend_compile_declare(zend_ast * ast)4960 void zend_compile_declare(zend_ast *ast) /* {{{ */
4961 {
4962 zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
4963 zend_ast *stmt_ast = ast->child[1];
4964 zend_declarables orig_declarables = FC(declarables);
4965 uint32_t i;
4966
4967 for (i = 0; i < declares->children; ++i) {
4968 zend_ast *declare_ast = declares->child[i];
4969 zend_ast *name_ast = declare_ast->child[0];
4970 zend_ast *value_ast = declare_ast->child[1];
4971 zend_string *name = zend_ast_get_str(name_ast);
4972
4973 if (value_ast->kind != ZEND_AST_ZVAL) {
4974 zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name));
4975 }
4976
4977 if (zend_string_equals_literal_ci(name, "ticks")) {
4978 zval value_zv;
4979 zend_const_expr_to_zval(&value_zv, value_ast);
4980 FC(declarables).ticks = zval_get_long(&value_zv);
4981 zval_dtor(&value_zv);
4982 } else if (zend_string_equals_literal_ci(name, "encoding")) {
4983
4984 if (FAILURE == zend_declare_is_first_statement(ast)) {
4985 zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
4986 "the very first statement in the script");
4987 }
4988 } else if (zend_string_equals_literal_ci(name, "strict_types")) {
4989 zval value_zv;
4990
4991 if (FAILURE == zend_declare_is_first_statement(ast)) {
4992 zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
4993 "the very first statement in the script");
4994 }
4995
4996 if (ast->child[1] != NULL) {
4997 zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
4998 "use block mode");
4999 }
5000
5001 zend_const_expr_to_zval(&value_zv, value_ast);
5002
5003 if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
5004 zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
5005 }
5006
5007 if (Z_LVAL(value_zv) == 1) {
5008 CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
5009 }
5010
5011 } else {
5012 zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
5013 }
5014 }
5015
5016 if (stmt_ast) {
5017 zend_compile_stmt(stmt_ast);
5018
5019 FC(declarables) = orig_declarables;
5020 }
5021 }
5022 /* }}} */
5023
zend_compile_stmt_list(zend_ast * ast)5024 void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
5025 {
5026 zend_ast_list *list = zend_ast_get_list(ast);
5027 uint32_t i;
5028 for (i = 0; i < list->children; ++i) {
5029 zend_compile_stmt(list->child[i]);
5030 }
5031 }
5032 /* }}} */
5033
zend_set_function_arg_flags(zend_function * func)5034 ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
5035 {
5036 uint32_t i, n;
5037
5038 func->common.arg_flags[0] = 0;
5039 func->common.arg_flags[1] = 0;
5040 func->common.arg_flags[2] = 0;
5041 if (func->common.arg_info) {
5042 n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
5043 i = 0;
5044 while (i < n) {
5045 ZEND_SET_ARG_FLAG(func, i + 1, func->common.arg_info[i].pass_by_reference);
5046 i++;
5047 }
5048 if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_VARIADIC && func->common.arg_info[i].pass_by_reference)) {
5049 uint32_t pass_by_reference = func->common.arg_info[i].pass_by_reference;
5050 while (i < MAX_ARG_FLAG_NUM) {
5051 ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
5052 i++;
5053 }
5054 }
5055 }
5056 }
5057 /* }}} */
5058
zend_compile_typename(zend_ast * ast,zend_arg_info * arg_info)5059 static void zend_compile_typename(zend_ast *ast, zend_arg_info *arg_info) /* {{{ */
5060 {
5061 if (ast->kind == ZEND_AST_TYPE) {
5062 arg_info->type_hint = ast->attr;
5063 } else {
5064 zend_string *class_name = zend_ast_get_str(ast);
5065 zend_uchar type = zend_lookup_builtin_type_by_name(class_name);
5066
5067 if (type != 0) {
5068 if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) {
5069 zend_error_noreturn(E_COMPILE_ERROR,
5070 "Scalar type declaration '%s' must be unqualified",
5071 ZSTR_VAL(zend_string_tolower(class_name)));
5072 }
5073 arg_info->type_hint = type;
5074 } else {
5075 uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
5076 if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
5077 class_name = zend_resolve_class_name_ast(ast);
5078 zend_assert_valid_class_name(class_name);
5079 } else {
5080 zend_ensure_valid_class_fetch_type(fetch_type);
5081 zend_string_addref(class_name);
5082 }
5083
5084 arg_info->type_hint = IS_OBJECT;
5085 arg_info->class_name = class_name;
5086 }
5087 }
5088 }
5089 /* }}} */
5090
zend_compile_params(zend_ast * ast,zend_ast * return_type_ast)5091 void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
5092 {
5093 zend_ast_list *list = zend_ast_get_list(ast);
5094 uint32_t i;
5095 zend_op_array *op_array = CG(active_op_array);
5096 zend_arg_info *arg_infos;
5097
5098 if (return_type_ast) {
5099 /* Use op_array->arg_info[-1] for return type */
5100 arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
5101 arg_infos->name = NULL;
5102 arg_infos->pass_by_reference = (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
5103 arg_infos->is_variadic = 0;
5104 arg_infos->type_hint = 0;
5105 arg_infos->allow_null = 0;
5106 arg_infos->class_name = NULL;
5107
5108 if (return_type_ast->attr & ZEND_TYPE_NULLABLE) {
5109 arg_infos->allow_null = 1;
5110 return_type_ast->attr &= ~ZEND_TYPE_NULLABLE;
5111 }
5112
5113 zend_compile_typename(return_type_ast, arg_infos);
5114
5115 if (arg_infos->type_hint == IS_VOID && arg_infos->allow_null) {
5116 zend_error_noreturn(E_COMPILE_ERROR, "Void type cannot be nullable");
5117 }
5118
5119 arg_infos++;
5120 op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
5121 } else {
5122 if (list->children == 0) {
5123 return;
5124 }
5125 arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
5126 }
5127
5128 for (i = 0; i < list->children; ++i) {
5129 zend_ast *param_ast = list->child[i];
5130 zend_ast *type_ast = param_ast->child[0];
5131 zend_ast *var_ast = param_ast->child[1];
5132 zend_ast *default_ast = param_ast->child[2];
5133 zend_string *name = zend_ast_get_str(var_ast);
5134 zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
5135 zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
5136
5137 znode var_node, default_node;
5138 zend_uchar opcode;
5139 zend_op *opline;
5140 zend_arg_info *arg_info;
5141
5142 if (zend_is_auto_global(name)) {
5143 zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
5144 ZSTR_VAL(name));
5145 }
5146
5147 var_node.op_type = IS_CV;
5148 var_node.u.op.var = lookup_cv(CG(active_op_array), zend_string_copy(name));
5149
5150 if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
5151 zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
5152 ZSTR_VAL(name));
5153 } else if (zend_string_equals_literal(name, "this")) {
5154 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter");
5155 }
5156
5157 if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
5158 zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
5159 }
5160
5161 if (is_variadic) {
5162 opcode = ZEND_RECV_VARIADIC;
5163 default_node.op_type = IS_UNUSED;
5164 op_array->fn_flags |= ZEND_ACC_VARIADIC;
5165
5166 if (default_ast) {
5167 zend_error_noreturn(E_COMPILE_ERROR,
5168 "Variadic parameter cannot have a default value");
5169 }
5170 } else if (default_ast) {
5171 /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
5172 uint32_t cops = CG(compiler_options);
5173 CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
5174 opcode = ZEND_RECV_INIT;
5175 default_node.op_type = IS_CONST;
5176 zend_const_expr_to_zval(&default_node.u.constant, default_ast);
5177 CG(compiler_options) = cops;
5178 } else {
5179 opcode = ZEND_RECV;
5180 default_node.op_type = IS_UNUSED;
5181 op_array->required_num_args = i + 1;
5182 }
5183
5184 opline = zend_emit_op(NULL, opcode, NULL, &default_node);
5185 SET_NODE(opline->result, &var_node);
5186 opline->op1.num = i + 1;
5187
5188 arg_info = &arg_infos[i];
5189 arg_info->name = zend_string_copy(name);
5190 arg_info->pass_by_reference = is_ref;
5191 arg_info->is_variadic = is_variadic;
5192 arg_info->type_hint = 0;
5193 arg_info->allow_null = 1;
5194 arg_info->class_name = NULL;
5195
5196 if (type_ast) {
5197 zend_bool has_null_default = default_ast
5198 && (Z_TYPE(default_node.u.constant) == IS_NULL
5199 || (Z_TYPE(default_node.u.constant) == IS_CONSTANT
5200 && strcasecmp(Z_STRVAL(default_node.u.constant), "NULL") == 0));
5201 zend_bool is_explicitly_nullable = (type_ast->attr & ZEND_TYPE_NULLABLE) == ZEND_TYPE_NULLABLE;
5202
5203 op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
5204 arg_info->allow_null = has_null_default || is_explicitly_nullable;
5205
5206 type_ast->attr &= ~ZEND_TYPE_NULLABLE;
5207 zend_compile_typename(type_ast, arg_info);
5208
5209 if (arg_info->type_hint == IS_VOID) {
5210 zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type");
5211 }
5212
5213 if (type_ast->kind == ZEND_AST_TYPE) {
5214 if (arg_info->type_hint == IS_ARRAY) {
5215 if (default_ast && !has_null_default
5216 && Z_TYPE(default_node.u.constant) != IS_ARRAY
5217 && !Z_CONSTANT(default_node.u.constant)
5218 ) {
5219 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5220 "with array type can only be an array or NULL");
5221 }
5222 } else if (arg_info->type_hint == IS_CALLABLE && default_ast) {
5223 if (!has_null_default && !Z_CONSTANT(default_node.u.constant)) {
5224 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5225 "with callable type can only be NULL");
5226 }
5227 }
5228 } else {
5229 if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) {
5230 if (arg_info->class_name) {
5231 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5232 "with a class type can only be NULL");
5233 } else switch (arg_info->type_hint) {
5234 case IS_DOUBLE:
5235 if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) {
5236 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5237 "with a float type can only be float, integer, or NULL");
5238 }
5239 break;
5240
5241 case IS_ITERABLE:
5242 if (Z_TYPE(default_node.u.constant) != IS_ARRAY) {
5243 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5244 "with iterable type can only be an array or NULL");
5245 }
5246 break;
5247
5248 default:
5249 if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) {
5250 zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
5251 "with a %s type can only be %s or NULL",
5252 zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint));
5253 }
5254 break;
5255 }
5256 }
5257 }
5258
5259 /* Allocate cache slot to speed-up run-time class resolution */
5260 if (opline->opcode == ZEND_RECV_INIT) {
5261 if (arg_info->class_name) {
5262 zend_alloc_cache_slot(opline->op2.constant);
5263 } else {
5264 Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = -1;
5265 }
5266 } else {
5267 if (arg_info->class_name) {
5268 opline->op2.num = op_array->cache_size;
5269 op_array->cache_size += sizeof(void*);
5270 } else {
5271 opline->op2.num = -1;
5272 }
5273 }
5274 } else {
5275 if (opline->opcode == ZEND_RECV_INIT) {
5276 Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = -1;
5277 } else {
5278 opline->op2.num = -1;
5279 }
5280 }
5281 }
5282
5283 /* These are assigned at the end to avoid unitialized memory in case of an error */
5284 op_array->num_args = list->children;
5285 op_array->arg_info = arg_infos;
5286
5287 /* Don't count the variadic argument */
5288 if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
5289 op_array->num_args--;
5290 }
5291 zend_set_function_arg_flags((zend_function*)op_array);
5292 }
5293 /* }}} */
5294
zend_compile_closure_binding(znode * closure,zend_ast * uses_ast)5295 static void zend_compile_closure_binding(znode *closure, zend_ast *uses_ast) /* {{{ */
5296 {
5297 zend_ast_list *list = zend_ast_get_list(uses_ast);
5298 uint32_t i;
5299
5300 for (i = 0; i < list->children; ++i) {
5301 zend_ast *var_name_ast = list->child[i];
5302 zend_string *var_name = zend_ast_get_str(var_name_ast);
5303 zend_bool by_ref = var_name_ast->attr;
5304 zend_op *opline;
5305
5306 if (zend_string_equals_literal(var_name, "this")) {
5307 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
5308 }
5309
5310 if (zend_is_auto_global(var_name)) {
5311 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable");
5312 }
5313
5314 opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL);
5315 opline->op2_type = IS_CV;
5316 opline->op2.var = lookup_cv(CG(active_op_array), zend_string_copy(var_name));
5317 opline->extended_value = by_ref;
5318 }
5319 }
5320 /* }}} */
5321
zend_compile_closure_uses(zend_ast * ast)5322 void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
5323 {
5324 zend_op_array *op_array = CG(active_op_array);
5325 zend_ast_list *list = zend_ast_get_list(ast);
5326 uint32_t i;
5327
5328 for (i = 0; i < list->children; ++i) {
5329 zend_ast *var_ast = list->child[i];
5330 zend_string *var_name = zend_ast_get_str(var_ast);
5331 zend_bool by_ref = var_ast->attr;
5332 zval zv;
5333 ZVAL_NULL(&zv);
5334
5335 if (op_array->static_variables
5336 && zend_hash_exists(op_array->static_variables, var_name)) {
5337 zend_error_noreturn(E_COMPILE_ERROR,
5338 "Cannot use variable $%s twice", ZSTR_VAL(var_name));
5339 }
5340
5341 {
5342 int i;
5343 for (i = 0; i < op_array->last_var; i++) {
5344 if (zend_string_equals(op_array->vars[i], var_name)) {
5345 zend_error_noreturn(E_COMPILE_ERROR,
5346 "Cannot use lexical variable $%s as a parameter name", ZSTR_VAL(var_name));
5347 }
5348 }
5349 }
5350
5351 zend_compile_static_var_common(var_ast, &zv, by_ref);
5352 }
5353 }
5354 /* }}} */
5355
zend_begin_method_decl(zend_op_array * op_array,zend_string * name,zend_bool has_body)5356 void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
5357 {
5358 zend_class_entry *ce = CG(active_class_entry);
5359 zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
5360 zend_bool in_trait = (ce->ce_flags & ZEND_ACC_TRAIT) != 0;
5361 zend_bool is_public = (op_array->fn_flags & ZEND_ACC_PUBLIC) != 0;
5362 zend_bool is_static = (op_array->fn_flags & ZEND_ACC_STATIC) != 0;
5363
5364 zend_string *lcname;
5365
5366 if (in_interface) {
5367 if (!is_public || (op_array->fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_ABSTRACT))) {
5368 zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
5369 "%s::%s() must be omitted", ZSTR_VAL(ce->name), ZSTR_VAL(name));
5370 }
5371 op_array->fn_flags |= ZEND_ACC_ABSTRACT;
5372 }
5373
5374 if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
5375 if (op_array->fn_flags & ZEND_ACC_PRIVATE) {
5376 zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
5377 in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
5378 }
5379
5380 if (has_body) {
5381 zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
5382 in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
5383 }
5384
5385 ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
5386 } else if (!has_body) {
5387 zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
5388 ZSTR_VAL(ce->name), ZSTR_VAL(name));
5389 }
5390
5391 op_array->scope = ce;
5392 op_array->function_name = zend_string_copy(name);
5393
5394 lcname = zend_string_tolower(name);
5395 lcname = zend_new_interned_string(lcname);
5396
5397 if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
5398 zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
5399 ZSTR_VAL(ce->name), ZSTR_VAL(name));
5400 }
5401
5402 if (in_interface) {
5403 if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
5404 if (!is_public || is_static) {
5405 zend_error(E_WARNING, "The magic method __call() must have "
5406 "public visibility and cannot be static");
5407 }
5408 } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
5409 if (!is_public || !is_static) {
5410 zend_error(E_WARNING, "The magic method __callStatic() must have "
5411 "public visibility and be static");
5412 }
5413 } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
5414 if (!is_public || is_static) {
5415 zend_error(E_WARNING, "The magic method __get() must have "
5416 "public visibility and cannot be static");
5417 }
5418 } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
5419 if (!is_public || is_static) {
5420 zend_error(E_WARNING, "The magic method __set() must have "
5421 "public visibility and cannot be static");
5422 }
5423 } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
5424 if (!is_public || is_static) {
5425 zend_error(E_WARNING, "The magic method __unset() must have "
5426 "public visibility and cannot be static");
5427 }
5428 } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
5429 if (!is_public || is_static) {
5430 zend_error(E_WARNING, "The magic method __isset() must have "
5431 "public visibility and cannot be static");
5432 }
5433 } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
5434 if (!is_public || is_static) {
5435 zend_error(E_WARNING, "The magic method __toString() must have "
5436 "public visibility and cannot be static");
5437 }
5438 } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
5439 if (!is_public || is_static) {
5440 zend_error(E_WARNING, "The magic method __invoke() must have "
5441 "public visibility and cannot be static");
5442 }
5443 } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
5444 if (!is_public || is_static) {
5445 zend_error(E_WARNING, "The magic method __debugInfo() must have "
5446 "public visibility and cannot be static");
5447 }
5448 }
5449 } else {
5450 if (!in_trait && zend_string_equals_ci(lcname, ce->name)) {
5451 if (!ce->constructor) {
5452 ce->constructor = (zend_function *) op_array;
5453 }
5454 } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
5455 ce->constructor = (zend_function *) op_array;
5456 } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
5457 ce->destructor = (zend_function *) op_array;
5458 } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
5459 ce->clone = (zend_function *) op_array;
5460 } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
5461 if (!is_public || is_static) {
5462 zend_error(E_WARNING, "The magic method __call() must have "
5463 "public visibility and cannot be static");
5464 }
5465 ce->__call = (zend_function *) op_array;
5466 } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
5467 if (!is_public || !is_static) {
5468 zend_error(E_WARNING, "The magic method __callStatic() must have "
5469 "public visibility and be static");
5470 }
5471 ce->__callstatic = (zend_function *) op_array;
5472 } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
5473 if (!is_public || is_static) {
5474 zend_error(E_WARNING, "The magic method __get() must have "
5475 "public visibility and cannot be static");
5476 }
5477 ce->__get = (zend_function *) op_array;
5478 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
5479 } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
5480 if (!is_public || is_static) {
5481 zend_error(E_WARNING, "The magic method __set() must have "
5482 "public visibility and cannot be static");
5483 }
5484 ce->__set = (zend_function *) op_array;
5485 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
5486 } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
5487 if (!is_public || is_static) {
5488 zend_error(E_WARNING, "The magic method __unset() must have "
5489 "public visibility and cannot be static");
5490 }
5491 ce->__unset = (zend_function *) op_array;
5492 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
5493 } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
5494 if (!is_public || is_static) {
5495 zend_error(E_WARNING, "The magic method __isset() must have "
5496 "public visibility and cannot be static");
5497 }
5498 ce->__isset = (zend_function *) op_array;
5499 ce->ce_flags |= ZEND_ACC_USE_GUARDS;
5500 } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
5501 if (!is_public || is_static) {
5502 zend_error(E_WARNING, "The magic method __toString() must have "
5503 "public visibility and cannot be static");
5504 }
5505 ce->__tostring = (zend_function *) op_array;
5506 } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
5507 if (!is_public || is_static) {
5508 zend_error(E_WARNING, "The magic method __invoke() must have "
5509 "public visibility and cannot be static");
5510 }
5511 } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
5512 if (!is_public || is_static) {
5513 zend_error(E_WARNING, "The magic method __debugInfo() must have "
5514 "public visibility and cannot be static");
5515 }
5516 ce->__debugInfo = (zend_function *) op_array;
5517 } else if (!is_static) {
5518 op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;
5519 }
5520 }
5521
5522 zend_string_release(lcname);
5523 }
5524 /* }}} */
5525
zend_begin_func_decl(znode * result,zend_op_array * op_array,zend_ast_decl * decl)5526 static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl) /* {{{ */
5527 {
5528 zend_ast *params_ast = decl->child[0];
5529 zend_string *unqualified_name, *name, *lcname, *key;
5530 zend_op *opline;
5531
5532 unqualified_name = decl->name;
5533 op_array->function_name = name = zend_prefix_with_ns(unqualified_name);
5534 lcname = zend_string_tolower(name);
5535
5536 if (FC(imports_function)) {
5537 zend_string *import_name = zend_hash_find_ptr_lc(
5538 FC(imports_function), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
5539 if (import_name && !zend_string_equals_ci(lcname, import_name)) {
5540 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
5541 "because the name is already in use", ZSTR_VAL(name));
5542 }
5543 }
5544
5545 if (zend_string_equals_literal(lcname, ZEND_AUTOLOAD_FUNC_NAME)
5546 && zend_ast_get_list(params_ast)->children != 1
5547 ) {
5548 zend_error_noreturn(E_COMPILE_ERROR, "%s() must take exactly 1 argument",
5549 ZEND_AUTOLOAD_FUNC_NAME);
5550 }
5551
5552 key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
5553 zend_hash_update_ptr(CG(function_table), key, op_array);
5554
5555 if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
5556 opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
5557 opline->op1_type = IS_CONST;
5558 LITERAL_STR(opline->op1, key);
5559 } else {
5560 opline = get_next_op(CG(active_op_array));
5561 opline->opcode = ZEND_DECLARE_FUNCTION;
5562 opline->op1_type = IS_CONST;
5563 LITERAL_STR(opline->op1, zend_string_copy(lcname));
5564 /* RTD key is placed after lcname literal in op1 */
5565 zend_add_literal_string(CG(active_op_array), &key);
5566 SET_UNUSED(opline->op2);
5567 }
5568
5569 zend_string_release(lcname);
5570 }
5571 /* }}} */
5572
zend_compile_func_decl(znode * result,zend_ast * ast)5573 void zend_compile_func_decl(znode *result, zend_ast *ast) /* {{{ */
5574 {
5575 zend_ast_decl *decl = (zend_ast_decl *) ast;
5576 zend_ast *params_ast = decl->child[0];
5577 zend_ast *uses_ast = decl->child[1];
5578 zend_ast *stmt_ast = decl->child[2];
5579 zend_ast *return_type_ast = decl->child[3];
5580 zend_bool is_method = decl->kind == ZEND_AST_METHOD;
5581
5582 zend_op_array *orig_op_array = CG(active_op_array);
5583 zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
5584 zend_oparray_context orig_oparray_context;
5585
5586 init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
5587
5588 op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
5589 op_array->fn_flags |= decl->flags;
5590 op_array->line_start = decl->start_lineno;
5591 op_array->line_end = decl->end_lineno;
5592 if (decl->doc_comment) {
5593 op_array->doc_comment = zend_string_copy(decl->doc_comment);
5594 }
5595 if (decl->kind == ZEND_AST_CLOSURE) {
5596 op_array->fn_flags |= ZEND_ACC_CLOSURE;
5597 }
5598
5599 if (is_method) {
5600 zend_bool has_body = stmt_ast != NULL;
5601 zend_begin_method_decl(op_array, decl->name, has_body);
5602 } else {
5603 zend_begin_func_decl(result, op_array, decl);
5604 if (uses_ast) {
5605 zend_compile_closure_binding(result, uses_ast);
5606 }
5607 }
5608
5609 CG(active_op_array) = op_array;
5610
5611 zend_oparray_context_begin(&orig_oparray_context);
5612
5613 if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
5614 zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
5615 opline_ext->lineno = decl->start_lineno;
5616 }
5617
5618 {
5619 /* Push a separator to the loop variable stack */
5620 zend_loop_var dummy_var;
5621 dummy_var.opcode = ZEND_RETURN;
5622
5623 zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
5624 }
5625
5626 zend_compile_params(params_ast, return_type_ast);
5627 if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
5628 zend_mark_function_as_generator();
5629 zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
5630 }
5631 if (uses_ast) {
5632 zend_compile_closure_uses(uses_ast);
5633 }
5634 zend_compile_stmt(stmt_ast);
5635
5636 if (is_method) {
5637 zend_check_magic_method_implementation(
5638 CG(active_class_entry), (zend_function *) op_array, E_COMPILE_ERROR);
5639 }
5640
5641 /* put the implicit return on the really last line */
5642 CG(zend_lineno) = decl->end_lineno;
5643
5644 zend_do_extended_info();
5645 zend_emit_final_return(0);
5646
5647 pass_two(CG(active_op_array));
5648 zend_oparray_context_end(&orig_oparray_context);
5649
5650 /* Pop the loop variable stack separator */
5651 zend_stack_del_top(&CG(loop_var_stack));
5652
5653 CG(active_op_array) = orig_op_array;
5654 }
5655 /* }}} */
5656
zend_compile_prop_decl(zend_ast * ast)5657 void zend_compile_prop_decl(zend_ast *ast) /* {{{ */
5658 {
5659 zend_ast_list *list = zend_ast_get_list(ast);
5660 uint32_t flags = list->attr;
5661 zend_class_entry *ce = CG(active_class_entry);
5662 uint32_t i, children = list->children;
5663
5664 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5665 zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
5666 }
5667
5668 if (flags & ZEND_ACC_ABSTRACT) {
5669 zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
5670 }
5671
5672 for (i = 0; i < children; ++i) {
5673 zend_ast *prop_ast = list->child[i];
5674 zend_ast *name_ast = prop_ast->child[0];
5675 zend_ast *value_ast = prop_ast->child[1];
5676 zend_ast *doc_comment_ast = prop_ast->child[2];
5677 zend_string *name = zend_ast_get_str(name_ast);
5678 zend_string *doc_comment = NULL;
5679 zval value_zv;
5680
5681 /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */
5682 if (doc_comment_ast) {
5683 doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast));
5684 }
5685
5686 if (flags & ZEND_ACC_FINAL) {
5687 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
5688 "the final modifier is allowed only for methods and classes",
5689 ZSTR_VAL(ce->name), ZSTR_VAL(name));
5690 }
5691
5692 if (zend_hash_exists(&ce->properties_info, name)) {
5693 zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
5694 ZSTR_VAL(ce->name), ZSTR_VAL(name));
5695 }
5696
5697 if (value_ast) {
5698 zend_const_expr_to_zval(&value_zv, value_ast);
5699 } else {
5700 ZVAL_NULL(&value_zv);
5701 }
5702
5703 name = zend_new_interned_string_safe(name);
5704 zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment);
5705 }
5706 }
5707 /* }}} */
5708
zend_compile_class_const_decl(zend_ast * ast)5709 void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
5710 {
5711 zend_ast_list *list = zend_ast_get_list(ast);
5712 zend_class_entry *ce = CG(active_class_entry);
5713 uint32_t i;
5714
5715 if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
5716 zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
5717 return;
5718 }
5719
5720 for (i = 0; i < list->children; ++i) {
5721 zend_ast *const_ast = list->child[i];
5722 zend_ast *name_ast = const_ast->child[0];
5723 zend_ast *value_ast = const_ast->child[1];
5724 zend_ast *doc_comment_ast = const_ast->child[2];
5725 zend_string *name = zend_ast_get_str(name_ast);
5726 zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
5727 zval value_zv;
5728
5729 if (UNEXPECTED(ast->attr & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
5730 if (ast->attr & ZEND_ACC_STATIC) {
5731 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as constant modifier");
5732 } else if (ast->attr & ZEND_ACC_ABSTRACT) {
5733 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as constant modifier");
5734 } else if (ast->attr & ZEND_ACC_FINAL) {
5735 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as constant modifier");
5736 }
5737 }
5738
5739 zend_const_expr_to_zval(&value_zv, value_ast);
5740
5741 name = zend_new_interned_string_safe(name);
5742 zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment);
5743 }
5744 }
5745 /* }}} */
5746
zend_compile_method_ref(zend_ast * ast)5747 static zend_trait_method_reference *zend_compile_method_ref(zend_ast *ast) /* {{{ */
5748 {
5749 zend_ast *class_ast = ast->child[0];
5750 zend_ast *method_ast = ast->child[1];
5751
5752 zend_trait_method_reference *method_ref = emalloc(sizeof(zend_trait_method_reference));
5753 method_ref->ce = NULL;
5754 method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
5755
5756 if (class_ast) {
5757 method_ref->class_name = zend_resolve_class_name_ast(class_ast);
5758 } else {
5759 method_ref->class_name = NULL;
5760 }
5761
5762 return method_ref;
5763 }
5764 /* }}} */
5765
zend_compile_name_list(zend_ast * ast)5766 static zend_string **zend_compile_name_list(zend_ast *ast) /* {{{ */
5767 {
5768 zend_ast_list *list = zend_ast_get_list(ast);
5769 zend_string **names = safe_emalloc(sizeof(zend_string *), list->children + 1, 0);
5770 uint32_t i;
5771
5772 for (i = 0; i < list->children; ++i) {
5773 zend_ast *name_ast = list->child[i];
5774 names[i] = zend_resolve_class_name_ast(name_ast);
5775 }
5776
5777 names[list->children] = NULL;
5778
5779 return names;
5780 }
5781 /* }}} */
5782
zend_compile_trait_precedence(zend_ast * ast)5783 static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
5784 {
5785 zend_ast *method_ref_ast = ast->child[0];
5786 zend_ast *insteadof_ast = ast->child[1];
5787
5788 zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence));
5789 precedence->trait_method = zend_compile_method_ref(method_ref_ast);
5790 precedence->exclude_from_classes
5791 = (void *) zend_compile_name_list(insteadof_ast);
5792
5793 zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
5794 }
5795 /* }}} */
5796
zend_compile_trait_alias(zend_ast * ast)5797 static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
5798 {
5799 zend_ast *method_ref_ast = ast->child[0];
5800 zend_ast *alias_ast = ast->child[1];
5801 uint32_t modifiers = ast->attr;
5802
5803 zend_trait_alias *alias;
5804
5805 if (modifiers == ZEND_ACC_STATIC) {
5806 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as method modifier");
5807 } else if (modifiers == ZEND_ACC_ABSTRACT) {
5808 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as method modifier");
5809 } else if (modifiers == ZEND_ACC_FINAL) {
5810 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as method modifier");
5811 }
5812
5813 alias = emalloc(sizeof(zend_trait_alias));
5814 alias->trait_method = zend_compile_method_ref(method_ref_ast);
5815 alias->modifiers = modifiers;
5816
5817 if (alias_ast) {
5818 alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
5819 } else {
5820 alias->alias = NULL;
5821 }
5822
5823 zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
5824 }
5825 /* }}} */
5826
zend_compile_use_trait(zend_ast * ast)5827 void zend_compile_use_trait(zend_ast *ast) /* {{{ */
5828 {
5829 zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
5830 zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
5831 zend_class_entry *ce = CG(active_class_entry);
5832 zend_op *opline;
5833 uint32_t i;
5834
5835 for (i = 0; i < traits->children; ++i) {
5836 zend_ast *trait_ast = traits->child[i];
5837 zend_string *name = zend_ast_get_str(trait_ast);
5838
5839 if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5840 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
5841 "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
5842 }
5843
5844 switch (zend_get_class_fetch_type(name)) {
5845 case ZEND_FETCH_CLASS_SELF:
5846 case ZEND_FETCH_CLASS_PARENT:
5847 case ZEND_FETCH_CLASS_STATIC:
5848 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as trait name "
5849 "as it is reserved", ZSTR_VAL(name));
5850 break;
5851 }
5852
5853 opline = get_next_op(CG(active_op_array));
5854 opline->opcode = ZEND_ADD_TRAIT;
5855 SET_NODE(opline->op1, &FC(implementing_class));
5856 opline->op2_type = IS_CONST;
5857 opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
5858 zend_resolve_class_name_ast(trait_ast));
5859
5860 ce->num_traits++;
5861 }
5862
5863 if (!adaptations) {
5864 return;
5865 }
5866
5867 for (i = 0; i < adaptations->children; ++i) {
5868 zend_ast *adaptation_ast = adaptations->child[i];
5869 switch (adaptation_ast->kind) {
5870 case ZEND_AST_TRAIT_PRECEDENCE:
5871 zend_compile_trait_precedence(adaptation_ast);
5872 break;
5873 case ZEND_AST_TRAIT_ALIAS:
5874 zend_compile_trait_alias(adaptation_ast);
5875 break;
5876 EMPTY_SWITCH_DEFAULT_CASE()
5877 }
5878 }
5879 }
5880 /* }}} */
5881
zend_compile_implements(znode * class_node,zend_ast * ast)5882 void zend_compile_implements(znode *class_node, zend_ast *ast) /* {{{ */
5883 {
5884 zend_ast_list *list = zend_ast_get_list(ast);
5885 uint32_t i;
5886 for (i = 0; i < list->children; ++i) {
5887 zend_ast *class_ast = list->child[i];
5888 zend_string *name = zend_ast_get_str(class_ast);
5889
5890 zend_op *opline;
5891
5892 if (!zend_is_const_default_class_ref(class_ast)) {
5893 zend_error_noreturn(E_COMPILE_ERROR,
5894 "Cannot use '%s' as interface name as it is reserved", ZSTR_VAL(name));
5895 }
5896
5897 opline = zend_emit_op(NULL, ZEND_ADD_INTERFACE, class_node, NULL);
5898 opline->op2_type = IS_CONST;
5899 opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
5900 zend_resolve_class_name_ast(class_ast));
5901
5902 CG(active_class_entry)->num_interfaces++;
5903 }
5904 }
5905 /* }}} */
5906
zend_generate_anon_class_name(unsigned char * lex_pos)5907 static zend_string *zend_generate_anon_class_name(unsigned char *lex_pos) /* {{{ */
5908 {
5909 zend_string *result;
5910 char char_pos_buf[32];
5911 size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
5912 zend_string *filename = CG(active_op_array)->filename;
5913
5914 /* NULL, name length, filename length, last accepting char position length */
5915 result = zend_string_alloc(sizeof("class@anonymous") + ZSTR_LEN(filename) + char_pos_len, 0);
5916 sprintf(ZSTR_VAL(result), "class@anonymous%c%s%s", '\0', ZSTR_VAL(filename), char_pos_buf);
5917 return zend_new_interned_string(result);
5918 }
5919 /* }}} */
5920
zend_compile_class_decl(zend_ast * ast)5921 void zend_compile_class_decl(zend_ast *ast) /* {{{ */
5922 {
5923 zend_ast_decl *decl = (zend_ast_decl *) ast;
5924 zend_ast *extends_ast = decl->child[0];
5925 zend_ast *implements_ast = decl->child[1];
5926 zend_ast *stmt_ast = decl->child[2];
5927 zend_string *name, *lcname;
5928 zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
5929 zend_op *opline;
5930 znode declare_node, extends_node;
5931
5932 zend_class_entry *original_ce = CG(active_class_entry);
5933 znode original_implementing_class = FC(implementing_class);
5934
5935 if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
5936 zend_string *unqualified_name = decl->name;
5937
5938 if (CG(active_class_entry)) {
5939 zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
5940 }
5941
5942 zend_assert_valid_class_name(unqualified_name);
5943 name = zend_prefix_with_ns(unqualified_name);
5944 name = zend_new_interned_string(name);
5945 lcname = zend_string_tolower(name);
5946
5947 if (FC(imports)) {
5948 zend_string *import_name = zend_hash_find_ptr_lc(
5949 FC(imports), ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name));
5950 if (import_name && !zend_string_equals_ci(lcname, import_name)) {
5951 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
5952 "because the name is already in use", ZSTR_VAL(name));
5953 }
5954 }
5955 } else {
5956 name = zend_generate_anon_class_name(decl->lex_pos);
5957 lcname = zend_string_tolower(name);
5958 }
5959 lcname = zend_new_interned_string(lcname);
5960
5961 ce->type = ZEND_USER_CLASS;
5962 ce->name = name;
5963 zend_initialize_class_data(ce, 1);
5964
5965 ce->ce_flags |= decl->flags;
5966 ce->info.user.filename = zend_get_compiled_filename();
5967 ce->info.user.line_start = decl->start_lineno;
5968 ce->info.user.line_end = decl->end_lineno;
5969
5970 if (decl->doc_comment) {
5971 ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
5972 }
5973
5974 if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
5975 /* Serialization is not supported for anonymous classes */
5976 ce->serialize = zend_class_serialize_deny;
5977 ce->unserialize = zend_class_unserialize_deny;
5978 }
5979
5980 if (extends_ast) {
5981 if (!zend_is_const_default_class_ref(extends_ast)) {
5982 zend_string *extends_name = zend_ast_get_str(extends_ast);
5983 zend_error_noreturn(E_COMPILE_ERROR,
5984 "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(extends_name));
5985 }
5986
5987 zend_compile_class_ref(&extends_node, extends_ast, 0);
5988 ce->ce_flags |= ZEND_ACC_INHERITED;
5989 }
5990
5991 opline = get_next_op(CG(active_op_array));
5992 zend_make_var_result(&declare_node, opline);
5993
5994 GET_NODE(&FC(implementing_class), opline->result);
5995
5996 opline->op1_type = IS_CONST;
5997 LITERAL_STR(opline->op1, lcname);
5998
5999 if (decl->flags & ZEND_ACC_ANON_CLASS) {
6000 if (extends_ast) {
6001 opline->opcode = ZEND_DECLARE_ANON_INHERITED_CLASS;
6002 SET_NODE(opline->op2, &extends_node);
6003 } else {
6004 opline->opcode = ZEND_DECLARE_ANON_CLASS;
6005 }
6006
6007 if (!zend_hash_exists(CG(class_table), lcname)) {
6008 zend_hash_add_ptr(CG(class_table), lcname, ce);
6009 } else {
6010 /* this anonymous class has been included */
6011 zval zv;
6012 ZVAL_PTR(&zv, ce);
6013 destroy_zend_class(&zv);
6014 return;
6015 }
6016 } else {
6017 zend_string *key;
6018
6019 if (extends_ast) {
6020 opline->opcode = ZEND_DECLARE_INHERITED_CLASS;
6021 SET_NODE(opline->op2, &extends_node);
6022 } else {
6023 opline->opcode = ZEND_DECLARE_CLASS;
6024 SET_UNUSED(opline->op2);
6025 }
6026
6027 key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
6028 /* RTD key is placed after lcname literal in op1 */
6029 zend_add_literal_string(CG(active_op_array), &key);
6030
6031 zend_hash_update_ptr(CG(class_table), key, ce);
6032 }
6033
6034 CG(active_class_entry) = ce;
6035
6036 zend_compile_stmt(stmt_ast);
6037
6038 /* Reset lineno for final opcodes and errors */
6039 CG(zend_lineno) = ast->lineno;
6040
6041 if (ce->num_traits == 0) {
6042 /* For traits this check is delayed until after trait binding */
6043 zend_check_deprecated_constructor(ce);
6044 }
6045
6046 if (ce->constructor) {
6047 ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
6048 if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
6049 zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
6050 ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
6051 }
6052 if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
6053 zend_error_noreturn(E_COMPILE_ERROR,
6054 "Constructor %s::%s() cannot declare a return type",
6055 ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
6056 }
6057 }
6058 if (ce->destructor) {
6059 ce->destructor->common.fn_flags |= ZEND_ACC_DTOR;
6060 if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
6061 zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
6062 ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
6063 } else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
6064 zend_error_noreturn(E_COMPILE_ERROR,
6065 "Destructor %s::%s() cannot declare a return type",
6066 ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
6067 }
6068 }
6069 if (ce->clone) {
6070 ce->clone->common.fn_flags |= ZEND_ACC_CLONE;
6071 if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
6072 zend_error_noreturn(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static",
6073 ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
6074 } else if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
6075 zend_error_noreturn(E_COMPILE_ERROR,
6076 "%s::%s() cannot declare a return type",
6077 ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
6078 }
6079 }
6080
6081 /* Check for traits and proceed like with interfaces.
6082 * The only difference will be a combined handling of them in the end.
6083 * Thus, we need another opcode here. */
6084 if (ce->num_traits > 0) {
6085 ce->traits = NULL;
6086 ce->num_traits = 0;
6087 ce->ce_flags |= ZEND_ACC_IMPLEMENT_TRAITS;
6088
6089 zend_emit_op(NULL, ZEND_BIND_TRAITS, &declare_node, NULL);
6090 }
6091
6092 if (implements_ast) {
6093 zend_compile_implements(&declare_node, implements_ast);
6094 }
6095
6096 if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
6097 && (extends_ast || implements_ast)
6098 ) {
6099 zend_verify_abstract_class(ce);
6100 if (implements_ast) {
6101 zend_emit_op(NULL, ZEND_VERIFY_ABSTRACT_CLASS, &declare_node, NULL);
6102 }
6103 }
6104
6105 /* Inherit interfaces; reset number to zero, we need it for above check and
6106 * will restore it during actual implementation.
6107 * The ZEND_ACC_IMPLEMENT_INTERFACES flag disables double call to
6108 * zend_verify_abstract_class() */
6109 if (ce->num_interfaces > 0) {
6110 ce->interfaces = NULL;
6111 ce->num_interfaces = 0;
6112 ce->ce_flags |= ZEND_ACC_IMPLEMENT_INTERFACES;
6113 }
6114
6115 FC(implementing_class) = original_implementing_class;
6116 CG(active_class_entry) = original_ce;
6117 }
6118 /* }}} */
6119
zend_get_import_ht(uint32_t type)6120 static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
6121 {
6122 switch (type) {
6123 case T_CLASS:
6124 if (!FC(imports)) {
6125 FC(imports) = emalloc(sizeof(HashTable));
6126 zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
6127 }
6128 return FC(imports);
6129 case T_FUNCTION:
6130 if (!FC(imports_function)) {
6131 FC(imports_function) = emalloc(sizeof(HashTable));
6132 zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
6133 }
6134 return FC(imports_function);
6135 case T_CONST:
6136 if (!FC(imports_const)) {
6137 FC(imports_const) = emalloc(sizeof(HashTable));
6138 zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
6139 }
6140 return FC(imports_const);
6141 EMPTY_SWITCH_DEFAULT_CASE()
6142 }
6143
6144 return NULL;
6145 }
6146 /* }}} */
6147
zend_get_use_type_str(uint32_t type)6148 static char *zend_get_use_type_str(uint32_t type) /* {{{ */
6149 {
6150 switch (type) {
6151 case T_CLASS:
6152 return "";
6153 case T_FUNCTION:
6154 return " function";
6155 case T_CONST:
6156 return " const";
6157 EMPTY_SWITCH_DEFAULT_CASE()
6158 }
6159
6160 return " unknown";
6161 }
6162 /* }}} */
6163
zend_check_already_in_use(uint32_t type,zend_string * old_name,zend_string * new_name,zend_string * check_name)6164 static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
6165 {
6166 if (zend_string_equals_ci(old_name, check_name)) {
6167 return;
6168 }
6169
6170 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
6171 "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
6172 }
6173 /* }}} */
6174
zend_check_use_conflict(uint32_t type,zend_string * old_name,zend_string * new_name,zend_string * lookup_name)6175 static void zend_check_use_conflict(
6176 uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *lookup_name) {
6177 switch (type) {
6178 case T_CLASS:
6179 {
6180 zend_class_entry *ce = zend_hash_find_ptr(CG(class_table), lookup_name);
6181 if (ce && ce->type == ZEND_USER_CLASS
6182 && ce->info.user.filename == CG(compiled_filename)
6183 ) {
6184 zend_check_already_in_use(type, old_name, new_name, lookup_name);
6185 }
6186 break;
6187 }
6188 case T_FUNCTION:
6189 {
6190 zend_function *fn = zend_hash_find_ptr(CG(function_table), lookup_name);
6191 if (fn && fn->type == ZEND_USER_FUNCTION
6192 && fn->op_array.filename == CG(compiled_filename)
6193 ) {
6194 zend_check_already_in_use(type, old_name, new_name, lookup_name);
6195 }
6196 break;
6197 }
6198 case T_CONST:
6199 {
6200 zend_string *filename = zend_hash_find_ptr(&CG(const_filenames), lookup_name);
6201 if (filename && filename == CG(compiled_filename)) {
6202 zend_check_already_in_use(type, old_name, new_name, lookup_name);
6203 }
6204 break;
6205 }
6206 EMPTY_SWITCH_DEFAULT_CASE()
6207 }
6208 }
6209
zend_compile_use(zend_ast * ast)6210 void zend_compile_use(zend_ast *ast) /* {{{ */
6211 {
6212 zend_ast_list *list = zend_ast_get_list(ast);
6213 uint32_t i;
6214 zend_string *current_ns = FC(current_namespace);
6215 uint32_t type = ast->attr;
6216 HashTable *current_import = zend_get_import_ht(type);
6217 zend_bool case_sensitive = type == T_CONST;
6218
6219 for (i = 0; i < list->children; ++i) {
6220 zend_ast *use_ast = list->child[i];
6221 zend_ast *old_name_ast = use_ast->child[0];
6222 zend_ast *new_name_ast = use_ast->child[1];
6223 zend_string *old_name = zend_ast_get_str(old_name_ast);
6224 zend_string *new_name, *lookup_name;
6225
6226 if (new_name_ast) {
6227 new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
6228 } else {
6229 const char *unqualified_name;
6230 size_t unqualified_name_len;
6231 if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
6232 /* The form "use A\B" is equivalent to "use A\B as B" */
6233 new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
6234 } else {
6235 new_name = zend_string_copy(old_name);
6236
6237 if (!current_ns) {
6238 if (type == T_CLASS && zend_string_equals_literal(new_name, "strict")) {
6239 zend_error_noreturn(E_COMPILE_ERROR,
6240 "You seem to be trying to use a different language...");
6241 }
6242
6243 zend_error(E_WARNING, "The use statement with non-compound name '%s' "
6244 "has no effect", ZSTR_VAL(new_name));
6245 }
6246 }
6247 }
6248
6249 if (case_sensitive) {
6250 lookup_name = zend_string_copy(new_name);
6251 } else {
6252 lookup_name = zend_string_tolower(new_name);
6253 }
6254
6255 if (type == T_CLASS && zend_is_reserved_class_name(new_name)) {
6256 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
6257 "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
6258 }
6259
6260 if (current_ns) {
6261 zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
6262 zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
6263 ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
6264 memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1);
6265
6266 zend_check_use_conflict(type, old_name, new_name, ns_name);
6267
6268 zend_string_free(ns_name);
6269 } else {
6270 zend_check_use_conflict(type, old_name, new_name, lookup_name);
6271 }
6272
6273 zend_string_addref(old_name);
6274 if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
6275 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
6276 "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
6277 }
6278
6279 zend_string_release(lookup_name);
6280 zend_string_release(new_name);
6281 }
6282 }
6283 /* }}} */
6284
zend_compile_group_use(zend_ast * ast)6285 void zend_compile_group_use(zend_ast *ast) /* {{{ */
6286 {
6287 uint32_t i;
6288 zend_string *ns = zend_ast_get_str(ast->child[0]);
6289 zend_ast_list *list = zend_ast_get_list(ast->child[1]);
6290
6291 for (i = 0; i < list->children; i++) {
6292 zend_ast *inline_use, *use = list->child[i];
6293 zval *name_zval = zend_ast_get_zval(use->child[0]);
6294 zend_string *name = Z_STR_P(name_zval);
6295 zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
6296 zend_string_release(name);
6297 ZVAL_STR(name_zval, compound_ns);
6298 inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
6299 inline_use->attr = ast->attr ? ast->attr : use->attr;
6300 zend_compile_use(inline_use);
6301 }
6302 }
6303 /* }}} */
6304
zend_compile_const_decl(zend_ast * ast)6305 void zend_compile_const_decl(zend_ast *ast) /* {{{ */
6306 {
6307 zend_ast_list *list = zend_ast_get_list(ast);
6308 uint32_t i;
6309 for (i = 0; i < list->children; ++i) {
6310 zend_ast *const_ast = list->child[i];
6311 zend_ast *name_ast = const_ast->child[0];
6312 zend_ast *value_ast = const_ast->child[1];
6313 zend_string *unqualified_name = zend_ast_get_str(name_ast);
6314
6315 zend_string *name;
6316 znode name_node, value_node;
6317 zval *value_zv = &value_node.u.constant;
6318
6319 value_node.op_type = IS_CONST;
6320 zend_const_expr_to_zval(value_zv, value_ast);
6321
6322 if (zend_lookup_reserved_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) {
6323 zend_error_noreturn(E_COMPILE_ERROR,
6324 "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name));
6325 }
6326
6327 name = zend_prefix_with_ns(unqualified_name);
6328 name = zend_new_interned_string(name);
6329
6330 if (FC(imports_const)) {
6331 zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name);
6332 if (import_name && !zend_string_equals(import_name, name)) {
6333 zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because "
6334 "the name is already in use", ZSTR_VAL(name));
6335 }
6336 }
6337
6338 name_node.op_type = IS_CONST;
6339 ZVAL_STR(&name_node.u.constant, name);
6340
6341 zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
6342
6343 zend_hash_add_ptr(&CG(const_filenames), name, CG(compiled_filename));
6344 }
6345 }
6346 /* }}}*/
6347
zend_compile_namespace(zend_ast * ast)6348 void zend_compile_namespace(zend_ast *ast) /* {{{ */
6349 {
6350 zend_ast *name_ast = ast->child[0];
6351 zend_ast *stmt_ast = ast->child[1];
6352 zend_string *name;
6353 zend_bool with_bracket = stmt_ast != NULL;
6354
6355 /* handle mixed syntax declaration or nested namespaces */
6356 if (!FC(has_bracketed_namespaces)) {
6357 if (FC(current_namespace)) {
6358 /* previous namespace declarations were unbracketed */
6359 if (with_bracket) {
6360 zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
6361 "with unbracketed namespace declarations");
6362 }
6363 }
6364 } else {
6365 /* previous namespace declarations were bracketed */
6366 if (!with_bracket) {
6367 zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
6368 "with unbracketed namespace declarations");
6369 } else if (FC(current_namespace) || FC(in_namespace)) {
6370 zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
6371 }
6372 }
6373
6374 if (((!with_bracket && !FC(current_namespace))
6375 || (with_bracket && !FC(has_bracketed_namespaces))) && CG(active_op_array)->last > 0
6376 ) {
6377 /* ignore ZEND_EXT_STMT and ZEND_TICKS */
6378 uint32_t num = CG(active_op_array)->last;
6379 while (num > 0 &&
6380 (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
6381 CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
6382 --num;
6383 }
6384 if (num > 0) {
6385 zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
6386 "the very first statement or after any declare call in the script");
6387 }
6388 }
6389
6390 if (FC(current_namespace)) {
6391 zend_string_release(FC(current_namespace));
6392 }
6393
6394 if (name_ast) {
6395 name = zend_ast_get_str(name_ast);
6396
6397 if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
6398 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name));
6399 }
6400
6401 FC(current_namespace) = zend_string_copy(name);
6402 } else {
6403 FC(current_namespace) = NULL;
6404 }
6405
6406 zend_reset_import_tables();
6407
6408 FC(in_namespace) = 1;
6409 if (with_bracket) {
6410 FC(has_bracketed_namespaces) = 1;
6411 }
6412
6413 if (stmt_ast) {
6414 zend_compile_top_stmt(stmt_ast);
6415 zend_end_namespace();
6416 }
6417 }
6418 /* }}} */
6419
zend_compile_halt_compiler(zend_ast * ast)6420 void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
6421 {
6422 zend_ast *offset_ast = ast->child[0];
6423 zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
6424
6425 zend_string *filename, *name;
6426 const char const_name[] = "__COMPILER_HALT_OFFSET__";
6427
6428 if (FC(has_bracketed_namespaces) && FC(in_namespace)) {
6429 zend_error_noreturn(E_COMPILE_ERROR,
6430 "__HALT_COMPILER() can only be used from the outermost scope");
6431 }
6432
6433 filename = zend_get_compiled_filename();
6434 name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
6435 ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
6436
6437 zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, CONST_CS, 0);
6438 zend_string_release(name);
6439 }
6440 /* }}} */
6441
zend_try_ct_eval_magic_const(zval * zv,zend_ast * ast)6442 static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
6443 {
6444 zend_op_array *op_array = CG(active_op_array);
6445 zend_class_entry *ce = CG(active_class_entry);
6446
6447 switch (ast->attr) {
6448 case T_LINE:
6449 ZVAL_LONG(zv, ast->lineno);
6450 break;
6451 case T_FILE:
6452 ZVAL_STR_COPY(zv, CG(compiled_filename));
6453 break;
6454 case T_DIR:
6455 {
6456 zend_string *filename = CG(compiled_filename);
6457 zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0);
6458 #ifdef ZEND_WIN32
6459 php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
6460 #else
6461 zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname));
6462 #endif
6463
6464 if (strcmp(ZSTR_VAL(dirname), ".") == 0) {
6465 dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
6466 #if HAVE_GETCWD
6467 ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN));
6468 #elif HAVE_GETWD
6469 ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname)));
6470 #endif
6471 }
6472
6473 ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname));
6474 ZVAL_STR(zv, dirname);
6475 break;
6476 }
6477 case T_FUNC_C:
6478 if (op_array && op_array->function_name) {
6479 ZVAL_STR_COPY(zv, op_array->function_name);
6480 } else {
6481 ZVAL_EMPTY_STRING(zv);
6482 }
6483 break;
6484 case T_METHOD_C:
6485 if ((op_array && !op_array->scope && op_array->function_name) || (op_array->fn_flags & ZEND_ACC_CLOSURE)) {
6486 ZVAL_STR_COPY(zv, op_array->function_name);
6487 } else if (ce) {
6488 if (op_array && op_array->function_name) {
6489 ZVAL_NEW_STR(zv, zend_concat3(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), "::", 2,
6490 ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name)));
6491 } else {
6492 ZVAL_STR_COPY(zv, ce->name);
6493 }
6494 } else if (op_array && op_array->function_name) {
6495 ZVAL_STR_COPY(zv, op_array->function_name);
6496 } else {
6497 ZVAL_EMPTY_STRING(zv);
6498 }
6499 break;
6500 case T_CLASS_C:
6501 if (ce) {
6502 if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
6503 return 0;
6504 } else {
6505 ZVAL_STR_COPY(zv, ce->name);
6506 }
6507 } else {
6508 ZVAL_EMPTY_STRING(zv);
6509 }
6510 break;
6511 case T_TRAIT_C:
6512 if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
6513 ZVAL_STR_COPY(zv, ce->name);
6514 } else {
6515 ZVAL_EMPTY_STRING(zv);
6516 }
6517 break;
6518 case T_NS_C:
6519 if (FC(current_namespace)) {
6520 ZVAL_STR_COPY(zv, FC(current_namespace));
6521 } else {
6522 ZVAL_EMPTY_STRING(zv);
6523 }
6524 break;
6525 EMPTY_SWITCH_DEFAULT_CASE()
6526 }
6527
6528 return 1;
6529 }
6530 /* }}} */
6531
zend_binary_op_produces_numeric_string_error(uint32_t opcode,zval * op1,zval * op2)6532 ZEND_API zend_bool zend_binary_op_produces_numeric_string_error(uint32_t opcode, zval *op1, zval *op2) /* {{{ */
6533 {
6534 if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV
6535 || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR
6536 || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) {
6537 return 0;
6538 }
6539
6540 /* While basic arithmetic operators always produce numeric string errors,
6541 * bitwise operators don't produce errors if both operands are strings */
6542 if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)
6543 && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) {
6544 return 0;
6545 }
6546
6547 if (Z_TYPE_P(op1) == IS_STRING
6548 && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) {
6549 return 1;
6550 }
6551
6552 if (Z_TYPE_P(op2) == IS_STRING
6553 && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) {
6554 return 1;
6555 }
6556
6557 return 0;
6558 }
6559 /* }}} */
6560
zend_try_ct_eval_binary_op(zval * result,uint32_t opcode,zval * op1,zval * op2)6561 static inline zend_bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
6562 {
6563 binary_op_type fn = get_binary_op(opcode);
6564
6565 /* don't evaluate division by zero at compile-time */
6566 if ((opcode == ZEND_DIV || opcode == ZEND_MOD) &&
6567 zval_get_long(op2) == 0) {
6568 return 0;
6569 } else if ((opcode == ZEND_SL || opcode == ZEND_SR) &&
6570 zval_get_long(op2) < 0) {
6571 return 0;
6572 }
6573
6574 /* don't evaluate numeric string error-producing operations at compile-time */
6575 if (zend_binary_op_produces_numeric_string_error(opcode, op1, op2)) {
6576 return 0;
6577 }
6578
6579 fn(result, op1, op2);
6580 return 1;
6581 }
6582 /* }}} */
6583
zend_ct_eval_unary_op(zval * result,uint32_t opcode,zval * op)6584 static inline void zend_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */
6585 {
6586 unary_op_type fn = get_unary_op(opcode);
6587 fn(result, op);
6588 }
6589 /* }}} */
6590
zend_try_ct_eval_unary_pm(zval * result,zend_ast_kind kind,zval * op)6591 static inline zend_bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
6592 {
6593 zval left;
6594 ZVAL_LONG(&left, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
6595 return zend_try_ct_eval_binary_op(result, ZEND_MUL, &left, op);
6596 }
6597 /* }}} */
6598
zend_ct_eval_greater(zval * result,zend_ast_kind kind,zval * op1,zval * op2)6599 static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
6600 {
6601 binary_op_type fn = kind == ZEND_AST_GREATER
6602 ? is_smaller_function : is_smaller_or_equal_function;
6603 fn(result, op2, op1);
6604 }
6605 /* }}} */
6606
zend_try_ct_eval_array(zval * result,zend_ast * ast)6607 static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
6608 {
6609 zend_ast_list *list = zend_ast_get_list(ast);
6610 uint32_t i;
6611 zend_bool is_constant = 1;
6612
6613 if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) {
6614 zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression");
6615 }
6616
6617 /* First ensure that *all* child nodes are constant and by-val */
6618 for (i = 0; i < list->children; ++i) {
6619 zend_ast *elem_ast = list->child[i];
6620
6621 if (elem_ast == NULL) {
6622 zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
6623 }
6624
6625 zend_eval_const_expr(&elem_ast->child[0]);
6626 zend_eval_const_expr(&elem_ast->child[1]);
6627
6628 if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL
6629 || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
6630 ) {
6631 is_constant = 0;
6632 }
6633 }
6634
6635 if (!is_constant) {
6636 return 0;
6637 }
6638
6639 array_init_size(result, list->children);
6640 for (i = 0; i < list->children; ++i) {
6641 zend_ast *elem_ast = list->child[i];
6642 zend_ast *value_ast = elem_ast->child[0];
6643 zend_ast *key_ast = elem_ast->child[1];
6644
6645 zval *value = zend_ast_get_zval(value_ast);
6646 if (Z_REFCOUNTED_P(value)) Z_ADDREF_P(value);
6647
6648 if (key_ast) {
6649 zval *key = zend_ast_get_zval(key_ast);
6650 switch (Z_TYPE_P(key)) {
6651 case IS_LONG:
6652 zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value);
6653 break;
6654 case IS_STRING:
6655 zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value);
6656 break;
6657 case IS_DOUBLE:
6658 zend_hash_index_update(Z_ARRVAL_P(result),
6659 zend_dval_to_lval(Z_DVAL_P(key)), value);
6660 break;
6661 case IS_FALSE:
6662 zend_hash_index_update(Z_ARRVAL_P(result), 0, value);
6663 break;
6664 case IS_TRUE:
6665 zend_hash_index_update(Z_ARRVAL_P(result), 1, value);
6666 break;
6667 case IS_NULL:
6668 zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value);
6669 break;
6670 default:
6671 zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type");
6672 break;
6673 }
6674 } else {
6675 if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) {
6676 zval_ptr_dtor_nogc(value);
6677 zval_ptr_dtor(result);
6678 return 0;
6679 }
6680 }
6681 }
6682
6683 return 1;
6684 }
6685 /* }}} */
6686
zend_compile_binary_op(znode * result,zend_ast * ast)6687 void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */
6688 {
6689 zend_ast *left_ast = ast->child[0];
6690 zend_ast *right_ast = ast->child[1];
6691 uint32_t opcode = ast->attr;
6692
6693 znode left_node, right_node;
6694 zend_compile_expr(&left_node, left_ast);
6695 zend_compile_expr(&right_node, right_ast);
6696
6697 if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
6698 if (zend_try_ct_eval_binary_op(&result->u.constant, opcode,
6699 &left_node.u.constant, &right_node.u.constant)
6700 ) {
6701 result->op_type = IS_CONST;
6702 zval_ptr_dtor(&left_node.u.constant);
6703 zval_ptr_dtor(&right_node.u.constant);
6704 return;
6705 }
6706 }
6707
6708 do {
6709 if (opcode == ZEND_IS_EQUAL || opcode == ZEND_IS_NOT_EQUAL) {
6710 if (left_node.op_type == IS_CONST) {
6711 if (Z_TYPE(left_node.u.constant) == IS_FALSE) {
6712 opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
6713 zend_emit_op_tmp(result, opcode, &right_node, NULL);
6714 break;
6715 } else if (Z_TYPE(left_node.u.constant) == IS_TRUE) {
6716 opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
6717 zend_emit_op_tmp(result, opcode, &right_node, NULL);
6718 break;
6719 }
6720 } else if (right_node.op_type == IS_CONST) {
6721 if (Z_TYPE(right_node.u.constant) == IS_FALSE) {
6722 opcode = (opcode == ZEND_IS_NOT_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
6723 zend_emit_op_tmp(result, opcode, &left_node, NULL);
6724 break;
6725 } else if (Z_TYPE(right_node.u.constant) == IS_TRUE) {
6726 opcode = (opcode == ZEND_IS_EQUAL) ? ZEND_BOOL : ZEND_BOOL_NOT;
6727 zend_emit_op_tmp(result, opcode, &left_node, NULL);
6728 break;
6729 }
6730 }
6731 }
6732 if (opcode == ZEND_CONCAT) {
6733 /* convert constant operands to strings at compile-time */
6734 if (left_node.op_type == IS_CONST) {
6735 convert_to_string(&left_node.u.constant);
6736 }
6737 if (right_node.op_type == IS_CONST) {
6738 convert_to_string(&right_node.u.constant);
6739 }
6740 }
6741 zend_emit_op_tmp(result, opcode, &left_node, &right_node);
6742 } while (0);
6743 }
6744 /* }}} */
6745
6746 /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right
6747 * evaluation order. */
zend_compile_greater(znode * result,zend_ast * ast)6748 void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */
6749 {
6750 zend_ast *left_ast = ast->child[0];
6751 zend_ast *right_ast = ast->child[1];
6752 znode left_node, right_node;
6753
6754 ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL);
6755
6756 zend_compile_expr(&left_node, left_ast);
6757 zend_compile_expr(&right_node, right_ast);
6758
6759 if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) {
6760 result->op_type = IS_CONST;
6761 zend_ct_eval_greater(&result->u.constant, ast->kind,
6762 &left_node.u.constant, &right_node.u.constant);
6763 zval_ptr_dtor(&left_node.u.constant);
6764 zval_ptr_dtor(&right_node.u.constant);
6765 return;
6766 }
6767
6768 zend_emit_op_tmp(result,
6769 ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL,
6770 &right_node, &left_node);
6771 }
6772 /* }}} */
6773
zend_compile_unary_op(znode * result,zend_ast * ast)6774 void zend_compile_unary_op(znode *result, zend_ast *ast) /* {{{ */
6775 {
6776 zend_ast *expr_ast = ast->child[0];
6777 uint32_t opcode = ast->attr;
6778
6779 znode expr_node;
6780 zend_compile_expr(&expr_node, expr_ast);
6781
6782 if (expr_node.op_type == IS_CONST) {
6783 result->op_type = IS_CONST;
6784 zend_ct_eval_unary_op(&result->u.constant, opcode,
6785 &expr_node.u.constant);
6786 zval_ptr_dtor(&expr_node.u.constant);
6787 return;
6788 }
6789
6790 zend_emit_op_tmp(result, opcode, &expr_node, NULL);
6791 }
6792 /* }}} */
6793
zend_compile_unary_pm(znode * result,zend_ast * ast)6794 void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */
6795 {
6796 zend_ast *expr_ast = ast->child[0];
6797 znode expr_node;
6798 znode lefthand_node;
6799
6800 ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS);
6801
6802 zend_compile_expr(&expr_node, expr_ast);
6803
6804 if (expr_node.op_type == IS_CONST) {
6805 if (zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) {
6806 result->op_type = IS_CONST;
6807 zval_ptr_dtor(&expr_node.u.constant);
6808 return;
6809 }
6810 }
6811
6812 lefthand_node.op_type = IS_CONST;
6813 ZVAL_LONG(&lefthand_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1);
6814 zend_emit_op_tmp(result, ZEND_MUL, &lefthand_node, &expr_node);
6815 }
6816 /* }}} */
6817
zend_compile_short_circuiting(znode * result,zend_ast * ast)6818 void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */
6819 {
6820 zend_ast *left_ast = ast->child[0];
6821 zend_ast *right_ast = ast->child[1];
6822
6823 znode left_node, right_node;
6824 zend_op *opline_jmpz, *opline_bool;
6825 uint32_t opnum_jmpz;
6826
6827 ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR);
6828
6829 zend_compile_expr(&left_node, left_ast);
6830
6831 if (left_node.op_type == IS_CONST) {
6832 if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant))
6833 || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) {
6834 result->op_type = IS_CONST;
6835 ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant));
6836 } else {
6837 zend_compile_expr(&right_node, right_ast);
6838
6839 if (right_node.op_type == IS_CONST) {
6840 result->op_type = IS_CONST;
6841 ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant));
6842
6843 zval_ptr_dtor(&right_node.u.constant);
6844 } else {
6845 zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL);
6846 }
6847 }
6848
6849 zval_ptr_dtor(&left_node.u.constant);
6850 return;
6851 }
6852
6853 opnum_jmpz = get_next_op_number(CG(active_op_array));
6854 opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX,
6855 &left_node, NULL);
6856
6857 if (left_node.op_type == IS_TMP_VAR) {
6858 SET_NODE(opline_jmpz->result, &left_node);
6859 } else {
6860 opline_jmpz->result.var = get_temporary_variable(CG(active_op_array));
6861 opline_jmpz->result_type = IS_TMP_VAR;
6862 }
6863
6864 GET_NODE(result, opline_jmpz->result);
6865 zend_compile_expr(&right_node, right_ast);
6866
6867 opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL);
6868 SET_NODE(opline_bool->result, result);
6869
6870 zend_update_jump_target_to_next(opnum_jmpz);
6871 }
6872 /* }}} */
6873
zend_compile_post_incdec(znode * result,zend_ast * ast)6874 void zend_compile_post_incdec(znode *result, zend_ast *ast) /* {{{ */
6875 {
6876 zend_ast *var_ast = ast->child[0];
6877 ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC);
6878
6879 zend_ensure_writable_variable(var_ast);
6880
6881 if (var_ast->kind == ZEND_AST_PROP) {
6882 zend_op *opline = zend_compile_prop_common(NULL, var_ast, BP_VAR_RW);
6883 opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ;
6884 zend_make_tmp_result(result, opline);
6885 } else {
6886 znode var_node;
6887 zend_compile_var(&var_node, var_ast, BP_VAR_RW);
6888 zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC,
6889 &var_node, NULL);
6890 }
6891 }
6892 /* }}} */
6893
zend_compile_pre_incdec(znode * result,zend_ast * ast)6894 void zend_compile_pre_incdec(znode *result, zend_ast *ast) /* {{{ */
6895 {
6896 zend_ast *var_ast = ast->child[0];
6897 ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC);
6898
6899 zend_ensure_writable_variable(var_ast);
6900
6901 if (var_ast->kind == ZEND_AST_PROP) {
6902 zend_op *opline = zend_compile_prop_common(result, var_ast, BP_VAR_RW);
6903 opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ;
6904 } else {
6905 znode var_node;
6906 zend_compile_var(&var_node, var_ast, BP_VAR_RW);
6907 zend_emit_op(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC,
6908 &var_node, NULL);
6909 }
6910 }
6911 /* }}} */
6912
zend_compile_cast(znode * result,zend_ast * ast)6913 void zend_compile_cast(znode *result, zend_ast *ast) /* {{{ */
6914 {
6915 zend_ast *expr_ast = ast->child[0];
6916 znode expr_node;
6917 zend_op *opline;
6918
6919 zend_compile_expr(&expr_node, expr_ast);
6920
6921 opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL);
6922 opline->extended_value = ast->attr;
6923 }
6924 /* }}} */
6925
zend_compile_shorthand_conditional(znode * result,zend_ast * ast)6926 static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */
6927 {
6928 zend_ast *cond_ast = ast->child[0];
6929 zend_ast *false_ast = ast->child[2];
6930
6931 znode cond_node, false_node;
6932 zend_op *opline_qm_assign;
6933 uint32_t opnum_jmp_set;
6934
6935 ZEND_ASSERT(ast->child[1] == NULL);
6936
6937 zend_compile_expr(&cond_node, cond_ast);
6938
6939 opnum_jmp_set = get_next_op_number(CG(active_op_array));
6940 zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL);
6941
6942 zend_compile_expr(&false_node, false_ast);
6943
6944 opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
6945 SET_NODE(opline_qm_assign->result, result);
6946
6947 zend_update_jump_target_to_next(opnum_jmp_set);
6948 }
6949 /* }}} */
6950
zend_compile_conditional(znode * result,zend_ast * ast)6951 void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
6952 {
6953 zend_ast *cond_ast = ast->child[0];
6954 zend_ast *true_ast = ast->child[1];
6955 zend_ast *false_ast = ast->child[2];
6956
6957 znode cond_node, true_node, false_node;
6958 zend_op *opline_qm_assign2;
6959 uint32_t opnum_jmpz, opnum_jmp;
6960
6961 if (!true_ast) {
6962 zend_compile_shorthand_conditional(result, ast);
6963 return;
6964 }
6965
6966 zend_compile_expr(&cond_node, cond_ast);
6967
6968 opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
6969
6970 zend_compile_expr(&true_node, true_ast);
6971
6972 zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL);
6973
6974 opnum_jmp = zend_emit_jump(0);
6975
6976 zend_update_jump_target_to_next(opnum_jmpz);
6977
6978 zend_compile_expr(&false_node, false_ast);
6979
6980 opline_qm_assign2 = zend_emit_op(NULL, ZEND_QM_ASSIGN, &false_node, NULL);
6981 SET_NODE(opline_qm_assign2->result, result);
6982
6983 zend_update_jump_target_to_next(opnum_jmp);
6984 }
6985 /* }}} */
6986
zend_compile_coalesce(znode * result,zend_ast * ast)6987 void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */
6988 {
6989 zend_ast *expr_ast = ast->child[0];
6990 zend_ast *default_ast = ast->child[1];
6991
6992 znode expr_node, default_node;
6993 zend_op *opline;
6994 uint32_t opnum;
6995
6996 zend_compile_var(&expr_node, expr_ast, BP_VAR_IS);
6997
6998 opnum = get_next_op_number(CG(active_op_array));
6999 zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL);
7000
7001 zend_compile_expr(&default_node, default_ast);
7002
7003 opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL);
7004 SET_NODE(opline->result, result);
7005
7006 opline = &CG(active_op_array)->opcodes[opnum];
7007 opline->op2.opline_num = get_next_op_number(CG(active_op_array));
7008 }
7009 /* }}} */
7010
zend_compile_print(znode * result,zend_ast * ast)7011 void zend_compile_print(znode *result, zend_ast *ast) /* {{{ */
7012 {
7013 zend_op *opline;
7014 zend_ast *expr_ast = ast->child[0];
7015
7016 znode expr_node;
7017 zend_compile_expr(&expr_node, expr_ast);
7018
7019 opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
7020 opline->extended_value = 1;
7021
7022 result->op_type = IS_CONST;
7023 ZVAL_LONG(&result->u.constant, 1);
7024 }
7025 /* }}} */
7026
zend_compile_exit(znode * result,zend_ast * ast)7027 void zend_compile_exit(znode *result, zend_ast *ast) /* {{{ */
7028 {
7029 zend_ast *expr_ast = ast->child[0];
7030
7031 if (expr_ast) {
7032 znode expr_node;
7033 zend_compile_expr(&expr_node, expr_ast);
7034 zend_emit_op(NULL, ZEND_EXIT, &expr_node, NULL);
7035 } else {
7036 zend_emit_op(NULL, ZEND_EXIT, NULL, NULL);
7037 }
7038
7039 result->op_type = IS_CONST;
7040 ZVAL_BOOL(&result->u.constant, 1);
7041 }
7042 /* }}} */
7043
zend_compile_yield(znode * result,zend_ast * ast)7044 void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
7045 {
7046 zend_ast *value_ast = ast->child[0];
7047 zend_ast *key_ast = ast->child[1];
7048
7049 znode value_node, key_node;
7050 znode *value_node_ptr = NULL, *key_node_ptr = NULL;
7051 zend_op *opline;
7052 zend_bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
7053
7054 zend_mark_function_as_generator();
7055
7056 if (key_ast) {
7057 zend_compile_expr(&key_node, key_ast);
7058 key_node_ptr = &key_node;
7059 }
7060
7061 if (value_ast) {
7062 if (returns_by_ref && zend_is_variable(value_ast) && !zend_is_call(value_ast)) {
7063 zend_compile_var(&value_node, value_ast, BP_VAR_W);
7064 } else {
7065 zend_compile_expr(&value_node, value_ast);
7066 }
7067 value_node_ptr = &value_node;
7068 }
7069
7070 opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);
7071
7072 if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
7073 opline->extended_value = ZEND_RETURNS_FUNCTION;
7074 }
7075 }
7076 /* }}} */
7077
zend_compile_yield_from(znode * result,zend_ast * ast)7078 void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
7079 {
7080 zend_ast *expr_ast = ast->child[0];
7081 znode expr_node;
7082
7083 zend_mark_function_as_generator();
7084
7085 if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
7086 zend_error_noreturn(E_COMPILE_ERROR,
7087 "Cannot use \"yield from\" inside a by-reference generator");
7088 }
7089
7090 zend_compile_expr(&expr_node, expr_ast);
7091 zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
7092 }
7093 /* }}} */
7094
zend_compile_instanceof(znode * result,zend_ast * ast)7095 void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */
7096 {
7097 zend_ast *obj_ast = ast->child[0];
7098 zend_ast *class_ast = ast->child[1];
7099
7100 znode obj_node, class_node;
7101 zend_op *opline;
7102
7103 zend_compile_expr(&obj_node, obj_ast);
7104 if (obj_node.op_type == IS_CONST) {
7105 zend_error_noreturn(E_COMPILE_ERROR,
7106 "instanceof expects an object instance, constant given");
7107 }
7108
7109 zend_compile_class_ref_ex(&class_node, class_ast,
7110 ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION);
7111
7112 opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL);
7113
7114 if (class_node.op_type == IS_CONST) {
7115 opline->op2_type = IS_CONST;
7116 opline->op2.constant = zend_add_class_name_literal(
7117 CG(active_op_array), Z_STR(class_node.u.constant));
7118 } else {
7119 SET_NODE(opline->op2, &class_node);
7120 }
7121 }
7122 /* }}} */
7123
zend_compile_include_or_eval(znode * result,zend_ast * ast)7124 void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */
7125 {
7126 zend_ast *expr_ast = ast->child[0];
7127 znode expr_node;
7128 zend_op *opline;
7129
7130 zend_do_extended_fcall_begin();
7131 zend_compile_expr(&expr_node, expr_ast);
7132
7133 opline = zend_emit_op(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL);
7134 opline->extended_value = ast->attr;
7135
7136 zend_do_extended_fcall_end();
7137 }
7138 /* }}} */
7139
zend_compile_isset_or_empty(znode * result,zend_ast * ast)7140 void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */
7141 {
7142 zend_ast *var_ast = ast->child[0];
7143
7144 znode var_node;
7145 zend_op *opline = NULL;
7146
7147 ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY);
7148
7149 if (!zend_is_variable(var_ast) || zend_is_call(var_ast)) {
7150 if (ast->kind == ZEND_AST_EMPTY) {
7151 /* empty(expr) can be transformed to !expr */
7152 zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast);
7153 zend_compile_expr(result, not_ast);
7154 return;
7155 } else {
7156 zend_error_noreturn(E_COMPILE_ERROR,
7157 "Cannot use isset() on the result of an expression "
7158 "(you can use \"null !== expression\" instead)");
7159 }
7160 }
7161
7162 switch (var_ast->kind) {
7163 case ZEND_AST_VAR:
7164 if (is_this_fetch(var_ast)) {
7165 opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL);
7166 } else if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
7167 opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL);
7168 opline->extended_value = ZEND_FETCH_LOCAL | ZEND_QUICK_SET;
7169 } else {
7170 opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, 0);
7171 opline->opcode = ZEND_ISSET_ISEMPTY_VAR;
7172 }
7173 break;
7174 case ZEND_AST_DIM:
7175 opline = zend_compile_dim_common(result, var_ast, BP_VAR_IS);
7176 opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ;
7177 break;
7178 case ZEND_AST_PROP:
7179 opline = zend_compile_prop_common(result, var_ast, BP_VAR_IS);
7180 opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ;
7181 break;
7182 case ZEND_AST_STATIC_PROP:
7183 opline = zend_compile_static_prop_common(result, var_ast, BP_VAR_IS, 0);
7184 opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP;
7185 break;
7186 EMPTY_SWITCH_DEFAULT_CASE()
7187 }
7188
7189 result->op_type = opline->result_type = IS_TMP_VAR;
7190 opline->extended_value |= ast->kind == ZEND_AST_ISSET ? ZEND_ISSET : ZEND_ISEMPTY;
7191 }
7192 /* }}} */
7193
zend_compile_silence(znode * result,zend_ast * ast)7194 void zend_compile_silence(znode *result, zend_ast *ast) /* {{{ */
7195 {
7196 zend_ast *expr_ast = ast->child[0];
7197 znode silence_node;
7198 uint32_t range;
7199
7200 range = zend_start_live_range(CG(active_op_array), get_next_op_number(CG(active_op_array)));
7201 zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL);
7202
7203 if (expr_ast->kind == ZEND_AST_VAR) {
7204 /* For @$var we need to force a FETCH instruction, otherwise the CV access will
7205 * happen outside the silenced section. */
7206 zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, 0 );
7207 } else {
7208 zend_compile_expr(result, expr_ast);
7209 }
7210
7211 /* Store BEGIN_SILENCE/END_SILENCE pair to restore previous
7212 * EG(error_reporting) value on exception */
7213 zend_end_live_range(CG(active_op_array), range, get_next_op_number(CG(active_op_array)),
7214 ZEND_LIVE_SILENCE, silence_node.u.op.var);
7215
7216 zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL);
7217 }
7218 /* }}} */
7219
zend_compile_shell_exec(znode * result,zend_ast * ast)7220 void zend_compile_shell_exec(znode *result, zend_ast *ast) /* {{{ */
7221 {
7222 zend_ast *expr_ast = ast->child[0];
7223
7224 zval fn_name;
7225 zend_ast *name_ast, *args_ast, *call_ast;
7226
7227 ZVAL_STRING(&fn_name, "shell_exec");
7228 name_ast = zend_ast_create_zval(&fn_name);
7229 args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast);
7230 call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast);
7231
7232 zend_compile_expr(result, call_ast);
7233
7234 zval_ptr_dtor(&fn_name);
7235 }
7236 /* }}} */
7237
zend_compile_array(znode * result,zend_ast * ast)7238 void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */
7239 {
7240 zend_ast_list *list = zend_ast_get_list(ast);
7241 zend_op *opline;
7242 uint32_t i, opnum_init = -1;
7243 zend_bool packed = 1;
7244
7245 if (zend_try_ct_eval_array(&result->u.constant, ast)) {
7246 result->op_type = IS_CONST;
7247 return;
7248 }
7249
7250 for (i = 0; i < list->children; ++i) {
7251 zend_ast *elem_ast = list->child[i];
7252 zend_ast *value_ast, *key_ast;
7253 zend_bool by_ref;
7254 znode value_node, key_node, *key_node_ptr = NULL;
7255
7256 if (elem_ast == NULL) {
7257 zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays");
7258 }
7259
7260 value_ast = elem_ast->child[0];
7261 key_ast = elem_ast->child[1];
7262 by_ref = elem_ast->attr;
7263
7264 if (key_ast) {
7265 zend_compile_expr(&key_node, key_ast);
7266 zend_handle_numeric_op(&key_node);
7267 key_node_ptr = &key_node;
7268 }
7269
7270 if (by_ref) {
7271 zend_ensure_writable_variable(value_ast);
7272 zend_compile_var(&value_node, value_ast, BP_VAR_W);
7273 } else {
7274 zend_compile_expr(&value_node, value_ast);
7275 }
7276
7277 if (i == 0) {
7278 opnum_init = get_next_op_number(CG(active_op_array));
7279 opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr);
7280 opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT;
7281 } else {
7282 opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT,
7283 &value_node, key_node_ptr);
7284 SET_NODE(opline->result, result);
7285 }
7286 opline->extended_value |= by_ref;
7287
7288 if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) {
7289 packed = 0;
7290 }
7291 }
7292
7293 /* Handle empty array */
7294 if (!list->children) {
7295 zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL);
7296 }
7297
7298 /* Add a flag to INIT_ARRAY if we know this array cannot be packed */
7299 if (!packed) {
7300 ZEND_ASSERT(opnum_init != (uint32_t)-1);
7301 opline = &CG(active_op_array)->opcodes[opnum_init];
7302 opline->extended_value |= ZEND_ARRAY_NOT_PACKED;
7303 }
7304 }
7305 /* }}} */
7306
zend_compile_const(znode * result,zend_ast * ast)7307 void zend_compile_const(znode *result, zend_ast *ast) /* {{{ */
7308 {
7309 zend_ast *name_ast = ast->child[0];
7310
7311 zend_op *opline;
7312
7313 zend_bool is_fully_qualified;
7314 zend_string *orig_name = zend_ast_get_str(name_ast);
7315 zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified);
7316
7317 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__"))) {
7318 zend_ast *last = CG(ast);
7319
7320 while (last->kind == ZEND_AST_STMT_LIST) {
7321 zend_ast_list *list = zend_ast_get_list(last);
7322 last = list->child[list->children-1];
7323 }
7324 if (last->kind == ZEND_AST_HALT_COMPILER) {
7325 result->op_type = IS_CONST;
7326 ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0])));
7327 zend_string_release(resolved_name);
7328 return;
7329 }
7330 }
7331
7332 if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) {
7333 result->op_type = IS_CONST;
7334 zend_string_release(resolved_name);
7335 return;
7336 }
7337
7338 opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL);
7339 opline->op2_type = IS_CONST;
7340
7341 if (is_fully_qualified) {
7342 opline->op2.constant = zend_add_const_name_literal(
7343 CG(active_op_array), resolved_name, 0);
7344 } else {
7345 opline->extended_value = IS_CONSTANT_UNQUALIFIED;
7346 if (FC(current_namespace)) {
7347 opline->extended_value |= IS_CONSTANT_IN_NAMESPACE;
7348 opline->op2.constant = zend_add_const_name_literal(
7349 CG(active_op_array), resolved_name, 1);
7350 } else {
7351 opline->op2.constant = zend_add_const_name_literal(
7352 CG(active_op_array), resolved_name, 0);
7353 }
7354 }
7355 zend_alloc_cache_slot(opline->op2.constant);
7356 }
7357 /* }}} */
7358
zend_compile_class_const(znode * result,zend_ast * ast)7359 void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */
7360 {
7361 zend_ast *class_ast = ast->child[0];
7362 zend_ast *const_ast = ast->child[1];
7363
7364 znode class_node, const_node;
7365 zend_op *opline;
7366
7367 if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast, const_ast, 0)) {
7368 if (Z_TYPE(result->u.constant) == IS_NULL) {
7369 zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
7370 opline->extended_value = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
7371 } else {
7372 result->op_type = IS_CONST;
7373 }
7374 return;
7375 }
7376
7377 zend_eval_const_expr(&ast->child[0]);
7378 zend_eval_const_expr(&ast->child[1]);
7379
7380 class_ast = ast->child[0];
7381 const_ast = ast->child[1];
7382
7383 if (class_ast->kind == ZEND_AST_ZVAL) {
7384 zend_string *resolved_name;
7385
7386 resolved_name = zend_resolve_class_name_ast(class_ast);
7387 if (const_ast->kind == ZEND_AST_ZVAL && zend_try_ct_eval_class_const(&result->u.constant, resolved_name, zend_ast_get_str(const_ast))) {
7388 result->op_type = IS_CONST;
7389 zend_string_release(resolved_name);
7390 return;
7391 }
7392 zend_string_release(resolved_name);
7393 }
7394 if (const_ast->kind == ZEND_AST_ZVAL && zend_string_equals_literal_ci(zend_ast_get_str(const_ast), "class")) {
7395 zend_error_noreturn(E_COMPILE_ERROR,
7396 "Dynamic class names are not allowed in compile-time ::class fetch");
7397 }
7398
7399 zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION);
7400
7401 zend_compile_expr(&const_node, const_ast);
7402
7403 opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node);
7404
7405 zend_set_class_name_op1(opline, &class_node);
7406
7407 if (opline->op1_type == IS_CONST) {
7408 zend_alloc_cache_slot(opline->op2.constant);
7409 } else {
7410 zend_alloc_polymorphic_cache_slot(opline->op2.constant);
7411 }
7412 }
7413 /* }}} */
7414
zend_compile_resolve_class_name(znode * result,zend_ast * ast)7415 void zend_compile_resolve_class_name(znode *result, zend_ast *ast) /* {{{ */
7416 {
7417 zend_ast *name_ast = ast->child[0];
7418 uint32_t fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast));
7419 zend_ensure_valid_class_fetch_type(fetch_type);
7420
7421 switch (fetch_type) {
7422 case ZEND_FETCH_CLASS_SELF:
7423 if (CG(active_class_entry) && zend_is_scope_known()) {
7424 result->op_type = IS_CONST;
7425 ZVAL_STR_COPY(&result->u.constant, CG(active_class_entry)->name);
7426 } else {
7427 zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
7428 opline->extended_value = fetch_type;
7429 }
7430 break;
7431 case ZEND_FETCH_CLASS_STATIC:
7432 case ZEND_FETCH_CLASS_PARENT:
7433 {
7434 zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
7435 opline->extended_value = fetch_type;
7436 }
7437 break;
7438 case ZEND_FETCH_CLASS_DEFAULT:
7439 result->op_type = IS_CONST;
7440 ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast));
7441 break;
7442 EMPTY_SWITCH_DEFAULT_CASE()
7443 }
7444 }
7445 /* }}} */
7446
zend_compile_rope_add(znode * result,uint32_t num,znode * elem_node)7447 static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */
7448 {
7449 zend_op *opline = get_next_op(CG(active_op_array));
7450
7451 if (num == 0) {
7452 result->op_type = IS_TMP_VAR;
7453 result->u.op.var = -1;
7454 opline->opcode = ZEND_ROPE_INIT;
7455 SET_UNUSED(opline->op1);
7456 } else {
7457 opline->opcode = ZEND_ROPE_ADD;
7458 SET_NODE(opline->op1, result);
7459 }
7460 SET_NODE(opline->op2, elem_node);
7461 SET_NODE(opline->result, result);
7462 opline->extended_value = num;
7463 return opline;
7464 }
7465 /* }}} */
7466
zend_compile_encaps_list(znode * result,zend_ast * ast)7467 static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */
7468 {
7469 uint32_t i, j;
7470 uint32_t rope_init_lineno = -1;
7471 zend_op *opline = NULL, *init_opline;
7472 znode elem_node, last_const_node;
7473 zend_ast_list *list = zend_ast_get_list(ast);
7474
7475 ZEND_ASSERT(list->children > 0);
7476
7477 j = 0;
7478 last_const_node.op_type = IS_UNUSED;
7479 for (i = 0; i < list->children; i++) {
7480 zend_compile_expr(&elem_node, list->child[i]);
7481
7482 if (elem_node.op_type == IS_CONST) {
7483 convert_to_string(&elem_node.u.constant);
7484
7485 if (Z_STRLEN(elem_node.u.constant) == 0) {
7486 zval_ptr_dtor(&elem_node.u.constant);
7487 } else if (last_const_node.op_type == IS_CONST) {
7488 concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant);
7489 zval_ptr_dtor(&elem_node.u.constant);
7490 } else {
7491 last_const_node.op_type = IS_CONST;
7492 ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant);
7493 }
7494 continue;
7495 } else {
7496 if (j == 0) {
7497 rope_init_lineno = get_next_op_number(CG(active_op_array));
7498 }
7499 if (last_const_node.op_type == IS_CONST) {
7500 zend_compile_rope_add(result, j++, &last_const_node);
7501 last_const_node.op_type = IS_UNUSED;
7502 }
7503 opline = zend_compile_rope_add(result, j++, &elem_node);
7504 }
7505 }
7506
7507 if (j == 0) {
7508 result->op_type = IS_CONST;
7509 if (last_const_node.op_type == IS_CONST) {
7510 ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant);
7511 } else {
7512 ZVAL_EMPTY_STRING(&result->u.constant);
7513 /* empty string */
7514 }
7515 return;
7516 } else if (last_const_node.op_type == IS_CONST) {
7517 opline = zend_compile_rope_add(result, j++, &last_const_node);
7518 }
7519 init_opline = CG(active_op_array)->opcodes + rope_init_lineno;
7520 if (j == 1) {
7521 if (opline->op2_type == IS_CONST) {
7522 GET_NODE(result, opline->op2);
7523 MAKE_NOP(opline);
7524 } else {
7525 opline->opcode = ZEND_CAST;
7526 opline->extended_value = IS_STRING;
7527 opline->op1_type = opline->op2_type;
7528 opline->op1 = opline->op2;
7529 opline->result_type = IS_TMP_VAR;
7530 opline->result.var = get_temporary_variable(CG(active_op_array));
7531 SET_UNUSED(opline->op2);
7532 GET_NODE(result, opline->result);
7533 }
7534 } else if (j == 2) {
7535 opline->opcode = ZEND_FAST_CONCAT;
7536 opline->extended_value = 0;
7537 opline->op1_type = init_opline->op2_type;
7538 opline->op1 = init_opline->op2;
7539 opline->result_type = IS_TMP_VAR;
7540 opline->result.var = get_temporary_variable(CG(active_op_array));
7541 MAKE_NOP(init_opline);
7542 GET_NODE(result, opline->result);
7543 } else {
7544 uint32_t var;
7545 uint32_t range = zend_start_live_range(CG(active_op_array), rope_init_lineno);
7546
7547 init_opline->extended_value = j;
7548 opline->opcode = ZEND_ROPE_END;
7549 opline->result.var = get_temporary_variable(CG(active_op_array));
7550 var = opline->op1.var = get_temporary_variable(CG(active_op_array));
7551 GET_NODE(result, opline->result);
7552
7553 /* Allocates the necessary number of zval slots to keep the rope */
7554 i = ((j * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval);
7555 while (i > 1) {
7556 get_temporary_variable(CG(active_op_array));
7557 i--;
7558 }
7559
7560 zend_end_live_range(CG(active_op_array), range, opline - CG(active_op_array)->opcodes,
7561 ZEND_LIVE_ROPE, var);
7562
7563 /* Update all the previous opcodes to use the same variable */
7564 while (opline != init_opline) {
7565 opline--;
7566 if (opline->opcode == ZEND_ROPE_ADD &&
7567 opline->result.var == (uint32_t)-1) {
7568 opline->op1.var = var;
7569 opline->result.var = var;
7570 } else if (opline->opcode == ZEND_ROPE_INIT &&
7571 opline->result.var == (uint32_t)-1) {
7572 opline->result.var = var;
7573 }
7574 }
7575 }
7576 }
7577 /* }}} */
7578
zend_compile_magic_const(znode * result,zend_ast * ast)7579 void zend_compile_magic_const(znode *result, zend_ast *ast) /* {{{ */
7580 {
7581 zend_op *opline;
7582
7583 if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) {
7584 result->op_type = IS_CONST;
7585 return;
7586 }
7587
7588 ZEND_ASSERT(ast->attr == T_CLASS_C &&
7589 CG(active_class_entry) &&
7590 (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
7591
7592 opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL);
7593 opline->extended_value = ZEND_FETCH_CLASS_SELF;
7594 }
7595 /* }}} */
7596
zend_is_allowed_in_const_expr(zend_ast_kind kind)7597 zend_bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */
7598 {
7599 return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP
7600 || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL
7601 || kind == ZEND_AST_AND || kind == ZEND_AST_OR
7602 || kind == ZEND_AST_UNARY_OP
7603 || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS
7604 || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM
7605 || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM
7606 || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST
7607 || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE;
7608 }
7609 /* }}} */
7610
zend_compile_const_expr_class_const(zend_ast ** ast_ptr)7611 void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */
7612 {
7613 zend_ast *ast = *ast_ptr;
7614 zend_ast *class_ast = ast->child[0];
7615 zend_ast *const_ast = ast->child[1];
7616 zend_string *class_name;
7617 zend_string *const_name = zend_ast_get_str(const_ast);
7618 zval result;
7619 int fetch_type;
7620
7621 if (class_ast->kind != ZEND_AST_ZVAL) {
7622 zend_error_noreturn(E_COMPILE_ERROR,
7623 "Dynamic class names are not allowed in compile-time class constant references");
7624 }
7625
7626 if (zend_try_compile_const_expr_resolve_class_name(&result, class_ast, const_ast, 1)) {
7627 *ast_ptr = zend_ast_create_zval(&result);
7628 return;
7629 }
7630
7631 class_name = zend_ast_get_str(class_ast);
7632 fetch_type = zend_get_class_fetch_type(class_name);
7633
7634 if (ZEND_FETCH_CLASS_STATIC == fetch_type) {
7635 zend_error_noreturn(E_COMPILE_ERROR,
7636 "\"static::\" is not allowed in compile-time constants");
7637 }
7638
7639 if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) {
7640 class_name = zend_resolve_class_name_ast(class_ast);
7641 } else {
7642 zend_string_addref(class_name);
7643 }
7644
7645 Z_STR(result) = zend_concat3(
7646 ZSTR_VAL(class_name), ZSTR_LEN(class_name), "::", 2, ZSTR_VAL(const_name), ZSTR_LEN(const_name));
7647
7648 Z_TYPE_INFO(result) = IS_CONSTANT_EX;
7649 Z_CONST_FLAGS(result) = fetch_type;
7650
7651 zend_ast_destroy(ast);
7652 zend_string_release(class_name);
7653
7654 *ast_ptr = zend_ast_create_zval(&result);
7655 }
7656 /* }}} */
7657
zend_compile_const_expr_const(zend_ast ** ast_ptr)7658 void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */
7659 {
7660 zend_ast *ast = *ast_ptr;
7661 zend_ast *name_ast = ast->child[0];
7662 zend_string *orig_name = zend_ast_get_str(name_ast);
7663 zend_bool is_fully_qualified;
7664
7665 zval result, resolved_name;
7666 ZVAL_STR(&resolved_name, zend_resolve_const_name(
7667 orig_name, name_ast->attr, &is_fully_qualified));
7668
7669 if (zend_try_ct_eval_const(&result, Z_STR(resolved_name), is_fully_qualified)) {
7670 zend_string_release(Z_STR(resolved_name));
7671 zend_ast_destroy(ast);
7672 *ast_ptr = zend_ast_create_zval(&result);
7673 return;
7674 }
7675
7676 Z_TYPE_INFO(resolved_name) = IS_CONSTANT_EX;
7677 if (!is_fully_qualified) {
7678 Z_CONST_FLAGS(resolved_name) = IS_CONSTANT_UNQUALIFIED;
7679 }
7680
7681 zend_ast_destroy(ast);
7682 *ast_ptr = zend_ast_create_zval(&resolved_name);
7683 }
7684 /* }}} */
7685
zend_compile_const_expr_magic_const(zend_ast ** ast_ptr)7686 void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
7687 {
7688 zend_ast *ast = *ast_ptr;
7689
7690 /* Other cases already resolved by constant folding */
7691 ZEND_ASSERT(ast->attr == T_CLASS_C &&
7692 CG(active_class_entry) &&
7693 (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
7694
7695 {
7696 zval const_zv;
7697 Z_STR(const_zv) = zend_string_init("__CLASS__", sizeof("__CLASS__")-1, 0);
7698 Z_TYPE_INFO(const_zv) = IS_CONSTANT_EX | (IS_CONSTANT_CLASS << Z_CONST_FLAGS_SHIFT);
7699
7700 zend_ast_destroy(ast);
7701 *ast_ptr = zend_ast_create_zval(&const_zv);
7702 }
7703 }
7704 /* }}} */
7705
zend_compile_const_expr(zend_ast ** ast_ptr)7706 void zend_compile_const_expr(zend_ast **ast_ptr) /* {{{ */
7707 {
7708 zend_ast *ast = *ast_ptr;
7709 if (ast == NULL || ast->kind == ZEND_AST_ZVAL) {
7710 return;
7711 }
7712
7713 if (!zend_is_allowed_in_const_expr(ast->kind)) {
7714 zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations");
7715 }
7716
7717 switch (ast->kind) {
7718 case ZEND_AST_CLASS_CONST:
7719 zend_compile_const_expr_class_const(ast_ptr);
7720 break;
7721 case ZEND_AST_CONST:
7722 zend_compile_const_expr_const(ast_ptr);
7723 break;
7724 case ZEND_AST_MAGIC_CONST:
7725 zend_compile_const_expr_magic_const(ast_ptr);
7726 break;
7727 default:
7728 zend_ast_apply(ast, zend_compile_const_expr);
7729 break;
7730 }
7731 }
7732 /* }}} */
7733
zend_const_expr_to_zval(zval * result,zend_ast * ast)7734 void zend_const_expr_to_zval(zval *result, zend_ast *ast) /* {{{ */
7735 {
7736 zend_ast *orig_ast = ast;
7737 zend_eval_const_expr(&ast);
7738 zend_compile_const_expr(&ast);
7739 if (ast->kind == ZEND_AST_ZVAL) {
7740 ZVAL_COPY_VALUE(result, zend_ast_get_zval(ast));
7741 } else {
7742 ZVAL_NEW_AST(result, zend_ast_copy(ast));
7743 /* destroy the ast here, it might have been replaced */
7744 zend_ast_destroy(ast);
7745 }
7746
7747 /* Kill this branch of the original AST, as it was already destroyed.
7748 * It would be nice to find a better solution to this problem in the
7749 * future. */
7750 orig_ast->kind = 0;
7751 }
7752 /* }}} */
7753
7754 /* Same as compile_stmt, but with early binding */
zend_compile_top_stmt(zend_ast * ast)7755 void zend_compile_top_stmt(zend_ast *ast) /* {{{ */
7756 {
7757 if (!ast) {
7758 return;
7759 }
7760
7761 if (ast->kind == ZEND_AST_STMT_LIST) {
7762 zend_ast_list *list = zend_ast_get_list(ast);
7763 uint32_t i;
7764 for (i = 0; i < list->children; ++i) {
7765 zend_compile_top_stmt(list->child[i]);
7766 }
7767 return;
7768 }
7769
7770 zend_compile_stmt(ast);
7771
7772 if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) {
7773 zend_verify_namespace();
7774 }
7775 if (ast->kind == ZEND_AST_FUNC_DECL || ast->kind == ZEND_AST_CLASS) {
7776 CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno;
7777 zend_do_early_binding();
7778 }
7779 }
7780 /* }}} */
7781
zend_compile_stmt(zend_ast * ast)7782 void zend_compile_stmt(zend_ast *ast) /* {{{ */
7783 {
7784 if (!ast) {
7785 return;
7786 }
7787
7788 CG(zend_lineno) = ast->lineno;
7789
7790 if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) && !zend_is_unticked_stmt(ast)) {
7791 zend_do_extended_info();
7792 }
7793
7794 switch (ast->kind) {
7795 case ZEND_AST_STMT_LIST:
7796 zend_compile_stmt_list(ast);
7797 break;
7798 case ZEND_AST_GLOBAL:
7799 zend_compile_global_var(ast);
7800 break;
7801 case ZEND_AST_STATIC:
7802 zend_compile_static_var(ast);
7803 break;
7804 case ZEND_AST_UNSET:
7805 zend_compile_unset(ast);
7806 break;
7807 case ZEND_AST_RETURN:
7808 zend_compile_return(ast);
7809 break;
7810 case ZEND_AST_ECHO:
7811 zend_compile_echo(ast);
7812 break;
7813 case ZEND_AST_THROW:
7814 zend_compile_throw(ast);
7815 break;
7816 case ZEND_AST_BREAK:
7817 case ZEND_AST_CONTINUE:
7818 zend_compile_break_continue(ast);
7819 break;
7820 case ZEND_AST_GOTO:
7821 zend_compile_goto(ast);
7822 break;
7823 case ZEND_AST_LABEL:
7824 zend_compile_label(ast);
7825 break;
7826 case ZEND_AST_WHILE:
7827 zend_compile_while(ast);
7828 break;
7829 case ZEND_AST_DO_WHILE:
7830 zend_compile_do_while(ast);
7831 break;
7832 case ZEND_AST_FOR:
7833 zend_compile_for(ast);
7834 break;
7835 case ZEND_AST_FOREACH:
7836 zend_compile_foreach(ast);
7837 break;
7838 case ZEND_AST_IF:
7839 zend_compile_if(ast);
7840 break;
7841 case ZEND_AST_SWITCH:
7842 zend_compile_switch(ast);
7843 break;
7844 case ZEND_AST_TRY:
7845 zend_compile_try(ast);
7846 break;
7847 case ZEND_AST_DECLARE:
7848 zend_compile_declare(ast);
7849 break;
7850 case ZEND_AST_FUNC_DECL:
7851 case ZEND_AST_METHOD:
7852 zend_compile_func_decl(NULL, ast);
7853 break;
7854 case ZEND_AST_PROP_DECL:
7855 zend_compile_prop_decl(ast);
7856 break;
7857 case ZEND_AST_CLASS_CONST_DECL:
7858 zend_compile_class_const_decl(ast);
7859 break;
7860 case ZEND_AST_USE_TRAIT:
7861 zend_compile_use_trait(ast);
7862 break;
7863 case ZEND_AST_CLASS:
7864 zend_compile_class_decl(ast);
7865 break;
7866 case ZEND_AST_GROUP_USE:
7867 zend_compile_group_use(ast);
7868 break;
7869 case ZEND_AST_USE:
7870 zend_compile_use(ast);
7871 break;
7872 case ZEND_AST_CONST_DECL:
7873 zend_compile_const_decl(ast);
7874 break;
7875 case ZEND_AST_NAMESPACE:
7876 zend_compile_namespace(ast);
7877 break;
7878 case ZEND_AST_HALT_COMPILER:
7879 zend_compile_halt_compiler(ast);
7880 break;
7881 default:
7882 {
7883 znode result;
7884 zend_compile_expr(&result, ast);
7885 zend_do_free(&result);
7886 }
7887 }
7888
7889 if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) {
7890 zend_emit_tick();
7891 }
7892 }
7893 /* }}} */
7894
zend_compile_expr(znode * result,zend_ast * ast)7895 void zend_compile_expr(znode *result, zend_ast *ast) /* {{{ */
7896 {
7897 /* CG(zend_lineno) = ast->lineno; */
7898 CG(zend_lineno) = zend_ast_get_lineno(ast);
7899
7900 switch (ast->kind) {
7901 case ZEND_AST_ZVAL:
7902 ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast));
7903 result->op_type = IS_CONST;
7904 return;
7905 case ZEND_AST_ZNODE:
7906 *result = *zend_ast_get_znode(ast);
7907 return;
7908 case ZEND_AST_VAR:
7909 case ZEND_AST_DIM:
7910 case ZEND_AST_PROP:
7911 case ZEND_AST_STATIC_PROP:
7912 case ZEND_AST_CALL:
7913 case ZEND_AST_METHOD_CALL:
7914 case ZEND_AST_STATIC_CALL:
7915 zend_compile_var(result, ast, BP_VAR_R);
7916 return;
7917 case ZEND_AST_ASSIGN:
7918 zend_compile_assign(result, ast);
7919 return;
7920 case ZEND_AST_ASSIGN_REF:
7921 zend_compile_assign_ref(result, ast);
7922 return;
7923 case ZEND_AST_NEW:
7924 zend_compile_new(result, ast);
7925 return;
7926 case ZEND_AST_CLONE:
7927 zend_compile_clone(result, ast);
7928 return;
7929 case ZEND_AST_ASSIGN_OP:
7930 zend_compile_compound_assign(result, ast);
7931 return;
7932 case ZEND_AST_BINARY_OP:
7933 zend_compile_binary_op(result, ast);
7934 return;
7935 case ZEND_AST_GREATER:
7936 case ZEND_AST_GREATER_EQUAL:
7937 zend_compile_greater(result, ast);
7938 return;
7939 case ZEND_AST_UNARY_OP:
7940 zend_compile_unary_op(result, ast);
7941 return;
7942 case ZEND_AST_UNARY_PLUS:
7943 case ZEND_AST_UNARY_MINUS:
7944 zend_compile_unary_pm(result, ast);
7945 return;
7946 case ZEND_AST_AND:
7947 case ZEND_AST_OR:
7948 zend_compile_short_circuiting(result, ast);
7949 return;
7950 case ZEND_AST_POST_INC:
7951 case ZEND_AST_POST_DEC:
7952 zend_compile_post_incdec(result, ast);
7953 return;
7954 case ZEND_AST_PRE_INC:
7955 case ZEND_AST_PRE_DEC:
7956 zend_compile_pre_incdec(result, ast);
7957 return;
7958 case ZEND_AST_CAST:
7959 zend_compile_cast(result, ast);
7960 return;
7961 case ZEND_AST_CONDITIONAL:
7962 zend_compile_conditional(result, ast);
7963 return;
7964 case ZEND_AST_COALESCE:
7965 zend_compile_coalesce(result, ast);
7966 return;
7967 case ZEND_AST_PRINT:
7968 zend_compile_print(result, ast);
7969 return;
7970 case ZEND_AST_EXIT:
7971 zend_compile_exit(result, ast);
7972 return;
7973 case ZEND_AST_YIELD:
7974 zend_compile_yield(result, ast);
7975 return;
7976 case ZEND_AST_YIELD_FROM:
7977 zend_compile_yield_from(result, ast);
7978 return;
7979 case ZEND_AST_INSTANCEOF:
7980 zend_compile_instanceof(result, ast);
7981 return;
7982 case ZEND_AST_INCLUDE_OR_EVAL:
7983 zend_compile_include_or_eval(result, ast);
7984 return;
7985 case ZEND_AST_ISSET:
7986 case ZEND_AST_EMPTY:
7987 zend_compile_isset_or_empty(result, ast);
7988 return;
7989 case ZEND_AST_SILENCE:
7990 zend_compile_silence(result, ast);
7991 return;
7992 case ZEND_AST_SHELL_EXEC:
7993 zend_compile_shell_exec(result, ast);
7994 return;
7995 case ZEND_AST_ARRAY:
7996 zend_compile_array(result, ast);
7997 return;
7998 case ZEND_AST_CONST:
7999 zend_compile_const(result, ast);
8000 return;
8001 case ZEND_AST_CLASS_CONST:
8002 zend_compile_class_const(result, ast);
8003 return;
8004 case ZEND_AST_ENCAPS_LIST:
8005 zend_compile_encaps_list(result, ast);
8006 return;
8007 case ZEND_AST_MAGIC_CONST:
8008 zend_compile_magic_const(result, ast);
8009 return;
8010 case ZEND_AST_CLOSURE:
8011 zend_compile_func_decl(result, ast);
8012 return;
8013 default:
8014 ZEND_ASSERT(0 /* not supported */);
8015 }
8016 }
8017 /* }}} */
8018
zend_compile_var(znode * result,zend_ast * ast,uint32_t type)8019 void zend_compile_var(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
8020 {
8021 CG(zend_lineno) = zend_ast_get_lineno(ast);
8022
8023 switch (ast->kind) {
8024 case ZEND_AST_VAR:
8025 zend_compile_simple_var(result, ast, type, 0);
8026 return;
8027 case ZEND_AST_DIM:
8028 zend_compile_dim(result, ast, type);
8029 return;
8030 case ZEND_AST_PROP:
8031 zend_compile_prop(result, ast, type);
8032 return;
8033 case ZEND_AST_STATIC_PROP:
8034 zend_compile_static_prop(result, ast, type, 0);
8035 return;
8036 case ZEND_AST_CALL:
8037 zend_compile_call(result, ast, type);
8038 return;
8039 case ZEND_AST_METHOD_CALL:
8040 zend_compile_method_call(result, ast, type);
8041 return;
8042 case ZEND_AST_STATIC_CALL:
8043 zend_compile_static_call(result, ast, type);
8044 return;
8045 case ZEND_AST_ZNODE:
8046 *result = *zend_ast_get_znode(ast);
8047 return;
8048 default:
8049 if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) {
8050 zend_error_noreturn(E_COMPILE_ERROR,
8051 "Cannot use temporary expression in write context");
8052 }
8053
8054 zend_compile_expr(result, ast);
8055 return;
8056 }
8057 }
8058 /* }}} */
8059
zend_delayed_compile_var(znode * result,zend_ast * ast,uint32_t type)8060 void zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
8061 {
8062 zend_op *opline;
8063 switch (ast->kind) {
8064 case ZEND_AST_VAR:
8065 zend_compile_simple_var(result, ast, type, 1);
8066 return;
8067 case ZEND_AST_DIM:
8068 opline = zend_delayed_compile_dim(result, ast, type);
8069 zend_adjust_for_fetch_type(opline, type);
8070 return;
8071 case ZEND_AST_PROP:
8072 opline = zend_delayed_compile_prop(result, ast, type);
8073 zend_adjust_for_fetch_type(opline, type);
8074 return;
8075 case ZEND_AST_STATIC_PROP:
8076 zend_compile_static_prop(result, ast, type, 1);
8077 return;
8078 default:
8079 zend_compile_var(result, ast, type);
8080 return;
8081 }
8082 }
8083 /* }}} */
8084
zend_eval_const_expr(zend_ast ** ast_ptr)8085 void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
8086 {
8087 zend_ast *ast = *ast_ptr;
8088 zval result;
8089
8090 if (!ast) {
8091 return;
8092 }
8093
8094 switch (ast->kind) {
8095 case ZEND_AST_BINARY_OP:
8096 zend_eval_const_expr(&ast->child[0]);
8097 zend_eval_const_expr(&ast->child[1]);
8098 if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
8099 return;
8100 }
8101
8102 if (!zend_try_ct_eval_binary_op(&result, ast->attr,
8103 zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]))
8104 ) {
8105 return;
8106 }
8107 break;
8108 case ZEND_AST_GREATER:
8109 case ZEND_AST_GREATER_EQUAL:
8110 zend_eval_const_expr(&ast->child[0]);
8111 zend_eval_const_expr(&ast->child[1]);
8112 if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
8113 return;
8114 }
8115
8116 zend_ct_eval_greater(&result, ast->kind,
8117 zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1]));
8118 break;
8119 case ZEND_AST_AND:
8120 case ZEND_AST_OR:
8121 {
8122 int i;
8123 for (i = 0; i <= 1; i++) {
8124 zend_eval_const_expr(&ast->child[i]);
8125 if (ast->child[i]->kind == ZEND_AST_ZVAL) {
8126 if (zend_is_true(zend_ast_get_zval(ast->child[i])) == (ast->kind == ZEND_AST_OR)) {
8127 ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR);
8128 return;
8129 }
8130 }
8131 }
8132
8133 if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
8134 return;
8135 }
8136
8137 if (ast->kind == ZEND_AST_OR) {
8138 ZVAL_BOOL(&result, zend_is_true(zend_ast_get_zval(ast->child[0])) || zend_is_true(zend_ast_get_zval(ast->child[1])));
8139 } else {
8140 ZVAL_BOOL(&result, zend_is_true(zend_ast_get_zval(ast->child[0])) && zend_is_true(zend_ast_get_zval(ast->child[1])));
8141 }
8142 break;
8143 }
8144 case ZEND_AST_UNARY_OP:
8145 zend_eval_const_expr(&ast->child[0]);
8146 if (ast->child[0]->kind != ZEND_AST_ZVAL) {
8147 return;
8148 }
8149
8150 zend_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]));
8151 break;
8152 case ZEND_AST_UNARY_PLUS:
8153 case ZEND_AST_UNARY_MINUS:
8154 zend_eval_const_expr(&ast->child[0]);
8155 if (ast->child[0]->kind != ZEND_AST_ZVAL) {
8156 return;
8157 }
8158
8159 if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) {
8160 return;
8161 }
8162 break;
8163 case ZEND_AST_COALESCE:
8164 /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
8165 if (ast->child[0]->kind == ZEND_AST_DIM) {
8166 ast->child[0]->attr = ZEND_DIM_IS;
8167 }
8168 zend_eval_const_expr(&ast->child[0]);
8169
8170 if (ast->child[0]->kind != ZEND_AST_ZVAL) {
8171 /* ensure everything was compile-time evaluated at least once */
8172 zend_eval_const_expr(&ast->child[1]);
8173 return;
8174 }
8175
8176 if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) {
8177 zend_eval_const_expr(&ast->child[1]);
8178 *ast_ptr = ast->child[1];
8179 ast->child[1] = NULL;
8180 zend_ast_destroy(ast);
8181 } else {
8182 *ast_ptr = ast->child[0];
8183 ast->child[0] = NULL;
8184 zend_ast_destroy(ast);
8185 }
8186 return;
8187 case ZEND_AST_CONDITIONAL:
8188 {
8189 zend_ast **child, *child_ast;
8190 zend_eval_const_expr(&ast->child[0]);
8191 if (ast->child[0]->kind != ZEND_AST_ZVAL) {
8192 /* ensure everything was compile-time evaluated at least once */
8193 if (ast->child[1]) {
8194 zend_eval_const_expr(&ast->child[1]);
8195 }
8196 zend_eval_const_expr(&ast->child[2]);
8197 return;
8198 }
8199
8200 child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))];
8201 if (*child == NULL) {
8202 child--;
8203 }
8204 child_ast = *child;
8205 *child = NULL;
8206 zend_ast_destroy(ast);
8207 *ast_ptr = child_ast;
8208 zend_eval_const_expr(ast_ptr);
8209 return;
8210 }
8211 case ZEND_AST_DIM:
8212 {
8213 /* constant expression should be always read context ... */
8214 zval *container, *dim;
8215
8216 if (ast->child[1] == NULL) {
8217 zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
8218 }
8219
8220 /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */
8221 if (ast->attr == ZEND_DIM_IS && ast->child[0]->kind == ZEND_AST_DIM) {
8222 ast->child[0]->attr = ZEND_DIM_IS;
8223 }
8224
8225 zend_eval_const_expr(&ast->child[0]);
8226 zend_eval_const_expr(&ast->child[1]);
8227 if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) {
8228 return;
8229 }
8230
8231 container = zend_ast_get_zval(ast->child[0]);
8232 dim = zend_ast_get_zval(ast->child[1]);
8233
8234 if (Z_TYPE_P(container) == IS_ARRAY) {
8235 zval *el;
8236 if (Z_TYPE_P(dim) == IS_LONG) {
8237 el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim));
8238 if (el) {
8239 ZVAL_COPY(&result, el);
8240 } else {
8241 return;
8242 }
8243 } else if (Z_TYPE_P(dim) == IS_STRING) {
8244 el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim));
8245 if (el) {
8246 ZVAL_COPY(&result, el);
8247 } else {
8248 return;
8249 }
8250 } else {
8251 return; /* warning... handle at runtime */
8252 }
8253 } else if (Z_TYPE_P(container) == IS_STRING) {
8254 zend_long offset;
8255 zend_uchar c;
8256 if (Z_TYPE_P(dim) == IS_LONG) {
8257 offset = Z_LVAL_P(dim);
8258 } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
8259 return;
8260 }
8261 if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
8262 return;
8263 }
8264 c = (zend_uchar) Z_STRVAL_P(container)[offset];
8265 if (CG(one_char_string)[c]) {
8266 ZVAL_INTERNED_STR(&result, CG(one_char_string)[c]);
8267 } else {
8268 ZVAL_NEW_STR(&result, zend_string_init((char *) &c, 1, 0));
8269 }
8270 } else if (Z_TYPE_P(container) <= IS_FALSE) {
8271 ZVAL_NULL(&result);
8272 } else {
8273 return;
8274 }
8275 break;
8276 }
8277 case ZEND_AST_ARRAY:
8278 if (!zend_try_ct_eval_array(&result, ast)) {
8279 return;
8280 }
8281 break;
8282 case ZEND_AST_MAGIC_CONST:
8283 if (!zend_try_ct_eval_magic_const(&result, ast)) {
8284 return;
8285 }
8286 break;
8287 case ZEND_AST_CONST:
8288 {
8289 zend_ast *name_ast = ast->child[0];
8290 zend_bool is_fully_qualified;
8291 zend_string *resolved_name = zend_resolve_const_name(
8292 zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified);
8293
8294 if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) {
8295 zend_string_release(resolved_name);
8296 return;
8297 }
8298
8299 zend_string_release(resolved_name);
8300 break;
8301 }
8302 case ZEND_AST_CLASS_CONST:
8303 {
8304 zend_ast *class_ast = ast->child[0];
8305 zend_ast *name_ast = ast->child[1];
8306 zend_string *resolved_name;
8307
8308 if (zend_try_compile_const_expr_resolve_class_name(&result, class_ast, name_ast, 0)) {
8309 if (Z_TYPE(result) == IS_NULL) {
8310 if (zend_get_class_fetch_type(zend_ast_get_str(class_ast)) == ZEND_FETCH_CLASS_SELF) {
8311 zend_ast_destroy(ast);
8312 *ast_ptr = zend_ast_create_ex(ZEND_AST_MAGIC_CONST, T_CLASS_C);
8313 }
8314 return;
8315 }
8316 break;
8317 }
8318
8319 zend_eval_const_expr(&ast->child[0]);
8320 zend_eval_const_expr(&ast->child[1]);
8321
8322 class_ast = ast->child[0];
8323 name_ast = ast->child[1];
8324
8325 if (name_ast->kind == ZEND_AST_ZVAL && zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "class")) {
8326 zend_error_noreturn(E_COMPILE_ERROR,
8327 "Dynamic class names are not allowed in compile-time ::class fetch");
8328 }
8329
8330 if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) {
8331 return;
8332 }
8333
8334 resolved_name = zend_resolve_class_name_ast(class_ast);
8335
8336 if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) {
8337 zend_string_release(resolved_name);
8338 return;
8339 }
8340
8341 zend_string_release(resolved_name);
8342 break;
8343 }
8344
8345 default:
8346 return;
8347 }
8348
8349 zend_ast_destroy(ast);
8350 *ast_ptr = zend_ast_create_zval(&result);
8351 }
8352 /* }}} */
8353
8354 /*
8355 * Local variables:
8356 * tab-width: 4
8357 * c-basic-offset: 4
8358 * indent-tabs-mode: t
8359 * End:
8360 */
8361