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