2013-10-31 15:57:12 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2016-01-02 17:56:11 +08:00
|
|
|
| Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
|
2013-10-31 15:57:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Bob Weinand <bwoebi@php.net> |
|
2013-11-07 02:21:07 +08:00
|
|
|
| Dmitry Stogov <dmitry@zend.com> |
|
2014-07-27 19:25:32 +08:00
|
|
|
| Nikita Popov <nikic@php.net> |
|
2013-10-31 15:57:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifndef ZEND_AST_H
|
|
|
|
#define ZEND_AST_H
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
|
2014-07-28 20:51:08 +08:00
|
|
|
#define ZEND_AST_SPECIAL_SHIFT 6
|
|
|
|
#define ZEND_AST_IS_LIST_SHIFT 7
|
|
|
|
#define ZEND_AST_NUM_CHILDREN_SHIFT 8
|
2014-07-28 04:26:06 +08:00
|
|
|
|
2014-06-19 19:44:17 +08:00
|
|
|
enum _zend_ast_kind {
|
2014-07-28 20:51:08 +08:00
|
|
|
/* special nodes */
|
|
|
|
ZEND_AST_ZVAL = 1 << ZEND_AST_SPECIAL_SHIFT,
|
2014-06-07 19:06:53 +08:00
|
|
|
ZEND_AST_ZNODE,
|
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
/* declaration nodes */
|
|
|
|
ZEND_AST_FUNC_DECL,
|
|
|
|
ZEND_AST_CLOSURE,
|
|
|
|
ZEND_AST_METHOD,
|
|
|
|
ZEND_AST_CLASS,
|
2014-06-07 19:06:53 +08:00
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
/* list nodes */
|
|
|
|
ZEND_AST_ARG_LIST = 1 << ZEND_AST_IS_LIST_SHIFT,
|
|
|
|
ZEND_AST_LIST,
|
|
|
|
ZEND_AST_ARRAY,
|
|
|
|
ZEND_AST_ENCAPS_LIST,
|
|
|
|
ZEND_AST_EXPR_LIST,
|
|
|
|
ZEND_AST_STMT_LIST,
|
|
|
|
ZEND_AST_IF,
|
|
|
|
ZEND_AST_SWITCH_LIST,
|
|
|
|
ZEND_AST_CATCH_LIST,
|
|
|
|
ZEND_AST_PARAM_LIST,
|
|
|
|
ZEND_AST_CLOSURE_USES,
|
|
|
|
ZEND_AST_PROP_DECL,
|
|
|
|
ZEND_AST_CONST_DECL,
|
|
|
|
ZEND_AST_CLASS_CONST_DECL,
|
|
|
|
ZEND_AST_NAME_LIST,
|
|
|
|
ZEND_AST_TRAIT_ADAPTATIONS,
|
|
|
|
ZEND_AST_USE,
|
|
|
|
|
|
|
|
/* 0 child nodes */
|
|
|
|
ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
|
|
|
|
ZEND_AST_TYPE,
|
2014-07-08 03:06:02 +08:00
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
/* 1 child node */
|
|
|
|
ZEND_AST_VAR = 1 << ZEND_AST_NUM_CHILDREN_SHIFT,
|
2014-07-08 03:06:02 +08:00
|
|
|
ZEND_AST_CONST,
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_UNPACK,
|
2014-07-08 03:06:02 +08:00
|
|
|
ZEND_AST_UNARY_PLUS,
|
|
|
|
ZEND_AST_UNARY_MINUS,
|
|
|
|
ZEND_AST_CAST,
|
|
|
|
ZEND_AST_EMPTY,
|
|
|
|
ZEND_AST_ISSET,
|
|
|
|
ZEND_AST_SILENCE,
|
|
|
|
ZEND_AST_SHELL_EXEC,
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_CLONE,
|
|
|
|
ZEND_AST_EXIT,
|
|
|
|
ZEND_AST_PRINT,
|
|
|
|
ZEND_AST_INCLUDE_OR_EVAL,
|
|
|
|
ZEND_AST_UNARY_OP,
|
|
|
|
ZEND_AST_PRE_INC,
|
|
|
|
ZEND_AST_PRE_DEC,
|
|
|
|
ZEND_AST_POST_INC,
|
|
|
|
ZEND_AST_POST_DEC,
|
2015-02-20 19:59:56 +08:00
|
|
|
ZEND_AST_YIELD_FROM,
|
2014-06-15 00:30:18 +08:00
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_GLOBAL,
|
|
|
|
ZEND_AST_UNSET,
|
|
|
|
ZEND_AST_RETURN,
|
|
|
|
ZEND_AST_LABEL,
|
|
|
|
ZEND_AST_REF,
|
|
|
|
ZEND_AST_HALT_COMPILER,
|
|
|
|
ZEND_AST_ECHO,
|
|
|
|
ZEND_AST_THROW,
|
|
|
|
ZEND_AST_GOTO,
|
|
|
|
ZEND_AST_BREAK,
|
|
|
|
ZEND_AST_CONTINUE,
|
|
|
|
|
|
|
|
/* 2 child nodes */
|
|
|
|
ZEND_AST_DIM = 2 << ZEND_AST_NUM_CHILDREN_SHIFT,
|
|
|
|
ZEND_AST_PROP,
|
|
|
|
ZEND_AST_STATIC_PROP,
|
|
|
|
ZEND_AST_CALL,
|
|
|
|
ZEND_AST_CLASS_CONST,
|
|
|
|
ZEND_AST_ASSIGN,
|
|
|
|
ZEND_AST_ASSIGN_REF,
|
|
|
|
ZEND_AST_ASSIGN_OP,
|
2014-06-26 18:43:20 +08:00
|
|
|
ZEND_AST_BINARY_OP,
|
2014-06-15 00:30:18 +08:00
|
|
|
ZEND_AST_GREATER,
|
|
|
|
ZEND_AST_GREATER_EQUAL,
|
2014-06-26 18:43:20 +08:00
|
|
|
ZEND_AST_AND,
|
|
|
|
ZEND_AST_OR,
|
2014-06-19 19:57:29 +08:00
|
|
|
ZEND_AST_ARRAY_ELEM,
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_NEW,
|
|
|
|
ZEND_AST_INSTANCEOF,
|
|
|
|
ZEND_AST_YIELD,
|
2014-09-17 02:14:46 +08:00
|
|
|
ZEND_AST_COALESCE,
|
2014-07-10 05:39:21 +08:00
|
|
|
|
2014-07-12 23:00:53 +08:00
|
|
|
ZEND_AST_STATIC,
|
2014-07-10 20:35:59 +08:00
|
|
|
ZEND_AST_WHILE,
|
2014-07-10 20:46:22 +08:00
|
|
|
ZEND_AST_DO_WHILE,
|
2014-07-10 22:38:04 +08:00
|
|
|
ZEND_AST_IF_ELEM,
|
2014-07-11 21:31:47 +08:00
|
|
|
ZEND_AST_SWITCH,
|
|
|
|
ZEND_AST_SWITCH_CASE,
|
2014-07-22 22:11:19 +08:00
|
|
|
ZEND_AST_DECLARE,
|
2014-07-19 21:13:50 +08:00
|
|
|
ZEND_AST_CONST_ELEM,
|
2014-07-20 04:39:01 +08:00
|
|
|
ZEND_AST_USE_TRAIT,
|
|
|
|
ZEND_AST_TRAIT_PRECEDENCE,
|
|
|
|
ZEND_AST_METHOD_REFERENCE,
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_NAMESPACE,
|
|
|
|
ZEND_AST_USE_ELEM,
|
|
|
|
ZEND_AST_TRAIT_ALIAS,
|
2015-03-08 04:59:20 +08:00
|
|
|
ZEND_AST_GROUP_USE,
|
2014-07-21 22:34:45 +08:00
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
/* 3 child nodes */
|
|
|
|
ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
|
|
|
|
ZEND_AST_STATIC_CALL,
|
|
|
|
ZEND_AST_CONDITIONAL,
|
2014-07-22 04:49:31 +08:00
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_AST_TRY,
|
|
|
|
ZEND_AST_CATCH,
|
|
|
|
ZEND_AST_PARAM,
|
2015-10-07 04:32:23 +08:00
|
|
|
ZEND_AST_PROP_ELEM,
|
2014-07-28 04:26:06 +08:00
|
|
|
|
|
|
|
/* 4 child nodes */
|
|
|
|
ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,
|
|
|
|
ZEND_AST_FOREACH,
|
2014-06-19 19:44:17 +08:00
|
|
|
};
|
|
|
|
|
2014-08-26 03:21:16 +08:00
|
|
|
typedef uint16_t zend_ast_kind;
|
|
|
|
typedef uint16_t zend_ast_attr;
|
2013-10-31 15:57:12 +08:00
|
|
|
|
|
|
|
struct _zend_ast {
|
2014-07-28 20:51:08 +08:00
|
|
|
zend_ast_kind kind; /* Type of the node (ZEND_AST_* enum constant) */
|
2014-06-19 19:44:17 +08:00
|
|
|
zend_ast_attr attr; /* Additional attribute, use depending on node type */
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t lineno; /* Line number */
|
2014-06-19 19:44:17 +08:00
|
|
|
zend_ast *child[1]; /* Array of children (using struct hack) */
|
2013-10-31 15:57:12 +08:00
|
|
|
};
|
|
|
|
|
2014-07-28 04:26:06 +08:00
|
|
|
/* Same as zend_ast, but with children count, which is updated dynamically */
|
|
|
|
typedef struct _zend_ast_list {
|
|
|
|
zend_ast_kind kind;
|
|
|
|
zend_ast_attr attr;
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t lineno;
|
|
|
|
uint32_t children;
|
2014-07-28 04:26:06 +08:00
|
|
|
zend_ast *child[1];
|
|
|
|
} zend_ast_list;
|
|
|
|
|
2014-07-28 20:51:08 +08:00
|
|
|
/* Lineno is stored in val.u2.lineno */
|
2014-06-07 04:35:21 +08:00
|
|
|
typedef struct _zend_ast_zval {
|
2014-06-19 19:44:17 +08:00
|
|
|
zend_ast_kind kind;
|
|
|
|
zend_ast_attr attr;
|
2014-06-07 04:35:21 +08:00
|
|
|
zval val;
|
|
|
|
} zend_ast_zval;
|
|
|
|
|
2014-07-21 21:38:21 +08:00
|
|
|
/* Separate structure for function and class declaration, as they need extra information. */
|
|
|
|
typedef struct _zend_ast_decl {
|
2014-07-18 20:57:00 +08:00
|
|
|
zend_ast_kind kind;
|
2014-07-18 21:47:46 +08:00
|
|
|
zend_ast_attr attr; /* Unused - for structure compatibility */
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t start_lineno;
|
|
|
|
uint32_t end_lineno;
|
|
|
|
uint32_t flags;
|
2014-07-18 21:05:31 +08:00
|
|
|
unsigned char *lex_pos;
|
2014-07-18 21:23:16 +08:00
|
|
|
zend_string *doc_comment;
|
2014-07-18 20:57:00 +08:00
|
|
|
zend_string *name;
|
2015-01-09 04:40:36 +08:00
|
|
|
zend_ast *child[4];
|
2014-07-21 21:38:21 +08:00
|
|
|
} zend_ast_decl;
|
2014-07-18 20:57:00 +08:00
|
|
|
|
2015-01-20 00:39:52 +08:00
|
|
|
typedef void (*zend_ast_process_t)(zend_ast *ast);
|
2015-01-20 18:30:03 +08:00
|
|
|
extern ZEND_API zend_ast_process_t zend_ast_process;
|
2015-01-20 00:39:52 +08:00
|
|
|
|
2014-06-19 19:57:29 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr);
|
|
|
|
|
2014-07-29 00:18:47 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...);
|
|
|
|
ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...);
|
2014-06-19 19:44:17 +08:00
|
|
|
|
2014-07-21 21:38:21 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_decl(
|
2014-08-26 03:21:16 +08:00
|
|
|
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
|
2015-01-09 04:40:36 +08:00
|
|
|
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
|
2014-07-18 20:57:00 +08:00
|
|
|
);
|
|
|
|
|
2014-08-30 02:52:26 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...);
|
|
|
|
ZEND_API zend_ast *zend_ast_list_add(zend_ast *list, zend_ast *op);
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2015-04-02 07:05:25 +08:00
|
|
|
ZEND_API int zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope);
|
2015-02-19 16:11:17 +08:00
|
|
|
ZEND_API zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix);
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2013-11-07 02:21:07 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_copy(zend_ast *ast);
|
|
|
|
ZEND_API void zend_ast_destroy(zend_ast *ast);
|
2014-07-27 00:08:31 +08:00
|
|
|
ZEND_API void zend_ast_destroy_and_free(zend_ast *ast);
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
typedef void (*zend_ast_apply_func)(zend_ast **ast_ptr);
|
|
|
|
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn);
|
2014-07-28 04:26:06 +08:00
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_bool zend_ast_is_list(zend_ast *ast) {
|
2014-07-28 04:26:06 +08:00
|
|
|
return (ast->kind >> ZEND_AST_IS_LIST_SHIFT) & 1;
|
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast_list *zend_ast_get_list(zend_ast *ast) {
|
2014-07-28 04:26:06 +08:00
|
|
|
ZEND_ASSERT(zend_ast_is_list(ast));
|
|
|
|
return (zend_ast_list *) ast;
|
|
|
|
}
|
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zval *zend_ast_get_zval(zend_ast *ast) {
|
2014-07-28 20:51:08 +08:00
|
|
|
ZEND_ASSERT(ast->kind == ZEND_AST_ZVAL);
|
2014-07-27 02:54:41 +08:00
|
|
|
return &((zend_ast_zval *) ast)->val;
|
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_string *zend_ast_get_str(zend_ast *ast) {
|
2014-09-23 06:40:17 +08:00
|
|
|
zval *zv = zend_ast_get_zval(ast);
|
|
|
|
ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
|
|
|
|
return Z_STR_P(zv);
|
2014-07-27 02:54:41 +08:00
|
|
|
}
|
2014-07-28 20:51:08 +08:00
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
|
2014-07-28 20:51:08 +08:00
|
|
|
ZEND_ASSERT(!zend_ast_is_list(ast));
|
|
|
|
return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
|
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
|
2014-07-27 19:25:32 +08:00
|
|
|
if (ast->kind == ZEND_AST_ZVAL) {
|
|
|
|
zval *zv = zend_ast_get_zval(ast);
|
|
|
|
return zv->u2.lineno;
|
|
|
|
} else {
|
|
|
|
return ast->lineno;
|
|
|
|
}
|
|
|
|
}
|
2014-07-27 02:54:41 +08:00
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_zval(zval *zv) {
|
2014-06-19 19:57:29 +08:00
|
|
|
return zend_ast_create_zval_ex(zv, 0);
|
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_zval_from_str(zend_string *str) {
|
2014-07-27 01:01:14 +08:00
|
|
|
zval zv;
|
|
|
|
ZVAL_STR(&zv, str);
|
|
|
|
return zend_ast_create_zval(&zv);
|
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_zval_from_long(zend_long lval) {
|
2014-07-27 01:01:14 +08:00
|
|
|
zval zv;
|
|
|
|
ZVAL_LONG(&zv, lval);
|
|
|
|
return zend_ast_create_zval(&zv);
|
|
|
|
}
|
2014-07-10 21:18:08 +08:00
|
|
|
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
|
2014-07-29 00:18:47 +08:00
|
|
|
return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
|
2014-06-19 19:57:29 +08:00
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_assign_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
|
2014-07-29 00:18:47 +08:00
|
|
|
return zend_ast_create_ex(ZEND_AST_ASSIGN_OP, opcode, op0, op1);
|
2014-07-10 21:18:08 +08:00
|
|
|
}
|
2014-09-18 08:59:01 +08:00
|
|
|
static zend_always_inline zend_ast *zend_ast_create_cast(uint32_t type, zend_ast *op0) {
|
2014-07-29 00:18:47 +08:00
|
|
|
return zend_ast_create_ex(ZEND_AST_CAST, type, op0);
|
2014-06-19 19:57:29 +08:00
|
|
|
}
|
2014-06-07 19:06:53 +08:00
|
|
|
|
2013-10-31 15:57:12 +08:00
|
|
|
#endif
|