com php-src: Merge branch 'PHP-5.6' into PHP-7.0: Zend/tests/co nst_array_with_resource_key.phpt Zend/zend_ast. c Zend/zend_compile.c

From: Date: Wed, 28 Sep 2016 21:22:11 +0000
Subject: com php-src: Merge branch 'PHP-5.6' into PHP-7.0: Zend/tests/co nst_array_with_resource_key.phpt Zend/zend_ast. c Zend/zend_compile.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-94755@lists.php.net to get a copy of this message
Commit: e520b9e12763cc3719c02fb5ebd538e89644a35c Author: Nikita Popov <nikic@php.net> Wed, 28 Sep 2016 23:12:45 +0200 Parents: 437942d972c15e17539e9029205a1198bb597b90 1f5412982cd65800b061cef4877d0fd87662568b Branches: PHP-7.0 PHP-7.1 master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=e520b9e12763cc3719c02fb5ebd538e89644a35c Log: Merge branch 'PHP-5.6' into PHP-7.0 Changed paths: AM Zend/tests/const_array_with_resource_key.phpt MM Zend/zend_ast.c MM Zend/zend_compile.c

diff --cc Zend/tests/const_array_with_resource_key.phpt index 0000000,2533b78..0bf546a mode 000000,100644..100644 --- a/Zend/tests/const_array_with_resource_key.phpt +++ b/Zend/tests/const_array_with_resource_key.phpt @@@ -1,0 -1,15 +1,15 @@@ + --TEST-- + Constexpr arrays should be able to handle resource keys + --FILE-- + <?php + + const FOO = [STDIN => 42]; + var_dump(FOO); + + ?> + --EXPECTF-- -Strict Standards: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d ++Notice: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d + array(1) { + [%d]=> + int(42) + } diff --cc Zend/zend_ast.c index 11980f1,ae84db0..db4e86d --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@@ -139,139 -71,198 +139,147 @@@ ZEND_API zend_ast *zend_ast_create(zend return ast; } -ZEND_API void zend_ast_dynamic_add(zend_ast **ast, zend_ast *op) -{ - if ((*ast)->children >= 4 && (*ast)->children == ((*ast)->children & -(*ast)->children)) { - *ast = erealloc(*ast, sizeof(zend_ast) + sizeof(zend_ast*) * ((*ast)->children * 2 + 1)); +ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) { + zend_ast *ast; + zend_ast_list *list; + + ast = zend_ast_alloc(zend_ast_list_size(4)); + list = (zend_ast_list *) ast; + list->kind = kind; + list->attr = 0; + list->lineno = CG(zend_lineno); + list->children = 0; + + { + va_list va; + uint32_t i; + va_start(va, kind); + for (i = 0; i < init_children; ++i) { + ast = zend_ast_list_add(ast, va_arg(va, zend_ast *)); + } + va_end(va); } - (&(*ast)->u.child)[(*ast)->children++] = op; -} -ZEND_API void zend_ast_dynamic_shrink(zend_ast **ast) -{ - *ast = erealloc(*ast, sizeof(zend_ast) + sizeof(zend_ast*) * ((*ast)->children - 1)); + return ast; } -ZEND_API int zend_ast_is_ct_constant(zend_ast *ast) -{ - int i; +static inline zend_bool is_power_of_two(uint32_t n) { + return ((n != 0) && (n == (n & (~n + 1)))); +} - if (ast->kind == ZEND_CONST) { - return !IS_CONSTANT_TYPE(Z_TYPE_P(ast->u.val)); - } else { - for (i = 0; i < ast->children; i++) { - if ((&ast->u.child)[i]) { - if (!zend_ast_is_ct_constant((&ast->u.child)[i])) { - return 0; - } - } - } - return 1; +ZEND_API zend_ast *zend_ast_list_add(zend_ast *ast, zend_ast *op) { + zend_ast_list *list = zend_ast_get_list(ast); + if (list->children >= 4 && is_power_of_two(list->children)) { + list = zend_ast_realloc(list, + zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2)); } + list->child[list->children++] = op; + return (zend_ast *) list; } -ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope TSRMLS_DC) +static int zend_ast_add_array_element(zval *result, zval *offset, zval *expr) { - zval op1, op2; - - switch (ast->kind) { - case ZEND_ADD: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - add_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_SUB: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - sub_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_MUL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - mul_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_POW: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - pow_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_DIV: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - div_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_MOD: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - mod_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_SL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - shift_left_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_SR: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - shift_right_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_CONCAT: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - concat_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + switch (Z_TYPE_P(offset)) { + case IS_UNDEF: - zend_hash_next_index_insert(Z_ARRVAL_P(result), expr); ++ if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) { ++ zend_error(E_WARNING, ++ "Cannot add element to the array as the next element is already occupied"); ++ zval_ptr_dtor(expr); ++ } break; - case ZEND_BW_OR: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - bitwise_or_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case IS_STRING: + zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr); + zval_dtor(offset); break; - case ZEND_BW_AND: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - bitwise_and_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case IS_NULL: + zend_symtable_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), expr); break; - case ZEND_BW_XOR: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - bitwise_xor_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case IS_LONG: + zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr); break; - case ZEND_BW_NOT: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - bitwise_not_function(result, &op1 TSRMLS_CC); - zval_dtor(&op1); + case IS_FALSE: + zend_hash_index_update(Z_ARRVAL_P(result), 0, expr); break; - case ZEND_BOOL_NOT: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - boolean_not_function(result, &op1 TSRMLS_CC); - zval_dtor(&op1); + case IS_TRUE: + zend_hash_index_update(Z_ARRVAL_P(result), 1, expr); break; - case ZEND_BOOL_XOR: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - boolean_xor_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case IS_DOUBLE: + zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr); break; - case ZEND_IS_IDENTICAL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_identical_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_IS_NOT_IDENTICAL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_not_identical_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); ++ case IS_RESOURCE: ++ zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset)); ++ zend_hash_index_update(Z_ARRVAL_P(result), Z_RES_HANDLE_P(offset), expr); + break; - case ZEND_IS_EQUAL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_equal_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); - break; - case ZEND_IS_NOT_EQUAL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_not_equal_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + default: + zend_throw_error(NULL, "Illegal offset type"); + return FAILURE; + } + return SUCCESS; +} + +ZEND_API int zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope) +{ + zval op1, op2; + int ret = SUCCESS; + + switch (ast->kind) { + case ZEND_AST_BINARY_OP: + if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) { + ret = FAILURE; + } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) { + zval_dtor(&op1); + ret = FAILURE; + } else { + binary_op_type op = get_binary_op(ast->attr); + ret = op(result, &op1, &op2); + zval_dtor(&op1); + zval_dtor(&op2); + } break; - case ZEND_IS_SMALLER: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_smaller_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case ZEND_AST_GREATER: + case ZEND_AST_GREATER_EQUAL: + if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) { + ret = FAILURE; + } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) { + zval_dtor(&op1); + ret = FAILURE; + } else { + /* 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; + ret = op(result, &op2, &op1); + zval_dtor(&op1); + zval_dtor(&op2); + } break; - case ZEND_IS_SMALLER_OR_EQUAL: - zend_ast_evaluate(&op1, (&ast->u.child)[0], scope TSRMLS_CC); - zend_ast_evaluate(&op2, (&ast->u.child)[1], scope TSRMLS_CC); - is_smaller_or_equal_function(result, &op1, &op2 TSRMLS_CC); - zval_dtor(&op1); - zval_dtor(&op2); + case ZEND_AST_UNARY_OP: + if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) { + ret = FAILURE; + } else { + unary_op_type op = get_unary_op(ast->attr); + ret = op(result, &op1); + zval_dtor(&op1); + } break; - case ZEND_CONST: - /* class constants may be updated in-place */ + case ZEND_AST_ZVAL: + { + zval *zv = zend_ast_get_zval(ast); if (scope) { - if (IS_CONSTANT_TYPE(Z_TYPE_P(ast->u.val))) { - zval_update_constant_ex(&ast->u.val, 1, scope TSRMLS_CC); + /* class constants may be updated in-place */ + if (Z_OPT_CONSTANT_P(zv)) { + if (UNEXPECTED(zval_update_constant_ex(zv, 1, scope) != SUCCESS)) { + ret = FAILURE; + break; + } } - *result = *ast->u.val; - zval_copy_ctor(result); + ZVAL_DUP(result, zv); } else { - *result = *ast->u.val; - zval_copy_ctor(result); - if (IS_CONSTANT_TYPE(Z_TYPE_P(result))) { - zval_update_constant_ex(&result, 1, scope TSRMLS_CC); + ZVAL_DUP(result, zv); + if (Z_OPT_CONSTANT_P(result)) { + if (UNEXPECTED(zval_update_constant_ex(result, 1, scope) != SUCCESS)) { + ret = FAILURE; + break; + } } } break; diff --cc Zend/zend_compile.c index 7c4d9d5,f91f029..53cafcb --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@@ -5867,94 -6787,19 +5867,98 @@@ static inline zend_bool zend_try_ct_eva } /* }}} */ -void zend_do_extended_info(TSRMLS_D) /* {{{ */ +static inline void zend_ct_eval_unary_op(zval *result, uint32_t opcode, zval *op) /* {{{ */ { - zend_op *opline; + unary_op_type fn = get_unary_op(opcode); + fn(result, op); +} +/* }}} */ - if (!(CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO)) { - return; +static inline void zend_ct_eval_unary_pm(zval *result, zend_ast_kind kind, zval *op) /* {{{ */ +{ + zval left; + ZVAL_LONG(&left, (kind == ZEND_AST_UNARY_PLUS) ? 1 : -1); + mul_function(result, &left, op); +} +/* }}} */ + +static inline void zend_ct_eval_greater(zval *result, zend_ast_kind kind, zval *op1, zval *op2) /* {{{ */ +{ + binary_op_type fn = kind == ZEND_AST_GREATER + ? is_smaller_function : is_smaller_or_equal_function; + fn(result, op2, op1); +} +/* }}} */ + +static zend_bool zend_try_ct_eval_array(zval *result, zend_ast *ast) /* {{{ */ +{ + zend_ast_list *list = zend_ast_get_list(ast); + uint32_t i; + zend_bool is_constant = 1; + + /* First ensure that *all* child nodes are constant and by-val */ + for (i = 0; i < list->children; ++i) { + zend_ast *elem_ast = list->child[i]; + zend_bool by_ref = elem_ast->attr; + zend_eval_const_expr(&elem_ast->child[0]); + zend_eval_const_expr(&elem_ast->child[1]); + + if (by_ref || elem_ast->child[0]->kind != ZEND_AST_ZVAL + || (elem_ast->child[1] && elem_ast->child[1]->kind != ZEND_AST_ZVAL) + ) { + is_constant = 0; + } } - opline = get_next_op(CG(active_op_array) TSRMLS_CC); + if (!is_constant) { + return 0; + } - opline->opcode = ZEND_EXT_STMT; - SET_UNUSED(opline->op1); - SET_UNUSED(opline->op2); + array_init_size(result, list->children); + for (i = 0; i < list->children; ++i) { + zend_ast *elem_ast = list->child[i]; + zend_ast *value_ast = elem_ast->child[0]; + zend_ast *key_ast = elem_ast->child[1]; + + zval *value = zend_ast_get_zval(value_ast); + if (Z_REFCOUNTED_P(value)) Z_ADDREF_P(value); + + if (key_ast) { + zval *key = zend_ast_get_zval(key_ast); + switch (Z_TYPE_P(key)) { + case IS_LONG: + zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(key), value); + break; + case IS_STRING: + zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(key), value); + break; + case IS_DOUBLE: + zend_hash_index_update(Z_ARRVAL_P(result), + zend_dval_to_lval(Z_DVAL_P(key)), value); + break; + case IS_FALSE: + zend_hash_index_update(Z_ARRVAL_P(result), 0, value); + break; + case IS_TRUE: + zend_hash_index_update(Z_ARRVAL_P(result), 1, value); + break; + case IS_NULL: + zend_hash_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), value); + break; + default: + zend_error_noreturn(E_COMPILE_ERROR, "Illegal offset type"); + break; + } + } else { - zend_hash_next_index_insert(Z_ARRVAL_P(result), value); ++ if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), value)) { ++ zval_ptr_dtor_nogc(value); ++ zval_ptr_dtor(result); ++ return 0; ++ } + } + } + + return 1; } /* }}} */
« previous php.cvs (#94755) next »