Skip to content

Commit 6166ed0

Browse files
nikicGirgias
authored andcommitted
Code cleanup
1 parent 1446dec commit 6166ed0

File tree

3 files changed

+45
-74
lines changed

3 files changed

+45
-74
lines changed

Zend/zend_execute.c

Lines changed: 39 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -836,18 +836,15 @@ static zend_class_entry *resolve_single_class_type(zend_string *name, zend_class
836836
}
837837
}
838838

839-
// TODO better name
840-
static zend_always_inline zend_class_entry* zend_resolve_ce(
839+
static zend_always_inline zend_class_entry *zend_ce_from_type(
841840
zend_property_info *info, zend_type *type) {
842-
zend_class_entry *ce;
843-
zend_string *name = NULL;
844-
845841
if (UNEXPECTED(!ZEND_TYPE_HAS_NAME(*type))) {
842+
ZEND_ASSERT(ZEND_TYPE_HAS_CE(*type));
846843
return ZEND_TYPE_CE(*type);
847844
}
848845

849-
name = ZEND_TYPE_NAME(*type);
850-
846+
zend_string *name = ZEND_TYPE_NAME(*type);
847+
zend_class_entry *ce;
851848
if (ZSTR_HAS_CE_CACHE(name)) {
852849
ce = ZSTR_GET_CE_CACHE(name);
853850
if (!ce) {
@@ -865,50 +862,28 @@ static zend_always_inline zend_class_entry* zend_resolve_ce(
865862

866863
static bool zend_check_and_resolve_property_class_type(
867864
zend_property_info *info, zend_class_entry *object_ce) {
868-
zend_class_entry *ce;
869865
if (ZEND_TYPE_HAS_LIST(info->type)) {
870866
zend_type *list_type;
871-
872867
if (ZEND_TYPE_IS_INTERSECTION(info->type)) {
873868
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
874-
ce = zend_resolve_ce(info, list_type);
875-
876-
/* If we cannot resolve the CE we cannot check if it satisfies
877-
* the type constraint, fail. */
878-
if (ce == NULL) {
879-
return false;
880-
}
881-
882-
if (!instanceof_function(object_ce, ce)) {
869+
zend_class_entry *ce = zend_ce_from_type(info, list_type);
870+
if (!ce || !instanceof_function(object_ce, ce)) {
883871
return false;
884872
}
885873
} ZEND_TYPE_LIST_FOREACH_END();
886874
return true;
887875
} else {
888876
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
889-
ce = zend_resolve_ce(info, list_type);
890-
891-
/* If we cannot resolve the CE we cannot check if it satisfies
892-
* the type constraint, check the next one. */
893-
if (ce == NULL) {
894-
continue;
895-
}
896-
if (instanceof_function(object_ce, ce)) {
877+
zend_class_entry *ce = zend_ce_from_type(info, list_type);
878+
if (ce && instanceof_function(object_ce, ce)) {
897879
return true;
898880
}
899881
} ZEND_TYPE_LIST_FOREACH_END();
900882
return false;
901883
}
902884
} else {
903-
ce = zend_resolve_ce(info, &info->type);
904-
905-
/* If we cannot resolve the CE we cannot check if it satisfies
906-
* the type constraint, fail. */
907-
if (ce == NULL) {
908-
return false;
909-
}
910-
911-
return instanceof_function(object_ce, ce);
885+
zend_class_entry *ce = zend_ce_from_type(info, &info->type);
886+
return ce && instanceof_function(object_ce, ce);
912887
}
913888
}
914889

@@ -981,39 +956,38 @@ static zend_always_inline bool zend_value_instanceof_static(zval *zv) {
981956
# define HAVE_CACHE_SLOT 1
982957
#endif
983958

984-
static zend_always_inline zend_class_entry* zend_fetch_ce_from_cache_slot(void **cache_slot, zend_type *type)
959+
static zend_always_inline zend_class_entry *zend_fetch_ce_from_cache_slot(
960+
void **cache_slot, zend_type *type)
985961
{
986-
zend_class_entry *ce;
987-
988962
if (EXPECTED(HAVE_CACHE_SLOT && *cache_slot)) {
989-
ce = (zend_class_entry *) *cache_slot;
990-
} else {
991-
zend_string *name = ZEND_TYPE_NAME(*type);
992-
993-
if (ZSTR_HAS_CE_CACHE(name)) {
994-
ce = ZSTR_GET_CE_CACHE(name);
995-
if (!ce) {
996-
ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
997-
if (UNEXPECTED(!ce)) {
998-
/* Cannot resolve */
999-
return NULL;
1000-
}
1001-
}
1002-
} else {
1003-
ce = zend_fetch_class(name,
1004-
ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
963+
return (zend_class_entry *) *cache_slot;
964+
}
965+
966+
zend_string *name = ZEND_TYPE_NAME(*type);
967+
zend_class_entry *ce;
968+
if (ZSTR_HAS_CE_CACHE(name)) {
969+
ce = ZSTR_GET_CE_CACHE(name);
970+
if (!ce) {
971+
ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
1005972
if (UNEXPECTED(!ce)) {
973+
/* Cannot resolve */
1006974
return NULL;
1007975
}
1008976
}
1009-
if (HAVE_CACHE_SLOT) {
1010-
*cache_slot = (void *) ce;
977+
} else {
978+
ce = zend_fetch_class(name,
979+
ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
980+
if (UNEXPECTED(!ce)) {
981+
return NULL;
1011982
}
1012983
}
984+
if (HAVE_CACHE_SLOT) {
985+
*cache_slot = (void *) ce;
986+
}
1013987
return ce;
1014988
}
1015989

1016-
ZEND_API bool zend_check_type_slow(
990+
static zend_always_inline bool zend_check_type_slow(
1017991
zend_type *type, zval *arg, zend_reference *ref, void **cache_slot,
1018992
bool is_return_type, bool is_internal)
1019993
{
@@ -1025,16 +999,9 @@ ZEND_API bool zend_check_type_slow(
1025999
if (ZEND_TYPE_IS_INTERSECTION(*type)) {
10261000
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(*type), list_type) {
10271001
ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
1028-
/* If we cannot resolve the CE we cannot check if it satisfies
1029-
* the type constraint, fail. */
1030-
if (ce == NULL) {
1031-
return false;
1032-
}
1033-
1034-
/* Perform actual type check */
10351002
/* If type is not an instance of one of the types taking part in the
10361003
* intersection it cannot be a valid instance of the whole intersection type. */
1037-
if (!instanceof_function(Z_OBJCE_P(arg), ce)) {
1004+
if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce)) {
10381005
return false;
10391006
}
10401007
if (HAVE_CACHE_SLOT) {
@@ -1045,7 +1012,6 @@ ZEND_API bool zend_check_type_slow(
10451012
} else {
10461013
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(*type), list_type) {
10471014
ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
1048-
/* Perform actual type check if we have a CE */
10491015
/* Instance of a single type part of a union is sufficient to pass the type check */
10501016
if (ce && instanceof_function(Z_OBJCE_P(arg), ce)) {
10511017
return true;
@@ -1113,6 +1079,13 @@ static zend_always_inline bool zend_check_type(
11131079
return zend_check_type_slow(type, arg, ref, cache_slot, is_return_type, is_internal);
11141080
}
11151081

1082+
ZEND_API bool zend_check_user_type_slow(
1083+
zend_type *type, zval *arg, zend_reference *ref, void **cache_slot, bool is_return_type)
1084+
{
1085+
return zend_check_type_slow(
1086+
type, arg, ref, cache_slot, is_return_type, /* is_internal */ false);
1087+
}
1088+
11161089
static zend_always_inline bool zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot)
11171090
{
11181091
zend_arg_info *cur_arg_info;

Zend/zend_execute.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ ZEND_API ZEND_COLD void zend_verify_return_error(
7676
ZEND_API ZEND_COLD void zend_verify_never_error(
7777
const zend_function *zf);
7878
ZEND_API bool zend_verify_ref_array_assignable(zend_reference *ref);
79-
ZEND_API bool zend_check_type_slow(zend_type *type, zval *arg, zend_reference *ref, void **cache_slot,
80-
bool is_return_type, bool is_internal);
79+
ZEND_API bool zend_check_user_type_slow(
80+
zend_type *type, zval *arg, zend_reference *ref, void **cache_slot, bool is_return_type);
8181

8282

8383
#define ZEND_REF_TYPE_SOURCES(ref) \

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,8 @@ static bool ZEND_FASTCALL zend_jit_verify_arg_slow(zval *arg, zend_arg_info *arg
13251325
zend_execute_data *execute_data = EG(current_execute_data);
13261326
const zend_op *opline = EX(opline);
13271327
void **cache_slot = CACHE_ADDR(opline->extended_value);
1328-
bool ret;
1329-
1330-
ret = zend_check_type_slow(&arg_info->type, arg, /* ref */ NULL, cache_slot,
1331-
/* is_return_type */ false, /* is_internal */ false);
1328+
bool ret = zend_check_user_type_slow(
1329+
&arg_info->type, arg, /* ref */ NULL, cache_slot, /* is_return_type */ false);
13321330
if (UNEXPECTED(!ret)) {
13331331
zend_verify_arg_error(EX(func), arg_info, opline->op1.num, arg);
13341332
return 0;
@@ -1338,8 +1336,8 @@ static bool ZEND_FASTCALL zend_jit_verify_arg_slow(zval *arg, zend_arg_info *arg
13381336

13391337
static void ZEND_FASTCALL zend_jit_verify_return_slow(zval *arg, const zend_op_array *op_array, zend_arg_info *arg_info, void **cache_slot)
13401338
{
1341-
if (UNEXPECTED(!zend_check_type_slow(&arg_info->type, arg, /* ref */ NULL, cache_slot,
1342-
/* is_return_type */ true, /* is_internal */ false))) {
1339+
if (UNEXPECTED(!zend_check_user_type_slow(
1340+
&arg_info->type, arg, /* ref */ NULL, cache_slot, /* is_return_type */ true))) {
13431341
zend_verify_return_error((zend_function*)op_array, arg);
13441342
}
13451343
}

0 commit comments

Comments
 (0)