0.8.0.8:
[sbcl.git] / src / pcl / defclass.lisp
index 99f2f7a..4226df5 100644 (file)
@@ -48,6 +48,7 @@
 (defvar *initfunctions-for-this-defclass*)
 (defvar *readers-for-this-defclass*)
 (defvar *writers-for-this-defclass*)
+(defvar *slot-names-for-this-defclass*)
 
 ;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is
 ;;; fixed. DEFCLASS always expands into a call to LOAD-DEFCLASS. Until
@@ -58,7 +59,7 @@
 ;;; After the metabraid has been setup, and the protocol for defining
 ;;; classes has been defined, the real definition of LOAD-DEFCLASS is
 ;;; installed by the file std-class.lisp
-(defmacro defclass (name %direct-superclasses %direct-slots &rest %options)
+(defmacro defclass (&environment env name %direct-superclasses %direct-slots &rest %options)
   (let ((supers  (copy-tree %direct-superclasses))
        (slots   (copy-tree %direct-slots))
        (options (copy-tree %options)))
              (error "The value of the :metaclass option (~S) is not a~%~
                      legal class name."
                     (cadr option)))
-           (setq metaclass
-                    (case (cadr option)
-                      (cl:standard-class 'standard-class)
-                      (cl:structure-class 'structure-class)
-                      (t (cadr option))))
+           (setq metaclass (cadr option))
            (setf options (remove option options))
            (return t))))
 
       (let ((*initfunctions-for-this-defclass* ())
             (*readers-for-this-defclass* ()) ;Truly a crock, but we got
-            (*writers-for-this-defclass* ())) ;to have it to live nicely.
+            (*writers-for-this-defclass* ()) ;to have it to live nicely.
+            (*slot-names-for-this-defclass* ()))
         (let ((canonical-slots
                 (mapcar (lambda (spec)
-                         (canonicalize-slot-specification name spec))
+                         (canonicalize-slot-specification name spec env))
                         slots))
               (other-initargs
                 (mapcar (lambda (option)
                     ,@(mapcar (lambda (x)
                                 `(declaim (ftype (function (t t) t) ,x)))
                               *writers-for-this-defclass*)
+                     ,@(mapcar (lambda (x)
+                                 `(declaim (ftype (function (t) t)
+                                                  ,(slot-reader-name x)
+                                                  ,(slot-boundp-name x))
+                                           (ftype (function (t t) t)
+                                                  ,(slot-writer-name x))))
+                               *slot-names-for-this-defclass*)
                     (let ,(mapcar #'cdr *initfunctions-for-this-defclass*)
                       (load-defclass ',name
                                      ',metaclass
                                                       '(:from-defclass-p t))
                                                     other-initargs)))))))
             (if defstruct-p
-               (let* ((include (or (and supers
-                                        (fix-super (car supers)))
-                                   (and (not (eq name 'structure-object))
-                                        *the-class-structure-object*)))
-                      (defstruct-form (make-structure-class-defstruct-form
-                                       name slots include)))
-                 `(progn
-                    (eval-when (:compile-toplevel :load-toplevel :execute)
-                      ,defstruct-form) ; really compile the defstruct-form
-                    (eval-when (:compile-toplevel :load-toplevel :execute)
-                      ,defclass-form)))
+               (progn
+                 ;; FIXME: (YUK!) Why do we do this? Because in order
+                 ;; to make the defstruct form, we need to know what
+                 ;; the accessors for the slots are, so we need
+                 ;; already to have hooked into the CLOS machinery.
+                 ;;
+                 ;; There may be a better way to do this: it would
+                 ;; involve knowing enough about PCL to ask "what
+                 ;; will my slot names and accessors be"; failing
+                 ;; this, we currently just evaluate the whole
+                 ;; kaboodle, and then use CLASS-DIRECT-SLOTS. --
+                 ;; CSR, 2002-06-07
+                 (eval defclass-form)
+                 (let* ((include (or (and supers
+                                          (fix-super (car supers)))
+                                     (and (not (eq name 'structure-object))
+                                          *the-class-structure-object*)))
+                        (defstruct-form (make-structure-class-defstruct-form
+                                         name (class-direct-slots (find-class name)) include)))
+                   `(progn
+                     (eval-when (:compile-toplevel :load-toplevel :execute)
+                       ,defstruct-form) ; really compile the defstruct-form
+                     (eval-when (:compile-toplevel :load-toplevel :execute)
+                       ,defclass-form))))
                `(progn
                   ;; By telling the type system at compile time about
                   ;; the existence of a class named NAME, we can avoid
             (push entry *initfunctions-for-this-defclass*))
           (cadr entry)))))
 
-(defun canonicalize-slot-specification (class-name spec)
-  (cond ((and (symbolp spec)
-             (not (keywordp spec))
-             (not (memq spec '(t nil))))
-        `'(:name ,spec))
-       ((not (consp spec))
-        (error "~S is not a legal slot specification." spec))
-       ((null (cdr spec))
+(defun canonicalize-slot-specification (class-name spec env)
+  (labels ((slot-name-illegal (reason)
+            (error 'simple-program-error
+                   :format-control
+                   (format nil "~~@<in DEFCLASS ~~S, the slot name in the ~
+                                 specification ~~S is ~A.~~@:>" reason)
+                   :format-arguments (list class-name spec)))
+          (check-slot-name-legality (name)
+            (cond
+              ((not (symbolp name))
+               (slot-name-illegal "not a symbol"))
+              ((keywordp name)
+               (slot-name-illegal "a keyword"))
+              ((constantp name env)
+               (slot-name-illegal "a constant")))))
+    (cond ((atom spec)
+          (check-slot-name-legality spec)
+          (push spec *slot-names-for-this-defclass*)
+          `'(:name ,spec))
+         ((null (cdr spec))
+          (check-slot-name-legality (car spec))
+          (push (car spec) *slot-names-for-this-defclass*)
         `'(:name ,(car spec)))
-       ((null (cddr spec))
-        (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
-                Convert it to ~S"
-               class-name spec (list (car spec) :initform (cadr spec))))
-       (t
-        (let* ((name (pop spec))
-               (readers ())
-               (writers ())
-               (initargs ())
-               (unsupplied (list nil))
-               (initform (getf spec :initform unsupplied)))
-          (doplist (key val) spec
-            (case key
-              (:accessor (push val readers)
-                         (push `(setf ,val) writers))
-              (:reader   (push val readers))
-              (:writer   (push val writers))
-              (:initarg  (push val initargs))))
-          (loop (unless (remf spec :accessor) (return)))
-          (loop (unless (remf spec :reader)   (return)))
-          (loop (unless (remf spec :writer)   (return)))
-          (loop (unless (remf spec :initarg)  (return)))
-          (setq *writers-for-this-defclass*
-                (append writers *writers-for-this-defclass*))
-          (setq *readers-for-this-defclass*
-                (append readers *readers-for-this-defclass*))
-          (setq spec `(:name     ',name
-                       :readers  ',readers
-                       :writers  ',writers
-                       :initargs ',initargs
-                       ',spec))
-          (if (eq initform unsupplied)
-              `(list* ,@spec)
-              `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
-                                               
+         ((null (cddr spec))
+          (error 'simple-program-error
+                 :format-control
+                 "~@<in DEFCLASS ~S, the slot specification ~S is invalid; ~
+                   the probable intended meaning may be achieved by ~
+                   specifiying ~S instead."
+                 :format-arguments
+                 (list class-name spec
+                       `(,(car spec) :initform ,(cadr spec)))))
+         (t
+          (let* ((name (car spec))
+                 (spec (cdr spec))
+                 (readers ())
+                 (writers ())
+                 (initargs ())
+                 (unsupplied (list nil))
+                 (initform (getf spec :initform unsupplied)))
+            (check-slot-name-legality name)
+            (push name *slot-names-for-this-defclass*)
+            (doplist (key val) spec
+              (case key
+                (:accessor (push val readers)
+                           (push `(setf ,val) writers))
+                (:reader   (push val readers))
+                (:writer   (push val writers))
+                (:initarg  (push val initargs))))
+            (loop (unless (remf spec :accessor) (return)))
+            (loop (unless (remf spec :reader)   (return)))
+            (loop (unless (remf spec :writer)   (return)))
+            (loop (unless (remf spec :initarg)  (return)))
+            (setq *writers-for-this-defclass*
+                  (append writers *writers-for-this-defclass*))
+            (setq *readers-for-this-defclass*
+                  (append readers *readers-for-this-defclass*))
+            (setq spec `(:name     ',name
+                         :readers  ',readers
+                         :writers  ',writers
+                         :initargs ',initargs
+                         ',spec))
+            (if (eq initform unsupplied)
+                `(list* ,@spec)
+                `(list* :initfunction ,(make-initfunction initform)
+                        ,@spec)))))))
+
 (defun canonicalize-defclass-option (class-name option)
   (declare (ignore class-name))
   (case (car option)
        canonical-options (copy-tree canonical-options))
   (let ((ecd
          (make-early-class-definition name
-                                      *load-truename*
+                                      *load-pathname*
                                       metaclass
                                       supers
                                       canonical-slots