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
2964static int zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, zend_function *fbc) /* {{{ */
2965{
2966    if (EG(assertions) >= 0) {
2967        znode name_node;
2968        zend_op *opline;
2969        uint32_t check_op_number = get_next_op_number(CG(active_op_array));
2970
2971        zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);
2972
2973        if (fbc) {
2974            name_node.op_type = IS_CONST;
2975            ZVAL_STR_COPY(&name_node.u.constant, name);
2976
2977            opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
2978        } else {
2979            opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL);
2980            opline->op2_type = IS_CONST;
2981            opline->op2.constant = zend_add_ns_func_name_literal(
2982                CG(active_op_array), name);
2983        }
2984        zend_alloc_cache_slot(opline->op2.constant);
2985
2986        if (args->children == 1 &&
2987            (args->child[0]->kind != ZEND_AST_ZVAL ||
2988             Z_TYPE_P(zend_ast_get_zval(args->child[0])) != IS_STRING)) {
2989            /* add "assert(condition) as assertion message */
2990            zend_ast_list_add((zend_ast*)args,
2991                zend_ast_create_zval_from_str(
2992                    zend_ast_export("assert(", args->child[0], ")")));
2993        }
2994
2995        zend_compile_call_common(result, (zend_ast*)args, fbc);
2996
2997        CG(active_op_array)->opcodes[check_op_number].op2.opline_num = get_next_op_number(CG(active_op_array));
2998    } else {
2999        if (!fbc) {
3000            zend_string_release(name);
3001        }
3002        result->op_type = IS_CONST;
3003        ZVAL_TRUE(&result->u.constant);
3004    }
3005
3006    return SUCCESS;
3007}
3008/* }}} */
3009
3010int zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, zend_function *fbc) /* {{{ */
3011{
3012    if (fbc->internal_function.handler == ZEND_FN(display_disabled_function)) {
3013        return FAILURE;
3014    }
3015
3016    if (zend_string_equals_literal(lcname, "strlen")) {
3017        return zend_compile_func_strlen(result, args);
3018    } else if (zend_string_equals_literal(lcname, "is_null")) {
3019        return zend_compile_func_typecheck(result, args, IS_NULL);
3020    } else if (zend_string_equals_literal(lcname, "is_bool")) {
3021        return zend_compile_func_typecheck(result, args, _IS_BOOL);
3022    } else if (zend_string_equals_literal(lcname, "is_long")
3023        || zend_string_equals_literal(lcname, "is_int")
3024        || zend_string_equals_literal(lcname, "is_integer")
3025    ) {
3026        return zend_compile_func_typecheck(result, args, IS_LONG);
3027    } else if (zend_string_equals_literal(lcname, "is_float")
3028        || zend_string_equals_literal(lcname, "is_double")
3029        || zend_string_equals_literal(lcname, "is_real")
3030    ) {
3031        return zend_compile_func_typecheck(result, args, IS_DOUBLE);
3032    } else if (zend_string_equals_literal(lcname, "is_string")) {
3033        return zend_compile_func_typecheck(result, args, IS_STRING);
3034    } else if (zend_string_equals_literal(lcname, "is_array")) {
3035        return zend_compile_func_typecheck(result, args, IS_ARRAY);
3036    } else if (zend_string_equals_literal(lcname, "is_object")) {
3037        return zend_compile_func_typecheck(result, args, IS_OBJECT);
3038    } else if (zend_string_equals_literal(lcname, "is_resource")) {
3039        return zend_compile_func_typecheck(result, args, IS_RESOURCE);
3040    } else if (zend_string_equals_literal(lcname, "defined")) {
3041        return zend_compile_func_defined(result, args);
3042    } else if (zend_string_equals_literal(lcname, "call_user_func_array")) {
3043        return zend_compile_func_cufa(result, args, lcname);
3044    } else if (zend_string_equals_literal(lcname, "call_user_func")) {
3045        return zend_compile_func_cuf(result, args, lcname);
3046    } else if (zend_string_equals_literal(lcname, "assert")) {
3047        return zend_compile_assert(result, args, lcname, fbc);
3048    } else {
3049        return FAILURE;
3050    }
3051}
3052/* }}} */
3053
3054void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3055{
3056    zend_ast *name_ast = ast->child[0];
3057    zend_ast *args_ast = ast->child[1];
3058
3059    znode name_node;
3060
3061    if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
3062        zend_compile_expr(&name_node, name_ast);
3063        zend_compile_dynamic_call(result, &name_node, args_ast);
3064        return;
3065    }
3066
3067    {
3068        zend_bool runtime_resolution = zend_compile_function_name(&name_node, name_ast);
3069        if (runtime_resolution) {
3070            if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")) {
3071                zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
3072            } else {
3073                zend_compile_ns_call(result, &name_node, args_ast);
3074            }
3075            return;
3076        }
3077    }
3078
3079    {
3080        zval *name = &name_node.u.constant;
3081        zend_string *lcname;
3082        zend_function *fbc;
3083        zend_op *opline;
3084
3085        lcname = zend_string_tolower(Z_STR_P(name));
3086
3087        fbc = zend_hash_find_ptr(CG(function_table), lcname);
3088        if (!fbc || (fbc->type == ZEND_INTERNAL_FUNCTION &&
3089            (CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS))
3090        ) {
3091            zend_string_release(lcname);
3092            zend_compile_dynamic_call(result, &name_node, args_ast);
3093            return;
3094        }
3095
3096        if (zend_try_compile_special_func(result, lcname,
3097                zend_ast_get_list(args_ast), fbc) == SUCCESS
3098        ) {
3099            zend_string_release(lcname);
3100            zval_ptr_dtor(&name_node.u.constant);
3101            return;
3102        }
3103
3104        zval_ptr_dtor(&name_node.u.constant);
3105        ZVAL_NEW_STR(&name_node.u.constant, lcname);
3106
3107        opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node);
3108        zend_alloc_cache_slot(opline->op2.constant);
3109
3110        zend_compile_call_common(result, args_ast, fbc);
3111    }
3112}
3113/* }}} */
3114
3115void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3116{
3117    zend_ast *obj_ast = ast->child[0];
3118    zend_ast *method_ast = ast->child[1];
3119    zend_ast *args_ast = ast->child[2];
3120
3121    znode obj_node, method_node;
3122    zend_op *opline;
3123
3124    if (is_this_fetch(obj_ast)) {
3125        obj_node.op_type = IS_UNUSED;
3126    } else {
3127        zend_compile_expr(&obj_node, obj_ast);
3128    }
3129
3130    zend_compile_expr(&method_node, method_ast);
3131    opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL);
3132
3133    if (method_node.op_type == IS_CONST) {
3134        if (Z_TYPE(method_node.u.constant) != IS_STRING) {
3135            zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3136        }
3137
3138        opline->op2_type = IS_CONST;
3139        opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3140            Z_STR(method_node.u.constant));
3141        zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3142    } else {
3143        SET_NODE(opline->op2, &method_node);
3144    }
3145
3146    zend_compile_call_common(result, args_ast, NULL);
3147}
3148/* }}} */
3149
3150zend_bool zend_is_constructor(zend_string *name) /* {{{ */
3151{
3152    return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
3153}
3154/* }}} */
3155
3156void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
3157{
3158    zend_ast *class_ast = ast->child[0];
3159    zend_ast *method_ast = ast->child[1];
3160    zend_ast *args_ast = ast->child[2];
3161
3162    znode class_node, method_node;
3163    zend_op *opline;
3164    zend_ulong extended_value = 0;
3165
3166    if (zend_is_const_default_class_ref(class_ast)) {
3167        class_node.op_type = IS_CONST;
3168        ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(class_ast));
3169    } else {
3170        opline = zend_compile_class_ref(&class_node, class_ast);
3171        extended_value = opline->extended_value;
3172    }
3173
3174    zend_compile_expr(&method_node, method_ast);
3175    if (method_node.op_type == IS_CONST) {
3176        zval *name = &method_node.u.constant;
3177        if (Z_TYPE_P(name) != IS_STRING) {
3178            zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string");
3179        }
3180        if (zend_is_constructor(Z_STR_P(name))) {
3181            zval_ptr_dtor(name);
3182            method_node.op_type = IS_UNUSED;
3183        }
3184    }
3185
3186    opline = get_next_op(CG(active_op_array));
3187    opline->opcode = ZEND_INIT_STATIC_METHOD_CALL;
3188    opline->extended_value = extended_value;
3189
3190    zend_set_class_name_op1(opline, &class_node);
3191
3192    if (method_node.op_type == IS_CONST) {
3193        opline->op2_type = IS_CONST;
3194        opline->op2.constant = zend_add_func_name_literal(CG(active_op_array),
3195            Z_STR(method_node.u.constant));
3196        if (opline->op1_type == IS_CONST) {
3197            zend_alloc_cache_slot(opline->op2.constant);
3198        } else {
3199            zend_alloc_polymorphic_cache_slot(opline->op2.constant);
3200        }
3201    } else {
3202        SET_NODE(opline->op2, &method_node);
3203    }
3204
3205    zend_compile_call_common(result, args_ast, NULL);
3206}
3207/* }}} */
3208
3209void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */
3210{
3211    zend_ast *class_ast = ast->child[0];
3212    zend_ast *args_ast = ast->child[1];
3213
3214    znode class_node, ctor_result;
3215    zend_op *opline;
3216    uint32_t opnum;
3217
3218    if (zend_is_const_default_class_ref(class_ast)) {
3219        class_node.op_type = IS_CONST;
3220        ZVAL_STR(&class_node.u.constant, zend_resolve_class_name_ast(class_ast));
3221    } else {
3222        zend_compile_class_ref(&class_node, class_ast);
3223    }
3224
3225    opnum = get_next_op_number(CG(active_op_array));
3226    opline = zend_emit_op(result, ZEND_NEW, NULL, NULL);
3227
3228    if (class_node.op_type == IS_CONST) {
3229        opline->op1_type = IS_CONST;
3230        opline->op1.constant = zend_add_class_name_literal(
3231            CG(active_op_array), Z_STR(class_node.u.constant));
3232    } else {
3233        SET_NODE(opline->op1, &class_node);
3234    }
3235
3236    zend_compile_call_common(&ctor_result, args_ast, NULL);
3237    zend_do_free(&ctor_result);
3238
3239    /* New jumps over ctor call if ctor does not exist */
3240    opline = &CG(active_op_array)->opcodes[opnum];
3241    opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3242}
3243/* }}} */
3244
3245void zend_compile_clone(znode *result, zend_ast *ast) /* {{{ */
3246{
3247    zend_ast *obj_ast = ast->child[0];
3248
3249    znode obj_node;
3250    zend_compile_expr(&obj_node, obj_ast);
3251
3252    zend_emit_op(result, ZEND_CLONE, &obj_node, NULL);
3253}
3254/* }}} */
3255
3256void zend_compile_global_var(zend_ast *ast) /* {{{ */
3257{
3258    zend_ast *var_ast = ast->child[0];
3259    zend_ast *name_ast = var_ast->child[0];
3260
3261    znode name_node, result;
3262
3263    zend_compile_expr(&name_node, name_ast);
3264    if (name_node.op_type == IS_CONST) {
3265        convert_to_string(&name_node.u.constant);
3266    }
3267
3268    if (zend_try_compile_cv(&result, var_ast) == SUCCESS) {
3269        zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node);
3270        zend_alloc_cache_slot(opline->op2.constant);
3271    } else {
3272        zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL);
3273
3274        // TODO.AST Avoid double fetch
3275        //opline->extended_value = ZEND_FETCH_GLOBAL_LOCK;
3276
3277        zend_emit_assign_ref_znode(var_ast, &result);
3278    }
3279}
3280/* }}} */
3281
3282static void zend_compile_static_var_common(zend_ast *var_ast, zval *value, zend_bool by_ref) /* {{{ */
3283{
3284    znode var_node, result;
3285    zend_op *opline;
3286
3287    zend_compile_expr(&var_node, var_ast);
3288
3289    if (!CG(active_op_array)->static_variables) {
3290        if (CG(active_op_array)->scope) {
3291            CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
3292        }
3293        ALLOC_HASHTABLE(CG(active_op_array)->static_variables);
3294        zend_hash_init(CG(active_op_array)->static_variables, 8, NULL, ZVAL_PTR_DTOR, 0);
3295    }
3296
3297    if (GC_REFCOUNT(CG(active_op_array)->static_variables) > 1) {
3298        if (!(GC_FLAGS(CG(active_op_array)->static_variables) & IS_ARRAY_IMMUTABLE)) {
3299            GC_REFCOUNT(CG(active_op_array)->static_variables)--;
3300        }
3301        CG(active_op_array)->static_variables = zend_array_dup(CG(active_op_array)->static_variables);
3302    }
3303    zend_hash_update(CG(active_op_array)->static_variables, Z_STR(var_node.u.constant), value);
3304
3305    opline = zend_emit_op(&result, by_ref ? ZEND_FETCH_W : ZEND_FETCH_R, &var_node, NULL);
3306    opline->extended_value = ZEND_FETCH_STATIC;
3307
3308    if (by_ref) {
3309        zend_ast *fetch_ast = zend_ast_create(ZEND_AST_VAR, var_ast);
3310        zend_emit_assign_ref_znode(fetch_ast, &result);
3311    } else {
3312        zend_ast *fetch_ast = zend_ast_create(ZEND_AST_VAR, var_ast);
3313        zend_emit_assign_znode(fetch_ast, &result);
3314    }
3315}
3316/* }}} */
3317
3318void zend_compile_static_var(zend_ast *ast) /* {{{ */
3319{
3320    zend_ast *var_ast = ast->child[0];
3321    zend_ast *value_ast = ast->child[1];
3322    zval value_zv;
3323
3324    if (value_ast) {
3325        zend_const_expr_to_zval(&value_zv, value_ast);
3326    } else {
3327        ZVAL_NULL(&value_zv);
3328    }
3329
3330    zend_compile_static_var_common(var_ast, &value_zv, 1);
3331}
3332/* }}} */
3333
3334void zend_compile_unset(zend_ast *ast) /* {{{ */
3335{
3336    zend_ast *var_ast = ast->child[0];
3337
3338    znode var_node;
3339    zend_op *opline;
3340    switch (var_ast->kind) {
3341        case ZEND_AST_VAR:
3342            if (zend_try_compile_cv(&var_node, var_ast) == SUCCESS) {
3343                opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL);
3344                opline->extended_value = ZEND_FETCH_LOCAL | ZEND_QUICK_SET;
3345            } else {
3346                opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, 0);
3347                opline->opcode = ZEND_UNSET_VAR;
3348            }
3349            return;
3350        case ZEND_AST_DIM:
3351            opline = zend_compile_dim_common(NULL, var_ast, BP_VAR_UNSET);
3352            opline->opcode = ZEND_UNSET_DIM;
3353            return;
3354        case ZEND_AST_PROP:
3355            opline = zend_compile_prop_common(NULL, var_ast, BP_VAR_UNSET);
3356            opline->opcode = ZEND_UNSET_OBJ;
3357            return;
3358        case ZEND_AST_STATIC_PROP:
3359            opline = zend_compile_static_prop_common(NULL, var_ast, BP_VAR_UNSET, 0);
3360            opline->opcode = ZEND_UNSET_VAR;
3361            return;
3362        EMPTY_SWITCH_DEFAULT_CASE()
3363    }
3364}
3365/* }}} */
3366
3367static void zend_free_foreach_and_switch_variables(void) /* {{{ */
3368{
3369    uint32_t opnum_start, opnum_end, i;
3370
3371    opnum_start = get_next_op_number(CG(active_op_array));
3372
3373    zend_stack_apply(&CG(loop_var_stack), ZEND_STACK_APPLY_TOPDOWN, (int (*)(void *element)) generate_free_loop_var);
3374
3375    opnum_end = get_next_op_number(CG(active_op_array));
3376
3377    for (i = opnum_start; i < opnum_end; ++i) {
3378        CG(active_op_array)->opcodes[i].extended_value |= EXT_TYPE_FREE_ON_RETURN;
3379    }
3380}
3381/* }}} */
3382
3383
3384void zend_compile_return(zend_ast *ast) /* {{{ */
3385{
3386    zend_ast *expr_ast = ast->child[0];
3387    zend_bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
3388
3389    znode expr_node;
3390    zend_op *opline;
3391
3392    if (!expr_ast) {
3393        expr_node.op_type = IS_CONST;
3394        ZVAL_NULL(&expr_node.u.constant);
3395    } else if (by_ref && zend_is_variable(expr_ast) && !zend_is_call(expr_ast)) {
3396        zend_compile_var(&expr_node, expr_ast, BP_VAR_REF);
3397    } else {
3398        zend_compile_expr(&expr_node, expr_ast);
3399    }
3400
3401    zend_free_foreach_and_switch_variables();
3402
3403    if (CG(context).in_finally) {
3404        opline = zend_emit_op(NULL, ZEND_DISCARD_EXCEPTION, NULL, NULL);
3405        opline->op1_type = IS_TMP_VAR;
3406        opline->op1.var = CG(context).fast_call_var;
3407    }
3408
3409    /* Generator return types are handled separately */
3410    if (!(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) && CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
3411        zend_emit_return_type_check(expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1);
3412    }
3413    opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN,
3414        &expr_node, NULL);
3415
3416    if (expr_ast) {
3417        if (zend_is_call(expr_ast)) {
3418            opline->extended_value = ZEND_RETURNS_FUNCTION;
3419        } else if (by_ref && !zend_is_variable(expr_ast)) {
3420            opline->extended_value = ZEND_RETURNS_VALUE;
3421        }
3422    }
3423}
3424/* }}} */
3425
3426void zend_compile_echo(zend_ast *ast) /* {{{ */
3427{
3428    zend_ast *expr_ast = ast->child[0];
3429
3430    znode expr_node;
3431    zend_compile_expr(&expr_node, expr_ast);
3432
3433    zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL);
3434}
3435/* }}} */
3436
3437void zend_compile_throw(zend_ast *ast) /* {{{ */
3438{
3439    zend_ast *expr_ast = ast->child[0];
3440
3441    znode expr_node;
3442    zend_compile_expr(&expr_node, expr_ast);
3443
3444    zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL);
3445}
3446/* }}} */
3447
3448void zend_compile_break_continue(zend_ast *ast) /* {{{ */
3449{
3450    zend_ast *depth_ast = ast->child[0];
3451
3452    znode depth_node;
3453    zend_op *opline;
3454
3455    ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE);
3456
3457    if (depth_ast) {
3458        if (depth_ast->kind != ZEND_AST_ZVAL) {
3459            zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-constant operand "
3460                "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue");
3461        }
3462
3463        zend_compile_expr(&depth_node, depth_ast);
3464
3465        if (Z_TYPE(depth_node.u.constant) != IS_LONG || Z_LVAL(depth_node.u.constant) < 1) {
3466            zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive numbers",
3467                ast->kind == ZEND_AST_BREAK ? "break" : "continue");
3468        }
3469    } else {
3470        depth_node.op_type = IS_CONST;
3471        ZVAL_LONG(&depth_node.u.constant, 1);
3472    }
3473
3474    opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT,
3475        NULL, &depth_node);
3476    opline->op1.opline_num = CG(context).current_brk_cont;
3477}
3478/* }}} */
3479
3480void zend_compile_goto(zend_ast *ast) /* {{{ */
3481{
3482    zend_ast *label_ast = ast->child[0];
3483    znode label_node;
3484    zend_op *opline;
3485
3486    zend_compile_expr(&label_node, label_ast);
3487    opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node);
3488    opline->extended_value = CG(context).current_brk_cont;
3489    zend_resolve_goto_label(CG(active_op_array), opline, 0);
3490}
3491/* }}} */
3492
3493void zend_compile_label(zend_ast *ast) /* {{{ */
3494{
3495    zend_string *label = zend_ast_get_str(ast->child[0]);
3496    zend_label dest;
3497
3498    if (!CG(context).labels) {
3499        ALLOC_HASHTABLE(CG(context).labels);
3500        zend_hash_init(CG(context).labels, 8, NULL, ptr_dtor, 0);
3501    }
3502
3503    dest.brk_cont = CG(context).current_brk_cont;
3504    dest.opline_num = get_next_op_number(CG(active_op_array));
3505
3506    if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) {
3507        zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", label->val);
3508    }
3509}
3510/* }}} */
3511
3512void zend_compile_while(zend_ast *ast) /* {{{ */
3513{
3514    zend_ast *cond_ast = ast->child[0];
3515    zend_ast *stmt_ast = ast->child[1];
3516    znode cond_node;
3517    uint32_t opnum_start, opnum_jmp, opnum_cond;
3518
3519    opnum_jmp = zend_emit_jump(0);
3520
3521    zend_begin_loop();
3522
3523    opnum_start = get_next_op_number(CG(active_op_array));
3524    zend_compile_stmt(stmt_ast);
3525
3526    opnum_cond = get_next_op_number(CG(active_op_array));
3527    zend_update_jump_target(opnum_jmp, opnum_cond);
3528    zend_compile_expr(&cond_node, cond_ast);
3529
3530    zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
3531
3532    zend_end_loop(opnum_cond, 0);
3533}
3534/* }}} */
3535
3536void zend_compile_do_while(zend_ast *ast) /* {{{ */
3537{
3538    zend_ast *stmt_ast = ast->child[0];
3539    zend_ast *cond_ast = ast->child[1];
3540
3541    znode cond_node;
3542    uint32_t opnum_start, opnum_cond;
3543
3544    zend_begin_loop();
3545
3546    opnum_start = get_next_op_number(CG(active_op_array));
3547    zend_compile_stmt(stmt_ast);
3548
3549    opnum_cond = get_next_op_number(CG(active_op_array));
3550    zend_compile_expr(&cond_node, cond_ast);
3551
3552    zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start);
3553
3554    zend_end_loop(opnum_cond, 0);
3555}
3556/* }}} */
3557
3558void zend_compile_expr_list(znode *result, zend_ast *ast) /* {{{ */
3559{
3560    zend_ast_list *list;
3561    uint32_t i;
3562
3563    result->op_type = IS_CONST;
3564    ZVAL_TRUE(&result->u.constant);
3565
3566    if (!ast) {
3567        return;
3568    }
3569
3570    list = zend_ast_get_list(ast);
3571    for (i = 0; i < list->children; ++i) {
3572        zend_ast *expr_ast = list->child[i];
3573
3574        zend_do_free(result);
3575        zend_compile_expr(result, expr_ast);
3576    }
3577}
3578/* }}} */
3579
3580void zend_compile_for(zend_ast *ast) /* {{{ */
3581{
3582    zend_ast *init_ast = ast->child[0];
3583    zend_ast *cond_ast = ast->child[1];
3584    zend_ast *loop_ast = ast->child[2];
3585    zend_ast *stmt_ast = ast->child[3];
3586
3587    znode result;
3588    uint32_t opnum_start, opnum_jmp, opnum_loop;
3589
3590    zend_compile_expr_list(&result, init_ast);
3591    zend_do_free(&result);
3592
3593    opnum_jmp = zend_emit_jump(0);
3594
3595    zend_begin_loop();
3596
3597    opnum_start = get_next_op_number(CG(active_op_array));
3598    zend_compile_stmt(stmt_ast);
3599
3600    opnum_loop = get_next_op_number(CG(active_op_array));
3601    zend_compile_expr_list(&result, loop_ast);
3602    zend_do_free(&result);
3603
3604    zend_update_jump_target_to_next(opnum_jmp);
3605    zend_compile_expr_list(&result, cond_ast);
3606    zend_do_extended_info();
3607
3608    zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start);
3609
3610    zend_end_loop(opnum_loop, 0);
3611}
3612/* }}} */
3613
3614void zend_compile_foreach(zend_ast *ast) /* {{{ */
3615{
3616    zend_ast *expr_ast = ast->child[0];
3617    zend_ast *value_ast = ast->child[1];
3618    zend_ast *key_ast = ast->child[2];
3619    zend_ast *stmt_ast = ast->child[3];
3620    zend_bool by_ref = value_ast->kind == ZEND_AST_REF;
3621    zend_bool is_variable = zend_is_variable(expr_ast) && !zend_is_call(expr_ast)
3622        && zend_can_write_to_variable(expr_ast);
3623
3624    znode expr_node, reset_node, value_node, key_node;
3625    zend_op *opline;
3626    uint32_t opnum_reset, opnum_fetch;
3627
3628    if (key_ast) {
3629        if (key_ast->kind == ZEND_AST_REF) {
3630            zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference");
3631        }
3632        if (key_ast->kind == ZEND_AST_LIST) {
3633            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element");
3634        }
3635    }
3636
3637    if (by_ref) {
3638        value_ast = value_ast->child[0];
3639    }
3640
3641    if (by_ref && is_variable) {
3642        zend_compile_var(&expr_node, expr_ast, BP_VAR_W);
3643    } else {
3644        zend_compile_expr(&expr_node, expr_ast);
3645    }
3646
3647    if (by_ref) {
3648        zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W);
3649    }
3650
3651    opnum_reset = get_next_op_number(CG(active_op_array));
3652    opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL);
3653
3654    reset_node.flag = 1; /* generate FE_FREE */
3655    zend_stack_push(&CG(loop_var_stack), &reset_node);
3656
3657    opnum_fetch = get_next_op_number(CG(active_op_array));
3658    opline = zend_emit_op(&value_node, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL);
3659    if (key_ast) {
3660        opline->extended_value = 1;
3661    }
3662
3663    opline = zend_emit_op(NULL, ZEND_OP_DATA, NULL, NULL);
3664
3665    if (key_ast) {
3666        zend_make_tmp_result(&key_node, opline);
3667    }
3668
3669    if (by_ref) {
3670        zend_emit_assign_ref_znode(value_ast, &value_node);
3671    } else {
3672        zend_emit_assign_znode(value_ast, &value_node);
3673    }
3674
3675    if (key_ast) {
3676        zend_emit_assign_znode(key_ast, &key_node);
3677    }
3678
3679    zend_begin_loop();
3680
3681    zend_compile_stmt(stmt_ast);
3682
3683    zend_emit_jump(opnum_fetch);
3684
3685    opline = &CG(active_op_array)->opcodes[opnum_reset];
3686    opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3687
3688    opline = &CG(active_op_array)->opcodes[opnum_fetch];
3689    opline->op2.opline_num = get_next_op_number(CG(active_op_array));
3690
3691    zend_end_loop(opnum_fetch, 1);
3692
3693    generate_free_loop_var(&reset_node);
3694    zend_stack_del_top(&CG(loop_var_stack));
3695}
3696/* }}} */
3697
3698void zend_compile_if(zend_ast *ast) /* {{{ */
3699{
3700    zend_ast_list *list = zend_ast_get_list(ast);
3701    uint32_t i;
3702    uint32_t *jmp_opnums = NULL;
3703
3704    if (list->children > 1) {
3705        jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0);
3706    }
3707
3708    for (i = 0; i < list->children; ++i) {
3709        zend_ast *elem_ast = list->child[i];
3710        zend_ast *cond_ast = elem_ast->child[0];
3711        zend_ast *stmt_ast = elem_ast->child[1];
3712
3713        znode cond_node;
3714        uint32_t opnum_jmpz;
3715        if (cond_ast) {
3716            zend_compile_expr(&cond_node, cond_ast);
3717            opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
3718        }
3719
3720        zend_compile_stmt(stmt_ast);
3721
3722        if (i != list->children - 1) {
3723            jmp_opnums[i] = zend_emit_jump(0);
3724        }
3725
3726        if (cond_ast) {
3727            zend_update_jump_target_to_next(opnum_jmpz);
3728        }
3729    }
3730
3731    if (list->children > 1) {
3732        for (i = 0; i < list->children - 1; ++i) {
3733            zend_update_jump_target_to_next(jmp_opnums[i]);
3734        }
3735        efree(jmp_opnums);
3736    }
3737}
3738/* }}} */
3739
3740void zend_compile_switch(zend_ast *ast) /* {{{ */
3741{
3742    zend_ast *expr_ast = ast->child[0];
3743    zend_ast_list *cases = zend_ast_get_list(ast->child[1]);
3744
3745    uint32_t i;
3746    zend_bool has_default_case = 0;
3747
3748    znode expr_node, case_node;
3749    zend_op *opline;
3750    uint32_t *jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0);
3751    uint32_t opnum_default_jmp;
3752
3753    zend_compile_expr(&expr_node, expr_ast);
3754
3755    expr_node.flag = 0;
3756    zend_stack_push(&CG(loop_var_stack), &expr_node);
3757
3758    zend_begin_loop();
3759
3760    case_node.op_type = IS_TMP_VAR;
3761    case_node.u.op.var = get_temporary_variable(CG(active_op_array));
3762
3763    for (i = 0; i < cases->children; ++i) {
3764        zend_ast *case_ast = cases->child[i];
3765        zend_ast *cond_ast = case_ast->child[0];
3766        znode cond_node;
3767
3768        if (!cond_ast) {
3769            if (has_default_case) {
3770                CG(zend_lineno) = case_ast->lineno;
3771                zend_error_noreturn(E_COMPILE_ERROR,
3772                    "Switch statements may only contain one default clause");
3773            }
3774            has_default_case = 1;
3775            continue;
3776        }
3777
3778        zend_compile_expr(&cond_node, cond_ast);
3779
3780        if (expr_node.op_type == IS_CONST
3781            && Z_TYPE(expr_node.u.constant) == IS_FALSE) {
3782            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
3783        } else if (expr_node.op_type == IS_CONST
3784            && Z_TYPE(expr_node.u.constant) == IS_TRUE) {
3785            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
3786        } else {
3787            opline = zend_emit_op(NULL, ZEND_CASE, &expr_node, &cond_node);
3788            SET_NODE(opline->result, &case_node);
3789            if (opline->op1_type == IS_CONST) {
3790                zval_copy_ctor(CT_CONSTANT(opline->op1));
3791            }
3792
3793            jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0);
3794        }
3795    }
3796
3797    opnum_default_jmp = zend_emit_jump(0);
3798
3799    for (i = 0; i < cases->children; ++i) {
3800        zend_ast *case_ast = cases->child[i];
3801        zend_ast *cond_ast = case_ast->child[0];
3802        zend_ast *stmt_ast = case_ast->child[1];
3803
3804        if (cond_ast) {
3805            zend_update_jump_target_to_next(jmpnz_opnums[i]);
3806        } else {
3807            zend_update_jump_target_to_next(opnum_default_jmp);
3808        }
3809
3810        zend_compile_stmt(stmt_ast);
3811    }
3812
3813    if (!has_default_case) {
3814        zend_update_jump_target_to_next(opnum_default_jmp);
3815    }
3816
3817    zend_end_loop(get_next_op_number(CG(active_op_array)), 1);
3818
3819    if (expr_node.op_type == IS_VAR || expr_node.op_type == IS_TMP_VAR) {
3820        zend_emit_op(NULL, ZEND_FREE,
3821            &expr_node, NULL);
3822    } else if (expr_node.op_type == IS_CONST) {
3823        zval_dtor(&expr_node.u.constant);
3824    }
3825
3826    zend_stack_del_top(&CG(loop_var_stack));
3827    efree(jmpnz_opnums);
3828}
3829/* }}} */
3830
3831void zend_compile_try(zend_ast *ast) /* {{{ */
3832{
3833    zend_ast *try_ast = ast->child[0];
3834    zend_ast_list *catches = zend_ast_get_list(ast->child[1]);
3835    zend_ast *finally_ast = ast->child[2];
3836
3837    uint32_t i;
3838    zend_op *opline;
3839    uint32_t try_catch_offset = zend_add_try_element(
3840        get_next_op_number(CG(active_op_array)));
3841    uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0);
3842
3843    if (catches->children == 0 && !finally_ast) {
3844        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally");
3845    }
3846
3847    zend_compile_stmt(try_ast);
3848
3849    if (catches->children != 0) {
3850        jmp_opnums[0] = zend_emit_jump(0);
3851    }
3852
3853    for (i = 0; i < catches->children; ++i) {
3854        zend_ast *catch_ast = catches->child[i];
3855        zend_ast *class_ast = catch_ast->child[0];
3856        zend_ast *var_ast = catch_ast->child[1];
3857        zend_ast *stmt_ast = catch_ast->child[2];
3858        zval *var_name = zend_ast_get_zval(var_ast);
3859        zend_bool is_last_catch = (i + 1 == catches->children);
3860
3861        uint32_t opnum_catch;
3862
3863        if (!zend_is_const_default_class_ref(class_ast)) {
3864            zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement");
3865        }
3866
3867        opnum_catch = get_next_op_number(CG(active_op_array));
3868        if (i == 0) {
3869            CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch;
3870        }
3871
3872        CG(zend_lineno) = catch_ast->lineno;
3873
3874        opline = get_next_op(CG(active_op_array));
3875        opline->opcode = ZEND_CATCH;
3876        opline->op1_type = IS_CONST;
3877        opline->op1.constant = zend_add_class_name_literal(CG(active_op_array),
3878            zend_resolve_class_name_ast(class_ast));
3879
3880        opline->op2_type = IS_CV;
3881        opline->op2.var = lookup_cv(CG(active_op_array), zend_string_copy(Z_STR_P(var_name)));
3882        opline->result.num = is_last_catch;
3883
3884        zend_compile_stmt(stmt_ast);
3885
3886        if (!is_last_catch) {
3887            jmp_opnums[i + 1] = zend_emit_jump(0);
3888        }
3889
3890        opline = &CG(active_op_array)->opcodes[opnum_catch];
3891        opline->extended_value = get_next_op_number(CG(active_op_array));
3892    }
3893
3894    for (i = 0; i < catches->children; ++i) {
3895        zend_update_jump_target_to_next(jmp_opnums[i]);
3896    }
3897
3898    if (finally_ast) {
3899        uint32_t opnum_jmp = get_next_op_number(CG(active_op_array)) + 1;
3900
3901        if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) {
3902            CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK;
3903            CG(context).fast_call_var = get_temporary_variable(CG(active_op_array));
3904        }
3905
3906        opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL);
3907        opline->op1.opline_num = opnum_jmp + 1;
3908        opline->result_type = IS_TMP_VAR;
3909        opline->result.var = CG(context).fast_call_var;
3910
3911        zend_emit_op(NULL, ZEND_JMP, NULL, NULL);
3912
3913        CG(context).in_finally++;
3914        zend_compile_stmt(finally_ast);
3915        CG(context).in_finally--;
3916
3917        CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1;
3918        CG(active_op_array)->try_catch_array[try_catch_offset].finally_end
3919            = get_next_op_number(CG(active_op_array));
3920
3921        opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL);
3922        opline->op1_type = IS_TMP_VAR;
3923        opline->op1.var = CG(context).fast_call_var;
3924
3925        zend_update_jump_target_to_next(opnum_jmp);
3926    }
3927
3928    efree(jmp_opnums);
3929}
3930/* }}} */
3931
3932/* Encoding declarations must already be handled during parsing */
3933void zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */
3934{
3935    zend_ast_list *declares = zend_ast_get_list(ast);
3936    uint32_t i;
3937    for (i = 0; i < declares->children; ++i) {
3938        zend_ast *declare_ast = declares->child[i];
3939        zend_ast *name_ast = declare_ast->child[0];
3940        zend_ast *value_ast = declare_ast->child[1];
3941        zend_string *name = zend_ast_get_str(name_ast);
3942
3943        if (zend_string_equals_literal_ci(name, "encoding")) {
3944            if (value_ast->kind != ZEND_AST_ZVAL) {
3945                zend_error_noreturn(E_COMPILE_ERROR, "Encoding must be a literal");
3946            }
3947
3948            if (CG(multibyte)) {
3949                zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast));
3950
3951                const zend_encoding *new_encoding, *old_encoding;
3952                zend_encoding_filter old_input_filter;
3953
3954                CG(encoding_declared) = 1;
3955
3956                new_encoding = zend_multibyte_fetch_encoding(encoding_name->val);
3957                if (!new_encoding) {
3958                    zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", encoding_name->val);
3959                } else {
3960                    old_input_filter = LANG_SCNG(input_filter);
3961                    old_encoding = LANG_SCNG(script_encoding);
3962                    zend_multibyte_set_filter(new_encoding);
3963
3964                    /* need to re-scan if input filter changed */
3965                    if (old_input_filter != LANG_SCNG(input_filter) ||
3966                         (old_input_filter && new_encoding != old_encoding)) {
3967                        zend_multibyte_yyinput_again(old_input_filter, old_encoding);
3968                    }
3969                }
3970
3971                zend_string_release(encoding_name);
3972            } else {
3973                zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because "
3974                    "Zend multibyte feature is turned off by settings");
3975            }
3976        }
3977    }
3978}
3979/* }}} */
3980
3981static int zend_declare_is_first_statement(zend_ast *ast) /* {{{ */
3982{
3983    uint32_t i = 0;
3984    zend_ast_list *file_ast = zend_ast_get_list(CG(ast));
3985
3986    /* Check to see if this declare is preceeded only by declare statements */
3987    while (i < file_ast->children) {
3988        if (file_ast->child[i] == ast) {
3989            return SUCCESS;
3990        } else if (file_ast->child[i] == NULL) {
3991            /* Empty statements are not allowed prior to a declare */
3992            return FAILURE;
3993        } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) {
3994            /* declares can only be preceeded by other declares */
3995            return FAILURE;
3996        }
3997        i++;
3998    }
3999    return FAILURE;
4000}
4001/* }}} */
4002
4003void zend_compile_declare(zend_ast *ast) /* {{{ */
4004{
4005    zend_ast_list *declares = zend_ast_get_list(ast->child[0]);
4006    zend_ast *stmt_ast = ast->child[1];
4007    zend_declarables orig_declarables = CG(declarables);
4008    uint32_t i;
4009
4010    for (i = 0; i < declares->children; ++i) {
4011        zend_ast *declare_ast = declares->child[i];
4012        zend_ast *name_ast = declare_ast->child[0];
4013        zend_ast *value_ast = declare_ast->child[1];
4014
4015        zend_string *name = zend_ast_get_str(name_ast);
4016        if (zend_string_equals_literal_ci(name, "ticks")) {
4017            zval value_zv;
4018            zend_const_expr_to_zval(&value_zv, value_ast);
4019            convert_to_long(&value_zv);
4020            ZVAL_COPY_VALUE(&CG(declarables).ticks, &value_zv);
4021            zval_dtor(&value_zv);
4022        } else if (zend_string_equals_literal_ci(name, "encoding")) {
4023
4024            if (FAILURE == zend_declare_is_first_statement(ast)) {
4025                zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be "
4026                    "the very first statement in the script");
4027            }
4028        } else if (zend_string_equals_literal_ci(name, "strict_types")) {
4029            zval value_zv;
4030
4031            if (FAILURE == zend_declare_is_first_statement(ast)) {
4032                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be "
4033                    "the very first statement in the script");
4034            }
4035
4036            if (ast->child[1] != NULL) {
4037                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not "
4038                    "use block mode");
4039            }
4040
4041            zend_const_expr_to_zval(&value_zv, value_ast);
4042
4043            if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) {
4044                zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value");
4045            }
4046
4047            if (Z_LVAL(value_zv) == 1) {
4048                CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES;
4049            }
4050
4051        } else {
4052            zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", name->val);
4053        }
4054    }
4055
4056    if (stmt_ast) {
4057        zend_compile_stmt(stmt_ast);
4058
4059        CG(declarables) = orig_declarables;
4060    }
4061}
4062/* }}} */
4063
4064void zend_compile_stmt_list(zend_ast *ast) /* {{{ */
4065{
4066    zend_ast_list *list = zend_ast_get_list(ast);
4067    uint32_t i;
4068    for (i = 0; i < list->children; ++i) {
4069        zend_compile_stmt(list->child[i]);
4070    }
4071}
4072/* }}} */
4073
4074void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_method) /* {{{ */
4075{
4076    zend_ast_list *list = zend_ast_get_list(ast);
4077    uint32_t i;
4078    zend_op_array *op_array = CG(active_op_array);
4079    zend_arg_info *arg_infos;
4080
4081    if (return_type_ast) {
4082        /* Use op_array->arg_info[-1] for return type hinting */
4083        arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0);
4084        arg_infos->name = NULL;
4085        arg_infos->pass_by_reference = (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;
4086        arg_infos->is_variadic = 0;
4087        arg_infos->type_hint = 0;
4088        arg_infos->allow_null = 0;
4089        arg_infos->class_name = NULL;
4090
4091        if (return_type_ast->kind == ZEND_AST_TYPE) {
4092            arg_infos->type_hint = return_type_ast->attr;
4093        } else {
4094            zend_string *class_name = zend_ast_get_str(return_type_ast);
4095            zend_uchar type = zend_lookup_scalar_typehint_by_name(class_name);
4096
4097            if (type != 0) {
4098                arg_infos->type_hint = type;
4099            } else {
4100                if (zend_is_const_default_class_ref(return_type_ast)) {
4101                    class_name = zend_resolve_class_name_ast(return_type_ast);
4102                } else {
4103                    zend_string_addref(class_name);
4104                    if (!is_method) {
4105                        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare a return type of %s outside of a class scope", class_name->val);
4106                        return;
4107                    }
4108                }
4109
4110                arg_infos->type_hint = IS_OBJECT;
4111                arg_infos->class_name = class_name;
4112            }
4113        }
4114
4115        arg_infos++;
4116        op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
4117    } else {
4118        if (list->children == 0) {
4119            return;
4120        }
4121        arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0);
4122    }
4123
4124    for (i = 0; i < list->children; ++i) {
4125        zend_ast *param_ast = list->child[i];
4126        zend_ast *type_ast = param_ast->child[0];
4127        zend_ast *var_ast = param_ast->child[1];
4128        zend_ast *default_ast = param_ast->child[2];
4129        zend_string *name = zend_ast_get_str(var_ast);
4130        zend_bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0;
4131        zend_bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0;
4132
4133        znode var_node, default_node;
4134        zend_uchar opcode;
4135        zend_op *opline;
4136        zend_arg_info *arg_info;
4137
4138        if (zend_is_auto_global(name)) {
4139            zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s",
4140                name->val);
4141        }
4142
4143        var_node.op_type = IS_CV;
4144        var_node.u.op.var = lookup_cv(CG(active_op_array), zend_string_copy(name));
4145
4146        if (EX_VAR_TO_NUM(var_node.u.op.var) != i) {
4147            zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s",
4148                name->val);
4149        } else if (zend_string_equals_literal(name, "this")) {
4150            if (op_array->scope && (op_array->fn_flags & ZEND_ACC_STATIC) == 0) {
4151                zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this");
4152            }
4153            op_array->this_var = var_node.u.op.var;
4154        }
4155
4156        if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
4157            zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic");
4158        }
4159
4160        if (is_variadic) {
4161            opcode = ZEND_RECV_VARIADIC;
4162            default_node.op_type = IS_UNUSED;
4163            op_array->fn_flags |= ZEND_ACC_VARIADIC;
4164
4165            if (default_ast) {
4166                zend_error_noreturn(E_COMPILE_ERROR,
4167                    "Variadic parameter cannot have a default value");
4168            }
4169        } else if (default_ast) {
4170            /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */
4171            uint32_t cops = CG(compiler_options);
4172            CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
4173            opcode = ZEND_RECV_INIT;
4174            default_node.op_type = IS_CONST;
4175            zend_const_expr_to_zval(&default_node.u.constant, default_ast);
4176            CG(compiler_options) = cops;
4177        } else {
4178            opcode = ZEND_RECV;
4179            default_node.op_type = IS_UNUSED;
4180            op_array->required_num_args = i + 1;
4181        }
4182
4183        opline = zend_emit_op(NULL, opcode, NULL, &default_node);
4184        SET_NODE(opline->result, &var_node);
4185        opline->op1.num = i + 1;
4186
4187        arg_info = &arg_infos[i];
4188        arg_info->name = zend_string_copy(name);
4189        arg_info->pass_by_reference = is_ref;
4190        arg_info->is_variadic = is_variadic;
4191        arg_info->type_hint = 0;
4192        arg_info->allow_null = 1;
4193        arg_info->class_name = NULL;
4194
4195        if (type_ast) {
4196            zend_bool has_null_default = default_ast
4197                && (Z_TYPE(default_node.u.constant) == IS_NULL
4198                    || (Z_TYPE(default_node.u.constant) == IS_CONSTANT
4199                        && strcasecmp(Z_STRVAL(default_node.u.constant), "NULL") == 0));
4200
4201            op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
4202            arg_info->allow_null = has_null_default;
4203
4204            if (type_ast->kind == ZEND_AST_TYPE) {
4205                arg_info->type_hint = type_ast->attr;
4206                if (arg_info->type_hint == IS_ARRAY) {
4207                    if (default_ast && !has_null_default
4208                        && Z_TYPE(default_node.u.constant) != IS_ARRAY
4209                        && !Z_CONSTANT(default_node.u.constant)
4210                    ) {
4211                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4212                            "with array type hint can only be an array or NULL");
4213                    }
4214                } else if (arg_info->type_hint == IS_CALLABLE && default_ast) {
4215                    if (!has_null_default && !Z_CONSTANT(default_node.u.constant)) {
4216                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4217                            "with callable type hint can only be NULL");
4218                    }
4219                }
4220            } else {
4221                zend_string *class_name = zend_ast_get_str(type_ast);
4222                zend_uchar type;
4223
4224                type = zend_lookup_scalar_typehint_by_name(class_name);
4225                if (type != 0) {
4226                    arg_info->type_hint = type;
4227                } else {
4228
4229                    if (zend_is_const_default_class_ref(type_ast)) {
4230                        class_name = zend_resolve_class_name_ast(type_ast);
4231                    } else {
4232                        zend_string_addref(class_name);
4233                    }
4234
4235                    arg_info->type_hint = IS_OBJECT;
4236                    arg_info->class_name = class_name;
4237                }
4238                if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) {
4239                    if (arg_info->class_name) {
4240                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4241                            "with a class type hint can only be NULL");
4242                    } else if (!ZEND_SAME_FAKE_TYPE(arg_info->type_hint, Z_TYPE(default_node.u.constant))) {
4243                        zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters "
4244                            "with a %s type hint can only be %s or NULL", class_name->val, class_name->val);
4245                    }
4246                }
4247            }
4248        }
4249    }
4250
4251    /* These are assigned at the end to avoid unitialized memory in case of an error */
4252    op_array->num_args = list->children;
4253    op_array->arg_info = arg_infos;
4254
4255    /* Don't count the variadic argument */
4256    if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
4257        op_array->num_args--;
4258    }
4259}
4260/* }}} */
4261
4262void zend_compile_closure_uses(zend_ast *ast) /* {{{ */
4263{
4264    zend_ast_list *list = zend_ast_get_list(ast);
4265    uint32_t i;
4266
4267    for (i = 0; i < list->children; ++i) {
4268        zend_ast *var_ast = list->child[i];
4269        zend_string *name = zend_ast_get_str(var_ast);
4270        zend_bool by_ref = var_ast->attr;
4271        zval zv;
4272
4273        if (zend_string_equals_literal(name, "this")) {
4274            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
4275        }
4276
4277        ZVAL_NULL(&zv);
4278        Z_CONST_FLAGS(zv) = by_ref ? IS_LEXICAL_REF : IS_LEXICAL_VAR;
4279
4280        zend_compile_static_var_common(var_ast, &zv, by_ref);
4281    }
4282}
4283/* }}} */
4284
4285void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_bool has_body) /* {{{ */
4286{
4287    zend_class_entry *ce = CG(active_class_entry);
4288    zend_bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0;
4289    zend_bool in_trait = (ce->ce_flags & ZEND_ACC_TRAIT) != 0;
4290    zend_bool is_public = (op_array->fn_flags & ZEND_ACC_PUBLIC) != 0;
4291    zend_bool is_static = (op_array->fn_flags & ZEND_ACC_STATIC) != 0;
4292
4293    zend_string *lcname;
4294
4295    if (in_interface) {
4296        if ((op_array->fn_flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PUBLIC) {
4297            zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method "
4298                "%s::%s() must be omitted", ce->name->val, name->val);
4299        }
4300        op_array->fn_flags |= ZEND_ACC_ABSTRACT;
4301    } else if (is_static && (op_array->fn_flags & ZEND_ACC_ABSTRACT)) {
4302        zend_error(E_STRICT, "Static function %s::%s() should not be abstract",
4303            ce->name->val, name->val);
4304    }
4305
4306    if (op_array->fn_flags & ZEND_ACC_ABSTRACT) {
4307        if (op_array->fn_flags & ZEND_ACC_PRIVATE) {
4308            zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private",
4309                in_interface ? "Interface" : "Abstract", ce->name->val, name->val);
4310        }
4311
4312        if (has_body) {
4313            zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body",
4314                in_interface ? "Interface" : "Abstract", ce->name->val, name->val);
4315        }
4316
4317        ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
4318    } else if (!has_body) {
4319        zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body",
4320            ce->name->val, name->val);
4321    }
4322
4323    op_array->scope = ce;
4324    op_array->function_name = zend_string_copy(name);
4325
4326    lcname = zend_string_tolower(name);
4327    lcname = zend_new_interned_string(lcname);
4328
4329    if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) {
4330        zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()",
4331            ce->name->val, name->val);
4332    }
4333
4334    if (in_interface) {
4335        if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
4336            if (!is_public || is_static) {
4337                zend_error(E_WARNING, "The magic method __call() must have "
4338                    "public visibility and cannot be static");
4339            }
4340        } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
4341            if (!is_public || !is_static) {
4342                zend_error(E_WARNING, "The magic method __callStatic() must have "
4343                    "public visibility and be static");
4344            }
4345        } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
4346            if (!is_public || is_static) {
4347                zend_error(E_WARNING, "The magic method __get() must have "
4348                    "public visibility and cannot be static");
4349            }
4350        } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
4351            if (!is_public || is_static) {
4352                zend_error(E_WARNING, "The magic method __set() must have "
4353                    "public visibility and cannot be static");
4354            }
4355        } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
4356            if (!is_public || is_static) {
4357                zend_error(E_WARNING, "The magic method __unset() must have "
4358                    "public visibility and cannot be static");
4359            }
4360        } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
4361            if (!is_public || is_static) {
4362                zend_error(E_WARNING, "The magic method __isset() must have "
4363                    "public visibility and cannot be static");
4364            }
4365        } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
4366            if (!is_public || is_static) {
4367                zend_error(E_WARNING, "The magic method __toString() must have "
4368                    "public visibility and cannot be static");
4369            }
4370        } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
4371            if (!is_public || is_static) {
4372                zend_error(E_WARNING, "The magic method __invoke() must have "
4373                    "public visibility and cannot be static");
4374            }
4375        } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
4376            if (!is_public || is_static) {
4377                zend_error(E_WARNING, "The magic method __debugInfo() must have "
4378                    "public visibility and cannot be static");
4379            }
4380        }
4381    } else {
4382        if (!in_trait && zend_string_equals_ci(lcname, ce->name)) {
4383            if (!ce->constructor) {
4384                ce->constructor = (zend_function *) op_array;
4385            }
4386        } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
4387            if (CG(active_class_entry)->constructor) {
4388                zend_error(E_STRICT, "Redefining already defined constructor for class %s",
4389                    ce->name->val);
4390            }
4391            ce->constructor = (zend_function *) op_array;
4392        } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
4393            ce->destructor = (zend_function *) op_array;
4394        } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
4395            ce->clone = (zend_function *) op_array;
4396        } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
4397            if (!is_public || is_static) {
4398                zend_error(E_WARNING, "The magic method __call() must have "
4399                    "public visibility and cannot be static");
4400            }
4401            ce->__call = (zend_function *) op_array;
4402        } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
4403            if (!is_public || !is_static) {
4404                zend_error(E_WARNING, "The magic method __callStatic() must have "
4405                    "public visibility and be static");
4406            }
4407            ce->__callstatic = (zend_function *) op_array;
4408        } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
4409            if (!is_public || is_static) {
4410                zend_error(E_WARNING, "The magic method __get() must have "
4411                    "public visibility and cannot be static");
4412            }
4413            ce->__get = (zend_function *) op_array;
4414            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4415        } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
4416            if (!is_public || is_static) {
4417                zend_error(E_WARNING, "The magic method __set() must have "
4418                    "public visibility and cannot be static");
4419            }
4420            ce->__set = (zend_function *) op_array;
4421            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4422        } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
4423            if (!is_public || is_static) {
4424                zend_error(E_WARNING, "The magic method __unset() must have "
4425                    "public visibility and cannot be static");
4426            }
4427            ce->__unset = (zend_function *) op_array;
4428            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4429        } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
4430            if (!is_public || is_static) {
4431                zend_error(E_WARNING, "The magic method __isset() must have "
4432                    "public visibility and cannot be static");
4433            }
4434            ce->__isset = (zend_function *) op_array;
4435            ce->ce_flags |= ZEND_ACC_USE_GUARDS;
4436        } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
4437            if (!is_public || is_static) {
4438                zend_error(E_WARNING, "The magic method __toString() must have "
4439                    "public visibility and cannot be static");
4440            }
4441            ce->__tostring = (zend_function *) op_array;
4442        } else if (zend_string_equals_literal(lcname, ZEND_INVOKE_FUNC_NAME)) {
4443            if (!is_public || is_static) {
4444                zend_error(E_WARNING, "The magic method __invoke() must have "
4445                    "public visibility and cannot be static");
4446            }
4447        } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
4448            if (!is_public || is_static) {
4449                zend_error(E_WARNING, "The magic method __debugInfo() must have "
4450                    "public visibility and cannot be static");
4451            }
4452            ce->__debugInfo = (zend_function *) op_array;
4453        } else if (!is_static) {
4454            op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;
4455        }
4456    }
4457
4458    zend_string_release(lcname);
4459}
4460/* }}} */
4461
4462static void zend_begin_func_decl(znode *result, zend_op_array *op_array, zend_ast_decl *decl) /* {{{ */
4463{
4464    zend_ast *params_ast = decl->child[0];
4465    zend_string *name = decl->name, *lcname;
4466    zend_op *opline;
4467
4468    op_array->function_name = name = zend_prefix_with_ns(name);
4469
4470    lcname = zend_string_tolower(name);
4471
4472    if (CG(current_import_function)) {
4473        zend_string *import_name = zend_hash_find_ptr(CG(current_import_function), lcname);
4474        if (import_name && !zend_string_equals_ci(lcname, import_name)) {
4475            zend_error(E_COMPILE_ERROR, "Cannot declare function %s "
4476                "because the name is already in use", name->val);
4477        }
4478    }
4479
4480    if (zend_string_equals_literal(lcname, ZEND_AUTOLOAD_FUNC_NAME)
4481        && zend_ast_get_list(params_ast)->children != 1
4482    ) {
4483        zend_error_noreturn(E_COMPILE_ERROR, "%s() must take exactly 1 argument",
4484            ZEND_AUTOLOAD_FUNC_NAME);
4485    }
4486
4487    if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
4488        opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL);
4489    } else {
4490        opline = get_next_op(CG(active_op_array));
4491        opline->opcode = ZEND_DECLARE_FUNCTION;
4492        opline->op2_type = IS_CONST;
4493        LITERAL_STR(opline->op2, zend_string_copy(lcname));
4494    }
4495
4496    {
4497        zend_string *key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
4498
4499        opline->op1_type = IS_CONST;
4500        LITERAL_STR(opline->op1, key);
4501
4502        zend_hash_update_ptr(CG(function_table), key, op_array);
4503    }
4504
4505    zend_string_release(lcname);
4506}
4507/* }}} */
4508
4509void zend_compile_func_decl(znode *result, zend_ast *ast) /* {{{ */
4510{
4511    zend_ast_decl *decl = (zend_ast_decl *) ast;
4512    zend_ast *params_ast = decl->child[0];
4513    zend_ast *uses_ast = decl->child[1];
4514    zend_ast *stmt_ast = decl->child[2];
4515    zend_ast *return_type_ast = decl->child[3];
4516    zend_bool is_method = decl->kind == ZEND_AST_METHOD;
4517
4518    zend_op_array *orig_op_array = CG(active_op_array);
4519    zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
4520
4521    // TODO.AST interactive (not just here - also bpc etc!)
4522
4523    init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE);
4524
4525    op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
4526    op_array->fn_flags |= decl->flags;
4527    op_array->line_start = decl->start_lineno;
4528    op_array->line_end = decl->end_lineno;
4529    if (decl->doc_comment) {
4530        op_array->doc_comment = zend_string_copy(decl->doc_comment);
4531    }
4532    if (decl->kind == ZEND_AST_CLOSURE) {
4533        op_array->fn_flags |= ZEND_ACC_CLOSURE;
4534    }
4535
4536    if (is_method) {
4537        zend_bool has_body = stmt_ast != NULL;
4538        zend_begin_method_decl(op_array, decl->name, has_body);
4539    } else {
4540        zend_begin_func_decl(result, op_array, decl);
4541    }
4542
4543    CG(active_op_array) = op_array;
4544    zend_stack_push(&CG(context_stack), (void *) &CG(context));
4545    zend_init_compiler_context();
4546
4547    if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
4548        zend_op *opline_ext = zend_emit_op(NULL, ZEND_EXT_NOP, NULL, NULL);
4549        opline_ext->lineno = decl->start_lineno;
4550    }
4551
4552    {
4553        /* Push a separator to the loop variable stack */
4554        znode dummy_var;
4555        dummy_var.op_type = IS_UNUSED;
4556
4557        zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
4558    }
4559
4560    zend_compile_params(params_ast, return_type_ast, is_method);
4561    if (uses_ast) {
4562        zend_compile_closure_uses(uses_ast);
4563    }
4564    zend_compile_stmt(stmt_ast);
4565
4566    if (is_method) {
4567        zend_check_magic_method_implementation(
4568            CG(active_class_entry), (zend_function *) op_array, E_COMPILE_ERROR);
4569    }
4570
4571    zend_do_extended_info();
4572    zend_emit_final_return(NULL);
4573
4574    pass_two(CG(active_op_array));
4575    zend_release_labels(0);
4576
4577    /* Pop the loop variable stack separator */
4578    zend_stack_del_top(&CG(loop_var_stack));
4579
4580    CG(active_op_array) = orig_op_array;
4581}
4582/* }}} */
4583
4584void zend_compile_prop_decl(zend_ast *ast) /* {{{ */
4585{
4586    zend_ast_list *list = zend_ast_get_list(ast);
4587    uint32_t flags = list->attr;
4588    zend_class_entry *ce = CG(active_class_entry);
4589    uint32_t i, children = list->children;
4590    zend_string *doc_comment = NULL;
4591
4592    if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4593        zend_error_noreturn(E_COMPILE_ERROR, "Interfaces may not include member variables");
4594    }
4595
4596    if (flags & ZEND_ACC_ABSTRACT) {
4597        zend_error_noreturn(E_COMPILE_ERROR, "Properties cannot be declared abstract");
4598    }
4599
4600    /* Doc comment has been appended as last element in property list */
4601    if (list->child[children - 1]->kind == ZEND_AST_ZVAL) {
4602        doc_comment = zend_string_copy(zend_ast_get_str(list->child[children - 1]));
4603        children -= 1;
4604    }
4605
4606    for (i = 0; i < children; ++i) {
4607        zend_ast *prop_ast = list->child[i];
4608        zend_ast *name_ast = prop_ast->child[0];
4609        zend_ast *value_ast = prop_ast->child[1];
4610        zend_string *name = zend_ast_get_str(name_ast);
4611        zval value_zv;
4612
4613        if (flags & ZEND_ACC_FINAL) {
4614            zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare property %s::$%s final, "
4615                "the final modifier is allowed only for methods and classes",
4616                ce->name->val, name->val);
4617        }
4618
4619        if (zend_hash_exists(&ce->properties_info, name)) {
4620            zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s",
4621                ce->name->val, name->val);
4622        }
4623
4624        if (value_ast) {
4625            zend_const_expr_to_zval(&value_zv, value_ast);
4626        } else {
4627            ZVAL_NULL(&value_zv);
4628        }
4629
4630        name = zend_new_interned_string_safe(name);
4631        zend_declare_property_ex(ce, name, &value_zv, flags, doc_comment);
4632
4633        /* Doc comment is only assigned to first property */
4634        doc_comment = NULL;
4635    }
4636}
4637/* }}} */
4638
4639void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
4640{
4641    zend_ast_list *list = zend_ast_get_list(ast);
4642    zend_class_entry *ce = CG(active_class_entry);
4643    uint32_t i;
4644
4645    if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
4646        zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
4647        return;
4648    }
4649
4650    for (i = 0; i < list->children; ++i) {
4651        zend_ast *const_ast = list->child[i];
4652        zend_ast *name_ast = const_ast->child[0];
4653        zend_ast *value_ast = const_ast->child[1];
4654        zend_string *name = zend_ast_get_str(name_ast);
4655        zval value_zv;
4656
4657        zend_const_expr_to_zval(&value_zv, value_ast);
4658
4659        name = zend_new_interned_string_safe(name);
4660        if (zend_hash_add(&ce->constants_table, name, &value_zv) == NULL) {
4661            zend_error_noreturn(E_COMPILE_ERROR, "Cannot redefine class constant %s::%s",
4662                ce->name->val, name->val);
4663        }
4664
4665        if (Z_CONSTANT(value_zv)) {
4666            ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
4667        }
4668    }
4669}
4670/* }}} */
4671
4672static zend_trait_method_reference *zend_compile_method_ref(zend_ast *ast) /* {{{ */
4673{
4674    zend_ast *class_ast = ast->child[0];
4675    zend_ast *method_ast = ast->child[1];
4676
4677    zend_trait_method_reference *method_ref = emalloc(sizeof(zend_trait_method_reference));
4678    method_ref->ce = NULL;
4679    method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast));
4680
4681    if (class_ast) {
4682        method_ref->class_name = zend_resolve_class_name_ast(class_ast);
4683    } else {
4684        method_ref->class_name = NULL;
4685    }
4686
4687    return method_ref;
4688}
4689/* }}} */
4690
4691static zend_string **zend_compile_name_list(zend_ast *ast) /* {{{ */
4692{
4693    zend_ast_list *list = zend_ast_get_list(ast);
4694    zend_string **names = safe_emalloc(sizeof(zend_string *), list->children + 1, 0);
4695    uint32_t i;
4696
4697    for (i = 0; i < list->children; ++i) {
4698        zend_ast *name_ast = list->child[i];
4699        names[i] = zend_resolve_class_name_ast(name_ast);
4700    }
4701
4702    names[list->children] = NULL;
4703
4704    return names;
4705}
4706/* }}} */
4707
4708static void zend_compile_trait_precedence(zend_ast *ast) /* {{{ */
4709{
4710    zend_ast *method_ref_ast = ast->child[0];
4711    zend_ast *insteadof_ast = ast->child[1];
4712
4713    zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence));
4714    precedence->trait_method = zend_compile_method_ref(method_ref_ast);
4715    precedence->exclude_from_classes
4716        = (void *) zend_compile_name_list(insteadof_ast);
4717
4718    zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence);
4719}
4720/* }}} */
4721
4722static void zend_compile_trait_alias(zend_ast *ast) /* {{{ */
4723{
4724    zend_ast *method_ref_ast = ast->child[0];
4725    zend_ast *alias_ast = ast->child[1];
4726    uint32_t modifiers = ast->attr;
4727
4728    zend_trait_alias *alias;
4729
4730    if (modifiers == ZEND_ACC_STATIC) {
4731        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as method modifier");
4732    } else if (modifiers == ZEND_ACC_ABSTRACT) {
4733        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as method modifier");
4734    } else if (modifiers == ZEND_ACC_FINAL) {
4735        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as method modifier");
4736    }
4737
4738    alias = emalloc(sizeof(zend_trait_alias));
4739    alias->trait_method = zend_compile_method_ref(method_ref_ast);
4740    alias->modifiers = modifiers;
4741
4742    if (alias_ast) {
4743        alias->alias = zend_string_copy(zend_ast_get_str(alias_ast));
4744    } else {
4745        alias->alias = NULL;
4746    }
4747
4748    zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias);
4749}
4750/* }}} */
4751
4752void zend_compile_use_trait(zend_ast *ast) /* {{{ */
4753{
4754    zend_ast_list *traits = zend_ast_get_list(ast->child[0]);
4755    zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
4756    zend_class_entry *ce = CG(active_class_entry);
4757    zend_op *opline;
4758    uint32_t i;
4759
4760    for (i = 0; i < traits->children; ++i) {
4761        zend_ast *trait_ast = traits->child[i];
4762        zend_string *name = zend_ast_get_str(trait_ast);
4763
4764        if (ce->ce_flags & ZEND_ACC_INTERFACE) {
4765            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. "
4766                "%s is used in %s", name->val, ce->name->val);
4767        }
4768
4769        switch (zend_get_class_fetch_type(name)) {
4770            case ZEND_FETCH_CLASS_SELF:
4771            case ZEND_FETCH_CLASS_PARENT:
4772            case ZEND_FETCH_CLASS_STATIC:
4773                zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as trait name "
4774                    "as it is reserved", name->val);
4775                break;
4776        }
4777
4778        opline = get_next_op(CG(active_op_array));
4779        opline->opcode = ZEND_ADD_TRAIT;
4780        SET_NODE(opline->op1, &CG(implementing_class));
4781        opline->op2_type = IS_CONST;
4782        opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
4783            zend_resolve_class_name_ast(trait_ast));
4784
4785        ce->num_traits++;
4786    }
4787
4788    if (!adaptations) {
4789        return;
4790    }
4791
4792    for (i = 0; i < adaptations->children; ++i) {
4793        zend_ast *adaptation_ast = adaptations->child[i];
4794        switch (adaptation_ast->kind) {
4795            case ZEND_AST_TRAIT_PRECEDENCE:
4796                zend_compile_trait_precedence(adaptation_ast);
4797                break;
4798            case ZEND_AST_TRAIT_ALIAS:
4799                zend_compile_trait_alias(adaptation_ast);
4800                break;
4801            EMPTY_SWITCH_DEFAULT_CASE()
4802        }
4803    }
4804}
4805/* }}} */
4806
4807void zend_compile_implements(znode *class_node, zend_ast *ast) /* {{{ */
4808{
4809    zend_ast_list *list = zend_ast_get_list(ast);
4810    uint32_t i;
4811    for (i = 0; i < list->children; ++i) {
4812        zend_ast *class_ast = list->child[i];
4813        zend_string *name = zend_ast_get_str(class_ast);
4814
4815        zend_op *opline;
4816
4817        if (!zend_is_const_default_class_ref(class_ast)) {
4818            zend_error_noreturn(E_COMPILE_ERROR,
4819                "Cannot use '%s' as interface name as it is reserved", name->val);
4820        }
4821
4822        opline = zend_emit_op(NULL, ZEND_ADD_INTERFACE, class_node, NULL);
4823        opline->op2_type = IS_CONST;
4824        opline->op2.constant = zend_add_class_name_literal(CG(active_op_array),
4825            zend_resolve_class_name_ast(class_ast));
4826
4827        CG(active_class_entry)->num_interfaces++;
4828    }
4829}
4830/* }}} */
4831
4832void zend_compile_class_decl(zend_ast *ast) /* {{{ */
4833{
4834    zend_ast_decl *decl = (zend_ast_decl *) ast;
4835    zend_ast *extends_ast = decl->child[0];
4836    zend_ast *implements_ast = decl->child[1];
4837    zend_ast *stmt_ast = decl->child[2];
4838
4839    zend_string *name = decl->name, *lcname, *import_name = NULL;
4840    zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
4841    zend_op *opline;
4842    znode declare_node, extends_node;
4843
4844    if (CG(active_class_entry)) {
4845        zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested");
4846        return;
4847    }
4848
4849    if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
4850        zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as class name as it is reserved",
4851            name->val);
4852    }
4853
4854    lcname = zend_string_tolower(name);
4855
4856    if (CG(current_import)) {
4857        import_name = zend_hash_find_ptr(CG(current_import), lcname);
4858    }
4859
4860    zend_assert_valid_class_name(name);
4861
4862    if (CG(current_namespace)) {
4863        name = zend_prefix_with_ns(name);
4864
4865        zend_string_release(lcname);
4866        lcname = zend_string_tolower(name);
4867    } else {
4868        zend_string_addref(name);
4869    }
4870
4871    if (import_name && !zend_string_equals_ci(lcname, import_name)) {
4872        zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare class %s "
4873            "because the name is already in use", name->val);
4874    }
4875
4876    name = zend_new_interned_string(name);
4877    lcname = zend_new_interned_string(lcname);
4878
4879    ce->type = ZEND_USER_CLASS;
4880    ce->name = name;
4881    zend_initialize_class_data(ce, 1);
4882
4883    ce->ce_flags |= decl->flags;
4884    ce->info.user.filename = zend_get_compiled_filename();
4885    ce->info.user.line_start = decl->start_lineno;
4886    ce->info.user.line_end = decl->end_lineno;
4887    if (decl->doc_comment) {
4888        ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
4889    }
4890
4891    if (extends_ast) {
4892        if (!zend_is_const_default_class_ref(extends_ast)) {
4893            zend_string *extends_name = zend_ast_get_str(extends_ast);
4894            zend_error_noreturn(E_COMPILE_ERROR,
4895                "Cannot use '%s' as class name as it is reserved", extends_name->val);
4896        }
4897
4898        zend_compile_class_ref(&extends_node, extends_ast);
4899    }
4900
4901    opline = get_next_op(CG(active_op_array));
4902    zend_make_var_result(&declare_node, opline);
4903
4904    // TODO.AST drop this
4905    GET_NODE(&CG(implementing_class), opline->result);
4906
4907    opline->op2_type = IS_CONST;
4908    LITERAL_STR(opline->op2, lcname);
4909
4910    if (extends_ast) {
4911        opline->opcode = ZEND_DECLARE_INHERITED_CLASS;
4912        opline->extended_value = extends_node.u.op.var;
4913    } else {
4914        opline->opcode = ZEND_DECLARE_CLASS;
4915    }
4916
4917    {
4918        zend_string *key = zend_build_runtime_definition_key(lcname, decl->lex_pos);
4919
4920        opline->op1_type = IS_CONST;
4921        LITERAL_STR(opline->op1, key);
4922
4923        zend_hash_update_ptr(CG(class_table), key, ce);
4924    }
4925
4926    CG(active_class_entry) = ce;
4927
4928    if (implements_ast) {
4929        zend_compile_implements(&declare_node, implements_ast);
4930    }
4931
4932    zend_compile_stmt(stmt_ast);
4933
4934    if (ce->constructor) {
4935        ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
4936        if (ce->constructor->common.fn_flags & ZEND_ACC_STATIC) {
4937            zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
4938                ce->name->val, ce->constructor->common.function_name->val);
4939        }
4940        if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
4941            zend_error_noreturn(E_COMPILE_ERROR,
4942                "Constructor %s::%s() cannot declare a return type",
4943                ce->name->val, ce->constructor->common.function_name->val);
4944        }
4945    }
4946    if (ce->destructor) {
4947        ce->destructor->common.fn_flags |= ZEND_ACC_DTOR;
4948        if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
4949            zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
4950                ce->name->val, ce->destructor->common.function_name->val);
4951        } else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
4952            zend_error_noreturn(E_COMPILE_ERROR,
4953                "Destructor %s::%s() cannot declare a return type",
4954                ce->name->val, ce->destructor->common.function_name->val);
4955        }
4956    }
4957    if (ce->clone) {
4958        ce->clone->common.fn_flags |= ZEND_ACC_CLONE;
4959        if (ce->clone->common.fn_flags & ZEND_ACC_STATIC) {
4960            zend_error_noreturn(E_COMPILE_ERROR, "Clone method %s::%s() cannot be static",
4961                ce->name->val, ce->clone->common.function_name->val);
4962        } else if (ce->clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
4963            zend_error_noreturn(E_COMPILE_ERROR,
4964                "%s::%s() cannot declare a return type",
4965                ce->name->val, ce->clone->common.function_name->val);
4966        }
4967    }
4968
4969    /* Check for traits and proceed like with interfaces.
4970     * The only difference will be a combined handling of them in the end.
4971     * Thus, we need another opcode here. */
4972    if (ce->num_traits > 0) {
4973        ce->traits = NULL;
4974        ce->num_traits = 0;
4975        ce->ce_flags |= ZEND_ACC_IMPLEMENT_TRAITS;
4976
4977        zend_emit_op(NULL, ZEND_BIND_TRAITS, &declare_node, NULL);
4978    }
4979
4980    if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
4981        && (extends_ast || ce->num_interfaces > 0)
4982    ) {
4983        zend_verify_abstract_class(ce);
4984        if (ce->num_interfaces && !(ce->ce_flags & ZEND_ACC_IMPLEMENT_TRAITS)) {
4985            zend_emit_op(NULL, ZEND_VERIFY_ABSTRACT_CLASS, &declare_node, NULL);
4986        }
4987    }
4988
4989    /* Inherit interfaces; reset number to zero, we need it for above check and
4990     * will restore it during actual implementation.
4991     * The ZEND_ACC_IMPLEMENT_INTERFACES flag disables double call to
4992     * zend_verify_abstract_class() */
4993    if (ce->num_interfaces > 0) {
4994        ce->interfaces = NULL;
4995        ce->num_interfaces = 0;
4996        ce->ce_flags |= ZEND_ACC_IMPLEMENT_INTERFACES;
4997    }
4998
4999    CG(active_class_entry) = NULL;
5000}
5001/* }}} */
5002
5003static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */
5004{
5005    switch (type) {
5006        case T_CLASS:
5007            if (!CG(current_import)) {
5008                CG(current_import) = emalloc(sizeof(HashTable));
5009                zend_hash_init(CG(current_import), 8, NULL, str_dtor, 0);
5010            }
5011            return CG(current_import);
5012        case T_FUNCTION:
5013            if (!CG(current_import_function)) {
5014                CG(current_import_function) = emalloc(sizeof(HashTable));
5015                zend_hash_init(CG(current_import_function), 8, NULL, str_dtor, 0);
5016            }
5017            return CG(current_import_function);
5018        case T_CONST:
5019            if (!CG(current_import_const)) {
5020                CG(current_import_const) = emalloc(sizeof(HashTable));
5021                zend_hash_init(CG(current_import_const), 8, NULL, str_dtor, 0);
5022            }
5023            return CG(current_import_const);
5024        EMPTY_SWITCH_DEFAULT_CASE()
5025    }
5026
5027    return NULL;
5028}
5029/* }}} */
5030
5031static char *zend_get_use_type_str(uint32_t type) /* {{{ */
5032{
5033    switch (type) {
5034        case T_CLASS:
5035            return "";
5036        case T_FUNCTION:
5037            return " function";
5038        case T_CONST:
5039            return " const";
5040        EMPTY_SWITCH_DEFAULT_CASE()
5041    }
5042
5043    return " unknown";
5044}
5045/* }}} */
5046
5047static void zend_check_already_in_use(uint32_t type, zend_string *old_name, zend_string *new_name, zend_string *check_name) /* {{{ */
5048{
5049    if (zend_string_equals_ci(old_name, check_name)) {
5050        return;
5051    }
5052
5053    zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
5054        "is already in use", zend_get_use_type_str(type), old_name->val, new_name->val);
5055}
5056/* }}} */
5057
5058void zend_compile_use(zend_ast *ast) /* {{{ */
5059{
5060    zend_ast_list *list = zend_ast_get_list(ast);
5061    uint32_t i;
5062    zend_string *current_ns = CG(current_namespace);
5063    uint32_t type = ast->attr;
5064    HashTable *current_import = zend_get_import_ht(type);
5065    zend_bool case_sensitive = type == T_CONST;
5066
5067    for (i = 0; i < list->children; ++i) {
5068        zend_ast *use_ast = list->child[i];
5069        zend_ast *old_name_ast = use_ast->child[0];
5070        zend_ast *new_name_ast = use_ast->child[1];
5071        zend_string *old_name = zend_ast_get_str(old_name_ast);
5072        zend_string *new_name, *lookup_name;
5073
5074        if (new_name_ast) {
5075            new_name = zend_string_copy(zend_ast_get_str(new_name_ast));
5076        } else {
5077            const char *unqualified_name;
5078            size_t unqualified_name_len;
5079            if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) {
5080                /* The form "use A\B" is equivalent to "use A\B as B" */
5081                new_name = zend_string_init(unqualified_name, unqualified_name_len, 0);
5082            } else {
5083                new_name = zend_string_copy(old_name);
5084
5085                if (!current_ns) {
5086                    if (type == T_CLASS && zend_string_equals_literal(new_name, "strict")) {
5087                        zend_error_noreturn(E_COMPILE_ERROR,
5088                            "You seem to be trying to use a different language...");
5089                    }
5090
5091                    zend_error(E_WARNING, "The use statement with non-compound name '%s' "
5092                        "has no effect", new_name->val);
5093                }
5094            }
5095        }
5096
5097        if (type == T_CLASS) {
5098            zend_assert_valid_class_name(new_name);
5099        }
5100
5101        if (case_sensitive) {
5102            lookup_name = zend_string_copy(new_name);
5103        } else {
5104            lookup_name = zend_string_tolower(new_name);
5105        }
5106
5107        if (type == T_CLASS && (zend_string_equals_literal(lookup_name, "self")
5108            || zend_string_equals_literal(lookup_name, "parent"))
5109        ) {
5110            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
5111                "is a special class name", old_name->val, new_name->val, new_name->val);
5112        }
5113
5114        if (current_ns) {
5115            zend_string *ns_name = zend_string_alloc(current_ns->len + 1 + new_name->len, 0);
5116            zend_str_tolower_copy(ns_name->val, current_ns->val, current_ns->len);
5117            ns_name->val[current_ns->len] = '\\';
5118            memcpy(ns_name->val + current_ns->len + 1, lookup_name->val, lookup_name->len);
5119
5120            if (zend_hash_exists(CG(class_table), ns_name)) {
5121                zend_check_already_in_use(type, old_name, new_name, ns_name);
5122            }
5123
5124            zend_string_free(ns_name);
5125        } else {
5126            switch (type) {
5127                case T_CLASS:
5128                {
5129                    zend_class_entry *ce = zend_hash_find_ptr(CG(class_table), lookup_name);
5130                    if (ce && ce->type == ZEND_USER_CLASS
5131                        && ce->info.user.filename == CG(compiled_filename)
5132                    ) {
5133                        zend_check_already_in_use(type, old_name, new_name, lookup_name);
5134                    }
5135                    break;
5136                }
5137                case T_FUNCTION:
5138                {
5139                    zend_function *fn = zend_hash_find_ptr(CG(function_table), lookup_name);
5140                    if (fn && fn->type == ZEND_USER_FUNCTION
5141                        && fn->op_array.filename == CG(compiled_filename)
5142                    ) {
5143                        zend_check_already_in_use(type, old_name, new_name, lookup_name);
5144                    }
5145                    break;
5146                }
5147                case T_CONST:
5148                {
5149                    zend_string *filename = zend_hash_find_ptr(&CG(const_filenames), lookup_name);
5150                    if (filename && filename == CG(compiled_filename)) {
5151                        zend_check_already_in_use(type, old_name, new_name, lookup_name);
5152                    }
5153                    break;
5154                }
5155                EMPTY_SWITCH_DEFAULT_CASE()
5156            }
5157        }
5158
5159        zend_string_addref(old_name);
5160        if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) {
5161            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name "
5162                "is already in use", zend_get_use_type_str(type), old_name->val, new_name->val);
5163        }
5164
5165        zend_string_release(lookup_name);
5166        zend_string_release(new_name);
5167    }
5168}
5169/* }}} */
5170
5171void zend_compile_group_use(zend_ast *ast) /* {{{ */
5172{
5173    uint32_t i;
5174    zend_string *ns = zend_ast_get_str(ast->child[0]);
5175    zend_ast_list *list = zend_ast_get_list(ast->child[1]);
5176
5177    for (i = 0; i < list->children; i++) {
5178        zend_ast *inline_use, *use = list->child[i];
5179        zval *name_zval = zend_ast_get_zval(use->child[0]);
5180        zend_string *name = Z_STR_P(name_zval);
5181        zend_string *compound_ns = zend_concat_names(ns->val, ns->len, name->val, name->len);
5182        zend_string_release(name);
5183        ZVAL_STR(name_zval, compound_ns);
5184        inline_use = zend_ast_create_list(1, ZEND_AST_USE, use);
5185        inline_use->attr = ast->attr ? ast->attr : use->attr;
5186        zend_compile_use(inline_use);
5187    }
5188}
5189/* }}} */
5190
5191
5192void zend_compile_const_decl(zend_ast *ast) /* {{{ */
5193{
5194    zend_ast_list *list = zend_ast_get_list(ast);
5195    uint32_t i;
5196    for (i = 0; i < list->children; ++i) {
5197        zend_ast *const_ast = list->child[i];
5198        zend_ast *name_ast = const_ast->child[0];
5199        zend_ast *value_ast = const_ast->child[1];
5200        zend_string *name = zend_ast_get_str(name_ast);
5201
5202        zend_string *import_name;
5203        znode name_node, value_node;
5204        zval *value_zv = &value_node.u.constant;
5205
5206        value_node.op_type = IS_CONST;
5207        zend_const_expr_to_zval(value_zv, value_ast);
5208
5209        if (zend_lookup_reserved_const(name->val, name->len)) {
5210            zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare constant '%s'", name->val);
5211        }
5212
5213        name = zend_prefix_with_ns(name);
5214        name = zend_new_interned_string(name);
5215
5216        if (CG(current_import_const)
5217            && (import_name = zend_hash_find_ptr(CG(current_import_const), name))
5218        ) {
5219            if (!zend_string_equals(import_name, name)) {
5220                zend_error(E_COMPILE_ERROR, "Cannot declare const %s because "
5221                    "the name is already in use", name->val);
5222            }
5223        }
5224
5225        name_node.op_type = IS_CONST;
5226        ZVAL_STR(&name_node.u.constant, name);
5227
5228        zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node);
5229
5230        zend_hash_add_ptr(&CG(const_filenames), name, CG(compiled_filename));
5231    }
5232}
5233/* }}}*/
5234
5235void zend_compile_namespace(zend_ast *ast) /* {{{ */
5236{
5237    zend_ast *name_ast = ast->child[0];
5238    zend_ast *stmt_ast = ast->child[1];
5239    zend_string *name;
5240    zend_bool with_bracket = stmt_ast != NULL;
5241
5242    /* handle mixed syntax declaration or nested namespaces */
5243    if (!CG(has_bracketed_namespaces)) {
5244        if (CG(current_namespace)) {
5245            /* previous namespace declarations were unbracketed */
5246            if (with_bracket) {
5247                zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
5248                    "with unbracketed namespace declarations");
5249            }
5250        }
5251    } else {
5252        /* previous namespace declarations were bracketed */
5253        if (!with_bracket) {
5254            zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
5255                "with unbracketed namespace declarations");
5256        } else if (CG(current_namespace) || CG(in_namespace)) {
5257            zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
5258        }
5259    }
5260
5261    if (((!with_bracket && !CG(current_namespace))
5262         || (with_bracket && !CG(has_bracketed_namespaces))) && CG(active_op_array)->last > 0
5263    ) {
5264        /* ignore ZEND_EXT_STMT and ZEND_TICKS */
5265        uint32_t num = CG(active_op_array)->last;
5266        while (num > 0 &&
5267               (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
5268                CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
5269            --num;
5270        }
5271        if (num > 0) {
5272            zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be "
5273                "the very first statement in the script");
5274        }
5275    }
5276
5277    if (CG(current_namespace)) {
5278        zend_string_release(CG(current_namespace));
5279    }
5280
5281    if (name_ast) {
5282        name = zend_ast_get_str(name_ast);
5283
5284        if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
5285            zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", name->val);
5286        }
5287
5288        CG(current_namespace) = zend_string_copy(name);
5289    } else {
5290        CG(current_namespace) = NULL;
5291    }
5292
5293    zend_reset_import_tables();
5294
5295    CG(in_namespace) = 1;
5296    if (with_bracket) {
5297        CG(has_bracketed_namespaces) = 1;
5298    }
5299
5300    if (stmt_ast) {
5301        zend_compile_top_stmt(stmt_ast);
5302        zend_end_namespace();
5303    }
5304}
5305/* }}} */
5306
5307void zend_compile_halt_compiler(zend_ast *ast) /* {{{ */
5308{
5309    zend_ast *offset_ast = ast->child[0];
5310    zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast));
5311
5312    zend_string *filename, *name;
5313    const char const_name[] = "__COMPILER_HALT_OFFSET__";
5314
5315    if (CG(has_bracketed_namespaces) && CG(in_namespace)) {
5316        zend_error_noreturn(E_COMPILE_ERROR,
5317            "__HALT_COMPILER() can only be used from the outermost scope");
5318    }
5319
5320    filename = zend_get_compiled_filename();
5321    name = zend_mangle_property_name(const_name, sizeof(const_name) - 1,
5322        filename->val, filename->len, 0);
5323
5324    zend_register_long_constant(name->val, name->len, offset, CONST_CS, 0);
5325    zend_string_release(name);
5326}
5327/* }}} */
5328
5329static zend_bool zend_try_ct_eval_magic_const(zval *zv, zend_ast *ast) /* {{{ */
5330{
5331    zend_op_array *op_array = CG(active_op_array);
5332    zend_class_entry *ce = CG(active_class_entry);
5333
5334    switch (ast->attr) {
5335        case T_LINE:
5336            ZVAL_LONG(zv, ast->lineno);
5337            break;
5338        case T_FILE:
5339            ZVAL_STR_COPY(zv, CG(compiled_filename));
5340            break;
5341        case T_DIR:
5342        {
5343            zend_string *filename = CG(compiled_filename);
5344            zend_string *dirname = zend_string_init(filename->val, filename->len, 0);
5345            zend_dirname(dirname->val, dirname->len);
5346
5347            if (strcmp(dirname->val, ".") == 0) {
5348                dirname = zend_string_extend(dirname, MAXPATHLEN, 0);
5349#if HAVE_GETCWD
5350                VCWD_GETCWD(dirname->val, MAXPATHLEN);
5351#elif HAVE_GETWD
5352                VCWD_GETWD(dirname->val);
5353#endif
5354            }
5355
5356            dirname->len = strlen(dirname->val);
5357            ZVAL_STR(zv, dirname);
5358            break;
5359        }
5360        case T_FUNC_C:
5361            if (op_array && op_array->function_name) {
5362                ZVAL_STR_COPY(zv, op_array->function_name);
5363            } else {
5364                ZVAL_EMPTY_STRING(zv);
5365            }
5366            break;
5367        case T_METHOD_C:
5368            if (ce) {
5369                if (op_array && op_array->function_name) {
5370                    ZVAL_NEW_STR(zv, zend_concat3(ce->name->val, ce->name->len, "::", 2,
5371                        op_array->function_name->val, op_array->function_name->len));
5372                } else {
5373                    ZVAL_STR_COPY(zv, ce->name);
5374                }
5375            } else if (op_array && op_array->function_name) {
5376                ZVAL_STR_COPY(zv, op_array->function_name);
5377            } else {
5378                ZVAL_EMPTY_STRING(zv);
5379            }
5380            break;
5381        case T_CLASS_C:
5382            if (ce) {
5383                if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
5384                    return 0;
5385                } else {
5386                    ZVAL_STR_COPY(zv, ce->name);
5387                }
5388            } else {
5389                ZVAL_EMPTY_STRING(zv);
5390            }
5391            break;
5392        case T_TRAIT_C:
5393            if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
5394                ZVAL_STR_COPY(zv, ce->name);
5395            } else {
5396                ZVAL_EMPTY_STRING(zv);
5397            }
5398            break;
5399        case T_NS_C:
5400            if (CG(current_namespace)) {
5401                ZVAL_STR_COPY(zv, CG(current_namespace));
5402            } else {
5403                ZVAL_EMPTY_STRING(zv);
5404            }
5405            break;
5406        EMPTY_SWITCH_DEFAULT_CASE()
5407    }
5408
5409    return 1;
5410}
5411/* }}} */
5412
5413static inline void zend_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */
5414{
5415    binary_op_type fn = get_binary_op(opcode);
5416    fn(result, op1, op2);
5417}
5418/* }}} */
5419
5420static inline void zend_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */
5421{
5422    binary_op_type fn = kind == ZEND_AST_UNARY_PLUS
5423        ? add_function : sub_function;
5424
5425    zval left;
5426    ZVAL_LONG(&left, 0);
5427    fn(result, &left, op);
5428}
5429/* }}} */
5430
5431static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */
5432{
5433    binary_op_type fn = kind == ZEND_AST_GREATER
5434        ? is_smaller_function : is_smaller_or_equal_function;
5435    fn(result, op2, op1);
5436}
5437/* }}} */
5438
5439static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */
5440{
5441    zend_ast_list *list = zend_ast_get_list(ast);
5442    uint32_t i;
5443
5444    /* First ensure that *all* child nodes are constant and by-val */
5445    for (i = 0; i < list->children; ++i) {
5446        zend_ast *elem_ast = list->child[i];
5447        zend_bool by_ref = elem_ast->attr;
5448        zend_eval_const_expr(&elem_ast->child[0]);
5449        zend_eval_const_expr(&elem_ast->child[1]);
5450
5451        if (by_ref || elem_ast->child[0]->kind != ZEND_AST_ZVAL
5452            || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL)
5453        ) {
5454            return 0;
5455        }
5456    }
5457
5458    array_init_size(result, list->children);
5459</