com php-src: Refactor typehint lookup: Zend/zend_compile.c Zend/zend_compile.h
| From: | Dmitry Stogov | Date: | Tue, 10 Feb 2015 15:48:08 +0000 |
| Subject: | com php-src: Refactor typehint lookup: Zend/zend_compile.c Zend/zend_compile.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-85715@lists.php.net to get a copy of this message | ||
Commit: c7719ab4e5e49cdcef350a873ce4067bab3b7c02
Author: Andrea Faulds <ajf@ajf.me> Sun, 1 Feb 2015 14:22:48 +0000
Parents: cbbaea08da8363358fa2257f7b441ab9d148aae4
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=c7719ab4e5e49cdcef350a873ce4067bab3b7c02
Log:
Refactor typehint lookup
Changed paths:
M Zend/zend_compile.c
M Zend/zend_compile.h
Diff:
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 1d5466b..0f5fb03 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -139,6 +139,71 @@ static zend_bool zend_get_unqualified_name(const zend_string *name, const char
*
}
/* }}} */
+struct _scalar_typehint_info {
+ const char* name;
+ const size_t name_len;
+ const zend_uchar type;
+};
+
+static const struct _scalar_typehint_info scalar_typehints[] = {
+ {"int", sizeof("int") - 1, IS_LONG},
+ {"integer", sizeof("integer") - 1, IS_LONG},
+ {"float", sizeof("float") - 1, IS_DOUBLE},
+ {"string", sizeof("string") - 1, IS_STRING},
+ {"bool", sizeof("bool") - 1, _IS_BOOL},
+ {"boolean", sizeof("boolean") - 1, _IS_BOOL},
+ {NULL, 0, IS_UNDEF}
+};
+
+static zend_always_inline const struct _scalar_typehint_info* zend_find_scalar_typehint(const
zend_string *const_name) /* {{{ */
+{
+ const struct _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;
+ }
+ info++;
+ }
+
+ if (info->name) {
+ return info;
+ } else {
+ return NULL;
+ }
+}
+/* }}} */
+
+ZEND_API void zend_assert_valid_class_name(const zend_string *const_name) /* {{{ */
+{
+ const struct _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);
+ }
+}
+/* }}} */
+
+static zend_always_inline zend_uchar zend_lookup_scalar_typehint_by_name(const zend_string
*const_name) /* {{{ */
+{
+ const struct _scalar_typehint_info *info = zend_find_scalar_typehint(const_name);
+
+ if (info) {
+ return info->type;
+ } else {
+ return 0;
+ }
+}
+/* }}} */
+
+
static void init_compiler_declarables(void) /* {{{ */
{
ZVAL_LONG(&CG(declarables).ticks, 0);
@@ -3962,15 +4027,12 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, zend_bool
is_
}
} else {
zend_string *class_name = zend_ast_get_str(type_ast);
- const struct _scalar_typehint_info *info = &scalar_typehints[0];
+ zend_uchar type;
- while (info->name) {
- if (class_name->len == info->name_len
- && zend_binary_strcasecmp(class_name->val, info->name_len, info->name,
info->name_len) == 0) {
- arg_info->type_hint = info->type;
- goto done;
- }
- info++;
+ type = zend_lookup_scalar_typehint_by_name(class_name);
+ if (type != 0) {
+ arg_info->type_hint = type;
+ goto done;
}
if (zend_is_const_default_class_ref(type_ast)) {
diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h
index 3d44a08..ae5a042 100644
--- a/Zend/zend_compile.h
+++ b/Zend/zend_compile.h
@@ -31,45 +31,6 @@
#include "zend_llist.h"
-struct _scalar_typehint_info {
- const char* name;
- const size_t name_len;
- const zend_uchar type;
-};
-
-static const struct _scalar_typehint_info scalar_typehints[] = {
- {"int", sizeof("int") - 1, IS_LONG},
- {"integer", sizeof("integer") - 1, IS_LONG},
- {"float", sizeof("float") - 1, IS_DOUBLE},
- {"string", sizeof("string") - 1, IS_STRING},
- {"bool", sizeof("bool") - 1, _IS_BOOL},
- {"boolean", sizeof("boolean") - 1, _IS_BOOL},
- {NULL, 0, IS_UNDEF}
-};
-
-static zend_always_inline void zend_assert_valid_class_name(const zend_string *const_name)
-{
- const struct _scalar_typehint_info *info = &scalar_typehints[0];
- const char *end_slash = strrchr(const_name->val, '\\');
- zend_string *name = (zend_string*)const_name;
-
- if (end_slash) {
- end_slash++;
- name = zend_string_init(end_slash, strlen(end_slash), 0);
- }
-
- while (info->name) {
- if (name->len == info->name_len && zend_binary_strcasecmp(name->val,
name->len, info->name, info->name_len) == 0) {
- zend_error_noreturn(E_COMPILE_ERROR, "\"%s\" cannot be used as a class
name", name->val);
- }
- info++;
- }
-
- if (end_slash) {
- zend_string_release(name);
- }
-}
-
#define DEBUG_ZEND 0
#define SET_UNUSED(op) op ## _type = IS_UNUSED
@@ -777,6 +738,8 @@ int zendlex(zend_parser_stack_elem *elem);
int zend_add_literal(zend_op_array *op_array, zval *zv);
+ZEND_API void zend_assert_valid_class_name(const zend_string *const_name);
+
/* BEGIN: OPCODES */
#include "zend_vm_opcodes.h"