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