1.0.20: release, will be tagged sbcl_1_0_20
[sbcl/eslaughter.git] / src / pcl / init.lisp
blob4306079af2c6b237abcff22cb560934af04bd33b
1 ;;;; This file defines the initialization and related protocols.
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; information.
12 ;;;; copyright information from original PCL sources:
13 ;;;;
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
16 ;;;;
17 ;;;; Use and copying of this software and preparation of derivative works based
18 ;;;; upon this software are permitted. Any distribution of this software or
19 ;;;; derivative works must comply with all applicable United States export
20 ;;;; control laws.
21 ;;;;
22 ;;;; This software is made available AS IS, and Xerox Corporation makes no
23 ;;;; warranty about the software, its performance or its conformity to any
24 ;;;; specification.
26 (in-package "SB-PCL")
28 (defmethod make-instance ((class symbol) &rest initargs)
29 (apply #'make-instance (find-class class) initargs))
31 (defmethod make-instance ((class class) &rest initargs)
32 (unless (class-finalized-p class) (finalize-inheritance class))
33 (let ((class-default-initargs (class-default-initargs class)))
34 (when class-default-initargs
35 (setf initargs (default-initargs class initargs class-default-initargs)))
36 (when initargs
37 (when (and (eq *boot-state* 'complete)
38 (not (getf initargs :allow-other-keys)))
39 (let ((class-proto (class-prototype class)))
40 (check-initargs-1
41 class initargs
42 (append (compute-applicable-methods
43 #'allocate-instance (list class))
44 (compute-applicable-methods
45 #'initialize-instance (list class-proto))
46 (compute-applicable-methods
47 #'shared-initialize (list class-proto t)))))))
48 (let ((instance (apply #'allocate-instance class initargs)))
49 (apply #'initialize-instance instance initargs)
50 instance)))
52 (defmethod default-initargs ((class slot-class)
53 supplied-initargs
54 class-default-initargs)
55 (loop for (key nil fun) in class-default-initargs
56 when (eq (getf supplied-initargs key '.not-there.) '.not-there.)
57 append (list key (funcall fun)) into default-initargs
58 finally
59 (return (append supplied-initargs default-initargs))))
61 (defmethod initialize-instance ((instance slot-object) &rest initargs)
62 (apply #'shared-initialize instance t initargs))
64 (defmethod reinitialize-instance ((instance slot-object) &rest initargs)
65 ;; the ctor machinery allows us to track when memoization of
66 ;; validity of initargs should be cleared.
67 (check-ri-initargs instance initargs)
68 (apply #'shared-initialize instance nil initargs)
69 instance)
71 (defmethod update-instance-for-different-class
72 ((previous standard-object) (current standard-object) &rest initargs)
73 ;; First we must compute the newly added slots. The spec defines
74 ;; newly added slots as "those local slots for which no slot of
75 ;; the same name exists in the previous class."
76 (let ((added-slots '())
77 (current-slotds (class-slots (class-of current)))
78 (previous-slot-names (mapcar #'slot-definition-name
79 (class-slots (class-of previous)))))
80 (dolist (slotd current-slotds)
81 (if (and (not (memq (slot-definition-name slotd) previous-slot-names))
82 (eq (slot-definition-allocation slotd) :instance))
83 (push (slot-definition-name slotd) added-slots)))
84 (check-initargs-1
85 (class-of current) initargs
86 (list (list* 'update-instance-for-different-class previous current initargs)
87 (list* 'shared-initialize current added-slots initargs)))
88 (apply #'shared-initialize current added-slots initargs)))
90 (defmethod update-instance-for-redefined-class
91 ((instance standard-object) added-slots discarded-slots property-list
92 &rest initargs)
93 (check-initargs-1
94 (class-of instance) initargs
95 (list (list* 'update-instance-for-redefined-class
96 instance added-slots discarded-slots property-list initargs)
97 (list* 'shared-initialize instance added-slots initargs)))
98 (apply #'shared-initialize instance added-slots initargs))
100 (defmethod shared-initialize ((instance slot-object) slot-names &rest initargs)
101 (flet ((initialize-slot-from-initarg (class instance slotd)
102 (let ((slot-initargs (slot-definition-initargs slotd)))
103 (doplist (initarg value) initargs
104 (when (memq initarg slot-initargs)
105 (setf (slot-value-using-class class instance slotd)
106 value)
107 (return t)))))
108 (initialize-slot-from-initfunction (class instance slotd)
109 ;; CLHS: If a before method stores something in a slot,
110 ;; that slot won't be initialized from its :INITFORM, if any.
111 (if (typep instance 'structure-object)
112 (when (eq (funcall
113 ;; not SLOT-VALUE-USING-CLASS, as that
114 ;; throws an error if the value is the
115 ;; unbound marker.
116 (slot-definition-internal-reader-function slotd)
117 instance)
118 +slot-unbound+)
119 (setf (slot-value-using-class class instance slotd)
120 (let ((initfn (slot-definition-initfunction slotd)))
121 (when initfn
122 (funcall initfn)))))
123 (unless (or (null (slot-definition-initfunction slotd))
124 (slot-boundp-using-class class instance slotd))
125 (setf (slot-value-using-class class instance slotd)
126 (funcall (slot-definition-initfunction slotd)))))))
127 (let* ((class (class-of instance))
128 (initfn-slotds
129 (loop for slotd in (class-slots class)
130 unless (initialize-slot-from-initarg class instance slotd)
131 collect slotd)))
132 (dolist (slotd initfn-slotds)
133 (if (eq (slot-definition-allocation slotd) :class)
134 (when (or (eq t slot-names)
135 (memq (slot-definition-name slotd) slot-names))
136 (unless (slot-boundp-using-class class instance slotd)
137 (initialize-slot-from-initfunction class instance slotd)))
138 (when (or (eq t slot-names)
139 (memq (slot-definition-name slotd) slot-names))
140 (initialize-slot-from-initfunction class instance slotd)))))
141 instance))
143 ;;; If initargs are valid return nil, otherwise signal an error.
144 (defun check-initargs-1 (class initargs call-list
145 &optional (plist-p t) (error-p t))
146 (multiple-value-bind (legal allow-other-keys)
147 (check-initargs-values class call-list)
148 (unless allow-other-keys
149 (if plist-p
150 (check-initargs-2-plist initargs class legal error-p)
151 (check-initargs-2-list initargs class legal error-p)))))
153 (defun check-initargs-values (class call-list)
154 (let ((methods (mapcan (lambda (call)
155 (if (consp call)
156 (copy-list (compute-applicable-methods
157 (gdefinition (car call))
158 (cdr call)))
159 (list call)))
160 call-list))
161 (legal (apply #'append (mapcar #'slot-definition-initargs
162 (class-slots class)))))
163 ;; Add to the set of slot-filling initargs the set of
164 ;; initargs that are accepted by the methods. If at
165 ;; any point we come across &allow-other-keys, we can
166 ;; just quit.
167 (dolist (method methods)
168 (multiple-value-bind (nreq nopt keysp restp allow-other-keys keys)
169 (analyze-lambda-list (if (consp method)
170 (early-method-lambda-list method)
171 (method-lambda-list method)))
172 (declare (ignore nreq nopt keysp restp))
173 (when allow-other-keys
174 (return-from check-initargs-values (values nil t)))
175 (setq legal (append keys legal))))
176 (values legal nil)))
178 (define-condition initarg-error (reference-condition program-error)
179 ((class :reader initarg-error-class :initarg :class)
180 (initargs :reader initarg-error-initargs :initarg :initargs))
181 (:default-initargs :references (list '(:ansi-cl :section (7 1 2))))
182 (:report (lambda (condition stream)
183 (format stream "~@<Invalid initialization argument~P: ~2I~_~
184 ~<~{~S~^, ~} ~@:>~I~_in call for class ~S.~:>"
185 (length (initarg-error-initargs condition))
186 (list (initarg-error-initargs condition))
187 (initarg-error-class condition)))))
189 (defun check-initargs-2-plist (initargs class legal &optional (error-p t))
190 (let ((invalid-keys ()))
191 (unless (getf initargs :allow-other-keys)
192 ;; Now check the supplied-initarg-names and the default initargs
193 ;; against the total set that we know are legal.
194 (doplist (key val) initargs
195 (unless (or (memq key legal)
196 ;; :ALLOW-OTHER-KEYS NIL gets here
197 (eq key :allow-other-keys))
198 (push key invalid-keys)))
199 (when (and invalid-keys error-p)
200 (error 'initarg-error :class class :initargs invalid-keys)))
201 invalid-keys))
203 (defun check-initargs-2-list (initkeys class legal &optional (error-p t))
204 (let ((invalid-keys ()))
205 (unless (memq :allow-other-keys initkeys)
206 ;; Now check the supplied-initarg-names and the default initargs
207 ;; against the total set that we know are legal.
208 (dolist (key initkeys)
209 (unless (memq key legal)
210 (push key invalid-keys)))
211 (when (and invalid-keys error-p)
212 (error 'initarg-error :class class :initargs invalid-keys)))
213 invalid-keys))