1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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
57static inline void zend_alloc_cache_slot(uint32_t literal) {
58    zend_op_array *op_array = CG(active_op_array);
59    Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
60    op_array->cache_size += sizeof(void*);
61}
62
63#define POLYMORPHIC_CACHE_SLOT_SIZE 2
64
65static inline void zend_alloc_polymorphic_cache_slot(uint32_t literal) {
66    zend_op_array *op_array = CG(active_op_array);
67    Z_CACHE_SLOT(op_array->literals[literal]) = op_array->cache_size;
68    op_array->cache_size += POLYMORPHIC_CACHE_SLOT_SIZE * sizeof(void*);
69}
70
71ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
72ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
73
74#ifndef ZTS
75ZEND_API zend_compiler_globals compiler_globals;
76ZEND_API zend_executor_globals executor_globals;
77#endif
78
79static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
80{
81    zend_property_info *property_info = Z_PTR_P(zv);
82
83    zend_string_release(property_info->name);
84    free(property_info);
85}
86/* }}} */
87
88static zend_string *zend_new_interned_string_safe(zend_string *str) /* {{{ */ {
89    zend_string *interned_str;
90
91    zend_string_addref(str);
92    interned_str = zend_new_interned_string(str);
93    if (str != interned_str) {
94        return interned_str;
95    } else {
96        zend_string_release(str);
97        return str;
98    }
99}
100/* }}} */
101
102static zend_string *zend_build_runtime_definition_key(zend_string *name, unsigned char *lex_pos) /* {{{ */
103{
104    zend_string *result;
105    char char_pos_buf[32];
106    size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
107    zend_string *filename = CG(active_op_array)->filename;
108
109    /* NULL, name length, filename length, last accepting char position length */
110    result = zend_string_alloc(1 + ZSTR_LEN(name) + ZSTR_LEN(filename) + char_pos_len, 0);
111    sprintf(ZSTR_VAL(result), "%c%s%s%s", '\0', ZSTR_VAL(name), ZSTR_VAL(filename), char_pos_buf);
112    return zend_new_interned_string(result);
113}
114/* }}} */
115
116static zend_bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */
117{
118    const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
119    if (ns_separator != NULL) {
120        *result = ns_separator + 1;
121        *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result;
122        return 1;
123    }
124
125    return 0;
126}
127/* }}} */
128
129struct reserved_class_name {
130    const char *name;
131    size_t len;
132};
133static const struct reserved_class_name reserved_class_names[] = {
134    {ZEND_STRL("bool")},
135    {ZEND_STRL("false")},
136    {ZEND_STRL("float")},
137    {ZEND_STRL("int")},
138    {ZEND_STRL("null")},
139    {ZEND_STRL("parent")},
140    {ZEND_STRL("self")},
141    {ZEND_STRL("static")},
142    {ZEND_STRL("string")},
143    {ZEND_STRL("true")},
144    {NULL, 0}
145};
146
147static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
148{
149    const struct reserved_class_name *reserved = reserved_class_names;
150
151    const char *uqname = ZSTR_VAL(name);
152    size_t uqname_len = ZSTR_LEN(name);
153    zend_get_unqualified_name(name, &uqname, &uqname_len);
154
155    for (; reserved->name; ++reserved) {
156        if (uqname_len == reserved->len
157            && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
158        ) {
159            return 1;
160        }
161    }
162
163    return 0;
164}
165/* }}} */
166
167ZEND_API void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
168{
169    if (zend_is_reserved_class_name(name)) {
170        zend_error_noreturn(E_COMPILE_ERROR,
171            "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(name));
172    }
173}
174/* }}} */
175
176typedef struct _builtin_type_info {
177    const char* name;
178    const size_t name_len;
179    const zend_uchar type;
180} builtin_type_info;
181
182static const builtin_type_info builtin_types[] = {
183    {"int", sizeof("int") - 1, IS_LONG},
184    {"float", sizeof("float") - 1, IS_DOUBLE},
185    {"string", sizeof("string") - 1, IS_STRING},
186    {"bool", sizeof("bool") - 1, _IS_BOOL},
187    {NULL, 0, IS_UNDEF}
188};
189
190
191static zend_always_inline zend_uchar zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */
192{
193    const builtin_type_info *info = &builtin_types[0];
194
195    for (; info->name; ++info) {
196        if (ZSTR_LEN(name) == info->name_len
197            && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0
198        ) {
199            return info->type;
200        }
201    }
202
203    return 0;
204}
205/* }}} */
206
207
208void zend_oparray_context_begin(zend_oparray_context *prev_context) /* {{{ */
209{
210    *prev_context = CG(context);
211    CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE;
212    CG(context).vars_size = 0;
213    CG(context).literals_size = 0;
214    CG(context).backpatch_count = 0;
215    CG(context).in_finally = 0;
216    CG(context).fast_call_var = -1;
217    CG(context).current_brk_cont = -1;
218    CG(context).last_brk_cont = 0;
219    CG(context).brk_cont_array = NULL;
220    CG(context).labels = NULL;
221}
222/* }}} */
223
224void zend_oparray_context_end(zend_oparray_context *prev_context) /* {{{ */
225{
226    if (CG(context).brk_cont_array) {
227        efree(CG(context).brk_cont_array);
228    }
229    if (CG(context).labels) {
230        zend_hash_destroy(CG(context).labels);
231        FREE_HASHTABLE(CG(context).labels);
232        CG(context).labels = NULL;
233    }
234    CG(context) = *prev_context;
235}
236/* }}} */
237
238static void zend_reset_import_tables(void) /* {{{ */
239{
240    if (FC(imports)) {
241        zend_hash_destroy(FC(imports));
242        efree(FC(imports));
243        FC(imports) = NULL;
244    }
245
246    if (FC(imports_function)) {
247        zend_hash_destroy(FC(imports_function));
248        efree(FC(imports_function));
249        FC(imports_function) = NULL;
250    }
251
252    if (FC(imports_const)) {
253        zend_hash_destroy(FC(imports_const));
254        efree(FC(imports_const));
255        FC(imports_const) = NULL;
256    }
257}
258/* }}} */
259
260static void zend_end_namespace(void) /* {{{ */ {
261    FC(in_namespace) = 0;
262    zend_reset_import_tables();
263    if (FC(current_namespace)) {
264        zend_string_release(FC(current_namespace));
265        FC(current_namespace) = NULL;
266    }
267}
268/* }}} */
269
270void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */
271{
272    *prev_context = CG(file_context);
273    FC(imports) = NULL;
274    FC(imports_function) = NULL;
275    FC(imports_const) = NULL;
276    FC(current_namespace) = NULL;
277    FC(in_namespace) = 0;
278    FC(has_bracketed_namespaces) = 0;
279    FC(declarables).ticks = 0;
280}
281/* }}} */
282
283void zend_file_context_end(zend_file_context *prev_context) /* {{{ */
284{
285    zend_end_namespace();
286    CG(file_context) = *prev_context;
287}
288/* }}} */
289
290void zend_init_compiler_data_structures(void) /* {{{ */
291{
292    zend_stack_init(&CG(loop_var_stack), sizeof(znode));
293    zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op));
294    CG(active_class_entry) = NULL;
295    CG(in_compilation) = 0;
296    CG(start_lineno) = 0;
297    zend_hash_init(&CG(const_filenames), 8, NULL, NULL, 0);
298
299    CG(encoding_declared) = 0;
300}
301/* }}} */
302
303ZEND_API void file_handle_dtor(zend_file_handle *fh) /* {{{ */
304{
305
306    zend_file_handle_dtor(fh);
307}
308/* }}} */
309
310void init_compiler(void) /* {{{ */
311{
312    CG(arena) = zend_arena_create(64 * 1024);
313    CG(active_op_array) = NULL;
314    memset(&CG(context), 0, sizeof(CG(context)));
315    zend_init_compiler_data_structures();
316    zend_init_rsrc_list();
317    zend_hash_init(&CG(filenames_table), 8, NULL, free_string_zval, 0);
318    zend_llist_init(&CG(open_files), sizeof(zend_file_handle), (void (*)(void *)) file_handle_dtor, 0);
319    CG(unclean_shutdown) = 0;
320}
321/* }}} */
322
323void shutdown_compiler(void) /* {{{ */
324{
325    zend_stack_destroy(&CG(loop_var_stack));
326    zend_stack_destroy(&CG(delayed_oplines_stack));
327    zend_hash_destroy(&CG(filenames_table));
328    zend_hash_destroy(&CG(const_filenames));
329    zend_arena_destroy(CG(arena));
330}
331/* }}} */
332
333ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */
334{
335    zend_string *p;
336
337    p = zend_hash_find_ptr(&CG(filenames_table), new_compiled_filename);
338    if (p != NULL) {
339        CG(compiled_filename) = p;
340        return p;
341    }
342    p = zend_string_copy(new_compiled_filename);
343    zend_hash_update_ptr(&CG(filenames_table), new_compiled_filename, p);
344    CG(compiled_filename) = p;
345    return p;
346}
347/* }}} */
348
349ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */
350{
351    CG(compiled_filename) = original_compiled_filename;
352}
353/* }}} */
354
355ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */
356{
357    return CG(compiled_filename);
358}
359/* }}} */
360
361ZEND_API int zend_get_compiled_lineno(void) /* {{{ */
362{
363    return CG(zend_lineno);
364}
365/* }}} */
366
367ZEND_API zend_bool zend_is_compiling(void) /* {{{ */
368{
369    return CG(in_compilation);
370}
371/* }}} */
372
373static uint32_t get_temporary_variable(zend_op_array *op_array) /* {{{ */
374{
375    return (uint32_t)op_array->T++;
376}
377/* }}} */
378
379static int lookup_cv(zend_op_array *op_array, zend_string* name) /* {{{ */{
380    int i = 0;
381    zend_ulong hash_value = zend_string_hash_val(name);
382
383    while (i < op_array->last_var) {
384        if (ZSTR_VAL(op_array->vars[i]) == ZSTR_VAL(name) ||
385            (ZSTR_H(op_array->vars[i]) == hash_value &&
386             ZSTR_LEN(op_array->vars[i]) == ZSTR_LEN(name) &&
387             memcmp(ZSTR_VAL(op_array->vars[i]), ZSTR_VAL(name), ZSTR_LEN(name)) == 0)) {
388            zend_string_release(name);
389            return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
390        }
391        i++;
392    }
393    i = op_array->last_var;
394    op_array->last_var++;
395    if (op_array->last_var > CG(context).vars_size) {
396        CG(context).vars_size += 16; /* FIXME */
397        op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*));
398    }
399
400    op_array->vars[i] = zend_new_interned_string(name);
401    return (int)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, i);
402}
403/* }}} */
404
405void zend_del_literal(zend_op_array *op_array, int n) /* {{{ */
406{
407    zval_dtor(CT_CONSTANT_EX(op_array, n));
408    if (n + 1 == op_array->last_literal) {
409        op_array->last_literal--;
410    } else {
411        ZVAL_UNDEF(CT_CONSTANT_EX(op_array, n));
412    }
413}
414/* }}} */
415
416/* Common part of zend_add_literal and zend_append_individual_literal */
417static inline void zend_insert_literal(zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */
418{
419    if (Z_TYPE_P(zv) == IS_STRING || Z_TYPE_P(zv) == IS_CONSTANT) {
420        zend_string_hash_val(Z_STR_P(zv));
421        Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
422        if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
423            Z_TYPE_FLAGS_P(zv) &= ~ (IS_TYPE_REFCOUNTED | IS_TYPE_COPYABLE);
424        }
425    }
426    ZVAL_COPY_VALUE(CT_CONSTANT_EX(op_array, literal_position), zv);
427    Z_CACHE_SLOT(op_array->literals[literal_position]) = -1;
428}
429/* }}} */
430
431/* Is used while compiling a function, using the context to keep track
432   of an approximate size to avoid to relocate to often.
433   Literals are truncated to actual size in the second compiler pass (pass_two()). */
434int zend_add_literal(zend_op_array *op_array, zval *zv) /* {{{ */
435{
436    int i = op_array->last_literal;
437    op_array->last_literal++;
438    if (i >= CG(context).literals_size) {
439        while (i >= CG(context).literals_size) {
440            CG(context).literals_size += 16; /* FIXME */
441        }
442        op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval));
443    }
444    zend_insert_literal(op_array, zv, i);
445    return i;
446}
447/* }}} */
448
449static inline int zend_add_literal_string(zend_op_array *op_array, zend_string **str) /* {{{ */
450{
451    int ret;
452    zval zv;
453    ZVAL_STR(&zv, *str);
454    ret = zend_add_literal(op_array, &zv);
455    *str = Z_STR(zv);
456    return ret;
457}
458/* }}} */
459
460static int zend_add_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
461{
462    /* Original name */
463    int ret = zend_add_literal_string(op_array, &name);
464
465    /* Lowercased name */
466    zend_string *lc_name = zend_string_tolower(name);
467    zend_add_literal_string(op_array, &lc_name);
468
469    return ret;
470}
471/* }}} */
472
473static int zend_add_ns_func_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
474{
475    const char *unqualified_name;
476    size_t unqualified_name_len;
477
478    /* Original name */
479    int ret = zend_add_literal_string(op_array, &name);
480
481    /* Lowercased name */
482    zend_string *lc_name = zend_string_tolower(name);
483    zend_add_literal_string(op_array, &lc_name);
484
485    /* Lowercased unqualfied name */
486    if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) {
487        lc_name = zend_string_alloc(unqualified_name_len, 0);
488        zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len);
489        zend_add_literal_string(op_array, &lc_name);
490    }
491
492    return ret;
493}
494/* }}} */
495
496static int zend_add_class_name_literal(zend_op_array *op_array, zend_string *name) /* {{{ */
497{
498    /* Original name */
499    int ret = zend_add_literal_string(op_array, &name);
500
501    /* Lowercased name */
502    zend_string *lc_name = zend_string_tolower(name);
503    zend_add_literal_string(op_array, &lc_name);
504
505    zend_alloc_cache_slot(ret);
506
507    return ret;
508}
509/* }}} */
510
511static int zend_add_const_name_literal(zend_op_array *op_array, zend_string *name, zend_bool unqualified) /* {{{ */
512{
513    zend_string *tmp_name;
514
515    int ret = zend_add_literal_string(op_array, &name);
516
517    size_t ns_len = 0, after_ns_len = ZSTR_LEN(name);
518    const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
519    if (after_ns) {
520        after_ns += 1;
521        ns_len = after_ns - ZSTR_VAL(name) - 1;
522        after_ns_len = ZSTR_LEN(name) - ns_len - 1;
523
524        /* lowercased namespace name & original constant name */
525        tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0);
526        zend_str_tolower(ZSTR_VAL(tmp_name), ns_len);
527        zend_add_literal_string(op_array, &tmp_name);
528
529        /* lowercased namespace name & lowercased constant name */
530        tmp_name = zend_string_tolower(name);
531        zend_add_literal_string(op_array, &tmp_name);
532
533        if (!unqualified) {
534            return ret;
535        }
536    } else {
537        after_ns = ZSTR_VAL(name);
538    }
539
540    /* original unqualified constant name */
541    tmp_name = zend_string_init(after_ns, after_ns_len, 0);
542    zend_add_literal_string(op_array, &tmp_name);
543
544    /* lowercased unqualified constant name */
545    tmp_name = zend_string_alloc(after_ns_len, 0);
546    zend_str_tolower_copy(ZSTR_VAL(tmp_name), after_ns, after_ns_len);
547    zend_add_literal_string(op_array, &tmp_name);
548
549    return ret;
550}
551/* }}} */
552
553#define LITERAL_STR(op, str) do { \
554        zval _c; \
555        ZVAL_STR(&_c, str); \
556        op.constant = zend_add_literal(CG(active_op_array), &_c); \
557    } while (0)
558
559void zend_stop_lexing(void)
560{
561    if(LANG_SCNG(on_event)) LANG_SCNG(on_event)(ON_STOP, END, 0);
562
563    LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit);
564}
565
566static inline void zend_begin_loop(const znode *loop_var) /* {{{ */
567{
568    zend_brk_cont_element *brk_cont_element;
569    int parent = CG(context).current_brk_cont;
570
571    CG(context).current_brk_cont = CG(context).last_brk_cont;
572    brk_cont_element = get_next_brk_cont_element(CG(active_op_array));
573    brk_cont_element->parent = parent;
574
575    if (loop_var) {
576        zend_stack_push(&CG(loop_var_stack), loop_var);
577        brk_cont_element->start = get_next_op_number(CG(active_op_array));
578    } else {
579        /* The start field is used to free temporary variables in case of exceptions.
580         * We won't try to free something of we don't have loop variable.  */
581        brk_cont_element->start = -1;
582    }
583}
584/* }}} */
585
586static inline void zend_end_loop(int cont_addr) /* {{{ */
587{
588    zend_brk_cont_element *brk_cont_element
589        = &CG(context).brk_cont_array[CG(context).current_brk_cont];
590    brk_cont_element->cont = cont_addr;
591    brk_cont_element->brk = get_next_op_number(CG(active_op_array));
592    CG(context).current_brk_cont = brk_cont_element->parent;
593
594    if (brk_cont_element->start >= 0) {
595        zend_stack_del_top(&CG(loop_var_stack));
596    }
597}
598/* }}} */
599
600void zend_do_free(znode *op1) /* {{{ */
601{
602    if (op1->op_type==IS_TMP_VAR) {
603        zend_op *opline = get_next_op(CG(active_op_array));
604
605        opline->opcode = ZEND_FREE;
606        SET_NODE(opline->op1, op1);
607        SET_UNUSED(opline->op2);
608    } else if (op1->op_type==IS_VAR) {
609        zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
610
611        while (opline->opcode == ZEND_END_SILENCE || opline->opcode == ZEND_EXT_FCALL_END || opline->opcode == ZEND_OP_DATA) {
612            opline--;
613        }
614        if (opline->result_type == IS_VAR
615            && opline->result.var == op1->u.op.var) {
616            if (opline->opcode == ZEND_FETCH_R ||
617                opline->opcode == ZEND_FETCH_DIM_R ||
618                opline->opcode == ZEND_FETCH_OBJ_R) {
619                /* It's very rare and useless case. It's better to use
620                   additional FREE opcode and simplify the FETCH handlers
621                   their selves */
622                opline = get_next_op(CG(active_op_array));
623                opline->opcode = ZEND_FREE;
624                SET_NODE(opline->op1, op1);
625                SET_UNUSED(opline->op2);
626            } else {
627                opline->result_type |= EXT_TYPE_UNUSED;
628            }
629        } else {
630            while (opline >= CG(active_op_array)->opcodes) {
631                if (opline->opcode == ZEND_FETCH_LIST &&
632                    opline->op1_type == IS_VAR &&
633                    opline->op1.var == op1->u.op.var) {
634                    opline = get_next_op(CG(active_op_array));
635
636                    opline->opcode = ZEND_FREE;
637                    SET_NODE(opline->op1, op1);
638                    SET_UNUSED(opline->op2);
639                    return;
640                }
641                if (opline->result_type==IS_VAR
642                    && opline->result.var == op1->u.op.var) {
643                    if (opline->opcode == ZEND_NEW) {
644                        opline->result_type |= EXT_TYPE_UNUSED;
645                        opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
646                        while (opline->opcode != ZEND_DO_FCALL || opline->op1.num != ZEND_CALL_CTOR) {
647                            opline--;
648                        }
649                        opline->op1.num |= ZEND_CALL_CTOR_RESULT_UNUSED;
650                    }
651                    break;
652                }
653                opline--;
654            }
655        }
656    } else if (op1->op_type == IS_CONST) {
657        /* Destroy value without using GC: When opcache moves arrays into SHM it will
658         * free the zend_array structure, so references to it from outside the op array
659         * become invalid. GC would cause such a reference in the root buffer. */
660        zval_ptr_dtor_nogc(&op1->u.constant);
661    }
662}
663/* }}} */
664
665uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
666{
667    uint32_t new_flags = flags | new_flag;
668    if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
669        zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
670    }
671    if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
672        zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
673    }
674    if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
675        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class");
676    }
677    return new_flags;
678}
679/* }}} */
680
681uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
682{
683    uint32_t new_flags = flags | new_flag;
684    if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) {
685        zend_error_noreturn(E_COMPILE_ERROR, "Multiple access type modifiers are not allowed");
686    }
687    if ((flags & ZEND_ACC_ABSTRACT) && (new_flag & ZEND_ACC_ABSTRACT)) {
688        zend_error_noreturn(E_COMPILE_ERROR, "Multiple abstract modifiers are not allowed");
689    }
690    if ((flags & ZEND_ACC_STATIC) && (new_flag & ZEND_ACC_STATIC)) {
691        zend_error_noreturn(E_COMPILE_ERROR, "Multiple static modifiers are not allowed");
692    }
693    if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) {
694        zend_error_noreturn(E_COMPILE_ERROR, "Multiple final modifiers are not allowed");
695    }
696    if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) {
697        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use the final modifier on an abstract class member");
698    }
699    return new_flags;
700}
701/* }}} */
702
703zend_string *zend_concat3(char *str1, size_t str1_len, char *str2, size_t str2_len, char *str3, size_t str3_len) /* {{{ */
704{
705    size_t len = str1_len + str2_len + str3_len;
706    zend_string *res = zend_string_alloc(len, 0);
707
708    memcpy(ZSTR_VAL(res), str1, str1_len);
709    memcpy(ZSTR_VAL(res) + str1_len, str2, str2_len);
710    memcpy(ZSTR_VAL(res) + str1_len + str2_len, str3, str3_len);
711    ZSTR_VAL(res)[len] = '\0';
712
713    return res;
714}
715
716zend_string *zend_concat_names(char *name1, size_t name1_len, char *name2, size_t name2_len) {
717    return zend_concat3(name1, name1_len, "\\", 1, name2, name2_len);
718}
719
720zend_string *zend_prefix_with_ns(zend_string *name) {
721    if (FC(current_namespace)) {
722        zend_string *ns = FC(current_namespace);
723        return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name));
724    } else {
725        return zend_string_copy(name);
726    }
727}
728
729void *zend_hash_find_ptr_lc(HashTable *ht, const char *str, size_t len) {
730    void *result;
731    zend_string *lcname;
732    ALLOCA_FLAG(use_heap);
733
734    ZSTR_ALLOCA_ALLOC(lcname, len, use_heap);
735    zend_str_tolower_copy(ZSTR_VAL(lcname), str, len);
736    result = zend_hash_find_ptr(ht, lcname);
737    ZSTR_ALLOCA_FREE(lcname, use_heap);
738
739    return result;
740}
741
742zend_string *zend_resolve_non_class_name(
743    zend_string *name, uint32_t type, zend_bool *is_fully_qualified,
744    zend_bool case_sensitive, HashTable *current_import_sub
745) {
746    char *compound;
747    *is_fully_qualified = 0;
748
749    if (ZSTR_VAL(name)[0] == '\\') {
750        /* Remove \ prefix (only relevant if this is a string rather than a label) */
751        return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
752    }
753
754    if (type == ZEND_NAME_FQ) {
755        *is_fully_qualified = 1;
756        return zend_string_copy(name);
757    }
758
759    if (type == ZEND_NAME_RELATIVE) {
760        *is_fully_qualified = 1;
761        return zend_prefix_with_ns(name);
762    }
763
764    if (current_import_sub) {
765        /* If an unqualified name is a function/const alias, replace it. */
766        zend_string *import_name;
767        if (case_sensitive) {
768            import_name = zend_hash_find_ptr(current_import_sub, name);
769        } else {
770            import_name = zend_hash_find_ptr_lc(current_import_sub, ZSTR_VAL(name), ZSTR_LEN(name));
771        }
772
773        if (import_name) {
774            *is_fully_qualified = 1;
775            return zend_string_copy(import_name);
776        }
777    }
778
779    compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
780    if (compound) {
781        *is_fully_qualified = 1;
782    }
783
784    if (compound && FC(imports)) {
785        /* If the first part of a qualified name is an alias, substitute it. */
786        size_t len = compound - ZSTR_VAL(name);
787        zend_string *import_name = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
788
789        if (import_name) {
790            return zend_concat_names(
791                ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
792        }
793    }
794
795    return zend_prefix_with_ns(name);
796}
797/* }}} */
798
799zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */
800{
801    return zend_resolve_non_class_name(
802        name, type, is_fully_qualified, 0, FC(imports_function));
803}
804/* }}} */
805
806zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, zend_bool *is_fully_qualified) /* {{{ */ {
807    return zend_resolve_non_class_name(
808        name, type, is_fully_qualified, 1, FC(imports_const));
809}
810/* }}} */
811
812zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */
813{
814    char *compound;
815
816    if (type == ZEND_NAME_RELATIVE) {
817        return zend_prefix_with_ns(name);
818    }
819
820    if (type == ZEND_NAME_FQ || ZSTR_VAL(name)[0] == '\\') {
821        /* Remove \ prefix (only relevant if this is a string rather than a label) */
822        if (ZSTR_VAL(name)[0] == '\\') {
823            name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0);
824        } else {
825            zend_string_addref(name);
826        }
827        /* Ensure that \self, \parent and \static are not used */
828        if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
829            zend_error_noreturn(E_COMPILE_ERROR, "'\\%s' is an invalid class name", ZSTR_VAL(name));
830        }
831        return name;
832    }
833
834    if (FC(imports)) {
835        compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
836        if (compound) {
837            /* If the first part of a qualified name is an alias, substitute it. */
838            size_t len = compound - ZSTR_VAL(name);
839            zend_string *import_name =
840                zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), len);
841
842            if (import_name) {
843                return zend_concat_names(
844                    ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1);
845            }
846        } else {
847            /* If an unqualified name is an alias, replace it. */
848            zend_string *import_name
849                = zend_hash_find_ptr_lc(FC(imports), ZSTR_VAL(name), ZSTR_LEN(name));
850
851            if (import_name) {
852                return zend_string_copy(import_name);
853            }
854        }
855    }
856
857    /* If not fully qualified and not an alias, prepend the current namespace */
858    return zend_prefix_with_ns(name);
859}
860/* }}} */
861
862zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */
863{
864    zval *class_name = zend_ast_get_zval(ast);
865    if (Z_TYPE_P(class_name) != IS_STRING) {
866        zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name");
867    }
868    return zend_resolve_class_name(Z_STR_P(class_name), ast->attr);
869}
870/* }}} */
871
872static void ptr_dtor(zval *zv) /* {{{ */
873{
874    efree(Z_PTR_P(zv));
875}
876/* }}} */
877
878static void str_dtor(zval *zv)  /* {{{ */ {
879    zend_string_release(Z_STR_P(zv));
880}
881/* }}} */
882
883static zend_bool zend_is_call(zend_ast *ast);
884
885static int generate_free_loop_var(znode *var) /* {{{ */
886{
887    switch (var->op_type) {
888        case IS_UNUSED:
889            /* Stack separator on function boundary, stop applying */
890            return 1;
891        case IS_VAR:
892        case IS_TMP_VAR:
893        {
894            zend_op *opline = get_next_op(CG(active_op_array));
895
896            opline->opcode = var->flag ? ZEND_FE_FREE : ZEND_FREE;
897            SET_NODE(opline->op1, var);
898            SET_UNUSED(opline->op2);
899        }
900    }
901
902    return 0;
903}
904/* }}} */
905
906static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */
907{
908    zend_op_array *op_array = CG(active_op_array);
909    uint32_t try_catch_offset = op_array->last_try_catch++;
910    zend_try_catch_element *elem;
911
912    op_array->try_catch_array = safe_erealloc(
913        op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0);
914
915    elem = &op_array->try_catch_array[try_catch_offset];
916    elem->try_op = try_op;
917    elem->catch_op = 0;
918    elem->finally_op = 0;
919    elem->finally_end = 0;
920
921    return try_catch_offset;
922}
923/* }}} */
924
925ZEND_API void function_add_ref(zend_function *function) /* {{{ */
926{
927    if (function->type == ZEND_USER_FUNCTION) {
928        zend_op_array *op_array = &function->op_array;
929
930        if (op_array->refcount) {
931            (*op_array->refcount)++;
932        }
933        if (op_array->static_variables) {
934            if (!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
935                GC_REFCOUNT(op_array->static_variables)++;
936            }
937        }
938        op_array->run_time_cache = NULL;
939    } else if (function->type == ZEND_INTERNAL_FUNCTION) {
940        if (function->common.function_name) {
941            zend_string_addref(function->common.function_name);
942        }
943    }
944}
945/* }}} */
946
947ZEND_API int do_bind_function(const zend_op_array *op_array, const zend_op *opline, HashTable *function_table, zend_bool compile_time) /* {{{ */
948{
949    zend_function *function, *new_function;
950    zval *op1, *op2;
951
952    if (compile_time) {
953        op1 = CT_CONSTANT_EX(op_array, opline->op1.constant);
954        op2 = CT_CONSTANT_EX(op_array, opline->op2.constant);
955    } else {
956        op1 = RT_CONSTANT(op_array, opline->op1);
957        op2 = RT_CONSTANT(op_array, opline->op2);
958    }
959
960    function = zend_hash_find_ptr(function_table, Z_STR_P(op1));
961    new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
962    memcpy(new_function, function, sizeof(zend_op_array));
963    if (zend_hash_add_ptr(function_table, Z_STR_P(op2), new_function) == NULL) {
964        int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR;
965        zend_function *old_function;
966
967        if ((old_function = zend_hash_find_ptr(function_table, Z_STR_P(op2))) != NULL
968            && old_function->type == ZEND_USER_FUNCTION
969            && old_function->op_array.last > 0) {
970            zend_error_noreturn(error_level, "Cannot redeclare %s() (previously declared in %s:%d)",
971                        ZSTR_VAL(function->common.function_name),
972                        ZSTR_VAL(old_function->op_array.filename),
973                        old_function->op_array.opcodes[0].lineno);
974        } else {
975            zend_error_noreturn(error_level, "Cannot redeclare %s()", ZSTR_VAL(function->common.function_name));
976        }
977        return FAILURE;
978    } else {
979        if (function->op_array.refcount) {
980            (*function->op_array.refcount)++;
981        }
982        function->op_array.static_variables = NULL; /* NULL out the unbound function */
983        return SUCCESS;
984    }
985}
986/* }}} */
987
988ZEND_API zend_class_entry *do_bind_class(const zend_op_array* op_array, const zend_op *opline, HashTable *class_table, zend_bool compile_time) /* {{{ */
989{
990    zend_class_entry *ce;
991    zval *op1, *op2;
992
993    if (compile_time) {
994        op1 = CT_CONSTANT_EX(op_array, opline->op1.constant);
995        op2 = CT_CONSTANT_EX(op_array, opline->op2.constant);
996    } else {
997        op1 = RT_CONSTANT(op_array, opline->op1);
998        op2 = RT_CONSTANT(op_array, opline->op2);
999    }
1000    if ((ce = zend_hash_find_ptr(class_table, Z_STR_P(op1))) == NULL) {
1001        zend_error_noreturn(E_COMPILE_ERROR, "Internal Zend error - Missing class information for %s", Z_STRVAL_P(op1));
1002        return NULL;
1003    }
1004    ce->refcount++;
1005    if (zend_hash_add_ptr(class_table, Z_STR_P(op2), ce) == NULL) {
1006        ce->refcount--;
1007        if (!compile_time) {
1008            /* If we're in compile time, in practice, it's quite possible
1009             * that we'll never reach this class declaration at runtime,
1010             * so we shut up about it.  This allows the if (!defined('FOO')) { return; }
1011             * approach to work.
1012             */
1013            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));
1014        }
1015        return NULL;
1016    } else {
1017        if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_IMPLEMENT_INTERFACES|ZEND_ACC_IMPLEMENT_TRAITS))) {
1018            zend_verify_abstract_class(ce);
1019        }
1020        return ce;
1021    }
1022}
1023/* }}} */
1024
1025ZEND_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) /* {{{ */
1026{
1027    zend_class_entry *ce;
1028    zval *op1, *op2;
1029
1030    if (compile_time) {
1031        op1 = CT_CONSTANT_EX(op_array, opline->op1.constant);
1032        op2 = CT_CONSTANT_EX(op_array, opline->op2.constant);
1033    } else {
1034        op1 = RT_CONSTANT(op_array, opline->op1);
1035        op2 = RT_CONSTANT(op_array, opline->op2);
1036    }
1037
1038    ce = zend_hash_find_ptr(class_table, Z_STR_P(op1));
1039
1040    if (!ce) {
1041        if (!compile_time) {
1042            /* If we're in compile time, in practice, it's quite possible
1043             * that we'll never reach this class declaration at runtime,
1044             * so we shut up about it.  This allows the if (!defined('FOO')) { return; }
1045             * approach to work.
1046             */
1047            zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(Z_OBJCE_P(op2)), Z_STRVAL_P(op2));
1048        }
1049        return NULL;
1050    }
1051
1052    if (zend_hash_exists(class_table, Z_STR_P(op2))) {
1053        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));
1054    }
1055
1056    zend_do_inheritance(ce, parent_ce);
1057
1058    ce->refcount++;
1059
1060    /* Register the derived class */
1061    if (zend_hash_add_ptr(class_table, Z_STR_P(op2), ce) == NULL) {
1062        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));
1063    }
1064    return ce;
1065}
1066/* }}} */
1067
1068void zend_do_early_binding(void) /* {{{ */
1069{
1070    zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1];
1071    HashTable *table;
1072
1073    while (opline->opcode == ZEND_TICKS && opline > CG(active_op_array)->opcodes) {
1074        opline--;
1075    }
1076
1077    switch (opline->opcode) {
1078        case ZEND_DECLARE_FUNCTION:
1079            if (do_bind_function(CG(active_op_array), opline, CG(function_table), 1) == FAILURE) {
1080                return;
1081            }
1082            table = CG(function_table);
1083            break;
1084        case ZEND_DECLARE_CLASS:
1085            if (do_bind_class(CG(active_op_array), opline, CG(class_table), 1) == NULL) {
1086                return;
1087            }
1088            table = CG(class_table);
1089            break;
1090        case ZEND_DECLARE_INHERITED_CLASS:
1091            {
1092                zend_op *fetch_class_opline = opline-1;
1093                zval *parent_name;
1094                zend_class_entry *ce;
1095
1096                parent_name = CT_CONSTANT(fetch_class_opline->op2);
1097                if (((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) == NULL) ||
1098                    ((CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES) &&
1099                     (ce->type == ZEND_INTERNAL_CLASS))) {
1100                    if (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) {
1101                        uint32_t *opline_num = &CG(active_op_array)->early_binding;
1102
1103                        while (*opline_num != (uint32_t)-1) {
1104                            opline_num = &CG(active_op_array)->opcodes[*opline_num].result.opline_num;
1105                        }
1106                        *opline_num = opline - CG(active_op_array)->opcodes;
1107                        opline->opcode = ZEND_DECLARE_INHERITED_CLASS_DELAYED;
1108                        opline->result_type = IS_UNUSED;
1109                        opline->result.opline_num = -1;
1110                    }
1111                    return;
1112                }
1113                if (do_bind_inherited_class(CG(active_op_array), opline, CG(class_table), ce, 1) == NULL) {
1114                    return;
1115                }
1116                /* clear unnecessary ZEND_FETCH_CLASS opcode */
1117                zend_del_literal(CG(active_op_array), fetch_class_opline->op2.constant);
1118                MAKE_NOP(fetch_class_opline);
1119
1120                table = CG(class_table);
1121                break;
1122            }
1123        case ZEND_VERIFY_ABSTRACT_CLASS:
1124        case ZEND_ADD_INTERFACE:
1125        case ZEND_ADD_TRAIT:
1126        case ZEND_BIND_TRAITS:
1127            /* We currently don't early-bind classes that implement interfaces */
1128            /* Classes with traits are handled exactly the same, no early-bind here */
1129            return;
1130        default:
1131            zend_error_noreturn(E_COMPILE_ERROR, "Invalid binding type");
1132            return;
1133    }
1134
1135    zend_hash_del(table, Z_STR_P(CT_CONSTANT(opline->op1)));
1136    zend_del_literal(CG(active_op_array), opline->op1.constant);
1137    zend_del_literal(CG(active_op_array), opline->op2.constant);
1138    MAKE_NOP(opline);
1139}
1140/* }}} */
1141
1142ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array) /* {{{ */
1143{
1144    if (op_array->early_binding != (uint32_t)-1) {
1145        zend_bool orig_in_compilation = CG(in_compilation);
1146        uint32_t opline_num = op_array->early_binding;
1147        zend_class_entry *ce;
1148
1149        CG(in_compilation) = 1;
1150        while (opline_num != (uint32_t)-1) {
1151            zval *parent_name = RT_CONSTANT(op_array, op_array->opcodes[opline_num-1].op2);
1152            if ((ce = zend_lookup_class_ex(Z_STR_P(parent_name), parent_name + 1, 0)) != NULL) {
1153                do_bind_inherited_class(op_array, &op_array->opcodes[opline_num], EG(class_table), ce, 0);
1154            }
1155            opline_num = op_array->opcodes[opline_num].result.opline_num;
1156        }
1157        CG(in_compilation) = orig_in_compilation;
1158    }
1159}
1160/* }}} */
1161
1162ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal) /* {{{ */
1163{
1164    size_t prop_name_length = 1 + src1_length + 1 + src2_length;
1165    zend_string *prop_name = zend_string_alloc(prop_name_length, internal);
1166
1167    ZSTR_VAL(prop_name)[0] = '\0';
1168    memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1);
1169    memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1);
1170    return prop_name;
1171}
1172/* }}} */
1173
1174static zend_always_inline size_t zend_strnlen(const char* s, size_t maxlen) /* {{{ */
1175{
1176    size_t len = 0;
1177    while (*s++ && maxlen--) len++;
1178    return len;
1179}
1180/* }}} */
1181
1182ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */
1183{
1184    size_t class_name_len;
1185
1186    *class_name = NULL;
1187
1188    if (ZSTR_VAL(name)[0] != '\0') {
1189        *prop_name = ZSTR_VAL(name);
1190        if (prop_len) {
1191            *prop_len = ZSTR_LEN(name);
1192        }
1193        return SUCCESS;
1194    }
1195    if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') {
1196        zend_error(E_NOTICE, "Illegal member variable name");
1197        *prop_name = ZSTR_VAL(name);
1198        if (prop_len) {
1199            *prop_len = ZSTR_LEN(name);
1200        }
1201        return FAILURE;
1202    }
1203
1204    class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2);
1205    if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') {
1206        zend_error(E_NOTICE, "Corrupt member variable name");
1207        *prop_name = ZSTR_VAL(name);
1208        if (prop_len) {
1209            *prop_len = ZSTR_LEN(name);
1210        }
1211        return FAILURE;
1212    }
1213
1214    *class_name = ZSTR_VAL(name) + 1;
1215    *prop_name = ZSTR_VAL(name) + class_name_len + 2;
1216    if (prop_len) {
1217        *prop_len = ZSTR_LEN(name) - class_name_len - 2;
1218    }
1219    return SUCCESS;
1220}
1221/* }}} */
1222
1223static zend_constant *zend_lookup_reserved_const(const char *name, size_t len) /* {{{ */
1224{
1225    zend_constant *c = zend_hash_find_ptr_lc(EG(zend_constants), name, len);
1226    if (c && !(c->flags & CONST_CS) && (c->flags & CONST_CT_SUBST)) {
1227        return c;
1228    }
1229    return NULL;
1230}
1231/* }}} */
1232
1233static zend_bool zend_try_ct_eval_const(zval *zv, zend_string *name, zend_bool is_fully_qualified) /* {{{ */
1234{
1235    zend_constant *c;
1236
1237    /* Substitute case-sensitive (or lowercase) constants */
1238    c = zend_hash_find_ptr(EG(zend_constants), name);
1239    if (c && (
1240          ((c->flags & CONST_PERSISTENT) && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION))
1241       || (Z_TYPE(c->value) < IS_OBJECT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION))
1242    )) {
1243        ZVAL_DUP(zv, &c->value);
1244        return 1;
1245    }
1246
1247    {
1248        /* Substitute true, false and null (including unqualified usage in namespaces) */
1249        const char *lookup_name = ZSTR_VAL(name);
1250        size_t lookup_len = ZSTR_LEN(name);
1251
1252        if (!is_fully_qualified) {
1253            zend_get_unqualified_name(name, &lookup_name, &lookup_len);
1254        }
1255
1256        c = zend_lookup_reserved_const(lookup_name, lookup_len);
1257        if (c) {
1258            ZVAL_DUP(zv, &c->value);
1259            return 1;
1260        }
1261    }
1262
1263    return 0;
1264}
1265/* }}} */
1266
1267static inline zend_bool zend_is_scope_known() /* {{{ */
1268{
1269    if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) {
1270        /* Closures can be rebound to a different scope */
1271        return 0;
1272    }
1273
1274    if (!CG(active_class_entry)) {
1275        /* The scope is known if we're in a free function (no scope), but not if we're in
1276         * a file/eval (which inherits including/eval'ing scope). */
1277        return CG(active_op_array)->function_name != NULL;
1278    }
1279
1280    /* For traits self etc refers to the using class, not the trait itself */
1281    return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0;
1282}
1283/* }}} */
1284
1285static inline zend_bool class_name_refers_to_active_ce(zend_string *class_name, uint32_t fetch_type) /* {{{ */
1286{
1287    if (!CG(active_class_entry)) {
1288        return 0;
1289    }
1290    if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) {
1291        return 1;
1292    }
1293    return fetch_type == ZEND_FETCH_CLASS_DEFAULT
1294        && zend_string_equals_ci(class_name, CG(active_class_entry)->name);
1295}
1296/* }}} */
1297
1298uint32_t zend_get_class_fetch_type(zend_string *name) /* {{{ */
1299{
1300    if (zend_string_equals_literal_ci(name, "self")) {
1301        return ZEND_FETCH_CLASS_SELF;
1302    } else if (zend_string_equals_literal_ci(name, "parent")) {
1303        return ZEND_FETCH_CLASS_PARENT;
1304    } else if (zend_string_equals_literal_ci(name, "static")) {
1305        return ZEND_FETCH_CLASS_STATIC;
1306    } else {
1307        return ZEND_FETCH_CLASS_DEFAULT;
1308    }
1309}
1310/* }}} */
1311
1312static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */
1313{
1314    /* Fully qualified names are always default refs */
1315    if (name_ast->attr == ZEND_NAME_FQ) {
1316        return ZEND_FETCH_CLASS_DEFAULT;
1317    }
1318
1319    return zend_get_class_fetch_type(zend_ast_get_str(name_ast));
1320}
1321/* }}} */
1322
1323static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */
1324{
1325    if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && !CG(active_class_entry) && zend_is_scope_known()) {
1326        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active",
1327            fetch_type == ZEND_FETCH_CLASS_SELF ? "self" :
1328            fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static");
1329    }
1330}
1331/* }}} */
1332
1333static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast, zend_ast *name_ast, zend_bool constant) /* {{{ */
1334{
1335    uint32_t fetch_type;
1336
1337    if (name_ast->kind != ZEND_AST_ZVAL) {
1338        return 0;
1339    }
1340
1341    if (!zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "class")) {
1342        return 0;
1343    }
1344
1345    if (class_ast->kind != ZEND_AST_ZVAL) {
1346        zend_error_noreturn(E_COMPILE_ERROR,
1347            "Dynamic class names are not allowed in compile-time ::class fetch");
1348    }
1349
1350    fetch_type = zend_get_class_fetch_type(zend_ast_get_str(class_ast));
1351    zend_ensure_valid_class_fetch_type(fetch_type);
1352
1353    switch (fetch_type) {
1354        case ZEND_FETCH_CLASS_SELF:
1355            if (constant || (CG(active_class_entry) && zend_is_scope_known())) {
1356                ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
1357            } else {
1358                ZVAL_NULL(zv);
1359            }
1360            return 1;
1361        case ZEND_FETCH_CLASS_STATIC:
1362        case ZEND_FETCH_CLASS_PARENT:
1363            if (constant) {
1364                zend_error_noreturn(E_COMPILE_ERROR,
1365                    "%s::class cannot be used for compile-time class name resolution",
1366                    fetch_type == ZEND_FETCH_CLASS_STATIC ? "static" : "parent"
1367                );
1368            } else {
1369                ZVAL_NULL(zv);
1370            }
1371            return 1;
1372        case ZEND_FETCH_CLASS_DEFAULT:
1373            ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast));
1374            return 1;
1375        EMPTY_SWITCH_DEFAULT_CASE()
1376    }
1377}
1378/* }}} */
1379
1380static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
1381{
1382    uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1383    zval *c;
1384
1385    if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1386        c = zend_hash_find(&CG(active_class_entry)->constants_table, name);
1387    } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
1388        zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
1389        if (ce) {
1390            c = zend_hash_find(&ce->constants_table, name);
1391        } else {
1392            return 0;
1393        }
1394    } else {
1395        return 0;
1396    }
1397
1398    if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) {
1399        return 0;
1400    }
1401
1402    /* Substitute case-sensitive (or lowercase) persistent class constants */
1403    if (c && Z_TYPE_P(c) < IS_OBJECT) {
1404        ZVAL_DUP(zv, c);
1405        return 1;
1406    }
1407
1408    return 0;
1409}
1410/* }}} */
1411
1412static void zend_add_to_list(void *result, void *item) /* {{{ */
1413{
1414    void** list = *(void**)result;
1415    size_t n = 0;
1416
1417    if (list) {
1418        while (list[n]) {
1419            n++;
1420        }
1421    }
1422
1423    list = erealloc(list, sizeof(void*) * (n+2));
1424
1425    list[n]   = item;
1426    list[n+1] = NULL;
1427
1428    *(void**)result = list;
1429}
1430/* }}} */
1431
1432void zend_do_extended_info(void) /* {{{ */
1433{
1434    zend_op *opline;
1435
1436    if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1437        return;
1438    }
1439
1440    opline = get_next_op(CG(active_op_array));
1441
1442    opline->opcode = ZEND_EXT_STMT;
1443    SET_UNUSED(opline->op1);
1444    SET_UNUSED(opline->op2);
1445}
1446/* }}} */
1447
1448void zend_do_extended_fcall_begin(void) /* {{{ */
1449{
1450    zend_op *opline;
1451
1452    if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1453        return;
1454    }
1455
1456    opline = get_next_op(CG(active_op_array));
1457
1458    opline->opcode = ZEND_EXT_FCALL_BEGIN;
1459    SET_UNUSED(opline->op1);
1460    SET_UNUSED(opline->op2);
1461}
1462/* }}} */
1463
1464void zend_do_extended_fcall_end(void) /* {{{ */
1465{
1466    zend_op *opline;
1467
1468    if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) {
1469        return;
1470    }
1471
1472    opline = get_next_op(CG(active_op_array));
1473
1474    opline->opcode = ZEND_EXT_FCALL_END;
1475    SET_UNUSED(opline->op1);
1476    SET_UNUSED(opline->op2);
1477}
1478/* }}} */
1479
1480zend_bool zend_is_auto_global_str(char *name, size_t len) /* {{{ */ {
1481    zend_auto_global *auto_global;
1482
1483    if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) {
1484        if (auto_global->armed) {
1485            auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1486        }
1487        return 1;
1488    }
1489    return 0;
1490}
1491/* }}} */
1492
1493zend_bool zend_is_auto_global(zend_string *name) /* {{{ */
1494{
1495    zend_auto_global *auto_global;
1496
1497    if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) {
1498        if (auto_global->armed) {
1499            auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1500        }
1501        return 1;
1502    }
1503    return 0;
1504}
1505/* }}} */
1506
1507int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */
1508{
1509    zend_auto_global auto_global;
1510    int retval;
1511
1512    auto_global.name = zend_new_interned_string(name);
1513    auto_global.auto_global_callback = auto_global_callback;
1514    auto_global.jit = jit;
1515
1516    retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE;
1517
1518    zend_string_release(name);
1519    return retval;
1520}
1521/* }}} */
1522
1523ZEND_API void zend_activate_auto_globals(void) /* {{{ */
1524{
1525    zend_auto_global *auto_global;
1526
1527    ZEND_HASH_FOREACH_PTR(CG(auto_globals), auto_global) {
1528        if (auto_global->jit) {
1529            auto_global->armed = 1;
1530        } else if (auto_global->auto_global_callback) {
1531            auto_global->armed = auto_global->auto_global_callback(auto_global->name);
1532        } else {
1533            auto_global->armed = 0;
1534        }
1535    } ZEND_HASH_FOREACH_END();
1536}
1537/* }}} */
1538
1539int zendlex(zend_parser_stack_elem *elem) /* {{{ */
1540{
1541    zval zv;
1542    int retval;
1543
1544    if (CG(increment_lineno)) {
1545        CG(zend_lineno)++;
1546        CG(increment_lineno) = 0;
1547    }
1548
1549again:
1550    ZVAL_UNDEF(&zv);
1551    retval = lex_scan(&zv);
1552    switch (retval) {
1553        case T_COMMENT:
1554        case T_DOC_COMMENT:
1555        case T_OPEN_TAG:
1556        case T_WHITESPACE:
1557            goto again;
1558
1559        case T_CLOSE_TAG:
1560            if (LANG_SCNG(yy_text)[LANG_SCNG(yy_leng)-1] != '>') {
1561                CG(increment_lineno) = 1;
1562            }
1563            retval = ';'; /* implicit ; */
1564            break;
1565        case T_OPEN_TAG_WITH_ECHO:
1566            retval = T_ECHO;
1567            break;
1568    }
1569    if (Z_TYPE(zv) != IS_UNDEF) {
1570        elem->ast = zend_ast_create_zval(&zv);
1571    }
1572
1573    return retval;
1574}
1575/* }}} */
1576
1577ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
1578{
1579    zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
1580    dtor_func_t zval_ptr_dtor_func = ((persistent_hashes) ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR);
1581
1582    ce->refcount = 1;
1583    ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
1584
1585    ce->default_properties_table = NULL;
1586    ce->default_static_members_table = NULL;
1587    zend_hash_init_ex(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes, 0);
1588    zend_hash_init_ex(&ce->constants_table, 8, NULL, zval_ptr_dtor_func, persistent_hashes, 0);
1589    zend_hash_init_ex(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
1590
1591    if (ce->type == ZEND_INTERNAL_CLASS) {
1592#ifdef ZTS
1593        int n = zend_hash_num_elements(CG(class_table));
1594
1595        if (CG(static_members_table) && n >= CG(last_static_member)) {
1596            /* Support for run-time declaration: dl() */
1597            CG(last_static_member) = n+1;
1598            CG(static_members_table) = realloc(CG(static_members_table), (n+1)*sizeof(zval*));
1599            CG(static_members_table)[n] = NULL;
1600        }
1601        ce->static_members_table = (zval*)(zend_intptr_t)n;
1602#else
1603        ce->static_members_table = NULL;
1604#endif
1605    } else {
1606        ce->static_members_table = ce->default_static_members_table;
1607        ce->info.user.doc_comment = NULL;
1608    }
1609
1610    ce->default_properties_count = 0;
1611    ce->default_static_members_count = 0;
1612
1613    if (nullify_handlers) {
1614        ce->constructor = NULL;
1615        ce->destructor = NULL;
1616        ce->clone = NULL;
1617        ce->__get = NULL;
1618        ce->__set = NULL;
1619        ce->__unset = NULL;
1620        ce->__isset = NULL;
1621        ce->__call = NULL;
1622        ce->__callstatic = NULL;
1623        ce->__tostring = NULL;
1624        ce->create_object = NULL;
1625        ce->get_iterator = NULL;
1626        ce->iterator_funcs.funcs = NULL;
1627        ce->interface_gets_implemented = NULL;
1628        ce->get_static_method = NULL;
1629        ce->parent = NULL;
1630        ce->num_interfaces = 0;
1631        ce->interfaces = NULL;
1632        ce->num_traits = 0;
1633        ce->traits = NULL;
1634        ce->trait_aliases = NULL;
1635        ce->trait_precedences = NULL;
1636        ce->serialize = NULL;
1637        ce->unserialize = NULL;
1638        ce->serialize_func = NULL;
1639        ce->unserialize_func = NULL;
1640        ce->__debugInfo = NULL;
1641        if (ce->type == ZEND_INTERNAL_CLASS) {
1642            ce->info.internal.module = NULL;
1643            ce->info.internal.builtin_functions = NULL;
1644        }
1645    }
1646}
1647/* }}} */
1648
1649ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */
1650{
1651    return op_array->vars[EX_VAR_TO_NUM(var)];
1652}
1653/* }}} */
1654
1655zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */
1656{
1657    zval *left_zv = zend_ast_get_zval(left_ast);
1658    zend_string *left = Z_STR_P(left_zv);
1659    zend_string *right = zend_ast_get_str(right_ast);
1660
1661    zend_string *result;
1662    size_t left_len = ZSTR_LEN(left);
1663    size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */
1664
1665    result = zend_string_extend(left, len, 0);
1666    ZSTR_VAL(result)[left_len] = '\\';
1667    memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right));
1668    ZSTR_VAL(result)[len] = '\0';
1669    zend_string_release(right);
1670
1671    ZVAL_STR(left_zv, result);
1672    return left_ast;
1673}
1674/* }}} */
1675
1676/* A hacky way that is used to store the doc comment for properties */
1677zend_ast *zend_ast_append_doc_comment(zend_ast *list) /* {{{ */
1678{
1679    if (CG(doc_comment)) {
1680        list = zend_ast_list_add(list, zend_ast_create_zval_from_str(CG(doc_comment)));
1681        CG(doc_comment) = NULL;
1682    }
1683
1684    return list;
1685}
1686/* }}} */
1687
1688void zend_verify_namespace(void) /* {{{ */
1689{
1690    if (FC(has_bracketed_namespaces) && !FC(in_namespace)) {
1691        zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}");
1692    }
1693}
1694/* }}} */
1695
1696/* {{{ zend_dirname
1697   Returns directory name component of path */
1698ZEND_API size_t zend_dirname(char *path, size_t len)
1699{
1700    register char *end = path + len - 1;
1701    unsigned int len_adjust = 0;
1702
1703#ifdef ZEND_WIN32
1704    /* Note that on Win32 CWD is per drive (heritage from CP/M).
1705     * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive.
1706     */
1707    if ((2 <= len) && isalpha((int)((unsigned char *)path)[0]) && (':' == path[1])) {
1708        /* Skip over the drive spec (if any) so as not to change */
1709        path += 2;
1710        len_adjust += 2;
1711        if (2 == len) {
1712            /* Return "c:" on Win32 for dirname("c:").
1713             * It would be more consistent to return "c:."
1714             * but that would require making the string *longer*.
1715             */
1716            return len;
1717        }
1718    }
1719#elif defined(NETWARE)
1720    /*
1721     * Find the first occurrence of : from the left
1722     * move the path pointer to the position just after :
1723     * increment the len_adjust to the length of path till colon character(inclusive)
1724     * If there is no character beyond : simple return len
1725     */
1726    char *colonpos = NULL;
1727    colonpos = strchr(path, ':');
1728    if (colonpos != NULL) {
1729        len_adjust = ((colonpos - path) + 1);
1730        path += len_adjust;
1731        if (len_adjust == len) {
1732            return len;
1733        }
1734    }
1735#endif
1736
1737    if (len == 0) {
1738        /* Illegal use of this function */
1739        return 0;
1740    }
1741
1742    /* Strip trailing slashes */
1743    while (end >= path && IS_SLASH_P(end)) {
1744        end--;
1745    }
1746    if (end < path) {
1747        /* The path only contained slashes */
1748        path[0] = DEFAULT_SLASH;
1749        path[1] = '\0';
1750        return 1 + len_adjust;
1751    }
1752
1753    /* Strip filename */
1754    while (end >= path && !IS_SLASH_P(end)) {
1755        end--;
1756    }
1757    if (end < path) {
1758        /* No slash found, therefore return '.' */
1759#ifdef NETWARE
1760        if (len_adjust == 0) {
1761            path[0] = '.';
1762            path[1] = '\0';
1763            return 1; /* only one character */
1764        } else {
1765            path[0] = '\0';
1766            return len_adjust;
1767        }
1768#else
1769        path[0] = '.';
1770        path[1] = '\0';
1771        return 1 + len_adjust;
1772#endif
1773    }
1774
1775    /* Strip slashes which came before the file name */
1776    while (end >= path && IS_SLASH_P(end)) {
1777        end--;
1778    }
1779    if (end < path) {
1780        path[0] = DEFAULT_SLASH;
1781        path[1] = '\0';
1782        return 1 + len_adjust;
1783    }
1784    *(end+1) = '\0';
1785
1786    return (size_t)(end + 1 - path) + len_adjust;
1787}
1788/* }}} */
1789
1790static void zend_adjust_for_fetch_type(zend_op *opline, uint32_t type) /* {{{ */
1791{
1792    switch (type & BP_VAR_MASK) {
1793        case BP_VAR_R:
1794            return;
1795        case BP_VAR_W:
1796        case BP_VAR_REF:
1797            opline->opcode += 3;
1798            return;
1799        case BP_VAR_RW:
1800            opline->opcode += 6;
1801            return;
1802        case BP_VAR_IS:
1803            opline->opcode += 9;
1804            return;
1805        case BP_VAR_FUNC_ARG:
1806            opline->opcode += 12;
1807            opline->extended_value |= type >> BP_VAR_SHIFT;
1808            return;
1809        case BP_VAR_UNSET:
1810            opline->opcode += 15;
1811            return;
1812        EMPTY_SWITCH_DEFAULT_CASE()
1813    }
1814}
1815/* }}} */
1816
1817static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */
1818{
1819    opline->result_type = IS_VAR;
1820    opline->result.var = get_temporary_variable(CG(active_op_array));
1821    GET_NODE(result, opline->result);
1822}
1823/* }}} */
1824
1825static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */
1826{
1827    opline->result_type = IS_TMP_VAR;
1828    opline->result.var = get_temporary_variable(CG(active_op_array));
1829    GET_NODE(result, opline->result);
1830}
1831/* }}} */
1832
1833static zend_op *zend_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
1834{
1835    zend_op *opline = get_next_op(CG(active_op_array));
1836    opline->opcode = opcode;
1837
1838    if (op1 == NULL) {
1839        SET_UNUSED(opline->op1);
1840    } else {
1841        SET_NODE(opline->op1, op1);
1842    }
1843
1844    if (op2 == NULL) {
1845        SET_UNUSED(opline->op2);
1846    } else {
1847        SET_NODE(opline->op2, op2);
1848    }
1849
1850    if (result) {
1851        zend_make_var_result(result, opline);
1852    }
1853    return opline;
1854}
1855/* }}} */
1856
1857static zend_op *zend_emit_op_tmp(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
1858{
1859    zend_op *opline = get_next_op(CG(active_op_array));
1860    opline->opcode = opcode;
1861
1862    if (op1 == NULL) {
1863        SET_UNUSED(opline->op1);
1864    } else {
1865        SET_NODE(opline->op1, op1);
1866    }
1867
1868    if (op2 == NULL) {
1869        SET_UNUSED(opline->op2);
1870    } else {
1871        SET_NODE(opline->op2, op2);
1872    }
1873
1874    if (result) {
1875        zend_make_tmp_result(result, opline);
1876    }
1877
1878    return opline;
1879}
1880/* }}} */
1881
1882static void zend_emit_tick(void) /* {{{ */
1883{
1884    /* This prevents a double TICK generated by the parser statement of "declare()" */
1885    if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) {
1886        return;
1887    }
1888
1889    zend_op *opline = get_next_op(CG(active_op_array));
1890
1891    opline->opcode = ZEND_TICKS;
1892    SET_UNUSED(opline->op1);
1893    SET_UNUSED(opline->op2);
1894    opline->extended_value = FC(declarables).ticks;
1895}
1896/* }}} */
1897
1898static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */
1899{
1900    return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL);
1901}
1902/* }}} */
1903
1904static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */
1905{
1906    uint32_t opnum = get_next_op_number(CG(active_op_array));
1907    zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
1908    opline->op1.opline_num = opnum_target;
1909    return opnum;
1910}
1911/* }}} */
1912
1913static inline uint32_t zend_emit_cond_jump(zend_uchar opcode, znode *cond, uint32_t opnum_target) /* {{{ */
1914{
1915    uint32_t opnum = get_next_op_number(CG(active_op_array));
1916    zend_op *opline = zend_emit_op(NULL, opcode, cond, NULL);
1917    opline->op2.opline_num = opnum_target;
1918    return opnum;
1919}
1920/* }}} */
1921
1922static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */
1923{
1924    zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump];
1925    switch (opline->opcode) {
1926        case ZEND_JMP:
1927            opline->op1.opline_num = opnum_target;
1928            break;
1929        case ZEND_JMPZ:
1930        case ZEND_JMPNZ:
1931        case ZEND_JMPZ_EX:
1932        case ZEND_JMPNZ_EX:
1933        case ZEND_JMP_SET:
1934            opline->op2.opline_num = opnum_target;
1935            break;
1936        EMPTY_SWITCH_DEFAULT_CASE()
1937    }
1938}
1939/* }}} */
1940
1941static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */
1942{
1943    zend_update_jump_target(opnum_jump, get_next_op_number(CG(active_op_array)));
1944}
1945/* }}} */
1946
1947static inline zend_op *zend_delayed_emit_op(znode *result, zend_uchar opcode, znode *op1, znode *op2) /* {{{ */
1948{
1949    zend_op tmp_opline;
1950    init_op(&tmp_opline);
1951    tmp_opline.opcode = opcode;
1952    if (op1 == NULL) {
1953        SET_UNUSED(tmp_opline.op1);
1954    } else {
1955        SET_NODE(tmp_opline.op1, op1);
1956    }
1957    if (op2 == NULL) {
1958        SET_UNUSED(tmp_opline.op2);
1959    } else {
1960        SET_NODE(tmp_opline.op2, op2);
1961    }
1962    if (result) {
1963        zend_make_var_result(result, &tmp_opline);
1964    }
1965
1966    zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline);
1967    return zend_stack_top(&CG(delayed_oplines_stack));
1968}
1969/* }}} */
1970
1971static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */
1972{
1973    return zend_stack_count(&CG(delayed_oplines_stack));
1974}
1975/* }}} */
1976
1977static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */
1978{
1979    zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack));
1980    uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack));
1981
1982    ZEND_ASSERT(count > offset);
1983    for (i = offset; i < count; ++i) {
1984        opline = get_next_op(CG(active_op_array));
1985        memcpy(opline, &oplines[i], sizeof(zend_op));
1986    }
1987    CG(delayed_oplines_stack).top = offset;
1988    return opline;
1989}
1990/* }}} */
1991
1992static void zend_emit_return_type_check(znode *expr, zend_arg_info *return_info) /* {{{ */
1993{
1994    if (return_info->type_hint != IS_UNDEF) {
1995        zend_op *opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL);
1996        if (expr && expr->op_type == IS_CONST) {
1997            opline->result_type = expr->op_type = IS_TMP_VAR;
1998            opline->result.var = expr->u.op.var = get_temporary_variable(CG(active_op_array));
1999        }
2000        if (return_info->class_name) {
2001            opline->op2.num = CG(active_op_array)->cache_size;
2002            CG(active_op_array)->cache_size += sizeof(void*);
2003        } else {
2004            opline->op2.num = -1;
2005        }
2006    }
2007}
2008/* }}} */
2009
2010void zend_emit_final_return(zval *zv) /* {{{ */
2011{
2012    znode zn;
2013    zend_bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
2014
2015    if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
2016        zend_emit_return_type_check(NULL, CG(active_op_array)->arg_info - 1);
2017    }
2018
2019    zn.op_type = IS_CONST;
2020    if (zv) {
2021        ZVAL_COPY_VALUE(&zn.u.constant, zv);
2022    } else {
2023        ZVAL_NULL(&zn.u.constant);
2024    }
2025
2026    zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL);
2027}
2028/* }}} */
2029
2030static inline zend_bool zend_is_variable(zend_ast *ast) /* {{{ */
2031{
2032    return ast->kind == ZEND_AST_VAR || ast->kind == ZEND_AST_DIM
2033        || ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_STATIC_PROP
2034        || ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_METHOD_CALL
2035        || ast->kind == ZEND_AST_STATIC_CALL;
2036}
2037/* }}} */
2038
2039static inline zend_bool zend_is_call(zend_ast *ast) /* {{{ */
2040{
2041    return ast->kind == ZEND_AST_CALL
2042        || ast->kind == ZEND_AST_METHOD_CALL
2043        || ast->kind == ZEND_AST_STATIC_CALL;
2044}
2045/* }}} */
2046
2047static inline zend_bool zend_is_unticked_stmt(zend_ast *ast) /* {{{ */
2048{
2049    return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL;
2050}
2051/* }}} */
2052
2053static inline zend_bool zend_can_write_to_variable(zend_ast *ast) /* {{{ */
2054{
2055    while (ast->kind == ZEND_AST_DIM || ast->kind == ZEND_AST_PROP) {
2056        ast = ast->child[0];
2057    }
2058
2059    return zend_is_variable(ast);
2060}
2061/* }}} */
2062
2063static inline zend_bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */
2064{
2065    if (name_ast->kind != ZEND_AST_ZVAL) {
2066        return 0;
2067    }
2068
2069    return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast);
2070}
2071/* }}} */
2072
2073static inline void zend_handle_numeric_op(znode *node) /* {{{ */
2074{
2075    if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) {
2076        zend_ulong index;
2077
2078        if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) {
2079            zval_ptr_dtor(&node->u.constant);
2080            ZVAL_LONG(&node->u.constant, index);
2081        }
2082    }
2083}
2084/* }}} */
2085
2086static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */
2087{
2088    if (class_node->op_type == IS_CONST) {
2089        opline->op1_type = IS_CONST;
2090        opline->op1.constant = zend_add_class_name_literal(
2091            CG(active_op_array), Z_STR(class_node->u.constant));
2092    } else {
2093        SET_NODE(opline->op1, class_node);
2094    }
2095}
2096/* }}} */
2097
2098static zend_op *zend_compile_class_ref(znode *result, zend_ast *name_ast, int throw_exception) /* {{{ */
2099{
2100    zend_op *opline;
2101    znode name_node;
2102    zend_compile_expr(&name_node, name_ast);
2103
2104    if (name_node.op_type == IS_CONST) {
2105        zend_string *name = Z_STR(name_node.u.constant);
2106        uint32_t fetch_type = zend_get_class_fetch_type(name);
2107
2108        opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, NULL);
2109        opline->extended_value = fetch_type | (throw_exception ? ZEND_FETCH_CLASS_EXCEPTION : 0);
2110
2111        if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
2112            uint32_t type = name_ast->kind == ZEND_AST_ZVAL ? name_ast->attr : ZEND_NAME_FQ;
2113            opline->op2_type = IS_CONST;
2114            opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
2115                zend_resolve_class_name(name, type));
2116        } else {
2117            zend_ensure_valid_class_fetch_type(fetch_type);
2118        }
2119
2120        zend_string_release(name);
2121    } else {
2122        opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node);
2123        opline->extended_value = ZEND_FETCH_CLASS_DEFAULT | (throw_exception ? ZEND_FETCH_CLASS_EXCEPTION : 0);
2124    }
2125
2126    return opline;
2127}
2128/* }}} */
2129
2130static int zend_try_compile_cv(znode *result, zend_ast *ast) /* {{{ */
2131{
2132    zend_ast *name_ast = ast->child[0];
2133    if (name_ast->kind == ZEND_AST_ZVAL) {
2134        zend_string *name = zval_get_string(zend_ast_get_zval(name_ast));
2135
2136        if (zend_is_auto_global(name)) {
2137            zend_string_release(name);
2138            return FAILURE;
2139        }
2140
2141        result->op_type = IS_CV;
2142        result->u.op.var = lookup_cv(CG(active_op_array), name);
2143
2144        /* lookup_cv may be using another zend_string instance  */
2145        name = CG(active_op_array)->vars[EX_VAR_TO_NUM(result->u.op.var)];
2146
2147        if (zend_string_equals_literal(name, "this")) {
2148            CG(active_op_array)->this_var = result->u.op.var;
2149        }
2150        return SUCCESS;
2151    }
2152
2153    return FAILURE;
2154}
2155/* }}} */
2156
2157static zend_op *zend_compile_simple_var_no_cv(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2158{
2159    zend_ast *name_ast = ast->child[0];
2160    znode name_node;
2161    zend_op *opline;
2162
2163    zend_compile_expr(&name_node, name_ast);
2164    if (name_node.op_type == IS_CONST) {
2165        convert_to_string(&name_node.u.constant);
2166    }
2167
2168    if (delayed) {
2169        opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2170    } else {
2171        opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL);
2172    }
2173
2174    if (name_node.op_type == IS_CONST &&
2175        zend_is_auto_global(Z_STR(name_node.u.constant))) {
2176
2177        opline->extended_value = ZEND_FETCH_GLOBAL;
2178    } else {
2179        opline->extended_value = ZEND_FETCH_LOCAL;
2180        /* there is a chance someone is accessing $this */
2181        if (ast->kind != ZEND_AST_ZVAL
2182            && CG(active_op_array)->scope && CG(active_op_array)->this_var == (uint32_t)-1
2183        ) {
2184            zend_string *key = zend_string_init("this", sizeof("this") - 1, 0);
2185            CG(active_op_array)->this_var = lookup_cv(CG(active_op_array), key);
2186        }
2187    }
2188
2189    return opline;
2190}
2191/* }}} */
2192
2193static void zend_compile_simple_var(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2194{
2195    if (zend_try_compile_cv(result, ast) == FAILURE) {
2196        zend_op *opline = zend_compile_simple_var_no_cv(result, ast, type, delayed);
2197        zend_adjust_for_fetch_type(opline, type);
2198    }
2199}
2200/* }}} */
2201
2202static void zend_separate_if_call_and_write(znode *node, zend_ast *ast, uint32_t type) /* {{{ */
2203{
2204    if (type != BP_VAR_R && type != BP_VAR_IS && zend_is_call(ast)) {
2205        if (node->op_type == IS_VAR) {
2206            zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL);
2207            opline->result_type = IS_VAR;
2208            opline->result.var = opline->op1.var;
2209        } else {
2210            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
2211        }
2212    }
2213}
2214/* }}} */
2215
2216void zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type);
2217void zend_compile_assign(znode *result, zend_ast *ast);
2218static void zend_compile_list_assign(znode *result, zend_ast *ast, znode *expr_node);
2219
2220static inline void zend_emit_assign_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
2221{
2222    znode dummy_node;
2223    if (var_ast->kind == ZEND_AST_LIST) {
2224        zend_compile_list_assign(&dummy_node, var_ast, value_node);
2225    } else {
2226        zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast,
2227            zend_ast_create_znode(value_node));
2228        zend_compile_assign(&dummy_node, assign_ast);
2229    }
2230    zend_do_free(&dummy_node);
2231}
2232/* }}} */
2233
2234static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2235{
2236    zend_ast *var_ast = ast->child[0];
2237    zend_ast *dim_ast = ast->child[1];
2238
2239    znode var_node, dim_node;
2240
2241    zend_delayed_compile_var(&var_node, var_ast, type);
2242    zend_separate_if_call_and_write(&var_node, var_ast, type);
2243
2244    if (dim_ast == NULL) {
2245        if (type == BP_VAR_R || type == BP_VAR_IS) {
2246            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
2247        }
2248        if (type == BP_VAR_UNSET) {
2249            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting");
2250        }
2251        dim_node.op_type = IS_UNUSED;
2252    } else {
2253        zend_compile_expr(&dim_node, dim_ast);
2254        zend_handle_numeric_op(&dim_node);
2255    }
2256
2257    return zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node);
2258}
2259/* }}} */
2260
2261static inline zend_op *zend_compile_dim_common(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2262{
2263    uint32_t offset = zend_delayed_compile_begin();
2264    zend_delayed_compile_dim(result, ast, type);
2265    return zend_delayed_compile_end(offset);
2266}
2267/* }}} */
2268
2269void zend_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2270{
2271    zend_op *opline = zend_compile_dim_common(result, ast, type);
2272    zend_adjust_for_fetch_type(opline, type);
2273}
2274/* }}} */
2275
2276static zend_bool is_this_fetch(zend_ast *ast) /* {{{ */
2277{
2278    if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) {
2279        zval *name = zend_ast_get_zval(ast->child[0]);
2280        return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "this");
2281    }
2282
2283    return 0;
2284}
2285/* }}} */
2286
2287static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2288{
2289    zend_ast *obj_ast = ast->child[0];
2290    zend_ast *prop_ast = ast->child[1];
2291
2292    znode obj_node, prop_node;
2293    zend_op *opline;
2294
2295    if (is_this_fetch(obj_ast)) {
2296        obj_node.op_type = IS_UNUSED;
2297    } else {
2298        zend_delayed_compile_var(&obj_node, obj_ast, type);
2299        zend_separate_if_call_and_write(&obj_node, obj_ast, type);
2300    }
2301    zend_compile_expr(&prop_node, prop_ast);
2302
2303    opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node);
2304    if (opline->op2_type == IS_CONST) {
2305        convert_to_string(CT_CONSTANT(opline->op2));
2306        zend_alloc_polymorphic_cache_slot(opline->op2.constant);
2307    }
2308
2309    return opline;
2310}
2311/* }}} */
2312
2313static zend_op *zend_compile_prop_common(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2314{
2315    uint32_t offset = zend_delayed_compile_begin();
2316    zend_delayed_compile_prop(result, ast, type);
2317    return zend_delayed_compile_end(offset);
2318}
2319/* }}} */
2320
2321void zend_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
2322{
2323    zend_op *opline = zend_compile_prop_common(result, ast, type);
2324    zend_adjust_for_fetch_type(opline, type);
2325}
2326/* }}} */
2327
2328zend_op *zend_compile_static_prop_common(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2329{
2330    zend_ast *class_ast = ast->child[0];
2331    zend_ast *prop_ast = ast->child[1];
2332
2333    znode class_node, prop_node;
2334    zend_op *opline;
2335
2336    if (zend_is_const_default_class_ref(class_ast)) {
2337        class_node.op_type = IS_CONST;
2338        ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(class_ast));
2339    } else {
2340        zend_compile_class_ref(&class_node, class_ast, 1);
2341    }
2342
2343    zend_compile_expr(&prop_node, prop_ast);
2344
2345    if (delayed) {
2346        opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &prop_node, NULL);
2347    } else {
2348        opline = zend_emit_op(result, ZEND_FETCH_R, &prop_node, NULL);
2349    }
2350    if (opline->op1_type == IS_CONST) {
2351        zend_alloc_polymorphic_cache_slot(opline->op1.constant);
2352    }
2353    if (class_node.op_type == IS_CONST) {
2354        opline->op2_type = IS_CONST;
2355        opline->op2.constant = zend_add_class_name_literal(
2356            CG(active_op_array), Z_STR(class_node.u.constant));
2357    } else {
2358        SET_NODE(opline->op2, &class_node);
2359    }
2360    opline->extended_value |= ZEND_FETCH_STATIC_MEMBER;
2361
2362    return opline;
2363}
2364/* }}} */
2365
2366void zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, int delayed) /* {{{ */
2367{
2368    zend_op *opline = zend_compile_static_prop_common(result, ast, type, delayed);
2369    zend_adjust_for_fetch_type(opline, type);
2370}
2371/* }}} */
2372
2373static void zend_compile_list_assign(znode *result, zend_ast *ast, znode *expr_node) /* {{{ */
2374{
2375    zend_ast_list *list = zend_ast_get_list(ast);
2376    uint32_t i;
2377    zend_bool has_elems = 0;
2378
2379    for (i = 0; i < list->children; ++i) {
2380        zend_ast *var_ast = list->child[i];
2381        znode fetch_result, dim_node;
2382
2383        if (var_ast == NULL) {
2384            continue;
2385        }
2386        has_elems = 1;
2387
2388        dim_node.op_type = IS_CONST;
2389        ZVAL_LONG(&dim_node.u.constant, i);
2390
2391        if (expr_node->op_type == IS_CONST) {
2392            Z_TRY_ADDREF(expr_node->u.constant);
2393        }
2394
2395        zend_emit_op(&fetch_result, ZEND_FETCH_LIST, expr_node, &dim_node);
2396        zend_emit_assign_znode(var_ast, &fetch_result);
2397    }
2398
2399    if (!has_elems) {
2400        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list");
2401    }
2402
2403    *result = *expr_node;
2404}
2405/* }}} */
2406
2407static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */
2408{
2409    if (ast->kind == ZEND_AST_CALL) {
2410        zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context");
2411    }
2412    if (ast->kind == ZEND_AST_METHOD_CALL || ast->kind == ZEND_AST_STATIC_CALL) {
2413        zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context");
2414    }
2415}
2416/* }}} */
2417
2418/* Detects $a... = $a pattern */
2419zend_bool zend_is_assign_to_self(zend_ast *var_ast, zend_ast *expr_ast) /* {{{ */
2420{
2421    if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) {
2422        return 0;
2423    }
2424
2425    while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) {
2426        var_ast = var_ast->child[0];
2427    }
2428
2429    if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) {
2430        return 0;
2431    }
2432
2433    {
2434        zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
2435        zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
2436        zend_bool result = zend_string_equals(name1, name2);
2437        zend_string_release(name1);
2438        zend_string_release(name2);
2439        return result;
2440    }
2441}
2442/* }}} */
2443
2444/* Detects if list($a, $b, $c) contains variable with given name */
2445zend_bool zend_list_has_assign_to(zend_ast *list_ast, zend_string *name) /* {{{ */
2446{
2447    zend_ast_list *list = zend_ast_get_list(list_ast);
2448    uint32_t i;
2449    for (i = 0; i < list->children; i++) {
2450        zend_ast *var_ast = list->child[i];
2451        if (!var_ast) {
2452            continue;
2453        }
2454
2455        /* Recursively check nested list()s */
2456        if (var_ast->kind == ZEND_AST_LIST && zend_list_has_assign_to(var_ast, name)) {
2457            return 1;
2458        }
2459
2460        if (var_ast->kind == ZEND_AST_VAR && var_ast->child[0]->kind == ZEND_AST_ZVAL) {
2461            zend_string *var_name = zval_get_string(zend_ast_get_zval(var_ast->child[0]));
2462            zend_bool result = zend_string_equals(var_name, name);
2463            zend_string_release(var_name);
2464            if (result) {
2465                return 1;
2466            }
2467        }
2468    }
2469
2470    return 0;
2471}
2472/* }}} */
2473
2474/* Detects patterns like list($a, $b, $c) = $a */
2475zend_bool zend_list_has_assign_to_self(zend_ast *list_ast, zend_ast *expr_ast) /* {{{ */
2476{
2477    /* Only check simple variables on the RHS, as only CVs cause issues with this. */
2478    if (expr_ast->kind == ZEND_AST_VAR && expr_ast->child[0]->kind == ZEND_AST_ZVAL) {
2479        zend_string *name = zval_get_string(zend_ast_get_zval(expr_ast->child[0]));
2480        zend_bool result = zend_list_has_assign_to(list_ast, name);
2481        zend_string_release(name);
2482        return result;
2483    }
2484    return 0;
2485}
2486/* }}} */
2487
2488void zend_compile_assign(znode *result, zend_ast *ast) /* {{{ */
2489{
2490    zend_ast *var_ast = ast->child[0];
2491    zend_ast *expr_ast = ast->child[1];
2492
2493    znode var_node, expr_node;
2494    zend_op *opline;
2495    uint32_t offset;
2496
2497    if (is_this_fetch(var_ast)) {
2498        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
2499    }
2500
2501    zend_ensure_writable_variable(var_ast);
2502
2503    switch (var_ast->kind) {
2504        case ZEND_AST_VAR:
2505        case ZEND_AST_STATIC_PROP:
2506            zend_compile_var(&var_node, var_ast, BP_VAR_W);
2507            zend_compile_expr(&expr_node, expr_ast);
2508            zend_emit_op(result, ZEND_ASSIGN, &var_node, &expr_node);
2509            return;
2510        case ZEND_AST_DIM:
2511            offset = zend_delayed_compile_begin();
2512            zend_delayed_compile_dim(result, var_ast, BP_VAR_W);
2513
2514            if (zend_is_assign_to_self(var_ast, expr_ast)) {
2515                /* $a[0] = $a should evaluate the right $a first */
2516                zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
2517            } else {
2518                zend_compile_expr(&expr_node, expr_ast);
2519            }
2520
2521            opline = zend_delayed_compile_end(offset);
2522            opline->opcode = ZEND_ASSIGN_DIM;
2523
2524            opline = zend_emit_op_data(&expr_node);
2525            return;
2526        case ZEND_AST_PROP:
2527            offset = zend_delayed_compile_begin();
2528            zend_delayed_compile_prop(result, var_ast, BP_VAR_W);
2529            zend_compile_expr(&expr_node, expr_ast);
2530
2531            opline = zend_delayed_compile_end(offset);
2532            opline->opcode = ZEND_ASSIGN_OBJ;
2533
2534            zend_emit_op_data(&expr_node);
2535            return;
2536        case ZEND_AST_LIST:
2537            if (zend_list_has_assign_to_self(var_ast, expr_ast)) {
2538                /* list($a, $b) = $a should evaluate the right $a first */
2539                zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, 0);
2540            } else {
2541                zend_compile_expr(&expr_node, expr_ast);
2542            }
2543
2544            zend_compile_list_assign(result, var_ast, &expr_node);
2545            return;
2546        EMPTY_SWITCH_DEFAULT_CASE();
2547    }
2548}
2549/* }}} */
2550
2551void zend_compile_assign_ref(znode *result, zend_ast *ast) /* {{{ */
2552{
2553    zend_ast *target_ast = ast->child[0];
2554    zend_ast *source_ast = ast->child[1];
2555
2556    znode target_node, source_node;
2557    zend_op *opline;
2558
2559    if (is_this_fetch(target_ast)) {
2560        zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
2561    }
2562    zend_ensure_writable_variable(target_ast);
2563
2564    zend_compile_var(&target_node, target_ast, BP_VAR_W);
2565    zend_compile_var(&source_node, source_ast, BP_VAR_REF);
2566
2567    if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) {
2568        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context");
2569    }
2570
2571    opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node);
2572    if (!result) {
2573        opline->result_type |= EXT_TYPE_UNUSED;
2574    }
2575
2576    if (zend_is_call(source_ast)) {
2577        opline->extended_value = ZEND_RETURNS_FUNCTION;
2578    }
2579}
2580/* }}} */
2581
2582static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, znode *value_node) /* {{{ */
2583{
2584    zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast,
2585        zend_ast_create_znode(value_node));
2586    zend_compile_assign_ref(NULL, assign_ast);
2587}
2588/* }}} */
2589
2590void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */
2591{
2592    zend_ast *var_ast = ast->child[0];
2593    zend_ast *expr_ast = ast->child[1];
2594    uint32_t opcode = ast->attr;
2595
2596    znode var_node, expr_node;
2597    zend_op *opline;
2598    uint32_t offset;
2599
2600    zend_ensure_writable_variable(var_ast);
2601
2602    switch (var_ast->kind) {
2603        case ZEND_AST_VAR:
2604        case ZEND_AST_STATIC_PROP:
2605            zend_compile_var(&var_node, var_ast, BP_VAR_RW);
2606            zend_compile_expr(&expr_node, expr_ast);
2607            zend_emit_op(result, opcode, &var_node, &expr_node);
2608            return;
2609        case ZEND_AST_DIM:
2610            offset = zend_delayed_compile_begin();
2611            zend_delayed_compile_dim(result, var_ast, BP_VAR_RW);
2612            zend_compile_expr(&expr_node, expr_ast);
2613
2614            opline = zend_delayed_compile_end(offset);
2615            opline->opcode = opcode;
2616            opline->extended_value = ZEND_ASSIGN_DIM;
2617
2618            opline = zend_emit_op_data(&expr_node);
2619            return;
2620        case ZEND_AST_PROP:
2621            offset = zend_delayed_compile_begin();
2622            zend_delayed_compile_prop(result, var_ast, BP_VAR_RW);
2623            zend_compile_expr(&expr_node, expr_ast);
2624
2625            opline = zend_delayed_compile_end(offset);
2626            opline->opcode = opcode;
2627            opline->extended_value = ZEND_ASSIGN_OBJ;
2628
2629            zend_emit_op_data(&expr_node);
2630            return;
2631        EMPTY_SWITCH_DEFAULT_CASE()
2632    }
2633}
2634/* }}} */
2635
2636uint32_t zend_compile_args(zend_ast *ast, zend_function *fbc) /* {{{ */
2637{
2638    /* TODO.AST &var error */
2639    zend_ast_list *args = zend_ast_get_list(ast);
2640    uint32_t i;
2641    zend_bool uses_arg_unpack = 0;
2642    uint32_t arg_count = 0; /* number of arguments not including unpacks */
2643
2644    for (i = 0; i < args->children; ++i) {
2645        zend_ast *arg = args->child[i];
2646        uint32_t arg_num = i + 1;
2647
2648        znode arg_node;
2649        zend_op *opline;
2650        zend_uchar opcode;
2651        zend_ulong flags = 0;
2652
2653        if (arg->kind == ZEND_AST_UNPACK) {
2654            uses_arg_unpack = 1;
2655            fbc = NULL;
2656
2657            zend_compile_expr(&arg_node, arg->child[0]);
2658            opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL);
2659            opline->op2.num = arg_count;
2660            opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_count);
2661            continue;
2662        }
2663
2664        if (uses_arg_unpack) {
2665            zend_error_noreturn(E_COMPILE_ERROR,
2666                "Cannot use positional argument after argument unpacking");
2667        }
2668
2669        arg_count++;
2670        if (zend_is_variable(arg)) {
2671            if (zend_is_call(arg)) {
2672                zend_compile_var(&arg_node, arg, BP_VAR_R);
2673                if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) {
2674                    /* Function call was converted into builtin instruction */
2675                    opcode = ZEND_SEND_VAL;
2676                } else {
2677                    opcode = ZEND_SEND_VAR_NO_REF;
2678                    flags |= ZEND_ARG_SEND_FUNCTION;
2679                    if (fbc && ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
2680                        flags |= ZEND_ARG_SEND_BY_REF;
2681                        if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) {
2682                            flags |= ZEND_ARG_SEND_SILENT;
2683                        }
2684                    }
2685                }
2686            } else if (fbc) {
2687                if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) {
2688                    zend_compile_var(&arg_node, arg, BP_VAR_W);
2689                    opcode = ZEND_SEND_REF;
2690                } else {
2691                    zend_compile_var(&arg_node, arg, BP_VAR_R);
2692                    opcode = ZEND_SEND_VAR;
2693                }
2694            } else {
2695                zend_compile_var(&arg_node, arg,
2696                    BP_VAR_FUNC_ARG | (arg_num << BP_VAR_SHIFT));
2697                opcode = ZEND_SEND_VAR_EX;
2698            }
2699        } else {
2700            zend_compile_expr(&arg_node, arg);
2701            if (arg_node.op_type & (IS_VAR|IS_CV)) {
2702                opcode = ZEND_SEND_VAR_NO_REF;
2703                if (fbc && ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
2704                    flags |= ZEND_ARG_SEND_BY_REF;
2705                }
2706            } else {
2707                if (fbc) {
2708                    opcode = ZEND_SEND_VAL;
2709                    if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) {
2710                        zend_error_noreturn(E_COMPILE_ERROR, "Only variables can be passed by reference");
2711                    }
2712                } else {
2713                    opcode = ZEND_SEND_VAL_EX;
2714                }
2715            }
2716        }
2717
2718        opline = get_next_op(CG(active_op_array));
2719        opline->opcode = opcode;
2720        SET_NODE(opline->op1, &arg_node);
2721        SET_UNUSED(opline->op2);
2722        opline->op2.opline_num = arg_num;
2723        opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, arg_num);
2724
2725        if (opcode == ZEND_SEND_VAR_NO_REF) {
2726            if (fbc) {
2727                flags |= ZEND_ARG_COMPILE_TIME_BOUND;
2728            }
2729            if ((flags & ZEND_ARG_COMPILE_TIME_BOUND) && !(flags & ZEND_ARG_SEND_BY_REF)) {
2730                opline->opcode = ZEND_SEND_VAR;
2731                opline->extended_value = ZEND_ARG_COMPILE_TIME_BOUND;
2732            } else {
2733                opline->extended_value = flags;
2734            }
2735        } else if (fbc) {
2736            opline->extended_value = ZEND_ARG_COMPILE_TIME_BOUND;
2737        }
2738    }
2739
2740    return arg_count;
2741}
2742/* }}} */
2743
2744ZEND_API zend_uchar zend_get_call_op(zend_uchar init_op, zend_function *fbc) /* {{{ */
2745{
2746    if (fbc) {
2747        if (fbc->type == ZEND_INTERNAL_FUNCTION) {
2748            if (!zend_execute_internal &&
2749                !fbc->common.scope &&
2750                !(fbc->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED|ZEND_ACC_HAS_TYPE_HINTS|ZEND_ACC_RETURN_REFERENCE))) {
2751                return ZEND_DO_ICALL;
2752            }
2753        } else {
2754            if (zend_execute_ex == execute_ex &&
2755                !(fbc->common.fn_flags & ZEND_ACC_GENERATOR)) {
2756                return ZEND_DO_UCALL;
2757            }
2758        }
2759    } else if (zend_execute_ex == execute_ex &&
2760               !zend_execute_internal &&
2761               (init_op == ZEND_INIT_FCALL_BY_NAME ||
2762                init_op == ZEND_INIT_NS_FCALL_BY_NAME)) {
2763        return ZEND_DO_FCALL_BY_NAME;
2764    }
2765    return ZEND_DO_FCALL;
2766}
2767/* }}} */
2768
2769void zend_compile_call_common(znode *result, zend_ast *args_ast, zend_function *fbc) /* {{{ */
2770{
2771    zend_op *opline;
2772    uint32_t opnum_init = get_next_op_number(CG(active_op_array)) - 1;
2773    uint32_t arg_count;
2774    uint32_t call_flags;
2775
2776    zend_do_extended_fcall_begin();
2777
2778    arg_count = zend_compile_args(args_ast, fbc);
2779
2780    opline = &CG(active_op_array)->opcodes[opnum_init];
2781    opline->extended_value = arg_count;
2782
2783    if (opline->opcode == ZEND_INIT_FCALL) {
2784        opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc);
2785    }
2786
2787    call_flags = (opline->opcode == ZEND_NEW ? ZEND_CALL_CTOR : 0);
2788    opline = zend_emit_op(result, zend_get_call_op(opline->opcode, fbc), NULL, NULL);
2789    opline->op1.num = call_flags;
2790
2791    zend_do_extended_fcall_end();
2792}
2793/* }}} */
2794
2795zend_bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */
2796{
2797    zend_string *orig_name = zend_ast_get_str(name_ast);
2798    zend_bool is_fully_qualified;
2799
2800    name_node->op_type = IS_CONST;
2801    ZVAL_STR(&name_node->u.constant, zend_resolve_function_name(
2802        orig_name, name_ast->attr, &is_fully_qualified));
2803
2804    return !is_fully_qualified && FC(current_namespace);
2805}
2806/* }}} */
2807
2808void zend_compile_ns_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
2809{
2810    zend_op *opline = get_next_op(CG(active_op_array));
2811    opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME;
2812    SET_UNUSED(opline->op1);
2813    opline->op2_type = IS_CONST;
2814    opline->op2.constant = zend_add_ns_func_name_literal(
2815        CG(active_op_array), Z_STR(name_node->u.constant));
2816    zend_alloc_cache_slot(opline->op2.constant);
2817
2818    zend_compile_call_common(result, args_ast, NULL);
2819}
2820/* }}} */
2821
2822void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast) /* {{{ */
2823{
2824    zend_op *opline = get_next_op(CG(active_op_array));
2825    if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) {
2826        const char *colon;
2827        zend_string *str = Z_STR(name_node->u.constant);
2828        if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') {
2829            zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0);
2830            zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0);
2831            opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
2832            opline->op1_type = IS_CONST;
2833            opline->op1.constant = zend_add_class_name_literal(CG(active_op_array), class);
2834            opline->op2_type = IS_CONST;
2835            opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), method);
2836            zend_alloc_cache_slot(opline->op2.constant);
2837            zval_ptr_dtor(&name_node->u.constant);
2838        } else {
2839            opline->opcode = ZEND_INIT_FCALL_BY_NAME;
2840            SET_UNUSED(opline->op1);
2841            opline->op2_type = IS_CONST;
2842            opline->op2.constant = zend_add_func_name_literal(CG(active_op_array), str);
2843            zend_alloc_cache_slot(opline->op2.constant);
2844        }
2845    } else {
2846        opline->opcode = ZEND_INIT_DYNAMIC_CALL;
2847        SET_UNUSED(opline->op1);
2848        SET_NODE(opline->op2, name_node);
2849    }
2850
2851    zend_compile_call_common(result, args_ast, NULL);
2852}
2853/* }}} */
2854
2855static zend_bool zend_args_contain_unpack(zend_ast_list *args) /* {{{ */
2856{
2857    uint32_t i;
2858    for (i = 0; i < args->children; ++i) {
2859        if (args->child[i]->kind == ZEND_AST_UNPACK) {
2860            return 1;
2861        }
2862    }
2863    return 0;
2864}
2865/* }}} */
2866
2867int zend_compile_func_strlen(znode *result, zend_ast_list *args) /* {{{ */
2868{
2869    znode arg_node;
2870
2871    if ((CG(compiler_options) & ZEND_COMPILE_NO_BUILTIN_STRLEN)
2872        || args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK
2873    ) {
2874        return FAILURE;
2875    }
2876
2877    zend_compile_expr(&arg_node, args->child[0]);
2878    if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) {
2879        result->op_type = IS_CONST;
2880        ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant));
2881        zval_dtor(&arg_node.u.constant);
2882    } else {
2883        zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL);
2884    }
2885    return SUCCESS;
2886}
2887/* }}} */
2888
2889int zend_compile_func_typecheck(znode *result, zend_ast_list *args, uint32_t type) /* {{{ */
2890{
2891    znode arg_node;
2892    zend_op *opline;
2893
2894    if (args->children != 1 || args->child[0]->kind == ZEND_AST_UNPACK) {
2895        return FAILURE;
2896    }
2897
2898    zend_compile_expr(&arg_node, args->child[0]);
2899    opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL);
2900    opline->extended_value = type;
2901    return SUCCESS;
2902}
2903/* }}} */
2904
2905int zend_compile_func_defined(znode *result, zend_ast_list *args) /* {{{ */
2906{
2907    zend_string *name;
2908    zend_op *opline;
2909
2910    if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) {
2911        return FAILURE;
2912    }
2913
2914    name = zval_get_string(zend_ast_get_zval(args->child[0]));
2915    if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) {
2916        zend_string_release(name);
2917        return FAILURE;
2918    }
2919
2920    opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL);
2921    opline->op1_type = IS_CONST;
2922    LITERAL_STR(opline->op1, name);
2923    zend_alloc_cache_slot(opline->op1.constant);
2924
2925    /* Lowercase constant name in a separate literal */
2926    {
2927        zval c;
2928        zend_string *lcname = zend_string_tolower(name);
2929        ZVAL_NEW_STR(&c, lcname);
2930        zend_add_literal(CG(active_op_array), &c);
2931    }
2932    return SUCCESS;
2933}
2934/* }}} */
2935
2936static int zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */
2937{
2938    zend_string *name, *lcname;
2939    zend_function *fbc;
2940    zend_op *opline;
2941
2942    if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
2943        return FAILURE;
2944    }
2945
2946    name = zend_ast_get_str(name_ast);
2947    lcname = zend_string_tolower(name);
2948
2949    fbc = zend_hash_find_ptr(CG(function_table), lcname);
2950    if (!fbc || (fbc->type == ZEND_INTERNAL_FUNCTION &&
2951        (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
2952    ) {
2953        zend_string_release(lcname);
2954        return FAILURE;
2955    }
2956
2957    opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL);
2958    opline->extended_value = num_args;
2959    opline->op1.num = zend_vm_calc_used_stack(num_args, fbc);
2960    opline->op2_type = IS_CONST;
2961    LITERAL_STR(opline->op2, lcname);
2962    zend_alloc_cache_slot(opline->op2.constant);
2963
2964    return SUCCESS;
2965}
2966/* }}} */
2967
2968static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */
2969{
2970    zend_op *opline;
2971    znode name_node;
2972
2973    if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) {
2974        return;
2975    }
2976
2977    zend_compile_expr(&name_node, name_ast);
2978
2979    opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node);
2980    opline->op1_type = IS_CONST;
2981    LITERAL_STR(opline->op1, zend_string_copy(orig_func_name));
2982    opline->extended_value = num_args;
2983}
2984/* }}} */
2985
2986/* cufa = call_user_func_array */
2987int zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
2988{
2989    znode arg_node;
2990
2991    if (args->children != 2 || zend_args_contain_unpack(args)) {
2992        return FAILURE;
2993    }
2994
2995    zend_compile_init_user_func(args->child[0], 0, lcname);
2996    zend_compile_expr(&arg_node, args->child[1]);
2997    zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL);
2998    zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
2999
3000    return SUCCESS;
3001}
3002/* }}} */
3003
3004/* cuf = call_user_func */
3005int zend_compile_func_cuf(znode *result, zend_ast_list *args, zend_string *lcname) /* {{{ */
3006{
3007    uint32_t i;
3008
3009    if (args->children < 1 || zend_args_contain_unpack(args)) {
3010        return FAILURE;
3011    }
3012
3013    zend_compile_init_user_func(args->child[0], args->children - 1, lcname);
3014    for (i = 1; i < args->children; ++i) {
3015        zend_ast *arg_ast = args->child[i];
3016        znode arg_node;
3017        zend_op *opline;
3018        zend_bool send_user = 0;
3019
3020        if (zend_is_variable(arg_ast) && !zend_is_call(arg_ast)) {
3021            zend_compile_var(&arg_node, arg_ast, BP_VAR_FUNC_ARG | (i << BP_VAR_SHIFT));
3022            send_user = 1;
3023        } else {
3024            zend_compile_expr(&arg_node, arg_ast);
3025            if (arg_node.op_type & (IS_VAR|IS_CV)) {
3026                send_user = 1;
3027            }
3028        }
3029
3030        if (send_user) {
3031            opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL);
3032        } else {
3033            opline = zend_emit_op(NULL, ZEND_SEND_VAL, &arg_node, NULL);
3034        }
3035
3036        opline->op2.num = i;
3037        opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_ARG(NULL, i);
3038    }
3039    zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL);
3040
3041    return SUCCESS;
3042}
3043/* }}} */
3044
3045static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
3046{
3047    if (EG(assertions) >= 0) {
3048        znode name_node;
3049        zend_op *opline;
3050        uint32_t check_op_number = get_next_op_number(CG(active_op_array));
3051
3052        zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
3053
3054        if (fbc) {
3055            name_node.op_type = IS_CONST;
3056            ZVAL_STR_COPY(&name_node.u.constant, name);
3057
3058            opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3059        } else {
3060            opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
3061            opline->op2_type = IS_CONST;
3062            opline->op2.constant = zend_add_ns_func_name_literal(
3063                CG(active_op_array), name);
3064        }
3065        zend_alloc_cache_slot(opline->op2.constant);
3066
3067        if (args->children == 1 &&
3068            (args->child[0]->kind != ZEND_AST_ZVAL ||
3069             Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
3070            /* add "assert(condition) as assertion message */
3071            zend_ast_list_add((zend_ast*)args,
3072                zend_ast_create_zval_from_str(
3073                    zend_ast_export("assert(", args->child[0], ")")));
3074        }
3075
3076        zend_compile_call_common(result, (zend_ast*)args, fbc);
3077
3078        CG(active_op_array)->opcodes[check_op_number].op2.opline_num = get_next_op_number(CG(active_op_array));
3079    } else {
3080        if (!fbc) {
3081            zend_string_release(name);
3082        }
3083        result->op_type = IS_CONST;
3084        ZVAL_TRUE(&result->u.constant);
3085    }
3086
3087    return SUCCESS;
3088}
3089/* }}} */
3090
3091int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc) /* {{{ */
3092{
3093    if (fbc->internal_function.handler == ZEND_FN(display_disabled_function)) {
3094        return FAILURE;
3095    }
3096
3097    if (zend_string_equals_literal(lcname, "strlen")) {
3098        return zend_compile_func_strlen(result, args);
3099    } else if (zend_string_equals_literal(lcname, "is_null")) {
3100        return zend_compile_func_typecheck(result, args, IS_NULL);
3101    } else if (zend_string_equals_literal(lcname, "is_bool")) {
3102        return zend_compile_func_typecheck(result, args, _IS_BOOL);
3103    } else if (zend_string_equals_literal(lcname, "is_long")
3104        || zend_string_equals_literal(lcname, "is_int")
3105        || zend_string_equals_literal(lcname, "is_integer")
3106    ) {
3107        return zend_compile_func_typecheck(result, args, IS_LONG);
3108    } else if (zend_string_equals_literal(lcname, "is_float")
3109        || zend_string_equals_literal(lcname, "is_double")
3110        || zend_string_equals_literal(lcname, "is_real")
3111    ) {
3112        return zend_compile_func_typecheck(result, args, IS_DOUBLE);
3113    } else if (zend_string_equals_literal(lcname, "is_string")) {
3114        return zend_compile_func_typecheck(result, args, IS_STRING);
3115    } else if (zend_string_equals_literal(lcname, "is_array")) {
3116        return zend_compile_func_typecheck(result, args, IS_ARRAY);
3117    } else if (zend_string_equals_literal(lcname, "is_object")) {
3118        return zend_compile_func_typecheck(result, args, IS_OBJECT);
3119    } else if (zend_string_equals_literal(lcname, "is_resource")) {
3120        return zend_compile_func_typecheck(result, args, IS_RESOURCE);
3121    } else if (zend_string_equals_literal(lcname, "defined")) {
3122        return zend_compile_func_defined(result, args);
3123    } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
3124        return zend_compile_func_cufa(result, args, lcname);
3125    } else if (zend_string_equals_literal(lcname, "call_user_func")) {
3126        return zend_compile_func_cuf(result, args, lcname);
3127    } else if (zend_string_equals_literal(lcname, "assert")) {
3128        return zend_compile_assert(result, args, lcname, fbc);
3129    } else {
3130        return FAILURE;
3131    }
3132}
3133/* }}} */
3134
3135void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3136{
3137    zend_ast *name_ast = ast->child[0];
3138    zend_ast *args_ast = ast->child[1];
3139
3140    znode name_node;
3141
3142    if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
3143        zend_compile_expr(&name_node, name_ast);
3144        zend_compile_dynamic_call(result, &name_node, args_ast);
3145        return;
3146    }
3147
3148    {
3149        zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
3150        if (runtime_resolution) {
3151            if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
3152                zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
3153            } else {
3154                zend_compile_ns_call(result, &name_node, args_ast);
3155            }
3156            return;
3157        }
3158    }
3159
3160    {
3161        zval *name = &name_node.u.constant;
3162        zend_string *lcname;
3163        zend_function *fbc;
3164        zend_op *opline;
3165
3166        lcname = zend_string_tolower(Z_STR_P(name));
3167
3168        fbc = zend_hash_find_ptr(CG(function_table), lcname);
3169        if (!fbc || (fbc->type == ZEND_INTERNAL_FUNCTION &&
3170            (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
3171        ) {
3172            zend_string_release(lcname);
3173            zend_compile_dynamic_call(result, &name_node, args_ast);
3174            return;
3175        }
3176
3177        if (zend_try_compile_special_func(result, lcname,
3178                zend_ast_get_list(args_ast), fbc) == SUCCESS
3179        ) {
3180            zend_string_release(lcname);
3181            zval_ptr_dtor(&name_node.u.constant);
3182            return;
3183        }
3184
3185        zval_ptr_dtor(&name_node.u.constant);
3186        ZVAL_NEW_STR(&name_node.u.constant, lcname);
3187
3188        opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3189        zend_alloc_cache_slot(opline->op2.constant);
3190
3191        zend_compile_call_common(result, args_ast, fbc);
3192    }
3193}
3194/* }}} */
3195
3196void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3197{
3198    zend_ast *obj_ast = ast->child[0];
3199    zend_ast *method_ast = ast->child[1];
3200    zend_ast *args_ast = ast->child[2];
3201
3202    znode obj_node, method_node;
3203    zend_op *opline;
3204
3205    if (is_this_fetch(obj_ast)) {
3206        obj_node.op_type = IS_UNUSED;
3207    } else {
3208        zend_compile_expr(&obj_node, obj_ast);
3209    }
3210
3211    zend_compile_expr(&method_node, method_ast);
3212    opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
3213
3214    if (method_node.op_type == IS_CONST) {
3215        if (Z_TYPE(method_node.u.constant) != IS_STRING) {
3216            zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3217        }
3218
3219        opline->op2_type = IS_CONST;
3220        opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3221            Z_STR(method_node.u.constant));
3222        zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3223    } else {
3224        SET_NODE(opline->op2, &method_node);
3225    }
3226
3227    zend_compile_call_common(result, args_ast, NULL);
3228}
3229/* }}} */
3230
3231static zend_bool zend_is_constructor(zend_string *name) /* {{{ */
3232{
3233    return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
3234}
3235/* }}} */
3236
3237void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3238{
3239    zend_ast *class_ast = ast->child[0];
3240    zend_ast *method_ast = ast->child[1];
3241    zend_ast *args_ast = ast->child[2];
3242
3243    znode class_node, method_node;
3244    zend_op *opline;
3245    zend_ulong extended_value = 0;
3246
3247    if (zend_is_const_default_class_ref(class_ast)) {
3248        class_node.op_type = IS_CONST;
3249        ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(class_ast));
3250    } else {
3251        opline = zend_compile_class_ref(&class_node, class_ast, 1);
3252        extended_value = opline->extended_value;
3253    }
3254
3255    zend_compile_expr(&method_node, method_ast);
3256    if (method_node.op_type == IS_CONST) {
3257        zval *name = &method_node.u.constant;
3258        if (Z_TYPE_P(name) != IS_STRING) {
3259            zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3260        }
3261        if (zend_is_constructor(Z_STR_P(name))) {
3262            zval_ptr_dtor(name);
3263            method_node.op_type = IS_UNUSED;
3264        }
3265    }
3266
3267    opline = get_next_op(CG(active_op_array));
3268    opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
3269    opline->extended_value = extended_value;
3270
3271    zend_set_class_name_op1(opline, &class_node);
3272
3273    if (method_node.op_type == IS_CONST) {
3274        opline->op2_type = IS_CONST;
3275        opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3276            Z_STR(method_node.u.constant));
3277        if (opline->op1_type == IS_CONST) {
3278            zend_alloc_cache_slot(opline->op2.constant);
3279        } else {
3280            zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3281        }
3282    } else {
3283        SET_NODE(opline->op2, &method_node);
3284    }
3285
3286    zend_compile_call_common(result, args_ast, NULL);
3287}
3288/* }}} */
3289
3290void zend_compile_class_decl(zend_ast *ast);
3291
3292void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
3293{
3294    zend_ast *class_ast = ast->child[0];
3295    zend_ast *args_ast = ast->child[1];
3296
3297    znode class_node, ctor_result;
3298    zend_op *opline;
3299    uint32_t opnum;
3300
3301    if (zend_is_const_default_class_ref(class_ast)) {
3302        class_node.op_type = IS_CONST;
3303        ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(class_ast));
3304    } else if (class_ast->kind == ZEND_AST_CLASS) {
3305        uint32_t dcl_opnum = get_next_op_number(CG(active_op_array));
3306        zend_compile_class_decl(class_ast);
3307        /* jump over anon class declaration */
3308        opline = &CG(active_op_array)->opcodes[dcl_opnum];
3309        if (opline->opcode == ZEND_FETCH_CLASS) {
3310            opline++;
3311        }
3312        class_node.op_type = opline->result_type;
3313        class_node.u.op.var = opline->result.var;
3314        opline->op1.opline_num = get_next_op_number(CG(active_op_array));
3315    } else {
3316        zend_compile_class_ref(&class_node, class_ast, 1);
3317    }
3318
3319    opnum = get_next_op_number(CG(active_op_array));
3320    opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
3321
3322    if (class_node.op_type == IS_CONST) {
3323        opline->op1_type = IS_CONST;
3324        opline->op1.constant = zend_add_class_name_literal(
3325            CG(active_op_array), Z_STR(class_node.u.constant));
3326    } else {
3327        SET_NODE(opline->op1, &class_node);
3328    }
3329
3330    zend_compile_call_common(&ctor_result, args_ast, NULL);
3331    zend_do_free(&ctor_result);
3332
3333    /* New jumps over ctor call if ctor does not exist */
3334    opline = &CG(active_op_array)->opcodes[opnum];
3335    opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3336}
3337/* }}} */
3338
3339void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
3340{
3341    zend_ast *obj_ast = ast->child[0];
3342
3343    znode obj_node;
3344    zend_compile_expr(&obj_node, obj_ast);
3345
3346    zend_emit_op(result, ZEND_CLONE, &obj_node, NULL);
3347}
3348/* }}} */
3349
3350void zend_compile_global_var(zend_ast *ast) /* {{{ */
3351{
3352    zend_ast *var_ast = ast->child[0];
3353    zend_ast *name_ast = var_ast->child[0];
3354
3355    znode name_node, result;
3356
3357    zend_compile_expr(&name_node, name_ast);
3358    if (name_node.op_type == IS_CONST) {
3359        convert_to_string(&name_node.u.constant);
3360    }
3361
3362    if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
3363        zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
3364        zend_alloc_cache_slot(opline->op2.constant);
3365    } else {
3366        zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
3367
3368        // TODO.AST Avoid double fetch
3369        //opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
3370
3371        zend_emit_assign_ref_znode(var_ast, &result);
3372    }
3373}
3374/* }}} */
3375
3376static void zend_compile_static_var_common(zend_ast *var_ast, zval *value, zend_bool by_ref) /* {{{ */
3377{
3378    znode var_node, result;
3379    zend_op *opline;
3380
3381    zend_compile_expr(&var_node, var_ast);
3382
3383    if (!CG(active_op_array)->static_variables) {
3384        if (CG(active_op_array)->scope) {
3385            CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
3386        }
3387        ALLOC_HASHTABLE(CG(active_op_array)->static_variables);
3388        zend_hash_init(CG(active_op_array)->static_variables, 8, NULL, ZVAL_PTR_DTOR, 0);
3389    }
3390
3391    if (GC_REFCOUNT(CG(active_op_array)->static_variables) > 1) {
3392        if (!(GC_FLAGS(CG(active_op_array)->static_variables) & IS_ARRAY_IMMUTABLE)) {
3393            GC_REFCOUNT(CG(active_op_array)->static_variables)--;
3394        }
3395        CG(active_op_array)->static_variables = zend_array_dup(CG(active_op_array)->static_variables);
3396    }
3397    zend_hash_update(CG(active_op_array)->static_variables, Z_STR(var_node.u.constant), value);
3398
3399    opline = zend_emit_op(&result, by_ref ? ZEND_FETCH_W : ZEND_FETCH_R, &var_node, NULL);
3400    opline->extended_value = ZEND_FETCH_STATIC;
3401
3402    if (by_ref) {
3403        zend_ast *fetch_ast = zend_ast_create(ZEND_AST_VAR, var_ast);
3404        zend_emit_assign_ref_znode(fetch_ast, &result);
3405    } else {
3406        zend_ast *fetch_ast = zend_ast_create(ZEND_AST_VAR, var_ast);
3407        zend_emit_assign_znode(fetch_ast, &result);
3408    }
3409}
3410/* }}} */
3411
3412void zend_compile_static_var(zend_ast *ast) /* {{{ */
3413{
3414    zend_ast *var_ast = ast->child[0];
3415    zend_ast *value_ast = ast->child[1];
3416    zval value_zv;
3417
3418    if (value_ast) {
3419        zend_const_expr_to_zval(&value_zv, value_ast);
3420    } else {
3421        ZVAL_NULL(&value_zv);
3422    }
3423
3424    zend_compile_static_var_common(var_ast, &value_zv, 1);
3425}
3426/* }}} */
3427
3428void zend_compile_unset(zend_ast *ast) /* {{{ */
3429{
3430    zend_ast *var_ast = ast->child[0];
3431
3432    znode var_node;
3433    zend_op *opline;
3434    switch (var_ast->kind) {
3435        case ZEND_AST_VAR:
3436            if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
3437                opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
3438                opline->extended_value = ZEND_FETCH_LOCAL | ZEND_QUICK_SET;
3439            } else {
3440                opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
3441                opline->opcode = ZEND_UNSET_VAR;
3442            }
3443            return;
3444        case ZEND_AST_DIM:
3445            opline = zend_compile_dim_common(NULL, var_ast, BP_VAR_UNSET);
3446            opline->opcode = ZEND_UNSET_DIM;
3447            return;
3448        case ZEND_AST_PROP:
3449            opline = zend_compile_prop_common(NULL, var_ast, BP_VAR_UNSET);
3450            opline->opcode = ZEND_UNSET_OBJ;
3451            return;
3452        case ZEND_AST_STATIC_PROP:
3453            opline = zend_compile_static_prop_common(NULL, var_ast, BP_VAR_UNSET, 0);
3454            opline->opcode = ZEND_UNSET_VAR;
3455            return;
3456        EMPTY_SWITCH_DEFAULT_CASE()
3457    }
3458}
3459/* }}} */
3460
3461static void zend_free_foreach_and_switch_variables(void) /* {{{ */
3462{
3463    zend_stack_apply(&CG(loop_var_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element)) generate_free_loop_var);
3464}
3465/* }}} */
3466
3467
3468void zend_compile_return(zend_ast *ast) /* {{{ */
3469{
3470    zend_ast *expr_ast = ast->child[0];
3471    zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
3472
3473    znode expr_node;
3474    zend_op *opline;
3475
3476    if (!expr_ast) {
3477        expr_node.op_type = IS_CONST;
3478        ZVAL_NULL(&expr_node.u.constant);
3479    } else if (by_ref && zend_is_variable(expr_ast) && !zend_is_call(expr_ast)) {
3480        zend_compile_var(&expr_node, expr_ast, BP_VAR_REF);
3481    } else {
3482        zend_compile_expr(&expr_node, expr_ast);
3483    }
3484
3485    zend_free_foreach_and_switch_variables();
3486
3487    if (CG(context).in_finally) {
3488        opline = zend_emit_op(NULL, ZEND_DISCARD_EXCEPTION, NULL, NULL);
3489        opline->op1_type = IS_TMP_VAR;
3490        opline->op1.var = CG(context).fast_call_var;
3491    }
3492
3493    /* Generator return types are handled separately */
3494    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) && CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
3495        zend_emit_return_type_check(expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1);
3496    }
3497    opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
3498        &expr_node, NULL);
3499
3500    if (expr_ast) {
3501        if (zend_is_call(expr_ast)) {
3502            opline->extended_value = ZEND_RETURNS_FUNCTION;
3503        } else if (by_ref && !zend_is_variable(expr_ast)) {
3504            opline->extended_value = ZEND_RETURNS_VALUE;
3505        }
3506    }
3507}
3508/* }}} */
3509
3510void zend_compile_echo(zend_ast *ast) /* {{{ */
3511{
3512    zend_ast *expr_ast = ast->child[0];
3513
3514    znode expr_node;
3515    zend_compile_expr(&expr_node, expr_ast);
3516
3517    zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
3518}
3519/* }}} */
3520
3521void zend_compile_throw(zend_ast *ast) /* {{{ */
3522{
3523    zend_ast *expr_ast = ast->child[0];
3524
3525    znode expr_node;
3526    zend_compile_expr(&expr_node, expr_ast);
3527
3528    zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
3529}
3530/* }}} */
3531
3532void zend_compile_break_continue(zend_ast *ast) /* {{{ */
3533{
3534    zend_ast *depth_ast = ast->child[0];
3535
3536    zend_op *opline;
3537    int depth;
3538
3539    ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
3540
3541    if (depth_ast) {
3542        zval *depth_zv;
3543        if (depth_ast->kind != ZEND_AST_ZVAL) {
3544            zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-constant operand "
3545                "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
3546        }
3547
3548        depth_zv = zend_ast_get_zval(depth_ast);
3549        if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) {
3550            zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive numbers",
3551                ast->kind == ZEND_AST_BREAK ? "break" : "continue");
3552        }
3553
3554        depth = Z_LVAL_P(depth_zv);
3555    } else {
3556        depth = 1;
3557    }
3558
3559    if (CG(context).current_brk_cont == -1) {
3560        zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context",
3561            ast->kind == ZEND_AST_BREAK ? "break" : "continue");
3562    } else {
3563        int array_offset = CG(context).current_brk_cont;
3564        zend_long nest_level = depth;
3565        znode *loop_var = zend_stack_top(&CG(loop_var_stack));
3566
3567        do {
3568            if (array_offset == -1) {
3569                zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' %d level%s",
3570                    ast->kind == ZEND_AST_BREAK ? "break" : "continue",
3571                    depth, depth == 1 ? "" : "s");
3572            }
3573
3574            if (nest_level > 1 && CG(context).brk_cont_array[array_offset].start >= 0) {
3575                generate_free_loop_var(loop_var);
3576                loop_var--;
3577            }
3578
3579            array_offset = CG(context).brk_cont_array[array_offset].parent;
3580        } while (--nest_level > 0);
3581    }
3582    opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL);
3583    opline->op1.num = CG(context).current_brk_cont;
3584    opline->op2.num = depth;
3585}
3586/* }}} */
3587
3588void zend_resolve_goto_label(zend_op_array *op_array, znode *label_node, zend_op *pass2_opline) /* {{{ */
3589{
3590    zend_label *dest;
3591    int current, distance, free_vars;
3592    zval *label;
3593    znode *loop_var = NULL;
3594
3595    if (pass2_opline) {
3596        label = RT_CONSTANT(op_array, pass2_opline->op2);
3597    } else {
3598        label = &label_node->u.constant;
3599    }
3600    if (CG(context).labels == NULL ||
3601        (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL) {
3602
3603        if (pass2_opline) {
3604            CG(in_compilation) = 1;
3605            CG(active_op_array) = op_array;
3606            CG(zend_lineno) = pass2_opline->lineno;
3607            zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label));
3608        } else {
3609            /* Label is not defined. Delay to pass 2. */
3610            zend_op *opline;
3611
3612            current = CG(context).current_brk_cont;
3613            while (current != -1) {
3614                if (CG(context).brk_cont_array[current].start >= 0) {
3615                    zend_emit_op(NULL, ZEND_NOP, NULL, NULL);
3616                }
3617                current = CG(context).brk_cont_array[current].parent;
3618            }
3619            opline = zend_emit_op(NULL, ZEND_GOTO, NULL, label_node);
3620            opline->extended_value = CG(context).current_brk_cont;
3621            return;
3622        }
3623    }
3624
3625    zval_dtor(label);
3626    ZVAL_NULL(label);
3627
3628    /* Check that we are not moving into loop or switch */
3629    if (pass2_opline) {
3630        current = pass2_opline->extended_value;
3631    } else {
3632        current = CG(context).current_brk_cont;
3633    }
3634    if (!pass2_opline) {
3635        loop_var = zend_stack_top(&CG(loop_var_stack));
3636    }
3637    for (distance = 0, free_vars = 0; current != dest->brk_cont; distance++) {
3638        if (current == -1) {
3639            if (pass2_opline) {
3640                CG(in_compilation) = 1;
3641                CG(active_op_array) = op_array;
3642                CG(zend_lineno) = pass2_opline->lineno;
3643            }
3644            zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed");
3645        }
3646        if (CG(context).brk_cont_array[current].start >= 0) {
3647            if (pass2_opline) {
3648                free_vars++;
3649            } else {
3650                generate_free_loop_var(loop_var);
3651                loop_var--;
3652            }
3653        }
3654        current = CG(context).brk_cont_array[current].parent;
3655    }
3656
3657    if (pass2_opline) {
3658        if (free_vars) {
3659            current = pass2_opline->extended_value;
3660            while (current != dest->brk_cont) {
3661                if (CG(context).brk_cont_array[current].start >= 0) {
3662                    zend_op *brk_opline = &op_array->opcodes[CG(context).brk_cont_array[current].brk];
3663
3664                    if (brk_opline->opcode == ZEND_FREE) {
3665                        (pass2_opline - free_vars)->opcode = ZEND_FREE;
3666                        (pass2_opline - free_vars)->op1_type = brk_opline->op1_type;
3667                        if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
3668                            (pass2_opline - free_vars)->op1.var = brk_opline->op1.var;
3669                        } else {
3670                            (pass2_opline - free_vars)->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + brk_opline->op1.var);
3671                            ZEND_VM_SET_OPCODE_HANDLER(pass2_opline - free_vars);
3672                        }
3673                        free_vars--;
3674                    } else if (brk_opline->opcode == ZEND_FE_FREE) {
3675                        (pass2_opline - free_vars)->opcode = ZEND_FE_FREE;
3676                        (pass2_opline - free_vars)->op1_type = brk_opline->op1_type;
3677                        if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
3678                            (pass2_opline - free_vars)->op1.var = brk_opline->op1.var;
3679                        } else {
3680                            (pass2_opline - free_vars)->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + brk_opline->op1.var);
3681                            ZEND_VM_SET_OPCODE_HANDLER(pass2_opline - free_vars);
3682                        }
3683                        free_vars--;
3684                    }
3685                }
3686                current = CG(context).brk_cont_array[current].parent;
3687            }
3688        }
3689        pass2_opline->opcode = ZEND_JMP;
3690        pass2_opline->op1.opline_num = dest->opline_num;
3691        SET_UNUSED(pass2_opline->op2);
3692        pass2_opline->extended_value = 0;
3693    } else {
3694        zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
3695        opline->op1.opline_num = dest->opline_num;
3696    }
3697}
3698/* }}} */
3699
3700void zend_compile_goto(zend_ast *ast) /* {{{ */
3701{
3702    zend_ast *label_ast = ast->child[0];
3703    znode label_node;
3704
3705    zend_compile_expr(&label_node, label_ast);
3706    zend_resolve_goto_label(CG(active_op_array), &label_node, NULL);
3707}
3708/* }}} */
3709
3710void zend_compile_label(zend_ast *ast) /* {{{ */
3711{
3712    zend_string *label = zend_ast_get_str(ast->child[0]);
3713    zend_label dest;
3714
3715    if (!CG(context).labels) {
3716        ALLOC_HASHTABLE(CG(context).labels);
3717        zend_hash_init(CG(context).labels, 8, NULL, ptr_dtor, 0);
3718    }
3719
3720    dest.brk_cont = CG(context).current_brk_cont;
3721    dest.opline_num = get_next_op_number(CG(active_op_array));
3722
3723    if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
3724        zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label));
3725    }
3726}
3727/* }}} */
3728
3729void zend_compile_while(zend_ast *ast) /* {{{ */
3730{
3731    zend_ast *cond_ast = ast->child[0];
3732    zend_ast *stmt_ast = ast->child[1];
3733    znode cond_node;
3734    uint32_t opnum_start, opnum_jmp, opnum_cond;
3735
3736    opnum_jmp = zend_emit_jump(0);
3737
3738    zend_begin_loop(NULL);
3739
3740    opnum_start = get_next_op_number(CG(active_op_array));
3741    zend_compile_stmt(stmt_ast);
3742
3743    opnum_cond = get_next_op_number(CG(active_op_array));
3744    zend_update_jump_target(opnum_jmp, opnum_cond);
3745    zend_compile_expr(&cond_node, cond_ast);
3746
3747    zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
3748
3749    zend_end_loop(opnum_cond);
3750}
3751/* }}} */
3752
3753void zend_compile_do_while(zend_ast *ast) /* {{{ */
3754{
3755    zend_ast *stmt_ast = ast->child[0];
3756    zend_ast *cond_ast = ast->child[1];
3757
3758    znode cond_node;
3759    uint32_t opnum_start, opnum_cond;
3760
3761    zend_begin_loop(NULL);
3762
3763    opnum_start = get_next_op_number(CG(active_op_array));
3764    zend_compile_stmt(stmt_ast);
3765
3766    opnum_cond = get_next_op_number(CG(active_op_array));
3767    zend_compile_expr(&cond_node, cond_ast);
3768
3769    zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
3770
3771    zend_end_loop(opnum_cond);
3772}
3773/* }}} */
3774
3775void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
3776{
3777    zend_ast_list *list;
3778    uint32_t i;
3779
3780    result->op_type = IS_CONST;
3781    ZVAL_TRUE(&result->u.constant);
3782
3783    if (!ast) {
3784        return;
3785    }
3786
3787    list = zend_ast_get_list(ast);
3788    for (i = 0; i < list->children; ++i) {
3789        zend_ast *expr_ast = list->child[i];
3790
3791        zend_do_free(result);
3792        zend_compile_expr(result, expr_ast);
3793    }
3794}
3795/* }}} */
3796
3797void zend_compile_for(zend_ast *ast) /* {{{ */
3798{
3799    zend_ast *init_ast = ast->child[0];
3800    zend_ast *cond_ast = ast->child[1];
3801    zend_ast *loop_ast = ast->child[2];
3802    zend_ast *stmt_ast = ast->child[3];
3803
3804    znode result;
3805    uint32_t opnum_start, opnum_jmp, opnum_loop;
3806
3807    zend_compile_expr_list(&result, init_ast);
3808    zend_do_free(&result);
3809
3810    opnum_jmp = zend_emit_jump(0);
3811
3812    zend_begin_loop(NULL);
3813
3814    opnum_start = get_next_op_number(CG(active_op_array));
3815    zend_compile_stmt(stmt_ast);
3816
3817    opnum_loop = get_next_op_number(CG(active_op_array));
3818    zend_compile_expr_list(&result, loop_ast);
3819    zend_do_free(&result);
3820
3821    zend_update_jump_target_to_next(opnum_jmp);
3822    zend_compile_expr_list(&result, cond_ast);
3823    zend_do_extended_info();
3824
3825    zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
3826
3827    zend_end_loop(opnum_loop);
3828}
3829/* }}} */
3830
3831void zend_compile_foreach(zend_ast *ast) /* {{{ */
3832{
3833    zend_ast *expr_ast = ast->child[0];
3834    zend_ast *value_ast = ast->child[1];
3835    zend_ast *key_ast = ast->child[2];
3836    zend_ast *stmt_ast = ast->child[3];
3837    zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
3838    zend_bool is_variable = zend_is_variable(expr_ast) && !zend_is_call(expr_ast)
3839        && zend_can_write_to_variable(expr_ast);
3840
3841    znode expr_node, reset_node, value_node, key_node;
3842    zend_op *opline;
3843    uint32_t opnum_reset, opnum_fetch;
3844
3845    if (key_ast) {
3846        if (key_ast->kind == ZEND_AST_REF) {
3847            zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
3848        }
3849        if (key_ast->kind == ZEND_AST_LIST) {
3850            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
3851        }
3852    }
3853
3854    if (by_ref) {
3855        value_ast = value_ast->child[0];
3856    }
3857
3858    if (by_ref && is_variable) {
3859        zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
3860    } else {
3861        zend_compile_expr(&expr_node, expr_ast);
3862    }
3863
3864    if (by_ref) {
3865        zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
3866    }
3867
3868    opnum_reset = get_next_op_number(CG(active_op_array));
3869    opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
3870
3871    opnum_fetch = get_next_op_number(CG(active_op_array));
3872    opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
3873
3874    if (value_ast->kind == ZEND_AST_VAR &&
3875        zend_try_compile_cv(&value_node, value_ast) == SUCCESS) {
3876        SET_NODE(opline->op2, &value_node);
3877    } else {
3878        opline->op2_type = IS_VAR;
3879        opline->op2.var = get_temporary_variable(CG(active_op_array));
3880        GET_NODE(&value_node, opline->op2);
3881        if (by_ref) {
3882            zend_emit_assign_ref_znode(value_ast, &value_node);
3883        } else {
3884            zend_emit_assign_znode(value_ast, &value_node);
3885        }
3886    }
3887
3888    if (key_ast) {
3889        opline = &CG(active_op_array)->opcodes[opnum_fetch];
3890        zend_make_tmp_result(&key_node, opline);
3891        zend_emit_assign_znode(key_ast, &key_node);
3892    }
3893
3894    reset_node.flag = 1; /* generate FE_FREE */
3895    zend_begin_loop(&reset_node);
3896
3897    zend_compile_stmt(stmt_ast);
3898
3899    zend_emit_jump(opnum_fetch);
3900
3901    opline = &CG(active_op_array)->opcodes[opnum_reset];
3902    opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3903
3904    opline = &CG(active_op_array)->opcodes[opnum_fetch];
3905    opline->extended_value = get_next_op_number(CG(active_op_array));
3906
3907    zend_end_loop(opnum_fetch);
3908
3909    generate_free_loop_var(&reset_node);
3910}
3911/* }}} */
3912
3913void zend_compile_if(zend_ast *ast) /* {{{ */
3914{
3915    zend_ast_list *list = zend_ast_get_list(ast);
3916    uint32_t i;
3917    uint32_t *jmp_opnums = NULL;
3918
3919    if (list->children > 1) {
3920        jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
3921    }
3922
3923    for (i = 0; i < list->children; ++i) {
3924        zend_ast *elem_ast = list->child[i];
3925        zend_ast *cond_ast = elem_ast->child[0];
3926        zend_ast *stmt_ast = elem_ast->child[1];
3927
3928        znode cond_node;
3929        uint32_t opnum_jmpz;
3930        if (cond_ast) {
3931            zend_compile_expr(&cond_node, cond_ast);
3932            opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
3933        }
3934
3935        zend_compile_stmt(stmt_ast);
3936
3937        if (i != list->children - 1) {
3938            jmp_opnums[i] = zend_emit_jump(0);
3939        }
3940
3941        if (cond_ast) {
3942            zend_update_jump_target_to_next(opnum_jmpz);
3943        }
3944    }
3945
3946    if (list->children > 1) {
3947        for (i = 0; i < list->children - 1; ++i) {
3948            zend_update_jump_target_to_next(jmp_opnums[i]);
3949        }
3950        efree(jmp_opnums);
3951    }
3952}
3953/* }}} */
3954
3955void zend_compile_switch(zend_ast *ast) /* {{{ */
3956{
3957    zend_ast *expr_ast = ast->child[0];
3958    zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
3959
3960    uint32_t i;
3961    zend_bool has_default_case = 0;
3962
3963    znode expr_node, case_node;
3964    zend_op *opline;
3965    uint32_t *jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
3966    uint32_t opnum_default_jmp;
3967
3968    zend_compile_expr(&expr_node, expr_ast);
3969
3970    expr_node.flag = 0; /* Generate normal FREE */
3971    zend_begin_loop(&expr_node);
3972
3973    case_node.op_type = IS_TMP_VAR;
3974    case_node.u.op.var = get_temporary_variable(CG(active_op_array));
3975
3976    for (i = 0; i < cases->children; ++i) {
3977        zend_ast *case_ast = cases->child[i];
3978        zend_ast *cond_ast = case_ast->child[0];
3979        znode cond_node;
3980
3981        if (!cond_ast) {
3982            if (has_default_case) {
3983                CG(zend_lineno) = case_ast->lineno;
3984                zend_error_noreturn(E_COMPILE_ERROR,
3985                    "Switch statements may only contain one default clause");
3986            }
3987            has_default_case = 1;
3988            continue;
3989        }
3990
3991        zend_compile_expr(&cond_node, cond_ast);
3992
3993        if (expr_node.op_type == IS_CONST
3994            && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
3995            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
3996        } else if (expr_node.op_type == IS_CONST
3997            && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
3998            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
3999        } else {
4000            opline = zend_emit_op(NULL, ZEND_CASE, &expr_node, &cond_node);
4001            SET_NODE(opline->result, &case_node);
4002            if (opline->op1_type == IS_CONST) {
4003                zval_copy_ctor(CT_CONSTANT(opline->op1));
4004            }
4005
4006            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
4007        }
4008    }
4009
4010    opnum_default_jmp = zend_emit_jump(0);
4011
4012    for (i = 0; i < cases->children; ++i) {
4013        zend_ast *case_ast = cases->child[i];
4014        zend_ast *cond_ast = case_ast->child[0];
4015        zend_ast *stmt_ast = case_ast->child[1];
4016
4017        if (cond_ast) {
4018            zend_update_jump_target_to_next(jmpnz_opnums[i]);
4019        } else {
4020            zend_update_jump_target_to_next(opnum_default_jmp);
4021        }
4022
4023        zend_compile_stmt(stmt_ast);
4024    }
4025
4026    if (!has_default_case) {
4027        zend_update_jump_target_to_next(opnum_default_jmp);
4028    }
4029
4030    zend_end_loop(get_next_op_number(CG(active_op_array)));
4031
4032    if (expr_node.op_type == IS_VAR || expr_node.op_type == IS_TMP_VAR) {
4033        zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL);
4034    } else if (expr_node.op_type == IS_CONST) {
4035        zval_dtor(&expr_node.u.constant);
4036    }
4037
4038    efree(jmpnz_opnums);
4039}
4040/* }}} */
4041
4042void zend_compile_try(zend_ast *ast) /* {{{ */
4043{
4044    zend_ast *try_ast = ast->child[0];
4045    zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
4046    zend_ast *finally_ast = ast->child[2];
4047
4048    uint32_t i;
4049    zend_op *opline;
4050    uint32_t try_catch_offset = zend_add_try_element(
4051        get_next_op_number(CG(active_op_array)));
4052    uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
4053
4054    if (catches->children == 0 && !finally_ast) {
4055        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
4056    }
4057
4058    zend_compile_stmt(try_ast);
4059
4060    if (catches->children != 0) {
4061        jmp_opnums[0] = zend_emit_jump(0);
4062    }
4063
4064    for (i = 0; i < catches->children; ++i) {
4065        zend_ast *catch_ast = catches->child[i];
4066        zend_ast *class_ast = catch_ast->child[0];
4067        zend_ast *var_ast = catch_ast->child[1];
4068        zend_ast *stmt_ast = catch_ast->child[2];
4069        zval *var_name = zend_ast_get_zval(var_ast);
4070        zend_bool is_last_catch = (i + 1 == catches->children);
4071
4072        uint32_t opnum_catch;
4073
4074        if (!zend_is_const_default_class_ref(class_ast)) {
4075            zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
4076        }
4077
4078        opnum_catch = get_next_op_number(CG(active_op_array));
4079        if (i == 0) {
4080            CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
4081        }
4082
4083        CG(zend_lineno) = catch_ast->lineno;
4084
4085        opline = get_next_op(CG(active_op_array));
4086        opline->opcode = ZEND_CATCH;
4087        opline->op1_type = IS_CONST;
4088        opline->op1.constant = zend_add_class_name_literal(CG(active_op_array),
4089            zend_resolve_class_name_ast(class_ast));
4090
4091        opline->op2_type = IS_CV;
4092        opline->op2.var = lookup_cv(CG(active_op_array), zend_string_copy(Z_STR_P(var_name)));
4093        opline->result.num = is_last_catch;
4094
4095        zend_compile_stmt(stmt_ast);
4096
4097        if (!is_last_catch) {
4098            jmp_opnums[i + 1] = zend_emit_jump(0);
4099        }
4100
4101        opline = &CG(active_op_array)->opcodes[opnum_catch];
4102        opline->extended_value = get_next_op_number(CG(active_op_array));
4103    }
4104
4105    for (i = 0; i < catches->children; ++i) {
4106        zend_update_jump_target_to_next(jmp_opnums[i]);
4107    }
4108
4109    if (finally_ast) {
4110        uint32_t opnum_jmp = get_next_op_number(CG(active_op_array)) + 1;
4111
4112        if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
4113            CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
4114            CG(context).fast_call_var = get_temporary_variable(CG(active_op_array));
4115        }
4116
4117        opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
4118        opline->op1.opline_num = opnum_jmp + 1;
4119        opline->result_type = IS_TMP_VAR;
4120        opline->result.var = CG(context).fast_call_var;
4121
4122        zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
4123
4124        CG(context).in_finally++;
4125        zend_compile_stmt(finally_ast);
4126        CG(context).in_finally--;
4127
4128        CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
4129        CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
4130            = get_next_op_number(CG(active_op_array));
4131
4132        opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
4133        opline->op1_type = IS_TMP_VAR;
4134        opline->op1.var = CG(context).fast_call_var;
4135
4136        zend_update_jump_target_to_next(opnum_jmp);
4137    }
4138
4139    efree(jmp_opnums);
4140}
4141/* }}} */
4142
4143/* Encoding declarations must already be handled during parsing */
4144void zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
4145{
4146    zend_ast_list *declares = zend_ast_get_list(ast);
4147    uint32_t i;
4148    for (i = 0; i < declares->children; ++i) {
4149        zend_ast *declare_ast = declares->child[i];
4150        zend_ast *name_ast = declare_ast->child[0];
4151        zend_ast *value_ast = declare_ast->child[1];
4152        zend_string *name = zend_ast_get_str(name_ast);
4153
4154        if (zend_string_equals_literal_ci(name, "encoding")) {
4155            if (value_ast->kind != ZEND_AST_ZVAL) {
4156                zend_error_noreturn(E_COMPILE_ERROR, "Encoding must be a literal");
4157            }
4158
4159            if (CG(multibyte)) {
4160                zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
4161
4162                const zend_encoding *new_encoding, *old_encoding;
4163                zend_encoding_filter old_input_filter;
4164
4165                CG(encoding_declared) = 1;
4166
4167                new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name));
4168                if (!new_encoding) {
4169                    zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name));
4170                } else {
4171                    old_input_filter = LANG_SCNG(input_filter);
4172                    old_encoding = LANG_SCNG(script_encoding);
4173                    zend_multibyte_set_filter(new_encoding);
4174
4175                    /* need to re-scan if input filter changed */
4176                    if (old_input_filter != LANG_SCNG(input_filter) ||
4177                         (old_input_filter && new_encoding != old_encoding)) {
4178                        zend_multibyte_yyinput_again(old_input_filter, old_encoding);
4179                    }
4180                }
4181
4182                zend_string_release(encoding_name);
4183            } else {
4184                zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
4185                    "Zend multibyte feature is turned off by settings");
4186            }
4187        }
4188    }
4189}
4190/* }}} */
4191
4192static int zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
4193{
4194    uint32_t i = 0;
4195    zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
4196
4197    /* Check to see if this declare is preceeded only by declare statements */
4198    while (i < file_ast->children) {
4199        if (file_ast->child[i] == ast) {
4200            return SUCCESS;
4201        } else if (file_ast->child[i] == NULL) {
4202            /* Empty statements are not allowed prior to a declare */
4203            return FAILURE;
4204        } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
4205            /* declares can only be preceeded by other declares */
4206            return FAILURE;
4207        }
4208        i++;
4209    }
4210    return FAILURE;
4211}
4212/* }}} */
4213
4214void zend_compile_declare(zend_ast *ast) /* {{{ */
4215{
4216    zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
4217    zend_ast *stmt_ast = ast->child[1];
4218    zend_declarables orig_declarables = FC(declarables);
4219    uint32_t i;
4220
4221    for (i = 0; i < declares->children; ++i) {
4222        zend_ast *declare_ast = declares->child[i];
4223        zend_ast *name_ast = declare_ast->child[0];
4224        zend_ast *value_ast = declare_ast->child[1];
4225
4226        zend_string *name = zend_ast_get_str(name_ast);
4227        if (zend_string_equals_literal_ci(name, "ticks")) {
4228            zval value_zv;
4229            zend_const_expr_to_zval(&value_zv, value_ast);
4230            FC(declarables).ticks = zval_get_long(&value_zv);
4231            zval_dtor(&value_zv);
4232        } else if (zend_string_equals_literal_ci(name, "encoding")) {
4233
4234            if (FAILURE == zend_declare_is_first_statement(ast)) {
4235                zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
4236                    "the very first statement in the script");
4237            }
4238        } else if (zend_string_equals_literal_ci(name, "strict_types")) {
4239            zval value_zv;
4240
4241            if (FAILURE == zend_declare_is_first_statement(ast)) {
4242                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
4243                    "the very first statement in the script");
4244            }
4245
4246            if (ast->child[1] != NULL) {
4247                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
4248                    "use block mode");
4249            }
4250
4251            zend_const_expr_to_zval(&value_zv, value_ast);
4252
4253            if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
4254                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
4255            }
4256
4257            if (Z_LVAL(value_zv) == 1) {
4258                CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
4259            }
4260
4261        } else {
4262            zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name));
4263        }
4264    }
4265
4266    if (stmt_ast) {
4267        zend_compile_stmt(stmt_ast);
4268
4269        FC(declarables) = orig_declarables;
4270    }
4271}
4272/* }}} */
4273
4274void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
4275{
4276    zend_ast_list *list = zend_ast_get_list(ast);
4277    uint32_t i;
4278    for (i = 0; i < list->children; ++i) {
4279        zend_compile_stmt(list->child[i]);
4280    }
4281}
4282/* }}} */
4283
4284ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */
4285{
4286    uint32_t i, n;
4287
4288    func->common.arg_flags[0] = 0;
4289    func->common.arg_flags[1] = 0;
4290    func->common.arg_flags[2] = 0;
4291    if (func->common.arg_info) {
4292        n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM);
4293        i = 0;
4294        while (i < n) {
4295            ZEND_SET_ARG_FLAG(func, i + 1, func->common.arg_info[i].pass_by_reference);
4296            i++;
4297        }
4298        if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_VARIADIC && func->common.arg_info[i].pass_by_reference)) {
4299            uint32_t pass_by_reference = func->common.arg_info[i].pass_by_reference;
4300            while (i < MAX_ARG_FLAG_NUM) {
4301                ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference);
4302                i++;
4303            }
4304        }
4305    }
4306}
4307/* }}} */
4308
4309static void zend_compile_typename(zend_ast *ast, zend_arg_info *arg_info) /* {{{ */
4310{
4311    if (ast->kind == ZEND_AST_TYPE) {
4312        arg_info->type_hint = ast->attr;
4313    } else {
4314        zend_string *class_name = zend_ast_get_str(ast);
4315        zend_uchar type = zend_lookup_builtin_type_by_name(class_name);
4316
4317        if (type != 0) {
4318            arg_info->type_hint = type;
4319        } else {
4320            uint32_t fetch_type = zend_get_class_fetch_type_ast(ast);
4321            if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) {
4322                class_name = zend_resolve_class_name_ast(ast);
4323                zend_assert_valid_class_name(class_name);
4324            } else {
4325                zend_ensure_valid_class_fetch_type(fetch_type);
4326                zend_string_addref(class_name);
4327            }
4328
4329            arg_info->type_hint = IS_OBJECT;
4330            arg_info->class_name = class_name;
4331        }
4332    }
4333}
4334/* }}} */
4335
4336void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */
4337{
4338    zend_ast_list *list = zend_ast_get_list(ast);
4339    uint32_t i;
4340    zend_op_array *op_array = CG(active_op_array);
4341    zend_arg_info *arg_infos;
4342
4343    if (return_type_ast) {
4344        /* Use op_array->arg_info[-1] for return type hinting */
4345        arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
4346        arg_infos->name = NULL;
4347        arg_infos->pass_by_reference = (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
4348        arg_infos->is_variadic = 0;
4349        arg_infos->type_hint = 0;
4350        arg_infos->allow_null = 0;
4351        arg_infos->class_name = NULL;
4352
4353        zend_compile_typename(return_type_ast, arg_infos);
4354
4355        arg_infos++;
4356        op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
4357    } else {
4358        if (list->children == 0) {
4359            return;
4360        }
4361        arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
4362    }
4363
4364    for (i = 0; i < list->children; ++i) {
4365        zend_ast *param_ast = list->child[i];
4366        zend_ast *type_ast = param_ast->child[0];
4367        zend_ast *var_ast = param_ast->child[1];
4368        zend_ast *default_ast = param_ast->child[2];
4369        zend_string *name = zend_ast_get_str(var_ast);
4370        zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
4371        zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
4372
4373        znode var_node, default_node;
4374        zend_uchar opcode;
4375        zend_op *opline;
4376        zend_arg_info *arg_info;
4377
4378        if (zend_is_auto_global(name)) {
4379            zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
4380                ZSTR_VAL(name));
4381        }
4382
4383        var_node.op_type = IS_CV;
4384        var_node.u.op.var = lookup_cv(CG(active_op_array), zend_string_copy(name));
4385
4386        if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
4387            zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
4388                ZSTR_VAL(name));
4389        } else if (zend_string_equals_literal(name, "this")) {
4390            if (op_array->scope && (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
4391                zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
4392            }
4393            op_array->this_var = var_node.u.op.var;
4394        }
4395
4396        if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
4397            zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
4398        }
4399
4400        if (is_variadic) {
4401            opcode = ZEND_RECV_VARIADIC;
4402            default_node.op_type = IS_UNUSED;
4403            op_array->fn_flags |= ZEND_ACC_VARIADIC;
4404
4405            if (default_ast) {
4406                zend_error_noreturn(E_COMPILE_ERROR,
4407                    "Variadic parameter cannot have a default value");
4408            }
4409        } else if (default_ast) {
4410            /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
4411            uint32_t cops = CG(compiler_options);
4412            CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
4413            opcode = ZEND_RECV_INIT;
4414            default_node.op_type = IS_CONST;
4415            zend_const_expr_to_zval(&default_node.u.constant, default_ast);
4416            CG(compiler_options) = cops;
4417        } else {
4418            opcode = ZEND_RECV;
4419            default_node.op_type = IS_UNUSED;
4420            op_array->required_num_args = i + 1;
4421        }
4422
4423        opline = zend_emit_op(NULL, opcode, NULL, &default_node);
4424        SET_NODE(opline->result, &var_node);
4425        opline->op1.num = i + 1;
4426
4427        arg_info = &arg_infos[i];
4428        arg_info->name = zend_string_copy(name);
4429        arg_info->pass_by_reference = is_ref;
4430        arg_info->is_variadic = is_variadic;
4431        arg_info->type_hint = 0;
4432        arg_info->allow_null = 1;
4433        arg_info->class_name = NULL;
4434
4435        if (type_ast) {
4436            zend_bool has_null_default = default_ast
4437                && (Z_TYPE(default_node.u.constant) == IS_NULL
4438                    || (Z_TYPE(default_node.u.constant) == IS_CONSTANT
4439                        && strcasecmp(Z_STRVAL(default_node.u.constant), "NULL") == 0));
4440
4441            op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4442            arg_info->allow_null = has_null_default;
4443
4444            zend_compile_typename(type_ast, arg_info);
4445
4446            if (type_ast->kind == ZEND_AST_TYPE) {
4447                if (arg_info->type_hint == IS_ARRAY) {
4448                    if (default_ast && !has_null_default
4449                        && Z_TYPE(default_node.u.constant) != IS_ARRAY
4450                        && !Z_CONSTANT(default_node.u.constant)
4451                    ) {
4452                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4453                            "with array type hint can only be an array or NULL");
4454                    }
4455                } else if (arg_info->type_hint == IS_CALLABLE && default_ast) {
4456                    if (!has_null_default && !Z_CONSTANT(default_node.u.constant)) {
4457                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4458                            "with callable type hint can only be NULL");
4459                    }
4460                }
4461            } else {
4462                if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) {
4463                    if (arg_info->class_name) {
4464                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4465                            "with a class type hint can only be NULL");
4466                    } else switch (arg_info->type_hint) {
4467                        case IS_DOUBLE:
4468                            if (Z_TYPE(default_node.u.constant) != IS_DOUBLE && Z_TYPE(default_node.u.constant) != IS_LONG) {
4469                                zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4470                                    "with a float type hint can only be float, integer, or NULL");
4471                            }
4472                            break;
4473
4474                        default:
4475                            if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) {
4476                                zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4477                                    "with a %s type hint can only be %s or NULL",
4478                                    zend_get_type_by_const(arg_info->type_hint), zend_get_type_by_const(arg_info->type_hint));
4479                            }
4480                            break;
4481                    }
4482                }
4483            }
4484
4485            /* Allocate cache slot to speed-up run-time class resolution */
4486            if (opline->opcode == ZEND_RECV_INIT) {
4487                if (arg_info->class_name) {
4488                    zend_alloc_cache_slot(opline->op2.constant);
4489                } else {
4490                    Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = -1;
4491                }
4492            } else {
4493                if (arg_info->class_name) {
4494                    opline->op2.num = op_array->cache_size;
4495                    op_array->cache_size += sizeof(void*);
4496                } else {
4497                    opline->op2.num = -1;
4498                }
4499            }
4500        } else {
4501            if (opline->opcode == ZEND_RECV_INIT) {
4502                Z_CACHE_SLOT(op_array->literals[opline->op2.constant]) = -1;
4503            } else {
4504                opline->op2.num = -1;
4505            }
4506        }
4507    }
4508
4509    /* These are assigned at the end to avoid unitialized memory in case of an error */
4510    op_array->num_args = list->children;
4511    op_array->arg_info = arg_infos;
4512
4513    /* Don't count the variadic argument */
4514    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
4515        op_array->num_args--;
4516    }
4517    zend_set_function_arg_flags((zend_function*)op_array);
4518}
4519/* }}} */
4520
4521void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
4522{
4523    zend_ast_list *list = zend_ast_get_list(ast);
4524    uint32_t i;
4525
4526    for (i = 0; i < list->children; ++i) {
4527        zend_ast *var_ast = list->child[i];
4528        zend_string *name = zend_ast_get_str(var_ast);
4529        zend_bool by_ref = var_ast->attr;
4530        zval zv;
4531
4532        if (zend_string_equals_literal(name, "this")) {
4533            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
4534        }
4535
4536        ZVAL_NULL(&zv);
4537        Z_CONST_FLAGS(zv) = by_ref ? IS_LEXICAL_REF : IS_LEXICAL_VAR;
4538
4539        zend_compile_static_var_common(var_ast, &zv, by_ref);
4540    }
4541}
4542/* }}} */
4543
4544void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
4545{
4546    zend_class_entry *ce = CG(active_class_entry);
4547    zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
4548    zend_bool in_trait = (ce->ce_flags & ZEND_ACC_TRAIT) != 0;
4549    zend_bool is_public = (op_array->fn_flags & ZEND_ACC_PUBLIC) != 0;
4550    zend_bool is_static = (op_array->fn_flags & ZEND_ACC_STATIC) != 0;
4551
4552    zend_string *lcname;
4553
4554    if (in_interface) {
4555        if ((op_array->fn_flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PUBLIC) {
4556            zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
4557                "%s::%s() must be omitted", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4558        }
4559        op_array->fn_flags |= ZEND_ACC_ABSTRACT;
4560    }
4561
4562    if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
4563        if (op_array->fn_flags & ZEND_ACC_PRIVATE) {
4564            zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
4565                in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4566        }
4567
4568        if (has_body) {
4569            zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
4570                in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name));
4571        }
4572
4573        ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
4574    } else if (!has_body) {
4575        zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
4576            ZSTR_VAL(ce->name), ZSTR_VAL(name));
4577    }
4578
4579    op_array->scope = ce;
4580    op_array->function_name = zend_string_copy(name);
4581
4582    lcname = zend_string_tolower(name);
4583    lcname = zend_new_interned_string(lcname);
4584
4585    if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
4586        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
4587            ZSTR_VAL(ce->name), ZSTR_VAL(name));
4588    }
4589
4590    if (in_interface) {
4591        if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
4592            if (!is_public || is_static) {
4593                zend_error(E_WARNING, "The magic method __call() must have "
4594                    "public visibility and cannot be static");
4595            }
4596        } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
4597            if (!is_public || !is_static) {
4598                zend_error(E_WARNING, "The magic method __callStatic() must have "
4599                    "public visibility and be static");
4600            }
4601        } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
4602            if (!is_public || is_static) {
4603                zend_error(E_WARNING, "The magic method __get() must have "
4604                    "public visibility and cannot be static");
4605            }
4606        } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
4607            if (!is_public || is_static) {
4608                zend_error(E_WARNING, "The magic method __set() must have "
4609                    "public visibility and cannot be static");
4610            }
4611        } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
4612            if (!is_public || is_static) {
4613                zend_error(E_WARNING, "The magic method __unset() must have "
4614                    "public visibility and cannot be static");
4615            }
4616        } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
4617            if (!is_public || is_static) {
4618                zend_error(E_WARNING, "The magic method __isset() must have "
4619                    "public visibility and cannot be static");
4620            }
4621        } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
4622            if (!is_public || is_static) {
4623                zend_error(E_WARNING, "The magic method __toString() must have "
4624                    "public visibility and cannot be static");
4625            }
4626        } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
4627            if (!is_public || is_static) {
4628                zend_error(E_WARNING, "The magic method __invoke() must have "
4629                    "public visibility and cannot be static");
4630            }
4631        } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
4632            if (!is_public || is_static) {
4633                zend_error(E_WARNING, "The magic method __debugInfo() must have "
4634                    "public visibility and cannot be static");
4635            }
4636        }
4637    } else {
4638        if (!in_trait && zend_string_equals_ci(lcname, ce->name)) {
4639            if (!ce->constructor) {
4640                ce->constructor = (zend_function *) op_array;
4641            }
4642        } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
4643            ce->constructor = (zend_function *) op_array;
4644        } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
4645            ce->destructor = (zend_function *) op_array;
4646        } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
4647            ce->clone = (zend_function *) op_array;
4648        } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
4649            if (!is_public || is_static) {
4650                zend_error(E_WARNING, "The magic method __call() must have "
4651                    "public visibility and cannot be static");
4652            }
4653            ce->__call = (zend_function *) op_array;
4654        } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
4655            if (!is_public || !is_static) {
4656                zend_error(E_WARNING, "The magic method __callStatic() must have "
4657                    "public visibility and be static");
4658            }
4659            ce->__callstatic = (zend_function *) op_array;
4660        } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
4661            if (!is_public || is_static) {
4662                zend_error(E_WARNING, "The magic method __get() must have "
4663                    "public visibility and cannot be static");
4664            }
4665            ce->__get = (zend_function *) op_array;
4666            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4667        } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
4668            if (!is_public || is_static) {
4669                zend_error(E_WARNING, "The magic method __set() must have "
4670                    "public visibility and cannot be static");
4671            }
4672            ce->__set = (zend_function *) op_array;
4673            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4674        } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
4675            if (!is_public || is_static) {
4676                zend_error(E_WARNING, "The magic method __unset() must have "
4677                    "public visibility and cannot be static");
4678            }
4679            ce->__unset = (zend_function *) op_array;
4680            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4681        } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
4682            if (!is_public || is_static) {
4683                zend_error(E_WARNING, "The magic method __isset() must have "
4684                    "public visibility and cannot be static");
4685            }
4686            ce->__isset = (zend_function *) op_array;
4687            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4688        } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
4689            if (!is_public || is_static) {
4690                zend_error(E_WARNING, "The magic method __toString() must have "
4691                    "public visibility and cannot be static");
4692            }
4693            ce->__tostring = (zend_function *) op_array;
4694        } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
4695            if (!is_public || is_static) {
4696                zend_error(E_WARNING, "The magic method __invoke() must have "
4697                    "public visibility and cannot be static");
4698            }
4699        } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
4700            if (!is_public || is_static) {
4701                zend_error(E_WARNING, "The magic method __debugInfo() must have "
4702                    "public visibility and cannot be static");
4703            }
4704            ce->__debugInfo = (zend_function *) op_array;
4705        } else if (!is_static) {
4706            op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;
4707        }
4708    }
4709
4710    zend_string_release(lcname);
4711}
4712/* }}} */
4713
4714static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl) /* {{{ */
4715{
4716    zend_ast *params_ast = decl->child[0];
4717    zend_string *name = decl->name, *lcname;
4718    zend_op *opline;
4719
4720    op_array->function_name = name = zend_prefix_with_ns(name);
4721
4722    lcname = zend_string_tolower(name);
4723
4724    if (FC(imports_function)) {
4725        zend_string *import_name = zend_hash_find_ptr(FC(imports_function), lcname);
4726        if (import_name && !zend_string_equals_ci(lcname, import_name)) {
4727            zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare function %s "
4728                "because the name is already in use", ZSTR_VAL(name));
4729        }
4730    }
4731
4732    if (zend_string_equals_literal(lcname, ZEND_AUTOLOAD_FUNC_NAME)
4733        && zend_ast_get_list(params_ast)->children != 1
4734    ) {
4735        zend_error_noreturn(E_COMPILE_ERROR, "%s() must take exactly 1 argument",
4736            ZEND_AUTOLOAD_FUNC_NAME);
4737    }
4738
4739    if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
4740        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
4741    } else {
4742        opline = get_next_op(CG(active_op_array));
4743        opline->opcode = ZEND_DECLARE_FUNCTION;
4744        opline->op2_type = IS_CONST;
4745        LITERAL_STR(opline->op2, zend_string_copy(lcname));
4746    }
4747
4748    {
4749        zend_string *key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
4750
4751        opline->op1_type = IS_CONST;
4752        LITERAL_STR(opline->op1, key);
4753
4754        zend_hash_update_ptr(CG(function_table), key, op_array);
4755    }
4756
4757    zend_string_release(lcname);
4758}
4759/* }}} */
4760
4761void zend_compile_func_decl(znode *result, zend_ast *ast) /* {{{ */
4762{
4763    zend_ast_decl *decl = (zend_ast_decl *) ast;
4764    zend_ast *params_ast = decl->child[0];
4765    zend_ast *uses_ast = decl->child[1];
4766    zend_ast *stmt_ast = decl->child[2];
4767    zend_ast *return_type_ast = decl->child[3];
4768    zend_bool is_method = decl->kind == ZEND_AST_METHOD;
4769
4770    zend_op_array *orig_op_array = CG(active_op_array);
4771    zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
4772    zend_oparray_context orig_oparray_context;
4773
4774    init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
4775
4776    op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
4777    op_array->fn_flags |= decl->flags;
4778    op_array->line_start = decl->start_lineno;
4779    op_array->line_end = decl->end_lineno;
4780    if (decl->doc_comment) {
4781        op_array->doc_comment = zend_string_copy(decl->doc_comment);
4782    }
4783    if (decl->kind == ZEND_AST_CLOSURE) {
4784        op_array->fn_flags |= ZEND_ACC_CLOSURE;
4785    }
4786
4787    if (is_method) {
4788        zend_bool has_body = stmt_ast != NULL;
4789        zend_begin_method_decl(op_array, decl->name, has_body);
4790    } else {
4791        zend_begin_func_decl(result, op_array, decl);
4792    }
4793
4794    CG(active_op_array) = op_array;
4795    zend_oparray_context_begin(&orig_oparray_context);
4796
4797    if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
4798        zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
4799        opline_ext->lineno = decl->start_lineno;
4800    }
4801
4802    {
4803        /* Push a separator to the loop variable stack */
4804        znode dummy_var;
4805        dummy_var.op_type = IS_UNUSED;
4806
4807        zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
4808    }
4809
4810    zend_compile_params(params_ast, return_type_ast);
4811    if (uses_ast) {
4812        zend_compile_closure_uses(uses_ast);
4813    }
4814    zend_compile_stmt(stmt_ast);
4815
4816    if (is_method) {
4817        zend_check_magic_method_implementation(
4818            CG(active_class_entry), (zend_function *) op_array, E_COMPILE_ERROR);
4819    }
4820
4821    zend_do_extended_info();
4822    zend_emit_final_return(NULL);
4823
4824    pass_two(CG(active_op_array));
4825    zend_oparray_context_end(&orig_oparray_context);
4826
4827    /* Pop the loop variable stack separator */
4828    zend_stack_del_top(&CG(loop_var_stack));
4829
4830    CG(active_op_array) = orig_op_array;
4831}
4832/* }}} */
4833
4834void zend_compile_prop_decl(zend_ast *ast) /* {{{ */
4835{
4836    zend_ast_list *list = zend_ast_get_list(ast);
4837    uint32_t flags = list->attr;
4838    zend_class_entry *ce = CG(active_class_entry);
4839    uint32_t i, children = list->children;
4840    zend_string *doc_comment = NULL;
4841
4842    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4843        zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
4844    }
4845
4846    if (flags & ZEND_ACC_ABSTRACT) {
4847        zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
4848    }
4849
4850    /* Doc comment has been appended as last element in property list */
4851    if (list->child[children - 1]->kind == ZEND_AST_ZVAL) {
4852        doc_comment = zend_string_copy(zend_ast_get_str(list->child[children - 1]));
4853        children -= 1;
4854    }
4855
4856    for (i = 0; i < children; ++i) {
4857        zend_ast *prop_ast = list->child[i];
4858        zend_ast *name_ast = prop_ast->child[0];
4859        zend_ast *value_ast = prop_ast->child[1];
4860        zend_string *name = zend_ast_get_str(name_ast);
4861        zval value_zv;
4862
4863        if (flags & ZEND_ACC_FINAL) {
4864            zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
4865                "the final modifier is allowed only for methods and classes",
4866                ZSTR_VAL(ce->name), ZSTR_VAL(name));
4867        }
4868
4869        if (zend_hash_exists(&ce->properties_info, name)) {
4870            zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
4871                ZSTR_VAL(ce->name), ZSTR_VAL(name));
4872        }
4873
4874        if (value_ast) {
4875            zend_const_expr_to_zval(&value_zv, value_ast);
4876        } else {
4877            ZVAL_NULL(&value_zv);
4878        }
4879
4880        name = zend_new_interned_string_safe(name);
4881        zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment);
4882
4883        /* Doc comment is only assigned to first property */
4884        doc_comment = NULL;
4885    }
4886}
4887/* }}} */
4888
4889void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
4890{
4891    zend_ast_list *list = zend_ast_get_list(ast);
4892    zend_class_entry *ce = CG(active_class_entry);
4893    uint32_t i;
4894
4895    if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
4896        zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
4897        return;
4898    }
4899
4900    for (i = 0; i < list->children; ++i) {
4901        zend_ast *const_ast = list->child[i];
4902        zend_ast *name_ast = const_ast->child[0];
4903        zend_ast *value_ast = const_ast->child[1];
4904        zend_string *name = zend_ast_get_str(name_ast);
4905        zval value_zv;
4906
4907        if (zend_string_equals_literal_ci(name, "class")) {
4908            zend_error(E_COMPILE_ERROR,
4909                "A class constant must not be called 'class'; it is reserved for class name fetching");
4910        }
4911
4912        zend_const_expr_to_zval(&value_zv, value_ast);
4913
4914        name = zend_new_interned_string_safe(name);
4915        if (zend_hash_add(&ce->constants_table, name, &value_zv) == NULL) {
4916            zend_error_noreturn(E_COMPILE_ERROR, "Cannot redefine class constant %s::%s",
4917                ZSTR_VAL(ce->name), ZSTR_VAL(name));
4918        }
4919
4920        if (Z_CONSTANT(value_zv)) {
4921            ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4922        }
4923    }
4924}
4925/* }}} */
4926
4927static zend_trait_method_reference *zend_compile_method_ref(zend_ast *ast) /* {{{ */
4928{
4929    zend_ast *class_ast = ast->child[0];
4930    zend_ast *method_ast = ast->child[1];
4931
4932    zend_trait_method_reference *method_ref = emalloc(sizeof(zend_trait_method_reference));
4933    method_ref->ce = NULL;
4934    method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
4935
4936    if (class_ast) {
4937        method_ref->class_name = zend_resolve_class_name_ast(class_ast);
4938    } else {
4939        method_ref->class_name = NULL;
4940    }
4941
4942    return method_ref;
4943}
4944/* }}} */
4945
4946static zend_string **zend_compile_name_list(zend_ast *ast) /* {{{ */
4947{
4948    zend_ast_list *list = zend_ast_get_list(ast);
4949    zend_string **names = safe_emalloc(sizeof(zend_string *), list->children + 1, 0);
4950    uint32_t i;
4951
4952    for (i = 0; i < list->children; ++i) {
4953        zend_ast *name_ast = list->child[i];
4954        names[i] = zend_resolve_class_name_ast(name_ast);
4955    }
4956
4957    names[list->children] = NULL;
4958
4959    return names;
4960}
4961/* }}} */
4962
4963static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
4964{
4965    zend_ast *method_ref_ast = ast->child[0];
4966    zend_ast *insteadof_ast = ast->child[1];
4967
4968    zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence));
4969    precedence->trait_method = zend_compile_method_ref(method_ref_ast);
4970    precedence->exclude_from_classes
4971        = (void *) zend_compile_name_list(insteadof_ast);
4972
4973    zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
4974}
4975/* }}} */
4976
4977static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
4978{
4979    zend_ast *method_ref_ast = ast->child[0];
4980    zend_ast *alias_ast = ast->child[1];
4981    uint32_t modifiers = ast->attr;
4982
4983    zend_trait_alias *alias;
4984
4985    if (modifiers == ZEND_ACC_STATIC) {
4986        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as method modifier");
4987    } else if (modifiers == ZEND_ACC_ABSTRACT) {
4988        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as method modifier");
4989    } else if (modifiers == ZEND_ACC_FINAL) {
4990        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as method modifier");
4991    }
4992
4993    alias = emalloc(sizeof(zend_trait_alias));
4994    alias->trait_method = zend_compile_method_ref(method_ref_ast);
4995    alias->modifiers = modifiers;
4996
4997    if (alias_ast) {
4998        alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
4999    } else {
5000        alias->alias = NULL;
5001    }
5002
5003    zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
5004}
5005/* }}} */
5006
5007void zend_compile_use_trait(zend_ast *ast) /* {{{ */
5008{
5009    zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
5010    zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
5011    zend_class_entry *ce = CG(active_class_entry);
5012    zend_op *opline;
5013    uint32_t i;
5014
5015    for (i = 0; i < traits->children; ++i) {
5016        zend_ast *trait_ast = traits->child[i];
5017        zend_string *name = zend_ast_get_str(trait_ast);
5018
5019        if (ce->ce_flags & ZEND_ACC_INTERFACE) {
5020            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
5021                "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name));
5022        }
5023
5024        switch (zend_get_class_fetch_type(name)) {
5025            case ZEND_FETCH_CLASS_SELF:
5026            case ZEND_FETCH_CLASS_PARENT:
5027            case ZEND_FETCH_CLASS_STATIC:
5028                zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as trait name "
5029                    "as it is reserved", ZSTR_VAL(name));
5030                break;
5031        }
5032
5033        opline = get_next_op(CG(active_op_array));
5034        opline->opcode = ZEND_ADD_TRAIT;
5035        SET_NODE(opline->op1, &FC(implementing_class));
5036        opline->op2_type = IS_CONST;
5037        opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
5038            zend_resolve_class_name_ast(trait_ast));
5039
5040        ce->num_traits++;
5041    }
5042
5043    if (!adaptations) {
5044        return;
5045    }
5046
5047    for (i = 0; i < adaptations->children; ++i) {
5048        zend_ast *adaptation_ast = adaptations->child[i];
5049        switch (adaptation_ast->kind) {
5050            case ZEND_AST_TRAIT_PRECEDENCE:
5051                zend_compile_trait_precedence(adaptation_ast);
5052                break;
5053            case ZEND_AST_TRAIT_ALIAS:
5054                zend_compile_trait_alias(adaptation_ast);
5055                break;
5056            EMPTY_SWITCH_DEFAULT_CASE()
5057        }
5058    }
5059}
5060/* }}} */
5061
5062void zend_compile_implements(znode *class_node, zend_ast *ast) /* {{{ */
5063{
5064    zend_ast_list *list = zend_ast_get_list(ast);
5065    uint32_t i;
5066    for (i = 0; i < list->children; ++i) {
5067        zend_ast *class_ast = list->child[i];
5068        zend_string *name = zend_ast_get_str(class_ast);
5069
5070        zend_op *opline;
5071
5072        if (!zend_is_const_default_class_ref(class_ast)) {
5073            zend_error_noreturn(E_COMPILE_ERROR,
5074                "Cannot use '%s' as interface name as it is reserved", ZSTR_VAL(name));
5075        }
5076
5077        opline = zend_emit_op(NULL, ZEND_ADD_INTERFACE, class_node, NULL);
5078        opline->op2_type = IS_CONST;
5079        opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
5080            zend_resolve_class_name_ast(class_ast));
5081
5082        CG(active_class_entry)->num_interfaces++;
5083    }
5084}
5085/* }}} */
5086
5087static zend_string *zend_generate_anon_class_name(unsigned char *lex_pos) /* {{{ */
5088{
5089    zend_string *result;
5090    char char_pos_buf[32];
5091    size_t char_pos_len = zend_sprintf(char_pos_buf, "%p", lex_pos);
5092    zend_string *filename = CG(active_op_array)->filename;
5093
5094    /* NULL, name length, filename length, last accepting char position length */
5095    result = zend_string_alloc(sizeof("class@anonymous") + ZSTR_LEN(filename) + char_pos_len, 0);
5096    sprintf(ZSTR_VAL(result), "class@anonymous%c%s%s", '\0', ZSTR_VAL(filename), char_pos_buf);
5097    return zend_new_interned_string(result);
5098}
5099/* }}} */
5100
5101void zend_compile_class_decl(zend_ast *ast) /* {{{ */
5102{
5103    zend_ast_decl *decl = (zend_ast_decl *) ast;
5104    zend_ast *extends_ast = decl->child[0];
5105    zend_ast *implements_ast = decl->child[1];
5106    zend_ast *stmt_ast = decl->child[2];
5107    zend_string *name, *lcname, *import_name = NULL;
5108    zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
5109    zend_op *opline;
5110    znode declare_node, extends_node;
5111
5112    zend_class_entry *original_ce = CG(active_class_entry);
5113    znode original_implementing_class = FC(implementing_class);
5114
5115    if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) {
5116        if (CG(active_class_entry)) {
5117            zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
5118        }
5119        name = decl->name;
5120        zend_assert_valid_class_name(name);
5121        lcname = zend_string_tolower(name);
5122        if (FC(current_namespace)) {
5123            name = zend_prefix_with_ns(name);
5124
5125            zend_string_release(lcname);
5126            lcname = zend_string_tolower(name);
5127        } else {
5128            zend_string_addref(name);
5129        }
5130
5131        if (FC(imports)) {
5132            import_name = zend_hash_find_ptr(FC(imports), lcname);
5133        }
5134
5135        if (import_name && !zend_string_equals_ci(lcname, import_name)) {
5136            zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
5137                    "because the name is already in use", ZSTR_VAL(name));
5138        }
5139
5140        name = zend_new_interned_string(name);
5141        lcname = zend_new_interned_string(lcname);
5142    } else {
5143        name = zend_generate_anon_class_name(decl->lex_pos);
5144        lcname = zend_string_copy(name); /* this normally is an interned string, except with opcache. We need a proper copy here or opcache will fail with use after free. */
5145    }
5146
5147    ce->type = ZEND_USER_CLASS;
5148    ce->name = name;
5149    zend_initialize_class_data(ce, 1);
5150
5151    ce->ce_flags |= decl->flags;
5152    ce->info.user.filename = zend_get_compiled_filename();
5153    ce->info.user.line_start = decl->start_lineno;
5154    ce->info.user.line_end = decl->end_lineno;
5155
5156    if (decl->doc_comment) {
5157        ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
5158    }
5159
5160    if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
5161        /* Serialization is not supported for anonymous classes */
5162        ce->serialize = zend_class_serialize_deny;
5163        ce->unserialize = zend_class_unserialize_deny;
5164    }
5165
5166    if (extends_ast) {
5167        if (!zend_is_const_default_class_ref(extends_ast)) {
5168            zend_string *extends_name = zend_ast_get_str(extends_ast);
5169            zend_error_noreturn(E_COMPILE_ERROR,
5170                "Cannot use '%s' as class name as it is reserved", ZSTR_VAL(extends_name));
5171        }
5172
5173        zend_compile_class_ref(&extends_node, extends_ast, 0);
5174    }
5175
5176    opline = get_next_op(CG(active_op_array));
5177    zend_make_var_result(&declare_node, opline);
5178
5179    // TODO.AST drop this
5180    GET_NODE(&FC(implementing_class), opline->result);
5181
5182    opline->op2_type = IS_CONST;
5183    LITERAL_STR(opline->op2, lcname);
5184
5185    if (decl->flags & ZEND_ACC_ANON_CLASS) {
5186        if (extends_ast) {
5187            opline->opcode = ZEND_DECLARE_ANON_INHERITED_CLASS;
5188            opline->extended_value = extends_node.u.op.var;
5189        } else {
5190            opline->opcode = ZEND_DECLARE_ANON_CLASS;
5191        }
5192
5193        opline->op1_type = IS_UNUSED;
5194
5195        zend_hash_update_ptr(CG(class_table), lcname, ce);
5196    } else {
5197        zend_string *key;
5198
5199        if (extends_ast) {
5200            opline->opcode = ZEND_DECLARE_INHERITED_CLASS;
5201            opline->extended_value = extends_node.u.op.var;
5202        } else {
5203            opline->opcode = ZEND_DECLARE_CLASS;
5204        }
5205
5206        key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
5207
5208        opline->op1_type = IS_CONST;
5209        LITERAL_STR(opline->op1, key);
5210
5211        zend_hash_update_ptr(CG(class_table), key, ce);
5212    }
5213
5214    CG(active_class_entry) = ce;
5215
5216    if (implements_ast) {
5217        zend_compile_implements(&declare_node, implements_ast);
5218    }
5219
5220    zend_compile_stmt(stmt_ast);
5221
5222    if (ce->num_traits == 0) {
5223        /* For traits this check is delayed until after trait binding */
5224        zend_check_deprecated_constructor(ce);
5225    }
5226
5227    if (ce->constructor) {
5228        ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
5229        if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
5230            zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
5231                ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
5232        }
5233        if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
5234            zend_error_noreturn(E_COMPILE_ERROR,
5235                "Constructor %s::%s() cannot declare a return type",
5236                ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
5237        }
5238    }
5239    if (ce->destructor) {
5240        ce->destructor->common.fn_flags |= ZEND_ACC_DTOR;
5241        if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
5242            zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
5243                ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
5244        } else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
5245            zend_error_noreturn(E_COMPILE_ERROR,
5246                "Destructor %s::%s() cannot declare a return type",
5247                ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
5248        }
5249    }
5250    if (ce->clone) {
5251        ce->clone->common.fn_flags |= ZEND_ACC_CLONE;
5252        if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
5253            zend_error_noreturn(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static",
5254                ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
5255        } else if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
5256            zend_error_noreturn(E_COMPILE_ERROR,
5257                "%s::%s() cannot declare a return type",
5258                ZSTR_VAL(ce->name), ZSTR_VAL(ce->clone->common.function_name));
5259        }
5260    }
5261
5262    /* Check for traits and proceed like with interfaces.
5263     * The only difference will be a combined handling of them in the end.
5264     * Thus, we need another opcode here. */
5265    if (ce->num_traits > 0) {
5266        ce->traits = NULL;
5267        ce->num_traits = 0;
5268        ce->ce_flags |= ZEND_ACC_IMPLEMENT_TRAITS;
5269
5270        zend_emit_op(NULL, ZEND_BIND_TRAITS, &declare_node, NULL);
5271    }
5272
5273    if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
5274        && (extends_ast || ce->num_interfaces > 0)
5275    ) {
5276        zend_verify_abstract_class(ce);
5277        if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_IMPLEMENT_TRAITS)) {
5278            zend_emit_op(NULL, ZEND_VERIFY_ABSTRACT_CLASS, &declare_node, NULL);
5279        }
5280    }
5281
5282    /* Inherit interfaces; reset number to zero, we need it for above check and
5283     * will restore it during actual implementation.
5284     * The ZEND_ACC_IMPLEMENT_INTERFACES flag disables double call to
5285     * zend_verify_abstract_class() */
5286    if (ce->num_interfaces > 0) {
5287        ce->interfaces = NULL;
5288        ce->num_interfaces = 0;
5289        ce->ce_flags |= ZEND_ACC_IMPLEMENT_INTERFACES;
5290    }
5291
5292    FC(implementing_class) = original_implementing_class;
5293    CG(active_class_entry) = original_ce;
5294}
5295/* }}} */
5296
5297static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
5298{
5299    switch (type) {
5300        case T_CLASS:
5301            if (!FC(imports)) {
5302                FC(imports) = emalloc(sizeof(HashTable));
5303                zend_hash_init(FC(imports), 8, NULL, str_dtor, 0);
5304            }
5305            return FC(imports);
5306        case T_FUNCTION:
5307            if (!FC(imports_function)) {
5308                FC(imports_function) = emalloc(sizeof(HashTable));
5309                zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0);
5310            }
5311            return FC(imports_function);
5312        case T_CONST:
5313            if (!FC(imports_const)) {
5314                FC(imports_const) = emalloc(sizeof(HashTable));
5315                zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0);
5316            }
5317            return FC(imports_const);
5318        EMPTY_SWITCH_DEFAULT_CASE()
5319    }
5320
5321    return NULL;
5322}
5323/* }}} */
5324
5325static char *zend_get_use_type_str(uint32_t type) /* {{{ */
5326{
5327    switch (type) {
5328        case T_CLASS:
5329            return "";
5330        case T_FUNCTION:
5331            return " function";
5332        case T_CONST:
5333            return " const";
5334        EMPTY_SWITCH_DEFAULT_CASE()
5335    }
5336
5337    return " unknown";
5338}
5339/* }}} */
5340
5341static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
5342{
5343    if (zend_string_equals_ci(old_name, check_name)) {
5344        return;
5345    }
5346
5347    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
5348        "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name));
5349}
5350/* }}} */
5351
5352void zend_compile_use(zend_ast *ast) /* {{{ */
5353{
5354    zend_ast_list *list = zend_ast_get_list(ast);
5355    uint32_t i;
5356    zend_string *current_ns = FC(current_namespace);
5357    uint32_t type = ast->attr;
5358    HashTable *current_import = zend_get_import_ht(type);
5359    zend_bool case_sensitive = type == T_CONST;
5360
5361    for (i = 0; i < list->children; ++i) {
5362        zend_ast *use_ast = list->child[i];
5363        zend_ast *old_name_ast = use_ast->child[0];
5364        zend_ast *new_name_ast = use_ast->child[1];
5365        zend_string *old_name = zend_ast_get_str(old_name_ast);
5366        zend_string *new_name, *lookup_name;
5367
5368        if (new_name_ast) {
5369            new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
5370        } else {
5371            const char *unqualified_name;
5372            size_t unqualified_name_len;
5373            if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
5374                /* The form "use A\B" is equivalent to "use A\B as B" */
5375                new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
5376            } else {
5377                new_name = zend_string_copy(old_name);
5378
5379                if (!current_ns) {
5380                    if (type == T_CLASS && zend_string_equals_literal(new_name, "strict")) {
5381                        zend_error_noreturn(E_COMPILE_ERROR,
5382                            "You seem to be trying to use a different language...");
5383                    }
5384
5385                    zend_error(E_WARNING, "The use statement with non-compound name '%s' "
5386                        "has no effect", ZSTR_VAL(new_name));
5387                }
5388            }
5389        }
5390
5391        if (case_sensitive) {
5392            lookup_name = zend_string_copy(new_name);
5393        } else {
5394            lookup_name = zend_string_tolower(new_name);
5395        }
5396
5397        if (type == T_CLASS && zend_is_reserved_class_name(new_name)) {
5398            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
5399                "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name));
5400        }
5401
5402        if (current_ns) {
5403            zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0);
5404            zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns));
5405            ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\';
5406            memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name));
5407
5408            if (zend_hash_exists(CG(class_table), ns_name)) {
5409                zend_check_already_in_use(type, old_name, new_name, ns_name);
5410            }
5411
5412            zend_string_free(ns_name);
5413        } else {
5414            switch (type) {
5415                case T_CLASS:
5416                {
5417                    zend_class_entry *ce = zend_hash_find_ptr(CG(class_table), lookup_name);
5418                    if (ce && ce->type == ZEND_USER_CLASS
5419                        && ce->info.user.filename == CG(compiled_filename)
5420                    ) {
5421                        zend_check_already_in_use(type, old_name, new_name, lookup_name);
5422                    }
5423                    break;
5424                }
5425                case T_FUNCTION:
5426                {
5427                    zend_function *fn = zend_hash_find_ptr(CG(function_table), lookup_name);
5428                    if (fn && fn->type == ZEND_USER_FUNCTION
5429                        && fn->op_array.filename == CG(compiled_filename)
5430                    ) {
5431                        zend_check_already_in_use(type, old_name, new_name, lookup_name);
5432                    }
5433                    break;
5434                }
5435                case T_CONST: