You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.4 KiB

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