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