Skip to content

Commit a75c195

Browse files
committed
Implemented the RFC Support Class Constant Visibility.
Squashed commit of the following: commit f11ca0e Author: Dmitry Stogov <[email protected]> Date: Tue Dec 8 12:38:42 2015 +0300 Fixed test expectation commit 211f873 Author: Dmitry Stogov <[email protected]> Date: Tue Dec 8 12:28:38 2015 +0300 Embed zend_class_constant.flags into zend_class_constants.value.u2.access_flags commit 51deab8 Author: Dmitry Stogov <[email protected]> Date: Mon Dec 7 11:18:55 2015 +0300 Fixed issues found by Nikita commit 544dbd5 Author: Dmitry Stogov <[email protected]> Date: Sat Dec 5 02:41:05 2015 +0300 Refactored immplementation of https://wiki.php.net/rfc/class_const_visibility @reeze created an RFC here and I emailed internals here and didn't get any responses positive/negative.
1 parent a8b7d0c commit a75c195

40 files changed

Lines changed: 1143 additions & 126 deletions

‎Zend/zend_API.c‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,12 +1129,13 @@ ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
11291129
zend_class_entry **scope = EG(current_execute_data) ? &EG(scope) : &CG(active_class_entry);
11301130
zend_class_entry *old_scope = *scope;
11311131
zend_class_entry *ce;
1132+
zend_class_constant *c;
11321133
zval *val;
11331134
zend_property_info *prop_info;
11341135

11351136
*scope = class_type;
1136-
ZEND_HASH_FOREACH_VAL(&class_type->constants_table, val) {
1137-
ZVAL_DEREF(val);
1137+
ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
1138+
val = &c->value;
11381139
if (Z_CONSTANT_P(val)) {
11391140
if (UNEXPECTED(zval_update_constant_ex(val, 1, class_type) != SUCCESS)) {
11401141
return FAILURE;
@@ -3737,16 +3738,49 @@ ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *nam
37373738
}
37383739
/* }}} */
37393740

3740-
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3741+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
37413742
{
3743+
zend_class_constant *c;
3744+
3745+
if (ce->ce_flags & ZEND_ACC_INTERFACE) {
3746+
if (access_type != ZEND_ACC_PUBLIC) {
3747+
zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
3748+
}
3749+
}
3750+
3751+
if (zend_string_equals_literal_ci(name, "class")) {
3752+
zend_error((ce->type == ZEND_INTERNAL_CLASS) ? E_CORE_ERROR : E_COMPILE_ERROR,
3753+
"A class constant must not be called 'class'; it is reserved for class name fetching");
3754+
}
3755+
3756+
if (ce->type == ZEND_INTERNAL_CLASS) {
3757+
c = pemalloc(sizeof(zend_class_constant), 1);
3758+
} else {
3759+
c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
3760+
}
3761+
ZVAL_COPY_VALUE(&c->value, value);
3762+
Z_ACCESS_FLAGS(c->value) = access_type;
3763+
c->doc_comment = doc_comment;
3764+
c->ce = ce;
37423765
if (Z_CONSTANT_P(value)) {
37433766
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
37443767
}
3745-
return zend_hash_str_update(&ce->constants_table, name, name_length, value) ?
3768+
return zend_hash_add_ptr(&ce->constants_table, name, c) ?
37463769
SUCCESS : FAILURE;
37473770
}
37483771
/* }}} */
37493772

3773+
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
3774+
{
3775+
int ret;
3776+
3777+
zend_string *key = zend_string_init(name, name_length, ce->type & ZEND_INTERNAL_CLASS);
3778+
ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
3779+
zend_string_release(key);
3780+
return ret;
3781+
}
3782+
/* }}} */
3783+
37503784
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
37513785
{
37523786
zval constant;

‎Zend/zend_API.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name
324324
ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type);
325325
ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type);
326326

327+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment);
327328
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value);
328329
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length);
329330
ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value);

‎Zend/zend_ast.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ enum _zend_ast_kind {
124124
ZEND_AST_SWITCH,
125125
ZEND_AST_SWITCH_CASE,
126126
ZEND_AST_DECLARE,
127-
ZEND_AST_CONST_ELEM,
128127
ZEND_AST_USE_TRAIT,
129128
ZEND_AST_TRAIT_PRECEDENCE,
130129
ZEND_AST_METHOD_REFERENCE,
@@ -142,6 +141,7 @@ enum _zend_ast_kind {
142141
ZEND_AST_CATCH,
143142
ZEND_AST_PARAM,
144143
ZEND_AST_PROP_ELEM,
144+
ZEND_AST_CONST_ELEM,
145145

146146
/* 4 child nodes */
147147
ZEND_AST_FOR = 4 << ZEND_AST_NUM_CHILDREN_SHIFT,

‎Zend/zend_compile.c‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ static void zend_destroy_property_info_internal(zval *zv) /* {{{ */
9797
}
9898
/* }}} */
9999

100+
static void zend_destroy_class_constant_internal(zval *zv) /* {{{ */
101+
{
102+
free(Z_PTR_P(zv));
103+
}
104+
/* }}} */
105+
100106
static zend_string *zend_new_interned_string_safe(zend_string *str) /* {{{ */ {
101107
zend_string *interned_str;
102108

@@ -1437,14 +1443,15 @@ static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_a
14371443
static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name, zend_string *name) /* {{{ */
14381444
{
14391445
uint32_t fetch_type = zend_get_class_fetch_type(class_name);
1446+
zend_class_constant *cc;
14401447
zval *c;
14411448

14421449
if (class_name_refers_to_active_ce(class_name, fetch_type)) {
1443-
c = zend_hash_find(&CG(active_class_entry)->constants_table, name);
1450+
cc = zend_hash_find_ptr(&CG(active_class_entry)->constants_table, name);
14441451
} else if (fetch_type == ZEND_FETCH_CLASS_DEFAULT && !(CG(compiler_options) & ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION)) {
14451452
zend_class_entry *ce = zend_hash_find_ptr_lc(CG(class_table), ZSTR_VAL(class_name), ZSTR_LEN(class_name));
14461453
if (ce) {
1447-
c = zend_hash_find(&ce->constants_table, name);
1454+
cc = zend_hash_find_ptr(&ce->constants_table, name);
14481455
} else {
14491456
return 0;
14501457
}
@@ -1456,8 +1463,14 @@ static zend_bool zend_try_ct_eval_class_const(zval *zv, zend_string *class_name,
14561463
return 0;
14571464
}
14581465

1466+
if (!cc || !zend_verify_const_access(cc, CG(active_class_entry))) {
1467+
return 0;
1468+
}
1469+
1470+
c = &cc->value;
1471+
14591472
/* Substitute case-sensitive (or lowercase) persistent class constants */
1460-
if (c && Z_TYPE_P(c) < IS_OBJECT) {
1473+
if (Z_TYPE_P(c) < IS_OBJECT) {
14611474
ZVAL_DUP(zv, c);
14621475
return 1;
14631476
}
@@ -1638,7 +1651,6 @@ int zendlex(zend_parser_stack_elem *elem) /* {{{ */
16381651
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
16391652
{
16401653
zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0;
1641-
dtor_func_t zval_ptr_dtor_func = ((persistent_hashes) ? ZVAL_INTERNAL_PTR_DTOR : ZVAL_PTR_DTOR);
16421654

16431655
ce->refcount = 1;
16441656
ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;
@@ -1650,7 +1662,7 @@ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify
16501662
ce->default_properties_table = NULL;
16511663
ce->default_static_members_table = NULL;
16521664
zend_hash_init_ex(&ce->properties_info, 8, NULL, (persistent_hashes ? zend_destroy_property_info_internal : NULL), persistent_hashes, 0);
1653-
zend_hash_init_ex(&ce->constants_table, 8, NULL, zval_ptr_dtor_func, persistent_hashes, 0);
1665+
zend_hash_init_ex(&ce->constants_table, 8, NULL, (persistent_hashes ? zend_destroy_class_constant_internal : NULL), persistent_hashes, 0);
16541666
zend_hash_init_ex(&ce->function_table, 8, NULL, ZEND_FUNCTION_DTOR, persistent_hashes, 0);
16551667

16561668
if (ce->type == ZEND_INTERNAL_CLASS) {
@@ -5183,25 +5195,28 @@ void zend_compile_class_const_decl(zend_ast *ast) /* {{{ */
51835195
zend_ast *const_ast = list->child[i];
51845196
zend_ast *name_ast = const_ast->child[0];
51855197
zend_ast *value_ast = const_ast->child[1];
5198+
zend_ast *doc_comment_ast = const_ast->child[2];
51865199
zend_string *name = zend_ast_get_str(name_ast);
5200+
zend_string *doc_comment = doc_comment_ast ? zend_string_copy(zend_ast_get_str(doc_comment_ast)) : NULL;
51875201
zval value_zv;
51885202

5189-
if (zend_string_equals_literal_ci(name, "class")) {
5190-
zend_error(E_COMPILE_ERROR,
5191-
"A class constant must not be called 'class'; it is reserved for class name fetching");
5203+
if (UNEXPECTED(ast->attr & (ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_FINAL))) {
5204+
if (ast->attr & ZEND_ACC_STATIC) {
5205+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'static' as constant modifier");
5206+
} else if (ast->attr & ZEND_ACC_ABSTRACT) {
5207+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'abstract' as constant modifier");
5208+
} else if (ast->attr & ZEND_ACC_FINAL) {
5209+
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use 'final' as constant modifier");
5210+
}
51925211
}
51935212

51945213
zend_const_expr_to_zval(&value_zv, value_ast);
51955214

51965215
name = zend_new_interned_string_safe(name);
5197-
if (zend_hash_add(&ce->constants_table, name, &value_zv) == NULL) {
5216+
if (zend_declare_class_constant_ex(ce, name, &value_zv, ast->attr, doc_comment) != SUCCESS) {
51985217
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redefine class constant %s::%s",
51995218
ZSTR_VAL(ce->name), ZSTR_VAL(name));
52005219
}
5201-
5202-
if (Z_CONSTANT(value_zv)) {
5203-
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
5204-
}
52055220
}
52065221
}
52075222
/* }}} */

‎Zend/zend_compile.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ typedef struct _zend_property_info {
310310
#define OBJ_PROP_TO_NUM(offset) \
311311
((offset - OBJ_PROP_TO_OFFSET(0)) / sizeof(zval))
312312

313+
typedef struct _zend_class_constant {
314+
zval value; /* access flags are stored in reserved: zval.u2.access_flags */
315+
zend_string *doc_comment;
316+
zend_class_entry *ce;
317+
} zend_class_constant;
318+
313319
/* arg_info for internal functions */
314320
typedef struct _zend_internal_arg_info {
315321
const char *name;

‎Zend/zend_constants.c‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ static zend_constant *zend_get_special_constant(const char *name, size_t name_le
251251
}
252252
}
253253

254+
ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *scope) /* {{{ */
255+
{
256+
if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PUBLIC) {
257+
return 1;
258+
} else if (Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PRIVATE) {
259+
return (c->ce == scope);
260+
} else {
261+
ZEND_ASSERT(Z_ACCESS_FLAGS(c->value) & ZEND_ACC_PROTECTED);
262+
return zend_check_protected(c->ce, scope);
263+
}
264+
}
265+
/* }}} */
254266

255267
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
256268
{
@@ -360,16 +372,23 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
360372
}
361373
free_alloca(lcname, use_heap);
362374
if (ce) {
363-
ret_constant = zend_hash_find(&ce->constants_table, constant_name);
364-
if (ret_constant == NULL) {
375+
zend_class_constant *c = zend_hash_find_ptr(&ce->constants_table, constant_name);
376+
if (c == NULL) {
365377
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
366378
zend_throw_error(NULL, "Undefined class constant '%s::%s'", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
367379
zend_string_release(class_name);
368380
zend_string_free(constant_name);
369381
return NULL;
370382
}
371-
} else if (Z_ISREF_P(ret_constant)) {
372-
ret_constant = Z_REFVAL_P(ret_constant);
383+
ret_constant = NULL;
384+
} else {
385+
if (!zend_verify_const_access(c, scope)) {
386+
zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
387+
zend_string_release(class_name);
388+
zend_string_free(constant_name);
389+
return NULL;
390+
}
391+
ret_constant = &c->value;
373392
}
374393
}
375394
zend_string_release(class_name);

‎Zend/zend_constants.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ int zend_startup_constants(void);
6565
int zend_shutdown_constants(void);
6666
void zend_register_standard_constants(void);
6767
void clean_non_persistent_constants(void);
68+
ZEND_API int zend_verify_const_access(zend_class_constant *c, zend_class_entry *ce);
6869
ZEND_API zval *zend_get_constant(zend_string *name);
6970
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len);
7071
ZEND_API zval *zend_get_constant_ex(zend_string *name, zend_class_entry *scope, uint32_t flags);

0 commit comments

Comments
 (0)