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 | Nikita Popov <nikic@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #ifndef ZEND_AST_H
22 #define ZEND_AST_H
23
24 #include "zend.h"
25
26 #ifndef ZEND_AST_SPEC
27 # define ZEND_AST_SPEC 1
28 #endif
29
30 #define ZEND_AST_SPECIAL_SHIFT 6
31 #define ZEND_AST_IS_LIST_SHIFT 7
32 #define ZEND_AST_NUM_CHILDREN_SHIFT 8
33
34 enum _zend_ast_kind {
35 /* special nodes */
36 ZEND_AST_ZVAL = 1 << ZEND_AST_SPECIAL_SHIFT,
37 ZEND_AST_CONSTANT,
38 ZEND_AST_ZNODE,
39
40 /* declaration nodes */
41 ZEND_AST_FUNC_DECL,
42 ZEND_AST_CLOSURE,
43 ZEND_AST_METHOD,
44 ZEND_AST_CLASS,
45 ZEND_AST_ARROW_FUNC,
46
47 /* list nodes */
48 ZEND_AST_ARG_LIST = 1 << ZEND_AST_IS_LIST_SHIFT,
49 ZEND_AST_ARRAY,
50 ZEND_AST_ENCAPS_LIST,
51 ZEND_AST_EXPR_LIST,
52 ZEND_AST_STMT_LIST,
53 ZEND_AST_IF,
54 ZEND_AST_SWITCH_LIST,
55 ZEND_AST_CATCH_LIST,
56 ZEND_AST_PARAM_LIST,
57 ZEND_AST_CLOSURE_USES,
58 ZEND_AST_PROP_DECL,
59 ZEND_AST_CONST_DECL,
60 ZEND_AST_CLASS_CONST_DECL,
61 ZEND_AST_NAME_LIST,
62 ZEND_AST_TRAIT_ADAPTATIONS,
63 ZEND_AST_USE,
64 ZEND_AST_TYPE_UNION,
65 ZEND_AST_TYPE_INTERSECTION,
66 ZEND_AST_ATTRIBUTE_LIST,
67 ZEND_AST_ATTRIBUTE_GROUP,
68 ZEND_AST_MATCH_ARM_LIST,
69 ZEND_AST_MODIFIER_LIST,
70
71 /* 0 child nodes */
72 ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
73 ZEND_AST_TYPE,
74 ZEND_AST_CONSTANT_CLASS,
75 ZEND_AST_CALLABLE_CONVERT,
76
77 /* 1 child node */
78 ZEND_AST_VAR = 1 << ZEND_AST_NUM_CHILDREN_SHIFT,
79 ZEND_AST_CONST,
80 ZEND_AST_UNPACK,
81 ZEND_AST_UNARY_PLUS,
82 ZEND_AST_UNARY_MINUS,
83 ZEND_AST_CAST,
84 ZEND_AST_EMPTY,
85 ZEND_AST_ISSET,
86 ZEND_AST_SILENCE,
87 ZEND_AST_SHELL_EXEC,
88 ZEND_AST_CLONE,
89 ZEND_AST_EXIT,
90 ZEND_AST_PRINT,
91 ZEND_AST_INCLUDE_OR_EVAL,
92 ZEND_AST_UNARY_OP,
93 ZEND_AST_PRE_INC,
94 ZEND_AST_PRE_DEC,
95 ZEND_AST_POST_INC,
96 ZEND_AST_POST_DEC,
97 ZEND_AST_YIELD_FROM,
98 ZEND_AST_CLASS_NAME,
99
100 ZEND_AST_GLOBAL,
101 ZEND_AST_UNSET,
102 ZEND_AST_RETURN,
103 ZEND_AST_LABEL,
104 ZEND_AST_REF,
105 ZEND_AST_HALT_COMPILER,
106 ZEND_AST_ECHO,
107 ZEND_AST_THROW,
108 ZEND_AST_GOTO,
109 ZEND_AST_BREAK,
110 ZEND_AST_CONTINUE,
111
112 /* 2 child nodes */
113 ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT,
114 ZEND_AST_PROP,
115 ZEND_AST_NULLSAFE_PROP,
116 ZEND_AST_STATIC_PROP,
117 ZEND_AST_CALL,
118 ZEND_AST_CLASS_CONST,
119 ZEND_AST_ASSIGN,
120 ZEND_AST_ASSIGN_REF,
121 ZEND_AST_ASSIGN_OP,
122 ZEND_AST_BINARY_OP,
123 ZEND_AST_GREATER,
124 ZEND_AST_GREATER_EQUAL,
125 ZEND_AST_AND,
126 ZEND_AST_OR,
127 ZEND_AST_ARRAY_ELEM,
128 ZEND_AST_NEW,
129 ZEND_AST_INSTANCEOF,
130 ZEND_AST_YIELD,
131 ZEND_AST_COALESCE,
132 ZEND_AST_ASSIGN_COALESCE,
133
134 ZEND_AST_STATIC,
135 ZEND_AST_WHILE,
136 ZEND_AST_DO_WHILE,
137 ZEND_AST_IF_ELEM,
138 ZEND_AST_SWITCH,
139 ZEND_AST_SWITCH_CASE,
140 ZEND_AST_DECLARE,
141 ZEND_AST_USE_TRAIT,
142 ZEND_AST_TRAIT_PRECEDENCE,
143 ZEND_AST_METHOD_REFERENCE,
144 ZEND_AST_NAMESPACE,
145 ZEND_AST_USE_ELEM,
146 ZEND_AST_TRAIT_ALIAS,
147 ZEND_AST_GROUP_USE,
148 ZEND_AST_ATTRIBUTE,
149 ZEND_AST_MATCH,
150 ZEND_AST_MATCH_ARM,
151 ZEND_AST_NAMED_ARG,
152
153 /* 3 child nodes */
154 ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
155 ZEND_AST_NULLSAFE_METHOD_CALL,
156 ZEND_AST_STATIC_CALL,
157 ZEND_AST_CONDITIONAL,
158
159 ZEND_AST_TRY,
160 ZEND_AST_CATCH,
161 ZEND_AST_PROP_GROUP,
162 ZEND_AST_PROP_ELEM,
163 ZEND_AST_CONST_ELEM,
164 ZEND_AST_CLASS_CONST_GROUP,
165
166 // Pseudo node for initializing enums
167 ZEND_AST_CONST_ENUM_INIT,
168
169 /* 4 child nodes */
170 ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,
171 ZEND_AST_FOREACH,
172 ZEND_AST_ENUM_CASE,
173
174 /* 5 child nodes */
175 ZEND_AST_PARAM = 5 << ZEND_AST_NUM_CHILDREN_SHIFT,
176 };
177
178 typedef uint16_t zend_ast_kind;
179 typedef uint16_t zend_ast_attr;
180
181 struct _zend_ast {
182 zend_ast_kind kind; /* Type of the node (ZEND_AST_* enum constant) */
183 zend_ast_attr attr; /* Additional attribute, use depending on node type */
184 uint32_t lineno; /* Line number */
185 zend_ast *child[1]; /* Array of children (using struct hack) */
186 };
187
188 /* Same as zend_ast, but with children count, which is updated dynamically */
189 typedef struct _zend_ast_list {
190 zend_ast_kind kind;
191 zend_ast_attr attr;
192 uint32_t lineno;
193 uint32_t children;
194 zend_ast *child[1];
195 } zend_ast_list;
196
197 /* Lineno is stored in val.u2.lineno */
198 typedef struct _zend_ast_zval {
199 zend_ast_kind kind;
200 zend_ast_attr attr;
201 zval val;
202 } zend_ast_zval;
203
204 /* Separate structure for function and class declaration, as they need extra information. */
205 typedef struct _zend_ast_decl {
206 zend_ast_kind kind;
207 zend_ast_attr attr; /* Unused - for structure compatibility */
208 uint32_t start_lineno;
209 uint32_t end_lineno;
210 uint32_t flags;
211 zend_string *doc_comment;
212 zend_string *name;
213 zend_ast *child[5];
214 } zend_ast_decl;
215
216 typedef void (*zend_ast_process_t)(zend_ast *ast);
217 extern ZEND_API zend_ast_process_t zend_ast_process;
218
219 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(zval *zv, uint32_t lineno);
220 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr);
221 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(zval *zv);
222 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str);
223 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval);
224
225 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr);
226 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *class_name, zend_ast *name);
227
228 #if ZEND_AST_SPEC
229 # define ZEND_AST_SPEC_CALL(name, ...) \
230 ZEND_EXPAND_VA(ZEND_AST_SPEC_CALL_(name, __VA_ARGS__, _5, _4, _3, _2, _1, _0)(__VA_ARGS__))
231 # define ZEND_AST_SPEC_CALL_(name, _, _5, _4, _3, _2, _1, suffix, ...) \
232 name ## suffix
233 # define ZEND_AST_SPEC_CALL_EX(name, ...) \
234 ZEND_EXPAND_VA(ZEND_AST_SPEC_CALL_EX_(name, __VA_ARGS__, _5, _4, _3, _2, _1, _0)(__VA_ARGS__))
235 # define ZEND_AST_SPEC_CALL_EX_(name, _, _6, _5, _4, _3, _2, _1, suffix, ...) \
236 name ## suffix
237
238 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind);
239 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child);
240 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2);
241 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3);
242 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);
243 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);
244
zend_ast_create_ex_0(zend_ast_kind kind,zend_ast_attr attr)245 static zend_always_inline zend_ast * zend_ast_create_ex_0(zend_ast_kind kind, zend_ast_attr attr) {
246 zend_ast *ast = zend_ast_create_0(kind);
247 ast->attr = attr;
248 return ast;
249 }
zend_ast_create_ex_1(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child)250 static zend_always_inline zend_ast * zend_ast_create_ex_1(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child) {
251 zend_ast *ast = zend_ast_create_1(kind, child);
252 ast->attr = attr;
253 return ast;
254 }
zend_ast_create_ex_2(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2)255 static zend_always_inline zend_ast * zend_ast_create_ex_2(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child1, zend_ast *child2) {
256 zend_ast *ast = zend_ast_create_2(kind, child1, child2);
257 ast->attr = attr;
258 return ast;
259 }
zend_ast_create_ex_3(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2,zend_ast * child3)260 static zend_always_inline zend_ast * zend_ast_create_ex_3(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child1, zend_ast *child2, zend_ast *child3) {
261 zend_ast *ast = zend_ast_create_3(kind, child1, child2, child3);
262 ast->attr = attr;
263 return ast;
264 }
zend_ast_create_ex_4(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2,zend_ast * child3,zend_ast * child4)265 static zend_always_inline zend_ast * zend_ast_create_ex_4(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4) {
266 zend_ast *ast = zend_ast_create_4(kind, child1, child2, child3, child4);
267 ast->attr = attr;
268 return ast;
269 }
zend_ast_create_ex_5(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2,zend_ast * child3,zend_ast * child4,zend_ast * child5)270 static zend_always_inline zend_ast * zend_ast_create_ex_5(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4, zend_ast *child5) {
271 zend_ast *ast = zend_ast_create_5(kind, child1, child2, child3, child4, child5);
272 ast->attr = attr;
273 return ast;
274 }
275
276 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind);
277 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child);
278 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2);
279
280 # define zend_ast_create(...) \
281 ZEND_AST_SPEC_CALL(zend_ast_create, __VA_ARGS__)
282 # define zend_ast_create_ex(...) \
283 ZEND_AST_SPEC_CALL_EX(zend_ast_create_ex, __VA_ARGS__)
284 # define zend_ast_create_list(init_children, ...) \
285 ZEND_AST_SPEC_CALL(zend_ast_create_list, __VA_ARGS__)
286
287 #else
288 ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...);
289 ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...);
290 ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...);
291 #endif
292
293 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *list, zend_ast *op);
294
295 ZEND_API zend_ast *zend_ast_create_decl(
296 zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
297 zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
298 );
299
300 typedef struct {
301 bool had_side_effects;
302 } zend_ast_evaluate_ctx;
303
304 ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope);
305 ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_ex(zval *result, zend_ast *ast, zend_class_entry *scope, bool *short_circuited_ptr, zend_ast_evaluate_ctx *ctx);
306 ZEND_API zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix);
307
308 ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast);
309 ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast);
310 ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast);
311
312 typedef void (*zend_ast_apply_func)(zend_ast **ast_ptr, void *context);
313 ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn, void *context);
314
zend_ast_size(uint32_t children)315 static zend_always_inline size_t zend_ast_size(uint32_t children) {
316 return XtOffsetOf(zend_ast, child) + (sizeof(zend_ast *) * children);
317 }
318
zend_ast_is_special(zend_ast * ast)319 static zend_always_inline bool zend_ast_is_special(zend_ast *ast) {
320 return (ast->kind >> ZEND_AST_SPECIAL_SHIFT) & 1;
321 }
322
zend_ast_is_list(zend_ast * ast)323 static zend_always_inline bool zend_ast_is_list(zend_ast *ast) {
324 return (ast->kind >> ZEND_AST_IS_LIST_SHIFT) & 1;
325 }
zend_ast_get_list(zend_ast * ast)326 static zend_always_inline zend_ast_list *zend_ast_get_list(zend_ast *ast) {
327 ZEND_ASSERT(zend_ast_is_list(ast));
328 return (zend_ast_list *) ast;
329 }
330
zend_ast_get_zval(zend_ast * ast)331 static zend_always_inline zval *zend_ast_get_zval(zend_ast *ast) {
332 ZEND_ASSERT(ast->kind == ZEND_AST_ZVAL);
333 return &((zend_ast_zval *) ast)->val;
334 }
zend_ast_get_str(zend_ast * ast)335 static zend_always_inline zend_string *zend_ast_get_str(zend_ast *ast) {
336 zval *zv = zend_ast_get_zval(ast);
337 ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
338 return Z_STR_P(zv);
339 }
340
zend_ast_get_constant_name(zend_ast * ast)341 static zend_always_inline zend_string *zend_ast_get_constant_name(zend_ast *ast) {
342 ZEND_ASSERT(ast->kind == ZEND_AST_CONSTANT);
343 ZEND_ASSERT(Z_TYPE(((zend_ast_zval *) ast)->val) == IS_STRING);
344 return Z_STR(((zend_ast_zval *) ast)->val);
345 }
346
zend_ast_get_num_children(zend_ast * ast)347 static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
348 ZEND_ASSERT(!zend_ast_is_list(ast));
349 return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
350 }
zend_ast_get_lineno(zend_ast * ast)351 static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
352 if (ast->kind == ZEND_AST_ZVAL) {
353 zval *zv = zend_ast_get_zval(ast);
354 return Z_LINENO_P(zv);
355 } else if (ast->kind == ZEND_AST_CONSTANT) {
356 zval *zv = &((zend_ast_zval *) ast)->val;
357 return Z_LINENO_P(zv);
358 } else {
359 return ast->lineno;
360 }
361 }
362
zend_ast_create_binary_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)363 static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
364 return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
365 }
366
367 zend_ast *zend_ast_create_concat_op(zend_ast *op0, zend_ast *op1);
368
zend_ast_create_assign_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)369 static zend_always_inline zend_ast *zend_ast_create_assign_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
370 return zend_ast_create_ex(ZEND_AST_ASSIGN_OP, opcode, op0, op1);
371 }
zend_ast_create_cast(uint32_t type,zend_ast * op0)372 static zend_always_inline zend_ast *zend_ast_create_cast(uint32_t type, zend_ast *op0) {
373 return zend_ast_create_ex(ZEND_AST_CAST, type, op0);
374 }
zend_ast_list_rtrim(zend_ast * ast)375 static zend_always_inline zend_ast *zend_ast_list_rtrim(zend_ast *ast) {
376 zend_ast_list *list = zend_ast_get_list(ast);
377 if (list->children && list->child[list->children - 1] == NULL) {
378 list->children--;
379 }
380 return ast;
381 }
382
383 zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr);
384
385 #endif
386