Skip to content

Zend: Add MUTABLE zend_type foreach macros and const qualifiers #18256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/Optimizer/compact_literals.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static size_t type_num_classes(const zend_op_array *op_array, uint32_t arg_num)
}
ZEND_ASSERT(ZEND_TYPE_IS_UNION(arg_info->type));
size_t count = 0;
zend_type *list_type;
const zend_type *list_type;

ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(arg_info->type), list_type) {
if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
Expand Down
8 changes: 4 additions & 4 deletions Zend/Optimizer/dfa_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static void zend_ssa_remove_nops(zend_op_array *op_array, zend_ssa *ssa, zend_op
free_alloca(shiftlist, use_heap);
}

static bool safe_instanceof(zend_class_entry *ce1, zend_class_entry *ce2) {
static bool safe_instanceof(const zend_class_entry *ce1, const zend_class_entry *ce2) {
if (ce1 == ce2) {
return 1;
}
Expand All @@ -267,9 +267,9 @@ static bool safe_instanceof(zend_class_entry *ce1, zend_class_entry *ce2) {

static inline bool can_elide_list_type(
const zend_script *script, const zend_op_array *op_array,
const zend_ssa_var_info *use_info, zend_type type)
const zend_ssa_var_info *use_info, const zend_type type)
{
zend_type *single_type;
const zend_type *single_type;
/* For intersection: result==false is failure, default is success.
* For union: result==true is success, default is failure. */
bool is_intersection = ZEND_TYPE_IS_INTERSECTION(type);
Expand All @@ -280,7 +280,7 @@ static inline bool can_elide_list_type(
}
if (ZEND_TYPE_HAS_NAME(*single_type)) {
zend_string *lcname = zend_string_tolower(ZEND_TYPE_NAME(*single_type));
zend_class_entry *ce = zend_optimizer_get_class_entry(script, op_array, lcname);
const zend_class_entry *ce = zend_optimizer_get_class_entry(script, op_array, lcname);
zend_string_release(lcname);
bool result = ce && safe_instanceof(use_info->ce, ce);
if (result == !is_intersection) {
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -2914,14 +2914,14 @@ static zend_always_inline void zend_normalize_internal_type(zend_type *type) {
ZEND_ASSERT(!ZEND_TYPE_CONTAINS_CODE(*type, IS_RESOURCE) && "resource is not allowed in a zend_type");
}
zend_type *current;
ZEND_TYPE_FOREACH(*type, current) {
ZEND_TYPE_FOREACH_MUTABLE(*type, current) {
if (ZEND_TYPE_HAS_NAME(*current)) {
zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*current));
zend_alloc_ce_cache(name);
ZEND_TYPE_SET_PTR(*current, name);
} else if (ZEND_TYPE_HAS_LIST(*current)) {
zend_type *inner;
ZEND_TYPE_FOREACH(*current, inner) {
ZEND_TYPE_FOREACH_MUTABLE(*current, inner) {
ZEND_ASSERT(!ZEND_TYPE_HAS_LITERAL_NAME(*inner) && !ZEND_TYPE_HAS_LIST(*inner));
if (ZEND_TYPE_HAS_NAME(*inner)) {
zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*inner));
Expand Down
30 changes: 15 additions & 15 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1407,10 +1407,10 @@ static zend_string *resolve_class_name(zend_string *name, zend_class_entry *scop
}

static zend_string *add_intersection_type(zend_string *str,
zend_type_list *intersection_type_list, zend_class_entry *scope,
const zend_type_list *intersection_type_list, zend_class_entry *scope,
bool is_bracketed)
{
zend_type *single_type;
const zend_type *single_type;
zend_string *intersection_str = NULL;

ZEND_TYPE_LIST_FOREACH(intersection_type_list, single_type) {
Expand All @@ -1432,7 +1432,7 @@ static zend_string *add_intersection_type(zend_string *str,
return str;
}

zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope) {
zend_string *zend_type_to_string_resolved(const zend_type type, zend_class_entry *scope) {
zend_string *str = NULL;

/* Pure intersection type */
Expand All @@ -1441,7 +1441,7 @@ zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scop
str = add_intersection_type(str, ZEND_TYPE_LIST(type), scope, /* is_bracketed */ false);
} else if (ZEND_TYPE_HAS_LIST(type)) {
/* A union type might not be a list */
zend_type *list_type;
const zend_type *list_type;
ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
str = add_intersection_type(str, ZEND_TYPE_LIST(*list_type), scope, /* is_bracketed */ true);
Expand Down Expand Up @@ -1527,7 +1527,7 @@ ZEND_API zend_string *zend_type_to_string(zend_type type) {
return zend_type_to_string_resolved(type, NULL);
}

static bool is_generator_compatible_class_type(zend_string *name) {
static bool is_generator_compatible_class_type(const zend_string *name) {
return zend_string_equals_ci(name, ZSTR_KNOWN(ZEND_STR_TRAVERSABLE))
|| zend_string_equals_literal_ci(name, "Iterator")
|| zend_string_equals_literal_ci(name, "Generator");
Expand All @@ -1541,10 +1541,10 @@ static void zend_mark_function_as_generator(void) /* {{{ */
}

if (CG(active_op_array)->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
zend_type return_type = CG(active_op_array)->arg_info[-1].type;
const zend_type return_type = CG(active_op_array)->arg_info[-1].type;
bool valid_type = (ZEND_TYPE_FULL_MASK(return_type) & MAY_BE_OBJECT) != 0;
if (!valid_type) {
zend_type *single_type;
const zend_type *single_type;
ZEND_TYPE_FOREACH(return_type, single_type) {
if (ZEND_TYPE_HAS_NAME(*single_type)
&& is_generator_compatible_class_type(ZEND_TYPE_NAME(*single_type))) {
Expand Down Expand Up @@ -2621,7 +2621,7 @@ static void zend_compile_memoized_expr(znode *result, zend_ast *expr) /* {{{ */
/* }}} */

/* Remember to update type_num_classes() in compact_literals.c when changing this function */
static size_t zend_type_get_num_classes(zend_type type) {
static size_t zend_type_get_num_classes(const zend_type type) {
if (!ZEND_TYPE_IS_COMPLEX(type)) {
return 0;
}
Expand All @@ -2632,7 +2632,7 @@ static size_t zend_type_get_num_classes(zend_type type) {
}
ZEND_ASSERT(ZEND_TYPE_IS_UNION(type));
size_t count = 0;
zend_type *list_type;
const zend_type *list_type;

ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(type), list_type) {
if (ZEND_TYPE_IS_INTERSECTION(*list_type)) {
Expand Down Expand Up @@ -7048,7 +7048,7 @@ static zend_type zend_compile_single_typename(zend_ast *ast)
}
}

static void zend_are_intersection_types_redundant(zend_type left_type, zend_type right_type)
static void zend_are_intersection_types_redundant(const zend_type left_type, const zend_type right_type)
{
ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(left_type));
ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(right_type));
Expand All @@ -7067,9 +7067,9 @@ static void zend_are_intersection_types_redundant(zend_type left_type, zend_type
}

unsigned int sum = 0;
zend_type *outer_type;
const zend_type *outer_type;
ZEND_TYPE_LIST_FOREACH(smaller_type_list, outer_type)
zend_type *inner_type;
const zend_type *inner_type;
ZEND_TYPE_LIST_FOREACH(larger_type_list, inner_type)
if (zend_string_equals_ci(ZEND_TYPE_NAME(*inner_type), ZEND_TYPE_NAME(*outer_type))) {
sum++;
Expand Down Expand Up @@ -7098,12 +7098,12 @@ static void zend_are_intersection_types_redundant(zend_type left_type, zend_type
}
}

static void zend_is_intersection_type_redundant_by_single_type(zend_type intersection_type, zend_type single_type)
static void zend_is_intersection_type_redundant_by_single_type(const zend_type intersection_type, const zend_type single_type)
{
ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(intersection_type));
ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(single_type));

zend_type *single_intersection_type = NULL;
const zend_type *single_intersection_type = NULL;
ZEND_TYPE_FOREACH(intersection_type, single_intersection_type)
if (zend_string_equals_ci(ZEND_TYPE_NAME(*single_intersection_type), ZEND_TYPE_NAME(single_type))) {
zend_string *single_type_str = zend_type_to_string(single_type);
Expand All @@ -7115,7 +7115,7 @@ static void zend_is_intersection_type_redundant_by_single_type(zend_type interse
}

/* Used by both intersection and union types prior to transforming the type list to a full zend_type */
static void zend_is_type_list_redundant_by_single_type(zend_type_list *type_list, zend_type type)
static void zend_is_type_list_redundant_by_single_type(const zend_type_list *type_list, const zend_type type)
{
ZEND_ASSERT(!ZEND_TYPE_IS_INTERSECTION(type));
for (size_t i = 0; i < type_list->num_types - 1; i++) {
Expand Down
Loading
Loading