/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_portability.h" |
32 | | #include "zend_string.h" |
33 | | #include "zend_virtual_cwd.h" |
34 | | #include "zend_multibyte.h" |
35 | | #include "zend_language_scanner.h" |
36 | | #include "zend_inheritance.h" |
37 | | #include "zend_vm.h" |
38 | | #include "zend_enum.h" |
39 | | #include "zend_observer.h" |
40 | | #include "zend_call_stack.h" |
41 | | #include "zend_frameless_function.h" |
42 | | #include "zend_property_hooks.h" |
43 | | |
44 | 60.9M | #define SET_NODE(target, src) do { \ |
45 | 60.9M | target ## _type = (src)->op_type; \ |
46 | 60.9M | if ((src)->op_type == IS_CONST) { \ |
47 | 8.29M | target.constant = zend_add_literal(&(src)->u.constant); \ |
48 | 52.6M | } else { \ |
49 | 52.6M | target = (src)->u.op; \ |
50 | 52.6M | } \ |
51 | 60.9M | } while (0) |
52 | | |
53 | 44.6M | #define GET_NODE(target, src) do { \ |
54 | 44.6M | (target)->op_type = src ## _type; \ |
55 | 44.6M | if ((target)->op_type == IS_CONST) { \ |
56 | 523 | ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \ |
57 | 44.6M | } else { \ |
58 | 44.6M | (target)->u.op = src; \ |
59 | 44.6M | } \ |
60 | 44.6M | } while (0) |
61 | | |
62 | 105M | #define FC(member) (CG(file_context).member) |
63 | | |
64 | | typedef struct _zend_loop_var { |
65 | | uint8_t opcode; |
66 | | uint8_t var_type; |
67 | | uint32_t var_num; |
68 | | uint32_t try_catch_offset; |
69 | | } zend_loop_var; |
70 | | |
71 | 16.5M | static inline uint32_t zend_alloc_cache_slots(unsigned count) { |
72 | 16.5M | if (count == 0) { |
73 | | /* Even if no cache slots are desired, the VM handler may still want to acquire |
74 | | * CACHE_ADDR() unconditionally. Returning zero makes sure that the address |
75 | | * calculation is still legal and ubsan does not complain. */ |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | 16.5M | zend_op_array *op_array = CG(active_op_array); |
80 | 16.5M | uint32_t ret = op_array->cache_size; |
81 | 16.5M | op_array->cache_size += count * sizeof(void*); |
82 | 16.5M | return ret; |
83 | 16.5M | } |
84 | | |
85 | 15.9M | static inline uint32_t zend_alloc_cache_slot(void) { |
86 | 15.9M | return zend_alloc_cache_slots(1); |
87 | 15.9M | } |
88 | | |
89 | | ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type); |
90 | | ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position); |
91 | | |
92 | | #ifndef ZTS |
93 | | ZEND_API zend_compiler_globals compiler_globals; |
94 | | ZEND_API zend_executor_globals executor_globals; |
95 | | #endif |
96 | | |
97 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2); |
98 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast); |
99 | | static void zend_eval_const_expr(zend_ast **ast_ptr); |
100 | | |
101 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
102 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
103 | | static void zend_compile_expr(znode *result, zend_ast *ast); |
104 | | static void zend_compile_stmt(zend_ast *ast); |
105 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type); |
106 | | |
107 | | static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg); |
108 | | |
109 | | #ifdef ZEND_CHECK_STACK_LIMIT |
110 | | zend_never_inline static void zend_stack_limit_error(void) |
111 | 0 | { |
112 | 0 | size_t max_stack_size = 0; |
113 | 0 | if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) { |
114 | 0 | max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit)); |
115 | 0 | } |
116 | |
|
117 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
118 | 0 | "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression", |
119 | 0 | max_stack_size); |
120 | 0 | } |
121 | | |
122 | | static void zend_check_stack_limit(void) |
123 | 68.0M | { |
124 | 68.0M | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
125 | 0 | zend_stack_limit_error(); |
126 | 0 | } |
127 | 68.0M | } |
128 | | #else /* ZEND_CHECK_STACK_LIMIT */ |
129 | | static void zend_check_stack_limit(void) |
130 | | { |
131 | | } |
132 | | #endif /* ZEND_CHECK_STACK_LIMIT */ |
133 | | |
134 | | static void init_op(zend_op *op) |
135 | 89.8M | { |
136 | 89.8M | MAKE_NOP(op); |
137 | 89.8M | op->extended_value = 0; |
138 | 89.8M | op->lineno = CG(zend_lineno); |
139 | | #ifdef ZEND_VERIFY_TYPE_INFERENCE |
140 | | op->op1_use_type = 0; |
141 | | op->op2_use_type = 0; |
142 | | op->result_use_type = 0; |
143 | | op->op1_def_type = 0; |
144 | | op->op2_def_type = 0; |
145 | | op->result_def_type = 0; |
146 | | #endif |
147 | 89.8M | } |
148 | | |
149 | | static zend_always_inline uint32_t get_next_op_number(void) |
150 | 14.6M | { |
151 | 14.6M | return CG(active_op_array)->last; |
152 | 14.6M | } |
153 | | |
154 | | static zend_op *get_next_op(void) |
155 | 89.0M | { |
156 | 89.0M | zend_op_array *op_array = CG(active_op_array); |
157 | 89.0M | uint32_t next_op_num = op_array->last++; |
158 | 89.0M | zend_op *next_op; |
159 | | |
160 | 89.0M | if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) { |
161 | 719k | CG(context).opcodes_size *= 4; |
162 | 719k | op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op)); |
163 | 719k | } |
164 | | |
165 | 89.0M | next_op = &(op_array->opcodes[next_op_num]); |
166 | | |
167 | 89.0M | init_op(next_op); |
168 | | |
169 | 89.0M | return next_op; |
170 | 89.0M | } |
171 | | |
172 | | static zend_brk_cont_element *get_next_brk_cont_element(void) |
173 | 44.7k | { |
174 | 44.7k | CG(context).last_brk_cont++; |
175 | 44.7k | CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont); |
176 | 44.7k | return &CG(context).brk_cont_array[CG(context).last_brk_cont-1]; |
177 | 44.7k | } |
178 | | |
179 | | static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */ |
180 | 268k | { |
181 | 268k | zend_string *filename = CG(active_op_array)->filename; |
182 | 268k | zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32, |
183 | 268k | '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
184 | 268k | return zend_new_interned_string(result); |
185 | 268k | } |
186 | | /* }}} */ |
187 | | |
188 | | static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */ |
189 | 26.4M | { |
190 | 26.4M | const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
191 | 26.4M | if (ns_separator != NULL) { |
192 | 25.7M | *result = ns_separator + 1; |
193 | 25.7M | *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result; |
194 | 25.7M | return true; |
195 | 25.7M | } |
196 | | |
197 | 717k | return false; |
198 | 26.4M | } |
199 | | /* }}} */ |
200 | | |
201 | | struct reserved_class_name { |
202 | | const char *name; |
203 | | size_t len; |
204 | | }; |
205 | | static const struct reserved_class_name reserved_class_names[] = { |
206 | | {ZEND_STRL("bool")}, |
207 | | {ZEND_STRL("false")}, |
208 | | {ZEND_STRL("float")}, |
209 | | {ZEND_STRL("int")}, |
210 | | {ZEND_STRL("null")}, |
211 | | {ZEND_STRL("parent")}, |
212 | | {ZEND_STRL("self")}, |
213 | | {ZEND_STRL("static")}, |
214 | | {ZEND_STRL("string")}, |
215 | | {ZEND_STRL("true")}, |
216 | | {ZEND_STRL("void")}, |
217 | | {ZEND_STRL("never")}, |
218 | | {ZEND_STRL("iterable")}, |
219 | | {ZEND_STRL("object")}, |
220 | | {ZEND_STRL("mixed")}, |
221 | | /* These are not usable as class names because they're proper tokens, |
222 | | * but they are here for class aliases. */ |
223 | | {ZEND_STRL("array")}, |
224 | | {ZEND_STRL("callable")}, |
225 | | {NULL, 0} |
226 | | }; |
227 | | |
228 | | static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */ |
229 | 957k | { |
230 | 957k | const struct reserved_class_name *reserved = reserved_class_names; |
231 | | |
232 | 957k | const char *uqname = ZSTR_VAL(name); |
233 | 957k | size_t uqname_len = ZSTR_LEN(name); |
234 | 957k | zend_get_unqualified_name(name, &uqname, &uqname_len); |
235 | | |
236 | 17.2M | for (; reserved->name; ++reserved) { |
237 | 16.2M | if (uqname_len == reserved->len |
238 | 245k | && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0 |
239 | 16.2M | ) { |
240 | 99 | return true; |
241 | 99 | } |
242 | 16.2M | } |
243 | | |
244 | 957k | return false; |
245 | 957k | } |
246 | | /* }}} */ |
247 | | |
248 | | void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */ |
249 | 955k | { |
250 | 955k | if (zend_is_reserved_class_name(name)) { |
251 | 77 | zend_error_noreturn(E_COMPILE_ERROR, |
252 | 77 | "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type); |
253 | 77 | } |
254 | 955k | if (zend_string_equals_literal(name, "_")) { |
255 | 1.39k | zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type); |
256 | 1.39k | } |
257 | 955k | } |
258 | | /* }}} */ |
259 | | |
260 | | typedef struct _builtin_type_info { |
261 | | const char* name; |
262 | | const size_t name_len; |
263 | | const uint8_t type; |
264 | | } builtin_type_info; |
265 | | |
266 | | static const builtin_type_info builtin_types[] = { |
267 | | {ZEND_STRL("null"), IS_NULL}, |
268 | | {ZEND_STRL("true"), IS_TRUE}, |
269 | | {ZEND_STRL("false"), IS_FALSE}, |
270 | | {ZEND_STRL("int"), IS_LONG}, |
271 | | {ZEND_STRL("float"), IS_DOUBLE}, |
272 | | {ZEND_STRL("string"), IS_STRING}, |
273 | | {ZEND_STRL("bool"), _IS_BOOL}, |
274 | | {ZEND_STRL("void"), IS_VOID}, |
275 | | {ZEND_STRL("never"), IS_NEVER}, |
276 | | {ZEND_STRL("iterable"), IS_ITERABLE}, |
277 | | {ZEND_STRL("object"), IS_OBJECT}, |
278 | | {ZEND_STRL("mixed"), IS_MIXED}, |
279 | | {NULL, 0, IS_UNDEF} |
280 | | }; |
281 | | |
282 | | typedef struct { |
283 | | const char *name; |
284 | | size_t name_len; |
285 | | const char *correct_name; |
286 | | } confusable_type_info; |
287 | | |
288 | | static const confusable_type_info confusable_types[] = { |
289 | | {ZEND_STRL("boolean"), "bool"}, |
290 | | {ZEND_STRL("integer"), "int"}, |
291 | | {ZEND_STRL("double"), "float"}, |
292 | | {ZEND_STRL("resource"), NULL}, |
293 | | {NULL, 0, NULL}, |
294 | | }; |
295 | | |
296 | | static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */ |
297 | 972k | { |
298 | 972k | const builtin_type_info *info = &builtin_types[0]; |
299 | | |
300 | 11.5M | for (; info->name; ++info) { |
301 | 10.9M | if (ZSTR_LEN(name) == info->name_len |
302 | 411k | && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0 |
303 | 10.9M | ) { |
304 | 315k | return info->type; |
305 | 315k | } |
306 | 10.9M | } |
307 | | |
308 | 657k | return 0; |
309 | 972k | } |
310 | | /* }}} */ |
311 | | |
312 | | static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */ |
313 | 655k | { |
314 | 655k | const confusable_type_info *info = confusable_types; |
315 | | |
316 | | /* Intentionally using case-sensitive comparison here, because "integer" is likely intended |
317 | | * as a scalar type, while "Integer" is likely a class type. */ |
318 | 3.18M | for (; info->name; ++info) { |
319 | 2.56M | if (zend_string_equals_cstr(name, info->name, info->name_len)) { |
320 | 42.4k | *correct_name = info->correct_name; |
321 | 42.4k | return 1; |
322 | 42.4k | } |
323 | 2.56M | } |
324 | | |
325 | 613k | return 0; |
326 | 655k | } |
327 | | /* }}} */ |
328 | | |
329 | 42.4k | static bool zend_is_not_imported(zend_string *name) { |
330 | | /* Assuming "name" is unqualified here. */ |
331 | 42.4k | return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL; |
332 | 42.4k | } |
333 | | |
334 | | void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */ |
335 | 2.25M | { |
336 | 2.25M | *prev_context = CG(context); |
337 | 2.25M | CG(context).prev = CG(context).op_array ? prev_context : NULL; |
338 | 2.25M | CG(context).op_array = op_array; |
339 | 2.25M | CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE; |
340 | 2.25M | CG(context).vars_size = 0; |
341 | 2.25M | CG(context).literals_size = 0; |
342 | 2.25M | CG(context).fast_call_var = -1; |
343 | 2.25M | CG(context).try_catch_offset = -1; |
344 | 2.25M | CG(context).current_brk_cont = -1; |
345 | 2.25M | CG(context).last_brk_cont = 0; |
346 | 2.25M | CG(context).has_assigned_to_http_response_header = false; |
347 | 2.25M | CG(context).brk_cont_array = NULL; |
348 | 2.25M | CG(context).labels = NULL; |
349 | 2.25M | CG(context).in_jmp_frameless_branch = false; |
350 | 2.25M | CG(context).active_property_info_name = NULL; |
351 | 2.25M | CG(context).active_property_hook_kind = (zend_property_hook_kind)-1; |
352 | 2.25M | } |
353 | | /* }}} */ |
354 | | |
355 | | void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */ |
356 | 2.24M | { |
357 | 2.24M | if (CG(context).brk_cont_array) { |
358 | 28.7k | efree(CG(context).brk_cont_array); |
359 | 28.7k | CG(context).brk_cont_array = NULL; |
360 | 28.7k | } |
361 | 2.24M | if (CG(context).labels) { |
362 | 2.36k | zend_hash_destroy(CG(context).labels); |
363 | 2.36k | FREE_HASHTABLE(CG(context).labels); |
364 | 2.36k | CG(context).labels = NULL; |
365 | 2.36k | } |
366 | 2.24M | CG(context) = *prev_context; |
367 | 2.24M | } |
368 | | /* }}} */ |
369 | | |
370 | | static void zend_reset_import_tables(void) /* {{{ */ |
371 | 94.1k | { |
372 | 94.1k | if (FC(imports)) { |
373 | 598 | zend_hash_destroy(FC(imports)); |
374 | 598 | efree(FC(imports)); |
375 | 598 | FC(imports) = NULL; |
376 | 598 | } |
377 | | |
378 | 94.1k | if (FC(imports_function)) { |
379 | 263 | zend_hash_destroy(FC(imports_function)); |
380 | 263 | efree(FC(imports_function)); |
381 | 263 | FC(imports_function) = NULL; |
382 | 263 | } |
383 | | |
384 | 94.1k | if (FC(imports_const)) { |
385 | 249 | zend_hash_destroy(FC(imports_const)); |
386 | 249 | efree(FC(imports_const)); |
387 | 249 | FC(imports_const) = NULL; |
388 | 249 | } |
389 | | |
390 | 94.1k | zend_hash_clean(&FC(seen_symbols)); |
391 | 94.1k | } |
392 | | /* }}} */ |
393 | | |
394 | 89.8k | static void zend_end_namespace(void) /* {{{ */ { |
395 | 89.8k | FC(in_namespace) = 0; |
396 | 89.8k | zend_reset_import_tables(); |
397 | 89.8k | if (FC(current_namespace)) { |
398 | 2.91k | zend_string_release_ex(FC(current_namespace), 0); |
399 | 2.91k | FC(current_namespace) = NULL; |
400 | 2.91k | } |
401 | 89.8k | } |
402 | | /* }}} */ |
403 | | |
404 | | void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */ |
405 | 93.8k | { |
406 | 93.8k | *prev_context = CG(file_context); |
407 | 93.8k | FC(imports) = NULL; |
408 | 93.8k | FC(imports_function) = NULL; |
409 | 93.8k | FC(imports_const) = NULL; |
410 | 93.8k | FC(current_namespace) = NULL; |
411 | 93.8k | FC(in_namespace) = 0; |
412 | 93.8k | FC(has_bracketed_namespaces) = 0; |
413 | 93.8k | FC(declarables).ticks = 0; |
414 | 93.8k | zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0); |
415 | 93.8k | } |
416 | | /* }}} */ |
417 | | |
418 | | void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */ |
419 | 88.3k | { |
420 | 88.3k | zend_end_namespace(); |
421 | 88.3k | zend_hash_destroy(&FC(seen_symbols)); |
422 | 88.3k | CG(file_context) = *prev_context; |
423 | 88.3k | } |
424 | | /* }}} */ |
425 | | |
426 | | void zend_init_compiler_data_structures(void) /* {{{ */ |
427 | 306k | { |
428 | 306k | zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var)); |
429 | 306k | zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op)); |
430 | 306k | zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t)); |
431 | 306k | CG(active_class_entry) = NULL; |
432 | 306k | CG(in_compilation) = 0; |
433 | 306k | CG(skip_shebang) = 0; |
434 | | |
435 | 306k | CG(encoding_declared) = 0; |
436 | 306k | CG(memoized_exprs) = NULL; |
437 | 306k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
438 | 306k | } |
439 | | /* }}} */ |
440 | | |
441 | 2.31M | static void zend_register_seen_symbol(zend_string *name, uint32_t kind) { |
442 | 2.31M | zval *zv = zend_hash_find(&FC(seen_symbols), name); |
443 | 2.31M | if (zv) { |
444 | 2.22M | Z_LVAL_P(zv) |= kind; |
445 | 2.22M | } else { |
446 | 85.6k | zval tmp; |
447 | 85.6k | ZVAL_LONG(&tmp, kind); |
448 | 85.6k | zend_hash_add_new(&FC(seen_symbols), name, &tmp); |
449 | 85.6k | } |
450 | 2.31M | } |
451 | | |
452 | 2.04k | static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) { |
453 | 2.04k | const zval *zv = zend_hash_find(&FC(seen_symbols), name); |
454 | 2.04k | return zv && (Z_LVAL_P(zv) & kind) != 0; |
455 | 2.04k | } |
456 | | |
457 | | void init_compiler(void) /* {{{ */ |
458 | 300k | { |
459 | 300k | CG(arena) = zend_arena_create(64 * 1024); |
460 | 300k | CG(active_op_array) = NULL; |
461 | 300k | memset(&CG(context), 0, sizeof(CG(context))); |
462 | 300k | zend_init_compiler_data_structures(); |
463 | 300k | zend_init_rsrc_list(); |
464 | 300k | zend_stream_init(); |
465 | 300k | CG(unclean_shutdown) = 0; |
466 | | |
467 | 300k | CG(delayed_variance_obligations) = NULL; |
468 | 300k | CG(delayed_autoloads) = NULL; |
469 | 300k | CG(unlinked_uses) = NULL; |
470 | 300k | CG(current_linking_class) = NULL; |
471 | 300k | } |
472 | | /* }}} */ |
473 | | |
474 | | void shutdown_compiler(void) /* {{{ */ |
475 | 306k | { |
476 | | /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */ |
477 | 306k | zend_restore_compiled_filename(NULL); |
478 | | |
479 | 306k | zend_stack_destroy(&CG(loop_var_stack)); |
480 | 306k | zend_stack_destroy(&CG(delayed_oplines_stack)); |
481 | 306k | zend_stack_destroy(&CG(short_circuiting_opnums)); |
482 | | |
483 | 306k | if (CG(delayed_variance_obligations)) { |
484 | 295 | zend_hash_destroy(CG(delayed_variance_obligations)); |
485 | 295 | FREE_HASHTABLE(CG(delayed_variance_obligations)); |
486 | 295 | CG(delayed_variance_obligations) = NULL; |
487 | 295 | } |
488 | 306k | if (CG(delayed_autoloads)) { |
489 | 298 | zend_hash_destroy(CG(delayed_autoloads)); |
490 | 298 | FREE_HASHTABLE(CG(delayed_autoloads)); |
491 | 298 | CG(delayed_autoloads) = NULL; |
492 | 298 | } |
493 | 306k | if (CG(unlinked_uses)) { |
494 | 230 | zend_hash_destroy(CG(unlinked_uses)); |
495 | 230 | FREE_HASHTABLE(CG(unlinked_uses)); |
496 | 230 | CG(unlinked_uses) = NULL; |
497 | 230 | } |
498 | 306k | CG(current_linking_class) = NULL; |
499 | 306k | } |
500 | | /* }}} */ |
501 | | |
502 | | ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */ |
503 | 151k | { |
504 | 151k | CG(compiled_filename) = zend_string_copy(new_compiled_filename); |
505 | 151k | return new_compiled_filename; |
506 | 151k | } |
507 | | /* }}} */ |
508 | | |
509 | | ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */ |
510 | 602k | { |
511 | 602k | if (CG(compiled_filename)) { |
512 | 151k | zend_string_release(CG(compiled_filename)); |
513 | 151k | CG(compiled_filename) = NULL; |
514 | 151k | } |
515 | 602k | CG(compiled_filename) = original_compiled_filename; |
516 | 602k | } |
517 | | /* }}} */ |
518 | | |
519 | | ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */ |
520 | 3.19M | { |
521 | 3.19M | return CG(compiled_filename); |
522 | 3.19M | } |
523 | | /* }}} */ |
524 | | |
525 | | ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */ |
526 | 356k | { |
527 | 356k | return CG(zend_lineno); |
528 | 356k | } |
529 | | /* }}} */ |
530 | | |
531 | | ZEND_API bool zend_is_compiling(void) /* {{{ */ |
532 | 3.50M | { |
533 | 3.50M | return CG(in_compilation); |
534 | 3.50M | } |
535 | | /* }}} */ |
536 | | |
537 | | static bool zend_is_constructor(const zend_string *name) /* {{{ */ |
538 | 28.2k | { |
539 | 28.2k | return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME); |
540 | 28.2k | } |
541 | | /* }}} */ |
542 | | |
543 | | static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */ |
544 | 44.9M | { |
545 | 44.9M | return (uint32_t)CG(active_op_array)->T++; |
546 | 44.9M | } |
547 | | /* }}} */ |
548 | | |
549 | 3.13M | static uint32_t lookup_cv(zend_string *name) /* {{{ */{ |
550 | 3.13M | zend_op_array *op_array = CG(active_op_array); |
551 | 3.13M | int i = 0; |
552 | 3.13M | zend_ulong hash_value = zend_string_hash_val(name); |
553 | | |
554 | 7.73M | while (i < op_array->last_var) { |
555 | 5.37M | if (ZSTR_H(op_array->vars[i]) == hash_value |
556 | 779k | && zend_string_equals(op_array->vars[i], name)) { |
557 | 778k | return EX_NUM_TO_VAR(i); |
558 | 778k | } |
559 | 4.59M | i++; |
560 | 4.59M | } |
561 | 2.35M | i = op_array->last_var; |
562 | 2.35M | op_array->last_var++; |
563 | 2.35M | if (op_array->last_var > CG(context).vars_size) { |
564 | 1.77M | CG(context).vars_size += 16; /* FIXME */ |
565 | 1.77M | op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*)); |
566 | 1.77M | } |
567 | | |
568 | 2.35M | op_array->vars[i] = zend_string_copy(name); |
569 | 2.35M | return EX_NUM_TO_VAR(i); |
570 | 3.13M | } |
571 | | /* }}} */ |
572 | | |
573 | | zend_string *zval_make_interned_string(zval *zv) |
574 | 54.0M | { |
575 | 54.0M | ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING); |
576 | 54.0M | Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv)); |
577 | 54.0M | if (ZSTR_IS_INTERNED(Z_STR_P(zv))) { |
578 | 7.02M | Z_TYPE_FLAGS_P(zv) = 0; |
579 | 7.02M | } |
580 | 54.0M | return Z_STR_P(zv); |
581 | 54.0M | } |
582 | | |
583 | | /* Common part of zend_add_literal and zend_append_individual_literal */ |
584 | | static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */ |
585 | 54.1M | { |
586 | 54.1M | zval *lit = CT_CONSTANT_EX(op_array, literal_position); |
587 | 54.1M | if (Z_TYPE_P(zv) == IS_STRING) { |
588 | 50.9M | zval_make_interned_string(zv); |
589 | 50.9M | } |
590 | 54.1M | ZVAL_COPY_VALUE(lit, zv); |
591 | 54.1M | Z_EXTRA_P(lit) = 0; |
592 | 54.1M | } |
593 | | /* }}} */ |
594 | | |
595 | | /* Is used while compiling a function, using the context to keep track |
596 | | of an approximate size to avoid to relocate to often. |
597 | | Literals are truncated to actual size in the second compiler pass (pass_two()). */ |
598 | | static int zend_add_literal(zval *zv) /* {{{ */ |
599 | 54.1M | { |
600 | 54.1M | zend_op_array *op_array = CG(active_op_array); |
601 | 54.1M | uint32_t i = op_array->last_literal; |
602 | 54.1M | op_array->last_literal++; |
603 | 54.1M | if (i >= CG(context).literals_size) { |
604 | 8.85M | while (i >= CG(context).literals_size) { |
605 | 4.42M | CG(context).literals_size += 16; /* FIXME */ |
606 | 4.42M | } |
607 | 4.42M | op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval)); |
608 | 4.42M | } |
609 | 54.1M | zend_insert_literal(op_array, zv, i); |
610 | 54.1M | return i; |
611 | 54.1M | } |
612 | | /* }}} */ |
613 | | |
614 | | static inline int zend_add_literal_string(zend_string **str) /* {{{ */ |
615 | 45.7M | { |
616 | 45.7M | int ret; |
617 | 45.7M | zval zv; |
618 | 45.7M | ZVAL_STR(&zv, *str); |
619 | 45.7M | ret = zend_add_literal(&zv); |
620 | 45.7M | *str = Z_STR(zv); |
621 | 45.7M | return ret; |
622 | 45.7M | } |
623 | | /* }}} */ |
624 | | |
625 | | static int zend_add_func_name_literal(zend_string *name) /* {{{ */ |
626 | 311k | { |
627 | | /* Original name */ |
628 | 311k | int ret = zend_add_literal_string(&name); |
629 | | |
630 | | /* Lowercased name */ |
631 | 311k | zend_string *lc_name = zend_string_tolower(name); |
632 | 311k | zend_add_literal_string(&lc_name); |
633 | | |
634 | 311k | return ret; |
635 | 311k | } |
636 | | /* }}} */ |
637 | | |
638 | | static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */ |
639 | 4.12M | { |
640 | 4.12M | const char *unqualified_name; |
641 | 4.12M | size_t unqualified_name_len; |
642 | | |
643 | | /* Original name */ |
644 | 4.12M | int ret = zend_add_literal_string(&name); |
645 | | |
646 | | /* Lowercased name */ |
647 | 4.12M | zend_string *lc_name = zend_string_tolower(name); |
648 | 4.12M | zend_add_literal_string(&lc_name); |
649 | | |
650 | | /* Lowercased unqualified name */ |
651 | 4.12M | if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) { |
652 | 4.12M | lc_name = zend_string_alloc(unqualified_name_len, 0); |
653 | 4.12M | zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len); |
654 | 4.12M | zend_add_literal_string(&lc_name); |
655 | 4.12M | } |
656 | | |
657 | 4.12M | return ret; |
658 | 4.12M | } |
659 | | /* }}} */ |
660 | | |
661 | | static int zend_add_class_name_literal(zend_string *name) /* {{{ */ |
662 | 163k | { |
663 | | /* Original name */ |
664 | 163k | int ret = zend_add_literal_string(&name); |
665 | | |
666 | | /* Lowercased name */ |
667 | 163k | zend_string *lc_name = zend_string_tolower(name); |
668 | 163k | zend_add_literal_string(&lc_name); |
669 | | |
670 | 163k | return ret; |
671 | 163k | } |
672 | | /* }}} */ |
673 | | |
674 | | static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */ |
675 | 10.6M | { |
676 | 10.6M | zend_string *tmp_name; |
677 | | |
678 | 10.6M | int ret = zend_add_literal_string(&name); |
679 | | |
680 | 10.6M | const char *after_ns; |
681 | 10.6M | size_t after_ns_len; |
682 | 10.6M | if (zend_get_unqualified_name(name, &after_ns, &after_ns_len)) { |
683 | 10.4M | size_t ns_len = after_ns - ZSTR_VAL(name) - 1; |
684 | | |
685 | | /* lowercased namespace name & original constant name */ |
686 | 10.4M | tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0); |
687 | 10.4M | zend_str_tolower(ZSTR_VAL(tmp_name), ns_len); |
688 | 10.4M | zend_add_literal_string(&tmp_name); |
689 | | |
690 | 10.4M | if (!unqualified) { |
691 | 2.25k | return ret; |
692 | 2.25k | } |
693 | 10.4M | } else { |
694 | 144k | after_ns = ZSTR_VAL(name); |
695 | 144k | after_ns_len = ZSTR_LEN(name); |
696 | 144k | } |
697 | | |
698 | | /* original unqualified constant name */ |
699 | 10.6M | tmp_name = zend_string_init(after_ns, after_ns_len, 0); |
700 | 10.6M | zend_add_literal_string(&tmp_name); |
701 | | |
702 | 10.6M | return ret; |
703 | 10.6M | } |
704 | | /* }}} */ |
705 | | |
706 | 80.6k | #define LITERAL_STR(op, str) do { \ |
707 | 80.6k | zval _c; \ |
708 | 80.6k | ZVAL_STR(&_c, str); \ |
709 | 80.6k | op.constant = zend_add_literal(&_c); \ |
710 | 80.6k | } while (0) |
711 | | |
712 | | void zend_stop_lexing(void) |
713 | 117 | { |
714 | 117 | if (LANG_SCNG(on_event)) { |
715 | 0 | LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context)); |
716 | 0 | } |
717 | | |
718 | 117 | LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit); |
719 | 117 | } |
720 | | |
721 | | static inline void zend_begin_loop( |
722 | | uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */ |
723 | 44.7k | { |
724 | 44.7k | zend_brk_cont_element *brk_cont_element; |
725 | 44.7k | int parent = CG(context).current_brk_cont; |
726 | 44.7k | zend_loop_var info = {0}; |
727 | | |
728 | 44.7k | CG(context).current_brk_cont = CG(context).last_brk_cont; |
729 | 44.7k | brk_cont_element = get_next_brk_cont_element(); |
730 | 44.7k | brk_cont_element->parent = parent; |
731 | 44.7k | brk_cont_element->is_switch = is_switch; |
732 | | |
733 | 44.7k | if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) { |
734 | 20.6k | uint32_t start = get_next_op_number(); |
735 | | |
736 | 20.6k | info.opcode = free_opcode; |
737 | 20.6k | info.var_type = loop_var->op_type; |
738 | 20.6k | info.var_num = loop_var->u.op.var; |
739 | 20.6k | brk_cont_element->start = start; |
740 | 24.1k | } else { |
741 | 24.1k | info.opcode = ZEND_NOP; |
742 | | /* The start field is used to free temporary variables in case of exceptions. |
743 | | * We won't try to free something of we don't have loop variable. */ |
744 | 24.1k | brk_cont_element->start = -1; |
745 | 24.1k | } |
746 | | |
747 | 44.7k | zend_stack_push(&CG(loop_var_stack), &info); |
748 | 44.7k | } |
749 | | /* }}} */ |
750 | | |
751 | | static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */ |
752 | 44.5k | { |
753 | 44.5k | uint32_t end = get_next_op_number(); |
754 | 44.5k | zend_brk_cont_element *brk_cont_element |
755 | 44.5k | = &CG(context).brk_cont_array[CG(context).current_brk_cont]; |
756 | 44.5k | brk_cont_element->cont = cont_addr; |
757 | 44.5k | brk_cont_element->brk = end; |
758 | 44.5k | CG(context).current_brk_cont = brk_cont_element->parent; |
759 | | |
760 | 44.5k | zend_stack_del_top(&CG(loop_var_stack)); |
761 | 44.5k | } |
762 | | /* }}} */ |
763 | | |
764 | | bool zend_op_may_elide_result(uint8_t opcode) |
765 | 4.44M | { |
766 | 4.44M | switch (opcode) { |
767 | 138k | case ZEND_ASSIGN: |
768 | 150k | case ZEND_ASSIGN_DIM: |
769 | 164k | case ZEND_ASSIGN_OBJ: |
770 | 165k | case ZEND_ASSIGN_STATIC_PROP: |
771 | 197k | case ZEND_ASSIGN_OP: |
772 | 200k | case ZEND_ASSIGN_DIM_OP: |
773 | 201k | case ZEND_ASSIGN_OBJ_OP: |
774 | 201k | case ZEND_ASSIGN_STATIC_PROP_OP: |
775 | 201k | case ZEND_PRE_INC_STATIC_PROP: |
776 | 201k | case ZEND_PRE_DEC_STATIC_PROP: |
777 | 202k | case ZEND_PRE_INC_OBJ: |
778 | 202k | case ZEND_PRE_DEC_OBJ: |
779 | 203k | case ZEND_PRE_INC: |
780 | 203k | case ZEND_PRE_DEC: |
781 | 331k | case ZEND_DO_FCALL: |
782 | 340k | case ZEND_DO_ICALL: |
783 | 348k | case ZEND_DO_UCALL: |
784 | 1.51M | case ZEND_DO_FCALL_BY_NAME: |
785 | 1.51M | case ZEND_YIELD: |
786 | 1.51M | case ZEND_YIELD_FROM: |
787 | 1.51M | case ZEND_INCLUDE_OR_EVAL: |
788 | 1.51M | return true; |
789 | 2.92M | default: |
790 | 2.92M | return false; |
791 | 4.44M | } |
792 | 4.44M | } |
793 | | |
794 | | static void zend_do_free(znode *op1) /* {{{ */ |
795 | 5.11M | { |
796 | 5.11M | if (op1->op_type == IS_TMP_VAR) { |
797 | 4.64M | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
798 | | |
799 | 4.85M | while (opline->opcode == ZEND_END_SILENCE || |
800 | 4.83M | opline->opcode == ZEND_OP_DATA) { |
801 | 207k | opline--; |
802 | 207k | } |
803 | | |
804 | 4.64M | if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) { |
805 | 4.42M | switch (opline->opcode) { |
806 | 1.68k | case ZEND_BOOL: |
807 | 6.63k | case ZEND_BOOL_NOT: |
808 | | /* boolean results don't have to be freed */ |
809 | 6.63k | return; |
810 | 97 | case ZEND_POST_INC_STATIC_PROP: |
811 | 121 | case ZEND_POST_DEC_STATIC_PROP: |
812 | 768 | case ZEND_POST_INC_OBJ: |
813 | 894 | case ZEND_POST_DEC_OBJ: |
814 | 8.10k | case ZEND_POST_INC: |
815 | 8.90k | case ZEND_POST_DEC: |
816 | | /* convert $i++ to ++$i */ |
817 | 8.90k | opline->opcode -= 2; |
818 | 8.90k | SET_UNUSED(opline->result); |
819 | 8.90k | return; |
820 | 4.40M | default: |
821 | 4.40M | if (zend_op_may_elide_result(opline->opcode)) { |
822 | 1.51M | SET_UNUSED(opline->result); |
823 | 1.51M | return; |
824 | 1.51M | } |
825 | 2.89M | break; |
826 | 4.42M | } |
827 | 4.42M | } |
828 | | |
829 | 3.11M | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
830 | 3.11M | } else if (op1->op_type == IS_VAR) { |
831 | 231k | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
832 | 231k | while (opline->opcode == ZEND_END_SILENCE || |
833 | 231k | opline->opcode == ZEND_EXT_FCALL_END || |
834 | 231k | opline->opcode == ZEND_OP_DATA) { |
835 | 160 | opline--; |
836 | 160 | } |
837 | 231k | if (opline->result_type == IS_VAR |
838 | 229k | && opline->result.var == op1->u.op.var) { |
839 | 229k | if (opline->opcode == ZEND_FETCH_THIS) { |
840 | 0 | opline->opcode = ZEND_NOP; |
841 | 0 | } |
842 | 229k | if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) { |
843 | 229k | SET_UNUSED(opline->result); |
844 | 229k | } else { |
845 | | /* Frameless calls usually use the return value, so always emit a free. This should be |
846 | | * faster than checking RETURN_VALUE_USED inside the handler. */ |
847 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
848 | 0 | } |
849 | 229k | } else { |
850 | 10.2k | while (opline >= CG(active_op_array)->opcodes) { |
851 | 10.2k | if ((opline->opcode == ZEND_FETCH_LIST_R || |
852 | 10.1k | opline->opcode == ZEND_FETCH_LIST_W || |
853 | 7.01k | opline->opcode == ZEND_EXT_STMT) && |
854 | 3.27k | opline->op1_type == IS_VAR && |
855 | 3.27k | opline->op1.var == op1->u.op.var) { |
856 | 1.85k | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
857 | 1.85k | return; |
858 | 1.85k | } |
859 | 8.43k | if (opline->result_type == IS_VAR |
860 | 5.09k | && opline->result.var == op1->u.op.var) { |
861 | 0 | if (opline->opcode == ZEND_NEW) { |
862 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
863 | 0 | } |
864 | 0 | break; |
865 | 0 | } |
866 | 8.43k | opline--; |
867 | 8.43k | } |
868 | 1.85k | } |
869 | 236k | } else if (op1->op_type == IS_CONST) { |
870 | | /* Destroy value without using GC: When opcache moves arrays into SHM it will |
871 | | * free the zend_array structure, so references to it from outside the op array |
872 | | * become invalid. GC would cause such a reference in the root buffer. */ |
873 | 225k | zval_ptr_dtor_nogc(&op1->u.constant); |
874 | 225k | } |
875 | 5.11M | } |
876 | | /* }}} */ |
877 | | |
878 | | |
879 | | static const char *zend_modifier_token_to_string(uint32_t token) |
880 | 82 | { |
881 | 82 | switch (token) { |
882 | 5 | case T_PUBLIC: |
883 | 5 | return "public"; |
884 | 5 | case T_PROTECTED: |
885 | 5 | return "protected"; |
886 | 9 | case T_PRIVATE: |
887 | 9 | return "private"; |
888 | 14 | case T_STATIC: |
889 | 14 | return "static"; |
890 | 12 | case T_FINAL: |
891 | 12 | return "final"; |
892 | 23 | case T_READONLY: |
893 | 23 | return "readonly"; |
894 | 8 | case T_ABSTRACT: |
895 | 8 | return "abstract"; |
896 | 2 | case T_PUBLIC_SET: |
897 | 2 | return "public(set)"; |
898 | 3 | case T_PROTECTED_SET: |
899 | 3 | return "protected(set)"; |
900 | 1 | case T_PRIVATE_SET: |
901 | 1 | return "private(set)"; |
902 | 0 | default: ZEND_UNREACHABLE(); |
903 | 82 | } |
904 | 82 | } |
905 | | |
906 | | uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token) |
907 | 69.2k | { |
908 | 69.2k | switch (token) { |
909 | 44.4k | case T_PUBLIC: |
910 | 44.4k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
911 | 44.4k | return ZEND_ACC_PUBLIC; |
912 | 44.4k | } |
913 | 5 | break; |
914 | 2.69k | case T_PROTECTED: |
915 | 2.69k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
916 | 2.68k | return ZEND_ACC_PROTECTED; |
917 | 2.68k | } |
918 | 5 | break; |
919 | 6.52k | case T_PRIVATE: |
920 | 6.52k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
921 | 6.51k | return ZEND_ACC_PRIVATE; |
922 | 6.51k | } |
923 | 9 | break; |
924 | 919 | case T_READONLY: |
925 | 919 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
926 | 905 | return ZEND_ACC_READONLY; |
927 | 905 | } |
928 | 14 | break; |
929 | 1.11k | case T_ABSTRACT: |
930 | 1.11k | if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) { |
931 | 1.11k | return ZEND_ACC_ABSTRACT; |
932 | 1.11k | } |
933 | 2 | break; |
934 | 1.66k | case T_FINAL: |
935 | 1.66k | return ZEND_ACC_FINAL; |
936 | 10.8k | case T_STATIC: |
937 | 10.8k | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) { |
938 | 10.8k | return ZEND_ACC_STATIC; |
939 | 10.8k | } |
940 | 10 | break; |
941 | 274 | case T_PUBLIC_SET: |
942 | 274 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
943 | 272 | return ZEND_ACC_PUBLIC_SET; |
944 | 272 | } |
945 | 2 | break; |
946 | 318 | case T_PROTECTED_SET: |
947 | 318 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
948 | 315 | return ZEND_ACC_PROTECTED_SET; |
949 | 315 | } |
950 | 3 | break; |
951 | 435 | case T_PRIVATE_SET: |
952 | 435 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
953 | 434 | return ZEND_ACC_PRIVATE_SET; |
954 | 434 | } |
955 | 1 | break; |
956 | 69.2k | } |
957 | | |
958 | 51 | char *member; |
959 | 51 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
960 | 0 | member = "property"; |
961 | 51 | } else if (target == ZEND_MODIFIER_TARGET_METHOD) { |
962 | 11 | member = "method"; |
963 | 40 | } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) { |
964 | 6 | member = "class constant"; |
965 | 34 | } else if (target == ZEND_MODIFIER_TARGET_CPP) { |
966 | 6 | member = "parameter"; |
967 | 28 | } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
968 | 28 | member = "property hook"; |
969 | 28 | } else { |
970 | 0 | ZEND_UNREACHABLE(); |
971 | 0 | } |
972 | | |
973 | 51 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
974 | 51 | "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member); |
975 | 51 | return 0; |
976 | 51 | } |
977 | | |
978 | | uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers) |
979 | 59.1k | { |
980 | 59.1k | uint32_t flags = 0; |
981 | 59.1k | const zend_ast_list *modifier_list = zend_ast_get_list(modifiers); |
982 | | |
983 | 127k | for (uint32_t i = 0; i < modifier_list->children; i++) { |
984 | 68.5k | uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i])); |
985 | 68.5k | uint32_t new_flag = zend_modifier_token_to_flag(target, token); |
986 | 68.5k | if (!new_flag) { |
987 | 46 | return 0; |
988 | 46 | } |
989 | | /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */ |
990 | 68.5k | bool duplicate_flag = (flags & new_flag); |
991 | 68.4k | flags = zend_add_member_modifier(flags, new_flag, target); |
992 | 68.4k | if (!flags) { |
993 | 69 | return 0; |
994 | 69 | } |
995 | 68.3k | if (duplicate_flag) { |
996 | 31 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
997 | 31 | "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token)); |
998 | 31 | return 0; |
999 | 31 | } |
1000 | 68.3k | } |
1001 | | |
1002 | 59.0k | return flags; |
1003 | 59.1k | } |
1004 | | |
1005 | | uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */ |
1006 | 101 | { |
1007 | 101 | uint32_t new_flags = flags | new_flag; |
1008 | 101 | if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) { |
1009 | 5 | zend_throw_exception(zend_ce_compile_error, |
1010 | 5 | "Multiple abstract modifiers are not allowed", 0); |
1011 | 5 | return 0; |
1012 | 5 | } |
1013 | 96 | if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) { |
1014 | 5 | zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0); |
1015 | 5 | return 0; |
1016 | 5 | } |
1017 | 91 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1018 | 5 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1019 | 5 | return 0; |
1020 | 5 | } |
1021 | 86 | if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) { |
1022 | 5 | zend_throw_exception(zend_ce_compile_error, |
1023 | 5 | "Cannot use the final modifier on an abstract class", 0); |
1024 | 5 | return 0; |
1025 | 5 | } |
1026 | 81 | return new_flags; |
1027 | 86 | } |
1028 | | /* }}} */ |
1029 | | |
1030 | | uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag) |
1031 | 63 | { |
1032 | 63 | uint32_t new_flags = flags | new_flag; |
1033 | 63 | if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) { |
1034 | 5 | zend_throw_exception(zend_ce_compile_error, |
1035 | 5 | "Cannot use the abstract modifier on an anonymous class", 0); |
1036 | 5 | return 0; |
1037 | 5 | } |
1038 | 58 | if (new_flag & ZEND_ACC_FINAL) { |
1039 | 5 | zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0); |
1040 | 5 | return 0; |
1041 | 5 | } |
1042 | 53 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1043 | 5 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1044 | 5 | return 0; |
1045 | 5 | } |
1046 | 48 | return new_flags; |
1047 | 53 | } |
1048 | | |
1049 | | uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */ |
1050 | 68.4k | { |
1051 | 68.4k | uint32_t new_flags = flags | new_flag; |
1052 | 68.4k | if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) { |
1053 | 51 | zend_throw_exception(zend_ce_compile_error, |
1054 | 51 | "Multiple access type modifiers are not allowed", 0); |
1055 | 51 | return 0; |
1056 | 51 | } |
1057 | 68.4k | if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) { |
1058 | 10 | if (target == ZEND_MODIFIER_TARGET_METHOD) { |
1059 | 5 | zend_throw_exception(zend_ce_compile_error, |
1060 | 5 | "Cannot use the final modifier on an abstract method", 0); |
1061 | 5 | return 0; |
1062 | 5 | } |
1063 | 5 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
1064 | 5 | zend_throw_exception(zend_ce_compile_error, |
1065 | 5 | "Cannot use the final modifier on an abstract property", 0); |
1066 | 5 | return 0; |
1067 | 5 | } |
1068 | 5 | } |
1069 | 68.4k | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
1070 | 33.4k | if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) { |
1071 | 8 | zend_throw_exception(zend_ce_compile_error, |
1072 | 8 | "Multiple access type modifiers are not allowed", 0); |
1073 | 8 | return 0; |
1074 | 8 | } |
1075 | 33.4k | } |
1076 | 68.3k | return new_flags; |
1077 | 68.4k | } |
1078 | | /* }}} */ |
1079 | | |
1080 | 20.6k | ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) { |
1081 | 20.6k | return zend_string_concat3( |
1082 | 20.6k | ZSTR_VAL(class_name), ZSTR_LEN(class_name), |
1083 | 20.6k | "::", sizeof("::") - 1, |
1084 | 20.6k | ZSTR_VAL(member_name), ZSTR_LEN(member_name)); |
1085 | 20.6k | } |
1086 | | |
1087 | 18.5M | static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) { |
1088 | 18.5M | return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len); |
1089 | 18.5M | } |
1090 | | |
1091 | 19.2M | static zend_string *zend_prefix_with_ns(zend_string *name) { |
1092 | 19.2M | if (FC(current_namespace)) { |
1093 | 18.5M | const zend_string *ns = FC(current_namespace); |
1094 | 18.5M | return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
1095 | 18.5M | } else { |
1096 | 727k | return zend_string_copy(name); |
1097 | 727k | } |
1098 | 19.2M | } |
1099 | | |
1100 | | static zend_string *zend_resolve_non_class_name( |
1101 | | zend_string *name, uint32_t type, bool *is_fully_qualified, |
1102 | | bool case_sensitive, const HashTable *current_import_sub |
1103 | 15.1M | ) { |
1104 | 15.1M | const char *compound; |
1105 | 15.1M | *is_fully_qualified = false; |
1106 | | |
1107 | 15.1M | if (ZSTR_VAL(name)[0] == '\\') { |
1108 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1109 | 210 | *is_fully_qualified = true; |
1110 | 210 | return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1111 | 210 | } |
1112 | | |
1113 | 15.1M | if (type == ZEND_NAME_FQ) { |
1114 | 76.7k | *is_fully_qualified = true; |
1115 | 76.7k | return zend_string_copy(name); |
1116 | 76.7k | } |
1117 | | |
1118 | 15.0M | if (type == ZEND_NAME_RELATIVE) { |
1119 | 908 | *is_fully_qualified = true; |
1120 | 908 | return zend_prefix_with_ns(name); |
1121 | 908 | } |
1122 | | |
1123 | 15.0M | if (current_import_sub) { |
1124 | | /* If an unqualified name is a function/const alias, replace it. */ |
1125 | 1.89k | zend_string *import_name; |
1126 | 1.89k | if (case_sensitive) { |
1127 | 1.48k | import_name = zend_hash_find_ptr(current_import_sub, name); |
1128 | 1.48k | } else { |
1129 | 416 | import_name = zend_hash_find_ptr_lc(current_import_sub, name); |
1130 | 416 | } |
1131 | | |
1132 | 1.89k | if (import_name) { |
1133 | 599 | *is_fully_qualified = true; |
1134 | 599 | return zend_string_copy(import_name); |
1135 | 599 | } |
1136 | 1.89k | } |
1137 | | |
1138 | 15.0M | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1139 | 15.0M | if (compound) { |
1140 | 4.50k | *is_fully_qualified = true; |
1141 | 4.50k | } |
1142 | | |
1143 | 15.0M | if (compound && FC(imports)) { |
1144 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1145 | 2.33k | size_t len = compound - ZSTR_VAL(name); |
1146 | 2.33k | const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1147 | | |
1148 | 2.33k | if (import_name) { |
1149 | 345 | return zend_concat_names( |
1150 | 345 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1151 | 345 | } |
1152 | 2.33k | } |
1153 | | |
1154 | 15.0M | return zend_prefix_with_ns(name); |
1155 | 15.0M | } |
1156 | | /* }}} */ |
1157 | | |
1158 | | static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1159 | 4.41M | { |
1160 | 4.41M | return zend_resolve_non_class_name( |
1161 | 4.41M | name, type, is_fully_qualified, false, FC(imports_function)); |
1162 | 4.41M | } |
1163 | | |
1164 | | static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1165 | 10.7M | { |
1166 | 10.7M | return zend_resolve_non_class_name( |
1167 | 10.7M | name, type, is_fully_qualified, true, FC(imports_const)); |
1168 | 10.7M | } |
1169 | | |
1170 | | static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */ |
1171 | 3.82M | { |
1172 | 3.82M | const char *compound; |
1173 | | |
1174 | 3.82M | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1175 | 3.24k | if (type == ZEND_NAME_FQ) { |
1176 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
1177 | 7 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1178 | 7 | } |
1179 | 3.23k | if (type == ZEND_NAME_RELATIVE) { |
1180 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1181 | 0 | "'namespace\\%s' is an invalid class name", ZSTR_VAL(name)); |
1182 | 0 | } |
1183 | 3.23k | ZEND_ASSERT(type == ZEND_NAME_NOT_FQ); |
1184 | 3.23k | return zend_string_copy(name); |
1185 | 3.23k | } |
1186 | | |
1187 | 3.82M | if (type == ZEND_NAME_RELATIVE) { |
1188 | 414 | return zend_prefix_with_ns(name); |
1189 | 414 | } |
1190 | | |
1191 | 3.82M | if (type == ZEND_NAME_FQ) { |
1192 | 18.2k | if (ZSTR_VAL(name)[0] == '\\') { |
1193 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1194 | 464 | name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1195 | 464 | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1196 | 1 | zend_error_noreturn(E_COMPILE_ERROR, |
1197 | 1 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1198 | 1 | } |
1199 | 463 | return name; |
1200 | 464 | } |
1201 | | |
1202 | 17.7k | return zend_string_copy(name); |
1203 | 18.2k | } |
1204 | | |
1205 | 3.80M | if (FC(imports)) { |
1206 | 2.68k | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1207 | 2.68k | if (compound) { |
1208 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1209 | 733 | size_t len = compound - ZSTR_VAL(name); |
1210 | 733 | const zend_string *import_name = |
1211 | 733 | zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1212 | | |
1213 | 733 | if (import_name) { |
1214 | 309 | return zend_concat_names( |
1215 | 309 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1216 | 309 | } |
1217 | 1.95k | } else { |
1218 | | /* If an unqualified name is an alias, replace it. */ |
1219 | 1.95k | zend_string *import_name |
1220 | 1.95k | = zend_hash_find_ptr_lc(FC(imports), name); |
1221 | | |
1222 | 1.95k | if (import_name) { |
1223 | 914 | return zend_string_copy(import_name); |
1224 | 914 | } |
1225 | 1.95k | } |
1226 | 2.68k | } |
1227 | | |
1228 | | /* If not fully qualified and not an alias, prepend the current namespace */ |
1229 | 3.80M | return zend_prefix_with_ns(name); |
1230 | 3.80M | } |
1231 | | /* }}} */ |
1232 | | |
1233 | | static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */ |
1234 | 3.26M | { |
1235 | 3.26M | const zval *class_name = zend_ast_get_zval(ast); |
1236 | 3.26M | if (Z_TYPE_P(class_name) != IS_STRING) { |
1237 | 35 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1238 | 35 | } |
1239 | 3.26M | return zend_resolve_class_name(Z_STR_P(class_name), ast->attr); |
1240 | 3.26M | } |
1241 | | /* }}} */ |
1242 | | |
1243 | | static void label_ptr_dtor(zval *zv) /* {{{ */ |
1244 | 2.83k | { |
1245 | 2.83k | efree_size(Z_PTR_P(zv), sizeof(zend_label)); |
1246 | 2.83k | } |
1247 | | /* }}} */ |
1248 | | |
1249 | 1.78k | static void str_dtor(zval *zv) /* {{{ */ { |
1250 | 1.78k | zend_string_release_ex(Z_STR_P(zv), 0); |
1251 | 1.78k | } |
1252 | | /* }}} */ |
1253 | | |
1254 | | static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */ |
1255 | 35.5k | { |
1256 | 35.5k | zend_op_array *op_array = CG(active_op_array); |
1257 | 35.5k | uint32_t try_catch_offset = op_array->last_try_catch++; |
1258 | 35.5k | zend_try_catch_element *elem; |
1259 | | |
1260 | 35.5k | op_array->try_catch_array = safe_erealloc( |
1261 | 35.5k | op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0); |
1262 | | |
1263 | 35.5k | elem = &op_array->try_catch_array[try_catch_offset]; |
1264 | 35.5k | elem->try_op = try_op; |
1265 | 35.5k | elem->catch_op = 0; |
1266 | 35.5k | elem->finally_op = 0; |
1267 | 35.5k | elem->finally_end = 0; |
1268 | | |
1269 | 35.5k | return try_catch_offset; |
1270 | 35.5k | } |
1271 | | /* }}} */ |
1272 | | |
1273 | | ZEND_API void function_add_ref(zend_function *function) /* {{{ */ |
1274 | 1.88k | { |
1275 | 1.88k | if (function->type == ZEND_USER_FUNCTION) { |
1276 | 1.88k | zend_op_array *op_array = &function->op_array; |
1277 | 1.88k | if (op_array->refcount) { |
1278 | 254 | (*op_array->refcount)++; |
1279 | 254 | } |
1280 | | |
1281 | 1.88k | ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL); |
1282 | 1.88k | ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL); |
1283 | 1.88k | } |
1284 | | |
1285 | 1.88k | if (function->common.function_name) { |
1286 | 1.88k | zend_string_addref(function->common.function_name); |
1287 | 1.88k | } |
1288 | 1.88k | } |
1289 | | /* }}} */ |
1290 | | |
1291 | | 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) /* {{{ */ |
1292 | 124 | { |
1293 | 124 | const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname); |
1294 | 124 | int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR; |
1295 | 124 | const zend_function *old_function; |
1296 | | |
1297 | 124 | ZEND_ASSERT(zv != NULL); |
1298 | 124 | old_function = Z_PTR_P(zv); |
1299 | 124 | if (old_function->type == ZEND_USER_FUNCTION |
1300 | 119 | && old_function->op_array.last > 0) { |
1301 | 119 | zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)", |
1302 | 119 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name), |
1303 | 119 | ZSTR_VAL(old_function->op_array.filename), |
1304 | 119 | old_function->op_array.line_start); |
1305 | 119 | } else { |
1306 | 5 | zend_error_noreturn(error_level, "Cannot redeclare function %s()", |
1307 | 5 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name)); |
1308 | 5 | } |
1309 | 124 | } |
1310 | | |
1311 | | ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */ |
1312 | 229 | { |
1313 | 229 | zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func); |
1314 | 229 | if (UNEXPECTED(!added_func)) { |
1315 | 14 | do_bind_function_error(Z_STR_P(lcname), &func->op_array, false); |
1316 | 0 | return FAILURE; |
1317 | 14 | } |
1318 | | |
1319 | 215 | if (func->op_array.refcount) { |
1320 | 37 | ++*func->op_array.refcount; |
1321 | 37 | } |
1322 | 215 | if (func->common.function_name) { |
1323 | 215 | zend_string_addref(func->common.function_name); |
1324 | 215 | } |
1325 | 215 | zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname)); |
1326 | 215 | return SUCCESS; |
1327 | 229 | } |
1328 | | /* }}} */ |
1329 | | |
1330 | | ZEND_API zend_class_entry *zend_bind_class_in_slot( |
1331 | | zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name) |
1332 | 7.64k | { |
1333 | 7.64k | zend_class_entry *ce = Z_PTR_P(class_table_slot); |
1334 | 7.64k | bool is_preloaded = |
1335 | 7.64k | (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD); |
1336 | 7.64k | bool success; |
1337 | 7.64k | if (EXPECTED(!is_preloaded)) { |
1338 | 7.64k | success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL; |
1339 | 7.64k | } else { |
1340 | | /* If preloading is used, don't replace the existing bucket, add a new one. */ |
1341 | 0 | success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL; |
1342 | 0 | } |
1343 | 7.64k | if (UNEXPECTED(!success)) { |
1344 | 155 | zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1345 | 155 | ZEND_ASSERT(old_class); |
1346 | 155 | zend_class_redeclaration_error(E_COMPILE_ERROR, old_class); |
1347 | 155 | return NULL; |
1348 | 155 | } |
1349 | | |
1350 | 7.49k | if (ce->ce_flags & ZEND_ACC_LINKED) { |
1351 | 284 | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1352 | 284 | return ce; |
1353 | 284 | } |
1354 | | |
1355 | 7.20k | ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname)); |
1356 | 7.20k | if (ce) { |
1357 | 5.71k | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1358 | 5.71k | return ce; |
1359 | 5.71k | } |
1360 | | |
1361 | 1.49k | if (!is_preloaded) { |
1362 | | /* Reload bucket pointer, the hash table may have been reallocated */ |
1363 | 391 | zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname)); |
1364 | 391 | zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1)); |
1365 | 1.10k | } else { |
1366 | 1.10k | zend_hash_del(EG(class_table), Z_STR_P(lcname)); |
1367 | 1.10k | } |
1368 | 1.49k | return NULL; |
1369 | 7.20k | } |
1370 | | |
1371 | | ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */ |
1372 | 6.94k | { |
1373 | 6.94k | zval *rtd_key, *zv; |
1374 | | |
1375 | 6.94k | rtd_key = lcname + 1; |
1376 | | |
1377 | 6.94k | zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key)); |
1378 | | |
1379 | 6.94k | if (UNEXPECTED(!zv)) { |
1380 | 14 | const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1381 | 14 | ZEND_ASSERT(ce); |
1382 | 14 | zend_class_redeclaration_error(E_COMPILE_ERROR, ce); |
1383 | 14 | return FAILURE; |
1384 | 14 | } |
1385 | | |
1386 | | /* Register the derived class */ |
1387 | 6.93k | return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE; |
1388 | 6.94k | } |
1389 | | /* }}} */ |
1390 | | |
1391 | 21.0k | static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) { |
1392 | 21.0k | zend_string *result; |
1393 | 21.0k | if (type == NULL) { |
1394 | 15.7k | return zend_string_copy(new_type); |
1395 | 15.7k | } |
1396 | | |
1397 | 5.36k | if (is_intersection) { |
1398 | 2.21k | result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type), |
1399 | 2.21k | "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1400 | 2.21k | zend_string_release(type); |
1401 | 3.15k | } else { |
1402 | 3.15k | result = zend_string_concat3( |
1403 | 3.15k | ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1404 | 3.15k | zend_string_release(type); |
1405 | 3.15k | } |
1406 | 5.36k | return result; |
1407 | 21.0k | } |
1408 | | |
1409 | 3.70k | static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) { |
1410 | 3.70k | if (scope) { |
1411 | 2.16k | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1412 | 42 | name = scope->name; |
1413 | 2.12k | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) { |
1414 | 0 | name = scope->parent->name; |
1415 | 0 | } |
1416 | 2.16k | } |
1417 | | |
1418 | | /* The resolved name for anonymous classes contains null bytes. Cut off everything after the |
1419 | | * null byte here, to avoid larger parts of the type being omitted by printing code later. */ |
1420 | 3.70k | size_t len = strlen(ZSTR_VAL(name)); |
1421 | 3.70k | if (len != ZSTR_LEN(name)) { |
1422 | 10 | return zend_string_init(ZSTR_VAL(name), len, 0); |
1423 | 10 | } |
1424 | 3.69k | return zend_string_copy(name); |
1425 | 3.70k | } |
1426 | | |
1427 | | static zend_string *add_intersection_type(zend_string *str, |
1428 | | const zend_type_list *intersection_type_list, |
1429 | | bool is_bracketed) |
1430 | 1.26k | { |
1431 | 1.26k | const zend_type *single_type; |
1432 | 1.26k | zend_string *intersection_str = NULL; |
1433 | | |
1434 | 4.74k | ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) { |
1435 | 4.74k | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type)); |
1436 | 4.74k | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type)); |
1437 | | |
1438 | 3.47k | intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true); |
1439 | 3.47k | } ZEND_TYPE_LIST_FOREACH_END(); |
1440 | | |
1441 | 1.26k | ZEND_ASSERT(intersection_str); |
1442 | | |
1443 | 1.26k | if (is_bracketed) { |
1444 | 936 | zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1); |
1445 | 936 | zend_string_release(intersection_str); |
1446 | 936 | intersection_str = result; |
1447 | 936 | } |
1448 | 1.26k | str = add_type_string(str, intersection_str, /* is_intersection */ false); |
1449 | 1.26k | zend_string_release(intersection_str); |
1450 | 1.26k | return str; |
1451 | 1.26k | } |
1452 | | |
1453 | 17.1k | zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) { |
1454 | 17.1k | zend_string *str = NULL; |
1455 | | |
1456 | | /* Pure intersection type */ |
1457 | 17.1k | if (ZEND_TYPE_IS_INTERSECTION(type)) { |
1458 | 326 | ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type)); |
1459 | 326 | str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false); |
1460 | 16.8k | } else if (ZEND_TYPE_HAS_LIST(type)) { |
1461 | | /* A union type might not be a list */ |
1462 | 575 | const zend_type *list_type; |
1463 | 2.50k | ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) { |
1464 | 2.50k | if (ZEND_TYPE_IS_INTERSECTION(*list_type)) { |
1465 | 936 | str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true); |
1466 | 936 | continue; |
1467 | 936 | } |
1468 | 995 | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type)); |
1469 | 995 | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type)); |
1470 | | |
1471 | 995 | zend_string *name = ZEND_TYPE_NAME(*list_type); |
1472 | 995 | zend_string *resolved = resolve_class_name(name, scope); |
1473 | 995 | str = add_type_string(str, resolved, /* is_intersection */ false); |
1474 | 995 | zend_string_release(resolved); |
1475 | 995 | } ZEND_TYPE_LIST_FOREACH_END(); |
1476 | 16.2k | } else if (ZEND_TYPE_HAS_NAME(type)) { |
1477 | 2.70k | str = resolve_class_name(ZEND_TYPE_NAME(type), scope); |
1478 | 2.70k | } |
1479 | | |
1480 | 17.1k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
1481 | | |
1482 | 17.1k | if (type_mask == MAY_BE_ANY) { |
1483 | 257 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false); |
1484 | | |
1485 | 257 | return str; |
1486 | 257 | } |
1487 | 16.8k | if (type_mask & MAY_BE_STATIC) { |
1488 | 195 | zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC); |
1489 | | // During compilation of eval'd code the called scope refers to the scope calling the eval |
1490 | 195 | if (scope && !zend_is_compiling()) { |
1491 | 63 | const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data)); |
1492 | 63 | if (called_scope) { |
1493 | 33 | name = called_scope->name; |
1494 | 33 | } |
1495 | 63 | } |
1496 | 195 | str = add_type_string(str, name, /* is_intersection */ false); |
1497 | 195 | } |
1498 | 16.8k | if (type_mask & MAY_BE_CALLABLE) { |
1499 | 68 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false); |
1500 | 68 | } |
1501 | 16.8k | if (type_mask & MAY_BE_OBJECT) { |
1502 | 267 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false); |
1503 | 267 | } |
1504 | 16.8k | if (type_mask & MAY_BE_ARRAY) { |
1505 | 1.74k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false); |
1506 | 1.74k | } |
1507 | 16.8k | if (type_mask & MAY_BE_STRING) { |
1508 | 6.97k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false); |
1509 | 6.97k | } |
1510 | 16.8k | if (type_mask & MAY_BE_LONG) { |
1511 | 3.95k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false); |
1512 | 3.95k | } |
1513 | 16.8k | if (type_mask & MAY_BE_DOUBLE) { |
1514 | 598 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false); |
1515 | 598 | } |
1516 | 16.8k | if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) { |
1517 | 684 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false); |
1518 | 16.2k | } else if (type_mask & MAY_BE_FALSE) { |
1519 | 168 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false); |
1520 | 16.0k | } else if (type_mask & MAY_BE_TRUE) { |
1521 | 40 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false); |
1522 | 40 | } |
1523 | 16.8k | if (type_mask & MAY_BE_VOID) { |
1524 | 207 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false); |
1525 | 207 | } |
1526 | 16.8k | if (type_mask & MAY_BE_NEVER) { |
1527 | 19 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false); |
1528 | 19 | } |
1529 | | |
1530 | 16.8k | if (type_mask & MAY_BE_NULL) { |
1531 | 1.57k | bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL; |
1532 | 1.57k | bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL; |
1533 | 1.57k | if (!is_union && !has_intersection) { |
1534 | 1.39k | zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str)); |
1535 | 1.39k | zend_string_release(str); |
1536 | 1.39k | return nullable_str; |
1537 | 1.39k | } |
1538 | | |
1539 | 171 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false); |
1540 | 171 | } |
1541 | 15.5k | return str; |
1542 | 16.8k | } |
1543 | | |
1544 | 11.9k | ZEND_API zend_string *zend_type_to_string(zend_type type) { |
1545 | 11.9k | return zend_type_to_string_resolved(type, NULL); |
1546 | 11.9k | } |
1547 | | |
1548 | 2.03k | static bool is_generator_compatible_class_type(const zend_string *name) { |
1549 | 2.03k | return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE)) |
1550 | 1.75k | || zend_string_equals_literal_ci(name, "Iterator") |
1551 | 1.22k | || zend_string_equals_literal_ci(name, "Generator"); |
1552 | 2.03k | } |
1553 | | |
1554 | | static void zend_mark_function_as_generator(void) /* {{{ */ |
1555 | 819k | { |
1556 | 819k | if (!CG(active_op_array)->function_name) { |
1557 | 61 | zend_error_noreturn(E_COMPILE_ERROR, |
1558 | 61 | "The \"yield\" expression can only be used inside a function"); |
1559 | 61 | } |
1560 | | |
1561 | 818k | if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
1562 | 1.14k | const zend_type return_type = CG(active_op_array)->arg_info[-1].type; |
1563 | 1.14k | bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0; |
1564 | 1.14k | if (!valid_type) { |
1565 | 1.10k | const zend_type *single_type; |
1566 | 3.14k | ZEND_TYPE_FOREACH(return_type, single_type) { |
1567 | 3.14k | if (ZEND_TYPE_HAS_NAME(*single_type) |
1568 | 2.03k | && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) { |
1569 | 1.07k | valid_type = true; |
1570 | 1.07k | break; |
1571 | 1.07k | } |
1572 | 3.14k | } ZEND_TYPE_FOREACH_END(); |
1573 | 1.10k | } |
1574 | | |
1575 | 1.14k | if (!valid_type) { |
1576 | 32 | zend_string *str = zend_type_to_string(return_type); |
1577 | 32 | zend_error_noreturn(E_COMPILE_ERROR, |
1578 | 32 | "Generator return type must be a supertype of Generator, %s given", |
1579 | 32 | ZSTR_VAL(str)); |
1580 | 32 | } |
1581 | 1.14k | } |
1582 | | |
1583 | 818k | CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR; |
1584 | 818k | } |
1585 | | /* }}} */ |
1586 | | |
1587 | | ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */ |
1588 | 68.9k | { |
1589 | 68.9k | size_t prop_name_length = 1 + src1_length + 1 + src2_length; |
1590 | 68.9k | zend_string *prop_name = zend_string_alloc(prop_name_length, internal); |
1591 | | |
1592 | 68.9k | ZSTR_VAL(prop_name)[0] = '\0'; |
1593 | 68.9k | memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1); |
1594 | 68.9k | memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1); |
1595 | 68.9k | return prop_name; |
1596 | 68.9k | } |
1597 | | /* }}} */ |
1598 | | |
1599 | | 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) /* {{{ */ |
1600 | 1.30M | { |
1601 | 1.30M | size_t class_name_len; |
1602 | 1.30M | size_t anonclass_src_len; |
1603 | | |
1604 | 1.30M | *class_name = NULL; |
1605 | | |
1606 | 1.30M | if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') { |
1607 | 1.17M | *prop_name = ZSTR_VAL(name); |
1608 | 1.17M | if (prop_len) { |
1609 | 804k | *prop_len = ZSTR_LEN(name); |
1610 | 804k | } |
1611 | 1.17M | return SUCCESS; |
1612 | 1.17M | } |
1613 | 132k | if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') { |
1614 | 1.93k | zend_error(E_NOTICE, "Illegal member variable name"); |
1615 | 1.93k | *prop_name = ZSTR_VAL(name); |
1616 | 1.93k | if (prop_len) { |
1617 | 1.52k | *prop_len = ZSTR_LEN(name); |
1618 | 1.52k | } |
1619 | 1.93k | return FAILURE; |
1620 | 1.93k | } |
1621 | | |
1622 | 130k | class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2); |
1623 | 130k | if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') { |
1624 | 420 | zend_error(E_NOTICE, "Corrupt member variable name"); |
1625 | 420 | *prop_name = ZSTR_VAL(name); |
1626 | 420 | if (prop_len) { |
1627 | 192 | *prop_len = ZSTR_LEN(name); |
1628 | 192 | } |
1629 | 420 | return FAILURE; |
1630 | 420 | } |
1631 | | |
1632 | 130k | *class_name = ZSTR_VAL(name) + 1; |
1633 | 130k | anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2); |
1634 | 130k | if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) { |
1635 | 29.5k | class_name_len += anonclass_src_len + 1; |
1636 | 29.5k | } |
1637 | 130k | *prop_name = ZSTR_VAL(name) + class_name_len + 2; |
1638 | 130k | if (prop_len) { |
1639 | 80.7k | *prop_len = ZSTR_LEN(name) - class_name_len - 2; |
1640 | 80.7k | } |
1641 | 130k | return SUCCESS; |
1642 | 130k | } |
1643 | | /* }}} */ |
1644 | | |
1645 | | static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks) |
1646 | 230 | { |
1647 | 230 | if (zend_hash_num_elements(array) > *max_checks) { |
1648 | 3 | return false; |
1649 | 3 | } |
1650 | 227 | *max_checks -= zend_hash_num_elements(array); |
1651 | | |
1652 | 227 | zval *element; |
1653 | 633 | ZEND_HASH_FOREACH_VAL(array, element) { |
1654 | 633 | if (Z_TYPE_P(element) < IS_ARRAY) { |
1655 | 53 | continue; |
1656 | 150 | } else if (Z_TYPE_P(element) == IS_ARRAY) { |
1657 | 150 | if (!array_is_const_ex(array, max_checks)) { |
1658 | 150 | return false; |
1659 | 150 | } |
1660 | 150 | } else { |
1661 | 0 | return false; |
1662 | 0 | } |
1663 | 633 | } ZEND_HASH_FOREACH_END(); |
1664 | | |
1665 | 77 | return true; |
1666 | 227 | } |
1667 | | |
1668 | | static bool array_is_const(const zend_array *array) |
1669 | 80 | { |
1670 | 80 | uint32_t max_checks = 50; |
1671 | 80 | return array_is_const_ex(array, &max_checks); |
1672 | 80 | } |
1673 | | |
1674 | 11.1k | static bool can_ct_eval_const(const zend_constant *c) { |
1675 | 11.1k | if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) { |
1676 | 199 | return false; |
1677 | 199 | } |
1678 | 10.9k | if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) |
1679 | 10.9k | && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) |
1680 | 9.76k | && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) |
1681 | 9.76k | && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) { |
1682 | 9.76k | return true; |
1683 | 9.76k | } |
1684 | 1.15k | if (Z_TYPE(c->value) < IS_ARRAY |
1685 | 1.15k | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1686 | 16 | return 1; |
1687 | 1.13k | } else if (Z_TYPE(c->value) == IS_ARRAY |
1688 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION) |
1689 | 0 | && array_is_const(Z_ARR(c->value))) { |
1690 | 0 | return 1; |
1691 | 0 | } |
1692 | 1.13k | return false; |
1693 | 1.15k | } |
1694 | | |
1695 | | static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */ |
1696 | 10.7M | { |
1697 | | /* Substitute true, false and null (including unqualified usage in namespaces) |
1698 | | * before looking up the possibly namespaced name. */ |
1699 | 10.7M | const char *lookup_name = ZSTR_VAL(name); |
1700 | 10.7M | size_t lookup_len = ZSTR_LEN(name); |
1701 | | |
1702 | 10.7M | if (!is_fully_qualified) { |
1703 | 10.7M | zend_get_unqualified_name(name, &lookup_name, &lookup_len); |
1704 | 10.7M | } |
1705 | | |
1706 | 10.7M | zend_constant *c; |
1707 | 10.7M | if ((c = zend_get_special_const(lookup_name, lookup_len))) { |
1708 | 31.7k | ZVAL_COPY_VALUE(zv, &c->value); |
1709 | 31.7k | return true; |
1710 | 31.7k | } |
1711 | 10.7M | c = zend_hash_find_ptr(EG(zend_constants), name); |
1712 | 10.7M | if (c && can_ct_eval_const(c)) { |
1713 | 9.78k | ZVAL_COPY_OR_DUP(zv, &c->value); |
1714 | 9.78k | return true; |
1715 | 9.78k | } |
1716 | 10.7M | return false; |
1717 | 10.7M | } |
1718 | | /* }}} */ |
1719 | | |
1720 | | static inline bool zend_is_scope_known(void) /* {{{ */ |
1721 | 15.2k | { |
1722 | 15.2k | if (!CG(active_op_array)) { |
1723 | | /* This can only happen when evaluating a default value string. */ |
1724 | 0 | return false; |
1725 | 0 | } |
1726 | | |
1727 | 15.2k | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
1728 | | /* Closures can be rebound to a different scope */ |
1729 | 2.71k | return false; |
1730 | 2.71k | } |
1731 | | |
1732 | 12.5k | if (!CG(active_class_entry)) { |
1733 | | /* The scope is known if we're in a free function (no scope), but not if we're in |
1734 | | * a file/eval (which inherits including/eval'ing scope). */ |
1735 | 1.21k | return CG(active_op_array)->function_name != NULL; |
1736 | 1.21k | } |
1737 | | |
1738 | | /* For traits self etc refers to the using class, not the trait itself */ |
1739 | 11.3k | return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0; |
1740 | 12.5k | } |
1741 | | /* }}} */ |
1742 | | |
1743 | | static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */ |
1744 | 88.4k | { |
1745 | 88.4k | if (!CG(active_class_entry)) { |
1746 | 82.9k | return false; |
1747 | 82.9k | } |
1748 | 5.48k | if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) { |
1749 | 1.62k | return true; |
1750 | 1.62k | } |
1751 | 3.86k | return fetch_type == ZEND_FETCH_CLASS_DEFAULT |
1752 | 3.09k | && zend_string_equals_ci(class_name, CG(active_class_entry)->name); |
1753 | 5.48k | } |
1754 | | /* }}} */ |
1755 | | |
1756 | | uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */ |
1757 | 5.31M | { |
1758 | 5.31M | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1759 | 10.1k | return ZEND_FETCH_CLASS_SELF; |
1760 | 5.30M | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
1761 | 6.84k | return ZEND_FETCH_CLASS_PARENT; |
1762 | 5.29M | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
1763 | 2.20k | return ZEND_FETCH_CLASS_STATIC; |
1764 | 5.29M | } else { |
1765 | 5.29M | return ZEND_FETCH_CLASS_DEFAULT; |
1766 | 5.29M | } |
1767 | 5.31M | } |
1768 | | /* }}} */ |
1769 | | |
1770 | | static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */ |
1771 | 1.26M | { |
1772 | | /* Fully qualified names are always default refs */ |
1773 | 1.26M | if (name_ast->attr == ZEND_NAME_FQ) { |
1774 | 4.05k | return ZEND_FETCH_CLASS_DEFAULT; |
1775 | 4.05k | } |
1776 | | |
1777 | 1.26M | return zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
1778 | 1.26M | } |
1779 | | /* }}} */ |
1780 | | |
1781 | | static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type) |
1782 | 564k | { |
1783 | 564k | zend_string *class_name = zend_ast_get_str(ast); |
1784 | 564k | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) { |
1785 | 22 | zend_error_noreturn(E_COMPILE_ERROR, |
1786 | 22 | "Cannot use \"%s\" as %s, as it is reserved", |
1787 | 22 | ZSTR_VAL(class_name), type); |
1788 | 22 | } |
1789 | 564k | return zend_resolve_class_name(class_name, ast->attr); |
1790 | 564k | } |
1791 | | |
1792 | | static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */ |
1793 | 14.1k | { |
1794 | 14.1k | if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) { |
1795 | 5.73k | zend_class_entry *ce = CG(active_class_entry); |
1796 | 5.73k | if (!ce) { |
1797 | 25 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active", |
1798 | 25 | fetch_type == ZEND_FETCH_CLASS_SELF ? "self" : |
1799 | 25 | fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static"); |
1800 | 5.71k | } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) { |
1801 | 23 | zend_error_noreturn(E_COMPILE_ERROR, |
1802 | 23 | "Cannot use \"parent\" when current class scope has no parent"); |
1803 | 23 | } |
1804 | 5.73k | } |
1805 | 14.1k | } |
1806 | | /* }}} */ |
1807 | | |
1808 | | static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */ |
1809 | 9.08k | { |
1810 | 9.08k | uint32_t fetch_type; |
1811 | 9.08k | const zval *class_name; |
1812 | | |
1813 | 9.08k | if (class_ast->kind != ZEND_AST_ZVAL) { |
1814 | 2.36k | return false; |
1815 | 2.36k | } |
1816 | | |
1817 | 6.72k | class_name = zend_ast_get_zval(class_ast); |
1818 | | |
1819 | 6.72k | if (Z_TYPE_P(class_name) != IS_STRING) { |
1820 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1821 | 5 | } |
1822 | | |
1823 | 6.71k | fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name)); |
1824 | 6.71k | zend_ensure_valid_class_fetch_type(fetch_type); |
1825 | | |
1826 | 6.71k | switch (fetch_type) { |
1827 | 631 | case ZEND_FETCH_CLASS_SELF: |
1828 | 631 | if (CG(active_class_entry) && zend_is_scope_known()) { |
1829 | 251 | ZVAL_STR_COPY(zv, CG(active_class_entry)->name); |
1830 | 251 | return true; |
1831 | 251 | } |
1832 | 380 | return false; |
1833 | 930 | case ZEND_FETCH_CLASS_PARENT: |
1834 | 930 | if (CG(active_class_entry) && CG(active_class_entry)->parent_name |
1835 | 406 | && zend_is_scope_known()) { |
1836 | 406 | ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name); |
1837 | 406 | return true; |
1838 | 406 | } |
1839 | 524 | return false; |
1840 | 424 | case ZEND_FETCH_CLASS_STATIC: |
1841 | 424 | return false; |
1842 | 4.72k | case ZEND_FETCH_CLASS_DEFAULT: |
1843 | 4.72k | ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast)); |
1844 | 4.72k | return true; |
1845 | 0 | default: ZEND_UNREACHABLE(); |
1846 | 6.71k | } |
1847 | 6.71k | } |
1848 | | /* }}} */ |
1849 | | |
1850 | | /* We don't use zend_verify_const_access because we need to deal with unlinked classes. */ |
1851 | | static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope) |
1852 | 836 | { |
1853 | 836 | if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) { |
1854 | 14 | return 0; |
1855 | 822 | } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) { |
1856 | | /* This condition is only met on directly accessing trait constants, |
1857 | | * because the ce is replaced to the class entry of the composing class |
1858 | | * on binding. */ |
1859 | 1 | return 0; |
1860 | 821 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) { |
1861 | 519 | return 1; |
1862 | 519 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) { |
1863 | 302 | return c->ce == scope; |
1864 | 302 | } else { |
1865 | 0 | zend_class_entry *ce = c->ce; |
1866 | 0 | while (1) { |
1867 | 0 | if (ce == scope) { |
1868 | 0 | return 1; |
1869 | 0 | } |
1870 | 0 | if (!ce->parent) { |
1871 | 0 | break; |
1872 | 0 | } |
1873 | 0 | if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) { |
1874 | 0 | ce = ce->parent; |
1875 | 0 | } else { |
1876 | 0 | ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name); |
1877 | 0 | if (!ce) { |
1878 | 0 | break; |
1879 | 0 | } |
1880 | 0 | } |
1881 | 0 | } |
1882 | | /* Reverse case cannot be true during compilation */ |
1883 | 0 | return 0; |
1884 | 0 | } |
1885 | 836 | } |
1886 | | |
1887 | | static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */ |
1888 | 88.4k | { |
1889 | 88.4k | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
1890 | 88.4k | zend_class_constant *cc; |
1891 | 88.4k | zval *c; |
1892 | | |
1893 | 88.4k | if (class_name_refers_to_active_ce(class_name, fetch_type)) { |
1894 | 1.73k | cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name); |
1895 | 86.7k | } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1896 | 580 | const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name); |
1897 | 580 | if (ce) { |
1898 | 143 | cc = zend_hash_find_ptr(&ce->constants_table, name); |
1899 | 437 | } else { |
1900 | 437 | return 0; |
1901 | 437 | } |
1902 | 86.1k | } else { |
1903 | 86.1k | return 0; |
1904 | 86.1k | } |
1905 | | |
1906 | 1.87k | if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) { |
1907 | 656 | return false; |
1908 | 656 | } |
1909 | | |
1910 | 1.22k | if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) { |
1911 | 402 | return false; |
1912 | 402 | } |
1913 | | |
1914 | 821 | c = &cc->value; |
1915 | | |
1916 | | /* Substitute case-sensitive (or lowercase) persistent class constants */ |
1917 | 821 | if (Z_TYPE_P(c) < IS_ARRAY) { |
1918 | 498 | ZVAL_COPY_OR_DUP(zv, c); |
1919 | 498 | return 1; |
1920 | 498 | } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) { |
1921 | 77 | ZVAL_COPY_OR_DUP(zv, c); |
1922 | 77 | return 1; |
1923 | 77 | } |
1924 | | |
1925 | 246 | return false; |
1926 | 821 | } |
1927 | | /* }}} */ |
1928 | | |
1929 | | static void zend_add_to_list(void *result, void *item) /* {{{ */ |
1930 | 196k | { |
1931 | 196k | void** list = *(void**)result; |
1932 | 196k | size_t n = 0; |
1933 | | |
1934 | 196k | if (list) { |
1935 | 407k | while (list[n]) { |
1936 | 275k | n++; |
1937 | 275k | } |
1938 | 131k | } |
1939 | | |
1940 | 196k | list = erealloc(list, sizeof(void*) * (n+2)); |
1941 | | |
1942 | 196k | list[n] = item; |
1943 | 196k | list[n+1] = NULL; |
1944 | | |
1945 | 196k | *(void**)result = list; |
1946 | 196k | } |
1947 | | /* }}} */ |
1948 | | |
1949 | | static void zend_do_extended_stmt(znode* result) /* {{{ */ |
1950 | 2.20M | { |
1951 | 2.20M | zend_op *opline; |
1952 | | |
1953 | 2.20M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) { |
1954 | 2.20M | return; |
1955 | 2.20M | } |
1956 | | |
1957 | 0 | opline = get_next_op(); |
1958 | |
|
1959 | 0 | opline->opcode = ZEND_EXT_STMT; |
1960 | 0 | if (result) { |
1961 | 0 | if (result->op_type == IS_CONST) { |
1962 | 0 | Z_TRY_ADDREF(result->u.constant); |
1963 | 0 | } |
1964 | 0 | SET_NODE(opline->op1, result); |
1965 | 0 | } |
1966 | 0 | } |
1967 | | /* }}} */ |
1968 | | |
1969 | | static void zend_do_extended_fcall_begin(void) /* {{{ */ |
1970 | 4.87M | { |
1971 | 4.87M | zend_op *opline; |
1972 | | |
1973 | 4.87M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1974 | 4.87M | return; |
1975 | 4.87M | } |
1976 | | |
1977 | 0 | opline = get_next_op(); |
1978 | |
|
1979 | 0 | opline->opcode = ZEND_EXT_FCALL_BEGIN; |
1980 | 0 | } |
1981 | | /* }}} */ |
1982 | | |
1983 | | static void zend_do_extended_fcall_end(void) /* {{{ */ |
1984 | 4.87M | { |
1985 | 4.87M | zend_op *opline; |
1986 | | |
1987 | 4.87M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1988 | 4.87M | return; |
1989 | 4.87M | } |
1990 | | |
1991 | 0 | opline = get_next_op(); |
1992 | |
|
1993 | 0 | opline->opcode = ZEND_EXT_FCALL_END; |
1994 | 0 | } |
1995 | | /* }}} */ |
1996 | | |
1997 | 0 | ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ { |
1998 | 0 | zend_auto_global *auto_global; |
1999 | |
|
2000 | 0 | if ((auto_global = zend_hash_str_find_ptr(CG(auto_globals), name, len)) != NULL) { |
2001 | 0 | if (auto_global->armed) { |
2002 | 0 | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2003 | 0 | } |
2004 | 0 | return 1; |
2005 | 0 | } |
2006 | 0 | return 0; |
2007 | 0 | } |
2008 | | /* }}} */ |
2009 | | |
2010 | | ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */ |
2011 | 3.32M | { |
2012 | 3.32M | zend_auto_global *auto_global; |
2013 | | |
2014 | 3.32M | if ((auto_global = zend_hash_find_ptr(CG(auto_globals), name)) != NULL) { |
2015 | 4.82k | if (auto_global->armed) { |
2016 | 210 | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2017 | 210 | } |
2018 | 4.82k | return 1; |
2019 | 4.82k | } |
2020 | 3.31M | return 0; |
2021 | 3.32M | } |
2022 | | /* }}} */ |
2023 | | |
2024 | | ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */ |
2025 | 128 | { |
2026 | 128 | zend_auto_global auto_global; |
2027 | 128 | zend_result retval; |
2028 | | |
2029 | 128 | auto_global.name = name; |
2030 | 128 | auto_global.auto_global_callback = auto_global_callback; |
2031 | 128 | auto_global.jit = jit; |
2032 | | |
2033 | 128 | retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE; |
2034 | | |
2035 | 128 | return retval; |
2036 | 128 | } |
2037 | | /* }}} */ |
2038 | | |
2039 | | ZEND_API void zend_activate_auto_globals(void) /* {{{ */ |
2040 | 300k | { |
2041 | 300k | zend_auto_global *auto_global; |
2042 | | |
2043 | 5.41M | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2044 | 5.41M | auto_global->armed = auto_global->jit || auto_global->auto_global_callback; |
2045 | 5.41M | } ZEND_HASH_FOREACH_END(); |
2046 | | |
2047 | 5.41M | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2048 | 5.41M | if (auto_global->armed && !auto_global->jit) { |
2049 | 1.20M | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2050 | 1.20M | } |
2051 | 5.41M | } ZEND_HASH_FOREACH_END(); |
2052 | 300k | } |
2053 | | /* }}} */ |
2054 | | |
2055 | | int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */ |
2056 | 10.8M | { |
2057 | 10.8M | zval zv; |
2058 | 10.8M | int ret; |
2059 | | |
2060 | 10.8M | if (CG(increment_lineno)) { |
2061 | 14.9k | CG(zend_lineno)++; |
2062 | 14.9k | CG(increment_lineno) = 0; |
2063 | 14.9k | } |
2064 | | |
2065 | 10.8M | ret = lex_scan(&zv, elem); |
2066 | 10.8M | ZEND_ASSERT(!EG(exception) || ret == T_ERROR); |
2067 | 10.8M | return ret; |
2068 | | |
2069 | 10.8M | } |
2070 | | /* }}} */ |
2071 | | |
2072 | | ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */ |
2073 | 446k | { |
2074 | 446k | bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS; |
2075 | | |
2076 | 446k | ce->refcount = 1; |
2077 | 446k | ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED; |
2078 | 446k | ce->ce_flags2 = 0; |
2079 | | |
2080 | 446k | if (CG(compiler_options) & ZEND_COMPILE_GUARDS) { |
2081 | 0 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2082 | 0 | } |
2083 | | |
2084 | 446k | ce->default_properties_table = NULL; |
2085 | 446k | ce->default_static_members_table = NULL; |
2086 | 446k | zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes); |
2087 | 446k | zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes); |
2088 | 446k | zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes); |
2089 | | |
2090 | 446k | ce->doc_comment = NULL; |
2091 | | |
2092 | 446k | ZEND_MAP_PTR_INIT(ce->static_members_table, NULL); |
2093 | 446k | ZEND_MAP_PTR_INIT(ce->mutable_data, NULL); |
2094 | | |
2095 | 446k | ce->default_object_handlers = &std_object_handlers; |
2096 | 446k | ce->default_properties_count = 0; |
2097 | 446k | ce->default_static_members_count = 0; |
2098 | 446k | ce->properties_info_table = NULL; |
2099 | 446k | ce->attributes = NULL; |
2100 | 446k | ce->enum_backing_type = IS_UNDEF; |
2101 | 446k | ce->backed_enum_table = NULL; |
2102 | | |
2103 | 446k | if (nullify_handlers) { |
2104 | 443k | ce->constructor = NULL; |
2105 | 443k | ce->destructor = NULL; |
2106 | 443k | ce->clone = NULL; |
2107 | 443k | ce->__get = NULL; |
2108 | 443k | ce->__set = NULL; |
2109 | 443k | ce->__unset = NULL; |
2110 | 443k | ce->__isset = NULL; |
2111 | 443k | ce->__call = NULL; |
2112 | 443k | ce->__callstatic = NULL; |
2113 | 443k | ce->__tostring = NULL; |
2114 | 443k | ce->__serialize = NULL; |
2115 | 443k | ce->__unserialize = NULL; |
2116 | 443k | ce->__debugInfo = NULL; |
2117 | 443k | ce->create_object = NULL; |
2118 | 443k | ce->get_iterator = NULL; |
2119 | 443k | ce->iterator_funcs_ptr = NULL; |
2120 | 443k | ce->arrayaccess_funcs_ptr = NULL; |
2121 | 443k | ce->get_static_method = NULL; |
2122 | 443k | ce->parent = NULL; |
2123 | 443k | ce->parent_name = NULL; |
2124 | 443k | ce->num_interfaces = 0; |
2125 | 443k | ce->interfaces = NULL; |
2126 | 443k | ce->num_traits = 0; |
2127 | 443k | ce->num_hooked_props = 0; |
2128 | 443k | ce->num_hooked_prop_variance_checks = 0; |
2129 | 443k | ce->trait_names = NULL; |
2130 | 443k | ce->trait_aliases = NULL; |
2131 | 443k | ce->trait_precedences = NULL; |
2132 | 443k | ce->serialize = NULL; |
2133 | 443k | ce->unserialize = NULL; |
2134 | 443k | if (ce->type == ZEND_INTERNAL_CLASS) { |
2135 | 0 | ce->info.internal.module = NULL; |
2136 | 0 | ce->info.internal.builtin_functions = NULL; |
2137 | 0 | } |
2138 | 443k | } |
2139 | 446k | } |
2140 | | /* }}} */ |
2141 | | |
2142 | | ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */ |
2143 | 0 | { |
2144 | 0 | return op_array->vars[EX_VAR_TO_NUM(var)]; |
2145 | 0 | } |
2146 | | /* }}} */ |
2147 | | |
2148 | | zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */ |
2149 | 0 | { |
2150 | 0 | zval *left_zv = zend_ast_get_zval(left_ast); |
2151 | 0 | zend_string *left = Z_STR_P(left_zv); |
2152 | 0 | zend_string *right = zend_ast_get_str(right_ast); |
2153 | |
|
2154 | 0 | zend_string *result; |
2155 | 0 | size_t left_len = ZSTR_LEN(left); |
2156 | 0 | size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */ |
2157 | |
|
2158 | 0 | result = zend_string_extend(left, len, 0); |
2159 | 0 | ZSTR_VAL(result)[left_len] = '\\'; |
2160 | 0 | memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right)); |
2161 | 0 | ZSTR_VAL(result)[len] = '\0'; |
2162 | 0 | zend_string_release_ex(right, 0); |
2163 | |
|
2164 | 0 | ZVAL_STR(left_zv, result); |
2165 | 0 | return left_ast; |
2166 | 0 | } |
2167 | | /* }}} */ |
2168 | | |
2169 | | zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */ |
2170 | 1.55k | { |
2171 | 1.55k | zval *zv = zend_ast_get_zval(ast); |
2172 | 1.55k | if (Z_TYPE_P(zv) == IS_LONG) { |
2173 | 1.18k | if (Z_LVAL_P(zv) == 0) { |
2174 | 445 | ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0)); |
2175 | 743 | } else { |
2176 | 743 | ZEND_ASSERT(Z_LVAL_P(zv) > 0); |
2177 | 743 | Z_LVAL_P(zv) *= -1; |
2178 | 743 | } |
2179 | 1.18k | } else if (Z_TYPE_P(zv) == IS_STRING) { |
2180 | 370 | size_t orig_len = Z_STRLEN_P(zv); |
2181 | 370 | Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0); |
2182 | 370 | memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1); |
2183 | 370 | Z_STRVAL_P(zv)[0] = '-'; |
2184 | 370 | } else { |
2185 | 0 | ZEND_UNREACHABLE(); |
2186 | 0 | } |
2187 | 1.55k | return ast; |
2188 | 1.55k | } |
2189 | | /* }}} */ |
2190 | | |
2191 | | static void zend_verify_namespace(void) /* {{{ */ |
2192 | 508k | { |
2193 | 508k | if (FC(has_bracketed_namespaces) && !FC(in_namespace)) { |
2194 | 53 | zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}"); |
2195 | 53 | } |
2196 | 508k | } |
2197 | | /* }}} */ |
2198 | | |
2199 | | /* {{{ zend_dirname |
2200 | | Returns directory name component of path */ |
2201 | | ZEND_API size_t zend_dirname(char *path, size_t len) |
2202 | 1.17k | { |
2203 | 1.17k | char *end = path + len - 1; |
2204 | 1.17k | unsigned int len_adjust = 0; |
2205 | | |
2206 | | #ifdef ZEND_WIN32 |
2207 | | /* Note that on Win32 CWD is per drive (heritage from CP/M). |
2208 | | * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive. |
2209 | | */ |
2210 | | if ((2 <= len) && isalpha((unsigned char)path[0]) && (':' == path[1])) { |
2211 | | /* Skip over the drive spec (if any) so as not to change */ |
2212 | | path += 2; |
2213 | | len_adjust += 2; |
2214 | | if (2 == len) { |
2215 | | /* Return "c:" on Win32 for dirname("c:"). |
2216 | | * It would be more consistent to return "c:." |
2217 | | * but that would require making the string *longer*. |
2218 | | */ |
2219 | | return len; |
2220 | | } |
2221 | | } |
2222 | | #endif |
2223 | | |
2224 | 1.17k | if (len == 0) { |
2225 | | /* Illegal use of this function */ |
2226 | 0 | return 0; |
2227 | 0 | } |
2228 | | |
2229 | | /* Strip trailing slashes */ |
2230 | 1.17k | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2231 | 0 | end--; |
2232 | 0 | } |
2233 | 1.17k | if (end < path) { |
2234 | | /* The path only contained slashes */ |
2235 | 0 | path[0] = DEFAULT_SLASH; |
2236 | 0 | path[1] = '\0'; |
2237 | 0 | return 1 + len_adjust; |
2238 | 0 | } |
2239 | | |
2240 | | /* Strip filename */ |
2241 | 12.9k | while (end >= path && !IS_SLASH_P_EX(end, end == path)) { |
2242 | 11.8k | end--; |
2243 | 11.8k | } |
2244 | 1.17k | if (end < path) { |
2245 | | /* No slash found, therefore return '.' */ |
2246 | 595 | path[0] = '.'; |
2247 | 595 | path[1] = '\0'; |
2248 | 595 | return 1 + len_adjust; |
2249 | 595 | } |
2250 | | |
2251 | | /* Strip slashes which came before the file name */ |
2252 | 1.16k | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2253 | 583 | end--; |
2254 | 583 | } |
2255 | 583 | if (end < path) { |
2256 | 5 | path[0] = DEFAULT_SLASH; |
2257 | 5 | path[1] = '\0'; |
2258 | 5 | return 1 + len_adjust; |
2259 | 5 | } |
2260 | 578 | *(end+1) = '\0'; |
2261 | | |
2262 | 578 | return (size_t)(end + 1 - path) + len_adjust; |
2263 | 583 | } |
2264 | | /* }}} */ |
2265 | | |
2266 | | static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */ |
2267 | 1.13M | { |
2268 | 1.13M | uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3; |
2269 | | |
2270 | 1.13M | switch (type) { |
2271 | 490k | case BP_VAR_R: |
2272 | 490k | opline->result_type = IS_TMP_VAR; |
2273 | 490k | result->op_type = IS_TMP_VAR; |
2274 | 490k | return; |
2275 | 278k | case BP_VAR_W: |
2276 | 278k | opline->opcode += 1 * factor; |
2277 | 278k | return; |
2278 | 14.8k | case BP_VAR_RW: |
2279 | 14.8k | opline->opcode += 2 * factor; |
2280 | 14.8k | return; |
2281 | 243k | case BP_VAR_IS: |
2282 | 243k | opline->result_type = IS_TMP_VAR; |
2283 | 243k | result->op_type = IS_TMP_VAR; |
2284 | 243k | opline->opcode += 3 * factor; |
2285 | 243k | return; |
2286 | 102k | case BP_VAR_FUNC_ARG: |
2287 | 102k | opline->opcode += 4 * factor; |
2288 | 102k | return; |
2289 | 5.34k | case BP_VAR_UNSET: |
2290 | 5.34k | opline->opcode += 5 * factor; |
2291 | 5.34k | return; |
2292 | 0 | default: ZEND_UNREACHABLE(); |
2293 | 1.13M | } |
2294 | 1.13M | } |
2295 | | /* }}} */ |
2296 | | |
2297 | | static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */ |
2298 | 6.42M | { |
2299 | 6.42M | opline->result_type = IS_VAR; |
2300 | 6.42M | opline->result.var = get_temporary_variable(); |
2301 | 6.42M | GET_NODE(result, opline->result); |
2302 | 6.42M | } |
2303 | | /* }}} */ |
2304 | | |
2305 | | static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */ |
2306 | 38.1M | { |
2307 | 38.1M | opline->result_type = IS_TMP_VAR; |
2308 | 38.1M | opline->result.var = get_temporary_variable(); |
2309 | 38.1M | GET_NODE(result, opline->result); |
2310 | 38.1M | } |
2311 | | /* }}} */ |
2312 | | |
2313 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2314 | 42.0M | { |
2315 | 42.0M | zend_op *opline = get_next_op(); |
2316 | 42.0M | opline->opcode = opcode; |
2317 | | |
2318 | 42.0M | if (op1 != NULL) { |
2319 | 33.5M | SET_NODE(opline->op1, op1); |
2320 | 33.5M | } |
2321 | | |
2322 | 42.0M | if (op2 != NULL) { |
2323 | 1.94M | SET_NODE(opline->op2, op2); |
2324 | 1.94M | } |
2325 | | |
2326 | 42.0M | if (result) { |
2327 | 5.46M | zend_make_var_result(result, opline); |
2328 | 5.46M | } |
2329 | 42.0M | return opline; |
2330 | 42.0M | } |
2331 | | /* }}} */ |
2332 | | |
2333 | | static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2334 | 40.8M | { |
2335 | 40.8M | zend_op *opline = get_next_op(); |
2336 | 40.8M | opline->opcode = opcode; |
2337 | | |
2338 | 40.8M | if (op1 != NULL) { |
2339 | 10.8M | SET_NODE(opline->op1, op1); |
2340 | 10.8M | } |
2341 | | |
2342 | 40.8M | if (op2 != NULL) { |
2343 | 4.77M | SET_NODE(opline->op2, op2); |
2344 | 4.77M | } |
2345 | | |
2346 | 40.8M | if (result) { |
2347 | 38.0M | zend_make_tmp_result(result, opline); |
2348 | 38.0M | } |
2349 | | |
2350 | 40.8M | return opline; |
2351 | 40.8M | } |
2352 | | /* }}} */ |
2353 | | |
2354 | | static void zend_emit_tick(void) /* {{{ */ |
2355 | 23.7k | { |
2356 | 23.7k | zend_op *opline; |
2357 | | |
2358 | | /* This prevents a double TICK generated by the parser statement of "declare()" */ |
2359 | 23.7k | if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) { |
2360 | 2.42k | return; |
2361 | 2.42k | } |
2362 | | |
2363 | 21.3k | opline = get_next_op(); |
2364 | | |
2365 | 21.3k | opline->opcode = ZEND_TICKS; |
2366 | 21.3k | opline->extended_value = FC(declarables).ticks; |
2367 | 21.3k | } |
2368 | | /* }}} */ |
2369 | | |
2370 | | static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */ |
2371 | 337k | { |
2372 | 337k | return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL); |
2373 | 337k | } |
2374 | | /* }}} */ |
2375 | | |
2376 | | static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */ |
2377 | 965k | { |
2378 | 965k | uint32_t opnum = get_next_op_number(); |
2379 | 965k | zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
2380 | 965k | opline->op1.opline_num = opnum_target; |
2381 | 965k | return opnum; |
2382 | 965k | } |
2383 | | /* }}} */ |
2384 | | |
2385 | | ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */ |
2386 | 107k | { |
2387 | 107k | switch (opline->opcode) { |
2388 | 4.08k | case ZEND_IS_IDENTICAL: |
2389 | 4.71k | case ZEND_IS_NOT_IDENTICAL: |
2390 | 22.0k | case ZEND_IS_EQUAL: |
2391 | 23.5k | case ZEND_IS_NOT_EQUAL: |
2392 | 49.1k | case ZEND_IS_SMALLER: |
2393 | 51.8k | case ZEND_IS_SMALLER_OR_EQUAL: |
2394 | 55.8k | case ZEND_CASE: |
2395 | 56.6k | case ZEND_CASE_STRICT: |
2396 | 56.8k | case ZEND_ISSET_ISEMPTY_CV: |
2397 | 56.9k | case ZEND_ISSET_ISEMPTY_VAR: |
2398 | 57.5k | case ZEND_ISSET_ISEMPTY_DIM_OBJ: |
2399 | 57.7k | case ZEND_ISSET_ISEMPTY_PROP_OBJ: |
2400 | 57.8k | case ZEND_ISSET_ISEMPTY_STATIC_PROP: |
2401 | 57.9k | case ZEND_INSTANCEOF: |
2402 | 58.6k | case ZEND_TYPE_CHECK: |
2403 | 58.7k | case ZEND_DEFINED: |
2404 | 58.7k | case ZEND_IN_ARRAY: |
2405 | 58.8k | case ZEND_ARRAY_KEY_EXISTS: |
2406 | 58.8k | return 1; |
2407 | 49.1k | default: |
2408 | 49.1k | return 0; |
2409 | 107k | } |
2410 | 107k | } |
2411 | | /* }}} */ |
2412 | | |
2413 | | static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */ |
2414 | 108k | { |
2415 | 108k | uint32_t opnum = get_next_op_number(); |
2416 | 108k | zend_op *opline; |
2417 | | |
2418 | 108k | if (cond->op_type == IS_TMP_VAR && opnum > 0) { |
2419 | 85.2k | opline = CG(active_op_array)->opcodes + opnum - 1; |
2420 | 85.2k | if (opline->result_type == IS_TMP_VAR |
2421 | 70.0k | && opline->result.var == cond->u.op.var |
2422 | 70.0k | && zend_is_smart_branch(opline)) { |
2423 | 58.6k | if (opcode == ZEND_JMPZ) { |
2424 | 36.7k | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ; |
2425 | 36.7k | } else { |
2426 | 21.9k | ZEND_ASSERT(opcode == ZEND_JMPNZ); |
2427 | 21.9k | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ; |
2428 | 21.9k | } |
2429 | 58.6k | } |
2430 | 85.2k | } |
2431 | 108k | opline = zend_emit_op(NULL, opcode, cond, NULL); |
2432 | 108k | opline->op2.opline_num = opnum_target; |
2433 | 108k | return opnum; |
2434 | 108k | } |
2435 | | /* }}} */ |
2436 | | |
2437 | | static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */ |
2438 | 1.19M | { |
2439 | 1.19M | zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump]; |
2440 | 1.19M | switch (opline->opcode) { |
2441 | 949k | case ZEND_JMP: |
2442 | 949k | opline->op1.opline_num = opnum_target; |
2443 | 949k | break; |
2444 | 74.5k | case ZEND_JMPZ: |
2445 | 91.4k | case ZEND_JMPNZ: |
2446 | 97.7k | case ZEND_JMPZ_EX: |
2447 | 103k | case ZEND_JMPNZ_EX: |
2448 | 108k | case ZEND_JMP_SET: |
2449 | 240k | case ZEND_COALESCE: |
2450 | 240k | case ZEND_JMP_NULL: |
2451 | 241k | case ZEND_BIND_INIT_STATIC_OR_JMP: |
2452 | 241k | case ZEND_JMP_FRAMELESS: |
2453 | 241k | opline->op2.opline_num = opnum_target; |
2454 | 241k | break; |
2455 | 0 | default: ZEND_UNREACHABLE(); |
2456 | 1.19M | } |
2457 | 1.19M | } |
2458 | | /* }}} */ |
2459 | | |
2460 | | static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */ |
2461 | 1.18M | { |
2462 | 1.18M | zend_update_jump_target(opnum_jump, get_next_op_number()); |
2463 | 1.18M | } |
2464 | | /* }}} */ |
2465 | | |
2466 | | static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2467 | 820k | { |
2468 | 820k | zend_op tmp_opline; |
2469 | | |
2470 | 820k | init_op(&tmp_opline); |
2471 | | |
2472 | 820k | tmp_opline.opcode = opcode; |
2473 | 820k | if (op1 != NULL) { |
2474 | 820k | SET_NODE(tmp_opline.op1, op1); |
2475 | 820k | } |
2476 | 820k | if (op2 != NULL) { |
2477 | 800k | SET_NODE(tmp_opline.op2, op2); |
2478 | 800k | } |
2479 | 820k | if (result) { |
2480 | 815k | zend_make_var_result(result, &tmp_opline); |
2481 | 815k | } |
2482 | | |
2483 | 820k | zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline); |
2484 | 820k | return zend_stack_top(&CG(delayed_oplines_stack)); |
2485 | 820k | } |
2486 | | /* }}} */ |
2487 | | |
2488 | | static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */ |
2489 | 1.03M | { |
2490 | 1.03M | return zend_stack_count(&CG(delayed_oplines_stack)); |
2491 | 1.03M | } |
2492 | | /* }}} */ |
2493 | | |
2494 | | static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */ |
2495 | 1.03M | { |
2496 | 1.03M | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
2497 | 1.03M | uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack)); |
2498 | | |
2499 | 1.03M | ZEND_ASSERT(count >= offset); |
2500 | 1.85M | for (i = offset; i < count; ++i) { |
2501 | 819k | if (EXPECTED(oplines[i].opcode != ZEND_NOP)) { |
2502 | 800k | opline = get_next_op(); |
2503 | 800k | memcpy(opline, &oplines[i], sizeof(zend_op)); |
2504 | 800k | } else { |
2505 | 19.9k | opline = CG(active_op_array)->opcodes + oplines[i].extended_value; |
2506 | 19.9k | } |
2507 | 819k | } |
2508 | | |
2509 | 1.03M | CG(delayed_oplines_stack).top = offset; |
2510 | 1.03M | return opline; |
2511 | 1.03M | } |
2512 | | /* }}} */ |
2513 | | |
2514 | | static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind) |
2515 | 59.4M | { |
2516 | 59.4M | switch (ast_kind) { |
2517 | 637k | case ZEND_AST_DIM: |
2518 | 742k | case ZEND_AST_PROP: |
2519 | 895k | case ZEND_AST_NULLSAFE_PROP: |
2520 | 913k | case ZEND_AST_STATIC_PROP: |
2521 | 1.67M | case ZEND_AST_METHOD_CALL: |
2522 | 1.67M | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2523 | 1.71M | case ZEND_AST_STATIC_CALL: |
2524 | 1.71M | return true; |
2525 | 57.7M | default: |
2526 | 57.7M | return false; |
2527 | 59.4M | } |
2528 | 59.4M | } |
2529 | | |
2530 | | static bool zend_ast_is_short_circuited(const zend_ast *ast) |
2531 | 1.83M | { |
2532 | 1.83M | switch (ast->kind) { |
2533 | 353k | case ZEND_AST_DIM: |
2534 | 394k | case ZEND_AST_PROP: |
2535 | 404k | case ZEND_AST_STATIC_PROP: |
2536 | 524k | case ZEND_AST_METHOD_CALL: |
2537 | 525k | case ZEND_AST_STATIC_CALL: |
2538 | 525k | return zend_ast_is_short_circuited(ast->child[0]); |
2539 | 571 | case ZEND_AST_NULLSAFE_PROP: |
2540 | 677 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2541 | 677 | return true; |
2542 | 1.30M | default: |
2543 | 1.30M | return false; |
2544 | 1.83M | } |
2545 | 1.83M | } |
2546 | | |
2547 | | static void zend_assert_not_short_circuited(const zend_ast *ast) |
2548 | 392k | { |
2549 | 392k | if (zend_ast_is_short_circuited(ast)) { |
2550 | 36 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain"); |
2551 | 36 | } |
2552 | 392k | } |
2553 | | |
2554 | | /* Mark nodes that are an inner part of a short-circuiting chain. |
2555 | | * We should not perform a "commit" on them, as it will be performed by the outer-most node. |
2556 | | * We do this to avoid passing down an argument in various compile functions. */ |
2557 | | |
2558 | 1.72M | #define ZEND_SHORT_CIRCUITING_INNER 0x8000 |
2559 | | |
2560 | 1.03M | static void zend_short_circuiting_mark_inner(zend_ast *ast) { |
2561 | 1.03M | if (zend_ast_kind_is_short_circuited(ast->kind)) { |
2562 | 543k | ast->attr |= ZEND_SHORT_CIRCUITING_INNER; |
2563 | 543k | } |
2564 | 1.03M | } |
2565 | | |
2566 | | static uint32_t zend_short_circuiting_checkpoint(void) |
2567 | 58.6M | { |
2568 | 58.6M | return zend_stack_count(&CG(short_circuiting_opnums)); |
2569 | 58.6M | } |
2570 | | |
2571 | | static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast) |
2572 | 58.4M | { |
2573 | 58.4M | bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind) |
2574 | 57.2M | || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY; |
2575 | 58.4M | if (!is_short_circuited) { |
2576 | 57.2M | ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint |
2577 | 57.2M | && "Short circuiting stack should be empty"); |
2578 | 57.2M | return; |
2579 | 57.2M | } |
2580 | | |
2581 | 1.17M | if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) { |
2582 | | /* Outer-most node will commit. */ |
2583 | 381k | return; |
2584 | 381k | } |
2585 | | |
2586 | 882k | while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) { |
2587 | 86.8k | uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums)); |
2588 | 86.8k | zend_op *opline = &CG(active_op_array)->opcodes[opnum]; |
2589 | 86.8k | opline->op2.opline_num = get_next_op_number(); |
2590 | 86.8k | SET_NODE(opline->result, result); |
2591 | 86.8k | opline->extended_value |= |
2592 | 86.8k | ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET : |
2593 | 86.8k | ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY : |
2594 | 86.3k | ZEND_SHORT_CIRCUITING_CHAIN_EXPR; |
2595 | 86.8k | zend_stack_del_top(&CG(short_circuiting_opnums)); |
2596 | 86.8k | } |
2597 | 796k | } |
2598 | | |
2599 | | static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type) |
2600 | 86.8k | { |
2601 | 86.8k | uint32_t jmp_null_opnum = get_next_op_number(); |
2602 | 86.8k | zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL); |
2603 | 86.8k | if (opline->op1_type == IS_CONST) { |
2604 | 969 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
2605 | 969 | } |
2606 | 86.8k | if (bp_type == BP_VAR_IS) { |
2607 | 9.92k | opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS; |
2608 | 9.92k | } |
2609 | 86.8k | zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum); |
2610 | 86.8k | } |
2611 | | |
2612 | | static inline bool zend_is_variable_or_call(const zend_ast *ast); |
2613 | | |
2614 | | static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */ |
2615 | 690k | { |
2616 | 690k | const zend_memoize_mode memoize_mode = CG(memoize_mode); |
2617 | 690k | if (memoize_mode == ZEND_MEMOIZE_COMPILE) { |
2618 | 346k | znode memoized_result; |
2619 | | |
2620 | | /* Go through normal compilation */ |
2621 | 346k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
2622 | 346k | if (zend_is_variable_or_call(expr)) { |
2623 | 127k | zend_compile_var(result, expr, type, /* by_ref */ false); |
2624 | 218k | } else { |
2625 | 218k | zend_compile_expr(result, expr); |
2626 | 218k | } |
2627 | 346k | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
2628 | | |
2629 | 346k | if (result->op_type == IS_VAR) { |
2630 | 123k | zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2631 | 222k | } else if (result->op_type == IS_TMP_VAR) { |
2632 | 213k | zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2633 | 213k | } else { |
2634 | 9.18k | if (result->op_type == IS_CONST) { |
2635 | 7.22k | Z_TRY_ADDREF(result->u.constant); |
2636 | 7.22k | } |
2637 | 9.18k | memoized_result = *result; |
2638 | 9.18k | } |
2639 | | |
2640 | 346k | zend_hash_index_update_mem( |
2641 | 346k | CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode)); |
2642 | 346k | } else if (memoize_mode == ZEND_MEMOIZE_FETCH) { |
2643 | 344k | const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr); |
2644 | 344k | *result = *memoized_result; |
2645 | 344k | if (result->op_type == IS_CONST) { |
2646 | 6.98k | Z_TRY_ADDREF(result->u.constant); |
2647 | 6.98k | } |
2648 | 344k | } else { |
2649 | 0 | ZEND_UNREACHABLE(); |
2650 | 0 | } |
2651 | 690k | } |
2652 | | /* }}} */ |
2653 | | |
2654 | | static void zend_emit_return_type_check( |
2655 | | znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */ |
2656 | 128k | { |
2657 | 128k | zend_type type = return_info->type; |
2658 | 128k | if (ZEND_TYPE_IS_SET(type)) { |
2659 | 128k | zend_op *opline; |
2660 | | |
2661 | | /* `return ...;` is illegal in a void function (but `return;` isn't) */ |
2662 | 128k | if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) { |
2663 | 6.56k | if (expr) { |
2664 | 17 | if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) { |
2665 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
2666 | 7 | "A void %s must not return a value " |
2667 | 7 | "(did you mean \"return;\" instead of \"return null;\"?)", |
2668 | 7 | CG(active_class_entry) != NULL ? "method" : "function"); |
2669 | 10 | } else { |
2670 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value", |
2671 | 10 | CG(active_class_entry) != NULL ? "method" : "function"); |
2672 | 10 | } |
2673 | 17 | } |
2674 | | /* we don't need run-time check */ |
2675 | 6.54k | return; |
2676 | 6.56k | } |
2677 | | |
2678 | | /* `return` is illegal in a never-returning function */ |
2679 | 122k | if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) { |
2680 | | /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */ |
2681 | 9 | ZEND_ASSERT(!implicit); |
2682 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return", |
2683 | 9 | CG(active_class_entry) != NULL ? "method" : "function"); |
2684 | 9 | } |
2685 | | |
2686 | 122k | if (!expr && !implicit) { |
2687 | 13 | if (ZEND_TYPE_ALLOW_NULL(type)) { |
2688 | 8 | zend_error_noreturn(E_COMPILE_ERROR, |
2689 | 8 | "A %s with return type must return a value " |
2690 | 8 | "(did you mean \"return null;\" instead of \"return;\"?)", |
2691 | 8 | CG(active_class_entry) != NULL ? "method" : "function"); |
2692 | 8 | } else { |
2693 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
2694 | 5 | "A %s with return type must return a value", |
2695 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
2696 | 5 | } |
2697 | 13 | } |
2698 | | |
2699 | 122k | if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) { |
2700 | | /* we don't need run-time check for mixed return type */ |
2701 | 644 | return; |
2702 | 644 | } |
2703 | | |
2704 | 121k | if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) { |
2705 | | /* we don't need run-time check */ |
2706 | 2.16k | return; |
2707 | 2.16k | } |
2708 | | |
2709 | 119k | opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL); |
2710 | 119k | if (expr && expr->op_type == IS_CONST) { |
2711 | 444 | opline->result_type = expr->op_type = IS_TMP_VAR; |
2712 | 444 | opline->result.var = expr->u.op.var = get_temporary_variable(); |
2713 | 444 | } |
2714 | 119k | } |
2715 | 128k | } |
2716 | | /* }}} */ |
2717 | | |
2718 | | void zend_emit_final_return(bool return_one) /* {{{ */ |
2719 | 2.24M | { |
2720 | 2.24M | znode zn; |
2721 | 2.24M | zend_op *ret; |
2722 | 2.24M | bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
2723 | | |
2724 | 2.24M | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) |
2725 | 120k | && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) { |
2726 | 120k | zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
2727 | | |
2728 | 120k | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
2729 | 474 | zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL); |
2730 | 474 | return; |
2731 | 474 | } |
2732 | | |
2733 | 119k | zend_emit_return_type_check(NULL, return_info, true); |
2734 | 119k | } |
2735 | | |
2736 | 2.24M | zn.op_type = IS_CONST; |
2737 | 2.24M | if (return_one) { |
2738 | 83.7k | ZVAL_LONG(&zn.u.constant, 1); |
2739 | 2.16M | } else { |
2740 | 2.16M | ZVAL_NULL(&zn.u.constant); |
2741 | 2.16M | } |
2742 | | |
2743 | 2.24M | ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL); |
2744 | 2.24M | ret->extended_value = -1; |
2745 | 2.24M | } |
2746 | | /* }}} */ |
2747 | | |
2748 | | static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */ |
2749 | 6.51M | { |
2750 | 6.51M | return ast->kind == ZEND_AST_VAR |
2751 | 6.36M | || ast->kind == ZEND_AST_DIM |
2752 | 6.29M | || ast->kind == ZEND_AST_PROP |
2753 | 6.28M | || ast->kind == ZEND_AST_NULLSAFE_PROP |
2754 | 6.28M | || ast->kind == ZEND_AST_STATIC_PROP; |
2755 | 6.51M | } |
2756 | | /* }}} */ |
2757 | | |
2758 | | static bool zend_propagate_list_refs(zend_ast *ast); |
2759 | | |
2760 | | static inline bool zend_is_passable_by_ref(const zend_ast *ast) |
2761 | 5.71M | { |
2762 | 5.71M | if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) { |
2763 | 165k | return true; |
2764 | 165k | } |
2765 | 5.55M | if (ast->kind == ZEND_AST_ASSIGN |
2766 | 4.99k | && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY) |
2767 | 44 | && zend_propagate_list_refs(ast->child[0])) { |
2768 | 21 | return true; |
2769 | 21 | } |
2770 | 5.55M | return false; |
2771 | 5.55M | } |
2772 | | |
2773 | | static inline bool zend_is_call(const zend_ast *ast) /* {{{ */ |
2774 | 7.33M | { |
2775 | 7.33M | return ast->kind == ZEND_AST_CALL |
2776 | 6.35M | || ast->kind == ZEND_AST_METHOD_CALL |
2777 | 6.11M | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
2778 | 6.11M | || ast->kind == ZEND_AST_STATIC_CALL |
2779 | 6.10M | || ast->kind == ZEND_AST_PIPE; |
2780 | 7.33M | } |
2781 | | /* }}} */ |
2782 | | |
2783 | | static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */ |
2784 | 751k | { |
2785 | 751k | return zend_is_variable(ast) || zend_is_call(ast); |
2786 | 751k | } |
2787 | | /* }}} */ |
2788 | | |
2789 | | static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */ |
2790 | 30.4k | { |
2791 | 30.4k | return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL |
2792 | 24.5k | || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP |
2793 | 24.1k | || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD; |
2794 | 30.4k | } |
2795 | | /* }}} */ |
2796 | | |
2797 | | static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */ |
2798 | 22.0k | { |
2799 | 22.0k | while ( |
2800 | 25.0k | ast->kind == ZEND_AST_DIM |
2801 | 23.8k | || ast->kind == ZEND_AST_PROP |
2802 | 22.0k | ) { |
2803 | 2.97k | ast = ast->child[0]; |
2804 | 2.97k | } |
2805 | | |
2806 | 22.0k | return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast); |
2807 | 22.0k | } |
2808 | | /* }}} */ |
2809 | | |
2810 | | static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */ |
2811 | 42.5k | { |
2812 | 42.5k | if (name_ast->kind != ZEND_AST_ZVAL) { |
2813 | 0 | return false; |
2814 | 0 | } |
2815 | | |
2816 | 42.5k | return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast); |
2817 | 42.5k | } |
2818 | | /* }}} */ |
2819 | | |
2820 | | static inline void zend_handle_numeric_op(znode *node) /* {{{ */ |
2821 | 12.0k | { |
2822 | 12.0k | if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) { |
2823 | 10.2k | zend_ulong index; |
2824 | | |
2825 | 10.2k | if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) { |
2826 | 3.04k | zval_ptr_dtor(&node->u.constant); |
2827 | 3.04k | ZVAL_LONG(&node->u.constant, index); |
2828 | 3.04k | } |
2829 | 10.2k | } |
2830 | 12.0k | } |
2831 | | /* }}} */ |
2832 | | |
2833 | | static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */ |
2834 | 164k | { |
2835 | 164k | if (Z_TYPE(dim_node->u.constant) == IS_STRING) { |
2836 | 132k | zend_ulong index; |
2837 | | |
2838 | 132k | if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) { |
2839 | | /* For numeric indexes we also keep the original value to use by ArrayAccess |
2840 | | * See bug #63217 |
2841 | | */ |
2842 | 14.7k | int c = zend_add_literal(&dim_node->u.constant); |
2843 | 14.7k | ZEND_ASSERT(opline->op2.constant + 1 == c); |
2844 | 14.7k | ZVAL_LONG(CT_CONSTANT(opline->op2), index); |
2845 | 14.7k | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE; |
2846 | 14.7k | return; |
2847 | 14.7k | } |
2848 | 132k | } |
2849 | 164k | } |
2850 | | /* }}} */ |
2851 | | |
2852 | | static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */ |
2853 | 348k | { |
2854 | 348k | if (class_node->op_type == IS_CONST) { |
2855 | 109k | opline->op1_type = IS_CONST; |
2856 | 109k | opline->op1.constant = zend_add_class_name_literal( |
2857 | 109k | Z_STR(class_node->u.constant)); |
2858 | 238k | } else { |
2859 | 238k | SET_NODE(opline->op1, class_node); |
2860 | 238k | } |
2861 | 348k | } |
2862 | | /* }}} */ |
2863 | | |
2864 | | static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */ |
2865 | 221k | { |
2866 | 221k | uint32_t fetch_type; |
2867 | | |
2868 | 221k | if (name_ast->kind != ZEND_AST_ZVAL) { |
2869 | 95.6k | znode name_node; |
2870 | | |
2871 | 95.6k | zend_compile_expr(&name_node, name_ast); |
2872 | | |
2873 | 95.6k | if (name_node.op_type == IS_CONST) { |
2874 | 960 | zend_string *name; |
2875 | | |
2876 | 960 | if (Z_TYPE(name_node.u.constant) != IS_STRING) { |
2877 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
2878 | 6 | } |
2879 | | |
2880 | 954 | name = Z_STR(name_node.u.constant); |
2881 | 954 | fetch_type = zend_get_class_fetch_type(name); |
2882 | | |
2883 | 954 | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
2884 | 611 | result->op_type = IS_CONST; |
2885 | 611 | ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ)); |
2886 | 611 | } else { |
2887 | 343 | zend_ensure_valid_class_fetch_type(fetch_type); |
2888 | 343 | result->op_type = IS_UNUSED; |
2889 | 343 | result->u.op.num = fetch_type | fetch_flags; |
2890 | 343 | } |
2891 | | |
2892 | 954 | zend_string_release_ex(name, 0); |
2893 | 94.6k | } else { |
2894 | 94.6k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node); |
2895 | 94.6k | opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags; |
2896 | 94.6k | } |
2897 | 95.6k | return; |
2898 | 95.6k | } |
2899 | | |
2900 | | /* Fully qualified names are always default refs */ |
2901 | 126k | if (name_ast->attr == ZEND_NAME_FQ) { |
2902 | 7.83k | result->op_type = IS_CONST; |
2903 | 7.83k | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2904 | 7.83k | return; |
2905 | 7.83k | } |
2906 | | |
2907 | 118k | fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
2908 | 118k | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
2909 | 112k | result->op_type = IS_CONST; |
2910 | 112k | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2911 | 112k | } else { |
2912 | 6.10k | zend_ensure_valid_class_fetch_type(fetch_type); |
2913 | 6.10k | result->op_type = IS_UNUSED; |
2914 | 6.10k | result->u.op.num = fetch_type | fetch_flags; |
2915 | 6.10k | } |
2916 | 118k | } |
2917 | | /* }}} */ |
2918 | | |
2919 | | static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
2920 | 1.50M | { |
2921 | 1.50M | zend_ast *name_ast = ast->child[0]; |
2922 | 1.50M | if (name_ast->kind == ZEND_AST_ZVAL) { |
2923 | 1.38M | zval *zv = zend_ast_get_zval(name_ast); |
2924 | 1.38M | zend_string *name; |
2925 | | |
2926 | 1.38M | if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) { |
2927 | 1.37M | name = zval_make_interned_string(zv); |
2928 | 1.37M | } else { |
2929 | 10.6k | name = zend_new_interned_string(zval_get_string_func(zv)); |
2930 | 10.6k | } |
2931 | | |
2932 | 1.38M | if (zend_is_auto_global(name)) { |
2933 | 923 | return FAILURE; |
2934 | 923 | } |
2935 | | |
2936 | 1.38M | if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) { |
2937 | 370 | if (type == BP_VAR_R) { |
2938 | 54 | zend_error(E_DEPRECATED, |
2939 | 54 | "The predefined locally scoped $http_response_header variable is deprecated," |
2940 | 54 | " call http_get_last_response_headers() instead"); |
2941 | 316 | } else if (type == BP_VAR_W) { |
2942 | 15 | CG(context).has_assigned_to_http_response_header = true; |
2943 | 15 | } |
2944 | 370 | } |
2945 | | |
2946 | 1.38M | result->op_type = IS_CV; |
2947 | 1.38M | result->u.op.var = lookup_cv(name); |
2948 | | |
2949 | 1.38M | if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) { |
2950 | 10.6k | zend_string_release_ex(name, 0); |
2951 | 10.6k | } |
2952 | | |
2953 | 1.38M | return SUCCESS; |
2954 | 1.38M | } |
2955 | | |
2956 | 111k | return FAILURE; |
2957 | 1.50M | } |
2958 | | /* }}} */ |
2959 | | |
2960 | | static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
2961 | 315k | { |
2962 | 315k | zend_ast *name_ast = ast->child[0]; |
2963 | 315k | znode name_node; |
2964 | 315k | zend_op *opline; |
2965 | | |
2966 | 315k | zend_compile_expr(&name_node, name_ast); |
2967 | 315k | if (name_node.op_type == IS_CONST) { |
2968 | 206k | convert_to_string(&name_node.u.constant); |
2969 | 206k | } |
2970 | | |
2971 | 315k | if (delayed) { |
2972 | 10.7k | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2973 | 304k | } else { |
2974 | 304k | opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2975 | 304k | } |
2976 | | |
2977 | 315k | if (name_node.op_type == IS_CONST && |
2978 | 206k | zend_is_auto_global(Z_STR(name_node.u.constant))) { |
2979 | | |
2980 | 895 | opline->extended_value = ZEND_FETCH_GLOBAL; |
2981 | 314k | } else { |
2982 | 314k | if (name_node.op_type == IS_CONST |
2983 | 205k | && type == BP_VAR_R |
2984 | 205k | && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) { |
2985 | 238 | zend_error(E_DEPRECATED, |
2986 | 238 | "The predefined locally scoped $http_response_header variable is deprecated," |
2987 | 238 | " call http_get_last_response_headers() instead"); |
2988 | 238 | } |
2989 | 314k | opline->extended_value = ZEND_FETCH_LOCAL; |
2990 | 314k | } |
2991 | | |
2992 | 315k | zend_adjust_for_fetch_type(opline, result, type); |
2993 | 315k | return opline; |
2994 | 315k | } |
2995 | | /* }}} */ |
2996 | | |
2997 | | static bool is_this_fetch(const zend_ast *ast) /* {{{ */ |
2998 | 2.47M | { |
2999 | 2.47M | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
3000 | 1.99M | const zval *name = zend_ast_get_zval(ast->child[0]); |
3001 | 1.99M | return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS)); |
3002 | 1.99M | } |
3003 | | |
3004 | 483k | return false; |
3005 | 2.47M | } |
3006 | | /* }}} */ |
3007 | | |
3008 | | static bool is_globals_fetch(const zend_ast *ast) |
3009 | 8.92M | { |
3010 | 8.92M | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
3011 | 2.09M | const zval *name = zend_ast_get_zval(ast->child[0]); |
3012 | 2.09M | return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS"); |
3013 | 2.09M | } |
3014 | | |
3015 | 6.82M | return false; |
3016 | 8.92M | } |
3017 | | |
3018 | | static bool is_global_var_fetch(const zend_ast *ast) |
3019 | 699k | { |
3020 | 699k | return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]); |
3021 | 699k | } |
3022 | | |
3023 | | static bool this_guaranteed_exists(void) /* {{{ */ |
3024 | 20.2k | { |
3025 | 20.2k | const zend_oparray_context *ctx = &CG(context); |
3026 | 61.1k | while (ctx) { |
3027 | | /* Instance methods always have a $this. |
3028 | | * This also includes closures that have a scope and use $this. */ |
3029 | 61.1k | const zend_op_array *op_array = ctx->op_array; |
3030 | 61.1k | if (op_array->fn_flags & ZEND_ACC_STATIC) { |
3031 | 46 | return false; |
3032 | 61.0k | } else if (op_array->scope) { |
3033 | 13.7k | return true; |
3034 | 47.2k | } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
3035 | 6.41k | return false; |
3036 | 6.41k | } |
3037 | 40.8k | ctx = ctx->prev; |
3038 | 40.8k | } |
3039 | 0 | return false; |
3040 | 20.2k | } |
3041 | | /* }}} */ |
3042 | | |
3043 | | static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
3044 | 1.46M | { |
3045 | 1.46M | if (is_this_fetch(ast)) { |
3046 | 5.75k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL); |
3047 | 5.75k | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3048 | 5.59k | opline->result_type = IS_TMP_VAR; |
3049 | 5.59k | result->op_type = IS_TMP_VAR; |
3050 | 5.59k | } |
3051 | 5.75k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3052 | 5.75k | return opline; |
3053 | 1.45M | } else if (is_globals_fetch(ast)) { |
3054 | 2.50k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL); |
3055 | 2.50k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3056 | 2.29k | opline->result_type = IS_TMP_VAR; |
3057 | 2.29k | result->op_type = IS_TMP_VAR; |
3058 | 2.29k | } |
3059 | 2.50k | return opline; |
3060 | 1.45M | } else if (zend_try_compile_cv(result, ast, type) == FAILURE) { |
3061 | 111k | return zend_compile_simple_var_no_cv(result, ast, type, delayed); |
3062 | 111k | } |
3063 | 1.34M | return NULL; |
3064 | 1.46M | } |
3065 | | /* }}} */ |
3066 | | |
3067 | | static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */ |
3068 | 783k | { |
3069 | 783k | if (type != BP_VAR_R |
3070 | 611k | && type != BP_VAR_IS |
3071 | | /* Whether a FUNC_ARG is R may only be determined at runtime. */ |
3072 | 374k | && type != BP_VAR_FUNC_ARG |
3073 | 273k | && zend_is_call(ast)) { |
3074 | 124k | if (node->op_type == IS_VAR) { |
3075 | 124k | zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL); |
3076 | 124k | opline->result_type = IS_VAR; |
3077 | 124k | opline->result.var = opline->op1.var; |
3078 | 124k | } else { |
3079 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3080 | 14 | } |
3081 | 124k | } |
3082 | 783k | } |
3083 | | /* }}} */ |
3084 | | |
3085 | | static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3086 | 6.30k | { |
3087 | 6.30k | znode dummy_node; |
3088 | 6.30k | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast, |
3089 | 6.30k | zend_ast_create_znode(value_node)); |
3090 | 6.30k | zend_compile_expr(&dummy_node, assign_ast); |
3091 | 6.30k | zend_do_free(&dummy_node); |
3092 | 6.30k | } |
3093 | | /* }}} */ |
3094 | | |
3095 | | static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
3096 | 628k | { |
3097 | 628k | zend_ast *var_ast = ast->child[0]; |
3098 | 628k | zend_ast *dim_ast = ast->child[1]; |
3099 | 628k | zend_op *opline; |
3100 | | |
3101 | 628k | znode var_node, dim_node; |
3102 | | |
3103 | 628k | if (is_globals_fetch(var_ast)) { |
3104 | 2.69k | if (dim_ast == NULL) { |
3105 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS"); |
3106 | 8 | } |
3107 | | |
3108 | 2.68k | zend_compile_expr(&dim_node, dim_ast); |
3109 | 2.68k | if (dim_node.op_type == IS_CONST) { |
3110 | 2.46k | convert_to_string(&dim_node.u.constant); |
3111 | 2.46k | } |
3112 | | |
3113 | 2.68k | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL); |
3114 | 2.68k | opline->extended_value = ZEND_FETCH_GLOBAL; |
3115 | 2.68k | zend_adjust_for_fetch_type(opline, result, type); |
3116 | 2.68k | return opline; |
3117 | 625k | } else { |
3118 | 625k | zend_short_circuiting_mark_inner(var_ast); |
3119 | 625k | opline = zend_delayed_compile_var(&var_node, var_ast, type, false); |
3120 | 625k | if (opline) { |
3121 | 253k | if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) { |
3122 | 3.16k | opline->extended_value |= ZEND_FETCH_DIM_WRITE; |
3123 | 250k | } else if (opline->opcode == ZEND_FETCH_DIM_W |
3124 | 162k | || opline->opcode == ZEND_FETCH_DIM_RW |
3125 | 161k | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3126 | 134k | || opline->opcode == ZEND_FETCH_DIM_UNSET) { |
3127 | 134k | opline->extended_value = ZEND_FETCH_DIM_DIM; |
3128 | 134k | } |
3129 | 253k | } |
3130 | 625k | } |
3131 | | |
3132 | 625k | zend_separate_if_call_and_write(&var_node, var_ast, type); |
3133 | | |
3134 | 625k | if (dim_ast == NULL) { |
3135 | 17.5k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3136 | 213 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
3137 | 213 | } |
3138 | 17.3k | if (type == BP_VAR_UNSET) { |
3139 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
3140 | 8 | } |
3141 | 17.3k | dim_node.op_type = IS_UNUSED; |
3142 | 608k | } else { |
3143 | 608k | zend_compile_expr(&dim_node, dim_ast); |
3144 | 608k | } |
3145 | | |
3146 | 625k | opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node); |
3147 | 625k | zend_adjust_for_fetch_type(opline, result, type); |
3148 | 625k | if (by_ref) { |
3149 | 56.3k | opline->extended_value = ZEND_FETCH_DIM_REF; |
3150 | 56.3k | } |
3151 | | |
3152 | 625k | if (dim_node.op_type == IS_CONST) { |
3153 | 158k | zend_handle_numeric_dim(opline, &dim_node); |
3154 | 158k | } |
3155 | 625k | return opline; |
3156 | 625k | } |
3157 | | |
3158 | | static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3159 | 360k | { |
3160 | 360k | uint32_t offset = zend_delayed_compile_begin(); |
3161 | 360k | zend_delayed_compile_dim(result, ast, type, by_ref); |
3162 | 360k | return zend_delayed_compile_end(offset); |
3163 | 360k | } |
3164 | | /* }}} */ |
3165 | | |
3166 | | static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3167 | 175k | { |
3168 | 175k | zend_ast *obj_ast = ast->child[0]; |
3169 | 175k | zend_ast *prop_ast = ast->child[1]; |
3170 | | |
3171 | 175k | znode obj_node, prop_node; |
3172 | 175k | zend_op *opline; |
3173 | 175k | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP; |
3174 | | |
3175 | 175k | if (is_this_fetch(obj_ast)) { |
3176 | 18.4k | if (this_guaranteed_exists()) { |
3177 | 12.4k | obj_node.op_type = IS_UNUSED; |
3178 | 12.4k | } else { |
3179 | 5.96k | opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
3180 | 5.96k | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3181 | 5.80k | opline->result_type = IS_TMP_VAR; |
3182 | 5.80k | obj_node.op_type = IS_TMP_VAR; |
3183 | 5.80k | } |
3184 | 5.96k | } |
3185 | 18.4k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3186 | | |
3187 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
3188 | | * check for a nullsafe access. */ |
3189 | 156k | } else { |
3190 | 156k | zend_short_circuiting_mark_inner(obj_ast); |
3191 | 156k | opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false); |
3192 | 156k | if (opline && (opline->opcode == ZEND_FETCH_DIM_W |
3193 | 36.8k | || opline->opcode == ZEND_FETCH_DIM_RW |
3194 | 36.5k | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3195 | 36.4k | || opline->opcode == ZEND_FETCH_DIM_UNSET)) { |
3196 | 1.38k | opline->extended_value = ZEND_FETCH_DIM_OBJ; |
3197 | 1.38k | } |
3198 | | |
3199 | 156k | zend_separate_if_call_and_write(&obj_node, obj_ast, type); |
3200 | 156k | if (nullsafe) { |
3201 | 83.8k | if (obj_node.op_type == IS_TMP_VAR) { |
3202 | | /* Flush delayed oplines */ |
3203 | 44.4k | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
3204 | 44.4k | uint32_t var = obj_node.u.op.var; |
3205 | 44.4k | uint32_t count = zend_stack_count(&CG(delayed_oplines_stack)); |
3206 | 44.4k | uint32_t i = count; |
3207 | | |
3208 | 131k | while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) { |
3209 | 89.1k | i--; |
3210 | 89.1k | if (oplines[i].op1_type == IS_TMP_VAR) { |
3211 | 86.8k | var = oplines[i].op1.var; |
3212 | 86.8k | } else { |
3213 | 2.28k | break; |
3214 | 2.28k | } |
3215 | 89.1k | } |
3216 | 133k | for (; i < count; ++i) { |
3217 | 89.1k | if (oplines[i].opcode != ZEND_NOP) { |
3218 | 19.9k | opline = get_next_op(); |
3219 | 19.9k | memcpy(opline, &oplines[i], sizeof(zend_op)); |
3220 | 19.9k | oplines[i].opcode = ZEND_NOP; |
3221 | 19.9k | oplines[i].extended_value = opline - CG(active_op_array)->opcodes; |
3222 | 19.9k | } |
3223 | 89.1k | } |
3224 | 44.4k | } |
3225 | 83.8k | zend_emit_jmp_null(&obj_node, type); |
3226 | 83.8k | } |
3227 | 156k | } |
3228 | | |
3229 | 175k | zend_compile_expr(&prop_node, prop_ast); |
3230 | | |
3231 | 175k | opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node); |
3232 | 175k | if (opline->op2_type == IS_CONST) { |
3233 | 169k | convert_to_string(CT_CONSTANT(opline->op2)); |
3234 | 169k | zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2))); |
3235 | 169k | opline->extended_value = zend_alloc_cache_slots(3); |
3236 | 169k | } |
3237 | | |
3238 | 175k | zend_adjust_for_fetch_type(opline, result, type); |
3239 | | |
3240 | 175k | return opline; |
3241 | 175k | } |
3242 | | /* }}} */ |
3243 | | |
3244 | | static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3245 | 119k | { |
3246 | 119k | uint32_t offset = zend_delayed_compile_begin(); |
3247 | 119k | zend_op *opline = zend_delayed_compile_prop(result, ast, type); |
3248 | 119k | if (by_ref) { /* shared with cache_slot */ |
3249 | 3.82k | opline->extended_value |= ZEND_FETCH_REF; |
3250 | 3.82k | } |
3251 | 119k | return zend_delayed_compile_end(offset); |
3252 | 119k | } |
3253 | | /* }}} */ |
3254 | | |
3255 | | static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */ |
3256 | 16.2k | { |
3257 | 16.2k | zend_ast *class_ast = ast->child[0]; |
3258 | 16.2k | zend_ast *prop_ast = ast->child[1]; |
3259 | | |
3260 | 16.2k | znode class_node, prop_node; |
3261 | 16.2k | zend_op *opline; |
3262 | | |
3263 | 16.2k | zend_short_circuiting_mark_inner(class_ast); |
3264 | 16.2k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
3265 | | |
3266 | 16.2k | zend_compile_expr(&prop_node, prop_ast); |
3267 | | |
3268 | 16.2k | if (delayed) { |
3269 | 6.35k | opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3270 | 9.86k | } else { |
3271 | 9.86k | opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3272 | 9.86k | } |
3273 | 16.2k | if (opline->op1_type == IS_CONST) { |
3274 | 14.1k | convert_to_string(CT_CONSTANT(opline->op1)); |
3275 | 14.1k | opline->extended_value = zend_alloc_cache_slots(3); |
3276 | 14.1k | } |
3277 | 16.2k | if (class_node.op_type == IS_CONST) { |
3278 | 10.5k | opline->op2_type = IS_CONST; |
3279 | 10.5k | opline->op2.constant = zend_add_class_name_literal( |
3280 | 10.5k | Z_STR(class_node.u.constant)); |
3281 | 10.5k | if (opline->op1_type != IS_CONST) { |
3282 | 1.74k | opline->extended_value = zend_alloc_cache_slot(); |
3283 | 1.74k | } |
3284 | 10.5k | } else { |
3285 | 5.64k | SET_NODE(opline->op2, &class_node); |
3286 | 5.64k | } |
3287 | | |
3288 | 16.2k | if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */ |
3289 | 2.00k | opline->extended_value |= ZEND_FETCH_REF; |
3290 | 2.00k | } |
3291 | | |
3292 | 16.2k | zend_adjust_for_fetch_type(opline, result, type); |
3293 | 16.2k | return opline; |
3294 | 16.2k | } |
3295 | | /* }}} */ |
3296 | | |
3297 | 6.78k | static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ { |
3298 | 6.78k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3299 | 526 | if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) { |
3300 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead"); |
3301 | 5 | } |
3302 | 521 | if (array_style != var_ast->attr) { |
3303 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()"); |
3304 | 5 | } |
3305 | 6.25k | } else if (!zend_can_write_to_variable(var_ast)) { |
3306 | 102 | zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values"); |
3307 | 102 | } |
3308 | 6.78k | } |
3309 | | /* }}} */ |
3310 | | |
3311 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node); |
3312 | | |
3313 | | /* Propagate refs used on leaf elements to the surrounding list() structures. */ |
3314 | 4.74k | static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */ |
3315 | 4.74k | const zend_ast_list *list = zend_ast_get_list(ast); |
3316 | 4.74k | bool has_refs = false; |
3317 | 4.74k | uint32_t i; |
3318 | | |
3319 | 18.7k | for (i = 0; i < list->children; ++i) { |
3320 | 13.9k | zend_ast *elem_ast = list->child[i]; |
3321 | | |
3322 | 13.9k | if (elem_ast) { |
3323 | 7.32k | zend_ast *var_ast = elem_ast->child[0]; |
3324 | 7.32k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3325 | 527 | elem_ast->attr = zend_propagate_list_refs(var_ast); |
3326 | 527 | } |
3327 | 7.32k | has_refs |= elem_ast->attr; |
3328 | 7.32k | } |
3329 | 13.9k | } |
3330 | | |
3331 | 4.74k | return has_refs; |
3332 | 4.74k | } |
3333 | | /* }}} */ |
3334 | | |
3335 | | static bool list_is_keyed(const zend_ast_list *list) |
3336 | 4.26k | { |
3337 | 4.57k | for (uint32_t i = 0; i < list->children; i++) { |
3338 | 4.54k | const zend_ast *child = list->child[i]; |
3339 | 4.54k | if (child) { |
3340 | 4.23k | return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL; |
3341 | 4.23k | } |
3342 | 4.54k | } |
3343 | 27 | return false; |
3344 | 4.26k | } |
3345 | | |
3346 | | static void zend_compile_list_assign( |
3347 | | znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */ |
3348 | 4.26k | { |
3349 | 4.26k | zend_ast_list *list = zend_ast_get_list(ast); |
3350 | 4.26k | uint32_t i; |
3351 | 4.26k | bool has_elems = false; |
3352 | 4.26k | bool is_keyed = list_is_keyed(list); |
3353 | | |
3354 | 4.26k | if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) { |
3355 | 79 | zval_make_interned_string(&expr_node->u.constant); |
3356 | 79 | } |
3357 | | |
3358 | 11.4k | for (i = 0; i < list->children; ++i) { |
3359 | 7.18k | zend_ast *elem_ast = list->child[i]; |
3360 | 7.18k | zend_ast *var_ast, *key_ast; |
3361 | 7.18k | znode fetch_result, dim_node; |
3362 | 7.18k | zend_op *opline; |
3363 | | |
3364 | 7.18k | if (elem_ast == NULL) { |
3365 | 382 | if (is_keyed) { |
3366 | 9 | zend_error(E_COMPILE_ERROR, |
3367 | 9 | "Cannot use empty array entries in keyed array assignment"); |
3368 | 373 | } else { |
3369 | 373 | continue; |
3370 | 373 | } |
3371 | 382 | } |
3372 | | |
3373 | 6.81k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
3374 | 9 | zend_error(E_COMPILE_ERROR, |
3375 | 9 | "Spread operator is not supported in assignments"); |
3376 | 9 | } |
3377 | | |
3378 | 6.81k | var_ast = elem_ast->child[0]; |
3379 | 6.81k | key_ast = elem_ast->child[1]; |
3380 | 6.81k | has_elems = true; |
3381 | | |
3382 | 6.81k | if (is_keyed) { |
3383 | 1.00k | if (key_ast == NULL) { |
3384 | 5 | zend_error(E_COMPILE_ERROR, |
3385 | 5 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3386 | 5 | } |
3387 | | |
3388 | 1.00k | zend_compile_expr(&dim_node, key_ast); |
3389 | 5.80k | } else { |
3390 | 5.80k | if (key_ast != NULL) { |
3391 | 7 | zend_error(E_COMPILE_ERROR, |
3392 | 7 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3393 | 7 | } |
3394 | | |
3395 | 5.80k | dim_node.op_type = IS_CONST; |
3396 | 5.80k | ZVAL_LONG(&dim_node.u.constant, i); |
3397 | 5.80k | } |
3398 | | |
3399 | 6.81k | if (expr_node->op_type == IS_CONST) { |
3400 | 834 | Z_TRY_ADDREF(expr_node->u.constant); |
3401 | 834 | } |
3402 | | |
3403 | 6.81k | zend_verify_list_assign_target(var_ast, array_style); |
3404 | | |
3405 | 6.81k | opline = zend_emit_op(&fetch_result, |
3406 | 6.81k | 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); |
3407 | 6.81k | if (opline->opcode == ZEND_FETCH_LIST_R) { |
3408 | 4.28k | opline->result_type = IS_TMP_VAR; |
3409 | 4.28k | fetch_result.op_type = IS_TMP_VAR; |
3410 | 4.28k | } |
3411 | | |
3412 | 6.81k | if (dim_node.op_type == IS_CONST) { |
3413 | 6.39k | zend_handle_numeric_dim(opline, &dim_node); |
3414 | 6.39k | } |
3415 | | |
3416 | 6.81k | if (elem_ast->attr) { |
3417 | 2.37k | zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL); |
3418 | 2.37k | } |
3419 | 6.81k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3420 | 516 | zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type); |
3421 | 6.29k | } else if (elem_ast->attr) { |
3422 | 2.22k | zend_emit_assign_ref_znode(var_ast, &fetch_result); |
3423 | 4.07k | } else { |
3424 | 4.07k | zend_emit_assign_znode(var_ast, &fetch_result); |
3425 | 4.07k | } |
3426 | 6.81k | } |
3427 | | |
3428 | 4.26k | if (has_elems == 0) { |
3429 | 27 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list"); |
3430 | 27 | } |
3431 | | |
3432 | 4.23k | if (result) { |
3433 | 463 | if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) { |
3434 | | /* Deref. */ |
3435 | 298 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL); |
3436 | 298 | } else { |
3437 | 165 | *result = *expr_node; |
3438 | 165 | } |
3439 | 3.77k | } else { |
3440 | 3.77k | zend_do_free(expr_node); |
3441 | 3.77k | } |
3442 | 4.23k | } |
3443 | | /* }}} */ |
3444 | | |
3445 | | static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */ |
3446 | 723k | { |
3447 | 723k | if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) { |
3448 | 103 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context"); |
3449 | 103 | } |
3450 | 723k | if ( |
3451 | 723k | ast->kind == ZEND_AST_METHOD_CALL |
3452 | 723k | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
3453 | 723k | || ast->kind == ZEND_AST_STATIC_CALL |
3454 | 723k | ) { |
3455 | 18 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context"); |
3456 | 18 | } |
3457 | 723k | if (zend_ast_is_short_circuited(ast)) { |
3458 | 25 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context"); |
3459 | 25 | } |
3460 | 723k | if (is_globals_fetch(ast)) { |
3461 | 43 | zend_error_noreturn(E_COMPILE_ERROR, |
3462 | 43 | "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax"); |
3463 | 43 | } |
3464 | 723k | } |
3465 | | /* }}} */ |
3466 | | |
3467 | | /* Detects $a... = $a pattern */ |
3468 | | static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */ |
3469 | 150k | { |
3470 | 150k | if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3471 | 145k | return false; |
3472 | 145k | } |
3473 | | |
3474 | 11.8k | while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) { |
3475 | 6.69k | var_ast = var_ast->child[0]; |
3476 | 6.69k | } |
3477 | | |
3478 | 5.14k | if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3479 | 804 | return false; |
3480 | 804 | } |
3481 | | |
3482 | 4.34k | { |
3483 | 4.34k | zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0])); |
3484 | 4.34k | zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0])); |
3485 | 4.34k | bool result = zend_string_equals(name1, name2); |
3486 | 4.34k | zend_string_release_ex(name1, 0); |
3487 | 4.34k | zend_string_release_ex(name2, 0); |
3488 | 4.34k | return result; |
3489 | 5.14k | } |
3490 | 5.14k | } |
3491 | | /* }}} */ |
3492 | | |
3493 | | static void zend_compile_expr_with_potential_assign_to_self( |
3494 | 150k | znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) { |
3495 | 150k | if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) { |
3496 | | /* $a[0] = $a should evaluate the right $a first */ |
3497 | 591 | znode cv_node; |
3498 | | |
3499 | 591 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3500 | 48 | zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false); |
3501 | 543 | } else { |
3502 | 543 | zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3503 | 543 | } |
3504 | 149k | } else { |
3505 | 149k | zend_compile_expr(expr_node, expr_ast); |
3506 | 149k | } |
3507 | 150k | } |
3508 | | |
3509 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */ |
3510 | 434k | { |
3511 | 434k | zend_ast *var_ast = ast->child[0]; |
3512 | 434k | zend_ast *expr_ast = ast->child[1]; |
3513 | | |
3514 | 434k | znode var_node, expr_node; |
3515 | 434k | zend_op *opline; |
3516 | 434k | uint32_t offset; |
3517 | 434k | if (is_this_fetch(var_ast)) { |
3518 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3519 | 9 | } |
3520 | | |
3521 | 434k | zend_ensure_writable_variable(var_ast); |
3522 | | |
3523 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3524 | 434k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3525 | 434k | switch (kind) { |
3526 | 396k | case ZEND_AST_VAR: |
3527 | 396k | offset = zend_delayed_compile_begin(); |
3528 | 396k | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false); |
3529 | 396k | zend_compile_expr(&expr_node, expr_ast); |
3530 | 396k | zend_delayed_compile_end(offset); |
3531 | 396k | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
3532 | 396k | zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node); |
3533 | 396k | return; |
3534 | 2.12k | case ZEND_AST_STATIC_PROP: |
3535 | 2.12k | offset = zend_delayed_compile_begin(); |
3536 | 2.12k | zend_delayed_compile_var(result, var_ast, BP_VAR_W, false); |
3537 | 2.12k | zend_compile_expr(&expr_node, expr_ast); |
3538 | | |
3539 | 2.12k | opline = zend_delayed_compile_end(offset); |
3540 | 2.12k | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
3541 | 2.12k | opline->result_type = IS_TMP_VAR; |
3542 | 2.12k | result->op_type = IS_TMP_VAR; |
3543 | | |
3544 | 2.12k | zend_emit_op_data(&expr_node); |
3545 | 2.12k | return; |
3546 | 17.3k | case ZEND_AST_DIM: |
3547 | 17.3k | offset = zend_delayed_compile_begin(); |
3548 | 17.3k | zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false); |
3549 | 17.3k | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3550 | | |
3551 | 17.3k | opline = zend_delayed_compile_end(offset); |
3552 | 17.3k | opline->opcode = ZEND_ASSIGN_DIM; |
3553 | 17.3k | opline->result_type = IS_TMP_VAR; |
3554 | 17.3k | result->op_type = IS_TMP_VAR; |
3555 | | |
3556 | 17.3k | opline = zend_emit_op_data(&expr_node); |
3557 | 17.3k | return; |
3558 | 15.2k | case ZEND_AST_PROP: |
3559 | 15.2k | case ZEND_AST_NULLSAFE_PROP: |
3560 | 15.2k | offset = zend_delayed_compile_begin(); |
3561 | 15.2k | zend_delayed_compile_prop(result, var_ast, BP_VAR_W); |
3562 | 15.2k | zend_compile_expr(&expr_node, expr_ast); |
3563 | | |
3564 | 15.2k | opline = zend_delayed_compile_end(offset); |
3565 | 15.2k | opline->opcode = ZEND_ASSIGN_OBJ; |
3566 | 15.2k | opline->result_type = IS_TMP_VAR; |
3567 | 15.2k | result->op_type = IS_TMP_VAR; |
3568 | | |
3569 | 15.2k | zend_emit_op_data(&expr_node); |
3570 | 15.2k | return; |
3571 | 3.76k | case ZEND_AST_ARRAY: |
3572 | 3.76k | if (zend_propagate_list_refs(var_ast)) { |
3573 | 1.97k | if (!zend_is_variable_or_call(expr_ast)) { |
3574 | 18 | zend_error_noreturn(E_COMPILE_ERROR, |
3575 | 18 | "Cannot assign reference to non referenceable value"); |
3576 | 1.95k | } else { |
3577 | 1.95k | zend_assert_not_short_circuited(expr_ast); |
3578 | 1.95k | } |
3579 | | |
3580 | 1.95k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
3581 | | /* MAKE_REF is usually not necessary for CVs. However, if there are |
3582 | | * self-assignments, this forces the RHS to evaluate first. */ |
3583 | 1.95k | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
3584 | 1.95k | } else { |
3585 | 1.79k | if (expr_ast->kind == ZEND_AST_VAR) { |
3586 | | /* list($a, $b) = $a should evaluate the right $a first */ |
3587 | 453 | znode cv_node; |
3588 | | |
3589 | 453 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3590 | 158 | zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false); |
3591 | 295 | } else { |
3592 | 295 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3593 | 295 | } |
3594 | 1.33k | } else { |
3595 | 1.33k | zend_compile_expr(&expr_node, expr_ast); |
3596 | 1.33k | } |
3597 | 1.79k | } |
3598 | | |
3599 | 3.74k | zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type); |
3600 | 3.74k | if (stmt) { |
3601 | 2.70k | result->op_type = IS_UNUSED; |
3602 | 2.70k | } |
3603 | 3.74k | return; |
3604 | 0 | default: ZEND_UNREACHABLE(); |
3605 | 434k | } |
3606 | 434k | } |
3607 | | /* }}} */ |
3608 | | |
3609 | | static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3610 | 11.2k | { |
3611 | 11.2k | zend_ast *target_ast = ast->child[0]; |
3612 | 11.2k | zend_ast *source_ast = ast->child[1]; |
3613 | | |
3614 | 11.2k | znode target_node, source_node; |
3615 | 11.2k | zend_op *opline; |
3616 | 11.2k | uint32_t offset, flags; |
3617 | | |
3618 | 11.2k | if (is_this_fetch(target_ast)) { |
3619 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3620 | 5 | } |
3621 | 11.2k | zend_ensure_writable_variable(target_ast); |
3622 | 11.2k | zend_assert_not_short_circuited(source_ast); |
3623 | 11.2k | if (is_globals_fetch(source_ast)) { |
3624 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS"); |
3625 | 5 | } |
3626 | | |
3627 | 11.2k | offset = zend_delayed_compile_begin(); |
3628 | 11.2k | zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true); |
3629 | 11.2k | zend_compile_var(&source_node, source_ast, BP_VAR_W, true); |
3630 | | |
3631 | 11.2k | if ((target_ast->kind != ZEND_AST_VAR |
3632 | 7.95k | || target_ast->child[0]->kind != ZEND_AST_ZVAL) |
3633 | 4.44k | && source_ast->kind != ZEND_AST_ZNODE |
3634 | 3.38k | && source_node.op_type != IS_CV) { |
3635 | | /* Both LHS and RHS expressions may modify the same data structure, |
3636 | | * and the modification during RHS evaluation may dangle the pointer |
3637 | | * to the result of the LHS evaluation. |
3638 | | * Use MAKE_REF instruction to replace direct pointer with REFERENCE. |
3639 | | * See: Bug #71539 |
3640 | | */ |
3641 | 1.58k | zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL); |
3642 | 1.58k | } |
3643 | | |
3644 | 11.2k | opline = zend_delayed_compile_end(offset); |
3645 | | |
3646 | 11.2k | if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) { |
3647 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3648 | 7 | } |
3649 | | |
3650 | 11.1k | flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0; |
3651 | | |
3652 | 11.1k | if (opline && opline->opcode == ZEND_FETCH_OBJ_W) { |
3653 | 1.17k | opline->opcode = ZEND_ASSIGN_OBJ_REF; |
3654 | 1.17k | opline->extended_value &= ~ZEND_FETCH_REF; |
3655 | 1.17k | opline->extended_value |= flags; |
3656 | 1.17k | if (result) { |
3657 | 210 | *result = target_node; |
3658 | 962 | } else { |
3659 | 962 | SET_UNUSED(opline->result); |
3660 | 962 | } |
3661 | 1.17k | zend_emit_op_data(&source_node); |
3662 | 10.0k | } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) { |
3663 | 697 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF; |
3664 | 697 | opline->extended_value &= ~ZEND_FETCH_REF; |
3665 | 697 | opline->extended_value |= flags; |
3666 | 697 | if (result) { |
3667 | 88 | *result = target_node; |
3668 | 609 | } else { |
3669 | 609 | SET_UNUSED(opline->result); |
3670 | 609 | } |
3671 | 697 | zend_emit_op_data(&source_node); |
3672 | 9.33k | } else { |
3673 | 9.33k | opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node); |
3674 | 9.33k | opline->extended_value = flags; |
3675 | 9.33k | } |
3676 | | |
3677 | 11.1k | if (result && (type == BP_VAR_R || type == BP_VAR_IS)) { |
3678 | | /* Deref. */ |
3679 | 2.37k | znode tmp_result = *result; |
3680 | 2.37k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL); |
3681 | 2.37k | } |
3682 | 11.1k | } |
3683 | | /* }}} */ |
3684 | | |
3685 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3686 | 2.79k | { |
3687 | 2.79k | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast, |
3688 | 2.79k | zend_ast_create_znode(value_node)); |
3689 | 2.79k | zend_compile_stmt(assign_ast); |
3690 | 2.79k | } |
3691 | | /* }}} */ |
3692 | | |
3693 | | static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */ |
3694 | 115k | { |
3695 | 115k | zend_ast *var_ast = ast->child[0]; |
3696 | 115k | zend_ast *expr_ast = ast->child[1]; |
3697 | 115k | uint32_t opcode = ast->attr; |
3698 | | |
3699 | 115k | znode var_node, expr_node; |
3700 | 115k | zend_op *opline; |
3701 | 115k | uint32_t offset, cache_slot; |
3702 | | |
3703 | 115k | zend_ensure_writable_variable(var_ast); |
3704 | | |
3705 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3706 | 115k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3707 | 115k | switch (kind) { |
3708 | 109k | case ZEND_AST_VAR: |
3709 | 109k | offset = zend_delayed_compile_begin(); |
3710 | 109k | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
3711 | 109k | zend_compile_expr(&expr_node, expr_ast); |
3712 | 109k | zend_delayed_compile_end(offset); |
3713 | 109k | opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node); |
3714 | 109k | opline->extended_value = opcode; |
3715 | 109k | return; |
3716 | 494 | case ZEND_AST_STATIC_PROP: |
3717 | 494 | offset = zend_delayed_compile_begin(); |
3718 | 494 | zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false); |
3719 | 494 | zend_compile_expr(&expr_node, expr_ast); |
3720 | | |
3721 | 494 | opline = zend_delayed_compile_end(offset); |
3722 | 494 | cache_slot = opline->extended_value; |
3723 | 494 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP; |
3724 | 494 | opline->extended_value = opcode; |
3725 | 494 | opline->result_type = IS_TMP_VAR; |
3726 | 494 | result->op_type = IS_TMP_VAR; |
3727 | | |
3728 | 494 | opline = zend_emit_op_data(&expr_node); |
3729 | 494 | opline->extended_value = cache_slot; |
3730 | 494 | return; |
3731 | 4.19k | case ZEND_AST_DIM: |
3732 | 4.19k | offset = zend_delayed_compile_begin(); |
3733 | 4.19k | zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false); |
3734 | 4.19k | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3735 | | |
3736 | 4.19k | opline = zend_delayed_compile_end(offset); |
3737 | 4.19k | opline->opcode = ZEND_ASSIGN_DIM_OP; |
3738 | 4.19k | opline->extended_value = opcode; |
3739 | 4.19k | opline->result_type = IS_TMP_VAR; |
3740 | 4.19k | result->op_type = IS_TMP_VAR; |
3741 | | |
3742 | 4.19k | zend_emit_op_data(&expr_node); |
3743 | 4.19k | return; |
3744 | 1.42k | case ZEND_AST_PROP: |
3745 | 1.42k | case ZEND_AST_NULLSAFE_PROP: |
3746 | 1.42k | offset = zend_delayed_compile_begin(); |
3747 | 1.42k | zend_delayed_compile_prop(result, var_ast, BP_VAR_RW); |
3748 | 1.42k | zend_compile_expr(&expr_node, expr_ast); |
3749 | | |
3750 | 1.42k | opline = zend_delayed_compile_end(offset); |
3751 | 1.42k | cache_slot = opline->extended_value; |
3752 | 1.42k | opline->opcode = ZEND_ASSIGN_OBJ_OP; |
3753 | 1.42k | opline->extended_value = opcode; |
3754 | 1.42k | opline->result_type = IS_TMP_VAR; |
3755 | 1.42k | result->op_type = IS_TMP_VAR; |
3756 | | |
3757 | 1.42k | opline = zend_emit_op_data(&expr_node); |
3758 | 1.42k | opline->extended_value = cache_slot; |
3759 | 1.42k | return; |
3760 | 0 | default: ZEND_UNREACHABLE(); |
3761 | 115k | } |
3762 | 115k | } |
3763 | | /* }}} */ |
3764 | | |
3765 | 5.92k | static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) { |
3766 | | // TODO: Caching? |
3767 | 20.8k | for (uint32_t i = 0; i < fn->common.num_args; i++) { |
3768 | 18.3k | zend_arg_info *arg_info = &fn->op_array.arg_info[i]; |
3769 | 18.3k | if (zend_string_equals(arg_info->name, arg_name)) { |
3770 | 3.42k | return i + 1; |
3771 | 3.42k | } |
3772 | 18.3k | } |
3773 | | |
3774 | | /* Either an invalid argument name, or collected into a variadic argument. */ |
3775 | 2.50k | return (uint32_t) -1; |
3776 | 5.92k | } |
3777 | | |
3778 | | static uint32_t zend_compile_args_ex( |
3779 | | zend_ast *ast, const zend_function *fbc, |
3780 | | bool *may_have_extra_named_args, |
3781 | | bool is_call_partial, bool *uses_variadic_placeholder_p, |
3782 | | zval *named_positions) /* {{{ */ |
3783 | 4.87M | { |
3784 | 4.87M | const zend_ast_list *args = zend_ast_get_list(ast); |
3785 | 4.87M | uint32_t i; |
3786 | 4.87M | bool uses_arg_unpack = false; |
3787 | 4.87M | bool uses_variadic_placeholder = false; |
3788 | 4.87M | uint32_t arg_count = 0; /* number of arguments not including unpacks */ |
3789 | | |
3790 | | /* Whether named arguments are used syntactically, to enforce language level limitations. |
3791 | | * May not actually use named argument passing. */ |
3792 | 4.87M | bool uses_named_args = false; |
3793 | | /* Whether there may be any undef arguments due to the use of named arguments. */ |
3794 | 4.87M | bool may_have_undef = false; |
3795 | | /* Whether there may be any extra named arguments collected into a variadic. */ |
3796 | 4.87M | *may_have_extra_named_args = false; |
3797 | | |
3798 | 10.8M | for (i = 0; i < args->children; ++i) { |
3799 | 5.94M | zend_ast *arg = args->child[i]; |
3800 | 5.94M | zend_string *arg_name = NULL; |
3801 | 5.94M | uint32_t arg_num = i + 1; |
3802 | | |
3803 | 5.94M | znode arg_node; |
3804 | 5.94M | zend_op *opline; |
3805 | 5.94M | uint8_t opcode; |
3806 | | |
3807 | 5.94M | if (arg->kind == ZEND_AST_UNPACK) { |
3808 | 2.50k | if (uses_named_args) { |
3809 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3810 | 5 | "Cannot use argument unpacking after named arguments"); |
3811 | 5 | } |
3812 | | |
3813 | 2.49k | if (is_call_partial) { |
3814 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
3815 | 7 | "Cannot combine partial application and unpacking"); |
3816 | 7 | } |
3817 | | |
3818 | | /* Unpack may contain named arguments. */ |
3819 | 2.49k | may_have_undef = true; |
3820 | 2.49k | if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3821 | 1.91k | *may_have_extra_named_args = true; |
3822 | 1.91k | } |
3823 | | |
3824 | 2.49k | uses_arg_unpack = true; |
3825 | 2.49k | fbc = NULL; |
3826 | | |
3827 | 2.49k | zend_compile_expr(&arg_node, arg->child[0]); |
3828 | 2.49k | opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL); |
3829 | 2.49k | opline->op2.num = arg_count; |
3830 | 2.49k | opline->result.var = EX_NUM_TO_VAR(arg_count - 1); |
3831 | | |
3832 | 2.49k | continue; |
3833 | 2.49k | } |
3834 | | |
3835 | 5.93M | if (arg->kind == ZEND_AST_NAMED_ARG) { |
3836 | 82.3k | uses_named_args = true; |
3837 | 82.3k | arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0])); |
3838 | 82.3k | arg = arg->child[1]; |
3839 | | |
3840 | 82.3k | if (fbc && !uses_arg_unpack) { |
3841 | 5.92k | arg_num = zend_get_arg_num(fbc, arg_name); |
3842 | 5.92k | if (arg_num == arg_count + 1 && !may_have_undef) { |
3843 | | /* Using named arguments, but passing in order. */ |
3844 | 756 | arg_name = NULL; |
3845 | 756 | arg_count++; |
3846 | 5.17k | } else { |
3847 | | // TODO: We could track which arguments were passed, even if out of order. |
3848 | 5.17k | may_have_undef = true; |
3849 | 5.17k | if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3850 | 554 | *may_have_extra_named_args = true; |
3851 | 554 | } |
3852 | 5.17k | } |
3853 | 76.4k | } else { |
3854 | 76.4k | arg_num = (uint32_t) -1; |
3855 | 76.4k | may_have_undef = true; |
3856 | 76.4k | *may_have_extra_named_args = true; |
3857 | 76.4k | } |
3858 | | |
3859 | 82.3k | if (uses_variadic_placeholder) { |
3860 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3861 | 5 | "Variadic placeholder must be last"); |
3862 | 5 | } |
3863 | 5.85M | } else { |
3864 | 5.85M | if (uses_arg_unpack) { |
3865 | 17 | zend_error_noreturn(E_COMPILE_ERROR, |
3866 | 17 | "Cannot use positional argument after argument unpacking"); |
3867 | 17 | } |
3868 | | |
3869 | 5.85M | bool is_variadic_placeholder = arg->kind == ZEND_AST_PLACEHOLDER_ARG |
3870 | 1.85k | && arg->attr == ZEND_PLACEHOLDER_VARIADIC; |
3871 | | |
3872 | 5.85M | if (uses_named_args && !is_variadic_placeholder) { |
3873 | 42 | zend_error_noreturn(E_COMPILE_ERROR, |
3874 | 42 | "Cannot use positional argument after named argument"); |
3875 | 42 | } |
3876 | | |
3877 | 5.85M | if (uses_variadic_placeholder) { |
3878 | 14 | if (is_variadic_placeholder) { |
3879 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3880 | 5 | "Variadic placeholder may only appear once"); |
3881 | 9 | } else { |
3882 | 9 | zend_error_noreturn(E_COMPILE_ERROR, |
3883 | 9 | "Variadic placeholder must be last"); |
3884 | 9 | } |
3885 | 14 | } |
3886 | | |
3887 | 5.85M | if (!is_variadic_placeholder) { |
3888 | 5.85M | arg_count++; |
3889 | 5.85M | } |
3890 | 5.85M | } |
3891 | | |
3892 | 5.93M | if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) { |
3893 | 2.11k | if (uses_arg_unpack) { |
3894 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3895 | 0 | "Cannot combine partial application and unpacking"); |
3896 | 0 | } |
3897 | | |
3898 | 2.11k | if (arg->attr == ZEND_PLACEHOLDER_VARIADIC) { |
3899 | 394 | uses_variadic_placeholder = true; |
3900 | | /* Do not emit ZEND_SEND_PLACEHOLDER: We represent the variadic |
3901 | | * placeholder with a flag on the ZEND_CONVERT_CALLABLE_PARTIAL |
3902 | | * op instead. */ |
3903 | 394 | continue; |
3904 | 394 | } |
3905 | | |
3906 | 1.71k | if (arg_name) { |
3907 | 262 | if (Z_ISUNDEF_P(named_positions)) { |
3908 | 199 | array_init(named_positions); |
3909 | 199 | } |
3910 | 262 | zval tmp; |
3911 | 262 | ZVAL_LONG(&tmp, zend_hash_num_elements(Z_ARRVAL_P(named_positions))); |
3912 | 262 | zend_hash_add(Z_ARRVAL_P(named_positions), arg_name, &tmp); |
3913 | 262 | } |
3914 | | |
3915 | 1.71k | opline = zend_emit_op(NULL, ZEND_SEND_PLACEHOLDER, NULL, NULL); |
3916 | 1.71k | if (arg_name) { |
3917 | 262 | opline->op2_type = IS_CONST; |
3918 | 262 | zend_string_addref(arg_name); |
3919 | 262 | opline->op2.constant = zend_add_literal_string(&arg_name); |
3920 | 262 | opline->result.num = zend_alloc_cache_slots(2); |
3921 | 1.45k | } else if (arg->attr != ZEND_PLACEHOLDER_VARIADIC) { |
3922 | 1.45k | opline->op2.opline_num = arg_num; |
3923 | 1.45k | opline->result.var = EX_NUM_TO_VAR(arg_num - 1); |
3924 | 1.45k | } |
3925 | | |
3926 | 1.71k | continue; |
3927 | 2.11k | } |
3928 | | |
3929 | | /* Treat passing of $GLOBALS the same as passing a call. |
3930 | | * This will error at runtime if the argument is by-ref. */ |
3931 | 5.93M | if (zend_is_call(arg) || is_globals_fetch(arg)) { |
3932 | 220k | uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) |
3933 | 220k | ? BP_VAR_R : BP_VAR_FUNC_ARG; |
3934 | 220k | zend_compile_var(&arg_node, arg, type, /* by_ref */ false); |
3935 | 220k | if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) { |
3936 | | /* Function call was converted into builtin instruction */ |
3937 | 49.5k | if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3938 | 4.96k | opcode = ZEND_SEND_VAL_EX; |
3939 | 44.5k | } else { |
3940 | 44.5k | opcode = ZEND_SEND_VAL; |
3941 | 44.5k | } |
3942 | 171k | } else { |
3943 | 171k | if (fbc && arg_num != (uint32_t) -1) { |
3944 | 752 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3945 | 688 | opcode = ZEND_SEND_VAR_NO_REF; |
3946 | 688 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
3947 | | /* For IS_VAR operands, SEND_VAL will pass through the operand without |
3948 | | * dereferencing, so it will use a by-ref pass if the call returned by-ref |
3949 | | * and a by-value pass if it returned by-value. */ |
3950 | 64 | opcode = ZEND_SEND_VAL; |
3951 | 64 | } else { |
3952 | 0 | opcode = ZEND_SEND_VAR; |
3953 | 0 | } |
3954 | 170k | } else { |
3955 | 170k | opcode = ZEND_SEND_VAR_NO_REF_EX; |
3956 | 170k | } |
3957 | 171k | } |
3958 | 5.71M | } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) { |
3959 | 164k | if (fbc && arg_num != (uint32_t) -1) { |
3960 | 90.3k | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
3961 | 3.51k | zend_compile_var(&arg_node, arg, BP_VAR_W, true); |
3962 | 3.51k | opcode = ZEND_SEND_REF; |
3963 | 86.7k | } else { |
3964 | 86.7k | zend_compile_var(&arg_node, arg, BP_VAR_R, false); |
3965 | 86.7k | opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR; |
3966 | 86.7k | } |
3967 | 90.3k | } else { |
3968 | 74.4k | do { |
3969 | 74.4k | if (arg->kind == ZEND_AST_VAR) { |
3970 | 18.2k | CG(zend_lineno) = zend_ast_get_lineno(ast); |
3971 | 18.2k | if (is_this_fetch(arg)) { |
3972 | 258 | zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL); |
3973 | 258 | opcode = ZEND_SEND_VAR_EX; |
3974 | 258 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3975 | 258 | break; |
3976 | 18.0k | } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) { |
3977 | 17.8k | opcode = ZEND_SEND_VAR_EX; |
3978 | 17.8k | break; |
3979 | 17.8k | } |
3980 | 18.2k | } |
3981 | 56.3k | opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL); |
3982 | 56.3k | if (arg_name) { |
3983 | 7.30k | opline->op2_type = IS_CONST; |
3984 | 7.30k | zend_string_addref(arg_name); |
3985 | 7.30k | opline->op2.constant = zend_add_literal_string(&arg_name); |
3986 | 7.30k | opline->result.num = zend_alloc_cache_slots(2); |
3987 | 49.0k | } else { |
3988 | 49.0k | opline->op2.num = arg_num; |
3989 | 49.0k | } |
3990 | 56.3k | zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true); |
3991 | 56.3k | opcode = ZEND_SEND_FUNC_ARG; |
3992 | 56.3k | } while (0); |
3993 | 74.4k | } |
3994 | 5.55M | } else { |
3995 | 5.55M | zend_compile_expr(&arg_node, arg); |
3996 | 5.55M | if (arg_node.op_type == IS_VAR) { |
3997 | | /* pass ++$a or something similar */ |
3998 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
3999 | 0 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
4000 | 0 | opcode = ZEND_SEND_VAR_NO_REF; |
4001 | 0 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
4002 | 0 | opcode = ZEND_SEND_VAL; |
4003 | 0 | } else { |
4004 | 0 | opcode = ZEND_SEND_VAR; |
4005 | 0 | } |
4006 | 0 | } else { |
4007 | 0 | opcode = ZEND_SEND_VAR_NO_REF_EX; |
4008 | 0 | } |
4009 | 5.55M | } else if (arg_node.op_type == IS_CV) { |
4010 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
4011 | 0 | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
4012 | 0 | opcode = ZEND_SEND_REF; |
4013 | 0 | } else { |
4014 | 0 | opcode = ZEND_SEND_VAR; |
4015 | 0 | } |
4016 | 0 | } else { |
4017 | 0 | opcode = ZEND_SEND_VAR_EX; |
4018 | 0 | } |
4019 | 5.55M | } else { |
4020 | | /* Delay "Only variables can be passed by reference" error to execution */ |
4021 | 5.55M | if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
4022 | 131k | opcode = ZEND_SEND_VAL; |
4023 | 5.41M | } else { |
4024 | 5.41M | opcode = ZEND_SEND_VAL_EX; |
4025 | 5.41M | } |
4026 | 5.55M | } |
4027 | 5.55M | } |
4028 | | |
4029 | 5.93M | opline = zend_emit_op(NULL, opcode, &arg_node, NULL); |
4030 | 5.93M | if (arg_name) { |
4031 | 81.3k | opline->op2_type = IS_CONST; |
4032 | 81.3k | zend_string_addref(arg_name); |
4033 | 81.3k | opline->op2.constant = zend_add_literal_string(&arg_name); |
4034 | 81.3k | opline->result.num = zend_alloc_cache_slots(2); |
4035 | 5.85M | } else { |
4036 | 5.85M | opline->op2.opline_num = arg_num; |
4037 | 5.85M | opline->result.var = EX_NUM_TO_VAR(arg_num - 1); |
4038 | 5.85M | } |
4039 | 5.93M | } |
4040 | | |
4041 | 4.87M | if (!is_call_partial) { |
4042 | 4.86M | if (may_have_undef) { |
4043 | 72.3k | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
4044 | 72.3k | } |
4045 | 4.86M | } else { |
4046 | 5.13k | *uses_variadic_placeholder_p = uses_variadic_placeholder; |
4047 | 5.13k | } |
4048 | | |
4049 | 4.87M | return arg_count; |
4050 | 4.87M | } |
4051 | | /* }}} */ |
4052 | | |
4053 | | static uint32_t zend_compile_args(zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) |
4054 | 4.87M | { |
4055 | 4.87M | return zend_compile_args_ex(ast, fbc, may_have_extra_named_args, false, NULL, NULL); |
4056 | 4.87M | } |
4057 | | |
4058 | | ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */ |
4059 | 5.01M | { |
4060 | 5.01M | uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD; |
4061 | | |
4062 | 5.01M | if (fbc && init_op->opcode != ZEND_NEW) { |
4063 | 306k | ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)); |
4064 | 306k | if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) { |
4065 | 243k | if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) { |
4066 | 21.6k | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
4067 | 21.6k | return ZEND_DO_ICALL; |
4068 | 21.6k | } else { |
4069 | 61 | return ZEND_DO_FCALL_BY_NAME; |
4070 | 61 | } |
4071 | 21.6k | } |
4072 | 243k | } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){ |
4073 | 63.3k | if (zend_execute_ex == execute_ex) { |
4074 | 30.1k | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
4075 | 29.8k | return ZEND_DO_UCALL; |
4076 | 29.8k | } else { |
4077 | 298 | return ZEND_DO_FCALL_BY_NAME; |
4078 | 298 | } |
4079 | 30.1k | } |
4080 | 63.3k | } |
4081 | 4.70M | } else if (zend_execute_ex == execute_ex && |
4082 | 4.62M | !zend_execute_internal && |
4083 | 4.57M | (init_op->opcode == ZEND_INIT_FCALL_BY_NAME || |
4084 | 4.49M | init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) { |
4085 | 4.19M | return ZEND_DO_FCALL_BY_NAME; |
4086 | 4.19M | } |
4087 | 768k | return ZEND_DO_FCALL; |
4088 | 5.01M | } |
4089 | | /* }}} */ |
4090 | | |
4091 | | static void zend_compile_call_partial(znode *result, uint32_t arg_count, |
4092 | | bool may_have_extra_named_args, bool uses_variadic_placeholder, |
4093 | 1.57k | zval *named_positions, uint32_t opnum_init, const zend_function *fbc) { |
4094 | | |
4095 | 1.57k | zend_op *init_opline = &CG(active_op_array)->opcodes[opnum_init]; |
4096 | | |
4097 | 1.57k | init_opline->extended_value = arg_count; |
4098 | | |
4099 | 1.57k | ZEND_ASSERT(init_opline->opcode != ZEND_NEW); |
4100 | | |
4101 | 1.57k | if (init_opline->opcode == ZEND_INIT_FCALL) { |
4102 | 898 | init_opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc); |
4103 | 898 | } |
4104 | | |
4105 | 1.57k | zend_op *opline = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT_PARTIAL, |
4106 | 1.57k | NULL, NULL); |
4107 | | |
4108 | 1.57k | opline->op1.num = zend_alloc_cache_slots(2); |
4109 | | |
4110 | 1.57k | if (may_have_extra_named_args) { |
4111 | 195 | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4112 | 195 | } |
4113 | 1.57k | if (uses_variadic_placeholder) { |
4114 | 374 | opline->extended_value |= ZEND_FCALL_USES_VARIADIC_PLACEHOLDER; |
4115 | 374 | } |
4116 | | |
4117 | 1.57k | if (!Z_ISUNDEF_P(named_positions)) { |
4118 | 199 | opline->op2.constant = zend_add_literal(named_positions); |
4119 | 199 | opline->op2_type = IS_CONST; |
4120 | 199 | } |
4121 | 1.57k | } |
4122 | | |
4123 | | static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4124 | 4.87M | { |
4125 | 4.87M | zend_op *opline; |
4126 | 4.87M | uint32_t opnum_init = get_next_op_number() - 1; |
4127 | | |
4128 | 4.87M | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
4129 | 7.58k | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4130 | 7.58k | opline->extended_value = 0; |
4131 | | /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */ |
4132 | 7.58k | uint8_t opcode = opline->opcode; |
4133 | | |
4134 | 7.58k | if (opcode == ZEND_NEW) { |
4135 | 19 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
4136 | 19 | } |
4137 | | |
4138 | 7.56k | zend_ast_list *fcc_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
4139 | | |
4140 | | /* FCCs are a special case of PFAs with a single variadic placeholder */ |
4141 | 7.56k | if (fcc_args->children == 1 && fcc_args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { |
4142 | | |
4143 | 5.95k | if (opline->opcode == ZEND_INIT_FCALL) { |
4144 | 815 | opline->op1.num = zend_vm_calc_used_stack(0, fbc); |
4145 | 815 | } |
4146 | | |
4147 | 5.95k | zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL); |
4148 | 5.95k | if (opcode == ZEND_INIT_FCALL |
4149 | 5.14k | || opcode == ZEND_INIT_FCALL_BY_NAME |
4150 | 4.82k | || opcode == ZEND_INIT_NS_FCALL_BY_NAME) { |
4151 | 4.82k | callable_convert_op->extended_value = zend_alloc_cache_slot(); |
4152 | 4.82k | } else { |
4153 | 1.13k | callable_convert_op->extended_value = (uint32_t)-1; |
4154 | 1.13k | } |
4155 | | |
4156 | 5.95k | return true; |
4157 | 5.95k | } |
4158 | | |
4159 | 1.60k | args_ast = ((zend_ast_fcc*)args_ast)->args; |
4160 | | |
4161 | 1.60k | bool may_have_extra_named_args; |
4162 | 1.60k | bool uses_variadic_placeholder; |
4163 | | |
4164 | 1.60k | zval named_positions; |
4165 | 1.60k | ZVAL_UNDEF(&named_positions); |
4166 | | |
4167 | 1.60k | uint32_t arg_count = zend_compile_args_ex(args_ast, fbc, |
4168 | 1.60k | &may_have_extra_named_args, true, &uses_variadic_placeholder, |
4169 | 1.60k | &named_positions); |
4170 | | |
4171 | 1.60k | zend_compile_call_partial(result, arg_count, |
4172 | 1.60k | may_have_extra_named_args, uses_variadic_placeholder, |
4173 | 1.60k | &named_positions, opnum_init, fbc); |
4174 | | |
4175 | 1.60k | return true; |
4176 | 7.56k | } |
4177 | | |
4178 | 4.87M | bool may_have_extra_named_args; |
4179 | 4.87M | uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args); |
4180 | | |
4181 | 4.87M | zend_do_extended_fcall_begin(); |
4182 | | |
4183 | 4.87M | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4184 | 4.87M | opline->extended_value = arg_count; |
4185 | 4.87M | uint8_t init_opcode = opline->opcode; |
4186 | | |
4187 | 4.87M | if (init_opcode == ZEND_INIT_FCALL) { |
4188 | 162k | opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc); |
4189 | 162k | } |
4190 | | |
4191 | 4.87M | uint8_t call_op = zend_get_call_op( |
4192 | 4.87M | opline, |
4193 | 4.87M | fbc, |
4194 | | /* result_used: At this point we do not yet reliably |
4195 | | * know if the result is used. Deoptimize #[\NoDiscard] |
4196 | | * calls to be sure. The optimizer will fix this up. |
4197 | | */ |
4198 | 4.87M | false |
4199 | 4.87M | ); |
4200 | 4.87M | opline = zend_emit_op(result, call_op, NULL, NULL); |
4201 | 4.87M | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4202 | 4.20M | if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) { |
4203 | 3.97M | opline->result_type = IS_TMP_VAR; |
4204 | 3.97M | result->op_type = IS_TMP_VAR; |
4205 | 3.97M | } |
4206 | 4.20M | } |
4207 | 4.87M | if (may_have_extra_named_args) { |
4208 | 70.1k | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4209 | 70.1k | } |
4210 | 4.87M | opline->lineno = lineno; |
4211 | 4.87M | zend_do_extended_fcall_end(); |
4212 | 4.87M | return false; |
4213 | 4.87M | } |
4214 | | /* }}} */ |
4215 | | |
4216 | | static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */ |
4217 | 4.40M | { |
4218 | 4.40M | zend_string *orig_name = zend_ast_get_str(name_ast); |
4219 | 4.40M | bool is_fully_qualified; |
4220 | | |
4221 | 4.40M | name_node->op_type = IS_CONST; |
4222 | 4.40M | ZVAL_STR(&name_node->u.constant, zend_resolve_function_name( |
4223 | 4.40M | orig_name, name_ast->attr, &is_fully_qualified)); |
4224 | | |
4225 | 4.40M | return !is_fully_qualified && FC(current_namespace); |
4226 | 4.40M | } |
4227 | | /* }}} */ |
4228 | | |
4229 | | static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
4230 | 130k | { |
4231 | 130k | if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) { |
4232 | 86.2k | const char *colon; |
4233 | 86.2k | zend_string *str = Z_STR(name_node->u.constant); |
4234 | 86.2k | if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') { |
4235 | 338 | zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0); |
4236 | 338 | zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0); |
4237 | 338 | zend_op *opline = get_next_op(); |
4238 | | |
4239 | 338 | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
4240 | 338 | opline->op1_type = IS_CONST; |
4241 | 338 | opline->op1.constant = zend_add_class_name_literal(class); |
4242 | 338 | opline->op2_type = IS_CONST; |
4243 | 338 | opline->op2.constant = zend_add_func_name_literal(method); |
4244 | | /* 2 slots, for class and method */ |
4245 | 338 | opline->result.num = zend_alloc_cache_slots(2); |
4246 | 338 | zval_ptr_dtor(&name_node->u.constant); |
4247 | 85.9k | } else { |
4248 | 85.9k | zend_op *opline = get_next_op(); |
4249 | | |
4250 | 85.9k | opline->opcode = ZEND_INIT_FCALL_BY_NAME; |
4251 | 85.9k | opline->op2_type = IS_CONST; |
4252 | 85.9k | opline->op2.constant = zend_add_func_name_literal(str); |
4253 | 85.9k | opline->result.num = zend_alloc_cache_slot(); |
4254 | 85.9k | } |
4255 | 86.2k | } else { |
4256 | 44.5k | zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node); |
4257 | 44.5k | } |
4258 | | |
4259 | 130k | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
4260 | 130k | } |
4261 | | /* }}} */ |
4262 | | |
4263 | | static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */ |
4264 | 4.24M | { |
4265 | 4.24M | uint32_t i; |
4266 | 9.81M | for (i = 0; i < args->children; ++i) { |
4267 | 5.59M | const zend_ast *arg = args->child[i]; |
4268 | 5.59M | if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) { |
4269 | 27.3k | return true; |
4270 | 27.3k | } |
4271 | 5.59M | } |
4272 | 4.22M | return false; |
4273 | 4.24M | } |
4274 | | /* }}} */ |
4275 | | |
4276 | | static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */ |
4277 | 1.07k | { |
4278 | 1.07k | znode arg_node; |
4279 | | |
4280 | 1.07k | if (args->children != 1) { |
4281 | 20 | return FAILURE; |
4282 | 20 | } |
4283 | | |
4284 | 1.05k | zend_compile_expr(&arg_node, args->child[0]); |
4285 | 1.05k | if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) { |
4286 | 256 | result->op_type = IS_CONST; |
4287 | 256 | ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant)); |
4288 | 256 | zval_ptr_dtor_str(&arg_node.u.constant); |
4289 | 802 | } else { |
4290 | 802 | zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL); |
4291 | 802 | } |
4292 | 1.05k | return SUCCESS; |
4293 | 1.07k | } |
4294 | | /* }}} */ |
4295 | | |
4296 | | static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4297 | 2.35k | { |
4298 | 2.35k | znode arg_node; |
4299 | 2.35k | zend_op *opline; |
4300 | | |
4301 | 2.35k | if (args->children != 1) { |
4302 | 592 | return FAILURE; |
4303 | 592 | } |
4304 | | |
4305 | 1.76k | zend_compile_expr(&arg_node, args->child[0]); |
4306 | 1.76k | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4307 | 1.76k | if (type != _IS_BOOL) { |
4308 | 1.53k | opline->extended_value = (1 << type); |
4309 | 1.53k | } else { |
4310 | 230 | opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE); |
4311 | 230 | } |
4312 | 1.76k | return SUCCESS; |
4313 | 2.35k | } |
4314 | | /* }}} */ |
4315 | | |
4316 | | static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */ |
4317 | 313 | { |
4318 | 313 | znode arg_node; |
4319 | 313 | zend_op *opline; |
4320 | | |
4321 | 313 | if (args->children != 1) { |
4322 | 23 | return FAILURE; |
4323 | 23 | } |
4324 | | |
4325 | 290 | zend_compile_expr(&arg_node, args->child[0]); |
4326 | 290 | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4327 | 290 | opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING); |
4328 | 290 | return SUCCESS; |
4329 | 313 | } |
4330 | | |
4331 | | static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4332 | 1.22k | { |
4333 | 1.22k | znode arg_node; |
4334 | 1.22k | zend_op *opline; |
4335 | | |
4336 | 1.22k | if (args->children != 1) { |
4337 | 320 | return FAILURE; |
4338 | 320 | } |
4339 | | |
4340 | 905 | zend_compile_expr(&arg_node, args->child[0]); |
4341 | 905 | if (type == _IS_BOOL) { |
4342 | 161 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL); |
4343 | 744 | } else { |
4344 | 744 | opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL); |
4345 | 744 | opline->extended_value = type; |
4346 | 744 | } |
4347 | 905 | return SUCCESS; |
4348 | 1.22k | } |
4349 | | /* }}} */ |
4350 | | |
4351 | | static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */ |
4352 | 2.35k | { |
4353 | 2.35k | zend_string *name; |
4354 | 2.35k | zend_op *opline; |
4355 | | |
4356 | 2.35k | if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) { |
4357 | 398 | return FAILURE; |
4358 | 398 | } |
4359 | | |
4360 | 1.95k | name = zval_get_string(zend_ast_get_zval(args->child[0])); |
4361 | 1.95k | if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) { |
4362 | 383 | zend_string_release_ex(name, 0); |
4363 | 383 | return FAILURE; |
4364 | 383 | } |
4365 | | |
4366 | 1.57k | if (zend_try_ct_eval_const(&result->u.constant, name, false)) { |
4367 | 122 | zend_string_release_ex(name, 0); |
4368 | 122 | zval_ptr_dtor(&result->u.constant); |
4369 | 122 | ZVAL_TRUE(&result->u.constant); |
4370 | 122 | result->op_type = IS_CONST; |
4371 | 122 | return SUCCESS; |
4372 | 122 | } |
4373 | | |
4374 | 1.45k | opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL); |
4375 | 1.45k | opline->op1_type = IS_CONST; |
4376 | 1.45k | LITERAL_STR(opline->op1, name); |
4377 | 1.45k | opline->extended_value = zend_alloc_cache_slot(); |
4378 | | |
4379 | 1.45k | return SUCCESS; |
4380 | 1.57k | } |
4381 | | /* }}} */ |
4382 | | |
4383 | | static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */ |
4384 | 1.12k | { |
4385 | 1.12k | zval *zint; |
4386 | 1.12k | if ( |
4387 | 1.12k | args->children == 1 |
4388 | 732 | && args->child[0]->kind == ZEND_AST_ZVAL |
4389 | 507 | && (zint = zend_ast_get_zval(args->child[0])) |
4390 | 507 | && Z_TYPE_P(zint) == IS_LONG |
4391 | 440 | && Z_LVAL_P(zint) >= 0 |
4392 | 440 | && Z_LVAL_P(zint) <= 255 |
4393 | 1.12k | ) { |
4394 | 290 | result->op_type = IS_CONST; |
4395 | 290 | ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint)); |
4396 | 290 | return SUCCESS; |
4397 | 830 | } else { |
4398 | 830 | return FAILURE; |
4399 | 830 | } |
4400 | 1.12k | } |
4401 | | /* }}} */ |
4402 | | |
4403 | | static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */ |
4404 | 662 | { |
4405 | 662 | zval *str; |
4406 | 662 | if ( |
4407 | 662 | args->children == 1 |
4408 | 533 | && args->child[0]->kind == ZEND_AST_ZVAL |
4409 | 300 | && (str = zend_ast_get_zval(args->child[0])) |
4410 | 300 | && Z_TYPE_P(str) == IS_STRING |
4411 | 92 | && Z_STRLEN_P(str) == 1 |
4412 | 662 | ) { |
4413 | 56 | result->op_type = IS_CONST; |
4414 | 56 | ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]); |
4415 | 56 | return SUCCESS; |
4416 | 606 | } else { |
4417 | 606 | return FAILURE; |
4418 | 606 | } |
4419 | 662 | } |
4420 | | /* }}} */ |
4421 | | |
4422 | | /* We can only calculate the stack size for functions that have been fully compiled, otherwise |
4423 | | * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for |
4424 | | * directly or indirectly recursive function calls. */ |
4425 | 199k | static bool fbc_is_finalized(const zend_function *fbc) { |
4426 | 199k | return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO); |
4427 | 199k | } |
4428 | | |
4429 | | static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename) |
4430 | 70.3k | { |
4431 | 70.3k | if (ce->type == ZEND_INTERNAL_CLASS) { |
4432 | 45.7k | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES; |
4433 | 45.7k | } else { |
4434 | 24.5k | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4435 | 19.3k | && ce->info.user.filename != filename; |
4436 | 24.5k | } |
4437 | 70.3k | } |
4438 | | |
4439 | | static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename) |
4440 | 186k | { |
4441 | 186k | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
4442 | 155k | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS; |
4443 | 155k | } else { |
4444 | 30.5k | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS) |
4445 | 30.5k | || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4446 | 28.4k | && fbc->op_array.filename != filename); |
4447 | 30.5k | } |
4448 | 186k | } |
4449 | | |
4450 | | static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */ |
4451 | 10.6k | { |
4452 | 10.6k | zend_string *name, *lcname; |
4453 | 10.6k | zend_function *fbc; |
4454 | 10.6k | zend_op *opline; |
4455 | | |
4456 | 10.6k | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
4457 | 2.71k | return FAILURE; |
4458 | 2.71k | } |
4459 | | |
4460 | 7.94k | name = zend_ast_get_str(name_ast); |
4461 | 7.94k | lcname = zend_string_tolower(name); |
4462 | | |
4463 | 7.94k | fbc = zend_hash_find_ptr(CG(function_table), lcname); |
4464 | 7.94k | if (!fbc |
4465 | 774 | || !fbc_is_finalized(fbc) |
4466 | 7.16k | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
4467 | 7.16k | zend_string_release_ex(lcname, 0); |
4468 | 7.16k | return FAILURE; |
4469 | 7.16k | } |
4470 | | |
4471 | 774 | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL); |
4472 | 774 | opline->extended_value = num_args; |
4473 | 774 | opline->op1.num = zend_vm_calc_used_stack(num_args, fbc); |
4474 | 774 | opline->op2_type = IS_CONST; |
4475 | 774 | LITERAL_STR(opline->op2, lcname); |
4476 | 774 | opline->result.num = zend_alloc_cache_slot(); |
4477 | | |
4478 | 774 | return SUCCESS; |
4479 | 7.94k | } |
4480 | | /* }}} */ |
4481 | | |
4482 | | static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */ |
4483 | 10.6k | { |
4484 | 10.6k | zend_op *opline; |
4485 | 10.6k | znode name_node; |
4486 | | |
4487 | 10.6k | if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) { |
4488 | 774 | return; |
4489 | 774 | } |
4490 | | |
4491 | 9.87k | zend_compile_expr(&name_node, name_ast); |
4492 | | |
4493 | 9.87k | opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node); |
4494 | 9.87k | opline->op1_type = IS_CONST; |
4495 | 9.87k | LITERAL_STR(opline->op1, zend_string_copy(orig_func_name)); |
4496 | 9.87k | opline->extended_value = num_args; |
4497 | 9.87k | } |
4498 | | /* }}} */ |
4499 | | |
4500 | | /* cufa = call_user_func_array */ |
4501 | | static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4502 | 2.43k | { |
4503 | 2.43k | znode arg_node; |
4504 | 2.43k | zend_op *opline; |
4505 | | |
4506 | 2.43k | if (args->children != 2) { |
4507 | 68 | return FAILURE; |
4508 | 68 | } |
4509 | | |
4510 | 2.36k | zend_compile_init_user_func(args->child[0], 0, lcname); |
4511 | 2.36k | if (args->child[1]->kind == ZEND_AST_CALL |
4512 | 1.73k | && args->child[1]->child[0]->kind == ZEND_AST_ZVAL |
4513 | 1.66k | && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING |
4514 | 1.60k | && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) { |
4515 | 1.40k | zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]); |
4516 | 1.40k | zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]); |
4517 | 1.40k | bool is_fully_qualified; |
4518 | 1.40k | zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified); |
4519 | | |
4520 | 1.40k | if (zend_string_equals_literal_ci(name, "array_slice") |
4521 | 913 | && !zend_args_contain_unpack_or_named(list) |
4522 | 745 | && list->children == 3 |
4523 | 525 | && list->child[1]->kind == ZEND_AST_ZVAL) { |
4524 | 491 | zval *zv = zend_ast_get_zval(list->child[1]); |
4525 | | |
4526 | 491 | if (Z_TYPE_P(zv) == IS_LONG |
4527 | 416 | && Z_LVAL_P(zv) >= 0 |
4528 | 416 | && Z_LVAL_P(zv) <= 0x7fffffff) { |
4529 | 220 | zend_op *opline; |
4530 | 220 | znode len_node; |
4531 | | |
4532 | 220 | zend_compile_expr(&arg_node, list->child[0]); |
4533 | 220 | zend_compile_expr(&len_node, list->child[2]); |
4534 | 220 | opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node); |
4535 | 220 | opline->extended_value = Z_LVAL_P(zv); |
4536 | 220 | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4537 | 220 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4538 | 220 | opline->result_type = IS_TMP_VAR; |
4539 | 220 | result->op_type = IS_TMP_VAR; |
4540 | 220 | } |
4541 | 220 | zend_string_release_ex(name, 0); |
4542 | 220 | return SUCCESS; |
4543 | 220 | } |
4544 | 491 | } |
4545 | 1.18k | zend_string_release_ex(name, 0); |
4546 | 1.18k | } |
4547 | 2.14k | zend_compile_expr(&arg_node, args->child[1]); |
4548 | 2.14k | zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL); |
4549 | 2.14k | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
4550 | 2.14k | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4551 | 2.14k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4552 | 2.07k | opline->result_type = IS_TMP_VAR; |
4553 | 2.07k | result->op_type = IS_TMP_VAR; |
4554 | 2.07k | } |
4555 | 2.14k | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4556 | | |
4557 | 2.14k | return SUCCESS; |
4558 | 2.36k | } |
4559 | | /* }}} */ |
4560 | | |
4561 | | /* cuf = call_user_func */ |
4562 | | static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4563 | 8.47k | { |
4564 | 8.47k | uint32_t i; |
4565 | | |
4566 | 8.47k | if (args->children < 1) { |
4567 | 182 | return FAILURE; |
4568 | 182 | } |
4569 | | |
4570 | 8.28k | zend_compile_init_user_func(args->child[0], args->children - 1, lcname); |
4571 | 12.1k | for (i = 1; i < args->children; ++i) { |
4572 | 3.90k | zend_ast *arg_ast = args->child[i]; |
4573 | 3.90k | znode arg_node; |
4574 | 3.90k | zend_op *opline; |
4575 | | |
4576 | 3.90k | zend_compile_expr(&arg_node, arg_ast); |
4577 | | |
4578 | 3.90k | opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL); |
4579 | 3.90k | opline->op2.num = i; |
4580 | 3.90k | opline->result.var = EX_NUM_TO_VAR(i - 1); |
4581 | 3.90k | } |
4582 | 8.28k | zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4583 | 8.28k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4584 | 1.03k | opline->result_type = IS_TMP_VAR; |
4585 | 1.03k | result->op_type = IS_TMP_VAR; |
4586 | 1.03k | } |
4587 | | |
4588 | 8.28k | return SUCCESS; |
4589 | 8.47k | } |
4590 | | /* }}} */ |
4591 | | |
4592 | | static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4593 | 42.2k | { |
4594 | 42.2k | if (EG(assertions) >= 0) { |
4595 | 42.2k | znode name_node; |
4596 | 42.2k | zend_op *opline; |
4597 | 42.2k | uint32_t check_op_number = get_next_op_number(); |
4598 | | |
4599 | 42.2k | zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL); |
4600 | | |
4601 | 42.2k | if (fbc && fbc_is_finalized(fbc)) { |
4602 | 13.0k | name_node.op_type = IS_CONST; |
4603 | 13.0k | ZVAL_STR_COPY(&name_node.u.constant, name); |
4604 | | |
4605 | 13.0k | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
4606 | 29.1k | } else { |
4607 | 29.1k | opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL); |
4608 | 29.1k | opline->op2_type = IS_CONST; |
4609 | 29.1k | opline->op2.constant = zend_add_ns_func_name_literal(name); |
4610 | 29.1k | } |
4611 | 42.2k | opline->result.num = zend_alloc_cache_slot(); |
4612 | | |
4613 | 42.2k | if (args->children == 1) { |
4614 | | /* add "assert(condition) as assertion message */ |
4615 | 13.3k | zend_ast *arg = zend_ast_create_zval_from_str( |
4616 | 13.3k | zend_ast_export("assert(", args->child[0], ")")); |
4617 | 13.3k | if (args->child[0]->kind == ZEND_AST_NAMED_ARG) { |
4618 | | /* If the original argument was named, add the new argument as named as well, |
4619 | | * as mixing named and positional is not allowed. */ |
4620 | 1.01k | zend_ast *name = zend_ast_create_zval_from_str( |
4621 | 1.01k | ZSTR_INIT_LITERAL("description", 0)); |
4622 | 1.01k | arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg); |
4623 | 1.01k | } |
4624 | 13.3k | args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg); |
4625 | 13.3k | } |
4626 | | |
4627 | 42.2k | zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type); |
4628 | | |
4629 | 42.2k | opline = &CG(active_op_array)->opcodes[check_op_number]; |
4630 | 42.2k | opline->op2.opline_num = get_next_op_number(); |
4631 | 42.2k | SET_NODE(opline->result, result); |
4632 | 42.2k | } else { |
4633 | 0 | if (!fbc) { |
4634 | 0 | zend_string_release_ex(name, 0); |
4635 | 0 | } |
4636 | 0 | result->op_type = IS_CONST; |
4637 | 0 | ZVAL_TRUE(&result->u.constant); |
4638 | 0 | } |
4639 | 42.2k | } |
4640 | | /* }}} */ |
4641 | | |
4642 | | static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */ |
4643 | 2.88k | { |
4644 | 2.88k | bool strict = false; |
4645 | 2.88k | znode array, needly; |
4646 | 2.88k | zend_op *opline; |
4647 | | |
4648 | 2.88k | if (args->children == 3) { |
4649 | 1.46k | if (args->child[2]->kind == ZEND_AST_ZVAL) { |
4650 | 656 | strict = zend_is_true(zend_ast_get_zval(args->child[2])); |
4651 | 807 | } else if (args->child[2]->kind == ZEND_AST_CONST) { |
4652 | 794 | zval value; |
4653 | 794 | zend_ast *name_ast = args->child[2]->child[0]; |
4654 | 794 | bool is_fully_qualified; |
4655 | 794 | zend_string *resolved_name = zend_resolve_const_name( |
4656 | 794 | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
4657 | | |
4658 | 794 | if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) { |
4659 | 619 | zend_string_release_ex(resolved_name, 0); |
4660 | 619 | return FAILURE; |
4661 | 619 | } |
4662 | | |
4663 | 175 | zend_string_release_ex(resolved_name, 0); |
4664 | 175 | strict = zend_is_true(&value); |
4665 | 175 | zval_ptr_dtor(&value); |
4666 | 175 | } else { |
4667 | 13 | return FAILURE; |
4668 | 13 | } |
4669 | 1.46k | } else if (args->children != 2) { |
4670 | 112 | return FAILURE; |
4671 | 112 | } |
4672 | | |
4673 | 2.14k | if (args->child[1]->kind != ZEND_AST_ARRAY |
4674 | 1.55k | || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) { |
4675 | 638 | return FAILURE; |
4676 | 638 | } |
4677 | | |
4678 | 1.50k | if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) { |
4679 | 1.35k | bool ok = true; |
4680 | 1.35k | zval *val, tmp; |
4681 | 1.35k | HashTable *src = Z_ARRVAL(array.u.constant); |
4682 | 1.35k | HashTable *dst = zend_new_array(zend_hash_num_elements(src)); |
4683 | | |
4684 | 1.35k | ZVAL_TRUE(&tmp); |
4685 | | |
4686 | 1.35k | if (strict) { |
4687 | 3.67k | ZEND_HASH_FOREACH_VAL(src, val) { |
4688 | 3.67k | if (Z_TYPE_P(val) == IS_STRING) { |
4689 | 62 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4690 | 1.06k | } else if (Z_TYPE_P(val) == IS_LONG) { |
4691 | 922 | zend_hash_index_add(dst, Z_LVAL_P(val), &tmp); |
4692 | 922 | } else { |
4693 | 144 | zend_array_destroy(dst); |
4694 | 144 | ok = false; |
4695 | 144 | break; |
4696 | 144 | } |
4697 | 3.67k | } ZEND_HASH_FOREACH_END(); |
4698 | 744 | } else { |
4699 | 3.46k | ZEND_HASH_FOREACH_VAL(src, val) { |
4700 | 3.46k | if (Z_TYPE_P(val) != IS_STRING |
4701 | 851 | || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) { |
4702 | 422 | zend_array_destroy(dst); |
4703 | 422 | ok = false; |
4704 | 422 | break; |
4705 | 422 | } |
4706 | 724 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4707 | 724 | } ZEND_HASH_FOREACH_END(); |
4708 | 744 | } |
4709 | | |
4710 | 1.35k | zend_array_destroy(src); |
4711 | 1.35k | if (!ok) { |
4712 | 566 | return FAILURE; |
4713 | 566 | } |
4714 | 792 | Z_ARRVAL(array.u.constant) = dst; |
4715 | 792 | } |
4716 | 941 | array.op_type = IS_CONST; |
4717 | | |
4718 | 941 | zend_compile_expr(&needly, args->child[0]); |
4719 | | |
4720 | 941 | opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array); |
4721 | 941 | opline->extended_value = strict; |
4722 | | |
4723 | 941 | return SUCCESS; |
4724 | 1.50k | } |
4725 | | /* }}} */ |
4726 | | |
4727 | | static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */ |
4728 | 1.74k | { |
4729 | 1.74k | znode arg_node; |
4730 | 1.74k | zend_op *opline; |
4731 | | |
4732 | 1.74k | if (args->children != 1) { |
4733 | 188 | return FAILURE; |
4734 | 188 | } |
4735 | | |
4736 | 1.55k | zend_compile_expr(&arg_node, args->child[0]); |
4737 | 1.55k | opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL); |
4738 | 1.55k | opline->extended_value = zend_string_equals_literal(lcname, "sizeof"); |
4739 | | |
4740 | 1.55k | return SUCCESS; |
4741 | 1.74k | } |
4742 | | /* }}} */ |
4743 | | |
4744 | | static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4745 | 1.53k | { |
4746 | 1.53k | if (args->children == 0) { |
4747 | 176 | zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL); |
4748 | 1.35k | } else { |
4749 | 1.35k | znode arg_node; |
4750 | | |
4751 | 1.35k | if (args->children != 1) { |
4752 | 186 | return FAILURE; |
4753 | 186 | } |
4754 | | |
4755 | 1.16k | zend_compile_expr(&arg_node, args->child[0]); |
4756 | 1.16k | zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL); |
4757 | 1.16k | } |
4758 | 1.34k | return SUCCESS; |
4759 | 1.53k | } |
4760 | | /* }}} */ |
4761 | | |
4762 | | static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4763 | 283 | { |
4764 | 283 | if (args->children != 0) { |
4765 | 96 | return FAILURE; |
4766 | 96 | } |
4767 | | |
4768 | 187 | zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL); |
4769 | 187 | return SUCCESS; |
4770 | 283 | } |
4771 | | /* }}} */ |
4772 | | |
4773 | | static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */ |
4774 | 692 | { |
4775 | 692 | znode arg_node; |
4776 | | |
4777 | 692 | if (args->children != 1) { |
4778 | 424 | return FAILURE; |
4779 | 424 | } |
4780 | | |
4781 | 268 | zend_compile_expr(&arg_node, args->child[0]); |
4782 | 268 | zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL); |
4783 | 268 | return SUCCESS; |
4784 | 692 | } |
4785 | | /* }}} */ |
4786 | | |
4787 | | static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4788 | 262 | { |
4789 | 262 | if (CG(active_op_array)->function_name && args->children == 0) { |
4790 | 211 | zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL); |
4791 | 211 | return SUCCESS; |
4792 | 211 | } else { |
4793 | 51 | return FAILURE; |
4794 | 51 | } |
4795 | 262 | } |
4796 | | /* }}} */ |
4797 | | |
4798 | | static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4799 | 711 | { |
4800 | 711 | if (CG(active_op_array)->function_name && args->children == 0) { |
4801 | 441 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL); |
4802 | 441 | return SUCCESS; |
4803 | 441 | } else { |
4804 | 270 | return FAILURE; |
4805 | 270 | } |
4806 | 711 | } |
4807 | | /* }}} */ |
4808 | | |
4809 | | static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */ |
4810 | 336 | { |
4811 | 336 | znode subject, needle; |
4812 | | |
4813 | 336 | if (args->children != 2) { |
4814 | 107 | return FAILURE; |
4815 | 107 | } |
4816 | | |
4817 | 229 | zend_compile_expr(&needle, args->child[0]); |
4818 | 229 | zend_compile_expr(&subject, args->child[1]); |
4819 | | |
4820 | 229 | zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject); |
4821 | 229 | return SUCCESS; |
4822 | 336 | } |
4823 | | /* }}} */ |
4824 | | |
4825 | | static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */ |
4826 | 2.57k | { |
4827 | 2.57k | if (CG(active_op_array)->function_name |
4828 | 1.57k | && args->children == 2 |
4829 | 841 | && args->child[0]->kind == ZEND_AST_CALL |
4830 | 721 | && args->child[0]->child[0]->kind == ZEND_AST_ZVAL |
4831 | 553 | && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING |
4832 | 553 | && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST |
4833 | 552 | && args->child[1]->kind == ZEND_AST_ZVAL) { |
4834 | | |
4835 | 522 | zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]); |
4836 | 522 | bool is_fully_qualified; |
4837 | 522 | zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified); |
4838 | 522 | const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]); |
4839 | 522 | const zval *zv = zend_ast_get_zval(args->child[1]); |
4840 | 522 | znode first; |
4841 | | |
4842 | 522 | if (zend_string_equals_literal_ci(name, "func_get_args") |
4843 | 428 | && list->children == 0 |
4844 | 395 | && Z_TYPE_P(zv) == IS_LONG |
4845 | 267 | && Z_LVAL_P(zv) >= 0) { |
4846 | 267 | first.op_type = IS_CONST; |
4847 | 267 | ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv)); |
4848 | 267 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL); |
4849 | 267 | zend_string_release_ex(name, 0); |
4850 | 267 | return SUCCESS; |
4851 | 267 | } |
4852 | 255 | zend_string_release_ex(name, 0); |
4853 | 255 | } |
4854 | 2.31k | return FAILURE; |
4855 | 2.57k | } |
4856 | | /* }}} */ |
4857 | | |
4858 | | static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler) |
4859 | 1.44M | { |
4860 | 1.44M | void **handlers = zend_flf_handlers; |
4861 | 1.44M | void **current = handlers; |
4862 | 13.2M | while (current) { |
4863 | 13.2M | if (*current == handler) { |
4864 | 1.44M | return current - handlers; |
4865 | 1.44M | } |
4866 | 11.8M | current++; |
4867 | 11.8M | } |
4868 | | |
4869 | 0 | return (uint32_t)-1; |
4870 | 1.44M | } |
4871 | | |
4872 | | static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
4873 | 868k | { |
4874 | 868k | if (zend_execute_internal) { |
4875 | 111k | return NULL; |
4876 | 111k | } |
4877 | | |
4878 | 756k | if (ZEND_USER_CODE(fbc->type)) { |
4879 | 10 | return NULL; |
4880 | 10 | } |
4881 | | |
4882 | 756k | const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos; |
4883 | 756k | if (!frameless_function_info) { |
4884 | 18.7k | return NULL; |
4885 | 18.7k | } |
4886 | | |
4887 | 737k | if (args->children > 3) { |
4888 | 390 | return NULL; |
4889 | 390 | } |
4890 | | |
4891 | 929k | while (frameless_function_info->handler) { |
4892 | 914k | if (frameless_function_info->num_args >= args->children |
4893 | 751k | && fbc->common.required_num_args <= args->children |
4894 | 722k | && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC) |
4895 | 722k | || frameless_function_info->num_args == args->children)) { |
4896 | 722k | uint32_t num_args = frameless_function_info->num_args; |
4897 | 722k | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4898 | 722k | if (offset == (uint32_t)-1) { |
4899 | 0 | continue; |
4900 | 0 | } |
4901 | 722k | return frameless_function_info; |
4902 | 722k | } |
4903 | 192k | frameless_function_info++; |
4904 | 192k | } |
4905 | | |
4906 | 15.3k | return NULL; |
4907 | 737k | } |
4908 | | |
4909 | | 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) |
4910 | 721k | { |
4911 | 721k | uint32_t lineno = CG(zend_lineno); |
4912 | 721k | uint32_t num_args = frameless_function_info->num_args; |
4913 | 721k | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4914 | 721k | znode arg_zvs[3]; |
4915 | 2.32M | for (uint32_t i = 0; i < num_args; i++) { |
4916 | 1.60M | if (i < args->children) { |
4917 | 1.60M | zend_compile_expr(&arg_zvs[i], args->child[i]); |
4918 | 1.60M | } else { |
4919 | 0 | const zend_arg_info *arg_info = &fbc->common.arg_info[i]; |
4920 | 0 | arg_zvs[i].op_type = IS_CONST; |
4921 | 0 | if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) { |
4922 | 0 | ZEND_UNREACHABLE(); |
4923 | 0 | } |
4924 | 0 | } |
4925 | 1.60M | } |
4926 | 721k | uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args; |
4927 | 721k | uint32_t opnum = get_next_op_number(); |
4928 | 721k | zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL); |
4929 | 721k | opline->extended_value = offset; |
4930 | 721k | opline->lineno = lineno; |
4931 | 721k | if (num_args >= 1) { |
4932 | 721k | SET_NODE(opline->op1, &arg_zvs[0]); |
4933 | 721k | } |
4934 | 721k | if (num_args >= 2) { |
4935 | 719k | SET_NODE(opline->op2, &arg_zvs[1]); |
4936 | 719k | } |
4937 | 721k | if (num_args >= 3) { |
4938 | 162k | zend_emit_op_data(&arg_zvs[2]); |
4939 | 162k | } |
4940 | 721k | return opnum; |
4941 | 721k | } |
4942 | | |
4943 | | static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
4944 | 122k | { |
4945 | 122k | const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type); |
4946 | 122k | if (!frameless_function_info) { |
4947 | 120k | return (uint32_t)-1; |
4948 | 120k | } |
4949 | | |
4950 | 1.96k | return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type); |
4951 | 122k | } |
4952 | | |
4953 | | static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
4954 | 4.09M | { |
4955 | 4.09M | int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant)); |
4956 | | |
4957 | | /* Find frameless function with same name. */ |
4958 | 4.09M | const zend_function *frameless_function = NULL; |
4959 | 4.09M | if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT |
4960 | 4.09M | && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast)) |
4961 | | /* Avoid blowing up op count with nested frameless branches. */ |
4962 | 4.06M | && !CG(context).in_jmp_frameless_branch) { |
4963 | 2.64M | zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2)); |
4964 | 2.64M | frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name); |
4965 | 2.64M | } |
4966 | | |
4967 | | /* Check whether any frameless handler may actually be used. */ |
4968 | 4.09M | uint32_t jmp_fl_opnum = 0; |
4969 | 4.09M | const zend_frameless_function_info *frameless_function_info = NULL; |
4970 | 4.09M | if (frameless_function) { |
4971 | 745k | frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type); |
4972 | 745k | if (frameless_function_info) { |
4973 | 720k | CG(context).in_jmp_frameless_branch = true; |
4974 | 720k | znode op1; |
4975 | 720k | op1.op_type = IS_CONST; |
4976 | 720k | ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1)); |
4977 | 720k | jmp_fl_opnum = get_next_op_number(); |
4978 | 720k | zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL); |
4979 | 720k | } |
4980 | 745k | } |
4981 | | |
4982 | | /* Compile ns call. */ |
4983 | 4.09M | zend_op *opline = get_next_op(); |
4984 | 4.09M | opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME; |
4985 | 4.09M | opline->op2_type = IS_CONST; |
4986 | 4.09M | opline->op2.constant = name_constants; |
4987 | 4.09M | opline->result.num = zend_alloc_cache_slot(); |
4988 | 4.09M | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
4989 | | |
4990 | | /* Compile frameless call. */ |
4991 | 4.09M | if (frameless_function_info) { |
4992 | 719k | CG(zend_lineno) = lineno; |
4993 | | |
4994 | 719k | uint32_t jmp_end_opnum = zend_emit_jump(0); |
4995 | 719k | uint32_t jmp_fl_target = get_next_op_number(); |
4996 | | |
4997 | 719k | uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type); |
4998 | | |
4999 | 719k | zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum]; |
5000 | 719k | jmp_fl->op2.opline_num = jmp_fl_target; |
5001 | 719k | jmp_fl->extended_value = zend_alloc_cache_slot(); |
5002 | 719k | zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum]; |
5003 | 719k | SET_NODE(flf_icall->result, result); |
5004 | 719k | zend_update_jump_target_to_next(jmp_end_opnum); |
5005 | | |
5006 | 719k | CG(context).in_jmp_frameless_branch = false; |
5007 | 719k | } |
5008 | 4.09M | } |
5009 | | /* }}} */ |
5010 | | |
5011 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node); |
5012 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node); |
5013 | | static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline); |
5014 | | |
5015 | | static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */ |
5016 | 4.78k | { |
5017 | | /* Bail out if we do not have a format string. */ |
5018 | 4.78k | if (args->children < 1) { |
5019 | 79 | return FAILURE; |
5020 | 79 | } |
5021 | | |
5022 | 4.70k | zend_eval_const_expr(&args->child[0]); |
5023 | | /* Bail out if the format string is not constant. */ |
5024 | 4.70k | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
5025 | 423 | return FAILURE; |
5026 | 423 | } |
5027 | | |
5028 | 4.27k | zval *format_string = zend_ast_get_zval(args->child[0]); |
5029 | 4.27k | if (Z_TYPE_P(format_string) != IS_STRING) { |
5030 | 66 | return FAILURE; |
5031 | 66 | } |
5032 | 4.21k | if (Z_STRLEN_P(format_string) >= 256) { |
5033 | 74 | return FAILURE; |
5034 | 74 | } |
5035 | | |
5036 | 4.13k | char *p; |
5037 | 4.13k | char *end; |
5038 | 4.13k | uint32_t placeholder_count; |
5039 | | |
5040 | 4.13k | placeholder_count = 0; |
5041 | 4.13k | p = Z_STRVAL_P(format_string); |
5042 | 4.13k | end = p + Z_STRLEN_P(format_string); |
5043 | | |
5044 | 9.03k | for (;;) { |
5045 | 9.03k | p = memchr(p, '%', end - p); |
5046 | 9.03k | if (!p) { |
5047 | 3.71k | break; |
5048 | 3.71k | } |
5049 | | |
5050 | 5.32k | char *q = p + 1; |
5051 | 5.32k | if (q == end) { |
5052 | 59 | return FAILURE; |
5053 | 59 | } |
5054 | | |
5055 | 5.26k | switch (*q) { |
5056 | 3.98k | case 's': |
5057 | 4.49k | case 'd': |
5058 | 4.49k | placeholder_count++; |
5059 | 4.49k | break; |
5060 | 408 | case '%': |
5061 | 408 | break; |
5062 | 361 | default: |
5063 | 361 | return FAILURE; |
5064 | 5.26k | } |
5065 | | |
5066 | 4.90k | p = q; |
5067 | 4.90k | p++; |
5068 | 4.90k | } |
5069 | | |
5070 | | /* Bail out if the number of placeholders does not match the number of values. */ |
5071 | 3.71k | if (placeholder_count != (args->children - 1)) { |
5072 | 154 | return FAILURE; |
5073 | 154 | } |
5074 | | |
5075 | | /* Handle empty format strings. */ |
5076 | 3.56k | if (Z_STRLEN_P(format_string) == 0) { |
5077 | 45 | result->op_type = IS_CONST; |
5078 | 45 | ZVAL_EMPTY_STRING(&result->u.constant); |
5079 | | |
5080 | 45 | return SUCCESS; |
5081 | 45 | } |
5082 | | |
5083 | 3.52k | znode *elements = NULL; |
5084 | | |
5085 | 3.52k | if (placeholder_count > 0) { |
5086 | 3.13k | elements = safe_emalloc(sizeof(*elements), placeholder_count, 0); |
5087 | 3.13k | } |
5088 | | |
5089 | | /* Compile the value expressions first for error handling that is consistent |
5090 | | * with a function call: Values that fail to convert to a string may emit errors. |
5091 | | */ |
5092 | 7.84k | for (uint32_t i = 0; i < placeholder_count; i++) { |
5093 | 4.32k | zend_compile_expr(elements + i, args->child[1 + i]); |
5094 | 4.32k | } |
5095 | | |
5096 | 3.52k | uint32_t rope_elements = 0; |
5097 | 3.52k | uint32_t rope_init_lineno = -1; |
5098 | 3.52k | zend_op *opline = NULL; |
5099 | | |
5100 | 3.52k | placeholder_count = 0; |
5101 | 3.52k | p = Z_STRVAL_P(format_string); |
5102 | 3.52k | end = p + Z_STRLEN_P(format_string); |
5103 | 3.52k | char *offset = p; |
5104 | 8.10k | for (;;) { |
5105 | 8.10k | p = memchr(p, '%', end - p); |
5106 | 8.10k | if (!p) { |
5107 | 3.52k | break; |
5108 | 3.52k | } |
5109 | | |
5110 | 4.58k | char *q = p + 1; |
5111 | 4.58k | ZEND_ASSERT(q < end); |
5112 | 4.58k | ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%'); |
5113 | | |
5114 | 4.58k | if (*q == '%') { |
5115 | | /* Optimization to not create a dedicated rope element for the literal '%': |
5116 | | * Include the first '%' within the "constant" part instead of dropping the |
5117 | | * full placeholder. |
5118 | | */ |
5119 | 261 | p++; |
5120 | 261 | } |
5121 | | |
5122 | 4.58k | if (p != offset) { |
5123 | 3.06k | znode const_node; |
5124 | 3.06k | const_node.op_type = IS_CONST; |
5125 | 3.06k | ZVAL_STRINGL(&const_node.u.constant, offset, p - offset); |
5126 | 3.06k | if (rope_elements == 0) { |
5127 | 1.73k | rope_init_lineno = get_next_op_number(); |
5128 | 1.73k | } |
5129 | 3.06k | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
5130 | 3.06k | } |
5131 | | |
5132 | 4.58k | if (*q != '%') { |
5133 | 4.32k | switch (*q) { |
5134 | 3.91k | case 's': |
5135 | | /* Perform the cast of constants when actually evaluating the corresponding placeholder |
5136 | | * for correct error reporting. |
5137 | | */ |
5138 | 3.91k | if (elements[placeholder_count].op_type == IS_CONST) { |
5139 | 579 | if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) { |
5140 | 56 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING; |
5141 | 523 | } else { |
5142 | 523 | convert_to_string(&elements[placeholder_count].u.constant); |
5143 | 523 | } |
5144 | 579 | } |
5145 | 3.91k | break; |
5146 | 412 | case 'd': |
5147 | 412 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG; |
5148 | 412 | break; |
5149 | 0 | default: ZEND_UNREACHABLE(); |
5150 | 4.32k | } |
5151 | | |
5152 | 4.32k | if (rope_elements == 0) { |
5153 | 1.42k | rope_init_lineno = get_next_op_number(); |
5154 | 1.42k | } |
5155 | 4.32k | opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]); |
5156 | | |
5157 | 4.32k | placeholder_count++; |
5158 | 4.32k | } |
5159 | | |
5160 | 4.58k | p = q; |
5161 | 4.58k | p++; |
5162 | 4.58k | offset = p; |
5163 | 4.58k | } |
5164 | 3.52k | if (end != offset) { |
5165 | | /* Add the constant part after the last placeholder. */ |
5166 | 3.11k | znode const_node; |
5167 | 3.11k | const_node.op_type = IS_CONST; |
5168 | 3.11k | ZVAL_STRINGL(&const_node.u.constant, offset, end - offset); |
5169 | 3.11k | if (rope_elements == 0) { |
5170 | 355 | rope_init_lineno = get_next_op_number(); |
5171 | 355 | } |
5172 | 3.11k | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
5173 | 3.11k | } |
5174 | 3.52k | ZEND_ASSERT(opline != NULL); |
5175 | | |
5176 | 3.52k | zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
5177 | 3.52k | zend_compile_rope_finalize(result, rope_elements, init_opline, opline); |
5178 | 3.52k | efree(elements); |
5179 | | |
5180 | 3.52k | return SUCCESS; |
5181 | 3.52k | } |
5182 | | |
5183 | | static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */ |
5184 | 4.33k | { |
5185 | | /* Special case: printf with a single constant string argument and no format specifiers. |
5186 | | * In this case, just emit ECHO and return the string length if needed. */ |
5187 | 4.33k | if (args->children == 1) { |
5188 | 1.04k | zend_eval_const_expr(&args->child[0]); |
5189 | 1.04k | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
5190 | 102 | return FAILURE; |
5191 | 102 | } |
5192 | 942 | zval *format_string = zend_ast_get_zval(args->child[0]); |
5193 | 942 | if (Z_TYPE_P(format_string) != IS_STRING) { |
5194 | 280 | return FAILURE; |
5195 | 280 | } |
5196 | | /* Check if there are any format specifiers */ |
5197 | 662 | if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) { |
5198 | | /* No format specifiers - just emit ECHO and return string length */ |
5199 | 561 | znode format_node; |
5200 | 561 | zend_compile_expr(&format_node, args->child[0]); |
5201 | 561 | zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL); |
5202 | | |
5203 | | /* Return the string length as a constant if the result is used */ |
5204 | 561 | result->op_type = IS_CONST; |
5205 | 561 | ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string)); |
5206 | 561 | return SUCCESS; |
5207 | 561 | } |
5208 | 662 | } |
5209 | | |
5210 | | /* Fall back to sprintf optimization for format strings with specifiers */ |
5211 | 3.39k | znode rope_result; |
5212 | 3.39k | if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) { |
5213 | 483 | return FAILURE; |
5214 | 483 | } |
5215 | | |
5216 | | /* printf() returns the amount of bytes written, so just an ECHO of the |
5217 | | * resulting sprintf() optimisation might not be enough. At this early |
5218 | | * stage we can't detect if the result is actually used, so we just emit |
5219 | | * the opcodes and let them be cleaned up by the dead code elimination |
5220 | | * pass in the Zend Optimizer if the result of the printf() is in fact |
5221 | | * unused */ |
5222 | 2.91k | znode copy; |
5223 | 2.91k | if (rope_result.op_type != IS_CONST) { |
5224 | | /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */ |
5225 | 2.74k | ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR); |
5226 | 2.74k | zend_emit_op_tmp(©, ZEND_COPY_TMP, &rope_result, NULL); |
5227 | 2.74k | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5228 | 2.74k | zend_emit_op_tmp(result, ZEND_STRLEN, ©, NULL); |
5229 | 2.74k | } else { |
5230 | 168 | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5231 | 168 | result->op_type = IS_CONST; |
5232 | 168 | ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant)); |
5233 | 168 | } |
5234 | | |
5235 | 2.91k | return SUCCESS; |
5236 | 2.91k | } |
5237 | | |
5238 | | static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args) |
5239 | 5.30k | { |
5240 | 5.30k | znode arg_node; |
5241 | | |
5242 | 5.30k | if (args->children != 1) { |
5243 | 264 | return FAILURE; |
5244 | 264 | } |
5245 | | |
5246 | 5.04k | zend_compile_expr(&arg_node, args->child[0]); |
5247 | 5.04k | zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL); |
5248 | | |
5249 | 5.04k | return SUCCESS; |
5250 | 5.30k | } |
5251 | | |
5252 | | static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ |
5253 | 587 | { |
5254 | | /* Bail out if we do not have exactly two parameters. */ |
5255 | 587 | if (args->children != 2) { |
5256 | 57 | return FAILURE; |
5257 | 57 | } |
5258 | | |
5259 | 530 | zend_ast *callback = args->child[0]; |
5260 | 530 | if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { |
5261 | 331 | return FAILURE; |
5262 | 331 | } |
5263 | | |
5264 | 199 | znode value; |
5265 | 199 | value.op_type = IS_TMP_VAR; |
5266 | 199 | value.u.op.var = get_temporary_variable(); |
5267 | | |
5268 | 199 | zend_ast *call_args = zend_partial_apply(callback, |
5269 | 199 | zend_ast_create_znode(&value)); |
5270 | 199 | if (!call_args) { |
5271 | 72 | CG(active_op_array)->T--; |
5272 | | /* The callback is not a FCC/PFA, or is not optimizable */ |
5273 | 72 | return FAILURE; |
5274 | 72 | } |
5275 | | |
5276 | 127 | zend_op *opline; |
5277 | | |
5278 | 127 | znode array; |
5279 | 127 | zend_compile_expr(&array, args->child[1]); |
5280 | | /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */ |
5281 | 127 | if (array.op_type == IS_CONST) { |
5282 | 60 | Z_TRY_ADDREF(array.u.constant); |
5283 | 60 | } |
5284 | | |
5285 | | /* Verify that the input array actually is an array. */ |
5286 | 127 | znode name; |
5287 | 127 | name.op_type = IS_CONST; |
5288 | 127 | ZVAL_STR_COPY(&name.u.constant, lcname); |
5289 | 127 | opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array); |
5290 | 127 | opline->lineno = lineno; |
5291 | 127 | opline->extended_value = (2 << 16) | IS_ARRAY; |
5292 | 127 | const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5293 | 127 | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5294 | 127 | Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData; |
5295 | | |
5296 | | /* Initialize the result array. */ |
5297 | 127 | zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
5298 | | |
5299 | | /* foreach loop starts here. */ |
5300 | 127 | znode key; |
5301 | | |
5302 | 127 | uint32_t opnum_reset = get_next_op_number(); |
5303 | 127 | znode reset_node; |
5304 | 127 | zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL); |
5305 | 127 | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
5306 | 127 | uint32_t opnum_fetch = get_next_op_number(); |
5307 | 127 | zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value); |
5308 | | |
5309 | | /* loop body */ |
5310 | 127 | znode call_result; |
5311 | 127 | switch (callback->kind) { |
5312 | 113 | case ZEND_AST_CALL: |
5313 | 113 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args)); |
5314 | 113 | break; |
5315 | 14 | case ZEND_AST_STATIC_CALL: |
5316 | 14 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args)); |
5317 | 14 | break; |
5318 | 127 | } |
5319 | 127 | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key); |
5320 | 127 | SET_NODE(opline->result, result); |
5321 | | /* end loop body */ |
5322 | | |
5323 | 127 | zend_emit_jump(opnum_fetch); |
5324 | | |
5325 | 127 | uint32_t opnum_loop_end = get_next_op_number(); |
5326 | 127 | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
5327 | 127 | opline->op2.opline_num = opnum_loop_end; |
5328 | 127 | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
5329 | 127 | opline->extended_value = opnum_loop_end; |
5330 | | |
5331 | 127 | zend_end_loop(opnum_fetch, &reset_node); |
5332 | 127 | zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
5333 | | |
5334 | 127 | return SUCCESS; |
5335 | 127 | } |
5336 | | |
5337 | | static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */ |
5338 | 153k | { |
5339 | 153k | if (zend_string_equals_literal(lcname, "strlen")) { |
5340 | 1.07k | return zend_compile_func_strlen(result, args); |
5341 | 152k | } else if (zend_string_equals_literal(lcname, "is_null")) { |
5342 | 359 | return zend_compile_func_typecheck(result, args, IS_NULL); |
5343 | 152k | } else if (zend_string_equals_literal(lcname, "is_bool")) { |
5344 | 278 | return zend_compile_func_typecheck(result, args, _IS_BOOL); |
5345 | 152k | } else if (zend_string_equals_literal(lcname, "is_long") |
5346 | 152k | || zend_string_equals_literal(lcname, "is_int") |
5347 | 151k | || zend_string_equals_literal(lcname, "is_integer") |
5348 | 152k | ) { |
5349 | 405 | return zend_compile_func_typecheck(result, args, IS_LONG); |
5350 | 151k | } else if (zend_string_equals_literal(lcname, "is_float") |
5351 | 151k | || zend_string_equals_literal(lcname, "is_double") |
5352 | 151k | ) { |
5353 | 634 | return zend_compile_func_typecheck(result, args, IS_DOUBLE); |
5354 | 151k | } else if (zend_string_equals_literal(lcname, "is_string")) { |
5355 | 43 | return zend_compile_func_typecheck(result, args, IS_STRING); |
5356 | 151k | } else if (zend_string_equals_literal(lcname, "is_array")) { |
5357 | 325 | return zend_compile_func_typecheck(result, args, IS_ARRAY); |
5358 | 150k | } else if (zend_string_equals_literal(lcname, "is_object")) { |
5359 | 141 | return zend_compile_func_typecheck(result, args, IS_OBJECT); |
5360 | 150k | } else if (zend_string_equals_literal(lcname, "is_resource")) { |
5361 | 168 | return zend_compile_func_typecheck(result, args, IS_RESOURCE); |
5362 | 150k | } else if (zend_string_equals_literal(lcname, "is_scalar")) { |
5363 | 313 | return zend_compile_func_is_scalar(result, args); |
5364 | 150k | } else if (zend_string_equals_literal(lcname, "boolval")) { |
5365 | 161 | return zend_compile_func_cast(result, args, _IS_BOOL); |
5366 | 150k | } else if (zend_string_equals_literal(lcname, "intval")) { |
5367 | 127 | return zend_compile_func_cast(result, args, IS_LONG); |
5368 | 149k | } else if (zend_string_equals_literal(lcname, "floatval") |
5369 | 149k | || zend_string_equals_literal(lcname, "doubleval") |
5370 | 149k | ) { |
5371 | 564 | return zend_compile_func_cast(result, args, IS_DOUBLE); |
5372 | 149k | } else if (zend_string_equals_literal(lcname, "strval")) { |
5373 | 373 | return zend_compile_func_cast(result, args, IS_STRING); |
5374 | 148k | } else if (zend_string_equals_literal(lcname, "defined")) { |
5375 | 2.35k | return zend_compile_func_defined(result, args); |
5376 | 146k | } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) { |
5377 | 1.12k | return zend_compile_func_chr(result, args); |
5378 | 145k | } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) { |
5379 | 662 | return zend_compile_func_ord(result, args); |
5380 | 144k | } else if (zend_string_equals_literal(lcname, "call_user_func_array")) { |
5381 | 2.43k | return zend_compile_func_cufa(result, args, lcname, type); |
5382 | 142k | } else if (zend_string_equals_literal(lcname, "call_user_func")) { |
5383 | 8.47k | return zend_compile_func_cuf(result, args, lcname, type); |
5384 | 133k | } else if (zend_string_equals_literal(lcname, "in_array")) { |
5385 | 2.88k | return zend_compile_func_in_array(result, args); |
5386 | 131k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT)) |
5387 | 129k | || zend_string_equals_literal(lcname, "sizeof")) { |
5388 | 1.74k | return zend_compile_func_count(result, args, lcname); |
5389 | 129k | } else if (zend_string_equals_literal(lcname, "get_class")) { |
5390 | 1.53k | return zend_compile_func_get_class(result, args); |
5391 | 127k | } else if (zend_string_equals_literal(lcname, "get_called_class")) { |
5392 | 283 | return zend_compile_func_get_called_class(result, args); |
5393 | 127k | } else if (zend_string_equals_literal(lcname, "gettype")) { |
5394 | 692 | return zend_compile_func_gettype(result, args); |
5395 | 126k | } else if (zend_string_equals_literal(lcname, "func_num_args")) { |
5396 | 262 | return zend_compile_func_num_args(result, args); |
5397 | 126k | } else if (zend_string_equals_literal(lcname, "func_get_args")) { |
5398 | 711 | return zend_compile_func_get_args(result, args); |
5399 | 125k | } else if (zend_string_equals_literal(lcname, "array_slice")) { |
5400 | 2.57k | return zend_compile_func_array_slice(result, args); |
5401 | 123k | } else if (zend_string_equals_literal(lcname, "array_key_exists")) { |
5402 | 336 | return zend_compile_func_array_key_exists(result, args); |
5403 | 122k | } else if (zend_string_equals_literal(lcname, "sprintf")) { |
5404 | 1.38k | return zend_compile_func_sprintf(result, args); |
5405 | 121k | } else if (zend_string_equals_literal(lcname, "printf")) { |
5406 | 4.33k | return zend_compile_func_printf(result, args); |
5407 | 117k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) { |
5408 | 5.30k | return zend_compile_func_clone(result, args); |
5409 | 111k | } else if (zend_string_equals_literal(lcname, "array_map")) { |
5410 | 587 | return zend_compile_func_array_map(result, args, lcname, lineno); |
5411 | 111k | } else { |
5412 | 111k | return FAILURE; |
5413 | 111k | } |
5414 | 153k | } |
5415 | | |
5416 | | 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) /* {{{ */ |
5417 | 183k | { |
5418 | 183k | if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) { |
5419 | 0 | return FAILURE; |
5420 | 0 | } |
5421 | | |
5422 | 183k | if (fbc->type != ZEND_INTERNAL_FUNCTION) { |
5423 | | /* If the function is part of disabled_functions, it may be redeclared as a userland |
5424 | | * function with a different implementation. Don't use the VM builtin in that case. */ |
5425 | 28.9k | return FAILURE; |
5426 | 28.9k | } |
5427 | | |
5428 | 154k | if (zend_args_contain_unpack_or_named(args)) { |
5429 | 1.04k | return FAILURE; |
5430 | 1.04k | } |
5431 | | |
5432 | 153k | if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) { |
5433 | 31.3k | return SUCCESS; |
5434 | 31.3k | } |
5435 | | |
5436 | 122k | return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE; |
5437 | 153k | } |
5438 | | |
5439 | 5 | static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) { |
5440 | 5 | switch (kind) { |
5441 | 0 | case ZEND_PROPERTY_HOOK_GET: |
5442 | 0 | return "get"; |
5443 | 5 | case ZEND_PROPERTY_HOOK_SET: |
5444 | 5 | return "set"; |
5445 | 0 | default: ZEND_UNREACHABLE(); |
5446 | 5 | } |
5447 | 5 | } |
5448 | | |
5449 | | static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name) |
5450 | 727 | { |
5451 | 727 | if (ZSTR_VAL(prop_name)[0] != '\0') { |
5452 | 422 | return zend_string_copy(prop_name); |
5453 | 422 | } else { |
5454 | 305 | const char *unmangled = zend_get_unmangled_property_name(prop_name); |
5455 | 305 | return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false); |
5456 | 305 | } |
5457 | 727 | } |
5458 | | |
5459 | | static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type) |
5460 | 16.6k | { |
5461 | 16.6k | ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL); |
5462 | | |
5463 | 16.6k | const zend_ast *class_ast = ast->child[0]; |
5464 | 16.6k | zend_ast *method_ast = ast->child[1]; |
5465 | | |
5466 | 16.6k | if (!zend_ast_is_parent_hook_call(ast)) { |
5467 | 15.8k | return false; |
5468 | 15.8k | } |
5469 | | |
5470 | 763 | zend_class_entry *ce = CG(active_class_entry); |
5471 | 763 | if (!ce) { |
5472 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active"); |
5473 | 8 | } |
5474 | | |
5475 | 755 | zend_ast *args_ast = ast->child[2]; |
5476 | 755 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
5477 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call"); |
5478 | 6 | } |
5479 | | |
5480 | 749 | zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]); |
5481 | 749 | zend_string *property_name = zval_get_string(property_hook_name_zv); |
5482 | 749 | zend_string *hook_name = zend_ast_get_str(method_ast); |
5483 | 749 | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name); |
5484 | 749 | ZEND_ASSERT(hook_kind != (uint32_t)-1); |
5485 | | |
5486 | 749 | const zend_string *prop_info_name = CG(context).active_property_info_name; |
5487 | 749 | if (!prop_info_name) { |
5488 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook", |
5489 | 5 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name)); |
5490 | 5 | } |
5491 | | |
5492 | 744 | const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name); |
5493 | 744 | if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) { |
5494 | 24 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)", |
5495 | 24 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name); |
5496 | 24 | } |
5497 | 720 | if (hook_kind != CG(context).active_property_hook_kind) { |
5498 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)", |
5499 | 5 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind)); |
5500 | 5 | } |
5501 | | |
5502 | 715 | zend_op *opline = get_next_op(); |
5503 | 715 | opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL; |
5504 | 715 | opline->op1_type = IS_CONST; |
5505 | 715 | opline->op1.constant = zend_add_literal_string(&property_name); |
5506 | 715 | opline->op2.num = hook_kind; |
5507 | | |
5508 | 715 | zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R); |
5509 | | |
5510 | 715 | return true; |
5511 | 720 | } |
5512 | | |
5513 | | static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
5514 | 4.45M | { |
5515 | 4.45M | zend_ast *name_ast = ast->child[0]; |
5516 | 4.45M | zend_ast *args_ast = ast->child[1]; |
5517 | 4.45M | bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT; |
5518 | | |
5519 | 4.45M | znode name_node; |
5520 | | |
5521 | 4.45M | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
5522 | 45.6k | zend_compile_expr(&name_node, name_ast); |
5523 | 45.6k | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5524 | 45.6k | return; |
5525 | 45.6k | } |
5526 | | |
5527 | 4.40M | { |
5528 | 4.40M | bool runtime_resolution = zend_compile_function_name(&name_node, name_ast); |
5529 | 4.40M | if (runtime_resolution) { |
5530 | 4.12M | if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert") |
5531 | 29.2k | && !is_callable_convert) { |
5532 | 29.1k | zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type); |
5533 | 4.09M | } else { |
5534 | 4.09M | zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type); |
5535 | 4.09M | } |
5536 | 4.12M | return; |
5537 | 4.12M | } |
5538 | 4.40M | } |
5539 | | |
5540 | 283k | { |
5541 | 283k | const zval *name = &name_node.u.constant; |
5542 | 283k | zend_string *lcname = zend_string_tolower(Z_STR_P(name)); |
5543 | 283k | zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5544 | 283k | const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL; |
5545 | 283k | zend_op *opline; |
5546 | | |
5547 | | /* Special assert() handling should apply independently of compiler flags. */ |
5548 | 283k | if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) { |
5549 | 13.0k | zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type); |
5550 | 13.0k | zend_string_release(lcname); |
5551 | 13.0k | zval_ptr_dtor(&name_node.u.constant); |
5552 | 13.0k | return; |
5553 | 13.0k | } |
5554 | | |
5555 | 270k | if (!fbc |
5556 | 185k | || !fbc_is_finalized(fbc) |
5557 | 185k | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
5558 | 85.2k | zend_string_release_ex(lcname, 0); |
5559 | 85.2k | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5560 | 85.2k | return; |
5561 | 85.2k | } |
5562 | | |
5563 | 185k | if (!is_callable_convert && |
5564 | 183k | zend_try_compile_special_func(result, lcname, |
5565 | 183k | zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS |
5566 | 185k | ) { |
5567 | 33.2k | zend_string_release_ex(lcname, 0); |
5568 | 33.2k | zval_ptr_dtor(&name_node.u.constant); |
5569 | 33.2k | return; |
5570 | 33.2k | } |
5571 | | |
5572 | 152k | zval_ptr_dtor(&name_node.u.constant); |
5573 | 152k | ZVAL_STR(&name_node.u.constant, lcname); |
5574 | | |
5575 | 152k | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
5576 | 152k | opline->result.num = zend_alloc_cache_slot(); |
5577 | | |
5578 | | /* Store offset to function from symbol table in op2.extra. */ |
5579 | 152k | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
5580 | 122k | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5581 | 122k | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData; |
5582 | 122k | } |
5583 | | |
5584 | 152k | zend_compile_call_common(result, args_ast, fbc, ast->lineno, type); |
5585 | 152k | } |
5586 | 152k | } |
5587 | | /* }}} */ |
5588 | | |
5589 | | static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5590 | 211k | { |
5591 | 211k | zend_ast *obj_ast = ast->child[0]; |
5592 | 211k | zend_ast *method_ast = ast->child[1]; |
5593 | 211k | zend_ast *args_ast = ast->child[2]; |
5594 | | |
5595 | 211k | znode obj_node, method_node; |
5596 | 211k | zend_op *opline; |
5597 | 211k | const zend_function *fbc = NULL; |
5598 | 211k | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL; |
5599 | 211k | uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint(); |
5600 | | |
5601 | 211k | if (is_this_fetch(obj_ast)) { |
5602 | 1.80k | if (this_guaranteed_exists()) { |
5603 | 1.29k | obj_node.op_type = IS_UNUSED; |
5604 | 1.29k | } else { |
5605 | 501 | zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
5606 | 501 | } |
5607 | 1.80k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
5608 | | |
5609 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
5610 | | * check for a nullsafe access. */ |
5611 | 209k | } else { |
5612 | 209k | zend_short_circuiting_mark_inner(obj_ast); |
5613 | 209k | zend_compile_expr(&obj_node, obj_ast); |
5614 | 209k | if (nullsafe) { |
5615 | 3.05k | zend_emit_jmp_null(&obj_node, type); |
5616 | 3.05k | } |
5617 | 209k | } |
5618 | | |
5619 | 211k | zend_compile_expr(&method_node, method_ast); |
5620 | 211k | opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL); |
5621 | | |
5622 | 211k | if (method_node.op_type == IS_CONST) { |
5623 | 210k | if (Z_TYPE(method_node.u.constant) != IS_STRING) { |
5624 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5625 | 10 | } |
5626 | | |
5627 | 210k | opline->op2_type = IS_CONST; |
5628 | 210k | opline->op2.constant = zend_add_func_name_literal( |
5629 | 210k | Z_STR(method_node.u.constant)); |
5630 | 210k | opline->result.num = zend_alloc_cache_slots(2); |
5631 | 210k | } else { |
5632 | 1.20k | SET_NODE(opline->op2, &method_node); |
5633 | 1.20k | } |
5634 | | |
5635 | | /* Check if this calls a known method on $this */ |
5636 | 211k | if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST && |
5637 | 1.13k | CG(active_class_entry) && zend_is_scope_known()) { |
5638 | 979 | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5639 | 979 | fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname); |
5640 | | |
5641 | | /* We only know the exact method that is being called if it is either private or final. |
5642 | | * Otherwise an overriding method in a child class may be called. */ |
5643 | 979 | if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) { |
5644 | 417 | fbc = NULL; |
5645 | 417 | } |
5646 | 979 | } |
5647 | | |
5648 | 211k | if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) { |
5649 | 538 | if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) { |
5650 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
5651 | 10 | "Cannot combine nullsafe operator with Closure creation"); |
5652 | 10 | } |
5653 | 538 | } |
5654 | 211k | } |
5655 | | /* }}} */ |
5656 | | |
5657 | | static bool is_func_accessible(const zend_function *fbc) |
5658 | 44.2k | { |
5659 | 44.2k | if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) { |
5660 | 44.1k | return true; |
5661 | 44.1k | } |
5662 | | |
5663 | 119 | if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE) |
5664 | 53 | && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED) |
5665 | 53 | && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED)) |
5666 | 18 | && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) { |
5667 | 0 | return true; |
5668 | 0 | } |
5669 | | |
5670 | 119 | return false; |
5671 | 119 | } |
5672 | | |
5673 | | static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */ |
5674 | 5.37k | { |
5675 | 5.37k | const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname); |
5676 | | |
5677 | 5.37k | if (!fbc || is_func_accessible(fbc)) { |
5678 | 5.29k | return fbc; |
5679 | 5.29k | } |
5680 | | |
5681 | 77 | return NULL; |
5682 | 5.37k | } |
5683 | | /* }}} */ |
5684 | | |
5685 | | static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5686 | 16.6k | { |
5687 | 16.6k | zend_ast *class_ast = ast->child[0]; |
5688 | 16.6k | zend_ast *method_ast = ast->child[1]; |
5689 | 16.6k | zend_ast *args_ast = ast->child[2]; |
5690 | | |
5691 | 16.6k | znode class_node, method_node; |
5692 | 16.6k | zend_op *opline; |
5693 | 16.6k | const zend_function *fbc = NULL; |
5694 | | |
5695 | 16.6k | if (zend_compile_parent_property_hook_call(result, ast, type)) { |
5696 | 715 | return; |
5697 | 715 | } |
5698 | | |
5699 | 15.9k | zend_short_circuiting_mark_inner(class_ast); |
5700 | 15.9k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5701 | | |
5702 | 15.9k | zend_compile_expr(&method_node, method_ast); |
5703 | | |
5704 | 15.9k | if (method_node.op_type == IS_CONST) { |
5705 | 14.6k | zval *name = &method_node.u.constant; |
5706 | 14.6k | if (Z_TYPE_P(name) != IS_STRING) { |
5707 | 4 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5708 | 4 | } |
5709 | 14.6k | if (zend_is_constructor(Z_STR_P(name))) { |
5710 | 262 | zval_ptr_dtor(name); |
5711 | 262 | method_node.op_type = IS_UNUSED; |
5712 | 262 | } |
5713 | 14.6k | } |
5714 | | |
5715 | 15.9k | opline = get_next_op(); |
5716 | 15.9k | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
5717 | | |
5718 | 15.9k | zend_set_class_name_op1(opline, &class_node); |
5719 | | |
5720 | 15.9k | if (method_node.op_type == IS_CONST) { |
5721 | 14.3k | opline->op2_type = IS_CONST; |
5722 | 14.3k | opline->op2.constant = zend_add_func_name_literal( |
5723 | 14.3k | Z_STR(method_node.u.constant)); |
5724 | 14.3k | opline->result.num = zend_alloc_cache_slots(2); |
5725 | 14.3k | } else { |
5726 | 1.54k | if (opline->op1_type == IS_CONST) { |
5727 | 316 | opline->result.num = zend_alloc_cache_slot(); |
5728 | 316 | } |
5729 | 1.54k | SET_NODE(opline->op2, &method_node); |
5730 | 1.54k | } |
5731 | | |
5732 | | /* Check if we already know which method we're calling */ |
5733 | 15.9k | if (opline->op2_type == IS_CONST) { |
5734 | 14.3k | zend_class_entry *ce = NULL; |
5735 | 14.3k | if (opline->op1_type == IS_CONST) { |
5736 | 9.15k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5737 | 9.15k | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5738 | 9.15k | if (ce) { |
5739 | 4.86k | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5740 | 0 | ce = NULL; |
5741 | 0 | } |
5742 | 4.86k | } else if (CG(active_class_entry) |
5743 | 648 | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5744 | 169 | ce = CG(active_class_entry); |
5745 | 169 | } |
5746 | 9.15k | } else if (opline->op1_type == IS_UNUSED |
5747 | 1.81k | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5748 | 723 | && zend_is_scope_known()) { |
5749 | 341 | ce = CG(active_class_entry); |
5750 | 341 | } |
5751 | 14.3k | if (ce) { |
5752 | 5.37k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5753 | 5.37k | fbc = zend_get_compatible_func_or_null(ce, lcname); |
5754 | 5.37k | } |
5755 | 14.3k | } |
5756 | | |
5757 | 15.9k | zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type); |
5758 | 15.9k | } |
5759 | | /* }}} */ |
5760 | | |
5761 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel); |
5762 | | |
5763 | | static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */ |
5764 | 229k | { |
5765 | 229k | zend_ast *class_ast = ast->child[0]; |
5766 | 229k | zend_ast *args_ast = ast->child[1]; |
5767 | | |
5768 | 229k | znode class_node, ctor_result; |
5769 | 229k | zend_op *opline; |
5770 | | |
5771 | 229k | if (class_ast->kind == ZEND_AST_CLASS) { |
5772 | | /* anon class declaration */ |
5773 | 143k | zend_compile_class_decl(&class_node, class_ast, false); |
5774 | 143k | } else { |
5775 | 85.9k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5776 | 85.9k | } |
5777 | | |
5778 | 229k | opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL); |
5779 | | |
5780 | 229k | zend_set_class_name_op1(opline, &class_node); |
5781 | | |
5782 | 229k | if (opline->op1_type == IS_CONST) { |
5783 | 84.7k | opline->op2.num = zend_alloc_cache_slot(); |
5784 | 84.7k | } |
5785 | | |
5786 | 229k | zend_class_entry *ce = NULL; |
5787 | 229k | if (opline->op1_type == IS_CONST) { |
5788 | 84.7k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5789 | 84.7k | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5790 | 84.7k | if (ce) { |
5791 | 56.9k | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5792 | 0 | ce = NULL; |
5793 | 0 | } |
5794 | 56.9k | } else if (CG(active_class_entry) |
5795 | 1.11k | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5796 | 503 | ce = CG(active_class_entry); |
5797 | 503 | } |
5798 | 144k | } else if (opline->op1_type == IS_UNUSED |
5799 | 494 | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5800 | 262 | && zend_is_scope_known()) { |
5801 | 165 | ce = CG(active_class_entry); |
5802 | 165 | } |
5803 | | |
5804 | | |
5805 | 229k | const zend_function *fbc = NULL; |
5806 | 229k | if (ce |
5807 | 57.6k | && ce->default_object_handlers->get_constructor == zend_std_get_constructor |
5808 | 57.4k | && ce->constructor |
5809 | 39.7k | && is_func_accessible(ce->constructor)) { |
5810 | 39.6k | fbc = ce->constructor; |
5811 | 39.6k | } |
5812 | | |
5813 | 229k | zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R); |
5814 | 229k | zend_do_free(&ctor_result); |
5815 | 229k | } |
5816 | | /* }}} */ |
5817 | | |
5818 | | static void zend_compile_global_var(zend_ast *ast) /* {{{ */ |
5819 | 2.32k | { |
5820 | 2.32k | zend_ast *var_ast = ast->child[0]; |
5821 | 2.32k | zend_ast *name_ast = var_ast->child[0]; |
5822 | | |
5823 | 2.32k | znode name_node, result; |
5824 | | |
5825 | 2.32k | zend_compile_expr(&name_node, name_ast); |
5826 | 2.32k | if (name_node.op_type == IS_CONST) { |
5827 | 1.97k | convert_to_string(&name_node.u.constant); |
5828 | 1.97k | } |
5829 | | |
5830 | | // TODO(GLOBALS) Forbid "global $GLOBALS"? |
5831 | 2.32k | if (is_this_fetch(var_ast)) { |
5832 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable"); |
5833 | 2.31k | } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) { |
5834 | 1.89k | zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node); |
5835 | 1.89k | opline->extended_value = zend_alloc_cache_slot(); |
5836 | 1.89k | } else { |
5837 | | /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W |
5838 | | * to not free the name_node operand, so it can be reused in the following |
5839 | | * ASSIGN_REF, which then frees it. */ |
5840 | 418 | zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL); |
5841 | 418 | opline->extended_value = ZEND_FETCH_GLOBAL_LOCK; |
5842 | | |
5843 | 418 | if (name_node.op_type == IS_CONST) { |
5844 | 75 | zend_string_addref(Z_STR(name_node.u.constant)); |
5845 | 75 | } |
5846 | | |
5847 | 418 | zend_emit_assign_ref_znode( |
5848 | 418 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)), |
5849 | 418 | &result |
5850 | 418 | ); |
5851 | 418 | } |
5852 | 2.32k | } |
5853 | | /* }}} */ |
5854 | | |
5855 | | static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */ |
5856 | 66.0k | { |
5857 | 66.0k | zend_op *opline; |
5858 | 66.0k | if (!CG(active_op_array)->static_variables) { |
5859 | 0 | if (CG(active_op_array)->scope) { |
5860 | 0 | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5861 | 0 | } |
5862 | 0 | CG(active_op_array)->static_variables = zend_new_array(8); |
5863 | 0 | } |
5864 | | |
5865 | 66.0k | value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value); |
5866 | | |
5867 | 66.0k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5868 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5869 | 0 | } |
5870 | | |
5871 | 66.0k | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL); |
5872 | 66.0k | opline->op1_type = IS_CV; |
5873 | 66.0k | opline->op1.var = lookup_cv(var_name); |
5874 | 66.0k | opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode; |
5875 | 66.0k | } |
5876 | | /* }}} */ |
5877 | | |
5878 | | static void zend_compile_static_var(zend_ast *ast) /* {{{ */ |
5879 | 6.00k | { |
5880 | 6.00k | zend_ast *var_ast = ast->child[0]; |
5881 | 6.00k | zend_string *var_name = zend_ast_get_str(var_ast); |
5882 | | |
5883 | 6.00k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5884 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5885 | 5 | } |
5886 | | |
5887 | 5.99k | if (!CG(active_op_array)->static_variables) { |
5888 | 5.65k | if (CG(active_op_array)->scope) { |
5889 | 4.49k | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5890 | 4.49k | } |
5891 | 5.65k | CG(active_op_array)->static_variables = zend_new_array(8); |
5892 | 5.65k | } |
5893 | | |
5894 | 5.99k | if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) { |
5895 | 13 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name); |
5896 | 13 | } |
5897 | | |
5898 | 5.98k | zend_eval_const_expr(&ast->child[1]); |
5899 | 5.98k | zend_ast *value_ast = ast->child[1]; |
5900 | | |
5901 | 5.98k | if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) { |
5902 | 5.39k | zval *value_zv = value_ast |
5903 | 5.39k | ? zend_ast_get_zval(value_ast) |
5904 | 5.39k | : &EG(uninitialized_zval); |
5905 | 5.39k | Z_TRY_ADDREF_P(value_zv); |
5906 | 5.39k | zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF); |
5907 | 5.39k | } else { |
5908 | 590 | zend_op *opline; |
5909 | | |
5910 | 590 | zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval)); |
5911 | 590 | uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData); |
5912 | | |
5913 | 590 | uint32_t static_def_jmp_opnum = get_next_op_number(); |
5914 | 590 | opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL); |
5915 | 590 | opline->op1_type = IS_CV; |
5916 | 590 | opline->op1.var = lookup_cv(var_name); |
5917 | 590 | opline->extended_value = placeholder_offset; |
5918 | | |
5919 | 590 | znode expr; |
5920 | 590 | zend_compile_expr(&expr, value_ast); |
5921 | | |
5922 | 590 | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr); |
5923 | 590 | opline->op1_type = IS_CV; |
5924 | 590 | opline->op1.var = lookup_cv(var_name); |
5925 | 590 | opline->extended_value = placeholder_offset | ZEND_BIND_REF; |
5926 | | |
5927 | 590 | zend_update_jump_target_to_next(static_def_jmp_opnum); |
5928 | 590 | } |
5929 | 5.98k | } |
5930 | | /* }}} */ |
5931 | | |
5932 | | static void zend_compile_unset(const zend_ast *ast) /* {{{ */ |
5933 | 7.97k | { |
5934 | 7.97k | zend_ast *var_ast = ast->child[0]; |
5935 | 7.97k | znode var_node; |
5936 | 7.97k | zend_op *opline; |
5937 | | |
5938 | 7.97k | zend_ensure_writable_variable(var_ast); |
5939 | | |
5940 | 7.97k | if (is_global_var_fetch(var_ast)) { |
5941 | 307 | if (!var_ast->child[1]) { |
5942 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
5943 | 5 | } |
5944 | | |
5945 | 302 | zend_compile_expr(&var_node, var_ast->child[1]); |
5946 | 302 | if (var_node.op_type == IS_CONST) { |
5947 | 226 | convert_to_string(&var_node.u.constant); |
5948 | 226 | } |
5949 | | |
5950 | 302 | opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL); |
5951 | 302 | opline->extended_value = ZEND_FETCH_GLOBAL; |
5952 | 302 | return; |
5953 | 307 | } |
5954 | | |
5955 | 7.67k | switch (var_ast->kind) { |
5956 | 3.73k | case ZEND_AST_VAR: |
5957 | 3.73k | if (is_this_fetch(var_ast)) { |
5958 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this"); |
5959 | 3.72k | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) { |
5960 | 3.51k | opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL); |
5961 | 3.51k | } else { |
5962 | 210 | opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false); |
5963 | 210 | opline->opcode = ZEND_UNSET_VAR; |
5964 | 210 | } |
5965 | 3.72k | return; |
5966 | 3.72k | case ZEND_AST_DIM: |
5967 | 2.54k | opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false); |
5968 | 2.54k | opline->opcode = ZEND_UNSET_DIM; |
5969 | 2.54k | return; |
5970 | 1.35k | case ZEND_AST_PROP: |
5971 | 1.35k | case ZEND_AST_NULLSAFE_PROP: |
5972 | 1.35k | opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false); |
5973 | 1.35k | opline->opcode = ZEND_UNSET_OBJ; |
5974 | 1.35k | return; |
5975 | 32 | case ZEND_AST_STATIC_PROP: |
5976 | 32 | opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false); |
5977 | 32 | opline->opcode = ZEND_UNSET_STATIC_PROP; |
5978 | 32 | return; |
5979 | 0 | default: ZEND_UNREACHABLE(); |
5980 | 7.67k | } |
5981 | 7.67k | } |
5982 | | /* }}} */ |
5983 | | |
5984 | | static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */ |
5985 | 424k | { |
5986 | 424k | const zend_loop_var *base; |
5987 | 424k | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
5988 | | |
5989 | 424k | if (!loop_var) { |
5990 | 2.25k | return true; |
5991 | 2.25k | } |
5992 | 421k | base = zend_stack_base(&CG(loop_var_stack)); |
5993 | 429k | for (; loop_var >= base; loop_var--) { |
5994 | 428k | if (loop_var->opcode == ZEND_FAST_CALL) { |
5995 | 2.24k | zend_op *opline = get_next_op(); |
5996 | | |
5997 | 2.24k | opline->opcode = ZEND_FAST_CALL; |
5998 | 2.24k | opline->result_type = IS_TMP_VAR; |
5999 | 2.24k | opline->result.var = loop_var->var_num; |
6000 | 2.24k | if (return_value) { |
6001 | 890 | SET_NODE(opline->op2, return_value); |
6002 | 890 | } |
6003 | 2.24k | opline->op1.num = loop_var->try_catch_offset; |
6004 | 425k | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
6005 | 1.63k | zend_op *opline = get_next_op(); |
6006 | 1.63k | opline->opcode = ZEND_DISCARD_EXCEPTION; |
6007 | 1.63k | opline->op1_type = IS_TMP_VAR; |
6008 | 1.63k | opline->op1.var = loop_var->var_num; |
6009 | 424k | } else if (loop_var->opcode == ZEND_RETURN) { |
6010 | | /* Stack separator */ |
6011 | 418k | break; |
6012 | 418k | } else if (depth <= 1) { |
6013 | 2.31k | return 1; |
6014 | 3.73k | } else if (loop_var->opcode == ZEND_NOP) { |
6015 | | /* Loop doesn't have freeable variable */ |
6016 | 1.98k | depth--; |
6017 | 1.98k | } else { |
6018 | 1.74k | zend_op *opline; |
6019 | | |
6020 | 1.74k | ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR)); |
6021 | 1.74k | opline = get_next_op(); |
6022 | 1.74k | opline->opcode = loop_var->opcode; |
6023 | 1.74k | opline->op1_type = loop_var->var_type; |
6024 | 1.74k | opline->op1.var = loop_var->var_num; |
6025 | 1.74k | opline->extended_value = ZEND_FREE_ON_RETURN; |
6026 | 1.74k | depth--; |
6027 | 1.74k | } |
6028 | 428k | } |
6029 | 419k | return (depth == 0); |
6030 | 421k | } |
6031 | | /* }}} */ |
6032 | | |
6033 | | static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */ |
6034 | 421k | { |
6035 | 421k | return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value); |
6036 | 421k | } |
6037 | | /* }}} */ |
6038 | | |
6039 | | static bool zend_has_finally_ex(zend_long depth) /* {{{ */ |
6040 | 1.41k | { |
6041 | 1.41k | const zend_loop_var *base; |
6042 | 1.41k | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
6043 | | |
6044 | 1.41k | if (!loop_var) { |
6045 | 281 | return false; |
6046 | 281 | } |
6047 | 1.13k | base = zend_stack_base(&CG(loop_var_stack)); |
6048 | 2.25k | for (; loop_var >= base; loop_var--) { |
6049 | 2.20k | if (loop_var->opcode == ZEND_FAST_CALL) { |
6050 | 254 | return 1; |
6051 | 1.95k | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
6052 | 1.07k | } else if (loop_var->opcode == ZEND_RETURN) { |
6053 | | /* Stack separator */ |
6054 | 826 | return 0; |
6055 | 826 | } else if (depth <= 1) { |
6056 | 0 | return 0; |
6057 | 245 | } else { |
6058 | 245 | depth--; |
6059 | 245 | } |
6060 | 2.20k | } |
6061 | 50 | return false; |
6062 | 1.13k | } |
6063 | | /* }}} */ |
6064 | | |
6065 | | static bool zend_has_finally(void) /* {{{ */ |
6066 | 1.41k | { |
6067 | 1.41k | return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1); |
6068 | 1.41k | } |
6069 | | /* }}} */ |
6070 | | |
6071 | | static void zend_compile_return(const zend_ast *ast) /* {{{ */ |
6072 | 420k | { |
6073 | 420k | zend_ast *expr_ast = ast->child[0]; |
6074 | 420k | bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0; |
6075 | 420k | bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
6076 | | |
6077 | 420k | znode expr_node; |
6078 | 420k | zend_op *opline; |
6079 | | |
6080 | 420k | if (is_generator) { |
6081 | | /* For generators the by-ref flag refers to yields, not returns */ |
6082 | 268k | by_ref = false; |
6083 | 268k | } |
6084 | | |
6085 | 420k | if (!expr_ast) { |
6086 | 2.50k | expr_node.op_type = IS_CONST; |
6087 | 2.50k | ZVAL_NULL(&expr_node.u.constant); |
6088 | 417k | } else if (by_ref && zend_is_variable_or_call(expr_ast)) { |
6089 | 112k | zend_assert_not_short_circuited(expr_ast); |
6090 | 112k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
6091 | 305k | } else { |
6092 | 305k | zend_compile_expr(&expr_node, expr_ast); |
6093 | 305k | } |
6094 | | |
6095 | 420k | if (expr_ast) { |
6096 | 417k | if (CG(active_class_entry) != NULL) { |
6097 | 11.6k | if (zend_is_constructor(CG(active_op_array)->function_name)) { |
6098 | 24 | zend_error(E_DEPRECATED, "Returning a value from a constructor is deprecated"); |
6099 | 11.6k | } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { |
6100 | 11 | zend_error(E_DEPRECATED, "Returning a value from a destructor is deprecated"); |
6101 | 11 | } |
6102 | 11.6k | } |
6103 | 417k | } |
6104 | | |
6105 | 420k | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) |
6106 | 2.24k | && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR)) |
6107 | 1.41k | && zend_has_finally()) { |
6108 | | /* Copy return value into temporary VAR to avoid modification in finally code */ |
6109 | 254 | if (by_ref) { |
6110 | 102 | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
6111 | 152 | } else { |
6112 | 152 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL); |
6113 | 152 | } |
6114 | 254 | } |
6115 | | |
6116 | | /* Generator return types are handled separately */ |
6117 | 420k | if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6118 | 9.12k | zend_emit_return_type_check( |
6119 | 9.12k | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6120 | 9.12k | } |
6121 | | |
6122 | 420k | uint32_t opnum_before_finally = get_next_op_number(); |
6123 | | |
6124 | 420k | zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL); |
6125 | | |
6126 | | /* Content of reference might have changed in finally, repeat type check. */ |
6127 | 420k | if (by_ref |
6128 | | /* Check if any opcodes were emitted since the last return type check. */ |
6129 | 115k | && opnum_before_finally != get_next_op_number() |
6130 | 1.08k | && !is_generator |
6131 | 1.08k | && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6132 | 47 | zend_emit_return_type_check( |
6133 | 47 | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6134 | 47 | } |
6135 | | |
6136 | 420k | opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN, |
6137 | 420k | &expr_node, NULL); |
6138 | | |
6139 | 420k | if (by_ref && expr_ast) { |
6140 | 113k | if (zend_is_call(expr_ast)) { |
6141 | 110k | opline->extended_value = ZEND_RETURNS_FUNCTION; |
6142 | 110k | } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) { |
6143 | 1.57k | opline->extended_value = ZEND_RETURNS_VALUE; |
6144 | 1.57k | } |
6145 | 113k | } |
6146 | 420k | } |
6147 | | /* }}} */ |
6148 | | |
6149 | | static void zend_compile_void_cast(znode *result, const zend_ast *ast) |
6150 | 658 | { |
6151 | 658 | zend_ast *expr_ast = ast->child[0]; |
6152 | 658 | znode expr_node; |
6153 | 658 | zend_op *opline; |
6154 | | |
6155 | 658 | zend_compile_expr(&expr_node, expr_ast); |
6156 | | |
6157 | 658 | switch (expr_node.op_type) { |
6158 | 255 | case IS_TMP_VAR: |
6159 | 255 | case IS_VAR: |
6160 | 255 | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6161 | 255 | opline->extended_value = ZEND_FREE_VOID_CAST; |
6162 | 255 | break; |
6163 | 317 | case IS_CONST: |
6164 | 317 | zend_do_free(&expr_node); |
6165 | 317 | break; |
6166 | 658 | } |
6167 | 658 | } |
6168 | | |
6169 | | static void zend_compile_echo(const zend_ast *ast) /* {{{ */ |
6170 | 2.87M | { |
6171 | 2.87M | zend_op *opline; |
6172 | 2.87M | zend_ast *expr_ast = ast->child[0]; |
6173 | | |
6174 | 2.87M | znode expr_node; |
6175 | 2.87M | zend_compile_expr(&expr_node, expr_ast); |
6176 | | |
6177 | 2.87M | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
6178 | 2.87M | opline->extended_value = 0; |
6179 | 2.87M | } |
6180 | | /* }}} */ |
6181 | | |
6182 | | static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */ |
6183 | 3.47k | { |
6184 | 3.47k | zend_ast *expr_ast = ast->child[0]; |
6185 | | |
6186 | 3.47k | znode expr_node; |
6187 | 3.47k | zend_compile_expr(&expr_node, expr_ast); |
6188 | | |
6189 | 3.47k | zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL); |
6190 | 3.47k | if (result) { |
6191 | | /* Mark this as an "expression throw" for opcache. */ |
6192 | 615 | opline->extended_value = ZEND_THROW_IS_EXPR; |
6193 | 615 | result->op_type = IS_CONST; |
6194 | 615 | ZVAL_TRUE(&result->u.constant); |
6195 | 615 | } |
6196 | 3.47k | } |
6197 | | /* }}} */ |
6198 | | |
6199 | | static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */ |
6200 | 2.43k | { |
6201 | 2.43k | zend_ast *depth_ast = ast->child[0]; |
6202 | | |
6203 | 2.43k | zend_op *opline; |
6204 | 2.43k | zend_long depth; |
6205 | | |
6206 | 2.43k | ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE); |
6207 | | |
6208 | 2.43k | if (depth_ast) { |
6209 | 432 | const zval *depth_zv; |
6210 | 432 | if (depth_ast->kind != ZEND_AST_ZVAL) { |
6211 | 17 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand " |
6212 | 17 | "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6213 | 17 | } |
6214 | | |
6215 | 415 | depth_zv = zend_ast_get_zval(depth_ast); |
6216 | 415 | if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) { |
6217 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers", |
6218 | 12 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6219 | 12 | } |
6220 | | |
6221 | 403 | depth = Z_LVAL_P(depth_zv); |
6222 | 2.00k | } else { |
6223 | 2.00k | depth = 1; |
6224 | 2.00k | } |
6225 | | |
6226 | 2.41k | if (CG(context).current_brk_cont == -1) { |
6227 | 29 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context", |
6228 | 29 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6229 | 2.38k | } else { |
6230 | 2.38k | if (!zend_handle_loops_and_finally_ex(depth, NULL)) { |
6231 | 71 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s", |
6232 | 71 | ast->kind == ZEND_AST_BREAK ? "break" : "continue", |
6233 | 71 | depth, depth == 1 ? "" : "s"); |
6234 | 71 | } |
6235 | 2.38k | } |
6236 | | |
6237 | 2.31k | if (ast->kind == ZEND_AST_CONTINUE) { |
6238 | 1.20k | int d, cur = CG(context).current_brk_cont; |
6239 | 1.36k | for (d = depth - 1; d > 0; d--) { |
6240 | 158 | cur = CG(context).brk_cont_array[cur].parent; |
6241 | 158 | ZEND_ASSERT(cur != -1); |
6242 | 158 | } |
6243 | | |
6244 | 1.20k | if (CG(context).brk_cont_array[cur].is_switch) { |
6245 | 615 | if (depth == 1) { |
6246 | 526 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6247 | 304 | zend_error(E_WARNING, |
6248 | 304 | "\"continue\" targeting switch is equivalent to \"break\""); |
6249 | 304 | } else { |
6250 | 222 | zend_error(E_WARNING, |
6251 | 222 | "\"continue\" targeting switch is equivalent to \"break\". " \ |
6252 | 222 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6253 | 222 | depth + 1); |
6254 | 222 | } |
6255 | 526 | } else { |
6256 | 89 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6257 | 62 | zend_error(E_WARNING, |
6258 | 62 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"", |
6259 | 62 | depth, depth); |
6260 | 62 | } else { |
6261 | 27 | zend_error(E_WARNING, |
6262 | 27 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \ |
6263 | 27 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6264 | 27 | depth, depth, depth + 1); |
6265 | 27 | } |
6266 | 89 | } |
6267 | 615 | } |
6268 | 1.20k | } |
6269 | | |
6270 | 2.31k | opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL); |
6271 | 2.31k | opline->op1.num = CG(context).current_brk_cont; |
6272 | 2.31k | opline->op2.num = depth; |
6273 | 2.31k | } |
6274 | | /* }}} */ |
6275 | | |
6276 | | void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */ |
6277 | 1.52k | { |
6278 | 1.52k | zend_label *dest; |
6279 | 1.52k | int remove_oplines = opline->op1.num; |
6280 | 1.52k | zval *label; |
6281 | 1.52k | uint32_t opnum = opline - op_array->opcodes; |
6282 | | |
6283 | 1.52k | label = CT_CONSTANT_EX(op_array, opline->op2.constant); |
6284 | 1.52k | if (CG(context).labels == NULL || |
6285 | 1.50k | (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL |
6286 | 1.52k | ) { |
6287 | 46 | CG(in_compilation) = 1; |
6288 | 46 | CG(active_op_array) = op_array; |
6289 | 46 | CG(zend_lineno) = opline->lineno; |
6290 | 46 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label)); |
6291 | 46 | } |
6292 | | |
6293 | 1.47k | zval_ptr_dtor_str(label); |
6294 | 1.47k | ZVAL_NULL(label); |
6295 | | |
6296 | 1.47k | uint32_t current = opline->extended_value; |
6297 | 3.25k | for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) { |
6298 | 1.79k | if (current == -1) { |
6299 | 10 | CG(in_compilation) = 1; |
6300 | 10 | CG(active_op_array) = op_array; |
6301 | 10 | CG(zend_lineno) = opline->lineno; |
6302 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed"); |
6303 | 10 | } |
6304 | 1.78k | if (CG(context).brk_cont_array[current].start >= 0) { |
6305 | 1.11k | remove_oplines--; |
6306 | 1.11k | } |
6307 | 1.78k | } |
6308 | | |
6309 | 3.03k | for (current = 0; current < op_array->last_try_catch; ++current) { |
6310 | 1.86k | const zend_try_catch_element *elem = &op_array->try_catch_array[current]; |
6311 | 1.86k | if (elem->try_op > opnum) { |
6312 | 298 | break; |
6313 | 298 | } |
6314 | 1.56k | if (elem->finally_op && opnum < elem->finally_op - 1 |
6315 | 1.02k | && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op) |
6316 | 1.56k | ) { |
6317 | 524 | remove_oplines--; |
6318 | 524 | } |
6319 | 1.56k | } |
6320 | | |
6321 | 1.46k | opline->opcode = ZEND_JMP; |
6322 | 1.46k | SET_UNUSED(opline->op1); |
6323 | 1.46k | SET_UNUSED(opline->op2); |
6324 | 1.46k | SET_UNUSED(opline->result); |
6325 | 1.46k | opline->op1.opline_num = dest->opline_num; |
6326 | 1.46k | opline->extended_value = 0; |
6327 | | |
6328 | 1.46k | ZEND_ASSERT(remove_oplines >= 0); |
6329 | 2.02k | while (remove_oplines--) { |
6330 | 555 | opline--; |
6331 | 555 | MAKE_NOP(opline); |
6332 | 555 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
6333 | 555 | } |
6334 | 1.46k | } |
6335 | | /* }}} */ |
6336 | | |
6337 | | static void zend_compile_goto(const zend_ast *ast) /* {{{ */ |
6338 | 1.70k | { |
6339 | 1.70k | zend_ast *label_ast = ast->child[0]; |
6340 | 1.70k | znode label_node; |
6341 | 1.70k | zend_op *opline; |
6342 | | |
6343 | 1.70k | zend_compile_expr(&label_node, label_ast); |
6344 | | |
6345 | | /* Label resolution and unwinding adjustments happen in pass two. */ |
6346 | 1.70k | uint32_t opnum_start = get_next_op_number(); |
6347 | 1.70k | zend_handle_loops_and_finally(NULL); |
6348 | 1.70k | opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node); |
6349 | 1.70k | opline->op1.num = get_next_op_number() - opnum_start - 1; |
6350 | 1.70k | opline->extended_value = CG(context).current_brk_cont; |
6351 | 1.70k | } |
6352 | | /* }}} */ |
6353 | | |
6354 | | static void zend_compile_label(const zend_ast *ast) /* {{{ */ |
6355 | 3.09k | { |
6356 | 3.09k | zend_string *label = zend_ast_get_str(ast->child[0]); |
6357 | 3.09k | zend_label dest; |
6358 | | |
6359 | 3.09k | if (!CG(context).labels) { |
6360 | 2.50k | ALLOC_HASHTABLE(CG(context).labels); |
6361 | 2.50k | zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0); |
6362 | 2.50k | } |
6363 | | |
6364 | 3.09k | dest.brk_cont = CG(context).current_brk_cont; |
6365 | 3.09k | dest.opline_num = get_next_op_number(); |
6366 | | |
6367 | 3.09k | if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) { |
6368 | 52 | zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label)); |
6369 | 52 | } |
6370 | 3.09k | } |
6371 | | /* }}} */ |
6372 | | |
6373 | | static void zend_compile_while(const zend_ast *ast) /* {{{ */ |
6374 | 2.21k | { |
6375 | 2.21k | zend_ast *cond_ast = ast->child[0]; |
6376 | 2.21k | zend_ast *stmt_ast = ast->child[1]; |
6377 | 2.21k | znode cond_node; |
6378 | 2.21k | uint32_t opnum_start, opnum_jmp, opnum_cond; |
6379 | | |
6380 | 2.21k | opnum_jmp = zend_emit_jump(0); |
6381 | | |
6382 | 2.21k | zend_begin_loop(ZEND_NOP, NULL, false); |
6383 | | |
6384 | 2.21k | opnum_start = get_next_op_number(); |
6385 | 2.21k | zend_compile_stmt(stmt_ast); |
6386 | | |
6387 | 2.21k | opnum_cond = get_next_op_number(); |
6388 | 2.21k | zend_update_jump_target(opnum_jmp, opnum_cond); |
6389 | 2.21k | zend_compile_expr(&cond_node, cond_ast); |
6390 | | |
6391 | 2.21k | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6392 | | |
6393 | 2.21k | zend_end_loop(opnum_cond, NULL); |
6394 | 2.21k | } |
6395 | | /* }}} */ |
6396 | | |
6397 | | static void zend_compile_do_while(const zend_ast *ast) /* {{{ */ |
6398 | 571 | { |
6399 | 571 | zend_ast *stmt_ast = ast->child[0]; |
6400 | 571 | zend_ast *cond_ast = ast->child[1]; |
6401 | | |
6402 | 571 | znode cond_node; |
6403 | 571 | uint32_t opnum_start, opnum_cond; |
6404 | | |
6405 | 571 | zend_begin_loop(ZEND_NOP, NULL, false); |
6406 | | |
6407 | 571 | opnum_start = get_next_op_number(); |
6408 | 571 | zend_compile_stmt(stmt_ast); |
6409 | | |
6410 | 571 | opnum_cond = get_next_op_number(); |
6411 | 571 | zend_compile_expr(&cond_node, cond_ast); |
6412 | | |
6413 | 571 | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6414 | | |
6415 | 571 | zend_end_loop(opnum_cond, NULL); |
6416 | 571 | } |
6417 | | /* }}} */ |
6418 | | |
6419 | | static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */ |
6420 | 41.5k | { |
6421 | 41.5k | const zend_ast_list *list; |
6422 | 41.5k | uint32_t i; |
6423 | | |
6424 | 41.5k | result->op_type = IS_CONST; |
6425 | 41.5k | ZVAL_TRUE(&result->u.constant); |
6426 | | |
6427 | 41.5k | if (!ast) { |
6428 | 7.59k | return; |
6429 | 7.59k | } |
6430 | | |
6431 | 33.9k | list = zend_ast_get_list(ast); |
6432 | 69.9k | for (i = 0; i < list->children; ++i) { |
6433 | 36.0k | zend_ast *expr_ast = list->child[i]; |
6434 | | |
6435 | 36.0k | zend_do_free(result); |
6436 | 36.0k | if (expr_ast->kind == ZEND_AST_CAST_VOID) { |
6437 | 117 | zend_compile_void_cast(NULL, expr_ast); |
6438 | 117 | result->op_type = IS_CONST; |
6439 | 117 | ZVAL_NULL(&result->u.constant); |
6440 | 35.8k | } else { |
6441 | 35.8k | zend_compile_expr(result, expr_ast); |
6442 | 35.8k | } |
6443 | 36.0k | } |
6444 | 33.9k | } |
6445 | | /* }}} */ |
6446 | | |
6447 | | static void zend_compile_for(const zend_ast *ast) /* {{{ */ |
6448 | 13.9k | { |
6449 | 13.9k | zend_ast *init_ast = ast->child[0]; |
6450 | 13.9k | zend_ast *cond_ast = ast->child[1]; |
6451 | 13.9k | zend_ast *loop_ast = ast->child[2]; |
6452 | 13.9k | zend_ast *stmt_ast = ast->child[3]; |
6453 | | |
6454 | 13.9k | znode result; |
6455 | 13.9k | uint32_t opnum_start, opnum_jmp, opnum_loop; |
6456 | | |
6457 | 13.9k | zend_compile_for_expr_list(&result, init_ast); |
6458 | 13.9k | zend_do_free(&result); |
6459 | | |
6460 | 13.9k | opnum_jmp = zend_emit_jump(0); |
6461 | | |
6462 | 13.9k | zend_begin_loop(ZEND_NOP, NULL, false); |
6463 | | |
6464 | 13.9k | opnum_start = get_next_op_number(); |
6465 | 13.9k | zend_compile_stmt(stmt_ast); |
6466 | | |
6467 | 13.9k | opnum_loop = get_next_op_number(); |
6468 | 13.9k | zend_compile_for_expr_list(&result, loop_ast); |
6469 | 13.9k | zend_do_free(&result); |
6470 | | |
6471 | 13.9k | zend_update_jump_target_to_next(opnum_jmp); |
6472 | 13.9k | zend_compile_for_expr_list(&result, cond_ast); |
6473 | 13.9k | zend_do_extended_stmt(NULL); |
6474 | | |
6475 | 13.9k | zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start); |
6476 | | |
6477 | 13.9k | zend_end_loop(opnum_loop, NULL); |
6478 | 13.9k | } |
6479 | | /* }}} */ |
6480 | | |
6481 | | static void zend_compile_foreach(zend_ast *ast) /* {{{ */ |
6482 | 18.8k | { |
6483 | 18.8k | zend_ast *expr_ast = ast->child[0]; |
6484 | 18.8k | zend_ast *value_ast = ast->child[1]; |
6485 | 18.8k | zend_ast *key_ast = ast->child[2]; |
6486 | 18.8k | zend_ast *stmt_ast = ast->child[3]; |
6487 | 18.8k | bool by_ref = value_ast->kind == ZEND_AST_REF; |
6488 | 18.8k | bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast)) |
6489 | 3.54k | || zend_is_call(expr_ast); |
6490 | | |
6491 | 18.8k | znode expr_node, reset_node, value_node, key_node; |
6492 | 18.8k | zend_op *opline; |
6493 | 18.8k | uint32_t opnum_reset, opnum_fetch; |
6494 | | |
6495 | 18.8k | if (key_ast) { |
6496 | 2.23k | if (key_ast->kind == ZEND_AST_REF) { |
6497 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference"); |
6498 | 6 | } |
6499 | 2.23k | if (key_ast->kind == ZEND_AST_ARRAY) { |
6500 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element"); |
6501 | 9 | } |
6502 | 2.23k | } |
6503 | | |
6504 | 18.8k | if (by_ref) { |
6505 | 1.67k | value_ast = value_ast->child[0]; |
6506 | 1.67k | } |
6507 | | |
6508 | 18.8k | if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) { |
6509 | 76 | by_ref = true; |
6510 | 76 | } |
6511 | | |
6512 | 18.8k | if (by_ref && is_variable) { |
6513 | 1.39k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
6514 | 17.4k | } else { |
6515 | 17.4k | zend_compile_expr(&expr_node, expr_ast); |
6516 | 17.4k | } |
6517 | | |
6518 | 18.8k | if (by_ref) { |
6519 | 1.75k | zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W); |
6520 | 1.75k | } |
6521 | | |
6522 | 18.8k | opnum_reset = get_next_op_number(); |
6523 | 18.8k | opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL); |
6524 | 18.8k | if (!by_ref) { |
6525 | 17.0k | opline->result_type = IS_TMP_VAR; |
6526 | 17.0k | reset_node.op_type = IS_TMP_VAR; |
6527 | 17.0k | } |
6528 | | |
6529 | 18.8k | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
6530 | | |
6531 | 18.8k | opnum_fetch = get_next_op_number(); |
6532 | 18.8k | opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL); |
6533 | | |
6534 | 18.8k | if (is_this_fetch(value_ast)) { |
6535 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
6536 | 18.8k | } else if (value_ast->kind == ZEND_AST_VAR && |
6537 | 18.1k | zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) { |
6538 | 18.0k | SET_NODE(opline->op2, &value_node); |
6539 | 18.0k | } else { |
6540 | 733 | opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR; |
6541 | 733 | opline->op2.var = get_temporary_variable(); |
6542 | 733 | GET_NODE(&value_node, opline->op2); |
6543 | 733 | if (value_ast->kind == ZEND_AST_ARRAY) { |
6544 | 405 | zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R); |
6545 | 405 | } else if (by_ref) { |
6546 | 156 | zend_emit_assign_ref_znode(value_ast, &value_node); |
6547 | 172 | } else { |
6548 | 172 | zend_emit_assign_znode(value_ast, &value_node); |
6549 | 172 | } |
6550 | 733 | } |
6551 | | |
6552 | 18.8k | if (key_ast) { |
6553 | 2.21k | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6554 | 2.21k | zend_make_tmp_result(&key_node, opline); |
6555 | 2.21k | zend_emit_assign_znode(key_ast, &key_node); |
6556 | 2.21k | } |
6557 | | |
6558 | 18.8k | zend_compile_stmt(stmt_ast); |
6559 | | |
6560 | | /* Place JMP and FE_FREE on the line where foreach starts. It would be |
6561 | | * better to use the end line, but this information is not available |
6562 | | * currently. */ |
6563 | 18.8k | CG(zend_lineno) = ast->lineno; |
6564 | 18.8k | zend_emit_jump(opnum_fetch); |
6565 | | |
6566 | 18.8k | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
6567 | 18.8k | opline->op2.opline_num = get_next_op_number(); |
6568 | | |
6569 | 18.8k | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6570 | 18.8k | opline->extended_value = get_next_op_number(); |
6571 | | |
6572 | 18.8k | zend_end_loop(opnum_fetch, &reset_node); |
6573 | | |
6574 | 18.8k | opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
6575 | 18.8k | } |
6576 | | /* }}} */ |
6577 | | |
6578 | | static void zend_compile_if(zend_ast *ast) /* {{{ */ |
6579 | 45.2k | { |
6580 | 45.2k | const zend_ast_list *list = zend_ast_get_list(ast); |
6581 | 45.2k | uint32_t i; |
6582 | 45.2k | uint32_t *jmp_opnums = NULL; |
6583 | | |
6584 | 45.2k | if (list->children > 1) { |
6585 | 10.5k | jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0); |
6586 | 10.5k | } |
6587 | | |
6588 | 107k | for (i = 0; i < list->children; ++i) { |
6589 | 62.0k | const zend_ast *elem_ast = list->child[i]; |
6590 | 62.0k | zend_ast *cond_ast = elem_ast->child[0]; |
6591 | 62.0k | zend_ast *stmt_ast = elem_ast->child[1]; |
6592 | | |
6593 | 62.0k | if (cond_ast) { |
6594 | 52.9k | znode cond_node; |
6595 | 52.9k | uint32_t opnum_jmpz; |
6596 | | |
6597 | 52.9k | if (i > 0) { |
6598 | 7.62k | CG(zend_lineno) = cond_ast->lineno; |
6599 | 7.62k | zend_do_extended_stmt(NULL); |
6600 | 7.62k | } |
6601 | | |
6602 | 52.9k | zend_compile_expr(&cond_node, cond_ast); |
6603 | 52.9k | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6604 | | |
6605 | 52.9k | zend_compile_stmt(stmt_ast); |
6606 | | |
6607 | 52.9k | if (i != list->children - 1) { |
6608 | | /* Set the lineno of JMP to the position of the if keyword, as we don't want to |
6609 | | * report the last line in the if branch as covered if it hasn't actually executed. */ |
6610 | 16.7k | CG(zend_lineno) = elem_ast->lineno; |
6611 | 16.7k | jmp_opnums[i] = zend_emit_jump(0); |
6612 | 16.7k | } |
6613 | 52.9k | zend_update_jump_target_to_next(opnum_jmpz); |
6614 | 52.9k | } else { |
6615 | | /* "else" can only occur as last element. */ |
6616 | 9.16k | ZEND_ASSERT(i == list->children - 1); |
6617 | 9.16k | zend_compile_stmt(stmt_ast); |
6618 | 9.16k | } |
6619 | 62.0k | } |
6620 | | |
6621 | 45.2k | if (list->children > 1) { |
6622 | 27.2k | for (i = 0; i < list->children - 1; ++i) { |
6623 | 16.7k | zend_update_jump_target_to_next(jmp_opnums[i]); |
6624 | 16.7k | } |
6625 | 10.4k | efree(jmp_opnums); |
6626 | 10.4k | } |
6627 | 45.2k | } |
6628 | | /* }}} */ |
6629 | | |
6630 | 9.11k | static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) { |
6631 | 9.11k | uint32_t i; |
6632 | 9.11k | uint8_t common_type = IS_UNDEF; |
6633 | 16.3k | for (i = 0; i < cases->children; i++) { |
6634 | 14.4k | zend_ast *case_ast = cases->child[i]; |
6635 | 14.4k | zend_ast **cond_ast = &case_ast->child[0]; |
6636 | 14.4k | const zval *cond_zv; |
6637 | 14.4k | if (!case_ast->child[0]) { |
6638 | | /* Skip default clause */ |
6639 | 300 | continue; |
6640 | 300 | } |
6641 | | |
6642 | 14.1k | zend_eval_const_expr(cond_ast); |
6643 | 14.1k | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6644 | | /* Non-constant case */ |
6645 | 4.88k | return IS_UNDEF; |
6646 | 4.88k | } |
6647 | | |
6648 | 9.22k | cond_zv = zend_ast_get_zval(case_ast->child[0]); |
6649 | 9.22k | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6650 | | /* We only optimize switched on integers and strings */ |
6651 | 1.87k | return IS_UNDEF; |
6652 | 1.87k | } |
6653 | | |
6654 | 7.34k | if (common_type == IS_UNDEF) { |
6655 | 3.89k | common_type = Z_TYPE_P(cond_zv); |
6656 | 3.89k | } else if (common_type != Z_TYPE_P(cond_zv)) { |
6657 | | /* Non-uniform case types */ |
6658 | 32 | return IS_UNDEF; |
6659 | 32 | } |
6660 | | |
6661 | 7.31k | if (Z_TYPE_P(cond_zv) == IS_STRING |
6662 | 6.55k | && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) { |
6663 | | /* Numeric strings cannot be compared with a simple hash lookup */ |
6664 | 353 | return IS_UNDEF; |
6665 | 353 | } |
6666 | 7.31k | } |
6667 | | |
6668 | 1.97k | return common_type; |
6669 | 9.11k | } |
6670 | | |
6671 | 1.68k | static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) { |
6672 | 1.68k | if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) { |
6673 | 0 | return false; |
6674 | 0 | } |
6675 | | |
6676 | | /* Thresholds are chosen based on when the average switch time for equidistributed |
6677 | | * input becomes smaller when using the jumptable optimization. */ |
6678 | 1.68k | if (jumptable_type == IS_LONG) { |
6679 | 212 | return cases->children >= 5; |
6680 | 1.46k | } else { |
6681 | 1.46k | ZEND_ASSERT(jumptable_type == IS_STRING); |
6682 | 1.46k | return cases->children >= 2; |
6683 | 1.46k | } |
6684 | 1.68k | } |
6685 | | |
6686 | | static void zend_compile_switch(zend_ast *ast) /* {{{ */ |
6687 | 9.11k | { |
6688 | 9.11k | zend_ast *expr_ast = ast->child[0]; |
6689 | 9.11k | zend_ast_list *cases = zend_ast_get_list(ast->child[1]); |
6690 | | |
6691 | 9.11k | uint32_t i; |
6692 | 9.11k | bool has_default_case = false; |
6693 | | |
6694 | 9.11k | znode expr_node, case_node; |
6695 | 9.11k | zend_op *opline; |
6696 | 9.11k | uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1; |
6697 | 9.11k | uint8_t jumptable_type; |
6698 | 9.11k | HashTable *jumptable = NULL; |
6699 | | |
6700 | 9.11k | zend_compile_expr(&expr_node, expr_ast); |
6701 | | |
6702 | 9.11k | zend_begin_loop(ZEND_FREE, &expr_node, true); |
6703 | | |
6704 | 9.11k | case_node.op_type = IS_TMP_VAR; |
6705 | 9.11k | case_node.u.op.var = get_temporary_variable(); |
6706 | | |
6707 | 9.11k | jumptable_type = determine_switch_jumptable_type(cases); |
6708 | 9.11k | if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) { |
6709 | 1.24k | znode jumptable_op; |
6710 | | |
6711 | 1.24k | ALLOC_HASHTABLE(jumptable); |
6712 | 1.24k | zend_hash_init(jumptable, cases->children, NULL, NULL, 0); |
6713 | 1.24k | jumptable_op.op_type = IS_CONST; |
6714 | 1.24k | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
6715 | | |
6716 | 1.24k | opline = zend_emit_op(NULL, |
6717 | 1.24k | jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING, |
6718 | 1.24k | &expr_node, &jumptable_op); |
6719 | 1.24k | if (opline->op1_type == IS_CONST) { |
6720 | 523 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6721 | 523 | } |
6722 | 1.24k | opnum_switch = opline - CG(active_op_array)->opcodes; |
6723 | 1.24k | } |
6724 | | |
6725 | 9.11k | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0); |
6726 | 37.0k | for (i = 0; i < cases->children; ++i) { |
6727 | 27.9k | zend_ast *case_ast = cases->child[i]; |
6728 | 27.9k | zend_ast *cond_ast = case_ast->child[0]; |
6729 | 27.9k | znode cond_node; |
6730 | | |
6731 | 27.9k | if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) { |
6732 | 208 | CG(zend_lineno) = case_ast->lineno; |
6733 | 208 | zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead"); |
6734 | 208 | } |
6735 | | |
6736 | 27.9k | if (!cond_ast) { |
6737 | 326 | if (has_default_case) { |
6738 | 16 | CG(zend_lineno) = case_ast->lineno; |
6739 | 16 | zend_error_noreturn(E_COMPILE_ERROR, |
6740 | 16 | "Switch statements may only contain one default clause"); |
6741 | 16 | } |
6742 | 310 | has_default_case = true; |
6743 | 310 | continue; |
6744 | 326 | } |
6745 | | |
6746 | 27.6k | zend_compile_expr(&cond_node, cond_ast); |
6747 | | |
6748 | 27.6k | if (expr_node.op_type == IS_CONST |
6749 | 22.2k | && Z_TYPE(expr_node.u.constant) == IS_FALSE) { |
6750 | 14.4k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6751 | 14.4k | } else if (expr_node.op_type == IS_CONST |
6752 | 7.84k | && Z_TYPE(expr_node.u.constant) == IS_TRUE) { |
6753 | 3.58k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0); |
6754 | 9.61k | } else { |
6755 | 9.61k | opline = zend_emit_op(NULL, |
6756 | 9.61k | (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL, |
6757 | 9.61k | &expr_node, &cond_node); |
6758 | 9.61k | SET_NODE(opline->result, &case_node); |
6759 | 9.61k | if (opline->op1_type == IS_CONST) { |
6760 | 4.26k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6761 | 4.26k | } |
6762 | | |
6763 | 9.61k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
6764 | 9.61k | } |
6765 | 27.6k | } |
6766 | | |
6767 | 9.10k | opnum_default_jmp = zend_emit_jump(0); |
6768 | | |
6769 | 36.9k | for (i = 0; i < cases->children; ++i) { |
6770 | 27.8k | zend_ast *case_ast = cases->child[i]; |
6771 | 27.8k | zend_ast *cond_ast = case_ast->child[0]; |
6772 | 27.8k | zend_ast *stmt_ast = case_ast->child[1]; |
6773 | | |
6774 | 27.8k | if (cond_ast) { |
6775 | 27.5k | zend_update_jump_target_to_next(jmpnz_opnums[i]); |
6776 | | |
6777 | 27.5k | if (jumptable) { |
6778 | 3.06k | zval *cond_zv = zend_ast_get_zval(cond_ast); |
6779 | 3.06k | zval jmp_target; |
6780 | 3.06k | ZVAL_LONG(&jmp_target, get_next_op_number()); |
6781 | | |
6782 | 3.06k | ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type); |
6783 | 3.06k | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
6784 | 352 | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
6785 | 2.71k | } else { |
6786 | 2.71k | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
6787 | 2.71k | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
6788 | 2.71k | } |
6789 | 3.06k | } |
6790 | 27.5k | } else { |
6791 | 294 | zend_update_jump_target_to_next(opnum_default_jmp); |
6792 | | |
6793 | 294 | if (jumptable) { |
6794 | 49 | ZEND_ASSERT(opnum_switch != (uint32_t)-1); |
6795 | 49 | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6796 | 49 | opline->extended_value = get_next_op_number(); |
6797 | 49 | } |
6798 | 294 | } |
6799 | | |
6800 | 27.8k | zend_compile_stmt(stmt_ast); |
6801 | 27.8k | } |
6802 | | |
6803 | 9.10k | if (!has_default_case) { |
6804 | 8.80k | zend_update_jump_target_to_next(opnum_default_jmp); |
6805 | | |
6806 | 8.80k | if (jumptable) { |
6807 | 1.18k | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6808 | 1.18k | opline->extended_value = get_next_op_number(); |
6809 | 1.18k | } |
6810 | 8.80k | } |
6811 | | |
6812 | 9.10k | zend_end_loop(get_next_op_number(), &expr_node); |
6813 | | |
6814 | 9.10k | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
6815 | 1.68k | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6816 | 1.68k | opline->extended_value = ZEND_FREE_SWITCH; |
6817 | 7.41k | } else if (expr_node.op_type == IS_CONST) { |
6818 | 6.73k | zval_ptr_dtor_nogc(&expr_node.u.constant); |
6819 | 6.73k | } |
6820 | | |
6821 | 9.10k | efree(jmpnz_opnums); |
6822 | 9.10k | } |
6823 | | /* }}} */ |
6824 | | |
6825 | | static uint32_t count_match_conds(const zend_ast_list *arms) |
6826 | 2.86k | { |
6827 | 2.86k | uint32_t num_conds = 0; |
6828 | | |
6829 | 7.21k | for (uint32_t i = 0; i < arms->children; i++) { |
6830 | 4.35k | const zend_ast *arm_ast = arms->child[i]; |
6831 | 4.35k | if (arm_ast->child[0] == NULL) { |
6832 | 517 | continue; |
6833 | 517 | } |
6834 | | |
6835 | 3.84k | const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6836 | 3.84k | num_conds += conds->children; |
6837 | 3.84k | } |
6838 | | |
6839 | 2.86k | return num_conds; |
6840 | 2.86k | } |
6841 | | |
6842 | 2.86k | static bool can_match_use_jumptable(const zend_ast_list *arms) { |
6843 | 5.23k | for (uint32_t i = 0; i < arms->children; i++) { |
6844 | 3.53k | const zend_ast *arm_ast = arms->child[i]; |
6845 | 3.53k | if (!arm_ast->child[0]) { |
6846 | | /* Skip default arm */ |
6847 | 331 | continue; |
6848 | 331 | } |
6849 | | |
6850 | 3.20k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6851 | 7.70k | for (uint32_t j = 0; j < conds->children; j++) { |
6852 | 5.65k | zend_ast **cond_ast = &conds->child[j]; |
6853 | | |
6854 | 5.65k | zend_eval_const_expr(cond_ast); |
6855 | 5.65k | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6856 | 820 | return false; |
6857 | 820 | } |
6858 | | |
6859 | 4.83k | const zval *cond_zv = zend_ast_get_zval(*cond_ast); |
6860 | 4.83k | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6861 | 338 | return false; |
6862 | 338 | } |
6863 | 4.83k | } |
6864 | 3.20k | } |
6865 | | |
6866 | 1.70k | return true; |
6867 | 2.86k | } |
6868 | | |
6869 | | static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast) |
6870 | 960 | { |
6871 | 960 | if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) { |
6872 | | /* Assert compilation adds a message operand, but this is incompatible with the |
6873 | | * pipe optimization that uses a temporary znode for the reference elimination. |
6874 | | * Therefore, disable the optimization for assert. |
6875 | | * Note that "assert" as a name is always treated as fully qualified. */ |
6876 | 713 | return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert"); |
6877 | 713 | } |
6878 | | |
6879 | 247 | return true; |
6880 | 960 | } |
6881 | | |
6882 | | static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg) |
6883 | 30.5k | { |
6884 | 30.5k | if (callable_ast->kind != ZEND_AST_CALL |
6885 | 29.2k | && callable_ast->kind != ZEND_AST_STATIC_CALL |
6886 | 28.4k | && callable_ast->kind != ZEND_AST_METHOD_CALL) { |
6887 | 27.5k | return NULL; |
6888 | 27.5k | } |
6889 | | |
6890 | 2.96k | zend_ast *args_ast = zend_ast_call_get_args(callable_ast); |
6891 | 2.96k | if (!args_ast || args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { |
6892 | 955 | return NULL; |
6893 | 955 | } |
6894 | | |
6895 | 2.01k | if (callable_ast->kind == ZEND_AST_CALL && |
6896 | 960 | !zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) { |
6897 | 81 | return NULL; |
6898 | 81 | } |
6899 | | |
6900 | 1.93k | zend_ast_list *arg_list = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
6901 | | |
6902 | 1.93k | zend_ast *first_placeholder = NULL; |
6903 | 1.93k | bool uses_named_args = false; |
6904 | | |
6905 | 3.91k | for (uint32_t i = 0; i < arg_list->children; i++) { |
6906 | 2.00k | zend_ast *arg = arg_list->child[i]; |
6907 | 2.00k | if (arg->kind == ZEND_AST_NAMED_ARG) { |
6908 | 37 | uses_named_args = true; |
6909 | 37 | arg = arg->child[1]; |
6910 | 37 | } |
6911 | | |
6912 | 2.00k | if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) { |
6913 | 1.94k | if (first_placeholder == NULL) { |
6914 | 1.93k | first_placeholder = arg; |
6915 | 1.93k | } else { |
6916 | | /* A PFA with multiple placeholders is unexpected in this |
6917 | | * context, and will usually error due to a missing argument, |
6918 | | * so we don't optimize those. */ |
6919 | 12 | return NULL; |
6920 | 12 | } |
6921 | 1.93k | if (arg->attr == ZEND_PLACEHOLDER_VARIADIC && uses_named_args) { |
6922 | | /* A PFA with both a variadic placeholder and named args can not |
6923 | | * be optimized because this would result in a positional arg |
6924 | | * after a named arg: f(name: $v, ...) -> f(name: $v, pipe_arg). |
6925 | | * Arg placeholders ('?') are safe since they are not allowed |
6926 | | * after named args. */ |
6927 | 9 | return NULL; |
6928 | 9 | } |
6929 | 1.93k | } |
6930 | 2.00k | } |
6931 | | |
6932 | 1.91k | ZEND_ASSERT(first_placeholder); |
6933 | | |
6934 | 1.91k | zend_ast *new_arg_list = zend_ast_create_list(0, arg_list->kind); |
6935 | 3.87k | for (uint32_t i = 0; i < arg_list->children; i++) { |
6936 | 1.96k | zend_ast *arg = arg_list->child[i]; |
6937 | 1.96k | if (arg == first_placeholder) { |
6938 | 1.90k | new_arg_list = zend_ast_list_add(new_arg_list, pipe_arg); |
6939 | 1.90k | } else if (arg->kind == ZEND_AST_NAMED_ARG |
6940 | 16 | && arg->child[1] == first_placeholder) { |
6941 | 12 | zend_ast *name = arg->child[0]; |
6942 | 12 | new_arg_list = zend_ast_list_add(new_arg_list, |
6943 | 12 | zend_ast_create(ZEND_AST_NAMED_ARG, name, pipe_arg)); |
6944 | 48 | } else { |
6945 | 48 | new_arg_list = zend_ast_list_add(new_arg_list, arg); |
6946 | 48 | } |
6947 | 1.96k | } |
6948 | | |
6949 | 1.91k | return new_arg_list; |
6950 | 1.91k | } |
6951 | | |
6952 | | static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type) |
6953 | 30.3k | { |
6954 | 30.3k | zend_ast *operand_ast = ast->child[0]; |
6955 | 30.3k | zend_ast *callable_ast = ast->child[1]; |
6956 | | |
6957 | 30.3k | if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) { |
6958 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized"); |
6959 | 8 | } |
6960 | | |
6961 | | /* Compile the left hand side down to a value first. */ |
6962 | 30.3k | znode operand_result; |
6963 | 30.3k | zend_compile_expr(&operand_result, operand_ast); |
6964 | | |
6965 | | /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references |
6966 | | * always fail. They will already fail in complex cases like arrays, |
6967 | | * so those don't need a wrapper. */ |
6968 | 30.3k | znode wrapped_operand_result; |
6969 | 30.3k | if (operand_result.op_type & (IS_CV|IS_VAR)) { |
6970 | 133 | zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL); |
6971 | 30.2k | } else { |
6972 | 30.2k | wrapped_operand_result = operand_result; |
6973 | 30.2k | } |
6974 | | |
6975 | | /* Turn the operand into a function parameter list. */ |
6976 | 30.3k | zend_ast *arg = zend_ast_create_znode(&wrapped_operand_result); |
6977 | | |
6978 | 30.3k | zend_ast *fcall_ast; |
6979 | 30.3k | znode callable_result; |
6980 | 30.3k | zend_ast *pfa_arg_list_ast = NULL; |
6981 | | |
6982 | | /* Turn $foo |> PFA into plain function call if possible */ |
6983 | 30.3k | if ((pfa_arg_list_ast = zend_partial_apply(callable_ast, arg))) { |
6984 | 1.78k | switch (callable_ast->kind) { |
6985 | 745 | case ZEND_AST_CALL: |
6986 | 745 | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
6987 | 745 | callable_ast->child[0], pfa_arg_list_ast); |
6988 | 745 | break; |
6989 | 727 | case ZEND_AST_STATIC_CALL: |
6990 | 727 | fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL, |
6991 | 727 | callable_ast->child[0], callable_ast->child[1], |
6992 | 727 | pfa_arg_list_ast); |
6993 | 727 | break; |
6994 | 313 | case ZEND_AST_METHOD_CALL: |
6995 | 313 | fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL, |
6996 | 313 | callable_ast->child[0], callable_ast->child[1], |
6997 | 313 | pfa_arg_list_ast); |
6998 | 313 | break; |
6999 | 0 | default: |
7000 | 0 | ZEND_UNREACHABLE(); |
7001 | 1.78k | } |
7002 | | /* Turn $foo |> $expr into ($expr)($foo) */ |
7003 | 28.5k | } else { |
7004 | 28.5k | zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, arg); |
7005 | 28.5k | zend_compile_expr(&callable_result, callable_ast); |
7006 | 28.5k | callable_ast = zend_ast_create_znode(&callable_result); |
7007 | 28.5k | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
7008 | 28.5k | callable_ast, arg_list_ast); |
7009 | 28.5k | } |
7010 | | |
7011 | 30.3k | zend_do_extended_stmt(&operand_result); |
7012 | | |
7013 | 30.3k | zend_compile_var(result, fcall_ast, type, /* by_ref */ false); |
7014 | 30.3k | } |
7015 | | |
7016 | | static void zend_compile_match(znode *result, zend_ast *ast) |
7017 | 2.86k | { |
7018 | 2.86k | zend_ast *expr_ast = ast->child[0]; |
7019 | 2.86k | zend_ast_list *arms = zend_ast_get_list(ast->child[1]); |
7020 | 2.86k | bool has_default_arm = false; |
7021 | 2.86k | uint32_t opnum_match = (uint32_t)-1; |
7022 | | |
7023 | 2.86k | znode expr_node; |
7024 | 2.86k | zend_compile_expr(&expr_node, expr_ast); |
7025 | | |
7026 | 2.86k | znode case_node; |
7027 | 2.86k | case_node.op_type = IS_TMP_VAR; |
7028 | 2.86k | case_node.u.op.var = get_temporary_variable(); |
7029 | | |
7030 | 2.86k | uint32_t num_conds = count_match_conds(arms); |
7031 | 2.86k | uint8_t can_use_jumptable = can_match_use_jumptable(arms); |
7032 | 2.86k | bool uses_jumptable = can_use_jumptable && num_conds >= 2; |
7033 | 2.86k | HashTable *jumptable = NULL; |
7034 | 2.86k | uint32_t *jmpnz_opnums = NULL; |
7035 | | |
7036 | 7.20k | for (uint32_t i = 0; i < arms->children; ++i) { |
7037 | 4.35k | zend_ast *arm_ast = arms->child[i]; |
7038 | | |
7039 | 4.35k | if (!arm_ast->child[0]) { |
7040 | 517 | if (has_default_arm) { |
7041 | 5 | CG(zend_lineno) = arm_ast->lineno; |
7042 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7043 | 5 | "Match expressions may only contain one default arm"); |
7044 | 5 | } |
7045 | 512 | has_default_arm = true; |
7046 | 512 | } |
7047 | 4.35k | } |
7048 | | |
7049 | 2.85k | if (uses_jumptable) { |
7050 | 1.03k | znode jumptable_op; |
7051 | | |
7052 | 1.03k | ALLOC_HASHTABLE(jumptable); |
7053 | 1.03k | zend_hash_init(jumptable, num_conds, NULL, NULL, 0); |
7054 | 1.03k | jumptable_op.op_type = IS_CONST; |
7055 | 1.03k | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
7056 | | |
7057 | 1.03k | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op); |
7058 | 1.03k | if (opline->op1_type == IS_CONST) { |
7059 | 448 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7060 | 448 | } |
7061 | 1.03k | opnum_match = opline - CG(active_op_array)->opcodes; |
7062 | 1.82k | } else { |
7063 | 1.82k | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0); |
7064 | 1.82k | uint32_t cond_count = 0; |
7065 | 4.28k | for (uint32_t i = 0; i < arms->children; ++i) { |
7066 | 2.46k | zend_ast *arm_ast = arms->child[i]; |
7067 | | |
7068 | 2.46k | if (!arm_ast->child[0]) { |
7069 | 373 | continue; |
7070 | 373 | } |
7071 | | |
7072 | 2.08k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
7073 | 5.91k | for (uint32_t j = 0; j < conds->children; j++) { |
7074 | 3.83k | zend_ast *cond_ast = conds->child[j]; |
7075 | | |
7076 | 3.83k | znode cond_node; |
7077 | 3.83k | zend_compile_expr(&cond_node, cond_ast); |
7078 | | |
7079 | 3.83k | uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL; |
7080 | 3.83k | zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node); |
7081 | 3.83k | SET_NODE(opline->result, &case_node); |
7082 | 3.83k | if (opline->op1_type == IS_CONST) { |
7083 | 2.36k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7084 | 2.36k | } |
7085 | | |
7086 | 3.83k | jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
7087 | | |
7088 | 3.83k | cond_count++; |
7089 | 3.83k | } |
7090 | 2.08k | } |
7091 | 1.82k | } |
7092 | | |
7093 | 2.85k | uint32_t opnum_default_jmp = 0; |
7094 | 2.85k | if (!uses_jumptable) { |
7095 | 1.81k | opnum_default_jmp = zend_emit_jump(0); |
7096 | 1.81k | } |
7097 | | |
7098 | 2.85k | bool is_first_case = true; |
7099 | 2.85k | uint32_t cond_count = 0; |
7100 | 2.85k | uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0); |
7101 | | |
7102 | | // The generated default arm is emitted first to avoid live range issues where the tmpvar |
7103 | | // for the arm result is freed even though it has not been initialized yet. |
7104 | 2.85k | if (!has_default_arm) { |
7105 | 2.34k | if (!uses_jumptable) { |
7106 | 1.44k | zend_update_jump_target_to_next(opnum_default_jmp); |
7107 | 1.44k | } |
7108 | | |
7109 | 2.34k | if (jumptable) { |
7110 | 901 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
7111 | 901 | opline->extended_value = get_next_op_number(); |
7112 | 901 | } |
7113 | | |
7114 | 2.34k | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL); |
7115 | 2.34k | if (opline->op1_type == IS_CONST) { |
7116 | 1.09k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7117 | 1.09k | } |
7118 | 2.34k | if (arms->children == 0) { |
7119 | | /* Mark this as an "expression throw" for opcache. */ |
7120 | 451 | opline->extended_value = ZEND_THROW_IS_EXPR; |
7121 | 451 | } |
7122 | 2.34k | } |
7123 | | |
7124 | 7.18k | for (uint32_t i = 0; i < arms->children; ++i) { |
7125 | 4.33k | zend_ast *arm_ast = arms->child[i]; |
7126 | 4.33k | zend_ast *body_ast = arm_ast->child[1]; |
7127 | | |
7128 | 4.33k | CG(zend_lineno) = zend_ast_get_lineno(arm_ast); |
7129 | | |
7130 | 4.33k | if (arm_ast->child[0] != NULL) { |
7131 | 3.82k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
7132 | | |
7133 | 11.2k | for (uint32_t j = 0; j < conds->children; j++) { |
7134 | 7.45k | zend_ast *cond_ast = conds->child[j]; |
7135 | | |
7136 | 7.45k | if (jmpnz_opnums != NULL) { |
7137 | 3.83k | zend_update_jump_target_to_next(jmpnz_opnums[cond_count]); |
7138 | 3.83k | } |
7139 | | |
7140 | 7.45k | if (jumptable) { |
7141 | 3.62k | zval *cond_zv = zend_ast_get_zval(cond_ast); |
7142 | 3.62k | zval jmp_target; |
7143 | 3.62k | ZVAL_LONG(&jmp_target, get_next_op_number()); |
7144 | | |
7145 | 3.62k | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
7146 | 3.18k | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
7147 | 3.18k | } else { |
7148 | 439 | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
7149 | 439 | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
7150 | 439 | } |
7151 | 3.62k | } |
7152 | | |
7153 | 7.45k | cond_count++; |
7154 | 7.45k | } |
7155 | 3.82k | } else { |
7156 | 507 | if (!uses_jumptable) { |
7157 | 373 | zend_update_jump_target_to_next(opnum_default_jmp); |
7158 | 373 | } |
7159 | | |
7160 | 507 | if (jumptable) { |
7161 | 134 | ZEND_ASSERT(opnum_match != (uint32_t)-1); |
7162 | 134 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
7163 | 134 | opline->extended_value = get_next_op_number(); |
7164 | 134 | } |
7165 | 507 | } |
7166 | | |
7167 | 4.33k | znode body_node; |
7168 | 4.33k | zend_compile_expr(&body_node, body_ast); |
7169 | | |
7170 | 4.33k | if (is_first_case) { |
7171 | 2.40k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL); |
7172 | 2.40k | is_first_case = false; |
7173 | 2.40k | } else { |
7174 | 1.93k | zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL); |
7175 | 1.93k | SET_NODE(opline_qm_assign->result, result); |
7176 | 1.93k | } |
7177 | | |
7178 | 4.33k | jmp_end_opnums[i] = zend_emit_jump(0); |
7179 | 4.33k | } |
7180 | | |
7181 | | // Initialize result in case there is no arm |
7182 | 2.85k | if (arms->children == 0) { |
7183 | 451 | result->op_type = IS_CONST; |
7184 | 451 | ZVAL_NULL(&result->u.constant); |
7185 | 451 | } |
7186 | | |
7187 | 7.18k | for (uint32_t i = 0; i < arms->children; ++i) { |
7188 | 4.33k | zend_update_jump_target_to_next(jmp_end_opnums[i]); |
7189 | 4.33k | } |
7190 | | |
7191 | 2.85k | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
7192 | 868 | zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
7193 | 868 | opline->extended_value = ZEND_FREE_SWITCH; |
7194 | 1.98k | } else if (expr_node.op_type == IS_CONST) { |
7195 | 1.33k | zval_ptr_dtor_nogc(&expr_node.u.constant); |
7196 | 1.33k | } |
7197 | | |
7198 | 2.85k | if (jmpnz_opnums != NULL) { |
7199 | 1.81k | efree(jmpnz_opnums); |
7200 | 1.81k | } |
7201 | 2.85k | efree(jmp_end_opnums); |
7202 | 2.85k | } |
7203 | | |
7204 | | static void zend_compile_try(const zend_ast *ast) /* {{{ */ |
7205 | 35.5k | { |
7206 | 35.5k | zend_ast *try_ast = ast->child[0]; |
7207 | 35.5k | const zend_ast_list *catches = zend_ast_get_list(ast->child[1]); |
7208 | 35.5k | zend_ast *finally_ast = ast->child[2]; |
7209 | | |
7210 | 35.5k | uint32_t i, j; |
7211 | 35.5k | zend_op *opline; |
7212 | 35.5k | uint32_t try_catch_offset; |
7213 | 35.5k | uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0); |
7214 | 35.5k | uint32_t orig_fast_call_var = CG(context).fast_call_var; |
7215 | 35.5k | uint32_t orig_try_catch_offset = CG(context).try_catch_offset; |
7216 | | |
7217 | 35.5k | if (catches->children == 0 && !finally_ast) { |
7218 | 79 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally"); |
7219 | 79 | } |
7220 | | |
7221 | | /* label: try { } must not be equal to try { label: } */ |
7222 | 35.5k | if (CG(context).labels) { |
7223 | 2.28k | zend_label *label; |
7224 | 2.28k | ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) { |
7225 | 2.28k | if (label->opline_num == get_next_op_number()) { |
7226 | 793 | zend_emit_op(NULL, ZEND_NOP, NULL, NULL); |
7227 | 793 | } |
7228 | 2.28k | break; |
7229 | 6.86k | } ZEND_HASH_FOREACH_END(); |
7230 | 2.28k | } |
7231 | | |
7232 | 35.5k | try_catch_offset = zend_add_try_element(get_next_op_number()); |
7233 | | |
7234 | 35.5k | if (finally_ast) { |
7235 | 2.78k | zend_loop_var fast_call; |
7236 | 2.78k | if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) { |
7237 | 1.37k | CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK; |
7238 | 1.37k | } |
7239 | 2.78k | CG(context).fast_call_var = get_temporary_variable(); |
7240 | | |
7241 | | /* Push FAST_CALL on unwind stack */ |
7242 | 2.78k | fast_call.opcode = ZEND_FAST_CALL; |
7243 | 2.78k | fast_call.var_type = IS_TMP_VAR; |
7244 | 2.78k | fast_call.var_num = CG(context).fast_call_var; |
7245 | 2.78k | fast_call.try_catch_offset = try_catch_offset; |
7246 | 2.78k | zend_stack_push(&CG(loop_var_stack), &fast_call); |
7247 | 2.78k | } |
7248 | | |
7249 | 35.5k | CG(context).try_catch_offset = try_catch_offset; |
7250 | | |
7251 | 35.5k | zend_compile_stmt(try_ast); |
7252 | | |
7253 | 35.5k | if (catches->children != 0) { |
7254 | 32.8k | jmp_opnums[0] = zend_emit_jump(0); |
7255 | 32.8k | } |
7256 | | |
7257 | 73.6k | for (i = 0; i < catches->children; ++i) { |
7258 | 38.1k | const zend_ast *catch_ast = catches->child[i]; |
7259 | 38.1k | const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]); |
7260 | 38.1k | zend_ast *var_ast = catch_ast->child[1]; |
7261 | 38.1k | zend_ast *stmt_ast = catch_ast->child[2]; |
7262 | 38.1k | zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL; |
7263 | 38.1k | bool is_last_catch = (i + 1 == catches->children); |
7264 | | |
7265 | 38.1k | uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0); |
7266 | 38.1k | uint32_t opnum_catch = (uint32_t)-1; |
7267 | | |
7268 | 38.1k | CG(zend_lineno) = catch_ast->lineno; |
7269 | | |
7270 | 80.7k | for (j = 0; j < classes->children; j++) { |
7271 | 42.5k | zend_ast *class_ast = classes->child[j]; |
7272 | 42.5k | bool is_last_class = (j + 1 == classes->children); |
7273 | | |
7274 | 42.5k | if (!zend_is_const_default_class_ref(class_ast)) { |
7275 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement"); |
7276 | 7 | } |
7277 | | |
7278 | 42.5k | opnum_catch = get_next_op_number(); |
7279 | 42.5k | if (i == 0 && j == 0) { |
7280 | 32.8k | CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch; |
7281 | 32.8k | } |
7282 | | |
7283 | 42.5k | opline = get_next_op(); |
7284 | 42.5k | opline->opcode = ZEND_CATCH; |
7285 | 42.5k | opline->op1_type = IS_CONST; |
7286 | 42.5k | opline->op1.constant = zend_add_class_name_literal( |
7287 | 42.5k | zend_resolve_class_name_ast(class_ast)); |
7288 | 42.5k | opline->extended_value = zend_alloc_cache_slot(); |
7289 | | |
7290 | 42.5k | if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
7291 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
7292 | 7 | } |
7293 | | |
7294 | 42.5k | opline->result_type = var_name ? IS_CV : IS_UNUSED; |
7295 | 42.5k | opline->result.var = var_name ? lookup_cv(var_name) : -1; |
7296 | | |
7297 | 42.5k | if (is_last_catch && is_last_class) { |
7298 | 32.8k | opline->extended_value |= ZEND_LAST_CATCH; |
7299 | 32.8k | } |
7300 | | |
7301 | 42.5k | if (!is_last_class) { |
7302 | 4.40k | jmp_multicatch[j] = zend_emit_jump(0); |
7303 | 4.40k | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7304 | 4.40k | opline->op2.opline_num = get_next_op_number(); |
7305 | 4.40k | } |
7306 | 42.5k | } |
7307 | | |
7308 | 42.5k | for (j = 0; j < classes->children - 1; j++) { |
7309 | 4.40k | zend_update_jump_target_to_next(jmp_multicatch[j]); |
7310 | 4.40k | } |
7311 | | |
7312 | 38.1k | efree(jmp_multicatch); |
7313 | | |
7314 | 38.1k | zend_compile_stmt(stmt_ast); |
7315 | | |
7316 | 38.1k | if (!is_last_catch) { |
7317 | 5.25k | jmp_opnums[i + 1] = zend_emit_jump(0); |
7318 | 5.25k | } |
7319 | | |
7320 | 38.1k | ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class"); |
7321 | 38.1k | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7322 | 38.1k | if (!is_last_catch) { |
7323 | 5.25k | opline->op2.opline_num = get_next_op_number(); |
7324 | 5.25k | } |
7325 | 38.1k | } |
7326 | | |
7327 | 73.5k | for (i = 0; i < catches->children; ++i) { |
7328 | 38.1k | zend_update_jump_target_to_next(jmp_opnums[i]); |
7329 | 38.1k | } |
7330 | | |
7331 | 35.4k | if (finally_ast) { |
7332 | 2.76k | zend_loop_var discard_exception; |
7333 | 2.76k | uint32_t opnum_jmp = get_next_op_number() + 1; |
7334 | | |
7335 | | /* Pop FAST_CALL from unwind stack */ |
7336 | 2.76k | zend_stack_del_top(&CG(loop_var_stack)); |
7337 | | |
7338 | | /* Push DISCARD_EXCEPTION on unwind stack */ |
7339 | 2.76k | discard_exception.opcode = ZEND_DISCARD_EXCEPTION; |
7340 | 2.76k | discard_exception.var_type = IS_TMP_VAR; |
7341 | 2.76k | discard_exception.var_num = CG(context).fast_call_var; |
7342 | 2.76k | zend_stack_push(&CG(loop_var_stack), &discard_exception); |
7343 | | |
7344 | 2.76k | CG(zend_lineno) = finally_ast->lineno; |
7345 | | |
7346 | 2.76k | opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL); |
7347 | 2.76k | opline->op1.num = try_catch_offset; |
7348 | 2.76k | opline->result_type = IS_TMP_VAR; |
7349 | 2.76k | opline->result.var = CG(context).fast_call_var; |
7350 | | |
7351 | 2.76k | zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
7352 | | |
7353 | 2.76k | zend_compile_stmt(finally_ast); |
7354 | | |
7355 | 2.76k | CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1; |
7356 | 2.76k | CG(active_op_array)->try_catch_array[try_catch_offset].finally_end |
7357 | 2.76k | = get_next_op_number(); |
7358 | | |
7359 | 2.76k | opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL); |
7360 | 2.76k | opline->op1_type = IS_TMP_VAR; |
7361 | 2.76k | opline->op1.var = CG(context).fast_call_var; |
7362 | 2.76k | opline->op2.num = orig_try_catch_offset; |
7363 | | |
7364 | 2.76k | zend_update_jump_target_to_next(opnum_jmp); |
7365 | | |
7366 | 2.76k | CG(context).fast_call_var = orig_fast_call_var; |
7367 | | |
7368 | | /* Pop DISCARD_EXCEPTION from unwind stack */ |
7369 | 2.76k | zend_stack_del_top(&CG(loop_var_stack)); |
7370 | 2.76k | } |
7371 | | |
7372 | 35.4k | CG(context).try_catch_offset = orig_try_catch_offset; |
7373 | | |
7374 | 35.4k | efree(jmp_opnums); |
7375 | 35.4k | } |
7376 | | /* }}} */ |
7377 | | |
7378 | | /* Encoding declarations must already be handled during parsing */ |
7379 | | bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */ |
7380 | 2.85k | { |
7381 | 2.85k | const zend_ast_list *declares = zend_ast_get_list(ast); |
7382 | 2.85k | uint32_t i; |
7383 | 6.44k | for (i = 0; i < declares->children; ++i) { |
7384 | 3.61k | const zend_ast *declare_ast = declares->child[i]; |
7385 | 3.61k | zend_ast *name_ast = declare_ast->child[0]; |
7386 | 3.61k | zend_ast *value_ast = declare_ast->child[1]; |
7387 | 3.61k | const zend_string *name = zend_ast_get_str(name_ast); |
7388 | | |
7389 | 3.61k | if (zend_string_equals_literal_ci(name, "encoding")) { |
7390 | 184 | if (value_ast->kind != ZEND_AST_ZVAL) { |
7391 | 30 | zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0); |
7392 | 30 | return false; |
7393 | 30 | } |
7394 | | |
7395 | 154 | if (CG(multibyte)) { |
7396 | 0 | zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast)); |
7397 | |
|
7398 | 0 | const zend_encoding *new_encoding, *old_encoding; |
7399 | 0 | zend_encoding_filter old_input_filter; |
7400 | |
|
7401 | 0 | CG(encoding_declared) = 1; |
7402 | |
|
7403 | 0 | new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name)); |
7404 | 0 | if (!new_encoding) { |
7405 | 0 | zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name)); |
7406 | 0 | } else { |
7407 | 0 | old_input_filter = LANG_SCNG(input_filter); |
7408 | 0 | old_encoding = LANG_SCNG(script_encoding); |
7409 | 0 | zend_multibyte_set_filter(new_encoding); |
7410 | | |
7411 | | /* need to re-scan if input filter changed */ |
7412 | 0 | if (old_input_filter != LANG_SCNG(input_filter) || |
7413 | 0 | (old_input_filter && new_encoding != old_encoding)) { |
7414 | 0 | zend_multibyte_yyinput_again(old_input_filter, old_encoding); |
7415 | 0 | } |
7416 | 0 | } |
7417 | |
|
7418 | 0 | zend_string_release_ex(encoding_name, 0); |
7419 | 154 | } else { |
7420 | 154 | zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because " |
7421 | 154 | "Zend multibyte feature is turned off by settings"); |
7422 | 154 | } |
7423 | 154 | } |
7424 | 3.61k | } |
7425 | | |
7426 | 2.82k | return true; |
7427 | 2.85k | } |
7428 | | /* }}} */ |
7429 | | |
7430 | | /* Check whether this is the first statement, not counting declares. */ |
7431 | | static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */ |
7432 | 3.28k | { |
7433 | 3.28k | uint32_t i = 0; |
7434 | 3.28k | const zend_ast_list *file_ast = zend_ast_get_list(CG(ast)); |
7435 | | |
7436 | 25.6k | while (i < file_ast->children) { |
7437 | 25.6k | if (file_ast->child[i] == ast) { |
7438 | 3.22k | return SUCCESS; |
7439 | 22.4k | } else if (file_ast->child[i] == NULL) { |
7440 | 14.6k | if (!allow_nop) { |
7441 | 0 | return FAILURE; |
7442 | 0 | } |
7443 | 14.6k | } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) { |
7444 | 55 | return FAILURE; |
7445 | 55 | } |
7446 | 22.4k | i++; |
7447 | 22.4k | } |
7448 | 2 | return FAILURE; |
7449 | 3.28k | } |
7450 | | /* }}} */ |
7451 | | |
7452 | | static void zend_compile_declare(const zend_ast *ast) /* {{{ */ |
7453 | 5.88k | { |
7454 | 5.88k | const zend_ast_list *declares = zend_ast_get_list(ast->child[0]); |
7455 | 5.88k | zend_ast *stmt_ast = ast->child[1]; |
7456 | 5.88k | zend_declarables orig_declarables = FC(declarables); |
7457 | 5.88k | uint32_t i; |
7458 | | |
7459 | 13.4k | for (i = 0; i < declares->children; ++i) { |
7460 | 7.67k | zend_ast *declare_ast = declares->child[i]; |
7461 | 7.67k | zend_ast *name_ast = declare_ast->child[0]; |
7462 | 7.67k | zend_ast **value_ast_ptr = &declare_ast->child[1]; |
7463 | 7.67k | zend_string *name = zend_ast_get_str(name_ast); |
7464 | | |
7465 | 7.67k | if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) { |
7466 | 24 | zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name)); |
7467 | 24 | } |
7468 | | |
7469 | 7.65k | if (zend_string_equals_literal_ci(name, "ticks")) { |
7470 | 5.39k | zval value_zv; |
7471 | 5.39k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7472 | 5.39k | FC(declarables).ticks = zval_get_long(&value_zv); |
7473 | 5.39k | zval_ptr_dtor_nogc(&value_zv); |
7474 | 5.39k | } else if (zend_string_equals_literal_ci(name, "encoding")) { |
7475 | | |
7476 | 78 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) { |
7477 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be " |
7478 | 6 | "the very first statement in the script"); |
7479 | 6 | } |
7480 | 2.18k | } else if (zend_string_equals_literal_ci(name, "strict_types")) { |
7481 | 494 | zval value_zv; |
7482 | | |
7483 | 494 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
7484 | 22 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be " |
7485 | 22 | "the very first statement in the script"); |
7486 | 22 | } |
7487 | | |
7488 | 472 | if (ast->child[1] != NULL) { |
7489 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not " |
7490 | 11 | "use block mode"); |
7491 | 11 | } |
7492 | | |
7493 | 461 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7494 | | |
7495 | 461 | if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) { |
7496 | 48 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value"); |
7497 | 48 | } |
7498 | | |
7499 | 413 | if (Z_LVAL(value_zv) == 1) { |
7500 | 314 | CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES; |
7501 | 314 | } |
7502 | | |
7503 | 1.68k | } else { |
7504 | 1.68k | zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name)); |
7505 | 1.68k | } |
7506 | 7.65k | } |
7507 | | |
7508 | 5.76k | if (stmt_ast) { |
7509 | 884 | zend_compile_stmt(stmt_ast); |
7510 | | |
7511 | 884 | FC(declarables) = orig_declarables; |
7512 | 884 | } |
7513 | 5.76k | } |
7514 | | /* }}} */ |
7515 | | |
7516 | | static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */ |
7517 | 2.42M | { |
7518 | 2.42M | const zend_ast_list *list = zend_ast_get_list(ast); |
7519 | 2.42M | uint32_t i; |
7520 | 11.0M | for (i = 0; i < list->children; ++i) { |
7521 | 8.59M | zend_compile_stmt(list->child[i]); |
7522 | 8.59M | } |
7523 | 2.42M | } |
7524 | | /* }}} */ |
7525 | | |
7526 | | ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */ |
7527 | 1.57M | { |
7528 | 1.57M | uint32_t i, n; |
7529 | | |
7530 | 1.57M | func->common.arg_flags[0] = 0; |
7531 | 1.57M | func->common.arg_flags[1] = 0; |
7532 | 1.57M | func->common.arg_flags[2] = 0; |
7533 | 1.57M | if (func->common.arg_info) { |
7534 | 1.57M | n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM); |
7535 | 1.57M | i = 0; |
7536 | 3.18M | while (i < n) { |
7537 | 1.60M | ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i])); |
7538 | 1.60M | i++; |
7539 | 1.60M | } |
7540 | 1.57M | if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) { |
7541 | 589 | uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]); |
7542 | 7.54k | while (i < MAX_ARG_FLAG_NUM) { |
7543 | 6.95k | ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference); |
7544 | 6.95k | i++; |
7545 | 6.95k | } |
7546 | 589 | } |
7547 | 1.57M | } |
7548 | 1.57M | } |
7549 | | /* }}} */ |
7550 | | |
7551 | | static zend_type zend_compile_single_typename(zend_ast *ast) |
7552 | 979k | { |
7553 | 979k | ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE)); |
7554 | 979k | if (ast->kind == ZEND_AST_TYPE) { |
7555 | 7.04k | if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) { |
7556 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7557 | 5 | "Cannot use \"static\" when no class scope is active"); |
7558 | 5 | } |
7559 | | |
7560 | 7.04k | return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0); |
7561 | 972k | } else { |
7562 | 972k | zend_string *type_name = zend_ast_get_str(ast); |
7563 | 972k | uint8_t type_code = zend_lookup_builtin_type_by_name(type_name); |
7564 | | |
7565 | 972k | if (type_code != 0) { |
7566 | 315k | if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) { |
7567 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
7568 | 6 | "Type declaration '%s' must be unqualified", |
7569 | 6 | ZSTR_VAL(zend_string_tolower(type_name))); |
7570 | 6 | } |
7571 | | |
7572 | | /* Transform iterable into a type union alias */ |
7573 | 315k | if (type_code == IS_ITERABLE) { |
7574 | | /* Set iterable bit for BC compat during Reflection and string representation of type */ |
7575 | 286k | zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE), |
7576 | 286k | (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT)); |
7577 | 286k | return iterable; |
7578 | 286k | } |
7579 | | |
7580 | 28.7k | return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0); |
7581 | 657k | } else { |
7582 | 657k | const char *correct_name; |
7583 | 657k | uint32_t fetch_type = zend_get_class_fetch_type_ast(ast); |
7584 | 657k | zend_string *class_name = type_name; |
7585 | | |
7586 | 657k | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
7587 | 656k | class_name = zend_resolve_class_name_ast(ast); |
7588 | 656k | zend_assert_valid_class_name(class_name, "a type name"); |
7589 | 656k | } else { |
7590 | 939 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT); |
7591 | | |
7592 | 939 | zend_ensure_valid_class_fetch_type(fetch_type); |
7593 | | |
7594 | 939 | bool substitute_self_parent = zend_is_scope_known() |
7595 | 630 | && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS); |
7596 | | |
7597 | 939 | if (fetch_type == ZEND_FETCH_CLASS_SELF) { |
7598 | | /* Scope might be unknown for unbound closures and traits */ |
7599 | 738 | if (substitute_self_parent) { |
7600 | 480 | class_name = CG(active_class_entry)->name; |
7601 | 480 | ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time"); |
7602 | 480 | } |
7603 | 738 | } else { |
7604 | 201 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT); |
7605 | | /* Scope might be unknown for unbound closures and traits */ |
7606 | 201 | if (substitute_self_parent) { |
7607 | 135 | class_name = CG(active_class_entry)->parent_name; |
7608 | 135 | ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time"); |
7609 | 135 | } |
7610 | 184 | } |
7611 | 939 | zend_string_addref(class_name); |
7612 | 922 | } |
7613 | | |
7614 | 657k | if (ast->attr == ZEND_NAME_NOT_FQ |
7615 | 655k | && zend_is_confusable_type(type_name, &correct_name) |
7616 | 42.4k | && zend_is_not_imported(type_name)) { |
7617 | 42.3k | const char *extra = |
7618 | 42.3k | FC(current_namespace) ? " or import the class with \"use\"" : ""; |
7619 | 42.3k | if (correct_name) { |
7620 | 27.9k | zend_error(E_COMPILE_WARNING, |
7621 | 27.9k | "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? " |
7622 | 27.9k | "Write \"\\%s\"%s to suppress this warning", |
7623 | 27.9k | ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra); |
7624 | 27.9k | } else { |
7625 | 14.4k | zend_error(E_COMPILE_WARNING, |
7626 | 14.4k | "\"%s\" is not a supported builtin type " |
7627 | 14.4k | "and will be interpreted as a class name. " |
7628 | 14.4k | "Write \"\\%s\"%s to suppress this warning", |
7629 | 14.4k | ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra); |
7630 | 14.4k | } |
7631 | 42.3k | } |
7632 | | |
7633 | 657k | class_name = zend_new_interned_string(class_name); |
7634 | 657k | zend_alloc_ce_cache(class_name); |
7635 | 657k | return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0); |
7636 | 657k | } |
7637 | 972k | } |
7638 | 979k | } |
7639 | | |
7640 | | static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type) |
7641 | 6.36k | { |
7642 | 6.36k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type)); |
7643 | 6.36k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type)); |
7644 | 6.36k | const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type); |
7645 | 6.36k | const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type); |
7646 | 6.36k | const zend_type_list *smaller_type_list, *larger_type_list; |
7647 | 6.36k | bool flipped = false; |
7648 | | |
7649 | 6.36k | if (r_type_list->num_types < l_type_list->num_types) { |
7650 | 2.39k | smaller_type_list = r_type_list; |
7651 | 2.39k | larger_type_list = l_type_list; |
7652 | 2.39k | flipped = true; |
7653 | 3.96k | } else { |
7654 | 3.96k | smaller_type_list = l_type_list; |
7655 | 3.96k | larger_type_list = r_type_list; |
7656 | 3.96k | } |
7657 | | |
7658 | 6.36k | unsigned int sum = 0; |
7659 | 6.36k | const zend_type *outer_type; |
7660 | 24.5k | ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type) { |
7661 | 24.5k | const zend_type *inner_type; |
7662 | 81.0k | ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type) { |
7663 | 81.0k | if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) { |
7664 | 6.26k | sum++; |
7665 | 6.26k | break; |
7666 | 6.26k | } |
7667 | 81.0k | } ZEND_TYPE_LIST_FOREACH_END(); |
7668 | 24.5k | } ZEND_TYPE_LIST_FOREACH_END(); |
7669 | | |
7670 | 6.36k | if (sum == smaller_type_list->num_types) { |
7671 | 18 | zend_string *smaller_type_str; |
7672 | 18 | zend_string *larger_type_str; |
7673 | 18 | if (flipped) { |
7674 | 6 | smaller_type_str = zend_type_to_string(right_type); |
7675 | 6 | larger_type_str = zend_type_to_string(left_type); |
7676 | 12 | } else { |
7677 | 12 | smaller_type_str = zend_type_to_string(left_type); |
7678 | 12 | larger_type_str = zend_type_to_string(right_type); |
7679 | 12 | } |
7680 | 18 | if (smaller_type_list->num_types == larger_type_list->num_types) { |
7681 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s", |
7682 | 7 | ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str)); |
7683 | 11 | } else { |
7684 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7685 | 11 | ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str)); |
7686 | 11 | } |
7687 | 18 | } |
7688 | 6.36k | } |
7689 | | |
7690 | | static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type) |
7691 | 8.66k | { |
7692 | 8.66k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type)); |
7693 | 8.66k | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type)); |
7694 | | |
7695 | 8.66k | const zend_type *single_intersection_type = NULL; |
7696 | 30.6k | ZEND_TYPE_FOREACH(intersection_type, single_intersection_type) { |
7697 | 30.6k | if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) { |
7698 | 6 | zend_string *single_type_str = zend_type_to_string(single_type); |
7699 | 6 | zend_string *complete_type = zend_type_to_string(intersection_type); |
7700 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7701 | 6 | ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str)); |
7702 | 6 | } |
7703 | 30.6k | } ZEND_TYPE_FOREACH_END(); |
7704 | 8.66k | } |
7705 | | |
7706 | | /* Used by both intersection and union types prior to transforming the type list to a full zend_type */ |
7707 | | static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type) |
7708 | 221k | { |
7709 | 221k | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type)); |
7710 | 441k | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7711 | 220k | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7712 | 5.93k | zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type); |
7713 | 5.93k | continue; |
7714 | 5.93k | } |
7715 | 214k | if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) { |
7716 | 45 | zend_string *single_type_str = zend_type_to_string(type); |
7717 | 45 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str)); |
7718 | 45 | } |
7719 | 214k | } |
7720 | 221k | } |
7721 | | |
7722 | | static zend_type zend_compile_typename(zend_ast *ast); |
7723 | | |
7724 | | static zend_type zend_compile_typename_ex( |
7725 | | zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */ |
7726 | 767k | { |
7727 | 767k | bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE; |
7728 | 767k | zend_ast_attr orig_ast_attr = ast->attr; |
7729 | 767k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
7730 | | |
7731 | 767k | if (is_marked_nullable) { |
7732 | 3.54k | ast->attr &= ~ZEND_TYPE_NULLABLE; |
7733 | 3.54k | } |
7734 | | |
7735 | 767k | if (ast->kind == ZEND_AST_TYPE_UNION) { |
7736 | 205k | const zend_ast_list *list = zend_ast_get_list(ast); |
7737 | 205k | zend_type_list *type_list; |
7738 | 205k | bool is_composite = false; |
7739 | 205k | bool has_only_iterable_class = true; |
7740 | 205k | ALLOCA_FLAG(use_heap) |
7741 | | |
7742 | 205k | type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap); |
7743 | 205k | type_list->num_types = 0; |
7744 | | |
7745 | 620k | for (uint32_t i = 0; i < list->children; i++) { |
7746 | 414k | zend_ast *type_ast = list->child[i]; |
7747 | 414k | zend_type single_type; |
7748 | 414k | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7749 | | |
7750 | 414k | if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7751 | 6.87k | has_only_iterable_class = false; |
7752 | 6.87k | is_composite = true; |
7753 | | /* The first class type can be stored directly as the type ptr payload. */ |
7754 | 6.87k | if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) { |
7755 | | /* Switch from single name to name list. */ |
7756 | 952 | type_list->num_types = 1; |
7757 | 952 | type_list->types[0] = type; |
7758 | | /* Clear MAY_BE_* type flags */ |
7759 | 952 | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7760 | 952 | } |
7761 | | /* Mark type as list type */ |
7762 | 6.87k | ZEND_TYPE_SET_LIST(type, type_list); |
7763 | | |
7764 | 6.87k | single_type = zend_compile_typename(type_ast); |
7765 | 6.87k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type)); |
7766 | | |
7767 | 6.87k | type_list->types[type_list->num_types++] = single_type; |
7768 | | |
7769 | | /* Check for trivially redundant class types */ |
7770 | 15.9k | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7771 | 9.09k | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7772 | 6.36k | zend_are_intersection_types_redundant(single_type, type_list->types[i]); |
7773 | 6.36k | continue; |
7774 | 6.36k | } |
7775 | | /* Type from type list is a simple type */ |
7776 | 2.73k | zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]); |
7777 | 2.73k | } |
7778 | 6.87k | continue; |
7779 | 6.87k | } |
7780 | | |
7781 | 408k | single_type = zend_compile_single_typename(type_ast); |
7782 | 408k | uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type); |
7783 | | |
7784 | 408k | if (single_type_mask == MAY_BE_ANY) { |
7785 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type"); |
7786 | 5 | } |
7787 | 408k | if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7788 | 354k | has_only_iterable_class = false; |
7789 | 354k | } |
7790 | | |
7791 | 408k | uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask; |
7792 | 408k | if (type_mask_overlap) { |
7793 | 30 | zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap); |
7794 | 30 | zend_string *overlap_type_str = zend_type_to_string(overlap_type); |
7795 | 30 | zend_error_noreturn(E_COMPILE_ERROR, |
7796 | 30 | "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str)); |
7797 | 30 | } |
7798 | | |
7799 | 407k | if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE)) |
7800 | 407k | || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) { |
7801 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
7802 | 10 | "Type contains both true and false, bool must be used instead"); |
7803 | 10 | } |
7804 | 407k | ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type); |
7805 | | /* Clear MAY_BE_* type flags */ |
7806 | 407k | ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7807 | | |
7808 | 407k | if (ZEND_TYPE_IS_COMPLEX(single_type)) { |
7809 | 403k | if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) { |
7810 | | /* The first class type can be stored directly as the type ptr payload. */ |
7811 | 199k | ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type)); |
7812 | 199k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT; |
7813 | 203k | } else { |
7814 | 203k | if (type_list->num_types == 0) { |
7815 | | /* Switch from single name to name list. */ |
7816 | 197k | type_list->num_types = 1; |
7817 | 197k | type_list->types[0] = type; |
7818 | | /* Clear MAY_BE_* type flags */ |
7819 | 197k | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7820 | 197k | ZEND_TYPE_SET_LIST(type, type_list); |
7821 | 197k | } |
7822 | | |
7823 | 203k | type_list->types[type_list->num_types++] = single_type; |
7824 | | |
7825 | | /* Check for trivially redundant class types */ |
7826 | 203k | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7827 | 203k | } |
7828 | 403k | } |
7829 | 407k | } |
7830 | | |
7831 | 205k | if (type_list->num_types) { |
7832 | 203k | zend_type_list *list = zend_arena_alloc( |
7833 | 203k | &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7834 | 203k | memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7835 | 203k | ZEND_TYPE_SET_LIST(type, list); |
7836 | 203k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7837 | | /* Inform that the type list is a union type */ |
7838 | 203k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7839 | 203k | } |
7840 | | |
7841 | 205k | free_alloca(type_list, use_heap); |
7842 | | |
7843 | 205k | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7844 | 205k | if ((type_mask & MAY_BE_OBJECT) && |
7845 | 537 | ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) { |
7846 | 27 | zend_string *type_str = zend_type_to_string(type); |
7847 | 27 | zend_error_noreturn(E_COMPILE_ERROR, |
7848 | 27 | "Type %s contains both object and a class type, which is redundant", |
7849 | 27 | ZSTR_VAL(type_str)); |
7850 | 27 | } |
7851 | 561k | } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7852 | 8.08k | const zend_ast_list *list = zend_ast_get_list(ast); |
7853 | 8.08k | zend_type_list *type_list; |
7854 | | |
7855 | | /* Allocate the type list directly on the arena as it must be a type |
7856 | | * list of the same number of elements as the AST list has children */ |
7857 | 8.08k | type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children)); |
7858 | 8.08k | type_list->num_types = 0; |
7859 | | |
7860 | 8.08k | ZEND_ASSERT(list->children > 1); |
7861 | | |
7862 | 25.9k | for (uint32_t i = 0; i < list->children; i++) { |
7863 | 17.9k | zend_ast *type_ast = list->child[i]; |
7864 | 17.9k | zend_type single_type = zend_compile_single_typename(type_ast); |
7865 | | |
7866 | | /* An intersection of union types cannot exist so invalidate it |
7867 | | * Currently only can happen with iterable getting canonicalized to Traversable|array */ |
7868 | 17.9k | if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7869 | 5 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7870 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7871 | 5 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7872 | 5 | } |
7873 | | /* An intersection of standard types cannot exist so invalidate it */ |
7874 | 17.9k | if (ZEND_TYPE_IS_ONLY_MASK(single_type)) { |
7875 | 65 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7876 | 65 | zend_error_noreturn(E_COMPILE_ERROR, |
7877 | 65 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7878 | 65 | } |
7879 | | /* Check for "self" and "parent" too */ |
7880 | 17.8k | if ( |
7881 | 17.8k | zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF)) |
7882 | 17.8k | || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT)) |
7883 | 17.8k | ) { |
7884 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
7885 | 10 | "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type))); |
7886 | 10 | } |
7887 | | |
7888 | | /* Add type to the type list */ |
7889 | 17.8k | type_list->types[type_list->num_types++] = single_type; |
7890 | | |
7891 | | /* Check for trivially redundant class types */ |
7892 | 17.8k | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7893 | 17.8k | } |
7894 | | |
7895 | 8.00k | ZEND_ASSERT(list->children == type_list->num_types); |
7896 | | |
7897 | | /* An implicitly nullable intersection type needs to be converted to a DNF type */ |
7898 | 8.00k | if (force_allow_null) { |
7899 | 47 | zend_type intersection_type = ZEND_TYPE_INIT_NONE(0); |
7900 | 47 | ZEND_TYPE_SET_LIST(intersection_type, type_list); |
7901 | 47 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7902 | 47 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT; |
7903 | | |
7904 | 47 | zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1)); |
7905 | 47 | dnf_type_list->num_types = 1; |
7906 | 47 | dnf_type_list->types[0] = intersection_type; |
7907 | 47 | ZEND_TYPE_SET_LIST(type, dnf_type_list); |
7908 | | /* Inform that the type list is a DNF type */ |
7909 | 47 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7910 | 47 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7911 | 7.94k | } else { |
7912 | 7.94k | ZEND_TYPE_SET_LIST(type, type_list); |
7913 | | /* Inform that the type list is an intersection type */ |
7914 | 7.94k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7915 | 7.94k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7916 | 7.94k | } |
7917 | 553k | } else { |
7918 | 553k | type = zend_compile_single_typename(ast); |
7919 | 553k | } |
7920 | | |
7921 | 767k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
7922 | | |
7923 | 767k | if (type_mask == MAY_BE_ANY && is_marked_nullable) { |
7924 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null"); |
7925 | 14 | } |
7926 | | |
7927 | 767k | if ((type_mask & MAY_BE_NULL) && is_marked_nullable) { |
7928 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable"); |
7929 | 5 | } |
7930 | | |
7931 | 767k | if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) { |
7932 | 197 | *forced_allow_null = true; |
7933 | 197 | } |
7934 | | |
7935 | 767k | if (is_marked_nullable || force_allow_null) { |
7936 | 3.87k | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
7937 | 3.87k | type_mask = ZEND_TYPE_PURE_MASK(type); |
7938 | 3.87k | } |
7939 | | |
7940 | 767k | if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) { |
7941 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type"); |
7942 | 10 | } |
7943 | | |
7944 | 767k | if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) { |
7945 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type"); |
7946 | 7 | } |
7947 | | |
7948 | 767k | ast->attr = orig_ast_attr; |
7949 | 767k | return type; |
7950 | 767k | } |
7951 | | /* }}} */ |
7952 | | |
7953 | | static zend_type zend_compile_typename(zend_ast *ast) |
7954 | 142k | { |
7955 | 142k | bool forced_allow_null; |
7956 | 142k | return zend_compile_typename_ex(ast, false, &forced_allow_null); |
7957 | 142k | } |
7958 | | |
7959 | | /* May convert value from int to float. */ |
7960 | | static bool zend_is_valid_default_value(zend_type type, zval *value) |
7961 | 4.37k | { |
7962 | 4.37k | ZEND_ASSERT(ZEND_TYPE_IS_SET(type)); |
7963 | 4.37k | if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) { |
7964 | 3.11k | return true; |
7965 | 3.11k | } |
7966 | 1.25k | if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) { |
7967 | | /* Integers are allowed as initializers for floating-point values. */ |
7968 | 1.10k | convert_to_double(value); |
7969 | 1.10k | return true; |
7970 | 1.10k | } |
7971 | 157 | return false; |
7972 | 1.25k | } |
7973 | | |
7974 | | static void zend_compile_attributes( |
7975 | | HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted |
7976 | 1.44M | ) /* {{{ */ { |
7977 | 1.44M | zend_attribute *attr; |
7978 | 1.44M | zend_internal_attribute *config; |
7979 | | |
7980 | 1.44M | const zend_ast_list *list = zend_ast_get_list(ast); |
7981 | 1.44M | uint32_t g, i, j; |
7982 | | |
7983 | 1.44M | ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST); |
7984 | | |
7985 | 2.90M | for (g = 0; g < list->children; g++) { |
7986 | 1.45M | const zend_ast_list *group = zend_ast_get_list(list->child[g]); |
7987 | | |
7988 | 1.45M | ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP); |
7989 | | |
7990 | 3.80M | for (i = 0; i < group->children; i++) { |
7991 | 2.34M | ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE); |
7992 | | |
7993 | 2.34M | const zend_ast *el = group->child[i]; |
7994 | | |
7995 | 2.34M | if (el->child[1] && |
7996 | 33.4k | el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) { |
7997 | 12 | zend_error_noreturn(E_COMPILE_ERROR, |
7998 | 12 | "Cannot create Closure as attribute argument"); |
7999 | 12 | } |
8000 | | |
8001 | 2.34M | zend_string *name = zend_resolve_class_name_ast(el->child[0]); |
8002 | 2.34M | zend_string *lcname = zend_string_tolower_ex(name, false); |
8003 | 2.34M | zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL; |
8004 | | |
8005 | 2.34M | config = zend_internal_attribute_get(lcname); |
8006 | 2.34M | zend_string_release(lcname); |
8007 | | |
8008 | | /* Exclude internal attributes that do not match on promoted properties. */ |
8009 | 2.34M | if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
8010 | 582 | if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) { |
8011 | 14 | zend_string_release(name); |
8012 | 14 | continue; |
8013 | 14 | } |
8014 | 582 | } |
8015 | | |
8016 | 2.34M | uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) |
8017 | 2.34M | ? ZEND_ATTRIBUTE_STRICT_TYPES : 0; |
8018 | 2.34M | attr = zend_add_attribute( |
8019 | 2.34M | attributes, name, args ? args->children : 0, flags, offset, el->lineno); |
8020 | 2.34M | zend_string_release(name); |
8021 | | |
8022 | | /* Populate arguments */ |
8023 | 2.34M | if (args) { |
8024 | 33.4k | ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST); |
8025 | | |
8026 | 33.4k | bool uses_named_args = false; |
8027 | 75.1k | for (j = 0; j < args->children; j++) { |
8028 | 41.7k | zend_ast **arg_ast_ptr = &args->child[j]; |
8029 | 41.7k | zend_ast *arg_ast = *arg_ast_ptr; |
8030 | | |
8031 | 41.7k | if (arg_ast->kind == ZEND_AST_UNPACK) { |
8032 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8033 | 5 | "Cannot use unpacking in attribute argument list"); |
8034 | 5 | } |
8035 | | |
8036 | 41.7k | if (arg_ast->kind == ZEND_AST_NAMED_ARG) { |
8037 | 1.12k | attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0])); |
8038 | 1.12k | arg_ast_ptr = &arg_ast->child[1]; |
8039 | 1.12k | uses_named_args = true; |
8040 | | |
8041 | 6.91k | for (uint32_t k = 0; k < j; k++) { |
8042 | 5.79k | if (attr->args[k].name && |
8043 | 3.56k | zend_string_equals(attr->args[k].name, attr->args[j].name)) { |
8044 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s", |
8045 | 9 | ZSTR_VAL(attr->args[j].name)); |
8046 | 9 | } |
8047 | 5.79k | } |
8048 | 40.5k | } else if (uses_named_args) { |
8049 | 33 | zend_error_noreturn(E_COMPILE_ERROR, |
8050 | 33 | "Cannot use positional argument after named argument"); |
8051 | 33 | } |
8052 | | |
8053 | 41.6k | zend_const_expr_to_zval( |
8054 | 41.6k | &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true); |
8055 | 41.6k | } |
8056 | 33.4k | } |
8057 | 2.34M | } |
8058 | 1.45M | } |
8059 | | |
8060 | 1.44M | if (*attributes != NULL) { |
8061 | | /* Allow delaying target validation for forward compatibility. */ |
8062 | 1.44M | const zend_attribute *delayed_target_validation = NULL; |
8063 | 1.44M | if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) { |
8064 | 30.0k | ZEND_ASSERT(offset >= 1); |
8065 | | /* zend_get_parameter_attribute_str will add 1 too */ |
8066 | 30.0k | delayed_target_validation = zend_get_parameter_attribute_str( |
8067 | 30.0k | *attributes, |
8068 | 30.0k | "delayedtargetvalidation", |
8069 | 30.0k | strlen("delayedtargetvalidation"), |
8070 | 30.0k | offset - 1 |
8071 | 30.0k | ); |
8072 | 1.41M | } else { |
8073 | 1.41M | delayed_target_validation = zend_get_attribute_str( |
8074 | 1.41M | *attributes, |
8075 | 1.41M | "delayedtargetvalidation", |
8076 | 1.41M | strlen("delayedtargetvalidation") |
8077 | 1.41M | ); |
8078 | 1.41M | } |
8079 | | /* Validate attributes in a secondary loop (needed to detect repeated attributes). */ |
8080 | 8.44M | ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) { |
8081 | 8.44M | if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) { |
8082 | 2.76M | continue; |
8083 | 2.76M | } |
8084 | | |
8085 | 8.44M | bool run_validator = true; |
8086 | 4.17k | if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
8087 | 270 | if (delayed_target_validation == NULL) { |
8088 | 38 | zend_string *location = zend_get_attribute_target_names(target); |
8089 | 38 | zend_string *allowed = zend_get_attribute_target_names(config->flags); |
8090 | | |
8091 | 38 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)", |
8092 | 38 | ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed) |
8093 | 38 | ); |
8094 | 38 | } |
8095 | 232 | run_validator = false; |
8096 | 232 | } |
8097 | | |
8098 | 4.13k | if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) { |
8099 | 4.13k | if (zend_is_attribute_repeated(*attributes, attr)) { |
8100 | 19 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name)); |
8101 | 19 | } |
8102 | 4.13k | } |
8103 | | |
8104 | | /* Validators are not run if the target is already invalid */ |
8105 | 4.11k | if (run_validator && config->validator != NULL) { |
8106 | 2.41k | zend_string *error = config->validator(attr, target, CG(active_class_entry)); |
8107 | 2.41k | if (error != NULL) { |
8108 | 356 | if (delayed_target_validation == NULL) { |
8109 | 86 | zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error)); |
8110 | 270 | } else { |
8111 | 270 | attr->validation_error = error; |
8112 | 270 | } |
8113 | 356 | } |
8114 | 2.41k | } |
8115 | 4.11k | } ZEND_HASH_FOREACH_END(); |
8116 | 1.44M | } |
8117 | 1.44M | } |
8118 | | /* }}} */ |
8119 | | |
8120 | | static void zend_compile_property_hooks( |
8121 | | zend_property_info *prop_info, zend_string *prop_name, |
8122 | | zend_ast *prop_type_ast, const zend_ast_list *hooks); |
8123 | | |
8124 | | typedef struct { |
8125 | | const zend_string *property_name; |
8126 | | bool uses_property; |
8127 | | } find_property_usage_context; |
8128 | | |
8129 | | static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */ |
8130 | 38.0k | { |
8131 | 38.0k | zend_ast *ast = *ast_ptr; |
8132 | 38.0k | find_property_usage_context *context = (find_property_usage_context *) _context; |
8133 | | |
8134 | 38.0k | if (ast == NULL) { |
8135 | 403 | return; |
8136 | 37.6k | } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) { |
8137 | 3.18k | const zend_ast *object_ast = ast->child[0]; |
8138 | 3.18k | zend_ast *property_ast = ast->child[1]; |
8139 | | |
8140 | 3.18k | if (object_ast->kind == ZEND_AST_VAR |
8141 | 2.72k | && object_ast->child[0]->kind == ZEND_AST_ZVAL |
8142 | 2.63k | && property_ast->kind == ZEND_AST_ZVAL) { |
8143 | 2.38k | const zval *object = zend_ast_get_zval(object_ast->child[0]); |
8144 | 2.38k | const zval *property = zend_ast_get_zval(property_ast); |
8145 | 2.38k | if (Z_TYPE_P(object) == IS_STRING |
8146 | 2.35k | && Z_TYPE_P(property) == IS_STRING |
8147 | 2.35k | && zend_string_equals_literal(Z_STR_P(object), "this") |
8148 | 1.99k | && zend_string_equals(Z_STR_P(property), context->property_name)) { |
8149 | 921 | context->uses_property = true; |
8150 | | /* No need to look for references in this branch. */ |
8151 | 921 | return; |
8152 | 921 | } |
8153 | 2.38k | } |
8154 | 3.18k | } |
8155 | | |
8156 | | /* Don't search across function/class boundaries. */ |
8157 | 36.7k | if (!zend_ast_is_special(ast)) { |
8158 | 23.3k | zend_ast_apply(ast, zend_property_hook_find_property_usage, context); |
8159 | 23.3k | } |
8160 | 36.7k | } |
8161 | | |
8162 | | static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast) |
8163 | 5.20k | { |
8164 | 5.20k | if (zend_string_equals_literal_ci(hook_name, "set") |
8165 | 2.12k | && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
8166 | 552 | return true; |
8167 | 552 | } |
8168 | | |
8169 | 4.65k | find_property_usage_context context = { property_name, false }; |
8170 | 4.65k | zend_property_hook_find_property_usage(&hook_ast, &context); |
8171 | 4.65k | return context.uses_property; |
8172 | 5.20k | } |
8173 | | |
8174 | | static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast) |
8175 | 34.0k | { |
8176 | 34.0k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
8177 | 299 | return true; |
8178 | 299 | } |
8179 | 33.7k | if (!hooks_ast) { |
8180 | 29.7k | return false; |
8181 | 29.7k | } |
8182 | | |
8183 | 33.7k | bool is_virtual = true; |
8184 | | |
8185 | 3.97k | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
8186 | 9.71k | for (uint32_t i = 0; i < hooks->children; i++) { |
8187 | 5.74k | const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i]; |
8188 | 5.74k | zend_ast *body = hook->child[2]; |
8189 | 5.74k | if (body && zend_property_hook_uses_property(property_name, hook->name, body)) { |
8190 | 1.41k | is_virtual = false; |
8191 | 1.41k | } |
8192 | 5.74k | } |
8193 | | |
8194 | 3.97k | return is_virtual; |
8195 | 33.7k | } |
8196 | | |
8197 | | static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */ |
8198 | 2.16M | { |
8199 | 2.16M | zend_ast_list *list = zend_ast_get_list(ast); |
8200 | 2.16M | uint32_t i; |
8201 | 2.16M | zend_op_array *op_array = CG(active_op_array); |
8202 | 2.16M | zend_arg_info *arg_infos; |
8203 | | |
8204 | 2.16M | if (return_type_ast || fallback_return_type) { |
8205 | | /* Use op_array->arg_info[-1] for return type */ |
8206 | 121k | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0); |
8207 | 121k | arg_infos->name = NULL; |
8208 | 121k | if (return_type_ast) { |
8209 | 115k | arg_infos->type = zend_compile_typename(return_type_ast); |
8210 | 115k | ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS( |
8211 | 115k | (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0); |
8212 | 115k | } else { |
8213 | 6.00k | arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0); |
8214 | 6.00k | } |
8215 | 121k | arg_infos->doc_comment = NULL; |
8216 | 121k | arg_infos++; |
8217 | 121k | op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE; |
8218 | | |
8219 | 121k | if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID) |
8220 | 4.63k | && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) { |
8221 | 657 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8222 | 657 | zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name)); |
8223 | 657 | zend_string_release(func_name); |
8224 | 657 | } |
8225 | 2.03M | } else { |
8226 | 2.03M | if (list->children == 0) { |
8227 | 611k | return; |
8228 | 611k | } |
8229 | 1.42M | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0); |
8230 | 1.42M | } |
8231 | | |
8232 | | /* Find last required parameter number for deprecation message. */ |
8233 | 1.54M | uint32_t last_required_param = (uint32_t) -1; |
8234 | 3.12M | for (i = 0; i < list->children; ++i) { |
8235 | 1.57M | zend_ast *param_ast = list->child[i]; |
8236 | 1.57M | zend_ast *default_ast_ptr = param_ast->child[2]; |
8237 | 1.57M | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8238 | 1.57M | if (!default_ast_ptr && !is_variadic) { |
8239 | 1.57M | last_required_param = i; |
8240 | 1.57M | } |
8241 | 1.57M | } |
8242 | | |
8243 | 1.54M | const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL; |
8244 | 3.12M | for (i = 0; i < list->children; ++i) { |
8245 | 1.57M | zend_ast *param_ast = list->child[i]; |
8246 | 1.57M | zend_ast *type_ast = param_ast->child[0]; |
8247 | 1.57M | zend_ast *var_ast = param_ast->child[1]; |
8248 | 1.57M | zend_ast **default_ast_ptr = ¶m_ast->child[2]; |
8249 | 1.57M | zend_ast *attributes_ast = param_ast->child[3]; |
8250 | 1.57M | zend_ast *doc_comment_ast = param_ast->child[4]; |
8251 | 1.57M | zend_ast *hooks_ast = param_ast->child[5]; |
8252 | 1.57M | zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast)); |
8253 | 1.57M | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8254 | 1.57M | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8255 | 1.57M | uint32_t property_flags = param_ast->attr & promotion_flags; |
8256 | 1.57M | bool is_promoted = property_flags || hooks_ast; |
8257 | | |
8258 | 1.57M | CG(zend_lineno) = param_ast->lineno; |
8259 | | |
8260 | 1.57M | znode var_node, default_node; |
8261 | 1.57M | uint8_t opcode; |
8262 | 1.57M | zend_op *opline; |
8263 | 1.57M | zend_arg_info *arg_info; |
8264 | | |
8265 | 1.57M | if (zend_is_auto_global(name)) { |
8266 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s", |
8267 | 1 | ZSTR_VAL(name)); |
8268 | 1 | } |
8269 | | |
8270 | 1.57M | var_node.op_type = IS_CV; |
8271 | 1.57M | var_node.u.op.var = lookup_cv(name); |
8272 | | |
8273 | 1.57M | if (EX_VAR_TO_NUM(var_node.u.op.var) != i) { |
8274 | 38 | zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s", |
8275 | 38 | ZSTR_VAL(name)); |
8276 | 1.57M | } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8277 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter"); |
8278 | 1.57M | } else if (zend_string_equals_literal(name, "http_response_header")) { |
8279 | 68 | CG(context).has_assigned_to_http_response_header = true; |
8280 | 68 | } |
8281 | | |
8282 | 1.57M | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8283 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic"); |
8284 | 7 | } |
8285 | | |
8286 | 1.57M | if (is_variadic) { |
8287 | 1.26k | opcode = ZEND_RECV_VARIADIC; |
8288 | 1.26k | default_node.op_type = IS_UNUSED; |
8289 | 1.26k | op_array->fn_flags |= ZEND_ACC_VARIADIC; |
8290 | | |
8291 | 1.26k | if (*default_ast_ptr) { |
8292 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8293 | 5 | "Variadic parameter cannot have a default value"); |
8294 | 5 | } |
8295 | 1.57M | } else if (*default_ast_ptr) { |
8296 | | /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */ |
8297 | 7.72k | uint32_t cops = CG(compiler_options); |
8298 | 7.72k | CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION; |
8299 | 7.72k | opcode = ZEND_RECV_INIT; |
8300 | 7.72k | default_node.op_type = IS_CONST; |
8301 | 7.72k | zend_const_expr_to_zval( |
8302 | 7.72k | &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true); |
8303 | 7.72k | CG(compiler_options) = cops; |
8304 | 1.57M | } else { |
8305 | 1.57M | opcode = ZEND_RECV; |
8306 | 1.57M | default_node.op_type = IS_UNUSED; |
8307 | 1.57M | op_array->required_num_args = i + 1; |
8308 | 1.57M | } |
8309 | | |
8310 | 1.57M | arg_info = &arg_infos[i]; |
8311 | 1.57M | arg_info->name = zend_string_copy(name); |
8312 | 1.57M | arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0); |
8313 | 1.57M | arg_info->default_value = NULL; |
8314 | 1.57M | arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8315 | | |
8316 | 1.57M | if (attributes_ast) { |
8317 | 30.0k | zend_compile_attributes( |
8318 | 30.0k | &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER, |
8319 | 30.0k | is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0 |
8320 | 30.0k | ); |
8321 | 30.0k | } |
8322 | | |
8323 | 1.57M | bool forced_allow_nullable = false; |
8324 | 1.57M | if (type_ast) { |
8325 | 624k | uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF; |
8326 | 624k | bool force_nullable = default_type == IS_NULL && !is_promoted; |
8327 | | |
8328 | 624k | op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS; |
8329 | 624k | arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable); |
8330 | 624k | if (forced_allow_nullable) { |
8331 | 197 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8332 | 197 | zend_error(E_DEPRECATED, |
8333 | 197 | "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type " |
8334 | 197 | "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name)); |
8335 | 197 | zend_string_release(func_name); |
8336 | 197 | } |
8337 | | |
8338 | 624k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) { |
8339 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type"); |
8340 | 5 | } |
8341 | | |
8342 | 624k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) { |
8343 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type"); |
8344 | 5 | } |
8345 | | |
8346 | 624k | if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable |
8347 | 1.03k | && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) { |
8348 | 29 | zend_string *type_str = zend_type_to_string(arg_info->type); |
8349 | 29 | zend_error_noreturn(E_COMPILE_ERROR, |
8350 | 29 | "Cannot use %s as default value for parameter $%s of type %s", |
8351 | 29 | zend_get_type_by_const(default_type), |
8352 | 29 | ZSTR_VAL(name), ZSTR_VAL(type_str)); |
8353 | 29 | } |
8354 | 624k | } |
8355 | 1.57M | if (last_required_param != (uint32_t) -1 |
8356 | 1.57M | && i < last_required_param |
8357 | 40.6k | && default_node.op_type == IS_CONST) { |
8358 | | /* Ignore parameters of the form "Type $param = null". |
8359 | | * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */ |
8360 | 716 | if (!forced_allow_nullable) { |
8361 | 615 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8362 | 615 | zend_ast *required_param_ast = list->child[last_required_param]; |
8363 | 615 | zend_error(E_DEPRECATED, |
8364 | 615 | "%s(): Optional parameter $%s declared before required parameter $%s " |
8365 | 615 | "is implicitly treated as a required parameter", |
8366 | 615 | ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1]))); |
8367 | 615 | zend_string_release(func_name); |
8368 | 615 | } |
8369 | | |
8370 | | /* Regardless of whether we issue a deprecation, convert this parameter into |
8371 | | * a required parameter without a default value. This ensures that it cannot be |
8372 | | * used as an optional parameter even with named parameters. */ |
8373 | 716 | opcode = ZEND_RECV; |
8374 | 716 | default_node.op_type = IS_UNUSED; |
8375 | 716 | zval_ptr_dtor(&default_node.u.constant); |
8376 | 716 | } |
8377 | | |
8378 | 1.57M | opline = zend_emit_op(NULL, opcode, NULL, &default_node); |
8379 | 1.57M | SET_NODE(opline->result, &var_node); |
8380 | 1.57M | opline->op1.num = i + 1; |
8381 | | |
8382 | 1.57M | uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0) |
8383 | 1.57M | | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0); |
8384 | 1.57M | ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags; |
8385 | 1.57M | if (opcode == ZEND_RECV) { |
8386 | 1.57M | opline->op2.num = type_ast ? |
8387 | 949k | ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY; |
8388 | 1.57M | } |
8389 | | |
8390 | 1.57M | if (is_promoted) { |
8391 | 1.25k | const zend_op_array *active_op_array = CG(active_op_array); |
8392 | 1.25k | zend_class_entry *scope = active_op_array->scope; |
8393 | | |
8394 | 1.25k | bool is_ctor = |
8395 | 1.25k | scope && zend_is_constructor(active_op_array->function_name); |
8396 | 1.25k | if (!is_ctor) { |
8397 | 34 | zend_error_noreturn(E_COMPILE_ERROR, |
8398 | 34 | "Cannot declare promoted property outside a constructor"); |
8399 | 34 | } |
8400 | 1.22k | if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT) |
8401 | 1.21k | || (scope->ce_flags & ZEND_ACC_INTERFACE)) { |
8402 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8403 | 5 | "Cannot declare promoted property in an abstract constructor"); |
8404 | 5 | } |
8405 | 1.21k | if (is_variadic) { |
8406 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8407 | 5 | "Cannot declare variadic promoted property"); |
8408 | 5 | } |
8409 | 1.21k | if (zend_hash_exists(&scope->properties_info, name)) { |
8410 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
8411 | 5 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8412 | 5 | } |
8413 | 1.20k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) { |
8414 | 5 | zend_string *str = zend_type_to_string(arg_info->type); |
8415 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8416 | 5 | "Property %s::$%s cannot have type %s", |
8417 | 5 | ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
8418 | 5 | } |
8419 | | |
8420 | 1.20k | if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
8421 | 21 | property_flags |= ZEND_ACC_READONLY; |
8422 | 21 | } |
8423 | | |
8424 | | /* Recompile the type, as it has different memory management requirements. */ |
8425 | 1.20k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
8426 | 1.20k | if (type_ast) { |
8427 | 924 | type = zend_compile_typename(type_ast); |
8428 | 924 | } |
8429 | | |
8430 | | /* Don't give the property an explicit default value. For typed properties this means |
8431 | | * uninitialized, for untyped properties it means an implicit null default value. |
8432 | | * Properties with hooks get an implicit default value of undefined until inheritance, |
8433 | | * where it is changed to null only once we know it is not virtual. If we were to set it |
8434 | | * here, we couldn't verify that a true virtual property must not have an explicit |
8435 | | * default value. */ |
8436 | 1.20k | zval default_value; |
8437 | 1.20k | if (ZEND_TYPE_IS_SET(type) || hooks_ast) { |
8438 | 973 | ZVAL_UNDEF(&default_value); |
8439 | 973 | } else { |
8440 | 230 | if (property_flags & ZEND_ACC_READONLY) { |
8441 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
8442 | 10 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8443 | 10 | } |
8444 | | |
8445 | 220 | ZVAL_NULL(&default_value); |
8446 | 220 | } |
8447 | | |
8448 | 1.19k | zend_string *doc_comment = |
8449 | 1.19k | doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8450 | 1.19k | zend_property_info *prop = zend_declare_typed_property( |
8451 | 1.19k | scope, name, &default_value, |
8452 | 1.19k | property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED, |
8453 | 1.19k | doc_comment, type); |
8454 | 1.19k | if (hooks_ast) { |
8455 | 108 | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
8456 | 108 | zend_compile_property_hooks(prop, name, type_ast, hooks); |
8457 | 108 | } |
8458 | 1.19k | if (attributes_ast) { |
8459 | 61 | zend_compile_attributes( |
8460 | 61 | &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER); |
8461 | | |
8462 | 61 | zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1); |
8463 | 61 | if (override_attribute) { |
8464 | 11 | prop->flags |= ZEND_ACC_OVERRIDE; |
8465 | 11 | } |
8466 | 61 | } |
8467 | 1.19k | } |
8468 | 1.57M | } |
8469 | | |
8470 | | /* These are assigned at the end to avoid uninitialized memory in case of an error */ |
8471 | 1.54M | op_array->num_args = list->children; |
8472 | 1.54M | op_array->arg_info = arg_infos; |
8473 | | |
8474 | | /* Don't count the variadic argument */ |
8475 | 1.54M | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8476 | 1.24k | op_array->num_args--; |
8477 | 1.24k | } |
8478 | 1.54M | zend_set_function_arg_flags((zend_function*)op_array); |
8479 | | |
8480 | 3.12M | for (i = 0; i < list->children; i++) { |
8481 | 1.57M | zend_ast *param_ast = list->child[i]; |
8482 | 1.57M | zend_ast *hooks_ast = param_ast->child[5]; |
8483 | 1.57M | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8484 | 1.57M | uint32_t flags = param_ast->attr & promotion_flags; |
8485 | 1.57M | bool is_promoted = flags || hooks_ast; |
8486 | 1.57M | if (!is_promoted) { |
8487 | 1.57M | continue; |
8488 | 1.57M | } |
8489 | | |
8490 | 1.17k | CG(zend_lineno) = param_ast->lineno; |
8491 | | |
8492 | | /* Emit $this->prop = $prop for promoted properties. */ |
8493 | 1.17k | zend_string *name = zend_ast_get_str(param_ast->child[1]); |
8494 | 1.17k | znode name_node, value_node; |
8495 | 1.17k | name_node.op_type = IS_CONST; |
8496 | 1.17k | ZVAL_STR_COPY(&name_node.u.constant, name); |
8497 | 1.17k | value_node.op_type = IS_CV; |
8498 | 1.17k | value_node.u.op.var = lookup_cv(name); |
8499 | | |
8500 | 1.17k | zend_op *opline = zend_emit_op(NULL, |
8501 | 1.17k | is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node); |
8502 | 1.17k | opline->extended_value = zend_alloc_cache_slots(3); |
8503 | 1.17k | zend_emit_op_data(&value_node); |
8504 | 1.17k | } |
8505 | 1.54M | } |
8506 | | /* }}} */ |
8507 | | |
8508 | | static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */ |
8509 | 4.11k | { |
8510 | 4.11k | const zend_ast_list *list = zend_ast_get_list(uses_ast); |
8511 | 4.11k | uint32_t i; |
8512 | | |
8513 | 4.11k | if (!list->children) { |
8514 | 325 | return; |
8515 | 325 | } |
8516 | | |
8517 | 3.78k | if (!op_array->static_variables) { |
8518 | 3.78k | op_array->static_variables = zend_new_array(8); |
8519 | 3.78k | } |
8520 | | |
8521 | 15.2k | for (i = 0; i < list->children; ++i) { |
8522 | 11.4k | zend_ast *var_name_ast = list->child[i]; |
8523 | 11.4k | zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast)); |
8524 | 11.4k | uint32_t mode = var_name_ast->attr; |
8525 | 11.4k | zend_op *opline; |
8526 | 11.4k | zval *value; |
8527 | | |
8528 | 11.4k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8529 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable"); |
8530 | 8 | } |
8531 | | |
8532 | 11.4k | if (zend_is_auto_global(var_name)) { |
8533 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable"); |
8534 | 5 | } |
8535 | | |
8536 | 11.4k | value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8537 | 11.4k | if (!value) { |
8538 | 7 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, |
8539 | 7 | "Cannot use variable $%S twice", var_name); |
8540 | 7 | } |
8541 | | |
8542 | 11.4k | CG(zend_lineno) = zend_ast_get_lineno(var_name_ast); |
8543 | | |
8544 | 11.4k | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8545 | 11.4k | opline->op2_type = IS_CV; |
8546 | 11.4k | opline->op2.var = lookup_cv(var_name); |
8547 | 11.4k | opline->extended_value = |
8548 | 11.4k | (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode; |
8549 | 11.4k | } |
8550 | 3.78k | } |
8551 | | /* }}} */ |
8552 | | |
8553 | | typedef struct { |
8554 | | HashTable uses; |
8555 | | bool varvars_used; |
8556 | | } closure_info; |
8557 | | |
8558 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast); |
8559 | | |
8560 | 3.80M | static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) { |
8561 | 3.80M | if (!ast) { |
8562 | 268k | return; |
8563 | 268k | } |
8564 | | |
8565 | 3.53M | if (ast->kind == ZEND_AST_VAR) { |
8566 | 141k | zend_ast *name_ast = ast->child[0]; |
8567 | 141k | if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) { |
8568 | 138k | zend_string *name = zend_ast_get_str(name_ast); |
8569 | 138k | if (zend_is_auto_global(name)) { |
8570 | | /* These is no need to explicitly import auto-globals. */ |
8571 | 2.87k | return; |
8572 | 2.87k | } |
8573 | | |
8574 | 136k | if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8575 | | /* $this does not need to be explicitly imported. */ |
8576 | 8.66k | return; |
8577 | 8.66k | } |
8578 | | |
8579 | 127k | zend_hash_add_empty_element(&info->uses, name); |
8580 | 127k | } else { |
8581 | 2.89k | info->varvars_used = true; |
8582 | 2.89k | find_implicit_binds_recursively(info, name_ast); |
8583 | 2.89k | } |
8584 | 3.39M | } else if (zend_ast_is_list(ast)) { |
8585 | 407k | const zend_ast_list *list = zend_ast_get_list(ast); |
8586 | 407k | uint32_t i; |
8587 | 952k | for (i = 0; i < list->children; i++) { |
8588 | 545k | find_implicit_binds_recursively(info, list->child[i]); |
8589 | 545k | } |
8590 | 2.98M | } else if (ast->kind == ZEND_AST_CLOSURE) { |
8591 | | /* For normal closures add the use() list. */ |
8592 | 1.46k | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8593 | 1.46k | zend_ast *uses_ast = closure_ast->child[1]; |
8594 | 1.46k | if (uses_ast) { |
8595 | 1.33k | const zend_ast_list *uses_list = zend_ast_get_list(uses_ast); |
8596 | 1.33k | uint32_t i; |
8597 | 8.03k | for (i = 0; i < uses_list->children; i++) { |
8598 | 6.70k | zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i])); |
8599 | 6.70k | } |
8600 | 1.33k | } |
8601 | 2.98M | } else if (ast->kind == ZEND_AST_ARROW_FUNC) { |
8602 | | /* For arrow functions recursively check the expression. */ |
8603 | 342k | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8604 | 342k | closure_info inner_info; |
8605 | 342k | find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]); |
8606 | 342k | if (inner_info.varvars_used) { |
8607 | 11.8k | info->varvars_used = true; |
8608 | 11.8k | } |
8609 | 342k | if (zend_hash_num_elements(&inner_info.uses)) { |
8610 | 308k | zend_hash_copy(&info->uses, &inner_info.uses, NULL); |
8611 | 308k | } |
8612 | 342k | zend_hash_destroy(&inner_info.uses); |
8613 | 2.64M | } else if (!zend_ast_is_special(ast)) { |
8614 | 1.76M | uint32_t i, children = zend_ast_get_num_children(ast); |
8615 | 4.28M | for (i = 0; i < children; i++) { |
8616 | 2.51M | find_implicit_binds_recursively(info, ast->child[i]); |
8617 | 2.51M | } |
8618 | 1.76M | } |
8619 | 3.53M | } |
8620 | | |
8621 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast) |
8622 | 737k | { |
8623 | 737k | const zend_ast_list *param_list = zend_ast_get_list(params_ast); |
8624 | 737k | uint32_t i; |
8625 | | |
8626 | 737k | zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0); |
8627 | 737k | info->varvars_used = false; |
8628 | | |
8629 | 737k | find_implicit_binds_recursively(info, stmt_ast); |
8630 | | |
8631 | | /* Remove variables that are parameters */ |
8632 | 818k | for (i = 0; i < param_list->children; i++) { |
8633 | 81.1k | const zend_ast *param_ast = param_list->child[i]; |
8634 | 81.1k | zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1])); |
8635 | 81.1k | } |
8636 | 737k | } |
8637 | | |
8638 | | static void compile_implicit_lexical_binds( |
8639 | | const closure_info *info, znode *closure, zend_op_array *op_array) |
8640 | 394k | { |
8641 | 394k | zend_string *var_name; |
8642 | 394k | zend_op *opline; |
8643 | | |
8644 | | /* TODO We might want to use a special binding mode if varvars_used is set. */ |
8645 | 394k | if (zend_hash_num_elements(&info->uses) == 0) { |
8646 | 385k | return; |
8647 | 385k | } |
8648 | | |
8649 | 9.51k | if (!op_array->static_variables) { |
8650 | 9.51k | op_array->static_variables = zend_new_array(8); |
8651 | 9.51k | } |
8652 | | |
8653 | 117k | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8654 | 117k | zval *value = zend_hash_add( |
8655 | 117k | op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8656 | 117k | uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData); |
8657 | | |
8658 | 117k | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8659 | 117k | opline->op2_type = IS_CV; |
8660 | 117k | opline->op2.var = lookup_cv(var_name); |
8661 | 117k | opline->extended_value = offset | ZEND_BIND_IMPLICIT; |
8662 | 117k | } ZEND_HASH_FOREACH_END(); |
8663 | 9.51k | } |
8664 | | |
8665 | | static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */ |
8666 | 4.09k | { |
8667 | 4.09k | const zend_op_array *op_array = CG(active_op_array); |
8668 | 4.09k | const zend_ast_list *list = zend_ast_get_list(ast); |
8669 | 4.09k | uint32_t i; |
8670 | | |
8671 | 15.5k | for (i = 0; i < list->children; ++i) { |
8672 | 11.4k | uint32_t mode = ZEND_BIND_EXPLICIT; |
8673 | 11.4k | zend_ast *var_ast = list->child[i]; |
8674 | 11.4k | zend_string *var_name = zend_ast_get_str(var_ast); |
8675 | 11.4k | zval zv; |
8676 | 11.4k | ZVAL_NULL(&zv); |
8677 | | |
8678 | 11.4k | { |
8679 | 11.4k | int i; |
8680 | 31.9k | for (i = 0; i < op_array->last_var; i++) { |
8681 | 20.5k | if (zend_string_equals(op_array->vars[i], var_name)) { |
8682 | 5 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, |
8683 | 5 | "Cannot use lexical variable $%S as a parameter name", var_name); |
8684 | 5 | } |
8685 | 20.5k | } |
8686 | 11.4k | } |
8687 | | |
8688 | 11.4k | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
8689 | | |
8690 | 11.4k | if (var_ast->attr) { |
8691 | 3.11k | mode |= ZEND_BIND_REF; |
8692 | 3.11k | } |
8693 | | |
8694 | 11.4k | zend_compile_static_var_common(var_name, &zv, mode); |
8695 | 11.4k | } |
8696 | 4.09k | } |
8697 | | /* }}} */ |
8698 | | |
8699 | | static void zend_compile_implicit_closure_uses(const closure_info *info) |
8700 | 394k | { |
8701 | 394k | zend_string *var_name; |
8702 | 888k | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8703 | 888k | zval zv; |
8704 | 888k | ZVAL_NULL(&zv); |
8705 | 888k | zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT); |
8706 | 888k | } ZEND_HASH_FOREACH_END(); |
8707 | 394k | } |
8708 | | |
8709 | 6.12k | static void add_stringable_interface(zend_class_entry *ce) { |
8710 | 16.6k | for (uint32_t i = 0; i < ce->num_interfaces; i++) { |
8711 | 10.5k | if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) { |
8712 | | /* Interface already explicitly implemented */ |
8713 | 49 | return; |
8714 | 49 | } |
8715 | 10.5k | } |
8716 | | |
8717 | 6.07k | ce->num_interfaces++; |
8718 | 6.07k | ce->interface_names = |
8719 | 6.07k | erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
8720 | | // TODO: Add known interned strings instead? |
8721 | 6.07k | ce->interface_names[ce->num_interfaces - 1].name = |
8722 | 6.07k | ZSTR_INIT_LITERAL("Stringable", 0); |
8723 | 6.07k | ce->interface_names[ce->num_interfaces - 1].lc_name = |
8724 | 6.07k | ZSTR_INIT_LITERAL("stringable", 0); |
8725 | 6.07k | } |
8726 | | |
8727 | | static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */ |
8728 | 146k | { |
8729 | 146k | zend_class_entry *ce = CG(active_class_entry); |
8730 | 146k | bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0; |
8731 | 146k | uint32_t fn_flags = op_array->fn_flags; |
8732 | | |
8733 | 146k | zend_string *lcname; |
8734 | | |
8735 | 146k | if (fn_flags & ZEND_ACC_READONLY) { |
8736 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier"); |
8737 | 0 | } |
8738 | | |
8739 | 146k | if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) { |
8740 | 339 | zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes"); |
8741 | 339 | } |
8742 | | |
8743 | 146k | if ((fn_flags & ZEND_ACC_ABSTRACT) |
8744 | 467 | && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) { |
8745 | | // Don't say that the class should be declared abstract if it is |
8746 | | // anonymous or an enum and can't be abstract |
8747 | 35 | if (ce->ce_flags & ZEND_ACC_ANON_CLASS) { |
8748 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract", |
8749 | 5 | ZSTR_VAL(name)); |
8750 | 30 | } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) { |
8751 | 20 | zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract", |
8752 | 20 | zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8753 | 20 | } else { |
8754 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract", |
8755 | 10 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8756 | 10 | } |
8757 | 35 | } |
8758 | | |
8759 | 146k | if (in_interface) { |
8760 | 823 | if (!(fn_flags & ZEND_ACC_PUBLIC)) { |
8761 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method " |
8762 | 1 | "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8763 | 1 | } |
8764 | 822 | if (fn_flags & ZEND_ACC_FINAL) { |
8765 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Interface method " |
8766 | 5 | "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8767 | 5 | } |
8768 | 817 | op_array->fn_flags |= ZEND_ACC_ABSTRACT; |
8769 | 817 | } |
8770 | | |
8771 | 146k | if (op_array->fn_flags & ZEND_ACC_ABSTRACT) { |
8772 | 1.24k | if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8773 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private", |
8774 | 5 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8775 | 5 | } |
8776 | | |
8777 | 1.24k | if (has_body) { |
8778 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body", |
8779 | 5 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8780 | 5 | } |
8781 | | |
8782 | 1.23k | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
8783 | 145k | } else if (!has_body) { |
8784 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body", |
8785 | 8 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8786 | 8 | } |
8787 | | |
8788 | 146k | op_array->scope = ce; |
8789 | 146k | op_array->function_name = zend_string_copy(name); |
8790 | | |
8791 | 146k | lcname = zend_string_tolower(name); |
8792 | 146k | lcname = zend_new_interned_string(lcname); |
8793 | | |
8794 | 146k | if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) { |
8795 | 23 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", |
8796 | 23 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8797 | 23 | } |
8798 | | |
8799 | 146k | zend_add_magic_method(ce, (zend_function *) op_array, lcname); |
8800 | 146k | if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME) |
8801 | 6.14k | && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8802 | 6.12k | add_stringable_interface(ce); |
8803 | 6.12k | } |
8804 | | |
8805 | 146k | return lcname; |
8806 | 146k | } |
8807 | | /* }}} */ |
8808 | | |
8809 | 1.98M | static uint32_t zend_add_dynamic_func_def(zend_op_array *def) { |
8810 | 1.98M | zend_op_array *op_array = CG(active_op_array); |
8811 | 1.98M | uint32_t def_offset = op_array->num_dynamic_func_defs++; |
8812 | 1.98M | op_array->dynamic_func_defs = erealloc( |
8813 | 1.98M | op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *)); |
8814 | 1.98M | op_array->dynamic_func_defs[def_offset] = def; |
8815 | 1.98M | return def_offset; |
8816 | 1.98M | } |
8817 | | |
8818 | | enum func_decl_level { |
8819 | | FUNC_DECL_LEVEL_TOPLEVEL, |
8820 | | FUNC_DECL_LEVEL_NESTED, |
8821 | | FUNC_DECL_LEVEL_CONSTEXPR, |
8822 | | }; |
8823 | | |
8824 | | static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */ |
8825 | 2.00M | { |
8826 | 2.00M | zend_string *unqualified_name, *name, *lcname; |
8827 | 2.00M | zend_op *opline; |
8828 | | |
8829 | 2.00M | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8830 | 1.98M | zend_string *filename = op_array->filename; |
8831 | 1.98M | uint32_t start_lineno = decl->start_lineno; |
8832 | | |
8833 | 1.98M | zend_string *class = zend_empty_string; |
8834 | 1.98M | zend_string *separator = zend_empty_string; |
8835 | 1.98M | zend_string *function = filename; |
8836 | 1.98M | const char *parens = ""; |
8837 | | |
8838 | 1.98M | if (CG(active_op_array) && CG(active_op_array)->function_name) { |
8839 | 1.97M | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
8840 | | /* If the parent function is a closure, don't redundantly |
8841 | | * add the classname and parentheses. |
8842 | | */ |
8843 | 1.97M | function = CG(active_op_array)->function_name; |
8844 | 1.97M | } else { |
8845 | 2.00k | function = CG(active_op_array)->function_name; |
8846 | 2.00k | parens = "()"; |
8847 | | |
8848 | 2.00k | if (CG(active_class_entry) && CG(active_class_entry)->name) { |
8849 | 1.27k | class = CG(active_class_entry)->name; |
8850 | 1.27k | separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM); |
8851 | 1.27k | } |
8852 | 2.00k | } |
8853 | 1.97M | } |
8854 | | |
8855 | 1.98M | unqualified_name = zend_strpprintf_unchecked( |
8856 | 1.98M | 0, |
8857 | 1.98M | "{closure:%S%S%S%s:%" PRIu32 "}", |
8858 | 1.98M | class, |
8859 | 1.98M | separator, |
8860 | 1.98M | function, |
8861 | 1.98M | parens, |
8862 | 1.98M | start_lineno |
8863 | 1.98M | ); |
8864 | | |
8865 | 1.98M | op_array->function_name = name = unqualified_name; |
8866 | 1.98M | } else { |
8867 | 22.0k | unqualified_name = decl->name; |
8868 | 22.0k | op_array->function_name = name = zend_prefix_with_ns(unqualified_name); |
8869 | 22.0k | } |
8870 | | |
8871 | 2.00M | lcname = zend_string_tolower(name); |
8872 | | |
8873 | 2.00M | if (FC(imports_function)) { |
8874 | 106 | const zend_string *import_name = |
8875 | 106 | zend_hash_find_ptr_lc(FC(imports_function), unqualified_name); |
8876 | 106 | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
8877 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)", |
8878 | 9 | ZSTR_VAL(name)); |
8879 | 9 | } |
8880 | 106 | } |
8881 | | |
8882 | 2.00M | if (zend_string_equals_literal(lcname, "__autoload")) { |
8883 | 1 | zend_error_noreturn(E_COMPILE_ERROR, |
8884 | 1 | "__autoload() is no longer supported, use spl_autoload_register() instead"); |
8885 | 1 | } |
8886 | | |
8887 | 2.00M | if (zend_string_equals_literal_ci(unqualified_name, "assert")) { |
8888 | 5 | zend_error(E_COMPILE_ERROR, |
8889 | 5 | "Defining a custom assert() function is not allowed, " |
8890 | 5 | "as the function has special semantics"); |
8891 | 5 | } |
8892 | | |
8893 | 2.00M | zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION); |
8894 | 2.00M | switch (level) { |
8895 | 1.98M | case FUNC_DECL_LEVEL_NESTED: { |
8896 | 1.98M | uint32_t func_ref = zend_add_dynamic_func_def(op_array); |
8897 | 1.98M | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8898 | 1.98M | opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL); |
8899 | 1.98M | opline->op2.num = func_ref; |
8900 | 1.98M | } else { |
8901 | 2.29k | opline = get_next_op(); |
8902 | 2.29k | opline->opcode = ZEND_DECLARE_FUNCTION; |
8903 | 2.29k | opline->op1_type = IS_CONST; |
8904 | 2.29k | LITERAL_STR(opline->op1, zend_string_copy(lcname)); |
8905 | 2.29k | opline->op2.num = func_ref; |
8906 | 2.29k | } |
8907 | 1.98M | break; |
8908 | 0 | } |
8909 | 155 | case FUNC_DECL_LEVEL_CONSTEXPR: |
8910 | 19.8k | case FUNC_DECL_LEVEL_TOPLEVEL: |
8911 | | /* Nothing to do. */ |
8912 | 19.8k | break; |
8913 | 2.00M | } |
8914 | 2.00M | return lcname; |
8915 | 2.00M | } |
8916 | | /* }}} */ |
8917 | | |
8918 | | static zend_op_array *zend_compile_func_decl_ex( |
8919 | | znode *result, zend_ast *ast, enum func_decl_level level, |
8920 | | zend_string *property_info_name, |
8921 | | zend_property_hook_kind hook_kind |
8922 | 2.16M | ) { |
8923 | 2.16M | zend_ast_decl *decl = (zend_ast_decl *) ast; |
8924 | 2.16M | zend_ast *params_ast = decl->child[0]; |
8925 | 2.16M | zend_ast *uses_ast = decl->child[1]; |
8926 | 2.16M | zend_ast *stmt_ast = decl->child[2]; |
8927 | 2.16M | zend_ast *return_type_ast = decl->child[3]; |
8928 | 2.16M | bool is_method = decl->kind == ZEND_AST_METHOD; |
8929 | 2.16M | zend_string *lcname = NULL; |
8930 | 2.16M | bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK; |
8931 | | |
8932 | 2.16M | zend_class_entry *orig_class_entry = CG(active_class_entry); |
8933 | 2.16M | zend_op_array *orig_op_array = CG(active_op_array); |
8934 | 2.16M | zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array)); |
8935 | 2.16M | zend_oparray_context orig_oparray_context; |
8936 | 2.16M | closure_info info; |
8937 | | |
8938 | 2.16M | init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE); |
8939 | | |
8940 | 2.16M | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
8941 | 0 | op_array->fn_flags |= ZEND_ACC_PRELOADED; |
8942 | 0 | } |
8943 | | |
8944 | 2.16M | op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES); |
8945 | 2.16M | op_array->fn_flags |= decl->flags; |
8946 | 2.16M | op_array->line_start = decl->start_lineno; |
8947 | 2.16M | op_array->line_end = decl->end_lineno; |
8948 | 2.16M | if (decl->doc_comment) { |
8949 | 527 | op_array->doc_comment = zend_string_copy(decl->doc_comment); |
8950 | 527 | } |
8951 | | |
8952 | 2.16M | if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) { |
8953 | 1.98M | op_array->fn_flags |= ZEND_ACC_CLOSURE; |
8954 | 1.98M | } |
8955 | | |
8956 | 2.16M | if (is_hook) { |
8957 | 5.11k | zend_class_entry *ce = CG(active_class_entry); |
8958 | 5.11k | op_array->scope = ce; |
8959 | 5.11k | op_array->function_name = zend_string_copy(decl->name); |
8960 | 2.15M | } else if (is_method) { |
8961 | 146k | bool has_body = stmt_ast != NULL; |
8962 | 146k | lcname = zend_begin_method_decl(op_array, decl->name, has_body); |
8963 | 2.00M | } else { |
8964 | 2.00M | lcname = zend_begin_func_decl(result, op_array, decl, level); |
8965 | 2.00M | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
8966 | 394k | find_implicit_binds(&info, params_ast, stmt_ast); |
8967 | 394k | compile_implicit_lexical_binds(&info, result, op_array); |
8968 | 1.61M | } else if (uses_ast) { |
8969 | 4.11k | zend_compile_closure_binding(result, op_array, uses_ast); |
8970 | 4.11k | } |
8971 | 2.00M | } |
8972 | | |
8973 | 2.16M | CG(active_op_array) = op_array; |
8974 | | |
8975 | 2.16M | zend_oparray_context_begin(&orig_oparray_context, op_array); |
8976 | 2.16M | CG(context).active_property_info_name = property_info_name; |
8977 | 2.16M | CG(context).active_property_hook_kind = hook_kind; |
8978 | | |
8979 | 2.16M | if (decl->child[4]) { |
8980 | 1.41M | int target = ZEND_ATTRIBUTE_TARGET_FUNCTION; |
8981 | | |
8982 | 1.41M | if (is_method || is_hook) { |
8983 | 877 | target = ZEND_ATTRIBUTE_TARGET_METHOD; |
8984 | 877 | } |
8985 | | |
8986 | 1.41M | zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0); |
8987 | | |
8988 | 1.41M | const zend_attribute *override_attribute = zend_get_attribute_str( |
8989 | 1.41M | op_array->attributes, |
8990 | 1.41M | "override", |
8991 | 1.41M | sizeof("override")-1 |
8992 | 1.41M | ); |
8993 | | |
8994 | 1.41M | if (override_attribute) { |
8995 | 349 | op_array->fn_flags |= ZEND_ACC_OVERRIDE; |
8996 | 349 | } |
8997 | | |
8998 | 1.41M | const zend_attribute *deprecated_attribute = zend_get_attribute_str( |
8999 | 1.41M | op_array->attributes, |
9000 | 1.41M | "deprecated", |
9001 | 1.41M | sizeof("deprecated")-1 |
9002 | 1.41M | ); |
9003 | | |
9004 | 1.41M | if (deprecated_attribute) { |
9005 | 293 | op_array->fn_flags |= ZEND_ACC_DEPRECATED; |
9006 | 293 | } |
9007 | | |
9008 | | // ZEND_ACC_NODISCARD is added via an attribute validator |
9009 | 1.41M | } |
9010 | | |
9011 | | /* Do not leak the class scope into free standing functions, even if they are dynamically |
9012 | | * defined inside a class method. This is necessary for correct handling of magic constants. |
9013 | | * For example __CLASS__ should always be "" inside a free standing function. */ |
9014 | 2.16M | if (decl->kind == ZEND_AST_FUNC_DECL) { |
9015 | 21.9k | CG(active_class_entry) = NULL; |
9016 | 21.9k | } |
9017 | | |
9018 | 2.16M | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9019 | 19.7k | op_array->fn_flags |= ZEND_ACC_TOP_LEVEL; |
9020 | 19.7k | } |
9021 | | |
9022 | 2.16M | { |
9023 | | /* Push a separator to the loop variable stack */ |
9024 | 2.16M | zend_loop_var dummy_var; |
9025 | 2.16M | dummy_var.opcode = ZEND_RETURN; |
9026 | | |
9027 | 2.16M | zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var); |
9028 | 2.16M | } |
9029 | | |
9030 | 2.16M | zend_compile_params(params_ast, return_type_ast, |
9031 | 2.16M | is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME) ? IS_STRING : 0); |
9032 | 2.16M | if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) { |
9033 | 380k | if (CG(active_class_entry) != NULL) { |
9034 | 281 | if (zend_is_constructor(CG(active_op_array)->function_name)) { |
9035 | 5 | zend_error(E_DEPRECATED, "Making a constructor a Generator is deprecated"); |
9036 | 276 | } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { |
9037 | 5 | zend_error(E_DEPRECATED, "Making a destructor a Generator is deprecated"); |
9038 | 5 | } |
9039 | 281 | } |
9040 | | |
9041 | 380k | zend_mark_function_as_generator(); |
9042 | 380k | zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL); |
9043 | 380k | } |
9044 | 2.16M | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
9045 | 394k | zend_compile_implicit_closure_uses(&info); |
9046 | 394k | zend_hash_destroy(&info.uses); |
9047 | 1.76M | } else if (uses_ast) { |
9048 | 4.09k | zend_compile_closure_uses(uses_ast); |
9049 | 4.09k | } |
9050 | | |
9051 | 2.16M | if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) { |
9052 | 10.5k | bool needs_return = true; |
9053 | 10.5k | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
9054 | 1.30k | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
9055 | 1.30k | needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER); |
9056 | 1.30k | } |
9057 | 10.5k | if (needs_return) { |
9058 | 10.1k | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
9059 | 10.1k | decl->child[2] = stmt_ast; |
9060 | 10.1k | } |
9061 | 10.5k | } |
9062 | | |
9063 | 2.16M | if (op_array->fn_flags & ZEND_ACC_NODISCARD) { |
9064 | | /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only |
9065 | | * if the method is not a hook; if it is a hook, then the validator |
9066 | | * will have returned an error message, even if the error message was |
9067 | | * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD |
9068 | | * flag should not have been added. */ |
9069 | 336 | ZEND_ASSERT(!is_hook); |
9070 | | |
9071 | 336 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
9072 | 263 | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
9073 | 263 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) { |
9074 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9075 | 5 | "A void %s does not return a value, but #[\\NoDiscard] requires a return value", |
9076 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
9077 | 5 | } |
9078 | | |
9079 | 258 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
9080 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9081 | 5 | "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value", |
9082 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
9083 | 5 | } |
9084 | 258 | } |
9085 | 336 | } |
9086 | | |
9087 | 2.16M | zend_compile_stmt(stmt_ast); |
9088 | | |
9089 | 2.16M | if (is_method) { |
9090 | 146k | CG(zend_lineno) = decl->start_lineno; |
9091 | 146k | zend_check_magic_method_implementation( |
9092 | 146k | CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR); |
9093 | 2.01M | } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9094 | | /* Only register the function after a successful compile */ |
9095 | 19.2k | if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) { |
9096 | 110 | CG(zend_lineno) = decl->start_lineno; |
9097 | 110 | do_bind_function_error(lcname, op_array, true); |
9098 | 110 | } |
9099 | 19.2k | } |
9100 | | |
9101 | | /* put the implicit return on the really last line */ |
9102 | 2.16M | CG(zend_lineno) = decl->end_lineno; |
9103 | | |
9104 | 2.16M | zend_do_extended_stmt(NULL); |
9105 | 2.16M | zend_emit_final_return(false); |
9106 | | |
9107 | 2.16M | pass_two(CG(active_op_array)); |
9108 | 2.16M | zend_oparray_context_end(&orig_oparray_context); |
9109 | | |
9110 | | /* Pop the loop variable stack separator */ |
9111 | 2.16M | zend_stack_del_top(&CG(loop_var_stack)); |
9112 | | |
9113 | 2.16M | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9114 | 19.1k | zend_observer_function_declared_notify(op_array, lcname); |
9115 | 19.1k | } |
9116 | | |
9117 | 2.16M | if (lcname != NULL) { |
9118 | 2.15M | zend_string_release_ex(lcname, 0); |
9119 | 2.15M | } |
9120 | | |
9121 | 2.16M | CG(active_op_array) = orig_op_array; |
9122 | 2.16M | CG(active_class_entry) = orig_class_entry; |
9123 | | |
9124 | 2.16M | return op_array; |
9125 | 2.16M | } |
9126 | | |
9127 | | static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level) |
9128 | 2.15M | { |
9129 | 2.15M | return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1); |
9130 | 2.15M | } |
9131 | | |
9132 | 6.01k | zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) { |
9133 | 6.01k | if (zend_string_equals_literal_ci(name, "get")) { |
9134 | 3.72k | return ZEND_PROPERTY_HOOK_GET; |
9135 | 3.72k | } else if (zend_string_equals_literal_ci(name, "set")) { |
9136 | 2.16k | return ZEND_PROPERTY_HOOK_SET; |
9137 | 2.16k | } else { |
9138 | 120 | return (zend_property_hook_kind)-1; |
9139 | 120 | } |
9140 | 6.01k | } |
9141 | | |
9142 | | static void zend_compile_property_hooks( |
9143 | | zend_property_info *prop_info, zend_string *prop_name, |
9144 | | zend_ast *prop_type_ast, const zend_ast_list *hooks) |
9145 | 4.26k | { |
9146 | 4.26k | zend_class_entry *ce = CG(active_class_entry); |
9147 | | |
9148 | 4.26k | if (prop_info->flags & ZEND_ACC_READONLY) { |
9149 | 13 | zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly"); |
9150 | 13 | } |
9151 | | |
9152 | 4.25k | if (hooks->children == 0) { |
9153 | 17 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty"); |
9154 | 17 | } |
9155 | | |
9156 | 9.31k | for (uint32_t i = 0; i < hooks->children; i++) { |
9157 | 5.33k | zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i]; |
9158 | 5.33k | zend_string *name = hook->name; |
9159 | 5.33k | zend_ast *stmt_ast = hook->child[2]; |
9160 | 5.33k | zend_ast **return_type_ast_ptr = NULL; |
9161 | 5.33k | zend_ast **value_type_ast_ptr = NULL; |
9162 | 5.33k | CG(zend_lineno) = hook->start_lineno; |
9163 | | |
9164 | | /* Non-private hooks are always public. This avoids having to copy the hook when inheriting |
9165 | | * hooks from protected properties to public ones. */ |
9166 | 5.33k | uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE; |
9167 | 5.33k | hook->flags |= hook_visibility; |
9168 | | |
9169 | 5.33k | if (prop_info->flags & ZEND_ACC_STATIC) { |
9170 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property"); |
9171 | 6 | } |
9172 | 5.32k | if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) { |
9173 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private"); |
9174 | 5 | } |
9175 | 5.32k | if ((ce->ce_flags & ZEND_ACC_INTERFACE) |
9176 | 5.01k | || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) { |
9177 | 767 | hook->flags |= ZEND_ACC_ABSTRACT; |
9178 | | |
9179 | 767 | if (stmt_ast) { |
9180 | 27 | zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body"); |
9181 | 27 | } |
9182 | 740 | if (hook->flags & ZEND_ACC_PRIVATE) { |
9183 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9184 | 5 | "Property hook cannot be both abstract and private"); |
9185 | 5 | } |
9186 | 735 | if (hook->flags & ZEND_ACC_FINAL) { |
9187 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final"); |
9188 | 6 | } |
9189 | 4.55k | } else if (!stmt_ast) { |
9190 | 19 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body"); |
9191 | 19 | } |
9192 | | |
9193 | 5.26k | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name); |
9194 | 5.26k | if (hook_kind == (zend_property_hook_kind)-1) { |
9195 | 120 | zend_error_noreturn(E_COMPILE_ERROR, |
9196 | 120 | "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"", |
9197 | 120 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9198 | 120 | } |
9199 | | |
9200 | 5.14k | if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
9201 | 1.92k | stmt_ast = stmt_ast->child[0]; |
9202 | 1.92k | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9203 | 1.60k | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
9204 | 1.60k | } else { |
9205 | 323 | ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET); |
9206 | 323 | stmt_ast = zend_ast_create(ZEND_AST_ASSIGN, |
9207 | 323 | zend_ast_create(ZEND_AST_PROP, |
9208 | 323 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))), |
9209 | 323 | zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))), |
9210 | 323 | stmt_ast); |
9211 | 323 | } |
9212 | 1.92k | stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast); |
9213 | 1.92k | hook->child[2] = stmt_ast; |
9214 | 1.92k | } |
9215 | | |
9216 | 5.14k | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9217 | 3.23k | if (hook->child[0]) { |
9218 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list", |
9219 | 7 | ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9220 | 7 | } |
9221 | | |
9222 | 3.22k | hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); |
9223 | | |
9224 | 3.22k | return_type_ast_ptr = &hook->child[3]; |
9225 | 3.22k | *return_type_ast_ptr = prop_type_ast; |
9226 | 3.22k | } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9227 | 1.91k | if (hook->child[0]) { |
9228 | 242 | const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]); |
9229 | 242 | if (param_list->children != 1) { |
9230 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters", |
9231 | 1 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9232 | 1 | } |
9233 | 241 | const zend_ast *value_param_ast = param_list->child[0]; |
9234 | 241 | if (value_param_ast->attr & ZEND_PARAM_REF) { |
9235 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference", |
9236 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9237 | 5 | } |
9238 | 236 | if (value_param_ast->attr & ZEND_PARAM_VARIADIC) { |
9239 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic", |
9240 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9241 | 5 | } |
9242 | 231 | if (value_param_ast->child[2]) { |
9243 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value", |
9244 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9245 | 5 | } |
9246 | 226 | if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) { |
9247 | 5 | zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name); |
9248 | 5 | } |
9249 | 1.67k | } else { |
9250 | 1.67k | zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE)); |
9251 | 1.67k | zend_ast *param = zend_ast_create( |
9252 | 1.67k | ZEND_AST_PARAM, prop_type_ast, param_name_ast, |
9253 | 1.67k | /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL, |
9254 | 1.67k | /* hooks */ NULL); |
9255 | 1.67k | value_type_ast_ptr = ¶m->child[0]; |
9256 | 1.67k | hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param); |
9257 | 1.67k | } |
9258 | 1.89k | zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE); |
9259 | 1.89k | return_type->attr = IS_VOID; |
9260 | 1.89k | hook->child[3] = return_type; |
9261 | 1.89k | } else { |
9262 | 0 | ZEND_UNREACHABLE(); |
9263 | 0 | } |
9264 | | |
9265 | 5.11k | hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name)); |
9266 | | |
9267 | 5.11k | zend_function *func = (zend_function *) zend_compile_func_decl_ex( |
9268 | 5.11k | NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind); |
9269 | | |
9270 | 5.11k | func->common.prop_info = prop_info; |
9271 | | |
9272 | 5.11k | if (!prop_info->hooks) { |
9273 | 3.98k | prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9274 | 3.98k | memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9275 | 3.98k | } |
9276 | | |
9277 | 5.11k | if (prop_info->hooks[hook_kind]) { |
9278 | 22 | zend_error_noreturn(E_COMPILE_ERROR, |
9279 | 22 | "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name)); |
9280 | 22 | } |
9281 | 5.09k | prop_info->hooks[hook_kind] = func; |
9282 | | |
9283 | 5.09k | if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9284 | 1.86k | switch (zend_verify_property_hook_variance(prop_info, func)) { |
9285 | 1.76k | case INHERITANCE_SUCCESS: |
9286 | 1.76k | break; |
9287 | 85 | case INHERITANCE_UNRESOLVED: |
9288 | 85 | ce->num_hooked_prop_variance_checks++; |
9289 | 85 | break; |
9290 | 9 | case INHERITANCE_ERROR: |
9291 | 9 | zend_hooked_property_variance_error(prop_info); |
9292 | 0 | case INHERITANCE_WARNING: |
9293 | 0 | ZEND_UNREACHABLE(); |
9294 | 1.86k | } |
9295 | 1.86k | } |
9296 | | |
9297 | 5.08k | zend_string_release(name); |
9298 | | /* Un-share type ASTs to avoid double-frees of zval nodes. */ |
9299 | 5.08k | if (return_type_ast_ptr) { |
9300 | 3.17k | *return_type_ast_ptr = NULL; |
9301 | 3.17k | } |
9302 | 5.08k | if (value_type_ast_ptr) { |
9303 | 1.64k | *value_type_ast_ptr = NULL; |
9304 | 1.64k | } |
9305 | 5.08k | } |
9306 | | |
9307 | 3.98k | ce->num_hooked_props++; |
9308 | | |
9309 | | /* See zend_link_hooked_object_iter(). */ |
9310 | 3.98k | #ifndef ZEND_OPCACHE_SHM_REATTACHMENT |
9311 | 3.98k | if (!ce->get_iterator) { |
9312 | | /* Will be removed again, in case of Iterator or IteratorAggregate. */ |
9313 | 2.95k | ce->get_iterator = zend_hooked_object_get_iterator; |
9314 | 2.95k | } |
9315 | 3.98k | #endif |
9316 | | |
9317 | 3.98k | if (!prop_info->ce->parent_name) { |
9318 | 2.62k | zend_verify_hooked_property(ce, prop_info, prop_name); |
9319 | 2.62k | } |
9320 | 3.98k | } |
9321 | | |
9322 | | static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */ |
9323 | 31.9k | { |
9324 | 31.9k | const zend_ast_list *list = zend_ast_get_list(ast); |
9325 | 31.9k | zend_class_entry *ce = CG(active_class_entry); |
9326 | 31.9k | uint32_t i, children = list->children; |
9327 | | |
9328 | 31.9k | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9329 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name)); |
9330 | 14 | } |
9331 | | |
9332 | 31.9k | if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) { |
9333 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private"); |
9334 | 5 | } |
9335 | | |
9336 | 31.9k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9337 | 314 | if (flags & ZEND_ACC_FINAL) { |
9338 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final"); |
9339 | 5 | } |
9340 | 309 | if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) { |
9341 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private"); |
9342 | 5 | } |
9343 | 304 | if (flags & ZEND_ACC_ABSTRACT) { |
9344 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9345 | 5 | "Property in interface cannot be explicitly abstract. " |
9346 | 5 | "All interface members are implicitly abstract"); |
9347 | 5 | } |
9348 | 299 | flags |= ZEND_ACC_ABSTRACT; |
9349 | 299 | } |
9350 | | |
9351 | 64.5k | for (i = 0; i < children; ++i) { |
9352 | 32.8k | zend_property_info *info; |
9353 | 32.8k | zend_ast *prop_ast = list->child[i]; |
9354 | 32.8k | zend_ast *name_ast = prop_ast->child[0]; |
9355 | 32.8k | zend_ast **value_ast_ptr = &prop_ast->child[1]; |
9356 | 32.8k | zend_ast *doc_comment_ast = prop_ast->child[2]; |
9357 | 32.8k | zend_ast *hooks_ast = prop_ast->child[3]; |
9358 | 32.8k | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9359 | 32.8k | zend_string *doc_comment = NULL; |
9360 | 32.8k | zval value_zv; |
9361 | 32.8k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9362 | 32.8k | flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0; |
9363 | | |
9364 | 32.8k | zend_string *old_active_property_info_name = CG(context).active_property_info_name; |
9365 | 32.8k | CG(context).active_property_info_name = name; |
9366 | | |
9367 | 32.8k | if (!hooks_ast) { |
9368 | 28.6k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9369 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9370 | 5 | "Interfaces may only include hooked properties"); |
9371 | 5 | } |
9372 | 28.6k | if (flags & ZEND_ACC_ABSTRACT) { |
9373 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9374 | 5 | "Only hooked properties may be declared abstract"); |
9375 | 5 | } |
9376 | 28.6k | } |
9377 | 32.8k | if ((flags & ZEND_ACC_ABSTRACT)) { |
9378 | 652 | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
9379 | 652 | } |
9380 | | |
9381 | 32.8k | if (type_ast) { |
9382 | 16.0k | type = zend_compile_typename(type_ast); |
9383 | | |
9384 | 16.0k | if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) { |
9385 | 5 | zend_string *str = zend_type_to_string(type); |
9386 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9387 | 5 | "Property %s::$%s cannot have type %s", |
9388 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9389 | 5 | } |
9390 | 16.0k | } |
9391 | | |
9392 | | /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */ |
9393 | 32.8k | if (doc_comment_ast) { |
9394 | 475 | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
9395 | 475 | } |
9396 | | |
9397 | 32.8k | if (zend_hash_exists(&ce->properties_info, name)) { |
9398 | 32 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
9399 | 32 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9400 | 32 | } |
9401 | | |
9402 | 32.7k | if (*value_ast_ptr) { |
9403 | 10.3k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9404 | | |
9405 | 10.3k | if (ZEND_TYPE_IS_SET(type) && Z_TYPE(value_zv) != IS_CONSTANT_AST |
9406 | 2.19k | && !zend_is_valid_default_value(type, &value_zv)) { |
9407 | 93 | zend_string *str = zend_type_to_string(type); |
9408 | 93 | if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) { |
9409 | 21 | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
9410 | 21 | zend_string *nullable_str = zend_type_to_string(type); |
9411 | | |
9412 | 21 | zend_error_noreturn(E_COMPILE_ERROR, |
9413 | 21 | "Default value for property of type %s may not be null. " |
9414 | 21 | "Use the nullable type %s to allow null default value", |
9415 | 21 | ZSTR_VAL(str), ZSTR_VAL(nullable_str)); |
9416 | 72 | } else { |
9417 | 72 | zend_error_noreturn(E_COMPILE_ERROR, |
9418 | 72 | "Cannot use %s as default value for property %s::$%s of type %s", |
9419 | 72 | zend_zval_value_name(&value_zv), |
9420 | 72 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9421 | 72 | } |
9422 | 93 | } |
9423 | 22.4k | } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) { |
9424 | 6.98k | ZVAL_NULL(&value_zv); |
9425 | 15.4k | } else { |
9426 | 15.4k | ZVAL_UNDEF(&value_zv); |
9427 | 15.4k | } |
9428 | | |
9429 | 32.6k | if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
9430 | 62 | flags |= ZEND_ACC_READONLY; |
9431 | 62 | } |
9432 | | |
9433 | 32.6k | if (flags & ZEND_ACC_READONLY) { |
9434 | 539 | if (!ZEND_TYPE_IS_SET(type)) { |
9435 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
9436 | 11 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9437 | 11 | } |
9438 | 528 | if (!Z_ISUNDEF(value_zv)) { |
9439 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
9440 | 7 | "Readonly property %s::$%s cannot have default value", |
9441 | 7 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9442 | 7 | } |
9443 | 521 | if (flags & ZEND_ACC_STATIC) { |
9444 | 9 | zend_error_noreturn(E_COMPILE_ERROR, |
9445 | 9 | "Static property %s::$%s cannot be readonly", |
9446 | 9 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9447 | 9 | } |
9448 | 521 | } |
9449 | | |
9450 | 32.6k | info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type); |
9451 | | |
9452 | 32.6k | if (hooks_ast) { |
9453 | 4.15k | zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast)); |
9454 | 4.15k | } |
9455 | | |
9456 | 32.6k | if (attr_ast) { |
9457 | 1.89k | zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0); |
9458 | | |
9459 | 1.89k | const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1); |
9460 | 1.89k | if (override_attribute) { |
9461 | 140 | info->flags |= ZEND_ACC_OVERRIDE; |
9462 | 140 | } |
9463 | 1.89k | } |
9464 | | |
9465 | 32.6k | CG(context).active_property_info_name = old_active_property_info_name; |
9466 | 32.6k | } |
9467 | 31.9k | } |
9468 | | /* }}} */ |
9469 | | |
9470 | | static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */ |
9471 | 31.9k | { |
9472 | 31.9k | zend_ast *type_ast = ast->child[0]; |
9473 | 31.9k | zend_ast *prop_ast = ast->child[1]; |
9474 | 31.9k | zend_ast *attr_ast = ast->child[2]; |
9475 | | |
9476 | 31.9k | zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast); |
9477 | 31.9k | } |
9478 | | /* }}} */ |
9479 | | |
9480 | | static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */ |
9481 | 195k | { |
9482 | 195k | if (attr & ZEND_ACC_STATIC) { |
9483 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias"); |
9484 | 195k | } else if (attr & ZEND_ACC_ABSTRACT) { |
9485 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias"); |
9486 | 5 | } |
9487 | 195k | } |
9488 | | /* }}} */ |
9489 | | |
9490 | | static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast) |
9491 | 7.29k | { |
9492 | 7.29k | const zend_ast_list *list = zend_ast_get_list(ast); |
9493 | 7.29k | zend_class_entry *ce = CG(active_class_entry); |
9494 | 7.29k | uint32_t i, children = list->children; |
9495 | | |
9496 | 14.6k | for (i = 0; i < children; ++i) { |
9497 | 7.35k | zend_class_constant *c; |
9498 | 7.35k | zend_ast *const_ast = list->child[i]; |
9499 | 7.35k | zend_ast *name_ast = const_ast->child[0]; |
9500 | 7.35k | zend_ast **value_ast_ptr = &const_ast->child[1]; |
9501 | 7.35k | zend_ast *doc_comment_ast = const_ast->child[2]; |
9502 | 7.35k | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9503 | 7.35k | zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
9504 | 7.35k | zval value_zv; |
9505 | 7.35k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9506 | | |
9507 | 7.35k | if (type_ast) { |
9508 | 2.89k | type = zend_compile_typename(type_ast); |
9509 | | |
9510 | 2.89k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9511 | | |
9512 | 2.89k | if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) { |
9513 | 5 | zend_string *type_str = zend_type_to_string(type); |
9514 | | |
9515 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s", |
9516 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9517 | 5 | } |
9518 | 2.89k | } |
9519 | | |
9520 | 7.35k | if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) { |
9521 | 5 | zend_error_noreturn( |
9522 | 5 | E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes", |
9523 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name) |
9524 | 5 | ); |
9525 | 5 | } |
9526 | | |
9527 | 7.34k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9528 | | |
9529 | 7.34k | if (Z_TYPE(value_zv) != IS_CONSTANT_AST && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) { |
9530 | 35 | zend_string *type_str = zend_type_to_string(type); |
9531 | | |
9532 | 35 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s", |
9533 | 35 | zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9534 | 35 | } |
9535 | | |
9536 | 7.31k | c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type); |
9537 | | |
9538 | 7.31k | if (attr_ast) { |
9539 | 619 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
9540 | | |
9541 | 619 | const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
9542 | | |
9543 | 619 | if (deprecated) { |
9544 | 139 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
9545 | | /* For deprecated constants, we need to flag the zval for recursion |
9546 | | * detection. Make sure the zval is separated out of shm. */ |
9547 | 139 | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
9548 | 139 | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
9549 | 139 | } |
9550 | | |
9551 | 619 | const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1); |
9552 | 619 | if (override) { |
9553 | 109 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE; |
9554 | | /* We need to be able to remove the flag once the override is |
9555 | | * resolved. See ZEND_ACC_DEPRECATED above. */ |
9556 | 109 | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
9557 | 109 | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
9558 | 109 | } |
9559 | 619 | } |
9560 | 7.31k | } |
9561 | 7.29k | } |
9562 | | |
9563 | | static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */ |
9564 | 7.29k | { |
9565 | 7.29k | zend_ast *const_ast = ast->child[0]; |
9566 | 7.29k | zend_ast *attr_ast = ast->child[1]; |
9567 | 7.29k | zend_ast *type_ast = ast->child[2]; |
9568 | | |
9569 | 7.29k | zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast); |
9570 | 7.29k | } |
9571 | | /* }}} */ |
9572 | | |
9573 | | static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */ |
9574 | 196k | { |
9575 | 196k | zend_ast *class_ast = ast->child[0]; |
9576 | 196k | zend_ast *method_ast = ast->child[1]; |
9577 | | |
9578 | 196k | method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast)); |
9579 | | |
9580 | 196k | if (class_ast) { |
9581 | 2.08k | method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name"); |
9582 | 194k | } else { |
9583 | 194k | method_ref->class_name = NULL; |
9584 | 194k | } |
9585 | 196k | } |
9586 | | /* }}} */ |
9587 | | |
9588 | | static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */ |
9589 | 1.14k | { |
9590 | 1.14k | const zend_ast *method_ref_ast = ast->child[0]; |
9591 | 1.14k | zend_ast *insteadof_ast = ast->child[1]; |
9592 | 1.14k | const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast); |
9593 | 1.14k | uint32_t i; |
9594 | | |
9595 | 1.14k | zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*)); |
9596 | 1.14k | zend_compile_method_ref(method_ref_ast, &precedence->trait_method); |
9597 | 1.14k | precedence->num_excludes = insteadof_list->children; |
9598 | | |
9599 | 2.69k | for (i = 0; i < insteadof_list->children; ++i) { |
9600 | 1.55k | zend_ast *name_ast = insteadof_list->child[i]; |
9601 | 1.55k | precedence->exclude_class_names[i] = |
9602 | 1.55k | zend_resolve_const_class_name_reference(name_ast, "trait name"); |
9603 | 1.55k | } |
9604 | | |
9605 | 1.14k | zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence); |
9606 | 1.14k | } |
9607 | | /* }}} */ |
9608 | | |
9609 | | static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */ |
9610 | 195k | { |
9611 | 195k | const zend_ast *method_ref_ast = ast->child[0]; |
9612 | 195k | zend_ast *alias_ast = ast->child[1]; |
9613 | 195k | uint32_t modifiers = ast->attr; |
9614 | | |
9615 | 195k | zend_trait_alias *alias; |
9616 | | |
9617 | 195k | zend_check_trait_alias_modifiers(modifiers); |
9618 | | |
9619 | 195k | alias = emalloc(sizeof(zend_trait_alias)); |
9620 | 195k | zend_compile_method_ref(method_ref_ast, &alias->trait_method); |
9621 | 195k | alias->modifiers = modifiers; |
9622 | | |
9623 | 195k | if (alias_ast) { |
9624 | 194k | alias->alias = zend_string_copy(zend_ast_get_str(alias_ast)); |
9625 | 194k | } else { |
9626 | 294 | alias->alias = NULL; |
9627 | 294 | } |
9628 | | |
9629 | 195k | zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias); |
9630 | 195k | } |
9631 | | /* }}} */ |
9632 | | |
9633 | | static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */ |
9634 | 67.2k | { |
9635 | 67.2k | const zend_ast_list *traits = zend_ast_get_list(ast->child[0]); |
9636 | 67.2k | zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL; |
9637 | 67.2k | zend_class_entry *ce = CG(active_class_entry); |
9638 | 67.2k | uint32_t i; |
9639 | | |
9640 | 67.2k | ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children)); |
9641 | | |
9642 | 135k | for (i = 0; i < traits->children; ++i) { |
9643 | 68.6k | zend_ast *trait_ast = traits->child[i]; |
9644 | | |
9645 | 68.6k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9646 | 5 | zend_string *name = zend_ast_get_str(trait_ast); |
9647 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. " |
9648 | 5 | "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name)); |
9649 | 5 | } |
9650 | | |
9651 | 68.6k | ce->trait_names[ce->num_traits].name = |
9652 | 68.6k | zend_resolve_const_class_name_reference(trait_ast, "trait name"); |
9653 | 68.6k | ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name); |
9654 | 68.6k | ce->num_traits++; |
9655 | 68.6k | } |
9656 | | |
9657 | 67.2k | if (!adaptations) { |
9658 | 2.26k | return; |
9659 | 2.26k | } |
9660 | | |
9661 | 261k | for (i = 0; i < adaptations->children; ++i) { |
9662 | 196k | const zend_ast *adaptation_ast = adaptations->child[i]; |
9663 | 196k | switch (adaptation_ast->kind) { |
9664 | 1.14k | case ZEND_AST_TRAIT_PRECEDENCE: |
9665 | 1.14k | zend_compile_trait_precedence(adaptation_ast); |
9666 | 1.14k | break; |
9667 | 195k | case ZEND_AST_TRAIT_ALIAS: |
9668 | 195k | zend_compile_trait_alias(adaptation_ast); |
9669 | 195k | break; |
9670 | 0 | default: ZEND_UNREACHABLE(); |
9671 | 196k | } |
9672 | 196k | } |
9673 | 64.9k | } |
9674 | | /* }}} */ |
9675 | | |
9676 | | static void zend_compile_implements(zend_ast *ast) /* {{{ */ |
9677 | 150k | { |
9678 | 150k | const zend_ast_list *list = zend_ast_get_list(ast); |
9679 | 150k | zend_class_entry *ce = CG(active_class_entry); |
9680 | 150k | zend_class_name *interface_names; |
9681 | 150k | uint32_t i; |
9682 | | |
9683 | 150k | interface_names = emalloc(sizeof(zend_class_name) * list->children); |
9684 | | |
9685 | 428k | for (i = 0; i < list->children; ++i) { |
9686 | 278k | zend_ast *class_ast = list->child[i]; |
9687 | 278k | interface_names[i].name = |
9688 | 278k | zend_resolve_const_class_name_reference(class_ast, "interface name"); |
9689 | 278k | interface_names[i].lc_name = zend_string_tolower(interface_names[i].name); |
9690 | 278k | } |
9691 | | |
9692 | 150k | ce->num_interfaces = list->children; |
9693 | 150k | ce->interface_names = interface_names; |
9694 | 150k | } |
9695 | | /* }}} */ |
9696 | | |
9697 | | static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl) |
9698 | 143k | { |
9699 | 143k | zend_string *filename = CG(active_op_array)->filename; |
9700 | 143k | uint32_t start_lineno = decl->start_lineno; |
9701 | | |
9702 | | /* Use parent or first interface as prefix. */ |
9703 | 143k | zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS); |
9704 | 143k | if (decl->child[0]) { |
9705 | 90 | prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name"); |
9706 | 143k | } else if (decl->child[1]) { |
9707 | 142k | const zend_ast_list *list = zend_ast_get_list(decl->child[1]); |
9708 | 142k | prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name"); |
9709 | 142k | } |
9710 | | |
9711 | 143k | zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32, |
9712 | 143k | ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
9713 | 143k | zend_string_release(prefix); |
9714 | 143k | return zend_new_interned_string(result); |
9715 | 143k | } |
9716 | | |
9717 | | static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast) |
9718 | 783 | { |
9719 | 783 | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM); |
9720 | 783 | zend_type type = zend_compile_typename(enum_backing_type_ast); |
9721 | 783 | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9722 | 783 | if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) { |
9723 | 54 | zend_string *type_string = zend_type_to_string(type); |
9724 | 54 | zend_error_noreturn(E_COMPILE_ERROR, |
9725 | 54 | "Enum backing type must be int or string, %s given", |
9726 | 54 | ZSTR_VAL(type_string)); |
9727 | 54 | } |
9728 | 729 | if (type_mask == MAY_BE_LONG) { |
9729 | 388 | ce->enum_backing_type = IS_LONG; |
9730 | 388 | } else { |
9731 | 341 | ZEND_ASSERT(type_mask == MAY_BE_STRING); |
9732 | 341 | ce->enum_backing_type = IS_STRING; |
9733 | 340 | } |
9734 | 729 | zend_type_release(type, 0); |
9735 | 728 | } |
9736 | | |
9737 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */ |
9738 | 443k | { |
9739 | 443k | const zend_ast_decl *decl = (const zend_ast_decl *) ast; |
9740 | 443k | zend_ast *extends_ast = decl->child[0]; |
9741 | 443k | zend_ast *implements_ast = decl->child[1]; |
9742 | 443k | zend_ast *stmt_ast = decl->child[2]; |
9743 | 443k | zend_ast *enum_backing_type_ast = decl->child[4]; |
9744 | 443k | zend_string *name, *lcname; |
9745 | 443k | zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry)); |
9746 | 443k | zend_op *opline; |
9747 | | |
9748 | 443k | zend_class_entry *original_ce = CG(active_class_entry); |
9749 | | |
9750 | 443k | if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) { |
9751 | 299k | zend_string *unqualified_name = decl->name; |
9752 | | |
9753 | 299k | if (CG(active_class_entry)) { |
9754 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested"); |
9755 | 9 | } |
9756 | | |
9757 | 299k | const char *type = "a class name"; |
9758 | 299k | if (decl->flags & ZEND_ACC_ENUM) { |
9759 | 12.4k | type = "an enum name"; |
9760 | 287k | } else if (decl->flags & ZEND_ACC_INTERFACE) { |
9761 | 5.07k | type = "an interface name"; |
9762 | 281k | } else if (decl->flags & ZEND_ACC_TRAIT) { |
9763 | 2.90k | type = "a trait name"; |
9764 | 2.90k | } |
9765 | 299k | zend_assert_valid_class_name(unqualified_name, type); |
9766 | 299k | name = zend_prefix_with_ns(unqualified_name); |
9767 | 299k | name = zend_new_interned_string(name); |
9768 | 299k | lcname = zend_string_tolower(name); |
9769 | | |
9770 | 299k | if (FC(imports)) { |
9771 | 1.09k | zend_string *import_name = |
9772 | 1.09k | zend_hash_find_ptr_lc(FC(imports), unqualified_name); |
9773 | 1.09k | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
9774 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s " |
9775 | 12 | "(previously declared as local import)", ZSTR_VAL(name)); |
9776 | 12 | } |
9777 | 1.09k | } |
9778 | | |
9779 | 299k | zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS); |
9780 | 299k | } else { |
9781 | | /* Find an anon class name that is not in use yet. */ |
9782 | 143k | name = NULL; |
9783 | 143k | lcname = NULL; |
9784 | 143k | do { |
9785 | 143k | zend_tmp_string_release(name); |
9786 | 143k | zend_tmp_string_release(lcname); |
9787 | 143k | name = zend_generate_anon_class_name(decl); |
9788 | 143k | lcname = zend_string_tolower(name); |
9789 | 143k | } while (zend_hash_exists(CG(class_table), lcname)); |
9790 | 143k | } |
9791 | 443k | lcname = zend_new_interned_string(lcname); |
9792 | | |
9793 | 443k | ce->type = ZEND_USER_CLASS; |
9794 | 443k | ce->name = name; |
9795 | 443k | zend_initialize_class_data(ce, true); |
9796 | 443k | if (!(decl->flags & ZEND_ACC_ANON_CLASS)) { |
9797 | 299k | zend_alloc_ce_cache(ce->name); |
9798 | 299k | } |
9799 | | |
9800 | 443k | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
9801 | 0 | ce->ce_flags |= ZEND_ACC_PRELOADED; |
9802 | 0 | ZEND_MAP_PTR_NEW(ce->static_members_table); |
9803 | 0 | ZEND_MAP_PTR_NEW(ce->mutable_data); |
9804 | 0 | } |
9805 | | |
9806 | 443k | ce->ce_flags |= decl->flags; |
9807 | 443k | ce->info.user.filename = zend_string_copy(zend_get_compiled_filename()); |
9808 | 443k | ce->info.user.line_start = decl->start_lineno; |
9809 | 443k | ce->info.user.line_end = decl->end_lineno; |
9810 | | |
9811 | 443k | if (decl->doc_comment) { |
9812 | 102 | ce->doc_comment = zend_string_copy(decl->doc_comment); |
9813 | 102 | } |
9814 | | |
9815 | 443k | if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) { |
9816 | | /* Serialization is not supported for anonymous classes */ |
9817 | 143k | ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; |
9818 | 143k | } |
9819 | | |
9820 | 443k | if (extends_ast) { |
9821 | 71.5k | ce->parent_name = |
9822 | 71.5k | zend_resolve_const_class_name_reference(extends_ast, "class name"); |
9823 | 71.5k | } |
9824 | | |
9825 | 443k | CG(active_class_entry) = ce; |
9826 | | |
9827 | 443k | if (decl->child[3]) { |
9828 | 2.91k | zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0); |
9829 | 2.91k | } |
9830 | | |
9831 | 443k | if (implements_ast) { |
9832 | 150k | zend_compile_implements(implements_ast); |
9833 | 150k | } |
9834 | | |
9835 | 443k | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9836 | 12.4k | if (enum_backing_type_ast != NULL) { |
9837 | 783 | zend_compile_enum_backing_type(ce, enum_backing_type_ast); |
9838 | 783 | } |
9839 | 12.4k | zend_enum_add_interfaces(ce); |
9840 | 12.4k | zend_enum_register_props(ce); |
9841 | 12.4k | } |
9842 | | |
9843 | 443k | zend_compile_stmt(stmt_ast); |
9844 | | |
9845 | | /* Reset lineno for final opcodes and errors */ |
9846 | 443k | CG(zend_lineno) = ast->lineno; |
9847 | | |
9848 | 443k | 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) { |
9849 | 58 | zend_verify_abstract_class(ce); |
9850 | 58 | } |
9851 | | |
9852 | 443k | CG(active_class_entry) = original_ce; |
9853 | | |
9854 | 443k | if (toplevel) { |
9855 | 45.3k | ce->ce_flags |= ZEND_ACC_TOP_LEVEL; |
9856 | 45.3k | } |
9857 | | |
9858 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
9859 | 443k | if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
9860 | | #ifdef ZEND_OPCACHE_SHM_REATTACHMENT |
9861 | | /* See zend_link_hooked_object_iter(). */ |
9862 | | && !ce->num_hooked_props |
9863 | | #endif |
9864 | 211k | && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) { |
9865 | 211k | if (toplevel) { |
9866 | 36.6k | if (extends_ast) { |
9867 | 9.39k | zend_class_entry *parent_ce = zend_lookup_class_ex( |
9868 | 9.39k | ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); |
9869 | | |
9870 | 9.39k | if (parent_ce |
9871 | 8.57k | && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) { |
9872 | 8.57k | if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) { |
9873 | 5.11k | zend_string_release(lcname); |
9874 | 5.11k | return; |
9875 | 5.11k | } |
9876 | 8.57k | } |
9877 | 27.2k | } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) { |
9878 | 22.9k | zend_string_release(lcname); |
9879 | 22.9k | zend_build_properties_info_table(ce); |
9880 | 22.9k | zend_inheritance_check_override(ce); |
9881 | 22.9k | ce->ce_flags |= ZEND_ACC_LINKED; |
9882 | 22.9k | zend_observer_class_linked_notify(ce, lcname); |
9883 | 22.9k | return; |
9884 | 22.9k | } else { |
9885 | 4.27k | goto link_unbound; |
9886 | 4.27k | } |
9887 | 174k | } else if (!extends_ast) { |
9888 | 117k | link_unbound: |
9889 | | /* Link unbound simple class */ |
9890 | 117k | zend_build_properties_info_table(ce); |
9891 | 117k | zend_inheritance_check_override(ce); |
9892 | 117k | ce->ce_flags |= ZEND_ACC_LINKED; |
9893 | 117k | } |
9894 | 211k | } |
9895 | | |
9896 | 415k | opline = get_next_op(); |
9897 | | |
9898 | 415k | if (ce->parent_name) { |
9899 | | /* Lowercased parent name */ |
9900 | 65.3k | zend_string *lc_parent_name = zend_string_tolower(ce->parent_name); |
9901 | 65.3k | opline->op2_type = IS_CONST; |
9902 | 65.3k | LITERAL_STR(opline->op2, lc_parent_name); |
9903 | 65.3k | } |
9904 | | |
9905 | 415k | opline->op1_type = IS_CONST; |
9906 | | /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table. |
9907 | | * However, by this point another thread may have caused `lcname` to be added in the interned string table. |
9908 | | * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use |
9909 | | * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the |
9910 | | * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using |
9911 | | * zend_add_literal_string() which gives us the new value. */ |
9912 | 415k | opline->op1.constant = zend_add_literal_string(&lcname); |
9913 | | |
9914 | 415k | if (decl->flags & ZEND_ACC_ANON_CLASS) { |
9915 | 143k | opline->opcode = ZEND_DECLARE_ANON_CLASS; |
9916 | 143k | opline->extended_value = zend_alloc_cache_slot(); |
9917 | 143k | zend_make_var_result(result, opline); |
9918 | 143k | if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) { |
9919 | | /* We checked above that the class name is not used. This really shouldn't happen. */ |
9920 | 0 | zend_error_noreturn(E_ERROR, |
9921 | 0 | "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name)); |
9922 | 0 | } |
9923 | 271k | } else { |
9924 | | /* Generate RTD keys until we find one that isn't in use yet. */ |
9925 | 271k | zend_string *key = NULL; |
9926 | 271k | do { |
9927 | 271k | zend_tmp_string_release(key); |
9928 | 271k | key = zend_build_runtime_definition_key(lcname, decl->start_lineno); |
9929 | 271k | } while (!zend_hash_add_ptr(CG(class_table), key, ce)); |
9930 | | |
9931 | | /* RTD key is placed after lcname literal in op1 */ |
9932 | 271k | zend_add_literal_string(&key); |
9933 | | |
9934 | 271k | opline->opcode = ZEND_DECLARE_CLASS; |
9935 | 271k | if (toplevel |
9936 | 16.3k | && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) |
9937 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
9938 | 6.24k | && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
9939 | 271k | ) { |
9940 | 1.56k | if (!extends_ast) { |
9941 | | /* Use empty string for classes without parents to avoid new handler, and special |
9942 | | * handling of zend_early_binding. */ |
9943 | 910 | opline->op2_type = IS_CONST; |
9944 | 910 | LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC()); |
9945 | 910 | } |
9946 | 1.56k | CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING; |
9947 | 1.56k | opline->opcode = ZEND_DECLARE_CLASS_DELAYED; |
9948 | 1.56k | opline->extended_value = zend_alloc_cache_slot(); |
9949 | 1.56k | opline->result_type = IS_UNUSED; |
9950 | 1.56k | opline->result.opline_num = -1; |
9951 | 1.56k | } |
9952 | 271k | } |
9953 | 415k | } |
9954 | | /* }}} */ |
9955 | | |
9956 | | static void zend_compile_enum_case(zend_ast *ast) |
9957 | 22.9k | { |
9958 | 22.9k | zend_class_entry *enum_class = CG(active_class_entry); |
9959 | 22.9k | if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) { |
9960 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums"); |
9961 | 5 | } |
9962 | | |
9963 | 22.9k | zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0])); |
9964 | 22.9k | zend_string *enum_class_name = enum_class->name; |
9965 | | |
9966 | 22.9k | zval class_name_zval; |
9967 | 22.9k | ZVAL_STR_COPY(&class_name_zval, enum_class_name); |
9968 | 22.9k | zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval); |
9969 | | |
9970 | 22.9k | zval case_id_zval; |
9971 | 22.9k | int case_id = zend_enum_next_case_id(enum_class); |
9972 | 22.9k | ZVAL_LONG(&case_id_zval, case_id); |
9973 | 22.9k | zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval); |
9974 | | |
9975 | 22.9k | zval case_name_zval; |
9976 | 22.9k | ZVAL_STR_COPY(&case_name_zval, enum_case_name); |
9977 | 22.9k | zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval); |
9978 | | |
9979 | 22.9k | zend_ast *case_value_ast = ast->child[1]; |
9980 | | // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval |
9981 | 22.9k | ast->child[1] = NULL; |
9982 | 22.9k | if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) { |
9983 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value", |
9984 | 8 | ZSTR_VAL(enum_case_name), |
9985 | 8 | ZSTR_VAL(enum_class_name)); |
9986 | 22.8k | } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) { |
9987 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value", |
9988 | 11 | ZSTR_VAL(enum_case_name), |
9989 | 11 | ZSTR_VAL(enum_class_name)); |
9990 | 11 | } |
9991 | | |
9992 | 22.8k | zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, |
9993 | 22.8k | class_name_ast, case_id_ast, case_name_ast, case_value_ast); |
9994 | | |
9995 | 22.8k | zval value_zv; |
9996 | 22.8k | zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false); |
9997 | | |
9998 | | /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */ |
9999 | 22.8k | zend_ast *doc_comment_ast = ast->child[2]; |
10000 | 22.8k | zend_string *doc_comment = NULL; |
10001 | 22.8k | if (doc_comment_ast) { |
10002 | 16.3k | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
10003 | 16.3k | } |
10004 | | |
10005 | 22.8k | zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment); |
10006 | 22.8k | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE; |
10007 | 22.8k | zend_ast_destroy(const_enum_init_ast); |
10008 | | |
10009 | 22.8k | zend_ast *attr_ast = ast->child[3]; |
10010 | 22.8k | if (attr_ast) { |
10011 | 178 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
10012 | | |
10013 | 178 | zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
10014 | | |
10015 | 178 | if (deprecated) { |
10016 | 53 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
10017 | 53 | } |
10018 | | |
10019 | 178 | const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1); |
10020 | 178 | if (override) { |
10021 | 14 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE; |
10022 | | /* We need to be able to remove the flag once the override is |
10023 | | * resolved. See ZEND_ACC_DEPRECATED handling in |
10024 | | * zend_compile_class_const_decl(). */ |
10025 | 14 | enum_class->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
10026 | 14 | enum_class->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
10027 | 14 | } |
10028 | 178 | } |
10029 | 22.8k | } |
10030 | | |
10031 | | static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */ |
10032 | 1.79k | { |
10033 | 1.79k | switch (type) { |
10034 | 1.04k | case ZEND_SYMBOL_CLASS: |
10035 | 1.04k | if (!FC(imports)) { |
10036 | 727 | FC(imports) = emalloc(sizeof(HashTable)); |
10037 | 727 | zend_hash_init(FC(imports), 8, NULL, str_dtor, 0); |
10038 | 727 | } |
10039 | 1.04k | return FC(imports); |
10040 | 402 | case ZEND_SYMBOL_FUNCTION: |
10041 | 402 | if (!FC(imports_function)) { |
10042 | 290 | FC(imports_function) = emalloc(sizeof(HashTable)); |
10043 | 290 | zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0); |
10044 | 290 | } |
10045 | 402 | return FC(imports_function); |
10046 | 349 | case ZEND_SYMBOL_CONST: |
10047 | 349 | if (!FC(imports_const)) { |
10048 | 280 | FC(imports_const) = emalloc(sizeof(HashTable)); |
10049 | 280 | zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0); |
10050 | 280 | } |
10051 | 349 | return FC(imports_const); |
10052 | 0 | default: ZEND_UNREACHABLE(); |
10053 | 1.79k | } |
10054 | | |
10055 | 0 | return NULL; |
10056 | 1.79k | } |
10057 | | /* }}} */ |
10058 | | |
10059 | | static char *zend_get_use_type_str(uint32_t type) /* {{{ */ |
10060 | 69 | { |
10061 | 69 | switch (type) { |
10062 | 37 | case ZEND_SYMBOL_CLASS: |
10063 | 37 | return ""; |
10064 | 18 | case ZEND_SYMBOL_FUNCTION: |
10065 | 18 | return " function"; |
10066 | 14 | case ZEND_SYMBOL_CONST: |
10067 | 14 | return " const"; |
10068 | 0 | default: ZEND_UNREACHABLE(); |
10069 | 69 | } |
10070 | | |
10071 | 0 | return " unknown"; |
10072 | 69 | } |
10073 | | /* }}} */ |
10074 | | |
10075 | | 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) /* {{{ */ |
10076 | 43 | { |
10077 | 43 | if (zend_string_equals_ci(old_name, check_name)) { |
10078 | 19 | return; |
10079 | 19 | } |
10080 | | |
10081 | 24 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
10082 | 24 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
10083 | 43 | } |
10084 | | /* }}} */ |
10085 | | |
10086 | | static void zend_compile_use(zend_ast *ast) /* {{{ */ |
10087 | 1.79k | { |
10088 | 1.79k | const zend_ast_list *list = zend_ast_get_list(ast); |
10089 | 1.79k | uint32_t i; |
10090 | 1.79k | zend_string *current_ns = FC(current_namespace); |
10091 | 1.79k | uint32_t type = ast->attr; |
10092 | 1.79k | HashTable *current_import = zend_get_import_ht(type); |
10093 | 1.79k | bool case_sensitive = type == ZEND_SYMBOL_CONST; |
10094 | | |
10095 | 3.79k | for (i = 0; i < list->children; ++i) { |
10096 | 2.06k | const zend_ast *use_ast = list->child[i]; |
10097 | 2.06k | zend_ast *old_name_ast = use_ast->child[0]; |
10098 | 2.06k | zend_ast *new_name_ast = use_ast->child[1]; |
10099 | 2.06k | zend_string *old_name = zend_ast_get_str(old_name_ast); |
10100 | 2.06k | zend_string *new_name, *lookup_name; |
10101 | | |
10102 | 2.06k | if (new_name_ast) { |
10103 | 481 | new_name = zend_string_copy(zend_ast_get_str(new_name_ast)); |
10104 | 1.58k | } else { |
10105 | 1.58k | const char *unqualified_name; |
10106 | 1.58k | size_t unqualified_name_len; |
10107 | 1.58k | if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) { |
10108 | | /* The form "use A\B" is equivalent to "use A\B as B" */ |
10109 | 774 | new_name = zend_string_init(unqualified_name, unqualified_name_len, 0); |
10110 | 813 | } else { |
10111 | 813 | new_name = zend_string_copy(old_name); |
10112 | | |
10113 | 813 | if (!current_ns) { |
10114 | 495 | zend_error(E_WARNING, "The use statement with non-compound name '%s' " |
10115 | 495 | "has no effect", ZSTR_VAL(new_name)); |
10116 | 495 | } |
10117 | 813 | } |
10118 | 1.58k | } |
10119 | | |
10120 | 2.06k | if (case_sensitive) { |
10121 | 431 | lookup_name = zend_string_copy(new_name); |
10122 | 1.63k | } else { |
10123 | 1.63k | lookup_name = zend_string_tolower(new_name); |
10124 | 1.63k | } |
10125 | | |
10126 | 2.06k | if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) { |
10127 | 22 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' " |
10128 | 22 | "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name)); |
10129 | 22 | } |
10130 | | |
10131 | 2.04k | if (current_ns) { |
10132 | 1.05k | zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0); |
10133 | 1.05k | zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns)); |
10134 | 1.05k | ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\'; |
10135 | 1.05k | memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1); |
10136 | | |
10137 | 1.05k | if (zend_have_seen_symbol(ns_name, type)) { |
10138 | 15 | zend_check_already_in_use(type, old_name, new_name, ns_name); |
10139 | 15 | } |
10140 | | |
10141 | 1.05k | zend_string_efree(ns_name); |
10142 | 1.05k | } else if (zend_have_seen_symbol(lookup_name, type)) { |
10143 | 28 | zend_check_already_in_use(type, old_name, new_name, lookup_name); |
10144 | 28 | } |
10145 | | |
10146 | 2.04k | zend_string_addref(old_name); |
10147 | 2.04k | old_name = zend_new_interned_string(old_name); |
10148 | 2.04k | if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) { |
10149 | 45 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
10150 | 45 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
10151 | 45 | } |
10152 | | |
10153 | 2.00k | zend_string_release_ex(lookup_name, 0); |
10154 | 2.00k | zend_string_release_ex(new_name, 0); |
10155 | 2.00k | } |
10156 | 1.79k | } |
10157 | | /* }}} */ |
10158 | | |
10159 | | static void zend_compile_group_use(const zend_ast *ast) /* {{{ */ |
10160 | 211 | { |
10161 | 211 | uint32_t i; |
10162 | 211 | const zend_string *ns = zend_ast_get_str(ast->child[0]); |
10163 | 211 | const zend_ast_list *list = zend_ast_get_list(ast->child[1]); |
10164 | | |
10165 | 813 | for (i = 0; i < list->children; i++) { |
10166 | 602 | zend_ast *inline_use, *use = list->child[i]; |
10167 | 602 | zval *name_zval = zend_ast_get_zval(use->child[0]); |
10168 | 602 | zend_string *name = Z_STR_P(name_zval); |
10169 | 602 | zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
10170 | 602 | zend_string_release_ex(name, 0); |
10171 | 602 | ZVAL_STR(name_zval, compound_ns); |
10172 | 602 | inline_use = zend_ast_create_list(1, ZEND_AST_USE, use); |
10173 | 602 | inline_use->attr = ast->attr ? ast->attr : use->attr; |
10174 | 602 | zend_compile_use(inline_use); |
10175 | 602 | } |
10176 | 211 | } |
10177 | | /* }}} */ |
10178 | | |
10179 | | static void zend_compile_const_decl(zend_ast *ast) /* {{{ */ |
10180 | 4.80k | { |
10181 | 4.80k | zend_ast_list *list = zend_ast_get_list(ast); |
10182 | 4.80k | uint32_t i; |
10183 | 4.80k | zend_ast *attributes_ast = NULL; |
10184 | 4.80k | zend_op *last_op = NULL; |
10185 | 10.8k | for (i = 0; i < list->children; ++i) { |
10186 | 6.06k | zend_ast *const_ast = list->child[i]; |
10187 | 6.06k | if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) { |
10188 | 778 | ZEND_ASSERT(i == list->children - 1); |
10189 | 778 | attributes_ast = const_ast; |
10190 | 778 | continue; |
10191 | 778 | } |
10192 | 5.28k | ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM); |
10193 | 5.28k | zend_ast *name_ast = const_ast->child[0]; |
10194 | 5.28k | zend_ast **value_ast_ptr = &const_ast->child[1]; |
10195 | 5.28k | zend_string *unqualified_name = zend_ast_get_str(name_ast); |
10196 | | |
10197 | 5.28k | zend_string *name; |
10198 | 5.28k | znode name_node, value_node; |
10199 | 5.28k | zval *value_zv = &value_node.u.constant; |
10200 | | |
10201 | 5.28k | value_node.op_type = IS_CONST; |
10202 | 5.28k | zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true); |
10203 | | |
10204 | 5.28k | if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) { |
10205 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
10206 | 6 | "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name)); |
10207 | 6 | } |
10208 | | |
10209 | 5.27k | name = zend_prefix_with_ns(unqualified_name); |
10210 | 5.27k | name = zend_new_interned_string(name); |
10211 | | |
10212 | 5.27k | if (FC(imports_const)) { |
10213 | 622 | zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name); |
10214 | 622 | if (import_name && !zend_string_equals(import_name, name)) { |
10215 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because " |
10216 | 6 | "the name is already in use", ZSTR_VAL(name)); |
10217 | 6 | } |
10218 | 622 | } |
10219 | | |
10220 | 5.27k | name_node.op_type = IS_CONST; |
10221 | 5.27k | ZVAL_STR(&name_node.u.constant, name); |
10222 | | |
10223 | 5.27k | last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node); |
10224 | | |
10225 | 5.27k | zend_register_seen_symbol(name, ZEND_SYMBOL_CONST); |
10226 | 5.27k | } |
10227 | 4.79k | if (attributes_ast == NULL) { |
10228 | 3.90k | return; |
10229 | 3.90k | } |
10230 | | /* Validate: attributes can only be applied to one constant at a time |
10231 | | * Since we store the AST for the attributes in the list of children, |
10232 | | * there should be exactly 2 children. */ |
10233 | 889 | if (list->children > 2) { |
10234 | 5 | zend_error_noreturn( |
10235 | 5 | E_COMPILE_ERROR, |
10236 | 5 | "Cannot apply attributes to multiple constants at once" |
10237 | 5 | ); |
10238 | 5 | } |
10239 | | |
10240 | 884 | HashTable *attributes = NULL; |
10241 | 884 | zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0); |
10242 | | |
10243 | 884 | ZEND_ASSERT(last_op != NULL); |
10244 | 884 | last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST; |
10245 | 762 | znode attribs_node; |
10246 | 762 | attribs_node.op_type = IS_CONST; |
10247 | 762 | ZVAL_PTR(&attribs_node.u.constant, attributes); |
10248 | 762 | zend_emit_op_data(&attribs_node); |
10249 | 762 | CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS; |
10250 | 762 | } |
10251 | | /* }}}*/ |
10252 | | |
10253 | | static void zend_compile_namespace(const zend_ast *ast) /* {{{ */ |
10254 | 4.30k | { |
10255 | 4.30k | zend_ast *name_ast = ast->child[0]; |
10256 | 4.30k | zend_ast *stmt_ast = ast->child[1]; |
10257 | 4.30k | zend_string *name; |
10258 | 4.30k | bool with_bracket = stmt_ast != NULL; |
10259 | | |
10260 | | /* handle mixed syntax declaration or nested namespaces */ |
10261 | 4.30k | if (!FC(has_bracketed_namespaces)) { |
10262 | 3.22k | if (FC(current_namespace)) { |
10263 | | /* previous namespace declarations were unbracketed */ |
10264 | 515 | if (with_bracket) { |
10265 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10266 | 6 | "with unbracketed namespace declarations"); |
10267 | 6 | } |
10268 | 515 | } |
10269 | 3.22k | } else { |
10270 | | /* previous namespace declarations were bracketed */ |
10271 | 1.07k | if (!with_bracket) { |
10272 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10273 | 9 | "with unbracketed namespace declarations"); |
10274 | 1.06k | } else if (FC(current_namespace) || FC(in_namespace)) { |
10275 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested"); |
10276 | 6 | } |
10277 | 1.07k | } |
10278 | | |
10279 | 4.30k | bool is_first_namespace = (!with_bracket && !FC(current_namespace)) |
10280 | 2.09k | || (with_bracket && !FC(has_bracketed_namespaces)); |
10281 | 4.28k | if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
10282 | 29 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be " |
10283 | 29 | "the very first statement or after any declare call in the script"); |
10284 | 29 | } |
10285 | | |
10286 | 4.25k | if (FC(current_namespace)) { |
10287 | 509 | zend_string_release_ex(FC(current_namespace), 0); |
10288 | 509 | } |
10289 | | |
10290 | 4.25k | if (name_ast) { |
10291 | 3.60k | name = zend_ast_get_str(name_ast); |
10292 | | |
10293 | 3.60k | if (zend_string_equals_literal_ci(name, "namespace")) { |
10294 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name)); |
10295 | 5 | } |
10296 | | |
10297 | 3.59k | FC(current_namespace) = zend_string_copy(name); |
10298 | 3.59k | } else { |
10299 | 653 | FC(current_namespace) = NULL; |
10300 | 653 | } |
10301 | | |
10302 | 4.24k | zend_reset_import_tables(); |
10303 | | |
10304 | 4.24k | FC(in_namespace) = 1; |
10305 | 4.24k | if (with_bracket) { |
10306 | 1.58k | FC(has_bracketed_namespaces) = 1; |
10307 | 1.58k | } |
10308 | | |
10309 | 4.24k | if (stmt_ast) { |
10310 | 1.58k | zend_compile_top_stmt(stmt_ast); |
10311 | 1.58k | zend_end_namespace(); |
10312 | 1.58k | } |
10313 | 4.24k | } |
10314 | | /* }}} */ |
10315 | | |
10316 | | static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */ |
10317 | 110 | { |
10318 | 110 | zend_ast *offset_ast = ast->child[0]; |
10319 | 110 | zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast)); |
10320 | | |
10321 | 110 | const char const_name[] = "__COMPILER_HALT_OFFSET__"; |
10322 | | |
10323 | 110 | if (FC(has_bracketed_namespaces) && FC(in_namespace)) { |
10324 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
10325 | 0 | "__HALT_COMPILER() can only be used from the outermost scope"); |
10326 | 0 | } |
10327 | | |
10328 | 110 | const zend_string *filename = zend_get_compiled_filename(); |
10329 | 110 | zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1, |
10330 | 110 | ZSTR_VAL(filename), ZSTR_LEN(filename), false); |
10331 | | |
10332 | | /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in |
10333 | | * case this file was already included. */ |
10334 | 110 | if (!zend_hash_find(EG(zend_constants), name)) { |
10335 | 110 | zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0); |
10336 | 110 | } |
10337 | 110 | zend_string_release_ex(name, 0); |
10338 | 110 | } |
10339 | | /* }}} */ |
10340 | | |
10341 | | static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */ |
10342 | 18.5k | { |
10343 | 18.5k | const zend_op_array *op_array = CG(active_op_array); |
10344 | 18.5k | const zend_class_entry *ce = CG(active_class_entry); |
10345 | | |
10346 | 18.5k | switch (ast->attr) { |
10347 | 277 | case T_LINE: |
10348 | 277 | ZVAL_LONG(zv, ast->lineno); |
10349 | 277 | break; |
10350 | 4.86k | case T_FILE: |
10351 | 4.86k | ZVAL_STR_COPY(zv, CG(compiled_filename)); |
10352 | 4.86k | break; |
10353 | 1.09k | case T_DIR: |
10354 | 1.09k | { |
10355 | 1.09k | const zend_string *filename = CG(compiled_filename); |
10356 | 1.09k | zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0); |
10357 | | #ifdef ZEND_WIN32 |
10358 | | ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10359 | | #else |
10360 | 1.09k | ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10361 | 1.09k | #endif |
10362 | | |
10363 | 1.09k | if (zend_string_equals_literal(dirname, ".")) { |
10364 | 595 | dirname = zend_string_extend(dirname, MAXPATHLEN, 0); |
10365 | 595 | #ifdef HAVE_GETCWD |
10366 | 595 | ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN)); |
10367 | | #elif defined(HAVE_GETWD) |
10368 | | ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname))); |
10369 | | #endif |
10370 | 595 | ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname)); |
10371 | 595 | } |
10372 | | |
10373 | 1.09k | ZVAL_STR(zv, dirname); |
10374 | 1.09k | break; |
10375 | 0 | } |
10376 | 1.97k | case T_FUNC_C: |
10377 | 1.97k | if (op_array && op_array->function_name) { |
10378 | 1.82k | ZVAL_STR_COPY(zv, op_array->function_name); |
10379 | 1.82k | } else { |
10380 | 153 | ZVAL_EMPTY_STRING(zv); |
10381 | 153 | } |
10382 | 1.97k | break; |
10383 | 579 | case T_PROPERTY_C: { |
10384 | 579 | zend_string *prop_info_name = CG(context).active_property_info_name; |
10385 | 579 | if (prop_info_name) { |
10386 | 404 | ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name)); |
10387 | 404 | } else { |
10388 | 175 | ZVAL_EMPTY_STRING(zv); |
10389 | 175 | } |
10390 | 579 | break; |
10391 | 0 | } |
10392 | 4.57k | case T_METHOD_C: |
10393 | | /* Detect whether we are directly inside a class (e.g. a class constant) and treat |
10394 | | * this as not being inside a function. */ |
10395 | 4.57k | if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
10396 | 337 | op_array = NULL; |
10397 | 337 | } |
10398 | 4.57k | if (op_array && op_array->function_name) { |
10399 | 3.92k | if (op_array->scope) { |
10400 | 3.49k | ZVAL_NEW_STR(zv, |
10401 | 3.49k | zend_create_member_string(op_array->scope->name, op_array->function_name)); |
10402 | 3.49k | } else { |
10403 | 430 | ZVAL_STR_COPY(zv, op_array->function_name); |
10404 | 430 | } |
10405 | 3.92k | } else { |
10406 | 652 | ZVAL_EMPTY_STRING(zv); |
10407 | 652 | } |
10408 | 4.57k | break; |
10409 | 3.07k | case T_CLASS_C: |
10410 | 3.07k | if (ce) { |
10411 | 2.40k | if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10412 | 1.76k | return 0; |
10413 | 1.76k | } else { |
10414 | 636 | ZVAL_STR_COPY(zv, ce->name); |
10415 | 636 | } |
10416 | 2.40k | } else { |
10417 | 668 | ZVAL_EMPTY_STRING(zv); |
10418 | 668 | } |
10419 | 1.30k | break; |
10420 | 1.30k | case T_TRAIT_C: |
10421 | 1.29k | if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10422 | 365 | ZVAL_STR_COPY(zv, ce->name); |
10423 | 930 | } else { |
10424 | 930 | ZVAL_EMPTY_STRING(zv); |
10425 | 930 | } |
10426 | 1.29k | break; |
10427 | 828 | case T_NS_C: |
10428 | 828 | if (FC(current_namespace)) { |
10429 | 587 | ZVAL_STR_COPY(zv, FC(current_namespace)); |
10430 | 587 | } else { |
10431 | 241 | ZVAL_EMPTY_STRING(zv); |
10432 | 241 | } |
10433 | 828 | break; |
10434 | 0 | default: ZEND_UNREACHABLE(); |
10435 | 18.5k | } |
10436 | | |
10437 | 16.8k | return true; |
10438 | 18.5k | } |
10439 | | /* }}} */ |
10440 | | |
10441 | | ZEND_API bool zend_is_op_long_compatible(const zval *op) |
10442 | 167k | { |
10443 | 167k | if (Z_TYPE_P(op) == IS_ARRAY) { |
10444 | 674 | return false; |
10445 | 674 | } |
10446 | | |
10447 | 166k | if (Z_TYPE_P(op) == IS_DOUBLE |
10448 | 61.7k | && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) { |
10449 | 55.3k | return false; |
10450 | 55.3k | } |
10451 | | |
10452 | 111k | if (Z_TYPE_P(op) == IS_STRING) { |
10453 | 15.9k | double dval = 0; |
10454 | 15.9k | uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval); |
10455 | 15.9k | if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) { |
10456 | 7.61k | return false; |
10457 | 7.61k | } |
10458 | 15.9k | } |
10459 | | |
10460 | 103k | return true; |
10461 | 111k | } |
10462 | | |
10463 | | ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */ |
10464 | 971k | { |
10465 | 971k | if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) { |
10466 | | /* Array to string warning. */ |
10467 | 36.0k | return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY; |
10468 | 36.0k | } |
10469 | | |
10470 | 935k | if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV |
10471 | 730k | || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR |
10472 | 654k | || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) { |
10473 | | /* Only the numeric operations throw errors. */ |
10474 | 576k | return 0; |
10475 | 576k | } |
10476 | | |
10477 | 359k | if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) { |
10478 | 104k | if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) { |
10479 | | /* Adding two arrays is allowed. */ |
10480 | 100k | return 0; |
10481 | 100k | } |
10482 | | |
10483 | | /* Numeric operators throw when one of the operands is an array. */ |
10484 | 3.91k | return 1; |
10485 | 104k | } |
10486 | | |
10487 | | /* While basic arithmetic operators always produce numeric string errors, |
10488 | | * bitwise operators don't produce errors if both operands are strings */ |
10489 | 254k | if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) |
10490 | 77.6k | && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { |
10491 | 15.6k | return 0; |
10492 | 15.6k | } |
10493 | | |
10494 | 239k | if (Z_TYPE_P(op1) == IS_STRING |
10495 | 59.6k | && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) { |
10496 | 35.9k | return 1; |
10497 | 35.9k | } |
10498 | | |
10499 | 203k | if (Z_TYPE_P(op2) == IS_STRING |
10500 | 47.9k | && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) { |
10501 | 38.2k | return 1; |
10502 | 38.2k | } |
10503 | | |
10504 | | /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */ |
10505 | 165k | if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR |
10506 | 154k | || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) { |
10507 | 67.7k | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) { |
10508 | 49.2k | return 1; |
10509 | 49.2k | } |
10510 | 67.7k | } |
10511 | | |
10512 | 115k | if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) { |
10513 | | /* Division by zero throws an error. */ |
10514 | 971 | return 1; |
10515 | 971 | } |
10516 | | |
10517 | | /* Mod is an operation that will cast float/float-strings to integers which might |
10518 | | produce float to int incompatible errors, and also cannot be divided by 0 */ |
10519 | 114k | if (opcode == ZEND_MOD) { |
10520 | 17.4k | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) { |
10521 | 14.1k | return 1; |
10522 | 14.1k | } |
10523 | 17.4k | } |
10524 | | |
10525 | 100k | if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) { |
10526 | | /* 0 ** (<0) throws a division by zero error. */ |
10527 | 189 | return 1; |
10528 | 189 | } |
10529 | 100k | if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) { |
10530 | | /* Shift by negative number throws an error. */ |
10531 | 228 | return 1; |
10532 | 228 | } |
10533 | | |
10534 | 100k | return 0; |
10535 | 100k | } |
10536 | | /* }}} */ |
10537 | | |
10538 | | static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */ |
10539 | 909k | { |
10540 | 909k | if (zend_binary_op_produces_error(opcode, op1, op2)) { |
10541 | 113k | return false; |
10542 | 113k | } |
10543 | | |
10544 | 795k | const binary_op_type fn = get_binary_op(opcode); |
10545 | 795k | fn(result, op1, op2); |
10546 | 795k | return true; |
10547 | 909k | } |
10548 | | /* }}} */ |
10549 | | |
10550 | | ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op) |
10551 | 277k | { |
10552 | 277k | if (opcode == ZEND_BW_NOT) { |
10553 | | /* BW_NOT on string does not convert the string into an integer. */ |
10554 | 8.27k | if (Z_TYPE_P(op) == IS_STRING) { |
10555 | 1.47k | return 0; |
10556 | 1.47k | } |
10557 | 6.79k | return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op); |
10558 | 8.27k | } |
10559 | | /* Can happen when called from zend_optimizer_eval_unary_op() */ |
10560 | 268k | if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) { |
10561 | | /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */ |
10562 | 268k | return Z_TYPE_P(op) == IS_DOUBLE; |
10563 | 268k | } |
10564 | | |
10565 | 0 | return 0; |
10566 | 268k | } |
10567 | | |
10568 | | static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */ |
10569 | 275k | { |
10570 | 275k | if (zend_unary_op_produces_error(opcode, op)) { |
10571 | 2.25k | return false; |
10572 | 2.25k | } |
10573 | | |
10574 | 273k | const unary_op_type fn = get_unary_op(opcode); |
10575 | 273k | fn(result, op); |
10576 | 273k | return true; |
10577 | 275k | } |
10578 | | /* }}} */ |
10579 | | |
10580 | | static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */ |
10581 | 49.7k | { |
10582 | 49.7k | zval right; |
10583 | 49.7k | ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10584 | 49.7k | return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right); |
10585 | 49.7k | } |
10586 | | /* }}} */ |
10587 | | |
10588 | | static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */ |
10589 | 19.1k | { |
10590 | 19.1k | const binary_op_type fn = kind == ZEND_AST_GREATER |
10591 | 19.1k | ? is_smaller_function : is_smaller_or_equal_function; |
10592 | 19.1k | fn(result, op2, op1); |
10593 | 19.1k | } |
10594 | | /* }}} */ |
10595 | | |
10596 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ |
10597 | 2.84M | { |
10598 | 2.84M | const zend_ast_list *list = zend_ast_get_list(ast); |
10599 | 2.84M | zend_ast *last_elem_ast = NULL; |
10600 | 2.84M | uint32_t i; |
10601 | 2.84M | bool is_constant = true; |
10602 | | |
10603 | 2.84M | if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) { |
10604 | 5 | zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression"); |
10605 | 5 | } |
10606 | | |
10607 | | /* First ensure that *all* child nodes are constant and by-val */ |
10608 | 6.06M | for (i = 0; i < list->children; ++i) { |
10609 | 3.21M | zend_ast *elem_ast = list->child[i]; |
10610 | | |
10611 | 3.21M | if (elem_ast == NULL) { |
10612 | | /* Report error at line of last non-empty element */ |
10613 | 107 | if (last_elem_ast) { |
10614 | 66 | CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast); |
10615 | 66 | } |
10616 | 107 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
10617 | 107 | } |
10618 | | |
10619 | 3.21M | if (elem_ast->kind != ZEND_AST_UNPACK) { |
10620 | 3.11M | zend_eval_const_expr(&elem_ast->child[0]); |
10621 | 3.11M | zend_eval_const_expr(&elem_ast->child[1]); |
10622 | | |
10623 | 3.11M | if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL |
10624 | 524k | || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL) |
10625 | 3.11M | ) { |
10626 | 2.59M | is_constant = false; |
10627 | 2.59M | } |
10628 | 3.11M | } else { |
10629 | 100k | zend_eval_const_expr(&elem_ast->child[0]); |
10630 | | |
10631 | 100k | if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) { |
10632 | 99.5k | is_constant = false; |
10633 | 99.5k | } |
10634 | 100k | } |
10635 | | |
10636 | 3.21M | last_elem_ast = elem_ast; |
10637 | 3.21M | } |
10638 | | |
10639 | 2.84M | if (!is_constant) { |
10640 | 2.62M | return false; |
10641 | 2.62M | } |
10642 | | |
10643 | 227k | if (!list->children) { |
10644 | 39.6k | ZVAL_EMPTY_ARRAY(result); |
10645 | 39.6k | return true; |
10646 | 39.6k | } |
10647 | | |
10648 | 187k | array_init_size(result, list->children); |
10649 | 649k | for (i = 0; i < list->children; ++i) { |
10650 | 464k | const zend_ast *elem_ast = list->child[i]; |
10651 | 464k | zend_ast *value_ast = elem_ast->child[0]; |
10652 | 464k | zend_ast *key_ast; |
10653 | | |
10654 | 464k | zval *value = zend_ast_get_zval(value_ast); |
10655 | 464k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
10656 | 1.03k | if (Z_TYPE_P(value) == IS_ARRAY) { |
10657 | 1.03k | const HashTable *ht = Z_ARRVAL_P(value); |
10658 | 1.03k | zval *val; |
10659 | 1.03k | zend_string *key; |
10660 | | |
10661 | 2.83k | ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) { |
10662 | 2.83k | if (key) { |
10663 | 118 | zend_hash_update(Z_ARRVAL_P(result), key, val); |
10664 | 427 | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) { |
10665 | 12 | zval_ptr_dtor(result); |
10666 | 12 | return 0; |
10667 | 12 | } |
10668 | 533 | Z_TRY_ADDREF_P(val); |
10669 | 533 | } ZEND_HASH_FOREACH_END(); |
10670 | | |
10671 | 1.01k | continue; |
10672 | 1.03k | } else { |
10673 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value)); |
10674 | 7 | } |
10675 | 1.03k | } |
10676 | | |
10677 | 463k | Z_TRY_ADDREF_P(value); |
10678 | | |
10679 | 463k | key_ast = elem_ast->child[1]; |
10680 | 463k | if (key_ast) { |
10681 | 129k | const zval *key = zend_ast_get_zval(key_ast); |
10682 | 129k | switch (Z_TYPE_P(key)) { |
10683 | 4.76k | case IS_LONG: |
10684 | 4.76k | zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value); |
10685 | 4.76k | break; |
10686 | 120k | case IS_STRING: |
10687 | 120k | zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value); |
10688 | 120k | break; |
10689 | 3.34k | case IS_DOUBLE: { |
10690 | 3.34k | zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); |
10691 | | /* Incompatible float will generate an error, leave this to run-time */ |
10692 | 3.34k | if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { |
10693 | 2.93k | goto fail; |
10694 | 2.93k | } |
10695 | 407 | zend_hash_index_update(Z_ARRVAL_P(result), lval, value); |
10696 | 407 | break; |
10697 | 3.34k | } |
10698 | 202 | case IS_FALSE: |
10699 | 202 | zend_hash_index_update(Z_ARRVAL_P(result), 0, value); |
10700 | 202 | break; |
10701 | 70 | case IS_TRUE: |
10702 | 70 | zend_hash_index_update(Z_ARRVAL_P(result), 1, value); |
10703 | 70 | break; |
10704 | 106 | case IS_NULL: |
10705 | | /* Null key will generate a warning at run-time. */ |
10706 | 106 | goto fail; |
10707 | 2 | default: |
10708 | 2 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type"); |
10709 | 129k | } |
10710 | 334k | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) { |
10711 | 3.05k | fail: |
10712 | 3.05k | zval_ptr_dtor_nogc(value); |
10713 | 3.05k | zval_ptr_dtor(result); |
10714 | 3.05k | return 0; |
10715 | 12 | } |
10716 | 463k | } |
10717 | | |
10718 | 184k | return true; |
10719 | 187k | } |
10720 | | /* }}} */ |
10721 | | |
10722 | | static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */ |
10723 | 4.21M | { |
10724 | 4.21M | zend_ast *left_ast = ast->child[0]; |
10725 | 4.21M | zend_ast *right_ast = ast->child[1]; |
10726 | 4.21M | uint32_t opcode = ast->attr; |
10727 | | |
10728 | 4.21M | znode left_node, right_node; |
10729 | | |
10730 | 4.21M | zend_compile_expr(&left_node, left_ast); |
10731 | 4.21M | zend_compile_expr(&right_node, right_ast); |
10732 | | |
10733 | 4.21M | CG(zend_lineno) = ast->lineno; |
10734 | | |
10735 | 4.21M | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10736 | 803k | if (zend_try_ct_eval_binary_op(&result->u.constant, opcode, |
10737 | 803k | &left_node.u.constant, &right_node.u.constant) |
10738 | 803k | ) { |
10739 | 741k | result->op_type = IS_CONST; |
10740 | 741k | zval_ptr_dtor(&left_node.u.constant); |
10741 | 741k | zval_ptr_dtor(&right_node.u.constant); |
10742 | 741k | return; |
10743 | 741k | } |
10744 | 803k | } |
10745 | | |
10746 | 3.47M | do { |
10747 | 3.47M | if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) { |
10748 | | /* 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) */ |
10749 | 67.1k | if (left_node.op_type == IS_CONST) { |
10750 | 35.6k | if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) { |
10751 | 14.2k | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL); |
10752 | 14.2k | opline->extended_value = |
10753 | 14.2k | (opcode == ZEND_IS_IDENTICAL) ? |
10754 | 353 | (1 << Z_TYPE(left_node.u.constant)) : |
10755 | 14.2k | (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant))); |
10756 | 14.2k | return; |
10757 | 14.2k | } |
10758 | 35.6k | } else if (right_node.op_type == IS_CONST) { |
10759 | 29.0k | if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) { |
10760 | 27.1k | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL); |
10761 | 27.1k | opline->extended_value = |
10762 | 27.1k | (opcode == ZEND_IS_IDENTICAL) ? |
10763 | 21.1k | (1 << Z_TYPE(right_node.u.constant)) : |
10764 | 27.1k | (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant))); |
10765 | 27.1k | return; |
10766 | 27.1k | } |
10767 | 29.0k | } |
10768 | 3.40M | } else if (opcode == ZEND_CONCAT) { |
10769 | | /* convert constant operands to strings at compile-time */ |
10770 | 196k | if (left_node.op_type == IS_CONST) { |
10771 | 16.1k | if (Z_TYPE(left_node.u.constant) == IS_ARRAY) { |
10772 | 151 | zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING; |
10773 | 15.9k | } else { |
10774 | 15.9k | convert_to_string(&left_node.u.constant); |
10775 | 15.9k | } |
10776 | 16.1k | } |
10777 | 196k | if (right_node.op_type == IS_CONST) { |
10778 | 26.6k | if (Z_TYPE(right_node.u.constant) == IS_ARRAY) { |
10779 | 252 | zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING; |
10780 | 26.4k | } else { |
10781 | 26.4k | convert_to_string(&right_node.u.constant); |
10782 | 26.4k | } |
10783 | 26.6k | } |
10784 | 196k | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10785 | 0 | opcode = ZEND_FAST_CONCAT; |
10786 | 0 | } |
10787 | 196k | } |
10788 | 3.43M | zend_emit_op_tmp(result, opcode, &left_node, &right_node); |
10789 | 3.43M | } while (0); |
10790 | 3.47M | } |
10791 | | /* }}} */ |
10792 | | |
10793 | | /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right |
10794 | | * evaluation order. */ |
10795 | | static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */ |
10796 | 545k | { |
10797 | 545k | zend_ast *left_ast = ast->child[0]; |
10798 | 545k | zend_ast *right_ast = ast->child[1]; |
10799 | 545k | znode left_node, right_node; |
10800 | | |
10801 | 545k | ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL); |
10802 | | |
10803 | 545k | zend_compile_expr(&left_node, left_ast); |
10804 | 545k | zend_compile_expr(&right_node, right_ast); |
10805 | | |
10806 | 545k | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10807 | 17.6k | result->op_type = IS_CONST; |
10808 | 17.6k | zend_ct_eval_greater(&result->u.constant, ast->kind, |
10809 | 17.6k | &left_node.u.constant, &right_node.u.constant); |
10810 | 17.6k | zval_ptr_dtor(&left_node.u.constant); |
10811 | 17.6k | zval_ptr_dtor(&right_node.u.constant); |
10812 | 17.6k | return; |
10813 | 17.6k | } |
10814 | | |
10815 | 528k | zend_emit_op_tmp(result, |
10816 | 528k | ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL, |
10817 | 528k | &right_node, &left_node); |
10818 | 528k | } |
10819 | | /* }}} */ |
10820 | | |
10821 | | static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */ |
10822 | 1.67M | { |
10823 | 1.67M | zend_ast *expr_ast = ast->child[0]; |
10824 | 1.67M | uint32_t opcode = ast->attr; |
10825 | | |
10826 | 1.67M | znode expr_node; |
10827 | 1.67M | zend_compile_expr(&expr_node, expr_ast); |
10828 | | |
10829 | 1.67M | if (expr_node.op_type == IS_CONST |
10830 | 269k | && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) { |
10831 | 268k | result->op_type = IS_CONST; |
10832 | 268k | zval_ptr_dtor(&expr_node.u.constant); |
10833 | 268k | return; |
10834 | 268k | } |
10835 | | |
10836 | 1.40M | zend_emit_op_tmp(result, opcode, &expr_node, NULL); |
10837 | 1.40M | } |
10838 | | /* }}} */ |
10839 | | |
10840 | | static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */ |
10841 | 224k | { |
10842 | 224k | zend_ast *expr_ast = ast->child[0]; |
10843 | 224k | znode expr_node, right_node; |
10844 | | |
10845 | 224k | ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS); |
10846 | | |
10847 | 224k | zend_compile_expr(&expr_node, expr_ast); |
10848 | | |
10849 | 224k | if (expr_node.op_type == IS_CONST |
10850 | 33.6k | && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) { |
10851 | 23.7k | result->op_type = IS_CONST; |
10852 | 23.7k | zval_ptr_dtor(&expr_node.u.constant); |
10853 | 23.7k | return; |
10854 | 23.7k | } |
10855 | | |
10856 | 200k | right_node.op_type = IS_CONST; |
10857 | 200k | ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10858 | 200k | zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node); |
10859 | 200k | } |
10860 | | /* }}} */ |
10861 | | |
10862 | | static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */ |
10863 | 16.3k | { |
10864 | 16.3k | zend_ast *left_ast = ast->child[0]; |
10865 | 16.3k | zend_ast *right_ast = ast->child[1]; |
10866 | | |
10867 | 16.3k | znode left_node, right_node; |
10868 | 16.3k | zend_op *opline_jmpz, *opline_bool; |
10869 | 16.3k | uint32_t opnum_jmpz; |
10870 | | |
10871 | 16.3k | ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR); |
10872 | | |
10873 | 16.3k | zend_compile_expr(&left_node, left_ast); |
10874 | | |
10875 | 16.3k | if (left_node.op_type == IS_CONST) { |
10876 | 2.40k | if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant)) |
10877 | 1.49k | || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) { |
10878 | 1.41k | result->op_type = IS_CONST; |
10879 | 1.41k | ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant)); |
10880 | 1.41k | } else { |
10881 | 993 | zend_compile_expr(&right_node, right_ast); |
10882 | | |
10883 | 993 | if (right_node.op_type == IS_CONST) { |
10884 | 418 | result->op_type = IS_CONST; |
10885 | 418 | ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant)); |
10886 | | |
10887 | 418 | zval_ptr_dtor(&right_node.u.constant); |
10888 | 575 | } else { |
10889 | 575 | zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL); |
10890 | 575 | } |
10891 | 993 | } |
10892 | | |
10893 | 2.40k | zval_ptr_dtor(&left_node.u.constant); |
10894 | 2.40k | return; |
10895 | 2.40k | } |
10896 | | |
10897 | 13.9k | opnum_jmpz = get_next_op_number(); |
10898 | 13.9k | opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX, |
10899 | 13.9k | &left_node, NULL); |
10900 | | |
10901 | 13.9k | if (left_node.op_type == IS_TMP_VAR) { |
10902 | 11.2k | SET_NODE(opline_jmpz->result, &left_node); |
10903 | 11.2k | GET_NODE(result, opline_jmpz->result); |
10904 | 11.2k | } else { |
10905 | 2.68k | zend_make_tmp_result(result, opline_jmpz); |
10906 | 2.68k | } |
10907 | | |
10908 | 13.9k | zend_compile_expr(&right_node, right_ast); |
10909 | | |
10910 | 13.9k | opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL); |
10911 | 13.9k | SET_NODE(opline_bool->result, result); |
10912 | | |
10913 | 13.9k | zend_update_jump_target_to_next(opnum_jmpz); |
10914 | 13.9k | } |
10915 | | /* }}} */ |
10916 | | |
10917 | | static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
10918 | 14.6k | { |
10919 | 14.6k | zend_ast *var_ast = ast->child[0]; |
10920 | 14.6k | ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC); |
10921 | | |
10922 | 14.6k | zend_ensure_writable_variable(var_ast); |
10923 | | |
10924 | 14.6k | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
10925 | 1.19k | zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false); |
10926 | 1.19k | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ; |
10927 | 1.19k | zend_make_tmp_result(result, opline); |
10928 | 13.4k | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
10929 | 414 | zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false); |
10930 | 414 | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP; |
10931 | 414 | zend_make_tmp_result(result, opline); |
10932 | 12.9k | } else { |
10933 | 12.9k | znode var_node; |
10934 | 12.9k | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
10935 | 12.9k | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
10936 | 195 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
10937 | 195 | } |
10938 | 12.9k | zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC, |
10939 | 12.9k | &var_node, NULL); |
10940 | 12.9k | } |
10941 | 14.6k | } |
10942 | | /* }}} */ |
10943 | | |
10944 | | static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
10945 | 5.50k | { |
10946 | 5.50k | zend_ast *var_ast = ast->child[0]; |
10947 | 5.50k | ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC); |
10948 | | |
10949 | 5.50k | zend_ensure_writable_variable(var_ast); |
10950 | | |
10951 | 5.50k | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
10952 | 1.17k | zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false); |
10953 | 1.17k | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ; |
10954 | 1.17k | opline->result_type = IS_TMP_VAR; |
10955 | 1.17k | result->op_type = IS_TMP_VAR; |
10956 | 4.33k | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
10957 | 200 | zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false); |
10958 | 200 | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP; |
10959 | 200 | opline->result_type = IS_TMP_VAR; |
10960 | 200 | result->op_type = IS_TMP_VAR; |
10961 | 4.13k | } else { |
10962 | 4.13k | znode var_node; |
10963 | 4.13k | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
10964 | 4.13k | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
10965 | 127 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
10966 | 127 | } |
10967 | 4.13k | zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC, |
10968 | 4.13k | &var_node, NULL); |
10969 | 4.13k | } |
10970 | 5.50k | } |
10971 | | /* }}} */ |
10972 | | |
10973 | | static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */ |
10974 | 3.77k | { |
10975 | 3.77k | zend_ast *expr_ast = ast->child[0]; |
10976 | 3.77k | znode expr_node; |
10977 | 3.77k | zend_op *opline; |
10978 | | |
10979 | 3.77k | zend_compile_expr(&expr_node, expr_ast); |
10980 | | |
10981 | 3.77k | if (ast->attr == _IS_BOOL) { |
10982 | 282 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL); |
10983 | 3.49k | } else if (ast->attr == IS_NULL) { |
10984 | 13 | zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
10985 | 3.48k | } else { |
10986 | 3.48k | opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL); |
10987 | 3.48k | opline->extended_value = ast->attr; |
10988 | 3.48k | } |
10989 | 3.77k | } |
10990 | | /* }}} */ |
10991 | | |
10992 | | static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */ |
10993 | 5.07k | { |
10994 | 5.07k | zend_ast *cond_ast = ast->child[0]; |
10995 | 5.07k | zend_ast *false_ast = ast->child[2]; |
10996 | | |
10997 | 5.07k | znode cond_node, false_node; |
10998 | 5.07k | zend_op *opline_qm_assign; |
10999 | 5.07k | uint32_t opnum_jmp_set; |
11000 | | |
11001 | 5.07k | ZEND_ASSERT(ast->child[1] == NULL); |
11002 | | |
11003 | 5.07k | zend_compile_expr(&cond_node, cond_ast); |
11004 | | |
11005 | 5.07k | opnum_jmp_set = get_next_op_number(); |
11006 | 5.07k | zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL); |
11007 | | |
11008 | 5.07k | zend_compile_expr(&false_node, false_ast); |
11009 | | |
11010 | 5.07k | opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
11011 | 5.07k | SET_NODE(opline_qm_assign->result, result); |
11012 | | |
11013 | 5.07k | zend_update_jump_target_to_next(opnum_jmp_set); |
11014 | 5.07k | } |
11015 | | /* }}} */ |
11016 | | |
11017 | | static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */ |
11018 | 12.3k | { |
11019 | 12.3k | zend_ast *cond_ast = ast->child[0]; |
11020 | 12.3k | zend_ast *true_ast = ast->child[1]; |
11021 | 12.3k | zend_ast *false_ast = ast->child[2]; |
11022 | | |
11023 | 12.3k | znode cond_node, true_node, false_node; |
11024 | 12.3k | zend_op *opline_qm_assign2; |
11025 | 12.3k | uint32_t opnum_jmpz, opnum_jmp; |
11026 | | |
11027 | 12.3k | if (cond_ast->kind == ZEND_AST_CONDITIONAL |
11028 | 1.66k | && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) { |
11029 | 1.26k | if (cond_ast->child[1]) { |
11030 | 28 | if (true_ast) { |
11031 | 15 | zend_error(E_COMPILE_ERROR, |
11032 | 15 | "Unparenthesized `a ? b : c ? d : e` is not supported. " |
11033 | 15 | "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`"); |
11034 | 15 | } else { |
11035 | 13 | zend_error(E_COMPILE_ERROR, |
11036 | 13 | "Unparenthesized `a ? b : c ?: d` is not supported. " |
11037 | 13 | "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`"); |
11038 | 13 | } |
11039 | 1.23k | } else { |
11040 | 1.23k | if (true_ast) { |
11041 | 6 | zend_error(E_COMPILE_ERROR, |
11042 | 6 | "Unparenthesized `a ?: b ? c : d` is not supported. " |
11043 | 6 | "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`"); |
11044 | 1.23k | } else { |
11045 | | /* This case is harmless: (a ?: b) ?: c always produces the same result |
11046 | | * as a ?: (b ?: c). */ |
11047 | 1.23k | } |
11048 | 1.23k | } |
11049 | 1.26k | } |
11050 | | |
11051 | 12.3k | if (!true_ast) { |
11052 | 5.07k | zend_compile_shorthand_conditional(result, ast); |
11053 | 5.07k | return; |
11054 | 5.07k | } |
11055 | | |
11056 | 7.25k | zend_compile_expr(&cond_node, cond_ast); |
11057 | | |
11058 | 7.25k | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
11059 | | |
11060 | 7.25k | zend_compile_expr(&true_node, true_ast); |
11061 | | |
11062 | 7.25k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL); |
11063 | | |
11064 | 7.25k | opnum_jmp = zend_emit_jump(0); |
11065 | | |
11066 | 7.25k | zend_update_jump_target_to_next(opnum_jmpz); |
11067 | | |
11068 | 7.25k | zend_compile_expr(&false_node, false_ast); |
11069 | | |
11070 | 7.25k | opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
11071 | 7.25k | SET_NODE(opline_qm_assign2->result, result); |
11072 | | |
11073 | 7.25k | zend_update_jump_target_to_next(opnum_jmp); |
11074 | 7.25k | } |
11075 | | /* }}} */ |
11076 | | |
11077 | | static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
11078 | 1.86M | { |
11079 | 1.86M | zend_ast *expr_ast = ast->child[0]; |
11080 | 1.86M | zend_ast *default_ast = ast->child[1]; |
11081 | | |
11082 | 1.86M | znode expr_node, default_node; |
11083 | 1.86M | zend_op *opline; |
11084 | 1.86M | uint32_t opnum; |
11085 | | |
11086 | 1.86M | zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false); |
11087 | | |
11088 | 1.86M | opnum = get_next_op_number(); |
11089 | 1.86M | zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL); |
11090 | | |
11091 | 1.86M | zend_compile_expr(&default_node, default_ast); |
11092 | | |
11093 | 1.86M | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL); |
11094 | 1.86M | SET_NODE(opline->result, result); |
11095 | | |
11096 | 1.86M | opline = &CG(active_op_array)->opcodes[opnum]; |
11097 | 1.86M | opline->op2.opline_num = get_next_op_number(); |
11098 | 1.86M | } |
11099 | | /* }}} */ |
11100 | | |
11101 | 344k | static void znode_dtor(zval *zv) { |
11102 | 344k | znode *node = Z_PTR_P(zv); |
11103 | 344k | if (node->op_type == IS_CONST) { |
11104 | 6.97k | zval_ptr_dtor_nogc(&node->u.constant); |
11105 | 6.97k | } |
11106 | 344k | efree(node); |
11107 | 344k | } |
11108 | | |
11109 | | static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
11110 | 132k | { |
11111 | 132k | zend_ast *var_ast = ast->child[0]; |
11112 | 132k | zend_ast *default_ast = ast->child[1]; |
11113 | | |
11114 | 132k | znode var_node_is, var_node_w, default_node, assign_node, *node; |
11115 | 132k | zend_op *opline; |
11116 | 132k | uint32_t coalesce_opnum; |
11117 | 132k | bool need_frees = false; |
11118 | | |
11119 | | /* Remember expressions compiled during the initial BP_VAR_IS lookup, |
11120 | | * to avoid double-evaluation when we compile again with BP_VAR_W. */ |
11121 | 132k | HashTable *orig_memoized_exprs = CG(memoized_exprs); |
11122 | 132k | const zend_memoize_mode orig_memoize_mode = CG(memoize_mode); |
11123 | | |
11124 | 132k | zend_ensure_writable_variable(var_ast); |
11125 | 132k | if (is_this_fetch(var_ast)) { |
11126 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
11127 | 5 | } |
11128 | | |
11129 | 132k | ALLOC_HASHTABLE(CG(memoized_exprs)); |
11130 | 132k | zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0); |
11131 | | |
11132 | 132k | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
11133 | 132k | zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false); |
11134 | | |
11135 | 132k | coalesce_opnum = get_next_op_number(); |
11136 | 132k | zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL); |
11137 | | |
11138 | 132k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
11139 | 132k | if (var_ast->kind == ZEND_AST_DIM) { |
11140 | 129k | zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast); |
11141 | 129k | } else { |
11142 | 3.53k | zend_compile_expr(&default_node, default_ast); |
11143 | 3.53k | } |
11144 | | |
11145 | 132k | CG(memoize_mode) = ZEND_MEMOIZE_FETCH; |
11146 | 132k | zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false); |
11147 | | |
11148 | | /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */ |
11149 | 132k | opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
11150 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
11151 | 132k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
11152 | 132k | switch (kind) { |
11153 | 1.58k | case ZEND_AST_VAR: |
11154 | 1.58k | zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node); |
11155 | 1.58k | break; |
11156 | 1.01k | case ZEND_AST_STATIC_PROP: |
11157 | 1.01k | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
11158 | 1.01k | opline->result_type = IS_TMP_VAR; |
11159 | 1.01k | var_node_w.op_type = IS_TMP_VAR; |
11160 | 1.01k | zend_emit_op_data(&default_node); |
11161 | 1.01k | assign_node = var_node_w; |
11162 | 1.01k | break; |
11163 | 128k | case ZEND_AST_DIM: |
11164 | 128k | opline->opcode = ZEND_ASSIGN_DIM; |
11165 | 128k | opline->result_type = IS_TMP_VAR; |
11166 | 128k | var_node_w.op_type = IS_TMP_VAR; |
11167 | 128k | zend_emit_op_data(&default_node); |
11168 | 128k | assign_node = var_node_w; |
11169 | 128k | break; |
11170 | 1.14k | case ZEND_AST_PROP: |
11171 | 1.14k | case ZEND_AST_NULLSAFE_PROP: |
11172 | 1.14k | opline->opcode = ZEND_ASSIGN_OBJ; |
11173 | 1.14k | opline->result_type = IS_TMP_VAR; |
11174 | 1.14k | var_node_w.op_type = IS_TMP_VAR; |
11175 | 1.14k | zend_emit_op_data(&default_node); |
11176 | 1.14k | assign_node = var_node_w; |
11177 | 1.14k | break; |
11178 | 0 | default: ZEND_UNREACHABLE(); |
11179 | 132k | } |
11180 | | |
11181 | 132k | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL); |
11182 | 132k | SET_NODE(opline->result, result); |
11183 | | |
11184 | 405k | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
11185 | 405k | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
11186 | 129k | need_frees = true; |
11187 | 129k | break; |
11188 | 129k | } |
11189 | 405k | } ZEND_HASH_FOREACH_END(); |
11190 | | |
11191 | | /* Free DUPed expressions if there are any */ |
11192 | 132k | if (need_frees) { |
11193 | 129k | uint32_t jump_opnum = zend_emit_jump(0); |
11194 | 129k | zend_update_jump_target_to_next(coalesce_opnum); |
11195 | 806k | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
11196 | 806k | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
11197 | 335k | zend_emit_op(NULL, ZEND_FREE, node, NULL); |
11198 | 335k | } |
11199 | 806k | } ZEND_HASH_FOREACH_END(); |
11200 | 129k | zend_update_jump_target_to_next(jump_opnum); |
11201 | 129k | } else { |
11202 | 3.31k | zend_update_jump_target_to_next(coalesce_opnum); |
11203 | 3.31k | } |
11204 | | |
11205 | 132k | zend_hash_destroy(CG(memoized_exprs)); |
11206 | 132k | FREE_HASHTABLE(CG(memoized_exprs)); |
11207 | 132k | CG(memoized_exprs) = orig_memoized_exprs; |
11208 | 132k | CG(memoize_mode) = orig_memoize_mode; |
11209 | 132k | } |
11210 | | /* }}} */ |
11211 | | |
11212 | | static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */ |
11213 | 10.2k | { |
11214 | 10.2k | zend_op *opline; |
11215 | 10.2k | zend_ast *expr_ast = ast->child[0]; |
11216 | | |
11217 | 10.2k | znode expr_node; |
11218 | 10.2k | zend_compile_expr(&expr_node, expr_ast); |
11219 | | |
11220 | 10.2k | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
11221 | 10.2k | opline->extended_value = 1; |
11222 | | |
11223 | 10.2k | result->op_type = IS_CONST; |
11224 | 10.2k | ZVAL_LONG(&result->u.constant, 1); |
11225 | 10.2k | } |
11226 | | /* }}} */ |
11227 | | |
11228 | | static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */ |
11229 | 436k | { |
11230 | 436k | zend_ast *value_ast = ast->child[0]; |
11231 | 436k | zend_ast *key_ast = ast->child[1]; |
11232 | | |
11233 | 436k | znode value_node, key_node; |
11234 | 436k | znode *value_node_ptr = NULL, *key_node_ptr = NULL; |
11235 | 436k | zend_op *opline; |
11236 | 436k | bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
11237 | | |
11238 | 436k | zend_mark_function_as_generator(); |
11239 | | |
11240 | 436k | if (key_ast) { |
11241 | 697 | zend_compile_expr(&key_node, key_ast); |
11242 | 697 | key_node_ptr = &key_node; |
11243 | 697 | } |
11244 | | |
11245 | 436k | if (value_ast) { |
11246 | 435k | if (returns_by_ref && zend_is_variable_or_call(value_ast)) { |
11247 | 267k | zend_assert_not_short_circuited(value_ast); |
11248 | 267k | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11249 | 267k | } else { |
11250 | 167k | zend_compile_expr(&value_node, value_ast); |
11251 | 167k | } |
11252 | 435k | value_node_ptr = &value_node; |
11253 | 435k | } |
11254 | | |
11255 | 436k | opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr); |
11256 | | |
11257 | 436k | if (value_ast && returns_by_ref && zend_is_call(value_ast)) { |
11258 | 267k | opline->extended_value = ZEND_RETURNS_FUNCTION; |
11259 | 267k | } |
11260 | 436k | } |
11261 | | /* }}} */ |
11262 | | |
11263 | | static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */ |
11264 | 1.27k | { |
11265 | 1.27k | zend_ast *expr_ast = ast->child[0]; |
11266 | 1.27k | znode expr_node; |
11267 | | |
11268 | 1.27k | zend_mark_function_as_generator(); |
11269 | | |
11270 | 1.27k | if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) { |
11271 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
11272 | 5 | "Cannot use \"yield from\" inside a by-reference generator"); |
11273 | 5 | } |
11274 | | |
11275 | 1.27k | zend_compile_expr(&expr_node, expr_ast); |
11276 | 1.27k | zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL); |
11277 | 1.27k | } |
11278 | | /* }}} */ |
11279 | | |
11280 | | static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */ |
11281 | 918 | { |
11282 | 918 | zend_ast *obj_ast = ast->child[0]; |
11283 | 918 | zend_ast *class_ast = ast->child[1]; |
11284 | | |
11285 | 918 | znode obj_node, class_node; |
11286 | 918 | zend_op *opline; |
11287 | | |
11288 | 918 | zend_compile_expr(&obj_node, obj_ast); |
11289 | 918 | if (obj_node.op_type == IS_CONST) { |
11290 | 158 | zend_do_free(&obj_node); |
11291 | 158 | result->op_type = IS_CONST; |
11292 | 158 | ZVAL_FALSE(&result->u.constant); |
11293 | 158 | return; |
11294 | 158 | } |
11295 | | |
11296 | 760 | zend_compile_class_ref(&class_node, class_ast, |
11297 | 760 | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT); |
11298 | | |
11299 | 760 | opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL); |
11300 | | |
11301 | 760 | if (class_node.op_type == IS_CONST) { |
11302 | 484 | opline->op2_type = IS_CONST; |
11303 | 484 | opline->op2.constant = zend_add_class_name_literal( |
11304 | 484 | Z_STR(class_node.u.constant)); |
11305 | 484 | opline->extended_value = zend_alloc_cache_slot(); |
11306 | 484 | } else { |
11307 | 276 | SET_NODE(opline->op2, &class_node); |
11308 | 276 | } |
11309 | 760 | } |
11310 | | /* }}} */ |
11311 | | |
11312 | | static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */ |
11313 | 9.52k | { |
11314 | 9.52k | zend_ast *expr_ast = ast->child[0]; |
11315 | 9.52k | znode expr_node; |
11316 | 9.52k | zend_op *opline; |
11317 | | |
11318 | 9.52k | zend_do_extended_fcall_begin(); |
11319 | 9.52k | zend_compile_expr(&expr_node, expr_ast); |
11320 | | |
11321 | 9.52k | opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL); |
11322 | 9.52k | opline->extended_value = ast->attr; |
11323 | | |
11324 | 9.52k | zend_do_extended_fcall_end(); |
11325 | 9.52k | } |
11326 | | /* }}} */ |
11327 | | |
11328 | | static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */ |
11329 | 9.33k | { |
11330 | 9.33k | zend_ast *var_ast = ast->child[0]; |
11331 | | |
11332 | 9.33k | znode var_node; |
11333 | 9.33k | zend_op *opline = NULL; |
11334 | | |
11335 | 9.33k | ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY); |
11336 | | |
11337 | 9.33k | if (!zend_is_variable(var_ast)) { |
11338 | 167 | if (ast->kind == ZEND_AST_EMPTY) { |
11339 | | /* empty(expr) can be transformed to !expr */ |
11340 | 129 | zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast); |
11341 | 129 | zend_compile_expr(result, not_ast); |
11342 | 129 | return; |
11343 | 129 | } else { |
11344 | 38 | zend_error_noreturn(E_COMPILE_ERROR, |
11345 | 38 | "Cannot use isset() on the result of an expression " |
11346 | 38 | "(you can use \"null !== expression\" instead)"); |
11347 | 38 | } |
11348 | 167 | } |
11349 | | |
11350 | 9.17k | if (is_globals_fetch(var_ast)) { |
11351 | 345 | result->op_type = IS_CONST; |
11352 | 345 | ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET); |
11353 | 345 | return; |
11354 | 345 | } |
11355 | | |
11356 | 8.82k | if (is_global_var_fetch(var_ast)) { |
11357 | 600 | if (!var_ast->child[1]) { |
11358 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
11359 | 6 | } |
11360 | | |
11361 | 594 | zend_compile_expr(&var_node, var_ast->child[1]); |
11362 | 594 | if (var_node.op_type == IS_CONST) { |
11363 | 493 | convert_to_string(&var_node.u.constant); |
11364 | 493 | } |
11365 | | |
11366 | 594 | opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL); |
11367 | 594 | opline->extended_value = |
11368 | 594 | ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0); |
11369 | 594 | return; |
11370 | 600 | } |
11371 | | |
11372 | 8.22k | zend_short_circuiting_mark_inner(var_ast); |
11373 | 8.22k | switch (var_ast->kind) { |
11374 | 2.39k | case ZEND_AST_VAR: |
11375 | 2.39k | if (is_this_fetch(var_ast)) { |
11376 | 147 | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL); |
11377 | 147 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
11378 | 2.24k | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) { |
11379 | 1.91k | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL); |
11380 | 1.91k | } else { |
11381 | 331 | opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false); |
11382 | 331 | opline->opcode = ZEND_ISSET_ISEMPTY_VAR; |
11383 | 331 | } |
11384 | 2.39k | break; |
11385 | 3.95k | case ZEND_AST_DIM: |
11386 | 3.95k | opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false); |
11387 | 3.95k | opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ; |
11388 | 3.95k | break; |
11389 | 1.34k | case ZEND_AST_PROP: |
11390 | 1.65k | case ZEND_AST_NULLSAFE_PROP: |
11391 | 1.65k | opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false); |
11392 | 1.65k | opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ; |
11393 | 1.65k | break; |
11394 | 219 | case ZEND_AST_STATIC_PROP: |
11395 | 219 | opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false); |
11396 | 219 | opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP; |
11397 | 219 | break; |
11398 | 0 | default: ZEND_UNREACHABLE(); |
11399 | 8.22k | } |
11400 | | |
11401 | 8.21k | result->op_type = opline->result_type = IS_TMP_VAR; |
11402 | 8.21k | if (!(ast->kind == ZEND_AST_ISSET)) { |
11403 | 1.30k | opline->extended_value |= ZEND_ISEMPTY; |
11404 | 1.30k | } |
11405 | 8.21k | } |
11406 | | /* }}} */ |
11407 | | |
11408 | | static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */ |
11409 | 16.2M | { |
11410 | 16.2M | zend_ast *expr_ast = ast->child[0]; |
11411 | 16.2M | znode silence_node; |
11412 | | |
11413 | 16.2M | zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL); |
11414 | | |
11415 | 16.2M | if (expr_ast->kind == ZEND_AST_VAR) { |
11416 | | /* For @$var we need to force a FETCH instruction, otherwise the CV access will |
11417 | | * happen outside the silenced section. */ |
11418 | 203k | zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false ); |
11419 | 16.0M | } else { |
11420 | 16.0M | zend_compile_expr(result, expr_ast); |
11421 | 16.0M | } |
11422 | | |
11423 | 16.2M | zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL); |
11424 | 16.2M | } |
11425 | | /* }}} */ |
11426 | | |
11427 | | static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */ |
11428 | 48.6k | { |
11429 | 48.6k | zend_ast *expr_ast = ast->child[0]; |
11430 | | |
11431 | 48.6k | zval fn_name; |
11432 | 48.6k | zend_ast *name_ast, *args_ast, *call_ast; |
11433 | | |
11434 | 48.6k | zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead"); |
11435 | | |
11436 | 48.6k | ZVAL_STRING(&fn_name, "shell_exec"); |
11437 | 48.6k | name_ast = zend_ast_create_zval(&fn_name); |
11438 | 48.6k | args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast); |
11439 | 48.6k | call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast); |
11440 | | |
11441 | 48.6k | zend_compile_expr(result, call_ast); |
11442 | | |
11443 | 48.6k | zval_ptr_dtor(&fn_name); |
11444 | 48.6k | } |
11445 | | /* }}} */ |
11446 | | |
11447 | | static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */ |
11448 | 234k | { |
11449 | 234k | zend_ast_list *list = zend_ast_get_list(ast); |
11450 | 234k | zend_op *opline; |
11451 | 234k | uint32_t i, opnum_init = -1; |
11452 | 234k | bool packed = true; |
11453 | | |
11454 | 234k | if (zend_try_ct_eval_array(&result->u.constant, ast)) { |
11455 | 191k | result->op_type = IS_CONST; |
11456 | 191k | return; |
11457 | 191k | } |
11458 | | |
11459 | | /* Empty arrays are handled at compile-time */ |
11460 | 42.4k | ZEND_ASSERT(list->children > 0); |
11461 | | |
11462 | 171k | for (i = 0; i < list->children; ++i) { |
11463 | 129k | zend_ast *elem_ast = list->child[i]; |
11464 | 129k | zend_ast *value_ast, *key_ast; |
11465 | 129k | bool by_ref; |
11466 | 129k | znode value_node, key_node, *key_node_ptr = NULL; |
11467 | | |
11468 | 129k | if (elem_ast == NULL) { |
11469 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
11470 | 0 | } |
11471 | | |
11472 | 129k | value_ast = elem_ast->child[0]; |
11473 | | |
11474 | 129k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
11475 | 1.47k | zend_compile_expr(&value_node, value_ast); |
11476 | 1.47k | if (i == 0) { |
11477 | 1.01k | opnum_init = get_next_op_number(); |
11478 | 1.01k | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
11479 | 1.01k | } |
11480 | 1.47k | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL); |
11481 | 1.47k | SET_NODE(opline->result, result); |
11482 | 1.47k | continue; |
11483 | 1.47k | } |
11484 | | |
11485 | 128k | key_ast = elem_ast->child[1]; |
11486 | 128k | by_ref = elem_ast->attr; |
11487 | | |
11488 | 128k | if (key_ast) { |
11489 | 12.0k | zend_compile_expr(&key_node, key_ast); |
11490 | 12.0k | zend_handle_numeric_op(&key_node); |
11491 | 12.0k | key_node_ptr = &key_node; |
11492 | 12.0k | } |
11493 | | |
11494 | 128k | if (by_ref) { |
11495 | 1.23k | zend_ensure_writable_variable(value_ast); |
11496 | 1.23k | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11497 | 126k | } else { |
11498 | 126k | zend_compile_expr(&value_node, value_ast); |
11499 | 126k | } |
11500 | | |
11501 | 128k | if (i == 0) { |
11502 | 41.3k | opnum_init = get_next_op_number(); |
11503 | 41.3k | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr); |
11504 | 41.3k | opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT; |
11505 | 86.8k | } else { |
11506 | 86.8k | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, |
11507 | 86.8k | &value_node, key_node_ptr); |
11508 | 86.8k | SET_NODE(opline->result, result); |
11509 | 86.8k | } |
11510 | 128k | opline->extended_value |= by_ref; |
11511 | | |
11512 | 128k | if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) { |
11513 | 7.16k | packed = false; |
11514 | 7.16k | } |
11515 | 128k | } |
11516 | | |
11517 | | /* Add a flag to INIT_ARRAY if we know this array cannot be packed */ |
11518 | 42.3k | if (!packed) { |
11519 | 2.69k | ZEND_ASSERT(opnum_init != (uint32_t)-1); |
11520 | 2.69k | opline = &CG(active_op_array)->opcodes[opnum_init]; |
11521 | 2.69k | opline->extended_value |= ZEND_ARRAY_NOT_PACKED; |
11522 | 2.69k | } |
11523 | 42.3k | } |
11524 | | /* }}} */ |
11525 | | |
11526 | | static void zend_emit_fetch_constant(znode *result, zend_string *resolved_name, bool unqualified_in_namespace) |
11527 | 10.6M | { |
11528 | 10.6M | zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CONSTANT, NULL, NULL); |
11529 | 10.6M | opline->op2_type = IS_CONST; |
11530 | | |
11531 | 10.6M | if (unqualified_in_namespace) { |
11532 | 10.4M | opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE; |
11533 | 10.4M | opline->op2.constant = zend_add_const_name_literal(resolved_name, true); |
11534 | 10.4M | } else { |
11535 | 146k | opline->op1.num = 0; |
11536 | 146k | opline->op2.constant = zend_add_const_name_literal(resolved_name, false); |
11537 | 146k | } |
11538 | 10.6M | opline->extended_value = zend_alloc_cache_slot(); |
11539 | 10.6M | } |
11540 | | |
11541 | | static void zend_compile_const(znode *result, const zend_ast *ast) /* {{{ */ |
11542 | 10.6M | { |
11543 | 10.6M | zend_ast *name_ast = ast->child[0]; |
11544 | | |
11545 | 10.6M | bool is_fully_qualified; |
11546 | 10.6M | zend_string *orig_name = zend_ast_get_str(name_ast); |
11547 | 10.6M | zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified); |
11548 | | |
11549 | 10.6M | 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__"))) { |
11550 | 1.13k | zend_ast *last = CG(ast); |
11551 | | |
11552 | 2.27k | while (last && last->kind == ZEND_AST_STMT_LIST) { |
11553 | 1.34k | const zend_ast_list *list = zend_ast_get_list(last); |
11554 | 1.34k | if (list->children == 0) { |
11555 | 211 | break; |
11556 | 211 | } |
11557 | 1.13k | last = list->child[list->children-1]; |
11558 | 1.13k | } |
11559 | 1.13k | if (last && last->kind == ZEND_AST_HALT_COMPILER) { |
11560 | 208 | result->op_type = IS_CONST; |
11561 | 208 | ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0]))); |
11562 | 208 | zend_string_release_ex(resolved_name, 0); |
11563 | 208 | return; |
11564 | 208 | } |
11565 | 1.13k | } |
11566 | | |
11567 | 10.6M | if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) { |
11568 | 34.3k | result->op_type = IS_CONST; |
11569 | 34.3k | zend_string_release_ex(resolved_name, 0); |
11570 | 34.3k | return; |
11571 | 34.3k | } |
11572 | | |
11573 | 10.6M | zend_emit_fetch_constant(result, resolved_name, |
11574 | 10.6M | !is_fully_qualified && FC(current_namespace)); |
11575 | 10.6M | } |
11576 | | /* }}} */ |
11577 | | |
11578 | | static void zend_compile_constant(znode *result, zend_ast *ast) |
11579 | 16 | { |
11580 | 16 | zend_string *name = zend_ast_get_constant_name(ast); |
11581 | | |
11582 | 16 | zend_emit_fetch_constant(result, zend_string_copy(name), |
11583 | 16 | (ast->attr & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) != 0); |
11584 | 16 | } |
11585 | | |
11586 | | static void zend_compile_class_const(znode *result, zend_ast *ast) /* {{{ */ |
11587 | 103k | { |
11588 | 103k | zend_ast *class_ast; |
11589 | 103k | zend_ast *const_ast; |
11590 | 103k | znode class_node, const_node; |
11591 | 103k | zend_op *opline; |
11592 | | |
11593 | 103k | zend_eval_const_expr(&ast->child[0]); |
11594 | 103k | zend_eval_const_expr(&ast->child[1]); |
11595 | | |
11596 | 103k | class_ast = ast->child[0]; |
11597 | 103k | const_ast = ast->child[1]; |
11598 | | |
11599 | 103k | if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) { |
11600 | 15.6k | zval *const_zv = zend_ast_get_zval(const_ast); |
11601 | 15.6k | if (Z_TYPE_P(const_zv) == IS_STRING) { |
11602 | 15.2k | zend_string *const_str = Z_STR_P(const_zv); |
11603 | 15.2k | zend_string *resolved_name = zend_resolve_class_name_ast(class_ast); |
11604 | 15.2k | if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) { |
11605 | 374 | result->op_type = IS_CONST; |
11606 | 374 | zend_string_release_ex(resolved_name, 0); |
11607 | 374 | return; |
11608 | 374 | } |
11609 | 14.9k | zend_string_release_ex(resolved_name, 0); |
11610 | 14.9k | } |
11611 | 15.6k | } |
11612 | | |
11613 | 103k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
11614 | | |
11615 | 103k | zend_compile_expr(&const_node, const_ast); |
11616 | | |
11617 | 103k | opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node); |
11618 | | |
11619 | 103k | zend_set_class_name_op1(opline, &class_node); |
11620 | | |
11621 | 103k | if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) { |
11622 | 102k | opline->extended_value = zend_alloc_cache_slots(2); |
11623 | 102k | } |
11624 | 103k | } |
11625 | | /* }}} */ |
11626 | | |
11627 | | static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */ |
11628 | 6.17k | { |
11629 | 6.17k | zend_ast *class_ast = ast->child[0]; |
11630 | | |
11631 | 6.17k | if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) { |
11632 | 3.69k | result->op_type = IS_CONST; |
11633 | 3.69k | return; |
11634 | 3.69k | } |
11635 | | |
11636 | 2.48k | if (class_ast->kind == ZEND_AST_ZVAL) { |
11637 | 708 | zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11638 | 708 | opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast)); |
11639 | 1.77k | } else { |
11640 | 1.77k | znode expr_node; |
11641 | 1.77k | zend_compile_expr(&expr_node, class_ast); |
11642 | 1.77k | if (expr_node.op_type == IS_CONST) { |
11643 | | /* Unlikely case that happen if class_ast is constant folded. |
11644 | | * Handle it here, to avoid needing a CONST specialization in the VM. */ |
11645 | 17 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s", |
11646 | 17 | zend_zval_value_name(&expr_node.u.constant)); |
11647 | 17 | } |
11648 | | |
11649 | 1.75k | zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL); |
11650 | 1.75k | } |
11651 | 2.48k | } |
11652 | | /* }}} */ |
11653 | | |
11654 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11655 | 354k | { |
11656 | 354k | if (num == 0) { |
11657 | 76.0k | result->op_type = IS_TMP_VAR; |
11658 | 76.0k | result->u.op.var = -1; |
11659 | 76.0k | opline->opcode = ZEND_ROPE_INIT; |
11660 | 278k | } else { |
11661 | 278k | opline->opcode = ZEND_ROPE_ADD; |
11662 | 278k | SET_NODE(opline->op1, result); |
11663 | 278k | } |
11664 | 354k | SET_NODE(opline->op2, elem_node); |
11665 | 354k | SET_NODE(opline->result, result); |
11666 | 354k | opline->extended_value = num; |
11667 | 354k | return opline; |
11668 | 354k | } |
11669 | | /* }}} */ |
11670 | | |
11671 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11672 | 305k | { |
11673 | 305k | zend_op *opline = get_next_op(); |
11674 | | |
11675 | 305k | if (num == 0) { |
11676 | 18.8k | result->op_type = IS_TMP_VAR; |
11677 | 18.8k | result->u.op.var = -1; |
11678 | 18.8k | opline->opcode = ZEND_ROPE_INIT; |
11679 | 286k | } else { |
11680 | 286k | opline->opcode = ZEND_ROPE_ADD; |
11681 | 286k | SET_NODE(opline->op1, result); |
11682 | 286k | } |
11683 | 305k | SET_NODE(opline->op2, elem_node); |
11684 | 305k | SET_NODE(opline->result, result); |
11685 | 305k | opline->extended_value = num; |
11686 | 305k | return opline; |
11687 | 305k | } |
11688 | | /* }}} */ |
11689 | | |
11690 | | static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline) |
11691 | 94.9k | { |
11692 | 94.9k | if (rope_elements == 1) { |
11693 | 2.49k | if (opline->op2_type == IS_CONST) { |
11694 | 523 | GET_NODE(result, opline->op2); |
11695 | 523 | ZVAL_UNDEF(CT_CONSTANT(opline->op2)); |
11696 | 523 | SET_UNUSED(opline->op2); |
11697 | 523 | MAKE_NOP(opline); |
11698 | 1.97k | } else { |
11699 | 1.97k | opline->opcode = ZEND_CAST; |
11700 | 1.97k | opline->extended_value = IS_STRING; |
11701 | 1.97k | opline->op1_type = opline->op2_type; |
11702 | 1.97k | opline->op1 = opline->op2; |
11703 | 1.97k | SET_UNUSED(opline->op2); |
11704 | 1.97k | zend_make_tmp_result(result, opline); |
11705 | 1.97k | } |
11706 | 92.4k | } else if (rope_elements == 2) { |
11707 | 8.75k | opline->opcode = ZEND_FAST_CONCAT; |
11708 | 8.75k | opline->extended_value = 0; |
11709 | 8.75k | opline->op1_type = init_opline->op2_type; |
11710 | 8.75k | opline->op1 = init_opline->op2; |
11711 | 8.75k | zend_make_tmp_result(result, opline); |
11712 | 8.75k | MAKE_NOP(init_opline); |
11713 | 83.7k | } else { |
11714 | 83.7k | uint32_t var; |
11715 | | |
11716 | 83.7k | init_opline->extended_value = rope_elements; |
11717 | 83.7k | opline->opcode = ZEND_ROPE_END; |
11718 | 83.7k | zend_make_tmp_result(result, opline); |
11719 | 83.7k | var = opline->op1.var = get_temporary_variable(); |
11720 | | |
11721 | | /* Allocates the necessary number of zval slots to keep the rope */ |
11722 | 83.7k | uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval); |
11723 | 359k | while (i > 1) { |
11724 | 275k | get_temporary_variable(); |
11725 | 275k | i--; |
11726 | 275k | } |
11727 | | |
11728 | | /* Update all the previous opcodes to use the same variable */ |
11729 | 844k | while (opline != init_opline) { |
11730 | 760k | opline--; |
11731 | 760k | if (opline->opcode == ZEND_ROPE_ADD && |
11732 | 478k | opline->result.var == (uint32_t)-1) { |
11733 | 472k | opline->op1.var = var; |
11734 | 472k | opline->result.var = var; |
11735 | 472k | } else if (opline->opcode == ZEND_ROPE_INIT && |
11736 | 84.1k | opline->result.var == (uint32_t)-1) { |
11737 | 83.7k | opline->result.var = var; |
11738 | 83.7k | } |
11739 | 760k | } |
11740 | 83.7k | } |
11741 | 94.9k | } |
11742 | | |
11743 | | static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */ |
11744 | 91.4k | { |
11745 | 91.4k | uint32_t i, j; |
11746 | 91.4k | uint32_t rope_init_lineno = -1; |
11747 | 91.4k | zend_op *opline = NULL, *init_opline; |
11748 | 91.4k | znode elem_node, last_const_node; |
11749 | 91.4k | zend_ast_list *list = zend_ast_get_list(ast); |
11750 | 91.4k | uint32_t reserved_op_number = -1; |
11751 | | |
11752 | 91.4k | ZEND_ASSERT(list->children > 0); |
11753 | | |
11754 | 91.4k | j = 0; |
11755 | 91.4k | last_const_node.op_type = IS_UNUSED; |
11756 | 742k | for (i = 0; i < list->children; i++) { |
11757 | 651k | zend_ast *encaps_var = list->child[i]; |
11758 | | |
11759 | 651k | if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11760 | 49.8k | if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) { |
11761 | 756 | zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead"); |
11762 | 49.1k | } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11763 | 49.1k | zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead"); |
11764 | 49.1k | } |
11765 | 49.8k | } |
11766 | | |
11767 | 651k | zend_compile_expr(&elem_node, encaps_var); |
11768 | | |
11769 | 651k | if (elem_node.op_type == IS_CONST) { |
11770 | 356k | convert_to_string(&elem_node.u.constant); |
11771 | | |
11772 | 356k | if (Z_STRLEN(elem_node.u.constant) == 0) { |
11773 | 1.47k | zval_ptr_dtor(&elem_node.u.constant); |
11774 | 354k | } else if (last_const_node.op_type == IS_CONST) { |
11775 | 0 | concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant); |
11776 | 0 | zval_ptr_dtor(&elem_node.u.constant); |
11777 | 354k | } else { |
11778 | 354k | last_const_node.op_type = IS_CONST; |
11779 | 354k | ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant); |
11780 | | /* Reserve place for ZEND_ROPE_ADD instruction */ |
11781 | 354k | reserved_op_number = get_next_op_number(); |
11782 | 354k | opline = get_next_op(); |
11783 | 354k | opline->opcode = ZEND_NOP; |
11784 | 354k | } |
11785 | 356k | continue; |
11786 | 356k | } else { |
11787 | 295k | if (j == 0) { |
11788 | 91.4k | if (last_const_node.op_type == IS_CONST) { |
11789 | 76.0k | rope_init_lineno = reserved_op_number; |
11790 | 76.0k | } else { |
11791 | 15.3k | rope_init_lineno = get_next_op_number(); |
11792 | 15.3k | } |
11793 | 91.4k | } |
11794 | 295k | if (last_const_node.op_type == IS_CONST) { |
11795 | 270k | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11796 | 270k | zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11797 | 270k | last_const_node.op_type = IS_UNUSED; |
11798 | 270k | } |
11799 | 295k | opline = zend_compile_rope_add(result, j++, &elem_node); |
11800 | 295k | } |
11801 | 651k | } |
11802 | | |
11803 | 91.4k | if (j == 0) { |
11804 | 0 | result->op_type = IS_CONST; |
11805 | 0 | if (last_const_node.op_type == IS_CONST) { |
11806 | 0 | ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant); |
11807 | 0 | } else { |
11808 | 0 | ZVAL_EMPTY_STRING(&result->u.constant); |
11809 | | /* empty string */ |
11810 | 0 | } |
11811 | 0 | CG(active_op_array)->last = reserved_op_number - 1; |
11812 | 0 | return; |
11813 | 91.4k | } else if (last_const_node.op_type == IS_CONST) { |
11814 | 84.5k | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11815 | 84.5k | opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11816 | 84.5k | } |
11817 | 91.4k | init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
11818 | 91.4k | zend_compile_rope_finalize(result, j, init_opline, opline); |
11819 | 91.4k | } |
11820 | | /* }}} */ |
11821 | | |
11822 | | static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */ |
11823 | 17.0k | { |
11824 | 17.0k | zend_op *opline; |
11825 | | |
11826 | 17.0k | if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) { |
11827 | 15.4k | result->op_type = IS_CONST; |
11828 | 15.4k | return; |
11829 | 15.4k | } |
11830 | | |
11831 | 1.57k | ZEND_ASSERT(ast->attr == T_CLASS_C && |
11832 | 1.57k | CG(active_class_entry) && |
11833 | 1.57k | (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0); |
11834 | | |
11835 | 1.57k | opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11836 | 1.57k | opline->op1.num = ZEND_FETCH_CLASS_SELF; |
11837 | 1.57k | } |
11838 | | /* }}} */ |
11839 | | |
11840 | | static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */ |
11841 | 61.8k | { |
11842 | 61.8k | return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP |
11843 | 57.4k | || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL |
11844 | 56.9k | || kind == ZEND_AST_AND || kind == ZEND_AST_OR |
11845 | 56.5k | || kind == ZEND_AST_UNARY_OP |
11846 | 56.2k | || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS |
11847 | 55.0k | || kind == ZEND_AST_CAST |
11848 | 54.7k | || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM |
11849 | 52.9k | || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM |
11850 | 49.5k | || kind == ZEND_AST_UNPACK |
11851 | 49.4k | || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST |
11852 | 28.5k | || kind == ZEND_AST_CLASS_NAME |
11853 | 28.4k | || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE |
11854 | 27.2k | || kind == ZEND_AST_CONST_ENUM_INIT |
11855 | 4.39k | || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST |
11856 | 2.35k | || kind == ZEND_AST_NAMED_ARG |
11857 | 2.22k | || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP |
11858 | 1.57k | || kind == ZEND_AST_CLOSURE |
11859 | 1.41k | || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT; |
11860 | 61.8k | } |
11861 | | /* }}} */ |
11862 | | |
11863 | | static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */ |
11864 | 2.62k | { |
11865 | 2.62k | zend_ast *ast = *ast_ptr; |
11866 | 2.62k | zend_ast *class_ast = ast->child[0]; |
11867 | 2.62k | zend_string *class_name; |
11868 | 2.62k | int fetch_type; |
11869 | | |
11870 | 2.62k | if (class_ast->kind != ZEND_AST_ZVAL) { |
11871 | 17 | zend_error_noreturn(E_COMPILE_ERROR, |
11872 | 17 | "Dynamic class names are not allowed in compile-time class constant references"); |
11873 | 17 | } |
11874 | 2.60k | if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) { |
11875 | 11 | zend_throw_error(NULL, "Class name must be a valid object or a string"); |
11876 | 11 | } |
11877 | | |
11878 | 2.60k | class_name = zend_ast_get_str(class_ast); |
11879 | 2.60k | fetch_type = zend_get_class_fetch_type(class_name); |
11880 | | |
11881 | 2.60k | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
11882 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
11883 | 5 | "\"static::\" is not allowed in compile-time constants"); |
11884 | 5 | } |
11885 | | |
11886 | 2.60k | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
11887 | 1.93k | zend_string *tmp = zend_resolve_class_name_ast(class_ast); |
11888 | | |
11889 | 1.93k | zend_string_release_ex(class_name, 0); |
11890 | 1.93k | if (tmp != class_name) { |
11891 | 376 | zval *zv = zend_ast_get_zval(class_ast); |
11892 | 376 | ZVAL_STR(zv, tmp); |
11893 | 376 | class_ast->attr = ZEND_NAME_FQ; |
11894 | 376 | } |
11895 | 1.93k | } |
11896 | | |
11897 | 2.60k | ast->attr |= ZEND_FETCH_CLASS_EXCEPTION; |
11898 | 2.60k | } |
11899 | | /* }}} */ |
11900 | | |
11901 | | static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */ |
11902 | 87 | { |
11903 | 87 | zend_ast *ast = *ast_ptr; |
11904 | 87 | zend_ast *class_ast = ast->child[0]; |
11905 | 87 | if (class_ast->kind != ZEND_AST_ZVAL) { |
11906 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
11907 | 5 | "(expression)::class cannot be used in constant expressions"); |
11908 | 5 | } |
11909 | | |
11910 | 82 | zend_string *class_name = zend_ast_get_str(class_ast); |
11911 | 82 | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
11912 | | |
11913 | 82 | switch (fetch_type) { |
11914 | 76 | case ZEND_FETCH_CLASS_SELF: |
11915 | 77 | case ZEND_FETCH_CLASS_PARENT: |
11916 | | /* For the const-eval representation store the fetch type instead of the name. */ |
11917 | 77 | zend_string_release(class_name); |
11918 | 77 | ast->child[0] = NULL; |
11919 | 77 | ast->attr = fetch_type; |
11920 | 77 | return; |
11921 | 5 | case ZEND_FETCH_CLASS_STATIC: |
11922 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
11923 | 5 | "static::class cannot be used for compile-time class name resolution"); |
11924 | 0 | default: ZEND_UNREACHABLE(); |
11925 | 82 | } |
11926 | 82 | } |
11927 | | |
11928 | | static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */ |
11929 | 18.3k | { |
11930 | 18.3k | zend_ast *ast = *ast_ptr; |
11931 | 18.3k | zend_ast *name_ast = ast->child[0]; |
11932 | 18.3k | zend_string *orig_name = zend_ast_get_str(name_ast); |
11933 | 18.3k | bool is_fully_qualified; |
11934 | 18.3k | zval result; |
11935 | 18.3k | zend_string *resolved_name; |
11936 | | |
11937 | 18.3k | CG(zend_lineno) = zend_ast_get_lineno(ast); |
11938 | | |
11939 | 18.3k | resolved_name = zend_resolve_const_name( |
11940 | 18.3k | orig_name, name_ast->attr, &is_fully_qualified); |
11941 | | |
11942 | 18.3k | if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
11943 | 0 | zend_string_release_ex(resolved_name, 0); |
11944 | 0 | zend_ast_destroy(ast); |
11945 | 0 | *ast_ptr = zend_ast_create_zval(&result); |
11946 | 0 | return; |
11947 | 0 | } |
11948 | | |
11949 | 18.3k | zend_ast_destroy(ast); |
11950 | 18.3k | *ast_ptr = zend_ast_create_constant(resolved_name, |
11951 | 18.3k | !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0); |
11952 | 18.3k | } |
11953 | | /* }}} */ |
11954 | | |
11955 | | static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */ |
11956 | 194 | { |
11957 | 194 | zend_ast *ast = *ast_ptr; |
11958 | | |
11959 | | /* Other cases already resolved by constant folding */ |
11960 | 194 | ZEND_ASSERT(ast->attr == T_CLASS_C); |
11961 | | |
11962 | 194 | zend_ast_destroy(ast); |
11963 | 194 | *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS); |
11964 | 194 | } |
11965 | | /* }}} */ |
11966 | | |
11967 | | static void zend_compile_const_expr_class_reference(zend_ast *class_ast) |
11968 | 1.19k | { |
11969 | 1.19k | if (class_ast->kind == ZEND_AST_CLASS) { |
11970 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
11971 | 0 | "Cannot use anonymous class in constant expression"); |
11972 | 0 | } |
11973 | 1.19k | if (class_ast->kind != ZEND_AST_ZVAL) { |
11974 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
11975 | 6 | "Cannot use dynamic class name in constant expression"); |
11976 | 6 | } |
11977 | | |
11978 | 1.18k | zend_string *class_name = zend_resolve_class_name_ast(class_ast); |
11979 | 1.18k | int fetch_type = zend_get_class_fetch_type(class_name); |
11980 | 1.18k | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
11981 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
11982 | 6 | "\"static\" is not allowed in compile-time constants"); |
11983 | 6 | } |
11984 | | |
11985 | 1.18k | zval *class_ast_zv = zend_ast_get_zval(class_ast); |
11986 | 1.18k | zval_ptr_dtor_nogc(class_ast_zv); |
11987 | 1.18k | ZVAL_STR(class_ast_zv, class_name); |
11988 | 1.18k | class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT; |
11989 | 1.18k | } |
11990 | | |
11991 | | static void zend_compile_const_expr_new(zend_ast **ast_ptr) |
11992 | 1.02k | { |
11993 | 1.02k | zend_ast *class_ast = (*ast_ptr)->child[0]; |
11994 | 1.02k | zend_compile_const_expr_class_reference(class_ast); |
11995 | | |
11996 | 1.02k | const zend_ast *args_ast = (*ast_ptr)->child[1]; |
11997 | 1.02k | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
11998 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
11999 | 5 | } |
12000 | 1.02k | } |
12001 | | |
12002 | | static void zend_compile_const_expr_closure(zend_ast **ast_ptr) |
12003 | 165 | { |
12004 | 165 | zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr; |
12005 | 165 | const zend_ast *uses_ast = closure_ast->child[1]; |
12006 | 165 | if (!(closure_ast->flags & ZEND_ACC_STATIC)) { |
12007 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12008 | 5 | "Closures in constant expressions must be static"); |
12009 | 5 | } |
12010 | 160 | if (uses_ast) { |
12011 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12012 | 5 | "Cannot use(...) variables in constant expression"); |
12013 | 5 | } |
12014 | | |
12015 | 155 | znode node; |
12016 | 155 | zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR); |
12017 | | |
12018 | 155 | zend_ast_destroy(*ast_ptr); |
12019 | 155 | *ast_ptr = zend_ast_create_op_array(op); |
12020 | 155 | } |
12021 | | |
12022 | | static void zend_compile_const_expr_fcc(zend_ast **ast_ptr) |
12023 | 709 | { |
12024 | 709 | zend_ast **args_ast; |
12025 | 709 | switch ((*ast_ptr)->kind) { |
12026 | 540 | case ZEND_AST_CALL: |
12027 | 540 | args_ast = &(*ast_ptr)->child[1]; |
12028 | 540 | break; |
12029 | 169 | case ZEND_AST_STATIC_CALL: |
12030 | 169 | args_ast = &(*ast_ptr)->child[2]; |
12031 | 169 | break; |
12032 | 0 | default: ZEND_UNREACHABLE(); |
12033 | 709 | } |
12034 | 709 | if ((*args_ast)->kind != ZEND_AST_CALLABLE_CONVERT) { |
12035 | 33 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
12036 | 33 | } |
12037 | | |
12038 | 676 | zend_ast_list *args = zend_ast_get_list(((zend_ast_fcc*)*args_ast)->args); |
12039 | 676 | if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) { |
12040 | | // TODO: PFAs |
12041 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
12042 | 7 | } |
12043 | | |
12044 | 669 | switch ((*ast_ptr)->kind) { |
12045 | 501 | case ZEND_AST_CALL: { |
12046 | 501 | zend_ast *name_ast = (*ast_ptr)->child[0]; |
12047 | 501 | if (name_ast->kind != ZEND_AST_ZVAL) { |
12048 | 13 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression"); |
12049 | 13 | } |
12050 | 488 | zval *name_ast_zv = zend_ast_get_zval(name_ast); |
12051 | 488 | if (Z_TYPE_P(name_ast_zv) != IS_STRING) { |
12052 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name"); |
12053 | 5 | } |
12054 | 488 | bool is_fully_qualified; |
12055 | 483 | zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified); |
12056 | 483 | zval_ptr_dtor_nogc(name_ast_zv); |
12057 | 483 | ZVAL_STR(name_ast_zv, name); |
12058 | 483 | if (is_fully_qualified) { |
12059 | 145 | name_ast->attr = ZEND_NAME_FQ; |
12060 | 145 | } |
12061 | 483 | break; |
12062 | 488 | } |
12063 | 168 | case ZEND_AST_STATIC_CALL: { |
12064 | 168 | zend_ast *class_ast = (*ast_ptr)->child[0]; |
12065 | 168 | zend_compile_const_expr_class_reference(class_ast); |
12066 | 168 | zend_ast *method_ast = (*ast_ptr)->child[1]; |
12067 | 168 | if (method_ast->kind != ZEND_AST_ZVAL) { |
12068 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression"); |
12069 | 0 | } |
12070 | 168 | if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) { |
12071 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name"); |
12072 | 0 | } |
12073 | 168 | break; |
12074 | 168 | } |
12075 | 168 | default: ZEND_UNREACHABLE(); |
12076 | 669 | } |
12077 | 669 | } |
12078 | | |
12079 | | static void zend_compile_const_expr_args(zend_ast **ast_ptr) |
12080 | 1.01k | { |
12081 | 1.01k | zend_ast_list *list = zend_ast_get_list(*ast_ptr); |
12082 | 1.01k | bool uses_named_args = false; |
12083 | 1.72k | for (uint32_t i = 0; i < list->children; i++) { |
12084 | 713 | const zend_ast *arg = list->child[i]; |
12085 | 713 | if (arg->kind == ZEND_AST_UNPACK) { |
12086 | 2 | zend_error_noreturn(E_COMPILE_ERROR, |
12087 | 2 | "Argument unpacking in constant expressions is not supported"); |
12088 | 2 | } |
12089 | 711 | if (arg->kind == ZEND_AST_NAMED_ARG) { |
12090 | 128 | uses_named_args = true; |
12091 | 583 | } else if (uses_named_args) { |
12092 | 1 | zend_error_noreturn(E_COMPILE_ERROR, |
12093 | 1 | "Cannot use positional argument after named argument"); |
12094 | 1 | } |
12095 | 711 | } |
12096 | 1.01k | if (uses_named_args) { |
12097 | 126 | list->attr = 1; |
12098 | 126 | } |
12099 | 1.01k | } |
12100 | | |
12101 | | typedef struct { |
12102 | | /* Whether the value of this expression may differ on each evaluation. */ |
12103 | | bool allow_dynamic; |
12104 | | } const_expr_context; |
12105 | | |
12106 | | static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */ |
12107 | 247k | { |
12108 | 247k | const const_expr_context *ctx = context; |
12109 | 247k | zend_ast *ast = *ast_ptr; |
12110 | 247k | if (ast == NULL || ast->kind == ZEND_AST_ZVAL) { |
12111 | 185k | return; |
12112 | 185k | } |
12113 | | |
12114 | 61.8k | if (!zend_is_allowed_in_const_expr(ast->kind)) { |
12115 | 61 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
12116 | 61 | } |
12117 | | |
12118 | 61.7k | switch (ast->kind) { |
12119 | 2.62k | case ZEND_AST_CLASS_CONST: |
12120 | 2.62k | zend_compile_const_expr_class_const(ast_ptr); |
12121 | 2.62k | break; |
12122 | 87 | case ZEND_AST_CLASS_NAME: |
12123 | 87 | zend_compile_const_expr_class_name(ast_ptr); |
12124 | 87 | break; |
12125 | 18.3k | case ZEND_AST_CONST: |
12126 | 18.3k | zend_compile_const_expr_const(ast_ptr); |
12127 | 18.3k | break; |
12128 | 194 | case ZEND_AST_MAGIC_CONST: |
12129 | 194 | zend_compile_const_expr_magic_const(ast_ptr); |
12130 | 194 | break; |
12131 | 342 | case ZEND_AST_CAST: |
12132 | 342 | if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) { |
12133 | 13 | zend_error_noreturn(E_COMPILE_ERROR, |
12134 | 13 | "Object casts are not supported in this context"); |
12135 | 13 | } |
12136 | 329 | break; |
12137 | 1.03k | case ZEND_AST_NEW: |
12138 | 1.03k | if (!ctx->allow_dynamic) { |
12139 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
12140 | 6 | "New expressions are not supported in this context"); |
12141 | 6 | } |
12142 | 1.02k | zend_compile_const_expr_new(ast_ptr); |
12143 | 1.02k | break; |
12144 | 1.01k | case ZEND_AST_ARG_LIST: |
12145 | 1.01k | zend_compile_const_expr_args(ast_ptr); |
12146 | 1.01k | break; |
12147 | 165 | case ZEND_AST_CLOSURE: |
12148 | 165 | zend_compile_const_expr_closure(ast_ptr); |
12149 | | /* Return, because we do not want to traverse the children. */ |
12150 | 165 | return; |
12151 | 540 | case ZEND_AST_CALL: |
12152 | 709 | case ZEND_AST_STATIC_CALL: |
12153 | 709 | zend_compile_const_expr_fcc(ast_ptr); |
12154 | 709 | break; |
12155 | 61.7k | } |
12156 | | |
12157 | 61.4k | zend_ast_apply(ast, zend_compile_const_expr, context); |
12158 | 61.4k | } |
12159 | | /* }}} */ |
12160 | | |
12161 | | void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */ |
12162 | 101k | { |
12163 | 101k | const_expr_context context; |
12164 | 101k | context.allow_dynamic = allow_dynamic; |
12165 | | |
12166 | 101k | zend_eval_const_expr(ast_ptr); |
12167 | 101k | zend_compile_const_expr(ast_ptr, &context); |
12168 | 101k | if ((*ast_ptr)->kind != ZEND_AST_ZVAL) { |
12169 | | /* Replace with compiled AST zval representation. */ |
12170 | 39.4k | zval ast_zv; |
12171 | 39.4k | ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr)); |
12172 | 39.4k | zend_ast_destroy(*ast_ptr); |
12173 | 39.4k | *ast_ptr = zend_ast_create_zval(&ast_zv); |
12174 | 39.4k | } |
12175 | 101k | ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr)); |
12176 | 101k | } |
12177 | | /* }}} */ |
12178 | | |
12179 | | /* Same as compile_stmt, but with early binding */ |
12180 | | void zend_compile_top_stmt(zend_ast *ast) /* {{{ */ |
12181 | 730k | { |
12182 | 730k | if (!ast) { |
12183 | 91.7k | return; |
12184 | 91.7k | } |
12185 | | |
12186 | 639k | if (ast->kind == ZEND_AST_STMT_LIST) { |
12187 | 120k | const zend_ast_list *list = zend_ast_get_list(ast); |
12188 | 120k | uint32_t i; |
12189 | 755k | for (i = 0; i < list->children; ++i) { |
12190 | 635k | zend_compile_top_stmt(list->child[i]); |
12191 | 635k | } |
12192 | 120k | return; |
12193 | 120k | } |
12194 | | |
12195 | 518k | if (ast->kind == ZEND_AST_FUNC_DECL) { |
12196 | 19.7k | CG(zend_lineno) = ast->lineno; |
12197 | 19.7k | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL); |
12198 | 19.7k | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
12199 | 499k | } else if (ast->kind == ZEND_AST_CLASS) { |
12200 | 46.9k | CG(zend_lineno) = ast->lineno; |
12201 | 46.9k | zend_compile_class_decl(NULL, ast, true); |
12202 | 46.9k | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
12203 | 452k | } else { |
12204 | 452k | zend_compile_stmt(ast); |
12205 | 452k | } |
12206 | 518k | if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) { |
12207 | 508k | zend_verify_namespace(); |
12208 | 508k | } |
12209 | 518k | } |
12210 | | /* }}} */ |
12211 | | |
12212 | | static void zend_compile_stmt(zend_ast *ast) /* {{{ */ |
12213 | 11.8M | { |
12214 | 11.8M | if (!ast) { |
12215 | 616k | return; |
12216 | 616k | } |
12217 | | |
12218 | 11.2M | CG(zend_lineno) = ast->lineno; |
12219 | | |
12220 | 11.2M | if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) { |
12221 | 0 | zend_do_extended_stmt(NULL); |
12222 | 0 | } |
12223 | | |
12224 | 11.2M | switch (ast->kind) { |
12225 | 2.42M | case ZEND_AST_STMT_LIST: |
12226 | 2.42M | zend_compile_stmt_list(ast); |
12227 | 2.42M | break; |
12228 | 2.32k | case ZEND_AST_GLOBAL: |
12229 | 2.32k | zend_compile_global_var(ast); |
12230 | 2.32k | break; |
12231 | 6.00k | case ZEND_AST_STATIC: |
12232 | 6.00k | zend_compile_static_var(ast); |
12233 | 6.00k | break; |
12234 | 7.97k | case ZEND_AST_UNSET: |
12235 | 7.97k | zend_compile_unset(ast); |
12236 | 7.97k | break; |
12237 | 420k | case ZEND_AST_RETURN: |
12238 | 420k | zend_compile_return(ast); |
12239 | 420k | break; |
12240 | 2.87M | case ZEND_AST_ECHO: |
12241 | 2.87M | zend_compile_echo(ast); |
12242 | 2.87M | break; |
12243 | 1.22k | case ZEND_AST_BREAK: |
12244 | 2.43k | case ZEND_AST_CONTINUE: |
12245 | 2.43k | zend_compile_break_continue(ast); |
12246 | 2.43k | break; |
12247 | 1.70k | case ZEND_AST_GOTO: |
12248 | 1.70k | zend_compile_goto(ast); |
12249 | 1.70k | break; |
12250 | 3.09k | case ZEND_AST_LABEL: |
12251 | 3.09k | zend_compile_label(ast); |
12252 | 3.09k | break; |
12253 | 2.21k | case ZEND_AST_WHILE: |
12254 | 2.21k | zend_compile_while(ast); |
12255 | 2.21k | break; |
12256 | 571 | case ZEND_AST_DO_WHILE: |
12257 | 571 | zend_compile_do_while(ast); |
12258 | 571 | break; |
12259 | 13.9k | case ZEND_AST_FOR: |
12260 | 13.9k | zend_compile_for(ast); |
12261 | 13.9k | break; |
12262 | 18.8k | case ZEND_AST_FOREACH: |
12263 | 18.8k | zend_compile_foreach(ast); |
12264 | 18.8k | break; |
12265 | 45.2k | case ZEND_AST_IF: |
12266 | 45.2k | zend_compile_if(ast); |
12267 | 45.2k | break; |
12268 | 9.11k | case ZEND_AST_SWITCH: |
12269 | 9.11k | zend_compile_switch(ast); |
12270 | 9.11k | break; |
12271 | 35.5k | case ZEND_AST_TRY: |
12272 | 35.5k | zend_compile_try(ast); |
12273 | 35.5k | break; |
12274 | 5.88k | case ZEND_AST_DECLARE: |
12275 | 5.88k | zend_compile_declare(ast); |
12276 | 5.88k | break; |
12277 | 2.29k | case ZEND_AST_FUNC_DECL: |
12278 | 148k | case ZEND_AST_METHOD: |
12279 | 148k | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED); |
12280 | 148k | break; |
12281 | 22.9k | case ZEND_AST_ENUM_CASE: |
12282 | 22.9k | zend_compile_enum_case(ast); |
12283 | 22.9k | break; |
12284 | 31.9k | case ZEND_AST_PROP_GROUP: |
12285 | 31.9k | zend_compile_prop_group(ast); |
12286 | 31.9k | break; |
12287 | 7.29k | case ZEND_AST_CLASS_CONST_GROUP: |
12288 | 7.29k | zend_compile_class_const_group(ast); |
12289 | 7.29k | break; |
12290 | 67.2k | case ZEND_AST_USE_TRAIT: |
12291 | 67.2k | zend_compile_use_trait(ast); |
12292 | 67.2k | break; |
12293 | 252k | case ZEND_AST_CLASS: |
12294 | 252k | zend_compile_class_decl(NULL, ast, false); |
12295 | 252k | break; |
12296 | 211 | case ZEND_AST_GROUP_USE: |
12297 | 211 | zend_compile_group_use(ast); |
12298 | 211 | break; |
12299 | 1.19k | case ZEND_AST_USE: |
12300 | 1.19k | zend_compile_use(ast); |
12301 | 1.19k | break; |
12302 | 4.80k | case ZEND_AST_CONST_DECL: |
12303 | 4.80k | zend_compile_const_decl(ast); |
12304 | 4.80k | break; |
12305 | 4.30k | case ZEND_AST_NAMESPACE: |
12306 | 4.30k | zend_compile_namespace(ast); |
12307 | 4.30k | break; |
12308 | 110 | case ZEND_AST_HALT_COMPILER: |
12309 | 110 | zend_compile_halt_compiler(ast); |
12310 | 110 | break; |
12311 | 2.86k | case ZEND_AST_THROW: |
12312 | 2.86k | zend_compile_expr(NULL, ast); |
12313 | 2.86k | break; |
12314 | 541 | case ZEND_AST_CAST_VOID: |
12315 | 541 | zend_compile_void_cast(NULL, ast); |
12316 | 541 | break; |
12317 | 151k | case ZEND_AST_ASSIGN: { |
12318 | 151k | znode result; |
12319 | 151k | zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R); |
12320 | 151k | zend_do_free(&result); |
12321 | 151k | return; |
12322 | 2.29k | } |
12323 | 8.75k | case ZEND_AST_ASSIGN_REF: |
12324 | 8.75k | zend_compile_assign_ref(NULL, ast, BP_VAR_R); |
12325 | 8.75k | return; |
12326 | 4.65M | default: |
12327 | 4.65M | { |
12328 | 4.65M | znode result; |
12329 | 4.65M | zend_compile_expr(&result, ast); |
12330 | 4.65M | zend_do_free(&result); |
12331 | 4.65M | } |
12332 | 11.2M | } |
12333 | | |
12334 | 11.0M | if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) { |
12335 | 23.7k | zend_emit_tick(); |
12336 | 23.7k | } |
12337 | 11.0M | } |
12338 | | /* }}} */ |
12339 | | |
12340 | | static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */ |
12341 | 50.3M | { |
12342 | 50.3M | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12343 | 443k | zend_compile_memoized_expr(result, ast, BP_VAR_R); |
12344 | 443k | return; |
12345 | 443k | } |
12346 | | |
12347 | 49.8M | switch (ast->kind) { |
12348 | 5.86M | case ZEND_AST_ZVAL: |
12349 | 5.86M | ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast)); |
12350 | 5.86M | result->op_type = IS_CONST; |
12351 | 5.86M | return; |
12352 | 65.7k | case ZEND_AST_ZNODE: |
12353 | 65.7k | *result = *zend_ast_get_znode(ast); |
12354 | 65.7k | return; |
12355 | 657k | case ZEND_AST_VAR: |
12356 | 691k | case ZEND_AST_DIM: |
12357 | 723k | case ZEND_AST_PROP: |
12358 | 790k | case ZEND_AST_NULLSAFE_PROP: |
12359 | 795k | case ZEND_AST_STATIC_PROP: |
12360 | 4.61M | case ZEND_AST_CALL: |
12361 | 4.69M | case ZEND_AST_METHOD_CALL: |
12362 | 4.69M | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12363 | 4.70M | case ZEND_AST_STATIC_CALL: |
12364 | 4.73M | case ZEND_AST_PIPE: |
12365 | 4.73M | zend_compile_var(result, ast, BP_VAR_R, false); |
12366 | 4.73M | return; |
12367 | 283k | case ZEND_AST_ASSIGN: |
12368 | 283k | zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R); |
12369 | 283k | return; |
12370 | 1.88k | case ZEND_AST_ASSIGN_REF: |
12371 | 1.88k | zend_compile_assign_ref(result, ast, BP_VAR_R); |
12372 | 1.88k | return; |
12373 | 229k | case ZEND_AST_NEW: |
12374 | 229k | zend_compile_new(result, ast); |
12375 | 229k | return; |
12376 | 115k | case ZEND_AST_ASSIGN_OP: |
12377 | 115k | zend_compile_compound_assign(result, ast); |
12378 | 115k | return; |
12379 | 4.21M | case ZEND_AST_BINARY_OP: |
12380 | 4.21M | zend_compile_binary_op(result, ast); |
12381 | 4.21M | return; |
12382 | 446k | case ZEND_AST_GREATER: |
12383 | 545k | case ZEND_AST_GREATER_EQUAL: |
12384 | 545k | zend_compile_greater(result, ast); |
12385 | 545k | return; |
12386 | 1.67M | case ZEND_AST_UNARY_OP: |
12387 | 1.67M | zend_compile_unary_op(result, ast); |
12388 | 1.67M | return; |
12389 | 33.0k | case ZEND_AST_UNARY_PLUS: |
12390 | 224k | case ZEND_AST_UNARY_MINUS: |
12391 | 224k | zend_compile_unary_pm(result, ast); |
12392 | 224k | return; |
12393 | 10.1k | case ZEND_AST_AND: |
12394 | 16.3k | case ZEND_AST_OR: |
12395 | 16.3k | zend_compile_short_circuiting(result, ast); |
12396 | 16.3k | return; |
12397 | 10.3k | case ZEND_AST_POST_INC: |
12398 | 14.6k | case ZEND_AST_POST_DEC: |
12399 | 14.6k | zend_compile_post_incdec(result, ast); |
12400 | 14.6k | return; |
12401 | 2.58k | case ZEND_AST_PRE_INC: |
12402 | 5.50k | case ZEND_AST_PRE_DEC: |
12403 | 5.50k | zend_compile_pre_incdec(result, ast); |
12404 | 5.50k | return; |
12405 | 3.77k | case ZEND_AST_CAST: |
12406 | 3.77k | zend_compile_cast(result, ast); |
12407 | 3.77k | return; |
12408 | 12.3k | case ZEND_AST_CONDITIONAL: |
12409 | 12.3k | zend_compile_conditional(result, ast); |
12410 | 12.3k | return; |
12411 | 1.86M | case ZEND_AST_COALESCE: |
12412 | 1.86M | zend_compile_coalesce(result, ast); |
12413 | 1.86M | return; |
12414 | 132k | case ZEND_AST_ASSIGN_COALESCE: |
12415 | 132k | zend_compile_assign_coalesce(result, ast); |
12416 | 132k | return; |
12417 | 10.2k | case ZEND_AST_PRINT: |
12418 | 10.2k | zend_compile_print(result, ast); |
12419 | 10.2k | return; |
12420 | 436k | case ZEND_AST_YIELD: |
12421 | 436k | zend_compile_yield(result, ast); |
12422 | 436k | return; |
12423 | 1.27k | case ZEND_AST_YIELD_FROM: |
12424 | 1.27k | zend_compile_yield_from(result, ast); |
12425 | 1.27k | return; |
12426 | 918 | case ZEND_AST_INSTANCEOF: |
12427 | 918 | zend_compile_instanceof(result, ast); |
12428 | 918 | return; |
12429 | 9.52k | case ZEND_AST_INCLUDE_OR_EVAL: |
12430 | 9.52k | zend_compile_include_or_eval(result, ast); |
12431 | 9.52k | return; |
12432 | 7.90k | case ZEND_AST_ISSET: |
12433 | 9.33k | case ZEND_AST_EMPTY: |
12434 | 9.33k | zend_compile_isset_or_empty(result, ast); |
12435 | 9.33k | return; |
12436 | 16.2M | case ZEND_AST_SILENCE: |
12437 | 16.2M | zend_compile_silence(result, ast); |
12438 | 16.2M | return; |
12439 | 48.6k | case ZEND_AST_SHELL_EXEC: |
12440 | 48.6k | zend_compile_shell_exec(result, ast); |
12441 | 48.6k | return; |
12442 | 234k | case ZEND_AST_ARRAY: |
12443 | 234k | zend_compile_array(result, ast); |
12444 | 234k | return; |
12445 | 10.6M | case ZEND_AST_CONST: |
12446 | 10.6M | zend_compile_const(result, ast); |
12447 | 10.6M | return; |
12448 | 16 | case ZEND_AST_CONSTANT: |
12449 | 16 | zend_compile_constant(result, ast); |
12450 | 16 | return; |
12451 | 103k | case ZEND_AST_CLASS_CONST: |
12452 | 103k | zend_compile_class_const(result, ast); |
12453 | 103k | return; |
12454 | 6.17k | case ZEND_AST_CLASS_NAME: |
12455 | 6.17k | zend_compile_class_name(result, ast); |
12456 | 6.17k | return; |
12457 | 91.4k | case ZEND_AST_ENCAPS_LIST: |
12458 | 91.4k | zend_compile_encaps_list(result, ast); |
12459 | 91.4k | return; |
12460 | 17.0k | case ZEND_AST_MAGIC_CONST: |
12461 | 17.0k | zend_compile_magic_const(result, ast); |
12462 | 17.0k | return; |
12463 | 1.59M | case ZEND_AST_CLOSURE: |
12464 | 1.98M | case ZEND_AST_ARROW_FUNC: |
12465 | 1.98M | zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED); |
12466 | 1.98M | return; |
12467 | 3.47k | case ZEND_AST_THROW: |
12468 | 3.47k | zend_compile_throw(result, ast); |
12469 | 3.47k | return; |
12470 | 2.86k | case ZEND_AST_MATCH: |
12471 | 2.86k | zend_compile_match(result, ast); |
12472 | 2.86k | return; |
12473 | 0 | default: |
12474 | 0 | ZEND_ASSERT(0 /* not supported */); |
12475 | 49.8M | } |
12476 | 49.8M | } |
12477 | | /* }}} */ |
12478 | | |
12479 | | static void zend_compile_expr(znode *result, zend_ast *ast) |
12480 | 50.3M | { |
12481 | 50.3M | zend_check_stack_limit(); |
12482 | | |
12483 | 50.3M | uint32_t prev_lineno = CG(zend_lineno); |
12484 | 50.3M | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12485 | | |
12486 | 50.3M | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12487 | 50.3M | zend_compile_expr_inner(result, ast); |
12488 | 50.3M | zend_short_circuiting_commit(checkpoint, result, ast); |
12489 | 50.3M | #if ZEND_DEBUG |
12490 | 50.3M | if (result) { |
12491 | | /* BP_VAR_R is not allowed to produce IS_VAR. */ |
12492 | 50.3M | ZEND_ASSERT(result->op_type != IS_VAR); |
12493 | 50.3M | } |
12494 | 50.3M | #endif |
12495 | | |
12496 | 50.3M | CG(zend_lineno) = prev_lineno; |
12497 | 50.3M | } |
12498 | | |
12499 | | static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
12500 | 8.12M | { |
12501 | 8.12M | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12502 | 512k | switch (ast->kind) { |
12503 | 12.6k | case ZEND_AST_CALL: |
12504 | 246k | case ZEND_AST_METHOD_CALL: |
12505 | 246k | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12506 | 246k | case ZEND_AST_STATIC_CALL: |
12507 | 247k | case ZEND_AST_PIPE: |
12508 | 247k | zend_compile_memoized_expr(result, ast, BP_VAR_W); |
12509 | | /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */ |
12510 | 247k | return NULL; |
12511 | 512k | } |
12512 | 512k | } |
12513 | | |
12514 | 7.87M | switch (ast->kind) { |
12515 | 771k | case ZEND_AST_VAR: |
12516 | 771k | return zend_compile_simple_var(result, ast, type, false); |
12517 | 354k | case ZEND_AST_DIM: |
12518 | 354k | return zend_compile_dim(result, ast, type, by_ref); |
12519 | 43.1k | case ZEND_AST_PROP: |
12520 | 114k | case ZEND_AST_NULLSAFE_PROP: |
12521 | 114k | return zend_compile_prop(result, ast, type, by_ref); |
12522 | 8.97k | case ZEND_AST_STATIC_PROP: |
12523 | 8.97k | return zend_compile_static_prop(result, ast, type, by_ref, false); |
12524 | 4.45M | case ZEND_AST_CALL: |
12525 | 4.45M | zend_compile_call(result, ast, type); |
12526 | 4.45M | return NULL; |
12527 | 208k | case ZEND_AST_METHOD_CALL: |
12528 | 211k | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12529 | 211k | zend_compile_method_call(result, ast, type); |
12530 | 211k | return NULL; |
12531 | 16.6k | case ZEND_AST_STATIC_CALL: |
12532 | 16.6k | zend_compile_static_call(result, ast, type); |
12533 | 16.6k | return NULL; |
12534 | 30.3k | case ZEND_AST_PIPE: |
12535 | 30.3k | zend_compile_pipe(result, ast, type); |
12536 | 30.3k | return NULL; |
12537 | 2.77k | case ZEND_AST_ZNODE: |
12538 | 2.77k | *result = *zend_ast_get_znode(ast); |
12539 | 2.77k | return NULL; |
12540 | 577 | case ZEND_AST_ASSIGN_REF: |
12541 | 577 | zend_compile_assign_ref(result, ast, type); |
12542 | 577 | return NULL; |
12543 | 30 | case ZEND_AST_ASSIGN: |
12544 | 30 | zend_compile_assign(result, ast, false, type); |
12545 | 30 | return NULL; |
12546 | 1.90M | default: |
12547 | 1.90M | if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) { |
12548 | 222 | zend_error_noreturn(E_COMPILE_ERROR, |
12549 | 222 | "Cannot use temporary expression in write context"); |
12550 | 222 | } |
12551 | | |
12552 | 1.90M | zend_compile_expr(result, ast); |
12553 | 1.90M | return NULL; |
12554 | 7.87M | } |
12555 | 7.87M | } |
12556 | | |
12557 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12558 | 8.12M | { |
12559 | 8.12M | zend_check_stack_limit(); |
12560 | | |
12561 | 8.12M | uint32_t prev_lineno = CG(zend_lineno); |
12562 | 8.12M | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12563 | | |
12564 | 8.12M | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12565 | 8.12M | zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref); |
12566 | 8.12M | zend_short_circuiting_commit(checkpoint, result, ast); |
12567 | 8.12M | #if ZEND_DEBUG |
12568 | 8.12M | if (result |
12569 | 8.11M | && (type == BP_VAR_R || type == BP_VAR_IS) |
12570 | | /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */ |
12571 | 7.08M | && CG(memoize_mode) == ZEND_MEMOIZE_NONE |
12572 | 8.12M | ) { |
12573 | | /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */ |
12574 | 6.82M | ZEND_ASSERT(result->op_type != IS_VAR); |
12575 | 6.82M | } |
12576 | 8.12M | #endif |
12577 | | |
12578 | 8.12M | CG(zend_lineno) = prev_lineno; |
12579 | | |
12580 | 8.12M | return opcode; |
12581 | 8.12M | } |
12582 | | |
12583 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12584 | 1.30M | { |
12585 | 1.30M | zend_check_stack_limit(); |
12586 | | |
12587 | 1.30M | uint32_t prev_lineno = CG(zend_lineno); |
12588 | 1.30M | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12589 | | |
12590 | 1.30M | zend_op *opline; |
12591 | 1.30M | switch (ast->kind) { |
12592 | 691k | case ZEND_AST_VAR: |
12593 | 691k | opline = zend_compile_simple_var(result, ast, type, true); |
12594 | 691k | break; |
12595 | 246k | case ZEND_AST_DIM: |
12596 | 246k | opline = zend_delayed_compile_dim(result, ast, type, by_ref); |
12597 | 246k | break; |
12598 | 26.6k | case ZEND_AST_PROP: |
12599 | 39.3k | case ZEND_AST_NULLSAFE_PROP: |
12600 | 39.3k | { |
12601 | 39.3k | opline = zend_delayed_compile_prop(result, ast, type); |
12602 | 39.3k | if (by_ref) { |
12603 | 1.17k | opline->extended_value |= ZEND_FETCH_REF; |
12604 | 1.17k | } |
12605 | 39.3k | break; |
12606 | 26.6k | } |
12607 | 6.37k | case ZEND_AST_STATIC_PROP: |
12608 | 6.37k | opline = zend_compile_static_prop(result, ast, type, by_ref, true); |
12609 | 6.37k | break; |
12610 | 318k | default: |
12611 | 318k | opline = zend_compile_var(result, ast, type, false); |
12612 | 318k | break; |
12613 | 1.30M | } |
12614 | | |
12615 | 1.30M | CG(zend_lineno) = prev_lineno; |
12616 | | |
12617 | 1.30M | return opline; |
12618 | 1.30M | } |
12619 | | /* }}} */ |
12620 | | |
12621 | | bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1) |
12622 | 23.2k | { |
12623 | | /* NAN warns when casting */ |
12624 | 23.2k | if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) { |
12625 | 0 | return false; |
12626 | 0 | } |
12627 | 23.2k | switch (type) { |
12628 | 54 | case _IS_BOOL: |
12629 | 54 | ZVAL_BOOL(result, zend_is_true(op1)); |
12630 | 54 | return true; |
12631 | 413 | case IS_LONG: |
12632 | 413 | if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) { |
12633 | 83 | return false; |
12634 | 83 | } |
12635 | 330 | ZVAL_LONG(result, zval_get_long(op1)); |
12636 | 330 | return true; |
12637 | 862 | case IS_DOUBLE: |
12638 | 862 | ZVAL_DOUBLE(result, zval_get_double(op1)); |
12639 | 862 | return true; |
12640 | 20.0k | case IS_STRING: |
12641 | | /* Conversion from double to string takes into account run-time |
12642 | | 'precision' setting and cannot be evaluated at compile-time */ |
12643 | 20.0k | if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) { |
12644 | 19.1k | ZVAL_STR(result, zval_get_string(op1)); |
12645 | 19.1k | return true; |
12646 | 19.1k | } |
12647 | 918 | break; |
12648 | 918 | case IS_ARRAY: |
12649 | 817 | ZVAL_COPY(result, op1); |
12650 | 817 | convert_to_array(result); |
12651 | 817 | return true; |
12652 | 23.2k | } |
12653 | 1.99k | return false; |
12654 | 23.2k | } |
12655 | | |
12656 | | static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */ |
12657 | 11.3M | { |
12658 | 11.3M | zend_ast *ast = *ast_ptr; |
12659 | 11.3M | zval result; |
12660 | | |
12661 | 11.3M | if (!ast) { |
12662 | 2.99M | return; |
12663 | 2.99M | } |
12664 | | |
12665 | 8.34M | zend_check_stack_limit(); |
12666 | | |
12667 | 8.34M | switch (ast->kind) { |
12668 | 83.5k | case ZEND_AST_BINARY_OP: |
12669 | 83.5k | zend_eval_const_expr(&ast->child[0]); |
12670 | 83.5k | zend_eval_const_expr(&ast->child[1]); |
12671 | 83.5k | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12672 | 27.2k | return; |
12673 | 27.2k | } |
12674 | | |
12675 | 56.2k | if (!zend_try_ct_eval_binary_op(&result, ast->attr, |
12676 | 56.2k | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])) |
12677 | 56.2k | ) { |
12678 | 39.0k | return; |
12679 | 39.0k | } |
12680 | 17.2k | break; |
12681 | 17.2k | case ZEND_AST_GREATER: |
12682 | 3.23k | case ZEND_AST_GREATER_EQUAL: |
12683 | 3.23k | zend_eval_const_expr(&ast->child[0]); |
12684 | 3.23k | zend_eval_const_expr(&ast->child[1]); |
12685 | 3.23k | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12686 | 1.72k | return; |
12687 | 1.72k | } |
12688 | | |
12689 | 1.51k | zend_ct_eval_greater(&result, ast->kind, |
12690 | 1.51k | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])); |
12691 | 1.51k | break; |
12692 | 2.02k | case ZEND_AST_AND: |
12693 | 3.21k | case ZEND_AST_OR: |
12694 | 3.21k | { |
12695 | 3.21k | bool child0_is_true, child1_is_true; |
12696 | 3.21k | zend_eval_const_expr(&ast->child[0]); |
12697 | 3.21k | zend_eval_const_expr(&ast->child[1]); |
12698 | 3.21k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12699 | 1.36k | return; |
12700 | 1.36k | } |
12701 | | |
12702 | 1.85k | child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0])); |
12703 | 1.85k | if (child0_is_true == (ast->kind == ZEND_AST_OR)) { |
12704 | 253 | ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR); |
12705 | 253 | break; |
12706 | 253 | } |
12707 | | |
12708 | 1.60k | if (ast->child[1]->kind != ZEND_AST_ZVAL) { |
12709 | 347 | return; |
12710 | 347 | } |
12711 | | |
12712 | 1.25k | child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1])); |
12713 | 1.25k | if (ast->kind == ZEND_AST_OR) { |
12714 | 422 | ZVAL_BOOL(&result, child0_is_true || child1_is_true); |
12715 | 831 | } else { |
12716 | 831 | ZVAL_BOOL(&result, child0_is_true && child1_is_true); |
12717 | 831 | } |
12718 | 1.25k | break; |
12719 | 1.60k | } |
12720 | 82.0k | case ZEND_AST_UNARY_OP: |
12721 | 82.0k | zend_eval_const_expr(&ast->child[0]); |
12722 | 82.0k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12723 | 75.7k | return; |
12724 | 75.7k | } |
12725 | | |
12726 | 6.27k | if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
12727 | 954 | return; |
12728 | 954 | } |
12729 | 5.32k | break; |
12730 | 5.32k | case ZEND_AST_UNARY_PLUS: |
12731 | 21.3k | case ZEND_AST_UNARY_MINUS: |
12732 | 21.3k | zend_eval_const_expr(&ast->child[0]); |
12733 | 21.3k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12734 | 5.30k | return; |
12735 | 5.30k | } |
12736 | | |
12737 | 16.0k | if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) { |
12738 | 2.82k | return; |
12739 | 2.82k | } |
12740 | 13.2k | break; |
12741 | 23.4k | case ZEND_AST_COALESCE: |
12742 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12743 | 23.4k | if (ast->child[0]->kind == ZEND_AST_DIM) { |
12744 | 458 | ast->child[0]->attr |= ZEND_DIM_IS; |
12745 | 458 | } |
12746 | 23.4k | zend_eval_const_expr(&ast->child[0]); |
12747 | | |
12748 | 23.4k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12749 | | /* ensure everything was compile-time evaluated at least once */ |
12750 | 22.8k | zend_eval_const_expr(&ast->child[1]); |
12751 | 22.8k | return; |
12752 | 22.8k | } |
12753 | | |
12754 | 644 | if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) { |
12755 | 489 | zend_eval_const_expr(&ast->child[1]); |
12756 | 489 | *ast_ptr = ast->child[1]; |
12757 | 489 | ast->child[1] = NULL; |
12758 | 489 | zend_ast_destroy(ast); |
12759 | 489 | } else { |
12760 | 155 | *ast_ptr = ast->child[0]; |
12761 | 155 | ast->child[0] = NULL; |
12762 | 155 | zend_ast_destroy(ast); |
12763 | 155 | } |
12764 | 644 | return; |
12765 | 1.41k | case ZEND_AST_CONDITIONAL: |
12766 | 1.41k | { |
12767 | 1.41k | zend_ast **child, *child_ast; |
12768 | 1.41k | zend_eval_const_expr(&ast->child[0]); |
12769 | 1.41k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12770 | | /* ensure everything was compile-time evaluated at least once */ |
12771 | 1.26k | if (ast->child[1]) { |
12772 | 666 | zend_eval_const_expr(&ast->child[1]); |
12773 | 666 | } |
12774 | 1.26k | zend_eval_const_expr(&ast->child[2]); |
12775 | 1.26k | return; |
12776 | 1.26k | } |
12777 | | |
12778 | 158 | child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))]; |
12779 | 158 | if (*child == NULL) { |
12780 | 80 | child--; |
12781 | 80 | } |
12782 | 158 | child_ast = *child; |
12783 | 158 | *child = NULL; |
12784 | 158 | zend_ast_destroy(ast); |
12785 | 158 | *ast_ptr = child_ast; |
12786 | 158 | zend_eval_const_expr(ast_ptr); |
12787 | 158 | return; |
12788 | 1.41k | } |
12789 | 1.23M | case ZEND_AST_DIM: |
12790 | 1.23M | { |
12791 | | /* constant expression should be always read context ... */ |
12792 | 1.23M | const zval *container, *dim; |
12793 | | |
12794 | 1.23M | if (ast->child[1] == NULL) { |
12795 | 40 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
12796 | 40 | } |
12797 | | |
12798 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12799 | 1.23M | if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) { |
12800 | 730 | ast->child[0]->attr |= ZEND_DIM_IS; |
12801 | 730 | } |
12802 | | |
12803 | 1.23M | zend_eval_const_expr(&ast->child[0]); |
12804 | 1.23M | zend_eval_const_expr(&ast->child[1]); |
12805 | 1.23M | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12806 | 1.22M | return; |
12807 | 1.22M | } |
12808 | | |
12809 | 16.0k | container = zend_ast_get_zval(ast->child[0]); |
12810 | 16.0k | dim = zend_ast_get_zval(ast->child[1]); |
12811 | | |
12812 | 16.0k | if (Z_TYPE_P(container) == IS_ARRAY) { |
12813 | 11.2k | zval *el; |
12814 | 11.2k | if (Z_TYPE_P(dim) == IS_LONG) { |
12815 | 1.02k | el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim)); |
12816 | 1.02k | if (el) { |
12817 | 532 | ZVAL_COPY(&result, el); |
12818 | 532 | } else { |
12819 | 490 | return; |
12820 | 490 | } |
12821 | 10.2k | } else if (Z_TYPE_P(dim) == IS_STRING) { |
12822 | 10.1k | el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim)); |
12823 | 10.1k | if (el) { |
12824 | 422 | ZVAL_COPY(&result, el); |
12825 | 9.73k | } else { |
12826 | 9.73k | return; |
12827 | 9.73k | } |
12828 | 10.1k | } else { |
12829 | 99 | return; /* warning... handle at runtime */ |
12830 | 99 | } |
12831 | 11.2k | } else if (Z_TYPE_P(container) == IS_STRING) { |
12832 | 4.51k | zend_long offset; |
12833 | 4.51k | uint8_t c; |
12834 | 4.51k | if (Z_TYPE_P(dim) == IS_LONG) { |
12835 | 2.90k | offset = Z_LVAL_P(dim); |
12836 | 2.90k | } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) { |
12837 | 960 | return; |
12838 | 960 | } |
12839 | 3.55k | if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) { |
12840 | 3.24k | return; |
12841 | 3.24k | } |
12842 | 307 | c = (uint8_t) Z_STRVAL_P(container)[offset]; |
12843 | 307 | ZVAL_CHAR(&result, c); |
12844 | 307 | } else if (Z_TYPE_P(container) <= IS_FALSE) { |
12845 | 13 | return; /* warning... handle at runtime */ |
12846 | 234 | } else { |
12847 | 234 | return; |
12848 | 234 | } |
12849 | 1.26k | break; |
12850 | 16.0k | } |
12851 | 2.61M | case ZEND_AST_ARRAY: |
12852 | 2.61M | if (!zend_try_ct_eval_array(&result, ast)) { |
12853 | 2.58M | return; |
12854 | 2.58M | } |
12855 | 31.0k | break; |
12856 | 31.0k | case ZEND_AST_MAGIC_CONST: |
12857 | 1.50k | if (!zend_try_ct_eval_magic_const(&result, ast)) { |
12858 | 194 | return; |
12859 | 194 | } |
12860 | 1.31k | break; |
12861 | 104k | case ZEND_AST_CONST: |
12862 | 104k | { |
12863 | 104k | zend_ast *name_ast = ast->child[0]; |
12864 | 104k | bool is_fully_qualified; |
12865 | 104k | zend_string *resolved_name = zend_resolve_const_name( |
12866 | 104k | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
12867 | | |
12868 | 104k | if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
12869 | 97.2k | zend_string_release_ex(resolved_name, 0); |
12870 | 97.2k | return; |
12871 | 97.2k | } |
12872 | | |
12873 | 6.88k | zend_string_release_ex(resolved_name, 0); |
12874 | 6.88k | break; |
12875 | 104k | } |
12876 | 907k | case ZEND_AST_CLASS_CONST: |
12877 | 907k | { |
12878 | 907k | zend_ast *class_ast; |
12879 | 907k | zend_ast *name_ast; |
12880 | 907k | zend_string *resolved_name; |
12881 | | |
12882 | 907k | zend_eval_const_expr(&ast->child[0]); |
12883 | 907k | zend_eval_const_expr(&ast->child[1]); |
12884 | | |
12885 | 907k | if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL |
12886 | 907k | || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) { |
12887 | 1.81k | return; |
12888 | 1.81k | } |
12889 | | |
12890 | 905k | class_ast = ast->child[0]; |
12891 | 905k | name_ast = ast->child[1]; |
12892 | | |
12893 | 905k | if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) { |
12894 | 832k | return; |
12895 | 832k | } |
12896 | | |
12897 | 73.2k | resolved_name = zend_resolve_class_name_ast(class_ast); |
12898 | 73.2k | if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) { |
12899 | 73.0k | zend_string_release_ex(resolved_name, 0); |
12900 | 73.0k | return; |
12901 | 73.0k | } |
12902 | | |
12903 | 218 | zend_string_release_ex(resolved_name, 0); |
12904 | 218 | break; |
12905 | 73.2k | } |
12906 | 2.91k | case ZEND_AST_CLASS_NAME: |
12907 | 2.91k | { |
12908 | 2.91k | zend_ast *class_ast = ast->child[0]; |
12909 | 2.91k | if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) { |
12910 | 1.21k | return; |
12911 | 1.21k | } |
12912 | 1.70k | break; |
12913 | 2.91k | } |
12914 | | // TODO: We should probably use zend_ast_apply to recursively walk nodes without |
12915 | | // special handling. It is required that all nodes that are part of a const expr |
12916 | | // are visited. Probably we should be distinguishing evaluation of const expr and |
12917 | | // normal exprs here. |
12918 | 3.84k | case ZEND_AST_ARG_LIST: |
12919 | 3.84k | { |
12920 | 3.84k | zend_ast_list *list = zend_ast_get_list(ast); |
12921 | 5.46k | for (uint32_t i = 0; i < list->children; i++) { |
12922 | 1.61k | zend_eval_const_expr(&list->child[i]); |
12923 | 1.61k | } |
12924 | 3.84k | return; |
12925 | 2.91k | } |
12926 | 3.85k | case ZEND_AST_NEW: |
12927 | 3.85k | zend_eval_const_expr(&ast->child[0]); |
12928 | 3.85k | zend_eval_const_expr(&ast->child[1]); |
12929 | 3.85k | return; |
12930 | 248 | case ZEND_AST_NAMED_ARG: |
12931 | 248 | zend_eval_const_expr(&ast->child[1]); |
12932 | 248 | return; |
12933 | 22.8k | case ZEND_AST_CONST_ENUM_INIT: |
12934 | 22.8k | zend_eval_const_expr(&ast->child[3]); |
12935 | 22.8k | return; |
12936 | 3.77k | case ZEND_AST_PROP: |
12937 | 4.42k | case ZEND_AST_NULLSAFE_PROP: |
12938 | 4.42k | zend_eval_const_expr(&ast->child[0]); |
12939 | 4.42k | zend_eval_const_expr(&ast->child[1]); |
12940 | 4.42k | return; |
12941 | 3.53k | case ZEND_AST_CAST: |
12942 | 3.53k | if (ast->attr == IS_NULL) { |
12943 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
12944 | 5 | } |
12945 | 3.52k | zend_eval_const_expr(&ast->child[0]); |
12946 | 3.52k | if (ast->child[0]->kind == ZEND_AST_ZVAL |
12947 | 3.03k | && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
12948 | 2.70k | break; |
12949 | 2.70k | } |
12950 | 819 | return; |
12951 | 3.22M | default: |
12952 | 3.22M | return; |
12953 | 8.34M | } |
12954 | | |
12955 | 83.4k | zend_ast_destroy(ast); |
12956 | 83.4k | *ast_ptr = zend_ast_create_zval(&result); |
12957 | 83.4k | } |
12958 | | /* }}} */ |