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
73 ((slotd 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 ;; KLUDGE: this logic is cut'n'pasted from
83 ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
84 ;; only called later, because it does things that can't be
85 ;; computed this early in class finalization; however, we need
86 ;; this bit as early as possible. -- CSR, 2009-11-05
87 (setf (slot-accessor-std-p slotd type
)
88 (let* ((std-method (standard-svuc-method type
))
89 (str-method (structure-svuc-method type
))
90 (types1 `((eql ,class
) (class-eq ,class
) (eql ,slotd
)))
91 (types (if (eq type
'writer
) `(t ,@types1
) types1
))
92 (methods (compute-applicable-methods-using-types gf types
)))
93 (null (cdr methods
))))
94 (setf (slot-accessor-function slotd type
)
96 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
97 ;; work here (see KLUDGE comment above).
98 (let ((fun (compute-slot-accessor-info slotd type gf
)))
99 (apply fun args
))))))))
101 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition
))
102 (let* ((name (slot-value slotd
'name
)))
103 (dolist (type '(reader writer boundp
))
104 (let* ((gf-name (ecase type
105 (reader 'slot-value-using-class
)
106 (writer '(setf slot-value-using-class
))
107 (boundp 'slot-boundp-using-class
)))
108 (gf (gdefinition gf-name
)))
109 (compute-slot-accessor-info slotd type gf
)))))
111 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
113 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
114 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
115 ;;; writing/testing effective slot SLOTD.
117 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
118 ;;; GF. Store the effective method in the effective slot definition
119 ;;; object itself; these GFs have special dispatch functions calling
120 ;;; effective methods directly retrieved from effective slot
121 ;;; definition objects, as an optimization.
123 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
125 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition
)
127 (let* ((name (slot-value slotd
'name
))
128 (class (slot-value slotd
'%class
)))
129 (multiple-value-bind (function std-p
)
130 (if (eq **boot-state
** 'complete
)
131 (get-accessor-method-function gf type class slotd
)
132 (get-optimized-std-accessor-method-function class slotd type
))
133 (setf (slot-accessor-std-p slotd type
) std-p
)
134 (setf (slot-accessor-function slotd type
) function
))))
136 (defmethod slot-definition-allocation ((slotd structure-slot-definition
))
139 ;;;; various class accessors that are a little more complicated than can be
140 ;;;; done with automatically generated reader methods
142 (defmethod class-prototype :before
(class)
143 (unless (class-finalized-p class
)
144 (error "~@<~S is not finalized.~:@>" class
)))
146 ;;; KLUDGE: For some reason factoring the common body into a function
147 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
149 (macrolet ((def (class)
150 `(defmethod class-prototype ((class ,class
))
151 (with-slots (prototype) class
153 (setf prototype
(allocate-instance class
)))))))
155 (def condition-class
)
156 (def structure-class
))
158 (defmethod class-direct-default-initargs ((class slot-class
))
159 (plist-value class
'direct-default-initargs
))
161 (defmethod class-default-initargs ((class slot-class
))
162 (plist-value class
'default-initargs
))
164 (defmethod class-slot-cells ((class std-class
))
165 (plist-value class
'class-slot-cells
))
166 (defmethod (setf class-slot-cells
) (new-value (class std-class
))
167 (setf (plist-value class
'class-slot-cells
) new-value
))
169 ;;;; class accessors that are even a little bit more complicated than those
170 ;;;; above. These have a protocol for updating them, we must implement that
173 ;;; Maintaining the direct subclasses backpointers. The update methods are
174 ;;; here, the values are read by an automatically generated reader method.
175 (defmethod add-direct-subclass ((class class
) (subclass class
))
176 (with-slots (direct-subclasses) class
177 (pushnew subclass direct-subclasses
:test
#'eq
)
179 (defmethod remove-direct-subclass ((class class
) (subclass class
))
180 (with-slots (direct-subclasses) class
181 (setq direct-subclasses
(remove subclass direct-subclasses
))
184 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
186 ;;; There are four generic functions involved, each has one method for the
187 ;;; class case and another method for the damned EQL specializers. All of
188 ;;; these are specified methods and appear in their specified place in the
191 ;;; ADD-DIRECT-METHOD
192 ;;; REMOVE-DIRECT-METHOD
193 ;;; SPECIALIZER-DIRECT-METHODS
194 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
196 ;;; In each case, we maintain one value which is a cons. The car is the list
197 ;;; methods. The cdr is a list of the generic functions. The cdr is always
200 ;;; This needs to be used recursively, in case a non-trivial user
201 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
202 ;;; function using the same lock.
203 (defvar *specializer-lock
* (sb-thread::make-spinlock
:name
"Specializer lock"))
205 (defmethod add-direct-method :around
((specializer specializer
) method
)
206 ;; All the actions done under this lock are done in an order
207 ;; that is safe to unwind at any point.
208 (sb-thread::with-recursive-system-spinlock
(*specializer-lock
*)
211 (defmethod remove-direct-method :around
((specializer specializer
) method
)
212 ;; All the actions done under this lock are done in an order
213 ;; that is safe to unwind at any point.
214 (sb-thread::with-recursive-system-spinlock
(*specializer-lock
*)
217 (defmethod add-direct-method ((specializer class
) (method method
))
218 (let ((cell (slot-value specializer
'direct-methods
)))
219 ;; We need to first smash the CDR, because a parallel read may
220 ;; be in progress, and because if an interrupt catches us we
221 ;; need to have a consistent state.
223 (car cell
) (adjoin method
(car cell
) :test
#'eq
)))
226 (defmethod remove-direct-method ((specializer class
) (method method
))
227 (let ((cell (slot-value specializer
'direct-methods
)))
228 ;; We need to first smash the CDR, because a parallel read may
229 ;; be in progress, and because if an interrupt catches us we
230 ;; need to have a consistent state.
232 (car cell
) (remove method
(car cell
))))
235 (defmethod specializer-direct-methods ((specializer class
))
236 (with-slots (direct-methods) specializer
237 (car direct-methods
)))
239 (defmethod specializer-direct-generic-functions ((specializer class
))
240 (let ((cell (slot-value specializer
'direct-methods
)))
241 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
242 ;; we behave as if we got just first or just after -- it's just
243 ;; for update that we need to lock.
245 (sb-thread::with-spinlock
(*specializer-lock
*)
248 (dolist (m (car cell
))
249 ;; the old PCL code used COLLECTING-ONCE which used
250 ;; #'EQ to check for newness
251 (pushnew (method-generic-function m
) collect
:test
#'eq
))
252 (nreverse collect
)))))))
254 ;;; This hash table is used to store the direct methods and direct generic
255 ;;; functions of EQL specializers. Each value in the table is the cons.
257 ;;; These tables are shared between threads, so they need to be synchronized.
258 (defvar *eql-specializer-methods
* (make-hash-table :test
'eql
:synchronized t
))
259 (defvar *class-eq-specializer-methods
* (make-hash-table :test
'eq
:synchronized t
))
261 (defmethod specializer-method-table ((specializer eql-specializer
))
262 *eql-specializer-methods
*)
264 (defmethod specializer-method-table ((specializer class-eq-specializer
))
265 *class-eq-specializer-methods
*)
267 (defmethod add-direct-method ((specializer specializer-with-object
)
269 (let* ((object (specializer-object specializer
))
270 (table (specializer-method-table specializer
))
271 (entry (gethash object table
)))
274 (setf (gethash object table
) (cons nil nil
))))
275 ;; We need to first smash the CDR, because a parallel read may
276 ;; be in progress, and because if an interrupt catches us we
277 ;; need to have a consistent state.
279 (car entry
) (adjoin method
(car entry
) :test
#'eq
))
282 (defmethod remove-direct-method ((specializer specializer-with-object
)
284 (let* ((object (specializer-object specializer
))
285 (entry (gethash object
(specializer-method-table specializer
))))
287 ;; We need to first smash the CDR, because a parallel read may
288 ;; be in progress, and because if an interrupt catches us we
289 ;; need to have a consistent state.
291 (car entry
) (remove method
(car entry
))))
294 (defmethod specializer-direct-methods ((specializer specializer-with-object
))
295 (car (gethash (specializer-object specializer
)
296 (specializer-method-table specializer
))))
298 (defmethod specializer-direct-generic-functions ((specializer
299 specializer-with-object
))
300 (let* ((object (specializer-object specializer
))
301 (entry (gethash object
(specializer-method-table specializer
))))
304 (sb-thread::with-spinlock
(*specializer-lock
*)
307 (dolist (m (car entry
))
308 (pushnew (method-generic-function m
) collect
:test
#'eq
))
309 (nreverse collect
))))))))
311 (defun map-specializers (function)
312 (map-all-classes (lambda (class)
313 (funcall function
(class-eq-specializer class
))
314 (funcall function class
)))
315 (maphash (lambda (object methods
)
316 (declare (ignore methods
))
317 (intern-eql-specializer object
))
318 *eql-specializer-methods
*)
319 (maphash (lambda (object specl
)
320 (declare (ignore object
))
321 (funcall function specl
))
322 *eql-specializer-table
*)
325 (defun map-all-generic-functions (function)
326 (let ((all-generic-functions (make-hash-table :test
'eq
)))
327 (map-specializers (lambda (specl)
328 (dolist (gf (specializer-direct-generic-functions
330 (unless (gethash gf all-generic-functions
)
331 (setf (gethash gf all-generic-functions
) t
)
332 (funcall function gf
))))))
335 (defmethod shared-initialize :after
((specl class-eq-specializer
)
338 (declare (ignore slot-names
))
339 (setf (slot-value specl
'%type
) `(class-eq ,(specializer-class specl
))))
341 (defmethod shared-initialize :after
((specl eql-specializer
) slot-names
&key
)
342 (declare (ignore slot-names
))
343 (setf (slot-value specl
'%type
)
344 `(eql ,(specializer-object specl
)))
345 (setf (info :type
:translator specl
)
346 (constantly (make-member-type :members
(list (specializer-object specl
))))))
348 (defun real-load-defclass (name metaclass-name supers slots other
349 readers writers slot-names source-location safe-p
)
350 (with-single-package-locked-error (:symbol name
"defining ~S as a class")
351 (%compiler-defclass name readers writers slot-names
)
352 (let ((res (apply #'ensure-class name
:metaclass metaclass-name
353 :direct-superclasses supers
355 :definition-source source-location
360 (setf (gdefinition 'load-defclass
) #'real-load-defclass
)
362 (defun ensure-class (name &rest args
)
364 (apply #'ensure-class-using-class
365 (let ((class (find-class name nil
)))
366 (when (and class
(eq name
(class-name class
)))
367 ;; NAME is the proper name of CLASS, so redefine it
372 (defmethod ensure-class-using-class ((class null
) name
&rest args
&key
)
374 (multiple-value-bind (meta initargs
)
375 (frob-ensure-class-args args
)
376 (setf class
(apply #'make-instance meta
:name name initargs
))
377 (without-package-locks
378 (setf (find-class name
) class
))))
379 ;; After boot (SETF FIND-CLASS) does this.
380 (unless (eq **boot-state
** 'complete
)
381 (%set-class-type-translation class name
))
384 (defmethod ensure-class-using-class ((class pcl-class
) name
&rest args
&key
)
386 (multiple-value-bind (meta initargs
)
387 (frob-ensure-class-args args
)
388 (unless (eq (class-of class
) meta
)
389 (apply #'change-class class meta initargs
))
390 (apply #'reinitialize-instance class initargs
)
391 (without-package-locks
392 (setf (find-class name
) class
))))
393 ;; After boot (SETF FIND-CLASS) does this.
394 (unless (eq **boot-state
** 'complete
)
395 (%set-class-type-translation class name
))
398 (defun frob-ensure-class-args (args)
399 (let (metaclass metaclassp reversed-plist
)
400 (flet ((frob-superclass (s)
403 ((legal-class-name-p s
)
404 (or (find-class s nil
)
405 (ensure-class s
:metaclass
'forward-referenced-class
)))
406 (t (error "Not a class or a legal class name: ~S." s
)))))
407 (doplist (key val
) args
408 (cond ((eq key
:metaclass
)
410 (setf metaclass val metaclassp key
)))
412 (when (eq key
:direct-superclasses
)
413 (setf val
(mapcar #'frob-superclass val
)))
414 (setf reversed-plist
(list* val key reversed-plist
)))))
415 (values (cond (metaclassp
416 (if (classp metaclass
)
418 (find-class metaclass
)))
419 (t *the-class-standard-class
*))
420 (nreverse reversed-plist
)))))
422 (defun call-initfun (fun slotd safe
)
423 (declare (function fun
))
424 (let ((value (funcall fun
)))
426 (let ((typecheck (slot-definition-type-check-function slotd
)))
428 (funcall (the function typecheck
) value
))))
431 (defmethod shared-initialize :after
432 ((class std-class
) slot-names
&key
433 (direct-superclasses nil direct-superclasses-p
)
434 (direct-slots nil direct-slots-p
)
435 (direct-default-initargs nil direct-default-initargs-p
)
437 (cond (direct-superclasses-p
438 (setq direct-superclasses
439 (or direct-superclasses
440 (list (if (funcallable-standard-class-p class
)
441 *the-class-funcallable-standard-object
*
442 *the-class-standard-object
*))))
443 (dolist (superclass direct-superclasses
)
444 (unless (validate-superclass class superclass
)
445 (error "~@<The class ~S was specified as a ~
446 super-class of the class ~S, ~
447 but the meta-classes ~S and ~S are incompatible. ~
448 Define a method for ~S to avoid this error.~@:>"
449 superclass class
(class-of superclass
) (class-of class
)
450 'validate-superclass
)))
451 (setf (slot-value class
'direct-superclasses
) direct-superclasses
))
453 (setq direct-superclasses
(slot-value class
'direct-superclasses
))))
456 (setf (slot-value class
'direct-slots
)
457 (mapcar (lambda (pl) (make-direct-slotd class pl
))
459 (slot-value class
'direct-slots
)))
460 (if direct-default-initargs-p
461 (setf (plist-value class
'direct-default-initargs
)
462 direct-default-initargs
)
463 (setq direct-default-initargs
464 (plist-value class
'direct-default-initargs
)))
465 (setf (plist-value class
'class-slot-cells
)
466 (let ((old-class-slot-cells (plist-value class
'class-slot-cells
))
467 (safe (safe-p class
))
469 (dolist (dslotd direct-slots
)
470 (when (eq :class
(slot-definition-allocation dslotd
))
472 (let* ((name (slot-definition-name dslotd
))
473 (old (assoc name old-class-slot-cells
)))
476 (member name slot-names
:test
#'eq
))
477 (let* ((initfunction (slot-definition-initfunction dslotd
))
480 (call-initfun initfunction dslotd safe
)
482 (push (cons name value
) collect
))
483 (push old collect
)))))
485 (add-direct-subclasses class direct-superclasses
)
486 (if (class-finalized-p class
)
487 ;; required by AMOP, "Reinitialization of Class Metaobjects"
488 (finalize-inheritance class
)
489 (update-class class nil
))
490 (add-slot-accessors class direct-slots definition-source
)
491 (make-preliminary-layout class
))
493 (defmethod shared-initialize :after
((class forward-referenced-class
)
494 slot-names
&key
&allow-other-keys
)
495 (declare (ignore slot-names
))
496 (make-preliminary-layout class
))
498 (defvar *allow-forward-referenced-classes-in-cpl-p
* nil
)
500 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
501 ;;; make it known to the type system.
502 (defun make-preliminary-layout (class)
503 (flet ((compute-preliminary-cpl (root)
504 (let ((*allow-forward-referenced-classes-in-cpl-p
* t
))
505 (compute-class-precedence-list root
))))
507 (without-package-locks
508 (unless (class-finalized-p class
)
509 (let ((name (class-name class
)))
510 ;; KLUDGE: This is fairly horrible. We need to make a
511 ;; full-fledged CLASSOID here, not just tell the compiler that
512 ;; some class is forthcoming, because there are legitimate
513 ;; questions one can ask of the type system, implemented in
514 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
515 (let ((layout (make-wrapper 0 class
)))
516 (setf (slot-value class
'wrapper
) layout
)
517 (let ((cpl (compute-preliminary-cpl class
)))
518 (setf (layout-inherits layout
)
519 (order-layout-inherits
520 (map 'simple-vector
#'class-wrapper
521 (reverse (rest cpl
))))))
522 (register-layout layout
:invalidate t
)
523 (%set-class-type-translation class
(layout-classoid layout
)))))
524 (mapc #'make-preliminary-layout
(class-direct-subclasses class
))))))
527 (defmethod shared-initialize :before
((class class
) slot-names
&key name
)
528 (declare (ignore slot-names name
))
529 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
530 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
531 (setf (slot-value class
'%type
) `(class ,class
))
532 (setf (slot-value class
'class-eq-specializer
)
533 (make-instance 'class-eq-specializer
:class class
)))
535 (defmethod reinitialize-instance :before
((class slot-class
) &key direct-superclasses
)
536 (dolist (old-super (set-difference (class-direct-superclasses class
) direct-superclasses
))
537 (remove-direct-subclass old-super class
))
538 (remove-slot-accessors class
(class-direct-slots class
)))
540 (defmethod reinitialize-instance :after
((class slot-class
)
543 (map-dependents class
545 (apply #'update-dependent class dependent initargs
))))
547 (defmethod reinitialize-instance :after
((class condition-class
) &key
)
548 (let* ((name (class-name class
))
549 (classoid (find-classoid name
))
550 (slots (condition-classoid-slots classoid
)))
551 ;; to balance the REMOVE-SLOT-ACCESSORS call in
552 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
554 (let ((slot-name (condition-slot-name slot
)))
555 (dolist (reader (condition-slot-readers slot
))
556 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
557 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
558 (sb-kernel::install-condition-slot-reader reader name slot-name
))
559 (dolist (writer (condition-slot-writers slot
))
560 (sb-kernel::install-condition-slot-writer writer name slot-name
))))))
562 (defmethod shared-initialize :after
((class condition-class
) slot-names
563 &key direct-slots direct-superclasses
)
564 (declare (ignore slot-names
))
565 (let ((classoid (find-classoid (slot-value class
'name
))))
566 (with-slots (wrapper %class-precedence-list cpl-available-p
567 prototype
(direct-supers direct-superclasses
))
569 (setf (slot-value class
'direct-slots
)
570 (mapcar (lambda (pl) (make-direct-slotd class pl
))
572 (setf (slot-value class
'finalized-p
) t
)
573 (setf (classoid-pcl-class classoid
) class
)
574 (setq direct-supers direct-superclasses
)
575 (setq wrapper
(classoid-layout classoid
))
576 (setq %class-precedence-list
(compute-class-precedence-list class
))
577 (setq cpl-available-p t
)
578 (add-direct-subclasses class direct-superclasses
)
579 (let ((slots (compute-slots class
)))
580 (setf (slot-value class
'slots
) slots
)
581 (setf (layout-slot-table wrapper
) (make-slot-table class slots
)))))
582 ;; Comment from Gerd's PCL, 2003-05-15:
584 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
585 ;; override condition accessors with generic functions. We do this
588 ;; ??? What does the above comment mean and why is it a good idea?
589 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
590 ;; comment) loses slot information in its condition classes:
591 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
592 ;; remove slot accessors but never put them back. I've added a
593 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
594 ;; was meant to happen? -- CSR, 2005-11-18
597 (defmethod direct-slot-definition-class ((class condition-class
)
599 (declare (ignore initargs
))
600 (find-class 'condition-direct-slot-definition
))
602 (defmethod effective-slot-definition-class ((class condition-class
)
604 (declare (ignore initargs
))
605 (find-class 'condition-effective-slot-definition
))
607 (defmethod finalize-inheritance ((class condition-class
))
608 (aver (slot-value class
'finalized-p
))
611 (defmethod compute-effective-slot-definition
612 ((class condition-class
) slot-name dslotds
)
613 (let ((slotd (call-next-method)))
614 (setf (slot-definition-reader-function slotd
)
616 (handler-case (condition-reader-function x slot-name
)
617 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
618 ;; is unbound; maybe it should be a CELL-ERROR of some
620 (error () (values (slot-unbound class x slot-name
))))))
621 (setf (slot-definition-writer-function slotd
)
623 (condition-writer-function x v slot-name
)))
624 (setf (slot-definition-boundp-function slotd
)
626 (multiple-value-bind (v c
)
627 (ignore-errors (condition-reader-function x slot-name
))
632 (defmethod compute-slots ((class condition-class
))
633 (mapcan (lambda (superclass)
634 (mapcar (lambda (dslotd)
635 (compute-effective-slot-definition
636 class
(slot-definition-name dslotd
) (list dslotd
)))
637 (class-direct-slots superclass
)))
638 (reverse (slot-value class
'%class-precedence-list
))))
640 (defmethod compute-slots :around
((class condition-class
))
641 (let ((eslotds (call-next-method)))
642 (mapc #'finalize-internal-slot-functions eslotds
)
645 (defmethod shared-initialize :after
646 ((slotd structure-slot-definition
) slot-names
&key
647 (allocation :instance
) allocation-class
)
648 (declare (ignore slot-names allocation-class
))
649 (unless (eq allocation
:instance
)
650 (error "Structure slots must have :INSTANCE allocation.")))
652 (defun make-structure-class-defstruct-form (name direct-slots include
)
653 (let* ((conc-name (format-symbol *package
* "~S structure class " name
))
654 (constructor (format-symbol *package
* "~Aconstructor" conc-name
))
655 (included-name (class-name include
))
658 (mapcar #'dsd-name
(dd-slots (find-defstruct-description included-name
)))))
663 (dolist (slotd (reverse direct-slots
))
664 (let* ((slot-name (slot-definition-name slotd
))
665 (initform (slot-definition-initform slotd
))
666 (type (slot-definition-type slotd
))
667 (desc `(,slot-name
,initform
:type
,type
)))
668 (push `(slot-accessor ,name
,slot-name reader
)
670 (push `(slot-accessor ,name
,slot-name writer
)
672 (if (member slot-name included-slots
:test
#'eq
)
673 (push desc old-slots
)
674 (push desc new-slots
))))
675 (let* ((defstruct `(defstruct (,name
677 `((:include
,included-name
679 (:constructor
,constructor
())
681 (:conc-name
,conc-name
)
685 (mapcar (lambda (slotd reader-name
)
687 (slot-definition-defstruct-accessor-symbol
689 `(defun ,reader-name
(obj)
690 (declare (type ,name obj
))
692 direct-slots reader-names
))
694 (mapcar (lambda (slotd writer-name
)
696 (slot-definition-defstruct-accessor-symbol
698 `(defun ,writer-name
(nv obj
)
699 (declare (type ,name obj
))
700 (setf (,accessor obj
) nv
))))
701 direct-slots writer-names
))
705 ,@readers-init
,@writers-init
707 (values defstruct-form constructor reader-names writer-names
))))
709 (defun make-defstruct-allocation-function (name)
710 ;; FIXME: Why don't we go class->layout->info == dd
711 (let ((dd (find-defstruct-description name
)))
714 (%make-structure-instance-allocator dd nil
))
715 (funcallable-structure
716 (%make-funcallable-structure-instance-allocator dd nil
)))))
718 (defmethod shared-initialize :after
719 ((class structure-class
) slot-names
&key
720 (direct-superclasses nil direct-superclasses-p
)
721 (direct-slots nil direct-slots-p
)
722 direct-default-initargs
724 (declare (ignore slot-names direct-default-initargs
))
725 (if direct-superclasses-p
726 (setf (slot-value class
'direct-superclasses
)
727 (or direct-superclasses
728 (setq direct-superclasses
729 (and (not (eq (slot-value class
'name
) 'structure-object
))
730 (list *the-class-structure-object
*)))))
731 (setq direct-superclasses
(slot-value class
'direct-superclasses
)))
732 (let* ((name (slot-value class
'name
))
733 (from-defclass-p (slot-value class
'from-defclass-p
))
734 (defstruct-p (or from-defclass-p
(not (structure-type-p name
)))))
736 (setf (slot-value class
'direct-slots
)
740 (let* ((slot-name (getf pl
:name
))
742 (format-symbol *package
*
743 "~S structure class ~A"
745 (setq pl
(list* :defstruct-accessor-symbol
747 (make-direct-slotd class pl
))
749 (setq direct-slots
(slot-value class
'direct-slots
)))
751 (let ((include (car (slot-value class
'direct-superclasses
))))
752 (multiple-value-bind (defstruct-form constructor reader-names writer-names
)
753 (make-structure-class-defstruct-form name direct-slots include
)
754 (unless (structure-type-p name
) (eval defstruct-form
))
755 (mapc (lambda (dslotd reader-name writer-name
)
756 (let* ((reader (gdefinition reader-name
))
757 (writer (when (fboundp writer-name
)
758 (gdefinition writer-name
))))
759 (setf (slot-value dslotd
'internal-reader-function
)
761 (setf (slot-value dslotd
'internal-writer-function
)
763 direct-slots reader-names writer-names
)
764 (setf (slot-value class
'defstruct-form
) defstruct-form
)
765 (setf (slot-value class
'defstruct-constructor
) constructor
)))
766 (setf (slot-value class
'defstruct-constructor
)
767 ;; KLUDGE: not class; in fixup.lisp, can't access slots
768 ;; outside methods yet.
769 (make-defstruct-allocation-function name
)))
770 (add-direct-subclasses class direct-superclasses
)
771 (setf (slot-value class
'%class-precedence-list
)
772 (compute-class-precedence-list class
))
773 (setf (slot-value class
'cpl-available-p
) t
)
774 (let ((slots (compute-slots class
)))
775 (setf (slot-value class
'slots
) slots
)
776 (let* ((lclass (find-classoid (slot-value class
'name
)))
777 (layout (classoid-layout lclass
)))
778 (setf (classoid-pcl-class lclass
) class
)
779 (setf (slot-value class
'wrapper
) layout
)
780 (setf (layout-slot-table layout
) (make-slot-table class slots
))))
781 (setf (slot-value class
'finalized-p
) t
)
782 (add-slot-accessors class direct-slots definition-source
)))
784 (defmethod direct-slot-definition-class ((class structure-class
) &rest initargs
)
785 (declare (ignore initargs
))
786 (find-class 'structure-direct-slot-definition
))
788 (defmethod finalize-inheritance ((class structure-class
))
789 nil
) ; always finalized
791 (defun add-slot-accessors (class dslotds
&optional source-location
)
792 (fix-slot-accessors class dslotds
'add source-location
))
794 (defun remove-slot-accessors (class dslotds
)
795 (fix-slot-accessors class dslotds
'remove
))
797 (defun fix-slot-accessors (class dslotds add
/remove
&optional source-location
)
798 (flet ((fix (gfspec name r
/w doc
)
799 (let ((gf (cond ((eq add
/remove
'add
)
800 (or (find-generic-function gfspec nil
)
801 (ensure-generic-function
802 gfspec
:lambda-list
(case r
/w
804 (w '(new-value object
))))))
806 (find-generic-function gfspec nil
)))))
809 (r (if (eq add
/remove
'add
)
810 (add-reader-method class gf name doc source-location
)
811 (remove-reader-method class gf
)))
812 (w (if (eq add
/remove
'add
)
813 (add-writer-method class gf name doc source-location
)
814 (remove-writer-method class gf
))))))))
815 (dolist (dslotd dslotds
)
816 (let ((slot-name (slot-definition-name dslotd
))
817 (slot-doc (%slot-definition-documentation dslotd
)))
818 (dolist (r (slot-definition-readers dslotd
))
819 (fix r slot-name
'r slot-doc
))
820 (dolist (w (slot-definition-writers dslotd
))
821 (fix w slot-name
'w slot-doc
))))))
823 (defun add-direct-subclasses (class supers
)
824 (dolist (super supers
)
825 (unless (memq class
(class-direct-subclasses class
))
826 (add-direct-subclass super class
))))
828 (defmethod finalize-inheritance ((class std-class
))
829 (update-class class t
))
831 (defmethod finalize-inheritance ((class forward-referenced-class
))
832 ;; FIXME: should we not be thinking a bit about what kinds of error
833 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
834 ;; possibly a forward-referenced-class-error, though that's
835 ;; difficult given e.g. class precedence list calculations...
837 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
842 (defun class-has-a-forward-referenced-superclass-p (class)
843 (or (forward-referenced-class-p class
)
844 (some #'class-has-a-forward-referenced-superclass-p
845 (class-direct-superclasses class
))))
847 ;;; This is called by :after shared-initialize whenever a class is initialized
848 ;;; or reinitialized. The class may or may not be finalized.
849 (defun update-class (class finalizep
)
850 (without-package-locks
852 (when (or finalizep
(class-finalized-p class
))
853 (%update-cpl class
(compute-class-precedence-list class
))
854 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
856 (%update-slots class
(compute-slots class
))
857 (update-gfs-of-class class
)
858 (setf (plist-value class
'default-initargs
) (compute-default-initargs class
))
859 (update-ctors 'finalize-inheritance
:class class
))
860 (dolist (sub (class-direct-subclasses class
))
861 (update-class sub nil
)))))
863 (define-condition cpl-protocol-violation
(reference-condition error
)
864 ((class :initarg
:class
:reader cpl-protocol-violation-class
)
865 (cpl :initarg
:cpl
:reader cpl-protocol-violation-cpl
))
866 (:default-initargs
:references
(list '(:sbcl
:node
"Metaobject Protocol")))
869 (format s
"~@<Protocol violation: the ~S class ~S ~
870 ~:[has~;does not have~] the class ~S in its ~
871 class precedence list: ~S.~@:>"
872 (class-name (class-of (cpl-protocol-violation-class c
)))
873 (cpl-protocol-violation-class c
)
874 (eq (class-of (cpl-protocol-violation-class c
))
875 *the-class-funcallable-standard-class
*)
876 (find-class 'function
)
877 (cpl-protocol-violation-cpl c
)))))
879 (defun %update-cpl
(class cpl
)
880 (when (eq (class-of class
) *the-class-standard-class
*)
881 (when (find (find-class 'function
) cpl
)
882 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
883 (when (eq (class-of class
) *the-class-funcallable-standard-class
*)
884 (unless (find (find-class 'function
) cpl
)
885 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
886 (if (class-finalized-p class
)
887 (unless (and (equal (class-precedence-list class
) cpl
)
889 (when (position :class
(class-direct-slots c
)
890 :key
#'slot-definition-allocation
)
892 ;; comment from the old CMU CL sources:
893 ;; Need to have the cpl setup before %update-lisp-class-layout
894 ;; is called on CMU CL.
895 (setf (slot-value class
'%class-precedence-list
) cpl
)
896 (setf (slot-value class
'cpl-available-p
) t
)
897 (%force-cache-flushes class
))
899 (setf (slot-value class
'%class-precedence-list
) cpl
)
900 (setf (slot-value class
'cpl-available-p
) t
)))
901 (update-class-can-precede-p cpl
))
903 (defun update-class-can-precede-p (cpl)
905 (let ((first (car cpl
)))
906 (dolist (c (cdr cpl
))
907 (pushnew c
(slot-value first
'can-precede-list
) :test
#'eq
)))
908 (update-class-can-precede-p (cdr cpl
))))
910 (defun class-can-precede-p (class1 class2
)
911 (member class2
(class-can-precede-list class1
) :test
#'eq
))
913 (defun %update-slots
(class eslotds
)
914 (let ((instance-slots ())
916 (dolist (eslotd eslotds
)
917 (let ((alloc (slot-definition-allocation eslotd
)))
919 (:instance
(push eslotd instance-slots
))
920 (:class
(push eslotd class-slots
)))))
922 ;; If there is a change in the shape of the instances then the
923 ;; old class is now obsolete.
924 (let* ((nlayout (mapcar #'slot-definition-name
925 (sort instance-slots
#'<
926 :key
#'slot-definition-location
)))
927 (nslots (length nlayout
))
928 (nwrapper-class-slots (compute-class-slots class-slots
))
929 (owrapper (when (class-finalized-p class
)
930 (class-wrapper class
)))
931 (olayout (when owrapper
932 (wrapper-instance-slots-layout owrapper
)))
933 (owrapper-class-slots (and owrapper
(wrapper-class-slots owrapper
)))
935 (cond ((null owrapper
)
936 (make-wrapper nslots class
))
937 ((and (equal nlayout olayout
)
939 (loop for o in owrapper-class-slots
940 for n in nwrapper-class-slots
941 do
(unless (eq (car o
) (car n
)) (return t
)))))
944 ;; This will initialize the new wrapper to have the
945 ;; same state as the old wrapper. We will then have
946 ;; to change that. This may seem like wasted work
947 ;; (and it is), but the spec requires that we call
948 ;; MAKE-INSTANCES-OBSOLETE.
949 (make-instances-obsolete class
)
950 (class-wrapper class
)))))
952 (%update-lisp-class-layout class nwrapper
)
953 (setf (slot-value class
'slots
) eslotds
954 (wrapper-slot-table nwrapper
) (make-slot-table class eslotds
)
955 (wrapper-instance-slots-layout nwrapper
) nlayout
956 (wrapper-class-slots nwrapper
) nwrapper-class-slots
957 (wrapper-length nwrapper
) nslots
958 (slot-value class
'wrapper
) nwrapper
)
959 (do* ((slots (slot-value class
'slots
) (cdr slots
))
964 "~@<slot names with the same SYMBOL-NAME but ~
965 different SYMBOL-PACKAGE (possible package problem) ~
966 for class ~S:~4I~@:_~<~@{~S~^~:@_~}~:>~@:>"
968 (let* ((slot (car slots
))
969 (oslots (remove (slot-definition-name slot
) (cdr slots
)
971 :key
#'slot-definition-name
)))
973 (pushnew (cons (slot-definition-name slot
)
974 (mapcar #'slot-definition-name oslots
))
976 :test
#'string
= :key
#'car
))))
977 (setf (slot-value class
'finalized-p
) t
)
978 (unless (eq owrapper nwrapper
)
979 (maybe-update-standard-slot-locations class
)))))
981 (defun compute-class-slots (eslotds)
983 (dolist (eslotd eslotds
(nreverse collect
))
984 (let ((cell (assoc (slot-definition-name eslotd
)
986 (slot-definition-allocation-class eslotd
)))))
988 (push cell collect
)))))
990 (defun update-gf-dfun (class gf
)
991 (let ((*new-class
* class
)
992 (arg-info (gf-arg-info gf
)))
994 ((special-case-for-compute-discriminating-function-p gf
))
995 ((gf-precompute-dfun-and-emf-p arg-info
)
996 (multiple-value-bind (dfun cache info
) (make-final-dfun-internal gf
)
997 (update-dfun gf dfun cache info
))))))
999 (defun update-gfs-of-class (class)
1000 (when (and (class-finalized-p class
)
1001 (let ((cpl (class-precedence-list class
)))
1002 (or (member *the-class-slot-class
* cpl
:test
#'eq
)
1003 (member *the-class-standard-effective-slot-definition
*
1005 (let ((gf-table (make-hash-table :test
'eq
)))
1006 (labels ((collect-gfs (class)
1007 (dolist (gf (specializer-direct-generic-functions class
))
1008 (setf (gethash gf gf-table
) t
))
1009 (mapc #'collect-gfs
(class-direct-superclasses class
))))
1011 (maphash (lambda (gf ignore
)
1012 (declare (ignore ignore
))
1013 (update-gf-dfun class gf
))
1016 (defmethod compute-default-initargs ((class slot-class
))
1017 (let ((initargs (loop for c in
(class-precedence-list class
)
1018 append
(class-direct-default-initargs c
))))
1019 (delete-duplicates initargs
:test
#'eq
:key
#'car
:from-end t
)))
1021 ;;;; protocols for constructing direct and effective slot definitions
1023 (defmethod direct-slot-definition-class ((class std-class
) &rest initargs
)
1024 (declare (ignore initargs
))
1025 (find-class 'standard-direct-slot-definition
))
1027 (defun make-direct-slotd (class initargs
)
1028 (apply #'make-instance
1029 (apply #'direct-slot-definition-class class initargs
)
1033 ;;; I (CSR) am not sure, but I believe that the particular order of
1034 ;;; slots is quite important: it is ideal to attempt to have a
1035 ;;; constant slot location for the same notional slots as much as
1036 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1037 ;;; al.) have a chance of working. The below at least walks through
1038 ;;; the slots predictably, but maybe it would be good to compute some
1039 ;;; kind of optimal slot layout by looking at locations of slots in
1041 (defun std-compute-slots (class)
1042 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1043 ;; for each different slot name we find in our superclasses. Each
1044 ;; call receives the class and a list of the dslotds with that name.
1045 ;; The list is in most-specific-first order.
1046 (let ((name-dslotds-alist ()))
1047 (dolist (c (reverse (class-precedence-list class
)))
1048 (dolist (slot (class-direct-slots c
))
1049 (let* ((name (slot-definition-name slot
))
1050 (entry (assq name name-dslotds-alist
)))
1052 (push slot
(cdr entry
))
1053 (push (list name slot
) name-dslotds-alist
)))))
1054 (mapcar (lambda (direct)
1055 (compute-effective-slot-definition class
1058 (nreverse name-dslotds-alist
))))
1060 (defmethod compute-slots ((class standard-class
))
1061 (std-compute-slots class
))
1062 (defmethod compute-slots ((class funcallable-standard-class
))
1063 (std-compute-slots class
))
1065 (defun std-compute-slots-around (class eslotds
)
1067 (safe (safe-p class
)))
1068 (dolist (eslotd eslotds eslotds
)
1069 (setf (slot-definition-location eslotd
)
1070 (case (slot-definition-allocation eslotd
)
1074 (let* ((name (slot-definition-name eslotd
))
1077 (slot-definition-allocation-class eslotd
)
1078 ;; we get here if the user adds an extra slot
1080 (setf (slot-definition-allocation-class eslotd
)
1082 ;; which raises the question of what we should
1083 ;; do if we find that said user has added a slot
1084 ;; with the same name as another slot...
1085 (cell (or (assq name
(class-slot-cells from-class
))
1086 (let ((c (cons name
+slot-unbound
+)))
1087 (push c
(class-slot-cells from-class
))
1090 (if (eq +slot-unbound
+ (cdr cell
))
1091 ;; We may have inherited an initfunction FIXME: Is this
1092 ;; really right? Is the initialization in
1093 ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1094 (let ((initfun (slot-definition-initfunction eslotd
)))
1096 (rplacd cell
(call-initfun initfun eslotd safe
))
1099 (unless (slot-definition-class eslotd
)
1100 (setf (slot-definition-class eslotd
) class
))
1101 (initialize-internal-slot-functions eslotd
))))
1103 (defmethod compute-slots :around
((class standard-class
))
1104 (let ((eslotds (call-next-method)))
1105 (std-compute-slots-around class eslotds
)))
1106 (defmethod compute-slots :around
((class funcallable-standard-class
))
1107 (let ((eslotds (call-next-method)))
1108 (std-compute-slots-around class eslotds
)))
1110 (defmethod compute-slots ((class structure-class
))
1111 (mapcan (lambda (superclass)
1112 (mapcar (lambda (dslotd)
1113 (compute-effective-slot-definition
1115 (slot-definition-name dslotd
)
1117 (class-direct-slots superclass
)))
1118 (reverse (slot-value class
'%class-precedence-list
))))
1120 (defmethod compute-slots :around
((class structure-class
))
1121 (let ((eslotds (call-next-method)))
1122 (mapc #'finalize-internal-slot-functions eslotds
)
1125 (defmethod compute-effective-slot-definition ((class slot-class
) name dslotds
)
1126 (declare (ignore name
))
1127 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds
))
1128 (class (apply #'effective-slot-definition-class class initargs
)))
1129 (apply #'make-instance class initargs
)))
1131 (defmethod effective-slot-definition-class ((class std-class
) &rest initargs
)
1132 (declare (ignore initargs
))
1133 (find-class 'standard-effective-slot-definition
))
1135 (defmethod effective-slot-definition-class ((class structure-class
) &rest initargs
)
1136 (declare (ignore initargs
))
1137 (find-class 'structure-effective-slot-definition
))
1139 (defmethod compute-effective-slot-definition-initargs
1140 ((class slot-class
) direct-slotds
)
1146 (allocation-class nil
)
1148 (type-check-function nil
)
1150 (documentationp nil
)
1155 (dolist (slotd direct-slotds
)
1158 (setq name
(slot-definition-name slotd
)
1161 (awhen (slot-definition-initfunction slotd
)
1162 (setq initform
(slot-definition-initform slotd
)
1165 (unless documentationp
1166 (awhen (%slot-definition-documentation slotd
)
1167 (setq documentation it
1170 (setq allocation
(slot-definition-allocation slotd
)
1171 allocation-class
(slot-definition-class slotd
)
1173 (setq initargs
(append (slot-definition-initargs slotd
) initargs
))
1174 (let ((fun (slot-definition-type-check-function slotd
)))
1176 (setf type-check-function
1177 (if type-check-function
1178 (let ((old-function type-check-function
))
1179 (declare (function old-function fun
))
1181 (funcall old-function value
)
1182 (funcall fun value
)))
1184 (let ((slotd-type (slot-definition-type slotd
)))
1186 ((eq type t
) slotd-type
)
1187 ;; This pairwise type intersection is perhaps a
1188 ;; little inefficient and inelegant, but it's
1189 ;; unlikely to lie on the critical path. Shout
1190 ;; if I'm wrong. -- CSR, 2005-11-24
1192 (specifier-type `(and ,type
,slotd-type
)))))))))
1195 :initfunction initfunction
1197 :allocation allocation
1198 :allocation-class allocation-class
1200 'type-check-function type-check-function
1202 :documentation documentation
)))
1204 (defmethod compute-effective-slot-definition-initargs :around
1205 ((class structure-class
) direct-slotds
)
1206 (let ((slotd (car direct-slotds
)))
1207 (list* :defstruct-accessor-symbol
1208 (slot-definition-defstruct-accessor-symbol slotd
)
1209 :internal-reader-function
1210 (slot-definition-internal-reader-function slotd
)
1211 :internal-writer-function
1212 (slot-definition-internal-writer-function slotd
)
1213 (call-next-method))))
1215 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1216 ;;; to make the method object. They have to use make-a-method which
1217 ;;; is a specially bootstrapped mechanism for making standard methods.
1218 (defmethod reader-method-class ((class slot-class
) direct-slot
&rest initargs
)
1219 (declare (ignore direct-slot initargs
))
1220 (find-class 'standard-reader-method
))
1222 (defmethod add-reader-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1223 (add-method generic-function
1224 (make-a-method 'standard-reader-method
1226 (list (or (class-name class
) 'object
))
1228 (make-reader-method-function class slot-name
)
1229 (or slot-documentation
"automatically generated reader method")
1230 :slot-name slot-name
1232 :method-class-function
#'reader-method-class
1233 :definition-source source-location
)))
1235 (defmethod writer-method-class ((class slot-class
) direct-slot
&rest initargs
)
1236 (declare (ignore direct-slot initargs
))
1237 (find-class 'standard-writer-method
))
1239 (defmethod add-writer-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1240 (add-method generic-function
1241 (make-a-method 'standard-writer-method
1243 (list 'new-value
(or (class-name class
) 'object
))
1244 (list *the-class-t
* class
)
1245 (make-writer-method-function class slot-name
)
1246 (or slot-documentation
"automatically generated writer method")
1247 :slot-name slot-name
1249 :method-class-function
#'writer-method-class
1250 :definition-source source-location
)))
1252 (defmethod add-boundp-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1253 (add-method generic-function
1254 (make-a-method (constantly (find-class 'standard-boundp-method
))
1257 (list (or (class-name class
) 'object
))
1259 (make-boundp-method-function class slot-name
)
1260 (or slot-documentation
"automatically generated boundp method")
1261 :slot-name slot-name
1262 :definition-source source-location
)))
1264 (defmethod remove-reader-method ((class slot-class
) generic-function
)
1265 (let ((method (get-method generic-function
() (list class
) nil
)))
1266 (when method
(remove-method generic-function method
))))
1268 (defmethod remove-writer-method ((class slot-class
) generic-function
)
1270 (get-method generic-function
() (list *the-class-t
* class
) nil
)))
1271 (when method
(remove-method generic-function method
))))
1273 (defmethod remove-boundp-method ((class slot-class
) generic-function
)
1274 (let ((method (get-method generic-function
() (list class
) nil
)))
1275 (when method
(remove-method generic-function method
))))
1277 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1278 ;;; function are NOT part of the standard protocol. They are however
1279 ;;; useful; PCL makes use of them internally and documents them for
1280 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1282 ;;; *** This needs work to make type testing by the writer functions which
1283 ;;; *** do type testing faster. The idea would be to have one constructor
1284 ;;; *** for each possible type test.
1286 ;;; *** There is a subtle bug here which is going to have to be fixed.
1287 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1288 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1289 ;;; *** defined for this metaclass a chance to run.
1291 (defmethod make-reader-method-function ((class slot-class
) slot-name
)
1292 (make-std-reader-method-function class slot-name
))
1294 (defmethod make-writer-method-function ((class slot-class
) slot-name
)
1295 (make-std-writer-method-function class slot-name
))
1297 (defmethod make-boundp-method-function ((class slot-class
) slot-name
)
1298 (make-std-boundp-method-function class slot-name
))
1300 (defmethod compatible-meta-class-change-p (class proto-new-class
)
1301 (eq (class-of class
) (class-of proto-new-class
)))
1303 (defmethod validate-superclass ((class class
) (superclass class
))
1304 (or (eq superclass
*the-class-t
*)
1305 (eq (class-of class
) (class-of superclass
))
1306 (and (eq (class-of superclass
) *the-class-standard-class
*)
1307 (eq (class-of class
) *the-class-funcallable-standard-class
*))
1308 (and (eq (class-of superclass
) *the-class-funcallable-standard-class
*)
1309 (eq (class-of class
) *the-class-standard-class
*))))
1311 ;;; What this does depends on which of the four possible values of
1312 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1313 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1314 ;;; nothing to do, as the new wrapper has already been created. If
1315 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1316 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1317 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1319 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1320 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1321 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1322 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1323 ;;; obsolete the wrapper.
1325 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1326 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1327 ;;; :UNINITIALIZED)))
1329 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1330 (defun %force-cache-flushes
(class)
1331 (let* ((owrapper (class-wrapper class
)))
1332 ;; We only need to do something if the wrapper is still valid. If
1333 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1334 ;; both of those will already be doing what we want. In
1335 ;; particular, we must be sure we never change an OBSOLETE into a
1336 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1337 (when (or (not (invalid-wrapper-p owrapper
))
1338 ;; KLUDGE: despite the observations above, this remains
1339 ;; a violation of locality or what might be considered
1340 ;; good style. There has to be a better way! -- CSR,
1342 (eq (layout-invalid owrapper
) t
))
1343 (let ((nwrapper (make-wrapper (layout-length owrapper
)
1345 (setf (wrapper-instance-slots-layout nwrapper
)
1346 (wrapper-instance-slots-layout owrapper
))
1347 (setf (wrapper-class-slots nwrapper
)
1348 (wrapper-class-slots owrapper
))
1349 (setf (wrapper-slot-table nwrapper
)
1350 (wrapper-slot-table owrapper
))
1351 (%update-lisp-class-layout class nwrapper
)
1352 (setf (slot-value class
'wrapper
) nwrapper
)
1353 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1355 (if (find-if (lambda (x)
1356 (and (consp x
) (eq :obsolete
(car x
))))
1357 (layout-inherits owrapper
)
1358 :key
#'layout-invalid
)
1359 (%invalidate-wrapper owrapper
:obsolete nwrapper
)
1360 (%invalidate-wrapper owrapper
:flush nwrapper
))))))
1362 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1363 ;;; the next access to the instance (as defined in 88-002R) to trap
1364 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1365 (defmethod make-instances-obsolete ((class std-class
))
1367 (let* ((owrapper (class-wrapper class
))
1368 (nwrapper (make-wrapper (layout-length owrapper
)
1370 (unless (class-finalized-p class
)
1371 (if (class-has-a-forward-referenced-superclass-p class
)
1372 (return-from make-instances-obsolete class
)
1373 (%update-cpl class
(compute-class-precedence-list class
))))
1374 (setf (wrapper-instance-slots-layout nwrapper
)
1375 (wrapper-instance-slots-layout owrapper
))
1376 (setf (wrapper-class-slots nwrapper
)
1377 (wrapper-class-slots owrapper
))
1378 (setf (wrapper-slot-table nwrapper
)
1379 (wrapper-slot-table owrapper
))
1380 (%update-lisp-class-layout class nwrapper
)
1381 (setf (slot-value class
'wrapper
) nwrapper
)
1382 (%invalidate-wrapper owrapper
:obsolete nwrapper
)
1385 (defmethod make-instances-obsolete ((class symbol
))
1386 (make-instances-obsolete (find-class class
))
1387 ;; ANSI wants the class name when called with a symbol.
1390 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1391 ;;; see an obsolete instance. The times when it is called are:
1392 ;;; - when the instance is involved in method lookup
1393 ;;; - when attempting to access a slot of an instance
1395 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1396 ;;; instance access macros.
1398 ;;; Of course these times when it is called are an internal
1399 ;;; implementation detail of PCL and are not part of the documented
1400 ;;; description of when the obsolete instance update happens. The
1401 ;;; documented description is as it appears in 88-002R.
1403 ;;; This has to return the new wrapper, so it counts on all the
1404 ;;; methods on obsolete-instance-trap-internal to return the new
1405 ;;; wrapper. It also does a little internal error checking to make
1406 ;;; sure that the traps are only happening when they should, and that
1407 ;;; the trap methods are computing appropriate new wrappers.
1409 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1410 ;;; after a structure is redefined. In most cases,
1411 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1412 ;;; so it must signal an error. The hard part of this is that the
1413 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1414 ;;; called again, so in that case, we have to return some reasonable
1415 ;;; wrapper, instead.
1417 (defvar *in-obsolete-instance-trap
* nil
)
1418 (defvar *the-wrapper-of-structure-object
*
1419 (class-wrapper (find-class 'structure-object
)))
1421 (define-condition obsolete-structure
(error)
1422 ((datum :reader obsolete-structure-datum
:initarg
:datum
))
1424 (lambda (condition stream
)
1425 ;; Don't try to print the structure, since it probably won't work.
1427 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1428 (type-of (obsolete-structure-datum condition
))))))
1430 (defun %obsolete-instance-trap
(owrapper nwrapper instance
)
1431 (if (not (layout-for-std-class-p owrapper
))
1432 (if *in-obsolete-instance-trap
*
1433 *the-wrapper-of-structure-object
*
1434 (let ((*in-obsolete-instance-trap
* t
))
1435 (error 'obsolete-structure
:datum instance
)))
1436 (let* ((class (wrapper-class* nwrapper
))
1437 (copy (allocate-instance class
)) ;??? allocate-instance ???
1438 (olayout (wrapper-instance-slots-layout owrapper
))
1439 (nlayout (wrapper-instance-slots-layout nwrapper
))
1440 (oslots (get-slots instance
))
1441 (nslots (get-slots copy
))
1442 (oclass-slots (wrapper-class-slots owrapper
))
1447 ;; local --> local transfer value
1448 ;; local --> shared discard value, discard slot
1449 ;; local --> -- discard slot
1450 ;; shared --> local transfer value
1451 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1452 ;; shared --> -- discard value
1453 ;; -- --> local add slot
1456 ;; Go through all the old local slots.
1458 (dolist (name olayout
)
1459 (let ((npos (posq name nlayout
)))
1461 (setf (clos-slots-ref nslots npos
)
1462 (clos-slots-ref oslots opos
))
1464 (push name discarded
)
1465 (unless (eq (clos-slots-ref oslots opos
) +slot-unbound
+)
1466 (setf (getf plist name
) (clos-slots-ref oslots opos
))))))
1469 ;; Go through all the old shared slots.
1470 (dolist (oclass-slot-and-val oclass-slots
)
1471 (let ((name (car oclass-slot-and-val
))
1472 (val (cdr oclass-slot-and-val
)))
1473 (let ((npos (posq name nlayout
)))
1475 (setf (clos-slots-ref nslots npos
) val
)))))
1477 ;; Go through all the new local slots to compute the added slots.
1478 (dolist (nlocal nlayout
)
1479 (unless (or (memq nlocal olayout
)
1480 (assq nlocal oclass-slots
))
1481 (push nlocal added
)))
1483 (%swap-wrappers-and-slots instance copy
)
1485 (update-instance-for-redefined-class instance
1491 (defun %change-class
(instance new-class initargs
)
1492 (let* ((old-class (class-of instance
))
1493 (copy (allocate-instance new-class
))
1494 (new-wrapper (get-wrapper copy
))
1495 (old-wrapper (class-wrapper old-class
))
1496 (old-layout (wrapper-instance-slots-layout old-wrapper
))
1497 (new-layout (wrapper-instance-slots-layout new-wrapper
))
1498 (old-slots (get-slots instance
))
1499 (new-slots (get-slots copy
))
1500 (old-class-slots (wrapper-class-slots old-wrapper
)))
1502 ;; "The values of local slots specified by both the class CTO and
1503 ;; CFROM are retained. If such a local slot was unbound, it
1504 ;; remains unbound."
1505 (let ((new-position 0))
1506 (dolist (new-slot new-layout
)
1507 (let ((old-position (posq new-slot old-layout
)))
1509 (setf (clos-slots-ref new-slots new-position
)
1510 (clos-slots-ref old-slots old-position
))))
1511 (incf new-position
)))
1513 ;; "The values of slots specified as shared in the class CFROM and
1514 ;; as local in the class CTO are retained."
1515 (dolist (slot-and-val old-class-slots
)
1516 (let ((position (posq (car slot-and-val
) new-layout
)))
1518 (setf (clos-slots-ref new-slots position
) (cdr slot-and-val
)))))
1520 ;; Make the copy point to the old instance's storage, and make the
1521 ;; old instance point to the new storage.
1522 (%swap-wrappers-and-slots instance copy
)
1524 (apply #'update-instance-for-different-class copy instance initargs
)
1528 (defmethod change-class ((instance standard-object
) (new-class standard-class
)
1531 (unless (class-finalized-p new-class
)
1532 (finalize-inheritance new-class
))
1533 (let ((cpl (class-precedence-list new-class
)))
1537 `(when (eq class
(find-class ',class-name
))
1538 (error 'metaobject-initialization-violation
1539 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1540 :format-arguments
(list 'change-class
',class-name
)
1541 :references
(list '(:amop
:initialization
,class-name
))))))
1543 (frob generic-function
)
1545 (frob slot-definition
))))
1546 (%change-class instance new-class initargs
)))
1548 (defmethod change-class ((instance forward-referenced-class
)
1549 (new-class standard-class
) &rest initargs
)
1551 (let ((cpl (class-precedence-list new-class
)))
1553 (error 'metaobject-initialization-violation
1555 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1557 (list 'change-class
'forward-referenced-class
'class
)
1559 (list '(:amop
:generic-function ensure-class-using-class
)
1560 '(:amop
:initialization class
))))
1561 (when (eq class
(find-class 'class
))
1563 (%change-class instance new-class initargs
)))
1565 (defmethod change-class ((instance funcallable-standard-object
)
1566 (new-class funcallable-standard-class
)
1569 (let ((cpl (class-precedence-list new-class
)))
1573 `(when (eq class
(find-class ',class-name
))
1574 (error 'metaobject-initialization-violation
1575 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1576 :format-arguments
(list 'change-class
',class-name
)
1577 :references
(list '(:amop
:initialization
,class-name
))))))
1579 (frob generic-function
)
1581 (frob slot-definition
))))
1582 (%change-class instance new-class initargs
)))
1584 (defmethod change-class ((instance standard-object
)
1585 (new-class funcallable-standard-class
)
1587 (declare (ignore initargs
))
1588 (error "You can't change the class of ~S to ~S~@
1589 because it isn't already an instance with metaclass ~S."
1590 instance new-class
'standard-class
))
1592 (defmethod change-class ((instance funcallable-standard-object
)
1593 (new-class standard-class
)
1595 (declare (ignore initargs
))
1596 (error "You can't change the class of ~S to ~S~@
1597 because it isn't already an instance with metaclass ~S."
1598 instance new-class
'funcallable-standard-class
))
1600 (defmethod change-class ((instance t
) (new-class-name symbol
) &rest initargs
)
1601 (apply #'change-class instance
(find-class new-class-name
) initargs
))
1603 ;;;; The metaclass BUILT-IN-CLASS
1605 ;;;; This metaclass is something of a weird creature. By this point, all
1606 ;;;; instances of it which will exist have been created, and no instance
1607 ;;;; is ever created by calling MAKE-INSTANCE.
1609 ;;;; But, there are other parts of the protocol we must follow and those
1610 ;;;; definitions appear here.
1612 (macrolet ((def (name args control
)
1613 `(defmethod ,name
,args
1614 (declare (ignore initargs
))
1615 (error 'metaobject-initialization-violation
1616 :format-control
,(format nil
"~@<~A~@:>" control
)
1617 :format-arguments
(list ',name
)
1618 :references
(list '(:amop
:initialization
"Class"))))))
1619 (def initialize-instance
((class built-in-class
) &rest initargs
)
1620 "Cannot ~S an instance of BUILT-IN-CLASS.")
1621 (def reinitialize-instance
((class built-in-class
) &rest initargs
)
1622 "Cannot ~S an instance of BUILT-IN-CLASS."))
1624 (macrolet ((def (name)
1625 `(defmethod ,name
((class built-in-class
)) nil
)))
1626 (def class-direct-slots
)
1628 (def class-direct-default-initargs
)
1629 (def class-default-initargs
))
1631 (defmethod validate-superclass ((c class
) (s built-in-class
))
1632 (or (eq s
*the-class-t
*) (eq s
*the-class-stream
*)
1633 ;; FIXME: bad things happen if someone tries to mix in both
1634 ;; FILE-STREAM and STRING-STREAM (as they have the same
1635 ;; layout-depthoid). Is there any way we can provide a useful
1636 ;; error message? -- CSR, 2005-05-03
1637 (eq s
*the-class-file-stream
*) (eq s
*the-class-string-stream
*)
1638 ;; This probably shouldn't be mixed in with certain other
1639 ;; classes, too, but it seems to work both with STANDARD-OBJECT
1640 ;; and FUNCALLABLE-STANDARD-OBJECT
1641 (eq s
*the-class-sequence
*)))
1643 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1644 (defmethod class-direct-slots ((class forward-referenced-class
)) ())
1645 (defmethod class-direct-default-initargs ((class forward-referenced-class
)) ())
1646 (macrolet ((def (method)
1647 `(defmethod ,method
((class forward-referenced-class
))
1648 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1650 (def class-default-initargs
)
1651 (def class-precedence-list
)
1654 (defmethod validate-superclass ((c slot-class
)
1655 (f forward-referenced-class
))
1658 (defmethod add-dependent ((metaobject dependent-update-mixin
) dependent
)
1659 (pushnew dependent
(plist-value metaobject
'dependents
) :test
#'eq
))
1661 (defmethod remove-dependent ((metaobject dependent-update-mixin
) dependent
)
1662 (setf (plist-value metaobject
'dependents
)
1663 (delete dependent
(plist-value metaobject
'dependents
))))
1665 (defmethod map-dependents ((metaobject dependent-update-mixin
) function
)
1666 (dolist (dependent (plist-value metaobject
'dependents
))
1667 (funcall function dependent
)))