0.8.20.6:
[sbcl.git] / src / pcl / init.lisp
index cce5b7f..a4f5e3a 100644 (file)
 
 (defmethod make-instance ((class class) &rest initargs)
   (unless (class-finalized-p class) (finalize-inheritance class))
-  (setq initargs (default-initargs class initargs))
-  #||
-  (check-initargs-1
-   class initargs
-   (list (list* 'allocate-instance class initargs)
-        (list* 'initialize-instance (class-prototype class) initargs)
-        (list* 'shared-initialize (class-prototype class) t initargs)))
-  ||#
-  (let* ((info (initialize-info class initargs))
-        (valid-p (initialize-info-valid-p info)))
-    (when (and (consp valid-p) (eq (car valid-p) :invalid))
-      (error 'simple-program-error
-            :format-control "Invalid initialization argument ~S for class ~S"
-            :format-arguments (list (cdr valid-p) (class-name class)))))
-  (let ((instance (apply #'allocate-instance class initargs)))
-    (apply #'initialize-instance instance initargs)
-    instance))
-
-(defvar *default-initargs-flag* (list nil))
-
-(defmethod default-initargs ((class slot-class) supplied-initargs)
-  (call-initialize-function
-   (initialize-info-default-initargs-function
-    (initialize-info class supplied-initargs))
-   nil supplied-initargs)
-  #||
-  ;; This implementation of default initargs is critically dependent
-  ;; on all-default-initargs not having any duplicate initargs in it.
-  (let ((all-default (class-default-initargs class))
-       (miss *default-initargs-flag*))
-    (flet ((getf* (plist key)
-            (do ()
-                ((null plist) miss)
-              (if (eq (car plist) key)
-                  (return (cadr plist))
-                  (setq plist (cddr plist))))))
-      (labels ((default-1 (tail)
-                (if (null tail)
-                    nil
-                    (if (eq (getf* supplied-initargs (caar tail)) miss)
-                        (list* (caar tail)
-                               (funcall (cadar tail))
-                               (default-1 (cdr tail)))
-                        (default-1 (cdr tail))))))
-       (append supplied-initargs (default-1 all-default)))))
-  ||#)
+  (let ((class-default-initargs (class-default-initargs class)))
+    (when class-default-initargs
+      (setf initargs (default-initargs class initargs class-default-initargs)))
+    (when initargs
+      (when (and (eq *boot-state* 'complete)
+                 (not (getf initargs :allow-other-keys)))
+        (let ((class-proto (class-prototype class)))
+          (check-initargs-1
+           class initargs
+           (append (compute-applicable-methods
+                    #'allocate-instance (list class))
+                   (compute-applicable-methods 
+                    #'initialize-instance (list class-proto))
+                   (compute-applicable-methods 
+                    #'shared-initialize (list class-proto t)))))))
+    (let ((instance (apply #'allocate-instance class initargs)))
+      (apply #'initialize-instance instance initargs)
+      instance)))
+
+(defmethod default-initargs ((class slot-class)
+                            supplied-initargs
+                            class-default-initargs)
+  (loop for (key fn) in class-default-initargs
+        when (eq (getf supplied-initargs key '.not-there.) '.not-there.)
+         append (list key (funcall fn)) into default-initargs
+        finally
+         (return (append supplied-initargs default-initargs))))
 
 (defmethod initialize-instance ((instance slot-object) &rest initargs)
   (apply #'shared-initialize instance t initargs))
 
 (defmethod reinitialize-instance ((instance slot-object) &rest initargs)
-  #||
-  (check-initargs-1
-   (class-of instance) initargs
-   (list (list* 'reinitialize-instance instance initargs)
-        (list* 'shared-initialize instance nil initargs)))
-  ||#
-  (let* ((class (class-of instance))
-        (info (initialize-info class initargs))
-        (valid-p (initialize-info-ri-valid-p info)))
-    (when (and (consp valid-p) (eq (car valid-p) :invalid))
-      (error 'simple-program-error 
-            :format-control "Invalid initialization argument ~S for class ~S"
-            :format-arguments (list (cdr valid-p) (class-name class)))))
+  ;; the ctor machinery allows us to track when memoization of
+  ;; validity of initargs should be cleared.
+  (check-ri-initargs instance initargs)
   (apply #'shared-initialize instance nil initargs)
   instance)
 
         (list* 'shared-initialize instance added-slots initargs)))
   (apply #'shared-initialize instance added-slots initargs))
 
-(defmethod shared-initialize
-    ((instance slot-object) slot-names &rest initargs)
-  (cond
-    ((eq slot-names t)
-     (call-initialize-function
-      (initialize-info-shared-initialize-t-fun
-       (initialize-info (class-of instance) initargs))
-      instance initargs))
-    ((eq slot-names nil)
-     (call-initialize-function
-      (initialize-info-shared-initialize-nil-fun
-       (initialize-info (class-of instance) initargs))
-      instance initargs))
-    (t
-     ;; Initialize the instance's slots in a two step process:
-     ;;   (1) A slot for which one of the initargs in initargs can set
-     ;;       the slot, should be set by that initarg. If more than
-     ;;       one initarg in initargs can set the slot, the leftmost
-     ;;       one should set it.
-     ;;   (2) Any slot not set by step 1, may be set from its initform
-     ;;       by step 2. Only those slots specified by the slot-names
-     ;;       argument are set. If slot-names is:
-     ;;       T
-     ;;              then any slot not set in step 1 is set from its
-     ;;              initform.
-     ;;       <list of slot names>
-     ;;              then any slot in the list, and not set in step 1
-     ;;              is set from its initform.
-     ;;       ()
-     ;;              then no slots are set from initforms.
-     (flet ((initialize-slot-from-initarg (class instance slotd)
-              (let ((slot-initargs (slot-definition-initargs slotd)))
-                (doplist (initarg value) initargs
-                         (when (memq initarg slot-initargs)
-                           (setf (slot-value-using-class class instance slotd)
-                                 value)
-                           (return t)))))
-            (initialize-slot-from-initfunction (class instance slotd)
-              (unless (or (slot-boundp-using-class class instance slotd)
-                          (null (slot-definition-initfunction slotd)))
-                (setf (slot-value-using-class class instance slotd)
-                      (funcall (slot-definition-initfunction slotd)))))
-            (class-slot-p (slotd)
-              (eq :class (slot-definition-allocation slotd))))
-       (loop with class = (class-of instance)
-             for slotd in (class-slots class)
-             unless (or (class-slot-p slotd)
-                        (initialize-slot-from-initarg class instance slotd))
-             when (memq (slot-definition-name slotd) slot-names) do
-             (initialize-slot-from-initfunction class instance slotd))
-       instance))))
+(defmethod shared-initialize ((instance slot-object) slot-names &rest initargs)
+  (flet ((initialize-slot-from-initarg (class instance slotd)
+           (let ((slot-initargs (slot-definition-initargs slotd)))
+             (doplist (initarg value) initargs
+                      (when (memq initarg slot-initargs)
+                        (setf (slot-value-using-class class instance slotd)
+                              value)
+                        (return t)))))
+         (initialize-slot-from-initfunction (class instance slotd)
+           ;; CLHS: If a before method stores something in a slot,
+           ;; that slot won't be initialized from its :INITFORM, if any.
+          (if (typep instance 'structure-object)
+              (when (eq (funcall
+                         ;; not SLOT-VALUE-USING-CLASS, as that
+                         ;; throws an error if the value is the
+                         ;; unbound marker.
+                         (slot-definition-internal-reader-function slotd)
+                         instance)
+                        +slot-unbound+)
+                (setf (slot-value-using-class class instance slotd)
+                      (let ((initfn (slot-definition-initfunction slotd)))
+                        (when initfn
+                          (funcall initfn)))))
+              (unless (or (null (slot-definition-initfunction slotd))
+                          (slot-boundp-using-class class instance slotd))
+                (setf (slot-value-using-class class instance slotd)
+                      (funcall (slot-definition-initfunction slotd)))))))
+    (let* ((class (class-of instance))
+           (initfn-slotds
+            (loop for slotd in (class-slots class)
+                  unless (initialize-slot-from-initarg class instance slotd)
+                    collect slotd)))
+      (dolist (slotd initfn-slotds)
+       (if (eq (slot-definition-allocation slotd) :class)
+           (when (or (eq t slot-names)
+                     (memq (slot-definition-name slotd) slot-names))
+             (unless (slot-boundp-using-class class instance slotd)
+               (initialize-slot-from-initfunction class instance slotd)))
+           (when (or (eq t slot-names)
+                     (memq (slot-definition-name slotd) slot-names))
+             (initialize-slot-from-initfunction class instance slotd)))))
+    instance))
 \f
 ;;; If initargs are valid return nil, otherwise signal an error.
 (defun check-initargs-1 (class initargs call-list
        (setq legal (append keys legal))))
     (values legal nil)))
 
+(define-condition initarg-error (reference-condition program-error)
+  ((class :reader initarg-error-class :initarg :class)
+   (initargs :reader initarg-error-initargs :initarg :initargs))
+  (:default-initargs :references (list '(:ansi-cl :section (7 1 2))))
+  (:report (lambda (condition stream)
+            (format stream "~@<Invalid initialization argument~P: ~2I~_~
+                             ~<~{~S~^, ~} ~@:>~I~_in call for class ~S.~:>"
+                    (length (initarg-error-initargs condition))
+                    (list (initarg-error-initargs condition))
+                    (initarg-error-class condition)))))
+
 (defun check-initargs-2-plist (initargs class legal &optional (error-p t))
-  (unless (getf initargs :allow-other-keys)
-    ;; Now check the supplied-initarg-names and the default initargs
-    ;; against the total set that we know are legal.
-    (doplist (key val) initargs
-      (unless (memq key legal)
-       (if error-p
-           (error 'simple-program-error
-                  :format-control "Invalid initialization argument ~S for class ~S"
-                  :format-arguments (list key (class-name class)))
-           (return-from check-initargs-2-plist nil)))))
-  t)
+  (let ((invalid-keys ()))
+    (unless (getf initargs :allow-other-keys)
+      ;; Now check the supplied-initarg-names and the default initargs
+      ;; against the total set that we know are legal.
+      (doplist (key val) initargs
+       (unless (or (memq key legal)
+                   ;; :ALLOW-OTHER-KEYS NIL gets here
+                   (eq key :allow-other-keys))
+         (push key invalid-keys)))
+      (when (and invalid-keys error-p)
+       (error 'initarg-error :class class :initargs invalid-keys)))
+    invalid-keys))
 
 (defun check-initargs-2-list (initkeys class legal &optional (error-p t))
-  (unless (memq :allow-other-keys initkeys)
-    ;; Now check the supplied-initarg-names and the default initargs
-    ;; against the total set that we know are legal.
-    (dolist (key initkeys)
-      (unless (memq key legal)
-       (if error-p
-           (error 'simple-program-error
-                  :format-control "Invalid initialization argument ~S for class ~S"
-                  :format-arguments (list key (class-name class)))
-           (return-from check-initargs-2-list nil)))))
-  t)
+  (let ((invalid-keys ()))
+    (unless (memq :allow-other-keys initkeys)
+      ;; Now check the supplied-initarg-names and the default initargs
+      ;; against the total set that we know are legal.
+      (dolist (key initkeys)
+       (unless (memq key legal)
+         (push key invalid-keys)))
+      (when (and invalid-keys error-p)
+       (error 'initarg-error :class class :initargs invalid-keys)))
+    invalid-keys))