Skip to content

Commit bc2f4f7

Browse files
committed
Implemented many of the smaller changes suggested by Nikita Popov.
1 parent 4263b3b commit bc2f4f7

File tree

9 files changed

+30
-57
lines changed

9 files changed

+30
-57
lines changed

Zend/tests/attributes/002_rfcexample.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace {
3030
var_dump($attributes[0]->getArguments());
3131
var_dump($attributes[0]->newInstance());
3232
}
33+
?>
3334
--EXPECTF--
3435
string(28) "My\Attributes\SingleArgument"
3536
array(1) {

Zend/tests/attributes/004_name_resolution.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Foo {
2626
namespace {
2727
dump_attributes((new ReflectionFunction('Foo\foo'))->getAttributes());
2828
}
29+
?>
2930
--EXPECTF--
3031
array(1) {
3132
["Doctrine\ORM\Mapping\Entity"]=>

Zend/zend_ast.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,8 +2141,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
21412141
ast = zend_ast_create(ZEND_AST_CLASS_CONST_DECL_ATTRIBUTES, ast, attr);
21422142
ast->lineno = ast->child[0]->lineno;
21432143
break;
2144-
default:
2145-
zend_error_noreturn(E_COMPILE_ERROR, "Invalid use of attributes");
2144+
EMPTY_SWITCH_DEFAULT_CASE()
21462145
}
21472146

21482147
return ast;

Zend/zend_compile.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5722,18 +5722,18 @@ static void zend_compile_attribute(zval *v, zend_ast *ast) /* {{{ */
57225722
ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE);
57235723

57245724
array_init_size(v, 1 + (ast->child[1] ? zend_ast_get_list(ast->child[1])->children : 0));
5725-
add_next_index_str(v, zend_resolve_class_name(zend_ast_get_str(ast->child[0]), ZEND_NAME_NOT_FQ));
5725+
add_next_index_str(v, zend_resolve_class_name_ast(ast->child[0]));
57265726

57275727
if (ast->child[1]) {
5728-
zend_ast_list *list;
5728+
zend_ast_list *list = zend_ast_get_list(ast->child[1]);
57295729
uint32_t i;
57305730
zval tmp;
57315731

57325732
ZEND_ASSERT(ast->child[1]->kind == ZEND_AST_ARG_LIST);
57335733

57345734
ZVAL_NULL(&tmp);
57355735

5736-
for (list = zend_ast_get_list(ast->child[1]), i = 0; i < list->children; i++) {
5736+
for (i = 0; i < list->children; i++) {
57375737
zend_const_expr_to_zval(zend_hash_next_index_insert(Z_ARRVAL_P(v), &tmp), list->child[i]);
57385738
}
57395739
}
@@ -5744,11 +5744,10 @@ static HashTable *zend_compile_attributes(zend_ast *ast, int target) /* {{{ */
57445744
{
57455745
HashTable *attr;
57465746

5747-
zend_ast_list *list;
5747+
zend_ast_list *list = zend_ast_get_list(ast);
57485748
uint32_t i;
57495749

57505750
zval tmp;
5751-
zend_attributes_internal_validator validator = NULL;
57525751

57535752
ZVAL_NULL(&tmp);
57545753

@@ -5757,7 +5756,7 @@ static HashTable *zend_compile_attributes(zend_ast *ast, int target) /* {{{ */
57575756
ALLOC_HASHTABLE(attr);
57585757
zend_hash_init(attr, zend_ast_get_list(ast)->children, NULL, ZVAL_PTR_DTOR, 0);
57595758

5760-
for (list = zend_ast_get_list(ast), i = 0; i < list->children; i++) {
5759+
for (i = 0; i < list->children; i++) {
57615760
zend_ast *el = list->child[i];
57625761
zend_string *name;
57635762

@@ -5769,8 +5768,8 @@ static HashTable *zend_compile_attributes(zend_ast *ast, int target) /* {{{ */
57695768
name = zend_string_tolower(Z_STR_P(zend_hash_index_find(Z_ARRVAL(a), 0)));
57705769
x = zend_hash_find(attr, name);
57715770

5772-
// validate internal attribute
5773-
validator = (zend_attributes_internal_validator)zend_hash_find_ptr(&zend_attributes_internal_validators, name);
5771+
// Validate internal attribute
5772+
zend_attributes_internal_validator validator = zend_hash_find_ptr(&zend_attributes_internal_validators, name);
57745773

57755774
if (validator != NULL) {
57765775
validator(&a, target);
@@ -6559,7 +6558,7 @@ void zend_compile_prop_group(zend_ast *list) /* {{{ */
65596558
zend_compile_prop_decl(prop_ast, type_ast, list->attr, attributes);
65606559

65616560
if (attributes) {
6562-
zend_array_ptr_dtor(attributes);
6561+
zend_array_release(attributes);
65636562
}
65646563
}
65656564
/* }}} */
@@ -6612,7 +6611,7 @@ void zend_compile_class_const_decl(zend_ast *ast, zend_ast *attr_ast) /* {{{ */
66126611
}
66136612

66146613
if (attributes) {
6615-
zend_array_ptr_dtor(attributes);
6614+
zend_array_release(attributes);
66166615
}
66176616
}
66186617
/* }}} */

Zend/zend_inheritance.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,10 @@ static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_ent
20462046
doc_comment = property_info->doc_comment ? zend_string_copy(property_info->doc_comment) : NULL;
20472047
if (property_info->attributes) {
20482048
attributes = property_info->attributes;
2049-
GC_ADDREF(attributes);
2049+
2050+
if (!(GC_FLAGS(attributes) & IS_ARRAY_IMMUTABLE)) {
2051+
GC_ADDREF(attributes);
2052+
}
20502053
}
20512054
zend_type_copy_ctor(&property_info->type, /* persistent */ 0);
20522055
zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, attributes, property_info->type);

Zend/zend_language_parser.y

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,16 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
227227
/* Token used to force a parse error from the lexer */
228228
%token T_ERROR
229229

230-
%type <ast> top_statement namespace_name name statement annotated_statement function_declaration_statement
230+
%type <ast> top_statement namespace_name name statement function_declaration_statement
231231
%type <ast> class_declaration_statement trait_declaration_statement
232232
%type <ast> interface_declaration_statement interface_extends_list
233233
%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
234234
%type <ast> mixed_group_use_declaration use_declaration unprefixed_use_declaration
235235
%type <ast> unprefixed_use_declarations const_decl inner_statement
236236
%type <ast> expr optional_expr while_statement for_statement foreach_variable
237237
%type <ast> foreach_statement declare_statement finally_statement unset_variable variable
238-
%type <ast> extends_from annotated_parameter parameter optional_type_without_static argument global_var
239-
%type <ast> static_var class_statement annotated_class_statement trait_adaptation trait_precedence trait_alias
238+
%type <ast> extends_from parameter optional_type_without_static argument global_var
239+
%type <ast> static_var class_statement trait_adaptation trait_precedence trait_alias
240240
%type <ast> absolute_trait_method_reference trait_method_reference property echo_expr
241241
%type <ast> new_expr anonymous_class class_name class_name_reference simple_variable
242242
%type <ast> internal_functions_in_yacc
@@ -257,6 +257,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
257257
%type <ast> isset_variable type return_type type_expr type_without_static
258258
%type <ast> identifier type_expr_without_static union_type_without_static
259259
%type <ast> inline_function union_type
260+
%type <ast> annotated_statement annotated_class_statement annotated_parameter
260261
%type <ast> attribute_arguments attribute_decl attribute attributes
261262

262263
%type <num> returns_ref function fn is_reference is_variadic variable_modifiers

Zend/zend_opcode.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ ZEND_API void destroy_zend_class(zval *zv)
319319
zend_string_release_ex(prop_info->doc_comment, 0);
320320
}
321321
if (prop_info->attributes) {
322-
zend_array_ptr_dtor(prop_info->attributes);
322+
zend_array_release(prop_info->attributes);
323323
}
324324
zend_type_release(prop_info->type, /* persistent */ 0);
325325
}
@@ -337,7 +337,7 @@ ZEND_API void destroy_zend_class(zval *zv)
337337
zend_string_release_ex(c->doc_comment, 0);
338338
}
339339
if (c->attributes) {
340-
zend_array_ptr_dtor(c->attributes);
340+
zend_array_release(c->attributes);
341341
}
342342
}
343343
} ZEND_HASH_FOREACH_END();
@@ -358,7 +358,7 @@ ZEND_API void destroy_zend_class(zval *zv)
358358
zend_string_release_ex(ce->info.user.doc_comment, 0);
359359
}
360360
if (ce->info.user.attributes) {
361-
zend_array_ptr_dtor(ce->info.user.attributes);
361+
zend_array_release(ce->info.user.attributes);
362362
}
363363

364364
if (ce->num_traits > 0) {
@@ -412,7 +412,7 @@ ZEND_API void destroy_zend_class(zval *zv)
412412
zend_string_release_ex(c->doc_comment, 1);
413413
}
414414
if (c->attributes) {
415-
zend_array_ptr_dtor(c->attributes);
415+
zend_array_release(c->attributes);
416416
}
417417
}
418418
free(c);
@@ -497,7 +497,7 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
497497
zend_string_release_ex(op_array->doc_comment, 0);
498498
}
499499
if (op_array->attributes) {
500-
zend_array_ptr_dtor(op_array->attributes);
500+
zend_array_release(op_array->attributes);
501501
}
502502
if (op_array->live_range) {
503503
efree(op_array->live_range);

Zend/zend_variables.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static zend_always_inline void i_zval_ptr_dtor(zval *zval_ptr)
4848
}
4949
}
5050

51-
static zend_always_inline void zend_array_ptr_dtor(zend_array *array)
51+
static zend_always_inline void zend_array_release(zend_array *array)
5252
{
5353
if (!(GC_FLAGS(array) & IS_ARRAY_IMMUTABLE)) {
5454
if (GC_DELREF(array) == 0) {

ext/reflection/php_reflection.c

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,32 +1096,6 @@ static void reflection_attribute_factory(zval *object, zend_string *name, zval *
10961096
}
10971097
/* }}} */
10981098

1099-
static int convert_ast_to_zval(zval *ret, zend_ast *ast, zend_class_entry *scope_ce) /* {{{ */
1100-
{
1101-
if (ast->kind == ZEND_AST_CONSTANT) {
1102-
zend_string *name = zend_ast_get_constant_name(ast);
1103-
zval *zv = zend_get_constant_ex(name, scope_ce, ast->attr);
1104-
1105-
if (UNEXPECTED(zv == NULL)) {
1106-
return FAILURE;
1107-
}
1108-
1109-
ZVAL_COPY_OR_DUP(ret, zv);
1110-
} else {
1111-
zval tmp;
1112-
1113-
if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope_ce) != SUCCESS)) {
1114-
return FAILURE;
1115-
}
1116-
1117-
ZVAL_COPY_OR_DUP(ret, &tmp);
1118-
zval_ptr_dtor(&tmp);
1119-
}
1120-
1121-
return SUCCESS;
1122-
}
1123-
/* }}} */
1124-
11251099
static int convert_ast_attributes(zval *ret, HashTable *attributes, zend_class_entry *scope_ce) /* {{{ */
11261100
{
11271101
Bucket *p;
@@ -1135,7 +1109,9 @@ static int convert_ast_attributes(zval *ret, HashTable *attributes, zend_class_e
11351109
}
11361110

11371111
if (Z_TYPE(p->val) == IS_CONSTANT_AST) {
1138-
if (FAILURE == convert_ast_to_zval(&tmp, Z_ASTVAL(p->val), scope_ce)) {
1112+
tmp = p->val;
1113+
1114+
if (FAILURE == zval_update_constant_ex(&tmp, scope_ce)) {
11391115
return FAILURE;
11401116
}
11411117

@@ -6572,7 +6548,7 @@ ZEND_METHOD(ReflectionAttribute, getArguments)
65726548
}
65736549
GET_REFLECTION_OBJECT_PTR(attr);
65746550

6575-
RETURN_ZVAL(&attr->arguments, 1, 0);
6551+
RETURN_COPY(&attr->arguments);
65766552
}
65776553
/* }}} */
65786554

@@ -6582,8 +6558,6 @@ static int call_attribute_constructor(zend_class_entry *ce, zend_object *obj, zv
65826558
zend_fcall_info_cache fcc;
65836559

65846560
zend_function *ctor;
6585-
zend_class_entry *scope;
6586-
65876561
zval retval;
65886562
int ret;
65896563

@@ -6608,13 +6582,8 @@ static int call_attribute_constructor(zend_class_entry *ce, zend_object *obj, zv
66086582
fcc.called_scope = ce;
66096583
fcc.object = obj;
66106584

6611-
scope = EG(fake_scope);
6612-
EG(fake_scope) = NULL;
6613-
66146585
ret = zend_call_function(&fci, &fcc);
66156586

6616-
EG(fake_scope) = scope;
6617-
66186587
if (EG(exception)) {
66196588
zend_object_store_ctor_failed(obj);
66206589
}

0 commit comments

Comments
 (0)