1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
26 (defmethod slot-accessor-function ((slotd effective-slot-definition
) type
)
28 (reader (slot-definition-reader-function slotd
))
29 (writer (slot-definition-writer-function slotd
))
30 (boundp (slot-definition-boundp-function slotd
))))
32 (defmethod (setf slot-accessor-function
) (function
33 (slotd effective-slot-definition
)
36 (reader (setf (slot-definition-reader-function slotd
) function
))
37 (writer (setf (slot-definition-writer-function slotd
) function
))
38 (boundp (setf (slot-definition-boundp-function slotd
) function
))))
40 (defconstant +slotd-reader-function-std-p
+ 1)
41 (defconstant +slotd-writer-function-std-p
+ 2)
42 (defconstant +slotd-boundp-function-std-p
+ 4)
43 (defconstant +slotd-all-function-std-p
+ 7)
45 (defmethod slot-accessor-std-p ((slotd effective-slot-definition
) type
)
46 (let ((flags (slot-value slotd
'accessor-flags
)))
47 (declare (type fixnum flags
))
49 (eql +slotd-all-function-std-p
+ flags
)
50 (let ((mask (ecase type
51 (reader +slotd-reader-function-std-p
+)
52 (writer +slotd-writer-function-std-p
+)
53 (boundp +slotd-boundp-function-std-p
+))))
54 (declare (type fixnum mask
))
55 (not (zerop (the fixnum
(logand mask flags
))))))))
57 (defmethod (setf slot-accessor-std-p
) (value
58 (slotd effective-slot-definition
)
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
)
68 (the fixnum
(logior mask flags
))
69 (the fixnum
(logand (the fixnum
(lognot mask
)) flags
)))))
72 (defmethod initialize-internal-slot-functions ((slotd
73 effective-slot-definition
))
74 (let* ((name (slot-value slotd
'name
))
75 (class (slot-value slotd
'%class
)))
76 (dolist (type '(reader writer boundp
))
77 (let* ((gf-name (ecase type
78 (reader 'slot-value-using-class
)
79 (writer '(setf slot-value-using-class
))
80 (boundp 'slot-boundp-using-class
)))
81 (gf (gdefinition gf-name
)))
82 (compute-slot-accessor-info slotd type gf
)))))
84 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
86 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
87 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
88 ;;; writing/testing effective slot SLOTD.
90 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
91 ;;; GF. Store the effective method in the effective slot definition
92 ;;; object itself; these GFs have special dispatch functions calling
93 ;;; effective methods directly retrieved from effective slot
94 ;;; definition objects, as an optimization.
96 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
98 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition
)
100 (let* ((name (slot-value slotd
'name
))
101 (class (slot-value slotd
'%class
))
102 (old-slotd (when (class-finalized-p class
)
103 (find-slot-definition class name
)))
104 (old-std-p (and old-slotd
(slot-accessor-std-p old-slotd
'all
))))
105 (multiple-value-bind (function std-p
)
106 (if (eq *boot-state
* 'complete
)
107 (get-accessor-method-function gf type class slotd
)
108 (get-optimized-std-accessor-method-function class slotd type
))
109 (setf (slot-accessor-std-p slotd type
) std-p
)
110 (setf (slot-accessor-function slotd type
) function
))))
112 (defmethod slot-definition-allocation ((slotd structure-slot-definition
))
115 ;;;; various class accessors that are a little more complicated than can be
116 ;;;; done with automatically generated reader methods
118 (defmethod class-prototype :before
(class)
119 (unless (class-finalized-p class
)
120 (error "~@<~S is not finalized.~:@>" class
)))
122 ;;; KLUDGE: For some reason factoring the common body into a function
123 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
125 (macrolet ((def (class)
126 `(defmethod class-prototype ((class ,class
))
127 (with-slots (prototype) class
129 (setf prototype
(allocate-instance class
)))))))
131 (def condition-class
)
132 (def structure-class
))
134 (defmethod class-direct-default-initargs ((class slot-class
))
135 (plist-value class
'direct-default-initargs
))
137 (defmethod class-default-initargs ((class slot-class
))
138 (plist-value class
'default-initargs
))
140 (defmethod class-slot-cells ((class std-class
))
141 (plist-value class
'class-slot-cells
))
142 (defmethod (setf class-slot-cells
) (new-value (class std-class
))
143 (setf (plist-value class
'class-slot-cells
) new-value
))
145 ;;;; class accessors that are even a little bit more complicated than those
146 ;;;; above. These have a protocol for updating them, we must implement that
149 ;;; Maintaining the direct subclasses backpointers. The update methods are
150 ;;; here, the values are read by an automatically generated reader method.
151 (defmethod add-direct-subclass ((class class
) (subclass class
))
152 (with-slots (direct-subclasses) class
153 (pushnew subclass direct-subclasses
)
155 (defmethod remove-direct-subclass ((class class
) (subclass class
))
156 (with-slots (direct-subclasses) class
157 (setq direct-subclasses
(remove subclass direct-subclasses
))
160 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
162 ;;; There are four generic functions involved, each has one method for the
163 ;;; class case and another method for the damned EQL specializers. All of
164 ;;; these are specified methods and appear in their specified place in the
167 ;;; ADD-DIRECT-METHOD
168 ;;; REMOVE-DIRECT-METHOD
169 ;;; SPECIALIZER-DIRECT-METHODS
170 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
172 ;;; In each case, we maintain one value which is a cons. The car is the list
173 ;;; methods. The cdr is a list of the generic functions. The cdr is always
176 ;;; This needs to be used recursively, in case a non-trivial user
177 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
178 ;;; function using the same lock.
179 (defvar *specializer-lock
* (sb-thread::make-spinlock
:name
"Specializer lock"))
181 (defmethod add-direct-method :around
((specializer specializer
) method
)
182 ;; All the actions done under this lock are done in an order
183 ;; that is safe to unwind at any point.
184 (sb-thread::with-recursive-spinlock
(*specializer-lock
*)
187 (defmethod remove-direct-method :around
((specializer specializer
) method
)
188 ;; All the actions done under this lock are done in an order
189 ;; that is safe to unwind at any point.
190 (sb-thread::with-recursive-spinlock
(*specializer-lock
*)
193 (defmethod add-direct-method ((specializer class
) (method method
))
194 (let ((cell (slot-value specializer
'direct-methods
)))
195 ;; We need to first smash the CDR, because a parallel read may
196 ;; be in progress, and because if an interrupt catches us we
197 ;; need to have a consistent state.
199 (car cell
) (adjoin method
(car cell
))))
202 (defmethod remove-direct-method ((specializer class
) (method method
))
203 (let ((cell (slot-value specializer
'direct-methods
)))
204 ;; We need to first smash the CDR, because a parallel read may
205 ;; be in progress, and because if an interrupt catches us we
206 ;; need to have a consistent state.
208 (car cell
) (remove method
(car cell
))))
211 (defmethod specializer-direct-methods ((specializer class
))
212 (with-slots (direct-methods) specializer
213 (car direct-methods
)))
215 (defmethod specializer-direct-generic-functions ((specializer class
))
216 (let ((cell (slot-value specializer
'direct-methods
)))
217 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
218 ;; we behave as if we got just first or just after -- it's just
219 ;; for update that we need to lock.
221 (sb-thread::with-spinlock
(*specializer-lock
*)
224 (dolist (m (car cell
))
225 ;; the old PCL code used COLLECTING-ONCE which used
226 ;; #'EQ to check for newness
227 (pushnew (method-generic-function m
) collect
:test
#'eq
))
228 (nreverse collect
)))))))
230 ;;; This hash table is used to store the direct methods and direct generic
231 ;;; functions of EQL specializers. Each value in the table is the cons.
232 (defvar *eql-specializer-methods
* (make-hash-table :test
'eql
))
233 (defvar *class-eq-specializer-methods
* (make-hash-table :test
'eq
))
235 (defmethod specializer-method-table ((specializer eql-specializer
))
236 *eql-specializer-methods
*)
238 (defmethod specializer-method-table ((specializer class-eq-specializer
))
239 *class-eq-specializer-methods
*)
241 (defmethod add-direct-method ((specializer specializer-with-object
)
243 (let* ((object (specializer-object specializer
))
244 (table (specializer-method-table specializer
))
245 (entry (gethash object table
)))
246 ;; This table is shared between multiple specializers, but
247 ;; no worries as (at least for the time being) our hash-tables
251 (setf (gethash object table
) (cons nil nil
))))
252 ;; We need to first smash the CDR, because a parallel read may
253 ;; be in progress, and because if an interrupt catches us we
254 ;; need to have a consistent state.
256 (car entry
) (adjoin method
(car entry
)))
259 (defmethod remove-direct-method ((specializer specializer-with-object
)
261 (let* ((object (specializer-object specializer
))
262 (entry (gethash object
(specializer-method-table specializer
))))
264 ;; We need to first smash the CDR, because a parallel read may
265 ;; be in progress, and because if an interrupt catches us we
266 ;; need to have a consistent state.
268 (car entry
) (remove method
(car entry
))))
271 (defmethod specializer-direct-methods ((specializer specializer-with-object
))
272 (car (gethash (specializer-object specializer
)
273 (specializer-method-table specializer
))))
275 (defmethod specializer-direct-generic-functions ((specializer
276 specializer-with-object
))
277 (let* ((object (specializer-object specializer
))
278 (entry (gethash object
(specializer-method-table specializer
))))
281 (sb-thread::with-spinlock
(*specializer-lock
*)
284 (dolist (m (car entry
))
285 (pushnew (method-generic-function m
) collect
:test
#'eq
))
286 (nreverse collect
))))))))
288 (defun map-specializers (function)
289 (map-all-classes (lambda (class)
290 (funcall function
(class-eq-specializer class
))
291 (funcall function class
)))
292 (maphash (lambda (object methods
)
293 (declare (ignore methods
))
294 (intern-eql-specializer object
))
295 *eql-specializer-methods
*)
296 (maphash (lambda (object specl
)
297 (declare (ignore object
))
298 (funcall function specl
))
299 *eql-specializer-table
*)
302 (defun map-all-generic-functions (function)
303 (let ((all-generic-functions (make-hash-table :test
'eq
)))
304 (map-specializers (lambda (specl)
305 (dolist (gf (specializer-direct-generic-functions
307 (unless (gethash gf all-generic-functions
)
308 (setf (gethash gf all-generic-functions
) t
)
309 (funcall function gf
))))))
312 (defmethod shared-initialize :after
((specl class-eq-specializer
)
315 (declare (ignore slot-names
))
316 (setf (slot-value specl
'%type
) `(class-eq ,(specializer-class specl
))))
318 (defmethod shared-initialize :after
((specl eql-specializer
) slot-names
&key
)
319 (declare (ignore slot-names
))
320 (setf (slot-value specl
'%type
)
321 `(eql ,(specializer-object specl
)))
322 (setf (info :type
:translator specl
)
323 (constantly (make-member-type :members
(list (specializer-object specl
))))))
325 (defun real-load-defclass (name metaclass-name supers slots other
326 readers writers slot-names source-location safe-p
)
327 (with-single-package-locked-error (:symbol name
"defining ~S as a class")
328 (%compiler-defclass name readers writers slot-names
)
329 (let ((res (apply #'ensure-class name
:metaclass metaclass-name
330 :direct-superclasses supers
332 :definition-source source-location
337 (setf (gdefinition 'load-defclass
) #'real-load-defclass
)
339 (defun ensure-class (name &rest args
)
340 (apply #'ensure-class-using-class
341 (let ((class (find-class name nil
)))
342 (when (and class
(eq name
(class-name class
)))
343 ;; NAME is the proper name of CLASS, so redefine it
348 (defmethod ensure-class-using-class ((class null
) name
&rest args
&key
)
349 (multiple-value-bind (meta initargs
)
350 (frob-ensure-class-args args
)
351 (setf class
(apply #'make-instance meta
:name name initargs
))
352 (without-package-locks
353 (setf (find-class name
) class
))
354 (set-class-type-translation class name
)
357 (defmethod ensure-class-using-class ((class pcl-class
) name
&rest args
&key
)
358 (multiple-value-bind (meta initargs
)
359 (frob-ensure-class-args args
)
360 (unless (eq (class-of class
) meta
)
361 (apply #'change-class class meta initargs
))
362 (apply #'reinitialize-instance class initargs
)
363 (without-package-locks
364 (setf (find-class name
) class
))
365 (set-class-type-translation class name
)
368 (defun frob-ensure-class-args (args)
369 (let (metaclass metaclassp reversed-plist
)
370 (flet ((frob-superclass (s)
373 ((legal-class-name-p s
)
374 (or (find-class s nil
)
375 (ensure-class s
:metaclass
'forward-referenced-class
)))
376 (t (error "Not a class or a legal class name: ~S." s
)))))
377 (doplist (key val
) args
378 (cond ((eq key
:metaclass
)
380 (setf metaclass val metaclassp key
)))
382 (when (eq key
:direct-superclasses
)
383 (setf val
(mapcar #'frob-superclass val
)))
384 (setf reversed-plist
(list* val key reversed-plist
)))))
385 (values (cond (metaclassp
386 (if (classp metaclass
)
388 (find-class metaclass
)))
389 (t *the-class-standard-class
*))
390 (nreverse reversed-plist
)))))
392 (defmethod shared-initialize :after
393 ((class std-class
) slot-names
&key
394 (direct-superclasses nil direct-superclasses-p
)
395 (direct-slots nil direct-slots-p
)
396 (direct-default-initargs nil direct-default-initargs-p
)
398 (cond (direct-superclasses-p
399 (setq direct-superclasses
400 (or direct-superclasses
401 (list (if (funcallable-standard-class-p class
)
402 *the-class-funcallable-standard-object
*
403 *the-class-standard-object
*))))
404 (dolist (superclass direct-superclasses
)
405 (unless (validate-superclass class superclass
)
406 (error "~@<The class ~S was specified as a ~
407 super-class of the class ~S, ~
408 but the meta-classes ~S and ~S are incompatible. ~
409 Define a method for ~S to avoid this error.~@:>"
410 superclass class
(class-of superclass
) (class-of class
)
411 'validate-superclass
)))
412 (setf (slot-value class
'direct-superclasses
) direct-superclasses
))
414 (setq direct-superclasses
(slot-value class
'direct-superclasses
))))
417 (setf (slot-value class
'direct-slots
)
418 (mapcar (lambda (pl) (make-direct-slotd class pl
))
420 (slot-value class
'direct-slots
)))
421 (if direct-default-initargs-p
422 (setf (plist-value class
'direct-default-initargs
)
423 direct-default-initargs
)
424 (setq direct-default-initargs
425 (plist-value class
'direct-default-initargs
)))
426 (setf (plist-value class
'class-slot-cells
)
427 (let ((old-class-slot-cells (plist-value class
'class-slot-cells
))
429 (dolist (dslotd direct-slots
)
430 (when (eq :class
(slot-definition-allocation dslotd
))
432 (let* ((name (slot-definition-name dslotd
))
433 (old (assoc name old-class-slot-cells
)))
436 (member name slot-names
))
437 (let* ((initfunction (slot-definition-initfunction dslotd
))
438 (value (if initfunction
439 (funcall initfunction
)
441 (push (cons name value
) collect
))
442 (push old collect
)))))
444 (add-direct-subclasses class direct-superclasses
)
445 (if (class-finalized-p class
)
446 ;; required by AMOP, "Reinitialization of Class Metaobjects"
447 (finalize-inheritance class
)
448 (update-class class nil
))
449 (add-slot-accessors class direct-slots definition-source
)
450 (make-preliminary-layout class
))
452 (defmethod shared-initialize :after
((class forward-referenced-class
)
453 slot-names
&key
&allow-other-keys
)
454 (declare (ignore slot-names
))
455 (make-preliminary-layout class
))
457 (defvar *allow-forward-referenced-classes-in-cpl-p
* nil
)
459 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
460 ;;; make it known to the type system.
461 (defun make-preliminary-layout (class)
462 (flet ((compute-preliminary-cpl (root)
463 (let ((*allow-forward-referenced-classes-in-cpl-p
* t
))
464 (compute-class-precedence-list root
))))
465 (without-package-locks
466 (unless (class-finalized-p class
)
467 (let ((name (class-name class
)))
468 ;; KLUDGE: This is fairly horrible. We need to make a
469 ;; full-fledged CLASSOID here, not just tell the compiler that
470 ;; some class is forthcoming, because there are legitimate
471 ;; questions one can ask of the type system, implemented in
472 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
473 (let ((layout (make-wrapper 0 class
)))
474 (setf (slot-value class
'wrapper
) layout
)
475 (let ((cpl (compute-preliminary-cpl class
)))
476 (setf (layout-inherits layout
)
477 (order-layout-inherits
478 (map 'simple-vector
#'class-wrapper
479 (reverse (rest cpl
))))))
480 (register-layout layout
:invalidate t
)
481 (set-class-type-translation class
(layout-classoid layout
)))))
482 (mapc #'make-preliminary-layout
(class-direct-subclasses class
)))))
485 (defmethod shared-initialize :before
((class class
) slot-names
&key name
)
486 (declare (ignore slot-names name
))
487 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
488 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
489 (setf (slot-value class
'%type
) `(class ,class
))
490 (setf (slot-value class
'class-eq-specializer
)
491 (make-instance 'class-eq-specializer
:class class
)))
493 (defmethod reinitialize-instance :before
((class slot-class
) &key direct-superclasses
)
494 (dolist (old-super (set-difference (class-direct-superclasses class
) direct-superclasses
))
495 (remove-direct-subclass old-super class
))
496 (remove-slot-accessors class
(class-direct-slots class
)))
498 (defmethod reinitialize-instance :after
((class slot-class
)
501 (map-dependents class
503 (apply #'update-dependent class dependent initargs
))))
505 (defmethod reinitialize-instance :after
((class condition-class
) &key
)
506 (let* ((name (class-name class
))
507 (classoid (find-classoid name
))
508 (slots (condition-classoid-slots classoid
)))
509 ;; to balance the REMOVE-SLOT-ACCESSORS call in
510 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
512 (let ((slot-name (condition-slot-name slot
)))
513 (dolist (reader (condition-slot-readers slot
))
514 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
515 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
516 (sb-kernel::install-condition-slot-reader reader name slot-name
))
517 (dolist (writer (condition-slot-writers slot
))
518 (sb-kernel::install-condition-slot-writer writer name slot-name
))))))
520 (defmethod shared-initialize :after
((class condition-class
) slot-names
521 &key direct-slots direct-superclasses
)
522 (declare (ignore slot-names
))
523 (let ((classoid (find-classoid (class-name class
))))
524 (with-slots (wrapper %class-precedence-list cpl-available-p
525 prototype
(direct-supers direct-superclasses
))
527 (setf (slot-value class
'direct-slots
)
528 (mapcar (lambda (pl) (make-direct-slotd class pl
))
530 (setf (slot-value class
'finalized-p
) t
)
531 (setf (classoid-pcl-class classoid
) class
)
532 (setq direct-supers direct-superclasses
)
533 (setq wrapper
(classoid-layout classoid
))
534 (setq %class-precedence-list
(compute-class-precedence-list class
))
535 (setq cpl-available-p t
)
536 (add-direct-subclasses class direct-superclasses
)
537 (let ((slots (compute-slots class
)))
538 (setf (slot-value class
'slots
) slots
)
539 (setf (layout-slot-table wrapper
) (make-slot-table class slots
)))))
540 ;; Comment from Gerd's PCL, 2003-05-15:
542 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
543 ;; override condition accessors with generic functions. We do this
546 ;; ??? What does the above comment mean and why is it a good idea?
547 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
548 ;; comment) loses slot information in its condition classes:
549 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
550 ;; remove slot accessors but never put them back. I've added a
551 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
552 ;; was meant to happen? -- CSR, 2005-11-18
555 (defmethod direct-slot-definition-class ((class condition-class
)
557 (declare (ignore initargs
))
558 (find-class 'condition-direct-slot-definition
))
560 (defmethod effective-slot-definition-class ((class condition-class
)
562 (declare (ignore initargs
))
563 (find-class 'condition-effective-slot-definition
))
565 (defmethod finalize-inheritance ((class condition-class
))
566 (aver (slot-value class
'finalized-p
))
569 (defmethod compute-effective-slot-definition
570 ((class condition-class
) slot-name dslotds
)
571 (let ((slotd (call-next-method)))
572 (setf (slot-definition-reader-function slotd
)
574 (handler-case (condition-reader-function x slot-name
)
575 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
576 ;; is unbound; maybe it should be a CELL-ERROR of some
578 (error () (values (slot-unbound class x slot-name
))))))
579 (setf (slot-definition-writer-function slotd
)
581 (condition-writer-function x v slot-name
)))
582 (setf (slot-definition-boundp-function slotd
)
584 (multiple-value-bind (v c
)
585 (ignore-errors (condition-reader-function x slot-name
))
590 (defmethod compute-slots ((class condition-class
))
591 (mapcan (lambda (superclass)
592 (mapcar (lambda (dslotd)
593 (compute-effective-slot-definition
594 class
(slot-definition-name dslotd
) (list dslotd
)))
595 (class-direct-slots superclass
)))
596 (reverse (slot-value class
'%class-precedence-list
))))
598 (defmethod compute-slots :around
((class condition-class
))
599 (let ((eslotds (call-next-method)))
600 (mapc #'initialize-internal-slot-functions eslotds
)
603 (defmethod shared-initialize :after
604 ((slotd structure-slot-definition
) slot-names
&key
605 (allocation :instance
) allocation-class
)
606 (declare (ignore slot-names allocation-class
))
607 (unless (eq allocation
:instance
)
608 (error "Structure slots must have :INSTANCE allocation.")))
610 (defun make-structure-class-defstruct-form (name direct-slots include
)
611 (let* ((conc-name (format-symbol *package
* "~S structure class " name
))
612 (constructor (format-symbol *package
* "~Aconstructor" conc-name
))
613 (defstruct `(defstruct (,name
615 `((:include
,(class-name include
))))
617 (:conc-name
,conc-name
)
618 (:constructor
,constructor
())
620 ,@(mapcar (lambda (slot)
621 `(,(slot-definition-name slot
)
624 (reader-names (mapcar (lambda (slotd)
625 (list 'slot-accessor name
626 (slot-definition-name slotd
)
629 (writer-names (mapcar (lambda (slotd)
630 (list 'slot-accessor name
631 (slot-definition-name slotd
)
635 (mapcar (lambda (slotd reader-name
)
637 (slot-definition-defstruct-accessor-symbol
639 `(defun ,reader-name
(obj)
640 (declare (type ,name obj
))
642 direct-slots reader-names
))
644 (mapcar (lambda (slotd writer-name
)
646 (slot-definition-defstruct-accessor-symbol
648 `(defun ,writer-name
(nv obj
)
649 (declare (type ,name obj
))
650 (setf (,accessor obj
) nv
))))
651 direct-slots writer-names
))
655 ,@readers-init
,@writers-init
657 (values defstruct-form constructor reader-names writer-names
)))
659 (defun make-defstruct-allocation-function (class)
660 ;; FIXME: Why don't we go class->layout->info == dd
661 (let ((dd (find-defstruct-description (class-name class
))))
663 (sb-kernel::%make-instance-with-layout
664 (sb-kernel::compiler-layout-or-lose
(dd-name dd
))))))
666 (defmethod shared-initialize :after
667 ((class structure-class
) slot-names
&key
668 (direct-superclasses nil direct-superclasses-p
)
669 (direct-slots nil direct-slots-p
)
670 direct-default-initargs
672 (declare (ignore slot-names direct-default-initargs
))
673 (if direct-superclasses-p
674 (setf (slot-value class
'direct-superclasses
)
675 (or direct-superclasses
676 (setq direct-superclasses
677 (and (not (eq (class-name class
) 'structure-object
))
678 (list *the-class-structure-object
*)))))
679 (setq direct-superclasses
(slot-value class
'direct-superclasses
)))
680 (let* ((name (class-name class
))
681 (from-defclass-p (slot-value class
'from-defclass-p
))
682 (defstruct-p (or from-defclass-p
(not (structure-type-p name
)))))
684 (setf (slot-value class
'direct-slots
)
688 (let* ((slot-name (getf pl
:name
))
690 (format-symbol *package
*
691 "~S structure class ~A"
693 (setq pl
(list* :defstruct-accessor-symbol
695 (make-direct-slotd class pl
))
697 (setq direct-slots
(slot-value class
'direct-slots
)))
699 (let ((include (car (slot-value class
'direct-superclasses
))))
700 (multiple-value-bind (defstruct-form constructor reader-names writer-names
)
701 (make-structure-class-defstruct-form name direct-slots include
)
702 (unless (structure-type-p name
) (eval defstruct-form
))
703 (mapc (lambda (dslotd reader-name writer-name
)
704 (let* ((reader (gdefinition reader-name
))
705 (writer (when (fboundp writer-name
)
706 (gdefinition writer-name
))))
707 (setf (slot-value dslotd
'internal-reader-function
)
709 (setf (slot-value dslotd
'internal-writer-function
)
711 direct-slots reader-names writer-names
)
712 (setf (slot-value class
'defstruct-form
) defstruct-form
)
713 (setf (slot-value class
'defstruct-constructor
) constructor
)))
714 (setf (slot-value class
'defstruct-constructor
)
715 (make-defstruct-allocation-function class
)))
716 (add-direct-subclasses class direct-superclasses
)
717 (setf (slot-value class
'%class-precedence-list
)
718 (compute-class-precedence-list class
))
719 (setf (slot-value class
'cpl-available-p
) t
)
720 (let ((slots (compute-slots class
)))
721 (setf (slot-value class
'slots
) slots
)
722 (let* ((lclass (find-classoid (class-name class
)))
723 (layout (classoid-layout lclass
)))
724 (setf (classoid-pcl-class lclass
) class
)
725 (setf (slot-value class
'wrapper
) layout
)
726 (setf (layout-slot-table layout
) (make-slot-table class slots
))))
727 (setf (slot-value class
'finalized-p
) t
)
728 (add-slot-accessors class direct-slots definition-source
)))
730 (defmethod direct-slot-definition-class ((class structure-class
) &rest initargs
)
731 (declare (ignore initargs
))
732 (find-class 'structure-direct-slot-definition
))
734 (defmethod finalize-inheritance ((class structure-class
))
735 nil
) ; always finalized
737 (defun add-slot-accessors (class dslotds
&optional source-location
)
738 (fix-slot-accessors class dslotds
'add source-location
))
740 (defun remove-slot-accessors (class dslotds
)
741 (fix-slot-accessors class dslotds
'remove
))
743 (defun fix-slot-accessors (class dslotds add
/remove
&optional source-location
)
744 (flet ((fix (gfspec name r
/w doc
)
745 (let ((gf (cond ((eq add
/remove
'add
)
746 (or (find-generic-function gfspec nil
)
747 (ensure-generic-function
748 gfspec
:lambda-list
(case r
/w
750 (w '(new-value object
))))))
752 (find-generic-function gfspec nil
)))))
755 (r (if (eq add
/remove
'add
)
756 (add-reader-method class gf name doc source-location
)
757 (remove-reader-method class gf
)))
758 (w (if (eq add
/remove
'add
)
759 (add-writer-method class gf name doc source-location
)
760 (remove-writer-method class gf
))))))))
761 (dolist (dslotd dslotds
)
762 (let ((slot-name (slot-definition-name dslotd
))
763 (slot-doc (%slot-definition-documentation dslotd
)))
764 (dolist (r (slot-definition-readers dslotd
))
765 (fix r slot-name
'r slot-doc
))
766 (dolist (w (slot-definition-writers dslotd
))
767 (fix w slot-name
'w slot-doc
))))))
769 (defun add-direct-subclasses (class supers
)
770 (dolist (super supers
)
771 (unless (memq class
(class-direct-subclasses class
))
772 (add-direct-subclass super class
))))
774 (defmethod finalize-inheritance ((class std-class
))
775 (update-class class t
))
777 (defmethod finalize-inheritance ((class forward-referenced-class
))
778 ;; FIXME: should we not be thinking a bit about what kinds of error
779 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
780 ;; possibly a forward-referenced-class-error, though that's
781 ;; difficult given e.g. class precedence list calculations...
783 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
788 (defun class-has-a-forward-referenced-superclass-p (class)
789 (or (forward-referenced-class-p class
)
790 (some #'class-has-a-forward-referenced-superclass-p
791 (class-direct-superclasses class
))))
793 ;;; This is called by :after shared-initialize whenever a class is initialized
794 ;;; or reinitialized. The class may or may not be finalized.
795 (defun update-class (class finalizep
)
796 (without-package-locks
797 (when (or finalizep
(class-finalized-p class
))
798 (update-cpl class
(compute-class-precedence-list class
))
799 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
801 (update-slots class
(compute-slots class
))
802 (update-gfs-of-class class
)
803 (update-initargs class
(compute-default-initargs class
))
804 (update-ctors 'finalize-inheritance
:class class
))
805 (dolist (sub (class-direct-subclasses class
))
806 (update-class sub nil
))))
808 (define-condition cpl-protocol-violation
(reference-condition error
)
809 ((class :initarg
:class
:reader cpl-protocol-violation-class
)
810 (cpl :initarg
:cpl
:reader cpl-protocol-violation-cpl
))
811 (:default-initargs
:references
(list '(:sbcl
:node
"Metaobject Protocol")))
814 (format s
"~@<Protocol violation: the ~S class ~S ~
815 ~:[has~;does not have~] the class ~S in its ~
816 class precedence list: ~S.~@:>"
817 (class-name (class-of (cpl-protocol-violation-class c
)))
818 (cpl-protocol-violation-class c
)
819 (eq (class-of (cpl-protocol-violation-class c
))
820 *the-class-funcallable-standard-class
*)
821 (find-class 'function
)
822 (cpl-protocol-violation-cpl c
)))))
824 (defun update-cpl (class cpl
)
825 (when (eq (class-of class
) *the-class-standard-class
*)
826 (when (find (find-class 'function
) cpl
)
827 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
828 (when (eq (class-of class
) *the-class-funcallable-standard-class
*)
829 (unless (find (find-class 'function
) cpl
)
830 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
831 (if (class-finalized-p class
)
832 (unless (and (equal (class-precedence-list class
) cpl
)
834 (when (position :class
(class-direct-slots c
)
835 :key
#'slot-definition-allocation
)
837 ;; comment from the old CMU CL sources:
838 ;; Need to have the cpl setup before update-lisp-class-layout
839 ;; is called on CMU CL.
840 (setf (slot-value class
'%class-precedence-list
) cpl
)
841 (setf (slot-value class
'cpl-available-p
) t
)
842 (force-cache-flushes class
))
844 (setf (slot-value class
'%class-precedence-list
) cpl
)
845 (setf (slot-value class
'cpl-available-p
) t
)))
846 (update-class-can-precede-p cpl
))
848 (defun update-class-can-precede-p (cpl)
850 (let ((first (car cpl
)))
851 (dolist (c (cdr cpl
))
852 (pushnew c
(slot-value first
'can-precede-list
))))
853 (update-class-can-precede-p (cdr cpl
))))
855 (defun class-can-precede-p (class1 class2
)
856 (member class2
(class-can-precede-list class1
)))
858 (defun update-slots (class eslotds
)
859 (let ((instance-slots ())
861 (dolist (eslotd eslotds
)
862 (let ((alloc (slot-definition-allocation eslotd
)))
864 (:instance
(push eslotd instance-slots
))
865 (:class
(push eslotd class-slots
)))))
867 ;; If there is a change in the shape of the instances then the
868 ;; old class is now obsolete.
869 (let* ((nlayout (mapcar #'slot-definition-name
870 (sort instance-slots
#'<
871 :key
#'slot-definition-location
)))
872 (nslots (length nlayout
))
873 (nwrapper-class-slots (compute-class-slots class-slots
))
874 (owrapper (when (class-finalized-p class
)
875 (class-wrapper class
)))
876 (olayout (when owrapper
877 (wrapper-instance-slots-layout owrapper
)))
878 (owrapper-class-slots (and owrapper
(wrapper-class-slots owrapper
)))
880 (cond ((null owrapper
)
881 (make-wrapper nslots class
))
882 ((and (equal nlayout olayout
)
884 (loop for o in owrapper-class-slots
885 for n in nwrapper-class-slots
886 do
(unless (eq (car o
) (car n
)) (return t
)))))
889 ;; This will initialize the new wrapper to have the
890 ;; same state as the old wrapper. We will then have
891 ;; to change that. This may seem like wasted work
892 ;; (and it is), but the spec requires that we call
893 ;; MAKE-INSTANCES-OBSOLETE.
894 (make-instances-obsolete class
)
895 (class-wrapper class
)))))
897 (update-lisp-class-layout class nwrapper
)
898 (setf (slot-value class
'slots
) eslotds
899 (wrapper-slot-table nwrapper
) (make-slot-table class eslotds
)
900 (wrapper-instance-slots-layout nwrapper
) nlayout
901 (wrapper-class-slots nwrapper
) nwrapper-class-slots
902 (wrapper-length nwrapper
) nslots
903 (slot-value class
'wrapper
) nwrapper
)
904 (do* ((slots (slot-value class
'slots
) (cdr slots
))
909 "~@<slot names with the same SYMBOL-NAME but ~
910 different SYMBOL-PACKAGE (possible package problem) ~
911 for class ~S:~4I~@:_~<~@{~S~^~:@_~}~:>~@:>"
913 (let* ((slot (car slots
))
914 (oslots (remove (slot-definition-name slot
) (cdr slots
)
916 :key
#'slot-definition-name
)))
918 (pushnew (cons (slot-definition-name slot
)
919 (mapcar #'slot-definition-name oslots
))
921 :test
#'string
= :key
#'car
))))
922 (setf (slot-value class
'finalized-p
) t
)
923 (unless (eq owrapper nwrapper
)
924 (maybe-update-standard-class-locations class
)))))
926 (defun compute-class-slots (eslotds)
928 (dolist (eslotd eslotds
(nreverse collect
))
929 (let ((cell (assoc (slot-definition-name eslotd
)
931 (slot-definition-allocation-class eslotd
)))))
933 (push cell collect
)))))
935 (defun update-gfs-of-class (class)
936 (when (and (class-finalized-p class
)
937 (let ((cpl (class-precedence-list class
)))
938 (or (member *the-class-slot-class
* cpl
)
939 (member *the-class-standard-effective-slot-definition
*
941 (let ((gf-table (make-hash-table :test
'eq
)))
942 (labels ((collect-gfs (class)
943 (dolist (gf (specializer-direct-generic-functions class
))
944 (setf (gethash gf gf-table
) t
))
945 (mapc #'collect-gfs
(class-direct-superclasses class
))))
947 (maphash (lambda (gf ignore
)
948 (declare (ignore ignore
))
949 (update-gf-dfun class gf
))
952 (defun update-initargs (class inits
)
953 (setf (plist-value class
'default-initargs
) inits
))
955 (defmethod compute-default-initargs ((class slot-class
))
956 (let ((initargs (loop for c in
(class-precedence-list class
)
957 append
(class-direct-default-initargs c
))))
958 (delete-duplicates initargs
:test
#'eq
:key
#'car
:from-end t
)))
960 ;;;; protocols for constructing direct and effective slot definitions
962 (defmethod direct-slot-definition-class ((class std-class
) &rest initargs
)
963 (declare (ignore initargs
))
964 (find-class 'standard-direct-slot-definition
))
966 (defun make-direct-slotd (class initargs
)
967 (apply #'make-instance
968 (apply #'direct-slot-definition-class class initargs
)
972 ;;; I (CSR) am not sure, but I believe that the particular order of
973 ;;; slots is quite important: it is ideal to attempt to have a
974 ;;; constant slot location for the same notional slots as much as
975 ;;; possible, so that clever discriminating functions (ONE-INDEX et
976 ;;; al.) have a chance of working. The below at least walks through
977 ;;; the slots predictably, but maybe it would be good to compute some
978 ;;; kind of optimal slot layout by looking at locations of slots in
980 (defun std-compute-slots (class)
981 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
982 ;; for each different slot name we find in our superclasses. Each
983 ;; call receives the class and a list of the dslotds with that name.
984 ;; The list is in most-specific-first order.
985 (let ((name-dslotds-alist ()))
986 (dolist (c (reverse (class-precedence-list class
)))
987 (dolist (slot (class-direct-slots c
))
988 (let* ((name (slot-definition-name slot
))
989 (entry (assq name name-dslotds-alist
)))
991 (push slot
(cdr entry
))
992 (push (list name slot
) name-dslotds-alist
)))))
993 (mapcar (lambda (direct)
994 (compute-effective-slot-definition class
997 (nreverse name-dslotds-alist
))))
999 (defmethod compute-slots ((class standard-class
))
1000 (std-compute-slots class
))
1001 (defmethod compute-slots ((class funcallable-standard-class
))
1002 (std-compute-slots class
))
1004 (defun std-compute-slots-around (class eslotds
)
1005 (let ((location -
1))
1006 (dolist (eslotd eslotds eslotds
)
1007 (setf (slot-definition-location eslotd
)
1008 (case (slot-definition-allocation eslotd
)
1012 (let* ((name (slot-definition-name eslotd
))
1015 (slot-definition-allocation-class eslotd
)
1016 ;; we get here if the user adds an extra slot
1018 (setf (slot-definition-allocation-class eslotd
)
1020 ;; which raises the question of what we should
1021 ;; do if we find that said user has added a slot
1022 ;; with the same name as another slot...
1023 (cell (or (assq name
(class-slot-cells from-class
))
1024 (let ((c (cons name
+slot-unbound
+)))
1025 (push c
(class-slot-cells from-class
))
1028 (if (eq +slot-unbound
+ (cdr cell
))
1029 ;; We may have inherited an initfunction
1030 (let ((initfun (slot-definition-initfunction eslotd
)))
1032 (rplacd cell
(funcall initfun
))
1035 (unless (slot-definition-class eslotd
)
1036 (setf (slot-definition-class eslotd
) class
))
1037 (initialize-internal-slot-functions eslotd
))))
1039 (defmethod compute-slots :around
((class standard-class
))
1040 (let ((eslotds (call-next-method)))
1041 (std-compute-slots-around class eslotds
)))
1042 (defmethod compute-slots :around
((class funcallable-standard-class
))
1043 (let ((eslotds (call-next-method)))
1044 (std-compute-slots-around class eslotds
)))
1046 (defmethod compute-slots ((class structure-class
))
1047 (mapcan (lambda (superclass)
1048 (mapcar (lambda (dslotd)
1049 (compute-effective-slot-definition
1051 (slot-definition-name dslotd
)
1053 (class-direct-slots superclass
)))
1054 (reverse (slot-value class
'%class-precedence-list
))))
1056 (defmethod compute-slots :around
((class structure-class
))
1057 (let ((eslotds (call-next-method)))
1058 (mapc #'initialize-internal-slot-functions eslotds
)
1061 (defmethod compute-effective-slot-definition ((class slot-class
) name dslotds
)
1062 (declare (ignore name
))
1063 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds
))
1064 (class (apply #'effective-slot-definition-class class initargs
)))
1065 (apply #'make-instance class initargs
)))
1067 (defmethod effective-slot-definition-class ((class std-class
) &rest initargs
)
1068 (declare (ignore initargs
))
1069 (find-class 'standard-effective-slot-definition
))
1071 (defmethod effective-slot-definition-class ((class structure-class
) &rest initargs
)
1072 (declare (ignore initargs
))
1073 (find-class 'structure-effective-slot-definition
))
1075 (defmethod compute-effective-slot-definition-initargs
1076 ((class slot-class
) direct-slotds
)
1082 (allocation-class nil
)
1084 (type-check-function nil
)
1086 (documentationp nil
)
1091 (dolist (slotd direct-slotds
)
1094 (setq name
(slot-definition-name slotd
)
1097 (when (slot-definition-initfunction slotd
)
1098 (setq initform
(slot-definition-initform slotd
)
1099 initfunction
(slot-definition-initfunction slotd
)
1101 (unless documentationp
1102 (when (%slot-definition-documentation slotd
)
1103 (setq documentation
(%slot-definition-documentation slotd
)
1106 (setq allocation
(slot-definition-allocation slotd
)
1107 allocation-class
(slot-definition-class slotd
)
1109 (setq initargs
(append (slot-definition-initargs slotd
) initargs
))
1110 (let ((fun (slot-definition-type-check-function slotd
)))
1112 (setf type-check-function
1113 (if type-check-function
1114 (let ((old-function type-check-function
))
1116 (funcall old-function value
)
1117 (funcall fun value
)))
1119 (let ((slotd-type (slot-definition-type slotd
)))
1121 ((eq type t
) slotd-type
)
1122 ;; This pairwise type intersection is perhaps a
1123 ;; little inefficient and inelegant, but it's
1124 ;; unlikely to lie on the critical path. Shout
1125 ;; if I'm wrong. -- CSR, 2005-11-24
1127 (specifier-type `(and ,type
,slotd-type
)))))))))
1130 :initfunction initfunction
1132 :allocation allocation
1133 :allocation-class allocation-class
1135 'type-check-function type-check-function
1137 :documentation documentation
)))
1139 (defmethod compute-effective-slot-definition-initargs :around
1140 ((class structure-class
) direct-slotds
)
1141 (let ((slotd (car direct-slotds
)))
1142 (list* :defstruct-accessor-symbol
1143 (slot-definition-defstruct-accessor-symbol slotd
)
1144 :internal-reader-function
1145 (slot-definition-internal-reader-function slotd
)
1146 :internal-writer-function
1147 (slot-definition-internal-writer-function slotd
)
1148 (call-next-method))))
1150 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1151 ;;; to make the method object. They have to use make-a-method which
1152 ;;; is a specially bootstrapped mechanism for making standard methods.
1153 (defmethod reader-method-class ((class slot-class
) direct-slot
&rest initargs
)
1154 (declare (ignore direct-slot initargs
))
1155 (find-class 'standard-reader-method
))
1157 (defmethod add-reader-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1158 (add-method generic-function
1159 (make-a-method 'standard-reader-method
1161 (list (or (class-name class
) 'object
))
1163 (make-reader-method-function class slot-name
)
1164 (or slot-documentation
"automatically generated reader method")
1165 :slot-name slot-name
1167 :method-class-function
#'reader-method-class
1168 :definition-source source-location
)))
1170 (defmethod writer-method-class ((class slot-class
) direct-slot
&rest initargs
)
1171 (declare (ignore direct-slot initargs
))
1172 (find-class 'standard-writer-method
))
1174 (defmethod add-writer-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1175 (add-method generic-function
1176 (make-a-method 'standard-writer-method
1178 (list 'new-value
(or (class-name class
) 'object
))
1179 (list *the-class-t
* class
)
1180 (make-writer-method-function class slot-name
)
1181 (or slot-documentation
"automatically generated writer method")
1182 :slot-name slot-name
1184 :method-class-function
#'writer-method-class
1185 :definition-source source-location
)))
1187 (defmethod add-boundp-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1188 (add-method generic-function
1189 (make-a-method (constantly (find-class 'standard-boundp-method
))
1192 (list (or (class-name class
) 'object
))
1194 (make-boundp-method-function class slot-name
)
1195 (or slot-documentation
"automatically generated boundp method")
1196 :slot-name slot-name
1197 :definition-source source-location
)))
1199 (defmethod remove-reader-method ((class slot-class
) generic-function
)
1200 (let ((method (get-method generic-function
() (list class
) nil
)))
1201 (when method
(remove-method generic-function method
))))
1203 (defmethod remove-writer-method ((class slot-class
) generic-function
)
1205 (get-method generic-function
() (list *the-class-t
* class
) nil
)))
1206 (when method
(remove-method generic-function method
))))
1208 (defmethod remove-boundp-method ((class slot-class
) generic-function
)
1209 (let ((method (get-method generic-function
() (list class
) nil
)))
1210 (when method
(remove-method generic-function method
))))
1212 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1213 ;;; function are NOT part of the standard protocol. They are however
1214 ;;; useful; PCL makes use of them internally and documents them for
1215 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1217 ;;; *** This needs work to make type testing by the writer functions which
1218 ;;; *** do type testing faster. The idea would be to have one constructor
1219 ;;; *** for each possible type test.
1221 ;;; *** There is a subtle bug here which is going to have to be fixed.
1222 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1223 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1224 ;;; *** defined for this metaclass a chance to run.
1226 (defmethod make-reader-method-function ((class slot-class
) slot-name
)
1227 (make-std-reader-method-function class slot-name
))
1229 (defmethod make-writer-method-function ((class slot-class
) slot-name
)
1230 (make-std-writer-method-function class slot-name
))
1232 (defmethod make-boundp-method-function ((class slot-class
) slot-name
)
1233 (make-std-boundp-method-function class slot-name
))
1235 (defmethod compatible-meta-class-change-p (class proto-new-class
)
1236 (eq (class-of class
) (class-of proto-new-class
)))
1238 (defmethod validate-superclass ((class class
) (superclass class
))
1239 (or (eq superclass
*the-class-t
*)
1240 (eq (class-of class
) (class-of superclass
))
1241 (and (eq (class-of superclass
) *the-class-standard-class
*)
1242 (eq (class-of class
) *the-class-funcallable-standard-class
*))
1243 (and (eq (class-of superclass
) *the-class-funcallable-standard-class
*)
1244 (eq (class-of class
) *the-class-standard-class
*))))
1246 ;;; What this does depends on which of the four possible values of
1247 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1248 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1249 ;;; nothing to do, as the new wrapper has already been created. If
1250 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1251 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1252 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1254 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1255 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1256 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1257 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1258 ;;; obsolete the wrapper.
1260 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1261 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1262 ;;; :UNINITIALIZED)))
1264 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1265 (defun force-cache-flushes (class)
1266 (let* ((owrapper (class-wrapper class
)))
1267 ;; We only need to do something if the wrapper is still valid. If
1268 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1269 ;; both of those will already be doing what we want. In
1270 ;; particular, we must be sure we never change an OBSOLETE into a
1271 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1272 (when (or (not (invalid-wrapper-p owrapper
))
1273 ;; KLUDGE: despite the observations above, this remains
1274 ;; a violation of locality or what might be considered
1275 ;; good style. There has to be a better way! -- CSR,
1277 (eq (layout-invalid owrapper
) t
))
1278 (let ((nwrapper (make-wrapper (layout-length owrapper
)
1280 (setf (wrapper-instance-slots-layout nwrapper
)
1281 (wrapper-instance-slots-layout owrapper
))
1282 (setf (wrapper-class-slots nwrapper
)
1283 (wrapper-class-slots owrapper
))
1284 (setf (wrapper-slot-table nwrapper
)
1285 (wrapper-slot-table owrapper
))
1287 (update-lisp-class-layout class nwrapper
)
1288 (setf (slot-value class
'wrapper
) nwrapper
)
1289 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1291 (if (find-if (lambda (x)
1292 (and (consp x
) (eq :obsolete
(car x
))))
1293 (layout-inherits owrapper
)
1294 :key
#'layout-invalid
)
1295 (invalidate-wrapper owrapper
:obsolete nwrapper
)
1296 (invalidate-wrapper owrapper
:flush nwrapper
)))))))
1298 (defun flush-cache-trap (owrapper nwrapper instance
)
1299 (declare (ignore owrapper
))
1300 (set-wrapper instance nwrapper
))
1302 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1303 ;;; the next access to the instance (as defined in 88-002R) to trap
1304 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1305 (defmethod make-instances-obsolete ((class std-class
))
1306 (let* ((owrapper (class-wrapper class
))
1307 (nwrapper (make-wrapper (layout-length owrapper
)
1309 (unless (class-finalized-p class
)
1310 (if (class-has-a-forward-referenced-superclass-p class
)
1311 (return-from make-instances-obsolete class
)
1312 (update-cpl class
(compute-class-precedence-list class
))))
1313 (setf (wrapper-instance-slots-layout nwrapper
)
1314 (wrapper-instance-slots-layout owrapper
))
1315 (setf (wrapper-class-slots nwrapper
)
1316 (wrapper-class-slots owrapper
))
1317 (setf (wrapper-slot-table nwrapper
)
1318 (wrapper-slot-table owrapper
))
1320 (update-lisp-class-layout class nwrapper
)
1321 (setf (slot-value class
'wrapper
) nwrapper
)
1322 (invalidate-wrapper owrapper
:obsolete nwrapper
)
1325 (defmethod make-instances-obsolete ((class symbol
))
1326 (make-instances-obsolete (find-class class
))
1327 ;; ANSI wants the class name when called with a symbol.
1330 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1331 ;;; see an obsolete instance. The times when it is called are:
1332 ;;; - when the instance is involved in method lookup
1333 ;;; - when attempting to access a slot of an instance
1335 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1336 ;;; instance access macros.
1338 ;;; Of course these times when it is called are an internal
1339 ;;; implementation detail of PCL and are not part of the documented
1340 ;;; description of when the obsolete instance update happens. The
1341 ;;; documented description is as it appears in 88-002R.
1343 ;;; This has to return the new wrapper, so it counts on all the
1344 ;;; methods on obsolete-instance-trap-internal to return the new
1345 ;;; wrapper. It also does a little internal error checking to make
1346 ;;; sure that the traps are only happening when they should, and that
1347 ;;; the trap methods are computing appropriate new wrappers.
1349 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1350 ;;; after a structure is redefined. In most cases,
1351 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1352 ;;; so it must signal an error. The hard part of this is that the
1353 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1354 ;;; called again, so in that case, we have to return some reasonable
1355 ;;; wrapper, instead.
1357 (defvar *in-obsolete-instance-trap
* nil
)
1358 (defvar *the-wrapper-of-structure-object
*
1359 (class-wrapper (find-class 'structure-object
)))
1361 (define-condition obsolete-structure
(error)
1362 ((datum :reader obsolete-structure-datum
:initarg
:datum
))
1364 (lambda (condition stream
)
1365 ;; Don't try to print the structure, since it probably won't work.
1367 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1368 (type-of (obsolete-structure-datum condition
))))))
1370 (defun obsolete-instance-trap (owrapper nwrapper instance
)
1371 (if (not (layout-for-std-class-p owrapper
))
1372 (if *in-obsolete-instance-trap
*
1373 *the-wrapper-of-structure-object
*
1374 (let ((*in-obsolete-instance-trap
* t
))
1375 (error 'obsolete-structure
:datum instance
)))
1376 (let* ((class (wrapper-class* nwrapper
))
1377 (copy (allocate-instance class
)) ;??? allocate-instance ???
1378 (olayout (wrapper-instance-slots-layout owrapper
))
1379 (nlayout (wrapper-instance-slots-layout nwrapper
))
1380 (oslots (get-slots instance
))
1381 (nslots (get-slots copy
))
1382 (oclass-slots (wrapper-class-slots owrapper
))
1387 ;; local --> local transfer value
1388 ;; local --> shared discard value, discard slot
1389 ;; local --> -- discard slot
1390 ;; shared --> local transfer value
1391 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1392 ;; shared --> -- discard value
1393 ;; -- --> local add slot
1396 ;; Go through all the old local slots.
1398 (dolist (name olayout
)
1399 (let ((npos (posq name nlayout
)))
1401 (setf (clos-slots-ref nslots npos
)
1402 (clos-slots-ref oslots opos
))
1404 (push name discarded
)
1405 (unless (eq (clos-slots-ref oslots opos
) +slot-unbound
+)
1406 (setf (getf plist name
) (clos-slots-ref oslots opos
))))))
1409 ;; Go through all the old shared slots.
1410 (dolist (oclass-slot-and-val oclass-slots
)
1411 (let ((name (car oclass-slot-and-val
))
1412 (val (cdr oclass-slot-and-val
)))
1413 (let ((npos (posq name nlayout
)))
1415 (setf (clos-slots-ref nslots npos
) val
)))))
1417 ;; Go through all the new local slots to compute the added slots.
1418 (dolist (nlocal nlayout
)
1419 (unless (or (memq nlocal olayout
)
1420 (assq nlocal oclass-slots
))
1421 (push nlocal added
)))
1423 (swap-wrappers-and-slots instance copy
)
1425 (update-instance-for-redefined-class instance
1431 (defun change-class-internal (instance new-class initargs
)
1432 (let* ((old-class (class-of instance
))
1433 (copy (allocate-instance new-class
))
1434 (new-wrapper (get-wrapper copy
))
1435 (old-wrapper (class-wrapper old-class
))
1436 (old-layout (wrapper-instance-slots-layout old-wrapper
))
1437 (new-layout (wrapper-instance-slots-layout new-wrapper
))
1438 (old-slots (get-slots instance
))
1439 (new-slots (get-slots copy
))
1440 (old-class-slots (wrapper-class-slots old-wrapper
)))
1442 ;; "The values of local slots specified by both the class CTO and
1443 ;; CFROM are retained. If such a local slot was unbound, it
1444 ;; remains unbound."
1445 (let ((new-position 0))
1446 (dolist (new-slot new-layout
)
1447 (let ((old-position (posq new-slot old-layout
)))
1449 (setf (clos-slots-ref new-slots new-position
)
1450 (clos-slots-ref old-slots old-position
))))
1451 (incf new-position
)))
1453 ;; "The values of slots specified as shared in the class CFROM and
1454 ;; as local in the class CTO are retained."
1455 (dolist (slot-and-val old-class-slots
)
1456 (let ((position (posq (car slot-and-val
) new-layout
)))
1458 (setf (clos-slots-ref new-slots position
) (cdr slot-and-val
)))))
1460 ;; Make the copy point to the old instance's storage, and make the
1461 ;; old instance point to the new storage.
1462 (swap-wrappers-and-slots instance copy
)
1464 (apply #'update-instance-for-different-class copy instance initargs
)
1467 (defmethod change-class ((instance standard-object
) (new-class standard-class
)
1469 (unless (class-finalized-p new-class
)
1470 (finalize-inheritance new-class
))
1471 (let ((cpl (class-precedence-list new-class
)))
1475 `(when (eq class
(find-class ',class-name
))
1476 (error 'metaobject-initialization-violation
1477 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1478 :format-arguments
(list 'change-class
',class-name
)
1479 :references
(list '(:amop
:initialization
,class-name
))))))
1481 (frob generic-function
)
1483 (frob slot-definition
))))
1484 (change-class-internal instance new-class initargs
))
1486 (defmethod change-class ((instance forward-referenced-class
)
1487 (new-class standard-class
) &rest initargs
)
1488 (let ((cpl (class-precedence-list new-class
)))
1490 (error 'metaobject-initialization-violation
1492 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1494 (list 'change-class
'forward-referenced-class
'class
)
1496 (list '(:amop
:generic-function ensure-class-using-class
)
1497 '(:amop
:initialization class
))))
1498 (when (eq class
(find-class 'class
))
1500 (change-class-internal instance new-class initargs
))
1502 (defmethod change-class ((instance funcallable-standard-object
)
1503 (new-class funcallable-standard-class
)
1505 (let ((cpl (class-precedence-list new-class
)))
1509 `(when (eq class
(find-class ',class-name
))
1510 (error 'metaobject-initialization-violation
1511 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1512 :format-arguments
(list 'change-class
',class-name
)
1513 :references
(list '(:amop
:initialization
,class-name
))))))
1515 (frob generic-function
)
1517 (frob slot-definition
))))
1518 (change-class-internal instance new-class initargs
))
1520 (defmethod change-class ((instance standard-object
)
1521 (new-class funcallable-standard-class
)
1523 (declare (ignore initargs
))
1524 (error "You can't change the class of ~S to ~S~@
1525 because it isn't already an instance with metaclass ~S."
1526 instance new-class
'standard-class
))
1528 (defmethod change-class ((instance funcallable-standard-object
)
1529 (new-class standard-class
)
1531 (declare (ignore initargs
))
1532 (error "You can't change the class of ~S to ~S~@
1533 because it isn't already an instance with metaclass ~S."
1534 instance new-class
'funcallable-standard-class
))
1536 (defmethod change-class ((instance t
) (new-class-name symbol
) &rest initargs
)
1537 (apply #'change-class instance
(find-class new-class-name
) initargs
))
1539 ;;;; The metaclass BUILT-IN-CLASS
1541 ;;;; This metaclass is something of a weird creature. By this point, all
1542 ;;;; instances of it which will exist have been created, and no instance
1543 ;;;; is ever created by calling MAKE-INSTANCE.
1545 ;;;; But, there are other parts of the protocol we must follow and those
1546 ;;;; definitions appear here.
1548 (macrolet ((def (name args control
)
1549 `(defmethod ,name
,args
1550 (declare (ignore initargs
))
1551 (error 'metaobject-initialization-violation
1552 :format-control
,(format nil
"~@<~A~@:>" control
)
1553 :format-arguments
(list ',name
)
1554 :references
(list '(:amop
:initialization
"Class"))))))
1555 (def initialize-instance
((class built-in-class
) &rest initargs
)
1556 "Cannot ~S an instance of BUILT-IN-CLASS.")
1557 (def reinitialize-instance
((class built-in-class
) &rest initargs
)
1558 "Cannot ~S an instance of BUILT-IN-CLASS."))
1560 (macrolet ((def (name)
1561 `(defmethod ,name
((class built-in-class
)) nil
)))
1562 (def class-direct-slots
)
1564 (def class-direct-default-initargs
)
1565 (def class-default-initargs
))
1567 (defmethod validate-superclass ((c class
) (s built-in-class
))
1568 (or (eq s
*the-class-t
*) (eq s
*the-class-stream
*)
1569 ;; FIXME: bad things happen if someone tries to mix in both
1570 ;; FILE-STREAM and STRING-STREAM (as they have the same
1571 ;; layout-depthoid). Is there any way we can provide a useful
1572 ;; error message? -- CSR, 2005-05-03
1573 (eq s
*the-class-file-stream
*) (eq s
*the-class-string-stream
*)
1574 ;; This probably shouldn't be mixed in with certain other
1575 ;; classes, too, but it seems to work both with STANDARD-OBJECT
1576 ;; and FUNCALLABLE-STANDARD-OBJECT
1577 (eq s
*the-class-sequence
*)))
1579 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1580 (defmethod class-direct-slots ((class forward-referenced-class
)) ())
1581 (defmethod class-direct-default-initargs ((class forward-referenced-class
)) ())
1582 (macrolet ((def (method)
1583 `(defmethod ,method
((class forward-referenced-class
))
1584 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1586 (def class-default-initargs
)
1587 (def class-precedence-list
)
1590 (defmethod validate-superclass ((c slot-class
)
1591 (f forward-referenced-class
))
1594 (defmethod add-dependent ((metaobject dependent-update-mixin
) dependent
)
1595 (pushnew dependent
(plist-value metaobject
'dependents
)))
1597 (defmethod remove-dependent ((metaobject dependent-update-mixin
) dependent
)
1598 (setf (plist-value metaobject
'dependents
)
1599 (delete dependent
(plist-value metaobject
'dependents
))))
1601 (defmethod map-dependents ((metaobject dependent-update-mixin
) function
)
1602 (dolist (dependent (plist-value metaobject
'dependents
))
1603 (funcall function dependent
)))