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