Clean up nested IFs.
[sbcl.git] / src / pcl / std-class.lisp
blobdc42715c62034860403f1b03d32fb723b3c967cf
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
24 (in-package "SB-PCL")
26 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
27 (let ((info (the slot-info (slot-definition-info slotd))))
28 (ecase type
29 (reader (slot-info-reader info))
30 (writer (slot-info-writer info))
31 (boundp (slot-info-boundp info)))))
33 (defmethod (setf slot-accessor-function) (function
34 (slotd effective-slot-definition)
35 type)
36 (let ((info (the slot-info (slot-definition-info slotd))))
37 (ecase type
38 (reader (setf (slot-info-reader info) function))
39 (writer (setf (slot-info-writer info) function))
40 (boundp (setf (slot-info-boundp info) function)))))
42 (defconstant +slotd-reader-function-std-p+ 1)
43 (defconstant +slotd-writer-function-std-p+ 2)
44 (defconstant +slotd-boundp-function-std-p+ 4)
45 (defconstant +slotd-all-function-std-p+ 7)
47 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
48 (let ((flags (slot-value slotd 'accessor-flags)))
49 (declare (type fixnum flags))
50 (if (eq type 'all)
51 (eql +slotd-all-function-std-p+ flags)
52 (logtest flags (ecase type
53 (reader +slotd-reader-function-std-p+)
54 (writer +slotd-writer-function-std-p+)
55 (boundp +slotd-boundp-function-std-p+))))))
57 (defmethod (setf slot-accessor-std-p) (value
58 (slotd effective-slot-definition)
59 type)
60 (let ((mask (ecase type
61 (reader +slotd-reader-function-std-p+)
62 (writer +slotd-writer-function-std-p+)
63 (boundp +slotd-boundp-function-std-p+)))
64 (flags (slot-value slotd 'accessor-flags)))
65 (declare (type fixnum mask flags))
66 (setf (slot-value slotd 'accessor-flags)
67 (logior (logandc2 flags mask) (if value mask 0))))
68 value)
70 (defmethod initialize-internal-slot-functions
71 ((slotd effective-slot-definition))
72 (let* ((name (slot-value slotd 'name)) ; flushable? (is it ever unbound?)
73 (class (slot-value slotd '%class)))
74 (declare (ignore name))
75 (dolist (type '(reader writer boundp))
76 (let* ((gf-name (ecase type
77 (reader 'slot-value-using-class)
78 (writer '(setf slot-value-using-class))
79 (boundp 'slot-boundp-using-class)))
80 (gf (gdefinition gf-name)))
81 ;; KLUDGE: this logic is cut'n'pasted from
82 ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
83 ;; only called later, because it does things that can't be
84 ;; computed this early in class finalization; however, we need
85 ;; this bit as early as possible. -- CSR, 2009-11-05
86 (setf (slot-accessor-std-p slotd type)
87 (let* ((types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
88 (types (if (eq type 'writer) `(t ,@types1) types1))
89 (methods (compute-applicable-methods-using-types gf types)))
90 (null (cdr methods))))
91 (setf (slot-accessor-function slotd type)
92 (lambda (&rest args)
93 (declare (dynamic-extent args))
94 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
95 ;; work here (see KLUDGE comment above).
96 (let ((fun (compute-slot-accessor-info slotd type gf)))
97 (apply fun args))))))))
99 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition))
100 (dolist (type '(reader writer boundp))
101 (let* ((gf-name (ecase type
102 (reader 'slot-value-using-class)
103 (writer '(setf slot-value-using-class))
104 (boundp 'slot-boundp-using-class)))
105 (gf (gdefinition gf-name)))
106 (compute-slot-accessor-info slotd type gf))))
108 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
110 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
111 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
112 ;;; writing/testing effective slot SLOTD.
114 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
115 ;;; GF. Store the effective method in the effective slot definition
116 ;;; object itself; these GFs have special dispatch functions calling
117 ;;; effective methods directly retrieved from effective slot
118 ;;; definition objects, as an optimization.
120 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
121 ;;; or some such.
122 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
123 type gf)
124 (let* ((name (slot-value slotd 'name)) ; flushable?
125 (class (slot-value slotd '%class)))
126 (declare (ignore name))
127 (multiple-value-bind (function std-p)
128 (if (eq **boot-state** 'complete)
129 (get-accessor-method-function gf type class slotd)
130 (get-optimized-std-accessor-method-function class slotd type))
131 (setf (slot-accessor-std-p slotd type) std-p)
132 (setf (slot-accessor-function slotd type) function))))
134 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
135 :instance)
137 ;;;; various class accessors that are a little more complicated than can be
138 ;;;; done with automatically generated reader methods
140 (defmethod class-prototype :before (class)
141 (unless (class-finalized-p class)
142 (error "~@<~S is not finalized.~:@>" class)))
144 ;;; KLUDGE: For some reason factoring the common body into a function
145 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
146 ;;; all.
147 (macrolet ((def (class)
148 `(defmethod class-prototype ((class ,class))
149 (with-slots (prototype) class
150 (or prototype
151 (setf prototype (allocate-instance class)))))))
152 (def std-class)
153 (def condition-class)
154 (def structure-class))
156 (defmethod class-direct-default-initargs ((class slot-class))
157 (plist-value class 'direct-default-initargs))
159 (defmethod class-default-initargs ((class slot-class))
160 (plist-value class 'default-initargs))
162 (defmethod class-slot-cells ((class std-class))
163 (plist-value class 'class-slot-cells))
164 (defmethod (setf class-slot-cells) (new-value (class std-class))
165 (setf (plist-value class 'class-slot-cells) new-value))
167 ;;;; class accessors that are even a little bit more complicated than those
168 ;;;; above. These have a protocol for updating them, we must implement that
169 ;;;; protocol.
171 ;;; Maintaining the direct subclasses backpointers. The update methods are
172 ;;; here, the values are read by an automatically generated reader method.
173 (defmethod add-direct-subclass ((class class) (subclass class))
174 (with-slots (direct-subclasses) class
175 (pushnew subclass direct-subclasses :test #'eq)
176 subclass))
177 (defmethod remove-direct-subclass ((class class) (subclass class))
178 (with-slots (direct-subclasses) class
179 (setq direct-subclasses (remove subclass direct-subclasses))
180 subclass))
182 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
184 ;;; There are four generic functions involved, each has one method. All of
185 ;;; these are specified methods and appear in their specified place in the
186 ;;; class graph.
188 ;;; ADD-DIRECT-METHOD
189 ;;; REMOVE-DIRECT-METHOD
190 ;;; SPECIALIZER-DIRECT-METHODS
191 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
193 ;;; In each case, we maintain one value which is a cons. The car is the list
194 ;;; of methods. The cdr is a list of the generic functions. The cdr is always
195 ;;; computed lazily.
197 ;;; This needs to be used recursively, in case a non-trivial user
198 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
199 ;;; function using the same lock.
200 (defvar *specializer-lock* (sb-thread:make-mutex :name "Specializer lock"))
202 (defmethod add-direct-method :around ((specializer specializer) method)
203 ;; All the actions done under this lock are done in an order
204 ;; that is safe to unwind at any point.
205 (sb-thread::with-recursive-system-lock (*specializer-lock*)
206 (call-next-method)))
208 (defmethod remove-direct-method :around ((specializer specializer) method)
209 ;; All the actions done under this lock are done in an order
210 ;; that is safe to unwind at any point.
211 (sb-thread::with-recursive-system-lock (*specializer-lock*)
212 (call-next-method)))
214 (defmethod add-direct-method ((specializer specializer) (method method))
215 (let ((cell (specializer-method-holder specializer)))
216 ;; We need to first smash the CDR, because a parallel read may
217 ;; be in progress, and because if an interrupt catches us we
218 ;; need to have a consistent state.
219 (setf (cdr cell) ()
220 (car cell) (adjoin method (car cell) :test #'eq)))
221 method)
223 (defmethod remove-direct-method ((specializer specializer) (method method))
224 (let ((cell (specializer-method-holder specializer)))
225 ;; We need to first smash the CDR, because a parallel read may
226 ;; be in progress, and because if an interrupt catches us we
227 ;; need to have a consistent state.
228 (setf (cdr cell) ()
229 (car cell) (remove method (car cell))))
230 method)
232 (defmethod specializer-direct-methods ((specializer specializer))
233 (car (specializer-method-holder specializer nil)))
235 (defmethod specializer-direct-generic-functions ((specializer specializer))
236 (let ((cell (specializer-method-holder specializer nil)))
237 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
238 ;; we behave as if we got just first or just after -- it's just
239 ;; for update that we need to lock.
240 (or (cdr cell)
241 (when (car cell)
242 (setf (cdr cell)
243 (sb-thread:with-mutex (*specializer-lock*)
244 (let (collect)
245 (dolist (m (car cell) (nreverse collect))
246 ;; the old PCL code used COLLECTING-ONCE which used
247 ;; #'EQ to check for newness
248 (pushnew (method-generic-function m) collect
249 :test #'eq)))))))))
251 (defmethod specializer-method-holder ((self specializer) &optional create)
252 ;; CREATE can be ignored, because instances of SPECIALIZER
253 ;; other than CLASS-EQ-SPECIALIZER have their DIRECT-METHODS slot
254 ;; preinitialized with (NIL . NIL).
255 (declare (ignore create))
256 (slot-value self 'direct-methods))
258 ;; Same as for CLASS but the only ancestor it shares is SPECIALIZER.
259 ;; If this were defined on SPECIALIZER-WITH-OBJECT then it would
260 ;; apply as well to CLASS-EQ specializers which we don't want.
261 (defmethod specializer-method-holder ((self eql-specializer) &optional create)
262 (declare (ignore create))
263 (slot-value self 'direct-methods))
265 ;;; This hash table is used to store the direct methods and direct generic
266 ;;; functions of CLASS-EQ specializers.
267 ;;; This is needed because CLASS-EQ specializers are not interned: two different
268 ;;; specializers could refer to the identical CLASS. But semantically you don't
269 ;;; want to collect the direct methods separately, you want them all together.
270 ;;; So a logical place to do that would be in the CLASS. We could add another
271 ;;; slot to CLASS holding the CLASS-EQ-DIRECT-METHODS. I guess the idea though
272 ;;; is to be able to define other kinds of specializers that hold an object
273 ;;; where you don't have the ability to add a slot to that object.
274 ;;; So that's fine, you look up the object in this table, and the approach
275 ;;; generalizes by defining other tables similarly, but has the same problem
276 ;;; of garbage retention as did EQL specializers.
277 ;;; I suspect CLASS-EQ specializers aren't used enough to worry about.
279 ;;; This table is shared between threads, so needs to be synchronized.
280 ;;; (though insertions are only performed when holding the specializer-lock,
281 ;;; so the preceding claim is probably overly paranoid.)
282 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq :synchronized t))
284 (defmethod specializer-method-table ((specializer class-eq-specializer))
285 *class-eq-specializer-methods*)
287 (defmethod specializer-method-holder ((self specializer-with-object)
288 &optional (create t))
289 (let ((table (specializer-method-table self))
290 (object (specializer-object self)))
291 (if create
292 (sb-impl::puthash-if-absent object table (lambda () (cons nil nil)))
293 (gethash object table))))
295 (defun map-specializers (function)
296 (map-all-classes (lambda (class)
297 (funcall function (class-eq-specializer class))
298 (funcall function class)))
299 (maphash (lambda (object specl)
300 (declare (ignore object))
301 (funcall function specl))
302 *eql-specializer-table*)
303 nil)
305 (defun map-all-generic-functions (function)
306 (let ((all-generic-functions (make-hash-table :test 'eq)))
307 (map-specializers (lambda (specl)
308 (dolist (gf (specializer-direct-generic-functions
309 specl))
310 (unless (gethash gf all-generic-functions)
311 (setf (gethash gf all-generic-functions) t)
312 (funcall function gf))))))
313 nil)
315 (defmethod shared-initialize :after ((specl class-eq-specializer)
316 slot-names
317 &key)
318 (declare (ignore slot-names))
319 (setf (slot-value specl '%type) `(class-eq ,(specializer-class specl))))
321 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
322 (declare (ignore slot-names))
323 (setf (slot-value specl '%type)
324 `(eql ,(specializer-object specl))))
326 (defun real-load-defclass (name metaclass-name supers slots other
327 readers writers slot-names source-location &optional safe-p)
328 (with-single-package-locked-error (:symbol name "defining ~S as a class")
329 (%compiler-defclass name readers writers slot-names)
330 (let ((res (apply #'ensure-class name :metaclass metaclass-name
331 :direct-superclasses supers
332 :direct-slots slots
333 :definition-source source-location
334 'safe-p safe-p
335 other)))
336 res)))
338 (setf (gdefinition 'load-defclass) #'real-load-defclass)
340 (defun ensure-class (name &rest args)
341 (with-world-lock ()
342 (apply #'ensure-class-using-class
343 (let ((class (find-class name nil)))
344 (when (and class (eq name (class-name class)))
345 ;; NAME is the proper name of CLASS, so redefine it
346 class))
347 name args)))
349 (defun parse-ensure-class-args (class name args)
350 (let ((metaclass *the-class-standard-class*)
351 (metaclassp nil)
352 (reversed-plist '()))
353 (labels ((find-class* (which class-or-name)
354 (cond
355 ((classp class-or-name)
356 (cond
357 ((eq class-or-name class)
358 (error "~@<Class ~A specified as its own ~
359 ~(~A~)class.~@:>"
360 class-or-name which))
362 class-or-name)))
363 ((and class-or-name (legal-class-name-p class-or-name))
364 (cond
365 ((eq class-or-name name)
366 (error "~@<Class named ~
367 ~/sb-impl::print-symbol-with-prefix/ ~
368 specified as its own ~(~A~)class.~@:>"
369 class-or-name which))
370 ((find-class class-or-name (eq which :meta)))
371 ((ensure-class
372 class-or-name :metaclass 'forward-referenced-class))))
374 (error "~@<Not a class or a legal ~(~A~)class name: ~
375 ~S.~@:>"
376 which class-or-name))))
377 (find-superclass (class-or-name)
378 (find-class* :super class-or-name)))
379 (doplist (key value) args
380 (case key
381 (:metaclass
382 (unless metaclassp
383 (setf metaclass (find-class* :meta value)
384 metaclassp key)))
385 (:direct-superclasses
386 (let ((superclasses (mapcar #'find-superclass value)))
387 (setf reversed-plist (list* superclasses key reversed-plist))))
389 (setf reversed-plist (list* value key reversed-plist)))))
390 (values metaclass (nreverse reversed-plist)))))
392 (defun call-with-ensure-class-context (class name args thunk)
393 (let ((class (with-world-lock ()
394 (multiple-value-bind (metaclass initargs)
395 (parse-ensure-class-args class name args)
396 (let ((class (funcall thunk class name metaclass initargs)))
397 (without-package-locks
398 (setf (find-class name) class)))))))
399 class))
401 (defmethod ensure-class-using-class ((class null) name &rest args &key)
402 (call-with-ensure-class-context
403 class name args (lambda (class name metaclass initargs)
404 (declare (ignore class))
405 (apply #'make-instance metaclass :name name initargs))))
407 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
408 (call-with-ensure-class-context
409 class name args (lambda (class name metaclass initargs)
410 (aver (eq name (class-name class)))
411 (unless (eq (class-of class) metaclass)
412 (apply #'change-class class metaclass initargs))
413 (apply #'reinitialize-instance class initargs)
414 class)))
416 ;;; This is used to call initfunctions of :allocation :class slots.
417 (defun call-initfun (fun slotd safe)
418 (declare (function fun))
419 (let ((value (funcall fun)))
420 (when safe
421 (let ((type (slot-definition-type slotd)))
422 (unless (or (eq t type)
423 (typep value type))
424 (error 'type-error :expected-type type :datum value))))
425 value))
427 (defmethod shared-initialize :after
428 ((class std-class) slot-names &key
429 (direct-superclasses nil direct-superclasses-p)
430 (direct-slots nil direct-slots-p)
431 (direct-default-initargs nil direct-default-initargs-p))
432 (cond (direct-superclasses-p
433 (setq direct-superclasses
434 (or direct-superclasses
435 (list (if (funcallable-standard-class-p class)
436 *the-class-funcallable-standard-object*
437 *the-class-standard-object*))))
438 (dolist (superclass direct-superclasses)
439 (unless (validate-superclass class superclass)
440 (invalid-superclass class superclass)))
441 (setf (slot-value class 'direct-superclasses) direct-superclasses))
443 (setq direct-superclasses (slot-value class 'direct-superclasses))))
444 (setq direct-slots
445 (if direct-slots-p
446 (setf (slot-value class 'direct-slots)
447 (mapcar (lambda (pl) (make-direct-slotd class pl))
448 direct-slots))
449 (slot-value class 'direct-slots)))
450 (if direct-default-initargs-p
451 (setf (plist-value class 'direct-default-initargs)
452 direct-default-initargs)
453 (setq direct-default-initargs
454 (plist-value class 'direct-default-initargs)))
455 (setf (plist-value class 'class-slot-cells)
456 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
457 (safe (safe-p class))
458 (collect '()))
459 (dolist (dslotd direct-slots)
460 (when (eq :class (slot-definition-allocation dslotd))
461 ;; see CLHS 4.3.6
462 (let* ((name (slot-definition-name dslotd))
463 (old (assoc name old-class-slot-cells)))
464 (if (or (not old)
465 (eq t slot-names)
466 (member name slot-names :test #'eq))
467 (let* ((initfunction (slot-definition-initfunction dslotd))
468 (value
469 (if initfunction
470 (call-initfun initfunction dslotd safe)
471 +slot-unbound+)))
472 (push (cons name value) collect))
473 (push old collect)))))
474 (nreverse collect)))
475 (add-direct-subclasses class direct-superclasses)
476 (if (class-finalized-p class)
477 ;; required by AMOP, "Reinitialization of Class Metaobjects"
478 (finalize-inheritance class)
479 (update-class class nil))
480 (add-slot-accessors class direct-slots)
481 (make-preliminary-layout class))
483 (define-condition invalid-superclass (reference-condition error)
484 ((class :initarg :class :reader invalid-superclass-class)
485 (superclass :initarg :superclass :reader invalid-superclass-superclass))
486 (:report
487 (lambda (c s)
488 (let ((class (invalid-superclass-class c))
489 (superclass (invalid-superclass-superclass c)))
490 (format s
491 "~@<The class ~S was specified as a superclass of the ~
492 class ~S, but the metaclasses ~S and ~S are ~
493 incompatible.~@[ Define a method for ~S to avoid this ~
494 error.~]~@:>"
495 superclass class (class-of superclass) (class-of class)
496 (and (typep superclass 'standard-class)
497 'validate-superclass))))))
499 (defmethod invalid-superclass ((class class) (superclass class))
500 (error 'invalid-superclass :class class :superclass superclass
501 :references (list* '(:amop :generic-function validate-superclass)
502 (and (typep superclass 'built-in-class)
503 (list '(:ansi-cl :system-class built-in-class)
504 '(:ansi-cl :section (4 3 7)))))))
506 (defmethod shared-initialize :after ((class forward-referenced-class)
507 slot-names &key &allow-other-keys)
508 (declare (ignore slot-names))
509 (make-preliminary-layout class))
511 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
513 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
514 ;;; make it known to the type system.
515 (defun make-preliminary-layout (class)
516 (flet ((compute-preliminary-cpl (root)
517 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
518 (compute-class-precedence-list root))))
519 (with-world-lock ()
520 (without-package-locks
521 (unless (class-finalized-p class)
522 (let ((name (class-name class))) ; flushable?
523 (declare (ignore name))
524 ;; KLUDGE: This is fairly horrible. We need to make a
525 ;; full-fledged CLASSOID here, not just tell the compiler that
526 ;; some class is forthcoming, because there are legitimate
527 ;; questions one can ask of the type system, implemented in
528 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
529 (let ((layout (make-wrapper 0 class)))
530 (setf (slot-value class 'wrapper) layout)
531 (let ((cpl (compute-preliminary-cpl class)))
532 (setf (layout-inherits layout)
533 (order-layout-inherits
534 (map 'simple-vector #'class-wrapper
535 (reverse (rest cpl))))))
536 (register-layout layout :invalidate t))))
537 (mapc #'make-preliminary-layout (class-direct-subclasses class))))))
540 (defmethod shared-initialize :before ((class class) slot-names &key name)
541 (declare (ignore slot-names name))
542 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
543 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
544 (setf (slot-value class '%type) `(class ,class))
545 (setf (slot-value class 'class-eq-specializer)
546 (make-instance 'class-eq-specializer :class class)))
548 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
549 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
550 (remove-direct-subclass old-super class))
551 (remove-slot-accessors class (class-direct-slots class)))
553 (defmethod reinitialize-instance :after ((class slot-class)
554 &rest initargs
555 &key)
556 (map-dependents class
557 (lambda (dependent)
558 (apply #'update-dependent class dependent initargs))))
560 (defmethod reinitialize-instance :after ((class condition-class) &key)
561 (let* ((name (class-name class))
562 (classoid (find-classoid name))
563 (slots (condition-classoid-slots classoid)))
564 ;; to balance the REMOVE-SLOT-ACCESSORS call in
565 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
566 (dolist (slot slots)
567 (let ((slot-name (condition-slot-name slot)))
568 (dolist (reader (condition-slot-readers slot))
569 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
570 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
571 (sb-kernel::install-condition-slot-reader reader name slot-name))
572 (dolist (writer (condition-slot-writers slot))
573 (sb-kernel::install-condition-slot-writer writer name slot-name))))))
575 (defmethod shared-initialize :after ((class condition-class) slot-names
576 &key direct-slots direct-superclasses)
577 (declare (ignore slot-names))
578 (let ((classoid (find-classoid (slot-value class 'name))))
579 (with-slots (wrapper
580 %class-precedence-list cpl-available-p finalized-p
581 prototype (direct-supers direct-superclasses)
582 plist)
583 class
584 (setf (slot-value class 'direct-slots)
585 (mapcar (lambda (pl) (make-direct-slotd class pl))
586 direct-slots)
587 finalized-p t
588 (classoid-pcl-class classoid) class
589 direct-supers direct-superclasses
590 wrapper (classoid-layout classoid)
591 %class-precedence-list (compute-class-precedence-list class)
592 cpl-available-p t
593 (getf plist 'direct-default-initargs)
594 (sb-kernel::condition-classoid-direct-default-initargs classoid))
595 (add-direct-subclasses class direct-superclasses)
596 (let ((slots (compute-slots class)))
597 (setf (slot-value class 'slots) slots)
598 (setf (layout-slot-table wrapper) (make-slot-table class slots)))))
599 ;; Comment from Gerd's PCL, 2003-05-15:
601 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
602 ;; override condition accessors with generic functions. We do this
603 ;; differently.
605 ;; ??? What does the above comment mean and why is it a good idea?
606 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
607 ;; comment) loses slot information in its condition classes:
608 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
609 ;; remove slot accessors but never put them back. I've added a
610 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
611 ;; was meant to happen? -- CSR, 2005-11-18
614 (defmethod direct-slot-definition-class ((class condition-class)
615 &rest initargs)
616 (declare (ignore initargs))
617 (find-class 'condition-direct-slot-definition))
619 (defmethod effective-slot-definition-class ((class condition-class)
620 &rest initargs)
621 (declare (ignore initargs))
622 (find-class 'condition-effective-slot-definition))
624 (defmethod finalize-inheritance ((class condition-class))
625 (aver (slot-value class 'finalized-p))
626 nil)
628 (defmethod compute-effective-slot-definition
629 ((class condition-class) slot-name dslotds)
630 (let* ((slotd (call-next-method))
631 (info (slot-definition-info slotd)))
632 (setf (slot-info-reader info)
633 (lambda (x)
634 (handler-case (condition-reader-function x slot-name)
635 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
636 ;; is unbound; maybe it should be a CELL-ERROR of some
637 ;; sort?
638 (error () (values (slot-unbound class x slot-name))))))
639 (setf (slot-info-writer info)
640 (lambda (v x)
641 (condition-writer-function x v slot-name)))
642 (setf (slot-info-boundp info)
643 (lambda (x)
644 (multiple-value-bind (v c)
645 (ignore-errors (condition-reader-function x slot-name))
646 (declare (ignore v))
647 (null c))))
648 slotd))
650 (defmethod compute-slots :around ((class condition-class))
651 (let ((eslotds (call-next-method)))
652 (mapc #'finalize-internal-slot-functions eslotds)
653 eslotds))
655 (defmethod shared-initialize :after
656 ((slotd structure-slot-definition) slot-names &key
657 (allocation :instance) allocation-class)
658 (declare (ignore slot-names allocation-class))
659 (unless (eq allocation :instance)
660 (error "Structure slots must have :INSTANCE allocation.")))
662 (defun make-structure-class-defstruct-form (name direct-slots include)
663 (let* ((conc-name (format-symbol *package* "~S structure class " name))
664 (constructor (format-symbol *package* "~Aconstructor" conc-name))
665 (included-name (class-name include))
666 (included-slots
667 (when include
668 (mapcar #'dsd-name (dd-slots (find-defstruct-description included-name)))))
669 (old-slots nil)
670 (new-slots nil)
671 (reader-names nil)
672 (writer-names nil))
673 (dolist (slotd (reverse direct-slots))
674 (let* ((slot-name (slot-definition-name slotd))
675 (initform (slot-definition-initform slotd))
676 (type (slot-definition-type slotd))
677 (desc `(,slot-name ,initform :type ,type)))
678 (push `(slot-accessor ,name ,slot-name reader)
679 reader-names)
680 (push `(slot-accessor ,name ,slot-name writer)
681 writer-names)
682 (if (member slot-name included-slots :test #'eq)
683 (push desc old-slots)
684 (push desc new-slots))))
685 (let* ((defstruct `(defstruct (,name
686 ,@(when include
687 `((:include ,included-name
688 ,@old-slots)))
689 (:constructor ,constructor ())
690 (:predicate nil)
691 (:conc-name ,conc-name)
692 (:copier nil))
693 ,@new-slots))
694 (readers-init
695 (mapcar (lambda (slotd reader-name)
696 (let ((accessor
697 (slot-definition-defstruct-accessor-symbol
698 slotd)))
699 `(defun ,reader-name (obj)
700 (declare (type ,name obj))
701 (,accessor obj))))
702 direct-slots reader-names))
703 (writers-init
704 (mapcar (lambda (slotd writer-name)
705 (let ((accessor
706 (slot-definition-defstruct-accessor-symbol
707 slotd)))
708 `(defun ,writer-name (nv obj)
709 (declare (type ,name obj))
710 (setf (,accessor obj) nv))))
711 direct-slots writer-names))
712 (defstruct-form
713 `(progn
714 ,defstruct
715 ,@readers-init ,@writers-init
716 (cons nil nil))))
717 (values defstruct-form constructor reader-names writer-names))))
719 ;;; Return a thunk to allocate an instance of CLASS named NAME.
720 ;;; This is broken with regard to the expectation that all semantic
721 ;;; processing is done by the compiler. e.g. after COMPILE-FILE on
722 ;;; a file containing these two toplevel forms:
723 ;;; (eval-when (:compile-toplevel) (defmacro maker () '(list 1)))
724 ;;; (defstruct foo (a (maker)))
725 ;;; then LOADing the resulting fasl in a fresh image will err:
726 ;;; (make-instance 'foo) => "The function MAKER is undefined."
728 ;;; The way to fix that is to ensure that every defstruct has a zero-argument
729 ;;; constructor made by the compiler and stashed in a random symbol.
730 (defun make-defstruct-allocation-function (name class)
731 ;; FIXME: Why don't we go class->layout->info == dd
732 (let ((dd (find-defstruct-description name)))
733 (ecase (dd-type dd)
734 (structure
735 ;; This used to call COMPILE directly, which is basically pointless,
736 ;; because it certainly does not avoid compiling code at runtime,
737 ;; which seems to have been the goal. We're also compiling:
738 ;; (LAMBDA () (SB-PCL::FAST-MAKE-INSTANCE #<STRUCTURE-CLASS THING>))
739 ;; So maybe we can figure out how to bundle two lambdas together?
740 (lambda ()
741 (let* ((dd (layout-info (class-wrapper class)))
742 (f (%make-structure-instance-allocator dd nil)))
743 (if (functionp f)
744 (funcall (setf (slot-value class 'defstruct-constructor) f))
745 (error "Can't allocate ~S" class)))))
746 (funcallable-structure
747 ;; FIXME: you can't dynamically define new funcallable structures
748 ;; that are not GENERIC-FUNCTION subtypes, so why this branch?
749 ;; We should pull this out, fixup PCL bootstrap, and not pretend
750 ;; that this code is more general than it really is.
751 (%make-funcallable-structure-instance-allocator dd nil)))))
753 (defmethod shared-initialize :after
754 ((class structure-class) slot-names &key
755 (direct-superclasses nil direct-superclasses-p)
756 (direct-slots nil direct-slots-p)
757 direct-default-initargs)
758 (declare (ignore slot-names direct-default-initargs))
759 (if direct-superclasses-p
760 (setf (slot-value class 'direct-superclasses)
761 (or direct-superclasses
762 (setq direct-superclasses
763 (and (not (eq (slot-value class 'name) 'structure-object))
764 (list *the-class-structure-object*)))))
765 (setq direct-superclasses (slot-value class 'direct-superclasses)))
766 (let* ((name (slot-value class 'name))
767 (from-defclass-p (slot-value class 'from-defclass-p))
768 ;; DEFSTRUCT-P means we should perform the effect of DEFSTRUCT,
769 ;; and not that this structure came from a DEFSTRUCT.
770 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
771 (if direct-slots-p
772 (setf (slot-value class 'direct-slots)
773 (setq direct-slots
774 (mapcar (lambda (pl)
775 (when defstruct-p
776 (let* ((slot-name (getf pl :name))
777 (accessor
778 (format-symbol *package*
779 "~S structure class ~A"
780 name slot-name)))
781 (setq pl (list* :defstruct-accessor-symbol
782 accessor pl))))
783 (make-direct-slotd class pl))
784 direct-slots)))
785 (setq direct-slots (slot-value class 'direct-slots)))
786 (if defstruct-p
787 (let ((include (car (slot-value class 'direct-superclasses))))
788 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
789 (make-structure-class-defstruct-form name direct-slots include)
790 (unless (structure-type-p name) (eval defstruct-form))
791 (mapc (lambda (dslotd reader-name writer-name)
792 (let* ((reader (gdefinition reader-name))
793 (writer (when (fboundp writer-name)
794 (gdefinition writer-name))))
795 (setf (slot-value dslotd 'internal-reader-function)
796 reader)
797 (setf (slot-value dslotd 'internal-writer-function)
798 writer)))
799 direct-slots reader-names writer-names)
800 (setf (slot-value class 'defstruct-form) defstruct-form)
801 (setf (slot-value class 'defstruct-constructor) constructor)))
802 ;; FIXME: If we always need a default constructor, then why not just make
803 ;; a "hidden" one at actual-compile-time and store it?
804 ;; And this is very broken with regard to the lexical environment.
805 ;; And why isn't this just DD-DEFAULT-CONSTRUCTOR if there was one?
806 (setf (slot-value class 'defstruct-constructor)
807 ;; KLUDGE: not class; in fixup.lisp, can't access slots
808 ;; outside methods yet.
809 (make-defstruct-allocation-function name class)))
810 (add-direct-subclasses class direct-superclasses)
811 (setf (slot-value class '%class-precedence-list)
812 (compute-class-precedence-list class))
813 (setf (slot-value class 'cpl-available-p) t)
814 (let ((slots (compute-slots class)))
815 (setf (slot-value class 'slots) slots)
816 (let* ((lclass (find-classoid (slot-value class 'name)))
817 (layout (classoid-layout lclass)))
818 (setf (classoid-pcl-class lclass) class)
819 (setf (slot-value class 'wrapper) layout)
820 (setf (layout-slot-table layout) (make-slot-table class slots))))
821 (setf (slot-value class 'finalized-p) t)
822 (add-slot-accessors class direct-slots)))
824 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
825 (declare (ignore initargs))
826 (find-class 'structure-direct-slot-definition))
828 (defmethod finalize-inheritance ((class structure-class))
829 nil) ; always finalized
831 (defun add-slot-accessors (class dslotds)
832 (fix-slot-accessors class dslotds 'add))
834 (defun remove-slot-accessors (class dslotds)
835 (fix-slot-accessors class dslotds 'remove))
837 (defun fix-slot-accessors (class dslotds add/remove)
838 (flet ((fix (gfspec name r/w doc source-location)
839 (let ((gf (cond ((eq add/remove 'add)
840 (or (find-generic-function gfspec nil)
841 (ensure-generic-function
842 gfspec :lambda-list (case r/w
843 (r '(object))
844 (w '(new-value object))))))
846 (find-generic-function gfspec nil)))))
847 (when gf
848 (case r/w
849 (r (if (eq add/remove 'add)
850 (add-reader-method class gf name doc source-location)
851 (remove-reader-method class gf)))
852 (w (if (eq add/remove 'add)
853 (add-writer-method class gf name doc source-location)
854 (remove-writer-method class gf))))))))
855 (dolist (dslotd dslotds)
856 (let ((slot-name (slot-definition-name dslotd))
857 (slot-doc (%slot-definition-documentation dslotd))
858 (location (definition-source dslotd)))
859 (dolist (r (slot-definition-readers dslotd))
860 (fix r slot-name 'r slot-doc location))
861 (dolist (w (slot-definition-writers dslotd))
862 (fix w slot-name 'w slot-doc location))))))
864 (defun add-direct-subclasses (class supers)
865 (dolist (super supers)
866 (unless (memq class (class-direct-subclasses class))
867 (add-direct-subclass super class))))
869 (defmethod finalize-inheritance ((class std-class))
870 (update-class class t))
872 (defmethod finalize-inheritance ((class forward-referenced-class))
873 ;; FIXME: should we not be thinking a bit about what kinds of error
874 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
875 ;; possibly a forward-referenced-class-error, though that's
876 ;; difficult given e.g. class precedence list calculations...
877 (error
878 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
879 ~2I~_~S~:>"
880 class))
883 (defun class-has-a-forward-referenced-superclass-p (class)
884 (or (when (forward-referenced-class-p class)
885 class)
886 (some #'class-has-a-forward-referenced-superclass-p
887 (class-direct-superclasses class))))
889 ;;; This is called by :after shared-initialize whenever a class is initialized
890 ;;; or reinitialized. The class may or may not be finalized.
891 (defun update-class (class finalizep)
892 (labels ((rec (class finalizep &optional (seen '()))
893 (when (find class seen :test #'eq)
894 (error "~@<Specified class ~S as a superclass of ~
895 itself.~@:>"
896 class))
897 (without-package-locks
898 (with-world-lock ()
899 (when (or finalizep (class-finalized-p class))
900 (%update-cpl class (compute-class-precedence-list class))
901 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
902 ;; class
903 (%update-slots class (compute-slots class))
904 (update-gfs-of-class class)
905 (setf (plist-value class 'default-initargs) (compute-default-initargs class))
906 (update-ctors 'finalize-inheritance :class class))
907 (let ((seen (list* class seen)))
908 (dolist (sub (class-direct-subclasses class))
909 (rec sub nil seen)))))))
910 (rec class finalizep)))
912 (define-condition cpl-protocol-violation (reference-condition error)
913 ((class :initarg :class :reader cpl-protocol-violation-class)
914 (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
915 (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
916 (:report
917 (lambda (c s)
918 (format s "~@<Protocol violation: the ~S class ~S ~
919 ~:[has~;does not have~] the class ~S in its ~
920 class precedence list: ~S.~@:>"
921 (class-name (class-of (cpl-protocol-violation-class c)))
922 (cpl-protocol-violation-class c)
923 (eq (class-of (cpl-protocol-violation-class c))
924 *the-class-funcallable-standard-class*)
925 (find-class 'function)
926 (cpl-protocol-violation-cpl c)))))
928 (defun class-has-a-cpl-protocol-violation-p (class)
929 (labels ((find-in-superclasses (class classes)
930 (cond
931 ((null classes) nil)
932 ((eql class (car classes)) t)
933 (t (find-in-superclasses class (append (class-direct-superclasses (car classes)) (cdr classes)))))))
934 (let ((metaclass (class-of class)))
935 (cond
936 ((eql metaclass *the-class-standard-class*)
937 (find-in-superclasses (find-class 'function) (list class)))
938 ((eql metaclass *the-class-funcallable-standard-class*)
939 (not (find-in-superclasses (find-class 'function) (list class))))))))
941 (defun %update-cpl (class cpl)
942 (when (or (and
943 (eq (class-of class) *the-class-standard-class*)
944 (find *the-class-function* cpl))
945 (and (eq (class-of class) *the-class-funcallable-standard-class*)
946 (not (and (find (find-class 'function) cpl)))))
947 (error 'cpl-protocol-violation :class class :cpl cpl))
948 (cond ((not (class-finalized-p class))
949 (setf (slot-value class '%class-precedence-list) cpl
950 (slot-value class 'cpl-available-p) t))
951 ((not (and (equal (class-precedence-list class) cpl)
952 (dolist (c cpl t)
953 (when (position :class (class-direct-slots c)
954 :key #'slot-definition-allocation)
955 (return nil)))))
957 ;; comment from the old CMU CL sources:
958 ;; Need to have the cpl setup before %update-lisp-class-layout
959 ;; is called on CMU CL.
960 (setf (slot-value class '%class-precedence-list) cpl
961 (slot-value class 'cpl-available-p) t)
962 (%force-cache-flushes class)))
963 (update-class-can-precede-p cpl))
965 (defun update-class-can-precede-p (cpl)
966 (when cpl
967 (let ((first (car cpl)))
968 (dolist (c (cdr cpl))
969 (pushnew c (slot-value first 'can-precede-list) :test #'eq)))
970 (update-class-can-precede-p (cdr cpl))))
972 (defun class-can-precede-p (class1 class2)
973 (member class2 (class-can-precede-list class1) :test #'eq))
975 ;;; This is called from %UPDATE-SLOTS to check if slot layouts are compatible.
977 ;;; In addition to slot locations (implicit in the ordering of the slots), we
978 ;;; must check classes: SLOT-INFO structures from old slotds may have been
979 ;;; cached in permutation vectors, but new slotds have had new ones allocated
980 ;;; to them. This is non-problematic for standard slotds, because we know the
981 ;;; structure is compatible, but if a slot definition class changes, this can
982 ;;; change the way SLOT-VALUE-USING-CLASS should dispatch.
984 ;;; Also, if the slot has a non-standard allocation, we need to check that it
985 ;;; doesn't change.
986 (defun slot-layouts-compatible-p
987 (oslotds new-instance-slotds new-class-slotds new-custom-slotds)
988 (multiple-value-bind (old-instance-slotds old-class-slotds old-custom-slotds)
989 (classify-slotds oslotds)
990 (and
991 ;; Instance slots: name, type, and class.
992 (dolist (o old-instance-slotds (not new-instance-slotds))
993 (let ((n (pop new-instance-slotds)))
994 (unless (and n
995 (eq (slot-definition-name o) (slot-definition-name n))
996 (eq (slot-definition-type o) (slot-definition-type n))
997 (eq (class-of o) (class-of n)))
998 (return nil))))
999 ;; Class slots: name and class. (FIXME: class slots not typechecked?)
1000 (dolist (o old-class-slotds (not new-class-slotds))
1001 (let ((n (pop new-class-slotds)))
1002 (unless (and n
1003 (eq (slot-definition-name o) (slot-definition-name n))
1004 (eq (class-of n) (class-of o)))
1005 (return nil))))
1006 ;; Custom slots: check name, type, allocation, and class. (FIXME: should we just punt?)
1007 (dolist (o old-custom-slotds (not new-custom-slotds))
1008 (let ((n (pop new-custom-slotds)))
1009 (unless (and n
1010 (eq (slot-definition-name o) (slot-definition-name n))
1011 (eq (slot-definition-type o) (slot-definition-type n))
1012 (eq (slot-definition-allocation o) (slot-definition-allocation n))
1013 (eq (class-of o) (class-of n)))
1014 (return nil)))))))
1016 (defun style-warn-about-duplicate-slots (class)
1017 (do* ((slots (slot-value class 'slots) (cdr slots))
1018 (dupes nil))
1019 ((null slots)
1020 (when dupes
1021 (style-warn
1022 "~@<slot names with the same SYMBOL-NAME but ~
1023 different SYMBOL-PACKAGE (possible package problem) ~
1024 for class ~S:~4I~@:_~<~@{~/sb-impl::print-symbol-with-prefix/~^~:@_~}~:>~@:>"
1025 class dupes)))
1026 (let* ((slot-name (slot-definition-name (car slots)))
1027 (oslots (and (not (eq (symbol-package slot-name)
1028 *pcl-package*))
1029 (remove-if
1030 (lambda (slot-name-2)
1031 (or (eq (symbol-package slot-name-2)
1032 *pcl-package*)
1033 (string/= slot-name slot-name-2)))
1034 (cdr slots)
1035 :key #'slot-definition-name))))
1036 (when oslots
1037 (pushnew (cons slot-name
1038 (mapcar #'slot-definition-name oslots))
1039 dupes
1040 :test #'string= :key #'car)))))
1042 (defun %update-slots (class eslotds)
1043 (multiple-value-bind (instance-slots class-slots custom-slots)
1044 (classify-slotds eslotds)
1045 (let* ((nslots (length instance-slots))
1046 (owrapper (class-wrapper class))
1047 (nwrapper
1048 (cond ((null owrapper)
1049 (make-wrapper nslots class))
1050 ((slot-layouts-compatible-p (layout-slot-list owrapper)
1051 instance-slots class-slots custom-slots)
1052 owrapper)
1054 ;; This will initialize the new wrapper to have the
1055 ;; same state as the old wrapper. We will then have
1056 ;; to change that. This may seem like wasted work
1057 ;; (and it is), but the spec requires that we call
1058 ;; MAKE-INSTANCES-OBSOLETE.
1059 (make-instances-obsolete class)
1060 (class-wrapper class)))))
1061 (%update-lisp-class-layout class nwrapper)
1062 (setf (slot-value class 'slots) eslotds
1063 (layout-slot-list nwrapper) eslotds
1064 (layout-slot-table nwrapper) (make-slot-table class eslotds)
1065 (layout-length nwrapper) nslots
1066 (slot-value class 'wrapper) nwrapper)
1067 (style-warn-about-duplicate-slots class)
1068 (setf (slot-value class 'finalized-p) t)
1069 (unless (eq owrapper nwrapper)
1070 (maybe-update-standard-slot-locations class)))))
1072 (defun update-gf-dfun (class gf)
1073 (let ((*new-class* class)
1074 (arg-info (gf-arg-info gf)))
1075 (cond
1076 ((special-case-for-compute-discriminating-function-p gf))
1077 ((gf-precompute-dfun-and-emf-p arg-info)
1078 (multiple-value-bind (dfun cache info) (make-final-dfun-internal gf)
1079 (update-dfun gf dfun cache info))))))
1081 (defun update-gfs-of-class (class)
1082 (when (and (class-finalized-p class)
1083 (let ((cpl (class-precedence-list class)))
1084 (or (member *the-class-slot-class* cpl :test #'eq)
1085 (member *the-class-standard-effective-slot-definition*
1086 cpl :test #'eq))))
1087 (let ((gf-table (make-hash-table :test 'eq)))
1088 (labels ((collect-gfs (class)
1089 (dolist (gf (specializer-direct-generic-functions class))
1090 (setf (gethash gf gf-table) t))
1091 (mapc #'collect-gfs (class-direct-superclasses class))))
1092 (collect-gfs class)
1093 (maphash (lambda (gf ignore)
1094 (declare (ignore ignore))
1095 (update-gf-dfun class gf))
1096 gf-table)))))
1098 (defmethod compute-default-initargs ((class slot-class))
1099 (let ((initargs (loop for c in (class-precedence-list class)
1100 append (class-direct-default-initargs c))))
1101 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1103 ;;;; protocols for constructing direct and effective slot definitions
1105 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1106 (declare (ignore initargs))
1107 (find-class 'standard-direct-slot-definition))
1109 (defun make-direct-slotd (class initargs)
1110 (apply #'make-instance
1111 (apply #'direct-slot-definition-class class initargs)
1112 :class class
1113 initargs))
1115 ;;; I (CSR) am not sure, but I believe that the particular order of
1116 ;;; slots is quite important: it is ideal to attempt to have a
1117 ;;; constant slot location for the same notional slots as much as
1118 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1119 ;;; al.) have a chance of working. The below at least walks through
1120 ;;; the slots predictably, but maybe it would be good to compute some
1121 ;;; kind of optimal slot layout by looking at locations of slots in
1122 ;;; superclasses?
1123 (defun std-compute-slots (class)
1124 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1125 ;; for each different slot name we find in our superclasses. Each
1126 ;; call receives the class and a list of the dslotds with that name.
1127 ;; The list is in most-specific-first order.
1128 (let ((name-dslotds-alist ()))
1129 (dolist (c (reverse (class-precedence-list class)))
1130 (dolist (slot (class-direct-slots c))
1131 (let* ((name (slot-definition-name slot))
1132 (entry (assq name name-dslotds-alist)))
1133 (if entry
1134 (push slot (cdr entry))
1135 (push (list name slot) name-dslotds-alist)))))
1136 (mapcar (lambda (direct)
1137 (compute-effective-slot-definition class
1138 (car direct)
1139 (cdr direct)))
1140 (nreverse name-dslotds-alist))))
1142 ;; It seems to me that this should be one method defined on SLOT-CLASS.
1143 (defmethod compute-slots ((class standard-class))
1144 (std-compute-slots class))
1145 (defmethod compute-slots ((class funcallable-standard-class))
1146 (std-compute-slots class))
1147 (defmethod compute-slots ((class structure-class))
1148 (std-compute-slots class))
1149 (defmethod compute-slots ((class condition-class))
1150 (std-compute-slots class))
1152 (defun std-compute-slots-around (class eslotds)
1153 (let ((location -1)
1154 (safe (safe-p class)))
1155 (dolist (eslotd eslotds eslotds)
1156 (setf (slot-definition-location eslotd)
1157 (case (slot-definition-allocation eslotd)
1158 (:instance
1159 (incf location))
1160 (:class
1161 (let* ((name (slot-definition-name eslotd))
1162 (from-class
1164 (slot-definition-allocation-class eslotd)
1165 ;; we get here if the user adds an extra slot
1166 ;; himself...
1167 (setf (slot-definition-allocation-class eslotd)
1168 class)))
1169 ;; which raises the question of what we should
1170 ;; do if we find that said user has added a slot
1171 ;; with the same name as another slot...
1172 (cell (or (assq name (class-slot-cells from-class))
1173 (let ((c (cons name +slot-unbound+)))
1174 (push c (class-slot-cells from-class))
1175 c))))
1176 (aver (consp cell))
1177 (if (eq +slot-unbound+ (cdr cell))
1178 ;; We may have inherited an initfunction FIXME: Is this
1179 ;; really right? Is the initialization in
1180 ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1181 (let ((initfun (slot-definition-initfunction eslotd)))
1182 (if initfun
1183 (rplacd cell (call-initfun initfun eslotd safe))
1184 cell))
1185 cell)))))
1186 (unless (slot-definition-class eslotd)
1187 (setf (slot-definition-class eslotd) class))
1188 (initialize-internal-slot-functions eslotd))))
1190 (defmethod compute-slots :around ((class standard-class))
1191 (let ((eslotds (call-next-method)))
1192 (std-compute-slots-around class eslotds)))
1193 (defmethod compute-slots :around ((class funcallable-standard-class))
1194 (let ((eslotds (call-next-method)))
1195 (std-compute-slots-around class eslotds)))
1196 (defmethod compute-slots :around ((class structure-class))
1197 (let ((eslotds (call-next-method)))
1198 (mapc #'finalize-internal-slot-functions eslotds)
1199 eslotds))
1201 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1202 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1203 (class (apply #'effective-slot-definition-class class initargs))
1204 (slotd (apply #'make-instance class initargs)))
1205 slotd))
1207 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1208 (declare (ignore initargs))
1209 (find-class 'standard-effective-slot-definition))
1211 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1212 (declare (ignore initargs))
1213 (find-class 'structure-effective-slot-definition))
1215 (defmethod compute-effective-slot-definition-initargs
1216 ((class slot-class) direct-slotds)
1217 (let* ((name nil)
1218 (initfunction nil)
1219 (initform nil)
1220 (initargs nil)
1221 (allocation nil)
1222 (allocation-class nil)
1223 (type t)
1224 (documentation nil)
1225 (documentationp nil)
1226 (namep nil)
1227 (initp nil)
1228 (allocp nil))
1230 (dolist (slotd direct-slotds)
1231 (when slotd
1232 (unless namep
1233 (setq name (slot-definition-name slotd)
1234 namep t))
1235 (unless initp
1236 (awhen (slot-definition-initfunction slotd)
1237 (setq initform (slot-definition-initform slotd)
1238 initfunction it
1239 initp t)))
1240 (unless documentationp
1241 (awhen (%slot-definition-documentation slotd)
1242 (setq documentation it
1243 documentationp t)))
1244 (unless allocp
1245 (setq allocation (slot-definition-allocation slotd)
1246 allocation-class (slot-definition-class slotd)
1247 allocp t))
1248 (setq initargs (append (slot-definition-initargs slotd) initargs))
1249 (let ((slotd-type (slot-definition-type slotd)))
1250 (setq type (cond
1251 ((eq type t) slotd-type)
1252 ;; This pairwise type intersection is perhaps a
1253 ;; little inefficient and inelegant, but it's
1254 ;; unlikely to lie on the critical path. Shout
1255 ;; if I'm wrong. -- CSR, 2005-11-24
1256 (t (type-specifier
1257 (specifier-type `(and ,type ,slotd-type)))))))))
1258 (list :name name
1259 :initform initform
1260 :initfunction initfunction
1261 :initargs initargs
1262 :allocation allocation
1263 :allocation-class allocation-class
1264 :type type
1265 :class class
1266 :documentation documentation)))
1268 (defmethod compute-effective-slot-definition-initargs :around
1269 ((class structure-class) direct-slotds)
1270 (let* ((slotd (car direct-slotds))
1271 (accessor (slot-definition-defstruct-accessor-symbol slotd)))
1272 (list* :defstruct-accessor-symbol accessor
1273 :internal-reader-function
1274 (slot-definition-internal-reader-function slotd)
1275 :internal-writer-function
1276 (slot-definition-internal-writer-function slotd)
1277 (call-next-method))))
1279 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1280 ;;; to make the method object. They have to use make-a-method which
1281 ;;; is a specially bootstrapped mechanism for making standard methods.
1282 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1283 (declare (ignore direct-slot initargs))
1284 (find-class 'standard-reader-method))
1286 (defmethod add-reader-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1287 (add-method generic-function
1288 (make-a-method 'standard-reader-method
1290 (list (or (class-name class) 'object))
1291 (list class)
1292 (make-reader-method-function class slot-name)
1293 (or slot-documentation "automatically generated reader method")
1294 :slot-name slot-name
1295 :object-class class
1296 :method-class-function #'reader-method-class
1297 :definition-source source-location)))
1299 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1300 (declare (ignore direct-slot initargs))
1301 (find-class 'standard-writer-method))
1303 (defmethod add-writer-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1304 (add-method generic-function
1305 (make-a-method 'standard-writer-method
1307 (list 'new-value (or (class-name class) 'object))
1308 (list *the-class-t* class)
1309 (make-writer-method-function class slot-name)
1310 (or slot-documentation "automatically generated writer method")
1311 :slot-name slot-name
1312 :object-class class
1313 :method-class-function #'writer-method-class
1314 :definition-source source-location)))
1316 (defmethod add-boundp-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1317 (add-method generic-function
1318 (make-a-method (constantly (find-class 'standard-boundp-method))
1319 class
1321 (list (or (class-name class) 'object))
1322 (list class)
1323 (make-boundp-method-function class slot-name)
1324 (or slot-documentation "automatically generated boundp method")
1325 :slot-name slot-name
1326 :definition-source source-location)))
1328 (defmethod remove-reader-method ((class slot-class) generic-function)
1329 (let ((method (get-method generic-function () (list class) nil)))
1330 (when method (remove-method generic-function method))))
1332 (defmethod remove-writer-method ((class slot-class) generic-function)
1333 (let ((method
1334 (get-method generic-function () (list *the-class-t* class) nil)))
1335 (when method (remove-method generic-function method))))
1337 (defmethod remove-boundp-method ((class slot-class) generic-function)
1338 (let ((method (get-method generic-function () (list class) nil)))
1339 (when method (remove-method generic-function method))))
1341 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1342 ;;; function are NOT part of the standard protocol. They are however
1343 ;;; useful; PCL makes use of them internally and documents them for
1344 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1346 ;;; *** This needs work to make type testing by the writer functions which
1347 ;;; *** do type testing faster. The idea would be to have one constructor
1348 ;;; *** for each possible type test.
1350 ;;; *** There is a subtle bug here which is going to have to be fixed.
1351 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1352 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1353 ;;; *** defined for this metaclass a chance to run.
1355 (defmethod make-reader-method-function ((class slot-class) slot-name)
1356 (make-std-reader-method-function class slot-name))
1358 (defmethod make-writer-method-function ((class slot-class) slot-name)
1359 (make-std-writer-method-function class slot-name))
1361 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1362 (make-std-boundp-method-function class slot-name))
1364 (defmethod compatible-meta-class-change-p (class proto-new-class)
1365 (eq (class-of class) (class-of proto-new-class)))
1367 (defmethod validate-superclass ((class class) (superclass class))
1368 (or (eq (class-of class) (class-of superclass))
1369 (and (eq (class-of superclass) *the-class-standard-class*)
1370 (eq (class-of class) *the-class-funcallable-standard-class*))
1371 (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1372 (eq (class-of class) *the-class-standard-class*))))
1374 ;;; What this does depends on which of the four possible values of
1375 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1376 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1377 ;;; nothing to do, as the new wrapper has already been created. If
1378 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1379 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1380 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1382 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1383 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1384 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1385 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1386 ;;; obsolete the wrapper.
1388 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1389 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1390 ;;; :UNINITIALIZED)))
1392 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1393 (defun %force-cache-flushes (class)
1394 (with-world-lock ()
1395 (let* ((owrapper (class-wrapper class)))
1396 ;; We only need to do something if the wrapper is still valid. If
1397 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1398 ;; both of those will already be doing what we want. In
1399 ;; particular, we must be sure we never change an OBSOLETE into a
1400 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1401 (when (or (not (invalid-wrapper-p owrapper))
1402 ;; KLUDGE: despite the observations above, this remains
1403 ;; a violation of locality or what might be considered
1404 ;; good style. There has to be a better way! -- CSR,
1405 ;; 2002-10-29
1406 (eq (layout-invalid owrapper) t))
1407 (let ((nwrapper (make-wrapper (layout-length owrapper)
1408 class)))
1409 (setf (layout-slot-list nwrapper) (layout-slot-list owrapper))
1410 (setf (layout-slot-table nwrapper) (layout-slot-table owrapper))
1411 (%update-lisp-class-layout class nwrapper)
1412 (setf (slot-value class 'wrapper) nwrapper)
1413 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1414 ;; been obsoleted.
1415 (if (find-if (lambda (x)
1416 (and (consp x) (eq :obsolete (car x))))
1417 (layout-inherits owrapper)
1418 :key #'layout-invalid)
1419 (%invalidate-wrapper owrapper :obsolete nwrapper)
1420 (%invalidate-wrapper owrapper :flush nwrapper))))))
1421 nil)
1423 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1424 ;;; the next access to the instance (as defined in 88-002R) to trap
1425 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1426 (defmethod make-instances-obsolete ((class std-class))
1427 (with-world-lock ()
1428 (let* ((owrapper (class-wrapper class))
1429 (nwrapper (make-wrapper (layout-length owrapper)
1430 class)))
1431 (unless (class-finalized-p class)
1432 (if (class-has-a-forward-referenced-superclass-p class)
1433 (return-from make-instances-obsolete class)
1434 (%update-cpl class (compute-class-precedence-list class))))
1435 (setf (layout-slot-list nwrapper) (layout-slot-list owrapper))
1436 (setf (layout-slot-table nwrapper) (layout-slot-table owrapper))
1437 (%update-lisp-class-layout class nwrapper)
1438 (setf (slot-value class 'wrapper) nwrapper)
1439 (%invalidate-wrapper owrapper :obsolete nwrapper)
1440 class)))
1442 (defmethod make-instances-obsolete ((class symbol))
1443 (make-instances-obsolete (find-class class))
1444 ;; ANSI wants the class name when called with a symbol.
1445 class)
1447 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1448 ;;; see an obsolete instance. The times when it is called are:
1449 ;;; - when the instance is involved in method lookup
1450 ;;; - when attempting to access a slot of an instance
1452 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1453 ;;; instance access macros.
1455 ;;; Of course these times when it is called are an internal
1456 ;;; implementation detail of PCL and are not part of the documented
1457 ;;; description of when the obsolete instance update happens. The
1458 ;;; documented description is as it appears in 88-002R.
1460 ;;; This has to return the new wrapper, so it counts on all the
1461 ;;; methods on obsolete-instance-trap-internal to return the new
1462 ;;; wrapper. It also does a little internal error checking to make
1463 ;;; sure that the traps are only happening when they should, and that
1464 ;;; the trap methods are computing appropriate new wrappers.
1466 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1467 ;;; after a structure is redefined. In most cases,
1468 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1469 ;;; so it must signal an error. The hard part of this is that the
1470 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1471 ;;; called again, so in that case, we have to return some reasonable
1472 ;;; wrapper, instead.
1474 (defun %ensure-slot-value-type (context slot-name slot-type value
1475 old-class new-class)
1476 (do () ((typep value slot-type))
1477 (restart-case
1478 (bad-type value slot-type
1479 "~@<Error during ~A. Current value in slot ~
1480 ~/sb-impl::print-symbol-with-prefix/ of an instance ~
1481 of ~S is ~S, which does not match the new slot type ~
1482 ~S in class ~S.~:@>"
1483 context slot-name old-class value slot-type new-class)
1484 (use-value (new-value)
1485 :interactive read-evaluated-form
1486 :report (lambda (stream)
1487 (format stream "~@<Specify a new value to by used ~
1488 for slot ~
1489 ~/sb-impl::print-symbol-with-prefix/ ~
1490 instead of ~S.~@:>"
1491 slot-name value))
1492 (setf value new-value))))
1493 value)
1495 (defun %set-slot-value-checking-type (context slots slot value
1496 safe old-class new-class)
1497 (setf (clos-slots-ref slots (slot-definition-location slot))
1498 (if (and safe (neq value +slot-unbound+))
1499 (let ((name (slot-definition-name slot))
1500 (type (slot-definition-type slot)))
1501 (%ensure-slot-value-type context name type value
1502 old-class new-class))
1503 value)))
1505 (defvar *in-obsolete-instance-trap* nil)
1506 (defvar *the-wrapper-of-structure-object*
1507 (class-wrapper (find-class 'structure-object)))
1509 (define-condition obsolete-structure (error)
1510 ((datum :reader obsolete-structure-datum :initarg :datum))
1511 (:report
1512 (lambda (condition stream)
1513 ;; Don't try to print the structure, since it probably won't work.
1514 (format stream
1515 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1516 (type-of (obsolete-structure-datum condition))))))
1518 (defun %obsolete-instance-trap (owrapper nwrapper instance)
1519 (cond
1520 ((layout-for-std-class-p owrapper)
1521 (binding* ((class (wrapper-class* nwrapper))
1522 (copy (allocate-instance class)) ;??? allocate-instance ???
1523 (oslots (get-slots instance))
1524 (nslots (get-slots copy))
1525 (added ())
1526 (discarded ())
1527 (plist ())
1528 (safe (safe-p class))
1529 ((new-instance-slots nil new-custom-slots)
1530 (classify-slotds (layout-slot-list nwrapper)))
1531 ((old-instance-slots old-class-slots old-custom-slots)
1532 (classify-slotds (layout-slot-list owrapper)))
1533 (layout (mapcar (lambda (slotd)
1534 ;; Get the names only once.
1535 (cons (slot-definition-name slotd) slotd))
1536 new-instance-slots)))
1537 ;; local --> local transfer value, check type
1538 ;; local --> shared discard value, discard slot
1539 ;; local --> -- discard slot
1540 ;; local --> custom XXX
1542 ;; shared --> local transfer value, check type
1543 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1544 ;; shared --> -- discard value
1545 ;; shared --> custom XXX
1547 ;; -- --> local add slot
1548 ;; -- --> shared --
1549 ;; -- --> custom XXX
1550 (flet ((set-value (value cell)
1551 (%set-slot-value-checking-type
1552 "updating obsolete instance"
1553 nslots (cdr cell) value safe class class)
1554 ;; Prune from the list now that it's been dealt with.
1555 (setf layout (remove cell layout))))
1557 ;; Go through all the old local slots.
1558 (dolist (old old-instance-slots)
1559 (let* ((name (slot-definition-name old))
1560 (value (clos-slots-ref oslots (slot-definition-location old))))
1561 (unless (eq value +slot-unbound+)
1562 (let ((new (assq name layout)))
1563 (cond (new
1564 (set-value value new))
1566 (push name discarded)
1567 (setf (getf plist name) value)))))))
1569 ;; Go through all the old shared slots.
1570 (dolist (old old-class-slots)
1571 (binding* ((cell (slot-definition-location old))
1572 (name (car cell))
1573 (new (assq name layout) :exit-if-null))
1574 (set-value (cdr cell) new)))
1576 ;; Go through all custom slots to find added ones. CLHS
1577 ;; doesn't specify what to do about them, and neither does
1578 ;; AMOP. We do want them to get initialized, though, so we
1579 ;; list them in ADDED for the benefit of SHARED-INITIALIZE.
1580 (dolist (new new-custom-slots)
1581 (let* ((name (slot-definition-name new))
1582 (old (find name old-custom-slots
1583 :key #'slot-definition-name)))
1584 (unless old
1585 (push name added))))
1587 ;; Go through all the remaining new local slots to compute
1588 ;; the added slots.
1589 (dolist (cell layout)
1590 (push (car cell) added)))
1592 (%swap-wrappers-and-slots instance copy)
1594 (update-instance-for-redefined-class
1595 instance added discarded plist)
1597 nwrapper))
1598 (*in-obsolete-instance-trap*
1599 *the-wrapper-of-structure-object*)
1601 (let ((*in-obsolete-instance-trap* t))
1602 (error 'obsolete-structure :datum instance)))))
1605 (defun %change-class (instance new-class initargs)
1606 (binding* ((old-class (class-of instance))
1607 (copy (allocate-instance new-class))
1608 (new-wrapper (get-wrapper copy))
1609 (old-wrapper (class-wrapper old-class))
1610 (old-slots (get-slots instance))
1611 (new-slots (get-slots copy))
1612 (safe (safe-p new-class))
1613 (new-instance-slots
1614 (classify-slotds (layout-slot-list new-wrapper)))
1615 ((old-instance-slots old-class-slots)
1616 (classify-slotds (layout-slot-list old-wrapper))))
1617 (labels ((find-slot (name slots)
1618 (find name slots :key #'slot-definition-name))
1619 (initarg-for-slot-p (slot)
1620 (dolist (slot-initarg (slot-definition-initargs slot))
1621 ;; Abuse +slot-unbound+
1622 (unless (eq +slot-unbound+
1623 (getf initargs slot-initarg +slot-unbound+))
1624 (return t))))
1625 (set-value (value slotd)
1626 (%set-slot-value-checking-type
1627 'change-class new-slots slotd value safe
1628 old-class new-class)))
1630 ;; "The values of local slots specified by both the class CTO
1631 ;; and CFROM are retained. If such a local slot was unbound, it
1632 ;; remains unbound."
1633 (dolist (new new-instance-slots)
1634 (unless (initarg-for-slot-p new)
1635 (binding* ((old (find-slot (slot-definition-name new) old-instance-slots)
1636 :exit-if-null)
1637 (value (clos-slots-ref old-slots (slot-definition-location old))))
1638 (set-value value new))))
1640 ;; "The values of slots specified as shared in the class CFROM and
1641 ;; as local in the class CTO are retained."
1642 (dolist (old old-class-slots)
1643 (binding* ((slot-and-val (slot-definition-location old))
1644 (new (find-slot (car slot-and-val) new-instance-slots)
1645 :exit-if-null))
1646 (set-value (cdr slot-and-val) new))))
1648 ;; Make the copy point to the old instance's storage, and make the
1649 ;; old instance point to the new storage.
1650 (%swap-wrappers-and-slots instance copy)
1652 (apply #'update-instance-for-different-class copy instance initargs)
1654 instance))
1656 (defun check-new-class-not-metaobject (new-class)
1657 (dolist (class (class-precedence-list
1658 (ensure-class-finalized new-class)))
1659 (macrolet
1660 ((check-metaobject (class-name)
1661 `(when (eq class (find-class ',class-name))
1662 (change-class-to-metaobject-violation
1663 ',class-name nil '((:amop :initialization ,class-name))))))
1664 (check-metaobject class)
1665 (check-metaobject generic-function)
1666 (check-metaobject method)
1667 (check-metaobject slot-definition))))
1669 (defmethod change-class ((instance standard-object) (new-class standard-class)
1670 &rest initargs)
1671 (with-world-lock ()
1672 (check-new-class-not-metaobject new-class)
1673 (%change-class instance new-class initargs)))
1675 (defmethod change-class ((instance forward-referenced-class)
1676 (new-class standard-class) &rest initargs)
1677 (with-world-lock ()
1678 (dolist (class (class-precedence-list
1679 (ensure-class-finalized new-class))
1680 (change-class-to-metaobject-violation
1681 '(not class) 'forward-referenced-class
1682 '((:amop :generic-function ensure-class-using-class)
1683 (:amop :initialization class))))
1684 (when (eq class (find-class 'class))
1685 (return nil)))
1686 (%change-class instance new-class initargs)))
1688 (defmethod change-class ((instance t)
1689 (new-class forward-referenced-class) &rest initargs)
1690 (declare (ignore initargs))
1691 (change-class-to-metaobject-violation
1692 'forward-referenced-class nil
1693 '((:amop :generic-function ensure-class-using-class)
1694 (:amop :initialization class))))
1696 (defmethod change-class ((instance funcallable-standard-object)
1697 (new-class funcallable-standard-class)
1698 &rest initargs)
1699 (with-world-lock ()
1700 (check-new-class-not-metaobject new-class)
1701 (%change-class instance new-class initargs)))
1703 (defmethod change-class ((instance standard-object)
1704 (new-class funcallable-standard-class)
1705 &rest initargs)
1706 (declare (ignore initargs))
1707 (error "You can't change the class of ~S to ~S~@
1708 because it isn't already an instance with metaclass ~S."
1709 instance new-class 'standard-class))
1711 (defmethod change-class ((instance funcallable-standard-object)
1712 (new-class standard-class)
1713 &rest initargs)
1714 (declare (ignore initargs))
1715 (error "You can't change the class of ~S to ~S~@
1716 because it isn't already an instance with metaclass ~S."
1717 instance new-class 'funcallable-standard-class))
1719 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1720 (apply #'change-class instance (find-class new-class-name) initargs))
1722 ;;;; The metaclasses SYSTEM-CLASS and BUILT-IN-CLASS
1723 ;;;;
1724 ;;;; These metaclasses are something of a weird creature. By this
1725 ;;;; point, all instances which will exist have been created, and no
1726 ;;;; instance is ever created by calling MAKE-INSTANCE. (The
1727 ;;;; distinction between the metaclasses is that we allow subclassing
1728 ;;;; of SYSTEM-CLASS, such as through STREAM and SEQUENCE protocols,
1729 ;;;; but not of BUILT-IN-CLASS.)
1730 ;;;;
1731 ;;;; AMOP mandates some behaviour of the implementation with respect
1732 ;;;; to BUILT-IN-CLASSes, and we implement that through methods on
1733 ;;;; SYSTEM-CLASS here.
1735 (macrolet ((def (name args control)
1736 `(defmethod ,name ,args
1737 (declare (ignore initargs))
1738 (error 'metaobject-initialization-violation
1739 :format-control ,(format nil "~@<~A~@:>" control)
1740 :format-arguments (list (class-name class))
1741 :references (list '(:amop :initialization "Class"))))))
1742 (def initialize-instance ((class system-class) &rest initargs)
1743 "Cannot initialize an instance of ~S.")
1744 (def reinitialize-instance ((class system-class) &rest initargs)
1745 "Cannot reinitialize an instance of ~S."))
1747 (macrolet ((def (name) `(defmethod ,name ((class system-class)) nil)))
1748 (def class-direct-slots)
1749 (def class-slots)
1750 (def class-direct-default-initargs)
1751 (def class-default-initargs))
1753 (defmethod validate-superclass ((c class) (s system-class))
1755 (defmethod validate-superclass ((c class) (s built-in-class))
1756 nil)
1758 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1759 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1760 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1761 (macrolet ((def (method)
1762 `(defmethod ,method ((class forward-referenced-class))
1763 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1764 ',method class))))
1765 (def class-default-initargs)
1766 (def class-precedence-list)
1767 (def class-slots))
1769 (defmethod validate-superclass ((c slot-class) (f forward-referenced-class))
1772 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1773 (pushnew dependent (plist-value metaobject 'dependents) :test #'eq))
1775 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1776 (setf (plist-value metaobject 'dependents)
1777 (delete dependent (plist-value metaobject 'dependents))))
1779 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1780 (dolist (dependent (plist-value metaobject 'dependents))
1781 (funcall function dependent)))