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