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