/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 | | #include "zend_partial.h" |
44 | | |
45 | 44.3M | #define SET_NODE(target, src) do { \ |
46 | 44.3M | target ## _type = (src)->op_type; \ |
47 | 44.3M | if ((src)->op_type == IS_CONST) { \ |
48 | 6.76M | target.constant = zend_add_literal(&(src)->u.constant); \ |
49 | 37.5M | } else { \ |
50 | 37.5M | target = (src)->u.op; \ |
51 | 37.5M | } \ |
52 | 44.3M | } while (0) |
53 | | |
54 | 31.7M | #define GET_NODE(target, src) do { \ |
55 | 31.7M | (target)->op_type = src ## _type; \ |
56 | 31.7M | if ((target)->op_type == IS_CONST) { \ |
57 | 531 | ZVAL_COPY_VALUE(&(target)->u.constant, CT_CONSTANT(src)); \ |
58 | 31.7M | } else { \ |
59 | 31.7M | (target)->u.op = src; \ |
60 | 31.7M | } \ |
61 | 31.7M | } while (0) |
62 | | |
63 | 73.3M | #define FC(member) (CG(file_context).member) |
64 | | |
65 | | typedef struct _zend_loop_var { |
66 | | uint8_t opcode; |
67 | | uint8_t var_type; |
68 | | uint32_t var_num; |
69 | | uint32_t try_catch_offset; |
70 | | } zend_loop_var; |
71 | | |
72 | 12.0M | static inline uint32_t zend_alloc_cache_slots(unsigned count) { |
73 | 12.0M | if (count == 0) { |
74 | | /* Even if no cache slots are desired, the VM handler may still want to acquire |
75 | | * CACHE_ADDR() unconditionally. Returning zero makes sure that the address |
76 | | * calculation is still legal and ubsan does not complain. */ |
77 | 0 | return 0; |
78 | 0 | } |
79 | | |
80 | 12.0M | zend_op_array *op_array = CG(active_op_array); |
81 | 12.0M | uint32_t ret = op_array->cache_size; |
82 | 12.0M | op_array->cache_size += count * sizeof(void*); |
83 | 12.0M | return ret; |
84 | 12.0M | } |
85 | | |
86 | 11.1M | static inline uint32_t zend_alloc_cache_slot(void) { |
87 | 11.1M | return zend_alloc_cache_slots(1); |
88 | 11.1M | } |
89 | | |
90 | | ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type); |
91 | | ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position); |
92 | | |
93 | | #ifndef ZTS |
94 | | ZEND_API zend_compiler_globals compiler_globals; |
95 | | ZEND_API zend_executor_globals executor_globals; |
96 | | #endif |
97 | | |
98 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2); |
99 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast); |
100 | | static void zend_eval_const_expr(zend_ast **ast_ptr); |
101 | | |
102 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
103 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref); |
104 | | static void zend_compile_expr(znode *result, zend_ast *ast); |
105 | | static void zend_compile_stmt(zend_ast *ast); |
106 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type); |
107 | | |
108 | | static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg); |
109 | | |
110 | | #ifdef ZEND_CHECK_STACK_LIMIT |
111 | | zend_never_inline static void zend_stack_limit_error(void) |
112 | 0 | { |
113 | 0 | size_t max_stack_size = 0; |
114 | 0 | if ((uintptr_t) EG(stack_base) > (uintptr_t) EG(stack_limit)) { |
115 | 0 | max_stack_size = (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit)); |
116 | 0 | } |
117 | |
|
118 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
119 | 0 | "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached during compilation. Try splitting expression", |
120 | 0 | max_stack_size); |
121 | 0 | } |
122 | | |
123 | | static void zend_check_stack_limit(void) |
124 | 52.5M | { |
125 | 52.5M | if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) { |
126 | 0 | zend_stack_limit_error(); |
127 | 0 | } |
128 | 52.5M | } |
129 | | #else /* ZEND_CHECK_STACK_LIMIT */ |
130 | | static void zend_check_stack_limit(void) |
131 | | { |
132 | | } |
133 | | #endif /* ZEND_CHECK_STACK_LIMIT */ |
134 | | |
135 | | static void init_op(zend_op *op) |
136 | 63.2M | { |
137 | 63.2M | MAKE_NOP(op); |
138 | 63.2M | op->extended_value = 0; |
139 | 63.2M | op->lineno = CG(zend_lineno); |
140 | | #ifdef ZEND_VERIFY_TYPE_INFERENCE |
141 | | op->op1_use_type = 0; |
142 | | op->op2_use_type = 0; |
143 | | op->result_use_type = 0; |
144 | | op->op1_def_type = 0; |
145 | | op->op2_def_type = 0; |
146 | | op->result_def_type = 0; |
147 | | #endif |
148 | 63.2M | } |
149 | | |
150 | | static zend_always_inline uint32_t get_next_op_number(void) |
151 | 10.3M | { |
152 | 10.3M | return CG(active_op_array)->last; |
153 | 10.3M | } |
154 | | |
155 | | static zend_op *get_next_op(void) |
156 | 62.2M | { |
157 | 62.2M | zend_op_array *op_array = CG(active_op_array); |
158 | 62.2M | uint32_t next_op_num = op_array->last++; |
159 | 62.2M | zend_op *next_op; |
160 | | |
161 | 62.2M | if (UNEXPECTED(next_op_num >= CG(context).opcodes_size)) { |
162 | 506k | CG(context).opcodes_size *= 4; |
163 | 506k | op_array->opcodes = erealloc(op_array->opcodes, CG(context).opcodes_size * sizeof(zend_op)); |
164 | 506k | } |
165 | | |
166 | 62.2M | next_op = &(op_array->opcodes[next_op_num]); |
167 | | |
168 | 62.2M | init_op(next_op); |
169 | | |
170 | 62.2M | return next_op; |
171 | 62.2M | } |
172 | | |
173 | | static zend_brk_cont_element *get_next_brk_cont_element(void) |
174 | 42.1k | { |
175 | 42.1k | CG(context).last_brk_cont++; |
176 | 42.1k | CG(context).brk_cont_array = erealloc(CG(context).brk_cont_array, sizeof(zend_brk_cont_element) * CG(context).last_brk_cont); |
177 | 42.1k | return &CG(context).brk_cont_array[CG(context).last_brk_cont-1]; |
178 | 42.1k | } |
179 | | |
180 | | static zend_string *zend_build_runtime_definition_key(zend_string *name, uint32_t start_lineno) /* {{{ */ |
181 | 138k | { |
182 | 138k | zend_string *filename = CG(active_op_array)->filename; |
183 | 138k | zend_string *result = zend_strpprintf(0, "%c%s%s:%" PRIu32 "$%" PRIx32, |
184 | 138k | '\0', ZSTR_VAL(name), ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
185 | 138k | return zend_new_interned_string(result); |
186 | 138k | } |
187 | | /* }}} */ |
188 | | |
189 | | static bool zend_get_unqualified_name(const zend_string *name, const char **result, size_t *result_len) /* {{{ */ |
190 | 18.3M | { |
191 | 18.3M | const char *ns_separator = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
192 | 18.3M | if (ns_separator != NULL) { |
193 | 17.7M | *result = ns_separator + 1; |
194 | 17.7M | *result_len = ZSTR_VAL(name) + ZSTR_LEN(name) - *result; |
195 | 17.7M | return true; |
196 | 17.7M | } |
197 | | |
198 | 579k | return false; |
199 | 18.3M | } |
200 | | /* }}} */ |
201 | | |
202 | | struct reserved_class_name { |
203 | | const char *name; |
204 | | size_t len; |
205 | | }; |
206 | | static const struct reserved_class_name reserved_class_names[] = { |
207 | | {ZEND_STRL("bool")}, |
208 | | {ZEND_STRL("false")}, |
209 | | {ZEND_STRL("float")}, |
210 | | {ZEND_STRL("int")}, |
211 | | {ZEND_STRL("null")}, |
212 | | {ZEND_STRL("parent")}, |
213 | | {ZEND_STRL("self")}, |
214 | | {ZEND_STRL("static")}, |
215 | | {ZEND_STRL("string")}, |
216 | | {ZEND_STRL("true")}, |
217 | | {ZEND_STRL("void")}, |
218 | | {ZEND_STRL("never")}, |
219 | | {ZEND_STRL("iterable")}, |
220 | | {ZEND_STRL("object")}, |
221 | | {ZEND_STRL("mixed")}, |
222 | | /* These are not usable as class names because they're proper tokens, |
223 | | * but they are here for class aliases. */ |
224 | | {ZEND_STRL("array")}, |
225 | | {ZEND_STRL("callable")}, |
226 | | {NULL, 0} |
227 | | }; |
228 | | |
229 | | static bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */ |
230 | 573k | { |
231 | 573k | const struct reserved_class_name *reserved = reserved_class_names; |
232 | | |
233 | 573k | const char *uqname = ZSTR_VAL(name); |
234 | 573k | size_t uqname_len = ZSTR_LEN(name); |
235 | 573k | zend_get_unqualified_name(name, &uqname, &uqname_len); |
236 | | |
237 | 10.3M | for (; reserved->name; ++reserved) { |
238 | 9.75M | if (uqname_len == reserved->len |
239 | 280k | && zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0 |
240 | 9.75M | ) { |
241 | 97 | return true; |
242 | 97 | } |
243 | 9.75M | } |
244 | | |
245 | 573k | return false; |
246 | 573k | } |
247 | | /* }}} */ |
248 | | |
249 | | void zend_assert_valid_class_name(const zend_string *name, const char *type) /* {{{ */ |
250 | 572k | { |
251 | 572k | if (zend_is_reserved_class_name(name)) { |
252 | 72 | zend_error_noreturn(E_COMPILE_ERROR, |
253 | 72 | "Cannot use \"%s\" as %s as it is reserved", ZSTR_VAL(name), type); |
254 | 72 | } |
255 | 572k | if (zend_string_equals_literal(name, "_") || zend_string_ends_with_literal(name, "\\_")) { |
256 | 1.59k | zend_error(E_DEPRECATED, "Using \"_\" as %s is deprecated since 8.4", type); |
257 | 1.59k | } |
258 | 572k | } |
259 | | /* }}} */ |
260 | | |
261 | | typedef struct _builtin_type_info { |
262 | | const char* name; |
263 | | const size_t name_len; |
264 | | const uint8_t type; |
265 | | } builtin_type_info; |
266 | | |
267 | | static const builtin_type_info builtin_types[] = { |
268 | | {ZEND_STRL("null"), IS_NULL}, |
269 | | {ZEND_STRL("true"), IS_TRUE}, |
270 | | {ZEND_STRL("false"), IS_FALSE}, |
271 | | {ZEND_STRL("int"), IS_LONG}, |
272 | | {ZEND_STRL("float"), IS_DOUBLE}, |
273 | | {ZEND_STRL("string"), IS_STRING}, |
274 | | {ZEND_STRL("bool"), _IS_BOOL}, |
275 | | {ZEND_STRL("void"), IS_VOID}, |
276 | | {ZEND_STRL("never"), IS_NEVER}, |
277 | | {ZEND_STRL("iterable"), IS_ITERABLE}, |
278 | | {ZEND_STRL("object"), IS_OBJECT}, |
279 | | {ZEND_STRL("mixed"), IS_MIXED}, |
280 | | {NULL, 0, IS_UNDEF} |
281 | | }; |
282 | | |
283 | | typedef struct { |
284 | | const char *name; |
285 | | size_t name_len; |
286 | | const char *correct_name; |
287 | | } confusable_type_info; |
288 | | |
289 | | static const confusable_type_info confusable_types[] = { |
290 | | {ZEND_STRL("boolean"), "bool"}, |
291 | | {ZEND_STRL("integer"), "int"}, |
292 | | {ZEND_STRL("double"), "float"}, |
293 | | {ZEND_STRL("resource"), NULL}, |
294 | | {NULL, 0, NULL}, |
295 | | }; |
296 | | |
297 | | static zend_always_inline uint8_t zend_lookup_builtin_type_by_name(const zend_string *name) /* {{{ */ |
298 | 675k | { |
299 | 675k | const builtin_type_info *info = &builtin_types[0]; |
300 | | |
301 | 7.84M | for (; info->name; ++info) { |
302 | 7.43M | if (ZSTR_LEN(name) == info->name_len |
303 | 373k | && zend_binary_strcasecmp(ZSTR_VAL(name), ZSTR_LEN(name), info->name, info->name_len) == 0 |
304 | 7.43M | ) { |
305 | 273k | return info->type; |
306 | 273k | } |
307 | 7.43M | } |
308 | | |
309 | 402k | return 0; |
310 | 675k | } |
311 | | /* }}} */ |
312 | | |
313 | | static zend_always_inline bool zend_is_confusable_type(const zend_string *name, const char **correct_name) /* {{{ */ |
314 | 401k | { |
315 | 401k | const confusable_type_info *info = confusable_types; |
316 | | |
317 | | /* Intentionally using case-sensitive comparison here, because "integer" is likely intended |
318 | | * as a scalar type, while "Integer" is likely a class type. */ |
319 | 1.92M | for (; info->name; ++info) { |
320 | 1.57M | if (zend_string_equals_cstr(name, info->name, info->name_len)) { |
321 | 49.4k | *correct_name = info->correct_name; |
322 | 49.4k | return 1; |
323 | 49.4k | } |
324 | 1.57M | } |
325 | | |
326 | 351k | return 0; |
327 | 401k | } |
328 | | /* }}} */ |
329 | | |
330 | 49.4k | static bool zend_is_not_imported(zend_string *name) { |
331 | | /* Assuming "name" is unqualified here. */ |
332 | 49.4k | return !FC(imports) || zend_hash_find_ptr_lc(FC(imports), name) == NULL; |
333 | 49.4k | } |
334 | | |
335 | | void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array) /* {{{ */ |
336 | 1.57M | { |
337 | 1.57M | *prev_context = CG(context); |
338 | 1.57M | CG(context).prev = CG(context).op_array ? prev_context : NULL; |
339 | 1.57M | CG(context).op_array = op_array; |
340 | 1.57M | CG(context).opcodes_size = INITIAL_OP_ARRAY_SIZE; |
341 | 1.57M | CG(context).vars_size = 0; |
342 | 1.57M | CG(context).literals_size = 0; |
343 | 1.57M | CG(context).fast_call_var = -1; |
344 | 1.57M | CG(context).try_catch_offset = -1; |
345 | 1.57M | CG(context).current_brk_cont = -1; |
346 | 1.57M | CG(context).last_brk_cont = 0; |
347 | 1.57M | CG(context).has_assigned_to_http_response_header = false; |
348 | 1.57M | CG(context).brk_cont_array = NULL; |
349 | 1.57M | CG(context).labels = NULL; |
350 | 1.57M | CG(context).in_jmp_frameless_branch = false; |
351 | 1.57M | CG(context).in_finally = false; |
352 | 1.57M | CG(context).active_property_info_name = NULL; |
353 | 1.57M | CG(context).active_property_hook_kind = (zend_property_hook_kind)-1; |
354 | 1.57M | } |
355 | | /* }}} */ |
356 | | |
357 | | void zend_oparray_context_end(const zend_oparray_context *prev_context) /* {{{ */ |
358 | 1.56M | { |
359 | 1.56M | if (CG(context).brk_cont_array) { |
360 | 28.3k | efree(CG(context).brk_cont_array); |
361 | 28.3k | CG(context).brk_cont_array = NULL; |
362 | 28.3k | } |
363 | 1.56M | if (CG(context).labels) { |
364 | 2.09k | zend_hash_destroy(CG(context).labels); |
365 | 2.09k | FREE_HASHTABLE(CG(context).labels); |
366 | 2.09k | CG(context).labels = NULL; |
367 | 2.09k | } |
368 | 1.56M | CG(context) = *prev_context; |
369 | 1.56M | } |
370 | | /* }}} */ |
371 | | |
372 | | static void zend_reset_import_tables(void) /* {{{ */ |
373 | 98.0k | { |
374 | 98.0k | if (FC(imports)) { |
375 | 707 | zend_hash_destroy(FC(imports)); |
376 | 707 | efree(FC(imports)); |
377 | 707 | FC(imports) = NULL; |
378 | 707 | } |
379 | | |
380 | 98.0k | if (FC(imports_function)) { |
381 | 325 | zend_hash_destroy(FC(imports_function)); |
382 | 325 | efree(FC(imports_function)); |
383 | 325 | FC(imports_function) = NULL; |
384 | 325 | } |
385 | | |
386 | 98.0k | if (FC(imports_const)) { |
387 | 221 | zend_hash_destroy(FC(imports_const)); |
388 | 221 | efree(FC(imports_const)); |
389 | 221 | FC(imports_const) = NULL; |
390 | 221 | } |
391 | | |
392 | 98.0k | zend_hash_clean(&FC(seen_symbols)); |
393 | 98.0k | } |
394 | | /* }}} */ |
395 | | |
396 | 93.1k | static void zend_end_namespace(void) /* {{{ */ { |
397 | 93.1k | FC(in_namespace) = 0; |
398 | 93.1k | zend_reset_import_tables(); |
399 | 93.1k | if (FC(current_namespace)) { |
400 | 3.12k | zend_string_release_ex(FC(current_namespace), 0); |
401 | 3.12k | FC(current_namespace) = NULL; |
402 | 3.12k | } |
403 | 93.1k | } |
404 | | /* }}} */ |
405 | | |
406 | | void zend_file_context_begin(zend_file_context *prev_context) /* {{{ */ |
407 | 96.5k | { |
408 | 96.5k | *prev_context = CG(file_context); |
409 | 96.5k | FC(imports) = NULL; |
410 | 96.5k | FC(imports_function) = NULL; |
411 | 96.5k | FC(imports_const) = NULL; |
412 | 96.5k | FC(current_namespace) = NULL; |
413 | 96.5k | FC(in_namespace) = 0; |
414 | 96.5k | FC(has_bracketed_namespaces) = 0; |
415 | 96.5k | FC(declarables).ticks = 0; |
416 | 96.5k | zend_hash_init(&FC(seen_symbols), 8, NULL, NULL, 0); |
417 | 96.5k | } |
418 | | /* }}} */ |
419 | | |
420 | | void zend_file_context_end(const zend_file_context *prev_context) /* {{{ */ |
421 | 91.1k | { |
422 | 91.1k | zend_end_namespace(); |
423 | 91.1k | zend_hash_destroy(&FC(seen_symbols)); |
424 | 91.1k | CG(file_context) = *prev_context; |
425 | 91.1k | } |
426 | | /* }}} */ |
427 | | |
428 | | void zend_init_compiler_data_structures(void) /* {{{ */ |
429 | 300k | { |
430 | 300k | zend_stack_init(&CG(loop_var_stack), sizeof(zend_loop_var)); |
431 | 300k | zend_stack_init(&CG(delayed_oplines_stack), sizeof(zend_op)); |
432 | 300k | zend_stack_init(&CG(short_circuiting_opnums), sizeof(uint32_t)); |
433 | 300k | CG(active_class_entry) = NULL; |
434 | 300k | CG(in_compilation) = 0; |
435 | 300k | CG(skip_shebang) = 0; |
436 | | |
437 | 300k | CG(encoding_declared) = 0; |
438 | 300k | CG(memoized_exprs) = NULL; |
439 | 300k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
440 | 300k | } |
441 | | /* }}} */ |
442 | | |
443 | 1.54M | static void zend_register_seen_symbol(zend_string *name, uint32_t kind) { |
444 | 1.54M | zval *zv = zend_hash_find(&FC(seen_symbols), name); |
445 | 1.54M | if (zv) { |
446 | 1.46M | Z_LVAL_P(zv) |= kind; |
447 | 1.46M | } else { |
448 | 89.5k | zval tmp; |
449 | 89.5k | ZVAL_LONG(&tmp, kind); |
450 | 89.5k | zend_hash_add_new(&FC(seen_symbols), name, &tmp); |
451 | 89.5k | } |
452 | 1.54M | } |
453 | | |
454 | 2.23k | static bool zend_have_seen_symbol(zend_string *name, uint32_t kind) { |
455 | 2.23k | const zval *zv = zend_hash_find(&FC(seen_symbols), name); |
456 | 2.23k | return zv && (Z_LVAL_P(zv) & kind) != 0; |
457 | 2.23k | } |
458 | | |
459 | | void init_compiler(void) /* {{{ */ |
460 | 295k | { |
461 | 295k | CG(arena) = zend_arena_create(64 * 1024); |
462 | 295k | CG(active_op_array) = NULL; |
463 | 295k | memset(&CG(context), 0, sizeof(CG(context))); |
464 | 295k | zend_init_compiler_data_structures(); |
465 | 295k | zend_init_rsrc_list(); |
466 | 295k | zend_stream_init(); |
467 | 295k | CG(unclean_shutdown) = 0; |
468 | | |
469 | 295k | CG(delayed_variance_obligations) = NULL; |
470 | 295k | CG(delayed_autoloads) = NULL; |
471 | 295k | CG(unlinked_uses) = NULL; |
472 | 295k | CG(current_linking_class) = NULL; |
473 | 295k | } |
474 | | /* }}} */ |
475 | | |
476 | | void shutdown_compiler(void) /* {{{ */ |
477 | 300k | { |
478 | | /* Reset filename before destroying the arena, as file cache may use arena allocated strings. */ |
479 | 300k | zend_restore_compiled_filename(NULL); |
480 | | |
481 | 300k | zend_stack_destroy(&CG(loop_var_stack)); |
482 | 300k | zend_stack_destroy(&CG(delayed_oplines_stack)); |
483 | 300k | zend_stack_destroy(&CG(short_circuiting_opnums)); |
484 | | |
485 | 300k | if (CG(delayed_variance_obligations)) { |
486 | 306 | zend_hash_destroy(CG(delayed_variance_obligations)); |
487 | 306 | FREE_HASHTABLE(CG(delayed_variance_obligations)); |
488 | 306 | CG(delayed_variance_obligations) = NULL; |
489 | 306 | } |
490 | 300k | if (CG(delayed_autoloads)) { |
491 | 313 | zend_hash_destroy(CG(delayed_autoloads)); |
492 | 313 | FREE_HASHTABLE(CG(delayed_autoloads)); |
493 | 313 | CG(delayed_autoloads) = NULL; |
494 | 313 | } |
495 | 300k | if (CG(unlinked_uses)) { |
496 | 232 | zend_hash_destroy(CG(unlinked_uses)); |
497 | 232 | FREE_HASHTABLE(CG(unlinked_uses)); |
498 | 232 | CG(unlinked_uses) = NULL; |
499 | 232 | } |
500 | 300k | CG(current_linking_class) = NULL; |
501 | 300k | } |
502 | | /* }}} */ |
503 | | |
504 | | ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename) /* {{{ */ |
505 | 157k | { |
506 | 157k | CG(compiled_filename) = zend_string_copy(new_compiled_filename); |
507 | 157k | return new_compiled_filename; |
508 | 157k | } |
509 | | /* }}} */ |
510 | | |
511 | | ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename) /* {{{ */ |
512 | 605k | { |
513 | 605k | if (CG(compiled_filename)) { |
514 | 157k | zend_string_release(CG(compiled_filename)); |
515 | 157k | CG(compiled_filename) = NULL; |
516 | 157k | } |
517 | 605k | CG(compiled_filename) = original_compiled_filename; |
518 | 605k | } |
519 | | /* }}} */ |
520 | | |
521 | | ZEND_API zend_string *zend_get_compiled_filename(void) /* {{{ */ |
522 | 2.26M | { |
523 | 2.26M | return CG(compiled_filename); |
524 | 2.26M | } |
525 | | /* }}} */ |
526 | | |
527 | | ZEND_API uint32_t zend_get_compiled_lineno(void) /* {{{ */ |
528 | 349k | { |
529 | 349k | return CG(zend_lineno); |
530 | 349k | } |
531 | | /* }}} */ |
532 | | |
533 | | ZEND_API bool zend_is_compiling(void) /* {{{ */ |
534 | 3.63M | { |
535 | 3.63M | return CG(in_compilation); |
536 | 3.63M | } |
537 | | /* }}} */ |
538 | | |
539 | | static bool zend_is_constructor(const zend_string *name) /* {{{ */ |
540 | 30.9k | { |
541 | 30.9k | return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME); |
542 | 30.9k | } |
543 | | /* }}} */ |
544 | | |
545 | | static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */ |
546 | 31.9M | { |
547 | 31.9M | return (uint32_t)CG(active_op_array)->T++; |
548 | 31.9M | } |
549 | | /* }}} */ |
550 | | |
551 | 2.51M | static uint32_t lookup_cv(zend_string *name) /* {{{ */{ |
552 | 2.51M | zend_op_array *op_array = CG(active_op_array); |
553 | 2.51M | int i = 0; |
554 | 2.51M | zend_ulong hash_value = zend_string_hash_val(name); |
555 | | |
556 | 7.06M | while (i < op_array->last_var) { |
557 | 5.29M | if (ZSTR_H(op_array->vars[i]) == hash_value |
558 | 745k | && zend_string_equals(op_array->vars[i], name)) { |
559 | 745k | return EX_NUM_TO_VAR(i); |
560 | 745k | } |
561 | 4.55M | i++; |
562 | 4.55M | } |
563 | 1.76M | i = op_array->last_var; |
564 | 1.76M | op_array->last_var++; |
565 | 1.76M | if (op_array->last_var > CG(context).vars_size) { |
566 | 1.24M | CG(context).vars_size += 16; /* FIXME */ |
567 | 1.24M | op_array->vars = erealloc(op_array->vars, CG(context).vars_size * sizeof(zend_string*)); |
568 | 1.24M | } |
569 | | |
570 | 1.76M | op_array->vars[i] = zend_string_copy(name); |
571 | 1.76M | return EX_NUM_TO_VAR(i); |
572 | 2.51M | } |
573 | | /* }}} */ |
574 | | |
575 | | zend_string *zval_make_interned_string(zval *zv) |
576 | 38.2M | { |
577 | 38.2M | ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING); |
578 | 38.2M | Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv)); |
579 | 38.2M | if (ZSTR_IS_INTERNED(Z_STR_P(zv))) { |
580 | 5.83M | Z_TYPE_FLAGS_P(zv) = 0; |
581 | 5.83M | } |
582 | 38.2M | return Z_STR_P(zv); |
583 | 38.2M | } |
584 | | |
585 | | /* Common part of zend_add_literal and zend_append_individual_literal */ |
586 | | static inline void zend_insert_literal(const zend_op_array *op_array, zval *zv, int literal_position) /* {{{ */ |
587 | 38.4M | { |
588 | 38.4M | zval *lit = CT_CONSTANT_EX(op_array, literal_position); |
589 | 38.4M | if (Z_TYPE_P(zv) == IS_STRING) { |
590 | 35.7M | zval_make_interned_string(zv); |
591 | 35.7M | } |
592 | 38.4M | ZVAL_COPY_VALUE(lit, zv); |
593 | 38.4M | Z_EXTRA_P(lit) = 0; |
594 | 38.4M | } |
595 | | /* }}} */ |
596 | | |
597 | | /* Is used while compiling a function, using the context to keep track |
598 | | of an approximate size to avoid to relocate to often. |
599 | | Literals are truncated to actual size in the second compiler pass (pass_two()). */ |
600 | | static int zend_add_literal(zval *zv) /* {{{ */ |
601 | 38.4M | { |
602 | 38.4M | zend_op_array *op_array = CG(active_op_array); |
603 | 38.4M | uint32_t i = op_array->last_literal; |
604 | 38.4M | op_array->last_literal++; |
605 | 38.4M | if (i >= CG(context).literals_size) { |
606 | 6.22M | while (i >= CG(context).literals_size) { |
607 | 3.11M | CG(context).literals_size += 16; /* FIXME */ |
608 | 3.11M | } |
609 | 3.11M | op_array->literals = (zval*)erealloc(op_array->literals, CG(context).literals_size * sizeof(zval)); |
610 | 3.11M | } |
611 | 38.4M | zend_insert_literal(op_array, zv, i); |
612 | 38.4M | return i; |
613 | 38.4M | } |
614 | | /* }}} */ |
615 | | |
616 | | static inline int zend_add_literal_string(zend_string **str) /* {{{ */ |
617 | 31.5M | { |
618 | 31.5M | int ret; |
619 | 31.5M | zval zv; |
620 | 31.5M | ZVAL_STR(&zv, *str); |
621 | 31.5M | ret = zend_add_literal(&zv); |
622 | 31.5M | *str = Z_STR(zv); |
623 | 31.5M | return ret; |
624 | 31.5M | } |
625 | | /* }}} */ |
626 | | |
627 | | static int zend_add_func_name_literal(zend_string *name) /* {{{ */ |
628 | 202k | { |
629 | | /* Original name */ |
630 | 202k | int ret = zend_add_literal_string(&name); |
631 | | |
632 | | /* Lowercased name */ |
633 | 202k | zend_string *lc_name = zend_string_tolower(name); |
634 | 202k | zend_add_literal_string(&lc_name); |
635 | | |
636 | 202k | return ret; |
637 | 202k | } |
638 | | /* }}} */ |
639 | | |
640 | | static int zend_add_ns_func_name_literal(zend_string *name) /* {{{ */ |
641 | 2.79M | { |
642 | 2.79M | const char *unqualified_name; |
643 | 2.79M | size_t unqualified_name_len; |
644 | | |
645 | | /* Original name */ |
646 | 2.79M | int ret = zend_add_literal_string(&name); |
647 | | |
648 | | /* Lowercased name */ |
649 | 2.79M | zend_string *lc_name = zend_string_tolower(name); |
650 | 2.79M | zend_add_literal_string(&lc_name); |
651 | | |
652 | | /* Lowercased unqualified name */ |
653 | 2.79M | if (zend_get_unqualified_name(name, &unqualified_name, &unqualified_name_len)) { |
654 | 2.79M | lc_name = zend_string_alloc(unqualified_name_len, 0); |
655 | 2.79M | zend_str_tolower_copy(ZSTR_VAL(lc_name), unqualified_name, unqualified_name_len); |
656 | 2.79M | zend_add_literal_string(&lc_name); |
657 | 2.79M | } |
658 | | |
659 | 2.79M | return ret; |
660 | 2.79M | } |
661 | | /* }}} */ |
662 | | |
663 | | static int zend_add_class_name_literal(zend_string *name) /* {{{ */ |
664 | 164k | { |
665 | | /* Original name */ |
666 | 164k | int ret = zend_add_literal_string(&name); |
667 | | |
668 | | /* Lowercased name */ |
669 | 164k | zend_string *lc_name = zend_string_tolower(name); |
670 | 164k | zend_add_literal_string(&lc_name); |
671 | | |
672 | 164k | return ret; |
673 | 164k | } |
674 | | /* }}} */ |
675 | | |
676 | | static int zend_add_const_name_literal(zend_string *name, bool unqualified) /* {{{ */ |
677 | 7.40M | { |
678 | 7.40M | zend_string *tmp_name; |
679 | | |
680 | 7.40M | int ret = zend_add_literal_string(&name); |
681 | | |
682 | 7.40M | const char *after_ns; |
683 | 7.40M | size_t after_ns_len; |
684 | 7.40M | if (zend_get_unqualified_name(name, &after_ns, &after_ns_len)) { |
685 | 7.26M | size_t ns_len = after_ns - ZSTR_VAL(name) - 1; |
686 | | |
687 | | /* lowercased namespace name & original constant name */ |
688 | 7.26M | tmp_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), 0); |
689 | 7.26M | zend_str_tolower(ZSTR_VAL(tmp_name), ns_len); |
690 | 7.26M | zend_add_literal_string(&tmp_name); |
691 | | |
692 | 7.26M | if (!unqualified) { |
693 | 3.36k | return ret; |
694 | 3.36k | } |
695 | 7.26M | } else { |
696 | 142k | after_ns = ZSTR_VAL(name); |
697 | 142k | after_ns_len = ZSTR_LEN(name); |
698 | 142k | } |
699 | | |
700 | | /* original unqualified constant name */ |
701 | 7.40M | tmp_name = zend_string_init(after_ns, after_ns_len, 0); |
702 | 7.40M | zend_add_literal_string(&tmp_name); |
703 | | |
704 | 7.40M | return ret; |
705 | 7.40M | } |
706 | | /* }}} */ |
707 | | |
708 | 51.2k | #define LITERAL_STR(op, str) do { \ |
709 | 51.2k | zval _c; \ |
710 | 51.2k | ZVAL_STR(&_c, str); \ |
711 | 51.2k | op.constant = zend_add_literal(&_c); \ |
712 | 51.2k | } while (0) |
713 | | |
714 | | void zend_stop_lexing(void) |
715 | 141 | { |
716 | 141 | if (LANG_SCNG(on_event)) { |
717 | 0 | LANG_SCNG(on_event)(ON_STOP, END, 0, NULL, 0, LANG_SCNG(on_event_context)); |
718 | 0 | } |
719 | | |
720 | 141 | LANG_SCNG(yy_cursor) = LANG_SCNG(yy_limit); |
721 | 141 | } |
722 | | |
723 | | static inline void zend_begin_loop( |
724 | | uint8_t free_opcode, const znode *loop_var, bool is_switch) /* {{{ */ |
725 | 42.1k | { |
726 | 42.1k | zend_brk_cont_element *brk_cont_element; |
727 | 42.1k | int parent = CG(context).current_brk_cont; |
728 | 42.1k | zend_loop_var info = {0}; |
729 | | |
730 | 42.1k | CG(context).current_brk_cont = CG(context).last_brk_cont; |
731 | 42.1k | brk_cont_element = get_next_brk_cont_element(); |
732 | 42.1k | brk_cont_element->parent = parent; |
733 | 42.1k | brk_cont_element->is_switch = is_switch; |
734 | | |
735 | 42.1k | if (loop_var && (loop_var->op_type & (IS_VAR|IS_TMP_VAR))) { |
736 | 20.5k | uint32_t start = get_next_op_number(); |
737 | | |
738 | 20.5k | info.opcode = free_opcode; |
739 | 20.5k | info.var_type = loop_var->op_type; |
740 | 20.5k | info.var_num = loop_var->u.op.var; |
741 | 20.5k | brk_cont_element->start = start; |
742 | 21.6k | } else { |
743 | 21.6k | info.opcode = ZEND_NOP; |
744 | | /* The start field is used to free temporary variables in case of exceptions. |
745 | | * We won't try to free something of we don't have loop variable. */ |
746 | 21.6k | brk_cont_element->start = -1; |
747 | 21.6k | } |
748 | | |
749 | 42.1k | zend_stack_push(&CG(loop_var_stack), &info); |
750 | 42.1k | } |
751 | | /* }}} */ |
752 | | |
753 | | static inline void zend_end_loop(int cont_addr, const znode *var_node) /* {{{ */ |
754 | 41.9k | { |
755 | 41.9k | uint32_t end = get_next_op_number(); |
756 | 41.9k | zend_brk_cont_element *brk_cont_element |
757 | 41.9k | = &CG(context).brk_cont_array[CG(context).current_brk_cont]; |
758 | 41.9k | brk_cont_element->cont = cont_addr; |
759 | 41.9k | brk_cont_element->brk = end; |
760 | 41.9k | CG(context).current_brk_cont = brk_cont_element->parent; |
761 | | |
762 | 41.9k | zend_stack_del_top(&CG(loop_var_stack)); |
763 | 41.9k | } |
764 | | /* }}} */ |
765 | | |
766 | | bool zend_op_may_elide_result(uint8_t opcode) |
767 | 3.21M | { |
768 | 3.21M | switch (opcode) { |
769 | 138k | case ZEND_ASSIGN: |
770 | 151k | case ZEND_ASSIGN_DIM: |
771 | 167k | case ZEND_ASSIGN_OBJ: |
772 | 168k | case ZEND_ASSIGN_STATIC_PROP: |
773 | 197k | case ZEND_ASSIGN_OP: |
774 | 206k | case ZEND_ASSIGN_DIM_OP: |
775 | 207k | case ZEND_ASSIGN_OBJ_OP: |
776 | 208k | case ZEND_ASSIGN_STATIC_PROP_OP: |
777 | 208k | case ZEND_PRE_INC_STATIC_PROP: |
778 | 208k | case ZEND_PRE_DEC_STATIC_PROP: |
779 | 208k | case ZEND_PRE_INC_OBJ: |
780 | 208k | case ZEND_PRE_DEC_OBJ: |
781 | 209k | case ZEND_PRE_INC: |
782 | 209k | case ZEND_PRE_DEC: |
783 | 347k | case ZEND_DO_FCALL: |
784 | 356k | case ZEND_DO_ICALL: |
785 | 365k | case ZEND_DO_UCALL: |
786 | 1.09M | case ZEND_DO_FCALL_BY_NAME: |
787 | 1.10M | case ZEND_YIELD: |
788 | 1.10M | case ZEND_YIELD_FROM: |
789 | 1.10M | case ZEND_INCLUDE_OR_EVAL: |
790 | 1.10M | return true; |
791 | 2.10M | default: |
792 | 2.10M | return false; |
793 | 3.21M | } |
794 | 3.21M | } |
795 | | |
796 | | static void zend_do_free(znode *op1) /* {{{ */ |
797 | 3.52M | { |
798 | 3.52M | if (op1->op_type == IS_TMP_VAR) { |
799 | 3.26M | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
800 | | |
801 | 3.42M | while (opline->opcode == ZEND_END_SILENCE || |
802 | 3.41M | opline->opcode == ZEND_OP_DATA) { |
803 | 161k | opline--; |
804 | 161k | } |
805 | | |
806 | 3.26M | if (opline->result_type == IS_TMP_VAR && opline->result.var == op1->u.op.var) { |
807 | 3.18M | switch (opline->opcode) { |
808 | 2.08k | case ZEND_BOOL: |
809 | 7.99k | case ZEND_BOOL_NOT: |
810 | | /* boolean results don't have to be freed */ |
811 | 7.99k | return; |
812 | 80 | case ZEND_POST_INC_STATIC_PROP: |
813 | 135 | case ZEND_POST_DEC_STATIC_PROP: |
814 | 704 | case ZEND_POST_INC_OBJ: |
815 | 854 | case ZEND_POST_DEC_OBJ: |
816 | 8.45k | case ZEND_POST_INC: |
817 | 9.04k | case ZEND_POST_DEC: |
818 | | /* convert $i++ to ++$i */ |
819 | 9.04k | opline->opcode -= 2; |
820 | 9.04k | SET_UNUSED(opline->result); |
821 | 9.04k | return; |
822 | 3.17M | default: |
823 | 3.17M | if (zend_op_may_elide_result(opline->opcode)) { |
824 | 1.10M | SET_UNUSED(opline->result); |
825 | 1.10M | return; |
826 | 1.10M | } |
827 | 2.06M | break; |
828 | 3.18M | } |
829 | 3.18M | } |
830 | | |
831 | 2.14M | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
832 | 2.14M | } else if (op1->op_type == IS_VAR) { |
833 | 116k | zend_op *opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
834 | 117k | while (opline->opcode == ZEND_END_SILENCE || |
835 | 117k | opline->opcode == ZEND_EXT_FCALL_END || |
836 | 117k | opline->opcode == ZEND_OP_DATA) { |
837 | 252 | opline--; |
838 | 252 | } |
839 | 116k | if (opline->result_type == IS_VAR |
840 | 114k | && opline->result.var == op1->u.op.var) { |
841 | 114k | if (opline->opcode == ZEND_FETCH_THIS) { |
842 | 0 | opline->opcode = ZEND_NOP; |
843 | 0 | } |
844 | 114k | if (!ZEND_OP_IS_FRAMELESS_ICALL(opline->opcode)) { |
845 | 114k | SET_UNUSED(opline->result); |
846 | 114k | } else { |
847 | | /* Frameless calls usually use the return value, so always emit a free. This should be |
848 | | * faster than checking RETURN_VALUE_USED inside the handler. */ |
849 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
850 | 0 | } |
851 | 114k | } else { |
852 | 9.15k | while (opline >= CG(active_op_array)->opcodes) { |
853 | 9.15k | if ((opline->opcode == ZEND_FETCH_LIST_R || |
854 | 9.03k | opline->opcode == ZEND_FETCH_LIST_W || |
855 | 6.49k | opline->opcode == ZEND_EXT_STMT) && |
856 | 2.66k | opline->op1_type == IS_VAR && |
857 | 2.64k | opline->op1.var == op1->u.op.var) { |
858 | 1.93k | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
859 | 1.93k | return; |
860 | 1.93k | } |
861 | 7.22k | if (opline->result_type == IS_VAR |
862 | 4.44k | && opline->result.var == op1->u.op.var) { |
863 | 0 | if (opline->opcode == ZEND_NEW) { |
864 | 0 | zend_emit_op(NULL, ZEND_FREE, op1, NULL); |
865 | 0 | } |
866 | 0 | break; |
867 | 0 | } |
868 | 7.22k | opline--; |
869 | 7.22k | } |
870 | 1.93k | } |
871 | 147k | } else if (op1->op_type == IS_CONST) { |
872 | | /* Destroy value without using GC: When opcache moves arrays into SHM it will |
873 | | * free the zend_array structure, so references to it from outside the op array |
874 | | * become invalid. GC would cause such a reference in the root buffer. */ |
875 | 138k | zval_ptr_dtor_nogc(&op1->u.constant); |
876 | 138k | } |
877 | 3.52M | } |
878 | | /* }}} */ |
879 | | |
880 | | |
881 | | static const char *zend_modifier_token_to_string(uint32_t token) |
882 | 85 | { |
883 | 85 | switch (token) { |
884 | 7 | case T_PUBLIC: |
885 | 7 | return "public"; |
886 | 5 | case T_PROTECTED: |
887 | 5 | return "protected"; |
888 | 7 | case T_PRIVATE: |
889 | 7 | return "private"; |
890 | 17 | case T_STATIC: |
891 | 17 | return "static"; |
892 | 11 | case T_FINAL: |
893 | 11 | return "final"; |
894 | 24 | case T_READONLY: |
895 | 24 | return "readonly"; |
896 | 12 | case T_ABSTRACT: |
897 | 12 | return "abstract"; |
898 | 1 | case T_PUBLIC_SET: |
899 | 1 | return "public(set)"; |
900 | 0 | case T_PROTECTED_SET: |
901 | 0 | return "protected(set)"; |
902 | 1 | case T_PRIVATE_SET: |
903 | 1 | return "private(set)"; |
904 | 0 | default: ZEND_UNREACHABLE(); |
905 | 85 | } |
906 | 85 | } |
907 | | |
908 | | uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t token) |
909 | 70.7k | { |
910 | 70.7k | switch (token) { |
911 | 45.5k | case T_PUBLIC: |
912 | 45.5k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
913 | 45.5k | return ZEND_ACC_PUBLIC; |
914 | 45.5k | } |
915 | 7 | break; |
916 | 2.77k | case T_PROTECTED: |
917 | 2.77k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
918 | 2.76k | return ZEND_ACC_PROTECTED; |
919 | 2.76k | } |
920 | 5 | break; |
921 | 6.64k | case T_PRIVATE: |
922 | 6.64k | if (target != ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
923 | 6.63k | return ZEND_ACC_PRIVATE; |
924 | 6.63k | } |
925 | 7 | break; |
926 | 1.10k | case T_READONLY: |
927 | 1.10k | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
928 | 1.09k | return ZEND_ACC_READONLY; |
929 | 1.09k | } |
930 | 15 | break; |
931 | 1.18k | case T_ABSTRACT: |
932 | 1.18k | if (target == ZEND_MODIFIER_TARGET_METHOD || target == ZEND_MODIFIER_TARGET_PROPERTY) { |
933 | 1.17k | return ZEND_ACC_ABSTRACT; |
934 | 1.17k | } |
935 | 6 | break; |
936 | 1.62k | case T_FINAL: |
937 | 1.62k | return ZEND_ACC_FINAL; |
938 | 10.8k | case T_STATIC: |
939 | 10.8k | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_METHOD) { |
940 | 10.8k | return ZEND_ACC_STATIC; |
941 | 10.8k | } |
942 | 11 | break; |
943 | 224 | case T_PUBLIC_SET: |
944 | 224 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
945 | 223 | return ZEND_ACC_PUBLIC_SET; |
946 | 223 | } |
947 | 1 | break; |
948 | 338 | case T_PROTECTED_SET: |
949 | 338 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
950 | 338 | return ZEND_ACC_PROTECTED_SET; |
951 | 338 | } |
952 | 0 | break; |
953 | 372 | case T_PRIVATE_SET: |
954 | 372 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
955 | 371 | return ZEND_ACC_PRIVATE_SET; |
956 | 371 | } |
957 | 1 | break; |
958 | 70.7k | } |
959 | | |
960 | 53 | char *member; |
961 | 53 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
962 | 0 | member = "property"; |
963 | 53 | } else if (target == ZEND_MODIFIER_TARGET_METHOD) { |
964 | 12 | member = "method"; |
965 | 41 | } else if (target == ZEND_MODIFIER_TARGET_CONSTANT) { |
966 | 8 | member = "class constant"; |
967 | 33 | } else if (target == ZEND_MODIFIER_TARGET_CPP) { |
968 | 6 | member = "parameter"; |
969 | 27 | } else if (target == ZEND_MODIFIER_TARGET_PROPERTY_HOOK) { |
970 | 27 | member = "property hook"; |
971 | 27 | } else { |
972 | 0 | ZEND_UNREACHABLE(); |
973 | 0 | } |
974 | | |
975 | 53 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
976 | 53 | "Cannot use the %s modifier on a %s", zend_modifier_token_to_string(token), member); |
977 | 53 | return 0; |
978 | 53 | } |
979 | | |
980 | | uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers) |
981 | 59.7k | { |
982 | 59.7k | uint32_t flags = 0; |
983 | 59.7k | const zend_ast_list *modifier_list = zend_ast_get_list(modifiers); |
984 | | |
985 | 129k | for (uint32_t i = 0; i < modifier_list->children; i++) { |
986 | 69.7k | uint32_t token = (uint32_t) Z_LVAL_P(zend_ast_get_zval(modifier_list->child[i])); |
987 | 69.7k | uint32_t new_flag = zend_modifier_token_to_flag(target, token); |
988 | 69.7k | if (!new_flag) { |
989 | 47 | return 0; |
990 | 47 | } |
991 | | /* Don't error immediately for duplicate flags, we want to prioritize the errors from zend_add_member_modifier(). */ |
992 | 69.7k | bool duplicate_flag = (flags & new_flag); |
993 | 69.6k | flags = zend_add_member_modifier(flags, new_flag, target); |
994 | 69.6k | if (!flags) { |
995 | 74 | return 0; |
996 | 74 | } |
997 | 69.5k | if (duplicate_flag) { |
998 | 32 | zend_throw_exception_ex(zend_ce_compile_error, 0, |
999 | 32 | "Multiple %s modifiers are not allowed", zend_modifier_token_to_string(token)); |
1000 | 32 | return 0; |
1001 | 32 | } |
1002 | 69.5k | } |
1003 | | |
1004 | 59.6k | return flags; |
1005 | 59.7k | } |
1006 | | |
1007 | | uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */ |
1008 | 156 | { |
1009 | 156 | uint32_t new_flags = flags | new_flag; |
1010 | 156 | if ((flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) { |
1011 | 5 | zend_throw_exception(zend_ce_compile_error, |
1012 | 5 | "Multiple abstract modifiers are not allowed", 0); |
1013 | 5 | return 0; |
1014 | 5 | } |
1015 | 151 | if ((flags & ZEND_ACC_FINAL) && (new_flag & ZEND_ACC_FINAL)) { |
1016 | 5 | zend_throw_exception(zend_ce_compile_error, "Multiple final modifiers are not allowed", 0); |
1017 | 5 | return 0; |
1018 | 5 | } |
1019 | 146 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1020 | 6 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1021 | 6 | return 0; |
1022 | 6 | } |
1023 | 140 | if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) { |
1024 | 6 | zend_throw_exception(zend_ce_compile_error, |
1025 | 6 | "Cannot use the final modifier on an abstract class", 0); |
1026 | 6 | return 0; |
1027 | 6 | } |
1028 | 134 | return new_flags; |
1029 | 140 | } |
1030 | | /* }}} */ |
1031 | | |
1032 | | uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag) |
1033 | 122 | { |
1034 | 122 | uint32_t new_flags = flags | new_flag; |
1035 | 122 | if (new_flag & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) { |
1036 | 6 | zend_throw_exception(zend_ce_compile_error, |
1037 | 6 | "Cannot use the abstract modifier on an anonymous class", 0); |
1038 | 6 | return 0; |
1039 | 6 | } |
1040 | 116 | if (new_flag & ZEND_ACC_FINAL) { |
1041 | 5 | zend_throw_exception(zend_ce_compile_error, "Cannot use the final modifier on an anonymous class", 0); |
1042 | 5 | return 0; |
1043 | 5 | } |
1044 | 111 | if ((flags & ZEND_ACC_READONLY_CLASS) && (new_flag & ZEND_ACC_READONLY_CLASS)) { |
1045 | 5 | zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0); |
1046 | 5 | return 0; |
1047 | 5 | } |
1048 | 106 | return new_flags; |
1049 | 111 | } |
1050 | | |
1051 | | uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target) /* {{{ */ |
1052 | 69.6k | { |
1053 | 69.6k | uint32_t new_flags = flags | new_flag; |
1054 | 69.6k | if ((flags & ZEND_ACC_PPP_MASK) && (new_flag & ZEND_ACC_PPP_MASK)) { |
1055 | 53 | zend_throw_exception(zend_ce_compile_error, |
1056 | 53 | "Multiple access type modifiers are not allowed", 0); |
1057 | 53 | return 0; |
1058 | 53 | } |
1059 | 69.6k | if ((new_flags & ZEND_ACC_ABSTRACT) && (new_flags & ZEND_ACC_FINAL)) { |
1060 | 10 | if (target == ZEND_MODIFIER_TARGET_METHOD) { |
1061 | 5 | zend_throw_exception(zend_ce_compile_error, |
1062 | 5 | "Cannot use the final modifier on an abstract method", 0); |
1063 | 5 | return 0; |
1064 | 5 | } |
1065 | 5 | if (target == ZEND_MODIFIER_TARGET_PROPERTY) { |
1066 | 5 | zend_throw_exception(zend_ce_compile_error, |
1067 | 5 | "Cannot use the final modifier on an abstract property", 0); |
1068 | 5 | return 0; |
1069 | 5 | } |
1070 | 5 | } |
1071 | 69.5k | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
1072 | 34.6k | if ((flags & ZEND_ACC_PPP_SET_MASK) && (new_flag & ZEND_ACC_PPP_SET_MASK)) { |
1073 | 11 | zend_throw_exception(zend_ce_compile_error, |
1074 | 11 | "Multiple access type modifiers are not allowed", 0); |
1075 | 11 | return 0; |
1076 | 11 | } |
1077 | 34.6k | } |
1078 | 69.5k | return new_flags; |
1079 | 69.5k | } |
1080 | | /* }}} */ |
1081 | | |
1082 | 20.5k | ZEND_API zend_string *zend_create_member_string(const zend_string *class_name, const zend_string *member_name) { |
1083 | 20.5k | return zend_string_concat3( |
1084 | 20.5k | ZSTR_VAL(class_name), ZSTR_LEN(class_name), |
1085 | 20.5k | "::", sizeof("::") - 1, |
1086 | 20.5k | ZSTR_VAL(member_name), ZSTR_LEN(member_name)); |
1087 | 20.5k | } |
1088 | | |
1089 | 12.6M | static zend_string *zend_concat_names(const char *name1, size_t name1_len, const char *name2, size_t name2_len) { |
1090 | 12.6M | return zend_string_concat3(name1, name1_len, "\\", 1, name2, name2_len); |
1091 | 12.6M | } |
1092 | | |
1093 | 13.3M | static zend_string *zend_prefix_with_ns(zend_string *name) { |
1094 | 13.3M | if (FC(current_namespace)) { |
1095 | 12.6M | const zend_string *ns = FC(current_namespace); |
1096 | 12.6M | return zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
1097 | 12.6M | } else { |
1098 | 733k | return zend_string_copy(name); |
1099 | 733k | } |
1100 | 13.3M | } |
1101 | | |
1102 | | static zend_string *zend_resolve_non_class_name( |
1103 | | zend_string *name, uint32_t type, bool *is_fully_qualified, |
1104 | | bool case_sensitive, const HashTable *current_import_sub |
1105 | 10.6M | ) { |
1106 | 10.6M | const char *compound; |
1107 | 10.6M | *is_fully_qualified = false; |
1108 | | |
1109 | 10.6M | if (ZSTR_VAL(name)[0] == '\\') { |
1110 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1111 | 129 | *is_fully_qualified = true; |
1112 | 129 | return zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1113 | 129 | } |
1114 | | |
1115 | 10.6M | if (type == ZEND_NAME_FQ) { |
1116 | 56.6k | *is_fully_qualified = true; |
1117 | 56.6k | return zend_string_copy(name); |
1118 | 56.6k | } |
1119 | | |
1120 | 10.5M | if (type == ZEND_NAME_RELATIVE) { |
1121 | 962 | *is_fully_qualified = true; |
1122 | 962 | return zend_prefix_with_ns(name); |
1123 | 962 | } |
1124 | | |
1125 | 10.5M | if (current_import_sub) { |
1126 | | /* If an unqualified name is a function/const alias, replace it. */ |
1127 | 3.73k | zend_string *import_name; |
1128 | 3.73k | if (case_sensitive) { |
1129 | 1.21k | import_name = zend_hash_find_ptr(current_import_sub, name); |
1130 | 2.51k | } else { |
1131 | 2.51k | import_name = zend_hash_find_ptr_lc(current_import_sub, name); |
1132 | 2.51k | } |
1133 | | |
1134 | 3.73k | if (import_name) { |
1135 | 537 | *is_fully_qualified = true; |
1136 | 537 | return zend_string_copy(import_name); |
1137 | 537 | } |
1138 | 3.73k | } |
1139 | | |
1140 | 10.5M | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1141 | 10.5M | if (compound) { |
1142 | 6.55k | *is_fully_qualified = true; |
1143 | 6.55k | } |
1144 | | |
1145 | 10.5M | if (compound && FC(imports)) { |
1146 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1147 | 3.22k | size_t len = compound - ZSTR_VAL(name); |
1148 | 3.22k | const zend_string *import_name = zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1149 | | |
1150 | 3.22k | if (import_name) { |
1151 | 668 | return zend_concat_names( |
1152 | 668 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1153 | 668 | } |
1154 | 3.22k | } |
1155 | | |
1156 | 10.5M | return zend_prefix_with_ns(name); |
1157 | 10.5M | } |
1158 | | /* }}} */ |
1159 | | |
1160 | | static zend_string *zend_resolve_function_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1161 | 3.07M | { |
1162 | 3.07M | return zend_resolve_non_class_name( |
1163 | 3.07M | name, type, is_fully_qualified, false, FC(imports_function)); |
1164 | 3.07M | } |
1165 | | |
1166 | | static zend_string *zend_resolve_const_name(zend_string *name, uint32_t type, bool *is_fully_qualified) |
1167 | 7.57M | { |
1168 | 7.57M | return zend_resolve_non_class_name( |
1169 | 7.57M | name, type, is_fully_qualified, true, FC(imports_const)); |
1170 | 7.57M | } |
1171 | | |
1172 | | static zend_string *zend_resolve_class_name(zend_string *name, uint32_t type) /* {{{ */ |
1173 | 2.57M | { |
1174 | 2.57M | const char *compound; |
1175 | | |
1176 | 2.57M | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1177 | 4.23k | if (type == ZEND_NAME_FQ) { |
1178 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
1179 | 7 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1180 | 7 | } |
1181 | 4.23k | if (type == ZEND_NAME_RELATIVE) { |
1182 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
1183 | 0 | "'namespace\\%s' is an invalid class name", ZSTR_VAL(name)); |
1184 | 0 | } |
1185 | 4.23k | ZEND_ASSERT(type == ZEND_NAME_NOT_FQ); |
1186 | 4.23k | return zend_string_copy(name); |
1187 | 4.23k | } |
1188 | | |
1189 | 2.57M | if (type == ZEND_NAME_RELATIVE) { |
1190 | 591 | return zend_prefix_with_ns(name); |
1191 | 591 | } |
1192 | | |
1193 | 2.57M | if (type == ZEND_NAME_FQ) { |
1194 | 17.7k | if (ZSTR_VAL(name)[0] == '\\') { |
1195 | | /* Remove \ prefix (only relevant if this is a string rather than a label) */ |
1196 | 2.16k | name = zend_string_init(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1, 0); |
1197 | 2.16k | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) { |
1198 | 1 | zend_error_noreturn(E_COMPILE_ERROR, |
1199 | 1 | "'\\%s' is an invalid class name", ZSTR_VAL(name)); |
1200 | 1 | } |
1201 | 2.16k | return name; |
1202 | 2.16k | } |
1203 | | |
1204 | 15.5k | return zend_string_copy(name); |
1205 | 17.7k | } |
1206 | | |
1207 | 2.55M | if (FC(imports)) { |
1208 | 2.95k | compound = memchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)); |
1209 | 2.95k | if (compound) { |
1210 | | /* If the first part of a qualified name is an alias, substitute it. */ |
1211 | 586 | size_t len = compound - ZSTR_VAL(name); |
1212 | 586 | const zend_string *import_name = |
1213 | 586 | zend_hash_str_find_ptr_lc(FC(imports), ZSTR_VAL(name), len); |
1214 | | |
1215 | 586 | if (import_name) { |
1216 | 304 | return zend_concat_names( |
1217 | 304 | ZSTR_VAL(import_name), ZSTR_LEN(import_name), ZSTR_VAL(name) + len + 1, ZSTR_LEN(name) - len - 1); |
1218 | 304 | } |
1219 | 2.36k | } else { |
1220 | | /* If an unqualified name is an alias, replace it. */ |
1221 | 2.36k | zend_string *import_name |
1222 | 2.36k | = zend_hash_find_ptr_lc(FC(imports), name); |
1223 | | |
1224 | 2.36k | if (import_name) { |
1225 | 769 | return zend_string_copy(import_name); |
1226 | 769 | } |
1227 | 2.36k | } |
1228 | 2.95k | } |
1229 | | |
1230 | | /* If not fully qualified and not an alias, prepend the current namespace */ |
1231 | 2.55M | return zend_prefix_with_ns(name); |
1232 | 2.55M | } |
1233 | | /* }}} */ |
1234 | | |
1235 | | static zend_string *zend_resolve_class_name_ast(zend_ast *ast) /* {{{ */ |
1236 | 2.41M | { |
1237 | 2.41M | const zval *class_name = zend_ast_get_zval(ast); |
1238 | 2.41M | if (Z_TYPE_P(class_name) != IS_STRING) { |
1239 | 27 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1240 | 27 | } |
1241 | 2.41M | return zend_resolve_class_name(Z_STR_P(class_name), ast->attr); |
1242 | 2.41M | } |
1243 | | /* }}} */ |
1244 | | |
1245 | | static void label_ptr_dtor(zval *zv) /* {{{ */ |
1246 | 3.25k | { |
1247 | 3.25k | efree_size(Z_PTR_P(zv), sizeof(zend_label)); |
1248 | 3.25k | } |
1249 | | /* }}} */ |
1250 | | |
1251 | 1.95k | static void str_dtor(zval *zv) /* {{{ */ { |
1252 | 1.95k | zend_string_release_ex(Z_STR_P(zv), 0); |
1253 | 1.95k | } |
1254 | | /* }}} */ |
1255 | | |
1256 | | static uint32_t zend_add_try_element(uint32_t try_op) /* {{{ */ |
1257 | 35.2k | { |
1258 | 35.2k | zend_op_array *op_array = CG(active_op_array); |
1259 | 35.2k | uint32_t try_catch_offset = op_array->last_try_catch++; |
1260 | 35.2k | zend_try_catch_element *elem; |
1261 | | |
1262 | 35.2k | op_array->try_catch_array = safe_erealloc( |
1263 | 35.2k | op_array->try_catch_array, sizeof(zend_try_catch_element), op_array->last_try_catch, 0); |
1264 | | |
1265 | 35.2k | elem = &op_array->try_catch_array[try_catch_offset]; |
1266 | 35.2k | elem->try_op = try_op; |
1267 | 35.2k | elem->catch_op = 0; |
1268 | 35.2k | elem->finally_op = 0; |
1269 | 35.2k | elem->finally_end = 0; |
1270 | | |
1271 | 35.2k | return try_catch_offset; |
1272 | 35.2k | } |
1273 | | /* }}} */ |
1274 | | |
1275 | | ZEND_API void function_add_ref(zend_function *function) /* {{{ */ |
1276 | 1.93k | { |
1277 | 1.93k | if (function->type == ZEND_USER_FUNCTION) { |
1278 | 1.93k | zend_op_array *op_array = &function->op_array; |
1279 | 1.93k | if (op_array->refcount) { |
1280 | 329 | (*op_array->refcount)++; |
1281 | 329 | } |
1282 | | |
1283 | 1.93k | ZEND_MAP_PTR_INIT(op_array->run_time_cache, NULL); |
1284 | 1.93k | ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, NULL); |
1285 | 1.93k | } |
1286 | | |
1287 | 1.93k | if (function->common.function_name) { |
1288 | 1.93k | zend_string_addref(function->common.function_name); |
1289 | 1.93k | } |
1290 | 1.93k | } |
1291 | | /* }}} */ |
1292 | | |
1293 | | 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) /* {{{ */ |
1294 | 110 | { |
1295 | 110 | const zval *zv = zend_hash_find_known_hash(compile_time ? CG(function_table) : EG(function_table), lcname); |
1296 | 110 | int error_level = compile_time ? E_COMPILE_ERROR : E_ERROR; |
1297 | 110 | const zend_function *old_function; |
1298 | | |
1299 | 110 | ZEND_ASSERT(zv != NULL); |
1300 | 110 | old_function = Z_PTR_P(zv); |
1301 | 110 | if (old_function->type == ZEND_USER_FUNCTION |
1302 | 105 | && old_function->op_array.last > 0) { |
1303 | 105 | zend_error_noreturn(error_level, "Cannot redeclare function %s() (previously declared in %s:%d)", |
1304 | 105 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name), |
1305 | 105 | ZSTR_VAL(old_function->op_array.filename), |
1306 | 105 | old_function->op_array.line_start); |
1307 | 105 | } else { |
1308 | 5 | zend_error_noreturn(error_level, "Cannot redeclare function %s()", |
1309 | 5 | op_array ? ZSTR_VAL(op_array->function_name) : ZSTR_VAL(old_function->common.function_name)); |
1310 | 5 | } |
1311 | 110 | } |
1312 | | |
1313 | | ZEND_API zend_result do_bind_function(zend_function *func, const zval *lcname) /* {{{ */ |
1314 | 251 | { |
1315 | 251 | zend_function *added_func = zend_hash_add_ptr(EG(function_table), Z_STR_P(lcname), func); |
1316 | 251 | if (UNEXPECTED(!added_func)) { |
1317 | 12 | do_bind_function_error(Z_STR_P(lcname), &func->op_array, false); |
1318 | 0 | return FAILURE; |
1319 | 12 | } |
1320 | | |
1321 | 239 | if (func->op_array.refcount) { |
1322 | 44 | ++*func->op_array.refcount; |
1323 | 44 | } |
1324 | 239 | if (func->common.function_name) { |
1325 | 239 | zend_string_addref(func->common.function_name); |
1326 | 239 | } |
1327 | 239 | zend_observer_function_declared_notify(&func->op_array, Z_STR_P(lcname)); |
1328 | 239 | return SUCCESS; |
1329 | 251 | } |
1330 | | /* }}} */ |
1331 | | |
1332 | | ZEND_API zend_class_entry *zend_bind_class_in_slot( |
1333 | | zval *class_table_slot, const zval *lcname, zend_string *lc_parent_name) |
1334 | 8.02k | { |
1335 | 8.02k | zend_class_entry *ce = Z_PTR_P(class_table_slot); |
1336 | 8.02k | bool is_preloaded = |
1337 | 8.02k | (ce->ce_flags & ZEND_ACC_PRELOADED) && !(CG(compiler_options) & ZEND_COMPILE_PRELOAD); |
1338 | 8.02k | bool success; |
1339 | 8.02k | if (EXPECTED(!is_preloaded)) { |
1340 | 8.02k | success = zend_hash_set_bucket_key(EG(class_table), (Bucket*) class_table_slot, Z_STR_P(lcname)) != NULL; |
1341 | 8.02k | } else { |
1342 | | /* If preloading is used, don't replace the existing bucket, add a new one. */ |
1343 | 0 | success = zend_hash_add_ptr(EG(class_table), Z_STR_P(lcname), ce) != NULL; |
1344 | 0 | } |
1345 | 8.02k | if (UNEXPECTED(!success)) { |
1346 | 164 | zend_class_entry *old_class = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1347 | 164 | ZEND_ASSERT(old_class); |
1348 | 164 | zend_class_redeclaration_error(E_COMPILE_ERROR, old_class); |
1349 | 164 | return NULL; |
1350 | 164 | } |
1351 | | |
1352 | 7.85k | if (ce->ce_flags & ZEND_ACC_LINKED) { |
1353 | 309 | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1354 | 309 | return ce; |
1355 | 309 | } |
1356 | | |
1357 | 7.55k | ce = zend_do_link_class(ce, lc_parent_name, Z_STR_P(lcname)); |
1358 | 7.55k | if (ce) { |
1359 | 6.02k | zend_observer_class_linked_notify(ce, Z_STR_P(lcname)); |
1360 | 6.02k | return ce; |
1361 | 6.02k | } |
1362 | | |
1363 | 1.52k | if (!is_preloaded) { |
1364 | | /* Reload bucket pointer, the hash table may have been reallocated */ |
1365 | 374 | zval *zv = zend_hash_find(EG(class_table), Z_STR_P(lcname)); |
1366 | 374 | zend_hash_set_bucket_key(EG(class_table), (Bucket *) zv, Z_STR_P(lcname + 1)); |
1367 | 1.15k | } else { |
1368 | 1.15k | zend_hash_del(EG(class_table), Z_STR_P(lcname)); |
1369 | 1.15k | } |
1370 | 1.52k | return NULL; |
1371 | 7.55k | } |
1372 | | |
1373 | | ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name) /* {{{ */ |
1374 | 7.35k | { |
1375 | 7.35k | zval *rtd_key, *zv; |
1376 | | |
1377 | 7.35k | rtd_key = lcname + 1; |
1378 | | |
1379 | 7.35k | zv = zend_hash_find_known_hash(EG(class_table), Z_STR_P(rtd_key)); |
1380 | | |
1381 | 7.35k | if (UNEXPECTED(!zv)) { |
1382 | 21 | const zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), Z_STR_P(lcname)); |
1383 | 21 | ZEND_ASSERT(ce); |
1384 | 21 | zend_class_redeclaration_error(E_COMPILE_ERROR, ce); |
1385 | 21 | return FAILURE; |
1386 | 21 | } |
1387 | | |
1388 | | /* Register the derived class */ |
1389 | 7.33k | return zend_bind_class_in_slot(zv, lcname, lc_parent_name) ? SUCCESS : FAILURE; |
1390 | 7.35k | } |
1391 | | /* }}} */ |
1392 | | |
1393 | 23.3k | static zend_string *add_type_string(zend_string *type, zend_string *new_type, bool is_intersection) { |
1394 | 23.3k | zend_string *result; |
1395 | 23.3k | if (type == NULL) { |
1396 | 17.1k | return zend_string_copy(new_type); |
1397 | 17.1k | } |
1398 | | |
1399 | 6.11k | if (is_intersection) { |
1400 | 2.66k | result = zend_string_concat3(ZSTR_VAL(type), ZSTR_LEN(type), |
1401 | 2.66k | "&", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1402 | 2.66k | zend_string_release(type); |
1403 | 3.44k | } else { |
1404 | 3.44k | result = zend_string_concat3( |
1405 | 3.44k | ZSTR_VAL(type), ZSTR_LEN(type), "|", 1, ZSTR_VAL(new_type), ZSTR_LEN(new_type)); |
1406 | 3.44k | zend_string_release(type); |
1407 | 3.44k | } |
1408 | 6.11k | return result; |
1409 | 23.3k | } |
1410 | | |
1411 | 3.97k | static zend_string *resolve_class_name(zend_string *name, const zend_class_entry *scope) { |
1412 | 3.97k | if (scope) { |
1413 | 2.23k | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1414 | 48 | name = scope->name; |
1415 | 2.18k | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT)) && scope->parent) { |
1416 | 0 | name = scope->parent->name; |
1417 | 0 | } |
1418 | 2.23k | } |
1419 | | |
1420 | | /* The resolved name for anonymous classes contains null bytes. Cut off everything after the |
1421 | | * null byte here, to avoid larger parts of the type being omitted by printing code later. */ |
1422 | 3.97k | size_t len = strlen(ZSTR_VAL(name)); |
1423 | 3.97k | if (len != ZSTR_LEN(name)) { |
1424 | 12 | return zend_string_init(ZSTR_VAL(name), len, 0); |
1425 | 12 | } |
1426 | 3.96k | return zend_string_copy(name); |
1427 | 3.97k | } |
1428 | | |
1429 | | static zend_string *add_intersection_type(zend_string *str, |
1430 | | const zend_type_list *intersection_type_list, |
1431 | | bool is_bracketed) |
1432 | 1.49k | { |
1433 | 1.49k | const zend_type *single_type; |
1434 | 1.49k | zend_string *intersection_str = NULL; |
1435 | | |
1436 | 5.64k | ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) { |
1437 | 5.64k | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*single_type)); |
1438 | 5.64k | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*single_type)); |
1439 | | |
1440 | 4.15k | intersection_str = add_type_string(intersection_str, ZEND_TYPE_NAME(*single_type), /* is_intersection */ true); |
1441 | 4.15k | } ZEND_TYPE_LIST_FOREACH_END(); |
1442 | | |
1443 | 1.49k | ZEND_ASSERT(intersection_str); |
1444 | | |
1445 | 1.49k | if (is_bracketed) { |
1446 | 1.12k | zend_string *result = zend_string_concat3("(", 1, ZSTR_VAL(intersection_str), ZSTR_LEN(intersection_str), ")", 1); |
1447 | 1.12k | zend_string_release(intersection_str); |
1448 | 1.12k | intersection_str = result; |
1449 | 1.12k | } |
1450 | 1.49k | str = add_type_string(str, intersection_str, /* is_intersection */ false); |
1451 | 1.49k | zend_string_release(intersection_str); |
1452 | 1.49k | return str; |
1453 | 1.49k | } |
1454 | | |
1455 | 18.6k | zend_string *zend_type_to_string_resolved(const zend_type type, const zend_class_entry *scope) { |
1456 | 18.6k | zend_string *str = NULL; |
1457 | | |
1458 | | /* Pure intersection type */ |
1459 | 18.6k | if (ZEND_TYPE_IS_INTERSECTION(type)) { |
1460 | 366 | ZEND_ASSERT(!ZEND_TYPE_IS_UNION(type)); |
1461 | 366 | str = add_intersection_type(str, ZEND_TYPE_LIST(type), /* is_bracketed */ false); |
1462 | 18.2k | } else if (ZEND_TYPE_HAS_LIST(type)) { |
1463 | | /* A union type might not be a list */ |
1464 | 630 | const zend_type *list_type; |
1465 | 2.81k | ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) { |
1466 | 2.81k | if (ZEND_TYPE_IS_INTERSECTION(*list_type)) { |
1467 | 1.12k | str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), /* is_bracketed */ true); |
1468 | 1.12k | continue; |
1469 | 1.12k | } |
1470 | 1.05k | ZEND_ASSERT(!ZEND_TYPE_HAS_LIST(*list_type)); |
1471 | 1.05k | ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*list_type)); |
1472 | | |
1473 | 1.05k | zend_string *name = ZEND_TYPE_NAME(*list_type); |
1474 | 1.05k | zend_string *resolved = resolve_class_name(name, scope); |
1475 | 1.05k | str = add_type_string(str, resolved, /* is_intersection */ false); |
1476 | 1.05k | zend_string_release(resolved); |
1477 | 1.05k | } ZEND_TYPE_LIST_FOREACH_END(); |
1478 | 17.6k | } else if (ZEND_TYPE_HAS_NAME(type)) { |
1479 | 2.91k | str = resolve_class_name(ZEND_TYPE_NAME(type), scope); |
1480 | 2.91k | } |
1481 | | |
1482 | 18.6k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
1483 | | |
1484 | 18.6k | if (type_mask == MAY_BE_ANY) { |
1485 | 336 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_MIXED), /* is_intersection */ false); |
1486 | | |
1487 | 336 | return str; |
1488 | 336 | } |
1489 | 18.2k | if (type_mask & MAY_BE_STATIC) { |
1490 | 254 | zend_string *name = ZSTR_KNOWN(ZEND_STR_STATIC); |
1491 | | // During compilation of eval'd code the called scope refers to the scope calling the eval |
1492 | 254 | if (scope && !zend_is_compiling()) { |
1493 | 65 | const zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data)); |
1494 | 65 | if (called_scope) { |
1495 | 35 | name = called_scope->name; |
1496 | 35 | } |
1497 | 65 | } |
1498 | 254 | str = add_type_string(str, name, /* is_intersection */ false); |
1499 | 254 | } |
1500 | 18.2k | if (type_mask & MAY_BE_CALLABLE) { |
1501 | 77 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_CALLABLE), /* is_intersection */ false); |
1502 | 77 | } |
1503 | 18.2k | if (type_mask & MAY_BE_OBJECT) { |
1504 | 316 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_OBJECT), /* is_intersection */ false); |
1505 | 316 | } |
1506 | 18.2k | if (type_mask & MAY_BE_ARRAY) { |
1507 | 2.06k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_ARRAY), /* is_intersection */ false); |
1508 | 2.06k | } |
1509 | 18.2k | if (type_mask & MAY_BE_STRING) { |
1510 | 7.50k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_STRING), /* is_intersection */ false); |
1511 | 7.50k | } |
1512 | 18.2k | if (type_mask & MAY_BE_LONG) { |
1513 | 4.14k | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_INT), /* is_intersection */ false); |
1514 | 4.14k | } |
1515 | 18.2k | if (type_mask & MAY_BE_DOUBLE) { |
1516 | 539 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FLOAT), /* is_intersection */ false); |
1517 | 539 | } |
1518 | 18.2k | if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL) { |
1519 | 707 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_BOOL), /* is_intersection */ false); |
1520 | 17.5k | } else if (type_mask & MAY_BE_FALSE) { |
1521 | 135 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_FALSE), /* is_intersection */ false); |
1522 | 17.4k | } else if (type_mask & MAY_BE_TRUE) { |
1523 | 53 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_TRUE), /* is_intersection */ false); |
1524 | 53 | } |
1525 | 18.2k | if (type_mask & MAY_BE_VOID) { |
1526 | 289 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_VOID), /* is_intersection */ false); |
1527 | 289 | } |
1528 | 18.2k | if (type_mask & MAY_BE_NEVER) { |
1529 | 25 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NEVER), /* is_intersection */ false); |
1530 | 25 | } |
1531 | | |
1532 | 18.2k | if (type_mask & MAY_BE_NULL) { |
1533 | 1.63k | bool is_union = !str || memchr(ZSTR_VAL(str), '|', ZSTR_LEN(str)) != NULL; |
1534 | 1.63k | bool has_intersection = !str || memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str)) != NULL; |
1535 | 1.63k | if (!is_union && !has_intersection) { |
1536 | 1.48k | zend_string *nullable_str = zend_string_concat2("?", 1, ZSTR_VAL(str), ZSTR_LEN(str)); |
1537 | 1.48k | zend_string_release(str); |
1538 | 1.48k | return nullable_str; |
1539 | 1.48k | } |
1540 | | |
1541 | 151 | str = add_type_string(str, ZSTR_KNOWN(ZEND_STR_NULL_LOWERCASE), /* is_intersection */ false); |
1542 | 151 | } |
1543 | 16.8k | return str; |
1544 | 18.2k | } |
1545 | | |
1546 | 13.5k | ZEND_API zend_string *zend_type_to_string(zend_type type) { |
1547 | 13.5k | return zend_type_to_string_resolved(type, NULL); |
1548 | 13.5k | } |
1549 | | |
1550 | 2.15k | static bool is_generator_compatible_class_type(const zend_string *name) { |
1551 | 2.15k | return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE)) |
1552 | 1.31k | || zend_string_equals_literal_ci(name, "Iterator") |
1553 | 852 | || zend_string_equals_literal_ci(name, "Generator"); |
1554 | 2.15k | } |
1555 | | |
1556 | | static void zend_mark_function_as_generator(void) /* {{{ */ |
1557 | 435k | { |
1558 | 435k | if (!CG(active_op_array)->function_name) { |
1559 | 73 | zend_error_noreturn(E_COMPILE_ERROR, |
1560 | 73 | "The \"yield\" expression can only be used inside a function"); |
1561 | 73 | } |
1562 | | |
1563 | 434k | if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
1564 | 1.66k | const zend_type return_type = CG(active_op_array)->arg_info[-1].type; |
1565 | 1.66k | bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0; |
1566 | 1.66k | if (!valid_type) { |
1567 | 1.63k | const zend_type *single_type; |
1568 | 3.79k | ZEND_TYPE_FOREACH(return_type, single_type) { |
1569 | 3.79k | if (ZEND_TYPE_HAS_NAME(*single_type) |
1570 | 2.15k | && is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) { |
1571 | 1.59k | valid_type = true; |
1572 | 1.59k | break; |
1573 | 1.59k | } |
1574 | 3.79k | } ZEND_TYPE_FOREACH_END(); |
1575 | 1.63k | } |
1576 | | |
1577 | 1.66k | if (!valid_type) { |
1578 | 36 | zend_string *str = zend_type_to_string(return_type); |
1579 | 36 | zend_error_noreturn(E_COMPILE_ERROR, |
1580 | 36 | "Generator return type must be a supertype of Generator, %s given", |
1581 | 36 | ZSTR_VAL(str)); |
1582 | 36 | } |
1583 | 1.66k | } |
1584 | | |
1585 | 434k | CG(active_op_array)->fn_flags |= ZEND_ACC_GENERATOR; |
1586 | 434k | } |
1587 | | /* }}} */ |
1588 | | |
1589 | | ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal) /* {{{ */ |
1590 | 67.8k | { |
1591 | 67.8k | size_t prop_name_length = 1 + src1_length + 1 + src2_length; |
1592 | 67.8k | zend_string *prop_name = zend_string_alloc(prop_name_length, internal); |
1593 | | |
1594 | 67.8k | ZSTR_VAL(prop_name)[0] = '\0'; |
1595 | 67.8k | memcpy(ZSTR_VAL(prop_name) + 1, src1, src1_length+1); |
1596 | 67.8k | memcpy(ZSTR_VAL(prop_name) + 1 + src1_length + 1, src2, src2_length+1); |
1597 | 67.8k | return prop_name; |
1598 | 67.8k | } |
1599 | | /* }}} */ |
1600 | | |
1601 | | 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) /* {{{ */ |
1602 | 1.59M | { |
1603 | 1.59M | size_t class_name_len; |
1604 | 1.59M | size_t anonclass_src_len; |
1605 | | |
1606 | 1.59M | *class_name = NULL; |
1607 | | |
1608 | 1.59M | if (!ZSTR_LEN(name) || ZSTR_VAL(name)[0] != '\0') { |
1609 | 1.42M | *prop_name = ZSTR_VAL(name); |
1610 | 1.42M | if (prop_len) { |
1611 | 973k | *prop_len = ZSTR_LEN(name); |
1612 | 973k | } |
1613 | 1.42M | return SUCCESS; |
1614 | 1.42M | } |
1615 | 168k | if (ZSTR_LEN(name) < 3 || ZSTR_VAL(name)[1] == '\0') { |
1616 | 1.33k | zend_error(E_NOTICE, "Illegal member variable name"); |
1617 | 1.33k | *prop_name = ZSTR_VAL(name); |
1618 | 1.33k | if (prop_len) { |
1619 | 1.28k | *prop_len = ZSTR_LEN(name); |
1620 | 1.28k | } |
1621 | 1.33k | return FAILURE; |
1622 | 1.33k | } |
1623 | | |
1624 | 167k | class_name_len = zend_strnlen(ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 2); |
1625 | 167k | if (class_name_len >= ZSTR_LEN(name) - 2 || ZSTR_VAL(name)[class_name_len + 1] != '\0') { |
1626 | 610 | zend_error(E_NOTICE, "Corrupt member variable name"); |
1627 | 610 | *prop_name = ZSTR_VAL(name); |
1628 | 610 | if (prop_len) { |
1629 | 377 | *prop_len = ZSTR_LEN(name); |
1630 | 377 | } |
1631 | 610 | return FAILURE; |
1632 | 610 | } |
1633 | | |
1634 | 166k | *class_name = ZSTR_VAL(name) + 1; |
1635 | 166k | anonclass_src_len = zend_strnlen(*class_name + class_name_len + 1, ZSTR_LEN(name) - class_name_len - 2); |
1636 | 166k | if (class_name_len + anonclass_src_len + 2 != ZSTR_LEN(name)) { |
1637 | 36.8k | class_name_len += anonclass_src_len + 1; |
1638 | 36.8k | } |
1639 | 166k | *prop_name = ZSTR_VAL(name) + class_name_len + 2; |
1640 | 166k | if (prop_len) { |
1641 | 102k | *prop_len = ZSTR_LEN(name) - class_name_len - 2; |
1642 | 102k | } |
1643 | 166k | return SUCCESS; |
1644 | 167k | } |
1645 | | /* }}} */ |
1646 | | |
1647 | | static bool array_is_const_ex(const zend_array *array, uint32_t *max_checks) |
1648 | 92 | { |
1649 | 92 | if (zend_hash_num_elements(array) > *max_checks) { |
1650 | 0 | return false; |
1651 | 0 | } |
1652 | 92 | *max_checks -= zend_hash_num_elements(array); |
1653 | | |
1654 | 92 | zval *element; |
1655 | 204 | ZEND_HASH_FOREACH_VAL(array, element) { |
1656 | 204 | if (Z_TYPE_P(element) < IS_ARRAY) { |
1657 | 56 | continue; |
1658 | 56 | } else if (Z_TYPE_P(element) == IS_ARRAY) { |
1659 | 0 | if (!array_is_const_ex(array, max_checks)) { |
1660 | 0 | return false; |
1661 | 0 | } |
1662 | 0 | } else { |
1663 | 0 | return false; |
1664 | 0 | } |
1665 | 204 | } ZEND_HASH_FOREACH_END(); |
1666 | | |
1667 | 92 | return true; |
1668 | 92 | } |
1669 | | |
1670 | | static bool array_is_const(const zend_array *array) |
1671 | 92 | { |
1672 | 92 | uint32_t max_checks = 50; |
1673 | 92 | return array_is_const_ex(array, &max_checks); |
1674 | 92 | } |
1675 | | |
1676 | 11.6k | static bool can_ct_eval_const(const zend_constant *c) { |
1677 | 11.6k | if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) { |
1678 | 164 | return false; |
1679 | 164 | } |
1680 | 11.5k | if ((ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) |
1681 | 11.4k | && !(CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) |
1682 | 9.90k | && !((ZEND_CONSTANT_FLAGS(c) & CONST_NO_FILE_CACHE) |
1683 | 9.90k | && (CG(compiler_options) & ZEND_COMPILE_WITH_FILE_CACHE))) { |
1684 | 9.90k | return true; |
1685 | 9.90k | } |
1686 | 1.60k | if (Z_TYPE(c->value) < IS_ARRAY |
1687 | 1.60k | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1688 | 14 | return 1; |
1689 | 1.58k | } else if (Z_TYPE(c->value) == IS_ARRAY |
1690 | 0 | && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION) |
1691 | 0 | && array_is_const(Z_ARR(c->value))) { |
1692 | 0 | return 1; |
1693 | 0 | } |
1694 | 1.58k | return false; |
1695 | 1.60k | } |
1696 | | |
1697 | | static bool zend_try_ct_eval_const(zval *zv, zend_string *name, bool is_fully_qualified) /* {{{ */ |
1698 | 7.57M | { |
1699 | | /* Substitute true, false and null (including unqualified usage in namespaces) |
1700 | | * before looking up the possibly namespaced name. */ |
1701 | 7.57M | const char *lookup_name = ZSTR_VAL(name); |
1702 | 7.57M | size_t lookup_len = ZSTR_LEN(name); |
1703 | | |
1704 | 7.57M | if (!is_fully_qualified) { |
1705 | 7.56M | zend_get_unqualified_name(name, &lookup_name, &lookup_len); |
1706 | 7.56M | } |
1707 | | |
1708 | 7.57M | zend_constant *c; |
1709 | 7.57M | if ((c = zend_get_special_const(lookup_name, lookup_len))) { |
1710 | 53.3k | ZVAL_COPY_VALUE(zv, &c->value); |
1711 | 53.3k | return true; |
1712 | 53.3k | } |
1713 | 7.52M | c = zend_hash_find_ptr(EG(zend_constants), name); |
1714 | 7.52M | if (c && can_ct_eval_const(c)) { |
1715 | 9.91k | ZVAL_COPY_OR_DUP(zv, &c->value); |
1716 | 9.91k | return true; |
1717 | 9.91k | } |
1718 | 7.51M | return false; |
1719 | 7.52M | } |
1720 | | /* }}} */ |
1721 | | |
1722 | | static inline bool zend_is_scope_known(void) /* {{{ */ |
1723 | 17.8k | { |
1724 | 17.8k | if (!CG(active_op_array)) { |
1725 | | /* This can only happen when evaluating a default value string. */ |
1726 | 0 | return false; |
1727 | 0 | } |
1728 | | |
1729 | 17.8k | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
1730 | | /* Closures can be rebound to a different scope */ |
1731 | 3.51k | return false; |
1732 | 3.51k | } |
1733 | | |
1734 | 14.3k | if (!CG(active_class_entry)) { |
1735 | | /* The scope is known if we're in a free function (no scope), but not if we're in |
1736 | | * a file/eval (which inherits including/eval'ing scope). */ |
1737 | 1.53k | return CG(active_op_array)->function_name != NULL; |
1738 | 1.53k | } |
1739 | | |
1740 | | /* For traits self etc refers to the using class, not the trait itself */ |
1741 | 12.7k | return (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) == 0; |
1742 | 14.3k | } |
1743 | | /* }}} */ |
1744 | | |
1745 | | static inline bool class_name_refers_to_active_ce(const zend_string *class_name, uint32_t fetch_type) /* {{{ */ |
1746 | 96.5k | { |
1747 | 96.5k | if (!CG(active_class_entry)) { |
1748 | 90.7k | return false; |
1749 | 90.7k | } |
1750 | 5.77k | if (fetch_type == ZEND_FETCH_CLASS_SELF && zend_is_scope_known()) { |
1751 | 1.97k | return true; |
1752 | 1.97k | } |
1753 | 3.79k | return fetch_type == ZEND_FETCH_CLASS_DEFAULT |
1754 | 2.76k | && zend_string_equals_ci(class_name, CG(active_class_entry)->name); |
1755 | 5.77k | } |
1756 | | /* }}} */ |
1757 | | |
1758 | | uint32_t zend_get_class_fetch_type(const zend_string *name) /* {{{ */ |
1759 | 3.41M | { |
1760 | 3.41M | if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_SELF))) { |
1761 | 13.3k | return ZEND_FETCH_CLASS_SELF; |
1762 | 3.40M | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_PARENT))) { |
1763 | 6.77k | return ZEND_FETCH_CLASS_PARENT; |
1764 | 3.39M | } else if (zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_STATIC))) { |
1765 | 2.51k | return ZEND_FETCH_CLASS_STATIC; |
1766 | 3.39M | } else { |
1767 | 3.39M | return ZEND_FETCH_CLASS_DEFAULT; |
1768 | 3.39M | } |
1769 | 3.41M | } |
1770 | | /* }}} */ |
1771 | | |
1772 | | static uint32_t zend_get_class_fetch_type_ast(zend_ast *name_ast) /* {{{ */ |
1773 | 603k | { |
1774 | | /* Fully qualified names are always default refs */ |
1775 | 603k | if (name_ast->attr == ZEND_NAME_FQ) { |
1776 | 4.44k | return ZEND_FETCH_CLASS_DEFAULT; |
1777 | 4.44k | } |
1778 | | |
1779 | 599k | return zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
1780 | 603k | } |
1781 | | /* }}} */ |
1782 | | |
1783 | | static zend_string *zend_resolve_const_class_name_reference(zend_ast *ast, const char *type) |
1784 | 159k | { |
1785 | 159k | zend_string *class_name = zend_ast_get_str(ast); |
1786 | 159k | if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type_ast(ast)) { |
1787 | 24 | zend_error_noreturn(E_COMPILE_ERROR, |
1788 | 24 | "Cannot use \"%s\" as %s, as it is reserved", |
1789 | 24 | ZSTR_VAL(class_name), type); |
1790 | 24 | } |
1791 | 159k | return zend_resolve_class_name(class_name, ast->attr); |
1792 | 159k | } |
1793 | | |
1794 | | static void zend_ensure_valid_class_fetch_type(uint32_t fetch_type) /* {{{ */ |
1795 | 15.3k | { |
1796 | 15.3k | if (fetch_type != ZEND_FETCH_CLASS_DEFAULT && zend_is_scope_known()) { |
1797 | 5.94k | zend_class_entry *ce = CG(active_class_entry); |
1798 | 5.94k | if (!ce) { |
1799 | 27 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"%s\" when no class scope is active", |
1800 | 27 | fetch_type == ZEND_FETCH_CLASS_SELF ? "self" : |
1801 | 27 | fetch_type == ZEND_FETCH_CLASS_PARENT ? "parent" : "static"); |
1802 | 5.91k | } else if (fetch_type == ZEND_FETCH_CLASS_PARENT && !ce->parent_name) { |
1803 | 26 | zend_error_noreturn(E_COMPILE_ERROR, |
1804 | 26 | "Cannot use \"parent\" when current class scope has no parent"); |
1805 | 26 | } |
1806 | 5.94k | } |
1807 | 15.3k | } |
1808 | | /* }}} */ |
1809 | | |
1810 | | static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *class_ast) /* {{{ */ |
1811 | 11.8k | { |
1812 | 11.8k | uint32_t fetch_type; |
1813 | 11.8k | const zval *class_name; |
1814 | | |
1815 | 11.8k | if (class_ast->kind != ZEND_AST_ZVAL) { |
1816 | 4.20k | return false; |
1817 | 4.20k | } |
1818 | | |
1819 | 7.67k | class_name = zend_ast_get_zval(class_ast); |
1820 | | |
1821 | 7.67k | if (Z_TYPE_P(class_name) != IS_STRING) { |
1822 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
1823 | 5 | } |
1824 | | |
1825 | 7.66k | fetch_type = zend_get_class_fetch_type(Z_STR_P(class_name)); |
1826 | 7.66k | zend_ensure_valid_class_fetch_type(fetch_type); |
1827 | | |
1828 | 7.66k | switch (fetch_type) { |
1829 | 676 | case ZEND_FETCH_CLASS_SELF: |
1830 | 676 | if (CG(active_class_entry) && zend_is_scope_known()) { |
1831 | 236 | ZVAL_STR_COPY(zv, CG(active_class_entry)->name); |
1832 | 236 | return true; |
1833 | 236 | } |
1834 | 440 | return false; |
1835 | 1.32k | case ZEND_FETCH_CLASS_PARENT: |
1836 | 1.32k | if (CG(active_class_entry) && CG(active_class_entry)->parent_name |
1837 | 310 | && zend_is_scope_known()) { |
1838 | 310 | ZVAL_STR_COPY(zv, CG(active_class_entry)->parent_name); |
1839 | 310 | return true; |
1840 | 310 | } |
1841 | 1.01k | return false; |
1842 | 552 | case ZEND_FETCH_CLASS_STATIC: |
1843 | 552 | return false; |
1844 | 5.10k | case ZEND_FETCH_CLASS_DEFAULT: |
1845 | 5.10k | ZVAL_STR(zv, zend_resolve_class_name_ast(class_ast)); |
1846 | 5.10k | return true; |
1847 | 0 | default: ZEND_UNREACHABLE(); |
1848 | 7.66k | } |
1849 | 7.66k | } |
1850 | | /* }}} */ |
1851 | | |
1852 | | /* We don't use zend_verify_const_access because we need to deal with unlinked classes. */ |
1853 | | static bool zend_verify_ct_const_access(const zend_class_constant *c, const zend_class_entry *scope) |
1854 | 822 | { |
1855 | 822 | if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) { |
1856 | 14 | return 0; |
1857 | 808 | } else if (c->ce->ce_flags & ZEND_ACC_TRAIT) { |
1858 | | /* This condition is only met on directly accessing trait constants, |
1859 | | * because the ce is replaced to the class entry of the composing class |
1860 | | * on binding. */ |
1861 | 0 | return 0; |
1862 | 808 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PUBLIC) { |
1863 | 474 | return 1; |
1864 | 474 | } else if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_PRIVATE) { |
1865 | 334 | return c->ce == scope; |
1866 | 334 | } else { |
1867 | 0 | zend_class_entry *ce = c->ce; |
1868 | 0 | while (1) { |
1869 | 0 | if (ce == scope) { |
1870 | 0 | return 1; |
1871 | 0 | } |
1872 | 0 | if (!ce->parent) { |
1873 | 0 | break; |
1874 | 0 | } |
1875 | 0 | if (ce->ce_flags & ZEND_ACC_RESOLVED_PARENT) { |
1876 | 0 | ce = ce->parent; |
1877 | 0 | } else { |
1878 | 0 | ce = zend_hash_find_ptr_lc(CG(class_table), ce->parent_name); |
1879 | 0 | if (!ce) { |
1880 | 0 | break; |
1881 | 0 | } |
1882 | 0 | } |
1883 | 0 | } |
1884 | | /* Reverse case cannot be true during compilation */ |
1885 | 0 | return 0; |
1886 | 0 | } |
1887 | 822 | } |
1888 | | |
1889 | | static bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */ |
1890 | 96.5k | { |
1891 | 96.5k | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
1892 | 96.5k | zend_class_constant *cc; |
1893 | 96.5k | zval *c; |
1894 | | |
1895 | 96.5k | if (class_name_refers_to_active_ce(class_name, fetch_type)) { |
1896 | 2.12k | cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name); |
1897 | 94.3k | } else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) { |
1898 | 380 | const zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), class_name); |
1899 | 380 | if (ce) { |
1900 | 74 | cc = zend_hash_find_ptr(&ce->constants_table, name); |
1901 | 306 | } else { |
1902 | 306 | return 0; |
1903 | 306 | } |
1904 | 93.9k | } else { |
1905 | 93.9k | return 0; |
1906 | 93.9k | } |
1907 | | |
1908 | 2.20k | if (CG(compiler_options) & ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION) { |
1909 | 974 | return false; |
1910 | 974 | } |
1911 | | |
1912 | 1.22k | if (!cc || !zend_verify_ct_const_access(cc, CG(active_class_entry))) { |
1913 | 421 | return false; |
1914 | 421 | } |
1915 | | |
1916 | 808 | c = &cc->value; |
1917 | | |
1918 | | /* Substitute case-sensitive (or lowercase) persistent class constants */ |
1919 | 808 | if (Z_TYPE_P(c) < IS_ARRAY) { |
1920 | 496 | ZVAL_COPY_OR_DUP(zv, c); |
1921 | 496 | return 1; |
1922 | 496 | } else if (Z_TYPE_P(c) == IS_ARRAY && array_is_const(Z_ARR_P(c))) { |
1923 | 92 | ZVAL_COPY_OR_DUP(zv, c); |
1924 | 92 | return 1; |
1925 | 92 | } |
1926 | | |
1927 | 220 | return false; |
1928 | 808 | } |
1929 | | /* }}} */ |
1930 | | |
1931 | | static void zend_add_to_list(void *result, void *item) /* {{{ */ |
1932 | 6.58k | { |
1933 | 6.58k | void** list = *(void**)result; |
1934 | 6.58k | size_t n = 0; |
1935 | | |
1936 | 6.58k | if (list) { |
1937 | 33.9k | while (list[n]) { |
1938 | 29.4k | n++; |
1939 | 29.4k | } |
1940 | 4.45k | } |
1941 | | |
1942 | 6.58k | list = erealloc(list, sizeof(void*) * (n+2)); |
1943 | | |
1944 | 6.58k | list[n] = item; |
1945 | 6.58k | list[n+1] = NULL; |
1946 | | |
1947 | 6.58k | *(void**)result = list; |
1948 | 6.58k | } |
1949 | | /* }}} */ |
1950 | | |
1951 | | static void zend_do_extended_stmt(znode* result) /* {{{ */ |
1952 | 1.54M | { |
1953 | 1.54M | zend_op *opline; |
1954 | | |
1955 | 1.54M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT)) { |
1956 | 1.54M | return; |
1957 | 1.54M | } |
1958 | | |
1959 | 0 | opline = get_next_op(); |
1960 | |
|
1961 | 0 | opline->opcode = ZEND_EXT_STMT; |
1962 | 0 | if (result) { |
1963 | 0 | if (result->op_type == IS_CONST) { |
1964 | 0 | Z_TRY_ADDREF(result->u.constant); |
1965 | 0 | } |
1966 | 0 | SET_NODE(opline->op1, result); |
1967 | 0 | } |
1968 | 0 | } |
1969 | | /* }}} */ |
1970 | | |
1971 | | static void zend_do_extended_fcall_begin(void) /* {{{ */ |
1972 | 3.35M | { |
1973 | 3.35M | zend_op *opline; |
1974 | | |
1975 | 3.35M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1976 | 3.35M | return; |
1977 | 3.35M | } |
1978 | | |
1979 | 0 | opline = get_next_op(); |
1980 | |
|
1981 | 0 | opline->opcode = ZEND_EXT_FCALL_BEGIN; |
1982 | 0 | } |
1983 | | /* }}} */ |
1984 | | |
1985 | | static void zend_do_extended_fcall_end(void) /* {{{ */ |
1986 | 3.35M | { |
1987 | 3.35M | zend_op *opline; |
1988 | | |
1989 | 3.35M | if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_FCALL)) { |
1990 | 3.35M | return; |
1991 | 3.35M | } |
1992 | | |
1993 | 0 | opline = get_next_op(); |
1994 | |
|
1995 | 0 | opline->opcode = ZEND_EXT_FCALL_END; |
1996 | 0 | } |
1997 | | /* }}} */ |
1998 | | |
1999 | | static zend_always_inline bool zend_auto_global_check(zend_auto_global *auto_global) |
2000 | 2.59M | { |
2001 | 2.59M | if (auto_global == NULL) { |
2002 | 2.59M | return false; |
2003 | 2.59M | } |
2004 | | |
2005 | 4.84k | if (auto_global->armed) { |
2006 | 207 | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2007 | 207 | } |
2008 | 4.84k | return true; |
2009 | 2.59M | } |
2010 | | |
2011 | | ZEND_API bool zend_is_auto_global_str(const char *name, size_t len) /* {{{ */ |
2012 | 0 | { |
2013 | 0 | return zend_auto_global_check(zend_hash_str_find_ptr(CG(auto_globals), name, len)); |
2014 | 0 | } |
2015 | | /* }}} */ |
2016 | | |
2017 | | ZEND_API bool zend_is_auto_global(zend_string *name) /* {{{ */ |
2018 | 2.59M | { |
2019 | 2.59M | return zend_auto_global_check(zend_hash_find_ptr(CG(auto_globals), name)); |
2020 | 2.59M | } |
2021 | | /* }}} */ |
2022 | | |
2023 | | ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback) /* {{{ */ |
2024 | 128 | { |
2025 | 128 | zend_auto_global auto_global; |
2026 | 128 | zend_result retval; |
2027 | | |
2028 | 128 | auto_global.name = name; |
2029 | 128 | auto_global.auto_global_callback = auto_global_callback; |
2030 | 128 | auto_global.jit = jit; |
2031 | | |
2032 | 128 | retval = zend_hash_add_mem(CG(auto_globals), auto_global.name, &auto_global, sizeof(zend_auto_global)) != NULL ? SUCCESS : FAILURE; |
2033 | | |
2034 | 128 | return retval; |
2035 | 128 | } |
2036 | | /* }}} */ |
2037 | | |
2038 | | ZEND_API void zend_activate_auto_globals(void) /* {{{ */ |
2039 | 295k | { |
2040 | 295k | zend_auto_global *auto_global; |
2041 | | |
2042 | 5.32M | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2043 | 5.32M | auto_global->armed = auto_global->jit || auto_global->auto_global_callback; |
2044 | 5.32M | } ZEND_HASH_FOREACH_END(); |
2045 | | |
2046 | 5.32M | ZEND_HASH_MAP_FOREACH_PTR(CG(auto_globals), auto_global) { |
2047 | 5.32M | if (auto_global->armed && !auto_global->jit) { |
2048 | 1.18M | auto_global->armed = auto_global->auto_global_callback(auto_global->name); |
2049 | 1.18M | } |
2050 | 5.32M | } ZEND_HASH_FOREACH_END(); |
2051 | 295k | } |
2052 | | /* }}} */ |
2053 | | |
2054 | | int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */ |
2055 | 10.7M | { |
2056 | 10.7M | zval zv; |
2057 | 10.7M | int ret; |
2058 | | |
2059 | 10.7M | if (CG(increment_lineno)) { |
2060 | 14.8k | CG(zend_lineno)++; |
2061 | 14.8k | CG(increment_lineno) = 0; |
2062 | 14.8k | } |
2063 | | |
2064 | 10.7M | ret = lex_scan(&zv, elem); |
2065 | 10.7M | ZEND_ASSERT(!EG(exception) || ret == T_ERROR); |
2066 | 10.7M | return ret; |
2067 | | |
2068 | 10.7M | } |
2069 | | /* }}} */ |
2070 | | |
2071 | | ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers) /* {{{ */ |
2072 | 204k | { |
2073 | 204k | bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS; |
2074 | | |
2075 | 204k | ce->refcount = 1; |
2076 | 204k | ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED; |
2077 | 204k | ce->ce_flags2 = 0; |
2078 | | |
2079 | 204k | if (CG(compiler_options) & ZEND_COMPILE_GUARDS) { |
2080 | 0 | ce->ce_flags |= ZEND_ACC_USE_GUARDS; |
2081 | 0 | } |
2082 | | |
2083 | 204k | ce->default_properties_table = NULL; |
2084 | 204k | ce->default_static_members_table = NULL; |
2085 | 204k | zend_hash_init(&ce->properties_info, 8, NULL, NULL, persistent_hashes); |
2086 | 204k | zend_hash_init(&ce->constants_table, 8, NULL, NULL, persistent_hashes); |
2087 | 204k | zend_hash_init(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes); |
2088 | | |
2089 | 204k | ce->doc_comment = NULL; |
2090 | | |
2091 | 204k | ZEND_MAP_PTR_INIT(ce->static_members_table, NULL); |
2092 | 204k | ZEND_MAP_PTR_INIT(ce->mutable_data, NULL); |
2093 | | |
2094 | 204k | ce->default_object_handlers = &std_object_handlers; |
2095 | 204k | ce->default_properties_count = 0; |
2096 | 204k | ce->default_static_members_count = 0; |
2097 | 204k | ce->properties_info_table = NULL; |
2098 | 204k | ce->attributes = NULL; |
2099 | 204k | ce->enum_backing_type = IS_UNDEF; |
2100 | 204k | ce->backed_enum_table = NULL; |
2101 | | |
2102 | 204k | if (nullify_handlers) { |
2103 | 201k | ce->constructor = NULL; |
2104 | 201k | ce->destructor = NULL; |
2105 | 201k | ce->clone = NULL; |
2106 | 201k | ce->__get = NULL; |
2107 | 201k | ce->__set = NULL; |
2108 | 201k | ce->__unset = NULL; |
2109 | 201k | ce->__isset = NULL; |
2110 | 201k | ce->__call = NULL; |
2111 | 201k | ce->__callstatic = NULL; |
2112 | 201k | ce->__tostring = NULL; |
2113 | 201k | ce->__serialize = NULL; |
2114 | 201k | ce->__unserialize = NULL; |
2115 | 201k | ce->__debugInfo = NULL; |
2116 | 201k | ce->create_object = NULL; |
2117 | 201k | ce->get_iterator = NULL; |
2118 | 201k | ce->iterator_funcs_ptr = NULL; |
2119 | 201k | ce->arrayaccess_funcs_ptr = NULL; |
2120 | 201k | ce->get_static_method = NULL; |
2121 | 201k | ce->parent = NULL; |
2122 | 201k | ce->parent_name = NULL; |
2123 | 201k | ce->num_interfaces = 0; |
2124 | 201k | ce->interfaces = NULL; |
2125 | 201k | ce->num_traits = 0; |
2126 | 201k | ce->num_hooked_props = 0; |
2127 | 201k | ce->num_hooked_prop_variance_checks = 0; |
2128 | 201k | ce->trait_names = NULL; |
2129 | 201k | ce->trait_aliases = NULL; |
2130 | 201k | ce->trait_precedences = NULL; |
2131 | 201k | ce->serialize = NULL; |
2132 | 201k | ce->unserialize = NULL; |
2133 | 201k | if (ce->type == ZEND_INTERNAL_CLASS) { |
2134 | 0 | ce->info.internal.module = NULL; |
2135 | 0 | ce->info.internal.builtin_functions = NULL; |
2136 | 0 | } |
2137 | 201k | } |
2138 | 204k | } |
2139 | | /* }}} */ |
2140 | | |
2141 | | ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var) /* {{{ */ |
2142 | 0 | { |
2143 | 0 | return op_array->vars[EX_VAR_TO_NUM(var)]; |
2144 | 0 | } |
2145 | | /* }}} */ |
2146 | | |
2147 | | zend_ast *zend_ast_append_str(zend_ast *left_ast, zend_ast *right_ast) /* {{{ */ |
2148 | 0 | { |
2149 | 0 | zval *left_zv = zend_ast_get_zval(left_ast); |
2150 | 0 | zend_string *left = Z_STR_P(left_zv); |
2151 | 0 | zend_string *right = zend_ast_get_str(right_ast); |
2152 | |
|
2153 | 0 | zend_string *result; |
2154 | 0 | size_t left_len = ZSTR_LEN(left); |
2155 | 0 | size_t len = left_len + ZSTR_LEN(right) + 1; /* left\right */ |
2156 | |
|
2157 | 0 | result = zend_string_extend(left, len, 0); |
2158 | 0 | ZSTR_VAL(result)[left_len] = '\\'; |
2159 | 0 | memcpy(&ZSTR_VAL(result)[left_len + 1], ZSTR_VAL(right), ZSTR_LEN(right)); |
2160 | 0 | ZSTR_VAL(result)[len] = '\0'; |
2161 | 0 | zend_string_release_ex(right, 0); |
2162 | |
|
2163 | 0 | ZVAL_STR(left_zv, result); |
2164 | 0 | return left_ast; |
2165 | 0 | } |
2166 | | /* }}} */ |
2167 | | |
2168 | | zend_ast *zend_negate_num_string(zend_ast *ast) /* {{{ */ |
2169 | 1.23k | { |
2170 | 1.23k | zval *zv = zend_ast_get_zval(ast); |
2171 | 1.23k | if (Z_TYPE_P(zv) == IS_LONG) { |
2172 | 891 | if (Z_LVAL_P(zv) == 0) { |
2173 | 375 | ZVAL_NEW_STR(zv, ZSTR_INIT_LITERAL("-0", 0)); |
2174 | 516 | } else { |
2175 | 516 | ZEND_ASSERT(Z_LVAL_P(zv) > 0); |
2176 | 516 | Z_LVAL_P(zv) *= -1; |
2177 | 516 | } |
2178 | 891 | } else if (Z_TYPE_P(zv) == IS_STRING) { |
2179 | 347 | size_t orig_len = Z_STRLEN_P(zv); |
2180 | 347 | Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), orig_len + 1, 0); |
2181 | 347 | memmove(Z_STRVAL_P(zv) + 1, Z_STRVAL_P(zv), orig_len + 1); |
2182 | 347 | Z_STRVAL_P(zv)[0] = '-'; |
2183 | 347 | } else { |
2184 | 0 | ZEND_UNREACHABLE(); |
2185 | 0 | } |
2186 | 1.23k | return ast; |
2187 | 1.23k | } |
2188 | | /* }}} */ |
2189 | | |
2190 | | static void zend_verify_namespace(void) /* {{{ */ |
2191 | 510k | { |
2192 | 510k | if (FC(has_bracketed_namespaces) && !FC(in_namespace)) { |
2193 | 73 | zend_error_noreturn(E_COMPILE_ERROR, "No code may exist outside of namespace {}"); |
2194 | 73 | } |
2195 | 510k | } |
2196 | | /* }}} */ |
2197 | | |
2198 | | /* {{{ zend_dirname |
2199 | | Returns directory name component of path */ |
2200 | | ZEND_API size_t zend_dirname(char *path, size_t len) |
2201 | 1.34k | { |
2202 | 1.34k | char *end = path + len - 1; |
2203 | 1.34k | unsigned int len_adjust = 0; |
2204 | | |
2205 | | #ifdef ZEND_WIN32 |
2206 | | /* Note that on Win32 CWD is per drive (heritage from CP/M). |
2207 | | * This means dirname("c:foo") maps to "c:." or "c:" - which means CWD on C: drive. |
2208 | | */ |
2209 | | if ((2 <= len) && isalpha((unsigned char)path[0]) && (':' == path[1])) { |
2210 | | /* Skip over the drive spec (if any) so as not to change */ |
2211 | | path += 2; |
2212 | | len_adjust += 2; |
2213 | | if (2 == len) { |
2214 | | /* Return "c:" on Win32 for dirname("c:"). |
2215 | | * It would be more consistent to return "c:." |
2216 | | * but that would require making the string *longer*. |
2217 | | */ |
2218 | | return len; |
2219 | | } |
2220 | | } |
2221 | | #endif |
2222 | | |
2223 | 1.34k | if (len == 0) { |
2224 | | /* Illegal use of this function */ |
2225 | 0 | return 0; |
2226 | 0 | } |
2227 | | |
2228 | | /* Strip trailing slashes */ |
2229 | 1.34k | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2230 | 0 | end--; |
2231 | 0 | } |
2232 | 1.34k | if (end < path) { |
2233 | | /* The path only contained slashes */ |
2234 | 0 | path[0] = DEFAULT_SLASH; |
2235 | 0 | path[1] = '\0'; |
2236 | 0 | return 1 + len_adjust; |
2237 | 0 | } |
2238 | | |
2239 | | /* Strip filename */ |
2240 | 14.7k | while (end >= path && !IS_SLASH_P_EX(end, end == path)) { |
2241 | 13.4k | end--; |
2242 | 13.4k | } |
2243 | 1.34k | if (end < path) { |
2244 | | /* No slash found, therefore return '.' */ |
2245 | 728 | path[0] = '.'; |
2246 | 728 | path[1] = '\0'; |
2247 | 728 | return 1 + len_adjust; |
2248 | 728 | } |
2249 | | |
2250 | | /* Strip slashes which came before the file name */ |
2251 | 1.22k | while (end >= path && IS_SLASH_P_EX(end, end == path)) { |
2252 | 613 | end--; |
2253 | 613 | } |
2254 | 613 | if (end < path) { |
2255 | 5 | path[0] = DEFAULT_SLASH; |
2256 | 5 | path[1] = '\0'; |
2257 | 5 | return 1 + len_adjust; |
2258 | 5 | } |
2259 | 608 | *(end+1) = '\0'; |
2260 | | |
2261 | 608 | return (size_t)(end + 1 - path) + len_adjust; |
2262 | 613 | } |
2263 | | /* }}} */ |
2264 | | |
2265 | | static void zend_adjust_for_fetch_type(zend_op *opline, znode *result, uint32_t type) /* {{{ */ |
2266 | 1.21M | { |
2267 | 1.21M | uint_fast8_t factor = (opline->opcode == ZEND_FETCH_STATIC_PROP_R) ? 1 : 3; |
2268 | | |
2269 | 1.21M | switch (type) { |
2270 | 461k | case BP_VAR_R: |
2271 | 461k | opline->result_type = IS_TMP_VAR; |
2272 | 461k | result->op_type = IS_TMP_VAR; |
2273 | 461k | return; |
2274 | 363k | case BP_VAR_W: |
2275 | 363k | opline->opcode += 1 * factor; |
2276 | 363k | return; |
2277 | 21.5k | case BP_VAR_RW: |
2278 | 21.5k | opline->opcode += 2 * factor; |
2279 | 21.5k | return; |
2280 | 311k | case BP_VAR_IS: |
2281 | 311k | opline->result_type = IS_TMP_VAR; |
2282 | 311k | result->op_type = IS_TMP_VAR; |
2283 | 311k | opline->opcode += 3 * factor; |
2284 | 311k | return; |
2285 | 55.7k | case BP_VAR_FUNC_ARG: |
2286 | 55.7k | opline->opcode += 4 * factor; |
2287 | 55.7k | return; |
2288 | 5.95k | case BP_VAR_UNSET: |
2289 | 5.95k | opline->opcode += 5 * factor; |
2290 | 5.95k | return; |
2291 | 0 | default: ZEND_UNREACHABLE(); |
2292 | 1.21M | } |
2293 | 1.21M | } |
2294 | | /* }}} */ |
2295 | | |
2296 | | static inline void zend_make_var_result(znode *result, zend_op *opline) /* {{{ */ |
2297 | 12.3M | { |
2298 | 12.3M | opline->result_type = IS_VAR; |
2299 | 12.3M | opline->result.var = get_temporary_variable(); |
2300 | 12.3M | GET_NODE(result, opline->result); |
2301 | 12.3M | } |
2302 | | /* }}} */ |
2303 | | |
2304 | | static inline void zend_make_tmp_result(znode *result, zend_op *opline) /* {{{ */ |
2305 | 19.3M | { |
2306 | 19.3M | opline->result_type = IS_TMP_VAR; |
2307 | 19.3M | opline->result.var = get_temporary_variable(); |
2308 | 19.3M | GET_NODE(result, opline->result); |
2309 | 19.3M | } |
2310 | | /* }}} */ |
2311 | | |
2312 | | static zend_op *zend_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2313 | 36.4M | { |
2314 | 36.4M | zend_op *opline = get_next_op(); |
2315 | 36.4M | opline->opcode = opcode; |
2316 | | |
2317 | 36.4M | if (op1 != NULL) { |
2318 | 22.9M | SET_NODE(opline->op1, op1); |
2319 | 22.9M | } |
2320 | | |
2321 | 36.4M | if (op2 != NULL) { |
2322 | 1.62M | SET_NODE(opline->op2, op2); |
2323 | 1.62M | } |
2324 | | |
2325 | 36.4M | if (result) { |
2326 | 11.2M | zend_make_var_result(result, opline); |
2327 | 11.2M | } |
2328 | 36.4M | return opline; |
2329 | 36.4M | } |
2330 | | /* }}} */ |
2331 | | |
2332 | | static zend_op *zend_emit_op_tmp(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2333 | 21.0M | { |
2334 | 21.0M | zend_op *opline = get_next_op(); |
2335 | 21.0M | opline->opcode = opcode; |
2336 | | |
2337 | 21.0M | if (op1 != NULL) { |
2338 | 8.06M | SET_NODE(opline->op1, op1); |
2339 | 8.06M | } |
2340 | | |
2341 | 21.0M | if (op2 != NULL) { |
2342 | 3.89M | SET_NODE(opline->op2, op2); |
2343 | 3.89M | } |
2344 | | |
2345 | 21.0M | if (result) { |
2346 | 19.2M | zend_make_tmp_result(result, opline); |
2347 | 19.2M | } |
2348 | | |
2349 | 21.0M | return opline; |
2350 | 21.0M | } |
2351 | | /* }}} */ |
2352 | | |
2353 | | static void zend_emit_tick(void) /* {{{ */ |
2354 | 48.2k | { |
2355 | 48.2k | zend_op *opline; |
2356 | | |
2357 | | /* This prevents a double TICK generated by the parser statement of "declare()" */ |
2358 | 48.2k | if (CG(active_op_array)->last && CG(active_op_array)->opcodes[CG(active_op_array)->last - 1].opcode == ZEND_TICKS) { |
2359 | 5.05k | return; |
2360 | 5.05k | } |
2361 | | |
2362 | 43.2k | opline = get_next_op(); |
2363 | | |
2364 | 43.2k | opline->opcode = ZEND_TICKS; |
2365 | 43.2k | opline->extended_value = FC(declarables).ticks; |
2366 | 43.2k | } |
2367 | | /* }}} */ |
2368 | | |
2369 | | static inline zend_op *zend_emit_op_data(znode *value) /* {{{ */ |
2370 | 229k | { |
2371 | 229k | return zend_emit_op(NULL, ZEND_OP_DATA, value, NULL); |
2372 | 229k | } |
2373 | | /* }}} */ |
2374 | | |
2375 | | static inline uint32_t zend_emit_jump(uint32_t opnum_target) /* {{{ */ |
2376 | 701k | { |
2377 | 701k | uint32_t opnum = get_next_op_number(); |
2378 | 701k | zend_op *opline = zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
2379 | 701k | opline->op1.opline_num = opnum_target; |
2380 | 701k | return opnum; |
2381 | 701k | } |
2382 | | /* }}} */ |
2383 | | |
2384 | | ZEND_API bool zend_is_smart_branch(const zend_op *opline) /* {{{ */ |
2385 | 144k | { |
2386 | 144k | switch (opline->opcode) { |
2387 | 4.07k | case ZEND_IS_IDENTICAL: |
2388 | 4.80k | case ZEND_IS_NOT_IDENTICAL: |
2389 | 20.2k | case ZEND_IS_EQUAL: |
2390 | 22.6k | case ZEND_IS_NOT_EQUAL: |
2391 | 66.6k | case ZEND_IS_SMALLER: |
2392 | 69.6k | case ZEND_IS_SMALLER_OR_EQUAL: |
2393 | 74.7k | case ZEND_CASE: |
2394 | 76.8k | case ZEND_CASE_STRICT: |
2395 | 77.0k | case ZEND_ISSET_ISEMPTY_CV: |
2396 | 77.2k | case ZEND_ISSET_ISEMPTY_VAR: |
2397 | 77.8k | case ZEND_ISSET_ISEMPTY_DIM_OBJ: |
2398 | 78.0k | case ZEND_ISSET_ISEMPTY_PROP_OBJ: |
2399 | 78.2k | case ZEND_ISSET_ISEMPTY_STATIC_PROP: |
2400 | 78.4k | case ZEND_INSTANCEOF: |
2401 | 79.0k | case ZEND_TYPE_CHECK: |
2402 | 79.3k | case ZEND_DEFINED: |
2403 | 79.4k | case ZEND_IN_ARRAY: |
2404 | 79.4k | case ZEND_ARRAY_KEY_EXISTS: |
2405 | 79.4k | return 1; |
2406 | 65.0k | default: |
2407 | 65.0k | return 0; |
2408 | 144k | } |
2409 | 144k | } |
2410 | | /* }}} */ |
2411 | | |
2412 | | static inline uint32_t zend_emit_cond_jump(uint8_t opcode, znode *cond, uint32_t opnum_target) /* {{{ */ |
2413 | 140k | { |
2414 | 140k | uint32_t opnum = get_next_op_number(); |
2415 | 140k | zend_op *opline; |
2416 | | |
2417 | 140k | if (cond->op_type == IS_TMP_VAR && opnum > 0) { |
2418 | 94.2k | opline = CG(active_op_array)->opcodes + opnum - 1; |
2419 | 94.2k | if (opline->result_type == IS_TMP_VAR |
2420 | 91.3k | && opline->result.var == cond->u.op.var |
2421 | 91.3k | && zend_is_smart_branch(opline)) { |
2422 | 79.2k | if (opcode == ZEND_JMPZ) { |
2423 | 55.8k | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPZ; |
2424 | 55.8k | } else { |
2425 | 23.3k | ZEND_ASSERT(opcode == ZEND_JMPNZ); |
2426 | 23.3k | opline->result_type = IS_TMP_VAR | IS_SMART_BRANCH_JMPNZ; |
2427 | 23.3k | } |
2428 | 79.2k | } |
2429 | 94.2k | } |
2430 | 140k | opline = zend_emit_op(NULL, opcode, cond, NULL); |
2431 | 140k | opline->op2.opline_num = opnum_target; |
2432 | 140k | return opnum; |
2433 | 140k | } |
2434 | | /* }}} */ |
2435 | | |
2436 | | static inline void zend_update_jump_target(uint32_t opnum_jump, uint32_t opnum_target) /* {{{ */ |
2437 | 898k | { |
2438 | 898k | zend_op *opline = &CG(active_op_array)->opcodes[opnum_jump]; |
2439 | 898k | switch (opline->opcode) { |
2440 | 687k | case ZEND_JMP: |
2441 | 687k | opline->op1.opline_num = opnum_target; |
2442 | 687k | break; |
2443 | 105k | case ZEND_JMPZ: |
2444 | 122k | case ZEND_JMPNZ: |
2445 | 129k | case ZEND_JMPZ_EX: |
2446 | 134k | case ZEND_JMPNZ_EX: |
2447 | 139k | case ZEND_JMP_SET: |
2448 | 209k | case ZEND_COALESCE: |
2449 | 209k | case ZEND_JMP_NULL: |
2450 | 210k | case ZEND_BIND_INIT_STATIC_OR_JMP: |
2451 | 210k | case ZEND_JMP_FRAMELESS: |
2452 | 210k | opline->op2.opline_num = opnum_target; |
2453 | 210k | break; |
2454 | 0 | default: ZEND_UNREACHABLE(); |
2455 | 898k | } |
2456 | 898k | } |
2457 | | /* }}} */ |
2458 | | |
2459 | | static inline void zend_update_jump_target_to_next(uint32_t opnum_jump) /* {{{ */ |
2460 | 896k | { |
2461 | 896k | zend_update_jump_target(opnum_jump, get_next_op_number()); |
2462 | 896k | } |
2463 | | /* }}} */ |
2464 | | |
2465 | | static inline zend_op *zend_delayed_emit_op(znode *result, uint8_t opcode, znode *op1, znode *op2) /* {{{ */ |
2466 | 1.00M | { |
2467 | 1.00M | zend_op tmp_opline; |
2468 | | |
2469 | 1.00M | init_op(&tmp_opline); |
2470 | | |
2471 | 1.00M | tmp_opline.opcode = opcode; |
2472 | 1.00M | if (op1 != NULL) { |
2473 | 1.00M | SET_NODE(tmp_opline.op1, op1); |
2474 | 1.00M | } |
2475 | 1.00M | if (op2 != NULL) { |
2476 | 979k | SET_NODE(tmp_opline.op2, op2); |
2477 | 979k | } |
2478 | 1.00M | if (result) { |
2479 | 1.00M | zend_make_var_result(result, &tmp_opline); |
2480 | 1.00M | } |
2481 | | |
2482 | 1.00M | zend_stack_push(&CG(delayed_oplines_stack), &tmp_opline); |
2483 | 1.00M | return zend_stack_top(&CG(delayed_oplines_stack)); |
2484 | 1.00M | } |
2485 | | /* }}} */ |
2486 | | |
2487 | | static inline uint32_t zend_delayed_compile_begin(void) /* {{{ */ |
2488 | 833k | { |
2489 | 833k | return zend_stack_count(&CG(delayed_oplines_stack)); |
2490 | 833k | } |
2491 | | /* }}} */ |
2492 | | |
2493 | | static zend_op *zend_delayed_compile_end(uint32_t offset) /* {{{ */ |
2494 | 832k | { |
2495 | 832k | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
2496 | 832k | uint32_t i, count = zend_stack_count(&CG(delayed_oplines_stack)); |
2497 | | |
2498 | 832k | ZEND_ASSERT(count >= offset); |
2499 | 1.83M | for (i = offset; i < count; ++i) { |
2500 | 1.00M | if (EXPECTED(oplines[i].opcode != ZEND_NOP)) { |
2501 | 943k | opline = get_next_op(); |
2502 | 943k | memcpy(opline, &oplines[i], sizeof(zend_op)); |
2503 | 943k | } else { |
2504 | 61.9k | opline = CG(active_op_array)->opcodes + oplines[i].extended_value; |
2505 | 61.9k | } |
2506 | 1.00M | } |
2507 | | |
2508 | 832k | CG(delayed_oplines_stack).top = offset; |
2509 | 832k | return opline; |
2510 | 832k | } |
2511 | | /* }}} */ |
2512 | | |
2513 | | static bool zend_ast_kind_is_short_circuited(zend_ast_kind ast_kind) |
2514 | 44.1M | { |
2515 | 44.1M | switch (ast_kind) { |
2516 | 422k | case ZEND_AST_DIM: |
2517 | 938k | case ZEND_AST_PROP: |
2518 | 1.06M | case ZEND_AST_NULLSAFE_PROP: |
2519 | 1.08M | case ZEND_AST_STATIC_PROP: |
2520 | 1.48M | case ZEND_AST_METHOD_CALL: |
2521 | 1.48M | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2522 | 1.52M | case ZEND_AST_STATIC_CALL: |
2523 | 1.52M | return true; |
2524 | 42.6M | default: |
2525 | 42.6M | return false; |
2526 | 44.1M | } |
2527 | 44.1M | } |
2528 | | |
2529 | | static bool zend_ast_is_short_circuited(const zend_ast *ast) |
2530 | 1.50M | { |
2531 | 1.50M | switch (ast->kind) { |
2532 | 209k | case ZEND_AST_DIM: |
2533 | 434k | case ZEND_AST_PROP: |
2534 | 444k | case ZEND_AST_STATIC_PROP: |
2535 | 498k | case ZEND_AST_METHOD_CALL: |
2536 | 500k | case ZEND_AST_STATIC_CALL: |
2537 | 500k | return zend_ast_is_short_circuited(ast->child[0]); |
2538 | 441 | case ZEND_AST_NULLSAFE_PROP: |
2539 | 599 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2540 | 599 | return true; |
2541 | 1.00M | default: |
2542 | 1.00M | return false; |
2543 | 1.50M | } |
2544 | 1.50M | } |
2545 | | |
2546 | | static void zend_assert_not_short_circuited(const zend_ast *ast) |
2547 | 237k | { |
2548 | 237k | if (zend_ast_is_short_circuited(ast)) { |
2549 | 37 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot take reference of a nullsafe chain"); |
2550 | 37 | } |
2551 | 237k | } |
2552 | | |
2553 | | /* Mark nodes that are an inner part of a short-circuiting chain. |
2554 | | * We should not perform a "commit" on them, as it will be performed by the outer-most node. |
2555 | | * We do this to avoid passing down an argument in various compile functions. */ |
2556 | | |
2557 | 1.53M | #define ZEND_SHORT_CIRCUITING_INNER 0x8000 |
2558 | | |
2559 | 1.12M | static void zend_short_circuiting_mark_inner(zend_ast *ast) { |
2560 | 1.12M | if (zend_ast_kind_is_short_circuited(ast->kind)) { |
2561 | 724k | ast->attr |= ZEND_SHORT_CIRCUITING_INNER; |
2562 | 724k | } |
2563 | 1.12M | } |
2564 | | |
2565 | | static uint32_t zend_short_circuiting_checkpoint(void) |
2566 | 43.1M | { |
2567 | 43.1M | return zend_stack_count(&CG(short_circuiting_opnums)); |
2568 | 43.1M | } |
2569 | | |
2570 | | static void zend_short_circuiting_commit(uint32_t checkpoint, znode *result, const zend_ast *ast) |
2571 | 43.0M | { |
2572 | 43.0M | bool is_short_circuited = zend_ast_kind_is_short_circuited(ast->kind) |
2573 | 42.2M | || ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY; |
2574 | 43.0M | if (!is_short_circuited) { |
2575 | 42.2M | ZEND_ASSERT(zend_stack_count(&CG(short_circuiting_opnums)) == checkpoint |
2576 | 42.2M | && "Short circuiting stack should be empty"); |
2577 | 42.2M | return; |
2578 | 42.2M | } |
2579 | | |
2580 | 807k | if (ast->attr & ZEND_SHORT_CIRCUITING_INNER) { |
2581 | | /* Outer-most node will commit. */ |
2582 | 189k | return; |
2583 | 189k | } |
2584 | | |
2585 | 703k | while (zend_stack_count(&CG(short_circuiting_opnums)) != checkpoint) { |
2586 | 85.0k | uint32_t opnum = *(uint32_t *) zend_stack_top(&CG(short_circuiting_opnums)); |
2587 | 85.0k | zend_op *opline = &CG(active_op_array)->opcodes[opnum]; |
2588 | 85.0k | opline->op2.opline_num = get_next_op_number(); |
2589 | 85.0k | SET_NODE(opline->result, result); |
2590 | 85.0k | opline->extended_value |= |
2591 | 85.0k | ast->kind == ZEND_AST_ISSET ? ZEND_SHORT_CIRCUITING_CHAIN_ISSET : |
2592 | 85.0k | ast->kind == ZEND_AST_EMPTY ? ZEND_SHORT_CIRCUITING_CHAIN_EMPTY : |
2593 | 84.8k | ZEND_SHORT_CIRCUITING_CHAIN_EXPR; |
2594 | 85.0k | zend_stack_del_top(&CG(short_circuiting_opnums)); |
2595 | 85.0k | } |
2596 | 618k | } |
2597 | | |
2598 | | /* Same value as ZEND_SHORT_CIRCUITING_INNER, AST kinds must not clash. */ |
2599 | 112k | #define ZEND_CONST_OBJECT_FETCH 0x8000 |
2600 | | |
2601 | | static void zend_mark_const_object_fetch(zend_ast *ast) |
2602 | 557k | { |
2603 | 559k | while (ast->kind == ZEND_AST_DIM) { |
2604 | 2.09k | ast = ast->child[0]; |
2605 | 2.09k | } |
2606 | | |
2607 | 557k | if (ast->kind == ZEND_AST_CONST || ast->kind == ZEND_AST_CLASS_CONST) { |
2608 | 35.0k | ast->attr |= ZEND_CONST_OBJECT_FETCH; |
2609 | 35.0k | } |
2610 | 557k | } |
2611 | | |
2612 | | static void zend_emit_jmp_null(znode *obj_node, uint32_t bp_type) |
2613 | 85.0k | { |
2614 | 85.0k | uint32_t jmp_null_opnum = get_next_op_number(); |
2615 | 85.0k | zend_op *opline = zend_emit_op(NULL, ZEND_JMP_NULL, obj_node, NULL); |
2616 | 85.0k | if (opline->op1_type == IS_CONST) { |
2617 | 1.49k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
2618 | 1.49k | } |
2619 | 85.0k | if (bp_type == BP_VAR_IS) { |
2620 | 6.01k | opline->extended_value |= ZEND_JMP_NULL_BP_VAR_IS; |
2621 | 6.01k | } |
2622 | 85.0k | zend_stack_push(&CG(short_circuiting_opnums), &jmp_null_opnum); |
2623 | 85.0k | } |
2624 | | |
2625 | | static inline bool zend_is_variable_or_call(const zend_ast *ast); |
2626 | | |
2627 | | static void zend_compile_memoized_expr(znode *result, zend_ast *expr, uint32_t type) /* {{{ */ |
2628 | 704k | { |
2629 | 704k | const zend_memoize_mode memoize_mode = CG(memoize_mode); |
2630 | 704k | if (memoize_mode == ZEND_MEMOIZE_COMPILE) { |
2631 | 352k | znode memoized_result; |
2632 | | |
2633 | | /* Go through normal compilation */ |
2634 | 352k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
2635 | 352k | if (zend_is_variable_or_call(expr)) { |
2636 | 59.7k | zend_compile_var(result, expr, type, /* by_ref */ false); |
2637 | 292k | } else { |
2638 | 292k | zend_compile_expr(result, expr); |
2639 | 292k | } |
2640 | 352k | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
2641 | | |
2642 | 352k | if (result->op_type == IS_VAR) { |
2643 | 55.9k | zend_emit_op(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2644 | 296k | } else if (result->op_type == IS_TMP_VAR) { |
2645 | 107k | zend_emit_op_tmp(&memoized_result, ZEND_COPY_TMP, result, NULL); |
2646 | 188k | } else { |
2647 | 188k | if (result->op_type == IS_CONST) { |
2648 | 187k | Z_TRY_ADDREF(result->u.constant); |
2649 | 187k | } |
2650 | 188k | memoized_result = *result; |
2651 | 188k | } |
2652 | | |
2653 | 352k | zend_hash_index_update_mem( |
2654 | 352k | CG(memoized_exprs), (uintptr_t) expr, &memoized_result, sizeof(znode)); |
2655 | 352k | } else if (memoize_mode == ZEND_MEMOIZE_FETCH) { |
2656 | 351k | const znode *memoized_result = zend_hash_index_find_ptr(CG(memoized_exprs), (uintptr_t) expr); |
2657 | 351k | *result = *memoized_result; |
2658 | 351k | if (result->op_type == IS_CONST) { |
2659 | 187k | Z_TRY_ADDREF(result->u.constant); |
2660 | 187k | } |
2661 | 351k | } else { |
2662 | 0 | ZEND_UNREACHABLE(); |
2663 | 0 | } |
2664 | 704k | } |
2665 | | /* }}} */ |
2666 | | |
2667 | | static void zend_emit_return_type_check( |
2668 | | znode *expr, const zend_arg_info *return_info, bool implicit) /* {{{ */ |
2669 | 84.2k | { |
2670 | 84.2k | zend_type type = return_info->type; |
2671 | 84.2k | if (ZEND_TYPE_IS_SET(type)) { |
2672 | 84.2k | zend_op *opline; |
2673 | | |
2674 | | /* `return ...;` is illegal in a void function (but `return;` isn't) */ |
2675 | 84.2k | if (ZEND_TYPE_CONTAINS_CODE(type, IS_VOID)) { |
2676 | 5.31k | if (expr) { |
2677 | 18 | if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) { |
2678 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
2679 | 7 | "A void %s must not return a value " |
2680 | 7 | "(did you mean \"return;\" instead of \"return null;\"?)", |
2681 | 7 | CG(active_class_entry) != NULL ? "method" : "function"); |
2682 | 11 | } else { |
2683 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "A void %s must not return a value", |
2684 | 11 | CG(active_class_entry) != NULL ? "method" : "function"); |
2685 | 11 | } |
2686 | 18 | } |
2687 | | /* we don't need run-time check */ |
2688 | 5.29k | return; |
2689 | 5.31k | } |
2690 | | |
2691 | | /* `return` is illegal in a never-returning function */ |
2692 | 78.9k | if (ZEND_TYPE_CONTAINS_CODE(type, IS_NEVER)) { |
2693 | | /* Implicit case handled separately using VERIFY_NEVER_TYPE opcode. */ |
2694 | 8 | ZEND_ASSERT(!implicit); |
2695 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "A never-returning %s must not return", |
2696 | 8 | CG(active_class_entry) != NULL ? "method" : "function"); |
2697 | 8 | } |
2698 | | |
2699 | 78.9k | if (!expr && !implicit) { |
2700 | 10 | if (ZEND_TYPE_ALLOW_NULL(type)) { |
2701 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
2702 | 5 | "A %s with return type must return a value " |
2703 | 5 | "(did you mean \"return null;\" instead of \"return;\"?)", |
2704 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
2705 | 5 | } else { |
2706 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
2707 | 5 | "A %s with return type must return a value", |
2708 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
2709 | 5 | } |
2710 | 10 | } |
2711 | | |
2712 | 78.9k | if (expr && ZEND_TYPE_PURE_MASK(type) == MAY_BE_ANY) { |
2713 | | /* we don't need run-time check for mixed return type */ |
2714 | 623 | return; |
2715 | 623 | } |
2716 | | |
2717 | 78.3k | if (expr && expr->op_type == IS_CONST && ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE(expr->u.constant))) { |
2718 | | /* we don't need run-time check */ |
2719 | 2.86k | return; |
2720 | 2.86k | } |
2721 | | |
2722 | 75.4k | opline = zend_emit_op(NULL, ZEND_VERIFY_RETURN_TYPE, expr, NULL); |
2723 | 75.4k | if (expr && expr->op_type == IS_CONST) { |
2724 | 485 | opline->result_type = expr->op_type = IS_TMP_VAR; |
2725 | 485 | opline->result.var = expr->u.op.var = get_temporary_variable(); |
2726 | 485 | } |
2727 | 75.4k | } |
2728 | 84.2k | } |
2729 | | /* }}} */ |
2730 | | |
2731 | | void zend_emit_final_return(bool return_one) /* {{{ */ |
2732 | 1.56M | { |
2733 | 1.56M | znode zn; |
2734 | 1.56M | zend_op *ret; |
2735 | 1.56M | bool returns_reference = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
2736 | | |
2737 | 1.56M | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) |
2738 | 76.2k | && !(CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR)) { |
2739 | 75.6k | zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
2740 | | |
2741 | 75.6k | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
2742 | 563 | zend_emit_op(NULL, ZEND_VERIFY_NEVER_TYPE, NULL, NULL); |
2743 | 563 | return; |
2744 | 563 | } |
2745 | | |
2746 | 75.0k | zend_emit_return_type_check(NULL, return_info, true); |
2747 | 75.0k | } |
2748 | | |
2749 | 1.56M | zn.op_type = IS_CONST; |
2750 | 1.56M | if (return_one) { |
2751 | 86.7k | ZVAL_LONG(&zn.u.constant, 1); |
2752 | 1.47M | } else { |
2753 | 1.47M | ZVAL_NULL(&zn.u.constant); |
2754 | 1.47M | } |
2755 | | |
2756 | 1.56M | ret = zend_emit_op(NULL, returns_reference ? ZEND_RETURN_BY_REF : ZEND_RETURN, &zn, NULL); |
2757 | 1.56M | ret->extended_value = -1; |
2758 | 1.56M | } |
2759 | | /* }}} */ |
2760 | | |
2761 | | static inline bool zend_is_variable(const zend_ast *ast) /* {{{ */ |
2762 | 4.65M | { |
2763 | 4.65M | return ast->kind == ZEND_AST_VAR |
2764 | 4.50M | || ast->kind == ZEND_AST_DIM |
2765 | 4.44M | || ast->kind == ZEND_AST_PROP |
2766 | 4.43M | || ast->kind == ZEND_AST_NULLSAFE_PROP |
2767 | 4.43M | || ast->kind == ZEND_AST_STATIC_PROP; |
2768 | 4.65M | } |
2769 | | /* }}} */ |
2770 | | |
2771 | | static bool zend_propagate_list_refs(zend_ast *ast); |
2772 | | |
2773 | | static inline bool zend_is_passable_by_ref(const zend_ast *ast) |
2774 | 3.96M | { |
2775 | 3.96M | if (zend_is_variable(ast) || ast->kind == ZEND_AST_ASSIGN_REF) { |
2776 | 150k | return true; |
2777 | 150k | } |
2778 | 3.81M | if (ast->kind == ZEND_AST_ASSIGN |
2779 | 13.2k | && UNEXPECTED(ast->child[0]->kind == ZEND_AST_ARRAY) |
2780 | 61 | && zend_propagate_list_refs(ast->child[0])) { |
2781 | 28 | return true; |
2782 | 28 | } |
2783 | 3.81M | return false; |
2784 | 3.81M | } |
2785 | | |
2786 | | static inline bool zend_is_call(const zend_ast *ast) /* {{{ */ |
2787 | 5.36M | { |
2788 | 5.36M | return ast->kind == ZEND_AST_CALL |
2789 | 4.74M | || ast->kind == ZEND_AST_METHOD_CALL |
2790 | 4.62M | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
2791 | 4.62M | || ast->kind == ZEND_AST_STATIC_CALL |
2792 | 4.62M | || ast->kind == ZEND_AST_PIPE; |
2793 | 5.36M | } |
2794 | | /* }}} */ |
2795 | | |
2796 | | static inline bool zend_is_variable_or_call(const zend_ast *ast) /* {{{ */ |
2797 | 618k | { |
2798 | 618k | return zend_is_variable(ast) || zend_is_call(ast); |
2799 | 618k | } |
2800 | | /* }}} */ |
2801 | | |
2802 | | static inline bool zend_is_unticked_stmt(const zend_ast *ast) /* {{{ */ |
2803 | 59.9k | { |
2804 | 59.9k | return ast->kind == ZEND_AST_STMT_LIST || ast->kind == ZEND_AST_LABEL |
2805 | 49.0k | || ast->kind == ZEND_AST_PROP_DECL || ast->kind == ZEND_AST_CLASS_CONST_GROUP |
2806 | 48.6k | || ast->kind == ZEND_AST_USE_TRAIT || ast->kind == ZEND_AST_METHOD; |
2807 | 59.9k | } |
2808 | | /* }}} */ |
2809 | | |
2810 | | static inline bool zend_can_write_to_variable(const zend_ast *ast) /* {{{ */ |
2811 | 22.0k | { |
2812 | 22.0k | bool via_prop = false; |
2813 | | |
2814 | 22.0k | while ( |
2815 | 26.4k | ast->kind == ZEND_AST_DIM |
2816 | 24.8k | || ast->kind == ZEND_AST_PROP |
2817 | 22.0k | ) { |
2818 | 4.43k | via_prop |= ast->kind == ZEND_AST_PROP; |
2819 | 4.43k | ast = ast->child[0]; |
2820 | 4.43k | } |
2821 | | |
2822 | 22.0k | if ((ast->kind == ZEND_AST_CONST || ast->kind == ZEND_AST_CLASS_CONST) && via_prop) { |
2823 | 384 | return true; |
2824 | 384 | } |
2825 | | |
2826 | 21.6k | return zend_is_variable_or_call(ast) && !zend_ast_is_short_circuited(ast); |
2827 | 22.0k | } |
2828 | | /* }}} */ |
2829 | | |
2830 | | static inline bool zend_is_const_default_class_ref(zend_ast *name_ast) /* {{{ */ |
2831 | 42.2k | { |
2832 | 42.2k | if (name_ast->kind != ZEND_AST_ZVAL) { |
2833 | 0 | return false; |
2834 | 0 | } |
2835 | | |
2836 | 42.2k | return ZEND_FETCH_CLASS_DEFAULT == zend_get_class_fetch_type_ast(name_ast); |
2837 | 42.2k | } |
2838 | | /* }}} */ |
2839 | | |
2840 | | static inline void zend_handle_numeric_op(znode *node) /* {{{ */ |
2841 | 12.8k | { |
2842 | 12.8k | if (node->op_type == IS_CONST && Z_TYPE(node->u.constant) == IS_STRING) { |
2843 | 10.6k | zend_ulong index; |
2844 | | |
2845 | 10.6k | if (ZEND_HANDLE_NUMERIC(Z_STR(node->u.constant), index)) { |
2846 | 3.27k | zval_ptr_dtor(&node->u.constant); |
2847 | 3.27k | ZVAL_LONG(&node->u.constant, index); |
2848 | 3.27k | } |
2849 | 10.6k | } |
2850 | 12.8k | } |
2851 | | /* }}} */ |
2852 | | |
2853 | | static inline void zend_handle_numeric_dim(const zend_op *opline, znode *dim_node) /* {{{ */ |
2854 | 125k | { |
2855 | 125k | if (Z_TYPE(dim_node->u.constant) == IS_STRING) { |
2856 | 86.3k | zend_ulong index; |
2857 | | |
2858 | 86.3k | if (ZEND_HANDLE_NUMERIC(Z_STR(dim_node->u.constant), index)) { |
2859 | | /* For numeric indexes we also keep the original value to use by ArrayAccess |
2860 | | * See bug #63217 |
2861 | | */ |
2862 | 15.3k | int c = zend_add_literal(&dim_node->u.constant); |
2863 | 15.3k | ZEND_ASSERT(opline->op2.constant + 1 == c); |
2864 | 15.3k | ZVAL_LONG(CT_CONSTANT(opline->op2), index); |
2865 | 15.3k | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = ZEND_EXTRA_VALUE; |
2866 | 15.3k | return; |
2867 | 15.3k | } |
2868 | 86.3k | } |
2869 | 125k | } |
2870 | | /* }}} */ |
2871 | | |
2872 | | static inline void zend_set_class_name_op1(zend_op *opline, znode *class_node) /* {{{ */ |
2873 | 242k | { |
2874 | 242k | if (class_node->op_type == IS_CONST) { |
2875 | 111k | opline->op1_type = IS_CONST; |
2876 | 111k | opline->op1.constant = zend_add_class_name_literal( |
2877 | 111k | Z_STR(class_node->u.constant)); |
2878 | 131k | } else { |
2879 | 131k | SET_NODE(opline->op1, class_node); |
2880 | 131k | } |
2881 | 242k | } |
2882 | | /* }}} */ |
2883 | | |
2884 | | static void zend_compile_class_ref(znode *result, zend_ast *name_ast, uint32_t fetch_flags) /* {{{ */ |
2885 | 230k | { |
2886 | 230k | uint32_t fetch_type; |
2887 | | |
2888 | 230k | if (name_ast->kind != ZEND_AST_ZVAL) { |
2889 | 104k | znode name_node; |
2890 | | |
2891 | 104k | zend_compile_expr(&name_node, name_ast); |
2892 | | |
2893 | 104k | if (name_node.op_type == IS_CONST) { |
2894 | 2.70k | zend_string *name; |
2895 | | |
2896 | 2.70k | if (Z_TYPE(name_node.u.constant) != IS_STRING) { |
2897 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal class name"); |
2898 | 5 | } |
2899 | | |
2900 | 2.69k | name = Z_STR(name_node.u.constant); |
2901 | 2.69k | fetch_type = zend_get_class_fetch_type(name); |
2902 | | |
2903 | 2.69k | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
2904 | 2.44k | result->op_type = IS_CONST; |
2905 | 2.44k | ZVAL_STR(&result->u.constant, zend_resolve_class_name(name, ZEND_NAME_FQ)); |
2906 | 2.44k | } else { |
2907 | 257 | zend_ensure_valid_class_fetch_type(fetch_type); |
2908 | 257 | result->op_type = IS_UNUSED; |
2909 | 257 | result->u.op.num = fetch_type | fetch_flags; |
2910 | 257 | } |
2911 | | |
2912 | 2.69k | zend_string_release_ex(name, 0); |
2913 | 101k | } else { |
2914 | 101k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_CLASS, NULL, &name_node); |
2915 | 101k | opline->op1.num = ZEND_FETCH_CLASS_DEFAULT | fetch_flags; |
2916 | 101k | } |
2917 | 104k | return; |
2918 | 104k | } |
2919 | | |
2920 | | /* Fully qualified names are always default refs */ |
2921 | 126k | if (name_ast->attr == ZEND_NAME_FQ) { |
2922 | 4.77k | result->op_type = IS_CONST; |
2923 | 4.77k | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2924 | 4.77k | return; |
2925 | 4.77k | } |
2926 | | |
2927 | 121k | fetch_type = zend_get_class_fetch_type(zend_ast_get_str(name_ast)); |
2928 | 121k | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
2929 | 114k | result->op_type = IS_CONST; |
2930 | 114k | ZVAL_STR(&result->u.constant, zend_resolve_class_name_ast(name_ast)); |
2931 | 114k | } else { |
2932 | 6.49k | zend_ensure_valid_class_fetch_type(fetch_type); |
2933 | 6.49k | result->op_type = IS_UNUSED; |
2934 | 6.49k | result->u.op.num = fetch_type | fetch_flags; |
2935 | 6.49k | } |
2936 | 121k | } |
2937 | | /* }}} */ |
2938 | | |
2939 | | static zend_result zend_try_compile_cv(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
2940 | 1.36M | { |
2941 | 1.36M | zend_ast *name_ast = ast->child[0]; |
2942 | 1.36M | if (name_ast->kind == ZEND_AST_ZVAL) { |
2943 | 1.24M | zval *zv = zend_ast_get_zval(name_ast); |
2944 | 1.24M | zend_string *name; |
2945 | | |
2946 | 1.24M | if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) { |
2947 | 1.21M | name = zval_make_interned_string(zv); |
2948 | 1.21M | } else { |
2949 | 21.1k | name = zend_new_interned_string(zval_get_string_func(zv)); |
2950 | 21.1k | } |
2951 | | |
2952 | 1.24M | if (zend_is_auto_global(name)) { |
2953 | 997 | return FAILURE; |
2954 | 997 | } |
2955 | | |
2956 | 1.23M | if (!CG(context).has_assigned_to_http_response_header && zend_string_equals_literal(name, "http_response_header")) { |
2957 | 165 | if (type == BP_VAR_R) { |
2958 | 74 | zend_error(E_DEPRECATED, |
2959 | 74 | "The predefined locally scoped $http_response_header variable is deprecated," |
2960 | 74 | " call http_get_last_response_headers() instead"); |
2961 | 91 | } else if (type == BP_VAR_W) { |
2962 | 1 | CG(context).has_assigned_to_http_response_header = true; |
2963 | 1 | } |
2964 | 165 | } |
2965 | | |
2966 | 1.23M | result->op_type = IS_CV; |
2967 | 1.23M | result->u.op.var = lookup_cv(name); |
2968 | | |
2969 | 1.23M | if (UNEXPECTED(Z_TYPE_P(zv) != IS_STRING)) { |
2970 | 21.1k | zend_string_release_ex(name, 0); |
2971 | 21.1k | } |
2972 | | |
2973 | 1.23M | return SUCCESS; |
2974 | 1.24M | } |
2975 | | |
2976 | 127k | return FAILURE; |
2977 | 1.36M | } |
2978 | | /* }}} */ |
2979 | | |
2980 | | static zend_op *zend_compile_simple_var_no_cv(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
2981 | 220k | { |
2982 | 220k | zend_ast *name_ast = ast->child[0]; |
2983 | 220k | znode name_node; |
2984 | 220k | zend_op *opline; |
2985 | | |
2986 | 220k | zend_compile_expr(&name_node, name_ast); |
2987 | 220k | if (name_node.op_type == IS_CONST) { |
2988 | 96.6k | convert_to_string(&name_node.u.constant); |
2989 | 96.6k | } |
2990 | | |
2991 | 220k | if (delayed) { |
2992 | 16.9k | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2993 | 203k | } else { |
2994 | 203k | opline = zend_emit_op(result, ZEND_FETCH_R, &name_node, NULL); |
2995 | 203k | } |
2996 | | |
2997 | 220k | if (name_node.op_type == IS_CONST && |
2998 | 96.6k | zend_is_auto_global(Z_STR(name_node.u.constant))) { |
2999 | | |
3000 | 977 | opline->extended_value = ZEND_FETCH_GLOBAL; |
3001 | 219k | } else { |
3002 | 219k | if (name_node.op_type == IS_CONST |
3003 | 95.6k | && type == BP_VAR_R |
3004 | 94.9k | && zend_string_equals_literal(Z_STR(name_node.u.constant), "http_response_header")) { |
3005 | 228 | zend_error(E_DEPRECATED, |
3006 | 228 | "The predefined locally scoped $http_response_header variable is deprecated," |
3007 | 228 | " call http_get_last_response_headers() instead"); |
3008 | 228 | } |
3009 | 219k | opline->extended_value = ZEND_FETCH_LOCAL; |
3010 | 219k | } |
3011 | | |
3012 | 220k | zend_adjust_for_fetch_type(opline, result, type); |
3013 | 220k | return opline; |
3014 | 220k | } |
3015 | | /* }}} */ |
3016 | | |
3017 | | static bool is_this_fetch(const zend_ast *ast) /* {{{ */ |
3018 | 2.53M | { |
3019 | 2.53M | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
3020 | 1.74M | const zval *name = zend_ast_get_zval(ast->child[0]); |
3021 | 1.74M | return Z_TYPE_P(name) == IS_STRING && zend_string_equals(Z_STR_P(name), ZSTR_KNOWN(ZEND_STR_THIS)); |
3022 | 1.74M | } |
3023 | | |
3024 | 790k | return false; |
3025 | 2.53M | } |
3026 | | /* }}} */ |
3027 | | |
3028 | | static bool is_globals_fetch(const zend_ast *ast) |
3029 | 6.60M | { |
3030 | 6.60M | if (ast->kind == ZEND_AST_VAR && ast->child[0]->kind == ZEND_AST_ZVAL) { |
3031 | 1.84M | const zval *name = zend_ast_get_zval(ast->child[0]); |
3032 | 1.84M | return Z_TYPE_P(name) == IS_STRING && zend_string_equals_literal(Z_STR_P(name), "GLOBALS"); |
3033 | 1.84M | } |
3034 | | |
3035 | 4.75M | return false; |
3036 | 6.60M | } |
3037 | | |
3038 | | static bool is_global_var_fetch(const zend_ast *ast) |
3039 | 572k | { |
3040 | 572k | return ast->kind == ZEND_AST_DIM && is_globals_fetch(ast->child[0]); |
3041 | 572k | } |
3042 | | |
3043 | | static bool this_guaranteed_exists(void) /* {{{ */ |
3044 | 23.0k | { |
3045 | 23.0k | const zend_oparray_context *ctx = &CG(context); |
3046 | 75.8k | while (ctx) { |
3047 | | /* Instance methods always have a $this. |
3048 | | * This also includes closures that have a scope and use $this. */ |
3049 | 75.8k | const zend_op_array *op_array = ctx->op_array; |
3050 | 75.8k | if (op_array->fn_flags & ZEND_ACC_STATIC) { |
3051 | 513 | return false; |
3052 | 75.3k | } else if (op_array->scope) { |
3053 | 15.2k | return true; |
3054 | 60.1k | } else if (!(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
3055 | 7.33k | return false; |
3056 | 7.33k | } |
3057 | 52.7k | ctx = ctx->prev; |
3058 | 52.7k | } |
3059 | 0 | return false; |
3060 | 23.0k | } |
3061 | | /* }}} */ |
3062 | | |
3063 | | static zend_op *zend_compile_simple_var(znode *result, const zend_ast *ast, uint32_t type, bool delayed) /* {{{ */ |
3064 | 1.33M | { |
3065 | 1.33M | if (is_this_fetch(ast)) { |
3066 | 7.00k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_THIS, NULL, NULL); |
3067 | 7.00k | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3068 | 6.77k | opline->result_type = IS_TMP_VAR; |
3069 | 6.77k | result->op_type = IS_TMP_VAR; |
3070 | 6.77k | } |
3071 | 7.00k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3072 | 7.00k | return opline; |
3073 | 1.32M | } else if (is_globals_fetch(ast)) { |
3074 | 2.38k | zend_op *opline = zend_emit_op(result, ZEND_FETCH_GLOBALS, NULL, NULL); |
3075 | 2.38k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3076 | 2.35k | opline->result_type = IS_TMP_VAR; |
3077 | 2.35k | result->op_type = IS_TMP_VAR; |
3078 | 2.35k | } |
3079 | 2.38k | return opline; |
3080 | 1.32M | } else if (zend_try_compile_cv(result, ast, type) == FAILURE) { |
3081 | 126k | return zend_compile_simple_var_no_cv(result, ast, type, delayed); |
3082 | 126k | } |
3083 | 1.19M | return NULL; |
3084 | 1.33M | } |
3085 | | /* }}} */ |
3086 | | |
3087 | | static void zend_separate_if_call_and_write(znode *node, const zend_ast *ast, uint32_t type) /* {{{ */ |
3088 | 961k | { |
3089 | 961k | if (type != BP_VAR_R |
3090 | 718k | && type != BP_VAR_IS |
3091 | | /* Whether a FUNC_ARG is R may only be determined at runtime. */ |
3092 | 412k | && type != BP_VAR_FUNC_ARG |
3093 | 358k | && zend_is_call(ast)) { |
3094 | 57.8k | if (node->op_type == IS_VAR) { |
3095 | 57.8k | zend_op *opline = zend_emit_op(NULL, ZEND_SEPARATE, node, NULL); |
3096 | 57.8k | opline->result_type = IS_VAR; |
3097 | 57.8k | opline->result.var = opline->op1.var; |
3098 | 57.8k | } else { |
3099 | 17 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3100 | 17 | } |
3101 | 57.8k | } |
3102 | 961k | } |
3103 | | /* }}} */ |
3104 | | |
3105 | | static inline void zend_emit_assign_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3106 | 6.97k | { |
3107 | 6.97k | znode dummy_node; |
3108 | 6.97k | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN, var_ast, |
3109 | 6.97k | zend_ast_create_znode(value_node)); |
3110 | 6.97k | zend_compile_expr(&dummy_node, assign_ast); |
3111 | 6.97k | zend_do_free(&dummy_node); |
3112 | 6.97k | } |
3113 | | /* }}} */ |
3114 | | |
3115 | | static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
3116 | 405k | { |
3117 | 405k | zend_ast *var_ast = ast->child[0]; |
3118 | 405k | zend_ast *dim_ast = ast->child[1]; |
3119 | 405k | zend_op *opline; |
3120 | | |
3121 | 405k | znode var_node, dim_node; |
3122 | | |
3123 | 405k | if (is_globals_fetch(var_ast)) { |
3124 | 3.22k | if (dim_ast == NULL) { |
3125 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot append to $GLOBALS"); |
3126 | 8 | } |
3127 | | |
3128 | 3.22k | zend_compile_expr(&dim_node, dim_ast); |
3129 | 3.22k | if (dim_node.op_type == IS_CONST) { |
3130 | 3.05k | convert_to_string(&dim_node.u.constant); |
3131 | 3.05k | } |
3132 | | |
3133 | 3.22k | opline = zend_delayed_emit_op(result, ZEND_FETCH_R, &dim_node, NULL); |
3134 | 3.22k | opline->extended_value = ZEND_FETCH_GLOBAL; |
3135 | 3.22k | zend_adjust_for_fetch_type(opline, result, type); |
3136 | 3.22k | return opline; |
3137 | 402k | } else { |
3138 | 402k | zend_short_circuiting_mark_inner(var_ast); |
3139 | 402k | opline = zend_delayed_compile_var(&var_node, var_ast, type, false); |
3140 | 402k | if (opline) { |
3141 | 161k | if (type == BP_VAR_W && (opline->opcode == ZEND_FETCH_STATIC_PROP_W || opline->opcode == ZEND_FETCH_OBJ_W)) { |
3142 | 3.76k | opline->extended_value |= ZEND_FETCH_DIM_WRITE; |
3143 | 157k | } else if (opline->opcode == ZEND_FETCH_DIM_W |
3144 | 106k | || opline->opcode == ZEND_FETCH_DIM_RW |
3145 | 105k | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3146 | 91.0k | || opline->opcode == ZEND_FETCH_DIM_UNSET) { |
3147 | 67.0k | opline->extended_value = ZEND_FETCH_DIM_DIM; |
3148 | 67.0k | } |
3149 | 161k | } |
3150 | 402k | } |
3151 | | |
3152 | 402k | zend_separate_if_call_and_write(&var_node, var_ast, type); |
3153 | | |
3154 | 402k | if (dim_ast == NULL) { |
3155 | 44.3k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
3156 | 214 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
3157 | 214 | } |
3158 | 44.1k | if (type == BP_VAR_UNSET) { |
3159 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
3160 | 8 | } |
3161 | 44.0k | dim_node.op_type = IS_UNUSED; |
3162 | 358k | } else { |
3163 | 358k | zend_compile_expr(&dim_node, dim_ast); |
3164 | 358k | } |
3165 | | |
3166 | 402k | opline = zend_delayed_emit_op(result, ZEND_FETCH_DIM_R, &var_node, &dim_node); |
3167 | 402k | zend_adjust_for_fetch_type(opline, result, type); |
3168 | 402k | if (by_ref) { |
3169 | 41.7k | opline->extended_value = ZEND_FETCH_DIM_REF; |
3170 | 41.7k | } |
3171 | | |
3172 | 402k | if (dim_node.op_type == IS_CONST) { |
3173 | 118k | zend_handle_numeric_dim(opline, &dim_node); |
3174 | 118k | } |
3175 | 402k | return opline; |
3176 | 402k | } |
3177 | | |
3178 | | static zend_op *zend_compile_dim(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3179 | 223k | { |
3180 | 223k | uint32_t offset = zend_delayed_compile_begin(); |
3181 | 223k | zend_delayed_compile_dim(result, ast, type, by_ref); |
3182 | 223k | return zend_delayed_compile_end(offset); |
3183 | 223k | } |
3184 | | /* }}} */ |
3185 | | |
3186 | | static zend_op *zend_delayed_compile_prop(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3187 | 577k | { |
3188 | 577k | zend_ast *obj_ast = ast->child[0]; |
3189 | 577k | zend_ast *prop_ast = ast->child[1]; |
3190 | | |
3191 | 577k | znode obj_node, prop_node; |
3192 | 577k | zend_op *opline; |
3193 | 577k | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_PROP; |
3194 | | |
3195 | 577k | if (is_this_fetch(obj_ast)) { |
3196 | 20.0k | if (this_guaranteed_exists()) { |
3197 | 13.2k | obj_node.op_type = IS_UNUSED; |
3198 | 13.2k | } else { |
3199 | 6.85k | opline = zend_emit_op(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
3200 | 6.85k | if ((type == BP_VAR_R) || (type == BP_VAR_IS)) { |
3201 | 6.61k | opline->result_type = IS_TMP_VAR; |
3202 | 6.61k | obj_node.op_type = IS_TMP_VAR; |
3203 | 6.61k | } |
3204 | 6.85k | } |
3205 | 20.0k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3206 | | |
3207 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
3208 | | * check for a nullsafe access. */ |
3209 | 557k | } else { |
3210 | 557k | zend_short_circuiting_mark_inner(obj_ast); |
3211 | 557k | zend_mark_const_object_fetch(obj_ast); |
3212 | 557k | opline = zend_delayed_compile_var(&obj_node, obj_ast, type, false); |
3213 | 557k | if (opline && (opline->opcode == ZEND_FETCH_DIM_W |
3214 | 438k | || opline->opcode == ZEND_FETCH_DIM_RW |
3215 | 438k | || opline->opcode == ZEND_FETCH_DIM_FUNC_ARG |
3216 | 438k | || opline->opcode == ZEND_FETCH_DIM_UNSET)) { |
3217 | 914 | opline->extended_value = ZEND_FETCH_DIM_OBJ; |
3218 | 914 | } |
3219 | | |
3220 | 557k | zend_separate_if_call_and_write(&obj_node, obj_ast, type); |
3221 | 557k | if (nullsafe) { |
3222 | 81.4k | if (obj_node.op_type == IS_TMP_VAR) { |
3223 | | /* Flush delayed oplines */ |
3224 | 52.5k | zend_op *opline = NULL, *oplines = zend_stack_base(&CG(delayed_oplines_stack)); |
3225 | 52.5k | uint32_t var = obj_node.u.op.var; |
3226 | 52.5k | uint32_t count = zend_stack_count(&CG(delayed_oplines_stack)); |
3227 | 52.5k | uint32_t i = count; |
3228 | | |
3229 | 495k | while (i > 0 && oplines[i-1].result_type == IS_TMP_VAR && oplines[i-1].result.var == var) { |
3230 | 444k | i--; |
3231 | 444k | if (oplines[i].op1_type == IS_TMP_VAR) { |
3232 | 443k | var = oplines[i].op1.var; |
3233 | 443k | } else { |
3234 | 1.30k | break; |
3235 | 1.30k | } |
3236 | 444k | } |
3237 | 497k | for (; i < count; ++i) { |
3238 | 444k | if (oplines[i].opcode != ZEND_NOP) { |
3239 | 61.9k | opline = get_next_op(); |
3240 | 61.9k | memcpy(opline, &oplines[i], sizeof(zend_op)); |
3241 | 61.9k | oplines[i].opcode = ZEND_NOP; |
3242 | 61.9k | oplines[i].extended_value = opline - CG(active_op_array)->opcodes; |
3243 | 61.9k | } |
3244 | 444k | } |
3245 | 52.5k | } |
3246 | 81.4k | zend_emit_jmp_null(&obj_node, type); |
3247 | 81.4k | } |
3248 | 557k | } |
3249 | | |
3250 | 577k | zend_compile_expr(&prop_node, prop_ast); |
3251 | | |
3252 | 577k | opline = zend_delayed_emit_op(result, ZEND_FETCH_OBJ_R, &obj_node, &prop_node); |
3253 | 577k | if (opline->op2_type == IS_CONST) { |
3254 | 571k | convert_to_string(CT_CONSTANT(opline->op2)); |
3255 | 571k | zend_string_hash_val(Z_STR_P(CT_CONSTANT(opline->op2))); |
3256 | 571k | opline->extended_value = zend_alloc_cache_slots(3); |
3257 | 571k | } |
3258 | | |
3259 | 577k | zend_adjust_for_fetch_type(opline, result, type); |
3260 | | |
3261 | 577k | return opline; |
3262 | 577k | } |
3263 | | /* }}} */ |
3264 | | |
3265 | | static zend_op *zend_compile_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
3266 | 117k | { |
3267 | 117k | uint32_t offset = zend_delayed_compile_begin(); |
3268 | 117k | zend_op *opline = zend_delayed_compile_prop(result, ast, type); |
3269 | 117k | if (by_ref) { /* shared with cache_slot */ |
3270 | 4.65k | opline->extended_value |= ZEND_FETCH_REF; |
3271 | 4.65k | } |
3272 | 117k | return zend_delayed_compile_end(offset); |
3273 | 117k | } |
3274 | | /* }}} */ |
3275 | | |
3276 | | static zend_op *zend_compile_static_prop(znode *result, zend_ast *ast, uint32_t type, bool by_ref, bool delayed) /* {{{ */ |
3277 | 17.1k | { |
3278 | 17.1k | zend_ast *class_ast = ast->child[0]; |
3279 | 17.1k | zend_ast *prop_ast = ast->child[1]; |
3280 | | |
3281 | 17.1k | znode class_node, prop_node; |
3282 | 17.1k | zend_op *opline; |
3283 | | |
3284 | 17.1k | zend_short_circuiting_mark_inner(class_ast); |
3285 | 17.1k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
3286 | | |
3287 | 17.1k | zend_compile_expr(&prop_node, prop_ast); |
3288 | | |
3289 | 17.1k | if (delayed) { |
3290 | 6.60k | opline = zend_delayed_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3291 | 10.5k | } else { |
3292 | 10.5k | opline = zend_emit_op(result, ZEND_FETCH_STATIC_PROP_R, &prop_node, NULL); |
3293 | 10.5k | } |
3294 | 17.1k | if (opline->op1_type == IS_CONST) { |
3295 | 15.6k | convert_to_string(CT_CONSTANT(opline->op1)); |
3296 | 15.6k | opline->extended_value = zend_alloc_cache_slots(3); |
3297 | 15.6k | } |
3298 | 17.1k | if (class_node.op_type == IS_CONST) { |
3299 | 9.81k | opline->op2_type = IS_CONST; |
3300 | 9.81k | opline->op2.constant = zend_add_class_name_literal( |
3301 | 9.81k | Z_STR(class_node.u.constant)); |
3302 | 9.81k | if (opline->op1_type != IS_CONST) { |
3303 | 1.09k | opline->extended_value = zend_alloc_cache_slot(); |
3304 | 1.09k | } |
3305 | 9.81k | } else { |
3306 | 7.35k | SET_NODE(opline->op2, &class_node); |
3307 | 7.35k | } |
3308 | | |
3309 | 17.1k | if (by_ref && (type == BP_VAR_W || type == BP_VAR_FUNC_ARG)) { /* shared with cache_slot */ |
3310 | 1.86k | opline->extended_value |= ZEND_FETCH_REF; |
3311 | 1.86k | } |
3312 | | |
3313 | 17.1k | zend_adjust_for_fetch_type(opline, result, type); |
3314 | 17.1k | return opline; |
3315 | 17.1k | } |
3316 | | /* }}} */ |
3317 | | |
3318 | 7.35k | static void zend_verify_list_assign_target(const zend_ast *var_ast, zend_ast_attr array_style) /* {{{ */ { |
3319 | 7.35k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3320 | 477 | if (var_ast->attr == ZEND_ARRAY_SYNTAX_LONG) { |
3321 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot assign to array(), use [] instead"); |
3322 | 5 | } |
3323 | 472 | if (array_style != var_ast->attr) { |
3324 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix [] and list()"); |
3325 | 5 | } |
3326 | 6.87k | } else if (!zend_can_write_to_variable(var_ast)) { |
3327 | 120 | zend_error_noreturn(E_COMPILE_ERROR, "Assignments can only happen to writable values"); |
3328 | 120 | } |
3329 | 7.35k | } |
3330 | | /* }}} */ |
3331 | | |
3332 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node); |
3333 | | |
3334 | | /* Propagate refs used on leaf elements to the surrounding list() structures. */ |
3335 | 5.09k | static bool zend_propagate_list_refs(zend_ast *ast) { /* {{{ */ |
3336 | 5.09k | const zend_ast_list *list = zend_ast_get_list(ast); |
3337 | 5.09k | bool has_refs = false; |
3338 | 5.09k | uint32_t i; |
3339 | | |
3340 | 13.4k | for (i = 0; i < list->children; ++i) { |
3341 | 8.38k | zend_ast *elem_ast = list->child[i]; |
3342 | | |
3343 | 8.38k | if (elem_ast) { |
3344 | 7.85k | zend_ast *var_ast = elem_ast->child[0]; |
3345 | 7.85k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3346 | 486 | elem_ast->attr = zend_propagate_list_refs(var_ast); |
3347 | 486 | } |
3348 | 7.85k | has_refs |= elem_ast->attr; |
3349 | 7.85k | } |
3350 | 8.38k | } |
3351 | | |
3352 | 5.09k | return has_refs; |
3353 | 5.09k | } |
3354 | | /* }}} */ |
3355 | | |
3356 | | static bool list_is_keyed(const zend_ast_list *list) |
3357 | 4.55k | { |
3358 | 4.86k | for (uint32_t i = 0; i < list->children; i++) { |
3359 | 4.83k | const zend_ast *child = list->child[i]; |
3360 | 4.83k | if (child) { |
3361 | 4.52k | return child->kind == ZEND_AST_ARRAY_ELEM && child->child[1] != NULL; |
3362 | 4.52k | } |
3363 | 4.83k | } |
3364 | 36 | return false; |
3365 | 4.55k | } |
3366 | | |
3367 | | static void zend_compile_list_assign( |
3368 | | znode *result, zend_ast *ast, znode *expr_node, zend_ast_attr array_style, uint32_t type) /* {{{ */ |
3369 | 4.55k | { |
3370 | 4.55k | zend_ast_list *list = zend_ast_get_list(ast); |
3371 | 4.55k | uint32_t i; |
3372 | 4.55k | bool has_elems = false; |
3373 | 4.55k | bool is_keyed = list_is_keyed(list); |
3374 | | |
3375 | 4.55k | if (list->children && expr_node->op_type == IS_CONST && Z_TYPE(expr_node->u.constant) == IS_STRING) { |
3376 | 178 | zval_make_interned_string(&expr_node->u.constant); |
3377 | 178 | } |
3378 | | |
3379 | 12.4k | for (i = 0; i < list->children; ++i) { |
3380 | 7.90k | zend_ast *elem_ast = list->child[i]; |
3381 | 7.90k | zend_ast *var_ast, *key_ast; |
3382 | 7.90k | znode fetch_result, dim_node; |
3383 | 7.90k | zend_op *opline; |
3384 | | |
3385 | 7.90k | if (elem_ast == NULL) { |
3386 | 521 | if (is_keyed) { |
3387 | 9 | zend_error(E_COMPILE_ERROR, |
3388 | 9 | "Cannot use empty array entries in keyed array assignment"); |
3389 | 512 | } else { |
3390 | 512 | continue; |
3391 | 512 | } |
3392 | 521 | } |
3393 | | |
3394 | 7.39k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
3395 | 9 | zend_error(E_COMPILE_ERROR, |
3396 | 9 | "Spread operator is not supported in assignments"); |
3397 | 9 | } |
3398 | | |
3399 | 7.39k | var_ast = elem_ast->child[0]; |
3400 | 7.39k | key_ast = elem_ast->child[1]; |
3401 | 7.39k | has_elems = true; |
3402 | | |
3403 | 7.39k | if (is_keyed) { |
3404 | 1.06k | if (key_ast == NULL) { |
3405 | 7 | zend_error(E_COMPILE_ERROR, |
3406 | 7 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3407 | 7 | } |
3408 | | |
3409 | 1.06k | zend_compile_expr(&dim_node, key_ast); |
3410 | 6.33k | } else { |
3411 | 6.33k | if (key_ast != NULL) { |
3412 | 7 | zend_error(E_COMPILE_ERROR, |
3413 | 7 | "Cannot mix keyed and unkeyed array entries in assignments"); |
3414 | 7 | } |
3415 | | |
3416 | 6.33k | dim_node.op_type = IS_CONST; |
3417 | 6.33k | ZVAL_LONG(&dim_node.u.constant, i); |
3418 | 6.33k | } |
3419 | | |
3420 | 7.39k | if (expr_node->op_type == IS_CONST) { |
3421 | 931 | Z_TRY_ADDREF(expr_node->u.constant); |
3422 | 931 | } |
3423 | | |
3424 | 7.39k | zend_verify_list_assign_target(var_ast, array_style); |
3425 | | |
3426 | 7.39k | opline = zend_emit_op(&fetch_result, |
3427 | 7.39k | 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); |
3428 | 7.39k | if (opline->opcode == ZEND_FETCH_LIST_R) { |
3429 | 4.87k | opline->result_type = IS_TMP_VAR; |
3430 | 4.87k | fetch_result.op_type = IS_TMP_VAR; |
3431 | 4.87k | } |
3432 | | |
3433 | 7.39k | if (dim_node.op_type == IS_CONST) { |
3434 | 6.92k | zend_handle_numeric_dim(opline, &dim_node); |
3435 | 6.92k | } |
3436 | | |
3437 | 7.39k | if (elem_ast->attr) { |
3438 | 2.35k | zend_emit_op(&fetch_result, ZEND_MAKE_REF, &fetch_result, NULL); |
3439 | 2.35k | } |
3440 | 7.39k | if (var_ast->kind == ZEND_AST_ARRAY) { |
3441 | 467 | zend_compile_list_assign(NULL, var_ast, &fetch_result, var_ast->attr, type); |
3442 | 6.92k | } else if (elem_ast->attr) { |
3443 | 2.17k | zend_emit_assign_ref_znode(var_ast, &fetch_result); |
3444 | 4.74k | } else { |
3445 | 4.74k | zend_emit_assign_znode(var_ast, &fetch_result); |
3446 | 4.74k | } |
3447 | 7.39k | } |
3448 | | |
3449 | 4.55k | if (has_elems == 0) { |
3450 | 36 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use empty list"); |
3451 | 36 | } |
3452 | | |
3453 | 4.52k | if (result) { |
3454 | 472 | if ((type == BP_VAR_R || type == BP_VAR_IS) && expr_node->op_type == IS_VAR) { |
3455 | | /* Deref. */ |
3456 | 177 | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, expr_node, NULL); |
3457 | 295 | } else { |
3458 | 295 | *result = *expr_node; |
3459 | 295 | } |
3460 | 4.05k | } else { |
3461 | 4.05k | zend_do_free(expr_node); |
3462 | 4.05k | } |
3463 | 4.52k | } |
3464 | | /* }}} */ |
3465 | | |
3466 | | static void zend_ensure_writable_variable(const zend_ast *ast) /* {{{ */ |
3467 | 597k | { |
3468 | 597k | if (ast->kind == ZEND_AST_CALL || ast->kind == ZEND_AST_PIPE) { |
3469 | 91 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use function return value in write context"); |
3470 | 91 | } |
3471 | 597k | if ( |
3472 | 597k | ast->kind == ZEND_AST_METHOD_CALL |
3473 | 597k | || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL |
3474 | 597k | || ast->kind == ZEND_AST_STATIC_CALL |
3475 | 597k | ) { |
3476 | 15 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use method return value in write context"); |
3477 | 15 | } |
3478 | 597k | if (zend_ast_is_short_circuited(ast)) { |
3479 | 33 | zend_error_noreturn(E_COMPILE_ERROR, "Can't use nullsafe operator in write context"); |
3480 | 33 | } |
3481 | 597k | if (is_globals_fetch(ast)) { |
3482 | 33 | zend_error_noreturn(E_COMPILE_ERROR, |
3483 | 33 | "$GLOBALS can only be modified using the $GLOBALS[$name] = $value syntax"); |
3484 | 33 | } |
3485 | 597k | } |
3486 | | /* }}} */ |
3487 | | |
3488 | | /* Detects $a... = $a pattern */ |
3489 | | static bool zend_is_assign_to_self(const zend_ast *var_ast, const zend_ast *expr_ast) /* {{{ */ |
3490 | 89.4k | { |
3491 | 89.4k | if (expr_ast->kind != ZEND_AST_VAR || expr_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3492 | 77.6k | return false; |
3493 | 77.6k | } |
3494 | | |
3495 | 25.7k | while (zend_is_variable(var_ast) && var_ast->kind != ZEND_AST_VAR) { |
3496 | 13.9k | var_ast = var_ast->child[0]; |
3497 | 13.9k | } |
3498 | | |
3499 | 11.8k | if (var_ast->kind != ZEND_AST_VAR || var_ast->child[0]->kind != ZEND_AST_ZVAL) { |
3500 | 843 | return false; |
3501 | 843 | } |
3502 | | |
3503 | 10.9k | { |
3504 | 10.9k | zend_string *name1 = zval_get_string(zend_ast_get_zval(var_ast->child[0])); |
3505 | 10.9k | zend_string *name2 = zval_get_string(zend_ast_get_zval(expr_ast->child[0])); |
3506 | 10.9k | bool result = zend_string_equals(name1, name2); |
3507 | 10.9k | zend_string_release_ex(name1, 0); |
3508 | 10.9k | zend_string_release_ex(name2, 0); |
3509 | 10.9k | return result; |
3510 | 11.8k | } |
3511 | 11.8k | } |
3512 | | /* }}} */ |
3513 | | |
3514 | | static void zend_compile_expr_with_potential_assign_to_self( |
3515 | 89.4k | znode *expr_node, zend_ast *expr_ast, const zend_ast *var_ast) { |
3516 | 89.4k | if (zend_is_assign_to_self(var_ast, expr_ast) && !is_this_fetch(expr_ast)) { |
3517 | | /* $a[0] = $a should evaluate the right $a first */ |
3518 | 724 | znode cv_node; |
3519 | | |
3520 | 724 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3521 | 124 | zend_compile_simple_var_no_cv(expr_node, expr_ast, BP_VAR_R, false); |
3522 | 600 | } else { |
3523 | 600 | zend_emit_op_tmp(expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3524 | 600 | } |
3525 | 88.6k | } else { |
3526 | 88.6k | zend_compile_expr(expr_node, expr_ast); |
3527 | 88.6k | } |
3528 | 89.4k | } |
3529 | | |
3530 | | static void zend_compile_assign(znode *result, zend_ast *ast, bool stmt, uint32_t type) /* {{{ */ |
3531 | 372k | { |
3532 | 372k | zend_ast *var_ast = ast->child[0]; |
3533 | 372k | zend_ast *expr_ast = ast->child[1]; |
3534 | | |
3535 | 372k | znode var_node, expr_node; |
3536 | 372k | zend_op *opline; |
3537 | 372k | uint32_t offset; |
3538 | 372k | if (is_this_fetch(var_ast)) { |
3539 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3540 | 9 | } |
3541 | | |
3542 | 372k | zend_ensure_writable_variable(var_ast); |
3543 | | |
3544 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3545 | 372k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3546 | 372k | switch (kind) { |
3547 | 331k | case ZEND_AST_VAR: |
3548 | 331k | offset = zend_delayed_compile_begin(); |
3549 | 331k | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_W, false); |
3550 | 331k | zend_compile_expr(&expr_node, expr_ast); |
3551 | 331k | zend_delayed_compile_end(offset); |
3552 | 331k | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
3553 | 331k | zend_emit_op_tmp(result, ZEND_ASSIGN, &var_node, &expr_node); |
3554 | 331k | return; |
3555 | 1.87k | case ZEND_AST_STATIC_PROP: |
3556 | 1.87k | offset = zend_delayed_compile_begin(); |
3557 | 1.87k | zend_delayed_compile_var(result, var_ast, BP_VAR_W, false); |
3558 | 1.87k | zend_compile_expr(&expr_node, expr_ast); |
3559 | | |
3560 | 1.87k | opline = zend_delayed_compile_end(offset); |
3561 | 1.87k | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
3562 | 1.87k | opline->result_type = IS_TMP_VAR; |
3563 | 1.87k | result->op_type = IS_TMP_VAR; |
3564 | | |
3565 | 1.87k | zend_emit_op_data(&expr_node); |
3566 | 1.87k | return; |
3567 | 18.2k | case ZEND_AST_DIM: |
3568 | 18.2k | offset = zend_delayed_compile_begin(); |
3569 | 18.2k | zend_delayed_compile_dim(result, var_ast, BP_VAR_W, /* by_ref */ false); |
3570 | 18.2k | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3571 | | |
3572 | 18.2k | opline = zend_delayed_compile_end(offset); |
3573 | 18.2k | opline->opcode = ZEND_ASSIGN_DIM; |
3574 | 18.2k | opline->result_type = IS_TMP_VAR; |
3575 | 18.2k | result->op_type = IS_TMP_VAR; |
3576 | | |
3577 | 18.2k | opline = zend_emit_op_data(&expr_node); |
3578 | 18.2k | return; |
3579 | 16.7k | case ZEND_AST_PROP: |
3580 | 16.7k | case ZEND_AST_NULLSAFE_PROP: |
3581 | 16.7k | offset = zend_delayed_compile_begin(); |
3582 | 16.7k | zend_delayed_compile_prop(result, var_ast, BP_VAR_W); |
3583 | 16.7k | zend_compile_expr(&expr_node, expr_ast); |
3584 | | |
3585 | 16.7k | opline = zend_delayed_compile_end(offset); |
3586 | 16.7k | opline->opcode = ZEND_ASSIGN_OBJ; |
3587 | 16.7k | opline->result_type = IS_TMP_VAR; |
3588 | 16.7k | result->op_type = IS_TMP_VAR; |
3589 | | |
3590 | 16.7k | zend_emit_op_data(&expr_node); |
3591 | 16.7k | return; |
3592 | 4.25k | case ZEND_AST_ARRAY: |
3593 | 4.25k | if (zend_propagate_list_refs(var_ast)) { |
3594 | 1.98k | if (!zend_is_variable_or_call(expr_ast)) { |
3595 | 19 | zend_error_noreturn(E_COMPILE_ERROR, |
3596 | 19 | "Cannot assign reference to non referenceable value"); |
3597 | 1.96k | } else { |
3598 | 1.96k | zend_assert_not_short_circuited(expr_ast); |
3599 | 1.96k | } |
3600 | | |
3601 | 1.96k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
3602 | | /* MAKE_REF is usually not necessary for CVs. However, if there are |
3603 | | * self-assignments, this forces the RHS to evaluate first. */ |
3604 | 1.96k | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
3605 | 2.27k | } else { |
3606 | 2.27k | if (expr_ast->kind == ZEND_AST_VAR) { |
3607 | | /* list($a, $b) = $a should evaluate the right $a first */ |
3608 | 579 | znode cv_node; |
3609 | | |
3610 | 579 | if (zend_try_compile_cv(&cv_node, expr_ast, BP_VAR_R) == FAILURE) { |
3611 | 103 | zend_compile_simple_var_no_cv(&expr_node, expr_ast, BP_VAR_R, false); |
3612 | 476 | } else { |
3613 | 476 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &cv_node, NULL); |
3614 | 476 | } |
3615 | 1.69k | } else { |
3616 | 1.69k | zend_compile_expr(&expr_node, expr_ast); |
3617 | 1.69k | } |
3618 | 2.27k | } |
3619 | | |
3620 | 4.23k | zend_compile_list_assign(!stmt ? result : NULL, var_ast, &expr_node, var_ast->attr, type); |
3621 | 4.23k | if (stmt) { |
3622 | 3.11k | result->op_type = IS_UNUSED; |
3623 | 3.11k | } |
3624 | 4.23k | return; |
3625 | 0 | default: ZEND_UNREACHABLE(); |
3626 | 372k | } |
3627 | 372k | } |
3628 | | /* }}} */ |
3629 | | |
3630 | | static void zend_compile_assign_ref(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
3631 | 12.1k | { |
3632 | 12.1k | zend_ast *target_ast = ast->child[0]; |
3633 | 12.1k | zend_ast *source_ast = ast->child[1]; |
3634 | | |
3635 | 12.1k | znode target_node, source_node; |
3636 | 12.1k | zend_op *opline; |
3637 | 12.1k | uint32_t offset, flags; |
3638 | | |
3639 | 12.1k | if (is_this_fetch(target_ast)) { |
3640 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
3641 | 5 | } |
3642 | 12.1k | zend_ensure_writable_variable(target_ast); |
3643 | 12.1k | zend_assert_not_short_circuited(source_ast); |
3644 | 12.1k | if (is_globals_fetch(source_ast)) { |
3645 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot acquire reference to $GLOBALS"); |
3646 | 5 | } |
3647 | | |
3648 | 12.1k | offset = zend_delayed_compile_begin(); |
3649 | 12.1k | zend_delayed_compile_var(&target_node, target_ast, BP_VAR_W, true); |
3650 | 12.1k | zend_compile_var(&source_node, source_ast, BP_VAR_W, true); |
3651 | | |
3652 | 12.1k | if ((target_ast->kind != ZEND_AST_VAR |
3653 | 8.48k | || target_ast->child[0]->kind != ZEND_AST_ZVAL) |
3654 | 6.31k | && source_ast->kind != ZEND_AST_ZNODE |
3655 | 4.32k | && source_node.op_type != IS_CV) { |
3656 | | /* Both LHS and RHS expressions may modify the same data structure, |
3657 | | * and the modification during RHS evaluation may dangle the pointer |
3658 | | * to the result of the LHS evaluation. |
3659 | | * Use MAKE_REF instruction to replace direct pointer with REFERENCE. |
3660 | | * See: Bug #71539 |
3661 | | */ |
3662 | 1.82k | zend_emit_op(&source_node, ZEND_MAKE_REF, &source_node, NULL); |
3663 | 1.82k | } |
3664 | | |
3665 | 12.1k | opline = zend_delayed_compile_end(offset); |
3666 | | |
3667 | 12.1k | if (source_node.op_type != IS_VAR && zend_is_call(source_ast)) { |
3668 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use result of built-in function in write context"); |
3669 | 6 | } |
3670 | | |
3671 | 12.1k | flags = zend_is_call(source_ast) ? ZEND_RETURNS_FUNCTION : 0; |
3672 | | |
3673 | 12.1k | if (opline && opline->opcode == ZEND_FETCH_OBJ_W) { |
3674 | 1.27k | opline->opcode = ZEND_ASSIGN_OBJ_REF; |
3675 | 1.27k | opline->extended_value &= ~ZEND_FETCH_REF; |
3676 | 1.27k | opline->extended_value |= flags; |
3677 | 1.27k | if (result) { |
3678 | 209 | *result = target_node; |
3679 | 1.06k | } else { |
3680 | 1.06k | SET_UNUSED(opline->result); |
3681 | 1.06k | } |
3682 | 1.27k | zend_emit_op_data(&source_node); |
3683 | 10.9k | } else if (opline && opline->opcode == ZEND_FETCH_STATIC_PROP_W) { |
3684 | 898 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_REF; |
3685 | 898 | opline->extended_value &= ~ZEND_FETCH_REF; |
3686 | 898 | opline->extended_value |= flags; |
3687 | 898 | if (result) { |
3688 | 122 | *result = target_node; |
3689 | 776 | } else { |
3690 | 776 | SET_UNUSED(opline->result); |
3691 | 776 | } |
3692 | 898 | zend_emit_op_data(&source_node); |
3693 | 10.0k | } else { |
3694 | 10.0k | opline = zend_emit_op(result, ZEND_ASSIGN_REF, &target_node, &source_node); |
3695 | 10.0k | opline->extended_value = flags; |
3696 | 10.0k | } |
3697 | | |
3698 | 12.1k | if (result && (type == BP_VAR_R || type == BP_VAR_IS)) { |
3699 | | /* Deref. */ |
3700 | 2.69k | znode tmp_result = *result; |
3701 | 2.69k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &tmp_result, NULL); |
3702 | 2.69k | } |
3703 | 12.1k | } |
3704 | | /* }}} */ |
3705 | | |
3706 | | static inline void zend_emit_assign_ref_znode(zend_ast *var_ast, const znode *value_node) /* {{{ */ |
3707 | 2.94k | { |
3708 | 2.94k | zend_ast *assign_ast = zend_ast_create(ZEND_AST_ASSIGN_REF, var_ast, |
3709 | 2.94k | zend_ast_create_znode(value_node)); |
3710 | 2.94k | zend_compile_stmt(assign_ast); |
3711 | 2.94k | } |
3712 | | /* }}} */ |
3713 | | |
3714 | | static void zend_compile_compound_assign(znode *result, zend_ast *ast) /* {{{ */ |
3715 | 111k | { |
3716 | 111k | zend_ast *var_ast = ast->child[0]; |
3717 | 111k | zend_ast *expr_ast = ast->child[1]; |
3718 | 111k | uint32_t opcode = ast->attr; |
3719 | | |
3720 | 111k | znode var_node, expr_node; |
3721 | 111k | zend_op *opline; |
3722 | 111k | uint32_t offset, cache_slot; |
3723 | | |
3724 | 111k | zend_ensure_writable_variable(var_ast); |
3725 | | |
3726 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
3727 | 111k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
3728 | 111k | switch (kind) { |
3729 | 99.5k | case ZEND_AST_VAR: |
3730 | 99.5k | offset = zend_delayed_compile_begin(); |
3731 | 99.5k | zend_delayed_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
3732 | 99.5k | zend_compile_expr(&expr_node, expr_ast); |
3733 | 99.5k | zend_delayed_compile_end(offset); |
3734 | 99.5k | opline = zend_emit_op_tmp(result, ZEND_ASSIGN_OP, &var_node, &expr_node); |
3735 | 99.5k | opline->extended_value = opcode; |
3736 | 99.5k | return; |
3737 | 328 | case ZEND_AST_STATIC_PROP: |
3738 | 328 | offset = zend_delayed_compile_begin(); |
3739 | 328 | zend_delayed_compile_var(result, var_ast, BP_VAR_RW, false); |
3740 | 328 | zend_compile_expr(&expr_node, expr_ast); |
3741 | | |
3742 | 328 | opline = zend_delayed_compile_end(offset); |
3743 | 328 | cache_slot = opline->extended_value; |
3744 | 328 | opline->opcode = ZEND_ASSIGN_STATIC_PROP_OP; |
3745 | 328 | opline->extended_value = opcode; |
3746 | 328 | opline->result_type = IS_TMP_VAR; |
3747 | 328 | result->op_type = IS_TMP_VAR; |
3748 | | |
3749 | 328 | opline = zend_emit_op_data(&expr_node); |
3750 | 328 | opline->extended_value = cache_slot; |
3751 | 328 | return; |
3752 | 10.4k | case ZEND_AST_DIM: |
3753 | 10.4k | offset = zend_delayed_compile_begin(); |
3754 | 10.4k | zend_delayed_compile_dim(result, var_ast, BP_VAR_RW, /* by_ref */ false); |
3755 | 10.4k | zend_compile_expr_with_potential_assign_to_self(&expr_node, expr_ast, var_ast); |
3756 | | |
3757 | 10.4k | opline = zend_delayed_compile_end(offset); |
3758 | 10.4k | opline->opcode = ZEND_ASSIGN_DIM_OP; |
3759 | 10.4k | opline->extended_value = opcode; |
3760 | 10.4k | opline->result_type = IS_TMP_VAR; |
3761 | 10.4k | result->op_type = IS_TMP_VAR; |
3762 | | |
3763 | 10.4k | zend_emit_op_data(&expr_node); |
3764 | 10.4k | return; |
3765 | 1.62k | case ZEND_AST_PROP: |
3766 | 1.62k | case ZEND_AST_NULLSAFE_PROP: |
3767 | 1.62k | offset = zend_delayed_compile_begin(); |
3768 | 1.62k | zend_delayed_compile_prop(result, var_ast, BP_VAR_RW); |
3769 | 1.62k | zend_compile_expr(&expr_node, expr_ast); |
3770 | | |
3771 | 1.62k | opline = zend_delayed_compile_end(offset); |
3772 | 1.62k | cache_slot = opline->extended_value; |
3773 | 1.62k | opline->opcode = ZEND_ASSIGN_OBJ_OP; |
3774 | 1.62k | opline->extended_value = opcode; |
3775 | 1.62k | opline->result_type = IS_TMP_VAR; |
3776 | 1.62k | result->op_type = IS_TMP_VAR; |
3777 | | |
3778 | 1.62k | opline = zend_emit_op_data(&expr_node); |
3779 | 1.62k | opline->extended_value = cache_slot; |
3780 | 1.62k | return; |
3781 | 0 | default: ZEND_UNREACHABLE(); |
3782 | 111k | } |
3783 | 111k | } |
3784 | | /* }}} */ |
3785 | | |
3786 | 5.80k | static uint32_t zend_get_arg_num(const zend_function *fn, const zend_string *arg_name) { |
3787 | | // TODO: Caching? |
3788 | 20.6k | for (uint32_t i = 0; i < fn->common.num_args; i++) { |
3789 | 18.2k | zend_arg_info *arg_info = &fn->op_array.arg_info[i]; |
3790 | 18.2k | if (zend_string_equals(arg_info->name, arg_name)) { |
3791 | 3.34k | return i + 1; |
3792 | 3.34k | } |
3793 | 18.2k | } |
3794 | | |
3795 | | /* Either an invalid argument name, or collected into a variadic argument. */ |
3796 | 2.45k | return (uint32_t) -1; |
3797 | 5.80k | } |
3798 | | |
3799 | | static uint32_t zend_compile_args_ex( |
3800 | | zend_ast *ast, const zend_function *fbc, |
3801 | | bool *may_have_extra_named_args, |
3802 | | bool is_call_partial, bool *uses_variadic_placeholder_p, |
3803 | | zval *named_positions) /* {{{ */ |
3804 | 3.35M | { |
3805 | 3.35M | const zend_ast_list *args = zend_ast_get_list(ast); |
3806 | 3.35M | uint32_t i; |
3807 | 3.35M | bool uses_arg_unpack = false; |
3808 | 3.35M | bool uses_variadic_placeholder = false; |
3809 | 3.35M | uint32_t arg_count = 0; /* number of arguments not including unpacks */ |
3810 | | |
3811 | | /* Whether named arguments are used syntactically, to enforce language level limitations. |
3812 | | * May not actually use named argument passing. */ |
3813 | 3.35M | bool uses_named_args = false; |
3814 | | /* Whether there may be any undef arguments due to the use of named arguments. */ |
3815 | 3.35M | bool may_have_undef = false; |
3816 | | /* Whether there may be any extra named arguments collected into a variadic. */ |
3817 | 3.35M | *may_have_extra_named_args = false; |
3818 | | |
3819 | 7.51M | for (i = 0; i < args->children; ++i) { |
3820 | 4.16M | zend_ast *arg = args->child[i]; |
3821 | 4.16M | zend_string *arg_name = NULL; |
3822 | 4.16M | uint32_t arg_num = i + 1; |
3823 | | |
3824 | 4.16M | znode arg_node; |
3825 | 4.16M | zend_op *opline; |
3826 | 4.16M | uint8_t opcode; |
3827 | | |
3828 | 4.16M | if (arg->kind == ZEND_AST_UNPACK) { |
3829 | 2.35k | if (uses_named_args) { |
3830 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3831 | 5 | "Cannot use argument unpacking after named arguments"); |
3832 | 5 | } |
3833 | | |
3834 | 2.35k | if (is_call_partial) { |
3835 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3836 | 5 | "Cannot combine partial application and unpacking"); |
3837 | 5 | } |
3838 | | |
3839 | | /* Unpack may contain named arguments. */ |
3840 | 2.34k | may_have_undef = true; |
3841 | 2.34k | if (!fbc || (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3842 | 1.73k | *may_have_extra_named_args = true; |
3843 | 1.73k | } |
3844 | | |
3845 | 2.34k | uses_arg_unpack = true; |
3846 | 2.34k | fbc = NULL; |
3847 | | |
3848 | 2.34k | zend_compile_expr(&arg_node, arg->child[0]); |
3849 | 2.34k | opline = zend_emit_op(NULL, ZEND_SEND_UNPACK, &arg_node, NULL); |
3850 | 2.34k | opline->op2.num = arg_count; |
3851 | 2.34k | opline->result.var = EX_NUM_TO_VAR(arg_count - 1); |
3852 | | |
3853 | 2.34k | continue; |
3854 | 2.35k | } |
3855 | | |
3856 | 4.16M | if (arg->kind == ZEND_AST_NAMED_ARG) { |
3857 | 55.4k | uses_named_args = true; |
3858 | 55.4k | arg_name = zval_make_interned_string(zend_ast_get_zval(arg->child[0])); |
3859 | 55.4k | arg = arg->child[1]; |
3860 | | |
3861 | 55.4k | if (fbc && !uses_arg_unpack) { |
3862 | 5.80k | arg_num = zend_get_arg_num(fbc, arg_name); |
3863 | 5.80k | if (arg_num == arg_count + 1 && !may_have_undef) { |
3864 | | /* Using named arguments, but passing in order. */ |
3865 | 777 | arg_name = NULL; |
3866 | 777 | arg_count++; |
3867 | 5.02k | } else { |
3868 | | // TODO: We could track which arguments were passed, even if out of order. |
3869 | 5.02k | may_have_undef = true; |
3870 | 5.02k | if (arg_num == (uint32_t) -1 && (fbc->common.fn_flags & ZEND_ACC_VARIADIC)) { |
3871 | 580 | *may_have_extra_named_args = true; |
3872 | 580 | } |
3873 | 5.02k | } |
3874 | 49.6k | } else { |
3875 | 49.6k | arg_num = (uint32_t) -1; |
3876 | 49.6k | may_have_undef = true; |
3877 | 49.6k | *may_have_extra_named_args = true; |
3878 | 49.6k | } |
3879 | | |
3880 | 55.4k | if (uses_variadic_placeholder) { |
3881 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3882 | 5 | "Variadic placeholder must be last"); |
3883 | 5 | } |
3884 | 4.10M | } else { |
3885 | 4.10M | if (uses_arg_unpack) { |
3886 | 15 | zend_error_noreturn(E_COMPILE_ERROR, |
3887 | 15 | "Cannot use positional argument after argument unpacking"); |
3888 | 15 | } |
3889 | | |
3890 | 4.10M | bool is_variadic_placeholder = arg->kind == ZEND_AST_PLACEHOLDER_ARG |
3891 | 2.84k | && arg->attr == ZEND_PLACEHOLDER_VARIADIC; |
3892 | | |
3893 | 4.10M | if (uses_named_args && !is_variadic_placeholder) { |
3894 | 54 | zend_error_noreturn(E_COMPILE_ERROR, |
3895 | 54 | "Cannot use positional argument after named argument"); |
3896 | 54 | } |
3897 | | |
3898 | 4.10M | if (uses_variadic_placeholder) { |
3899 | 15 | if (is_variadic_placeholder) { |
3900 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
3901 | 5 | "Variadic placeholder may only appear once"); |
3902 | 10 | } else { |
3903 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
3904 | 10 | "Variadic placeholder must be last"); |
3905 | 10 | } |
3906 | 15 | } |
3907 | | |
3908 | 4.10M | if (!is_variadic_placeholder) { |
3909 | 4.10M | arg_count++; |
3910 | 4.10M | } |
3911 | 4.10M | } |
3912 | | |
3913 | 4.16M | if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) { |
3914 | 4.96k | if (uses_arg_unpack) { |
3915 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
3916 | 0 | "Cannot combine partial application and unpacking"); |
3917 | 0 | } |
3918 | | |
3919 | 4.96k | if (arg->attr == ZEND_PLACEHOLDER_VARIADIC) { |
3920 | 554 | uses_variadic_placeholder = true; |
3921 | | /* Do not emit ZEND_SEND_PLACEHOLDER: We represent the variadic |
3922 | | * placeholder with a flag on the ZEND_CONVERT_CALLABLE_PARTIAL |
3923 | | * op instead. */ |
3924 | 554 | continue; |
3925 | 554 | } |
3926 | | |
3927 | 4.40k | if (arg_name) { |
3928 | 2.12k | if (Z_ISUNDEF_P(named_positions)) { |
3929 | 2.00k | array_init(named_positions); |
3930 | 2.00k | } |
3931 | 2.12k | zval tmp; |
3932 | 2.12k | ZVAL_LONG(&tmp, zend_hash_num_elements(Z_ARRVAL_P(named_positions))); |
3933 | 2.12k | zend_hash_add(Z_ARRVAL_P(named_positions), arg_name, &tmp); |
3934 | 2.12k | } |
3935 | | |
3936 | 4.40k | opline = zend_emit_op(NULL, ZEND_SEND_PLACEHOLDER, NULL, NULL); |
3937 | 4.40k | if (arg_name) { |
3938 | 2.12k | opline->op2_type = IS_CONST; |
3939 | 2.12k | zend_string_addref(arg_name); |
3940 | 2.12k | opline->op2.constant = zend_add_literal_string(&arg_name); |
3941 | 2.12k | opline->result.num = zend_alloc_cache_slots(2); |
3942 | 2.28k | } else if (arg->attr != ZEND_PLACEHOLDER_VARIADIC) { |
3943 | 2.28k | opline->op2.opline_num = arg_num; |
3944 | 2.28k | opline->result.var = EX_NUM_TO_VAR(arg_num - 1); |
3945 | 2.28k | } |
3946 | | |
3947 | 4.40k | continue; |
3948 | 4.96k | } |
3949 | | |
3950 | | /* Treat passing of $GLOBALS the same as passing a call. |
3951 | | * This will error at runtime if the argument is by-ref. */ |
3952 | 4.15M | if (zend_is_call(arg) || is_globals_fetch(arg)) { |
3953 | 190k | uint32_t type = is_globals_fetch(arg) || (fbc && !ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) |
3954 | 190k | ? BP_VAR_R : BP_VAR_FUNC_ARG; |
3955 | 190k | zend_compile_var(&arg_node, arg, type, /* by_ref */ false); |
3956 | 190k | if (arg_node.op_type & (IS_CONST|IS_TMP_VAR)) { |
3957 | | /* Function call was converted into builtin instruction */ |
3958 | 50.5k | if (!fbc || ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3959 | 2.31k | opcode = ZEND_SEND_VAL_EX; |
3960 | 48.2k | } else { |
3961 | 48.2k | opcode = ZEND_SEND_VAL; |
3962 | 48.2k | } |
3963 | 139k | } else { |
3964 | 139k | if (fbc && arg_num != (uint32_t) -1) { |
3965 | 669 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
3966 | 579 | opcode = ZEND_SEND_VAR_NO_REF; |
3967 | 579 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
3968 | | /* For IS_VAR operands, SEND_VAL will pass through the operand without |
3969 | | * dereferencing, so it will use a by-ref pass if the call returned by-ref |
3970 | | * and a by-value pass if it returned by-value. */ |
3971 | 90 | opcode = ZEND_SEND_VAL; |
3972 | 90 | } else { |
3973 | 0 | opcode = ZEND_SEND_VAR; |
3974 | 0 | } |
3975 | 138k | } else { |
3976 | 138k | opcode = ZEND_SEND_VAR_NO_REF_EX; |
3977 | 138k | } |
3978 | 139k | } |
3979 | 3.96M | } else if (zend_is_passable_by_ref(arg) && !zend_ast_is_short_circuited(arg)) { |
3980 | 149k | if (fbc && arg_num != (uint32_t) -1) { |
3981 | 89.5k | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
3982 | 3.65k | zend_compile_var(&arg_node, arg, BP_VAR_W, true); |
3983 | 3.65k | opcode = ZEND_SEND_REF; |
3984 | 85.9k | } else { |
3985 | 85.9k | zend_compile_var(&arg_node, arg, BP_VAR_R, false); |
3986 | 85.9k | opcode = (arg_node.op_type == IS_TMP_VAR) ? ZEND_SEND_VAL : ZEND_SEND_VAR; |
3987 | 85.9k | } |
3988 | 89.5k | } else { |
3989 | 60.2k | do { |
3990 | 60.2k | if (arg->kind == ZEND_AST_VAR) { |
3991 | 18.7k | CG(zend_lineno) = zend_ast_get_lineno(ast); |
3992 | 18.7k | if (is_this_fetch(arg)) { |
3993 | 541 | zend_emit_op(&arg_node, ZEND_FETCH_THIS, NULL, NULL); |
3994 | 541 | opcode = ZEND_SEND_VAR_EX; |
3995 | 541 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
3996 | 541 | break; |
3997 | 18.1k | } else if (zend_try_compile_cv(&arg_node, arg, BP_VAR_R) == SUCCESS) { |
3998 | 17.8k | opcode = ZEND_SEND_VAR_EX; |
3999 | 17.8k | break; |
4000 | 17.8k | } |
4001 | 18.7k | } |
4002 | 41.8k | opline = zend_emit_op(NULL, ZEND_CHECK_FUNC_ARG, NULL, NULL); |
4003 | 41.8k | if (arg_name) { |
4004 | 25.4k | opline->op2_type = IS_CONST; |
4005 | 25.4k | zend_string_addref(arg_name); |
4006 | 25.4k | opline->op2.constant = zend_add_literal_string(&arg_name); |
4007 | 25.4k | opline->result.num = zend_alloc_cache_slots(2); |
4008 | 25.4k | } else { |
4009 | 16.3k | opline->op2.num = arg_num; |
4010 | 16.3k | } |
4011 | 41.8k | zend_compile_var(&arg_node, arg, BP_VAR_FUNC_ARG, true); |
4012 | 41.8k | opcode = ZEND_SEND_FUNC_ARG; |
4013 | 41.8k | } while (0); |
4014 | 60.2k | } |
4015 | 3.81M | } else { |
4016 | 3.81M | zend_compile_expr(&arg_node, arg); |
4017 | 3.81M | if (arg_node.op_type == IS_VAR) { |
4018 | | /* pass ++$a or something similar */ |
4019 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
4020 | 0 | if (ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
4021 | 0 | opcode = ZEND_SEND_VAR_NO_REF; |
4022 | 0 | } else if (ARG_MAY_BE_SENT_BY_REF(fbc, arg_num)) { |
4023 | 0 | opcode = ZEND_SEND_VAL; |
4024 | 0 | } else { |
4025 | 0 | opcode = ZEND_SEND_VAR; |
4026 | 0 | } |
4027 | 0 | } else { |
4028 | 0 | opcode = ZEND_SEND_VAR_NO_REF_EX; |
4029 | 0 | } |
4030 | 3.81M | } else if (arg_node.op_type == IS_CV) { |
4031 | 0 | if (fbc && arg_num != (uint32_t) -1) { |
4032 | 0 | if (ARG_SHOULD_BE_SENT_BY_REF(fbc, arg_num)) { |
4033 | 0 | opcode = ZEND_SEND_REF; |
4034 | 0 | } else { |
4035 | 0 | opcode = ZEND_SEND_VAR; |
4036 | 0 | } |
4037 | 0 | } else { |
4038 | 0 | opcode = ZEND_SEND_VAR_EX; |
4039 | 0 | } |
4040 | 3.81M | } else { |
4041 | | /* Delay "Only variables can be passed by reference" error to execution */ |
4042 | 3.81M | if (fbc && arg_num != (uint32_t) -1 && !ARG_MUST_BE_SENT_BY_REF(fbc, arg_num)) { |
4043 | 144k | opcode = ZEND_SEND_VAL; |
4044 | 3.67M | } else { |
4045 | 3.67M | opcode = ZEND_SEND_VAL_EX; |
4046 | 3.67M | } |
4047 | 3.81M | } |
4048 | 3.81M | } |
4049 | | |
4050 | 4.15M | opline = zend_emit_op(NULL, opcode, &arg_node, NULL); |
4051 | 4.15M | if (arg_name) { |
4052 | 52.5k | opline->op2_type = IS_CONST; |
4053 | 52.5k | zend_string_addref(arg_name); |
4054 | 52.5k | opline->op2.constant = zend_add_literal_string(&arg_name); |
4055 | 52.5k | opline->result.num = zend_alloc_cache_slots(2); |
4056 | 4.10M | } else { |
4057 | 4.10M | opline->op2.opline_num = arg_num; |
4058 | 4.10M | opline->result.var = EX_NUM_TO_VAR(arg_num - 1); |
4059 | 4.10M | } |
4060 | 4.15M | } |
4061 | | |
4062 | 3.35M | if (!is_call_partial) { |
4063 | 3.34M | if (may_have_undef) { |
4064 | 24.5k | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
4065 | 24.5k | } |
4066 | 3.34M | } else { |
4067 | 8.29k | *uses_variadic_placeholder_p = uses_variadic_placeholder; |
4068 | 8.29k | } |
4069 | | |
4070 | 3.35M | return arg_count; |
4071 | 3.35M | } |
4072 | | /* }}} */ |
4073 | | |
4074 | | static uint32_t zend_compile_args(zend_ast *ast, const zend_function *fbc, bool *may_have_extra_named_args) |
4075 | 3.34M | { |
4076 | 3.34M | return zend_compile_args_ex(ast, fbc, may_have_extra_named_args, false, NULL, NULL); |
4077 | 3.34M | } |
4078 | | |
4079 | | ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, const zend_function *fbc, bool result_used) /* {{{ */ |
4080 | 3.49M | { |
4081 | 3.49M | uint32_t no_discard = result_used ? 0 : ZEND_ACC_NODISCARD; |
4082 | | |
4083 | 3.49M | if (fbc && init_op->opcode != ZEND_NEW) { |
4084 | 319k | ZEND_ASSERT(!(fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)); |
4085 | 319k | if (fbc->type == ZEND_INTERNAL_FUNCTION && !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS)) { |
4086 | 255k | if (init_op->opcode == ZEND_INIT_FCALL && !zend_execute_internal) { |
4087 | 24.8k | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
4088 | 24.5k | return ZEND_DO_ICALL; |
4089 | 24.5k | } else { |
4090 | 258 | return ZEND_DO_FCALL_BY_NAME; |
4091 | 258 | } |
4092 | 24.8k | } |
4093 | 255k | } else if (!(CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS)){ |
4094 | 64.0k | if (zend_execute_ex == execute_ex) { |
4095 | 30.4k | if (!(fbc->common.fn_flags & (ZEND_ACC_DEPRECATED|no_discard))) { |
4096 | 30.1k | return ZEND_DO_UCALL; |
4097 | 30.1k | } else { |
4098 | 325 | return ZEND_DO_FCALL_BY_NAME; |
4099 | 325 | } |
4100 | 30.4k | } |
4101 | 64.0k | } |
4102 | 3.17M | } else if (zend_execute_ex == execute_ex && |
4103 | 3.08M | !zend_execute_internal && |
4104 | 3.03M | (init_op->opcode == ZEND_INIT_FCALL_BY_NAME || |
4105 | 2.97M | init_op->opcode == ZEND_INIT_NS_FCALL_BY_NAME)) { |
4106 | 2.84M | return ZEND_DO_FCALL_BY_NAME; |
4107 | 2.84M | } |
4108 | 591k | return ZEND_DO_FCALL; |
4109 | 3.49M | } |
4110 | | /* }}} */ |
4111 | | |
4112 | | static zend_string *zend_compile_partial_name(const zend_op_array *declaring_op_array, |
4113 | | const uint32_t declaring_lineno) |
4114 | 4.54k | { |
4115 | | /* We attempt to generate a name that hints at where the PFA was created, |
4116 | | * similarly to Closures (GH-13550). |
4117 | | * We do not attempt to make the name unique. */ |
4118 | | |
4119 | 4.54k | zend_string *filename = declaring_op_array->filename; |
4120 | 4.54k | uint32_t start_lineno = declaring_lineno; |
4121 | | |
4122 | 4.54k | zend_string *class = zend_empty_string; |
4123 | 4.54k | zend_string *separator = zend_empty_string; |
4124 | 4.54k | zend_string *function = filename; |
4125 | 4.54k | const char *parens = ""; |
4126 | | |
4127 | 4.54k | if (declaring_op_array->function_name) { |
4128 | 784 | function = declaring_op_array->function_name; |
4129 | 784 | if (declaring_op_array->fn_flags & ZEND_ACC_CLOSURE) { |
4130 | | /* If the parent function is a closure, don't redundantly |
4131 | | * add the classname and parentheses. */ |
4132 | 683 | } else { |
4133 | 683 | parens = "()"; |
4134 | | |
4135 | 683 | if (declaring_op_array->scope && declaring_op_array->scope->name) { |
4136 | 425 | class = declaring_op_array->scope->name; |
4137 | 425 | separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM); |
4138 | 425 | } |
4139 | 683 | } |
4140 | 784 | } |
4141 | | |
4142 | 4.54k | zend_string *name = zend_strpprintf_unchecked( |
4143 | 4.54k | 0, |
4144 | 4.54k | "{closure:pfa:%S%S%S%s:%" PRIu32 "}", |
4145 | 4.54k | class, |
4146 | 4.54k | separator, |
4147 | 4.54k | function, |
4148 | 4.54k | parens, |
4149 | 4.54k | start_lineno |
4150 | 4.54k | ); |
4151 | | |
4152 | 4.54k | return name; |
4153 | 4.54k | } |
4154 | | |
4155 | | static void zend_compile_call_partial(znode *result, zend_ast_fcc *fcc_ast, uint32_t arg_count, |
4156 | | bool may_have_extra_named_args, bool uses_variadic_placeholder, |
4157 | 4.22k | zval *named_positions, uint32_t opnum_init, const zend_function *fbc) { |
4158 | | |
4159 | 4.22k | zend_op *init_opline = &CG(active_op_array)->opcodes[opnum_init]; |
4160 | | |
4161 | 4.22k | init_opline->extended_value = arg_count; |
4162 | | |
4163 | 4.22k | ZEND_ASSERT(init_opline->opcode != ZEND_NEW); |
4164 | | |
4165 | 4.22k | if (init_opline->opcode == ZEND_INIT_FCALL) { |
4166 | 1.52k | init_opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc); |
4167 | 1.52k | } |
4168 | | |
4169 | 4.22k | zend_op *opline = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT_PARTIAL, |
4170 | 4.22k | NULL, NULL); |
4171 | | |
4172 | 4.22k | opline->extended_value = zend_alloc_cache_slots(2); |
4173 | | |
4174 | 4.22k | if (uses_variadic_placeholder) { |
4175 | 534 | opline->extended_value |= ZEND_PARTIAL_USES_VARIADIC_PLACEHOLDER; |
4176 | 534 | } |
4177 | | |
4178 | 4.22k | zend_string *name = zend_compile_partial_name(CG(active_op_array), opline->lineno); |
4179 | 4.22k | opline->op1.constant = zend_add_literal_string(&name); |
4180 | 4.22k | opline->op1_type = IS_CONST; |
4181 | | |
4182 | 4.22k | if (!Z_ISUNDEF_P(named_positions)) { |
4183 | 1.99k | opline->op2.constant = zend_add_literal(named_positions); |
4184 | 1.99k | opline->op2_type = IS_CONST; |
4185 | 2.23k | } else { |
4186 | 2.23k | opline->op2.num = 0; |
4187 | 2.23k | } |
4188 | 4.22k | } |
4189 | | |
4190 | | static bool zend_compile_call_common(znode *result, zend_ast *args_ast, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4191 | 3.35M | { |
4192 | 3.35M | zend_op *opline; |
4193 | 3.35M | uint32_t opnum_init = get_next_op_number() - 1; |
4194 | | |
4195 | 3.35M | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
4196 | 7.07k | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4197 | 7.07k | opline->extended_value = 0; |
4198 | | /* opcode array may be reallocated, so don't access opcode field after zend_emit_op_tmp(). */ |
4199 | 7.07k | uint8_t opcode = opline->opcode; |
4200 | | |
4201 | 7.07k | if (opcode == ZEND_NEW) { |
4202 | 21 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
4203 | 21 | } |
4204 | | |
4205 | 7.05k | zend_ast_list *fcc_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
4206 | | |
4207 | | /* FCCs are a special case of PFAs with a single variadic placeholder */ |
4208 | 7.05k | if (fcc_args->children == 1 && fcc_args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC) { |
4209 | | |
4210 | 2.79k | if (opline->opcode == ZEND_INIT_FCALL) { |
4211 | 733 | opline->op1.num = zend_vm_calc_used_stack(0, fbc); |
4212 | 733 | } |
4213 | | |
4214 | 2.79k | zend_op *callable_convert_op = zend_emit_op_tmp(result, ZEND_CALLABLE_CONVERT, NULL, NULL); |
4215 | 2.79k | if (opcode == ZEND_INIT_FCALL |
4216 | 2.06k | || opcode == ZEND_INIT_FCALL_BY_NAME |
4217 | 2.00k | || opcode == ZEND_INIT_NS_FCALL_BY_NAME) { |
4218 | 2.00k | callable_convert_op->extended_value = zend_alloc_cache_slot(); |
4219 | 2.00k | } else { |
4220 | 786 | callable_convert_op->extended_value = (uint32_t)-1; |
4221 | 786 | } |
4222 | | |
4223 | 2.79k | return true; |
4224 | 2.79k | } |
4225 | | |
4226 | 4.26k | zend_ast_fcc *fcc_ast = (zend_ast_fcc*)args_ast; |
4227 | 4.26k | args_ast = fcc_ast->args; |
4228 | | |
4229 | 4.26k | bool may_have_extra_named_args; |
4230 | 4.26k | bool uses_variadic_placeholder; |
4231 | | |
4232 | 4.26k | zval named_positions; |
4233 | 4.26k | ZVAL_UNDEF(&named_positions); |
4234 | | |
4235 | 4.26k | uint32_t arg_count = zend_compile_args_ex(args_ast, fbc, |
4236 | 4.26k | &may_have_extra_named_args, true, &uses_variadic_placeholder, |
4237 | 4.26k | &named_positions); |
4238 | | |
4239 | 4.26k | zend_compile_call_partial(result, fcc_ast, arg_count, |
4240 | 4.26k | may_have_extra_named_args, uses_variadic_placeholder, |
4241 | 4.26k | &named_positions, opnum_init, fbc); |
4242 | | |
4243 | 4.26k | return true; |
4244 | 7.05k | } |
4245 | | |
4246 | 3.35M | bool may_have_extra_named_args; |
4247 | 3.34M | uint32_t arg_count = zend_compile_args(args_ast, fbc, &may_have_extra_named_args); |
4248 | | |
4249 | 3.34M | zend_do_extended_fcall_begin(); |
4250 | | |
4251 | 3.34M | opline = &CG(active_op_array)->opcodes[opnum_init]; |
4252 | 3.34M | opline->extended_value = arg_count; |
4253 | 3.34M | uint8_t init_opcode = opline->opcode; |
4254 | | |
4255 | 3.34M | if (init_opcode == ZEND_INIT_FCALL) { |
4256 | 173k | opline->op1.num = zend_vm_calc_used_stack(arg_count, fbc); |
4257 | 173k | } |
4258 | | |
4259 | 3.34M | uint8_t call_op = zend_get_call_op( |
4260 | 3.34M | opline, |
4261 | 3.34M | fbc, |
4262 | | /* result_used: At this point we do not yet reliably |
4263 | | * know if the result is used. Deoptimize #[\NoDiscard] |
4264 | | * calls to be sure. The optimizer will fix this up. |
4265 | | */ |
4266 | 3.34M | false |
4267 | 3.34M | ); |
4268 | 3.34M | opline = zend_emit_op(result, call_op, NULL, NULL); |
4269 | 3.34M | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4270 | 2.93M | if (init_opcode != ZEND_NEW && opline->result_type == IS_VAR) { |
4271 | 2.82M | opline->result_type = IS_TMP_VAR; |
4272 | 2.82M | result->op_type = IS_TMP_VAR; |
4273 | 2.82M | } |
4274 | 2.93M | } |
4275 | 3.34M | if (may_have_extra_named_args) { |
4276 | 22.5k | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4277 | 22.5k | } |
4278 | 3.34M | opline->lineno = lineno; |
4279 | 3.34M | zend_do_extended_fcall_end(); |
4280 | 3.34M | return false; |
4281 | 3.35M | } |
4282 | | /* }}} */ |
4283 | | |
4284 | | static bool zend_compile_function_name(znode *name_node, zend_ast *name_ast) /* {{{ */ |
4285 | 3.07M | { |
4286 | 3.07M | zend_string *orig_name = zend_ast_get_str(name_ast); |
4287 | 3.07M | bool is_fully_qualified; |
4288 | | |
4289 | 3.07M | name_node->op_type = IS_CONST; |
4290 | 3.07M | ZVAL_STR(&name_node->u.constant, zend_resolve_function_name( |
4291 | 3.07M | orig_name, name_ast->attr, &is_fully_qualified)); |
4292 | | |
4293 | 3.07M | return !is_fully_qualified && FC(current_namespace); |
4294 | 3.07M | } |
4295 | | /* }}} */ |
4296 | | |
4297 | | static void zend_compile_dynamic_call(znode *result, znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
4298 | 120k | { |
4299 | 120k | if (name_node->op_type == IS_CONST && Z_TYPE(name_node->u.constant) == IS_STRING) { |
4300 | 62.6k | const char *colon; |
4301 | 62.6k | zend_string *str = Z_STR(name_node->u.constant); |
4302 | 62.6k | if ((colon = zend_memrchr(ZSTR_VAL(str), ':', ZSTR_LEN(str))) != NULL && colon > ZSTR_VAL(str) && *(colon - 1) == ':') { |
4303 | 270 | zend_string *class = zend_string_init(ZSTR_VAL(str), colon - ZSTR_VAL(str) - 1, 0); |
4304 | 270 | zend_string *method = zend_string_init(colon + 1, ZSTR_LEN(str) - (colon - ZSTR_VAL(str)) - 1, 0); |
4305 | 270 | zend_op *opline = get_next_op(); |
4306 | | |
4307 | 270 | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
4308 | 270 | opline->op1_type = IS_CONST; |
4309 | 270 | opline->op1.constant = zend_add_class_name_literal(class); |
4310 | 270 | opline->op2_type = IS_CONST; |
4311 | 270 | opline->op2.constant = zend_add_func_name_literal(method); |
4312 | | /* 2 slots, for class and method */ |
4313 | 270 | opline->result.num = zend_alloc_cache_slots(2); |
4314 | 270 | zval_ptr_dtor(&name_node->u.constant); |
4315 | 62.4k | } else { |
4316 | 62.4k | zend_op *opline = get_next_op(); |
4317 | | |
4318 | 62.4k | opline->opcode = ZEND_INIT_FCALL_BY_NAME; |
4319 | 62.4k | opline->op2_type = IS_CONST; |
4320 | 62.4k | opline->op2.constant = zend_add_func_name_literal(str); |
4321 | 62.4k | opline->result.num = zend_alloc_cache_slot(); |
4322 | 62.4k | } |
4323 | 62.6k | } else { |
4324 | 58.0k | zend_emit_op(NULL, ZEND_INIT_DYNAMIC_CALL, NULL, name_node); |
4325 | 58.0k | } |
4326 | | |
4327 | 120k | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
4328 | 120k | } |
4329 | | /* }}} */ |
4330 | | |
4331 | | static inline bool zend_args_contain_unpack_or_named(const zend_ast_list *args) /* {{{ */ |
4332 | 2.94M | { |
4333 | 2.94M | uint32_t i; |
4334 | 6.79M | for (i = 0; i < args->children; ++i) { |
4335 | 3.85M | const zend_ast *arg = args->child[i]; |
4336 | 3.85M | if (arg->kind == ZEND_AST_UNPACK || arg->kind == ZEND_AST_NAMED_ARG) { |
4337 | 2.98k | return true; |
4338 | 2.98k | } |
4339 | 3.85M | } |
4340 | 2.94M | return false; |
4341 | 2.94M | } |
4342 | | /* }}} */ |
4343 | | |
4344 | | static zend_result zend_compile_func_strlen(znode *result, const zend_ast_list *args) /* {{{ */ |
4345 | 1.18k | { |
4346 | 1.18k | znode arg_node; |
4347 | | |
4348 | 1.18k | if (args->children != 1) { |
4349 | 141 | return FAILURE; |
4350 | 141 | } |
4351 | | |
4352 | 1.04k | zend_compile_expr(&arg_node, args->child[0]); |
4353 | 1.04k | if (arg_node.op_type == IS_CONST && Z_TYPE(arg_node.u.constant) == IS_STRING) { |
4354 | 237 | result->op_type = IS_CONST; |
4355 | 237 | ZVAL_LONG(&result->u.constant, Z_STRLEN(arg_node.u.constant)); |
4356 | 237 | zval_ptr_dtor_str(&arg_node.u.constant); |
4357 | 806 | } else { |
4358 | 806 | zend_emit_op_tmp(result, ZEND_STRLEN, &arg_node, NULL); |
4359 | 806 | } |
4360 | 1.04k | return SUCCESS; |
4361 | 1.18k | } |
4362 | | /* }}} */ |
4363 | | |
4364 | | static zend_result zend_compile_func_typecheck(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4365 | 2.22k | { |
4366 | 2.22k | znode arg_node; |
4367 | 2.22k | zend_op *opline; |
4368 | | |
4369 | 2.22k | if (args->children != 1) { |
4370 | 538 | return FAILURE; |
4371 | 538 | } |
4372 | | |
4373 | 1.68k | zend_compile_expr(&arg_node, args->child[0]); |
4374 | 1.68k | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4375 | 1.68k | if (type != _IS_BOOL) { |
4376 | 1.34k | opline->extended_value = (1 << type); |
4377 | 1.34k | } else { |
4378 | 340 | opline->extended_value = (1 << IS_FALSE) | (1 << IS_TRUE); |
4379 | 340 | } |
4380 | 1.68k | return SUCCESS; |
4381 | 2.22k | } |
4382 | | /* }}} */ |
4383 | | |
4384 | | static zend_result zend_compile_func_is_scalar(znode *result, const zend_ast_list *args) /* {{{ */ |
4385 | 183 | { |
4386 | 183 | znode arg_node; |
4387 | 183 | zend_op *opline; |
4388 | | |
4389 | 183 | if (args->children != 1) { |
4390 | 87 | return FAILURE; |
4391 | 87 | } |
4392 | | |
4393 | 96 | zend_compile_expr(&arg_node, args->child[0]); |
4394 | 96 | opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &arg_node, NULL); |
4395 | 96 | opline->extended_value = (1 << IS_FALSE | 1 << IS_TRUE | 1 << IS_DOUBLE | 1 << IS_LONG | 1 << IS_STRING); |
4396 | 96 | return SUCCESS; |
4397 | 183 | } |
4398 | | |
4399 | | static zend_result zend_compile_func_cast(znode *result, const zend_ast_list *args, uint32_t type) /* {{{ */ |
4400 | 1.48k | { |
4401 | 1.48k | znode arg_node; |
4402 | 1.48k | zend_op *opline; |
4403 | | |
4404 | 1.48k | if (args->children != 1) { |
4405 | 348 | return FAILURE; |
4406 | 348 | } |
4407 | | |
4408 | 1.13k | zend_compile_expr(&arg_node, args->child[0]); |
4409 | 1.13k | if (type == _IS_BOOL) { |
4410 | 208 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &arg_node, NULL); |
4411 | 925 | } else { |
4412 | 925 | opline = zend_emit_op_tmp(result, ZEND_CAST, &arg_node, NULL); |
4413 | 925 | opline->extended_value = type; |
4414 | 925 | } |
4415 | 1.13k | return SUCCESS; |
4416 | 1.48k | } |
4417 | | /* }}} */ |
4418 | | |
4419 | | static zend_result zend_compile_func_defined(znode *result, const zend_ast_list *args) /* {{{ */ |
4420 | 1.93k | { |
4421 | 1.93k | zend_string *name; |
4422 | 1.93k | zend_op *opline; |
4423 | | |
4424 | 1.93k | if (args->children != 1 || args->child[0]->kind != ZEND_AST_ZVAL) { |
4425 | 193 | return FAILURE; |
4426 | 193 | } |
4427 | | |
4428 | 1.73k | name = zval_get_string(zend_ast_get_zval(args->child[0])); |
4429 | 1.73k | if (zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name)) || zend_memrchr(ZSTR_VAL(name), ':', ZSTR_LEN(name))) { |
4430 | 519 | zend_string_release_ex(name, 0); |
4431 | 519 | return FAILURE; |
4432 | 519 | } |
4433 | | |
4434 | 1.21k | if (zend_try_ct_eval_const(&result->u.constant, name, false)) { |
4435 | 52 | zend_string_release_ex(name, 0); |
4436 | 52 | zval_ptr_dtor(&result->u.constant); |
4437 | 52 | ZVAL_TRUE(&result->u.constant); |
4438 | 52 | result->op_type = IS_CONST; |
4439 | 52 | return SUCCESS; |
4440 | 52 | } |
4441 | | |
4442 | 1.16k | opline = zend_emit_op_tmp(result, ZEND_DEFINED, NULL, NULL); |
4443 | 1.16k | opline->op1_type = IS_CONST; |
4444 | 1.16k | LITERAL_STR(opline->op1, name); |
4445 | 1.16k | opline->extended_value = zend_alloc_cache_slot(); |
4446 | | |
4447 | 1.16k | return SUCCESS; |
4448 | 1.21k | } |
4449 | | /* }}} */ |
4450 | | |
4451 | | static zend_result zend_compile_func_chr(znode *result, const zend_ast_list *args) /* {{{ */ |
4452 | 890 | { |
4453 | 890 | zval *zint; |
4454 | 890 | if ( |
4455 | 890 | args->children == 1 |
4456 | 722 | && args->child[0]->kind == ZEND_AST_ZVAL |
4457 | 527 | && (zint = zend_ast_get_zval(args->child[0])) |
4458 | 527 | && Z_TYPE_P(zint) == IS_LONG |
4459 | 418 | && Z_LVAL_P(zint) >= 0 |
4460 | 418 | && Z_LVAL_P(zint) <= 255 |
4461 | 890 | ) { |
4462 | 376 | result->op_type = IS_CONST; |
4463 | 376 | ZVAL_CHAR(&result->u.constant, Z_LVAL_P(zint)); |
4464 | 376 | return SUCCESS; |
4465 | 514 | } else { |
4466 | 514 | return FAILURE; |
4467 | 514 | } |
4468 | 890 | } |
4469 | | /* }}} */ |
4470 | | |
4471 | | static zend_result zend_compile_func_ord(znode *result, const zend_ast_list *args) /* {{{ */ |
4472 | 682 | { |
4473 | 682 | zval *str; |
4474 | 682 | if ( |
4475 | 682 | args->children == 1 |
4476 | 599 | && args->child[0]->kind == ZEND_AST_ZVAL |
4477 | 357 | && (str = zend_ast_get_zval(args->child[0])) |
4478 | 357 | && Z_TYPE_P(str) == IS_STRING |
4479 | 141 | && Z_STRLEN_P(str) == 1 |
4480 | 682 | ) { |
4481 | 65 | result->op_type = IS_CONST; |
4482 | 65 | ZVAL_LONG(&result->u.constant, (unsigned char)Z_STRVAL_P(str)[0]); |
4483 | 65 | return SUCCESS; |
4484 | 617 | } else { |
4485 | 617 | return FAILURE; |
4486 | 617 | } |
4487 | 682 | } |
4488 | | /* }}} */ |
4489 | | |
4490 | | /* We can only calculate the stack size for functions that have been fully compiled, otherwise |
4491 | | * additional CV or TMP slots may still be added. This prevents the use of INIT_FCALL for |
4492 | | * directly or indirectly recursive function calls. */ |
4493 | 210k | static bool fbc_is_finalized(const zend_function *fbc) { |
4494 | 210k | return !ZEND_USER_CODE(fbc->type) || (fbc->common.fn_flags & ZEND_ACC_DONE_PASS_TWO); |
4495 | 210k | } |
4496 | | |
4497 | | static bool zend_compile_ignore_class(const zend_class_entry *ce, const zend_string *filename) |
4498 | 71.6k | { |
4499 | 71.6k | if (ce->type == ZEND_INTERNAL_CLASS) { |
4500 | 45.7k | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES; |
4501 | 45.7k | } else { |
4502 | 25.8k | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4503 | 19.6k | && ce->info.user.filename != filename; |
4504 | 25.8k | } |
4505 | 71.6k | } |
4506 | | |
4507 | | static bool zend_compile_ignore_function(const zend_function *fbc, const zend_string *filename) |
4508 | 193k | { |
4509 | 193k | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
4510 | 162k | return CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS; |
4511 | 162k | } else { |
4512 | 31.5k | return (CG(compiler_options) & ZEND_COMPILE_IGNORE_USER_FUNCTIONS) |
4513 | 31.5k | || ((CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES) |
4514 | 28.4k | && fbc->op_array.filename != filename); |
4515 | 31.5k | } |
4516 | 193k | } |
4517 | | |
4518 | | static zend_result zend_try_compile_ct_bound_init_user_func(zend_ast *name_ast, uint32_t num_args) /* {{{ */ |
4519 | 10.7k | { |
4520 | 10.7k | zend_string *name, *lcname; |
4521 | 10.7k | zend_function *fbc; |
4522 | 10.7k | zend_op *opline; |
4523 | | |
4524 | 10.7k | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
4525 | 2.35k | return FAILURE; |
4526 | 2.35k | } |
4527 | | |
4528 | 8.44k | name = zend_ast_get_str(name_ast); |
4529 | 8.44k | lcname = zend_string_tolower(name); |
4530 | | |
4531 | 8.44k | fbc = zend_hash_find_ptr(CG(function_table), lcname); |
4532 | 8.44k | if (!fbc |
4533 | 936 | || !fbc_is_finalized(fbc) |
4534 | 7.50k | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
4535 | 7.50k | zend_string_release_ex(lcname, 0); |
4536 | 7.50k | return FAILURE; |
4537 | 7.50k | } |
4538 | | |
4539 | 936 | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, NULL); |
4540 | 936 | opline->extended_value = num_args; |
4541 | 936 | opline->op1.num = zend_vm_calc_used_stack(num_args, fbc); |
4542 | 936 | opline->op2_type = IS_CONST; |
4543 | 936 | LITERAL_STR(opline->op2, lcname); |
4544 | 936 | opline->result.num = zend_alloc_cache_slot(); |
4545 | | |
4546 | 936 | return SUCCESS; |
4547 | 8.44k | } |
4548 | | /* }}} */ |
4549 | | |
4550 | | static void zend_compile_init_user_func(zend_ast *name_ast, uint32_t num_args, zend_string *orig_func_name) /* {{{ */ |
4551 | 10.7k | { |
4552 | 10.7k | zend_op *opline; |
4553 | 10.7k | znode name_node; |
4554 | | |
4555 | 10.7k | if (zend_try_compile_ct_bound_init_user_func(name_ast, num_args) == SUCCESS) { |
4556 | 936 | return; |
4557 | 936 | } |
4558 | | |
4559 | 9.85k | zend_compile_expr(&name_node, name_ast); |
4560 | | |
4561 | 9.85k | opline = zend_emit_op(NULL, ZEND_INIT_USER_CALL, NULL, &name_node); |
4562 | 9.85k | opline->op1_type = IS_CONST; |
4563 | 9.85k | LITERAL_STR(opline->op1, zend_string_copy(orig_func_name)); |
4564 | 9.85k | opline->extended_value = num_args; |
4565 | 9.85k | } |
4566 | | /* }}} */ |
4567 | | |
4568 | | /* cufa = call_user_func_array */ |
4569 | | static zend_result zend_compile_func_cufa(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4570 | 2.10k | { |
4571 | 2.10k | znode arg_node; |
4572 | 2.10k | zend_op *opline; |
4573 | | |
4574 | 2.10k | if (args->children != 2) { |
4575 | 102 | return FAILURE; |
4576 | 102 | } |
4577 | | |
4578 | 2.00k | zend_compile_init_user_func(args->child[0], 0, lcname); |
4579 | 2.00k | if (args->child[1]->kind == ZEND_AST_CALL |
4580 | 1.31k | && args->child[1]->child[0]->kind == ZEND_AST_ZVAL |
4581 | 1.21k | && Z_TYPE_P(zend_ast_get_zval(args->child[1]->child[0])) == IS_STRING |
4582 | 1.17k | && args->child[1]->child[1]->kind == ZEND_AST_ARG_LIST) { |
4583 | 1.14k | zend_string *orig_name = zend_ast_get_str(args->child[1]->child[0]); |
4584 | 1.14k | zend_ast_list *list = zend_ast_get_list(args->child[1]->child[1]); |
4585 | 1.14k | bool is_fully_qualified; |
4586 | 1.14k | zend_string *name = zend_resolve_function_name(orig_name, args->child[1]->child[0]->attr, &is_fully_qualified); |
4587 | | |
4588 | 1.14k | if (zend_string_equals_literal_ci(name, "array_slice") |
4589 | 699 | && !zend_args_contain_unpack_or_named(list) |
4590 | 541 | && list->children == 3 |
4591 | 488 | && list->child[1]->kind == ZEND_AST_ZVAL) { |
4592 | 376 | zval *zv = zend_ast_get_zval(list->child[1]); |
4593 | | |
4594 | 376 | if (Z_TYPE_P(zv) == IS_LONG |
4595 | 359 | && Z_LVAL_P(zv) >= 0 |
4596 | 359 | && Z_LVAL_P(zv) <= 0x7fffffff) { |
4597 | 274 | zend_op *opline; |
4598 | 274 | znode len_node; |
4599 | | |
4600 | 274 | zend_compile_expr(&arg_node, list->child[0]); |
4601 | 274 | zend_compile_expr(&len_node, list->child[2]); |
4602 | 274 | opline = zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, &len_node); |
4603 | 274 | opline->extended_value = Z_LVAL_P(zv); |
4604 | 274 | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4605 | 274 | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4606 | 260 | opline->result_type = IS_TMP_VAR; |
4607 | 260 | result->op_type = IS_TMP_VAR; |
4608 | 260 | } |
4609 | 274 | zend_string_release_ex(name, 0); |
4610 | 274 | return SUCCESS; |
4611 | 274 | } |
4612 | 376 | } |
4613 | 871 | zend_string_release_ex(name, 0); |
4614 | 871 | } |
4615 | 1.72k | zend_compile_expr(&arg_node, args->child[1]); |
4616 | 1.72k | zend_emit_op(NULL, ZEND_SEND_ARRAY, &arg_node, NULL); |
4617 | 1.72k | zend_emit_op(NULL, ZEND_CHECK_UNDEF_ARGS, NULL, NULL); |
4618 | 1.72k | opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4619 | 1.72k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4620 | 1.65k | opline->result_type = IS_TMP_VAR; |
4621 | 1.65k | result->op_type = IS_TMP_VAR; |
4622 | 1.65k | } |
4623 | 1.72k | opline->extended_value = ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS; |
4624 | | |
4625 | 1.72k | return SUCCESS; |
4626 | 2.00k | } |
4627 | | /* }}} */ |
4628 | | |
4629 | | /* cuf = call_user_func */ |
4630 | | static zend_result zend_compile_func_cuf(znode *result, const zend_ast_list *args, zend_string *lcname, uint32_t type) /* {{{ */ |
4631 | 8.94k | { |
4632 | 8.94k | uint32_t i; |
4633 | | |
4634 | 8.94k | if (args->children < 1) { |
4635 | 154 | return FAILURE; |
4636 | 154 | } |
4637 | | |
4638 | 8.79k | zend_compile_init_user_func(args->child[0], args->children - 1, lcname); |
4639 | 13.5k | for (i = 1; i < args->children; ++i) { |
4640 | 4.71k | zend_ast *arg_ast = args->child[i]; |
4641 | 4.71k | znode arg_node; |
4642 | 4.71k | zend_op *opline; |
4643 | | |
4644 | 4.71k | zend_compile_expr(&arg_node, arg_ast); |
4645 | | |
4646 | 4.71k | opline = zend_emit_op(NULL, ZEND_SEND_USER, &arg_node, NULL); |
4647 | 4.71k | opline->op2.num = i; |
4648 | 4.71k | opline->result.var = EX_NUM_TO_VAR(i - 1); |
4649 | 4.71k | } |
4650 | 8.79k | zend_op *opline = zend_emit_op(result, ZEND_DO_FCALL, NULL, NULL); |
4651 | 8.79k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
4652 | 981 | opline->result_type = IS_TMP_VAR; |
4653 | 981 | result->op_type = IS_TMP_VAR; |
4654 | 981 | } |
4655 | | |
4656 | 8.79k | return SUCCESS; |
4657 | 8.94k | } |
4658 | | /* }}} */ |
4659 | | |
4660 | | static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string *name, const zend_function *fbc, uint32_t lineno, uint32_t type) /* {{{ */ |
4661 | 31.7k | { |
4662 | 31.7k | if (EG(assertions) >= 0) { |
4663 | 31.7k | znode name_node; |
4664 | 31.7k | zend_op *opline; |
4665 | 31.7k | uint32_t check_op_number = get_next_op_number(); |
4666 | | |
4667 | 31.7k | zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL); |
4668 | | |
4669 | 31.7k | if (fbc && fbc_is_finalized(fbc)) { |
4670 | 16.8k | name_node.op_type = IS_CONST; |
4671 | 16.8k | ZVAL_STR_COPY(&name_node.u.constant, name); |
4672 | | |
4673 | 16.8k | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
4674 | 16.8k | } else { |
4675 | 14.8k | opline = zend_emit_op(NULL, ZEND_INIT_NS_FCALL_BY_NAME, NULL, NULL); |
4676 | 14.8k | opline->op2_type = IS_CONST; |
4677 | 14.8k | opline->op2.constant = zend_add_ns_func_name_literal(name); |
4678 | 14.8k | } |
4679 | 31.7k | opline->result.num = zend_alloc_cache_slot(); |
4680 | | |
4681 | 31.7k | if (args->children == 1) { |
4682 | | /* add "assert(condition) as assertion message */ |
4683 | 17.0k | zend_ast *arg = zend_ast_create_zval_from_str( |
4684 | 17.0k | zend_ast_export("assert(", args->child[0], ")")); |
4685 | 17.0k | if (args->child[0]->kind == ZEND_AST_NAMED_ARG) { |
4686 | | /* If the original argument was named, add the new argument as named as well, |
4687 | | * as mixing named and positional is not allowed. */ |
4688 | 712 | zend_ast *name = zend_ast_create_zval_from_str( |
4689 | 712 | ZSTR_INIT_LITERAL("description", 0)); |
4690 | 712 | arg = zend_ast_create(ZEND_AST_NAMED_ARG, name, arg); |
4691 | 712 | } |
4692 | 17.0k | args = (zend_ast_list *)zend_ast_list_add((zend_ast *) args, arg); |
4693 | 17.0k | } |
4694 | | |
4695 | 31.7k | zend_compile_call_common(result, (zend_ast*)args, fbc, lineno, type); |
4696 | | |
4697 | 31.7k | opline = &CG(active_op_array)->opcodes[check_op_number]; |
4698 | 31.7k | opline->op2.opline_num = get_next_op_number(); |
4699 | 31.7k | SET_NODE(opline->result, result); |
4700 | 31.7k | } else { |
4701 | 0 | if (!fbc) { |
4702 | 0 | zend_string_release_ex(name, 0); |
4703 | 0 | } |
4704 | 0 | result->op_type = IS_CONST; |
4705 | 0 | ZVAL_TRUE(&result->u.constant); |
4706 | 0 | } |
4707 | 31.7k | } |
4708 | | /* }}} */ |
4709 | | |
4710 | | static zend_result zend_compile_func_in_array(znode *result, zend_ast_list *args) /* {{{ */ |
4711 | 7.05k | { |
4712 | 7.05k | bool strict = false; |
4713 | 7.05k | znode array, needly; |
4714 | 7.05k | zend_op *opline; |
4715 | | |
4716 | 7.05k | if (args->children == 3) { |
4717 | 4.69k | if (args->child[2]->kind == ZEND_AST_ZVAL) { |
4718 | 285 | strict = zend_is_true(zend_ast_get_zval(args->child[2])); |
4719 | 4.40k | } else if (args->child[2]->kind == ZEND_AST_CONST) { |
4720 | 3.81k | zval value; |
4721 | 3.81k | zend_ast *name_ast = args->child[2]->child[0]; |
4722 | 3.81k | bool is_fully_qualified; |
4723 | 3.81k | zend_string *resolved_name = zend_resolve_const_name( |
4724 | 3.81k | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
4725 | | |
4726 | 3.81k | if (!zend_try_ct_eval_const(&value, resolved_name, is_fully_qualified)) { |
4727 | 966 | zend_string_release_ex(resolved_name, 0); |
4728 | 966 | return FAILURE; |
4729 | 966 | } |
4730 | | |
4731 | 2.85k | zend_string_release_ex(resolved_name, 0); |
4732 | 2.85k | strict = zend_is_true(&value); |
4733 | 2.85k | zval_ptr_dtor(&value); |
4734 | 2.85k | } else { |
4735 | 588 | return FAILURE; |
4736 | 588 | } |
4737 | 4.69k | } else if (args->children != 2) { |
4738 | 1.36k | return FAILURE; |
4739 | 1.36k | } |
4740 | | |
4741 | 4.13k | if (args->child[1]->kind != ZEND_AST_ARRAY |
4742 | 3.84k | || !zend_try_ct_eval_array(&array.u.constant, args->child[1])) { |
4743 | 478 | return FAILURE; |
4744 | 478 | } |
4745 | | |
4746 | 3.65k | if (zend_hash_num_elements(Z_ARRVAL(array.u.constant)) > 0) { |
4747 | 995 | bool ok = true; |
4748 | 995 | zval *val, tmp; |
4749 | 995 | HashTable *src = Z_ARRVAL(array.u.constant); |
4750 | 995 | HashTable *dst = zend_new_array(zend_hash_num_elements(src)); |
4751 | | |
4752 | 995 | ZVAL_TRUE(&tmp); |
4753 | | |
4754 | 995 | if (strict) { |
4755 | 1.67k | ZEND_HASH_FOREACH_VAL(src, val) { |
4756 | 1.67k | if (Z_TYPE_P(val) == IS_STRING) { |
4757 | 57 | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4758 | 454 | } else if (Z_TYPE_P(val) == IS_LONG) { |
4759 | 388 | zend_hash_index_add(dst, Z_LVAL_P(val), &tmp); |
4760 | 388 | } else { |
4761 | 66 | zend_array_destroy(dst); |
4762 | 66 | ok = false; |
4763 | 66 | break; |
4764 | 66 | } |
4765 | 1.67k | } ZEND_HASH_FOREACH_END(); |
4766 | 717 | } else { |
4767 | 4.17k | ZEND_HASH_FOREACH_VAL(src, val) { |
4768 | 4.17k | if (Z_TYPE_P(val) != IS_STRING |
4769 | 1.20k | || is_numeric_string(Z_STRVAL_P(val), Z_STRLEN_P(val), NULL, NULL, 0)) { |
4770 | 483 | zend_array_destroy(dst); |
4771 | 483 | ok = false; |
4772 | 483 | break; |
4773 | 483 | } |
4774 | 1.01k | zend_hash_add(dst, Z_STR_P(val), &tmp); |
4775 | 1.01k | } ZEND_HASH_FOREACH_END(); |
4776 | 717 | } |
4777 | | |
4778 | 995 | zend_array_destroy(src); |
4779 | 995 | if (!ok) { |
4780 | 549 | return FAILURE; |
4781 | 549 | } |
4782 | 446 | Z_ARRVAL(array.u.constant) = dst; |
4783 | 446 | } |
4784 | 3.10k | array.op_type = IS_CONST; |
4785 | | |
4786 | 3.10k | zend_compile_expr(&needly, args->child[0]); |
4787 | | |
4788 | 3.10k | opline = zend_emit_op_tmp(result, ZEND_IN_ARRAY, &needly, &array); |
4789 | 3.10k | opline->extended_value = strict; |
4790 | | |
4791 | 3.10k | return SUCCESS; |
4792 | 3.65k | } |
4793 | | /* }}} */ |
4794 | | |
4795 | | static zend_result zend_compile_func_count(znode *result, const zend_ast_list *args, const zend_string *lcname) /* {{{ */ |
4796 | 1.75k | { |
4797 | 1.75k | znode arg_node; |
4798 | 1.75k | zend_op *opline; |
4799 | | |
4800 | 1.75k | if (args->children != 1) { |
4801 | 82 | return FAILURE; |
4802 | 82 | } |
4803 | | |
4804 | 1.67k | zend_compile_expr(&arg_node, args->child[0]); |
4805 | 1.67k | opline = zend_emit_op_tmp(result, ZEND_COUNT, &arg_node, NULL); |
4806 | 1.67k | opline->extended_value = zend_string_equals_literal(lcname, "sizeof"); |
4807 | | |
4808 | 1.67k | return SUCCESS; |
4809 | 1.75k | } |
4810 | | /* }}} */ |
4811 | | |
4812 | | static zend_result zend_compile_func_get_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4813 | 1.43k | { |
4814 | 1.43k | if (args->children == 0) { |
4815 | 106 | zend_emit_op_tmp(result, ZEND_GET_CLASS, NULL, NULL); |
4816 | 1.33k | } else { |
4817 | 1.33k | znode arg_node; |
4818 | | |
4819 | 1.33k | if (args->children != 1) { |
4820 | 194 | return FAILURE; |
4821 | 194 | } |
4822 | | |
4823 | 1.13k | zend_compile_expr(&arg_node, args->child[0]); |
4824 | 1.13k | zend_emit_op_tmp(result, ZEND_GET_CLASS, &arg_node, NULL); |
4825 | 1.13k | } |
4826 | 1.24k | return SUCCESS; |
4827 | 1.43k | } |
4828 | | /* }}} */ |
4829 | | |
4830 | | static zend_result zend_compile_func_get_called_class(znode *result, const zend_ast_list *args) /* {{{ */ |
4831 | 373 | { |
4832 | 373 | if (args->children != 0) { |
4833 | 172 | return FAILURE; |
4834 | 172 | } |
4835 | | |
4836 | 201 | zend_emit_op_tmp(result, ZEND_GET_CALLED_CLASS, NULL, NULL); |
4837 | 201 | return SUCCESS; |
4838 | 373 | } |
4839 | | /* }}} */ |
4840 | | |
4841 | | static zend_result zend_compile_func_gettype(znode *result, const zend_ast_list *args) /* {{{ */ |
4842 | 1.20k | { |
4843 | 1.20k | znode arg_node; |
4844 | | |
4845 | 1.20k | if (args->children != 1) { |
4846 | 728 | return FAILURE; |
4847 | 728 | } |
4848 | | |
4849 | 473 | zend_compile_expr(&arg_node, args->child[0]); |
4850 | 473 | zend_emit_op_tmp(result, ZEND_GET_TYPE, &arg_node, NULL); |
4851 | 473 | return SUCCESS; |
4852 | 1.20k | } |
4853 | | /* }}} */ |
4854 | | |
4855 | | static zend_result zend_compile_func_num_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4856 | 313 | { |
4857 | 313 | if (CG(active_op_array)->function_name && args->children == 0) { |
4858 | 269 | zend_emit_op_tmp(result, ZEND_FUNC_NUM_ARGS, NULL, NULL); |
4859 | 269 | return SUCCESS; |
4860 | 269 | } else { |
4861 | 44 | return FAILURE; |
4862 | 44 | } |
4863 | 313 | } |
4864 | | /* }}} */ |
4865 | | |
4866 | | static zend_result zend_compile_func_get_args(znode *result, const zend_ast_list *args) /* {{{ */ |
4867 | 449 | { |
4868 | 449 | if (CG(active_op_array)->function_name && args->children == 0) { |
4869 | 332 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, NULL, NULL); |
4870 | 332 | return SUCCESS; |
4871 | 332 | } else { |
4872 | 117 | return FAILURE; |
4873 | 117 | } |
4874 | 449 | } |
4875 | | /* }}} */ |
4876 | | |
4877 | | static zend_result zend_compile_func_array_key_exists(znode *result, const zend_ast_list *args) /* {{{ */ |
4878 | 285 | { |
4879 | 285 | znode subject, needle; |
4880 | | |
4881 | 285 | if (args->children != 2) { |
4882 | 50 | return FAILURE; |
4883 | 50 | } |
4884 | | |
4885 | 235 | zend_compile_expr(&needle, args->child[0]); |
4886 | 235 | zend_compile_expr(&subject, args->child[1]); |
4887 | | |
4888 | 235 | zend_emit_op_tmp(result, ZEND_ARRAY_KEY_EXISTS, &needle, &subject); |
4889 | 235 | return SUCCESS; |
4890 | 285 | } |
4891 | | /* }}} */ |
4892 | | |
4893 | | static zend_result zend_compile_func_array_slice(znode *result, const zend_ast_list *args) /* {{{ */ |
4894 | 1.45k | { |
4895 | 1.45k | if (CG(active_op_array)->function_name |
4896 | 974 | && args->children == 2 |
4897 | 762 | && args->child[0]->kind == ZEND_AST_CALL |
4898 | 678 | && args->child[0]->child[0]->kind == ZEND_AST_ZVAL |
4899 | 547 | && Z_TYPE_P(zend_ast_get_zval(args->child[0]->child[0])) == IS_STRING |
4900 | 547 | && args->child[0]->child[1]->kind == ZEND_AST_ARG_LIST |
4901 | 540 | && args->child[1]->kind == ZEND_AST_ZVAL) { |
4902 | | |
4903 | 468 | zend_string *orig_name = zend_ast_get_str(args->child[0]->child[0]); |
4904 | 468 | bool is_fully_qualified; |
4905 | 468 | zend_string *name = zend_resolve_function_name(orig_name, args->child[0]->child[0]->attr, &is_fully_qualified); |
4906 | 468 | const zend_ast_list *list = zend_ast_get_list(args->child[0]->child[1]); |
4907 | 468 | const zval *zv = zend_ast_get_zval(args->child[1]); |
4908 | 468 | znode first; |
4909 | | |
4910 | 468 | if (zend_string_equals_literal_ci(name, "func_get_args") |
4911 | 390 | && list->children == 0 |
4912 | 351 | && Z_TYPE_P(zv) == IS_LONG |
4913 | 296 | && Z_LVAL_P(zv) >= 0) { |
4914 | 296 | first.op_type = IS_CONST; |
4915 | 296 | ZVAL_LONG(&first.u.constant, Z_LVAL_P(zv)); |
4916 | 296 | zend_emit_op_tmp(result, ZEND_FUNC_GET_ARGS, &first, NULL); |
4917 | 296 | zend_string_release_ex(name, 0); |
4918 | 296 | return SUCCESS; |
4919 | 296 | } |
4920 | 172 | zend_string_release_ex(name, 0); |
4921 | 172 | } |
4922 | 1.15k | return FAILURE; |
4923 | 1.45k | } |
4924 | | /* }}} */ |
4925 | | |
4926 | | static uint32_t find_frameless_function_offset(uint32_t arity, const void *handler) |
4927 | 1.01M | { |
4928 | 1.01M | void **handlers = zend_flf_handlers; |
4929 | 1.01M | void **current = handlers; |
4930 | 9.29M | while (current) { |
4931 | 9.29M | if (*current == handler) { |
4932 | 1.01M | return current - handlers; |
4933 | 1.01M | } |
4934 | 8.28M | current++; |
4935 | 8.28M | } |
4936 | | |
4937 | 0 | return (uint32_t)-1; |
4938 | 1.01M | } |
4939 | | |
4940 | | static const zend_frameless_function_info *find_frameless_function_info(const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
4941 | 645k | { |
4942 | 645k | if (zend_execute_internal) { |
4943 | 118k | return NULL; |
4944 | 118k | } |
4945 | | |
4946 | 527k | if (ZEND_USER_CODE(fbc->type)) { |
4947 | 6 | return NULL; |
4948 | 6 | } |
4949 | | |
4950 | 527k | const zend_frameless_function_info *frameless_function_info = fbc->internal_function.frameless_function_infos; |
4951 | 527k | if (!frameless_function_info) { |
4952 | 17.4k | return NULL; |
4953 | 17.4k | } |
4954 | | |
4955 | 509k | if (args->children > 3) { |
4956 | 268 | return NULL; |
4957 | 268 | } |
4958 | | |
4959 | 623k | while (frameless_function_info->handler) { |
4960 | 620k | if (frameless_function_info->num_args >= args->children |
4961 | 512k | && fbc->common.required_num_args <= args->children |
4962 | 506k | && (!(fbc->common.fn_flags & ZEND_ACC_VARIADIC) |
4963 | 506k | || frameless_function_info->num_args == args->children)) { |
4964 | 506k | uint32_t num_args = frameless_function_info->num_args; |
4965 | 506k | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4966 | 506k | if (offset == (uint32_t)-1) { |
4967 | 0 | continue; |
4968 | 0 | } |
4969 | 506k | return frameless_function_info; |
4970 | 506k | } |
4971 | 114k | frameless_function_info++; |
4972 | 114k | } |
4973 | | |
4974 | 2.98k | return NULL; |
4975 | 509k | } |
4976 | | |
4977 | | 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) |
4978 | 506k | { |
4979 | 506k | uint32_t lineno = CG(zend_lineno); |
4980 | 506k | uint32_t num_args = frameless_function_info->num_args; |
4981 | 506k | uint32_t offset = find_frameless_function_offset(num_args, frameless_function_info->handler); |
4982 | 506k | znode arg_zvs[3]; |
4983 | 1.62M | for (uint32_t i = 0; i < num_args; i++) { |
4984 | 1.11M | if (i < args->children) { |
4985 | 1.11M | zend_compile_expr(&arg_zvs[i], args->child[i]); |
4986 | 1.11M | } else { |
4987 | 0 | const zend_arg_info *arg_info = &fbc->common.arg_info[i]; |
4988 | 0 | arg_zvs[i].op_type = IS_CONST; |
4989 | 0 | if (zend_get_default_from_internal_arg_info(&arg_zvs[i].u.constant, arg_info) == FAILURE) { |
4990 | 0 | ZEND_UNREACHABLE(); |
4991 | 0 | } |
4992 | 0 | } |
4993 | 1.11M | } |
4994 | 506k | uint8_t opcode = ZEND_FRAMELESS_ICALL_0 + num_args; |
4995 | 506k | uint32_t opnum = get_next_op_number(); |
4996 | 506k | zend_op *opline = zend_emit_op_tmp(result, opcode, NULL, NULL); |
4997 | 506k | opline->extended_value = offset; |
4998 | 506k | opline->lineno = lineno; |
4999 | 506k | if (num_args >= 1) { |
5000 | 505k | SET_NODE(opline->op1, &arg_zvs[0]); |
5001 | 505k | } |
5002 | 506k | if (num_args >= 2) { |
5003 | 503k | SET_NODE(opline->op2, &arg_zvs[1]); |
5004 | 503k | } |
5005 | 506k | if (num_args >= 3) { |
5006 | 108k | zend_emit_op_data(&arg_zvs[2]); |
5007 | 108k | } |
5008 | 506k | return opnum; |
5009 | 506k | } |
5010 | | |
5011 | | static uint32_t zend_compile_frameless_icall(znode *result, const zend_ast_list *args, const zend_function *fbc, uint32_t type) |
5012 | 130k | { |
5013 | 130k | const zend_frameless_function_info *frameless_function_info = find_frameless_function_info(args, fbc, type); |
5014 | 130k | if (!frameless_function_info) { |
5015 | 127k | return (uint32_t)-1; |
5016 | 127k | } |
5017 | | |
5018 | 2.59k | return zend_compile_frameless_icall_ex(result, args, fbc, frameless_function_info, type); |
5019 | 130k | } |
5020 | | |
5021 | | static void zend_compile_ns_call(znode *result, const znode *name_node, zend_ast *args_ast, uint32_t lineno, uint32_t type) /* {{{ */ |
5022 | 2.78M | { |
5023 | 2.78M | int name_constants = zend_add_ns_func_name_literal(Z_STR(name_node->u.constant)); |
5024 | | |
5025 | | /* Find frameless function with same name. */ |
5026 | 2.78M | const zend_function *frameless_function = NULL; |
5027 | 2.78M | if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT |
5028 | 2.78M | && !zend_args_contain_unpack_or_named(zend_ast_get_list(args_ast)) |
5029 | | /* Avoid blowing up op count with nested frameless branches. */ |
5030 | 2.78M | && !CG(context).in_jmp_frameless_branch) { |
5031 | 1.80M | zend_string *lc_func_name = Z_STR_P(CT_CONSTANT_EX(CG(active_op_array), name_constants + 2)); |
5032 | 1.80M | frameless_function = zend_hash_find_ptr(CG(function_table), lc_func_name); |
5033 | 1.80M | } |
5034 | | |
5035 | | /* Check whether any frameless handler may actually be used. */ |
5036 | 2.78M | uint32_t jmp_fl_opnum = 0; |
5037 | 2.78M | const zend_frameless_function_info *frameless_function_info = NULL; |
5038 | 2.78M | if (frameless_function) { |
5039 | 515k | frameless_function_info = find_frameless_function_info(zend_ast_get_list(args_ast), frameless_function, type); |
5040 | 515k | if (frameless_function_info) { |
5041 | 503k | CG(context).in_jmp_frameless_branch = true; |
5042 | 503k | znode op1; |
5043 | 503k | op1.op_type = IS_CONST; |
5044 | 503k | ZVAL_COPY(&op1.u.constant, CT_CONSTANT_EX(CG(active_op_array), name_constants + 1)); |
5045 | 503k | jmp_fl_opnum = get_next_op_number(); |
5046 | 503k | zend_emit_op(NULL, ZEND_JMP_FRAMELESS, &op1, NULL); |
5047 | 503k | } |
5048 | 515k | } |
5049 | | |
5050 | | /* Compile ns call. */ |
5051 | 2.78M | zend_op *opline = get_next_op(); |
5052 | 2.78M | opline->opcode = ZEND_INIT_NS_FCALL_BY_NAME; |
5053 | 2.78M | opline->op2_type = IS_CONST; |
5054 | 2.78M | opline->op2.constant = name_constants; |
5055 | 2.78M | opline->result.num = zend_alloc_cache_slot(); |
5056 | 2.78M | zend_compile_call_common(result, args_ast, NULL, lineno, type); |
5057 | | |
5058 | | /* Compile frameless call. */ |
5059 | 2.78M | if (frameless_function_info) { |
5060 | 503k | CG(zend_lineno) = lineno; |
5061 | | |
5062 | 503k | uint32_t jmp_end_opnum = zend_emit_jump(0); |
5063 | 503k | uint32_t jmp_fl_target = get_next_op_number(); |
5064 | | |
5065 | 503k | uint32_t flf_icall_opnum = zend_compile_frameless_icall_ex(NULL, zend_ast_get_list(args_ast), frameless_function, frameless_function_info, type); |
5066 | | |
5067 | 503k | zend_op *jmp_fl = &CG(active_op_array)->opcodes[jmp_fl_opnum]; |
5068 | 503k | jmp_fl->op2.opline_num = jmp_fl_target; |
5069 | 503k | jmp_fl->extended_value = zend_alloc_cache_slot(); |
5070 | 503k | zend_op *flf_icall = &CG(active_op_array)->opcodes[flf_icall_opnum]; |
5071 | 503k | SET_NODE(flf_icall->result, result); |
5072 | 503k | zend_update_jump_target_to_next(jmp_end_opnum); |
5073 | | |
5074 | 503k | CG(context).in_jmp_frameless_branch = false; |
5075 | 503k | } |
5076 | 2.78M | } |
5077 | | /* }}} */ |
5078 | | |
5079 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node); |
5080 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node); |
5081 | | static void zend_compile_rope_finalize(znode *result, uint32_t j, zend_op *init_opline, zend_op *opline); |
5082 | | |
5083 | | static zend_result zend_compile_func_sprintf(znode *result, zend_ast_list *args) /* {{{ */ |
5084 | 5.08k | { |
5085 | | /* Bail out if we do not have a format string. */ |
5086 | 5.08k | if (args->children < 1) { |
5087 | 73 | return FAILURE; |
5088 | 73 | } |
5089 | | |
5090 | 5.01k | zend_eval_const_expr(&args->child[0]); |
5091 | | /* Bail out if the format string is not constant. */ |
5092 | 5.01k | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
5093 | 424 | return FAILURE; |
5094 | 424 | } |
5095 | | |
5096 | 4.59k | zval *format_string = zend_ast_get_zval(args->child[0]); |
5097 | 4.59k | if (Z_TYPE_P(format_string) != IS_STRING) { |
5098 | 72 | return FAILURE; |
5099 | 72 | } |
5100 | 4.52k | if (Z_STRLEN_P(format_string) >= 256) { |
5101 | 56 | return FAILURE; |
5102 | 56 | } |
5103 | | |
5104 | 4.46k | char *p; |
5105 | 4.46k | char *end; |
5106 | 4.46k | uint32_t placeholder_count; |
5107 | | |
5108 | 4.46k | placeholder_count = 0; |
5109 | 4.46k | p = Z_STRVAL_P(format_string); |
5110 | 4.46k | end = p + Z_STRLEN_P(format_string); |
5111 | | |
5112 | 9.82k | for (;;) { |
5113 | 9.82k | p = memchr(p, '%', end - p); |
5114 | 9.82k | if (!p) { |
5115 | 3.75k | break; |
5116 | 3.75k | } |
5117 | | |
5118 | 6.06k | char *q = p + 1; |
5119 | 6.06k | if (q == end) { |
5120 | 310 | return FAILURE; |
5121 | 310 | } |
5122 | | |
5123 | 5.75k | switch (*q) { |
5124 | 4.01k | case 's': |
5125 | 4.59k | case 'd': |
5126 | 4.59k | placeholder_count++; |
5127 | 4.59k | break; |
5128 | 770 | case '%': |
5129 | 770 | break; |
5130 | 395 | default: |
5131 | 395 | return FAILURE; |
5132 | 5.75k | } |
5133 | | |
5134 | 5.36k | p = q; |
5135 | 5.36k | p++; |
5136 | 5.36k | } |
5137 | | |
5138 | | /* Bail out if the number of placeholders does not match the number of values. */ |
5139 | 3.75k | if (placeholder_count != (args->children - 1)) { |
5140 | 130 | return FAILURE; |
5141 | 130 | } |
5142 | | |
5143 | | /* Handle empty format strings. */ |
5144 | 3.62k | if (Z_STRLEN_P(format_string) == 0) { |
5145 | 64 | result->op_type = IS_CONST; |
5146 | 64 | ZVAL_EMPTY_STRING(&result->u.constant); |
5147 | | |
5148 | 64 | return SUCCESS; |
5149 | 64 | } |
5150 | | |
5151 | 3.56k | znode *elements = NULL; |
5152 | | |
5153 | 3.56k | if (placeholder_count > 0) { |
5154 | 3.22k | elements = safe_emalloc(sizeof(*elements), placeholder_count, 0); |
5155 | 3.22k | } |
5156 | | |
5157 | | /* Compile the value expressions first for error handling that is consistent |
5158 | | * with a function call: Values that fail to convert to a string may emit errors. |
5159 | | */ |
5160 | 7.95k | for (uint32_t i = 0; i < placeholder_count; i++) { |
5161 | 4.38k | zend_compile_expr(elements + i, args->child[1 + i]); |
5162 | 4.38k | } |
5163 | | |
5164 | 3.56k | uint32_t rope_elements = 0; |
5165 | 3.56k | uint32_t rope_init_lineno = -1; |
5166 | 3.56k | zend_op *opline = NULL; |
5167 | | |
5168 | 3.56k | placeholder_count = 0; |
5169 | 3.56k | p = Z_STRVAL_P(format_string); |
5170 | 3.56k | end = p + Z_STRLEN_P(format_string); |
5171 | 3.56k | char *offset = p; |
5172 | 8.49k | for (;;) { |
5173 | 8.49k | p = memchr(p, '%', end - p); |
5174 | 8.49k | if (!p) { |
5175 | 3.56k | break; |
5176 | 3.56k | } |
5177 | | |
5178 | 4.92k | char *q = p + 1; |
5179 | 4.92k | ZEND_ASSERT(q < end); |
5180 | 4.92k | ZEND_ASSERT(*q == 's' || *q == 'd' || *q == '%'); |
5181 | | |
5182 | 4.92k | if (*q == '%') { |
5183 | | /* Optimization to not create a dedicated rope element for the literal '%': |
5184 | | * Include the first '%' within the "constant" part instead of dropping the |
5185 | | * full placeholder. |
5186 | | */ |
5187 | 537 | p++; |
5188 | 537 | } |
5189 | | |
5190 | 4.92k | if (p != offset) { |
5191 | 3.32k | znode const_node; |
5192 | 3.32k | const_node.op_type = IS_CONST; |
5193 | 3.32k | ZVAL_STRINGL(&const_node.u.constant, offset, p - offset); |
5194 | 3.32k | if (rope_elements == 0) { |
5195 | 1.69k | rope_init_lineno = get_next_op_number(); |
5196 | 1.69k | } |
5197 | 3.32k | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
5198 | 3.32k | } |
5199 | | |
5200 | 4.92k | if (*q != '%') { |
5201 | 4.38k | switch (*q) { |
5202 | 3.91k | case 's': |
5203 | | /* Perform the cast of constants when actually evaluating the corresponding placeholder |
5204 | | * for correct error reporting. |
5205 | | */ |
5206 | 3.91k | if (elements[placeholder_count].op_type == IS_CONST) { |
5207 | 602 | if (Z_TYPE(elements[placeholder_count].u.constant) == IS_ARRAY) { |
5208 | 39 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_STRING; |
5209 | 563 | } else { |
5210 | 563 | convert_to_string(&elements[placeholder_count].u.constant); |
5211 | 563 | } |
5212 | 602 | } |
5213 | 3.91k | break; |
5214 | 470 | case 'd': |
5215 | 470 | zend_emit_op_tmp(&elements[placeholder_count], ZEND_CAST, &elements[placeholder_count], NULL)->extended_value = IS_LONG; |
5216 | 470 | break; |
5217 | 0 | default: ZEND_UNREACHABLE(); |
5218 | 4.38k | } |
5219 | | |
5220 | 4.38k | if (rope_elements == 0) { |
5221 | 1.54k | rope_init_lineno = get_next_op_number(); |
5222 | 1.54k | } |
5223 | 4.38k | opline = zend_compile_rope_add(result, rope_elements++, &elements[placeholder_count]); |
5224 | | |
5225 | 4.38k | placeholder_count++; |
5226 | 4.38k | } |
5227 | | |
5228 | 4.92k | p = q; |
5229 | 4.92k | p++; |
5230 | 4.92k | offset = p; |
5231 | 4.92k | } |
5232 | 3.56k | if (end != offset) { |
5233 | | /* Add the constant part after the last placeholder. */ |
5234 | 3.08k | znode const_node; |
5235 | 3.08k | const_node.op_type = IS_CONST; |
5236 | 3.08k | ZVAL_STRINGL(&const_node.u.constant, offset, end - offset); |
5237 | 3.08k | if (rope_elements == 0) { |
5238 | 323 | rope_init_lineno = get_next_op_number(); |
5239 | 323 | } |
5240 | 3.08k | opline = zend_compile_rope_add(result, rope_elements++, &const_node); |
5241 | 3.08k | } |
5242 | 3.56k | ZEND_ASSERT(opline != NULL); |
5243 | | |
5244 | 3.56k | zend_op *init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
5245 | 3.56k | zend_compile_rope_finalize(result, rope_elements, init_opline, opline); |
5246 | 3.56k | efree(elements); |
5247 | | |
5248 | 3.56k | return SUCCESS; |
5249 | 3.56k | } |
5250 | | |
5251 | | static zend_result zend_compile_func_printf(znode *result, zend_ast_list *args) /* {{{ */ |
5252 | 4.58k | { |
5253 | | /* Special case: printf with a single constant string argument and no format specifiers. |
5254 | | * In this case, just emit ECHO and return the string length if needed. */ |
5255 | 4.58k | if (args->children == 1) { |
5256 | 1.13k | zend_eval_const_expr(&args->child[0]); |
5257 | 1.13k | if (args->child[0]->kind != ZEND_AST_ZVAL) { |
5258 | 166 | return FAILURE; |
5259 | 166 | } |
5260 | 972 | zval *format_string = zend_ast_get_zval(args->child[0]); |
5261 | 972 | if (Z_TYPE_P(format_string) != IS_STRING) { |
5262 | 216 | return FAILURE; |
5263 | 216 | } |
5264 | | /* Check if there are any format specifiers */ |
5265 | 756 | if (!memchr(Z_STRVAL_P(format_string), '%', Z_STRLEN_P(format_string))) { |
5266 | | /* No format specifiers - just emit ECHO and return string length */ |
5267 | 402 | znode format_node; |
5268 | 402 | zend_compile_expr(&format_node, args->child[0]); |
5269 | 402 | zend_emit_op(NULL, ZEND_ECHO, &format_node, NULL); |
5270 | | |
5271 | | /* Return the string length as a constant if the result is used */ |
5272 | 402 | result->op_type = IS_CONST; |
5273 | 402 | ZVAL_LONG(&result->u.constant, Z_STRLEN_P(format_string)); |
5274 | 402 | return SUCCESS; |
5275 | 402 | } |
5276 | 756 | } |
5277 | | |
5278 | | /* Fall back to sprintf optimization for format strings with specifiers */ |
5279 | 3.80k | znode rope_result; |
5280 | 3.80k | if (zend_compile_func_sprintf(&rope_result, args) != SUCCESS) { |
5281 | 809 | return FAILURE; |
5282 | 809 | } |
5283 | | |
5284 | | /* printf() returns the amount of bytes written, so just an ECHO of the |
5285 | | * resulting sprintf() optimisation might not be enough. At this early |
5286 | | * stage we can't detect if the result is actually used, so we just emit |
5287 | | * the opcodes and let them be cleaned up by the dead code elimination |
5288 | | * pass in the Zend Optimizer if the result of the printf() is in fact |
5289 | | * unused */ |
5290 | 2.99k | znode copy; |
5291 | 2.99k | if (rope_result.op_type != IS_CONST) { |
5292 | | /* Note: ZEND_COPY_TMP is only valid for TMPVAR. */ |
5293 | 2.78k | ZEND_ASSERT(rope_result.op_type == IS_TMP_VAR); |
5294 | 2.78k | zend_emit_op_tmp(©, ZEND_COPY_TMP, &rope_result, NULL); |
5295 | 2.78k | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5296 | 2.78k | zend_emit_op_tmp(result, ZEND_STRLEN, ©, NULL); |
5297 | 2.78k | } else { |
5298 | 208 | zend_emit_op(NULL, ZEND_ECHO, &rope_result, NULL); |
5299 | 208 | result->op_type = IS_CONST; |
5300 | 208 | ZVAL_LONG(&result->u.constant, Z_STRLEN(rope_result.u.constant)); |
5301 | 208 | } |
5302 | | |
5303 | 2.99k | return SUCCESS; |
5304 | 2.99k | } |
5305 | | |
5306 | | static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *args) |
5307 | 1.58k | { |
5308 | 1.58k | znode arg_node; |
5309 | | |
5310 | 1.58k | if (args->children != 1) { |
5311 | 449 | return FAILURE; |
5312 | 449 | } |
5313 | | |
5314 | 1.13k | zend_compile_expr(&arg_node, args->child[0]); |
5315 | 1.13k | zend_emit_op_tmp(result, ZEND_CLONE, &arg_node, NULL); |
5316 | | |
5317 | 1.13k | return SUCCESS; |
5318 | 1.58k | } |
5319 | | |
5320 | | static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ |
5321 | 734 | { |
5322 | | /* Bail out if we do not have exactly two parameters. */ |
5323 | 734 | if (args->children != 2) { |
5324 | 91 | return FAILURE; |
5325 | 91 | } |
5326 | | |
5327 | 643 | zend_ast *callback = args->child[0]; |
5328 | 643 | if (callback->kind != ZEND_AST_CALL && callback->kind != ZEND_AST_STATIC_CALL) { |
5329 | 419 | return FAILURE; |
5330 | 419 | } |
5331 | | |
5332 | 224 | znode value; |
5333 | 224 | value.op_type = IS_TMP_VAR; |
5334 | 224 | value.u.op.var = get_temporary_variable(); |
5335 | | |
5336 | 224 | zend_ast *call_args = zend_partial_apply(callback, |
5337 | 224 | zend_ast_create_znode(&value)); |
5338 | 224 | if (!call_args) { |
5339 | 49 | CG(active_op_array)->T--; |
5340 | | /* The callback is not a FCC/PFA, or is not optimizable */ |
5341 | 49 | return FAILURE; |
5342 | 49 | } |
5343 | | |
5344 | 175 | zend_op *opline; |
5345 | | |
5346 | 175 | znode array; |
5347 | 175 | zend_compile_expr(&array, args->child[1]); |
5348 | | /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */ |
5349 | 175 | if (array.op_type == IS_CONST) { |
5350 | 23 | Z_TRY_ADDREF(array.u.constant); |
5351 | 23 | } |
5352 | | |
5353 | | /* Verify that the input array actually is an array. */ |
5354 | 175 | znode name; |
5355 | 175 | name.op_type = IS_CONST; |
5356 | 175 | ZVAL_STR_COPY(&name.u.constant, lcname); |
5357 | 175 | opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array); |
5358 | 175 | opline->lineno = lineno; |
5359 | 175 | opline->extended_value = (2 << 16) | IS_ARRAY; |
5360 | 175 | const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5361 | 175 | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5362 | 175 | Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData; |
5363 | | |
5364 | | /* Initialize the result array. */ |
5365 | 175 | zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
5366 | | |
5367 | | /* foreach loop starts here. */ |
5368 | 175 | znode key; |
5369 | | |
5370 | 175 | uint32_t opnum_reset = get_next_op_number(); |
5371 | 175 | znode reset_node; |
5372 | 175 | zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL); |
5373 | 175 | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
5374 | 175 | uint32_t opnum_fetch = get_next_op_number(); |
5375 | 175 | zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value); |
5376 | | |
5377 | | /* loop body */ |
5378 | 175 | znode call_result; |
5379 | 175 | switch (callback->kind) { |
5380 | 153 | case ZEND_AST_CALL: |
5381 | 153 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args)); |
5382 | 153 | break; |
5383 | 22 | case ZEND_AST_STATIC_CALL: |
5384 | 22 | zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args)); |
5385 | 22 | break; |
5386 | 175 | } |
5387 | 175 | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key); |
5388 | 175 | SET_NODE(opline->result, result); |
5389 | | /* end loop body */ |
5390 | | |
5391 | 175 | zend_emit_jump(opnum_fetch); |
5392 | | |
5393 | 175 | uint32_t opnum_loop_end = get_next_op_number(); |
5394 | 175 | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
5395 | 175 | opline->op2.opline_num = opnum_loop_end; |
5396 | 175 | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
5397 | 175 | opline->extended_value = opnum_loop_end; |
5398 | | |
5399 | 175 | zend_end_loop(opnum_fetch, &reset_node); |
5400 | 175 | zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
5401 | | |
5402 | 175 | return SUCCESS; |
5403 | 175 | } |
5404 | | |
5405 | | static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */ |
5406 | 160k | { |
5407 | 160k | if (zend_string_equals_literal(lcname, "strlen")) { |
5408 | 1.18k | return zend_compile_func_strlen(result, args); |
5409 | 158k | } else if (zend_string_equals_literal(lcname, "is_null")) { |
5410 | 239 | return zend_compile_func_typecheck(result, args, IS_NULL); |
5411 | 158k | } else if (zend_string_equals_literal(lcname, "is_bool")) { |
5412 | 416 | return zend_compile_func_typecheck(result, args, _IS_BOOL); |
5413 | 158k | } else if (zend_string_equals_literal(lcname, "is_int")) { |
5414 | 203 | return zend_compile_func_typecheck(result, args, IS_LONG); |
5415 | 158k | } else if (zend_string_equals_literal(lcname, "is_float")) { |
5416 | 541 | return zend_compile_func_typecheck(result, args, IS_DOUBLE); |
5417 | 157k | } else if (zend_string_equals_literal(lcname, "is_string")) { |
5418 | 42 | return zend_compile_func_typecheck(result, args, IS_STRING); |
5419 | 157k | } else if (zend_string_equals_literal(lcname, "is_array")) { |
5420 | 254 | return zend_compile_func_typecheck(result, args, IS_ARRAY); |
5421 | 157k | } else if (zend_string_equals_literal(lcname, "is_object")) { |
5422 | 182 | return zend_compile_func_typecheck(result, args, IS_OBJECT); |
5423 | 157k | } else if (zend_string_equals_literal(lcname, "is_resource")) { |
5424 | 348 | return zend_compile_func_typecheck(result, args, IS_RESOURCE); |
5425 | 156k | } else if (zend_string_equals_literal(lcname, "is_scalar")) { |
5426 | 183 | return zend_compile_func_is_scalar(result, args); |
5427 | 156k | } else if (zend_string_equals_literal(lcname, "boolval")) { |
5428 | 208 | return zend_compile_func_cast(result, args, _IS_BOOL); |
5429 | 156k | } else if (zend_string_equals_literal(lcname, "intval")) { |
5430 | 116 | return zend_compile_func_cast(result, args, IS_LONG); |
5431 | 156k | } else if (zend_string_equals_literal(lcname, "floatval")) { |
5432 | 797 | return zend_compile_func_cast(result, args, IS_DOUBLE); |
5433 | 155k | } else if (zend_string_equals_literal(lcname, "strval")) { |
5434 | 360 | return zend_compile_func_cast(result, args, IS_STRING); |
5435 | 155k | } else if (zend_string_equals_literal(lcname, "defined")) { |
5436 | 1.93k | return zend_compile_func_defined(result, args); |
5437 | 153k | } else if (zend_string_equals_literal(lcname, "chr") && type == BP_VAR_R) { |
5438 | 890 | return zend_compile_func_chr(result, args); |
5439 | 152k | } else if (zend_string_equals_literal(lcname, "ord") && type == BP_VAR_R) { |
5440 | 682 | return zend_compile_func_ord(result, args); |
5441 | 151k | } else if (zend_string_equals_literal(lcname, "call_user_func_array")) { |
5442 | 2.10k | return zend_compile_func_cufa(result, args, lcname, type); |
5443 | 149k | } else if (zend_string_equals_literal(lcname, "call_user_func")) { |
5444 | 8.94k | return zend_compile_func_cuf(result, args, lcname, type); |
5445 | 140k | } else if (zend_string_equals_literal(lcname, "in_array")) { |
5446 | 7.05k | return zend_compile_func_in_array(result, args); |
5447 | 133k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_COUNT)) |
5448 | 131k | || zend_string_equals_literal(lcname, "sizeof")) { |
5449 | 1.75k | return zend_compile_func_count(result, args, lcname); |
5450 | 131k | } else if (zend_string_equals_literal(lcname, "get_class")) { |
5451 | 1.43k | return zend_compile_func_get_class(result, args); |
5452 | 130k | } else if (zend_string_equals_literal(lcname, "get_called_class")) { |
5453 | 373 | return zend_compile_func_get_called_class(result, args); |
5454 | 129k | } else if (zend_string_equals_literal(lcname, "gettype")) { |
5455 | 1.20k | return zend_compile_func_gettype(result, args); |
5456 | 128k | } else if (zend_string_equals_literal(lcname, "func_num_args")) { |
5457 | 313 | return zend_compile_func_num_args(result, args); |
5458 | 128k | } else if (zend_string_equals_literal(lcname, "func_get_args")) { |
5459 | 449 | return zend_compile_func_get_args(result, args); |
5460 | 127k | } else if (zend_string_equals_literal(lcname, "array_slice")) { |
5461 | 1.45k | return zend_compile_func_array_slice(result, args); |
5462 | 126k | } else if (zend_string_equals_literal(lcname, "array_key_exists")) { |
5463 | 285 | return zend_compile_func_array_key_exists(result, args); |
5464 | 126k | } else if (zend_string_equals_literal(lcname, "sprintf")) { |
5465 | 1.28k | return zend_compile_func_sprintf(result, args); |
5466 | 124k | } else if (zend_string_equals_literal(lcname, "printf")) { |
5467 | 4.58k | return zend_compile_func_printf(result, args); |
5468 | 120k | } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) { |
5469 | 1.58k | return zend_compile_func_clone(result, args); |
5470 | 118k | } else if (zend_string_equals_literal(lcname, "array_map")) { |
5471 | 734 | return zend_compile_func_array_map(result, args, lcname, lineno); |
5472 | 117k | } else { |
5473 | 117k | return FAILURE; |
5474 | 117k | } |
5475 | 160k | } |
5476 | | |
5477 | | 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) /* {{{ */ |
5478 | 190k | { |
5479 | 190k | if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) { |
5480 | 0 | return FAILURE; |
5481 | 0 | } |
5482 | | |
5483 | 190k | if (fbc->type != ZEND_INTERNAL_FUNCTION) { |
5484 | | /* If the function is part of disabled_functions, it may be redeclared as a userland |
5485 | | * function with a different implementation. Don't use the VM builtin in that case. */ |
5486 | 29.5k | return FAILURE; |
5487 | 29.5k | } |
5488 | | |
5489 | 161k | if (zend_args_contain_unpack_or_named(args)) { |
5490 | 1.12k | return FAILURE; |
5491 | 1.12k | } |
5492 | | |
5493 | 160k | if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) { |
5494 | 29.5k | return SUCCESS; |
5495 | 29.5k | } |
5496 | | |
5497 | 130k | return zend_compile_frameless_icall(result, args, fbc, type) != (uint32_t)-1 ? SUCCESS : FAILURE; |
5498 | 160k | } |
5499 | | |
5500 | 5 | static const char *zend_get_cstring_from_property_hook_kind(zend_property_hook_kind kind) { |
5501 | 5 | switch (kind) { |
5502 | 0 | case ZEND_PROPERTY_HOOK_GET: |
5503 | 0 | return "get"; |
5504 | 5 | case ZEND_PROPERTY_HOOK_SET: |
5505 | 5 | return "set"; |
5506 | 0 | default: ZEND_UNREACHABLE(); |
5507 | 5 | } |
5508 | 5 | } |
5509 | | |
5510 | | static zend_string *zend_copy_unmangled_prop_name(zend_string *prop_name) |
5511 | 684 | { |
5512 | 684 | if (ZSTR_VAL(prop_name)[0] != '\0') { |
5513 | 500 | return zend_string_copy(prop_name); |
5514 | 500 | } else { |
5515 | 184 | const char *unmangled = zend_get_unmangled_property_name(prop_name); |
5516 | 184 | return zend_string_init(unmangled, strlen(unmangled), /* persistent */ false); |
5517 | 184 | } |
5518 | 684 | } |
5519 | | |
5520 | | static bool zend_compile_parent_property_hook_call(znode *result, const zend_ast *ast, uint32_t type) |
5521 | 17.7k | { |
5522 | 17.7k | ZEND_ASSERT(ast->kind == ZEND_AST_STATIC_CALL); |
5523 | | |
5524 | 17.7k | const zend_ast *class_ast = ast->child[0]; |
5525 | 17.7k | zend_ast *method_ast = ast->child[1]; |
5526 | | |
5527 | 17.7k | if (!zend_ast_is_parent_hook_call(ast)) { |
5528 | 16.9k | return false; |
5529 | 16.9k | } |
5530 | | |
5531 | 810 | zend_class_entry *ce = CG(active_class_entry); |
5532 | 810 | if (!ce) { |
5533 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"parent\" when no class scope is active"); |
5534 | 8 | } |
5535 | | |
5536 | 802 | zend_ast *args_ast = ast->child[2]; |
5537 | 802 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
5538 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for parent property hook call"); |
5539 | 7 | } |
5540 | | |
5541 | 795 | zval *property_hook_name_zv = zend_ast_get_zval(class_ast->child[1]); |
5542 | 795 | zend_string *property_name = zval_get_string(property_hook_name_zv); |
5543 | 795 | zend_string *hook_name = zend_ast_get_str(method_ast); |
5544 | 795 | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(hook_name); |
5545 | 795 | ZEND_ASSERT(hook_kind != (uint32_t)-1); |
5546 | | |
5547 | 795 | const zend_string *prop_info_name = CG(context).active_property_info_name; |
5548 | 795 | if (!prop_info_name) { |
5549 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() outside a property hook", |
5550 | 5 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name)); |
5551 | 5 | } |
5552 | | |
5553 | 790 | const char *unmangled_prop_name = zend_get_unmangled_property_name(prop_info_name); |
5554 | 790 | if (!zend_string_equals_cstr(property_name, unmangled_prop_name, strlen(unmangled_prop_name))) { |
5555 | 24 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property ($%s)", |
5556 | 24 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), unmangled_prop_name); |
5557 | 24 | } |
5558 | 766 | if (hook_kind != CG(context).active_property_hook_kind) { |
5559 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Must not use parent::$%s::%s() in a different property hook (%s)", |
5560 | 5 | ZSTR_VAL(property_name), ZSTR_VAL(hook_name), zend_get_cstring_from_property_hook_kind(CG(context).active_property_hook_kind)); |
5561 | 5 | } |
5562 | | |
5563 | 761 | zend_op *opline = get_next_op(); |
5564 | 761 | opline->opcode = ZEND_INIT_PARENT_PROPERTY_HOOK_CALL; |
5565 | 761 | opline->op1_type = IS_CONST; |
5566 | 761 | opline->op1.constant = zend_add_literal_string(&property_name); |
5567 | 761 | opline->op2.num = hook_kind; |
5568 | | |
5569 | 761 | zend_compile_call_common(result, args_ast, NULL, zend_ast_get_lineno(method_ast), BP_VAR_R); |
5570 | | |
5571 | 761 | return true; |
5572 | 766 | } |
5573 | | |
5574 | | static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) /* {{{ */ |
5575 | 3.13M | { |
5576 | 3.13M | zend_ast *name_ast = ast->child[0]; |
5577 | 3.13M | zend_ast *args_ast = ast->child[1]; |
5578 | 3.13M | bool is_callable_convert = args_ast->kind == ZEND_AST_CALLABLE_CONVERT; |
5579 | | |
5580 | 3.13M | znode name_node; |
5581 | | |
5582 | 3.13M | if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) { |
5583 | 59.2k | zend_compile_expr(&name_node, name_ast); |
5584 | 59.2k | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5585 | 59.2k | return; |
5586 | 59.2k | } |
5587 | | |
5588 | 3.07M | { |
5589 | 3.07M | bool runtime_resolution = zend_compile_function_name(&name_node, name_ast); |
5590 | 3.07M | if (runtime_resolution) { |
5591 | 2.79M | if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert") |
5592 | 14.9k | && !is_callable_convert) { |
5593 | 14.8k | zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL, ast->lineno, type); |
5594 | 2.78M | } else { |
5595 | 2.78M | zend_compile_ns_call(result, &name_node, args_ast, ast->lineno, type); |
5596 | 2.78M | } |
5597 | 2.79M | return; |
5598 | 2.79M | } |
5599 | 3.07M | } |
5600 | | |
5601 | 271k | { |
5602 | 271k | const zval *name = &name_node.u.constant; |
5603 | 271k | zend_string *lcname = zend_string_tolower(Z_STR_P(name)); |
5604 | 271k | zval *fbc_zv = zend_hash_find(CG(function_table), lcname); |
5605 | 271k | const zend_function *fbc = fbc_zv ? Z_PTR_P(fbc_zv) : NULL; |
5606 | 271k | zend_op *opline; |
5607 | | |
5608 | | /* Special assert() handling should apply independently of compiler flags. */ |
5609 | 271k | if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) { |
5610 | 16.8k | zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc, ast->lineno, type); |
5611 | 16.8k | zend_string_release(lcname); |
5612 | 16.8k | zval_ptr_dtor(&name_node.u.constant); |
5613 | 16.8k | return; |
5614 | 16.8k | } |
5615 | | |
5616 | 254k | if (!fbc |
5617 | 193k | || !fbc_is_finalized(fbc) |
5618 | 193k | || zend_compile_ignore_function(fbc, CG(active_op_array)->filename)) { |
5619 | 61.5k | zend_string_release_ex(lcname, 0); |
5620 | 61.5k | zend_compile_dynamic_call(result, &name_node, args_ast, ast->lineno, type); |
5621 | 61.5k | return; |
5622 | 61.5k | } |
5623 | | |
5624 | 193k | if (!is_callable_convert && |
5625 | 190k | zend_try_compile_special_func(result, lcname, |
5626 | 190k | zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS |
5627 | 193k | ) { |
5628 | 32.1k | zend_string_release_ex(lcname, 0); |
5629 | 32.1k | zval_ptr_dtor(&name_node.u.constant); |
5630 | 32.1k | return; |
5631 | 32.1k | } |
5632 | | |
5633 | 160k | zval_ptr_dtor(&name_node.u.constant); |
5634 | 160k | ZVAL_STR(&name_node.u.constant, lcname); |
5635 | | |
5636 | 160k | opline = zend_emit_op(NULL, ZEND_INIT_FCALL, NULL, &name_node); |
5637 | 160k | opline->result.num = zend_alloc_cache_slot(); |
5638 | | |
5639 | | /* Store offset to function from symbol table in op2.extra. */ |
5640 | 160k | if (fbc->type == ZEND_INTERNAL_FUNCTION) { |
5641 | 129k | const Bucket *fbc_bucket = ZEND_CONTAINER_OF(fbc_zv, Bucket, val); |
5642 | 129k | Z_EXTRA_P(CT_CONSTANT(opline->op2)) = fbc_bucket - CG(function_table)->arData; |
5643 | 129k | } |
5644 | | |
5645 | 160k | zend_compile_call_common(result, args_ast, fbc, ast->lineno, type); |
5646 | 160k | } |
5647 | 160k | } |
5648 | | /* }}} */ |
5649 | | |
5650 | | static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5651 | 126k | { |
5652 | 126k | zend_ast *obj_ast = ast->child[0]; |
5653 | 126k | zend_ast *method_ast = ast->child[1]; |
5654 | 126k | zend_ast *args_ast = ast->child[2]; |
5655 | | |
5656 | 126k | znode obj_node, method_node; |
5657 | 126k | zend_op *opline; |
5658 | 126k | const zend_function *fbc = NULL; |
5659 | 126k | bool nullsafe = ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL; |
5660 | 126k | uint32_t short_circuiting_checkpoint = zend_short_circuiting_checkpoint(); |
5661 | | |
5662 | 126k | if (is_this_fetch(obj_ast)) { |
5663 | 2.98k | if (this_guaranteed_exists()) { |
5664 | 1.99k | obj_node.op_type = IS_UNUSED; |
5665 | 1.99k | } else { |
5666 | 991 | zend_emit_op_tmp(&obj_node, ZEND_FETCH_THIS, NULL, NULL); |
5667 | 991 | } |
5668 | 2.98k | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
5669 | | |
5670 | | /* We will throw if $this doesn't exist, so there's no need to emit a JMP_NULL |
5671 | | * check for a nullsafe access. */ |
5672 | 123k | } else { |
5673 | 123k | zend_short_circuiting_mark_inner(obj_ast); |
5674 | 123k | zend_compile_expr(&obj_node, obj_ast); |
5675 | 123k | if (nullsafe) { |
5676 | 3.64k | zend_emit_jmp_null(&obj_node, type); |
5677 | 3.64k | } |
5678 | 123k | } |
5679 | | |
5680 | 126k | zend_compile_expr(&method_node, method_ast); |
5681 | 126k | opline = zend_emit_op(NULL, ZEND_INIT_METHOD_CALL, &obj_node, NULL); |
5682 | | |
5683 | 126k | if (method_node.op_type == IS_CONST) { |
5684 | 125k | if (Z_TYPE(method_node.u.constant) != IS_STRING) { |
5685 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5686 | 8 | } |
5687 | | |
5688 | 124k | opline->op2_type = IS_CONST; |
5689 | 124k | opline->op2.constant = zend_add_func_name_literal( |
5690 | 124k | Z_STR(method_node.u.constant)); |
5691 | 124k | opline->result.num = zend_alloc_cache_slots(2); |
5692 | 124k | } else { |
5693 | 1.45k | SET_NODE(opline->op2, &method_node); |
5694 | 1.45k | } |
5695 | | |
5696 | | /* Check if this calls a known method on $this */ |
5697 | 126k | if (opline->op1_type == IS_UNUSED && opline->op2_type == IS_CONST && |
5698 | 1.87k | CG(active_class_entry) && zend_is_scope_known()) { |
5699 | 1.37k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5700 | 1.37k | fbc = zend_hash_find_ptr(&CG(active_class_entry)->function_table, lcname); |
5701 | | |
5702 | | /* We only know the exact method that is being called if it is either private or final. |
5703 | | * Otherwise an overriding method in a child class may be called. */ |
5704 | 1.37k | if (fbc && !(fbc->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_FINAL))) { |
5705 | 440 | fbc = NULL; |
5706 | 440 | } |
5707 | 1.37k | } |
5708 | | |
5709 | 126k | if (zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type)) { |
5710 | 480 | if (short_circuiting_checkpoint != zend_short_circuiting_checkpoint()) { |
5711 | 11 | zend_error_noreturn(E_COMPILE_ERROR, |
5712 | 11 | "Cannot combine nullsafe operator with Closure creation"); |
5713 | 11 | } |
5714 | 480 | } |
5715 | 126k | } |
5716 | | /* }}} */ |
5717 | | |
5718 | | static bool is_func_accessible(const zend_function *fbc) |
5719 | 44.3k | { |
5720 | 44.3k | if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) { |
5721 | 44.2k | return true; |
5722 | 44.2k | } |
5723 | | |
5724 | 166 | if (!(fbc->common.fn_flags & ZEND_ACC_PRIVATE) |
5725 | 63 | && (fbc->common.scope->ce_flags & ZEND_ACC_LINKED) |
5726 | 63 | && (!CG(active_class_entry) || (CG(active_class_entry)->ce_flags & ZEND_ACC_LINKED)) |
5727 | 19 | && zend_check_protected(zend_get_function_root_class(fbc), CG(active_class_entry))) { |
5728 | 0 | return true; |
5729 | 0 | } |
5730 | | |
5731 | 166 | return false; |
5732 | 166 | } |
5733 | | |
5734 | | static const zend_function *zend_get_compatible_func_or_null(const zend_class_entry *ce, zend_string *lcname) /* {{{ */ |
5735 | 5.76k | { |
5736 | 5.76k | const zend_function *fbc = zend_hash_find_ptr(&ce->function_table, lcname); |
5737 | | |
5738 | 5.76k | if (!fbc || is_func_accessible(fbc)) { |
5739 | 5.63k | return fbc; |
5740 | 5.63k | } |
5741 | | |
5742 | 130 | return NULL; |
5743 | 5.76k | } |
5744 | | /* }}} */ |
5745 | | |
5746 | | static void zend_compile_static_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */ |
5747 | 17.7k | { |
5748 | 17.7k | zend_ast *class_ast = ast->child[0]; |
5749 | 17.7k | zend_ast *method_ast = ast->child[1]; |
5750 | 17.7k | zend_ast *args_ast = ast->child[2]; |
5751 | | |
5752 | 17.7k | znode class_node, method_node; |
5753 | 17.7k | zend_op *opline; |
5754 | 17.7k | const zend_function *fbc = NULL; |
5755 | | |
5756 | 17.7k | if (zend_compile_parent_property_hook_call(result, ast, type)) { |
5757 | 761 | return; |
5758 | 761 | } |
5759 | | |
5760 | 17.0k | zend_short_circuiting_mark_inner(class_ast); |
5761 | 17.0k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5762 | | |
5763 | 17.0k | zend_compile_expr(&method_node, method_ast); |
5764 | | |
5765 | 17.0k | if (method_node.op_type == IS_CONST) { |
5766 | 15.0k | zval *name = &method_node.u.constant; |
5767 | 15.0k | if (Z_TYPE_P(name) != IS_STRING) { |
5768 | 3 | zend_error_noreturn(E_COMPILE_ERROR, "Method name must be a string"); |
5769 | 3 | } |
5770 | 15.0k | if (zend_is_constructor(Z_STR_P(name))) { |
5771 | 292 | zval_ptr_dtor(name); |
5772 | 292 | method_node.op_type = IS_UNUSED; |
5773 | 292 | } |
5774 | 15.0k | } |
5775 | | |
5776 | 17.0k | opline = get_next_op(); |
5777 | 17.0k | opline->opcode = ZEND_INIT_STATIC_METHOD_CALL; |
5778 | | |
5779 | 17.0k | zend_set_class_name_op1(opline, &class_node); |
5780 | | |
5781 | 17.0k | if (method_node.op_type == IS_CONST) { |
5782 | 14.7k | opline->op2_type = IS_CONST; |
5783 | 14.7k | opline->op2.constant = zend_add_func_name_literal( |
5784 | 14.7k | Z_STR(method_node.u.constant)); |
5785 | 14.7k | opline->result.num = zend_alloc_cache_slots(2); |
5786 | 14.7k | } else { |
5787 | 2.24k | if (opline->op1_type == IS_CONST) { |
5788 | 210 | opline->result.num = zend_alloc_cache_slot(); |
5789 | 210 | } |
5790 | 2.24k | SET_NODE(opline->op2, &method_node); |
5791 | 2.24k | } |
5792 | | |
5793 | | /* Check if we already know which method we're calling */ |
5794 | 17.0k | if (opline->op2_type == IS_CONST) { |
5795 | 14.7k | zend_class_entry *ce = NULL; |
5796 | 14.7k | if (opline->op1_type == IS_CONST) { |
5797 | 9.36k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5798 | 9.36k | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5799 | 9.36k | if (ce) { |
5800 | 5.20k | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5801 | 0 | ce = NULL; |
5802 | 0 | } |
5803 | 5.20k | } else if (CG(active_class_entry) |
5804 | 783 | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5805 | 139 | ce = CG(active_class_entry); |
5806 | 139 | } |
5807 | 9.36k | } else if (opline->op1_type == IS_UNUSED |
5808 | 1.67k | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5809 | 804 | && zend_is_scope_known()) { |
5810 | 415 | ce = CG(active_class_entry); |
5811 | 415 | } |
5812 | 14.7k | if (ce) { |
5813 | 5.76k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op2) + 1); |
5814 | 5.76k | fbc = zend_get_compatible_func_or_null(ce, lcname); |
5815 | 5.76k | } |
5816 | 14.7k | } |
5817 | | |
5818 | 17.0k | zend_compile_call_common(result, args_ast, fbc, zend_ast_get_lineno(method_ast), type); |
5819 | 17.0k | } |
5820 | | /* }}} */ |
5821 | | |
5822 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel); |
5823 | | |
5824 | | static void zend_compile_new(znode *result, zend_ast *ast) /* {{{ */ |
5825 | 115k | { |
5826 | 115k | zend_ast *class_ast = ast->child[0]; |
5827 | 115k | zend_ast *args_ast = ast->child[1]; |
5828 | | |
5829 | 115k | znode class_node, ctor_result; |
5830 | 115k | zend_op *opline; |
5831 | | |
5832 | 115k | if (class_ast->kind == ZEND_AST_CLASS) { |
5833 | | /* anon class declaration */ |
5834 | 30.4k | zend_compile_class_decl(&class_node, class_ast, false); |
5835 | 84.6k | } else { |
5836 | 84.6k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
5837 | 84.6k | } |
5838 | | |
5839 | 115k | opline = zend_emit_op_tmp(result, ZEND_NEW, NULL, NULL); |
5840 | | |
5841 | 115k | zend_set_class_name_op1(opline, &class_node); |
5842 | | |
5843 | 115k | if (opline->op1_type == IS_CONST) { |
5844 | 83.2k | opline->op2.num = zend_alloc_cache_slot(); |
5845 | 83.2k | } |
5846 | | |
5847 | 115k | zend_class_entry *ce = NULL; |
5848 | 115k | if (opline->op1_type == IS_CONST) { |
5849 | 83.2k | zend_string *lcname = Z_STR_P(CT_CONSTANT(opline->op1) + 1); |
5850 | 83.2k | ce = zend_hash_find_ptr(CG(class_table), lcname); |
5851 | 83.2k | if (ce) { |
5852 | 57.8k | if (zend_compile_ignore_class(ce, CG(active_op_array)->filename)) { |
5853 | 0 | ce = NULL; |
5854 | 0 | } |
5855 | 57.8k | } else if (CG(active_class_entry) |
5856 | 2.18k | && zend_string_equals_ci(CG(active_class_entry)->name, lcname)) { |
5857 | 557 | ce = CG(active_class_entry); |
5858 | 557 | } |
5859 | 83.2k | } else if (opline->op1_type == IS_UNUSED |
5860 | 707 | && (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF |
5861 | 442 | && zend_is_scope_known()) { |
5862 | 193 | ce = CG(active_class_entry); |
5863 | 193 | } |
5864 | | |
5865 | | |
5866 | 115k | const zend_function *fbc = NULL; |
5867 | 115k | if (ce |
5868 | 58.5k | && ce->default_object_handlers->get_constructor == zend_std_get_constructor |
5869 | 58.4k | && ce->constructor |
5870 | 39.5k | && is_func_accessible(ce->constructor)) { |
5871 | 39.4k | fbc = ce->constructor; |
5872 | 39.4k | } |
5873 | | |
5874 | 115k | zend_compile_call_common(&ctor_result, args_ast, fbc, ast->lineno, BP_VAR_R); |
5875 | 115k | zend_do_free(&ctor_result); |
5876 | 115k | } |
5877 | | /* }}} */ |
5878 | | |
5879 | | static void zend_compile_global_var(zend_ast *ast) /* {{{ */ |
5880 | 2.63k | { |
5881 | 2.63k | zend_ast *var_ast = ast->child[0]; |
5882 | 2.63k | zend_ast *name_ast = var_ast->child[0]; |
5883 | | |
5884 | 2.63k | znode name_node, result; |
5885 | | |
5886 | 2.63k | zend_compile_expr(&name_node, name_ast); |
5887 | 2.63k | if (name_node.op_type == IS_CONST) { |
5888 | 2.37k | convert_to_string(&name_node.u.constant); |
5889 | 2.37k | } |
5890 | | |
5891 | | // TODO(GLOBALS) Forbid "global $GLOBALS"? |
5892 | 2.63k | if (is_this_fetch(var_ast)) { |
5893 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as global variable"); |
5894 | 2.62k | } else if (zend_try_compile_cv(&result, var_ast, BP_VAR_R) == SUCCESS) { |
5895 | 2.00k | zend_op *opline = zend_emit_op(NULL, ZEND_BIND_GLOBAL, &result, &name_node); |
5896 | 2.00k | opline->extended_value = zend_alloc_cache_slot(); |
5897 | 2.00k | } else { |
5898 | | /* name_ast should be evaluated only. FETCH_GLOBAL_LOCK instructs FETCH_W |
5899 | | * to not free the name_node operand, so it can be reused in the following |
5900 | | * ASSIGN_REF, which then frees it. */ |
5901 | 619 | zend_op *opline = zend_emit_op(&result, ZEND_FETCH_W, &name_node, NULL); |
5902 | 619 | opline->extended_value = ZEND_FETCH_GLOBAL_LOCK; |
5903 | | |
5904 | 619 | if (name_node.op_type == IS_CONST) { |
5905 | 361 | zend_string_addref(Z_STR(name_node.u.constant)); |
5906 | 361 | } |
5907 | | |
5908 | 619 | zend_emit_assign_ref_znode( |
5909 | 619 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_znode(&name_node)), |
5910 | 619 | &result |
5911 | 619 | ); |
5912 | 619 | } |
5913 | 2.63k | } |
5914 | | /* }}} */ |
5915 | | |
5916 | | static void zend_compile_static_var_common(zend_string *var_name, zval *value, uint32_t mode) /* {{{ */ |
5917 | 59.3k | { |
5918 | 59.3k | zend_op *opline; |
5919 | 59.3k | if (!CG(active_op_array)->static_variables) { |
5920 | 0 | if (CG(active_op_array)->scope) { |
5921 | 0 | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5922 | 0 | } |
5923 | 0 | CG(active_op_array)->static_variables = zend_new_array(8); |
5924 | 0 | } |
5925 | | |
5926 | 59.3k | value = zend_hash_update(CG(active_op_array)->static_variables, var_name, value); |
5927 | | |
5928 | 59.3k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5929 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5930 | 0 | } |
5931 | | |
5932 | 59.3k | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, NULL); |
5933 | 59.3k | opline->op1_type = IS_CV; |
5934 | 59.3k | opline->op1.var = lookup_cv(var_name); |
5935 | | |
5936 | 59.3k | ZEND_STATIC_ASSERT(sizeof(Bucket) % 8 == 0, "Bucket size not compatible with storing flags in lower three bits"); |
5937 | 59.3k | opline->extended_value = (uint32_t)((char*)value - (char*)CG(active_op_array)->static_variables->arData) | mode; |
5938 | 59.3k | } |
5939 | | /* }}} */ |
5940 | | |
5941 | | static void zend_compile_static_var(zend_ast *ast) /* {{{ */ |
5942 | 3.91k | { |
5943 | 3.91k | zend_ast *var_ast = ast->child[0]; |
5944 | 3.91k | zend_string *var_name = zend_ast_get_str(var_ast); |
5945 | | |
5946 | 3.91k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
5947 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as static variable"); |
5948 | 5 | } |
5949 | | |
5950 | 3.90k | if (!CG(active_op_array)->static_variables) { |
5951 | 3.41k | if (CG(active_op_array)->scope) { |
5952 | 1.39k | CG(active_op_array)->scope->ce_flags |= ZEND_HAS_STATIC_IN_METHODS; |
5953 | 1.39k | } |
5954 | 3.41k | CG(active_op_array)->static_variables = zend_new_array(8); |
5955 | 3.41k | } |
5956 | | |
5957 | 3.90k | if (zend_hash_exists(CG(active_op_array)->static_variables, var_name)) { |
5958 | 15 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, "Duplicate declaration of static variable $%S", var_name); |
5959 | 15 | } |
5960 | | |
5961 | 3.89k | zend_eval_const_expr(&ast->child[1]); |
5962 | 3.89k | zend_ast *value_ast = ast->child[1]; |
5963 | | |
5964 | 3.89k | if (!value_ast || value_ast->kind == ZEND_AST_ZVAL) { |
5965 | 2.81k | zval *value_zv = value_ast |
5966 | 2.81k | ? zend_ast_get_zval(value_ast) |
5967 | 2.81k | : &EG(uninitialized_zval); |
5968 | 2.81k | Z_TRY_ADDREF_P(value_zv); |
5969 | 2.81k | zend_compile_static_var_common(var_name, value_zv, ZEND_BIND_REF); |
5970 | 2.81k | } else { |
5971 | 1.08k | zend_op *opline; |
5972 | | |
5973 | 1.08k | zval *placeholder_ptr = zend_hash_update(CG(active_op_array)->static_variables, var_name, &EG(uninitialized_zval)); |
5974 | 1.08k | uint32_t placeholder_offset = (uint32_t)((char*)placeholder_ptr - (char*)CG(active_op_array)->static_variables->arData); |
5975 | | |
5976 | 1.08k | uint32_t static_def_jmp_opnum = get_next_op_number(); |
5977 | 1.08k | opline = zend_emit_op(NULL, ZEND_BIND_INIT_STATIC_OR_JMP, NULL, NULL); |
5978 | 1.08k | opline->op1_type = IS_CV; |
5979 | 1.08k | opline->op1.var = lookup_cv(var_name); |
5980 | 1.08k | opline->extended_value = placeholder_offset; |
5981 | | |
5982 | 1.08k | znode expr; |
5983 | 1.08k | zend_compile_expr(&expr, value_ast); |
5984 | | |
5985 | 1.08k | opline = zend_emit_op(NULL, ZEND_BIND_STATIC, NULL, &expr); |
5986 | 1.08k | opline->op1_type = IS_CV; |
5987 | 1.08k | opline->op1.var = lookup_cv(var_name); |
5988 | 1.08k | opline->extended_value = placeholder_offset | ZEND_BIND_REF; |
5989 | | |
5990 | 1.08k | zend_update_jump_target_to_next(static_def_jmp_opnum); |
5991 | 1.08k | } |
5992 | 3.89k | } |
5993 | | /* }}} */ |
5994 | | |
5995 | | static void zend_compile_unset(const zend_ast *ast) /* {{{ */ |
5996 | 8.97k | { |
5997 | 8.97k | zend_ast *var_ast = ast->child[0]; |
5998 | 8.97k | znode var_node; |
5999 | 8.97k | zend_op *opline; |
6000 | | |
6001 | 8.97k | zend_ensure_writable_variable(var_ast); |
6002 | | |
6003 | 8.97k | if (is_global_var_fetch(var_ast)) { |
6004 | 223 | if (!var_ast->child[1]) { |
6005 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for unsetting"); |
6006 | 5 | } |
6007 | | |
6008 | 218 | zend_compile_expr(&var_node, var_ast->child[1]); |
6009 | 218 | if (var_node.op_type == IS_CONST) { |
6010 | 198 | convert_to_string(&var_node.u.constant); |
6011 | 198 | } |
6012 | | |
6013 | 218 | opline = zend_emit_op(NULL, ZEND_UNSET_VAR, &var_node, NULL); |
6014 | 218 | opline->extended_value = ZEND_FETCH_GLOBAL; |
6015 | 218 | return; |
6016 | 223 | } |
6017 | | |
6018 | 8.75k | switch (var_ast->kind) { |
6019 | 4.27k | case ZEND_AST_VAR: |
6020 | 4.27k | if (is_this_fetch(var_ast)) { |
6021 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot unset $this"); |
6022 | 4.26k | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_UNSET) == SUCCESS) { |
6023 | 4.01k | opline = zend_emit_op(NULL, ZEND_UNSET_CV, &var_node, NULL); |
6024 | 4.01k | } else { |
6025 | 254 | opline = zend_compile_simple_var_no_cv(NULL, var_ast, BP_VAR_UNSET, false); |
6026 | 254 | opline->opcode = ZEND_UNSET_VAR; |
6027 | 254 | } |
6028 | 4.26k | return; |
6029 | 4.26k | case ZEND_AST_DIM: |
6030 | 2.86k | opline = zend_compile_dim(NULL, var_ast, BP_VAR_UNSET, /* by_ref */ false); |
6031 | 2.86k | opline->opcode = ZEND_UNSET_DIM; |
6032 | 2.86k | return; |
6033 | 1.49k | case ZEND_AST_PROP: |
6034 | 1.49k | case ZEND_AST_NULLSAFE_PROP: |
6035 | 1.49k | opline = zend_compile_prop(NULL, var_ast, BP_VAR_UNSET, false); |
6036 | 1.49k | opline->opcode = ZEND_UNSET_OBJ; |
6037 | 1.49k | return; |
6038 | 115 | case ZEND_AST_STATIC_PROP: |
6039 | 115 | opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_UNSET, false, false); |
6040 | 115 | opline->opcode = ZEND_UNSET_STATIC_PROP; |
6041 | 115 | return; |
6042 | 0 | default: ZEND_UNREACHABLE(); |
6043 | 8.75k | } |
6044 | 8.75k | } |
6045 | | /* }}} */ |
6046 | | |
6047 | | static bool zend_handle_loops_and_finally_ex(zend_long depth, znode *return_value) /* {{{ */ |
6048 | 283k | { |
6049 | 283k | const zend_loop_var *base; |
6050 | 283k | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
6051 | | |
6052 | 283k | if (!loop_var) { |
6053 | 2.17k | return true; |
6054 | 2.17k | } |
6055 | 281k | base = zend_stack_base(&CG(loop_var_stack)); |
6056 | 290k | for (; loop_var >= base; loop_var--) { |
6057 | 288k | if (loop_var->opcode == ZEND_FAST_CALL) { |
6058 | 2.99k | zend_op *opline = get_next_op(); |
6059 | | |
6060 | 2.99k | opline->opcode = ZEND_FAST_CALL; |
6061 | 2.99k | opline->result_type = IS_TMP_VAR; |
6062 | 2.99k | opline->result.var = loop_var->var_num; |
6063 | 2.99k | if (return_value) { |
6064 | 1.09k | SET_NODE(opline->op2, return_value); |
6065 | 1.09k | } |
6066 | 2.99k | opline->op1.num = loop_var->try_catch_offset; |
6067 | 285k | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
6068 | 1.82k | zend_op *opline = get_next_op(); |
6069 | 1.82k | opline->opcode = ZEND_DISCARD_EXCEPTION; |
6070 | 1.82k | opline->op1_type = IS_TMP_VAR; |
6071 | 1.82k | opline->op1.var = loop_var->var_num; |
6072 | 283k | } else if (loop_var->opcode == ZEND_RETURN) { |
6073 | | /* Stack separator */ |
6074 | 276k | break; |
6075 | 276k | } else if (depth <= 1) { |
6076 | 2.60k | return 1; |
6077 | 4.52k | } else if (loop_var->opcode == ZEND_NOP) { |
6078 | | /* Loop doesn't have freeable variable */ |
6079 | 2.35k | depth--; |
6080 | 2.35k | } else { |
6081 | 2.17k | zend_op *opline; |
6082 | | |
6083 | 2.17k | ZEND_ASSERT(loop_var->var_type & (IS_VAR|IS_TMP_VAR)); |
6084 | 2.17k | opline = get_next_op(); |
6085 | 2.17k | opline->opcode = loop_var->opcode; |
6086 | 2.17k | opline->op1_type = loop_var->var_type; |
6087 | 2.17k | opline->op1.var = loop_var->var_num; |
6088 | 2.17k | opline->extended_value = ZEND_FREE_ON_RETURN; |
6089 | 2.17k | depth--; |
6090 | 2.17k | } |
6091 | 288k | } |
6092 | 278k | return (depth == 0); |
6093 | 281k | } |
6094 | | /* }}} */ |
6095 | | |
6096 | | static bool zend_handle_loops_and_finally(znode *return_value) /* {{{ */ |
6097 | 280k | { |
6098 | 280k | return zend_handle_loops_and_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1, return_value); |
6099 | 280k | } |
6100 | | /* }}} */ |
6101 | | |
6102 | | static bool zend_has_finally_ex(zend_long depth) /* {{{ */ |
6103 | 1.44k | { |
6104 | 1.44k | const zend_loop_var *base; |
6105 | 1.44k | zend_loop_var *loop_var = zend_stack_top(&CG(loop_var_stack)); |
6106 | | |
6107 | 1.44k | if (!loop_var) { |
6108 | 228 | return false; |
6109 | 228 | } |
6110 | 1.22k | base = zend_stack_base(&CG(loop_var_stack)); |
6111 | 2.52k | for (; loop_var >= base; loop_var--) { |
6112 | 2.46k | if (loop_var->opcode == ZEND_FAST_CALL) { |
6113 | 336 | return 1; |
6114 | 2.12k | } else if (loop_var->opcode == ZEND_DISCARD_EXCEPTION) { |
6115 | 1.07k | } else if (loop_var->opcode == ZEND_RETURN) { |
6116 | | /* Stack separator */ |
6117 | 819 | return 0; |
6118 | 819 | } else if (depth <= 1) { |
6119 | 0 | return 0; |
6120 | 255 | } else { |
6121 | 255 | depth--; |
6122 | 255 | } |
6123 | 2.46k | } |
6124 | 65 | return false; |
6125 | 1.22k | } |
6126 | | /* }}} */ |
6127 | | |
6128 | | static bool zend_has_finally(void) /* {{{ */ |
6129 | 1.44k | { |
6130 | 1.44k | return zend_has_finally_ex(zend_stack_count(&CG(loop_var_stack)) + 1); |
6131 | 1.44k | } |
6132 | | /* }}} */ |
6133 | | |
6134 | | static void zend_compile_return(const zend_ast *ast) /* {{{ */ |
6135 | 278k | { |
6136 | 278k | zend_ast *expr_ast = ast->child[0]; |
6137 | 278k | bool is_generator = (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) != 0; |
6138 | 278k | bool by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
6139 | | |
6140 | 278k | znode expr_node; |
6141 | 278k | zend_op *opline; |
6142 | | |
6143 | 278k | if (is_generator) { |
6144 | | /* For generators the by-ref flag refers to yields, not returns */ |
6145 | 127k | by_ref = false; |
6146 | 127k | } |
6147 | | |
6148 | 278k | if (!expr_ast) { |
6149 | 1.75k | expr_node.op_type = IS_CONST; |
6150 | 1.75k | ZVAL_NULL(&expr_node.u.constant); |
6151 | 276k | } else if (by_ref && zend_is_variable_or_call(expr_ast)) { |
6152 | 96.7k | zend_assert_not_short_circuited(expr_ast); |
6153 | 96.7k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
6154 | 180k | } else { |
6155 | 180k | zend_compile_expr(&expr_node, expr_ast); |
6156 | 180k | } |
6157 | | |
6158 | 278k | if (expr_ast) { |
6159 | 276k | if (CG(active_class_entry) != NULL) { |
6160 | 12.7k | if (zend_is_constructor(CG(active_op_array)->function_name)) { |
6161 | 31 | zend_error(E_DEPRECATED, "Returning a value from a constructor is deprecated"); |
6162 | 12.6k | } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { |
6163 | 11 | zend_error(E_DEPRECATED, "Returning a value from a destructor is deprecated"); |
6164 | 11 | } |
6165 | 12.7k | } |
6166 | 276k | } |
6167 | | |
6168 | 278k | if (CG(context).in_finally) { |
6169 | 1.58k | zend_error(E_DEPRECATED, "Returning from a finally block is deprecated"); |
6170 | 1.58k | } |
6171 | | |
6172 | 278k | if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) |
6173 | 2.36k | && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR)) |
6174 | 1.44k | && zend_has_finally()) { |
6175 | | /* Copy return value into temporary VAR to avoid modification in finally code */ |
6176 | 336 | if (by_ref) { |
6177 | 143 | zend_emit_op(&expr_node, ZEND_MAKE_REF, &expr_node, NULL); |
6178 | 193 | } else { |
6179 | 193 | zend_emit_op_tmp(&expr_node, ZEND_QM_ASSIGN, &expr_node, NULL); |
6180 | 193 | } |
6181 | 336 | } |
6182 | | |
6183 | | /* Generator return types are handled separately */ |
6184 | 278k | if (!is_generator && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6185 | 9.15k | zend_emit_return_type_check( |
6186 | 9.15k | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6187 | 9.15k | } |
6188 | | |
6189 | 278k | uint32_t opnum_before_finally = get_next_op_number(); |
6190 | | |
6191 | 278k | zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL); |
6192 | | |
6193 | | /* Content of reference might have changed in finally, repeat type check. */ |
6194 | 278k | if (by_ref |
6195 | | /* Check if any opcodes were emitted since the last return type check. */ |
6196 | 116k | && opnum_before_finally != get_next_op_number() |
6197 | 924 | && !is_generator |
6198 | 924 | && (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) { |
6199 | 80 | zend_emit_return_type_check( |
6200 | 80 | expr_ast ? &expr_node : NULL, CG(active_op_array)->arg_info - 1, false); |
6201 | 80 | } |
6202 | | |
6203 | 278k | opline = zend_emit_op(NULL, by_ref ? ZEND_RETURN_BY_REF : ZEND_RETURN, |
6204 | 278k | &expr_node, NULL); |
6205 | | |
6206 | 278k | if (by_ref && expr_ast) { |
6207 | 115k | if (zend_is_call(expr_ast)) { |
6208 | 94.6k | opline->extended_value = ZEND_RETURNS_FUNCTION; |
6209 | 94.6k | } else if (!zend_is_variable(expr_ast) || zend_ast_is_short_circuited(expr_ast)) { |
6210 | 18.6k | opline->extended_value = ZEND_RETURNS_VALUE; |
6211 | 18.6k | } |
6212 | 115k | } |
6213 | 278k | } |
6214 | | /* }}} */ |
6215 | | |
6216 | | static void zend_compile_void_cast(znode *result, const zend_ast *ast) |
6217 | 1.24k | { |
6218 | 1.24k | zend_ast *expr_ast = ast->child[0]; |
6219 | 1.24k | znode expr_node; |
6220 | 1.24k | zend_op *opline; |
6221 | | |
6222 | 1.24k | zend_compile_expr(&expr_node, expr_ast); |
6223 | | |
6224 | 1.24k | switch (expr_node.op_type) { |
6225 | 281 | case IS_TMP_VAR: |
6226 | 281 | case IS_VAR: |
6227 | 281 | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6228 | 281 | opline->extended_value = ZEND_FREE_VOID_CAST; |
6229 | 281 | break; |
6230 | 907 | case IS_CONST: |
6231 | 907 | zend_do_free(&expr_node); |
6232 | 907 | break; |
6233 | 1.24k | } |
6234 | 1.24k | } |
6235 | | |
6236 | | static void zend_compile_echo(const zend_ast *ast) /* {{{ */ |
6237 | 1.88M | { |
6238 | 1.88M | zend_op *opline; |
6239 | 1.88M | zend_ast *expr_ast = ast->child[0]; |
6240 | | |
6241 | 1.88M | znode expr_node; |
6242 | 1.88M | zend_compile_expr(&expr_node, expr_ast); |
6243 | | |
6244 | 1.88M | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
6245 | 1.88M | opline->extended_value = 0; |
6246 | 1.88M | } |
6247 | | /* }}} */ |
6248 | | |
6249 | | static void zend_compile_throw(znode *result, const zend_ast *ast) /* {{{ */ |
6250 | 3.78k | { |
6251 | 3.78k | zend_ast *expr_ast = ast->child[0]; |
6252 | | |
6253 | 3.78k | znode expr_node; |
6254 | 3.78k | zend_compile_expr(&expr_node, expr_ast); |
6255 | | |
6256 | 3.78k | zend_op *opline = zend_emit_op(NULL, ZEND_THROW, &expr_node, NULL); |
6257 | 3.78k | if (result) { |
6258 | | /* Mark this as an "expression throw" for opcache. */ |
6259 | 666 | opline->extended_value = ZEND_THROW_IS_EXPR; |
6260 | 666 | result->op_type = IS_CONST; |
6261 | 666 | ZVAL_TRUE(&result->u.constant); |
6262 | 666 | } |
6263 | 3.78k | } |
6264 | | /* }}} */ |
6265 | | |
6266 | | static void zend_compile_break_continue(const zend_ast *ast) /* {{{ */ |
6267 | 2.72k | { |
6268 | 2.72k | zend_ast *depth_ast = ast->child[0]; |
6269 | | |
6270 | 2.72k | zend_op *opline; |
6271 | 2.72k | zend_long depth; |
6272 | | |
6273 | 2.72k | ZEND_ASSERT(ast->kind == ZEND_AST_BREAK || ast->kind == ZEND_AST_CONTINUE); |
6274 | | |
6275 | 2.72k | if (depth_ast) { |
6276 | 657 | const zval *depth_zv; |
6277 | 657 | if (depth_ast->kind != ZEND_AST_ZVAL) { |
6278 | 17 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator with non-integer operand " |
6279 | 17 | "is no longer supported", ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6280 | 17 | } |
6281 | | |
6282 | 640 | depth_zv = zend_ast_get_zval(depth_ast); |
6283 | 640 | if (Z_TYPE_P(depth_zv) != IS_LONG || Z_LVAL_P(depth_zv) < 1) { |
6284 | 13 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' operator accepts only positive integers", |
6285 | 13 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6286 | 13 | } |
6287 | | |
6288 | 627 | depth = Z_LVAL_P(depth_zv); |
6289 | 2.07k | } else { |
6290 | 2.07k | depth = 1; |
6291 | 2.07k | } |
6292 | | |
6293 | 2.69k | if (CG(context).current_brk_cont == -1) { |
6294 | 26 | zend_error_noreturn(E_COMPILE_ERROR, "'%s' not in the 'loop' or 'switch' context", |
6295 | 26 | ast->kind == ZEND_AST_BREAK ? "break" : "continue"); |
6296 | 2.67k | } else { |
6297 | 2.67k | if (!zend_handle_loops_and_finally_ex(depth, NULL)) { |
6298 | 71 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot '%s' " ZEND_LONG_FMT " level%s", |
6299 | 71 | ast->kind == ZEND_AST_BREAK ? "break" : "continue", |
6300 | 71 | depth, depth == 1 ? "" : "s"); |
6301 | 71 | } |
6302 | 2.67k | } |
6303 | | |
6304 | 2.60k | if (ast->kind == ZEND_AST_CONTINUE) { |
6305 | 1.20k | int d, cur = CG(context).current_brk_cont; |
6306 | 1.44k | for (d = depth - 1; d > 0; d--) { |
6307 | 243 | cur = CG(context).brk_cont_array[cur].parent; |
6308 | 243 | ZEND_ASSERT(cur != -1); |
6309 | 243 | } |
6310 | | |
6311 | 1.20k | if (CG(context).brk_cont_array[cur].is_switch) { |
6312 | 723 | if (depth == 1) { |
6313 | 564 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6314 | 224 | zend_error(E_WARNING, |
6315 | 224 | "\"continue\" targeting switch is equivalent to \"break\""); |
6316 | 340 | } else { |
6317 | 340 | zend_error(E_WARNING, |
6318 | 340 | "\"continue\" targeting switch is equivalent to \"break\". " \ |
6319 | 340 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6320 | 340 | depth + 1); |
6321 | 340 | } |
6322 | 564 | } else { |
6323 | 159 | if (CG(context).brk_cont_array[cur].parent == -1) { |
6324 | 130 | zend_error(E_WARNING, |
6325 | 130 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"", |
6326 | 130 | depth, depth); |
6327 | 130 | } else { |
6328 | 29 | zend_error(E_WARNING, |
6329 | 29 | "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \ |
6330 | 29 | "Did you mean to use \"continue " ZEND_LONG_FMT "\"?", |
6331 | 29 | depth, depth, depth + 1); |
6332 | 29 | } |
6333 | 159 | } |
6334 | 723 | } |
6335 | 1.20k | } |
6336 | | |
6337 | 2.60k | opline = zend_emit_op(NULL, ast->kind == ZEND_AST_BREAK ? ZEND_BRK : ZEND_CONT, NULL, NULL); |
6338 | 2.60k | opline->op1.num = CG(context).current_brk_cont; |
6339 | 2.60k | opline->op2.num = depth; |
6340 | 2.60k | } |
6341 | | /* }}} */ |
6342 | | |
6343 | | void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline) /* {{{ */ |
6344 | 1.85k | { |
6345 | 1.85k | zend_label *dest; |
6346 | 1.85k | int remove_oplines = opline->op1.num; |
6347 | 1.85k | zval *label; |
6348 | 1.85k | uint32_t opnum = opline - op_array->opcodes; |
6349 | | |
6350 | 1.85k | label = CT_CONSTANT_EX(op_array, opline->op2.constant); |
6351 | 1.85k | if (CG(context).labels == NULL || |
6352 | 1.83k | (dest = zend_hash_find_ptr(CG(context).labels, Z_STR_P(label))) == NULL |
6353 | 1.85k | ) { |
6354 | 52 | CG(in_compilation) = 1; |
6355 | 52 | CG(active_op_array) = op_array; |
6356 | 52 | CG(zend_lineno) = opline->lineno; |
6357 | 52 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' to undefined label '%s'", Z_STRVAL_P(label)); |
6358 | 52 | } |
6359 | | |
6360 | 1.80k | zval_ptr_dtor_str(label); |
6361 | 1.80k | ZVAL_NULL(label); |
6362 | | |
6363 | 1.80k | uint32_t current = opline->extended_value; |
6364 | 4.05k | for (; current != dest->brk_cont; current = CG(context).brk_cont_array[current].parent) { |
6365 | 2.25k | if (current == -1) { |
6366 | 12 | CG(in_compilation) = 1; |
6367 | 12 | CG(active_op_array) = op_array; |
6368 | 12 | CG(zend_lineno) = opline->lineno; |
6369 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "'goto' into loop or switch statement is disallowed"); |
6370 | 12 | } |
6371 | 2.24k | if (CG(context).brk_cont_array[current].start >= 0) { |
6372 | 1.34k | remove_oplines--; |
6373 | 1.34k | } |
6374 | 2.24k | } |
6375 | | |
6376 | 3.88k | for (current = 0; current < op_array->last_try_catch; ++current) { |
6377 | 2.63k | const zend_try_catch_element *elem = &op_array->try_catch_array[current]; |
6378 | 2.63k | if (elem->try_op > opnum) { |
6379 | 537 | break; |
6380 | 537 | } |
6381 | 2.09k | if (elem->finally_op && opnum < elem->finally_op - 1 |
6382 | 1.37k | && (dest->opline_num > elem->finally_end || dest->opline_num < elem->try_op) |
6383 | 2.09k | ) { |
6384 | 720 | remove_oplines--; |
6385 | 720 | } |
6386 | 2.09k | } |
6387 | | |
6388 | 1.79k | opline->opcode = ZEND_JMP; |
6389 | 1.79k | SET_UNUSED(opline->op1); |
6390 | 1.79k | SET_UNUSED(opline->op2); |
6391 | 1.79k | SET_UNUSED(opline->result); |
6392 | 1.79k | opline->op1.opline_num = dest->opline_num; |
6393 | 1.79k | opline->extended_value = 0; |
6394 | | |
6395 | 1.79k | ZEND_ASSERT(remove_oplines >= 0); |
6396 | 2.53k | while (remove_oplines--) { |
6397 | 737 | opline--; |
6398 | 737 | MAKE_NOP(opline); |
6399 | 737 | ZEND_VM_SET_OPCODE_HANDLER(opline); |
6400 | 737 | } |
6401 | 1.79k | } |
6402 | | /* }}} */ |
6403 | | |
6404 | | static void zend_compile_goto(const zend_ast *ast) /* {{{ */ |
6405 | 2.07k | { |
6406 | 2.07k | zend_ast *label_ast = ast->child[0]; |
6407 | 2.07k | znode label_node; |
6408 | 2.07k | zend_op *opline; |
6409 | | |
6410 | 2.07k | zend_compile_expr(&label_node, label_ast); |
6411 | | |
6412 | | /* Label resolution and unwinding adjustments happen in pass two. */ |
6413 | 2.07k | uint32_t opnum_start = get_next_op_number(); |
6414 | 2.07k | zend_handle_loops_and_finally(NULL); |
6415 | 2.07k | opline = zend_emit_op(NULL, ZEND_GOTO, NULL, &label_node); |
6416 | 2.07k | opline->op1.num = get_next_op_number() - opnum_start - 1; |
6417 | 2.07k | opline->extended_value = CG(context).current_brk_cont; |
6418 | 2.07k | } |
6419 | | /* }}} */ |
6420 | | |
6421 | | static void zend_compile_label(const zend_ast *ast) /* {{{ */ |
6422 | 3.49k | { |
6423 | 3.49k | zend_string *label = zend_ast_get_str(ast->child[0]); |
6424 | 3.49k | zend_label dest; |
6425 | | |
6426 | 3.49k | if (!CG(context).labels) { |
6427 | 2.22k | ALLOC_HASHTABLE(CG(context).labels); |
6428 | 2.22k | zend_hash_init(CG(context).labels, 8, NULL, label_ptr_dtor, 0); |
6429 | 2.22k | } |
6430 | | |
6431 | 3.49k | dest.brk_cont = CG(context).current_brk_cont; |
6432 | 3.49k | dest.opline_num = get_next_op_number(); |
6433 | | |
6434 | 3.49k | if (!zend_hash_add_mem(CG(context).labels, label, &dest, sizeof(zend_label))) { |
6435 | 42 | zend_error_noreturn(E_COMPILE_ERROR, "Label '%s' already defined", ZSTR_VAL(label)); |
6436 | 42 | } |
6437 | 3.49k | } |
6438 | | /* }}} */ |
6439 | | |
6440 | | static void zend_compile_while(const zend_ast *ast) /* {{{ */ |
6441 | 1.98k | { |
6442 | 1.98k | zend_ast *cond_ast = ast->child[0]; |
6443 | 1.98k | zend_ast *stmt_ast = ast->child[1]; |
6444 | 1.98k | znode cond_node; |
6445 | 1.98k | uint32_t opnum_start, opnum_jmp, opnum_cond; |
6446 | | |
6447 | 1.98k | opnum_jmp = zend_emit_jump(0); |
6448 | | |
6449 | 1.98k | zend_begin_loop(ZEND_NOP, NULL, false); |
6450 | | |
6451 | 1.98k | opnum_start = get_next_op_number(); |
6452 | 1.98k | zend_compile_stmt(stmt_ast); |
6453 | | |
6454 | 1.98k | opnum_cond = get_next_op_number(); |
6455 | 1.98k | zend_update_jump_target(opnum_jmp, opnum_cond); |
6456 | 1.98k | zend_compile_expr(&cond_node, cond_ast); |
6457 | | |
6458 | 1.98k | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6459 | | |
6460 | 1.98k | zend_end_loop(opnum_cond, NULL); |
6461 | 1.98k | } |
6462 | | /* }}} */ |
6463 | | |
6464 | | static void zend_compile_do_while(const zend_ast *ast) /* {{{ */ |
6465 | 1.18k | { |
6466 | 1.18k | zend_ast *stmt_ast = ast->child[0]; |
6467 | 1.18k | zend_ast *cond_ast = ast->child[1]; |
6468 | | |
6469 | 1.18k | znode cond_node; |
6470 | 1.18k | uint32_t opnum_start, opnum_cond; |
6471 | | |
6472 | 1.18k | zend_begin_loop(ZEND_NOP, NULL, false); |
6473 | | |
6474 | 1.18k | opnum_start = get_next_op_number(); |
6475 | 1.18k | zend_compile_stmt(stmt_ast); |
6476 | | |
6477 | 1.18k | opnum_cond = get_next_op_number(); |
6478 | 1.18k | zend_compile_expr(&cond_node, cond_ast); |
6479 | | |
6480 | 1.18k | zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, opnum_start); |
6481 | | |
6482 | 1.18k | zend_end_loop(opnum_cond, NULL); |
6483 | 1.18k | } |
6484 | | /* }}} */ |
6485 | | |
6486 | | static void zend_compile_for_expr_list(znode *result, zend_ast *ast) /* {{{ */ |
6487 | 43.8k | { |
6488 | 43.8k | const zend_ast_list *list; |
6489 | 43.8k | uint32_t i; |
6490 | | |
6491 | 43.8k | result->op_type = IS_CONST; |
6492 | 43.8k | ZVAL_TRUE(&result->u.constant); |
6493 | | |
6494 | 43.8k | if (!ast) { |
6495 | 7.36k | return; |
6496 | 7.36k | } |
6497 | | |
6498 | 36.5k | list = zend_ast_get_list(ast); |
6499 | 75.5k | for (i = 0; i < list->children; ++i) { |
6500 | 39.0k | zend_ast *expr_ast = list->child[i]; |
6501 | | |
6502 | 39.0k | zend_do_free(result); |
6503 | 39.0k | if (expr_ast->kind == ZEND_AST_CAST_VOID) { |
6504 | 191 | zend_compile_void_cast(NULL, expr_ast); |
6505 | 191 | result->op_type = IS_CONST; |
6506 | 191 | ZVAL_NULL(&result->u.constant); |
6507 | 38.8k | } else { |
6508 | 38.8k | zend_compile_expr(result, expr_ast); |
6509 | 38.8k | } |
6510 | 39.0k | } |
6511 | 36.5k | } |
6512 | | /* }}} */ |
6513 | | |
6514 | | static void zend_compile_for(const zend_ast *ast) /* {{{ */ |
6515 | 14.6k | { |
6516 | 14.6k | zend_ast *init_ast = ast->child[0]; |
6517 | 14.6k | zend_ast *cond_ast = ast->child[1]; |
6518 | 14.6k | zend_ast *loop_ast = ast->child[2]; |
6519 | 14.6k | zend_ast *stmt_ast = ast->child[3]; |
6520 | | |
6521 | 14.6k | znode result; |
6522 | 14.6k | uint32_t opnum_start, opnum_jmp, opnum_loop; |
6523 | | |
6524 | 14.6k | zend_compile_for_expr_list(&result, init_ast); |
6525 | 14.6k | zend_do_free(&result); |
6526 | | |
6527 | 14.6k | opnum_jmp = zend_emit_jump(0); |
6528 | | |
6529 | 14.6k | zend_begin_loop(ZEND_NOP, NULL, false); |
6530 | | |
6531 | 14.6k | opnum_start = get_next_op_number(); |
6532 | 14.6k | zend_compile_stmt(stmt_ast); |
6533 | | |
6534 | 14.6k | opnum_loop = get_next_op_number(); |
6535 | 14.6k | zend_compile_for_expr_list(&result, loop_ast); |
6536 | 14.6k | zend_do_free(&result); |
6537 | | |
6538 | 14.6k | zend_update_jump_target_to_next(opnum_jmp); |
6539 | 14.6k | zend_compile_for_expr_list(&result, cond_ast); |
6540 | 14.6k | zend_do_extended_stmt(NULL); |
6541 | | |
6542 | 14.6k | zend_emit_cond_jump(ZEND_JMPNZ, &result, opnum_start); |
6543 | | |
6544 | 14.6k | zend_end_loop(opnum_loop, NULL); |
6545 | 14.6k | } |
6546 | | /* }}} */ |
6547 | | |
6548 | | static void zend_compile_foreach(zend_ast *ast) /* {{{ */ |
6549 | 18.0k | { |
6550 | 18.0k | zend_ast *expr_ast = ast->child[0]; |
6551 | 18.0k | zend_ast *value_ast = ast->child[1]; |
6552 | 18.0k | zend_ast *key_ast = ast->child[2]; |
6553 | 18.0k | zend_ast *stmt_ast = ast->child[3]; |
6554 | 18.0k | bool by_ref = value_ast->kind == ZEND_AST_REF; |
6555 | 18.0k | bool is_variable = (zend_is_variable(expr_ast) && zend_can_write_to_variable(expr_ast)) |
6556 | 3.66k | || zend_is_call(expr_ast); |
6557 | | |
6558 | 18.0k | znode expr_node, reset_node, value_node, key_node; |
6559 | 18.0k | zend_op *opline; |
6560 | 18.0k | uint32_t opnum_reset, opnum_fetch; |
6561 | | |
6562 | 18.0k | if (key_ast) { |
6563 | 2.19k | if (key_ast->kind == ZEND_AST_REF) { |
6564 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Key element cannot be a reference"); |
6565 | 5 | } |
6566 | 2.19k | if (key_ast->kind == ZEND_AST_ARRAY) { |
6567 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use list as key element"); |
6568 | 9 | } |
6569 | 2.19k | } |
6570 | | |
6571 | 18.0k | if (by_ref) { |
6572 | 1.73k | value_ast = value_ast->child[0]; |
6573 | 1.73k | } |
6574 | | |
6575 | 18.0k | if (value_ast->kind == ZEND_AST_ARRAY && zend_propagate_list_refs(value_ast)) { |
6576 | 44 | by_ref = true; |
6577 | 44 | } |
6578 | | |
6579 | 18.0k | if (by_ref && is_variable) { |
6580 | 1.43k | zend_compile_var(&expr_node, expr_ast, BP_VAR_W, true); |
6581 | 16.5k | } else { |
6582 | 16.5k | zend_compile_expr(&expr_node, expr_ast); |
6583 | 16.5k | } |
6584 | | |
6585 | 18.0k | if (by_ref) { |
6586 | 1.77k | zend_separate_if_call_and_write(&expr_node, expr_ast, BP_VAR_W); |
6587 | 1.77k | } |
6588 | | |
6589 | 18.0k | opnum_reset = get_next_op_number(); |
6590 | 18.0k | opline = zend_emit_op(&reset_node, by_ref ? ZEND_FE_RESET_RW : ZEND_FE_RESET_R, &expr_node, NULL); |
6591 | 18.0k | if (!by_ref) { |
6592 | 16.2k | opline->result_type = IS_TMP_VAR; |
6593 | 16.2k | reset_node.op_type = IS_TMP_VAR; |
6594 | 16.2k | } |
6595 | | |
6596 | 18.0k | zend_begin_loop(ZEND_FE_FREE, &reset_node, false); |
6597 | | |
6598 | 18.0k | opnum_fetch = get_next_op_number(); |
6599 | 18.0k | opline = zend_emit_op(NULL, by_ref ? ZEND_FE_FETCH_RW : ZEND_FE_FETCH_R, &reset_node, NULL); |
6600 | | |
6601 | 18.0k | if (is_this_fetch(value_ast)) { |
6602 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
6603 | 18.0k | } else if (value_ast->kind == ZEND_AST_VAR && |
6604 | 17.4k | zend_try_compile_cv(&value_node, value_ast, BP_VAR_R) == SUCCESS) { |
6605 | 17.3k | SET_NODE(opline->op2, &value_node); |
6606 | 17.3k | } else { |
6607 | 681 | opline->op2_type = by_ref ? IS_VAR : IS_TMP_VAR; |
6608 | 681 | opline->op2.var = get_temporary_variable(); |
6609 | 681 | GET_NODE(&value_node, opline->op2); |
6610 | 681 | if (value_ast->kind == ZEND_AST_ARRAY) { |
6611 | 294 | zend_compile_list_assign(NULL, value_ast, &value_node, value_ast->attr, BP_VAR_R); |
6612 | 387 | } else if (by_ref) { |
6613 | 151 | zend_emit_assign_ref_znode(value_ast, &value_node); |
6614 | 236 | } else { |
6615 | 236 | zend_emit_assign_znode(value_ast, &value_node); |
6616 | 236 | } |
6617 | 681 | } |
6618 | | |
6619 | 18.0k | if (key_ast) { |
6620 | 2.17k | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6621 | 2.17k | zend_make_tmp_result(&key_node, opline); |
6622 | 2.17k | zend_emit_assign_znode(key_ast, &key_node); |
6623 | 2.17k | } |
6624 | | |
6625 | 18.0k | zend_compile_stmt(stmt_ast); |
6626 | | |
6627 | | /* Place JMP and FE_FREE on the line where foreach starts. It would be |
6628 | | * better to use the end line, but this information is not available |
6629 | | * currently. */ |
6630 | 18.0k | CG(zend_lineno) = ast->lineno; |
6631 | 18.0k | zend_emit_jump(opnum_fetch); |
6632 | | |
6633 | 18.0k | opline = &CG(active_op_array)->opcodes[opnum_reset]; |
6634 | 18.0k | opline->op2.opline_num = get_next_op_number(); |
6635 | | |
6636 | 18.0k | opline = &CG(active_op_array)->opcodes[opnum_fetch]; |
6637 | 18.0k | opline->extended_value = get_next_op_number(); |
6638 | | |
6639 | 18.0k | zend_end_loop(opnum_fetch, &reset_node); |
6640 | | |
6641 | 18.0k | opline = zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); |
6642 | 18.0k | } |
6643 | | /* }}} */ |
6644 | | |
6645 | | static void zend_compile_if(zend_ast *ast) /* {{{ */ |
6646 | 76.9k | { |
6647 | 76.9k | const zend_ast_list *list = zend_ast_get_list(ast); |
6648 | 76.9k | uint32_t i; |
6649 | 76.9k | uint32_t *jmp_opnums = NULL; |
6650 | | |
6651 | 76.9k | if (list->children > 1) { |
6652 | 19.9k | jmp_opnums = safe_emalloc(sizeof(uint32_t), list->children - 1, 0); |
6653 | 19.9k | } |
6654 | | |
6655 | 190k | for (i = 0; i < list->children; ++i) { |
6656 | 113k | const zend_ast *elem_ast = list->child[i]; |
6657 | 113k | zend_ast *cond_ast = elem_ast->child[0]; |
6658 | 113k | zend_ast *stmt_ast = elem_ast->child[1]; |
6659 | | |
6660 | 113k | if (cond_ast) { |
6661 | 94.7k | znode cond_node; |
6662 | 94.7k | uint32_t opnum_jmpz; |
6663 | | |
6664 | 94.7k | if (i > 0) { |
6665 | 17.7k | CG(zend_lineno) = cond_ast->lineno; |
6666 | 17.7k | zend_do_extended_stmt(NULL); |
6667 | 17.7k | } |
6668 | | |
6669 | 94.7k | zend_compile_expr(&cond_node, cond_ast); |
6670 | 94.7k | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6671 | | |
6672 | 94.7k | zend_compile_stmt(stmt_ast); |
6673 | | |
6674 | 94.7k | if (i != list->children - 1) { |
6675 | | /* Set the lineno of JMP to the position of the if keyword, as we don't want to |
6676 | | * report the last line in the if branch as covered if it hasn't actually executed. */ |
6677 | 36.3k | CG(zend_lineno) = elem_ast->lineno; |
6678 | 36.3k | jmp_opnums[i] = zend_emit_jump(0); |
6679 | 36.3k | } |
6680 | 94.7k | zend_update_jump_target_to_next(opnum_jmpz); |
6681 | 94.7k | } else { |
6682 | | /* "else" can only occur as last element. */ |
6683 | 18.6k | ZEND_ASSERT(i == list->children - 1); |
6684 | 18.6k | zend_compile_stmt(stmt_ast); |
6685 | 18.6k | } |
6686 | 113k | } |
6687 | | |
6688 | 76.9k | if (list->children > 1) { |
6689 | 56.3k | for (i = 0; i < list->children - 1; ++i) { |
6690 | 36.3k | zend_update_jump_target_to_next(jmp_opnums[i]); |
6691 | 36.3k | } |
6692 | 19.9k | efree(jmp_opnums); |
6693 | 19.9k | } |
6694 | 76.9k | } |
6695 | | /* }}} */ |
6696 | | |
6697 | 6.16k | static uint8_t determine_switch_jumptable_type(const zend_ast_list *cases) { |
6698 | 6.16k | uint32_t i; |
6699 | 6.16k | uint8_t common_type = IS_UNDEF; |
6700 | 15.4k | for (i = 0; i < cases->children; i++) { |
6701 | 12.1k | zend_ast *case_ast = cases->child[i]; |
6702 | 12.1k | zend_ast **cond_ast = &case_ast->child[0]; |
6703 | 12.1k | const zval *cond_zv; |
6704 | 12.1k | if (!case_ast->child[0]) { |
6705 | | /* Skip default clause */ |
6706 | 327 | continue; |
6707 | 327 | } |
6708 | | |
6709 | 11.8k | zend_eval_const_expr(cond_ast); |
6710 | 11.8k | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6711 | | /* Non-constant case */ |
6712 | 673 | return IS_UNDEF; |
6713 | 673 | } |
6714 | | |
6715 | 11.1k | cond_zv = zend_ast_get_zval(case_ast->child[0]); |
6716 | 11.1k | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6717 | | /* We only optimize switched on integers and strings */ |
6718 | 2.05k | return IS_UNDEF; |
6719 | 2.05k | } |
6720 | | |
6721 | 9.13k | if (common_type == IS_UNDEF) { |
6722 | 4.90k | common_type = Z_TYPE_P(cond_zv); |
6723 | 4.90k | } else if (common_type != Z_TYPE_P(cond_zv)) { |
6724 | | /* Non-uniform case types */ |
6725 | 21 | return IS_UNDEF; |
6726 | 21 | } |
6727 | | |
6728 | 9.10k | if (Z_TYPE_P(cond_zv) == IS_STRING |
6729 | 8.28k | && is_numeric_string(Z_STRVAL_P(cond_zv), Z_STRLEN_P(cond_zv), NULL, NULL, 0)) { |
6730 | | /* Numeric strings cannot be compared with a simple hash lookup */ |
6731 | 180 | return IS_UNDEF; |
6732 | 180 | } |
6733 | 9.10k | } |
6734 | | |
6735 | 3.24k | return common_type; |
6736 | 6.16k | } |
6737 | | |
6738 | 2.70k | static bool should_use_jumptable(const zend_ast_list *cases, uint8_t jumptable_type) { |
6739 | 2.70k | if (CG(compiler_options) & ZEND_COMPILE_NO_JUMPTABLES) { |
6740 | 0 | return false; |
6741 | 0 | } |
6742 | | |
6743 | | /* Thresholds are chosen based on when the average switch time for equidistributed |
6744 | | * input becomes smaller when using the jumptable optimization. */ |
6745 | 2.70k | if (jumptable_type == IS_LONG) { |
6746 | 223 | return cases->children >= 5; |
6747 | 2.48k | } else { |
6748 | 2.48k | ZEND_ASSERT(jumptable_type == IS_STRING); |
6749 | 2.48k | return cases->children >= 2; |
6750 | 2.48k | } |
6751 | 2.70k | } |
6752 | | |
6753 | | static void zend_compile_switch(zend_ast *ast) /* {{{ */ |
6754 | 6.16k | { |
6755 | 6.16k | zend_ast *expr_ast = ast->child[0]; |
6756 | 6.16k | zend_ast_list *cases = zend_ast_get_list(ast->child[1]); |
6757 | | |
6758 | 6.16k | uint32_t i; |
6759 | 6.16k | bool has_default_case = false; |
6760 | | |
6761 | 6.16k | znode expr_node, case_node; |
6762 | 6.16k | zend_op *opline; |
6763 | 6.16k | uint32_t *jmpnz_opnums, opnum_default_jmp, opnum_switch = (uint32_t)-1; |
6764 | 6.16k | uint8_t jumptable_type; |
6765 | 6.16k | HashTable *jumptable = NULL; |
6766 | | |
6767 | 6.16k | zend_compile_expr(&expr_node, expr_ast); |
6768 | | |
6769 | 6.16k | zend_begin_loop(ZEND_FREE, &expr_node, true); |
6770 | | |
6771 | 6.16k | case_node.op_type = IS_TMP_VAR; |
6772 | 6.16k | case_node.u.op.var = get_temporary_variable(); |
6773 | | |
6774 | 6.16k | jumptable_type = determine_switch_jumptable_type(cases); |
6775 | 6.16k | if (jumptable_type != IS_UNDEF && should_use_jumptable(cases, jumptable_type)) { |
6776 | 1.56k | znode jumptable_op; |
6777 | | |
6778 | 1.56k | ALLOC_HASHTABLE(jumptable); |
6779 | 1.56k | zend_hash_init(jumptable, cases->children, NULL, NULL, 0); |
6780 | 1.56k | jumptable_op.op_type = IS_CONST; |
6781 | 1.56k | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
6782 | | |
6783 | 1.56k | opline = zend_emit_op(NULL, |
6784 | 1.56k | jumptable_type == IS_LONG ? ZEND_SWITCH_LONG : ZEND_SWITCH_STRING, |
6785 | 1.56k | &expr_node, &jumptable_op); |
6786 | 1.56k | if (opline->op1_type == IS_CONST) { |
6787 | 904 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6788 | 904 | } |
6789 | 1.56k | opnum_switch = opline - CG(active_op_array)->opcodes; |
6790 | 1.56k | } |
6791 | | |
6792 | 6.16k | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), cases->children, 0); |
6793 | 20.1k | for (i = 0; i < cases->children; ++i) { |
6794 | 13.9k | zend_ast *case_ast = cases->child[i]; |
6795 | 13.9k | zend_ast *cond_ast = case_ast->child[0]; |
6796 | 13.9k | znode cond_node; |
6797 | | |
6798 | 13.9k | if (case_ast->attr == ZEND_ALT_CASE_SYNTAX) { |
6799 | 265 | CG(zend_lineno) = case_ast->lineno; |
6800 | 265 | zend_error(E_DEPRECATED, "Case statements followed by a semicolon (;) are deprecated, use a colon (:) instead"); |
6801 | 265 | } |
6802 | | |
6803 | 13.9k | if (!cond_ast) { |
6804 | 358 | if (has_default_case) { |
6805 | 21 | CG(zend_lineno) = case_ast->lineno; |
6806 | 21 | zend_error_noreturn(E_COMPILE_ERROR, |
6807 | 21 | "Switch statements may only contain one default clause"); |
6808 | 21 | } |
6809 | 337 | has_default_case = true; |
6810 | 337 | continue; |
6811 | 358 | } |
6812 | | |
6813 | 13.6k | zend_compile_expr(&cond_node, cond_ast); |
6814 | | |
6815 | 13.6k | if (expr_node.op_type == IS_CONST |
6816 | 7.40k | && Z_TYPE(expr_node.u.constant) == IS_FALSE) { |
6817 | 1.23k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
6818 | 12.3k | } else if (expr_node.op_type == IS_CONST |
6819 | 6.17k | && Z_TYPE(expr_node.u.constant) == IS_TRUE) { |
6820 | 2.37k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0); |
6821 | 10.0k | } else { |
6822 | 10.0k | opline = zend_emit_op(NULL, |
6823 | 10.0k | (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL, |
6824 | 10.0k | &expr_node, &cond_node); |
6825 | 10.0k | SET_NODE(opline->result, &case_node); |
6826 | 10.0k | if (opline->op1_type == IS_CONST) { |
6827 | 3.79k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
6828 | 3.79k | } |
6829 | | |
6830 | 10.0k | jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
6831 | 10.0k | } |
6832 | 13.6k | } |
6833 | | |
6834 | 6.14k | opnum_default_jmp = zend_emit_jump(0); |
6835 | | |
6836 | 19.8k | for (i = 0; i < cases->children; ++i) { |
6837 | 13.6k | zend_ast *case_ast = cases->child[i]; |
6838 | 13.6k | zend_ast *cond_ast = case_ast->child[0]; |
6839 | 13.6k | zend_ast *stmt_ast = case_ast->child[1]; |
6840 | | |
6841 | 13.6k | if (cond_ast) { |
6842 | 13.3k | zend_update_jump_target_to_next(jmpnz_opnums[i]); |
6843 | | |
6844 | 13.3k | if (jumptable) { |
6845 | 3.78k | zval *cond_zv = zend_ast_get_zval(cond_ast); |
6846 | 3.78k | zval jmp_target; |
6847 | 3.78k | ZVAL_LONG(&jmp_target, get_next_op_number()); |
6848 | | |
6849 | 3.78k | ZEND_ASSERT(Z_TYPE_P(cond_zv) == jumptable_type); |
6850 | 3.78k | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
6851 | 390 | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
6852 | 3.39k | } else { |
6853 | 3.39k | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
6854 | 3.39k | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
6855 | 3.39k | } |
6856 | 3.78k | } |
6857 | 13.3k | } else { |
6858 | 313 | zend_update_jump_target_to_next(opnum_default_jmp); |
6859 | | |
6860 | 313 | if (jumptable) { |
6861 | 66 | ZEND_ASSERT(opnum_switch != (uint32_t)-1); |
6862 | 66 | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6863 | 66 | opline->extended_value = get_next_op_number(); |
6864 | 66 | } |
6865 | 313 | } |
6866 | | |
6867 | 13.6k | zend_compile_stmt(stmt_ast); |
6868 | 13.6k | } |
6869 | | |
6870 | 6.14k | if (!has_default_case) { |
6871 | 5.81k | zend_update_jump_target_to_next(opnum_default_jmp); |
6872 | | |
6873 | 5.81k | if (jumptable) { |
6874 | 1.48k | opline = &CG(active_op_array)->opcodes[opnum_switch]; |
6875 | 1.48k | opline->extended_value = get_next_op_number(); |
6876 | 1.48k | } |
6877 | 5.81k | } |
6878 | | |
6879 | 6.14k | zend_end_loop(get_next_op_number(), &expr_node); |
6880 | | |
6881 | 6.14k | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
6882 | 2.32k | opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
6883 | 2.32k | opline->extended_value = ZEND_FREE_SWITCH; |
6884 | 3.81k | } else if (expr_node.op_type == IS_CONST) { |
6885 | 3.37k | zval_ptr_dtor_nogc(&expr_node.u.constant); |
6886 | 3.37k | } |
6887 | | |
6888 | 6.14k | efree(jmpnz_opnums); |
6889 | 6.14k | } |
6890 | | /* }}} */ |
6891 | | |
6892 | | static uint32_t count_match_conds(const zend_ast_list *arms) |
6893 | 4.25k | { |
6894 | 4.25k | uint32_t num_conds = 0; |
6895 | | |
6896 | 9.94k | for (uint32_t i = 0; i < arms->children; i++) { |
6897 | 5.69k | const zend_ast *arm_ast = arms->child[i]; |
6898 | 5.69k | if (arm_ast->child[0] == NULL) { |
6899 | 959 | continue; |
6900 | 959 | } |
6901 | | |
6902 | 4.73k | const zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6903 | 4.73k | num_conds += conds->children; |
6904 | 4.73k | } |
6905 | | |
6906 | 4.25k | return num_conds; |
6907 | 4.25k | } |
6908 | | |
6909 | 4.25k | static bool can_match_use_jumptable(const zend_ast_list *arms) { |
6910 | 6.75k | for (uint32_t i = 0; i < arms->children; i++) { |
6911 | 4.88k | const zend_ast *arm_ast = arms->child[i]; |
6912 | 4.88k | if (!arm_ast->child[0]) { |
6913 | | /* Skip default arm */ |
6914 | 840 | continue; |
6915 | 840 | } |
6916 | | |
6917 | 4.04k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
6918 | 7.26k | for (uint32_t j = 0; j < conds->children; j++) { |
6919 | 5.59k | zend_ast **cond_ast = &conds->child[j]; |
6920 | | |
6921 | 5.59k | zend_eval_const_expr(cond_ast); |
6922 | 5.59k | if ((*cond_ast)->kind != ZEND_AST_ZVAL) { |
6923 | 2.11k | return false; |
6924 | 2.11k | } |
6925 | | |
6926 | 3.48k | const zval *cond_zv = zend_ast_get_zval(*cond_ast); |
6927 | 3.48k | if (Z_TYPE_P(cond_zv) != IS_LONG && Z_TYPE_P(cond_zv) != IS_STRING) { |
6928 | 265 | return false; |
6929 | 265 | } |
6930 | 3.48k | } |
6931 | 4.04k | } |
6932 | | |
6933 | 1.87k | return true; |
6934 | 4.25k | } |
6935 | | |
6936 | | static bool zend_is_pipe_optimizable_callable_name(zend_ast *ast) |
6937 | 810 | { |
6938 | 810 | if (ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(ast)) == IS_STRING) { |
6939 | | /* Assert compilation adds a message operand, but this is incompatible with the |
6940 | | * pipe optimization that uses a temporary znode for the reference elimination. |
6941 | | * Therefore, disable the optimization for assert. |
6942 | | * Note that "assert" as a name is always treated as fully qualified. */ |
6943 | 671 | return !zend_string_equals_literal_ci(zend_ast_get_str(ast), "assert"); |
6944 | 671 | } |
6945 | | |
6946 | 139 | return true; |
6947 | 810 | } |
6948 | | |
6949 | | static zend_ast *zend_partial_apply(zend_ast *callable_ast, zend_ast *pipe_arg) |
6950 | 36.3k | { |
6951 | 36.3k | if (callable_ast->kind != ZEND_AST_CALL |
6952 | 35.2k | && callable_ast->kind != ZEND_AST_STATIC_CALL |
6953 | 34.9k | && callable_ast->kind != ZEND_AST_METHOD_CALL) { |
6954 | 33.9k | return NULL; |
6955 | 33.9k | } |
6956 | | |
6957 | 2.43k | zend_ast *args_ast = zend_ast_call_get_args(callable_ast); |
6958 | 2.43k | if (!args_ast || args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { |
6959 | 1.34k | return NULL; |
6960 | 1.34k | } |
6961 | | |
6962 | 1.09k | if (callable_ast->kind == ZEND_AST_CALL && |
6963 | 810 | !zend_is_pipe_optimizable_callable_name(callable_ast->child[0])) { |
6964 | 115 | return NULL; |
6965 | 115 | } |
6966 | | |
6967 | 975 | zend_ast_list *arg_list = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); |
6968 | | |
6969 | 975 | zend_ast *first_placeholder = NULL; |
6970 | 975 | bool uses_named_args = false; |
6971 | | |
6972 | 2.70k | for (uint32_t i = 0; i < arg_list->children; i++) { |
6973 | 1.75k | zend_ast *arg = arg_list->child[i]; |
6974 | 1.75k | if (arg->kind == ZEND_AST_NAMED_ARG) { |
6975 | 33 | uses_named_args = true; |
6976 | 33 | arg = arg->child[1]; |
6977 | 33 | } |
6978 | | |
6979 | 1.75k | if (arg->kind == ZEND_AST_PLACEHOLDER_ARG) { |
6980 | 995 | if (first_placeholder == NULL) { |
6981 | 975 | first_placeholder = arg; |
6982 | 975 | } else { |
6983 | | /* A PFA with multiple placeholders is unexpected in this |
6984 | | * context, and will usually error due to a missing argument, |
6985 | | * so we don't optimize those. */ |
6986 | 20 | return NULL; |
6987 | 20 | } |
6988 | 975 | if (arg->attr == ZEND_PLACEHOLDER_VARIADIC && uses_named_args) { |
6989 | | /* A PFA with both a variadic placeholder and named args can not |
6990 | | * be optimized because this would result in a positional arg |
6991 | | * after a named arg: f(name: $v, ...) -> f(name: $v, pipe_arg). |
6992 | | * Arg placeholders ('?') are safe since they are not allowed |
6993 | | * after named args. */ |
6994 | 9 | return NULL; |
6995 | 9 | } |
6996 | 975 | } |
6997 | 1.75k | } |
6998 | | |
6999 | 946 | ZEND_ASSERT(first_placeholder); |
7000 | | |
7001 | 946 | zend_ast *new_arg_list = zend_ast_create_list(0, arg_list->kind); |
7002 | 2.64k | for (uint32_t i = 0; i < arg_list->children; i++) { |
7003 | 1.70k | zend_ast *arg = arg_list->child[i]; |
7004 | 1.70k | if (arg == first_placeholder) { |
7005 | 938 | new_arg_list = zend_ast_list_add(new_arg_list, pipe_arg); |
7006 | 938 | } else if (arg->kind == ZEND_AST_NAMED_ARG |
7007 | 14 | && arg->child[1] == first_placeholder) { |
7008 | 8 | zend_ast *name = arg->child[0]; |
7009 | 8 | new_arg_list = zend_ast_list_add(new_arg_list, |
7010 | 8 | zend_ast_create(ZEND_AST_NAMED_ARG, name, pipe_arg)); |
7011 | 755 | } else { |
7012 | 755 | new_arg_list = zend_ast_list_add(new_arg_list, arg); |
7013 | 755 | } |
7014 | 1.70k | } |
7015 | | |
7016 | 946 | return new_arg_list; |
7017 | 946 | } |
7018 | | |
7019 | | static void zend_compile_pipe(znode *result, zend_ast *ast, uint32_t type) |
7020 | 36.1k | { |
7021 | 36.1k | zend_ast *operand_ast = ast->child[0]; |
7022 | 36.1k | zend_ast *callable_ast = ast->child[1]; |
7023 | | |
7024 | 36.1k | if (callable_ast->kind == ZEND_AST_ARROW_FUNC && !(callable_ast->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) { |
7025 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Arrow functions on the right hand side of |> must be parenthesized"); |
7026 | 7 | } |
7027 | | |
7028 | | /* Compile the left hand side down to a value first. */ |
7029 | 36.1k | znode operand_result; |
7030 | 36.1k | zend_compile_expr(&operand_result, operand_ast); |
7031 | | |
7032 | | /* Wrap simple values in a ZEND_QM_ASSIGN opcode to ensure references |
7033 | | * always fail. They will already fail in complex cases like arrays, |
7034 | | * so those don't need a wrapper. */ |
7035 | 36.1k | znode wrapped_operand_result; |
7036 | 36.1k | if (operand_result.op_type & (IS_CV|IS_VAR)) { |
7037 | 121 | zend_emit_op_tmp(&wrapped_operand_result, ZEND_QM_ASSIGN, &operand_result, NULL); |
7038 | 36.0k | } else { |
7039 | 36.0k | wrapped_operand_result = operand_result; |
7040 | 36.0k | } |
7041 | | |
7042 | | /* Turn the operand into a function parameter list. */ |
7043 | 36.1k | zend_ast *arg = zend_ast_create_znode(&wrapped_operand_result); |
7044 | | |
7045 | 36.1k | zend_ast *fcall_ast; |
7046 | 36.1k | znode callable_result; |
7047 | 36.1k | zend_ast *pfa_arg_list_ast = NULL; |
7048 | | |
7049 | | /* Turn $foo |> PFA into plain function call if possible */ |
7050 | 36.1k | if ((pfa_arg_list_ast = zend_partial_apply(callable_ast, arg))) { |
7051 | 771 | switch (callable_ast->kind) { |
7052 | 513 | case ZEND_AST_CALL: |
7053 | 513 | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
7054 | 513 | callable_ast->child[0], pfa_arg_list_ast); |
7055 | 513 | break; |
7056 | 248 | case ZEND_AST_STATIC_CALL: |
7057 | 248 | fcall_ast = zend_ast_create(ZEND_AST_STATIC_CALL, |
7058 | 248 | callable_ast->child[0], callable_ast->child[1], |
7059 | 248 | pfa_arg_list_ast); |
7060 | 248 | break; |
7061 | 10 | case ZEND_AST_METHOD_CALL: |
7062 | 10 | fcall_ast = zend_ast_create(ZEND_AST_METHOD_CALL, |
7063 | 10 | callable_ast->child[0], callable_ast->child[1], |
7064 | 10 | pfa_arg_list_ast); |
7065 | 10 | break; |
7066 | 0 | default: |
7067 | 0 | ZEND_UNREACHABLE(); |
7068 | 771 | } |
7069 | | /* Turn $foo |> $expr into ($expr)($foo) */ |
7070 | 35.3k | } else { |
7071 | 35.3k | zend_ast *arg_list_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, arg); |
7072 | 35.3k | zend_compile_expr(&callable_result, callable_ast); |
7073 | 35.3k | callable_ast = zend_ast_create_znode(&callable_result); |
7074 | 35.3k | fcall_ast = zend_ast_create(ZEND_AST_CALL, |
7075 | 35.3k | callable_ast, arg_list_ast); |
7076 | 35.3k | } |
7077 | | |
7078 | 36.1k | zend_do_extended_stmt(&operand_result); |
7079 | | |
7080 | 36.1k | zend_compile_var(result, fcall_ast, type, /* by_ref */ false); |
7081 | 36.1k | } |
7082 | | |
7083 | | static void zend_compile_match(znode *result, zend_ast *ast) |
7084 | 4.25k | { |
7085 | 4.25k | zend_ast *expr_ast = ast->child[0]; |
7086 | 4.25k | zend_ast_list *arms = zend_ast_get_list(ast->child[1]); |
7087 | 4.25k | bool has_default_arm = false; |
7088 | 4.25k | uint32_t opnum_match = (uint32_t)-1; |
7089 | | |
7090 | 4.25k | znode expr_node; |
7091 | 4.25k | zend_compile_expr(&expr_node, expr_ast); |
7092 | | |
7093 | 4.25k | znode case_node; |
7094 | 4.25k | case_node.op_type = IS_TMP_VAR; |
7095 | 4.25k | case_node.u.op.var = get_temporary_variable(); |
7096 | | |
7097 | 4.25k | uint32_t num_conds = count_match_conds(arms); |
7098 | 4.25k | uint8_t can_use_jumptable = can_match_use_jumptable(arms); |
7099 | 4.25k | bool uses_jumptable = can_use_jumptable && num_conds >= 2; |
7100 | 4.25k | HashTable *jumptable = NULL; |
7101 | 4.25k | uint32_t *jmpnz_opnums = NULL; |
7102 | | |
7103 | 9.94k | for (uint32_t i = 0; i < arms->children; ++i) { |
7104 | 5.69k | zend_ast *arm_ast = arms->child[i]; |
7105 | | |
7106 | 5.69k | if (!arm_ast->child[0]) { |
7107 | 959 | if (has_default_arm) { |
7108 | 5 | CG(zend_lineno) = arm_ast->lineno; |
7109 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7110 | 5 | "Match expressions may only contain one default arm"); |
7111 | 5 | } |
7112 | 954 | has_default_arm = true; |
7113 | 954 | } |
7114 | 5.69k | } |
7115 | | |
7116 | 4.24k | if (uses_jumptable) { |
7117 | 757 | znode jumptable_op; |
7118 | | |
7119 | 757 | ALLOC_HASHTABLE(jumptable); |
7120 | 757 | zend_hash_init(jumptable, num_conds, NULL, NULL, 0); |
7121 | 757 | jumptable_op.op_type = IS_CONST; |
7122 | 757 | ZVAL_ARR(&jumptable_op.u.constant, jumptable); |
7123 | | |
7124 | 757 | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH, &expr_node, &jumptable_op); |
7125 | 757 | if (opline->op1_type == IS_CONST) { |
7126 | 266 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7127 | 266 | } |
7128 | 757 | opnum_match = opline - CG(active_op_array)->opcodes; |
7129 | 3.49k | } else { |
7130 | 3.49k | jmpnz_opnums = safe_emalloc(sizeof(uint32_t), num_conds, 0); |
7131 | 3.49k | uint32_t cond_count = 0; |
7132 | 7.67k | for (uint32_t i = 0; i < arms->children; ++i) { |
7133 | 4.18k | zend_ast *arm_ast = arms->child[i]; |
7134 | | |
7135 | 4.18k | if (!arm_ast->child[0]) { |
7136 | 776 | continue; |
7137 | 776 | } |
7138 | | |
7139 | 3.40k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
7140 | 8.39k | for (uint32_t j = 0; j < conds->children; j++) { |
7141 | 4.98k | zend_ast *cond_ast = conds->child[j]; |
7142 | | |
7143 | 4.98k | znode cond_node; |
7144 | 4.98k | zend_compile_expr(&cond_node, cond_ast); |
7145 | | |
7146 | 4.98k | uint32_t opcode = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE_STRICT : ZEND_IS_IDENTICAL; |
7147 | 4.98k | zend_op *opline = zend_emit_op(NULL, opcode, &expr_node, &cond_node); |
7148 | 4.98k | SET_NODE(opline->result, &case_node); |
7149 | 4.98k | if (opline->op1_type == IS_CONST) { |
7150 | 2.15k | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7151 | 2.15k | } |
7152 | | |
7153 | 4.98k | jmpnz_opnums[cond_count] = zend_emit_cond_jump(ZEND_JMPNZ, &case_node, 0); |
7154 | | |
7155 | 4.98k | cond_count++; |
7156 | 4.98k | } |
7157 | 3.40k | } |
7158 | 3.49k | } |
7159 | | |
7160 | 4.24k | uint32_t opnum_default_jmp = 0; |
7161 | 4.24k | if (!uses_jumptable) { |
7162 | 3.49k | opnum_default_jmp = zend_emit_jump(0); |
7163 | 3.49k | } |
7164 | | |
7165 | 4.24k | bool is_first_case = true; |
7166 | 4.24k | uint32_t cond_count = 0; |
7167 | 4.24k | uint32_t *jmp_end_opnums = safe_emalloc(sizeof(uint32_t), arms->children, 0); |
7168 | | |
7169 | | // The generated default arm is emitted first to avoid live range issues where the tmpvar |
7170 | | // for the arm result is freed even though it has not been initialized yet. |
7171 | 4.24k | if (!has_default_arm) { |
7172 | 3.29k | if (!uses_jumptable) { |
7173 | 2.71k | zend_update_jump_target_to_next(opnum_default_jmp); |
7174 | 2.71k | } |
7175 | | |
7176 | 3.29k | if (jumptable) { |
7177 | 584 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
7178 | 584 | opline->extended_value = get_next_op_number(); |
7179 | 584 | } |
7180 | | |
7181 | 3.29k | zend_op *opline = zend_emit_op(NULL, ZEND_MATCH_ERROR, &expr_node, NULL); |
7182 | 3.29k | if (opline->op1_type == IS_CONST) { |
7183 | 971 | Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1)); |
7184 | 971 | } |
7185 | 3.29k | if (arms->children == 0) { |
7186 | | /* Mark this as an "expression throw" for opcache. */ |
7187 | 427 | opline->extended_value = ZEND_THROW_IS_EXPR; |
7188 | 427 | } |
7189 | 3.29k | } |
7190 | | |
7191 | 9.92k | for (uint32_t i = 0; i < arms->children; ++i) { |
7192 | 5.67k | zend_ast *arm_ast = arms->child[i]; |
7193 | 5.67k | zend_ast *body_ast = arm_ast->child[1]; |
7194 | | |
7195 | 5.67k | if (arm_ast->child[0] != NULL) { |
7196 | 4.72k | zend_ast_list *conds = zend_ast_get_list(arm_ast->child[0]); |
7197 | | |
7198 | 12.0k | for (uint32_t j = 0; j < conds->children; j++) { |
7199 | 7.36k | zend_ast *cond_ast = conds->child[j]; |
7200 | | |
7201 | 7.36k | if (jmpnz_opnums != NULL) { |
7202 | 4.98k | zend_update_jump_target_to_next(jmpnz_opnums[cond_count]); |
7203 | 4.98k | } |
7204 | | |
7205 | 7.36k | if (jumptable) { |
7206 | 2.38k | zval *cond_zv = zend_ast_get_zval(cond_ast); |
7207 | 2.38k | zval jmp_target; |
7208 | 2.38k | ZVAL_LONG(&jmp_target, get_next_op_number()); |
7209 | | |
7210 | 2.38k | if (Z_TYPE_P(cond_zv) == IS_LONG) { |
7211 | 2.02k | zend_hash_index_add(jumptable, Z_LVAL_P(cond_zv), &jmp_target); |
7212 | 2.02k | } else { |
7213 | 359 | ZEND_ASSERT(Z_TYPE_P(cond_zv) == IS_STRING); |
7214 | 359 | zend_hash_add(jumptable, Z_STR_P(cond_zv), &jmp_target); |
7215 | 359 | } |
7216 | 2.38k | } |
7217 | | |
7218 | 7.36k | cond_count++; |
7219 | 7.36k | } |
7220 | 4.72k | } else { |
7221 | 949 | if (!uses_jumptable) { |
7222 | 776 | zend_update_jump_target_to_next(opnum_default_jmp); |
7223 | 776 | } |
7224 | | |
7225 | 949 | if (jumptable) { |
7226 | 173 | ZEND_ASSERT(opnum_match != (uint32_t)-1); |
7227 | 173 | zend_op *opline = &CG(active_op_array)->opcodes[opnum_match]; |
7228 | 173 | opline->extended_value = get_next_op_number(); |
7229 | 173 | } |
7230 | 949 | } |
7231 | | |
7232 | 5.67k | znode body_node; |
7233 | 5.67k | zend_compile_expr(&body_node, body_ast); |
7234 | | |
7235 | 5.67k | if (is_first_case) { |
7236 | 3.82k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL); |
7237 | 3.82k | is_first_case = false; |
7238 | 3.82k | } else { |
7239 | 1.85k | zend_op *opline_qm_assign = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL); |
7240 | 1.85k | SET_NODE(opline_qm_assign->result, result); |
7241 | 1.85k | } |
7242 | | |
7243 | 5.67k | jmp_end_opnums[i] = zend_emit_jump(0); |
7244 | 5.67k | } |
7245 | | |
7246 | | // Initialize result in case there is no arm |
7247 | 4.24k | if (arms->children == 0) { |
7248 | 427 | result->op_type = IS_CONST; |
7249 | 427 | ZVAL_NULL(&result->u.constant); |
7250 | 427 | } |
7251 | | |
7252 | 9.92k | for (uint32_t i = 0; i < arms->children; ++i) { |
7253 | 5.67k | zend_update_jump_target_to_next(jmp_end_opnums[i]); |
7254 | 5.67k | } |
7255 | | |
7256 | 4.24k | if (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) { |
7257 | 2.45k | zend_op *opline = zend_emit_op(NULL, ZEND_FREE, &expr_node, NULL); |
7258 | 2.45k | opline->extended_value = ZEND_FREE_SWITCH; |
7259 | 2.45k | } else if (expr_node.op_type == IS_CONST) { |
7260 | 1.13k | zval_ptr_dtor_nogc(&expr_node.u.constant); |
7261 | 1.13k | } |
7262 | | |
7263 | 4.24k | if (jmpnz_opnums != NULL) { |
7264 | 3.49k | efree(jmpnz_opnums); |
7265 | 3.49k | } |
7266 | 4.24k | efree(jmp_end_opnums); |
7267 | 4.24k | } |
7268 | | |
7269 | | static void zend_compile_try(const zend_ast *ast) /* {{{ */ |
7270 | 35.3k | { |
7271 | 35.3k | zend_ast *try_ast = ast->child[0]; |
7272 | 35.3k | const zend_ast_list *catches = zend_ast_get_list(ast->child[1]); |
7273 | 35.3k | zend_ast *finally_ast = ast->child[2]; |
7274 | | |
7275 | 35.3k | uint32_t i, j; |
7276 | 35.3k | zend_op *opline; |
7277 | 35.3k | uint32_t try_catch_offset; |
7278 | 35.3k | uint32_t *jmp_opnums = safe_emalloc(sizeof(uint32_t), catches->children, 0); |
7279 | 35.3k | uint32_t orig_fast_call_var = CG(context).fast_call_var; |
7280 | 35.3k | uint32_t orig_try_catch_offset = CG(context).try_catch_offset; |
7281 | | |
7282 | 35.3k | if (catches->children == 0 && !finally_ast) { |
7283 | 76 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use try without catch or finally"); |
7284 | 76 | } |
7285 | | |
7286 | | /* label: try { } must not be equal to try { label: } */ |
7287 | 35.2k | if (CG(context).labels) { |
7288 | 1.27k | zend_label *label; |
7289 | 1.27k | ZEND_HASH_MAP_REVERSE_FOREACH_PTR(CG(context).labels, label) { |
7290 | 1.27k | if (label->opline_num == get_next_op_number()) { |
7291 | 560 | zend_emit_op(NULL, ZEND_NOP, NULL, NULL); |
7292 | 560 | } |
7293 | 1.27k | break; |
7294 | 3.82k | } ZEND_HASH_FOREACH_END(); |
7295 | 1.27k | } |
7296 | | |
7297 | 35.2k | try_catch_offset = zend_add_try_element(get_next_op_number()); |
7298 | | |
7299 | 35.2k | if (finally_ast) { |
7300 | 4.32k | zend_loop_var fast_call; |
7301 | 4.32k | if (!(CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)) { |
7302 | 1.60k | CG(active_op_array)->fn_flags |= ZEND_ACC_HAS_FINALLY_BLOCK; |
7303 | 1.60k | } |
7304 | 4.32k | CG(context).fast_call_var = get_temporary_variable(); |
7305 | | |
7306 | | /* Push FAST_CALL on unwind stack */ |
7307 | 4.32k | fast_call.opcode = ZEND_FAST_CALL; |
7308 | 4.32k | fast_call.var_type = IS_TMP_VAR; |
7309 | 4.32k | fast_call.var_num = CG(context).fast_call_var; |
7310 | 4.32k | fast_call.try_catch_offset = try_catch_offset; |
7311 | 4.32k | zend_stack_push(&CG(loop_var_stack), &fast_call); |
7312 | 4.32k | } |
7313 | | |
7314 | 35.2k | CG(context).try_catch_offset = try_catch_offset; |
7315 | | |
7316 | 35.2k | zend_compile_stmt(try_ast); |
7317 | | |
7318 | 35.2k | if (catches->children != 0) { |
7319 | 31.1k | jmp_opnums[0] = zend_emit_jump(0); |
7320 | 31.1k | } |
7321 | | |
7322 | 71.2k | for (i = 0; i < catches->children; ++i) { |
7323 | 35.9k | const zend_ast *catch_ast = catches->child[i]; |
7324 | 35.9k | const zend_ast_list *classes = zend_ast_get_list(catch_ast->child[0]); |
7325 | 35.9k | zend_ast *var_ast = catch_ast->child[1]; |
7326 | 35.9k | zend_ast *stmt_ast = catch_ast->child[2]; |
7327 | 35.9k | zend_string *var_name = var_ast ? zval_make_interned_string(zend_ast_get_zval(var_ast)) : NULL; |
7328 | 35.9k | bool is_last_catch = (i + 1 == catches->children); |
7329 | | |
7330 | 35.9k | uint32_t *jmp_multicatch = safe_emalloc(sizeof(uint32_t), classes->children - 1, 0); |
7331 | 35.9k | uint32_t opnum_catch = (uint32_t)-1; |
7332 | | |
7333 | 35.9k | CG(zend_lineno) = catch_ast->lineno; |
7334 | | |
7335 | 78.2k | for (j = 0; j < classes->children; j++) { |
7336 | 42.2k | zend_ast *class_ast = classes->child[j]; |
7337 | 42.2k | bool is_last_class = (j + 1 == classes->children); |
7338 | | |
7339 | 42.2k | if (!zend_is_const_default_class_ref(class_ast)) { |
7340 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Bad class name in the catch statement"); |
7341 | 7 | } |
7342 | | |
7343 | 42.2k | opnum_catch = get_next_op_number(); |
7344 | 42.2k | if (i == 0 && j == 0) { |
7345 | 31.1k | CG(active_op_array)->try_catch_array[try_catch_offset].catch_op = opnum_catch; |
7346 | 31.1k | } |
7347 | | |
7348 | 42.2k | opline = get_next_op(); |
7349 | 42.2k | opline->opcode = ZEND_CATCH; |
7350 | 42.2k | opline->op1_type = IS_CONST; |
7351 | 42.2k | opline->op1.constant = zend_add_class_name_literal( |
7352 | 42.2k | zend_resolve_class_name_ast(class_ast)); |
7353 | 42.2k | opline->extended_value = zend_alloc_cache_slot(); |
7354 | | |
7355 | 42.2k | if (var_name && zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
7356 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
7357 | 7 | } |
7358 | | |
7359 | 42.2k | opline->result_type = var_name ? IS_CV : IS_UNUSED; |
7360 | 42.2k | opline->result.var = var_name ? lookup_cv(var_name) : -1; |
7361 | | |
7362 | 42.2k | if (is_last_catch && is_last_class) { |
7363 | 31.1k | opline->extended_value |= ZEND_LAST_CATCH; |
7364 | 31.1k | } |
7365 | | |
7366 | 42.2k | if (!is_last_class) { |
7367 | 6.28k | jmp_multicatch[j] = zend_emit_jump(0); |
7368 | 6.28k | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7369 | 6.28k | opline->op2.opline_num = get_next_op_number(); |
7370 | 6.28k | } |
7371 | 42.2k | } |
7372 | | |
7373 | 42.2k | for (j = 0; j < classes->children - 1; j++) { |
7374 | 6.28k | zend_update_jump_target_to_next(jmp_multicatch[j]); |
7375 | 6.28k | } |
7376 | | |
7377 | 35.9k | efree(jmp_multicatch); |
7378 | | |
7379 | 35.9k | zend_compile_stmt(stmt_ast); |
7380 | | |
7381 | 35.9k | if (!is_last_catch) { |
7382 | 4.83k | jmp_opnums[i + 1] = zend_emit_jump(0); |
7383 | 4.83k | } |
7384 | | |
7385 | 35.9k | ZEND_ASSERT(opnum_catch != (uint32_t)-1 && "Should have at least one class"); |
7386 | 35.9k | opline = &CG(active_op_array)->opcodes[opnum_catch]; |
7387 | 35.9k | if (!is_last_catch) { |
7388 | 4.83k | opline->op2.opline_num = get_next_op_number(); |
7389 | 4.83k | } |
7390 | 35.9k | } |
7391 | | |
7392 | 71.2k | for (i = 0; i < catches->children; ++i) { |
7393 | 35.9k | zend_update_jump_target_to_next(jmp_opnums[i]); |
7394 | 35.9k | } |
7395 | | |
7396 | 35.2k | if (finally_ast) { |
7397 | 4.31k | zend_loop_var discard_exception; |
7398 | 4.31k | uint32_t opnum_jmp = get_next_op_number() + 1; |
7399 | | |
7400 | | /* Pop FAST_CALL from unwind stack */ |
7401 | 4.31k | zend_stack_del_top(&CG(loop_var_stack)); |
7402 | | |
7403 | | /* Push DISCARD_EXCEPTION on unwind stack */ |
7404 | 4.31k | discard_exception.opcode = ZEND_DISCARD_EXCEPTION; |
7405 | 4.31k | discard_exception.var_type = IS_TMP_VAR; |
7406 | 4.31k | discard_exception.var_num = CG(context).fast_call_var; |
7407 | 4.31k | zend_stack_push(&CG(loop_var_stack), &discard_exception); |
7408 | | |
7409 | 4.31k | CG(zend_lineno) = finally_ast->lineno; |
7410 | | |
7411 | 4.31k | opline = zend_emit_op(NULL, ZEND_FAST_CALL, NULL, NULL); |
7412 | 4.31k | opline->op1.num = try_catch_offset; |
7413 | 4.31k | opline->result_type = IS_TMP_VAR; |
7414 | 4.31k | opline->result.var = CG(context).fast_call_var; |
7415 | | |
7416 | 4.31k | zend_emit_op(NULL, ZEND_JMP, NULL, NULL); |
7417 | | |
7418 | 4.31k | bool orig_in_finally = CG(context).in_finally; |
7419 | 4.31k | CG(context).in_finally = true; |
7420 | 4.31k | zend_compile_stmt(finally_ast); |
7421 | 4.31k | CG(context).in_finally = orig_in_finally; |
7422 | | |
7423 | 4.31k | CG(active_op_array)->try_catch_array[try_catch_offset].finally_op = opnum_jmp + 1; |
7424 | 4.31k | CG(active_op_array)->try_catch_array[try_catch_offset].finally_end |
7425 | 4.31k | = get_next_op_number(); |
7426 | | |
7427 | 4.31k | opline = zend_emit_op(NULL, ZEND_FAST_RET, NULL, NULL); |
7428 | 4.31k | opline->op1_type = IS_TMP_VAR; |
7429 | 4.31k | opline->op1.var = CG(context).fast_call_var; |
7430 | 4.31k | opline->op2.num = orig_try_catch_offset; |
7431 | | |
7432 | 4.31k | zend_update_jump_target_to_next(opnum_jmp); |
7433 | | |
7434 | 4.31k | CG(context).fast_call_var = orig_fast_call_var; |
7435 | | |
7436 | | /* Pop DISCARD_EXCEPTION from unwind stack */ |
7437 | 4.31k | zend_stack_del_top(&CG(loop_var_stack)); |
7438 | 4.31k | } |
7439 | | |
7440 | 35.2k | CG(context).try_catch_offset = orig_try_catch_offset; |
7441 | | |
7442 | 35.2k | efree(jmp_opnums); |
7443 | 35.2k | } |
7444 | | /* }}} */ |
7445 | | |
7446 | | /* Encoding declarations must already be handled during parsing */ |
7447 | | bool zend_handle_encoding_declaration(zend_ast *ast) /* {{{ */ |
7448 | 3.26k | { |
7449 | 3.26k | const zend_ast_list *declares = zend_ast_get_list(ast); |
7450 | 3.26k | uint32_t i; |
7451 | 7.12k | for (i = 0; i < declares->children; ++i) { |
7452 | 3.86k | const zend_ast *declare_ast = declares->child[i]; |
7453 | 3.86k | zend_ast *name_ast = declare_ast->child[0]; |
7454 | 3.86k | zend_ast *value_ast = declare_ast->child[1]; |
7455 | 3.86k | const zend_string *name = zend_ast_get_str(name_ast); |
7456 | | |
7457 | 3.86k | if (zend_string_equals_literal_ci(name, "encoding")) { |
7458 | 201 | if (value_ast->kind != ZEND_AST_ZVAL) { |
7459 | 18 | zend_throw_exception(zend_ce_compile_error, "Encoding must be a literal", 0); |
7460 | 18 | return false; |
7461 | 18 | } |
7462 | | |
7463 | 183 | if (CG(multibyte)) { |
7464 | 0 | zend_string *encoding_name = zval_get_string(zend_ast_get_zval(value_ast)); |
7465 | |
|
7466 | 0 | const zend_encoding *new_encoding, *old_encoding; |
7467 | 0 | zend_encoding_filter old_input_filter; |
7468 | |
|
7469 | 0 | CG(encoding_declared) = 1; |
7470 | |
|
7471 | 0 | new_encoding = zend_multibyte_fetch_encoding(ZSTR_VAL(encoding_name)); |
7472 | 0 | if (!new_encoding) { |
7473 | 0 | zend_error(E_COMPILE_WARNING, "Unsupported encoding [%s]", ZSTR_VAL(encoding_name)); |
7474 | 0 | } else { |
7475 | 0 | old_input_filter = LANG_SCNG(input_filter); |
7476 | 0 | old_encoding = LANG_SCNG(script_encoding); |
7477 | 0 | zend_multibyte_set_filter(new_encoding); |
7478 | | |
7479 | | /* need to re-scan if input filter changed */ |
7480 | 0 | if (old_input_filter != LANG_SCNG(input_filter) || |
7481 | 0 | (old_input_filter && new_encoding != old_encoding)) { |
7482 | 0 | zend_multibyte_yyinput_again(old_input_filter, old_encoding); |
7483 | 0 | } |
7484 | 0 | } |
7485 | |
|
7486 | 0 | zend_string_release_ex(encoding_name, 0); |
7487 | 183 | } else { |
7488 | 183 | zend_error(E_COMPILE_WARNING, "declare(encoding=...) ignored because " |
7489 | 183 | "Zend multibyte feature is turned off by settings"); |
7490 | 183 | } |
7491 | 183 | } |
7492 | 3.86k | } |
7493 | | |
7494 | 3.25k | return true; |
7495 | 3.26k | } |
7496 | | /* }}} */ |
7497 | | |
7498 | | /* Check whether this is the first statement, not counting declares. */ |
7499 | | static zend_result zend_is_first_statement(const zend_ast *ast, bool allow_nop) /* {{{ */ |
7500 | 3.46k | { |
7501 | 3.46k | uint32_t i = 0; |
7502 | 3.46k | const zend_ast_list *file_ast = zend_ast_get_list(CG(ast)); |
7503 | | |
7504 | 14.2k | while (i < file_ast->children) { |
7505 | 14.2k | if (file_ast->child[i] == ast) { |
7506 | 3.40k | return SUCCESS; |
7507 | 10.8k | } else if (file_ast->child[i] == NULL) { |
7508 | 6.37k | if (!allow_nop) { |
7509 | 2 | return FAILURE; |
7510 | 2 | } |
7511 | 6.37k | } else if (file_ast->child[i]->kind != ZEND_AST_DECLARE) { |
7512 | 57 | return FAILURE; |
7513 | 57 | } |
7514 | 10.8k | i++; |
7515 | 10.8k | } |
7516 | 1 | return FAILURE; |
7517 | 3.46k | } |
7518 | | /* }}} */ |
7519 | | |
7520 | | static void zend_compile_declare(const zend_ast *ast) /* {{{ */ |
7521 | 9.45k | { |
7522 | 9.45k | const zend_ast_list *declares = zend_ast_get_list(ast->child[0]); |
7523 | 9.45k | zend_ast *stmt_ast = ast->child[1]; |
7524 | 9.45k | zend_declarables orig_declarables = FC(declarables); |
7525 | 9.45k | uint32_t i; |
7526 | | |
7527 | 19.8k | for (i = 0; i < declares->children; ++i) { |
7528 | 10.5k | zend_ast *declare_ast = declares->child[i]; |
7529 | 10.5k | zend_ast *name_ast = declare_ast->child[0]; |
7530 | 10.5k | zend_ast **value_ast_ptr = &declare_ast->child[1]; |
7531 | 10.5k | zend_string *name = zend_ast_get_str(name_ast); |
7532 | | |
7533 | 10.5k | if ((*value_ast_ptr)->kind != ZEND_AST_ZVAL) { |
7534 | 28 | zend_error_noreturn(E_COMPILE_ERROR, "declare(%s) value must be a literal", ZSTR_VAL(name)); |
7535 | 28 | } |
7536 | | |
7537 | 10.4k | if (zend_string_equals_literal_ci(name, "ticks")) { |
7538 | 7.75k | zval value_zv; |
7539 | 7.75k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7540 | 7.75k | FC(declarables).ticks = zval_get_long(&value_zv); |
7541 | 7.75k | zval_ptr_dtor_nogc(&value_zv); |
7542 | 7.75k | } else if (zend_string_equals_literal_ci(name, "encoding")) { |
7543 | | |
7544 | 114 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ false)) { |
7545 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Encoding declaration pragma must be " |
7546 | 8 | "the very first statement in the script"); |
7547 | 8 | } |
7548 | 2.62k | } else if (zend_string_equals_literal_ci(name, "strict_types")) { |
7549 | 451 | zval value_zv; |
7550 | | |
7551 | 451 | if (FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
7552 | 24 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must be " |
7553 | 24 | "the very first statement in the script"); |
7554 | 24 | } |
7555 | | |
7556 | 427 | if (ast->child[1] != NULL) { |
7557 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must not " |
7558 | 12 | "use block mode"); |
7559 | 12 | } |
7560 | | |
7561 | 415 | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
7562 | | |
7563 | 415 | if (Z_TYPE(value_zv) != IS_LONG || (Z_LVAL(value_zv) != 0 && Z_LVAL(value_zv) != 1)) { |
7564 | 52 | zend_error_noreturn(E_COMPILE_ERROR, "strict_types declaration must have 0 or 1 as its value"); |
7565 | 52 | } |
7566 | | |
7567 | 363 | if (Z_LVAL(value_zv) == 1) { |
7568 | 298 | CG(active_op_array)->fn_flags |= ZEND_ACC_STRICT_TYPES; |
7569 | 298 | } |
7570 | | |
7571 | 2.17k | } else { |
7572 | 2.17k | zend_error(E_COMPILE_WARNING, "Unsupported declare '%s'", ZSTR_VAL(name)); |
7573 | 2.17k | } |
7574 | 10.4k | } |
7575 | | |
7576 | 9.33k | if (stmt_ast) { |
7577 | 1.04k | zend_compile_stmt(stmt_ast); |
7578 | | |
7579 | 1.04k | FC(declarables) = orig_declarables; |
7580 | 1.04k | } |
7581 | 9.33k | } |
7582 | | /* }}} */ |
7583 | | |
7584 | | static void zend_compile_stmt_list(zend_ast *ast) /* {{{ */ |
7585 | 1.64M | { |
7586 | 1.64M | const zend_ast_list *list = zend_ast_get_list(ast); |
7587 | 1.64M | uint32_t i; |
7588 | 7.30M | for (i = 0; i < list->children; ++i) { |
7589 | 5.66M | zend_compile_stmt(list->child[i]); |
7590 | 5.66M | } |
7591 | 1.64M | } |
7592 | | /* }}} */ |
7593 | | |
7594 | | ZEND_API void zend_set_function_arg_flags(zend_function *func) /* {{{ */ |
7595 | 1.11M | { |
7596 | 1.11M | uint32_t i, n; |
7597 | | |
7598 | 1.11M | func->common.arg_flags[0] = 0; |
7599 | 1.11M | func->common.arg_flags[1] = 0; |
7600 | 1.11M | func->common.arg_flags[2] = 0; |
7601 | 1.11M | if (func->common.arg_info) { |
7602 | 1.11M | n = MIN(func->common.num_args, MAX_ARG_FLAG_NUM); |
7603 | 1.11M | i = 0; |
7604 | 2.25M | while (i < n) { |
7605 | 1.13M | ZEND_SET_ARG_FLAG(func, i + 1, ZEND_ARG_SEND_MODE(&func->common.arg_info[i])); |
7606 | 1.13M | i++; |
7607 | 1.13M | } |
7608 | 1.11M | if (UNEXPECTED((func->common.fn_flags & ZEND_ACC_VARIADIC) && ZEND_ARG_SEND_MODE(&func->common.arg_info[i]))) { |
7609 | 399 | uint32_t pass_by_reference = ZEND_ARG_SEND_MODE(&func->common.arg_info[i]); |
7610 | 5.05k | while (i < MAX_ARG_FLAG_NUM) { |
7611 | 4.66k | ZEND_SET_ARG_FLAG(func, i + 1, pass_by_reference); |
7612 | 4.66k | i++; |
7613 | 4.66k | } |
7614 | 399 | } |
7615 | 1.11M | } |
7616 | 1.11M | } |
7617 | | /* }}} */ |
7618 | | |
7619 | | static zend_type zend_compile_single_typename(zend_ast *ast) |
7620 | 683k | { |
7621 | 683k | ZEND_ASSERT(!(ast->attr & ZEND_TYPE_NULLABLE)); |
7622 | 683k | if (ast->kind == ZEND_AST_TYPE) { |
7623 | 7.89k | if (ast->attr == IS_STATIC && !CG(active_class_entry) && zend_is_scope_known()) { |
7624 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7625 | 5 | "Cannot use \"static\" when no class scope is active"); |
7626 | 5 | } |
7627 | | |
7628 | 7.88k | return (zend_type) ZEND_TYPE_INIT_CODE(ast->attr, 0, 0); |
7629 | 675k | } else { |
7630 | 675k | zend_string *type_name = zend_ast_get_str(ast); |
7631 | 675k | uint8_t type_code = zend_lookup_builtin_type_by_name(type_name); |
7632 | | |
7633 | 675k | if (type_code != 0) { |
7634 | 273k | if ((ast->attr & ZEND_NAME_NOT_FQ) != ZEND_NAME_NOT_FQ) { |
7635 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7636 | 5 | "Type declaration '%s' must be unqualified", |
7637 | 5 | ZSTR_VAL(zend_string_tolower(type_name))); |
7638 | 5 | } |
7639 | | |
7640 | | /* Transform iterable into a type union alias */ |
7641 | 273k | if (type_code == IS_ITERABLE) { |
7642 | | /* Set iterable bit for BC compat during Reflection and string representation of type */ |
7643 | 241k | zend_type iterable = (zend_type) ZEND_TYPE_INIT_CLASS_MASK(ZSTR_KNOWN(ZEND_STR_TRAVERSABLE), |
7644 | 241k | (MAY_BE_ARRAY|_ZEND_TYPE_ITERABLE_BIT)); |
7645 | 241k | return iterable; |
7646 | 241k | } |
7647 | | |
7648 | 31.6k | return (zend_type) ZEND_TYPE_INIT_CODE(type_code, 0, 0); |
7649 | 402k | } else { |
7650 | 402k | const char *correct_name; |
7651 | 402k | uint32_t fetch_type = zend_get_class_fetch_type_ast(ast); |
7652 | 402k | zend_string *class_name = type_name; |
7653 | | |
7654 | 402k | if (fetch_type == ZEND_FETCH_CLASS_DEFAULT) { |
7655 | 401k | class_name = zend_resolve_class_name_ast(ast); |
7656 | 401k | zend_assert_valid_class_name(class_name, "a type name"); |
7657 | 401k | } else { |
7658 | 928 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT); |
7659 | | |
7660 | 928 | zend_ensure_valid_class_fetch_type(fetch_type); |
7661 | | |
7662 | 928 | bool substitute_self_parent = zend_is_scope_known() |
7663 | 616 | && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS); |
7664 | | |
7665 | 928 | if (fetch_type == ZEND_FETCH_CLASS_SELF) { |
7666 | | /* Scope might be unknown for unbound closures and traits */ |
7667 | 739 | if (substitute_self_parent) { |
7668 | 474 | class_name = CG(active_class_entry)->name; |
7669 | 474 | ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time"); |
7670 | 474 | } |
7671 | 739 | } else { |
7672 | 189 | ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT); |
7673 | | /* Scope might be unknown for unbound closures and traits */ |
7674 | 189 | if (substitute_self_parent) { |
7675 | 127 | class_name = CG(active_class_entry)->parent_name; |
7676 | 127 | ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time"); |
7677 | 127 | } |
7678 | 172 | } |
7679 | 928 | zend_string_addref(class_name); |
7680 | 911 | } |
7681 | | |
7682 | 402k | if (ast->attr == ZEND_NAME_NOT_FQ |
7683 | 401k | && zend_is_confusable_type(type_name, &correct_name) |
7684 | 49.4k | && zend_is_not_imported(type_name)) { |
7685 | 49.4k | const char *extra = |
7686 | 49.4k | FC(current_namespace) ? " or import the class with \"use\"" : ""; |
7687 | 49.4k | if (correct_name) { |
7688 | 16.9k | zend_error(E_COMPILE_WARNING, |
7689 | 16.9k | "\"%s\" will be interpreted as a class name. Did you mean \"%s\"? " |
7690 | 16.9k | "Write \"\\%s\"%s to suppress this warning", |
7691 | 16.9k | ZSTR_VAL(type_name), correct_name, ZSTR_VAL(class_name), extra); |
7692 | 32.5k | } else { |
7693 | 32.5k | zend_error(E_COMPILE_WARNING, |
7694 | 32.5k | "\"%s\" is not a supported builtin type " |
7695 | 32.5k | "and will be interpreted as a class name. " |
7696 | 32.5k | "Write \"\\%s\"%s to suppress this warning", |
7697 | 32.5k | ZSTR_VAL(type_name), ZSTR_VAL(class_name), extra); |
7698 | 32.5k | } |
7699 | 49.4k | } |
7700 | | |
7701 | 402k | class_name = zend_new_interned_string(class_name); |
7702 | 402k | zend_alloc_ce_cache(class_name); |
7703 | 402k | return (zend_type) ZEND_TYPE_INIT_CLASS(class_name, /* allow null */ false, 0); |
7704 | 402k | } |
7705 | 675k | } |
7706 | 683k | } |
7707 | | |
7708 | | static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type) |
7709 | 8.44k | { |
7710 | 8.44k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type)); |
7711 | 8.44k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type)); |
7712 | 8.44k | const zend_type_list *l_type_list = ZEND_TYPE_LIST(left_type); |
7713 | 8.44k | const zend_type_list *r_type_list = ZEND_TYPE_LIST(right_type); |
7714 | 8.44k | const zend_type_list *smaller_type_list, *larger_type_list; |
7715 | 8.44k | bool flipped = false; |
7716 | | |
7717 | 8.44k | if (r_type_list->num_types < l_type_list->num_types) { |
7718 | 3.14k | smaller_type_list = r_type_list; |
7719 | 3.14k | larger_type_list = l_type_list; |
7720 | 3.14k | flipped = true; |
7721 | 5.30k | } else { |
7722 | 5.30k | smaller_type_list = l_type_list; |
7723 | 5.30k | larger_type_list = r_type_list; |
7724 | 5.30k | } |
7725 | | |
7726 | 8.44k | unsigned int sum = 0; |
7727 | 8.44k | const zend_type *outer_type; |
7728 | 32.4k | ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type) { |
7729 | 32.4k | const zend_type *inner_type; |
7730 | 106k | ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type) { |
7731 | 106k | if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) { |
7732 | 8.65k | sum++; |
7733 | 8.65k | break; |
7734 | 8.65k | } |
7735 | 106k | } ZEND_TYPE_LIST_FOREACH_END(); |
7736 | 32.4k | } ZEND_TYPE_LIST_FOREACH_END(); |
7737 | | |
7738 | 8.44k | if (sum == smaller_type_list->num_types) { |
7739 | 21 | zend_string *smaller_type_str; |
7740 | 21 | zend_string *larger_type_str; |
7741 | 21 | if (flipped) { |
7742 | 5 | smaller_type_str = zend_type_to_string(right_type); |
7743 | 5 | larger_type_str = zend_type_to_string(left_type); |
7744 | 16 | } else { |
7745 | 16 | smaller_type_str = zend_type_to_string(left_type); |
7746 | 16 | larger_type_str = zend_type_to_string(right_type); |
7747 | 16 | } |
7748 | 21 | if (smaller_type_list->num_types == larger_type_list->num_types) { |
7749 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant with type %s", |
7750 | 9 | ZSTR_VAL(smaller_type_str), ZSTR_VAL(larger_type_str)); |
7751 | 12 | } else { |
7752 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7753 | 12 | ZSTR_VAL(larger_type_str), ZSTR_VAL(smaller_type_str)); |
7754 | 12 | } |
7755 | 21 | } |
7756 | 8.44k | } |
7757 | | |
7758 | | static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type) |
7759 | 8.30k | { |
7760 | 8.30k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type)); |
7761 | 8.30k | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type)); |
7762 | | |
7763 | 8.30k | const zend_type *single_intersection_type = NULL; |
7764 | 30.7k | ZEND_TYPE_FOREACH(intersection_type, single_intersection_type) { |
7765 | 30.7k | if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) { |
7766 | 9 | zend_string *single_type_str = zend_type_to_string(single_type); |
7767 | 9 | zend_string *complete_type = zend_type_to_string(intersection_type); |
7768 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Type %s is redundant as it is more restrictive than type %s", |
7769 | 9 | ZSTR_VAL(complete_type), ZSTR_VAL(single_type_str)); |
7770 | 9 | } |
7771 | 30.7k | } ZEND_TYPE_FOREACH_END(); |
7772 | 8.30k | } |
7773 | | |
7774 | | /* Used by both intersection and union types prior to transforming the type list to a full zend_type */ |
7775 | | static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type) |
7776 | 128k | { |
7777 | 128k | ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type)); |
7778 | 256k | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7779 | 128k | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7780 | 5.00k | zend_is_intersection_type_redundant_by_single_type(type_list->types[i], type); |
7781 | 5.00k | continue; |
7782 | 5.00k | } |
7783 | 123k | if (zend_string_equals_ci(ZEND_TYPE_NAME(type_list->types[i]), ZEND_TYPE_NAME(type))) { |
7784 | 47 | zend_string *single_type_str = zend_type_to_string(type); |
7785 | 47 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate type %s is redundant", ZSTR_VAL(single_type_str)); |
7786 | 47 | } |
7787 | 123k | } |
7788 | 128k | } |
7789 | | |
7790 | | static zend_type zend_compile_typename(zend_ast *ast); |
7791 | | |
7792 | | static zend_type zend_compile_typename_ex( |
7793 | | zend_ast *ast, bool force_allow_null, bool *forced_allow_null) /* {{{ */ |
7794 | 561k | { |
7795 | 561k | bool is_marked_nullable = ast->attr & ZEND_TYPE_NULLABLE; |
7796 | 561k | zend_ast_attr orig_ast_attr = ast->attr; |
7797 | 561k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
7798 | | |
7799 | 561k | if (is_marked_nullable) { |
7800 | 3.23k | ast->attr &= ~ZEND_TYPE_NULLABLE; |
7801 | 3.23k | } |
7802 | | |
7803 | 561k | if (ast->kind == ZEND_AST_TYPE_UNION) { |
7804 | 114k | const zend_ast_list *list = zend_ast_get_list(ast); |
7805 | 114k | zend_type_list *type_list; |
7806 | 114k | bool is_composite = false; |
7807 | 114k | bool has_only_iterable_class = true; |
7808 | 114k | ALLOCA_FLAG(use_heap) |
7809 | | |
7810 | 114k | type_list = do_alloca(ZEND_TYPE_LIST_SIZE(list->children), use_heap); |
7811 | 114k | type_list->num_types = 0; |
7812 | | |
7813 | 346k | for (uint32_t i = 0; i < list->children; i++) { |
7814 | 232k | zend_ast *type_ast = list->child[i]; |
7815 | 232k | zend_type single_type; |
7816 | 232k | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7817 | | |
7818 | 232k | if (type_ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7819 | 6.28k | has_only_iterable_class = false; |
7820 | 6.28k | is_composite = true; |
7821 | | /* The first class type can be stored directly as the type ptr payload. */ |
7822 | 6.28k | if (ZEND_TYPE_IS_COMPLEX(type) && !ZEND_TYPE_HAS_LIST(type)) { |
7823 | | /* Switch from single name to name list. */ |
7824 | 1.09k | type_list->num_types = 1; |
7825 | 1.09k | type_list->types[0] = type; |
7826 | | /* Clear MAY_BE_* type flags */ |
7827 | 1.09k | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7828 | 1.09k | } |
7829 | | /* Mark type as list type */ |
7830 | 6.28k | ZEND_TYPE_SET_LIST(type, type_list); |
7831 | | |
7832 | 6.28k | single_type = zend_compile_typename(type_ast); |
7833 | 6.28k | ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(single_type)); |
7834 | | |
7835 | 6.28k | type_list->types[type_list->num_types++] = single_type; |
7836 | | |
7837 | | /* Check for trivially redundant class types */ |
7838 | 18.0k | for (size_t i = 0; i < type_list->num_types - 1; i++) { |
7839 | 11.7k | if (ZEND_TYPE_IS_INTERSECTION(type_list->types[i])) { |
7840 | 8.44k | zend_are_intersection_types_redundant(single_type, type_list->types[i]); |
7841 | 8.44k | continue; |
7842 | 8.44k | } |
7843 | | /* Type from type list is a simple type */ |
7844 | 3.30k | zend_is_intersection_type_redundant_by_single_type(single_type, type_list->types[i]); |
7845 | 3.30k | } |
7846 | 6.28k | continue; |
7847 | 6.28k | } |
7848 | | |
7849 | 225k | single_type = zend_compile_single_typename(type_ast); |
7850 | 225k | uint32_t single_type_mask = ZEND_TYPE_PURE_MASK(single_type); |
7851 | | |
7852 | 225k | if (single_type_mask == MAY_BE_ANY) { |
7853 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed can only be used as a standalone type"); |
7854 | 5 | } |
7855 | 225k | if (ZEND_TYPE_IS_COMPLEX(single_type) && !ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7856 | 193k | has_only_iterable_class = false; |
7857 | 193k | } |
7858 | | |
7859 | 225k | uint32_t type_mask_overlap = ZEND_TYPE_PURE_MASK(type) & single_type_mask; |
7860 | 225k | if (type_mask_overlap) { |
7861 | 28 | zend_type overlap_type = ZEND_TYPE_INIT_MASK(type_mask_overlap); |
7862 | 28 | zend_string *overlap_type_str = zend_type_to_string(overlap_type); |
7863 | 28 | zend_error_noreturn(E_COMPILE_ERROR, |
7864 | 28 | "Duplicate type %s is redundant", ZSTR_VAL(overlap_type_str)); |
7865 | 28 | } |
7866 | | |
7867 | 225k | if ( ((type_mask & MAY_BE_TRUE) && (single_type_mask == MAY_BE_FALSE)) |
7868 | 225k | || ((type_mask & MAY_BE_FALSE) && (single_type_mask == MAY_BE_TRUE)) ) { |
7869 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
7870 | 10 | "Type contains both true and false, bool must be used instead"); |
7871 | 10 | } |
7872 | 225k | ZEND_TYPE_FULL_MASK(type) |= ZEND_TYPE_PURE_MASK(single_type); |
7873 | | /* Clear MAY_BE_* type flags */ |
7874 | 225k | ZEND_TYPE_FULL_MASK(single_type) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7875 | | |
7876 | 225k | if (ZEND_TYPE_IS_COMPLEX(single_type)) { |
7877 | 220k | if (!ZEND_TYPE_IS_COMPLEX(type) && !is_composite) { |
7878 | | /* The first class type can be stored directly as the type ptr payload. */ |
7879 | 109k | ZEND_TYPE_SET_PTR(type, ZEND_TYPE_NAME(single_type)); |
7880 | 109k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_NAME_BIT; |
7881 | 110k | } else { |
7882 | 110k | if (type_list->num_types == 0) { |
7883 | | /* Switch from single name to name list. */ |
7884 | 106k | type_list->num_types = 1; |
7885 | 106k | type_list->types[0] = type; |
7886 | | /* Clear MAY_BE_* type flags */ |
7887 | 106k | ZEND_TYPE_FULL_MASK(type_list->types[0]) &= ~_ZEND_TYPE_MAY_BE_MASK; |
7888 | 106k | ZEND_TYPE_SET_LIST(type, type_list); |
7889 | 106k | } |
7890 | | |
7891 | 110k | type_list->types[type_list->num_types++] = single_type; |
7892 | | |
7893 | | /* Check for trivially redundant class types */ |
7894 | 110k | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7895 | 110k | } |
7896 | 220k | } |
7897 | 225k | } |
7898 | | |
7899 | 114k | if (type_list->num_types) { |
7900 | 111k | zend_type_list *list = zend_arena_alloc( |
7901 | 111k | &CG(arena), ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7902 | 111k | memcpy(list, type_list, ZEND_TYPE_LIST_SIZE(type_list->num_types)); |
7903 | 111k | ZEND_TYPE_SET_LIST(type, list); |
7904 | 111k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7905 | | /* Inform that the type list is a union type */ |
7906 | 111k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7907 | 111k | } |
7908 | | |
7909 | 114k | free_alloca(type_list, use_heap); |
7910 | | |
7911 | 114k | uint32_t type_mask = ZEND_TYPE_FULL_MASK(type); |
7912 | 114k | if ((type_mask & MAY_BE_OBJECT) && |
7913 | 804 | ((!has_only_iterable_class && ZEND_TYPE_IS_COMPLEX(type)) || (type_mask & MAY_BE_STATIC))) { |
7914 | 28 | zend_string *type_str = zend_type_to_string(type); |
7915 | 28 | zend_error_noreturn(E_COMPILE_ERROR, |
7916 | 28 | "Type %s contains both object and a class type, which is redundant", |
7917 | 28 | ZSTR_VAL(type_str)); |
7918 | 28 | } |
7919 | 447k | } else if (ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
7920 | 7.83k | const zend_ast_list *list = zend_ast_get_list(ast); |
7921 | 7.83k | zend_type_list *type_list; |
7922 | | |
7923 | | /* Allocate the type list directly on the arena as it must be a type |
7924 | | * list of the same number of elements as the AST list has children */ |
7925 | 7.83k | type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(list->children)); |
7926 | 7.83k | type_list->num_types = 0; |
7927 | | |
7928 | 7.83k | ZEND_ASSERT(list->children > 1); |
7929 | | |
7930 | 25.4k | for (uint32_t i = 0; i < list->children; i++) { |
7931 | 17.7k | zend_ast *type_ast = list->child[i]; |
7932 | 17.7k | zend_type single_type = zend_compile_single_typename(type_ast); |
7933 | | |
7934 | | /* An intersection of union types cannot exist so invalidate it |
7935 | | * Currently only can happen with iterable getting canonicalized to Traversable|array */ |
7936 | 17.7k | if (ZEND_TYPE_IS_ITERABLE_FALLBACK(single_type)) { |
7937 | 5 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7938 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
7939 | 5 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7940 | 5 | } |
7941 | | /* An intersection of standard types cannot exist so invalidate it */ |
7942 | 17.7k | if (ZEND_TYPE_IS_ONLY_MASK(single_type)) { |
7943 | 64 | zend_string *standard_type_str = zend_type_to_string(single_type); |
7944 | 64 | zend_error_noreturn(E_COMPILE_ERROR, |
7945 | 64 | "Type %s cannot be part of an intersection type", ZSTR_VAL(standard_type_str)); |
7946 | 64 | } |
7947 | | /* Check for "self" and "parent" too */ |
7948 | 17.6k | if ( |
7949 | 17.6k | zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_SELF)) |
7950 | 17.6k | || zend_string_equals_ci(ZEND_TYPE_NAME(single_type), ZSTR_KNOWN(ZEND_STR_PARENT)) |
7951 | 17.6k | ) { |
7952 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
7953 | 10 | "Type %s cannot be part of an intersection type", ZSTR_VAL(ZEND_TYPE_NAME(single_type))); |
7954 | 10 | } |
7955 | | |
7956 | | /* Add type to the type list */ |
7957 | 17.6k | type_list->types[type_list->num_types++] = single_type; |
7958 | | |
7959 | | /* Check for trivially redundant class types */ |
7960 | 17.6k | zend_is_type_list_redundant_by_single_type(type_list, single_type); |
7961 | 17.6k | } |
7962 | | |
7963 | 7.76k | ZEND_ASSERT(list->children == type_list->num_types); |
7964 | | |
7965 | | /* An implicitly nullable intersection type needs to be converted to a DNF type */ |
7966 | 7.76k | if (force_allow_null) { |
7967 | 114 | zend_type intersection_type = ZEND_TYPE_INIT_NONE(0); |
7968 | 114 | ZEND_TYPE_SET_LIST(intersection_type, type_list); |
7969 | 114 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7970 | 114 | ZEND_TYPE_FULL_MASK(intersection_type) |= _ZEND_TYPE_ARENA_BIT; |
7971 | | |
7972 | 114 | zend_type_list *dnf_type_list = zend_arena_alloc(&CG(arena), ZEND_TYPE_LIST_SIZE(1)); |
7973 | 114 | dnf_type_list->num_types = 1; |
7974 | 114 | dnf_type_list->types[0] = intersection_type; |
7975 | 114 | ZEND_TYPE_SET_LIST(type, dnf_type_list); |
7976 | | /* Inform that the type list is a DNF type */ |
7977 | 114 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_UNION_BIT; |
7978 | 114 | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7979 | 7.63k | } else { |
7980 | 7.63k | ZEND_TYPE_SET_LIST(type, type_list); |
7981 | | /* Inform that the type list is an intersection type */ |
7982 | 7.63k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_INTERSECTION_BIT; |
7983 | 7.63k | ZEND_TYPE_FULL_MASK(type) |= _ZEND_TYPE_ARENA_BIT; |
7984 | 7.63k | } |
7985 | 439k | } else { |
7986 | 439k | type = zend_compile_single_typename(ast); |
7987 | 439k | } |
7988 | | |
7989 | 561k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
7990 | | |
7991 | 561k | if (type_mask == MAY_BE_ANY && is_marked_nullable) { |
7992 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Type mixed cannot be marked as nullable since mixed already includes null"); |
7993 | 14 | } |
7994 | | |
7995 | 561k | if ((type_mask & MAY_BE_NULL) && is_marked_nullable) { |
7996 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "null cannot be marked as nullable"); |
7997 | 5 | } |
7998 | | |
7999 | 561k | if (force_allow_null && !is_marked_nullable && !(type_mask & MAY_BE_NULL)) { |
8000 | 459 | *forced_allow_null = true; |
8001 | 459 | } |
8002 | | |
8003 | 561k | if (is_marked_nullable || force_allow_null) { |
8004 | 3.79k | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
8005 | 3.79k | type_mask = ZEND_TYPE_PURE_MASK(type); |
8006 | 3.79k | } |
8007 | | |
8008 | 561k | if ((type_mask & MAY_BE_VOID) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_VOID)) { |
8009 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Void can only be used as a standalone type"); |
8010 | 10 | } |
8011 | | |
8012 | 561k | if ((type_mask & MAY_BE_NEVER) && (ZEND_TYPE_IS_COMPLEX(type) || type_mask != MAY_BE_NEVER)) { |
8013 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "never can only be used as a standalone type"); |
8014 | 5 | } |
8015 | | |
8016 | 561k | ast->attr = orig_ast_attr; |
8017 | 561k | return type; |
8018 | 561k | } |
8019 | | /* }}} */ |
8020 | | |
8021 | | static zend_type zend_compile_typename(zend_ast *ast) |
8022 | 97.3k | { |
8023 | 97.3k | bool forced_allow_null; |
8024 | 97.3k | return zend_compile_typename_ex(ast, false, &forced_allow_null); |
8025 | 97.3k | } |
8026 | | |
8027 | | /* May convert value from int to float. */ |
8028 | | static bool zend_is_valid_default_value(zend_type type, zval *value) |
8029 | 5.97k | { |
8030 | 5.97k | ZEND_ASSERT(ZEND_TYPE_IS_SET(type)); |
8031 | 5.97k | if (ZEND_TYPE_CONTAINS_CODE(type, Z_TYPE_P(value))) { |
8032 | 3.85k | return true; |
8033 | 3.85k | } |
8034 | 2.12k | if ((ZEND_TYPE_FULL_MASK(type) & MAY_BE_DOUBLE) && Z_TYPE_P(value) == IS_LONG) { |
8035 | | /* Integers are allowed as initializers for floating-point values. */ |
8036 | 1.96k | convert_to_double(value); |
8037 | 1.96k | return true; |
8038 | 1.96k | } |
8039 | 158 | return false; |
8040 | 2.12k | } |
8041 | | |
8042 | | static void zend_compile_attributes( |
8043 | | HashTable **attributes, zend_ast *ast, uint32_t offset, uint32_t target, uint32_t promoted |
8044 | 1.01M | ) /* {{{ */ { |
8045 | 1.01M | zend_attribute *attr; |
8046 | 1.01M | zend_internal_attribute *config; |
8047 | | |
8048 | 1.01M | const zend_ast_list *list = zend_ast_get_list(ast); |
8049 | 1.01M | uint32_t g, i, j; |
8050 | | |
8051 | 1.01M | ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE_LIST); |
8052 | | |
8053 | 2.02M | for (g = 0; g < list->children; g++) { |
8054 | 1.01M | const zend_ast_list *group = zend_ast_get_list(list->child[g]); |
8055 | | |
8056 | 1.01M | ZEND_ASSERT(group->kind == ZEND_AST_ATTRIBUTE_GROUP); |
8057 | | |
8058 | 2.76M | for (i = 0; i < group->children; i++) { |
8059 | 1.74M | ZEND_ASSERT(group->child[i]->kind == ZEND_AST_ATTRIBUTE); |
8060 | | |
8061 | 1.74M | const zend_ast *el = group->child[i]; |
8062 | | |
8063 | 1.74M | if (el->child[1] && |
8064 | 28.4k | el->child[1]->kind == ZEND_AST_CALLABLE_CONVERT) { |
8065 | 11 | zend_error_noreturn(E_COMPILE_ERROR, |
8066 | 11 | "Cannot create Closure as attribute argument"); |
8067 | 11 | } |
8068 | | |
8069 | 1.74M | zend_string *name = zend_resolve_class_name_ast(el->child[0]); |
8070 | 1.74M | zend_string *lcname = zend_string_tolower_ex(name, false); |
8071 | 1.74M | zend_ast_list *args = el->child[1] ? zend_ast_get_list(el->child[1]) : NULL; |
8072 | | |
8073 | 1.74M | config = zend_internal_attribute_get(lcname); |
8074 | 1.74M | zend_string_release(lcname); |
8075 | | |
8076 | | /* Exclude internal attributes that do not match on promoted properties. */ |
8077 | 1.74M | if (config && !(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
8078 | 547 | if (promoted & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL)) { |
8079 | 11 | zend_string_release(name); |
8080 | 11 | continue; |
8081 | 11 | } |
8082 | 547 | } |
8083 | | |
8084 | 1.74M | uint32_t flags = (CG(active_op_array)->fn_flags & ZEND_ACC_STRICT_TYPES) |
8085 | 1.74M | ? ZEND_ATTRIBUTE_STRICT_TYPES : 0; |
8086 | 1.74M | attr = zend_add_attribute( |
8087 | 1.74M | attributes, name, args ? args->children : 0, flags, offset, el->lineno); |
8088 | 1.74M | zend_string_release(name); |
8089 | | |
8090 | | /* Populate arguments */ |
8091 | 1.74M | if (args) { |
8092 | 28.4k | ZEND_ASSERT(args->kind == ZEND_AST_ARG_LIST); |
8093 | | |
8094 | 28.4k | bool uses_named_args = false; |
8095 | 66.9k | for (j = 0; j < args->children; j++) { |
8096 | 38.5k | zend_ast **arg_ast_ptr = &args->child[j]; |
8097 | 38.5k | zend_ast *arg_ast = *arg_ast_ptr; |
8098 | | |
8099 | 38.5k | if (arg_ast->kind == ZEND_AST_UNPACK) { |
8100 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8101 | 5 | "Cannot use unpacking in attribute argument list"); |
8102 | 5 | } |
8103 | | |
8104 | 38.5k | if (arg_ast->kind == ZEND_AST_NAMED_ARG) { |
8105 | 990 | attr->args[j].name = zend_string_copy(zend_ast_get_str(arg_ast->child[0])); |
8106 | 990 | arg_ast_ptr = &arg_ast->child[1]; |
8107 | 990 | uses_named_args = true; |
8108 | | |
8109 | 5.88k | for (uint32_t k = 0; k < j; k++) { |
8110 | 4.90k | if (attr->args[k].name && |
8111 | 2.87k | zend_string_equals(attr->args[k].name, attr->args[j].name)) { |
8112 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Duplicate named parameter $%s", |
8113 | 6 | ZSTR_VAL(attr->args[j].name)); |
8114 | 6 | } |
8115 | 4.90k | } |
8116 | 37.5k | } else if (uses_named_args) { |
8117 | 30 | zend_error_noreturn(E_COMPILE_ERROR, |
8118 | 30 | "Cannot use positional argument after named argument"); |
8119 | 30 | } |
8120 | | |
8121 | 38.4k | zend_const_expr_to_zval( |
8122 | 38.4k | &attr->args[j].value, arg_ast_ptr, /* allow_dynamic */ true); |
8123 | 38.4k | } |
8124 | 28.4k | } |
8125 | 1.74M | } |
8126 | 1.01M | } |
8127 | | |
8128 | 1.01M | if (*attributes != NULL) { |
8129 | | /* Allow delaying target validation for forward compatibility. */ |
8130 | 1.01M | const zend_attribute *delayed_target_validation = NULL; |
8131 | 1.01M | if (target == ZEND_ATTRIBUTE_TARGET_PARAMETER) { |
8132 | 24.9k | ZEND_ASSERT(offset >= 1); |
8133 | | /* zend_get_parameter_attribute_str will add 1 too */ |
8134 | 24.9k | delayed_target_validation = zend_get_parameter_attribute_str( |
8135 | 24.9k | *attributes, |
8136 | 24.9k | "delayedtargetvalidation", |
8137 | 24.9k | strlen("delayedtargetvalidation"), |
8138 | 24.9k | offset - 1 |
8139 | 24.9k | ); |
8140 | 987k | } else { |
8141 | 987k | delayed_target_validation = zend_get_attribute_str( |
8142 | 987k | *attributes, |
8143 | 987k | "delayedtargetvalidation", |
8144 | 987k | strlen("delayedtargetvalidation") |
8145 | 987k | ); |
8146 | 987k | } |
8147 | | /* Validate attributes in a secondary loop (needed to detect repeated attributes). */ |
8148 | 6.20M | ZEND_HASH_PACKED_FOREACH_PTR(*attributes, attr) { |
8149 | 6.20M | if (attr->offset != offset || NULL == (config = zend_internal_attribute_get(attr->lcname))) { |
8150 | 2.08M | continue; |
8151 | 2.08M | } |
8152 | | |
8153 | 6.20M | bool run_validator = true; |
8154 | 4.49k | if (!(target & (config->flags & ZEND_ATTRIBUTE_TARGET_ALL))) { |
8155 | 352 | if (delayed_target_validation == NULL) { |
8156 | 41 | zend_string *location = zend_get_attribute_target_names(target); |
8157 | 41 | zend_string *allowed = zend_get_attribute_target_names(config->flags); |
8158 | | |
8159 | 41 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" cannot target %s (allowed targets: %s)", |
8160 | 41 | ZSTR_VAL(attr->name), ZSTR_VAL(location), ZSTR_VAL(allowed) |
8161 | 41 | ); |
8162 | 41 | } |
8163 | 311 | run_validator = false; |
8164 | 311 | } |
8165 | | |
8166 | 4.45k | if (!(config->flags & ZEND_ATTRIBUTE_IS_REPEATABLE)) { |
8167 | 4.45k | if (zend_is_attribute_repeated(*attributes, attr)) { |
8168 | 16 | zend_error_noreturn(E_ERROR, "Attribute \"%s\" must not be repeated", ZSTR_VAL(attr->name)); |
8169 | 16 | } |
8170 | 4.45k | } |
8171 | | |
8172 | | /* Validators are not run if the target is already invalid */ |
8173 | 4.44k | if (run_validator && config->validator != NULL) { |
8174 | 2.56k | zend_string *error = config->validator(attr, target, CG(active_class_entry)); |
8175 | 2.56k | if (error != NULL) { |
8176 | 333 | if (delayed_target_validation == NULL) { |
8177 | 81 | zend_error_noreturn(E_COMPILE_ERROR, "%s", ZSTR_VAL(error)); |
8178 | 252 | } else { |
8179 | 252 | attr->validation_error = error; |
8180 | 252 | } |
8181 | 333 | } |
8182 | 2.56k | } |
8183 | 4.44k | } ZEND_HASH_FOREACH_END(); |
8184 | 1.01M | } |
8185 | 1.01M | } |
8186 | | /* }}} */ |
8187 | | |
8188 | | static void zend_compile_property_hooks( |
8189 | | zend_property_info *prop_info, zend_string *prop_name, |
8190 | | zend_ast *prop_type_ast, const zend_ast_list *hooks); |
8191 | | |
8192 | | typedef struct { |
8193 | | const zend_string *property_name; |
8194 | | bool uses_property; |
8195 | | } find_property_usage_context; |
8196 | | |
8197 | | static void zend_property_hook_find_property_usage(zend_ast **ast_ptr, void *_context) /* {{{ */ |
8198 | 47.1k | { |
8199 | 47.1k | zend_ast *ast = *ast_ptr; |
8200 | 47.1k | find_property_usage_context *context = (find_property_usage_context *) _context; |
8201 | | |
8202 | 47.1k | if (ast == NULL) { |
8203 | 1.52k | return; |
8204 | 45.6k | } else if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) { |
8205 | 3.12k | const zend_ast *object_ast = ast->child[0]; |
8206 | 3.12k | zend_ast *property_ast = ast->child[1]; |
8207 | | |
8208 | 3.12k | if (object_ast->kind == ZEND_AST_VAR |
8209 | 2.88k | && object_ast->child[0]->kind == ZEND_AST_ZVAL |
8210 | 2.74k | && property_ast->kind == ZEND_AST_ZVAL) { |
8211 | 2.66k | const zval *object = zend_ast_get_zval(object_ast->child[0]); |
8212 | 2.66k | const zval *property = zend_ast_get_zval(property_ast); |
8213 | 2.66k | if (Z_TYPE_P(object) == IS_STRING |
8214 | 2.66k | && Z_TYPE_P(property) == IS_STRING |
8215 | 2.66k | && zend_string_equals_literal(Z_STR_P(object), "this") |
8216 | 2.23k | && zend_string_equals(Z_STR_P(property), context->property_name)) { |
8217 | 996 | context->uses_property = true; |
8218 | | /* No need to look for references in this branch. */ |
8219 | 996 | return; |
8220 | 996 | } |
8221 | 2.66k | } |
8222 | 3.12k | } |
8223 | | |
8224 | | /* Don't search across function/class boundaries. */ |
8225 | 44.6k | if (!zend_ast_is_special(ast)) { |
8226 | 28.4k | zend_ast_apply(ast, zend_property_hook_find_property_usage, context); |
8227 | 28.4k | } |
8228 | 44.6k | } |
8229 | | |
8230 | | static bool zend_property_hook_uses_property(const zend_string *property_name, const zend_string *hook_name, zend_ast *hook_ast) |
8231 | 5.01k | { |
8232 | 5.01k | if (zend_string_equals_literal_ci(hook_name, "set") |
8233 | 2.14k | && hook_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
8234 | 621 | return true; |
8235 | 621 | } |
8236 | | |
8237 | 4.38k | find_property_usage_context context = { property_name, false }; |
8238 | 4.38k | zend_property_hook_find_property_usage(&hook_ast, &context); |
8239 | 4.38k | return context.uses_property; |
8240 | 5.01k | } |
8241 | | |
8242 | | static bool zend_property_is_virtual(const zend_class_entry *ce, const zend_string *property_name, zend_ast *hooks_ast) |
8243 | 32.7k | { |
8244 | 32.7k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
8245 | 249 | return true; |
8246 | 249 | } |
8247 | 32.5k | if (!hooks_ast) { |
8248 | 28.4k | return false; |
8249 | 28.4k | } |
8250 | | |
8251 | 32.5k | bool is_virtual = true; |
8252 | | |
8253 | 4.10k | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
8254 | 9.65k | for (uint32_t i = 0; i < hooks->children; i++) { |
8255 | 5.54k | const zend_ast_decl *hook = (const zend_ast_decl *) hooks->child[i]; |
8256 | 5.54k | zend_ast *body = hook->child[2]; |
8257 | 5.54k | if (body && zend_property_hook_uses_property(property_name, hook->name, body)) { |
8258 | 1.54k | is_virtual = false; |
8259 | 1.54k | } |
8260 | 5.54k | } |
8261 | | |
8262 | 4.10k | return is_virtual; |
8263 | 32.5k | } |
8264 | | |
8265 | | static void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fallback_return_type) /* {{{ */ |
8266 | 1.47M | { |
8267 | 1.47M | zend_ast_list *list = zend_ast_get_list(ast); |
8268 | 1.47M | uint32_t i; |
8269 | 1.47M | zend_op_array *op_array = CG(active_op_array); |
8270 | 1.47M | zend_arg_info *arg_infos; |
8271 | | |
8272 | 1.47M | if (return_type_ast || fallback_return_type) { |
8273 | | /* Use op_array->arg_info[-1] for return type */ |
8274 | 76.6k | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children + 1, 0); |
8275 | 76.6k | arg_infos->name = NULL; |
8276 | 76.6k | if (return_type_ast) { |
8277 | 70.6k | arg_infos->type = zend_compile_typename(return_type_ast); |
8278 | 70.6k | ZEND_TYPE_FULL_MASK(arg_infos->type) |= _ZEND_ARG_INFO_FLAGS( |
8279 | 70.6k | (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0, /* is_variadic */ 0, /* is_tentative */ 0); |
8280 | 70.6k | } else { |
8281 | 6.08k | arg_infos->type = (zend_type) ZEND_TYPE_INIT_CODE(fallback_return_type, 0, 0); |
8282 | 6.08k | } |
8283 | 76.6k | arg_infos->doc_comment = NULL; |
8284 | 76.6k | arg_infos++; |
8285 | 76.6k | op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE; |
8286 | | |
8287 | 76.6k | if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID) |
8288 | 4.38k | && (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) { |
8289 | 353 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8290 | 353 | zend_error(E_DEPRECATED, "%s(): Returning by reference from a void function is deprecated", ZSTR_VAL(func_name)); |
8291 | 353 | zend_string_release(func_name); |
8292 | 353 | } |
8293 | 1.40M | } else { |
8294 | 1.40M | if (list->children == 0) { |
8295 | 388k | return; |
8296 | 388k | } |
8297 | 1.01M | arg_infos = safe_emalloc(sizeof(zend_arg_info), list->children, 0); |
8298 | 1.01M | } |
8299 | | |
8300 | | /* Find last required parameter number for deprecation message. */ |
8301 | 1.08M | uint32_t last_required_param = (uint32_t) -1; |
8302 | 2.20M | for (i = 0; i < list->children; ++i) { |
8303 | 1.11M | zend_ast *param_ast = list->child[i]; |
8304 | 1.11M | zend_ast *default_ast_ptr = param_ast->child[2]; |
8305 | 1.11M | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8306 | 1.11M | if (!default_ast_ptr && !is_variadic) { |
8307 | 1.10M | last_required_param = i; |
8308 | 1.10M | } |
8309 | 1.11M | } |
8310 | | |
8311 | 1.08M | const uint32_t promotion_flags = ZEND_ACC_PPP_MASK | ZEND_ACC_PPP_SET_MASK | ZEND_ACC_READONLY | ZEND_ACC_FINAL; |
8312 | 2.20M | for (i = 0; i < list->children; ++i) { |
8313 | 1.11M | zend_ast *param_ast = list->child[i]; |
8314 | 1.11M | zend_ast *type_ast = param_ast->child[0]; |
8315 | 1.11M | zend_ast *var_ast = param_ast->child[1]; |
8316 | 1.11M | zend_ast **default_ast_ptr = ¶m_ast->child[2]; |
8317 | 1.11M | zend_ast *attributes_ast = param_ast->child[3]; |
8318 | 1.11M | zend_ast *doc_comment_ast = param_ast->child[4]; |
8319 | 1.11M | zend_ast *hooks_ast = param_ast->child[5]; |
8320 | 1.11M | zend_string *name = zval_make_interned_string(zend_ast_get_zval(var_ast)); |
8321 | 1.11M | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8322 | 1.11M | bool is_variadic = (param_ast->attr & ZEND_PARAM_VARIADIC) != 0; |
8323 | 1.11M | uint32_t property_flags = param_ast->attr & promotion_flags; |
8324 | 1.11M | bool is_promoted = property_flags || hooks_ast; |
8325 | | |
8326 | 1.11M | CG(zend_lineno) = param_ast->lineno; |
8327 | | |
8328 | 1.11M | znode var_node, default_node; |
8329 | 1.11M | uint8_t opcode; |
8330 | 1.11M | zend_op *opline; |
8331 | 1.11M | zend_arg_info *arg_info; |
8332 | | |
8333 | 1.11M | if (zend_is_auto_global(name)) { |
8334 | 3 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign auto-global variable %s", |
8335 | 3 | ZSTR_VAL(name)); |
8336 | 3 | } |
8337 | | |
8338 | 1.11M | var_node.op_type = IS_CV; |
8339 | 1.11M | var_node.u.op.var = lookup_cv(name); |
8340 | | |
8341 | 1.11M | if (EX_VAR_TO_NUM(var_node.u.op.var) != i) { |
8342 | 29 | zend_error_noreturn(E_COMPILE_ERROR, "Redefinition of parameter $%s", |
8343 | 29 | ZSTR_VAL(name)); |
8344 | 1.11M | } else if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8345 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as parameter"); |
8346 | 1.11M | } else if (zend_string_equals_literal(name, "http_response_header")) { |
8347 | 50 | CG(context).has_assigned_to_http_response_header = true; |
8348 | 50 | } |
8349 | | |
8350 | 1.11M | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8351 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Only the last parameter can be variadic"); |
8352 | 6 | } |
8353 | | |
8354 | 1.11M | if (is_variadic) { |
8355 | 1.24k | opcode = ZEND_RECV_VARIADIC; |
8356 | 1.24k | default_node.op_type = IS_UNUSED; |
8357 | 1.24k | op_array->fn_flags |= ZEND_ACC_VARIADIC; |
8358 | | |
8359 | 1.24k | if (*default_ast_ptr) { |
8360 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8361 | 5 | "Variadic parameter cannot have a default value"); |
8362 | 5 | } |
8363 | 1.11M | } else if (*default_ast_ptr) { |
8364 | | /* we cannot substitute constants here or it will break ReflectionParameter::getDefaultValueConstantName() and ReflectionParameter::isDefaultValueConstant() */ |
8365 | 8.26k | uint32_t cops = CG(compiler_options); |
8366 | 8.26k | CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION; |
8367 | 8.26k | opcode = ZEND_RECV_INIT; |
8368 | 8.26k | default_node.op_type = IS_CONST; |
8369 | 8.26k | zend_const_expr_to_zval( |
8370 | 8.26k | &default_node.u.constant, default_ast_ptr, /* allow_dynamic */ true); |
8371 | 8.26k | CG(compiler_options) = cops; |
8372 | 1.10M | } else { |
8373 | 1.10M | opcode = ZEND_RECV; |
8374 | 1.10M | default_node.op_type = IS_UNUSED; |
8375 | 1.10M | op_array->required_num_args = i + 1; |
8376 | 1.10M | } |
8377 | | |
8378 | 1.11M | arg_info = &arg_infos[i]; |
8379 | 1.11M | arg_info->name = zend_string_copy(name); |
8380 | 1.11M | arg_info->type = (zend_type) ZEND_TYPE_INIT_NONE(0); |
8381 | 1.11M | arg_info->default_value = NULL; |
8382 | 1.11M | arg_info->doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8383 | | |
8384 | 1.11M | if (attributes_ast) { |
8385 | 24.9k | zend_compile_attributes( |
8386 | 24.9k | &op_array->attributes, attributes_ast, i + 1, ZEND_ATTRIBUTE_TARGET_PARAMETER, |
8387 | 24.9k | is_promoted ? ZEND_ATTRIBUTE_TARGET_PROPERTY : 0 |
8388 | 24.9k | ); |
8389 | 24.9k | } |
8390 | | |
8391 | 1.11M | bool forced_allow_nullable = false; |
8392 | 1.11M | if (type_ast) { |
8393 | 464k | uint32_t default_type = *default_ast_ptr ? Z_TYPE(default_node.u.constant) : IS_UNDEF; |
8394 | 464k | bool force_nullable = default_type == IS_NULL && !is_promoted; |
8395 | | |
8396 | 464k | op_array->fn_flags |= ZEND_ACC_HAS_TYPE_HINTS; |
8397 | 464k | arg_info->type = zend_compile_typename_ex(type_ast, force_nullable, &forced_allow_nullable); |
8398 | 464k | if (forced_allow_nullable) { |
8399 | 459 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8400 | 459 | zend_error(E_DEPRECATED, |
8401 | 459 | "%s(): Implicitly marking parameter $%s as nullable is deprecated, the explicit nullable type " |
8402 | 459 | "must be used instead", ZSTR_VAL(func_name), ZSTR_VAL(name)); |
8403 | 459 | zend_string_release(func_name); |
8404 | 459 | } |
8405 | | |
8406 | 464k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_VOID) { |
8407 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "void cannot be used as a parameter type"); |
8408 | 6 | } |
8409 | | |
8410 | 464k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_NEVER) { |
8411 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "never cannot be used as a parameter type"); |
8412 | 5 | } |
8413 | | |
8414 | 464k | if (default_type != IS_UNDEF && default_type != IS_CONSTANT_AST && !force_nullable |
8415 | 1.29k | && !zend_is_valid_default_value(arg_info->type, &default_node.u.constant)) { |
8416 | 37 | zend_string *type_str = zend_type_to_string(arg_info->type); |
8417 | 37 | zend_error_noreturn(E_COMPILE_ERROR, |
8418 | 37 | "Cannot use %s as default value for parameter $%s of type %s", |
8419 | 37 | zend_get_type_by_const(default_type), |
8420 | 37 | ZSTR_VAL(name), ZSTR_VAL(type_str)); |
8421 | 37 | } |
8422 | 464k | } |
8423 | 1.11M | if (last_required_param != (uint32_t) -1 |
8424 | 1.10M | && i < last_required_param |
8425 | 35.9k | && default_node.op_type == IS_CONST) { |
8426 | | /* Ignore parameters of the form "Type $param = null". |
8427 | | * This is the PHP 5 style way of writing "?Type $param", so allow it for now. */ |
8428 | 729 | if (!forced_allow_nullable) { |
8429 | 560 | zend_string *func_name = get_function_or_method_name((zend_function *) op_array); |
8430 | 560 | zend_ast *required_param_ast = list->child[last_required_param]; |
8431 | 560 | zend_error(E_DEPRECATED, |
8432 | 560 | "%s(): Optional parameter $%s declared before required parameter $%s " |
8433 | 560 | "is implicitly treated as a required parameter", |
8434 | 560 | ZSTR_VAL(func_name), ZSTR_VAL(name), ZSTR_VAL(zend_ast_get_str(required_param_ast->child[1]))); |
8435 | 560 | zend_string_release(func_name); |
8436 | 560 | } |
8437 | | |
8438 | | /* Regardless of whether we issue a deprecation, convert this parameter into |
8439 | | * a required parameter without a default value. This ensures that it cannot be |
8440 | | * used as an optional parameter even with named parameters. */ |
8441 | 729 | opcode = ZEND_RECV; |
8442 | 729 | default_node.op_type = IS_UNUSED; |
8443 | 729 | zval_ptr_dtor(&default_node.u.constant); |
8444 | 729 | } |
8445 | | |
8446 | 1.11M | opline = zend_emit_op(NULL, opcode, NULL, &default_node); |
8447 | 1.11M | SET_NODE(opline->result, &var_node); |
8448 | 1.11M | opline->op1.num = i + 1; |
8449 | | |
8450 | 1.11M | uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic, /* is_tentative */ 0) |
8451 | 1.11M | | (is_promoted ? _ZEND_IS_PROMOTED_BIT : 0); |
8452 | 1.11M | ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags; |
8453 | 1.11M | if (opcode == ZEND_RECV) { |
8454 | 1.10M | opline->op2.num = type_ast ? |
8455 | 642k | ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY; |
8456 | 1.10M | } |
8457 | | |
8458 | 1.11M | if (is_promoted) { |
8459 | 1.43k | const zend_op_array *active_op_array = CG(active_op_array); |
8460 | 1.43k | zend_class_entry *scope = active_op_array->scope; |
8461 | | |
8462 | 1.43k | bool is_ctor = |
8463 | 1.43k | scope && zend_is_constructor(active_op_array->function_name); |
8464 | 1.43k | if (!is_ctor) { |
8465 | 32 | zend_error_noreturn(E_COMPILE_ERROR, |
8466 | 32 | "Cannot declare promoted property outside a constructor"); |
8467 | 32 | } |
8468 | 1.40k | if ((active_op_array->fn_flags & ZEND_ACC_ABSTRACT) |
8469 | 1.40k | || (scope->ce_flags & ZEND_ACC_INTERFACE)) { |
8470 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8471 | 5 | "Cannot declare promoted property in an abstract constructor"); |
8472 | 5 | } |
8473 | 1.40k | if (is_variadic) { |
8474 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8475 | 5 | "Cannot declare variadic promoted property"); |
8476 | 5 | } |
8477 | 1.39k | if (zend_hash_exists(&scope->properties_info, name)) { |
8478 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
8479 | 5 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8480 | 5 | } |
8481 | 1.39k | if (ZEND_TYPE_FULL_MASK(arg_info->type) & MAY_BE_CALLABLE) { |
8482 | 5 | zend_string *str = zend_type_to_string(arg_info->type); |
8483 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
8484 | 5 | "Property %s::$%s cannot have type %s", |
8485 | 5 | ZSTR_VAL(scope->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
8486 | 5 | } |
8487 | | |
8488 | 1.38k | if (!(property_flags & ZEND_ACC_READONLY) && (scope->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
8489 | 21 | property_flags |= ZEND_ACC_READONLY; |
8490 | 21 | } |
8491 | | |
8492 | | /* Recompile the type, as it has different memory management requirements. */ |
8493 | 1.38k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
8494 | 1.38k | if (type_ast) { |
8495 | 1.11k | type = zend_compile_typename(type_ast); |
8496 | 1.11k | } |
8497 | | |
8498 | | /* Don't give the property an explicit default value. For typed properties this means |
8499 | | * uninitialized, for untyped properties it means an implicit null default value. |
8500 | | * Properties with hooks get an implicit default value of undefined until inheritance, |
8501 | | * where it is changed to null only once we know it is not virtual. If we were to set it |
8502 | | * here, we couldn't verify that a true virtual property must not have an explicit |
8503 | | * default value. */ |
8504 | 1.38k | zval default_value; |
8505 | 1.38k | if (ZEND_TYPE_IS_SET(type) || hooks_ast) { |
8506 | 1.15k | ZVAL_UNDEF(&default_value); |
8507 | 1.15k | } else { |
8508 | 229 | if (property_flags & ZEND_ACC_READONLY) { |
8509 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
8510 | 10 | ZSTR_VAL(scope->name), ZSTR_VAL(name)); |
8511 | 10 | } |
8512 | | |
8513 | 219 | ZVAL_NULL(&default_value); |
8514 | 219 | } |
8515 | | |
8516 | 1.37k | zend_string *doc_comment = |
8517 | 1.37k | doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
8518 | 1.37k | zend_property_info *prop = zend_declare_typed_property( |
8519 | 1.37k | scope, name, &default_value, |
8520 | 1.37k | property_flags | (zend_property_is_virtual(scope, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0) | ZEND_ACC_PROMOTED, |
8521 | 1.37k | doc_comment, type); |
8522 | 1.37k | if (hooks_ast) { |
8523 | 117 | const zend_ast_list *hooks = zend_ast_get_list(hooks_ast); |
8524 | 117 | zend_compile_property_hooks(prop, name, type_ast, hooks); |
8525 | 117 | } |
8526 | 1.37k | if (attributes_ast) { |
8527 | 70 | zend_compile_attributes( |
8528 | 70 | &prop->attributes, attributes_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, ZEND_ATTRIBUTE_TARGET_PARAMETER); |
8529 | | |
8530 | 70 | zend_attribute *override_attribute = zend_get_attribute_str(prop->attributes, "override", sizeof("override")-1); |
8531 | 70 | if (override_attribute) { |
8532 | 8 | prop->flags |= ZEND_ACC_OVERRIDE; |
8533 | 8 | } |
8534 | 70 | } |
8535 | 1.37k | } |
8536 | 1.11M | } |
8537 | | |
8538 | | /* These are assigned at the end to avoid uninitialized memory in case of an error */ |
8539 | 1.08M | op_array->num_args = list->children; |
8540 | 1.08M | op_array->arg_info = arg_infos; |
8541 | | |
8542 | | /* Don't count the variadic argument */ |
8543 | 1.08M | if (op_array->fn_flags & ZEND_ACC_VARIADIC) { |
8544 | 1.22k | op_array->num_args--; |
8545 | 1.22k | } |
8546 | 1.08M | zend_set_function_arg_flags((zend_function*)op_array); |
8547 | | |
8548 | 2.20M | for (i = 0; i < list->children; i++) { |
8549 | 1.11M | zend_ast *param_ast = list->child[i]; |
8550 | 1.11M | zend_ast *hooks_ast = param_ast->child[5]; |
8551 | 1.11M | bool is_ref = (param_ast->attr & ZEND_PARAM_REF) != 0; |
8552 | 1.11M | uint32_t flags = param_ast->attr & promotion_flags; |
8553 | 1.11M | bool is_promoted = flags || hooks_ast; |
8554 | 1.11M | if (!is_promoted) { |
8555 | 1.11M | continue; |
8556 | 1.11M | } |
8557 | | |
8558 | 1.35k | CG(zend_lineno) = param_ast->lineno; |
8559 | | |
8560 | | /* Emit $this->prop = $prop for promoted properties. */ |
8561 | 1.35k | zend_string *name = zend_ast_get_str(param_ast->child[1]); |
8562 | 1.35k | znode name_node, value_node; |
8563 | 1.35k | name_node.op_type = IS_CONST; |
8564 | 1.35k | ZVAL_STR_COPY(&name_node.u.constant, name); |
8565 | 1.35k | value_node.op_type = IS_CV; |
8566 | 1.35k | value_node.u.op.var = lookup_cv(name); |
8567 | | |
8568 | 1.35k | zend_op *opline = zend_emit_op(NULL, |
8569 | 1.35k | is_ref ? ZEND_ASSIGN_OBJ_REF : ZEND_ASSIGN_OBJ, NULL, &name_node); |
8570 | 1.35k | opline->extended_value = zend_alloc_cache_slots(3); |
8571 | 1.35k | zend_emit_op_data(&value_node); |
8572 | 1.35k | } |
8573 | 1.08M | } |
8574 | | /* }}} */ |
8575 | | |
8576 | | static void zend_compile_closure_binding(znode *closure, zend_op_array *op_array, zend_ast *uses_ast) /* {{{ */ |
8577 | 3.79k | { |
8578 | 3.79k | const zend_ast_list *list = zend_ast_get_list(uses_ast); |
8579 | 3.79k | uint32_t i; |
8580 | | |
8581 | 3.79k | if (!list->children) { |
8582 | 562 | return; |
8583 | 562 | } |
8584 | | |
8585 | 3.23k | if (!op_array->static_variables) { |
8586 | 3.23k | op_array->static_variables = zend_new_array(8); |
8587 | 3.23k | } |
8588 | | |
8589 | 10.8k | for (i = 0; i < list->children; ++i) { |
8590 | 7.66k | zend_ast *var_name_ast = list->child[i]; |
8591 | 7.66k | zend_string *var_name = zval_make_interned_string(zend_ast_get_zval(var_name_ast)); |
8592 | 7.66k | uint32_t mode = var_name_ast->attr; |
8593 | 7.66k | zend_op *opline; |
8594 | 7.66k | zval *value; |
8595 | | |
8596 | 7.66k | if (zend_string_equals(var_name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8597 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use $this as lexical variable"); |
8598 | 5 | } |
8599 | | |
8600 | 7.65k | if (zend_is_auto_global(var_name)) { |
8601 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable"); |
8602 | 5 | } |
8603 | | |
8604 | 7.65k | value = zend_hash_add(op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8605 | 7.65k | if (!value) { |
8606 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
8607 | 7 | "Cannot use variable $%pS twice", var_name); |
8608 | 7 | } |
8609 | | |
8610 | 7.64k | CG(zend_lineno) = zend_ast_get_lineno(var_name_ast); |
8611 | | |
8612 | 7.64k | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8613 | 7.64k | opline->op2_type = IS_CV; |
8614 | 7.64k | opline->op2.var = lookup_cv(var_name); |
8615 | 7.64k | opline->extended_value = |
8616 | 7.64k | (uint32_t)((char*)value - (char*)op_array->static_variables->arData) | mode; |
8617 | 7.64k | } |
8618 | 3.23k | } |
8619 | | /* }}} */ |
8620 | | |
8621 | | typedef struct { |
8622 | | HashTable uses; |
8623 | | bool varvars_used; |
8624 | | } closure_info; |
8625 | | |
8626 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast); |
8627 | | |
8628 | 2.67M | static void find_implicit_binds_recursively(closure_info *info, zend_ast *ast) { |
8629 | 2.67M | if (!ast) { |
8630 | 127k | return; |
8631 | 127k | } |
8632 | | |
8633 | 2.54M | if (ast->kind == ZEND_AST_VAR) { |
8634 | 141k | zend_ast *name_ast = ast->child[0]; |
8635 | 141k | if (name_ast->kind == ZEND_AST_ZVAL && Z_TYPE_P(zend_ast_get_zval(name_ast)) == IS_STRING) { |
8636 | 138k | zend_string *name = zend_ast_get_str(name_ast); |
8637 | 138k | if (zend_is_auto_global(name)) { |
8638 | | /* These is no need to explicitly import auto-globals. */ |
8639 | 2.71k | return; |
8640 | 2.71k | } |
8641 | | |
8642 | 136k | if (zend_string_equals(name, ZSTR_KNOWN(ZEND_STR_THIS))) { |
8643 | | /* $this does not need to be explicitly imported. */ |
8644 | 11.8k | return; |
8645 | 11.8k | } |
8646 | | |
8647 | 124k | zend_hash_add_empty_element(&info->uses, name); |
8648 | 124k | } else { |
8649 | 2.76k | info->varvars_used = true; |
8650 | 2.76k | find_implicit_binds_recursively(info, name_ast); |
8651 | 2.76k | } |
8652 | 2.40M | } else if (zend_ast_is_list(ast)) { |
8653 | 254k | const zend_ast_list *list = zend_ast_get_list(ast); |
8654 | 254k | uint32_t i; |
8655 | 667k | for (i = 0; i < list->children; i++) { |
8656 | 413k | find_implicit_binds_recursively(info, list->child[i]); |
8657 | 413k | } |
8658 | 2.15M | } else if (ast->kind == ZEND_AST_CLOSURE) { |
8659 | | /* For normal closures add the use() list. */ |
8660 | 171 | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8661 | 171 | zend_ast *uses_ast = closure_ast->child[1]; |
8662 | 171 | if (uses_ast) { |
8663 | 93 | const zend_ast_list *uses_list = zend_ast_get_list(uses_ast); |
8664 | 93 | uint32_t i; |
8665 | 665 | for (i = 0; i < uses_list->children; i++) { |
8666 | 572 | zend_hash_add_empty_element(&info->uses, zend_ast_get_str(uses_list->child[i])); |
8667 | 572 | } |
8668 | 93 | } |
8669 | 2.15M | } else if (ast->kind == ZEND_AST_ARROW_FUNC) { |
8670 | | /* For arrow functions recursively check the expression. */ |
8671 | 325k | const zend_ast_decl *closure_ast = (const zend_ast_decl *) ast; |
8672 | 325k | closure_info inner_info; |
8673 | 325k | find_implicit_binds(&inner_info, closure_ast->child[0], closure_ast->child[2]); |
8674 | 325k | if (inner_info.varvars_used) { |
8675 | 7.01k | info->varvars_used = true; |
8676 | 7.01k | } |
8677 | 325k | if (zend_hash_num_elements(&inner_info.uses)) { |
8678 | 300k | zend_hash_copy(&info->uses, &inner_info.uses, NULL); |
8679 | 300k | } |
8680 | 325k | zend_hash_destroy(&inner_info.uses); |
8681 | 1.82M | } else if (!zend_ast_is_special(ast)) { |
8682 | 1.21M | uint32_t i, children = zend_ast_get_num_children(ast); |
8683 | 2.89M | for (i = 0; i < children; i++) { |
8684 | 1.68M | find_implicit_binds_recursively(info, ast->child[i]); |
8685 | 1.68M | } |
8686 | 1.21M | } |
8687 | 2.54M | } |
8688 | | |
8689 | | static void find_implicit_binds(closure_info *info, zend_ast *params_ast, zend_ast *stmt_ast) |
8690 | 577k | { |
8691 | 577k | const zend_ast_list *param_list = zend_ast_get_list(params_ast); |
8692 | 577k | uint32_t i; |
8693 | | |
8694 | 577k | zend_hash_init(&info->uses, param_list->children, NULL, NULL, 0); |
8695 | 577k | info->varvars_used = false; |
8696 | | |
8697 | 577k | find_implicit_binds_recursively(info, stmt_ast); |
8698 | | |
8699 | | /* Remove variables that are parameters */ |
8700 | 623k | for (i = 0; i < param_list->children; i++) { |
8701 | 45.8k | const zend_ast *param_ast = param_list->child[i]; |
8702 | 45.8k | zend_hash_del(&info->uses, zend_ast_get_str(param_ast->child[1])); |
8703 | 45.8k | } |
8704 | 577k | } |
8705 | | |
8706 | | static void compile_implicit_lexical_binds( |
8707 | | const closure_info *info, znode *closure, zend_op_array *op_array) |
8708 | 251k | { |
8709 | 251k | zend_string *var_name; |
8710 | 251k | zend_op *opline; |
8711 | | |
8712 | | /* TODO We might want to use a special binding mode if varvars_used is set. */ |
8713 | 251k | if (zend_hash_num_elements(&info->uses) == 0) { |
8714 | 244k | return; |
8715 | 244k | } |
8716 | | |
8717 | 7.52k | if (!op_array->static_variables) { |
8718 | 7.52k | op_array->static_variables = zend_new_array(8); |
8719 | 7.52k | } |
8720 | | |
8721 | 113k | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8722 | 113k | zval *value = zend_hash_add( |
8723 | 113k | op_array->static_variables, var_name, &EG(uninitialized_zval)); |
8724 | 113k | uint32_t offset = (uint32_t)((char*)value - (char*)op_array->static_variables->arData); |
8725 | | |
8726 | 113k | opline = zend_emit_op(NULL, ZEND_BIND_LEXICAL, closure, NULL); |
8727 | 113k | opline->op2_type = IS_CV; |
8728 | 113k | opline->op2.var = lookup_cv(var_name); |
8729 | 113k | opline->extended_value = offset | ZEND_BIND_IMPLICIT; |
8730 | 113k | } ZEND_HASH_FOREACH_END(); |
8731 | 7.52k | } |
8732 | | |
8733 | | static void zend_compile_closure_uses(zend_ast *ast) /* {{{ */ |
8734 | 3.77k | { |
8735 | 3.77k | const zend_op_array *op_array = CG(active_op_array); |
8736 | 3.77k | const zend_ast_list *list = zend_ast_get_list(ast); |
8737 | 3.77k | uint32_t i; |
8738 | | |
8739 | 11.3k | for (i = 0; i < list->children; ++i) { |
8740 | 7.62k | uint32_t mode = ZEND_BIND_EXPLICIT; |
8741 | 7.62k | zend_ast *var_ast = list->child[i]; |
8742 | 7.62k | zend_string *var_name = zend_ast_get_str(var_ast); |
8743 | 7.62k | zval zv; |
8744 | 7.62k | ZVAL_NULL(&zv); |
8745 | | |
8746 | 7.62k | { |
8747 | 7.62k | int i; |
8748 | 20.8k | for (i = 0; i < op_array->last_var; i++) { |
8749 | 13.2k | if (zend_string_equals(op_array->vars[i], var_name)) { |
8750 | 5 | zend_error_noreturn_unchecked(E_COMPILE_ERROR, |
8751 | 5 | "Cannot use lexical variable $%S as a parameter name", var_name); |
8752 | 5 | } |
8753 | 13.2k | } |
8754 | 7.62k | } |
8755 | | |
8756 | 7.62k | CG(zend_lineno) = zend_ast_get_lineno(var_ast); |
8757 | | |
8758 | 7.62k | if (var_ast->attr) { |
8759 | 5.34k | mode |= ZEND_BIND_REF; |
8760 | 5.34k | } |
8761 | | |
8762 | 7.62k | zend_compile_static_var_common(var_name, &zv, mode); |
8763 | 7.62k | } |
8764 | 3.77k | } |
8765 | | /* }}} */ |
8766 | | |
8767 | | static void zend_compile_implicit_closure_uses(const closure_info *info) |
8768 | 251k | { |
8769 | 251k | zend_string *var_name; |
8770 | 601k | ZEND_HASH_MAP_FOREACH_STR_KEY(&info->uses, var_name) { |
8771 | 601k | zval zv; |
8772 | 601k | ZVAL_NULL(&zv); |
8773 | 601k | zend_compile_static_var_common(var_name, &zv, ZEND_BIND_IMPLICIT); |
8774 | 601k | } ZEND_HASH_FOREACH_END(); |
8775 | 251k | } |
8776 | | |
8777 | 6.12k | static void add_stringable_interface(zend_class_entry *ce) { |
8778 | 16.6k | for (uint32_t i = 0; i < ce->num_interfaces; i++) { |
8779 | 10.5k | if (zend_string_equals_literal(ce->interface_names[i].lc_name, "stringable")) { |
8780 | | /* Interface already explicitly implemented */ |
8781 | 50 | return; |
8782 | 50 | } |
8783 | 10.5k | } |
8784 | | |
8785 | 6.07k | ce->num_interfaces++; |
8786 | 6.07k | ce->interface_names = |
8787 | 6.07k | erealloc(ce->interface_names, sizeof(zend_class_name) * ce->num_interfaces); |
8788 | | // TODO: Add known interned strings instead? |
8789 | 6.07k | ce->interface_names[ce->num_interfaces - 1].name = |
8790 | 6.07k | ZSTR_INIT_LITERAL("Stringable", 0); |
8791 | 6.07k | ce->interface_names[ce->num_interfaces - 1].lc_name = |
8792 | 6.07k | ZSTR_INIT_LITERAL("stringable", 0); |
8793 | 6.07k | } |
8794 | | |
8795 | | static zend_string *zend_begin_method_decl(zend_op_array *op_array, zend_string *name, bool has_body) /* {{{ */ |
8796 | 98.4k | { |
8797 | 98.4k | zend_class_entry *ce = CG(active_class_entry); |
8798 | 98.4k | bool in_interface = (ce->ce_flags & ZEND_ACC_INTERFACE) != 0; |
8799 | 98.4k | uint32_t fn_flags = op_array->fn_flags; |
8800 | | |
8801 | 98.4k | zend_string *lcname; |
8802 | | |
8803 | 98.4k | if (fn_flags & ZEND_ACC_READONLY) { |
8804 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use 'readonly' as method modifier"); |
8805 | 0 | } |
8806 | | |
8807 | 98.4k | if ((fn_flags & ZEND_ACC_PRIVATE) && (fn_flags & ZEND_ACC_FINAL) && !zend_is_constructor(name)) { |
8808 | 882 | zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes"); |
8809 | 882 | } |
8810 | | |
8811 | 98.4k | if ((fn_flags & ZEND_ACC_ABSTRACT) |
8812 | 729 | && !(ce->ce_flags & (ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_TRAIT))) { |
8813 | | // Don't say that the class should be declared abstract if it is |
8814 | | // anonymous or an enum and can't be abstract |
8815 | 36 | if (ce->ce_flags & ZEND_ACC_ANON_CLASS) { |
8816 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Anonymous class method %s() must not be abstract", |
8817 | 5 | ZSTR_VAL(name)); |
8818 | 31 | } else if (ce->ce_flags & (ZEND_ACC_ENUM|ZEND_ACC_INTERFACE)) { |
8819 | 20 | zend_error_noreturn(E_COMPILE_ERROR, "%s method %s::%s() must not be abstract", |
8820 | 20 | zend_get_object_type_case(ce, true), ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8821 | 20 | } else { |
8822 | 11 | zend_error_noreturn(E_COMPILE_ERROR, "Class %s declares abstract method %s() and must therefore be declared abstract", |
8823 | 11 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8824 | 11 | } |
8825 | 36 | } |
8826 | | |
8827 | 98.4k | if (in_interface) { |
8828 | 801 | if (!(fn_flags & ZEND_ACC_PUBLIC)) { |
8829 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface method " |
8830 | 0 | "%s::%s() must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8831 | 0 | } |
8832 | 801 | if (fn_flags & ZEND_ACC_FINAL) { |
8833 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Interface method " |
8834 | 5 | "%s::%s() must not be final", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8835 | 5 | } |
8836 | 796 | op_array->fn_flags |= ZEND_ACC_ABSTRACT; |
8837 | 796 | } |
8838 | | |
8839 | 98.4k | if (op_array->fn_flags & ZEND_ACC_ABSTRACT) { |
8840 | 1.48k | if ((op_array->fn_flags & ZEND_ACC_PRIVATE) && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8841 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot be declared private", |
8842 | 5 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8843 | 5 | } |
8844 | | |
8845 | 1.48k | if (has_body) { |
8846 | 4 | zend_error_noreturn(E_COMPILE_ERROR, "%s function %s::%s() cannot contain body", |
8847 | 4 | in_interface ? "Interface" : "Abstract", ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8848 | 4 | } |
8849 | | |
8850 | 1.48k | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
8851 | 96.9k | } else if (!has_body) { |
8852 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract method %s::%s() must contain body", |
8853 | 8 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8854 | 8 | } |
8855 | | |
8856 | 98.4k | op_array->scope = ce; |
8857 | 98.4k | op_array->function_name = zend_string_copy(name); |
8858 | | |
8859 | 98.4k | lcname = zend_string_tolower(name); |
8860 | 98.4k | lcname = zend_new_interned_string(lcname); |
8861 | | |
8862 | 98.4k | if (zend_hash_add_ptr(&ce->function_table, lcname, op_array) == NULL) { |
8863 | 20 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", |
8864 | 20 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
8865 | 20 | } |
8866 | | |
8867 | 98.4k | zend_add_magic_method(ce, (zend_function *) op_array, lcname); |
8868 | 98.4k | if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME) |
8869 | 6.23k | && !(ce->ce_flags & ZEND_ACC_TRAIT)) { |
8870 | 6.12k | add_stringable_interface(ce); |
8871 | 6.12k | } |
8872 | | |
8873 | 98.4k | return lcname; |
8874 | 98.4k | } |
8875 | | /* }}} */ |
8876 | | |
8877 | 1.35M | static uint32_t zend_add_dynamic_func_def(zend_op_array *def) { |
8878 | 1.35M | zend_op_array *op_array = CG(active_op_array); |
8879 | 1.35M | uint32_t def_offset = op_array->num_dynamic_func_defs++; |
8880 | 1.35M | op_array->dynamic_func_defs = erealloc( |
8881 | 1.35M | op_array->dynamic_func_defs, op_array->num_dynamic_func_defs * sizeof(zend_op_array *)); |
8882 | 1.35M | op_array->dynamic_func_defs[def_offset] = def; |
8883 | 1.35M | return def_offset; |
8884 | 1.35M | } |
8885 | | |
8886 | | enum func_decl_level { |
8887 | | FUNC_DECL_LEVEL_TOPLEVEL, |
8888 | | FUNC_DECL_LEVEL_NESTED, |
8889 | | FUNC_DECL_LEVEL_CONSTEXPR, |
8890 | | }; |
8891 | | |
8892 | | static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array, const zend_ast_decl *decl, enum func_decl_level level) /* {{{ */ |
8893 | 1.37M | { |
8894 | 1.37M | zend_string *unqualified_name, *name, *lcname; |
8895 | 1.37M | zend_op *opline; |
8896 | | |
8897 | 1.37M | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8898 | 1.35M | zend_string *filename = op_array->filename; |
8899 | 1.35M | uint32_t start_lineno = decl->start_lineno; |
8900 | | |
8901 | 1.35M | zend_string *class = zend_empty_string; |
8902 | 1.35M | zend_string *separator = zend_empty_string; |
8903 | 1.35M | zend_string *function = filename; |
8904 | 1.35M | const char *parens = ""; |
8905 | | |
8906 | 1.35M | if (CG(active_op_array) && CG(active_op_array)->function_name) { |
8907 | 1.33M | if (CG(active_op_array)->fn_flags & ZEND_ACC_CLOSURE) { |
8908 | | /* If the parent function is a closure, don't redundantly |
8909 | | * add the classname and parentheses. |
8910 | | */ |
8911 | 1.33M | function = CG(active_op_array)->function_name; |
8912 | 1.33M | } else { |
8913 | 2.03k | function = CG(active_op_array)->function_name; |
8914 | 2.03k | parens = "()"; |
8915 | | |
8916 | 2.03k | if (CG(active_class_entry) && CG(active_class_entry)->name) { |
8917 | 1.25k | class = CG(active_class_entry)->name; |
8918 | 1.25k | separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM); |
8919 | 1.25k | } |
8920 | 2.03k | } |
8921 | 1.33M | } |
8922 | | |
8923 | 1.35M | unqualified_name = zend_strpprintf_unchecked( |
8924 | 1.35M | 0, |
8925 | 1.35M | "{closure:%S%S%S%s:%" PRIu32 "}", |
8926 | 1.35M | class, |
8927 | 1.35M | separator, |
8928 | 1.35M | function, |
8929 | 1.35M | parens, |
8930 | 1.35M | start_lineno |
8931 | 1.35M | ); |
8932 | | |
8933 | 1.35M | op_array->function_name = name = unqualified_name; |
8934 | 1.35M | } else { |
8935 | 22.4k | unqualified_name = decl->name; |
8936 | 22.4k | op_array->function_name = name = zend_prefix_with_ns(unqualified_name); |
8937 | 22.4k | } |
8938 | | |
8939 | 1.37M | lcname = zend_string_tolower(name); |
8940 | | |
8941 | 1.37M | if (FC(imports_function)) { |
8942 | 553 | const zend_string *import_name = |
8943 | 553 | zend_hash_find_ptr_lc(FC(imports_function), unqualified_name); |
8944 | 553 | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
8945 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare function %s() (previously declared as local import)", |
8946 | 10 | ZSTR_VAL(name)); |
8947 | 10 | } |
8948 | 553 | } |
8949 | | |
8950 | 1.37M | if (zend_string_equals_literal(lcname, "__autoload")) { |
8951 | 1 | zend_error_noreturn(E_COMPILE_ERROR, |
8952 | 1 | "__autoload() is no longer supported, use spl_autoload_register() instead"); |
8953 | 1 | } |
8954 | | |
8955 | 1.37M | if (zend_string_equals_literal_ci(unqualified_name, "readonly")) { |
8956 | 10 | zend_error(E_DEPRECATED, "Calling a function “readonly” is deprecated"); |
8957 | 10 | } |
8958 | | |
8959 | 1.37M | if (zend_string_equals_literal_ci(unqualified_name, "assert")) { |
8960 | 5 | zend_error(E_COMPILE_ERROR, |
8961 | 5 | "Defining a custom assert() function is not allowed, " |
8962 | 5 | "as the function has special semantics"); |
8963 | 5 | } |
8964 | | |
8965 | 1.37M | zend_register_seen_symbol(lcname, ZEND_SYMBOL_FUNCTION); |
8966 | 1.37M | switch (level) { |
8967 | 1.35M | case FUNC_DECL_LEVEL_NESTED: { |
8968 | 1.35M | uint32_t func_ref = zend_add_dynamic_func_def(op_array); |
8969 | 1.35M | if (op_array->fn_flags & ZEND_ACC_CLOSURE) { |
8970 | 1.35M | opline = zend_emit_op_tmp(result, ZEND_DECLARE_LAMBDA_FUNCTION, NULL, NULL); |
8971 | 1.35M | opline->op2.num = func_ref; |
8972 | 1.35M | opline->extended_value = (uint32_t)-1; |
8973 | 1.35M | } else { |
8974 | 1.83k | opline = get_next_op(); |
8975 | 1.83k | opline->opcode = ZEND_DECLARE_FUNCTION; |
8976 | 1.83k | opline->op1_type = IS_CONST; |
8977 | 1.83k | LITERAL_STR(opline->op1, zend_string_copy(lcname)); |
8978 | 1.83k | opline->op2.num = func_ref; |
8979 | 1.83k | } |
8980 | 1.35M | break; |
8981 | 0 | } |
8982 | 136 | case FUNC_DECL_LEVEL_CONSTEXPR: |
8983 | 20.7k | case FUNC_DECL_LEVEL_TOPLEVEL: |
8984 | | /* Nothing to do. */ |
8985 | 20.7k | break; |
8986 | 1.37M | } |
8987 | 1.37M | return lcname; |
8988 | 1.37M | } |
8989 | | /* }}} */ |
8990 | | |
8991 | | static zend_op_array *zend_compile_func_decl_ex( |
8992 | | znode *result, zend_ast *ast, enum func_decl_level level, |
8993 | | zend_string *property_info_name, |
8994 | | zend_property_hook_kind hook_kind |
8995 | 1.47M | ) { |
8996 | 1.47M | zend_ast_decl *decl = (zend_ast_decl *) ast; |
8997 | 1.47M | zend_ast *params_ast = decl->child[0]; |
8998 | 1.47M | zend_ast *uses_ast = decl->child[1]; |
8999 | 1.47M | zend_ast *stmt_ast = decl->child[2]; |
9000 | 1.47M | zend_ast *return_type_ast = decl->child[3]; |
9001 | 1.47M | bool is_method = decl->kind == ZEND_AST_METHOD; |
9002 | 1.47M | zend_string *lcname = NULL; |
9003 | 1.47M | bool is_hook = decl->kind == ZEND_AST_PROPERTY_HOOK; |
9004 | | |
9005 | 1.47M | zend_class_entry *orig_class_entry = CG(active_class_entry); |
9006 | 1.47M | zend_op_array *orig_op_array = CG(active_op_array); |
9007 | 1.47M | zend_op_array *op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array)); |
9008 | 1.47M | zend_oparray_context orig_oparray_context; |
9009 | 1.47M | closure_info info; |
9010 | | |
9011 | 1.47M | init_op_array(op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE); |
9012 | | |
9013 | 1.47M | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
9014 | 0 | op_array->fn_flags |= ZEND_ACC_PRELOADED; |
9015 | 0 | } |
9016 | | |
9017 | 1.47M | op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES); |
9018 | 1.47M | op_array->fn_flags |= decl->flags; |
9019 | 1.47M | op_array->line_start = decl->start_lineno; |
9020 | 1.47M | op_array->line_end = decl->end_lineno; |
9021 | 1.47M | if (decl->doc_comment) { |
9022 | 247 | op_array->doc_comment = zend_string_copy(decl->doc_comment); |
9023 | 247 | } |
9024 | | |
9025 | 1.47M | if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) { |
9026 | 1.35M | op_array->fn_flags |= ZEND_ACC_CLOSURE; |
9027 | 1.35M | } |
9028 | | |
9029 | 1.47M | if (is_hook) { |
9030 | 5.07k | zend_class_entry *ce = CG(active_class_entry); |
9031 | 5.07k | op_array->scope = ce; |
9032 | 5.07k | op_array->function_name = zend_string_copy(decl->name); |
9033 | 1.47M | } else if (is_method) { |
9034 | 98.4k | bool has_body = stmt_ast != NULL; |
9035 | 98.4k | lcname = zend_begin_method_decl(op_array, decl->name, has_body); |
9036 | 1.37M | } else { |
9037 | 1.37M | lcname = zend_begin_func_decl(result, op_array, decl, level); |
9038 | 1.37M | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
9039 | 251k | find_implicit_binds(&info, params_ast, stmt_ast); |
9040 | 251k | compile_implicit_lexical_binds(&info, result, op_array); |
9041 | 1.12M | } else if (uses_ast) { |
9042 | 3.79k | zend_compile_closure_binding(result, op_array, uses_ast); |
9043 | 3.79k | } |
9044 | 1.37M | } |
9045 | | |
9046 | 1.47M | CG(active_op_array) = op_array; |
9047 | | |
9048 | 1.47M | zend_oparray_context_begin(&orig_oparray_context, op_array); |
9049 | 1.47M | CG(context).active_property_info_name = property_info_name; |
9050 | 1.47M | CG(context).active_property_hook_kind = hook_kind; |
9051 | | |
9052 | 1.47M | if (decl->child[4]) { |
9053 | 980k | int target = ZEND_ATTRIBUTE_TARGET_FUNCTION; |
9054 | | |
9055 | 980k | if (is_method || is_hook) { |
9056 | 885 | target = ZEND_ATTRIBUTE_TARGET_METHOD; |
9057 | 885 | } |
9058 | | |
9059 | 980k | zend_compile_attributes(&op_array->attributes, decl->child[4], 0, target, 0); |
9060 | | |
9061 | 980k | const zend_attribute *override_attribute = zend_get_attribute_str( |
9062 | 980k | op_array->attributes, |
9063 | 980k | "override", |
9064 | 980k | sizeof("override")-1 |
9065 | 980k | ); |
9066 | | |
9067 | 980k | if (override_attribute) { |
9068 | 353 | op_array->fn_flags |= ZEND_ACC_OVERRIDE; |
9069 | 353 | } |
9070 | | |
9071 | 980k | const zend_attribute *deprecated_attribute = zend_get_attribute_str( |
9072 | 980k | op_array->attributes, |
9073 | 980k | "deprecated", |
9074 | 980k | sizeof("deprecated")-1 |
9075 | 980k | ); |
9076 | | |
9077 | 980k | if (deprecated_attribute) { |
9078 | 291 | op_array->fn_flags |= ZEND_ACC_DEPRECATED; |
9079 | 291 | } |
9080 | | |
9081 | | // ZEND_ACC_NODISCARD is added via an attribute validator |
9082 | 980k | } |
9083 | | |
9084 | | /* Do not leak the class scope into free standing functions, even if they are dynamically |
9085 | | * defined inside a class method. This is necessary for correct handling of magic constants. |
9086 | | * For example __CLASS__ should always be "" inside a free standing function. */ |
9087 | 1.47M | if (decl->kind == ZEND_AST_FUNC_DECL) { |
9088 | 22.4k | CG(active_class_entry) = NULL; |
9089 | 22.4k | } |
9090 | | |
9091 | 1.47M | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9092 | 20.5k | op_array->fn_flags |= ZEND_ACC_TOP_LEVEL; |
9093 | 20.5k | } |
9094 | | |
9095 | 1.47M | { |
9096 | | /* Push a separator to the loop variable stack */ |
9097 | 1.47M | zend_loop_var dummy_var; |
9098 | 1.47M | dummy_var.opcode = ZEND_RETURN; |
9099 | | |
9100 | 1.47M | zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var); |
9101 | 1.47M | } |
9102 | | |
9103 | 1.47M | zend_compile_params(params_ast, return_type_ast, |
9104 | 1.47M | is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_LCNAME) ? IS_STRING : 0); |
9105 | 1.47M | if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) { |
9106 | 196k | if (CG(active_class_entry) != NULL) { |
9107 | 292 | if (zend_is_constructor(CG(active_op_array)->function_name)) { |
9108 | 5 | zend_error(E_DEPRECATED, "Making a constructor a Generator is deprecated"); |
9109 | 287 | } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { |
9110 | 6 | zend_error(E_DEPRECATED, "Making a destructor a Generator is deprecated"); |
9111 | 6 | } |
9112 | 292 | } |
9113 | | |
9114 | 196k | zend_mark_function_as_generator(); |
9115 | 196k | zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL); |
9116 | 196k | } |
9117 | 1.47M | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
9118 | 251k | zend_compile_implicit_closure_uses(&info); |
9119 | 251k | zend_hash_destroy(&info.uses); |
9120 | 1.22M | } else if (uses_ast) { |
9121 | 3.77k | zend_compile_closure_uses(uses_ast); |
9122 | 3.77k | } |
9123 | | |
9124 | 1.47M | if (ast->kind == ZEND_AST_ARROW_FUNC && decl->child[2]->kind != ZEND_AST_RETURN) { |
9125 | 10.8k | bool needs_return = true; |
9126 | 10.8k | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
9127 | 1.42k | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
9128 | 1.42k | needs_return = !ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER); |
9129 | 1.42k | } |
9130 | 10.8k | if (needs_return) { |
9131 | 10.4k | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
9132 | 10.4k | decl->child[2] = stmt_ast; |
9133 | 10.4k | } |
9134 | 10.8k | } |
9135 | | |
9136 | 1.47M | if (op_array->fn_flags & ZEND_ACC_NODISCARD) { |
9137 | | /* ZEND_ACC_NODISCARD gets added by the attribute validator, but only |
9138 | | * if the method is not a hook; if it is a hook, then the validator |
9139 | | * will have returned an error message, even if the error message was |
9140 | | * delayed with #[\DelayedTargetValidation] that ZEND_ACC_NODISCARD |
9141 | | * flag should not have been added. */ |
9142 | 327 | ZEND_ASSERT(!is_hook); |
9143 | | |
9144 | 327 | if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) { |
9145 | 239 | const zend_arg_info *return_info = CG(active_op_array)->arg_info - 1; |
9146 | 239 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_VOID)) { |
9147 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9148 | 5 | "A void %s does not return a value, but #[\\NoDiscard] requires a return value", |
9149 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
9150 | 5 | } |
9151 | | |
9152 | 234 | if (ZEND_TYPE_CONTAINS_CODE(return_info->type, IS_NEVER)) { |
9153 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9154 | 5 | "A never returning %s does not return a value, but #[\\NoDiscard] requires a return value", |
9155 | 5 | CG(active_class_entry) != NULL ? "method" : "function"); |
9156 | 5 | } |
9157 | 234 | } |
9158 | 327 | } |
9159 | | |
9160 | 1.47M | zend_compile_stmt(stmt_ast); |
9161 | | |
9162 | 1.47M | if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) { |
9163 | 1.35M | zend_op_array *declaring_op_array = orig_oparray_context.op_array; |
9164 | | |
9165 | 1.35M | if ((op_array->fn_flags & ZEND_ACC_STATIC) |
9166 | 1.31k | && !op_array->static_variables |
9167 | | /* Don't cache closures in main, as those would leak without a proper |
9168 | | * cleanup mechanism. */ |
9169 | 888 | && declaring_op_array->function_name |
9170 | 159 | && declaring_op_array->last) { |
9171 | 114 | zend_op *declare_lambda_op = &declaring_op_array->opcodes[declaring_op_array->last - 1]; |
9172 | 114 | if (declare_lambda_op->opcode == ZEND_DECLARE_LAMBDA_FUNCTION) { |
9173 | 114 | declare_lambda_op->extended_value = declaring_op_array->cache_size; |
9174 | 114 | declaring_op_array->cache_size += sizeof(void *); |
9175 | 114 | } |
9176 | 114 | } |
9177 | 1.35M | } |
9178 | | |
9179 | 1.47M | if (is_method) { |
9180 | 98.1k | CG(zend_lineno) = decl->start_lineno; |
9181 | 98.1k | zend_check_magic_method_implementation( |
9182 | 98.1k | CG(active_class_entry), (zend_function *) op_array, lcname, E_COMPILE_ERROR); |
9183 | 1.37M | } else if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9184 | | /* Only register the function after a successful compile */ |
9185 | 20.1k | if (UNEXPECTED(zend_hash_add_ptr(CG(function_table), lcname, op_array) == NULL)) { |
9186 | 98 | CG(zend_lineno) = decl->start_lineno; |
9187 | 98 | do_bind_function_error(lcname, op_array, true); |
9188 | 98 | } |
9189 | 20.1k | } |
9190 | | |
9191 | | /* put the implicit return on the really last line */ |
9192 | 1.47M | CG(zend_lineno) = decl->end_lineno; |
9193 | | |
9194 | 1.47M | zend_do_extended_stmt(NULL); |
9195 | 1.47M | zend_emit_final_return(false); |
9196 | | |
9197 | 1.47M | pass_two(CG(active_op_array)); |
9198 | 1.47M | zend_oparray_context_end(&orig_oparray_context); |
9199 | | |
9200 | | /* Pop the loop variable stack separator */ |
9201 | 1.47M | zend_stack_del_top(&CG(loop_var_stack)); |
9202 | | |
9203 | 1.47M | if (level == FUNC_DECL_LEVEL_TOPLEVEL) { |
9204 | 19.9k | zend_observer_function_declared_notify(op_array, lcname); |
9205 | 19.9k | } |
9206 | | |
9207 | 1.47M | if (lcname != NULL) { |
9208 | 1.46M | zend_string_release_ex(lcname, 0); |
9209 | 1.46M | } |
9210 | | |
9211 | 1.47M | CG(active_op_array) = orig_op_array; |
9212 | 1.47M | CG(active_class_entry) = orig_class_entry; |
9213 | | |
9214 | 1.47M | return op_array; |
9215 | 1.47M | } |
9216 | | |
9217 | | static zend_op_array *zend_compile_func_decl(znode *result, zend_ast *ast, enum func_decl_level level) |
9218 | 1.47M | { |
9219 | 1.47M | return zend_compile_func_decl_ex(result, ast, level, /* property_info */ NULL, (zend_property_hook_kind)-1); |
9220 | 1.47M | } |
9221 | | |
9222 | 5.99k | zend_property_hook_kind zend_get_property_hook_kind_from_name(const zend_string *name) { |
9223 | 5.99k | if (zend_string_equals_literal_ci(name, "get")) { |
9224 | 3.70k | return ZEND_PROPERTY_HOOK_GET; |
9225 | 3.70k | } else if (zend_string_equals_literal_ci(name, "set")) { |
9226 | 2.19k | return ZEND_PROPERTY_HOOK_SET; |
9227 | 2.19k | } else { |
9228 | 100 | return (zend_property_hook_kind)-1; |
9229 | 100 | } |
9230 | 5.99k | } |
9231 | | |
9232 | | static void zend_compile_property_hooks( |
9233 | | zend_property_info *prop_info, zend_string *prop_name, |
9234 | | zend_ast *prop_type_ast, const zend_ast_list *hooks) |
9235 | 4.34k | { |
9236 | 4.34k | zend_class_entry *ce = CG(active_class_entry); |
9237 | | |
9238 | 4.34k | if (prop_info->flags & ZEND_ACC_READONLY) { |
9239 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Hooked properties cannot be readonly"); |
9240 | 14 | } |
9241 | | |
9242 | 4.32k | if (hooks->children == 0) { |
9243 | 18 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook list must not be empty"); |
9244 | 18 | } |
9245 | | |
9246 | 9.35k | for (uint32_t i = 0; i < hooks->children; i++) { |
9247 | 5.26k | zend_ast_decl *hook = (zend_ast_decl *) hooks->child[i]; |
9248 | 5.26k | zend_string *name = hook->name; |
9249 | 5.26k | zend_ast *stmt_ast = hook->child[2]; |
9250 | 5.26k | zend_ast **return_type_ast_ptr = NULL; |
9251 | 5.26k | zend_ast **value_type_ast_ptr = NULL; |
9252 | 5.26k | CG(zend_lineno) = hook->start_lineno; |
9253 | | |
9254 | | /* Non-private hooks are always public. This avoids having to copy the hook when inheriting |
9255 | | * hooks from protected properties to public ones. */ |
9256 | 5.26k | uint32_t hook_visibility = (prop_info->flags & ZEND_ACC_PPP_MASK) != ZEND_ACC_PRIVATE ? ZEND_ACC_PUBLIC : ZEND_ACC_PRIVATE; |
9257 | 5.26k | hook->flags |= hook_visibility; |
9258 | | |
9259 | 5.26k | if (prop_info->flags & ZEND_ACC_STATIC) { |
9260 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare hooks for static property"); |
9261 | 7 | } |
9262 | 5.25k | if ((hook->flags & ZEND_ACC_FINAL) && (hook->flags & ZEND_ACC_PRIVATE)) { |
9263 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both final and private"); |
9264 | 5 | } |
9265 | 5.25k | if ((ce->ce_flags & ZEND_ACC_INTERFACE) |
9266 | 5.00k | || ((prop_info->flags & ZEND_ACC_ABSTRACT) && !stmt_ast)) { |
9267 | 683 | hook->flags |= ZEND_ACC_ABSTRACT; |
9268 | | |
9269 | 683 | if (stmt_ast) { |
9270 | 22 | zend_error_noreturn(E_COMPILE_ERROR, "Abstract property hook cannot have body"); |
9271 | 22 | } |
9272 | 661 | if (hook->flags & ZEND_ACC_PRIVATE) { |
9273 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9274 | 5 | "Property hook cannot be both abstract and private"); |
9275 | 5 | } |
9276 | 656 | if (hook->flags & ZEND_ACC_FINAL) { |
9277 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Property hook cannot be both abstract and final"); |
9278 | 6 | } |
9279 | 4.56k | } else if (!stmt_ast) { |
9280 | 21 | zend_error_noreturn(E_COMPILE_ERROR, "Non-abstract property hook must have a body"); |
9281 | 21 | } |
9282 | | |
9283 | 5.19k | zend_property_hook_kind hook_kind = zend_get_property_hook_kind_from_name(name); |
9284 | 5.19k | if (hook_kind == (zend_property_hook_kind)-1) { |
9285 | 100 | zend_error_noreturn(E_COMPILE_ERROR, |
9286 | 100 | "Unknown hook \"%s\" for property %s::$%s, expected \"get\" or \"set\"", |
9287 | 100 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9288 | 100 | } |
9289 | | |
9290 | 5.09k | if (stmt_ast && stmt_ast->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
9291 | 2.01k | stmt_ast = stmt_ast->child[0]; |
9292 | 2.01k | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9293 | 1.60k | stmt_ast = zend_ast_create(ZEND_AST_RETURN, stmt_ast); |
9294 | 1.60k | } else { |
9295 | 410 | ZEND_ASSERT(hook_kind == ZEND_PROPERTY_HOOK_SET); |
9296 | 410 | stmt_ast = zend_ast_create(ZEND_AST_ASSIGN, |
9297 | 410 | zend_ast_create(ZEND_AST_PROP, |
9298 | 410 | zend_ast_create(ZEND_AST_VAR, zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_THIS))), |
9299 | 410 | zend_ast_create_zval_from_str(zend_copy_unmangled_prop_name(prop_info->name))), |
9300 | 410 | stmt_ast); |
9301 | 410 | } |
9302 | 2.01k | stmt_ast = zend_ast_create_list(1, ZEND_AST_STMT_LIST, stmt_ast); |
9303 | 2.01k | hook->child[2] = stmt_ast; |
9304 | 2.01k | } |
9305 | | |
9306 | 5.09k | if (hook_kind == ZEND_PROPERTY_HOOK_GET) { |
9307 | 3.18k | if (hook->child[0]) { |
9308 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "get hook of property %s::$%s must not have a parameter list", |
9309 | 7 | ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9310 | 7 | } |
9311 | | |
9312 | 3.17k | hook->child[0] = zend_ast_create_list(0, ZEND_AST_PARAM_LIST); |
9313 | | |
9314 | 3.17k | return_type_ast_ptr = &hook->child[3]; |
9315 | 3.17k | *return_type_ast_ptr = prop_type_ast; |
9316 | 3.17k | } else if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9317 | 1.91k | if (hook->child[0]) { |
9318 | 210 | const zend_ast_list *param_list = zend_ast_get_list(hook->child[0]); |
9319 | 210 | if (param_list->children != 1) { |
9320 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "%s hook of property %s::$%s must accept exactly one parameters", |
9321 | 0 | ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9322 | 0 | } |
9323 | 210 | const zend_ast *value_param_ast = param_list->child[0]; |
9324 | 210 | if (value_param_ast->attr & ZEND_PARAM_REF) { |
9325 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be pass-by-reference", |
9326 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9327 | 5 | } |
9328 | 205 | if (value_param_ast->attr & ZEND_PARAM_VARIADIC) { |
9329 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not be variadic", |
9330 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9331 | 5 | } |
9332 | 200 | if (value_param_ast->child[2]) { |
9333 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Parameter $%s of %s hook %s::$%s must not have a default value", |
9334 | 5 | ZSTR_VAL(zend_ast_get_str(value_param_ast->child[1])), ZSTR_VAL(name), ZSTR_VAL(ce->name), ZSTR_VAL(prop_name)); |
9335 | 5 | } |
9336 | 195 | if ((prop_type_ast != NULL) != (value_param_ast->child[0] != NULL)) { |
9337 | 5 | zend_hooked_property_variance_error_ex(zend_ast_get_str(value_param_ast->child[1]), ce->name, prop_info->name); |
9338 | 5 | } |
9339 | 1.70k | } else { |
9340 | 1.70k | zend_ast *param_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_VALUE)); |
9341 | 1.70k | zend_ast *param = zend_ast_create( |
9342 | 1.70k | ZEND_AST_PARAM, prop_type_ast, param_name_ast, |
9343 | 1.70k | /* expr */ NULL, /* doc_comment */ NULL, /* attributes */ NULL, |
9344 | 1.70k | /* hooks */ NULL); |
9345 | 1.70k | value_type_ast_ptr = ¶m->child[0]; |
9346 | 1.70k | hook->child[0] = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param); |
9347 | 1.70k | } |
9348 | 1.89k | zend_ast *return_type = zend_ast_create(ZEND_AST_TYPE); |
9349 | 1.89k | return_type->attr = IS_VOID; |
9350 | 1.89k | hook->child[3] = return_type; |
9351 | 1.89k | } else { |
9352 | 0 | ZEND_UNREACHABLE(); |
9353 | 0 | } |
9354 | | |
9355 | 5.07k | hook->name = zend_strpprintf(0, "$%s::%s", ZSTR_VAL(prop_name), ZSTR_VAL(name)); |
9356 | | |
9357 | 5.07k | zend_function *func = (zend_function *) zend_compile_func_decl_ex( |
9358 | 5.07k | NULL, (zend_ast *) hook, FUNC_DECL_LEVEL_NESTED, prop_info->name, hook_kind); |
9359 | | |
9360 | 5.07k | func->common.prop_info = prop_info; |
9361 | | |
9362 | 5.07k | if (!prop_info->hooks) { |
9363 | 4.07k | prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9364 | 4.07k | memset(prop_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE); |
9365 | 4.07k | } |
9366 | | |
9367 | 5.07k | if (prop_info->hooks[hook_kind]) { |
9368 | 14 | zend_error_noreturn(E_COMPILE_ERROR, |
9369 | 14 | "Cannot redeclare property hook \"%s\"", ZSTR_VAL(name)); |
9370 | 14 | } |
9371 | 5.05k | prop_info->hooks[hook_kind] = func; |
9372 | | |
9373 | 5.05k | if (hook_kind == ZEND_PROPERTY_HOOK_SET) { |
9374 | 1.86k | switch (zend_verify_property_hook_variance(prop_info, func)) { |
9375 | 1.79k | case INHERITANCE_SUCCESS: |
9376 | 1.79k | break; |
9377 | 57 | case INHERITANCE_UNRESOLVED: |
9378 | 57 | ce->num_hooked_prop_variance_checks++; |
9379 | 57 | break; |
9380 | 9 | case INHERITANCE_ERROR: |
9381 | 9 | zend_hooked_property_variance_error(prop_info); |
9382 | 0 | case INHERITANCE_WARNING: |
9383 | 0 | ZEND_UNREACHABLE(); |
9384 | 1.86k | } |
9385 | 1.86k | } |
9386 | | |
9387 | 5.04k | zend_string_release(name); |
9388 | | /* Un-share type ASTs to avoid double-frees of zval nodes. */ |
9389 | 5.04k | if (return_type_ast_ptr) { |
9390 | 3.13k | *return_type_ast_ptr = NULL; |
9391 | 3.13k | } |
9392 | 5.04k | if (value_type_ast_ptr) { |
9393 | 1.67k | *value_type_ast_ptr = NULL; |
9394 | 1.67k | } |
9395 | 5.04k | } |
9396 | | |
9397 | 4.09k | ce->num_hooked_props++; |
9398 | | |
9399 | | /* See zend_link_hooked_object_iter(). */ |
9400 | 4.09k | #ifndef ZEND_OPCACHE_SHM_REATTACHMENT |
9401 | 4.09k | if (!ce->get_iterator) { |
9402 | | /* Will be removed again, in case of Iterator or IteratorAggregate. */ |
9403 | 3.06k | ce->get_iterator = zend_hooked_object_get_iterator; |
9404 | 3.06k | } |
9405 | 4.09k | #endif |
9406 | | |
9407 | 4.09k | if (!prop_info->ce->parent_name) { |
9408 | 2.60k | zend_verify_hooked_property(ce, prop_info, prop_name); |
9409 | 2.60k | } |
9410 | 4.09k | } |
9411 | | |
9412 | | static void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */ |
9413 | 30.5k | { |
9414 | 30.5k | const zend_ast_list *list = zend_ast_get_list(ast); |
9415 | 30.5k | zend_class_entry *ce = CG(active_class_entry); |
9416 | 30.5k | uint32_t i, children = list->children; |
9417 | | |
9418 | 30.5k | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9419 | 15 | zend_error_noreturn(E_COMPILE_ERROR, "Enum %s cannot include properties", ZSTR_VAL(ce->name)); |
9420 | 15 | } |
9421 | | |
9422 | 30.5k | if ((flags & ZEND_ACC_FINAL) && (flags & ZEND_ACC_PRIVATE)) { |
9423 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property cannot be both final and private"); |
9424 | 5 | } |
9425 | | |
9426 | 30.5k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9427 | 267 | if (flags & ZEND_ACC_FINAL) { |
9428 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be final"); |
9429 | 8 | } |
9430 | 259 | if (flags & (ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) { |
9431 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Property in interface cannot be protected or private"); |
9432 | 5 | } |
9433 | 254 | if (flags & ZEND_ACC_ABSTRACT) { |
9434 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9435 | 5 | "Property in interface cannot be explicitly abstract. " |
9436 | 5 | "All interface members are implicitly abstract"); |
9437 | 5 | } |
9438 | 249 | flags |= ZEND_ACC_ABSTRACT; |
9439 | 249 | } |
9440 | | |
9441 | 61.7k | for (i = 0; i < children; ++i) { |
9442 | 31.3k | zend_property_info *info; |
9443 | 31.3k | zend_ast *prop_ast = list->child[i]; |
9444 | 31.3k | zend_ast *name_ast = prop_ast->child[0]; |
9445 | 31.3k | zend_ast **value_ast_ptr = &prop_ast->child[1]; |
9446 | 31.3k | zend_ast *doc_comment_ast = prop_ast->child[2]; |
9447 | 31.3k | zend_ast *hooks_ast = prop_ast->child[3]; |
9448 | 31.3k | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9449 | 31.3k | zend_string *doc_comment = NULL; |
9450 | 31.3k | zval value_zv; |
9451 | 31.3k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9452 | 31.3k | flags |= zend_property_is_virtual(ce, name, hooks_ast) ? ZEND_ACC_VIRTUAL : 0; |
9453 | | |
9454 | 31.3k | zend_string *old_active_property_info_name = CG(context).active_property_info_name; |
9455 | 31.3k | CG(context).active_property_info_name = name; |
9456 | | |
9457 | 31.3k | if (!hooks_ast) { |
9458 | 27.1k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9459 | 8 | zend_error_noreturn(E_COMPILE_ERROR, |
9460 | 8 | "Interfaces may only include hooked properties"); |
9461 | 8 | } |
9462 | 27.1k | if (flags & ZEND_ACC_ABSTRACT) { |
9463 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9464 | 5 | "Only hooked properties may be declared abstract"); |
9465 | 5 | } |
9466 | 27.1k | } |
9467 | 31.3k | if ((flags & ZEND_ACC_ABSTRACT)) { |
9468 | 619 | ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS; |
9469 | 619 | } |
9470 | | |
9471 | 31.3k | if (type_ast) { |
9472 | 15.1k | type = zend_compile_typename(type_ast); |
9473 | | |
9474 | 15.1k | if (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_VOID|MAY_BE_NEVER|MAY_BE_CALLABLE)) { |
9475 | 5 | zend_string *str = zend_type_to_string(type); |
9476 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
9477 | 5 | "Property %s::$%s cannot have type %s", |
9478 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9479 | 5 | } |
9480 | 15.1k | } |
9481 | | |
9482 | | /* Doc comment has been appended as last element in ZEND_AST_PROP_ELEM ast */ |
9483 | 31.3k | if (doc_comment_ast) { |
9484 | 510 | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
9485 | 510 | } |
9486 | | |
9487 | 31.3k | if (zend_hash_exists(&ce->properties_info, name)) { |
9488 | 33 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s::$%s", |
9489 | 33 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9490 | 33 | } |
9491 | | |
9492 | 31.3k | if (*value_ast_ptr) { |
9493 | 10.7k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9494 | | |
9495 | 10.7k | if (ZEND_TYPE_IS_SET(type) && Z_TYPE(value_zv) != IS_CONSTANT_AST |
9496 | 3.27k | && !zend_is_valid_default_value(type, &value_zv)) { |
9497 | 95 | zend_string *str = zend_type_to_string(type); |
9498 | 95 | if (Z_TYPE(value_zv) == IS_NULL && !ZEND_TYPE_IS_INTERSECTION(type)) { |
9499 | 21 | ZEND_TYPE_FULL_MASK(type) |= MAY_BE_NULL; |
9500 | 21 | zend_string *nullable_str = zend_type_to_string(type); |
9501 | | |
9502 | 21 | zend_error_noreturn(E_COMPILE_ERROR, |
9503 | 21 | "Default value for property of type %s may not be null. " |
9504 | 21 | "Use the nullable type %s to allow null default value", |
9505 | 21 | ZSTR_VAL(str), ZSTR_VAL(nullable_str)); |
9506 | 74 | } else { |
9507 | 74 | zend_error_noreturn(E_COMPILE_ERROR, |
9508 | 74 | "Cannot use %s as default value for property %s::$%s of type %s", |
9509 | 74 | zend_zval_value_name(&value_zv), |
9510 | 74 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(str)); |
9511 | 74 | } |
9512 | 95 | } |
9513 | 20.5k | } else if (!ZEND_TYPE_IS_SET(type) && !hooks_ast) { |
9514 | 6.81k | ZVAL_NULL(&value_zv); |
9515 | 13.7k | } else { |
9516 | 13.7k | ZVAL_UNDEF(&value_zv); |
9517 | 13.7k | } |
9518 | | |
9519 | 31.2k | if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS)) { |
9520 | 82 | flags |= ZEND_ACC_READONLY; |
9521 | 82 | } |
9522 | | |
9523 | 31.2k | if (flags & ZEND_ACC_READONLY) { |
9524 | 728 | if (!ZEND_TYPE_IS_SET(type)) { |
9525 | 15 | zend_error_noreturn(E_COMPILE_ERROR, "Readonly property %s::$%s must have type", |
9526 | 15 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9527 | 15 | } |
9528 | 713 | if (flags & ZEND_ACC_STATIC) { |
9529 | 9 | zend_error_noreturn(E_COMPILE_ERROR, |
9530 | 9 | "Static property %s::$%s cannot be readonly", |
9531 | 9 | ZSTR_VAL(ce->name), ZSTR_VAL(name)); |
9532 | 9 | } |
9533 | 713 | } |
9534 | | |
9535 | 31.2k | info = zend_declare_typed_property(ce, name, &value_zv, flags, doc_comment, type); |
9536 | | |
9537 | 31.2k | if (hooks_ast) { |
9538 | 4.22k | zend_compile_property_hooks(info, name, type_ast, zend_ast_get_list(hooks_ast)); |
9539 | 4.22k | } |
9540 | | |
9541 | 31.2k | if (attr_ast) { |
9542 | 1.73k | zend_compile_attributes(&info->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_PROPERTY, 0); |
9543 | | |
9544 | 1.73k | const zend_attribute *override_attribute = zend_get_attribute_str(info->attributes, "override", sizeof("override")-1); |
9545 | 1.73k | if (override_attribute) { |
9546 | 87 | info->flags |= ZEND_ACC_OVERRIDE; |
9547 | 87 | } |
9548 | 1.73k | } |
9549 | | |
9550 | 31.2k | CG(context).active_property_info_name = old_active_property_info_name; |
9551 | 31.2k | } |
9552 | 30.5k | } |
9553 | | /* }}} */ |
9554 | | |
9555 | | static void zend_compile_prop_group(const zend_ast *ast) /* {{{ */ |
9556 | 30.5k | { |
9557 | 30.5k | zend_ast *type_ast = ast->child[0]; |
9558 | 30.5k | zend_ast *prop_ast = ast->child[1]; |
9559 | 30.5k | zend_ast *attr_ast = ast->child[2]; |
9560 | | |
9561 | 30.5k | zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast); |
9562 | 30.5k | } |
9563 | | /* }}} */ |
9564 | | |
9565 | | static void zend_check_trait_alias_modifiers(uint32_t attr) /* {{{ */ |
9566 | 5.76k | { |
9567 | 5.76k | if (attr & ZEND_ACC_STATIC) { |
9568 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"static\" as method modifier in trait alias"); |
9569 | 5.75k | } else if (attr & ZEND_ACC_ABSTRACT) { |
9570 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"abstract\" as method modifier in trait alias"); |
9571 | 5 | } |
9572 | 5.76k | } |
9573 | | /* }}} */ |
9574 | | |
9575 | | static void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast, zend_ast *type_ast) |
9576 | 8.89k | { |
9577 | 8.89k | const zend_ast_list *list = zend_ast_get_list(ast); |
9578 | 8.89k | zend_class_entry *ce = CG(active_class_entry); |
9579 | 8.89k | uint32_t i, children = list->children; |
9580 | | |
9581 | 17.8k | for (i = 0; i < children; ++i) { |
9582 | 8.95k | zend_class_constant *c; |
9583 | 8.95k | zend_ast *const_ast = list->child[i]; |
9584 | 8.95k | zend_ast *name_ast = const_ast->child[0]; |
9585 | 8.95k | zend_ast **value_ast_ptr = &const_ast->child[1]; |
9586 | 8.95k | zend_ast *doc_comment_ast = const_ast->child[2]; |
9587 | 8.95k | zend_string *name = zval_make_interned_string(zend_ast_get_zval(name_ast)); |
9588 | 8.95k | zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL; |
9589 | 8.95k | zval value_zv; |
9590 | 8.95k | zend_type type = ZEND_TYPE_INIT_NONE(0); |
9591 | | |
9592 | 8.95k | if (type_ast) { |
9593 | 3.18k | type = zend_compile_typename(type_ast); |
9594 | | |
9595 | 3.18k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9596 | | |
9597 | 3.18k | if (type_mask != MAY_BE_ANY && (type_mask & (MAY_BE_CALLABLE|MAY_BE_VOID|MAY_BE_NEVER))) { |
9598 | 5 | zend_string *type_str = zend_type_to_string(type); |
9599 | | |
9600 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Class constant %s::%s cannot have type %s", |
9601 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9602 | 5 | } |
9603 | 3.18k | } |
9604 | | |
9605 | 8.95k | if (UNEXPECTED((flags & ZEND_ACC_PRIVATE) && (flags & ZEND_ACC_FINAL))) { |
9606 | 5 | zend_error_noreturn( |
9607 | 5 | E_COMPILE_ERROR, "Private constant %s::%s cannot be final as it is not visible to other classes", |
9608 | 5 | ZSTR_VAL(ce->name), ZSTR_VAL(name) |
9609 | 5 | ); |
9610 | 5 | } |
9611 | | |
9612 | 8.94k | zend_const_expr_to_zval(&value_zv, value_ast_ptr, /* allow_dynamic */ false); |
9613 | | |
9614 | 8.94k | if (Z_TYPE(value_zv) != IS_CONSTANT_AST && ZEND_TYPE_IS_SET(type) && !zend_is_valid_default_value(type, &value_zv)) { |
9615 | 26 | zend_string *type_str = zend_type_to_string(type); |
9616 | | |
9617 | 26 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as value for class constant %s::%s of type %s", |
9618 | 26 | zend_zval_type_name(&value_zv), ZSTR_VAL(ce->name), ZSTR_VAL(name), ZSTR_VAL(type_str)); |
9619 | 26 | } |
9620 | | |
9621 | 8.91k | c = zend_declare_typed_class_constant(ce, name, &value_zv, flags, doc_comment, type); |
9622 | | |
9623 | 8.91k | if (attr_ast) { |
9624 | 872 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
9625 | | |
9626 | 872 | const zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
9627 | | |
9628 | 872 | if (deprecated) { |
9629 | 109 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
9630 | | /* For deprecated constants, we need to flag the zval for recursion |
9631 | | * detection. Make sure the zval is separated out of shm. */ |
9632 | 109 | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
9633 | 109 | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
9634 | 109 | } |
9635 | | |
9636 | 872 | const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1); |
9637 | 872 | if (override) { |
9638 | 167 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE; |
9639 | | /* We need to be able to remove the flag once the override is |
9640 | | * resolved. See ZEND_ACC_DEPRECATED above. */ |
9641 | 167 | ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
9642 | 167 | ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
9643 | 167 | } |
9644 | 872 | } |
9645 | 8.91k | } |
9646 | 8.89k | } |
9647 | | |
9648 | | static void zend_compile_class_const_group(const zend_ast *ast) /* {{{ */ |
9649 | 8.89k | { |
9650 | 8.89k | zend_ast *const_ast = ast->child[0]; |
9651 | 8.89k | zend_ast *attr_ast = ast->child[1]; |
9652 | 8.89k | zend_ast *type_ast = ast->child[2]; |
9653 | | |
9654 | 8.89k | zend_compile_class_const_decl(const_ast, ast->attr, attr_ast, type_ast); |
9655 | 8.89k | } |
9656 | | /* }}} */ |
9657 | | |
9658 | | static void zend_compile_method_ref(const zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */ |
9659 | 6.58k | { |
9660 | 6.58k | zend_ast *class_ast = ast->child[0]; |
9661 | 6.58k | zend_ast *method_ast = ast->child[1]; |
9662 | | |
9663 | 6.58k | method_ref->method_name = zend_string_copy(zend_ast_get_str(method_ast)); |
9664 | | |
9665 | 6.58k | if (class_ast) { |
9666 | 1.59k | method_ref->class_name = zend_resolve_const_class_name_reference(class_ast, "trait name"); |
9667 | 4.99k | } else { |
9668 | 4.99k | method_ref->class_name = NULL; |
9669 | 4.99k | } |
9670 | 6.58k | } |
9671 | | /* }}} */ |
9672 | | |
9673 | | static void zend_compile_trait_precedence(const zend_ast *ast) /* {{{ */ |
9674 | 838 | { |
9675 | 838 | const zend_ast *method_ref_ast = ast->child[0]; |
9676 | 838 | zend_ast *insteadof_ast = ast->child[1]; |
9677 | 838 | const zend_ast_list *insteadof_list = zend_ast_get_list(insteadof_ast); |
9678 | 838 | uint32_t i; |
9679 | | |
9680 | 838 | zend_trait_precedence *precedence = emalloc(sizeof(zend_trait_precedence) + (insteadof_list->children - 1) * sizeof(zend_string*)); |
9681 | 838 | zend_compile_method_ref(method_ref_ast, &precedence->trait_method); |
9682 | 838 | precedence->num_excludes = insteadof_list->children; |
9683 | | |
9684 | 10.7k | for (i = 0; i < insteadof_list->children; ++i) { |
9685 | 9.93k | zend_ast *name_ast = insteadof_list->child[i]; |
9686 | 9.93k | precedence->exclude_class_names[i] = |
9687 | 9.93k | zend_resolve_const_class_name_reference(name_ast, "trait name"); |
9688 | 9.93k | } |
9689 | | |
9690 | 838 | zend_add_to_list(&CG(active_class_entry)->trait_precedences, precedence); |
9691 | 838 | } |
9692 | | /* }}} */ |
9693 | | |
9694 | | static void zend_compile_trait_alias(const zend_ast *ast) /* {{{ */ |
9695 | 5.76k | { |
9696 | 5.76k | const zend_ast *method_ref_ast = ast->child[0]; |
9697 | 5.76k | zend_ast *alias_ast = ast->child[1]; |
9698 | 5.76k | uint32_t modifiers = ast->attr; |
9699 | | |
9700 | 5.76k | zend_trait_alias *alias; |
9701 | | |
9702 | 5.76k | zend_check_trait_alias_modifiers(modifiers); |
9703 | | |
9704 | 5.76k | alias = emalloc(sizeof(zend_trait_alias)); |
9705 | 5.76k | zend_compile_method_ref(method_ref_ast, &alias->trait_method); |
9706 | 5.76k | alias->modifiers = modifiers; |
9707 | | |
9708 | 5.76k | if (alias_ast) { |
9709 | 5.47k | alias->alias = zend_string_copy(zend_ast_get_str(alias_ast)); |
9710 | 5.47k | } else { |
9711 | 292 | alias->alias = NULL; |
9712 | 292 | } |
9713 | | |
9714 | 5.76k | zend_add_to_list(&CG(active_class_entry)->trait_aliases, alias); |
9715 | 5.76k | } |
9716 | | /* }}} */ |
9717 | | |
9718 | | static void zend_compile_use_trait(const zend_ast *ast) /* {{{ */ |
9719 | 4.63k | { |
9720 | 4.63k | const zend_ast_list *traits = zend_ast_get_list(ast->child[0]); |
9721 | 4.63k | zend_ast_list *adaptations = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL; |
9722 | 4.63k | zend_class_entry *ce = CG(active_class_entry); |
9723 | 4.63k | uint32_t i; |
9724 | | |
9725 | 4.63k | ce->trait_names = erealloc(ce->trait_names, sizeof(zend_class_name) * (ce->num_traits + traits->children)); |
9726 | | |
9727 | 12.5k | for (i = 0; i < traits->children; ++i) { |
9728 | 7.92k | zend_ast *trait_ast = traits->child[i]; |
9729 | | |
9730 | 7.92k | if (ce->ce_flags & ZEND_ACC_INTERFACE) { |
9731 | 5 | zend_string *name = zend_ast_get_str(trait_ast); |
9732 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use traits inside of interfaces. " |
9733 | 5 | "%s is used in %s", ZSTR_VAL(name), ZSTR_VAL(ce->name)); |
9734 | 5 | } |
9735 | | |
9736 | 7.91k | ce->trait_names[ce->num_traits].name = |
9737 | 7.91k | zend_resolve_const_class_name_reference(trait_ast, "trait name"); |
9738 | 7.91k | ce->trait_names[ce->num_traits].lc_name = zend_string_tolower(ce->trait_names[ce->num_traits].name); |
9739 | 7.91k | ce->num_traits++; |
9740 | 7.91k | } |
9741 | | |
9742 | 4.63k | if (!adaptations) { |
9743 | 2.48k | return; |
9744 | 2.48k | } |
9745 | | |
9746 | 8.72k | for (i = 0; i < adaptations->children; ++i) { |
9747 | 6.60k | const zend_ast *adaptation_ast = adaptations->child[i]; |
9748 | 6.60k | switch (adaptation_ast->kind) { |
9749 | 838 | case ZEND_AST_TRAIT_PRECEDENCE: |
9750 | 838 | zend_compile_trait_precedence(adaptation_ast); |
9751 | 838 | break; |
9752 | 5.76k | case ZEND_AST_TRAIT_ALIAS: |
9753 | 5.76k | zend_compile_trait_alias(adaptation_ast); |
9754 | 5.76k | break; |
9755 | 0 | default: ZEND_UNREACHABLE(); |
9756 | 6.60k | } |
9757 | 6.60k | } |
9758 | 2.14k | } |
9759 | | /* }}} */ |
9760 | | |
9761 | | static void zend_compile_implements(zend_ast *ast) /* {{{ */ |
9762 | 37.1k | { |
9763 | 37.1k | const zend_ast_list *list = zend_ast_get_list(ast); |
9764 | 37.1k | zend_class_entry *ce = CG(active_class_entry); |
9765 | 37.1k | zend_class_name *interface_names; |
9766 | 37.1k | uint32_t i; |
9767 | | |
9768 | 37.1k | interface_names = emalloc(sizeof(zend_class_name) * list->children); |
9769 | | |
9770 | 104k | for (i = 0; i < list->children; ++i) { |
9771 | 67.7k | zend_ast *class_ast = list->child[i]; |
9772 | 67.7k | interface_names[i].name = |
9773 | 67.7k | zend_resolve_const_class_name_reference(class_ast, "interface name"); |
9774 | 67.7k | interface_names[i].lc_name = zend_string_tolower(interface_names[i].name); |
9775 | 67.7k | } |
9776 | | |
9777 | 37.1k | ce->num_interfaces = list->children; |
9778 | 37.1k | ce->interface_names = interface_names; |
9779 | 37.1k | } |
9780 | | /* }}} */ |
9781 | | |
9782 | | static zend_string *zend_generate_anon_class_name(const zend_ast_decl *decl) |
9783 | 30.4k | { |
9784 | 30.4k | zend_string *filename = CG(active_op_array)->filename; |
9785 | 30.4k | uint32_t start_lineno = decl->start_lineno; |
9786 | | |
9787 | | /* Use parent or first interface as prefix. */ |
9788 | 30.4k | zend_string *prefix = ZSTR_KNOWN(ZEND_STR_CLASS); |
9789 | 30.4k | if (decl->child[0]) { |
9790 | 193 | prefix = zend_resolve_const_class_name_reference(decl->child[0], "class name"); |
9791 | 30.2k | } else if (decl->child[1]) { |
9792 | 28.8k | const zend_ast_list *list = zend_ast_get_list(decl->child[1]); |
9793 | 28.8k | prefix = zend_resolve_const_class_name_reference(list->child[0], "interface name"); |
9794 | 28.8k | } |
9795 | | |
9796 | 30.4k | zend_string *result = zend_strpprintf(0, "%s@anonymous%c%s:%" PRIu32 "$%" PRIx32, |
9797 | 30.4k | ZSTR_VAL(prefix), '\0', ZSTR_VAL(filename), start_lineno, CG(rtd_key_counter)++); |
9798 | 30.4k | zend_string_release(prefix); |
9799 | 30.4k | return zend_new_interned_string(result); |
9800 | 30.4k | } |
9801 | | |
9802 | | static void zend_compile_enum_backing_type(zend_class_entry *ce, zend_ast *enum_backing_type_ast) |
9803 | 1.00k | { |
9804 | 1.00k | ZEND_ASSERT(ce->ce_flags & ZEND_ACC_ENUM); |
9805 | 1.00k | zend_type type = zend_compile_typename(enum_backing_type_ast); |
9806 | 1.00k | uint32_t type_mask = ZEND_TYPE_PURE_MASK(type); |
9807 | 1.00k | if (ZEND_TYPE_IS_COMPLEX(type) || (type_mask != MAY_BE_LONG && type_mask != MAY_BE_STRING)) { |
9808 | 49 | zend_string *type_string = zend_type_to_string(type); |
9809 | 49 | zend_error_noreturn(E_COMPILE_ERROR, |
9810 | 49 | "Enum backing type must be int or string, %s given", |
9811 | 49 | ZSTR_VAL(type_string)); |
9812 | 49 | } |
9813 | 951 | if (type_mask == MAY_BE_LONG) { |
9814 | 625 | ce->enum_backing_type = IS_LONG; |
9815 | 625 | } else { |
9816 | 326 | ZEND_ASSERT(type_mask == MAY_BE_STRING); |
9817 | 326 | ce->enum_backing_type = IS_STRING; |
9818 | 322 | } |
9819 | 951 | zend_type_release(type, 0); |
9820 | 947 | } |
9821 | | |
9822 | | static void zend_compile_class_decl(znode *result, const zend_ast *ast, bool toplevel) /* {{{ */ |
9823 | 201k | { |
9824 | 201k | const zend_ast_decl *decl = (const zend_ast_decl *) ast; |
9825 | 201k | zend_ast *extends_ast = decl->child[0]; |
9826 | 201k | zend_ast *implements_ast = decl->child[1]; |
9827 | 201k | zend_ast *stmt_ast = decl->child[2]; |
9828 | 201k | zend_ast *enum_backing_type_ast = decl->child[4]; |
9829 | 201k | zend_string *name, *lcname; |
9830 | 201k | zend_class_entry *ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry)); |
9831 | 201k | zend_op *opline; |
9832 | | |
9833 | 201k | zend_class_entry *original_ce = CG(active_class_entry); |
9834 | | |
9835 | 201k | if (EXPECTED((decl->flags & ZEND_ACC_ANON_CLASS) == 0)) { |
9836 | 170k | zend_string *unqualified_name = decl->name; |
9837 | | |
9838 | 170k | if (CG(active_class_entry)) { |
9839 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Class declarations may not be nested"); |
9840 | 8 | } |
9841 | | |
9842 | 170k | const char *type = "a class name"; |
9843 | 170k | if (decl->flags & ZEND_ACC_ENUM) { |
9844 | 21.9k | type = "an enum name"; |
9845 | 148k | } else if (decl->flags & ZEND_ACC_INTERFACE) { |
9846 | 4.77k | type = "an interface name"; |
9847 | 144k | } else if (decl->flags & ZEND_ACC_TRAIT) { |
9848 | 4.37k | type = "a trait name"; |
9849 | 4.37k | } |
9850 | 170k | zend_assert_valid_class_name(unqualified_name, type); |
9851 | 170k | name = zend_prefix_with_ns(unqualified_name); |
9852 | 170k | name = zend_new_interned_string(name); |
9853 | 170k | lcname = zend_string_tolower(name); |
9854 | | |
9855 | 170k | if (FC(imports)) { |
9856 | 734 | zend_string *import_name = |
9857 | 734 | zend_hash_find_ptr_lc(FC(imports), unqualified_name); |
9858 | 734 | if (import_name && !zend_string_equals_ci(lcname, import_name)) { |
9859 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare class %s " |
9860 | 10 | "(previously declared as local import)", ZSTR_VAL(name)); |
9861 | 10 | } |
9862 | 734 | } |
9863 | | |
9864 | 170k | zend_register_seen_symbol(lcname, ZEND_SYMBOL_CLASS); |
9865 | 170k | } else { |
9866 | | /* Find an anon class name that is not in use yet. */ |
9867 | 30.4k | name = NULL; |
9868 | 30.4k | lcname = NULL; |
9869 | 30.4k | do { |
9870 | 30.4k | zend_tmp_string_release(name); |
9871 | 30.4k | zend_tmp_string_release(lcname); |
9872 | 30.4k | name = zend_generate_anon_class_name(decl); |
9873 | 30.4k | lcname = zend_string_tolower(name); |
9874 | 30.4k | } while (zend_hash_exists(CG(class_table), lcname)); |
9875 | 30.4k | } |
9876 | 201k | lcname = zend_new_interned_string(lcname); |
9877 | | |
9878 | 201k | ce->type = ZEND_USER_CLASS; |
9879 | 201k | ce->name = name; |
9880 | 201k | zend_initialize_class_data(ce, true); |
9881 | 201k | if (!(decl->flags & ZEND_ACC_ANON_CLASS)) { |
9882 | 170k | zend_alloc_ce_cache(ce->name); |
9883 | 170k | } |
9884 | | |
9885 | 201k | if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) { |
9886 | 0 | ce->ce_flags |= ZEND_ACC_PRELOADED; |
9887 | 0 | ZEND_MAP_PTR_NEW(ce->static_members_table); |
9888 | 0 | ZEND_MAP_PTR_NEW(ce->mutable_data); |
9889 | 0 | } |
9890 | | |
9891 | 201k | ce->ce_flags |= decl->flags; |
9892 | 201k | ce->info.user.filename = zend_string_copy(zend_get_compiled_filename()); |
9893 | 201k | ce->info.user.line_start = decl->start_lineno; |
9894 | 201k | ce->info.user.line_end = decl->end_lineno; |
9895 | | |
9896 | 201k | if (decl->doc_comment) { |
9897 | 111 | ce->doc_comment = zend_string_copy(decl->doc_comment); |
9898 | 111 | } |
9899 | | |
9900 | 201k | if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) { |
9901 | | /* Serialization is not supported for anonymous classes */ |
9902 | 30.4k | ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE; |
9903 | 30.4k | } |
9904 | | |
9905 | 201k | if (extends_ast) { |
9906 | 42.8k | ce->parent_name = |
9907 | 42.8k | zend_resolve_const_class_name_reference(extends_ast, "class name"); |
9908 | 42.8k | } |
9909 | | |
9910 | 201k | CG(active_class_entry) = ce; |
9911 | | |
9912 | 201k | if (decl->child[3]) { |
9913 | 2.98k | zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0); |
9914 | 2.98k | } |
9915 | | |
9916 | 201k | if (implements_ast) { |
9917 | 37.1k | zend_compile_implements(implements_ast); |
9918 | 37.1k | } |
9919 | | |
9920 | 201k | if (ce->ce_flags & ZEND_ACC_ENUM) { |
9921 | 21.9k | if (enum_backing_type_ast != NULL) { |
9922 | 1.00k | zend_compile_enum_backing_type(ce, enum_backing_type_ast); |
9923 | 1.00k | } |
9924 | 21.9k | zend_enum_add_interfaces(ce); |
9925 | 21.9k | zend_enum_register_props(ce); |
9926 | 21.9k | } |
9927 | | |
9928 | 201k | zend_compile_stmt(stmt_ast); |
9929 | | |
9930 | | /* Reset lineno for final opcodes and errors */ |
9931 | 201k | CG(zend_lineno) = ast->lineno; |
9932 | | |
9933 | 201k | 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) { |
9934 | 69 | zend_verify_abstract_class(ce); |
9935 | 69 | } |
9936 | | |
9937 | 201k | CG(active_class_entry) = original_ce; |
9938 | | |
9939 | 201k | if (toplevel) { |
9940 | 46.1k | ce->ce_flags |= ZEND_ACC_TOP_LEVEL; |
9941 | 46.1k | } |
9942 | | |
9943 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
9944 | 201k | if (!ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
9945 | | #ifdef ZEND_OPCACHE_SHM_REATTACHMENT |
9946 | | /* See zend_link_hooked_object_iter(). */ |
9947 | | && !ce->num_hooked_props |
9948 | | #endif |
9949 | 135k | && !(CG(compiler_options) & ZEND_COMPILE_WITHOUT_EXECUTION)) { |
9950 | 135k | if (toplevel) { |
9951 | 37.7k | if (extends_ast) { |
9952 | 9.55k | zend_class_entry *parent_ce = zend_lookup_class_ex( |
9953 | 9.55k | ce->parent_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); |
9954 | | |
9955 | 9.55k | if (parent_ce |
9956 | 8.61k | && !zend_compile_ignore_class(parent_ce, ce->info.user.filename)) { |
9957 | 8.61k | if (zend_try_early_bind(ce, parent_ce, lcname, NULL)) { |
9958 | 5.23k | zend_string_release(lcname); |
9959 | 5.23k | return; |
9960 | 5.23k | } |
9961 | 8.61k | } |
9962 | 28.1k | } else if (EXPECTED(zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL)) { |
9963 | 24.3k | zend_string_release(lcname); |
9964 | 24.3k | zend_build_properties_info_table(ce); |
9965 | 24.3k | zend_inheritance_check_override(ce); |
9966 | 24.3k | ce->ce_flags |= ZEND_ACC_LINKED; |
9967 | 24.3k | zend_observer_class_linked_notify(ce, lcname); |
9968 | 24.3k | return; |
9969 | 24.3k | } else { |
9970 | 3.79k | goto link_unbound; |
9971 | 3.79k | } |
9972 | 98.1k | } else if (!extends_ast) { |
9973 | 69.3k | link_unbound: |
9974 | | /* Link unbound simple class */ |
9975 | 69.3k | zend_build_properties_info_table(ce); |
9976 | 69.3k | zend_inheritance_check_override(ce); |
9977 | 69.3k | ce->ce_flags |= ZEND_ACC_LINKED; |
9978 | 69.3k | } |
9979 | 135k | } |
9980 | | |
9981 | 171k | opline = get_next_op(); |
9982 | | |
9983 | 171k | if (ce->parent_name) { |
9984 | | /* Lowercased parent name */ |
9985 | 36.6k | zend_string *lc_parent_name = zend_string_tolower(ce->parent_name); |
9986 | 36.6k | opline->op2_type = IS_CONST; |
9987 | 36.6k | LITERAL_STR(opline->op2, lc_parent_name); |
9988 | 36.6k | } |
9989 | | |
9990 | 171k | opline->op1_type = IS_CONST; |
9991 | | /* It's possible that `lcname` is not an interned string because it was not yet in the interned string table. |
9992 | | * However, by this point another thread may have caused `lcname` to be added in the interned string table. |
9993 | | * This will cause `lcname` to get freed once it is found in the interned string table. If we were to use |
9994 | | * LITERAL_STR() here we would not change the `lcname` pointer to the new value, and it would point to the |
9995 | | * now-freed string. This will cause issues when we use `lcname` in the code below. We solve this by using |
9996 | | * zend_add_literal_string() which gives us the new value. */ |
9997 | 171k | opline->op1.constant = zend_add_literal_string(&lcname); |
9998 | | |
9999 | 171k | if (decl->flags & ZEND_ACC_ANON_CLASS) { |
10000 | 30.3k | opline->opcode = ZEND_DECLARE_ANON_CLASS; |
10001 | 30.3k | opline->extended_value = zend_alloc_cache_slot(); |
10002 | 30.3k | zend_make_var_result(result, opline); |
10003 | 30.3k | if (!zend_hash_add_ptr(CG(class_table), lcname, ce)) { |
10004 | | /* We checked above that the class name is not used. This really shouldn't happen. */ |
10005 | 0 | zend_error_noreturn(E_ERROR, |
10006 | 0 | "Runtime definition key collision for %s. This is a bug", ZSTR_VAL(name)); |
10007 | 0 | } |
10008 | 141k | } else { |
10009 | | /* Generate RTD keys until we find one that isn't in use yet. */ |
10010 | 141k | zend_string *key = NULL; |
10011 | 141k | do { |
10012 | 141k | zend_tmp_string_release(key); |
10013 | 141k | key = zend_build_runtime_definition_key(lcname, decl->start_lineno); |
10014 | 141k | } while (!zend_hash_add_ptr(CG(class_table), key, ce)); |
10015 | | |
10016 | | /* RTD key is placed after lcname literal in op1 */ |
10017 | 141k | zend_add_literal_string(&key); |
10018 | | |
10019 | 141k | opline->opcode = ZEND_DECLARE_CLASS; |
10020 | 141k | if (toplevel |
10021 | 15.7k | && (CG(compiler_options) & ZEND_COMPILE_DELAYED_BINDING) |
10022 | | /* We currently don't early-bind classes that implement interfaces or use traits */ |
10023 | 6.18k | && !ce->num_interfaces && !ce->num_traits && !ce->num_hooked_prop_variance_checks |
10024 | 141k | ) { |
10025 | 1.48k | if (!extends_ast) { |
10026 | | /* Use empty string for classes without parents to avoid new handler, and special |
10027 | | * handling of zend_early_binding. */ |
10028 | 867 | opline->op2_type = IS_CONST; |
10029 | 867 | LITERAL_STR(opline->op2, ZSTR_EMPTY_ALLOC()); |
10030 | 867 | } |
10031 | 1.48k | CG(active_op_array)->fn_flags |= ZEND_ACC_EARLY_BINDING; |
10032 | 1.48k | opline->opcode = ZEND_DECLARE_CLASS_DELAYED; |
10033 | 1.48k | opline->extended_value = zend_alloc_cache_slot(); |
10034 | 1.48k | opline->result_type = IS_UNUSED; |
10035 | 1.48k | opline->result.opline_num = -1; |
10036 | 1.48k | } |
10037 | 141k | } |
10038 | 171k | } |
10039 | | /* }}} */ |
10040 | | |
10041 | | static void zend_compile_enum_case(zend_ast *ast) |
10042 | 38.3k | { |
10043 | 38.3k | zend_class_entry *enum_class = CG(active_class_entry); |
10044 | 38.3k | if (!(enum_class->ce_flags & ZEND_ACC_ENUM)) { |
10045 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Case can only be used in enums"); |
10046 | 7 | } |
10047 | | |
10048 | 38.3k | zend_string *enum_case_name = zval_make_interned_string(zend_ast_get_zval(ast->child[0])); |
10049 | 38.3k | zend_string *enum_class_name = enum_class->name; |
10050 | | |
10051 | 38.3k | zval class_name_zval; |
10052 | 38.3k | ZVAL_STR_COPY(&class_name_zval, enum_class_name); |
10053 | 38.3k | zend_ast *class_name_ast = zend_ast_create_zval(&class_name_zval); |
10054 | | |
10055 | 38.3k | zval case_id_zval; |
10056 | 38.3k | int case_id = zend_enum_next_case_id(enum_class); |
10057 | 38.3k | ZVAL_LONG(&case_id_zval, case_id); |
10058 | 38.3k | zend_ast *case_id_ast = zend_ast_create_zval(&case_id_zval); |
10059 | | |
10060 | 38.3k | zval case_name_zval; |
10061 | 38.3k | ZVAL_STR_COPY(&case_name_zval, enum_case_name); |
10062 | 38.3k | zend_ast *case_name_ast = zend_ast_create_zval(&case_name_zval); |
10063 | | |
10064 | 38.3k | zend_ast *case_value_ast = ast->child[1]; |
10065 | | // Remove case_value_ast from the original AST to avoid freeing it, as it will be freed by zend_const_expr_to_zval |
10066 | 38.3k | ast->child[1] = NULL; |
10067 | 38.3k | if (enum_class->enum_backing_type != IS_UNDEF && case_value_ast == NULL) { |
10068 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of backed enum %s must have a value", |
10069 | 5 | ZSTR_VAL(enum_case_name), |
10070 | 5 | ZSTR_VAL(enum_class_name)); |
10071 | 38.3k | } else if (enum_class->enum_backing_type == IS_UNDEF && case_value_ast != NULL) { |
10072 | 12 | zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-backed enum %s must not have a value", |
10073 | 12 | ZSTR_VAL(enum_case_name), |
10074 | 12 | ZSTR_VAL(enum_class_name)); |
10075 | 12 | } |
10076 | | |
10077 | 38.3k | zend_ast *const_enum_init_ast = zend_ast_create(ZEND_AST_CONST_ENUM_INIT, |
10078 | 38.3k | class_name_ast, case_id_ast, case_name_ast, case_value_ast); |
10079 | | |
10080 | 38.3k | zval value_zv; |
10081 | 38.3k | zend_const_expr_to_zval(&value_zv, &const_enum_init_ast, /* allow_dynamic */ false); |
10082 | | |
10083 | | /* Doc comment has been appended as second last element in ZEND_AST_ENUM ast - attributes are conventionally last */ |
10084 | 38.3k | zend_ast *doc_comment_ast = ast->child[2]; |
10085 | 38.3k | zend_string *doc_comment = NULL; |
10086 | 38.3k | if (doc_comment_ast) { |
10087 | 32.0k | doc_comment = zend_string_copy(zend_ast_get_str(doc_comment_ast)); |
10088 | 32.0k | } |
10089 | | |
10090 | 38.3k | zend_class_constant *c = zend_declare_class_constant_ex(enum_class, enum_case_name, &value_zv, ZEND_ACC_PUBLIC, doc_comment); |
10091 | 38.3k | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_CLASS_CONST_IS_CASE; |
10092 | 38.3k | zend_ast_destroy(const_enum_init_ast); |
10093 | | |
10094 | 38.3k | zend_ast *attr_ast = ast->child[3]; |
10095 | 38.3k | if (attr_ast) { |
10096 | 367 | zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST, 0); |
10097 | | |
10098 | 367 | zend_attribute *deprecated = zend_get_attribute_str(c->attributes, "deprecated", sizeof("deprecated")-1); |
10099 | | |
10100 | 367 | if (deprecated) { |
10101 | 111 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; |
10102 | 111 | } |
10103 | | |
10104 | 367 | const zend_attribute *override = zend_get_attribute_str(c->attributes, "override", sizeof("override") - 1); |
10105 | 367 | if (override) { |
10106 | 19 | ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_OVERRIDE; |
10107 | | /* We need to be able to remove the flag once the override is |
10108 | | * resolved. See ZEND_ACC_DEPRECATED handling in |
10109 | | * zend_compile_class_const_decl(). */ |
10110 | 19 | enum_class->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; |
10111 | 19 | enum_class->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; |
10112 | 19 | } |
10113 | 367 | } |
10114 | 38.3k | } |
10115 | | |
10116 | | static HashTable *zend_get_import_ht(uint32_t type) /* {{{ */ |
10117 | 2.05k | { |
10118 | 2.05k | switch (type) { |
10119 | 1.25k | case ZEND_SYMBOL_CLASS: |
10120 | 1.25k | if (!FC(imports)) { |
10121 | 832 | FC(imports) = emalloc(sizeof(HashTable)); |
10122 | 832 | zend_hash_init(FC(imports), 8, NULL, str_dtor, 0); |
10123 | 832 | } |
10124 | 1.25k | return FC(imports); |
10125 | 462 | case ZEND_SYMBOL_FUNCTION: |
10126 | 462 | if (!FC(imports_function)) { |
10127 | 357 | FC(imports_function) = emalloc(sizeof(HashTable)); |
10128 | 357 | zend_hash_init(FC(imports_function), 8, NULL, str_dtor, 0); |
10129 | 357 | } |
10130 | 462 | return FC(imports_function); |
10131 | 340 | case ZEND_SYMBOL_CONST: |
10132 | 340 | if (!FC(imports_const)) { |
10133 | 257 | FC(imports_const) = emalloc(sizeof(HashTable)); |
10134 | 257 | zend_hash_init(FC(imports_const), 8, NULL, str_dtor, 0); |
10135 | 257 | } |
10136 | 340 | return FC(imports_const); |
10137 | 0 | default: ZEND_UNREACHABLE(); |
10138 | 2.05k | } |
10139 | | |
10140 | 0 | return NULL; |
10141 | 2.05k | } |
10142 | | /* }}} */ |
10143 | | |
10144 | | static char *zend_get_use_type_str(uint32_t type) /* {{{ */ |
10145 | 71 | { |
10146 | 71 | switch (type) { |
10147 | 40 | case ZEND_SYMBOL_CLASS: |
10148 | 40 | return ""; |
10149 | 18 | case ZEND_SYMBOL_FUNCTION: |
10150 | 18 | return " function"; |
10151 | 13 | case ZEND_SYMBOL_CONST: |
10152 | 13 | return " const"; |
10153 | 0 | default: ZEND_UNREACHABLE(); |
10154 | 71 | } |
10155 | | |
10156 | 0 | return " unknown"; |
10157 | 71 | } |
10158 | | /* }}} */ |
10159 | | |
10160 | | 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) /* {{{ */ |
10161 | 38 | { |
10162 | 38 | if (zend_string_equals_ci(old_name, check_name)) { |
10163 | 13 | return; |
10164 | 13 | } |
10165 | | |
10166 | 25 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
10167 | 25 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
10168 | 38 | } |
10169 | | /* }}} */ |
10170 | | |
10171 | | static void zend_compile_use(zend_ast *ast) /* {{{ */ |
10172 | 2.05k | { |
10173 | 2.05k | const zend_ast_list *list = zend_ast_get_list(ast); |
10174 | 2.05k | uint32_t i; |
10175 | 2.05k | zend_string *current_ns = FC(current_namespace); |
10176 | 2.05k | uint32_t type = ast->attr; |
10177 | 2.05k | HashTable *current_import = zend_get_import_ht(type); |
10178 | 2.05k | bool case_sensitive = type == ZEND_SYMBOL_CONST; |
10179 | | |
10180 | 4.24k | for (i = 0; i < list->children; ++i) { |
10181 | 2.25k | const zend_ast *use_ast = list->child[i]; |
10182 | 2.25k | zend_ast *old_name_ast = use_ast->child[0]; |
10183 | 2.25k | zend_ast *new_name_ast = use_ast->child[1]; |
10184 | 2.25k | zend_string *old_name = zend_ast_get_str(old_name_ast); |
10185 | 2.25k | zend_string *new_name, *lookup_name; |
10186 | | |
10187 | 2.25k | if (new_name_ast) { |
10188 | 438 | new_name = zend_string_copy(zend_ast_get_str(new_name_ast)); |
10189 | 1.81k | } else { |
10190 | 1.81k | const char *unqualified_name; |
10191 | 1.81k | size_t unqualified_name_len; |
10192 | 1.81k | if (zend_get_unqualified_name(old_name, &unqualified_name, &unqualified_name_len)) { |
10193 | | /* The form "use A\B" is equivalent to "use A\B as B" */ |
10194 | 961 | new_name = zend_string_init(unqualified_name, unqualified_name_len, 0); |
10195 | 961 | } else { |
10196 | 856 | new_name = zend_string_copy(old_name); |
10197 | | |
10198 | 856 | if (!current_ns) { |
10199 | 438 | zend_error(E_WARNING, "The use statement with non-compound name '%s' " |
10200 | 438 | "has no effect", ZSTR_VAL(new_name)); |
10201 | 438 | } |
10202 | 856 | } |
10203 | 1.81k | } |
10204 | | |
10205 | 2.25k | if (case_sensitive) { |
10206 | 368 | lookup_name = zend_string_copy(new_name); |
10207 | 1.88k | } else { |
10208 | 1.88k | lookup_name = zend_string_tolower(new_name); |
10209 | 1.88k | } |
10210 | | |
10211 | 2.25k | if (type == ZEND_SYMBOL_CLASS && zend_is_reserved_class_name(new_name)) { |
10212 | 25 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' " |
10213 | 25 | "is a special class name", ZSTR_VAL(old_name), ZSTR_VAL(new_name), ZSTR_VAL(new_name)); |
10214 | 25 | } |
10215 | | |
10216 | 2.23k | if (zend_string_equals(new_name, ZSTR_CHAR('_'))) { |
10217 | 25 | switch (type) { |
10218 | 8 | case ZEND_SYMBOL_CLASS: |
10219 | 8 | zend_error(E_DEPRECATED, "Using \"_\" as a class name is deprecated"); |
10220 | 8 | break; |
10221 | 8 | case ZEND_SYMBOL_CONST: |
10222 | 8 | zend_error(E_DEPRECATED, "Using \"_\" as a constant name is deprecated since 8.6"); |
10223 | 8 | break; |
10224 | 9 | case ZEND_SYMBOL_FUNCTION: |
10225 | 9 | break; |
10226 | 0 | default: ZEND_UNREACHABLE(); |
10227 | 25 | } |
10228 | 25 | } |
10229 | | |
10230 | 2.23k | if (current_ns) { |
10231 | 1.32k | zend_string *ns_name = zend_string_alloc(ZSTR_LEN(current_ns) + 1 + ZSTR_LEN(new_name), 0); |
10232 | 1.32k | zend_str_tolower_copy(ZSTR_VAL(ns_name), ZSTR_VAL(current_ns), ZSTR_LEN(current_ns)); |
10233 | 1.32k | ZSTR_VAL(ns_name)[ZSTR_LEN(current_ns)] = '\\'; |
10234 | 1.32k | memcpy(ZSTR_VAL(ns_name) + ZSTR_LEN(current_ns) + 1, ZSTR_VAL(lookup_name), ZSTR_LEN(lookup_name) + 1); |
10235 | | |
10236 | 1.32k | if (zend_have_seen_symbol(ns_name, type)) { |
10237 | 17 | zend_check_already_in_use(type, old_name, new_name, ns_name); |
10238 | 17 | } |
10239 | | |
10240 | 1.32k | zend_string_efree(ns_name); |
10241 | 1.32k | } else if (zend_have_seen_symbol(lookup_name, type)) { |
10242 | 21 | zend_check_already_in_use(type, old_name, new_name, lookup_name); |
10243 | 21 | } |
10244 | | |
10245 | 2.23k | zend_string_addref(old_name); |
10246 | 2.23k | old_name = zend_new_interned_string(old_name); |
10247 | 2.23k | if (!zend_hash_add_ptr(current_import, lookup_name, old_name)) { |
10248 | 46 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use%s %s as %s because the name " |
10249 | 46 | "is already in use", zend_get_use_type_str(type), ZSTR_VAL(old_name), ZSTR_VAL(new_name)); |
10250 | 46 | } |
10251 | | |
10252 | 2.18k | zend_string_release_ex(lookup_name, 0); |
10253 | 2.18k | zend_string_release_ex(new_name, 0); |
10254 | 2.18k | } |
10255 | 2.05k | } |
10256 | | /* }}} */ |
10257 | | |
10258 | | static void zend_compile_group_use(const zend_ast *ast) /* {{{ */ |
10259 | 239 | { |
10260 | 239 | uint32_t i; |
10261 | 239 | const zend_string *ns = zend_ast_get_str(ast->child[0]); |
10262 | 239 | const zend_ast_list *list = zend_ast_get_list(ast->child[1]); |
10263 | | |
10264 | 919 | for (i = 0; i < list->children; i++) { |
10265 | 680 | zend_ast *inline_use, *use = list->child[i]; |
10266 | 680 | zval *name_zval = zend_ast_get_zval(use->child[0]); |
10267 | 680 | zend_string *name = Z_STR_P(name_zval); |
10268 | 680 | zend_string *compound_ns = zend_concat_names(ZSTR_VAL(ns), ZSTR_LEN(ns), ZSTR_VAL(name), ZSTR_LEN(name)); |
10269 | 680 | zend_string_release_ex(name, 0); |
10270 | 680 | ZVAL_STR(name_zval, compound_ns); |
10271 | 680 | inline_use = zend_ast_create_list(1, ZEND_AST_USE, use); |
10272 | 680 | inline_use->attr = ast->attr ? ast->attr : use->attr; |
10273 | 680 | zend_compile_use(inline_use); |
10274 | 680 | } |
10275 | 239 | } |
10276 | | /* }}} */ |
10277 | | |
10278 | | static void zend_compile_const_decl(zend_ast *ast) /* {{{ */ |
10279 | 5.11k | { |
10280 | 5.11k | zend_ast_list *list = zend_ast_get_list(ast); |
10281 | 5.11k | uint32_t i; |
10282 | 5.11k | zend_ast *attributes_ast = NULL; |
10283 | 5.11k | zend_op *last_op = NULL; |
10284 | 10.9k | for (i = 0; i < list->children; ++i) { |
10285 | 5.89k | zend_ast *const_ast = list->child[i]; |
10286 | 5.89k | if (const_ast->kind == ZEND_AST_ATTRIBUTE_LIST) { |
10287 | 476 | ZEND_ASSERT(i == list->children - 1); |
10288 | 476 | attributes_ast = const_ast; |
10289 | 476 | continue; |
10290 | 476 | } |
10291 | 5.41k | ZEND_ASSERT(const_ast->kind == ZEND_AST_CONST_ELEM); |
10292 | 5.41k | zend_ast *name_ast = const_ast->child[0]; |
10293 | 5.41k | zend_ast **value_ast_ptr = &const_ast->child[1]; |
10294 | 5.41k | zend_string *unqualified_name = zend_ast_get_str(name_ast); |
10295 | | |
10296 | 5.41k | zend_string *name; |
10297 | 5.41k | znode name_node, value_node; |
10298 | 5.41k | zval *value_zv = &value_node.u.constant; |
10299 | | |
10300 | 5.41k | value_node.op_type = IS_CONST; |
10301 | 5.41k | zend_const_expr_to_zval(value_zv, value_ast_ptr, /* allow_dynamic */ true); |
10302 | | |
10303 | 5.41k | if (zend_get_special_const(ZSTR_VAL(unqualified_name), ZSTR_LEN(unqualified_name))) { |
10304 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
10305 | 6 | "Cannot redeclare constant '%s'", ZSTR_VAL(unqualified_name)); |
10306 | 6 | } |
10307 | | |
10308 | 5.41k | name = zend_prefix_with_ns(unqualified_name); |
10309 | 5.41k | name = zend_new_interned_string(name); |
10310 | | |
10311 | 5.41k | if (FC(imports_const)) { |
10312 | 906 | zend_string *import_name = zend_hash_find_ptr(FC(imports_const), unqualified_name); |
10313 | 906 | if (import_name && !zend_string_equals(import_name, name)) { |
10314 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot declare const %s because " |
10315 | 8 | "the name is already in use", ZSTR_VAL(name)); |
10316 | 8 | } |
10317 | 906 | } |
10318 | | |
10319 | 5.40k | name_node.op_type = IS_CONST; |
10320 | 5.40k | ZVAL_STR(&name_node.u.constant, name); |
10321 | | |
10322 | 5.40k | last_op = zend_emit_op(NULL, ZEND_DECLARE_CONST, &name_node, &value_node); |
10323 | | |
10324 | 5.40k | zend_register_seen_symbol(name, ZEND_SYMBOL_CONST); |
10325 | 5.40k | } |
10326 | 5.10k | if (attributes_ast == NULL) { |
10327 | 4.51k | return; |
10328 | 4.51k | } |
10329 | | /* Validate: attributes can only be applied to one constant at a time |
10330 | | * Since we store the AST for the attributes in the list of children, |
10331 | | * there should be exactly 2 children. */ |
10332 | 585 | if (list->children > 2) { |
10333 | 5 | zend_error_noreturn( |
10334 | 5 | E_COMPILE_ERROR, |
10335 | 5 | "Cannot apply attributes to multiple constants at once" |
10336 | 5 | ); |
10337 | 5 | } |
10338 | | |
10339 | 580 | HashTable *attributes = NULL; |
10340 | 580 | zend_compile_attributes(&attributes, list->child[1], 0, ZEND_ATTRIBUTE_TARGET_CONST, 0); |
10341 | | |
10342 | 580 | ZEND_ASSERT(last_op != NULL); |
10343 | 580 | last_op->opcode = ZEND_DECLARE_ATTRIBUTED_CONST; |
10344 | 460 | znode attribs_node; |
10345 | 460 | attribs_node.op_type = IS_CONST; |
10346 | 460 | ZVAL_PTR(&attribs_node.u.constant, attributes); |
10347 | 460 | zend_emit_op_data(&attribs_node); |
10348 | 460 | CG(active_op_array)->fn_flags |= ZEND_ACC_PTR_OPS; |
10349 | 460 | } |
10350 | | /* }}}*/ |
10351 | | |
10352 | | static void zend_compile_namespace(const zend_ast *ast) /* {{{ */ |
10353 | 4.96k | { |
10354 | 4.96k | zend_ast *name_ast = ast->child[0]; |
10355 | 4.96k | zend_ast *stmt_ast = ast->child[1]; |
10356 | 4.96k | zend_string *name; |
10357 | 4.96k | bool with_bracket = stmt_ast != NULL; |
10358 | | |
10359 | | /* handle mixed syntax declaration or nested namespaces */ |
10360 | 4.96k | if (!FC(has_bracketed_namespaces)) { |
10361 | 3.48k | if (FC(current_namespace)) { |
10362 | | /* previous namespace declarations were unbracketed */ |
10363 | 584 | if (with_bracket) { |
10364 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10365 | 7 | "with unbracketed namespace declarations"); |
10366 | 7 | } |
10367 | 584 | } |
10368 | 3.48k | } else { |
10369 | | /* previous namespace declarations were bracketed */ |
10370 | 1.48k | if (!with_bracket) { |
10371 | 9 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations " |
10372 | 9 | "with unbracketed namespace declarations"); |
10373 | 1.47k | } else if (FC(current_namespace) || FC(in_namespace)) { |
10374 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested"); |
10375 | 6 | } |
10376 | 1.48k | } |
10377 | | |
10378 | 4.96k | bool is_first_namespace = (!with_bracket && !FC(current_namespace)) |
10379 | 2.65k | || (with_bracket && !FC(has_bracketed_namespaces)); |
10380 | 4.94k | if (is_first_namespace && FAILURE == zend_is_first_statement(ast, /* allow_nop */ true)) { |
10381 | 28 | zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be " |
10382 | 28 | "the very first statement or after any declare call in the script"); |
10383 | 28 | } |
10384 | | |
10385 | 4.91k | if (FC(current_namespace)) { |
10386 | 577 | zend_string_release_ex(FC(current_namespace), 0); |
10387 | 577 | } |
10388 | | |
10389 | 4.91k | if (name_ast) { |
10390 | 3.85k | name = zend_ast_get_str(name_ast); |
10391 | | |
10392 | 3.85k | if (zend_string_equals_literal_ci(name, "namespace")) { |
10393 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name", ZSTR_VAL(name)); |
10394 | 5 | } |
10395 | | |
10396 | 3.84k | FC(current_namespace) = zend_string_copy(name); |
10397 | 3.84k | } else { |
10398 | 1.06k | FC(current_namespace) = NULL; |
10399 | 1.06k | } |
10400 | | |
10401 | 4.91k | zend_reset_import_tables(); |
10402 | | |
10403 | 4.91k | FC(in_namespace) = 1; |
10404 | 4.91k | if (with_bracket) { |
10405 | 2.07k | FC(has_bracketed_namespaces) = 1; |
10406 | 2.07k | } |
10407 | | |
10408 | 4.91k | if (stmt_ast) { |
10409 | 2.07k | zend_compile_top_stmt(stmt_ast); |
10410 | 2.07k | zend_end_namespace(); |
10411 | 2.07k | } |
10412 | 4.91k | } |
10413 | | /* }}} */ |
10414 | | |
10415 | | static void zend_compile_halt_compiler(const zend_ast *ast) /* {{{ */ |
10416 | 134 | { |
10417 | 134 | zend_ast *offset_ast = ast->child[0]; |
10418 | 134 | zend_long offset = Z_LVAL_P(zend_ast_get_zval(offset_ast)); |
10419 | | |
10420 | 134 | const char const_name[] = "__COMPILER_HALT_OFFSET__"; |
10421 | | |
10422 | 134 | if (FC(has_bracketed_namespaces) && FC(in_namespace)) { |
10423 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
10424 | 0 | "__HALT_COMPILER() can only be used from the outermost scope"); |
10425 | 0 | } |
10426 | | |
10427 | 134 | const zend_string *filename = zend_get_compiled_filename(); |
10428 | 134 | zend_string *name = zend_mangle_property_name(const_name, sizeof(const_name) - 1, |
10429 | 134 | ZSTR_VAL(filename), ZSTR_LEN(filename), false); |
10430 | | |
10431 | | /* Avoid repeated declaration of the __COMPILER_HALT_OFFSET__ constant in |
10432 | | * case this file was already included. */ |
10433 | 134 | if (!zend_hash_find(EG(zend_constants), name)) { |
10434 | 134 | zend_register_long_constant(ZSTR_VAL(name), ZSTR_LEN(name), offset, 0, 0); |
10435 | 134 | } |
10436 | 134 | zend_string_release_ex(name, 0); |
10437 | 134 | } |
10438 | | /* }}} */ |
10439 | | |
10440 | | static bool zend_try_ct_eval_magic_const(zval *zv, const zend_ast *ast) /* {{{ */ |
10441 | 17.2k | { |
10442 | 17.2k | const zend_op_array *op_array = CG(active_op_array); |
10443 | 17.2k | const zend_class_entry *ce = CG(active_class_entry); |
10444 | | |
10445 | 17.2k | switch (ast->attr) { |
10446 | 168 | case T_LINE: |
10447 | 168 | ZVAL_LONG(zv, ast->lineno); |
10448 | 168 | break; |
10449 | 4.99k | case T_FILE: |
10450 | 4.99k | ZVAL_STR_COPY(zv, CG(compiled_filename)); |
10451 | 4.99k | break; |
10452 | 1.26k | case T_DIR: |
10453 | 1.26k | { |
10454 | 1.26k | const zend_string *filename = CG(compiled_filename); |
10455 | 1.26k | zend_string *dirname = zend_string_init(ZSTR_VAL(filename), ZSTR_LEN(filename), 0); |
10456 | | #ifdef ZEND_WIN32 |
10457 | | ZSTR_LEN(dirname) = php_win32_ioutil_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10458 | | #else |
10459 | 1.26k | ZSTR_LEN(dirname) = zend_dirname(ZSTR_VAL(dirname), ZSTR_LEN(dirname)); |
10460 | 1.26k | #endif |
10461 | | |
10462 | 1.26k | if (zend_string_equals_literal(dirname, ".")) { |
10463 | 728 | dirname = zend_string_extend(dirname, MAXPATHLEN, 0); |
10464 | 728 | #ifdef HAVE_GETCWD |
10465 | 728 | ZEND_IGNORE_VALUE(VCWD_GETCWD(ZSTR_VAL(dirname), MAXPATHLEN)); |
10466 | | #elif defined(HAVE_GETWD) |
10467 | | ZEND_IGNORE_VALUE(VCWD_GETWD(ZSTR_VAL(dirname))); |
10468 | | #endif |
10469 | 728 | ZSTR_LEN(dirname) = strlen(ZSTR_VAL(dirname)); |
10470 | 728 | } |
10471 | | |
10472 | 1.26k | ZVAL_STR(zv, dirname); |
10473 | 1.26k | break; |
10474 | 0 | } |
10475 | 1.77k | case T_FUNC_C: |
10476 | 1.77k | if (op_array && op_array->function_name) { |
10477 | 1.53k | ZVAL_STR_COPY(zv, op_array->function_name); |
10478 | 1.53k | } else { |
10479 | 239 | ZVAL_EMPTY_STRING(zv); |
10480 | 239 | } |
10481 | 1.77k | break; |
10482 | 353 | case T_PROPERTY_C: { |
10483 | 353 | zend_string *prop_info_name = CG(context).active_property_info_name; |
10484 | 353 | if (prop_info_name) { |
10485 | 274 | ZVAL_STR(zv, zend_copy_unmangled_prop_name(prop_info_name)); |
10486 | 274 | } else { |
10487 | 79 | ZVAL_EMPTY_STRING(zv); |
10488 | 79 | } |
10489 | 353 | break; |
10490 | 0 | } |
10491 | 4.32k | case T_METHOD_C: |
10492 | | /* Detect whether we are directly inside a class (e.g. a class constant) and treat |
10493 | | * this as not being inside a function. */ |
10494 | 4.32k | if (op_array && ce && !op_array->scope && !(op_array->fn_flags & ZEND_ACC_CLOSURE)) { |
10495 | 364 | op_array = NULL; |
10496 | 364 | } |
10497 | 4.32k | if (op_array && op_array->function_name) { |
10498 | 3.68k | if (op_array->scope) { |
10499 | 3.13k | ZVAL_NEW_STR(zv, |
10500 | 3.13k | zend_create_member_string(op_array->scope->name, op_array->function_name)); |
10501 | 3.13k | } else { |
10502 | 547 | ZVAL_STR_COPY(zv, op_array->function_name); |
10503 | 547 | } |
10504 | 3.68k | } else { |
10505 | 648 | ZVAL_EMPTY_STRING(zv); |
10506 | 648 | } |
10507 | 4.32k | break; |
10508 | 2.08k | case T_CLASS_C: |
10509 | 2.08k | if (ce) { |
10510 | 1.69k | if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10511 | 1.07k | return 0; |
10512 | 1.07k | } else { |
10513 | 614 | ZVAL_STR_COPY(zv, ce->name); |
10514 | 614 | } |
10515 | 1.69k | } else { |
10516 | 388 | ZVAL_EMPTY_STRING(zv); |
10517 | 388 | } |
10518 | 1.00k | break; |
10519 | 1.33k | case T_TRAIT_C: |
10520 | 1.33k | if (ce && (ce->ce_flags & ZEND_ACC_TRAIT) != 0) { |
10521 | 98 | ZVAL_STR_COPY(zv, ce->name); |
10522 | 1.23k | } else { |
10523 | 1.23k | ZVAL_EMPTY_STRING(zv); |
10524 | 1.23k | } |
10525 | 1.33k | break; |
10526 | 924 | case T_NS_C: |
10527 | 924 | if (FC(current_namespace)) { |
10528 | 844 | ZVAL_STR_COPY(zv, FC(current_namespace)); |
10529 | 844 | } else { |
10530 | 80 | ZVAL_EMPTY_STRING(zv); |
10531 | 80 | } |
10532 | 924 | break; |
10533 | 0 | default: ZEND_UNREACHABLE(); |
10534 | 17.2k | } |
10535 | | |
10536 | 16.1k | return true; |
10537 | 17.2k | } |
10538 | | /* }}} */ |
10539 | | |
10540 | | ZEND_API bool zend_is_op_long_compatible(const zval *op) |
10541 | 101k | { |
10542 | 101k | if (Z_TYPE_P(op) == IS_ARRAY) { |
10543 | 855 | return false; |
10544 | 855 | } |
10545 | | |
10546 | 100k | if (Z_TYPE_P(op) == IS_DOUBLE |
10547 | 33.9k | && !zend_is_long_compatible(Z_DVAL_P(op), zend_dval_to_lval_silent(Z_DVAL_P(op)))) { |
10548 | 28.7k | return false; |
10549 | 28.7k | } |
10550 | | |
10551 | 71.9k | if (Z_TYPE_P(op) == IS_STRING) { |
10552 | 6.80k | double dval = 0; |
10553 | 6.80k | uint8_t is_num = is_numeric_str_function(Z_STR_P(op), NULL, &dval); |
10554 | 6.80k | if (is_num == 0 || (is_num == IS_DOUBLE && !zend_is_long_compatible(dval, zend_dval_to_lval_silent(dval)))) { |
10555 | 3.20k | return false; |
10556 | 3.20k | } |
10557 | 6.80k | } |
10558 | | |
10559 | 68.7k | return true; |
10560 | 71.9k | } |
10561 | | |
10562 | | ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2) /* {{{ */ |
10563 | 938k | { |
10564 | 938k | if ((opcode == ZEND_CONCAT || opcode == ZEND_FAST_CONCAT)) { |
10565 | | /* Array to string warning. */ |
10566 | 41.0k | return Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY; |
10567 | 41.0k | } |
10568 | | |
10569 | 897k | if (!(opcode == ZEND_ADD || opcode == ZEND_SUB || opcode == ZEND_MUL || opcode == ZEND_DIV |
10570 | 759k | || opcode == ZEND_POW || opcode == ZEND_MOD || opcode == ZEND_SL || opcode == ZEND_SR |
10571 | 699k | || opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR)) { |
10572 | | /* Only the numeric operations throw errors. */ |
10573 | 648k | return 0; |
10574 | 648k | } |
10575 | | |
10576 | 248k | if (Z_TYPE_P(op1) == IS_ARRAY || Z_TYPE_P(op2) == IS_ARRAY) { |
10577 | 6.05k | if (opcode == ZEND_ADD && Z_TYPE_P(op1) == IS_ARRAY && Z_TYPE_P(op2) == IS_ARRAY) { |
10578 | | /* Adding two arrays is allowed. */ |
10579 | 2.95k | return 0; |
10580 | 2.95k | } |
10581 | | |
10582 | | /* Numeric operators throw when one of the operands is an array. */ |
10583 | 3.10k | return 1; |
10584 | 6.05k | } |
10585 | | |
10586 | | /* While basic arithmetic operators always produce numeric string errors, |
10587 | | * bitwise operators don't produce errors if both operands are strings */ |
10588 | 242k | if ((opcode == ZEND_BW_OR || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) |
10589 | 50.2k | && Z_TYPE_P(op1) == IS_STRING && Z_TYPE_P(op2) == IS_STRING) { |
10590 | 14.8k | return 0; |
10591 | 14.8k | } |
10592 | | |
10593 | 228k | if (Z_TYPE_P(op1) == IS_STRING |
10594 | 41.3k | && !is_numeric_string(Z_STRVAL_P(op1), Z_STRLEN_P(op1), NULL, NULL, 0)) { |
10595 | 31.3k | return 1; |
10596 | 31.3k | } |
10597 | | |
10598 | 196k | if (Z_TYPE_P(op2) == IS_STRING |
10599 | 38.1k | && !is_numeric_string(Z_STRVAL_P(op2), Z_STRLEN_P(op2), NULL, NULL, 0)) { |
10600 | 32.6k | return 1; |
10601 | 32.6k | } |
10602 | | |
10603 | | /* Operation which cast float/float-strings to integers might produce incompatible float to int errors */ |
10604 | 164k | if (opcode == ZEND_SL || opcode == ZEND_SR || opcode == ZEND_BW_OR |
10605 | 156k | || opcode == ZEND_BW_AND || opcode == ZEND_BW_XOR) { |
10606 | 38.2k | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2)) { |
10607 | 22.2k | return 1; |
10608 | 22.2k | } |
10609 | 38.2k | } |
10610 | | |
10611 | 141k | if (opcode == ZEND_DIV && zval_get_double(op2) == 0.0) { |
10612 | | /* Division by zero throws an error. */ |
10613 | 979 | return 1; |
10614 | 979 | } |
10615 | | |
10616 | | /* Mod is an operation that will cast float/float-strings to integers which might |
10617 | | produce float to int incompatible errors, and also cannot be divided by 0 */ |
10618 | 140k | if (opcode == ZEND_MOD) { |
10619 | 13.7k | if (!zend_is_op_long_compatible(op1) || !zend_is_op_long_compatible(op2) || zval_get_long(op2) == 0) { |
10620 | 10.0k | return 1; |
10621 | 10.0k | } |
10622 | 13.7k | } |
10623 | | |
10624 | 130k | if ((opcode == ZEND_POW) && zval_get_double(op1) == 0 && zval_get_double(op2) < 0) { |
10625 | | /* 0 ** (<0) throws a division by zero error. */ |
10626 | 191 | return 1; |
10627 | 191 | } |
10628 | 130k | if ((opcode == ZEND_SL || opcode == ZEND_SR) && zval_get_long(op2) < 0) { |
10629 | | /* Shift by negative number throws an error. */ |
10630 | 198 | return 1; |
10631 | 198 | } |
10632 | | |
10633 | 130k | return 0; |
10634 | 130k | } |
10635 | | /* }}} */ |
10636 | | |
10637 | | static inline bool zend_try_ct_eval_binary_op(zval *result, uint32_t opcode, zval *op1, zval *op2) /* {{{ */ |
10638 | 878k | { |
10639 | 878k | if (zend_binary_op_produces_error(opcode, op1, op2)) { |
10640 | 71.9k | return false; |
10641 | 71.9k | } |
10642 | | |
10643 | 806k | const binary_op_type fn = get_binary_op(opcode); |
10644 | 806k | fn(result, op1, op2); |
10645 | 806k | return true; |
10646 | 878k | } |
10647 | | /* }}} */ |
10648 | | |
10649 | | ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op) |
10650 | 392k | { |
10651 | 392k | if (opcode == ZEND_BW_NOT) { |
10652 | | /* BW_NOT on string does not convert the string into an integer. */ |
10653 | 8.41k | if (Z_TYPE_P(op) == IS_STRING) { |
10654 | 1.35k | return 0; |
10655 | 1.35k | } |
10656 | 7.06k | return Z_TYPE_P(op) <= IS_TRUE || !zend_is_op_long_compatible(op); |
10657 | 8.41k | } |
10658 | | /* Can happen when called from zend_optimizer_eval_unary_op() */ |
10659 | 384k | if (opcode == ZEND_BOOL || opcode == ZEND_BOOL_NOT) { |
10660 | | /* ZEND_BOOL/ZEND_BOOL_NOT warns when casting NAN. */ |
10661 | 384k | return Z_TYPE_P(op) == IS_DOUBLE; |
10662 | 384k | } |
10663 | | |
10664 | 0 | return 0; |
10665 | 384k | } |
10666 | | |
10667 | | static inline bool zend_try_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */ |
10668 | 390k | { |
10669 | 390k | if (zend_unary_op_produces_error(opcode, op)) { |
10670 | 23.6k | return false; |
10671 | 23.6k | } |
10672 | | |
10673 | 366k | const unary_op_type fn = get_unary_op(opcode); |
10674 | 366k | fn(result, op); |
10675 | 366k | return true; |
10676 | 390k | } |
10677 | | /* }}} */ |
10678 | | |
10679 | | static inline bool zend_try_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */ |
10680 | 77.7k | { |
10681 | 77.7k | zval right; |
10682 | 77.7k | ZVAL_LONG(&right, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10683 | 77.7k | return zend_try_ct_eval_binary_op(result, ZEND_MUL, op, &right); |
10684 | 77.7k | } |
10685 | | /* }}} */ |
10686 | | |
10687 | | static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */ |
10688 | 18.5k | { |
10689 | 18.5k | const binary_op_type fn = kind == ZEND_AST_GREATER |
10690 | 18.5k | ? is_smaller_function : is_smaller_or_equal_function; |
10691 | 18.5k | fn(result, op2, op1); |
10692 | 18.5k | } |
10693 | | /* }}} */ |
10694 | | |
10695 | | static bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ |
10696 | 2.82M | { |
10697 | 2.82M | const zend_ast_list *list = zend_ast_get_list(ast); |
10698 | 2.82M | zend_ast *last_elem_ast = NULL; |
10699 | 2.82M | uint32_t i; |
10700 | 2.82M | bool is_constant = true; |
10701 | | |
10702 | 2.82M | if (ast->attr == ZEND_ARRAY_SYNTAX_LIST) { |
10703 | 7 | zend_error(E_COMPILE_ERROR, "Cannot use list() as standalone expression"); |
10704 | 7 | } |
10705 | | |
10706 | | /* First ensure that *all* child nodes are constant and by-val */ |
10707 | 5.98M | for (i = 0; i < list->children; ++i) { |
10708 | 3.15M | zend_ast *elem_ast = list->child[i]; |
10709 | | |
10710 | 3.15M | if (elem_ast == NULL) { |
10711 | | /* Report error at line of last non-empty element */ |
10712 | 111 | if (last_elem_ast) { |
10713 | 70 | CG(zend_lineno) = zend_ast_get_lineno(last_elem_ast); |
10714 | 70 | } |
10715 | 111 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
10716 | 111 | } |
10717 | | |
10718 | 3.15M | if (elem_ast->kind != ZEND_AST_UNPACK) { |
10719 | 2.95M | zend_eval_const_expr(&elem_ast->child[0]); |
10720 | 2.95M | zend_eval_const_expr(&elem_ast->child[1]); |
10721 | | |
10722 | 2.95M | if (elem_ast->attr /* by_ref */ || elem_ast->child[0]->kind != ZEND_AST_ZVAL |
10723 | 390k | || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL) |
10724 | 2.95M | ) { |
10725 | 2.56M | is_constant = false; |
10726 | 2.56M | } |
10727 | 2.95M | } else { |
10728 | 201k | zend_eval_const_expr(&elem_ast->child[0]); |
10729 | | |
10730 | 201k | if (elem_ast->child[0]->kind != ZEND_AST_ZVAL) { |
10731 | 200k | is_constant = false; |
10732 | 200k | } |
10733 | 201k | } |
10734 | | |
10735 | 3.15M | last_elem_ast = elem_ast; |
10736 | 3.15M | } |
10737 | | |
10738 | 2.82M | if (!is_constant) { |
10739 | 2.72M | return false; |
10740 | 2.72M | } |
10741 | | |
10742 | 106k | if (!list->children) { |
10743 | 24.4k | ZVAL_EMPTY_ARRAY(result); |
10744 | 24.4k | return true; |
10745 | 24.4k | } |
10746 | | |
10747 | 81.9k | array_init_size(result, list->children); |
10748 | 414k | for (i = 0; i < list->children; ++i) { |
10749 | 335k | const zend_ast *elem_ast = list->child[i]; |
10750 | 335k | zend_ast *value_ast = elem_ast->child[0]; |
10751 | 335k | zend_ast *key_ast; |
10752 | | |
10753 | 335k | zval *value = zend_ast_get_zval(value_ast); |
10754 | 335k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
10755 | 969 | if (Z_TYPE_P(value) == IS_ARRAY) { |
10756 | 962 | const HashTable *ht = Z_ARRVAL_P(value); |
10757 | 962 | zval *val; |
10758 | 962 | zend_string *key; |
10759 | | |
10760 | 2.24k | ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) { |
10761 | 2.24k | if (key) { |
10762 | 240 | zend_hash_update(Z_ARRVAL_P(result), key, val); |
10763 | 308 | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) { |
10764 | 16 | zval_ptr_dtor(result); |
10765 | 16 | return 0; |
10766 | 16 | } |
10767 | 532 | Z_TRY_ADDREF_P(val); |
10768 | 532 | } ZEND_HASH_FOREACH_END(); |
10769 | | |
10770 | 946 | continue; |
10771 | 962 | } else { |
10772 | 7 | zend_error_noreturn(E_COMPILE_ERROR, "Only arrays and Traversables can be unpacked, %s given", zend_zval_value_name(value)); |
10773 | 7 | } |
10774 | 969 | } |
10775 | | |
10776 | 334k | Z_TRY_ADDREF_P(value); |
10777 | | |
10778 | 334k | key_ast = elem_ast->child[1]; |
10779 | 334k | if (key_ast) { |
10780 | 23.6k | const zval *key = zend_ast_get_zval(key_ast); |
10781 | 23.6k | switch (Z_TYPE_P(key)) { |
10782 | 6.27k | case IS_LONG: |
10783 | 6.27k | zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value); |
10784 | 6.27k | break; |
10785 | 13.0k | case IS_STRING: |
10786 | 13.0k | zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value); |
10787 | 13.0k | break; |
10788 | 3.70k | case IS_DOUBLE: { |
10789 | 3.70k | zend_long lval = zend_dval_to_lval_silent(Z_DVAL_P(key)); |
10790 | | /* Incompatible float will generate an error, leave this to run-time */ |
10791 | 3.70k | if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) { |
10792 | 3.11k | goto fail; |
10793 | 3.11k | } |
10794 | 585 | zend_hash_index_update(Z_ARRVAL_P(result), lval, value); |
10795 | 585 | break; |
10796 | 3.70k | } |
10797 | 333 | case IS_FALSE: |
10798 | 333 | zend_hash_index_update(Z_ARRVAL_P(result), 0, value); |
10799 | 333 | break; |
10800 | 204 | case IS_TRUE: |
10801 | 204 | zend_hash_index_update(Z_ARRVAL_P(result), 1, value); |
10802 | 204 | break; |
10803 | 77 | case IS_NULL: |
10804 | | /* Null key will generate a warning at run-time. */ |
10805 | 77 | goto fail; |
10806 | 1 | default: |
10807 | 1 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type"); |
10808 | 23.6k | } |
10809 | 311k | } else if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) { |
10810 | 3.20k | fail: |
10811 | 3.20k | zval_ptr_dtor_nogc(value); |
10812 | 3.20k | zval_ptr_dtor(result); |
10813 | 3.20k | return 0; |
10814 | 9 | } |
10815 | 334k | } |
10816 | | |
10817 | 78.7k | return true; |
10818 | 81.9k | } |
10819 | | /* }}} */ |
10820 | | |
10821 | | static void zend_compile_binary_op(znode *result, zend_ast *ast) /* {{{ */ |
10822 | 3.76M | { |
10823 | 3.76M | zend_ast *left_ast = ast->child[0]; |
10824 | 3.76M | zend_ast *right_ast = ast->child[1]; |
10825 | 3.76M | uint32_t opcode = ast->attr; |
10826 | | |
10827 | 3.76M | znode left_node, right_node; |
10828 | | |
10829 | 3.76M | zend_compile_expr(&left_node, left_ast); |
10830 | 3.76M | zend_compile_expr(&right_node, right_ast); |
10831 | | |
10832 | 3.76M | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10833 | 757k | if (zend_try_ct_eval_binary_op(&result->u.constant, opcode, |
10834 | 757k | &left_node.u.constant, &right_node.u.constant) |
10835 | 757k | ) { |
10836 | 723k | result->op_type = IS_CONST; |
10837 | 723k | zval_ptr_dtor(&left_node.u.constant); |
10838 | 723k | zval_ptr_dtor(&right_node.u.constant); |
10839 | 723k | return; |
10840 | 723k | } |
10841 | 757k | } |
10842 | | |
10843 | 3.04M | do { |
10844 | 3.04M | if (opcode == ZEND_IS_IDENTICAL || opcode == ZEND_IS_NOT_IDENTICAL) { |
10845 | | /* 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) */ |
10846 | 132k | if (left_node.op_type == IS_CONST) { |
10847 | 56.8k | if (Z_TYPE(left_node.u.constant) <= IS_TRUE && Z_TYPE(left_node.u.constant) >= IS_NULL) { |
10848 | 21.5k | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &right_node, NULL); |
10849 | 21.5k | opline->extended_value = |
10850 | 21.5k | (opcode == ZEND_IS_IDENTICAL) ? |
10851 | 283 | (1 << Z_TYPE(left_node.u.constant)) : |
10852 | 21.5k | (MAY_BE_ANY - (1 << Z_TYPE(left_node.u.constant))); |
10853 | 21.5k | return; |
10854 | 21.5k | } |
10855 | 75.5k | } else if (right_node.op_type == IS_CONST) { |
10856 | 73.0k | if (Z_TYPE(right_node.u.constant) <= IS_TRUE && Z_TYPE(right_node.u.constant) >= IS_NULL) { |
10857 | 70.0k | zend_op *opline = zend_emit_op_tmp(result, ZEND_TYPE_CHECK, &left_node, NULL); |
10858 | 70.0k | opline->extended_value = |
10859 | 70.0k | (opcode == ZEND_IS_IDENTICAL) ? |
10860 | 34.9k | (1 << Z_TYPE(right_node.u.constant)) : |
10861 | 70.0k | (MAY_BE_ANY - (1 << Z_TYPE(right_node.u.constant))); |
10862 | 70.0k | return; |
10863 | 70.0k | } |
10864 | 73.0k | } |
10865 | 2.91M | } else if (opcode == ZEND_CONCAT) { |
10866 | | /* convert constant operands to strings at compile-time */ |
10867 | 176k | if (left_node.op_type == IS_CONST) { |
10868 | 16.1k | if (Z_TYPE(left_node.u.constant) == IS_ARRAY) { |
10869 | 76 | zend_emit_op_tmp(&left_node, ZEND_CAST, &left_node, NULL)->extended_value = IS_STRING; |
10870 | 16.0k | } else { |
10871 | 16.0k | convert_to_string(&left_node.u.constant); |
10872 | 16.0k | } |
10873 | 16.1k | } |
10874 | 176k | if (right_node.op_type == IS_CONST) { |
10875 | 43.3k | if (Z_TYPE(right_node.u.constant) == IS_ARRAY) { |
10876 | 260 | zend_emit_op_tmp(&right_node, ZEND_CAST, &right_node, NULL)->extended_value = IS_STRING; |
10877 | 43.0k | } else { |
10878 | 43.0k | convert_to_string(&right_node.u.constant); |
10879 | 43.0k | } |
10880 | 43.3k | } |
10881 | 176k | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10882 | 0 | opcode = ZEND_FAST_CONCAT; |
10883 | 0 | } |
10884 | 176k | } |
10885 | 2.95M | zend_emit_op_tmp(result, opcode, &left_node, &right_node); |
10886 | 2.95M | } while (0); |
10887 | 3.04M | } |
10888 | | /* }}} */ |
10889 | | |
10890 | | /* We do not use zend_compile_binary_op for this because we want to retain the left-to-right |
10891 | | * evaluation order. */ |
10892 | | static void zend_compile_greater(znode *result, zend_ast *ast) /* {{{ */ |
10893 | 386k | { |
10894 | 386k | zend_ast *left_ast = ast->child[0]; |
10895 | 386k | zend_ast *right_ast = ast->child[1]; |
10896 | 386k | znode left_node, right_node; |
10897 | | |
10898 | 386k | ZEND_ASSERT(ast->kind == ZEND_AST_GREATER || ast->kind == ZEND_AST_GREATER_EQUAL); |
10899 | | |
10900 | 386k | zend_compile_expr(&left_node, left_ast); |
10901 | 386k | zend_compile_expr(&right_node, right_ast); |
10902 | | |
10903 | 386k | if (left_node.op_type == IS_CONST && right_node.op_type == IS_CONST) { |
10904 | 16.8k | result->op_type = IS_CONST; |
10905 | 16.8k | zend_ct_eval_greater(&result->u.constant, ast->kind, |
10906 | 16.8k | &left_node.u.constant, &right_node.u.constant); |
10907 | 16.8k | zval_ptr_dtor(&left_node.u.constant); |
10908 | 16.8k | zval_ptr_dtor(&right_node.u.constant); |
10909 | 16.8k | return; |
10910 | 16.8k | } |
10911 | | |
10912 | 369k | zend_emit_op_tmp(result, |
10913 | 369k | ast->kind == ZEND_AST_GREATER ? ZEND_IS_SMALLER : ZEND_IS_SMALLER_OR_EQUAL, |
10914 | 369k | &right_node, &left_node); |
10915 | 369k | } |
10916 | | /* }}} */ |
10917 | | |
10918 | | static void zend_compile_unary_op(znode *result, const zend_ast *ast) /* {{{ */ |
10919 | 1.38M | { |
10920 | 1.38M | zend_ast *expr_ast = ast->child[0]; |
10921 | 1.38M | uint32_t opcode = ast->attr; |
10922 | | |
10923 | 1.38M | znode expr_node; |
10924 | 1.38M | zend_compile_expr(&expr_node, expr_ast); |
10925 | | |
10926 | 1.38M | if (expr_node.op_type == IS_CONST |
10927 | 385k | && zend_try_ct_eval_unary_op(&result->u.constant, opcode, &expr_node.u.constant)) { |
10928 | 363k | result->op_type = IS_CONST; |
10929 | 363k | zval_ptr_dtor(&expr_node.u.constant); |
10930 | 363k | return; |
10931 | 363k | } |
10932 | | |
10933 | 1.02M | zend_emit_op_tmp(result, opcode, &expr_node, NULL); |
10934 | 1.02M | } |
10935 | | /* }}} */ |
10936 | | |
10937 | | static void zend_compile_unary_pm(znode *result, zend_ast *ast) /* {{{ */ |
10938 | 188k | { |
10939 | 188k | zend_ast *expr_ast = ast->child[0]; |
10940 | 188k | znode expr_node, right_node; |
10941 | | |
10942 | 188k | ZEND_ASSERT(ast->kind == ZEND_AST_UNARY_PLUS || ast->kind == ZEND_AST_UNARY_MINUS); |
10943 | | |
10944 | 188k | zend_compile_expr(&expr_node, expr_ast); |
10945 | | |
10946 | 188k | if (expr_node.op_type == IS_CONST |
10947 | 63.0k | && zend_try_ct_eval_unary_pm(&result->u.constant, ast->kind, &expr_node.u.constant)) { |
10948 | 54.4k | result->op_type = IS_CONST; |
10949 | 54.4k | zval_ptr_dtor(&expr_node.u.constant); |
10950 | 54.4k | return; |
10951 | 54.4k | } |
10952 | | |
10953 | 133k | right_node.op_type = IS_CONST; |
10954 | 133k | ZVAL_LONG(&right_node.u.constant, (ast->kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); |
10955 | 133k | zend_emit_op_tmp(result, ZEND_MUL, &expr_node, &right_node); |
10956 | 133k | } |
10957 | | /* }}} */ |
10958 | | |
10959 | | static void zend_compile_short_circuiting(znode *result, zend_ast *ast) /* {{{ */ |
10960 | 15.1k | { |
10961 | 15.1k | zend_ast *left_ast = ast->child[0]; |
10962 | 15.1k | zend_ast *right_ast = ast->child[1]; |
10963 | | |
10964 | 15.1k | znode left_node, right_node; |
10965 | 15.1k | zend_op *opline_jmpz, *opline_bool; |
10966 | 15.1k | uint32_t opnum_jmpz; |
10967 | | |
10968 | 15.1k | ZEND_ASSERT(ast->kind == ZEND_AST_AND || ast->kind == ZEND_AST_OR); |
10969 | | |
10970 | 15.1k | zend_compile_expr(&left_node, left_ast); |
10971 | | |
10972 | 15.1k | if (left_node.op_type == IS_CONST) { |
10973 | 2.85k | if ((ast->kind == ZEND_AST_AND && !zend_is_true(&left_node.u.constant)) |
10974 | 2.01k | || (ast->kind == ZEND_AST_OR && zend_is_true(&left_node.u.constant))) { |
10975 | 1.44k | result->op_type = IS_CONST; |
10976 | 1.44k | ZVAL_BOOL(&result->u.constant, zend_is_true(&left_node.u.constant)); |
10977 | 1.44k | } else { |
10978 | 1.41k | zend_compile_expr(&right_node, right_ast); |
10979 | | |
10980 | 1.41k | if (right_node.op_type == IS_CONST) { |
10981 | 656 | result->op_type = IS_CONST; |
10982 | 656 | ZVAL_BOOL(&result->u.constant, zend_is_true(&right_node.u.constant)); |
10983 | | |
10984 | 656 | zval_ptr_dtor(&right_node.u.constant); |
10985 | 763 | } else { |
10986 | 763 | zend_emit_op_tmp(result, ZEND_BOOL, &right_node, NULL); |
10987 | 763 | } |
10988 | 1.41k | } |
10989 | | |
10990 | 2.85k | zval_ptr_dtor(&left_node.u.constant); |
10991 | 2.85k | return; |
10992 | 2.85k | } |
10993 | | |
10994 | 12.2k | opnum_jmpz = get_next_op_number(); |
10995 | 12.2k | opline_jmpz = zend_emit_op(NULL, ast->kind == ZEND_AST_AND ? ZEND_JMPZ_EX : ZEND_JMPNZ_EX, |
10996 | 12.2k | &left_node, NULL); |
10997 | | |
10998 | 12.2k | if (left_node.op_type == IS_TMP_VAR) { |
10999 | 11.6k | SET_NODE(opline_jmpz->result, &left_node); |
11000 | 11.6k | GET_NODE(result, opline_jmpz->result); |
11001 | 11.6k | } else { |
11002 | 564 | zend_make_tmp_result(result, opline_jmpz); |
11003 | 564 | } |
11004 | | |
11005 | 12.2k | zend_compile_expr(&right_node, right_ast); |
11006 | | |
11007 | 12.2k | opline_bool = zend_emit_op(NULL, ZEND_BOOL, &right_node, NULL); |
11008 | 12.2k | SET_NODE(opline_bool->result, result); |
11009 | | |
11010 | 12.2k | zend_update_jump_target_to_next(opnum_jmpz); |
11011 | 12.2k | } |
11012 | | /* }}} */ |
11013 | | |
11014 | | static void zend_compile_post_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
11015 | 14.9k | { |
11016 | 14.9k | zend_ast *var_ast = ast->child[0]; |
11017 | 14.9k | ZEND_ASSERT(ast->kind == ZEND_AST_POST_INC || ast->kind == ZEND_AST_POST_DEC); |
11018 | | |
11019 | 14.9k | zend_ensure_writable_variable(var_ast); |
11020 | | |
11021 | 14.9k | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
11022 | 1.21k | zend_op *opline = zend_compile_prop(NULL, var_ast, BP_VAR_RW, false); |
11023 | 1.21k | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_OBJ : ZEND_POST_DEC_OBJ; |
11024 | 1.21k | zend_make_tmp_result(result, opline); |
11025 | 13.7k | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
11026 | 488 | zend_op *opline = zend_compile_static_prop(NULL, var_ast, BP_VAR_RW, false, false); |
11027 | 488 | opline->opcode = ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC_STATIC_PROP : ZEND_POST_DEC_STATIC_PROP; |
11028 | 488 | zend_make_tmp_result(result, opline); |
11029 | 13.2k | } else { |
11030 | 13.2k | znode var_node; |
11031 | 13.2k | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
11032 | 13.2k | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
11033 | 238 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
11034 | 238 | } |
11035 | 13.2k | zend_emit_op_tmp(result, ast->kind == ZEND_AST_POST_INC ? ZEND_POST_INC : ZEND_POST_DEC, |
11036 | 13.2k | &var_node, NULL); |
11037 | 13.2k | } |
11038 | 14.9k | } |
11039 | | /* }}} */ |
11040 | | |
11041 | | static void zend_compile_pre_incdec(znode *result, const zend_ast *ast) /* {{{ */ |
11042 | 4.66k | { |
11043 | 4.66k | zend_ast *var_ast = ast->child[0]; |
11044 | 4.66k | ZEND_ASSERT(ast->kind == ZEND_AST_PRE_INC || ast->kind == ZEND_AST_PRE_DEC); |
11045 | | |
11046 | 4.66k | zend_ensure_writable_variable(var_ast); |
11047 | | |
11048 | 4.66k | if (var_ast->kind == ZEND_AST_PROP || var_ast->kind == ZEND_AST_NULLSAFE_PROP) { |
11049 | 1.06k | zend_op *opline = zend_compile_prop(result, var_ast, BP_VAR_RW, false); |
11050 | 1.06k | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_OBJ : ZEND_PRE_DEC_OBJ; |
11051 | 1.06k | opline->result_type = IS_TMP_VAR; |
11052 | 1.06k | result->op_type = IS_TMP_VAR; |
11053 | 3.59k | } else if (var_ast->kind == ZEND_AST_STATIC_PROP) { |
11054 | 83 | zend_op *opline = zend_compile_static_prop(result, var_ast, BP_VAR_RW, false, false); |
11055 | 83 | opline->opcode = ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC_STATIC_PROP : ZEND_PRE_DEC_STATIC_PROP; |
11056 | 83 | opline->result_type = IS_TMP_VAR; |
11057 | 83 | result->op_type = IS_TMP_VAR; |
11058 | 3.51k | } else { |
11059 | 3.51k | znode var_node; |
11060 | 3.51k | zend_op *opline = zend_compile_var(&var_node, var_ast, BP_VAR_RW, false); |
11061 | 3.51k | if (opline && opline->opcode == ZEND_FETCH_DIM_RW) { |
11062 | 142 | opline->extended_value = ZEND_FETCH_DIM_INCDEC; |
11063 | 142 | } |
11064 | 3.51k | zend_emit_op_tmp(result, ast->kind == ZEND_AST_PRE_INC ? ZEND_PRE_INC : ZEND_PRE_DEC, |
11065 | 3.51k | &var_node, NULL); |
11066 | 3.51k | } |
11067 | 4.66k | } |
11068 | | /* }}} */ |
11069 | | |
11070 | | static void zend_compile_cast(znode *result, const zend_ast *ast) /* {{{ */ |
11071 | 3.92k | { |
11072 | 3.92k | zend_ast *expr_ast = ast->child[0]; |
11073 | 3.92k | znode expr_node; |
11074 | 3.92k | zend_op *opline; |
11075 | | |
11076 | 3.92k | zend_compile_expr(&expr_node, expr_ast); |
11077 | | |
11078 | 3.92k | if (ast->attr == _IS_BOOL) { |
11079 | 281 | opline = zend_emit_op_tmp(result, ZEND_BOOL, &expr_node, NULL); |
11080 | 3.64k | } else if (ast->attr == IS_NULL) { |
11081 | 12 | zend_error(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
11082 | 3.63k | } else { |
11083 | 3.63k | opline = zend_emit_op_tmp(result, ZEND_CAST, &expr_node, NULL); |
11084 | 3.63k | opline->extended_value = ast->attr; |
11085 | 3.63k | } |
11086 | 3.92k | } |
11087 | | /* }}} */ |
11088 | | |
11089 | | static void zend_compile_shorthand_conditional(znode *result, zend_ast *ast) /* {{{ */ |
11090 | 5.33k | { |
11091 | 5.33k | zend_ast *cond_ast = ast->child[0]; |
11092 | 5.33k | zend_ast *false_ast = ast->child[2]; |
11093 | | |
11094 | 5.33k | znode cond_node, false_node; |
11095 | 5.33k | zend_op *opline_qm_assign; |
11096 | 5.33k | uint32_t opnum_jmp_set; |
11097 | | |
11098 | 5.33k | ZEND_ASSERT(ast->child[1] == NULL); |
11099 | | |
11100 | 5.33k | zend_compile_expr(&cond_node, cond_ast); |
11101 | | |
11102 | 5.33k | opnum_jmp_set = get_next_op_number(); |
11103 | 5.33k | zend_emit_op_tmp(result, ZEND_JMP_SET, &cond_node, NULL); |
11104 | | |
11105 | 5.33k | zend_compile_expr(&false_node, false_ast); |
11106 | | |
11107 | 5.33k | opline_qm_assign = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
11108 | 5.33k | SET_NODE(opline_qm_assign->result, result); |
11109 | | |
11110 | 5.33k | zend_update_jump_target_to_next(opnum_jmp_set); |
11111 | 5.33k | } |
11112 | | /* }}} */ |
11113 | | |
11114 | | static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */ |
11115 | 14.5k | { |
11116 | 14.5k | zend_ast *cond_ast = ast->child[0]; |
11117 | 14.5k | zend_ast *true_ast = ast->child[1]; |
11118 | 14.5k | zend_ast *false_ast = ast->child[2]; |
11119 | | |
11120 | 14.5k | znode cond_node, true_node, false_node; |
11121 | 14.5k | zend_op *opline_qm_assign2; |
11122 | 14.5k | uint32_t opnum_jmpz, opnum_jmp; |
11123 | | |
11124 | 14.5k | if (cond_ast->kind == ZEND_AST_CONDITIONAL |
11125 | 1.52k | && cond_ast->attr != ZEND_PARENTHESIZED_CONDITIONAL) { |
11126 | 1.01k | if (cond_ast->child[1]) { |
11127 | 24 | if (true_ast) { |
11128 | 10 | zend_error(E_COMPILE_ERROR, |
11129 | 10 | "Unparenthesized `a ? b : c ? d : e` is not supported. " |
11130 | 10 | "Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`"); |
11131 | 14 | } else { |
11132 | 14 | zend_error(E_COMPILE_ERROR, |
11133 | 14 | "Unparenthesized `a ? b : c ?: d` is not supported. " |
11134 | 14 | "Use either `(a ? b : c) ?: d` or `a ? b : (c ?: d)`"); |
11135 | 14 | } |
11136 | 987 | } else { |
11137 | 987 | if (true_ast) { |
11138 | 5 | zend_error(E_COMPILE_ERROR, |
11139 | 5 | "Unparenthesized `a ?: b ? c : d` is not supported. " |
11140 | 5 | "Use either `(a ?: b) ? c : d` or `a ?: (b ? c : d)`"); |
11141 | 982 | } else { |
11142 | | /* This case is harmless: (a ?: b) ?: c always produces the same result |
11143 | | * as a ?: (b ?: c). */ |
11144 | 982 | } |
11145 | 987 | } |
11146 | 1.01k | } |
11147 | | |
11148 | 14.5k | if (!true_ast) { |
11149 | 5.33k | zend_compile_shorthand_conditional(result, ast); |
11150 | 5.33k | return; |
11151 | 5.33k | } |
11152 | | |
11153 | 9.18k | zend_compile_expr(&cond_node, cond_ast); |
11154 | | |
11155 | 9.18k | opnum_jmpz = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0); |
11156 | | |
11157 | 9.18k | zend_compile_expr(&true_node, true_ast); |
11158 | | |
11159 | 9.18k | zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &true_node, NULL); |
11160 | | |
11161 | 9.18k | opnum_jmp = zend_emit_jump(0); |
11162 | | |
11163 | 9.18k | zend_update_jump_target_to_next(opnum_jmpz); |
11164 | | |
11165 | 9.18k | zend_compile_expr(&false_node, false_ast); |
11166 | | |
11167 | 9.18k | opline_qm_assign2 = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &false_node, NULL); |
11168 | 9.18k | SET_NODE(opline_qm_assign2->result, result); |
11169 | | |
11170 | 9.18k | zend_update_jump_target_to_next(opnum_jmp); |
11171 | 9.18k | } |
11172 | | /* }}} */ |
11173 | | |
11174 | | static void zend_compile_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
11175 | 1.21M | { |
11176 | 1.21M | zend_ast *expr_ast = ast->child[0]; |
11177 | 1.21M | zend_ast *default_ast = ast->child[1]; |
11178 | | |
11179 | 1.21M | znode expr_node, default_node; |
11180 | 1.21M | zend_op *opline; |
11181 | 1.21M | uint32_t opnum; |
11182 | | |
11183 | 1.21M | zend_compile_var(&expr_node, expr_ast, BP_VAR_IS, false); |
11184 | | |
11185 | 1.21M | opnum = get_next_op_number(); |
11186 | 1.21M | zend_emit_op_tmp(result, ZEND_COALESCE, &expr_node, NULL); |
11187 | | |
11188 | 1.21M | zend_compile_expr(&default_node, default_ast); |
11189 | | |
11190 | 1.21M | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &default_node, NULL); |
11191 | 1.21M | SET_NODE(opline->result, result); |
11192 | | |
11193 | 1.21M | opline = &CG(active_op_array)->opcodes[opnum]; |
11194 | 1.21M | opline->op2.opline_num = get_next_op_number(); |
11195 | 1.21M | } |
11196 | | /* }}} */ |
11197 | | |
11198 | 351k | static void znode_dtor(zval *zv) { |
11199 | 351k | znode *node = Z_PTR_P(zv); |
11200 | 351k | if (node->op_type == IS_CONST) { |
11201 | 187k | zval_ptr_dtor_nogc(&node->u.constant); |
11202 | 187k | } |
11203 | 351k | efree(node); |
11204 | 351k | } |
11205 | | |
11206 | | static void zend_compile_assign_coalesce(znode *result, zend_ast *ast) /* {{{ */ |
11207 | 70.1k | { |
11208 | 70.1k | zend_ast *var_ast = ast->child[0]; |
11209 | 70.1k | zend_ast *default_ast = ast->child[1]; |
11210 | | |
11211 | 70.1k | znode var_node_is, var_node_w, default_node, assign_node, *node; |
11212 | 70.1k | zend_op *opline; |
11213 | 70.1k | uint32_t coalesce_opnum; |
11214 | 70.1k | bool need_frees = false; |
11215 | | |
11216 | | /* Remember expressions compiled during the initial BP_VAR_IS lookup, |
11217 | | * to avoid double-evaluation when we compile again with BP_VAR_W. */ |
11218 | 70.1k | HashTable *orig_memoized_exprs = CG(memoized_exprs); |
11219 | 70.1k | const zend_memoize_mode orig_memoize_mode = CG(memoize_mode); |
11220 | | |
11221 | 70.1k | zend_ensure_writable_variable(var_ast); |
11222 | 70.1k | if (is_this_fetch(var_ast)) { |
11223 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot re-assign $this"); |
11224 | 5 | } |
11225 | | |
11226 | 70.1k | ALLOC_HASHTABLE(CG(memoized_exprs)); |
11227 | 70.1k | zend_hash_init(CG(memoized_exprs), 0, NULL, znode_dtor, 0); |
11228 | | |
11229 | 70.1k | CG(memoize_mode) = ZEND_MEMOIZE_COMPILE; |
11230 | 70.1k | zend_compile_var(&var_node_is, var_ast, BP_VAR_IS, false); |
11231 | | |
11232 | 70.1k | coalesce_opnum = get_next_op_number(); |
11233 | 70.1k | zend_emit_op_tmp(result, ZEND_COALESCE, &var_node_is, NULL); |
11234 | | |
11235 | 70.1k | CG(memoize_mode) = ZEND_MEMOIZE_NONE; |
11236 | 70.1k | if (var_ast->kind == ZEND_AST_DIM) { |
11237 | 60.8k | zend_compile_expr_with_potential_assign_to_self(&default_node, default_ast, var_ast); |
11238 | 60.8k | } else { |
11239 | 9.34k | zend_compile_expr(&default_node, default_ast); |
11240 | 9.34k | } |
11241 | | |
11242 | 70.1k | CG(memoize_mode) = ZEND_MEMOIZE_FETCH; |
11243 | 70.1k | zend_compile_var(&var_node_w, var_ast, BP_VAR_W, false); |
11244 | | |
11245 | | /* Reproduce some of the zend_compile_assign() opcode fixup logic here. */ |
11246 | 70.1k | opline = &CG(active_op_array)->opcodes[CG(active_op_array)->last-1]; |
11247 | | /* Treat $GLOBALS['x'] assignment like assignment to variable. */ |
11248 | 70.1k | zend_ast_kind kind = is_global_var_fetch(var_ast) ? ZEND_AST_VAR : var_ast->kind; |
11249 | 70.1k | switch (kind) { |
11250 | 1.90k | case ZEND_AST_VAR: |
11251 | 1.90k | zend_emit_op_tmp(&assign_node, ZEND_ASSIGN, &var_node_w, &default_node); |
11252 | 1.90k | break; |
11253 | 988 | case ZEND_AST_STATIC_PROP: |
11254 | 988 | opline->opcode = ZEND_ASSIGN_STATIC_PROP; |
11255 | 988 | opline->result_type = IS_TMP_VAR; |
11256 | 988 | var_node_w.op_type = IS_TMP_VAR; |
11257 | 988 | zend_emit_op_data(&default_node); |
11258 | 988 | assign_node = var_node_w; |
11259 | 988 | break; |
11260 | 60.3k | case ZEND_AST_DIM: |
11261 | 60.3k | opline->opcode = ZEND_ASSIGN_DIM; |
11262 | 60.3k | opline->result_type = IS_TMP_VAR; |
11263 | 60.3k | var_node_w.op_type = IS_TMP_VAR; |
11264 | 60.3k | zend_emit_op_data(&default_node); |
11265 | 60.3k | assign_node = var_node_w; |
11266 | 60.3k | break; |
11267 | 6.82k | case ZEND_AST_PROP: |
11268 | 6.82k | case ZEND_AST_NULLSAFE_PROP: |
11269 | 6.82k | opline->opcode = ZEND_ASSIGN_OBJ; |
11270 | 6.82k | opline->result_type = IS_TMP_VAR; |
11271 | 6.82k | var_node_w.op_type = IS_TMP_VAR; |
11272 | 6.82k | zend_emit_op_data(&default_node); |
11273 | 6.82k | assign_node = var_node_w; |
11274 | 6.82k | break; |
11275 | 0 | default: ZEND_UNREACHABLE(); |
11276 | 70.1k | } |
11277 | | |
11278 | 70.0k | opline = zend_emit_op_tmp(NULL, ZEND_QM_ASSIGN, &assign_node, NULL); |
11279 | 70.0k | SET_NODE(opline->result, result); |
11280 | | |
11281 | 565k | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
11282 | 565k | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
11283 | 60.3k | need_frees = true; |
11284 | 60.3k | break; |
11285 | 60.3k | } |
11286 | 565k | } ZEND_HASH_FOREACH_END(); |
11287 | | |
11288 | | /* Free DUPed expressions if there are any */ |
11289 | 70.0k | if (need_frees) { |
11290 | 60.3k | uint32_t jump_opnum = zend_emit_jump(0); |
11291 | 60.3k | zend_update_jump_target_to_next(coalesce_opnum); |
11292 | 391k | ZEND_HASH_FOREACH_PTR(CG(memoized_exprs), node) { |
11293 | 391k | if (node->op_type == IS_TMP_VAR || node->op_type == IS_VAR) { |
11294 | 163k | zend_emit_op(NULL, ZEND_FREE, node, NULL); |
11295 | 163k | } |
11296 | 391k | } ZEND_HASH_FOREACH_END(); |
11297 | 60.3k | zend_update_jump_target_to_next(jump_opnum); |
11298 | 60.3k | } else { |
11299 | 9.66k | zend_update_jump_target_to_next(coalesce_opnum); |
11300 | 9.66k | } |
11301 | | |
11302 | 70.0k | zend_hash_destroy(CG(memoized_exprs)); |
11303 | 70.0k | FREE_HASHTABLE(CG(memoized_exprs)); |
11304 | 70.0k | CG(memoized_exprs) = orig_memoized_exprs; |
11305 | 70.0k | CG(memoize_mode) = orig_memoize_mode; |
11306 | 70.0k | } |
11307 | | /* }}} */ |
11308 | | |
11309 | | static void zend_compile_print(znode *result, const zend_ast *ast) /* {{{ */ |
11310 | 8.14k | { |
11311 | 8.14k | zend_op *opline; |
11312 | 8.14k | zend_ast *expr_ast = ast->child[0]; |
11313 | | |
11314 | 8.14k | znode expr_node; |
11315 | 8.14k | zend_compile_expr(&expr_node, expr_ast); |
11316 | | |
11317 | 8.14k | opline = zend_emit_op(NULL, ZEND_ECHO, &expr_node, NULL); |
11318 | 8.14k | opline->extended_value = 1; |
11319 | | |
11320 | 8.14k | result->op_type = IS_CONST; |
11321 | 8.14k | ZVAL_LONG(&result->u.constant, 1); |
11322 | 8.14k | } |
11323 | | /* }}} */ |
11324 | | |
11325 | | static void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */ |
11326 | 237k | { |
11327 | 237k | zend_ast *value_ast = ast->child[0]; |
11328 | 237k | zend_ast *key_ast = ast->child[1]; |
11329 | | |
11330 | 237k | znode value_node, key_node; |
11331 | 237k | znode *value_node_ptr = NULL, *key_node_ptr = NULL; |
11332 | 237k | zend_op *opline; |
11333 | 237k | bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0; |
11334 | | |
11335 | 237k | zend_mark_function_as_generator(); |
11336 | | |
11337 | 237k | if (key_ast) { |
11338 | 1.00k | zend_compile_expr(&key_node, key_ast); |
11339 | 1.00k | key_node_ptr = &key_node; |
11340 | 1.00k | } |
11341 | | |
11342 | 237k | if (value_ast) { |
11343 | 235k | if (returns_by_ref && zend_is_variable_or_call(value_ast)) { |
11344 | 126k | zend_assert_not_short_circuited(value_ast); |
11345 | 126k | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11346 | 126k | } else { |
11347 | 109k | zend_compile_expr(&value_node, value_ast); |
11348 | 109k | } |
11349 | 235k | value_node_ptr = &value_node; |
11350 | 235k | } |
11351 | | |
11352 | 237k | opline = zend_emit_op_tmp(result, ZEND_YIELD, value_node_ptr, key_node_ptr); |
11353 | | |
11354 | 237k | if (value_ast && returns_by_ref && zend_is_call(value_ast)) { |
11355 | 125k | opline->extended_value = ZEND_RETURNS_FUNCTION; |
11356 | 125k | } |
11357 | 237k | } |
11358 | | /* }}} */ |
11359 | | |
11360 | | static void zend_compile_yield_from(znode *result, const zend_ast *ast) /* {{{ */ |
11361 | 1.09k | { |
11362 | 1.09k | zend_ast *expr_ast = ast->child[0]; |
11363 | 1.09k | znode expr_node; |
11364 | | |
11365 | 1.09k | zend_mark_function_as_generator(); |
11366 | | |
11367 | 1.09k | if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) { |
11368 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
11369 | 6 | "Cannot use \"yield from\" inside a by-reference generator"); |
11370 | 6 | } |
11371 | | |
11372 | 1.08k | zend_compile_expr(&expr_node, expr_ast); |
11373 | 1.08k | zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL); |
11374 | 1.08k | } |
11375 | | /* }}} */ |
11376 | | |
11377 | | static void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */ |
11378 | 1.02k | { |
11379 | 1.02k | zend_ast *obj_ast = ast->child[0]; |
11380 | 1.02k | zend_ast *class_ast = ast->child[1]; |
11381 | | |
11382 | 1.02k | znode obj_node, class_node; |
11383 | 1.02k | zend_op *opline; |
11384 | | |
11385 | 1.02k | zend_compile_expr(&obj_node, obj_ast); |
11386 | 1.02k | if (obj_node.op_type == IS_CONST) { |
11387 | 84 | zend_do_free(&obj_node); |
11388 | 84 | result->op_type = IS_CONST; |
11389 | 84 | ZVAL_FALSE(&result->u.constant); |
11390 | 84 | return; |
11391 | 84 | } |
11392 | | |
11393 | 944 | zend_compile_class_ref(&class_node, class_ast, |
11394 | 944 | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION | ZEND_FETCH_CLASS_SILENT); |
11395 | | |
11396 | 944 | opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL); |
11397 | | |
11398 | 944 | if (class_node.op_type == IS_CONST) { |
11399 | 568 | opline->op2_type = IS_CONST; |
11400 | 568 | opline->op2.constant = zend_add_class_name_literal( |
11401 | 568 | Z_STR(class_node.u.constant)); |
11402 | 568 | opline->extended_value = zend_alloc_cache_slot(); |
11403 | 568 | } else { |
11404 | 376 | SET_NODE(opline->op2, &class_node); |
11405 | 376 | } |
11406 | 944 | } |
11407 | | /* }}} */ |
11408 | | |
11409 | | static void zend_compile_include_or_eval(znode *result, const zend_ast *ast) /* {{{ */ |
11410 | 8.83k | { |
11411 | 8.83k | zend_ast *expr_ast = ast->child[0]; |
11412 | 8.83k | znode expr_node; |
11413 | 8.83k | zend_op *opline; |
11414 | | |
11415 | 8.83k | zend_do_extended_fcall_begin(); |
11416 | 8.83k | zend_compile_expr(&expr_node, expr_ast); |
11417 | | |
11418 | 8.83k | opline = zend_emit_op_tmp(result, ZEND_INCLUDE_OR_EVAL, &expr_node, NULL); |
11419 | 8.83k | opline->extended_value = ast->attr; |
11420 | | |
11421 | 8.83k | zend_do_extended_fcall_end(); |
11422 | 8.83k | } |
11423 | | /* }}} */ |
11424 | | |
11425 | | static void zend_compile_isset_or_empty(znode *result, const zend_ast *ast) /* {{{ */ |
11426 | 9.02k | { |
11427 | 9.02k | zend_ast *var_ast = ast->child[0]; |
11428 | | |
11429 | 9.02k | znode var_node; |
11430 | 9.02k | zend_op *opline = NULL; |
11431 | | |
11432 | 9.02k | ZEND_ASSERT(ast->kind == ZEND_AST_ISSET || ast->kind == ZEND_AST_EMPTY); |
11433 | | |
11434 | 9.02k | if (!zend_is_variable(var_ast)) { |
11435 | 162 | if (ast->kind == ZEND_AST_EMPTY) { |
11436 | | /* empty(expr) can be transformed to !expr */ |
11437 | 134 | zend_ast *not_ast = zend_ast_create_ex(ZEND_AST_UNARY_OP, ZEND_BOOL_NOT, var_ast); |
11438 | 134 | zend_compile_expr(result, not_ast); |
11439 | 134 | return; |
11440 | 134 | } else { |
11441 | 28 | zend_error_noreturn(E_COMPILE_ERROR, |
11442 | 28 | "Cannot use isset() on the result of an expression " |
11443 | 28 | "(you can use \"null !== expression\" instead)"); |
11444 | 28 | } |
11445 | 162 | } |
11446 | | |
11447 | 8.85k | if (is_globals_fetch(var_ast)) { |
11448 | 381 | result->op_type = IS_CONST; |
11449 | 381 | ZVAL_BOOL(&result->u.constant, ast->kind == ZEND_AST_ISSET); |
11450 | 381 | return; |
11451 | 381 | } |
11452 | | |
11453 | 8.47k | if (is_global_var_fetch(var_ast)) { |
11454 | 291 | if (!var_ast->child[1]) { |
11455 | 6 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
11456 | 6 | } |
11457 | | |
11458 | 285 | zend_compile_expr(&var_node, var_ast->child[1]); |
11459 | 285 | if (var_node.op_type == IS_CONST) { |
11460 | 194 | convert_to_string(&var_node.u.constant); |
11461 | 194 | } |
11462 | | |
11463 | 285 | opline = zend_emit_op_tmp(result, ZEND_ISSET_ISEMPTY_VAR, &var_node, NULL); |
11464 | 285 | opline->extended_value = |
11465 | 285 | ZEND_FETCH_GLOBAL | (ast->kind == ZEND_AST_EMPTY ? ZEND_ISEMPTY : 0); |
11466 | 285 | return; |
11467 | 291 | } |
11468 | | |
11469 | 8.18k | zend_short_circuiting_mark_inner(var_ast); |
11470 | 8.18k | switch (var_ast->kind) { |
11471 | 2.41k | case ZEND_AST_VAR: |
11472 | 2.41k | if (is_this_fetch(var_ast)) { |
11473 | 150 | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_THIS, NULL, NULL); |
11474 | 150 | CG(active_op_array)->fn_flags |= ZEND_ACC_USES_THIS; |
11475 | 2.26k | } else if (zend_try_compile_cv(&var_node, var_ast, BP_VAR_IS) == SUCCESS) { |
11476 | 1.88k | opline = zend_emit_op(result, ZEND_ISSET_ISEMPTY_CV, &var_node, NULL); |
11477 | 1.88k | } else { |
11478 | 377 | opline = zend_compile_simple_var_no_cv(result, var_ast, BP_VAR_IS, false); |
11479 | 377 | opline->opcode = ZEND_ISSET_ISEMPTY_VAR; |
11480 | 377 | } |
11481 | 2.41k | break; |
11482 | 3.84k | case ZEND_AST_DIM: |
11483 | 3.84k | opline = zend_compile_dim(result, var_ast, BP_VAR_IS, /* by_ref */ false); |
11484 | 3.84k | opline->opcode = ZEND_ISSET_ISEMPTY_DIM_OBJ; |
11485 | 3.84k | break; |
11486 | 1.49k | case ZEND_AST_PROP: |
11487 | 1.62k | case ZEND_AST_NULLSAFE_PROP: |
11488 | 1.62k | opline = zend_compile_prop(result, var_ast, BP_VAR_IS, false); |
11489 | 1.62k | opline->opcode = ZEND_ISSET_ISEMPTY_PROP_OBJ; |
11490 | 1.62k | break; |
11491 | 304 | case ZEND_AST_STATIC_PROP: |
11492 | 304 | opline = zend_compile_static_prop(result, var_ast, BP_VAR_IS, false, false); |
11493 | 304 | opline->opcode = ZEND_ISSET_ISEMPTY_STATIC_PROP; |
11494 | 304 | break; |
11495 | 0 | default: ZEND_UNREACHABLE(); |
11496 | 8.18k | } |
11497 | | |
11498 | 8.17k | result->op_type = opline->result_type = IS_TMP_VAR; |
11499 | 8.17k | if (!(ast->kind == ZEND_AST_ISSET)) { |
11500 | 1.75k | opline->extended_value |= ZEND_ISEMPTY; |
11501 | 1.75k | } |
11502 | 8.17k | } |
11503 | | /* }}} */ |
11504 | | |
11505 | | static void zend_compile_silence(znode *result, const zend_ast *ast) /* {{{ */ |
11506 | 11.0M | { |
11507 | 11.0M | zend_ast *expr_ast = ast->child[0]; |
11508 | 11.0M | znode silence_node; |
11509 | | |
11510 | 11.0M | zend_emit_op_tmp(&silence_node, ZEND_BEGIN_SILENCE, NULL, NULL); |
11511 | | |
11512 | 11.0M | if (expr_ast->kind == ZEND_AST_VAR) { |
11513 | | /* For @$var we need to force a FETCH instruction, otherwise the CV access will |
11514 | | * happen outside the silenced section. */ |
11515 | 93.2k | zend_compile_simple_var_no_cv(result, expr_ast, BP_VAR_R, false ); |
11516 | 10.9M | } else { |
11517 | 10.9M | zend_compile_expr(result, expr_ast); |
11518 | 10.9M | } |
11519 | | |
11520 | 11.0M | zend_emit_op(NULL, ZEND_END_SILENCE, &silence_node, NULL); |
11521 | 11.0M | } |
11522 | | /* }}} */ |
11523 | | |
11524 | | static void zend_compile_shell_exec(znode *result, const zend_ast *ast) /* {{{ */ |
11525 | 23.1k | { |
11526 | 23.1k | zend_ast *expr_ast = ast->child[0]; |
11527 | | |
11528 | 23.1k | zval fn_name; |
11529 | 23.1k | zend_ast *name_ast, *args_ast, *call_ast; |
11530 | | |
11531 | 23.1k | zend_error(E_DEPRECATED, "The backtick (`) operator is deprecated, use shell_exec() instead"); |
11532 | | |
11533 | 23.1k | ZVAL_STRING(&fn_name, "shell_exec"); |
11534 | 23.1k | name_ast = zend_ast_create_zval(&fn_name); |
11535 | 23.1k | args_ast = zend_ast_create_list(1, ZEND_AST_ARG_LIST, expr_ast); |
11536 | 23.1k | call_ast = zend_ast_create(ZEND_AST_CALL, name_ast, args_ast); |
11537 | | |
11538 | 23.1k | zend_compile_expr(result, call_ast); |
11539 | | |
11540 | 23.1k | zval_ptr_dtor(&fn_name); |
11541 | 23.1k | } |
11542 | | /* }}} */ |
11543 | | |
11544 | | static void zend_compile_array(znode *result, zend_ast *ast) /* {{{ */ |
11545 | 113k | { |
11546 | 113k | zend_ast_list *list = zend_ast_get_list(ast); |
11547 | 113k | zend_op *opline; |
11548 | 113k | uint32_t i, opnum_init = -1; |
11549 | 113k | bool packed = true; |
11550 | | |
11551 | 113k | if (zend_try_ct_eval_array(&result->u.constant, ast)) { |
11552 | 66.0k | result->op_type = IS_CONST; |
11553 | 66.0k | return; |
11554 | 66.0k | } |
11555 | | |
11556 | | /* Empty arrays are handled at compile-time */ |
11557 | 47.2k | ZEND_ASSERT(list->children > 0); |
11558 | | |
11559 | 161k | for (i = 0; i < list->children; ++i) { |
11560 | 114k | zend_ast *elem_ast = list->child[i]; |
11561 | 114k | zend_ast *value_ast, *key_ast; |
11562 | 114k | bool by_ref; |
11563 | 114k | znode value_node, key_node, *key_node_ptr = NULL; |
11564 | | |
11565 | 114k | if (elem_ast == NULL) { |
11566 | 0 | zend_error(E_COMPILE_ERROR, "Cannot use empty array elements in arrays"); |
11567 | 0 | } |
11568 | | |
11569 | 114k | value_ast = elem_ast->child[0]; |
11570 | | |
11571 | 114k | if (elem_ast->kind == ZEND_AST_UNPACK) { |
11572 | 2.08k | zend_compile_expr(&value_node, value_ast); |
11573 | 2.08k | if (i == 0) { |
11574 | 1.55k | opnum_init = get_next_op_number(); |
11575 | 1.55k | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); |
11576 | 1.55k | } |
11577 | 2.08k | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_UNPACK, &value_node, NULL); |
11578 | 2.08k | SET_NODE(opline->result, result); |
11579 | 2.08k | continue; |
11580 | 2.08k | } |
11581 | | |
11582 | 112k | key_ast = elem_ast->child[1]; |
11583 | 112k | by_ref = elem_ast->attr; |
11584 | | |
11585 | 112k | if (key_ast) { |
11586 | 12.8k | zend_compile_expr(&key_node, key_ast); |
11587 | 12.8k | zend_handle_numeric_op(&key_node); |
11588 | 12.8k | key_node_ptr = &key_node; |
11589 | 12.8k | } |
11590 | | |
11591 | 112k | if (by_ref) { |
11592 | 1.77k | zend_ensure_writable_variable(value_ast); |
11593 | 1.77k | zend_compile_var(&value_node, value_ast, BP_VAR_W, true); |
11594 | 110k | } else { |
11595 | 110k | zend_compile_expr(&value_node, value_ast); |
11596 | 110k | } |
11597 | | |
11598 | 112k | if (i == 0) { |
11599 | 45.5k | opnum_init = get_next_op_number(); |
11600 | 45.5k | opline = zend_emit_op_tmp(result, ZEND_INIT_ARRAY, &value_node, key_node_ptr); |
11601 | 45.5k | opline->extended_value = list->children << ZEND_ARRAY_SIZE_SHIFT; |
11602 | 66.7k | } else { |
11603 | 66.7k | opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, |
11604 | 66.7k | &value_node, key_node_ptr); |
11605 | 66.7k | SET_NODE(opline->result, result); |
11606 | 66.7k | } |
11607 | 112k | opline->extended_value |= by_ref; |
11608 | | |
11609 | 112k | if (key_ast && key_node.op_type == IS_CONST && Z_TYPE(key_node.u.constant) == IS_STRING) { |
11610 | 7.35k | packed = false; |
11611 | 7.35k | } |
11612 | 112k | } |
11613 | | |
11614 | | /* Add a flag to INIT_ARRAY if we know this array cannot be packed */ |
11615 | 47.1k | if (!packed) { |
11616 | 2.97k | ZEND_ASSERT(opnum_init != (uint32_t)-1); |
11617 | 2.97k | opline = &CG(active_op_array)->opcodes[opnum_init]; |
11618 | 2.97k | opline->extended_value |= ZEND_ARRAY_NOT_PACKED; |
11619 | 2.97k | } |
11620 | 47.1k | } |
11621 | | /* }}} */ |
11622 | | |
11623 | | static void zend_emit_fetch_constant(znode *result, zend_string *resolved_name, bool unqualified_in_namespace, uint8_t type) |
11624 | 7.40M | { |
11625 | 7.40M | zend_op *opline = zend_emit_op(result, ZEND_FETCH_CONSTANT, NULL, NULL); |
11626 | 7.40M | if (type == BP_VAR_R || type == BP_VAR_IS) { |
11627 | 7.39M | opline->result_type = IS_TMP_VAR; |
11628 | 7.39M | result->op_type = IS_TMP_VAR; |
11629 | 7.39M | } |
11630 | | |
11631 | 7.40M | opline->op2_type = IS_CONST; |
11632 | | |
11633 | 7.40M | if (unqualified_in_namespace) { |
11634 | 7.25M | opline->op1.num = IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE; |
11635 | 7.25M | opline->op2.constant = zend_add_const_name_literal(resolved_name, true); |
11636 | 7.25M | } else { |
11637 | 146k | opline->op1.num = 0; |
11638 | 146k | opline->op2.constant = zend_add_const_name_literal(resolved_name, false); |
11639 | 146k | } |
11640 | 7.40M | opline->extended_value = zend_alloc_cache_slot(); |
11641 | 7.40M | } |
11642 | | |
11643 | | static void zend_compile_const(znode *result, const zend_ast *ast, uint8_t type) /* {{{ */ |
11644 | 7.45M | { |
11645 | 7.45M | zend_ast *name_ast = ast->child[0]; |
11646 | | |
11647 | 7.45M | bool is_fully_qualified; |
11648 | 7.45M | zend_string *orig_name = zend_ast_get_str(name_ast); |
11649 | 7.45M | zend_string *resolved_name = zend_resolve_const_name(orig_name, name_ast->attr, &is_fully_qualified); |
11650 | | |
11651 | 7.45M | 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__"))) { |
11652 | 992 | zend_ast *last = CG(ast); |
11653 | | |
11654 | 1.98k | while (last && last->kind == ZEND_AST_STMT_LIST) { |
11655 | 1.04k | const zend_ast_list *list = zend_ast_get_list(last); |
11656 | 1.04k | if (list->children == 0) { |
11657 | 55 | break; |
11658 | 55 | } |
11659 | 992 | last = list->child[list->children-1]; |
11660 | 992 | } |
11661 | 992 | if (last && last->kind == ZEND_AST_HALT_COMPILER) { |
11662 | 174 | result->op_type = IS_CONST; |
11663 | 174 | ZVAL_LONG(&result->u.constant, Z_LVAL_P(zend_ast_get_zval(last->child[0]))); |
11664 | 174 | zend_string_release_ex(resolved_name, 0); |
11665 | 174 | return; |
11666 | 174 | } |
11667 | 992 | } |
11668 | | |
11669 | 7.45M | if (zend_try_ct_eval_const(&result->u.constant, resolved_name, is_fully_qualified)) { |
11670 | 53.8k | result->op_type = IS_CONST; |
11671 | 53.8k | zend_string_release_ex(resolved_name, 0); |
11672 | 53.8k | return; |
11673 | 53.8k | } |
11674 | | |
11675 | 7.40M | zend_emit_fetch_constant(result, resolved_name, |
11676 | 7.40M | !is_fully_qualified && FC(current_namespace), type); |
11677 | 7.40M | } |
11678 | | /* }}} */ |
11679 | | |
11680 | | static void zend_compile_constant(znode *result, zend_ast *ast, uint8_t type) |
11681 | 20 | { |
11682 | 20 | zend_string *name = zend_ast_get_constant_name(ast); |
11683 | | |
11684 | 20 | zend_emit_fetch_constant(result, zend_string_copy(name), |
11685 | 20 | (ast->attr & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) != 0, type); |
11686 | 20 | } |
11687 | | |
11688 | | static void zend_compile_class_const(znode *result, zend_ast *ast, uint8_t type) /* {{{ */ |
11689 | 111k | { |
11690 | 111k | zend_ast *class_ast; |
11691 | 111k | zend_ast *const_ast; |
11692 | 111k | znode class_node, const_node; |
11693 | 111k | zend_op *opline; |
11694 | | |
11695 | 111k | zend_eval_const_expr(&ast->child[0]); |
11696 | 111k | zend_eval_const_expr(&ast->child[1]); |
11697 | | |
11698 | 111k | class_ast = ast->child[0]; |
11699 | 111k | const_ast = ast->child[1]; |
11700 | | |
11701 | 111k | if (class_ast->kind == ZEND_AST_ZVAL && const_ast->kind == ZEND_AST_ZVAL) { |
11702 | 17.9k | zval *const_zv = zend_ast_get_zval(const_ast); |
11703 | 17.9k | if (Z_TYPE_P(const_zv) == IS_STRING) { |
11704 | 17.7k | zend_string *const_str = Z_STR_P(const_zv); |
11705 | 17.7k | zend_string *resolved_name = zend_resolve_class_name_ast(class_ast); |
11706 | 17.7k | if (zend_try_ct_eval_class_const(&result->u.constant, resolved_name, const_str)) { |
11707 | 397 | result->op_type = IS_CONST; |
11708 | 397 | zend_string_release_ex(resolved_name, 0); |
11709 | 397 | return; |
11710 | 397 | } |
11711 | 17.3k | zend_string_release_ex(resolved_name, 0); |
11712 | 17.3k | } |
11713 | 17.9k | } |
11714 | | |
11715 | 110k | zend_compile_class_ref(&class_node, class_ast, ZEND_FETCH_CLASS_EXCEPTION); |
11716 | | |
11717 | 110k | zend_compile_expr(&const_node, const_ast); |
11718 | | |
11719 | 110k | opline = zend_emit_op(result, ZEND_FETCH_CLASS_CONSTANT, NULL, &const_node); |
11720 | 110k | if (type == BP_VAR_R || type == BP_VAR_IS) { |
11721 | 110k | opline->result_type = IS_TMP_VAR; |
11722 | 110k | result->op_type = IS_TMP_VAR; |
11723 | 110k | } |
11724 | | |
11725 | 110k | zend_set_class_name_op1(opline, &class_node); |
11726 | | |
11727 | 110k | if (opline->op1_type == IS_CONST || opline->op2_type == IS_CONST) { |
11728 | 110k | opline->extended_value = zend_alloc_cache_slots(2); |
11729 | 110k | } |
11730 | 110k | } |
11731 | | /* }}} */ |
11732 | | |
11733 | | static void zend_compile_class_name(znode *result, const zend_ast *ast) /* {{{ */ |
11734 | 8.71k | { |
11735 | 8.71k | zend_ast *class_ast = ast->child[0]; |
11736 | | |
11737 | 8.71k | if (zend_try_compile_const_expr_resolve_class_name(&result->u.constant, class_ast)) { |
11738 | 3.99k | result->op_type = IS_CONST; |
11739 | 3.99k | return; |
11740 | 3.99k | } |
11741 | | |
11742 | 4.71k | if (class_ast->kind == ZEND_AST_ZVAL) { |
11743 | 1.06k | zend_op *opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11744 | 1.06k | opline->op1.num = zend_get_class_fetch_type(zend_ast_get_str(class_ast)); |
11745 | 3.65k | } else { |
11746 | 3.65k | znode expr_node; |
11747 | 3.65k | zend_compile_expr(&expr_node, class_ast); |
11748 | 3.65k | if (expr_node.op_type == IS_CONST) { |
11749 | | /* Unlikely case that happen if class_ast is constant folded. |
11750 | | * Handle it here, to avoid needing a CONST specialization in the VM. */ |
11751 | 10 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use \"::class\" on %s", |
11752 | 10 | zend_zval_value_name(&expr_node.u.constant)); |
11753 | 10 | } |
11754 | | |
11755 | 3.64k | zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, &expr_node, NULL); |
11756 | 3.64k | } |
11757 | 4.71k | } |
11758 | | /* }}} */ |
11759 | | |
11760 | | static zend_op *zend_compile_rope_add_ex(zend_op *opline, znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11761 | 273k | { |
11762 | 273k | if (num == 0) { |
11763 | 48.2k | result->op_type = IS_TMP_VAR; |
11764 | 48.2k | result->u.op.var = -1; |
11765 | 48.2k | opline->opcode = ZEND_ROPE_INIT; |
11766 | 225k | } else { |
11767 | 225k | opline->opcode = ZEND_ROPE_ADD; |
11768 | 225k | SET_NODE(opline->op1, result); |
11769 | 225k | } |
11770 | 273k | SET_NODE(opline->op2, elem_node); |
11771 | 273k | SET_NODE(opline->result, result); |
11772 | 273k | opline->extended_value = num; |
11773 | 273k | return opline; |
11774 | 273k | } |
11775 | | /* }}} */ |
11776 | | |
11777 | | static zend_op *zend_compile_rope_add(znode *result, uint32_t num, znode *elem_node) /* {{{ */ |
11778 | 255k | { |
11779 | 255k | zend_op *opline = get_next_op(); |
11780 | | |
11781 | 255k | if (num == 0) { |
11782 | 19.6k | result->op_type = IS_TMP_VAR; |
11783 | 19.6k | result->u.op.var = -1; |
11784 | 19.6k | opline->opcode = ZEND_ROPE_INIT; |
11785 | 235k | } else { |
11786 | 235k | opline->opcode = ZEND_ROPE_ADD; |
11787 | 235k | SET_NODE(opline->op1, result); |
11788 | 235k | } |
11789 | 255k | SET_NODE(opline->op2, elem_node); |
11790 | 255k | SET_NODE(opline->result, result); |
11791 | 255k | opline->extended_value = num; |
11792 | 255k | return opline; |
11793 | 255k | } |
11794 | | /* }}} */ |
11795 | | |
11796 | | static void zend_compile_rope_finalize(znode *result, uint32_t rope_elements, zend_op *init_opline, zend_op *opline) |
11797 | 67.8k | { |
11798 | 67.8k | if (rope_elements == 1) { |
11799 | 2.56k | if (opline->op2_type == IS_CONST) { |
11800 | 531 | GET_NODE(result, opline->op2); |
11801 | 531 | ZVAL_UNDEF(CT_CONSTANT(opline->op2)); |
11802 | 531 | SET_UNUSED(opline->op2); |
11803 | 531 | MAKE_NOP(opline); |
11804 | 2.03k | } else { |
11805 | 2.03k | opline->opcode = ZEND_CAST; |
11806 | 2.03k | opline->extended_value = IS_STRING; |
11807 | 2.03k | opline->op1_type = opline->op2_type; |
11808 | 2.03k | opline->op1 = opline->op2; |
11809 | 2.03k | SET_UNUSED(opline->op2); |
11810 | 2.03k | zend_make_tmp_result(result, opline); |
11811 | 2.03k | } |
11812 | 65.3k | } else if (rope_elements == 2) { |
11813 | 7.76k | opline->opcode = ZEND_FAST_CONCAT; |
11814 | 7.76k | opline->extended_value = 0; |
11815 | 7.76k | opline->op1_type = init_opline->op2_type; |
11816 | 7.76k | opline->op1 = init_opline->op2; |
11817 | 7.76k | zend_make_tmp_result(result, opline); |
11818 | 7.76k | MAKE_NOP(init_opline); |
11819 | 57.5k | } else { |
11820 | 57.5k | uint32_t var; |
11821 | | |
11822 | 57.5k | init_opline->extended_value = rope_elements; |
11823 | 57.5k | opline->opcode = ZEND_ROPE_END; |
11824 | 57.5k | zend_make_tmp_result(result, opline); |
11825 | 57.5k | var = opline->op1.var = get_temporary_variable(); |
11826 | | |
11827 | | /* Allocates the necessary number of zval slots to keep the rope */ |
11828 | 57.5k | uint32_t i = ((rope_elements * sizeof(zend_string*)) + (sizeof(zval) - 1)) / sizeof(zval); |
11829 | 282k | while (i > 1) { |
11830 | 224k | get_temporary_variable(); |
11831 | 224k | i--; |
11832 | 224k | } |
11833 | | |
11834 | | /* Update all the previous opcodes to use the same variable */ |
11835 | 632k | while (opline != init_opline) { |
11836 | 574k | opline--; |
11837 | 574k | if (opline->opcode == ZEND_ROPE_ADD && |
11838 | 396k | opline->result.var == (uint32_t)-1) { |
11839 | 396k | opline->op1.var = var; |
11840 | 396k | opline->result.var = var; |
11841 | 396k | } else if (opline->opcode == ZEND_ROPE_INIT && |
11842 | 57.7k | opline->result.var == (uint32_t)-1) { |
11843 | 57.5k | opline->result.var = var; |
11844 | 57.5k | } |
11845 | 574k | } |
11846 | 57.5k | } |
11847 | 67.8k | } |
11848 | | |
11849 | | static void zend_compile_encaps_list(znode *result, zend_ast *ast) /* {{{ */ |
11850 | 64.3k | { |
11851 | 64.3k | uint32_t i, j; |
11852 | 64.3k | uint32_t rope_init_lineno = -1; |
11853 | 64.3k | zend_op *opline = NULL, *init_opline; |
11854 | 64.3k | znode elem_node, last_const_node; |
11855 | 64.3k | zend_ast_list *list = zend_ast_get_list(ast); |
11856 | 64.3k | uint32_t reserved_op_number = -1; |
11857 | | |
11858 | 64.3k | ZEND_ASSERT(list->children > 0); |
11859 | | |
11860 | 64.3k | j = 0; |
11861 | 64.3k | last_const_node.op_type = IS_UNUSED; |
11862 | 583k | for (i = 0; i < list->children; i++) { |
11863 | 519k | zend_ast *encaps_var = list->child[i]; |
11864 | | |
11865 | 519k | if (encaps_var->attr & (ZEND_ENCAPS_VAR_DOLLAR_CURLY|ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11866 | 27.0k | if ((encaps_var->kind == ZEND_AST_VAR || encaps_var->kind == ZEND_AST_DIM) && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY)) { |
11867 | 1.04k | zend_error(E_DEPRECATED, "Using ${var} in strings is deprecated, use {$var} instead"); |
11868 | 26.0k | } else if (encaps_var->kind == ZEND_AST_VAR && (encaps_var->attr & ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR)) { |
11869 | 26.0k | zend_error(E_DEPRECATED, "Using ${expr} (variable variables) in strings is deprecated, use {${expr}} instead"); |
11870 | 26.0k | } |
11871 | 27.0k | } |
11872 | | |
11873 | 519k | zend_compile_expr(&elem_node, encaps_var); |
11874 | | |
11875 | 519k | if (elem_node.op_type == IS_CONST) { |
11876 | 274k | convert_to_string(&elem_node.u.constant); |
11877 | | |
11878 | 274k | if (Z_STRLEN(elem_node.u.constant) == 0) { |
11879 | 716 | zval_ptr_dtor(&elem_node.u.constant); |
11880 | 273k | } else if (last_const_node.op_type == IS_CONST) { |
11881 | 0 | concat_function(&last_const_node.u.constant, &last_const_node.u.constant, &elem_node.u.constant); |
11882 | 0 | zval_ptr_dtor(&elem_node.u.constant); |
11883 | 273k | } else { |
11884 | 273k | last_const_node.op_type = IS_CONST; |
11885 | 273k | ZVAL_COPY_VALUE(&last_const_node.u.constant, &elem_node.u.constant); |
11886 | | /* Reserve place for ZEND_ROPE_ADD instruction */ |
11887 | 273k | reserved_op_number = get_next_op_number(); |
11888 | 273k | opline = get_next_op(); |
11889 | 273k | opline->opcode = ZEND_NOP; |
11890 | 273k | } |
11891 | 274k | continue; |
11892 | 274k | } else { |
11893 | 244k | if (j == 0) { |
11894 | 64.3k | if (last_const_node.op_type == IS_CONST) { |
11895 | 48.2k | rope_init_lineno = reserved_op_number; |
11896 | 48.2k | } else { |
11897 | 16.1k | rope_init_lineno = get_next_op_number(); |
11898 | 16.1k | } |
11899 | 64.3k | } |
11900 | 244k | if (last_const_node.op_type == IS_CONST) { |
11901 | 215k | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11902 | 215k | zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11903 | 215k | last_const_node.op_type = IS_UNUSED; |
11904 | 215k | } |
11905 | 244k | opline = zend_compile_rope_add(result, j++, &elem_node); |
11906 | 244k | } |
11907 | 519k | } |
11908 | | |
11909 | 64.3k | if (j == 0) { |
11910 | 0 | result->op_type = IS_CONST; |
11911 | 0 | if (last_const_node.op_type == IS_CONST) { |
11912 | 0 | ZVAL_COPY_VALUE(&result->u.constant, &last_const_node.u.constant); |
11913 | 0 | } else { |
11914 | 0 | ZVAL_EMPTY_STRING(&result->u.constant); |
11915 | | /* empty string */ |
11916 | 0 | } |
11917 | 0 | CG(active_op_array)->last = reserved_op_number - 1; |
11918 | 0 | return; |
11919 | 64.3k | } else if (last_const_node.op_type == IS_CONST) { |
11920 | 57.9k | opline = &CG(active_op_array)->opcodes[reserved_op_number]; |
11921 | 57.9k | opline = zend_compile_rope_add_ex(opline, result, j++, &last_const_node); |
11922 | 57.9k | } |
11923 | 64.3k | init_opline = CG(active_op_array)->opcodes + rope_init_lineno; |
11924 | 64.3k | zend_compile_rope_finalize(result, j, init_opline, opline); |
11925 | 64.3k | } |
11926 | | /* }}} */ |
11927 | | |
11928 | | static void zend_compile_magic_const(znode *result, const zend_ast *ast) /* {{{ */ |
11929 | 15.2k | { |
11930 | 15.2k | zend_op *opline; |
11931 | | |
11932 | 15.2k | if (zend_try_ct_eval_magic_const(&result->u.constant, ast)) { |
11933 | 14.3k | result->op_type = IS_CONST; |
11934 | 14.3k | return; |
11935 | 14.3k | } |
11936 | | |
11937 | 908 | ZEND_ASSERT(ast->attr == T_CLASS_C && |
11938 | 908 | CG(active_class_entry) && |
11939 | 908 | (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0); |
11940 | | |
11941 | 908 | opline = zend_emit_op_tmp(result, ZEND_FETCH_CLASS_NAME, NULL, NULL); |
11942 | 908 | opline->op1.num = ZEND_FETCH_CLASS_SELF; |
11943 | 908 | } |
11944 | | /* }}} */ |
11945 | | |
11946 | | static bool zend_is_allowed_in_const_expr(zend_ast_kind kind) /* {{{ */ |
11947 | 82.9k | { |
11948 | 82.9k | return kind == ZEND_AST_ZVAL || kind == ZEND_AST_BINARY_OP |
11949 | 78.4k | || kind == ZEND_AST_GREATER || kind == ZEND_AST_GREATER_EQUAL |
11950 | 77.5k | || kind == ZEND_AST_AND || kind == ZEND_AST_OR |
11951 | 77.1k | || kind == ZEND_AST_UNARY_OP |
11952 | 76.6k | || kind == ZEND_AST_UNARY_PLUS || kind == ZEND_AST_UNARY_MINUS |
11953 | 75.6k | || kind == ZEND_AST_CAST |
11954 | 75.0k | || kind == ZEND_AST_CONDITIONAL || kind == ZEND_AST_DIM |
11955 | 72.9k | || kind == ZEND_AST_ARRAY || kind == ZEND_AST_ARRAY_ELEM |
11956 | 69.8k | || kind == ZEND_AST_UNPACK |
11957 | 69.7k | || kind == ZEND_AST_CONST || kind == ZEND_AST_CLASS_CONST |
11958 | 47.7k | || kind == ZEND_AST_CLASS_NAME |
11959 | 47.6k | || kind == ZEND_AST_MAGIC_CONST || kind == ZEND_AST_COALESCE |
11960 | 47.0k | || kind == ZEND_AST_CONST_ENUM_INIT |
11961 | 8.72k | || kind == ZEND_AST_NEW || kind == ZEND_AST_ARG_LIST |
11962 | 5.34k | || kind == ZEND_AST_NAMED_ARG |
11963 | 5.15k | || kind == ZEND_AST_PROP || kind == ZEND_AST_NULLSAFE_PROP |
11964 | 4.43k | || kind == ZEND_AST_CLOSURE |
11965 | 4.28k | || kind == ZEND_AST_CALL || kind == ZEND_AST_STATIC_CALL || kind == ZEND_AST_CALLABLE_CONVERT |
11966 | 1.44k | || kind == ZEND_AST_PLACEHOLDER_ARG; |
11967 | 82.9k | } |
11968 | | /* }}} */ |
11969 | | |
11970 | | static void zend_compile_const_expr_class_const(zend_ast **ast_ptr) /* {{{ */ |
11971 | 3.27k | { |
11972 | 3.27k | zend_ast *ast = *ast_ptr; |
11973 | 3.27k | zend_ast *class_ast = ast->child[0]; |
11974 | 3.27k | zend_string *class_name; |
11975 | 3.27k | int fetch_type; |
11976 | | |
11977 | 3.27k | if (class_ast->kind != ZEND_AST_ZVAL) { |
11978 | 18 | zend_error_noreturn(E_COMPILE_ERROR, |
11979 | 18 | "Dynamic class names are not allowed in compile-time class constant references"); |
11980 | 18 | } |
11981 | 3.25k | if (Z_TYPE_P(zend_ast_get_zval(class_ast)) != IS_STRING) { |
11982 | 6 | zend_throw_error(NULL, "Class name must be a valid object or a string"); |
11983 | 6 | } |
11984 | | |
11985 | 3.25k | class_name = zend_ast_get_str(class_ast); |
11986 | 3.25k | fetch_type = zend_get_class_fetch_type(class_name); |
11987 | | |
11988 | 3.25k | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
11989 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
11990 | 5 | "\"static::\" is not allowed in compile-time constants"); |
11991 | 5 | } |
11992 | | |
11993 | 3.24k | if (ZEND_FETCH_CLASS_DEFAULT == fetch_type) { |
11994 | 2.19k | zend_string *tmp = zend_resolve_class_name_ast(class_ast); |
11995 | | |
11996 | 2.19k | zend_string_release_ex(class_name, 0); |
11997 | 2.19k | if (tmp != class_name) { |
11998 | 383 | zval *zv = zend_ast_get_zval(class_ast); |
11999 | 383 | ZVAL_STR(zv, tmp); |
12000 | 383 | class_ast->attr = ZEND_NAME_FQ; |
12001 | 383 | } |
12002 | 2.19k | } |
12003 | | |
12004 | 3.24k | ast->attr |= ZEND_FETCH_CLASS_EXCEPTION; |
12005 | 3.24k | } |
12006 | | /* }}} */ |
12007 | | |
12008 | | static void zend_compile_const_expr_class_name(zend_ast **ast_ptr) /* {{{ */ |
12009 | 99 | { |
12010 | 99 | zend_ast *ast = *ast_ptr; |
12011 | 99 | zend_ast *class_ast = ast->child[0]; |
12012 | 99 | if (class_ast->kind != ZEND_AST_ZVAL) { |
12013 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12014 | 5 | "(expression)::class cannot be used in constant expressions"); |
12015 | 5 | } |
12016 | | |
12017 | 94 | zend_string *class_name = zend_ast_get_str(class_ast); |
12018 | 94 | uint32_t fetch_type = zend_get_class_fetch_type(class_name); |
12019 | | |
12020 | 94 | switch (fetch_type) { |
12021 | 72 | case ZEND_FETCH_CLASS_SELF: |
12022 | 89 | case ZEND_FETCH_CLASS_PARENT: |
12023 | | /* For the const-eval representation store the fetch type instead of the name. */ |
12024 | 89 | zend_string_release(class_name); |
12025 | 89 | ast->child[0] = NULL; |
12026 | 89 | ast->attr = fetch_type; |
12027 | 89 | return; |
12028 | 5 | case ZEND_FETCH_CLASS_STATIC: |
12029 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12030 | 5 | "static::class cannot be used for compile-time class name resolution"); |
12031 | 0 | default: ZEND_UNREACHABLE(); |
12032 | 94 | } |
12033 | 94 | } |
12034 | | |
12035 | | static void zend_compile_const_expr_const(zend_ast **ast_ptr) /* {{{ */ |
12036 | 18.7k | { |
12037 | 18.7k | zend_ast *ast = *ast_ptr; |
12038 | 18.7k | zend_ast *name_ast = ast->child[0]; |
12039 | 18.7k | zend_string *orig_name = zend_ast_get_str(name_ast); |
12040 | 18.7k | bool is_fully_qualified; |
12041 | 18.7k | zval result; |
12042 | 18.7k | zend_string *resolved_name; |
12043 | | |
12044 | 18.7k | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12045 | | |
12046 | 18.7k | resolved_name = zend_resolve_const_name( |
12047 | 18.7k | orig_name, name_ast->attr, &is_fully_qualified); |
12048 | | |
12049 | 18.7k | if (zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
12050 | 0 | zend_string_release_ex(resolved_name, 0); |
12051 | 0 | zend_ast_destroy(ast); |
12052 | 0 | *ast_ptr = zend_ast_create_zval(&result); |
12053 | 0 | return; |
12054 | 0 | } |
12055 | | |
12056 | 18.7k | zend_ast_destroy(ast); |
12057 | 18.7k | *ast_ptr = zend_ast_create_constant(resolved_name, |
12058 | 18.7k | !is_fully_qualified && FC(current_namespace) ? IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE : 0); |
12059 | 18.7k | } |
12060 | | /* }}} */ |
12061 | | |
12062 | | static void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */ |
12063 | 170 | { |
12064 | 170 | zend_ast *ast = *ast_ptr; |
12065 | | |
12066 | | /* Other cases already resolved by constant folding */ |
12067 | 170 | ZEND_ASSERT(ast->attr == T_CLASS_C); |
12068 | | |
12069 | 170 | zend_ast_destroy(ast); |
12070 | 170 | *ast_ptr = zend_ast_create(ZEND_AST_CONSTANT_CLASS); |
12071 | 170 | } |
12072 | | /* }}} */ |
12073 | | |
12074 | | static void zend_compile_const_expr_class_reference(zend_ast *class_ast) |
12075 | 1.23k | { |
12076 | 1.23k | if (class_ast->kind == ZEND_AST_CLASS) { |
12077 | 0 | zend_error_noreturn(E_COMPILE_ERROR, |
12078 | 0 | "Cannot use anonymous class in constant expression"); |
12079 | 0 | } |
12080 | 1.23k | if (class_ast->kind != ZEND_AST_ZVAL) { |
12081 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
12082 | 7 | "Cannot use dynamic class name in constant expression"); |
12083 | 7 | } |
12084 | | |
12085 | 1.22k | zend_string *class_name = zend_resolve_class_name_ast(class_ast); |
12086 | 1.22k | int fetch_type = zend_get_class_fetch_type(class_name); |
12087 | 1.22k | if (ZEND_FETCH_CLASS_STATIC == fetch_type) { |
12088 | 7 | zend_error_noreturn(E_COMPILE_ERROR, |
12089 | 7 | "\"static\" is not allowed in compile-time constants"); |
12090 | 7 | } |
12091 | | |
12092 | 1.21k | zval *class_ast_zv = zend_ast_get_zval(class_ast); |
12093 | 1.21k | zval_ptr_dtor_nogc(class_ast_zv); |
12094 | 1.21k | ZVAL_STR(class_ast_zv, class_name); |
12095 | 1.21k | class_ast->attr = fetch_type << ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT; |
12096 | 1.21k | } |
12097 | | |
12098 | | static void zend_compile_const_expr_new(zend_ast **ast_ptr) |
12099 | 995 | { |
12100 | 995 | zend_ast *class_ast = (*ast_ptr)->child[0]; |
12101 | 995 | zend_compile_const_expr_class_reference(class_ast); |
12102 | | |
12103 | 995 | const zend_ast *args_ast = (*ast_ptr)->child[1]; |
12104 | 995 | if (args_ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
12105 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot create Closure for new expression"); |
12106 | 5 | } |
12107 | 995 | } |
12108 | | |
12109 | | static void zend_compile_const_expr_closure(zend_ast **ast_ptr) |
12110 | 146 | { |
12111 | 146 | zend_ast_decl *closure_ast = (zend_ast_decl *) *ast_ptr; |
12112 | 146 | const zend_ast *uses_ast = closure_ast->child[1]; |
12113 | 146 | if (!(closure_ast->flags & ZEND_ACC_STATIC)) { |
12114 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12115 | 5 | "Closures in constant expressions must be static"); |
12116 | 5 | } |
12117 | 141 | if (uses_ast) { |
12118 | 5 | zend_error_noreturn(E_COMPILE_ERROR, |
12119 | 5 | "Cannot use(...) variables in constant expression"); |
12120 | 5 | } |
12121 | | |
12122 | 136 | znode node; |
12123 | 136 | zend_op_array *op = zend_compile_func_decl(&node, (zend_ast*)closure_ast, FUNC_DECL_LEVEL_CONSTEXPR); |
12124 | | |
12125 | 136 | zend_ast_destroy(*ast_ptr); |
12126 | 136 | *ast_ptr = zend_ast_create_op_array(op); |
12127 | 136 | } |
12128 | | |
12129 | | static void zend_compile_const_expr_fcc(zend_ast **ast_ptr) |
12130 | 1.45k | { |
12131 | 1.45k | zend_ast *args_ast = zend_ast_call_get_args(*ast_ptr); |
12132 | | |
12133 | 1.45k | if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { |
12134 | 37 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
12135 | 37 | } |
12136 | | |
12137 | 1.41k | zend_ast_fcc *fcc = (zend_ast_fcc*)args_ast; |
12138 | | |
12139 | 1.41k | zend_ast_list *args = zend_ast_get_list(fcc->args); |
12140 | 1.41k | bool is_fcc = args->children == 1 && args->child[0]->attr == ZEND_PLACEHOLDER_VARIADIC; |
12141 | | |
12142 | 1.41k | if (!is_fcc) { |
12143 | 318 | fcc->filename = zend_string_copy(CG(active_op_array)->filename); |
12144 | 318 | fcc->name = zend_compile_partial_name(CG(active_op_array), fcc->lineno); |
12145 | 318 | } |
12146 | | |
12147 | 1.41k | switch ((*ast_ptr)->kind) { |
12148 | 1.18k | case ZEND_AST_CALL: { |
12149 | 1.18k | zend_ast *name_ast = (*ast_ptr)->child[0]; |
12150 | 1.18k | if (name_ast->kind != ZEND_AST_ZVAL) { |
12151 | 14 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic function name in constant expression"); |
12152 | 14 | } |
12153 | 1.16k | zval *name_ast_zv = zend_ast_get_zval(name_ast); |
12154 | 1.16k | if (Z_TYPE_P(name_ast_zv) != IS_STRING) { |
12155 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal function name"); |
12156 | 5 | } |
12157 | 1.16k | bool is_fully_qualified; |
12158 | 1.16k | zend_string *name = zend_resolve_function_name(Z_STR_P(name_ast_zv), name_ast->attr, &is_fully_qualified); |
12159 | 1.16k | zval_ptr_dtor_nogc(name_ast_zv); |
12160 | 1.16k | ZVAL_STR(name_ast_zv, name); |
12161 | 1.16k | if (is_fully_qualified) { |
12162 | 499 | name_ast->attr = ZEND_NAME_FQ; |
12163 | 499 | } |
12164 | 1.16k | break; |
12165 | 1.16k | } |
12166 | 236 | case ZEND_AST_STATIC_CALL: { |
12167 | 236 | zend_ast *class_ast = (*ast_ptr)->child[0]; |
12168 | 236 | zend_compile_const_expr_class_reference(class_ast); |
12169 | 236 | zend_ast *method_ast = (*ast_ptr)->child[1]; |
12170 | 236 | if (method_ast->kind != ZEND_AST_ZVAL) { |
12171 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use dynamic method name in constant expression"); |
12172 | 0 | } |
12173 | 236 | if (Z_TYPE_P(zend_ast_get_zval(method_ast)) != IS_STRING) { |
12174 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Illegal method name"); |
12175 | 0 | } |
12176 | 236 | break; |
12177 | 236 | } |
12178 | 236 | default: ZEND_UNREACHABLE(); |
12179 | 1.41k | } |
12180 | 1.41k | } |
12181 | | |
12182 | | static void zend_compile_const_expr_args(zend_ast **ast_ptr) |
12183 | 2.37k | { |
12184 | 2.37k | zend_ast_list *list = zend_ast_get_list(*ast_ptr); |
12185 | 2.37k | bool uses_named_args = false; |
12186 | 2.37k | bool uses_variadic_placeholder = false; |
12187 | 4.57k | for (uint32_t i = 0; i < list->children; i++) { |
12188 | 2.22k | const zend_ast *arg = list->child[i]; |
12189 | 2.22k | if (arg->kind == ZEND_AST_UNPACK) { |
12190 | 2 | zend_error_noreturn(E_COMPILE_ERROR, |
12191 | 2 | "Argument unpacking in constant expressions is not supported"); |
12192 | 2 | } |
12193 | 2.21k | if (arg->kind == ZEND_AST_NAMED_ARG) { |
12194 | 200 | uses_named_args = true; |
12195 | 200 | if (uses_variadic_placeholder) { |
12196 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder must be last"); |
12197 | 5 | } |
12198 | 2.01k | } else { |
12199 | 2.01k | bool is_variadic_placeholder = arg->kind == ZEND_AST_PLACEHOLDER_ARG |
12200 | 1.39k | && arg->attr == ZEND_PLACEHOLDER_VARIADIC; |
12201 | | |
12202 | 2.01k | if (uses_named_args && !is_variadic_placeholder) { |
12203 | 2 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use positional argument after named argument"); |
12204 | 2 | } |
12205 | | |
12206 | 2.01k | if (uses_variadic_placeholder) { |
12207 | 10 | if (is_variadic_placeholder) { |
12208 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder may only appear once"); |
12209 | 5 | } else { |
12210 | 5 | zend_error_noreturn(E_COMPILE_ERROR, "Variadic placeholder must be last"); |
12211 | 5 | } |
12212 | 10 | } |
12213 | | |
12214 | 2.00k | if (is_variadic_placeholder) { |
12215 | 1.15k | uses_variadic_placeholder = true; |
12216 | 1.15k | } |
12217 | 2.00k | } |
12218 | 2.21k | } |
12219 | | |
12220 | 2.35k | if (uses_named_args) { |
12221 | 168 | list->attr = 1; |
12222 | 168 | } |
12223 | 2.35k | } |
12224 | | |
12225 | | typedef struct { |
12226 | | /* Whether the value of this expression may differ on each evaluation. */ |
12227 | | bool allow_dynamic; |
12228 | | } const_expr_context; |
12229 | | |
12230 | | static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */ |
12231 | 333k | { |
12232 | 333k | const const_expr_context *ctx = context; |
12233 | 333k | zend_ast *ast = *ast_ptr; |
12234 | 333k | if (ast == NULL || ast->kind == ZEND_AST_ZVAL) { |
12235 | 250k | return; |
12236 | 250k | } |
12237 | | |
12238 | 82.9k | if (!zend_is_allowed_in_const_expr(ast->kind)) { |
12239 | 62 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
12240 | 62 | } |
12241 | | |
12242 | 82.8k | switch (ast->kind) { |
12243 | 3.27k | case ZEND_AST_CLASS_CONST: |
12244 | 3.27k | zend_compile_const_expr_class_const(ast_ptr); |
12245 | 3.27k | break; |
12246 | 99 | case ZEND_AST_CLASS_NAME: |
12247 | 99 | zend_compile_const_expr_class_name(ast_ptr); |
12248 | 99 | break; |
12249 | 18.7k | case ZEND_AST_CONST: |
12250 | 18.7k | zend_compile_const_expr_const(ast_ptr); |
12251 | 18.7k | break; |
12252 | 170 | case ZEND_AST_MAGIC_CONST: |
12253 | 170 | zend_compile_const_expr_magic_const(ast_ptr); |
12254 | 170 | break; |
12255 | 602 | case ZEND_AST_CAST: |
12256 | 602 | if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) { |
12257 | 10 | zend_error_noreturn(E_COMPILE_ERROR, |
12258 | 10 | "Object casts are not supported in this context"); |
12259 | 10 | } |
12260 | 592 | break; |
12261 | 1.00k | case ZEND_AST_NEW: |
12262 | 1.00k | if (!ctx->allow_dynamic) { |
12263 | 6 | zend_error_noreturn(E_COMPILE_ERROR, |
12264 | 6 | "New expressions are not supported in this context"); |
12265 | 6 | } |
12266 | 995 | zend_compile_const_expr_new(ast_ptr); |
12267 | 995 | break; |
12268 | 2.37k | case ZEND_AST_ARG_LIST: |
12269 | 2.37k | zend_compile_const_expr_args(ast_ptr); |
12270 | 2.37k | break; |
12271 | 146 | case ZEND_AST_CLOSURE: |
12272 | 146 | zend_compile_const_expr_closure(ast_ptr); |
12273 | | /* Return, because we do not want to traverse the children. */ |
12274 | 146 | return; |
12275 | 1.21k | case ZEND_AST_CALL: |
12276 | 1.45k | case ZEND_AST_STATIC_CALL: |
12277 | 1.45k | zend_compile_const_expr_fcc(ast_ptr); |
12278 | 1.45k | break; |
12279 | 82.8k | } |
12280 | | |
12281 | 82.5k | zend_ast_apply(ast, zend_compile_const_expr, context); |
12282 | 82.5k | } |
12283 | | /* }}} */ |
12284 | | |
12285 | | void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic) /* {{{ */ |
12286 | 118k | { |
12287 | 118k | const_expr_context context; |
12288 | 118k | context.allow_dynamic = allow_dynamic; |
12289 | | |
12290 | 118k | zend_eval_const_expr(ast_ptr); |
12291 | 118k | zend_compile_const_expr(ast_ptr, &context); |
12292 | 118k | if ((*ast_ptr)->kind != ZEND_AST_ZVAL) { |
12293 | | /* Replace with compiled AST zval representation. */ |
12294 | 56.8k | zval ast_zv; |
12295 | 56.8k | ZVAL_AST(&ast_zv, zend_ast_copy(*ast_ptr)); |
12296 | 56.8k | zend_ast_destroy(*ast_ptr); |
12297 | 56.8k | *ast_ptr = zend_ast_create_zval(&ast_zv); |
12298 | 56.8k | } |
12299 | 118k | ZVAL_COPY(result, zend_ast_get_zval(*ast_ptr)); |
12300 | 118k | } |
12301 | | /* }}} */ |
12302 | | |
12303 | | /* Same as compile_stmt, but with early binding */ |
12304 | | void zend_compile_top_stmt(zend_ast *ast) /* {{{ */ |
12305 | 734k | { |
12306 | 734k | if (!ast) { |
12307 | 89.2k | return; |
12308 | 89.2k | } |
12309 | | |
12310 | 645k | if (ast->kind == ZEND_AST_STMT_LIST) { |
12311 | 124k | const zend_ast_list *list = zend_ast_get_list(ast); |
12312 | 124k | uint32_t i; |
12313 | 760k | for (i = 0; i < list->children; ++i) { |
12314 | 635k | zend_compile_top_stmt(list->child[i]); |
12315 | 635k | } |
12316 | 124k | return; |
12317 | 124k | } |
12318 | | |
12319 | 520k | if (ast->kind == ZEND_AST_FUNC_DECL) { |
12320 | 20.6k | CG(zend_lineno) = ast->lineno; |
12321 | 20.6k | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_TOPLEVEL); |
12322 | 20.6k | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
12323 | 499k | } else if (ast->kind == ZEND_AST_CLASS) { |
12324 | 47.7k | CG(zend_lineno) = ast->lineno; |
12325 | 47.7k | zend_compile_class_decl(NULL, ast, true); |
12326 | 47.7k | CG(zend_lineno) = ((zend_ast_decl *) ast)->end_lineno; |
12327 | 452k | } else { |
12328 | 452k | zend_compile_stmt(ast); |
12329 | 452k | } |
12330 | 520k | if (ast->kind != ZEND_AST_NAMESPACE && ast->kind != ZEND_AST_HALT_COMPILER) { |
12331 | 510k | zend_verify_namespace(); |
12332 | 510k | } |
12333 | 520k | } |
12334 | | /* }}} */ |
12335 | | |
12336 | | static void zend_compile_stmt(zend_ast *ast) /* {{{ */ |
12337 | 8.03M | { |
12338 | 8.03M | if (!ast) { |
12339 | 375k | return; |
12340 | 375k | } |
12341 | | |
12342 | 7.65M | CG(zend_lineno) = ast->lineno; |
12343 | | |
12344 | 7.65M | if ((CG(compiler_options) & ZEND_COMPILE_EXTENDED_STMT) && !zend_is_unticked_stmt(ast)) { |
12345 | 0 | zend_do_extended_stmt(NULL); |
12346 | 0 | } |
12347 | | |
12348 | 7.65M | switch (ast->kind) { |
12349 | 1.64M | case ZEND_AST_STMT_LIST: |
12350 | 1.64M | zend_compile_stmt_list(ast); |
12351 | 1.64M | break; |
12352 | 2.63k | case ZEND_AST_GLOBAL: |
12353 | 2.63k | zend_compile_global_var(ast); |
12354 | 2.63k | break; |
12355 | 3.91k | case ZEND_AST_STATIC: |
12356 | 3.91k | zend_compile_static_var(ast); |
12357 | 3.91k | break; |
12358 | 8.97k | case ZEND_AST_UNSET: |
12359 | 8.97k | zend_compile_unset(ast); |
12360 | 8.97k | break; |
12361 | 278k | case ZEND_AST_RETURN: |
12362 | 278k | zend_compile_return(ast); |
12363 | 278k | break; |
12364 | 1.88M | case ZEND_AST_ECHO: |
12365 | 1.88M | zend_compile_echo(ast); |
12366 | 1.88M | break; |
12367 | 1.50k | case ZEND_AST_BREAK: |
12368 | 2.72k | case ZEND_AST_CONTINUE: |
12369 | 2.72k | zend_compile_break_continue(ast); |
12370 | 2.72k | break; |
12371 | 2.07k | case ZEND_AST_GOTO: |
12372 | 2.07k | zend_compile_goto(ast); |
12373 | 2.07k | break; |
12374 | 3.49k | case ZEND_AST_LABEL: |
12375 | 3.49k | zend_compile_label(ast); |
12376 | 3.49k | break; |
12377 | 1.98k | case ZEND_AST_WHILE: |
12378 | 1.98k | zend_compile_while(ast); |
12379 | 1.98k | break; |
12380 | 1.18k | case ZEND_AST_DO_WHILE: |
12381 | 1.18k | zend_compile_do_while(ast); |
12382 | 1.18k | break; |
12383 | 14.6k | case ZEND_AST_FOR: |
12384 | 14.6k | zend_compile_for(ast); |
12385 | 14.6k | break; |
12386 | 18.0k | case ZEND_AST_FOREACH: |
12387 | 18.0k | zend_compile_foreach(ast); |
12388 | 18.0k | break; |
12389 | 76.9k | case ZEND_AST_IF: |
12390 | 76.9k | zend_compile_if(ast); |
12391 | 76.9k | break; |
12392 | 6.16k | case ZEND_AST_SWITCH: |
12393 | 6.16k | zend_compile_switch(ast); |
12394 | 6.16k | break; |
12395 | 35.3k | case ZEND_AST_TRY: |
12396 | 35.3k | zend_compile_try(ast); |
12397 | 35.3k | break; |
12398 | 9.45k | case ZEND_AST_DECLARE: |
12399 | 9.45k | zend_compile_declare(ast); |
12400 | 9.45k | break; |
12401 | 1.83k | case ZEND_AST_FUNC_DECL: |
12402 | 100k | case ZEND_AST_METHOD: |
12403 | 100k | zend_compile_func_decl(NULL, ast, FUNC_DECL_LEVEL_NESTED); |
12404 | 100k | break; |
12405 | 38.3k | case ZEND_AST_ENUM_CASE: |
12406 | 38.3k | zend_compile_enum_case(ast); |
12407 | 38.3k | break; |
12408 | 30.5k | case ZEND_AST_PROP_GROUP: |
12409 | 30.5k | zend_compile_prop_group(ast); |
12410 | 30.5k | break; |
12411 | 8.89k | case ZEND_AST_CLASS_CONST_GROUP: |
12412 | 8.89k | zend_compile_class_const_group(ast); |
12413 | 8.89k | break; |
12414 | 4.63k | case ZEND_AST_USE_TRAIT: |
12415 | 4.63k | zend_compile_use_trait(ast); |
12416 | 4.63k | break; |
12417 | 123k | case ZEND_AST_CLASS: |
12418 | 123k | zend_compile_class_decl(NULL, ast, false); |
12419 | 123k | break; |
12420 | 239 | case ZEND_AST_GROUP_USE: |
12421 | 239 | zend_compile_group_use(ast); |
12422 | 239 | break; |
12423 | 1.37k | case ZEND_AST_USE: |
12424 | 1.37k | zend_compile_use(ast); |
12425 | 1.37k | break; |
12426 | 5.11k | case ZEND_AST_CONST_DECL: |
12427 | 5.11k | zend_compile_const_decl(ast); |
12428 | 5.11k | break; |
12429 | 4.96k | case ZEND_AST_NAMESPACE: |
12430 | 4.96k | zend_compile_namespace(ast); |
12431 | 4.96k | break; |
12432 | 134 | case ZEND_AST_HALT_COMPILER: |
12433 | 134 | zend_compile_halt_compiler(ast); |
12434 | 134 | break; |
12435 | 3.11k | case ZEND_AST_THROW: |
12436 | 3.11k | zend_compile_expr(NULL, ast); |
12437 | 3.11k | break; |
12438 | 1.05k | case ZEND_AST_CAST_VOID: |
12439 | 1.05k | zend_compile_void_cast(NULL, ast); |
12440 | 1.05k | break; |
12441 | 153k | case ZEND_AST_ASSIGN: { |
12442 | 153k | znode result; |
12443 | 153k | zend_compile_assign(&result, ast, /* stmt */ true, BP_VAR_R); |
12444 | 153k | zend_do_free(&result); |
12445 | 153k | return; |
12446 | 1.83k | } |
12447 | 9.01k | case ZEND_AST_ASSIGN_REF: |
12448 | 9.01k | zend_compile_assign_ref(NULL, ast, BP_VAR_R); |
12449 | 9.01k | return; |
12450 | 3.18M | default: |
12451 | 3.18M | { |
12452 | 3.18M | znode result; |
12453 | 3.18M | zend_compile_expr(&result, ast); |
12454 | 3.18M | zend_do_free(&result); |
12455 | 3.18M | } |
12456 | 7.65M | } |
12457 | | |
12458 | 7.49M | if (FC(declarables).ticks && !zend_is_unticked_stmt(ast)) { |
12459 | 48.2k | zend_emit_tick(); |
12460 | 48.2k | } |
12461 | 7.49M | } |
12462 | | /* }}} */ |
12463 | | |
12464 | | /* Keep this out of zend_compile_expr(): the two form a recursion cycle, so inlining merges this |
12465 | | * frame into every nesting level of an expression, tripling the stack needed to compile it. */ |
12466 | | static zend_never_inline void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */ |
12467 | 37.2M | { |
12468 | | /* CG(zend_lineno) = ast->lineno; */ |
12469 | 37.2M | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12470 | | |
12471 | 37.2M | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12472 | 592k | zend_compile_memoized_expr(result, ast, BP_VAR_R); |
12473 | 592k | return; |
12474 | 592k | } |
12475 | | |
12476 | 36.6M | switch (ast->kind) { |
12477 | 5.07M | case ZEND_AST_ZVAL: |
12478 | 5.07M | ZVAL_COPY(&result->u.constant, zend_ast_get_zval(ast)); |
12479 | 5.07M | result->op_type = IS_CONST; |
12480 | 5.07M | return; |
12481 | 79.2k | case ZEND_AST_ZNODE: |
12482 | 79.2k | *result = *zend_ast_get_znode(ast); |
12483 | 79.2k | return; |
12484 | 633k | case ZEND_AST_VAR: |
12485 | 683k | case ZEND_AST_DIM: |
12486 | 723k | case ZEND_AST_PROP: |
12487 | 768k | case ZEND_AST_NULLSAFE_PROP: |
12488 | 773k | case ZEND_AST_STATIC_PROP: |
12489 | 3.45M | case ZEND_AST_CALL: |
12490 | 3.50M | case ZEND_AST_METHOD_CALL: |
12491 | 3.51M | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12492 | 3.52M | case ZEND_AST_STATIC_CALL: |
12493 | 3.56M | case ZEND_AST_PIPE: |
12494 | 3.56M | zend_compile_var(result, ast, BP_VAR_R, false); |
12495 | 3.56M | return; |
12496 | 219k | case ZEND_AST_ASSIGN: |
12497 | 219k | zend_compile_assign(result, ast, /* stmt */ false, BP_VAR_R); |
12498 | 219k | return; |
12499 | 2.19k | case ZEND_AST_ASSIGN_REF: |
12500 | 2.19k | zend_compile_assign_ref(result, ast, BP_VAR_R); |
12501 | 2.19k | return; |
12502 | 115k | case ZEND_AST_NEW: |
12503 | 115k | zend_compile_new(result, ast); |
12504 | 115k | return; |
12505 | 111k | case ZEND_AST_ASSIGN_OP: |
12506 | 111k | zend_compile_compound_assign(result, ast); |
12507 | 111k | return; |
12508 | 3.76M | case ZEND_AST_BINARY_OP: |
12509 | 3.76M | zend_compile_binary_op(result, ast); |
12510 | 3.76M | return; |
12511 | 309k | case ZEND_AST_GREATER: |
12512 | 386k | case ZEND_AST_GREATER_EQUAL: |
12513 | 386k | zend_compile_greater(result, ast); |
12514 | 386k | return; |
12515 | 1.38M | case ZEND_AST_UNARY_OP: |
12516 | 1.38M | zend_compile_unary_op(result, ast); |
12517 | 1.38M | return; |
12518 | 33.8k | case ZEND_AST_UNARY_PLUS: |
12519 | 188k | case ZEND_AST_UNARY_MINUS: |
12520 | 188k | zend_compile_unary_pm(result, ast); |
12521 | 188k | return; |
12522 | 9.03k | case ZEND_AST_AND: |
12523 | 15.1k | case ZEND_AST_OR: |
12524 | 15.1k | zend_compile_short_circuiting(result, ast); |
12525 | 15.1k | return; |
12526 | 10.7k | case ZEND_AST_POST_INC: |
12527 | 14.9k | case ZEND_AST_POST_DEC: |
12528 | 14.9k | zend_compile_post_incdec(result, ast); |
12529 | 14.9k | return; |
12530 | 2.54k | case ZEND_AST_PRE_INC: |
12531 | 4.66k | case ZEND_AST_PRE_DEC: |
12532 | 4.66k | zend_compile_pre_incdec(result, ast); |
12533 | 4.66k | return; |
12534 | 3.92k | case ZEND_AST_CAST: |
12535 | 3.92k | zend_compile_cast(result, ast); |
12536 | 3.92k | return; |
12537 | 14.5k | case ZEND_AST_CONDITIONAL: |
12538 | 14.5k | zend_compile_conditional(result, ast); |
12539 | 14.5k | return; |
12540 | 1.21M | case ZEND_AST_COALESCE: |
12541 | 1.21M | zend_compile_coalesce(result, ast); |
12542 | 1.21M | return; |
12543 | 70.1k | case ZEND_AST_ASSIGN_COALESCE: |
12544 | 70.1k | zend_compile_assign_coalesce(result, ast); |
12545 | 70.1k | return; |
12546 | 8.14k | case ZEND_AST_PRINT: |
12547 | 8.14k | zend_compile_print(result, ast); |
12548 | 8.14k | return; |
12549 | 237k | case ZEND_AST_YIELD: |
12550 | 237k | zend_compile_yield(result, ast); |
12551 | 237k | return; |
12552 | 1.09k | case ZEND_AST_YIELD_FROM: |
12553 | 1.09k | zend_compile_yield_from(result, ast); |
12554 | 1.09k | return; |
12555 | 1.02k | case ZEND_AST_INSTANCEOF: |
12556 | 1.02k | zend_compile_instanceof(result, ast); |
12557 | 1.02k | return; |
12558 | 8.83k | case ZEND_AST_INCLUDE_OR_EVAL: |
12559 | 8.83k | zend_compile_include_or_eval(result, ast); |
12560 | 8.83k | return; |
12561 | 7.13k | case ZEND_AST_ISSET: |
12562 | 9.02k | case ZEND_AST_EMPTY: |
12563 | 9.02k | zend_compile_isset_or_empty(result, ast); |
12564 | 9.02k | return; |
12565 | 11.0M | case ZEND_AST_SILENCE: |
12566 | 11.0M | zend_compile_silence(result, ast); |
12567 | 11.0M | return; |
12568 | 23.1k | case ZEND_AST_SHELL_EXEC: |
12569 | 23.1k | zend_compile_shell_exec(result, ast); |
12570 | 23.1k | return; |
12571 | 113k | case ZEND_AST_ARRAY: |
12572 | 113k | zend_compile_array(result, ast); |
12573 | 113k | return; |
12574 | 7.42M | case ZEND_AST_CONST: |
12575 | 7.42M | zend_compile_const(result, ast, BP_VAR_R); |
12576 | 7.42M | return; |
12577 | 20 | case ZEND_AST_CONSTANT: |
12578 | 20 | zend_compile_constant(result, ast, BP_VAR_R); |
12579 | 20 | return; |
12580 | 110k | case ZEND_AST_CLASS_CONST: |
12581 | 110k | zend_compile_class_const(result, ast, BP_VAR_R); |
12582 | 110k | return; |
12583 | 8.71k | case ZEND_AST_CLASS_NAME: |
12584 | 8.71k | zend_compile_class_name(result, ast); |
12585 | 8.71k | return; |
12586 | 64.3k | case ZEND_AST_ENCAPS_LIST: |
12587 | 64.3k | zend_compile_encaps_list(result, ast); |
12588 | 64.3k | return; |
12589 | 15.2k | case ZEND_AST_MAGIC_CONST: |
12590 | 15.2k | zend_compile_magic_const(result, ast); |
12591 | 15.2k | return; |
12592 | 1.09M | case ZEND_AST_CLOSURE: |
12593 | 1.35M | case ZEND_AST_ARROW_FUNC: |
12594 | 1.35M | zend_compile_func_decl(result, ast, FUNC_DECL_LEVEL_NESTED); |
12595 | 1.35M | return; |
12596 | 3.78k | case ZEND_AST_THROW: |
12597 | 3.78k | zend_compile_throw(result, ast); |
12598 | 3.78k | return; |
12599 | 4.25k | case ZEND_AST_MATCH: |
12600 | 4.25k | zend_compile_match(result, ast); |
12601 | 4.25k | return; |
12602 | 0 | default: |
12603 | 0 | ZEND_ASSERT(0 /* not supported */); |
12604 | 36.6M | } |
12605 | 36.6M | } |
12606 | | /* }}} */ |
12607 | | |
12608 | | static void zend_compile_expr(znode *result, zend_ast *ast) |
12609 | 37.2M | { |
12610 | 37.2M | zend_check_stack_limit(); |
12611 | | |
12612 | 37.2M | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12613 | 37.2M | zend_compile_expr_inner(result, ast); |
12614 | 37.2M | zend_short_circuiting_commit(checkpoint, result, ast); |
12615 | 37.2M | #if ZEND_DEBUG |
12616 | 37.2M | if (result) { |
12617 | | /* BP_VAR_R is not allowed to produce IS_VAR. */ |
12618 | 37.2M | ZEND_ASSERT(result->op_type != IS_VAR); |
12619 | 37.2M | } |
12620 | 37.2M | #endif |
12621 | 37.2M | } |
12622 | | |
12623 | | static zend_op *zend_compile_var_inner(znode *result, zend_ast *ast, uint32_t type, bool by_ref) |
12624 | 5.77M | { |
12625 | 5.77M | CG(zend_lineno) = zend_ast_get_lineno(ast); |
12626 | | |
12627 | 5.77M | if (CG(memoize_mode) != ZEND_MEMOIZE_NONE) { |
12628 | 252k | switch (ast->kind) { |
12629 | 8.28k | case ZEND_AST_CALL: |
12630 | 111k | case ZEND_AST_METHOD_CALL: |
12631 | 111k | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12632 | 111k | case ZEND_AST_STATIC_CALL: |
12633 | 111k | case ZEND_AST_PIPE: |
12634 | 111k | zend_compile_memoized_expr(result, ast, BP_VAR_W); |
12635 | | /* This might not actually produce an opcode, e.g. for expressions evaluated at comptime. */ |
12636 | 111k | return NULL; |
12637 | 252k | } |
12638 | 252k | } |
12639 | | |
12640 | 5.66M | switch (ast->kind) { |
12641 | 746k | case ZEND_AST_VAR: |
12642 | 746k | return zend_compile_simple_var(result, ast, type, false); |
12643 | 216k | case ZEND_AST_DIM: |
12644 | 216k | return zend_compile_dim(result, ast, type, by_ref); |
12645 | 64.3k | case ZEND_AST_PROP: |
12646 | 111k | case ZEND_AST_NULLSAFE_PROP: |
12647 | 111k | return zend_compile_prop(result, ast, type, by_ref); |
12648 | 9.52k | case ZEND_AST_STATIC_PROP: |
12649 | 9.52k | return zend_compile_static_prop(result, ast, type, by_ref, false); |
12650 | 3.13M | case ZEND_AST_CALL: |
12651 | 3.13M | zend_compile_call(result, ast, type); |
12652 | 3.13M | return NULL; |
12653 | 122k | case ZEND_AST_METHOD_CALL: |
12654 | 126k | case ZEND_AST_NULLSAFE_METHOD_CALL: |
12655 | 126k | zend_compile_method_call(result, ast, type); |
12656 | 126k | return NULL; |
12657 | 17.7k | case ZEND_AST_STATIC_CALL: |
12658 | 17.7k | zend_compile_static_call(result, ast, type); |
12659 | 17.7k | return NULL; |
12660 | 36.1k | case ZEND_AST_PIPE: |
12661 | 36.1k | zend_compile_pipe(result, ast, type); |
12662 | 36.1k | return NULL; |
12663 | 2.93k | case ZEND_AST_ZNODE: |
12664 | 2.93k | *result = *zend_ast_get_znode(ast); |
12665 | 2.93k | return NULL; |
12666 | 978 | case ZEND_AST_ASSIGN_REF: |
12667 | 978 | zend_compile_assign_ref(result, ast, type); |
12668 | 978 | return NULL; |
12669 | 34 | case ZEND_AST_ASSIGN: |
12670 | 34 | zend_compile_assign(result, ast, false, type); |
12671 | 34 | return NULL; |
12672 | 1.26M | default: |
12673 | 1.26M | if (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET) { |
12674 | 116 | zend_error_noreturn(E_COMPILE_ERROR, |
12675 | 116 | "Cannot use temporary expression in write context"); |
12676 | 116 | } |
12677 | | |
12678 | 1.26M | zend_compile_expr(result, ast); |
12679 | 1.26M | return NULL; |
12680 | 5.66M | } |
12681 | 5.66M | } |
12682 | | |
12683 | | static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12684 | 5.77M | { |
12685 | 5.77M | zend_check_stack_limit(); |
12686 | | |
12687 | 5.77M | uint32_t checkpoint = zend_short_circuiting_checkpoint(); |
12688 | 5.77M | zend_op *opcode = zend_compile_var_inner(result, ast, type, by_ref); |
12689 | 5.77M | zend_short_circuiting_commit(checkpoint, result, ast); |
12690 | 5.77M | #if ZEND_DEBUG |
12691 | 5.77M | if (result |
12692 | 5.77M | && (type == BP_VAR_R || type == BP_VAR_IS) |
12693 | | /* Don't check memoized result, as it will force BP_VAR_W even for BP_VAR_IS. */ |
12694 | 5.11M | && CG(memoize_mode) == ZEND_MEMOIZE_NONE |
12695 | 5.77M | ) { |
12696 | | /* BP_VAR_{R,IS} is not allowed to produce IS_VAR. */ |
12697 | 4.99M | ZEND_ASSERT(result->op_type != IS_VAR); |
12698 | 4.99M | } |
12699 | 5.77M | #endif |
12700 | 5.77M | return opcode; |
12701 | 5.77M | } |
12702 | | |
12703 | | static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref) /* {{{ */ |
12704 | 1.40M | { |
12705 | 1.40M | zend_check_stack_limit(); |
12706 | | |
12707 | 1.40M | switch (ast->kind) { |
12708 | 585k | case ZEND_AST_VAR: |
12709 | 585k | return zend_compile_simple_var(result, ast, type, true); |
12710 | 153k | case ZEND_AST_DIM: |
12711 | 153k | return zend_delayed_compile_dim(result, ast, type, by_ref); |
12712 | 407k | case ZEND_AST_PROP: |
12713 | 441k | case ZEND_AST_NULLSAFE_PROP: |
12714 | 441k | { |
12715 | 441k | zend_op *opline = zend_delayed_compile_prop(result, ast, type); |
12716 | 441k | if (by_ref) { |
12717 | 1.27k | opline->extended_value |= ZEND_FETCH_REF; |
12718 | 1.27k | } |
12719 | 441k | return opline; |
12720 | 407k | } |
12721 | 6.64k | case ZEND_AST_STATIC_PROP: |
12722 | 6.64k | return zend_compile_static_prop(result, ast, type, by_ref, true); |
12723 | 76.2k | case ZEND_AST_CONST: |
12724 | 76.2k | if (!(ast->attr & ZEND_CONST_OBJECT_FETCH)) { |
12725 | 42.2k | break; |
12726 | 42.2k | } |
12727 | 33.9k | zend_compile_const(result, ast, type); |
12728 | 33.9k | if (!(type == BP_VAR_R || type == BP_VAR_IS) && result->op_type == IS_CONST) { |
12729 | 30 | znode op1 = *result; |
12730 | | /* Intentionally IS_VAR result. */ |
12731 | 30 | zend_emit_op(result, ZEND_QM_ASSIGN, &op1, NULL); |
12732 | 30 | } |
12733 | 33.9k | return NULL; |
12734 | 1.65k | case ZEND_AST_CLASS_CONST: |
12735 | 1.65k | if (!(ast->attr & ZEND_CONST_OBJECT_FETCH)) { |
12736 | 539 | break; |
12737 | 539 | } |
12738 | 1.12k | zend_compile_class_const(result, ast, type); |
12739 | 1.12k | if (!(type == BP_VAR_R || type == BP_VAR_IS) && result->op_type == IS_CONST) { |
12740 | 6 | znode op1 = *result; |
12741 | | /* Intentionally IS_VAR result. */ |
12742 | 6 | zend_emit_op(result, ZEND_QM_ASSIGN, &op1, NULL); |
12743 | 6 | } |
12744 | 1.12k | return NULL; |
12745 | 1.40M | } |
12746 | | |
12747 | 183k | return zend_compile_var(result, ast, type, false); |
12748 | 1.40M | } |
12749 | | /* }}} */ |
12750 | | |
12751 | | bool zend_try_ct_eval_cast(zval *result, uint32_t type, zval *op1) |
12752 | 21.9k | { |
12753 | | /* NAN warns when casting */ |
12754 | 21.9k | if (UNEXPECTED(Z_TYPE_P(op1) == IS_DOUBLE && zend_isnan(Z_DVAL_P(op1)))) { |
12755 | 0 | return false; |
12756 | 0 | } |
12757 | 21.9k | switch (type) { |
12758 | 77 | case _IS_BOOL: |
12759 | 77 | ZVAL_BOOL(result, zend_is_true(op1)); |
12760 | 77 | return true; |
12761 | 564 | case IS_LONG: |
12762 | 564 | if (Z_TYPE_P(op1) == IS_DOUBLE && !ZEND_DOUBLE_FITS_LONG(Z_DVAL_P((op1)))) { |
12763 | 133 | return false; |
12764 | 133 | } |
12765 | 431 | ZVAL_LONG(result, zval_get_long(op1)); |
12766 | 431 | return true; |
12767 | 1.28k | case IS_DOUBLE: |
12768 | 1.28k | ZVAL_DOUBLE(result, zval_get_double(op1)); |
12769 | 1.28k | return true; |
12770 | 18.3k | case IS_STRING: |
12771 | | /* Conversion from double to string takes into account run-time |
12772 | | 'precision' setting and cannot be evaluated at compile-time */ |
12773 | 18.3k | if (Z_TYPE_P(op1) != IS_ARRAY && Z_TYPE_P(op1) != IS_DOUBLE) { |
12774 | 17.3k | ZVAL_STR(result, zval_get_string(op1)); |
12775 | 17.3k | return true; |
12776 | 17.3k | } |
12777 | 1.00k | break; |
12778 | 1.00k | case IS_ARRAY: |
12779 | 655 | ZVAL_COPY(result, op1); |
12780 | 655 | convert_to_array(result); |
12781 | 655 | return true; |
12782 | 21.9k | } |
12783 | 2.01k | return false; |
12784 | 21.9k | } |
12785 | | |
12786 | | static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */ |
12787 | 11.0M | { |
12788 | 11.0M | zend_ast *ast = *ast_ptr; |
12789 | 11.0M | zval result; |
12790 | | |
12791 | 11.0M | if (!ast) { |
12792 | 2.93M | return; |
12793 | 2.93M | } |
12794 | | |
12795 | 8.14M | zend_check_stack_limit(); |
12796 | | |
12797 | 8.14M | switch (ast->kind) { |
12798 | 75.7k | case ZEND_AST_BINARY_OP: |
12799 | 75.7k | zend_eval_const_expr(&ast->child[0]); |
12800 | 75.7k | zend_eval_const_expr(&ast->child[1]); |
12801 | 75.7k | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12802 | 32.4k | return; |
12803 | 32.4k | } |
12804 | | |
12805 | 43.3k | if (!zend_try_ct_eval_binary_op(&result, ast->attr, |
12806 | 43.3k | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])) |
12807 | 43.3k | ) { |
12808 | 26.4k | return; |
12809 | 26.4k | } |
12810 | 16.9k | break; |
12811 | 16.9k | case ZEND_AST_GREATER: |
12812 | 3.95k | case ZEND_AST_GREATER_EQUAL: |
12813 | 3.95k | zend_eval_const_expr(&ast->child[0]); |
12814 | 3.95k | zend_eval_const_expr(&ast->child[1]); |
12815 | 3.95k | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12816 | 2.22k | return; |
12817 | 2.22k | } |
12818 | | |
12819 | 1.72k | zend_ct_eval_greater(&result, ast->kind, |
12820 | 1.72k | zend_ast_get_zval(ast->child[0]), zend_ast_get_zval(ast->child[1])); |
12821 | 1.72k | break; |
12822 | 2.17k | case ZEND_AST_AND: |
12823 | 3.49k | case ZEND_AST_OR: |
12824 | 3.49k | { |
12825 | 3.49k | bool child0_is_true, child1_is_true; |
12826 | 3.49k | zend_eval_const_expr(&ast->child[0]); |
12827 | 3.49k | zend_eval_const_expr(&ast->child[1]); |
12828 | 3.49k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12829 | 1.97k | return; |
12830 | 1.97k | } |
12831 | | |
12832 | 1.51k | child0_is_true = zend_is_true(zend_ast_get_zval(ast->child[0])); |
12833 | 1.51k | if (child0_is_true == (ast->kind == ZEND_AST_OR)) { |
12834 | 259 | ZVAL_BOOL(&result, ast->kind == ZEND_AST_OR); |
12835 | 259 | break; |
12836 | 259 | } |
12837 | | |
12838 | 1.26k | if (ast->child[1]->kind != ZEND_AST_ZVAL) { |
12839 | 294 | return; |
12840 | 294 | } |
12841 | | |
12842 | 966 | child1_is_true = zend_is_true(zend_ast_get_zval(ast->child[1])); |
12843 | 966 | if (ast->kind == ZEND_AST_OR) { |
12844 | 142 | ZVAL_BOOL(&result, child0_is_true || child1_is_true); |
12845 | 824 | } else { |
12846 | 824 | ZVAL_BOOL(&result, child0_is_true && child1_is_true); |
12847 | 824 | } |
12848 | 966 | break; |
12849 | 1.26k | } |
12850 | 8.63k | case ZEND_AST_UNARY_OP: |
12851 | 8.63k | zend_eval_const_expr(&ast->child[0]); |
12852 | 8.63k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12853 | 3.18k | return; |
12854 | 3.18k | } |
12855 | | |
12856 | 5.45k | if (!zend_try_ct_eval_unary_op(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
12857 | 1.41k | return; |
12858 | 1.41k | } |
12859 | 4.04k | break; |
12860 | 4.04k | case ZEND_AST_UNARY_PLUS: |
12861 | 17.7k | case ZEND_AST_UNARY_MINUS: |
12862 | 17.7k | zend_eval_const_expr(&ast->child[0]); |
12863 | 17.7k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12864 | 3.07k | return; |
12865 | 3.07k | } |
12866 | | |
12867 | 14.6k | if (!zend_try_ct_eval_unary_pm(&result, ast->kind, zend_ast_get_zval(ast->child[0]))) { |
12868 | 2.36k | return; |
12869 | 2.36k | } |
12870 | 12.2k | break; |
12871 | 12.2k | case ZEND_AST_COALESCE: |
12872 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12873 | 1.12k | if (ast->child[0]->kind == ZEND_AST_DIM) { |
12874 | 488 | ast->child[0]->attr |= ZEND_DIM_IS; |
12875 | 488 | } |
12876 | 1.12k | zend_eval_const_expr(&ast->child[0]); |
12877 | | |
12878 | 1.12k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12879 | | /* ensure everything was compile-time evaluated at least once */ |
12880 | 792 | zend_eval_const_expr(&ast->child[1]); |
12881 | 792 | return; |
12882 | 792 | } |
12883 | | |
12884 | 333 | if (Z_TYPE_P(zend_ast_get_zval(ast->child[0])) == IS_NULL) { |
12885 | 265 | zend_eval_const_expr(&ast->child[1]); |
12886 | 265 | *ast_ptr = ast->child[1]; |
12887 | 265 | ast->child[1] = NULL; |
12888 | 265 | zend_ast_destroy(ast); |
12889 | 265 | } else { |
12890 | 68 | *ast_ptr = ast->child[0]; |
12891 | 68 | ast->child[0] = NULL; |
12892 | 68 | zend_ast_destroy(ast); |
12893 | 68 | } |
12894 | 333 | return; |
12895 | 1.97k | case ZEND_AST_CONDITIONAL: |
12896 | 1.97k | { |
12897 | 1.97k | zend_ast **child, *child_ast; |
12898 | 1.97k | zend_eval_const_expr(&ast->child[0]); |
12899 | 1.97k | if (ast->child[0]->kind != ZEND_AST_ZVAL) { |
12900 | | /* ensure everything was compile-time evaluated at least once */ |
12901 | 1.71k | if (ast->child[1]) { |
12902 | 1.03k | zend_eval_const_expr(&ast->child[1]); |
12903 | 1.03k | } |
12904 | 1.71k | zend_eval_const_expr(&ast->child[2]); |
12905 | 1.71k | return; |
12906 | 1.71k | } |
12907 | | |
12908 | 258 | child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))]; |
12909 | 258 | if (*child == NULL) { |
12910 | 133 | child--; |
12911 | 133 | } |
12912 | 258 | child_ast = *child; |
12913 | 258 | *child = NULL; |
12914 | 258 | zend_ast_destroy(ast); |
12915 | 258 | *ast_ptr = child_ast; |
12916 | 258 | zend_eval_const_expr(ast_ptr); |
12917 | 258 | return; |
12918 | 1.97k | } |
12919 | 1.24M | case ZEND_AST_DIM: |
12920 | 1.24M | { |
12921 | | /* constant expression should be always read context ... */ |
12922 | 1.24M | const zval *container, *dim; |
12923 | | |
12924 | 1.24M | if (ast->child[1] == NULL) { |
12925 | 37 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
12926 | 37 | } |
12927 | | |
12928 | | /* Set isset fetch indicator here, opcache disallows runtime altering of the AST */ |
12929 | 1.24M | if ((ast->attr & ZEND_DIM_IS) && ast->child[0]->kind == ZEND_AST_DIM) { |
12930 | 687 | ast->child[0]->attr |= ZEND_DIM_IS; |
12931 | 687 | } |
12932 | | |
12933 | 1.24M | zend_eval_const_expr(&ast->child[0]); |
12934 | 1.24M | zend_eval_const_expr(&ast->child[1]); |
12935 | 1.24M | if (ast->child[0]->kind != ZEND_AST_ZVAL || ast->child[1]->kind != ZEND_AST_ZVAL) { |
12936 | 1.22M | return; |
12937 | 1.22M | } |
12938 | | |
12939 | 13.8k | container = zend_ast_get_zval(ast->child[0]); |
12940 | 13.8k | dim = zend_ast_get_zval(ast->child[1]); |
12941 | | |
12942 | 13.8k | if (Z_TYPE_P(container) == IS_ARRAY) { |
12943 | 9.28k | zval *el; |
12944 | 9.28k | if (Z_TYPE_P(dim) == IS_LONG) { |
12945 | 789 | el = zend_hash_index_find(Z_ARR_P(container), Z_LVAL_P(dim)); |
12946 | 789 | if (el) { |
12947 | 459 | ZVAL_COPY(&result, el); |
12948 | 459 | } else { |
12949 | 330 | return; |
12950 | 330 | } |
12951 | 8.49k | } else if (Z_TYPE_P(dim) == IS_STRING) { |
12952 | 8.34k | el = zend_symtable_find(Z_ARR_P(container), Z_STR_P(dim)); |
12953 | 8.34k | if (el) { |
12954 | 524 | ZVAL_COPY(&result, el); |
12955 | 7.81k | } else { |
12956 | 7.81k | return; |
12957 | 7.81k | } |
12958 | 8.34k | } else { |
12959 | 156 | return; /* warning... handle at runtime */ |
12960 | 156 | } |
12961 | 9.28k | } else if (Z_TYPE_P(container) == IS_STRING) { |
12962 | 4.15k | zend_long offset; |
12963 | 4.15k | uint8_t c; |
12964 | 4.15k | if (Z_TYPE_P(dim) == IS_LONG) { |
12965 | 2.82k | offset = Z_LVAL_P(dim); |
12966 | 2.82k | } else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) { |
12967 | 952 | return; |
12968 | 952 | } |
12969 | 3.19k | if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) { |
12970 | 2.93k | return; |
12971 | 2.93k | } |
12972 | 268 | c = (uint8_t) Z_STRVAL_P(container)[offset]; |
12973 | 268 | ZVAL_CHAR(&result, c); |
12974 | 397 | } else if (Z_TYPE_P(container) <= IS_FALSE) { |
12975 | 8 | return; /* warning... handle at runtime */ |
12976 | 389 | } else { |
12977 | 389 | return; |
12978 | 389 | } |
12979 | 1.25k | break; |
12980 | 13.8k | } |
12981 | 2.70M | case ZEND_AST_ARRAY: |
12982 | 2.70M | if (!zend_try_ct_eval_array(&result, ast)) { |
12983 | 2.67M | return; |
12984 | 2.67M | } |
12985 | 33.3k | break; |
12986 | 33.3k | case ZEND_AST_MAGIC_CONST: |
12987 | 1.94k | if (!zend_try_ct_eval_magic_const(&result, ast)) { |
12988 | 170 | return; |
12989 | 170 | } |
12990 | 1.77k | break; |
12991 | 90.8k | case ZEND_AST_CONST: |
12992 | 90.8k | { |
12993 | 90.8k | zend_ast *name_ast = ast->child[0]; |
12994 | 90.8k | bool is_fully_qualified; |
12995 | 90.8k | zend_string *resolved_name = zend_resolve_const_name( |
12996 | 90.8k | zend_ast_get_str(name_ast), name_ast->attr, &is_fully_qualified); |
12997 | | |
12998 | 90.8k | if (!zend_try_ct_eval_const(&result, resolved_name, is_fully_qualified)) { |
12999 | 84.3k | zend_string_release_ex(resolved_name, 0); |
13000 | 84.3k | return; |
13001 | 84.3k | } |
13002 | | |
13003 | 6.46k | zend_string_release_ex(resolved_name, 0); |
13004 | 6.46k | break; |
13005 | 90.8k | } |
13006 | 926k | case ZEND_AST_CLASS_CONST: |
13007 | 926k | { |
13008 | 926k | zend_ast *class_ast; |
13009 | 926k | zend_ast *name_ast; |
13010 | 926k | zend_string *resolved_name; |
13011 | | |
13012 | 926k | zend_eval_const_expr(&ast->child[0]); |
13013 | 926k | zend_eval_const_expr(&ast->child[1]); |
13014 | | |
13015 | 926k | if (UNEXPECTED(ast->child[1]->kind != ZEND_AST_ZVAL |
13016 | 926k | || Z_TYPE_P(zend_ast_get_zval(ast->child[1])) != IS_STRING)) { |
13017 | 884 | return; |
13018 | 884 | } |
13019 | | |
13020 | 925k | class_ast = ast->child[0]; |
13021 | 925k | name_ast = ast->child[1]; |
13022 | | |
13023 | 925k | if (class_ast->kind != ZEND_AST_ZVAL || name_ast->kind != ZEND_AST_ZVAL) { |
13024 | 846k | return; |
13025 | 846k | } |
13026 | | |
13027 | 78.8k | resolved_name = zend_resolve_class_name_ast(class_ast); |
13028 | 78.8k | if (!zend_try_ct_eval_class_const(&result, resolved_name, zend_ast_get_str(name_ast))) { |
13029 | 78.6k | zend_string_release_ex(resolved_name, 0); |
13030 | 78.6k | return; |
13031 | 78.6k | } |
13032 | | |
13033 | 208 | zend_string_release_ex(resolved_name, 0); |
13034 | 208 | break; |
13035 | 78.8k | } |
13036 | 3.16k | case ZEND_AST_CLASS_NAME: |
13037 | 3.16k | { |
13038 | 3.16k | zend_ast *class_ast = ast->child[0]; |
13039 | 3.16k | if (!zend_try_compile_const_expr_resolve_class_name(&result, class_ast)) { |
13040 | 1.49k | return; |
13041 | 1.49k | } |
13042 | 1.66k | break; |
13043 | 3.16k | } |
13044 | | // TODO: We should probably use zend_ast_apply to recursively walk nodes without |
13045 | | // special handling. It is required that all nodes that are part of a const expr |
13046 | | // are visited. Probably we should be distinguishing evaluation of const expr and |
13047 | | // normal exprs here. |
13048 | 4.36k | case ZEND_AST_ARG_LIST: |
13049 | 4.36k | { |
13050 | 4.36k | zend_ast_list *list = zend_ast_get_list(ast); |
13051 | 5.71k | for (uint32_t i = 0; i < list->children; i++) { |
13052 | 1.35k | zend_eval_const_expr(&list->child[i]); |
13053 | 1.35k | } |
13054 | 4.36k | return; |
13055 | 3.16k | } |
13056 | 4.36k | case ZEND_AST_NEW: |
13057 | 4.36k | zend_eval_const_expr(&ast->child[0]); |
13058 | 4.36k | zend_eval_const_expr(&ast->child[1]); |
13059 | 4.36k | return; |
13060 | 228 | case ZEND_AST_NAMED_ARG: |
13061 | 228 | zend_eval_const_expr(&ast->child[1]); |
13062 | 228 | return; |
13063 | 38.3k | case ZEND_AST_CONST_ENUM_INIT: |
13064 | 38.3k | zend_eval_const_expr(&ast->child[3]); |
13065 | 38.3k | return; |
13066 | 1.94k | case ZEND_AST_PROP: |
13067 | 2.51k | case ZEND_AST_NULLSAFE_PROP: |
13068 | 2.51k | zend_eval_const_expr(&ast->child[0]); |
13069 | 2.51k | zend_eval_const_expr(&ast->child[1]); |
13070 | 2.51k | return; |
13071 | 7.33k | case ZEND_AST_CAST: |
13072 | 7.33k | if (ast->attr == IS_NULL) { |
13073 | 8 | zend_error_noreturn(E_COMPILE_ERROR, "The (unset) cast is no longer supported"); |
13074 | 8 | } |
13075 | 7.32k | zend_eval_const_expr(&ast->child[0]); |
13076 | 7.32k | if (ast->child[0]->kind == ZEND_AST_ZVAL |
13077 | 3.16k | && zend_try_ct_eval_cast(&result, ast->attr, zend_ast_get_zval(ast->child[0]))) { |
13078 | 2.79k | break; |
13079 | 2.79k | } |
13080 | 4.52k | return; |
13081 | 2.99M | default: |
13082 | 2.99M | return; |
13083 | 8.14M | } |
13084 | | |
13085 | 83.2k | zend_ast_destroy(ast); |
13086 | 83.2k | *ast_ptr = zend_ast_create_zval(&result); |
13087 | 83.2k | } |
13088 | | /* }}} */ |