Replace DEF!METHOD and SB!XC:DEFMETHOD with just DEFMETHOD.
[sbcl.git] / src / pcl / std-class.lisp
blob8c03437c8a19809f54b17bda0ac6e171306f85e6
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 (let ((info (the slot-info (slot-definition-info slotd))))
28 (ecase type
29 (reader (slot-info-reader info))
30 (writer (slot-info-writer info))
31 (boundp (slot-info-boundp info)))))
33 (defmethod (setf slot-accessor-function) (function
34 (slotd effective-slot-definition)
35 type)
36 (let ((info (the slot-info (slot-definition-info slotd))))
37 (ecase type
38 (reader (setf (slot-info-reader info) function))
39 (writer (setf (slot-info-writer info) function))
40 (boundp (setf (slot-info-boundp info) function)))))
42 (defconstant +slotd-reader-function-std-p+ 1)
43 (defconstant +slotd-writer-function-std-p+ 2)
44 (defconstant +slotd-boundp-function-std-p+ 4)
45 (defconstant +slotd-all-function-std-p+ 7)
47 (defmethod slot-accessor-std-p ((slotd effective-slot-definition) type)
48 (let ((flags (slot-value slotd 'accessor-flags)))
49 (declare (type fixnum flags))
50 (if (eq type 'all)
51 (eql +slotd-all-function-std-p+ flags)
52 (logtest flags (ecase type
53 (reader +slotd-reader-function-std-p+)
54 (writer +slotd-writer-function-std-p+)
55 (boundp +slotd-boundp-function-std-p+))))))
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 (logior (logandc2 flags mask) (if value mask 0))))
68 value)
70 (defmethod initialize-internal-slot-functions
71 ((slotd effective-slot-definition))
72 (let* ((name (slot-value slotd 'name)) ; flushable? (is it ever unbound?)
73 (class (slot-value slotd '%class)))
74 (declare (ignore name))
75 (dolist (type '(reader writer boundp))
76 (let* ((gf-name (ecase type
77 (reader 'slot-value-using-class)
78 (writer '(setf slot-value-using-class))
79 (boundp 'slot-boundp-using-class)))
80 (gf (gdefinition gf-name)))
81 ;; KLUDGE: this logic is cut'n'pasted from
82 ;; GET-ACCESSOR-METHOD-FUNCTION, which (for STD-CLASSes) is
83 ;; only called later, because it does things that can't be
84 ;; computed this early in class finalization; however, we need
85 ;; this bit as early as possible. -- CSR, 2009-11-05
86 (setf (slot-accessor-std-p slotd type)
87 (let* ((types1 `((eql ,class) (class-eq ,class) (eql ,slotd)))
88 (types (if (eq type 'writer) `(t ,@types1) types1))
89 (methods (compute-applicable-methods-using-types gf types)))
90 (null (cdr methods))))
91 (setf (slot-accessor-function slotd type)
92 (lambda (&rest args)
93 (declare (dynamic-extent args))
94 ;; FIXME: a tiny amount of wasted SLOT-ACCESSOR-STD-P
95 ;; work here (see KLUDGE comment above).
96 (let ((fun (compute-slot-accessor-info slotd type gf)))
97 (apply fun args))))))))
99 (defmethod finalize-internal-slot-functions ((slotd effective-slot-definition))
100 (dolist (type '(reader writer boundp))
101 (let* ((gf-name (ecase type
102 (reader 'slot-value-using-class)
103 (writer '(setf slot-value-using-class))
104 (boundp 'slot-boundp-using-class)))
105 (gf (gdefinition gf-name)))
106 (compute-slot-accessor-info slotd type gf))))
108 ;;; CMUCL (Gerd PCL 2003-04-25) comment:
110 ;;; Compute an effective method for SLOT-VALUE-USING-CLASS, (SETF
111 ;;; SLOT-VALUE-USING-CLASS) or SLOT-BOUNDP-USING-CLASS for reading/
112 ;;; writing/testing effective slot SLOTD.
114 ;;; TYPE is one of the symbols READER, WRITER or BOUNDP, depending on
115 ;;; GF. Store the effective method in the effective slot definition
116 ;;; object itself; these GFs have special dispatch functions calling
117 ;;; effective methods directly retrieved from effective slot
118 ;;; definition objects, as an optimization.
120 ;;; FIXME: Change the function name to COMPUTE-SVUC-SLOTD-FUNCTION,
121 ;;; or some such.
122 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
123 type gf)
124 (let* ((name (slot-value slotd 'name)) ; flushable?
125 (class (slot-value slotd '%class)))
126 (declare (ignore name))
127 (multiple-value-bind (function std-p)
128 (if (eq **boot-state** 'complete)
129 (get-accessor-method-function gf type class slotd)
130 (get-optimized-std-accessor-method-function class slotd type))
131 (setf (slot-accessor-std-p slotd type) std-p)
132 (setf (slot-accessor-function slotd type) function))))
134 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
135 :instance)
137 ;;;; various class accessors that are a little more complicated than can be
138 ;;;; done with automatically generated reader methods
140 (defmethod class-prototype :before (class)
141 (unless (class-finalized-p class)
142 (error "~@<~S is not finalized.~:@>" class)))
144 ;;; KLUDGE: For some reason factoring the common body into a function
145 ;;; breaks PCL bootstrapping, so just generate it with a macrolet for
146 ;;; all.
147 (macrolet ((def (class)
148 `(defmethod class-prototype ((class ,class))
149 (with-slots (prototype) class
150 (or prototype
151 (setf prototype (allocate-instance class)))))))
152 (def std-class)
153 (def condition-class)
154 (def structure-class))
156 (defmethod class-direct-default-initargs ((class slot-class))
157 (plist-value class 'direct-default-initargs))
159 (defmethod class-default-initargs ((class slot-class))
160 (plist-value class 'default-initargs))
162 (defmethod class-slot-cells ((class std-class))
163 (plist-value class 'class-slot-cells))
164 (defmethod (setf class-slot-cells) (new-value (class std-class))
165 (setf (plist-value class 'class-slot-cells) new-value))
167 ;;;; class accessors that are even a little bit more complicated than those
168 ;;;; above. These have a protocol for updating them, we must implement that
169 ;;;; protocol.
171 ;;; Maintaining the direct subclasses backpointers. The update methods are
172 ;;; here, the values are read by an automatically generated reader method.
173 (defmethod add-direct-subclass ((class class) (subclass class))
174 (with-slots (direct-subclasses) class
175 (pushnew subclass direct-subclasses :test #'eq)
176 subclass))
177 (defmethod remove-direct-subclass ((class class) (subclass class))
178 (with-slots (direct-subclasses) class
179 (setq direct-subclasses (remove subclass direct-subclasses))
180 subclass))
182 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
184 ;;; There are four generic functions involved, each has one method. All of
185 ;;; these are specified methods and appear in their specified place in the
186 ;;; class graph.
188 ;;; ADD-DIRECT-METHOD
189 ;;; REMOVE-DIRECT-METHOD
190 ;;; SPECIALIZER-DIRECT-METHODS
191 ;;; SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
193 ;;; In each case, we maintain one value which is a cons. The car is the list
194 ;;; of methods. The cdr is a list of the generic functions. The cdr is always
195 ;;; computed lazily.
197 ;;; This needs to be used recursively, in case a non-trivial user
198 ;;; defined ADD/REMOVE-DIRECT-METHOD method ends up calling another
199 ;;; function using the same lock.
200 (defvar *specializer-lock* (sb-thread:make-mutex :name "Specializer lock"))
202 (defmethod add-direct-method :around ((specializer specializer) method)
203 ;; All the actions done under this lock are done in an order
204 ;; that is safe to unwind at any point.
205 (sb-thread::with-recursive-system-lock (*specializer-lock*)
206 (call-next-method)))
208 (defmethod remove-direct-method :around ((specializer specializer) method)
209 ;; All the actions done under this lock are done in an order
210 ;; that is safe to unwind at any point.
211 (sb-thread::with-recursive-system-lock (*specializer-lock*)
212 (call-next-method)))
214 (defmethod add-direct-method ((specializer specializer) (method method))
215 (let ((cell (specializer-method-holder specializer)))
216 ;; We need to first smash the CDR, because a parallel read may
217 ;; be in progress, and because if an interrupt catches us we
218 ;; need to have a consistent state.
219 (setf (cdr cell) ()
220 (car cell) (adjoin method (car cell) :test #'eq)))
221 method)
223 (defmethod remove-direct-method ((specializer specializer) (method method))
224 (let ((cell (specializer-method-holder specializer)))
225 ;; We need to first smash the CDR, because a parallel read may
226 ;; be in progress, and because if an interrupt catches us we
227 ;; need to have a consistent state.
228 (setf (cdr cell) ()
229 (car cell) (remove method (car cell))))
230 method)
232 (defmethod specializer-direct-methods ((specializer specializer))
233 (car (specializer-method-holder specializer nil)))
235 (defmethod specializer-direct-generic-functions ((specializer specializer))
236 (let ((cell (specializer-method-holder specializer nil)))
237 ;; If an ADD/REMOVE-METHOD is in progress, no matter: either
238 ;; we behave as if we got just first or just after -- it's just
239 ;; for update that we need to lock.
240 (or (cdr cell)
241 (when (car cell)
242 (setf (cdr cell)
243 (sb-thread:with-mutex (*specializer-lock*)
244 (let (collect)
245 (dolist (m (car cell) (nreverse collect))
246 ;; the old PCL code used COLLECTING-ONCE which used
247 ;; #'EQ to check for newness
248 (pushnew (method-generic-function m) collect
249 :test #'eq)))))))))
251 (defmethod specializer-method-holder ((self specializer) &optional create)
252 ;; CREATE can be ignored, because instances of SPECIALIZER
253 ;; other than CLASS-EQ-SPECIALIZER have their DIRECT-METHODS slot
254 ;; preinitialized with (NIL . NIL).
255 (declare (ignore create))
256 (slot-value self 'direct-methods))
258 ;; Same as for CLASS but the only ancestor it shares is SPECIALIZER.
259 ;; If this were defined on SPECIALIZER-WITH-OBJECT then it would
260 ;; apply as well to CLASS-EQ specializers which we don't want.
261 (defmethod specializer-method-holder ((self eql-specializer) &optional create)
262 (declare (ignore create))
263 (slot-value self 'direct-methods))
265 ;;; This hash table is used to store the direct methods and direct generic
266 ;;; functions of CLASS-EQ specializers.
267 ;;; This is needed because CLASS-EQ specializers are not interned: two different
268 ;;; specializers could refer to the identical CLASS. But semantically you don't
269 ;;; want to collect the direct methods separately, you want them all together.
270 ;;; So a logical place to do that would be in the CLASS. We could add another
271 ;;; slot to CLASS holding the CLASS-EQ-DIRECT-METHODS. I guess the idea though
272 ;;; is to be able to define other kinds of specializers that hold an object
273 ;;; where you don't have the ability to add a slot to that object.
274 ;;; So that's fine, you look up the object in this table, and the approach
275 ;;; generalizes by defining other tables similarly, but has the same problem
276 ;;; of garbage retention as did EQL specializers.
277 ;;; I suspect CLASS-EQ specializers aren't used enough to worry about.
279 ;;; This table is shared between threads, so needs to be synchronized.
280 ;;; (though insertions are only performed when holding the specializer-lock,
281 ;;; so the preceding claim is probably overly paranoid.)
282 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq :synchronized t))
284 (defmethod specializer-method-table ((specializer class-eq-specializer))
285 *class-eq-specializer-methods*)
287 (defmethod specializer-method-holder ((self specializer-with-object)
288 &optional (create t))
289 (let ((table (specializer-method-table self))
290 (object (specializer-object self)))
291 (if create
292 (sb-impl::puthash-if-absent object table (lambda () (cons nil nil)))
293 (gethash object table))))
295 (defun map-specializers (function)
296 (map-all-classes (lambda (class)
297 (funcall function (class-eq-specializer class))
298 (funcall function class)))
299 (maphash (lambda (object specl)
300 (declare (ignore object))
301 (funcall function specl))
302 *eql-specializer-table*)
303 nil)
305 (defun map-all-generic-functions (function)
306 (let ((all-generic-functions (make-hash-table :test 'eq)))
307 (map-specializers (lambda (specl)
308 (dolist (gf (specializer-direct-generic-functions
309 specl))
310 (unless (gethash gf all-generic-functions)
311 (setf (gethash gf all-generic-functions) t)
312 (funcall function gf))))))
313 nil)
315 (defmethod shared-initialize :after ((specl class-eq-specializer)
316 slot-names
317 &key)
318 (declare (ignore slot-names))
319 (setf (slot-value specl '%type) `(class-eq ,(specializer-class specl))))
321 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
322 (declare (ignore slot-names))
323 (setf (slot-value specl '%type)
324 `(eql ,(specializer-object specl))))
326 (defun real-load-defclass (name metaclass-name supers slots other
327 readers writers slot-names source-location &optional 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 (with-world-lock ()
342 (apply #'ensure-class-using-class
343 (let ((class (find-class name nil)))
344 (when (and class (eq name (class-name class)))
345 ;; NAME is the proper name of CLASS, so redefine it
346 class))
347 name args)))
349 (defun parse-ensure-class-args (class name args)
350 (let ((metaclass *the-class-standard-class*)
351 (metaclassp nil)
352 (reversed-plist '()))
353 (labels ((find-class* (which class-or-name)
354 (cond
355 ((classp class-or-name)
356 (cond
357 ((eq class-or-name class)
358 (error "~@<Class ~A specified as its own ~
359 ~(~A~)class.~@:>"
360 class-or-name which))
362 class-or-name)))
363 ((and class-or-name (legal-class-name-p class-or-name))
364 (cond
365 ((eq class-or-name name)
366 (error "~@<Class named ~
367 ~/sb-impl::print-symbol-with-prefix/ ~
368 specified as its own ~(~A~)class.~@:>"
369 class-or-name which))
370 ((find-class class-or-name (eq which :meta)))
371 ((ensure-class
372 class-or-name :metaclass 'forward-referenced-class))))
374 (error "~@<Not a class or a legal ~(~A~)class name: ~
375 ~S.~@:>"
376 which class-or-name))))
377 (find-superclass (class-or-name)
378 (find-class* :super class-or-name)))
379 (doplist (key value) args
380 (case key
381 (:metaclass
382 (unless metaclassp
383 (setf metaclass (find-class* :meta value)
384 metaclassp key)))
385 (:direct-superclasses
386 (let ((superclasses (mapcar #'find-superclass value)))
387 (setf reversed-plist (list* superclasses key reversed-plist))))
389 (setf reversed-plist (list* value key reversed-plist)))))
390 (values metaclass (nreverse reversed-plist)))))
392 (defun call-with-ensure-class-context (class name args thunk)
393 (let ((class (with-world-lock ()
394 (multiple-value-bind (metaclass initargs)
395 (parse-ensure-class-args class name args)
396 (let ((class (funcall thunk class name metaclass initargs)))
397 (without-package-locks
398 (setf (find-class name) class)))))))
399 class))
401 (defmethod ensure-class-using-class ((class null) name &rest args &key)
402 (call-with-ensure-class-context
403 class name args (lambda (class name metaclass initargs)
404 (declare (ignore class))
405 (apply #'make-instance metaclass :name name initargs))))
407 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
408 (call-with-ensure-class-context
409 class name args (lambda (class name metaclass initargs)
410 (aver (eq name (class-name class)))
411 (unless (eq (class-of class) metaclass)
412 (apply #'change-class class metaclass initargs))
413 (apply #'reinitialize-instance class initargs)
414 class)))
416 ;;; This is used to call initfunctions of :allocation :class slots.
417 (defun call-initfun (fun slotd safe)
418 (declare (function fun))
419 (let ((value (funcall fun)))
420 (when safe
421 (let ((type (slot-definition-type slotd)))
422 (unless (or (eq t type)
423 (typep value type))
424 (error 'type-error :expected-type type :datum value))))
425 value))
427 (defmethod shared-initialize :after
428 ((class std-class) slot-names &key
429 (direct-superclasses nil direct-superclasses-p)
430 (direct-slots nil direct-slots-p)
431 (direct-default-initargs nil direct-default-initargs-p))
432 (cond (direct-superclasses-p
433 (setq direct-superclasses
434 (or direct-superclasses
435 (list (if (funcallable-standard-class-p class)
436 *the-class-funcallable-standard-object*
437 *the-class-standard-object*))))
438 (dolist (superclass direct-superclasses)
439 (unless (validate-superclass class superclass)
440 (invalid-superclass class superclass)))
441 (setf (slot-value class 'direct-superclasses) direct-superclasses))
443 (setq direct-superclasses (slot-value class 'direct-superclasses))))
444 (setq direct-slots
445 (if direct-slots-p
446 (setf (slot-value class 'direct-slots)
447 (mapcar (lambda (pl) (make-direct-slotd class pl))
448 direct-slots))
449 (slot-value class 'direct-slots)))
450 (if direct-default-initargs-p
451 (setf (plist-value class 'direct-default-initargs)
452 direct-default-initargs)
453 (setq direct-default-initargs
454 (plist-value class 'direct-default-initargs)))
455 (setf (plist-value class 'class-slot-cells)
456 (let ((old-class-slot-cells (plist-value class 'class-slot-cells))
457 (safe (safe-p class))
458 (collect '()))
459 (dolist (dslotd direct-slots)
460 (when (eq :class (slot-definition-allocation dslotd))
461 ;; see CLHS 4.3.6
462 (let* ((name (slot-definition-name dslotd))
463 (old (assoc name old-class-slot-cells)))
464 (if (or (not old)
465 (eq t slot-names)
466 (member name slot-names :test #'eq))
467 (let* ((initfunction (slot-definition-initfunction dslotd))
468 (value
469 (if initfunction
470 (call-initfun initfunction dslotd safe)
471 +slot-unbound+)))
472 (push (cons name value) collect))
473 (push old collect)))))
474 (nreverse collect)))
475 (add-direct-subclasses class direct-superclasses)
476 (if (class-finalized-p class)
477 ;; required by AMOP, "Reinitialization of Class Metaobjects"
478 (finalize-inheritance class)
479 (update-class class nil))
480 (add-slot-accessors class direct-slots)
481 (make-preliminary-layout class))
483 (define-condition invalid-superclass (reference-condition error)
484 ((class :initarg :class :reader invalid-superclass-class)
485 (superclass :initarg :superclass :reader invalid-superclass-superclass))
486 (:report
487 (lambda (c s)
488 (let ((class (invalid-superclass-class c))
489 (superclass (invalid-superclass-superclass c)))
490 (format s
491 "~@<The class ~S was specified as a superclass of the ~
492 class ~S, but the metaclasses ~S and ~S are ~
493 incompatible.~@[ Define a method for ~S to avoid this ~
494 error.~]~@:>"
495 superclass class (class-of superclass) (class-of class)
496 (and (typep superclass 'standard-class)
497 'validate-superclass))))))
499 (defmethod invalid-superclass ((class class) (superclass class))
500 (error 'invalid-superclass :class class :superclass superclass
501 :references (list* '(:amop :generic-function validate-superclass)
502 (and (typep superclass 'built-in-class)
503 (list '(:ansi-cl :system-class built-in-class)
504 '(:ansi-cl :section (4 3 7)))))))
506 (defmethod shared-initialize :after ((class forward-referenced-class)
507 slot-names &key &allow-other-keys)
508 (declare (ignore slot-names))
509 (make-preliminary-layout class))
511 (defvar *allow-forward-referenced-classes-in-cpl-p* nil)
513 ;;; Give CLASS a preliminary layout if it doesn't have one already, to
514 ;;; make it known to the type system.
515 (defun make-preliminary-layout (class)
516 (flet ((compute-preliminary-cpl (root)
517 (let ((*allow-forward-referenced-classes-in-cpl-p* t))
518 (compute-class-precedence-list root))))
519 (with-world-lock ()
520 (without-package-locks
521 (unless (class-finalized-p class)
522 (let ((name (class-name class))) ; flushable?
523 (declare (ignore name))
524 ;; KLUDGE: This is fairly horrible. We need to make a
525 ;; full-fledged CLASSOID here, not just tell the compiler that
526 ;; some class is forthcoming, because there are legitimate
527 ;; questions one can ask of the type system, implemented in
528 ;; terms of CLASSOIDs, involving forward-referenced classes. So.
529 (let ((layout (make-wrapper 0 class)))
530 (setf (slot-value class 'wrapper) layout)
531 (let ((cpl (compute-preliminary-cpl class)))
532 (setf (layout-inherits layout)
533 (order-layout-inherits
534 (map 'simple-vector #'class-wrapper
535 (reverse (rest cpl))))))
536 (register-layout layout :invalidate t))))
537 (mapc #'make-preliminary-layout (class-direct-subclasses class))))))
540 (defmethod shared-initialize :before ((class class) slot-names &key name)
541 (declare (ignore slot-names name))
542 ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
543 ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
544 (setf (slot-value class '%type) `(class ,class))
545 (setf (slot-value class 'class-eq-specializer)
546 (make-instance 'class-eq-specializer :class class)))
548 (defmethod reinitialize-instance :before ((class slot-class) &key direct-superclasses)
549 (dolist (old-super (set-difference (class-direct-superclasses class) direct-superclasses))
550 (remove-direct-subclass old-super class))
551 (remove-slot-accessors class (class-direct-slots class)))
553 (defmethod reinitialize-instance :after ((class slot-class)
554 &rest initargs
555 &key)
556 (map-dependents class
557 (lambda (dependent)
558 (apply #'update-dependent class dependent initargs))))
560 (defmethod reinitialize-instance :after ((class condition-class) &key)
561 (let* ((name (class-name class))
562 (classoid (find-classoid name))
563 (slots (condition-classoid-slots classoid))
564 (source (sb-kernel::layout-source-location (classoid-layout classoid))))
565 ;; to balance the REMOVE-SLOT-ACCESSORS call in
566 ;; REINITIALIZE-INSTANCE :BEFORE (SLOT-CLASS).
567 (flet ((add-source-location (method)
568 (when source
569 (setf (slot-value method 'source) source))))
570 (dolist (slot slots)
571 (let ((slot-name (condition-slot-name slot)))
572 (dolist (reader (condition-slot-readers slot))
573 ;; FIXME: see comment in SHARED-INITIALIZE :AFTER
574 ;; (CONDITION-CLASS T), below. -- CSR, 2005-11-18
575 (add-source-location
576 (sb-kernel::install-condition-slot-reader reader name slot-name)))
577 (dolist (writer (condition-slot-writers slot))
578 (add-source-location
579 (sb-kernel::install-condition-slot-writer writer name slot-name))))))))
581 (defmethod shared-initialize :after ((class condition-class) slot-names
582 &key direct-slots direct-superclasses)
583 (declare (ignore slot-names))
584 (let ((classoid (find-classoid (slot-value class 'name))))
585 (with-slots (wrapper
586 %class-precedence-list cpl-available-p finalized-p
587 prototype (direct-supers direct-superclasses)
588 plist)
589 class
590 (setf (slot-value class 'direct-slots)
591 (mapcar (lambda (pl) (make-direct-slotd class pl))
592 direct-slots)
593 finalized-p t
594 (classoid-pcl-class classoid) class
595 direct-supers direct-superclasses
596 wrapper (classoid-layout classoid)
597 %class-precedence-list (compute-class-precedence-list class)
598 cpl-available-p t
599 (getf plist 'direct-default-initargs)
600 (sb-kernel::condition-classoid-direct-default-initargs classoid))
601 (add-direct-subclasses class direct-superclasses)
602 (let ((slots (compute-slots class)))
603 (setf (slot-value class 'slots) slots)
604 (setf (layout-slot-table wrapper) (make-slot-table class slots)))))
605 ;; Comment from Gerd's PCL, 2003-05-15:
607 ;; We don't ADD-SLOT-ACCESSORS here because we don't want to
608 ;; override condition accessors with generic functions. We do this
609 ;; differently.
611 ;; ??? What does the above comment mean and why is it a good idea?
612 ;; CMUCL (which still as of 2005-11-18 uses this code and has this
613 ;; comment) loses slot information in its condition classes:
614 ;; DIRECT-SLOTS is always NIL. We have the right information, so we
615 ;; remove slot accessors but never put them back. I've added a
616 ;; REINITIALIZE-INSTANCE :AFTER (CONDITION-CLASS) method, but what
617 ;; was meant to happen? -- CSR, 2005-11-18
620 (defmethod direct-slot-definition-class ((class condition-class)
621 &rest initargs)
622 (declare (ignore initargs))
623 (find-class 'condition-direct-slot-definition))
625 (defmethod effective-slot-definition-class ((class condition-class)
626 &rest initargs)
627 (declare (ignore initargs))
628 (find-class 'condition-effective-slot-definition))
630 (defmethod finalize-inheritance ((class condition-class))
631 (aver (slot-value class 'finalized-p))
632 nil)
634 (defmethod compute-effective-slot-definition
635 ((class condition-class) slot-name dslotds)
636 (let* ((slotd (call-next-method))
637 (info (slot-definition-info slotd)))
638 (setf (slot-info-reader info)
639 (lambda (x)
640 (handler-case (condition-reader-function x slot-name)
641 ;; FIXME: FIND-SLOT-DEFAULT throws an error if the slot
642 ;; is unbound; maybe it should be a CELL-ERROR of some
643 ;; sort?
644 (error () (values (slot-unbound class x slot-name))))))
645 (setf (slot-info-writer info)
646 (lambda (v x)
647 (condition-writer-function x v slot-name)))
648 (setf (slot-info-boundp info)
649 (lambda (x)
650 (multiple-value-bind (v c)
651 (ignore-errors (condition-reader-function x slot-name))
652 (declare (ignore v))
653 (null c))))
654 slotd))
656 (defmethod compute-slots :around ((class condition-class))
657 (let ((eslotds (call-next-method)))
658 (mapc #'finalize-internal-slot-functions eslotds)
659 eslotds))
661 (defmethod shared-initialize :after
662 ((slotd structure-slot-definition) slot-names &key
663 (allocation :instance) allocation-class)
664 (declare (ignore slot-names allocation-class))
665 (unless (eq allocation :instance)
666 (error "Structure slots must have :INSTANCE allocation.")))
668 (defun make-structure-class-defstruct-form (name direct-slots include)
669 (let* ((conc-name (format-symbol *package* "~S structure class " name))
670 (constructor (format-symbol *package* "~Aconstructor" conc-name))
671 (included-name (class-name include))
672 (included-slots
673 (when include
674 (mapcar #'dsd-name (dd-slots (find-defstruct-description included-name)))))
675 (old-slots nil)
676 (new-slots nil)
677 (reader-names nil)
678 (writer-names nil))
679 (dolist (slotd (reverse direct-slots))
680 (let* ((slot-name (slot-definition-name slotd))
681 (initform (slot-definition-initform slotd))
682 (type (slot-definition-type slotd))
683 (desc `(,slot-name ,initform :type ,type)))
684 (push `(slot-accessor ,name ,slot-name reader)
685 reader-names)
686 (push `(slot-accessor ,name ,slot-name writer)
687 writer-names)
688 (if (member slot-name included-slots :test #'eq)
689 (push desc old-slots)
690 (push desc new-slots))))
691 (let* ((defstruct `(defstruct (,name
692 ,@(when include
693 `((:include ,included-name
694 ,@old-slots)))
695 (:constructor ,constructor ())
696 (:predicate nil)
697 (:conc-name ,conc-name)
698 (:copier nil))
699 ,@new-slots))
700 (readers-init
701 (mapcar (lambda (slotd reader-name)
702 (let ((accessor
703 (slot-definition-defstruct-accessor-symbol
704 slotd)))
705 `(defun ,reader-name (obj)
706 (declare (type ,name obj))
707 (,accessor obj))))
708 direct-slots reader-names))
709 (writers-init
710 (mapcar (lambda (slotd writer-name)
711 (let ((accessor
712 (slot-definition-defstruct-accessor-symbol
713 slotd)))
714 `(defun ,writer-name (nv obj)
715 (declare (type ,name obj))
716 (setf (,accessor obj) nv))))
717 direct-slots writer-names))
718 (defstruct-form
719 `(progn
720 ,defstruct
721 ,@readers-init ,@writers-init
722 (cons nil nil))))
723 (values defstruct-form constructor reader-names writer-names))))
725 ;;; Return a thunk to allocate an instance of CLASS named NAME.
726 ;;; This is broken with regard to the expectation that all semantic
727 ;;; processing is done by the compiler. e.g. after COMPILE-FILE on
728 ;;; a file containing these two toplevel forms:
729 ;;; (eval-when (:compile-toplevel) (defmacro maker () '(list 1)))
730 ;;; (defstruct foo (a (maker)))
731 ;;; then LOADing the resulting fasl in a fresh image will err:
732 ;;; (make-instance 'foo) => "The function MAKER is undefined."
734 ;;; The way to fix that is to ensure that every defstruct has a zero-argument
735 ;;; constructor made by the compiler and stashed in a random symbol.
736 (defun make-defstruct-allocation-function (name class)
737 (declare (muffle-conditions code-deletion-note))
738 ;; FIXME: Why don't we go class->layout->info == dd
739 (let ((dd (find-defstruct-description name)))
740 (ecase (dd-type dd)
741 (structure
742 ;; This used to call COMPILE directly, which is basically pointless,
743 ;; because it certainly does not avoid compiling code at runtime,
744 ;; which seems to have been the goal. We're also compiling:
745 ;; (LAMBDA () (SB-PCL::FAST-MAKE-INSTANCE #<STRUCTURE-CLASS THING>))
746 ;; So maybe we can figure out how to bundle two lambdas together?
747 (lambda ()
748 (let* ((dd (layout-info (class-wrapper class)))
749 (f (%make-structure-instance-allocator dd nil)))
750 (if (functionp f)
751 (funcall (setf (slot-value class 'defstruct-constructor) f))
752 (error "Can't allocate ~S" class)))))
753 (funcallable-structure
754 ;; FIXME: you can't dynamically define new funcallable structures
755 ;; that are not GENERIC-FUNCTION subtypes, so why this branch?
756 ;; We should pull this out, fixup PCL bootstrap, and not pretend
757 ;; that this code is more general than it really is.
758 (%make-funcallable-structure-instance-allocator dd nil)))))
760 (defmethod shared-initialize :after
761 ((class structure-class) slot-names &key
762 (direct-superclasses nil direct-superclasses-p)
763 (direct-slots nil direct-slots-p)
764 direct-default-initargs)
765 (declare (ignore slot-names direct-default-initargs))
766 (if direct-superclasses-p
767 (setf (slot-value class 'direct-superclasses)
768 (or direct-superclasses
769 (setq direct-superclasses
770 (and (not (eq (slot-value class 'name) 'structure-object))
771 (list *the-class-structure-object*)))))
772 (setq direct-superclasses (slot-value class 'direct-superclasses)))
773 (let* ((name (slot-value class 'name))
774 (from-defclass-p (slot-value class 'from-defclass-p))
775 ;; DEFSTRUCT-P means we should perform the effect of DEFSTRUCT,
776 ;; and not that this structure came from a DEFSTRUCT.
777 (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
778 (if direct-slots-p
779 (setf (slot-value class 'direct-slots)
780 (setq direct-slots
781 (mapcar (lambda (pl)
782 (when defstruct-p
783 (let* ((slot-name (getf pl :name))
784 (accessor
785 (format-symbol *package*
786 "~S structure class ~A"
787 name slot-name)))
788 (setq pl (list* :defstruct-accessor-symbol
789 accessor pl))))
790 (make-direct-slotd class pl))
791 direct-slots)))
792 (setq direct-slots (slot-value class 'direct-slots)))
793 (if defstruct-p
794 (let ((include (car (slot-value class 'direct-superclasses))))
795 (multiple-value-bind (defstruct-form constructor reader-names writer-names)
796 (make-structure-class-defstruct-form name direct-slots include)
797 (unless (structure-type-p name) (eval defstruct-form))
798 (mapc (lambda (dslotd reader-name writer-name)
799 (let* ((reader (gdefinition reader-name))
800 (writer (when (fboundp writer-name)
801 (gdefinition writer-name))))
802 (setf (slot-value dslotd 'internal-reader-function)
803 reader)
804 (setf (slot-value dslotd 'internal-writer-function)
805 writer)))
806 direct-slots reader-names writer-names)
807 (setf (slot-value class 'defstruct-form) defstruct-form)
808 (setf (slot-value class 'defstruct-constructor) constructor)))
809 ;; FIXME: If we always need a default constructor, then why not just make
810 ;; a "hidden" one at actual-compile-time and store it?
811 ;; And this is very broken with regard to the lexical environment.
812 ;; And why isn't this just DD-DEFAULT-CONSTRUCTOR if there was one?
813 (setf (slot-value class 'defstruct-constructor)
814 ;; KLUDGE: not class; in fixup.lisp, can't access slots
815 ;; outside methods yet.
816 (make-defstruct-allocation-function name class)))
817 (add-direct-subclasses class direct-superclasses)
818 (setf (slot-value class '%class-precedence-list)
819 (compute-class-precedence-list class))
820 (setf (slot-value class 'cpl-available-p) t)
821 (let ((slots (compute-slots class)))
822 (setf (slot-value class 'slots) slots)
823 (let* ((lclass (find-classoid (slot-value class 'name)))
824 (layout (classoid-layout lclass)))
825 (setf (classoid-pcl-class lclass) class)
826 (setf (slot-value class 'wrapper) layout)
827 (setf (layout-slot-table layout) (make-slot-table class slots))))
828 (setf (slot-value class 'finalized-p) t)
829 (add-slot-accessors class direct-slots)))
831 (defmethod direct-slot-definition-class ((class structure-class) &rest initargs)
832 (declare (ignore initargs))
833 (find-class 'structure-direct-slot-definition))
835 (defmethod finalize-inheritance ((class structure-class))
836 nil) ; always finalized
838 (defun add-slot-accessors (class dslotds)
839 (fix-slot-accessors class dslotds 'add))
841 (defun remove-slot-accessors (class dslotds)
842 (fix-slot-accessors class dslotds 'remove))
844 (defun fix-slot-accessors (class dslotds add/remove)
845 (flet ((fix (gfspec name r/w doc source-location)
846 (let ((gf (cond ((eq add/remove 'add)
847 (or (find-generic-function gfspec nil)
848 (ensure-generic-function
849 gfspec :lambda-list (case r/w
850 (r '(object))
851 (w '(new-value object))))))
853 (find-generic-function gfspec nil)))))
854 (when gf
855 (case r/w
856 (r (if (eq add/remove 'add)
857 (add-reader-method class gf name doc source-location)
858 (remove-reader-method class gf)))
859 (w (if (eq add/remove 'add)
860 (add-writer-method class gf name doc source-location)
861 (remove-writer-method class gf))))))))
862 (dolist (dslotd dslotds)
863 (let ((slot-name (slot-definition-name dslotd))
864 (slot-doc (%slot-definition-documentation dslotd))
865 (location (definition-source dslotd)))
866 (dolist (r (slot-definition-readers dslotd))
867 (fix r slot-name 'r slot-doc location))
868 (dolist (w (slot-definition-writers dslotd))
869 (fix w slot-name 'w slot-doc location))))))
871 (defun add-direct-subclasses (class supers)
872 (dolist (super supers)
873 (unless (memq class (class-direct-subclasses class))
874 (add-direct-subclass super class))))
876 (defmethod finalize-inheritance ((class std-class))
877 (update-class class t))
879 (defmethod finalize-inheritance ((class forward-referenced-class))
880 ;; FIXME: should we not be thinking a bit about what kinds of error
881 ;; we're throwing? Maybe we need a clos-error type to mix in? Or
882 ;; possibly a forward-referenced-class-error, though that's
883 ;; difficult given e.g. class precedence list calculations...
884 (error
885 "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
886 ~2I~_~S~:>"
887 class))
890 (defun class-has-a-forward-referenced-superclass-p (class)
891 (or (when (forward-referenced-class-p class)
892 class)
893 (some #'class-has-a-forward-referenced-superclass-p
894 (class-direct-superclasses class))))
896 ;;; This is called by :after shared-initialize whenever a class is initialized
897 ;;; or reinitialized. The class may or may not be finalized.
898 (defun update-class (class finalizep)
899 (labels ((rec (class finalizep &optional (seen '()))
900 (when (find class seen :test #'eq)
901 (error "~@<Specified class ~S as a superclass of ~
902 itself.~@:>"
903 class))
904 (without-package-locks
905 (with-world-lock ()
906 (when (or finalizep (class-finalized-p class))
907 (%update-cpl class (compute-class-precedence-list class))
908 ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
909 ;; class
910 (%update-slots class (compute-slots class))
911 (update-gfs-of-class class)
912 (setf (plist-value class 'default-initargs) (compute-default-initargs class))
913 (update-ctors 'finalize-inheritance :class class))
914 (let ((seen (list* class seen)))
915 (dolist (sub (class-direct-subclasses class))
916 (rec sub nil seen)))))))
917 (rec class finalizep)))
919 (define-condition cpl-protocol-violation (reference-condition error)
920 ((class :initarg :class :reader cpl-protocol-violation-class)
921 (cpl :initarg :cpl :reader cpl-protocol-violation-cpl))
922 (:default-initargs :references (list '(:sbcl :node "Metaobject Protocol")))
923 (:report
924 (lambda (c s)
925 (format s "~@<Protocol violation: the ~S class ~S ~
926 ~:[has~;does not have~] the class ~S in its ~
927 class precedence list: ~S.~@:>"
928 (class-name (class-of (cpl-protocol-violation-class c)))
929 (cpl-protocol-violation-class c)
930 (eq (class-of (cpl-protocol-violation-class c))
931 *the-class-funcallable-standard-class*)
932 (find-class 'function)
933 (cpl-protocol-violation-cpl c)))))
935 (defun class-has-a-cpl-protocol-violation-p (class)
936 (labels ((find-in-superclasses (class classes)
937 (cond
938 ((null classes) nil)
939 ((eql class (car classes)) t)
940 (t (find-in-superclasses class (append (class-direct-superclasses (car classes)) (cdr classes)))))))
941 (let ((metaclass (class-of class)))
942 (cond
943 ((eql metaclass *the-class-standard-class*)
944 (find-in-superclasses (find-class 'function) (list class)))
945 ((eql metaclass *the-class-funcallable-standard-class*)
946 (not (find-in-superclasses (find-class 'function) (list class))))))))
948 (defun %update-cpl (class cpl)
949 (when (or (and
950 (eq (class-of class) *the-class-standard-class*)
951 (find *the-class-function* cpl))
952 (and (eq (class-of class) *the-class-funcallable-standard-class*)
953 (not (and (find (find-class 'function) cpl)))))
954 (error 'cpl-protocol-violation :class class :cpl cpl))
955 (cond ((not (class-finalized-p class))
956 (setf (slot-value class '%class-precedence-list) cpl
957 (slot-value class 'cpl-available-p) t))
958 ((not (and (equal (class-precedence-list class) cpl)
959 (dolist (c cpl t)
960 (when (position :class (class-direct-slots c)
961 :key #'slot-definition-allocation)
962 (return nil)))))
964 ;; comment from the old CMU CL sources:
965 ;; Need to have the cpl setup before %update-lisp-class-layout
966 ;; is called on CMU CL.
967 (setf (slot-value class '%class-precedence-list) cpl
968 (slot-value class 'cpl-available-p) t)
969 (%force-cache-flushes class)))
970 (update-class-can-precede-p cpl))
972 (defun update-class-can-precede-p (cpl)
973 (when cpl
974 (let ((first (car cpl)))
975 (dolist (c (cdr cpl))
976 (pushnew c (slot-value first 'can-precede-list) :test #'eq)))
977 (update-class-can-precede-p (cdr cpl))))
979 (defun class-can-precede-p (class1 class2)
980 (member class2 (class-can-precede-list class1) :test #'eq))
982 ;;; This is called from %UPDATE-SLOTS to check if slot layouts are compatible.
984 ;;; In addition to slot locations (implicit in the ordering of the slots), we
985 ;;; must check classes: SLOT-INFO structures from old slotds may have been
986 ;;; cached in permutation vectors, but new slotds have had new ones allocated
987 ;;; to them. This is non-problematic for standard slotds, because we know the
988 ;;; structure is compatible, but if a slot definition class changes, this can
989 ;;; change the way SLOT-VALUE-USING-CLASS should dispatch.
991 ;;; Also, if the slot has a non-standard allocation, we need to check that it
992 ;;; doesn't change.
993 (defun slot-layouts-compatible-p
994 (oslotds new-instance-slotds new-class-slotds new-custom-slotds)
995 (multiple-value-bind (old-instance-slotds old-class-slotds old-custom-slotds)
996 (classify-slotds oslotds)
997 (and
998 ;; Instance slots: name, type, and class.
999 (dolist (o old-instance-slotds (not new-instance-slotds))
1000 (let ((n (pop new-instance-slotds)))
1001 (unless (and n
1002 (eq (slot-definition-name o) (slot-definition-name n))
1003 (eq (slot-definition-type o) (slot-definition-type n))
1004 (eq (class-of o) (class-of n)))
1005 (return nil))))
1006 ;; Class slots: name and class. (FIXME: class slots not typechecked?)
1007 (dolist (o old-class-slotds (not new-class-slotds))
1008 (let ((n (pop new-class-slotds)))
1009 (unless (and n
1010 (eq (slot-definition-name o) (slot-definition-name n))
1011 (eq (class-of n) (class-of o)))
1012 (return nil))))
1013 ;; Custom slots: check name, type, allocation, and class. (FIXME: should we just punt?)
1014 (dolist (o old-custom-slotds (not new-custom-slotds))
1015 (let ((n (pop new-custom-slotds)))
1016 (unless (and n
1017 (eq (slot-definition-name o) (slot-definition-name n))
1018 (eq (slot-definition-type o) (slot-definition-type n))
1019 (eq (slot-definition-allocation o) (slot-definition-allocation n))
1020 (eq (class-of o) (class-of n)))
1021 (return nil)))))))
1023 (defun style-warn-about-duplicate-slots (class)
1024 (do* ((slots (slot-value class 'slots) (cdr slots))
1025 (dupes nil))
1026 ((null slots)
1027 (when dupes
1028 (style-warn
1029 "~@<slot names with the same SYMBOL-NAME but ~
1030 different SYMBOL-PACKAGE (possible package problem) ~
1031 for class ~S:~4I~@:_~<~@{~/sb-impl::print-symbol-with-prefix/~^~:@_~}~:>~@:>"
1032 class dupes)))
1033 (let* ((slot-name (slot-definition-name (car slots)))
1034 (oslots (and (not (eq (symbol-package slot-name)
1035 *pcl-package*))
1036 (remove-if
1037 (lambda (slot-name-2)
1038 (or (eq (symbol-package slot-name-2)
1039 *pcl-package*)
1040 (string/= slot-name slot-name-2)))
1041 (cdr slots)
1042 :key #'slot-definition-name))))
1043 (when oslots
1044 (pushnew (cons slot-name
1045 (mapcar #'slot-definition-name oslots))
1046 dupes
1047 :test #'string= :key #'car)))))
1049 (defun %update-slots (class eslotds)
1050 (multiple-value-bind (instance-slots class-slots custom-slots)
1051 (classify-slotds eslotds)
1052 (let* ((nslots (length instance-slots))
1053 (owrapper (class-wrapper class))
1054 (nwrapper
1055 (cond ((and owrapper
1056 (slot-layouts-compatible-p (layout-slot-list owrapper)
1057 instance-slots class-slots custom-slots))
1058 owrapper)
1059 ((or (not owrapper)
1060 (not (class-finalized-p class)))
1061 (make-wrapper nslots class))
1063 ;; This will initialize the new wrapper to have the
1064 ;; same state as the old wrapper. We will then have
1065 ;; to change that. This may seem like wasted work
1066 ;; (and it is), but the spec requires that we call
1067 ;; MAKE-INSTANCES-OBSOLETE.
1068 (make-instances-obsolete class)
1069 (class-wrapper class)))))
1070 (%update-lisp-class-layout class nwrapper)
1071 (setf (slot-value class 'slots) eslotds
1072 (layout-slot-list nwrapper) eslotds
1073 (layout-slot-table nwrapper) (make-slot-table class eslotds)
1074 (layout-length nwrapper) nslots
1075 (slot-value class 'wrapper) nwrapper)
1076 (style-warn-about-duplicate-slots class)
1077 (setf (slot-value class 'finalized-p) t)
1078 (unless (eq owrapper nwrapper)
1079 (maybe-update-standard-slot-locations class)))))
1081 (defun update-gf-dfun (class gf)
1082 (let ((*new-class* class)
1083 (arg-info (gf-arg-info gf)))
1084 (cond
1085 ((special-case-for-compute-discriminating-function-p gf))
1086 ((gf-precompute-dfun-and-emf-p arg-info)
1087 (multiple-value-bind (dfun cache info) (make-final-dfun-internal gf)
1088 (update-dfun gf dfun cache info))))))
1090 (defun update-gfs-of-class (class)
1091 (when (and (class-finalized-p class)
1092 (let ((cpl (class-precedence-list class)))
1093 (or (member *the-class-slot-class* cpl :test #'eq)
1094 (member *the-class-standard-effective-slot-definition*
1095 cpl :test #'eq))))
1096 (let ((gf-table (make-hash-table :test 'eq)))
1097 (labels ((collect-gfs (class)
1098 (dolist (gf (specializer-direct-generic-functions class))
1099 (setf (gethash gf gf-table) t))
1100 (mapc #'collect-gfs (class-direct-superclasses class))))
1101 (collect-gfs class)
1102 (maphash (lambda (gf ignore)
1103 (declare (ignore ignore))
1104 (update-gf-dfun class gf))
1105 gf-table)))))
1107 (defmethod compute-default-initargs ((class slot-class))
1108 (let ((initargs (loop for c in (class-precedence-list class)
1109 append (class-direct-default-initargs c))))
1110 (delete-duplicates initargs :test #'eq :key #'car :from-end t)))
1112 ;;;; protocols for constructing direct and effective slot definitions
1114 (defmethod direct-slot-definition-class ((class std-class) &rest initargs)
1115 (declare (ignore initargs))
1116 (find-class 'standard-direct-slot-definition))
1118 (defun make-direct-slotd (class initargs)
1119 (apply #'make-instance
1120 (apply #'direct-slot-definition-class class initargs)
1121 :class class
1122 initargs))
1124 ;;; I (CSR) am not sure, but I believe that the particular order of
1125 ;;; slots is quite important: it is ideal to attempt to have a
1126 ;;; constant slot location for the same notional slots as much as
1127 ;;; possible, so that clever discriminating functions (ONE-INDEX et
1128 ;;; al.) have a chance of working. The below at least walks through
1129 ;;; the slots predictably, but maybe it would be good to compute some
1130 ;;; kind of optimal slot layout by looking at locations of slots in
1131 ;;; superclasses?
1132 (defun std-compute-slots (class)
1133 ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
1134 ;; for each different slot name we find in our superclasses. Each
1135 ;; call receives the class and a list of the dslotds with that name.
1136 ;; The list is in most-specific-first order.
1137 (let ((name-dslotds-alist ()))
1138 (dolist (c (reverse (class-precedence-list class)))
1139 (dolist (slot (class-direct-slots c))
1140 (let* ((name (slot-definition-name slot))
1141 (entry (assq name name-dslotds-alist)))
1142 (if entry
1143 (push slot (cdr entry))
1144 (push (list name slot) name-dslotds-alist)))))
1145 (mapcar (lambda (direct)
1146 (compute-effective-slot-definition class
1147 (car direct)
1148 (cdr direct)))
1149 (nreverse name-dslotds-alist))))
1151 ;; It seems to me that this should be one method defined on SLOT-CLASS.
1152 (defmethod compute-slots ((class standard-class))
1153 (std-compute-slots class))
1154 (defmethod compute-slots ((class funcallable-standard-class))
1155 (std-compute-slots class))
1156 (defmethod compute-slots ((class structure-class))
1157 (std-compute-slots class))
1158 (defmethod compute-slots ((class condition-class))
1159 (std-compute-slots class))
1161 (defun std-compute-slots-around (class eslotds)
1162 (let ((location -1)
1163 (safe (safe-p class)))
1164 (dolist (eslotd eslotds eslotds)
1165 (setf (slot-definition-location eslotd)
1166 (case (slot-definition-allocation eslotd)
1167 (:instance
1168 (incf location))
1169 (:class
1170 (let* ((name (slot-definition-name eslotd))
1171 (from-class
1173 (slot-definition-allocation-class eslotd)
1174 ;; we get here if the user adds an extra slot
1175 ;; himself...
1176 (setf (slot-definition-allocation-class eslotd)
1177 class)))
1178 ;; which raises the question of what we should
1179 ;; do if we find that said user has added a slot
1180 ;; with the same name as another slot...
1181 (cell (or (assq name (class-slot-cells from-class))
1182 (let ((c (cons name +slot-unbound+)))
1183 (push c (class-slot-cells from-class))
1184 c))))
1185 (aver (consp cell))
1186 (if (eq +slot-unbound+ (cdr cell))
1187 ;; We may have inherited an initfunction FIXME: Is this
1188 ;; really right? Is the initialization in
1189 ;; SHARED-INITIALIZE (STD-CLASS) not enough?
1190 (let ((initfun (slot-definition-initfunction eslotd)))
1191 (if initfun
1192 (rplacd cell (call-initfun initfun eslotd safe))
1193 cell))
1194 cell)))))
1195 (unless (slot-definition-class eslotd)
1196 (setf (slot-definition-class eslotd) class))
1197 (initialize-internal-slot-functions eslotd))))
1199 (defmethod compute-slots :around ((class standard-class))
1200 (let ((eslotds (call-next-method)))
1201 (std-compute-slots-around class eslotds)))
1202 (defmethod compute-slots :around ((class funcallable-standard-class))
1203 (let ((eslotds (call-next-method)))
1204 (std-compute-slots-around class eslotds)))
1205 (defmethod compute-slots :around ((class structure-class))
1206 (let ((eslotds (call-next-method)))
1207 (mapc #'finalize-internal-slot-functions eslotds)
1208 eslotds))
1210 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
1211 (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
1212 (class (apply #'effective-slot-definition-class class initargs))
1213 (slotd (apply #'make-instance class initargs)))
1214 slotd))
1216 (defmethod effective-slot-definition-class ((class std-class) &rest initargs)
1217 (declare (ignore initargs))
1218 (find-class 'standard-effective-slot-definition))
1220 (defmethod effective-slot-definition-class ((class structure-class) &rest initargs)
1221 (declare (ignore initargs))
1222 (find-class 'structure-effective-slot-definition))
1224 (defmethod compute-effective-slot-definition-initargs
1225 ((class slot-class) direct-slotds)
1226 (let* ((name nil)
1227 (initfunction nil)
1228 (initform nil)
1229 (initargs nil)
1230 (allocation nil)
1231 (allocation-class nil)
1232 (type t)
1233 (documentation nil)
1234 (documentationp nil)
1235 (namep nil)
1236 (initp nil)
1237 (allocp nil))
1239 (dolist (slotd direct-slotds)
1240 (when slotd
1241 (unless namep
1242 (setq name (slot-definition-name slotd)
1243 namep t))
1244 (unless initp
1245 (awhen (slot-definition-initfunction slotd)
1246 (setq initform (slot-definition-initform slotd)
1247 initfunction it
1248 initp t)))
1249 (unless documentationp
1250 (awhen (%slot-definition-documentation slotd)
1251 (setq documentation it
1252 documentationp t)))
1253 (unless allocp
1254 (setq allocation (slot-definition-allocation slotd)
1255 allocation-class (slot-definition-class slotd)
1256 allocp t))
1257 (setq initargs (append (slot-definition-initargs slotd) initargs))
1258 (let ((slotd-type (slot-definition-type slotd)))
1259 (setq type (cond
1260 ((eq type t) slotd-type)
1261 ;; This pairwise type intersection is perhaps a
1262 ;; little inefficient and inelegant, but it's
1263 ;; unlikely to lie on the critical path. Shout
1264 ;; if I'm wrong. -- CSR, 2005-11-24
1265 (t (type-specifier
1266 (specifier-type `(and ,type ,slotd-type)))))))))
1267 (list :name name
1268 :initform initform
1269 :initfunction initfunction
1270 :initargs initargs
1271 :allocation allocation
1272 :allocation-class allocation-class
1273 :type type
1274 :class class
1275 :documentation documentation)))
1277 (defmethod compute-effective-slot-definition-initargs :around
1278 ((class structure-class) direct-slotds)
1279 (let* ((slotd (car direct-slotds))
1280 (accessor (slot-definition-defstruct-accessor-symbol slotd)))
1281 (list* :defstruct-accessor-symbol accessor
1282 :internal-reader-function
1283 (slot-definition-internal-reader-function slotd)
1284 :internal-writer-function
1285 (slot-definition-internal-writer-function slotd)
1286 (call-next-method))))
1288 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1289 ;;; to make the method object. They have to use make-a-method which
1290 ;;; is a specially bootstrapped mechanism for making standard methods.
1291 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1292 (declare (ignore direct-slot initargs))
1293 (find-class 'standard-reader-method))
1295 (defmethod add-reader-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1296 (add-method generic-function
1297 (make-a-method 'standard-reader-method
1299 (list (or (class-name class) 'object))
1300 (list class)
1301 (make-reader-method-function class slot-name)
1302 (or slot-documentation "automatically generated reader method")
1303 :slot-name slot-name
1304 :object-class class
1305 :method-class-function #'reader-method-class
1306 :definition-source source-location)))
1308 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1309 (declare (ignore direct-slot initargs))
1310 (find-class 'standard-writer-method))
1312 (defmethod add-writer-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1313 (add-method generic-function
1314 (make-a-method 'standard-writer-method
1316 (list 'new-value (or (class-name class) 'object))
1317 (list *the-class-t* class)
1318 (make-writer-method-function class slot-name)
1319 (or slot-documentation "automatically generated writer method")
1320 :slot-name slot-name
1321 :object-class class
1322 :method-class-function #'writer-method-class
1323 :definition-source source-location)))
1325 (defmethod add-boundp-method ((class slot-class) generic-function slot-name slot-documentation source-location)
1326 (add-method generic-function
1327 (make-a-method (constantly (find-class 'standard-boundp-method))
1328 class
1330 (list (or (class-name class) 'object))
1331 (list class)
1332 (make-boundp-method-function class slot-name)
1333 (or slot-documentation "automatically generated boundp method")
1334 :slot-name slot-name
1335 :definition-source source-location)))
1337 (defmethod remove-reader-method ((class slot-class) generic-function)
1338 (let ((method (get-method generic-function () (list class) nil)))
1339 (when method (remove-method generic-function method))))
1341 (defmethod remove-writer-method ((class slot-class) generic-function)
1342 (let ((method
1343 (get-method generic-function () (list *the-class-t* class) nil)))
1344 (when method (remove-method generic-function method))))
1346 (defmethod remove-boundp-method ((class slot-class) generic-function)
1347 (let ((method (get-method generic-function () (list class) nil)))
1348 (when method (remove-method generic-function method))))
1350 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITER-METHOD-FUNCTION
1351 ;;; function are NOT part of the standard protocol. They are however
1352 ;;; useful; PCL makes use of them internally and documents them for
1353 ;;; PCL users. (FIXME: but SBCL certainly doesn't)
1355 ;;; *** This needs work to make type testing by the writer functions which
1356 ;;; *** do type testing faster. The idea would be to have one constructor
1357 ;;; *** for each possible type test.
1359 ;;; *** There is a subtle bug here which is going to have to be fixed.
1360 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1361 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1362 ;;; *** defined for this metaclass a chance to run.
1364 (defmethod make-reader-method-function ((class slot-class) slot-name)
1365 (make-std-reader-method-function class slot-name))
1367 (defmethod make-writer-method-function ((class slot-class) slot-name)
1368 (make-std-writer-method-function class slot-name))
1370 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1371 (make-std-boundp-method-function class slot-name))
1373 (defmethod compatible-meta-class-change-p (class proto-new-class)
1374 (eq (class-of class) (class-of proto-new-class)))
1376 (defmethod validate-superclass ((class class) (superclass class))
1377 (or (eq (class-of class) (class-of superclass))
1378 (and (eq (class-of superclass) *the-class-standard-class*)
1379 (eq (class-of class) *the-class-funcallable-standard-class*))
1380 (and (eq (class-of superclass) *the-class-funcallable-standard-class*)
1381 (eq (class-of class) *the-class-standard-class*))))
1383 ;;; What this does depends on which of the four possible values of
1384 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1385 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1386 ;;; nothing to do, as the new wrapper has already been created. If
1387 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1388 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1389 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1391 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1392 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1393 ;;; invalidated all the subclasses in SB-KERNEL land). Again, here we
1394 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1395 ;;; obsolete the wrapper.
1397 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1398 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1399 ;;; :UNINITIALIZED)))
1401 ;;; Thanks to Gerd Moellmann for the explanation. -- CSR, 2002-10-29
1402 (defun %force-cache-flushes (class)
1403 (with-world-lock ()
1404 (let* ((owrapper (class-wrapper class)))
1405 ;; We only need to do something if the wrapper is still valid. If
1406 ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1407 ;; both of those will already be doing what we want. In
1408 ;; particular, we must be sure we never change an OBSOLETE into a
1409 ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1410 (when (or (not (invalid-wrapper-p owrapper))
1411 ;; KLUDGE: despite the observations above, this remains
1412 ;; a violation of locality or what might be considered
1413 ;; good style. There has to be a better way! -- CSR,
1414 ;; 2002-10-29
1415 (eq (layout-invalid owrapper) t))
1416 (let ((nwrapper (make-wrapper (layout-length owrapper)
1417 class)))
1418 (setf (layout-slot-list nwrapper) (layout-slot-list owrapper))
1419 (setf (layout-slot-table nwrapper) (layout-slot-table owrapper))
1420 (%update-lisp-class-layout class nwrapper)
1421 (setf (slot-value class 'wrapper) nwrapper)
1422 ;; Use :OBSOLETE instead of :FLUSH if any superclass has
1423 ;; been obsoleted.
1424 (if (find-if (lambda (x)
1425 (and (consp x) (eq :obsolete (car x))))
1426 (layout-inherits owrapper)
1427 :key #'layout-invalid)
1428 (%invalidate-wrapper owrapper :obsolete nwrapper)
1429 (%invalidate-wrapper owrapper :flush nwrapper))))))
1430 nil)
1432 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1433 ;;; the next access to the instance (as defined in 88-002R) to trap
1434 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1435 (defmethod make-instances-obsolete ((class std-class))
1436 (with-world-lock ()
1437 (let* ((owrapper (class-wrapper class))
1438 (nwrapper (make-wrapper (layout-length owrapper)
1439 class)))
1440 (unless (class-finalized-p class)
1441 (if (class-has-a-forward-referenced-superclass-p class)
1442 (return-from make-instances-obsolete class)
1443 (%update-cpl class (compute-class-precedence-list class))))
1444 (setf (layout-slot-list nwrapper) (layout-slot-list owrapper))
1445 (setf (layout-slot-table nwrapper) (layout-slot-table owrapper))
1446 (%update-lisp-class-layout class nwrapper)
1447 (setf (slot-value class 'wrapper) nwrapper)
1448 (%invalidate-wrapper owrapper :obsolete nwrapper)
1449 class)))
1451 (defmethod make-instances-obsolete ((class symbol))
1452 (make-instances-obsolete (find-class class))
1453 ;; ANSI wants the class name when called with a symbol.
1454 class)
1456 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1457 ;;; see an obsolete instance. The times when it is called are:
1458 ;;; - when the instance is involved in method lookup
1459 ;;; - when attempting to access a slot of an instance
1461 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1462 ;;; instance access macros.
1464 ;;; Of course these times when it is called are an internal
1465 ;;; implementation detail of PCL and are not part of the documented
1466 ;;; description of when the obsolete instance update happens. The
1467 ;;; documented description is as it appears in 88-002R.
1469 ;;; This has to return the new wrapper, so it counts on all the
1470 ;;; methods on obsolete-instance-trap-internal to return the new
1471 ;;; wrapper. It also does a little internal error checking to make
1472 ;;; sure that the traps are only happening when they should, and that
1473 ;;; the trap methods are computing appropriate new wrappers.
1475 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1476 ;;; after a structure is redefined. In most cases,
1477 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1478 ;;; so it must signal an error. The hard part of this is that the
1479 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1480 ;;; called again, so in that case, we have to return some reasonable
1481 ;;; wrapper, instead.
1483 (defun %ensure-slot-value-type (context slot-name slot-type value
1484 old-class new-class)
1485 (do () ((typep value slot-type))
1486 (restart-case
1487 (bad-type value slot-type
1488 "~@<Error during ~A. Current value in slot ~
1489 ~/sb-impl::print-symbol-with-prefix/ of an instance ~
1490 of ~S is ~S, which does not match the new slot type ~
1491 ~S in class ~S.~:@>"
1492 context slot-name old-class value slot-type new-class)
1493 (use-value (new-value)
1494 :interactive read-evaluated-form
1495 :report (lambda (stream)
1496 (format stream "~@<Specify a new value to by used ~
1497 for slot ~
1498 ~/sb-impl::print-symbol-with-prefix/ ~
1499 instead of ~S.~@:>"
1500 slot-name value))
1501 (setf value new-value))))
1502 value)
1504 (defun %set-slot-value-checking-type (context slots slot value
1505 safe old-class new-class)
1506 (setf (clos-slots-ref slots (slot-definition-location slot))
1507 (if (and safe (neq value +slot-unbound+))
1508 (let ((name (slot-definition-name slot))
1509 (type (slot-definition-type slot)))
1510 (%ensure-slot-value-type context name type value
1511 old-class new-class))
1512 value)))
1514 (defvar *in-obsolete-instance-trap* nil)
1515 (defvar *the-wrapper-of-structure-object*
1516 (class-wrapper (find-class 'structure-object)))
1518 (define-condition obsolete-structure (error)
1519 ((datum :reader obsolete-structure-datum :initarg :datum))
1520 (:report
1521 (lambda (condition stream)
1522 ;; Don't try to print the structure, since it probably won't work.
1523 (format stream
1524 "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1525 (type-of (obsolete-structure-datum condition))))))
1527 (defun %obsolete-instance-trap (owrapper nwrapper instance)
1528 (cond
1529 ((layout-for-std-class-p owrapper)
1530 (binding* ((class (wrapper-class* nwrapper))
1531 (copy (allocate-instance class)) ;??? allocate-instance ???
1532 (oslots (get-slots instance))
1533 (nslots (get-slots copy))
1534 (added ())
1535 (discarded ())
1536 (plist ())
1537 (safe (safe-p class))
1538 ((new-instance-slots nil new-custom-slots)
1539 (classify-slotds (layout-slot-list nwrapper)))
1540 ((old-instance-slots old-class-slots old-custom-slots)
1541 (classify-slotds (layout-slot-list owrapper)))
1542 (layout (mapcar (lambda (slotd)
1543 ;; Get the names only once.
1544 (cons (slot-definition-name slotd) slotd))
1545 new-instance-slots)))
1546 ;; local --> local transfer value, check type
1547 ;; local --> shared discard value, discard slot
1548 ;; local --> -- discard slot
1549 ;; local --> custom XXX
1551 ;; shared --> local transfer value, check type
1552 ;; shared --> shared -- (cf SHARED-INITIALIZE :AFTER STD-CLASS)
1553 ;; shared --> -- discard value
1554 ;; shared --> custom XXX
1556 ;; -- --> local add slot
1557 ;; -- --> shared --
1558 ;; -- --> custom XXX
1559 (flet ((set-value (value cell)
1560 (%set-slot-value-checking-type
1561 "updating obsolete instance"
1562 nslots (cdr cell) value safe class class)
1563 ;; Prune from the list now that it's been dealt with.
1564 (setf layout (remove cell layout))))
1566 ;; Go through all the old local slots.
1567 (dolist (old old-instance-slots)
1568 (let* ((name (slot-definition-name old))
1569 (value (clos-slots-ref oslots (slot-definition-location old))))
1570 (unless (eq value +slot-unbound+)
1571 (let ((new (assq name layout)))
1572 (cond (new
1573 (set-value value new))
1575 (push name discarded)
1576 (setf (getf plist name) value)))))))
1578 ;; Go through all the old shared slots.
1579 (dolist (old old-class-slots)
1580 (binding* ((cell (slot-definition-location old))
1581 (name (car cell))
1582 (new (assq name layout) :exit-if-null))
1583 (set-value (cdr cell) new)))
1585 ;; Go through all custom slots to find added ones. CLHS
1586 ;; doesn't specify what to do about them, and neither does
1587 ;; AMOP. We do want them to get initialized, though, so we
1588 ;; list them in ADDED for the benefit of SHARED-INITIALIZE.
1589 (dolist (new new-custom-slots)
1590 (let* ((name (slot-definition-name new))
1591 (old (find name old-custom-slots
1592 :key #'slot-definition-name)))
1593 (unless old
1594 (push name added))))
1596 ;; Go through all the remaining new local slots to compute
1597 ;; the added slots.
1598 (dolist (cell layout)
1599 (push (car cell) added)))
1601 (%swap-wrappers-and-slots instance copy)
1603 (update-instance-for-redefined-class
1604 instance added discarded plist)
1606 nwrapper))
1607 (*in-obsolete-instance-trap*
1608 *the-wrapper-of-structure-object*)
1610 (let ((*in-obsolete-instance-trap* t))
1611 (error 'obsolete-structure :datum instance)))))
1614 (defun %change-class (instance new-class initargs)
1615 (binding* ((old-class (class-of instance))
1616 (copy (allocate-instance new-class))
1617 (new-wrapper (get-wrapper copy))
1618 (old-wrapper (class-wrapper old-class))
1619 (old-slots (get-slots instance))
1620 (new-slots (get-slots copy))
1621 (safe (safe-p new-class))
1622 (new-instance-slots
1623 (classify-slotds (layout-slot-list new-wrapper)))
1624 ((old-instance-slots old-class-slots)
1625 (classify-slotds (layout-slot-list old-wrapper))))
1626 (labels ((find-slot (name slots)
1627 (find name slots :key #'slot-definition-name))
1628 (initarg-for-slot-p (slot)
1629 (dolist (slot-initarg (slot-definition-initargs slot))
1630 ;; Abuse +slot-unbound+
1631 (unless (eq +slot-unbound+
1632 (getf initargs slot-initarg +slot-unbound+))
1633 (return t))))
1634 (set-value (value slotd)
1635 (%set-slot-value-checking-type
1636 'change-class new-slots slotd value safe
1637 old-class new-class)))
1639 ;; "The values of local slots specified by both the class CTO
1640 ;; and CFROM are retained. If such a local slot was unbound, it
1641 ;; remains unbound."
1642 (dolist (new new-instance-slots)
1643 (unless (initarg-for-slot-p new)
1644 (binding* ((old (find-slot (slot-definition-name new) old-instance-slots)
1645 :exit-if-null)
1646 (value (clos-slots-ref old-slots (slot-definition-location old))))
1647 (set-value value new))))
1649 ;; "The values of slots specified as shared in the class CFROM and
1650 ;; as local in the class CTO are retained."
1651 (dolist (old old-class-slots)
1652 (binding* ((slot-and-val (slot-definition-location old))
1653 (new (find-slot (car slot-and-val) new-instance-slots)
1654 :exit-if-null))
1655 (set-value (cdr slot-and-val) new))))
1657 ;; Make the copy point to the old instance's storage, and make the
1658 ;; old instance point to the new storage.
1659 (%swap-wrappers-and-slots instance copy)
1661 (apply #'update-instance-for-different-class copy instance initargs)
1663 instance))
1665 (defun check-new-class-not-metaobject (new-class)
1666 (dolist (class (class-precedence-list
1667 (ensure-class-finalized new-class)))
1668 (macrolet
1669 ((check-metaobject (class-name)
1670 `(when (eq class (find-class ',class-name))
1671 (change-class-to-metaobject-violation
1672 ',class-name nil '((:amop :initialization ,class-name))))))
1673 (check-metaobject class)
1674 (check-metaobject generic-function)
1675 (check-metaobject method)
1676 (check-metaobject slot-definition))))
1678 (defmethod change-class ((instance standard-object) (new-class standard-class)
1679 &rest initargs)
1680 (with-world-lock ()
1681 (check-new-class-not-metaobject new-class)
1682 (%change-class instance new-class initargs)))
1684 (defmethod change-class ((instance forward-referenced-class)
1685 (new-class standard-class) &rest initargs)
1686 (with-world-lock ()
1687 (dolist (class (class-precedence-list
1688 (ensure-class-finalized new-class))
1689 (change-class-to-metaobject-violation
1690 '(not class) 'forward-referenced-class
1691 '((:amop :generic-function ensure-class-using-class)
1692 (:amop :initialization class))))
1693 (when (eq class (find-class 'class))
1694 (return nil)))
1695 (%change-class instance new-class initargs)))
1697 (defmethod change-class ((instance t)
1698 (new-class forward-referenced-class) &rest initargs)
1699 (declare (ignore initargs))
1700 (change-class-to-metaobject-violation
1701 'forward-referenced-class nil
1702 '((:amop :generic-function ensure-class-using-class)
1703 (:amop :initialization class))))
1705 (defmethod change-class ((instance funcallable-standard-object)
1706 (new-class funcallable-standard-class)
1707 &rest initargs)
1708 (with-world-lock ()
1709 (check-new-class-not-metaobject new-class)
1710 (%change-class instance new-class initargs)))
1712 (defmethod change-class ((instance standard-object)
1713 (new-class funcallable-standard-class)
1714 &rest initargs)
1715 (declare (ignore initargs))
1716 (error "You can't change the class of ~S to ~S~@
1717 because it isn't already an instance with metaclass ~S."
1718 instance new-class 'standard-class))
1720 (defmethod change-class ((instance funcallable-standard-object)
1721 (new-class standard-class)
1722 &rest initargs)
1723 (declare (ignore initargs))
1724 (error "You can't change the class of ~S to ~S~@
1725 because it isn't already an instance with metaclass ~S."
1726 instance new-class 'funcallable-standard-class))
1728 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1729 (apply #'change-class instance (find-class new-class-name) initargs))
1731 ;;;; The metaclasses SYSTEM-CLASS and BUILT-IN-CLASS
1732 ;;;;
1733 ;;;; These metaclasses are something of a weird creature. By this
1734 ;;;; point, all instances which will exist have been created, and no
1735 ;;;; instance is ever created by calling MAKE-INSTANCE. (The
1736 ;;;; distinction between the metaclasses is that we allow subclassing
1737 ;;;; of SYSTEM-CLASS, such as through STREAM and SEQUENCE protocols,
1738 ;;;; but not of BUILT-IN-CLASS.)
1739 ;;;;
1740 ;;;; AMOP mandates some behaviour of the implementation with respect
1741 ;;;; to BUILT-IN-CLASSes, and we implement that through methods on
1742 ;;;; SYSTEM-CLASS here.
1744 (macrolet ((def (name args control)
1745 `(defmethod ,name ,args
1746 (declare (ignore initargs))
1747 (error 'metaobject-initialization-violation
1748 :format-control ,(format nil "~@<~A~@:>" control)
1749 :format-arguments (list (class-name class))
1750 :references (list '(:amop :initialization "Class"))))))
1751 (def initialize-instance ((class system-class) &rest initargs)
1752 "Cannot initialize an instance of ~S.")
1753 (def reinitialize-instance ((class system-class) &rest initargs)
1754 "Cannot reinitialize an instance of ~S."))
1756 (macrolet ((def (name) `(defmethod ,name ((class system-class)) nil)))
1757 (def class-direct-slots)
1758 (def class-slots)
1759 (def class-direct-default-initargs)
1760 (def class-default-initargs))
1762 (defmethod validate-superclass ((c class) (s system-class))
1764 (defmethod validate-superclass ((c class) (s built-in-class))
1765 nil)
1767 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1768 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1769 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1770 (macrolet ((def (method)
1771 `(defmethod ,method ((class forward-referenced-class))
1772 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1773 ',method class))))
1774 (def class-default-initargs)
1775 (def class-precedence-list)
1776 (def class-slots))
1778 (defmethod validate-superclass ((c slot-class) (f forward-referenced-class))
1781 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1782 (pushnew dependent (plist-value metaobject 'dependents) :test #'eq))
1784 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1785 (setf (plist-value metaobject 'dependents)
1786 (delete dependent (plist-value metaobject 'dependents))))
1788 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1789 (dolist (dependent (plist-value metaobject 'dependents))
1790 (funcall function dependent)))