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