-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Use attribute validator for assigning flags #19465
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,7 @@ uint32_t zend_attribute_attribute_get_flags(zend_attribute *attr, zend_class_ent | |
} | ||
|
||
static void validate_allow_dynamic_properties( | ||
zend_attribute *attr, uint32_t target, zend_class_entry *scope) | ||
zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset) | ||
{ | ||
if (scope->ce_flags & ZEND_ACC_TRAIT) { | ||
zend_error_noreturn(E_ERROR, "Cannot apply #[\\AllowDynamicProperties] to trait %s", | ||
|
@@ -96,7 +96,7 @@ static void validate_allow_dynamic_properties( | |
} | ||
|
||
static void validate_attribute( | ||
zend_attribute *attr, uint32_t target, zend_class_entry *scope) | ||
zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset) | ||
{ | ||
const char *msg = NULL; | ||
if (scope->ce_flags & ZEND_ACC_TRAIT) { | ||
|
@@ -113,6 +113,51 @@ static void validate_attribute( | |
} | ||
} | ||
|
||
static void validate_override( | ||
zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset) | ||
{ | ||
if (target_type & ZEND_ATTRIBUTE_TARGET_METHOD) { | ||
zend_op_array *op_array = target; | ||
op_array->fn_flags |= ZEND_ACC_OVERRIDE; | ||
} else { | ||
ZEND_ASSERT(target_type & ZEND_ATTRIBUTE_TARGET_PROPERTY); | ||
zend_property_info *prop_info = target; | ||
prop_info->flags |= ZEND_ACC_OVERRIDE; | ||
} | ||
} | ||
|
||
static void validate_deprecated( | ||
zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset) | ||
{ | ||
if (target_type & (ZEND_ATTRIBUTE_TARGET_FUNCTION|ZEND_ATTRIBUTE_TARGET_METHOD)) { | ||
zend_op_array *op_array = target; | ||
op_array->fn_flags |= ZEND_ACC_DEPRECATED; | ||
} else if (target_type & (ZEND_ATTRIBUTE_TARGET_CLASS_CONST)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
} else if (target_type & (ZEND_ATTRIBUTE_TARGET_CLASS_CONST)) {
} else if (target_type & ZEND_ATTRIBUTE_TARGET_CLASS_CONST) {
|
||
zend_class_constant *c = target; | ||
ZEND_CLASS_CONST_FLAGS(c) |= ZEND_ACC_DEPRECATED; | ||
/* For deprecated constants, we need to flag the zval for recursion | ||
* detection. Make sure the zval is separated out of shm. */ | ||
scope->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after removing the separate
Suggested change
scope->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
c->ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
and similar for the removal of ZEND_ACC_CONSTANTS_UPDATED |
||
scope->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED; | ||
} else { | ||
ZEND_ASSERT(target_type & ZEND_ATTRIBUTE_TARGET_CONST); | ||
zend_constant *c = target; | ||
ZEND_CONSTANT_SET_FLAGS( | ||
c, | ||
ZEND_CONSTANT_FLAGS(c) | CONST_DEPRECATED, | ||
ZEND_CONSTANT_MODULE_NUMBER(c) | ||
); | ||
} | ||
} | ||
|
||
static void validate_no_discard( | ||
zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset) | ||
{ | ||
ZEND_ASSERT(target_type & (ZEND_ATTRIBUTE_TARGET_FUNCTION|ZEND_ATTRIBUTE_TARGET_METHOD)); | ||
zend_op_array *op_array = target; | ||
op_array->fn_flags |= ZEND_ACC_NODISCARD; | ||
} | ||
|
||
ZEND_METHOD(Attribute, __construct) | ||
{ | ||
zend_long flags = ZEND_ATTRIBUTE_TARGET_ALL; | ||
|
@@ -560,13 +605,16 @@ void zend_register_attribute_ce(void) | |
zend_ce_sensitive_parameter_value->default_object_handlers = &attributes_object_handlers_sensitive_parameter_value; | ||
|
||
zend_ce_override = register_class_Override(); | ||
zend_mark_internal_attribute(zend_ce_override); | ||
attr = zend_mark_internal_attribute(zend_ce_override); | ||
attr->validator = validate_override; | ||
|
||
zend_ce_deprecated = register_class_Deprecated(); | ||
attr = zend_mark_internal_attribute(zend_ce_deprecated); | ||
attr->validator = validate_deprecated; | ||
|
||
zend_ce_nodiscard = register_class_NoDiscard(); | ||
attr = zend_mark_internal_attribute(zend_ce_nodiscard); | ||
attr->validator = validate_no_discard; | ||
} | ||
|
||
void zend_attributes_shutdown(void) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,8 @@ typedef struct _zend_attribute { | |
typedef struct _zend_internal_attribute { | ||
zend_class_entry *ce; | ||
uint32_t flags; | ||
void (*validator)(zend_attribute *attr, uint32_t target, zend_class_entry *scope); | ||
/* Parameter offsets start at 1, everything else uses 0. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't need both |
||
void (*validator)(zend_attribute *attr, uint32_t target_type, zend_class_entry *scope, void *target, uint32_t offset); | ||
} zend_internal_attribute; | ||
|
||
ZEND_API zend_attribute *zend_get_attribute(HashTable *attributes, zend_string *lcname); | ||
|