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