1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
26 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
28 (reader (slot-definition-reader-function slotd))
29 (writer (slot-definition-writer-function slotd))
30 (boundp (slot-definition-boundp-function slotd))))
32 (defmethod (setf slot-accessor-function) (function
33 (slotd effective-slot-definition)
36 (reader (setf (slot-definition-reader-function slotd) function))
37 (writer (setf (slot-definition-writer-function slotd) function))
38 (boundp (setf (slot-definition-boundp-function slotd) function))))
40 (defconstant +slotd-reader-function-std-p+ 1)
41 (defconstant +slotd-writer-function-std-p+ 2)
42 (defconstant +slotd-boundp-function-std-p+ 4)
43 (defconstant +slotd-all-function-std-p+ 7)
45 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
46 (let ((flags (slot-value slotd 'accessor-flags)))
47 (declare (type fixnum flags))
49 (eql +slotd-all-function-std-p+ flags)
50 (let ((mask (ecase type
51 (reader +slotd-reader-function-std-p+)
52 (writer +slotd-writer-function-std-p+)
53 (boundp +slotd-boundp-function-std-p+))))
54 (declare (type fixnum mask))
55 (not (zerop (the fixnum (logand mask flags))))))))
57 (defmethod (setf slot-accessor-std-p) (value
58 (slotd effective-slot-definition)
60 (let ((mask (ecase type
61 (reader +slotd-reader-function-std-p+)
62 (writer +slotd-writer-function-std-p+)
63 (boundp +slotd-boundp-function-std-p+)))
64 (flags (slot-value slotd 'accessor-flags)))
65 (declare (type fixnum mask flags))
66 (setf (slot-value slotd 'accessor-flags)
68 (the fixnum (logior mask flags))
69 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
72 (defmethod initialize-internal-slot-functions ((slotd
73 effective-slot-definition))
74 (let* ((name (slot-value slotd 'name))
75 (class (slot-value slotd 'class)))
76 (let ((table (or (gethash name *name->class->slotd-table*)
77 (setf (gethash name *name->class->slotd-table*)
78 (make-hash-table :test 'eq :size 5)))))
79 (setf (gethash class table) slotd))
80 (dolist (type '(reader writer boundp))
81 (let* ((gf-name (ecase type
82 (reader 'slot-value-using-class)
83 (writer '(setf slot-value-using-class))
84 (boundp 'slot-boundp-using-class)))
85 (gf (gdefinition gf-name)))
86 (compute-slot-accessor-info slotd type gf)))
87 (initialize-internal-slot-gfs name)))
89 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
91 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
92 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
93 ;;; writing/testing effective slot SLOTD.
95 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
96 ;;; GF. Store the effective method in the effective slot definition
97 ;;; object itself; these GFs have special dispatch functions calling
98 ;;; effective methods directly retrieved from effective slot
99 ;;; definition objects, as an optimization.
101 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
103 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
105 (let* ((name (slot-value slotd 'name))
106 (class (slot-value slotd 'class))
107 (old-slotd (find-slot-definition class name))
108 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
109 (multiple-value-bind (function std-p)
110 (if (eq *boot-state* 'complete)
111 (get-accessor-method-function gf type class slotd)
112 (get-optimized-std-accessor-method-function class slotd type))
113 (setf (slot-accessor-std-p slotd type) std-p)
114 (setf (slot-accessor-function slotd type) function))
115 (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
116 (push (cons class name) *pv-table-cache-update-info*))))
118 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
121 ;;;; various class accessors that are a little more complicated than can be
122 ;;;; done with automatically generated reader methods
124 (defmethod class-prototype ((class std-class))
125 (with-slots (prototype) class
126 (or prototype (setq prototype (allocate-instance class)))))
128 (defmethod class-prototype ((class structure-class))
129 (with-slots (prototype wrapper defstruct-constructor) class
132 (if defstruct-constructor
133 (allocate-instance class)
134 (allocate-standard-instance wrapper))))))
136 (defmethod class-direct-default-initargs ((class slot-class))
137 (plist-value class 'direct-default-initargs))
139 (defmethod class-default-initargs ((class slot-class))
140 (plist-value class 'default-initargs))
142 (defmethod class-slot-cells ((class std-class))
143 (plist-value class 'class-slot-cells))
145 ;;;; class accessors that are even a little bit more complicated than those
146 ;;;; above. These have a protocol for updating them, we must implement that
149 ;;; Maintaining the direct subclasses backpointers. The update methods are
150 ;;; here, the values are read by an automatically generated reader method.
151 (defmethod add-direct-subclass ((class class) (subclass class))
152 (with-slots (direct-subclasses) class
153 (pushnew subclass direct-subclasses)
155 (defmethod remove-direct-subclass ((class class) (subclass class))
156 (with-slots (direct-subclasses) class
157 (setq direct-subclasses (remove subclass direct-subclasses))
160 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
162 ;;; There are four generic functions involved, each has one method for the
163 ;;; class case and another method for the damned EQL specializers. All of
164 ;;; these are specified methods and appear in their specified place in the
167 ;;; ADD-DIRECT-METHOD
168 ;;; REMOVE-DIRECT-METHOD
169 ;;; SPECIALIZER-DIRECT-METHODS
170 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
172 ;;; In each case, we maintain one value which is a cons. The car is the list
173 ;;; methods. The cdr is a list of the generic functions. The cdr is always
175 (defmethod add-direct-method ((specializer class) (method method))
176 (with-slots (direct-methods) specializer
177 (setf (car direct-methods) (adjoin method (car direct-methods)) ;PUSH
178 (cdr direct-methods) ()))
180 (defmethod remove-direct-method ((specializer class) (method method))
181 (with-slots (direct-methods) specializer
182 (setf (car direct-methods) (remove method (car direct-methods))
183 (cdr direct-methods) ()))
186 (defmethod specializer-direct-methods ((specializer class))
187 (with-slots (direct-methods) specializer
188 (car direct-methods)))
190 (defmethod specializer-direct-generic-functions ((specializer class))
191 (with-slots (direct-methods) specializer
192 (or (cdr direct-methods)
193 (setf (cdr direct-methods)
195 (dolist (m (car direct-methods))
196 ;; the old PCL code used COLLECTING-ONCE which used
197 ;; #'EQ to check for newness
198 (pushnew (method-generic-function m) collect :test #'eq))
199 (nreverse collect))))))
201 ;;; This hash table is used to store the direct methods and direct generic
202 ;;; functions of EQL specializers. Each value in the table is the cons.
203 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
204 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
206 (defmethod specializer-method-table ((specializer eql-specializer))
207 *eql-specializer-methods*)
209 (defmethod specializer-method-table ((specializer class-eq-specializer))
210 *class-eq-specializer-methods*)
212 (defmethod add-direct-method ((specializer specializer-with-object)
214 (let* ((object (specializer-object specializer))
215 (table (specializer-method-table specializer))
216 (entry (gethash object table)))
219 (setf (gethash object table)
221 (setf (car entry) (adjoin method (car entry))
225 (defmethod remove-direct-method ((specializer specializer-with-object)
227 (let* ((object (specializer-object specializer))
228 (entry (gethash object (specializer-method-table specializer))))
230 (setf (car entry) (remove method (car entry))
234 (defmethod specializer-direct-methods ((specializer specializer-with-object))
235 (car (gethash (specializer-object specializer)
236 (specializer-method-table specializer))))
238 (defmethod specializer-direct-generic-functions ((specializer
239 specializer-with-object))
240 (let* ((object (specializer-object specializer))
241 (entry (gethash object (specializer-method-table specializer))))
246 (dolist (m (car entry))
247 (pushnew (method-generic-function m) collect :test #'eq))
248 (nreverse collect)))))))
250 (defun map-specializers (function)
251 (map-all-classes (lambda (class)
252 (funcall function (class-eq-specializer class))
253 (funcall function class)))
254 (maphash (lambda (object methods)
255 (declare (ignore methods))
256 (intern-eql-specializer object))
257 *eql-specializer-methods*)
258 (maphash (lambda (object specl)
259 (declare (ignore object))
260 (funcall function specl))
261 *eql-specializer-table*)
264 (defun map-all-generic-functions (function)
265 (let ((all-generic-functions (make-hash-table :test 'eq)))
266 (map-specializers (lambda (specl)
267 (dolist (gf (specializer-direct-generic-functions
269 (unless (gethash gf all-generic-functions)
270 (setf (gethash gf all-generic-functions) t)
271 (funcall function gf))))))
274 (defmethod shared-initialize :after ((specl class-eq-specializer)
277 (declare (ignore slot-names))
278 (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
280 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
281 (declare (ignore slot-names))
282 (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
284 (defun real-load-defclass (name metaclass-name supers slots other)
285 (let ((res (apply #'ensure-class name :metaclass metaclass-name
286 :direct-superclasses supers
288 :definition-source `((defclass ,name)
293 (setf (gdefinition 'load-defclass) #'real-load-defclass)
295 (defun ensure-class (name &rest args)
296 (apply #'ensure-class-using-class
297 (let ((class (find-class name nil)))
298 (when (and class (eq name (class-name class)))
299 ;; NAME is the proper name of CLASS, so redefine it
304 (defmethod ensure-class-using-class ((class null) name &rest args &key)
305 (multiple-value-bind (meta initargs)
306 (ensure-class-values class args)
307 (set-class-type-translation (class-prototype meta) name)
308 (setf class (apply #'make-instance meta :name name initargs)
309 (find-class name) class)
310 (set-class-type-translation class name)
313 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
314 (multiple-value-bind (meta initargs)
315 (ensure-class-values class args)
316 (unless (eq (class-of class) meta)
317 (apply #'change-class class meta initargs))
318 (apply #'reinitialize-instance class initargs)
319 (setf (find-class name) class)
320 (set-class-type-translation class name)
323 (defmethod class-predicate-name ((class t))
328 ((not (legal-class-name-p s))
329 (error "~S is not a class or a legal class name." s))
331 (or (find-class s nil)
332 (make-instance 'forward-referenced-class
335 (defun ensure-class-values (class args)
336 (let* ((initargs (copy-list args))
337 (unsupplied (list 1))
338 (supplied-meta (getf initargs :metaclass unsupplied))
339 (supplied-supers (getf initargs :direct-superclasses unsupplied))
340 (supplied-slots (getf initargs :direct-slots unsupplied))
342 (cond ((neq supplied-meta unsupplied)
343 (find-class supplied-meta))
345 (forward-referenced-class-p class))
346 *the-class-standard-class*)
349 ;; KLUDGE: It seemed to me initially that there ought to be a way
350 ;; of collecting all the erroneous problems in one go, rather than
351 ;; this way of solving the problem of signalling the errors that
352 ;; we are required to, which stops at the first bogus input.
353 ;; However, after playing around a little, I couldn't find that
354 ;; way, so I've left it as is, but if someone does come up with a
355 ;; better way... -- CSR, 2002-09-08
356 (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
357 ((endp direct-slots) nil)
358 (destructuring-bind (slot &rest more) direct-slots
359 (let ((slot-name (getf slot :name)))
360 (when (some (lambda (s) (eq slot-name (getf s :name))) more)
361 ;; FIXME: It's quite possible that we ought to define an
362 ;; SB-INT:PROGRAM-ERROR function to signal these and other
363 ;; errors throughout the codebase that are required to be
364 ;; of type PROGRAM-ERROR.
365 (error 'simple-program-error
366 :format-control "~@<There is more than one direct slot ~
368 :format-arguments (list slot-name)))
369 (do ((stuff slot (cddr stuff)))
371 (destructuring-bind (option value &rest more) stuff
373 ((and (member option '(:allocation :type
374 :initform :documentation))
376 (getf more option unsupplied))))
377 (error 'simple-program-error
378 :format-control "~@<Duplicate slot option ~S for ~
380 :format-arguments (list option slot-name)))
381 ((and (eq option :readers)
382 (notevery #'symbolp value))
383 (error 'simple-program-error
384 :format-control "~@<Slot reader names for slot ~
385 named ~S must be symbols.~:>"
386 :format-arguments (list slot-name)))
387 ((and (eq option :initargs)
388 (notevery #'symbolp value))
389 (error 'simple-program-error
390 :format-control "~@<Slot initarg names for slot ~
391 named ~S must be symbols.~:>"
392 :format-arguments (list slot-name)))))))))
393 (loop for (initarg . more) on (getf initargs :direct-default-initargs)
394 for name = (car initarg)
395 when (some (lambda (a) (eq (car a) name)) more)
396 do (error 'simple-program-error
397 :format-control "~@<Duplicate initialization argument ~
398 name ~S in :DEFAULT-INITARGS.~:>"
399 :format-arguments (list name class)))
401 (default-initargs 0))
402 (do ((args initargs (cddr args)))
406 (when (> (incf metaclass) 1)
407 (error 'simple-program-error
408 :format-control "~@<More than one :METACLASS ~
409 option specified.~:>")))
410 (:direct-default-initargs
411 (when (> (incf default-initargs) 1)
412 (error 'simple-program-error
413 :format-control "~@<More than one :DEFAULT-INITARGS ~
414 option specified.~:>"))))))
415 (remf initargs :metaclass)
416 (loop (unless (remf initargs :direct-superclasses) (return)))
417 (loop (unless (remf initargs :direct-slots) (return)))
421 (when (neq supplied-supers unsupplied)
422 (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
423 (when (neq supplied-slots unsupplied)
424 (list :direct-slots supplied-slots))
427 (defmethod shared-initialize :after
430 &key (direct-superclasses nil direct-superclasses-p)
431 (direct-slots nil direct-slots-p)
432 (direct-default-initargs nil direct-default-initargs-p)
433 (predicate-name nil predicate-name-p))
434 (declare (ignore slot-names))
435 (cond (direct-superclasses-p
436 (setq direct-superclasses
437 (or direct-superclasses
438 (list (if (funcallable-standard-class-p class)
439 *the-class-funcallable-standard-object*
440 *the-class-standard-object*))))
441 (dolist (superclass direct-superclasses)
442 (unless (validate-superclass class superclass)
443 (error "The class ~S was specified as a~%
444 super-class of the class ~S;~%~
445 but the meta-classes ~S and~%~S are incompatible.~@
446 Define a method for ~S to avoid this error."
447 superclass class (class-of superclass) (class-of class)
448 'validate-superclass)))
449 (setf (slot-value class 'direct-superclasses) direct-superclasses))
451 (setq direct-superclasses (slot-value class 'direct-superclasses))))
454 (setf (slot-value class 'direct-slots)
455 (mapcar (lambda (pl) (make-direct-slotd class pl))
457 (slot-value class 'direct-slots)))
458 (if direct-default-initargs-p
459 (setf (plist-value class 'direct-default-initargs)
460 direct-default-initargs)
461 (setq direct-default-initargs
462 (plist-value class 'direct-default-initargs)))
463 (setf (plist-value class 'class-slot-cells)
464 ;; The below initializes shared slots from direct initforms,
465 ;; but one might inherit initforms from superclasses
466 ;; (cf. UPDATE-SHARED-SLOT-VALUES).
468 (dolist (dslotd direct-slots)
469 (when (eq :class (slot-definition-allocation dslotd))
470 (let ((initfunction (slot-definition-initfunction dslotd)))
471 (push (cons (slot-definition-name dslotd)
473 (funcall initfunction)
477 (setq predicate-name (if predicate-name-p
478 (setf (slot-value class 'predicate-name)
479 (car predicate-name))
480 (or (slot-value class 'predicate-name)
481 (setf (slot-value class 'predicate-name)
482 (make-class-predicate-name (class-name
484 (add-direct-subclasses class direct-superclasses)
485 (make-class-predicate class predicate-name)
486 (update-class class nil)
487 (do* ((slots (slot-value class 'slots) (cdr slots))
489 ((null slots) (when dupes
491 ;; FIXME: the indentation request ("~4I")
492 ;; below appears not to do anything. Finding
493 ;; out why would be nice. -- CSR, 2003-04-24
494 "~@<slot names with the same SYMBOL-NAME but ~
495 different SYMBOL-PACKAGE (possible package problem) ~
496 for class ~S:~@:_~4I~<~@{~S~^~:@_~}~:>~@:>"
499 (let* ((slot (car slots))
500 (oslots (remove (slot-definition-name slot) (cdr slots)
501 :test-not #'string= :key #'slot-definition-name)))
503 (pushnew (cons (slot-definition-name slot)
504 (mapcar #'slot-definition-name oslots))
506 :test #'string= :key #'car))))
507 (add-slot-accessors class direct-slots)
508 (make-preliminary-layout class))
510 (defmethod shared-initialize :after ((class forward-referenced-class)
511 slot-names &key &allow-other-keys)
512 (declare (ignore slot-names))
513 (make-preliminary-layout class))
515 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
517 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
518 ;;; make it known to the type system.
519 (defun make-preliminary-layout (class)
520 (flet ((compute-preliminary-cpl (root)
521 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
522 (compute-class-precedence-list root))))
523 (unless (class-finalized-p class)
524 (let ((name (class-name class)))
525 (setf (find-class name) class)
526 ;; KLUDGE: This is fairly horrible. We need to make a
527 ;; full-fledged CLASSOID here, not just tell the compiler that
528 ;; some class is forthcoming, because there are legitimate
529 ;; questions one can ask of the type system, implemented in
530 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
531 (when (and (eq *boot-state* 'complete)
532 (null (find-classoid name nil)))
533 (setf (find-classoid name)
534 (make-standard-classoid :name name)))
535 (set-class-type-translation class name)
536 (let ((layout (make-wrapper 0 class))
537 (classoid (find-classoid name)))
538 (setf (layout-classoid layout) classoid)
539 (setf (classoid-pcl-class classoid) class)
540 (setf (slot-value class 'wrapper) layout)
541 (let ((cpl (compute-preliminary-cpl class)))
542 (setf (layout-inherits layout)
543 (order-layout-inherits
544 (map 'simple-vector #'class-wrapper
545 (reverse (rest cpl))))))
546 (register-layout layout :invalidate t)
547 (setf (classoid-layout classoid) layout)
548 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))))
551 (defmethod shared-initialize :before ((class class) slot-names &key name)
552 (declare (ignore slot-names name))
553 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
554 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
555 (setf (slot-value class 'type) `(class ,class))
556 (setf (slot-value class 'class-eq-specializer)
557 (make-instance 'class-eq-specializer :class class)))
559 (defmethod reinitialize-instance :before ((class slot-class) &key)
560 (remove-direct-subclasses class (class-direct-superclasses 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 shared-initialize :after ((class condition-class) slot-names
571 &key direct-slots direct-superclasses)
572 (declare (ignore slot-names))
573 (let ((classoid (find-classoid (class-name class))))
574 (with-slots (wrapper class-precedence-list prototype predicate-name
575 (direct-supers direct-superclasses))
577 (setf (slot-value class 'direct-slots)
578 (mapcar (lambda (pl) (make-direct-slotd class pl))
580 (setf (slot-value class 'finalized-p) t)
581 (setf (classoid-pcl-class classoid) class)
582 (setq direct-supers direct-superclasses)
583 (setq wrapper (classoid-layout classoid))
584 (setq class-precedence-list (compute-class-precedence-list class))
585 (setq prototype (make-condition (class-name class)))
586 (add-direct-subclasses class direct-superclasses)
587 (setq predicate-name (make-class-predicate-name (class-name class)))
588 (make-class-predicate class predicate-name)
589 (setf (slot-value class 'slots) (compute-slots class))))
590 ;; Comment from Gerd's PCL, 2003-05-15:
592 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
593 ;; override condition accessors with generic functions. We do this
595 (update-pv-table-cache-info class))
597 (defmethod direct-slot-definition-class ((class condition-class)
599 (declare (ignore initargs))
600 (find-class 'condition-direct-slot-definition))
602 (defmethod effective-slot-definition-class ((class condition-class)
604 (declare (ignore initargs))
605 (find-class 'condition-effective-slot-definition))
607 (defmethod finalize-inheritance ((class condition-class))
608 (aver (slot-value class 'finalized-p))
611 (defmethod compute-effective-slot-definition
612 ((class condition-class) slot-name dslotds)
613 (let ((slotd (call-next-method)))
614 (setf (slot-definition-reader-function slotd)
616 (handler-case (condition-reader-function x slot-name)
617 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
618 ;; is unbound; maybe it should be a CELL-ERROR of some
620 (error () (values (slot-unbound class x slot-name))))))
621 (setf (slot-definition-writer-function slotd)
623 (condition-writer-function x v slot-name)))
624 (setf (slot-definition-boundp-function slotd)
626 (multiple-value-bind (v c)
627 (ignore-errors (condition-reader-function x slot-name))
632 (defmethod compute-slots ((class condition-class))
633 (mapcan (lambda (superclass)
634 (mapcar (lambda (dslotd)
635 (compute-effective-slot-definition
636 class (slot-definition-name dslotd) (list dslotd)))
637 (class-direct-slots superclass)))
638 (reverse (slot-value class 'class-precedence-list))))
640 (defmethod compute-slots :around ((class condition-class))
641 (let ((eslotds (call-next-method)))
642 (mapc #'initialize-internal-slot-functions eslotds)
645 (defmethod shared-initialize :after
646 ((slotd structure-slot-definition) slot-names &key
647 (allocation :instance) allocation-class)
648 (declare (ignore slot-names allocation-class))
649 (unless (eq allocation :instance)
650 (error "Structure slots must have :INSTANCE allocation.")))
652 (defun make-structure-class-defstruct-form (name direct-slots include)
653 (let* ((conc-name (intern (format nil "~S structure class " name)))
654 (constructor (intern (format nil "~Aconstructor" conc-name)))
655 (defstruct `(defstruct (,name
657 `((:include ,(class-name include))))
659 (:conc-name ,conc-name)
660 (:constructor ,constructor ())
662 ,@(mapcar (lambda (slot)
663 `(,(slot-definition-name slot)
666 (reader-names (mapcar (lambda (slotd)
667 (list 'slot-accessor name
668 (slot-definition-name slotd)
671 (writer-names (mapcar (lambda (slotd)
672 (list 'slot-accessor name
673 (slot-definition-name slotd)
677 (mapcar (lambda (slotd reader-name)
679 (slot-definition-defstruct-accessor-symbol
681 `(defun ,reader-name (obj)
682 (declare (type ,name obj))
684 direct-slots reader-names))
686 (mapcar (lambda (slotd writer-name)
688 (slot-definition-defstruct-accessor-symbol
690 `(defun ,writer-name (nv obj)
691 (declare (type ,name obj))
692 (setf (,accessor obj) nv))))
693 direct-slots writer-names))
697 ,@readers-init ,@writers-init
699 (values defstruct-form constructor reader-names writer-names)))
701 (defun make-defstruct-allocation-function (class)
702 (let ((dd (get-structure-dd (class-name class))))
704 (let ((instance (%make-instance (dd-length dd)))
705 (raw-index (dd-raw-index dd)))
706 (setf (%instance-layout instance)
707 (sb-kernel::compiler-layout-or-lose (dd-name dd)))
709 (setf (%instance-ref instance raw-index)
710 (make-array (dd-raw-length dd)
711 :element-type '(unsigned-byte 32))))
714 (defmethod shared-initialize :after
715 ((class structure-class)
717 &key (direct-superclasses nil direct-superclasses-p)
718 (direct-slots nil direct-slots-p)
719 direct-default-initargs
720 (predicate-name nil predicate-name-p))
721 (declare (ignore slot-names direct-default-initargs))
722 (if direct-superclasses-p
723 (setf (slot-value class 'direct-superclasses)
724 (or direct-superclasses
725 (setq direct-superclasses
726 (and (not (eq (class-name class) 'structure-object))
727 (list *the-class-structure-object*)))))
728 (setq direct-superclasses (slot-value class 'direct-superclasses)))
729 (let* ((name (class-name class))
730 (from-defclass-p (slot-value class 'from-defclass-p))
731 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
733 (setf (slot-value class 'direct-slots)
737 (let* ((slot-name (getf pl :name))
740 "~S structure class ~A"
742 (accessor (intern acc-name)))
743 (setq pl (list* :defstruct-accessor-symbol
745 (make-direct-slotd class pl))
747 (setq direct-slots (slot-value class 'direct-slots)))
749 (let ((include (car (slot-value class 'direct-superclasses))))
750 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
751 (make-structure-class-defstruct-form name direct-slots include)
752 (unless (structure-type-p name) (eval defstruct-form))
753 (mapc (lambda (dslotd reader-name writer-name)
754 (let* ((reader (gdefinition reader-name))
755 (writer (when (gboundp writer-name)
756 (gdefinition writer-name))))
757 (setf (slot-value dslotd 'internal-reader-function)
759 (setf (slot-value dslotd 'internal-writer-function)
761 direct-slots reader-names writer-names)
762 (setf (slot-value class 'defstruct-form) defstruct-form)
763 (setf (slot-value class 'defstruct-constructor) constructor)))
764 (setf (slot-value class 'defstruct-constructor)
765 (make-defstruct-allocation-function class)))
766 (add-direct-subclasses class direct-superclasses)
767 (setf (slot-value class 'class-precedence-list)
768 (compute-class-precedence-list class))
769 (setf (slot-value class 'slots) (compute-slots class))
770 (let ((lclass (find-classoid (class-name class))))
771 (setf (classoid-pcl-class lclass) class)
772 (setf (slot-value class 'wrapper) (classoid-layout lclass)))
773 (setf (slot-value class 'finalized-p) t)
774 (update-pv-table-cache-info class)
775 (setq predicate-name (if predicate-name-p
776 (setf (slot-value class 'predicate-name)
777 (car predicate-name))
778 (or (slot-value class 'predicate-name)
779 (setf (slot-value class 'predicate-name)
780 (make-class-predicate-name
781 (class-name class))))))
782 (make-class-predicate class predicate-name)
783 (add-slot-accessors class direct-slots)))
785 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
786 (declare (ignore initargs))
787 (find-class 'structure-direct-slot-definition))
789 (defmethod finalize-inheritance ((class structure-class))
790 nil) ; always finalized
792 (defun add-slot-accessors (class dslotds)
793 (fix-slot-accessors class dslotds 'add))
795 (defun remove-slot-accessors (class dslotds)
796 (fix-slot-accessors class dslotds 'remove))
798 (defun fix-slot-accessors (class dslotds add/remove)
799 (flet ((fix (gfspec name r/w)
800 (let ((gf (ensure-generic-function gfspec)))
802 (r (if (eq add/remove 'add)
803 (add-reader-method class gf name)
804 (remove-reader-method class gf)))
805 (w (if (eq add/remove 'add)
806 (add-writer-method class gf name)
807 (remove-writer-method class gf)))))))
808 (dolist (dslotd dslotds)
809 (let ((slot-name (slot-definition-name dslotd)))
810 (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
811 (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
813 (defun add-direct-subclasses (class supers)
814 (dolist (super supers)
815 (unless (memq class (class-direct-subclasses class))
816 (add-direct-subclass super class))))
818 (defun remove-direct-subclasses (class supers)
819 (let ((old (class-direct-superclasses class)))
820 (dolist (o (set-difference old supers))
821 (remove-direct-subclass o class))))
823 (defmethod finalize-inheritance ((class std-class))
824 (update-class class t))
826 (defmethod finalize-inheritance ((class forward-referenced-class))
827 ;; FIXME: should we not be thinking a bit about what kinds of error
828 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
829 ;; possibly a forward-referenced-class-error, though that's
830 ;; difficult given e.g. class precedence list calculations...
832 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
837 (defun class-has-a-forward-referenced-superclass-p (class)
838 (or (forward-referenced-class-p class)
839 (some #'class-has-a-forward-referenced-superclass-p
840 (class-direct-superclasses class))))
842 ;;; This is called by :after shared-initialize whenever a class is initialized
843 ;;; or reinitialized. The class may or may not be finalized.
844 (defun update-class (class finalizep)
845 ;; Comment from Gerd Moellmann:
847 ;; Note that we can't simply delay the finalization when CLASS has
848 ;; no forward referenced superclasses because that causes bootstrap
850 (when (and (not finalizep)
851 (not (class-finalized-p class))
852 (not (class-has-a-forward-referenced-superclass-p class)))
853 (finalize-inheritance class)
854 (return-from update-class))
855 (when (or finalizep (class-finalized-p class)
856 (not (class-has-a-forward-referenced-superclass-p class)))
857 (setf (find-class (class-name class)) class)
858 (update-cpl class (compute-class-precedence-list class))
859 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
860 ;; class. The hoops above are to ensure that FINALIZE-INHERITANCE
861 ;; is called at finalization, so that MOP programmers can hook
862 ;; into the system as described in "Class Finalization Protocol"
863 ;; (section 5.5.2 of AMOP).
864 (update-slots class (compute-slots class))
865 (update-gfs-of-class class)
866 (update-inits class (compute-default-initargs class))
867 (update-shared-slot-values class)
868 (update-ctors 'finalize-inheritance :class class))
870 (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
872 (defun update-shared-slot-values (class)
873 (dolist (slot (class-slots class))
874 (when (eq (slot-definition-allocation slot) :class)
875 (let ((cell (assq (slot-definition-name slot) (class-slot-cells class))))
877 (let ((initfn (slot-definition-initfunction slot)))
879 (setf (cdr cell) (funcall initfn)))))))))
881 (defun update-cpl (class cpl)
882 (if (class-finalized-p class)
883 (unless (and (equal (class-precedence-list class) cpl)
885 (when (position :class (class-direct-slots c)
886 :key #'slot-definition-allocation)
888 ;; comment from the old CMU CL sources:
889 ;; Need to have the cpl setup before update-lisp-class-layout
890 ;; is called on CMU CL.
891 (setf (slot-value class 'class-precedence-list) cpl)
892 (force-cache-flushes class))
893 (setf (slot-value class 'class-precedence-list) cpl))
894 (update-class-can-precede-p cpl))
896 (defun update-class-can-precede-p (cpl)
898 (let ((first (car cpl)))
899 (dolist (c (cdr cpl))
900 (pushnew c (slot-value first 'can-precede-list))))
901 (update-class-can-precede-p (cdr cpl))))
903 (defun class-can-precede-p (class1 class2)
904 (member class2 (class-can-precede-list class1)))
906 (defun update-slots (class eslotds)
907 (let ((instance-slots ())
909 (dolist (eslotd eslotds)
910 (let ((alloc (slot-definition-allocation eslotd)))
912 (:instance (push eslotd instance-slots))
913 (:class (push eslotd class-slots)))))
915 ;; If there is a change in the shape of the instances then the
916 ;; old class is now obsolete.
917 (let* ((nlayout (mapcar #'slot-definition-name
918 (sort instance-slots #'<
919 :key #'slot-definition-location)))
920 (nslots (length nlayout))
921 (nwrapper-class-slots (compute-class-slots class-slots))
922 (owrapper (when (class-finalized-p class)
923 (class-wrapper class)))
924 (olayout (when owrapper
925 (wrapper-instance-slots-layout owrapper)))
926 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
928 (cond ((null owrapper)
929 (make-wrapper nslots class))
930 ((and (equal nlayout olayout)
932 (loop for o in owrapper-class-slots
933 for n in nwrapper-class-slots
934 do (unless (eq (car o) (car n)) (return t)))))
937 ;; This will initialize the new wrapper to have the
938 ;; same state as the old wrapper. We will then have
939 ;; to change that. This may seem like wasted work
940 ;; (and it is), but the spec requires that we call
941 ;; MAKE-INSTANCES-OBSOLETE.
942 (make-instances-obsolete class)
943 (class-wrapper class)))))
945 (with-slots (wrapper slots) class
946 (update-lisp-class-layout class nwrapper)
948 (wrapper-instance-slots-layout nwrapper) nlayout
949 (wrapper-class-slots nwrapper) nwrapper-class-slots
950 (wrapper-no-of-instance-slots nwrapper) nslots
952 (setf (slot-value class 'finalized-p) t)
953 (unless (eq owrapper nwrapper)
954 (update-pv-table-cache-info class)
955 (maybe-update-standard-class-locations class)))))
957 (defun compute-class-slots (eslotds)
959 (dolist (eslotd eslotds)
960 (push (assoc (slot-definition-name eslotd)
961 (class-slot-cells (slot-definition-class eslotd)))
965 (defun update-gfs-of-class (class)
966 (when (and (class-finalized-p class)
967 (let ((cpl (class-precedence-list class)))
968 (or (member *the-class-slot-class* cpl)
969 (member *the-class-standard-effective-slot-definition*
971 (let ((gf-table (make-hash-table :test 'eq)))
972 (labels ((collect-gfs (class)
973 (dolist (gf (specializer-direct-generic-functions class))
974 (setf (gethash gf gf-table) t))
975 (mapc #'collect-gfs (class-direct-superclasses class))))
977 (maphash (lambda (gf ignore)
978 (declare (ignore ignore))
979 (update-gf-dfun class gf))
982 (defun update-inits (class inits)
983 (setf (plist-value class 'default-initargs) inits))
985 (defmethod compute-default-initargs ((class slot-class))
986 (let ((initargs (loop for c in (class-precedence-list class)
987 append (class-direct-default-initargs c))))
988 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
990 ;;;; protocols for constructing direct and effective slot definitions
992 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
993 (declare (ignore initargs))
994 (find-class 'standard-direct-slot-definition))
996 (defun make-direct-slotd (class initargs)
997 (let ((initargs (list* :class class initargs)))
998 (apply #'make-instance
999 (apply #'direct-slot-definition-class class initargs)
1002 (defmethod compute-slots ((class std-class))
1003 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1004 ;; for each different slot name we find in our superclasses. Each
1005 ;; call receives the class and a list of the dslotds with that name.
1006 ;; The list is in most-specific-first order.
1007 (let ((name-dslotds-alist ()))
1008 (dolist (c (class-precedence-list class))
1009 (dolist (slot (class-direct-slots c))
1010 (let* ((name (slot-definition-name slot))
1011 (entry (assq name name-dslotds-alist)))
1013 (push slot (cdr entry))
1014 (push (list name slot) name-dslotds-alist)))))
1015 (mapcar (lambda (direct)
1016 (compute-effective-slot-definition class
1018 (nreverse (cdr direct))))
1019 name-dslotds-alist)))
1021 (defmethod compute-slots ((class standard-class))
1024 (defmethod compute-slots :around ((class standard-class))
1025 (let ((eslotds (call-next-method))
1027 (dolist (eslotd eslotds eslotds)
1028 (setf (slot-definition-location eslotd)
1029 (ecase (slot-definition-allocation eslotd)
1033 (let* ((name (slot-definition-name eslotd))
1034 (from-class (slot-definition-allocation-class eslotd))
1035 (cell (assq name (class-slot-cells from-class))))
1038 (initialize-internal-slot-functions eslotd))))
1040 (defmethod compute-slots ((class funcallable-standard-class))
1043 (defmethod compute-slots :around ((class funcallable-standard-class))
1044 (labels ((instance-slot-names (slotds)
1046 (dolist (slotd slotds (nreverse collect))
1047 (when (eq (slot-definition-allocation slotd) :instance)
1048 (push (slot-definition-name slotd) collect)))))
1049 ;; This sorts slots so that slots of classes later in the CPL
1050 ;; come before slots of other classes. This is crucial for
1051 ;; funcallable instances because it ensures that the slots of
1052 ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
1053 ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
1054 ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
1055 ;; a funcallable instance.
1056 (compute-layout (eslotds)
1058 (names (instance-slot-names eslotds)))
1060 (reverse (class-precedence-list class))
1061 (nreverse (nconc names first)))
1062 (dolist (ss (class-slots class))
1063 (let ((name (slot-definition-name ss)))
1064 (when (member name names)
1066 (setq names (delete name names)))))))))
1067 (let ((all-slotds (call-next-method))
1070 (dolist (slotd all-slotds)
1071 (ecase (slot-definition-allocation slotd)
1072 (:instance (push slotd instance-slots))
1073 (:class (push slotd class-slots))))
1074 (let ((layout (compute-layout instance-slots)))
1075 (dolist (slotd instance-slots)
1076 (setf (slot-definition-location slotd)
1077 (position (slot-definition-name slotd) layout))
1078 (initialize-internal-slot-functions slotd)))
1079 (dolist (slotd class-slots)
1080 (let ((name (slot-definition-name slotd))
1081 (from-class (slot-definition-allocation-class slotd)))
1082 (setf (slot-definition-location slotd)
1083 (assoc name (class-slot-cells from-class)))
1084 (aver (consp (slot-definition-location slotd)))
1085 (initialize-internal-slot-functions slotd)))
1088 (defmethod compute-slots ((class structure-class))
1089 (mapcan (lambda (superclass)
1090 (mapcar (lambda (dslotd)
1091 (compute-effective-slot-definition
1093 (slot-definition-name dslotd)
1095 (class-direct-slots superclass)))
1096 (reverse (slot-value class 'class-precedence-list))))
1098 (defmethod compute-slots :around ((class structure-class))
1099 (let ((eslotds (call-next-method)))
1100 (mapc #'initialize-internal-slot-functions eslotds)
1103 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1104 (declare (ignore name))
1105 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1106 (class (apply #'effective-slot-definition-class class initargs)))
1107 (apply #'make-instance class initargs)))
1109 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1110 (declare (ignore initargs))
1111 (find-class 'standard-effective-slot-definition))
1113 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1114 (declare (ignore initargs))
1115 (find-class 'structure-effective-slot-definition))
1117 (defmethod compute-effective-slot-definition-initargs
1118 ((class slot-class) direct-slotds)
1124 (allocation-class nil)
1130 (dolist (slotd direct-slotds)
1133 (setq name (slot-definition-name slotd)
1136 (when (slot-definition-initfunction slotd)
1137 (setq initform (slot-definition-initform slotd)
1138 initfunction (slot-definition-initfunction slotd)
1141 (setq allocation (slot-definition-allocation slotd)
1142 allocation-class (slot-definition-class slotd)
1144 (setq initargs (append (slot-definition-initargs slotd) initargs))
1145 (let ((slotd-type (slot-definition-type slotd)))
1146 (setq type (cond ((eq type t) slotd-type)
1147 ((*subtypep type slotd-type) type)
1148 (t `(and ,type ,slotd-type)))))))
1151 :initfunction initfunction
1153 :allocation allocation
1154 :allocation-class allocation-class
1158 (defmethod compute-effective-slot-definition-initargs :around
1159 ((class structure-class) direct-slotds)
1160 (let ((slotd (car direct-slotds)))
1161 (list* :defstruct-accessor-symbol
1162 (slot-definition-defstruct-accessor-symbol slotd)
1163 :internal-reader-function
1164 (slot-definition-internal-reader-function slotd)
1165 :internal-writer-function
1166 (slot-definition-internal-writer-function slotd)
1167 (call-next-method))))
1169 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1170 ;;; to make the method object. They have to use make-a-method which
1171 ;;; is a specially bootstrapped mechanism for making standard methods.
1172 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1173 (declare (ignore direct-slot initargs))
1174 (find-class 'standard-reader-method))
1176 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1177 (add-method generic-function
1178 (make-a-method 'standard-reader-method
1180 (list (or (class-name class) 'object))
1182 (make-reader-method-function class slot-name)
1183 "automatically generated reader method"
1186 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1187 (declare (ignore direct-slot initargs))
1188 (find-class 'standard-writer-method))
1190 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1191 (add-method generic-function
1192 (make-a-method 'standard-writer-method
1194 (list 'new-value (or (class-name class) 'object))
1195 (list *the-class-t* class)
1196 (make-writer-method-function class slot-name)
1197 "automatically generated writer method"
1200 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1201 (add-method generic-function
1202 (make-a-method 'standard-boundp-method
1204 (list (or (class-name class) 'object))
1206 (make-boundp-method-function class slot-name)
1207 "automatically generated boundp method"
1210 (defmethod remove-reader-method ((class slot-class) generic-function)
1211 (let ((method (get-method generic-function () (list class) nil)))
1212 (when method (remove-method generic-function method))))
1214 (defmethod remove-writer-method ((class slot-class) generic-function)
1216 (get-method generic-function () (list *the-class-t* class) nil)))
1217 (when method (remove-method generic-function method))))
1219 (defmethod remove-boundp-method ((class slot-class) generic-function)
1220 (let ((method (get-method generic-function () (list class) nil)))
1221 (when method (remove-method generic-function method))))
1223 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1224 ;;; part of the standard protocol. They are however useful, PCL makes
1225 ;;; use of them internally and documents them for PCL users.
1227 ;;; *** This needs work to make type testing by the writer functions which
1228 ;;; *** do type testing faster. The idea would be to have one constructor
1229 ;;; *** for each possible type test.
1231 ;;; *** There is a subtle bug here which is going to have to be fixed.
1232 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1233 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1234 ;;; *** defined for this metaclass a chance to run.
1236 (defmethod make-reader-method-function ((class slot-class) slot-name)
1237 (make-std-reader-method-function (class-name class) slot-name))
1239 (defmethod make-writer-method-function ((class slot-class) slot-name)
1240 (make-std-writer-method-function (class-name class) slot-name))
1242 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1243 (make-std-boundp-method-function (class-name class) slot-name))
1245 (defmethod compatible-meta-class-change-p (class proto-new-class)
1246 (eq (class-of class) (class-of proto-new-class)))
1248 (defmethod validate-superclass ((class class) (new-super class))
1249 (or (eq new-super *the-class-t*)
1250 (eq (class-of class) (class-of new-super))))
1252 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1253 (let ((new-super-meta-class (class-of new-super)))
1254 (or (eq new-super-meta-class *the-class-std-class*)
1255 (eq (class-of class) new-super-meta-class))))
1257 ;;; What this does depends on which of the four possible values of
1258 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1259 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1260 ;;; nothing to do, as the new wrapper has already been created. If
1261 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1262 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1263 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1265 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1266 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1267 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1268 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1269 ;;; obsolete the wrapper.
1271 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1272 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1273 ;;; :UNINITIALIZED)))
1275 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1276 (defun force-cache-flushes (class)
1277 (let* ((owrapper (class-wrapper class)))
1278 ;; We only need to do something if the wrapper is still valid. If
1279 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1280 ;; both of those will already be doing what we want. In
1281 ;; particular, we must be sure we never change an OBSOLETE into a
1282 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1283 (when (or (not (invalid-wrapper-p owrapper))
1284 ;; KLUDGE: despite the observations above, this remains
1285 ;; a violation of locality or what might be considered
1286 ;; good style. There has to be a better way! -- CSR,
1288 (eq (layout-invalid owrapper) t))
1289 (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1291 (setf (wrapper-instance-slots-layout nwrapper)
1292 (wrapper-instance-slots-layout owrapper))
1293 (setf (wrapper-class-slots nwrapper)
1294 (wrapper-class-slots owrapper))
1296 (update-lisp-class-layout class nwrapper)
1297 (setf (slot-value class 'wrapper) nwrapper)
1298 (invalidate-wrapper owrapper :flush nwrapper))))))
1300 (defun flush-cache-trap (owrapper nwrapper instance)
1301 (declare (ignore owrapper))
1302 (set-wrapper instance nwrapper))
1304 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1305 ;;; the next access to the instance (as defined in 88-002R) to trap
1306 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1307 (defmethod make-instances-obsolete ((class std-class))
1308 (let* ((owrapper (class-wrapper class))
1309 (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1311 (setf (wrapper-instance-slots-layout nwrapper)
1312 (wrapper-instance-slots-layout owrapper))
1313 (setf (wrapper-class-slots nwrapper)
1314 (wrapper-class-slots owrapper))
1316 (update-lisp-class-layout class nwrapper)
1317 (setf (slot-value class 'wrapper) nwrapper)
1318 (invalidate-wrapper owrapper :obsolete nwrapper)
1321 (defmethod make-instances-obsolete ((class symbol))
1322 (make-instances-obsolete (find-class class)))
1324 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1325 ;;; see an obsolete instance. The times when it is called are:
1326 ;;; - when the instance is involved in method lookup
1327 ;;; - when attempting to access a slot of an instance
1329 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1330 ;;; instance access macros.
1332 ;;; Of course these times when it is called are an internal
1333 ;;; implementation detail of PCL and are not part of the documented
1334 ;;; description of when the obsolete instance update happens. The
1335 ;;; documented description is as it appears in 88-002R.
1337 ;;; This has to return the new wrapper, so it counts on all the
1338 ;;; methods on obsolete-instance-trap-internal to return the new
1339 ;;; wrapper. It also does a little internal error checking to make
1340 ;;; sure that the traps are only happening when they should, and that
1341 ;;; the trap methods are computing appropriate new wrappers.
1343 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1344 ;;; after a structure is redefined. In most cases,
1345 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1346 ;;; so it must signal an error. The hard part of this is that the
1347 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1348 ;;; called again, so in that case, we have to return some reasonable
1349 ;;; wrapper, instead.
1351 (defvar *in-obsolete-instance-trap* nil)
1352 (defvar *the-wrapper-of-structure-object*
1353 (class-wrapper (find-class 'structure-object)))
1355 (define-condition obsolete-structure (error)
1356 ((datum :reader obsolete-structure-datum :initarg :datum))
1358 (lambda (condition stream)
1359 ;; Don't try to print the structure, since it probably won't work.
1361 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1362 (type-of (obsolete-structure-datum condition))))))
1364 (defun obsolete-instance-trap (owrapper nwrapper instance)
1365 (if (not (pcl-instance-p instance))
1366 (if *in-obsolete-instance-trap*
1367 *the-wrapper-of-structure-object*
1368 (let ((*in-obsolete-instance-trap* t))
1369 (error 'obsolete-structure :datum instance)))
1370 (let* ((class (wrapper-class* nwrapper))
1371 (copy (allocate-instance class)) ;??? allocate-instance ???
1372 (olayout (wrapper-instance-slots-layout owrapper))
1373 (nlayout (wrapper-instance-slots-layout nwrapper))
1374 (oslots (get-slots instance))
1375 (nslots (get-slots copy))
1376 (oclass-slots (wrapper-class-slots owrapper))
1380 ;; local --> local transfer
1381 ;; local --> shared discard
1382 ;; local --> -- discard
1383 ;; shared --> local transfer
1384 ;; shared --> shared discard
1385 ;; shared --> -- discard
1389 ;; Go through all the old local slots.
1391 (dolist (name olayout)
1392 (let ((npos (posq name nlayout)))
1394 (setf (clos-slots-ref nslots npos)
1395 (clos-slots-ref oslots opos))
1397 (push name discarded)
1398 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1399 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1402 ;; Go through all the old shared slots.
1403 (dolist (oclass-slot-and-val oclass-slots)
1404 (let ((name (car oclass-slot-and-val))
1405 (val (cdr oclass-slot-and-val)))
1406 (let ((npos (posq name nlayout)))
1408 (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1409 (progn (push name discarded)
1410 (unless (eq val +slot-unbound+)
1411 (setf (getf plist name) val)))))))
1413 ;; Go through all the new local slots to compute the added slots.
1414 (dolist (nlocal nlayout)
1415 (unless (or (memq nlocal olayout)
1416 (assq nlocal oclass-slots))
1417 (push nlocal added)))
1419 (swap-wrappers-and-slots instance copy)
1421 (update-instance-for-redefined-class instance
1427 (defun change-class-internal (instance new-class initargs)
1428 (let* ((old-class (class-of instance))
1429 (copy (allocate-instance new-class))
1430 (new-wrapper (get-wrapper copy))
1431 (old-wrapper (class-wrapper old-class))
1432 (old-layout (wrapper-instance-slots-layout old-wrapper))
1433 (new-layout (wrapper-instance-slots-layout new-wrapper))
1434 (old-slots (get-slots instance))
1435 (new-slots (get-slots copy))
1436 (old-class-slots (wrapper-class-slots old-wrapper)))
1438 ;; "The values of local slots specified by both the class CTO and
1439 ;; CFROM are retained. If such a local slot was unbound, it
1440 ;; remains unbound."
1441 (let ((new-position 0))
1442 (dolist (new-slot new-layout)
1443 (let ((old-position (posq new-slot old-layout)))
1445 (setf (clos-slots-ref new-slots new-position)
1446 (clos-slots-ref old-slots old-position))))
1447 (incf new-position)))
1449 ;; "The values of slots specified as shared in the class CFROM and
1450 ;; as local in the class CTO are retained."
1451 (dolist (slot-and-val old-class-slots)
1452 (let ((position (posq (car slot-and-val) new-layout)))
1454 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1456 ;; Make the copy point to the old instance's storage, and make the
1457 ;; old instance point to the new storage.
1458 (swap-wrappers-and-slots instance copy)
1460 (apply #'update-instance-for-different-class copy instance initargs)
1463 (defmethod change-class ((instance standard-object)
1464 (new-class standard-class)
1466 (change-class-internal instance new-class initargs))
1468 (defmethod change-class ((instance funcallable-standard-object)
1469 (new-class funcallable-standard-class)
1471 (change-class-internal instance new-class initargs))
1473 (defmethod change-class ((instance standard-object)
1474 (new-class funcallable-standard-class)
1476 (declare (ignore initargs))
1477 (error "You can't change the class of ~S to ~S~@
1478 because it isn't already an instance with metaclass ~S."
1479 instance new-class 'standard-class))
1481 (defmethod change-class ((instance funcallable-standard-object)
1482 (new-class standard-class)
1484 (declare (ignore initargs))
1485 (error "You can't change the class of ~S to ~S~@
1486 because it isn't already an instance with metaclass ~S."
1487 instance new-class 'funcallable-standard-class))
1489 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1490 (apply #'change-class instance (find-class new-class-name) initargs))
1492 ;;;; The metaclass BUILT-IN-CLASS
1494 ;;;; This metaclass is something of a weird creature. By this point, all
1495 ;;;; instances of it which will exist have been created, and no instance
1496 ;;;; is ever created by calling MAKE-INSTANCE.
1498 ;;;; But, there are other parts of the protocol we must follow and those
1499 ;;;; definitions appear here.
1501 (defmethod shared-initialize :before
1502 ((class built-in-class) slot-names &rest initargs)
1503 (declare (ignore slot-names initargs))
1504 (error "attempt to initialize or reinitialize a built in class"))
1506 (defmethod class-direct-slots ((class built-in-class)) ())
1507 (defmethod class-slots ((class built-in-class)) ())
1508 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1509 (defmethod class-default-initargs ((class built-in-class)) ())
1511 (defmethod validate-superclass ((c class) (s built-in-class))
1512 (or (eq s *the-class-t*)
1513 (eq s *the-class-stream*)))
1515 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1516 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1517 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1518 (macrolet ((def (method)
1519 `(defmethod ,method ((class forward-referenced-class))
1520 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1522 (def class-default-initargs)
1523 (def class-precedence-list)
1526 (defmethod validate-superclass ((c slot-class)
1527 (f forward-referenced-class))
1530 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1531 (pushnew dependent (plist-value metaobject 'dependents)))
1533 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1534 (setf (plist-value metaobject 'dependents)
1535 (delete dependent (plist-value metaobject 'dependents))))
1537 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1538 (dolist (dependent (plist-value metaobject 'dependents))
1539 (funcall function dependent)))