Skip to content

Code Cleanup #16

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 3 commits into from
May 28, 2020
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
10 changes: 4 additions & 6 deletions Zend/tests/attributes/012-ast-export.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ assert(0 && ($a = function () {

?>
--EXPECTF--
Warning: assert(): assert(0 && ($a = <<A1>>
<<A2>>
function ($a, <<A3(1)>> $b) {
Warning: assert(): assert(0 && ($a = <<A1>> <<A2>> function ($a, <<A3(1)>> $b) {
})) failed in %s on line %d

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

Warning: assert(): assert(0 && ($a = new <<A1>> class {
<<A1>>
<<A2>>
const FOO = 'foo';
<<A2>>
public $x;
<<A3>> public function a() {
<<A3>>
public function a() {
}

})) failed in %s on line %d
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Attributes make use of correct scope.
Attributes make use of class scope.
--FILE--
<?php

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

echo "\n";

class C2
trait T1
{
private const FOO = 'foo';

public static function foo()
{
return <<A1(self::class, self::FOO)>> function (<<A1(self::class, self::FOO)>> $p) { };
}
<<A1(self::class, self::FOO)>>
public function foo() { }
}

$ref = new \ReflectionFunction(C2::foo());
print_r($ref->getAttributes()[0]->getArguments());
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());
class C2
{
use T1;

private const FOO = 'bar';
}

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

class C3
Expand All @@ -56,13 +57,13 @@ class C3
}
}

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

print_r($ref->getAttributes()[0]->getArguments());
var_dump($name == get_class($obj));
var_dump($ref->getMethod('bar')->getAttributes()[0]->getArguments()[1]);
$args = $ref->getAttributes()[0]->getArguments();
var_dump($args[0] == $ref->getName(), $args[1]);

$args = $ref->getMethod('bar')->getAttributes()[0]->getArguments();
var_dump($args[0] == $ref->getName(), $args[1]);

?>
--EXPECT--
Expand Down Expand Up @@ -95,18 +96,10 @@ Array
Array
(
[0] => C2
[1] => foo
)
Array
(
[0] => C2
[1] => foo
[1] => bar
)

Array
(
[0] => C3
[1] => foo
)
bool(true)
string(3) "bar"
bool(true)
string(3) "bar"
53 changes: 53 additions & 0 deletions Zend/tests/attributes/017_closure_scope.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
Attributes make use of closure scope.
--FILE--
<?php

class Test1
{
private const FOO = 'bar';
}

class C1
{
private const FOO = 'foo';

public static function foo()
{
return <<A1(self::class, self::FOO)>> function (<<A1(self::class, self::FOO)>> $p) { };
}
}

$ref = new \ReflectionFunction(C1::foo());
print_r($ref->getAttributes()[0]->getArguments());
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());

echo "\n";

$ref = new \ReflectionFunction(C1::foo()->bindTo(null, Test1::class));
print_r($ref->getAttributes()[0]->getArguments());
print_r($ref->getParameters()[0]->getAttributes()[0]->getArguments());

?>
--EXPECT--
Array
(
[0] => C1
[1] => foo
)
Array
(
[0] => C1
[1] => foo
)

Array
(
[0] => Test1
[1] => bar
)
Array
(
[0] => Test1
[1] => bar
)
26 changes: 12 additions & 14 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_class_const_or_name(zend_ast *

ZEND_API zend_ast *zend_ast_create_decl(
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
) {
zend_ast_decl *ast;

Expand All @@ -126,12 +126,12 @@ ZEND_API zend_ast *zend_ast_create_decl(
ast->flags = flags;
ast->lex_pos = LANG_SCNG(yy_text);
ast->doc_comment = doc_comment;
ast->attributes = NULL;
ast->name = name;
ast->child[0] = child0;
ast->child[1] = child1;
ast->child[2] = child2;
ast->child[3] = child3;
ast->child[4] = child4;

return (zend_ast *) ast;
}
Expand Down Expand Up @@ -858,13 +858,11 @@ ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
if (decl->doc_comment) {
zend_string_release_ex(decl->doc_comment, 0);
}
if (decl->attributes) {
zend_ast_destroy(decl->attributes);
}
zend_ast_destroy(decl->child[0]);
zend_ast_destroy(decl->child[1]);
zend_ast_destroy(decl->child[2]);
ast = decl->child[3];
zend_ast_destroy(decl->child[3]);
ast = decl->child[4];
goto tail_call;
}
}
Expand Down Expand Up @@ -1445,9 +1443,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
case ZEND_AST_ARROW_FUNC:
case ZEND_AST_METHOD:
decl = (zend_ast_decl *) ast;
if (decl->attributes) {
zend_bool newlines = (ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
zend_ast_export_attributes(str, decl->attributes, indent, newlines);
if (decl->child[4]) {
zend_bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
zend_ast_export_attributes(str, decl->child[4], indent, newlines);
}
if (decl->flags & ZEND_ACC_PUBLIC) {
smart_str_appends(str, "public ");
Expand Down Expand Up @@ -1505,8 +1503,8 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
break;
case ZEND_AST_CLASS:
decl = (zend_ast_decl *) ast;
if (decl->attributes) {
zend_ast_export_attributes(str, decl->attributes, indent, 1);
if (decl->child[4]) {
zend_ast_export_attributes(str, decl->child[4], indent, 1);
}
if (decl->flags & ZEND_ACC_INTERFACE) {
smart_str_appends(str, "interface ");
Expand Down Expand Up @@ -1839,8 +1837,8 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
smart_str_appends(str, "new ");
if (ast->child[0]->kind == ZEND_AST_CLASS) {
zend_ast_decl *decl = (zend_ast_decl *) ast->child[0];
if (decl->attributes) {
zend_ast_export_attributes(str, decl->attributes, indent, 0);
if (decl->child[4]) {
zend_ast_export_attributes(str, decl->child[4], indent, 0);
}
smart_str_appends(str, "class");
if (zend_ast_get_list(ast->child[1])->children) {
Expand Down Expand Up @@ -2185,7 +2183,7 @@ zend_ast * ZEND_FASTCALL zend_ast_with_attributes(zend_ast *ast, zend_ast *attr)
case ZEND_AST_METHOD:
case ZEND_AST_CLASS:
case ZEND_AST_ARROW_FUNC:
((zend_ast_decl *) ast)->attributes = attr;
((zend_ast_decl *) ast)->child[4] = attr;
break;
case ZEND_AST_PROP_GROUP:
ast->child[2] = attr;
Expand Down
17 changes: 2 additions & 15 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ typedef struct _zend_ast_decl {
uint32_t flags;
unsigned char *lex_pos;
zend_string *doc_comment;
zend_ast *attributes;
zend_string *name;
zend_ast *child[4];
zend_ast *child[5];
} zend_ast_decl;

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

ZEND_API zend_ast *zend_ast_create_decl(
zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4
);

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

static zend_always_inline HashTable *zend_ast_get_hash(zend_ast *ast) {
zval *zv = zend_ast_get_zval(ast);
ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
return Z_ARR_P(zv);
}

static zend_always_inline uint32_t zend_ast_get_num_children(zend_ast *ast) {
ZEND_ASSERT(!zend_ast_is_list(ast));
return ast->kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
Expand All @@ -334,12 +327,6 @@ static zend_always_inline uint32_t zend_ast_get_lineno(zend_ast *ast) {
}
}

static zend_always_inline zend_ast *zend_ast_create_zval_from_hash(HashTable *hash) {
zval zv;
ZVAL_ARR(&zv, hash);
return zend_ast_create_zval(&zv);
}

static zend_always_inline zend_ast *zend_ast_create_binary_op(uint32_t opcode, zend_ast *op0, zend_ast *op1) {
return zend_ast_create_ex(ZEND_AST_BINARY_OP, opcode, op0, op1);
}
Expand Down
32 changes: 16 additions & 16 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1046,10 +1046,6 @@ ZEND_API void function_add_ref(zend_function *function) /* {{{ */
ZEND_MAP_PTR_INIT(op_array->run_time_cache, zend_arena_alloc(&CG(arena), sizeof(void*)));
ZEND_MAP_PTR_SET(op_array->run_time_cache, NULL);
}

if (op_array->attributes) {
GC_ADDREF(op_array->attributes);
}
}

if (function->common.function_name) {
Expand Down Expand Up @@ -6371,15 +6367,7 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
if (decl->doc_comment) {
op_array->doc_comment = zend_string_copy(decl->doc_comment);
}
if (decl->attributes) {
int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;

if (is_method) {
target = ZEND_ATTRIBUTE_TARGET_METHOD;
}
op_array->attributes = create_attribute_array();
zend_compile_attributes(op_array->attributes, decl->attributes, 0, target);
}
if (decl->kind == ZEND_AST_CLOSURE || decl->kind == ZEND_AST_ARROW_FUNC) {
op_array->fn_flags |= ZEND_ACC_CLOSURE;
}
Expand All @@ -6399,6 +6387,17 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*

CG(active_op_array) = op_array;

if (decl->child[4]) {
int target = ZEND_ATTRIBUTE_TARGET_FUNCTION;

if (is_method) {
target = ZEND_ATTRIBUTE_TARGET_METHOD;
}

op_array->attributes = create_attribute_array();
zend_compile_attributes(op_array->attributes, decl->child[4], 0, target);
}

/* Do not leak the class scope into free standing functions, even if they are dynamically
* defined inside a class method. This is necessary for correct handling of magic constants.
* For example __CLASS__ should always be "" inside a free standing function. */
Expand Down Expand Up @@ -6824,10 +6823,6 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
if (decl->doc_comment) {
ce->info.user.doc_comment = zend_string_copy(decl->doc_comment);
}
if (decl->attributes) {
ce->attributes = create_attribute_array();
zend_compile_attributes(ce->attributes, decl->attributes, 0, ZEND_ATTRIBUTE_TARGET_CLASS);
}

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

CG(active_class_entry) = ce;

if (decl->child[4]) {
ce->attributes = create_attribute_array();
zend_compile_attributes(ce->attributes, decl->child[4], 0, ZEND_ATTRIBUTE_TARGET_CLASS);
}

if (implements_ast) {
zend_compile_implements(implements_ast);
}
Expand Down
Loading