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