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@zend.com> |
17 | Nikita Popov <nikic@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 /* $Id$ */
22
23 #ifndef ZEND_AST_H
24 #define ZEND_AST_H
25
26 #include "zend.h"
27
28 #define ZEND_AST_SPECIAL_SHIFT 6
29 #define ZEND_AST_IS_LIST_SHIFT 7
30 #define ZEND_AST_NUM_CHILDREN_SHIFT 8
31
32 enum _zend_ast_kind {
33 /* special nodes */
34 ZEND_AST_ZVAL = 1 << ZEND_AST_SPECIAL_SHIFT,
35 ZEND_AST_ZNODE,
36
37 /* declaration nodes */
38 ZEND_AST_FUNC_DECL,
39 ZEND_AST_CLOSURE,
40 ZEND_AST_METHOD,
41 ZEND_AST_CLASS,
42
43 /* list nodes */
44 ZEND_AST_ARG_LIST = 1 << ZEND_AST_IS_LIST_SHIFT,
45 ZEND_AST_ARRAY,
46 ZEND_AST_ENCAPS_LIST,
47 ZEND_AST_EXPR_LIST,
48 ZEND_AST_STMT_LIST,
49 ZEND_AST_IF,
50 ZEND_AST_SWITCH_LIST,
51 ZEND_AST_CATCH_LIST,
52 ZEND_AST_PARAM_LIST,
53 ZEND_AST_CLOSURE_USES,
54 ZEND_AST_PROP_DECL,
55 ZEND_AST_CONST_DECL,
56 ZEND_AST_CLASS_CONST_DECL,
57 ZEND_AST_NAME_LIST,
58 ZEND_AST_TRAIT_ADAPTATIONS,
59 ZEND_AST_USE,
60
61 /* 0 child nodes */
62 ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
63 ZEND_AST_TYPE,
64
65 /* 1 child node */
66 ZEND_AST_VAR = 1 << ZEND_AST_NUM_CHILDREN_SHIFT,
67 ZEND_AST_CONST,
68 ZEND_AST_UNPACK,
69 ZEND_AST_UNARY_PLUS,
70 ZEND_AST_UNARY_MINUS,
71 ZEND_AST_CAST,
72 ZEND_AST_EMPTY,
73 ZEND_AST_ISSET,
74 ZEND_AST_SILENCE,
75 ZEND_AST_SHELL_EXEC,
76 ZEND_AST_CLONE,
77 ZEND_AST_EXIT,
78 ZEND_AST_PRINT,
79 ZEND_AST_INCLUDE_OR_EVAL,
80 ZEND_AST_UNARY_OP,
81 ZEND_AST_PRE_INC,
82 ZEND_AST_PRE_DEC,
83 ZEND_AST_POST_INC,
84 ZEND_AST_POST_DEC,
85 ZEND_AST_YIELD_FROM,
86
87 ZEND_AST_GLOBAL,
88 ZEND_AST_UNSET,
89 ZEND_AST_RETURN,
90 ZEND_AST_LABEL,
91 ZEND_AST_REF,
92 ZEND_AST_HALT_COMPILER,
93 ZEND_AST_ECHO,
94 ZEND_AST_THROW,
95 ZEND_AST_GOTO,
96 ZEND_AST_BREAK,
97 ZEND_AST_CONTINUE,
98
99 /* 2 child nodes */
100 ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT,
101 ZEND_AST_PROP,
102 ZEND_AST_STATIC_PROP,
103 ZEND_AST_CALL,
104 ZEND_AST_CLASS_CONST,
105 ZEND_AST_ASSIGN,
106 ZEND_AST_ASSIGN_REF,
107 ZEND_AST_ASSIGN_OP,
108 ZEND_AST_BINARY_OP,
109 ZEND_AST_GREATER,
110 ZEND_AST_GREATER_EQUAL,
111 ZEND_AST_AND,
112 ZEND_AST_OR,
113 ZEND_AST_ARRAY_ELEM,
114 ZEND_AST_NEW,
115 ZEND_AST_INSTANCEOF,
116 ZEND_AST_YIELD,
117 ZEND_AST_COALESCE,
118
119 ZEND_AST_STATIC,
120 ZEND_AST_WHILE,
121 ZEND_AST_DO_WHILE,
122 ZEND_AST_IF_ELEM,
123 ZEND_AST_SWITCH,
124 ZEND_AST_SWITCH_CASE,
125 ZEND_AST_DECLARE,
126 ZEND_AST_USE_TRAIT,
127 ZEND_AST_TRAIT_PRECEDENCE,
128 ZEND_AST_METHOD_REFERENCE,
129 ZEND_AST_NAMESPACE,
130 ZEND_AST_USE_ELEM,
131 ZEND_AST_TRAIT_ALIAS,
132 ZEND_AST_GROUP_USE,
133
134 /* 3 child nodes */
135 ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
136 ZEND_AST_STATIC_CALL,
137 ZEND_AST_CONDITIONAL,
138
139 ZEND_AST_TRY,
140 ZEND_AST_CATCH,
141 ZEND_AST_PARAM,
142 ZEND_AST_PROP_ELEM,
143 ZEND_AST_CONST_ELEM,
144
145 /* 4 child nodes */
146 ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,
147 ZEND_AST_FOREACH,
148 };
149
150 typedef uint16_t zend_ast_kind;
151 typedef uint16_t zend_ast_attr;
152
153 struct _zend_ast {
154 zend_ast_kind kind; /* Type of the node (ZEND_AST_* enum constant) */
155 zend_ast_attr attr; /* Additional attribute, use depending on node type */
156 uint32_t lineno; /* Line number */
157 zend_ast *child[1]; /* Array of children (using struct hack) */
158 };
159
160 /* Same as zend_ast, but with children count, which is updated dynamically */
161 typedef struct _zend_ast_list {
162 zend_ast_kind kind;
163 zend_ast_attr attr;
164 uint32_t lineno;
165 uint32_t children;
166 zend_ast *child[1];
167 } zend_ast_list;
168
169 /* Lineno is stored in val.u2.lineno */
170 typedef struct _zend_ast_zval {
171 zend_ast_kind kind;
172 zend_ast_attr attr;
173 zval val;
174 } zend_ast_zval;
175
176 /* Separate structure for function and class declaration, as they need extra information. */
177 typedef struct _zend_ast_decl {
178 zend_ast_kind kind;
179 zend_ast_attr attr; /* Unused - for structure compatibility */
180 uint32_t start_lineno;
181 uint32_t end_lineno;
182 uint32_t flags;
183 unsigned char *lex_pos;
184 zend_string *doc_comment;
185 zend_string *name;
186 zend_ast *child[4];
187 } zend_ast_decl;
188
189 typedef void (*zend_ast_process_t)(zend_ast *ast);
190 extern ZEND_API zend_ast_process_t zend_ast_process;
191
192 ZEND_API zend_ast *zend_ast_create_zval_with_lineno(zval *zv, zend_ast_attr attr, uint32_t lineno);
193 ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr);
194
195 ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...);
196 ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...);
197
198 ZEND_API zend_ast *zend_ast_create_decl(
199 zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
200 zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
201 );
202
203 ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...);
204 ZEND_API zend_ast *zend_ast_list_add(zend_ast *list, zend_ast *op);
205
206 ZEND_API int zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope);
207 ZEND_API zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix);
208
209 ZEND_API zend_ast *zend_ast_copy(zend_ast *ast);
210 ZEND_API void zend_ast_destroy(zend_ast *ast);
211 ZEND_API void zend_ast_destroy_and_free(zend_ast *ast);
212
213 typedef void (*zend_ast_apply_func)(zend_ast **ast_ptr);
214 ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn);
215
zend_ast_is_list(zend_ast * ast)216 static zend_always_inline zend_bool zend_ast_is_list(zend_ast *ast) {
217 return (ast->kind >> ZEND_AST_IS_LIST_SHIFT) & 1;
218 }
zend_ast_get_list(zend_ast * ast)219 static zend_always_inline zend_ast_list *zend_ast_get_list(zend_ast *ast) {
220 ZEND_ASSERT(zend_ast_is_list(ast));
221 return (zend_ast_list *) ast;
222 }
223
zend_ast_get_zval(zend_ast * ast)224 static zend_always_inline zval *zend_ast_get_zval(zend_ast *ast) {
225 ZEND_ASSERT(ast->kind == ZEND_AST_ZVAL);
226 return &((zend_ast_zval *) ast)->val;
227 }
zend_ast_get_str(zend_ast * ast)228 static zend_always_inline zend_string *zend_ast_get_str(zend_ast *ast) {
229 zval *zv = zend_ast_get_zval(ast);
230 ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
231 return Z_STR_P(zv);
232 }
233
zend_ast_get_num_children(zend_ast * ast)234 static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
235 ZEND_ASSERT(!zend_ast_is_list(ast));
236 return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
237 }
zend_ast_get_lineno(zend_ast * ast)238 static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
239 if (ast->kind == ZEND_AST_ZVAL) {
240 zval *zv = zend_ast_get_zval(ast);
241 return zv->u2.lineno;
242 } else {
243 return ast->lineno;
244 }
245 }
246
zend_ast_create_zval(zval * zv)247 static zend_always_inline zend_ast *zend_ast_create_zval(zval *zv) {
248 return zend_ast_create_zval_ex(zv, 0);
249 }
zend_ast_create_zval_from_str(zend_string * str)250 static zend_always_inline zend_ast *zend_ast_create_zval_from_str(zend_string *str) {
251 zval zv;
252 ZVAL_STR(&zv, str);
253 return zend_ast_create_zval(&zv);
254 }
zend_ast_create_zval_from_long(zend_long lval)255 static zend_always_inline zend_ast *zend_ast_create_zval_from_long(zend_long lval) {
256 zval zv;
257 ZVAL_LONG(&zv, lval);
258 return zend_ast_create_zval(&zv);
259 }
260
zend_ast_create_binary_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)261 static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
262 return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
263 }
zend_ast_create_assign_op(uint32_t opcode,zend_ast * op0,zend_ast * op1)264 static zend_always_inline zend_ast *zend_ast_create_assign_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
265 return zend_ast_create_ex(ZEND_AST_ASSIGN_OP, opcode, op0, op1);
266 }
zend_ast_create_cast(uint32_t type,zend_ast * op0)267 static zend_always_inline zend_ast *zend_ast_create_cast(uint32_t type, zend_ast *op0) {
268 return zend_ast_create_ex(ZEND_AST_CAST, type, op0);
269 }
zend_ast_list_rtrim(zend_ast * ast)270 static zend_always_inline zend_ast *zend_ast_list_rtrim(zend_ast *ast) {
271 zend_ast_list *list = zend_ast_get_list(ast);
272 if (list->children && list->child[list->children - 1] == NULL) {
273 list->children--;
274 }
275 return ast;
276 }
277 #endif
278