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