/src/php-src/Zend/zend_ast.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Zend Engine | |
4 | | +----------------------------------------------------------------------+ |
5 | | | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
6 | | +----------------------------------------------------------------------+ |
7 | | | This source file is subject to version 2.00 of the Zend license, | |
8 | | | that is bundled with this package in the file LICENSE, and is | |
9 | | | available through the world-wide-web at the following url: | |
10 | | | http://www.zend.com/license/2_00.txt. | |
11 | | | If you did not receive a copy of the Zend license and are unable to | |
12 | | | obtain it through the world-wide-web, please send a note to | |
13 | | | license@zend.com so we can mail you a copy immediately. | |
14 | | +----------------------------------------------------------------------+ |
15 | | | Authors: Bob Weinand <bwoebi@php.net> | |
16 | | | Dmitry Stogov <dmitry@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | #include "zend_ast.h" |
21 | | #include "zend_API.h" |
22 | | #include "zend_operators.h" |
23 | | #include "zend_language_parser.h" |
24 | | #include "zend_smart_str.h" |
25 | | #include "zend_exceptions.h" |
26 | | #include "zend_closures.h" |
27 | | #include "zend_constants.h" |
28 | | #include "zend_enum.h" |
29 | | |
30 | | ZEND_API zend_ast_process_t zend_ast_process = NULL; |
31 | | |
32 | 11.2k | static inline void *zend_ast_alloc(size_t size) { |
33 | 11.2k | return zend_arena_alloc(&CG(ast_arena), size); |
34 | 11.2k | } |
35 | | |
36 | 113 | static inline void *zend_ast_realloc(const void *old, size_t old_size, size_t new_size) { |
37 | 113 | void *new = zend_ast_alloc(new_size); |
38 | 113 | memcpy(new, old, old_size); |
39 | 113 | return new; |
40 | 113 | } |
41 | | |
42 | 1.31k | static inline size_t zend_ast_list_size(uint32_t children) { |
43 | 1.31k | return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children; |
44 | 1.31k | } |
45 | | |
46 | 1 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(const znode *node) { |
47 | 1 | zend_ast_znode *ast; |
48 | | |
49 | 1 | ast = zend_ast_alloc(sizeof(zend_ast_znode)); |
50 | 1 | ast->kind = ZEND_AST_ZNODE; |
51 | 1 | ast->attr = 0; |
52 | 1 | ast->lineno = CG(zend_lineno); |
53 | 1 | ast->node = *node; |
54 | 1 | return (zend_ast *) ast; |
55 | 1 | } |
56 | | |
57 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_fcc(zend_ast *args) { |
58 | 0 | zend_ast_fcc *ast; |
59 | |
|
60 | 0 | ast = zend_ast_alloc(sizeof(zend_ast_fcc)); |
61 | 0 | ast->kind = ZEND_AST_CALLABLE_CONVERT; |
62 | 0 | ast->attr = 0; |
63 | 0 | ast->lineno = CG(zend_lineno); |
64 | 0 | ast->args = args; |
65 | 0 | ZEND_MAP_PTR_INIT(ast->fptr, NULL); |
66 | |
|
67 | 0 | return (zend_ast *) ast; |
68 | 0 | } |
69 | | |
70 | 5.35k | static zend_always_inline zend_ast * zend_ast_create_zval_int(const zval *zv, uint32_t attr, uint32_t lineno) { |
71 | 5.35k | zend_ast_zval *ast; |
72 | | |
73 | 5.35k | ast = zend_ast_alloc(sizeof(zend_ast_zval)); |
74 | 5.35k | ast->kind = ZEND_AST_ZVAL; |
75 | 5.35k | ast->attr = attr; |
76 | 5.35k | ZVAL_COPY_VALUE(&ast->val, zv); |
77 | 5.35k | Z_LINENO(ast->val) = lineno; |
78 | 5.35k | return (zend_ast *) ast; |
79 | 5.35k | } |
80 | | |
81 | 5.29k | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(const zval *zv, uint32_t lineno) { |
82 | 5.29k | return zend_ast_create_zval_int(zv, 0, lineno); |
83 | 5.29k | } |
84 | | |
85 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(const zval *zv, zend_ast_attr attr) { |
86 | 0 | return zend_ast_create_zval_int(zv, attr, CG(zend_lineno)); |
87 | 0 | } |
88 | | |
89 | 49 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(const zval *zv) { |
90 | 49 | return zend_ast_create_zval_int(zv, 0, CG(zend_lineno)); |
91 | 49 | } |
92 | | |
93 | 1 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) { |
94 | 1 | zval zv; |
95 | 1 | ZVAL_STR(&zv, str); |
96 | 1 | return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno)); |
97 | 1 | } |
98 | | |
99 | 12 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) { |
100 | 12 | zval zv; |
101 | 12 | ZVAL_LONG(&zv, lval); |
102 | 12 | return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno)); |
103 | 12 | } |
104 | | |
105 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) { |
106 | 0 | zend_ast_zval *ast; |
107 | |
|
108 | 0 | ast = zend_ast_alloc(sizeof(zend_ast_zval)); |
109 | 0 | ast->kind = ZEND_AST_CONSTANT; |
110 | 0 | ast->attr = attr; |
111 | 0 | ZVAL_STR(&ast->val, name); |
112 | 0 | Z_LINENO(ast->val) = CG(zend_lineno); |
113 | 0 | return (zend_ast *) ast; |
114 | 0 | } |
115 | | |
116 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_op_array(zend_op_array *op_array) { |
117 | 0 | zend_ast_op_array *ast; |
118 | |
|
119 | 0 | ast = zend_ast_alloc(sizeof(zend_ast_op_array)); |
120 | 0 | ast->kind = ZEND_AST_OP_ARRAY; |
121 | 0 | ast->attr = 0; |
122 | 0 | ast->lineno = CG(zend_lineno); |
123 | 0 | ast->op_array = op_array; |
124 | |
|
125 | 0 | return (zend_ast *) ast; |
126 | 0 | } |
127 | | |
128 | 4 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name) { |
129 | 4 | zend_string *name_str = zend_ast_get_str(name); |
130 | 4 | if (zend_string_equals_ci(name_str, ZSTR_KNOWN(ZEND_STR_CLASS))) { |
131 | 0 | zend_string_release(name_str); |
132 | 0 | return zend_ast_create(ZEND_AST_CLASS_NAME, class_name); |
133 | 4 | } else { |
134 | 4 | return zend_ast_create(ZEND_AST_CLASS_CONST, class_name, name); |
135 | 4 | } |
136 | 4 | } |
137 | | |
138 | | ZEND_API zend_ast *zend_ast_create_decl( |
139 | | zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment, |
140 | | zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4 |
141 | 36 | ) { |
142 | 36 | zend_ast_decl *ast; |
143 | | |
144 | 36 | ast = zend_ast_alloc(sizeof(zend_ast_decl)); |
145 | 36 | ast->kind = kind; |
146 | 36 | ast->attr = 0; |
147 | 36 | ast->start_lineno = start_lineno; |
148 | 36 | ast->end_lineno = CG(zend_lineno); |
149 | 36 | ast->flags = flags; |
150 | 36 | ast->doc_comment = doc_comment; |
151 | 36 | ast->name = name; |
152 | 36 | ast->child[0] = child0; |
153 | 36 | ast->child[1] = child1; |
154 | 36 | ast->child[2] = child2; |
155 | 36 | ast->child[3] = child3; |
156 | 36 | ast->child[4] = child4; |
157 | | |
158 | 36 | return (zend_ast *) ast; |
159 | 36 | } |
160 | | |
161 | 336 | static bool zend_ast_is_placeholder_arg(zend_ast *arg) { |
162 | 336 | return arg->kind == ZEND_AST_PLACEHOLDER_ARG |
163 | 336 | || (arg->kind == ZEND_AST_NAMED_ARG |
164 | 0 | && arg->child[1]->kind == ZEND_AST_PLACEHOLDER_ARG); |
165 | 336 | } |
166 | | |
167 | | #if ZEND_AST_SPEC |
168 | 26 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) { |
169 | 26 | zend_ast *ast; |
170 | | |
171 | 26 | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0); |
172 | 26 | ast = zend_ast_alloc(zend_ast_size(0)); |
173 | 26 | ast->kind = kind; |
174 | 26 | ast->attr = 0; |
175 | 26 | ast->lineno = CG(zend_lineno); |
176 | | |
177 | 26 | return ast; |
178 | 26 | } |
179 | | |
180 | 2.40k | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) { |
181 | 2.40k | zend_ast *ast; |
182 | 2.40k | uint32_t lineno; |
183 | | |
184 | 2.40k | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1); |
185 | 2.40k | ast = zend_ast_alloc(zend_ast_size(1)); |
186 | 2.40k | ast->kind = kind; |
187 | 2.40k | ast->attr = 0; |
188 | 2.40k | ast->child[0] = child; |
189 | 2.40k | if (child) { |
190 | 2.40k | lineno = zend_ast_get_lineno(child); |
191 | 2.40k | } else { |
192 | 0 | lineno = CG(zend_lineno); |
193 | 0 | } |
194 | 2.40k | ast->lineno = lineno; |
195 | | |
196 | 2.40k | return ast; |
197 | 2.40k | } |
198 | | |
199 | 1.99k | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) { |
200 | 1.99k | zend_ast *ast; |
201 | 1.99k | uint32_t lineno; |
202 | | |
203 | 1.99k | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2); |
204 | 1.99k | ast = zend_ast_alloc(zend_ast_size(2)); |
205 | 1.99k | ast->kind = kind; |
206 | 1.99k | ast->attr = 0; |
207 | 1.99k | ast->child[0] = child1; |
208 | 1.99k | ast->child[1] = child2; |
209 | 1.99k | if (child1) { |
210 | 1.99k | lineno = zend_ast_get_lineno(child1); |
211 | 1.99k | } else if (child2) { |
212 | 0 | lineno = zend_ast_get_lineno(child2); |
213 | 0 | } else { |
214 | 0 | lineno = CG(zend_lineno); |
215 | 0 | } |
216 | 1.99k | ast->lineno = lineno; |
217 | | |
218 | 1.99k | return ast; |
219 | 1.99k | } |
220 | | |
221 | 183 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3) { |
222 | 183 | zend_ast *ast; |
223 | 183 | uint32_t lineno; |
224 | | |
225 | 183 | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3); |
226 | 183 | ast = zend_ast_alloc(zend_ast_size(3)); |
227 | 183 | ast->kind = kind; |
228 | 183 | ast->attr = 0; |
229 | 183 | ast->child[0] = child1; |
230 | 183 | ast->child[1] = child2; |
231 | 183 | ast->child[2] = child3; |
232 | 183 | if (child1) { |
233 | 183 | lineno = zend_ast_get_lineno(child1); |
234 | 183 | } else if (child2) { |
235 | 0 | lineno = zend_ast_get_lineno(child2); |
236 | 0 | } else if (child3) { |
237 | 0 | lineno = zend_ast_get_lineno(child3); |
238 | 0 | } else { |
239 | 0 | lineno = CG(zend_lineno); |
240 | 0 | } |
241 | 183 | ast->lineno = lineno; |
242 | | |
243 | 183 | return ast; |
244 | 183 | } |
245 | | |
246 | 88 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_4(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4) { |
247 | 88 | zend_ast *ast; |
248 | 88 | uint32_t lineno; |
249 | | |
250 | 88 | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4); |
251 | 88 | ast = zend_ast_alloc(zend_ast_size(4)); |
252 | 88 | ast->kind = kind; |
253 | 88 | ast->attr = 0; |
254 | 88 | ast->child[0] = child1; |
255 | 88 | ast->child[1] = child2; |
256 | 88 | ast->child[2] = child3; |
257 | 88 | ast->child[3] = child4; |
258 | 88 | if (child1) { |
259 | 88 | lineno = zend_ast_get_lineno(child1); |
260 | 88 | } else if (child2) { |
261 | 0 | lineno = zend_ast_get_lineno(child2); |
262 | 0 | } else if (child3) { |
263 | 0 | lineno = zend_ast_get_lineno(child3); |
264 | 0 | } else if (child4) { |
265 | 0 | lineno = zend_ast_get_lineno(child4); |
266 | 0 | } else { |
267 | 0 | lineno = CG(zend_lineno); |
268 | 0 | } |
269 | 88 | ast->lineno = lineno; |
270 | | |
271 | 88 | return ast; |
272 | 88 | } |
273 | | |
274 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_5(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4, zend_ast *child5) { |
275 | 0 | zend_ast *ast; |
276 | 0 | uint32_t lineno; |
277 | |
|
278 | 0 | ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 5); |
279 | 0 | ast = zend_ast_alloc(zend_ast_size(5)); |
280 | 0 | ast->kind = kind; |
281 | 0 | ast->attr = 0; |
282 | 0 | ast->child[0] = child1; |
283 | 0 | ast->child[1] = child2; |
284 | 0 | ast->child[2] = child3; |
285 | 0 | ast->child[3] = child4; |
286 | 0 | ast->child[4] = child5; |
287 | 0 | if (child1) { |
288 | 0 | lineno = zend_ast_get_lineno(child1); |
289 | 0 | } else if (child2) { |
290 | 0 | lineno = zend_ast_get_lineno(child2); |
291 | 0 | } else if (child3) { |
292 | 0 | lineno = zend_ast_get_lineno(child3); |
293 | 0 | } else if (child4) { |
294 | 0 | lineno = zend_ast_get_lineno(child4); |
295 | 0 | } else if (child5) { |
296 | 0 | lineno = zend_ast_get_lineno(child5); |
297 | 0 | } else { |
298 | 0 | lineno = CG(zend_lineno); |
299 | 0 | } |
300 | 0 | ast->lineno = lineno; |
301 | |
|
302 | 0 | return ast; |
303 | 0 | } |
304 | | |
305 | | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_va( |
306 | 8 | zend_ast_kind kind, zend_ast_attr attr, va_list *va) { |
307 | 8 | uint32_t lineno = (uint32_t)-1; |
308 | 8 | uint32_t children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT; |
309 | 8 | zend_ast *ast = zend_ast_alloc(zend_ast_size(children)); |
310 | 8 | ast->kind = kind; |
311 | 8 | ast->attr = attr; |
312 | 56 | for (uint32_t i = 0; i < children; i++) { |
313 | 48 | ast->child[i] = va_arg(*va, zend_ast *); |
314 | 48 | if (lineno == (uint32_t)-1 && ast->child[i]) { |
315 | 8 | lineno = zend_ast_get_lineno(ast->child[i]); |
316 | 8 | } |
317 | 48 | } |
318 | 8 | if (lineno == (uint32_t)-1) { |
319 | 0 | lineno = CG(zend_lineno); |
320 | 0 | } |
321 | 8 | ast->lineno = lineno; |
322 | 8 | return ast; |
323 | 8 | } |
324 | | |
325 | 0 | ZEND_API zend_ast * zend_ast_create_n(unsigned kind, ...) { |
326 | 0 | va_list va; |
327 | 0 | va_start(va, kind); |
328 | 0 | zend_ast *ast = zend_ast_create_va(kind, 0, &va); |
329 | 0 | va_end(va); |
330 | 0 | return ast; |
331 | 0 | } |
332 | | |
333 | | ZEND_API zend_ast * zend_ast_create_ex_n( |
334 | 8 | zend_ast_kind kind, unsigned attr, ...) { |
335 | 8 | va_list va; |
336 | 8 | va_start(va, attr); |
337 | 8 | zend_ast *ast = zend_ast_create_va(kind, attr, &va); |
338 | 8 | va_end(va); |
339 | 8 | return ast; |
340 | 8 | } |
341 | | |
342 | 412 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) { |
343 | 412 | zend_ast *ast; |
344 | 412 | zend_ast_list *list; |
345 | | |
346 | 412 | ast = zend_ast_alloc(zend_ast_list_size(4)); |
347 | 412 | list = (zend_ast_list *) ast; |
348 | 412 | list->kind = kind; |
349 | 412 | list->attr = 0; |
350 | 412 | list->lineno = CG(zend_lineno); |
351 | 412 | list->children = 0; |
352 | | |
353 | 412 | return ast; |
354 | 412 | } |
355 | | |
356 | 630 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child) { |
357 | 630 | zend_ast *ast; |
358 | 630 | zend_ast_list *list; |
359 | 630 | uint32_t lineno; |
360 | | |
361 | 630 | ast = zend_ast_alloc(zend_ast_list_size(4)); |
362 | 630 | list = (zend_ast_list *) ast; |
363 | 630 | list->kind = kind; |
364 | 630 | list->attr = 0; |
365 | 630 | list->children = 1; |
366 | 630 | list->child[0] = child; |
367 | 630 | if (child) { |
368 | 594 | lineno = zend_ast_get_lineno(child); |
369 | 594 | if (lineno > CG(zend_lineno)) { |
370 | 0 | lineno = CG(zend_lineno); |
371 | 0 | } |
372 | 594 | } else { |
373 | 36 | lineno = CG(zend_lineno); |
374 | 36 | } |
375 | 630 | list->lineno = lineno; |
376 | | |
377 | 630 | return ast; |
378 | 630 | } |
379 | | |
380 | 46 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) { |
381 | 46 | zend_ast *ast; |
382 | 46 | zend_ast_list *list; |
383 | 46 | uint32_t lineno; |
384 | | |
385 | 46 | ast = zend_ast_alloc(zend_ast_list_size(4)); |
386 | 46 | list = (zend_ast_list *) ast; |
387 | 46 | list->kind = kind; |
388 | 46 | list->attr = 0; |
389 | 46 | list->children = 2; |
390 | 46 | list->child[0] = child1; |
391 | 46 | list->child[1] = child2; |
392 | 46 | if (child1) { |
393 | 46 | lineno = zend_ast_get_lineno(child1); |
394 | 46 | if (lineno > CG(zend_lineno)) { |
395 | 0 | lineno = CG(zend_lineno); |
396 | 0 | } |
397 | 46 | } else if (child2) { |
398 | 0 | lineno = zend_ast_get_lineno(child2); |
399 | 0 | if (lineno > CG(zend_lineno)) { |
400 | 0 | lineno = CG(zend_lineno); |
401 | 0 | } |
402 | 0 | } else { |
403 | 0 | list->children = 0; |
404 | 0 | lineno = CG(zend_lineno); |
405 | 0 | } |
406 | 46 | list->lineno = lineno; |
407 | | |
408 | 46 | return ast; |
409 | 46 | } |
410 | | |
411 | 65 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_0(zend_ast_kind kind) { |
412 | 65 | return zend_ast_create_list(0, kind); |
413 | 65 | } |
414 | | |
415 | 244 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_1(zend_ast_kind kind, zend_ast *arg) { |
416 | 244 | zend_ast *list = zend_ast_create_list(1, kind, arg); |
417 | | |
418 | 244 | if (zend_ast_is_placeholder_arg(arg)) { |
419 | 0 | return zend_ast_create_fcc(list); |
420 | 0 | } |
421 | | |
422 | 244 | return list; |
423 | 244 | } |
424 | | |
425 | 0 | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_arg_list_2(zend_ast_kind kind, zend_ast *arg1, zend_ast *arg2) { |
426 | 0 | zend_ast *list = zend_ast_create_list(2, kind, arg1, arg2); |
427 | |
|
428 | 0 | if (zend_ast_is_placeholder_arg(arg1) || zend_ast_is_placeholder_arg(arg2)) { |
429 | 0 | return zend_ast_create_fcc(list); |
430 | 0 | } |
431 | | |
432 | 0 | return list; |
433 | 0 | } |
434 | | #else |
435 | | static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) { |
436 | | uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT; |
437 | | zend_ast *ast; |
438 | | |
439 | | ast = zend_ast_alloc(zend_ast_size(children)); |
440 | | ast->kind = kind; |
441 | | ast->attr = attr; |
442 | | ast->lineno = (uint32_t) -1; |
443 | | |
444 | | for (i = 0; i < children; ++i) { |
445 | | ast->child[i] = va_arg(va, zend_ast *); |
446 | | if (ast->child[i] != NULL) { |
447 | | uint32_t lineno = zend_ast_get_lineno(ast->child[i]); |
448 | | if (lineno < ast->lineno) { |
449 | | ast->lineno = lineno; |
450 | | } |
451 | | } |
452 | | } |
453 | | |
454 | | if (ast->lineno == UINT_MAX) { |
455 | | ast->lineno = CG(zend_lineno); |
456 | | } |
457 | | |
458 | | return ast; |
459 | | } |
460 | | |
461 | | ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) { |
462 | | va_list va; |
463 | | zend_ast *ast; |
464 | | |
465 | | va_start(va, attr); |
466 | | ast = zend_ast_create_from_va_list(kind, attr, va); |
467 | | va_end(va); |
468 | | |
469 | | return ast; |
470 | | } |
471 | | |
472 | | ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) { |
473 | | va_list va; |
474 | | zend_ast *ast; |
475 | | |
476 | | va_start(va, kind); |
477 | | ast = zend_ast_create_from_va_list(kind, 0, va); |
478 | | va_end(va); |
479 | | |
480 | | return ast; |
481 | | } |
482 | | |
483 | | ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) { |
484 | | zend_ast *ast; |
485 | | zend_ast_list *list; |
486 | | |
487 | | ast = zend_ast_alloc(zend_ast_list_size(4)); |
488 | | list = (zend_ast_list *) ast; |
489 | | list->kind = kind; |
490 | | list->attr = 0; |
491 | | list->lineno = CG(zend_lineno); |
492 | | list->children = 0; |
493 | | |
494 | | { |
495 | | va_list va; |
496 | | uint32_t i; |
497 | | va_start(va, kind); |
498 | | for (i = 0; i < init_children; ++i) { |
499 | | zend_ast *child = va_arg(va, zend_ast *); |
500 | | ast = zend_ast_list_add(ast, child); |
501 | | if (child != NULL) { |
502 | | uint32_t lineno = zend_ast_get_lineno(child); |
503 | | if (lineno < ast->lineno) { |
504 | | ast->lineno = lineno; |
505 | | } |
506 | | } |
507 | | } |
508 | | va_end(va); |
509 | | } |
510 | | |
511 | | return ast; |
512 | | } |
513 | | |
514 | | ZEND_API zend_ast *zend_ast_create_arg_list(uint32_t init_children, zend_ast_kind kind, ...) { |
515 | | zend_ast *ast; |
516 | | zend_ast_list *list; |
517 | | bool has_placeholders = false; |
518 | | |
519 | | ast = zend_ast_alloc(zend_ast_list_size(4)); |
520 | | list = (zend_ast_list *) ast; |
521 | | list->kind = kind; |
522 | | list->attr = 0; |
523 | | list->lineno = CG(zend_lineno); |
524 | | list->children = 0; |
525 | | |
526 | | { |
527 | | va_list va; |
528 | | uint32_t i; |
529 | | va_start(va, kind); |
530 | | for (i = 0; i < init_children; ++i) { |
531 | | zend_ast *child = va_arg(va, zend_ast *); |
532 | | ast = zend_ast_list_add(ast, child); |
533 | | uint32_t lineno = zend_ast_get_lineno(child); |
534 | | if (lineno < ast->lineno) { |
535 | | ast->lineno = lineno; |
536 | | } |
537 | | has_placeholders = has_placeholders || zend_ast_is_placeholder_arg(child); |
538 | | } |
539 | | va_end(va); |
540 | | } |
541 | | |
542 | | if (has_placeholders) { |
543 | | return zend_ast_create_fcc(list); |
544 | | } |
545 | | |
546 | | return ast; |
547 | | } |
548 | | #endif |
549 | | |
550 | 126 | zend_ast *zend_ast_create_concat_op(zend_ast *op0, zend_ast *op1) { |
551 | 126 | if (op0->kind == ZEND_AST_ZVAL && op1->kind == ZEND_AST_ZVAL) { |
552 | 28 | zval *zv0 = zend_ast_get_zval(op0); |
553 | 28 | zval *zv1 = zend_ast_get_zval(op1); |
554 | 28 | if (!zend_binary_op_produces_error(ZEND_CONCAT, zv0, zv1) && |
555 | 28 | concat_function(zv0, zv0, zv1) == SUCCESS) { |
556 | 28 | zval_ptr_dtor_nogc(zv1); |
557 | 28 | return zend_ast_create_zval(zv0); |
558 | 28 | } |
559 | 28 | } |
560 | 98 | return zend_ast_create_binary_op(ZEND_CONCAT, op0, op1); |
561 | 126 | } |
562 | | |
563 | 2.53k | static inline bool is_power_of_two(uint32_t n) { |
564 | 2.53k | return ((n != 0) && (n == (n & (~n + 1)))); |
565 | 2.53k | } |
566 | | |
567 | 3.23k | ZEND_ATTRIBUTE_NODISCARD ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) { |
568 | 3.23k | zend_ast_list *list = zend_ast_get_list(ast); |
569 | 3.23k | if (list->children >= 4 && is_power_of_two(list->children)) { |
570 | 113 | list = zend_ast_realloc(list, |
571 | 113 | zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2)); |
572 | 113 | } |
573 | 3.23k | list->child[list->children++] = op; |
574 | 3.23k | return (zend_ast *) list; |
575 | 3.23k | } |
576 | | |
577 | | ZEND_API zend_ast * ZEND_FASTCALL zend_ast_arg_list_add(zend_ast *list, zend_ast *arg) |
578 | 92 | { |
579 | 92 | if (list->kind == ZEND_AST_CALLABLE_CONVERT) { |
580 | 0 | zend_ast_fcc *fcc_ast = (zend_ast_fcc*)list; |
581 | 0 | fcc_ast->args = zend_ast_list_add(fcc_ast->args, arg); |
582 | 0 | return (zend_ast*)fcc_ast; |
583 | 0 | } |
584 | | |
585 | 92 | ZEND_ASSERT(list->kind == ZEND_AST_ARG_LIST); |
586 | | |
587 | 92 | if (zend_ast_is_placeholder_arg(arg)) { |
588 | 0 | return zend_ast_create_fcc(zend_ast_list_add(list, arg)); |
589 | 0 | } |
590 | | |
591 | 92 | return zend_ast_list_add(list, arg); |
592 | 92 | } |
593 | | |
594 | | static zend_result zend_ast_add_array_element(const zval *result, zval *offset, zval *expr) |
595 | 0 | { |
596 | 0 | if (Z_TYPE_P(offset) == IS_UNDEF) { |
597 | 0 | if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) { |
598 | 0 | zend_throw_error(NULL, |
599 | 0 | "Cannot add element to the array as the next element is already occupied"); |
600 | 0 | return FAILURE; |
601 | 0 | } |
602 | 0 | return SUCCESS; |
603 | 0 | } |
604 | | |
605 | 0 | if (array_set_zval_key(Z_ARRVAL_P(result), offset, expr) == FAILURE) { |
606 | 0 | return FAILURE; |
607 | 0 | } |
608 | | |
609 | 0 | zval_ptr_dtor_nogc(offset); |
610 | 0 | zval_ptr_dtor_nogc(expr); |
611 | 0 | return SUCCESS; |
612 | 0 | } |
613 | | |
614 | 0 | static zend_result zend_ast_add_unpacked_element(const zval *result, const zval *expr) { |
615 | 0 | if (EXPECTED(Z_TYPE_P(expr) == IS_ARRAY)) { |
616 | 0 | const HashTable *ht = Z_ARRVAL_P(expr); |
617 | 0 | zval *val; |
618 | 0 | zend_string *key; |
619 | |
|
620 | 0 | ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) { |
621 | 0 | if (key) { |
622 | 0 | zend_hash_update(Z_ARRVAL_P(result), key, val); |
623 | 0 | } else { |
624 | 0 | if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), val)) { |
625 | 0 | zend_throw_error(NULL, |
626 | 0 | "Cannot add element to the array as the next element is already occupied"); |
627 | 0 | return FAILURE; |
628 | 0 | } |
629 | 0 | } |
630 | 0 | Z_TRY_ADDREF_P(val); |
631 | 0 | } ZEND_HASH_FOREACH_END(); |
632 | 0 | return SUCCESS; |
633 | 0 | } |
634 | | |
635 | 0 | zend_throw_error(NULL, "Only arrays can be unpacked in constant expression"); |
636 | 0 | return FAILURE; |
637 | 0 | } |
638 | | |
639 | | static zend_class_entry *zend_ast_fetch_class(zend_ast *ast, zend_class_entry *scope) |
640 | 0 | { |
641 | 0 | return zend_fetch_class_with_scope(zend_ast_get_str(ast), (ast->attr >> ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT) | ZEND_FETCH_CLASS_EXCEPTION, scope); |
642 | 0 | } |
643 | | |
644 | | static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( |
645 | | zval *result, |
646 | | zend_ast *ast, |
647 | | zend_class_entry *scope, |
648 | | bool *short_circuited_ptr, |
649 | | zend_ast_evaluate_ctx *ctx |
650 | | ); |
651 | | |
652 | | ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_ex( |
653 | | zval *result, |
654 | | zend_ast *ast, |
655 | | zend_class_entry *scope, |
656 | | bool *short_circuited_ptr, |
657 | | zend_ast_evaluate_ctx *ctx |
658 | 5 | ) { |
659 | 5 | zend_string *previous_filename; |
660 | 5 | zend_long previous_lineno; |
661 | 5 | if (scope) { |
662 | 5 | previous_filename = EG(filename_override); |
663 | 5 | previous_lineno = EG(lineno_override); |
664 | 5 | EG(filename_override) = scope->info.user.filename; |
665 | 5 | EG(lineno_override) = zend_ast_get_lineno(ast); |
666 | 5 | } |
667 | 5 | zend_result r = zend_ast_evaluate_inner(result, ast, scope, short_circuited_ptr, ctx); |
668 | 5 | if (scope) { |
669 | 5 | EG(filename_override) = previous_filename; |
670 | 5 | EG(lineno_override) = previous_lineno; |
671 | 5 | } |
672 | 5 | return r; |
673 | 5 | } |
674 | | |
675 | | static zend_result ZEND_FASTCALL zend_ast_evaluate_inner( |
676 | | zval *result, |
677 | | zend_ast *ast, |
678 | | zend_class_entry *scope, |
679 | | bool *short_circuited_ptr, |
680 | | zend_ast_evaluate_ctx *ctx |
681 | 5 | ) { |
682 | 5 | zval op1, op2; |
683 | 5 | zend_result ret = SUCCESS; |
684 | 5 | bool short_circuited; |
685 | 5 | *short_circuited_ptr = false; |
686 | | |
687 | 5 | switch (ast->kind) { |
688 | 0 | case ZEND_AST_BINARY_OP: |
689 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
690 | 0 | ret = FAILURE; |
691 | 0 | } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
692 | 0 | zval_ptr_dtor_nogc(&op1); |
693 | 0 | ret = FAILURE; |
694 | 0 | } else { |
695 | 0 | binary_op_type op = get_binary_op(ast->attr); |
696 | 0 | op(result, &op1, &op2); |
697 | 0 | zval_ptr_dtor_nogc(&op1); |
698 | 0 | zval_ptr_dtor_nogc(&op2); |
699 | 0 | ret = EG(exception) ? FAILURE : SUCCESS; |
700 | 0 | } |
701 | 0 | break; |
702 | 0 | case ZEND_AST_GREATER: |
703 | 0 | case ZEND_AST_GREATER_EQUAL: |
704 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
705 | 0 | ret = FAILURE; |
706 | 0 | } else if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
707 | 0 | zval_ptr_dtor_nogc(&op1); |
708 | 0 | ret = FAILURE; |
709 | 0 | } else { |
710 | | /* op1 > op2 is the same as op2 < op1 */ |
711 | 0 | binary_op_type op = ast->kind == ZEND_AST_GREATER |
712 | 0 | ? is_smaller_function : is_smaller_or_equal_function; |
713 | 0 | op(result, &op2, &op1); |
714 | 0 | zval_ptr_dtor_nogc(&op1); |
715 | 0 | zval_ptr_dtor_nogc(&op2); |
716 | 0 | ret = EG(exception) ? FAILURE : SUCCESS; |
717 | 0 | } |
718 | 0 | break; |
719 | 0 | case ZEND_AST_UNARY_OP: |
720 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
721 | 0 | ret = FAILURE; |
722 | 0 | } else { |
723 | 0 | unary_op_type op = get_unary_op(ast->attr); |
724 | 0 | ret = op(result, &op1); |
725 | 0 | zval_ptr_dtor_nogc(&op1); |
726 | 0 | } |
727 | 0 | break; |
728 | 2 | case ZEND_AST_ZVAL: |
729 | 2 | { |
730 | 2 | zval *zv = zend_ast_get_zval(ast); |
731 | | |
732 | 2 | ZVAL_COPY(result, zv); |
733 | 2 | break; |
734 | 0 | } |
735 | 0 | case ZEND_AST_CONSTANT: |
736 | 0 | { |
737 | 0 | zend_string *name = zend_ast_get_constant_name(ast); |
738 | 0 | zval *zv = zend_get_constant_ex(name, scope, ast->attr); |
739 | |
|
740 | 0 | if (UNEXPECTED(zv == NULL)) { |
741 | 0 | ZVAL_UNDEF(result); |
742 | 0 | return FAILURE; |
743 | 0 | } |
744 | 0 | ZVAL_COPY_OR_DUP(result, zv); |
745 | 0 | break; |
746 | 0 | } |
747 | 0 | case ZEND_AST_CONSTANT_CLASS: |
748 | 0 | if (scope) { |
749 | 0 | ZVAL_STR_COPY(result, scope->name); |
750 | 0 | } else { |
751 | 0 | ZVAL_EMPTY_STRING(result); |
752 | 0 | } |
753 | 0 | break; |
754 | 0 | case ZEND_AST_CLASS_NAME: |
755 | 0 | if (!scope) { |
756 | 0 | zend_throw_error(NULL, "Cannot use \"self\" when no class scope is active"); |
757 | 0 | return FAILURE; |
758 | 0 | } |
759 | 0 | if (ast->attr == ZEND_FETCH_CLASS_SELF) { |
760 | 0 | ZVAL_STR_COPY(result, scope->name); |
761 | 0 | } else if (ast->attr == ZEND_FETCH_CLASS_PARENT) { |
762 | 0 | if (!scope->parent) { |
763 | 0 | zend_throw_error(NULL, |
764 | 0 | "Cannot use \"parent\" when current class scope has no parent"); |
765 | 0 | return FAILURE; |
766 | 0 | } |
767 | 0 | ZVAL_STR_COPY(result, scope->parent->name); |
768 | 0 | } else { |
769 | 0 | ZEND_ASSERT(0 && "Should have errored during compilation"); |
770 | 0 | } |
771 | 0 | break; |
772 | 0 | case ZEND_AST_AND: |
773 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
774 | 0 | ret = FAILURE; |
775 | 0 | break; |
776 | 0 | } |
777 | 0 | if (zend_is_true(&op1)) { |
778 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
779 | 0 | zval_ptr_dtor_nogc(&op1); |
780 | 0 | ret = FAILURE; |
781 | 0 | break; |
782 | 0 | } |
783 | 0 | ZVAL_BOOL(result, zend_is_true(&op2)); |
784 | 0 | zval_ptr_dtor_nogc(&op2); |
785 | 0 | } else { |
786 | 0 | ZVAL_FALSE(result); |
787 | 0 | } |
788 | 0 | zval_ptr_dtor_nogc(&op1); |
789 | 0 | break; |
790 | 0 | case ZEND_AST_CAST: |
791 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
792 | 0 | ret = FAILURE; |
793 | 0 | break; |
794 | 0 | } |
795 | 0 | if (ast->attr == Z_TYPE(op1)) { |
796 | 0 | ZVAL_COPY_VALUE(result, &op1); |
797 | 0 | } else { |
798 | 0 | switch (ast->attr) { |
799 | 0 | case _IS_BOOL: |
800 | 0 | ZVAL_BOOL(result, zend_is_true(&op1)); |
801 | 0 | break; |
802 | 0 | case IS_LONG: |
803 | 0 | ZVAL_LONG(result, zval_get_long_func(&op1, false)); |
804 | 0 | break; |
805 | 0 | case IS_DOUBLE: |
806 | 0 | ZVAL_DOUBLE(result, zval_get_double_func(&op1)); |
807 | 0 | break; |
808 | 0 | case IS_STRING: |
809 | 0 | ZVAL_STR(result, zval_get_string_func(&op1)); |
810 | 0 | break; |
811 | 0 | case IS_ARRAY: |
812 | 0 | zend_cast_zval_to_array(result, &op1, IS_VAR); |
813 | 0 | break; |
814 | 0 | case IS_OBJECT: |
815 | 0 | zend_cast_zval_to_object(result, &op1, IS_VAR); |
816 | 0 | break; |
817 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
818 | 0 | } |
819 | 0 | zval_ptr_dtor_nogc(&op1); |
820 | 0 | if (UNEXPECTED(EG(exception))) { |
821 | 0 | ret = FAILURE; |
822 | 0 | } |
823 | 0 | } |
824 | 0 | break; |
825 | 0 | case ZEND_AST_OR: |
826 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
827 | 0 | ret = FAILURE; |
828 | 0 | break; |
829 | 0 | } |
830 | 0 | if (zend_is_true(&op1)) { |
831 | 0 | ZVAL_TRUE(result); |
832 | 0 | } else { |
833 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
834 | 0 | zval_ptr_dtor_nogc(&op1); |
835 | 0 | ret = FAILURE; |
836 | 0 | break; |
837 | 0 | } |
838 | 0 | ZVAL_BOOL(result, zend_is_true(&op2)); |
839 | 0 | zval_ptr_dtor_nogc(&op2); |
840 | 0 | } |
841 | 0 | zval_ptr_dtor_nogc(&op1); |
842 | 0 | break; |
843 | 0 | case ZEND_AST_CONDITIONAL: |
844 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
845 | 0 | ret = FAILURE; |
846 | 0 | break; |
847 | 0 | } |
848 | 0 | if (zend_is_true(&op1)) { |
849 | 0 | if (!ast->child[1]) { |
850 | 0 | *result = op1; |
851 | 0 | } else { |
852 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
853 | 0 | zval_ptr_dtor_nogc(&op1); |
854 | 0 | ret = FAILURE; |
855 | 0 | break; |
856 | 0 | } |
857 | 0 | zval_ptr_dtor_nogc(&op1); |
858 | 0 | } |
859 | 0 | } else { |
860 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[2], scope, &short_circuited, ctx) != SUCCESS)) { |
861 | 0 | zval_ptr_dtor_nogc(&op1); |
862 | 0 | ret = FAILURE; |
863 | 0 | break; |
864 | 0 | } |
865 | 0 | zval_ptr_dtor_nogc(&op1); |
866 | 0 | } |
867 | 0 | break; |
868 | 0 | case ZEND_AST_COALESCE: |
869 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
870 | 0 | ret = FAILURE; |
871 | 0 | break; |
872 | 0 | } |
873 | 0 | if (Z_TYPE(op1) > IS_NULL) { |
874 | 0 | *result = op1; |
875 | 0 | } else { |
876 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(result, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
877 | 0 | zval_ptr_dtor_nogc(&op1); |
878 | 0 | ret = FAILURE; |
879 | 0 | break; |
880 | 0 | } |
881 | 0 | zval_ptr_dtor_nogc(&op1); |
882 | 0 | } |
883 | 0 | break; |
884 | 0 | case ZEND_AST_UNARY_PLUS: |
885 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
886 | 0 | ret = FAILURE; |
887 | 0 | } else { |
888 | 0 | ZVAL_LONG(&op1, 0); |
889 | 0 | ret = add_function(result, &op1, &op2); |
890 | 0 | zval_ptr_dtor_nogc(&op2); |
891 | 0 | } |
892 | 0 | break; |
893 | 0 | case ZEND_AST_UNARY_MINUS: |
894 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
895 | 0 | ret = FAILURE; |
896 | 0 | } else { |
897 | 0 | ZVAL_LONG(&op1, -1); |
898 | 0 | ret = mul_function(result, &op1, &op2); |
899 | 0 | zval_ptr_dtor_nogc(&op2); |
900 | 0 | } |
901 | 0 | break; |
902 | 0 | case ZEND_AST_ARRAY: |
903 | 0 | { |
904 | 0 | uint32_t i; |
905 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
906 | |
|
907 | 0 | if (!list->children) { |
908 | 0 | ZVAL_EMPTY_ARRAY(result); |
909 | 0 | break; |
910 | 0 | } |
911 | 0 | array_init(result); |
912 | 0 | for (i = 0; i < list->children; i++) { |
913 | 0 | zend_ast *elem = list->child[i]; |
914 | 0 | if (elem->kind == ZEND_AST_UNPACK) { |
915 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
916 | 0 | zval_ptr_dtor_nogc(result); |
917 | 0 | return FAILURE; |
918 | 0 | } |
919 | 0 | if (UNEXPECTED(zend_ast_add_unpacked_element(result, &op1) != SUCCESS)) { |
920 | 0 | zval_ptr_dtor_nogc(&op1); |
921 | 0 | zval_ptr_dtor_nogc(result); |
922 | 0 | return FAILURE; |
923 | 0 | } |
924 | 0 | zval_ptr_dtor_nogc(&op1); |
925 | 0 | continue; |
926 | 0 | } |
927 | 0 | if (elem->child[1]) { |
928 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, elem->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
929 | 0 | zval_ptr_dtor_nogc(result); |
930 | 0 | return FAILURE; |
931 | 0 | } |
932 | 0 | } else { |
933 | 0 | ZVAL_UNDEF(&op1); |
934 | 0 | } |
935 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, elem->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
936 | 0 | zval_ptr_dtor_nogc(&op1); |
937 | 0 | zval_ptr_dtor_nogc(result); |
938 | 0 | return FAILURE; |
939 | 0 | } |
940 | 0 | if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) { |
941 | 0 | zval_ptr_dtor_nogc(&op1); |
942 | 0 | zval_ptr_dtor_nogc(&op2); |
943 | 0 | zval_ptr_dtor_nogc(result); |
944 | 0 | return FAILURE; |
945 | 0 | } |
946 | 0 | } |
947 | 0 | } |
948 | 0 | break; |
949 | 0 | case ZEND_AST_DIM: |
950 | 0 | if (ast->child[1] == NULL) { |
951 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading"); |
952 | 0 | } |
953 | | |
954 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
955 | 0 | ret = FAILURE; |
956 | 0 | break; |
957 | 0 | } |
958 | 0 | if (short_circuited) { |
959 | 0 | *short_circuited_ptr = true; |
960 | 0 | ZVAL_NULL(result); |
961 | 0 | return SUCCESS; |
962 | 0 | } |
963 | | |
964 | | // DIM on objects is disallowed because it allows executing arbitrary expressions |
965 | 0 | if (Z_TYPE(op1) == IS_OBJECT) { |
966 | 0 | zval_ptr_dtor_nogc(&op1); |
967 | 0 | zend_throw_error(NULL, "Cannot use [] on objects in constant expression"); |
968 | 0 | ret = FAILURE; |
969 | 0 | break; |
970 | 0 | } |
971 | | |
972 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
973 | 0 | zval_ptr_dtor_nogc(&op1); |
974 | 0 | ret = FAILURE; |
975 | 0 | break; |
976 | 0 | } |
977 | | |
978 | 0 | zend_fetch_dimension_const(result, &op1, &op2, (ast->attr & ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R); |
979 | |
|
980 | 0 | zval_ptr_dtor_nogc(&op1); |
981 | 0 | zval_ptr_dtor_nogc(&op2); |
982 | 0 | if (UNEXPECTED(EG(exception))) { |
983 | 0 | return FAILURE; |
984 | 0 | } |
985 | | |
986 | 0 | break; |
987 | 3 | case ZEND_AST_CONST_ENUM_INIT: |
988 | 3 | { |
989 | | // Preloading will attempt to resolve constants but objects can't be stored in shm |
990 | | // Aborting here to store the const AST instead |
991 | 3 | if (CG(in_compilation)) { |
992 | 0 | return FAILURE; |
993 | 0 | } |
994 | | |
995 | 3 | zend_ast *class_name_ast = ast->child[0]; |
996 | 3 | zend_string *class_name = zend_ast_get_str(class_name_ast); |
997 | | |
998 | 3 | zend_ast *case_name_ast = ast->child[1]; |
999 | 3 | zend_string *case_name = zend_ast_get_str(case_name_ast); |
1000 | | |
1001 | 3 | zend_ast *case_value_ast = ast->child[2]; |
1002 | | |
1003 | 3 | zval case_value_zv; |
1004 | 3 | ZVAL_UNDEF(&case_value_zv); |
1005 | 3 | if (case_value_ast != NULL) { |
1006 | 2 | if (UNEXPECTED(zend_ast_evaluate_ex(&case_value_zv, case_value_ast, scope, &short_circuited, ctx) != SUCCESS)) { |
1007 | 0 | return FAILURE; |
1008 | 0 | } |
1009 | 2 | } |
1010 | | |
1011 | 3 | zend_class_entry *ce = zend_lookup_class(class_name); |
1012 | 3 | zend_enum_new(result, ce, case_name, case_value_ast != NULL ? &case_value_zv : NULL); |
1013 | 3 | zval_ptr_dtor_nogc(&case_value_zv); |
1014 | 3 | break; |
1015 | 3 | } |
1016 | 0 | case ZEND_AST_CLASS_CONST: |
1017 | 0 | { |
1018 | 0 | zend_string *class_name = zend_ast_get_str(ast->child[0]); |
1019 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
1020 | 0 | return FAILURE; |
1021 | 0 | } |
1022 | 0 | if (UNEXPECTED(Z_TYPE(op2) != IS_STRING)) { |
1023 | 0 | zend_invalid_class_constant_type_error(Z_TYPE(op2)); |
1024 | 0 | zval_ptr_dtor_nogc(&op2); |
1025 | 0 | return FAILURE; |
1026 | 0 | } |
1027 | 0 | zend_string *const_name = Z_STR(op2); |
1028 | |
|
1029 | 0 | zend_string *previous_filename; |
1030 | 0 | zend_long previous_lineno; |
1031 | 0 | if (scope) { |
1032 | 0 | previous_filename = EG(filename_override); |
1033 | 0 | previous_lineno = EG(lineno_override); |
1034 | 0 | EG(filename_override) = scope->info.user.filename; |
1035 | 0 | EG(lineno_override) = zend_ast_get_lineno(ast); |
1036 | 0 | } |
1037 | 0 | zval *zv = zend_get_class_constant_ex(class_name, const_name, scope, ast->attr); |
1038 | 0 | if (scope) { |
1039 | 0 | EG(filename_override) = previous_filename; |
1040 | 0 | EG(lineno_override) = previous_lineno; |
1041 | 0 | } |
1042 | |
|
1043 | 0 | if (UNEXPECTED(zv == NULL)) { |
1044 | 0 | ZVAL_UNDEF(result); |
1045 | 0 | zval_ptr_dtor_nogc(&op2); |
1046 | 0 | return FAILURE; |
1047 | 0 | } |
1048 | 0 | ZVAL_COPY_OR_DUP(result, zv); |
1049 | 0 | zval_ptr_dtor_nogc(&op2); |
1050 | 0 | break; |
1051 | 0 | } |
1052 | 0 | case ZEND_AST_NEW: |
1053 | 0 | { |
1054 | 0 | zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope); |
1055 | 0 | if (!ce) { |
1056 | 0 | return FAILURE; |
1057 | 0 | } |
1058 | | |
1059 | 0 | if (object_init_ex(result, ce) != SUCCESS) { |
1060 | 0 | return FAILURE; |
1061 | 0 | } |
1062 | | |
1063 | | /* Even if there is no constructor, the object can have cause side-effects in various ways (__toString(), __get(), __isset(), etc). */ |
1064 | 0 | ctx->had_side_effects = true; |
1065 | |
|
1066 | 0 | zend_ast_list *args_ast = zend_ast_get_list(ast->child[1]); |
1067 | 0 | if (args_ast->attr) { |
1068 | | /* Has named arguments. */ |
1069 | 0 | HashTable *args = zend_new_array(args_ast->children); |
1070 | 0 | for (uint32_t i = 0; i < args_ast->children; i++) { |
1071 | 0 | zend_ast *arg_ast = args_ast->child[i]; |
1072 | 0 | zend_string *name = NULL; |
1073 | 0 | zval arg; |
1074 | 0 | if (arg_ast->kind == ZEND_AST_NAMED_ARG) { |
1075 | 0 | name = zend_ast_get_str(arg_ast->child[0]); |
1076 | 0 | arg_ast = arg_ast->child[1]; |
1077 | 0 | } |
1078 | 0 | if (zend_ast_evaluate_ex(&arg, arg_ast, scope, &short_circuited, ctx) == FAILURE) { |
1079 | 0 | zend_array_destroy(args); |
1080 | 0 | zval_ptr_dtor(result); |
1081 | 0 | return FAILURE; |
1082 | 0 | } |
1083 | 0 | if (name) { |
1084 | 0 | if (!zend_hash_add(args, name, &arg)) { |
1085 | 0 | zend_throw_error(NULL, |
1086 | 0 | "Named parameter $%s overwrites previous argument", |
1087 | 0 | ZSTR_VAL(name)); |
1088 | 0 | zend_array_destroy(args); |
1089 | 0 | zval_ptr_dtor(result); |
1090 | 0 | return FAILURE; |
1091 | 0 | } |
1092 | 0 | } else { |
1093 | 0 | zend_hash_next_index_insert(args, &arg); |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | 0 | zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result)); |
1098 | 0 | if (ctor) { |
1099 | 0 | zend_call_known_function( |
1100 | 0 | ctor, Z_OBJ_P(result), Z_OBJCE_P(result), NULL, 0, NULL, args); |
1101 | 0 | } |
1102 | |
|
1103 | 0 | zend_array_destroy(args); |
1104 | 0 | } else { |
1105 | 0 | ALLOCA_FLAG(use_heap) |
1106 | 0 | zval *args = do_alloca(sizeof(zval) * args_ast->children, use_heap); |
1107 | 0 | for (uint32_t i = 0; i < args_ast->children; i++) { |
1108 | 0 | if (zend_ast_evaluate_ex(&args[i], args_ast->child[i], scope, &short_circuited, ctx) == FAILURE) { |
1109 | 0 | for (uint32_t j = 0; j < i; j++) { |
1110 | 0 | zval_ptr_dtor(&args[j]); |
1111 | 0 | } |
1112 | 0 | free_alloca(args, use_heap); |
1113 | 0 | zval_ptr_dtor(result); |
1114 | 0 | return FAILURE; |
1115 | 0 | } |
1116 | 0 | } |
1117 | | |
1118 | 0 | zend_function *ctor = Z_OBJ_HT_P(result)->get_constructor(Z_OBJ_P(result)); |
1119 | 0 | if (ctor) { |
1120 | 0 | zend_call_known_instance_method( |
1121 | 0 | ctor, Z_OBJ_P(result), NULL, args_ast->children, args); |
1122 | 0 | } |
1123 | |
|
1124 | 0 | for (uint32_t i = 0; i < args_ast->children; i++) { |
1125 | 0 | zval_ptr_dtor(&args[i]); |
1126 | 0 | } |
1127 | 0 | free_alloca(args, use_heap); |
1128 | 0 | } |
1129 | | |
1130 | 0 | if (EG(exception)) { |
1131 | 0 | zend_object_store_ctor_failed(Z_OBJ_P(result)); |
1132 | 0 | zval_ptr_dtor(result); |
1133 | 0 | return FAILURE; |
1134 | 0 | } |
1135 | 0 | return SUCCESS; |
1136 | 0 | } |
1137 | 0 | case ZEND_AST_CALL: |
1138 | 0 | case ZEND_AST_STATIC_CALL: |
1139 | 0 | { |
1140 | 0 | zend_function *fptr; |
1141 | 0 | zend_class_entry *called_scope = NULL; |
1142 | |
|
1143 | 0 | zend_ast *args_ast = zend_ast_call_get_args(ast); |
1144 | 0 | ZEND_ASSERT(args_ast->kind == ZEND_AST_CALLABLE_CONVERT); |
1145 | |
|
1146 | 0 | zend_ast_fcc *fcc_ast = (zend_ast_fcc*)args_ast; |
1147 | |
|
1148 | 0 | zend_ast_list *args = zend_ast_get_list(fcc_ast->args); |
1149 | 0 | ZEND_ASSERT(args->children > 0); |
1150 | 0 | if (args->children != 1 || args->child[0]->attr != ZEND_PLACEHOLDER_VARIADIC) { |
1151 | | /* TODO: PFAs */ |
1152 | 0 | zend_error_noreturn(E_COMPILE_ERROR, "Constant expression contains invalid operations"); |
1153 | 0 | return FAILURE; |
1154 | 0 | } |
1155 | | |
1156 | 0 | switch (ast->kind) { |
1157 | 0 | case ZEND_AST_CALL: { |
1158 | 0 | fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr); |
1159 | |
|
1160 | 0 | if (!fptr) { |
1161 | 0 | zend_string *function_name = zend_ast_get_str(ast->child[0]); |
1162 | 0 | zend_string *function_name_lc = zend_string_tolower(function_name); |
1163 | 0 | fptr = zend_fetch_function(function_name_lc); |
1164 | 0 | if (!fptr && ast->child[0]->attr != ZEND_NAME_FQ) { |
1165 | 0 | const char *backslash = zend_memrchr(ZSTR_VAL(function_name_lc), '\\', ZSTR_LEN(function_name_lc)); |
1166 | 0 | if (backslash) { |
1167 | 0 | fptr = zend_fetch_function_str(backslash + 1, ZSTR_LEN(function_name_lc) - (backslash - ZSTR_VAL(function_name_lc) + 1)); |
1168 | 0 | } |
1169 | 0 | } |
1170 | 0 | zend_string_release(function_name_lc); |
1171 | 0 | if (!fptr) { |
1172 | 0 | zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(function_name)); |
1173 | 0 | return FAILURE; |
1174 | 0 | } |
1175 | | |
1176 | 0 | ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr); |
1177 | 0 | } |
1178 | | |
1179 | 0 | break; |
1180 | 0 | } |
1181 | 0 | case ZEND_AST_STATIC_CALL: { |
1182 | 0 | zend_class_entry *ce = zend_ast_fetch_class(ast->child[0], scope); |
1183 | 0 | if (!ce) { |
1184 | 0 | return FAILURE; |
1185 | 0 | } |
1186 | 0 | called_scope = ce; |
1187 | |
|
1188 | 0 | fptr = ZEND_MAP_PTR_GET(fcc_ast->fptr); |
1189 | |
|
1190 | 0 | if (!fptr) { |
1191 | 0 | zend_string *method_name = zend_ast_get_str(ast->child[1]); |
1192 | 0 | if (ce->get_static_method) { |
1193 | 0 | fptr = ce->get_static_method(ce, method_name); |
1194 | 0 | } else { |
1195 | 0 | fptr = zend_hash_find_ptr_lc(&ce->function_table, method_name); |
1196 | 0 | if (fptr) { |
1197 | 0 | if (!zend_check_method_accessible(fptr, scope)) { |
1198 | 0 | if (ce->__callstatic) { |
1199 | 0 | zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions"); |
1200 | 0 | } else { |
1201 | 0 | zend_bad_method_call(fptr, method_name, scope); |
1202 | 0 | } |
1203 | |
|
1204 | 0 | return FAILURE; |
1205 | 0 | } |
1206 | 0 | } else { |
1207 | 0 | if (ce->__callstatic) { |
1208 | 0 | zend_throw_error(NULL, "Creating a callable for the magic __callStatic() method is not supported in constant expressions"); |
1209 | 0 | } else { |
1210 | 0 | zend_undefined_method(ce, method_name); |
1211 | 0 | } |
1212 | |
|
1213 | 0 | return FAILURE; |
1214 | 0 | } |
1215 | 0 | } |
1216 | | |
1217 | 0 | if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) { |
1218 | 0 | zend_non_static_method_call(fptr); |
1219 | |
|
1220 | 0 | return FAILURE; |
1221 | 0 | } |
1222 | 0 | if ((fptr->common.fn_flags & ZEND_ACC_ABSTRACT)) { |
1223 | 0 | zend_abstract_method_call(fptr); |
1224 | |
|
1225 | 0 | return FAILURE; |
1226 | 0 | } else if (fptr->common.scope->ce_flags & ZEND_ACC_TRAIT) { |
1227 | 0 | zend_error(E_DEPRECATED, |
1228 | 0 | "Calling static trait method %s::%s is deprecated, " |
1229 | 0 | "it should only be called on a class using the trait", |
1230 | 0 | ZSTR_VAL(fptr->common.scope->name), ZSTR_VAL(fptr->common.function_name)); |
1231 | 0 | if (EG(exception)) { |
1232 | 0 | return FAILURE; |
1233 | 0 | } |
1234 | 0 | } |
1235 | | |
1236 | 0 | ZEND_MAP_PTR_SET(fcc_ast->fptr, fptr); |
1237 | 0 | } |
1238 | | |
1239 | 0 | break; |
1240 | 0 | } |
1241 | 0 | EMPTY_SWITCH_DEFAULT_CASE() |
1242 | 0 | } |
1243 | | |
1244 | 0 | zend_create_fake_closure(result, fptr, fptr->common.scope, called_scope, NULL); |
1245 | |
|
1246 | 0 | return SUCCESS; |
1247 | 0 | } |
1248 | 0 | case ZEND_AST_OP_ARRAY: |
1249 | 0 | { |
1250 | 0 | zend_function *func = (zend_function *)zend_ast_get_op_array(ast)->op_array; |
1251 | |
|
1252 | 0 | zend_create_closure(result, func, scope, scope, NULL); |
1253 | 0 | return SUCCESS; |
1254 | 0 | } |
1255 | 0 | case ZEND_AST_PROP: |
1256 | 0 | case ZEND_AST_NULLSAFE_PROP: |
1257 | 0 | { |
1258 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op1, ast->child[0], scope, &short_circuited, ctx) != SUCCESS)) { |
1259 | 0 | return FAILURE; |
1260 | 0 | } |
1261 | 0 | if (short_circuited) { |
1262 | 0 | *short_circuited_ptr = true; |
1263 | 0 | ZVAL_NULL(result); |
1264 | 0 | return SUCCESS; |
1265 | 0 | } |
1266 | 0 | if (ast->kind == ZEND_AST_NULLSAFE_PROP && Z_TYPE(op1) == IS_NULL) { |
1267 | 0 | *short_circuited_ptr = true; |
1268 | 0 | ZVAL_NULL(result); |
1269 | 0 | return SUCCESS; |
1270 | 0 | } |
1271 | | |
1272 | 0 | if (UNEXPECTED(zend_ast_evaluate_ex(&op2, ast->child[1], scope, &short_circuited, ctx) != SUCCESS)) { |
1273 | 0 | zval_ptr_dtor_nogc(&op1); |
1274 | 0 | return FAILURE; |
1275 | 0 | } |
1276 | | |
1277 | 0 | if (!try_convert_to_string(&op2)) { |
1278 | 0 | zval_ptr_dtor_nogc(&op1); |
1279 | 0 | zval_ptr_dtor_nogc(&op2); |
1280 | 0 | return FAILURE; |
1281 | 0 | } |
1282 | | |
1283 | 0 | if (Z_TYPE(op1) != IS_OBJECT) { |
1284 | 0 | zend_wrong_property_read(&op1, &op2); |
1285 | |
|
1286 | 0 | zval_ptr_dtor_nogc(&op1); |
1287 | 0 | zval_ptr_dtor_nogc(&op2); |
1288 | |
|
1289 | 0 | ZVAL_NULL(result); |
1290 | 0 | return SUCCESS; |
1291 | 0 | } |
1292 | | |
1293 | 0 | zend_object *zobj = Z_OBJ(op1); |
1294 | 0 | if (!(zobj->ce->ce_flags & ZEND_ACC_ENUM)) { |
1295 | 0 | zend_throw_error(NULL, "Fetching properties on non-enums in constant expressions is not allowed"); |
1296 | 0 | zval_ptr_dtor_nogc(&op1); |
1297 | 0 | zval_ptr_dtor_nogc(&op2); |
1298 | 0 | return FAILURE; |
1299 | 0 | } |
1300 | | |
1301 | 0 | zend_string *name = Z_STR(op2); |
1302 | 0 | zval *property_result = zend_read_property_ex(scope, zobj, name, 0, result); |
1303 | 0 | if (EG(exception)) { |
1304 | 0 | zval_ptr_dtor_nogc(&op1); |
1305 | 0 | zval_ptr_dtor_nogc(&op2); |
1306 | 0 | return FAILURE; |
1307 | 0 | } |
1308 | | |
1309 | 0 | if (result != property_result) { |
1310 | 0 | ZVAL_COPY(result, property_result); |
1311 | 0 | } |
1312 | 0 | zval_ptr_dtor_nogc(&op1); |
1313 | 0 | zval_ptr_dtor_nogc(&op2); |
1314 | 0 | return SUCCESS; |
1315 | 0 | } |
1316 | 0 | default: |
1317 | 0 | zend_throw_error(NULL, "Unsupported constant expression"); |
1318 | 0 | ret = FAILURE; |
1319 | 5 | } |
1320 | 5 | return ret; |
1321 | 5 | } |
1322 | | |
1323 | | ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope) |
1324 | 0 | { |
1325 | 0 | zend_ast_evaluate_ctx ctx = {0}; |
1326 | 0 | bool short_circuited; |
1327 | 0 | return zend_ast_evaluate_ex(result, ast, scope, &short_circuited, &ctx); |
1328 | 0 | } |
1329 | | |
1330 | | static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast) |
1331 | 14 | { |
1332 | 14 | size_t size; |
1333 | | |
1334 | 14 | if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) { |
1335 | 10 | size = sizeof(zend_ast_zval); |
1336 | 10 | } else if (ast->kind == ZEND_AST_OP_ARRAY) { |
1337 | 0 | size = sizeof(zend_ast_op_array); |
1338 | 4 | } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
1339 | 0 | zend_ast *args_ast = ((zend_ast_fcc*)ast)->args; |
1340 | 0 | size = sizeof(zend_ast_fcc) + zend_ast_tree_size(args_ast); |
1341 | 4 | } else if (zend_ast_is_list(ast)) { |
1342 | 0 | uint32_t i; |
1343 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
1344 | |
|
1345 | 0 | size = zend_ast_list_size(list->children); |
1346 | 0 | for (i = 0; i < list->children; i++) { |
1347 | 0 | if (list->child[i]) { |
1348 | 0 | size += zend_ast_tree_size(list->child[i]); |
1349 | 0 | } |
1350 | 0 | } |
1351 | 4 | } else if (zend_ast_is_decl(ast)) { |
1352 | | /* Not implemented. */ |
1353 | 0 | ZEND_UNREACHABLE(); |
1354 | 4 | } else { |
1355 | 4 | uint32_t i, children = zend_ast_get_num_children(ast); |
1356 | | |
1357 | 4 | size = zend_ast_size(children); |
1358 | 16 | for (i = 0; i < children; i++) { |
1359 | 12 | if (ast->child[i]) { |
1360 | 10 | size += zend_ast_tree_size(ast->child[i]); |
1361 | 10 | } |
1362 | 12 | } |
1363 | 4 | } |
1364 | 14 | return size; |
1365 | 14 | } |
1366 | | |
1367 | | static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf) |
1368 | 14 | { |
1369 | 14 | if (ast->kind == ZEND_AST_ZVAL) { |
1370 | 10 | zend_ast_zval *new = (zend_ast_zval*)buf; |
1371 | 10 | new->kind = ZEND_AST_ZVAL; |
1372 | 10 | new->attr = ast->attr; |
1373 | 10 | ZVAL_COPY(&new->val, zend_ast_get_zval(ast)); |
1374 | 10 | Z_LINENO(new->val) = zend_ast_get_lineno(ast); |
1375 | 10 | buf = (void*)((char*)buf + sizeof(zend_ast_zval)); |
1376 | 10 | } else if (ast->kind == ZEND_AST_CONSTANT) { |
1377 | 0 | zend_ast_zval *new = (zend_ast_zval*)buf; |
1378 | 0 | new->kind = ZEND_AST_CONSTANT; |
1379 | 0 | new->attr = ast->attr; |
1380 | 0 | ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast)); |
1381 | 0 | Z_LINENO(new->val) = zend_ast_get_lineno(ast); |
1382 | 0 | buf = (void*)((char*)buf + sizeof(zend_ast_zval)); |
1383 | 4 | } else if (zend_ast_is_list(ast)) { |
1384 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
1385 | 0 | zend_ast_list *new = (zend_ast_list*)buf; |
1386 | 0 | uint32_t i; |
1387 | 0 | new->kind = list->kind; |
1388 | 0 | new->attr = list->attr; |
1389 | 0 | new->children = list->children; |
1390 | 0 | new->lineno = list->lineno; |
1391 | 0 | buf = (void*)((char*)buf + zend_ast_list_size(list->children)); |
1392 | 0 | for (i = 0; i < list->children; i++) { |
1393 | 0 | if (list->child[i]) { |
1394 | 0 | new->child[i] = (zend_ast*)buf; |
1395 | 0 | buf = zend_ast_tree_copy(list->child[i], buf); |
1396 | 0 | } else { |
1397 | 0 | new->child[i] = NULL; |
1398 | 0 | } |
1399 | 0 | } |
1400 | 4 | } else if (ast->kind == ZEND_AST_OP_ARRAY) { |
1401 | 0 | const zend_ast_op_array *old = zend_ast_get_op_array(ast); |
1402 | 0 | zend_ast_op_array *new = (zend_ast_op_array*)buf; |
1403 | 0 | new->kind = old->kind; |
1404 | 0 | new->attr = old->attr; |
1405 | 0 | new->lineno = old->lineno; |
1406 | 0 | new->op_array = old->op_array; |
1407 | 0 | function_add_ref((zend_function *)new->op_array); |
1408 | 0 | buf = (void*)((char*)buf + sizeof(zend_ast_op_array)); |
1409 | 4 | } else if (ast->kind == ZEND_AST_CALLABLE_CONVERT) { |
1410 | 0 | const zend_ast_fcc *old = (zend_ast_fcc*)ast; |
1411 | 0 | zend_ast_fcc *new = (zend_ast_fcc*)buf; |
1412 | 0 | new->kind = old->kind; |
1413 | 0 | new->attr = old->attr; |
1414 | 0 | new->lineno = old->lineno; |
1415 | 0 | ZEND_MAP_PTR_INIT(new->fptr, ZEND_MAP_PTR(old->fptr)); |
1416 | 0 | buf = (void*)((char*)buf + sizeof(zend_ast_fcc)); |
1417 | 0 | new->args = buf; |
1418 | 0 | buf = zend_ast_tree_copy(old->args, buf); |
1419 | 4 | } else if (zend_ast_is_decl(ast)) { |
1420 | | /* Not implemented. */ |
1421 | 0 | ZEND_UNREACHABLE(); |
1422 | 4 | } else { |
1423 | 4 | uint32_t i, children = zend_ast_get_num_children(ast); |
1424 | 4 | zend_ast *new = (zend_ast*)buf; |
1425 | 4 | new->kind = ast->kind; |
1426 | 4 | new->attr = ast->attr; |
1427 | 4 | new->lineno = ast->lineno; |
1428 | 4 | buf = (void*)((char*)buf + zend_ast_size(children)); |
1429 | 16 | for (i = 0; i < children; i++) { |
1430 | 12 | if (ast->child[i]) { |
1431 | 10 | new->child[i] = (zend_ast*)buf; |
1432 | 10 | buf = zend_ast_tree_copy(ast->child[i], buf); |
1433 | 10 | } else { |
1434 | 2 | new->child[i] = NULL; |
1435 | 2 | } |
1436 | 12 | } |
1437 | 4 | } |
1438 | 14 | return buf; |
1439 | 14 | } |
1440 | | |
1441 | | ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast) |
1442 | 4 | { |
1443 | 4 | size_t tree_size; |
1444 | 4 | zend_ast_ref *ref; |
1445 | | |
1446 | 4 | ZEND_ASSERT(ast != NULL); |
1447 | 4 | tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref); |
1448 | 4 | ref = emalloc(tree_size); |
1449 | 4 | zend_ast_tree_copy(ast, GC_AST(ref)); |
1450 | 4 | GC_SET_REFCOUNT(ref, 1); |
1451 | 4 | GC_TYPE_INFO(ref) = GC_CONSTANT_AST; |
1452 | 4 | return ref; |
1453 | 4 | } |
1454 | | |
1455 | | ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast) |
1456 | 5.88k | { |
1457 | 11.5k | tail_call: |
1458 | 11.5k | if (!ast) { |
1459 | 446 | return; |
1460 | 446 | } |
1461 | | |
1462 | 11.0k | if (EXPECTED(ast->kind >= ZEND_AST_VAR)) { |
1463 | 4.67k | uint32_t i, children = zend_ast_get_num_children(ast); |
1464 | | |
1465 | 7.33k | for (i = 1; i < children; i++) { |
1466 | 2.66k | zend_ast_destroy(ast->child[i]); |
1467 | 2.66k | } |
1468 | 4.67k | ast = ast->child[0]; |
1469 | 4.67k | goto tail_call; |
1470 | 6.39k | } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) { |
1471 | 5.25k | zval_ptr_dtor_nogc(zend_ast_get_zval(ast)); |
1472 | 5.25k | } else if (EXPECTED(zend_ast_is_list(ast))) { |
1473 | 1.07k | const zend_ast_list *list = zend_ast_get_list(ast); |
1474 | 1.07k | if (list->children) { |
1475 | 914 | uint32_t i; |
1476 | | |
1477 | 3.91k | for (i = 1; i < list->children; i++) { |
1478 | 2.99k | zend_ast_destroy(list->child[i]); |
1479 | 2.99k | } |
1480 | 914 | ast = list->child[0]; |
1481 | 914 | goto tail_call; |
1482 | 914 | } |
1483 | 1.07k | } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) { |
1484 | 0 | zend_string_release_ex(zend_ast_get_constant_name(ast), 0); |
1485 | 62 | } else if (EXPECTED(ast->kind == ZEND_AST_OP_ARRAY)) { |
1486 | 0 | destroy_op_array(zend_ast_get_op_array(ast)->op_array); |
1487 | 62 | } else if (EXPECTED(zend_ast_is_decl(ast))) { |
1488 | 36 | const zend_ast_decl *decl = (const zend_ast_decl *) ast; |
1489 | | |
1490 | 36 | if (decl->name) { |
1491 | 35 | zend_string_release_ex(decl->name, 0); |
1492 | 35 | } |
1493 | 36 | if (decl->doc_comment) { |
1494 | 0 | zend_string_release_ex(decl->doc_comment, 0); |
1495 | 0 | } |
1496 | 36 | zend_ast_destroy(decl->child[0]); |
1497 | 36 | zend_ast_destroy(decl->child[1]); |
1498 | 36 | zend_ast_destroy(decl->child[2]); |
1499 | 36 | zend_ast_destroy(decl->child[3]); |
1500 | 36 | ast = decl->child[4]; |
1501 | 36 | goto tail_call; |
1502 | 36 | } else if (EXPECTED(ast->kind == ZEND_AST_CALLABLE_CONVERT)) { |
1503 | 0 | zend_ast_fcc *fcc_ast = (zend_ast_fcc*) ast; |
1504 | |
|
1505 | 0 | ast = fcc_ast->args; |
1506 | 0 | goto tail_call; |
1507 | 0 | } |
1508 | 11.0k | } |
1509 | | |
1510 | | ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast) |
1511 | 0 | { |
1512 | 0 | zend_ast_destroy(GC_AST(ast)); |
1513 | 0 | efree(ast); |
1514 | 0 | } |
1515 | | |
1516 | 4 | ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn, void *context) { |
1517 | 4 | if (zend_ast_is_list(ast)) { |
1518 | 0 | zend_ast_list *list = zend_ast_get_list(ast); |
1519 | 0 | uint32_t i; |
1520 | 0 | for (i = 0; i < list->children; ++i) { |
1521 | 0 | fn(&list->child[i], context); |
1522 | 0 | } |
1523 | 4 | } else if (zend_ast_is_decl(ast)) { |
1524 | | /* Not implemented. */ |
1525 | 0 | ZEND_UNREACHABLE(); |
1526 | 4 | } else { |
1527 | 4 | uint32_t i, children = zend_ast_get_num_children(ast); |
1528 | 16 | for (i = 0; i < children; ++i) { |
1529 | 12 | fn(&ast->child[i], context); |
1530 | 12 | } |
1531 | 4 | } |
1532 | 4 | } |
1533 | | |
1534 | | /* |
1535 | | * Operator Precedence |
1536 | | * ==================== |
1537 | | * priority associativity operators |
1538 | | * ---------------------------------- |
1539 | | * 10 left include, include_once, eval, require, require_once |
1540 | | * 20 left , |
1541 | | * 30 left or |
1542 | | * 40 left xor |
1543 | | * 50 left and |
1544 | | * 60 right print |
1545 | | * 70 right yield |
1546 | | * 80 right => |
1547 | | * 85 right yield from |
1548 | | * 90 right = += -= *= /= .= %= &= |= ^= <<= >>= **= |
1549 | | * 100 left ? : |
1550 | | * 110 right ?? |
1551 | | * 120 left || |
1552 | | * 130 left && |
1553 | | * 140 left | |
1554 | | * 150 left ^ |
1555 | | * 160 left & |
1556 | | * 170 non-associative == != === !== |
1557 | | * 180 non-associative < <= > >= <=> |
1558 | | * 185 left . |
1559 | | * 190 left << >> |
1560 | | * 200 left + - |
1561 | | * 210 left * / % |
1562 | | * 220 right ! |
1563 | | * 230 non-associative instanceof |
1564 | | * 240 right + - ++ -- ~ (type) @ |
1565 | | * 250 right ** |
1566 | | * 260 left [ |
1567 | | * 270 non-associative clone new |
1568 | | */ |
1569 | | |
1570 | | static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent); |
1571 | | |
1572 | | static ZEND_COLD void zend_ast_export_str(smart_str *str, const zend_string *s) |
1573 | 0 | { |
1574 | 0 | size_t i; |
1575 | |
|
1576 | 0 | for (i = 0; i < ZSTR_LEN(s); i++) { |
1577 | 0 | unsigned char c = ZSTR_VAL(s)[i]; |
1578 | 0 | if (c == '\'' || c == '\\') { |
1579 | 0 | smart_str_appendc(str, '\\'); |
1580 | 0 | smart_str_appendc(str, c); |
1581 | 0 | } else { |
1582 | 0 | smart_str_appendc(str, c); |
1583 | 0 | } |
1584 | 0 | } |
1585 | 0 | } |
1586 | | |
1587 | | static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, const zend_string *s) |
1588 | 385 | { |
1589 | 385 | size_t i; |
1590 | | |
1591 | 6.35k | for (i = 0; i < ZSTR_LEN(s); i++) { |
1592 | 5.97k | unsigned char c = ZSTR_VAL(s)[i]; |
1593 | 5.97k | if (c < ' ') { |
1594 | 1.52k | switch (c) { |
1595 | 315 | case '\n': |
1596 | 315 | smart_str_appends(str, "\\n"); |
1597 | 315 | break; |
1598 | 0 | case '\r': |
1599 | 0 | smart_str_appends(str, "\\r"); |
1600 | 0 | break; |
1601 | 8 | case '\t': |
1602 | 8 | smart_str_appends(str, "\\t"); |
1603 | 8 | break; |
1604 | 0 | case '\f': |
1605 | 0 | smart_str_appends(str, "\\f"); |
1606 | 0 | break; |
1607 | 10 | case '\v': |
1608 | 10 | smart_str_appends(str, "\\v"); |
1609 | 10 | break; |
1610 | | #ifdef ZEND_WIN32 |
1611 | | case VK_ESCAPE: |
1612 | | #else |
1613 | 0 | case '\e': |
1614 | 0 | #endif |
1615 | 0 | smart_str_appends(str, "\\e"); |
1616 | 0 | break; |
1617 | 1.19k | default: |
1618 | 1.19k | smart_str_appends(str, "\\0"); |
1619 | 1.19k | smart_str_appendc(str, '0' + (c / 8)); |
1620 | 1.19k | smart_str_appendc(str, '0' + (c % 8)); |
1621 | 1.19k | break; |
1622 | 1.52k | } |
1623 | 4.44k | } else { |
1624 | 4.44k | if (c == quote || c == '$' || c == '\\') { |
1625 | 33 | smart_str_appendc(str, '\\'); |
1626 | 33 | } |
1627 | 4.44k | smart_str_appendc(str, c); |
1628 | 4.44k | } |
1629 | 5.97k | } |
1630 | 385 | } |
1631 | | |
1632 | | static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent) |
1633 | 0 | { |
1634 | 0 | while (indent > 0) { |
1635 | 0 | smart_str_appends(str, " "); |
1636 | 0 | indent--; |
1637 | 0 | } |
1638 | 0 | } |
1639 | | |
1640 | | static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent) |
1641 | 0 | { |
1642 | 0 | if (ast->kind == ZEND_AST_ZVAL) { |
1643 | 0 | const zval *zv = zend_ast_get_zval(ast); |
1644 | |
|
1645 | 0 | if (Z_TYPE_P(zv) == IS_STRING) { |
1646 | 0 | smart_str_append(str, Z_STR_P(zv)); |
1647 | 0 | return; |
1648 | 0 | } |
1649 | 0 | } |
1650 | 0 | zend_ast_export_ex(str, ast, priority, indent); |
1651 | 0 | } |
1652 | | |
1653 | | static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent) |
1654 | 1 | { |
1655 | 1 | if (ast->kind == ZEND_AST_ZVAL) { |
1656 | 0 | const zval *zv = zend_ast_get_zval(ast); |
1657 | |
|
1658 | 0 | if (Z_TYPE_P(zv) == IS_STRING) { |
1659 | 0 | if (ast->attr == ZEND_NAME_FQ) { |
1660 | 0 | smart_str_appendc(str, '\\'); |
1661 | 0 | } else if (ast->attr == ZEND_NAME_RELATIVE) { |
1662 | 0 | smart_str_appends(str, "namespace\\"); |
1663 | 0 | } |
1664 | 0 | smart_str_append(str, Z_STR_P(zv)); |
1665 | 0 | return; |
1666 | 0 | } |
1667 | 0 | } |
1668 | 1 | zend_ast_export_ex(str, ast, priority, indent); |
1669 | 1 | } |
1670 | | |
1671 | | static ZEND_COLD bool zend_ast_valid_var_char(char ch) |
1672 | 160 | { |
1673 | 160 | unsigned char c = (unsigned char)ch; |
1674 | | |
1675 | 160 | if (c != '_' && c < 127 && |
1676 | 160 | (c < '0' || c > '9') && |
1677 | 160 | (c < 'A' || c > 'Z') && |
1678 | 160 | (c < 'a' || c > 'z')) { |
1679 | 160 | return false; |
1680 | 160 | } |
1681 | 0 | return true; |
1682 | 160 | } |
1683 | | |
1684 | | static ZEND_COLD bool zend_ast_valid_var_name(const char *s, size_t len) |
1685 | 610 | { |
1686 | 610 | unsigned char c; |
1687 | 610 | size_t i; |
1688 | | |
1689 | 610 | if (len == 0) { |
1690 | 0 | return false; |
1691 | 0 | } |
1692 | 610 | c = (unsigned char)s[0]; |
1693 | 610 | if (c != '_' && c < 127 && |
1694 | 601 | (c < 'A' || c > 'Z') && |
1695 | 398 | (c < 'a' || c > 'z')) { |
1696 | 0 | return false; |
1697 | 0 | } |
1698 | 1.08k | for (i = 1; i < len; i++) { |
1699 | 472 | c = (unsigned char)s[i]; |
1700 | 472 | if (c != '_' && c < 127 && |
1701 | 375 | (c < '0' || c > '9') && |
1702 | 358 | (c < 'A' || c > 'Z') && |
1703 | 312 | (c < 'a' || c > 'z')) { |
1704 | 0 | return false; |
1705 | 0 | } |
1706 | 472 | } |
1707 | 610 | return true; |
1708 | 610 | } |
1709 | | |
1710 | | static ZEND_COLD bool zend_ast_var_needs_braces(char ch) |
1711 | 160 | { |
1712 | 160 | return ch == '[' || zend_ast_valid_var_char(ch); |
1713 | 160 | } |
1714 | | |
1715 | | static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int priority, int indent) |
1716 | 610 | { |
1717 | 610 | if (ast->kind == ZEND_AST_ZVAL) { |
1718 | 610 | zval *zv = zend_ast_get_zval(ast); |
1719 | 610 | if (Z_TYPE_P(zv) == IS_STRING && |
1720 | 610 | zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) { |
1721 | 610 | smart_str_append(str, Z_STR_P(zv)); |
1722 | 610 | return; |
1723 | 610 | } |
1724 | 610 | } else if (ast->kind == ZEND_AST_VAR) { |
1725 | 0 | zend_ast_export_ex(str, ast, 0, indent); |
1726 | 0 | return; |
1727 | 0 | } |
1728 | 0 | smart_str_appendc(str, '{'); |
1729 | 0 | zend_ast_export_name(str, ast, 0, indent); |
1730 | 0 | smart_str_appendc(str, '}'); |
1731 | 0 | } |
1732 | | |
1733 | | /* Use zend_ast_export_list() unless fewer than `list->children` children should |
1734 | | * be exported. */ |
1735 | | static ZEND_COLD void zend_ast_export_list_ex(smart_str *str, const zend_ast_list *list, bool separator, int priority, int indent, uint32_t children) |
1736 | 2 | { |
1737 | 2 | ZEND_ASSERT(children <= list->children); |
1738 | 2 | uint32_t i = 0; |
1739 | | |
1740 | 2 | while (i < children) { |
1741 | 0 | if (i != 0 && separator) { |
1742 | 0 | smart_str_appends(str, ", "); |
1743 | 0 | } |
1744 | 0 | zend_ast_export_ex(str, list->child[i], priority, indent); |
1745 | 0 | i++; |
1746 | 0 | } |
1747 | 2 | } |
1748 | | |
1749 | | static ZEND_COLD void zend_ast_export_list(smart_str *str, const zend_ast_list *list, bool separator, int priority, int indent) |
1750 | 2 | { |
1751 | 2 | zend_ast_export_list_ex(str, list, separator, priority, indent, list->children); |
1752 | 2 | } |
1753 | | |
1754 | | static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, const zend_ast_list *list, int indent) |
1755 | 1 | { |
1756 | 1 | uint32_t i = 0; |
1757 | 1 | zend_ast *ast; |
1758 | | |
1759 | 772 | while (i < list->children) { |
1760 | 771 | ast = list->child[i]; |
1761 | 771 | if (ast->kind == ZEND_AST_ZVAL) { |
1762 | 385 | const zval *zv = zend_ast_get_zval(ast); |
1763 | | |
1764 | 385 | ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING); |
1765 | 385 | zend_ast_export_qstr(str, quote, Z_STR_P(zv)); |
1766 | 386 | } else if (ast->kind == ZEND_AST_VAR && |
1767 | 162 | ast->child[0]->kind == ZEND_AST_ZVAL && |
1768 | 162 | (i + 1 == list->children || |
1769 | 162 | list->child[i + 1]->kind != ZEND_AST_ZVAL || |
1770 | 160 | !zend_ast_var_needs_braces( |
1771 | 160 | *Z_STRVAL_P( |
1772 | 162 | zend_ast_get_zval(list->child[i + 1]))))) { |
1773 | 162 | zend_ast_export_ex(str, ast, 0, indent); |
1774 | 224 | } else { |
1775 | 224 | smart_str_appendc(str, '{'); |
1776 | 224 | zend_ast_export_ex(str, ast, 0, indent); |
1777 | 224 | smart_str_appendc(str, '}'); |
1778 | 224 | } |
1779 | 771 | i++; |
1780 | 771 | } |
1781 | 1 | } |
1782 | | |
1783 | | static ZEND_COLD void zend_ast_export_name_list_ex(smart_str *str, const zend_ast_list *list, int indent, const char *separator) |
1784 | 0 | { |
1785 | 0 | uint32_t i = 0; |
1786 | |
|
1787 | 0 | while (i < list->children) { |
1788 | 0 | if (i != 0) { |
1789 | 0 | smart_str_appends(str, separator); |
1790 | 0 | } |
1791 | 0 | zend_ast_export_name(str, list->child[i], 0, indent); |
1792 | 0 | i++; |
1793 | 0 | } |
1794 | 0 | } |
1795 | | |
1796 | 0 | #define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ") |
1797 | 0 | #define zend_ast_export_catch_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, "|") |
1798 | | |
1799 | | static ZEND_COLD void zend_ast_export_var_list(smart_str *str, const zend_ast_list *list, int indent) |
1800 | 0 | { |
1801 | 0 | uint32_t i = 0; |
1802 | |
|
1803 | 0 | while (i < list->children) { |
1804 | 0 | if (i != 0) { |
1805 | 0 | smart_str_appends(str, ", "); |
1806 | 0 | } |
1807 | 0 | if (list->child[i]->attr & ZEND_BIND_REF) { |
1808 | 0 | smart_str_appendc(str, '&'); |
1809 | 0 | } |
1810 | 0 | smart_str_appendc(str, '$'); |
1811 | 0 | zend_ast_export_name(str, list->child[i], 20, indent); |
1812 | 0 | i++; |
1813 | 0 | } |
1814 | 0 | } |
1815 | | |
1816 | | static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent) |
1817 | 0 | { |
1818 | 0 | if (!ast) { |
1819 | 0 | return; |
1820 | 0 | } |
1821 | | |
1822 | 0 | if (ast->kind == ZEND_AST_STMT_LIST || |
1823 | 0 | ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) { |
1824 | 0 | const zend_ast_list *list = (const zend_ast_list*)ast; |
1825 | 0 | uint32_t i = 0; |
1826 | |
|
1827 | 0 | while (i < list->children) { |
1828 | 0 | ast = list->child[i]; |
1829 | 0 | zend_ast_export_stmt(str, ast, indent); |
1830 | 0 | i++; |
1831 | 0 | } |
1832 | 0 | } else { |
1833 | 0 | zend_ast_export_indent(str, indent); |
1834 | 0 | zend_ast_export_ex(str, ast, 0, indent); |
1835 | 0 | switch (ast->kind) { |
1836 | 0 | case ZEND_AST_LABEL: |
1837 | 0 | case ZEND_AST_IF: |
1838 | 0 | case ZEND_AST_SWITCH: |
1839 | 0 | case ZEND_AST_WHILE: |
1840 | 0 | case ZEND_AST_TRY: |
1841 | 0 | case ZEND_AST_FOR: |
1842 | 0 | case ZEND_AST_FOREACH: |
1843 | 0 | case ZEND_AST_FUNC_DECL: |
1844 | 0 | case ZEND_AST_METHOD: |
1845 | 0 | case ZEND_AST_CLASS: |
1846 | 0 | case ZEND_AST_USE_TRAIT: |
1847 | 0 | case ZEND_AST_NAMESPACE: |
1848 | 0 | case ZEND_AST_DECLARE: |
1849 | 0 | break; |
1850 | 0 | case ZEND_AST_PROP_GROUP: { |
1851 | 0 | const zend_ast *first_prop = zend_ast_get_list(ast->child[1])->child[0]; |
1852 | 0 | const zend_ast *hook_list = first_prop->child[3]; |
1853 | 0 | if (hook_list == NULL) { |
1854 | 0 | smart_str_appendc(str, ';'); |
1855 | 0 | } |
1856 | 0 | break; |
1857 | 0 | } |
1858 | 0 | default: |
1859 | 0 | smart_str_appendc(str, ';'); |
1860 | 0 | break; |
1861 | 0 | } |
1862 | 0 | smart_str_appendc(str, '\n'); |
1863 | 0 | } |
1864 | 0 | } |
1865 | | |
1866 | | static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, const zend_ast_list *list, int indent) |
1867 | 0 | { |
1868 | 0 | uint32_t i; |
1869 | 0 | zend_ast *ast; |
1870 | |
|
1871 | 0 | tail_call: |
1872 | 0 | i = 0; |
1873 | 0 | while (i < list->children) { |
1874 | 0 | ast = list->child[i]; |
1875 | 0 | ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM); |
1876 | 0 | if (ast->child[0]) { |
1877 | 0 | if (i == 0) { |
1878 | 0 | smart_str_appends(str, "if ("); |
1879 | 0 | } else { |
1880 | 0 | zend_ast_export_indent(str, indent); |
1881 | 0 | smart_str_appends(str, "} elseif ("); |
1882 | 0 | } |
1883 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
1884 | 0 | smart_str_appends(str, ") {\n"); |
1885 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
1886 | 0 | } else { |
1887 | 0 | zend_ast_export_indent(str, indent); |
1888 | 0 | smart_str_appends(str, "} else "); |
1889 | 0 | if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) { |
1890 | 0 | list = (const zend_ast_list*)ast->child[1]; |
1891 | 0 | goto tail_call; |
1892 | 0 | } else { |
1893 | 0 | smart_str_appends(str, "{\n"); |
1894 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
1895 | 0 | } |
1896 | 0 | } |
1897 | 0 | i++; |
1898 | 0 | } |
1899 | 0 | zend_ast_export_indent(str, indent); |
1900 | 0 | smart_str_appendc(str, '}'); |
1901 | 0 | } |
1902 | | |
1903 | | static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int priority, int indent) |
1904 | 7 | { |
1905 | 7 | ZVAL_DEREF(zv); |
1906 | 7 | switch (Z_TYPE_P(zv)) { |
1907 | 0 | case IS_NULL: |
1908 | 0 | smart_str_appends(str, "null"); |
1909 | 0 | break; |
1910 | 0 | case IS_FALSE: |
1911 | 0 | smart_str_appends(str, "false"); |
1912 | 0 | break; |
1913 | 0 | case IS_TRUE: |
1914 | 0 | smart_str_appends(str, "true"); |
1915 | 0 | break; |
1916 | 5 | case IS_LONG: |
1917 | 5 | smart_str_append_long(str, Z_LVAL_P(zv)); |
1918 | 5 | break; |
1919 | 2 | case IS_DOUBLE: |
1920 | 2 | smart_str_append_double( |
1921 | 2 | str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true); |
1922 | 2 | break; |
1923 | 0 | case IS_STRING: |
1924 | 0 | smart_str_appendc(str, '\''); |
1925 | 0 | zend_ast_export_str(str, Z_STR_P(zv)); |
1926 | 0 | smart_str_appendc(str, '\''); |
1927 | 0 | break; |
1928 | 0 | case IS_ARRAY: { |
1929 | 0 | zend_long idx; |
1930 | 0 | zend_string *key; |
1931 | 0 | zval *val; |
1932 | 0 | bool first = true; |
1933 | 0 | smart_str_appendc(str, '['); |
1934 | 0 | ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zv), idx, key, val) { |
1935 | 0 | if (first) { |
1936 | 0 | first = false; |
1937 | 0 | } else { |
1938 | 0 | smart_str_appends(str, ", "); |
1939 | 0 | } |
1940 | 0 | if (key) { |
1941 | 0 | smart_str_appendc(str, '\''); |
1942 | 0 | zend_ast_export_str(str, key); |
1943 | 0 | smart_str_appends(str, "' => "); |
1944 | 0 | } else { |
1945 | 0 | smart_str_append_long(str, idx); |
1946 | 0 | smart_str_appends(str, " => "); |
1947 | 0 | } |
1948 | 0 | zend_ast_export_zval(str, val, 0, indent); |
1949 | 0 | } ZEND_HASH_FOREACH_END(); |
1950 | 0 | smart_str_appendc(str, ']'); |
1951 | 0 | break; |
1952 | 0 | } |
1953 | 0 | case IS_CONSTANT_AST: |
1954 | 0 | zend_ast_export_ex(str, Z_ASTVAL_P(zv), priority, indent); |
1955 | 0 | break; |
1956 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
1957 | 7 | } |
1958 | 7 | } |
1959 | | |
1960 | 0 | static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, const zend_ast_decl *decl, int indent) { |
1961 | 0 | if (decl->child[0]) { |
1962 | 0 | smart_str_appends(str, " extends "); |
1963 | 0 | zend_ast_export_ns_name(str, decl->child[0], 0, indent); |
1964 | 0 | } |
1965 | 0 | if (decl->child[1]) { |
1966 | 0 | smart_str_appends(str, " implements "); |
1967 | 0 | zend_ast_export_ex(str, decl->child[1], 0, indent); |
1968 | 0 | } |
1969 | 0 | smart_str_appends(str, " {\n"); |
1970 | 0 | zend_ast_export_stmt(str, decl->child[2], indent + 1); |
1971 | 0 | zend_ast_export_indent(str, indent); |
1972 | 0 | smart_str_appendc(str, '}'); |
1973 | 0 | } |
1974 | | |
1975 | 0 | static ZEND_COLD void zend_ast_export_attribute_group(smart_str *str, zend_ast *ast, int indent) { |
1976 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
1977 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
1978 | 0 | const zend_ast *attr = list->child[i]; |
1979 | |
|
1980 | 0 | if (i) { |
1981 | 0 | smart_str_appends(str, ", "); |
1982 | 0 | } |
1983 | 0 | zend_ast_export_ns_name(str, attr->child[0], 0, indent); |
1984 | |
|
1985 | 0 | if (attr->child[1]) { |
1986 | 0 | smart_str_appendc(str, '('); |
1987 | 0 | zend_ast_export_ex(str, attr->child[1], 0, indent); |
1988 | 0 | smart_str_appendc(str, ')'); |
1989 | 0 | } |
1990 | 0 | } |
1991 | 0 | } |
1992 | | |
1993 | 0 | static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast, int indent, bool newlines) { |
1994 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
1995 | 0 | uint32_t i; |
1996 | |
|
1997 | 0 | for (i = 0; i < list->children; i++) { |
1998 | 0 | smart_str_appends(str, "#["); |
1999 | 0 | zend_ast_export_attribute_group(str, list->child[i], indent); |
2000 | 0 | smart_str_appendc(str, ']'); |
2001 | |
|
2002 | 0 | if (newlines) { |
2003 | 0 | smart_str_appendc(str, '\n'); |
2004 | 0 | zend_ast_export_indent(str, indent); |
2005 | 0 | } else { |
2006 | 0 | smart_str_appendc(str, ' '); |
2007 | 0 | } |
2008 | 0 | } |
2009 | 0 | } |
2010 | | |
2011 | 1 | static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags, zend_modifier_target target) { |
2012 | 1 | if (flags & ZEND_ACC_PUBLIC) { |
2013 | 0 | smart_str_appends(str, "public "); |
2014 | 1 | } else if (flags & ZEND_ACC_PROTECTED) { |
2015 | 0 | smart_str_appends(str, "protected "); |
2016 | 1 | } else if (flags & ZEND_ACC_PRIVATE) { |
2017 | 0 | smart_str_appends(str, "private "); |
2018 | 0 | } |
2019 | | |
2020 | 1 | if (target == ZEND_MODIFIER_TARGET_PROPERTY || target == ZEND_MODIFIER_TARGET_CPP) { |
2021 | 0 | if (flags & ZEND_ACC_PRIVATE_SET) { |
2022 | 0 | smart_str_appends(str, "private(set) "); |
2023 | 0 | } else if (flags & ZEND_ACC_PROTECTED_SET) { |
2024 | 0 | smart_str_appends(str, "protected(set) "); |
2025 | 0 | } else if (flags & ZEND_ACC_PUBLIC_SET) { |
2026 | 0 | smart_str_appends(str, "public(set) "); |
2027 | 0 | } |
2028 | 0 | } |
2029 | 1 | } |
2030 | | |
2031 | 0 | static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) { |
2032 | 0 | if (ast->kind == ZEND_AST_TYPE_UNION) { |
2033 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
2034 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
2035 | 0 | if (i != 0) { |
2036 | 0 | smart_str_appendc(str, '|'); |
2037 | 0 | } |
2038 | 0 | zend_ast_export_type(str, list->child[i], indent); |
2039 | 0 | } |
2040 | 0 | return; |
2041 | 0 | } |
2042 | 0 | if (ast->kind == ZEND_AST_TYPE_INTERSECTION) { |
2043 | 0 | const zend_ast_list *list = zend_ast_get_list(ast); |
2044 | 0 | for (uint32_t i = 0; i < list->children; i++) { |
2045 | 0 | if (i != 0) { |
2046 | 0 | smart_str_appendc(str, '&'); |
2047 | 0 | } |
2048 | 0 | zend_ast_export_type(str, list->child[i], indent); |
2049 | 0 | } |
2050 | 0 | return; |
2051 | 0 | } |
2052 | 0 | if (ast->attr & ZEND_TYPE_NULLABLE) { |
2053 | 0 | smart_str_appendc(str, '?'); |
2054 | 0 | } |
2055 | 0 | zend_ast_export_ns_name(str, ast, 0, indent); |
2056 | 0 | } |
2057 | | |
2058 | | static ZEND_COLD void zend_ast_export_hook_list(smart_str *str, const zend_ast_list *hook_list, int indent) |
2059 | 0 | { |
2060 | 0 | smart_str_appends(str, " {"); |
2061 | 0 | smart_str_appendc(str, '\n'); |
2062 | 0 | indent++; |
2063 | 0 | zend_ast_export_indent(str, indent); |
2064 | |
|
2065 | 0 | for (uint32_t i = 0; i < hook_list->children; i++) { |
2066 | 0 | const zend_ast_decl *hook = (const zend_ast_decl *)hook_list->child[i]; |
2067 | 0 | zend_ast_export_visibility(str, hook->flags, ZEND_MODIFIER_TARGET_PROPERTY); |
2068 | 0 | if (hook->flags & ZEND_ACC_FINAL) { |
2069 | 0 | smart_str_appends(str, "final "); |
2070 | 0 | } |
2071 | 0 | smart_str_append(str, hook->name); |
2072 | 0 | zend_ast *body = hook->child[2]; |
2073 | 0 | if (body == NULL) { |
2074 | 0 | smart_str_appendc(str, ';'); |
2075 | 0 | } else if (body->kind == ZEND_AST_PROPERTY_HOOK_SHORT_BODY) { |
2076 | 0 | smart_str_appends(str, " => "); |
2077 | 0 | zend_ast_export_ex(str, body->child[0], 0, indent); |
2078 | 0 | smart_str_appendc(str, ';'); |
2079 | 0 | } else { |
2080 | 0 | smart_str_appends(str, " {\n"); |
2081 | 0 | zend_ast_export_stmt(str, body, indent + 1); |
2082 | 0 | zend_ast_export_indent(str, indent); |
2083 | 0 | smart_str_appendc(str, '}'); |
2084 | 0 | } |
2085 | 0 | if (i < (hook_list->children - 1)) { |
2086 | 0 | smart_str_appendc(str, '\n'); |
2087 | 0 | zend_ast_export_indent(str, indent); |
2088 | 0 | } |
2089 | 0 | } |
2090 | 0 | smart_str_appendc(str, '\n'); |
2091 | 0 | indent--; |
2092 | 0 | zend_ast_export_indent(str, indent); |
2093 | 0 | smart_str_appendc(str, '}'); |
2094 | 0 | } |
2095 | | |
2096 | 7 | #define BINARY_OP(_op, _p, _pl, _pr) do { \ |
2097 | 7 | op = _op; \ |
2098 | 7 | p = _p; \ |
2099 | 7 | pl = _pl; \ |
2100 | 7 | pr = _pr; \ |
2101 | 7 | goto binary_op; \ |
2102 | 7 | } while (0) |
2103 | | |
2104 | 0 | #define PREFIX_OP(_op, _p, _pl) do { \ |
2105 | 0 | op = _op; \ |
2106 | 0 | p = _p; \ |
2107 | 0 | pl = _pl; \ |
2108 | 0 | goto prefix_op; \ |
2109 | 0 | } while (0) |
2110 | | |
2111 | 0 | #define FUNC_OP(_op) do { \ |
2112 | 0 | op = _op; \ |
2113 | 0 | goto func_op; \ |
2114 | 0 | } while (0) |
2115 | | |
2116 | 0 | #define POSTFIX_OP(_op, _p, _pl) do { \ |
2117 | 0 | op = _op; \ |
2118 | 0 | p = _p; \ |
2119 | 0 | pl = _pl; \ |
2120 | 0 | goto postfix_op; \ |
2121 | 0 | } while (0) |
2122 | | |
2123 | 0 | #define APPEND_NODE_1(_op) do { \ |
2124 | 0 | op = _op; \ |
2125 | 0 | goto append_node_1; \ |
2126 | 0 | } while (0) |
2127 | | |
2128 | 0 | #define APPEND_STR(_op) do { \ |
2129 | 0 | op = _op; \ |
2130 | 0 | goto append_str; \ |
2131 | 0 | } while (0) |
2132 | | |
2133 | 0 | #define APPEND_DEFAULT_VALUE(n) do { \ |
2134 | 0 | p = n; \ |
2135 | 0 | goto append_default_value; \ |
2136 | 0 | } while (0) |
2137 | | |
2138 | | static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent) |
2139 | 630 | { |
2140 | 630 | const zend_ast_decl *decl; |
2141 | 630 | int p, pl, pr; |
2142 | 630 | const char *op; |
2143 | | |
2144 | 630 | tail_call: |
2145 | 630 | if (!ast) { |
2146 | 1 | return; |
2147 | 1 | } |
2148 | 629 | switch (ast->kind) { |
2149 | | /* special nodes */ |
2150 | 7 | case ZEND_AST_ZVAL: |
2151 | 7 | zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent); |
2152 | 7 | break; |
2153 | 0 | case ZEND_AST_CONSTANT: { |
2154 | 0 | zend_string *name = zend_ast_get_constant_name(ast); |
2155 | 0 | smart_str_appendl(str, ZSTR_VAL(name), ZSTR_LEN(name)); |
2156 | 0 | break; |
2157 | 0 | } |
2158 | 0 | case ZEND_AST_OP_ARRAY: |
2159 | 0 | smart_str_appends(str, "Closure("); |
2160 | 0 | smart_str_append(str, zend_ast_get_op_array(ast)->op_array->function_name); |
2161 | 0 | smart_str_appendc(str, ')'); |
2162 | 0 | break; |
2163 | 0 | case ZEND_AST_CONSTANT_CLASS: |
2164 | 0 | smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1); |
2165 | 0 | break; |
2166 | 0 | case ZEND_AST_ZNODE: |
2167 | | /* This AST kind is only used for temporary nodes during compilation */ |
2168 | 0 | ZEND_UNREACHABLE(); |
2169 | 0 | break; |
2170 | | |
2171 | | /* declaration nodes */ |
2172 | 0 | case ZEND_AST_FUNC_DECL: |
2173 | 0 | case ZEND_AST_CLOSURE: |
2174 | 1 | case ZEND_AST_ARROW_FUNC: |
2175 | 1 | case ZEND_AST_METHOD: |
2176 | 1 | decl = (const zend_ast_decl *) ast; |
2177 | 1 | if (decl->kind == ZEND_AST_ARROW_FUNC && (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC)) { |
2178 | 0 | smart_str_appendc(str, '('); |
2179 | 0 | } |
2180 | 1 | if (decl->child[4]) { |
2181 | 0 | bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC); |
2182 | 0 | zend_ast_export_attributes(str, decl->child[4], indent, newlines); |
2183 | 0 | } |
2184 | | |
2185 | 1 | zend_ast_export_visibility(str, decl->flags, ZEND_MODIFIER_TARGET_METHOD); |
2186 | | |
2187 | 1 | if (decl->flags & ZEND_ACC_STATIC) { |
2188 | 0 | smart_str_appends(str, "static "); |
2189 | 0 | } |
2190 | 1 | if (decl->flags & ZEND_ACC_ABSTRACT) { |
2191 | 0 | smart_str_appends(str, "abstract "); |
2192 | 0 | } |
2193 | 1 | if (decl->flags & ZEND_ACC_FINAL) { |
2194 | 0 | smart_str_appends(str, "final "); |
2195 | 0 | } |
2196 | 1 | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
2197 | 1 | smart_str_appends(str, "fn"); |
2198 | 1 | } else { |
2199 | 0 | smart_str_appends(str, "function "); |
2200 | 0 | } |
2201 | 1 | if (decl->flags & ZEND_ACC_RETURN_REFERENCE) { |
2202 | 0 | smart_str_appendc(str, '&'); |
2203 | 0 | } |
2204 | 1 | if (ast->kind != ZEND_AST_CLOSURE && ast->kind != ZEND_AST_ARROW_FUNC) { |
2205 | 0 | smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name)); |
2206 | 0 | } |
2207 | 1 | smart_str_appendc(str, '('); |
2208 | 1 | zend_ast_export_ex(str, decl->child[0], 0, indent); |
2209 | 1 | smart_str_appendc(str, ')'); |
2210 | 1 | zend_ast_export_ex(str, decl->child[1], 0, indent); |
2211 | 1 | if (decl->child[3]) { |
2212 | 0 | smart_str_appends(str, ": "); |
2213 | 0 | zend_ast_export_type(str, decl->child[3], indent); |
2214 | 0 | } |
2215 | 1 | if (decl->child[2]) { |
2216 | 1 | if (decl->kind == ZEND_AST_ARROW_FUNC) { |
2217 | 1 | zend_ast *body = decl->child[2]; |
2218 | 1 | if (body->kind == ZEND_AST_RETURN) { |
2219 | 0 | body = body->child[0]; |
2220 | 0 | } |
2221 | 1 | smart_str_appends(str, " => "); |
2222 | 1 | zend_ast_export_ex(str, body, 0, indent); |
2223 | 1 | if (decl->attr & ZEND_PARENTHESIZED_ARROW_FUNC) { |
2224 | 0 | smart_str_appendc(str, ')'); |
2225 | 0 | } |
2226 | 1 | break; |
2227 | 1 | } |
2228 | | |
2229 | 0 | smart_str_appends(str, " {\n"); |
2230 | 0 | zend_ast_export_stmt(str, decl->child[2], indent + 1); |
2231 | 0 | zend_ast_export_indent(str, indent); |
2232 | 0 | smart_str_appendc(str, '}'); |
2233 | 0 | if (ast->kind != ZEND_AST_CLOSURE) { |
2234 | 0 | smart_str_appendc(str, '\n'); |
2235 | 0 | } |
2236 | 0 | } else { |
2237 | 0 | smart_str_appends(str, ";\n"); |
2238 | 0 | } |
2239 | 0 | break; |
2240 | 0 | case ZEND_AST_CLASS: |
2241 | 0 | decl = (const zend_ast_decl *) ast; |
2242 | 0 | if (decl->child[3]) { |
2243 | 0 | zend_ast_export_attributes(str, decl->child[3], indent, true); |
2244 | 0 | } |
2245 | 0 | if (decl->flags & ZEND_ACC_INTERFACE) { |
2246 | 0 | smart_str_appends(str, "interface "); |
2247 | 0 | } else if (decl->flags & ZEND_ACC_TRAIT) { |
2248 | 0 | smart_str_appends(str, "trait "); |
2249 | 0 | } else if (decl->flags & ZEND_ACC_ENUM) { |
2250 | 0 | smart_str_appends(str, "enum "); |
2251 | 0 | } else { |
2252 | 0 | if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) { |
2253 | 0 | smart_str_appends(str, "abstract "); |
2254 | 0 | } |
2255 | 0 | if (decl->flags & ZEND_ACC_FINAL) { |
2256 | 0 | smart_str_appends(str, "final "); |
2257 | 0 | } |
2258 | 0 | if (decl->flags & ZEND_ACC_READONLY_CLASS) { |
2259 | 0 | smart_str_appends(str, "readonly "); |
2260 | 0 | } |
2261 | 0 | smart_str_appends(str, "class "); |
2262 | 0 | } |
2263 | 0 | smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name)); |
2264 | 0 | if (decl->flags & ZEND_ACC_ENUM && decl->child[4]) { |
2265 | 0 | smart_str_appends(str, ": "); |
2266 | 0 | zend_ast_export_type(str, decl->child[4], indent); |
2267 | 0 | } |
2268 | 0 | zend_ast_export_class_no_header(str, decl, indent); |
2269 | 0 | smart_str_appendc(str, '\n'); |
2270 | 0 | break; |
2271 | | |
2272 | | /* list nodes */ |
2273 | 1 | case ZEND_AST_ARG_LIST: |
2274 | 1 | case ZEND_AST_EXPR_LIST: |
2275 | 2 | case ZEND_AST_PARAM_LIST: |
2276 | 2 | simple_list: |
2277 | 2 | zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent); |
2278 | 2 | break; |
2279 | 0 | case ZEND_AST_ARRAY: |
2280 | 0 | smart_str_appendc(str, '['); |
2281 | 0 | zend_ast_export_list(str, zend_ast_get_list(ast), true, 20, indent); |
2282 | 0 | smart_str_appendc(str, ']'); |
2283 | 0 | break; |
2284 | 1 | case ZEND_AST_ENCAPS_LIST: |
2285 | 1 | smart_str_appendc(str, '"'); |
2286 | 1 | zend_ast_export_encaps_list(str, '"', zend_ast_get_list(ast), indent); |
2287 | 1 | smart_str_appendc(str, '"'); |
2288 | 1 | break; |
2289 | 0 | case ZEND_AST_STMT_LIST: |
2290 | 0 | case ZEND_AST_TRAIT_ADAPTATIONS: |
2291 | 0 | zend_ast_export_stmt(str, ast, indent); |
2292 | 0 | break; |
2293 | 0 | case ZEND_AST_IF: |
2294 | 0 | zend_ast_export_if_stmt(str, zend_ast_get_list(ast), indent); |
2295 | 0 | break; |
2296 | 0 | case ZEND_AST_SWITCH_LIST: |
2297 | 0 | case ZEND_AST_CATCH_LIST: |
2298 | 0 | case ZEND_AST_MATCH_ARM_LIST: |
2299 | 0 | zend_ast_export_list(str, zend_ast_get_list(ast), false, 0, indent); |
2300 | 0 | break; |
2301 | 0 | case ZEND_AST_CLOSURE_USES: |
2302 | 0 | smart_str_appends(str, " use("); |
2303 | 0 | zend_ast_export_var_list(str, zend_ast_get_list(ast), indent); |
2304 | 0 | smart_str_appendc(str, ')'); |
2305 | 0 | break; |
2306 | 0 | case ZEND_AST_PROP_GROUP: { |
2307 | 0 | zend_ast *type_ast = ast->child[0]; |
2308 | 0 | zend_ast *prop_ast = ast->child[1]; |
2309 | |
|
2310 | 0 | if (ast->child[2]) { |
2311 | 0 | zend_ast_export_attributes(str, ast->child[2], indent, true); |
2312 | 0 | } |
2313 | |
|
2314 | 0 | zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_PROPERTY); |
2315 | |
|
2316 | 0 | if (ast->attr & ZEND_ACC_STATIC) { |
2317 | 0 | smart_str_appends(str, "static "); |
2318 | 0 | } |
2319 | 0 | if (ast->attr & ZEND_ACC_READONLY) { |
2320 | 0 | smart_str_appends(str, "readonly "); |
2321 | 0 | } |
2322 | |
|
2323 | 0 | if (type_ast) { |
2324 | 0 | zend_ast_export_type(str, type_ast, indent); |
2325 | 0 | smart_str_appendc(str, ' '); |
2326 | 0 | } |
2327 | |
|
2328 | 0 | ast = prop_ast; |
2329 | 0 | goto simple_list; |
2330 | 0 | } |
2331 | | |
2332 | 0 | case ZEND_AST_CONST_DECL: { |
2333 | 0 | zend_ast_list *ast_list = zend_ast_get_list(ast); |
2334 | | /* Attributes are stored at the end of the list if present. */ |
2335 | 0 | if (ast_list->child[ast_list->children - 1]->kind == ZEND_AST_ATTRIBUTE_LIST) { |
2336 | 0 | zend_ast_export_attributes( |
2337 | 0 | str, |
2338 | 0 | ast_list->child[ast_list->children - 1], |
2339 | 0 | indent, |
2340 | 0 | true |
2341 | 0 | ); |
2342 | | /* So that the list printing doesn't try to print the attributes, |
2343 | | * use zend_ast_export_list_ex() to override the number of children |
2344 | | * to print. */ |
2345 | 0 | smart_str_appends(str, "const "); |
2346 | 0 | zend_ast_export_list_ex(str, ast_list, true, 20, indent, ast_list->children - 1); |
2347 | 0 | break; |
2348 | 0 | } |
2349 | 0 | smart_str_appends(str, "const "); |
2350 | 0 | goto simple_list; |
2351 | 0 | } |
2352 | 0 | case ZEND_AST_CLASS_CONST_GROUP: |
2353 | 0 | if (ast->child[1]) { |
2354 | 0 | zend_ast_export_attributes(str, ast->child[1], indent, true); |
2355 | 0 | } |
2356 | |
|
2357 | 0 | zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CONSTANT); |
2358 | 0 | smart_str_appends(str, "const "); |
2359 | 0 | if (ast->child[2]) { |
2360 | 0 | zend_ast_export_type(str, ast->child[2], indent); |
2361 | 0 | smart_str_appendc(str, ' '); |
2362 | 0 | } |
2363 | |
|
2364 | 0 | ast = ast->child[0]; |
2365 | |
|
2366 | 0 | goto simple_list; |
2367 | 0 | case ZEND_AST_NAME_LIST: |
2368 | 0 | zend_ast_export_name_list(str, zend_ast_get_list(ast), indent); |
2369 | 0 | break; |
2370 | 0 | case ZEND_AST_USE: |
2371 | 0 | smart_str_appends(str, "use "); |
2372 | 0 | if (ast->attr == T_FUNCTION) { |
2373 | 0 | smart_str_appends(str, "function "); |
2374 | 0 | } else if (ast->attr == T_CONST) { |
2375 | 0 | smart_str_appends(str, "const "); |
2376 | 0 | } |
2377 | 0 | goto simple_list; |
2378 | | |
2379 | | /* 0 child nodes */ |
2380 | 0 | case ZEND_AST_MAGIC_CONST: |
2381 | 0 | switch (ast->attr) { |
2382 | 0 | case T_LINE: APPEND_STR("__LINE__"); |
2383 | 0 | case T_FILE: APPEND_STR("__FILE__"); |
2384 | 0 | case T_DIR: APPEND_STR("__DIR__"); |
2385 | 0 | case T_TRAIT_C: APPEND_STR("__TRAIT__"); |
2386 | 0 | case T_METHOD_C: APPEND_STR("__METHOD__"); |
2387 | 0 | case T_FUNC_C: APPEND_STR("__FUNCTION__"); |
2388 | 0 | case T_PROPERTY_C: APPEND_STR("__PROPERTY__"); |
2389 | 0 | case T_NS_C: APPEND_STR("__NAMESPACE__"); |
2390 | 0 | case T_CLASS_C: APPEND_STR("__CLASS__"); |
2391 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2392 | 0 | } |
2393 | 0 | break; |
2394 | 0 | case ZEND_AST_TYPE: |
2395 | 0 | switch (ast->attr & ~ZEND_TYPE_NULLABLE) { |
2396 | 0 | case IS_ARRAY: APPEND_STR("array"); |
2397 | 0 | case IS_CALLABLE: APPEND_STR("callable"); |
2398 | 0 | case IS_STATIC: APPEND_STR("static"); |
2399 | 0 | case IS_MIXED: APPEND_STR("mixed"); |
2400 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2401 | 0 | } |
2402 | 0 | break; |
2403 | 0 | case ZEND_AST_PLACEHOLDER_ARG: |
2404 | 0 | if (ast->attr == ZEND_PLACEHOLDER_VARIADIC) { |
2405 | 0 | APPEND_STR("..."); |
2406 | 0 | } else { |
2407 | 0 | APPEND_STR("?"); |
2408 | 0 | } |
2409 | 0 | break; |
2410 | | |
2411 | | /* 1 child node */ |
2412 | 386 | case ZEND_AST_VAR: |
2413 | 386 | smart_str_appendc(str, '$'); |
2414 | 386 | zend_ast_export_var(str, ast->child[0], 0, indent); |
2415 | 386 | break; |
2416 | 0 | case ZEND_AST_CONST: |
2417 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2418 | 0 | break; |
2419 | 0 | case ZEND_AST_UNPACK: |
2420 | 0 | smart_str_appends(str, "..."); |
2421 | 0 | ast = ast->child[0]; |
2422 | 0 | goto tail_call; |
2423 | 0 | case ZEND_AST_UNARY_PLUS: PREFIX_OP("+", 240, 241); |
2424 | 0 | case ZEND_AST_UNARY_MINUS: PREFIX_OP("-", 240, 241); |
2425 | 0 | case ZEND_AST_CAST: |
2426 | 0 | switch (ast->attr) { |
2427 | 0 | case IS_NULL: PREFIX_OP("(unset)", 240, 241); |
2428 | 0 | case _IS_BOOL: PREFIX_OP("(bool)", 240, 241); |
2429 | 0 | case IS_LONG: PREFIX_OP("(int)", 240, 241); |
2430 | 0 | case IS_DOUBLE: PREFIX_OP("(float)", 240, 241); |
2431 | 0 | case IS_STRING: PREFIX_OP("(string)", 240, 241); |
2432 | 0 | case IS_ARRAY: PREFIX_OP("(array)", 240, 241); |
2433 | 0 | case IS_OBJECT: PREFIX_OP("(object)", 240, 241); |
2434 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2435 | 0 | } |
2436 | 0 | break; |
2437 | 0 | case ZEND_AST_CAST_VOID: |
2438 | 0 | PREFIX_OP("(void)", 240, 241); |
2439 | 0 | break; |
2440 | 0 | case ZEND_AST_EMPTY: |
2441 | 0 | FUNC_OP("empty"); |
2442 | 0 | case ZEND_AST_ISSET: |
2443 | 0 | FUNC_OP("isset"); |
2444 | 0 | case ZEND_AST_SILENCE: |
2445 | 0 | PREFIX_OP("@", 240, 241); |
2446 | 0 | case ZEND_AST_SHELL_EXEC: |
2447 | 0 | smart_str_appendc(str, '`'); |
2448 | 0 | if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) { |
2449 | 0 | zend_ast_export_encaps_list(str, '`', zend_ast_get_list(ast->child[0]), indent); |
2450 | 0 | } else { |
2451 | 0 | zval *zv; |
2452 | 0 | ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL); |
2453 | 0 | zv = zend_ast_get_zval(ast->child[0]); |
2454 | 0 | ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING); |
2455 | 0 | zend_ast_export_qstr(str, '`', Z_STR_P(zv)); |
2456 | 0 | } |
2457 | 0 | smart_str_appendc(str, '`'); |
2458 | 0 | break; |
2459 | 0 | case ZEND_AST_PRINT: |
2460 | 0 | PREFIX_OP("print ", 60, 61); |
2461 | 0 | case ZEND_AST_INCLUDE_OR_EVAL: |
2462 | 0 | switch (ast->attr) { |
2463 | 0 | case ZEND_INCLUDE_ONCE: FUNC_OP("include_once"); |
2464 | 0 | case ZEND_INCLUDE: FUNC_OP("include"); |
2465 | 0 | case ZEND_REQUIRE_ONCE: FUNC_OP("require_once"); |
2466 | 0 | case ZEND_REQUIRE: FUNC_OP("require"); |
2467 | 0 | case ZEND_EVAL: FUNC_OP("eval"); |
2468 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2469 | 0 | } |
2470 | 0 | break; |
2471 | 0 | case ZEND_AST_UNARY_OP: |
2472 | 0 | switch (ast->attr) { |
2473 | 0 | case ZEND_BW_NOT: PREFIX_OP("~", 240, 241); |
2474 | 0 | case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241); |
2475 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2476 | 0 | } |
2477 | 0 | break; |
2478 | 0 | case ZEND_AST_PRE_INC: |
2479 | 0 | PREFIX_OP("++", 240, 241); |
2480 | 0 | case ZEND_AST_PRE_DEC: |
2481 | 0 | PREFIX_OP("--", 240, 241); |
2482 | 0 | case ZEND_AST_POST_INC: |
2483 | 0 | POSTFIX_OP("++", 240, 241); |
2484 | 0 | case ZEND_AST_POST_DEC: |
2485 | 0 | POSTFIX_OP("--", 240, 241); |
2486 | | |
2487 | 0 | case ZEND_AST_GLOBAL: |
2488 | 0 | APPEND_NODE_1("global"); |
2489 | 0 | case ZEND_AST_UNSET: |
2490 | 0 | FUNC_OP("unset"); |
2491 | 0 | case ZEND_AST_RETURN: |
2492 | 0 | APPEND_NODE_1("return"); |
2493 | 0 | case ZEND_AST_LABEL: |
2494 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2495 | 0 | smart_str_appendc(str, ':'); |
2496 | 0 | break; |
2497 | 0 | case ZEND_AST_REF: |
2498 | 0 | smart_str_appendc(str, '&'); |
2499 | 0 | ast = ast->child[0]; |
2500 | 0 | goto tail_call; |
2501 | 0 | case ZEND_AST_HALT_COMPILER: |
2502 | 0 | APPEND_STR("__HALT_COMPILER()"); |
2503 | 0 | case ZEND_AST_ECHO: |
2504 | 0 | APPEND_NODE_1("echo"); |
2505 | 0 | case ZEND_AST_THROW: |
2506 | 0 | APPEND_NODE_1("throw"); |
2507 | 0 | case ZEND_AST_GOTO: |
2508 | 0 | smart_str_appends(str, "goto "); |
2509 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2510 | 0 | break; |
2511 | 0 | case ZEND_AST_BREAK: |
2512 | 0 | APPEND_NODE_1("break"); |
2513 | 0 | case ZEND_AST_CONTINUE: |
2514 | 0 | APPEND_NODE_1("continue"); |
2515 | | |
2516 | | /* 2 child nodes */ |
2517 | 0 | case ZEND_AST_DIM: |
2518 | 0 | zend_ast_export_ex(str, ast->child[0], 260, indent); |
2519 | 0 | smart_str_appendc(str, '['); |
2520 | 0 | if (ast->child[1]) { |
2521 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2522 | 0 | } |
2523 | 0 | smart_str_appendc(str, ']'); |
2524 | 0 | break; |
2525 | 204 | case ZEND_AST_PROP: |
2526 | 224 | case ZEND_AST_NULLSAFE_PROP: |
2527 | 224 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2528 | 224 | smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_PROP ? "?->" : "->"); |
2529 | 224 | zend_ast_export_var(str, ast->child[1], 0, indent); |
2530 | 224 | break; |
2531 | 0 | case ZEND_AST_STATIC_PROP: |
2532 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2533 | 0 | smart_str_appends(str, "::$"); |
2534 | 0 | zend_ast_export_var(str, ast->child[1], 0, indent); |
2535 | 0 | break; |
2536 | 1 | case ZEND_AST_CALL: { |
2537 | 1 | zend_ast *left = ast->child[0]; |
2538 | 1 | if (left->kind == ZEND_AST_ARROW_FUNC || left->kind == ZEND_AST_CLOSURE) { |
2539 | 1 | smart_str_appendc(str, '('); |
2540 | 1 | zend_ast_export_ns_name(str, left, 0, indent); |
2541 | 1 | smart_str_appendc(str, ')'); |
2542 | 1 | } else { |
2543 | 0 | zend_ast_export_ns_name(str, left, 0, indent); |
2544 | 0 | } |
2545 | 1 | smart_str_appendc(str, '('); |
2546 | 1 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2547 | 1 | smart_str_appendc(str, ')'); |
2548 | 1 | break; |
2549 | 204 | } |
2550 | 0 | case ZEND_AST_PARENT_PROPERTY_HOOK_CALL: |
2551 | 0 | smart_str_append(str, Z_STR_P(zend_ast_get_zval(ast->child[0]))); |
2552 | 0 | smart_str_appendc(str, '('); |
2553 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2554 | 0 | smart_str_appendc(str, ')'); |
2555 | 0 | break; |
2556 | 0 | case ZEND_AST_CALLABLE_CONVERT: { |
2557 | 0 | zend_ast_fcc *fcc_ast = (zend_ast_fcc*)ast; |
2558 | 0 | ast = fcc_ast->args; |
2559 | 0 | goto simple_list; |
2560 | 204 | } |
2561 | 0 | case ZEND_AST_CLASS_CONST: |
2562 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2563 | 0 | smart_str_appends(str, "::"); |
2564 | 0 | zend_ast_export_name(str, ast->child[1], 0, indent); |
2565 | 0 | break; |
2566 | 0 | case ZEND_AST_CLASS_NAME: |
2567 | 0 | if (ast->child[0] == NULL) { |
2568 | | /* The const expr representation stores the fetch type instead. */ |
2569 | 0 | switch (ast->attr) { |
2570 | 0 | case ZEND_FETCH_CLASS_SELF: |
2571 | 0 | smart_str_append(str, ZSTR_KNOWN(ZEND_STR_SELF)); |
2572 | 0 | break; |
2573 | 0 | case ZEND_FETCH_CLASS_PARENT: |
2574 | 0 | smart_str_append(str, ZSTR_KNOWN(ZEND_STR_PARENT)); |
2575 | 0 | break; |
2576 | 0 | EMPTY_SWITCH_DEFAULT_CASE() |
2577 | 0 | } |
2578 | 0 | } else { |
2579 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2580 | 0 | } |
2581 | 0 | smart_str_appends(str, "::class"); |
2582 | 0 | break; |
2583 | 0 | case ZEND_AST_ASSIGN: BINARY_OP(" = ", 90, 91, 90); |
2584 | 0 | case ZEND_AST_ASSIGN_REF: BINARY_OP(" =& ", 90, 91, 90); |
2585 | 0 | case ZEND_AST_ASSIGN_OP: |
2586 | 0 | switch (ast->attr) { |
2587 | 0 | case ZEND_ADD: BINARY_OP(" += ", 90, 91, 90); |
2588 | 0 | case ZEND_SUB: BINARY_OP(" -= ", 90, 91, 90); |
2589 | 0 | case ZEND_MUL: BINARY_OP(" *= ", 90, 91, 90); |
2590 | 0 | case ZEND_DIV: BINARY_OP(" /= ", 90, 91, 90); |
2591 | 0 | case ZEND_MOD: BINARY_OP(" %= ", 90, 91, 90); |
2592 | 0 | case ZEND_SL: BINARY_OP(" <<= ", 90, 91, 90); |
2593 | 0 | case ZEND_SR: BINARY_OP(" >>= ", 90, 91, 90); |
2594 | 0 | case ZEND_CONCAT: BINARY_OP(" .= ", 90, 91, 90); |
2595 | 0 | case ZEND_BW_OR: BINARY_OP(" |= ", 90, 91, 90); |
2596 | 0 | case ZEND_BW_AND: BINARY_OP(" &= ", 90, 91, 90); |
2597 | 0 | case ZEND_BW_XOR: BINARY_OP(" ^= ", 90, 91, 90); |
2598 | 0 | case ZEND_POW: BINARY_OP(" **= ", 90, 91, 90); |
2599 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2600 | 0 | } |
2601 | 0 | break; |
2602 | 0 | case ZEND_AST_ASSIGN_COALESCE: BINARY_OP(" \?\?= ", 90, 91, 90); |
2603 | 7 | case ZEND_AST_BINARY_OP: |
2604 | 7 | switch (ast->attr) { |
2605 | 0 | case ZEND_ADD: BINARY_OP(" + ", 200, 200, 201); |
2606 | 3 | case ZEND_SUB: BINARY_OP(" - ", 200, 200, 201); |
2607 | 0 | case ZEND_MUL: BINARY_OP(" * ", 210, 210, 211); |
2608 | 0 | case ZEND_DIV: BINARY_OP(" / ", 210, 210, 211); |
2609 | 1 | case ZEND_MOD: BINARY_OP(" % ", 210, 210, 211); |
2610 | 0 | case ZEND_SL: BINARY_OP(" << ", 190, 190, 191); |
2611 | 0 | case ZEND_SR: BINARY_OP(" >> ", 190, 190, 191); |
2612 | 0 | case ZEND_CONCAT: BINARY_OP(" . ", 185, 185, 186); |
2613 | 0 | case ZEND_BW_OR: BINARY_OP(" | ", 140, 140, 141); |
2614 | 2 | case ZEND_BW_AND: BINARY_OP(" & ", 160, 160, 161); |
2615 | 0 | case ZEND_BW_XOR: BINARY_OP(" ^ ", 150, 150, 151); |
2616 | 0 | case ZEND_IS_IDENTICAL: BINARY_OP(" === ", 170, 171, 171); |
2617 | 0 | case ZEND_IS_NOT_IDENTICAL: BINARY_OP(" !== ", 170, 171, 171); |
2618 | 0 | case ZEND_IS_EQUAL: BINARY_OP(" == ", 170, 171, 171); |
2619 | 0 | case ZEND_IS_NOT_EQUAL: BINARY_OP(" != ", 170, 171, 171); |
2620 | 1 | case ZEND_IS_SMALLER: BINARY_OP(" < ", 180, 181, 181); |
2621 | 0 | case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ", 180, 181, 181); |
2622 | 0 | case ZEND_POW: BINARY_OP(" ** ", 250, 251, 250); |
2623 | 0 | case ZEND_BOOL_XOR: BINARY_OP(" xor ", 40, 40, 41); |
2624 | 0 | case ZEND_SPACESHIP: BINARY_OP(" <=> ", 180, 181, 181); |
2625 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2626 | 7 | } |
2627 | 0 | break; |
2628 | 0 | case ZEND_AST_GREATER: BINARY_OP(" > ", 180, 181, 181); |
2629 | 0 | case ZEND_AST_GREATER_EQUAL: BINARY_OP(" >= ", 180, 181, 181); |
2630 | 0 | case ZEND_AST_AND: BINARY_OP(" && ", 130, 130, 131); |
2631 | 0 | case ZEND_AST_OR: BINARY_OP(" || ", 120, 120, 121); |
2632 | 0 | case ZEND_AST_PIPE: BINARY_OP(" |> ", 183, 183, 184); |
2633 | 0 | case ZEND_AST_ARRAY_ELEM: |
2634 | 0 | if (ast->child[1]) { |
2635 | 0 | zend_ast_export_ex(str, ast->child[1], 80, indent); |
2636 | 0 | smart_str_appends(str, " => "); |
2637 | 0 | } |
2638 | 0 | if (ast->attr) |
2639 | 0 | smart_str_appendc(str, '&'); |
2640 | 0 | zend_ast_export_ex(str, ast->child[0], 80, indent); |
2641 | 0 | break; |
2642 | 0 | case ZEND_AST_NEW: |
2643 | 0 | smart_str_appends(str, "new "); |
2644 | 0 | if (ast->child[0]->kind == ZEND_AST_CLASS) { |
2645 | 0 | const zend_ast_decl *decl = (const zend_ast_decl *) ast->child[0]; |
2646 | 0 | if (decl->child[3]) { |
2647 | 0 | zend_ast_export_attributes(str, decl->child[3], indent, false); |
2648 | 0 | } |
2649 | 0 | smart_str_appends(str, "class"); |
2650 | 0 | if (!zend_ast_is_list(ast->child[1]) |
2651 | 0 | || zend_ast_get_list(ast->child[1])->children) { |
2652 | 0 | smart_str_appendc(str, '('); |
2653 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2654 | 0 | smart_str_appendc(str, ')'); |
2655 | 0 | } |
2656 | 0 | zend_ast_export_class_no_header(str, decl, indent); |
2657 | 0 | } else { |
2658 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2659 | 0 | smart_str_appendc(str, '('); |
2660 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2661 | 0 | smart_str_appendc(str, ')'); |
2662 | 0 | } |
2663 | 0 | break; |
2664 | 0 | case ZEND_AST_INSTANCEOF: |
2665 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2666 | 0 | smart_str_appends(str, " instanceof "); |
2667 | 0 | zend_ast_export_ns_name(str, ast->child[1], 0, indent); |
2668 | 0 | break; |
2669 | 0 | case ZEND_AST_YIELD: |
2670 | 0 | if (priority > 70) smart_str_appendc(str, '('); |
2671 | 0 | smart_str_appends(str, "yield "); |
2672 | 0 | if (ast->child[0]) { |
2673 | 0 | if (ast->child[1]) { |
2674 | 0 | zend_ast_export_ex(str, ast->child[1], 70, indent); |
2675 | 0 | smart_str_appends(str, " => "); |
2676 | 0 | } |
2677 | 0 | zend_ast_export_ex(str, ast->child[0], 70, indent); |
2678 | 0 | } |
2679 | 0 | if (priority > 70) smart_str_appendc(str, ')'); |
2680 | 0 | break; |
2681 | 0 | case ZEND_AST_YIELD_FROM: |
2682 | 0 | PREFIX_OP("yield from ", 85, 86); |
2683 | 0 | case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110); |
2684 | 0 | case ZEND_AST_STATIC: |
2685 | 0 | smart_str_appends(str, "static $"); |
2686 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2687 | 0 | APPEND_DEFAULT_VALUE(1); |
2688 | 0 | case ZEND_AST_WHILE: |
2689 | 0 | smart_str_appends(str, "while ("); |
2690 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2691 | 0 | smart_str_appends(str, ") {\n"); |
2692 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2693 | 0 | zend_ast_export_indent(str, indent); |
2694 | 0 | smart_str_appendc(str, '}'); |
2695 | 0 | break; |
2696 | 0 | case ZEND_AST_DO_WHILE: |
2697 | 0 | smart_str_appends(str, "do {\n"); |
2698 | 0 | zend_ast_export_stmt(str, ast->child[0], indent + 1); |
2699 | 0 | zend_ast_export_indent(str, indent); |
2700 | 0 | smart_str_appends(str, "} while ("); |
2701 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2702 | 0 | smart_str_appendc(str, ')'); |
2703 | 0 | break; |
2704 | | |
2705 | 0 | case ZEND_AST_IF_ELEM: |
2706 | 0 | if (ast->child[0]) { |
2707 | 0 | smart_str_appends(str, "if ("); |
2708 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2709 | 0 | smart_str_appends(str, ") {\n"); |
2710 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2711 | 0 | } else { |
2712 | 0 | smart_str_appends(str, "else {\n"); |
2713 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2714 | 0 | } |
2715 | 0 | zend_ast_export_indent(str, indent); |
2716 | 0 | smart_str_appendc(str, '}'); |
2717 | 0 | break; |
2718 | 0 | case ZEND_AST_SWITCH: |
2719 | 0 | smart_str_appends(str, "switch ("); |
2720 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2721 | 0 | smart_str_appends(str, ") {\n"); |
2722 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent + 1); |
2723 | 0 | zend_ast_export_indent(str, indent); |
2724 | 0 | smart_str_appendc(str, '}'); |
2725 | 0 | break; |
2726 | 0 | case ZEND_AST_SWITCH_CASE: |
2727 | 0 | zend_ast_export_indent(str, indent); |
2728 | 0 | if (ast->child[0]) { |
2729 | 0 | smart_str_appends(str, "case "); |
2730 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2731 | 0 | smart_str_appends(str, ":\n"); |
2732 | 0 | } else { |
2733 | 0 | smart_str_appends(str, "default:\n"); |
2734 | 0 | } |
2735 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2736 | 0 | break; |
2737 | 0 | case ZEND_AST_MATCH: |
2738 | 0 | smart_str_appends(str, "match ("); |
2739 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2740 | 0 | smart_str_appends(str, ") {\n"); |
2741 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent + 1); |
2742 | 0 | zend_ast_export_indent(str, indent); |
2743 | 0 | smart_str_appendc(str, '}'); |
2744 | 0 | break; |
2745 | 0 | case ZEND_AST_MATCH_ARM: |
2746 | 0 | zend_ast_export_indent(str, indent); |
2747 | 0 | if (ast->child[0]) { |
2748 | 0 | zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent); |
2749 | 0 | smart_str_appends(str, " => "); |
2750 | 0 | } else { |
2751 | 0 | smart_str_appends(str, "default => "); |
2752 | 0 | } |
2753 | 0 | zend_ast_export_ex(str, ast->child[1], 0, 0); |
2754 | 0 | smart_str_appends(str, ",\n"); |
2755 | 0 | break; |
2756 | 0 | case ZEND_AST_DECLARE: |
2757 | 0 | smart_str_appends(str, "declare("); |
2758 | 0 | ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL); |
2759 | 0 | zend_ast_export_list(str, zend_ast_get_list(ast->child[0]), true, 0, indent); |
2760 | 0 | smart_str_appendc(str, ')'); |
2761 | 0 | if (ast->child[1]) { |
2762 | 0 | smart_str_appends(str, " {\n"); |
2763 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2764 | 0 | zend_ast_export_indent(str, indent); |
2765 | 0 | smart_str_appendc(str, '}'); |
2766 | 0 | } else { |
2767 | 0 | smart_str_appendc(str, ';'); |
2768 | 0 | } |
2769 | 0 | break; |
2770 | 0 | case ZEND_AST_PROP_ELEM: |
2771 | 0 | smart_str_appendc(str, '$'); |
2772 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2773 | |
|
2774 | 0 | zend_ast *default_value = ast->child[1]; |
2775 | 0 | if (default_value) { |
2776 | 0 | smart_str_appends(str, " = "); |
2777 | 0 | zend_ast_export_ex(str, default_value, 0, indent + 1); |
2778 | 0 | } |
2779 | |
|
2780 | 0 | if (ast->child[3]) { |
2781 | 0 | zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[3]), indent); |
2782 | 0 | } |
2783 | 0 | break; |
2784 | 0 | case ZEND_AST_CONST_ELEM: |
2785 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2786 | 0 | APPEND_DEFAULT_VALUE(1); |
2787 | 0 | case ZEND_AST_USE_TRAIT: |
2788 | 0 | smart_str_appends(str, "use "); |
2789 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2790 | 0 | if (ast->child[1]) { |
2791 | 0 | smart_str_appends(str, " {\n"); |
2792 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent + 1); |
2793 | 0 | zend_ast_export_indent(str, indent); |
2794 | 0 | smart_str_appendc(str, '}'); |
2795 | 0 | } else { |
2796 | 0 | smart_str_appendc(str, ';'); |
2797 | 0 | } |
2798 | 0 | break; |
2799 | 0 | case ZEND_AST_TRAIT_PRECEDENCE: |
2800 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2801 | 0 | smart_str_appends(str, " insteadof "); |
2802 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2803 | 0 | break; |
2804 | 0 | case ZEND_AST_METHOD_REFERENCE: |
2805 | 0 | if (ast->child[0]) { |
2806 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2807 | 0 | smart_str_appends(str, "::"); |
2808 | 0 | } |
2809 | 0 | zend_ast_export_name(str, ast->child[1], 0, indent); |
2810 | 0 | break; |
2811 | 0 | case ZEND_AST_NAMESPACE: |
2812 | 0 | smart_str_appends(str, "namespace"); |
2813 | 0 | if (ast->child[0]) { |
2814 | 0 | smart_str_appendc(str, ' '); |
2815 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2816 | 0 | } |
2817 | 0 | if (ast->child[1]) { |
2818 | 0 | smart_str_appends(str, " {\n"); |
2819 | 0 | zend_ast_export_stmt(str, ast->child[1], indent + 1); |
2820 | 0 | zend_ast_export_indent(str, indent); |
2821 | 0 | smart_str_appends(str, "}\n"); |
2822 | 0 | } else { |
2823 | 0 | smart_str_appendc(str, ';'); |
2824 | 0 | } |
2825 | 0 | break; |
2826 | 0 | case ZEND_AST_USE_ELEM: |
2827 | 0 | case ZEND_AST_TRAIT_ALIAS: |
2828 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2829 | 0 | if (ast->attr & ZEND_ACC_PUBLIC) { |
2830 | 0 | smart_str_appends(str, " as public"); |
2831 | 0 | } else if (ast->attr & ZEND_ACC_PROTECTED) { |
2832 | 0 | smart_str_appends(str, " as protected"); |
2833 | 0 | } else if (ast->attr & ZEND_ACC_PRIVATE) { |
2834 | 0 | smart_str_appends(str, " as private"); |
2835 | 0 | } else if (ast->child[1]) { |
2836 | 0 | smart_str_appends(str, " as"); |
2837 | 0 | } |
2838 | 0 | if (ast->child[1]) { |
2839 | 0 | smart_str_appendc(str, ' '); |
2840 | 0 | zend_ast_export_name(str, ast->child[1], 0, indent); |
2841 | 0 | } |
2842 | 0 | break; |
2843 | 0 | case ZEND_AST_NAMED_ARG: |
2844 | 0 | smart_str_append(str, zend_ast_get_str(ast->child[0])); |
2845 | 0 | smart_str_appends(str, ": "); |
2846 | 0 | ast = ast->child[1]; |
2847 | 0 | goto tail_call; |
2848 | | |
2849 | | /* 3 child nodes */ |
2850 | 0 | case ZEND_AST_METHOD_CALL: |
2851 | 0 | case ZEND_AST_NULLSAFE_METHOD_CALL: |
2852 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2853 | 0 | smart_str_appends(str, ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL ? "?->" : "->"); |
2854 | 0 | zend_ast_export_var(str, ast->child[1], 0, indent); |
2855 | 0 | smart_str_appendc(str, '('); |
2856 | 0 | zend_ast_export_ex(str, ast->child[2], 0, indent); |
2857 | 0 | smart_str_appendc(str, ')'); |
2858 | 0 | break; |
2859 | 0 | case ZEND_AST_STATIC_CALL: |
2860 | 0 | zend_ast_export_ns_name(str, ast->child[0], 0, indent); |
2861 | 0 | smart_str_appends(str, "::"); |
2862 | 0 | zend_ast_export_var(str, ast->child[1], 0, indent); |
2863 | 0 | smart_str_appendc(str, '('); |
2864 | 0 | zend_ast_export_ex(str, ast->child[2], 0, indent); |
2865 | 0 | smart_str_appendc(str, ')'); |
2866 | 0 | break; |
2867 | 0 | case ZEND_AST_CONDITIONAL: |
2868 | 0 | if (priority > 100) smart_str_appendc(str, '('); |
2869 | 0 | zend_ast_export_ex(str, ast->child[0], 100, indent); |
2870 | 0 | if (ast->child[1]) { |
2871 | 0 | smart_str_appends(str, " ? "); |
2872 | 0 | zend_ast_export_ex(str, ast->child[1], 101, indent); |
2873 | 0 | smart_str_appends(str, " : "); |
2874 | 0 | } else { |
2875 | 0 | smart_str_appends(str, " ?: "); |
2876 | 0 | } |
2877 | 0 | zend_ast_export_ex(str, ast->child[2], 101, indent); |
2878 | 0 | if (priority > 100) smart_str_appendc(str, ')'); |
2879 | 0 | break; |
2880 | | |
2881 | 0 | case ZEND_AST_TRY: |
2882 | 0 | smart_str_appends(str, "try {\n"); |
2883 | 0 | zend_ast_export_stmt(str, ast->child[0], indent + 1); |
2884 | 0 | zend_ast_export_indent(str, indent); |
2885 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2886 | 0 | if (ast->child[2]) { |
2887 | 0 | smart_str_appends(str, "} finally {\n"); |
2888 | 0 | zend_ast_export_stmt(str, ast->child[2], indent + 1); |
2889 | 0 | zend_ast_export_indent(str, indent); |
2890 | 0 | } |
2891 | 0 | smart_str_appendc(str, '}'); |
2892 | 0 | break; |
2893 | 0 | case ZEND_AST_CATCH: |
2894 | 0 | smart_str_appends(str, "} catch ("); |
2895 | 0 | zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent); |
2896 | 0 | if (ast->child[1]) { |
2897 | 0 | smart_str_appends(str, " $"); |
2898 | 0 | zend_ast_export_var(str, ast->child[1], 0, indent); |
2899 | 0 | } |
2900 | 0 | smart_str_appends(str, ") {\n"); |
2901 | 0 | zend_ast_export_stmt(str, ast->child[2], indent + 1); |
2902 | 0 | zend_ast_export_indent(str, indent); |
2903 | 0 | break; |
2904 | 0 | case ZEND_AST_PARAM: |
2905 | 0 | if (ast->child[3]) { |
2906 | 0 | zend_ast_export_attributes(str, ast->child[3], indent, false); |
2907 | 0 | } |
2908 | 0 | zend_ast_export_visibility(str, ast->attr, ZEND_MODIFIER_TARGET_CPP); |
2909 | 0 | if (ast->attr & ZEND_ACC_FINAL) { |
2910 | 0 | smart_str_appends(str, "final "); |
2911 | 0 | } |
2912 | 0 | if (ast->child[0]) { |
2913 | 0 | zend_ast_export_type(str, ast->child[0], indent); |
2914 | 0 | smart_str_appendc(str, ' '); |
2915 | 0 | } |
2916 | 0 | if (ast->attr & ZEND_PARAM_REF) { |
2917 | 0 | smart_str_appendc(str, '&'); |
2918 | 0 | } |
2919 | 0 | if (ast->attr & ZEND_PARAM_VARIADIC) { |
2920 | 0 | smart_str_appends(str, "..."); |
2921 | 0 | } |
2922 | 0 | smart_str_appendc(str, '$'); |
2923 | 0 | zend_ast_export_name(str, ast->child[1], 0, indent); |
2924 | 0 | if (ast->child[2]) { |
2925 | 0 | smart_str_appends(str, " = "); |
2926 | 0 | zend_ast_export_ex(str, ast->child[2], 0, indent); |
2927 | 0 | } |
2928 | 0 | if (ast->child[5]) { |
2929 | 0 | zend_ast_export_hook_list(str, zend_ast_get_list(ast->child[5]), indent); |
2930 | 0 | } |
2931 | 0 | break; |
2932 | 0 | case ZEND_AST_ENUM_CASE: |
2933 | 0 | if (ast->child[3]) { |
2934 | 0 | zend_ast_export_attributes(str, ast->child[3], indent, true); |
2935 | 0 | } |
2936 | 0 | smart_str_appends(str, "case "); |
2937 | 0 | zend_ast_export_name(str, ast->child[0], 0, indent); |
2938 | 0 | if (ast->child[1]) { |
2939 | 0 | smart_str_appends(str, " = "); |
2940 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2941 | 0 | } |
2942 | 0 | break; |
2943 | | |
2944 | | /* 4 child nodes */ |
2945 | 0 | case ZEND_AST_FOR: |
2946 | 0 | smart_str_appends(str, "for ("); |
2947 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2948 | 0 | smart_str_appendc(str, ';'); |
2949 | 0 | if (ast->child[1]) { |
2950 | 0 | smart_str_appendc(str, ' '); |
2951 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2952 | 0 | } |
2953 | 0 | smart_str_appendc(str, ';'); |
2954 | 0 | if (ast->child[2]) { |
2955 | 0 | smart_str_appendc(str, ' '); |
2956 | 0 | zend_ast_export_ex(str, ast->child[2], 0, indent); |
2957 | 0 | } |
2958 | 0 | smart_str_appends(str, ") {\n"); |
2959 | 0 | zend_ast_export_stmt(str, ast->child[3], indent + 1); |
2960 | 0 | zend_ast_export_indent(str, indent); |
2961 | 0 | smart_str_appendc(str, '}'); |
2962 | 0 | break; |
2963 | 0 | case ZEND_AST_FOREACH: |
2964 | 0 | smart_str_appends(str, "foreach ("); |
2965 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
2966 | 0 | smart_str_appends(str, " as "); |
2967 | 0 | if (ast->child[2]) { |
2968 | 0 | zend_ast_export_ex(str, ast->child[2], 0, indent); |
2969 | 0 | smart_str_appends(str, " => "); |
2970 | 0 | } |
2971 | 0 | zend_ast_export_ex(str, ast->child[1], 0, indent); |
2972 | 0 | smart_str_appends(str, ") {\n"); |
2973 | 0 | zend_ast_export_stmt(str, ast->child[3], indent + 1); |
2974 | 0 | zend_ast_export_indent(str, indent); |
2975 | 0 | smart_str_appendc(str, '}'); |
2976 | 0 | break; |
2977 | 0 | EMPTY_SWITCH_DEFAULT_CASE(); |
2978 | 629 | } |
2979 | 622 | return; |
2980 | | |
2981 | 622 | binary_op: |
2982 | 7 | if (priority > p) smart_str_appendc(str, '('); |
2983 | 7 | zend_ast_export_ex(str, ast->child[0], pl, indent); |
2984 | 7 | smart_str_appends(str, op); |
2985 | 7 | zend_ast_export_ex(str, ast->child[1], pr, indent); |
2986 | 7 | if (priority > p) smart_str_appendc(str, ')'); |
2987 | 7 | return; |
2988 | | |
2989 | 0 | prefix_op: |
2990 | 0 | if (priority > p) smart_str_appendc(str, '('); |
2991 | 0 | smart_str_appends(str, op); |
2992 | 0 | zend_ast_export_ex(str, ast->child[0], pl, indent); |
2993 | 0 | if (priority > p) smart_str_appendc(str, ')'); |
2994 | 0 | return; |
2995 | | |
2996 | 0 | postfix_op: |
2997 | 0 | if (priority > p) smart_str_appendc(str, '('); |
2998 | 0 | zend_ast_export_ex(str, ast->child[0], pl, indent); |
2999 | 0 | smart_str_appends(str, op); |
3000 | 0 | if (priority > p) smart_str_appendc(str, ')'); |
3001 | 0 | return; |
3002 | | |
3003 | 0 | func_op: |
3004 | 0 | smart_str_appends(str, op); |
3005 | 0 | smart_str_appendc(str, '('); |
3006 | 0 | zend_ast_export_ex(str, ast->child[0], 0, indent); |
3007 | 0 | smart_str_appendc(str, ')'); |
3008 | 0 | return; |
3009 | | |
3010 | 0 | append_node_1: |
3011 | 0 | smart_str_appends(str, op); |
3012 | 0 | if (ast->child[0]) { |
3013 | 0 | smart_str_appendc(str, ' '); |
3014 | 0 | ast = ast->child[0]; |
3015 | 0 | goto tail_call; |
3016 | 0 | } |
3017 | 0 | return; |
3018 | | |
3019 | 0 | append_str: |
3020 | 0 | smart_str_appends(str, op); |
3021 | 0 | return; |
3022 | | |
3023 | 0 | append_default_value: |
3024 | 0 | if (ast->child[p]) { |
3025 | 0 | smart_str_appends(str, " = "); |
3026 | 0 | ast = ast->child[p]; |
3027 | 0 | goto tail_call; |
3028 | 0 | } |
3029 | 0 | return; |
3030 | 0 | } |
3031 | | |
3032 | | ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix) |
3033 | 1 | { |
3034 | 1 | smart_str str = {0}; |
3035 | | |
3036 | 1 | smart_str_appends(&str, prefix); |
3037 | 1 | zend_ast_export_ex(&str, ast, 0, 0); |
3038 | 1 | smart_str_appends(&str, suffix); |
3039 | 1 | smart_str_0(&str); |
3040 | 1 | return str.s; |
3041 | 1 | } |
3042 | | |
3043 | | zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr) |
3044 | 0 | { |
3045 | 0 | ZEND_ASSERT(attr->kind == ZEND_AST_ATTRIBUTE_LIST); |
3046 | |
|
3047 | 0 | switch (ast->kind) { |
3048 | 0 | case ZEND_AST_FUNC_DECL: |
3049 | 0 | case ZEND_AST_CLOSURE: |
3050 | 0 | case ZEND_AST_METHOD: |
3051 | 0 | case ZEND_AST_ARROW_FUNC: |
3052 | 0 | case ZEND_AST_PROPERTY_HOOK: |
3053 | 0 | ((zend_ast_decl *) ast)->child[4] = attr; |
3054 | 0 | break; |
3055 | 0 | case ZEND_AST_CLASS: |
3056 | 0 | ((zend_ast_decl *) ast)->child[3] = attr; |
3057 | 0 | break; |
3058 | 0 | case ZEND_AST_PROP_GROUP: |
3059 | 0 | ast->child[2] = attr; |
3060 | 0 | break; |
3061 | 0 | case ZEND_AST_PARAM: |
3062 | 0 | case ZEND_AST_ENUM_CASE: |
3063 | 0 | ast->child[3] = attr; |
3064 | 0 | break; |
3065 | 0 | case ZEND_AST_CLASS_CONST_GROUP: |
3066 | 0 | ast->child[1] = attr; |
3067 | 0 | break; |
3068 | 0 | case ZEND_AST_CONST_DECL: |
3069 | | /* Since constants are already stored in a list, just add the attributes |
3070 | | * to that list instead of storing them elsewhere; |
3071 | | * zend_compile_const_decl() checks the kind of the list elements. */ |
3072 | 0 | ast = zend_ast_list_add(ast, attr); |
3073 | 0 | break; |
3074 | 0 | EMPTY_SWITCH_DEFAULT_CASE() |
3075 | 0 | } |
3076 | | |
3077 | 0 | return ast; |
3078 | 0 | } |
3079 | | |
3080 | | zend_ast * ZEND_FASTCALL zend_ast_call_get_args(zend_ast *ast) |
3081 | 0 | { |
3082 | 0 | if (ast->kind == ZEND_AST_CALL) { |
3083 | 0 | return ast->child[1]; |
3084 | 0 | } else if (ast->kind == ZEND_AST_STATIC_CALL || ast->kind == ZEND_AST_METHOD_CALL) { |
3085 | 0 | return ast->child[2]; |
3086 | 0 | } |
3087 | | |
3088 | 0 | ZEND_UNREACHABLE(); |
3089 | 0 | return NULL; |
3090 | 0 | } |