Skip to content

Commit 4a0d690

Browse files
moliatanikic
authored andcommitted
refactor: class constants parsing
As part of my work on typed class constants, I wanted to make a separate pull request for unrelated changes. These include: * Moving from ast->child[0]->attr to ast->attr * Making zend_ast_export_ex() export class constants' visibility * Extracting an additional zend_compile_class_const_group() function Closes GH-5812.
1 parent ff3c402 commit 4a0d690

File tree

5 files changed

+55
-38
lines changed

5 files changed

+55
-38
lines changed

Zend/tests/assert/expect_015.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ Warning: assert(): assert(0 && ($a = function () {
164164

165165
Warning: assert(): assert(0 && ($a = function &(array &$a, ?X $b = null) use($c, &$d): ?X {
166166
abstract class A extends B implements C, D {
167-
const X = 12;
168-
const Y = self::X, Z = 'aaa';
167+
public const X = 12;
168+
public const Y = self::X, Z = 'aaa';
169169
public $a = 1, $b;
170170
protected $c;
171171
private static $d = null;

Zend/tests/attributes/012_ast_export.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Warning: assert(): assert(0 && ($a = <<A1(1, 2, 1 + 2)>> fn() => 1)) failed in %
2929
Warning: assert(): assert(0 && ($a = new <<A1>> class {
3030
<<A1>>
3131
<<A2>>
32-
const FOO = 'foo';
32+
public const FOO = 'foo';
3333
<<A2>>
3434
public $x;
3535
<<A3>>

Zend/zend_ast.c

+23-17
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,16 @@ static ZEND_COLD void zend_ast_export_attributes(smart_str *str, zend_ast *ast,
13811381
}
13821382
}
13831383

1384+
static ZEND_COLD void zend_ast_export_visibility(smart_str *str, uint32_t flags) {
1385+
if (flags & ZEND_ACC_PUBLIC) {
1386+
smart_str_appends(str, "public ");
1387+
} else if (flags & ZEND_ACC_PROTECTED) {
1388+
smart_str_appends(str, "protected ");
1389+
} else if (flags & ZEND_ACC_PRIVATE) {
1390+
smart_str_appends(str, "private ");
1391+
}
1392+
}
1393+
13841394
static ZEND_COLD void zend_ast_export_type(smart_str *str, zend_ast *ast, int indent) {
13851395
if (ast->kind == ZEND_AST_TYPE_UNION) {
13861396
zend_ast_list *list = zend_ast_get_list(ast);
@@ -1478,13 +1488,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
14781488
zend_bool newlines = !(ast->kind == ZEND_AST_CLOSURE || ast->kind == ZEND_AST_ARROW_FUNC);
14791489
zend_ast_export_attributes(str, decl->child[4], indent, newlines);
14801490
}
1481-
if (decl->flags & ZEND_ACC_PUBLIC) {
1482-
smart_str_appends(str, "public ");
1483-
} else if (decl->flags & ZEND_ACC_PROTECTED) {
1484-
smart_str_appends(str, "protected ");
1485-
} else if (decl->flags & ZEND_ACC_PRIVATE) {
1486-
smart_str_appends(str, "private ");
1487-
}
1491+
1492+
zend_ast_export_visibility(str, decl->flags);
1493+
14881494
if (decl->flags & ZEND_ACC_STATIC) {
14891495
smart_str_appends(str, "static ");
14901496
}
@@ -1595,13 +1601,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
15951601
if (ast->child[2]) {
15961602
zend_ast_export_attributes(str, ast->child[2], indent, 1);
15971603
}
1598-
if (ast->attr & ZEND_ACC_PUBLIC) {
1599-
smart_str_appends(str, "public ");
1600-
} else if (ast->attr & ZEND_ACC_PROTECTED) {
1601-
smart_str_appends(str, "protected ");
1602-
} else if (ast->attr & ZEND_ACC_PRIVATE) {
1603-
smart_str_appends(str, "private ");
1604-
}
1604+
1605+
zend_ast_export_visibility(str, ast->attr);
1606+
16051607
if (ast->attr & ZEND_ACC_STATIC) {
16061608
smart_str_appends(str, "static ");
16071609
}
@@ -1616,15 +1618,19 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
16161618
}
16171619

16181620
case ZEND_AST_CONST_DECL:
1619-
case ZEND_AST_CLASS_CONST_DECL:
16201621
smart_str_appends(str, "const ");
16211622
goto simple_list;
16221623
case ZEND_AST_CLASS_CONST_GROUP:
16231624
if (ast->child[1]) {
16241625
zend_ast_export_attributes(str, ast->child[1], indent, 1);
16251626
}
1626-
zend_ast_export_ex(str, ast->child[0], 0, indent);
1627-
break;
1627+
1628+
zend_ast_export_visibility(str, ast->attr);
1629+
smart_str_appends(str, "const ");
1630+
1631+
ast = ast->child[0];
1632+
1633+
goto simple_list;
16281634
case ZEND_AST_NAME_LIST:
16291635
zend_ast_export_name_list(str, (zend_ast_list*)ast, indent);
16301636
break;

Zend/zend_compile.c

+27-17
Original file line numberDiff line numberDiff line change
@@ -6613,18 +6613,18 @@ void zend_compile_prop_decl(zend_ast *ast, zend_ast *type_ast, uint32_t flags, z
66136613
}
66146614
/* }}} */
66156615

6616-
void zend_compile_prop_group(zend_ast *list) /* {{{ */
6616+
void zend_compile_prop_group(zend_ast *ast) /* {{{ */
66176617
{
6618-
zend_ast *type_ast = list->child[0];
6619-
zend_ast *prop_ast = list->child[1];
6620-
zend_ast *attr_ast = list->child[2];
6618+
zend_ast *type_ast = ast->child[0];
6619+
zend_ast *prop_ast = ast->child[1];
6620+
zend_ast *attr_ast = ast->child[2];
66216621

66226622
if (attr_ast && zend_ast_get_list(prop_ast)->children > 1) {
66236623
zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of properties");
66246624
return;
66256625
}
66266626

6627-
zend_compile_prop_decl(prop_ast, type_ast, list->attr, attr_ast);
6627+
zend_compile_prop_decl(prop_ast, type_ast, ast->attr, attr_ast);
66286628
}
66296629
/* }}} */
66306630

@@ -6640,23 +6640,18 @@ static void zend_check_const_and_trait_alias_attr(uint32_t attr, const char* ent
66406640
}
66416641
/* }}} */
66426642

6643-
void zend_compile_class_const_decl(zend_ast *ast, zend_ast *attr_ast) /* {{{ */
6643+
void zend_compile_class_const_decl(zend_ast *ast, uint32_t flags, zend_ast *attr_ast) /* {{{ */
66446644
{
66456645
zend_ast_list *list = zend_ast_get_list(ast);
66466646
zend_class_entry *ce = CG(active_class_entry);
6647-
uint32_t i;
6647+
uint32_t i, children = list->children;
66486648

66496649
if ((ce->ce_flags & ZEND_ACC_TRAIT) != 0) {
66506650
zend_error_noreturn(E_COMPILE_ERROR, "Traits cannot have constants");
66516651
return;
66526652
}
66536653

6654-
if (attr_ast && list->children > 1) {
6655-
zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of constants");
6656-
return;
6657-
}
6658-
6659-
for (i = 0; i < list->children; ++i) {
6654+
for (i = 0; i < children; ++i) {
66606655
zend_class_constant *c;
66616656
zend_ast *const_ast = list->child[i];
66626657
zend_ast *name_ast = const_ast->child[0];
@@ -6666,12 +6661,12 @@ void zend_compile_class_const_decl(zend_ast *ast, zend_ast *attr_ast) /* {{{ */
66666661
zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
66676662
zval value_zv;
66686663

6669-
if (UNEXPECTED(ast->attr & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
6670-
zend_check_const_and_trait_alias_attr(ast->attr, "constant");
6664+
if (UNEXPECTED(flags & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
6665+
zend_check_const_and_trait_alias_attr(flags, "constant");
66716666
}
66726667

66736668
zend_const_expr_to_zval(&value_zv, value_ast);
6674-
c = zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment);
6669+
c = zend_declare_class_constant_ex(ce, name, &value_zv, flags, doc_comment);
66756670

66766671
if (attr_ast) {
66776672
zend_compile_attributes(&c->attributes, attr_ast, 0, ZEND_ATTRIBUTE_TARGET_CLASS_CONST);
@@ -6680,6 +6675,21 @@ void zend_compile_class_const_decl(zend_ast *ast, zend_ast *attr_ast) /* {{{ */
66806675
}
66816676
/* }}} */
66826677

6678+
void zend_compile_class_const_group(zend_ast *ast) /* {{{ */
6679+
{
6680+
zend_ast *const_ast = ast->child[0];
6681+
zend_ast *attr_ast = ast->child[1];
6682+
6683+
if (attr_ast && zend_ast_get_list(const_ast)->children > 1) {
6684+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot apply attributes to a group of constants");
6685+
6686+
return;
6687+
}
6688+
6689+
zend_compile_class_const_decl(const_ast, ast->attr, attr_ast);
6690+
}
6691+
/* }}} */
6692+
66836693
static void zend_compile_method_ref(zend_ast *ast, zend_trait_method_reference *method_ref) /* {{{ */
66846694
{
66856695
zend_ast *class_ast = ast->child[0];
@@ -8983,7 +8993,7 @@ void zend_compile_stmt(zend_ast *ast) /* {{{ */
89838993
zend_compile_prop_group(ast);
89848994
break;
89858995
case ZEND_AST_CLASS_CONST_GROUP:
8986-
zend_compile_class_const_decl(ast->child[0], ast->child[1]);
8996+
zend_compile_class_const_group(ast);
89878997
break;
89888998
case ZEND_AST_USE_TRAIT:
89898999
zend_compile_use_trait(ast);

Zend/zend_language_parser.y

+2-1
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@ attributed_class_statement:
797797
{ $$ = zend_ast_create(ZEND_AST_PROP_GROUP, $2, $3, NULL);
798798
$$->attr = $1; }
799799
| method_modifiers T_CONST class_const_list ';'
800-
{ $$ = zend_ast_create(ZEND_AST_CLASS_CONST_GROUP, $3, NULL); $3->attr = $1; }
800+
{ $$ = zend_ast_create(ZEND_AST_CLASS_CONST_GROUP, $3, NULL);
801+
$$->attr = $1; }
801802
| method_modifiers function returns_ref identifier backup_doc_comment '(' parameter_list ')'
802803
return_type backup_fn_flags method_body backup_fn_flags
803804
{ $$ = zend_ast_create_decl(ZEND_AST_METHOD, $3 | $1 | $12, $2, $5,

0 commit comments

Comments
 (0)