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
)
27 (let ((info (slot-definition-info slotd
)))
29 (reader (slot-info-reader info
))
30 (writer (slot-info-writer info
))
31 (boundp (slot-info-boundp info
)))))
33 (defmethod (setf slot-accessor-function
) (function
34 (slotd effective-slot-definition
)
36 (let ((info (slot-definition-info slotd
)))
38 (reader (setf (slot-info-reader info
) function
))
39 (writer (setf (slot-info-writer info
) function
))
40 (boundp (setf (slot-info-boundp info
) function
)))))
42 (defconstant +slotd-reader-function-std-p
+ 1)
43 (defconstant +slotd-writer-function-std-p
+ 2)
44 (defconstant +slotd-boundp-function-std-p
+ 4)
45 (defconstant +slotd-all-function-std-p
+ 7)
47 (defmethod slot-accessor-std-p ((slotd effective-slot-definition
) type
)
48 (let ((flags (slot-value slotd
'accessor-flags
)))
49 (declare (type fixnum flags
))
51 (eql +slotd-all-function-std-p
+ flags
)
52 (let ((mask (ecase type
53 (reader +slotd-reader-function-std-p
+)
54 (writer +slotd-writer-function-std-p
+)
55 (boundp +slotd-boundp-function-std-p
+))))
56 (declare (type fixnum mask
))
57 (not (zerop (the fixnum
(logand mask flags
))))))))
59 (defmethod (setf slot-accessor-std-p
) (value
60 (slotd effective-slot-definition
)
62 (let ((mask (ecase type
63 (reader +slotd-reader-function-std-p
+)
64 (writer +slotd-writer-function-std-p
+)
65 (boundp +slotd-boundp-function-std-p
+)))
66 (flags (slot-value slotd
'accessor-flags
)))
67 (declare (type fixnum mask flags
))
68 (setf (slot-value slotd
'accessor-flags
)
70 (the fixnum
(logior mask flags
))
71 (the fixnum
(logand (the fixnum
(lognot mask
)) flags
)))))
74 (defmethod initialize-internal-slot-functions
75 ((slotd effective-slot-definition
))
76 (let* ((name (slot-value slotd
'name
)) ; flushable? (is it ever unbound?)
77 (class (slot-value slotd
'%class
)))
78 (declare (ignore name
))
79 (dolist (type '(reader writer boundp
))
80 (let* ((gf-name (ecase type
81 (reader 'slot-value-using-class
)
82 (writer '(setf slot-value-using-class
))
83 (boundp 'slot-boundp-using-class
)))
84 (gf (gdefinition gf-name
)))
85 ;; KLUDGE: this logic is cut'n'pasted from
86 ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
87 ;; only called later, because it does things that can't be
88 ;; computed this early in class finalization; however, we need
89 ;; this bit as early as possible. -- CSR, 2009-11-05
90 (setf (slot-accessor-std-p slotd type
)
91 (let* ((types1 `((eql ,class
) (class-eq ,class
) (eql ,slotd
)))
92 (types (if (eq type
'writer
) `(t ,@types1
) types1
))
93 (methods (compute-applicable-methods-using-types gf types
)))
94 (null (cdr methods
))))
95 (setf (slot-accessor-function slotd type
)
97 (declare (dynamic-extent args
))
98 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
99 ;; work here (see KLUDGE comment above).
100 (let ((fun (compute-slot-accessor-info slotd type gf
)))
101 (apply fun args
))))))))
103 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition
))
104 (dolist (type '(reader writer boundp
))
105 (let* ((gf-name (ecase type
106 (reader 'slot-value-using-class
)
107 (writer '(setf slot-value-using-class
))
108 (boundp 'slot-boundp-using-class
)))
109 (gf (gdefinition gf-name
)))
110 (compute-slot-accessor-info slotd type gf
))))
112 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
114 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
115 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
116 ;;; writing/testing effective slot SLOTD.
118 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
119 ;;; GF. Store the effective method in the effective slot definition
120 ;;; object itself; these GFs have special dispatch functions calling
121 ;;; effective methods directly retrieved from effective slot
122 ;;; definition objects, as an optimization.
124 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
126 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition
)
128 (let* ((name (slot-value slotd
'name
)) ; flushable?
129 (class (slot-value slotd
'%class
)))
130 (declare (ignore name
))
131 (multiple-value-bind (function std-p
)
132 (if (eq **boot-state
** 'complete
)
133 (get-accessor-method-function gf type class slotd
)
134 (get-optimized-std-accessor-method-function class slotd type
))
135 (setf (slot-accessor-std-p slotd type
) std-p
)
136 (setf (slot-accessor-function slotd type
) function
))))
138 (defmethod slot-definition-allocation ((slotd structure-slot-definition
))
141 ;;;; various class accessors that are a little more complicated than can be
142 ;;;; done with automatically generated reader methods
144 (defmethod class-prototype :before
(class)
145 (unless (class-finalized-p class
)
146 (error "~@<~S is not finalized.~:@>" class
)))
148 ;;; KLUDGE: For some reason factoring the common body into a function
149 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
151 (macrolet ((def (class)
152 `(defmethod class-prototype ((class ,class
))
153 (with-slots (prototype) class
155 (setf prototype
(allocate-instance class
)))))))
157 (def condition-class
)
158 (def structure-class
))
160 (defmethod class-direct-default-initargs ((class slot-class
))
161 (plist-value class
'direct-default-initargs
))
163 (defmethod class-default-initargs ((class slot-class
))
164 (plist-value class
'default-initargs
))
166 (defmethod class-slot-cells ((class std-class
))
167 (plist-value class
'class-slot-cells
))
168 (defmethod (setf class-slot-cells
) (new-value (class std-class
))
169 (setf (plist-value class
'class-slot-cells
) new-value
))
171 ;;;; class accessors that are even a little bit more complicated than those
172 ;;;; above. These have a protocol for updating them, we must implement that
175 ;;; Maintaining the direct subclasses backpointers. The update methods are
176 ;;; here, the values are read by an automatically generated reader method.
177 (defmethod add-direct-subclass ((class class
) (subclass class
))
178 (with-slots (direct-subclasses) class
179 (pushnew subclass direct-subclasses
:test
#'eq
)
181 (defmethod remove-direct-subclass ((class class
) (subclass class
))
182 (with-slots (direct-subclasses) class
183 (setq direct-subclasses
(remove subclass direct-subclasses
))
186 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
188 ;;; There are four generic functions involved, each has one method for the
189 ;;; class case and another method for the damned EQL specializers. All of
190 ;;; these are specified methods and appear in their specified place in the
193 ;;; ADD-DIRECT-METHOD
194 ;;; REMOVE-DIRECT-METHOD
195 ;;; SPECIALIZER-DIRECT-METHODS
196 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
198 ;;; In each case, we maintain one value which is a cons. The car is the list
199 ;;; methods. The cdr is a list of the generic functions. The cdr is always
202 ;;; This needs to be used recursively, in case a non-trivial user
203 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
204 ;;; function using the same lock.
205 (defvar *specializer-lock
* (sb-thread:make-mutex
:name
"Specializer lock"))
207 (defmethod add-direct-method :around
((specializer specializer
) method
)
208 ;; All the actions done under this lock are done in an order
209 ;; that is safe to unwind at any point.
210 (sb-thread::with-recursive-system-lock
(*specializer-lock
*)
213 (defmethod remove-direct-method :around
((specializer specializer
) method
)
214 ;; All the actions done under this lock are done in an order
215 ;; that is safe to unwind at any point.
216 (sb-thread::with-recursive-system-lock
(*specializer-lock
*)
219 (defmethod add-direct-method ((specializer class
) (method method
))
220 (let ((cell (slot-value specializer
'direct-methods
)))
221 ;; We need to first smash the CDR, because a parallel read may
222 ;; be in progress, and because if an interrupt catches us we
223 ;; need to have a consistent state.
225 (car cell
) (adjoin method
(car cell
) :test
#'eq
)))
228 (defmethod remove-direct-method ((specializer class
) (method method
))
229 (let ((cell (slot-value specializer
'direct-methods
)))
230 ;; We need to first smash the CDR, because a parallel read may
231 ;; be in progress, and because if an interrupt catches us we
232 ;; need to have a consistent state.
234 (car cell
) (remove method
(car cell
))))
237 (defmethod specializer-direct-methods ((specializer class
))
238 (with-slots (direct-methods) specializer
239 (car direct-methods
)))
241 (defmethod specializer-direct-generic-functions ((specializer class
))
242 (let ((cell (slot-value specializer
'direct-methods
)))
243 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
244 ;; we behave as if we got just first or just after -- it's just
245 ;; for update that we need to lock.
247 (sb-thread:with-mutex
(*specializer-lock
*)
250 (dolist (m (car cell
))
251 ;; the old PCL code used COLLECTING-ONCE which used
252 ;; #'EQ to check for newness
253 (pushnew (method-generic-function m
) collect
:test
#'eq
))
254 (nreverse collect
)))))))
256 ;;; This hash table is used to store the direct methods and direct generic
257 ;;; functions of EQL specializers. Each value in the table is the cons.
259 ;;; These tables are shared between threads, so they need to be synchronized.
260 (defvar *eql-specializer-methods
* (make-hash-table :test
'eql
:synchronized t
))
261 (defvar *class-eq-specializer-methods
* (make-hash-table :test
'eq
:synchronized t
))
263 (defmethod specializer-method-table ((specializer eql-specializer
))
264 *eql-specializer-methods
*)
266 (defmethod specializer-method-table ((specializer class-eq-specializer
))
267 *class-eq-specializer-methods
*)
269 (defmethod add-direct-method ((specializer specializer-with-object
)
271 (let* ((object (specializer-object specializer
))
272 (table (specializer-method-table specializer
))
273 (entry (gethash object table
)))
276 (setf (gethash object table
) (cons nil nil
))))
277 ;; We need to first smash the CDR, because a parallel read may
278 ;; be in progress, and because if an interrupt catches us we
279 ;; need to have a consistent state.
281 (car entry
) (adjoin method
(car entry
) :test
#'eq
))
284 (defmethod remove-direct-method ((specializer specializer-with-object
)
286 (let* ((object (specializer-object specializer
))
287 (entry (gethash object
(specializer-method-table specializer
))))
289 ;; We need to first smash the CDR, because a parallel read may
290 ;; be in progress, and because if an interrupt catches us we
291 ;; need to have a consistent state.
293 (car entry
) (remove method
(car entry
))))
296 (defmethod specializer-direct-methods ((specializer specializer-with-object
))
297 (car (gethash (specializer-object specializer
)
298 (specializer-method-table specializer
))))
300 (defmethod specializer-direct-generic-functions ((specializer
301 specializer-with-object
))
302 (let* ((object (specializer-object specializer
))
303 (entry (gethash object
(specializer-method-table specializer
))))
306 (sb-thread:with-mutex
(*specializer-lock
*)
309 (dolist (m (car entry
))
310 (pushnew (method-generic-function m
) collect
:test
#'eq
))
311 (nreverse collect
))))))))
313 (defun map-specializers (function)
314 (map-all-classes (lambda (class)
315 (funcall function
(class-eq-specializer class
))
316 (funcall function class
)))
317 (maphash (lambda (object methods
)
318 (declare (ignore methods
))
319 (intern-eql-specializer object
))
320 *eql-specializer-methods
*)
321 (maphash (lambda (object specl
)
322 (declare (ignore object
))
323 (funcall function specl
))
324 *eql-specializer-table
*)
327 (defun map-all-generic-functions (function)
328 (let ((all-generic-functions (make-hash-table :test
'eq
)))
329 (map-specializers (lambda (specl)
330 (dolist (gf (specializer-direct-generic-functions
332 (unless (gethash gf all-generic-functions
)
333 (setf (gethash gf all-generic-functions
) t
)
334 (funcall function gf
))))))
337 (defmethod shared-initialize :after
((specl class-eq-specializer
)
340 (declare (ignore slot-names
))
341 (setf (slot-value specl
'%type
) `(class-eq ,(specializer-class specl
))))
343 (defmethod shared-initialize :after
((specl eql-specializer
) slot-names
&key
)
344 (declare (ignore slot-names
))
345 (setf (slot-value specl
'%type
)
346 `(eql ,(specializer-object specl
)))
347 (setf (info :type
:translator specl
)
348 (constantly (make-member-type :members
(list (specializer-object specl
))))))
350 (defun real-load-defclass (name metaclass-name supers slots other
351 readers writers slot-names source-location safe-p
)
352 (with-single-package-locked-error (:symbol name
"defining ~S as a class")
353 (%compiler-defclass name readers writers slot-names
)
354 (let ((res (apply #'ensure-class name
:metaclass metaclass-name
355 :direct-superclasses supers
357 :definition-source source-location
362 (setf (gdefinition 'load-defclass
) #'real-load-defclass
)
364 (defun ensure-class (name &rest args
)
366 (apply #'ensure-class-using-class
367 (let ((class (find-class name nil
)))
368 (when (and class
(eq name
(class-name class
)))
369 ;; NAME is the proper name of CLASS, so redefine it
374 (defmethod ensure-class-using-class ((class null
) name
&rest args
&key
)
376 (multiple-value-bind (meta initargs
)
377 (frob-ensure-class-args args
)
378 (setf class
(apply #'make-instance meta
:name name initargs
))
379 (without-package-locks
380 (setf (find-class name
) class
))))
381 ;; After boot (SETF FIND-CLASS) does this.
382 (unless (eq **boot-state
** 'complete
)
383 (%set-class-type-translation class name
))
386 (defmethod ensure-class-using-class ((class pcl-class
) name
&rest args
&key
)
388 (multiple-value-bind (meta initargs
)
389 (frob-ensure-class-args args
)
390 (unless (eq (class-of class
) meta
)
391 (apply #'change-class class meta initargs
))
392 (apply #'reinitialize-instance class initargs
)
393 (without-package-locks
394 (setf (find-class name
) class
))))
395 ;; After boot (SETF FIND-CLASS) does this.
396 (unless (eq **boot-state
** 'complete
)
397 (%set-class-type-translation class name
))
400 (defun frob-ensure-class-args (args)
401 (let (metaclass metaclassp reversed-plist
)
402 (flet ((frob-superclass (s)
405 ((legal-class-name-p s
)
406 (or (find-class s nil
)
407 (ensure-class s
:metaclass
'forward-referenced-class
)))
408 (t (error "Not a class or a legal class name: ~S." s
)))))
409 (doplist (key val
) args
410 (cond ((eq key
:metaclass
)
412 (setf metaclass val metaclassp key
)))
414 (when (eq key
:direct-superclasses
)
415 (setf val
(mapcar #'frob-superclass val
)))
416 (setf reversed-plist
(list* val key reversed-plist
)))))
417 (values (cond (metaclassp
418 (if (classp metaclass
)
420 (find-class metaclass
)))
421 (t *the-class-standard-class
*))
422 (nreverse reversed-plist
)))))
424 ;;; This is used to call initfunctions of :allocation :class slots.
425 (defun call-initfun (fun slotd safe
)
426 (declare (function fun
))
427 (let ((value (funcall fun
)))
429 (let ((type (slot-definition-type slotd
)))
430 (unless (or (eq t type
)
432 (error 'type-error
:expected-type type
:datum value
))))
435 (defmethod shared-initialize :after
436 ((class std-class
) slot-names
&key
437 (direct-superclasses nil direct-superclasses-p
)
438 (direct-slots nil direct-slots-p
)
439 (direct-default-initargs nil direct-default-initargs-p
)
441 (cond (direct-superclasses-p
442 (setq direct-superclasses
443 (or direct-superclasses
444 (list (if (funcallable-standard-class-p class
)
445 *the-class-funcallable-standard-object
*
446 *the-class-standard-object
*))))
447 (dolist (superclass direct-superclasses
)
448 (unless (validate-superclass class superclass
)
449 (invalid-superclass class superclass
)))
450 (setf (slot-value class
'direct-superclasses
) direct-superclasses
))
452 (setq direct-superclasses
(slot-value class
'direct-superclasses
))))
455 (setf (slot-value class
'direct-slots
)
456 (mapcar (lambda (pl) (make-direct-slotd class pl
))
458 (slot-value class
'direct-slots
)))
459 (if direct-default-initargs-p
460 (setf (plist-value class
'direct-default-initargs
)
461 direct-default-initargs
)
462 (setq direct-default-initargs
463 (plist-value class
'direct-default-initargs
)))
464 (setf (plist-value class
'class-slot-cells
)
465 (let ((old-class-slot-cells (plist-value class
'class-slot-cells
))
466 (safe (safe-p class
))
468 (dolist (dslotd direct-slots
)
469 (when (eq :class
(slot-definition-allocation dslotd
))
471 (let* ((name (slot-definition-name dslotd
))
472 (old (assoc name old-class-slot-cells
)))
475 (member name slot-names
:test
#'eq
))
476 (let* ((initfunction (slot-definition-initfunction dslotd
))
479 (call-initfun initfunction dslotd safe
)
481 (push (cons name value
) collect
))
482 (push old collect
)))))
484 (add-direct-subclasses class direct-superclasses
)
485 (if (class-finalized-p class
)
486 ;; required by AMOP, "Reinitialization of Class Metaobjects"
487 (finalize-inheritance class
)
488 (update-class class nil
))
489 (add-slot-accessors class direct-slots definition-source
)
490 (make-preliminary-layout class
))
492 (define-condition invalid-superclass
(reference-condition error
)
493 ((class :initarg
:class
:reader invalid-superclass-class
)
494 (superclass :initarg
:superclass
:reader invalid-superclass-superclass
))
497 (let ((class (invalid-superclass-class c
))
498 (superclass (invalid-superclass-superclass c
)))
500 "~@<The class ~S was specified as a superclass of the ~
501 class ~S, but the metaclasses ~S and ~S are ~
502 incompatible.~@[ Define a method for ~S to avoid this ~
504 superclass class
(class-of superclass
) (class-of class
)
505 (and (typep superclass
'standard-class
)
506 'validate-superclass
))))))
508 (defmethod invalid-superclass ((class class
) (superclass class
))
509 (error 'invalid-superclass
:class class
:superclass superclass
510 :references
(list* '(:amop
:generic-function validate-superclass
)
511 (and (typep superclass
'built-in-class
)
512 (list '(:ansi-cl
:system-class built-in-class
)
513 '(:ansi-cl
:section
(4 3 7)))))))
515 (defmethod shared-initialize :after
((class forward-referenced-class
)
516 slot-names
&key
&allow-other-keys
)
517 (declare (ignore slot-names
))
518 (make-preliminary-layout class
))
520 (defvar *allow-forward-referenced-classes-in-cpl-p
* nil
)
522 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
523 ;;; make it known to the type system.
524 (defun make-preliminary-layout (class)
525 (flet ((compute-preliminary-cpl (root)
526 (let ((*allow-forward-referenced-classes-in-cpl-p
* t
))
527 (compute-class-precedence-list root
))))
529 (without-package-locks
530 (unless (class-finalized-p class
)
531 (let ((name (class-name class
))) ; flushable?
532 (declare (ignore name
))
533 ;; KLUDGE: This is fairly horrible. We need to make a
534 ;; full-fledged CLASSOID here, not just tell the compiler that
535 ;; some class is forthcoming, because there are legitimate
536 ;; questions one can ask of the type system, implemented in
537 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
538 (let ((layout (make-wrapper 0 class
)))
539 (setf (slot-value class
'wrapper
) layout
)
540 (let ((cpl (compute-preliminary-cpl class
)))
541 (setf (layout-inherits layout
)
542 (order-layout-inherits
543 (map 'simple-vector
#'class-wrapper
544 (reverse (rest cpl
))))))
545 (register-layout layout
:invalidate t
)
546 (%set-class-type-translation class
(layout-classoid layout
)))))
547 (mapc #'make-preliminary-layout
(class-direct-subclasses class
))))))
550 (defmethod shared-initialize :before
((class class
) slot-names
&key name
)
551 (declare (ignore slot-names name
))
552 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
553 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
554 (setf (slot-value class
'%type
) `(class ,class
))
555 (setf (slot-value class
'class-eq-specializer
)
556 (make-instance 'class-eq-specializer
:class class
)))
558 (defmethod reinitialize-instance :before
((class slot-class
) &key direct-superclasses
)
559 (dolist (old-super (set-difference (class-direct-superclasses class
) direct-superclasses
))
560 (remove-direct-subclass old-super class
))
561 (remove-slot-accessors class
(class-direct-slots class
)))
563 (defmethod reinitialize-instance :after
((class slot-class
)
566 (map-dependents class
568 (apply #'update-dependent class dependent initargs
))))
570 (defmethod reinitialize-instance :after
((class condition-class
) &key
)
571 (let* ((name (class-name class
))
572 (classoid (find-classoid name
))
573 (slots (condition-classoid-slots classoid
)))
574 ;; to balance the REMOVE-SLOT-ACCESSORS call in
575 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
577 (let ((slot-name (condition-slot-name slot
)))
578 (dolist (reader (condition-slot-readers slot
))
579 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
580 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
581 (sb-kernel::install-condition-slot-reader reader name slot-name
))
582 (dolist (writer (condition-slot-writers slot
))
583 (sb-kernel::install-condition-slot-writer writer name slot-name
))))))
585 (defmethod shared-initialize :after
((class condition-class
) slot-names
586 &key direct-slots direct-superclasses
)
587 (declare (ignore slot-names
))
588 (let ((classoid (find-classoid (slot-value class
'name
))))
590 %class-precedence-list cpl-available-p finalized-p
591 prototype
(direct-supers direct-superclasses
)
594 (setf (slot-value class
'direct-slots
)
595 (mapcar (lambda (pl) (make-direct-slotd class pl
))
598 (classoid-pcl-class classoid
) class
599 direct-supers direct-superclasses
600 wrapper
(classoid-layout classoid
)
601 %class-precedence-list
(compute-class-precedence-list class
)
603 (getf plist
'direct-default-initargs
)
604 (sb-kernel::condition-classoid-direct-default-initargs classoid
))
605 (add-direct-subclasses class direct-superclasses
)
606 (let ((slots (compute-slots class
)))
607 (setf (slot-value class
'slots
) slots
)
608 (setf (layout-slot-table wrapper
) (make-slot-table class slots
)))))
609 ;; Comment from Gerd's PCL, 2003-05-15:
611 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
612 ;; override condition accessors with generic functions. We do this
615 ;; ??? What does the above comment mean and why is it a good idea?
616 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
617 ;; comment) loses slot information in its condition classes:
618 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
619 ;; remove slot accessors but never put them back. I've added a
620 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
621 ;; was meant to happen? -- CSR, 2005-11-18
624 (defmethod direct-slot-definition-class ((class condition-class
)
626 (declare (ignore initargs
))
627 (find-class 'condition-direct-slot-definition
))
629 (defmethod effective-slot-definition-class ((class condition-class
)
631 (declare (ignore initargs
))
632 (find-class 'condition-effective-slot-definition
))
634 (defmethod finalize-inheritance ((class condition-class
))
635 (aver (slot-value class
'finalized-p
))
638 (defmethod compute-effective-slot-definition
639 ((class condition-class
) slot-name dslotds
)
640 (let* ((slotd (call-next-method))
641 (info (slot-definition-info slotd
)))
642 (setf (slot-info-reader info
)
644 (handler-case (condition-reader-function x slot-name
)
645 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
646 ;; is unbound; maybe it should be a CELL-ERROR of some
648 (error () (values (slot-unbound class x slot-name
))))))
649 (setf (slot-info-writer info
)
651 (condition-writer-function x v slot-name
)))
652 (setf (slot-info-boundp info
)
654 (multiple-value-bind (v c
)
655 (ignore-errors (condition-reader-function x slot-name
))
660 (defmethod compute-slots ((class condition-class
))
661 (mapcan (lambda (superclass)
662 (mapcar (lambda (dslotd)
663 (compute-effective-slot-definition
664 class
(slot-definition-name dslotd
) (list dslotd
)))
665 (class-direct-slots superclass
)))
666 (reverse (slot-value class
'%class-precedence-list
))))
668 (defmethod compute-slots :around
((class condition-class
))
669 (let ((eslotds (call-next-method)))
670 (mapc #'finalize-internal-slot-functions eslotds
)
673 (defmethod shared-initialize :after
674 ((slotd structure-slot-definition
) slot-names
&key
675 (allocation :instance
) allocation-class
)
676 (declare (ignore slot-names allocation-class
))
677 (unless (eq allocation
:instance
)
678 (error "Structure slots must have :INSTANCE allocation.")))
680 (defun make-structure-class-defstruct-form (name direct-slots include
)
681 (let* ((conc-name (format-symbol *package
* "~S structure class " name
))
682 (constructor (format-symbol *package
* "~Aconstructor" conc-name
))
683 (included-name (class-name include
))
686 (mapcar #'dsd-name
(dd-slots (find-defstruct-description included-name
)))))
691 (dolist (slotd (reverse direct-slots
))
692 (let* ((slot-name (slot-definition-name slotd
))
693 (initform (slot-definition-initform slotd
))
694 (type (slot-definition-type slotd
))
695 (desc `(,slot-name
,initform
:type
,type
)))
696 (push `(slot-accessor ,name
,slot-name reader
)
698 (push `(slot-accessor ,name
,slot-name writer
)
700 (if (member slot-name included-slots
:test
#'eq
)
701 (push desc old-slots
)
702 (push desc new-slots
))))
703 (let* ((defstruct `(defstruct (,name
705 `((:include
,included-name
707 (:constructor
,constructor
())
709 (:conc-name
,conc-name
)
713 (mapcar (lambda (slotd reader-name
)
715 (slot-definition-defstruct-accessor-symbol
717 `(defun ,reader-name
(obj)
718 (declare (type ,name obj
))
720 direct-slots reader-names
))
722 (mapcar (lambda (slotd writer-name
)
724 (slot-definition-defstruct-accessor-symbol
726 `(defun ,writer-name
(nv obj
)
727 (declare (type ,name obj
))
728 (setf (,accessor obj
) nv
))))
729 direct-slots writer-names
))
733 ,@readers-init
,@writers-init
735 (values defstruct-form constructor reader-names writer-names
))))
737 (defun make-defstruct-allocation-function (name)
738 ;; FIXME: Why don't we go class->layout->info == dd
739 (let ((dd (find-defstruct-description name
)))
742 (%make-structure-instance-allocator dd nil
))
743 (funcallable-structure
744 (%make-funcallable-structure-instance-allocator dd nil
)))))
746 (defmethod shared-initialize :after
747 ((class structure-class
) slot-names
&key
748 (direct-superclasses nil direct-superclasses-p
)
749 (direct-slots nil direct-slots-p
)
750 direct-default-initargs
752 (declare (ignore slot-names direct-default-initargs
))
753 (if direct-superclasses-p
754 (setf (slot-value class
'direct-superclasses
)
755 (or direct-superclasses
756 (setq direct-superclasses
757 (and (not (eq (slot-value class
'name
) 'structure-object
))
758 (list *the-class-structure-object
*)))))
759 (setq direct-superclasses
(slot-value class
'direct-superclasses
)))
760 (let* ((name (slot-value class
'name
))
761 (from-defclass-p (slot-value class
'from-defclass-p
))
762 (defstruct-p (or from-defclass-p
(not (structure-type-p name
)))))
764 (setf (slot-value class
'direct-slots
)
768 (let* ((slot-name (getf pl
:name
))
770 (format-symbol *package
*
771 "~S structure class ~A"
773 (setq pl
(list* :defstruct-accessor-symbol
775 (make-direct-slotd class pl
))
777 (setq direct-slots
(slot-value class
'direct-slots
)))
779 (let ((include (car (slot-value class
'direct-superclasses
))))
780 (multiple-value-bind (defstruct-form constructor reader-names writer-names
)
781 (make-structure-class-defstruct-form name direct-slots include
)
782 (unless (structure-type-p name
) (eval defstruct-form
))
783 (mapc (lambda (dslotd reader-name writer-name
)
784 (let* ((reader (gdefinition reader-name
))
785 (writer (when (fboundp writer-name
)
786 (gdefinition writer-name
))))
787 (setf (slot-value dslotd
'internal-reader-function
)
789 (setf (slot-value dslotd
'internal-writer-function
)
791 direct-slots reader-names writer-names
)
792 (setf (slot-value class
'defstruct-form
) defstruct-form
)
793 (setf (slot-value class
'defstruct-constructor
) constructor
)))
794 (setf (slot-value class
'defstruct-constructor
)
795 ;; KLUDGE: not class; in fixup.lisp, can't access slots
796 ;; outside methods yet.
797 (make-defstruct-allocation-function name
)))
798 (add-direct-subclasses class direct-superclasses
)
799 (setf (slot-value class
'%class-precedence-list
)
800 (compute-class-precedence-list class
))
801 (setf (slot-value class
'cpl-available-p
) t
)
802 (let ((slots (compute-slots class
)))
803 (setf (slot-value class
'slots
) slots
)
804 (let* ((lclass (find-classoid (slot-value class
'name
)))
805 (layout (classoid-layout lclass
)))
806 (setf (classoid-pcl-class lclass
) class
)
807 (setf (slot-value class
'wrapper
) layout
)
808 (setf (layout-slot-table layout
) (make-slot-table class slots
))))
809 (setf (slot-value class
'finalized-p
) t
)
810 (add-slot-accessors class direct-slots definition-source
)))
812 (defmethod direct-slot-definition-class ((class structure-class
) &rest initargs
)
813 (declare (ignore initargs
))
814 (find-class 'structure-direct-slot-definition
))
816 (defmethod finalize-inheritance ((class structure-class
))
817 nil
) ; always finalized
819 (defun add-slot-accessors (class dslotds
&optional source-location
)
820 (fix-slot-accessors class dslotds
'add source-location
))
822 (defun remove-slot-accessors (class dslotds
)
823 (fix-slot-accessors class dslotds
'remove
))
825 (defun fix-slot-accessors (class dslotds add
/remove
&optional source-location
)
826 (flet ((fix (gfspec name r
/w doc
)
827 (let ((gf (cond ((eq add
/remove
'add
)
828 (or (find-generic-function gfspec nil
)
829 (ensure-generic-function
830 gfspec
:lambda-list
(case r
/w
832 (w '(new-value object
))))))
834 (find-generic-function gfspec nil
)))))
837 (r (if (eq add
/remove
'add
)
838 (add-reader-method class gf name doc source-location
)
839 (remove-reader-method class gf
)))
840 (w (if (eq add
/remove
'add
)
841 (add-writer-method class gf name doc source-location
)
842 (remove-writer-method class gf
))))))))
843 (dolist (dslotd dslotds
)
844 (let ((slot-name (slot-definition-name dslotd
))
845 (slot-doc (%slot-definition-documentation dslotd
)))
846 (dolist (r (slot-definition-readers dslotd
))
847 (fix r slot-name
'r slot-doc
))
848 (dolist (w (slot-definition-writers dslotd
))
849 (fix w slot-name
'w slot-doc
))))))
851 (defun add-direct-subclasses (class supers
)
852 (dolist (super supers
)
853 (unless (memq class
(class-direct-subclasses class
))
854 (add-direct-subclass super class
))))
856 (defmethod finalize-inheritance ((class std-class
))
857 (update-class class t
))
859 (defmethod finalize-inheritance ((class forward-referenced-class
))
860 ;; FIXME: should we not be thinking a bit about what kinds of error
861 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
862 ;; possibly a forward-referenced-class-error, though that's
863 ;; difficult given e.g. class precedence list calculations...
865 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
870 (defun class-has-a-forward-referenced-superclass-p (class)
871 (or (when (forward-referenced-class-p class
)
873 (some #'class-has-a-forward-referenced-superclass-p
874 (class-direct-superclasses class
))))
876 ;;; This is called by :after shared-initialize whenever a class is initialized
877 ;;; or reinitialized. The class may or may not be finalized.
878 (defun update-class (class finalizep
)
879 (without-package-locks
881 (when (or finalizep
(class-finalized-p class
))
882 (%update-cpl class
(compute-class-precedence-list class
))
883 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
885 (%update-slots class
(compute-slots class
))
886 (update-gfs-of-class class
)
887 (setf (plist-value class
'default-initargs
) (compute-default-initargs class
))
888 (update-ctors 'finalize-inheritance
:class class
))
889 (dolist (sub (class-direct-subclasses class
))
890 (update-class sub nil
)))))
892 (define-condition cpl-protocol-violation
(reference-condition error
)
893 ((class :initarg
:class
:reader cpl-protocol-violation-class
)
894 (cpl :initarg
:cpl
:reader cpl-protocol-violation-cpl
))
895 (:default-initargs
:references
(list '(:sbcl
:node
"Metaobject Protocol")))
898 (format s
"~@<Protocol violation: the ~S class ~S ~
899 ~:[has~;does not have~] the class ~S in its ~
900 class precedence list: ~S.~@:>"
901 (class-name (class-of (cpl-protocol-violation-class c
)))
902 (cpl-protocol-violation-class c
)
903 (eq (class-of (cpl-protocol-violation-class c
))
904 *the-class-funcallable-standard-class
*)
905 (find-class 'function
)
906 (cpl-protocol-violation-cpl c
)))))
908 (defun class-has-a-cpl-protocol-violation-p (class)
909 (labels ((find-in-superclasses (class classes
)
912 ((eql class
(car classes
)) t
)
913 (t (find-in-superclasses class
(append (class-direct-superclasses (car classes
)) (cdr classes
)))))))
914 (let ((metaclass (class-of class
)))
916 ((eql metaclass
*the-class-standard-class
*)
917 (find-in-superclasses (find-class 'function
) (list class
)))
918 ((eql metaclass
*the-class-funcallable-standard-class
*)
919 (not (find-in-superclasses (find-class 'function
) (list class
))))))))
921 (defun %update-cpl
(class cpl
)
922 (when (eq (class-of class
) *the-class-standard-class
*)
923 (when (find (find-class 'function
) cpl
)
924 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
925 (when (eq (class-of class
) *the-class-funcallable-standard-class
*)
926 (unless (find (find-class 'function
) cpl
)
927 (error 'cpl-protocol-violation
:class class
:cpl cpl
)))
928 (if (class-finalized-p class
)
929 (unless (and (equal (class-precedence-list class
) cpl
)
931 (when (position :class
(class-direct-slots c
)
932 :key
#'slot-definition-allocation
)
934 ;; comment from the old CMU CL sources:
935 ;; Need to have the cpl setup before %update-lisp-class-layout
936 ;; is called on CMU CL.
937 (setf (slot-value class
'%class-precedence-list
) cpl
)
938 (setf (slot-value class
'cpl-available-p
) t
)
939 (%force-cache-flushes class
))
941 (setf (slot-value class
'%class-precedence-list
) cpl
)
942 (setf (slot-value class
'cpl-available-p
) t
)))
943 (update-class-can-precede-p cpl
))
945 (defun update-class-can-precede-p (cpl)
947 (let ((first (car cpl
)))
948 (dolist (c (cdr cpl
))
949 (pushnew c
(slot-value first
'can-precede-list
) :test
#'eq
)))
950 (update-class-can-precede-p (cdr cpl
))))
952 (defun class-can-precede-p (class1 class2
)
953 (member class2
(class-can-precede-list class1
) :test
#'eq
))
955 ;;; This is called from %UPDATE-SLOTS to check if slot layouts are compatible.
957 ;;; In addition to slot locations (implicit in the ordering of the slots), we
958 ;;; must check classes: SLOT-INFO structures from old slotds may have been
959 ;;; cached in permutation vectors, but new slotds have had new ones allocated
960 ;;; to them. This is non-problematic for standard slotds, because we know the
961 ;;; structure is compatible, but if a slot definition class changes, this can
962 ;;; change the way SLOT-VALUE-USING-CLASS should dispatch.
964 ;;; Also, if the slot has a non-standard allocation, we need to check that it
966 (defun slot-layouts-compatible-p
967 (oslotds new-instance-slotds new-class-slotds new-custom-slotds
)
968 (multiple-value-bind (old-instance-slotds old-class-slotds old-custom-slotds
)
969 (classify-slotds oslotds
)
971 ;; Instance slots: name, type, and class.
972 (dolist (o old-instance-slotds
(not new-instance-slotds
))
973 (let ((n (pop new-instance-slotds
)))
975 (eq (slot-definition-name o
) (slot-definition-name n
))
976 (eq (slot-definition-type o
) (slot-definition-type n
))
977 (eq (class-of o
) (class-of n
)))
979 ;; Class slots: name and class. (FIXME: class slots not typechecked?)
980 (dolist (o old-class-slotds
(not new-class-slotds
))
981 (let ((n (pop new-class-slotds
)))
983 (eq (slot-definition-name o
) (slot-definition-name n
))
984 (eq (class-of n
) (class-of o
)))
986 ;; Custom slots: check name, type, allocation, and class. (FIXME: should we just punt?)
987 (dolist (o old-custom-slotds
(not new-custom-slotds
))
988 (let ((n (pop new-custom-slotds
)))
990 (eq (slot-definition-name o
) (slot-definition-name n
))
991 (eq (slot-definition-type o
) (slot-definition-type n
))
992 (eq (slot-definition-allocation o
) (slot-definition-allocation n
))
993 (eq (class-of o
) (class-of n
)))
996 (defun style-warn-about-duplicate-slots (class)
997 (do* ((slots (slot-value class
'slots
) (cdr slots
))
1002 "~@<slot names with the same SYMBOL-NAME but ~
1003 different SYMBOL-PACKAGE (possible package problem) ~
1004 for class ~S:~4I~@:_~<~@{~/sb-impl::print-symbol-with-prefix/~^~:@_~}~:>~@:>"
1006 (let* ((slot-name (slot-definition-name (car slots
)))
1007 (oslots (and (not (eq (symbol-package slot-name
)
1010 (lambda (slot-name-2)
1011 (or (eq (symbol-package slot-name-2
)
1013 (string/= slot-name slot-name-2
)))
1015 :key
#'slot-definition-name
))))
1017 (pushnew (cons slot-name
1018 (mapcar #'slot-definition-name oslots
))
1020 :test
#'string
= :key
#'car
)))))
1022 (defun %update-slots
(class eslotds
)
1023 (multiple-value-bind (instance-slots class-slots custom-slots
)
1024 (classify-slotds eslotds
)
1025 (let* ((nslots (length instance-slots
))
1026 (owrapper (when (class-finalized-p class
) (class-wrapper class
)))
1028 (cond ((null owrapper
)
1029 (make-wrapper nslots class
))
1030 ((slot-layouts-compatible-p (layout-slot-list owrapper
)
1031 instance-slots class-slots custom-slots
)
1034 ;; This will initialize the new wrapper to have the
1035 ;; same state as the old wrapper. We will then have
1036 ;; to change that. This may seem like wasted work
1037 ;; (and it is), but the spec requires that we call
1038 ;; MAKE-INSTANCES-OBSOLETE.
1039 (make-instances-obsolete class
)
1040 (class-wrapper class
)))))
1041 (%update-lisp-class-layout class nwrapper
)
1042 (setf (slot-value class
'slots
) eslotds
1043 (layout-slot-list nwrapper
) eslotds
1044 (layout-slot-table nwrapper
) (make-slot-table class eslotds
)
1045 (layout-length nwrapper
) nslots
1046 (slot-value class
'wrapper
) nwrapper
)
1047 (style-warn-about-duplicate-slots class
)
1048 (setf (slot-value class
'finalized-p
) t
)
1049 (unless (eq owrapper nwrapper
)
1050 (maybe-update-standard-slot-locations class
)))))
1052 (defun update-gf-dfun (class gf
)
1053 (let ((*new-class
* class
)
1054 (arg-info (gf-arg-info gf
)))
1056 ((special-case-for-compute-discriminating-function-p gf
))
1057 ((gf-precompute-dfun-and-emf-p arg-info
)
1058 (multiple-value-bind (dfun cache info
) (make-final-dfun-internal gf
)
1059 (update-dfun gf dfun cache info
))))))
1061 (defun update-gfs-of-class (class)
1062 (when (and (class-finalized-p class
)
1063 (let ((cpl (class-precedence-list class
)))
1064 (or (member *the-class-slot-class
* cpl
:test
#'eq
)
1065 (member *the-class-standard-effective-slot-definition
*
1067 (let ((gf-table (make-hash-table :test
'eq
)))
1068 (labels ((collect-gfs (class)
1069 (dolist (gf (specializer-direct-generic-functions class
))
1070 (setf (gethash gf gf-table
) t
))
1071 (mapc #'collect-gfs
(class-direct-superclasses class
))))
1073 (maphash (lambda (gf ignore
)
1074 (declare (ignore ignore
))
1075 (update-gf-dfun class gf
))
1078 (defmethod compute-default-initargs ((class slot-class
))
1079 (let ((initargs (loop for c in
(class-precedence-list class
)
1080 append
(class-direct-default-initargs c
))))
1081 (delete-duplicates initargs
:test
#'eq
:key
#'car
:from-end t
)))
1083 ;;;; protocols for constructing direct and effective slot definitions
1085 (defmethod direct-slot-definition-class ((class std-class
) &rest initargs
)
1086 (declare (ignore initargs
))
1087 (find-class 'standard-direct-slot-definition
))
1089 (defun make-direct-slotd (class initargs
)
1090 (apply #'make-instance
1091 (apply #'direct-slot-definition-class class initargs
)
1095 ;;; I (CSR) am not sure, but I believe that the particular order of
1096 ;;; slots is quite important: it is ideal to attempt to have a
1097 ;;; constant slot location for the same notional slots as much as
1098 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1099 ;;; al.) have a chance of working. The below at least walks through
1100 ;;; the slots predictably, but maybe it would be good to compute some
1101 ;;; kind of optimal slot layout by looking at locations of slots in
1103 (defun std-compute-slots (class)
1104 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1105 ;; for each different slot name we find in our superclasses. Each
1106 ;; call receives the class and a list of the dslotds with that name.
1107 ;; The list is in most-specific-first order.
1108 (let ((name-dslotds-alist ()))
1109 (dolist (c (reverse (class-precedence-list class
)))
1110 (dolist (slot (class-direct-slots c
))
1111 (let* ((name (slot-definition-name slot
))
1112 (entry (assq name name-dslotds-alist
)))
1114 (push slot
(cdr entry
))
1115 (push (list name slot
) name-dslotds-alist
)))))
1116 (mapcar (lambda (direct)
1117 (compute-effective-slot-definition class
1120 (nreverse name-dslotds-alist
))))
1122 (defmethod compute-slots ((class standard-class
))
1123 (std-compute-slots class
))
1124 (defmethod compute-slots ((class funcallable-standard-class
))
1125 (std-compute-slots class
))
1127 (defun std-compute-slots-around (class eslotds
)
1129 (safe (safe-p class
)))
1130 (dolist (eslotd eslotds eslotds
)
1131 (setf (slot-definition-location eslotd
)
1132 (case (slot-definition-allocation eslotd
)
1136 (let* ((name (slot-definition-name eslotd
))
1139 (slot-definition-allocation-class eslotd
)
1140 ;; we get here if the user adds an extra slot
1142 (setf (slot-definition-allocation-class eslotd
)
1144 ;; which raises the question of what we should
1145 ;; do if we find that said user has added a slot
1146 ;; with the same name as another slot...
1147 (cell (or (assq name
(class-slot-cells from-class
))
1148 (let ((c (cons name
+slot-unbound
+)))
1149 (push c
(class-slot-cells from-class
))
1152 (if (eq +slot-unbound
+ (cdr cell
))
1153 ;; We may have inherited an initfunction FIXME: Is this
1154 ;; really right? Is the initialization in
1155 ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1156 (let ((initfun (slot-definition-initfunction eslotd
)))
1158 (rplacd cell
(call-initfun initfun eslotd safe
))
1161 (unless (slot-definition-class eslotd
)
1162 (setf (slot-definition-class eslotd
) class
))
1163 (initialize-internal-slot-functions eslotd
))))
1165 (defmethod compute-slots :around
((class standard-class
))
1166 (let ((eslotds (call-next-method)))
1167 (std-compute-slots-around class eslotds
)))
1168 (defmethod compute-slots :around
((class funcallable-standard-class
))
1169 (let ((eslotds (call-next-method)))
1170 (std-compute-slots-around class eslotds
)))
1172 (defmethod compute-slots ((class structure-class
))
1173 (mapcan (lambda (superclass)
1174 (mapcar (lambda (dslotd)
1175 (compute-effective-slot-definition
1177 (slot-definition-name dslotd
)
1179 (class-direct-slots superclass
)))
1180 (reverse (slot-value class
'%class-precedence-list
))))
1182 (defmethod compute-slots :around
((class structure-class
))
1183 (let ((eslotds (call-next-method)))
1184 (mapc #'finalize-internal-slot-functions eslotds
)
1187 (defmethod compute-effective-slot-definition ((class slot-class
) name dslotds
)
1188 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds
))
1189 (class (apply #'effective-slot-definition-class class initargs
))
1190 (slotd (apply #'make-instance class initargs
)))
1193 (defmethod effective-slot-definition-class ((class std-class
) &rest initargs
)
1194 (declare (ignore initargs
))
1195 (find-class 'standard-effective-slot-definition
))
1197 (defmethod effective-slot-definition-class ((class structure-class
) &rest initargs
)
1198 (declare (ignore initargs
))
1199 (find-class 'structure-effective-slot-definition
))
1201 (defmethod compute-effective-slot-definition-initargs
1202 ((class slot-class
) direct-slotds
)
1208 (allocation-class nil
)
1211 (documentationp nil
)
1216 (dolist (slotd direct-slotds
)
1219 (setq name
(slot-definition-name slotd
)
1222 (awhen (slot-definition-initfunction slotd
)
1223 (setq initform
(slot-definition-initform slotd
)
1226 (unless documentationp
1227 (awhen (%slot-definition-documentation slotd
)
1228 (setq documentation it
1231 (setq allocation
(slot-definition-allocation slotd
)
1232 allocation-class
(slot-definition-class slotd
)
1234 (setq initargs
(append (slot-definition-initargs slotd
) initargs
))
1235 (let ((slotd-type (slot-definition-type slotd
)))
1237 ((eq type t
) slotd-type
)
1238 ;; This pairwise type intersection is perhaps a
1239 ;; little inefficient and inelegant, but it's
1240 ;; unlikely to lie on the critical path. Shout
1241 ;; if I'm wrong. -- CSR, 2005-11-24
1243 (specifier-type `(and ,type
,slotd-type
)))))))))
1246 :initfunction initfunction
1248 :allocation allocation
1249 :allocation-class allocation-class
1252 :documentation documentation
)))
1254 (defmethod compute-effective-slot-definition-initargs :around
1255 ((class structure-class
) direct-slotds
)
1256 (let* ((slotd (car direct-slotds
))
1257 (accessor (slot-definition-defstruct-accessor-symbol slotd
)))
1258 (list* :defstruct-accessor-symbol accessor
1259 :internal-reader-function
1260 (slot-definition-internal-reader-function slotd
)
1261 :internal-writer-function
1262 (slot-definition-internal-writer-function slotd
)
1263 (call-next-method))))
1265 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1266 ;;; to make the method object. They have to use make-a-method which
1267 ;;; is a specially bootstrapped mechanism for making standard methods.
1268 (defmethod reader-method-class ((class slot-class
) direct-slot
&rest initargs
)
1269 (declare (ignore direct-slot initargs
))
1270 (find-class 'standard-reader-method
))
1272 (defmethod add-reader-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1273 (add-method generic-function
1274 (make-a-method 'standard-reader-method
1276 (list (or (class-name class
) 'object
))
1278 (make-reader-method-function class slot-name
)
1279 (or slot-documentation
"automatically generated reader method")
1280 :slot-name slot-name
1282 :method-class-function
#'reader-method-class
1283 :definition-source source-location
)))
1285 (defmethod writer-method-class ((class slot-class
) direct-slot
&rest initargs
)
1286 (declare (ignore direct-slot initargs
))
1287 (find-class 'standard-writer-method
))
1289 (defmethod add-writer-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1290 (add-method generic-function
1291 (make-a-method 'standard-writer-method
1293 (list 'new-value
(or (class-name class
) 'object
))
1294 (list *the-class-t
* class
)
1295 (make-writer-method-function class slot-name
)
1296 (or slot-documentation
"automatically generated writer method")
1297 :slot-name slot-name
1299 :method-class-function
#'writer-method-class
1300 :definition-source source-location
)))
1302 (defmethod add-boundp-method ((class slot-class
) generic-function slot-name slot-documentation source-location
)
1303 (add-method generic-function
1304 (make-a-method (constantly (find-class 'standard-boundp-method
))
1307 (list (or (class-name class
) 'object
))
1309 (make-boundp-method-function class slot-name
)
1310 (or slot-documentation
"automatically generated boundp method")
1311 :slot-name slot-name
1312 :definition-source source-location
)))
1314 (defmethod remove-reader-method ((class slot-class
) generic-function
)
1315 (let ((method (get-method generic-function
() (list class
) nil
)))
1316 (when method
(remove-method generic-function method
))))
1318 (defmethod remove-writer-method ((class slot-class
) generic-function
)
1320 (get-method generic-function
() (list *the-class-t
* class
) nil
)))
1321 (when method
(remove-method generic-function method
))))
1323 (defmethod remove-boundp-method ((class slot-class
) generic-function
)
1324 (let ((method (get-method generic-function
() (list class
) nil
)))
1325 (when method
(remove-method generic-function method
))))
1327 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1328 ;;; function are NOT part of the standard protocol. They are however
1329 ;;; useful; PCL makes use of them internally and documents them for
1330 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1332 ;;; *** This needs work to make type testing by the writer functions which
1333 ;;; *** do type testing faster. The idea would be to have one constructor
1334 ;;; *** for each possible type test.
1336 ;;; *** There is a subtle bug here which is going to have to be fixed.
1337 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1338 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1339 ;;; *** defined for this metaclass a chance to run.
1341 (defmethod make-reader-method-function ((class slot-class
) slot-name
)
1342 (make-std-reader-method-function class slot-name
))
1344 (defmethod make-writer-method-function ((class slot-class
) slot-name
)
1345 (make-std-writer-method-function class slot-name
))
1347 (defmethod make-boundp-method-function ((class slot-class
) slot-name
)
1348 (make-std-boundp-method-function class slot-name
))
1350 (defmethod compatible-meta-class-change-p (class proto-new-class
)
1351 (eq (class-of class
) (class-of proto-new-class
)))
1353 (defmethod validate-superclass ((class class
) (superclass class
))
1354 (or (eq (class-of class
) (class-of superclass
))
1355 (and (eq (class-of superclass
) *the-class-standard-class
*)
1356 (eq (class-of class
) *the-class-funcallable-standard-class
*))
1357 (and (eq (class-of superclass
) *the-class-funcallable-standard-class
*)
1358 (eq (class-of class
) *the-class-standard-class
*))))
1360 ;;; What this does depends on which of the four possible values of
1361 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1362 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1363 ;;; nothing to do, as the new wrapper has already been created. If
1364 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1365 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1366 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1368 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1369 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1370 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1371 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1372 ;;; obsolete the wrapper.
1374 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1375 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1376 ;;; :UNINITIALIZED)))
1378 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1379 (defun %force-cache-flushes
(class)
1381 (let* ((owrapper (class-wrapper class
)))
1382 ;; We only need to do something if the wrapper is still valid. If
1383 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1384 ;; both of those will already be doing what we want. In
1385 ;; particular, we must be sure we never change an OBSOLETE into a
1386 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1387 (when (or (not (invalid-wrapper-p owrapper
))
1388 ;; KLUDGE: despite the observations above, this remains
1389 ;; a violation of locality or what might be considered
1390 ;; good style. There has to be a better way! -- CSR,
1392 (eq (layout-invalid owrapper
) t
))
1393 (let ((nwrapper (make-wrapper (layout-length owrapper
)
1395 (setf (layout-slot-list nwrapper
) (layout-slot-list owrapper
))
1396 (setf (layout-slot-table nwrapper
) (layout-slot-table owrapper
))
1397 (%update-lisp-class-layout class nwrapper
)
1398 (setf (slot-value class
'wrapper
) nwrapper
)
1399 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1401 (if (find-if (lambda (x)
1402 (and (consp x
) (eq :obsolete
(car x
))))
1403 (layout-inherits owrapper
)
1404 :key
#'layout-invalid
)
1405 (%invalidate-wrapper owrapper
:obsolete nwrapper
)
1406 (%invalidate-wrapper owrapper
:flush nwrapper
))))))
1409 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1410 ;;; the next access to the instance (as defined in 88-002R) to trap
1411 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1412 (defmethod make-instances-obsolete ((class std-class
))
1414 (let* ((owrapper (class-wrapper class
))
1415 (nwrapper (make-wrapper (layout-length owrapper
)
1417 (unless (class-finalized-p class
)
1418 (if (class-has-a-forward-referenced-superclass-p class
)
1419 (return-from make-instances-obsolete class
)
1420 (%update-cpl class
(compute-class-precedence-list class
))))
1421 (setf (layout-slot-list nwrapper
) (layout-slot-list owrapper
))
1422 (setf (layout-slot-table nwrapper
) (layout-slot-table owrapper
))
1423 (%update-lisp-class-layout class nwrapper
)
1424 (setf (slot-value class
'wrapper
) nwrapper
)
1425 (%invalidate-wrapper owrapper
:obsolete nwrapper
)
1428 (defmethod make-instances-obsolete ((class symbol
))
1429 (make-instances-obsolete (find-class class
))
1430 ;; ANSI wants the class name when called with a symbol.
1433 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1434 ;;; see an obsolete instance. The times when it is called are:
1435 ;;; - when the instance is involved in method lookup
1436 ;;; - when attempting to access a slot of an instance
1438 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1439 ;;; instance access macros.
1441 ;;; Of course these times when it is called are an internal
1442 ;;; implementation detail of PCL and are not part of the documented
1443 ;;; description of when the obsolete instance update happens. The
1444 ;;; documented description is as it appears in 88-002R.
1446 ;;; This has to return the new wrapper, so it counts on all the
1447 ;;; methods on obsolete-instance-trap-internal to return the new
1448 ;;; wrapper. It also does a little internal error checking to make
1449 ;;; sure that the traps are only happening when they should, and that
1450 ;;; the trap methods are computing appropriate new wrappers.
1452 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1453 ;;; after a structure is redefined. In most cases,
1454 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1455 ;;; so it must signal an error. The hard part of this is that the
1456 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1457 ;;; called again, so in that case, we have to return some reasonable
1458 ;;; wrapper, instead.
1460 (defvar *in-obsolete-instance-trap
* nil
)
1461 (defvar *the-wrapper-of-structure-object
*
1462 (class-wrapper (find-class 'structure-object
)))
1464 (define-condition obsolete-structure
(error)
1465 ((datum :reader obsolete-structure-datum
:initarg
:datum
))
1467 (lambda (condition stream
)
1468 ;; Don't try to print the structure, since it probably won't work.
1470 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1471 (type-of (obsolete-structure-datum condition
))))))
1473 (defun %obsolete-instance-trap
(owrapper nwrapper instance
)
1474 (if (not (layout-for-std-class-p owrapper
))
1475 (if *in-obsolete-instance-trap
*
1476 *the-wrapper-of-structure-object
*
1477 (let ((*in-obsolete-instance-trap
* t
))
1478 (error 'obsolete-structure
:datum instance
)))
1479 (let* ((class (wrapper-class* nwrapper
))
1480 (copy (allocate-instance class
)) ;??? allocate-instance ???
1481 (oslots (get-slots instance
))
1482 (nslots (get-slots copy
))
1486 (safe (safe-p class
)))
1488 ;; local --> local transfer value, check type
1489 ;; local --> shared discard value, discard slot
1490 ;; local --> -- discard slot
1491 ;; local --> custom XXX
1493 ;; shared --> local transfer value, check type
1494 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1495 ;; shared --> -- discard value
1496 ;; shared --> custom XXX
1498 ;; -- --> local add slot
1500 ;; -- --> custom XXX
1502 (multiple-value-bind (new-instance-slots new-class-slots new-custom-slots
)
1503 (classify-slotds (layout-slot-list nwrapper
))
1504 (declare (ignore new-class-slots
))
1505 (multiple-value-bind (old-instance-slots old-class-slots old-custom-slots
)
1506 (classify-slotds (layout-slot-list owrapper
))
1508 (let ((layout (mapcar (lambda (slotd)
1509 ;; Get the names only once.
1510 (cons (slot-definition-name slotd
) slotd
))
1511 new-instance-slots
)))
1513 (flet ((set-value (value cell
)
1514 (let ((name (car cell
))
1516 (when (and safe
(neq value
+slot-unbound
+))
1517 (let ((type (slot-definition-type slotd
)))
1519 (typep value type
) (value)
1520 "~@<Error updating obsolete instance. Current value in slot ~
1521 ~S of an instance of ~S is ~S, which does not match the new ~
1523 name class value type
)))
1524 (setf (clos-slots-ref nslots
(slot-definition-location slotd
)) value
1525 ;; Prune from the list now that it's been dealt with.
1526 layout
(remove cell layout
)))))
1528 ;; Go through all the old local slots.
1529 (dolist (old old-instance-slots
)
1530 (let* ((name (slot-definition-name old
))
1531 (value (clos-slots-ref oslots
(slot-definition-location old
))))
1532 (unless (eq value
+slot-unbound
+)
1533 (let ((new (assq name layout
)))
1535 (set-value value new
))
1537 (push name discarded
)
1538 (setf (getf plist name
) value
)))))))
1540 ;; Go through all the old shared slots.
1541 (dolist (old old-class-slots
)
1542 (let* ((cell (slot-definition-location old
))
1544 (new (assq name layout
)))
1546 (set-value (cdr cell
) new
))))
1548 ;; Go through all custom slots to find added ones. CLHS
1549 ;; doesn't specify what to do about them, and neither does
1550 ;; AMOP. We do want them to get initialized, though, so we
1551 ;; list them in ADDED for the benefit of SHARED-INITIALIZE.
1552 (dolist (new new-custom-slots
)
1553 (let* ((name (slot-definition-name new
))
1554 (old (find name old-custom-slots
:key
#'slot-definition-name
)))
1556 (push name added
))))
1558 ;; Go through all the remaining new local slots to compute the added slots.
1559 (dolist (cell layout
)
1560 (push (car cell
) added
))))))
1562 (%swap-wrappers-and-slots instance copy
)
1564 (update-instance-for-redefined-class instance
1570 (defun %change-class
(instance new-class initargs
)
1571 (let* ((old-class (class-of instance
))
1572 (copy (allocate-instance new-class
))
1573 (new-wrapper (get-wrapper copy
))
1574 (old-wrapper (class-wrapper old-class
))
1575 (old-slots (get-slots instance
))
1576 (new-slots (get-slots copy
))
1577 (safe (safe-p new-class
)))
1578 (multiple-value-bind (new-instance-slots new-class-slots
)
1579 (classify-slotds (layout-slot-list new-wrapper
))
1580 (declare (ignore new-class-slots
))
1581 (multiple-value-bind (old-instance-slots old-class-slots
)
1582 (classify-slotds (layout-slot-list old-wrapper
))
1584 (flet ((set-value (value slotd
)
1586 (assert (typep value
(slot-definition-type slotd
)) (value)
1587 "~@<Error changing class. Current value in slot ~S ~
1588 of an instance of ~S is ~S, which does not match the new ~
1589 slot type ~S in class ~S.~:@>"
1590 (slot-definition-name slotd
) old-class value
1591 (slot-definition-type slotd
) new-class
))
1592 (setf (clos-slots-ref new-slots
(slot-definition-location slotd
)) value
)))
1594 ;; "The values of local slots specified by both the class CTO and
1595 ;; CFROM are retained. If such a local slot was unbound, it
1596 ;; remains unbound."
1597 (dolist (new new-instance-slots
)
1598 (let* ((name (slot-definition-name new
))
1599 (old (find name old-instance-slots
:key
#'slot-definition-name
)))
1601 (set-value (clos-slots-ref old-slots
(slot-definition-location old
))
1604 ;; "The values of slots specified as shared in the class CFROM and
1605 ;; as local in the class CTO are retained."
1606 (dolist (old old-class-slots
)
1607 (let* ((slot-and-val (slot-definition-location old
))
1608 (new (find (car slot-and-val
) new-instance-slots
1609 :key
#'slot-definition-name
)))
1611 (set-value (cdr slot-and-val
) new
)))))))
1613 ;; Make the copy point to the old instance's storage, and make the
1614 ;; old instance point to the new storage.
1615 (%swap-wrappers-and-slots instance copy
)
1617 (apply #'update-instance-for-different-class copy instance initargs
)
1621 (flet ((cpl-maybe-finalize (class)
1622 (unless (class-finalized-p class
)
1623 (finalize-inheritance class
))
1624 (class-precedence-list class
)))
1626 (defmethod change-class ((instance standard-object
) (new-class standard-class
)
1629 (dolist (class (cpl-maybe-finalize new-class
))
1632 `(when (eq class
(find-class ',class-name
))
1633 (error 'metaobject-initialization-violation
1634 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1635 :format-arguments
(list 'change-class
',class-name
)
1636 :references
(list '(:amop
:initialization
,class-name
))))))
1638 (frob generic-function
)
1640 (frob slot-definition
)))
1641 (%change-class instance new-class initargs
)))
1643 (defmethod change-class ((instance forward-referenced-class
)
1644 (new-class standard-class
) &rest initargs
)
1646 (dolist (class (cpl-maybe-finalize new-class
)
1647 (error 'metaobject-initialization-violation
1649 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1651 (list 'change-class
'forward-referenced-class
'class
)
1653 (list '(:amop
:generic-function ensure-class-using-class
)
1654 '(:amop
:initialization class
))))
1655 (when (eq class
(find-class 'class
))
1657 (%change-class instance new-class initargs
)))
1659 (defmethod change-class ((instance funcallable-standard-object
)
1660 (new-class funcallable-standard-class
)
1663 (dolist (class (cpl-maybe-finalize new-class
))
1666 `(when (eq class
(find-class ',class-name
))
1667 (error 'metaobject-initialization-violation
1668 :format-control
"~@<Cannot ~S objects into ~S metaobjects.~@:>"
1669 :format-arguments
(list 'change-class
',class-name
)
1670 :references
(list '(:amop
:initialization
,class-name
))))))
1672 (frob generic-function
)
1674 (frob slot-definition
)))
1675 (%change-class instance new-class initargs
))))
1677 (defmethod change-class ((instance standard-object
)
1678 (new-class funcallable-standard-class
)
1680 (declare (ignore initargs
))
1681 (error "You can't change the class of ~S to ~S~@
1682 because it isn't already an instance with metaclass ~S."
1683 instance new-class
'standard-class
))
1685 (defmethod change-class ((instance funcallable-standard-object
)
1686 (new-class standard-class
)
1688 (declare (ignore initargs
))
1689 (error "You can't change the class of ~S to ~S~@
1690 because it isn't already an instance with metaclass ~S."
1691 instance new-class
'funcallable-standard-class
))
1693 (defmethod change-class ((instance t
) (new-class-name symbol
) &rest initargs
)
1694 (apply #'change-class instance
(find-class new-class-name
) initargs
))
1696 ;;;; The metaclasses SYSTEM-CLASS and BUILT-IN-CLASS
1698 ;;;; These metaclasses are something of a weird creature. By this
1699 ;;;; point, all instances which will exist have been created, and no
1700 ;;;; instance is ever created by calling MAKE-INSTANCE. (The
1701 ;;;; distinction between the metaclasses is that we allow subclassing
1702 ;;;; of SYSTEM-CLASS, such as through STREAM and SEQUENCE protocols,
1703 ;;;; but not of BUILT-IN-CLASS.)
1705 ;;;; AMOP mandates some behaviour of the implementation with respect
1706 ;;;; to BUILT-IN-CLASSes, and we implement that through methods on
1707 ;;;; SYSTEM-CLASS here.
1709 (macrolet ((def (name args control
)
1710 `(defmethod ,name
,args
1711 (declare (ignore initargs
))
1712 (error 'metaobject-initialization-violation
1713 :format-control
,(format nil
"~@<~A~@:>" control
)
1714 :format-arguments
(list (class-name class
))
1715 :references
(list '(:amop
:initialization
"Class"))))))
1716 (def initialize-instance
((class system-class
) &rest initargs
)
1717 "Cannot initialize an instance of ~S.")
1718 (def reinitialize-instance
((class system-class
) &rest initargs
)
1719 "Cannot reinitialize an instance of ~S."))
1721 (macrolet ((def (name) `(defmethod ,name
((class system-class
)) nil
)))
1722 (def class-direct-slots
)
1724 (def class-direct-default-initargs
)
1725 (def class-default-initargs
))
1727 (defmethod validate-superclass ((c class
) (s system-class
))
1729 (defmethod validate-superclass ((c class
) (s built-in-class
))
1732 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1733 (defmethod class-direct-slots ((class forward-referenced-class
)) ())
1734 (defmethod class-direct-default-initargs ((class forward-referenced-class
)) ())
1735 (macrolet ((def (method)
1736 `(defmethod ,method
((class forward-referenced-class
))
1737 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1739 (def class-default-initargs
)
1740 (def class-precedence-list
)
1743 (defmethod validate-superclass ((c slot-class
) (f forward-referenced-class
))
1746 (defmethod add-dependent ((metaobject dependent-update-mixin
) dependent
)
1747 (pushnew dependent
(plist-value metaobject
'dependents
) :test
#'eq
))
1749 (defmethod remove-dependent ((metaobject dependent-update-mixin
) dependent
)
1750 (setf (plist-value metaobject
'dependents
)
1751 (delete dependent
(plist-value metaobject
'dependents
))))
1753 (defmethod map-dependents ((metaobject dependent-update-mixin
) function
)
1754 (dolist (dependent (plist-value metaobject
'dependents
))
1755 (funcall function dependent
)))