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