/src/php-src/Zend/zend_compile.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright © Zend Technologies Ltd., a subsidiary company of | |
6 | | | Perforce Software, Inc., and Contributors. | |
7 | | +----------------------------------------------------------------------+ |
8 | | | This source file is subject to the Modified BSD License that is | |
9 | | | bundled with this package in the file LICENSE, and is available | |
10 | | | through the World Wide Web at <https://www.php.net/license/>. | |
11 | | | | |
12 | | | SPDX-License-Identifier: BSD-3-Clause | |
13 | | +----------------------------------------------------------------------+ |
14 | | | Authors: Andi Gutmans <andi@php.net> | |
15 | | | Zeev Suraski <zeev@php.net> | |
16 | | | Nikita Popov <nikic@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include <zend_language_parser.h> |
21 | | #include "zend.h" |
22 | | #include "zend_ast.h" |
23 | | #include "zend_attributes.h" |
24 | | #include "zend_compile.h" |
25 | | #include "zend_constants.h" |
26 | | #include "zend_llist.h" |
27 | | #include "zend_API.h" |
28 | | #include "zend_exceptions.h" |
29 | | #include "zend_interfaces.h" |
30 | | #include "zend_types.h" |
31 | | #include "zend_virtual_cwd.h" |
32 | | #include "zend_multibyte.h" |
33 | | #include "zend_language_scanner.h" |
34 | | #include "zend_inheritance.h" |
35 | | #include "zend_vm.h" |
36 | | #include "zend_enum.h" |
37 | | #include "zend_observer.h" |
38 | | #include "zend_call_stack.h" |
39 | | #include "zend_frameless_function.h" |
40 | | #include "zend_property_hooks.h" |
41 | | |
42 | 0 | #define SET_NODE(target, src) do { \ |
43 | 0 | target ## _type = (src)->op_type; \ |
44 | 0 | if ((src)->op_type == IS_CONST) { \ |
45 | 0 | target.constant = zend_add_literal(&(src)->u.constant); \ |
46 | 0 | } else { \ |
47 | 0 | target = (src)->u.op; \ |
48 | 0 | } \ |
49 | 0 | } while (0) |
50 | | |
51 | 0 | #define GET_NODE(target, src) do { \ |
52 | 0 | (target)->op_type = src ## _type; \ |
53 | 0 | if ((target)->op_type == IS_CONST) { \ |
54 | 0 | ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \ |
55 | 0 | } else { \ |
56 | 0 | (target)->u.op = src; \ |
57 | 0 | } \ |
58 | 0 | } while (0) |
59 | | |
60 | 0 | #define FC(member) (CG(file_context).member) |
61 | | |
62 | | typedef struct _zend_loop_var { |
63 | | uint8_t opcode; |
64 | | uint8_t var_type; |
65 | | uint32_t var_num; |
66 | | uint32_t try_catch_offset; |
67 | | } zend_loop_var; |
68 | | |
69 | 0 | static inline uint32_t zend_alloc_cache_slots(unsigned count) { |
70 | 0 | if (count == 0) { |
71 | | /* Even if no cache slots are desired, the VM handler may still want to acquire |
72 | | * CACHE_ADDR() unconditionally. Returning zero makes sure that the address |
73 | | * calculation is still legal and ubsan does not complain. */ |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | 0 | zend_op_array *op_array = CG(active_op_array); |
78 | 0 | uint32_t ret = op_array->cache_size; |
79 | 0 | op_array->cache_size += count * sizeof(void*); |
80 | 0 | return ret; |
81 | 0 | } |
82 | | |
83 | 0 | static inline uint32_t zend_alloc_cache_slot(void) { |
84 | 0 | return zend_alloc_cache_slots(1); |
85 | 0 | } |
86 | | |
87 | | ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type); |
88 | | ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position); |
89 | | |
90 | | #ifndef ZTS |
91 | | ZEND_API zend_compiler_globals compiler_globals; |
92 | | ZEND_API zend_executor_globals executor_globals; |
93 | | #endif |
94 | | |
95 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2); |
96 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast); |
97 | | static void zend_eval_const_expr(zend_ast **ast_ptr); |
98 | | |
99 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
100 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
101 | | static void zend_compile_expr(znode *result, zend_ast *ast); |
102 | | static void zend_compile_stmt(zend_ast *ast); |
103 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type); |
104 | | |
105 | | #ifdef ZEND_CHECK_STACK_LIMIT |
106 | | zend_never_inline static void zend_stack_limit_error(void) |
107 | 0 | { |
108 | 0 | size_t max_stack_size = 0; |
109 | 0 | if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) { |
110 | 0 | max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit)); |
111 | 0 | } |
112 | |
|
113 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
114 | 0 | "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression", |
115 | 0 | max_stack_size); |
116 | 0 | } |
117 | | |
118 | | static void zend_check_stack_limit(void) |
119 | 0 | { |
120 | 0 | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
121 | 0 | zend_stack_limit_error(); |
122 | 0 | } |
123 | 0 | } |
124 | | #else /* ZEND_CHECK_STACK_LIMIT */ |
125 | | static void zend_check_stack_limit(void) |
126 | | { |
127 | | } |
128 | | #endif /* ZEND_CHECK_STACK_LIMIT */ |
129 | | |
130 | | static void init_op(zend_op *op) |
131 | 0 | { |
132 | 0 | MAKE_NOP(op); |
133 | 0 | op->extended_value = 0; |
134 | 0 | op->lineno = CG(zend_lineno); |
135 | | #ifdef ZEND_VERIFY_TYPE_INFERENCE |
136 | | op->op1_use_type = 0; |
137 | | op->op2_use_type = 0; |
138 | | op->result_use_type = 0; |
139 | | op->op1_def_type = 0; |
140 | | op->op2_def_type = 0; |
141 | | op->result_def_type = 0; |
142 | | #endif |
143 | 0 | } |
144 | | |
145 | | static zend_always_inline uint32_t get_next_op_number(void) |
146 | 0 | { |
147 | 0 | return CG(active_op_array)->last; |
148 | 0 | } |
149 | | |
150 | | static zend_op *get_next_op(void) |
151 | 0 | { |
152 | 0 | zend_op_array *op_array = CG(active_op_array); |
153 | 0 | uint32_t next_op_num = op_array->last++; |
154 | 0 | zend_op *next_op; |
155 | |
|
156 | 0 | if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) { |
157 | 0 | CG(context).opcodes_size *= 4; |
158 | 0 | op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op)); |
159 | 0 | } |
160 | |
|
161 | 0 | next_op = &(op_array->opcodes[next_op_num]); |
162 | |
|
163 | 0 | init_op(next_op); |
164 | |
|
165 | 0 | return next_op; |
166 | 0 | } |
167 | | |
168 | | static zend_brk_cont_element *get_next_brk_cont_element(void) |
169 | 0 | { |
170 | 0 | CG(context).last_brk_cont++; |
171 | 0 | CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont); |
172 | 0 | return &CG(context).brk_cont_array[CG(context).last_brk_cont-1]; |
173 | 0 | } |
174 | | |
175 | | static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */ |
176 | 0 | { |
177 | 0 | zend_string *filename = CG(active_op_array)->filename; |
178 | 0 | zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32, |
179 | 0 | '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
180 | 0 | return zend_new_interned_string(result); |
181 | 0 | } |
182 | | /* }}} */ |
183 | | |
184 | | static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */ |
185 | 0 | { |
186 | 0 | const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
187 | 0 | if (ns_separator != NULL) { |
188 | 0 | *result = ns_separator + 1; |
189 | 0 | *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result; |
190 | 0 | return true; |
191 | 0 | } |
192 | | |
193 | 0 | return false; |
194 | 0 | } |
195 | | /* }}} */ |
196 | | |
197 | | struct reserved_class_name { |
198 | | const char *name; |
199 | | size_t len; |
200 | | }; |
201 | | static const struct reserved_class_name reserved_class_names[] = { |
202 | | {ZEND_STRL("bool")}, |
203 | | {ZEND_STRL("false")}, |
204 | | {ZEND_STRL("float")}, |
205 | | {ZEND_STRL("int")}, |
206 | | {ZEND_STRL("null")}, |
207 | | {ZEND_STRL("parent")}, |
208 | | {ZEND_STRL("self")}, |
209 | | {ZEND_STRL("static")}, |
210 | | {ZEND_STRL("string")}, |
211 | | {ZEND_STRL("true")}, |
212 | | {ZEND_STRL("void")}, |
213 | | {ZEND_STRL("never")}, |
214 | | {ZEND_STRL("iterable")}, |
215 | | {ZEND_STRL("object")}, |
216 | | {ZEND_STRL("mixed")}, |
217 | | /* These are not usable as class names because they're proper tokens, |
218 | | * but they are here for class aliases. */ |
219 | | {ZEND_STRL("array")}, |
220 | | {ZEND_STRL("callable")}, |
221 | | {NULL, 0} |
222 | | }; |
223 | | |
224 | | static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */ |
225 | 0 | { |
226 | 0 | const struct reserved_class_name *reserved = reserved_class_names; |
227 | |
|
228 | 0 | const char *uqname = ZSTR_VAL(name); |
229 | 0 | size_t uqname_len = ZSTR_LEN(name); |
230 | 0 | zend_get_unqualified_name(name, &uqname, &uqname_len); |
231 | |
|
232 | 0 | for (; reserved->name; ++reserved) { |
233 | 0 | if (uqname_len == reserved->len |
234 | 0 | && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0 |
235 | 0 | ) { |
236 | 0 | return true; |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | 0 | return false; |
241 | 0 | } |
242 | | /* }}} */ |
243 | | |
244 | | void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */ |
245 | 0 | { |
246 | 0 | if (zend_is_reserved_class_name(name)) { |
247 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
248 | 0 | "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type); |
249 | 0 | } |
250 | 0 | if (zend_string_equals_literal(name, "_")) { |
251 | 0 | zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type); |
252 | 0 | } |
253 | 0 | } |
254 | | /* }}} */ |
255 | | |
256 | | typedef struct _builtin_type_info { |
257 | | const char* name; |
258 | | const size_t name_len; |
259 | | const uint8_t type; |
260 | | } builtin_type_info; |
261 | | |
262 | | static const builtin_type_info builtin_types[] = { |
263 | | {ZEND_STRL("null"), IS_NULL}, |
264 | | {ZEND_STRL("true"), IS_TRUE}, |
265 | | {ZEND_STRL("false"), IS_FALSE}, |
266 | | {ZEND_STRL("int"), IS_LONG}, |
267 | | {ZEND_STRL("float"), IS_DOUBLE}, |
268 | | {ZEND_STRL("string"), IS_STRING}, |
269 | | {ZEND_STRL("bool"), _IS_BOOL}, |
270 | | {ZEND_STRL("void"), IS_VOID}, |
271 | | {ZEND_STRL("never"), IS_NEVER}, |
272 | | {ZEND_STRL("iterable"), IS_ITERABLE}, |
273 | | {ZEND_STRL("object"), IS_OBJECT}, |
274 | | {ZEND_STRL("mixed"), IS_MIXED}, |
275 | | {NULL, 0, IS_UNDEF} |
276 | | }; |
277 | | |
278 | | typedef struct { |
279 | | const char *name; |
280 | | size_t name_len; |
281 | | const char *correct_name; |
282 | | } confusable_type_info; |
283 | | |
284 | | static const confusable_type_info confusable_types[] = { |
285 | | {ZEND_STRL("boolean"), "bool"}, |
286 | | {ZEND_STRL("integer"), "int"}, |
287 | | {ZEND_STRL("double"), "float"}, |
288 | | {ZEND_STRL("resource"), NULL}, |
289 | | {NULL, 0, NULL}, |
290 | | }; |
291 | | |
292 | | static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */ |
293 | 0 | { |
294 | 0 | const builtin_type_info *info = &builtin_types[0]; |
295 | |
|
296 | 0 | for (; info->name; ++info) { |
297 | 0 | if (ZSTR_LEN(name) == info->name_len |
298 | 0 | && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0 |
299 | 0 | ) { |
300 | 0 | return info->type; |
301 | 0 | } |
302 | 0 | } |
303 | | |
304 | 0 | return 0; |
305 | 0 | } |
306 | | /* }}} */ |
307 | | |
308 | | static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */ |
309 | 0 | { |
310 | 0 | const confusable_type_info *info = confusable_types; |
311 | | |
312 | | /* Intentionally using case-sensitive comparison here, because "integer" is likely intended |
313 | | * as a scalar type, while "Integer" is likely a class type. */ |
314 | 0 | for (; info->name; ++info) { |
315 | 0 | if (zend_string_equals_cstr(name, info->name, info->name_len)) { |
316 | 0 | *correct_name = info->correct_name; |
317 | 0 | return 1; |
318 | 0 | } |
319 | 0 | } |
320 | | |
321 | 0 | return 0; |
322 | 0 | } |
323 | | /* }}} */ |
324 | | |
325 | 0 | static bool zend_is_not_imported(zend_string *name) { |
326 | | /* Assuming "name" is unqualified here. */ |
327 | 0 | return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL; |
328 | 0 | } |
329 | | |
330 | | void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */ |
331 | 0 | { |
332 | 0 | *prev_context = CG(context); |
333 | 0 | CG(context).prev = CG(context).op_array ? prev_context : NULL; |
334 | 0 | CG(context).op_array = op_array; |
335 | 0 | CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE; |
336 | 0 | CG(context).vars_size = 0; |
337 | 0 | CG(context).literals_size = 0; |
338 | 0 | CG(context).fast_call_var = -1; |
339 | 0 | CG(context).try_catch_offset = -1; |
340 | 0 | CG(context).current_brk_cont = -1; |
341 | 0 | CG(context).last_brk_cont = 0; |
342 | 0 | CG(context).has_assigned_to_http_response_header = false; |
343 | 0 | CG(context).brk_cont_array = NULL; |
344 | 0 | CG(context).labels = NULL; |
345 | 0 | CG(context).in_jmp_frameless_branch = false; |
346 | 0 | CG(context).active_property_info_name = NULL; |
347 | 0 | CG(context).active_property_hook_kind = (zend_property_hook_kind)-1; |
348 | 0 | } |
349 | | /* }}} */ |
350 | | |
351 | | void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */ |
352 | 0 | { |
353 | 0 | if (CG(context).brk_cont_array) { |
354 | 0 | efree(CG(context).brk_cont_array); |
355 | 0 | CG(context).brk_cont_array = NULL; |
356 | 0 | } |
357 | 0 | if (CG(context).labels) { |
358 | 0 | zend_hash_destroy(CG(context).labels); |
359 | 0 | FREE_HASHTABLE(CG(context).labels); |
360 | 0 | CG(context).labels = NULL; |
361 | 0 | } |
362 | 0 | CG(context) = *prev_context; |
363 | 0 | } |
364 | | /* }}} */ |
365 | | |
366 | | static void zend_reset_import_tables(void) /* {{{ */ |
367 | 0 | { |
368 | 0 | if (FC(imports)) { |
369 | 0 | zend_hash_destroy(FC(imports)); |
370 | 0 | efree(FC(imports)); |
371 | 0 | FC(imports) = NULL; |
372 | 0 | } |
373 | |
|
374 | 0 | if (FC(imports_function)) { |
375 | 0 | zend_hash_destroy(FC(imports_function)); |
376 | 0 | efree(FC(imports_function)); |
377 | 0 | FC(imports_function) = NULL; |
378 | 0 | } |
379 | |
|
380 | 0 | if (FC(imports_const)) { |
381 | 0 | zend_hash_destroy(FC(imports_const)); |
382 | 0 | efree(FC(imports_const)); |
383 | 0 | FC(imports_const) = NULL; |
384 | 0 | } |
385 | |
|
386 | 0 | zend_hash_clean(&FC(seen_symbols)); |
387 | 0 | } |
388 | | /* }}} */ |
389 | | |
390 | 0 | static void zend_end_namespace(void) /* {{{ */ { |
391 | 0 | FC(in_namespace) = 0; |
392 | 0 | zend_reset_import_tables(); |
393 | 0 | if (FC(current_namespace)) { |
394 | 0 | zend_string_release_ex(FC(current_namespace), 0); |
395 | 0 | FC(current_namespace) = NULL; |
396 | 0 | } |
397 | 0 | } |
398 | | /* }}} */ |
399 | | |
400 | | void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */ |
401 | 0 | { |
402 | 0 | *prev_context = CG(file_context); |
403 | 0 | FC(imports) = NULL; |
404 | 0 | FC(imports_function) = NULL; |
405 | 0 | FC(imports_const) = NULL; |
406 | 0 | FC(current_namespace) = NULL; |
407 | 0 | FC(in_namespace) = 0; |
408 | 0 | FC(has_bracketed_namespaces) = 0; |
409 | 0 | FC(declarables).ticks = 0; |
410 | 0 | zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0); |
411 | 0 | } |
412 | | /* }}} */ |
413 | | |
414 | | void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */ |
415 | 0 | { |
416 | 0 | zend_end_namespace(); |
417 | 0 | zend_hash_destroy(&FC(seen_symbols)); |
418 | 0 | CG(file_context) = *prev_context; |
419 | 0 | } |
420 | | /* }}} */ |
421 | | |
422 | | void zend_init_compiler_data_structures(void) /* {{{ */ |
423 | 1.99k | { |
424 | 1.99k | zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var)); |
425 | 1.99k | zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op)); |
426 | 1.99k | zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t)); |
427 | 1.99k | CG(active_class_entry) = NULL; |
428 | 1.99k | CG(in_compilation) = 0; |
429 | 1.99k | CG(skip_shebang) = 0; |
430 | | |
431 | 1.99k | CG(encoding_declared) = 0; |
432 | 1.99k | CG(memoized_exprs) = NULL; |
433 | 1.99k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
434 | 1.99k | } |
435 | | /* }}} */ |
436 | | |
437 | 0 | static void zend_register_seen_symbol(zend_string *name, uint32_t kind) { |
438 | 0 | zval *zv = zend_hash_find(&FC(seen_symbols), name); |
439 | 0 | if (zv) { |
440 | 0 | Z_LVAL_P(zv) |= kind; |
441 | 0 | } else { |
442 | 0 | zval tmp; |
443 | 0 | ZVAL_LONG(&tmp, kind); |
444 | 0 | zend_hash_add_new(&FC(seen_symbols), name, &tmp); |
445 | 0 | } |
446 | 0 | } |
447 | | |
448 | 0 | static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) { |
449 | 0 | const zval *zv = zend_hash_find(&FC(seen_symbols), name); |
450 | 0 | return zv && (Z_LVAL_P(zv) & kind) != 0; |
451 | 0 | } |
452 | | |
453 | | void init_compiler(void) /* {{{ */ |
454 | 1.99k | { |
455 | 1.99k | CG(arena) = zend_arena_create(64 * 1024); |
456 | 1.99k | CG(active_op_array) = NULL; |
457 | 1.99k | memset(&CG(context), 0, sizeof(CG(context))); |
458 | 1.99k | zend_init_compiler_data_structures(); |
459 | 1.99k | zend_init_rsrc_list(); |
460 | 1.99k | zend_stream_init(); |
461 | 1.99k | CG(unclean_shutdown) = 0; |
462 | | |
463 | 1.99k | CG(delayed_variance_obligations) = NULL; |
464 | 1.99k | CG(delayed_autoloads) = NULL; |
465 | 1.99k | CG(unlinked_uses) = NULL; |
466 | 1.99k | CG(current_linking_class) = NULL; |
467 | 1.99k | } |
468 | | /* }}} */ |
469 | | |
470 | | void shutdown_compiler(void) /* {{{ */ |
471 | 1.99k | { |
472 | | /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */ |
473 | 1.99k | zend_restore_compiled_filename(NULL); |
474 | | |
475 | 1.99k | zend_stack_destroy(&CG(loop_var_stack)); |
476 | 1.99k | zend_stack_destroy(&CG(delayed_oplines_stack)); |
477 | 1.99k | zend_stack_destroy(&CG(short_circuiting_opnums)); |
478 | | |
479 | 1.99k | if (CG(delayed_variance_obligations)) { |
480 | 0 | zend_hash_destroy(CG(delayed_variance_obligations)); |
481 | 0 | FREE_HASHTABLE(CG(delayed_variance_obligations)); |
482 | 0 | CG(delayed_variance_obligations) = NULL; |
483 | 0 | } |
484 | 1.99k | if (CG(delayed_autoloads)) { |
485 | 0 | zend_hash_destroy(CG(delayed_autoloads)); |
486 | 0 | FREE_HASHTABLE(CG(delayed_autoloads)); |
487 | 0 | CG(delayed_autoloads) = NULL; |
488 | 0 | } |
489 | 1.99k | if (CG(unlinked_uses)) { |
490 | 0 | zend_hash_destroy(CG(unlinked_uses)); |
491 | 0 | FREE_HASHTABLE(CG(unlinked_uses)); |
492 | 0 | CG(unlinked_uses) = NULL; |
493 | 0 | } |
494 | 1.99k | CG(current_linking_class) = NULL; |
495 | 1.99k | } |
496 | | /* }}} */ |
497 | | |
498 | | ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */ |
499 | 0 | { |
500 | 0 | CG(compiled_filename) = zend_string_copy(new_compiled_filename); |
501 | 0 | return new_compiled_filename; |
502 | 0 | } |
503 | | /* }}} */ |
504 | | |
505 | | ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */ |
506 | 1.99k | { |
507 | 1.99k | if (CG(compiled_filename)) { |
508 | 0 | zend_string_release(CG(compiled_filename)); |
509 | 0 | CG(compiled_filename) = NULL; |
510 | 0 | } |
511 | 1.99k | CG(compiled_filename) = original_compiled_filename; |
512 | 1.99k | } |
513 | | /* }}} */ |
514 | | |
515 | | ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */ |
516 | 0 | { |
517 | 0 | return CG(compiled_filename); |
518 | 0 | } |
519 | | /* }}} */ |
520 | | |
521 | | ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */ |
522 | 0 | { |
523 | 0 | return CG(zend_lineno); |
524 | 0 | } |
525 | | /* }}} */ |
526 | | |
527 | | ZEND_API bool zend_is_compiling(void) /* {{{ */ |
528 | 13.7k | { |
529 | 13.7k | return CG(in_compilation); |
530 | 13.7k | } |
531 | | /* }}} */ |
532 | | |
533 | | static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */ |
534 | 0 | { |
535 | 0 | return (uint32_t)CG(active_op_array)->T++; |
536 | 0 | } |
537 | | /* }}} */ |
538 | | |
539 | 0 | static uint32_t lookup_cv(zend_string *name) /* {{{ */{ |
540 | 0 | zend_op_array *op_array = CG(active_op_array); |
541 | 0 | int i = 0; |
542 | 0 | zend_ulong hash_value = zend_string_hash_val(name); |
543 | |
|
544 | 0 | while (i < op_array->last_var) { |
545 | 0 | if (ZSTR_H(op_array->vars[i]) == hash_value |
546 | 0 | && zend_string_equals(op_array->vars[i], name)) { |
547 | 0 | return EX_NUM_TO_VAR(i); |
548 | 0 | } |
549 | 0 | i++; |
550 | 0 | } |
551 | 0 | i = op_array->last_var; |
552 | 0 | op_array->last_var++; |
553 | 0 | if (op_array->last_var > CG(context).vars_size) { |
554 | 0 | CG(context).vars_size += 16; /* FIXME */ |
555 | 0 | op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*)); |
556 | 0 | } |
557 | |
|
558 | 0 | op_array->vars[i] = zend_string_copy(name); |
559 | 0 | return EX_NUM_TO_VAR(i); |
560 | 0 | } |
561 | | /* }}} */ |
562 | | |
563 | | zend_string *zval_make_interned_string(zval *zv) |
564 | 32 | { |
565 | 32 | ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING); |
566 | 32 | Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv)); |
567 | 32 | if (ZSTR_IS_INTERNED(Z_STR_P(zv))) { |
568 | 32 | Z_TYPE_FLAGS_P(zv) = 0; |
569 | 32 | } |
570 | 32 | return Z_STR_P(zv); |
571 | 32 | } |
572 | | |
573 | | /* Common part of zend_add_literal and zend_append_individual_literal */ |
574 | | static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */ |
575 | 0 | { |
576 | 0 | zval *lit = CT_CONSTANT_EX(op_array, literal_position); |
577 | 0 | if (Z_TYPE_P(zv) == IS_STRING) { |
578 | 0 | zval_make_interned_string(zv); |
579 | 0 | } |
580 | 0 | ZVAL_COPY_VALUE(lit, zv); |
581 | 0 | Z_EXTRA_P(lit) = 0; |
582 | 0 | } |
583 | | /* }}} */ |
584 | | |
585 | | /* Is used while compiling a function, using the context to keep track |
586 | | of an approximate size to avoid to relocate to often. |
587 | | Literals are truncated to actual size in the second compiler pass (pass_two()). */ |
588 | | static int zend_add_literal(zval *zv) /* {{{ */ |
589 | 0 | { |
590 | 0 | zend_op_array *op_array = CG(active_op_array); |
591 | 0 | uint32_t i = op_array->last_literal; |
592 | 0 | op_array->last_literal++; |
593 | 0 | if (i >= CG(context).literals_size) { |
594 | 0 | while (i >= CG(context).literals_size) { |
595 | 0 | CG(context).literals_size += 16; /* FIXME */ |
596 | 0 | } |
597 | 0 | op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval)); |
598 | 0 | } |
599 | 0 | zend_insert_literal(op_array, zv, i); |
600 | 0 | return i; |
601 | 0 | } |
602 | | /* }}} */ |
603 | | |
604 | | static inline int zend_add_literal_string(zend_string **str) /* {{{ */ |
605 | 0 | { |
606 | 0 | int ret; |
607 | 0 | zval zv; |
608 | 0 | ZVAL_STR(&zv, *str); |
609 | 0 | ret = zend_add_literal(&zv); |
610 | 0 | *str = Z_STR(zv); |
611 | 0 | return ret; |
612 | 0 | } |
613 | | /* }}} */ |
614 | | |
615 | | static int zend_add_func_name_literal(zend_string *name) /* {{{ */ |
616 | 0 | { |
617 | | /* Original name */ |
618 | 0 | int ret = zend_add_literal_string(&name); |
619 | | |
620 | | /* Lowercased name */ |
621 | 0 | zend_string *lc_name = zend_string_tolower(name); |
622 | 0 | zend_add_literal_string(&lc_name); |
623 | |
|
624 | 0 | return ret; |
625 | 0 | } |
626 | | /* }}} */ |
627 | | |
628 | | static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */ |
629 | 0 | { |
630 | 0 | const char *unqualified_name; |
631 | 0 | size_t unqualified_name_len; |
632 | | |
633 | | /* Original name */ |
634 | 0 | int ret = zend_add_literal_string(&name); |
635 | | |
636 | | /* Lowercased name */ |
637 | 0 | zend_string *lc_name = zend_string_tolower(name); |
638 | 0 | zend_add_literal_string(&lc_name); |
639 | | |
640 | | /* Lowercased unqualified name */ |
641 | 0 | if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) { |
642 | 0 | lc_name = zend_string_alloc(unqualified_name_len, 0); |
643 | 0 | zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len); |
644 | 0 | zend_add_literal_string(&lc_name); |
645 | 0 | } |
646 | |
|
647 | 0 | return ret; |
648 | 0 | } |
649 | | /* }}} */ |
650 | | |
651 | | static int zend_add_class_name_literal(zend_string *name) /* {{{ */ |
652 | 0 | { |
653 | | /* Original name */ |
654 | 0 | int ret = zend_add_literal_string(&name); |
655 | | |
656 | | /* Lowercased name */ |
657 | 0 | zend_string *lc_name = zend_string_tolower(name); |
658 | 0 | zend_add_literal_string(&lc_name); |
659 | |
|
660 | 0 | return ret; |
661 | 0 | } |
662 | | /* }}} */ |
663 | | |
664 | | static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */ |
665 | 0 | { |
666 | 0 | zend_string *tmp_name; |
667 | |
|
668 | 0 | int ret = zend_add_literal_string(&name); |
669 | |
|
670 | 0 | size_t ns_len = 0, after_ns_len = ZSTR_LEN(name); |
671 | 0 | const char *after_ns = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
672 | 0 | if (after_ns) { |
673 | 0 | after_ns += 1; |
674 | 0 | ns_len = after_ns - ZSTR_VAL(name) - 1; |
675 | 0 | after_ns_len = ZSTR_LEN(name) - ns_len - 1; |
676 | | |
677 | | /* lowercased namespace name & original constant name */ |
678 | 0 | tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0); |
679 | 0 | zend_str_tolower(ZSTR_VAL(tmp_name), ns_len); |
680 | 0 | zend_add_literal_string(&tmp_name); |
681 | |
|
682 | 0 | if (!unqualified) { |
683 | 0 | return ret; |
684 | 0 | } |
685 | 0 | } else { |
686 | 0 | after_ns = ZSTR_VAL(name); |
687 | 0 | } |
688 | | |
689 | | /* original unqualified constant name */ |
690 | 0 | tmp_name = zend_string_init(after_ns, after_ns_len, 0); |
691 | 0 | zend_add_literal_string(&tmp_name); |
692 | |
|
693 | 0 | return ret; |
694 | 0 | } |
695 | | /* }}} */ |
696 | | |
697 | 0 | #define LITERAL_STR(op, str) do { \ |
698 | 0 | zval _c; \ |
699 | 0 | ZVAL_STR(&_c, str); \ |
700 | 0 | op.constant = zend_add_literal(&_c); \ |
701 | 0 | } while (0) |
702 | | |
703 | | void zend_stop_lexing(void) |
704 | 0 | { |
705 | 0 | if (LANG_SCNG(on_event)) { |
706 | 0 | LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context)); |
707 | 0 | } |
708 | |
|
709 | 0 | LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit); |
710 | 0 | } |
711 | | |
712 | | static inline void zend_begin_loop( |
713 | | uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */ |
714 | 0 | { |
715 | 0 | zend_brk_cont_element *brk_cont_element; |
716 | 0 | int parent = CG(context).current_brk_cont; |
717 | 0 | zend_loop_var info = {0}; |
718 | |
|
719 | 0 | CG(context).current_brk_cont = CG(context).last_brk_cont; |
720 | 0 | brk_cont_element = get_next_brk_cont_element(); |
721 | 0 | brk_cont_element->parent = parent; |
722 | 0 | brk_cont_element->is_switch = is_switch; |
723 | |
|
724 | 0 | if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) { |
725 | 0 | uint32_t start = get_next_op_number(); |
726 | |
|
727 | 0 | info.opcode = free_opcode; |
728 | 0 | info.var_type = loop_var->op_type; |
729 | 0 | info.var_num = loop_var->u.op.var; |
730 | 0 | brk_cont_element->start = start; |
731 | 0 | } else { |
732 | 0 | info.opcode = ZEND_NOP; |
733 | | /* The start field is used to free temporary variables in case of exceptions. |
734 | | * We won't try to free something of we don't have loop variable. */ |
735 | 0 | brk_cont_element->start = -1; |
736 | 0 | } |
737 | |
|
738 | 0 | zend_stack_push(&CG(loop_var_stack), &info); |
739 | 0 | } |
740 | | /* }}} */ |
741 | | |
742 | | static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */ |
743 | 0 | { |
744 | 0 | uint32_t end = get_next_op_number(); |
745 | 0 | zend_brk_cont_element *brk_cont_element |
746 | 0 | = &CG(context).brk_cont_array[CG(context).current_brk_cont]; |
747 | 0 | brk_cont_element->cont = cont_addr; |
748 | 0 | brk_cont_element->brk = end; |
749 | 0 | CG(context).current_brk_cont = brk_cont_element->parent; |
750 | |
|
751 | 0 | zend_stack_del_top(&CG(loop_var_stack)); |
752 | 0 | } |
753 | | /* }}} */ |
754 | | |
755 | | bool zend_op_may_elide_result(uint8_t opcode) |
756 | 0 | { |
757 | 0 | switch (opcode) { |
758 | 0 | case ZEND_ASSIGN: |
759 | 0 | case ZEND_ASSIGN_DIM: |
760 | 0 | case ZEND_ASSIGN_OBJ: |
761 | 0 | case ZEND_ASSIGN_STATIC_PROP: |
762 | 0 | case ZEND_ASSIGN_OP: |
763 | 0 | case ZEND_ASSIGN_DIM_OP: |
764 | 0 | case ZEND_ASSIGN_OBJ_OP: |
765 | 0 | case ZEND_ASSIGN_STATIC_PROP_OP: |
766 | 0 | case ZEND_PRE_INC_STATIC_PROP: |
767 | 0 | case ZEND_PRE_DEC_STATIC_PROP: |
768 | 0 | case ZEND_PRE_INC_OBJ: |
769 | 0 | case ZEND_PRE_DEC_OBJ: |
770 | 0 | case ZEND_PRE_INC: |
771 | 0 | case ZEND_PRE_DEC: |
772 | 0 | case ZEND_DO_FCALL: |
773 | 0 | case ZEND_DO_ICALL: |
774 | 0 | case ZEND_DO_UCALL: |
775 | 0 | case ZEND_DO_FCALL_BY_NAME: |
776 | 0 | case ZEND_YIELD: |
777 | 0 | case ZEND_YIELD_FROM: |
778 | 0 | case ZEND_INCLUDE_OR_EVAL: |
779 | 0 | return true; |
780 | 0 | default: |
781 | 0 | return false; |
782 | 0 | } |
783 | 0 | } |
784 | | |
785 | | static void zend_do_free(znode *op1) /* {{{ */ |
786 | 0 | { |
787 | 0 | if (op1->op_type == IS_TMP_VAR) { |
788 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
789 | |
|
790 | 0 | while (opline->opcode == ZEND_END_SILENCE || |
791 | 0 | opline->opcode == ZEND_OP_DATA) { |
792 | 0 | opline--; |
793 | 0 | } |
794 | |
|
795 | 0 | if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) { |
796 | 0 | switch (opline->opcode) { |
797 | 0 | case ZEND_BOOL: |
798 | 0 | case ZEND_BOOL_NOT: |
799 | | /* boolean results don't have to be freed */ |
800 | 0 | return; |
801 | 0 | case ZEND_POST_INC_STATIC_PROP: |
802 | 0 | case ZEND_POST_DEC_STATIC_PROP: |
803 | 0 | case ZEND_POST_INC_OBJ: |
804 | 0 | case ZEND_POST_DEC_OBJ: |
805 | 0 | case ZEND_POST_INC: |
806 | 0 | case ZEND_POST_DEC: |
807 | | /* convert $i++ to ++$i */ |
808 | 0 | opline->opcode -= 2; |
809 | 0 | SET_UNUSED(opline->result); |
810 | 0 | return; |
811 | 0 | default: |
812 | 0 | if (zend_op_may_elide_result(opline->opcode)) { |
813 | 0 | SET_UNUSED(opline->result); |
814 | 0 | return; |
815 | 0 | } |
816 | 0 | break; |
817 | 0 | } |
818 | 0 | } |
819 | | |
820 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
821 | 0 | } else if (op1->op_type == IS_VAR) { |
822 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
823 | 0 | while (opline->opcode == ZEND_END_SILENCE || |
824 | 0 | opline->opcode == ZEND_EXT_FCALL_END || |
825 | 0 | opline->opcode == ZEND_OP_DATA) { |
826 | 0 | opline--; |
827 | 0 | } |
828 | 0 | if (opline->result_type == IS_VAR |
829 | 0 | && opline->result.var == op1->u.op.var) { |
830 | 0 | if (opline->opcode == ZEND_FETCH_THIS) { |
831 | 0 | opline->opcode = ZEND_NOP; |
832 | 0 | } |
833 | 0 | if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) { |
834 | 0 | SET_UNUSED(opline->result); |
835 | 0 | } else { |
836 | | /* Frameless calls usually use the return value, so always emit a free. This should be |
837 | | * faster than checking RETURN_VALUE_USED inside the handler. */ |
838 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
839 | 0 | } |
840 | 0 | } else { |
841 | 0 | while (opline >= CG(active_op_array)->opcodes) { |
842 | 0 | if ((opline->opcode == ZEND_FETCH_LIST_R || |
843 | 0 | opline->opcode == ZEND_FETCH_LIST_W || |
844 | 0 | opline->opcode == ZEND_EXT_STMT) && |
845 | 0 | opline->op1_type == IS_VAR && |
846 | 0 | opline->op1.var == op1->u.op.var) { |
847 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
848 | 0 | return; |
849 | 0 | } |
850 | 0 | if (opline->result_type == IS_VAR |
851 | 0 | && opline->result.var == op1->u.op.var) { |
852 | 0 | if (opline->opcode == ZEND_NEW) { |
853 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
854 | 0 | } |
855 | 0 | break; |
856 | 0 | } |
857 | 0 | opline--; |
858 | 0 | } |
859 | 0 | } |
860 | 0 | } else if (op1->op_type == IS_CONST) { |
861 | | /* Destroy value without using GC: When opcache moves arrays into SHM it will |
862 | | * free the zend_array structure, so references to it from outside the op array |
863 | | * become invalid. GC would cause such a reference in the root buffer. */ |
864 | 0 | zval_ptr_dtor_nogc(&op1->u.constant); |
865 | 0 | } |
866 | 0 | } |
867 | | /* }}} */ |
868 | | |
869 | | |
870 | | static const char *zend_modifier_token_to_string(uint32_t token) |
871 | 0 | { |
872 | 0 | switch (token) { |
873 | 0 | case T_PUBLIC: |
874 | 0 | return "public"; |
875 | 0 | case T_PROTECTED: |
876 | 0 | return "protected"; |
877 | 0 | case T_PRIVATE: |
878 | 0 | return "private"; |
879 | 0 | case T_STATIC: |
880 | 0 | return "static"; |
881 | 0 | case T_FINAL: |
882 | 0 | return "final"; |
883 | 0 | case T_READONLY: |
884 | 0 | return "readonly"; |
885 | 0 | case T_ABSTRACT: |
886 | 0 | return "abstract"; |
887 | 0 | case T_PUBLIC_SET: |
888 | 0 | return "public(set)"; |
889 | 0 | case T_PROTECTED_SET: |
890 | 0 | return "protected(set)"; |
891 | 0 | case T_PRIVATE_SET: |
892 | 0 | return "private(set)"; |
893 | 0 | default: ZEND_UNREACHABLE(); |
894 | 0 | } |
895 | 0 | } |
896 | | |
897 | | uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token) |
898 | 0 | { |
899 | 0 | switch (token) { |
900 | 0 | case T_PUBLIC: |
901 | 0 | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
902 | 0 | return ZEND_ACC_PUBLIC; |
903 | 0 | } |
904 | 0 | break; |
905 | 0 | case T_PROTECTED: |
906 | 0 | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
907 | 0 | return ZEND_ACC_PROTECTED; |
908 | 0 | } |
909 | 0 | break; |
910 | 0 | case T_PRIVATE: |
911 | 0 | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
912 | 0 | return ZEND_ACC_PRIVATE; |
913 | 0 | } |
914 | 0 | break; |
915 | 0 | case T_READONLY: |
916 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
917 | 0 | return ZEND_ACC_READONLY; |
918 | 0 | } |
919 | 0 | break; |
920 | 0 | case T_ABSTRACT: |
921 | 0 | if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) { |
922 | 0 | return ZEND_ACC_ABSTRACT; |
923 | 0 | } |
924 | 0 | break; |
925 | 0 | case T_FINAL: |
926 | 0 | return ZEND_ACC_FINAL; |
927 | 0 | case T_STATIC: |
928 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) { |
929 | 0 | return ZEND_ACC_STATIC; |
930 | 0 | } |
931 | 0 | break; |
932 | 0 | case T_PUBLIC_SET: |
933 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
934 | 0 | return ZEND_ACC_PUBLIC_SET; |
935 | 0 | } |
936 | 0 | break; |
937 | 0 | case T_PROTECTED_SET: |
938 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
939 | 0 | return ZEND_ACC_PROTECTED_SET; |
940 | 0 | } |
941 | 0 | break; |
942 | 0 | case T_PRIVATE_SET: |
943 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
944 | 0 | return ZEND_ACC_PRIVATE_SET; |
945 | 0 | } |
946 | 0 | break; |
947 | 0 | } |
948 | | |
949 | 0 | char *member; |
950 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
951 | 0 | member = "property"; |
952 | 0 | } else if (target == ZEND_MODIFIER_TARGET_METHOD) { |
953 | 0 | member = "method"; |
954 | 0 | } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) { |
955 | 0 | member = "class constant"; |
956 | 0 | } else if (target == ZEND_MODIFIER_TARGET_CPP) { |
957 | 0 | member = "parameter"; |
958 | 0 | } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
959 | 0 | member = "property hook"; |
960 | 0 | } else { |
961 | 0 | ZEND_UNREACHABLE(); |
962 | 0 | } |
963 | | |
964 | 0 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
965 | 0 | "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member); |
966 | 0 | return 0; |
967 | 0 | } |
968 | | |
969 | | uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers) |
970 | 0 | { |
971 | 0 | uint32_t flags = 0; |
972 | 0 | const zend_ast_list *modifier_list = zend_ast_get_list(modifiers); |
973 | |
|
974 | 0 | for (uint32_t i = 0; i < modifier_list->children; i++) { |
975 | 0 | uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i])); |
976 | 0 | uint32_t new_flag = zend_modifier_token_to_flag(target, token); |
977 | 0 | if (!new_flag) { |
978 | 0 | return 0; |
979 | 0 | } |
980 | | /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */ |
981 | 0 | bool duplicate_flag = (flags & new_flag); |
982 | 0 | flags = zend_add_member_modifier(flags, new_flag, target); |
983 | 0 | if (!flags) { |
984 | 0 | return 0; |
985 | 0 | } |
986 | 0 | if (duplicate_flag) { |
987 | 0 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
988 | 0 | "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token)); |
989 | 0 | return 0; |
990 | 0 | } |
991 | 0 | } |
992 | | |
993 | 0 | return flags; |
994 | 0 | } |
995 | | |
996 | | uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */ |
997 | 0 | { |
998 | 0 | uint32_t new_flags = flags | new_flag; |
999 | 0 | if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) { |
1000 | 0 | zend_throw_exception(zend_ce_compile_error, |
1001 | 0 | "Multiple abstract modifiers are not allowed", 0); |
1002 | 0 | return 0; |
1003 | 0 | } |
1004 | 0 | if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) { |
1005 | 0 | zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0); |
1006 | 0 | return 0; |
1007 | 0 | } |
1008 | 0 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1009 | 0 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1010 | 0 | return 0; |
1011 | 0 | } |
1012 | 0 | if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) { |
1013 | 0 | zend_throw_exception(zend_ce_compile_error, |
1014 | 0 | "Cannot use the final modifier on an abstract class", 0); |
1015 | 0 | return 0; |
1016 | 0 | } |
1017 | 0 | return new_flags; |
1018 | 0 | } |
1019 | | /* }}} */ |
1020 | | |
1021 | | uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag) |
1022 | 0 | { |
1023 | 0 | uint32_t new_flags = flags | new_flag; |
1024 | 0 | if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) { |
1025 | 0 | zend_throw_exception(zend_ce_compile_error, |
1026 | 0 | "Cannot use the abstract modifier on an anonymous class", 0); |
1027 | 0 | return 0; |
1028 | 0 | } |
1029 | 0 | if (new_flag & ZEND_ACC_FINAL) { |
1030 | 0 | zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0); |
1031 | 0 | return 0; |
1032 | 0 | } |
1033 | 0 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1034 | 0 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | 0 | return new_flags; |
1038 | 0 | } |
1039 | | |
1040 | | uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */ |
1041 | 0 | { |
1042 | 0 | uint32_t new_flags = flags | new_flag; |
1043 | 0 | if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) { |
1044 | 0 | zend_throw_exception(zend_ce_compile_error, |
1045 | 0 | "Multiple access type modifiers are not allowed", 0); |
1046 | 0 | return 0; |
1047 | 0 | } |
1048 | 0 | if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) { |
1049 | 0 | if (target == ZEND_MODIFIER_TARGET_METHOD) { |
1050 | 0 | zend_throw_exception(zend_ce_compile_error, |
1051 | 0 | "Cannot use the final modifier on an abstract method", 0); |
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
1055 | 0 | zend_throw_exception(zend_ce_compile_error, |
1056 | 0 | "Cannot use the final modifier on an abstract property", 0); |
1057 | 0 | return 0; |
1058 | 0 | } |
1059 | 0 | } |
1060 | 0 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
1061 | 0 | if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) { |
1062 | 0 | zend_throw_exception(zend_ce_compile_error, |
1063 | 0 | "Multiple access type modifiers are not allowed", 0); |
1064 | 0 | return 0; |
1065 | 0 | } |
1066 | 0 | } |
1067 | 0 | return new_flags; |
1068 | 0 | } |
1069 | | /* }}} */ |
1070 | | |
1071 | 0 | ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) { |
1072 | 0 | return zend_string_concat3( |
1073 | 0 | ZSTR_VAL(class_name), ZSTR_LEN(class_name), |
1074 | 0 | "::", sizeof("::") - 1, |
1075 | 0 | ZSTR_VAL(member_name), ZSTR_LEN(member_name)); |
1076 | 0 | } |
1077 | | |
1078 | 0 | static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) { |
1079 | 0 | return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len); |
1080 | 0 | } |
1081 | | |
1082 | 0 | static zend_string *zend_prefix_with_ns(zend_string *name) { |
1083 | 0 | if (FC(current_namespace)) { |
1084 | 0 | const zend_string *ns = FC(current_namespace); |
1085 | 0 | return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
1086 | 0 | } else { |
1087 | 0 | return zend_string_copy(name); |
1088 | 0 | } |
1089 | 0 | } |
1090 | | |
1091 | | static zend_string *zend_resolve_non_class_name( |
1092 | | zend_string *name, uint32_t type, bool *is_fully_qualified, |
1093 | | bool case_sensitive, const HashTable *current_import_sub |
1094 | 0 | ) { |
1095 | 0 | const char *compound; |
1096 | 0 | *is_fully_qualified = false; |
1097 | |
|
1098 | 0 | if (ZSTR_VAL(name)[0] == '\\') { |
1099 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1100 | 0 | *is_fully_qualified = true; |
1101 | 0 | return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1102 | 0 | } |
1103 | | |
1104 | 0 | if (type == ZEND_NAME_FQ) { |
1105 | 0 | *is_fully_qualified = true; |
1106 | 0 | return zend_string_copy(name); |
1107 | 0 | } |
1108 | | |
1109 | 0 | if (type == ZEND_NAME_RELATIVE) { |
1110 | 0 | *is_fully_qualified = true; |
1111 | 0 | return zend_prefix_with_ns(name); |
1112 | 0 | } |
1113 | | |
1114 | 0 | if (current_import_sub) { |
1115 | | /* If an unqualified name is a function/const alias, replace it. */ |
1116 | 0 | zend_string *import_name; |
1117 | 0 | if (case_sensitive) { |
1118 | 0 | import_name = zend_hash_find_ptr(current_import_sub, name); |
1119 | 0 | } else { |
1120 | 0 | import_name = zend_hash_find_ptr_lc(current_import_sub, name); |
1121 | 0 | } |
1122 | |
|
1123 | 0 | if (import_name) { |
1124 | 0 | *is_fully_qualified = true; |
1125 | 0 | return zend_string_copy(import_name); |
1126 | 0 | } |
1127 | 0 | } |
1128 | | |
1129 | 0 | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1130 | 0 | if (compound) { |
1131 | 0 | *is_fully_qualified = true; |
1132 | 0 | } |
1133 | |
|
1134 | 0 | if (compound && FC(imports)) { |
1135 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1136 | 0 | size_t len = compound - ZSTR_VAL(name); |
1137 | 0 | const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1138 | |
|
1139 | 0 | if (import_name) { |
1140 | 0 | return zend_concat_names( |
1141 | 0 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1142 | 0 | } |
1143 | 0 | } |
1144 | | |
1145 | 0 | return zend_prefix_with_ns(name); |
1146 | 0 | } |
1147 | | /* }}} */ |
1148 | | |
1149 | | static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1150 | 0 | { |
1151 | 0 | return zend_resolve_non_class_name( |
1152 | 0 | name, type, is_fully_qualified, false, FC(imports_function)); |
1153 | 0 | } |
1154 | | |
1155 | | static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1156 | 0 | { |
1157 | 0 | return zend_resolve_non_class_name( |
1158 | 0 | name, type, is_fully_qualified, true, FC(imports_const)); |
1159 | 0 | } |
1160 | | |
1161 | | static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */ |
1162 | 0 | { |
1163 | 0 | const char *compound; |
1164 | |
|
1165 | 0 | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1166 | 0 | if (type == ZEND_NAME_FQ) { |
1167 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1168 | 0 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1169 | 0 | } |
1170 | 0 | if (type == ZEND_NAME_RELATIVE) { |
1171 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1172 | 0 | "'namespace\\%s' is an invalid class name", ZSTR_VAL(name)); |
1173 | 0 | } |
1174 | 0 | ZEND_ASSERT(type == ZEND_NAME_NOT_FQ); |
1175 | 0 | return zend_string_copy(name); |
1176 | 0 | } |
1177 | | |
1178 | 0 | if (type == ZEND_NAME_RELATIVE) { |
1179 | 0 | return zend_prefix_with_ns(name); |
1180 | 0 | } |
1181 | | |
1182 | 0 | if (type == ZEND_NAME_FQ) { |
1183 | 0 | if (ZSTR_VAL(name)[0] == '\\') { |
1184 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1185 | 0 | name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1186 | 0 | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1187 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1188 | 0 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1189 | 0 | } |
1190 | 0 | return name; |
1191 | 0 | } |
1192 | | |
1193 | 0 | return zend_string_copy(name); |
1194 | 0 | } |
1195 | | |
1196 | 0 | if (FC(imports)) { |
1197 | 0 | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1198 | 0 | if (compound) { |
1199 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1200 | 0 | size_t len = compound - ZSTR_VAL(name); |
1201 | 0 | const zend_string *import_name = |
1202 | 0 | zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1203 | |
|
1204 | 0 | if (import_name) { |
1205 | 0 | return zend_concat_names( |
1206 | 0 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1207 | 0 | } |
1208 | 0 | } else { |
1209 | | /* If an unqualified name is an alias, replace it. */ |
1210 | 0 | zend_string *import_name |
1211 | 0 | = zend_hash_find_ptr_lc(FC(imports), name); |
1212 | |
|
1213 | 0 | if (import_name) { |
1214 | 0 | return zend_string_copy(import_name); |
1215 | 0 | } |
1216 | 0 | } |
1217 | 0 | } |
1218 | | |
1219 | | /* If not fully qualified and not an alias, prepend the current namespace */ |
1220 | 0 | return zend_prefix_with_ns(name); |
1221 | 0 | } |
1222 | | /* }}} */ |
1223 | | |
1224 | | static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */ |
1225 | 0 | { |
1226 | 0 | const zval *class_name = zend_ast_get_zval(ast); |
1227 | 0 | if (Z_TYPE_P(class_name) != IS_STRING) { |
1228 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1229 | 0 | } |
1230 | 0 | return zend_resolve_class_name(Z_STR_P(class_name), ast->attr); |
1231 | 0 | } |
1232 | | /* }}} */ |
1233 | | |
1234 | | static void label_ptr_dtor(zval *zv) /* {{{ */ |
1235 | 0 | { |
1236 | 0 | efree_size(Z_PTR_P(zv), sizeof(zend_label)); |
1237 | 0 | } |
1238 | | /* }}} */ |
1239 | | |
1240 | 0 | static void str_dtor(zval *zv) /* {{{ */ { |
1241 | 0 | zend_string_release_ex(Z_STR_P(zv), 0); |
1242 | 0 | } |
1243 | | /* }}} */ |
1244 | | |
1245 | | static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */ |
1246 | 0 | { |
1247 | 0 | zend_op_array *op_array = CG(active_op_array); |
1248 | 0 | uint32_t try_catch_offset = op_array->last_try_catch++; |
1249 | 0 | zend_try_catch_element *elem; |
1250 | |
|
1251 | 0 | op_array->try_catch_array = safe_erealloc( |
1252 | 0 | op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0); |
1253 | |
|
1254 | 0 | elem = &op_array->try_catch_array[try_catch_offset]; |
1255 | 0 | elem->try_op = try_op; |
1256 | 0 | elem->catch_op = 0; |
1257 | 0 | elem->finally_op = 0; |
1258 | 0 | elem->finally_end = 0; |
1259 | |
|
1260 | 0 | return try_catch_offset; |
1261 | 0 | } |
1262 | | /* }}} */ |
1263 | | |
1264 | | ZEND_API void function_add_ref(zend_function *function) /* {{{ */ |
1265 | 0 | { |
1266 | 0 | if (function->type == ZEND_USER_FUNCTION) { |
1267 | 0 | zend_op_array *op_array = &function->op_array; |
1268 | 0 | if (op_array->refcount) { |
1269 | 0 | (*op_array->refcount)++; |
1270 | 0 | } |
1271 | |
|
1272 | 0 | ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL); |
1273 | 0 | ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL); |
1274 | 0 | } |
1275 | |
|
1276 | 0 | if (function->common.function_name) { |
1277 | 0 | zend_string_addref(function->common.function_name); |
1278 | 0 | } |
1279 | 0 | } |
1280 | | /* }}} */ |
1281 | | |
1282 | | static zend_never_inline ZEND_COLD ZEND_NORETURN void do_bind_function_error(const zend_string *lcname, const zend_op_array *op_array, bool compile_time) /* {{{ */ |
1283 | 0 | { |
1284 | 0 | const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname); |
1285 | 0 | int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR; |
1286 | 0 | const zend_function *old_function; |
1287 | |
|
1288 | 0 | ZEND_ASSERT(zv != NULL); |
1289 | 0 | old_function = Z_PTR_P(zv); |
1290 | 0 | if (old_function->type == ZEND_USER_FUNCTION |
1291 | 0 | && old_function->op_array.last > 0) { |
1292 | 0 | zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)", |
1293 | 0 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name), |
1294 | 0 | ZSTR_VAL(old_function->op_array.filename), |
1295 | 0 | old_function->op_array.line_start); |
1296 | 0 | } else { |
1297 | 0 | zend_error_noreturn(error_level, "Cannot redeclare function %s()", |
1298 | 0 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name)); |
1299 | 0 | } |
1300 | 0 | } |
1301 | | |
1302 | | ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */ |
1303 | 0 | { |
1304 | 0 | zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func); |
1305 | 0 | if (UNEXPECTED(!added_func)) { |
1306 | 0 | do_bind_function_error(Z_STR_P(lcname), &func->op_array, false); |
1307 | 0 | return FAILURE; |
1308 | 0 | } |
1309 | | |
1310 | 0 | if (func->op_array.refcount) { |
1311 | 0 | ++*func->op_array.refcount; |
1312 | 0 | } |
1313 | 0 | if (func->common.function_name) { |
1314 | 0 | zend_string_addref(func->common.function_name); |
1315 | 0 | } |
1316 | 0 | zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname)); |
1317 | 0 | return SUCCESS; |
1318 | 0 | } |
1319 | | /* }}} */ |
1320 | | |
1321 | | ZEND_API zend_class_entry *zend_bind_class_in_slot( |
1322 | | zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name) |
1323 | 0 | { |
1324 | 0 | zend_class_entry *ce = Z_PTR_P(class_table_slot); |
1325 | 0 | bool is_preloaded = |
1326 | 0 | (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD); |
1327 | 0 | bool success; |
1328 | 0 | if (EXPECTED(!is_preloaded)) { |
1329 | 0 | success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL; |
1330 | 0 | } else { |
1331 | | /* If preloading is used, don't replace the existing bucket, add a new one. */ |
1332 | 0 | success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL; |
1333 | 0 | } |
1334 | 0 | if (UNEXPECTED(!success)) { |
1335 | 0 | zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1336 | 0 | ZEND_ASSERT(old_class); |
1337 | 0 | zend_class_redeclaration_error(E_COMPILE_ERROR, old_class); |
1338 | 0 | return NULL; |
1339 | 0 | } |
1340 | | |
1341 | 0 | if (ce->ce_flags & ZEND_ACC_LINKED) { |
1342 | 0 | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1343 | 0 | return ce; |
1344 | 0 | } |
1345 | | |
1346 | 0 | ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname)); |
1347 | 0 | if (ce) { |
1348 | 0 | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1349 | 0 | return ce; |
1350 | 0 | } |
1351 | | |
1352 | 0 | if (!is_preloaded) { |
1353 | | /* Reload bucket pointer, the hash table may have been reallocated */ |
1354 | 0 | zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname)); |
1355 | 0 | zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1)); |
1356 | 0 | } else { |
1357 | 0 | zend_hash_del(EG(class_table), Z_STR_P(lcname)); |
1358 | 0 | } |
1359 | 0 | return NULL; |
1360 | 0 | } |
1361 | | |
1362 | | ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */ |
1363 | 0 | { |
1364 | 0 | zval *rtd_key, *zv; |
1365 | |
|
1366 | 0 | rtd_key = lcname + 1; |
1367 | |
|
1368 | 0 | zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key)); |
1369 | |
|
1370 | 0 | if (UNEXPECTED(!zv)) { |
1371 | 0 | const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1372 | 0 | ZEND_ASSERT(ce); |
1373 | 0 | zend_class_redeclaration_error(E_COMPILE_ERROR, ce); |
1374 | 0 | return FAILURE; |
1375 | 0 | } |
1376 | | |
1377 | | /* Register the derived class */ |
1378 | 0 | return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE; |
1379 | 0 | } |
1380 | | /* }}} */ |
1381 | | |
1382 | 0 | static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) { |
1383 | 0 | zend_string *result; |
1384 | 0 | if (type == NULL) { |
1385 | 0 | return zend_string_copy(new_type); |
1386 | 0 | } |
1387 | | |
1388 | 0 | if (is_intersection) { |
1389 | 0 | result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type), |
1390 | 0 | "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1391 | 0 | zend_string_release(type); |
1392 | 0 | } else { |
1393 | 0 | result = zend_string_concat3( |
1394 | 0 | ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1395 | 0 | zend_string_release(type); |
1396 | 0 | } |
1397 | 0 | return result; |
1398 | 0 | } |
1399 | | |
1400 | 0 | static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) { |
1401 | 0 | if (scope) { |
1402 | 0 | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1403 | 0 | name = scope->name; |
1404 | 0 | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) { |
1405 | 0 | name = scope->parent->name; |
1406 | 0 | } |
1407 | 0 | } |
1408 | | |
1409 | | /* The resolved name for anonymous classes contains null bytes. Cut off everything after the |
1410 | | * null byte here, to avoid larger parts of the type being omitted by printing code later. */ |
1411 | 0 | size_t len = strlen(ZSTR_VAL(name)); |
1412 | 0 | if (len != ZSTR_LEN(name)) { |
1413 | 0 | return zend_string_init(ZSTR_VAL(name), len, 0); |
1414 | 0 | } |
1415 | 0 | return zend_string_copy(name); |
1416 | 0 | } |
1417 | | |
1418 | | static zend_string *add_intersection_type(zend_string *str, |
1419 | | const zend_type_list *intersection_type_list, |
1420 | | bool is_bracketed) |
1421 | 0 | { |
1422 | 0 | const zend_type *single_type; |
1423 | 0 | zend_string *intersection_str = NULL; |
1424 | |
|
1425 | 0 | ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) { |
1426 | 0 | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type)); |
1427 | 0 | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type)); |
1428 | |
|
1429 | 0 | intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true); |
1430 | 0 | } ZEND_TYPE_LIST_FOREACH_END(); |
1431 | |
|
1432 | 0 | ZEND_ASSERT(intersection_str); |
1433 | |
|
1434 | 0 | if (is_bracketed) { |
1435 | 0 | zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1); |
1436 | 0 | zend_string_release(intersection_str); |
1437 | 0 | intersection_str = result; |
1438 | 0 | } |
1439 | 0 | str = add_type_string(str, intersection_str, /* is_intersection */ false); |
1440 | 0 | zend_string_release(intersection_str); |
1441 | 0 | return str; |
1442 | 0 | } |
1443 | | |
1444 | 0 | zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) { |
1445 | 0 | zend_string *str = NULL; |
1446 | | |
1447 | | /* Pure intersection type */ |
1448 | 0 | if (ZEND_TYPE_IS_INTERSECTION(type)) { |
1449 | 0 | ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type)); |
1450 | 0 | str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false); |
1451 | 0 | } else if (ZEND_TYPE_HAS_LIST(type)) { |
1452 | | /* A union type might not be a list */ |
1453 | 0 | const zend_type *list_type; |
1454 | 0 | ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) { |
1455 | 0 | if (ZEND_TYPE_IS_INTERSECTION(*list_type)) { |
1456 | 0 | str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true); |
1457 | 0 | continue; |
1458 | 0 | } |
1459 | 0 | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type)); |
1460 | 0 | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type)); |
1461 | |
|
1462 | 0 | zend_string *name = ZEND_TYPE_NAME(*list_type); |
1463 | 0 | zend_string *resolved = resolve_class_name(name, scope); |
1464 | 0 | str = add_type_string(str, resolved, /* is_intersection */ false); |
1465 | 0 | zend_string_release(resolved); |
1466 | 0 | } ZEND_TYPE_LIST_FOREACH_END(); |
1467 | 0 | } else if (ZEND_TYPE_HAS_NAME(type)) { |
1468 | 0 | str = resolve_class_name(ZEND_TYPE_NAME(type), scope); |
1469 | 0 | } |
1470 | | |
1471 | 0 | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
1472 | |
|
1473 | 0 | if (type_mask == MAY_BE_ANY) { |
1474 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false); |
1475 | |
|
1476 | 0 | return str; |
1477 | 0 | } |
1478 | 0 | if (type_mask & MAY_BE_STATIC) { |
1479 | 0 | zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC); |
1480 | | // During compilation of eval'd code the called scope refers to the scope calling the eval |
1481 | 0 | if (scope && !zend_is_compiling()) { |
1482 | 0 | const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data)); |
1483 | 0 | if (called_scope) { |
1484 | 0 | name = called_scope->name; |
1485 | 0 | } |
1486 | 0 | } |
1487 | 0 | str = add_type_string(str, name, /* is_intersection */ false); |
1488 | 0 | } |
1489 | 0 | if (type_mask & MAY_BE_CALLABLE) { |
1490 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false); |
1491 | 0 | } |
1492 | 0 | if (type_mask & MAY_BE_OBJECT) { |
1493 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false); |
1494 | 0 | } |
1495 | 0 | if (type_mask & MAY_BE_ARRAY) { |
1496 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false); |
1497 | 0 | } |
1498 | 0 | if (type_mask & MAY_BE_STRING) { |
1499 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false); |
1500 | 0 | } |
1501 | 0 | if (type_mask & MAY_BE_LONG) { |
1502 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false); |
1503 | 0 | } |
1504 | 0 | if (type_mask & MAY_BE_DOUBLE) { |
1505 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false); |
1506 | 0 | } |
1507 | 0 | if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) { |
1508 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false); |
1509 | 0 | } else if (type_mask & MAY_BE_FALSE) { |
1510 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false); |
1511 | 0 | } else if (type_mask & MAY_BE_TRUE) { |
1512 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false); |
1513 | 0 | } |
1514 | 0 | if (type_mask & MAY_BE_VOID) { |
1515 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false); |
1516 | 0 | } |
1517 | 0 | if (type_mask & MAY_BE_NEVER) { |
1518 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false); |
1519 | 0 | } |
1520 | |
|
1521 | 0 | if (type_mask & MAY_BE_NULL) { |
1522 | 0 | bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL; |
1523 | 0 | bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL; |
1524 | 0 | if (!is_union && !has_intersection) { |
1525 | 0 | zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str)); |
1526 | 0 | zend_string_release(str); |
1527 | 0 | return nullable_str; |
1528 | 0 | } |
1529 | | |
1530 | 0 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false); |
1531 | 0 | } |
1532 | 0 | return str; |
1533 | 0 | } |
1534 | | |
1535 | 0 | ZEND_API zend_string *zend_type_to_string(zend_type type) { |
1536 | 0 | return zend_type_to_string_resolved(type, NULL); |
1537 | 0 | } |
1538 | | |
1539 | 0 | static bool is_generator_compatible_class_type(const zend_string *name) { |
1540 | 0 | return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE)) |
1541 | 0 | || zend_string_equals_literal_ci(name, "Iterator") |
1542 | 0 | || zend_string_equals_literal_ci(name, "Generator"); |
1543 | 0 | } |
1544 | | |
1545 | | static void zend_mark_function_as_generator(void) /* {{{ */ |
1546 | 0 | { |
1547 | 0 | if (!CG(active_op_array)->function_name) { |
1548 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1549 | 0 | "The \"yield\" expression can only be used inside a function"); |
1550 | 0 | } |
1551 | | |
1552 | 0 | if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
1553 | 0 | const zend_type return_type = CG(active_op_array)->arg_info[-1].type; |
1554 | 0 | bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0; |
1555 | 0 | if (!valid_type) { |
1556 | 0 | const zend_type *single_type; |
1557 | 0 | ZEND_TYPE_FOREACH(return_type, single_type) { |
1558 | 0 | if (ZEND_TYPE_HAS_NAME(*single_type) |
1559 | 0 | && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) { |
1560 | 0 | valid_type = true; |
1561 | 0 | break; |
1562 | 0 | } |
1563 | 0 | } ZEND_TYPE_FOREACH_END(); |
1564 | 0 | } |
1565 | |
|
1566 | 0 | if (!valid_type) { |
1567 | 0 | zend_string *str = zend_type_to_string(return_type); |
1568 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1569 | 0 | "Generator return type must be a supertype of Generator, %s given", |
1570 | 0 | ZSTR_VAL(str)); |
1571 | 0 | } |
1572 | 0 | } |
1573 | | |
1574 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR; |
1575 | 0 | } |
1576 | | /* }}} */ |
1577 | | |
1578 | | ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */ |
1579 | 34 | { |
1580 | 34 | size_t prop_name_length = 1 + src1_length + 1 + src2_length; |
1581 | 34 | zend_string *prop_name = zend_string_alloc(prop_name_length, internal); |
1582 | | |
1583 | 34 | ZSTR_VAL(prop_name)[0] = '\0'; |
1584 | 34 | memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1); |
1585 | 34 | memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1); |
1586 | 34 | return prop_name; |
1587 | 34 | } |
1588 | | /* }}} */ |
1589 | | |
1590 | | ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len) /* {{{ */ |
1591 | 0 | { |
1592 | 0 | size_t class_name_len; |
1593 | 0 | size_t anonclass_src_len; |
1594 | |
|
1595 | 0 | *class_name = NULL; |
1596 | |
|
1597 | 0 | if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') { |
1598 | 0 | *prop_name = ZSTR_VAL(name); |
1599 | 0 | if (prop_len) { |
1600 | 0 | *prop_len = ZSTR_LEN(name); |
1601 | 0 | } |
1602 | 0 | return SUCCESS; |
1603 | 0 | } |
1604 | 0 | if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') { |
1605 | 0 | zend_error(E_NOTICE, "Illegal member variable name"); |
1606 | 0 | *prop_name = ZSTR_VAL(name); |
1607 | 0 | if (prop_len) { |
1608 | 0 | *prop_len = ZSTR_LEN(name); |
1609 | 0 | } |
1610 | 0 | return FAILURE; |
1611 | 0 | } |
1612 | | |
1613 | 0 | class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2); |
1614 | 0 | if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') { |
1615 | 0 | zend_error(E_NOTICE, "Corrupt member variable name"); |
1616 | 0 | *prop_name = ZSTR_VAL(name); |
1617 | 0 | if (prop_len) { |
1618 | 0 | *prop_len = ZSTR_LEN(name); |
1619 | 0 | } |
1620 | 0 | return FAILURE; |
1621 | 0 | } |
1622 | | |
1623 | 0 | *class_name = ZSTR_VAL(name) + 1; |
1624 | 0 | anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2); |
1625 | 0 | if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) { |
1626 | 0 | class_name_len += anonclass_src_len + 1; |
1627 | 0 | } |
1628 | 0 | *prop_name = ZSTR_VAL(name) + class_name_len + 2; |
1629 | 0 | if (prop_len) { |
1630 | 0 | *prop_len = ZSTR_LEN(name) - class_name_len - 2; |
1631 | 0 | } |
1632 | 0 | return SUCCESS; |
1633 | 0 | } |
1634 | | /* }}} */ |
1635 | | |
1636 | | static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks) |
1637 | 0 | { |
1638 | 0 | if (zend_hash_num_elements(array) > *max_checks) { |
1639 | 0 | return false; |
1640 | 0 | } |
1641 | 0 | *max_checks -= zend_hash_num_elements(array); |
1642 | |
|
1643 | 0 | zval *element; |
1644 | 0 | ZEND_HASH_FOREACH_VAL(array, element) { |
1645 | 0 | if (Z_TYPE_P(element) < IS_ARRAY) { |
1646 | 0 | continue; |
1647 | 0 | } else if (Z_TYPE_P(element) == IS_ARRAY) { |
1648 | 0 | if (!array_is_const_ex(array, max_checks)) { |
1649 | 0 | return false; |
1650 | 0 | } |
1651 | 0 | } else { |
1652 | 0 | return false; |
1653 | 0 | } |
1654 | 0 | } ZEND_HASH_FOREACH_END(); |
1655 | | |
1656 | 0 | return true; |
1657 | 0 | } |
1658 | | |
1659 | | static bool array_is_const(const zend_array *array) |
1660 | 0 | { |
1661 | 0 | uint32_t max_checks = 50; |
1662 | 0 | return array_is_const_ex(array, &max_checks); |
1663 | 0 | } |
1664 | | |
1665 | 0 | static bool can_ct_eval_const(const zend_constant *c) { |
1666 | 0 | if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) { |
1667 | 0 | return false; |
1668 | 0 | } |
1669 | 0 | if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) |
1670 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) |
1671 | 0 | && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) |
1672 | 0 | && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) { |
1673 | 0 | return true; |
1674 | 0 | } |
1675 | 0 | if (Z_TYPE(c->value) < IS_ARRAY |
1676 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1677 | 0 | return 1; |
1678 | 0 | } else if (Z_TYPE(c->value) == IS_ARRAY |
1679 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION) |
1680 | 0 | && array_is_const(Z_ARR(c->value))) { |
1681 | 0 | return 1; |
1682 | 0 | } |
1683 | 0 | return false; |
1684 | 0 | } |
1685 | | |
1686 | | static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */ |
1687 | 0 | { |
1688 | | /* Substitute true, false and null (including unqualified usage in namespaces) |
1689 | | * before looking up the possibly namespaced name. */ |
1690 | 0 | const char *lookup_name = ZSTR_VAL(name); |
1691 | 0 | size_t lookup_len = ZSTR_LEN(name); |
1692 | |
|
1693 | 0 | if (!is_fully_qualified) { |
1694 | 0 | zend_get_unqualified_name(name, &lookup_name, &lookup_len); |
1695 | 0 | } |
1696 | |
|
1697 | 0 | zend_constant *c; |
1698 | 0 | if ((c = zend_get_special_const(lookup_name, lookup_len))) { |
1699 | 0 | ZVAL_COPY_VALUE(zv, &c->value); |
1700 | 0 | return true; |
1701 | 0 | } |
1702 | 0 | c = zend_hash_find_ptr(EG(zend_constants), name); |
1703 | 0 | if (c && can_ct_eval_const(c)) { |
1704 | 0 | ZVAL_COPY_OR_DUP(zv, &c->value); |
1705 | 0 | return true; |
1706 | 0 | } |
1707 | 0 | return false; |
1708 | 0 | } |
1709 | | /* }}} */ |
1710 | | |
1711 | | static inline bool zend_is_scope_known(void) /* {{{ */ |
1712 | 0 | { |
1713 | 0 | if (!CG(active_op_array)) { |
1714 | | /* This can only happen when evaluating a default value string. */ |
1715 | 0 | return false; |
1716 | 0 | } |
1717 | | |
1718 | 0 | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
1719 | | /* Closures can be rebound to a different scope */ |
1720 | 0 | return false; |
1721 | 0 | } |
1722 | | |
1723 | 0 | if (!CG(active_class_entry)) { |
1724 | | /* The scope is known if we're in a free function (no scope), but not if we're in |
1725 | | * a file/eval (which inherits including/eval'ing scope). */ |
1726 | 0 | return CG(active_op_array)->function_name != NULL; |
1727 | 0 | } |
1728 | | |
1729 | | /* For traits self etc refers to the using class, not the trait itself */ |
1730 | 0 | return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0; |
1731 | 0 | } |
1732 | | /* }}} */ |
1733 | | |
1734 | | static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */ |
1735 | 0 | { |
1736 | 0 | if (!CG(active_class_entry)) { |
1737 | 0 | return false; |
1738 | 0 | } |
1739 | 0 | if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) { |
1740 | 0 | return true; |
1741 | 0 | } |
1742 | 0 | return fetch_type == ZEND_FETCH_CLASS_DEFAULT |
1743 | 0 | && zend_string_equals_ci(class_name, CG(active_class_entry)->name); |
1744 | 0 | } |
1745 | | /* }}} */ |
1746 | | |
1747 | | uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */ |
1748 | 0 | { |
1749 | 0 | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1750 | 0 | return ZEND_FETCH_CLASS_SELF; |
1751 | 0 | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
1752 | 0 | return ZEND_FETCH_CLASS_PARENT; |
1753 | 0 | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
1754 | 0 | return ZEND_FETCH_CLASS_STATIC; |
1755 | 0 | } else { |
1756 | 0 | return ZEND_FETCH_CLASS_DEFAULT; |
1757 | 0 | } |
1758 | 0 | } |
1759 | | /* }}} */ |
1760 | | |
1761 | | static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */ |
1762 | 0 | { |
1763 | | /* Fully qualified names are always default refs */ |
1764 | 0 | if (name_ast->attr == ZEND_NAME_FQ) { |
1765 | 0 | return ZEND_FETCH_CLASS_DEFAULT; |
1766 | 0 | } |
1767 | | |
1768 | 0 | return zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
1769 | 0 | } |
1770 | | /* }}} */ |
1771 | | |
1772 | | static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type) |
1773 | 0 | { |
1774 | 0 | zend_string *class_name = zend_ast_get_str(ast); |
1775 | 0 | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) { |
1776 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1777 | 0 | "Cannot use \"%s\" as %s, as it is reserved", |
1778 | 0 | ZSTR_VAL(class_name), type); |
1779 | 0 | } |
1780 | 0 | return zend_resolve_class_name(class_name, ast->attr); |
1781 | 0 | } |
1782 | | |
1783 | | static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */ |
1784 | 0 | { |
1785 | 0 | if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) { |
1786 | 0 | zend_class_entry *ce = CG(active_class_entry); |
1787 | 0 | if (!ce) { |
1788 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active", |
1789 | 0 | fetch_type == ZEND_FETCH_CLASS_SELF ? "self" : |
1790 | 0 | fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static"); |
1791 | 0 | } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) { |
1792 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1793 | 0 | "Cannot use \"parent\" when current class scope has no parent"); |
1794 | 0 | } |
1795 | 0 | } |
1796 | 0 | } |
1797 | | /* }}} */ |
1798 | | |
1799 | | static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */ |
1800 | 0 | { |
1801 | 0 | uint32_t fetch_type; |
1802 | 0 | const zval *class_name; |
1803 | |
|
1804 | 0 | if (class_ast->kind != ZEND_AST_ZVAL) { |
1805 | 0 | return false; |
1806 | 0 | } |
1807 | | |
1808 | 0 | class_name = zend_ast_get_zval(class_ast); |
1809 | |
|
1810 | 0 | if (Z_TYPE_P(class_name) != IS_STRING) { |
1811 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1812 | 0 | } |
1813 | | |
1814 | 0 | fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name)); |
1815 | 0 | zend_ensure_valid_class_fetch_type(fetch_type); |
1816 | |
|
1817 | 0 | switch (fetch_type) { |
1818 | 0 | case ZEND_FETCH_CLASS_SELF: |
1819 | 0 | if (CG(active_class_entry) && zend_is_scope_known()) { |
1820 | 0 | ZVAL_STR_COPY(zv, CG(active_class_entry)->name); |
1821 | 0 | return true; |
1822 | 0 | } |
1823 | 0 | return false; |
1824 | 0 | case ZEND_FETCH_CLASS_PARENT: |
1825 | 0 | if (CG(active_class_entry) && CG(active_class_entry)->parent_name |
1826 | 0 | && zend_is_scope_known()) { |
1827 | 0 | ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name); |
1828 | 0 | return true; |
1829 | 0 | } |
1830 | 0 | return false; |
1831 | 0 | case ZEND_FETCH_CLASS_STATIC: |
1832 | 0 | return false; |
1833 | 0 | case ZEND_FETCH_CLASS_DEFAULT: |
1834 | 0 | ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast)); |
1835 | 0 | return true; |
1836 | 0 | default: ZEND_UNREACHABLE(); |
1837 | 0 | } |
1838 | 0 | } |
1839 | | /* }}} */ |
1840 | | |
1841 | | /* We don't use zend_verify_const_access because we need to deal with unlinked classes. */ |
1842 | | static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope) |
1843 | 0 | { |
1844 | 0 | if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) { |
1845 | 0 | return 0; |
1846 | 0 | } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) { |
1847 | | /* This condition is only met on directly accessing trait constants, |
1848 | | * because the ce is replaced to the class entry of the composing class |
1849 | | * on binding. */ |
1850 | 0 | return 0; |
1851 | 0 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) { |
1852 | 0 | return 1; |
1853 | 0 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) { |
1854 | 0 | return c->ce == scope; |
1855 | 0 | } else { |
1856 | 0 | zend_class_entry *ce = c->ce; |
1857 | 0 | while (1) { |
1858 | 0 | if (ce == scope) { |
1859 | 0 | return 1; |
1860 | 0 | } |
1861 | 0 | if (!ce->parent) { |
1862 | 0 | break; |
1863 | 0 | } |
1864 | 0 | if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) { |
1865 | 0 | ce = ce->parent; |
1866 | 0 | } else { |
1867 | 0 | ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name); |
1868 | 0 | if (!ce) { |
1869 | 0 | break; |
1870 | 0 | } |
1871 | 0 | } |
1872 | 0 | } |
1873 | | /* Reverse case cannot be true during compilation */ |
1874 | 0 | return 0; |
1875 | 0 | } |
1876 | 0 | } |
1877 | | |
1878 | | static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */ |
1879 | 0 | { |
1880 | 0 | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
1881 | 0 | zend_class_constant *cc; |
1882 | 0 | zval *c; |
1883 | |
|
1884 | 0 | if (class_name_refers_to_active_ce(class_name, fetch_type)) { |
1885 | 0 | cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name); |
1886 | 0 | } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1887 | 0 | const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name); |
1888 | 0 | if (ce) { |
1889 | 0 | cc = zend_hash_find_ptr(&ce->constants_table, name); |
1890 | 0 | } else { |
1891 | 0 | return 0; |
1892 | 0 | } |
1893 | 0 | } else { |
1894 | 0 | return 0; |
1895 | 0 | } |
1896 | | |
1897 | 0 | if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) { |
1898 | 0 | return false; |
1899 | 0 | } |
1900 | | |
1901 | 0 | if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) { |
1902 | 0 | return false; |
1903 | 0 | } |
1904 | | |
1905 | 0 | c = &cc->value; |
1906 | | |
1907 | | /* Substitute case-sensitive (or lowercase) persistent class constants */ |
1908 | 0 | if (Z_TYPE_P(c) < IS_ARRAY) { |
1909 | 0 | ZVAL_COPY_OR_DUP(zv, c); |
1910 | 0 | return 1; |
1911 | 0 | } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) { |
1912 | 0 | ZVAL_COPY_OR_DUP(zv, c); |
1913 | 0 | return 1; |
1914 | 0 | } |
1915 | | |
1916 | 0 | return false; |
1917 | 0 | } |
1918 | | /* }}} */ |
1919 | | |
1920 | | static void zend_add_to_list(void *result, void *item) /* {{{ */ |
1921 | 0 | { |
1922 | 0 | void** list = *(void**)result; |
1923 | 0 | size_t n = 0; |
1924 | |
|
1925 | 0 | if (list) { |
1926 | 0 | while (list[n]) { |
1927 | 0 | n++; |
1928 | 0 | } |
1929 | 0 | } |
1930 | |
|
1931 | 0 | list = erealloc(list, sizeof(void*) * (n+2)); |
1932 | |
|
1933 | 0 | list[n] = item; |
1934 | 0 | list[n+1] = NULL; |
1935 | |
|
1936 | 0 | *(void**)result = list; |
1937 | 0 | } |
1938 | | /* }}} */ |
1939 | | |
1940 | | static void zend_do_extended_stmt(znode* result) /* {{{ */ |
1941 | 0 | { |
1942 | 0 | zend_op *opline; |
1943 | |
|
1944 | 0 | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) { |
1945 | 0 | return; |
1946 | 0 | } |
1947 | | |
1948 | 0 | opline = get_next_op(); |
1949 | |
|
1950 | 0 | opline->opcode = ZEND_EXT_STMT; |
1951 | 0 | if (result) { |
1952 | 0 | if (result->op_type == IS_CONST) { |
1953 | 0 | Z_TRY_ADDREF(result->u.constant); |
1954 | 0 | } |
1955 | 0 | SET_NODE(opline->op1, result); |
1956 | 0 | } |
1957 | 0 | } |
1958 | | /* }}} */ |
1959 | | |
1960 | | static void zend_do_extended_fcall_begin(void) /* {{{ */ |
1961 | 0 | { |
1962 | 0 | zend_op *opline; |
1963 | |
|
1964 | 0 | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1965 | 0 | return; |
1966 | 0 | } |
1967 | | |
1968 | 0 | opline = get_next_op(); |
1969 | |
|
1970 | 0 | opline->opcode = ZEND_EXT_FCALL_BEGIN; |
1971 | 0 | } |
1972 | | /* }}} */ |
1973 | | |
1974 | | static void zend_do_extended_fcall_end(void) /* {{{ */ |
1975 | 0 | { |
1976 | 0 | zend_op *opline; |
1977 | |
|
1978 | 0 | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1979 | 0 | return; |
1980 | 0 | } |
1981 | | |
1982 | 0 | opline = get_next_op(); |
1983 | |
|
1984 | 0 | opline->opcode = ZEND_EXT_FCALL_END; |
1985 | 0 | } |
1986 | | /* }}} */ |
1987 | | |
1988 | 0 | ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ { |
1989 | 0 | zend_auto_global *auto_global; |
1990 | |
|
1991 | 0 | if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) { |
1992 | 0 | if (auto_global->armed) { |
1993 | 0 | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
1994 | 0 | } |
1995 | 0 | return 1; |
1996 | 0 | } |
1997 | 0 | return 0; |
1998 | 0 | } |
1999 | | /* }}} */ |
2000 | | |
2001 | | ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */ |
2002 | 0 | { |
2003 | 0 | zend_auto_global *auto_global; |
2004 | |
|
2005 | 0 | if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) { |
2006 | 0 | if (auto_global->armed) { |
2007 | 0 | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2008 | 0 | } |
2009 | 0 | return 1; |
2010 | 0 | } |
2011 | 0 | return 0; |
2012 | 0 | } |
2013 | | /* }}} */ |
2014 | | |
2015 | | ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */ |
2016 | 16 | { |
2017 | 16 | zend_auto_global auto_global; |
2018 | 16 | zend_result retval; |
2019 | | |
2020 | 16 | auto_global.name = name; |
2021 | 16 | auto_global.auto_global_callback = auto_global_callback; |
2022 | 16 | auto_global.jit = jit; |
2023 | | |
2024 | 16 | retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE; |
2025 | | |
2026 | 16 | return retval; |
2027 | 16 | } |
2028 | | /* }}} */ |
2029 | | |
2030 | | ZEND_API void zend_activate_auto_globals(void) /* {{{ */ |
2031 | 1.99k | { |
2032 | 1.99k | zend_auto_global *auto_global; |
2033 | | |
2034 | 35.8k | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2035 | 35.8k | auto_global->armed = auto_global->jit || auto_global->auto_global_callback; |
2036 | 35.8k | } ZEND_HASH_FOREACH_END(); |
2037 | | |
2038 | 35.8k | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2039 | 35.8k | if (auto_global->armed && !auto_global->jit) { |
2040 | 7.96k | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2041 | 7.96k | } |
2042 | 35.8k | } ZEND_HASH_FOREACH_END(); |
2043 | 1.99k | } |
2044 | | /* }}} */ |
2045 | | |
2046 | | int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */ |
2047 | 0 | { |
2048 | 0 | zval zv; |
2049 | 0 | int ret; |
2050 | |
|
2051 | 0 | if (CG(increment_lineno)) { |
2052 | 0 | CG(zend_lineno)++; |
2053 | 0 | CG(increment_lineno) = 0; |
2054 | 0 | } |
2055 | |
|
2056 | 0 | ret = lex_scan(&zv, elem); |
2057 | 0 | ZEND_ASSERT(!EG(exception) || ret == T_ERROR); |
2058 | 0 | return ret; |
2059 | |
|
2060 | 0 | } |
2061 | | /* }}} */ |
2062 | | |
2063 | | ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */ |
2064 | 348 | { |
2065 | 348 | bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS; |
2066 | | |
2067 | 348 | ce->refcount = 1; |
2068 | 348 | ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED; |
2069 | 348 | ce->ce_flags2 = 0; |
2070 | | |
2071 | 348 | if (CG(compiler_options) & ZEND_COMPILE_GUARDS) { |
2072 | 0 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2073 | 0 | } |
2074 | | |
2075 | 348 | ce->default_properties_table = NULL; |
2076 | 348 | ce->default_static_members_table = NULL; |
2077 | 348 | zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes); |
2078 | 348 | zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes); |
2079 | 348 | zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes); |
2080 | | |
2081 | 348 | ce->doc_comment = NULL; |
2082 | | |
2083 | 348 | ZEND_MAP_PTR_INIT(ce->static_members_table, NULL); |
2084 | 348 | ZEND_MAP_PTR_INIT(ce->mutable_data, NULL); |
2085 | | |
2086 | 348 | ce->default_object_handlers = &std_object_handlers; |
2087 | 348 | ce->default_properties_count = 0; |
2088 | 348 | ce->default_static_members_count = 0; |
2089 | 348 | ce->properties_info_table = NULL; |
2090 | 348 | ce->attributes = NULL; |
2091 | 348 | ce->enum_backing_type = IS_UNDEF; |
2092 | 348 | ce->backed_enum_table = NULL; |
2093 | | |
2094 | 348 | if (nullify_handlers) { |
2095 | 0 | ce->constructor = NULL; |
2096 | 0 | ce->destructor = NULL; |
2097 | 0 | ce->clone = NULL; |
2098 | 0 | ce->__get = NULL; |
2099 | 0 | ce->__set = NULL; |
2100 | 0 | ce->__unset = NULL; |
2101 | 0 | ce->__isset = NULL; |
2102 | 0 | ce->__call = NULL; |
2103 | 0 | ce->__callstatic = NULL; |
2104 | 0 | ce->__tostring = NULL; |
2105 | 0 | ce->__serialize = NULL; |
2106 | 0 | ce->__unserialize = NULL; |
2107 | 0 | ce->__debugInfo = NULL; |
2108 | 0 | ce->create_object = NULL; |
2109 | 0 | ce->get_iterator = NULL; |
2110 | 0 | ce->iterator_funcs_ptr = NULL; |
2111 | 0 | ce->arrayaccess_funcs_ptr = NULL; |
2112 | 0 | ce->get_static_method = NULL; |
2113 | 0 | ce->parent = NULL; |
2114 | 0 | ce->parent_name = NULL; |
2115 | 0 | ce->num_interfaces = 0; |
2116 | 0 | ce->interfaces = NULL; |
2117 | 0 | ce->num_traits = 0; |
2118 | 0 | ce->num_hooked_props = 0; |
2119 | 0 | ce->num_hooked_prop_variance_checks = 0; |
2120 | 0 | ce->trait_names = NULL; |
2121 | 0 | ce->trait_aliases = NULL; |
2122 | 0 | ce->trait_precedences = NULL; |
2123 | 0 | ce->serialize = NULL; |
2124 | 0 | ce->unserialize = NULL; |
2125 | 0 | if (ce->type == ZEND_INTERNAL_CLASS) { |
2126 | 0 | ce->info.internal.module = NULL; |
2127 | 0 | ce->info.internal.builtin_functions = NULL; |
2128 | 0 | } |
2129 | 0 | } |
2130 | 348 | } |
2131 | | /* }}} */ |
2132 | | |
2133 | | ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */ |
2134 | 0 | { |
2135 | 0 | return op_array->vars[EX_VAR_TO_NUM(var)]; |
2136 | 0 | } |
2137 | | /* }}} */ |
2138 | | |
2139 | | zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */ |
2140 | 0 | { |
2141 | 0 | zval *left_zv = zend_ast_get_zval(left_ast); |
2142 | 0 | zend_string *left = Z_STR_P(left_zv); |
2143 | 0 | zend_string *right = zend_ast_get_str(right_ast); |
2144 | |
|
2145 | 0 | zend_string *result; |
2146 | 0 | size_t left_len = ZSTR_LEN(left); |
2147 | 0 | size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */ |
2148 | |
|
2149 | 0 | result = zend_string_extend(left, len, 0); |
2150 | 0 | ZSTR_VAL(result)[left_len] = '\\'; |
2151 | 0 | memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right)); |
2152 | 0 | ZSTR_VAL(result)[len] = '\0'; |
2153 | 0 | zend_string_release_ex(right, 0); |
2154 | |
|
2155 | 0 | ZVAL_STR(left_zv, result); |
2156 | 0 | return left_ast; |
2157 | 0 | } |
2158 | | /* }}} */ |
2159 | | |
2160 | | zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */ |
2161 | 0 | { |
2162 | 0 | zval *zv = zend_ast_get_zval(ast); |
2163 | 0 | if (Z_TYPE_P(zv) == IS_LONG) { |
2164 | 0 | if (Z_LVAL_P(zv) == 0) { |
2165 | 0 | ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0)); |
2166 | 0 | } else { |
2167 | 0 | ZEND_ASSERT(Z_LVAL_P(zv) > 0); |
2168 | 0 | Z_LVAL_P(zv) *= -1; |
2169 | 0 | } |
2170 | 0 | } else if (Z_TYPE_P(zv) == IS_STRING) { |
2171 | 0 | size_t orig_len = Z_STRLEN_P(zv); |
2172 | 0 | Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0); |
2173 | 0 | memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1); |
2174 | 0 | Z_STRVAL_P(zv)[0] = '-'; |
2175 | 0 | } else { |
2176 | 0 | ZEND_UNREACHABLE(); |
2177 | 0 | } |
2178 | 0 | return ast; |
2179 | 0 | } |
2180 | | /* }}} */ |
2181 | | |
2182 | | static void zend_verify_namespace(void) /* {{{ */ |
2183 | 0 | { |
2184 | 0 | if (FC(has_bracketed_namespaces) && !FC(in_namespace)) { |
2185 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}"); |
2186 | 0 | } |
2187 | 0 | } |
2188 | | /* }}} */ |
2189 | | |
2190 | | /* {{{ zend_dirname |
2191 | | Returns directory name component of path */ |
2192 | | ZEND_API size_t zend_dirname(char *path, size_t len) |
2193 | 0 | { |
2194 | 0 | char *end = path + len - 1; |
2195 | 0 | unsigned int len_adjust = 0; |
2196 | |
|
2197 | | #ifdef ZEND_WIN32 |
2198 | | /* Note that on Win32 CWD is per drive (heritage from CP/M). |
2199 | | * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive. |
2200 | | */ |
2201 | | if ((2 <= len) && isalpha((unsigned char)path[0]) && (':' == path[1])) { |
2202 | | /* Skip over the drive spec (if any) so as not to change */ |
2203 | | path += 2; |
2204 | | len_adjust += 2; |
2205 | | if (2 == len) { |
2206 | | /* Return "c:" on Win32 for dirname("c:"). |
2207 | | * It would be more consistent to return "c:." |
2208 | | * but that would require making the string *longer*. |
2209 | | */ |
2210 | | return len; |
2211 | | } |
2212 | | } |
2213 | | #endif |
2214 | |
|
2215 | 0 | if (len == 0) { |
2216 | | /* Illegal use of this function */ |
2217 | 0 | return 0; |
2218 | 0 | } |
2219 | | |
2220 | | /* Strip trailing slashes */ |
2221 | 0 | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2222 | 0 | end--; |
2223 | 0 | } |
2224 | 0 | if (end < path) { |
2225 | | /* The path only contained slashes */ |
2226 | 0 | path[0] = DEFAULT_SLASH; |
2227 | 0 | path[1] = '\0'; |
2228 | 0 | return 1 + len_adjust; |
2229 | 0 | } |
2230 | | |
2231 | | /* Strip filename */ |
2232 | 0 | while (end >= path && !IS_SLASH_P_EX(end, end == path)) { |
2233 | 0 | end--; |
2234 | 0 | } |
2235 | 0 | if (end < path) { |
2236 | | /* No slash found, therefore return '.' */ |
2237 | 0 | path[0] = '.'; |
2238 | 0 | path[1] = '\0'; |
2239 | 0 | return 1 + len_adjust; |
2240 | 0 | } |
2241 | | |
2242 | | /* Strip slashes which came before the file name */ |
2243 | 0 | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2244 | 0 | end--; |
2245 | 0 | } |
2246 | 0 | if (end < path) { |
2247 | 0 | path[0] = DEFAULT_SLASH; |
2248 | 0 | path[1] = '\0'; |
2249 | 0 | return 1 + len_adjust; |
2250 | 0 | } |
2251 | 0 | *(end+1) = '\0'; |
2252 | |
|
2253 | 0 | return (size_t)(end + 1 - path) + len_adjust; |
2254 | 0 | } |
2255 | | /* }}} */ |
2256 | | |
2257 | | static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */ |
2258 | 0 | { |
2259 | 0 | uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3; |
2260 | |
|
2261 | 0 | switch (type) { |
2262 | 0 | case BP_VAR_R: |
2263 | 0 | opline->result_type = IS_TMP_VAR; |
2264 | 0 | result->op_type = IS_TMP_VAR; |
2265 | 0 | return; |
2266 | 0 | case BP_VAR_W: |
2267 | 0 | opline->opcode += 1 * factor; |
2268 | 0 | return; |
2269 | 0 | case BP_VAR_RW: |
2270 | 0 | opline->opcode += 2 * factor; |
2271 | 0 | return; |
2272 | 0 | case BP_VAR_IS: |
2273 | 0 | opline->result_type = IS_TMP_VAR; |
2274 | 0 | result->op_type = IS_TMP_VAR; |
2275 | 0 | opline->opcode += 3 * factor; |
2276 | 0 | return; |
2277 | 0 | case BP_VAR_FUNC_ARG: |
2278 | 0 | opline->opcode += 4 * factor; |
2279 | 0 | return; |
2280 | 0 | case BP_VAR_UNSET: |
2281 | 0 | opline->opcode += 5 * factor; |
2282 | 0 | return; |
2283 | 0 | default: ZEND_UNREACHABLE(); |
2284 | 0 | } |
2285 | 0 | } |
2286 | | /* }}} */ |
2287 | | |
2288 | | static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */ |
2289 | 0 | { |
2290 | 0 | opline->result_type = IS_VAR; |
2291 | 0 | opline->result.var = get_temporary_variable(); |
2292 | 0 | GET_NODE(result, opline->result); |
2293 | 0 | } |
2294 | | /* }}} */ |
2295 | | |
2296 | | static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */ |
2297 | 0 | { |
2298 | 0 | opline->result_type = IS_TMP_VAR; |
2299 | 0 | opline->result.var = get_temporary_variable(); |
2300 | 0 | GET_NODE(result, opline->result); |
2301 | 0 | } |
2302 | | /* }}} */ |
2303 | | |
2304 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2305 | 0 | { |
2306 | 0 | zend_op *opline = get_next_op(); |
2307 | 0 | opline->opcode = opcode; |
2308 | |
|
2309 | 0 | if (op1 != NULL) { |
2310 | 0 | SET_NODE(opline->op1, op1); |
2311 | 0 | } |
2312 | |
|
2313 | 0 | if (op2 != NULL) { |
2314 | 0 | SET_NODE(opline->op2, op2); |
2315 | 0 | } |
2316 | |
|
2317 | 0 | if (result) { |
2318 | 0 | zend_make_var_result(result, opline); |
2319 | 0 | } |
2320 | 0 | return opline; |
2321 | 0 | } |
2322 | | /* }}} */ |
2323 | | |
2324 | | static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2325 | 0 | { |
2326 | 0 | zend_op *opline = get_next_op(); |
2327 | 0 | opline->opcode = opcode; |
2328 | |
|
2329 | 0 | if (op1 != NULL) { |
2330 | 0 | SET_NODE(opline->op1, op1); |
2331 | 0 | } |
2332 | |
|
2333 | 0 | if (op2 != NULL) { |
2334 | 0 | SET_NODE(opline->op2, op2); |
2335 | 0 | } |
2336 | |
|
2337 | 0 | if (result) { |
2338 | 0 | zend_make_tmp_result(result, opline); |
2339 | 0 | } |
2340 | |
|
2341 | 0 | return opline; |
2342 | 0 | } |
2343 | | /* }}} */ |
2344 | | |
2345 | | static void zend_emit_tick(void) /* {{{ */ |
2346 | 0 | { |
2347 | 0 | zend_op *opline; |
2348 | | |
2349 | | /* This prevents a double TICK generated by the parser statement of "declare()" */ |
2350 | 0 | if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) { |
2351 | 0 | return; |
2352 | 0 | } |
2353 | | |
2354 | 0 | opline = get_next_op(); |
2355 | |
|
2356 | 0 | opline->opcode = ZEND_TICKS; |
2357 | 0 | opline->extended_value = FC(declarables).ticks; |
2358 | 0 | } |
2359 | | /* }}} */ |
2360 | | |
2361 | | static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */ |
2362 | 0 | { |
2363 | 0 | return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL); |
2364 | 0 | } |
2365 | | /* }}} */ |
2366 | | |
2367 | | static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */ |
2368 | 0 | { |
2369 | 0 | uint32_t opnum = get_next_op_number(); |
2370 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
2371 | 0 | opline->op1.opline_num = opnum_target; |
2372 | 0 | return opnum; |
2373 | 0 | } |
2374 | | /* }}} */ |
2375 | | |
2376 | | ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */ |
2377 | 0 | { |
2378 | 0 | switch (opline->opcode) { |
2379 | 0 | case ZEND_IS_IDENTICAL: |
2380 | 0 | case ZEND_IS_NOT_IDENTICAL: |
2381 | 0 | case ZEND_IS_EQUAL: |
2382 | 0 | case ZEND_IS_NOT_EQUAL: |
2383 | 0 | case ZEND_IS_SMALLER: |
2384 | 0 | case ZEND_IS_SMALLER_OR_EQUAL: |
2385 | 0 | case ZEND_CASE: |
2386 | 0 | case ZEND_CASE_STRICT: |
2387 | 0 | case ZEND_ISSET_ISEMPTY_CV: |
2388 | 0 | case ZEND_ISSET_ISEMPTY_VAR: |
2389 | 0 | case ZEND_ISSET_ISEMPTY_DIM_OBJ: |
2390 | 0 | case ZEND_ISSET_ISEMPTY_PROP_OBJ: |
2391 | 0 | case ZEND_ISSET_ISEMPTY_STATIC_PROP: |
2392 | 0 | case ZEND_INSTANCEOF: |
2393 | 0 | case ZEND_TYPE_CHECK: |
2394 | 0 | case ZEND_DEFINED: |
2395 | 0 | case ZEND_IN_ARRAY: |
2396 | 0 | case ZEND_ARRAY_KEY_EXISTS: |
2397 | 0 | return 1; |
2398 | 0 | default: |
2399 | 0 | return 0; |
2400 | 0 | } |
2401 | 0 | } |
2402 | | /* }}} */ |
2403 | | |
2404 | | static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */ |
2405 | 0 | { |
2406 | 0 | uint32_t opnum = get_next_op_number(); |
2407 | 0 | zend_op *opline; |
2408 | |
|
2409 | 0 | if (cond->op_type == IS_TMP_VAR && opnum > 0) { |
2410 | 0 | opline = CG(active_op_array)->opcodes + opnum - 1; |
2411 | 0 | if (opline->result_type == IS_TMP_VAR |
2412 | 0 | && opline->result.var == cond->u.op.var |
2413 | 0 | && zend_is_smart_branch(opline)) { |
2414 | 0 | if (opcode == ZEND_JMPZ) { |
2415 | 0 | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ; |
2416 | 0 | } else { |
2417 | 0 | ZEND_ASSERT(opcode == ZEND_JMPNZ); |
2418 | 0 | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ; |
2419 | 0 | } |
2420 | 0 | } |
2421 | 0 | } |
2422 | 0 | opline = zend_emit_op(NULL, opcode, cond, NULL); |
2423 | 0 | opline->op2.opline_num = opnum_target; |
2424 | 0 | return opnum; |
2425 | 0 | } |
2426 | | /* }}} */ |
2427 | | |
2428 | | static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */ |
2429 | 0 | { |
2430 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump]; |
2431 | 0 | switch (opline->opcode) { |
2432 | 0 | case ZEND_JMP: |
2433 | 0 | opline->op1.opline_num = opnum_target; |
2434 | 0 | break; |
2435 | 0 | case ZEND_JMPZ: |
2436 | 0 | case ZEND_JMPNZ: |
2437 | 0 | case ZEND_JMPZ_EX: |
2438 | 0 | case ZEND_JMPNZ_EX: |
2439 | 0 | case ZEND_JMP_SET: |
2440 | 0 | case ZEND_COALESCE: |
2441 | 0 | case ZEND_JMP_NULL: |
2442 | 0 | case ZEND_BIND_INIT_STATIC_OR_JMP: |
2443 | 0 | case ZEND_JMP_FRAMELESS: |
2444 | 0 | opline->op2.opline_num = opnum_target; |
2445 | 0 | break; |
2446 | 0 | default: ZEND_UNREACHABLE(); |
2447 | 0 | } |
2448 | 0 | } |
2449 | | /* }}} */ |
2450 | | |
2451 | | static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */ |
2452 | 0 | { |
2453 | 0 | zend_update_jump_target(opnum_jump, get_next_op_number()); |
2454 | 0 | } |
2455 | | /* }}} */ |
2456 | | |
2457 | | static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2458 | 0 | { |
2459 | 0 | zend_op tmp_opline; |
2460 | |
|
2461 | 0 | init_op(&tmp_opline); |
2462 | |
|
2463 | 0 | tmp_opline.opcode = opcode; |
2464 | 0 | if (op1 != NULL) { |
2465 | 0 | SET_NODE(tmp_opline.op1, op1); |
2466 | 0 | } |
2467 | 0 | if (op2 != NULL) { |
2468 | 0 | SET_NODE(tmp_opline.op2, op2); |
2469 | 0 | } |
2470 | 0 | if (result) { |
2471 | 0 | zend_make_var_result(result, &tmp_opline); |
2472 | 0 | } |
2473 | |
|
2474 | 0 | zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline); |
2475 | 0 | return zend_stack_top(&CG(delayed_oplines_stack)); |
2476 | 0 | } |
2477 | | /* }}} */ |
2478 | | |
2479 | | static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */ |
2480 | 0 | { |
2481 | 0 | return zend_stack_count(&CG(delayed_oplines_stack)); |
2482 | 0 | } |
2483 | | /* }}} */ |
2484 | | |
2485 | | static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */ |
2486 | 0 | { |
2487 | 0 | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
2488 | 0 | uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack)); |
2489 | |
|
2490 | 0 | ZEND_ASSERT(count >= offset); |
2491 | 0 | for (i = offset; i < count; ++i) { |
2492 | 0 | if (EXPECTED(oplines[i].opcode != ZEND_NOP)) { |
2493 | 0 | opline = get_next_op(); |
2494 | 0 | memcpy(opline, &oplines[i], sizeof(zend_op)); |
2495 | 0 | } else { |
2496 | 0 | opline = CG(active_op_array)->opcodes + oplines[i].extended_value; |
2497 | 0 | } |
2498 | 0 | } |
2499 | |
|
2500 | 0 | CG(delayed_oplines_stack).top = offset; |
2501 | 0 | return opline; |
2502 | 0 | } |
2503 | | /* }}} */ |
2504 | | |
2505 | | static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind) |
2506 | 0 | { |
2507 | 0 | switch (ast_kind) { |
2508 | 0 | case ZEND_AST_DIM: |
2509 | 0 | case ZEND_AST_PROP: |
2510 | 0 | case ZEND_AST_NULLSAFE_PROP: |
2511 | 0 | case ZEND_AST_STATIC_PROP: |
2512 | 0 | case ZEND_AST_METHOD_CALL: |
2513 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2514 | 0 | case ZEND_AST_STATIC_CALL: |
2515 | 0 | return true; |
2516 | 0 | default: |
2517 | 0 | return false; |
2518 | 0 | } |
2519 | 0 | } |
2520 | | |
2521 | | static bool zend_ast_is_short_circuited(const zend_ast *ast) |
2522 | 0 | { |
2523 | 0 | switch (ast->kind) { |
2524 | 0 | case ZEND_AST_DIM: |
2525 | 0 | case ZEND_AST_PROP: |
2526 | 0 | case ZEND_AST_STATIC_PROP: |
2527 | 0 | case ZEND_AST_METHOD_CALL: |
2528 | 0 | case ZEND_AST_STATIC_CALL: |
2529 | 0 | return zend_ast_is_short_circuited(ast->child[0]); |
2530 | 0 | case ZEND_AST_NULLSAFE_PROP: |
2531 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2532 | 0 | return true; |
2533 | 0 | default: |
2534 | 0 | return false; |
2535 | 0 | } |
2536 | 0 | } |
2537 | | |
2538 | | static void zend_assert_not_short_circuited(const zend_ast *ast) |
2539 | 0 | { |
2540 | 0 | if (zend_ast_is_short_circuited(ast)) { |
2541 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain"); |
2542 | 0 | } |
2543 | 0 | } |
2544 | | |
2545 | | /* Mark nodes that are an inner part of a short-circuiting chain. |
2546 | | * We should not perform a "commit" on them, as it will be performed by the outer-most node. |
2547 | | * We do this to avoid passing down an argument in various compile functions. */ |
2548 | | |
2549 | 0 | #define ZEND_SHORT_CIRCUITING_INNER 0x8000 |
2550 | | |
2551 | 0 | static void zend_short_circuiting_mark_inner(zend_ast *ast) { |
2552 | 0 | if (zend_ast_kind_is_short_circuited(ast->kind)) { |
2553 | 0 | ast->attr |= ZEND_SHORT_CIRCUITING_INNER; |
2554 | 0 | } |
2555 | 0 | } |
2556 | | |
2557 | | static uint32_t zend_short_circuiting_checkpoint(void) |
2558 | 0 | { |
2559 | 0 | return zend_stack_count(&CG(short_circuiting_opnums)); |
2560 | 0 | } |
2561 | | |
2562 | | static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast) |
2563 | 0 | { |
2564 | 0 | bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind) |
2565 | 0 | || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY; |
2566 | 0 | if (!is_short_circuited) { |
2567 | 0 | ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint |
2568 | 0 | && "Short circuiting stack should be empty"); |
2569 | 0 | return; |
2570 | 0 | } |
2571 | | |
2572 | 0 | if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) { |
2573 | | /* Outer-most node will commit. */ |
2574 | 0 | return; |
2575 | 0 | } |
2576 | | |
2577 | 0 | while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) { |
2578 | 0 | uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums)); |
2579 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[opnum]; |
2580 | 0 | opline->op2.opline_num = get_next_op_number(); |
2581 | 0 | SET_NODE(opline->result, result); |
2582 | 0 | opline->extended_value |= |
2583 | 0 | ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET : |
2584 | 0 | ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY : |
2585 | 0 | ZEND_SHORT_CIRCUITING_CHAIN_EXPR; |
2586 | 0 | zend_stack_del_top(&CG(short_circuiting_opnums)); |
2587 | 0 | } |
2588 | 0 | } |
2589 | | |
2590 | | static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type) |
2591 | 0 | { |
2592 | 0 | uint32_t jmp_null_opnum = get_next_op_number(); |
2593 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL); |
2594 | 0 | if (opline->op1_type == IS_CONST) { |
2595 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
2596 | 0 | } |
2597 | 0 | if (bp_type == BP_VAR_IS) { |
2598 | 0 | opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS; |
2599 | 0 | } |
2600 | 0 | zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum); |
2601 | 0 | } |
2602 | | |
2603 | | static inline bool zend_is_variable_or_call(const zend_ast *ast); |
2604 | | |
2605 | | static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */ |
2606 | 0 | { |
2607 | 0 | const zend_memoize_mode memoize_mode = CG(memoize_mode); |
2608 | 0 | if (memoize_mode == ZEND_MEMOIZE_COMPILE) { |
2609 | 0 | znode memoized_result; |
2610 | | |
2611 | | /* Go through normal compilation */ |
2612 | 0 | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
2613 | 0 | if (zend_is_variable_or_call(expr)) { |
2614 | 0 | zend_compile_var(result, expr, type, /* by_ref */ false); |
2615 | 0 | } else { |
2616 | 0 | zend_compile_expr(result, expr); |
2617 | 0 | } |
2618 | 0 | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
2619 | |
|
2620 | 0 | if (result->op_type == IS_VAR) { |
2621 | 0 | zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2622 | 0 | } else if (result->op_type == IS_TMP_VAR) { |
2623 | 0 | zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2624 | 0 | } else { |
2625 | 0 | if (result->op_type == IS_CONST) { |
2626 | 0 | Z_TRY_ADDREF(result->u.constant); |
2627 | 0 | } |
2628 | 0 | memoized_result = *result; |
2629 | 0 | } |
2630 | |
|
2631 | 0 | zend_hash_index_update_mem( |
2632 | 0 | CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode)); |
2633 | 0 | } else if (memoize_mode == ZEND_MEMOIZE_FETCH) { |
2634 | 0 | const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr); |
2635 | 0 | *result = *memoized_result; |
2636 | 0 | if (result->op_type == IS_CONST) { |
2637 | 0 | Z_TRY_ADDREF(result->u.constant); |
2638 | 0 | } |
2639 | 0 | } else { |
2640 | 0 | ZEND_UNREACHABLE(); |
2641 | 0 | } |
2642 | 0 | } |
2643 | | /* }}} */ |
2644 | | |
2645 | | static void zend_emit_return_type_check( |
2646 | | znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */ |
2647 | 0 | { |
2648 | 0 | zend_type type = return_info->type; |
2649 | 0 | if (ZEND_TYPE_IS_SET(type)) { |
2650 | 0 | zend_op *opline; |
2651 | | |
2652 | | /* `return ...;` is illegal in a void function (but `return;` isn't) */ |
2653 | 0 | if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) { |
2654 | 0 | if (expr) { |
2655 | 0 | if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) { |
2656 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
2657 | 0 | "A void %s must not return a value " |
2658 | 0 | "(did you mean \"return;\" instead of \"return null;\"?)", |
2659 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
2660 | 0 | } else { |
2661 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value", |
2662 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
2663 | 0 | } |
2664 | 0 | } |
2665 | | /* we don't need run-time check */ |
2666 | 0 | return; |
2667 | 0 | } |
2668 | | |
2669 | | /* `return` is illegal in a never-returning function */ |
2670 | 0 | if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) { |
2671 | | /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */ |
2672 | 0 | ZEND_ASSERT(!implicit); |
2673 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return", |
2674 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
2675 | 0 | } |
2676 | | |
2677 | 0 | if (!expr && !implicit) { |
2678 | 0 | if (ZEND_TYPE_ALLOW_NULL(type)) { |
2679 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
2680 | 0 | "A %s with return type must return a value " |
2681 | 0 | "(did you mean \"return null;\" instead of \"return;\"?)", |
2682 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
2683 | 0 | } else { |
2684 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
2685 | 0 | "A %s with return type must return a value", |
2686 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
2687 | 0 | } |
2688 | 0 | } |
2689 | | |
2690 | 0 | if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) { |
2691 | | /* we don't need run-time check for mixed return type */ |
2692 | 0 | return; |
2693 | 0 | } |
2694 | | |
2695 | 0 | if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) { |
2696 | | /* we don't need run-time check */ |
2697 | 0 | return; |
2698 | 0 | } |
2699 | | |
2700 | 0 | opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL); |
2701 | 0 | if (expr && expr->op_type == IS_CONST) { |
2702 | 0 | opline->result_type = expr->op_type = IS_TMP_VAR; |
2703 | 0 | opline->result.var = expr->u.op.var = get_temporary_variable(); |
2704 | 0 | } |
2705 | 0 | } |
2706 | 0 | } |
2707 | | /* }}} */ |
2708 | | |
2709 | | void zend_emit_final_return(bool return_one) /* {{{ */ |
2710 | 0 | { |
2711 | 0 | znode zn; |
2712 | 0 | zend_op *ret; |
2713 | 0 | bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
2714 | |
|
2715 | 0 | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) |
2716 | 0 | && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) { |
2717 | 0 | zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
2718 | |
|
2719 | 0 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
2720 | 0 | zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL); |
2721 | 0 | return; |
2722 | 0 | } |
2723 | | |
2724 | 0 | zend_emit_return_type_check(NULL, return_info, true); |
2725 | 0 | } |
2726 | | |
2727 | 0 | zn.op_type = IS_CONST; |
2728 | 0 | if (return_one) { |
2729 | 0 | ZVAL_LONG(&zn.u.constant, 1); |
2730 | 0 | } else { |
2731 | 0 | ZVAL_NULL(&zn.u.constant); |
2732 | 0 | } |
2733 | |
|
2734 | 0 | ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL); |
2735 | 0 | ret->extended_value = -1; |
2736 | 0 | } |
2737 | | /* }}} */ |
2738 | | |
2739 | | static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */ |
2740 | 0 | { |
2741 | 0 | return ast->kind == ZEND_AST_VAR |
2742 | 0 | || ast->kind == ZEND_AST_DIM |
2743 | 0 | || ast->kind == ZEND_AST_PROP |
2744 | 0 | || ast->kind == ZEND_AST_NULLSAFE_PROP |
2745 | 0 | || ast->kind == ZEND_AST_STATIC_PROP; |
2746 | 0 | } |
2747 | | /* }}} */ |
2748 | | |
2749 | | static bool zend_propagate_list_refs(zend_ast *ast); |
2750 | | |
2751 | | static inline bool zend_is_passable_by_ref(const zend_ast *ast) |
2752 | 0 | { |
2753 | 0 | if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) { |
2754 | 0 | return true; |
2755 | 0 | } |
2756 | 0 | if (ast->kind == ZEND_AST_ASSIGN |
2757 | 0 | && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY) |
2758 | 0 | && zend_propagate_list_refs(ast->child[0])) { |
2759 | 0 | return true; |
2760 | 0 | } |
2761 | 0 | return false; |
2762 | 0 | } |
2763 | | |
2764 | | static inline bool zend_is_call(const zend_ast *ast) /* {{{ */ |
2765 | 0 | { |
2766 | 0 | return ast->kind == ZEND_AST_CALL |
2767 | 0 | || ast->kind == ZEND_AST_METHOD_CALL |
2768 | 0 | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
2769 | 0 | || ast->kind == ZEND_AST_STATIC_CALL |
2770 | 0 | || ast->kind == ZEND_AST_PIPE; |
2771 | 0 | } |
2772 | | /* }}} */ |
2773 | | |
2774 | | static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */ |
2775 | 0 | { |
2776 | 0 | return zend_is_variable(ast) || zend_is_call(ast); |
2777 | 0 | } |
2778 | | /* }}} */ |
2779 | | |
2780 | | static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */ |
2781 | 0 | { |
2782 | 0 | return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL |
2783 | 0 | || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP |
2784 | 0 | || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD; |
2785 | 0 | } |
2786 | | /* }}} */ |
2787 | | |
2788 | | static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */ |
2789 | 0 | { |
2790 | 0 | while ( |
2791 | 0 | ast->kind == ZEND_AST_DIM |
2792 | 0 | || ast->kind == ZEND_AST_PROP |
2793 | 0 | ) { |
2794 | 0 | ast = ast->child[0]; |
2795 | 0 | } |
2796 | |
|
2797 | 0 | return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast); |
2798 | 0 | } |
2799 | | /* }}} */ |
2800 | | |
2801 | | static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */ |
2802 | 0 | { |
2803 | 0 | if (name_ast->kind != ZEND_AST_ZVAL) { |
2804 | 0 | return false; |
2805 | 0 | } |
2806 | | |
2807 | 0 | return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast); |
2808 | 0 | } |
2809 | | /* }}} */ |
2810 | | |
2811 | | static inline void zend_handle_numeric_op(znode *node) /* {{{ */ |
2812 | 0 | { |
2813 | 0 | if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) { |
2814 | 0 | zend_ulong index; |
2815 | |
|
2816 | 0 | if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) { |
2817 | 0 | zval_ptr_dtor(&node->u.constant); |
2818 | 0 | ZVAL_LONG(&node->u.constant, index); |
2819 | 0 | } |
2820 | 0 | } |
2821 | 0 | } |
2822 | | /* }}} */ |
2823 | | |
2824 | | static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */ |
2825 | 0 | { |
2826 | 0 | if (Z_TYPE(dim_node->u.constant) == IS_STRING) { |
2827 | 0 | zend_ulong index; |
2828 | |
|
2829 | 0 | if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) { |
2830 | | /* For numeric indexes we also keep the original value to use by ArrayAccess |
2831 | | * See bug #63217 |
2832 | | */ |
2833 | 0 | int c = zend_add_literal(&dim_node->u.constant); |
2834 | 0 | ZEND_ASSERT(opline->op2.constant + 1 == c); |
2835 | 0 | ZVAL_LONG(CT_CONSTANT(opline->op2), index); |
2836 | 0 | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE; |
2837 | 0 | return; |
2838 | 0 | } |
2839 | 0 | } |
2840 | 0 | } |
2841 | | /* }}} */ |
2842 | | |
2843 | | static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */ |
2844 | 0 | { |
2845 | 0 | if (class_node->op_type == IS_CONST) { |
2846 | 0 | opline->op1_type = IS_CONST; |
2847 | 0 | opline->op1.constant = zend_add_class_name_literal( |
2848 | 0 | Z_STR(class_node->u.constant)); |
2849 | 0 | } else { |
2850 | 0 | SET_NODE(opline->op1, class_node); |
2851 | 0 | } |
2852 | 0 | } |
2853 | | /* }}} */ |
2854 | | |
2855 | | static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */ |
2856 | 0 | { |
2857 | 0 | uint32_t fetch_type; |
2858 | |
|
2859 | 0 | if (name_ast->kind != ZEND_AST_ZVAL) { |
2860 | 0 | znode name_node; |
2861 | |
|
2862 | 0 | zend_compile_expr(&name_node, name_ast); |
2863 | |
|
2864 | 0 | if (name_node.op_type == IS_CONST) { |
2865 | 0 | zend_string *name; |
2866 | |
|
2867 | 0 | if (Z_TYPE(name_node.u.constant) != IS_STRING) { |
2868 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
2869 | 0 | } |
2870 | | |
2871 | 0 | name = Z_STR(name_node.u.constant); |
2872 | 0 | fetch_type = zend_get_class_fetch_type(name); |
2873 | |
|
2874 | 0 | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
2875 | 0 | result->op_type = IS_CONST; |
2876 | 0 | ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ)); |
2877 | 0 | } else { |
2878 | 0 | zend_ensure_valid_class_fetch_type(fetch_type); |
2879 | 0 | result->op_type = IS_UNUSED; |
2880 | 0 | result->u.op.num = fetch_type | fetch_flags; |
2881 | 0 | } |
2882 | |
|
2883 | 0 | zend_string_release_ex(name, 0); |
2884 | 0 | } else { |
2885 | 0 | zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node); |
2886 | 0 | opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags; |
2887 | 0 | } |
2888 | 0 | return; |
2889 | 0 | } |
2890 | | |
2891 | | /* Fully qualified names are always default refs */ |
2892 | 0 | if (name_ast->attr == ZEND_NAME_FQ) { |
2893 | 0 | result->op_type = IS_CONST; |
2894 | 0 | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2895 | 0 | return; |
2896 | 0 | } |
2897 | | |
2898 | 0 | fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
2899 | 0 | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
2900 | 0 | result->op_type = IS_CONST; |
2901 | 0 | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2902 | 0 | } else { |
2903 | 0 | zend_ensure_valid_class_fetch_type(fetch_type); |
2904 | 0 | result->op_type = IS_UNUSED; |
2905 | 0 | result->u.op.num = fetch_type | fetch_flags; |
2906 | 0 | } |
2907 | 0 | } |
2908 | | /* }}} */ |
2909 | | |
2910 | | static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
2911 | 0 | { |
2912 | 0 | zend_ast *name_ast = ast->child[0]; |
2913 | 0 | if (name_ast->kind == ZEND_AST_ZVAL) { |
2914 | 0 | zval *zv = zend_ast_get_zval(name_ast); |
2915 | 0 | zend_string *name; |
2916 | |
|
2917 | 0 | if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) { |
2918 | 0 | name = zval_make_interned_string(zv); |
2919 | 0 | } else { |
2920 | 0 | name = zend_new_interned_string(zval_get_string_func(zv)); |
2921 | 0 | } |
2922 | |
|
2923 | 0 | if (zend_is_auto_global(name)) { |
2924 | 0 | return FAILURE; |
2925 | 0 | } |
2926 | | |
2927 | 0 | if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) { |
2928 | 0 | if (type == BP_VAR_R) { |
2929 | 0 | zend_error(E_DEPRECATED, |
2930 | 0 | "The predefined locally scoped $http_response_header variable is deprecated," |
2931 | 0 | " call http_get_last_response_headers() instead"); |
2932 | 0 | } else if (type == BP_VAR_W) { |
2933 | 0 | CG(context).has_assigned_to_http_response_header = true; |
2934 | 0 | } |
2935 | 0 | } |
2936 | |
|
2937 | 0 | result->op_type = IS_CV; |
2938 | 0 | result->u.op.var = lookup_cv(name); |
2939 | |
|
2940 | 0 | if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) { |
2941 | 0 | zend_string_release_ex(name, 0); |
2942 | 0 | } |
2943 | |
|
2944 | 0 | return SUCCESS; |
2945 | 0 | } |
2946 | | |
2947 | 0 | return FAILURE; |
2948 | 0 | } |
2949 | | /* }}} */ |
2950 | | |
2951 | | static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
2952 | 0 | { |
2953 | 0 | zend_ast *name_ast = ast->child[0]; |
2954 | 0 | znode name_node; |
2955 | 0 | zend_op *opline; |
2956 | |
|
2957 | 0 | zend_compile_expr(&name_node, name_ast); |
2958 | 0 | if (name_node.op_type == IS_CONST) { |
2959 | 0 | convert_to_string(&name_node.u.constant); |
2960 | 0 | } |
2961 | |
|
2962 | 0 | if (delayed) { |
2963 | 0 | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2964 | 0 | } else { |
2965 | 0 | opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2966 | 0 | } |
2967 | |
|
2968 | 0 | if (name_node.op_type == IS_CONST && |
2969 | 0 | zend_is_auto_global(Z_STR(name_node.u.constant))) { |
2970 | |
|
2971 | 0 | opline->extended_value = ZEND_FETCH_GLOBAL; |
2972 | 0 | } else { |
2973 | 0 | if (name_node.op_type == IS_CONST |
2974 | 0 | && type == BP_VAR_R |
2975 | 0 | && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) { |
2976 | 0 | zend_error(E_DEPRECATED, |
2977 | 0 | "The predefined locally scoped $http_response_header variable is deprecated," |
2978 | 0 | " call http_get_last_response_headers() instead"); |
2979 | 0 | } |
2980 | 0 | opline->extended_value = ZEND_FETCH_LOCAL; |
2981 | 0 | } |
2982 | |
|
2983 | 0 | zend_adjust_for_fetch_type(opline, result, type); |
2984 | 0 | return opline; |
2985 | 0 | } |
2986 | | /* }}} */ |
2987 | | |
2988 | | static bool is_this_fetch(const zend_ast *ast) /* {{{ */ |
2989 | 0 | { |
2990 | 0 | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
2991 | 0 | const zval *name = zend_ast_get_zval(ast->child[0]); |
2992 | 0 | return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS)); |
2993 | 0 | } |
2994 | | |
2995 | 0 | return false; |
2996 | 0 | } |
2997 | | /* }}} */ |
2998 | | |
2999 | | static bool is_globals_fetch(const zend_ast *ast) |
3000 | 0 | { |
3001 | 0 | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
3002 | 0 | const zval *name = zend_ast_get_zval(ast->child[0]); |
3003 | 0 | return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS"); |
3004 | 0 | } |
3005 | | |
3006 | 0 | return false; |
3007 | 0 | } |
3008 | | |
3009 | | static bool is_global_var_fetch(const zend_ast *ast) |
3010 | 0 | { |
3011 | 0 | return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]); |
3012 | 0 | } |
3013 | | |
3014 | | static bool this_guaranteed_exists(void) /* {{{ */ |
3015 | 0 | { |
3016 | 0 | const zend_oparray_context *ctx = &CG(context); |
3017 | 0 | while (ctx) { |
3018 | | /* Instance methods always have a $this. |
3019 | | * This also includes closures that have a scope and use $this. */ |
3020 | 0 | const zend_op_array *op_array = ctx->op_array; |
3021 | 0 | if (op_array->fn_flags & ZEND_ACC_STATIC) { |
3022 | 0 | return false; |
3023 | 0 | } else if (op_array->scope) { |
3024 | 0 | return true; |
3025 | 0 | } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
3026 | 0 | return false; |
3027 | 0 | } |
3028 | 0 | ctx = ctx->prev; |
3029 | 0 | } |
3030 | 0 | return false; |
3031 | 0 | } |
3032 | | /* }}} */ |
3033 | | |
3034 | | static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
3035 | 0 | { |
3036 | 0 | if (is_this_fetch(ast)) { |
3037 | 0 | zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL); |
3038 | 0 | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3039 | 0 | opline->result_type = IS_TMP_VAR; |
3040 | 0 | result->op_type = IS_TMP_VAR; |
3041 | 0 | } |
3042 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3043 | 0 | return opline; |
3044 | 0 | } else if (is_globals_fetch(ast)) { |
3045 | 0 | zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL); |
3046 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3047 | 0 | opline->result_type = IS_TMP_VAR; |
3048 | 0 | result->op_type = IS_TMP_VAR; |
3049 | 0 | } |
3050 | 0 | return opline; |
3051 | 0 | } else if (zend_try_compile_cv(result, ast, type) == FAILURE) { |
3052 | 0 | return zend_compile_simple_var_no_cv(result, ast, type, delayed); |
3053 | 0 | } |
3054 | 0 | return NULL; |
3055 | 0 | } |
3056 | | /* }}} */ |
3057 | | |
3058 | | static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */ |
3059 | 0 | { |
3060 | 0 | if (type != BP_VAR_R |
3061 | 0 | && type != BP_VAR_IS |
3062 | | /* Whether a FUNC_ARG is R may only be determined at runtime. */ |
3063 | 0 | && type != BP_VAR_FUNC_ARG |
3064 | 0 | && zend_is_call(ast)) { |
3065 | 0 | if (node->op_type == IS_VAR) { |
3066 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL); |
3067 | 0 | opline->result_type = IS_VAR; |
3068 | 0 | opline->result.var = opline->op1.var; |
3069 | 0 | } else { |
3070 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3071 | 0 | } |
3072 | 0 | } |
3073 | 0 | } |
3074 | | /* }}} */ |
3075 | | |
3076 | | static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3077 | 0 | { |
3078 | 0 | znode dummy_node; |
3079 | 0 | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast, |
3080 | 0 | zend_ast_create_znode(value_node)); |
3081 | 0 | zend_compile_expr(&dummy_node, assign_ast); |
3082 | 0 | zend_do_free(&dummy_node); |
3083 | 0 | } |
3084 | | /* }}} */ |
3085 | | |
3086 | | static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
3087 | 0 | { |
3088 | 0 | zend_ast *var_ast = ast->child[0]; |
3089 | 0 | zend_ast *dim_ast = ast->child[1]; |
3090 | 0 | zend_op *opline; |
3091 | |
|
3092 | 0 | znode var_node, dim_node; |
3093 | |
|
3094 | 0 | if (is_globals_fetch(var_ast)) { |
3095 | 0 | if (dim_ast == NULL) { |
3096 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS"); |
3097 | 0 | } |
3098 | | |
3099 | 0 | zend_compile_expr(&dim_node, dim_ast); |
3100 | 0 | if (dim_node.op_type == IS_CONST) { |
3101 | 0 | convert_to_string(&dim_node.u.constant); |
3102 | 0 | } |
3103 | |
|
3104 | 0 | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL); |
3105 | 0 | opline->extended_value = ZEND_FETCH_GLOBAL; |
3106 | 0 | zend_adjust_for_fetch_type(opline, result, type); |
3107 | 0 | return opline; |
3108 | 0 | } else { |
3109 | 0 | zend_short_circuiting_mark_inner(var_ast); |
3110 | 0 | opline = zend_delayed_compile_var(&var_node, var_ast, type, false); |
3111 | 0 | if (opline) { |
3112 | 0 | if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) { |
3113 | 0 | opline->extended_value |= ZEND_FETCH_DIM_WRITE; |
3114 | 0 | } else if (opline->opcode == ZEND_FETCH_DIM_W |
3115 | 0 | || opline->opcode == ZEND_FETCH_DIM_RW |
3116 | 0 | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3117 | 0 | || opline->opcode == ZEND_FETCH_DIM_UNSET) { |
3118 | 0 | opline->extended_value = ZEND_FETCH_DIM_DIM; |
3119 | 0 | } |
3120 | 0 | } |
3121 | 0 | } |
3122 | | |
3123 | 0 | zend_separate_if_call_and_write(&var_node, var_ast, type); |
3124 | |
|
3125 | 0 | if (dim_ast == NULL) { |
3126 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3127 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
3128 | 0 | } |
3129 | 0 | if (type == BP_VAR_UNSET) { |
3130 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
3131 | 0 | } |
3132 | 0 | dim_node.op_type = IS_UNUSED; |
3133 | 0 | } else { |
3134 | 0 | zend_compile_expr(&dim_node, dim_ast); |
3135 | 0 | } |
3136 | | |
3137 | 0 | opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node); |
3138 | 0 | zend_adjust_for_fetch_type(opline, result, type); |
3139 | 0 | if (by_ref) { |
3140 | 0 | opline->extended_value = ZEND_FETCH_DIM_REF; |
3141 | 0 | } |
3142 | |
|
3143 | 0 | if (dim_node.op_type == IS_CONST) { |
3144 | 0 | zend_handle_numeric_dim(opline, &dim_node); |
3145 | 0 | } |
3146 | 0 | return opline; |
3147 | 0 | } |
3148 | | |
3149 | | static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3150 | 0 | { |
3151 | 0 | uint32_t offset = zend_delayed_compile_begin(); |
3152 | 0 | zend_delayed_compile_dim(result, ast, type, by_ref); |
3153 | 0 | return zend_delayed_compile_end(offset); |
3154 | 0 | } |
3155 | | /* }}} */ |
3156 | | |
3157 | | static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3158 | 0 | { |
3159 | 0 | zend_ast *obj_ast = ast->child[0]; |
3160 | 0 | zend_ast *prop_ast = ast->child[1]; |
3161 | |
|
3162 | 0 | znode obj_node, prop_node; |
3163 | 0 | zend_op *opline; |
3164 | 0 | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP; |
3165 | |
|
3166 | 0 | if (is_this_fetch(obj_ast)) { |
3167 | 0 | if (this_guaranteed_exists()) { |
3168 | 0 | obj_node.op_type = IS_UNUSED; |
3169 | 0 | } else { |
3170 | 0 | opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
3171 | 0 | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3172 | 0 | opline->result_type = IS_TMP_VAR; |
3173 | 0 | obj_node.op_type = IS_TMP_VAR; |
3174 | 0 | } |
3175 | 0 | } |
3176 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3177 | | |
3178 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
3179 | | * check for a nullsafe access. */ |
3180 | 0 | } else { |
3181 | 0 | zend_short_circuiting_mark_inner(obj_ast); |
3182 | 0 | opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false); |
3183 | 0 | if (opline && (opline->opcode == ZEND_FETCH_DIM_W |
3184 | 0 | || opline->opcode == ZEND_FETCH_DIM_RW |
3185 | 0 | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3186 | 0 | || opline->opcode == ZEND_FETCH_DIM_UNSET)) { |
3187 | 0 | opline->extended_value = ZEND_FETCH_DIM_OBJ; |
3188 | 0 | } |
3189 | |
|
3190 | 0 | zend_separate_if_call_and_write(&obj_node, obj_ast, type); |
3191 | 0 | if (nullsafe) { |
3192 | 0 | if (obj_node.op_type == IS_TMP_VAR) { |
3193 | | /* Flush delayed oplines */ |
3194 | 0 | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
3195 | 0 | uint32_t var = obj_node.u.op.var; |
3196 | 0 | uint32_t count = zend_stack_count(&CG(delayed_oplines_stack)); |
3197 | 0 | uint32_t i = count; |
3198 | |
|
3199 | 0 | while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) { |
3200 | 0 | i--; |
3201 | 0 | if (oplines[i].op1_type == IS_TMP_VAR) { |
3202 | 0 | var = oplines[i].op1.var; |
3203 | 0 | } else { |
3204 | 0 | break; |
3205 | 0 | } |
3206 | 0 | } |
3207 | 0 | for (; i < count; ++i) { |
3208 | 0 | if (oplines[i].opcode != ZEND_NOP) { |
3209 | 0 | opline = get_next_op(); |
3210 | 0 | memcpy(opline, &oplines[i], sizeof(zend_op)); |
3211 | 0 | oplines[i].opcode = ZEND_NOP; |
3212 | 0 | oplines[i].extended_value = opline - CG(active_op_array)->opcodes; |
3213 | 0 | } |
3214 | 0 | } |
3215 | 0 | } |
3216 | 0 | zend_emit_jmp_null(&obj_node, type); |
3217 | 0 | } |
3218 | 0 | } |
3219 | |
|
3220 | 0 | zend_compile_expr(&prop_node, prop_ast); |
3221 | |
|
3222 | 0 | opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node); |
3223 | 0 | if (opline->op2_type == IS_CONST) { |
3224 | 0 | convert_to_string(CT_CONSTANT(opline->op2)); |
3225 | 0 | zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2))); |
3226 | 0 | opline->extended_value = zend_alloc_cache_slots(3); |
3227 | 0 | } |
3228 | |
|
3229 | 0 | zend_adjust_for_fetch_type(opline, result, type); |
3230 | |
|
3231 | 0 | return opline; |
3232 | 0 | } |
3233 | | /* }}} */ |
3234 | | |
3235 | | static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3236 | 0 | { |
3237 | 0 | uint32_t offset = zend_delayed_compile_begin(); |
3238 | 0 | zend_op *opline = zend_delayed_compile_prop(result, ast, type); |
3239 | 0 | if (by_ref) { /* shared with cache_slot */ |
3240 | 0 | opline->extended_value |= ZEND_FETCH_REF; |
3241 | 0 | } |
3242 | 0 | return zend_delayed_compile_end(offset); |
3243 | 0 | } |
3244 | | /* }}} */ |
3245 | | |
3246 | | static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */ |
3247 | 0 | { |
3248 | 0 | zend_ast *class_ast = ast->child[0]; |
3249 | 0 | zend_ast *prop_ast = ast->child[1]; |
3250 | |
|
3251 | 0 | znode class_node, prop_node; |
3252 | 0 | zend_op *opline; |
3253 | |
|
3254 | 0 | zend_short_circuiting_mark_inner(class_ast); |
3255 | 0 | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
3256 | |
|
3257 | 0 | zend_compile_expr(&prop_node, prop_ast); |
3258 | |
|
3259 | 0 | if (delayed) { |
3260 | 0 | opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3261 | 0 | } else { |
3262 | 0 | opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3263 | 0 | } |
3264 | 0 | if (opline->op1_type == IS_CONST) { |
3265 | 0 | convert_to_string(CT_CONSTANT(opline->op1)); |
3266 | 0 | opline->extended_value = zend_alloc_cache_slots(3); |
3267 | 0 | } |
3268 | 0 | if (class_node.op_type == IS_CONST) { |
3269 | 0 | opline->op2_type = IS_CONST; |
3270 | 0 | opline->op2.constant = zend_add_class_name_literal( |
3271 | 0 | Z_STR(class_node.u.constant)); |
3272 | 0 | if (opline->op1_type != IS_CONST) { |
3273 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
3274 | 0 | } |
3275 | 0 | } else { |
3276 | 0 | SET_NODE(opline->op2, &class_node); |
3277 | 0 | } |
3278 | |
|
3279 | 0 | if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */ |
3280 | 0 | opline->extended_value |= ZEND_FETCH_REF; |
3281 | 0 | } |
3282 | |
|
3283 | 0 | zend_adjust_for_fetch_type(opline, result, type); |
3284 | 0 | return opline; |
3285 | 0 | } |
3286 | | /* }}} */ |
3287 | | |
3288 | 0 | static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ { |
3289 | 0 | if (var_ast->kind == ZEND_AST_ARRAY) { |
3290 | 0 | if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) { |
3291 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead"); |
3292 | 0 | } |
3293 | 0 | if (array_style != var_ast->attr) { |
3294 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()"); |
3295 | 0 | } |
3296 | 0 | } else if (!zend_can_write_to_variable(var_ast)) { |
3297 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values"); |
3298 | 0 | } |
3299 | 0 | } |
3300 | | /* }}} */ |
3301 | | |
3302 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node); |
3303 | | |
3304 | | /* Propagate refs used on leaf elements to the surrounding list() structures. */ |
3305 | 0 | static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */ |
3306 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
3307 | 0 | bool has_refs = false; |
3308 | 0 | uint32_t i; |
3309 | |
|
3310 | 0 | for (i = 0; i < list->children; ++i) { |
3311 | 0 | zend_ast *elem_ast = list->child[i]; |
3312 | |
|
3313 | 0 | if (elem_ast) { |
3314 | 0 | zend_ast *var_ast = elem_ast->child[0]; |
3315 | 0 | if (var_ast->kind == ZEND_AST_ARRAY) { |
3316 | 0 | elem_ast->attr = zend_propagate_list_refs(var_ast); |
3317 | 0 | } |
3318 | 0 | has_refs |= elem_ast->attr; |
3319 | 0 | } |
3320 | 0 | } |
3321 | |
|
3322 | 0 | return has_refs; |
3323 | 0 | } |
3324 | | /* }}} */ |
3325 | | |
3326 | | static bool list_is_keyed(const zend_ast_list *list) |
3327 | 0 | { |
3328 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
3329 | 0 | const zend_ast *child = list->child[i]; |
3330 | 0 | if (child) { |
3331 | 0 | return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL; |
3332 | 0 | } |
3333 | 0 | } |
3334 | 0 | return false; |
3335 | 0 | } |
3336 | | |
3337 | | static void zend_compile_list_assign( |
3338 | | znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */ |
3339 | 0 | { |
3340 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
3341 | 0 | uint32_t i; |
3342 | 0 | bool has_elems = false; |
3343 | 0 | bool is_keyed = list_is_keyed(list); |
3344 | |
|
3345 | 0 | if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) { |
3346 | 0 | zval_make_interned_string(&expr_node->u.constant); |
3347 | 0 | } |
3348 | |
|
3349 | 0 | for (i = 0; i < list->children; ++i) { |
3350 | 0 | zend_ast *elem_ast = list->child[i]; |
3351 | 0 | zend_ast *var_ast, *key_ast; |
3352 | 0 | znode fetch_result, dim_node; |
3353 | 0 | zend_op *opline; |
3354 | |
|
3355 | 0 | if (elem_ast == NULL) { |
3356 | 0 | if (is_keyed) { |
3357 | 0 | zend_error(E_COMPILE_ERROR, |
3358 | 0 | "Cannot use empty array entries in keyed array assignment"); |
3359 | 0 | } else { |
3360 | 0 | continue; |
3361 | 0 | } |
3362 | 0 | } |
3363 | | |
3364 | 0 | if (elem_ast->kind == ZEND_AST_UNPACK) { |
3365 | 0 | zend_error(E_COMPILE_ERROR, |
3366 | 0 | "Spread operator is not supported in assignments"); |
3367 | 0 | } |
3368 | |
|
3369 | 0 | var_ast = elem_ast->child[0]; |
3370 | 0 | key_ast = elem_ast->child[1]; |
3371 | 0 | has_elems = true; |
3372 | |
|
3373 | 0 | if (is_keyed) { |
3374 | 0 | if (key_ast == NULL) { |
3375 | 0 | zend_error(E_COMPILE_ERROR, |
3376 | 0 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3377 | 0 | } |
3378 | |
|
3379 | 0 | zend_compile_expr(&dim_node, key_ast); |
3380 | 0 | } else { |
3381 | 0 | if (key_ast != NULL) { |
3382 | 0 | zend_error(E_COMPILE_ERROR, |
3383 | 0 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3384 | 0 | } |
3385 | |
|
3386 | 0 | dim_node.op_type = IS_CONST; |
3387 | 0 | ZVAL_LONG(&dim_node.u.constant, i); |
3388 | 0 | } |
3389 | |
|
3390 | 0 | if (expr_node->op_type == IS_CONST) { |
3391 | 0 | Z_TRY_ADDREF(expr_node->u.constant); |
3392 | 0 | } |
3393 | |
|
3394 | 0 | zend_verify_list_assign_target(var_ast, array_style); |
3395 | |
|
3396 | 0 | opline = zend_emit_op(&fetch_result, |
3397 | 0 | elem_ast->attr ? (expr_node->op_type == IS_CV ? ZEND_FETCH_DIM_W : ZEND_FETCH_LIST_W) : ZEND_FETCH_LIST_R, expr_node, &dim_node); |
3398 | 0 | if (opline->opcode == ZEND_FETCH_LIST_R) { |
3399 | 0 | opline->result_type = IS_TMP_VAR; |
3400 | 0 | fetch_result.op_type = IS_TMP_VAR; |
3401 | 0 | } |
3402 | |
|
3403 | 0 | if (dim_node.op_type == IS_CONST) { |
3404 | 0 | zend_handle_numeric_dim(opline, &dim_node); |
3405 | 0 | } |
3406 | |
|
3407 | 0 | if (elem_ast->attr) { |
3408 | 0 | zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL); |
3409 | 0 | } |
3410 | 0 | if (var_ast->kind == ZEND_AST_ARRAY) { |
3411 | 0 | zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type); |
3412 | 0 | } else if (elem_ast->attr) { |
3413 | 0 | zend_emit_assign_ref_znode(var_ast, &fetch_result); |
3414 | 0 | } else { |
3415 | 0 | zend_emit_assign_znode(var_ast, &fetch_result); |
3416 | 0 | } |
3417 | 0 | } |
3418 | |
|
3419 | 0 | if (has_elems == 0) { |
3420 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list"); |
3421 | 0 | } |
3422 | | |
3423 | 0 | if (result) { |
3424 | 0 | if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) { |
3425 | | /* Deref. */ |
3426 | 0 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL); |
3427 | 0 | } else { |
3428 | 0 | *result = *expr_node; |
3429 | 0 | } |
3430 | 0 | } else { |
3431 | 0 | zend_do_free(expr_node); |
3432 | 0 | } |
3433 | 0 | } |
3434 | | /* }}} */ |
3435 | | |
3436 | | static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */ |
3437 | 0 | { |
3438 | 0 | if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) { |
3439 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context"); |
3440 | 0 | } |
3441 | 0 | if ( |
3442 | 0 | ast->kind == ZEND_AST_METHOD_CALL |
3443 | 0 | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
3444 | 0 | || ast->kind == ZEND_AST_STATIC_CALL |
3445 | 0 | ) { |
3446 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context"); |
3447 | 0 | } |
3448 | 0 | if (zend_ast_is_short_circuited(ast)) { |
3449 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context"); |
3450 | 0 | } |
3451 | 0 | if (is_globals_fetch(ast)) { |
3452 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3453 | 0 | "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax"); |
3454 | 0 | } |
3455 | 0 | } |
3456 | | /* }}} */ |
3457 | | |
3458 | | /* Detects $a... = $a pattern */ |
3459 | | static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */ |
3460 | 0 | { |
3461 | 0 | if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3462 | 0 | return false; |
3463 | 0 | } |
3464 | | |
3465 | 0 | while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) { |
3466 | 0 | var_ast = var_ast->child[0]; |
3467 | 0 | } |
3468 | |
|
3469 | 0 | if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3470 | 0 | return false; |
3471 | 0 | } |
3472 | | |
3473 | 0 | { |
3474 | 0 | zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0])); |
3475 | 0 | zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0])); |
3476 | 0 | bool result = zend_string_equals(name1, name2); |
3477 | 0 | zend_string_release_ex(name1, 0); |
3478 | 0 | zend_string_release_ex(name2, 0); |
3479 | 0 | return result; |
3480 | 0 | } |
3481 | 0 | } |
3482 | | /* }}} */ |
3483 | | |
3484 | | static void zend_compile_expr_with_potential_assign_to_self( |
3485 | 0 | znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) { |
3486 | 0 | if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) { |
3487 | | /* $a[0] = $a should evaluate the right $a first */ |
3488 | 0 | znode cv_node; |
3489 | |
|
3490 | 0 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3491 | 0 | zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false); |
3492 | 0 | } else { |
3493 | 0 | zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3494 | 0 | } |
3495 | 0 | } else { |
3496 | 0 | zend_compile_expr(expr_node, expr_ast); |
3497 | 0 | } |
3498 | 0 | } |
3499 | | |
3500 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */ |
3501 | 0 | { |
3502 | 0 | zend_ast *var_ast = ast->child[0]; |
3503 | 0 | zend_ast *expr_ast = ast->child[1]; |
3504 | |
|
3505 | 0 | znode var_node, expr_node; |
3506 | 0 | zend_op *opline; |
3507 | 0 | uint32_t offset; |
3508 | 0 | if (is_this_fetch(var_ast)) { |
3509 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3510 | 0 | } |
3511 | | |
3512 | 0 | zend_ensure_writable_variable(var_ast); |
3513 | | |
3514 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3515 | 0 | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3516 | 0 | switch (kind) { |
3517 | 0 | case ZEND_AST_VAR: |
3518 | 0 | offset = zend_delayed_compile_begin(); |
3519 | 0 | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false); |
3520 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3521 | 0 | zend_delayed_compile_end(offset); |
3522 | 0 | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
3523 | 0 | zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node); |
3524 | 0 | return; |
3525 | 0 | case ZEND_AST_STATIC_PROP: |
3526 | 0 | offset = zend_delayed_compile_begin(); |
3527 | 0 | zend_delayed_compile_var(result, var_ast, BP_VAR_W, false); |
3528 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3529 | |
|
3530 | 0 | opline = zend_delayed_compile_end(offset); |
3531 | 0 | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
3532 | 0 | opline->result_type = IS_TMP_VAR; |
3533 | 0 | result->op_type = IS_TMP_VAR; |
3534 | |
|
3535 | 0 | zend_emit_op_data(&expr_node); |
3536 | 0 | return; |
3537 | 0 | case ZEND_AST_DIM: |
3538 | 0 | offset = zend_delayed_compile_begin(); |
3539 | 0 | zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false); |
3540 | 0 | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3541 | |
|
3542 | 0 | opline = zend_delayed_compile_end(offset); |
3543 | 0 | opline->opcode = ZEND_ASSIGN_DIM; |
3544 | 0 | opline->result_type = IS_TMP_VAR; |
3545 | 0 | result->op_type = IS_TMP_VAR; |
3546 | |
|
3547 | 0 | opline = zend_emit_op_data(&expr_node); |
3548 | 0 | return; |
3549 | 0 | case ZEND_AST_PROP: |
3550 | 0 | case ZEND_AST_NULLSAFE_PROP: |
3551 | 0 | offset = zend_delayed_compile_begin(); |
3552 | 0 | zend_delayed_compile_prop(result, var_ast, BP_VAR_W); |
3553 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3554 | |
|
3555 | 0 | opline = zend_delayed_compile_end(offset); |
3556 | 0 | opline->opcode = ZEND_ASSIGN_OBJ; |
3557 | 0 | opline->result_type = IS_TMP_VAR; |
3558 | 0 | result->op_type = IS_TMP_VAR; |
3559 | |
|
3560 | 0 | zend_emit_op_data(&expr_node); |
3561 | 0 | return; |
3562 | 0 | case ZEND_AST_ARRAY: |
3563 | 0 | if (zend_propagate_list_refs(var_ast)) { |
3564 | 0 | if (!zend_is_variable_or_call(expr_ast)) { |
3565 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3566 | 0 | "Cannot assign reference to non referenceable value"); |
3567 | 0 | } else { |
3568 | 0 | zend_assert_not_short_circuited(expr_ast); |
3569 | 0 | } |
3570 | | |
3571 | 0 | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
3572 | | /* MAKE_REF is usually not necessary for CVs. However, if there are |
3573 | | * self-assignments, this forces the RHS to evaluate first. */ |
3574 | 0 | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
3575 | 0 | } else { |
3576 | 0 | if (expr_ast->kind == ZEND_AST_VAR) { |
3577 | | /* list($a, $b) = $a should evaluate the right $a first */ |
3578 | 0 | znode cv_node; |
3579 | |
|
3580 | 0 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3581 | 0 | zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false); |
3582 | 0 | } else { |
3583 | 0 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3584 | 0 | } |
3585 | 0 | } else { |
3586 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3587 | 0 | } |
3588 | 0 | } |
3589 | | |
3590 | 0 | zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type); |
3591 | 0 | if (stmt) { |
3592 | 0 | result->op_type = IS_UNUSED; |
3593 | 0 | } |
3594 | 0 | return; |
3595 | 0 | default: ZEND_UNREACHABLE(); |
3596 | 0 | } |
3597 | 0 | } |
3598 | | /* }}} */ |
3599 | | |
3600 | | static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3601 | 0 | { |
3602 | 0 | zend_ast *target_ast = ast->child[0]; |
3603 | 0 | zend_ast *source_ast = ast->child[1]; |
3604 | |
|
3605 | 0 | znode target_node, source_node; |
3606 | 0 | zend_op *opline; |
3607 | 0 | uint32_t offset, flags; |
3608 | |
|
3609 | 0 | if (is_this_fetch(target_ast)) { |
3610 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3611 | 0 | } |
3612 | 0 | zend_ensure_writable_variable(target_ast); |
3613 | 0 | zend_assert_not_short_circuited(source_ast); |
3614 | 0 | if (is_globals_fetch(source_ast)) { |
3615 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS"); |
3616 | 0 | } |
3617 | | |
3618 | 0 | offset = zend_delayed_compile_begin(); |
3619 | 0 | zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true); |
3620 | 0 | zend_compile_var(&source_node, source_ast, BP_VAR_W, true); |
3621 | |
|
3622 | 0 | if ((target_ast->kind != ZEND_AST_VAR |
3623 | 0 | || target_ast->child[0]->kind != ZEND_AST_ZVAL) |
3624 | 0 | && source_ast->kind != ZEND_AST_ZNODE |
3625 | 0 | && source_node.op_type != IS_CV) { |
3626 | | /* Both LHS and RHS expressions may modify the same data structure, |
3627 | | * and the modification during RHS evaluation may dangle the pointer |
3628 | | * to the result of the LHS evaluation. |
3629 | | * Use MAKE_REF instruction to replace direct pointer with REFERENCE. |
3630 | | * See: Bug #71539 |
3631 | | */ |
3632 | 0 | zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL); |
3633 | 0 | } |
3634 | |
|
3635 | 0 | opline = zend_delayed_compile_end(offset); |
3636 | |
|
3637 | 0 | if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) { |
3638 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3639 | 0 | } |
3640 | | |
3641 | 0 | flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0; |
3642 | |
|
3643 | 0 | if (opline && opline->opcode == ZEND_FETCH_OBJ_W) { |
3644 | 0 | opline->opcode = ZEND_ASSIGN_OBJ_REF; |
3645 | 0 | opline->extended_value &= ~ZEND_FETCH_REF; |
3646 | 0 | opline->extended_value |= flags; |
3647 | 0 | if (result) { |
3648 | 0 | *result = target_node; |
3649 | 0 | } else { |
3650 | 0 | SET_UNUSED(opline->result); |
3651 | 0 | } |
3652 | 0 | zend_emit_op_data(&source_node); |
3653 | 0 | } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) { |
3654 | 0 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF; |
3655 | 0 | opline->extended_value &= ~ZEND_FETCH_REF; |
3656 | 0 | opline->extended_value |= flags; |
3657 | 0 | if (result) { |
3658 | 0 | *result = target_node; |
3659 | 0 | } else { |
3660 | 0 | SET_UNUSED(opline->result); |
3661 | 0 | } |
3662 | 0 | zend_emit_op_data(&source_node); |
3663 | 0 | } else { |
3664 | 0 | opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node); |
3665 | 0 | opline->extended_value = flags; |
3666 | 0 | } |
3667 | |
|
3668 | 0 | if (result && (type == BP_VAR_R || type == BP_VAR_IS)) { |
3669 | | /* Deref. */ |
3670 | 0 | znode tmp_result = *result; |
3671 | 0 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL); |
3672 | 0 | } |
3673 | 0 | } |
3674 | | /* }}} */ |
3675 | | |
3676 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3677 | 0 | { |
3678 | 0 | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast, |
3679 | 0 | zend_ast_create_znode(value_node)); |
3680 | 0 | zend_compile_stmt(assign_ast); |
3681 | 0 | } |
3682 | | /* }}} */ |
3683 | | |
3684 | | static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */ |
3685 | 0 | { |
3686 | 0 | zend_ast *var_ast = ast->child[0]; |
3687 | 0 | zend_ast *expr_ast = ast->child[1]; |
3688 | 0 | uint32_t opcode = ast->attr; |
3689 | |
|
3690 | 0 | znode var_node, expr_node; |
3691 | 0 | zend_op *opline; |
3692 | 0 | uint32_t offset, cache_slot; |
3693 | |
|
3694 | 0 | zend_ensure_writable_variable(var_ast); |
3695 | | |
3696 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3697 | 0 | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3698 | 0 | switch (kind) { |
3699 | 0 | case ZEND_AST_VAR: |
3700 | 0 | offset = zend_delayed_compile_begin(); |
3701 | 0 | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
3702 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3703 | 0 | zend_delayed_compile_end(offset); |
3704 | 0 | opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node); |
3705 | 0 | opline->extended_value = opcode; |
3706 | 0 | return; |
3707 | 0 | case ZEND_AST_STATIC_PROP: |
3708 | 0 | offset = zend_delayed_compile_begin(); |
3709 | 0 | zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false); |
3710 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3711 | |
|
3712 | 0 | opline = zend_delayed_compile_end(offset); |
3713 | 0 | cache_slot = opline->extended_value; |
3714 | 0 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP; |
3715 | 0 | opline->extended_value = opcode; |
3716 | 0 | opline->result_type = IS_TMP_VAR; |
3717 | 0 | result->op_type = IS_TMP_VAR; |
3718 | |
|
3719 | 0 | opline = zend_emit_op_data(&expr_node); |
3720 | 0 | opline->extended_value = cache_slot; |
3721 | 0 | return; |
3722 | 0 | case ZEND_AST_DIM: |
3723 | 0 | offset = zend_delayed_compile_begin(); |
3724 | 0 | zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false); |
3725 | 0 | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3726 | |
|
3727 | 0 | opline = zend_delayed_compile_end(offset); |
3728 | 0 | opline->opcode = ZEND_ASSIGN_DIM_OP; |
3729 | 0 | opline->extended_value = opcode; |
3730 | 0 | opline->result_type = IS_TMP_VAR; |
3731 | 0 | result->op_type = IS_TMP_VAR; |
3732 | |
|
3733 | 0 | zend_emit_op_data(&expr_node); |
3734 | 0 | return; |
3735 | 0 | case ZEND_AST_PROP: |
3736 | 0 | case ZEND_AST_NULLSAFE_PROP: |
3737 | 0 | offset = zend_delayed_compile_begin(); |
3738 | 0 | zend_delayed_compile_prop(result, var_ast, BP_VAR_RW); |
3739 | 0 | zend_compile_expr(&expr_node, expr_ast); |
3740 | |
|
3741 | 0 | opline = zend_delayed_compile_end(offset); |
3742 | 0 | cache_slot = opline->extended_value; |
3743 | 0 | opline->opcode = ZEND_ASSIGN_OBJ_OP; |
3744 | 0 | opline->extended_value = opcode; |
3745 | 0 | opline->result_type = IS_TMP_VAR; |
3746 | 0 | result->op_type = IS_TMP_VAR; |
3747 | |
|
3748 | 0 | opline = zend_emit_op_data(&expr_node); |
3749 | 0 | opline->extended_value = cache_slot; |
3750 | 0 | return; |
3751 | 0 | default: ZEND_UNREACHABLE(); |
3752 | 0 | } |
3753 | 0 | } |
3754 | | /* }}} */ |
3755 | | |
3756 | 0 | static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) { |
3757 | | // TODO: Caching? |
3758 | 0 | for (uint32_t i = 0; i < fn->common.num_args; i++) { |
3759 | 0 | zend_arg_info *arg_info = &fn->op_array.arg_info[i]; |
3760 | 0 | if (zend_string_equals(arg_info->name, arg_name)) { |
3761 | 0 | return i + 1; |
3762 | 0 | } |
3763 | 0 | } |
3764 | | |
3765 | | /* Either an invalid argument name, or collected into a variadic argument. */ |
3766 | 0 | return (uint32_t) -1; |
3767 | 0 | } |
3768 | | |
3769 | | static uint32_t zend_compile_args( |
3770 | | zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) /* {{{ */ |
3771 | 0 | { |
3772 | 0 | const zend_ast_list *args = zend_ast_get_list(ast); |
3773 | 0 | uint32_t i; |
3774 | 0 | bool uses_arg_unpack = false; |
3775 | 0 | uint32_t arg_count = 0; /* number of arguments not including unpacks */ |
3776 | | |
3777 | | /* Whether named arguments are used syntactically, to enforce language level limitations. |
3778 | | * May not actually use named argument passing. */ |
3779 | 0 | bool uses_named_args = false; |
3780 | | /* Whether there may be any undef arguments due to the use of named arguments. */ |
3781 | 0 | bool may_have_undef = false; |
3782 | | /* Whether there may be any extra named arguments collected into a variadic. */ |
3783 | 0 | *may_have_extra_named_args = false; |
3784 | |
|
3785 | 0 | for (i = 0; i < args->children; ++i) { |
3786 | 0 | zend_ast *arg = args->child[i]; |
3787 | 0 | zend_string *arg_name = NULL; |
3788 | 0 | uint32_t arg_num = i + 1; |
3789 | |
|
3790 | 0 | znode arg_node; |
3791 | 0 | zend_op *opline; |
3792 | 0 | uint8_t opcode; |
3793 | |
|
3794 | 0 | if (arg->kind == ZEND_AST_UNPACK) { |
3795 | 0 | if (uses_named_args) { |
3796 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3797 | 0 | "Cannot use argument unpacking after named arguments"); |
3798 | 0 | } |
3799 | | |
3800 | | /* Unpack may contain named arguments. */ |
3801 | 0 | may_have_undef = true; |
3802 | 0 | if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3803 | 0 | *may_have_extra_named_args = true; |
3804 | 0 | } |
3805 | |
|
3806 | 0 | uses_arg_unpack = true; |
3807 | 0 | fbc = NULL; |
3808 | |
|
3809 | 0 | zend_compile_expr(&arg_node, arg->child[0]); |
3810 | 0 | opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL); |
3811 | 0 | opline->op2.num = arg_count; |
3812 | 0 | opline->result.var = EX_NUM_TO_VAR(arg_count - 1); |
3813 | |
|
3814 | 0 | continue; |
3815 | 0 | } |
3816 | | |
3817 | 0 | if (arg->kind == ZEND_AST_NAMED_ARG) { |
3818 | 0 | uses_named_args = true; |
3819 | 0 | arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0])); |
3820 | 0 | arg = arg->child[1]; |
3821 | |
|
3822 | 0 | if (fbc && !uses_arg_unpack) { |
3823 | 0 | arg_num = zend_get_arg_num(fbc, arg_name); |
3824 | 0 | if (arg_num == arg_count + 1 && !may_have_undef) { |
3825 | | /* Using named arguments, but passing in order. */ |
3826 | 0 | arg_name = NULL; |
3827 | 0 | arg_count++; |
3828 | 0 | } else { |
3829 | | // TODO: We could track which arguments were passed, even if out of order. |
3830 | 0 | may_have_undef = true; |
3831 | 0 | if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3832 | 0 | *may_have_extra_named_args = true; |
3833 | 0 | } |
3834 | 0 | } |
3835 | 0 | } else { |
3836 | 0 | arg_num = (uint32_t) -1; |
3837 | 0 | may_have_undef = true; |
3838 | 0 | *may_have_extra_named_args = true; |
3839 | 0 | } |
3840 | 0 | } else { |
3841 | 0 | if (uses_arg_unpack) { |
3842 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3843 | 0 | "Cannot use positional argument after argument unpacking"); |
3844 | 0 | } |
3845 | | |
3846 | 0 | if (uses_named_args) { |
3847 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3848 | 0 | "Cannot use positional argument after named argument"); |
3849 | 0 | } |
3850 | | |
3851 | 0 | arg_count++; |
3852 | 0 | } |
3853 | | |
3854 | | /* Treat passing of $GLOBALS the same as passing a call. |
3855 | | * This will error at runtime if the argument is by-ref. */ |
3856 | 0 | if (zend_is_call(arg) || is_globals_fetch(arg)) { |
3857 | 0 | uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) |
3858 | 0 | ? BP_VAR_R : BP_VAR_FUNC_ARG; |
3859 | 0 | zend_compile_var(&arg_node, arg, type, /* by_ref */ false); |
3860 | 0 | if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) { |
3861 | | /* Function call was converted into builtin instruction */ |
3862 | 0 | if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3863 | 0 | opcode = ZEND_SEND_VAL_EX; |
3864 | 0 | } else { |
3865 | 0 | opcode = ZEND_SEND_VAL; |
3866 | 0 | } |
3867 | 0 | } else { |
3868 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
3869 | 0 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3870 | 0 | opcode = ZEND_SEND_VAR_NO_REF; |
3871 | 0 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
3872 | | /* For IS_VAR operands, SEND_VAL will pass through the operand without |
3873 | | * dereferencing, so it will use a by-ref pass if the call returned by-ref |
3874 | | * and a by-value pass if it returned by-value. */ |
3875 | 0 | opcode = ZEND_SEND_VAL; |
3876 | 0 | } else { |
3877 | 0 | opcode = ZEND_SEND_VAR; |
3878 | 0 | } |
3879 | 0 | } else { |
3880 | 0 | opcode = ZEND_SEND_VAR_NO_REF_EX; |
3881 | 0 | } |
3882 | 0 | } |
3883 | 0 | } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) { |
3884 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
3885 | 0 | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
3886 | 0 | zend_compile_var(&arg_node, arg, BP_VAR_W, true); |
3887 | 0 | opcode = ZEND_SEND_REF; |
3888 | 0 | } else { |
3889 | 0 | zend_compile_var(&arg_node, arg, BP_VAR_R, false); |
3890 | 0 | opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR; |
3891 | 0 | } |
3892 | 0 | } else { |
3893 | 0 | do { |
3894 | 0 | if (arg->kind == ZEND_AST_VAR) { |
3895 | 0 | CG(zend_lineno) = zend_ast_get_lineno(ast); |
3896 | 0 | if (is_this_fetch(arg)) { |
3897 | 0 | zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL); |
3898 | 0 | opcode = ZEND_SEND_VAR_EX; |
3899 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3900 | 0 | break; |
3901 | 0 | } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) { |
3902 | 0 | opcode = ZEND_SEND_VAR_EX; |
3903 | 0 | break; |
3904 | 0 | } |
3905 | 0 | } |
3906 | 0 | opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL); |
3907 | 0 | if (arg_name) { |
3908 | 0 | opline->op2_type = IS_CONST; |
3909 | 0 | zend_string_addref(arg_name); |
3910 | 0 | opline->op2.constant = zend_add_literal_string(&arg_name); |
3911 | 0 | opline->result.num = zend_alloc_cache_slots(2); |
3912 | 0 | } else { |
3913 | 0 | opline->op2.num = arg_num; |
3914 | 0 | } |
3915 | 0 | zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true); |
3916 | 0 | opcode = ZEND_SEND_FUNC_ARG; |
3917 | 0 | } while (0); |
3918 | 0 | } |
3919 | 0 | } else { |
3920 | 0 | zend_compile_expr(&arg_node, arg); |
3921 | 0 | if (arg_node.op_type == IS_VAR) { |
3922 | | /* pass ++$a or something similar */ |
3923 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
3924 | 0 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3925 | 0 | opcode = ZEND_SEND_VAR_NO_REF; |
3926 | 0 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
3927 | 0 | opcode = ZEND_SEND_VAL; |
3928 | 0 | } else { |
3929 | 0 | opcode = ZEND_SEND_VAR; |
3930 | 0 | } |
3931 | 0 | } else { |
3932 | 0 | opcode = ZEND_SEND_VAR_NO_REF_EX; |
3933 | 0 | } |
3934 | 0 | } else if (arg_node.op_type == IS_CV) { |
3935 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
3936 | 0 | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
3937 | 0 | opcode = ZEND_SEND_REF; |
3938 | 0 | } else { |
3939 | 0 | opcode = ZEND_SEND_VAR; |
3940 | 0 | } |
3941 | 0 | } else { |
3942 | 0 | opcode = ZEND_SEND_VAR_EX; |
3943 | 0 | } |
3944 | 0 | } else { |
3945 | | /* Delay "Only variables can be passed by reference" error to execution */ |
3946 | 0 | if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3947 | 0 | opcode = ZEND_SEND_VAL; |
3948 | 0 | } else { |
3949 | 0 | opcode = ZEND_SEND_VAL_EX; |
3950 | 0 | } |
3951 | 0 | } |
3952 | 0 | } |
3953 | |
|
3954 | 0 | opline = zend_emit_op(NULL, opcode, &arg_node, NULL); |
3955 | 0 | if (arg_name) { |
3956 | 0 | opline->op2_type = IS_CONST; |
3957 | 0 | zend_string_addref(arg_name); |
3958 | 0 | opline->op2.constant = zend_add_literal_string(&arg_name); |
3959 | 0 | opline->result.num = zend_alloc_cache_slots(2); |
3960 | 0 | } else { |
3961 | 0 | opline->op2.opline_num = arg_num; |
3962 | 0 | opline->result.var = EX_NUM_TO_VAR(arg_num - 1); |
3963 | 0 | } |
3964 | 0 | } |
3965 | | |
3966 | 0 | if (may_have_undef) { |
3967 | 0 | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
3968 | 0 | } |
3969 | |
|
3970 | 0 | return arg_count; |
3971 | 0 | } |
3972 | | /* }}} */ |
3973 | | |
3974 | | ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */ |
3975 | 0 | { |
3976 | 0 | uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD; |
3977 | |
|
3978 | 0 | if (fbc && init_op->opcode != ZEND_NEW) { |
3979 | 0 | ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)); |
3980 | 0 | if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) { |
3981 | 0 | if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) { |
3982 | 0 | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
3983 | 0 | return ZEND_DO_ICALL; |
3984 | 0 | } else { |
3985 | 0 | return ZEND_DO_FCALL_BY_NAME; |
3986 | 0 | } |
3987 | 0 | } |
3988 | 0 | } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){ |
3989 | 0 | if (zend_execute_ex == execute_ex) { |
3990 | 0 | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
3991 | 0 | return ZEND_DO_UCALL; |
3992 | 0 | } else { |
3993 | 0 | return ZEND_DO_FCALL_BY_NAME; |
3994 | 0 | } |
3995 | 0 | } |
3996 | 0 | } |
3997 | 0 | } else if (zend_execute_ex == execute_ex && |
3998 | 0 | !zend_execute_internal && |
3999 | 0 | (init_op->opcode == ZEND_INIT_FCALL_BY_NAME || |
4000 | 0 | init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) { |
4001 | 0 | return ZEND_DO_FCALL_BY_NAME; |
4002 | 0 | } |
4003 | 0 | return ZEND_DO_FCALL; |
4004 | 0 | } |
4005 | | /* }}} */ |
4006 | | |
4007 | | static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4008 | 0 | { |
4009 | 0 | zend_op *opline; |
4010 | 0 | uint32_t opnum_init = get_next_op_number() - 1; |
4011 | |
|
4012 | 0 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
4013 | 0 | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4014 | 0 | opline->extended_value = 0; |
4015 | | /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */ |
4016 | 0 | uint8_t opcode = opline->opcode; |
4017 | |
|
4018 | 0 | if (opcode == ZEND_NEW) { |
4019 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
4020 | 0 | } |
4021 | | |
4022 | 0 | zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
4023 | 0 | if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) { |
4024 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create a Closure for call expression with more than one argument, or non-variadic placeholders"); |
4025 | 0 | } |
4026 | | |
4027 | 0 | if (opcode == ZEND_INIT_FCALL) { |
4028 | 0 | opline->op1.num = zend_vm_calc_used_stack(0, fbc); |
4029 | 0 | } |
4030 | |
|
4031 | 0 | zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL); |
4032 | 0 | if (opcode == ZEND_INIT_FCALL |
4033 | 0 | || opcode == ZEND_INIT_FCALL_BY_NAME |
4034 | 0 | || opcode == ZEND_INIT_NS_FCALL_BY_NAME) { |
4035 | 0 | callable_convert_op->extended_value = zend_alloc_cache_slot(); |
4036 | 0 | } else { |
4037 | 0 | callable_convert_op->extended_value = (uint32_t)-1; |
4038 | 0 | } |
4039 | 0 | return true; |
4040 | 0 | } |
4041 | | |
4042 | 0 | bool may_have_extra_named_args; |
4043 | 0 | uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args); |
4044 | |
|
4045 | 0 | zend_do_extended_fcall_begin(); |
4046 | |
|
4047 | 0 | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4048 | 0 | opline->extended_value = arg_count; |
4049 | 0 | uint8_t init_opcode = opline->opcode; |
4050 | |
|
4051 | 0 | if (init_opcode == ZEND_INIT_FCALL) { |
4052 | 0 | opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc); |
4053 | 0 | } |
4054 | |
|
4055 | 0 | uint8_t call_op = zend_get_call_op( |
4056 | 0 | opline, |
4057 | 0 | fbc, |
4058 | | /* result_used: At this point we do not yet reliably |
4059 | | * know if the result is used. Deoptimize #[\NoDiscard] |
4060 | | * calls to be sure. The optimizer will fix this up. |
4061 | | */ |
4062 | 0 | false |
4063 | 0 | ); |
4064 | 0 | opline = zend_emit_op(result, call_op, NULL, NULL); |
4065 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4066 | 0 | if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) { |
4067 | 0 | opline->result_type = IS_TMP_VAR; |
4068 | 0 | result->op_type = IS_TMP_VAR; |
4069 | 0 | } |
4070 | 0 | } |
4071 | 0 | if (may_have_extra_named_args) { |
4072 | 0 | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4073 | 0 | } |
4074 | 0 | opline->lineno = lineno; |
4075 | 0 | zend_do_extended_fcall_end(); |
4076 | 0 | return false; |
4077 | 0 | } |
4078 | | /* }}} */ |
4079 | | |
4080 | | static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */ |
4081 | 0 | { |
4082 | 0 | zend_string *orig_name = zend_ast_get_str(name_ast); |
4083 | 0 | bool is_fully_qualified; |
4084 | |
|
4085 | 0 | name_node->op_type = IS_CONST; |
4086 | 0 | ZVAL_STR(&name_node->u.constant, zend_resolve_function_name( |
4087 | 0 | orig_name, name_ast->attr, &is_fully_qualified)); |
4088 | |
|
4089 | 0 | return !is_fully_qualified && FC(current_namespace); |
4090 | 0 | } |
4091 | | /* }}} */ |
4092 | | |
4093 | | static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
4094 | 0 | { |
4095 | 0 | if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) { |
4096 | 0 | const char *colon; |
4097 | 0 | zend_string *str = Z_STR(name_node->u.constant); |
4098 | 0 | if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') { |
4099 | 0 | zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0); |
4100 | 0 | zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0); |
4101 | 0 | zend_op *opline = get_next_op(); |
4102 | |
|
4103 | 0 | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
4104 | 0 | opline->op1_type = IS_CONST; |
4105 | 0 | opline->op1.constant = zend_add_class_name_literal(class); |
4106 | 0 | opline->op2_type = IS_CONST; |
4107 | 0 | opline->op2.constant = zend_add_func_name_literal(method); |
4108 | | /* 2 slots, for class and method */ |
4109 | 0 | opline->result.num = zend_alloc_cache_slots(2); |
4110 | 0 | zval_ptr_dtor(&name_node->u.constant); |
4111 | 0 | } else { |
4112 | 0 | zend_op *opline = get_next_op(); |
4113 | |
|
4114 | 0 | opline->opcode = ZEND_INIT_FCALL_BY_NAME; |
4115 | 0 | opline->op2_type = IS_CONST; |
4116 | 0 | opline->op2.constant = zend_add_func_name_literal(str); |
4117 | 0 | opline->result.num = zend_alloc_cache_slot(); |
4118 | 0 | } |
4119 | 0 | } else { |
4120 | 0 | zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node); |
4121 | 0 | } |
4122 | |
|
4123 | 0 | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
4124 | 0 | } |
4125 | | /* }}} */ |
4126 | | |
4127 | | static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */ |
4128 | 0 | { |
4129 | 0 | uint32_t i; |
4130 | 0 | for (i = 0; i < args->children; ++i) { |
4131 | 0 | const zend_ast *arg = args->child[i]; |
4132 | 0 | if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) { |
4133 | 0 | return true; |
4134 | 0 | } |
4135 | 0 | } |
4136 | 0 | return false; |
4137 | 0 | } |
4138 | | /* }}} */ |
4139 | | |
4140 | | static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */ |
4141 | 0 | { |
4142 | 0 | znode arg_node; |
4143 | |
|
4144 | 0 | if (args->children != 1) { |
4145 | 0 | return FAILURE; |
4146 | 0 | } |
4147 | | |
4148 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4149 | 0 | if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) { |
4150 | 0 | result->op_type = IS_CONST; |
4151 | 0 | ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant)); |
4152 | 0 | zval_ptr_dtor_str(&arg_node.u.constant); |
4153 | 0 | } else { |
4154 | 0 | zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL); |
4155 | 0 | } |
4156 | 0 | return SUCCESS; |
4157 | 0 | } |
4158 | | /* }}} */ |
4159 | | |
4160 | | static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4161 | 0 | { |
4162 | 0 | znode arg_node; |
4163 | 0 | zend_op *opline; |
4164 | |
|
4165 | 0 | if (args->children != 1) { |
4166 | 0 | return FAILURE; |
4167 | 0 | } |
4168 | | |
4169 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4170 | 0 | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4171 | 0 | if (type != _IS_BOOL) { |
4172 | 0 | opline->extended_value = (1 << type); |
4173 | 0 | } else { |
4174 | 0 | opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE); |
4175 | 0 | } |
4176 | 0 | return SUCCESS; |
4177 | 0 | } |
4178 | | /* }}} */ |
4179 | | |
4180 | | static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */ |
4181 | 0 | { |
4182 | 0 | znode arg_node; |
4183 | 0 | zend_op *opline; |
4184 | |
|
4185 | 0 | if (args->children != 1) { |
4186 | 0 | return FAILURE; |
4187 | 0 | } |
4188 | | |
4189 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4190 | 0 | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4191 | 0 | opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING); |
4192 | 0 | return SUCCESS; |
4193 | 0 | } |
4194 | | |
4195 | | static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4196 | 0 | { |
4197 | 0 | znode arg_node; |
4198 | 0 | zend_op *opline; |
4199 | |
|
4200 | 0 | if (args->children != 1) { |
4201 | 0 | return FAILURE; |
4202 | 0 | } |
4203 | | |
4204 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4205 | 0 | if (type == _IS_BOOL) { |
4206 | 0 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL); |
4207 | 0 | } else { |
4208 | 0 | opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL); |
4209 | 0 | opline->extended_value = type; |
4210 | 0 | } |
4211 | 0 | return SUCCESS; |
4212 | 0 | } |
4213 | | /* }}} */ |
4214 | | |
4215 | | static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */ |
4216 | 0 | { |
4217 | 0 | zend_string *name; |
4218 | 0 | zend_op *opline; |
4219 | |
|
4220 | 0 | if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) { |
4221 | 0 | return FAILURE; |
4222 | 0 | } |
4223 | | |
4224 | 0 | name = zval_get_string(zend_ast_get_zval(args->child[0])); |
4225 | 0 | if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) { |
4226 | 0 | zend_string_release_ex(name, 0); |
4227 | 0 | return FAILURE; |
4228 | 0 | } |
4229 | | |
4230 | 0 | if (zend_try_ct_eval_const(&result->u.constant, name, false)) { |
4231 | 0 | zend_string_release_ex(name, 0); |
4232 | 0 | zval_ptr_dtor(&result->u.constant); |
4233 | 0 | ZVAL_TRUE(&result->u.constant); |
4234 | 0 | result->op_type = IS_CONST; |
4235 | 0 | return SUCCESS; |
4236 | 0 | } |
4237 | | |
4238 | 0 | opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL); |
4239 | 0 | opline->op1_type = IS_CONST; |
4240 | 0 | LITERAL_STR(opline->op1, name); |
4241 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
4242 | |
|
4243 | 0 | return SUCCESS; |
4244 | 0 | } |
4245 | | /* }}} */ |
4246 | | |
4247 | | static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */ |
4248 | 0 | { |
4249 | 0 | zval *zint; |
4250 | 0 | if ( |
4251 | 0 | args->children == 1 |
4252 | 0 | && args->child[0]->kind == ZEND_AST_ZVAL |
4253 | 0 | && (zint = zend_ast_get_zval(args->child[0])) |
4254 | 0 | && Z_TYPE_P(zint) == IS_LONG |
4255 | 0 | && Z_LVAL_P(zint) >= 0 |
4256 | 0 | && Z_LVAL_P(zint) <= 255 |
4257 | 0 | ) { |
4258 | 0 | result->op_type = IS_CONST; |
4259 | 0 | ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint)); |
4260 | 0 | return SUCCESS; |
4261 | 0 | } else { |
4262 | 0 | return FAILURE; |
4263 | 0 | } |
4264 | 0 | } |
4265 | | /* }}} */ |
4266 | | |
4267 | | static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */ |
4268 | 0 | { |
4269 | 0 | zval *str; |
4270 | 0 | if ( |
4271 | 0 | args->children == 1 |
4272 | 0 | && args->child[0]->kind == ZEND_AST_ZVAL |
4273 | 0 | && (str = zend_ast_get_zval(args->child[0])) |
4274 | 0 | && Z_TYPE_P(str) == IS_STRING |
4275 | 0 | && Z_STRLEN_P(str) == 1 |
4276 | 0 | ) { |
4277 | 0 | result->op_type = IS_CONST; |
4278 | 0 | ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]); |
4279 | 0 | return SUCCESS; |
4280 | 0 | } else { |
4281 | 0 | return FAILURE; |
4282 | 0 | } |
4283 | 0 | } |
4284 | | /* }}} */ |
4285 | | |
4286 | | /* We can only calculate the stack size for functions that have been fully compiled, otherwise |
4287 | | * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for |
4288 | | * directly or indirectly recursive function calls. */ |
4289 | 0 | static bool fbc_is_finalized(const zend_function *fbc) { |
4290 | 0 | return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO); |
4291 | 0 | } |
4292 | | |
4293 | | static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename) |
4294 | 0 | { |
4295 | 0 | if (ce->type == ZEND_INTERNAL_CLASS) { |
4296 | 0 | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES; |
4297 | 0 | } else { |
4298 | 0 | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4299 | 0 | && ce->info.user.filename != filename; |
4300 | 0 | } |
4301 | 0 | } |
4302 | | |
4303 | | static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename) |
4304 | 0 | { |
4305 | 0 | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
4306 | 0 | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS; |
4307 | 0 | } else { |
4308 | 0 | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS) |
4309 | 0 | || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4310 | 0 | && fbc->op_array.filename != filename); |
4311 | 0 | } |
4312 | 0 | } |
4313 | | |
4314 | | static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */ |
4315 | 0 | { |
4316 | 0 | zend_string *name, *lcname; |
4317 | 0 | zend_function *fbc; |
4318 | 0 | zend_op *opline; |
4319 | |
|
4320 | 0 | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
4321 | 0 | return FAILURE; |
4322 | 0 | } |
4323 | | |
4324 | 0 | name = zend_ast_get_str(name_ast); |
4325 | 0 | lcname = zend_string_tolower(name); |
4326 | |
|
4327 | 0 | fbc = zend_hash_find_ptr(CG(function_table), lcname); |
4328 | 0 | if (!fbc |
4329 | 0 | || !fbc_is_finalized(fbc) |
4330 | 0 | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
4331 | 0 | zend_string_release_ex(lcname, 0); |
4332 | 0 | return FAILURE; |
4333 | 0 | } |
4334 | | |
4335 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL); |
4336 | 0 | opline->extended_value = num_args; |
4337 | 0 | opline->op1.num = zend_vm_calc_used_stack(num_args, fbc); |
4338 | 0 | opline->op2_type = IS_CONST; |
4339 | 0 | LITERAL_STR(opline->op2, lcname); |
4340 | 0 | opline->result.num = zend_alloc_cache_slot(); |
4341 | |
|
4342 | 0 | return SUCCESS; |
4343 | 0 | } |
4344 | | /* }}} */ |
4345 | | |
4346 | | static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */ |
4347 | 0 | { |
4348 | 0 | zend_op *opline; |
4349 | 0 | znode name_node; |
4350 | |
|
4351 | 0 | if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) { |
4352 | 0 | return; |
4353 | 0 | } |
4354 | | |
4355 | 0 | zend_compile_expr(&name_node, name_ast); |
4356 | |
|
4357 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node); |
4358 | 0 | opline->op1_type = IS_CONST; |
4359 | 0 | LITERAL_STR(opline->op1, zend_string_copy(orig_func_name)); |
4360 | 0 | opline->extended_value = num_args; |
4361 | 0 | } |
4362 | | /* }}} */ |
4363 | | |
4364 | | /* cufa = call_user_func_array */ |
4365 | | static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4366 | 0 | { |
4367 | 0 | znode arg_node; |
4368 | 0 | zend_op *opline; |
4369 | |
|
4370 | 0 | if (args->children != 2) { |
4371 | 0 | return FAILURE; |
4372 | 0 | } |
4373 | | |
4374 | 0 | zend_compile_init_user_func(args->child[0], 0, lcname); |
4375 | 0 | if (args->child[1]->kind == ZEND_AST_CALL |
4376 | 0 | && args->child[1]->child[0]->kind == ZEND_AST_ZVAL |
4377 | 0 | && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING |
4378 | 0 | && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) { |
4379 | 0 | zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]); |
4380 | 0 | zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]); |
4381 | 0 | bool is_fully_qualified; |
4382 | 0 | zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified); |
4383 | |
|
4384 | 0 | if (zend_string_equals_literal_ci(name, "array_slice") |
4385 | 0 | && !zend_args_contain_unpack_or_named(list) |
4386 | 0 | && list->children == 3 |
4387 | 0 | && list->child[1]->kind == ZEND_AST_ZVAL) { |
4388 | 0 | zval *zv = zend_ast_get_zval(list->child[1]); |
4389 | |
|
4390 | 0 | if (Z_TYPE_P(zv) == IS_LONG |
4391 | 0 | && Z_LVAL_P(zv) >= 0 |
4392 | 0 | && Z_LVAL_P(zv) <= 0x7fffffff) { |
4393 | 0 | zend_op *opline; |
4394 | 0 | znode len_node; |
4395 | |
|
4396 | 0 | zend_compile_expr(&arg_node, list->child[0]); |
4397 | 0 | zend_compile_expr(&len_node, list->child[2]); |
4398 | 0 | opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node); |
4399 | 0 | opline->extended_value = Z_LVAL_P(zv); |
4400 | 0 | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4401 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4402 | 0 | opline->result_type = IS_TMP_VAR; |
4403 | 0 | result->op_type = IS_TMP_VAR; |
4404 | 0 | } |
4405 | 0 | zend_string_release_ex(name, 0); |
4406 | 0 | return SUCCESS; |
4407 | 0 | } |
4408 | 0 | } |
4409 | 0 | zend_string_release_ex(name, 0); |
4410 | 0 | } |
4411 | 0 | zend_compile_expr(&arg_node, args->child[1]); |
4412 | 0 | zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL); |
4413 | 0 | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
4414 | 0 | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4415 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4416 | 0 | opline->result_type = IS_TMP_VAR; |
4417 | 0 | result->op_type = IS_TMP_VAR; |
4418 | 0 | } |
4419 | 0 | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4420 | |
|
4421 | 0 | return SUCCESS; |
4422 | 0 | } |
4423 | | /* }}} */ |
4424 | | |
4425 | | /* cuf = call_user_func */ |
4426 | | static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4427 | 0 | { |
4428 | 0 | uint32_t i; |
4429 | |
|
4430 | 0 | if (args->children < 1) { |
4431 | 0 | return FAILURE; |
4432 | 0 | } |
4433 | | |
4434 | 0 | zend_compile_init_user_func(args->child[0], args->children - 1, lcname); |
4435 | 0 | for (i = 1; i < args->children; ++i) { |
4436 | 0 | zend_ast *arg_ast = args->child[i]; |
4437 | 0 | znode arg_node; |
4438 | 0 | zend_op *opline; |
4439 | |
|
4440 | 0 | zend_compile_expr(&arg_node, arg_ast); |
4441 | |
|
4442 | 0 | opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL); |
4443 | 0 | opline->op2.num = i; |
4444 | 0 | opline->result.var = EX_NUM_TO_VAR(i - 1); |
4445 | 0 | } |
4446 | 0 | zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4447 | 0 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4448 | 0 | opline->result_type = IS_TMP_VAR; |
4449 | 0 | result->op_type = IS_TMP_VAR; |
4450 | 0 | } |
4451 | |
|
4452 | 0 | return SUCCESS; |
4453 | 0 | } |
4454 | | /* }}} */ |
4455 | | |
4456 | | static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4457 | 0 | { |
4458 | 0 | if (EG(assertions) >= 0) { |
4459 | 0 | znode name_node; |
4460 | 0 | zend_op *opline; |
4461 | 0 | uint32_t check_op_number = get_next_op_number(); |
4462 | |
|
4463 | 0 | zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL); |
4464 | |
|
4465 | 0 | if (fbc && fbc_is_finalized(fbc)) { |
4466 | 0 | name_node.op_type = IS_CONST; |
4467 | 0 | ZVAL_STR_COPY(&name_node.u.constant, name); |
4468 | |
|
4469 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
4470 | 0 | } else { |
4471 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL); |
4472 | 0 | opline->op2_type = IS_CONST; |
4473 | 0 | opline->op2.constant = zend_add_ns_func_name_literal(name); |
4474 | 0 | } |
4475 | 0 | opline->result.num = zend_alloc_cache_slot(); |
4476 | |
|
4477 | 0 | if (args->children == 1) { |
4478 | | /* add "assert(condition) as assertion message */ |
4479 | 0 | zend_ast *arg = zend_ast_create_zval_from_str( |
4480 | 0 | zend_ast_export("assert(", args->child[0], ")")); |
4481 | 0 | if (args->child[0]->kind == ZEND_AST_NAMED_ARG) { |
4482 | | /* If the original argument was named, add the new argument as named as well, |
4483 | | * as mixing named and positional is not allowed. */ |
4484 | 0 | zend_ast *name = zend_ast_create_zval_from_str( |
4485 | 0 | ZSTR_INIT_LITERAL("description", 0)); |
4486 | 0 | arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg); |
4487 | 0 | } |
4488 | 0 | args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg); |
4489 | 0 | } |
4490 | |
|
4491 | 0 | zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type); |
4492 | |
|
4493 | 0 | opline = &CG(active_op_array)->opcodes[check_op_number]; |
4494 | 0 | opline->op2.opline_num = get_next_op_number(); |
4495 | 0 | SET_NODE(opline->result, result); |
4496 | 0 | } else { |
4497 | 0 | if (!fbc) { |
4498 | 0 | zend_string_release_ex(name, 0); |
4499 | 0 | } |
4500 | 0 | result->op_type = IS_CONST; |
4501 | 0 | ZVAL_TRUE(&result->u.constant); |
4502 | 0 | } |
4503 | 0 | } |
4504 | | /* }}} */ |
4505 | | |
4506 | | static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */ |
4507 | 0 | { |
4508 | 0 | bool strict = false; |
4509 | 0 | znode array, needly; |
4510 | 0 | zend_op *opline; |
4511 | |
|
4512 | 0 | if (args->children == 3) { |
4513 | 0 | if (args->child[2]->kind == ZEND_AST_ZVAL) { |
4514 | 0 | strict = zend_is_true(zend_ast_get_zval(args->child[2])); |
4515 | 0 | } else if (args->child[2]->kind == ZEND_AST_CONST) { |
4516 | 0 | zval value; |
4517 | 0 | zend_ast *name_ast = args->child[2]->child[0]; |
4518 | 0 | bool is_fully_qualified; |
4519 | 0 | zend_string *resolved_name = zend_resolve_const_name( |
4520 | 0 | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
4521 | |
|
4522 | 0 | if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) { |
4523 | 0 | zend_string_release_ex(resolved_name, 0); |
4524 | 0 | return FAILURE; |
4525 | 0 | } |
4526 | | |
4527 | 0 | zend_string_release_ex(resolved_name, 0); |
4528 | 0 | strict = zend_is_true(&value); |
4529 | 0 | zval_ptr_dtor(&value); |
4530 | 0 | } else { |
4531 | 0 | return FAILURE; |
4532 | 0 | } |
4533 | 0 | } else if (args->children != 2) { |
4534 | 0 | return FAILURE; |
4535 | 0 | } |
4536 | | |
4537 | 0 | if (args->child[1]->kind != ZEND_AST_ARRAY |
4538 | 0 | || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) { |
4539 | 0 | return FAILURE; |
4540 | 0 | } |
4541 | | |
4542 | 0 | if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) { |
4543 | 0 | bool ok = true; |
4544 | 0 | zval *val, tmp; |
4545 | 0 | HashTable *src = Z_ARRVAL(array.u.constant); |
4546 | 0 | HashTable *dst = zend_new_array(zend_hash_num_elements(src)); |
4547 | |
|
4548 | 0 | ZVAL_TRUE(&tmp); |
4549 | |
|
4550 | 0 | if (strict) { |
4551 | 0 | ZEND_HASH_FOREACH_VAL(src, val) { |
4552 | 0 | if (Z_TYPE_P(val) == IS_STRING) { |
4553 | 0 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4554 | 0 | } else if (Z_TYPE_P(val) == IS_LONG) { |
4555 | 0 | zend_hash_index_add(dst, Z_LVAL_P(val), &tmp); |
4556 | 0 | } else { |
4557 | 0 | zend_array_destroy(dst); |
4558 | 0 | ok = false; |
4559 | 0 | break; |
4560 | 0 | } |
4561 | 0 | } ZEND_HASH_FOREACH_END(); |
4562 | 0 | } else { |
4563 | 0 | ZEND_HASH_FOREACH_VAL(src, val) { |
4564 | 0 | if (Z_TYPE_P(val) != IS_STRING |
4565 | 0 | || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) { |
4566 | 0 | zend_array_destroy(dst); |
4567 | 0 | ok = false; |
4568 | 0 | break; |
4569 | 0 | } |
4570 | 0 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4571 | 0 | } ZEND_HASH_FOREACH_END(); |
4572 | 0 | } |
4573 | |
|
4574 | 0 | zend_array_destroy(src); |
4575 | 0 | if (!ok) { |
4576 | 0 | return FAILURE; |
4577 | 0 | } |
4578 | 0 | Z_ARRVAL(array.u.constant) = dst; |
4579 | 0 | } |
4580 | 0 | array.op_type = IS_CONST; |
4581 | |
|
4582 | 0 | zend_compile_expr(&needly, args->child[0]); |
4583 | |
|
4584 | 0 | opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array); |
4585 | 0 | opline->extended_value = strict; |
4586 | |
|
4587 | 0 | return SUCCESS; |
4588 | 0 | } |
4589 | | /* }}} */ |
4590 | | |
4591 | | static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */ |
4592 | 0 | { |
4593 | 0 | znode arg_node; |
4594 | 0 | zend_op *opline; |
4595 | |
|
4596 | 0 | if (args->children != 1) { |
4597 | 0 | return FAILURE; |
4598 | 0 | } |
4599 | | |
4600 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4601 | 0 | opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL); |
4602 | 0 | opline->extended_value = zend_string_equals_literal(lcname, "sizeof"); |
4603 | |
|
4604 | 0 | return SUCCESS; |
4605 | 0 | } |
4606 | | /* }}} */ |
4607 | | |
4608 | | static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4609 | 0 | { |
4610 | 0 | if (args->children == 0) { |
4611 | 0 | zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL); |
4612 | 0 | } else { |
4613 | 0 | znode arg_node; |
4614 | |
|
4615 | 0 | if (args->children != 1) { |
4616 | 0 | return FAILURE; |
4617 | 0 | } |
4618 | | |
4619 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4620 | 0 | zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL); |
4621 | 0 | } |
4622 | 0 | return SUCCESS; |
4623 | 0 | } |
4624 | | /* }}} */ |
4625 | | |
4626 | | static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4627 | 0 | { |
4628 | 0 | if (args->children != 0) { |
4629 | 0 | return FAILURE; |
4630 | 0 | } |
4631 | | |
4632 | 0 | zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL); |
4633 | 0 | return SUCCESS; |
4634 | 0 | } |
4635 | | /* }}} */ |
4636 | | |
4637 | | static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */ |
4638 | 0 | { |
4639 | 0 | znode arg_node; |
4640 | |
|
4641 | 0 | if (args->children != 1) { |
4642 | 0 | return FAILURE; |
4643 | 0 | } |
4644 | | |
4645 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
4646 | 0 | zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL); |
4647 | 0 | return SUCCESS; |
4648 | 0 | } |
4649 | | /* }}} */ |
4650 | | |
4651 | | static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4652 | 0 | { |
4653 | 0 | if (CG(active_op_array)->function_name && args->children == 0) { |
4654 | 0 | zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL); |
4655 | 0 | return SUCCESS; |
4656 | 0 | } else { |
4657 | 0 | return FAILURE; |
4658 | 0 | } |
4659 | 0 | } |
4660 | | /* }}} */ |
4661 | | |
4662 | | static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4663 | 0 | { |
4664 | 0 | if (CG(active_op_array)->function_name && args->children == 0) { |
4665 | 0 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL); |
4666 | 0 | return SUCCESS; |
4667 | 0 | } else { |
4668 | 0 | return FAILURE; |
4669 | 0 | } |
4670 | 0 | } |
4671 | | /* }}} */ |
4672 | | |
4673 | | static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */ |
4674 | 0 | { |
4675 | 0 | znode subject, needle; |
4676 | |
|
4677 | 0 | if (args->children != 2) { |
4678 | 0 | return FAILURE; |
4679 | 0 | } |
4680 | | |
4681 | 0 | zend_compile_expr(&needle, args->child[0]); |
4682 | 0 | zend_compile_expr(&subject, args->child[1]); |
4683 | |
|
4684 | 0 | zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject); |
4685 | 0 | return SUCCESS; |
4686 | 0 | } |
4687 | | /* }}} */ |
4688 | | |
4689 | | static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */ |
4690 | 0 | { |
4691 | 0 | if (CG(active_op_array)->function_name |
4692 | 0 | && args->children == 2 |
4693 | 0 | && args->child[0]->kind == ZEND_AST_CALL |
4694 | 0 | && args->child[0]->child[0]->kind == ZEND_AST_ZVAL |
4695 | 0 | && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING |
4696 | 0 | && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST |
4697 | 0 | && args->child[1]->kind == ZEND_AST_ZVAL) { |
4698 | |
|
4699 | 0 | zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]); |
4700 | 0 | bool is_fully_qualified; |
4701 | 0 | zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified); |
4702 | 0 | const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]); |
4703 | 0 | const zval *zv = zend_ast_get_zval(args->child[1]); |
4704 | 0 | znode first; |
4705 | |
|
4706 | 0 | if (zend_string_equals_literal_ci(name, "func_get_args") |
4707 | 0 | && list->children == 0 |
4708 | 0 | && Z_TYPE_P(zv) == IS_LONG |
4709 | 0 | && Z_LVAL_P(zv) >= 0) { |
4710 | 0 | first.op_type = IS_CONST; |
4711 | 0 | ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv)); |
4712 | 0 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL); |
4713 | 0 | zend_string_release_ex(name, 0); |
4714 | 0 | return SUCCESS; |
4715 | 0 | } |
4716 | 0 | zend_string_release_ex(name, 0); |
4717 | 0 | } |
4718 | 0 | return FAILURE; |
4719 | 0 | } |
4720 | | /* }}} */ |
4721 | | |
4722 | | static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler) |
4723 | 0 | { |
4724 | 0 | void **handlers = zend_flf_handlers; |
4725 | 0 | void **current = handlers; |
4726 | 0 | while (current) { |
4727 | 0 | if (*current == handler) { |
4728 | 0 | return current - handlers; |
4729 | 0 | } |
4730 | 0 | current++; |
4731 | 0 | } |
4732 | | |
4733 | 0 | return (uint32_t)-1; |
4734 | 0 | } |
4735 | | |
4736 | | static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
4737 | 0 | { |
4738 | 0 | if (zend_execute_internal) { |
4739 | 0 | return NULL; |
4740 | 0 | } |
4741 | | |
4742 | 0 | if (ZEND_USER_CODE(fbc->type)) { |
4743 | 0 | return NULL; |
4744 | 0 | } |
4745 | | |
4746 | 0 | const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos; |
4747 | 0 | if (!frameless_function_info) { |
4748 | 0 | return NULL; |
4749 | 0 | } |
4750 | | |
4751 | 0 | if (args->children > 3) { |
4752 | 0 | return NULL; |
4753 | 0 | } |
4754 | | |
4755 | 0 | while (frameless_function_info->handler) { |
4756 | 0 | if (frameless_function_info->num_args >= args->children |
4757 | 0 | && fbc->common.required_num_args <= args->children |
4758 | 0 | && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC) |
4759 | 0 | || frameless_function_info->num_args == args->children)) { |
4760 | 0 | uint32_t num_args = frameless_function_info->num_args; |
4761 | 0 | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4762 | 0 | if (offset == (uint32_t)-1) { |
4763 | 0 | continue; |
4764 | 0 | } |
4765 | 0 | return frameless_function_info; |
4766 | 0 | } |
4767 | 0 | frameless_function_info++; |
4768 | 0 | } |
4769 | | |
4770 | 0 | return NULL; |
4771 | 0 | } |
4772 | | |
4773 | | static uint32_t zend_compile_frameless_icall_ex(znode *result, const zend_ast_list *args, const zend_function *fbc, const zend_frameless_function_info *frameless_function_info, uint32_t type) |
4774 | 0 | { |
4775 | 0 | uint32_t lineno = CG(zend_lineno); |
4776 | 0 | uint32_t num_args = frameless_function_info->num_args; |
4777 | 0 | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4778 | 0 | znode arg_zvs[3]; |
4779 | 0 | for (uint32_t i = 0; i < num_args; i++) { |
4780 | 0 | if (i < args->children) { |
4781 | 0 | zend_compile_expr(&arg_zvs[i], args->child[i]); |
4782 | 0 | } else { |
4783 | 0 | const zend_arg_info *arg_info = &fbc->common.arg_info[i]; |
4784 | 0 | arg_zvs[i].op_type = IS_CONST; |
4785 | 0 | if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) { |
4786 | 0 | ZEND_UNREACHABLE(); |
4787 | 0 | } |
4788 | 0 | } |
4789 | 0 | } |
4790 | 0 | uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args; |
4791 | 0 | uint32_t opnum = get_next_op_number(); |
4792 | 0 | zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL); |
4793 | 0 | opline->extended_value = offset; |
4794 | 0 | opline->lineno = lineno; |
4795 | 0 | if (num_args >= 1) { |
4796 | 0 | SET_NODE(opline->op1, &arg_zvs[0]); |
4797 | 0 | } |
4798 | 0 | if (num_args >= 2) { |
4799 | 0 | SET_NODE(opline->op2, &arg_zvs[1]); |
4800 | 0 | } |
4801 | 0 | if (num_args >= 3) { |
4802 | 0 | zend_emit_op_data(&arg_zvs[2]); |
4803 | 0 | } |
4804 | 0 | return opnum; |
4805 | 0 | } |
4806 | | |
4807 | | static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
4808 | 0 | { |
4809 | 0 | const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type); |
4810 | 0 | if (!frameless_function_info) { |
4811 | 0 | return (uint32_t)-1; |
4812 | 0 | } |
4813 | | |
4814 | 0 | return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type); |
4815 | 0 | } |
4816 | | |
4817 | | static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
4818 | 0 | { |
4819 | 0 | int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant)); |
4820 | | |
4821 | | /* Find frameless function with same name. */ |
4822 | 0 | const zend_function *frameless_function = NULL; |
4823 | 0 | if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT |
4824 | 0 | && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast)) |
4825 | | /* Avoid blowing up op count with nested frameless branches. */ |
4826 | 0 | && !CG(context).in_jmp_frameless_branch) { |
4827 | 0 | zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2)); |
4828 | 0 | frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name); |
4829 | 0 | } |
4830 | | |
4831 | | /* Check whether any frameless handler may actually be used. */ |
4832 | 0 | uint32_t jmp_fl_opnum = 0; |
4833 | 0 | const zend_frameless_function_info *frameless_function_info = NULL; |
4834 | 0 | if (frameless_function) { |
4835 | 0 | frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type); |
4836 | 0 | if (frameless_function_info) { |
4837 | 0 | CG(context).in_jmp_frameless_branch = true; |
4838 | 0 | znode op1; |
4839 | 0 | op1.op_type = IS_CONST; |
4840 | 0 | ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1)); |
4841 | 0 | jmp_fl_opnum = get_next_op_number(); |
4842 | 0 | zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL); |
4843 | 0 | } |
4844 | 0 | } |
4845 | | |
4846 | | /* Compile ns call. */ |
4847 | 0 | zend_op *opline = get_next_op(); |
4848 | 0 | opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME; |
4849 | 0 | opline->op2_type = IS_CONST; |
4850 | 0 | opline->op2.constant = name_constants; |
4851 | 0 | opline->result.num = zend_alloc_cache_slot(); |
4852 | 0 | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
4853 | | |
4854 | | /* Compile frameless call. */ |
4855 | 0 | if (frameless_function_info) { |
4856 | 0 | CG(zend_lineno) = lineno; |
4857 | |
|
4858 | 0 | uint32_t jmp_end_opnum = zend_emit_jump(0); |
4859 | 0 | uint32_t jmp_fl_target = get_next_op_number(); |
4860 | |
|
4861 | 0 | uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type); |
4862 | |
|
4863 | 0 | zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum]; |
4864 | 0 | jmp_fl->op2.opline_num = jmp_fl_target; |
4865 | 0 | jmp_fl->extended_value = zend_alloc_cache_slot(); |
4866 | 0 | zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum]; |
4867 | 0 | SET_NODE(flf_icall->result, result); |
4868 | 0 | zend_update_jump_target_to_next(jmp_end_opnum); |
4869 | |
|
4870 | 0 | CG(context).in_jmp_frameless_branch = false; |
4871 | 0 | } |
4872 | 0 | } |
4873 | | /* }}} */ |
4874 | | |
4875 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node); |
4876 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node); |
4877 | | static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline); |
4878 | | |
4879 | | static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */ |
4880 | 0 | { |
4881 | | /* Bail out if we do not have a format string. */ |
4882 | 0 | if (args->children < 1) { |
4883 | 0 | return FAILURE; |
4884 | 0 | } |
4885 | | |
4886 | 0 | zend_eval_const_expr(&args->child[0]); |
4887 | | /* Bail out if the format string is not constant. */ |
4888 | 0 | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
4889 | 0 | return FAILURE; |
4890 | 0 | } |
4891 | | |
4892 | 0 | zval *format_string = zend_ast_get_zval(args->child[0]); |
4893 | 0 | if (Z_TYPE_P(format_string) != IS_STRING) { |
4894 | 0 | return FAILURE; |
4895 | 0 | } |
4896 | 0 | if (Z_STRLEN_P(format_string) >= 256) { |
4897 | 0 | return FAILURE; |
4898 | 0 | } |
4899 | | |
4900 | 0 | char *p; |
4901 | 0 | char *end; |
4902 | 0 | uint32_t placeholder_count; |
4903 | |
|
4904 | 0 | placeholder_count = 0; |
4905 | 0 | p = Z_STRVAL_P(format_string); |
4906 | 0 | end = p + Z_STRLEN_P(format_string); |
4907 | |
|
4908 | 0 | for (;;) { |
4909 | 0 | p = memchr(p, '%', end - p); |
4910 | 0 | if (!p) { |
4911 | 0 | break; |
4912 | 0 | } |
4913 | | |
4914 | 0 | char *q = p + 1; |
4915 | 0 | if (q == end) { |
4916 | 0 | return FAILURE; |
4917 | 0 | } |
4918 | | |
4919 | 0 | switch (*q) { |
4920 | 0 | case 's': |
4921 | 0 | case 'd': |
4922 | 0 | placeholder_count++; |
4923 | 0 | break; |
4924 | 0 | case '%': |
4925 | 0 | break; |
4926 | 0 | default: |
4927 | 0 | return FAILURE; |
4928 | 0 | } |
4929 | | |
4930 | 0 | p = q; |
4931 | 0 | p++; |
4932 | 0 | } |
4933 | | |
4934 | | /* Bail out if the number of placeholders does not match the number of values. */ |
4935 | 0 | if (placeholder_count != (args->children - 1)) { |
4936 | 0 | return FAILURE; |
4937 | 0 | } |
4938 | | |
4939 | | /* Handle empty format strings. */ |
4940 | 0 | if (Z_STRLEN_P(format_string) == 0) { |
4941 | 0 | result->op_type = IS_CONST; |
4942 | 0 | ZVAL_EMPTY_STRING(&result->u.constant); |
4943 | |
|
4944 | 0 | return SUCCESS; |
4945 | 0 | } |
4946 | | |
4947 | 0 | znode *elements = NULL; |
4948 | |
|
4949 | 0 | if (placeholder_count > 0) { |
4950 | 0 | elements = safe_emalloc(sizeof(*elements), placeholder_count, 0); |
4951 | 0 | } |
4952 | | |
4953 | | /* Compile the value expressions first for error handling that is consistent |
4954 | | * with a function call: Values that fail to convert to a string may emit errors. |
4955 | | */ |
4956 | 0 | for (uint32_t i = 0; i < placeholder_count; i++) { |
4957 | 0 | zend_compile_expr(elements + i, args->child[1 + i]); |
4958 | 0 | } |
4959 | |
|
4960 | 0 | uint32_t rope_elements = 0; |
4961 | 0 | uint32_t rope_init_lineno = -1; |
4962 | 0 | zend_op *opline = NULL; |
4963 | |
|
4964 | 0 | placeholder_count = 0; |
4965 | 0 | p = Z_STRVAL_P(format_string); |
4966 | 0 | end = p + Z_STRLEN_P(format_string); |
4967 | 0 | char *offset = p; |
4968 | 0 | for (;;) { |
4969 | 0 | p = memchr(p, '%', end - p); |
4970 | 0 | if (!p) { |
4971 | 0 | break; |
4972 | 0 | } |
4973 | | |
4974 | 0 | char *q = p + 1; |
4975 | 0 | ZEND_ASSERT(q < end); |
4976 | 0 | ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%'); |
4977 | |
|
4978 | 0 | if (*q == '%') { |
4979 | | /* Optimization to not create a dedicated rope element for the literal '%': |
4980 | | * Include the first '%' within the "constant" part instead of dropping the |
4981 | | * full placeholder. |
4982 | | */ |
4983 | 0 | p++; |
4984 | 0 | } |
4985 | |
|
4986 | 0 | if (p != offset) { |
4987 | 0 | znode const_node; |
4988 | 0 | const_node.op_type = IS_CONST; |
4989 | 0 | ZVAL_STRINGL(&const_node.u.constant, offset, p - offset); |
4990 | 0 | if (rope_elements == 0) { |
4991 | 0 | rope_init_lineno = get_next_op_number(); |
4992 | 0 | } |
4993 | 0 | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
4994 | 0 | } |
4995 | |
|
4996 | 0 | if (*q != '%') { |
4997 | 0 | switch (*q) { |
4998 | 0 | case 's': |
4999 | | /* Perform the cast of constants when actually evaluating the corresponding placeholder |
5000 | | * for correct error reporting. |
5001 | | */ |
5002 | 0 | if (elements[placeholder_count].op_type == IS_CONST) { |
5003 | 0 | if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) { |
5004 | 0 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING; |
5005 | 0 | } else { |
5006 | 0 | convert_to_string(&elements[placeholder_count].u.constant); |
5007 | 0 | } |
5008 | 0 | } |
5009 | 0 | break; |
5010 | 0 | case 'd': |
5011 | 0 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG; |
5012 | 0 | break; |
5013 | 0 | default: ZEND_UNREACHABLE(); |
5014 | 0 | } |
5015 | | |
5016 | 0 | if (rope_elements == 0) { |
5017 | 0 | rope_init_lineno = get_next_op_number(); |
5018 | 0 | } |
5019 | 0 | opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]); |
5020 | |
|
5021 | 0 | placeholder_count++; |
5022 | 0 | } |
5023 | | |
5024 | 0 | p = q; |
5025 | 0 | p++; |
5026 | 0 | offset = p; |
5027 | 0 | } |
5028 | 0 | if (end != offset) { |
5029 | | /* Add the constant part after the last placeholder. */ |
5030 | 0 | znode const_node; |
5031 | 0 | const_node.op_type = IS_CONST; |
5032 | 0 | ZVAL_STRINGL(&const_node.u.constant, offset, end - offset); |
5033 | 0 | if (rope_elements == 0) { |
5034 | 0 | rope_init_lineno = get_next_op_number(); |
5035 | 0 | } |
5036 | 0 | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
5037 | 0 | } |
5038 | 0 | ZEND_ASSERT(opline != NULL); |
5039 | |
|
5040 | 0 | zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
5041 | 0 | zend_compile_rope_finalize(result, rope_elements, init_opline, opline); |
5042 | 0 | efree(elements); |
5043 | |
|
5044 | 0 | return SUCCESS; |
5045 | 0 | } |
5046 | | |
5047 | | static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */ |
5048 | 0 | { |
5049 | | /* Special case: printf with a single constant string argument and no format specifiers. |
5050 | | * In this case, just emit ECHO and return the string length if needed. */ |
5051 | 0 | if (args->children == 1) { |
5052 | 0 | zend_eval_const_expr(&args->child[0]); |
5053 | 0 | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
5054 | 0 | return FAILURE; |
5055 | 0 | } |
5056 | 0 | zval *format_string = zend_ast_get_zval(args->child[0]); |
5057 | 0 | if (Z_TYPE_P(format_string) != IS_STRING) { |
5058 | 0 | return FAILURE; |
5059 | 0 | } |
5060 | | /* Check if there are any format specifiers */ |
5061 | 0 | if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) { |
5062 | | /* No format specifiers - just emit ECHO and return string length */ |
5063 | 0 | znode format_node; |
5064 | 0 | zend_compile_expr(&format_node, args->child[0]); |
5065 | 0 | zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL); |
5066 | | |
5067 | | /* Return the string length as a constant if the result is used */ |
5068 | 0 | result->op_type = IS_CONST; |
5069 | 0 | ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string)); |
5070 | 0 | return SUCCESS; |
5071 | 0 | } |
5072 | 0 | } |
5073 | | |
5074 | | /* Fall back to sprintf optimization for format strings with specifiers */ |
5075 | 0 | znode rope_result; |
5076 | 0 | if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) { |
5077 | 0 | return FAILURE; |
5078 | 0 | } |
5079 | | |
5080 | | /* printf() returns the amount of bytes written, so just an ECHO of the |
5081 | | * resulting sprintf() optimisation might not be enough. At this early |
5082 | | * stage we can't detect if the result is actually used, so we just emit |
5083 | | * the opcodes and let them be cleaned up by the dead code elimination |
5084 | | * pass in the Zend Optimizer if the result of the printf() is in fact |
5085 | | * unused */ |
5086 | 0 | znode copy; |
5087 | 0 | if (rope_result.op_type != IS_CONST) { |
5088 | | /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */ |
5089 | 0 | ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR); |
5090 | 0 | zend_emit_op_tmp(©, ZEND_COPY_TMP, &rope_result, NULL); |
5091 | 0 | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5092 | 0 | zend_emit_op_tmp(result, ZEND_STRLEN, ©, NULL); |
5093 | 0 | } else { |
5094 | 0 | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5095 | 0 | result->op_type = IS_CONST; |
5096 | 0 | ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant)); |
5097 | 0 | } |
5098 | |
|
5099 | 0 | return SUCCESS; |
5100 | 0 | } |
5101 | | |
5102 | | static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args) |
5103 | 0 | { |
5104 | 0 | znode arg_node; |
5105 | |
|
5106 | 0 | if (args->children != 1) { |
5107 | 0 | return FAILURE; |
5108 | 0 | } |
5109 | | |
5110 | 0 | zend_compile_expr(&arg_node, args->child[0]); |
5111 | 0 | zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL); |
5112 | |
|
5113 | 0 | return SUCCESS; |
5114 | 0 | } |
5115 | | |
5116 | | static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ |
5117 | 0 | { |
5118 | | /* Bail out if we do not have exactly two parameters. */ |
5119 | 0 | if (args->children != 2) { |
5120 | 0 | return FAILURE; |
5121 | 0 | } |
5122 | | |
5123 | 0 | zend_ast *callback = args->child[0]; |
5124 | | |
5125 | | /* Bail out if the callback is not a FCC/PFA. */ |
5126 | 0 | zend_ast *args_ast; |
5127 | 0 | switch (callback->kind) { |
5128 | 0 | case ZEND_AST_CALL: |
5129 | 0 | case ZEND_AST_STATIC_CALL: |
5130 | 0 | args_ast = zend_ast_call_get_args(callback); |
5131 | 0 | if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { |
5132 | 0 | return FAILURE; |
5133 | 0 | } |
5134 | | |
5135 | 0 | break; |
5136 | 0 | default: |
5137 | 0 | return FAILURE; |
5138 | 0 | } |
5139 | | |
5140 | | /* Bail out if the callback is assert() due to the AST stringification logic |
5141 | | * breaking for the generated call. |
5142 | | */ |
5143 | 0 | if (callback->kind == ZEND_AST_CALL |
5144 | 0 | && callback->child[0]->kind == ZEND_AST_ZVAL |
5145 | 0 | && Z_TYPE_P(zend_ast_get_zval(callback->child[0])) == IS_STRING |
5146 | 0 | && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) { |
5147 | 0 | return FAILURE; |
5148 | 0 | } |
5149 | | |
5150 | 0 | zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
5151 | 0 | if (callback_args->children != 1 || callback_args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) { |
5152 | | /* Full PFA is not yet implemented, will fail in zend_compile_call_common(). */ |
5153 | 0 | return FAILURE; |
5154 | 0 | } |
5155 | | |
5156 | 0 | znode value; |
5157 | 0 | value.op_type = IS_TMP_VAR; |
5158 | 0 | value.u.op.var = get_temporary_variable(); |
5159 | 0 | zend_ast *call_args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&value)); |
5160 | |
|
5161 | 0 | zend_op *opline; |
5162 | |
|
5163 | 0 | znode array; |
5164 | 0 | zend_compile_expr(&array, args->child[1]); |
5165 | | /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */ |
5166 | 0 | if (array.op_type == IS_CONST) { |
5167 | 0 | Z_TRY_ADDREF(array.u.constant); |
5168 | 0 | } |
5169 | | |
5170 | | /* Verify that the input array actually is an array. */ |
5171 | 0 | znode name; |
5172 | 0 | name.op_type = IS_CONST; |
5173 | 0 | ZVAL_STR_COPY(&name.u.constant, lcname); |
5174 | 0 | opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array); |
5175 | 0 | opline->lineno = lineno; |
5176 | 0 | opline->extended_value = (2 << 16) | IS_ARRAY; |
5177 | 0 | const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5178 | 0 | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5179 | 0 | Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData; |
5180 | | |
5181 | | /* Initialize the result array. */ |
5182 | 0 | zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
5183 | | |
5184 | | /* foreach loop starts here. */ |
5185 | 0 | znode key; |
5186 | |
|
5187 | 0 | uint32_t opnum_reset = get_next_op_number(); |
5188 | 0 | znode reset_node; |
5189 | 0 | zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL); |
5190 | 0 | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
5191 | 0 | uint32_t opnum_fetch = get_next_op_number(); |
5192 | 0 | zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value); |
5193 | | |
5194 | | /* loop body */ |
5195 | 0 | znode call_result; |
5196 | 0 | switch (callback->kind) { |
5197 | 0 | case ZEND_AST_CALL: |
5198 | 0 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args)); |
5199 | 0 | break; |
5200 | 0 | case ZEND_AST_STATIC_CALL: |
5201 | 0 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args)); |
5202 | 0 | break; |
5203 | 0 | } |
5204 | 0 | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key); |
5205 | 0 | SET_NODE(opline->result, result); |
5206 | | /* end loop body */ |
5207 | |
|
5208 | 0 | zend_emit_jump(opnum_fetch); |
5209 | |
|
5210 | 0 | uint32_t opnum_loop_end = get_next_op_number(); |
5211 | 0 | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
5212 | 0 | opline->op2.opline_num = opnum_loop_end; |
5213 | 0 | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
5214 | 0 | opline->extended_value = opnum_loop_end; |
5215 | |
|
5216 | 0 | zend_end_loop(opnum_fetch, &reset_node); |
5217 | 0 | zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
5218 | |
|
5219 | 0 | return SUCCESS; |
5220 | 0 | } |
5221 | | |
5222 | | static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */ |
5223 | 0 | { |
5224 | 0 | if (zend_string_equals_literal(lcname, "strlen")) { |
5225 | 0 | return zend_compile_func_strlen(result, args); |
5226 | 0 | } else if (zend_string_equals_literal(lcname, "is_null")) { |
5227 | 0 | return zend_compile_func_typecheck(result, args, IS_NULL); |
5228 | 0 | } else if (zend_string_equals_literal(lcname, "is_bool")) { |
5229 | 0 | return zend_compile_func_typecheck(result, args, _IS_BOOL); |
5230 | 0 | } else if (zend_string_equals_literal(lcname, "is_long") |
5231 | 0 | || zend_string_equals_literal(lcname, "is_int") |
5232 | 0 | || zend_string_equals_literal(lcname, "is_integer") |
5233 | 0 | ) { |
5234 | 0 | return zend_compile_func_typecheck(result, args, IS_LONG); |
5235 | 0 | } else if (zend_string_equals_literal(lcname, "is_float") |
5236 | 0 | || zend_string_equals_literal(lcname, "is_double") |
5237 | 0 | ) { |
5238 | 0 | return zend_compile_func_typecheck(result, args, IS_DOUBLE); |
5239 | 0 | } else if (zend_string_equals_literal(lcname, "is_string")) { |
5240 | 0 | return zend_compile_func_typecheck(result, args, IS_STRING); |
5241 | 0 | } else if (zend_string_equals_literal(lcname, "is_array")) { |
5242 | 0 | return zend_compile_func_typecheck(result, args, IS_ARRAY); |
5243 | 0 | } else if (zend_string_equals_literal(lcname, "is_object")) { |
5244 | 0 | return zend_compile_func_typecheck(result, args, IS_OBJECT); |
5245 | 0 | } else if (zend_string_equals_literal(lcname, "is_resource")) { |
5246 | 0 | return zend_compile_func_typecheck(result, args, IS_RESOURCE); |
5247 | 0 | } else if (zend_string_equals_literal(lcname, "is_scalar")) { |
5248 | 0 | return zend_compile_func_is_scalar(result, args); |
5249 | 0 | } else if (zend_string_equals_literal(lcname, "boolval")) { |
5250 | 0 | return zend_compile_func_cast(result, args, _IS_BOOL); |
5251 | 0 | } else if (zend_string_equals_literal(lcname, "intval")) { |
5252 | 0 | return zend_compile_func_cast(result, args, IS_LONG); |
5253 | 0 | } else if (zend_string_equals_literal(lcname, "floatval") |
5254 | 0 | || zend_string_equals_literal(lcname, "doubleval") |
5255 | 0 | ) { |
5256 | 0 | return zend_compile_func_cast(result, args, IS_DOUBLE); |
5257 | 0 | } else if (zend_string_equals_literal(lcname, "strval")) { |
5258 | 0 | return zend_compile_func_cast(result, args, IS_STRING); |
5259 | 0 | } else if (zend_string_equals_literal(lcname, "defined")) { |
5260 | 0 | return zend_compile_func_defined(result, args); |
5261 | 0 | } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) { |
5262 | 0 | return zend_compile_func_chr(result, args); |
5263 | 0 | } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) { |
5264 | 0 | return zend_compile_func_ord(result, args); |
5265 | 0 | } else if (zend_string_equals_literal(lcname, "call_user_func_array")) { |
5266 | 0 | return zend_compile_func_cufa(result, args, lcname, type); |
5267 | 0 | } else if (zend_string_equals_literal(lcname, "call_user_func")) { |
5268 | 0 | return zend_compile_func_cuf(result, args, lcname, type); |
5269 | 0 | } else if (zend_string_equals_literal(lcname, "in_array")) { |
5270 | 0 | return zend_compile_func_in_array(result, args); |
5271 | 0 | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT)) |
5272 | 0 | || zend_string_equals_literal(lcname, "sizeof")) { |
5273 | 0 | return zend_compile_func_count(result, args, lcname); |
5274 | 0 | } else if (zend_string_equals_literal(lcname, "get_class")) { |
5275 | 0 | return zend_compile_func_get_class(result, args); |
5276 | 0 | } else if (zend_string_equals_literal(lcname, "get_called_class")) { |
5277 | 0 | return zend_compile_func_get_called_class(result, args); |
5278 | 0 | } else if (zend_string_equals_literal(lcname, "gettype")) { |
5279 | 0 | return zend_compile_func_gettype(result, args); |
5280 | 0 | } else if (zend_string_equals_literal(lcname, "func_num_args")) { |
5281 | 0 | return zend_compile_func_num_args(result, args); |
5282 | 0 | } else if (zend_string_equals_literal(lcname, "func_get_args")) { |
5283 | 0 | return zend_compile_func_get_args(result, args); |
5284 | 0 | } else if (zend_string_equals_literal(lcname, "array_slice")) { |
5285 | 0 | return zend_compile_func_array_slice(result, args); |
5286 | 0 | } else if (zend_string_equals_literal(lcname, "array_key_exists")) { |
5287 | 0 | return zend_compile_func_array_key_exists(result, args); |
5288 | 0 | } else if (zend_string_equals_literal(lcname, "sprintf")) { |
5289 | 0 | return zend_compile_func_sprintf(result, args); |
5290 | 0 | } else if (zend_string_equals_literal(lcname, "printf")) { |
5291 | 0 | return zend_compile_func_printf(result, args); |
5292 | 0 | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) { |
5293 | 0 | return zend_compile_func_clone(result, args); |
5294 | 0 | } else if (zend_string_equals_literal(lcname, "array_map")) { |
5295 | 0 | return zend_compile_func_array_map(result, args, lcname, lineno); |
5296 | 0 | } else { |
5297 | 0 | return FAILURE; |
5298 | 0 | } |
5299 | 0 | } |
5300 | | |
5301 | | static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */ |
5302 | 0 | { |
5303 | 0 | if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) { |
5304 | 0 | return FAILURE; |
5305 | 0 | } |
5306 | | |
5307 | 0 | if (fbc->type != ZEND_INTERNAL_FUNCTION) { |
5308 | | /* If the function is part of disabled_functions, it may be redeclared as a userland |
5309 | | * function with a different implementation. Don't use the VM builtin in that case. */ |
5310 | 0 | return FAILURE; |
5311 | 0 | } |
5312 | | |
5313 | 0 | if (zend_args_contain_unpack_or_named(args)) { |
5314 | 0 | return FAILURE; |
5315 | 0 | } |
5316 | | |
5317 | 0 | if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) { |
5318 | 0 | return SUCCESS; |
5319 | 0 | } |
5320 | | |
5321 | 0 | return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE; |
5322 | 0 | } |
5323 | | |
5324 | 0 | static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) { |
5325 | 0 | switch (kind) { |
5326 | 0 | case ZEND_PROPERTY_HOOK_GET: |
5327 | 0 | return "get"; |
5328 | 0 | case ZEND_PROPERTY_HOOK_SET: |
5329 | 0 | return "set"; |
5330 | 0 | default: ZEND_UNREACHABLE(); |
5331 | 0 | } |
5332 | 0 | } |
5333 | | |
5334 | | static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name) |
5335 | 0 | { |
5336 | 0 | if (ZSTR_VAL(prop_name)[0] != '\0') { |
5337 | 0 | return zend_string_copy(prop_name); |
5338 | 0 | } else { |
5339 | 0 | const char *unmangled = zend_get_unmangled_property_name(prop_name); |
5340 | 0 | return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false); |
5341 | 0 | } |
5342 | 0 | } |
5343 | | |
5344 | | static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type) |
5345 | 0 | { |
5346 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL); |
5347 | |
|
5348 | 0 | const zend_ast *class_ast = ast->child[0]; |
5349 | 0 | zend_ast *method_ast = ast->child[1]; |
5350 | | |
5351 | | /* Recognize parent::$prop::get() pattern. */ |
5352 | 0 | if (class_ast->kind != ZEND_AST_STATIC_PROP |
5353 | 0 | || (class_ast->attr & ZEND_PARENTHESIZED_STATIC_PROP) |
5354 | 0 | || class_ast->child[0]->kind != ZEND_AST_ZVAL |
5355 | 0 | || Z_TYPE_P(zend_ast_get_zval(class_ast->child[0])) != IS_STRING |
5356 | 0 | || zend_get_class_fetch_type(zend_ast_get_str(class_ast->child[0])) != ZEND_FETCH_CLASS_PARENT |
5357 | 0 | || class_ast->child[1]->kind != ZEND_AST_ZVAL |
5358 | 0 | || method_ast->kind != ZEND_AST_ZVAL |
5359 | 0 | || Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING |
5360 | 0 | || (!zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "get") |
5361 | 0 | && !zend_string_equals_literal_ci(zend_ast_get_str(method_ast), "set"))) { |
5362 | 0 | return false; |
5363 | 0 | } |
5364 | | |
5365 | 0 | zend_class_entry *ce = CG(active_class_entry); |
5366 | 0 | if (!ce) { |
5367 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active"); |
5368 | 0 | } |
5369 | | |
5370 | 0 | zend_ast *args_ast = ast->child[2]; |
5371 | 0 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
5372 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call"); |
5373 | 0 | } |
5374 | | |
5375 | 0 | zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]); |
5376 | 0 | zend_string *property_name = zval_get_string(property_hook_name_zv); |
5377 | 0 | zend_string *hook_name = zend_ast_get_str(method_ast); |
5378 | 0 | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name); |
5379 | 0 | ZEND_ASSERT(hook_kind != (uint32_t)-1); |
5380 | |
|
5381 | 0 | const zend_string *prop_info_name = CG(context).active_property_info_name; |
5382 | 0 | if (!prop_info_name) { |
5383 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook", |
5384 | 0 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name)); |
5385 | 0 | } |
5386 | | |
5387 | 0 | const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name); |
5388 | 0 | if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) { |
5389 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)", |
5390 | 0 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name); |
5391 | 0 | } |
5392 | 0 | if (hook_kind != CG(context).active_property_hook_kind) { |
5393 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)", |
5394 | 0 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind)); |
5395 | 0 | } |
5396 | | |
5397 | 0 | zend_op *opline = get_next_op(); |
5398 | 0 | opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL; |
5399 | 0 | opline->op1_type = IS_CONST; |
5400 | 0 | opline->op1.constant = zend_add_literal_string(&property_name); |
5401 | 0 | opline->op2.num = hook_kind; |
5402 | |
|
5403 | 0 | zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R); |
5404 | |
|
5405 | 0 | return true; |
5406 | 0 | } |
5407 | | |
5408 | | static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
5409 | 0 | { |
5410 | 0 | zend_ast *name_ast = ast->child[0]; |
5411 | 0 | zend_ast *args_ast = ast->child[1]; |
5412 | 0 | bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT; |
5413 | |
|
5414 | 0 | znode name_node; |
5415 | |
|
5416 | 0 | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
5417 | 0 | zend_compile_expr(&name_node, name_ast); |
5418 | 0 | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5419 | 0 | return; |
5420 | 0 | } |
5421 | | |
5422 | 0 | { |
5423 | 0 | bool runtime_resolution = zend_compile_function_name(&name_node, name_ast); |
5424 | 0 | if (runtime_resolution) { |
5425 | 0 | if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert") |
5426 | 0 | && !is_callable_convert) { |
5427 | 0 | zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type); |
5428 | 0 | } else { |
5429 | 0 | zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type); |
5430 | 0 | } |
5431 | 0 | return; |
5432 | 0 | } |
5433 | 0 | } |
5434 | | |
5435 | 0 | { |
5436 | 0 | const zval *name = &name_node.u.constant; |
5437 | 0 | zend_string *lcname = zend_string_tolower(Z_STR_P(name)); |
5438 | 0 | zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5439 | 0 | const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL; |
5440 | 0 | zend_op *opline; |
5441 | | |
5442 | | /* Special assert() handling should apply independently of compiler flags. */ |
5443 | 0 | if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) { |
5444 | 0 | zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type); |
5445 | 0 | zend_string_release(lcname); |
5446 | 0 | zval_ptr_dtor(&name_node.u.constant); |
5447 | 0 | return; |
5448 | 0 | } |
5449 | | |
5450 | 0 | if (!fbc |
5451 | 0 | || !fbc_is_finalized(fbc) |
5452 | 0 | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
5453 | 0 | zend_string_release_ex(lcname, 0); |
5454 | 0 | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5455 | 0 | return; |
5456 | 0 | } |
5457 | | |
5458 | 0 | if (!is_callable_convert && |
5459 | 0 | zend_try_compile_special_func(result, lcname, |
5460 | 0 | zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS |
5461 | 0 | ) { |
5462 | 0 | zend_string_release_ex(lcname, 0); |
5463 | 0 | zval_ptr_dtor(&name_node.u.constant); |
5464 | 0 | return; |
5465 | 0 | } |
5466 | | |
5467 | 0 | zval_ptr_dtor(&name_node.u.constant); |
5468 | 0 | ZVAL_STR(&name_node.u.constant, lcname); |
5469 | |
|
5470 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
5471 | 0 | opline->result.num = zend_alloc_cache_slot(); |
5472 | | |
5473 | | /* Store offset to function from symbol table in op2.extra. */ |
5474 | 0 | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
5475 | 0 | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5476 | 0 | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData; |
5477 | 0 | } |
5478 | |
|
5479 | 0 | zend_compile_call_common(result, args_ast, fbc, ast->lineno, type); |
5480 | 0 | } |
5481 | 0 | } |
5482 | | /* }}} */ |
5483 | | |
5484 | | static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5485 | 0 | { |
5486 | 0 | zend_ast *obj_ast = ast->child[0]; |
5487 | 0 | zend_ast *method_ast = ast->child[1]; |
5488 | 0 | zend_ast *args_ast = ast->child[2]; |
5489 | |
|
5490 | 0 | znode obj_node, method_node; |
5491 | 0 | zend_op *opline; |
5492 | 0 | const zend_function *fbc = NULL; |
5493 | 0 | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL; |
5494 | 0 | uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint(); |
5495 | |
|
5496 | 0 | if (is_this_fetch(obj_ast)) { |
5497 | 0 | if (this_guaranteed_exists()) { |
5498 | 0 | obj_node.op_type = IS_UNUSED; |
5499 | 0 | } else { |
5500 | 0 | zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
5501 | 0 | } |
5502 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
5503 | | |
5504 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
5505 | | * check for a nullsafe access. */ |
5506 | 0 | } else { |
5507 | 0 | zend_short_circuiting_mark_inner(obj_ast); |
5508 | 0 | zend_compile_expr(&obj_node, obj_ast); |
5509 | 0 | if (nullsafe) { |
5510 | 0 | zend_emit_jmp_null(&obj_node, type); |
5511 | 0 | } |
5512 | 0 | } |
5513 | |
|
5514 | 0 | zend_compile_expr(&method_node, method_ast); |
5515 | 0 | opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL); |
5516 | |
|
5517 | 0 | if (method_node.op_type == IS_CONST) { |
5518 | 0 | if (Z_TYPE(method_node.u.constant) != IS_STRING) { |
5519 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5520 | 0 | } |
5521 | | |
5522 | 0 | opline->op2_type = IS_CONST; |
5523 | 0 | opline->op2.constant = zend_add_func_name_literal( |
5524 | 0 | Z_STR(method_node.u.constant)); |
5525 | 0 | opline->result.num = zend_alloc_cache_slots(2); |
5526 | 0 | } else { |
5527 | 0 | SET_NODE(opline->op2, &method_node); |
5528 | 0 | } |
5529 | | |
5530 | | /* Check if this calls a known method on $this */ |
5531 | 0 | if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST && |
5532 | 0 | CG(active_class_entry) && zend_is_scope_known()) { |
5533 | 0 | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5534 | 0 | fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname); |
5535 | | |
5536 | | /* We only know the exact method that is being called if it is either private or final. |
5537 | | * Otherwise an overriding method in a child class may be called. */ |
5538 | 0 | if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) { |
5539 | 0 | fbc = NULL; |
5540 | 0 | } |
5541 | 0 | } |
5542 | |
|
5543 | 0 | if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) { |
5544 | 0 | if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) { |
5545 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
5546 | 0 | "Cannot combine nullsafe operator with Closure creation"); |
5547 | 0 | } |
5548 | 0 | } |
5549 | 0 | } |
5550 | | /* }}} */ |
5551 | | |
5552 | | static bool zend_is_constructor(const zend_string *name) /* {{{ */ |
5553 | 0 | { |
5554 | 0 | return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME); |
5555 | 0 | } |
5556 | | /* }}} */ |
5557 | | |
5558 | | static bool is_func_accessible(const zend_function *fbc) |
5559 | 0 | { |
5560 | 0 | if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) { |
5561 | 0 | return true; |
5562 | 0 | } |
5563 | | |
5564 | 0 | if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE) |
5565 | 0 | && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED) |
5566 | 0 | && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED)) |
5567 | 0 | && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) { |
5568 | 0 | return true; |
5569 | 0 | } |
5570 | | |
5571 | 0 | return false; |
5572 | 0 | } |
5573 | | |
5574 | | static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */ |
5575 | 0 | { |
5576 | 0 | const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname); |
5577 | |
|
5578 | 0 | if (!fbc || is_func_accessible(fbc)) { |
5579 | 0 | return fbc; |
5580 | 0 | } |
5581 | | |
5582 | 0 | return NULL; |
5583 | 0 | } |
5584 | | /* }}} */ |
5585 | | |
5586 | | static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5587 | 0 | { |
5588 | 0 | zend_ast *class_ast = ast->child[0]; |
5589 | 0 | zend_ast *method_ast = ast->child[1]; |
5590 | 0 | zend_ast *args_ast = ast->child[2]; |
5591 | |
|
5592 | 0 | znode class_node, method_node; |
5593 | 0 | zend_op *opline; |
5594 | 0 | const zend_function *fbc = NULL; |
5595 | |
|
5596 | 0 | if (zend_compile_parent_property_hook_call(result, ast, type)) { |
5597 | 0 | return; |
5598 | 0 | } |
5599 | | |
5600 | 0 | zend_short_circuiting_mark_inner(class_ast); |
5601 | 0 | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5602 | |
|
5603 | 0 | zend_compile_expr(&method_node, method_ast); |
5604 | |
|
5605 | 0 | if (method_node.op_type == IS_CONST) { |
5606 | 0 | zval *name = &method_node.u.constant; |
5607 | 0 | if (Z_TYPE_P(name) != IS_STRING) { |
5608 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5609 | 0 | } |
5610 | 0 | if (zend_is_constructor(Z_STR_P(name))) { |
5611 | 0 | zval_ptr_dtor(name); |
5612 | 0 | method_node.op_type = IS_UNUSED; |
5613 | 0 | } |
5614 | 0 | } |
5615 | | |
5616 | 0 | opline = get_next_op(); |
5617 | 0 | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
5618 | |
|
5619 | 0 | zend_set_class_name_op1(opline, &class_node); |
5620 | |
|
5621 | 0 | if (method_node.op_type == IS_CONST) { |
5622 | 0 | opline->op2_type = IS_CONST; |
5623 | 0 | opline->op2.constant = zend_add_func_name_literal( |
5624 | 0 | Z_STR(method_node.u.constant)); |
5625 | 0 | opline->result.num = zend_alloc_cache_slots(2); |
5626 | 0 | } else { |
5627 | 0 | if (opline->op1_type == IS_CONST) { |
5628 | 0 | opline->result.num = zend_alloc_cache_slot(); |
5629 | 0 | } |
5630 | 0 | SET_NODE(opline->op2, &method_node); |
5631 | 0 | } |
5632 | | |
5633 | | /* Check if we already know which method we're calling */ |
5634 | 0 | if (opline->op2_type == IS_CONST) { |
5635 | 0 | zend_class_entry *ce = NULL; |
5636 | 0 | if (opline->op1_type == IS_CONST) { |
5637 | 0 | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5638 | 0 | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5639 | 0 | if (ce) { |
5640 | 0 | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5641 | 0 | ce = NULL; |
5642 | 0 | } |
5643 | 0 | } else if (CG(active_class_entry) |
5644 | 0 | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5645 | 0 | ce = CG(active_class_entry); |
5646 | 0 | } |
5647 | 0 | } else if (opline->op1_type == IS_UNUSED |
5648 | 0 | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5649 | 0 | && zend_is_scope_known()) { |
5650 | 0 | ce = CG(active_class_entry); |
5651 | 0 | } |
5652 | 0 | if (ce) { |
5653 | 0 | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5654 | 0 | fbc = zend_get_compatible_func_or_null(ce, lcname); |
5655 | 0 | } |
5656 | 0 | } |
5657 | |
|
5658 | 0 | zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type); |
5659 | 0 | } |
5660 | | /* }}} */ |
5661 | | |
5662 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel); |
5663 | | |
5664 | | static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */ |
5665 | 0 | { |
5666 | 0 | zend_ast *class_ast = ast->child[0]; |
5667 | 0 | zend_ast *args_ast = ast->child[1]; |
5668 | |
|
5669 | 0 | znode class_node, ctor_result; |
5670 | 0 | zend_op *opline; |
5671 | |
|
5672 | 0 | if (class_ast->kind == ZEND_AST_CLASS) { |
5673 | | /* anon class declaration */ |
5674 | 0 | zend_compile_class_decl(&class_node, class_ast, false); |
5675 | 0 | } else { |
5676 | 0 | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5677 | 0 | } |
5678 | |
|
5679 | 0 | opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL); |
5680 | |
|
5681 | 0 | zend_set_class_name_op1(opline, &class_node); |
5682 | |
|
5683 | 0 | if (opline->op1_type == IS_CONST) { |
5684 | 0 | opline->op2.num = zend_alloc_cache_slot(); |
5685 | 0 | } |
5686 | |
|
5687 | 0 | zend_class_entry *ce = NULL; |
5688 | 0 | if (opline->op1_type == IS_CONST) { |
5689 | 0 | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5690 | 0 | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5691 | 0 | if (ce) { |
5692 | 0 | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5693 | 0 | ce = NULL; |
5694 | 0 | } |
5695 | 0 | } else if (CG(active_class_entry) |
5696 | 0 | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5697 | 0 | ce = CG(active_class_entry); |
5698 | 0 | } |
5699 | 0 | } else if (opline->op1_type == IS_UNUSED |
5700 | 0 | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5701 | 0 | && zend_is_scope_known()) { |
5702 | 0 | ce = CG(active_class_entry); |
5703 | 0 | } |
5704 | | |
5705 | |
|
5706 | 0 | const zend_function *fbc = NULL; |
5707 | 0 | if (ce |
5708 | 0 | && ce->default_object_handlers->get_constructor == zend_std_get_constructor |
5709 | 0 | && ce->constructor |
5710 | 0 | && is_func_accessible(ce->constructor)) { |
5711 | 0 | fbc = ce->constructor; |
5712 | 0 | } |
5713 | |
|
5714 | 0 | zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R); |
5715 | 0 | zend_do_free(&ctor_result); |
5716 | 0 | } |
5717 | | /* }}} */ |
5718 | | |
5719 | | static void zend_compile_global_var(zend_ast *ast) /* {{{ */ |
5720 | 0 | { |
5721 | 0 | zend_ast *var_ast = ast->child[0]; |
5722 | 0 | zend_ast *name_ast = var_ast->child[0]; |
5723 | |
|
5724 | 0 | znode name_node, result; |
5725 | |
|
5726 | 0 | zend_compile_expr(&name_node, name_ast); |
5727 | 0 | if (name_node.op_type == IS_CONST) { |
5728 | 0 | convert_to_string(&name_node.u.constant); |
5729 | 0 | } |
5730 | | |
5731 | | // TODO(GLOBALS) Forbid "global $GLOBALS"? |
5732 | 0 | if (is_this_fetch(var_ast)) { |
5733 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable"); |
5734 | 0 | } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) { |
5735 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node); |
5736 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
5737 | 0 | } else { |
5738 | | /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W |
5739 | | * to not free the name_node operand, so it can be reused in the following |
5740 | | * ASSIGN_REF, which then frees it. */ |
5741 | 0 | zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL); |
5742 | 0 | opline->extended_value = ZEND_FETCH_GLOBAL_LOCK; |
5743 | |
|
5744 | 0 | if (name_node.op_type == IS_CONST) { |
5745 | 0 | zend_string_addref(Z_STR(name_node.u.constant)); |
5746 | 0 | } |
5747 | |
|
5748 | 0 | zend_emit_assign_ref_znode( |
5749 | 0 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)), |
5750 | 0 | &result |
5751 | 0 | ); |
5752 | 0 | } |
5753 | 0 | } |
5754 | | /* }}} */ |
5755 | | |
5756 | | static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */ |
5757 | 0 | { |
5758 | 0 | zend_op *opline; |
5759 | 0 | if (!CG(active_op_array)->static_variables) { |
5760 | 0 | if (CG(active_op_array)->scope) { |
5761 | 0 | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5762 | 0 | } |
5763 | 0 | CG(active_op_array)->static_variables = zend_new_array(8); |
5764 | 0 | } |
5765 | |
|
5766 | 0 | value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value); |
5767 | |
|
5768 | 0 | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5769 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5770 | 0 | } |
5771 | | |
5772 | 0 | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL); |
5773 | 0 | opline->op1_type = IS_CV; |
5774 | 0 | opline->op1.var = lookup_cv(var_name); |
5775 | 0 | opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode; |
5776 | 0 | } |
5777 | | /* }}} */ |
5778 | | |
5779 | | static void zend_compile_static_var(zend_ast *ast) /* {{{ */ |
5780 | 0 | { |
5781 | 0 | zend_ast *var_ast = ast->child[0]; |
5782 | 0 | zend_string *var_name = zend_ast_get_str(var_ast); |
5783 | |
|
5784 | 0 | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5785 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5786 | 0 | } |
5787 | | |
5788 | 0 | if (!CG(active_op_array)->static_variables) { |
5789 | 0 | if (CG(active_op_array)->scope) { |
5790 | 0 | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5791 | 0 | } |
5792 | 0 | CG(active_op_array)->static_variables = zend_new_array(8); |
5793 | 0 | } |
5794 | |
|
5795 | 0 | if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) { |
5796 | 0 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name); |
5797 | 0 | } |
5798 | | |
5799 | 0 | zend_eval_const_expr(&ast->child[1]); |
5800 | 0 | zend_ast *value_ast = ast->child[1]; |
5801 | |
|
5802 | 0 | if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) { |
5803 | 0 | zval *value_zv = value_ast |
5804 | 0 | ? zend_ast_get_zval(value_ast) |
5805 | 0 | : &EG(uninitialized_zval); |
5806 | 0 | Z_TRY_ADDREF_P(value_zv); |
5807 | 0 | zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF); |
5808 | 0 | } else { |
5809 | 0 | zend_op *opline; |
5810 | |
|
5811 | 0 | zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval)); |
5812 | 0 | uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData); |
5813 | |
|
5814 | 0 | uint32_t static_def_jmp_opnum = get_next_op_number(); |
5815 | 0 | opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL); |
5816 | 0 | opline->op1_type = IS_CV; |
5817 | 0 | opline->op1.var = lookup_cv(var_name); |
5818 | 0 | opline->extended_value = placeholder_offset; |
5819 | |
|
5820 | 0 | znode expr; |
5821 | 0 | zend_compile_expr(&expr, value_ast); |
5822 | |
|
5823 | 0 | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr); |
5824 | 0 | opline->op1_type = IS_CV; |
5825 | 0 | opline->op1.var = lookup_cv(var_name); |
5826 | 0 | opline->extended_value = placeholder_offset | ZEND_BIND_REF; |
5827 | |
|
5828 | 0 | zend_update_jump_target_to_next(static_def_jmp_opnum); |
5829 | 0 | } |
5830 | 0 | } |
5831 | | /* }}} */ |
5832 | | |
5833 | | static void zend_compile_unset(const zend_ast *ast) /* {{{ */ |
5834 | 0 | { |
5835 | 0 | zend_ast *var_ast = ast->child[0]; |
5836 | 0 | znode var_node; |
5837 | 0 | zend_op *opline; |
5838 | |
|
5839 | 0 | zend_ensure_writable_variable(var_ast); |
5840 | |
|
5841 | 0 | if (is_global_var_fetch(var_ast)) { |
5842 | 0 | if (!var_ast->child[1]) { |
5843 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
5844 | 0 | } |
5845 | | |
5846 | 0 | zend_compile_expr(&var_node, var_ast->child[1]); |
5847 | 0 | if (var_node.op_type == IS_CONST) { |
5848 | 0 | convert_to_string(&var_node.u.constant); |
5849 | 0 | } |
5850 | |
|
5851 | 0 | opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL); |
5852 | 0 | opline->extended_value = ZEND_FETCH_GLOBAL; |
5853 | 0 | return; |
5854 | 0 | } |
5855 | | |
5856 | 0 | switch (var_ast->kind) { |
5857 | 0 | case ZEND_AST_VAR: |
5858 | 0 | if (is_this_fetch(var_ast)) { |
5859 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this"); |
5860 | 0 | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) { |
5861 | 0 | opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL); |
5862 | 0 | } else { |
5863 | 0 | opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false); |
5864 | 0 | opline->opcode = ZEND_UNSET_VAR; |
5865 | 0 | } |
5866 | 0 | return; |
5867 | 0 | case ZEND_AST_DIM: |
5868 | 0 | opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false); |
5869 | 0 | opline->opcode = ZEND_UNSET_DIM; |
5870 | 0 | return; |
5871 | 0 | case ZEND_AST_PROP: |
5872 | 0 | case ZEND_AST_NULLSAFE_PROP: |
5873 | 0 | opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false); |
5874 | 0 | opline->opcode = ZEND_UNSET_OBJ; |
5875 | 0 | return; |
5876 | 0 | case ZEND_AST_STATIC_PROP: |
5877 | 0 | opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false); |
5878 | 0 | opline->opcode = ZEND_UNSET_STATIC_PROP; |
5879 | 0 | return; |
5880 | 0 | default: ZEND_UNREACHABLE(); |
5881 | 0 | } |
5882 | 0 | } |
5883 | | /* }}} */ |
5884 | | |
5885 | | static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */ |
5886 | 0 | { |
5887 | 0 | const zend_loop_var *base; |
5888 | 0 | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
5889 | |
|
5890 | 0 | if (!loop_var) { |
5891 | 0 | return true; |
5892 | 0 | } |
5893 | 0 | base = zend_stack_base(&CG(loop_var_stack)); |
5894 | 0 | for (; loop_var >= base; loop_var--) { |
5895 | 0 | if (loop_var->opcode == ZEND_FAST_CALL) { |
5896 | 0 | zend_op *opline = get_next_op(); |
5897 | |
|
5898 | 0 | opline->opcode = ZEND_FAST_CALL; |
5899 | 0 | opline->result_type = IS_TMP_VAR; |
5900 | 0 | opline->result.var = loop_var->var_num; |
5901 | 0 | if (return_value) { |
5902 | 0 | SET_NODE(opline->op2, return_value); |
5903 | 0 | } |
5904 | 0 | opline->op1.num = loop_var->try_catch_offset; |
5905 | 0 | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
5906 | 0 | zend_op *opline = get_next_op(); |
5907 | 0 | opline->opcode = ZEND_DISCARD_EXCEPTION; |
5908 | 0 | opline->op1_type = IS_TMP_VAR; |
5909 | 0 | opline->op1.var = loop_var->var_num; |
5910 | 0 | } else if (loop_var->opcode == ZEND_RETURN) { |
5911 | | /* Stack separator */ |
5912 | 0 | break; |
5913 | 0 | } else if (depth <= 1) { |
5914 | 0 | return 1; |
5915 | 0 | } else if (loop_var->opcode == ZEND_NOP) { |
5916 | | /* Loop doesn't have freeable variable */ |
5917 | 0 | depth--; |
5918 | 0 | } else { |
5919 | 0 | zend_op *opline; |
5920 | |
|
5921 | 0 | ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR)); |
5922 | 0 | opline = get_next_op(); |
5923 | 0 | opline->opcode = loop_var->opcode; |
5924 | 0 | opline->op1_type = loop_var->var_type; |
5925 | 0 | opline->op1.var = loop_var->var_num; |
5926 | 0 | opline->extended_value = ZEND_FREE_ON_RETURN; |
5927 | 0 | depth--; |
5928 | 0 | } |
5929 | 0 | } |
5930 | 0 | return (depth == 0); |
5931 | 0 | } |
5932 | | /* }}} */ |
5933 | | |
5934 | | static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */ |
5935 | 0 | { |
5936 | 0 | return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value); |
5937 | 0 | } |
5938 | | /* }}} */ |
5939 | | |
5940 | | static bool zend_has_finally_ex(zend_long depth) /* {{{ */ |
5941 | 0 | { |
5942 | 0 | const zend_loop_var *base; |
5943 | 0 | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
5944 | |
|
5945 | 0 | if (!loop_var) { |
5946 | 0 | return false; |
5947 | 0 | } |
5948 | 0 | base = zend_stack_base(&CG(loop_var_stack)); |
5949 | 0 | for (; loop_var >= base; loop_var--) { |
5950 | 0 | if (loop_var->opcode == ZEND_FAST_CALL) { |
5951 | 0 | return 1; |
5952 | 0 | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
5953 | 0 | } else if (loop_var->opcode == ZEND_RETURN) { |
5954 | | /* Stack separator */ |
5955 | 0 | return 0; |
5956 | 0 | } else if (depth <= 1) { |
5957 | 0 | return 0; |
5958 | 0 | } else { |
5959 | 0 | depth--; |
5960 | 0 | } |
5961 | 0 | } |
5962 | 0 | return false; |
5963 | 0 | } |
5964 | | /* }}} */ |
5965 | | |
5966 | | static bool zend_has_finally(void) /* {{{ */ |
5967 | 0 | { |
5968 | 0 | return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1); |
5969 | 0 | } |
5970 | | /* }}} */ |
5971 | | |
5972 | | static void zend_compile_return(const zend_ast *ast) /* {{{ */ |
5973 | 0 | { |
5974 | 0 | zend_ast *expr_ast = ast->child[0]; |
5975 | 0 | bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0; |
5976 | 0 | bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
5977 | |
|
5978 | 0 | znode expr_node; |
5979 | 0 | zend_op *opline; |
5980 | |
|
5981 | 0 | if (is_generator) { |
5982 | | /* For generators the by-ref flag refers to yields, not returns */ |
5983 | 0 | by_ref = false; |
5984 | 0 | } |
5985 | |
|
5986 | 0 | if (!expr_ast) { |
5987 | 0 | expr_node.op_type = IS_CONST; |
5988 | 0 | ZVAL_NULL(&expr_node.u.constant); |
5989 | 0 | } else if (by_ref && zend_is_variable_or_call(expr_ast)) { |
5990 | 0 | zend_assert_not_short_circuited(expr_ast); |
5991 | 0 | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
5992 | 0 | } else { |
5993 | 0 | zend_compile_expr(&expr_node, expr_ast); |
5994 | 0 | } |
5995 | |
|
5996 | 0 | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) |
5997 | 0 | && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR)) |
5998 | 0 | && zend_has_finally()) { |
5999 | | /* Copy return value into temporary VAR to avoid modification in finally code */ |
6000 | 0 | if (by_ref) { |
6001 | 0 | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
6002 | 0 | } else { |
6003 | 0 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL); |
6004 | 0 | } |
6005 | 0 | } |
6006 | | |
6007 | | /* Generator return types are handled separately */ |
6008 | 0 | if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6009 | 0 | zend_emit_return_type_check( |
6010 | 0 | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6011 | 0 | } |
6012 | |
|
6013 | 0 | uint32_t opnum_before_finally = get_next_op_number(); |
6014 | |
|
6015 | 0 | zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL); |
6016 | | |
6017 | | /* Content of reference might have changed in finally, repeat type check. */ |
6018 | 0 | if (by_ref |
6019 | | /* Check if any opcodes were emitted since the last return type check. */ |
6020 | 0 | && opnum_before_finally != get_next_op_number() |
6021 | 0 | && !is_generator |
6022 | 0 | && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6023 | 0 | zend_emit_return_type_check( |
6024 | 0 | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6025 | 0 | } |
6026 | |
|
6027 | 0 | opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN, |
6028 | 0 | &expr_node, NULL); |
6029 | |
|
6030 | 0 | if (by_ref && expr_ast) { |
6031 | 0 | if (zend_is_call(expr_ast)) { |
6032 | 0 | opline->extended_value = ZEND_RETURNS_FUNCTION; |
6033 | 0 | } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) { |
6034 | 0 | opline->extended_value = ZEND_RETURNS_VALUE; |
6035 | 0 | } |
6036 | 0 | } |
6037 | 0 | } |
6038 | | /* }}} */ |
6039 | | |
6040 | | static void zend_compile_void_cast(znode *result, const zend_ast *ast) |
6041 | 0 | { |
6042 | 0 | zend_ast *expr_ast = ast->child[0]; |
6043 | 0 | znode expr_node; |
6044 | 0 | zend_op *opline; |
6045 | |
|
6046 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6047 | |
|
6048 | 0 | switch (expr_node.op_type) { |
6049 | 0 | case IS_TMP_VAR: |
6050 | 0 | case IS_VAR: |
6051 | 0 | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6052 | 0 | opline->extended_value = ZEND_FREE_VOID_CAST; |
6053 | 0 | break; |
6054 | 0 | case IS_CONST: |
6055 | 0 | zend_do_free(&expr_node); |
6056 | 0 | break; |
6057 | 0 | } |
6058 | 0 | } |
6059 | | |
6060 | | static void zend_compile_echo(const zend_ast *ast) /* {{{ */ |
6061 | 0 | { |
6062 | 0 | zend_op *opline; |
6063 | 0 | zend_ast *expr_ast = ast->child[0]; |
6064 | |
|
6065 | 0 | znode expr_node; |
6066 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6067 | |
|
6068 | 0 | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
6069 | 0 | opline->extended_value = 0; |
6070 | 0 | } |
6071 | | /* }}} */ |
6072 | | |
6073 | | static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */ |
6074 | 0 | { |
6075 | 0 | zend_ast *expr_ast = ast->child[0]; |
6076 | |
|
6077 | 0 | znode expr_node; |
6078 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6079 | |
|
6080 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL); |
6081 | 0 | if (result) { |
6082 | | /* Mark this as an "expression throw" for opcache. */ |
6083 | 0 | opline->extended_value = ZEND_THROW_IS_EXPR; |
6084 | 0 | result->op_type = IS_CONST; |
6085 | 0 | ZVAL_TRUE(&result->u.constant); |
6086 | 0 | } |
6087 | 0 | } |
6088 | | /* }}} */ |
6089 | | |
6090 | | static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */ |
6091 | 0 | { |
6092 | 0 | zend_ast *depth_ast = ast->child[0]; |
6093 | |
|
6094 | 0 | zend_op *opline; |
6095 | 0 | zend_long depth; |
6096 | |
|
6097 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE); |
6098 | |
|
6099 | 0 | if (depth_ast) { |
6100 | 0 | const zval *depth_zv; |
6101 | 0 | if (depth_ast->kind != ZEND_AST_ZVAL) { |
6102 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand " |
6103 | 0 | "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6104 | 0 | } |
6105 | | |
6106 | 0 | depth_zv = zend_ast_get_zval(depth_ast); |
6107 | 0 | if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) { |
6108 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers", |
6109 | 0 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6110 | 0 | } |
6111 | | |
6112 | 0 | depth = Z_LVAL_P(depth_zv); |
6113 | 0 | } else { |
6114 | 0 | depth = 1; |
6115 | 0 | } |
6116 | | |
6117 | 0 | if (CG(context).current_brk_cont == -1) { |
6118 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context", |
6119 | 0 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6120 | 0 | } else { |
6121 | 0 | if (!zend_handle_loops_and_finally_ex(depth, NULL)) { |
6122 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s", |
6123 | 0 | ast->kind == ZEND_AST_BREAK ? "break" : "continue", |
6124 | 0 | depth, depth == 1 ? "" : "s"); |
6125 | 0 | } |
6126 | 0 | } |
6127 | | |
6128 | 0 | if (ast->kind == ZEND_AST_CONTINUE) { |
6129 | 0 | int d, cur = CG(context).current_brk_cont; |
6130 | 0 | for (d = depth - 1; d > 0; d--) { |
6131 | 0 | cur = CG(context).brk_cont_array[cur].parent; |
6132 | 0 | ZEND_ASSERT(cur != -1); |
6133 | 0 | } |
6134 | |
|
6135 | 0 | if (CG(context).brk_cont_array[cur].is_switch) { |
6136 | 0 | if (depth == 1) { |
6137 | 0 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6138 | 0 | zend_error(E_WARNING, |
6139 | 0 | "\"continue\" targeting switch is equivalent to \"break\""); |
6140 | 0 | } else { |
6141 | 0 | zend_error(E_WARNING, |
6142 | 0 | "\"continue\" targeting switch is equivalent to \"break\". " \ |
6143 | 0 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6144 | 0 | depth + 1); |
6145 | 0 | } |
6146 | 0 | } else { |
6147 | 0 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6148 | 0 | zend_error(E_WARNING, |
6149 | 0 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"", |
6150 | 0 | depth, depth); |
6151 | 0 | } else { |
6152 | 0 | zend_error(E_WARNING, |
6153 | 0 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \ |
6154 | 0 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6155 | 0 | depth, depth, depth + 1); |
6156 | 0 | } |
6157 | 0 | } |
6158 | 0 | } |
6159 | 0 | } |
6160 | |
|
6161 | 0 | opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL); |
6162 | 0 | opline->op1.num = CG(context).current_brk_cont; |
6163 | 0 | opline->op2.num = depth; |
6164 | 0 | } |
6165 | | /* }}} */ |
6166 | | |
6167 | | void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */ |
6168 | 0 | { |
6169 | 0 | zend_label *dest; |
6170 | 0 | int remove_oplines = opline->op1.num; |
6171 | 0 | zval *label; |
6172 | 0 | uint32_t opnum = opline - op_array->opcodes; |
6173 | |
|
6174 | 0 | label = CT_CONSTANT_EX(op_array, opline->op2.constant); |
6175 | 0 | if (CG(context).labels == NULL || |
6176 | 0 | (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL |
6177 | 0 | ) { |
6178 | 0 | CG(in_compilation) = 1; |
6179 | 0 | CG(active_op_array) = op_array; |
6180 | 0 | CG(zend_lineno) = opline->lineno; |
6181 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label)); |
6182 | 0 | } |
6183 | | |
6184 | 0 | zval_ptr_dtor_str(label); |
6185 | 0 | ZVAL_NULL(label); |
6186 | |
|
6187 | 0 | uint32_t current = opline->extended_value; |
6188 | 0 | for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) { |
6189 | 0 | if (current == -1) { |
6190 | 0 | CG(in_compilation) = 1; |
6191 | 0 | CG(active_op_array) = op_array; |
6192 | 0 | CG(zend_lineno) = opline->lineno; |
6193 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed"); |
6194 | 0 | } |
6195 | 0 | if (CG(context).brk_cont_array[current].start >= 0) { |
6196 | 0 | remove_oplines--; |
6197 | 0 | } |
6198 | 0 | } |
6199 | | |
6200 | 0 | for (current = 0; current < op_array->last_try_catch; ++current) { |
6201 | 0 | const zend_try_catch_element *elem = &op_array->try_catch_array[current]; |
6202 | 0 | if (elem->try_op > opnum) { |
6203 | 0 | break; |
6204 | 0 | } |
6205 | 0 | if (elem->finally_op && opnum < elem->finally_op - 1 |
6206 | 0 | && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op) |
6207 | 0 | ) { |
6208 | 0 | remove_oplines--; |
6209 | 0 | } |
6210 | 0 | } |
6211 | |
|
6212 | 0 | opline->opcode = ZEND_JMP; |
6213 | 0 | SET_UNUSED(opline->op1); |
6214 | 0 | SET_UNUSED(opline->op2); |
6215 | 0 | SET_UNUSED(opline->result); |
6216 | 0 | opline->op1.opline_num = dest->opline_num; |
6217 | 0 | opline->extended_value = 0; |
6218 | |
|
6219 | 0 | ZEND_ASSERT(remove_oplines >= 0); |
6220 | 0 | while (remove_oplines--) { |
6221 | 0 | opline--; |
6222 | 0 | MAKE_NOP(opline); |
6223 | 0 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
6224 | 0 | } |
6225 | 0 | } |
6226 | | /* }}} */ |
6227 | | |
6228 | | static void zend_compile_goto(const zend_ast *ast) /* {{{ */ |
6229 | 0 | { |
6230 | 0 | zend_ast *label_ast = ast->child[0]; |
6231 | 0 | znode label_node; |
6232 | 0 | zend_op *opline; |
6233 | |
|
6234 | 0 | zend_compile_expr(&label_node, label_ast); |
6235 | | |
6236 | | /* Label resolution and unwinding adjustments happen in pass two. */ |
6237 | 0 | uint32_t opnum_start = get_next_op_number(); |
6238 | 0 | zend_handle_loops_and_finally(NULL); |
6239 | 0 | opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node); |
6240 | 0 | opline->op1.num = get_next_op_number() - opnum_start - 1; |
6241 | 0 | opline->extended_value = CG(context).current_brk_cont; |
6242 | 0 | } |
6243 | | /* }}} */ |
6244 | | |
6245 | | static void zend_compile_label(const zend_ast *ast) /* {{{ */ |
6246 | 0 | { |
6247 | 0 | zend_string *label = zend_ast_get_str(ast->child[0]); |
6248 | 0 | zend_label dest; |
6249 | |
|
6250 | 0 | if (!CG(context).labels) { |
6251 | 0 | ALLOC_HASHTABLE(CG(context).labels); |
6252 | 0 | zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0); |
6253 | 0 | } |
6254 | |
|
6255 | 0 | dest.brk_cont = CG(context).current_brk_cont; |
6256 | 0 | dest.opline_num = get_next_op_number(); |
6257 | |
|
6258 | 0 | if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) { |
6259 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label)); |
6260 | 0 | } |
6261 | 0 | } |
6262 | | /* }}} */ |
6263 | | |
6264 | | static void zend_compile_while(const zend_ast *ast) /* {{{ */ |
6265 | 0 | { |
6266 | 0 | zend_ast *cond_ast = ast->child[0]; |
6267 | 0 | zend_ast *stmt_ast = ast->child[1]; |
6268 | 0 | znode cond_node; |
6269 | 0 | uint32_t opnum_start, opnum_jmp, opnum_cond; |
6270 | |
|
6271 | 0 | opnum_jmp = zend_emit_jump(0); |
6272 | |
|
6273 | 0 | zend_begin_loop(ZEND_NOP, NULL, false); |
6274 | |
|
6275 | 0 | opnum_start = get_next_op_number(); |
6276 | 0 | zend_compile_stmt(stmt_ast); |
6277 | |
|
6278 | 0 | opnum_cond = get_next_op_number(); |
6279 | 0 | zend_update_jump_target(opnum_jmp, opnum_cond); |
6280 | 0 | zend_compile_expr(&cond_node, cond_ast); |
6281 | |
|
6282 | 0 | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6283 | |
|
6284 | 0 | zend_end_loop(opnum_cond, NULL); |
6285 | 0 | } |
6286 | | /* }}} */ |
6287 | | |
6288 | | static void zend_compile_do_while(const zend_ast *ast) /* {{{ */ |
6289 | 0 | { |
6290 | 0 | zend_ast *stmt_ast = ast->child[0]; |
6291 | 0 | zend_ast *cond_ast = ast->child[1]; |
6292 | |
|
6293 | 0 | znode cond_node; |
6294 | 0 | uint32_t opnum_start, opnum_cond; |
6295 | |
|
6296 | 0 | zend_begin_loop(ZEND_NOP, NULL, false); |
6297 | |
|
6298 | 0 | opnum_start = get_next_op_number(); |
6299 | 0 | zend_compile_stmt(stmt_ast); |
6300 | |
|
6301 | 0 | opnum_cond = get_next_op_number(); |
6302 | 0 | zend_compile_expr(&cond_node, cond_ast); |
6303 | |
|
6304 | 0 | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6305 | |
|
6306 | 0 | zend_end_loop(opnum_cond, NULL); |
6307 | 0 | } |
6308 | | /* }}} */ |
6309 | | |
6310 | | static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */ |
6311 | 0 | { |
6312 | 0 | const zend_ast_list *list; |
6313 | 0 | uint32_t i; |
6314 | |
|
6315 | 0 | result->op_type = IS_CONST; |
6316 | 0 | ZVAL_TRUE(&result->u.constant); |
6317 | |
|
6318 | 0 | if (!ast) { |
6319 | 0 | return; |
6320 | 0 | } |
6321 | | |
6322 | 0 | list = zend_ast_get_list(ast); |
6323 | 0 | for (i = 0; i < list->children; ++i) { |
6324 | 0 | zend_ast *expr_ast = list->child[i]; |
6325 | |
|
6326 | 0 | zend_do_free(result); |
6327 | 0 | if (expr_ast->kind == ZEND_AST_CAST_VOID) { |
6328 | 0 | zend_compile_void_cast(NULL, expr_ast); |
6329 | 0 | result->op_type = IS_CONST; |
6330 | 0 | ZVAL_NULL(&result->u.constant); |
6331 | 0 | } else { |
6332 | 0 | zend_compile_expr(result, expr_ast); |
6333 | 0 | } |
6334 | 0 | } |
6335 | 0 | } |
6336 | | /* }}} */ |
6337 | | |
6338 | | static void zend_compile_for(const zend_ast *ast) /* {{{ */ |
6339 | 0 | { |
6340 | 0 | zend_ast *init_ast = ast->child[0]; |
6341 | 0 | zend_ast *cond_ast = ast->child[1]; |
6342 | 0 | zend_ast *loop_ast = ast->child[2]; |
6343 | 0 | zend_ast *stmt_ast = ast->child[3]; |
6344 | |
|
6345 | 0 | znode result; |
6346 | 0 | uint32_t opnum_start, opnum_jmp, opnum_loop; |
6347 | |
|
6348 | 0 | zend_compile_for_expr_list(&result, init_ast); |
6349 | 0 | zend_do_free(&result); |
6350 | |
|
6351 | 0 | opnum_jmp = zend_emit_jump(0); |
6352 | |
|
6353 | 0 | zend_begin_loop(ZEND_NOP, NULL, false); |
6354 | |
|
6355 | 0 | opnum_start = get_next_op_number(); |
6356 | 0 | zend_compile_stmt(stmt_ast); |
6357 | |
|
6358 | 0 | opnum_loop = get_next_op_number(); |
6359 | 0 | zend_compile_for_expr_list(&result, loop_ast); |
6360 | 0 | zend_do_free(&result); |
6361 | |
|
6362 | 0 | zend_update_jump_target_to_next(opnum_jmp); |
6363 | 0 | zend_compile_for_expr_list(&result, cond_ast); |
6364 | 0 | zend_do_extended_stmt(NULL); |
6365 | |
|
6366 | 0 | zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start); |
6367 | |
|
6368 | 0 | zend_end_loop(opnum_loop, NULL); |
6369 | 0 | } |
6370 | | /* }}} */ |
6371 | | |
6372 | | static void zend_compile_foreach(zend_ast *ast) /* {{{ */ |
6373 | 0 | { |
6374 | 0 | zend_ast *expr_ast = ast->child[0]; |
6375 | 0 | zend_ast *value_ast = ast->child[1]; |
6376 | 0 | zend_ast *key_ast = ast->child[2]; |
6377 | 0 | zend_ast *stmt_ast = ast->child[3]; |
6378 | 0 | bool by_ref = value_ast->kind == ZEND_AST_REF; |
6379 | 0 | bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast)) |
6380 | 0 | || zend_is_call(expr_ast); |
6381 | |
|
6382 | 0 | znode expr_node, reset_node, value_node, key_node; |
6383 | 0 | zend_op *opline; |
6384 | 0 | uint32_t opnum_reset, opnum_fetch; |
6385 | |
|
6386 | 0 | if (key_ast) { |
6387 | 0 | if (key_ast->kind == ZEND_AST_REF) { |
6388 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference"); |
6389 | 0 | } |
6390 | 0 | if (key_ast->kind == ZEND_AST_ARRAY) { |
6391 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element"); |
6392 | 0 | } |
6393 | 0 | } |
6394 | | |
6395 | 0 | if (by_ref) { |
6396 | 0 | value_ast = value_ast->child[0]; |
6397 | 0 | } |
6398 | |
|
6399 | 0 | if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) { |
6400 | 0 | by_ref = true; |
6401 | 0 | } |
6402 | |
|
6403 | 0 | if (by_ref && is_variable) { |
6404 | 0 | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
6405 | 0 | } else { |
6406 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6407 | 0 | } |
6408 | |
|
6409 | 0 | if (by_ref) { |
6410 | 0 | zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W); |
6411 | 0 | } |
6412 | |
|
6413 | 0 | opnum_reset = get_next_op_number(); |
6414 | 0 | opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL); |
6415 | 0 | if (!by_ref) { |
6416 | 0 | opline->result_type = IS_TMP_VAR; |
6417 | 0 | reset_node.op_type = IS_TMP_VAR; |
6418 | 0 | } |
6419 | |
|
6420 | 0 | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
6421 | |
|
6422 | 0 | opnum_fetch = get_next_op_number(); |
6423 | 0 | opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL); |
6424 | |
|
6425 | 0 | if (is_this_fetch(value_ast)) { |
6426 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
6427 | 0 | } else if (value_ast->kind == ZEND_AST_VAR && |
6428 | 0 | zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) { |
6429 | 0 | SET_NODE(opline->op2, &value_node); |
6430 | 0 | } else { |
6431 | 0 | opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR; |
6432 | 0 | opline->op2.var = get_temporary_variable(); |
6433 | 0 | GET_NODE(&value_node, opline->op2); |
6434 | 0 | if (value_ast->kind == ZEND_AST_ARRAY) { |
6435 | 0 | zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R); |
6436 | 0 | } else if (by_ref) { |
6437 | 0 | zend_emit_assign_ref_znode(value_ast, &value_node); |
6438 | 0 | } else { |
6439 | 0 | zend_emit_assign_znode(value_ast, &value_node); |
6440 | 0 | } |
6441 | 0 | } |
6442 | | |
6443 | 0 | if (key_ast) { |
6444 | 0 | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6445 | 0 | zend_make_tmp_result(&key_node, opline); |
6446 | 0 | zend_emit_assign_znode(key_ast, &key_node); |
6447 | 0 | } |
6448 | |
|
6449 | 0 | zend_compile_stmt(stmt_ast); |
6450 | | |
6451 | | /* Place JMP and FE_FREE on the line where foreach starts. It would be |
6452 | | * better to use the end line, but this information is not available |
6453 | | * currently. */ |
6454 | 0 | CG(zend_lineno) = ast->lineno; |
6455 | 0 | zend_emit_jump(opnum_fetch); |
6456 | |
|
6457 | 0 | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
6458 | 0 | opline->op2.opline_num = get_next_op_number(); |
6459 | |
|
6460 | 0 | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6461 | 0 | opline->extended_value = get_next_op_number(); |
6462 | |
|
6463 | 0 | zend_end_loop(opnum_fetch, &reset_node); |
6464 | |
|
6465 | 0 | opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
6466 | 0 | } |
6467 | | /* }}} */ |
6468 | | |
6469 | | static void zend_compile_if(zend_ast *ast) /* {{{ */ |
6470 | 0 | { |
6471 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
6472 | 0 | uint32_t i; |
6473 | 0 | uint32_t *jmp_opnums = NULL; |
6474 | |
|
6475 | 0 | if (list->children > 1) { |
6476 | 0 | jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0); |
6477 | 0 | } |
6478 | |
|
6479 | 0 | for (i = 0; i < list->children; ++i) { |
6480 | 0 | const zend_ast *elem_ast = list->child[i]; |
6481 | 0 | zend_ast *cond_ast = elem_ast->child[0]; |
6482 | 0 | zend_ast *stmt_ast = elem_ast->child[1]; |
6483 | |
|
6484 | 0 | if (cond_ast) { |
6485 | 0 | znode cond_node; |
6486 | 0 | uint32_t opnum_jmpz; |
6487 | |
|
6488 | 0 | if (i > 0) { |
6489 | 0 | CG(zend_lineno) = cond_ast->lineno; |
6490 | 0 | zend_do_extended_stmt(NULL); |
6491 | 0 | } |
6492 | |
|
6493 | 0 | zend_compile_expr(&cond_node, cond_ast); |
6494 | 0 | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6495 | |
|
6496 | 0 | zend_compile_stmt(stmt_ast); |
6497 | |
|
6498 | 0 | if (i != list->children - 1) { |
6499 | | /* Set the lineno of JMP to the position of the if keyword, as we don't want to |
6500 | | * report the last line in the if branch as covered if it hasn't actually executed. */ |
6501 | 0 | CG(zend_lineno) = elem_ast->lineno; |
6502 | 0 | jmp_opnums[i] = zend_emit_jump(0); |
6503 | 0 | } |
6504 | 0 | zend_update_jump_target_to_next(opnum_jmpz); |
6505 | 0 | } else { |
6506 | | /* "else" can only occur as last element. */ |
6507 | 0 | ZEND_ASSERT(i == list->children - 1); |
6508 | 0 | zend_compile_stmt(stmt_ast); |
6509 | 0 | } |
6510 | 0 | } |
6511 | |
|
6512 | 0 | if (list->children > 1) { |
6513 | 0 | for (i = 0; i < list->children - 1; ++i) { |
6514 | 0 | zend_update_jump_target_to_next(jmp_opnums[i]); |
6515 | 0 | } |
6516 | 0 | efree(jmp_opnums); |
6517 | 0 | } |
6518 | 0 | } |
6519 | | /* }}} */ |
6520 | | |
6521 | 0 | static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) { |
6522 | 0 | uint32_t i; |
6523 | 0 | uint8_t common_type = IS_UNDEF; |
6524 | 0 | for (i = 0; i < cases->children; i++) { |
6525 | 0 | zend_ast *case_ast = cases->child[i]; |
6526 | 0 | zend_ast **cond_ast = &case_ast->child[0]; |
6527 | 0 | const zval *cond_zv; |
6528 | 0 | if (!case_ast->child[0]) { |
6529 | | /* Skip default clause */ |
6530 | 0 | continue; |
6531 | 0 | } |
6532 | | |
6533 | 0 | zend_eval_const_expr(cond_ast); |
6534 | 0 | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6535 | | /* Non-constant case */ |
6536 | 0 | return IS_UNDEF; |
6537 | 0 | } |
6538 | | |
6539 | 0 | cond_zv = zend_ast_get_zval(case_ast->child[0]); |
6540 | 0 | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6541 | | /* We only optimize switched on integers and strings */ |
6542 | 0 | return IS_UNDEF; |
6543 | 0 | } |
6544 | | |
6545 | 0 | if (common_type == IS_UNDEF) { |
6546 | 0 | common_type = Z_TYPE_P(cond_zv); |
6547 | 0 | } else if (common_type != Z_TYPE_P(cond_zv)) { |
6548 | | /* Non-uniform case types */ |
6549 | 0 | return IS_UNDEF; |
6550 | 0 | } |
6551 | | |
6552 | 0 | if (Z_TYPE_P(cond_zv) == IS_STRING |
6553 | 0 | && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) { |
6554 | | /* Numeric strings cannot be compared with a simple hash lookup */ |
6555 | 0 | return IS_UNDEF; |
6556 | 0 | } |
6557 | 0 | } |
6558 | | |
6559 | 0 | return common_type; |
6560 | 0 | } |
6561 | | |
6562 | 0 | static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) { |
6563 | 0 | if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) { |
6564 | 0 | return false; |
6565 | 0 | } |
6566 | | |
6567 | | /* Thresholds are chosen based on when the average switch time for equidistributed |
6568 | | * input becomes smaller when using the jumptable optimization. */ |
6569 | 0 | if (jumptable_type == IS_LONG) { |
6570 | 0 | return cases->children >= 5; |
6571 | 0 | } else { |
6572 | 0 | ZEND_ASSERT(jumptable_type == IS_STRING); |
6573 | 0 | return cases->children >= 2; |
6574 | 0 | } |
6575 | 0 | } |
6576 | | |
6577 | | static void zend_compile_switch(zend_ast *ast) /* {{{ */ |
6578 | 0 | { |
6579 | 0 | zend_ast *expr_ast = ast->child[0]; |
6580 | 0 | zend_ast_list *cases = zend_ast_get_list(ast->child[1]); |
6581 | |
|
6582 | 0 | uint32_t i; |
6583 | 0 | bool has_default_case = false; |
6584 | |
|
6585 | 0 | znode expr_node, case_node; |
6586 | 0 | zend_op *opline; |
6587 | 0 | uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1; |
6588 | 0 | uint8_t jumptable_type; |
6589 | 0 | HashTable *jumptable = NULL; |
6590 | |
|
6591 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6592 | |
|
6593 | 0 | zend_begin_loop(ZEND_FREE, &expr_node, true); |
6594 | |
|
6595 | 0 | case_node.op_type = IS_TMP_VAR; |
6596 | 0 | case_node.u.op.var = get_temporary_variable(); |
6597 | |
|
6598 | 0 | jumptable_type = determine_switch_jumptable_type(cases); |
6599 | 0 | if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) { |
6600 | 0 | znode jumptable_op; |
6601 | |
|
6602 | 0 | ALLOC_HASHTABLE(jumptable); |
6603 | 0 | zend_hash_init(jumptable, cases->children, NULL, NULL, 0); |
6604 | 0 | jumptable_op.op_type = IS_CONST; |
6605 | 0 | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
6606 | |
|
6607 | 0 | opline = zend_emit_op(NULL, |
6608 | 0 | jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING, |
6609 | 0 | &expr_node, &jumptable_op); |
6610 | 0 | if (opline->op1_type == IS_CONST) { |
6611 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6612 | 0 | } |
6613 | 0 | opnum_switch = opline - CG(active_op_array)->opcodes; |
6614 | 0 | } |
6615 | |
|
6616 | 0 | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0); |
6617 | 0 | for (i = 0; i < cases->children; ++i) { |
6618 | 0 | zend_ast *case_ast = cases->child[i]; |
6619 | 0 | zend_ast *cond_ast = case_ast->child[0]; |
6620 | 0 | znode cond_node; |
6621 | |
|
6622 | 0 | if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) { |
6623 | 0 | CG(zend_lineno) = case_ast->lineno; |
6624 | 0 | zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead"); |
6625 | 0 | } |
6626 | |
|
6627 | 0 | if (!cond_ast) { |
6628 | 0 | if (has_default_case) { |
6629 | 0 | CG(zend_lineno) = case_ast->lineno; |
6630 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
6631 | 0 | "Switch statements may only contain one default clause"); |
6632 | 0 | } |
6633 | 0 | has_default_case = true; |
6634 | 0 | continue; |
6635 | 0 | } |
6636 | | |
6637 | 0 | zend_compile_expr(&cond_node, cond_ast); |
6638 | |
|
6639 | 0 | if (expr_node.op_type == IS_CONST |
6640 | 0 | && Z_TYPE(expr_node.u.constant) == IS_FALSE) { |
6641 | 0 | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6642 | 0 | } else if (expr_node.op_type == IS_CONST |
6643 | 0 | && Z_TYPE(expr_node.u.constant) == IS_TRUE) { |
6644 | 0 | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0); |
6645 | 0 | } else { |
6646 | 0 | opline = zend_emit_op(NULL, |
6647 | 0 | (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL, |
6648 | 0 | &expr_node, &cond_node); |
6649 | 0 | SET_NODE(opline->result, &case_node); |
6650 | 0 | if (opline->op1_type == IS_CONST) { |
6651 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6652 | 0 | } |
6653 | |
|
6654 | 0 | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
6655 | 0 | } |
6656 | 0 | } |
6657 | | |
6658 | 0 | opnum_default_jmp = zend_emit_jump(0); |
6659 | |
|
6660 | 0 | for (i = 0; i < cases->children; ++i) { |
6661 | 0 | zend_ast *case_ast = cases->child[i]; |
6662 | 0 | zend_ast *cond_ast = case_ast->child[0]; |
6663 | 0 | zend_ast *stmt_ast = case_ast->child[1]; |
6664 | |
|
6665 | 0 | if (cond_ast) { |
6666 | 0 | zend_update_jump_target_to_next(jmpnz_opnums[i]); |
6667 | |
|
6668 | 0 | if (jumptable) { |
6669 | 0 | zval *cond_zv = zend_ast_get_zval(cond_ast); |
6670 | 0 | zval jmp_target; |
6671 | 0 | ZVAL_LONG(&jmp_target, get_next_op_number()); |
6672 | |
|
6673 | 0 | ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type); |
6674 | 0 | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
6675 | 0 | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
6676 | 0 | } else { |
6677 | 0 | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
6678 | 0 | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
6679 | 0 | } |
6680 | 0 | } |
6681 | 0 | } else { |
6682 | 0 | zend_update_jump_target_to_next(opnum_default_jmp); |
6683 | |
|
6684 | 0 | if (jumptable) { |
6685 | 0 | ZEND_ASSERT(opnum_switch != (uint32_t)-1); |
6686 | 0 | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6687 | 0 | opline->extended_value = get_next_op_number(); |
6688 | 0 | } |
6689 | 0 | } |
6690 | |
|
6691 | 0 | zend_compile_stmt(stmt_ast); |
6692 | 0 | } |
6693 | |
|
6694 | 0 | if (!has_default_case) { |
6695 | 0 | zend_update_jump_target_to_next(opnum_default_jmp); |
6696 | |
|
6697 | 0 | if (jumptable) { |
6698 | 0 | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6699 | 0 | opline->extended_value = get_next_op_number(); |
6700 | 0 | } |
6701 | 0 | } |
6702 | |
|
6703 | 0 | zend_end_loop(get_next_op_number(), &expr_node); |
6704 | |
|
6705 | 0 | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
6706 | 0 | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6707 | 0 | opline->extended_value = ZEND_FREE_SWITCH; |
6708 | 0 | } else if (expr_node.op_type == IS_CONST) { |
6709 | 0 | zval_ptr_dtor_nogc(&expr_node.u.constant); |
6710 | 0 | } |
6711 | |
|
6712 | 0 | efree(jmpnz_opnums); |
6713 | 0 | } |
6714 | | /* }}} */ |
6715 | | |
6716 | | static uint32_t count_match_conds(const zend_ast_list *arms) |
6717 | 0 | { |
6718 | 0 | uint32_t num_conds = 0; |
6719 | |
|
6720 | 0 | for (uint32_t i = 0; i < arms->children; i++) { |
6721 | 0 | const zend_ast *arm_ast = arms->child[i]; |
6722 | 0 | if (arm_ast->child[0] == NULL) { |
6723 | 0 | continue; |
6724 | 0 | } |
6725 | | |
6726 | 0 | const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6727 | 0 | num_conds += conds->children; |
6728 | 0 | } |
6729 | |
|
6730 | 0 | return num_conds; |
6731 | 0 | } |
6732 | | |
6733 | 0 | static bool can_match_use_jumptable(const zend_ast_list *arms) { |
6734 | 0 | for (uint32_t i = 0; i < arms->children; i++) { |
6735 | 0 | const zend_ast *arm_ast = arms->child[i]; |
6736 | 0 | if (!arm_ast->child[0]) { |
6737 | | /* Skip default arm */ |
6738 | 0 | continue; |
6739 | 0 | } |
6740 | | |
6741 | 0 | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6742 | 0 | for (uint32_t j = 0; j < conds->children; j++) { |
6743 | 0 | zend_ast **cond_ast = &conds->child[j]; |
6744 | |
|
6745 | 0 | zend_eval_const_expr(cond_ast); |
6746 | 0 | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6747 | 0 | return false; |
6748 | 0 | } |
6749 | | |
6750 | 0 | const zval *cond_zv = zend_ast_get_zval(*cond_ast); |
6751 | 0 | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6752 | 0 | return false; |
6753 | 0 | } |
6754 | 0 | } |
6755 | 0 | } |
6756 | | |
6757 | 0 | return true; |
6758 | 0 | } |
6759 | | |
6760 | | static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast) |
6761 | 0 | { |
6762 | 0 | if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) { |
6763 | | /* Assert compilation adds a message operand, but this is incompatible with the |
6764 | | * pipe optimization that uses a temporary znode for the reference elimination. |
6765 | | * Therefore, disable the optimization for assert. |
6766 | | * Note that "assert" as a name is always treated as fully qualified. */ |
6767 | 0 | return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert"); |
6768 | 0 | } |
6769 | | |
6770 | 0 | return true; |
6771 | 0 | } |
6772 | | |
6773 | | static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type) |
6774 | 0 | { |
6775 | 0 | zend_ast *operand_ast = ast->child[0]; |
6776 | 0 | zend_ast *callable_ast = ast->child[1]; |
6777 | |
|
6778 | 0 | if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) { |
6779 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized"); |
6780 | 0 | } |
6781 | | |
6782 | | /* Compile the left hand side down to a value first. */ |
6783 | 0 | znode operand_result; |
6784 | 0 | zend_compile_expr(&operand_result, operand_ast); |
6785 | | |
6786 | | /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references |
6787 | | * always fail. They will already fail in complex cases like arrays, |
6788 | | * so those don't need a wrapper. */ |
6789 | 0 | znode wrapped_operand_result; |
6790 | 0 | if (operand_result.op_type & (IS_CV|IS_VAR)) { |
6791 | 0 | zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL); |
6792 | 0 | } else { |
6793 | 0 | wrapped_operand_result = operand_result; |
6794 | 0 | } |
6795 | | |
6796 | | /* Turn the operand into a function parameter list. */ |
6797 | 0 | zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, zend_ast_create_znode(&wrapped_operand_result)); |
6798 | |
|
6799 | 0 | zend_ast *fcall_ast; |
6800 | 0 | znode callable_result; |
6801 | | |
6802 | | /* Turn $foo |> bar(...) into bar($foo). */ |
6803 | 0 | if (callable_ast->kind == ZEND_AST_CALL |
6804 | 0 | && callable_ast->child[1]->kind == ZEND_AST_CALLABLE_CONVERT |
6805 | 0 | && zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) { |
6806 | 0 | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
6807 | 0 | callable_ast->child[0], arg_list_ast); |
6808 | | /* Turn $foo |> bar::baz(...) into bar::baz($foo). */ |
6809 | 0 | } else if (callable_ast->kind == ZEND_AST_STATIC_CALL |
6810 | 0 | && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) { |
6811 | 0 | fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL, |
6812 | 0 | callable_ast->child[0], callable_ast->child[1], arg_list_ast); |
6813 | | /* Turn $foo |> $bar->baz(...) into $bar->baz($foo). */ |
6814 | 0 | } else if (callable_ast->kind == ZEND_AST_METHOD_CALL |
6815 | 0 | && callable_ast->child[2]->kind == ZEND_AST_CALLABLE_CONVERT) { |
6816 | 0 | fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL, |
6817 | 0 | callable_ast->child[0], callable_ast->child[1], arg_list_ast); |
6818 | | /* Turn $foo |> $expr into ($expr)($foo) */ |
6819 | 0 | } else { |
6820 | 0 | zend_compile_expr(&callable_result, callable_ast); |
6821 | 0 | callable_ast = zend_ast_create_znode(&callable_result); |
6822 | 0 | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
6823 | 0 | callable_ast, arg_list_ast); |
6824 | 0 | } |
6825 | |
|
6826 | 0 | zend_do_extended_stmt(&operand_result); |
6827 | |
|
6828 | 0 | zend_compile_var(result, fcall_ast, type, /* by_ref */ false); |
6829 | 0 | } |
6830 | | |
6831 | | static void zend_compile_match(znode *result, zend_ast *ast) |
6832 | 0 | { |
6833 | 0 | zend_ast *expr_ast = ast->child[0]; |
6834 | 0 | zend_ast_list *arms = zend_ast_get_list(ast->child[1]); |
6835 | 0 | bool has_default_arm = false; |
6836 | 0 | uint32_t opnum_match = (uint32_t)-1; |
6837 | |
|
6838 | 0 | znode expr_node; |
6839 | 0 | zend_compile_expr(&expr_node, expr_ast); |
6840 | |
|
6841 | 0 | znode case_node; |
6842 | 0 | case_node.op_type = IS_TMP_VAR; |
6843 | 0 | case_node.u.op.var = get_temporary_variable(); |
6844 | |
|
6845 | 0 | uint32_t num_conds = count_match_conds(arms); |
6846 | 0 | uint8_t can_use_jumptable = can_match_use_jumptable(arms); |
6847 | 0 | bool uses_jumptable = can_use_jumptable && num_conds >= 2; |
6848 | 0 | HashTable *jumptable = NULL; |
6849 | 0 | uint32_t *jmpnz_opnums = NULL; |
6850 | |
|
6851 | 0 | for (uint32_t i = 0; i < arms->children; ++i) { |
6852 | 0 | zend_ast *arm_ast = arms->child[i]; |
6853 | |
|
6854 | 0 | if (!arm_ast->child[0]) { |
6855 | 0 | if (has_default_arm) { |
6856 | 0 | CG(zend_lineno) = arm_ast->lineno; |
6857 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
6858 | 0 | "Match expressions may only contain one default arm"); |
6859 | 0 | } |
6860 | 0 | has_default_arm = true; |
6861 | 0 | } |
6862 | 0 | } |
6863 | | |
6864 | 0 | if (uses_jumptable) { |
6865 | 0 | znode jumptable_op; |
6866 | |
|
6867 | 0 | ALLOC_HASHTABLE(jumptable); |
6868 | 0 | zend_hash_init(jumptable, num_conds, NULL, NULL, 0); |
6869 | 0 | jumptable_op.op_type = IS_CONST; |
6870 | 0 | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
6871 | |
|
6872 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op); |
6873 | 0 | if (opline->op1_type == IS_CONST) { |
6874 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6875 | 0 | } |
6876 | 0 | opnum_match = opline - CG(active_op_array)->opcodes; |
6877 | 0 | } else { |
6878 | 0 | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0); |
6879 | 0 | uint32_t cond_count = 0; |
6880 | 0 | for (uint32_t i = 0; i < arms->children; ++i) { |
6881 | 0 | zend_ast *arm_ast = arms->child[i]; |
6882 | |
|
6883 | 0 | if (!arm_ast->child[0]) { |
6884 | 0 | continue; |
6885 | 0 | } |
6886 | | |
6887 | 0 | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6888 | 0 | for (uint32_t j = 0; j < conds->children; j++) { |
6889 | 0 | zend_ast *cond_ast = conds->child[j]; |
6890 | |
|
6891 | 0 | znode cond_node; |
6892 | 0 | zend_compile_expr(&cond_node, cond_ast); |
6893 | |
|
6894 | 0 | uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL; |
6895 | 0 | zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node); |
6896 | 0 | SET_NODE(opline->result, &case_node); |
6897 | 0 | if (opline->op1_type == IS_CONST) { |
6898 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6899 | 0 | } |
6900 | |
|
6901 | 0 | jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
6902 | |
|
6903 | 0 | cond_count++; |
6904 | 0 | } |
6905 | 0 | } |
6906 | 0 | } |
6907 | |
|
6908 | 0 | uint32_t opnum_default_jmp = 0; |
6909 | 0 | if (!uses_jumptable) { |
6910 | 0 | opnum_default_jmp = zend_emit_jump(0); |
6911 | 0 | } |
6912 | |
|
6913 | 0 | bool is_first_case = true; |
6914 | 0 | uint32_t cond_count = 0; |
6915 | 0 | uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0); |
6916 | | |
6917 | | // The generated default arm is emitted first to avoid live range issues where the tmpvar |
6918 | | // for the arm result is freed even though it has not been initialized yet. |
6919 | 0 | if (!has_default_arm) { |
6920 | 0 | if (!uses_jumptable) { |
6921 | 0 | zend_update_jump_target_to_next(opnum_default_jmp); |
6922 | 0 | } |
6923 | |
|
6924 | 0 | if (jumptable) { |
6925 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
6926 | 0 | opline->extended_value = get_next_op_number(); |
6927 | 0 | } |
6928 | |
|
6929 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL); |
6930 | 0 | if (opline->op1_type == IS_CONST) { |
6931 | 0 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6932 | 0 | } |
6933 | 0 | if (arms->children == 0) { |
6934 | | /* Mark this as an "expression throw" for opcache. */ |
6935 | 0 | opline->extended_value = ZEND_THROW_IS_EXPR; |
6936 | 0 | } |
6937 | 0 | } |
6938 | |
|
6939 | 0 | for (uint32_t i = 0; i < arms->children; ++i) { |
6940 | 0 | zend_ast *arm_ast = arms->child[i]; |
6941 | 0 | zend_ast *body_ast = arm_ast->child[1]; |
6942 | |
|
6943 | 0 | if (arm_ast->child[0] != NULL) { |
6944 | 0 | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6945 | |
|
6946 | 0 | for (uint32_t j = 0; j < conds->children; j++) { |
6947 | 0 | zend_ast *cond_ast = conds->child[j]; |
6948 | |
|
6949 | 0 | if (jmpnz_opnums != NULL) { |
6950 | 0 | zend_update_jump_target_to_next(jmpnz_opnums[cond_count]); |
6951 | 0 | } |
6952 | |
|
6953 | 0 | if (jumptable) { |
6954 | 0 | zval *cond_zv = zend_ast_get_zval(cond_ast); |
6955 | 0 | zval jmp_target; |
6956 | 0 | ZVAL_LONG(&jmp_target, get_next_op_number()); |
6957 | |
|
6958 | 0 | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
6959 | 0 | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
6960 | 0 | } else { |
6961 | 0 | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
6962 | 0 | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
6963 | 0 | } |
6964 | 0 | } |
6965 | |
|
6966 | 0 | cond_count++; |
6967 | 0 | } |
6968 | 0 | } else { |
6969 | 0 | if (!uses_jumptable) { |
6970 | 0 | zend_update_jump_target_to_next(opnum_default_jmp); |
6971 | 0 | } |
6972 | |
|
6973 | 0 | if (jumptable) { |
6974 | 0 | ZEND_ASSERT(opnum_match != (uint32_t)-1); |
6975 | 0 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
6976 | 0 | opline->extended_value = get_next_op_number(); |
6977 | 0 | } |
6978 | 0 | } |
6979 | |
|
6980 | 0 | znode body_node; |
6981 | 0 | zend_compile_expr(&body_node, body_ast); |
6982 | |
|
6983 | 0 | if (is_first_case) { |
6984 | 0 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL); |
6985 | 0 | is_first_case = false; |
6986 | 0 | } else { |
6987 | 0 | zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL); |
6988 | 0 | SET_NODE(opline_qm_assign->result, result); |
6989 | 0 | } |
6990 | |
|
6991 | 0 | jmp_end_opnums[i] = zend_emit_jump(0); |
6992 | 0 | } |
6993 | | |
6994 | | // Initialize result in case there is no arm |
6995 | 0 | if (arms->children == 0) { |
6996 | 0 | result->op_type = IS_CONST; |
6997 | 0 | ZVAL_NULL(&result->u.constant); |
6998 | 0 | } |
6999 | |
|
7000 | 0 | for (uint32_t i = 0; i < arms->children; ++i) { |
7001 | 0 | zend_update_jump_target_to_next(jmp_end_opnums[i]); |
7002 | 0 | } |
7003 | |
|
7004 | 0 | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
7005 | 0 | zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
7006 | 0 | opline->extended_value = ZEND_FREE_SWITCH; |
7007 | 0 | } else if (expr_node.op_type == IS_CONST) { |
7008 | 0 | zval_ptr_dtor_nogc(&expr_node.u.constant); |
7009 | 0 | } |
7010 | |
|
7011 | 0 | if (jmpnz_opnums != NULL) { |
7012 | 0 | efree(jmpnz_opnums); |
7013 | 0 | } |
7014 | 0 | efree(jmp_end_opnums); |
7015 | 0 | } |
7016 | | |
7017 | | static void zend_compile_try(const zend_ast *ast) /* {{{ */ |
7018 | 0 | { |
7019 | 0 | zend_ast *try_ast = ast->child[0]; |
7020 | 0 | const zend_ast_list *catches = zend_ast_get_list(ast->child[1]); |
7021 | 0 | zend_ast *finally_ast = ast->child[2]; |
7022 | |
|
7023 | 0 | uint32_t i, j; |
7024 | 0 | zend_op *opline; |
7025 | 0 | uint32_t try_catch_offset; |
7026 | 0 | uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0); |
7027 | 0 | uint32_t orig_fast_call_var = CG(context).fast_call_var; |
7028 | 0 | uint32_t orig_try_catch_offset = CG(context).try_catch_offset; |
7029 | |
|
7030 | 0 | if (catches->children == 0 && !finally_ast) { |
7031 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally"); |
7032 | 0 | } |
7033 | | |
7034 | | /* label: try { } must not be equal to try { label: } */ |
7035 | 0 | if (CG(context).labels) { |
7036 | 0 | zend_label *label; |
7037 | 0 | ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) { |
7038 | 0 | if (label->opline_num == get_next_op_number()) { |
7039 | 0 | zend_emit_op(NULL, ZEND_NOP, NULL, NULL); |
7040 | 0 | } |
7041 | 0 | break; |
7042 | 0 | } ZEND_HASH_FOREACH_END(); |
7043 | 0 | } |
7044 | |
|
7045 | 0 | try_catch_offset = zend_add_try_element(get_next_op_number()); |
7046 | |
|
7047 | 0 | if (finally_ast) { |
7048 | 0 | zend_loop_var fast_call; |
7049 | 0 | if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) { |
7050 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK; |
7051 | 0 | } |
7052 | 0 | CG(context).fast_call_var = get_temporary_variable(); |
7053 | | |
7054 | | /* Push FAST_CALL on unwind stack */ |
7055 | 0 | fast_call.opcode = ZEND_FAST_CALL; |
7056 | 0 | fast_call.var_type = IS_TMP_VAR; |
7057 | 0 | fast_call.var_num = CG(context).fast_call_var; |
7058 | 0 | fast_call.try_catch_offset = try_catch_offset; |
7059 | 0 | zend_stack_push(&CG(loop_var_stack), &fast_call); |
7060 | 0 | } |
7061 | |
|
7062 | 0 | CG(context).try_catch_offset = try_catch_offset; |
7063 | |
|
7064 | 0 | zend_compile_stmt(try_ast); |
7065 | |
|
7066 | 0 | if (catches->children != 0) { |
7067 | 0 | jmp_opnums[0] = zend_emit_jump(0); |
7068 | 0 | } |
7069 | |
|
7070 | 0 | for (i = 0; i < catches->children; ++i) { |
7071 | 0 | const zend_ast *catch_ast = catches->child[i]; |
7072 | 0 | const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]); |
7073 | 0 | zend_ast *var_ast = catch_ast->child[1]; |
7074 | 0 | zend_ast *stmt_ast = catch_ast->child[2]; |
7075 | 0 | zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL; |
7076 | 0 | bool is_last_catch = (i + 1 == catches->children); |
7077 | |
|
7078 | 0 | uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0); |
7079 | 0 | uint32_t opnum_catch = (uint32_t)-1; |
7080 | |
|
7081 | 0 | CG(zend_lineno) = catch_ast->lineno; |
7082 | |
|
7083 | 0 | for (j = 0; j < classes->children; j++) { |
7084 | 0 | zend_ast *class_ast = classes->child[j]; |
7085 | 0 | bool is_last_class = (j + 1 == classes->children); |
7086 | |
|
7087 | 0 | if (!zend_is_const_default_class_ref(class_ast)) { |
7088 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement"); |
7089 | 0 | } |
7090 | | |
7091 | 0 | opnum_catch = get_next_op_number(); |
7092 | 0 | if (i == 0 && j == 0) { |
7093 | 0 | CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch; |
7094 | 0 | } |
7095 | |
|
7096 | 0 | opline = get_next_op(); |
7097 | 0 | opline->opcode = ZEND_CATCH; |
7098 | 0 | opline->op1_type = IS_CONST; |
7099 | 0 | opline->op1.constant = zend_add_class_name_literal( |
7100 | 0 | zend_resolve_class_name_ast(class_ast)); |
7101 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
7102 | |
|
7103 | 0 | if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
7104 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
7105 | 0 | } |
7106 | | |
7107 | 0 | opline->result_type = var_name ? IS_CV : IS_UNUSED; |
7108 | 0 | opline->result.var = var_name ? lookup_cv(var_name) : -1; |
7109 | |
|
7110 | 0 | if (is_last_catch && is_last_class) { |
7111 | 0 | opline->extended_value |= ZEND_LAST_CATCH; |
7112 | 0 | } |
7113 | |
|
7114 | 0 | if (!is_last_class) { |
7115 | 0 | jmp_multicatch[j] = zend_emit_jump(0); |
7116 | 0 | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7117 | 0 | opline->op2.opline_num = get_next_op_number(); |
7118 | 0 | } |
7119 | 0 | } |
7120 | | |
7121 | 0 | for (j = 0; j < classes->children - 1; j++) { |
7122 | 0 | zend_update_jump_target_to_next(jmp_multicatch[j]); |
7123 | 0 | } |
7124 | |
|
7125 | 0 | efree(jmp_multicatch); |
7126 | |
|
7127 | 0 | zend_compile_stmt(stmt_ast); |
7128 | |
|
7129 | 0 | if (!is_last_catch) { |
7130 | 0 | jmp_opnums[i + 1] = zend_emit_jump(0); |
7131 | 0 | } |
7132 | |
|
7133 | 0 | ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class"); |
7134 | 0 | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7135 | 0 | if (!is_last_catch) { |
7136 | 0 | opline->op2.opline_num = get_next_op_number(); |
7137 | 0 | } |
7138 | 0 | } |
7139 | | |
7140 | 0 | for (i = 0; i < catches->children; ++i) { |
7141 | 0 | zend_update_jump_target_to_next(jmp_opnums[i]); |
7142 | 0 | } |
7143 | |
|
7144 | 0 | if (finally_ast) { |
7145 | 0 | zend_loop_var discard_exception; |
7146 | 0 | uint32_t opnum_jmp = get_next_op_number() + 1; |
7147 | | |
7148 | | /* Pop FAST_CALL from unwind stack */ |
7149 | 0 | zend_stack_del_top(&CG(loop_var_stack)); |
7150 | | |
7151 | | /* Push DISCARD_EXCEPTION on unwind stack */ |
7152 | 0 | discard_exception.opcode = ZEND_DISCARD_EXCEPTION; |
7153 | 0 | discard_exception.var_type = IS_TMP_VAR; |
7154 | 0 | discard_exception.var_num = CG(context).fast_call_var; |
7155 | 0 | zend_stack_push(&CG(loop_var_stack), &discard_exception); |
7156 | |
|
7157 | 0 | CG(zend_lineno) = finally_ast->lineno; |
7158 | |
|
7159 | 0 | opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL); |
7160 | 0 | opline->op1.num = try_catch_offset; |
7161 | 0 | opline->result_type = IS_TMP_VAR; |
7162 | 0 | opline->result.var = CG(context).fast_call_var; |
7163 | |
|
7164 | 0 | zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
7165 | |
|
7166 | 0 | zend_compile_stmt(finally_ast); |
7167 | |
|
7168 | 0 | CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1; |
7169 | 0 | CG(active_op_array)->try_catch_array[try_catch_offset].finally_end |
7170 | 0 | = get_next_op_number(); |
7171 | |
|
7172 | 0 | opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL); |
7173 | 0 | opline->op1_type = IS_TMP_VAR; |
7174 | 0 | opline->op1.var = CG(context).fast_call_var; |
7175 | 0 | opline->op2.num = orig_try_catch_offset; |
7176 | |
|
7177 | 0 | zend_update_jump_target_to_next(opnum_jmp); |
7178 | |
|
7179 | 0 | CG(context).fast_call_var = orig_fast_call_var; |
7180 | | |
7181 | | /* Pop DISCARD_EXCEPTION from unwind stack */ |
7182 | 0 | zend_stack_del_top(&CG(loop_var_stack)); |
7183 | 0 | } |
7184 | |
|
7185 | 0 | CG(context).try_catch_offset = orig_try_catch_offset; |
7186 | |
|
7187 | 0 | efree(jmp_opnums); |
7188 | 0 | } |
7189 | | /* }}} */ |
7190 | | |
7191 | | /* Encoding declarations must already be handled during parsing */ |
7192 | | bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */ |
7193 | 0 | { |
7194 | 0 | const zend_ast_list *declares = zend_ast_get_list(ast); |
7195 | 0 | uint32_t i; |
7196 | 0 | for (i = 0; i < declares->children; ++i) { |
7197 | 0 | const zend_ast *declare_ast = declares->child[i]; |
7198 | 0 | zend_ast *name_ast = declare_ast->child[0]; |
7199 | 0 | zend_ast *value_ast = declare_ast->child[1]; |
7200 | 0 | const zend_string *name = zend_ast_get_str(name_ast); |
7201 | |
|
7202 | 0 | if (zend_string_equals_literal_ci(name, "encoding")) { |
7203 | 0 | if (value_ast->kind != ZEND_AST_ZVAL) { |
7204 | 0 | zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0); |
7205 | 0 | return false; |
7206 | 0 | } |
7207 | | |
7208 | 0 | if (CG(multibyte)) { |
7209 | 0 | zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast)); |
7210 | |
|
7211 | 0 | const zend_encoding *new_encoding, *old_encoding; |
7212 | 0 | zend_encoding_filter old_input_filter; |
7213 | |
|
7214 | 0 | CG(encoding_declared) = 1; |
7215 | |
|
7216 | 0 | new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name)); |
7217 | 0 | if (!new_encoding) { |
7218 | 0 | zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name)); |
7219 | 0 | } else { |
7220 | 0 | old_input_filter = LANG_SCNG(input_filter); |
7221 | 0 | old_encoding = LANG_SCNG(script_encoding); |
7222 | 0 | zend_multibyte_set_filter(new_encoding); |
7223 | | |
7224 | | /* need to re-scan if input filter changed */ |
7225 | 0 | if (old_input_filter != LANG_SCNG(input_filter) || |
7226 | 0 | (old_input_filter && new_encoding != old_encoding)) { |
7227 | 0 | zend_multibyte_yyinput_again(old_input_filter, old_encoding); |
7228 | 0 | } |
7229 | 0 | } |
7230 | |
|
7231 | 0 | zend_string_release_ex(encoding_name, 0); |
7232 | 0 | } else { |
7233 | 0 | zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because " |
7234 | 0 | "Zend multibyte feature is turned off by settings"); |
7235 | 0 | } |
7236 | 0 | } |
7237 | 0 | } |
7238 | | |
7239 | 0 | return true; |
7240 | 0 | } |
7241 | | /* }}} */ |
7242 | | |
7243 | | /* Check whether this is the first statement, not counting declares. */ |
7244 | | static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */ |
7245 | 0 | { |
7246 | 0 | uint32_t i = 0; |
7247 | 0 | const zend_ast_list *file_ast = zend_ast_get_list(CG(ast)); |
7248 | |
|
7249 | 0 | while (i < file_ast->children) { |
7250 | 0 | if (file_ast->child[i] == ast) { |
7251 | 0 | return SUCCESS; |
7252 | 0 | } else if (file_ast->child[i] == NULL) { |
7253 | 0 | if (!allow_nop) { |
7254 | 0 | return FAILURE; |
7255 | 0 | } |
7256 | 0 | } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) { |
7257 | 0 | return FAILURE; |
7258 | 0 | } |
7259 | 0 | i++; |
7260 | 0 | } |
7261 | 0 | return FAILURE; |
7262 | 0 | } |
7263 | | /* }}} */ |
7264 | | |
7265 | | static void zend_compile_declare(const zend_ast *ast) /* {{{ */ |
7266 | 0 | { |
7267 | 0 | const zend_ast_list *declares = zend_ast_get_list(ast->child[0]); |
7268 | 0 | zend_ast *stmt_ast = ast->child[1]; |
7269 | 0 | zend_declarables orig_declarables = FC(declarables); |
7270 | 0 | uint32_t i; |
7271 | |
|
7272 | 0 | for (i = 0; i < declares->children; ++i) { |
7273 | 0 | zend_ast *declare_ast = declares->child[i]; |
7274 | 0 | zend_ast *name_ast = declare_ast->child[0]; |
7275 | 0 | zend_ast **value_ast_ptr = &declare_ast->child[1]; |
7276 | 0 | zend_string *name = zend_ast_get_str(name_ast); |
7277 | |
|
7278 | 0 | if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) { |
7279 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name)); |
7280 | 0 | } |
7281 | | |
7282 | 0 | if (zend_string_equals_literal_ci(name, "ticks")) { |
7283 | 0 | zval value_zv; |
7284 | 0 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7285 | 0 | FC(declarables).ticks = zval_get_long(&value_zv); |
7286 | 0 | zval_ptr_dtor_nogc(&value_zv); |
7287 | 0 | } else if (zend_string_equals_literal_ci(name, "encoding")) { |
7288 | |
|
7289 | 0 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) { |
7290 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be " |
7291 | 0 | "the very first statement in the script"); |
7292 | 0 | } |
7293 | 0 | } else if (zend_string_equals_literal_ci(name, "strict_types")) { |
7294 | 0 | zval value_zv; |
7295 | |
|
7296 | 0 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
7297 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be " |
7298 | 0 | "the very first statement in the script"); |
7299 | 0 | } |
7300 | | |
7301 | 0 | if (ast->child[1] != NULL) { |
7302 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not " |
7303 | 0 | "use block mode"); |
7304 | 0 | } |
7305 | | |
7306 | 0 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7307 | |
|
7308 | 0 | if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) { |
7309 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value"); |
7310 | 0 | } |
7311 | | |
7312 | 0 | if (Z_LVAL(value_zv) == 1) { |
7313 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES; |
7314 | 0 | } |
7315 | |
|
7316 | 0 | } else { |
7317 | 0 | zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name)); |
7318 | 0 | } |
7319 | 0 | } |
7320 | | |
7321 | 0 | if (stmt_ast) { |
7322 | 0 | zend_compile_stmt(stmt_ast); |
7323 | |
|
7324 | 0 | FC(declarables) = orig_declarables; |
7325 | 0 | } |
7326 | 0 | } |
7327 | | /* }}} */ |
7328 | | |
7329 | | static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */ |
7330 | 0 | { |
7331 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
7332 | 0 | uint32_t i; |
7333 | 0 | for (i = 0; i < list->children; ++i) { |
7334 | 0 | zend_compile_stmt(list->child[i]); |
7335 | 0 | } |
7336 | 0 | } |
7337 | | /* }}} */ |
7338 | | |
7339 | | ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */ |
7340 | 3.38k | { |
7341 | 3.38k | uint32_t i, n; |
7342 | | |
7343 | 3.38k | func->common.arg_flags[0] = 0; |
7344 | 3.38k | func->common.arg_flags[1] = 0; |
7345 | 3.38k | func->common.arg_flags[2] = 0; |
7346 | 3.38k | if (func->common.arg_info) { |
7347 | 3.38k | n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM); |
7348 | 3.38k | i = 0; |
7349 | 6.85k | while (i < n) { |
7350 | 3.47k | ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i])); |
7351 | 3.47k | i++; |
7352 | 3.47k | } |
7353 | 3.38k | if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) { |
7354 | 8 | uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]); |
7355 | 92 | while (i < MAX_ARG_FLAG_NUM) { |
7356 | 84 | ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference); |
7357 | 84 | i++; |
7358 | 84 | } |
7359 | 8 | } |
7360 | 3.38k | } |
7361 | 3.38k | } |
7362 | | /* }}} */ |
7363 | | |
7364 | | static zend_type zend_compile_single_typename(zend_ast *ast) |
7365 | 0 | { |
7366 | 0 | ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE)); |
7367 | 0 | if (ast->kind == ZEND_AST_TYPE) { |
7368 | 0 | if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) { |
7369 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7370 | 0 | "Cannot use \"static\" when no class scope is active"); |
7371 | 0 | } |
7372 | | |
7373 | 0 | return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0); |
7374 | 0 | } else { |
7375 | 0 | zend_string *type_name = zend_ast_get_str(ast); |
7376 | 0 | uint8_t type_code = zend_lookup_builtin_type_by_name(type_name); |
7377 | |
|
7378 | 0 | if (type_code != 0) { |
7379 | 0 | if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) { |
7380 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7381 | 0 | "Type declaration '%s' must be unqualified", |
7382 | 0 | ZSTR_VAL(zend_string_tolower(type_name))); |
7383 | 0 | } |
7384 | | |
7385 | | /* Transform iterable into a type union alias */ |
7386 | 0 | if (type_code == IS_ITERABLE) { |
7387 | | /* Set iterable bit for BC compat during Reflection and string representation of type */ |
7388 | 0 | zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE), |
7389 | 0 | (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT)); |
7390 | 0 | return iterable; |
7391 | 0 | } |
7392 | | |
7393 | 0 | return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0); |
7394 | 0 | } else { |
7395 | 0 | const char *correct_name; |
7396 | 0 | uint32_t fetch_type = zend_get_class_fetch_type_ast(ast); |
7397 | 0 | zend_string *class_name = type_name; |
7398 | |
|
7399 | 0 | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
7400 | 0 | class_name = zend_resolve_class_name_ast(ast); |
7401 | 0 | zend_assert_valid_class_name(class_name, "a type name"); |
7402 | 0 | } else { |
7403 | 0 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT); |
7404 | |
|
7405 | 0 | zend_ensure_valid_class_fetch_type(fetch_type); |
7406 | |
|
7407 | 0 | bool substitute_self_parent = zend_is_scope_known() |
7408 | 0 | && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS); |
7409 | |
|
7410 | 0 | if (fetch_type == ZEND_FETCH_CLASS_SELF) { |
7411 | | /* Scope might be unknown for unbound closures and traits */ |
7412 | 0 | if (substitute_self_parent) { |
7413 | 0 | class_name = CG(active_class_entry)->name; |
7414 | 0 | ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time"); |
7415 | 0 | } |
7416 | 0 | } else { |
7417 | 0 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT); |
7418 | | /* Scope might be unknown for unbound closures and traits */ |
7419 | 0 | if (substitute_self_parent) { |
7420 | 0 | class_name = CG(active_class_entry)->parent_name; |
7421 | 0 | ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time"); |
7422 | 0 | } |
7423 | 0 | } |
7424 | 0 | zend_string_addref(class_name); |
7425 | 0 | } |
7426 | |
|
7427 | 0 | if (ast->attr == ZEND_NAME_NOT_FQ |
7428 | 0 | && zend_is_confusable_type(type_name, &correct_name) |
7429 | 0 | && zend_is_not_imported(type_name)) { |
7430 | 0 | const char *extra = |
7431 | 0 | FC(current_namespace) ? " or import the class with \"use\"" : ""; |
7432 | 0 | if (correct_name) { |
7433 | 0 | zend_error(E_COMPILE_WARNING, |
7434 | 0 | "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? " |
7435 | 0 | "Write \"\\%s\"%s to suppress this warning", |
7436 | 0 | ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra); |
7437 | 0 | } else { |
7438 | 0 | zend_error(E_COMPILE_WARNING, |
7439 | 0 | "\"%s\" is not a supported builtin type " |
7440 | 0 | "and will be interpreted as a class name. " |
7441 | 0 | "Write \"\\%s\"%s to suppress this warning", |
7442 | 0 | ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra); |
7443 | 0 | } |
7444 | 0 | } |
7445 | |
|
7446 | 0 | class_name = zend_new_interned_string(class_name); |
7447 | 0 | zend_alloc_ce_cache(class_name); |
7448 | 0 | return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0); |
7449 | 0 | } |
7450 | 0 | } |
7451 | 0 | } |
7452 | | |
7453 | | static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type) |
7454 | 0 | { |
7455 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type)); |
7456 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type)); |
7457 | 0 | const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type); |
7458 | 0 | const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type); |
7459 | 0 | const zend_type_list *smaller_type_list, *larger_type_list; |
7460 | 0 | bool flipped = false; |
7461 | |
|
7462 | 0 | if (r_type_list->num_types < l_type_list->num_types) { |
7463 | 0 | smaller_type_list = r_type_list; |
7464 | 0 | larger_type_list = l_type_list; |
7465 | 0 | flipped = true; |
7466 | 0 | } else { |
7467 | 0 | smaller_type_list = l_type_list; |
7468 | 0 | larger_type_list = r_type_list; |
7469 | 0 | } |
7470 | |
|
7471 | 0 | unsigned int sum = 0; |
7472 | 0 | const zend_type *outer_type; |
7473 | 0 | ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type) { |
7474 | 0 | const zend_type *inner_type; |
7475 | 0 | ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type) { |
7476 | 0 | if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) { |
7477 | 0 | sum++; |
7478 | 0 | break; |
7479 | 0 | } |
7480 | 0 | } ZEND_TYPE_LIST_FOREACH_END(); |
7481 | 0 | } ZEND_TYPE_LIST_FOREACH_END(); |
7482 | |
|
7483 | 0 | if (sum == smaller_type_list->num_types) { |
7484 | 0 | zend_string *smaller_type_str; |
7485 | 0 | zend_string *larger_type_str; |
7486 | 0 | if (flipped) { |
7487 | 0 | smaller_type_str = zend_type_to_string(right_type); |
7488 | 0 | larger_type_str = zend_type_to_string(left_type); |
7489 | 0 | } else { |
7490 | 0 | smaller_type_str = zend_type_to_string(left_type); |
7491 | 0 | larger_type_str = zend_type_to_string(right_type); |
7492 | 0 | } |
7493 | 0 | if (smaller_type_list->num_types == larger_type_list->num_types) { |
7494 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s", |
7495 | 0 | ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str)); |
7496 | 0 | } else { |
7497 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7498 | 0 | ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str)); |
7499 | 0 | } |
7500 | 0 | } |
7501 | 0 | } |
7502 | | |
7503 | | static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type) |
7504 | 0 | { |
7505 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type)); |
7506 | 0 | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type)); |
7507 | |
|
7508 | 0 | const zend_type *single_intersection_type = NULL; |
7509 | 0 | ZEND_TYPE_FOREACH(intersection_type, single_intersection_type) { |
7510 | 0 | if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) { |
7511 | 0 | zend_string *single_type_str = zend_type_to_string(single_type); |
7512 | 0 | zend_string *complete_type = zend_type_to_string(intersection_type); |
7513 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7514 | 0 | ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str)); |
7515 | 0 | } |
7516 | 0 | } ZEND_TYPE_FOREACH_END(); |
7517 | 0 | } |
7518 | | |
7519 | | /* Used by both intersection and union types prior to transforming the type list to a full zend_type */ |
7520 | | static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type) |
7521 | 0 | { |
7522 | 0 | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type)); |
7523 | 0 | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7524 | 0 | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7525 | 0 | zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type); |
7526 | 0 | continue; |
7527 | 0 | } |
7528 | 0 | if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) { |
7529 | 0 | zend_string *single_type_str = zend_type_to_string(type); |
7530 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str)); |
7531 | 0 | } |
7532 | 0 | } |
7533 | 0 | } |
7534 | | |
7535 | | static zend_type zend_compile_typename(zend_ast *ast); |
7536 | | |
7537 | | static zend_type zend_compile_typename_ex( |
7538 | | zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */ |
7539 | 0 | { |
7540 | 0 | bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE; |
7541 | 0 | zend_ast_attr orig_ast_attr = ast->attr; |
7542 | 0 | zend_type type = ZEND_TYPE_INIT_NONE(0); |
7543 | |
|
7544 | 0 | if (is_marked_nullable) { |
7545 | 0 | ast->attr &= ~ZEND_TYPE_NULLABLE; |
7546 | 0 | } |
7547 | |
|
7548 | 0 | if (ast->kind == ZEND_AST_TYPE_UNION) { |
7549 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
7550 | 0 | zend_type_list *type_list; |
7551 | 0 | bool is_composite = false; |
7552 | 0 | bool has_only_iterable_class = true; |
7553 | 0 | ALLOCA_FLAG(use_heap) |
7554 | |
|
7555 | 0 | type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap); |
7556 | 0 | type_list->num_types = 0; |
7557 | |
|
7558 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
7559 | 0 | zend_ast *type_ast = list->child[i]; |
7560 | 0 | zend_type single_type; |
7561 | 0 | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7562 | |
|
7563 | 0 | if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7564 | 0 | has_only_iterable_class = false; |
7565 | 0 | is_composite = true; |
7566 | | /* The first class type can be stored directly as the type ptr payload. */ |
7567 | 0 | if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) { |
7568 | | /* Switch from single name to name list. */ |
7569 | 0 | type_list->num_types = 1; |
7570 | 0 | type_list->types[0] = type; |
7571 | | /* Clear MAY_BE_* type flags */ |
7572 | 0 | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7573 | 0 | } |
7574 | | /* Mark type as list type */ |
7575 | 0 | ZEND_TYPE_SET_LIST(type, type_list); |
7576 | |
|
7577 | 0 | single_type = zend_compile_typename(type_ast); |
7578 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type)); |
7579 | |
|
7580 | 0 | type_list->types[type_list->num_types++] = single_type; |
7581 | | |
7582 | | /* Check for trivially redundant class types */ |
7583 | 0 | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7584 | 0 | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7585 | 0 | zend_are_intersection_types_redundant(single_type, type_list->types[i]); |
7586 | 0 | continue; |
7587 | 0 | } |
7588 | | /* Type from type list is a simple type */ |
7589 | 0 | zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]); |
7590 | 0 | } |
7591 | 0 | continue; |
7592 | 0 | } |
7593 | | |
7594 | 0 | single_type = zend_compile_single_typename(type_ast); |
7595 | 0 | uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type); |
7596 | |
|
7597 | 0 | if (single_type_mask == MAY_BE_ANY) { |
7598 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type"); |
7599 | 0 | } |
7600 | 0 | if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7601 | 0 | has_only_iterable_class = false; |
7602 | 0 | } |
7603 | |
|
7604 | 0 | uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask; |
7605 | 0 | if (type_mask_overlap) { |
7606 | 0 | zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap); |
7607 | 0 | zend_string *overlap_type_str = zend_type_to_string(overlap_type); |
7608 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7609 | 0 | "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str)); |
7610 | 0 | } |
7611 | | |
7612 | 0 | if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE)) |
7613 | 0 | || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) { |
7614 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7615 | 0 | "Type contains both true and false, bool must be used instead"); |
7616 | 0 | } |
7617 | 0 | ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type); |
7618 | | /* Clear MAY_BE_* type flags */ |
7619 | 0 | ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7620 | |
|
7621 | 0 | if (ZEND_TYPE_IS_COMPLEX(single_type)) { |
7622 | 0 | if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) { |
7623 | | /* The first class type can be stored directly as the type ptr payload. */ |
7624 | 0 | ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type)); |
7625 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT; |
7626 | 0 | } else { |
7627 | 0 | if (type_list->num_types == 0) { |
7628 | | /* Switch from single name to name list. */ |
7629 | 0 | type_list->num_types = 1; |
7630 | 0 | type_list->types[0] = type; |
7631 | | /* Clear MAY_BE_* type flags */ |
7632 | 0 | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7633 | 0 | ZEND_TYPE_SET_LIST(type, type_list); |
7634 | 0 | } |
7635 | |
|
7636 | 0 | type_list->types[type_list->num_types++] = single_type; |
7637 | | |
7638 | | /* Check for trivially redundant class types */ |
7639 | 0 | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7640 | 0 | } |
7641 | 0 | } |
7642 | 0 | } |
7643 | | |
7644 | 0 | if (type_list->num_types) { |
7645 | 0 | zend_type_list *list = zend_arena_alloc( |
7646 | 0 | &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7647 | 0 | memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7648 | 0 | ZEND_TYPE_SET_LIST(type, list); |
7649 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7650 | | /* Inform that the type list is a union type */ |
7651 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7652 | 0 | } |
7653 | |
|
7654 | 0 | free_alloca(type_list, use_heap); |
7655 | |
|
7656 | 0 | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7657 | 0 | if ((type_mask & MAY_BE_OBJECT) && |
7658 | 0 | ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) { |
7659 | 0 | zend_string *type_str = zend_type_to_string(type); |
7660 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7661 | 0 | "Type %s contains both object and a class type, which is redundant", |
7662 | 0 | ZSTR_VAL(type_str)); |
7663 | 0 | } |
7664 | 0 | } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7665 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
7666 | 0 | zend_type_list *type_list; |
7667 | | |
7668 | | /* Allocate the type list directly on the arena as it must be a type |
7669 | | * list of the same number of elements as the AST list has children */ |
7670 | 0 | type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children)); |
7671 | 0 | type_list->num_types = 0; |
7672 | |
|
7673 | 0 | ZEND_ASSERT(list->children > 1); |
7674 | |
|
7675 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
7676 | 0 | zend_ast *type_ast = list->child[i]; |
7677 | 0 | zend_type single_type = zend_compile_single_typename(type_ast); |
7678 | | |
7679 | | /* An intersection of union types cannot exist so invalidate it |
7680 | | * Currently only can happen with iterable getting canonicalized to Traversable|array */ |
7681 | 0 | if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7682 | 0 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7683 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7684 | 0 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7685 | 0 | } |
7686 | | /* An intersection of standard types cannot exist so invalidate it */ |
7687 | 0 | if (ZEND_TYPE_IS_ONLY_MASK(single_type)) { |
7688 | 0 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7689 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7690 | 0 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7691 | 0 | } |
7692 | | /* Check for "self" and "parent" too */ |
7693 | 0 | if ( |
7694 | 0 | zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF)) |
7695 | 0 | || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT)) |
7696 | 0 | ) { |
7697 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7698 | 0 | "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type))); |
7699 | 0 | } |
7700 | | |
7701 | | /* Add type to the type list */ |
7702 | 0 | type_list->types[type_list->num_types++] = single_type; |
7703 | | |
7704 | | /* Check for trivially redundant class types */ |
7705 | 0 | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7706 | 0 | } |
7707 | | |
7708 | 0 | ZEND_ASSERT(list->children == type_list->num_types); |
7709 | | |
7710 | | /* An implicitly nullable intersection type needs to be converted to a DNF type */ |
7711 | 0 | if (force_allow_null) { |
7712 | 0 | zend_type intersection_type = ZEND_TYPE_INIT_NONE(0); |
7713 | 0 | ZEND_TYPE_SET_LIST(intersection_type, type_list); |
7714 | 0 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7715 | 0 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT; |
7716 | |
|
7717 | 0 | zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1)); |
7718 | 0 | dnf_type_list->num_types = 1; |
7719 | 0 | dnf_type_list->types[0] = intersection_type; |
7720 | 0 | ZEND_TYPE_SET_LIST(type, dnf_type_list); |
7721 | | /* Inform that the type list is a DNF type */ |
7722 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7723 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7724 | 0 | } else { |
7725 | 0 | ZEND_TYPE_SET_LIST(type, type_list); |
7726 | | /* Inform that the type list is an intersection type */ |
7727 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7728 | 0 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7729 | 0 | } |
7730 | 0 | } else { |
7731 | 0 | type = zend_compile_single_typename(ast); |
7732 | 0 | } |
7733 | | |
7734 | 0 | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
7735 | |
|
7736 | 0 | if (type_mask == MAY_BE_ANY && is_marked_nullable) { |
7737 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null"); |
7738 | 0 | } |
7739 | | |
7740 | 0 | if ((type_mask & MAY_BE_NULL) && is_marked_nullable) { |
7741 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable"); |
7742 | 0 | } |
7743 | | |
7744 | 0 | if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) { |
7745 | 0 | *forced_allow_null = true; |
7746 | 0 | } |
7747 | |
|
7748 | 0 | if (is_marked_nullable || force_allow_null) { |
7749 | 0 | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
7750 | 0 | type_mask = ZEND_TYPE_PURE_MASK(type); |
7751 | 0 | } |
7752 | |
|
7753 | 0 | if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) { |
7754 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type"); |
7755 | 0 | } |
7756 | | |
7757 | 0 | if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) { |
7758 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type"); |
7759 | 0 | } |
7760 | | |
7761 | 0 | ast->attr = orig_ast_attr; |
7762 | 0 | return type; |
7763 | 0 | } |
7764 | | /* }}} */ |
7765 | | |
7766 | | static zend_type zend_compile_typename(zend_ast *ast) |
7767 | 0 | { |
7768 | 0 | bool forced_allow_null; |
7769 | 0 | return zend_compile_typename_ex(ast, false, &forced_allow_null); |
7770 | 0 | } |
7771 | | |
7772 | | /* May convert value from int to float. */ |
7773 | | static bool zend_is_valid_default_value(zend_type type, zval *value) |
7774 | 0 | { |
7775 | 0 | ZEND_ASSERT(ZEND_TYPE_IS_SET(type)); |
7776 | 0 | if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) { |
7777 | 0 | return true; |
7778 | 0 | } |
7779 | 0 | if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) { |
7780 | | /* Integers are allowed as initializers for floating-point values. */ |
7781 | 0 | convert_to_double(value); |
7782 | 0 | return true; |
7783 | 0 | } |
7784 | 0 | return false; |
7785 | 0 | } |
7786 | | |
7787 | | static void zend_compile_attributes( |
7788 | | HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted |
7789 | 0 | ) /* {{{ */ { |
7790 | 0 | zend_attribute *attr; |
7791 | 0 | zend_internal_attribute *config; |
7792 | |
|
7793 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
7794 | 0 | uint32_t g, i, j; |
7795 | |
|
7796 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST); |
7797 | |
|
7798 | 0 | for (g = 0; g < list->children; g++) { |
7799 | 0 | const zend_ast_list *group = zend_ast_get_list(list->child[g]); |
7800 | |
|
7801 | 0 | ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP); |
7802 | |
|
7803 | 0 | for (i = 0; i < group->children; i++) { |
7804 | 0 | ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE); |
7805 | |
|
7806 | 0 | const zend_ast *el = group->child[i]; |
7807 | |
|
7808 | 0 | if (el->child[1] && |
7809 | 0 | el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) { |
7810 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7811 | 0 | "Cannot create Closure as attribute argument"); |
7812 | 0 | } |
7813 | | |
7814 | 0 | zend_string *name = zend_resolve_class_name_ast(el->child[0]); |
7815 | 0 | zend_string *lcname = zend_string_tolower_ex(name, false); |
7816 | 0 | zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL; |
7817 | |
|
7818 | 0 | config = zend_internal_attribute_get(lcname); |
7819 | 0 | zend_string_release(lcname); |
7820 | | |
7821 | | /* Exclude internal attributes that do not match on promoted properties. */ |
7822 | 0 | if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
7823 | 0 | if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) { |
7824 | 0 | zend_string_release(name); |
7825 | 0 | continue; |
7826 | 0 | } |
7827 | 0 | } |
7828 | | |
7829 | 0 | uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) |
7830 | 0 | ? ZEND_ATTRIBUTE_STRICT_TYPES : 0; |
7831 | 0 | attr = zend_add_attribute( |
7832 | 0 | attributes, name, args ? args->children : 0, flags, offset, el->lineno); |
7833 | 0 | zend_string_release(name); |
7834 | | |
7835 | | /* Populate arguments */ |
7836 | 0 | if (args) { |
7837 | 0 | ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST); |
7838 | |
|
7839 | 0 | bool uses_named_args = false; |
7840 | 0 | for (j = 0; j < args->children; j++) { |
7841 | 0 | zend_ast **arg_ast_ptr = &args->child[j]; |
7842 | 0 | zend_ast *arg_ast = *arg_ast_ptr; |
7843 | |
|
7844 | 0 | if (arg_ast->kind == ZEND_AST_UNPACK) { |
7845 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7846 | 0 | "Cannot use unpacking in attribute argument list"); |
7847 | 0 | } |
7848 | | |
7849 | 0 | if (arg_ast->kind == ZEND_AST_NAMED_ARG) { |
7850 | 0 | attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0])); |
7851 | 0 | arg_ast_ptr = &arg_ast->child[1]; |
7852 | 0 | uses_named_args = true; |
7853 | |
|
7854 | 0 | for (uint32_t k = 0; k < j; k++) { |
7855 | 0 | if (attr->args[k].name && |
7856 | 0 | zend_string_equals(attr->args[k].name, attr->args[j].name)) { |
7857 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s", |
7858 | 0 | ZSTR_VAL(attr->args[j].name)); |
7859 | 0 | } |
7860 | 0 | } |
7861 | 0 | } else if (uses_named_args) { |
7862 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
7863 | 0 | "Cannot use positional argument after named argument"); |
7864 | 0 | } |
7865 | | |
7866 | 0 | zend_const_expr_to_zval( |
7867 | 0 | &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true); |
7868 | 0 | } |
7869 | 0 | } |
7870 | 0 | } |
7871 | 0 | } |
7872 | | |
7873 | 0 | if (*attributes != NULL) { |
7874 | | /* Allow delaying target validation for forward compatibility. */ |
7875 | 0 | const zend_attribute *delayed_target_validation = NULL; |
7876 | 0 | if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) { |
7877 | 0 | ZEND_ASSERT(offset >= 1); |
7878 | | /* zend_get_parameter_attribute_str will add 1 too */ |
7879 | 0 | delayed_target_validation = zend_get_parameter_attribute_str( |
7880 | 0 | *attributes, |
7881 | 0 | "delayedtargetvalidation", |
7882 | 0 | strlen("delayedtargetvalidation"), |
7883 | 0 | offset - 1 |
7884 | 0 | ); |
7885 | 0 | } else { |
7886 | 0 | delayed_target_validation = zend_get_attribute_str( |
7887 | 0 | *attributes, |
7888 | 0 | "delayedtargetvalidation", |
7889 | 0 | strlen("delayedtargetvalidation") |
7890 | 0 | ); |
7891 | 0 | } |
7892 | | /* Validate attributes in a secondary loop (needed to detect repeated attributes). */ |
7893 | 0 | ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) { |
7894 | 0 | if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) { |
7895 | 0 | continue; |
7896 | 0 | } |
7897 | | |
7898 | 0 | bool run_validator = true; |
7899 | 0 | if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
7900 | 0 | if (delayed_target_validation == NULL) { |
7901 | 0 | zend_string *location = zend_get_attribute_target_names(target); |
7902 | 0 | zend_string *allowed = zend_get_attribute_target_names(config->flags); |
7903 | |
|
7904 | 0 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)", |
7905 | 0 | ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed) |
7906 | 0 | ); |
7907 | 0 | } |
7908 | 0 | run_validator = false; |
7909 | 0 | } |
7910 | | |
7911 | 0 | if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) { |
7912 | 0 | if (zend_is_attribute_repeated(*attributes, attr)) { |
7913 | 0 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name)); |
7914 | 0 | } |
7915 | 0 | } |
7916 | | |
7917 | | /* Validators are not run if the target is already invalid */ |
7918 | 0 | if (run_validator && config->validator != NULL) { |
7919 | 0 | zend_string *error = config->validator(attr, target, CG(active_class_entry)); |
7920 | 0 | if (error != NULL) { |
7921 | 0 | if (delayed_target_validation == NULL) { |
7922 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error)); |
7923 | 0 | } else { |
7924 | 0 | attr->validation_error = error; |
7925 | 0 | } |
7926 | 0 | } |
7927 | 0 | } |
7928 | 0 | } ZEND_HASH_FOREACH_END(); |
7929 | 0 | } |
7930 | 0 | } |
7931 | | /* }}} */ |
7932 | | |
7933 | | static void zend_compile_property_hooks( |
7934 | | zend_property_info *prop_info, zend_string *prop_name, |
7935 | | zend_ast *prop_type_ast, const zend_ast_list *hooks); |
7936 | | |
7937 | | typedef struct { |
7938 | | const zend_string *property_name; |
7939 | | bool uses_property; |
7940 | | } find_property_usage_context; |
7941 | | |
7942 | | static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */ |
7943 | 0 | { |
7944 | 0 | zend_ast *ast = *ast_ptr; |
7945 | 0 | find_property_usage_context *context = (find_property_usage_context *) _context; |
7946 | |
|
7947 | 0 | if (ast == NULL) { |
7948 | 0 | return; |
7949 | 0 | } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) { |
7950 | 0 | const zend_ast *object_ast = ast->child[0]; |
7951 | 0 | zend_ast *property_ast = ast->child[1]; |
7952 | |
|
7953 | 0 | if (object_ast->kind == ZEND_AST_VAR |
7954 | 0 | && object_ast->child[0]->kind == ZEND_AST_ZVAL |
7955 | 0 | && property_ast->kind == ZEND_AST_ZVAL) { |
7956 | 0 | const zval *object = zend_ast_get_zval(object_ast->child[0]); |
7957 | 0 | const zval *property = zend_ast_get_zval(property_ast); |
7958 | 0 | if (Z_TYPE_P(object) == IS_STRING |
7959 | 0 | && Z_TYPE_P(property) == IS_STRING |
7960 | 0 | && zend_string_equals_literal(Z_STR_P(object), "this") |
7961 | 0 | && zend_string_equals(Z_STR_P(property), context->property_name)) { |
7962 | 0 | context->uses_property = true; |
7963 | | /* No need to look for references in this branch. */ |
7964 | 0 | return; |
7965 | 0 | } |
7966 | 0 | } |
7967 | 0 | } |
7968 | | |
7969 | | /* Don't search across function/class boundaries. */ |
7970 | 0 | if (!zend_ast_is_special(ast)) { |
7971 | 0 | zend_ast_apply(ast, zend_property_hook_find_property_usage, context); |
7972 | 0 | } |
7973 | 0 | } |
7974 | | |
7975 | | static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast) |
7976 | 0 | { |
7977 | 0 | if (zend_string_equals_literal_ci(hook_name, "set") |
7978 | 0 | && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
7979 | 0 | return true; |
7980 | 0 | } |
7981 | | |
7982 | 0 | find_property_usage_context context = { property_name, false }; |
7983 | 0 | zend_property_hook_find_property_usage(&hook_ast, &context); |
7984 | 0 | return context.uses_property; |
7985 | 0 | } |
7986 | | |
7987 | | static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast) |
7988 | 0 | { |
7989 | 0 | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
7990 | 0 | return true; |
7991 | 0 | } |
7992 | 0 | if (!hooks_ast) { |
7993 | 0 | return false; |
7994 | 0 | } |
7995 | | |
7996 | 0 | bool is_virtual = true; |
7997 | |
|
7998 | 0 | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
7999 | 0 | for (uint32_t i = 0; i < hooks->children; i++) { |
8000 | 0 | const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i]; |
8001 | 0 | zend_ast *body = hook->child[2]; |
8002 | 0 | if (body && zend_property_hook_uses_property(property_name, hook->name, body)) { |
8003 | 0 | is_virtual = false; |
8004 | 0 | } |
8005 | 0 | } |
8006 | |
|
8007 | 0 | return is_virtual; |
8008 | 0 | } |
8009 | | |
8010 | | static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */ |
8011 | 0 | { |
8012 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
8013 | 0 | uint32_t i; |
8014 | 0 | zend_op_array *op_array = CG(active_op_array); |
8015 | 0 | zend_arg_info *arg_infos; |
8016 | |
|
8017 | 0 | if (return_type_ast || fallback_return_type) { |
8018 | | /* Use op_array->arg_info[-1] for return type */ |
8019 | 0 | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0); |
8020 | 0 | arg_infos->name = NULL; |
8021 | 0 | if (return_type_ast) { |
8022 | 0 | arg_infos->type = zend_compile_typename(return_type_ast); |
8023 | 0 | ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS( |
8024 | 0 | (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0); |
8025 | 0 | } else { |
8026 | 0 | arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0); |
8027 | 0 | } |
8028 | 0 | arg_infos->doc_comment = NULL; |
8029 | 0 | arg_infos++; |
8030 | 0 | op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE; |
8031 | |
|
8032 | 0 | if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID) |
8033 | 0 | && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) { |
8034 | 0 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8035 | 0 | zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name)); |
8036 | 0 | zend_string_release(func_name); |
8037 | 0 | } |
8038 | 0 | } else { |
8039 | 0 | if (list->children == 0) { |
8040 | 0 | return; |
8041 | 0 | } |
8042 | 0 | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0); |
8043 | 0 | } |
8044 | | |
8045 | | /* Find last required parameter number for deprecation message. */ |
8046 | 0 | uint32_t last_required_param = (uint32_t) -1; |
8047 | 0 | for (i = 0; i < list->children; ++i) { |
8048 | 0 | zend_ast *param_ast = list->child[i]; |
8049 | 0 | zend_ast *default_ast_ptr = param_ast->child[2]; |
8050 | 0 | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8051 | 0 | if (!default_ast_ptr && !is_variadic) { |
8052 | 0 | last_required_param = i; |
8053 | 0 | } |
8054 | 0 | } |
8055 | |
|
8056 | 0 | const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL; |
8057 | 0 | for (i = 0; i < list->children; ++i) { |
8058 | 0 | zend_ast *param_ast = list->child[i]; |
8059 | 0 | zend_ast *type_ast = param_ast->child[0]; |
8060 | 0 | zend_ast *var_ast = param_ast->child[1]; |
8061 | 0 | zend_ast **default_ast_ptr = ¶m_ast->child[2]; |
8062 | 0 | zend_ast *attributes_ast = param_ast->child[3]; |
8063 | 0 | zend_ast *doc_comment_ast = param_ast->child[4]; |
8064 | 0 | zend_ast *hooks_ast = param_ast->child[5]; |
8065 | 0 | zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast)); |
8066 | 0 | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8067 | 0 | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8068 | 0 | uint32_t property_flags = param_ast->attr & promotion_flags; |
8069 | 0 | bool is_promoted = property_flags || hooks_ast; |
8070 | |
|
8071 | 0 | CG(zend_lineno) = param_ast->lineno; |
8072 | |
|
8073 | 0 | znode var_node, default_node; |
8074 | 0 | uint8_t opcode; |
8075 | 0 | zend_op *opline; |
8076 | 0 | zend_arg_info *arg_info; |
8077 | |
|
8078 | 0 | if (zend_is_auto_global(name)) { |
8079 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s", |
8080 | 0 | ZSTR_VAL(name)); |
8081 | 0 | } |
8082 | | |
8083 | 0 | var_node.op_type = IS_CV; |
8084 | 0 | var_node.u.op.var = lookup_cv(name); |
8085 | |
|
8086 | 0 | if (EX_VAR_TO_NUM(var_node.u.op.var) != i) { |
8087 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s", |
8088 | 0 | ZSTR_VAL(name)); |
8089 | 0 | } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8090 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter"); |
8091 | 0 | } else if (zend_string_equals_literal(name, "http_response_header")) { |
8092 | 0 | CG(context).has_assigned_to_http_response_header = true; |
8093 | 0 | } |
8094 | | |
8095 | 0 | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8096 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic"); |
8097 | 0 | } |
8098 | | |
8099 | 0 | if (is_variadic) { |
8100 | 0 | opcode = ZEND_RECV_VARIADIC; |
8101 | 0 | default_node.op_type = IS_UNUSED; |
8102 | 0 | op_array->fn_flags |= ZEND_ACC_VARIADIC; |
8103 | |
|
8104 | 0 | if (*default_ast_ptr) { |
8105 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8106 | 0 | "Variadic parameter cannot have a default value"); |
8107 | 0 | } |
8108 | 0 | } else if (*default_ast_ptr) { |
8109 | | /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */ |
8110 | 0 | uint32_t cops = CG(compiler_options); |
8111 | 0 | CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION; |
8112 | 0 | opcode = ZEND_RECV_INIT; |
8113 | 0 | default_node.op_type = IS_CONST; |
8114 | 0 | zend_const_expr_to_zval( |
8115 | 0 | &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true); |
8116 | 0 | CG(compiler_options) = cops; |
8117 | 0 | } else { |
8118 | 0 | opcode = ZEND_RECV; |
8119 | 0 | default_node.op_type = IS_UNUSED; |
8120 | 0 | op_array->required_num_args = i + 1; |
8121 | 0 | } |
8122 | | |
8123 | 0 | arg_info = &arg_infos[i]; |
8124 | 0 | arg_info->name = zend_string_copy(name); |
8125 | 0 | arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0); |
8126 | 0 | arg_info->default_value = NULL; |
8127 | 0 | arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8128 | |
|
8129 | 0 | if (attributes_ast) { |
8130 | 0 | zend_compile_attributes( |
8131 | 0 | &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER, |
8132 | 0 | is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0 |
8133 | 0 | ); |
8134 | 0 | } |
8135 | |
|
8136 | 0 | bool forced_allow_nullable = false; |
8137 | 0 | if (type_ast) { |
8138 | 0 | uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF; |
8139 | 0 | bool force_nullable = default_type == IS_NULL && !is_promoted; |
8140 | |
|
8141 | 0 | op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS; |
8142 | 0 | arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable); |
8143 | 0 | if (forced_allow_nullable) { |
8144 | 0 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8145 | 0 | zend_error(E_DEPRECATED, |
8146 | 0 | "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type " |
8147 | 0 | "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name)); |
8148 | 0 | zend_string_release(func_name); |
8149 | 0 | } |
8150 | |
|
8151 | 0 | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) { |
8152 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type"); |
8153 | 0 | } |
8154 | | |
8155 | 0 | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) { |
8156 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type"); |
8157 | 0 | } |
8158 | | |
8159 | 0 | if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable |
8160 | 0 | && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) { |
8161 | 0 | zend_string *type_str = zend_type_to_string(arg_info->type); |
8162 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8163 | 0 | "Cannot use %s as default value for parameter $%s of type %s", |
8164 | 0 | zend_get_type_by_const(default_type), |
8165 | 0 | ZSTR_VAL(name), ZSTR_VAL(type_str)); |
8166 | 0 | } |
8167 | 0 | } |
8168 | 0 | if (last_required_param != (uint32_t) -1 |
8169 | 0 | && i < last_required_param |
8170 | 0 | && default_node.op_type == IS_CONST) { |
8171 | | /* Ignore parameters of the form "Type $param = null". |
8172 | | * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */ |
8173 | 0 | if (!forced_allow_nullable) { |
8174 | 0 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8175 | 0 | zend_ast *required_param_ast = list->child[last_required_param]; |
8176 | 0 | zend_error(E_DEPRECATED, |
8177 | 0 | "%s(): Optional parameter $%s declared before required parameter $%s " |
8178 | 0 | "is implicitly treated as a required parameter", |
8179 | 0 | ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1]))); |
8180 | 0 | zend_string_release(func_name); |
8181 | 0 | } |
8182 | | |
8183 | | /* Regardless of whether we issue a deprecation, convert this parameter into |
8184 | | * a required parameter without a default value. This ensures that it cannot be |
8185 | | * used as an optional parameter even with named parameters. */ |
8186 | 0 | opcode = ZEND_RECV; |
8187 | 0 | default_node.op_type = IS_UNUSED; |
8188 | 0 | zval_ptr_dtor(&default_node.u.constant); |
8189 | 0 | } |
8190 | |
|
8191 | 0 | opline = zend_emit_op(NULL, opcode, NULL, &default_node); |
8192 | 0 | SET_NODE(opline->result, &var_node); |
8193 | 0 | opline->op1.num = i + 1; |
8194 | |
|
8195 | 0 | uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0) |
8196 | 0 | | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0); |
8197 | 0 | ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags; |
8198 | 0 | if (opcode == ZEND_RECV) { |
8199 | 0 | opline->op2.num = type_ast ? |
8200 | 0 | ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY; |
8201 | 0 | } |
8202 | |
|
8203 | 0 | if (is_promoted) { |
8204 | 0 | const zend_op_array *active_op_array = CG(active_op_array); |
8205 | 0 | zend_class_entry *scope = active_op_array->scope; |
8206 | |
|
8207 | 0 | bool is_ctor = |
8208 | 0 | scope && zend_is_constructor(active_op_array->function_name); |
8209 | 0 | if (!is_ctor) { |
8210 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8211 | 0 | "Cannot declare promoted property outside a constructor"); |
8212 | 0 | } |
8213 | 0 | if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT) |
8214 | 0 | || (scope->ce_flags & ZEND_ACC_INTERFACE)) { |
8215 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8216 | 0 | "Cannot declare promoted property in an abstract constructor"); |
8217 | 0 | } |
8218 | 0 | if (is_variadic) { |
8219 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8220 | 0 | "Cannot declare variadic promoted property"); |
8221 | 0 | } |
8222 | 0 | if (zend_hash_exists(&scope->properties_info, name)) { |
8223 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
8224 | 0 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8225 | 0 | } |
8226 | 0 | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) { |
8227 | 0 | zend_string *str = zend_type_to_string(arg_info->type); |
8228 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8229 | 0 | "Property %s::$%s cannot have type %s", |
8230 | 0 | ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
8231 | 0 | } |
8232 | | |
8233 | 0 | if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
8234 | 0 | property_flags |= ZEND_ACC_READONLY; |
8235 | 0 | } |
8236 | | |
8237 | | /* Recompile the type, as it has different memory management requirements. */ |
8238 | 0 | zend_type type = ZEND_TYPE_INIT_NONE(0); |
8239 | 0 | if (type_ast) { |
8240 | 0 | type = zend_compile_typename(type_ast); |
8241 | 0 | } |
8242 | | |
8243 | | /* Don't give the property an explicit default value. For typed properties this means |
8244 | | * uninitialized, for untyped properties it means an implicit null default value. |
8245 | | * Properties with hooks get an implicit default value of undefined until inheritance, |
8246 | | * where it is changed to null only once we know it is not virtual. If we were to set it |
8247 | | * here, we couldn't verify that a true virtual property must not have an explicit |
8248 | | * default value. */ |
8249 | 0 | zval default_value; |
8250 | 0 | if (ZEND_TYPE_IS_SET(type) || hooks_ast) { |
8251 | 0 | ZVAL_UNDEF(&default_value); |
8252 | 0 | } else { |
8253 | 0 | if (property_flags & ZEND_ACC_READONLY) { |
8254 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
8255 | 0 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8256 | 0 | } |
8257 | | |
8258 | 0 | ZVAL_NULL(&default_value); |
8259 | 0 | } |
8260 | | |
8261 | 0 | zend_string *doc_comment = |
8262 | 0 | doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8263 | 0 | zend_property_info *prop = zend_declare_typed_property( |
8264 | 0 | scope, name, &default_value, |
8265 | 0 | property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED, |
8266 | 0 | doc_comment, type); |
8267 | 0 | if (hooks_ast) { |
8268 | 0 | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
8269 | 0 | zend_compile_property_hooks(prop, name, type_ast, hooks); |
8270 | 0 | } |
8271 | 0 | if (attributes_ast) { |
8272 | 0 | zend_compile_attributes( |
8273 | 0 | &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER); |
8274 | |
|
8275 | 0 | zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1); |
8276 | 0 | if (override_attribute) { |
8277 | 0 | prop->flags |= ZEND_ACC_OVERRIDE; |
8278 | 0 | } |
8279 | 0 | } |
8280 | 0 | } |
8281 | 0 | } |
8282 | | |
8283 | | /* These are assigned at the end to avoid uninitialized memory in case of an error */ |
8284 | 0 | op_array->num_args = list->children; |
8285 | 0 | op_array->arg_info = arg_infos; |
8286 | | |
8287 | | /* Don't count the variadic argument */ |
8288 | 0 | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8289 | 0 | op_array->num_args--; |
8290 | 0 | } |
8291 | 0 | zend_set_function_arg_flags((zend_function*)op_array); |
8292 | |
|
8293 | 0 | for (i = 0; i < list->children; i++) { |
8294 | 0 | zend_ast *param_ast = list->child[i]; |
8295 | 0 | zend_ast *hooks_ast = param_ast->child[5]; |
8296 | 0 | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8297 | 0 | uint32_t flags = param_ast->attr & promotion_flags; |
8298 | 0 | bool is_promoted = flags || hooks_ast; |
8299 | 0 | if (!is_promoted) { |
8300 | 0 | continue; |
8301 | 0 | } |
8302 | | |
8303 | 0 | CG(zend_lineno) = param_ast->lineno; |
8304 | | |
8305 | | /* Emit $this->prop = $prop for promoted properties. */ |
8306 | 0 | zend_string *name = zend_ast_get_str(param_ast->child[1]); |
8307 | 0 | znode name_node, value_node; |
8308 | 0 | name_node.op_type = IS_CONST; |
8309 | 0 | ZVAL_STR_COPY(&name_node.u.constant, name); |
8310 | 0 | value_node.op_type = IS_CV; |
8311 | 0 | value_node.u.op.var = lookup_cv(name); |
8312 | |
|
8313 | 0 | zend_op *opline = zend_emit_op(NULL, |
8314 | 0 | is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node); |
8315 | 0 | opline->extended_value = zend_alloc_cache_slots(3); |
8316 | 0 | zend_emit_op_data(&value_node); |
8317 | 0 | } |
8318 | 0 | } |
8319 | | /* }}} */ |
8320 | | |
8321 | | static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */ |
8322 | 0 | { |
8323 | 0 | const zend_ast_list *list = zend_ast_get_list(uses_ast); |
8324 | 0 | uint32_t i; |
8325 | |
|
8326 | 0 | if (!list->children) { |
8327 | 0 | return; |
8328 | 0 | } |
8329 | | |
8330 | 0 | if (!op_array->static_variables) { |
8331 | 0 | op_array->static_variables = zend_new_array(8); |
8332 | 0 | } |
8333 | |
|
8334 | 0 | for (i = 0; i < list->children; ++i) { |
8335 | 0 | zend_ast *var_name_ast = list->child[i]; |
8336 | 0 | zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast)); |
8337 | 0 | uint32_t mode = var_name_ast->attr; |
8338 | 0 | zend_op *opline; |
8339 | 0 | zval *value; |
8340 | |
|
8341 | 0 | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8342 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable"); |
8343 | 0 | } |
8344 | | |
8345 | 0 | if (zend_is_auto_global(var_name)) { |
8346 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable"); |
8347 | 0 | } |
8348 | | |
8349 | 0 | value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8350 | 0 | if (!value) { |
8351 | 0 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, |
8352 | 0 | "Cannot use variable $%S twice", var_name); |
8353 | 0 | } |
8354 | | |
8355 | 0 | CG(zend_lineno) = zend_ast_get_lineno(var_name_ast); |
8356 | |
|
8357 | 0 | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8358 | 0 | opline->op2_type = IS_CV; |
8359 | 0 | opline->op2.var = lookup_cv(var_name); |
8360 | 0 | opline->extended_value = |
8361 | 0 | (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode; |
8362 | 0 | } |
8363 | 0 | } |
8364 | | /* }}} */ |
8365 | | |
8366 | | typedef struct { |
8367 | | HashTable uses; |
8368 | | bool varvars_used; |
8369 | | } closure_info; |
8370 | | |
8371 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast); |
8372 | | |
8373 | 0 | static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) { |
8374 | 0 | if (!ast) { |
8375 | 0 | return; |
8376 | 0 | } |
8377 | | |
8378 | 0 | if (ast->kind == ZEND_AST_VAR) { |
8379 | 0 | zend_ast *name_ast = ast->child[0]; |
8380 | 0 | if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) { |
8381 | 0 | zend_string *name = zend_ast_get_str(name_ast); |
8382 | 0 | if (zend_is_auto_global(name)) { |
8383 | | /* These is no need to explicitly import auto-globals. */ |
8384 | 0 | return; |
8385 | 0 | } |
8386 | | |
8387 | 0 | if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8388 | | /* $this does not need to be explicitly imported. */ |
8389 | 0 | return; |
8390 | 0 | } |
8391 | | |
8392 | 0 | zend_hash_add_empty_element(&info->uses, name); |
8393 | 0 | } else { |
8394 | 0 | info->varvars_used = true; |
8395 | 0 | find_implicit_binds_recursively(info, name_ast); |
8396 | 0 | } |
8397 | 0 | } else if (zend_ast_is_list(ast)) { |
8398 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
8399 | 0 | uint32_t i; |
8400 | 0 | for (i = 0; i < list->children; i++) { |
8401 | 0 | find_implicit_binds_recursively(info, list->child[i]); |
8402 | 0 | } |
8403 | 0 | } else if (ast->kind == ZEND_AST_CLOSURE) { |
8404 | | /* For normal closures add the use() list. */ |
8405 | 0 | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8406 | 0 | zend_ast *uses_ast = closure_ast->child[1]; |
8407 | 0 | if (uses_ast) { |
8408 | 0 | const zend_ast_list *uses_list = zend_ast_get_list(uses_ast); |
8409 | 0 | uint32_t i; |
8410 | 0 | for (i = 0; i < uses_list->children; i++) { |
8411 | 0 | zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i])); |
8412 | 0 | } |
8413 | 0 | } |
8414 | 0 | } else if (ast->kind == ZEND_AST_ARROW_FUNC) { |
8415 | | /* For arrow functions recursively check the expression. */ |
8416 | 0 | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8417 | 0 | closure_info inner_info; |
8418 | 0 | find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]); |
8419 | 0 | if (inner_info.varvars_used) { |
8420 | 0 | info->varvars_used = true; |
8421 | 0 | } |
8422 | 0 | if (zend_hash_num_elements(&inner_info.uses)) { |
8423 | 0 | zend_hash_copy(&info->uses, &inner_info.uses, NULL); |
8424 | 0 | } |
8425 | 0 | zend_hash_destroy(&inner_info.uses); |
8426 | 0 | } else if (!zend_ast_is_special(ast)) { |
8427 | 0 | uint32_t i, children = zend_ast_get_num_children(ast); |
8428 | 0 | for (i = 0; i < children; i++) { |
8429 | 0 | find_implicit_binds_recursively(info, ast->child[i]); |
8430 | 0 | } |
8431 | 0 | } |
8432 | 0 | } |
8433 | | |
8434 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast) |
8435 | 0 | { |
8436 | 0 | const zend_ast_list *param_list = zend_ast_get_list(params_ast); |
8437 | 0 | uint32_t i; |
8438 | |
|
8439 | 0 | zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0); |
8440 | 0 | info->varvars_used = false; |
8441 | |
|
8442 | 0 | find_implicit_binds_recursively(info, stmt_ast); |
8443 | | |
8444 | | /* Remove variables that are parameters */ |
8445 | 0 | for (i = 0; i < param_list->children; i++) { |
8446 | 0 | const zend_ast *param_ast = param_list->child[i]; |
8447 | 0 | zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1])); |
8448 | 0 | } |
8449 | 0 | } |
8450 | | |
8451 | | static void compile_implicit_lexical_binds( |
8452 | | const closure_info *info, znode *closure, zend_op_array *op_array) |
8453 | 0 | { |
8454 | 0 | zend_string *var_name; |
8455 | 0 | zend_op *opline; |
8456 | | |
8457 | | /* TODO We might want to use a special binding mode if varvars_used is set. */ |
8458 | 0 | if (zend_hash_num_elements(&info->uses) == 0) { |
8459 | 0 | return; |
8460 | 0 | } |
8461 | | |
8462 | 0 | if (!op_array->static_variables) { |
8463 | 0 | op_array->static_variables = zend_new_array(8); |
8464 | 0 | } |
8465 | |
|
8466 | 0 | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8467 | 0 | zval *value = zend_hash_add( |
8468 | 0 | op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8469 | 0 | uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData); |
8470 | |
|
8471 | 0 | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8472 | 0 | opline->op2_type = IS_CV; |
8473 | 0 | opline->op2.var = lookup_cv(var_name); |
8474 | 0 | opline->extended_value = offset | ZEND_BIND_IMPLICIT; |
8475 | 0 | } ZEND_HASH_FOREACH_END(); |
8476 | 0 | } |
8477 | | |
8478 | | static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */ |
8479 | 0 | { |
8480 | 0 | const zend_op_array *op_array = CG(active_op_array); |
8481 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
8482 | 0 | uint32_t i; |
8483 | |
|
8484 | 0 | for (i = 0; i < list->children; ++i) { |
8485 | 0 | uint32_t mode = ZEND_BIND_EXPLICIT; |
8486 | 0 | zend_ast *var_ast = list->child[i]; |
8487 | 0 | zend_string *var_name = zend_ast_get_str(var_ast); |
8488 | 0 | zval zv; |
8489 | 0 | ZVAL_NULL(&zv); |
8490 | |
|
8491 | 0 | { |
8492 | 0 | int i; |
8493 | 0 | for (i = 0; i < op_array->last_var; i++) { |
8494 | 0 | if (zend_string_equals(op_array->vars[i], var_name)) { |
8495 | 0 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, |
8496 | 0 | "Cannot use lexical variable $%S as a parameter name", var_name); |
8497 | 0 | } |
8498 | 0 | } |
8499 | 0 | } |
8500 | | |
8501 | 0 | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
8502 | |
|
8503 | 0 | if (var_ast->attr) { |
8504 | 0 | mode |= ZEND_BIND_REF; |
8505 | 0 | } |
8506 | |
|
8507 | 0 | zend_compile_static_var_common(var_name, &zv, mode); |
8508 | 0 | } |
8509 | 0 | } |
8510 | | /* }}} */ |
8511 | | |
8512 | | static void zend_compile_implicit_closure_uses(const closure_info *info) |
8513 | 0 | { |
8514 | 0 | zend_string *var_name; |
8515 | 0 | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8516 | 0 | zval zv; |
8517 | 0 | ZVAL_NULL(&zv); |
8518 | 0 | zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT); |
8519 | 0 | } ZEND_HASH_FOREACH_END(); |
8520 | 0 | } |
8521 | | |
8522 | 0 | static void add_stringable_interface(zend_class_entry *ce) { |
8523 | 0 | for (uint32_t i = 0; i < ce->num_interfaces; i++) { |
8524 | 0 | if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) { |
8525 | | /* Interface already explicitly implemented */ |
8526 | 0 | return; |
8527 | 0 | } |
8528 | 0 | } |
8529 | | |
8530 | 0 | ce->num_interfaces++; |
8531 | 0 | ce->interface_names = |
8532 | 0 | erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
8533 | | // TODO: Add known interned strings instead? |
8534 | 0 | ce->interface_names[ce->num_interfaces - 1].name = |
8535 | 0 | ZSTR_INIT_LITERAL("Stringable", 0); |
8536 | 0 | ce->interface_names[ce->num_interfaces - 1].lc_name = |
8537 | 0 | ZSTR_INIT_LITERAL("stringable", 0); |
8538 | 0 | } |
8539 | | |
8540 | | static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */ |
8541 | 0 | { |
8542 | 0 | zend_class_entry *ce = CG(active_class_entry); |
8543 | 0 | bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0; |
8544 | 0 | uint32_t fn_flags = op_array->fn_flags; |
8545 | |
|
8546 | 0 | zend_string *lcname; |
8547 | |
|
8548 | 0 | if (fn_flags & ZEND_ACC_READONLY) { |
8549 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier"); |
8550 | 0 | } |
8551 | |
|
8552 | 0 | if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) { |
8553 | 0 | zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes"); |
8554 | 0 | } |
8555 | |
|
8556 | 0 | if ((fn_flags & ZEND_ACC_ABSTRACT) |
8557 | 0 | && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) { |
8558 | | // Don't say that the class should be declared abstract if it is |
8559 | | // anonymous or an enum and can't be abstract |
8560 | 0 | if (ce->ce_flags & ZEND_ACC_ANON_CLASS) { |
8561 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract", |
8562 | 0 | ZSTR_VAL(name)); |
8563 | 0 | } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) { |
8564 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract", |
8565 | 0 | zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8566 | 0 | } else { |
8567 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract", |
8568 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8569 | 0 | } |
8570 | 0 | } |
8571 | | |
8572 | 0 | if (in_interface) { |
8573 | 0 | if (!(fn_flags & ZEND_ACC_PUBLIC)) { |
8574 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method " |
8575 | 0 | "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8576 | 0 | } |
8577 | 0 | if (fn_flags & ZEND_ACC_FINAL) { |
8578 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Interface method " |
8579 | 0 | "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8580 | 0 | } |
8581 | 0 | op_array->fn_flags |= ZEND_ACC_ABSTRACT; |
8582 | 0 | } |
8583 | | |
8584 | 0 | if (op_array->fn_flags & ZEND_ACC_ABSTRACT) { |
8585 | 0 | if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8586 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private", |
8587 | 0 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8588 | 0 | } |
8589 | | |
8590 | 0 | if (has_body) { |
8591 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body", |
8592 | 0 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8593 | 0 | } |
8594 | | |
8595 | 0 | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
8596 | 0 | } else if (!has_body) { |
8597 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body", |
8598 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8599 | 0 | } |
8600 | | |
8601 | 0 | op_array->scope = ce; |
8602 | 0 | op_array->function_name = zend_string_copy(name); |
8603 | |
|
8604 | 0 | lcname = zend_string_tolower(name); |
8605 | 0 | lcname = zend_new_interned_string(lcname); |
8606 | |
|
8607 | 0 | if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) { |
8608 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", |
8609 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8610 | 0 | } |
8611 | | |
8612 | 0 | zend_add_magic_method(ce, (zend_function *) op_array, lcname); |
8613 | 0 | if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) |
8614 | 0 | && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8615 | 0 | add_stringable_interface(ce); |
8616 | 0 | } |
8617 | |
|
8618 | 0 | return lcname; |
8619 | 0 | } |
8620 | | /* }}} */ |
8621 | | |
8622 | 0 | static uint32_t zend_add_dynamic_func_def(zend_op_array *def) { |
8623 | 0 | zend_op_array *op_array = CG(active_op_array); |
8624 | 0 | uint32_t def_offset = op_array->num_dynamic_func_defs++; |
8625 | 0 | op_array->dynamic_func_defs = erealloc( |
8626 | 0 | op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *)); |
8627 | 0 | op_array->dynamic_func_defs[def_offset] = def; |
8628 | 0 | return def_offset; |
8629 | 0 | } |
8630 | | |
8631 | | enum func_decl_level { |
8632 | | FUNC_DECL_LEVEL_TOPLEVEL, |
8633 | | FUNC_DECL_LEVEL_NESTED, |
8634 | | FUNC_DECL_LEVEL_CONSTEXPR, |
8635 | | }; |
8636 | | |
8637 | | static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */ |
8638 | 0 | { |
8639 | 0 | zend_string *unqualified_name, *name, *lcname; |
8640 | 0 | zend_op *opline; |
8641 | |
|
8642 | 0 | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8643 | 0 | zend_string *filename = op_array->filename; |
8644 | 0 | uint32_t start_lineno = decl->start_lineno; |
8645 | |
|
8646 | 0 | zend_string *class = zend_empty_string; |
8647 | 0 | zend_string *separator = zend_empty_string; |
8648 | 0 | zend_string *function = filename; |
8649 | 0 | const char *parens = ""; |
8650 | |
|
8651 | 0 | if (CG(active_op_array) && CG(active_op_array)->function_name) { |
8652 | 0 | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
8653 | | /* If the parent function is a closure, don't redundantly |
8654 | | * add the classname and parentheses. |
8655 | | */ |
8656 | 0 | function = CG(active_op_array)->function_name; |
8657 | 0 | } else { |
8658 | 0 | function = CG(active_op_array)->function_name; |
8659 | 0 | parens = "()"; |
8660 | |
|
8661 | 0 | if (CG(active_class_entry) && CG(active_class_entry)->name) { |
8662 | 0 | class = CG(active_class_entry)->name; |
8663 | 0 | separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM); |
8664 | 0 | } |
8665 | 0 | } |
8666 | 0 | } |
8667 | |
|
8668 | 0 | unqualified_name = zend_strpprintf_unchecked( |
8669 | 0 | 0, |
8670 | 0 | "{closure:%S%S%S%s:%" PRIu32 "}", |
8671 | 0 | class, |
8672 | 0 | separator, |
8673 | 0 | function, |
8674 | 0 | parens, |
8675 | 0 | start_lineno |
8676 | 0 | ); |
8677 | |
|
8678 | 0 | op_array->function_name = name = unqualified_name; |
8679 | 0 | } else { |
8680 | 0 | unqualified_name = decl->name; |
8681 | 0 | op_array->function_name = name = zend_prefix_with_ns(unqualified_name); |
8682 | 0 | } |
8683 | |
|
8684 | 0 | lcname = zend_string_tolower(name); |
8685 | |
|
8686 | 0 | if (FC(imports_function)) { |
8687 | 0 | const zend_string *import_name = |
8688 | 0 | zend_hash_find_ptr_lc(FC(imports_function), unqualified_name); |
8689 | 0 | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
8690 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)", |
8691 | 0 | ZSTR_VAL(name)); |
8692 | 0 | } |
8693 | 0 | } |
8694 | | |
8695 | 0 | if (zend_string_equals_literal(lcname, "__autoload")) { |
8696 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8697 | 0 | "__autoload() is no longer supported, use spl_autoload_register() instead"); |
8698 | 0 | } |
8699 | | |
8700 | 0 | if (zend_string_equals_literal_ci(unqualified_name, "assert")) { |
8701 | 0 | zend_error(E_COMPILE_ERROR, |
8702 | 0 | "Defining a custom assert() function is not allowed, " |
8703 | 0 | "as the function has special semantics"); |
8704 | 0 | } |
8705 | |
|
8706 | 0 | zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION); |
8707 | 0 | switch (level) { |
8708 | 0 | case FUNC_DECL_LEVEL_NESTED: { |
8709 | 0 | uint32_t func_ref = zend_add_dynamic_func_def(op_array); |
8710 | 0 | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8711 | 0 | opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL); |
8712 | 0 | opline->op2.num = func_ref; |
8713 | 0 | } else { |
8714 | 0 | opline = get_next_op(); |
8715 | 0 | opline->opcode = ZEND_DECLARE_FUNCTION; |
8716 | 0 | opline->op1_type = IS_CONST; |
8717 | 0 | LITERAL_STR(opline->op1, zend_string_copy(lcname)); |
8718 | 0 | opline->op2.num = func_ref; |
8719 | 0 | } |
8720 | 0 | break; |
8721 | 0 | } |
8722 | 0 | case FUNC_DECL_LEVEL_CONSTEXPR: |
8723 | 0 | case FUNC_DECL_LEVEL_TOPLEVEL: |
8724 | | /* Nothing to do. */ |
8725 | 0 | break; |
8726 | 0 | } |
8727 | 0 | return lcname; |
8728 | 0 | } |
8729 | | /* }}} */ |
8730 | | |
8731 | | static zend_op_array *zend_compile_func_decl_ex( |
8732 | | znode *result, zend_ast *ast, enum func_decl_level level, |
8733 | | zend_string *property_info_name, |
8734 | | zend_property_hook_kind hook_kind |
8735 | 0 | ) { |
8736 | 0 | zend_ast_decl *decl = (zend_ast_decl *) ast; |
8737 | 0 | zend_ast *params_ast = decl->child[0]; |
8738 | 0 | zend_ast *uses_ast = decl->child[1]; |
8739 | 0 | zend_ast *stmt_ast = decl->child[2]; |
8740 | 0 | zend_ast *return_type_ast = decl->child[3]; |
8741 | 0 | bool is_method = decl->kind == ZEND_AST_METHOD; |
8742 | 0 | zend_string *lcname = NULL; |
8743 | 0 | bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK; |
8744 | |
|
8745 | 0 | zend_class_entry *orig_class_entry = CG(active_class_entry); |
8746 | 0 | zend_op_array *orig_op_array = CG(active_op_array); |
8747 | 0 | zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array)); |
8748 | 0 | zend_oparray_context orig_oparray_context; |
8749 | 0 | closure_info info; |
8750 | |
|
8751 | 0 | init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE); |
8752 | |
|
8753 | 0 | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
8754 | 0 | op_array->fn_flags |= ZEND_ACC_PRELOADED; |
8755 | 0 | } |
8756 | |
|
8757 | 0 | op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES); |
8758 | 0 | op_array->fn_flags |= decl->flags; |
8759 | 0 | op_array->line_start = decl->start_lineno; |
8760 | 0 | op_array->line_end = decl->end_lineno; |
8761 | 0 | if (decl->doc_comment) { |
8762 | 0 | op_array->doc_comment = zend_string_copy(decl->doc_comment); |
8763 | 0 | } |
8764 | |
|
8765 | 0 | if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) { |
8766 | 0 | op_array->fn_flags |= ZEND_ACC_CLOSURE; |
8767 | 0 | } |
8768 | |
|
8769 | 0 | if (is_hook) { |
8770 | 0 | zend_class_entry *ce = CG(active_class_entry); |
8771 | 0 | op_array->scope = ce; |
8772 | 0 | op_array->function_name = zend_string_copy(decl->name); |
8773 | 0 | } else if (is_method) { |
8774 | 0 | bool has_body = stmt_ast != NULL; |
8775 | 0 | lcname = zend_begin_method_decl(op_array, decl->name, has_body); |
8776 | 0 | } else { |
8777 | 0 | lcname = zend_begin_func_decl(result, op_array, decl, level); |
8778 | 0 | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
8779 | 0 | find_implicit_binds(&info, params_ast, stmt_ast); |
8780 | 0 | compile_implicit_lexical_binds(&info, result, op_array); |
8781 | 0 | } else if (uses_ast) { |
8782 | 0 | zend_compile_closure_binding(result, op_array, uses_ast); |
8783 | 0 | } |
8784 | 0 | } |
8785 | |
|
8786 | 0 | CG(active_op_array) = op_array; |
8787 | |
|
8788 | 0 | zend_oparray_context_begin(&orig_oparray_context, op_array); |
8789 | 0 | CG(context).active_property_info_name = property_info_name; |
8790 | 0 | CG(context).active_property_hook_kind = hook_kind; |
8791 | |
|
8792 | 0 | if (decl->child[4]) { |
8793 | 0 | int target = ZEND_ATTRIBUTE_TARGET_FUNCTION; |
8794 | |
|
8795 | 0 | if (is_method || is_hook) { |
8796 | 0 | target = ZEND_ATTRIBUTE_TARGET_METHOD; |
8797 | 0 | } |
8798 | |
|
8799 | 0 | zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0); |
8800 | |
|
8801 | 0 | const zend_attribute *override_attribute = zend_get_attribute_str( |
8802 | 0 | op_array->attributes, |
8803 | 0 | "override", |
8804 | 0 | sizeof("override")-1 |
8805 | 0 | ); |
8806 | |
|
8807 | 0 | if (override_attribute) { |
8808 | 0 | op_array->fn_flags |= ZEND_ACC_OVERRIDE; |
8809 | 0 | } |
8810 | |
|
8811 | 0 | const zend_attribute *deprecated_attribute = zend_get_attribute_str( |
8812 | 0 | op_array->attributes, |
8813 | 0 | "deprecated", |
8814 | 0 | sizeof("deprecated")-1 |
8815 | 0 | ); |
8816 | |
|
8817 | 0 | if (deprecated_attribute) { |
8818 | 0 | op_array->fn_flags |= ZEND_ACC_DEPRECATED; |
8819 | 0 | } |
8820 | | |
8821 | | // ZEND_ACC_NODISCARD is added via an attribute validator |
8822 | 0 | } |
8823 | | |
8824 | | /* Do not leak the class scope into free standing functions, even if they are dynamically |
8825 | | * defined inside a class method. This is necessary for correct handling of magic constants. |
8826 | | * For example __CLASS__ should always be "" inside a free standing function. */ |
8827 | 0 | if (decl->kind == ZEND_AST_FUNC_DECL) { |
8828 | 0 | CG(active_class_entry) = NULL; |
8829 | 0 | } |
8830 | |
|
8831 | 0 | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
8832 | 0 | op_array->fn_flags |= ZEND_ACC_TOP_LEVEL; |
8833 | 0 | } |
8834 | |
|
8835 | 0 | { |
8836 | | /* Push a separator to the loop variable stack */ |
8837 | 0 | zend_loop_var dummy_var; |
8838 | 0 | dummy_var.opcode = ZEND_RETURN; |
8839 | |
|
8840 | 0 | zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var); |
8841 | 0 | } |
8842 | |
|
8843 | 0 | zend_compile_params(params_ast, return_type_ast, |
8844 | 0 | is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0); |
8845 | 0 | if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) { |
8846 | 0 | zend_mark_function_as_generator(); |
8847 | 0 | zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL); |
8848 | 0 | } |
8849 | 0 | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
8850 | 0 | zend_compile_implicit_closure_uses(&info); |
8851 | 0 | zend_hash_destroy(&info.uses); |
8852 | 0 | } else if (uses_ast) { |
8853 | 0 | zend_compile_closure_uses(uses_ast); |
8854 | 0 | } |
8855 | |
|
8856 | 0 | if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) { |
8857 | 0 | bool needs_return = true; |
8858 | 0 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
8859 | 0 | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
8860 | 0 | needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER); |
8861 | 0 | } |
8862 | 0 | if (needs_return) { |
8863 | 0 | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
8864 | 0 | decl->child[2] = stmt_ast; |
8865 | 0 | } |
8866 | 0 | } |
8867 | |
|
8868 | 0 | if (op_array->fn_flags & ZEND_ACC_NODISCARD) { |
8869 | | /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only |
8870 | | * if the method is not a hook; if it is a hook, then the validator |
8871 | | * will have returned an error message, even if the error message was |
8872 | | * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD |
8873 | | * flag should not have been added. */ |
8874 | 0 | ZEND_ASSERT(!is_hook); |
8875 | |
|
8876 | 0 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
8877 | 0 | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
8878 | 0 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) { |
8879 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8880 | 0 | "A void %s does not return a value, but #[\\NoDiscard] requires a return value", |
8881 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
8882 | 0 | } |
8883 | | |
8884 | 0 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
8885 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8886 | 0 | "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value", |
8887 | 0 | CG(active_class_entry) != NULL ? "method" : "function"); |
8888 | 0 | } |
8889 | 0 | } |
8890 | 0 | } |
8891 | | |
8892 | 0 | zend_compile_stmt(stmt_ast); |
8893 | |
|
8894 | 0 | if (is_method) { |
8895 | 0 | CG(zend_lineno) = decl->start_lineno; |
8896 | 0 | zend_check_magic_method_implementation( |
8897 | 0 | CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR); |
8898 | 0 | } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
8899 | | /* Only register the function after a successful compile */ |
8900 | 0 | if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) { |
8901 | 0 | CG(zend_lineno) = decl->start_lineno; |
8902 | 0 | do_bind_function_error(lcname, op_array, true); |
8903 | 0 | } |
8904 | 0 | } |
8905 | | |
8906 | | /* put the implicit return on the really last line */ |
8907 | 0 | CG(zend_lineno) = decl->end_lineno; |
8908 | |
|
8909 | 0 | zend_do_extended_stmt(NULL); |
8910 | 0 | zend_emit_final_return(false); |
8911 | |
|
8912 | 0 | pass_two(CG(active_op_array)); |
8913 | 0 | zend_oparray_context_end(&orig_oparray_context); |
8914 | | |
8915 | | /* Pop the loop variable stack separator */ |
8916 | 0 | zend_stack_del_top(&CG(loop_var_stack)); |
8917 | |
|
8918 | 0 | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
8919 | 0 | zend_observer_function_declared_notify(op_array, lcname); |
8920 | 0 | } |
8921 | |
|
8922 | 0 | if (lcname != NULL) { |
8923 | 0 | zend_string_release_ex(lcname, 0); |
8924 | 0 | } |
8925 | |
|
8926 | 0 | CG(active_op_array) = orig_op_array; |
8927 | 0 | CG(active_class_entry) = orig_class_entry; |
8928 | |
|
8929 | 0 | return op_array; |
8930 | 0 | } |
8931 | | |
8932 | | static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level) |
8933 | 0 | { |
8934 | 0 | return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1); |
8935 | 0 | } |
8936 | | |
8937 | 0 | zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) { |
8938 | 0 | if (zend_string_equals_literal_ci(name, "get")) { |
8939 | 0 | return ZEND_PROPERTY_HOOK_GET; |
8940 | 0 | } else if (zend_string_equals_literal_ci(name, "set")) { |
8941 | 0 | return ZEND_PROPERTY_HOOK_SET; |
8942 | 0 | } else { |
8943 | 0 | return (zend_property_hook_kind)-1; |
8944 | 0 | } |
8945 | 0 | } |
8946 | | |
8947 | | static void zend_compile_property_hooks( |
8948 | | zend_property_info *prop_info, zend_string *prop_name, |
8949 | | zend_ast *prop_type_ast, const zend_ast_list *hooks) |
8950 | 0 | { |
8951 | 0 | zend_class_entry *ce = CG(active_class_entry); |
8952 | |
|
8953 | 0 | if (prop_info->flags & ZEND_ACC_READONLY) { |
8954 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly"); |
8955 | 0 | } |
8956 | | |
8957 | 0 | if (hooks->children == 0) { |
8958 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty"); |
8959 | 0 | } |
8960 | | |
8961 | 0 | for (uint32_t i = 0; i < hooks->children; i++) { |
8962 | 0 | zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i]; |
8963 | 0 | zend_string *name = hook->name; |
8964 | 0 | zend_ast *stmt_ast = hook->child[2]; |
8965 | 0 | zend_ast **return_type_ast_ptr = NULL; |
8966 | 0 | zend_ast **value_type_ast_ptr = NULL; |
8967 | 0 | CG(zend_lineno) = hook->start_lineno; |
8968 | | |
8969 | | /* Non-private hooks are always public. This avoids having to copy the hook when inheriting |
8970 | | * hooks from protected properties to public ones. */ |
8971 | 0 | uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE; |
8972 | 0 | hook->flags |= hook_visibility; |
8973 | |
|
8974 | 0 | if (prop_info->flags & ZEND_ACC_STATIC) { |
8975 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property"); |
8976 | 0 | } |
8977 | 0 | if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) { |
8978 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private"); |
8979 | 0 | } |
8980 | 0 | if ((ce->ce_flags & ZEND_ACC_INTERFACE) |
8981 | 0 | || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) { |
8982 | 0 | hook->flags |= ZEND_ACC_ABSTRACT; |
8983 | |
|
8984 | 0 | if (stmt_ast) { |
8985 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body"); |
8986 | 0 | } |
8987 | 0 | if (hook->flags & ZEND_ACC_PRIVATE) { |
8988 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
8989 | 0 | "Property hook cannot be both abstract and private"); |
8990 | 0 | } |
8991 | 0 | if (hook->flags & ZEND_ACC_FINAL) { |
8992 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final"); |
8993 | 0 | } |
8994 | 0 | } else if (!stmt_ast) { |
8995 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body"); |
8996 | 0 | } |
8997 | | |
8998 | 0 | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name); |
8999 | 0 | if (hook_kind == (zend_property_hook_kind)-1) { |
9000 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9001 | 0 | "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"", |
9002 | 0 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9003 | 0 | } |
9004 | | |
9005 | 0 | if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
9006 | 0 | stmt_ast = stmt_ast->child[0]; |
9007 | 0 | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9008 | 0 | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
9009 | 0 | } else { |
9010 | 0 | ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET); |
9011 | 0 | stmt_ast = zend_ast_create(ZEND_AST_ASSIGN, |
9012 | 0 | zend_ast_create(ZEND_AST_PROP, |
9013 | 0 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))), |
9014 | 0 | zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))), |
9015 | 0 | stmt_ast); |
9016 | 0 | } |
9017 | 0 | stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast); |
9018 | 0 | hook->child[2] = stmt_ast; |
9019 | 0 | } |
9020 | |
|
9021 | 0 | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9022 | 0 | if (hook->child[0]) { |
9023 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list", |
9024 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9025 | 0 | } |
9026 | | |
9027 | 0 | hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); |
9028 | |
|
9029 | 0 | return_type_ast_ptr = &hook->child[3]; |
9030 | 0 | *return_type_ast_ptr = prop_type_ast; |
9031 | 0 | } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9032 | 0 | if (hook->child[0]) { |
9033 | 0 | const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]); |
9034 | 0 | if (param_list->children != 1) { |
9035 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters", |
9036 | 0 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9037 | 0 | } |
9038 | 0 | const zend_ast *value_param_ast = param_list->child[0]; |
9039 | 0 | if (value_param_ast->attr & ZEND_PARAM_REF) { |
9040 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference", |
9041 | 0 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9042 | 0 | } |
9043 | 0 | if (value_param_ast->attr & ZEND_PARAM_VARIADIC) { |
9044 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic", |
9045 | 0 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9046 | 0 | } |
9047 | 0 | if (value_param_ast->child[2]) { |
9048 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value", |
9049 | 0 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9050 | 0 | } |
9051 | 0 | if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) { |
9052 | 0 | zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name); |
9053 | 0 | } |
9054 | 0 | } else { |
9055 | 0 | zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE)); |
9056 | 0 | zend_ast *param = zend_ast_create( |
9057 | 0 | ZEND_AST_PARAM, prop_type_ast, param_name_ast, |
9058 | 0 | /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL, |
9059 | 0 | /* hooks */ NULL); |
9060 | 0 | value_type_ast_ptr = ¶m->child[0]; |
9061 | 0 | hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param); |
9062 | 0 | } |
9063 | 0 | zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE); |
9064 | 0 | return_type->attr = IS_VOID; |
9065 | 0 | hook->child[3] = return_type; |
9066 | 0 | } else { |
9067 | 0 | ZEND_UNREACHABLE(); |
9068 | 0 | } |
9069 | | |
9070 | 0 | hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name)); |
9071 | |
|
9072 | 0 | zend_function *func = (zend_function *) zend_compile_func_decl_ex( |
9073 | 0 | NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind); |
9074 | |
|
9075 | 0 | func->common.prop_info = prop_info; |
9076 | |
|
9077 | 0 | if (!prop_info->hooks) { |
9078 | 0 | prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9079 | 0 | memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9080 | 0 | } |
9081 | |
|
9082 | 0 | if (prop_info->hooks[hook_kind]) { |
9083 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9084 | 0 | "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name)); |
9085 | 0 | } |
9086 | 0 | prop_info->hooks[hook_kind] = func; |
9087 | |
|
9088 | 0 | if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9089 | 0 | switch (zend_verify_property_hook_variance(prop_info, func)) { |
9090 | 0 | case INHERITANCE_SUCCESS: |
9091 | 0 | break; |
9092 | 0 | case INHERITANCE_UNRESOLVED: |
9093 | 0 | ce->num_hooked_prop_variance_checks++; |
9094 | 0 | break; |
9095 | 0 | case INHERITANCE_ERROR: |
9096 | 0 | zend_hooked_property_variance_error(prop_info); |
9097 | 0 | case INHERITANCE_WARNING: |
9098 | 0 | ZEND_UNREACHABLE(); |
9099 | 0 | } |
9100 | 0 | } |
9101 | | |
9102 | 0 | zend_string_release(name); |
9103 | | /* Un-share type ASTs to avoid double-frees of zval nodes. */ |
9104 | 0 | if (return_type_ast_ptr) { |
9105 | 0 | *return_type_ast_ptr = NULL; |
9106 | 0 | } |
9107 | 0 | if (value_type_ast_ptr) { |
9108 | 0 | *value_type_ast_ptr = NULL; |
9109 | 0 | } |
9110 | 0 | } |
9111 | | |
9112 | 0 | ce->num_hooked_props++; |
9113 | | |
9114 | | /* See zend_link_hooked_object_iter(). */ |
9115 | 0 | #ifndef ZEND_OPCACHE_SHM_REATTACHMENT |
9116 | 0 | if (!ce->get_iterator) { |
9117 | | /* Will be removed again, in case of Iterator or IteratorAggregate. */ |
9118 | 0 | ce->get_iterator = zend_hooked_object_get_iterator; |
9119 | 0 | } |
9120 | 0 | #endif |
9121 | |
|
9122 | 0 | if (!prop_info->ce->parent_name) { |
9123 | 0 | zend_verify_hooked_property(ce, prop_info, prop_name); |
9124 | 0 | } |
9125 | 0 | } |
9126 | | |
9127 | | static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */ |
9128 | 0 | { |
9129 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
9130 | 0 | zend_class_entry *ce = CG(active_class_entry); |
9131 | 0 | uint32_t i, children = list->children; |
9132 | |
|
9133 | 0 | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9134 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name)); |
9135 | 0 | } |
9136 | | |
9137 | 0 | if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) { |
9138 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private"); |
9139 | 0 | } |
9140 | | |
9141 | 0 | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9142 | 0 | if (flags & ZEND_ACC_FINAL) { |
9143 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final"); |
9144 | 0 | } |
9145 | 0 | if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) { |
9146 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private"); |
9147 | 0 | } |
9148 | 0 | if (flags & ZEND_ACC_ABSTRACT) { |
9149 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9150 | 0 | "Property in interface cannot be explicitly abstract. " |
9151 | 0 | "All interface members are implicitly abstract"); |
9152 | 0 | } |
9153 | 0 | flags |= ZEND_ACC_ABSTRACT; |
9154 | 0 | } |
9155 | | |
9156 | 0 | for (i = 0; i < children; ++i) { |
9157 | 0 | zend_property_info *info; |
9158 | 0 | zend_ast *prop_ast = list->child[i]; |
9159 | 0 | zend_ast *name_ast = prop_ast->child[0]; |
9160 | 0 | zend_ast **value_ast_ptr = &prop_ast->child[1]; |
9161 | 0 | zend_ast *doc_comment_ast = prop_ast->child[2]; |
9162 | 0 | zend_ast *hooks_ast = prop_ast->child[3]; |
9163 | 0 | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9164 | 0 | zend_string *doc_comment = NULL; |
9165 | 0 | zval value_zv; |
9166 | 0 | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9167 | 0 | flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0; |
9168 | |
|
9169 | 0 | zend_string *old_active_property_info_name = CG(context).active_property_info_name; |
9170 | 0 | CG(context).active_property_info_name = name; |
9171 | |
|
9172 | 0 | if (!hooks_ast) { |
9173 | 0 | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9174 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9175 | 0 | "Interfaces may only include hooked properties"); |
9176 | 0 | } |
9177 | 0 | if (flags & ZEND_ACC_ABSTRACT) { |
9178 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9179 | 0 | "Only hooked properties may be declared abstract"); |
9180 | 0 | } |
9181 | 0 | } |
9182 | 0 | if ((flags & ZEND_ACC_ABSTRACT)) { |
9183 | 0 | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
9184 | 0 | } |
9185 | |
|
9186 | 0 | if (type_ast) { |
9187 | 0 | type = zend_compile_typename(type_ast); |
9188 | |
|
9189 | 0 | if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) { |
9190 | 0 | zend_string *str = zend_type_to_string(type); |
9191 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9192 | 0 | "Property %s::$%s cannot have type %s", |
9193 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9194 | 0 | } |
9195 | 0 | } |
9196 | | |
9197 | | /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */ |
9198 | 0 | if (doc_comment_ast) { |
9199 | 0 | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
9200 | 0 | } |
9201 | |
|
9202 | 0 | if (zend_hash_exists(&ce->properties_info, name)) { |
9203 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
9204 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9205 | 0 | } |
9206 | | |
9207 | 0 | if (*value_ast_ptr) { |
9208 | 0 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9209 | |
|
9210 | 0 | if (ZEND_TYPE_IS_SET(type) && !Z_CONSTANT(value_zv) |
9211 | 0 | && !zend_is_valid_default_value(type, &value_zv)) { |
9212 | 0 | zend_string *str = zend_type_to_string(type); |
9213 | 0 | if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) { |
9214 | 0 | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
9215 | 0 | zend_string *nullable_str = zend_type_to_string(type); |
9216 | |
|
9217 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9218 | 0 | "Default value for property of type %s may not be null. " |
9219 | 0 | "Use the nullable type %s to allow null default value", |
9220 | 0 | ZSTR_VAL(str), ZSTR_VAL(nullable_str)); |
9221 | 0 | } else { |
9222 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9223 | 0 | "Cannot use %s as default value for property %s::$%s of type %s", |
9224 | 0 | zend_zval_value_name(&value_zv), |
9225 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9226 | 0 | } |
9227 | 0 | } |
9228 | 0 | } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) { |
9229 | 0 | ZVAL_NULL(&value_zv); |
9230 | 0 | } else { |
9231 | 0 | ZVAL_UNDEF(&value_zv); |
9232 | 0 | } |
9233 | | |
9234 | 0 | if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
9235 | 0 | flags |= ZEND_ACC_READONLY; |
9236 | 0 | } |
9237 | |
|
9238 | 0 | if (flags & ZEND_ACC_READONLY) { |
9239 | 0 | if (!ZEND_TYPE_IS_SET(type)) { |
9240 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
9241 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9242 | 0 | } |
9243 | 0 | if (!Z_ISUNDEF(value_zv)) { |
9244 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9245 | 0 | "Readonly property %s::$%s cannot have default value", |
9246 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9247 | 0 | } |
9248 | 0 | if (flags & ZEND_ACC_STATIC) { |
9249 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9250 | 0 | "Static property %s::$%s cannot be readonly", |
9251 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9252 | 0 | } |
9253 | 0 | } |
9254 | | |
9255 | 0 | info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type); |
9256 | |
|
9257 | 0 | if (hooks_ast) { |
9258 | 0 | zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast)); |
9259 | 0 | } |
9260 | |
|
9261 | 0 | if (attr_ast) { |
9262 | 0 | zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0); |
9263 | |
|
9264 | 0 | const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1); |
9265 | 0 | if (override_attribute) { |
9266 | 0 | info->flags |= ZEND_ACC_OVERRIDE; |
9267 | 0 | } |
9268 | 0 | } |
9269 | |
|
9270 | 0 | CG(context).active_property_info_name = old_active_property_info_name; |
9271 | 0 | } |
9272 | 0 | } |
9273 | | /* }}} */ |
9274 | | |
9275 | | static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */ |
9276 | 0 | { |
9277 | 0 | zend_ast *type_ast = ast->child[0]; |
9278 | 0 | zend_ast *prop_ast = ast->child[1]; |
9279 | 0 | zend_ast *attr_ast = ast->child[2]; |
9280 | |
|
9281 | 0 | zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast); |
9282 | 0 | } |
9283 | | /* }}} */ |
9284 | | |
9285 | | static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */ |
9286 | 0 | { |
9287 | 0 | if (attr & ZEND_ACC_STATIC) { |
9288 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias"); |
9289 | 0 | } else if (attr & ZEND_ACC_ABSTRACT) { |
9290 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias"); |
9291 | 0 | } |
9292 | 0 | } |
9293 | | /* }}} */ |
9294 | | |
9295 | | static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast) |
9296 | 0 | { |
9297 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
9298 | 0 | zend_class_entry *ce = CG(active_class_entry); |
9299 | 0 | uint32_t i, children = list->children; |
9300 | |
|
9301 | 0 | for (i = 0; i < children; ++i) { |
9302 | 0 | zend_class_constant *c; |
9303 | 0 | zend_ast *const_ast = list->child[i]; |
9304 | 0 | zend_ast *name_ast = const_ast->child[0]; |
9305 | 0 | zend_ast **value_ast_ptr = &const_ast->child[1]; |
9306 | 0 | zend_ast *doc_comment_ast = const_ast->child[2]; |
9307 | 0 | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9308 | 0 | zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
9309 | 0 | zval value_zv; |
9310 | 0 | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9311 | |
|
9312 | 0 | if (type_ast) { |
9313 | 0 | type = zend_compile_typename(type_ast); |
9314 | |
|
9315 | 0 | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9316 | |
|
9317 | 0 | if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) { |
9318 | 0 | zend_string *type_str = zend_type_to_string(type); |
9319 | |
|
9320 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s", |
9321 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9322 | 0 | } |
9323 | 0 | } |
9324 | | |
9325 | 0 | if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) { |
9326 | 0 | zend_error_noreturn( |
9327 | 0 | E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes", |
9328 | 0 | ZSTR_VAL(ce->name), ZSTR_VAL(name) |
9329 | 0 | ); |
9330 | 0 | } |
9331 | | |
9332 | 0 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9333 | |
|
9334 | 0 | if (!Z_CONSTANT(value_zv) && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) { |
9335 | 0 | zend_string *type_str = zend_type_to_string(type); |
9336 | |
|
9337 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s", |
9338 | 0 | zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9339 | 0 | } |
9340 | | |
9341 | 0 | c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type); |
9342 | |
|
9343 | 0 | if (attr_ast) { |
9344 | 0 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
9345 | |
|
9346 | 0 | const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
9347 | |
|
9348 | 0 | if (deprecated) { |
9349 | 0 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
9350 | | /* For deprecated constants, we need to flag the zval for recursion |
9351 | | * detection. Make sure the zval is separated out of shm. */ |
9352 | 0 | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
9353 | 0 | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
9354 | 0 | } |
9355 | 0 | } |
9356 | 0 | } |
9357 | 0 | } |
9358 | | |
9359 | | static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */ |
9360 | 0 | { |
9361 | 0 | zend_ast *const_ast = ast->child[0]; |
9362 | 0 | zend_ast *attr_ast = ast->child[1]; |
9363 | 0 | zend_ast *type_ast = ast->child[2]; |
9364 | |
|
9365 | 0 | zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast); |
9366 | 0 | } |
9367 | | /* }}} */ |
9368 | | |
9369 | | static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */ |
9370 | 0 | { |
9371 | 0 | zend_ast *class_ast = ast->child[0]; |
9372 | 0 | zend_ast *method_ast = ast->child[1]; |
9373 | |
|
9374 | 0 | method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast)); |
9375 | |
|
9376 | 0 | if (class_ast) { |
9377 | 0 | method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name"); |
9378 | 0 | } else { |
9379 | 0 | method_ref->class_name = NULL; |
9380 | 0 | } |
9381 | 0 | } |
9382 | | /* }}} */ |
9383 | | |
9384 | | static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */ |
9385 | 0 | { |
9386 | 0 | const zend_ast *method_ref_ast = ast->child[0]; |
9387 | 0 | zend_ast *insteadof_ast = ast->child[1]; |
9388 | 0 | const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast); |
9389 | 0 | uint32_t i; |
9390 | |
|
9391 | 0 | zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*)); |
9392 | 0 | zend_compile_method_ref(method_ref_ast, &precedence->trait_method); |
9393 | 0 | precedence->num_excludes = insteadof_list->children; |
9394 | |
|
9395 | 0 | for (i = 0; i < insteadof_list->children; ++i) { |
9396 | 0 | zend_ast *name_ast = insteadof_list->child[i]; |
9397 | 0 | precedence->exclude_class_names[i] = |
9398 | 0 | zend_resolve_const_class_name_reference(name_ast, "trait name"); |
9399 | 0 | } |
9400 | |
|
9401 | 0 | zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence); |
9402 | 0 | } |
9403 | | /* }}} */ |
9404 | | |
9405 | | static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */ |
9406 | 0 | { |
9407 | 0 | const zend_ast *method_ref_ast = ast->child[0]; |
9408 | 0 | zend_ast *alias_ast = ast->child[1]; |
9409 | 0 | uint32_t modifiers = ast->attr; |
9410 | |
|
9411 | 0 | zend_trait_alias *alias; |
9412 | |
|
9413 | 0 | zend_check_trait_alias_modifiers(modifiers); |
9414 | |
|
9415 | 0 | alias = emalloc(sizeof(zend_trait_alias)); |
9416 | 0 | zend_compile_method_ref(method_ref_ast, &alias->trait_method); |
9417 | 0 | alias->modifiers = modifiers; |
9418 | |
|
9419 | 0 | if (alias_ast) { |
9420 | 0 | alias->alias = zend_string_copy(zend_ast_get_str(alias_ast)); |
9421 | 0 | } else { |
9422 | 0 | alias->alias = NULL; |
9423 | 0 | } |
9424 | |
|
9425 | 0 | zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias); |
9426 | 0 | } |
9427 | | /* }}} */ |
9428 | | |
9429 | | static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */ |
9430 | 0 | { |
9431 | 0 | const zend_ast_list *traits = zend_ast_get_list(ast->child[0]); |
9432 | 0 | zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL; |
9433 | 0 | zend_class_entry *ce = CG(active_class_entry); |
9434 | 0 | uint32_t i; |
9435 | |
|
9436 | 0 | ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children)); |
9437 | |
|
9438 | 0 | for (i = 0; i < traits->children; ++i) { |
9439 | 0 | zend_ast *trait_ast = traits->child[i]; |
9440 | |
|
9441 | 0 | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9442 | 0 | zend_string *name = zend_ast_get_str(trait_ast); |
9443 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. " |
9444 | 0 | "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name)); |
9445 | 0 | } |
9446 | | |
9447 | 0 | ce->trait_names[ce->num_traits].name = |
9448 | 0 | zend_resolve_const_class_name_reference(trait_ast, "trait name"); |
9449 | 0 | ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name); |
9450 | 0 | ce->num_traits++; |
9451 | 0 | } |
9452 | | |
9453 | 0 | if (!adaptations) { |
9454 | 0 | return; |
9455 | 0 | } |
9456 | | |
9457 | 0 | for (i = 0; i < adaptations->children; ++i) { |
9458 | 0 | const zend_ast *adaptation_ast = adaptations->child[i]; |
9459 | 0 | switch (adaptation_ast->kind) { |
9460 | 0 | case ZEND_AST_TRAIT_PRECEDENCE: |
9461 | 0 | zend_compile_trait_precedence(adaptation_ast); |
9462 | 0 | break; |
9463 | 0 | case ZEND_AST_TRAIT_ALIAS: |
9464 | 0 | zend_compile_trait_alias(adaptation_ast); |
9465 | 0 | break; |
9466 | 0 | default: ZEND_UNREACHABLE(); |
9467 | 0 | } |
9468 | 0 | } |
9469 | 0 | } |
9470 | | /* }}} */ |
9471 | | |
9472 | | static void zend_compile_implements(zend_ast *ast) /* {{{ */ |
9473 | 0 | { |
9474 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
9475 | 0 | zend_class_entry *ce = CG(active_class_entry); |
9476 | 0 | zend_class_name *interface_names; |
9477 | 0 | uint32_t i; |
9478 | |
|
9479 | 0 | interface_names = emalloc(sizeof(zend_class_name) * list->children); |
9480 | |
|
9481 | 0 | for (i = 0; i < list->children; ++i) { |
9482 | 0 | zend_ast *class_ast = list->child[i]; |
9483 | 0 | interface_names[i].name = |
9484 | 0 | zend_resolve_const_class_name_reference(class_ast, "interface name"); |
9485 | 0 | interface_names[i].lc_name = zend_string_tolower(interface_names[i].name); |
9486 | 0 | } |
9487 | |
|
9488 | 0 | ce->num_interfaces = list->children; |
9489 | 0 | ce->interface_names = interface_names; |
9490 | 0 | } |
9491 | | /* }}} */ |
9492 | | |
9493 | | static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl) |
9494 | 0 | { |
9495 | 0 | zend_string *filename = CG(active_op_array)->filename; |
9496 | 0 | uint32_t start_lineno = decl->start_lineno; |
9497 | | |
9498 | | /* Use parent or first interface as prefix. */ |
9499 | 0 | zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS); |
9500 | 0 | if (decl->child[0]) { |
9501 | 0 | prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name"); |
9502 | 0 | } else if (decl->child[1]) { |
9503 | 0 | const zend_ast_list *list = zend_ast_get_list(decl->child[1]); |
9504 | 0 | prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name"); |
9505 | 0 | } |
9506 | |
|
9507 | 0 | zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32, |
9508 | 0 | ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
9509 | 0 | zend_string_release(prefix); |
9510 | 0 | return zend_new_interned_string(result); |
9511 | 0 | } |
9512 | | |
9513 | | static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast) |
9514 | 0 | { |
9515 | 0 | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM); |
9516 | 0 | zend_type type = zend_compile_typename(enum_backing_type_ast); |
9517 | 0 | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9518 | 0 | if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) { |
9519 | 0 | zend_string *type_string = zend_type_to_string(type); |
9520 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9521 | 0 | "Enum backing type must be int or string, %s given", |
9522 | 0 | ZSTR_VAL(type_string)); |
9523 | 0 | } |
9524 | 0 | if (type_mask == MAY_BE_LONG) { |
9525 | 0 | ce->enum_backing_type = IS_LONG; |
9526 | 0 | } else { |
9527 | 0 | ZEND_ASSERT(type_mask == MAY_BE_STRING); |
9528 | 0 | ce->enum_backing_type = IS_STRING; |
9529 | 0 | } |
9530 | 0 | zend_type_release(type, 0); |
9531 | 0 | } |
9532 | | |
9533 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */ |
9534 | 0 | { |
9535 | 0 | const zend_ast_decl *decl = (const zend_ast_decl *) ast; |
9536 | 0 | zend_ast *extends_ast = decl->child[0]; |
9537 | 0 | zend_ast *implements_ast = decl->child[1]; |
9538 | 0 | zend_ast *stmt_ast = decl->child[2]; |
9539 | 0 | zend_ast *enum_backing_type_ast = decl->child[4]; |
9540 | 0 | zend_string *name, *lcname; |
9541 | 0 | zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry)); |
9542 | 0 | zend_op *opline; |
9543 | |
|
9544 | 0 | zend_class_entry *original_ce = CG(active_class_entry); |
9545 | |
|
9546 | 0 | if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) { |
9547 | 0 | zend_string *unqualified_name = decl->name; |
9548 | |
|
9549 | 0 | if (CG(active_class_entry)) { |
9550 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested"); |
9551 | 0 | } |
9552 | | |
9553 | 0 | const char *type = "a class name"; |
9554 | 0 | if (decl->flags & ZEND_ACC_ENUM) { |
9555 | 0 | type = "an enum name"; |
9556 | 0 | } else if (decl->flags & ZEND_ACC_INTERFACE) { |
9557 | 0 | type = "an interface name"; |
9558 | 0 | } else if (decl->flags & ZEND_ACC_TRAIT) { |
9559 | 0 | type = "a trait name"; |
9560 | 0 | } |
9561 | 0 | zend_assert_valid_class_name(unqualified_name, type); |
9562 | 0 | name = zend_prefix_with_ns(unqualified_name); |
9563 | 0 | name = zend_new_interned_string(name); |
9564 | 0 | lcname = zend_string_tolower(name); |
9565 | |
|
9566 | 0 | if (FC(imports)) { |
9567 | 0 | zend_string *import_name = |
9568 | 0 | zend_hash_find_ptr_lc(FC(imports), unqualified_name); |
9569 | 0 | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
9570 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s " |
9571 | 0 | "(previously declared as local import)", ZSTR_VAL(name)); |
9572 | 0 | } |
9573 | 0 | } |
9574 | | |
9575 | 0 | zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS); |
9576 | 0 | } else { |
9577 | | /* Find an anon class name that is not in use yet. */ |
9578 | 0 | name = NULL; |
9579 | 0 | lcname = NULL; |
9580 | 0 | do { |
9581 | 0 | zend_tmp_string_release(name); |
9582 | 0 | zend_tmp_string_release(lcname); |
9583 | 0 | name = zend_generate_anon_class_name(decl); |
9584 | 0 | lcname = zend_string_tolower(name); |
9585 | 0 | } while (zend_hash_exists(CG(class_table), lcname)); |
9586 | 0 | } |
9587 | 0 | lcname = zend_new_interned_string(lcname); |
9588 | |
|
9589 | 0 | ce->type = ZEND_USER_CLASS; |
9590 | 0 | ce->name = name; |
9591 | 0 | zend_initialize_class_data(ce, true); |
9592 | 0 | if (!(decl->flags & ZEND_ACC_ANON_CLASS)) { |
9593 | 0 | zend_alloc_ce_cache(ce->name); |
9594 | 0 | } |
9595 | |
|
9596 | 0 | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
9597 | 0 | ce->ce_flags |= ZEND_ACC_PRELOADED; |
9598 | 0 | ZEND_MAP_PTR_NEW(ce->static_members_table); |
9599 | 0 | ZEND_MAP_PTR_NEW(ce->mutable_data); |
9600 | 0 | } |
9601 | |
|
9602 | 0 | ce->ce_flags |= decl->flags; |
9603 | 0 | ce->info.user.filename = zend_string_copy(zend_get_compiled_filename()); |
9604 | 0 | ce->info.user.line_start = decl->start_lineno; |
9605 | 0 | ce->info.user.line_end = decl->end_lineno; |
9606 | |
|
9607 | 0 | if (decl->doc_comment) { |
9608 | 0 | ce->doc_comment = zend_string_copy(decl->doc_comment); |
9609 | 0 | } |
9610 | |
|
9611 | 0 | if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) { |
9612 | | /* Serialization is not supported for anonymous classes */ |
9613 | 0 | ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; |
9614 | 0 | } |
9615 | |
|
9616 | 0 | if (extends_ast) { |
9617 | 0 | ce->parent_name = |
9618 | 0 | zend_resolve_const_class_name_reference(extends_ast, "class name"); |
9619 | 0 | } |
9620 | |
|
9621 | 0 | CG(active_class_entry) = ce; |
9622 | |
|
9623 | 0 | if (decl->child[3]) { |
9624 | 0 | zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0); |
9625 | 0 | } |
9626 | |
|
9627 | 0 | if (implements_ast) { |
9628 | 0 | zend_compile_implements(implements_ast); |
9629 | 0 | } |
9630 | |
|
9631 | 0 | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9632 | 0 | if (enum_backing_type_ast != NULL) { |
9633 | 0 | zend_compile_enum_backing_type(ce, enum_backing_type_ast); |
9634 | 0 | } |
9635 | 0 | zend_enum_add_interfaces(ce); |
9636 | 0 | zend_enum_register_props(ce); |
9637 | 0 | } |
9638 | |
|
9639 | 0 | zend_compile_stmt(stmt_ast); |
9640 | | |
9641 | | /* Reset lineno for final opcodes and errors */ |
9642 | 0 | CG(zend_lineno) = ast->lineno; |
9643 | |
|
9644 | 0 | if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) { |
9645 | 0 | zend_verify_abstract_class(ce); |
9646 | 0 | } |
9647 | |
|
9648 | 0 | CG(active_class_entry) = original_ce; |
9649 | |
|
9650 | 0 | if (toplevel) { |
9651 | 0 | ce->ce_flags |= ZEND_ACC_TOP_LEVEL; |
9652 | 0 | } |
9653 | | |
9654 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
9655 | 0 | if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
9656 | | #ifdef ZEND_OPCACHE_SHM_REATTACHMENT |
9657 | | /* See zend_link_hooked_object_iter(). */ |
9658 | | && !ce->num_hooked_props |
9659 | | #endif |
9660 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) { |
9661 | 0 | if (toplevel) { |
9662 | 0 | if (extends_ast) { |
9663 | 0 | zend_class_entry *parent_ce = zend_lookup_class_ex( |
9664 | 0 | ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); |
9665 | |
|
9666 | 0 | if (parent_ce |
9667 | 0 | && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) { |
9668 | 0 | if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) { |
9669 | 0 | zend_string_release(lcname); |
9670 | 0 | return; |
9671 | 0 | } |
9672 | 0 | } |
9673 | 0 | } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) { |
9674 | 0 | zend_string_release(lcname); |
9675 | 0 | zend_build_properties_info_table(ce); |
9676 | 0 | zend_inheritance_check_override(ce); |
9677 | 0 | ce->ce_flags |= ZEND_ACC_LINKED; |
9678 | 0 | zend_observer_class_linked_notify(ce, lcname); |
9679 | 0 | return; |
9680 | 0 | } else { |
9681 | 0 | goto link_unbound; |
9682 | 0 | } |
9683 | 0 | } else if (!extends_ast) { |
9684 | 0 | link_unbound: |
9685 | | /* Link unbound simple class */ |
9686 | 0 | zend_build_properties_info_table(ce); |
9687 | 0 | zend_inheritance_check_override(ce); |
9688 | 0 | ce->ce_flags |= ZEND_ACC_LINKED; |
9689 | 0 | } |
9690 | 0 | } |
9691 | | |
9692 | 0 | opline = get_next_op(); |
9693 | |
|
9694 | 0 | if (ce->parent_name) { |
9695 | | /* Lowercased parent name */ |
9696 | 0 | zend_string *lc_parent_name = zend_string_tolower(ce->parent_name); |
9697 | 0 | opline->op2_type = IS_CONST; |
9698 | 0 | LITERAL_STR(opline->op2, lc_parent_name); |
9699 | 0 | } |
9700 | |
|
9701 | 0 | opline->op1_type = IS_CONST; |
9702 | | /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table. |
9703 | | * However, by this point another thread may have caused `lcname` to be added in the interned string table. |
9704 | | * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use |
9705 | | * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the |
9706 | | * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using |
9707 | | * zend_add_literal_string() which gives us the new value. */ |
9708 | 0 | opline->op1.constant = zend_add_literal_string(&lcname); |
9709 | |
|
9710 | 0 | if (decl->flags & ZEND_ACC_ANON_CLASS) { |
9711 | 0 | opline->opcode = ZEND_DECLARE_ANON_CLASS; |
9712 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
9713 | 0 | zend_make_var_result(result, opline); |
9714 | 0 | if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) { |
9715 | | /* We checked above that the class name is not used. This really shouldn't happen. */ |
9716 | 0 | zend_error_noreturn(E_ERROR, |
9717 | 0 | "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name)); |
9718 | 0 | } |
9719 | 0 | } else { |
9720 | | /* Generate RTD keys until we find one that isn't in use yet. */ |
9721 | 0 | zend_string *key = NULL; |
9722 | 0 | do { |
9723 | 0 | zend_tmp_string_release(key); |
9724 | 0 | key = zend_build_runtime_definition_key(lcname, decl->start_lineno); |
9725 | 0 | } while (!zend_hash_add_ptr(CG(class_table), key, ce)); |
9726 | | |
9727 | | /* RTD key is placed after lcname literal in op1 */ |
9728 | 0 | zend_add_literal_string(&key); |
9729 | |
|
9730 | 0 | opline->opcode = ZEND_DECLARE_CLASS; |
9731 | 0 | if (toplevel |
9732 | 0 | && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) |
9733 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
9734 | 0 | && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
9735 | 0 | ) { |
9736 | 0 | if (!extends_ast) { |
9737 | | /* Use empty string for classes without parents to avoid new handler, and special |
9738 | | * handling of zend_early_binding. */ |
9739 | 0 | opline->op2_type = IS_CONST; |
9740 | 0 | LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC()); |
9741 | 0 | } |
9742 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING; |
9743 | 0 | opline->opcode = ZEND_DECLARE_CLASS_DELAYED; |
9744 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
9745 | 0 | opline->result_type = IS_UNUSED; |
9746 | 0 | opline->result.opline_num = -1; |
9747 | 0 | } |
9748 | 0 | } |
9749 | 0 | } |
9750 | | /* }}} */ |
9751 | | |
9752 | | static void zend_compile_enum_case(zend_ast *ast) |
9753 | 0 | { |
9754 | 0 | zend_class_entry *enum_class = CG(active_class_entry); |
9755 | 0 | if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) { |
9756 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums"); |
9757 | 0 | } |
9758 | | |
9759 | 0 | zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0])); |
9760 | 0 | zend_string *enum_class_name = enum_class->name; |
9761 | |
|
9762 | 0 | zval class_name_zval; |
9763 | 0 | ZVAL_STR_COPY(&class_name_zval, enum_class_name); |
9764 | 0 | zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval); |
9765 | |
|
9766 | 0 | zval case_id_zval; |
9767 | 0 | int case_id = zend_enum_next_case_id(enum_class); |
9768 | 0 | ZVAL_LONG(&case_id_zval, case_id); |
9769 | 0 | zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval); |
9770 | |
|
9771 | 0 | zval case_name_zval; |
9772 | 0 | ZVAL_STR_COPY(&case_name_zval, enum_case_name); |
9773 | 0 | zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval); |
9774 | |
|
9775 | 0 | zend_ast *case_value_ast = ast->child[1]; |
9776 | | // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval |
9777 | 0 | ast->child[1] = NULL; |
9778 | 0 | if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) { |
9779 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value", |
9780 | 0 | ZSTR_VAL(enum_case_name), |
9781 | 0 | ZSTR_VAL(enum_class_name)); |
9782 | 0 | } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) { |
9783 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value", |
9784 | 0 | ZSTR_VAL(enum_case_name), |
9785 | 0 | ZSTR_VAL(enum_class_name)); |
9786 | 0 | } |
9787 | | |
9788 | 0 | zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, |
9789 | 0 | class_name_ast, case_id_ast, case_name_ast, case_value_ast); |
9790 | |
|
9791 | 0 | zval value_zv; |
9792 | 0 | zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false); |
9793 | | |
9794 | | /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */ |
9795 | 0 | zend_ast *doc_comment_ast = ast->child[2]; |
9796 | 0 | zend_string *doc_comment = NULL; |
9797 | 0 | if (doc_comment_ast) { |
9798 | 0 | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
9799 | 0 | } |
9800 | |
|
9801 | 0 | zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment); |
9802 | 0 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE; |
9803 | 0 | zend_ast_destroy(const_enum_init_ast); |
9804 | |
|
9805 | 0 | zend_ast *attr_ast = ast->child[3]; |
9806 | 0 | if (attr_ast) { |
9807 | 0 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
9808 | |
|
9809 | 0 | zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
9810 | |
|
9811 | 0 | if (deprecated) { |
9812 | 0 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
9813 | 0 | } |
9814 | 0 | } |
9815 | 0 | } |
9816 | | |
9817 | | static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */ |
9818 | 0 | { |
9819 | 0 | switch (type) { |
9820 | 0 | case ZEND_SYMBOL_CLASS: |
9821 | 0 | if (!FC(imports)) { |
9822 | 0 | FC(imports) = emalloc(sizeof(HashTable)); |
9823 | 0 | zend_hash_init(FC(imports), 8, NULL, str_dtor, 0); |
9824 | 0 | } |
9825 | 0 | return FC(imports); |
9826 | 0 | case ZEND_SYMBOL_FUNCTION: |
9827 | 0 | if (!FC(imports_function)) { |
9828 | 0 | FC(imports_function) = emalloc(sizeof(HashTable)); |
9829 | 0 | zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0); |
9830 | 0 | } |
9831 | 0 | return FC(imports_function); |
9832 | 0 | case ZEND_SYMBOL_CONST: |
9833 | 0 | if (!FC(imports_const)) { |
9834 | 0 | FC(imports_const) = emalloc(sizeof(HashTable)); |
9835 | 0 | zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0); |
9836 | 0 | } |
9837 | 0 | return FC(imports_const); |
9838 | 0 | default: ZEND_UNREACHABLE(); |
9839 | 0 | } |
9840 | | |
9841 | 0 | return NULL; |
9842 | 0 | } |
9843 | | /* }}} */ |
9844 | | |
9845 | | static char *zend_get_use_type_str(uint32_t type) /* {{{ */ |
9846 | 0 | { |
9847 | 0 | switch (type) { |
9848 | 0 | case ZEND_SYMBOL_CLASS: |
9849 | 0 | return ""; |
9850 | 0 | case ZEND_SYMBOL_FUNCTION: |
9851 | 0 | return " function"; |
9852 | 0 | case ZEND_SYMBOL_CONST: |
9853 | 0 | return " const"; |
9854 | 0 | default: ZEND_UNREACHABLE(); |
9855 | 0 | } |
9856 | | |
9857 | 0 | return " unknown"; |
9858 | 0 | } |
9859 | | /* }}} */ |
9860 | | |
9861 | | static void zend_check_already_in_use(uint32_t type, const zend_string *old_name, const zend_string *new_name, const zend_string *check_name) /* {{{ */ |
9862 | 0 | { |
9863 | 0 | if (zend_string_equals_ci(old_name, check_name)) { |
9864 | 0 | return; |
9865 | 0 | } |
9866 | | |
9867 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
9868 | 0 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
9869 | 0 | } |
9870 | | /* }}} */ |
9871 | | |
9872 | | static void zend_compile_use(zend_ast *ast) /* {{{ */ |
9873 | 0 | { |
9874 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
9875 | 0 | uint32_t i; |
9876 | 0 | zend_string *current_ns = FC(current_namespace); |
9877 | 0 | uint32_t type = ast->attr; |
9878 | 0 | HashTable *current_import = zend_get_import_ht(type); |
9879 | 0 | bool case_sensitive = type == ZEND_SYMBOL_CONST; |
9880 | |
|
9881 | 0 | for (i = 0; i < list->children; ++i) { |
9882 | 0 | const zend_ast *use_ast = list->child[i]; |
9883 | 0 | zend_ast *old_name_ast = use_ast->child[0]; |
9884 | 0 | zend_ast *new_name_ast = use_ast->child[1]; |
9885 | 0 | zend_string *old_name = zend_ast_get_str(old_name_ast); |
9886 | 0 | zend_string *new_name, *lookup_name; |
9887 | |
|
9888 | 0 | if (new_name_ast) { |
9889 | 0 | new_name = zend_string_copy(zend_ast_get_str(new_name_ast)); |
9890 | 0 | } else { |
9891 | 0 | const char *unqualified_name; |
9892 | 0 | size_t unqualified_name_len; |
9893 | 0 | if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) { |
9894 | | /* The form "use A\B" is equivalent to "use A\B as B" */ |
9895 | 0 | new_name = zend_string_init(unqualified_name, unqualified_name_len, 0); |
9896 | 0 | } else { |
9897 | 0 | new_name = zend_string_copy(old_name); |
9898 | |
|
9899 | 0 | if (!current_ns) { |
9900 | 0 | zend_error(E_WARNING, "The use statement with non-compound name '%s' " |
9901 | 0 | "has no effect", ZSTR_VAL(new_name)); |
9902 | 0 | } |
9903 | 0 | } |
9904 | 0 | } |
9905 | |
|
9906 | 0 | if (case_sensitive) { |
9907 | 0 | lookup_name = zend_string_copy(new_name); |
9908 | 0 | } else { |
9909 | 0 | lookup_name = zend_string_tolower(new_name); |
9910 | 0 | } |
9911 | |
|
9912 | 0 | if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) { |
9913 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' " |
9914 | 0 | "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name)); |
9915 | 0 | } |
9916 | | |
9917 | 0 | if (current_ns) { |
9918 | 0 | zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0); |
9919 | 0 | zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns)); |
9920 | 0 | ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\'; |
9921 | 0 | memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1); |
9922 | |
|
9923 | 0 | if (zend_have_seen_symbol(ns_name, type)) { |
9924 | 0 | zend_check_already_in_use(type, old_name, new_name, ns_name); |
9925 | 0 | } |
9926 | |
|
9927 | 0 | zend_string_efree(ns_name); |
9928 | 0 | } else if (zend_have_seen_symbol(lookup_name, type)) { |
9929 | 0 | zend_check_already_in_use(type, old_name, new_name, lookup_name); |
9930 | 0 | } |
9931 | |
|
9932 | 0 | zend_string_addref(old_name); |
9933 | 0 | old_name = zend_new_interned_string(old_name); |
9934 | 0 | if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) { |
9935 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
9936 | 0 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
9937 | 0 | } |
9938 | | |
9939 | 0 | zend_string_release_ex(lookup_name, 0); |
9940 | 0 | zend_string_release_ex(new_name, 0); |
9941 | 0 | } |
9942 | 0 | } |
9943 | | /* }}} */ |
9944 | | |
9945 | | static void zend_compile_group_use(const zend_ast *ast) /* {{{ */ |
9946 | 0 | { |
9947 | 0 | uint32_t i; |
9948 | 0 | const zend_string *ns = zend_ast_get_str(ast->child[0]); |
9949 | 0 | const zend_ast_list *list = zend_ast_get_list(ast->child[1]); |
9950 | |
|
9951 | 0 | for (i = 0; i < list->children; i++) { |
9952 | 0 | zend_ast *inline_use, *use = list->child[i]; |
9953 | 0 | zval *name_zval = zend_ast_get_zval(use->child[0]); |
9954 | 0 | zend_string *name = Z_STR_P(name_zval); |
9955 | 0 | zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
9956 | 0 | zend_string_release_ex(name, 0); |
9957 | 0 | ZVAL_STR(name_zval, compound_ns); |
9958 | 0 | inline_use = zend_ast_create_list(1, ZEND_AST_USE, use); |
9959 | 0 | inline_use->attr = ast->attr ? ast->attr : use->attr; |
9960 | 0 | zend_compile_use(inline_use); |
9961 | 0 | } |
9962 | 0 | } |
9963 | | /* }}} */ |
9964 | | |
9965 | | static void zend_compile_const_decl(zend_ast *ast) /* {{{ */ |
9966 | 0 | { |
9967 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
9968 | 0 | uint32_t i; |
9969 | 0 | zend_ast *attributes_ast = NULL; |
9970 | 0 | zend_op *last_op = NULL; |
9971 | 0 | for (i = 0; i < list->children; ++i) { |
9972 | 0 | zend_ast *const_ast = list->child[i]; |
9973 | 0 | if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) { |
9974 | 0 | ZEND_ASSERT(i == list->children - 1); |
9975 | 0 | attributes_ast = const_ast; |
9976 | 0 | continue; |
9977 | 0 | } |
9978 | 0 | ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM); |
9979 | 0 | zend_ast *name_ast = const_ast->child[0]; |
9980 | 0 | zend_ast **value_ast_ptr = &const_ast->child[1]; |
9981 | 0 | zend_string *unqualified_name = zend_ast_get_str(name_ast); |
9982 | |
|
9983 | 0 | zend_string *name; |
9984 | 0 | znode name_node, value_node; |
9985 | 0 | zval *value_zv = &value_node.u.constant; |
9986 | |
|
9987 | 0 | value_node.op_type = IS_CONST; |
9988 | 0 | zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true); |
9989 | |
|
9990 | 0 | if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) { |
9991 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
9992 | 0 | "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name)); |
9993 | 0 | } |
9994 | | |
9995 | 0 | name = zend_prefix_with_ns(unqualified_name); |
9996 | 0 | name = zend_new_interned_string(name); |
9997 | |
|
9998 | 0 | if (FC(imports_const)) { |
9999 | 0 | zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name); |
10000 | 0 | if (import_name && !zend_string_equals(import_name, name)) { |
10001 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because " |
10002 | 0 | "the name is already in use", ZSTR_VAL(name)); |
10003 | 0 | } |
10004 | 0 | } |
10005 | | |
10006 | 0 | name_node.op_type = IS_CONST; |
10007 | 0 | ZVAL_STR(&name_node.u.constant, name); |
10008 | |
|
10009 | 0 | last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node); |
10010 | |
|
10011 | 0 | zend_register_seen_symbol(name, ZEND_SYMBOL_CONST); |
10012 | 0 | } |
10013 | 0 | if (attributes_ast == NULL) { |
10014 | 0 | return; |
10015 | 0 | } |
10016 | | /* Validate: attributes can only be applied to one constant at a time |
10017 | | * Since we store the AST for the attributes in the list of children, |
10018 | | * there should be exactly 2 children. */ |
10019 | 0 | if (list->children > 2) { |
10020 | 0 | zend_error_noreturn( |
10021 | 0 | E_COMPILE_ERROR, |
10022 | 0 | "Cannot apply attributes to multiple constants at once" |
10023 | 0 | ); |
10024 | 0 | } |
10025 | | |
10026 | 0 | HashTable *attributes = NULL; |
10027 | 0 | zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0); |
10028 | |
|
10029 | 0 | ZEND_ASSERT(last_op != NULL); |
10030 | 0 | last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST; |
10031 | 0 | znode attribs_node; |
10032 | 0 | attribs_node.op_type = IS_CONST; |
10033 | 0 | ZVAL_PTR(&attribs_node.u.constant, attributes); |
10034 | 0 | zend_emit_op_data(&attribs_node); |
10035 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS; |
10036 | 0 | } |
10037 | | /* }}}*/ |
10038 | | |
10039 | | static void zend_compile_namespace(const zend_ast *ast) /* {{{ */ |
10040 | 0 | { |
10041 | 0 | zend_ast *name_ast = ast->child[0]; |
10042 | 0 | zend_ast *stmt_ast = ast->child[1]; |
10043 | 0 | zend_string *name; |
10044 | 0 | bool with_bracket = stmt_ast != NULL; |
10045 | | |
10046 | | /* handle mixed syntax declaration or nested namespaces */ |
10047 | 0 | if (!FC(has_bracketed_namespaces)) { |
10048 | 0 | if (FC(current_namespace)) { |
10049 | | /* previous namespace declarations were unbracketed */ |
10050 | 0 | if (with_bracket) { |
10051 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10052 | 0 | "with unbracketed namespace declarations"); |
10053 | 0 | } |
10054 | 0 | } |
10055 | 0 | } else { |
10056 | | /* previous namespace declarations were bracketed */ |
10057 | 0 | if (!with_bracket) { |
10058 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10059 | 0 | "with unbracketed namespace declarations"); |
10060 | 0 | } else if (FC(current_namespace) || FC(in_namespace)) { |
10061 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested"); |
10062 | 0 | } |
10063 | 0 | } |
10064 | | |
10065 | 0 | bool is_first_namespace = (!with_bracket && !FC(current_namespace)) |
10066 | 0 | || (with_bracket && !FC(has_bracketed_namespaces)); |
10067 | 0 | if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
10068 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be " |
10069 | 0 | "the very first statement or after any declare call in the script"); |
10070 | 0 | } |
10071 | | |
10072 | 0 | if (FC(current_namespace)) { |
10073 | 0 | zend_string_release_ex(FC(current_namespace), 0); |
10074 | 0 | } |
10075 | |
|
10076 | 0 | if (name_ast) { |
10077 | 0 | name = zend_ast_get_str(name_ast); |
10078 | |
|
10079 | 0 | if (zend_string_equals_literal_ci(name, "namespace")) { |
10080 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name)); |
10081 | 0 | } |
10082 | | |
10083 | 0 | FC(current_namespace) = zend_string_copy(name); |
10084 | 0 | } else { |
10085 | 0 | FC(current_namespace) = NULL; |
10086 | 0 | } |
10087 | | |
10088 | 0 | zend_reset_import_tables(); |
10089 | |
|
10090 | 0 | FC(in_namespace) = 1; |
10091 | 0 | if (with_bracket) { |
10092 | 0 | FC(has_bracketed_namespaces) = 1; |
10093 | 0 | } |
10094 | |
|
10095 | 0 | if (stmt_ast) { |
10096 | 0 | zend_compile_top_stmt(stmt_ast); |
10097 | 0 | zend_end_namespace(); |
10098 | 0 | } |
10099 | 0 | } |
10100 | | /* }}} */ |
10101 | | |
10102 | | static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */ |
10103 | 0 | { |
10104 | 0 | zend_ast *offset_ast = ast->child[0]; |
10105 | 0 | zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast)); |
10106 | |
|
10107 | 0 | const char const_name[] = "__COMPILER_HALT_OFFSET__"; |
10108 | |
|
10109 | 0 | if (FC(has_bracketed_namespaces) && FC(in_namespace)) { |
10110 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
10111 | 0 | "__HALT_COMPILER() can only be used from the outermost scope"); |
10112 | 0 | } |
10113 | | |
10114 | 0 | const zend_string *filename = zend_get_compiled_filename(); |
10115 | 0 | zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1, |
10116 | 0 | ZSTR_VAL(filename), ZSTR_LEN(filename), false); |
10117 | | |
10118 | | /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in |
10119 | | * case this file was already included. */ |
10120 | 0 | if (!zend_hash_find(EG(zend_constants), name)) { |
10121 | 0 | zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0); |
10122 | 0 | } |
10123 | 0 | zend_string_release_ex(name, 0); |
10124 | 0 | } |
10125 | | /* }}} */ |
10126 | | |
10127 | | static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */ |
10128 | 0 | { |
10129 | 0 | const zend_op_array *op_array = CG(active_op_array); |
10130 | 0 | const zend_class_entry *ce = CG(active_class_entry); |
10131 | |
|
10132 | 0 | switch (ast->attr) { |
10133 | 0 | case T_LINE: |
10134 | 0 | ZVAL_LONG(zv, ast->lineno); |
10135 | 0 | break; |
10136 | 0 | case T_FILE: |
10137 | 0 | ZVAL_STR_COPY(zv, CG(compiled_filename)); |
10138 | 0 | break; |
10139 | 0 | case T_DIR: |
10140 | 0 | { |
10141 | 0 | const zend_string *filename = CG(compiled_filename); |
10142 | 0 | zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0); |
10143 | | #ifdef ZEND_WIN32 |
10144 | | ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10145 | | #else |
10146 | 0 | ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10147 | 0 | #endif |
10148 | |
|
10149 | 0 | if (zend_string_equals_literal(dirname, ".")) { |
10150 | 0 | dirname = zend_string_extend(dirname, MAXPATHLEN, 0); |
10151 | 0 | #ifdef HAVE_GETCWD |
10152 | 0 | ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN)); |
10153 | | #elif defined(HAVE_GETWD) |
10154 | | ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname))); |
10155 | | #endif |
10156 | 0 | ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname)); |
10157 | 0 | } |
10158 | |
|
10159 | 0 | ZVAL_STR(zv, dirname); |
10160 | 0 | break; |
10161 | 0 | } |
10162 | 0 | case T_FUNC_C: |
10163 | 0 | if (op_array && op_array->function_name) { |
10164 | 0 | ZVAL_STR_COPY(zv, op_array->function_name); |
10165 | 0 | } else { |
10166 | 0 | ZVAL_EMPTY_STRING(zv); |
10167 | 0 | } |
10168 | 0 | break; |
10169 | 0 | case T_PROPERTY_C: { |
10170 | 0 | zend_string *prop_info_name = CG(context).active_property_info_name; |
10171 | 0 | if (prop_info_name) { |
10172 | 0 | ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name)); |
10173 | 0 | } else { |
10174 | 0 | ZVAL_EMPTY_STRING(zv); |
10175 | 0 | } |
10176 | 0 | break; |
10177 | 0 | } |
10178 | 0 | case T_METHOD_C: |
10179 | | /* Detect whether we are directly inside a class (e.g. a class constant) and treat |
10180 | | * this as not being inside a function. */ |
10181 | 0 | if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
10182 | 0 | op_array = NULL; |
10183 | 0 | } |
10184 | 0 | if (op_array && op_array->function_name) { |
10185 | 0 | if (op_array->scope) { |
10186 | 0 | ZVAL_NEW_STR(zv, |
10187 | 0 | zend_create_member_string(op_array->scope->name, op_array->function_name)); |
10188 | 0 | } else { |
10189 | 0 | ZVAL_STR_COPY(zv, op_array->function_name); |
10190 | 0 | } |
10191 | 0 | } else { |
10192 | 0 | ZVAL_EMPTY_STRING(zv); |
10193 | 0 | } |
10194 | 0 | break; |
10195 | 0 | case T_CLASS_C: |
10196 | 0 | if (ce) { |
10197 | 0 | if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10198 | 0 | return 0; |
10199 | 0 | } else { |
10200 | 0 | ZVAL_STR_COPY(zv, ce->name); |
10201 | 0 | } |
10202 | 0 | } else { |
10203 | 0 | ZVAL_EMPTY_STRING(zv); |
10204 | 0 | } |
10205 | 0 | break; |
10206 | 0 | case T_TRAIT_C: |
10207 | 0 | if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10208 | 0 | ZVAL_STR_COPY(zv, ce->name); |
10209 | 0 | } else { |
10210 | 0 | ZVAL_EMPTY_STRING(zv); |
10211 | 0 | } |
10212 | 0 | break; |
10213 | 0 | case T_NS_C: |
10214 | 0 | if (FC(current_namespace)) { |
10215 | 0 | ZVAL_STR_COPY(zv, FC(current_namespace)); |
10216 | 0 | } else { |
10217 | 0 | ZVAL_EMPTY_STRING(zv); |
10218 | 0 | } |
10219 | 0 | break; |
10220 | 0 | default: ZEND_UNREACHABLE(); |
10221 | 0 | } |
10222 | | |
10223 | 0 | return true; |
10224 | 0 | } |
10225 | | /* }}} */ |
10226 | | |
10227 | | ZEND_API bool zend_is_op_long_compatible(const zval *op) |
10228 | 0 | { |
10229 | 0 | if (Z_TYPE_P(op) == IS_ARRAY) { |
10230 | 0 | return false; |
10231 | 0 | } |
10232 | | |
10233 | 0 | if (Z_TYPE_P(op) == IS_DOUBLE |
10234 | 0 | && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) { |
10235 | 0 | return false; |
10236 | 0 | } |
10237 | | |
10238 | 0 | if (Z_TYPE_P(op) == IS_STRING) { |
10239 | 0 | double dval = 0; |
10240 | 0 | uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval); |
10241 | 0 | if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) { |
10242 | 0 | return false; |
10243 | 0 | } |
10244 | 0 | } |
10245 | | |
10246 | 0 | return true; |
10247 | 0 | } |
10248 | | |
10249 | | ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */ |
10250 | 0 | { |
10251 | 0 | if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) { |
10252 | | /* Array to string warning. */ |
10253 | 0 | return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY; |
10254 | 0 | } |
10255 | | |
10256 | 0 | if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV |
10257 | 0 | || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR |
10258 | 0 | || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) { |
10259 | | /* Only the numeric operations throw errors. */ |
10260 | 0 | return 0; |
10261 | 0 | } |
10262 | | |
10263 | 0 | if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) { |
10264 | 0 | if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) { |
10265 | | /* Adding two arrays is allowed. */ |
10266 | 0 | return 0; |
10267 | 0 | } |
10268 | | |
10269 | | /* Numeric operators throw when one of the operands is an array. */ |
10270 | 0 | return 1; |
10271 | 0 | } |
10272 | | |
10273 | | /* While basic arithmetic operators always produce numeric string errors, |
10274 | | * bitwise operators don't produce errors if both operands are strings */ |
10275 | 0 | if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) |
10276 | 0 | && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { |
10277 | 0 | return 0; |
10278 | 0 | } |
10279 | | |
10280 | 0 | if (Z_TYPE_P(op1) == IS_STRING |
10281 | 0 | && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) { |
10282 | 0 | return 1; |
10283 | 0 | } |
10284 | | |
10285 | 0 | if (Z_TYPE_P(op2) == IS_STRING |
10286 | 0 | && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) { |
10287 | 0 | return 1; |
10288 | 0 | } |
10289 | | |
10290 | | /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */ |
10291 | 0 | if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR |
10292 | 0 | || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) { |
10293 | 0 | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) { |
10294 | 0 | return 1; |
10295 | 0 | } |
10296 | 0 | } |
10297 | | |
10298 | 0 | if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) { |
10299 | | /* Division by zero throws an error. */ |
10300 | 0 | return 1; |
10301 | 0 | } |
10302 | | |
10303 | | /* Mod is an operation that will cast float/float-strings to integers which might |
10304 | | produce float to int incompatible errors, and also cannot be divided by 0 */ |
10305 | 0 | if (opcode == ZEND_MOD) { |
10306 | 0 | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) { |
10307 | 0 | return 1; |
10308 | 0 | } |
10309 | 0 | } |
10310 | | |
10311 | 0 | if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) { |
10312 | | /* 0 ** (<0) throws a division by zero error. */ |
10313 | 0 | return 1; |
10314 | 0 | } |
10315 | 0 | if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) { |
10316 | | /* Shift by negative number throws an error. */ |
10317 | 0 | return 1; |
10318 | 0 | } |
10319 | | |
10320 | 0 | return 0; |
10321 | 0 | } |
10322 | | /* }}} */ |
10323 | | |
10324 | | static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */ |
10325 | 0 | { |
10326 | 0 | if (zend_binary_op_produces_error(opcode, op1, op2)) { |
10327 | 0 | return false; |
10328 | 0 | } |
10329 | | |
10330 | 0 | const binary_op_type fn = get_binary_op(opcode); |
10331 | 0 | fn(result, op1, op2); |
10332 | 0 | return true; |
10333 | 0 | } |
10334 | | /* }}} */ |
10335 | | |
10336 | | ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op) |
10337 | 0 | { |
10338 | 0 | if (opcode == ZEND_BW_NOT) { |
10339 | | /* BW_NOT on string does not convert the string into an integer. */ |
10340 | 0 | if (Z_TYPE_P(op) == IS_STRING) { |
10341 | 0 | return 0; |
10342 | 0 | } |
10343 | 0 | return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op); |
10344 | 0 | } |
10345 | | /* Can happen when called from zend_optimizer_eval_unary_op() */ |
10346 | 0 | if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) { |
10347 | | /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */ |
10348 | 0 | return Z_TYPE_P(op) == IS_DOUBLE; |
10349 | 0 | } |
10350 | | |
10351 | 0 | return 0; |
10352 | 0 | } |
10353 | | |
10354 | | static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */ |
10355 | 0 | { |
10356 | 0 | if (zend_unary_op_produces_error(opcode, op)) { |
10357 | 0 | return false; |
10358 | 0 | } |
10359 | | |
10360 | 0 | const unary_op_type fn = get_unary_op(opcode); |
10361 | 0 | fn(result, op); |
10362 | 0 | return true; |
10363 | 0 | } |
10364 | | /* }}} */ |
10365 | | |
10366 | | static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */ |
10367 | 0 | { |
10368 | 0 | zval right; |
10369 | 0 | ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10370 | 0 | return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right); |
10371 | 0 | } |
10372 | | /* }}} */ |
10373 | | |
10374 | | static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */ |
10375 | 0 | { |
10376 | 0 | const binary_op_type fn = kind == ZEND_AST_GREATER |
10377 | 0 | ? is_smaller_function : is_smaller_or_equal_function; |
10378 | 0 | fn(result, op2, op1); |
10379 | 0 | } |
10380 | | /* }}} */ |
10381 | | |
10382 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ |
10383 | 0 | { |
10384 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
10385 | 0 | zend_ast *last_elem_ast = NULL; |
10386 | 0 | uint32_t i; |
10387 | 0 | bool is_constant = true; |
10388 | |
|
10389 | 0 | if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) { |
10390 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression"); |
10391 | 0 | } |
10392 | | |
10393 | | /* First ensure that *all* child nodes are constant and by-val */ |
10394 | 0 | for (i = 0; i < list->children; ++i) { |
10395 | 0 | zend_ast *elem_ast = list->child[i]; |
10396 | |
|
10397 | 0 | if (elem_ast == NULL) { |
10398 | | /* Report error at line of last non-empty element */ |
10399 | 0 | if (last_elem_ast) { |
10400 | 0 | CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast); |
10401 | 0 | } |
10402 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
10403 | 0 | } |
10404 | |
|
10405 | 0 | if (elem_ast->kind != ZEND_AST_UNPACK) { |
10406 | 0 | zend_eval_const_expr(&elem_ast->child[0]); |
10407 | 0 | zend_eval_const_expr(&elem_ast->child[1]); |
10408 | |
|
10409 | 0 | if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL |
10410 | 0 | || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL) |
10411 | 0 | ) { |
10412 | 0 | is_constant = false; |
10413 | 0 | } |
10414 | 0 | } else { |
10415 | 0 | zend_eval_const_expr(&elem_ast->child[0]); |
10416 | |
|
10417 | 0 | if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) { |
10418 | 0 | is_constant = false; |
10419 | 0 | } |
10420 | 0 | } |
10421 | |
|
10422 | 0 | last_elem_ast = elem_ast; |
10423 | 0 | } |
10424 | |
|
10425 | 0 | if (!is_constant) { |
10426 | 0 | return false; |
10427 | 0 | } |
10428 | | |
10429 | 0 | if (!list->children) { |
10430 | 0 | ZVAL_EMPTY_ARRAY(result); |
10431 | 0 | return true; |
10432 | 0 | } |
10433 | | |
10434 | 0 | array_init_size(result, list->children); |
10435 | 0 | for (i = 0; i < list->children; ++i) { |
10436 | 0 | const zend_ast *elem_ast = list->child[i]; |
10437 | 0 | zend_ast *value_ast = elem_ast->child[0]; |
10438 | 0 | zend_ast *key_ast; |
10439 | |
|
10440 | 0 | zval *value = zend_ast_get_zval(value_ast); |
10441 | 0 | if (elem_ast->kind == ZEND_AST_UNPACK) { |
10442 | 0 | if (Z_TYPE_P(value) == IS_ARRAY) { |
10443 | 0 | const HashTable *ht = Z_ARRVAL_P(value); |
10444 | 0 | zval *val; |
10445 | 0 | zend_string *key; |
10446 | |
|
10447 | 0 | ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) { |
10448 | 0 | if (key) { |
10449 | 0 | zend_hash_update(Z_ARRVAL_P(result), key, val); |
10450 | 0 | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) { |
10451 | 0 | zval_ptr_dtor(result); |
10452 | 0 | return 0; |
10453 | 0 | } |
10454 | 0 | Z_TRY_ADDREF_P(val); |
10455 | 0 | } ZEND_HASH_FOREACH_END(); |
10456 | | |
10457 | 0 | continue; |
10458 | 0 | } else { |
10459 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value)); |
10460 | 0 | } |
10461 | 0 | } |
10462 | | |
10463 | 0 | Z_TRY_ADDREF_P(value); |
10464 | |
|
10465 | 0 | key_ast = elem_ast->child[1]; |
10466 | 0 | if (key_ast) { |
10467 | 0 | const zval *key = zend_ast_get_zval(key_ast); |
10468 | 0 | switch (Z_TYPE_P(key)) { |
10469 | 0 | case IS_LONG: |
10470 | 0 | zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value); |
10471 | 0 | break; |
10472 | 0 | case IS_STRING: |
10473 | 0 | zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value); |
10474 | 0 | break; |
10475 | 0 | case IS_DOUBLE: { |
10476 | 0 | zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); |
10477 | | /* Incompatible float will generate an error, leave this to run-time */ |
10478 | 0 | if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { |
10479 | 0 | goto fail; |
10480 | 0 | } |
10481 | 0 | zend_hash_index_update(Z_ARRVAL_P(result), lval, value); |
10482 | 0 | break; |
10483 | 0 | } |
10484 | 0 | case IS_FALSE: |
10485 | 0 | zend_hash_index_update(Z_ARRVAL_P(result), 0, value); |
10486 | 0 | break; |
10487 | 0 | case IS_TRUE: |
10488 | 0 | zend_hash_index_update(Z_ARRVAL_P(result), 1, value); |
10489 | 0 | break; |
10490 | 0 | case IS_NULL: |
10491 | | /* Null key will generate a warning at run-time. */ |
10492 | 0 | goto fail; |
10493 | 0 | default: |
10494 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type"); |
10495 | 0 | } |
10496 | 0 | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) { |
10497 | 0 | fail: |
10498 | 0 | zval_ptr_dtor_nogc(value); |
10499 | 0 | zval_ptr_dtor(result); |
10500 | 0 | return 0; |
10501 | 0 | } |
10502 | 0 | } |
10503 | | |
10504 | 0 | return true; |
10505 | 0 | } |
10506 | | /* }}} */ |
10507 | | |
10508 | | static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */ |
10509 | 0 | { |
10510 | 0 | zend_ast *left_ast = ast->child[0]; |
10511 | 0 | zend_ast *right_ast = ast->child[1]; |
10512 | 0 | uint32_t opcode = ast->attr; |
10513 | |
|
10514 | 0 | znode left_node, right_node; |
10515 | |
|
10516 | 0 | zend_compile_expr(&left_node, left_ast); |
10517 | 0 | zend_compile_expr(&right_node, right_ast); |
10518 | |
|
10519 | 0 | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10520 | 0 | if (zend_try_ct_eval_binary_op(&result->u.constant, opcode, |
10521 | 0 | &left_node.u.constant, &right_node.u.constant) |
10522 | 0 | ) { |
10523 | 0 | result->op_type = IS_CONST; |
10524 | 0 | zval_ptr_dtor(&left_node.u.constant); |
10525 | 0 | zval_ptr_dtor(&right_node.u.constant); |
10526 | 0 | return; |
10527 | 0 | } |
10528 | 0 | } |
10529 | | |
10530 | 0 | do { |
10531 | 0 | if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) { |
10532 | | /* convert $x === null to is_null($x) (i.e. ZEND_TYPE_CHECK opcode). Do the same thing for false/true. (covers IS_NULL, IS_FALSE, and IS_TRUE) */ |
10533 | 0 | if (left_node.op_type == IS_CONST) { |
10534 | 0 | if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) { |
10535 | 0 | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL); |
10536 | 0 | opline->extended_value = |
10537 | 0 | (opcode == ZEND_IS_IDENTICAL) ? |
10538 | 0 | (1 << Z_TYPE(left_node.u.constant)) : |
10539 | 0 | (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant))); |
10540 | 0 | return; |
10541 | 0 | } |
10542 | 0 | } else if (right_node.op_type == IS_CONST) { |
10543 | 0 | if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) { |
10544 | 0 | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL); |
10545 | 0 | opline->extended_value = |
10546 | 0 | (opcode == ZEND_IS_IDENTICAL) ? |
10547 | 0 | (1 << Z_TYPE(right_node.u.constant)) : |
10548 | 0 | (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant))); |
10549 | 0 | return; |
10550 | 0 | } |
10551 | 0 | } |
10552 | 0 | } else if (opcode == ZEND_CONCAT) { |
10553 | | /* convert constant operands to strings at compile-time */ |
10554 | 0 | if (left_node.op_type == IS_CONST) { |
10555 | 0 | if (Z_TYPE(left_node.u.constant) == IS_ARRAY) { |
10556 | 0 | zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING; |
10557 | 0 | } else { |
10558 | 0 | convert_to_string(&left_node.u.constant); |
10559 | 0 | } |
10560 | 0 | } |
10561 | 0 | if (right_node.op_type == IS_CONST) { |
10562 | 0 | if (Z_TYPE(right_node.u.constant) == IS_ARRAY) { |
10563 | 0 | zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING; |
10564 | 0 | } else { |
10565 | 0 | convert_to_string(&right_node.u.constant); |
10566 | 0 | } |
10567 | 0 | } |
10568 | 0 | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10569 | 0 | opcode = ZEND_FAST_CONCAT; |
10570 | 0 | } |
10571 | 0 | } |
10572 | 0 | zend_emit_op_tmp(result, opcode, &left_node, &right_node); |
10573 | 0 | } while (0); |
10574 | 0 | } |
10575 | | /* }}} */ |
10576 | | |
10577 | | /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right |
10578 | | * evaluation order. */ |
10579 | | static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */ |
10580 | 0 | { |
10581 | 0 | zend_ast *left_ast = ast->child[0]; |
10582 | 0 | zend_ast *right_ast = ast->child[1]; |
10583 | 0 | znode left_node, right_node; |
10584 | |
|
10585 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL); |
10586 | |
|
10587 | 0 | zend_compile_expr(&left_node, left_ast); |
10588 | 0 | zend_compile_expr(&right_node, right_ast); |
10589 | |
|
10590 | 0 | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10591 | 0 | result->op_type = IS_CONST; |
10592 | 0 | zend_ct_eval_greater(&result->u.constant, ast->kind, |
10593 | 0 | &left_node.u.constant, &right_node.u.constant); |
10594 | 0 | zval_ptr_dtor(&left_node.u.constant); |
10595 | 0 | zval_ptr_dtor(&right_node.u.constant); |
10596 | 0 | return; |
10597 | 0 | } |
10598 | | |
10599 | 0 | zend_emit_op_tmp(result, |
10600 | 0 | ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL, |
10601 | 0 | &right_node, &left_node); |
10602 | 0 | } |
10603 | | /* }}} */ |
10604 | | |
10605 | | static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */ |
10606 | 0 | { |
10607 | 0 | zend_ast *expr_ast = ast->child[0]; |
10608 | 0 | uint32_t opcode = ast->attr; |
10609 | |
|
10610 | 0 | znode expr_node; |
10611 | 0 | zend_compile_expr(&expr_node, expr_ast); |
10612 | |
|
10613 | 0 | if (expr_node.op_type == IS_CONST |
10614 | 0 | && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) { |
10615 | 0 | result->op_type = IS_CONST; |
10616 | 0 | zval_ptr_dtor(&expr_node.u.constant); |
10617 | 0 | return; |
10618 | 0 | } |
10619 | | |
10620 | 0 | zend_emit_op_tmp(result, opcode, &expr_node, NULL); |
10621 | 0 | } |
10622 | | /* }}} */ |
10623 | | |
10624 | | static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */ |
10625 | 0 | { |
10626 | 0 | zend_ast *expr_ast = ast->child[0]; |
10627 | 0 | znode expr_node, right_node; |
10628 | |
|
10629 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS); |
10630 | |
|
10631 | 0 | zend_compile_expr(&expr_node, expr_ast); |
10632 | |
|
10633 | 0 | if (expr_node.op_type == IS_CONST |
10634 | 0 | && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) { |
10635 | 0 | result->op_type = IS_CONST; |
10636 | 0 | zval_ptr_dtor(&expr_node.u.constant); |
10637 | 0 | return; |
10638 | 0 | } |
10639 | | |
10640 | 0 | right_node.op_type = IS_CONST; |
10641 | 0 | ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10642 | 0 | zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node); |
10643 | 0 | } |
10644 | | /* }}} */ |
10645 | | |
10646 | | static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */ |
10647 | 0 | { |
10648 | 0 | zend_ast *left_ast = ast->child[0]; |
10649 | 0 | zend_ast *right_ast = ast->child[1]; |
10650 | |
|
10651 | 0 | znode left_node, right_node; |
10652 | 0 | zend_op *opline_jmpz, *opline_bool; |
10653 | 0 | uint32_t opnum_jmpz; |
10654 | |
|
10655 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR); |
10656 | |
|
10657 | 0 | zend_compile_expr(&left_node, left_ast); |
10658 | |
|
10659 | 0 | if (left_node.op_type == IS_CONST) { |
10660 | 0 | if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant)) |
10661 | 0 | || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) { |
10662 | 0 | result->op_type = IS_CONST; |
10663 | 0 | ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant)); |
10664 | 0 | } else { |
10665 | 0 | zend_compile_expr(&right_node, right_ast); |
10666 | |
|
10667 | 0 | if (right_node.op_type == IS_CONST) { |
10668 | 0 | result->op_type = IS_CONST; |
10669 | 0 | ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant)); |
10670 | |
|
10671 | 0 | zval_ptr_dtor(&right_node.u.constant); |
10672 | 0 | } else { |
10673 | 0 | zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL); |
10674 | 0 | } |
10675 | 0 | } |
10676 | |
|
10677 | 0 | zval_ptr_dtor(&left_node.u.constant); |
10678 | 0 | return; |
10679 | 0 | } |
10680 | | |
10681 | 0 | opnum_jmpz = get_next_op_number(); |
10682 | 0 | opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX, |
10683 | 0 | &left_node, NULL); |
10684 | |
|
10685 | 0 | if (left_node.op_type == IS_TMP_VAR) { |
10686 | 0 | SET_NODE(opline_jmpz->result, &left_node); |
10687 | 0 | GET_NODE(result, opline_jmpz->result); |
10688 | 0 | } else { |
10689 | 0 | zend_make_tmp_result(result, opline_jmpz); |
10690 | 0 | } |
10691 | |
|
10692 | 0 | zend_compile_expr(&right_node, right_ast); |
10693 | |
|
10694 | 0 | opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL); |
10695 | 0 | SET_NODE(opline_bool->result, result); |
10696 | |
|
10697 | 0 | zend_update_jump_target_to_next(opnum_jmpz); |
10698 | 0 | } |
10699 | | /* }}} */ |
10700 | | |
10701 | | static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
10702 | 0 | { |
10703 | 0 | zend_ast *var_ast = ast->child[0]; |
10704 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC); |
10705 | |
|
10706 | 0 | zend_ensure_writable_variable(var_ast); |
10707 | |
|
10708 | 0 | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
10709 | 0 | zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false); |
10710 | 0 | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ; |
10711 | 0 | zend_make_tmp_result(result, opline); |
10712 | 0 | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
10713 | 0 | zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false); |
10714 | 0 | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP; |
10715 | 0 | zend_make_tmp_result(result, opline); |
10716 | 0 | } else { |
10717 | 0 | znode var_node; |
10718 | 0 | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
10719 | 0 | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
10720 | 0 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
10721 | 0 | } |
10722 | 0 | zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC, |
10723 | 0 | &var_node, NULL); |
10724 | 0 | } |
10725 | 0 | } |
10726 | | /* }}} */ |
10727 | | |
10728 | | static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
10729 | 0 | { |
10730 | 0 | zend_ast *var_ast = ast->child[0]; |
10731 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC); |
10732 | |
|
10733 | 0 | zend_ensure_writable_variable(var_ast); |
10734 | |
|
10735 | 0 | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
10736 | 0 | zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false); |
10737 | 0 | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ; |
10738 | 0 | opline->result_type = IS_TMP_VAR; |
10739 | 0 | result->op_type = IS_TMP_VAR; |
10740 | 0 | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
10741 | 0 | zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false); |
10742 | 0 | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP; |
10743 | 0 | opline->result_type = IS_TMP_VAR; |
10744 | 0 | result->op_type = IS_TMP_VAR; |
10745 | 0 | } else { |
10746 | 0 | znode var_node; |
10747 | 0 | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
10748 | 0 | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
10749 | 0 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
10750 | 0 | } |
10751 | 0 | zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC, |
10752 | 0 | &var_node, NULL); |
10753 | 0 | } |
10754 | 0 | } |
10755 | | /* }}} */ |
10756 | | |
10757 | | static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */ |
10758 | 0 | { |
10759 | 0 | zend_ast *expr_ast = ast->child[0]; |
10760 | 0 | znode expr_node; |
10761 | 0 | zend_op *opline; |
10762 | |
|
10763 | 0 | zend_compile_expr(&expr_node, expr_ast); |
10764 | |
|
10765 | 0 | if (ast->attr == _IS_BOOL) { |
10766 | 0 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL); |
10767 | 0 | } else if (ast->attr == IS_NULL) { |
10768 | 0 | zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
10769 | 0 | } else { |
10770 | 0 | opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL); |
10771 | 0 | opline->extended_value = ast->attr; |
10772 | 0 | } |
10773 | 0 | } |
10774 | | /* }}} */ |
10775 | | |
10776 | | static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */ |
10777 | 0 | { |
10778 | 0 | zend_ast *cond_ast = ast->child[0]; |
10779 | 0 | zend_ast *false_ast = ast->child[2]; |
10780 | |
|
10781 | 0 | znode cond_node, false_node; |
10782 | 0 | zend_op *opline_qm_assign; |
10783 | 0 | uint32_t opnum_jmp_set; |
10784 | |
|
10785 | 0 | ZEND_ASSERT(ast->child[1] == NULL); |
10786 | |
|
10787 | 0 | zend_compile_expr(&cond_node, cond_ast); |
10788 | |
|
10789 | 0 | opnum_jmp_set = get_next_op_number(); |
10790 | 0 | zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL); |
10791 | |
|
10792 | 0 | zend_compile_expr(&false_node, false_ast); |
10793 | |
|
10794 | 0 | opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
10795 | 0 | SET_NODE(opline_qm_assign->result, result); |
10796 | |
|
10797 | 0 | zend_update_jump_target_to_next(opnum_jmp_set); |
10798 | 0 | } |
10799 | | /* }}} */ |
10800 | | |
10801 | | static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */ |
10802 | 0 | { |
10803 | 0 | zend_ast *cond_ast = ast->child[0]; |
10804 | 0 | zend_ast *true_ast = ast->child[1]; |
10805 | 0 | zend_ast *false_ast = ast->child[2]; |
10806 | |
|
10807 | 0 | znode cond_node, true_node, false_node; |
10808 | 0 | zend_op *opline_qm_assign2; |
10809 | 0 | uint32_t opnum_jmpz, opnum_jmp; |
10810 | |
|
10811 | 0 | if (cond_ast->kind == ZEND_AST_CONDITIONAL |
10812 | 0 | && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) { |
10813 | 0 | if (cond_ast->child[1]) { |
10814 | 0 | if (true_ast) { |
10815 | 0 | zend_error(E_COMPILE_ERROR, |
10816 | 0 | "Unparenthesized `a ? b : c ? d : e` is not supported. " |
10817 | 0 | "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`"); |
10818 | 0 | } else { |
10819 | 0 | zend_error(E_COMPILE_ERROR, |
10820 | 0 | "Unparenthesized `a ? b : c ?: d` is not supported. " |
10821 | 0 | "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`"); |
10822 | 0 | } |
10823 | 0 | } else { |
10824 | 0 | if (true_ast) { |
10825 | 0 | zend_error(E_COMPILE_ERROR, |
10826 | 0 | "Unparenthesized `a ?: b ? c : d` is not supported. " |
10827 | 0 | "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`"); |
10828 | 0 | } else { |
10829 | | /* This case is harmless: (a ?: b) ?: c always produces the same result |
10830 | | * as a ?: (b ?: c). */ |
10831 | 0 | } |
10832 | 0 | } |
10833 | 0 | } |
10834 | |
|
10835 | 0 | if (!true_ast) { |
10836 | 0 | zend_compile_shorthand_conditional(result, ast); |
10837 | 0 | return; |
10838 | 0 | } |
10839 | | |
10840 | 0 | zend_compile_expr(&cond_node, cond_ast); |
10841 | |
|
10842 | 0 | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
10843 | |
|
10844 | 0 | zend_compile_expr(&true_node, true_ast); |
10845 | |
|
10846 | 0 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL); |
10847 | |
|
10848 | 0 | opnum_jmp = zend_emit_jump(0); |
10849 | |
|
10850 | 0 | zend_update_jump_target_to_next(opnum_jmpz); |
10851 | |
|
10852 | 0 | zend_compile_expr(&false_node, false_ast); |
10853 | |
|
10854 | 0 | opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
10855 | 0 | SET_NODE(opline_qm_assign2->result, result); |
10856 | |
|
10857 | 0 | zend_update_jump_target_to_next(opnum_jmp); |
10858 | 0 | } |
10859 | | /* }}} */ |
10860 | | |
10861 | | static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
10862 | 0 | { |
10863 | 0 | zend_ast *expr_ast = ast->child[0]; |
10864 | 0 | zend_ast *default_ast = ast->child[1]; |
10865 | |
|
10866 | 0 | znode expr_node, default_node; |
10867 | 0 | zend_op *opline; |
10868 | 0 | uint32_t opnum; |
10869 | |
|
10870 | 0 | zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false); |
10871 | |
|
10872 | 0 | opnum = get_next_op_number(); |
10873 | 0 | zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL); |
10874 | |
|
10875 | 0 | zend_compile_expr(&default_node, default_ast); |
10876 | |
|
10877 | 0 | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL); |
10878 | 0 | SET_NODE(opline->result, result); |
10879 | |
|
10880 | 0 | opline = &CG(active_op_array)->opcodes[opnum]; |
10881 | 0 | opline->op2.opline_num = get_next_op_number(); |
10882 | 0 | } |
10883 | | /* }}} */ |
10884 | | |
10885 | 0 | static void znode_dtor(zval *zv) { |
10886 | 0 | znode *node = Z_PTR_P(zv); |
10887 | 0 | if (node->op_type == IS_CONST) { |
10888 | 0 | zval_ptr_dtor_nogc(&node->u.constant); |
10889 | 0 | } |
10890 | 0 | efree(node); |
10891 | 0 | } |
10892 | | |
10893 | | static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
10894 | 0 | { |
10895 | 0 | zend_ast *var_ast = ast->child[0]; |
10896 | 0 | zend_ast *default_ast = ast->child[1]; |
10897 | |
|
10898 | 0 | znode var_node_is, var_node_w, default_node, assign_node, *node; |
10899 | 0 | zend_op *opline; |
10900 | 0 | uint32_t coalesce_opnum; |
10901 | 0 | bool need_frees = false; |
10902 | | |
10903 | | /* Remember expressions compiled during the initial BP_VAR_IS lookup, |
10904 | | * to avoid double-evaluation when we compile again with BP_VAR_W. */ |
10905 | 0 | HashTable *orig_memoized_exprs = CG(memoized_exprs); |
10906 | 0 | const zend_memoize_mode orig_memoize_mode = CG(memoize_mode); |
10907 | |
|
10908 | 0 | zend_ensure_writable_variable(var_ast); |
10909 | 0 | if (is_this_fetch(var_ast)) { |
10910 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
10911 | 0 | } |
10912 | | |
10913 | 0 | ALLOC_HASHTABLE(CG(memoized_exprs)); |
10914 | 0 | zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0); |
10915 | |
|
10916 | 0 | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
10917 | 0 | zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false); |
10918 | |
|
10919 | 0 | coalesce_opnum = get_next_op_number(); |
10920 | 0 | zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL); |
10921 | |
|
10922 | 0 | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
10923 | 0 | if (var_ast->kind == ZEND_AST_DIM) { |
10924 | 0 | zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast); |
10925 | 0 | } else { |
10926 | 0 | zend_compile_expr(&default_node, default_ast); |
10927 | 0 | } |
10928 | |
|
10929 | 0 | CG(memoize_mode) = ZEND_MEMOIZE_FETCH; |
10930 | 0 | zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false); |
10931 | | |
10932 | | /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */ |
10933 | 0 | opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
10934 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
10935 | 0 | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
10936 | 0 | switch (kind) { |
10937 | 0 | case ZEND_AST_VAR: |
10938 | 0 | zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node); |
10939 | 0 | break; |
10940 | 0 | case ZEND_AST_STATIC_PROP: |
10941 | 0 | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
10942 | 0 | opline->result_type = IS_TMP_VAR; |
10943 | 0 | var_node_w.op_type = IS_TMP_VAR; |
10944 | 0 | zend_emit_op_data(&default_node); |
10945 | 0 | assign_node = var_node_w; |
10946 | 0 | break; |
10947 | 0 | case ZEND_AST_DIM: |
10948 | 0 | opline->opcode = ZEND_ASSIGN_DIM; |
10949 | 0 | opline->result_type = IS_TMP_VAR; |
10950 | 0 | var_node_w.op_type = IS_TMP_VAR; |
10951 | 0 | zend_emit_op_data(&default_node); |
10952 | 0 | assign_node = var_node_w; |
10953 | 0 | break; |
10954 | 0 | case ZEND_AST_PROP: |
10955 | 0 | case ZEND_AST_NULLSAFE_PROP: |
10956 | 0 | opline->opcode = ZEND_ASSIGN_OBJ; |
10957 | 0 | opline->result_type = IS_TMP_VAR; |
10958 | 0 | var_node_w.op_type = IS_TMP_VAR; |
10959 | 0 | zend_emit_op_data(&default_node); |
10960 | 0 | assign_node = var_node_w; |
10961 | 0 | break; |
10962 | 0 | default: ZEND_UNREACHABLE(); |
10963 | 0 | } |
10964 | | |
10965 | 0 | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL); |
10966 | 0 | SET_NODE(opline->result, result); |
10967 | |
|
10968 | 0 | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
10969 | 0 | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
10970 | 0 | need_frees = true; |
10971 | 0 | break; |
10972 | 0 | } |
10973 | 0 | } ZEND_HASH_FOREACH_END(); |
10974 | | |
10975 | | /* Free DUPed expressions if there are any */ |
10976 | 0 | if (need_frees) { |
10977 | 0 | uint32_t jump_opnum = zend_emit_jump(0); |
10978 | 0 | zend_update_jump_target_to_next(coalesce_opnum); |
10979 | 0 | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
10980 | 0 | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
10981 | 0 | zend_emit_op(NULL, ZEND_FREE, node, NULL); |
10982 | 0 | } |
10983 | 0 | } ZEND_HASH_FOREACH_END(); |
10984 | 0 | zend_update_jump_target_to_next(jump_opnum); |
10985 | 0 | } else { |
10986 | 0 | zend_update_jump_target_to_next(coalesce_opnum); |
10987 | 0 | } |
10988 | |
|
10989 | 0 | zend_hash_destroy(CG(memoized_exprs)); |
10990 | 0 | FREE_HASHTABLE(CG(memoized_exprs)); |
10991 | 0 | CG(memoized_exprs) = orig_memoized_exprs; |
10992 | 0 | CG(memoize_mode) = orig_memoize_mode; |
10993 | 0 | } |
10994 | | /* }}} */ |
10995 | | |
10996 | | static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */ |
10997 | 0 | { |
10998 | 0 | zend_op *opline; |
10999 | 0 | zend_ast *expr_ast = ast->child[0]; |
11000 | |
|
11001 | 0 | znode expr_node; |
11002 | 0 | zend_compile_expr(&expr_node, expr_ast); |
11003 | |
|
11004 | 0 | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
11005 | 0 | opline->extended_value = 1; |
11006 | |
|
11007 | 0 | result->op_type = IS_CONST; |
11008 | 0 | ZVAL_LONG(&result->u.constant, 1); |
11009 | 0 | } |
11010 | | /* }}} */ |
11011 | | |
11012 | | static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */ |
11013 | 0 | { |
11014 | 0 | zend_ast *value_ast = ast->child[0]; |
11015 | 0 | zend_ast *key_ast = ast->child[1]; |
11016 | |
|
11017 | 0 | znode value_node, key_node; |
11018 | 0 | znode *value_node_ptr = NULL, *key_node_ptr = NULL; |
11019 | 0 | zend_op *opline; |
11020 | 0 | bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
11021 | |
|
11022 | 0 | zend_mark_function_as_generator(); |
11023 | |
|
11024 | 0 | if (key_ast) { |
11025 | 0 | zend_compile_expr(&key_node, key_ast); |
11026 | 0 | key_node_ptr = &key_node; |
11027 | 0 | } |
11028 | |
|
11029 | 0 | if (value_ast) { |
11030 | 0 | if (returns_by_ref && zend_is_variable_or_call(value_ast)) { |
11031 | 0 | zend_assert_not_short_circuited(value_ast); |
11032 | 0 | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11033 | 0 | } else { |
11034 | 0 | zend_compile_expr(&value_node, value_ast); |
11035 | 0 | } |
11036 | 0 | value_node_ptr = &value_node; |
11037 | 0 | } |
11038 | |
|
11039 | 0 | opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr); |
11040 | |
|
11041 | 0 | if (value_ast && returns_by_ref && zend_is_call(value_ast)) { |
11042 | 0 | opline->extended_value = ZEND_RETURNS_FUNCTION; |
11043 | 0 | } |
11044 | 0 | } |
11045 | | /* }}} */ |
11046 | | |
11047 | | static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */ |
11048 | 0 | { |
11049 | 0 | zend_ast *expr_ast = ast->child[0]; |
11050 | 0 | znode expr_node; |
11051 | |
|
11052 | 0 | zend_mark_function_as_generator(); |
11053 | |
|
11054 | 0 | if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) { |
11055 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11056 | 0 | "Cannot use \"yield from\" inside a by-reference generator"); |
11057 | 0 | } |
11058 | | |
11059 | 0 | zend_compile_expr(&expr_node, expr_ast); |
11060 | 0 | zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL); |
11061 | 0 | } |
11062 | | /* }}} */ |
11063 | | |
11064 | | static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */ |
11065 | 0 | { |
11066 | 0 | zend_ast *obj_ast = ast->child[0]; |
11067 | 0 | zend_ast *class_ast = ast->child[1]; |
11068 | |
|
11069 | 0 | znode obj_node, class_node; |
11070 | 0 | zend_op *opline; |
11071 | |
|
11072 | 0 | zend_compile_expr(&obj_node, obj_ast); |
11073 | 0 | if (obj_node.op_type == IS_CONST) { |
11074 | 0 | zend_do_free(&obj_node); |
11075 | 0 | result->op_type = IS_CONST; |
11076 | 0 | ZVAL_FALSE(&result->u.constant); |
11077 | 0 | return; |
11078 | 0 | } |
11079 | | |
11080 | 0 | zend_compile_class_ref(&class_node, class_ast, |
11081 | 0 | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT); |
11082 | |
|
11083 | 0 | opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL); |
11084 | |
|
11085 | 0 | if (class_node.op_type == IS_CONST) { |
11086 | 0 | opline->op2_type = IS_CONST; |
11087 | 0 | opline->op2.constant = zend_add_class_name_literal( |
11088 | 0 | Z_STR(class_node.u.constant)); |
11089 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
11090 | 0 | } else { |
11091 | 0 | SET_NODE(opline->op2, &class_node); |
11092 | 0 | } |
11093 | 0 | } |
11094 | | /* }}} */ |
11095 | | |
11096 | | static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */ |
11097 | 0 | { |
11098 | 0 | zend_ast *expr_ast = ast->child[0]; |
11099 | 0 | znode expr_node; |
11100 | 0 | zend_op *opline; |
11101 | |
|
11102 | 0 | zend_do_extended_fcall_begin(); |
11103 | 0 | zend_compile_expr(&expr_node, expr_ast); |
11104 | |
|
11105 | 0 | opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL); |
11106 | 0 | opline->extended_value = ast->attr; |
11107 | |
|
11108 | 0 | zend_do_extended_fcall_end(); |
11109 | 0 | } |
11110 | | /* }}} */ |
11111 | | |
11112 | | static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */ |
11113 | 0 | { |
11114 | 0 | zend_ast *var_ast = ast->child[0]; |
11115 | |
|
11116 | 0 | znode var_node; |
11117 | 0 | zend_op *opline = NULL; |
11118 | |
|
11119 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY); |
11120 | |
|
11121 | 0 | if (!zend_is_variable(var_ast)) { |
11122 | 0 | if (ast->kind == ZEND_AST_EMPTY) { |
11123 | | /* empty(expr) can be transformed to !expr */ |
11124 | 0 | zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast); |
11125 | 0 | zend_compile_expr(result, not_ast); |
11126 | 0 | return; |
11127 | 0 | } else { |
11128 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11129 | 0 | "Cannot use isset() on the result of an expression " |
11130 | 0 | "(you can use \"null !== expression\" instead)"); |
11131 | 0 | } |
11132 | 0 | } |
11133 | | |
11134 | 0 | if (is_globals_fetch(var_ast)) { |
11135 | 0 | result->op_type = IS_CONST; |
11136 | 0 | ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET); |
11137 | 0 | return; |
11138 | 0 | } |
11139 | | |
11140 | 0 | if (is_global_var_fetch(var_ast)) { |
11141 | 0 | if (!var_ast->child[1]) { |
11142 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
11143 | 0 | } |
11144 | | |
11145 | 0 | zend_compile_expr(&var_node, var_ast->child[1]); |
11146 | 0 | if (var_node.op_type == IS_CONST) { |
11147 | 0 | convert_to_string(&var_node.u.constant); |
11148 | 0 | } |
11149 | |
|
11150 | 0 | opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL); |
11151 | 0 | opline->extended_value = |
11152 | 0 | ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0); |
11153 | 0 | return; |
11154 | 0 | } |
11155 | | |
11156 | 0 | zend_short_circuiting_mark_inner(var_ast); |
11157 | 0 | switch (var_ast->kind) { |
11158 | 0 | case ZEND_AST_VAR: |
11159 | 0 | if (is_this_fetch(var_ast)) { |
11160 | 0 | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL); |
11161 | 0 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
11162 | 0 | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) { |
11163 | 0 | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL); |
11164 | 0 | } else { |
11165 | 0 | opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false); |
11166 | 0 | opline->opcode = ZEND_ISSET_ISEMPTY_VAR; |
11167 | 0 | } |
11168 | 0 | break; |
11169 | 0 | case ZEND_AST_DIM: |
11170 | 0 | opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false); |
11171 | 0 | opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ; |
11172 | 0 | break; |
11173 | 0 | case ZEND_AST_PROP: |
11174 | 0 | case ZEND_AST_NULLSAFE_PROP: |
11175 | 0 | opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false); |
11176 | 0 | opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ; |
11177 | 0 | break; |
11178 | 0 | case ZEND_AST_STATIC_PROP: |
11179 | 0 | opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false); |
11180 | 0 | opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP; |
11181 | 0 | break; |
11182 | 0 | default: ZEND_UNREACHABLE(); |
11183 | 0 | } |
11184 | | |
11185 | 0 | result->op_type = opline->result_type = IS_TMP_VAR; |
11186 | 0 | if (!(ast->kind == ZEND_AST_ISSET)) { |
11187 | 0 | opline->extended_value |= ZEND_ISEMPTY; |
11188 | 0 | } |
11189 | 0 | } |
11190 | | /* }}} */ |
11191 | | |
11192 | | static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */ |
11193 | 0 | { |
11194 | 0 | zend_ast *expr_ast = ast->child[0]; |
11195 | 0 | znode silence_node; |
11196 | |
|
11197 | 0 | zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL); |
11198 | |
|
11199 | 0 | if (expr_ast->kind == ZEND_AST_VAR) { |
11200 | | /* For @$var we need to force a FETCH instruction, otherwise the CV access will |
11201 | | * happen outside the silenced section. */ |
11202 | 0 | zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false ); |
11203 | 0 | } else { |
11204 | 0 | zend_compile_expr(result, expr_ast); |
11205 | 0 | } |
11206 | |
|
11207 | 0 | zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL); |
11208 | 0 | } |
11209 | | /* }}} */ |
11210 | | |
11211 | | static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */ |
11212 | 0 | { |
11213 | 0 | zend_ast *expr_ast = ast->child[0]; |
11214 | |
|
11215 | 0 | zval fn_name; |
11216 | 0 | zend_ast *name_ast, *args_ast, *call_ast; |
11217 | |
|
11218 | 0 | zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead"); |
11219 | |
|
11220 | 0 | ZVAL_STRING(&fn_name, "shell_exec"); |
11221 | 0 | name_ast = zend_ast_create_zval(&fn_name); |
11222 | 0 | args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast); |
11223 | 0 | call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast); |
11224 | |
|
11225 | 0 | zend_compile_expr(result, call_ast); |
11226 | |
|
11227 | 0 | zval_ptr_dtor(&fn_name); |
11228 | 0 | } |
11229 | | /* }}} */ |
11230 | | |
11231 | | static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */ |
11232 | 0 | { |
11233 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
11234 | 0 | zend_op *opline; |
11235 | 0 | uint32_t i, opnum_init = -1; |
11236 | 0 | bool packed = true; |
11237 | |
|
11238 | 0 | if (zend_try_ct_eval_array(&result->u.constant, ast)) { |
11239 | 0 | result->op_type = IS_CONST; |
11240 | 0 | return; |
11241 | 0 | } |
11242 | | |
11243 | | /* Empty arrays are handled at compile-time */ |
11244 | 0 | ZEND_ASSERT(list->children > 0); |
11245 | |
|
11246 | 0 | for (i = 0; i < list->children; ++i) { |
11247 | 0 | zend_ast *elem_ast = list->child[i]; |
11248 | 0 | zend_ast *value_ast, *key_ast; |
11249 | 0 | bool by_ref; |
11250 | 0 | znode value_node, key_node, *key_node_ptr = NULL; |
11251 | |
|
11252 | 0 | if (elem_ast == NULL) { |
11253 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
11254 | 0 | } |
11255 | |
|
11256 | 0 | value_ast = elem_ast->child[0]; |
11257 | |
|
11258 | 0 | if (elem_ast->kind == ZEND_AST_UNPACK) { |
11259 | 0 | zend_compile_expr(&value_node, value_ast); |
11260 | 0 | if (i == 0) { |
11261 | 0 | opnum_init = get_next_op_number(); |
11262 | 0 | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
11263 | 0 | } |
11264 | 0 | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL); |
11265 | 0 | SET_NODE(opline->result, result); |
11266 | 0 | continue; |
11267 | 0 | } |
11268 | | |
11269 | 0 | key_ast = elem_ast->child[1]; |
11270 | 0 | by_ref = elem_ast->attr; |
11271 | |
|
11272 | 0 | if (key_ast) { |
11273 | 0 | zend_compile_expr(&key_node, key_ast); |
11274 | 0 | zend_handle_numeric_op(&key_node); |
11275 | 0 | key_node_ptr = &key_node; |
11276 | 0 | } |
11277 | |
|
11278 | 0 | if (by_ref) { |
11279 | 0 | zend_ensure_writable_variable(value_ast); |
11280 | 0 | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11281 | 0 | } else { |
11282 | 0 | zend_compile_expr(&value_node, value_ast); |
11283 | 0 | } |
11284 | |
|
11285 | 0 | if (i == 0) { |
11286 | 0 | opnum_init = get_next_op_number(); |
11287 | 0 | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr); |
11288 | 0 | opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT; |
11289 | 0 | } else { |
11290 | 0 | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, |
11291 | 0 | &value_node, key_node_ptr); |
11292 | 0 | SET_NODE(opline->result, result); |
11293 | 0 | } |
11294 | 0 | opline->extended_value |= by_ref; |
11295 | |
|
11296 | 0 | if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) { |
11297 | 0 | packed = false; |
11298 | 0 | } |
11299 | 0 | } |
11300 | | |
11301 | | /* Add a flag to INIT_ARRAY if we know this array cannot be packed */ |
11302 | 0 | if (!packed) { |
11303 | 0 | ZEND_ASSERT(opnum_init != (uint32_t)-1); |
11304 | 0 | opline = &CG(active_op_array)->opcodes[opnum_init]; |
11305 | 0 | opline->extended_value |= ZEND_ARRAY_NOT_PACKED; |
11306 | 0 | } |
11307 | 0 | } |
11308 | | /* }}} */ |
11309 | | |
11310 | | static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */ |
11311 | 0 | { |
11312 | 0 | zend_ast *name_ast = ast->child[0]; |
11313 | |
|
11314 | 0 | zend_op *opline; |
11315 | |
|
11316 | 0 | bool is_fully_qualified; |
11317 | 0 | zend_string *orig_name = zend_ast_get_str(name_ast); |
11318 | 0 | zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified); |
11319 | |
|
11320 | 0 | if (zend_string_equals_literal(resolved_name, "__COMPILER_HALT_OFFSET__") || (name_ast->attr != ZEND_NAME_RELATIVE && zend_string_equals_literal(orig_name, "__COMPILER_HALT_OFFSET__"))) { |
11321 | 0 | zend_ast *last = CG(ast); |
11322 | |
|
11323 | 0 | while (last && last->kind == ZEND_AST_STMT_LIST) { |
11324 | 0 | const zend_ast_list *list = zend_ast_get_list(last); |
11325 | 0 | if (list->children == 0) { |
11326 | 0 | break; |
11327 | 0 | } |
11328 | 0 | last = list->child[list->children-1]; |
11329 | 0 | } |
11330 | 0 | if (last && last->kind == ZEND_AST_HALT_COMPILER) { |
11331 | 0 | result->op_type = IS_CONST; |
11332 | 0 | ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0]))); |
11333 | 0 | zend_string_release_ex(resolved_name, 0); |
11334 | 0 | return; |
11335 | 0 | } |
11336 | 0 | } |
11337 | | |
11338 | 0 | if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) { |
11339 | 0 | result->op_type = IS_CONST; |
11340 | 0 | zend_string_release_ex(resolved_name, 0); |
11341 | 0 | return; |
11342 | 0 | } |
11343 | | |
11344 | 0 | opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL); |
11345 | 0 | opline->op2_type = IS_CONST; |
11346 | |
|
11347 | 0 | if (is_fully_qualified || !FC(current_namespace)) { |
11348 | 0 | opline->op1.num = 0; |
11349 | 0 | opline->op2.constant = zend_add_const_name_literal( |
11350 | 0 | resolved_name, false); |
11351 | 0 | } else { |
11352 | 0 | opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE; |
11353 | 0 | opline->op2.constant = zend_add_const_name_literal( |
11354 | 0 | resolved_name, true); |
11355 | 0 | } |
11356 | 0 | opline->extended_value = zend_alloc_cache_slot(); |
11357 | 0 | } |
11358 | | /* }}} */ |
11359 | | |
11360 | | static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */ |
11361 | 0 | { |
11362 | 0 | zend_ast *class_ast; |
11363 | 0 | zend_ast *const_ast; |
11364 | 0 | znode class_node, const_node; |
11365 | 0 | zend_op *opline; |
11366 | |
|
11367 | 0 | zend_eval_const_expr(&ast->child[0]); |
11368 | 0 | zend_eval_const_expr(&ast->child[1]); |
11369 | |
|
11370 | 0 | class_ast = ast->child[0]; |
11371 | 0 | const_ast = ast->child[1]; |
11372 | |
|
11373 | 0 | if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) { |
11374 | 0 | zval *const_zv = zend_ast_get_zval(const_ast); |
11375 | 0 | if (Z_TYPE_P(const_zv) == IS_STRING) { |
11376 | 0 | zend_string *const_str = Z_STR_P(const_zv); |
11377 | 0 | zend_string *resolved_name = zend_resolve_class_name_ast(class_ast); |
11378 | 0 | if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) { |
11379 | 0 | result->op_type = IS_CONST; |
11380 | 0 | zend_string_release_ex(resolved_name, 0); |
11381 | 0 | return; |
11382 | 0 | } |
11383 | 0 | zend_string_release_ex(resolved_name, 0); |
11384 | 0 | } |
11385 | 0 | } |
11386 | | |
11387 | 0 | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
11388 | |
|
11389 | 0 | zend_compile_expr(&const_node, const_ast); |
11390 | |
|
11391 | 0 | opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node); |
11392 | |
|
11393 | 0 | zend_set_class_name_op1(opline, &class_node); |
11394 | |
|
11395 | 0 | if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) { |
11396 | 0 | opline->extended_value = zend_alloc_cache_slots(2); |
11397 | 0 | } |
11398 | 0 | } |
11399 | | /* }}} */ |
11400 | | |
11401 | | static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */ |
11402 | 0 | { |
11403 | 0 | zend_ast *class_ast = ast->child[0]; |
11404 | |
|
11405 | 0 | if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) { |
11406 | 0 | result->op_type = IS_CONST; |
11407 | 0 | return; |
11408 | 0 | } |
11409 | | |
11410 | 0 | if (class_ast->kind == ZEND_AST_ZVAL) { |
11411 | 0 | zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11412 | 0 | opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast)); |
11413 | 0 | } else { |
11414 | 0 | znode expr_node; |
11415 | 0 | zend_compile_expr(&expr_node, class_ast); |
11416 | 0 | if (expr_node.op_type == IS_CONST) { |
11417 | | /* Unlikely case that happen if class_ast is constant folded. |
11418 | | * Handle it here, to avoid needing a CONST specialization in the VM. */ |
11419 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s", |
11420 | 0 | zend_zval_value_name(&expr_node.u.constant)); |
11421 | 0 | } |
11422 | | |
11423 | 0 | zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL); |
11424 | 0 | } |
11425 | 0 | } |
11426 | | /* }}} */ |
11427 | | |
11428 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11429 | 0 | { |
11430 | 0 | if (num == 0) { |
11431 | 0 | result->op_type = IS_TMP_VAR; |
11432 | 0 | result->u.op.var = -1; |
11433 | 0 | opline->opcode = ZEND_ROPE_INIT; |
11434 | 0 | } else { |
11435 | 0 | opline->opcode = ZEND_ROPE_ADD; |
11436 | 0 | SET_NODE(opline->op1, result); |
11437 | 0 | } |
11438 | 0 | SET_NODE(opline->op2, elem_node); |
11439 | 0 | SET_NODE(opline->result, result); |
11440 | 0 | opline->extended_value = num; |
11441 | 0 | return opline; |
11442 | 0 | } |
11443 | | /* }}} */ |
11444 | | |
11445 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11446 | 0 | { |
11447 | 0 | zend_op *opline = get_next_op(); |
11448 | |
|
11449 | 0 | if (num == 0) { |
11450 | 0 | result->op_type = IS_TMP_VAR; |
11451 | 0 | result->u.op.var = -1; |
11452 | 0 | opline->opcode = ZEND_ROPE_INIT; |
11453 | 0 | } else { |
11454 | 0 | opline->opcode = ZEND_ROPE_ADD; |
11455 | 0 | SET_NODE(opline->op1, result); |
11456 | 0 | } |
11457 | 0 | SET_NODE(opline->op2, elem_node); |
11458 | 0 | SET_NODE(opline->result, result); |
11459 | 0 | opline->extended_value = num; |
11460 | 0 | return opline; |
11461 | 0 | } |
11462 | | /* }}} */ |
11463 | | |
11464 | | static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline) |
11465 | 0 | { |
11466 | 0 | if (rope_elements == 1) { |
11467 | 0 | if (opline->op2_type == IS_CONST) { |
11468 | 0 | GET_NODE(result, opline->op2); |
11469 | 0 | ZVAL_UNDEF(CT_CONSTANT(opline->op2)); |
11470 | 0 | SET_UNUSED(opline->op2); |
11471 | 0 | MAKE_NOP(opline); |
11472 | 0 | } else { |
11473 | 0 | opline->opcode = ZEND_CAST; |
11474 | 0 | opline->extended_value = IS_STRING; |
11475 | 0 | opline->op1_type = opline->op2_type; |
11476 | 0 | opline->op1 = opline->op2; |
11477 | 0 | SET_UNUSED(opline->op2); |
11478 | 0 | zend_make_tmp_result(result, opline); |
11479 | 0 | } |
11480 | 0 | } else if (rope_elements == 2) { |
11481 | 0 | opline->opcode = ZEND_FAST_CONCAT; |
11482 | 0 | opline->extended_value = 0; |
11483 | 0 | opline->op1_type = init_opline->op2_type; |
11484 | 0 | opline->op1 = init_opline->op2; |
11485 | 0 | zend_make_tmp_result(result, opline); |
11486 | 0 | MAKE_NOP(init_opline); |
11487 | 0 | } else { |
11488 | 0 | uint32_t var; |
11489 | |
|
11490 | 0 | init_opline->extended_value = rope_elements; |
11491 | 0 | opline->opcode = ZEND_ROPE_END; |
11492 | 0 | zend_make_tmp_result(result, opline); |
11493 | 0 | var = opline->op1.var = get_temporary_variable(); |
11494 | | |
11495 | | /* Allocates the necessary number of zval slots to keep the rope */ |
11496 | 0 | uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval); |
11497 | 0 | while (i > 1) { |
11498 | 0 | get_temporary_variable(); |
11499 | 0 | i--; |
11500 | 0 | } |
11501 | | |
11502 | | /* Update all the previous opcodes to use the same variable */ |
11503 | 0 | while (opline != init_opline) { |
11504 | 0 | opline--; |
11505 | 0 | if (opline->opcode == ZEND_ROPE_ADD && |
11506 | 0 | opline->result.var == (uint32_t)-1) { |
11507 | 0 | opline->op1.var = var; |
11508 | 0 | opline->result.var = var; |
11509 | 0 | } else if (opline->opcode == ZEND_ROPE_INIT && |
11510 | 0 | opline->result.var == (uint32_t)-1) { |
11511 | 0 | opline->result.var = var; |
11512 | 0 | } |
11513 | 0 | } |
11514 | 0 | } |
11515 | 0 | } |
11516 | | |
11517 | | static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */ |
11518 | 0 | { |
11519 | 0 | uint32_t i, j; |
11520 | 0 | uint32_t rope_init_lineno = -1; |
11521 | 0 | zend_op *opline = NULL, *init_opline; |
11522 | 0 | znode elem_node, last_const_node; |
11523 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
11524 | 0 | uint32_t reserved_op_number = -1; |
11525 | |
|
11526 | 0 | ZEND_ASSERT(list->children > 0); |
11527 | |
|
11528 | 0 | j = 0; |
11529 | 0 | last_const_node.op_type = IS_UNUSED; |
11530 | 0 | for (i = 0; i < list->children; i++) { |
11531 | 0 | zend_ast *encaps_var = list->child[i]; |
11532 | |
|
11533 | 0 | if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11534 | 0 | if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) { |
11535 | 0 | zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead"); |
11536 | 0 | } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11537 | 0 | zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead"); |
11538 | 0 | } |
11539 | 0 | } |
11540 | |
|
11541 | 0 | zend_compile_expr(&elem_node, encaps_var); |
11542 | |
|
11543 | 0 | if (elem_node.op_type == IS_CONST) { |
11544 | 0 | convert_to_string(&elem_node.u.constant); |
11545 | |
|
11546 | 0 | if (Z_STRLEN(elem_node.u.constant) == 0) { |
11547 | 0 | zval_ptr_dtor(&elem_node.u.constant); |
11548 | 0 | } else if (last_const_node.op_type == IS_CONST) { |
11549 | 0 | concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant); |
11550 | 0 | zval_ptr_dtor(&elem_node.u.constant); |
11551 | 0 | } else { |
11552 | 0 | last_const_node.op_type = IS_CONST; |
11553 | 0 | ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant); |
11554 | | /* Reserve place for ZEND_ROPE_ADD instruction */ |
11555 | 0 | reserved_op_number = get_next_op_number(); |
11556 | 0 | opline = get_next_op(); |
11557 | 0 | opline->opcode = ZEND_NOP; |
11558 | 0 | } |
11559 | 0 | continue; |
11560 | 0 | } else { |
11561 | 0 | if (j == 0) { |
11562 | 0 | if (last_const_node.op_type == IS_CONST) { |
11563 | 0 | rope_init_lineno = reserved_op_number; |
11564 | 0 | } else { |
11565 | 0 | rope_init_lineno = get_next_op_number(); |
11566 | 0 | } |
11567 | 0 | } |
11568 | 0 | if (last_const_node.op_type == IS_CONST) { |
11569 | 0 | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11570 | 0 | zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11571 | 0 | last_const_node.op_type = IS_UNUSED; |
11572 | 0 | } |
11573 | 0 | opline = zend_compile_rope_add(result, j++, &elem_node); |
11574 | 0 | } |
11575 | 0 | } |
11576 | |
|
11577 | 0 | if (j == 0) { |
11578 | 0 | result->op_type = IS_CONST; |
11579 | 0 | if (last_const_node.op_type == IS_CONST) { |
11580 | 0 | ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant); |
11581 | 0 | } else { |
11582 | 0 | ZVAL_EMPTY_STRING(&result->u.constant); |
11583 | | /* empty string */ |
11584 | 0 | } |
11585 | 0 | CG(active_op_array)->last = reserved_op_number - 1; |
11586 | 0 | return; |
11587 | 0 | } else if (last_const_node.op_type == IS_CONST) { |
11588 | 0 | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11589 | 0 | opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11590 | 0 | } |
11591 | 0 | init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
11592 | 0 | zend_compile_rope_finalize(result, j, init_opline, opline); |
11593 | 0 | } |
11594 | | /* }}} */ |
11595 | | |
11596 | | static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */ |
11597 | 0 | { |
11598 | 0 | zend_op *opline; |
11599 | |
|
11600 | 0 | if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) { |
11601 | 0 | result->op_type = IS_CONST; |
11602 | 0 | return; |
11603 | 0 | } |
11604 | | |
11605 | 0 | ZEND_ASSERT(ast->attr == T_CLASS_C && |
11606 | 0 | CG(active_class_entry) && |
11607 | 0 | (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0); |
11608 | |
|
11609 | 0 | opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11610 | 0 | opline->op1.num = ZEND_FETCH_CLASS_SELF; |
11611 | 0 | } |
11612 | | /* }}} */ |
11613 | | |
11614 | | static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */ |
11615 | 0 | { |
11616 | 0 | return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP |
11617 | 0 | || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL |
11618 | 0 | || kind == ZEND_AST_AND || kind == ZEND_AST_OR |
11619 | 0 | || kind == ZEND_AST_UNARY_OP |
11620 | 0 | || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS |
11621 | 0 | || kind == ZEND_AST_CAST |
11622 | 0 | || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM |
11623 | 0 | || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM |
11624 | 0 | || kind == ZEND_AST_UNPACK |
11625 | 0 | || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST |
11626 | 0 | || kind == ZEND_AST_CLASS_NAME |
11627 | 0 | || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE |
11628 | 0 | || kind == ZEND_AST_CONST_ENUM_INIT |
11629 | 0 | || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST |
11630 | 0 | || kind == ZEND_AST_NAMED_ARG |
11631 | 0 | || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP |
11632 | 0 | || kind == ZEND_AST_CLOSURE |
11633 | 0 | || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT; |
11634 | 0 | } |
11635 | | /* }}} */ |
11636 | | |
11637 | | static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */ |
11638 | 0 | { |
11639 | 0 | zend_ast *ast = *ast_ptr; |
11640 | 0 | zend_ast *class_ast = ast->child[0]; |
11641 | 0 | zend_string *class_name; |
11642 | 0 | int fetch_type; |
11643 | |
|
11644 | 0 | if (class_ast->kind != ZEND_AST_ZVAL) { |
11645 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11646 | 0 | "Dynamic class names are not allowed in compile-time class constant references"); |
11647 | 0 | } |
11648 | 0 | if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) { |
11649 | 0 | zend_throw_error(NULL, "Class name must be a valid object or a string"); |
11650 | 0 | } |
11651 | |
|
11652 | 0 | class_name = zend_ast_get_str(class_ast); |
11653 | 0 | fetch_type = zend_get_class_fetch_type(class_name); |
11654 | |
|
11655 | 0 | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
11656 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11657 | 0 | "\"static::\" is not allowed in compile-time constants"); |
11658 | 0 | } |
11659 | | |
11660 | 0 | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
11661 | 0 | zend_string *tmp = zend_resolve_class_name_ast(class_ast); |
11662 | |
|
11663 | 0 | zend_string_release_ex(class_name, 0); |
11664 | 0 | if (tmp != class_name) { |
11665 | 0 | zval *zv = zend_ast_get_zval(class_ast); |
11666 | 0 | ZVAL_STR(zv, tmp); |
11667 | 0 | class_ast->attr = ZEND_NAME_FQ; |
11668 | 0 | } |
11669 | 0 | } |
11670 | |
|
11671 | 0 | ast->attr |= ZEND_FETCH_CLASS_EXCEPTION; |
11672 | 0 | } |
11673 | | /* }}} */ |
11674 | | |
11675 | | static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */ |
11676 | 0 | { |
11677 | 0 | zend_ast *ast = *ast_ptr; |
11678 | 0 | zend_ast *class_ast = ast->child[0]; |
11679 | 0 | if (class_ast->kind != ZEND_AST_ZVAL) { |
11680 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11681 | 0 | "(expression)::class cannot be used in constant expressions"); |
11682 | 0 | } |
11683 | | |
11684 | 0 | zend_string *class_name = zend_ast_get_str(class_ast); |
11685 | 0 | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
11686 | |
|
11687 | 0 | switch (fetch_type) { |
11688 | 0 | case ZEND_FETCH_CLASS_SELF: |
11689 | 0 | case ZEND_FETCH_CLASS_PARENT: |
11690 | | /* For the const-eval representation store the fetch type instead of the name. */ |
11691 | 0 | zend_string_release(class_name); |
11692 | 0 | ast->child[0] = NULL; |
11693 | 0 | ast->attr = fetch_type; |
11694 | 0 | return; |
11695 | 0 | case ZEND_FETCH_CLASS_STATIC: |
11696 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11697 | 0 | "static::class cannot be used for compile-time class name resolution"); |
11698 | 0 | default: ZEND_UNREACHABLE(); |
11699 | 0 | } |
11700 | 0 | } |
11701 | | |
11702 | | static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */ |
11703 | 0 | { |
11704 | 0 | zend_ast *ast = *ast_ptr; |
11705 | 0 | zend_ast *name_ast = ast->child[0]; |
11706 | 0 | zend_string *orig_name = zend_ast_get_str(name_ast); |
11707 | 0 | bool is_fully_qualified; |
11708 | 0 | zval result; |
11709 | 0 | zend_string *resolved_name; |
11710 | |
|
11711 | 0 | CG(zend_lineno) = zend_ast_get_lineno(ast); |
11712 | |
|
11713 | 0 | resolved_name = zend_resolve_const_name( |
11714 | 0 | orig_name, name_ast->attr, &is_fully_qualified); |
11715 | |
|
11716 | 0 | if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
11717 | 0 | zend_string_release_ex(resolved_name, 0); |
11718 | 0 | zend_ast_destroy(ast); |
11719 | 0 | *ast_ptr = zend_ast_create_zval(&result); |
11720 | 0 | return; |
11721 | 0 | } |
11722 | | |
11723 | 0 | zend_ast_destroy(ast); |
11724 | 0 | *ast_ptr = zend_ast_create_constant(resolved_name, |
11725 | 0 | !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0); |
11726 | 0 | } |
11727 | | /* }}} */ |
11728 | | |
11729 | | static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */ |
11730 | 0 | { |
11731 | 0 | zend_ast *ast = *ast_ptr; |
11732 | | |
11733 | | /* Other cases already resolved by constant folding */ |
11734 | 0 | ZEND_ASSERT(ast->attr == T_CLASS_C); |
11735 | |
|
11736 | 0 | zend_ast_destroy(ast); |
11737 | 0 | *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS); |
11738 | 0 | } |
11739 | | /* }}} */ |
11740 | | |
11741 | | static void zend_compile_const_expr_class_reference(zend_ast *class_ast) |
11742 | 0 | { |
11743 | 0 | if (class_ast->kind == ZEND_AST_CLASS) { |
11744 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11745 | 0 | "Cannot use anonymous class in constant expression"); |
11746 | 0 | } |
11747 | 0 | if (class_ast->kind != ZEND_AST_ZVAL) { |
11748 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11749 | 0 | "Cannot use dynamic class name in constant expression"); |
11750 | 0 | } |
11751 | | |
11752 | 0 | zend_string *class_name = zend_resolve_class_name_ast(class_ast); |
11753 | 0 | int fetch_type = zend_get_class_fetch_type(class_name); |
11754 | 0 | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
11755 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11756 | 0 | "\"static\" is not allowed in compile-time constants"); |
11757 | 0 | } |
11758 | | |
11759 | 0 | zval *class_ast_zv = zend_ast_get_zval(class_ast); |
11760 | 0 | zval_ptr_dtor_nogc(class_ast_zv); |
11761 | 0 | ZVAL_STR(class_ast_zv, class_name); |
11762 | 0 | class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT; |
11763 | 0 | } |
11764 | | |
11765 | | static void zend_compile_const_expr_new(zend_ast **ast_ptr) |
11766 | 0 | { |
11767 | 0 | zend_ast *class_ast = (*ast_ptr)->child[0]; |
11768 | 0 | zend_compile_const_expr_class_reference(class_ast); |
11769 | |
|
11770 | 0 | const zend_ast *args_ast = (*ast_ptr)->child[1]; |
11771 | 0 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
11772 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
11773 | 0 | } |
11774 | 0 | } |
11775 | | |
11776 | | static void zend_compile_const_expr_closure(zend_ast **ast_ptr) |
11777 | 0 | { |
11778 | 0 | zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr; |
11779 | 0 | const zend_ast *uses_ast = closure_ast->child[1]; |
11780 | 0 | if (!(closure_ast->flags & ZEND_ACC_STATIC)) { |
11781 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11782 | 0 | "Closures in constant expressions must be static"); |
11783 | 0 | } |
11784 | 0 | if (uses_ast) { |
11785 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11786 | 0 | "Cannot use(...) variables in constant expression"); |
11787 | 0 | } |
11788 | | |
11789 | 0 | znode node; |
11790 | 0 | zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR); |
11791 | |
|
11792 | 0 | zend_ast_destroy(*ast_ptr); |
11793 | 0 | *ast_ptr = zend_ast_create_op_array(op); |
11794 | 0 | } |
11795 | | |
11796 | | static void zend_compile_const_expr_fcc(zend_ast **ast_ptr) |
11797 | 0 | { |
11798 | 0 | zend_ast **args_ast; |
11799 | 0 | switch ((*ast_ptr)->kind) { |
11800 | 0 | case ZEND_AST_CALL: |
11801 | 0 | args_ast = &(*ast_ptr)->child[1]; |
11802 | 0 | break; |
11803 | 0 | case ZEND_AST_STATIC_CALL: |
11804 | 0 | args_ast = &(*ast_ptr)->child[2]; |
11805 | 0 | break; |
11806 | 0 | default: ZEND_UNREACHABLE(); |
11807 | 0 | } |
11808 | 0 | if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) { |
11809 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
11810 | 0 | } |
11811 | 0 | ZEND_MAP_PTR_NEW(((zend_ast_fcc *)*args_ast)->fptr); |
11812 | |
|
11813 | 0 | switch ((*ast_ptr)->kind) { |
11814 | 0 | case ZEND_AST_CALL: { |
11815 | 0 | zend_ast *name_ast = (*ast_ptr)->child[0]; |
11816 | 0 | if (name_ast->kind != ZEND_AST_ZVAL) { |
11817 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression"); |
11818 | 0 | } |
11819 | 0 | zval *name_ast_zv = zend_ast_get_zval(name_ast); |
11820 | 0 | if (Z_TYPE_P(name_ast_zv) != IS_STRING) { |
11821 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name"); |
11822 | 0 | } |
11823 | 0 | bool is_fully_qualified; |
11824 | 0 | zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified); |
11825 | 0 | zval_ptr_dtor_nogc(name_ast_zv); |
11826 | 0 | ZVAL_STR(name_ast_zv, name); |
11827 | 0 | if (is_fully_qualified) { |
11828 | 0 | name_ast->attr = ZEND_NAME_FQ; |
11829 | 0 | } |
11830 | 0 | break; |
11831 | 0 | } |
11832 | 0 | case ZEND_AST_STATIC_CALL: { |
11833 | 0 | zend_ast *class_ast = (*ast_ptr)->child[0]; |
11834 | 0 | zend_compile_const_expr_class_reference(class_ast); |
11835 | 0 | zend_ast *method_ast = (*ast_ptr)->child[1]; |
11836 | 0 | if (method_ast->kind != ZEND_AST_ZVAL) { |
11837 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression"); |
11838 | 0 | } |
11839 | 0 | if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) { |
11840 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name"); |
11841 | 0 | } |
11842 | 0 | break; |
11843 | 0 | } |
11844 | 0 | default: ZEND_UNREACHABLE(); |
11845 | 0 | } |
11846 | 0 | } |
11847 | | |
11848 | | static void zend_compile_const_expr_args(zend_ast **ast_ptr) |
11849 | 0 | { |
11850 | 0 | zend_ast_list *list = zend_ast_get_list(*ast_ptr); |
11851 | 0 | bool uses_named_args = false; |
11852 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
11853 | 0 | const zend_ast *arg = list->child[i]; |
11854 | 0 | if (arg->kind == ZEND_AST_UNPACK) { |
11855 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11856 | 0 | "Argument unpacking in constant expressions is not supported"); |
11857 | 0 | } |
11858 | 0 | if (arg->kind == ZEND_AST_NAMED_ARG) { |
11859 | 0 | uses_named_args = true; |
11860 | 0 | } else if (uses_named_args) { |
11861 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11862 | 0 | "Cannot use positional argument after named argument"); |
11863 | 0 | } |
11864 | 0 | } |
11865 | 0 | if (uses_named_args) { |
11866 | 0 | list->attr = 1; |
11867 | 0 | } |
11868 | 0 | } |
11869 | | |
11870 | | typedef struct { |
11871 | | /* Whether the value of this expression may differ on each evaluation. */ |
11872 | | bool allow_dynamic; |
11873 | | } const_expr_context; |
11874 | | |
11875 | | static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */ |
11876 | 0 | { |
11877 | 0 | const const_expr_context *ctx = context; |
11878 | 0 | zend_ast *ast = *ast_ptr; |
11879 | 0 | if (ast == NULL || ast->kind == ZEND_AST_ZVAL) { |
11880 | 0 | return; |
11881 | 0 | } |
11882 | | |
11883 | 0 | if (!zend_is_allowed_in_const_expr(ast->kind)) { |
11884 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
11885 | 0 | } |
11886 | | |
11887 | 0 | switch (ast->kind) { |
11888 | 0 | case ZEND_AST_CLASS_CONST: |
11889 | 0 | zend_compile_const_expr_class_const(ast_ptr); |
11890 | 0 | break; |
11891 | 0 | case ZEND_AST_CLASS_NAME: |
11892 | 0 | zend_compile_const_expr_class_name(ast_ptr); |
11893 | 0 | break; |
11894 | 0 | case ZEND_AST_CONST: |
11895 | 0 | zend_compile_const_expr_const(ast_ptr); |
11896 | 0 | break; |
11897 | 0 | case ZEND_AST_MAGIC_CONST: |
11898 | 0 | zend_compile_const_expr_magic_const(ast_ptr); |
11899 | 0 | break; |
11900 | 0 | case ZEND_AST_CAST: |
11901 | 0 | if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) { |
11902 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11903 | 0 | "Object casts are not supported in this context"); |
11904 | 0 | } |
11905 | 0 | break; |
11906 | 0 | case ZEND_AST_NEW: |
11907 | 0 | if (!ctx->allow_dynamic) { |
11908 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11909 | 0 | "New expressions are not supported in this context"); |
11910 | 0 | } |
11911 | 0 | zend_compile_const_expr_new(ast_ptr); |
11912 | 0 | break; |
11913 | 0 | case ZEND_AST_ARG_LIST: |
11914 | 0 | zend_compile_const_expr_args(ast_ptr); |
11915 | 0 | break; |
11916 | 0 | case ZEND_AST_CLOSURE: |
11917 | 0 | zend_compile_const_expr_closure(ast_ptr); |
11918 | | /* Return, because we do not want to traverse the children. */ |
11919 | 0 | return; |
11920 | 0 | case ZEND_AST_CALL: |
11921 | 0 | case ZEND_AST_STATIC_CALL: |
11922 | 0 | zend_compile_const_expr_fcc(ast_ptr); |
11923 | 0 | break; |
11924 | 0 | } |
11925 | | |
11926 | 0 | zend_ast_apply(ast, zend_compile_const_expr, context); |
11927 | 0 | } |
11928 | | /* }}} */ |
11929 | | |
11930 | | void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */ |
11931 | 0 | { |
11932 | 0 | const_expr_context context; |
11933 | 0 | context.allow_dynamic = allow_dynamic; |
11934 | |
|
11935 | 0 | zend_eval_const_expr(ast_ptr); |
11936 | 0 | zend_compile_const_expr(ast_ptr, &context); |
11937 | 0 | if ((*ast_ptr)->kind != ZEND_AST_ZVAL) { |
11938 | | /* Replace with compiled AST zval representation. */ |
11939 | 0 | zval ast_zv; |
11940 | 0 | ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr)); |
11941 | 0 | zend_ast_destroy(*ast_ptr); |
11942 | 0 | *ast_ptr = zend_ast_create_zval(&ast_zv); |
11943 | 0 | } |
11944 | 0 | ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr)); |
11945 | 0 | } |
11946 | | /* }}} */ |
11947 | | |
11948 | | /* Same as compile_stmt, but with early binding */ |
11949 | | void zend_compile_top_stmt(zend_ast *ast) /* {{{ */ |
11950 | 0 | { |
11951 | 0 | if (!ast) { |
11952 | 0 | return; |
11953 | 0 | } |
11954 | | |
11955 | 0 | if (ast->kind == ZEND_AST_STMT_LIST) { |
11956 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
11957 | 0 | uint32_t i; |
11958 | 0 | for (i = 0; i < list->children; ++i) { |
11959 | 0 | zend_compile_top_stmt(list->child[i]); |
11960 | 0 | } |
11961 | 0 | return; |
11962 | 0 | } |
11963 | | |
11964 | 0 | if (ast->kind == ZEND_AST_FUNC_DECL) { |
11965 | 0 | CG(zend_lineno) = ast->lineno; |
11966 | 0 | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL); |
11967 | 0 | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
11968 | 0 | } else if (ast->kind == ZEND_AST_CLASS) { |
11969 | 0 | CG(zend_lineno) = ast->lineno; |
11970 | 0 | zend_compile_class_decl(NULL, ast, true); |
11971 | 0 | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
11972 | 0 | } else { |
11973 | 0 | zend_compile_stmt(ast); |
11974 | 0 | } |
11975 | 0 | if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) { |
11976 | 0 | zend_verify_namespace(); |
11977 | 0 | } |
11978 | 0 | } |
11979 | | /* }}} */ |
11980 | | |
11981 | | static void zend_compile_stmt(zend_ast *ast) /* {{{ */ |
11982 | 0 | { |
11983 | 0 | if (!ast) { |
11984 | 0 | return; |
11985 | 0 | } |
11986 | | |
11987 | 0 | CG(zend_lineno) = ast->lineno; |
11988 | |
|
11989 | 0 | if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) { |
11990 | 0 | zend_do_extended_stmt(NULL); |
11991 | 0 | } |
11992 | |
|
11993 | 0 | switch (ast->kind) { |
11994 | 0 | case ZEND_AST_STMT_LIST: |
11995 | 0 | zend_compile_stmt_list(ast); |
11996 | 0 | break; |
11997 | 0 | case ZEND_AST_GLOBAL: |
11998 | 0 | zend_compile_global_var(ast); |
11999 | 0 | break; |
12000 | 0 | case ZEND_AST_STATIC: |
12001 | 0 | zend_compile_static_var(ast); |
12002 | 0 | break; |
12003 | 0 | case ZEND_AST_UNSET: |
12004 | 0 | zend_compile_unset(ast); |
12005 | 0 | break; |
12006 | 0 | case ZEND_AST_RETURN: |
12007 | 0 | zend_compile_return(ast); |
12008 | 0 | break; |
12009 | 0 | case ZEND_AST_ECHO: |
12010 | 0 | zend_compile_echo(ast); |
12011 | 0 | break; |
12012 | 0 | case ZEND_AST_BREAK: |
12013 | 0 | case ZEND_AST_CONTINUE: |
12014 | 0 | zend_compile_break_continue(ast); |
12015 | 0 | break; |
12016 | 0 | case ZEND_AST_GOTO: |
12017 | 0 | zend_compile_goto(ast); |
12018 | 0 | break; |
12019 | 0 | case ZEND_AST_LABEL: |
12020 | 0 | zend_compile_label(ast); |
12021 | 0 | break; |
12022 | 0 | case ZEND_AST_WHILE: |
12023 | 0 | zend_compile_while(ast); |
12024 | 0 | break; |
12025 | 0 | case ZEND_AST_DO_WHILE: |
12026 | 0 | zend_compile_do_while(ast); |
12027 | 0 | break; |
12028 | 0 | case ZEND_AST_FOR: |
12029 | 0 | zend_compile_for(ast); |
12030 | 0 | break; |
12031 | 0 | case ZEND_AST_FOREACH: |
12032 | 0 | zend_compile_foreach(ast); |
12033 | 0 | break; |
12034 | 0 | case ZEND_AST_IF: |
12035 | 0 | zend_compile_if(ast); |
12036 | 0 | break; |
12037 | 0 | case ZEND_AST_SWITCH: |
12038 | 0 | zend_compile_switch(ast); |
12039 | 0 | break; |
12040 | 0 | case ZEND_AST_TRY: |
12041 | 0 | zend_compile_try(ast); |
12042 | 0 | break; |
12043 | 0 | case ZEND_AST_DECLARE: |
12044 | 0 | zend_compile_declare(ast); |
12045 | 0 | break; |
12046 | 0 | case ZEND_AST_FUNC_DECL: |
12047 | 0 | case ZEND_AST_METHOD: |
12048 | 0 | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED); |
12049 | 0 | break; |
12050 | 0 | case ZEND_AST_ENUM_CASE: |
12051 | 0 | zend_compile_enum_case(ast); |
12052 | 0 | break; |
12053 | 0 | case ZEND_AST_PROP_GROUP: |
12054 | 0 | zend_compile_prop_group(ast); |
12055 | 0 | break; |
12056 | 0 | case ZEND_AST_CLASS_CONST_GROUP: |
12057 | 0 | zend_compile_class_const_group(ast); |
12058 | 0 | break; |
12059 | 0 | case ZEND_AST_USE_TRAIT: |
12060 | 0 | zend_compile_use_trait(ast); |
12061 | 0 | break; |
12062 | 0 | case ZEND_AST_CLASS: |
12063 | 0 | zend_compile_class_decl(NULL, ast, false); |
12064 | 0 | break; |
12065 | 0 | case ZEND_AST_GROUP_USE: |
12066 | 0 | zend_compile_group_use(ast); |
12067 | 0 | break; |
12068 | 0 | case ZEND_AST_USE: |
12069 | 0 | zend_compile_use(ast); |
12070 | 0 | break; |
12071 | 0 | case ZEND_AST_CONST_DECL: |
12072 | 0 | zend_compile_const_decl(ast); |
12073 | 0 | break; |
12074 | 0 | case ZEND_AST_NAMESPACE: |
12075 | 0 | zend_compile_namespace(ast); |
12076 | 0 | break; |
12077 | 0 | case ZEND_AST_HALT_COMPILER: |
12078 | 0 | zend_compile_halt_compiler(ast); |
12079 | 0 | break; |
12080 | 0 | case ZEND_AST_THROW: |
12081 | 0 | zend_compile_expr(NULL, ast); |
12082 | 0 | break; |
12083 | 0 | case ZEND_AST_CAST_VOID: |
12084 | 0 | zend_compile_void_cast(NULL, ast); |
12085 | 0 | break; |
12086 | 0 | case ZEND_AST_ASSIGN: { |
12087 | 0 | znode result; |
12088 | 0 | zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R); |
12089 | 0 | zend_do_free(&result); |
12090 | 0 | return; |
12091 | 0 | } |
12092 | 0 | case ZEND_AST_ASSIGN_REF: |
12093 | 0 | zend_compile_assign_ref(NULL, ast, BP_VAR_R); |
12094 | 0 | return; |
12095 | 0 | default: |
12096 | 0 | { |
12097 | 0 | znode result; |
12098 | 0 | zend_compile_expr(&result, ast); |
12099 | 0 | zend_do_free(&result); |
12100 | 0 | } |
12101 | 0 | } |
12102 | | |
12103 | 0 | if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) { |
12104 | 0 | zend_emit_tick(); |
12105 | 0 | } |
12106 | 0 | } |
12107 | | /* }}} */ |
12108 | | |
12109 | | static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */ |
12110 | 0 | { |
12111 | | /* CG(zend_lineno) = ast->lineno; */ |
12112 | 0 | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12113 | |
|
12114 | 0 | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12115 | 0 | zend_compile_memoized_expr(result, ast, BP_VAR_R); |
12116 | 0 | return; |
12117 | 0 | } |
12118 | | |
12119 | 0 | switch (ast->kind) { |
12120 | 0 | case ZEND_AST_ZVAL: |
12121 | 0 | ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast)); |
12122 | 0 | result->op_type = IS_CONST; |
12123 | 0 | return; |
12124 | 0 | case ZEND_AST_ZNODE: |
12125 | 0 | *result = *zend_ast_get_znode(ast); |
12126 | 0 | return; |
12127 | 0 | case ZEND_AST_VAR: |
12128 | 0 | case ZEND_AST_DIM: |
12129 | 0 | case ZEND_AST_PROP: |
12130 | 0 | case ZEND_AST_NULLSAFE_PROP: |
12131 | 0 | case ZEND_AST_STATIC_PROP: |
12132 | 0 | case ZEND_AST_CALL: |
12133 | 0 | case ZEND_AST_METHOD_CALL: |
12134 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12135 | 0 | case ZEND_AST_STATIC_CALL: |
12136 | 0 | case ZEND_AST_PIPE: |
12137 | 0 | zend_compile_var(result, ast, BP_VAR_R, false); |
12138 | 0 | return; |
12139 | 0 | case ZEND_AST_ASSIGN: |
12140 | 0 | zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R); |
12141 | 0 | return; |
12142 | 0 | case ZEND_AST_ASSIGN_REF: |
12143 | 0 | zend_compile_assign_ref(result, ast, BP_VAR_R); |
12144 | 0 | return; |
12145 | 0 | case ZEND_AST_NEW: |
12146 | 0 | zend_compile_new(result, ast); |
12147 | 0 | return; |
12148 | 0 | case ZEND_AST_ASSIGN_OP: |
12149 | 0 | zend_compile_compound_assign(result, ast); |
12150 | 0 | return; |
12151 | 0 | case ZEND_AST_BINARY_OP: |
12152 | 0 | zend_compile_binary_op(result, ast); |
12153 | 0 | return; |
12154 | 0 | case ZEND_AST_GREATER: |
12155 | 0 | case ZEND_AST_GREATER_EQUAL: |
12156 | 0 | zend_compile_greater(result, ast); |
12157 | 0 | return; |
12158 | 0 | case ZEND_AST_UNARY_OP: |
12159 | 0 | zend_compile_unary_op(result, ast); |
12160 | 0 | return; |
12161 | 0 | case ZEND_AST_UNARY_PLUS: |
12162 | 0 | case ZEND_AST_UNARY_MINUS: |
12163 | 0 | zend_compile_unary_pm(result, ast); |
12164 | 0 | return; |
12165 | 0 | case ZEND_AST_AND: |
12166 | 0 | case ZEND_AST_OR: |
12167 | 0 | zend_compile_short_circuiting(result, ast); |
12168 | 0 | return; |
12169 | 0 | case ZEND_AST_POST_INC: |
12170 | 0 | case ZEND_AST_POST_DEC: |
12171 | 0 | zend_compile_post_incdec(result, ast); |
12172 | 0 | return; |
12173 | 0 | case ZEND_AST_PRE_INC: |
12174 | 0 | case ZEND_AST_PRE_DEC: |
12175 | 0 | zend_compile_pre_incdec(result, ast); |
12176 | 0 | return; |
12177 | 0 | case ZEND_AST_CAST: |
12178 | 0 | zend_compile_cast(result, ast); |
12179 | 0 | return; |
12180 | 0 | case ZEND_AST_CONDITIONAL: |
12181 | 0 | zend_compile_conditional(result, ast); |
12182 | 0 | return; |
12183 | 0 | case ZEND_AST_COALESCE: |
12184 | 0 | zend_compile_coalesce(result, ast); |
12185 | 0 | return; |
12186 | 0 | case ZEND_AST_ASSIGN_COALESCE: |
12187 | 0 | zend_compile_assign_coalesce(result, ast); |
12188 | 0 | return; |
12189 | 0 | case ZEND_AST_PRINT: |
12190 | 0 | zend_compile_print(result, ast); |
12191 | 0 | return; |
12192 | 0 | case ZEND_AST_YIELD: |
12193 | 0 | zend_compile_yield(result, ast); |
12194 | 0 | return; |
12195 | 0 | case ZEND_AST_YIELD_FROM: |
12196 | 0 | zend_compile_yield_from(result, ast); |
12197 | 0 | return; |
12198 | 0 | case ZEND_AST_INSTANCEOF: |
12199 | 0 | zend_compile_instanceof(result, ast); |
12200 | 0 | return; |
12201 | 0 | case ZEND_AST_INCLUDE_OR_EVAL: |
12202 | 0 | zend_compile_include_or_eval(result, ast); |
12203 | 0 | return; |
12204 | 0 | case ZEND_AST_ISSET: |
12205 | 0 | case ZEND_AST_EMPTY: |
12206 | 0 | zend_compile_isset_or_empty(result, ast); |
12207 | 0 | return; |
12208 | 0 | case ZEND_AST_SILENCE: |
12209 | 0 | zend_compile_silence(result, ast); |
12210 | 0 | return; |
12211 | 0 | case ZEND_AST_SHELL_EXEC: |
12212 | 0 | zend_compile_shell_exec(result, ast); |
12213 | 0 | return; |
12214 | 0 | case ZEND_AST_ARRAY: |
12215 | 0 | zend_compile_array(result, ast); |
12216 | 0 | return; |
12217 | 0 | case ZEND_AST_CONST: |
12218 | 0 | zend_compile_const(result, ast); |
12219 | 0 | return; |
12220 | 0 | case ZEND_AST_CLASS_CONST: |
12221 | 0 | zend_compile_class_const(result, ast); |
12222 | 0 | return; |
12223 | 0 | case ZEND_AST_CLASS_NAME: |
12224 | 0 | zend_compile_class_name(result, ast); |
12225 | 0 | return; |
12226 | 0 | case ZEND_AST_ENCAPS_LIST: |
12227 | 0 | zend_compile_encaps_list(result, ast); |
12228 | 0 | return; |
12229 | 0 | case ZEND_AST_MAGIC_CONST: |
12230 | 0 | zend_compile_magic_const(result, ast); |
12231 | 0 | return; |
12232 | 0 | case ZEND_AST_CLOSURE: |
12233 | 0 | case ZEND_AST_ARROW_FUNC: |
12234 | 0 | zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED); |
12235 | 0 | return; |
12236 | 0 | case ZEND_AST_THROW: |
12237 | 0 | zend_compile_throw(result, ast); |
12238 | 0 | return; |
12239 | 0 | case ZEND_AST_MATCH: |
12240 | 0 | zend_compile_match(result, ast); |
12241 | 0 | return; |
12242 | 0 | default: |
12243 | 0 | ZEND_ASSERT(0 /* not supported */); |
12244 | 0 | } |
12245 | 0 | } |
12246 | | /* }}} */ |
12247 | | |
12248 | | static void zend_compile_expr(znode *result, zend_ast *ast) |
12249 | 0 | { |
12250 | 0 | zend_check_stack_limit(); |
12251 | |
|
12252 | 0 | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12253 | 0 | zend_compile_expr_inner(result, ast); |
12254 | 0 | zend_short_circuiting_commit(checkpoint, result, ast); |
12255 | 0 | #if ZEND_DEBUG |
12256 | 0 | if (result) { |
12257 | | /* BP_VAR_R is not allowed to produce IS_VAR. */ |
12258 | 0 | ZEND_ASSERT(result->op_type != IS_VAR); |
12259 | 0 | } |
12260 | 0 | #endif |
12261 | 0 | } |
12262 | | |
12263 | | static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
12264 | 0 | { |
12265 | 0 | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12266 | |
|
12267 | 0 | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12268 | 0 | switch (ast->kind) { |
12269 | 0 | case ZEND_AST_CALL: |
12270 | 0 | case ZEND_AST_METHOD_CALL: |
12271 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12272 | 0 | case ZEND_AST_STATIC_CALL: |
12273 | 0 | zend_compile_memoized_expr(result, ast, BP_VAR_W); |
12274 | | /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */ |
12275 | 0 | return NULL; |
12276 | 0 | } |
12277 | 0 | } |
12278 | | |
12279 | 0 | switch (ast->kind) { |
12280 | 0 | case ZEND_AST_VAR: |
12281 | 0 | return zend_compile_simple_var(result, ast, type, false); |
12282 | 0 | case ZEND_AST_DIM: |
12283 | 0 | return zend_compile_dim(result, ast, type, by_ref); |
12284 | 0 | case ZEND_AST_PROP: |
12285 | 0 | case ZEND_AST_NULLSAFE_PROP: |
12286 | 0 | return zend_compile_prop(result, ast, type, by_ref); |
12287 | 0 | case ZEND_AST_STATIC_PROP: |
12288 | 0 | return zend_compile_static_prop(result, ast, type, by_ref, false); |
12289 | 0 | case ZEND_AST_CALL: |
12290 | 0 | zend_compile_call(result, ast, type); |
12291 | 0 | return NULL; |
12292 | 0 | case ZEND_AST_METHOD_CALL: |
12293 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12294 | 0 | zend_compile_method_call(result, ast, type); |
12295 | 0 | return NULL; |
12296 | 0 | case ZEND_AST_STATIC_CALL: |
12297 | 0 | zend_compile_static_call(result, ast, type); |
12298 | 0 | return NULL; |
12299 | 0 | case ZEND_AST_PIPE: |
12300 | 0 | zend_compile_pipe(result, ast, type); |
12301 | 0 | return NULL; |
12302 | 0 | case ZEND_AST_ZNODE: |
12303 | 0 | *result = *zend_ast_get_znode(ast); |
12304 | 0 | return NULL; |
12305 | 0 | case ZEND_AST_ASSIGN_REF: |
12306 | 0 | zend_compile_assign_ref(result, ast, type); |
12307 | 0 | return NULL; |
12308 | 0 | case ZEND_AST_ASSIGN: |
12309 | 0 | zend_compile_assign(result, ast, false, type); |
12310 | 0 | return NULL; |
12311 | 0 | default: |
12312 | 0 | if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) { |
12313 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
12314 | 0 | "Cannot use temporary expression in write context"); |
12315 | 0 | } |
12316 | | |
12317 | 0 | zend_compile_expr(result, ast); |
12318 | 0 | return NULL; |
12319 | 0 | } |
12320 | 0 | } |
12321 | | |
12322 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12323 | 0 | { |
12324 | 0 | zend_check_stack_limit(); |
12325 | |
|
12326 | 0 | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12327 | 0 | zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref); |
12328 | 0 | zend_short_circuiting_commit(checkpoint, result, ast); |
12329 | 0 | #if ZEND_DEBUG |
12330 | 0 | if (result |
12331 | 0 | && (type == BP_VAR_R || type == BP_VAR_IS) |
12332 | | /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */ |
12333 | 0 | && CG(memoize_mode) == ZEND_MEMOIZE_NONE |
12334 | 0 | ) { |
12335 | | /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */ |
12336 | 0 | ZEND_ASSERT(result->op_type != IS_VAR); |
12337 | 0 | } |
12338 | 0 | #endif |
12339 | 0 | return opcode; |
12340 | 0 | } |
12341 | | |
12342 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12343 | 0 | { |
12344 | 0 | zend_check_stack_limit(); |
12345 | |
|
12346 | 0 | switch (ast->kind) { |
12347 | 0 | case ZEND_AST_VAR: |
12348 | 0 | return zend_compile_simple_var(result, ast, type, true); |
12349 | 0 | case ZEND_AST_DIM: |
12350 | 0 | return zend_delayed_compile_dim(result, ast, type, by_ref); |
12351 | 0 | case ZEND_AST_PROP: |
12352 | 0 | case ZEND_AST_NULLSAFE_PROP: |
12353 | 0 | { |
12354 | 0 | zend_op *opline = zend_delayed_compile_prop(result, ast, type); |
12355 | 0 | if (by_ref) { |
12356 | 0 | opline->extended_value |= ZEND_FETCH_REF; |
12357 | 0 | } |
12358 | 0 | return opline; |
12359 | 0 | } |
12360 | 0 | case ZEND_AST_STATIC_PROP: |
12361 | 0 | return zend_compile_static_prop(result, ast, type, by_ref, true); |
12362 | 0 | default: |
12363 | 0 | return zend_compile_var(result, ast, type, false); |
12364 | 0 | } |
12365 | 0 | } |
12366 | | /* }}} */ |
12367 | | |
12368 | | bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1) |
12369 | 0 | { |
12370 | | /* NAN warns when casting */ |
12371 | 0 | if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) { |
12372 | 0 | return false; |
12373 | 0 | } |
12374 | 0 | switch (type) { |
12375 | 0 | case _IS_BOOL: |
12376 | 0 | ZVAL_BOOL(result, zend_is_true(op1)); |
12377 | 0 | return true; |
12378 | 0 | case IS_LONG: |
12379 | 0 | if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) { |
12380 | 0 | return false; |
12381 | 0 | } |
12382 | 0 | ZVAL_LONG(result, zval_get_long(op1)); |
12383 | 0 | return true; |
12384 | 0 | case IS_DOUBLE: |
12385 | 0 | ZVAL_DOUBLE(result, zval_get_double(op1)); |
12386 | 0 | return true; |
12387 | 0 | case IS_STRING: |
12388 | | /* Conversion from double to string takes into account run-time |
12389 | | 'precision' setting and cannot be evaluated at compile-time */ |
12390 | 0 | if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) { |
12391 | 0 | ZVAL_STR(result, zval_get_string(op1)); |
12392 | 0 | return true; |
12393 | 0 | } |
12394 | 0 | break; |
12395 | 0 | case IS_ARRAY: |
12396 | 0 | ZVAL_COPY(result, op1); |
12397 | 0 | convert_to_array(result); |
12398 | 0 | return true; |
12399 | 0 | } |
12400 | 0 | return false; |
12401 | 0 | } |
12402 | | |
12403 | | static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */ |
12404 | 0 | { |
12405 | 0 | zend_ast *ast = *ast_ptr; |
12406 | 0 | zval result; |
12407 | |
|
12408 | 0 | if (!ast) { |
12409 | 0 | return; |
12410 | 0 | } |
12411 | | |
12412 | 0 | zend_check_stack_limit(); |
12413 | |
|
12414 | 0 | switch (ast->kind) { |
12415 | 0 | case ZEND_AST_BINARY_OP: |
12416 | 0 | zend_eval_const_expr(&ast->child[0]); |
12417 | 0 | zend_eval_const_expr(&ast->child[1]); |
12418 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12419 | 0 | return; |
12420 | 0 | } |
12421 | | |
12422 | 0 | if (!zend_try_ct_eval_binary_op(&result, ast->attr, |
12423 | 0 | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])) |
12424 | 0 | ) { |
12425 | 0 | return; |
12426 | 0 | } |
12427 | 0 | break; |
12428 | 0 | case ZEND_AST_GREATER: |
12429 | 0 | case ZEND_AST_GREATER_EQUAL: |
12430 | 0 | zend_eval_const_expr(&ast->child[0]); |
12431 | 0 | zend_eval_const_expr(&ast->child[1]); |
12432 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12433 | 0 | return; |
12434 | 0 | } |
12435 | | |
12436 | 0 | zend_ct_eval_greater(&result, ast->kind, |
12437 | 0 | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])); |
12438 | 0 | break; |
12439 | 0 | case ZEND_AST_AND: |
12440 | 0 | case ZEND_AST_OR: |
12441 | 0 | { |
12442 | 0 | bool child0_is_true, child1_is_true; |
12443 | 0 | zend_eval_const_expr(&ast->child[0]); |
12444 | 0 | zend_eval_const_expr(&ast->child[1]); |
12445 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12446 | 0 | return; |
12447 | 0 | } |
12448 | | |
12449 | 0 | child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0])); |
12450 | 0 | if (child0_is_true == (ast->kind == ZEND_AST_OR)) { |
12451 | 0 | ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR); |
12452 | 0 | break; |
12453 | 0 | } |
12454 | | |
12455 | 0 | if (ast->child[1]->kind != ZEND_AST_ZVAL) { |
12456 | 0 | return; |
12457 | 0 | } |
12458 | | |
12459 | 0 | child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1])); |
12460 | 0 | if (ast->kind == ZEND_AST_OR) { |
12461 | 0 | ZVAL_BOOL(&result, child0_is_true || child1_is_true); |
12462 | 0 | } else { |
12463 | 0 | ZVAL_BOOL(&result, child0_is_true && child1_is_true); |
12464 | 0 | } |
12465 | 0 | break; |
12466 | 0 | } |
12467 | 0 | case ZEND_AST_UNARY_OP: |
12468 | 0 | zend_eval_const_expr(&ast->child[0]); |
12469 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12470 | 0 | return; |
12471 | 0 | } |
12472 | | |
12473 | 0 | if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
12474 | 0 | return; |
12475 | 0 | } |
12476 | 0 | break; |
12477 | 0 | case ZEND_AST_UNARY_PLUS: |
12478 | 0 | case ZEND_AST_UNARY_MINUS: |
12479 | 0 | zend_eval_const_expr(&ast->child[0]); |
12480 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12481 | 0 | return; |
12482 | 0 | } |
12483 | | |
12484 | 0 | if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) { |
12485 | 0 | return; |
12486 | 0 | } |
12487 | 0 | break; |
12488 | 0 | case ZEND_AST_COALESCE: |
12489 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12490 | 0 | if (ast->child[0]->kind == ZEND_AST_DIM) { |
12491 | 0 | ast->child[0]->attr |= ZEND_DIM_IS; |
12492 | 0 | } |
12493 | 0 | zend_eval_const_expr(&ast->child[0]); |
12494 | |
|
12495 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12496 | | /* ensure everything was compile-time evaluated at least once */ |
12497 | 0 | zend_eval_const_expr(&ast->child[1]); |
12498 | 0 | return; |
12499 | 0 | } |
12500 | | |
12501 | 0 | if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) { |
12502 | 0 | zend_eval_const_expr(&ast->child[1]); |
12503 | 0 | *ast_ptr = ast->child[1]; |
12504 | 0 | ast->child[1] = NULL; |
12505 | 0 | zend_ast_destroy(ast); |
12506 | 0 | } else { |
12507 | 0 | *ast_ptr = ast->child[0]; |
12508 | 0 | ast->child[0] = NULL; |
12509 | 0 | zend_ast_destroy(ast); |
12510 | 0 | } |
12511 | 0 | return; |
12512 | 0 | case ZEND_AST_CONDITIONAL: |
12513 | 0 | { |
12514 | 0 | zend_ast **child, *child_ast; |
12515 | 0 | zend_eval_const_expr(&ast->child[0]); |
12516 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12517 | | /* ensure everything was compile-time evaluated at least once */ |
12518 | 0 | if (ast->child[1]) { |
12519 | 0 | zend_eval_const_expr(&ast->child[1]); |
12520 | 0 | } |
12521 | 0 | zend_eval_const_expr(&ast->child[2]); |
12522 | 0 | return; |
12523 | 0 | } |
12524 | | |
12525 | 0 | child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))]; |
12526 | 0 | if (*child == NULL) { |
12527 | 0 | child--; |
12528 | 0 | } |
12529 | 0 | child_ast = *child; |
12530 | 0 | *child = NULL; |
12531 | 0 | zend_ast_destroy(ast); |
12532 | 0 | *ast_ptr = child_ast; |
12533 | 0 | zend_eval_const_expr(ast_ptr); |
12534 | 0 | return; |
12535 | 0 | } |
12536 | 0 | case ZEND_AST_DIM: |
12537 | 0 | { |
12538 | | /* constant expression should be always read context ... */ |
12539 | 0 | const zval *container, *dim; |
12540 | |
|
12541 | 0 | if (ast->child[1] == NULL) { |
12542 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
12543 | 0 | } |
12544 | | |
12545 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12546 | 0 | if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) { |
12547 | 0 | ast->child[0]->attr |= ZEND_DIM_IS; |
12548 | 0 | } |
12549 | |
|
12550 | 0 | zend_eval_const_expr(&ast->child[0]); |
12551 | 0 | zend_eval_const_expr(&ast->child[1]); |
12552 | 0 | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12553 | 0 | return; |
12554 | 0 | } |
12555 | | |
12556 | 0 | container = zend_ast_get_zval(ast->child[0]); |
12557 | 0 | dim = zend_ast_get_zval(ast->child[1]); |
12558 | |
|
12559 | 0 | if (Z_TYPE_P(container) == IS_ARRAY) { |
12560 | 0 | zval *el; |
12561 | 0 | if (Z_TYPE_P(dim) == IS_LONG) { |
12562 | 0 | el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim)); |
12563 | 0 | if (el) { |
12564 | 0 | ZVAL_COPY(&result, el); |
12565 | 0 | } else { |
12566 | 0 | return; |
12567 | 0 | } |
12568 | 0 | } else if (Z_TYPE_P(dim) == IS_STRING) { |
12569 | 0 | el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim)); |
12570 | 0 | if (el) { |
12571 | 0 | ZVAL_COPY(&result, el); |
12572 | 0 | } else { |
12573 | 0 | return; |
12574 | 0 | } |
12575 | 0 | } else { |
12576 | 0 | return; /* warning... handle at runtime */ |
12577 | 0 | } |
12578 | 0 | } else if (Z_TYPE_P(container) == IS_STRING) { |
12579 | 0 | zend_long offset; |
12580 | 0 | uint8_t c; |
12581 | 0 | if (Z_TYPE_P(dim) == IS_LONG) { |
12582 | 0 | offset = Z_LVAL_P(dim); |
12583 | 0 | } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) { |
12584 | 0 | return; |
12585 | 0 | } |
12586 | 0 | if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) { |
12587 | 0 | return; |
12588 | 0 | } |
12589 | 0 | c = (uint8_t) Z_STRVAL_P(container)[offset]; |
12590 | 0 | ZVAL_CHAR(&result, c); |
12591 | 0 | } else if (Z_TYPE_P(container) <= IS_FALSE) { |
12592 | 0 | return; /* warning... handle at runtime */ |
12593 | 0 | } else { |
12594 | 0 | return; |
12595 | 0 | } |
12596 | 0 | break; |
12597 | 0 | } |
12598 | 0 | case ZEND_AST_ARRAY: |
12599 | 0 | if (!zend_try_ct_eval_array(&result, ast)) { |
12600 | 0 | return; |
12601 | 0 | } |
12602 | 0 | break; |
12603 | 0 | case ZEND_AST_MAGIC_CONST: |
12604 | 0 | if (!zend_try_ct_eval_magic_const(&result, ast)) { |
12605 | 0 | return; |
12606 | 0 | } |
12607 | 0 | break; |
12608 | 0 | case ZEND_AST_CONST: |
12609 | 0 | { |
12610 | 0 | zend_ast *name_ast = ast->child[0]; |
12611 | 0 | bool is_fully_qualified; |
12612 | 0 | zend_string *resolved_name = zend_resolve_const_name( |
12613 | 0 | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
12614 | |
|
12615 | 0 | if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
12616 | 0 | zend_string_release_ex(resolved_name, 0); |
12617 | 0 | return; |
12618 | 0 | } |
12619 | | |
12620 | 0 | zend_string_release_ex(resolved_name, 0); |
12621 | 0 | break; |
12622 | 0 | } |
12623 | 0 | case ZEND_AST_CLASS_CONST: |
12624 | 0 | { |
12625 | 0 | zend_ast *class_ast; |
12626 | 0 | zend_ast *name_ast; |
12627 | 0 | zend_string *resolved_name; |
12628 | |
|
12629 | 0 | zend_eval_const_expr(&ast->child[0]); |
12630 | 0 | zend_eval_const_expr(&ast->child[1]); |
12631 | |
|
12632 | 0 | if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL |
12633 | 0 | || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) { |
12634 | 0 | return; |
12635 | 0 | } |
12636 | | |
12637 | 0 | class_ast = ast->child[0]; |
12638 | 0 | name_ast = ast->child[1]; |
12639 | |
|
12640 | 0 | if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) { |
12641 | 0 | return; |
12642 | 0 | } |
12643 | | |
12644 | 0 | resolved_name = zend_resolve_class_name_ast(class_ast); |
12645 | 0 | if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) { |
12646 | 0 | zend_string_release_ex(resolved_name, 0); |
12647 | 0 | return; |
12648 | 0 | } |
12649 | | |
12650 | 0 | zend_string_release_ex(resolved_name, 0); |
12651 | 0 | break; |
12652 | 0 | } |
12653 | 0 | case ZEND_AST_CLASS_NAME: |
12654 | 0 | { |
12655 | 0 | zend_ast *class_ast = ast->child[0]; |
12656 | 0 | if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) { |
12657 | 0 | return; |
12658 | 0 | } |
12659 | 0 | break; |
12660 | 0 | } |
12661 | | // TODO: We should probably use zend_ast_apply to recursively walk nodes without |
12662 | | // special handling. It is required that all nodes that are part of a const expr |
12663 | | // are visited. Probably we should be distinguishing evaluation of const expr and |
12664 | | // normal exprs here. |
12665 | 0 | case ZEND_AST_ARG_LIST: |
12666 | 0 | { |
12667 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
12668 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
12669 | 0 | zend_eval_const_expr(&list->child[i]); |
12670 | 0 | } |
12671 | 0 | return; |
12672 | 0 | } |
12673 | 0 | case ZEND_AST_NEW: |
12674 | 0 | zend_eval_const_expr(&ast->child[0]); |
12675 | 0 | zend_eval_const_expr(&ast->child[1]); |
12676 | 0 | return; |
12677 | 0 | case ZEND_AST_NAMED_ARG: |
12678 | 0 | zend_eval_const_expr(&ast->child[1]); |
12679 | 0 | return; |
12680 | 0 | case ZEND_AST_CONST_ENUM_INIT: |
12681 | 0 | zend_eval_const_expr(&ast->child[3]); |
12682 | 0 | return; |
12683 | 0 | case ZEND_AST_PROP: |
12684 | 0 | case ZEND_AST_NULLSAFE_PROP: |
12685 | 0 | zend_eval_const_expr(&ast->child[0]); |
12686 | 0 | zend_eval_const_expr(&ast->child[1]); |
12687 | 0 | return; |
12688 | 0 | case ZEND_AST_CAST: |
12689 | 0 | if (ast->attr == IS_NULL) { |
12690 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
12691 | 0 | } |
12692 | 0 | zend_eval_const_expr(&ast->child[0]); |
12693 | 0 | if (ast->child[0]->kind == ZEND_AST_ZVAL |
12694 | 0 | && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
12695 | 0 | break; |
12696 | 0 | } |
12697 | 0 | return; |
12698 | 0 | default: |
12699 | 0 | return; |
12700 | 0 | } |
12701 | | |
12702 | 0 | zend_ast_destroy(ast); |
12703 | 0 | *ast_ptr = zend_ast_create_zval(&result); |
12704 | 0 | } |
12705 | | /* }}} */ |