1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 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
46 /* list nodes */
47 ZEND_AST_ARG_LIST = 1 << ZEND_AST_IS_LIST_SHIFT,
48 ZEND_AST_ARRAY,
49 ZEND_AST_ENCAPS_LIST,
50 ZEND_AST_EXPR_LIST,
51 ZEND_AST_STMT_LIST,
52 ZEND_AST_IF,
53 ZEND_AST_SWITCH_LIST,
54 ZEND_AST_CATCH_LIST,
55 ZEND_AST_PARAM_LIST,
56 ZEND_AST_CLOSURE_USES,
57 ZEND_AST_PROP_DECL,
58 ZEND_AST_CONST_DECL,
59 ZEND_AST_CLASS_CONST_DECL,
60 ZEND_AST_NAME_LIST,
61 ZEND_AST_TRAIT_ADAPTATIONS,
62 ZEND_AST_USE,
63
64 /* 0 child nodes */
65 ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
66 ZEND_AST_TYPE,
67 ZEND_AST_CONSTANT_CLASS,
68
69 /* 1 child node */
70 ZEND_AST_VAR = 1 << ZEND_AST_NUM_CHILDREN_SHIFT,
71 ZEND_AST_CONST,
72 ZEND_AST_UNPACK,
73 ZEND_AST_UNARY_PLUS,
74 ZEND_AST_UNARY_MINUS,
75 ZEND_AST_CAST,
76 ZEND_AST_EMPTY,
77 ZEND_AST_ISSET,
78 ZEND_AST_SILENCE,
79 ZEND_AST_SHELL_EXEC,
80 ZEND_AST_CLONE,
81 ZEND_AST_EXIT,
82 ZEND_AST_PRINT,
83 ZEND_AST_INCLUDE_OR_EVAL,
84 ZEND_AST_UNARY_OP,
85 ZEND_AST_PRE_INC,
86 ZEND_AST_PRE_DEC,
87 ZEND_AST_POST_INC,
88 ZEND_AST_POST_DEC,
89 ZEND_AST_YIELD_FROM,
90
91 ZEND_AST_GLOBAL,
92 ZEND_AST_UNSET,
93 ZEND_AST_RETURN,
94 ZEND_AST_LABEL,
95 ZEND_AST_REF,
96 ZEND_AST_HALT_COMPILER,
97 ZEND_AST_ECHO,
98 ZEND_AST_THROW,
99 ZEND_AST_GOTO,
100 ZEND_AST_BREAK,
101 ZEND_AST_CONTINUE,
102
103 /* 2 child nodes */
104 ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT,
105 ZEND_AST_PROP,
106 ZEND_AST_STATIC_PROP,
107 ZEND_AST_CALL,
108 ZEND_AST_CLASS_CONST,
109 ZEND_AST_ASSIGN,
110 ZEND_AST_ASSIGN_REF,
111 ZEND_AST_ASSIGN_OP,
112 ZEND_AST_BINARY_OP,
113 ZEND_AST_GREATER,
114 ZEND_AST_GREATER_EQUAL,
115 ZEND_AST_AND,
116 ZEND_AST_OR,
117 ZEND_AST_ARRAY_ELEM,
118 ZEND_AST_NEW,
119 ZEND_AST_INSTANCEOF,
120 ZEND_AST_YIELD,
121 ZEND_AST_COALESCE,
122
123 ZEND_AST_STATIC,
124 ZEND_AST_WHILE,
125 ZEND_AST_DO_WHILE,
126 ZEND_AST_IF_ELEM,
127 ZEND_AST_SWITCH,
128 ZEND_AST_SWITCH_CASE,
129 ZEND_AST_DECLARE,
130 ZEND_AST_USE_TRAIT,
131 ZEND_AST_TRAIT_PRECEDENCE,
132 ZEND_AST_METHOD_REFERENCE,
133 ZEND_AST_NAMESPACE,
134 ZEND_AST_USE_ELEM,
135 ZEND_AST_TRAIT_ALIAS,
136 ZEND_AST_GROUP_USE,
137
138 /* 3 child nodes */
139 ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
140 ZEND_AST_STATIC_CALL,
141 ZEND_AST_CONDITIONAL,
142
143 ZEND_AST_TRY,
144 ZEND_AST_CATCH,
145 ZEND_AST_PARAM,
146 ZEND_AST_PROP_ELEM,
147 ZEND_AST_CONST_ELEM,
148
149 /* 4 child nodes */
150 ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,
151 ZEND_AST_FOREACH,
152 };
153
154 typedef uint16_t zend_ast_kind;
155 typedef uint16_t zend_ast_attr;
156
157 struct _zend_ast {
158 zend_ast_kind kind; /* Type of the node (ZEND_AST_* enum constant) */
159 zend_ast_attr attr; /* Additional attribute, use depending on node type */
160 uint32_t lineno; /* Line number */
161 zend_ast *child[1]; /* Array of children (using struct hack) */
162 };
163
164 /* Same as zend_ast, but with children count, which is updated dynamically */
165 typedef struct _zend_ast_list {
166 zend_ast_kind kind;
167 zend_ast_attr attr;
168 uint32_t lineno;
169 uint32_t children;
170 zend_ast *child[1];
171 } zend_ast_list;
172
173 /* Lineno is stored in val.u2.lineno */
174 typedef struct _zend_ast_zval {
175 zend_ast_kind kind;
176 zend_ast_attr attr;
177 zval val;
178 } zend_ast_zval;
179
180 /* Separate structure for function and class declaration, as they need extra information. */
181 typedef struct _zend_ast_decl {
182 zend_ast_kind kind;
183 zend_ast_attr attr; /* Unused - for structure compatibility */
184 uint32_t start_lineno;
185 uint32_t end_lineno;
186 uint32_t flags;
187 unsigned char *lex_pos;
188 zend_string *doc_comment;
189 zend_string *name;
190 zend_ast *child[4];
191 } zend_ast_decl;
192
193 typedef void (*zend_ast_process_t)(zend_ast *ast);
194 extern ZEND_API zend_ast_process_t zend_ast_process;
195
196 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(zval *zv, uint32_t lineno);
197 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr);
198 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(zval *zv);
199 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str);
200 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval);
201
202 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr);
203
204 #if ZEND_AST_SPEC
205 # define ZEND_AST_SPEC_CALL(name, ...) \
206 ZEND_EXPAND_VA(ZEND_AST_SPEC_CALL_(name, __VA_ARGS__, _4, _3, _2, _1, _0)(__VA_ARGS__))
207 # define ZEND_AST_SPEC_CALL_(name, _, _4, _3, _2, _1, suffix, ...) \
208 name ## suffix
209 # define ZEND_AST_SPEC_CALL_EX(name, ...) \
210 ZEND_EXPAND_VA(ZEND_AST_SPEC_CALL_EX_(name, __VA_ARGS__, _4, _3, _2, _1, _0)(__VA_ARGS__))
211 # define ZEND_AST_SPEC_CALL_EX_(name, _, _5, _4, _3, _2, _1, suffix, ...) \
212 name ## suffix
213
214 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind);
215 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child);
216 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2);
217 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3);
218 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);
219
zend_ast_create_ex_0(zend_ast_kind kind,zend_ast_attr attr)220 static zend_always_inline zend_ast * zend_ast_create_ex_0(zend_ast_kind kind, zend_ast_attr attr) {
221 zend_ast *ast = zend_ast_create_0(kind);
222 ast->attr = attr;
223 return ast;
224 }
zend_ast_create_ex_1(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child)225 static zend_always_inline zend_ast * zend_ast_create_ex_1(zend_ast_kind kind, zend_ast_attr attr, zend_ast *child) {
226 zend_ast *ast = zend_ast_create_1(kind, child);
227 ast->attr = attr;
228 return ast;
229 }
zend_ast_create_ex_2(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2)230 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) {
231 zend_ast *ast = zend_ast_create_2(kind, child1, child2);
232 ast->attr = attr;
233 return ast;
234 }
zend_ast_create_ex_3(zend_ast_kind kind,zend_ast_attr attr,zend_ast * child1,zend_ast * child2,zend_ast * child3)235 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) {
236 zend_ast *ast = zend_ast_create_3(kind, child1, child2, child3);
237 ast->attr = attr;
238 return ast;
239 }
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)240 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) {
241 zend_ast *ast = zend_ast_create_4(kind, child1, child2, child3, child4);
242 ast->attr = attr;
243 return ast;
244 }
245
246 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind);
247 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child);
248 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2);
249
250 # define zend_ast_create(...) \
251 ZEND_AST_SPEC_CALL(zend_ast_create, __VA_ARGS__)
252 # define zend_ast_create_ex(...) \
253 ZEND_AST_SPEC_CALL_EX(zend_ast_create_ex, __VA_ARGS__)
254 # define zend_ast_create_list(init_children, ...) \
255 ZEND_AST_SPEC_CALL(zend_ast_create_list, __VA_ARGS__)
256
257 #else
258 ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...);
259 ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...);
260 ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...);
261 #endif
262
263 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *list, zend_ast *op);
264
265 ZEND_API zend_ast *zend_ast_create_decl(
266 zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
267 zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
268 );
269
270 ZEND_API int ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope);
271 ZEND_API zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix);
272
273 ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast);
274 ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast);
275 ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast);
276
277 typedef void (*zend_ast_apply_func)(zend_ast **ast_ptr);
278 ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn);
279
zend_ast_is_list(zend_ast * ast)280 static zend_always_inline zend_bool zend_ast_is_list(zend_ast *ast) {
281 return (ast->kind >> ZEND_AST_IS_LIST_SHIFT) & 1;
282 }
zend_ast_get_list(zend_ast * ast)283 static zend_always_inline zend_ast_list *zend_ast_get_list(zend_ast *ast) {
284 ZEND_ASSERT(zend_ast_is_list(ast));
285 return (zend_ast_list *) ast;
286 }
287
zend_ast_get_zval(zend_ast * ast)288 static zend_always_inline zval *zend_ast_get_zval(zend_ast *ast) {
289 ZEND_ASSERT(ast->kind == ZEND_AST_ZVAL);
290 return &((zend_ast_zval *) ast)->val;
291 }
zend_ast_get_str(zend_ast * ast)292 static zend_always_inline zend_string *zend_ast_get_str(zend_ast *ast) {
293 zval *zv = zend_ast_get_zval(ast);
294 ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
295 return Z_STR_P(zv);
296 }
297
zend_ast_get_constant_name(zend_ast * ast)298 static zend_always_inline zend_string *zend_ast_get_constant_name(zend_ast *ast) {
299 ZEND_ASSERT(ast->kind == ZEND_AST_CONSTANT);
300 ZEND_ASSERT(Z_TYPE(((zend_ast_zval *) ast)->val) == IS_STRING);
301 return Z_STR(((zend_ast_zval *) ast)->val);
302 }
303
zend_ast_get_num_children(zend_ast * ast)304 static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
305 ZEND_ASSERT(!zend_ast_is_list(ast));
306 return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
307 }
zend_ast_get_lineno(zend_ast * ast)308 static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
309 if (ast->kind == ZEND_AST_ZVAL) {
310 zval *zv = zend_ast_get_zval(ast);
311 return Z_LINENO_P(zv);
312 } else {
313 return ast->lineno;
314 }
315 }
316
zend_ast_create_binary_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)317 static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
318 return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
319 }
zend_ast_create_assign_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)320 static zend_always_inline zend_ast *zend_ast_create_assign_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
321 return zend_ast_create_ex(ZEND_AST_ASSIGN_OP, opcode, op0, op1);
322 }
zend_ast_create_cast(uint32_t type,zend_ast * op0)323 static zend_always_inline zend_ast *zend_ast_create_cast(uint32_t type, zend_ast *op0) {
324 return zend_ast_create_ex(ZEND_AST_CAST, type, op0);
325 }
zend_ast_list_rtrim(zend_ast * ast)326 static zend_always_inline zend_ast *zend_ast_list_rtrim(zend_ast *ast) {
327 zend_ast_list *list = zend_ast_get_list(ast);
328 if (list->children && list->child[list->children - 1] == NULL) {
329 list->children--;
330 }
331 return ast;
332 }
333 #endif
334
335 /*
336 * Local variables:
337 * tab-width: 4
338 * c-basic-offset: 4
339 * indent-tabs-mode: t
340 * End:
341 * vim600: sw=4 ts=4 fdm=marker
342 * vim<600: sw=4 ts=4
343 */
344