Skip to content

Commit c643232

Browse files
authored
Merge pull request #16 from koolkode/Cleanup
Code Cleanup
2 parents 0c3743d + 511e3db commit c643232

File tree

10 files changed

+146
-121
lines changed

10 files changed

+146
-121
lines changed

Zend/tests/attributes/012-ast-export.phpt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ assert(0 && ($a = function () {
2121

2222
?>
2323
--EXPECTF--
24-
Warning: assert(): assert(0 && ($a = <<A1>>
25-
<<A2>>
26-
function ($a, <<A3(1)>> $b) {
24+
Warning: assert(): assert(0 && ($a = <<A1>> <<A2>> function ($a, <<A3(1)>> $b) {
2725
})) failed in %s on line %d
2826

29-
Warning: assert(): assert(0 && ($a = <<A1(1, 2, 1 + 2)>>
30-
fn() => 1)) failed in %s on line %d
27+
Warning: assert(): assert(0 && ($a = <<A1(1, 2, 1 + 2)>> fn() => 1)) failed in %s on line %d
3128

3229
Warning: assert(): assert(0 && ($a = new <<A1>> class {
3330
<<A1>>
3431
<<A2>>
3532
const FOO = 'foo';
3633
<<A2>>
3734
public $x;
38-
<<A3>> public function a() {
35+
<<A3>>
36+
public function a() {
3937
}
4038

4139
})) failed in %s on line %d

Zend/tests/attributes/013_scope_resolution.phpt renamed to Zend/tests/attributes/013_class_scope.phpt

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Attributes make use of correct scope.
2+
Attributes make use of class scope.
33
--FILE--
44
<?php
55

@@ -25,20 +25,21 @@ print_r($ref->getMethod('bar')->getParameters()[0]->getAttributes()[0]->getArgum
2525

2626
echo "\n";
2727

28-
class C2
28+
trait T1
2929
{
30-
private const FOO = 'foo';
31-
32-
public static function foo()
33-
{
34-
return <<A1(self::class, self::FOO)>> function (<<A1(self::class, self::FOO)>> $p) { };
35-
}
30+
<<A1(self::class, self::FOO)>>
31+
public function foo() { }
3632
}
3733

38-
$ref = new \ReflectionFunction(C2::foo());
39-
print_r($ref->getAttributes()[0]->getArguments());
40-
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());
34+
class C2
35+
{
36+
use T1;
37+
38+
private const FOO = 'bar';
39+
}
4140

41+
$ref = new \ReflectionClass(C2::class);
42+
print_r($ref->getMethod('foo')->getAttributes()[0]->getArguments());
4243
echo "\n";
4344

4445
class C3
@@ -56,13 +57,13 @@ class C3
5657
}
5758
}
5859

59-
$obj = C3::foo();
60-
$ref = new \ReflectionObject($obj);
61-
$name = $ref->getMethod('bar')->getAttributes()[0]->getArguments()[0];
60+
$ref = new \ReflectionObject(C3::foo());
6261

63-
print_r($ref->getAttributes()[0]->getArguments());
64-
var_dump($name == get_class($obj));
65-
var_dump($ref->getMethod('bar')->getAttributes()[0]->getArguments()[1]);
62+
$args = $ref->getAttributes()[0]->getArguments();
63+
var_dump($args[0] == $ref->getName(), $args[1]);
64+
65+
$args = $ref->getMethod('bar')->getAttributes()[0]->getArguments();
66+
var_dump($args[0] == $ref->getName(), $args[1]);
6667

6768
?>
6869
--EXPECT--
@@ -95,18 +96,10 @@ Array
9596
Array
9697
(
9798
[0] => C2
98-
[1] => foo
99-
)
100-
Array
101-
(
102-
[0] => C2
103-
[1] => foo
99+
[1] => bar
104100
)
105101

106-
Array
107-
(
108-
[0] => C3
109-
[1] => foo
110-
)
102+
bool(true)
103+
string(3) "bar"
111104
bool(true)
112105
string(3) "bar"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Attributes make use of closure scope.
3+
--FILE--
4+
<?php
5+
6+
class Test1
7+
{
8+
private const FOO = 'bar';
9+
}
10+
11+
class C1
12+
{
13+
private const FOO = 'foo';
14+
15+
public static function foo()
16+
{
17+
return <<A1(self::class, self::FOO)>> function (<<A1(self::class, self::FOO)>> $p) { };
18+
}
19+
}
20+
21+
$ref = new \ReflectionFunction(C1::foo());
22+
print_r($ref->getAttributes()[0]->getArguments());
23+
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());
24+
25+
echo "\n";
26+
27+
$ref = new \ReflectionFunction(C1::foo()->bindTo(null, Test1::class));
28+
print_r($ref->getAttributes()[0]->getArguments());
29+
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());
30+
31+
?>
32+
--EXPECT--
33+
Array
34+
(
35+
[0] => C1
36+
[1] => foo
37+
)
38+
Array
39+
(
40+
[0] => C1
41+
[1] => foo
42+
)
43+
44+
Array
45+
(
46+
[0] => Test1
47+
[1] => bar
48+
)
49+
Array
50+
(
51+
[0] => Test1
52+
[1] => bar
53+
)

Zend/zend_ast.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *
114114

115115
ZEND_API zend_ast *zend_ast_create_decl(
116116
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
117-
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
117+
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
118118
) {
119119
zend_ast_decl *ast;
120120

@@ -126,12 +126,12 @@ ZEND_API zend_ast *zend_ast_create_decl(
126126
ast->flags = flags;
127127
ast->lex_pos = LANG_SCNG(yy_text);
128128
ast->doc_comment = doc_comment;
129-
ast->attributes = NULL;
130129
ast->name = name;
131130
ast->child[0] = child0;
132131
ast->child[1] = child1;
133132
ast->child[2] = child2;
134133
ast->child[3] = child3;
134+
ast->child[4] = child4;
135135

136136
return (zend_ast *) ast;
137137
}
@@ -858,13 +858,11 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
858858
if (decl->doc_comment) {
859859
zend_string_release_ex(decl->doc_comment, 0);
860860
}
861-
if (decl->attributes) {
862-
zend_ast_destroy(decl->attributes);
863-
}
864861
zend_ast_destroy(decl->child[0]);
865862
zend_ast_destroy(decl->child[1]);
866863
zend_ast_destroy(decl->child[2]);
867-
ast = decl->child[3];
864+
zend_ast_destroy(decl->child[3]);
865+
ast = decl->child[4];
868866
goto tail_call;
869867
}
870868
}
@@ -1445,9 +1443,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
14451443
case ZEND_AST_ARROW_FUNC:
14461444
case ZEND_AST_METHOD:
14471445
decl = (zend_ast_decl *) ast;
1448-
if (decl->attributes) {
1449-
zend_bool newlines = (ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
1450-
zend_ast_export_attributes(str, decl->attributes, indent, newlines);
1446+
if (decl->child[4]) {
1447+
zend_bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
1448+
zend_ast_export_attributes(str, decl->child[4], indent, newlines);
14511449
}
14521450
if (decl->flags & ZEND_ACC_PUBLIC) {
14531451
smart_str_appends(str, "public ");
@@ -1505,8 +1503,8 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
15051503
break;
15061504
case ZEND_AST_CLASS:
15071505
decl = (zend_ast_decl *) ast;
1508-
if (decl->attributes) {
1509-
zend_ast_export_attributes(str, decl->attributes, indent, 1);
1506+
if (decl->child[4]) {
1507+
zend_ast_export_attributes(str, decl->child[4], indent, 1);
15101508
}
15111509
if (decl->flags & ZEND_ACC_INTERFACE) {
15121510
smart_str_appends(str, "interface ");
@@ -1839,8 +1837,8 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
18391837
smart_str_appends(str, "new ");
18401838
if (ast->child[0]->kind == ZEND_AST_CLASS) {
18411839
zend_ast_decl *decl = (zend_ast_decl *) ast->child[0];
1842-
if (decl->attributes) {
1843-
zend_ast_export_attributes(str, decl->attributes, indent, 0);
1840+
if (decl->child[4]) {
1841+
zend_ast_export_attributes(str, decl->child[4], indent, 0);
18441842
}
18451843
smart_str_appends(str, "class");
18461844
if (zend_ast_get_list(ast->child[1])->children) {
@@ -2185,7 +2183,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
21852183
case ZEND_AST_METHOD:
21862184
case ZEND_AST_CLASS:
21872185
case ZEND_AST_ARROW_FUNC:
2188-
((zend_ast_decl *) ast)->attributes = attr;
2186+
((zend_ast_decl *) ast)->child[4] = attr;
21892187
break;
21902188
case ZEND_AST_PROP_GROUP:
21912189
ast->child[2] = attr;

Zend/zend_ast.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ typedef struct _zend_ast_decl {
194194
uint32_t flags;
195195
unsigned char *lex_pos;
196196
zend_string *doc_comment;
197-
zend_ast *attributes;
198197
zend_string *name;
199-
zend_ast *child[4];
198+
zend_ast *child[5];
200199
} zend_ast_decl;
201200

202201
typedef void (*zend_ast_process_t)(zend_ast *ast);
@@ -274,7 +273,7 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *list, zend_ast *op
274273

275274
ZEND_API zend_ast *zend_ast_create_decl(
276275
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
277-
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
276+
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
278277
);
279278

280279
ZEND_API int ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope);
@@ -315,12 +314,6 @@ static zend_always_inline zend_string *zend_ast_get_constant_name(zend_ast *ast)
315314
return Z_STR(((zend_ast_zval *) ast)->val);
316315
}
317316

318-
static zend_always_inline HashTable *zend_ast_get_hash(zend_ast *ast) {
319-
zval *zv = zend_ast_get_zval(ast);
320-
ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
321-
return Z_ARR_P(zv);
322-
}
323-
324317
static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
325318
ZEND_ASSERT(!zend_ast_is_list(ast));
326319
return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
@@ -334,12 +327,6 @@ static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
334327
}
335328
}
336329

337-
static zend_always_inline zend_ast *zend_ast_create_zval_from_hash(HashTable *hash) {
338-
zval zv;
339-
ZVAL_ARR(&zv, hash);
340-
return zend_ast_create_zval(&zv);
341-
}
342-
343330
static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
344331
return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
345332
}

Zend/zend_compile.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,6 @@ ZEND_API void function_add_ref(zend_function *function) /* {{{ */
10461046
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
10471047
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
10481048
}
1049-
1050-
if (op_array->attributes) {
1051-
GC_ADDREF(op_array->attributes);
1052-
}
10531049
}
10541050

10551051
if (function->common.function_name) {
@@ -6371,15 +6367,7 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
63716367
if (decl->doc_comment) {
63726368
op_array->doc_comment = zend_string_copy(decl->doc_comment);
63736369
}
6374-
if (decl->attributes) {
6375-
int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
63766370

6377-
if (is_method) {
6378-
target = ZEND_ATTRIBUTE_TARGET_METHOD;
6379-
}
6380-
op_array->attributes = create_attribute_array();
6381-
zend_compile_attributes(op_array->attributes, decl->attributes, 0, target);
6382-
}
63836371
if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
63846372
op_array->fn_flags |= ZEND_ACC_CLOSURE;
63856373
}
@@ -6399,6 +6387,17 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
63996387

64006388
CG(active_op_array) = op_array;
64016389

6390+
if (decl->child[4]) {
6391+
int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
6392+
6393+
if (is_method) {
6394+
target = ZEND_ATTRIBUTE_TARGET_METHOD;
6395+
}
6396+
6397+
op_array->attributes = create_attribute_array();
6398+
zend_compile_attributes(op_array->attributes, decl->child[4], 0, target);
6399+
}
6400+
64026401
/* Do not leak the class scope into free standing functions, even if they are dynamically
64036402
* defined inside a class method. This is necessary for correct handling of magic constants.
64046403
* For example __CLASS__ should always be "" inside a free standing function. */
@@ -6824,10 +6823,6 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
68246823
if (decl->doc_comment) {
68256824
ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
68266825
}
6827-
if (decl->attributes) {
6828-
ce->attributes = create_attribute_array();
6829-
zend_compile_attributes(ce->attributes, decl->attributes, 0, ZEND_ATTRIBUTE_TARGET_CLASS);
6830-
}
68316826

68326827
if (UNEXPECTED((decl->flags & ZEND_ACC_ANON_CLASS))) {
68336828
/* Serialization is not supported for anonymous classes */
@@ -6842,6 +6837,11 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
68426837

68436838
CG(active_class_entry) = ce;
68446839

6840+
if (decl->child[4]) {
6841+
ce->attributes = create_attribute_array();
6842+
zend_compile_attributes(ce->attributes, decl->child[4], 0, ZEND_ATTRIBUTE_TARGET_CLASS);
6843+
}
6844+
68456845
if (implements_ast) {
68466846
zend_compile_implements(implements_ast);
68476847
}

0 commit comments

Comments
 (0)