com php-src: Add iterable pseudo-type: Zend/zend_API.c Ze nd/zend_API.h Zend/zend_compile.c Zend/zend_execu te.c Zend/zend_types.h Zend/zend_vm_def.h
Zen d/zend_vm_execute.h

From: Date: Fri, 03 Jun 2016 22:42:04 +0000
Subject: com php-src: Add iterable pseudo-type: Zend/zend_API.c Ze nd/zend_API.h Zend/zend_compile.c Zend/zend_execu te.c Zend/zend_types.h Zend/zend_vm_def.h
Zen d/zend_vm_execute.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-92761@lists.php.net to get a copy of this message
Commit: 72692e9a9c3439be3083135eec3b174410ef08e4 Author: Aaron Piotrowski <aaron@trowski.com> Fri, 3 Jun 2016 17:42:04 -0500 Parents: b7bb6d04fd829bc5e854e76f669316f8147e468a Branches: master Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=72692e9a9c3439be3083135eec3b174410ef08e4 Log: Add iterable pseudo-type Changed paths: M Zend/zend_API.c M Zend/zend_API.h M Zend/zend_compile.c M Zend/zend_execute.c M Zend/zend_types.h M Zend/zend_vm_def.h M Zend/zend_vm_execute.h

diff --git a/Zend/zend_API.c b/Zend/zend_API.c index eb76c67..0ecbe0e 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -27,6 +27,7 @@ #include "zend_modules.h" #include "zend_extensions.h" #include "zend_constants.h" +#include "zend_interfaces.h" #include "zend_exceptions.h" #include "zend_closures.h" #include "zend_inheritance.h" @@ -4201,6 +4202,19 @@ ZEND_API const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */ } /* }}} */ +ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */ +{ + switch (Z_TYPE_P(iterable)) { + case IS_ARRAY: + return 1; + case IS_OBJECT: + return instanceof_function(Z_OBJCE_P(iterable), zend_ce_traversable); + default: + return 0; + } +} +/* }}} */ + /* * Local variables: * tab-width: 4 diff --git a/Zend/zend_API.h b/Zend/zend_API.h index ff93a53..5b2b95a 100644 --- a/Zend/zend_API.h +++ b/Zend/zend_API.h @@ -550,6 +550,8 @@ ZEND_API zend_string *zend_resolve_method_name(zend_class_entry *ce, zend_functi ZEND_API const char *zend_get_object_type(const zend_class_entry *ce); +ZEND_API zend_bool zend_is_iterable(zval *iterable); + #define add_method(arg, key, method) add_assoc_function((arg), (key), (method)) ZEND_API ZEND_FUNCTION(display_disabled_function); diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 4bb7126..b6627b2 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -160,6 +160,7 @@ static const struct reserved_class_name reserved_class_names[] = { {ZEND_STRL("string")}, {ZEND_STRL("true")}, {ZEND_STRL("void")}, + {ZEND_STRL("iterable")}, {NULL, 0} }; @@ -204,6 +205,7 @@ static const builtin_type_info builtin_types[] = { {ZEND_STRL("string"), IS_STRING}, {ZEND_STRL("bool"), _IS_BOOL}, {ZEND_STRL("void"), IS_VOID}, + {ZEND_STRL("iterable"), IS_ITERABLE}, {NULL, 0, IS_UNDEF} }; @@ -5060,6 +5062,14 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast) /* {{{ */ zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " "with callable type can only be NULL"); } + } else if (arg_info->type_hint == IS_ITERABLE && default_ast) { + if (default_ast && !has_null_default + && Z_TYPE(default_node.u.constant) != IS_ARRAY + && !Z_CONSTANT(default_node.u.constant) + ) { + zend_error_noreturn(E_COMPILE_ERROR, "Default value for parameters " + "with iterable type can only be an array or NULL"); + } } } else { if (default_ast && !has_null_default && !Z_CONSTANT(default_node.u.constant)) { diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 8d97845..f7b92e7 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -758,6 +758,11 @@ static int zend_verify_internal_arg_type(zend_function *zf, uint32_t arg_num, zv zend_verify_arg_error(zf, arg_num, "be callable", "", zend_zval_type_name(arg), ""); return 0; } + } else if (cur_arg_info->type_hint == IS_ITERABLE) { + if (!zend_is_iterable(arg)) { + zend_verify_arg_error(zf, arg_num, "be iterable", "", zend_zval_type_name(arg), ""); + return 0; + } } else if (cur_arg_info->type_hint == _IS_BOOL && EXPECTED(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE)) { /* pass */ @@ -849,6 +854,11 @@ static zend_always_inline int zend_verify_arg_type(zend_function *zf, uint32_t a zend_verify_arg_error(zf, arg_num, "be callable", "", zend_zval_type_name(arg), ""); return 0; } + } else if (cur_arg_info->type_hint == IS_ITERABLE) { + if (!zend_is_iterable(arg)) { + zend_verify_arg_error(zf, arg_num, "be iterable", "", zend_zval_type_name(arg), ""); + return 0; + } } else if (cur_arg_info->type_hint == _IS_BOOL && EXPECTED(Z_TYPE_P(arg) == IS_FALSE || Z_TYPE_P(arg) == IS_TRUE)) { /* pass */ @@ -893,6 +903,8 @@ static zend_always_inline int zend_verify_missing_arg_type(zend_function *zf, ui zend_verify_arg_error(zf, arg_num, need_msg, ZSTR_VAL(ce->name), "none", ""); } else if (cur_arg_info->type_hint == IS_CALLABLE) { zend_verify_arg_error(zf, arg_num, "be callable", "", "none", ""); + } else if (cur_arg_info->type_hint == IS_ITERABLE) { + zend_verify_arg_error(zf, arg_num, "be iterable", "", "none", ""); } else { zend_verify_arg_error(zf, arg_num, "be of the type ", zend_get_type_by_const(cur_arg_info->type_hint), "none", ""); } @@ -998,6 +1010,11 @@ static int zend_verify_internal_return_type(zend_function *zf, zval *ret) zend_verify_internal_return_error(zf, "be callable", "", zend_zval_type_name(ret), ""); return 0; } + } else if (ret_info->type_hint == IS_ITERABLE) { + if (!zend_is_iterable(ret) && (Z_TYPE_P(ret) != IS_NULL || !ret_info->allow_null)) { + zend_verify_internal_return_error(zf, "be iterable", "", zend_zval_type_name(ret), ""); + return 0; + } } else if (ret_info->type_hint == _IS_BOOL && EXPECTED(Z_TYPE_P(ret) == IS_FALSE || Z_TYPE_P(ret) == IS_TRUE)) { /* pass */ @@ -1060,6 +1077,10 @@ static zend_always_inline void zend_verify_return_type(zend_function *zf, zval * if (!zend_is_callable(ret, IS_CALLABLE_CHECK_SILENT, NULL)) { zend_verify_return_error(zf, "be callable", "", zend_zval_type_name(ret), ""); } + } else if (ret_info->type_hint == IS_ITERABLE) { + if (!zend_is_iterable(ret)) { + zend_verify_return_error(zf, "be callable", "", zend_zval_type_name(ret), ""); + } } else if (ret_info->type_hint == _IS_BOOL && EXPECTED(Z_TYPE_P(ret) == IS_FALSE || Z_TYPE_P(ret) == IS_TRUE)) { /* pass */ @@ -1101,6 +1122,8 @@ static ZEND_COLD int zend_verify_missing_return_type(zend_function *zf, void **c return 0; } else if (ret_info->type_hint == IS_CALLABLE) { zend_verify_return_error(zf, "be callable", "", "none", ""); + } else if (ret_info->type_hint == IS_ITERABLE) { + zend_verify_return_error(zf, "be iterable", "", "none", ""); } else { zend_verify_return_error(zf, "be of the type ", zend_get_type_by_const(ret_info->type_hint), "none", ""); } diff --git a/Zend/zend_types.h b/Zend/zend_types.h index 45c2ebd..95c4ef6 100644 --- a/Zend/zend_types.h +++ b/Zend/zend_types.h @@ -319,12 +319,13 @@ struct _zend_ast_ref { /* fake types */ #define _IS_BOOL 13 #define IS_CALLABLE 14 +#define IS_ITERABLE 19 #define IS_VOID 18 /* internal types */ #define IS_INDIRECT 15 #define IS_PTR 17 -#define _IS_ERROR 19 +#define _IS_ERROR 20 static zend_always_inline zend_uchar zval_get_type(const zval* pz) { return pz->u1.v.type; diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 44fb729..f2eba7d 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -3861,6 +3861,7 @@ ZEND_VM_HANDLER(124, ZEND_VERIFY_RETURN_TYPE, CONST|TMP|VAR|UNUSED|CV, UNUSED) if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr) diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 67def76..e337d7c 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -7419,6 +7419,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_VERIFY_RETURN_TYPE_SPEC_CONST_ if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr) @@ -13925,6 +13926,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_VERIFY_RETURN_TYPE_SPEC_TMP_UN if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr) @@ -21654,6 +21656,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_VERIFY_RETURN_TYPE_SPEC_VAR_UN if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr) @@ -31610,6 +31613,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_VERIFY_RETURN_TYPE_SPEC_UNUSED if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr) @@ -45100,6 +45104,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_VERIFY_RETURN_TYPE_SPEC_CV_UNU if (UNEXPECTED(!ret_info->class_name && ret_info->type_hint != IS_CALLABLE + && ret_info->type_hint != IS_ITERABLE && !ZEND_SAME_FAKE_TYPE(ret_info->type_hint, Z_TYPE_P(retval_ptr)) && !(EX(func)->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) && retval_ref != retval_ptr)
« previous php.cvs (#92761) next »