1.0.9.54: clean up old pv updating code
[sbcl.git] / src / pcl / std-class.lisp
blobf8e62f9cddc5902b865b8dd0a2cca66240021013
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
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
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
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
18 ;;;; control laws.
19 ;;;;
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
22 ;;;; specification.
24 (in-package "SB-PCL")
26 (defmethod slot-accessor-function ((slotd effective-slot-definition) type)
27 (ecase 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)
34 type)
35 (ecase type
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))
48 (if (eq type 'all)
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)
59 type)
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)
67 (if value
68 (the fixnum (logior mask flags))
69 (the fixnum (logand (the fixnum (lognot mask)) flags)))))
70 value)
72 (defmethod initialize-internal-slot-functions ((slotd
73 effective-slot-definition))
74 (let* ((name (slot-value slotd 'name))
75 (class (slot-value slotd '%class)))
76 (dolist (type '(reader writer boundp))
77 (let* ((gf-name (ecase type
78 (reader 'slot-value-using-class)
79 (writer '(setf slot-value-using-class))
80 (boundp 'slot-boundp-using-class)))
81 (gf (gdefinition gf-name)))
82 (compute-slot-accessor-info slotd type gf)))))
84 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
85 ;;;
86 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
87 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
88 ;;; writing/testing effective slot SLOTD.
89 ;;;
90 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
91 ;;; GF. Store the effective method in the effective slot definition
92 ;;; object itself; these GFs have special dispatch functions calling
93 ;;; effective methods directly retrieved from effective slot
94 ;;; definition objects, as an optimization.
95 ;;;
96 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
97 ;;; or some such.
98 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
99 type gf)
100 (let* ((name (slot-value slotd 'name))
101 (class (slot-value slotd '%class))
102 (old-slotd (find-slot-definition class name))
103 (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
104 (multiple-value-bind (function std-p)
105 (if (eq *boot-state* 'complete)
106 (get-accessor-method-function gf type class slotd)
107 (get-optimized-std-accessor-method-function class slotd type))
108 (setf (slot-accessor-std-p slotd type) std-p)
109 (setf (slot-accessor-function slotd type) function))))
111 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
112 :instance)
114 ;;;; various class accessors that are a little more complicated than can be
115 ;;;; done with automatically generated reader methods
117 (defmethod class-prototype :before (class)
118 (unless (class-finalized-p class)
119 (error "~@<~S is not finalized.~:@>" class)))
121 ;;; KLUDGE: For some reason factoring the common body into a function
122 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
123 ;;; all.
124 (macrolet ((def (class)
125 `(defmethod class-prototype ((class ,class))
126 (with-slots (prototype) class
127 (or prototype
128 (setf prototype (allocate-instance class)))))))
129 (def std-class)
130 (def condition-class)
131 (def structure-class))
133 (defmethod class-direct-default-initargs ((class slot-class))
134 (plist-value class 'direct-default-initargs))
136 (defmethod class-default-initargs ((class slot-class))
137 (plist-value class 'default-initargs))
139 (defmethod class-slot-cells ((class std-class))
140 (plist-value class 'class-slot-cells))
141 (defmethod (setf class-slot-cells) (new-value (class std-class))
142 (setf (plist-value class 'class-slot-cells) new-value))
144 ;;;; class accessors that are even a little bit more complicated than those
145 ;;;; above. These have a protocol for updating them, we must implement that
146 ;;;; protocol.
148 ;;; Maintaining the direct subclasses backpointers. The update methods are
149 ;;; here, the values are read by an automatically generated reader method.
150 (defmethod add-direct-subclass ((class class) (subclass class))
151 (with-slots (direct-subclasses) class
152 (pushnew subclass direct-subclasses)
153 subclass))
154 (defmethod remove-direct-subclass ((class class) (subclass class))
155 (with-slots (direct-subclasses) class
156 (setq direct-subclasses (remove subclass direct-subclasses))
157 subclass))
159 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
161 ;;; There are four generic functions involved, each has one method for the
162 ;;; class case and another method for the damned EQL specializers. All of
163 ;;; these are specified methods and appear in their specified place in the
164 ;;; class graph.
166 ;;; ADD-DIRECT-METHOD
167 ;;; REMOVE-DIRECT-METHOD
168 ;;; SPECIALIZER-DIRECT-METHODS
169 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
171 ;;; In each case, we maintain one value which is a cons. The car is the list
172 ;;; methods. The cdr is a list of the generic functions. The cdr is always
173 ;;; computed lazily.
175 ;;; This needs to be used recursively, in case a non-trivial user
176 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
177 ;;; function using the same lock.
178 (defvar *specializer-lock* (sb-thread::make-spinlock :name "Specializer lock"))
180 (defmethod add-direct-method :around ((specializer specializer) method)
181 ;; All the actions done under this lock are done in an order
182 ;; that is safe to unwind at any point.
183 (sb-thread::with-recursive-spinlock (*specializer-lock*)
184 (call-next-method)))
186 (defmethod remove-direct-method :around ((specializer specializer) method)
187 ;; All the actions done under this lock are done in an order
188 ;; that is safe to unwind at any point.
189 (sb-thread::with-recursive-spinlock (*specializer-lock*)
190 (call-next-method)))
192 (defmethod add-direct-method ((specializer class) (method method))
193 (let ((cell (slot-value specializer 'direct-methods)))
194 ;; We need to first smash the CDR, because a parallel read may
195 ;; be in progress, and because if an interrupt catches us we
196 ;; need to have a consistent state.
197 (setf (cdr cell) ()
198 (car cell) (adjoin method (car cell))))
199 method)
201 (defmethod remove-direct-method ((specializer class) (method method))
202 (let ((cell (slot-value specializer 'direct-methods)))
203 ;; We need to first smash the CDR, because a parallel read may
204 ;; be in progress, and because if an interrupt catches us we
205 ;; need to have a consistent state.
206 (setf (cdr cell) ()
207 (car cell) (remove method (car cell))))
208 method)
210 (defmethod specializer-direct-methods ((specializer class))
211 (with-slots (direct-methods) specializer
212 (car direct-methods)))
214 (defmethod specializer-direct-generic-functions ((specializer class))
215 (let ((cell (slot-value specializer 'direct-methods)))
216 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
217 ;; we behave as if we got just first or just after -- it's just
218 ;; for update that we need to lock.
219 (or (cdr cell)
220 (sb-thread::with-spinlock (*specializer-lock*)
221 (setf (cdr cell)
222 (let (collect)
223 (dolist (m (car cell))
224 ;; the old PCL code used COLLECTING-ONCE which used
225 ;; #'EQ to check for newness
226 (pushnew (method-generic-function m) collect :test #'eq))
227 (nreverse collect)))))))
229 ;;; This hash table is used to store the direct methods and direct generic
230 ;;; functions of EQL specializers. Each value in the table is the cons.
231 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
232 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
234 (defmethod specializer-method-table ((specializer eql-specializer))
235 *eql-specializer-methods*)
237 (defmethod specializer-method-table ((specializer class-eq-specializer))
238 *class-eq-specializer-methods*)
240 (defmethod add-direct-method ((specializer specializer-with-object)
241 (method method))
242 (let* ((object (specializer-object specializer))
243 (table (specializer-method-table specializer))
244 (entry (gethash object table)))
245 ;; This table is shared between multiple specializers, but
246 ;; no worries as (at least for the time being) our hash-tables
247 ;; are thread safe.
248 (unless entry
249 (setf entry
250 (setf (gethash object table) (cons nil nil))))
251 ;; We need to first smash the CDR, because a parallel read may
252 ;; be in progress, and because if an interrupt catches us we
253 ;; need to have a consistent state.
254 (setf (cdr entry) ()
255 (car entry) (adjoin method (car entry)))
256 method))
258 (defmethod remove-direct-method ((specializer specializer-with-object)
259 (method method))
260 (let* ((object (specializer-object specializer))
261 (entry (gethash object (specializer-method-table specializer))))
262 (when entry
263 ;; We need to first smash the CDR, because a parallel read may
264 ;; be in progress, and because if an interrupt catches us we
265 ;; need to have a consistent state.
266 (setf (cdr entry) ()
267 (car entry) (remove method (car entry))))
268 method))
270 (defmethod specializer-direct-methods ((specializer specializer-with-object))
271 (car (gethash (specializer-object specializer)
272 (specializer-method-table specializer))))
274 (defmethod specializer-direct-generic-functions ((specializer
275 specializer-with-object))
276 (let* ((object (specializer-object specializer))
277 (entry (gethash object (specializer-method-table specializer))))
278 (when entry
279 (or (cdr entry)
280 (sb-thread::with-spinlock (*specializer-lock*)
281 (setf (cdr entry)
282 (let (collect)
283 (dolist (m (car entry))
284 (pushnew (method-generic-function m) collect :test #'eq))
285 (nreverse collect))))))))
287 (defun map-specializers (function)
288 (map-all-classes (lambda (class)
289 (funcall function (class-eq-specializer class))
290 (funcall function class)))
291 (maphash (lambda (object methods)
292 (declare (ignore methods))
293 (intern-eql-specializer object))
294 *eql-specializer-methods*)
295 (maphash (lambda (object specl)
296 (declare (ignore object))
297 (funcall function specl))
298 *eql-specializer-table*)
299 nil)
301 (defun map-all-generic-functions (function)
302 (let ((all-generic-functions (make-hash-table :test 'eq)))
303 (map-specializers (lambda (specl)
304 (dolist (gf (specializer-direct-generic-functions
305 specl))
306 (unless (gethash gf all-generic-functions)
307 (setf (gethash gf all-generic-functions) t)
308 (funcall function gf))))))
309 nil)
311 (defmethod shared-initialize :after ((specl class-eq-specializer)
312 slot-names
313 &key)
314 (declare (ignore slot-names))
315 (setf (slot-value specl '%type) `(class-eq ,(specializer-class specl))))
317 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
318 (declare (ignore slot-names))
319 (setf (slot-value specl '%type)
320 `(eql ,(specializer-object specl)))
321 (setf (info :type :translator specl)
322 (constantly (make-member-type :members (list (specializer-object specl))))))
324 (defun real-load-defclass (name metaclass-name supers slots other
325 readers writers slot-names source-location safe-p)
326 (with-single-package-locked-error (:symbol name "defining ~S as a class")
327 (%compiler-defclass name readers writers slot-names)
328 (let ((res (apply #'ensure-class name :metaclass metaclass-name
329 :direct-superclasses supers
330 :direct-slots slots
331 :definition-source source-location
332 'safe-p safe-p
333 other)))
334 res)))
336 (setf (gdefinition 'load-defclass) #'real-load-defclass)
338 (defun ensure-class (name &rest args)
339 (apply #'ensure-class-using-class
340 (let ((class (find-class name nil)))
341 (when (and class (eq name (class-name class)))
342 ;; NAME is the proper name of CLASS, so redefine it
343 class))
344 name
345 args))
347 (defmethod ensure-class-using-class ((class null) name &rest args &key)
348 (multiple-value-bind (meta initargs)
349 (frob-ensure-class-args args)
350 (setf class (apply #'make-instance meta :name name initargs))
351 (without-package-locks
352 (setf (find-class name) class))
353 (set-class-type-translation class name)
354 class))
356 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
357 (multiple-value-bind (meta initargs)
358 (frob-ensure-class-args args)
359 (unless (eq (class-of class) meta)
360 (apply #'change-class class meta initargs))
361 (apply #'reinitialize-instance class initargs)
362 (without-package-locks
363 (setf (find-class name) class))
364 (set-class-type-translation class name)
365 class))
367 (defun frob-ensure-class-args (args)
368 (let (metaclass metaclassp reversed-plist)
369 (flet ((frob-superclass (s)
370 (cond
371 ((classp s) s)
372 ((legal-class-name-p s)
373 (or (find-class s nil)
374 (ensure-class s :metaclass 'forward-referenced-class)))
375 (t (error "Not a class or a legal class name: ~S." s)))))
376 (doplist (key val) args
377 (cond ((eq key :metaclass)
378 (unless metaclassp
379 (setf metaclass val metaclassp key)))
381 (when (eq key :direct-superclasses)
382 (setf val (mapcar #'frob-superclass val)))
383 (setf reversed-plist (list* val key reversed-plist)))))
384 (values (cond (metaclassp
385 (if (classp metaclass)
386 metaclass
387 (find-class metaclass)))
388 (t *the-class-standard-class*))
389 (nreverse reversed-plist)))))
391 (defmethod shared-initialize :after
392 ((class std-class) slot-names &key
393 (direct-superclasses nil direct-superclasses-p)
394 (direct-slots nil direct-slots-p)
395 (direct-default-initargs nil direct-default-initargs-p))
396 (cond (direct-superclasses-p
397 (setq direct-superclasses
398 (or direct-superclasses
399 (list (if (funcallable-standard-class-p class)
400 *the-class-funcallable-standard-object*
401 *the-class-standard-object*))))
402 (dolist (superclass direct-superclasses)
403 (unless (validate-superclass class superclass)
404 (error "~@<The class ~S was specified as a ~
405 super-class of the class ~S, ~
406 but the meta-classes ~S and ~S are incompatible. ~
407 Define a method for ~S to avoid this error.~@:>"
408 superclass class (class-of superclass) (class-of class)
409 'validate-superclass)))
410 (setf (slot-value class 'direct-superclasses) direct-superclasses))
412 (setq direct-superclasses (slot-value class 'direct-superclasses))))
413 (setq direct-slots
414 (if direct-slots-p
415 (setf (slot-value class 'direct-slots)
416 (mapcar (lambda (pl) (make-direct-slotd class pl))
417 direct-slots))
418 (slot-value class 'direct-slots)))
419 (if direct-default-initargs-p
420 (setf (plist-value class 'direct-default-initargs)
421 direct-default-initargs)
422 (setq direct-default-initargs
423 (plist-value class 'direct-default-initargs)))
424 (setf (plist-value class 'class-slot-cells)
425 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
426 (collect '()))
427 (dolist (dslotd direct-slots)
428 (when (eq :class (slot-definition-allocation dslotd))
429 ;; see CLHS 4.3.6
430 (let* ((name (slot-definition-name dslotd))
431 (old (assoc name old-class-slot-cells)))
432 (if (or (not old)
433 (eq t slot-names)
434 (member name slot-names))
435 (let* ((initfunction (slot-definition-initfunction dslotd))
436 (value (if initfunction
437 (funcall initfunction)
438 +slot-unbound+)))
439 (push (cons name value) collect))
440 (push old collect)))))
441 (nreverse collect)))
442 (add-direct-subclasses class direct-superclasses)
443 (if (class-finalized-p class)
444 ;; required by AMOP, "Reinitialization of Class Metaobjects"
445 (finalize-inheritance class)
446 (update-class class nil))
447 (add-slot-accessors class direct-slots)
448 (make-preliminary-layout class))
450 (defmethod shared-initialize :after ((class forward-referenced-class)
451 slot-names &key &allow-other-keys)
452 (declare (ignore slot-names))
453 (make-preliminary-layout class))
455 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
457 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
458 ;;; make it known to the type system.
459 (defun make-preliminary-layout (class)
460 (flet ((compute-preliminary-cpl (root)
461 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
462 (compute-class-precedence-list root))))
463 (without-package-locks
464 (unless (class-finalized-p class)
465 (let ((name (class-name class)))
466 ;; KLUDGE: This is fairly horrible. We need to make a
467 ;; full-fledged CLASSOID here, not just tell the compiler that
468 ;; some class is forthcoming, because there are legitimate
469 ;; questions one can ask of the type system, implemented in
470 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
471 (let ((layout (make-wrapper 0 class)))
472 (setf (slot-value class 'wrapper) layout)
473 (let ((cpl (compute-preliminary-cpl class)))
474 (setf (layout-inherits layout)
475 (order-layout-inherits
476 (map 'simple-vector #'class-wrapper
477 (reverse (rest cpl))))))
478 (register-layout layout :invalidate t)
479 (set-class-type-translation class (layout-classoid layout)))))
480 (mapc #'make-preliminary-layout (class-direct-subclasses class)))))
483 (defmethod shared-initialize :before ((class class) slot-names &key name)
484 (declare (ignore slot-names name))
485 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
486 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
487 (setf (slot-value class '%type) `(class ,class))
488 (setf (slot-value class 'class-eq-specializer)
489 (make-instance 'class-eq-specializer :class class)))
491 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
492 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
493 (remove-direct-subclass old-super class))
494 (remove-slot-accessors class (class-direct-slots class)))
496 (defmethod reinitialize-instance :after ((class slot-class)
497 &rest initargs
498 &key)
499 (map-dependents class
500 (lambda (dependent)
501 (apply #'update-dependent class dependent initargs))))
503 (defmethod reinitialize-instance :after ((class condition-class) &key)
504 (let* ((name (class-name class))
505 (classoid (find-classoid name))
506 (slots (condition-classoid-slots classoid)))
507 ;; to balance the REMOVE-SLOT-ACCESSORS call in
508 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
509 (dolist (slot slots)
510 (let ((slot-name (condition-slot-name slot)))
511 (dolist (reader (condition-slot-readers slot))
512 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
513 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
514 (sb-kernel::install-condition-slot-reader reader name slot-name))
515 (dolist (writer (condition-slot-writers slot))
516 (sb-kernel::install-condition-slot-writer writer name slot-name))))))
518 (defmethod shared-initialize :after ((class condition-class) slot-names
519 &key direct-slots direct-superclasses)
520 (declare (ignore slot-names))
521 (let ((classoid (find-classoid (class-name class))))
522 (with-slots (wrapper %class-precedence-list cpl-available-p
523 prototype (direct-supers direct-superclasses))
524 class
525 (setf (slot-value class 'direct-slots)
526 (mapcar (lambda (pl) (make-direct-slotd class pl))
527 direct-slots))
528 (setf (slot-value class 'finalized-p) t)
529 (setf (classoid-pcl-class classoid) class)
530 (setq direct-supers direct-superclasses)
531 (setq wrapper (classoid-layout classoid))
532 (setq %class-precedence-list (compute-class-precedence-list class))
533 (setq cpl-available-p t)
534 (add-direct-subclasses class direct-superclasses)
535 (let ((slots (compute-slots class)))
536 (setf (slot-value class 'slots) slots)
537 (setf (layout-slot-table wrapper) (make-slot-table class slots)))))
538 ;; Comment from Gerd's PCL, 2003-05-15:
540 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
541 ;; override condition accessors with generic functions. We do this
542 ;; differently.
544 ;; ??? What does the above comment mean and why is it a good idea?
545 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
546 ;; comment) loses slot information in its condition classes:
547 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
548 ;; remove slot accessors but never put them back. I've added a
549 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
550 ;; was meant to happen? -- CSR, 2005-11-18
553 (defmethod direct-slot-definition-class ((class condition-class)
554 &rest initargs)
555 (declare (ignore initargs))
556 (find-class 'condition-direct-slot-definition))
558 (defmethod effective-slot-definition-class ((class condition-class)
559 &rest initargs)
560 (declare (ignore initargs))
561 (find-class 'condition-effective-slot-definition))
563 (defmethod finalize-inheritance ((class condition-class))
564 (aver (slot-value class 'finalized-p))
565 nil)
567 (defmethod compute-effective-slot-definition
568 ((class condition-class) slot-name dslotds)
569 (let ((slotd (call-next-method)))
570 (setf (slot-definition-reader-function slotd)
571 (lambda (x)
572 (handler-case (condition-reader-function x slot-name)
573 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
574 ;; is unbound; maybe it should be a CELL-ERROR of some
575 ;; sort?
576 (error () (values (slot-unbound class x slot-name))))))
577 (setf (slot-definition-writer-function slotd)
578 (lambda (v x)
579 (condition-writer-function x v slot-name)))
580 (setf (slot-definition-boundp-function slotd)
581 (lambda (x)
582 (multiple-value-bind (v c)
583 (ignore-errors (condition-reader-function x slot-name))
584 (declare (ignore v))
585 (null c))))
586 slotd))
588 (defmethod compute-slots ((class condition-class))
589 (mapcan (lambda (superclass)
590 (mapcar (lambda (dslotd)
591 (compute-effective-slot-definition
592 class (slot-definition-name dslotd) (list dslotd)))
593 (class-direct-slots superclass)))
594 (reverse (slot-value class '%class-precedence-list))))
596 (defmethod compute-slots :around ((class condition-class))
597 (let ((eslotds (call-next-method)))
598 (mapc #'initialize-internal-slot-functions eslotds)
599 eslotds))
601 (defmethod shared-initialize :after
602 ((slotd structure-slot-definition) slot-names &key
603 (allocation :instance) allocation-class)
604 (declare (ignore slot-names allocation-class))
605 (unless (eq allocation :instance)
606 (error "Structure slots must have :INSTANCE allocation.")))
608 (defun make-structure-class-defstruct-form (name direct-slots include)
609 (let* ((conc-name (format-symbol *package* "~S structure class " name))
610 (constructor (format-symbol *package* "~Aconstructor" conc-name))
611 (defstruct `(defstruct (,name
612 ,@(when include
613 `((:include ,(class-name include))))
614 (:predicate nil)
615 (:conc-name ,conc-name)
616 (:constructor ,constructor ())
617 (:copier nil))
618 ,@(mapcar (lambda (slot)
619 `(,(slot-definition-name slot)
620 +slot-unbound+))
621 direct-slots)))
622 (reader-names (mapcar (lambda (slotd)
623 (list 'slot-accessor name
624 (slot-definition-name slotd)
625 'reader))
626 direct-slots))
627 (writer-names (mapcar (lambda (slotd)
628 (list 'slot-accessor name
629 (slot-definition-name slotd)
630 'writer))
631 direct-slots))
632 (readers-init
633 (mapcar (lambda (slotd reader-name)
634 (let ((accessor
635 (slot-definition-defstruct-accessor-symbol
636 slotd)))
637 `(defun ,reader-name (obj)
638 (declare (type ,name obj))
639 (,accessor obj))))
640 direct-slots reader-names))
641 (writers-init
642 (mapcar (lambda (slotd writer-name)
643 (let ((accessor
644 (slot-definition-defstruct-accessor-symbol
645 slotd)))
646 `(defun ,writer-name (nv obj)
647 (declare (type ,name obj))
648 (setf (,accessor obj) nv))))
649 direct-slots writer-names))
650 (defstruct-form
651 `(progn
652 ,defstruct
653 ,@readers-init ,@writers-init
654 (cons nil nil))))
655 (values defstruct-form constructor reader-names writer-names)))
657 (defun make-defstruct-allocation-function (class)
658 ;; FIXME: Why don't we go class->layout->info == dd
659 (let ((dd (find-defstruct-description (class-name class))))
660 (lambda ()
661 (sb-kernel::%make-instance-with-layout
662 (sb-kernel::compiler-layout-or-lose (dd-name dd))))))
664 (defmethod shared-initialize :after
665 ((class structure-class) slot-names &key
666 (direct-superclasses nil direct-superclasses-p)
667 (direct-slots nil direct-slots-p)
668 direct-default-initargs)
669 (declare (ignore slot-names direct-default-initargs))
670 (if direct-superclasses-p
671 (setf (slot-value class 'direct-superclasses)
672 (or direct-superclasses
673 (setq direct-superclasses
674 (and (not (eq (class-name class) 'structure-object))
675 (list *the-class-structure-object*)))))
676 (setq direct-superclasses (slot-value class 'direct-superclasses)))
677 (let* ((name (class-name class))
678 (from-defclass-p (slot-value class 'from-defclass-p))
679 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
680 (if direct-slots-p
681 (setf (slot-value class 'direct-slots)
682 (setq direct-slots
683 (mapcar (lambda (pl)
684 (when defstruct-p
685 (let* ((slot-name (getf pl :name))
686 (accessor
687 (format-symbol *package*
688 "~S structure class ~A"
689 name slot-name)))
690 (setq pl (list* :defstruct-accessor-symbol
691 accessor pl))))
692 (make-direct-slotd class pl))
693 direct-slots)))
694 (setq direct-slots (slot-value class 'direct-slots)))
695 (if defstruct-p
696 (let ((include (car (slot-value class 'direct-superclasses))))
697 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
698 (make-structure-class-defstruct-form name direct-slots include)
699 (unless (structure-type-p name) (eval defstruct-form))
700 (mapc (lambda (dslotd reader-name writer-name)
701 (let* ((reader (gdefinition reader-name))
702 (writer (when (fboundp writer-name)
703 (gdefinition writer-name))))
704 (setf (slot-value dslotd 'internal-reader-function)
705 reader)
706 (setf (slot-value dslotd 'internal-writer-function)
707 writer)))
708 direct-slots reader-names writer-names)
709 (setf (slot-value class 'defstruct-form) defstruct-form)
710 (setf (slot-value class 'defstruct-constructor) constructor)))
711 (setf (slot-value class 'defstruct-constructor)
712 (make-defstruct-allocation-function class)))
713 (add-direct-subclasses class direct-superclasses)
714 (setf (slot-value class '%class-precedence-list)
715 (compute-class-precedence-list class))
716 (setf (slot-value class 'cpl-available-p) t)
717 (let ((slots (compute-slots class)))
718 (setf (slot-value class 'slots) slots)
719 (let* ((lclass (find-classoid (class-name class)))
720 (layout (classoid-layout lclass)))
721 (setf (classoid-pcl-class lclass) class)
722 (setf (slot-value class 'wrapper) layout)
723 (setf (layout-slot-table layout) (make-slot-table class slots))))
724 (setf (slot-value class 'finalized-p) t)
725 (add-slot-accessors class direct-slots)))
727 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
728 (declare (ignore initargs))
729 (find-class 'structure-direct-slot-definition))
731 (defmethod finalize-inheritance ((class structure-class))
732 nil) ; always finalized
734 (defun add-slot-accessors (class dslotds)
735 (fix-slot-accessors class dslotds 'add))
737 (defun remove-slot-accessors (class dslotds)
738 (fix-slot-accessors class dslotds 'remove))
740 (defun fix-slot-accessors (class dslotds add/remove)
741 (flet ((fix (gfspec name r/w doc)
742 (let ((gf (cond ((eq add/remove 'add)
743 (or (find-generic-function gfspec nil)
744 (ensure-generic-function
745 gfspec :lambda-list (case r/w
746 (r '(object))
747 (w '(new-value object))))))
749 (find-generic-function gfspec nil)))))
750 (when gf
751 (case r/w
752 (r (if (eq add/remove 'add)
753 (add-reader-method class gf name doc)
754 (remove-reader-method class gf)))
755 (w (if (eq add/remove 'add)
756 (add-writer-method class gf name doc)
757 (remove-writer-method class gf))))))))
758 (dolist (dslotd dslotds)
759 (let ((slot-name (slot-definition-name dslotd))
760 (slot-doc (%slot-definition-documentation dslotd)))
761 (dolist (r (slot-definition-readers dslotd))
762 (fix r slot-name 'r slot-doc))
763 (dolist (w (slot-definition-writers dslotd))
764 (fix w slot-name 'w slot-doc))))))
766 (defun add-direct-subclasses (class supers)
767 (dolist (super supers)
768 (unless (memq class (class-direct-subclasses class))
769 (add-direct-subclass super class))))
771 (defmethod finalize-inheritance ((class std-class))
772 (update-class class t))
774 (defmethod finalize-inheritance ((class forward-referenced-class))
775 ;; FIXME: should we not be thinking a bit about what kinds of error
776 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
777 ;; possibly a forward-referenced-class-error, though that's
778 ;; difficult given e.g. class precedence list calculations...
779 (error
780 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
781 ~2I~_~S~:>"
782 class))
785 (defun class-has-a-forward-referenced-superclass-p (class)
786 (or (forward-referenced-class-p class)
787 (some #'class-has-a-forward-referenced-superclass-p
788 (class-direct-superclasses class))))
790 ;;; This is called by :after shared-initialize whenever a class is initialized
791 ;;; or reinitialized. The class may or may not be finalized.
792 (defun update-class (class finalizep)
793 (without-package-locks
794 (when (or finalizep (class-finalized-p class))
795 (update-cpl class (compute-class-precedence-list class))
796 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
797 ;; class.
798 (update-slots class (compute-slots class))
799 (update-gfs-of-class class)
800 (update-initargs class (compute-default-initargs class))
801 (update-ctors 'finalize-inheritance :class class))
802 (dolist (sub (class-direct-subclasses class))
803 (update-class sub nil))))
805 (define-condition cpl-protocol-violation (reference-condition error)
806 ((class :initarg :class :reader cpl-protocol-violation-class)
807 (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
808 (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
809 (:report
810 (lambda (c s)
811 (format s "~@<Protocol violation: the ~S class ~S ~
812 ~:[has~;does not have~] the class ~S in its ~
813 class precedence list: ~S.~@:>"
814 (class-name (class-of (cpl-protocol-violation-class c)))
815 (cpl-protocol-violation-class c)
816 (eq (class-of (cpl-protocol-violation-class c))
817 *the-class-funcallable-standard-class*)
818 (find-class 'function)
819 (cpl-protocol-violation-cpl c)))))
821 (defun update-cpl (class cpl)
822 (when (eq (class-of class) *the-class-standard-class*)
823 (when (find (find-class 'function) cpl)
824 (error 'cpl-protocol-violation :class class :cpl cpl)))
825 (when (eq (class-of class) *the-class-funcallable-standard-class*)
826 (unless (find (find-class 'function) cpl)
827 (error 'cpl-protocol-violation :class class :cpl cpl)))
828 (if (class-finalized-p class)
829 (unless (and (equal (class-precedence-list class) cpl)
830 (dolist (c cpl t)
831 (when (position :class (class-direct-slots c)
832 :key #'slot-definition-allocation)
833 (return nil))))
834 ;; comment from the old CMU CL sources:
835 ;; Need to have the cpl setup before update-lisp-class-layout
836 ;; is called on CMU CL.
837 (setf (slot-value class '%class-precedence-list) cpl)
838 (setf (slot-value class 'cpl-available-p) t)
839 (force-cache-flushes class))
840 (progn
841 (setf (slot-value class '%class-precedence-list) cpl)
842 (setf (slot-value class 'cpl-available-p) t)))
843 (update-class-can-precede-p cpl))
845 (defun update-class-can-precede-p (cpl)
846 (when cpl
847 (let ((first (car cpl)))
848 (dolist (c (cdr cpl))
849 (pushnew c (slot-value first 'can-precede-list))))
850 (update-class-can-precede-p (cdr cpl))))
852 (defun class-can-precede-p (class1 class2)
853 (member class2 (class-can-precede-list class1)))
855 (defun update-slots (class eslotds)
856 (let ((instance-slots ())
857 (class-slots ()))
858 (dolist (eslotd eslotds)
859 (let ((alloc (slot-definition-allocation eslotd)))
860 (case alloc
861 (:instance (push eslotd instance-slots))
862 (:class (push eslotd class-slots)))))
864 ;; If there is a change in the shape of the instances then the
865 ;; old class is now obsolete.
866 (let* ((nlayout (mapcar #'slot-definition-name
867 (sort instance-slots #'<
868 :key #'slot-definition-location)))
869 (nslots (length nlayout))
870 (nwrapper-class-slots (compute-class-slots class-slots))
871 (owrapper (when (class-finalized-p class)
872 (class-wrapper class)))
873 (olayout (when owrapper
874 (wrapper-instance-slots-layout owrapper)))
875 (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
876 (nwrapper
877 (cond ((null owrapper)
878 (make-wrapper nslots class))
879 ((and (equal nlayout olayout)
880 (not
881 (loop for o in owrapper-class-slots
882 for n in nwrapper-class-slots
883 do (unless (eq (car o) (car n)) (return t)))))
884 owrapper)
886 ;; This will initialize the new wrapper to have the
887 ;; same state as the old wrapper. We will then have
888 ;; to change that. This may seem like wasted work
889 ;; (and it is), but the spec requires that we call
890 ;; MAKE-INSTANCES-OBSOLETE.
891 (make-instances-obsolete class)
892 (class-wrapper class)))))
894 (update-lisp-class-layout class nwrapper)
895 (setf (slot-value class 'slots) eslotds
896 (wrapper-slot-table nwrapper) (make-slot-table class eslotds)
897 (wrapper-instance-slots-layout nwrapper) nlayout
898 (wrapper-class-slots nwrapper) nwrapper-class-slots
899 (wrapper-length nwrapper) nslots
900 (slot-value class 'wrapper) nwrapper)
901 (do* ((slots (slot-value class 'slots) (cdr slots))
902 (dupes nil))
903 ((null slots)
904 (when dupes
905 (style-warn
906 "~@<slot names with the same SYMBOL-NAME but ~
907 different SYMBOL-PACKAGE (possible package problem) ~
908 for class ~S:~4I~@:_~<~@{~S~^~:@_~}~:>~@:>"
909 class dupes)))
910 (let* ((slot (car slots))
911 (oslots (remove (slot-definition-name slot) (cdr slots)
912 :test #'string/=
913 :key #'slot-definition-name)))
914 (when oslots
915 (pushnew (cons (slot-definition-name slot)
916 (mapcar #'slot-definition-name oslots))
917 dupes
918 :test #'string= :key #'car))))
919 (setf (slot-value class 'finalized-p) t)
920 (unless (eq owrapper nwrapper)
921 (maybe-update-standard-class-locations class)))))
923 (defun compute-class-slots (eslotds)
924 (let (collect)
925 (dolist (eslotd eslotds (nreverse collect))
926 (let ((cell (assoc (slot-definition-name eslotd)
927 (class-slot-cells
928 (slot-definition-allocation-class eslotd)))))
929 (aver cell)
930 (push cell collect)))))
932 (defun update-gfs-of-class (class)
933 (when (and (class-finalized-p class)
934 (let ((cpl (class-precedence-list class)))
935 (or (member *the-class-slot-class* cpl)
936 (member *the-class-standard-effective-slot-definition*
937 cpl))))
938 (let ((gf-table (make-hash-table :test 'eq)))
939 (labels ((collect-gfs (class)
940 (dolist (gf (specializer-direct-generic-functions class))
941 (setf (gethash gf gf-table) t))
942 (mapc #'collect-gfs (class-direct-superclasses class))))
943 (collect-gfs class)
944 (maphash (lambda (gf ignore)
945 (declare (ignore ignore))
946 (update-gf-dfun class gf))
947 gf-table)))))
949 (defun update-initargs (class inits)
950 (setf (plist-value class 'default-initargs) inits))
952 (defmethod compute-default-initargs ((class slot-class))
953 (let ((initargs (loop for c in (class-precedence-list class)
954 append (class-direct-default-initargs c))))
955 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
957 ;;;; protocols for constructing direct and effective slot definitions
959 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
960 (declare (ignore initargs))
961 (find-class 'standard-direct-slot-definition))
963 (defun make-direct-slotd (class initargs)
964 (apply #'make-instance
965 (apply #'direct-slot-definition-class class initargs)
966 :class class
967 initargs))
969 ;;; I (CSR) am not sure, but I believe that the particular order of
970 ;;; slots is quite important: it is ideal to attempt to have a
971 ;;; constant slot location for the same notional slots as much as
972 ;;; possible, so that clever discriminating functions (ONE-INDEX et
973 ;;; al.) have a chance of working. The below at least walks through
974 ;;; the slots predictably, but maybe it would be good to compute some
975 ;;; kind of optimal slot layout by looking at locations of slots in
976 ;;; superclasses?
977 (defun std-compute-slots (class)
978 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
979 ;; for each different slot name we find in our superclasses. Each
980 ;; call receives the class and a list of the dslotds with that name.
981 ;; The list is in most-specific-first order.
982 (let ((name-dslotds-alist ()))
983 (dolist (c (reverse (class-precedence-list class)))
984 (dolist (slot (class-direct-slots c))
985 (let* ((name (slot-definition-name slot))
986 (entry (assq name name-dslotds-alist)))
987 (if entry
988 (push slot (cdr entry))
989 (push (list name slot) name-dslotds-alist)))))
990 (mapcar (lambda (direct)
991 (compute-effective-slot-definition class
992 (car direct)
993 (cdr direct)))
994 (nreverse name-dslotds-alist))))
996 (defmethod compute-slots ((class standard-class))
997 (std-compute-slots class))
998 (defmethod compute-slots ((class funcallable-standard-class))
999 (std-compute-slots class))
1001 (defun std-compute-slots-around (class eslotds)
1002 (let ((location -1))
1003 (dolist (eslotd eslotds eslotds)
1004 (setf (slot-definition-location eslotd)
1005 (case (slot-definition-allocation eslotd)
1006 (:instance
1007 (incf location))
1008 (:class
1009 (let* ((name (slot-definition-name eslotd))
1010 (from-class
1012 (slot-definition-allocation-class eslotd)
1013 ;; we get here if the user adds an extra slot
1014 ;; himself...
1015 (setf (slot-definition-allocation-class eslotd)
1016 class)))
1017 ;; which raises the question of what we should
1018 ;; do if we find that said user has added a slot
1019 ;; with the same name as another slot...
1020 (cell (or (assq name (class-slot-cells from-class))
1021 (let ((c (cons name +slot-unbound+)))
1022 (push c (class-slot-cells from-class))
1023 c))))
1024 (aver (consp cell))
1025 (if (eq +slot-unbound+ (cdr cell))
1026 ;; We may have inherited an initfunction
1027 (let ((initfun (slot-definition-initfunction eslotd)))
1028 (if initfun
1029 (rplacd cell (funcall initfun))
1030 cell))
1031 cell)))))
1032 (unless (slot-definition-class eslotd)
1033 (setf (slot-definition-class eslotd) class))
1034 (initialize-internal-slot-functions eslotd))))
1036 (defmethod compute-slots :around ((class standard-class))
1037 (let ((eslotds (call-next-method)))
1038 (std-compute-slots-around class eslotds)))
1039 (defmethod compute-slots :around ((class funcallable-standard-class))
1040 (let ((eslotds (call-next-method)))
1041 (std-compute-slots-around class eslotds)))
1043 (defmethod compute-slots ((class structure-class))
1044 (mapcan (lambda (superclass)
1045 (mapcar (lambda (dslotd)
1046 (compute-effective-slot-definition
1047 class
1048 (slot-definition-name dslotd)
1049 (list dslotd)))
1050 (class-direct-slots superclass)))
1051 (reverse (slot-value class '%class-precedence-list))))
1053 (defmethod compute-slots :around ((class structure-class))
1054 (let ((eslotds (call-next-method)))
1055 (mapc #'initialize-internal-slot-functions eslotds)
1056 eslotds))
1058 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1059 (declare (ignore name))
1060 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1061 (class (apply #'effective-slot-definition-class class initargs)))
1062 (apply #'make-instance class initargs)))
1064 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1065 (declare (ignore initargs))
1066 (find-class 'standard-effective-slot-definition))
1068 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1069 (declare (ignore initargs))
1070 (find-class 'structure-effective-slot-definition))
1072 (defmethod compute-effective-slot-definition-initargs
1073 ((class slot-class) direct-slotds)
1074 (let* ((name nil)
1075 (initfunction nil)
1076 (initform nil)
1077 (initargs nil)
1078 (allocation nil)
1079 (allocation-class nil)
1080 (type t)
1081 (type-check-function nil)
1082 (documentation nil)
1083 (documentationp nil)
1084 (namep nil)
1085 (initp nil)
1086 (allocp nil))
1088 (dolist (slotd direct-slotds)
1089 (when slotd
1090 (unless namep
1091 (setq name (slot-definition-name slotd)
1092 namep t))
1093 (unless initp
1094 (when (slot-definition-initfunction slotd)
1095 (setq initform (slot-definition-initform slotd)
1096 initfunction (slot-definition-initfunction slotd)
1097 initp t)))
1098 (unless documentationp
1099 (when (%slot-definition-documentation slotd)
1100 (setq documentation (%slot-definition-documentation slotd)
1101 documentationp t)))
1102 (unless allocp
1103 (setq allocation (slot-definition-allocation slotd)
1104 allocation-class (slot-definition-class slotd)
1105 allocp t))
1106 (setq initargs (append (slot-definition-initargs slotd) initargs))
1107 (let ((fun (slot-definition-type-check-function slotd)))
1108 (when fun
1109 (setf type-check-function
1110 (if type-check-function
1111 (let ((old-function type-check-function))
1112 (lambda (value)
1113 (funcall old-function value)
1114 (funcall fun value)))
1115 fun))))
1116 (let ((slotd-type (slot-definition-type slotd)))
1117 (setq type (cond
1118 ((eq type t) slotd-type)
1119 ;; This pairwise type intersection is perhaps a
1120 ;; little inefficient and inelegant, but it's
1121 ;; unlikely to lie on the critical path. Shout
1122 ;; if I'm wrong. -- CSR, 2005-11-24
1123 (t (type-specifier
1124 (specifier-type `(and ,type ,slotd-type)))))))))
1125 (list :name name
1126 :initform initform
1127 :initfunction initfunction
1128 :initargs initargs
1129 :allocation allocation
1130 :allocation-class allocation-class
1131 :type type
1132 'type-check-function type-check-function
1133 :class class
1134 :documentation documentation)))
1136 (defmethod compute-effective-slot-definition-initargs :around
1137 ((class structure-class) direct-slotds)
1138 (let ((slotd (car direct-slotds)))
1139 (list* :defstruct-accessor-symbol
1140 (slot-definition-defstruct-accessor-symbol slotd)
1141 :internal-reader-function
1142 (slot-definition-internal-reader-function slotd)
1143 :internal-writer-function
1144 (slot-definition-internal-writer-function slotd)
1145 (call-next-method))))
1147 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1148 ;;; to make the method object. They have to use make-a-method which
1149 ;;; is a specially bootstrapped mechanism for making standard methods.
1150 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1151 (declare (ignore direct-slot initargs))
1152 (find-class 'standard-reader-method))
1154 (defmethod add-reader-method ((class slot-class) generic-function slot-name slot-documentation)
1155 (add-method generic-function
1156 (make-a-method 'standard-reader-method
1158 (list (or (class-name class) 'object))
1159 (list class)
1160 (make-reader-method-function class slot-name)
1161 (or slot-documentation "automatically generated reader method")
1162 :slot-name slot-name
1163 :object-class class
1164 :method-class-function #'reader-method-class)))
1166 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1167 (declare (ignore direct-slot initargs))
1168 (find-class 'standard-writer-method))
1170 (defmethod add-writer-method ((class slot-class) generic-function slot-name slot-documentation)
1171 (add-method generic-function
1172 (make-a-method 'standard-writer-method
1174 (list 'new-value (or (class-name class) 'object))
1175 (list *the-class-t* class)
1176 (make-writer-method-function class slot-name)
1177 (or slot-documentation "automatically generated writer method")
1178 :slot-name slot-name
1179 :object-class class
1180 :method-class-function #'writer-method-class)))
1182 (defmethod add-boundp-method ((class slot-class) generic-function slot-name slot-documentation)
1183 (add-method generic-function
1184 (make-a-method (constantly (find-class 'standard-boundp-method))
1185 class
1187 (list (or (class-name class) 'object))
1188 (list class)
1189 (make-boundp-method-function class slot-name)
1190 (or slot-documentation "automatically generated boundp method")
1191 slot-name)))
1193 (defmethod remove-reader-method ((class slot-class) generic-function)
1194 (let ((method (get-method generic-function () (list class) nil)))
1195 (when method (remove-method generic-function method))))
1197 (defmethod remove-writer-method ((class slot-class) generic-function)
1198 (let ((method
1199 (get-method generic-function () (list *the-class-t* class) nil)))
1200 (when method (remove-method generic-function method))))
1202 (defmethod remove-boundp-method ((class slot-class) generic-function)
1203 (let ((method (get-method generic-function () (list class) nil)))
1204 (when method (remove-method generic-function method))))
1206 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1207 ;;; function are NOT part of the standard protocol. They are however
1208 ;;; useful; PCL makes use of them internally and documents them for
1209 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1211 ;;; *** This needs work to make type testing by the writer functions which
1212 ;;; *** do type testing faster. The idea would be to have one constructor
1213 ;;; *** for each possible type test.
1215 ;;; *** There is a subtle bug here which is going to have to be fixed.
1216 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1217 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1218 ;;; *** defined for this metaclass a chance to run.
1220 (defmethod make-reader-method-function ((class slot-class) slot-name)
1221 (make-std-reader-method-function class slot-name))
1223 (defmethod make-writer-method-function ((class slot-class) slot-name)
1224 (make-std-writer-method-function class slot-name))
1226 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1227 (make-std-boundp-method-function class slot-name))
1229 (defmethod compatible-meta-class-change-p (class proto-new-class)
1230 (eq (class-of class) (class-of proto-new-class)))
1232 (defmethod validate-superclass ((class class) (superclass class))
1233 (or (eq superclass *the-class-t*)
1234 (eq (class-of class) (class-of superclass))
1235 (and (eq (class-of superclass) *the-class-standard-class*)
1236 (eq (class-of class) *the-class-funcallable-standard-class*))
1237 (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1238 (eq (class-of class) *the-class-standard-class*))))
1240 ;;; What this does depends on which of the four possible values of
1241 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1242 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1243 ;;; nothing to do, as the new wrapper has already been created. If
1244 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1245 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1246 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1248 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1249 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1250 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1251 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1252 ;;; obsolete the wrapper.
1254 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1255 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1256 ;;; :UNINITIALIZED)))
1258 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1259 (defun force-cache-flushes (class)
1260 (let* ((owrapper (class-wrapper class)))
1261 ;; We only need to do something if the wrapper is still valid. If
1262 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1263 ;; both of those will already be doing what we want. In
1264 ;; particular, we must be sure we never change an OBSOLETE into a
1265 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1266 (when (or (not (invalid-wrapper-p owrapper))
1267 ;; KLUDGE: despite the observations above, this remains
1268 ;; a violation of locality or what might be considered
1269 ;; good style. There has to be a better way! -- CSR,
1270 ;; 2002-10-29
1271 (eq (layout-invalid owrapper) t))
1272 (let ((nwrapper (make-wrapper (layout-length owrapper)
1273 class)))
1274 (setf (wrapper-instance-slots-layout nwrapper)
1275 (wrapper-instance-slots-layout owrapper))
1276 (setf (wrapper-class-slots nwrapper)
1277 (wrapper-class-slots owrapper))
1278 (setf (wrapper-slot-table nwrapper)
1279 (wrapper-slot-table owrapper))
1280 (with-pcl-lock
1281 (update-lisp-class-layout class nwrapper)
1282 (setf (slot-value class 'wrapper) nwrapper)
1283 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1284 ;; been obsoleted.
1285 (if (find-if (lambda (x)
1286 (and (consp x) (eq :obsolete (car x))))
1287 (layout-inherits owrapper)
1288 :key #'layout-invalid)
1289 (invalidate-wrapper owrapper :obsolete nwrapper)
1290 (invalidate-wrapper owrapper :flush nwrapper)))))))
1292 (defun flush-cache-trap (owrapper nwrapper instance)
1293 (declare (ignore owrapper))
1294 (set-wrapper instance nwrapper))
1296 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1297 ;;; the next access to the instance (as defined in 88-002R) to trap
1298 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1299 (defmethod make-instances-obsolete ((class std-class))
1300 (let* ((owrapper (class-wrapper class))
1301 (nwrapper (make-wrapper (layout-length owrapper)
1302 class)))
1303 (unless (class-finalized-p class)
1304 (if (class-has-a-forward-referenced-superclass-p class)
1305 (return-from make-instances-obsolete class)
1306 (update-cpl class (compute-class-precedence-list class))))
1307 (setf (wrapper-instance-slots-layout nwrapper)
1308 (wrapper-instance-slots-layout owrapper))
1309 (setf (wrapper-class-slots nwrapper)
1310 (wrapper-class-slots owrapper))
1311 (setf (wrapper-slot-table nwrapper)
1312 (wrapper-slot-table owrapper))
1313 (with-pcl-lock
1314 (update-lisp-class-layout class nwrapper)
1315 (setf (slot-value class 'wrapper) nwrapper)
1316 (invalidate-wrapper owrapper :obsolete nwrapper)
1317 class)))
1319 (defmethod make-instances-obsolete ((class symbol))
1320 (make-instances-obsolete (find-class class))
1321 ;; ANSI wants the class name when called with a symbol.
1322 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))
1357 (:report
1358 (lambda (condition stream)
1359 ;; Don't try to print the structure, since it probably won't work.
1360 (format stream
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 (layout-for-std-class-p owrapper))
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))
1377 (added ())
1378 (discarded ())
1379 (plist ()))
1381 ;; local --> local transfer value
1382 ;; local --> shared discard value, discard slot
1383 ;; local --> -- discard slot
1384 ;; shared --> local transfer value
1385 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1386 ;; shared --> -- discard value
1387 ;; -- --> local add slot
1388 ;; -- --> shared --
1390 ;; Go through all the old local slots.
1391 (let ((opos 0))
1392 (dolist (name olayout)
1393 (let ((npos (posq name nlayout)))
1394 (if npos
1395 (setf (clos-slots-ref nslots npos)
1396 (clos-slots-ref oslots opos))
1397 (progn
1398 (push name discarded)
1399 (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1400 (setf (getf plist name) (clos-slots-ref oslots opos))))))
1401 (incf opos)))
1403 ;; Go through all the old shared slots.
1404 (dolist (oclass-slot-and-val oclass-slots)
1405 (let ((name (car oclass-slot-and-val))
1406 (val (cdr oclass-slot-and-val)))
1407 (let ((npos (posq name nlayout)))
1408 (when npos
1409 (setf (clos-slots-ref nslots npos) val)))))
1411 ;; Go through all the new local slots to compute the added slots.
1412 (dolist (nlocal nlayout)
1413 (unless (or (memq nlocal olayout)
1414 (assq nlocal oclass-slots))
1415 (push nlocal added)))
1417 (swap-wrappers-and-slots instance copy)
1419 (update-instance-for-redefined-class instance
1420 added
1421 discarded
1422 plist)
1423 nwrapper)))
1425 (defun change-class-internal (instance new-class initargs)
1426 (let* ((old-class (class-of instance))
1427 (copy (allocate-instance new-class))
1428 (new-wrapper (get-wrapper copy))
1429 (old-wrapper (class-wrapper old-class))
1430 (old-layout (wrapper-instance-slots-layout old-wrapper))
1431 (new-layout (wrapper-instance-slots-layout new-wrapper))
1432 (old-slots (get-slots instance))
1433 (new-slots (get-slots copy))
1434 (old-class-slots (wrapper-class-slots old-wrapper)))
1436 ;; "The values of local slots specified by both the class CTO and
1437 ;; CFROM are retained. If such a local slot was unbound, it
1438 ;; remains unbound."
1439 (let ((new-position 0))
1440 (dolist (new-slot new-layout)
1441 (let ((old-position (posq new-slot old-layout)))
1442 (when old-position
1443 (setf (clos-slots-ref new-slots new-position)
1444 (clos-slots-ref old-slots old-position))))
1445 (incf new-position)))
1447 ;; "The values of slots specified as shared in the class CFROM and
1448 ;; as local in the class CTO are retained."
1449 (dolist (slot-and-val old-class-slots)
1450 (let ((position (posq (car slot-and-val) new-layout)))
1451 (when position
1452 (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1454 ;; Make the copy point to the old instance's storage, and make the
1455 ;; old instance point to the new storage.
1456 (swap-wrappers-and-slots instance copy)
1458 (apply #'update-instance-for-different-class copy instance initargs)
1459 instance))
1461 (defmethod change-class ((instance standard-object) (new-class standard-class)
1462 &rest initargs)
1463 (unless (class-finalized-p new-class)
1464 (finalize-inheritance new-class))
1465 (let ((cpl (class-precedence-list new-class)))
1466 (dolist (class cpl)
1467 (macrolet
1468 ((frob (class-name)
1469 `(when (eq class (find-class ',class-name))
1470 (error 'metaobject-initialization-violation
1471 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1472 :format-arguments (list 'change-class ',class-name)
1473 :references (list '(:amop :initialization ,class-name))))))
1474 (frob class)
1475 (frob generic-function)
1476 (frob method)
1477 (frob slot-definition))))
1478 (change-class-internal instance new-class initargs))
1480 (defmethod change-class ((instance forward-referenced-class)
1481 (new-class standard-class) &rest initargs)
1482 (let ((cpl (class-precedence-list new-class)))
1483 (dolist (class cpl
1484 (error 'metaobject-initialization-violation
1485 :format-control
1486 "~@<Cannot ~S ~S objects into non-~S objects.~@:>"
1487 :format-arguments
1488 (list 'change-class 'forward-referenced-class 'class)
1489 :references
1490 (list '(:amop :generic-function ensure-class-using-class)
1491 '(:amop :initialization class))))
1492 (when (eq class (find-class 'class))
1493 (return nil))))
1494 (change-class-internal instance new-class initargs))
1496 (defmethod change-class ((instance funcallable-standard-object)
1497 (new-class funcallable-standard-class)
1498 &rest initargs)
1499 (let ((cpl (class-precedence-list new-class)))
1500 (dolist (class cpl)
1501 (macrolet
1502 ((frob (class-name)
1503 `(when (eq class (find-class ',class-name))
1504 (error 'metaobject-initialization-violation
1505 :format-control "~@<Cannot ~S objects into ~S metaobjects.~@:>"
1506 :format-arguments (list 'change-class ',class-name)
1507 :references (list '(:amop :initialization ,class-name))))))
1508 (frob class)
1509 (frob generic-function)
1510 (frob method)
1511 (frob slot-definition))))
1512 (change-class-internal instance new-class initargs))
1514 (defmethod change-class ((instance standard-object)
1515 (new-class funcallable-standard-class)
1516 &rest initargs)
1517 (declare (ignore initargs))
1518 (error "You can't change the class of ~S to ~S~@
1519 because it isn't already an instance with metaclass ~S."
1520 instance new-class 'standard-class))
1522 (defmethod change-class ((instance funcallable-standard-object)
1523 (new-class standard-class)
1524 &rest initargs)
1525 (declare (ignore initargs))
1526 (error "You can't change the class of ~S to ~S~@
1527 because it isn't already an instance with metaclass ~S."
1528 instance new-class 'funcallable-standard-class))
1530 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1531 (apply #'change-class instance (find-class new-class-name) initargs))
1533 ;;;; The metaclass BUILT-IN-CLASS
1534 ;;;;
1535 ;;;; This metaclass is something of a weird creature. By this point, all
1536 ;;;; instances of it which will exist have been created, and no instance
1537 ;;;; is ever created by calling MAKE-INSTANCE.
1538 ;;;;
1539 ;;;; But, there are other parts of the protocol we must follow and those
1540 ;;;; definitions appear here.
1542 (macrolet ((def (name args control)
1543 `(defmethod ,name ,args
1544 (declare (ignore initargs))
1545 (error 'metaobject-initialization-violation
1546 :format-control ,(format nil "~@<~A~@:>" control)
1547 :format-arguments (list ',name)
1548 :references (list '(:amop :initialization "Class"))))))
1549 (def initialize-instance ((class built-in-class) &rest initargs)
1550 "Cannot ~S an instance of BUILT-IN-CLASS.")
1551 (def reinitialize-instance ((class built-in-class) &rest initargs)
1552 "Cannot ~S an instance of BUILT-IN-CLASS."))
1554 (macrolet ((def (name)
1555 `(defmethod ,name ((class built-in-class)) nil)))
1556 (def class-direct-slots)
1557 (def class-slots)
1558 (def class-direct-default-initargs)
1559 (def class-default-initargs))
1561 (defmethod validate-superclass ((c class) (s built-in-class))
1562 (or (eq s *the-class-t*) (eq s *the-class-stream*)
1563 ;; FIXME: bad things happen if someone tries to mix in both
1564 ;; FILE-STREAM and STRING-STREAM (as they have the same
1565 ;; layout-depthoid). Is there any way we can provide a useful
1566 ;; error message? -- CSR, 2005-05-03
1567 (eq s *the-class-file-stream*) (eq s *the-class-string-stream*)
1568 ;; This probably shouldn't be mixed in with certain other
1569 ;; classes, too, but it seems to work both with STANDARD-OBJECT
1570 ;; and FUNCALLABLE-STANDARD-OBJECT
1571 (eq s *the-class-sequence*)))
1573 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1574 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1575 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1576 (macrolet ((def (method)
1577 `(defmethod ,method ((class forward-referenced-class))
1578 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1579 ',method class))))
1580 (def class-default-initargs)
1581 (def class-precedence-list)
1582 (def class-slots))
1584 (defmethod validate-superclass ((c slot-class)
1585 (f forward-referenced-class))
1588 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1589 (pushnew dependent (plist-value metaobject 'dependents)))
1591 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1592 (setf (plist-value metaobject 'dependents)
1593 (delete dependent (plist-value metaobject 'dependents))))
1595 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1596 (dolist (dependent (plist-value metaobject 'dependents))
1597 (funcall function dependent)))