Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_compile.c zend_compile.h zend_language_parser.y

From: Date: Mon, 09 Dec 2002 16:25:16 +0000
Subject: Re: [ZEND-ENGINE-CVS] cvs: ZendEngine2 / zend_compile.c zend_compile.h zend_language_parser.y
References: 1  Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-703@lists.php.net to get a copy of this message
At 13:10 09.12.2002, Zeev Suraski wrote:
zeev            Mon Dec  9 07:10:18 2002 EDT
Modified files:
    /ZendEngine2        zend_compile.c zend_compile.h zend_language_parser.y
Log: - Allow variables to have both 'static' modifier and an access level.
    NOTE:  This only works at the syntax level right now (parser).  It
           doesn't actually work as of yet - all statics are considered
           public for now
I haven't tried it out but:
                T_PUBLIC                                { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
        |       T_PROTECTED                             { $$.u.constant.value.lval = ZEND_ACC_PROTECTED; }
        |       T_PRIVATE                               { $$.u.constant.value.lval = ZEND_ACC_PRIVATE; }
        |       T_STATIC                                { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
        |       T_STATIC                                { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
+       |       T_PUBLIC T_STATIC                       { $$.u.constant.value.lval = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC; }
+       |       T_PROTECTED T_STATIC            { $$.u.constant.value.lval = ZEND_ACC_STATIC | ZEND_ACC_PROTECTED; }
+       |       T_PRIVATE T_STATIC                      { $$.u.constant.value.lval = ZEND_ACC_STATIC | ZEND_ACC_PRIVATE; }
should work. At least it worked for all versions i experimented with and the important part for that is not very different from your actual implementation.
- Prevent users from putting more restrictions on methods in derived classes
    (i.e., you cannot make a public method private in a derived class, etc.)
I do not like it because it is an error from language design. Visibility is about information hiding and not information showing. Also Stanislav is wrong in his comment about 'is_a' but i will explain in more detailed there soon. marcus
Index: ZendEngine2/zend_compile.c diff -u ZendEngine2/zend_compile.c:1.333 ZendEngine2/zend_compile.c:1.334
--- ZendEngine2/zend_compile.c:1.333    Sun Dec  8 09:10:58 2002
+++ ZendEngine2/zend_compile.c Mon Dec 9 07:10:17 2002 @@ -1590,6 +1590,12 @@
        if ((child_flags & ZEND_ACC_ABSTRACT) && !(parent_flags & ZEND_ACC_ABSTRACT)) {
                zend_error(E_COMPILE_ERROR, "Cannot make non abstract method %s::%s() abstract in class %s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, ZEND_FN_SCOPE_NAME(child));
        }
+
+       /* Prevent derived classes from restricting access that was available in parent classes
+       */
+       if ((child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
+               zend_error(E_COMPILE_ERROR, "Access level to %s::%s() must be %s (as in class %s)%s", ZEND_FN_SCOPE_NAME(parent), child->common.function_name, zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(child), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
+       }
        return SUCCESS;
} @@ -2201,6 +2207,12 @@
                property->type = IS_NULL;
        }
+       if (CG(access_type) & ZEND_ACC_STATIC) {
+               /* FIXME:  Currently, ignores access type for static variables */
+               zend_hash_update(CG(active_class_entry)->static_members, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
+               return;
+       }
+
        switch (CG(access_type)) {
                case ZEND_ACC_PRIVATE:
                        {
@@ -2244,9 +2256,6 @@
                        }
                case ZEND_ACC_PUBLIC:
zend_hash_update(&CG(active_class_entry)->default_properties, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
-                       break;
-               case ZEND_ACC_STATIC:
- zend_hash_update(CG(active_class_entry)->static_members, var_name->u.constant.value.str.val, var_name->u.constant.value.str.len+1, &property, sizeof(zval *), NULL);
                        break;
        }
        FREE_PNODE(var_name);
Index: ZendEngine2/zend_compile.h diff -u ZendEngine2/zend_compile.h:1.199 ZendEngine2/zend_compile.h:1.200
--- ZendEngine2/zend_compile.h:1.199    Sun Dec  8 09:09:43 2002
+++ ZendEngine2/zend_compile.h Mon Dec 9 07:10:17 2002 @@ -91,9 +91,7 @@
 #define ZEND_ACC_STATIC                0x01
 #define ZEND_ACC_ABSTRACT      0x02
-/* No visibility is the same as public visibility. - * For inheritance no visibility means inheriting the visibility. - */ +/* The order of those must be kept - public < protected < private */
 #define ZEND_ACC_PUBLIC    0x10
#define ZEND_ACC_PROTECTED 0x20 #define ZEND_ACC_PRIVATE 0x40 Index: ZendEngine2/zend_language_parser.y diff -u ZendEngine2/zend_language_parser.y:1.82 ZendEngine2/zend_language_parser.y:1.83
--- ZendEngine2/zend_language_parser.y:1.82     Sun Dec  8 09:09:43 2002
+++ ZendEngine2/zend_language_parser.y Mon Dec 9 07:10:17 2002 @@ -440,7 +440,7 @@ class_statement:
-               variable_modifier { CG(access_type) = $1.u.constant.value.lval; } class_variable_declaration ';'
+               variable_modifiers { CG(access_type) = $1.u.constant.value.lval; } class_variable_declaration ';'
        |       class_constant_declaration ';'
        |       method_modifiers T_FUNCTION { $2.u.opline_num = CG(zend_lineno); } is_reference T_STRING { zend_do_begin_function_declaration(&$2, &$5, 1, $4.op_type, $1.u.constant.value.lval TSRMLS_CC); } '('
                        parameter_list ')' '{' inner_statement_list '}' { zend_do_end_function_declaration(&$2 TSRMLS_CC); }
@@ -449,28 +449,26 @@
        |       T_CLASS T_STRING extends_from '{' { zend_do_begin_class_declaration(&$1, &$2, &$3 TSRMLS_CC); } class_statement_list '}' { zend_do_end_class_declaration(&$1 TSRMLS_CC); }
; -variable_modifier:
-               access_modifier         { $$ = $1; }
-       |       T_VAR                           { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
-       |       T_STATIC                        { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
+variable_modifiers:
+               non_empty_access_modifiers              { $$ = $1; }
+       |       T_VAR                                                   { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
; method_modifiers:
                /* empty */                                                     { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
-       |       non_empty_method_modifiers                      { $$ = $1;  if (!($$.u.constant.value.lval & ZEND_ACC_PPP_MASK)) { $$.u.constant.value.lval |= ZEND_ACC_PUBLIC; } }
+       |       non_empty_access_modifiers                      { $$ = $1;  if (!($$.u.constant.value.lval & ZEND_ACC_PPP_MASK)) { $$.u.constant.value.lval |= ZEND_ACC_PUBLIC; } }
; -non_empty_method_modifiers:
-               T_STATIC      { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
-       |       access_modifier                                         { $$ = $1; }
-       |       non_empty_method_modifiers T_STATIC                     { $$.u.constant.value.lval = $1.u.constant.value.lval | ZEND_ACC_STATIC; }
-       |       non_empty_method_modifiers access_modifier      { $$.u.constant.value.lval = zend_do_verify_access_types(&$1, &$2); }
+non_empty_access_modifiers:
+               access_modifier                                         { $$ = $1; }
+       |       non_empty_access_modifiers access_modifier      { $$.u.constant.value.lval = zend_do_verify_access_types(&$1, &$2); }
; access_modifier:
                T_PUBLIC                                { $$.u.constant.value.lval = ZEND_ACC_PUBLIC; }
        |       T_PROTECTED                             { $$.u.constant.value.lval = ZEND_ACC_PROTECTED; }
        |       T_PRIVATE                               { $$.u.constant.value.lval = ZEND_ACC_PRIVATE; }
+       |       T_STATIC                                { $$.u.constant.value.lval = ZEND_ACC_STATIC; }
; class_variable_declaration: -- Zend Engine CVS Mailing List (http://cvs.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.zend-engine.cvs (#703) next »