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