2013-10-31 15:57:12 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1998-2014 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> |
|
2013-10-31 15:57:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "zend_ast.h"
|
2013-11-07 02:21:07 +08:00
|
|
|
#include "zend_API.h"
|
|
|
|
#include "zend_operators.h"
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2014-07-28 20:51:08 +08:00
|
|
|
static inline void *zend_ast_alloc(size_t size TSRMLS_DC) {
|
|
|
|
return zend_arena_alloc(&CG(ast_arena), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size TSRMLS_DC) {
|
|
|
|
void *new = zend_ast_alloc(new_size TSRMLS_CC);
|
|
|
|
memcpy(new, old, old_size);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2014-08-26 03:21:16 +08:00
|
|
|
static inline size_t zend_ast_size(uint32_t children) {
|
2014-08-19 17:38:09 +08:00
|
|
|
return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
|
2014-07-29 00:18:47 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 03:21:16 +08:00
|
|
|
static inline size_t zend_ast_list_size(uint32_t children) {
|
2014-08-19 17:38:09 +08:00
|
|
|
return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
|
2014-07-29 00:18:47 +08:00
|
|
|
}
|
|
|
|
|
2014-07-28 20:51:08 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_znode(znode *node) {
|
2014-09-01 03:50:00 +08:00
|
|
|
zend_ast_znode *ast;
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-09-01 03:50:00 +08:00
|
|
|
|
|
|
|
ast = zend_ast_alloc(sizeof(zend_ast_znode) TSRMLS_CC);
|
2014-06-07 19:06:53 +08:00
|
|
|
ast->kind = ZEND_AST_ZNODE;
|
2014-06-19 19:44:17 +08:00
|
|
|
ast->attr = 0;
|
2014-07-12 20:03:42 +08:00
|
|
|
ast->lineno = CG(zend_lineno);
|
2014-06-07 19:06:53 +08:00
|
|
|
ast->node = *node;
|
|
|
|
return (zend_ast *) ast;
|
|
|
|
}
|
|
|
|
|
2014-07-28 20:51:08 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
|
2014-09-01 03:50:00 +08:00
|
|
|
zend_ast_zval *ast;
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-09-01 03:50:00 +08:00
|
|
|
|
|
|
|
ast = zend_ast_alloc(sizeof(zend_ast_zval) TSRMLS_CC);
|
2014-06-29 00:03:26 +08:00
|
|
|
ast->kind = ZEND_AST_ZVAL;
|
2014-06-19 19:57:29 +08:00
|
|
|
ast->attr = attr;
|
|
|
|
ZVAL_COPY_VALUE(&ast->val, zv);
|
2014-07-27 19:25:32 +08:00
|
|
|
ast->val.u2.lineno = CG(zend_lineno);
|
2014-06-19 19:57:29 +08:00
|
|
|
return (zend_ast *) ast;
|
|
|
|
}
|
|
|
|
|
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,
|
2014-07-28 20:51:08 +08:00
|
|
|
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2
|
2014-07-18 20:57:00 +08:00
|
|
|
) {
|
2014-09-01 03:50:00 +08:00
|
|
|
zend_ast_decl *ast;
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-07-18 20:57:00 +08:00
|
|
|
|
2014-09-01 03:50:00 +08:00
|
|
|
ast = zend_ast_alloc(sizeof(zend_ast_decl) TSRMLS_CC);
|
2014-07-18 20:57:00 +08:00
|
|
|
ast->kind = kind;
|
2014-07-18 21:47:46 +08:00
|
|
|
ast->attr = 0;
|
2014-07-18 20:57:00 +08:00
|
|
|
ast->start_lineno = start_lineno;
|
2014-07-28 20:51:08 +08:00
|
|
|
ast->end_lineno = CG(zend_lineno);
|
2014-07-18 21:47:46 +08:00
|
|
|
ast->flags = flags;
|
2014-07-28 20:51:08 +08:00
|
|
|
ast->lex_pos = LANG_SCNG(yy_text);
|
2014-07-18 21:23:16 +08:00
|
|
|
ast->doc_comment = doc_comment;
|
2014-07-18 20:57:00 +08:00
|
|
|
ast->name = name;
|
2014-07-21 21:38:21 +08:00
|
|
|
ast->child[0] = child0;
|
|
|
|
ast->child[1] = child1;
|
|
|
|
ast->child[2] = child2;
|
2014-07-18 20:57:00 +08:00
|
|
|
|
|
|
|
return (zend_ast *) ast;
|
|
|
|
}
|
|
|
|
|
2014-07-29 00:18:47 +08:00
|
|
|
static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
|
2014-09-01 03:50:00 +08:00
|
|
|
zend_ast *ast;
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-09-01 03:50:00 +08:00
|
|
|
|
|
|
|
ast = zend_ast_alloc(zend_ast_size(children) TSRMLS_CC);
|
2013-11-07 23:39:47 +08:00
|
|
|
ast->kind = kind;
|
2014-06-19 19:44:17 +08:00
|
|
|
ast->attr = attr;
|
2014-08-26 03:21:16 +08:00
|
|
|
ast->lineno = (uint32_t) -1;
|
2014-07-10 21:18:08 +08:00
|
|
|
|
|
|
|
for (i = 0; i < children; ++i) {
|
|
|
|
ast->child[i] = va_arg(va, zend_ast *);
|
2014-07-27 19:25:32 +08:00
|
|
|
if (ast->child[i] != NULL) {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t lineno = zend_ast_get_lineno(ast->child[i]);
|
2014-07-27 19:25:32 +08:00
|
|
|
if (lineno < ast->lineno) {
|
|
|
|
ast->lineno = lineno;
|
|
|
|
}
|
2014-07-12 23:10:10 +08:00
|
|
|
}
|
2014-07-10 21:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 05:27:27 +08:00
|
|
|
if (ast->lineno == UINT_MAX) {
|
2014-07-12 23:10:10 +08:00
|
|
|
ast->lineno = CG(zend_lineno);
|
2014-07-17 05:27:27 +08:00
|
|
|
}
|
2014-07-12 23:10:10 +08:00
|
|
|
|
2013-11-07 23:39:47 +08:00
|
|
|
return ast;
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:18:47 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
|
2014-07-10 21:18:08 +08:00
|
|
|
va_list va;
|
|
|
|
zend_ast *ast;
|
|
|
|
|
|
|
|
va_start(va, attr);
|
2014-07-29 00:18:47 +08:00
|
|
|
ast = zend_ast_create_from_va_list(kind, attr, va);
|
2014-07-10 21:18:08 +08:00
|
|
|
va_end(va);
|
|
|
|
|
2013-11-07 23:39:47 +08:00
|
|
|
return ast;
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:18:47 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
|
2014-07-10 21:18:08 +08:00
|
|
|
va_list va;
|
|
|
|
zend_ast *ast;
|
|
|
|
|
|
|
|
va_start(va, kind);
|
2014-07-29 00:18:47 +08:00
|
|
|
ast = zend_ast_create_from_va_list(kind, 0, va);
|
2014-07-10 21:18:08 +08:00
|
|
|
va_end(va);
|
|
|
|
|
2013-11-07 23:39:47 +08:00
|
|
|
return ast;
|
2013-10-31 15:57:12 +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, ...) {
|
2014-09-01 03:50:00 +08:00
|
|
|
zend_ast *ast;
|
|
|
|
zend_ast_list *list;
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-08-30 02:52:26 +08:00
|
|
|
|
2014-09-01 03:50:00 +08:00
|
|
|
ast = zend_ast_alloc(zend_ast_list_size(4) TSRMLS_CC);
|
|
|
|
list = (zend_ast_list *) ast;
|
2014-07-28 04:26:06 +08:00
|
|
|
list->kind = kind;
|
|
|
|
list->attr = 0;
|
|
|
|
list->lineno = CG(zend_lineno);
|
|
|
|
list->children = 0;
|
2014-07-28 21:10:58 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
va_list va;
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-07-28 21:10:58 +08:00
|
|
|
va_start(va, kind);
|
|
|
|
for (i = 0; i < init_children; ++i) {
|
2014-08-30 02:52:26 +08:00
|
|
|
ast = zend_ast_list_add(ast, va_arg(va, zend_ast *));
|
2014-07-28 21:10:58 +08:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2014-08-30 02:52:26 +08:00
|
|
|
return ast;
|
2014-05-07 19:03:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 03:21:16 +08:00
|
|
|
static inline zend_bool is_power_of_two(uint32_t n) {
|
2014-09-15 17:59:48 +08:00
|
|
|
return ((n != 0) && (n == (n & (~n + 1))));
|
2014-06-07 04:35:21 +08:00
|
|
|
}
|
|
|
|
|
2014-08-30 02:52:26 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_list_add(zend_ast *ast, zend_ast *op) {
|
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
2014-07-28 04:26:06 +08:00
|
|
|
if (list->children >= 4 && is_power_of_two(list->children)) {
|
2014-10-15 15:37:55 +08:00
|
|
|
TSRMLS_FETCH();
|
2014-07-28 20:51:08 +08:00
|
|
|
list = zend_ast_realloc(list,
|
|
|
|
zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2) TSRMLS_CC);
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
2014-07-28 04:26:06 +08:00
|
|
|
list->child[list->children++] = op;
|
2014-08-30 02:52:26 +08:00
|
|
|
return (zend_ast *) list;
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-07 19:03:56 +08:00
|
|
|
static void zend_ast_add_array_element(zval *result, zval *offset, zval *expr TSRMLS_DC)
|
|
|
|
{
|
|
|
|
switch (Z_TYPE_P(offset)) {
|
|
|
|
case IS_UNDEF:
|
|
|
|
zend_hash_next_index_insert(Z_ARRVAL_P(result), expr);
|
|
|
|
break;
|
|
|
|
case IS_STRING:
|
|
|
|
zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
|
|
|
|
zval_dtor(offset);
|
|
|
|
break;
|
|
|
|
case IS_NULL:
|
|
|
|
zend_symtable_update(Z_ARRVAL_P(result), STR_EMPTY_ALLOC(), expr);
|
|
|
|
break;
|
|
|
|
case IS_LONG:
|
|
|
|
zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
|
|
|
|
break;
|
|
|
|
case IS_FALSE:
|
|
|
|
zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
|
|
|
|
break;
|
|
|
|
case IS_TRUE:
|
|
|
|
zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
|
|
|
|
break;
|
|
|
|
case IS_DOUBLE:
|
|
|
|
zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
zend_error(E_ERROR, "Illegal offset type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 18:19:17 +08:00
|
|
|
ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC)
|
2013-11-07 02:21:07 +08:00
|
|
|
{
|
|
|
|
zval op1, op2;
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2013-11-07 02:21:07 +08:00
|
|
|
switch (ast->kind) {
|
2014-06-26 19:00:13 +08:00
|
|
|
case ZEND_AST_BINARY_OP:
|
|
|
|
{
|
|
|
|
binary_op_type op = get_binary_op(ast->attr);
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
|
|
|
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
|
2014-06-26 19:00:13 +08:00
|
|
|
op(result, &op1, &op2 TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op1);
|
|
|
|
zval_dtor(&op2);
|
2013-10-31 15:57:12 +08:00
|
|
|
break;
|
2014-06-26 19:00:13 +08:00
|
|
|
}
|
2014-06-27 04:02:54 +08:00
|
|
|
case ZEND_AST_GREATER:
|
|
|
|
case ZEND_AST_GREATER_EQUAL:
|
|
|
|
{
|
|
|
|
/* op1 > op2 is the same as op2 < op1 */
|
|
|
|
binary_op_type op = ast->kind == ZEND_AST_GREATER
|
|
|
|
? is_smaller_function : is_smaller_or_equal_function;
|
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
|
|
|
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
|
|
|
|
op(result, &op2, &op1 TSRMLS_CC);
|
|
|
|
zval_dtor(&op1);
|
|
|
|
zval_dtor(&op2);
|
|
|
|
break;
|
|
|
|
}
|
2014-07-28 04:26:06 +08:00
|
|
|
case ZEND_AST_UNARY_OP:
|
|
|
|
{
|
|
|
|
unary_op_type op = get_unary_op(ast->attr);
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
2014-07-28 04:26:06 +08:00
|
|
|
op(result, &op1 TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op1);
|
|
|
|
break;
|
2014-07-28 04:26:06 +08:00
|
|
|
}
|
2014-06-29 00:03:26 +08:00
|
|
|
case ZEND_AST_ZVAL:
|
2014-08-17 03:55:08 +08:00
|
|
|
{
|
|
|
|
zval *zv = zend_ast_get_zval(ast);
|
2014-07-24 04:37:15 +08:00
|
|
|
if (scope) {
|
2014-08-17 03:55:08 +08:00
|
|
|
/* class constants may be updated in-place */
|
|
|
|
if (Z_OPT_CONSTANT_P(zv)) {
|
|
|
|
zval_update_constant_ex(zv, 1, scope TSRMLS_CC);
|
2014-07-24 04:37:15 +08:00
|
|
|
}
|
2014-08-17 03:55:08 +08:00
|
|
|
ZVAL_DUP(result, zv);
|
2014-07-24 04:37:15 +08:00
|
|
|
} else {
|
2014-08-17 03:55:08 +08:00
|
|
|
ZVAL_DUP(result, zv);
|
2014-07-24 05:54:21 +08:00
|
|
|
if (Z_OPT_CONSTANT_P(result)) {
|
|
|
|
zval_update_constant_ex(result, 1, scope TSRMLS_CC);
|
2014-07-24 04:37:15 +08:00
|
|
|
}
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-08-17 03:55:08 +08:00
|
|
|
}
|
2014-06-26 19:00:13 +08:00
|
|
|
case ZEND_AST_AND:
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
2013-12-18 14:25:05 +08:00
|
|
|
if (zend_is_true(&op1 TSRMLS_CC)) {
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
|
2013-12-18 14:25:05 +08:00
|
|
|
ZVAL_BOOL(result, zend_is_true(&op2 TSRMLS_CC));
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op2);
|
|
|
|
} else {
|
|
|
|
ZVAL_BOOL(result, 0);
|
|
|
|
}
|
|
|
|
zval_dtor(&op1);
|
|
|
|
break;
|
2014-06-26 19:00:13 +08:00
|
|
|
case ZEND_AST_OR:
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
2013-12-18 14:25:05 +08:00
|
|
|
if (zend_is_true(&op1 TSRMLS_CC)) {
|
2013-11-07 02:21:07 +08:00
|
|
|
ZVAL_BOOL(result, 1);
|
|
|
|
} else {
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
|
2013-12-18 14:25:05 +08:00
|
|
|
ZVAL_BOOL(result, zend_is_true(&op2 TSRMLS_CC));
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op2);
|
|
|
|
}
|
|
|
|
zval_dtor(&op1);
|
|
|
|
break;
|
2014-06-26 19:00:13 +08:00
|
|
|
case ZEND_AST_CONDITIONAL:
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
2013-12-18 14:25:05 +08:00
|
|
|
if (zend_is_true(&op1 TSRMLS_CC)) {
|
2014-06-07 04:35:21 +08:00
|
|
|
if (!ast->child[1]) {
|
2013-11-07 02:21:07 +08:00
|
|
|
*result = op1;
|
|
|
|
} else {
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(result, ast->child[1], scope TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op1);
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(result, ast->child[2], scope TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
zval_dtor(&op1);
|
|
|
|
}
|
|
|
|
break;
|
2014-06-26 18:43:20 +08:00
|
|
|
case ZEND_AST_UNARY_PLUS:
|
2013-11-07 02:21:07 +08:00
|
|
|
ZVAL_LONG(&op1, 0);
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op2, ast->child[0], scope TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
add_function(result, &op1, &op2 TSRMLS_CC);
|
|
|
|
zval_dtor(&op2);
|
|
|
|
break;
|
2014-06-26 18:43:20 +08:00
|
|
|
case ZEND_AST_UNARY_MINUS:
|
2013-11-07 02:21:07 +08:00
|
|
|
ZVAL_LONG(&op1, 0);
|
2014-06-07 04:35:21 +08:00
|
|
|
zend_ast_evaluate(&op2, ast->child[0], scope TSRMLS_CC);
|
2013-11-07 02:21:07 +08:00
|
|
|
sub_function(result, &op1, &op2 TSRMLS_CC);
|
|
|
|
zval_dtor(&op2);
|
|
|
|
break;
|
2014-06-26 19:16:31 +08:00
|
|
|
case ZEND_AST_ARRAY:
|
2014-05-07 19:03:56 +08:00
|
|
|
array_init(result);
|
|
|
|
{
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-07-28 04:26:06 +08:00
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
|
|
|
for (i = 0; i < list->children; i++) {
|
|
|
|
zend_ast *elem = list->child[i];
|
2014-06-26 19:16:31 +08:00
|
|
|
if (elem->child[1]) {
|
|
|
|
zend_ast_evaluate(&op1, elem->child[1], scope TSRMLS_CC);
|
2014-05-07 19:03:56 +08:00
|
|
|
} else {
|
|
|
|
ZVAL_UNDEF(&op1);
|
|
|
|
}
|
2014-06-26 19:16:31 +08:00
|
|
|
zend_ast_evaluate(&op2, elem->child[0], scope TSRMLS_CC);
|
2014-05-07 19:03:56 +08:00
|
|
|
zend_ast_add_array_element(result, &op1, &op2 TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-08-17 04:08:02 +08:00
|
|
|
case ZEND_AST_DIM:
|
2014-08-17 03:55:08 +08:00
|
|
|
zend_ast_evaluate(&op1, ast->child[0], scope TSRMLS_CC);
|
|
|
|
zend_ast_evaluate(&op2, ast->child[1], scope TSRMLS_CC);
|
2014-04-12 00:21:46 +08:00
|
|
|
{
|
2014-08-04 17:56:27 +08:00
|
|
|
zval tmp;
|
2014-04-12 00:21:46 +08:00
|
|
|
zend_fetch_dimension_by_zval(&tmp, &op1, &op2 TSRMLS_CC);
|
2014-08-04 17:56:27 +08:00
|
|
|
ZVAL_ZVAL(result, &tmp, 1, 1);
|
2014-04-12 00:21:46 +08:00
|
|
|
}
|
|
|
|
zval_dtor(&op1);
|
|
|
|
zval_dtor(&op2);
|
|
|
|
break;
|
2013-11-07 02:21:07 +08:00
|
|
|
default:
|
|
|
|
zend_error(E_ERROR, "Unsupported constant expression");
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
2013-11-01 23:16:58 +08:00
|
|
|
|
2013-11-07 02:21:07 +08:00
|
|
|
ZEND_API zend_ast *zend_ast_copy(zend_ast *ast)
|
|
|
|
{
|
2013-11-07 23:39:47 +08:00
|
|
|
if (ast == NULL) {
|
|
|
|
return NULL;
|
2014-06-29 00:03:26 +08:00
|
|
|
} else if (ast->kind == ZEND_AST_ZVAL) {
|
2014-07-28 04:26:06 +08:00
|
|
|
zend_ast_zval *new = emalloc(sizeof(zend_ast_zval));
|
|
|
|
new->kind = ZEND_AST_ZVAL;
|
|
|
|
new->attr = ast->attr;
|
2014-07-30 23:46:00 +08:00
|
|
|
ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
|
2014-07-28 04:26:06 +08:00
|
|
|
return (zend_ast *) new;
|
|
|
|
} else if (zend_ast_is_list(ast)) {
|
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
2014-07-29 00:18:47 +08:00
|
|
|
zend_ast_list *new = emalloc(zend_ast_list_size(list->children));
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-07-28 04:26:06 +08:00
|
|
|
new->kind = list->kind;
|
|
|
|
new->attr = list->attr;
|
|
|
|
new->children = list->children;
|
|
|
|
for (i = 0; i < list->children; i++) {
|
|
|
|
new->child[i] = zend_ast_copy(list->child[i]);
|
|
|
|
}
|
|
|
|
return (zend_ast *) new;
|
2014-07-22 00:02:31 +08:00
|
|
|
} else {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i, children = zend_ast_get_num_children(ast);
|
2014-07-29 00:18:47 +08:00
|
|
|
zend_ast *new = emalloc(zend_ast_size(children));
|
2014-04-11 16:06:17 +08:00
|
|
|
new->kind = ast->kind;
|
2014-06-29 00:03:26 +08:00
|
|
|
new->attr = ast->attr;
|
2014-07-28 04:26:06 +08:00
|
|
|
for (i = 0; i < children; i++) {
|
2014-06-07 04:35:21 +08:00
|
|
|
new->child[i] = zend_ast_copy(ast->child[i]);
|
2013-11-01 23:16:58 +08:00
|
|
|
}
|
2014-04-12 02:13:24 +08:00
|
|
|
return new;
|
2013-11-01 23:16:58 +08:00
|
|
|
}
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
|
|
|
|
2014-07-27 00:08:31 +08:00
|
|
|
static void zend_ast_destroy_ex(zend_ast *ast, zend_bool free) {
|
2014-07-18 20:57:00 +08:00
|
|
|
if (!ast) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-31 15:57:12 +08:00
|
|
|
|
2014-07-18 20:57:00 +08:00
|
|
|
switch (ast->kind) {
|
|
|
|
case ZEND_AST_ZVAL:
|
2014-08-18 01:15:54 +08:00
|
|
|
/* Destroy value without using GC: When opcache moves arrays into SHM it will
|
|
|
|
* free the zend_array structure, so references to it from outside the op array
|
|
|
|
* become invalid. GC would cause such a reference in the root buffer. */
|
2014-09-03 21:16:32 +08:00
|
|
|
zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
|
2014-07-18 20:57:00 +08:00
|
|
|
break;
|
|
|
|
case ZEND_AST_FUNC_DECL:
|
|
|
|
case ZEND_AST_CLOSURE:
|
2014-07-19 18:52:44 +08:00
|
|
|
case ZEND_AST_METHOD:
|
2014-07-21 22:34:45 +08:00
|
|
|
case ZEND_AST_CLASS:
|
2014-07-18 20:57:00 +08:00
|
|
|
{
|
2014-07-21 21:38:21 +08:00
|
|
|
zend_ast_decl *decl = (zend_ast_decl *) ast;
|
2014-08-26 03:21:16 +08:00
|
|
|
zend_string_release(decl->name);
|
2014-07-21 21:38:21 +08:00
|
|
|
if (decl->doc_comment) {
|
2014-08-26 03:21:16 +08:00
|
|
|
zend_string_release(decl->doc_comment);
|
2014-07-18 21:23:16 +08:00
|
|
|
}
|
2014-07-27 00:08:31 +08:00
|
|
|
zend_ast_destroy_ex(decl->child[0], free);
|
|
|
|
zend_ast_destroy_ex(decl->child[1], free);
|
|
|
|
zend_ast_destroy_ex(decl->child[2], free);
|
2014-07-18 20:57:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2014-07-28 04:26:06 +08:00
|
|
|
if (zend_ast_is_list(ast)) {
|
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-07-28 04:26:06 +08:00
|
|
|
for (i = 0; i < list->children; i++) {
|
|
|
|
zend_ast_destroy_ex(list->child[i], free);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i, children = zend_ast_get_num_children(ast);
|
2014-07-28 04:26:06 +08:00
|
|
|
for (i = 0; i < children; i++) {
|
|
|
|
zend_ast_destroy_ex(ast->child[i], free);
|
|
|
|
}
|
2013-11-07 02:21:07 +08:00
|
|
|
}
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
2014-07-18 20:57:00 +08:00
|
|
|
|
2014-07-27 00:08:31 +08:00
|
|
|
if (free) {
|
|
|
|
efree(ast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API void zend_ast_destroy(zend_ast *ast) {
|
|
|
|
zend_ast_destroy_ex(ast, 0);
|
|
|
|
}
|
|
|
|
ZEND_API void zend_ast_destroy_and_free(zend_ast *ast) {
|
|
|
|
zend_ast_destroy_ex(ast, 1);
|
2013-10-31 15:57:12 +08:00
|
|
|
}
|
2014-07-28 04:26:06 +08:00
|
|
|
|
|
|
|
ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn TSRMLS_DC) {
|
|
|
|
if (zend_ast_is_list(ast)) {
|
|
|
|
zend_ast_list *list = zend_ast_get_list(ast);
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i;
|
2014-07-28 04:26:06 +08:00
|
|
|
for (i = 0; i < list->children; ++i) {
|
|
|
|
fn(&list->child[i] TSRMLS_CC);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-26 03:21:16 +08:00
|
|
|
uint32_t i, children = zend_ast_get_num_children(ast);
|
2014-07-28 04:26:06 +08:00
|
|
|
for (i = 0; i < children; ++i) {
|
|
|
|
fn(&ast->child[i] TSRMLS_CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|