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))
77 (class (slot-value slotd '%class)))
78 (dolist (type '(reader writer boundp))
79 (let* ((gf-name (ecase type
80 (reader 'slot-value-using-class)
81 (writer '(setf slot-value-using-class))
82 (boundp 'slot-boundp-using-class)))
83 (gf (gdefinition gf-name)))
84 ;; KLUDGE: this logic is cut'n'pasted from
85 ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
86 ;; only called later, because it does things that can't be
87 ;; computed this early in class finalization; however, we need
88 ;; this bit as early as possible. -- CSR, 2009-11-05
89 (setf (slot-accessor-std-p slotd type)
90 (let* ((std-method (standard-svuc-method type))
91 (str-method (structure-svuc-method type))
92 (types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
93 (types (if (eq type 'writer) `(t ,@types1) types1))
94 (methods (compute-applicable-methods-using-types gf types)))
95 (null (cdr methods))))
96 (setf (slot-accessor-function slotd type)
98 (declare (dynamic-extent args))
99 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
100 ;; work here (see KLUDGE comment above).
101 (let ((fun (compute-slot-accessor-info slotd type gf)))
102 (apply fun args))))))))
104 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition))
105 (dolist (type '(reader writer boundp))
106 (let* ((gf-name (ecase type
107 (reader 'slot-value-using-class)
108 (writer '(setf slot-value-using-class))
109 (boundp 'slot-boundp-using-class)))
110 (gf (gdefinition gf-name)))
111 (compute-slot-accessor-info slotd type gf))))
113 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
115 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
116 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
117 ;;; writing/testing effective slot SLOTD.
119 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
120 ;;; GF. Store the effective method in the effective slot definition
121 ;;; object itself; these GFs have special dispatch functions calling
122 ;;; effective methods directly retrieved from effective slot
123 ;;; definition objects, as an optimization.
125 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
127 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
129 (let* ((name (slot-value slotd 'name))
130 (class (slot-value slotd '%class)))
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-spinlock :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-spinlock (*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-spinlock (*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-spinlock (*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-spinlock (*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 (error "~@<The class ~S was specified as a ~
450 super-class of the class ~S, ~
451 but the meta-classes ~S and ~S are incompatible. ~
452 Define a method for ~S to avoid this error.~@:>"
453 superclass class (class-of superclass) (class-of class)
454 'validate-superclass)))
455 (setf (slot-value class 'direct-superclasses) direct-superclasses))
457 (setq direct-superclasses (slot-value class 'direct-superclasses))))
460 (setf (slot-value class 'direct-slots)
461 (mapcar (lambda (pl) (make-direct-slotd class pl))
463 (slot-value class 'direct-slots)))
464 (if direct-default-initargs-p
465 (setf (plist-value class 'direct-default-initargs)
466 direct-default-initargs)
467 (setq direct-default-initargs
468 (plist-value class 'direct-default-initargs)))
469 (setf (plist-value class 'class-slot-cells)
470 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
471 (safe (safe-p class))
473 (dolist (dslotd direct-slots)
474 (when (eq :class (slot-definition-allocation dslotd))
476 (let* ((name (slot-definition-name dslotd))
477 (old (assoc name old-class-slot-cells)))
480 (member name slot-names :test #'eq))
481 (let* ((initfunction (slot-definition-initfunction dslotd))
484 (call-initfun initfunction dslotd safe)
486 (push (cons name value) collect))
487 (push old collect)))))
489 (add-direct-subclasses class direct-superclasses)
490 (if (class-finalized-p class)
491 ;; required by AMOP, "Reinitialization of Class Metaobjects"
492 (finalize-inheritance class)
493 (update-class class nil))
494 (add-slot-accessors class direct-slots definition-source)
495 (make-preliminary-layout class))
497 (defmethod shared-initialize :after ((class forward-referenced-class)
498 slot-names &key &allow-other-keys)
499 (declare (ignore slot-names))
500 (make-preliminary-layout class))
502 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
504 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
505 ;;; make it known to the type system.
506 (defun make-preliminary-layout (class)
507 (flet ((compute-preliminary-cpl (root)
508 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
509 (compute-class-precedence-list root))))
511 (without-package-locks
512 (unless (class-finalized-p class)
513 (let ((name (class-name class)))
514 ;; KLUDGE: This is fairly horrible. We need to make a
515 ;; full-fledged CLASSOID here, not just tell the compiler that
516 ;; some class is forthcoming, because there are legitimate
517 ;; questions one can ask of the type system, implemented in
518 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
519 (let ((layout (make-wrapper 0 class)))
520 (setf (slot-value class 'wrapper) layout)
521 (let ((cpl (compute-preliminary-cpl class)))
522 (setf (layout-inherits layout)
523 (order-layout-inherits
524 (map 'simple-vector #'class-wrapper
525 (reverse (rest cpl))))))
526 (register-layout layout :invalidate t)
527 (%set-class-type-translation class (layout-classoid layout)))))
528 (mapc #'make-preliminary-layout (class-direct-subclasses class))))))
531 (defmethod shared-initialize :before ((class class) slot-names &key name)
532 (declare (ignore slot-names name))
533 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
534 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
535 (setf (slot-value class '%type) `(class ,class))
536 (setf (slot-value class 'class-eq-specializer)
537 (make-instance 'class-eq-specializer :class class)))
539 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
540 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
541 (remove-direct-subclass old-super class))
542 (remove-slot-accessors class (class-direct-slots class)))
544 (defmethod reinitialize-instance :after ((class slot-class)
547 (map-dependents class
549 (apply #'update-dependent class dependent initargs))))
551 (defmethod reinitialize-instance :after ((class condition-class) &key)
552 (let* ((name (class-name class))
553 (classoid (find-classoid name))
554 (slots (condition-classoid-slots classoid)))
555 ;; to balance the REMOVE-SLOT-ACCESSORS call in
556 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
558 (let ((slot-name (condition-slot-name slot)))
559 (dolist (reader (condition-slot-readers slot))
560 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
561 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
562 (sb-kernel::install-condition-slot-reader reader name slot-name))
563 (dolist (writer (condition-slot-writers slot))
564 (sb-kernel::install-condition-slot-writer writer name slot-name))))))
566 (defmethod shared-initialize :after ((class condition-class) slot-names
567 &key direct-slots direct-superclasses)
568 (declare (ignore slot-names))
569 (let ((classoid (find-classoid (slot-value class 'name))))
570 (with-slots (wrapper %class-precedence-list cpl-available-p
571 prototype (direct-supers direct-superclasses))
573 (setf (slot-value class 'direct-slots)
574 (mapcar (lambda (pl) (make-direct-slotd class pl))
576 (setf (slot-value class 'finalized-p) t)
577 (setf (classoid-pcl-class classoid) class)
578 (setq direct-supers direct-superclasses)
579 (setq wrapper (classoid-layout classoid))
580 (setq %class-precedence-list (compute-class-precedence-list class))
581 (setq cpl-available-p t)
582 (add-direct-subclasses class direct-superclasses)
583 (let ((slots (compute-slots class)))
584 (setf (slot-value class 'slots) slots)
585 (setf (layout-slot-table wrapper) (make-slot-table class slots)))))
586 ;; Comment from Gerd's PCL, 2003-05-15:
588 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
589 ;; override condition accessors with generic functions. We do this
592 ;; ??? What does the above comment mean and why is it a good idea?
593 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
594 ;; comment) loses slot information in its condition classes:
595 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
596 ;; remove slot accessors but never put them back. I've added a
597 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
598 ;; was meant to happen? -- CSR, 2005-11-18
601 (defmethod direct-slot-definition-class ((class condition-class)
603 (declare (ignore initargs))
604 (find-class 'condition-direct-slot-definition))
606 (defmethod effective-slot-definition-class ((class condition-class)
608 (declare (ignore initargs))
609 (find-class 'condition-effective-slot-definition))
611 (defmethod finalize-inheritance ((class condition-class))
612 (aver (slot-value class 'finalized-p))
615 (defmethod compute-effective-slot-definition
616 ((class condition-class) slot-name dslotds)
617 (let* ((slotd (call-next-method))
618 (info (slot-definition-info slotd)))
619 (setf (slot-info-reader info)
621 (handler-case (condition-reader-function x slot-name)
622 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
623 ;; is unbound; maybe it should be a CELL-ERROR of some
625 (error () (values (slot-unbound class x slot-name))))))
626 (setf (slot-info-writer info)
628 (condition-writer-function x v slot-name)))
629 (setf (slot-info-boundp info)
631 (multiple-value-bind (v c)
632 (ignore-errors (condition-reader-function x slot-name))
637 (defmethod compute-slots ((class condition-class))
638 (mapcan (lambda (superclass)
639 (mapcar (lambda (dslotd)
640 (compute-effective-slot-definition
641 class (slot-definition-name dslotd) (list dslotd)))
642 (class-direct-slots superclass)))
643 (reverse (slot-value class '%class-precedence-list))))
645 (defmethod compute-slots :around ((class condition-class))
646 (let ((eslotds (call-next-method)))
647 (mapc #'finalize-internal-slot-functions eslotds)
650 (defmethod shared-initialize :after
651 ((slotd structure-slot-definition) slot-names &key
652 (allocation :instance) allocation-class)
653 (declare (ignore slot-names allocation-class))
654 (unless (eq allocation :instance)
655 (error "Structure slots must have :INSTANCE allocation.")))
657 (defun make-structure-class-defstruct-form (name direct-slots include)
658 (let* ((conc-name (format-symbol *package* "~S structure class " name))
659 (constructor (format-symbol *package* "~Aconstructor" conc-name))
660 (included-name (class-name include))
663 (mapcar #'dsd-name (dd-slots (find-defstruct-description included-name)))))
668 (dolist (slotd (reverse direct-slots))
669 (let* ((slot-name (slot-definition-name slotd))
670 (initform (slot-definition-initform slotd))
671 (type (slot-definition-type slotd))
672 (desc `(,slot-name ,initform :type ,type)))
673 (push `(slot-accessor ,name ,slot-name reader)
675 (push `(slot-accessor ,name ,slot-name writer)
677 (if (member slot-name included-slots :test #'eq)
678 (push desc old-slots)
679 (push desc new-slots))))
680 (let* ((defstruct `(defstruct (,name
682 `((:include ,included-name
684 (:constructor ,constructor ())
686 (:conc-name ,conc-name)
690 (mapcar (lambda (slotd reader-name)
692 (slot-definition-defstruct-accessor-symbol
694 `(defun ,reader-name (obj)
695 (declare (type ,name obj))
697 direct-slots reader-names))
699 (mapcar (lambda (slotd writer-name)
701 (slot-definition-defstruct-accessor-symbol
703 `(defun ,writer-name (nv obj)
704 (declare (type ,name obj))
705 (setf (,accessor obj) nv))))
706 direct-slots writer-names))
710 ,@readers-init ,@writers-init
712 (values defstruct-form constructor reader-names writer-names))))
714 (defun make-defstruct-allocation-function (name)
715 ;; FIXME: Why don't we go class->layout->info == dd
716 (let ((dd (find-defstruct-description name)))
719 (%make-structure-instance-allocator dd nil))
720 (funcallable-structure
721 (%make-funcallable-structure-instance-allocator dd nil)))))
723 (defmethod shared-initialize :after
724 ((class structure-class) slot-names &key
725 (direct-superclasses nil direct-superclasses-p)
726 (direct-slots nil direct-slots-p)
727 direct-default-initargs
729 (declare (ignore slot-names direct-default-initargs))
730 (if direct-superclasses-p
731 (setf (slot-value class 'direct-superclasses)
732 (or direct-superclasses
733 (setq direct-superclasses
734 (and (not (eq (slot-value class 'name) 'structure-object))
735 (list *the-class-structure-object*)))))
736 (setq direct-superclasses (slot-value class 'direct-superclasses)))
737 (let* ((name (slot-value class 'name))
738 (from-defclass-p (slot-value class 'from-defclass-p))
739 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
741 (setf (slot-value class 'direct-slots)
745 (let* ((slot-name (getf pl :name))
747 (format-symbol *package*
748 "~S structure class ~A"
750 (setq pl (list* :defstruct-accessor-symbol
752 (make-direct-slotd class pl))
754 (setq direct-slots (slot-value class 'direct-slots)))
756 (let ((include (car (slot-value class 'direct-superclasses))))
757 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
758 (make-structure-class-defstruct-form name direct-slots include)
759 (unless (structure-type-p name) (eval defstruct-form))
760 (mapc (lambda (dslotd reader-name writer-name)
761 (let* ((reader (gdefinition reader-name))
762 (writer (when (fboundp writer-name)
763 (gdefinition writer-name))))
764 (setf (slot-value dslotd 'internal-reader-function)
766 (setf (slot-value dslotd 'internal-writer-function)
768 direct-slots reader-names writer-names)
769 (setf (slot-value class 'defstruct-form) defstruct-form)
770 (setf (slot-value class 'defstruct-constructor) constructor)))
771 (setf (slot-value class 'defstruct-constructor)
772 ;; KLUDGE: not class; in fixup.lisp, can't access slots
773 ;; outside methods yet.
774 (make-defstruct-allocation-function name)))
775 (add-direct-subclasses class direct-superclasses)
776 (setf (slot-value class '%class-precedence-list)
777 (compute-class-precedence-list class))
778 (setf (slot-value class 'cpl-available-p) t)
779 (let ((slots (compute-slots class)))
780 (setf (slot-value class 'slots) slots)
781 (let* ((lclass (find-classoid (slot-value class 'name)))
782 (layout (classoid-layout lclass)))
783 (setf (classoid-pcl-class lclass) class)
784 (setf (slot-value class 'wrapper) layout)
785 (setf (layout-slot-table layout) (make-slot-table class slots))))
786 (setf (slot-value class 'finalized-p) t)
787 (add-slot-accessors class direct-slots definition-source)))
789 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
790 (declare (ignore initargs))
791 (find-class 'structure-direct-slot-definition))
793 (defmethod finalize-inheritance ((class structure-class))
794 nil) ; always finalized
796 (defun add-slot-accessors (class dslotds &optional source-location)
797 (fix-slot-accessors class dslotds 'add source-location))
799 (defun remove-slot-accessors (class dslotds)
800 (fix-slot-accessors class dslotds 'remove))
802 (defun fix-slot-accessors (class dslotds add/remove &optional source-location)
803 (flet ((fix (gfspec name r/w doc)
804 (let ((gf (cond ((eq add/remove 'add)
805 (or (find-generic-function gfspec nil)
806 (ensure-generic-function
807 gfspec :lambda-list (case r/w
809 (w '(new-value object))))))
811 (find-generic-function gfspec nil)))))
814 (r (if (eq add/remove 'add)
815 (add-reader-method class gf name doc source-location)
816 (remove-reader-method class gf)))
817 (w (if (eq add/remove 'add)
818 (add-writer-method class gf name doc source-location)
819 (remove-writer-method class gf))))))))
820 (dolist (dslotd dslotds)
821 (let ((slot-name (slot-definition-name dslotd))
822 (slot-doc (%slot-definition-documentation dslotd)))
823 (dolist (r (slot-definition-readers dslotd))
824 (fix r slot-name 'r slot-doc))
825 (dolist (w (slot-definition-writers dslotd))
826 (fix w slot-name 'w slot-doc))))))
828 (defun add-direct-subclasses (class supers)
829 (dolist (super supers)
830 (unless (memq class (class-direct-subclasses class))
831 (add-direct-subclass super class))))
833 (defmethod finalize-inheritance ((class std-class))
834 (update-class class t))
836 (defmethod finalize-inheritance ((class forward-referenced-class))
837 ;; FIXME: should we not be thinking a bit about what kinds of error
838 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
839 ;; possibly a forward-referenced-class-error, though that's
840 ;; difficult given e.g. class precedence list calculations...
842 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
847 (defun class-has-a-forward-referenced-superclass-p (class)
848 (or (forward-referenced-class-p class)
849 (some #'class-has-a-forward-referenced-superclass-p
850 (class-direct-superclasses class))))
852 ;;; This is called by :after shared-initialize whenever a class is initialized
853 ;;; or reinitialized. The class may or may not be finalized.
854 (defun update-class (class finalizep)
855 (without-package-locks
857 (when (or finalizep (class-finalized-p class))
858 (%update-cpl class (compute-class-precedence-list class))
859 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
861 (%update-slots class (compute-slots class))
862 (update-gfs-of-class class)
863 (setf (plist-value class 'default-initargs) (compute-default-initargs class))
864 (update-ctors 'finalize-inheritance :class class))
865 (dolist (sub (class-direct-subclasses class))
866 (update-class sub nil)))))
868 (define-condition cpl-protocol-violation (reference-condition error)
869 ((class :initarg :class :reader cpl-protocol-violation-class)
870 (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
871 (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
874 (format s "~@<Protocol violation: the ~S class ~S ~
875 ~:[has~;does not have~] the class ~S in its ~
876 class precedence list: ~S.~@:>"
877 (class-name (class-of (cpl-protocol-violation-class c)))
878 (cpl-protocol-violation-class c)
879 (eq (class-of (cpl-protocol-violation-class c))
880 *the-class-funcallable-standard-class*)
881 (find-class 'function)
882 (cpl-protocol-violation-cpl c)))))
884 (defun %update-cpl (class cpl)
885 (when (eq (class-of class) *the-class-standard-class*)
886 (when (find (find-class 'function) cpl)
887 (error 'cpl-protocol-violation :class class :cpl cpl)))
888 (when (eq (class-of class) *the-class-funcallable-standard-class*)
889 (unless (find (find-class 'function) cpl)
890 (error 'cpl-protocol-violation :class class :cpl cpl)))
891 (if (class-finalized-p class)
892 (unless (and (equal (class-precedence-list class) cpl)
894 (when (position :class (class-direct-slots c)
895 :key #'slot-definition-allocation)
897 ;; comment from the old CMU CL sources:
898 ;; Need to have the cpl setup before %update-lisp-class-layout
899 ;; is called on CMU CL.
900 (setf (slot-value class '%class-precedence-list) cpl)
901 (setf (slot-value class 'cpl-available-p) t)
902 (%force-cache-flushes class))
904 (setf (slot-value class '%class-precedence-list) cpl)
905 (setf (slot-value class 'cpl-available-p) t)))
906 (update-class-can-precede-p cpl))
908 (defun update-class-can-precede-p (cpl)
910 (let ((first (car cpl)))
911 (dolist (c (cdr cpl))
912 (pushnew c (slot-value first 'can-precede-list) :test #'eq)))
913 (update-class-can-precede-p (cdr cpl))))
915 (defun class-can-precede-p (class1 class2)
916 (member class2 (class-can-precede-list class1) :test #'eq))
918 ;;; This is called from %UPDATE-SLOTS when layout doesn't seem to change.
919 ;;; SLOT-INFO structures from old slotds may have been cached in permutation
920 ;;; vectors, but new slotds have had new ones allocated to them.
922 ;;; This is non-problematic for standard slotds, because we know the structure
923 ;;; is compatible, but if a slot definition class changes, this can change the
924 ;;; way SLOT-VALUE-USING-CLASS should dispatch.
926 ;;; So, compare all slotd classes, and return T if all remain the same.
927 (defun slotd-classes-eq (oslotds nslotds)
928 (labels ((pop-nslotd (name)
930 ;; Most of the time the first slot is right, but because the
931 ;; order of instance and non-instance slots can change without
932 ;; layout changing we cannot rely on that.
933 (let ((n (pop nslotds)))
934 (if (eq name (slot-definition-name n))
938 (push n nslotds))))))
940 for o = (pop oslotds)
941 for n = (pop-nslotd (slot-definition-name o))
942 always (eq (class-of o) (class-of n)))))
944 (defun %update-slots (class eslotds)
945 (let ((instance-slots ())
947 (dolist (eslotd eslotds)
948 (let ((alloc (slot-definition-allocation eslotd)))
950 (:instance (push eslotd instance-slots))
951 (:class (push eslotd class-slots)))))
953 ;; If there is a change in the shape of the instances then the
954 ;; old class is now obsolete.
955 (let* ((nlayout (mapcar (lambda (slotd)
956 (cons (slot-definition-name slotd)
957 (slot-definition-type slotd)))
958 (sort instance-slots #'<
959 :key #'slot-definition-location)))
960 (nslots (length nlayout))
961 (nwrapper-class-slots (compute-class-slots class-slots))
962 (owrapper (when (class-finalized-p class)
963 (class-wrapper class)))
964 (olayout (when owrapper
965 (wrapper-instance-slots-layout owrapper)))
966 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
968 (cond ((null owrapper)
969 (make-wrapper nslots class))
970 ((and (equal nlayout olayout)
971 (equal (mapcar #'car owrapper-class-slots)
972 (mapcar #'car nwrapper-class-slots))
973 (slotd-classes-eq (slot-value class 'slots) eslotds))
976 ;; This will initialize the new wrapper to have the
977 ;; same state as the old wrapper. We will then have
978 ;; to change that. This may seem like wasted work
979 ;; (and it is), but the spec requires that we call
980 ;; MAKE-INSTANCES-OBSOLETE.
981 (make-instances-obsolete class)
982 (class-wrapper class)))))
984 (%update-lisp-class-layout class nwrapper)
985 (setf (slot-value class 'slots) eslotds
986 (wrapper-slot-table nwrapper) (make-slot-table class eslotds)
987 (wrapper-instance-slots-layout nwrapper) nlayout
988 (wrapper-class-slots nwrapper) nwrapper-class-slots
989 (wrapper-length nwrapper) nslots
990 (slot-value class 'wrapper) nwrapper)
991 (do* ((slots (slot-value class 'slots) (cdr slots))
996 "~@<slot names with the same SYMBOL-NAME but ~
997 different SYMBOL-PACKAGE (possible package problem) ~
998 for class ~S:~4I~@:_~<~@{~/sb-impl::print-symbol-with-prefix/~^~:@_~}~:>~@:>"
1000 (let* ((slot (car slots))
1001 (oslots (remove (slot-definition-name slot) (cdr slots)
1003 :key #'slot-definition-name)))
1005 (pushnew (cons (slot-definition-name slot)
1006 (mapcar #'slot-definition-name oslots))
1008 :test #'string= :key #'car))))
1009 (setf (slot-value class 'finalized-p) t)
1010 (unless (eq owrapper nwrapper)
1011 (maybe-update-standard-slot-locations class)))))
1013 (defun compute-class-slots (eslotds)
1015 (dolist (eslotd eslotds (nreverse collect))
1016 (let ((cell (assoc (slot-definition-name eslotd)
1018 (slot-definition-allocation-class eslotd)))))
1020 (push cell collect)))))
1022 (defun update-gf-dfun (class gf)
1023 (let ((*new-class* class)
1024 (arg-info (gf-arg-info gf)))
1026 ((special-case-for-compute-discriminating-function-p gf))
1027 ((gf-precompute-dfun-and-emf-p arg-info)
1028 (multiple-value-bind (dfun cache info) (make-final-dfun-internal gf)
1029 (update-dfun gf dfun cache info))))))
1031 (defun update-gfs-of-class (class)
1032 (when (and (class-finalized-p class)
1033 (let ((cpl (class-precedence-list class)))
1034 (or (member *the-class-slot-class* cpl :test #'eq)
1035 (member *the-class-standard-effective-slot-definition*
1037 (let ((gf-table (make-hash-table :test 'eq)))
1038 (labels ((collect-gfs (class)
1039 (dolist (gf (specializer-direct-generic-functions class))
1040 (setf (gethash gf gf-table) t))
1041 (mapc #'collect-gfs (class-direct-superclasses class))))
1043 (maphash (lambda (gf ignore)
1044 (declare (ignore ignore))
1045 (update-gf-dfun class gf))
1048 (defmethod compute-default-initargs ((class slot-class))
1049 (let ((initargs (loop for c in (class-precedence-list class)
1050 append (class-direct-default-initargs c))))
1051 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1053 ;;;; protocols for constructing direct and effective slot definitions
1055 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1056 (declare (ignore initargs))
1057 (find-class 'standard-direct-slot-definition))
1059 (defun make-direct-slotd (class initargs)
1060 (apply #'make-instance
1061 (apply #'direct-slot-definition-class class initargs)
1065 ;;; I (CSR) am not sure, but I believe that the particular order of
1066 ;;; slots is quite important: it is ideal to attempt to have a
1067 ;;; constant slot location for the same notional slots as much as
1068 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1069 ;;; al.) have a chance of working. The below at least walks through
1070 ;;; the slots predictably, but maybe it would be good to compute some
1071 ;;; kind of optimal slot layout by looking at locations of slots in
1073 (defun std-compute-slots (class)
1074 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1075 ;; for each different slot name we find in our superclasses. Each
1076 ;; call receives the class and a list of the dslotds with that name.
1077 ;; The list is in most-specific-first order.
1078 (let ((name-dslotds-alist ()))
1079 (dolist (c (reverse (class-precedence-list class)))
1080 (dolist (slot (class-direct-slots c))
1081 (let* ((name (slot-definition-name slot))
1082 (entry (assq name name-dslotds-alist)))
1084 (push slot (cdr entry))
1085 (push (list name slot) name-dslotds-alist)))))
1086 (mapcar (lambda (direct)
1087 (compute-effective-slot-definition class
1090 (nreverse name-dslotds-alist))))
1092 (defmethod compute-slots ((class standard-class))
1093 (std-compute-slots class))
1094 (defmethod compute-slots ((class funcallable-standard-class))
1095 (std-compute-slots class))
1097 (defun std-compute-slots-around (class eslotds)
1099 (safe (safe-p class)))
1100 (dolist (eslotd eslotds eslotds)
1101 (setf (slot-definition-location eslotd)
1102 (case (slot-definition-allocation eslotd)
1106 (let* ((name (slot-definition-name eslotd))
1109 (slot-definition-allocation-class eslotd)
1110 ;; we get here if the user adds an extra slot
1112 (setf (slot-definition-allocation-class eslotd)
1114 ;; which raises the question of what we should
1115 ;; do if we find that said user has added a slot
1116 ;; with the same name as another slot...
1117 (cell (or (assq name (class-slot-cells from-class))
1118 (let ((c (cons name +slot-unbound+)))
1119 (push c (class-slot-cells from-class))
1122 (if (eq +slot-unbound+ (cdr cell))
1123 ;; We may have inherited an initfunction FIXME: Is this
1124 ;; really right? Is the initialization in
1125 ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1126 (let ((initfun (slot-definition-initfunction eslotd)))
1128 (rplacd cell (call-initfun initfun eslotd safe))
1131 (unless (slot-definition-class eslotd)
1132 (setf (slot-definition-class eslotd) class))
1133 (initialize-internal-slot-functions eslotd))))
1135 (defmethod compute-slots :around ((class standard-class))
1136 (let ((eslotds (call-next-method)))
1137 (std-compute-slots-around class eslotds)))
1138 (defmethod compute-slots :around ((class funcallable-standard-class))
1139 (let ((eslotds (call-next-method)))
1140 (std-compute-slots-around class eslotds)))
1142 (defmethod compute-slots ((class structure-class))
1143 (mapcan (lambda (superclass)
1144 (mapcar (lambda (dslotd)
1145 (compute-effective-slot-definition
1147 (slot-definition-name dslotd)
1149 (class-direct-slots superclass)))
1150 (reverse (slot-value class '%class-precedence-list))))
1152 (defmethod compute-slots :around ((class structure-class))
1153 (let ((eslotds (call-next-method)))
1154 (mapc #'finalize-internal-slot-functions eslotds)
1157 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1158 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1159 (class (apply #'effective-slot-definition-class class initargs))
1160 (slotd (apply #'make-instance class initargs)))
1163 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1164 (declare (ignore initargs))
1165 (find-class 'standard-effective-slot-definition))
1167 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1168 (declare (ignore initargs))
1169 (find-class 'structure-effective-slot-definition))
1171 (defmethod compute-effective-slot-definition-initargs
1172 ((class slot-class) direct-slotds)
1178 (allocation-class nil)
1181 (documentationp nil)
1186 (dolist (slotd direct-slotds)
1189 (setq name (slot-definition-name slotd)
1192 (awhen (slot-definition-initfunction slotd)
1193 (setq initform (slot-definition-initform slotd)
1196 (unless documentationp
1197 (awhen (%slot-definition-documentation slotd)
1198 (setq documentation it
1201 (setq allocation (slot-definition-allocation slotd)
1202 allocation-class (slot-definition-class slotd)
1204 (setq initargs (append (slot-definition-initargs slotd) initargs))
1205 (let ((slotd-type (slot-definition-type slotd)))
1207 ((eq type t) slotd-type)
1208 ;; This pairwise type intersection is perhaps a
1209 ;; little inefficient and inelegant, but it's
1210 ;; unlikely to lie on the critical path. Shout
1211 ;; if I'm wrong. -- CSR, 2005-11-24
1213 (specifier-type `(and ,type ,slotd-type)))))))))
1216 :initfunction initfunction
1218 :allocation allocation
1219 :allocation-class allocation-class
1222 :documentation documentation)))
1224 (defmethod compute-effective-slot-definition-initargs :around
1225 ((class structure-class) direct-slotds)
1226 (let* ((slotd (car direct-slotds))
1227 (accessor (slot-definition-defstruct-accessor-symbol slotd)))
1228 (list* :defstruct-accessor-symbol accessor
1229 :internal-reader-function
1230 (slot-definition-internal-reader-function slotd)
1231 :internal-writer-function
1232 (slot-definition-internal-writer-function slotd)
1233 (call-next-method))))
1235 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1236 ;;; to make the method object. They have to use make-a-method which
1237 ;;; is a specially bootstrapped mechanism for making standard methods.
1238 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1239 (declare (ignore direct-slot initargs))
1240 (find-class 'standard-reader-method))
1242 (defmethod add-reader-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1243 (add-method generic-function
1244 (make-a-method 'standard-reader-method
1246 (list (or (class-name class) 'object))
1248 (make-reader-method-function class slot-name)
1249 (or slot-documentation "automatically generated reader method")
1250 :slot-name slot-name
1252 :method-class-function #'reader-method-class
1253 :definition-source source-location)))
1255 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1256 (declare (ignore direct-slot initargs))
1257 (find-class 'standard-writer-method))
1259 (defmethod add-writer-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1260 (add-method generic-function
1261 (make-a-method 'standard-writer-method
1263 (list 'new-value (or (class-name class) 'object))
1264 (list *the-class-t* class)
1265 (make-writer-method-function class slot-name)
1266 (or slot-documentation "automatically generated writer method")
1267 :slot-name slot-name
1269 :method-class-function #'writer-method-class
1270 :definition-source source-location)))
1272 (defmethod add-boundp-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1273 (add-method generic-function
1274 (make-a-method (constantly (find-class 'standard-boundp-method))
1277 (list (or (class-name class) 'object))
1279 (make-boundp-method-function class slot-name)
1280 (or slot-documentation "automatically generated boundp method")
1281 :slot-name slot-name
1282 :definition-source source-location)))
1284 (defmethod remove-reader-method ((class slot-class) generic-function)
1285 (let ((method (get-method generic-function () (list class) nil)))
1286 (when method (remove-method generic-function method))))
1288 (defmethod remove-writer-method ((class slot-class) generic-function)
1290 (get-method generic-function () (list *the-class-t* class) nil)))
1291 (when method (remove-method generic-function method))))
1293 (defmethod remove-boundp-method ((class slot-class) generic-function)
1294 (let ((method (get-method generic-function () (list class) nil)))
1295 (when method (remove-method generic-function method))))
1297 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1298 ;;; function are NOT part of the standard protocol. They are however
1299 ;;; useful; PCL makes use of them internally and documents them for
1300 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1302 ;;; *** This needs work to make type testing by the writer functions which
1303 ;;; *** do type testing faster. The idea would be to have one constructor
1304 ;;; *** for each possible type test.
1306 ;;; *** There is a subtle bug here which is going to have to be fixed.
1307 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1308 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1309 ;;; *** defined for this metaclass a chance to run.
1311 (defmethod make-reader-method-function ((class slot-class) slot-name)
1312 (make-std-reader-method-function class slot-name))
1314 (defmethod make-writer-method-function ((class slot-class) slot-name)
1315 (make-std-writer-method-function class slot-name))
1317 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1318 (make-std-boundp-method-function class slot-name))
1320 (defmethod compatible-meta-class-change-p (class proto-new-class)
1321 (eq (class-of class) (class-of proto-new-class)))
1323 (defmethod validate-superclass ((class class) (superclass class))
1324 (or (eq superclass *the-class-t*)
1325 (eq (class-of class) (class-of superclass))
1326 (and (eq (class-of superclass) *the-class-standard-class*)
1327 (eq (class-of class) *the-class-funcallable-standard-class*))
1328 (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1329 (eq (class-of class) *the-class-standard-class*))))
1331 ;;; What this does depends on which of the four possible values of
1332 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1333 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1334 ;;; nothing to do, as the new wrapper has already been created. If
1335 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1336 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1337 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1339 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1340 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1341 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1342 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1343 ;;; obsolete the wrapper.
1345 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1346 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1347 ;;; :UNINITIALIZED)))
1349 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1350 (defun %force-cache-flushes (class)
1351 (let* ((owrapper (class-wrapper class)))
1352 ;; We only need to do something if the wrapper is still valid. If
1353 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1354 ;; both of those will already be doing what we want. In
1355 ;; particular, we must be sure we never change an OBSOLETE into a
1356 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1357 (when (or (not (invalid-wrapper-p owrapper))
1358 ;; KLUDGE: despite the observations above, this remains
1359 ;; a violation of locality or what might be considered
1360 ;; good style. There has to be a better way! -- CSR,
1362 (eq (layout-invalid owrapper) t))
1363 (let ((nwrapper (make-wrapper (layout-length owrapper)
1365 (setf (wrapper-instance-slots-layout nwrapper)
1366 (wrapper-instance-slots-layout owrapper))
1367 (setf (wrapper-class-slots nwrapper)
1368 (wrapper-class-slots owrapper))
1369 (setf (wrapper-slot-table nwrapper)
1370 (wrapper-slot-table owrapper))
1371 (%update-lisp-class-layout class nwrapper)
1372 (setf (slot-value class 'wrapper) nwrapper)
1373 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1375 (if (find-if (lambda (x)
1376 (and (consp x) (eq :obsolete (car x))))
1377 (layout-inherits owrapper)
1378 :key #'layout-invalid)
1379 (%invalidate-wrapper owrapper :obsolete nwrapper)
1380 (%invalidate-wrapper owrapper :flush nwrapper))))))
1382 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1383 ;;; the next access to the instance (as defined in 88-002R) to trap
1384 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1385 (defmethod make-instances-obsolete ((class std-class))
1387 (let* ((owrapper (class-wrapper class))
1388 (nwrapper (make-wrapper (layout-length owrapper)
1390 (unless (class-finalized-p class)
1391 (if (class-has-a-forward-referenced-superclass-p class)
1392 (return-from make-instances-obsolete class)
1393 (%update-cpl class (compute-class-precedence-list class))))
1394 (setf (wrapper-instance-slots-layout nwrapper)
1395 (wrapper-instance-slots-layout owrapper))
1396 (setf (wrapper-class-slots nwrapper)
1397 (wrapper-class-slots owrapper))
1398 (setf (wrapper-slot-table nwrapper)
1399 (wrapper-slot-table owrapper))
1400 (%update-lisp-class-layout class nwrapper)
1401 (setf (slot-value class 'wrapper) nwrapper)
1402 (%invalidate-wrapper owrapper :obsolete nwrapper)
1405 (defmethod make-instances-obsolete ((class symbol))
1406 (make-instances-obsolete (find-class class))
1407 ;; ANSI wants the class name when called with a symbol.
1410 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1411 ;;; see an obsolete instance. The times when it is called are:
1412 ;;; - when the instance is involved in method lookup
1413 ;;; - when attempting to access a slot of an instance
1415 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1416 ;;; instance access macros.
1418 ;;; Of course these times when it is called are an internal
1419 ;;; implementation detail of PCL and are not part of the documented
1420 ;;; description of when the obsolete instance update happens. The
1421 ;;; documented description is as it appears in 88-002R.
1423 ;;; This has to return the new wrapper, so it counts on all the
1424 ;;; methods on obsolete-instance-trap-internal to return the new
1425 ;;; wrapper. It also does a little internal error checking to make
1426 ;;; sure that the traps are only happening when they should, and that
1427 ;;; the trap methods are computing appropriate new wrappers.
1429 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1430 ;;; after a structure is redefined. In most cases,
1431 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1432 ;;; so it must signal an error. The hard part of this is that the
1433 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1434 ;;; called again, so in that case, we have to return some reasonable
1435 ;;; wrapper, instead.
1437 (defvar *in-obsolete-instance-trap* nil)
1438 (defvar *the-wrapper-of-structure-object*
1439 (class-wrapper (find-class 'structure-object)))
1441 (define-condition obsolete-structure (error)
1442 ((datum :reader obsolete-structure-datum :initarg :datum))
1444 (lambda (condition stream)
1445 ;; Don't try to print the structure, since it probably won't work.
1447 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1448 (type-of (obsolete-structure-datum condition))))))
1450 (defun %obsolete-instance-trap (owrapper nwrapper instance)
1451 (if (not (layout-for-std-class-p owrapper))
1452 (if *in-obsolete-instance-trap*
1453 *the-wrapper-of-structure-object*
1454 (let ((*in-obsolete-instance-trap* t))
1455 (error 'obsolete-structure :datum instance)))
1456 (let* ((class (wrapper-class* nwrapper))
1457 (copy (allocate-instance class)) ;??? allocate-instance ???
1458 (olayout (wrapper-instance-slots-layout owrapper))
1459 (nlayout (wrapper-instance-slots-layout nwrapper))
1460 (oslots (get-slots instance))
1461 (nslots (get-slots copy))
1462 (oclass-slots (wrapper-class-slots owrapper))
1466 (safe (safe-p class)))
1468 ;; local --> local transfer value, check type
1469 ;; local --> shared discard value, discard slot
1470 ;; local --> -- discard slot
1471 ;; shared --> local transfer value, check type
1472 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1473 ;; shared --> -- discard value
1474 ;; -- --> local add slot
1477 (flet ((set-value (value npos &optional (otype t))
1479 (let ((ntype (cdr (nth npos nlayout))))
1480 (unless (equal ntype otype)
1481 (assert (typep value ntype) (value)
1482 "~@<Error updating obsolete instance. Current value in slot ~
1483 ~S of an instance of ~S is ~S, which does not match the new ~
1485 (car (nth npos nlayout)) class value ntype))))
1486 (setf (clos-slots-ref nslots npos) value)))
1487 ;; Go through all the old local slots.
1489 (dolist (spec olayout)
1490 (destructuring-bind (name . otype) spec
1491 (let ((npos (position name nlayout :key #'car)))
1493 (set-value (clos-slots-ref oslots opos) npos otype)
1495 (push name discarded)
1496 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1497 (setf (getf plist name) (clos-slots-ref oslots opos)))))))
1500 ;; Go through all the old shared slots.
1501 (dolist (oclass-slot-and-val oclass-slots)
1502 (let ((name (car oclass-slot-and-val))
1503 (val (cdr oclass-slot-and-val)))
1504 (let ((npos (position name nlayout :key #'car)))
1506 (set-value val npos))))))
1508 ;; Go through all the new local slots to compute the added slots.
1509 (dolist (spec nlayout)
1510 (let ((name (car spec)))
1511 (unless (or (member name olayout :key #'car)
1512 (assq name oclass-slots))
1513 (push name added))))
1515 (%swap-wrappers-and-slots instance copy)
1517 (update-instance-for-redefined-class instance
1523 (defun %change-class (instance new-class initargs)
1524 (let* ((old-class (class-of instance))
1525 (copy (allocate-instance new-class))
1526 (new-wrapper (get-wrapper copy))
1527 (old-wrapper (class-wrapper old-class))
1528 (old-layout (wrapper-instance-slots-layout old-wrapper))
1529 (new-layout (wrapper-instance-slots-layout new-wrapper))
1530 (old-slots (get-slots instance))
1531 (new-slots (get-slots copy))
1532 (old-class-slots (wrapper-class-slots old-wrapper))
1533 (safe (safe-p new-class)))
1535 (flet ((set-value (value pos)
1537 (let ((spec (nth pos new-layout)))
1538 (assert (typep value (cdr spec)) (value)
1539 "~@<Error changing class. Current value in slot ~S ~
1540 of an instance of ~S is ~S, which does not match the new ~
1541 slot type ~S in class ~S.~:@>"
1542 (car spec) old-class value
1543 (cdr spec) new-class)))
1544 (setf (clos-slots-ref new-slots pos) value)))
1545 ;; "The values of local slots specified by both the class CTO and
1546 ;; CFROM are retained. If such a local slot was unbound, it
1547 ;; remains unbound."
1548 (let ((new-position 0))
1549 (dolist (new-slot new-layout)
1550 (let* ((name (car new-slot))
1551 (old-position (position name old-layout :key #'car)))
1553 (set-value (clos-slots-ref old-slots old-position)
1555 (incf new-position)))
1557 ;; "The values of slots specified as shared in the class CFROM and
1558 ;; as local in the class CTO are retained."
1559 (dolist (slot-and-val old-class-slots)
1560 (let ((position (position (car slot-and-val) new-layout :key #'car)))
1562 (set-value (cdr slot-and-val) position)))))
1564 ;; Make the copy point to the old instance's storage, and make the
1565 ;; old instance point to the new storage.
1566 (%swap-wrappers-and-slots instance copy)
1568 (apply #'update-instance-for-different-class copy instance initargs)
1572 (defmethod change-class ((instance standard-object) (new-class standard-class)
1575 (unless (class-finalized-p new-class)
1576 (finalize-inheritance new-class))
1577 (let ((cpl (class-precedence-list new-class)))
1581 `(when (eq class (find-class ',class-name))
1582 (error 'metaobject-initialization-violation
1583 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1584 :format-arguments (list 'change-class ',class-name)
1585 :references (list '(:amop :initialization ,class-name))))))
1587 (frob generic-function)
1589 (frob slot-definition))))
1590 (%change-class instance new-class initargs)))
1592 (defmethod change-class ((instance forward-referenced-class)
1593 (new-class standard-class) &rest initargs)
1595 (let ((cpl (class-precedence-list new-class)))
1597 (error 'metaobject-initialization-violation
1599 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1601 (list 'change-class 'forward-referenced-class 'class)
1603 (list '(:amop :generic-function ensure-class-using-class)
1604 '(:amop :initialization class))))
1605 (when (eq class (find-class 'class))
1607 (%change-class instance new-class initargs)))
1609 (defmethod change-class ((instance funcallable-standard-object)
1610 (new-class funcallable-standard-class)
1613 (let ((cpl (class-precedence-list new-class)))
1617 `(when (eq class (find-class ',class-name))
1618 (error 'metaobject-initialization-violation
1619 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1620 :format-arguments (list 'change-class ',class-name)
1621 :references (list '(:amop :initialization ,class-name))))))
1623 (frob generic-function)
1625 (frob slot-definition))))
1626 (%change-class instance new-class initargs)))
1628 (defmethod change-class ((instance standard-object)
1629 (new-class funcallable-standard-class)
1631 (declare (ignore initargs))
1632 (error "You can't change the class of ~S to ~S~@
1633 because it isn't already an instance with metaclass ~S."
1634 instance new-class 'standard-class))
1636 (defmethod change-class ((instance funcallable-standard-object)
1637 (new-class standard-class)
1639 (declare (ignore initargs))
1640 (error "You can't change the class of ~S to ~S~@
1641 because it isn't already an instance with metaclass ~S."
1642 instance new-class 'funcallable-standard-class))
1644 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1645 (apply #'change-class instance (find-class new-class-name) initargs))
1647 ;;;; The metaclass BUILT-IN-CLASS
1649 ;;;; This metaclass is something of a weird creature. By this point, all
1650 ;;;; instances of it which will exist have been created, and no instance
1651 ;;;; is ever created by calling MAKE-INSTANCE.
1653 ;;;; But, there are other parts of the protocol we must follow and those
1654 ;;;; definitions appear here.
1656 (macrolet ((def (name args control)
1657 `(defmethod ,name ,args
1658 (declare (ignore initargs))
1659 (error 'metaobject-initialization-violation
1660 :format-control ,(format nil "~@<~A~@:>" control)
1661 :format-arguments (list ',name)
1662 :references (list '(:amop :initialization "Class"))))))
1663 (def initialize-instance ((class built-in-class) &rest initargs)
1664 "Cannot ~S an instance of BUILT-IN-CLASS.")
1665 (def reinitialize-instance ((class built-in-class) &rest initargs)
1666 "Cannot ~S an instance of BUILT-IN-CLASS."))
1668 (macrolet ((def (name)
1669 `(defmethod ,name ((class built-in-class)) nil)))
1670 (def class-direct-slots)
1672 (def class-direct-default-initargs)
1673 (def class-default-initargs))
1675 (defmethod validate-superclass ((c class) (s built-in-class))
1676 (or (eq s *the-class-t*) (eq s *the-class-stream*)
1677 ;; FIXME: bad things happen if someone tries to mix in both
1678 ;; FILE-STREAM and STRING-STREAM (as they have the same
1679 ;; layout-depthoid). Is there any way we can provide a useful
1680 ;; error message? -- CSR, 2005-05-03
1681 (eq s *the-class-file-stream*) (eq s *the-class-string-stream*)
1682 ;; This probably shouldn't be mixed in with certain other
1683 ;; classes, too, but it seems to work both with STANDARD-OBJECT
1684 ;; and FUNCALLABLE-STANDARD-OBJECT
1685 (eq s *the-class-sequence*)))
1687 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1688 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1689 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1690 (macrolet ((def (method)
1691 `(defmethod ,method ((class forward-referenced-class))
1692 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1694 (def class-default-initargs)
1695 (def class-precedence-list)
1698 (defmethod validate-superclass ((c slot-class)
1699 (f forward-referenced-class))
1702 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1703 (pushnew dependent (plist-value metaobject 'dependents) :test #'eq))
1705 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1706 (setf (plist-value metaobject 'dependents)
1707 (delete dependent (plist-value metaobject 'dependents))))
1709 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1710 (dolist (dependent (plist-value metaobject 'dependents))
1711 (funcall function dependent)))