summaryrefslogtreecommitdiff
path: root/Zend/zend_inheritance.c
diff options
context:
space:
mode:
Diffstat (limited to 'Zend/zend_inheritance.c')
-rw-r--r--Zend/zend_inheritance.c788
1 files changed, 521 insertions, 267 deletions
diff --git a/Zend/zend_inheritance.c b/Zend/zend_inheritance.c
index eed2074729..efe727207f 100644
--- a/Zend/zend_inheritance.c
+++ b/Zend/zend_inheritance.c
@@ -2,7 +2,7 @@
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
- | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
+ | Copyright (c) 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 |
@@ -25,31 +25,21 @@
#include "zend_smart_str.h"
#include "zend_operators.h"
-static void overriden_ptr_dtor(zval *zv) /* {{{ */
+static void overridden_ptr_dtor(zval *zv) /* {{{ */
{
efree_size(Z_PTR_P(zv), sizeof(zend_function));
}
/* }}} */
-static zend_property_info *zend_duplicate_property_info(zend_property_info *property_info) /* {{{ */
-{
- zend_property_info* new_property_info;
-
- new_property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
- memcpy(new_property_info, property_info, sizeof(zend_property_info));
- zend_string_addref(new_property_info->name);
- if (new_property_info->doc_comment) {
- zend_string_addref(new_property_info->doc_comment);
- }
- return new_property_info;
-}
-/* }}} */
-
static zend_property_info *zend_duplicate_property_info_internal(zend_property_info *property_info) /* {{{ */
{
zend_property_info* new_property_info = pemalloc(sizeof(zend_property_info), 1);
memcpy(new_property_info, property_info, sizeof(zend_property_info));
zend_string_addref(new_property_info->name);
+ if (ZEND_TYPE_IS_NAME(new_property_info->type)) {
+ zend_string_addref(ZEND_TYPE_NAME(new_property_info->type));
+ }
+
return new_property_info;
}
/* }}} */
@@ -78,11 +68,16 @@ static zend_function *zend_duplicate_function(zend_function *func, zend_class_en
/* reuse the same op_array structure */
return func;
}
- if (!(GC_FLAGS(func->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
- GC_ADDREF(func->op_array.static_variables);
- }
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_function, func, sizeof(zend_op_array));
+ if (ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr)) {
+ /* See: Zend/tests/method_static_var.phpt */
+ new_function->op_array.static_variables = ZEND_MAP_PTR_GET(func->op_array.static_variables_ptr);
+ }
+ if (!(GC_FLAGS(new_function->op_array.static_variables) & IS_ARRAY_IMMUTABLE)) {
+ GC_ADDREF(new_function->op_array.static_variables);
+ }
+ ZEND_MAP_PTR_INIT(new_function->op_array.static_variables_ptr, &new_function->op_array.static_variables);
}
return new_function;
}
@@ -90,95 +85,81 @@ static zend_function *zend_duplicate_function(zend_function *func, zend_class_en
static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
{
- ZEND_ASSERT(ce->parent != NULL);
+ zend_class_entry *parent = ce->parent;
+
+ ZEND_ASSERT(parent != NULL);
/* You cannot change create_object */
- ce->create_object = ce->parent->create_object;
+ ce->create_object = parent->create_object;
/* Inherit special functions if needed */
if (EXPECTED(!ce->get_iterator)) {
- ce->get_iterator = ce->parent->get_iterator;
+ ce->get_iterator = parent->get_iterator;
}
- if (EXPECTED(!ce->iterator_funcs_ptr) && UNEXPECTED(ce->parent->iterator_funcs_ptr)) {
- if (ce->type == ZEND_INTERNAL_CLASS) {
- ce->iterator_funcs_ptr = calloc(1, sizeof(zend_class_iterator_funcs));
- if (ce->parent->iterator_funcs_ptr->zf_new_iterator) {
- ce->iterator_funcs_ptr->zf_new_iterator = zend_hash_str_find_ptr(&ce->function_table, "getiterator", sizeof("getiterator") - 1);
- }
- if (ce->parent->iterator_funcs_ptr->zf_current) {
- ce->iterator_funcs_ptr->zf_rewind = zend_hash_str_find_ptr(&ce->function_table, "rewind", sizeof("rewind") - 1);
- ce->iterator_funcs_ptr->zf_valid = zend_hash_str_find_ptr(&ce->function_table, "valid", sizeof("valid") - 1);
- ce->iterator_funcs_ptr->zf_key = zend_hash_str_find_ptr(&ce->function_table, "key", sizeof("key") - 1);
- ce->iterator_funcs_ptr->zf_current = zend_hash_str_find_ptr(&ce->function_table, "current", sizeof("current") - 1);
- ce->iterator_funcs_ptr->zf_next = zend_hash_str_find_ptr(&ce->function_table, "next", sizeof("next") - 1);
- }
- } else {
- ce->iterator_funcs_ptr = zend_arena_alloc(&CG(arena), sizeof(zend_class_iterator_funcs));
- memset(ce->iterator_funcs_ptr, 0, sizeof(zend_class_iterator_funcs));
- }
+ if (parent->iterator_funcs_ptr) {
+ /* Must be initialized through iface->interface_gets_implemented() */
+ ZEND_ASSERT(ce->iterator_funcs_ptr);
}
if (EXPECTED(!ce->__get)) {
- ce->__get = ce->parent->__get;
+ ce->__get = parent->__get;
}
if (EXPECTED(!ce->__set)) {
- ce->__set = ce->parent->__set;
+ ce->__set = parent->__set;
}
if (EXPECTED(!ce->__unset)) {
- ce->__unset = ce->parent->__unset;
+ ce->__unset = parent->__unset;
}
if (EXPECTED(!ce->__isset)) {
- ce->__isset = ce->parent->__isset;
+ ce->__isset = parent->__isset;
}
if (EXPECTED(!ce->__call)) {
- ce->__call = ce->parent->__call;
+ ce->__call = parent->__call;
}
if (EXPECTED(!ce->__callstatic)) {
- ce->__callstatic = ce->parent->__callstatic;
+ ce->__callstatic = parent->__callstatic;
}
if (EXPECTED(!ce->__tostring)) {
- ce->__tostring = ce->parent->__tostring;
+ ce->__tostring = parent->__tostring;
}
if (EXPECTED(!ce->clone)) {
- ce->clone = ce->parent->clone;
+ ce->clone = parent->clone;
}
if (EXPECTED(!ce->serialize)) {
- ce->serialize = ce->parent->serialize;
+ ce->serialize = parent->serialize;
}
if (EXPECTED(!ce->unserialize)) {
- ce->unserialize = ce->parent->unserialize;
+ ce->unserialize = parent->unserialize;
}
if (!ce->destructor) {
- ce->destructor = ce->parent->destructor;
+ ce->destructor = parent->destructor;
}
if (EXPECTED(!ce->__debugInfo)) {
- ce->__debugInfo = ce->parent->__debugInfo;
+ ce->__debugInfo = parent->__debugInfo;
}
if (ce->constructor) {
- if (ce->parent->constructor && UNEXPECTED(ce->parent->constructor->common.fn_flags & ZEND_ACC_FINAL)) {
+ if (parent->constructor && UNEXPECTED(parent->constructor->common.fn_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_ERROR, "Cannot override final %s::%s() with %s::%s()",
- ZSTR_VAL(ce->parent->name), ZSTR_VAL(ce->parent->constructor->common.function_name),
+ ZSTR_VAL(parent->name), ZSTR_VAL(parent->constructor->common.function_name),
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
return;
}
- ce->constructor = ce->parent->constructor;
+ ce->constructor = parent->constructor;
}
/* }}} */
char *zend_visibility_string(uint32_t fn_flags) /* {{{ */
{
- if (fn_flags & ZEND_ACC_PRIVATE) {
- return "private";
- }
- if (fn_flags & ZEND_ACC_PROTECTED) {
- return "protected";
- }
if (fn_flags & ZEND_ACC_PUBLIC) {
return "public";
+ } else if (fn_flags & ZEND_ACC_PRIVATE) {
+ return "private";
+ } else {
+ ZEND_ASSERT(fn_flags & ZEND_ACC_PROTECTED);
+ return "protected";
}
- return "";
}
/* }}} */
@@ -284,14 +265,14 @@ static zend_bool zend_do_perform_implementation_check(const zend_function *fe, c
* we still need to do the arg number checks. We are only willing to ignore this for internal
* functions because extensions don't always define arg_info.
*/
- if (!proto || (!proto->common.arg_info && proto->common.type != ZEND_USER_FUNCTION)) {
+ if (!proto->common.arg_info && proto->common.type != ZEND_USER_FUNCTION) {
return 1;
}
/* Checks for constructors only if they are declared in an interface,
* or explicitly marked as abstract
*/
- if ((fe->common.fn_flags & ZEND_ACC_CTOR)
+ if ((fe->common.scope->constructor == fe)
&& ((proto->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0
&& (proto->common.fn_flags & ZEND_ACC_ABSTRACT) == 0)) {
return 1;
@@ -559,7 +540,7 @@ static ZEND_COLD zend_string *zend_get_function_declaration(const zend_function
}
/* }}} */
-static void do_inheritance_check_on_method(zend_function *child, zend_function *parent) /* {{{ */
+static void do_inheritance_check_on_method(zend_function *child, zend_function *parent, zend_class_entry *ce, zval *child_zv) /* {{{ */
{
uint32_t child_flags;
uint32_t parent_flags = parent->common.fn_flags;
@@ -584,55 +565,80 @@ static void do_inheritance_check_on_method(zend_function *child, zend_function *
zend_error_noreturn(E_COMPILE_ERROR, "Cannot make non abstract method %s::%s() abstract in class %s", ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
}
- /* Prevent derived classes from restricting access that was available in parent classes (except deriving from non-abstract ctors) */
- if (UNEXPECTED((!(child_flags & ZEND_ACC_CTOR) || (parent_flags & (ZEND_ACC_ABSTRACT | ZEND_ACC_IMPLEMENTED_ABSTRACT))) &&
- (child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK))) {
- zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(child), ZSTR_VAL(child->common.function_name), zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
- }
-
- if ((child_flags & ZEND_ACC_PRIVATE) < (parent_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED))) {
+ if (parent_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED)) {
child->common.fn_flags |= ZEND_ACC_CHANGED;
}
- if (parent_flags & ZEND_ACC_PRIVATE) {
- child->common.prototype = NULL;
- } else if (parent_flags & ZEND_ACC_ABSTRACT) {
- child->common.fn_flags |= ZEND_ACC_IMPLEMENTED_ABSTRACT;
- child->common.prototype = parent;
- } else if (!(parent->common.fn_flags & ZEND_ACC_CTOR)) {
- child->common.prototype = parent->common.prototype ? parent->common.prototype : parent;
- } else if (parent->common.prototype && (parent->common.prototype->common.scope->ce_flags & ZEND_ACC_INTERFACE)) {
- /* ctors only have a prototype if it comes from an interface */
- child->common.prototype = parent->common.prototype ? parent->common.prototype : parent;
- /* and if that is the case, we want to check inheritance against it */
- parent = child->common.prototype;
- }
-
- if (UNEXPECTED(!zend_do_perform_implementation_check(child, parent))) {
- int error_level;
- const char *error_verb;
- zend_string *method_prototype = zend_get_function_declaration(parent);
- zend_string *child_prototype = zend_get_function_declaration(child);
-
- if (child->common.prototype && (
- child->common.prototype->common.fn_flags & ZEND_ACC_ABSTRACT
- )) {
- error_level = E_COMPILE_ERROR;
- error_verb = "must";
- } else if ((parent->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) &&
- (!(child->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
- !zend_do_perform_type_hint_check(child, child->common.arg_info - 1, parent, parent->common.arg_info - 1) ||
- (ZEND_TYPE_ALLOW_NULL(child->common.arg_info[-1].type) && !ZEND_TYPE_ALLOW_NULL(parent->common.arg_info[-1].type)))) {
- error_level = E_COMPILE_ERROR;
- error_verb = "must";
- } else {
- error_level = E_WARNING;
- error_verb = "should";
+ do {
+ if (!(parent_flags & ZEND_ACC_PRIVATE)) {
+ zend_function *proto = parent->common.prototype ?
+ parent->common.prototype : parent;
+
+ if (parent->common.scope->constructor != parent) {
+ if (!proto) {
+ proto = parent;
+ }
+ } else if (proto) {
+ if (proto->common.scope->ce_flags & ZEND_ACC_INTERFACE) {
+ /* ctors only have a prototype if it comes from an interface */
+ /* and if that is the case, we want to check inheritance against it */
+ parent = proto;
+ } else if (!(proto->common.fn_flags & ZEND_ACC_ABSTRACT)) {
+ break;
+ }
+ } else {
+ break;
+ }
+ if (child_zv && child->common.prototype != proto) {
+ do {
+ if (child->common.scope != ce
+ && child->type == ZEND_USER_FUNCTION
+ && !child->op_array.static_variables) {
+ if (ce->ce_flags & ZEND_ACC_INTERFACE) {
+ /* Few parent interfaces contain the same method */
+ break;
+ } else {
+ /* op_array wasn't duplicated yet */
+ zend_function *new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
+ memcpy(new_function, child, sizeof(zend_op_array));
+ Z_PTR_P(child_zv) = child = new_function;
+ }
+ }
+ child->common.prototype = proto;
+ } while (0);
+ }
+ /* Prevent derived classes from restricting access that was available in parent classes (except deriving from non-abstract ctors) */
+ if ((child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(child), ZSTR_VAL(child->common.function_name), zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
+ }
+
+ if (UNEXPECTED(!zend_do_perform_implementation_check(child, parent))) {
+ int error_level;
+ const char *error_verb;
+ zend_string *method_prototype = zend_get_function_declaration(parent);
+ zend_string *child_prototype = zend_get_function_declaration(child);
+
+ if (child->common.prototype && (
+ child->common.prototype->common.fn_flags & ZEND_ACC_ABSTRACT
+ )) {
+ error_level = E_COMPILE_ERROR;
+ error_verb = "must";
+ } else if ((parent->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) &&
+ (!(child->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) ||
+ !zend_do_perform_type_hint_check(child, child->common.arg_info - 1, parent, parent->common.arg_info - 1) ||
+ (ZEND_TYPE_ALLOW_NULL(child->common.arg_info[-1].type) && !ZEND_TYPE_ALLOW_NULL(parent->common.arg_info[-1].type)))) {
+ error_level = E_COMPILE_ERROR;
+ error_verb = "must";
+ } else {
+ error_level = E_WARNING;
+ error_verb = "should";
+ }
+ zend_error(error_level, "Declaration of %s %s be compatible with %s", ZSTR_VAL(child_prototype), error_verb, ZSTR_VAL(method_prototype));
+ zend_string_efree(child_prototype);
+ zend_string_efree(method_prototype);
+ }
}
- zend_error(error_level, "Declaration of %s %s be compatible with %s", ZSTR_VAL(child_prototype), error_verb, ZSTR_VAL(method_prototype));
- zend_string_efree(child_prototype);
- zend_string_efree(method_prototype);
- }
+ } while (0);
}
/* }}} */
@@ -642,19 +648,13 @@ static zend_function *do_inherit_method(zend_string *key, zend_function *parent,
if (child) {
zend_function *func = (zend_function*)Z_PTR_P(child);
- zend_function *orig_prototype = func->common.prototype;
-
- do_inheritance_check_on_method(func, parent);
- if (func->common.prototype != orig_prototype &&
- func->type == ZEND_USER_FUNCTION &&
- func->common.scope != ce &&
- !func->op_array.static_variables) {
- /* Lazy duplication */
- zend_function *new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
- memcpy(new_function, func, sizeof(zend_op_array));
- Z_PTR_P(child) = new_function;
- func->common.prototype = orig_prototype;
+
+ if (UNEXPECTED(func == parent)) {
+ /* The same method in interface may be inherited few times */
+ return NULL;
}
+
+ do_inheritance_check_on_method(func, parent, ce, child);
return NULL;
}
@@ -666,6 +666,55 @@ static zend_function *do_inherit_method(zend_string *key, zend_function *parent,
}
/* }}} */
+zend_string* zend_resolve_property_type(zend_string *type, zend_class_entry *scope) /* {{{ */
+{
+ if (zend_string_equals_literal_ci(type, "parent")) {
+ if (scope && scope->parent) {
+ return scope->parent->name;
+ }
+ }
+
+ if (zend_string_equals_literal_ci(type, "self")) {
+ if (scope) {
+ return scope->name;
+ }
+ }
+
+ return type;
+} /* }}} */
+
+zend_bool property_types_compatible(zend_property_info *parent_info, zend_property_info *child_info) {
+ zend_string *parent_name, *child_name;
+ zend_class_entry *parent_type_ce, *child_type_ce;
+ if (parent_info->type == child_info->type) {
+ return 1;
+ }
+
+ if (!ZEND_TYPE_IS_CLASS(parent_info->type) || !ZEND_TYPE_IS_CLASS(child_info->type) ||
+ ZEND_TYPE_ALLOW_NULL(parent_info->type) != ZEND_TYPE_ALLOW_NULL(child_info->type)) {
+ return 0;
+ }
+
+ parent_name = ZEND_TYPE_IS_CE(parent_info->type)
+ ? ZEND_TYPE_CE(parent_info->type)->name
+ : zend_resolve_property_type(ZEND_TYPE_NAME(parent_info->type), parent_info->ce);
+ child_name = ZEND_TYPE_IS_CE(child_info->type)
+ ? ZEND_TYPE_CE(child_info->type)->name
+ : zend_resolve_property_type(ZEND_TYPE_NAME(child_info->type), child_info->ce);
+ if (zend_string_equals_ci(parent_name, child_name)) {
+ return 1;
+ }
+
+ /* Check for class aliases */
+ parent_type_ce = ZEND_TYPE_IS_CE(parent_info->type)
+ ? ZEND_TYPE_CE(parent_info->type)
+ : zend_lookup_class(parent_name);
+ child_type_ce = ZEND_TYPE_IS_CE(child_info->type)
+ ? ZEND_TYPE_CE(child_info->type)
+ : zend_lookup_class(child_name);
+ return parent_type_ce && child_type_ce && parent_type_ce == child_type_ce;
+}
+
static void do_inherit_property(zend_property_info *parent_info, zend_string *key, zend_class_entry *ce) /* {{{ */
{
zval *child = zend_hash_find_ex(&ce->properties_info, key, 1);
@@ -673,19 +722,16 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke
if (UNEXPECTED(child)) {
child_info = Z_PTR_P(child);
- if (UNEXPECTED(parent_info->flags & (ZEND_ACC_PRIVATE|ZEND_ACC_SHADOW))) {
+ if (parent_info->flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED)) {
child_info->flags |= ZEND_ACC_CHANGED;
- } else {
+ }
+ if (!(parent_info->flags & ZEND_ACC_PRIVATE)) {
if (UNEXPECTED((parent_info->flags & ZEND_ACC_STATIC) != (child_info->flags & ZEND_ACC_STATIC))) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s%s::$%s as %s%s::$%s",
(parent_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", ZSTR_VAL(ce->parent->name), ZSTR_VAL(key),
(child_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", ZSTR_VAL(ce->name), ZSTR_VAL(key));
}
- if (parent_info->flags & ZEND_ACC_CHANGED) {
- child_info->flags |= ZEND_ACC_CHANGED;
- }
-
if (UNEXPECTED((child_info->flags & ZEND_ACC_PPP_MASK) > (parent_info->flags & ZEND_ACC_PPP_MASK))) {
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::$%s must be %s (as in class %s)%s", ZSTR_VAL(ce->name), ZSTR_VAL(key), zend_visibility_string(parent_info->flags), ZSTR_VAL(ce->parent->name), (parent_info->flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
} else if ((child_info->flags & ZEND_ACC_STATIC) == 0) {
@@ -698,22 +744,32 @@ static void do_inherit_property(zend_property_info *parent_info, zend_string *ke
ZVAL_UNDEF(&ce->default_properties_table[child_num]);
child_info->offset = parent_info->offset;
}
+
+ if (UNEXPECTED(ZEND_TYPE_IS_SET(parent_info->type))) {
+ if (!property_types_compatible(parent_info, child_info)) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "Type of %s::$%s must be %s%s (as in class %s)",
+ ZSTR_VAL(ce->name),
+ ZSTR_VAL(key),
+ ZEND_TYPE_ALLOW_NULL(parent_info->type) ? "?" : "",
+ ZEND_TYPE_IS_CLASS(parent_info->type)
+ ? ZSTR_VAL(ZEND_TYPE_IS_CE(parent_info->type) ? ZEND_TYPE_CE(parent_info->type)->name : zend_resolve_property_type(ZEND_TYPE_NAME(parent_info->type), parent_info->ce))
+ : zend_get_type_by_const(ZEND_TYPE_CODE(parent_info->type)),
+ ZSTR_VAL(ce->parent->name));
+ }
+ } else if (UNEXPECTED(ZEND_TYPE_IS_SET(child_info->type) && !ZEND_TYPE_IS_SET(parent_info->type))) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "Type of %s::$%s must not be defined (as in class %s)",
+ ZSTR_VAL(ce->name),
+ ZSTR_VAL(key),
+ ZSTR_VAL(ce->parent->name));
+ }
}
} else {
- if (UNEXPECTED(parent_info->flags & ZEND_ACC_PRIVATE)) {
- if (UNEXPECTED(ce->type & ZEND_INTERNAL_CLASS)) {
- child_info = zend_duplicate_property_info_internal(parent_info);
- } else {
- child_info = zend_duplicate_property_info(parent_info);
- }
- child_info->flags &= ~ZEND_ACC_PRIVATE; /* it's not private anymore */
- child_info->flags |= ZEND_ACC_SHADOW; /* but it's a shadow of private */
+ if (UNEXPECTED(ce->type & ZEND_INTERNAL_CLASS)) {
+ child_info = zend_duplicate_property_info_internal(parent_info);
} else {
- if (UNEXPECTED(ce->type & ZEND_INTERNAL_CLASS)) {
- child_info = zend_duplicate_property_info_internal(parent_info);
- } else {
- child_info = parent_info;
- }
+ child_info = parent_info;
}
_zend_hash_append_ptr(&ce->properties_info, key, child_info);
}
@@ -731,15 +787,12 @@ static inline void do_implement_interface(zend_class_entry *ce, zend_class_entry
}
/* }}} */
-ZEND_API void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_entry *iface) /* {{{ */
+static void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_entry *iface) /* {{{ */
{
/* expects interface to be contained in ce's interface list already */
uint32_t i, ce_num, if_num = iface->num_interfaces;
zend_class_entry *entry;
- if (if_num==0) {
- return;
- }
ce_num = ce->num_interfaces;
if (ce->type == ZEND_INTERNAL_CLASS) {
@@ -793,6 +846,42 @@ static void do_inherit_class_constant(zend_string *name, zend_class_constant *pa
}
/* }}} */
+void zend_build_properties_info_table(zend_class_entry *ce)
+{
+ zend_property_info **table, *prop;
+ if (ce->default_properties_count == 0) {
+ return;
+ }
+
+ ZEND_ASSERT(ce->properties_info_table == NULL);
+ if (ce->type == ZEND_USER_CLASS) {
+ ce->properties_info_table = table = zend_arena_alloc(&CG(arena),
+ sizeof(zend_property_info *) * ce->default_properties_count);
+ } else {
+ ce->properties_info_table = table = pemalloc(
+ sizeof(zend_property_info *) * ce->default_properties_count, 1);
+ }
+
+ if (ce->parent && ce->parent->default_properties_count != 0) {
+ zend_property_info **parent_table = ce->parent->properties_info_table;
+ memcpy(
+ table, parent_table,
+ sizeof(zend_property_info *) * ce->parent->default_properties_count
+ );
+
+ /* Child did not add any new properties, we are done */
+ if (ce->default_properties_count == ce->parent->default_properties_count) {
+ return;
+ }
+ }
+
+ ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop) {
+ if (prop->ce == ce && (prop->flags & ZEND_ACC_STATIC) == 0) {
+ table[OBJ_PROP_TO_NUM(prop->offset)] = prop;
+ }
+ } ZEND_HASH_FOREACH_END();
+}
+
ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent_ce) /* {{{ */
{
zend_property_info *property_info;
@@ -818,10 +907,23 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
}
}
+ if (ce->parent_name) {
+ zend_string_release_ex(ce->parent_name, 0);
+ }
ce->parent = parent_ce;
/* Inherit interfaces */
- zend_do_inherit_interfaces(ce, parent_ce);
+ if (parent_ce->num_interfaces) {
+ if (!(ce->ce_flags & ZEND_ACC_IMPLEMENT_INTERFACES)) {
+ zend_do_inherit_interfaces(ce, parent_ce);
+ } else {
+ uint32_t i;
+
+ for (i = 0; i < parent_ce->num_interfaces; i++) {
+ do_implement_interface(ce, parent_ce->interfaces[i]);
+ }
+ }
+ }
/* Inherit properties */
if (parent_ce->default_properties_count) {
@@ -911,7 +1013,11 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
}
} while (dst != end);
} else if (ce->type == ZEND_USER_CLASS) {
- src = parent_ce->default_static_members_table + parent_ce->default_static_members_count;
+ if (CE_STATIC_MEMBERS(parent_ce) == NULL) {
+ ZEND_ASSERT(parent_ce->ce_flags & ZEND_ACC_IMMUTABLE);
+ zend_class_init_statics(parent_ce);
+ }
+ src = CE_STATIC_MEMBERS(parent_ce) + parent_ce->default_static_members_count;
do {
dst--;
src--;
@@ -937,8 +1043,14 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
} while (dst != end);
}
ce->default_static_members_count += parent_ce->default_static_members_count;
- if (ce->type == ZEND_USER_CLASS) {
- ce->static_members_table = ce->default_static_members_table;
+ if (!ZEND_MAP_PTR(ce->static_members_table)) {
+ ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
+ if (!EG(current_execute_data)) {
+ ZEND_MAP_PTR_NEW(ce->static_members_table);
+ } else {
+ /* internal class loaded by dl() */
+ ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
+ }
}
}
@@ -990,13 +1102,12 @@ ZEND_API void zend_do_inheritance(zend_class_entry *ce, zend_class_entry *parent
do_inherit_parent_constructor(ce);
- if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS && ce->type == ZEND_INTERNAL_CLASS) {
- ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
- } else if (!(ce->ce_flags & (ZEND_ACC_IMPLEMENT_INTERFACES|ZEND_ACC_IMPLEMENT_TRAITS))) {
- /* The verification will be done in runtime by ZEND_VERIFY_ABSTRACT_CLASS */
- zend_verify_abstract_class(ce);
+ if (ce->type == ZEND_INTERNAL_CLASS) {
+ if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
+ ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
+ }
}
- ce->ce_flags |= parent_ce->ce_flags & (ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_USE_GUARDS);
+ ce->ce_flags |= parent_ce->ce_flags & (ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_USE_GUARDS);
}
/* }}} */
@@ -1042,6 +1153,8 @@ ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry
zend_string *key;
zend_class_constant *c;
+ ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
+
for (i = 0; i < ce->num_interfaces; i++) {
if (ce->interfaces[i] == NULL) {
memmove(ce->interfaces + i, ce->interfaces + i + 1, sizeof(zend_class_entry*) * (--ce->num_interfaces - i));
@@ -1082,48 +1195,93 @@ ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry
} ZEND_HASH_FOREACH_END();
do_implement_interface(ce, iface);
- zend_do_inherit_interfaces(ce, iface);
+ if (iface->num_interfaces) {
+ zend_do_inherit_interfaces(ce, iface);
+ }
}
}
/* }}} */
-ZEND_API void zend_do_implement_trait(zend_class_entry *ce, zend_class_entry *trait) /* {{{ */
+static void zend_do_implement_interfaces(zend_class_entry *ce) /* {{{ */
{
- uint32_t i, ignore = 0;
- uint32_t current_trait_num = ce->num_traits;
- uint32_t parent_trait_num = ce->parent ? ce->parent->num_traits : 0;
+ zend_class_entry **interfaces, *iface;
+ uint32_t num_interfaces = 0;
+ zend_function *func;
+ zend_string *key;
+ zend_class_constant *c;
+ uint32_t i, j;
- for (i = 0; i < ce->num_traits; i++) {
- if (ce->traits[i] == NULL) {
- memmove(ce->traits + i, ce->traits + i + 1, sizeof(zend_class_entry*) * (--ce->num_traits - i));
- i--;
- } else if (ce->traits[i] == trait) {
- if (i < parent_trait_num) {
- ignore = 1;
- }
- }
+ if (ce->parent && ce->parent->num_interfaces) {
+ interfaces = emalloc(sizeof(zend_class_entry*) * (ce->parent->num_interfaces + ce->num_interfaces));
+ memcpy(interfaces, ce->parent->interfaces, sizeof(zend_class_entry*) * ce->parent->num_interfaces);
+ num_interfaces = ce->parent->num_interfaces;
+ } else {
+ interfaces = emalloc(sizeof(zend_class_entry*) * ce->num_interfaces);
}
- if (!ignore) {
- if (ce->num_traits >= current_trait_num) {
- if (ce->type == ZEND_INTERNAL_CLASS) {
- ce->traits = (zend_class_entry **) realloc(ce->traits, sizeof(zend_class_entry *) * (++current_trait_num));
- } else {
- ce->traits = (zend_class_entry **) erealloc(ce->traits, sizeof(zend_class_entry *) * (++current_trait_num));
+
+ for (i = 0; i < ce->num_interfaces; i++) {
+ iface = zend_fetch_class_by_name(ce->interface_names[i].name,
+ ce->interface_names[i].lc_name, ZEND_FETCH_CLASS_INTERFACE);
+ if (UNEXPECTED(iface == NULL)) {
+ return;
+ }
+ if (UNEXPECTED(!(iface->ce_flags & ZEND_ACC_INTERFACE))) {
+ efree(interfaces);
+ zend_error_noreturn(E_ERROR, "%s cannot implement %s - it is not an interface", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
+ return;
+ }
+ for (j = 0; j < num_interfaces; j++) {
+ if (interfaces[j] == iface) {
+ if (!ce->parent || j >= ce->parent->num_interfaces) {
+ efree(interfaces);
+ zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot implement previously implemented interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
+ return;
+ }
+ /* skip duplications */
+ ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) {
+ do_inherit_constant_check(&iface->constants_table, c, key, iface);
+ } ZEND_HASH_FOREACH_END();
+
+ iface = NULL;
+ break;
}
}
- ce->traits[ce->num_traits++] = trait;
+ if (iface) {
+ interfaces[num_interfaces] = iface;
+ num_interfaces++;
+ }
}
-}
-/* }}} */
-static zend_bool zend_traits_method_compatibility_check(zend_function *fn, zend_function *other_fn) /* {{{ */
-{
- uint32_t fn_flags = fn->common.scope->ce_flags;
- uint32_t other_flags = other_fn->common.scope->ce_flags;
+ for (i = 0; i < ce->num_interfaces; i++) {
+ zend_string_release_ex(ce->interface_names[i].name, 0);
+ zend_string_release_ex(ce->interface_names[i].lc_name, 0);
+ }
+ efree(ce->interface_names);
+
+ ce->num_interfaces = num_interfaces;
+ ce->interfaces = interfaces;
+
+ i = ce->parent ? ce->parent->num_interfaces : 0;
+ for (; i < ce->num_interfaces; i++) {
+ iface = ce->interfaces[i];
- return zend_do_perform_implementation_check(fn, other_fn)
- && ((fn_flags & (ZEND_ACC_FINAL|ZEND_ACC_STATIC)) ==
- (other_flags & (ZEND_ACC_FINAL|ZEND_ACC_STATIC))); /* equal final and static qualifier */
+ ZEND_HASH_FOREACH_STR_KEY_PTR(&iface->constants_table, key, c) {
+ do_inherit_iface_constant(key, c, ce, iface);
+ } ZEND_HASH_FOREACH_END();
+
+ ZEND_HASH_FOREACH_STR_KEY_PTR(&iface->function_table, key, func) {
+ zend_function *new_func = do_inherit_method(key, func, ce);
+
+ if (new_func) {
+ zend_hash_add_new_ptr(&ce->function_table, key, new_func);
+ }
+ } ZEND_HASH_FOREACH_END();
+
+ do_implement_interface(ce, iface);
+ if (iface->num_interfaces) {
+ zend_do_inherit_interfaces(ce, iface);
+ }
+ }
}
/* }}} */
@@ -1137,9 +1295,9 @@ static void zend_add_magic_methods(zend_class_entry* ce, zend_string* mname, zen
if (ce->constructor && (!ce->parent || ce->constructor != ce->parent->constructor)) {
zend_error_noreturn(E_COMPILE_ERROR, "%s has colliding constructor definitions coming from traits", ZSTR_VAL(ce->name));
}
- ce->constructor = fe; fe->common.fn_flags |= ZEND_ACC_CTOR;
+ ce->constructor = fe;
} else if (zend_string_equals_literal(mname, ZEND_DESTRUCTOR_FUNC_NAME)) {
- ce->destructor = fe; fe->common.fn_flags |= ZEND_ACC_DTOR;
+ ce->destructor = fe;
} else if (zend_string_equals_literal(mname, ZEND_GET_FUNC_NAME)) {
ce->__get = fe;
ce->ce_flags |= ZEND_ACC_USE_GUARDS;
@@ -1168,14 +1326,13 @@ static void zend_add_magic_methods(zend_class_entry* ce, zend_string* mname, zen
zend_error_noreturn(E_COMPILE_ERROR, "%s has colliding constructor definitions coming from traits", ZSTR_VAL(ce->name));
}
ce->constructor = fe;
- fe->common.fn_flags |= ZEND_ACC_CTOR;
}
zend_string_release_ex(lowercase_name, 0);
}
}
/* }}} */
-static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_string *key, zend_function *fn, HashTable **overriden) /* {{{ */
+static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_string *key, zend_function *fn, HashTable **overridden) /* {{{ */
{
zend_function *existing_fn = NULL;
zend_function *new_fn;
@@ -1191,12 +1348,12 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
if (existing_fn->common.scope == ce) {
/* members from the current class override trait methods */
- /* use temporary *overriden HashTable to detect hidden conflict */
- if (*overriden) {
- if ((existing_fn = zend_hash_find_ptr(*overriden, key)) != NULL) {
+ /* use temporary *overridden HashTable to detect hidden conflict */
+ if (*overridden) {
+ if ((existing_fn = zend_hash_find_ptr(*overridden, key)) != NULL) {
if (existing_fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* Make sure the trait method is compatible with previosly declared abstract method */
- if (UNEXPECTED(!zend_traits_method_compatibility_check(fn, existing_fn))) {
+ if (UNEXPECTED(!zend_do_perform_implementation_check(fn, existing_fn))) {
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
ZSTR_VAL(zend_get_function_declaration(fn)),
ZSTR_VAL(zend_get_function_declaration(existing_fn)));
@@ -1204,7 +1361,7 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
}
if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* Make sure the abstract declaration is compatible with previous declaration */
- if (UNEXPECTED(!zend_traits_method_compatibility_check(existing_fn, fn))) {
+ if (UNEXPECTED(!zend_do_perform_implementation_check(existing_fn, fn))) {
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
ZSTR_VAL(zend_get_function_declaration(existing_fn)),
ZSTR_VAL(zend_get_function_declaration(fn)));
@@ -1213,22 +1370,22 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
}
}
} else {
- ALLOC_HASHTABLE(*overriden);
- zend_hash_init_ex(*overriden, 8, NULL, overriden_ptr_dtor, 0, 0);
+ ALLOC_HASHTABLE(*overridden);
+ zend_hash_init_ex(*overridden, 8, NULL, overridden_ptr_dtor, 0, 0);
}
- zend_hash_update_mem(*overriden, key, fn, sizeof(zend_function));
+ zend_hash_update_mem(*overridden, key, fn, sizeof(zend_function));
return;
} else if (existing_fn->common.fn_flags & ZEND_ACC_ABSTRACT &&
(existing_fn->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0) {
/* Make sure the trait method is compatible with previosly declared abstract method */
- if (UNEXPECTED(!zend_traits_method_compatibility_check(fn, existing_fn))) {
+ if (UNEXPECTED(!zend_do_perform_implementation_check(fn, existing_fn))) {
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
ZSTR_VAL(zend_get_function_declaration(fn)),
ZSTR_VAL(zend_get_function_declaration(existing_fn)));
}
} else if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* Make sure the abstract declaration is compatible with previous declaration */
- if (UNEXPECTED(!zend_traits_method_compatibility_check(existing_fn, fn))) {
+ if (UNEXPECTED(!zend_do_perform_implementation_check(existing_fn, fn))) {
zend_error_noreturn(E_COMPILE_ERROR, "Declaration of %s must be compatible with %s",
ZSTR_VAL(zend_get_function_declaration(existing_fn)),
ZSTR_VAL(zend_get_function_declaration(fn)));
@@ -1248,12 +1405,11 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
} else {
/* inherited members are overridden by members inserted by traits */
/* check whether the trait method fulfills the inheritance requirements */
- do_inheritance_check_on_method(fn, existing_fn);
+ do_inheritance_check_on_method(fn, existing_fn, ce, NULL);
fn->common.prototype = NULL;
}
}
- function_add_ref(fn);
if (UNEXPECTED(fn->type == ZEND_INTERNAL_FUNCTION)) {
new_fn = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_fn, fn, sizeof(zend_internal_function));
@@ -1261,7 +1417,10 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s
} else {
new_fn = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_fn, fn, sizeof(zend_op_array));
+ new_fn->op_array.fn_flags |= ZEND_ACC_TRAIT_CLONE;
+ new_fn->op_array.fn_flags &= ~ZEND_ACC_IMMUTABLE;
}
+ function_add_ref(new_fn);
fn = zend_hash_update_ptr(&ce->function_table, key, new_fn);
zend_add_magic_methods(ce, key, fn);
}
@@ -1283,7 +1442,7 @@ static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /*
}
/* }}} */
-static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, zend_class_entry *ce, HashTable **overriden, HashTable *exclude_table, zend_class_entry **aliases) /* {{{ */
+static void zend_traits_copy_functions(zend_string *fnname, zend_function *fn, zend_class_entry *ce, HashTable **overridden, HashTable *exclude_table, zend_class_entry **aliases) /* {{{ */
{
zend_trait_alias *alias, **alias_ptr;
zend_string *lcname;
@@ -1309,7 +1468,7 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze
}
lcname = zend_string_tolower(alias->alias);
- zend_add_trait_method(ce, ZSTR_VAL(alias->alias), lcname, &fn_copy, overriden);
+ zend_add_trait_method(ce, ZSTR_VAL(alias->alias), lcname, &fn_copy, overridden);
zend_string_release_ex(lcname, 0);
/* Record the trait from which this alias was resolved. */
@@ -1361,14 +1520,12 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze
}
}
- zend_add_trait_method(ce, ZSTR_VAL(fn->common.function_name), fnname, &fn_copy, overriden);
+ zend_add_trait_method(ce, ZSTR_VAL(fn->common.function_name), fnname, &fn_copy, overridden);
}
-
- return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
-static uint32_t zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *trait) /* {{{ */
+static uint32_t zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *trait, zend_class_entry **traits) /* {{{ */
{
uint32_t i;
@@ -1378,7 +1535,7 @@ static uint32_t zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *t
}
for (i = 0; i < ce->num_traits; i++) {
- if (ce->traits[i] == trait) {
+ if (traits[i] == trait) {
return i;
}
}
@@ -1387,14 +1544,13 @@ static uint32_t zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *t
}
/* }}} */
-static void zend_traits_init_trait_structures(zend_class_entry *ce, HashTable ***exclude_tables_ptr, zend_class_entry ***aliases_ptr) /* {{{ */
+static void zend_traits_init_trait_structures(zend_class_entry *ce, zend_class_entry **traits, HashTable ***exclude_tables_ptr, zend_class_entry ***aliases_ptr) /* {{{ */
{
size_t i, j = 0;
zend_trait_precedence **precedences;
zend_trait_precedence *cur_precedence;
zend_trait_method_reference *cur_method_ref;
zend_string *lcname;
- zend_bool method_exists;
HashTable **exclude_tables = NULL;
zend_class_entry **aliases = NULL;
zend_class_entry *trait;
@@ -1413,12 +1569,11 @@ static void zend_traits_init_trait_structures(zend_class_entry *ce, HashTable **
if (!trait) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(cur_method_ref->class_name));
}
- zend_check_trait_usage(ce, trait);
+ zend_check_trait_usage(ce, trait, traits);
/** Ensure that the preferred method is actually available. */
lcname = zend_string_tolower(cur_method_ref->method_name);
- method_exists = zend_hash_exists(&trait->function_table, lcname);
- if (!method_exists) {
+ if (!zend_hash_exists(&trait->function_table, lcname)) {
zend_error_noreturn(E_COMPILE_ERROR,
"A precedence rule was defined for %s::%s but this method does not exist",
ZSTR_VAL(trait->name),
@@ -1440,7 +1595,7 @@ static void zend_traits_init_trait_structures(zend_class_entry *ce, HashTable **
if (!exclude_ce) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(class_name));
}
- trait_num = zend_check_trait_usage(ce, exclude_ce);
+ trait_num = zend_check_trait_usage(ce, exclude_ce, traits);
if (!exclude_tables[trait_num]) {
ALLOC_HASHTABLE(exclude_tables[trait_num]);
zend_hash_init(exclude_tables[trait_num], 0, NULL, NULL, 0);
@@ -1481,17 +1636,15 @@ static void zend_traits_init_trait_structures(zend_class_entry *ce, HashTable **
if (!trait) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(cur_method_ref->class_name));
}
- zend_check_trait_usage(ce, trait);
+ zend_check_trait_usage(ce, trait, traits);
aliases[i] = trait;
/** And, ensure that the referenced method is resolvable, too. */
lcname = zend_string_tolower(cur_method_ref->method_name);
- method_exists = zend_hash_exists(&trait->function_table, lcname);
- zend_string_release_ex(lcname, 0);
-
- if (!method_exists) {
+ if (!zend_hash_exists(&trait->function_table, lcname)) {
zend_error_noreturn(E_COMPILE_ERROR, "An alias was defined for %s::%s but this method does not exist", ZSTR_VAL(trait->name), ZSTR_VAL(cur_method_ref->method_name));
}
+ zend_string_release_ex(lcname, 0);
}
i++;
}
@@ -1502,31 +1655,35 @@ static void zend_traits_init_trait_structures(zend_class_entry *ce, HashTable **
}
/* }}} */
-static void zend_do_traits_method_binding(zend_class_entry *ce, HashTable **exclude_tables, zend_class_entry **aliases) /* {{{ */
+static void zend_do_traits_method_binding(zend_class_entry *ce, zend_class_entry **traits, HashTable **exclude_tables, zend_class_entry **aliases) /* {{{ */
{
uint32_t i;
- HashTable *overriden = NULL;
+ HashTable *overridden = NULL;
zend_string *key;
zend_function *fn;
if (exclude_tables) {
for (i = 0; i < ce->num_traits; i++) {
- /* copies functions, applies defined aliasing, and excludes unused trait methods */
- ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->traits[i]->function_table, key, fn) {
- zend_traits_copy_functions(key, fn, ce, &overriden, exclude_tables[i], aliases);
- } ZEND_HASH_FOREACH_END();
-
- if (exclude_tables[i]) {
- zend_hash_destroy(exclude_tables[i]);
- FREE_HASHTABLE(exclude_tables[i]);
- exclude_tables[i] = NULL;
+ if (traits[i]) {
+ /* copies functions, applies defined aliasing, and excludes unused trait methods */
+ ZEND_HASH_FOREACH_STR_KEY_PTR(&traits[i]->function_table, key, fn) {
+ zend_traits_copy_functions(key, fn, ce, &overridden, exclude_tables[i], aliases);
+ } ZEND_HASH_FOREACH_END();
+
+ if (exclude_tables[i]) {
+ zend_hash_destroy(exclude_tables[i]);
+ FREE_HASHTABLE(exclude_tables[i]);
+ exclude_tables[i] = NULL;
+ }
}
}
} else {
for (i = 0; i < ce->num_traits; i++) {
- ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->traits[i]->function_table, key, fn) {
- zend_traits_copy_functions(key, fn, ce, &overriden, NULL, aliases);
- } ZEND_HASH_FOREACH_END();
+ if (traits[i]) {
+ ZEND_HASH_FOREACH_STR_KEY_PTR(&traits[i]->function_table, key, fn) {
+ zend_traits_copy_functions(key, fn, ce, &overridden, NULL, aliases);
+ } ZEND_HASH_FOREACH_END();
+ }
}
}
@@ -1534,21 +1691,22 @@ static void zend_do_traits_method_binding(zend_class_entry *ce, HashTable **excl
zend_fixup_trait_method(fn, ce);
} ZEND_HASH_FOREACH_END();
- if (overriden) {
- zend_hash_destroy(overriden);
- FREE_HASHTABLE(overriden);
+ if (overridden) {
+ zend_hash_destroy(overridden);
+ FREE_HASHTABLE(overridden);
}
}
/* }}} */
-static zend_class_entry* find_first_definition(zend_class_entry *ce, size_t current_trait, zend_string *prop_name, zend_class_entry *coliding_ce) /* {{{ */
+static zend_class_entry* find_first_definition(zend_class_entry *ce, zend_class_entry **traits, size_t current_trait, zend_string *prop_name, zend_class_entry *coliding_ce) /* {{{ */
{
size_t i;
if (coliding_ce == ce) {
for (i = 0; i < current_trait; i++) {
- if (zend_hash_exists(&ce->traits[i]->properties_info, prop_name)) {
- return ce->traits[i];
+ if (traits[i]
+ && zend_hash_exists(&traits[i]->properties_info, prop_name)) {
+ return traits[i];
}
}
}
@@ -1557,7 +1715,7 @@ static zend_class_entry* find_first_definition(zend_class_entry *ce, size_t curr
}
/* }}} */
-static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */
+static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
{
size_t i;
zend_property_info *property_info;
@@ -1575,7 +1733,10 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */
* - if compatible, then strict notice
*/
for (i = 0; i < ce->num_traits; i++) {
- ZEND_HASH_FOREACH_PTR(&ce->traits[i]->properties_info, property_info) {
+ if (!traits[i]) {
+ continue;
+ }
+ ZEND_HASH_FOREACH_PTR(&traits[i]->properties_info, property_info) {
/* first get the unmangeld name if necessary,
* then check whether the property is already there
*/
@@ -1594,30 +1755,28 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */
/* next: check for conflicts with current class */
if ((coliding_prop = zend_hash_find_ptr(&ce->properties_info, prop_name)) != NULL) {
- if (coliding_prop->flags & ZEND_ACC_SHADOW) {
- zend_string_release_ex(coliding_prop->name, 0);
- if (coliding_prop->doc_comment) {
- zend_string_release_ex(coliding_prop->doc_comment, 0);
- }
+ if ((coliding_prop->flags & ZEND_ACC_PRIVATE) && coliding_prop->ce != ce) {
zend_hash_del(&ce->properties_info, prop_name);
flags |= ZEND_ACC_CHANGED;
} else {
not_compatible = 1;
if ((coliding_prop->flags & (ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC))
- == (flags & (ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC))) {
+ == (flags & (ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC)) &&
+ property_types_compatible(property_info, coliding_prop)
+ ) {
/* the flags are identical, thus, the properties may be compatible */
zval *op1, *op2;
zval op1_tmp, op2_tmp;
if (flags & ZEND_ACC_STATIC) {
op1 = &ce->default_static_members_table[coliding_prop->offset];
- op2 = &ce->traits[i]->default_static_members_table[property_info->offset];
+ op2 = &traits[i]->default_static_members_table[property_info->offset];
ZVAL_DEINDIRECT(op1);
ZVAL_DEINDIRECT(op2);
} else {
op1 = &ce->default_properties_table[OBJ_PROP_TO_NUM(coliding_prop->offset)];
- op2 = &ce->traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
+ op2 = &traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
}
/* if any of the values is a constant, we try to resolve it */
@@ -1645,7 +1804,7 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */
if (not_compatible) {
zend_error_noreturn(E_COMPILE_ERROR,
"%s and %s define the same property ($%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed",
- ZSTR_VAL(find_first_definition(ce, i, prop_name, coliding_prop->ce)->name),
+ ZSTR_VAL(find_first_definition(ce, traits, i, prop_name, coliding_prop->ce)->name),
ZSTR_VAL(property_info->ce->name),
ZSTR_VAL(prop_name),
ZSTR_VAL(ce->name));
@@ -1658,17 +1817,15 @@ static void zend_do_traits_property_binding(zend_class_entry *ce) /* {{{ */
/* property not found, so lets add it */
if (flags & ZEND_ACC_STATIC) {
- prop_value = &ce->traits[i]->default_static_members_table[property_info->offset];
+ prop_value = &traits[i]->default_static_members_table[property_info->offset];
ZEND_ASSERT(Z_TYPE_P(prop_value) != IS_INDIRECT);
} else {
- prop_value = &ce->traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
+ prop_value = &traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
}
Z_TRY_ADDREF_P(prop_value);
doc_comment = property_info->doc_comment ? zend_string_copy(property_info->doc_comment) : NULL;
- zend_declare_property_ex(ce, prop_name,
- prop_value, flags,
- doc_comment);
+ zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, property_info->type);
zend_string_release_ex(prop_name, 0);
} ZEND_HASH_FOREACH_END();
}
@@ -1725,20 +1882,42 @@ static void zend_do_check_for_inconsistent_traits_aliasing(zend_class_entry *ce,
}
/* }}} */
-ZEND_API void zend_do_bind_traits(zend_class_entry *ce) /* {{{ */
+static void zend_do_bind_traits(zend_class_entry *ce) /* {{{ */
{
HashTable **exclude_tables;
zend_class_entry **aliases;
+ zend_class_entry **traits, *trait;
+ uint32_t i, j;
- if (ce->num_traits == 0) {
- return;
+ ZEND_ASSERT(ce->num_traits > 0);
+
+ traits = emalloc(sizeof(zend_class_entry*) * ce->num_traits);
+
+ for (i = 0; i < ce->num_traits; i++) {
+ trait = zend_fetch_class_by_name(ce->trait_names[i].name,
+ ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
+ if (UNEXPECTED(trait == NULL)) {
+ return;
+ }
+ if (UNEXPECTED(!(trait->ce_flags & ZEND_ACC_TRAIT))) {
+ zend_error_noreturn(E_ERROR, "%s cannot use %s - it is not a trait", ZSTR_VAL(ce->name), ZSTR_VAL(trait->name));
+ return;
+ }
+ for (j = 0; j < i; j++) {
+ if (traits[j] == trait) {
+ /* skip duplications */
+ trait = NULL;
+ break;
+ }
+ }
+ traits[i] = trait;
}
/* complete initialization of trait strutures in ce */
- zend_traits_init_trait_structures(ce, &exclude_tables, &aliases);
+ zend_traits_init_trait_structures(ce, traits, &exclude_tables, &aliases);
/* first care about all methods to be flattened into the class */
- zend_do_traits_method_binding(ce, exclude_tables, aliases);
+ zend_do_traits_method_binding(ce, traits, exclude_tables, aliases);
/* Aliases which have not been applied indicate typos/bugs. */
zend_do_check_for_inconsistent_traits_aliasing(ce, aliases);
@@ -1752,18 +1931,12 @@ ZEND_API void zend_do_bind_traits(zend_class_entry *ce) /* {{{ */
}
/* then flatten the properties into it, to, mostly to notfiy developer about problems */
- zend_do_traits_property_binding(ce);
+ zend_do_traits_property_binding(ce, traits);
- /* verify that all abstract methods from traits have been implemented */
- zend_verify_abstract_class(ce);
+ efree(traits);
/* Emit E_DEPRECATED for PHP 4 constructors */
zend_check_deprecated_constructor(ce);
-
- /* now everything should be fine and an added ZEND_ACC_IMPLICIT_ABSTRACT_CLASS should be removed */
- if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
- ce->ce_flags -= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
- }
}
/* }}} */
@@ -1790,6 +1963,87 @@ void zend_check_deprecated_constructor(const zend_class_entry *ce) /* {{{ */
}
/* }}} */
+#define MAX_ABSTRACT_INFO_CNT 3
+#define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
+#define DISPLAY_ABSTRACT_FN(idx) \
+ ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
+ ai.afn[idx] ? "::" : "", \
+ ai.afn[idx] ? ZSTR_VAL(ai.afn[idx]->common.function_name) : "", \
+ ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
+
+typedef struct _zend_abstract_info {
+ zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
+ int cnt;
+ int ctor;
+} zend_abstract_info;
+
+static void zend_verify_abstract_class_function(zend_function *fn, zend_abstract_info *ai) /* {{{ */
+{
+ if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
+ if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
+ ai->afn[ai->cnt] = fn;
+ }
+ if (fn->common.scope->constructor == fn) {
+ if (!ai->ctor) {
+ ai->cnt++;
+ ai->ctor = 1;
+ } else {
+ ai->afn[ai->cnt] = NULL;
+ }
+ } else {
+ ai->cnt++;
+ }
+ }
+}
+/* }}} */
+
+void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
+{
+ zend_function *func;
+ zend_abstract_info ai;
+
+ ZEND_ASSERT((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS);
+ memset(&ai, 0, sizeof(ai));
+
+ ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
+ zend_verify_abstract_class_function(func, &ai);
+ } ZEND_HASH_FOREACH_END();
+
+ if (ai.cnt) {
+ zend_error_noreturn(E_ERROR, "Class %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
+ ZSTR_VAL(ce->name), ai.cnt,
+ ai.cnt > 1 ? "s" : "",
+ DISPLAY_ABSTRACT_FN(0),
+ DISPLAY_ABSTRACT_FN(1),
+ DISPLAY_ABSTRACT_FN(2)
+ );
+ } else {
+ /* now everything should be fine and an added ZEND_ACC_IMPLICIT_ABSTRACT_CLASS should be removed */
+ ce->ce_flags &= ~ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
+ }
+}
+/* }}} */
+
+ZEND_API void zend_do_link_class(zend_class_entry *ce, zend_class_entry *parent) /* {{{ */
+{
+ ce->ce_flags |= ZEND_ACC_LINKED;
+ if (parent) {
+ zend_do_inheritance(ce, parent);
+ }
+ if (ce->ce_flags & ZEND_ACC_IMPLEMENT_TRAITS) {
+ zend_do_bind_traits(ce);
+ }
+ if (ce->ce_flags & ZEND_ACC_IMPLEMENT_INTERFACES) {
+ zend_do_implement_interfaces(ce);
+ }
+ if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
+ zend_verify_abstract_class(ce);
+ }
+
+ zend_build_properties_info_table(ce);
+}
+/* }}} */
+
/*
* Local variables:
* tab-width: 4