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
:test
#'eq
)
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
) :test
#'eq
)))
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.
233 ;;; These tables are shared between threads, so they need to be synchronized.
234 (defvar *eql-specializer-methods
* (make-hash-table :test
'eql
:synchronized t
))
235 (defvar *class-eq-specializer-methods
* (make-hash-table :test
'eq
:synchronized t
))
237 (defmethod specializer-method-table ((specializer eql-specializer
))
238 *eql-specializer-methods
*)
240 (defmethod specializer-method-table ((specializer class-eq-specializer
))
241 *class-eq-specializer-methods
*)
243 (defmethod add-direct-method ((specializer specializer-with-object
)
245 (let* ((object (specializer-object specializer
))
246 (table (specializer-method-table specializer
))
247 (entry (gethash object table
)))
250 (setf (gethash object table
) (cons nil nil
))))
251 ;; We need to first smash the CDR, because a parallel read may
252 ;; be in progress, and because if an interrupt catches us we
253 ;; need to have a consistent state.
255 (car entry
) (adjoin method
(car entry
) :test
#'eq
))
258 (defmethod remove-direct-method ((specializer specializer-with-object
)
260 (let* ((object (specializer-object specializer
))
261 (entry (gethash object
(specializer-method-table specializer
))))
263 ;; We need to first smash the CDR, because a parallel read may
264 ;; be in progress, and because if an interrupt catches us we
265 ;; need to have a consistent state.
267 (car entry
) (remove method
(car entry
))))
270 (defmethod specializer-direct-methods ((specializer specializer-with-object
))
271 (car (gethash (specializer-object specializer
)
272 (specializer-method-table specializer
))))
274 (defmethod specializer-direct-generic-functions ((specializer
275 specializer-with-object
))
276 (let* ((object (specializer-object specializer
))
277 (entry (gethash object
(specializer-method-table specializer
))))
280 (sb-thread::with-spinlock
(*specializer-lock
*)
283 (dolist (m (car entry
))
284 (pushnew (method-generic-function m
) collect
:test
#'eq
))
285 (nreverse collect
))))))))
287 (defun map-specializers (function)
288 (map-all-classes (lambda (class)
289 (funcall function
(class-eq-specializer class
))
290 (funcall function class
)))
291 (maphash (lambda (object methods
)
292 (declare (ignore methods
))
293 (intern-eql-specializer object
))
294 *eql-specializer-methods
*)
295 (maphash (lambda (object specl
)
296 (declare (ignore object
))
297 (funcall function specl
))
298 *eql-specializer-table
*)
301 (defun map-all-generic-functions (function)
302 (let ((all-generic-functions (make-hash-table :test
'eq
)))
303 (map-specializers (lambda (specl)
304 (dolist (gf (specializer-direct-generic-functions
306 (unless (gethash gf all-generic-functions
)
307 (setf (gethash gf all-generic-functions
) t
)
308 (funcall function gf
))))))
311 (defmethod shared-initialize :after
((specl class-eq-specializer
)
314 (declare (ignore slot-names
))
315 (setf (slot-value specl
'%type
) `(class-eq ,(specializer-class specl
))))
317 (defmethod shared-initialize :after
((specl eql-specializer
) slot-names
&key
)
318 (declare (ignore slot-names
))
319 (setf (slot-value specl
'%type
)
320 `(eql ,(specializer-object specl
)))
321 (setf (info :type
:translator specl
)
322 (constantly (make-member-type :members
(list (specializer-object specl
))))))
324 (defun real-load-defclass (name metaclass-name supers slots other
325 readers writers slot-names source-location safe-p
)
326 (with-single-package-locked-error (:symbol name
"defining ~S as a class")
327 (%compiler-defclass name readers writers slot-names
)
328 (let ((res (apply #'ensure-class name
:metaclass metaclass-name
329 :direct-superclasses supers
331 :definition-source source-location
336 (setf (gdefinition 'load-defclass
) #'real-load-defclass
)
338 (defun ensure-class (name &rest args
)
339 (apply #'ensure-class-using-class
340 (let ((class (find-class name nil
)))
341 (when (and class
(eq name
(class-name class
)))
342 ;; NAME is the proper name of CLASS, so redefine it
347 (defmethod ensure-class-using-class ((class null
) name
&rest args
&key
)
348 (multiple-value-bind (meta initargs
)
349 (frob-ensure-class-args args
)
350 (setf class
(apply #'make-instance meta
:name name initargs
))
351 (without-package-locks
352 (setf (find-class name
) class
))
353 (set-class-type-translation class name
)
356 (defmethod ensure-class-using-class ((class pcl-class
) name
&rest args
&key
)
357 (multiple-value-bind (meta initargs
)
358 (frob-ensure-class-args args
)
359 (unless (eq (class-of class
) meta
)
360 (apply #'change-class class meta initargs
))
361 (apply #'reinitialize-instance class initargs
)
362 (without-package-locks
363 (setf (find-class name
) class
))
364 (set-class-type-translation class name
)
367 (defun frob-ensure-class-args (args)
368 (let (metaclass metaclassp reversed-plist
)
369 (flet ((frob-superclass (s)
372 ((legal-class-name-p s
)
373 (or (find-class s nil
)
374 (ensure-class s
:metaclass
'forward-referenced-class
)))
375 (t (error "Not a class or a legal class name: ~S." s
)))))
376 (doplist (key val
) args
377 (cond ((eq key
:metaclass
)
379 (setf metaclass val metaclassp key
)))
381 (when (eq key
:direct-superclasses
)
382 (setf val
(mapcar #'frob-superclass val
)))
383 (setf reversed-plist
(list* val key reversed-plist
)))))
384 (values (cond (metaclassp
385 (if (classp metaclass
)
387 (find-class metaclass
)))
388 (t *the-class-standard-class
*))
389 (nreverse reversed-plist
)))))
391 (defmethod shared-initialize :after
392 ((class std-class
) slot-names
&key
393 (direct-superclasses nil direct-superclasses-p
)
394 (direct-slots nil direct-slots-p
)
395 (direct-default-initargs nil direct-default-initargs-p
)
397 (cond (direct-superclasses-p
398 (setq direct-superclasses
399 (or direct-superclasses
400 (list (if (funcallable-standard-class-p class
)
401 *the-class-funcallable-standard-object
*
402 *the-class-standard-object
*))))
403 (dolist (superclass direct-superclasses
)
404 (unless (validate-superclass class superclass
)
405 (error "~@<The class ~S was specified as a ~
406 super-class of the class ~S, ~
407 but the meta-classes ~S and ~S are incompatible. ~
408 Define a method for ~S to avoid this error.~@:>"
409 superclass class
(class-of superclass
) (class-of class
)
410 'validate-superclass
)))
411 (setf (slot-value class
'direct-superclasses
) direct-superclasses
))
413 (setq direct-superclasses
(slot-value class
'direct-superclasses
))))
416 (setf (slot-value class
'direct-slots
)
417 (mapcar (lambda (pl) (make-direct-slotd class pl
))
419 (slot-value class
'direct-slots
)))
420 (if direct-default-initargs-p
421 (setf (plist-value class
'direct-default-initargs
)
422 direct-default-initargs
)
423 (setq direct-default-initargs
424 (plist-value class
'direct-default-initargs
)))
425 (setf (plist-value class
'class-slot-cells
)
426 (let ((old-class-slot-cells (plist-value class
'class-slot-cells
))
428 (dolist (dslotd direct-slots
)
429 (when (eq :class
(slot-definition-allocation dslotd
))
431 (let* ((name (slot-definition-name dslotd
))
432 (old (assoc name old-class-slot-cells
)))
435 (member name slot-names
:test
#'eq
))
436 (let* ((initfunction (slot-definition-initfunction dslotd
))
437 (value (if initfunction
438 (funcall initfunction
)
440 (push (cons name value
) collect
))
441 (push old collect
)))))
443 (add-direct-subclasses class direct-superclasses
)
444 (if (class-finalized-p class
)
445 ;; required by AMOP, "Reinitialization of Class Metaobjects"
446 (finalize-inheritance class
)
447 (update-class class nil
))
448 (add-slot-accessors class direct-slots definition-source
)
449 (make-preliminary-layout class
))
451 (defmethod shared-initialize :after
((class forward-referenced-class
)
452 slot-names
&key
&allow-other-keys
)
453 (declare (ignore slot-names
))
454 (make-preliminary-layout class
))
456 (defvar *allow-forward-referenced-classes-in-cpl-p
* nil
)
458 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
459 ;;; make it known to the type system.
460 (defun make-preliminary-layout (class)
461 (flet ((compute-preliminary-cpl (root)
462 (let ((*allow-forward-referenced-classes-in-cpl-p
* t
))
463 (compute-class-precedence-list root
))))
464 (without-package-locks
465 (unless (class-finalized-p class
)
466 (let ((name (class-name class
)))
467 ;; KLUDGE: This is fairly horrible. We need to make a
468 ;; full-fledged CLASSOID here, not just tell the compiler that
469 ;; some class is forthcoming, because there are legitimate
470 ;; questions one can ask of the type system, implemented in
471 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
472 (let ((layout (make-wrapper 0 class
)))
473 (setf (slot-value class
'wrapper
) layout
)
474 (let ((cpl (compute-preliminary-cpl class
)))
475 (setf (layout-inherits layout
)
476 (order-layout-inherits
477 (map 'simple-vector
#'class-wrapper
478 (reverse (rest cpl
))))))
479 (register-layout layout
:invalidate t
)
480 (set-class-type-translation class
(layout-classoid layout
)))))
481 (mapc #'make-preliminary-layout
(class-direct-subclasses class
)))))
484 (defmethod shared-initialize :before
((class class
) slot-names
&key name
)
485 (declare (ignore slot-names name
))
486 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
487 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
488 (setf (slot-value class
'%type
) `(class ,class
))
489 (setf (slot-value class
'class-eq-specializer
)
490 (make-instance 'class-eq-specializer
:class class
)))
492 (defmethod reinitialize-instance :before
((class slot-class
) &key direct-superclasses
)
493 (dolist (old-super (set-difference (class-direct-superclasses class
) direct-superclasses
))
494 (remove-direct-subclass old-super class
))
495 (remove-slot-accessors class
(class-direct-slots class
)))
497 (defmethod reinitialize-instance :after
((class slot-class
)
500 (map-dependents class
502 (apply #'update-dependent class dependent initargs
))))
504 (defmethod reinitialize-instance :after
((class condition-class
) &key
)
505 (let* ((name (class-name class
))
506 (classoid (find-classoid name
))
507 (slots (condition-classoid-slots classoid
)))
508 ;; to balance the REMOVE-SLOT-ACCESSORS call in
509 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
511 (let ((slot-name (condition-slot-name slot
)))
512 (dolist (reader (condition-slot-readers slot
))
513 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
514 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
515 (sb-kernel::install-condition-slot-reader reader name slot-name
))
516 (dolist (writer (condition-slot-writers slot
))
517 (sb-kernel::install-condition-slot-writer writer name slot-name
))))))
519 (defmethod shared-initialize :after
((class condition-class
) slot-names
520 &key direct-slots direct-superclasses
)
521 (declare (ignore slot-names
))
522 (let ((classoid (find-classoid (slot-value class
'name
))))
523 (with-slots (wrapper %class-precedence-list cpl-available-p
524 prototype
(direct-supers direct-superclasses
))
526 (setf (slot-value class
'direct-slots
)
527 (mapcar (lambda (pl) (make-direct-slotd class pl
))
529 (setf (slot-value class
'finalized-p
) t
)
530 (setf (classoid-pcl-class classoid
) class
)
531 (setq direct-supers direct-superclasses
)
532 (setq wrapper
(classoid-layout classoid
))
533 (setq %class-precedence-list
(compute-class-precedence-list class
))
534 (setq cpl-available-p t
)
535 (add-direct-subclasses class direct-superclasses
)
536 (let ((slots (compute-slots class
)))
537 (setf (slot-value class
'slots
) slots
)
538 (setf (layout-slot-table wrapper
) (make-slot-table class slots
)))))
539 ;; Comment from Gerd's PCL, 2003-05-15:
541 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
542 ;; override condition accessors with generic functions. We do this
545 ;; ??? What does the above comment mean and why is it a good idea?
546 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
547 ;; comment) loses slot information in its condition classes:
548 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
549 ;; remove slot accessors but never put them back. I've added a
550 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
551 ;; was meant to happen? -- CSR, 2005-11-18
554 (defmethod direct-slot-definition-class ((class condition-class
)
556 (declare (ignore initargs
))
557 (find-class 'condition-direct-slot-definition
))
559 (defmethod effective-slot-definition-class ((class condition-class
)
561 (declare (ignore initargs
))
562 (find-class 'condition-effective-slot-definition
))
564 (defmethod finalize-inheritance ((class condition-class
))
565 (aver (slot-value class
'finalized-p
))
568 (defmethod compute-effective-slot-definition
569 ((class condition-class
) slot-name dslotds
)
570 (let ((slotd (call-next-method)))
571 (setf (slot-definition-reader-function slotd
)
573 (handler-case (condition-reader-function x slot-name
)
574 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
575 ;; is unbound; maybe it should be a CELL-ERROR of some
577 (error () (values (slot-unbound class x slot-name
))))))
578 (setf (slot-definition-writer-function slotd
)
580 (condition-writer-function x v slot-name
)))
581 (setf (slot-definition-boundp-function slotd
)
583 (multiple-value-bind (v c
)
584 (ignore-errors (condition-reader-function x slot-name
))
589 (defmethod compute-slots ((class condition-class
))
590 (mapcan (lambda (superclass)
591 (mapcar (lambda (dslotd)
592 (compute-effective-slot-definition
593 class
(slot-definition-name dslotd
) (list dslotd
)))
594 (class-direct-slots superclass
)))
595 (reverse (slot-value class
'%class-precedence-list
))))
597 (defmethod compute-slots :around
((class condition-class
))
598 (let ((eslotds (call-next-method)))
599 (mapc #'initialize-internal-slot-functions eslotds
)
602 (defmethod shared-initialize :after
603 ((slotd structure-slot-definition
) slot-names
&key
604 (allocation :instance
) allocation-class
)
605 (declare (ignore slot-names allocation-class
))
606 (unless (eq allocation
:instance
)
607 (error "Structure slots must have :INSTANCE allocation.")))
609 (defun make-structure-class-defstruct-form (name direct-slots include
)
610 (let* ((conc-name (format-symbol *package
* "~S structure class " name
))
611 (constructor (format-symbol *package
* "~Aconstructor" conc-name
))
612 (defstruct `(defstruct (,name
614 `((:include
,(class-name include
))))
616 (:conc-name
,conc-name
)
617 (:constructor
,constructor
())
619 ,@(mapcar (lambda (slot)
620 `(,(slot-definition-name slot
)
623 (reader-names (mapcar (lambda (slotd)
624 (list 'slot-accessor name
625 (slot-definition-name slotd
)
628 (writer-names (mapcar (lambda (slotd)
629 (list 'slot-accessor name
630 (slot-definition-name slotd
)
634 (mapcar (lambda (slotd reader-name
)
636 (slot-definition-defstruct-accessor-symbol
638 `(defun ,reader-name
(obj)
639 (declare (type ,name obj
))
641 direct-slots reader-names
))
643 (mapcar (lambda (slotd writer-name
)
645 (slot-definition-defstruct-accessor-symbol
647 `(defun ,writer-name
(nv obj
)
648 (declare (type ,name obj
))
649 (setf (,accessor obj
) nv
))))
650 direct-slots writer-names
))
654 ,@readers-init
,@writers-init
656 (values defstruct-form constructor reader-names writer-names
)))
658 (defun make-defstruct-allocation-function (name)
659 ;; FIXME: Why don't we go class->layout->info == dd
660 (let ((dd (find-defstruct-description name
)))
661 (%make-structure-instance-allocator dd nil
)))
663 (defmethod shared-initialize :after
664 ((class structure-class
) slot-names
&key
665 (direct-superclasses nil direct-superclasses-p
)
666 (direct-slots nil direct-slots-p
)
667 direct-default-initargs
669 (declare (ignore slot-names direct-default-initargs
))
670 (if direct-superclasses-p
671 (setf (slot-value class
'direct-superclasses
)
672 (or direct-superclasses
673 (setq direct-superclasses
674 (and (not (eq (slot-value class
'name
) 'structure-object
))
675 (list *the-class-structure-object
*)))))
676 (setq direct-superclasses
(slot-value class
'direct-superclasses
)))
677 (let* ((name (slot-value class
'name
))
678 (from-defclass-p (slot-value class
'from-defclass-p
))
679 (defstruct-p (or from-defclass-p
(not (structure-type-p name
)))))
681 (setf (slot-value class
'direct-slots
)
685 (let* ((slot-name (getf pl
:name
))
687 (format-symbol *package
*
688 "~S structure class ~A"
690 (setq pl
(list* :defstruct-accessor-symbol
692 (make-direct-slotd class pl
))
694 (setq direct-slots
(slot-value class
'direct-slots
)))
696 (let ((include (car (slot-value class
'direct-superclasses
))))
697 (multiple-value-bind (defstruct-form constructor reader-names writer-names
)
698 (make-structure-class-defstruct-form name direct-slots include
)
699 (unless (structure-type-p name
) (eval defstruct-form
))
700 (mapc (lambda (dslotd reader-name writer-name
)
701 (let* ((reader (gdefinition reader-name
))
702 (writer (when (fboundp writer-name
)
703 (gdefinition writer-name
))))
704 (setf (slot-value dslotd
'internal-reader-function
)
706 (setf (slot-value dslotd
'internal-writer-function
)
708 direct-slots reader-names writer-names
)
709 (setf (slot-value class
'defstruct-form
) defstruct-form
)
710 (setf (slot-value class
'defstruct-constructor
) constructor
)))
711 (setf (slot-value class
'defstruct-constructor
)
712 ;; KLUDGE: not class; in fixup.lisp, can't access slots
713 ;; outside methods yet.
714 (make-defstruct-allocation-function name
)))
715 (add-direct-subclasses class direct-superclasses
)
716 (setf (slot-value class
'%class-precedence-list
)
717 (compute-class-precedence-list class
))
718 (setf (slot-value class
'cpl-available-p
) t
)
719 (let ((slots (compute-slots class
)))
720 (setf (slot-value class
'slots
) slots
)
721 (let* ((lclass (find-classoid (slot-value class
'name
)))
722 (layout (classoid-layout lclass
)))
723 (setf (classoid-pcl-class lclass
) class
)
724 (setf (slot-value class
'wrapper
) layout
)
725 (setf (layout-slot-table layout
) (make-slot-table class slots
))))
726 (setf (slot-value class
'finalized-p
) t
)
727 (add-slot-accessors class direct-slots definition-source
)))
729 (defmethod direct-slot-definition-class ((class structure-class
) &rest initargs
)
730 (declare (ignore initargs
))
731 (find-class 'structure-direct-slot-definition
))
733 (defmethod finalize-inheritance ((class structure-class
))
734 nil
) ; always finalized
736 (defun add-slot-accessors (class dslotds
&optional source-location
)
737 (fix-slot-accessors class dslotds
'add source-location
))
739 (defun remove-slot-accessors (class dslotds
)
740 (fix-slot-accessors class dslotds
'remove
))
742 (defun fix-slot-accessors (class dslotds add
/remove
&optional source-location
)
743 (flet ((fix (gfspec name r
/w doc
)
744 (let ((gf (cond ((eq add
/remove
'add
)
745 (or (find-generic-function gfspec nil
)
746 (ensure-generic-function
747 gfspec
:lambda-list
(case r
/w
749 (w '(new-value object
))))))
751 (find-generic-function gfspec nil
)))))
754 (r (if (eq add
/remove
'add
)
755 (add-reader-method class gf name doc source-location
)
756 (remove-reader-method class gf
)))
757 (w (if (eq add
/remove
'add
)
758 (add-writer-method class gf name doc source-location
)
759 (remove-writer-method class gf
))))))))
760 (dolist (dslotd dslotds
)
761 (let ((slot-name (slot-definition-name dslotd
))
762 (slot-doc (%slot-definition-documentation dslotd
)))
763 (dolist (r (slot-definition-readers dslotd
))
764 (fix r slot-name
'r slot-doc
))
765 (dolist (w (slot-definition-writers dslotd
))
766 (fix w slot-name
'w slot-doc
))))))
768 (defun add-direct-subclasses (class supers
)
769 (dolist (super supers
)
770 (unless (memq class
(class-direct-subclasses class
))
771 (add-direct-subclass super class
))))
773 (defmethod finalize-inheritance ((class std-class
))
774 (update-class class t
))
776 (defmethod finalize-inheritance ((class forward-referenced-class
))
777 ;; FIXME: should we not be thinking a bit about what kinds of error
778 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
779 ;; possibly a forward-referenced-class-error, though that's
780 ;; difficult given e.g. class precedence list calculations...
782 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
787 (defun class-has-a-forward-referenced-superclass-p (class)
788 (or (forward-referenced-class-p class
)
789 (some #'class-has-a-forward-referenced-superclass-p
790 (class-direct-superclasses class
))))
792 ;;; This is called by :after shared-initialize whenever a class is initialized
793 ;;; or reinitialized. The class may or may not be finalized.
794 (defun update-class (class finalizep
)
795 (without-package-locks
796 (when (or finalizep
(class-finalized-p class
))
797 (update-cpl class
(compute-class-precedence-list class
))
798 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
800 (update-slots class
(compute-slots class
))
801 (update-gfs-of-class class
)
802 (update-initargs class
(compute-default-initargs class
))
803 (update-ctors 'finalize-inheritance
:class class
))
804 (dolist (sub (class-direct-subclasses class
))
805 (update-class sub nil
))))
807 (define-condition cpl-protocol-violation
(reference-condition error
)
808 ((class :initarg
:class
:reader cpl-protocol-violation-class
)
809 (cpl :initarg
:cpl
:reader cpl-protocol-violation-cpl
))
810 (:default-initargs
:references
(list '(:sbcl
:node
"Metaobject Protocol")))
813 (format s
"~@<Protocol violation: the ~S class ~S ~
814 ~:[has~;does not have~] the class ~S in its ~
815 class precedence list: ~S.~@:>"
816 (class-name (class-of (cpl-protocol-violation-class c
)))
817 (cpl-protocol-violation-class c
)
818 (eq (class-of (cpl-protocol-violation-class c
))
819 *the-class-funcallable-standard-class
*)
820 (find-class 'function
)
821 (cpl-protocol-violation-cpl c
)))))
823 (defun update-cpl (class cpl
)
824 (when (eq (class-of class
) *the-class-standard-class
*)
825 (when (find (find-class 'function
) cpl
)
826 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
827 (when (eq (class-of class
) *the-class-funcallable-standard-class
*)
828 (unless (find (find-class 'function
) cpl
)
829 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
830 (if (class-finalized-p class
)
831 (unless (and (equal (class-precedence-list class
) cpl
)
833 (when (position :class
(class-direct-slots c
)
834 :key
#'slot-definition-allocation
)
836 ;; comment from the old CMU CL sources:
837 ;; Need to have the cpl setup before update-lisp-class-layout
838 ;; is called on CMU CL.
839 (setf (slot-value class
'%class-precedence-list
) cpl
)
840 (setf (slot-value class
'cpl-available-p
) t
)
841 (force-cache-flushes class
))
843 (setf (slot-value class
'%class-precedence-list
) cpl
)
844 (setf (slot-value class
'cpl-available-p
) t
)))
845 (update-class-can-precede-p cpl
))
847 (defun update-class-can-precede-p (cpl)
849 (let ((first (car cpl
)))
850 (dolist (c (cdr cpl
))
851 (pushnew c
(slot-value first
'can-precede-list
) :test
#'eq
)))
852 (update-class-can-precede-p (cdr cpl
))))
854 (defun class-can-precede-p (class1 class2
)
855 (member class2
(class-can-precede-list class1
) :test
#'eq
))
857 (defun update-slots (class eslotds
)
858 (let ((instance-slots ())
860 (dolist (eslotd eslotds
)
861 (let ((alloc (slot-definition-allocation eslotd
)))
863 (:instance
(push eslotd instance-slots
))
864 (:class
(push eslotd class-slots
)))))
866 ;; If there is a change in the shape of the instances then the
867 ;; old class is now obsolete.
868 (let* ((nlayout (mapcar #'slot-definition-name
869 (sort instance-slots
#'<
870 :key
#'slot-definition-location
)))
871 (nslots (length nlayout
))
872 (nwrapper-class-slots (compute-class-slots class-slots
))
873 (owrapper (when (class-finalized-p class
)
874 (class-wrapper class
)))
875 (olayout (when owrapper
876 (wrapper-instance-slots-layout owrapper
)))
877 (owrapper-class-slots (and owrapper
(wrapper-class-slots owrapper
)))
879 (cond ((null owrapper
)
880 (make-wrapper nslots class
))
881 ((and (equal nlayout olayout
)
883 (loop for o in owrapper-class-slots
884 for n in nwrapper-class-slots
885 do
(unless (eq (car o
) (car n
)) (return t
)))))
888 ;; This will initialize the new wrapper to have the
889 ;; same state as the old wrapper. We will then have
890 ;; to change that. This may seem like wasted work
891 ;; (and it is), but the spec requires that we call
892 ;; MAKE-INSTANCES-OBSOLETE.
893 (make-instances-obsolete class
)
894 (class-wrapper class
)))))
896 (update-lisp-class-layout class nwrapper
)
897 (setf (slot-value class
'slots
) eslotds
898 (wrapper-slot-table nwrapper
) (make-slot-table class eslotds
)
899 (wrapper-instance-slots-layout nwrapper
) nlayout
900 (wrapper-class-slots nwrapper
) nwrapper-class-slots
901 (wrapper-length nwrapper
) nslots
902 (slot-value class
'wrapper
) nwrapper
)
903 (do* ((slots (slot-value class
'slots
) (cdr slots
))
908 "~@<slot names with the same SYMBOL-NAME but ~
909 different SYMBOL-PACKAGE (possible package problem) ~
910 for class ~S:~4I~@:_~<~@{~S~^~:@_~}~:>~@:>"
912 (let* ((slot (car slots
))
913 (oslots (remove (slot-definition-name slot
) (cdr slots
)
915 :key
#'slot-definition-name
)))
917 (pushnew (cons (slot-definition-name slot
)
918 (mapcar #'slot-definition-name oslots
))
920 :test
#'string
= :key
#'car
))))
921 (setf (slot-value class
'finalized-p
) t
)
922 (unless (eq owrapper nwrapper
)
923 (maybe-update-standard-class-locations class
)))))
925 (defun compute-class-slots (eslotds)
927 (dolist (eslotd eslotds
(nreverse collect
))
928 (let ((cell (assoc (slot-definition-name eslotd
)
930 (slot-definition-allocation-class eslotd
)))))
932 (push cell collect
)))))
934 (defun update-gfs-of-class (class)
935 (when (and (class-finalized-p class
)
936 (let ((cpl (class-precedence-list class
)))
937 (or (member *the-class-slot-class
* cpl
:test
#'eq
)
938 (member *the-class-standard-effective-slot-definition
*
940 (let ((gf-table (make-hash-table :test
'eq
)))
941 (labels ((collect-gfs (class)
942 (dolist (gf (specializer-direct-generic-functions class
))
943 (setf (gethash gf gf-table
) t
))
944 (mapc #'collect-gfs
(class-direct-superclasses class
))))
946 (maphash (lambda (gf ignore
)
947 (declare (ignore ignore
))
948 (update-gf-dfun class gf
))
951 (defun update-initargs (class inits
)
952 (setf (plist-value class
'default-initargs
) inits
))
954 (defmethod compute-default-initargs ((class slot-class
))
955 (let ((initargs (loop for c in
(class-precedence-list class
)
956 append
(class-direct-default-initargs c
))))
957 (delete-duplicates initargs
:test
#'eq
:key
#'car
:from-end t
)))
959 ;;;; protocols for constructing direct and effective slot definitions
961 (defmethod direct-slot-definition-class ((class std-class
) &rest initargs
)
962 (declare (ignore initargs
))
963 (find-class 'standard-direct-slot-definition
))
965 (defun make-direct-slotd (class initargs
)
966 (apply #'make-instance
967 (apply #'direct-slot-definition-class class initargs
)
971 ;;; I (CSR) am not sure, but I believe that the particular order of
972 ;;; slots is quite important: it is ideal to attempt to have a
973 ;;; constant slot location for the same notional slots as much as
974 ;;; possible, so that clever discriminating functions (ONE-INDEX et
975 ;;; al.) have a chance of working. The below at least walks through
976 ;;; the slots predictably, but maybe it would be good to compute some
977 ;;; kind of optimal slot layout by looking at locations of slots in
979 (defun std-compute-slots (class)
980 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
981 ;; for each different slot name we find in our superclasses. Each
982 ;; call receives the class and a list of the dslotds with that name.
983 ;; The list is in most-specific-first order.
984 (let ((name-dslotds-alist ()))
985 (dolist (c (reverse (class-precedence-list class
)))
986 (dolist (slot (class-direct-slots c
))
987 (let* ((name (slot-definition-name slot
))
988 (entry (assq name name-dslotds-alist
)))
990 (push slot
(cdr entry
))
991 (push (list name slot
) name-dslotds-alist
)))))
992 (mapcar (lambda (direct)
993 (compute-effective-slot-definition class
996 (nreverse name-dslotds-alist
))))
998 (defmethod compute-slots ((class standard-class
))
999 (std-compute-slots class
))
1000 (defmethod compute-slots ((class funcallable-standard-class
))
1001 (std-compute-slots class
))
1003 (defun std-compute-slots-around (class eslotds
)
1004 (let ((location -
1))
1005 (dolist (eslotd eslotds eslotds
)
1006 (setf (slot-definition-location eslotd
)
1007 (case (slot-definition-allocation eslotd
)
1011 (let* ((name (slot-definition-name eslotd
))
1014 (slot-definition-allocation-class eslotd
)
1015 ;; we get here if the user adds an extra slot
1017 (setf (slot-definition-allocation-class eslotd
)
1019 ;; which raises the question of what we should
1020 ;; do if we find that said user has added a slot
1021 ;; with the same name as another slot...
1022 (cell (or (assq name
(class-slot-cells from-class
))
1023 (let ((c (cons name
+slot-unbound
+)))
1024 (push c
(class-slot-cells from-class
))
1027 (if (eq +slot-unbound
+ (cdr cell
))
1028 ;; We may have inherited an initfunction
1029 (let ((initfun (slot-definition-initfunction eslotd
)))
1031 (rplacd cell
(funcall initfun
))
1034 (unless (slot-definition-class eslotd
)
1035 (setf (slot-definition-class eslotd
) class
))
1036 (initialize-internal-slot-functions eslotd
))))
1038 (defmethod compute-slots :around
((class standard-class
))
1039 (let ((eslotds (call-next-method)))
1040 (std-compute-slots-around class eslotds
)))
1041 (defmethod compute-slots :around
((class funcallable-standard-class
))
1042 (let ((eslotds (call-next-method)))
1043 (std-compute-slots-around class eslotds
)))
1045 (defmethod compute-slots ((class structure-class
))
1046 (mapcan (lambda (superclass)
1047 (mapcar (lambda (dslotd)
1048 (compute-effective-slot-definition
1050 (slot-definition-name dslotd
)
1052 (class-direct-slots superclass
)))
1053 (reverse (slot-value class
'%class-precedence-list
))))
1055 (defmethod compute-slots :around
((class structure-class
))
1056 (let ((eslotds (call-next-method)))
1057 (mapc #'initialize-internal-slot-functions eslotds
)
1060 (defmethod compute-effective-slot-definition ((class slot-class
) name dslotds
)
1061 (declare (ignore name
))
1062 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds
))
1063 (class (apply #'effective-slot-definition-class class initargs
)))
1064 (apply #'make-instance class initargs
)))
1066 (defmethod effective-slot-definition-class ((class std-class
) &rest initargs
)
1067 (declare (ignore initargs
))
1068 (find-class 'standard-effective-slot-definition
))
1070 (defmethod effective-slot-definition-class ((class structure-class
) &rest initargs
)
1071 (declare (ignore initargs
))
1072 (find-class 'structure-effective-slot-definition
))
1074 (defmethod compute-effective-slot-definition-initargs
1075 ((class slot-class
) direct-slotds
)
1081 (allocation-class nil
)
1083 (type-check-function nil
)
1085 (documentationp nil
)
1090 (dolist (slotd direct-slotds
)
1093 (setq name
(slot-definition-name slotd
)
1096 (when (slot-definition-initfunction slotd
)
1097 (setq initform
(slot-definition-initform slotd
)
1098 initfunction
(slot-definition-initfunction slotd
)
1100 (unless documentationp
1101 (when (%slot-definition-documentation slotd
)
1102 (setq documentation
(%slot-definition-documentation slotd
)
1105 (setq allocation
(slot-definition-allocation slotd
)
1106 allocation-class
(slot-definition-class slotd
)
1108 (setq initargs
(append (slot-definition-initargs slotd
) initargs
))
1109 (let ((fun (slot-definition-type-check-function slotd
)))
1111 (setf type-check-function
1112 (if type-check-function
1113 (let ((old-function type-check-function
))
1115 (funcall old-function value
)
1116 (funcall fun value
)))
1118 (let ((slotd-type (slot-definition-type slotd
)))
1120 ((eq type t
) slotd-type
)
1121 ;; This pairwise type intersection is perhaps a
1122 ;; little inefficient and inelegant, but it's
1123 ;; unlikely to lie on the critical path. Shout
1124 ;; if I'm wrong. -- CSR, 2005-11-24
1126 (specifier-type `(and ,type
,slotd-type
)))))))))
1129 :initfunction initfunction
1131 :allocation allocation
1132 :allocation-class allocation-class
1134 'type-check-function type-check-function
1136 :documentation documentation
)))
1138 (defmethod compute-effective-slot-definition-initargs :around
1139 ((class structure-class
) direct-slotds
)
1140 (let ((slotd (car direct-slotds
)))
1141 (list* :defstruct-accessor-symbol
1142 (slot-definition-defstruct-accessor-symbol slotd
)
1143 :internal-reader-function
1144 (slot-definition-internal-reader-function slotd
)
1145 :internal-writer-function
1146 (slot-definition-internal-writer-function slotd
)
1147 (call-next-method))))
1149 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1150 ;;; to make the method object. They have to use make-a-method which
1151 ;;; is a specially bootstrapped mechanism for making standard methods.
1152 (defmethod reader-method-class ((class slot-class
) direct-slot
&rest initargs
)
1153 (declare (ignore direct-slot initargs
))
1154 (find-class 'standard-reader-method
))
1156 (defmethod add-reader-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1157 (add-method generic-function
1158 (make-a-method 'standard-reader-method
1160 (list (or (class-name class
) 'object
))
1162 (make-reader-method-function class slot-name
)
1163 (or slot-documentation
"automatically generated reader method")
1164 :slot-name slot-name
1166 :method-class-function
#'reader-method-class
1167 :definition-source source-location
)))
1169 (defmethod writer-method-class ((class slot-class
) direct-slot
&rest initargs
)
1170 (declare (ignore direct-slot initargs
))
1171 (find-class 'standard-writer-method
))
1173 (defmethod add-writer-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1174 (add-method generic-function
1175 (make-a-method 'standard-writer-method
1177 (list 'new-value
(or (class-name class
) 'object
))
1178 (list *the-class-t
* class
)
1179 (make-writer-method-function class slot-name
)
1180 (or slot-documentation
"automatically generated writer method")
1181 :slot-name slot-name
1183 :method-class-function
#'writer-method-class
1184 :definition-source source-location
)))
1186 (defmethod add-boundp-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1187 (add-method generic-function
1188 (make-a-method (constantly (find-class 'standard-boundp-method
))
1191 (list (or (class-name class
) 'object
))
1193 (make-boundp-method-function class slot-name
)
1194 (or slot-documentation
"automatically generated boundp method")
1195 :slot-name slot-name
1196 :definition-source source-location
)))
1198 (defmethod remove-reader-method ((class slot-class
) generic-function
)
1199 (let ((method (get-method generic-function
() (list class
) nil
)))
1200 (when method
(remove-method generic-function method
))))
1202 (defmethod remove-writer-method ((class slot-class
) generic-function
)
1204 (get-method generic-function
() (list *the-class-t
* class
) nil
)))
1205 (when method
(remove-method generic-function method
))))
1207 (defmethod remove-boundp-method ((class slot-class
) generic-function
)
1208 (let ((method (get-method generic-function
() (list class
) nil
)))
1209 (when method
(remove-method generic-function method
))))
1211 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1212 ;;; function are NOT part of the standard protocol. They are however
1213 ;;; useful; PCL makes use of them internally and documents them for
1214 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1216 ;;; *** This needs work to make type testing by the writer functions which
1217 ;;; *** do type testing faster. The idea would be to have one constructor
1218 ;;; *** for each possible type test.
1220 ;;; *** There is a subtle bug here which is going to have to be fixed.
1221 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1222 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1223 ;;; *** defined for this metaclass a chance to run.
1225 (defmethod make-reader-method-function ((class slot-class
) slot-name
)
1226 (make-std-reader-method-function class slot-name
))
1228 (defmethod make-writer-method-function ((class slot-class
) slot-name
)
1229 (make-std-writer-method-function class slot-name
))
1231 (defmethod make-boundp-method-function ((class slot-class
) slot-name
)
1232 (make-std-boundp-method-function class slot-name
))
1234 (defmethod compatible-meta-class-change-p (class proto-new-class
)
1235 (eq (class-of class
) (class-of proto-new-class
)))
1237 (defmethod validate-superclass ((class class
) (superclass class
))
1238 (or (eq superclass
*the-class-t
*)
1239 (eq (class-of class
) (class-of superclass
))
1240 (and (eq (class-of superclass
) *the-class-standard-class
*)
1241 (eq (class-of class
) *the-class-funcallable-standard-class
*))
1242 (and (eq (class-of superclass
) *the-class-funcallable-standard-class
*)
1243 (eq (class-of class
) *the-class-standard-class
*))))
1245 ;;; What this does depends on which of the four possible values of
1246 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1247 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1248 ;;; nothing to do, as the new wrapper has already been created. If
1249 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1250 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1251 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1253 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1254 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1255 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1256 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1257 ;;; obsolete the wrapper.
1259 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1260 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1261 ;;; :UNINITIALIZED)))
1263 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1264 (defun force-cache-flushes (class)
1265 (let* ((owrapper (class-wrapper class
)))
1266 ;; We only need to do something if the wrapper is still valid. If
1267 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1268 ;; both of those will already be doing what we want. In
1269 ;; particular, we must be sure we never change an OBSOLETE into a
1270 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1271 (when (or (not (invalid-wrapper-p owrapper
))
1272 ;; KLUDGE: despite the observations above, this remains
1273 ;; a violation of locality or what might be considered
1274 ;; good style. There has to be a better way! -- CSR,
1276 (eq (layout-invalid owrapper
) t
))
1277 (let ((nwrapper (make-wrapper (layout-length owrapper
)
1279 (setf (wrapper-instance-slots-layout nwrapper
)
1280 (wrapper-instance-slots-layout owrapper
))
1281 (setf (wrapper-class-slots nwrapper
)
1282 (wrapper-class-slots owrapper
))
1283 (setf (wrapper-slot-table nwrapper
)
1284 (wrapper-slot-table owrapper
))
1286 (update-lisp-class-layout class nwrapper
)
1287 (setf (slot-value class
'wrapper
) nwrapper
)
1288 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1290 (if (find-if (lambda (x)
1291 (and (consp x
) (eq :obsolete
(car x
))))
1292 (layout-inherits owrapper
)
1293 :key
#'layout-invalid
)
1294 (invalidate-wrapper owrapper
:obsolete nwrapper
)
1295 (invalidate-wrapper owrapper
:flush nwrapper
)))))))
1297 (defun flush-cache-trap (owrapper nwrapper instance
)
1298 (declare (ignore owrapper
))
1299 (set-wrapper instance nwrapper
))
1301 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1302 ;;; the next access to the instance (as defined in 88-002R) to trap
1303 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1304 (defmethod make-instances-obsolete ((class std-class
))
1305 (let* ((owrapper (class-wrapper class
))
1306 (nwrapper (make-wrapper (layout-length owrapper
)
1308 (unless (class-finalized-p class
)
1309 (if (class-has-a-forward-referenced-superclass-p class
)
1310 (return-from make-instances-obsolete class
)
1311 (update-cpl class
(compute-class-precedence-list class
))))
1312 (setf (wrapper-instance-slots-layout nwrapper
)
1313 (wrapper-instance-slots-layout owrapper
))
1314 (setf (wrapper-class-slots nwrapper
)
1315 (wrapper-class-slots owrapper
))
1316 (setf (wrapper-slot-table nwrapper
)
1317 (wrapper-slot-table owrapper
))
1319 (update-lisp-class-layout class nwrapper
)
1320 (setf (slot-value class
'wrapper
) nwrapper
)
1321 (invalidate-wrapper owrapper
:obsolete nwrapper
)
1324 (defmethod make-instances-obsolete ((class symbol
))
1325 (make-instances-obsolete (find-class class
))
1326 ;; ANSI wants the class name when called with a symbol.
1329 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1330 ;;; see an obsolete instance. The times when it is called are:
1331 ;;; - when the instance is involved in method lookup
1332 ;;; - when attempting to access a slot of an instance
1334 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1335 ;;; instance access macros.
1337 ;;; Of course these times when it is called are an internal
1338 ;;; implementation detail of PCL and are not part of the documented
1339 ;;; description of when the obsolete instance update happens. The
1340 ;;; documented description is as it appears in 88-002R.
1342 ;;; This has to return the new wrapper, so it counts on all the
1343 ;;; methods on obsolete-instance-trap-internal to return the new
1344 ;;; wrapper. It also does a little internal error checking to make
1345 ;;; sure that the traps are only happening when they should, and that
1346 ;;; the trap methods are computing appropriate new wrappers.
1348 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1349 ;;; after a structure is redefined. In most cases,
1350 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1351 ;;; so it must signal an error. The hard part of this is that the
1352 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1353 ;;; called again, so in that case, we have to return some reasonable
1354 ;;; wrapper, instead.
1356 (defvar *in-obsolete-instance-trap
* nil
)
1357 (defvar *the-wrapper-of-structure-object
*
1358 (class-wrapper (find-class 'structure-object
)))
1360 (define-condition obsolete-structure
(error)
1361 ((datum :reader obsolete-structure-datum
:initarg
:datum
))
1363 (lambda (condition stream
)
1364 ;; Don't try to print the structure, since it probably won't work.
1366 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1367 (type-of (obsolete-structure-datum condition
))))))
1369 (defun obsolete-instance-trap (owrapper nwrapper instance
)
1370 (if (not (layout-for-std-class-p owrapper
))
1371 (if *in-obsolete-instance-trap
*
1372 *the-wrapper-of-structure-object
*
1373 (let ((*in-obsolete-instance-trap
* t
))
1374 (error 'obsolete-structure
:datum instance
)))
1375 (let* ((class (wrapper-class* nwrapper
))
1376 (copy (allocate-instance class
)) ;??? allocate-instance ???
1377 (olayout (wrapper-instance-slots-layout owrapper
))
1378 (nlayout (wrapper-instance-slots-layout nwrapper
))
1379 (oslots (get-slots instance
))
1380 (nslots (get-slots copy
))
1381 (oclass-slots (wrapper-class-slots owrapper
))
1386 ;; local --> local transfer value
1387 ;; local --> shared discard value, discard slot
1388 ;; local --> -- discard slot
1389 ;; shared --> local transfer value
1390 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1391 ;; shared --> -- discard value
1392 ;; -- --> local add slot
1395 ;; Go through all the old local slots.
1397 (dolist (name olayout
)
1398 (let ((npos (posq name nlayout
)))
1400 (setf (clos-slots-ref nslots npos
)
1401 (clos-slots-ref oslots opos
))
1403 (push name discarded
)
1404 (unless (eq (clos-slots-ref oslots opos
) +slot-unbound
+)
1405 (setf (getf plist name
) (clos-slots-ref oslots opos
))))))
1408 ;; Go through all the old shared slots.
1409 (dolist (oclass-slot-and-val oclass-slots
)
1410 (let ((name (car oclass-slot-and-val
))
1411 (val (cdr oclass-slot-and-val
)))
1412 (let ((npos (posq name nlayout
)))
1414 (setf (clos-slots-ref nslots npos
) val
)))))
1416 ;; Go through all the new local slots to compute the added slots.
1417 (dolist (nlocal nlayout
)
1418 (unless (or (memq nlocal olayout
)
1419 (assq nlocal oclass-slots
))
1420 (push nlocal added
)))
1422 (swap-wrappers-and-slots instance copy
)
1424 (update-instance-for-redefined-class instance
1430 (defun change-class-internal (instance new-class initargs
)
1431 (let* ((old-class (class-of instance
))
1432 (copy (allocate-instance new-class
))
1433 (new-wrapper (get-wrapper copy
))
1434 (old-wrapper (class-wrapper old-class
))
1435 (old-layout (wrapper-instance-slots-layout old-wrapper
))
1436 (new-layout (wrapper-instance-slots-layout new-wrapper
))
1437 (old-slots (get-slots instance
))
1438 (new-slots (get-slots copy
))
1439 (old-class-slots (wrapper-class-slots old-wrapper
)))
1441 ;; "The values of local slots specified by both the class CTO and
1442 ;; CFROM are retained. If such a local slot was unbound, it
1443 ;; remains unbound."
1444 (let ((new-position 0))
1445 (dolist (new-slot new-layout
)
1446 (let ((old-position (posq new-slot old-layout
)))
1448 (setf (clos-slots-ref new-slots new-position
)
1449 (clos-slots-ref old-slots old-position
))))
1450 (incf new-position
)))
1452 ;; "The values of slots specified as shared in the class CFROM and
1453 ;; as local in the class CTO are retained."
1454 (dolist (slot-and-val old-class-slots
)
1455 (let ((position (posq (car slot-and-val
) new-layout
)))
1457 (setf (clos-slots-ref new-slots position
) (cdr slot-and-val
)))))
1459 ;; Make the copy point to the old instance's storage, and make the
1460 ;; old instance point to the new storage.
1461 (swap-wrappers-and-slots instance copy
)
1463 (apply #'update-instance-for-different-class copy instance initargs
)
1466 (defmethod change-class ((instance standard-object
) (new-class standard-class
)
1468 (unless (class-finalized-p new-class
)
1469 (finalize-inheritance new-class
))
1470 (let ((cpl (class-precedence-list new-class
)))
1474 `(when (eq class
(find-class ',class-name
))
1475 (error 'metaobject-initialization-violation
1476 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1477 :format-arguments
(list 'change-class
',class-name
)
1478 :references
(list '(:amop
:initialization
,class-name
))))))
1480 (frob generic-function
)
1482 (frob slot-definition
))))
1483 (change-class-internal instance new-class initargs
))
1485 (defmethod change-class ((instance forward-referenced-class
)
1486 (new-class standard-class
) &rest initargs
)
1487 (let ((cpl (class-precedence-list new-class
)))
1489 (error 'metaobject-initialization-violation
1491 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1493 (list 'change-class
'forward-referenced-class
'class
)
1495 (list '(:amop
:generic-function ensure-class-using-class
)
1496 '(:amop
:initialization class
))))
1497 (when (eq class
(find-class 'class
))
1499 (change-class-internal instance new-class initargs
))
1501 (defmethod change-class ((instance funcallable-standard-object
)
1502 (new-class funcallable-standard-class
)
1504 (let ((cpl (class-precedence-list new-class
)))
1508 `(when (eq class
(find-class ',class-name
))
1509 (error 'metaobject-initialization-violation
1510 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1511 :format-arguments
(list 'change-class
',class-name
)
1512 :references
(list '(:amop
:initialization
,class-name
))))))
1514 (frob generic-function
)
1516 (frob slot-definition
))))
1517 (change-class-internal instance new-class initargs
))
1519 (defmethod change-class ((instance standard-object
)
1520 (new-class funcallable-standard-class
)
1522 (declare (ignore initargs
))
1523 (error "You can't change the class of ~S to ~S~@
1524 because it isn't already an instance with metaclass ~S."
1525 instance new-class
'standard-class
))
1527 (defmethod change-class ((instance funcallable-standard-object
)
1528 (new-class standard-class
)
1530 (declare (ignore initargs
))
1531 (error "You can't change the class of ~S to ~S~@
1532 because it isn't already an instance with metaclass ~S."
1533 instance new-class
'funcallable-standard-class
))
1535 (defmethod change-class ((instance t
) (new-class-name symbol
) &rest initargs
)
1536 (apply #'change-class instance
(find-class new-class-name
) initargs
))
1538 ;;;; The metaclass BUILT-IN-CLASS
1540 ;;;; This metaclass is something of a weird creature. By this point, all
1541 ;;;; instances of it which will exist have been created, and no instance
1542 ;;;; is ever created by calling MAKE-INSTANCE.
1544 ;;;; But, there are other parts of the protocol we must follow and those
1545 ;;;; definitions appear here.
1547 (macrolet ((def (name args control
)
1548 `(defmethod ,name
,args
1549 (declare (ignore initargs
))
1550 (error 'metaobject-initialization-violation
1551 :format-control
,(format nil
"~@<~A~@:>" control
)
1552 :format-arguments
(list ',name
)
1553 :references
(list '(:amop
:initialization
"Class"))))))
1554 (def initialize-instance
((class built-in-class
) &rest initargs
)
1555 "Cannot ~S an instance of BUILT-IN-CLASS.")
1556 (def reinitialize-instance
((class built-in-class
) &rest initargs
)
1557 "Cannot ~S an instance of BUILT-IN-CLASS."))
1559 (macrolet ((def (name)
1560 `(defmethod ,name
((class built-in-class
)) nil
)))
1561 (def class-direct-slots
)
1563 (def class-direct-default-initargs
)
1564 (def class-default-initargs
))
1566 (defmethod validate-superclass ((c class
) (s built-in-class
))
1567 (or (eq s
*the-class-t
*) (eq s
*the-class-stream
*)
1568 ;; FIXME: bad things happen if someone tries to mix in both
1569 ;; FILE-STREAM and STRING-STREAM (as they have the same
1570 ;; layout-depthoid). Is there any way we can provide a useful
1571 ;; error message? -- CSR, 2005-05-03
1572 (eq s
*the-class-file-stream
*) (eq s
*the-class-string-stream
*)
1573 ;; This probably shouldn't be mixed in with certain other
1574 ;; classes, too, but it seems to work both with STANDARD-OBJECT
1575 ;; and FUNCALLABLE-STANDARD-OBJECT
1576 (eq s
*the-class-sequence
*)))
1578 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1579 (defmethod class-direct-slots ((class forward-referenced-class
)) ())
1580 (defmethod class-direct-default-initargs ((class forward-referenced-class
)) ())
1581 (macrolet ((def (method)
1582 `(defmethod ,method
((class forward-referenced-class
))
1583 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1585 (def class-default-initargs
)
1586 (def class-precedence-list
)
1589 (defmethod validate-superclass ((c slot-class
)
1590 (f forward-referenced-class
))
1593 (defmethod add-dependent ((metaobject dependent-update-mixin
) dependent
)
1594 (pushnew dependent
(plist-value metaobject
'dependents
) :test
#'eq
))
1596 (defmethod remove-dependent ((metaobject dependent-update-mixin
) dependent
)
1597 (setf (plist-value metaobject
'dependents
)
1598 (delete dependent
(plist-value metaobject
'dependents
))))
1600 (defmethod map-dependents ((metaobject dependent-update-mixin
) function
)
1601 (dolist (dependent (plist-value metaobject
'dependents
))
1602 (funcall function dependent
)))