Changeset 78

Show
Ignore:
Timestamp:
08/05/07 02:57:38 (17 months ago)
Author:
weyrick
Message:

initial work on PHP5 property visibility. not fully working yet.

Location:
trunk/pcc
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/pcc/compiler/ast.scm

    r39 r78  
    110110       name 
    111111       value 
    112        static?) 
     112       static? 
     113       visibility) 
    113114    (final-class method-decl::declaration 
    114115       name 
  • trunk/pcc/compiler/evaluate.scm

    r39 r78  
    750750                      (if (null? (property-decl-value prop)) 
    751751                          (make-container '()) 
    752                           (d/evaluate (property-decl-value prop)))))) 
     752                          (d/evaluate (property-decl-value prop))) 
     753                      (property-decl-visibility prop)))) 
    753754 
    754755             ;; PHP5 class constants 
  • trunk/pcc/compiler/generate.scm

    r39 r78  
    782782;                                    (get-location (property-decl-value prop)) 
    783783                                      ''() 
    784                                       (get-value (property-decl-value prop)) 
    785                                      )) 
     784                                      (get-value (property-decl-value prop))) 
     785                                 ',(property-decl-visibility prop) 
     786                                 ) 
    786787                             code))) 
    787788                (php-hash-for-each class-constants 
  • trunk/pcc/compiler/parser.scm

    r39 r78  
    234234       ((class-function) class-function) 
    235235       ((varkey class-vars semi) class-vars) 
    236        ;; XXX public, private, and protected don't do anything yet 
     236       ((public class-vars semi) 
     237        (parse-require-php5) 
     238        class-vars) 
    237239       ((private class-vars semi) 
    238240        (parse-require-php5) 
    239         class-vars) 
    240        ((public class-vars semi) 
    241         (parse-require-php5) 
    242         class-vars) 
     241        (map (lambda (c) (property-decl-visibility-set! c 'private) c) class-vars)) 
    243242       ((protected class-vars semi) 
    244243        (parse-require-php5) 
    245         class-vars) 
     244        (map (lambda (c) (property-decl-visibility-set! c 'protected) c) class-vars)) 
    246245       ((static class-vars semi) 
    247246        (parse-require-php5) 
     
    254253      (class-var 
    255254       ((var equals decl-literal) 
    256         (make-property-decl *parse-loc* var decl-literal #f)) 
     255        (make-property-decl *parse-loc* var decl-literal #f 'public)) 
    257256       ((var) 
    258         (make-property-decl *parse-loc* var '() #f))) 
     257        (make-property-decl *parse-loc* var '() #f 'public))) 
    259258 
    260259      (decl-literal 
  • trunk/pcc/runtime/php-object.scm

    r51 r78  
    7272    (construct-php-object-sans-constructor class-name) 
    7373    (get-declared-php-classes) 
    74     (define-php-property class-name property-name value) 
    75     (define-php-method class-name method-name method)     
     74    (define-php-property class-name property-name value visibility) 
     75    (define-php-method class-name method-name method) 
    7676    (define-class-constant class-name constant-name value) 
    7777    (lookup-class-constant class-name constant-name))) 
     
    100100   ;;a hashtable mapping names of declared properties to an index in a property vector 
    101101   declared-property-offsets 
    102    ;;properties is a vector of properties 
     102   ;;properties is a vector of all properties (which points to actual php data) 
    103103   properties 
     104   ;; a list of protected properties (unmangled) 
     105   protected-properties 
     106   ;; a list of private properties (unmangled) 
     107   private-properties 
    104108   ;;extended properties is either #f or a php-hash of non-declared properties 
    105109   extended-properties 
     
    592596              #f)))) 
    593597 
    594 ; XXX update for PHP5 
    595598(define (%class-name-canonicalize name) 
    596599   "define class names as case-insensitive strings" 
    597    ;   (string->symbol 
    598600   (string-downcase (mkstr name))) 
    599     ;)) 
    600601 
    601602; XXX update for PHP5 
     
    625626   ;define the root of the class hierarchy 
    626627   (let ((stdclass (%php-class "stdClass" "stdclass" #f #f 
    627                                (make-hashtable) (make-vector 0) 
     628                               (make-hashtable) (make-vector 0) '() '() 
    628629                               (make-php-hash) (make-php-hash) 
    629630                               #f #f #f (make-php-hash))) 
    630631         (inc-class (%php-class "__PHP_Incomplete_Class" "__php_incomplete_class" #f #f 
    631                                 (make-hashtable) (make-vector 0) 
     632                                (make-hashtable) (make-vector 0) '() '() 
    632633                                (make-php-hash) (make-php-hash) 
    633634                                #f #f #f (make-php-hash)) )) 
     
    636637      (hashtable-put! %php-class-registry "__php_incomplete_class" inc-class))) 
    637638 
     639;; XXX this is in newer versions of bigloo, yank it when we upgrade 
     640(define (list-copy list) 
     641   (if (null? list) 
     642       '() 
     643       (cons (car list) 
     644             (list-copy (cdr list))))) 
    638645 
    639646(define (define-php-class name parent-name) 
     
    646653             (php-error "Defining class " name ": unable to find parent class " parent-name)) 
    647654          (let* ((canonical-name (%class-name-canonicalize name)) 
    648                  (new-class (%php-class (string-downcase (mkstr name)) 
     655                 (new-class (%php-class (if PHP5? 
     656                                            (mkstr name) 
     657                                            (string-downcase (mkstr name))) 
    649658                                        canonical-name 
    650659                                        parent-class 
     
    653662                                         (%php-class-declared-property-offsets parent-class)) 
    654663                                        (copy-properties-vector (%php-class-properties parent-class)) 
     664                                        '() ; private props 
     665                                        (list-copy (%php-class-protected-properties parent-class)) 
    655666                                        (copy-php-data (%php-class-extended-properties parent-class)) 
    656667                                        (copy-php-data (%php-class-methods parent-class)) 
     
    715726                           method)))) 
    716727 
    717 (define (define-php-property class-name property-name value) 
     728(define (mangle-property-private prop) 
     729   "mangle given property string to private visibility" 
     730   (mkstr prop ":private")) 
     731 
     732(define (mangle-property-protected prop) 
     733   "mangle given property string to protected visibility" 
     734   (mkstr prop ":protected")) 
     735 
     736(define (%property-name-mangle name visibility) 
     737   (cond 
     738      ((eqv? 'public visibility) 
     739       name) 
     740      ((eqv? 'private visibility) 
     741       (mangle-property-private name)) 
     742      ((eqv? 'protected visibility) 
     743       (mangle-property-protected name)))) 
     744 
     745(define (define-php-property class-name property-name value visibility) 
    718746   (let ((the-class (%lookup-class class-name))) 
    719747      (unless the-class 
     
    721749      (let* ((properties (%php-class-properties the-class)) 
    722750             (offset (vector-length properties)) 
    723              (canonical-name (%property-name-canonicalize property-name))) 
    724          (aif (hashtable-get (%php-class-declared-property-offsets the-class) canonical-name) 
     751             (canonical-name (%property-name-canonicalize property-name)) 
     752             (mangled-name (%property-name-mangle canonical-name visibility))) 
     753         (aif (hashtable-get (%php-class-declared-property-offsets the-class) mangled-name) 
    725754              ;; already defined, just set it 
    726755              (vector-set! properties it (make-container (maybe-unbox value))) 
     
    732761                  (cruddy-push-extend (make-container (maybe-unbox value)) properties)) 
    733762                 (hashtable-put! (%php-class-declared-property-offsets the-class) 
    734                                  canonical-name 
     763                                 mangled-name 
    735764                                 offset) 
    736765                 ;store the reverse, too 
    737766                 (hashtable-put! (%php-class-declared-property-offsets the-class) 
    738767                                 offset 
    739                                  canonical-name)))))) 
     768                                 mangled-name) 
     769                 ;store visibility 
     770                 (when (eqv? 'private visibility) 
     771                  (%php-class-private-properties-set! the-class (cons canonical-name (%php-class-private-properties the-class)))) 
     772                 (when (eqv? 'protected visibility) 
     773                  (%php-class-protected-properties-set! the-class (cons canonical-name (%php-class-protected-properties the-class))))))))) 
    740774 
    741775(define (%lookup-class name) 
     
    778812           #f)))) 
    779813 
     814;; this handles visibility mangling 
     815;; it will check in order: public (no mangle), protected, private 
     816;; it makes no attempt to restrict the property, it simply returns it if available in mangled form 
    780817(define (%prop-offset obj prop-canon-name) 
    781    (hashtable-get 
    782     (%php-class-declared-property-offsets 
    783      (%php-object-class obj)) 
    784     prop-canon-name)) 
    785  
     818   (let ((prop (hashtable-get 
     819                (%php-class-declared-property-offsets 
     820                 (%php-object-class obj)) 
     821                prop-canon-name))) 
     822      (if (or prop (not PHP5?)) 
     823          ; found a public 
     824          prop 
     825          (let ((prop (hashtable-get 
     826                       (%php-class-declared-property-offsets 
     827                        (%php-object-class obj)) 
     828                       (mangle-property-protected prop-canon-name)))) 
     829             (if prop 
     830                 ; found a protected 
     831                 prop 
     832                 ; either privte or nothin' 
     833                 (hashtable-get 
     834                  (%php-class-declared-property-offsets 
     835                   (%php-object-class obj)) 
     836                  (mangle-property-private prop-canon-name))))))) 
     837                 
    786838;;;;the actual property looker-uppers 
    787839(define (%lookup-prop-ref obj property)