Implement additional type reservations

* The class names false, true and null are now reserved.
 * The code dealing with reserved class names is now decoupled
   from scalar type hint handling. It also includes self, parent,
   and static, which are class names which were already reserved
   previously.
 * Reuse existing messages for reserved class names.

Fallout: class_alias() can no longer alias self, parent and static.
However this never really worked in the first place, as the test
which was testing this shows.
This commit is contained in:
Nikita Popov 2015-03-30 22:05:26 +02:00
parent 607b7d662a
commit 53a40386bc
16 changed files with 72 additions and 80 deletions

View file

@ -1,22 +0,0 @@
--TEST--
Testing declaration of alias to 'static'
--FILE--
<?php
class bar {
}
class foo {
public function test() {
class_alias('bar', 'static');
return new static;
}
}
$a = new foo;
var_dump($a->test());
?>
--EXPECTF--
object(foo)#%d (0) {
}

View file

@ -11,4 +11,4 @@ foo(10);
?>
--EXPECTF--
Fatal error: "bar\int" cannot be used as a type declaration in %s on line %d
Fatal error: Cannot use 'bar\int' as class name as it is reserved in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (2)
class int {}
--EXPECTF--
Fatal error: "int" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'int' as class name as it is reserved in %s on line %d

View file

@ -6,4 +6,4 @@ Scalar type hint names cannot be used as class, trait or interface names (2) - c
class foobar {}
class_alias("foobar", "int");
--EXPECTF--
Fatal error: "int" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'int' as class name as it is reserved in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (2) - u
use foobar as int;
--EXPECTF--
Fatal error: "int" cannot be used as a class name in %s on line %d
Fatal error: Cannot use foobar as int because 'int' is a special class name in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (3)
class float {}
--EXPECTF--
Fatal error: "float" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'float' as class name as it is reserved in %s on line %d

View file

@ -6,4 +6,4 @@ Scalar type hint names cannot be used as class, trait or interface names (3) - c
class foobar {}
class_alias("foobar", "float");
--EXPECTF--
Fatal error: "float" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'float' as class name as it is reserved in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (3) - u
use foobar as float;
--EXPECTF--
Fatal error: "float" cannot be used as a class name in %s on line %d
Fatal error: Cannot use foobar as float because 'float' is a special class name in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (4)
class string {}
--EXPECTF--
Fatal error: "string" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'string' as class name as it is reserved in %s on line %d

View file

@ -6,4 +6,4 @@ Scalar type hint names cannot be used as class, trait or interface names (4) - c
class foobar {}
class_alias("foobar", "string");
--EXPECTF--
Fatal error: "string" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'string' as class name as it is reserved in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (4) - u
use foobar as string;
--EXPECTF--
Fatal error: "string" cannot be used as a class name in %s on line %d
Fatal error: Cannot use foobar as string because 'string' is a special class name in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (6)
class bool {}
--EXPECTF--
Fatal error: "bool" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'bool' as class name as it is reserved in %s on line %d

View file

@ -6,4 +6,4 @@ Scalar type hint names cannot be used as class, trait or interface names (6) - c
class foobar {}
class_alias("foobar", "bool");
--EXPECTF--
Fatal error: "bool" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'bool' as class name as it is reserved in %s on line %d

View file

@ -5,4 +5,4 @@ Scalar type hint names cannot be used as class, trait or interface names (6) - u
use foobar as bool;
--EXPECTF--
Fatal error: "bool" cannot be used as a class name in %s on line %d
Fatal error: Cannot use foobar as bool because 'bool' is a special class name in %s on line %d

View file

@ -6,4 +6,4 @@ namespace foo;
class int {}
--EXPECTF--
Fatal error: "int" cannot be used as a class name in %s on line %d
Fatal error: Cannot use 'int' as class name as it is reserved in %s on line %d

View file

@ -129,6 +129,53 @@ static zend_bool zend_get_unqualified_name(const zend_string *name, const char *
}
/* }}} */
struct reserved_class_name {
const char *name;
size_t len;
};
static const struct reserved_class_name reserved_class_names[] = {
{ZEND_STRL("bool")},
{ZEND_STRL("false")},
{ZEND_STRL("float")},
{ZEND_STRL("int")},
{ZEND_STRL("null")},
{ZEND_STRL("parent")},
{ZEND_STRL("self")},
{ZEND_STRL("static")},
{ZEND_STRL("string")},
{ZEND_STRL("true")},
{NULL, 0}
};
static zend_bool zend_is_reserved_class_name(const zend_string *name) /* {{{ */
{
const struct reserved_class_name *reserved = reserved_class_names;
const char *uqname = name->val;
size_t uqname_len = name->len;
zend_get_unqualified_name(name, &uqname, &uqname_len);
for (; reserved->name; ++reserved) {
if (uqname_len == reserved->len
&& zend_binary_strcasecmp(uqname, uqname_len, reserved->name, reserved->len) == 0
) {
return 1;
}
}
return 0;
}
/* }}} */
ZEND_API void zend_assert_valid_class_name(const zend_string *name) /* {{{ */
{
if (zend_is_reserved_class_name(name)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot use '%s' as class name as it is reserved", name->val);
}
}
/* }}} */
typedef struct _scalar_typehint_info {
const char* name;
const size_t name_len;
@ -143,39 +190,19 @@ static const scalar_typehint_info scalar_typehints[] = {
{NULL, 0, IS_UNDEF}
};
static zend_always_inline const scalar_typehint_info* zend_find_scalar_typehint(const zend_string *const_name) /* {{{ */
static zend_always_inline const scalar_typehint_info* zend_find_scalar_typehint(const zend_string *name) /* {{{ */
{
const scalar_typehint_info *info = &scalar_typehints[0];
const char *uqname;
size_t uqname_len;
if (!zend_get_unqualified_name(const_name, &uqname, &uqname_len)) {
uqname = const_name->val;
uqname_len = const_name->len;
}
while (info->name) {
if (uqname_len == info->name_len && zend_binary_strcasecmp(uqname, uqname_len, info->name, info->name_len) == 0) {
break;
for (; info->name; ++info) {
if (name->len == info->name_len
&& zend_binary_strcasecmp(name->val, name->len, info->name, info->name_len) == 0
) {
return info;
}
info++;
}
if (info->name) {
return info;
} else {
return NULL;
}
}
/* }}} */
ZEND_API void zend_assert_valid_class_name(const zend_string *const_name) /* {{{ */
{
const scalar_typehint_info *info = zend_find_scalar_typehint(const_name);
if (info) {
zend_error_noreturn(E_COMPILE_ERROR, "\"%s\" cannot be used as a class name", info->name);
}
return NULL;
}
/* }}} */
@ -184,9 +211,6 @@ static zend_always_inline zend_uchar zend_lookup_scalar_typehint_by_name(const z
const scalar_typehint_info *info = zend_find_scalar_typehint(const_name);
if (info) {
if (const_name->len != info->name_len) {
zend_error_noreturn(E_COMPILE_ERROR, "\"%s\" cannot be used as a type declaration", const_name->val);
}
return info->type;
} else {
return 0;
@ -4099,6 +4123,7 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_
} else {
if (zend_is_const_default_class_ref(return_type_ast)) {
class_name = zend_resolve_class_name_ast(return_type_ast);
zend_assert_valid_class_name(class_name);
} else {
zend_string_addref(class_name);
if (!is_method) {
@ -4225,9 +4250,9 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool is_
if (type != 0) {
arg_info->type_hint = type;
} else {
if (zend_is_const_default_class_ref(type_ast)) {
class_name = zend_resolve_class_name_ast(type_ast);
zend_assert_valid_class_name(class_name);
} else {
zend_string_addref(class_name);
}
@ -4846,10 +4871,7 @@ void zend_compile_class_decl(zend_ast *ast) /* {{{ */
return;
}
if (ZEND_FETCH_CLASS_DEFAULT != zend_get_class_fetch_type(name)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as class name as it is reserved",
name->val);
}
zend_assert_valid_class_name(name);
lcname = zend_string_tolower(name);
@ -4857,8 +4879,6 @@ void zend_compile_class_decl(zend_ast *ast) /* {{{ */
import_name = zend_hash_find_ptr(CG(current_import), lcname);
}
zend_assert_valid_class_name(name);
if (CG(current_namespace)) {
name = zend_prefix_with_ns(name);
@ -5094,19 +5114,13 @@ void zend_compile_use(zend_ast *ast) /* {{{ */
}
}
if (type == T_CLASS) {
zend_assert_valid_class_name(new_name);
}
if (case_sensitive) {
lookup_name = zend_string_copy(new_name);
} else {
lookup_name = zend_string_tolower(new_name);
}
if (type == T_CLASS && (zend_string_equals_literal(lookup_name, "self")
|| zend_string_equals_literal(lookup_name, "parent"))
) {
if (type == T_CLASS && zend_is_reserved_class_name(new_name)) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use %s as %s because '%s' "
"is a special class name", old_name->val, new_name->val, new_name->val);
}