com php-src: Add NS error checks: Zend/zend_compile.c Zen d/zend_compile.h Zend/zend_language_parser.y
| From: | Nikita Popov | Date: | Tue, 22 Jul 2014 12:08:52 +0000 |
| Subject: | com php-src: Add NS error checks: Zend/zend_compile.c Zen d/zend_compile.h Zend/zend_language_parser.y | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-80946@lists.php.net to get a copy of this message | ||
Commit: de2f132f33aedb0945d99ac1b9ccf079795c6d28
Author: Nikita Popov <nikic@php.net> Tue, 22 Jul 2014 14:08:52 +0200
Parents: 776800b0a01793df0e907bc1dc0976ba8dd378d0
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=de2f132f33aedb0945d99ac1b9ccf079795c6d28
Log:
Add NS error checks
Those can probably be implemented in a better way now...
Changed paths:
M Zend/zend_compile.c
M Zend/zend_compile.h
M Zend/zend_language_parser.y
Diff:
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 696845c..eb7a8f1 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -3432,91 +3432,6 @@ void zend_do_build_namespace_name(znode *result, znode *prefix, znode *name
TSRM
}
/* }}} */
-void zend_do_begin_namespace(znode *name, zend_bool with_bracket TSRMLS_DC) /* {{{ */
-{
- char *lcname;
-
- /* handle mixed syntax declaration or nested namespaces */
- if (!CG(has_bracketed_namespaces)) {
- if (Z_TYPE(CG(current_namespace)) != IS_UNDEF) {
- /* previous namespace declarations were unbracketed */
- if (with_bracket) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations with
unbracketed namespace declarations");
- }
- }
- } else {
- /* previous namespace declarations were bracketed */
- if (!with_bracket) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations with
unbracketed namespace declarations");
- } else if (Z_TYPE(CG(current_namespace)) != IS_UNDEF || CG(in_namespace)) {
- zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
- }
- }
-
- if (((!with_bracket && Z_TYPE(CG(current_namespace)) == IS_UNDEF) || (with_bracket
&& !CG(has_bracketed_namespaces))) && CG(active_op_array)->last > 0) {
- /* ignore ZEND_EXT_STMT and ZEND_TICKS */
- int num = CG(active_op_array)->last;
- while (num > 0 &&
- (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
- CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
- --num;
- }
- if (num > 0) {
- zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be the very
first statement in the script");
- }
- }
-
- CG(in_namespace) = 1;
- if (with_bracket) {
- CG(has_bracketed_namespaces) = 1;
- }
-
- if (name) {
- lcname = zend_str_tolower_dup(Z_STRVAL(name->u.constant), Z_STRLEN(name->u.constant));
- if (((Z_STRLEN(name->u.constant) == sizeof("self")-1) &&
- !memcmp(lcname, "self", sizeof("self")-1)) ||
- ((Z_STRLEN(name->u.constant) == sizeof("parent")-1) &&
- !memcmp(lcname, "parent", sizeof("parent")-1))) {
- zend_error_noreturn(E_COMPILE_ERROR, "Cannot use '%s' as namespace name",
Z_STRVAL(name->u.constant));
- }
- efree(lcname);
-
- if (Z_TYPE(CG(current_namespace)) != IS_UNDEF) {
- zval_dtor(&CG(current_namespace));
- }
- ZVAL_COPY_VALUE(&CG(current_namespace), &name->u.constant);
- } else {
- if (Z_TYPE(CG(current_namespace)) != IS_UNDEF) {
- zval_dtor(&CG(current_namespace));
- ZVAL_UNDEF(&CG(current_namespace));
- }
- }
-
- if (CG(current_import)) {
- zend_hash_destroy(CG(current_import));
- efree(CG(current_import));
- CG(current_import) = NULL;
- }
-
- if (CG(current_import_function)) {
- zend_hash_destroy(CG(current_import_function));
- efree(CG(current_import_function));
- CG(current_import_function) = NULL;
- }
-
- if (CG(current_import_const)) {
- zend_hash_destroy(CG(current_import_const));
- efree(CG(current_import_const));
- CG(current_import_const) = NULL;
- }
-
- if (CG(doc_comment)) {
- STR_RELEASE(CG(doc_comment));
- CG(doc_comment) = NULL;
- }
-}
-/* }}} */
-
void zend_verify_namespace(TSRMLS_D) /* {{{ */
{
if (CG(has_bracketed_namespaces) && !CG(in_namespace)) {
@@ -6431,9 +6346,44 @@ void zend_compile_namespace(zend_ast *ast TSRMLS_DC) {
zend_ast *name_ast = ast->child[0];
zend_ast *stmt_ast = ast->child[1];
zend_string *name;
+ zend_bool with_bracket = stmt_ast != NULL;
// TODO.AST errors!
+ /* handle mixed syntax declaration or nested namespaces */
+ if (!CG(has_bracketed_namespaces)) {
+ if (Z_TYPE(CG(current_namespace)) != IS_UNDEF) {
+ /* previous namespace declarations were unbracketed */
+ if (with_bracket) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
+ "with unbracketed namespace declarations");
+ }
+ }
+ } else {
+ /* previous namespace declarations were bracketed */
+ if (!with_bracket) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Cannot mix bracketed namespace declarations "
+ "with unbracketed namespace declarations");
+ } else if (Z_TYPE(CG(current_namespace)) != IS_UNDEF || CG(in_namespace)) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Namespace declarations cannot be nested");
+ }
+ }
+
+ if (((!with_bracket && Z_TYPE(CG(current_namespace)) == IS_UNDEF)
+ || (with_bracket && !CG(has_bracketed_namespaces))) &&
CG(active_op_array)->last > 0
+ ) {
+ /* ignore ZEND_EXT_STMT and ZEND_TICKS */
+ zend_uint num = CG(active_op_array)->last;
+ while (num > 0 &&
+ (CG(active_op_array)->opcodes[num-1].opcode == ZEND_EXT_STMT ||
+ CG(active_op_array)->opcodes[num-1].opcode == ZEND_TICKS)) {
+ --num;
+ }
+ if (num > 0) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Namespace declaration statement has to be the very
first statement in the script");
+ }
+ }
+
if (Z_TYPE(CG(current_namespace)) != IS_UNDEF) {
zval_dtor(&CG(current_namespace));
}
@@ -6452,9 +6402,10 @@ void zend_compile_namespace(zend_ast *ast TSRMLS_DC) {
zend_reset_import_tables(TSRMLS_C);
- // TODO.AST reset doc comment
-
CG(in_namespace) = 1;
+ if (with_bracket) {
+ CG(has_bracketed_namespaces) = 1;
+ }
if (stmt_ast) {
zend_compile_top_stmt(stmt_ast TSRMLS_CC);
@@ -7324,6 +7275,9 @@ void zend_compile_top_stmt(zend_ast *ast TSRMLS_DC) {
}
zend_compile_stmt(ast TSRMLS_CC);
+ if (ast->kind != ZEND_AST_NAMESPACE) {
+ zend_verify_namespace(TSRMLS_C);
+ }
if (ast->kind == ZEND_AST_FUNC_DECL || ast->kind == ZEND_AST_CLASS) {
zend_do_early_binding(TSRMLS_C);
}
diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h
index db7573a..9ee1970 100644
--- a/Zend/zend_compile.h
+++ b/Zend/zend_compile.h
@@ -516,7 +516,6 @@ void zend_do_ticks(TSRMLS_D);
void zend_do_abstract_method(const znode *function_name, znode *modifiers, const znode *body
TSRMLS_DC);
void zend_do_build_namespace_name(znode *result, znode *prefix, znode *name TSRMLS_DC);
-void zend_do_begin_namespace(znode *name, zend_bool with_brackets TSRMLS_DC);
void zend_do_end_namespace(TSRMLS_D);
void zend_verify_namespace(TSRMLS_D);
void zend_do_end_compilation(TSRMLS_D);
diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
index a47ffbb..81e961d 100644
--- a/Zend/zend_language_parser.y
+++ b/Zend/zend_language_parser.y
@@ -243,7 +243,6 @@ name:
{ $$.u.ast = zend_ast_create_zval_ex(&$2.u.constant, ZEND_NAME_FQ); }
;
-/* TODO.AST early binding! */
top_statement:
statement { $$.u.ast = $1.u.ast; }
| function_declaration_statement { $$.u.ast = $1.u.ast; }