zend_ast.h 11 KB

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