summaryrefslogtreecommitdiff
path: root/Zend/zend_ast.h
blob: 2f07cf3b426e19ce1718249ddb472f1cda766bf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
   +----------------------------------------------------------------------+
   | 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>                                |
   |          Dmitry Stogov <dmitry@zend.com>                             |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#ifndef ZEND_AST_H
#define ZEND_AST_H

#include "zend.h"

enum _zend_ast_kind {
	/* first 256 kinds are reserved for opcodes */
	ZEND_AST_ZVAL = 256,
	ZEND_AST_ZNODE,

	ZEND_AST_VAR,
	ZEND_AST_DIM,
	ZEND_AST_PROP,
	ZEND_AST_STATIC_PROP,

	ZEND_AST_CALL,
	ZEND_AST_METHOD_CALL,
	ZEND_AST_STATIC_CALL,
	ZEND_AST_PARAMS,
	ZEND_AST_UNPACK,

	ZEND_AST_CONST,
	ZEND_AST_CLASS_CONST,
	ZEND_AST_RESOLVE_CLASS_NAME,

	ZEND_AST_ASSIGN,
	ZEND_AST_ASSIGN_REF,
	ZEND_AST_ASSIGN_OP,
	ZEND_AST_LIST,

	ZEND_AST_UNARY_PLUS,
	ZEND_AST_UNARY_MINUS,
	ZEND_AST_CAST,
	ZEND_AST_EMPTY,
	ZEND_AST_ISSET,
	ZEND_AST_SILENCE,
	ZEND_AST_SHELL_EXEC,

	ZEND_AST_BINARY_OP,
	ZEND_AST_GREATER,
	ZEND_AST_GREATER_EQUAL,
	ZEND_AST_AND,
	ZEND_AST_OR,

	ZEND_AST_CONDITIONAL,

	ZEND_AST_ARRAY,
	ZEND_AST_ARRAY_ELEM,
	ZEND_AST_ENCAPS_LIST,

	ZEND_AST_EXPR_LIST,
	ZEND_AST_STMT_LIST,

	ZEND_AST_GLOBAL,
	ZEND_AST_UNSET,
	ZEND_AST_RETURN,
	ZEND_AST_LABEL,

	ZEND_AST_WHILE,
	ZEND_AST_DO_WHILE,
	ZEND_AST_FOR,
	ZEND_AST_IF,
	ZEND_AST_IF_ELEM,
};

typedef unsigned short zend_ast_kind;
typedef unsigned short zend_ast_attr;

struct _zend_ast {
	zend_ast_kind kind; /* Type of the node (either opcode or ZEND_AST_* constant) */
	zend_ast_attr attr; /* Additional attribute, use depending on node type */
	zend_uint children; /* Number of children */
	zend_ast *child[1]; /* Array of children (using struct hack) */
};

typedef struct _zend_ast_zval {
	zend_ast_kind kind;
	zend_ast_attr attr;
	zval val;
} zend_ast_zval;

static inline zval *zend_ast_get_zval(zend_ast *ast) {
	return &((zend_ast_zval *) ast)->val;
}

ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr);

ZEND_API zend_ast *zend_ast_create_ex(
	zend_uint children, zend_ast_kind kind, zend_ast_attr attr, ...);
ZEND_API zend_ast *zend_ast_create(
	zend_uint children, zend_ast_kind kind, ...);

ZEND_API zend_ast *zend_ast_create_dynamic(zend_ast_kind kind);
ZEND_API zend_ast *zend_ast_dynamic_add(zend_ast *ast, zend_ast *op);
ZEND_API void zend_ast_dynamic_shrink(zend_ast **ast);

ZEND_API int zend_ast_is_ct_constant(zend_ast *ast);

ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC);

ZEND_API zend_ast *zend_ast_copy(zend_ast *ast);
ZEND_API void zend_ast_destroy(zend_ast *ast);

static inline zend_ast *zend_ast_create_zval(zval *zv) {
	return zend_ast_create_zval_ex(zv, 0);
}

static inline zend_ast *zend_ast_create_unary(zend_ast_kind kind, zend_ast *op0) {
	return zend_ast_create(1, kind, op0);
}
static inline zend_ast *zend_ast_create_binary(zend_ast_kind kind, zend_ast *op0, zend_ast *op1) {
	return zend_ast_create(2, kind, op0, op1);
}
static inline zend_ast *zend_ast_create_ternary(
	zend_ast_kind kind, zend_ast *op0, zend_ast *op1, zend_ast *op2
) {
	return zend_ast_create(3, kind, op0, op1, op2);
}

static inline zend_ast *zend_ast_create_dynamic_and_add(zend_ast_kind kind, zend_ast *op) {
	return zend_ast_dynamic_add(zend_ast_create_dynamic(kind), op);
}

static inline zend_ast *zend_ast_create_var(zval *name) {
	return zend_ast_create_unary(ZEND_AST_VAR, zend_ast_create_zval(name));
}
static inline zend_ast *zend_ast_create_binary_op(zend_uint opcode, zend_ast *op0, zend_ast *op1) {
	return zend_ast_create_ex(2, ZEND_AST_BINARY_OP, opcode, op0, op1);
}
static inline zend_ast *zend_ast_create_assign_op(zend_uint opcode, zend_ast *op0, zend_ast *op1) {
	return zend_ast_create_ex(2, ZEND_AST_ASSIGN_OP, opcode, op0, op1);
}
static inline zend_ast *zend_ast_create_cast(zend_uint type, zend_ast *op0) {
	return zend_ast_create_ex(1, ZEND_AST_CAST, type, op0);
}

/* Temporary, for porting */
#define AST_COMPILE(res, ast) do { \
	zend_ast *_ast = (ast); \
	zend_eval_const_expr(&_ast TSRMLS_CC); \
	zend_compile_expr((res), _ast TSRMLS_CC); \
	zend_ast_destroy(_ast); \
} while (0)
#define AST_COMPILE_VAR(res, ast, type) do { \
	zend_ast *_ast = (ast); \
	zend_compile_var((res), _ast, type TSRMLS_CC); \
	zend_ast_destroy(_ast); \
} while (0)
#define AST_COMPILE_STMT(ast) do { \
	zend_ast *_ast = (ast); \
	zend_compile_stmt(_ast TSRMLS_CC); \
	if (_ast) zend_ast_destroy(_ast); \
} while (0)

#define AST_ZNODE(znode) zend_ast_create_znode((znode))
#define AST_ZVAL(znode) zend_ast_create_zval(&(znode)->u.constant)

#define AC(znode) AST_COMPILE(&znode, znode.u.ast)
#define AS(znode) AST_COMPILE_STMT(znode.u.ast)
#define AZ(znode) ((znode).u.ast = AST_ZNODE(&znode))
#define AN(znode) ((znode).u.ast = NULL)

#endif