0.pre8.65:
[sbcl.git] / src / pcl / std-class.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3
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.
9
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.
23
24 (in-package "SB-PCL")
25 \f
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))))
31
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))))
39
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)
44
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))))))))
56
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)
71
72 (defmethod initialize-internal-slot-functions ((slotd
73                                                 effective-slot-definition))
74   (let* ((name (slot-value slotd 'name))
75          (class (slot-value slotd 'class)))
76     (let ((table (or (gethash name *name->class->slotd-table*)
77                      (setf (gethash name *name->class->slotd-table*)
78                            (make-hash-table :test 'eq :size 5)))))
79       (setf (gethash class table) slotd))
80     (dolist (type '(reader writer boundp))
81       (let* ((gf-name (ecase type
82                               (reader 'slot-value-using-class)
83                               (writer '(setf slot-value-using-class))
84                               (boundp 'slot-boundp-using-class)))
85              (gf (gdefinition gf-name)))
86         (compute-slot-accessor-info slotd type gf)))
87     (initialize-internal-slot-gfs name)))
88
89 (defmethod compute-slot-accessor-info ((slotd effective-slot-definition)
90                                        type gf)
91   (let* ((name (slot-value slotd 'name))
92          (class (slot-value slotd 'class))
93          (old-slotd (find-slot-definition class name))
94          (old-std-p (and old-slotd (slot-accessor-std-p old-slotd 'all))))
95     (multiple-value-bind (function std-p)
96         (if (eq *boot-state* 'complete)
97             (get-accessor-method-function gf type class slotd)
98             (get-optimized-std-accessor-method-function class slotd type))
99       (setf (slot-accessor-std-p slotd type) std-p)
100       (setf (slot-accessor-function slotd type) function))
101     (when (and old-slotd (not (eq old-std-p (slot-accessor-std-p slotd 'all))))
102       (push (cons class name) *pv-table-cache-update-info*))))
103
104 (defmethod slot-definition-allocation ((slotd structure-slot-definition))
105   :instance)
106 \f
107 (defmethod shared-initialize :after ((object documentation-mixin)
108                                      slot-names
109                                      &key (documentation nil documentation-p))
110   (declare (ignore slot-names))
111   (when documentation-p
112     (setf (plist-value object 'documentation) documentation)))
113
114 ;;; default if DOC-TYPE doesn't match one of the specified types
115 (defmethod documentation (object doc-type)
116   (warn "unsupported DOCUMENTATION: type ~S for object ~S"
117         doc-type
118         (type-of object))
119   nil)
120
121 ;;; default if DOC-TYPE doesn't match one of the specified types
122 (defmethod (setf documentation) (new-value object doc-type)
123   ;; CMU CL made this an error, but since ANSI says that even for supported
124   ;; doc types an implementation is permitted to discard docs at any time
125   ;; for any reason, this feels to me more like a warning. -- WHN 19991214
126   (warn "discarding unsupported DOCUMENTATION of type ~S for object ~S"
127         doc-type
128         (type-of object))
129   new-value)
130
131 (defmethod documentation ((object documentation-mixin) doc-type)
132   (declare (ignore doc-type))
133   (plist-value object 'documentation))
134
135 (defmethod (setf documentation) (new-value
136                                  (object documentation-mixin)
137                                  doc-type)
138   (declare (ignore doc-type))
139   (setf (plist-value object 'documentation) new-value))
140
141 (defmethod documentation ((slotd standard-slot-definition) doc-type)
142   (declare (ignore doc-type))
143   (slot-value slotd 'documentation))
144
145 (defmethod (setf documentation) (new-value
146                                  (slotd standard-slot-definition)
147                                  doc-type)
148   (declare (ignore doc-type))
149   (setf (slot-value slotd 'documentation) new-value))
150 \f
151 ;;;; various class accessors that are a little more complicated than can be
152 ;;;; done with automatically generated reader methods
153
154 (defmethod class-finalized-p ((class pcl-class))
155   (with-slots (wrapper) class
156     (not (null wrapper))))
157
158 (defmethod class-prototype ((class std-class))
159   (with-slots (prototype) class
160     (or prototype (setq prototype (allocate-instance class)))))
161
162 (defmethod class-prototype ((class structure-class))
163   (with-slots (prototype wrapper defstruct-constructor) class
164     (or prototype
165         (setq prototype
166               (if defstruct-constructor
167                   (allocate-instance class)
168                   (allocate-standard-instance wrapper))))))
169
170 (defmethod class-direct-default-initargs ((class slot-class))
171   (plist-value class 'direct-default-initargs))
172
173 (defmethod class-default-initargs ((class slot-class))
174   (plist-value class 'default-initargs))
175
176 (defmethod class-slot-cells ((class std-class))
177   (plist-value class 'class-slot-cells))
178 \f
179 ;;;; class accessors that are even a little bit more complicated than those
180 ;;;; above. These have a protocol for updating them, we must implement that
181 ;;;; protocol.
182
183 ;;; Maintaining the direct subclasses backpointers. The update methods are
184 ;;; here, the values are read by an automatically generated reader method.
185 (defmethod add-direct-subclass ((class class) (subclass class))
186   (with-slots (direct-subclasses) class
187     (pushnew subclass direct-subclasses)
188     subclass))
189 (defmethod remove-direct-subclass ((class class) (subclass class))
190   (with-slots (direct-subclasses) class
191     (setq direct-subclasses (remove subclass direct-subclasses))
192     subclass))
193
194 ;;; Maintaining the direct-methods and direct-generic-functions backpointers.
195 ;;;
196 ;;; There are four generic functions involved, each has one method for the
197 ;;; class case and another method for the damned EQL specializers. All of
198 ;;; these are specified methods and appear in their specified place in the
199 ;;; class graph.
200 ;;;
201 ;;;   ADD-DIRECT-METHOD
202 ;;;   REMOVE-DIRECT-METHOD
203 ;;;   SPECIALIZER-DIRECT-METHODS
204 ;;;   SPECIALIZER-DIRECT-GENERIC-FUNCTIONS
205 ;;;
206 ;;; In each case, we maintain one value which is a cons. The car is the list
207 ;;; methods. The cdr is a list of the generic functions. The cdr is always
208 ;;; computed lazily.
209 (defmethod add-direct-method ((specializer class) (method method))
210   (with-slots (direct-methods) specializer
211     (setf (car direct-methods) (adjoin method (car direct-methods))     ;PUSH
212           (cdr direct-methods) ()))
213   method)
214 (defmethod remove-direct-method ((specializer class) (method method))
215   (with-slots (direct-methods) specializer
216     (setf (car direct-methods) (remove method (car direct-methods))
217           (cdr direct-methods) ()))
218   method)
219
220 (defmethod specializer-direct-methods ((specializer class))
221   (with-slots (direct-methods) specializer
222     (car direct-methods)))
223
224 (defmethod specializer-direct-generic-functions ((specializer class))
225   (with-slots (direct-methods) specializer
226     (or (cdr direct-methods)
227         (setf (cdr direct-methods)
228               (let (collect)
229                 (dolist (m (car direct-methods))
230                   ;; the old PCL code used COLLECTING-ONCE which used
231                   ;; #'EQ to check for newness
232                   (pushnew (method-generic-function m) collect :test #'eq))
233                 (nreverse collect))))))
234 \f
235 ;;; This hash table is used to store the direct methods and direct generic
236 ;;; functions of EQL specializers. Each value in the table is the cons.
237 (defvar *eql-specializer-methods* (make-hash-table :test 'eql))
238 (defvar *class-eq-specializer-methods* (make-hash-table :test 'eq))
239
240 (defmethod specializer-method-table ((specializer eql-specializer))
241   *eql-specializer-methods*)
242
243 (defmethod specializer-method-table ((specializer class-eq-specializer))
244   *class-eq-specializer-methods*)
245
246 (defmethod add-direct-method ((specializer specializer-with-object)
247                               (method method))
248   (let* ((object (specializer-object specializer))
249          (table (specializer-method-table specializer))
250          (entry (gethash object table)))
251     (unless entry
252       (setq entry
253             (setf (gethash object table)
254                   (cons nil nil))))
255     (setf (car entry) (adjoin method (car entry))
256           (cdr entry) ())
257     method))
258
259 (defmethod remove-direct-method ((specializer specializer-with-object)
260                                  (method method))
261   (let* ((object (specializer-object specializer))
262          (entry (gethash object (specializer-method-table specializer))))
263     (when entry
264       (setf (car entry) (remove method (car entry))
265             (cdr entry) ()))
266     method))
267
268 (defmethod specializer-direct-methods ((specializer specializer-with-object))
269   (car (gethash (specializer-object specializer)
270                 (specializer-method-table specializer))))
271
272 (defmethod specializer-direct-generic-functions ((specializer
273                                                   specializer-with-object))
274   (let* ((object (specializer-object specializer))
275          (entry (gethash object (specializer-method-table specializer))))
276     (when entry
277       (or (cdr entry)
278           (setf (cdr entry)
279                 (let (collect)
280                   (dolist (m (car entry))
281                     (pushnew (method-generic-function m) collect :test #'eq))
282                   (nreverse collect)))))))
283
284 (defun map-specializers (function)
285   (map-all-classes (lambda (class)
286                      (funcall function (class-eq-specializer class))
287                      (funcall function class)))
288   (maphash (lambda (object methods)
289              (declare (ignore methods))
290              (intern-eql-specializer object))
291            *eql-specializer-methods*)
292   (maphash (lambda (object specl)
293              (declare (ignore object))
294              (funcall function specl))
295            *eql-specializer-table*)
296   nil)
297
298 (defun map-all-generic-functions (function)
299   (let ((all-generic-functions (make-hash-table :test 'eq)))
300     (map-specializers (lambda (specl)
301                         (dolist (gf (specializer-direct-generic-functions
302                                      specl))
303                           (unless (gethash gf all-generic-functions)
304                             (setf (gethash gf all-generic-functions) t)
305                             (funcall function gf))))))
306   nil)
307
308 (defmethod shared-initialize :after ((specl class-eq-specializer)
309                                      slot-names
310                                      &key)
311   (declare (ignore slot-names))
312   (setf (slot-value specl 'type) `(class-eq ,(specializer-class specl))))
313
314 (defmethod shared-initialize :after ((specl eql-specializer) slot-names &key)
315   (declare (ignore slot-names))
316   (setf (slot-value specl 'type) `(eql ,(specializer-object specl))))
317 \f
318 (defun real-load-defclass (name metaclass-name supers slots other)
319   (let ((res (apply #'ensure-class name :metaclass metaclass-name
320                     :direct-superclasses supers
321                     :direct-slots slots
322                     :definition-source `((defclass ,name)
323                                          ,*load-pathname*)
324                     other)))
325     res))
326
327 (setf (gdefinition 'load-defclass) #'real-load-defclass)
328
329 (defun ensure-class (name &rest all)
330   (apply #'ensure-class-using-class (find-class name nil) name all))
331
332 (defmethod ensure-class-using-class ((class null) name &rest args &key)
333   (multiple-value-bind (meta initargs)
334       (ensure-class-values class args)
335     (set-class-type-translation (class-prototype meta) name)
336     (setf class (apply #'make-instance meta :name name initargs)
337           (find-class name) class)
338     (set-class-type-translation class name)
339     class))
340
341 (defmethod ensure-class-using-class ((class pcl-class) name &rest args &key)
342   (multiple-value-bind (meta initargs)
343       (ensure-class-values class args)
344     (unless (eq (class-of class) meta)
345       (apply #'change-class class meta initargs))
346     (apply #'reinitialize-instance class initargs)
347     (setf (find-class name) class)
348     (set-class-type-translation class name)
349     class))
350
351 (defmethod class-predicate-name ((class t))
352   'constantly-nil)
353
354 (defun fix-super (s)
355   (cond ((classp s) s)
356         ((not (legal-class-name-p s))
357           (error "~S is not a class or a legal class name." s))
358         (t
359           (or (find-class s nil)
360               (setf (find-class s)
361                       (make-instance 'forward-referenced-class
362                                      :name s))))))
363
364 (defun ensure-class-values (class args)
365   (let* ((initargs (copy-list args))
366          (unsupplied (list 1))
367          (supplied-meta   (getf initargs :metaclass unsupplied))
368          (supplied-supers (getf initargs :direct-superclasses unsupplied))
369          (supplied-slots  (getf initargs :direct-slots unsupplied))
370          (meta
371            (cond ((neq supplied-meta unsupplied)
372                   (find-class supplied-meta))
373                  ((or (null class)
374                       (forward-referenced-class-p class))
375                   *the-class-standard-class*)
376                  (t
377                   (class-of class)))))
378     ;; KLUDGE: It seemed to me initially that there ought to be a way
379     ;; of collecting all the erroneous problems in one go, rather than
380     ;; this way of solving the problem of signalling the errors that
381     ;; we are required to, which stops at the first bogus input.
382     ;; However, after playing around a little, I couldn't find that
383     ;; way, so I've left it as is, but if someone does come up with a
384     ;; better way... -- CSR, 2002-09-08
385     (do ((direct-slots (getf initargs :direct-slots) (cdr direct-slots)))
386         ((endp direct-slots) nil)
387       (destructuring-bind (slot &rest more) direct-slots
388         (let ((slot-name (getf slot :name)))
389           (when (some (lambda (s) (eq slot-name (getf s :name))) more)
390             ;; FIXME: It's quite possible that we ought to define an
391             ;; SB-INT:PROGRAM-ERROR function to signal these and other
392             ;; errors throughout the codebase that are required to be
393             ;; of type PROGRAM-ERROR.
394             (error 'simple-program-error
395                    :format-control "~@<There is more than one direct slot ~
396                                    with name ~S.~:>"
397                    :format-arguments (list slot-name)))
398           (do ((stuff slot (cddr stuff)))
399               ((endp stuff) nil)
400             (destructuring-bind (option value &rest more) stuff
401               (cond
402                 ((and (member option '(:allocation :type
403                                        :initform :documentation))
404                       (not (eq unsupplied
405                                (getf more option unsupplied))))
406                  (error 'simple-program-error
407                         :format-control "~@<Duplicate slot option ~S for ~
408                                         slot named ~S.~:>"
409                         :format-arguments (list option slot-name)))
410                 ((and (eq option :readers)
411                       (notevery #'symbolp value))
412                  (error 'simple-program-error
413                         :format-control "~@<Slot reader names for slot ~
414                                         named ~S must be symbols.~:>"
415                         :format-arguments (list slot-name)))
416                 ((and (eq option :initargs)
417                       (notevery #'symbolp value))
418                  (error 'simple-program-error
419                         :format-control "~@<Slot initarg names for slot ~
420                                         named ~S must be symbols.~:>"
421                         :format-arguments (list slot-name)))))))))
422     (loop for (initarg . more) on (getf initargs :direct-default-initargs)
423           for name = (car initarg) 
424           when (some (lambda (a) (eq (car a) name)) more) 
425           do (error 'simple-program-error 
426                     :format-control "~@<Duplicate initialization argument ~
427                                     name ~S in :DEFAULT-INITARGS.~:>"
428                     :format-arguments (list name class)))
429     (let ((metaclass 0)
430           (default-initargs 0))
431       (do ((args initargs (cddr args)))
432           ((endp args) nil)
433         (case (car args)
434           (:metaclass
435            (when (> (incf metaclass) 1)
436              (error 'simple-program-error
437                     :format-control "~@<More than one :METACLASS ~
438                                     option specified.~:>")))
439           (:direct-default-initargs
440            (when (> (incf default-initargs) 1)
441              (error 'simple-program-error
442                     :format-control "~@<More than one :DEFAULT-INITARGS ~
443                                     option specified.~:>"))))))
444     (remf initargs :metaclass)
445     (loop (unless (remf initargs :direct-superclasses) (return)))
446     (loop (unless (remf initargs :direct-slots) (return)))
447     (values
448      meta
449      (nconc
450       (when (neq supplied-supers unsupplied)
451         (list :direct-superclasses (mapcar #'fix-super supplied-supers)))
452       (when (neq supplied-slots unsupplied)
453         (list :direct-slots supplied-slots))
454       initargs))))
455 \f
456 (defmethod shared-initialize :after
457            ((class std-class)
458             slot-names
459             &key (direct-superclasses nil direct-superclasses-p)
460                  (direct-slots nil direct-slots-p)
461                  (direct-default-initargs nil direct-default-initargs-p)
462                  (predicate-name nil predicate-name-p))
463   (declare (ignore slot-names))
464   (cond (direct-superclasses-p
465          (setq direct-superclasses
466                (or direct-superclasses
467                    (list (if (funcallable-standard-class-p class)
468                              *the-class-funcallable-standard-object*
469                              *the-class-standard-object*))))
470          (dolist (superclass direct-superclasses)
471            (unless (validate-superclass class superclass)
472              (error "The class ~S was specified as a~%
473                      super-class of the class ~S;~%~
474                      but the meta-classes ~S and~%~S are incompatible.~@
475                      Define a method for ~S to avoid this error."
476                      superclass class (class-of superclass) (class-of class)
477                      'validate-superclass)))
478          (setf (slot-value class 'direct-superclasses) direct-superclasses))
479         (t
480          (setq direct-superclasses (slot-value class 'direct-superclasses))))
481   (setq direct-slots
482         (if direct-slots-p
483             (setf (slot-value class 'direct-slots)
484                   (mapcar (lambda (pl) (make-direct-slotd class pl))
485                           direct-slots))
486             (slot-value class 'direct-slots)))
487   (if direct-default-initargs-p
488       (setf (plist-value class 'direct-default-initargs)
489             direct-default-initargs)
490       (setq direct-default-initargs
491             (plist-value class 'direct-default-initargs)))
492   (setf (plist-value class 'class-slot-cells)
493         (let (collect)
494           (dolist (dslotd direct-slots)
495             (when (eq :class (slot-definition-allocation dslotd))
496               (let ((initfunction (slot-definition-initfunction dslotd)))
497                 (push (cons (slot-definition-name dslotd)
498                                (if initfunction
499                                    (funcall initfunction)
500                                    +slot-unbound+))
501                       collect))))
502           (nreverse collect)))
503   (setq predicate-name (if predicate-name-p
504                            (setf (slot-value class 'predicate-name)
505                                  (car predicate-name))
506                            (or (slot-value class 'predicate-name)
507                                (setf (slot-value class 'predicate-name)
508                                      (make-class-predicate-name (class-name
509                                                                  class))))))
510   (add-direct-subclasses class direct-superclasses)
511   (make-class-predicate class predicate-name)
512   (update-class class nil)
513   (add-slot-accessors class direct-slots))
514
515 (defmethod shared-initialize :before ((class class) slot-names &key name)
516   (declare (ignore slot-names name))
517   ;; FIXME: Could this just be CLASS instead of `(CLASS ,CLASS)? If not,
518   ;; why not? (See also similar expression in !BOOTSTRAP-INITIALIZE-CLASS.)
519   (setf (slot-value class 'type) `(class ,class))
520   (setf (slot-value class 'class-eq-specializer)
521         (make-instance 'class-eq-specializer :class class)))
522
523 (defmethod reinitialize-instance :before ((class slot-class) &key)
524   (remove-direct-subclasses class (class-direct-superclasses class))
525   (remove-slot-accessors    class (class-direct-slots class)))
526
527 (defmethod reinitialize-instance :after ((class slot-class)
528                                          &rest initargs
529                                          &key)
530   (map-dependents class
531                   (lambda (dependent)
532                     (apply #'update-dependent class dependent initargs))))
533
534 (defmethod shared-initialize :after ((class condition-class) slot-names
535                                      &key direct-superclasses)
536   (declare (ignore slot-names))
537   (let ((classoid (find-classoid (class-name class))))
538     (with-slots (wrapper class-precedence-list prototype predicate-name
539                          (direct-supers direct-superclasses))
540         class
541       (setf (classoid-pcl-class classoid) class)
542       (setq direct-supers direct-superclasses)
543       (setq wrapper (classoid-layout classoid))
544       (setq class-precedence-list (compute-class-precedence-list class))
545       (setq prototype (make-condition (class-name class)))
546       (add-direct-subclasses class direct-superclasses)
547       (setq predicate-name (make-class-predicate-name (class-name class)))
548       (make-class-predicate class predicate-name))))
549
550 (defmethod shared-initialize :after
551     ((slotd structure-slot-definition) slot-names &key
552      (allocation :instance) allocation-class)
553   (declare (ignore slot-names allocation-class))
554   (unless (eq allocation :instance)
555     (error "Structure slots must have :INSTANCE allocation.")))
556
557 (defun make-structure-class-defstruct-form (name direct-slots include)
558   (let* ((conc-name (intern (format nil "~S structure class " name)))
559          (constructor (intern (format nil "~Aconstructor" conc-name)))
560          (defstruct `(defstruct (,name
561                                  ,@(when include
562                                          `((:include ,(class-name include))))
563                                  (:predicate nil)
564                                  (:conc-name ,conc-name)
565                                  (:constructor ,constructor ())
566                                  (:copier nil))
567                       ,@(mapcar (lambda (slot)
568                                   `(,(slot-definition-name slot)
569                                     +slot-unbound+))
570                                 direct-slots)))
571          (reader-names (mapcar (lambda (slotd)
572                                  (list 'slot-accessor name
573                                        (slot-definition-name slotd)
574                                        'reader))
575                                direct-slots))
576          (writer-names (mapcar (lambda (slotd)
577                                  (list 'slot-accessor name
578                                        (slot-definition-name slotd)
579                                        'writer))
580                                direct-slots))
581          (readers-init
582            (mapcar (lambda (slotd reader-name)
583                      (let ((accessor
584                              (slot-definition-defstruct-accessor-symbol
585                               slotd)))
586                        `(defun ,reader-name (obj)
587                          (declare (type ,name obj))
588                          (,accessor obj))))
589                    direct-slots reader-names))
590          (writers-init
591            (mapcar (lambda (slotd writer-name)
592                      (let ((accessor
593                              (slot-definition-defstruct-accessor-symbol
594                               slotd)))
595                        `(defun ,writer-name (nv obj)
596                          (declare (type ,name obj))
597                          (setf (,accessor obj) nv))))
598                    direct-slots writer-names))
599          (defstruct-form
600              `(progn
601                ,defstruct
602                ,@readers-init ,@writers-init
603                (cons nil nil))))
604     (values defstruct-form constructor reader-names writer-names)))
605
606 (defmethod shared-initialize :after
607       ((class structure-class)
608        slot-names
609        &key (direct-superclasses nil direct-superclasses-p)
610             (direct-slots nil direct-slots-p)
611             direct-default-initargs
612             (predicate-name nil predicate-name-p))
613   (declare (ignore slot-names direct-default-initargs))
614   (if direct-superclasses-p
615       (setf (slot-value class 'direct-superclasses)
616             (or direct-superclasses
617                 (setq direct-superclasses
618                       (and (not (eq (class-name class) 'structure-object))
619                            (list *the-class-structure-object*)))))
620       (setq direct-superclasses (slot-value class 'direct-superclasses)))
621   (let* ((name (class-name class))
622          (from-defclass-p (slot-value class 'from-defclass-p))
623          (defstruct-p (or from-defclass-p (not (structure-type-p name)))))
624     (if direct-slots-p
625         (setf (slot-value class 'direct-slots)
626               (setq direct-slots
627                     (mapcar (lambda (pl)
628                               (when defstruct-p
629                                 (let* ((slot-name (getf pl :name))
630                                        (acc-name
631                                         (format nil
632                                                 "~S structure class ~A"
633                                                 name slot-name))
634                                        (accessor (intern acc-name)))
635                                   (setq pl (list* :defstruct-accessor-symbol
636                                                   accessor pl))))
637                               (make-direct-slotd class pl))
638                             direct-slots)))
639         (setq direct-slots (slot-value class 'direct-slots)))
640     (when defstruct-p
641       (let ((include (car (slot-value class 'direct-superclasses))))
642         (multiple-value-bind (defstruct-form constructor reader-names writer-names)
643             (make-structure-class-defstruct-form name direct-slots include)
644           (unless (structure-type-p name) (eval defstruct-form))
645           (mapc (lambda (dslotd reader-name writer-name)
646                   (let* ((reader (gdefinition reader-name))
647                          (writer (when (gboundp writer-name)
648                                    (gdefinition writer-name))))
649                     (setf (slot-value dslotd 'internal-reader-function)
650                           reader)
651                     (setf (slot-value dslotd 'internal-writer-function)
652                           writer)))
653                 direct-slots reader-names writer-names)
654           (setf (slot-value class 'defstruct-form) defstruct-form)
655           (setf (slot-value class 'defstruct-constructor) constructor))))
656     (add-direct-subclasses class direct-superclasses)
657     (setf (slot-value class 'class-precedence-list)
658             (compute-class-precedence-list class))
659     (setf (slot-value class 'slots) (compute-slots class))
660     (let ((lclass (find-classoid (class-name class))))
661       (setf (classoid-pcl-class lclass) class)
662       (setf (slot-value class 'wrapper) (classoid-layout lclass)))
663     (update-pv-table-cache-info class)
664     (setq predicate-name (if predicate-name-p
665                            (setf (slot-value class 'predicate-name)
666                                    (car predicate-name))
667                            (or (slot-value class 'predicate-name)
668                                (setf (slot-value class 'predicate-name)
669                                        (make-class-predicate-name
670                                         (class-name class))))))
671     (make-class-predicate class predicate-name)
672     (add-slot-accessors class direct-slots)))
673
674 (defmethod direct-slot-definition-class ((class structure-class) initargs)
675   (declare (ignore initargs))
676   (find-class 'structure-direct-slot-definition))
677
678 (defmethod finalize-inheritance ((class structure-class))
679   nil) ; always finalized
680 \f
681 (defun add-slot-accessors (class dslotds)
682   (fix-slot-accessors class dslotds 'add))
683
684 (defun remove-slot-accessors (class dslotds)
685   (fix-slot-accessors class dslotds 'remove))
686
687 (defun fix-slot-accessors (class dslotds add/remove)
688   (flet ((fix (gfspec name r/w)
689            (let ((gf (ensure-generic-function gfspec)))
690              (case r/w
691                (r (if (eq add/remove 'add)
692                       (add-reader-method class gf name)
693                       (remove-reader-method class gf)))
694                (w (if (eq add/remove 'add)
695                       (add-writer-method class gf name)
696                       (remove-writer-method class gf)))))))
697     (dolist (dslotd dslotds)
698       (let ((slot-name (slot-definition-name dslotd)))
699         (dolist (r (slot-definition-readers dslotd)) (fix r slot-name 'r))
700         (dolist (w (slot-definition-writers dslotd)) (fix w slot-name 'w))))))
701 \f
702 (defun add-direct-subclasses (class supers)
703   (dolist (super supers)
704     (unless (memq class (class-direct-subclasses class))
705       (add-direct-subclass super class))))
706
707 (defun remove-direct-subclasses (class supers)
708   (let ((old (class-direct-superclasses class)))
709     (dolist (o (set-difference old supers))
710       (remove-direct-subclass o class))))
711 \f
712 (defmethod finalize-inheritance ((class std-class))
713   (update-class class t))
714
715 (defmethod finalize-inheritance ((class forward-referenced-class))
716   ;; FIXME: should we not be thinking a bit about what kinds of error
717   ;; we're throwing?  Maybe we need a clos-error type to mix in?  Or
718   ;; possibly a forward-referenced-class-error, though that's
719   ;; difficult given e.g. class precedence list calculations...
720   (error
721    "~@<FINALIZE-INHERITANCE was called on a forward referenced class:~
722        ~2I~_~S~:>"
723    class))
724
725 \f
726 (defun class-has-a-forward-referenced-superclass-p (class)
727   (or (forward-referenced-class-p class)
728       (some #'class-has-a-forward-referenced-superclass-p
729             (class-direct-superclasses class))))
730
731 ;;; This is called by :after shared-initialize whenever a class is initialized
732 ;;; or reinitialized. The class may or may not be finalized.
733 (defun update-class (class finalizep)
734   ;; Comment from Gerd Moellmann:
735   ;;
736   ;; Note that we can't simply delay the finalization when CLASS has
737   ;; no forward referenced superclasses because that causes bootstrap
738   ;; problems.
739   (when (and (not finalizep)
740              (not (class-finalized-p class))
741              (not (class-has-a-forward-referenced-superclass-p class)))
742     (finalize-inheritance class)
743     (return-from update-class))
744   (when (or finalizep (class-finalized-p class)
745             (not (class-has-a-forward-referenced-superclass-p class)))
746     (setf (find-class (class-name class)) class)
747     (update-cpl class (compute-class-precedence-list class))
748     ;; This invocation of UPDATE-SLOTS, in practice, finalizes the
749     ;; class.  The hoops above are to ensure that FINALIZE-INHERITANCE
750     ;; is called at finalization, so that MOP programmers can hook
751     ;; into the system as described in "Class Finalization Protocol"
752     ;; (section 5.5.2 of AMOP).
753     (update-slots class (compute-slots class))
754     (update-gfs-of-class class)
755     (update-inits class (compute-default-initargs class))
756     (update-ctors 'finalize-inheritance :class class))
757   (unless finalizep
758     (dolist (sub (class-direct-subclasses class)) (update-class sub nil))))
759
760 (defun update-cpl (class cpl)
761   (if (class-finalized-p class)
762       (unless (equal (class-precedence-list class) cpl)
763         ;; comment from the old CMU CL sources:
764         ;;   Need to have the cpl setup before update-lisp-class-layout
765         ;;   is called on CMU CL.
766         (setf (slot-value class 'class-precedence-list) cpl)
767         (force-cache-flushes class))
768       (setf (slot-value class 'class-precedence-list) cpl))
769   (update-class-can-precede-p cpl))
770
771 (defun update-class-can-precede-p (cpl)
772   (when cpl
773     (let ((first (car cpl)))
774       (dolist (c (cdr cpl))
775         (pushnew c (slot-value first 'can-precede-list))))
776     (update-class-can-precede-p (cdr cpl))))
777
778 (defun class-can-precede-p (class1 class2)
779   (member class2 (class-can-precede-list class1)))
780
781 (defun update-slots (class eslotds)
782   (let ((instance-slots ())
783         (class-slots    ()))
784     (dolist (eslotd eslotds)
785       (let ((alloc (slot-definition-allocation eslotd)))
786         (case alloc
787           (:instance (push eslotd instance-slots))
788           (:class (push eslotd class-slots)))))
789
790     ;; If there is a change in the shape of the instances then the
791     ;; old class is now obsolete.
792     (let* ((nlayout (mapcar #'slot-definition-name
793                             (sort instance-slots #'<
794                                   :key #'slot-definition-location)))
795            (nslots (length nlayout))
796            (nwrapper-class-slots (compute-class-slots class-slots))
797            (owrapper (class-wrapper class))
798            (olayout (and owrapper (wrapper-instance-slots-layout owrapper)))
799            (owrapper-class-slots (and owrapper (wrapper-class-slots owrapper)))
800            (nwrapper
801             (cond ((null owrapper)
802                    (make-wrapper nslots class))
803                   ((and (equal nlayout olayout)
804                         (not
805                          (loop for o in owrapper-class-slots
806                                for n in nwrapper-class-slots
807                                do (unless (eq (car o) (car n)) (return t)))))
808                    owrapper)
809                   (t
810                    ;; This will initialize the new wrapper to have the
811                    ;; same state as the old wrapper. We will then have
812                    ;; to change that. This may seem like wasted work
813                    ;; (and it is), but the spec requires that we call
814                    ;; MAKE-INSTANCES-OBSOLETE.
815                    (make-instances-obsolete class)
816                    (class-wrapper class)))))
817
818       (with-slots (wrapper slots) class
819         (update-lisp-class-layout class nwrapper)
820         (setf slots eslotds
821               (wrapper-instance-slots-layout nwrapper) nlayout
822               (wrapper-class-slots nwrapper) nwrapper-class-slots
823               (wrapper-no-of-instance-slots nwrapper) nslots
824               wrapper nwrapper))
825
826       (unless (eq owrapper nwrapper)
827         (update-pv-table-cache-info class)))))
828
829 (defun compute-class-slots (eslotds)
830   (let (collect)
831     (dolist (eslotd eslotds)
832       (push (assoc (slot-definition-name eslotd)
833                    (class-slot-cells (slot-definition-class eslotd)))
834             collect))
835     (nreverse collect)))
836
837 (defun update-gfs-of-class (class)
838   (when (and (class-finalized-p class)
839              (let ((cpl (class-precedence-list class)))
840                (or (member *the-class-slot-class* cpl)
841                    (member *the-class-standard-effective-slot-definition*
842                            cpl))))
843     (let ((gf-table (make-hash-table :test 'eq)))
844       (labels ((collect-gfs (class)
845                  (dolist (gf (specializer-direct-generic-functions class))
846                    (setf (gethash gf gf-table) t))
847                  (mapc #'collect-gfs (class-direct-superclasses class))))
848         (collect-gfs class)
849         (maphash (lambda (gf ignore)
850                    (declare (ignore ignore))
851                    (update-gf-dfun class gf))
852                  gf-table)))))
853
854 (defun update-inits (class inits)
855   (setf (plist-value class 'default-initargs) inits))
856 \f
857 (defmethod compute-default-initargs ((class slot-class))
858   (let ((cpl (class-precedence-list class))
859         (direct (class-direct-default-initargs class)))
860     (labels ((walk (tail)
861                (if (null tail)
862                    nil
863                    (let ((c (pop tail)))
864                      (append (if (eq c class)
865                                  direct
866                                  (class-direct-default-initargs c))
867                              (walk tail))))))
868       (let ((initargs (walk cpl)))
869         (delete-duplicates initargs :test #'eq :key #'car :from-end t)))))
870 \f
871 ;;;; protocols for constructing direct and effective slot definitions
872
873 (defmethod direct-slot-definition-class ((class std-class) initargs)
874   (declare (ignore initargs))
875   (find-class 'standard-direct-slot-definition))
876
877 (defun make-direct-slotd (class initargs)
878   (let ((initargs (list* :class class initargs)))
879     (apply #'make-instance
880            (direct-slot-definition-class class initargs)
881            initargs)))
882
883 (defmethod compute-slots ((class std-class))
884   ;; As specified, we must call COMPUTE-EFFECTIVE-SLOT-DEFINITION once
885   ;; for each different slot name we find in our superclasses. Each
886   ;; call receives the class and a list of the dslotds with that name.
887   ;; The list is in most-specific-first order.
888   (let ((name-dslotds-alist ()))
889     (dolist (c (class-precedence-list class))
890       (dolist (slot (class-direct-slots c))
891         (let* ((name (slot-definition-name slot))
892                (entry (assq name name-dslotds-alist)))
893           (if entry
894               (push slot (cdr entry))
895               (push (list name slot) name-dslotds-alist)))))
896     (mapcar (lambda (direct)
897               (compute-effective-slot-definition class
898                                                  (car direct)
899                                                  (nreverse (cdr direct))))
900             name-dslotds-alist)))
901
902 (defmethod compute-slots ((class standard-class))
903   (call-next-method))
904
905 (defmethod compute-slots :around ((class standard-class))
906   (let ((eslotds (call-next-method))
907         (location -1))
908     (dolist (eslotd eslotds eslotds)
909       (setf (slot-definition-location eslotd)
910             (ecase (slot-definition-allocation eslotd)
911               (:instance
912                (incf location))
913               (:class
914                (let* ((name (slot-definition-name eslotd))
915                       (from-class (slot-definition-allocation-class eslotd))
916                       (cell (assq name (class-slot-cells from-class))))
917                  (aver (consp cell))
918                  cell))))
919       (initialize-internal-slot-functions eslotd))))
920
921 (defmethod compute-slots ((class funcallable-standard-class))
922   (call-next-method))
923
924 (defmethod compute-slots :around ((class funcallable-standard-class))
925   (labels ((instance-slot-names (slotds)
926              (let (collect)
927                (dolist (slotd slotds (nreverse collect))
928                  (when (eq (slot-definition-allocation slotd) :instance)
929                    (push (slot-definition-name slotd) collect)))))
930            ;; This sorts slots so that slots of classes later in the CPL
931            ;; come before slots of other classes.  This is crucial for
932            ;; funcallable instances because it ensures that the slots of
933            ;; FUNCALLABLE-STANDARD-OBJECT, which includes the slots of
934            ;; KERNEL:FUNCALLABLE-INSTANCE, come first, which in turn
935            ;; makes it possible to treat FUNCALLABLE-STANDARD-OBJECT as
936            ;; a funcallable instance.
937            (compute-layout (eslotds)
938              (let ((first ())
939                    (names (instance-slot-names eslotds)))
940                (dolist (class
941                         (reverse (class-precedence-list class))
942                         (nreverse (nconc names first)))
943                  (dolist (ss (class-slots class))
944                    (let ((name (slot-definition-name ss)))
945                      (when (member name names)
946                        (push name first)
947                        (setq names (delete name names)))))))))
948     (let ((all-slotds (call-next-method))
949           (instance-slots ())
950           (class-slots ()))
951       (dolist (slotd all-slotds)
952         (ecase (slot-definition-allocation slotd)
953           (:instance (push slotd instance-slots))
954           (:class (push slotd class-slots))))
955       (let ((layout (compute-layout instance-slots)))
956         (dolist (slotd instance-slots)
957           (setf (slot-definition-location slotd)
958                 (position (slot-definition-name slotd) layout))
959           (initialize-internal-slot-functions slotd)))
960       (dolist (slotd class-slots)
961         (let ((name (slot-definition-name slotd))
962               (from-class (slot-definition-allocation-class slotd)))
963           (setf (slot-definition-location slotd)
964                 (assoc name (class-slot-cells from-class)))
965           (aver (consp (slot-definition-location slotd)))
966           (initialize-internal-slot-functions slotd)))
967       all-slotds)))
968
969 (defmethod compute-slots ((class structure-class))
970   (mapcan (lambda (superclass)
971             (mapcar (lambda (dslotd)
972                       (compute-effective-slot-definition
973                        class
974                        (slot-definition-name dslotd)
975                        (list dslotd)))
976                     (class-direct-slots superclass)))
977           (reverse (slot-value class 'class-precedence-list))))
978
979 (defmethod compute-slots :around ((class structure-class))
980   (let ((eslotds (call-next-method)))
981     (mapc #'initialize-internal-slot-functions eslotds)
982     eslotds))
983
984 (defmethod compute-effective-slot-definition ((class slot-class) name dslotds)
985   (declare (ignore name))
986   (let* ((initargs (compute-effective-slot-definition-initargs class dslotds))
987          (class (effective-slot-definition-class class initargs)))
988     (apply #'make-instance class initargs)))
989
990 (defmethod effective-slot-definition-class ((class std-class) initargs)
991   (declare (ignore initargs))
992   (find-class 'standard-effective-slot-definition))
993
994 (defmethod effective-slot-definition-class ((class structure-class) initargs)
995   (declare (ignore initargs))
996   (find-class 'structure-effective-slot-definition))
997
998 (defmethod compute-effective-slot-definition-initargs
999     ((class slot-class) direct-slotds)
1000   (let* ((name nil)
1001          (initfunction nil)
1002          (initform nil)
1003          (initargs nil)
1004          (allocation nil)
1005          (allocation-class nil)
1006          (type t)
1007          (namep  nil)
1008          (initp  nil)
1009          (allocp nil))
1010
1011     (dolist (slotd direct-slotds)
1012       (when slotd
1013         (unless namep
1014           (setq name (slot-definition-name slotd)
1015                 namep t))
1016         (unless initp
1017           (when (slot-definition-initfunction slotd)
1018             (setq initform (slot-definition-initform slotd)
1019                   initfunction (slot-definition-initfunction slotd)
1020                   initp t)))
1021         (unless allocp
1022           (setq allocation (slot-definition-allocation slotd)
1023                 allocation-class (slot-definition-class slotd)
1024                 allocp t))
1025         (setq initargs (append (slot-definition-initargs slotd) initargs))
1026         (let ((slotd-type (slot-definition-type slotd)))
1027           (setq type (cond ((eq type t) slotd-type)
1028                            ((*subtypep type slotd-type) type)
1029                            (t `(and ,type ,slotd-type)))))))
1030     (list :name name
1031           :initform initform
1032           :initfunction initfunction
1033           :initargs initargs
1034           :allocation allocation
1035           :allocation-class allocation-class
1036           :type type
1037           :class class)))
1038
1039 (defmethod compute-effective-slot-definition-initargs :around
1040     ((class structure-class) direct-slotds)
1041   (let ((slotd (car direct-slotds)))
1042     (list* :defstruct-accessor-symbol
1043            (slot-definition-defstruct-accessor-symbol slotd)
1044            :internal-reader-function
1045            (slot-definition-internal-reader-function slotd)
1046            :internal-writer-function
1047            (slot-definition-internal-writer-function slotd)
1048            (call-next-method))))
1049 \f
1050 ;;; NOTE: For bootstrapping considerations, these can't use MAKE-INSTANCE
1051 ;;;       to make the method object. They have to use make-a-method which
1052 ;;;       is a specially bootstrapped mechanism for making standard methods.
1053 (defmethod reader-method-class ((class slot-class) direct-slot &rest initargs)
1054   (declare (ignore direct-slot initargs))
1055   (find-class 'standard-reader-method))
1056
1057 (defmethod add-reader-method ((class slot-class) generic-function slot-name)
1058   (add-method generic-function
1059               (make-a-method 'standard-reader-method
1060                              ()
1061                              (list (or (class-name class) 'object))
1062                              (list class)
1063                              (make-reader-method-function class slot-name)
1064                              "automatically generated reader method"
1065                              slot-name)))
1066
1067 (defmethod writer-method-class ((class slot-class) direct-slot &rest initargs)
1068   (declare (ignore direct-slot initargs))
1069   (find-class 'standard-writer-method))
1070
1071 (defmethod add-writer-method ((class slot-class) generic-function slot-name)
1072   (add-method generic-function
1073               (make-a-method 'standard-writer-method
1074                              ()
1075                              (list 'new-value (or (class-name class) 'object))
1076                              (list *the-class-t* class)
1077                              (make-writer-method-function class slot-name)
1078                              "automatically generated writer method"
1079                              slot-name)))
1080
1081 (defmethod add-boundp-method ((class slot-class) generic-function slot-name)
1082   (add-method generic-function
1083               (make-a-method 'standard-boundp-method
1084                              ()
1085                              (list (or (class-name class) 'object))
1086                              (list class)
1087                              (make-boundp-method-function class slot-name)
1088                              "automatically generated boundp method"
1089                              slot-name)))
1090
1091 (defmethod remove-reader-method ((class slot-class) generic-function)
1092   (let ((method (get-method generic-function () (list class) nil)))
1093     (when method (remove-method generic-function method))))
1094
1095 (defmethod remove-writer-method ((class slot-class) generic-function)
1096   (let ((method
1097           (get-method generic-function () (list *the-class-t* class) nil)))
1098     (when method (remove-method generic-function method))))
1099
1100 (defmethod remove-boundp-method ((class slot-class) generic-function)
1101   (let ((method (get-method generic-function () (list class) nil)))
1102     (when method (remove-method generic-function method))))
1103 \f
1104 ;;; MAKE-READER-METHOD-FUNCTION and MAKE-WRITE-METHOD function are NOT
1105 ;;; part of the standard protocol. They are however useful, PCL makes
1106 ;;; use of them internally and documents them for PCL users.
1107 ;;;
1108 ;;; *** This needs work to make type testing by the writer functions which
1109 ;;; *** do type testing faster. The idea would be to have one constructor
1110 ;;; *** for each possible type test.
1111 ;;;
1112 ;;; *** There is a subtle bug here which is going to have to be fixed.
1113 ;;; *** Namely, the simplistic use of the template has to be fixed. We
1114 ;;; *** have to give the OPTIMIZE-SLOT-VALUE method the user might have
1115 ;;; *** defined for this metaclass a chance to run.
1116
1117 (defmethod make-reader-method-function ((class slot-class) slot-name)
1118   (make-std-reader-method-function (class-name class) slot-name))
1119
1120 (defmethod make-writer-method-function ((class slot-class) slot-name)
1121   (make-std-writer-method-function (class-name class) slot-name))
1122
1123 (defmethod make-boundp-method-function ((class slot-class) slot-name)
1124   (make-std-boundp-method-function (class-name class) slot-name))
1125 \f
1126 (defmethod compatible-meta-class-change-p (class proto-new-class)
1127   (eq (class-of class) (class-of proto-new-class)))
1128
1129 (defmethod validate-superclass ((class class) (new-super class))
1130   (or (eq new-super *the-class-t*)
1131       (eq (class-of class) (class-of new-super))))
1132
1133 (defmethod validate-superclass ((class standard-class) (new-super std-class))
1134   (let ((new-super-meta-class (class-of new-super)))
1135     (or (eq new-super-meta-class *the-class-std-class*)
1136         (eq (class-of class) new-super-meta-class))))
1137 \f
1138 ;;; What this does depends on which of the four possible values of
1139 ;;; LAYOUT-INVALID the PCL wrapper has; the simplest case is when it
1140 ;;; is (:FLUSH <wrapper>) or (:OBSOLETE <wrapper>), when there is
1141 ;;; nothing to do, as the new wrapper has already been created.  If
1142 ;;; LAYOUT-INVALID returns NIL, then we invalidate it (setting it to
1143 ;;; (:FLUSH <wrapper>); UPDATE-SLOTS later gets to choose whether or
1144 ;;; not to "upgrade" this to (:OBSOLETE <wrapper>).
1145 ;;;
1146 ;;; This leaves the case where LAYOUT-INVALID returns T, which happens
1147 ;;; when REGISTER-LAYOUT has invalidated a superclass of CLASS (which
1148 ;;; invalidated all the subclasses in SB-KERNEL land).  Again, here we
1149 ;;; must flush the caches and allow UPDATE-SLOTS to decide whether to
1150 ;;; obsolete the wrapper.
1151 ;;;
1152 ;;; FIXME: either here or in INVALID-WRAPPER-P looks like a good place
1153 ;;; for (AVER (NOT (EQ (LAYOUT-INVALID OWRAPPER)
1154 ;;;                    :UNINITIALIZED)))
1155 ;;;
1156 ;;; Thanks to Gerd Moellmann for the explanation.  -- CSR, 2002-10-29
1157 (defun force-cache-flushes (class)
1158   (let* ((owrapper (class-wrapper class)))
1159     ;; We only need to do something if the wrapper is still valid. If
1160     ;; the wrapper isn't valid, state will be FLUSH or OBSOLETE, and
1161     ;; both of those will already be doing what we want. In
1162     ;; particular, we must be sure we never change an OBSOLETE into a
1163     ;; FLUSH since OBSOLETE means do what FLUSH does and then some.
1164     (when (or (not (invalid-wrapper-p owrapper))
1165               ;; KLUDGE: despite the observations above, this remains
1166               ;; a violation of locality or what might be considered
1167               ;; good style.  There has to be a better way!  -- CSR,
1168               ;; 2002-10-29
1169               (eq (layout-invalid owrapper) t))
1170       (let ((nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1171                                     class)))
1172         (setf (wrapper-instance-slots-layout nwrapper)
1173               (wrapper-instance-slots-layout owrapper))
1174         (setf (wrapper-class-slots nwrapper)
1175               (wrapper-class-slots owrapper))
1176         (with-pcl-lock
1177           (update-lisp-class-layout class nwrapper)
1178           (setf (slot-value class 'wrapper) nwrapper)
1179           (invalidate-wrapper owrapper :flush nwrapper))))))
1180
1181 (defun flush-cache-trap (owrapper nwrapper instance)
1182   (declare (ignore owrapper))
1183   (set-wrapper instance nwrapper))
1184 \f
1185 ;;; MAKE-INSTANCES-OBSOLETE can be called by user code. It will cause
1186 ;;; the next access to the instance (as defined in 88-002R) to trap
1187 ;;; through the UPDATE-INSTANCE-FOR-REDEFINED-CLASS mechanism.
1188 (defmethod make-instances-obsolete ((class std-class))
1189   (let* ((owrapper (class-wrapper class))
1190          (nwrapper (make-wrapper (wrapper-no-of-instance-slots owrapper)
1191                                  class)))
1192       (setf (wrapper-instance-slots-layout nwrapper)
1193             (wrapper-instance-slots-layout owrapper))
1194       (setf (wrapper-class-slots nwrapper)
1195             (wrapper-class-slots owrapper))
1196       (with-pcl-lock
1197         (update-lisp-class-layout class nwrapper)
1198         (setf (slot-value class 'wrapper) nwrapper)
1199         (invalidate-wrapper owrapper :obsolete nwrapper)
1200         class)))
1201
1202 (defmethod make-instances-obsolete ((class symbol))
1203   (make-instances-obsolete (find-class class)))
1204
1205 ;;; OBSOLETE-INSTANCE-TRAP is the internal trap that is called when we
1206 ;;; see an obsolete instance. The times when it is called are:
1207 ;;;   - when the instance is involved in method lookup
1208 ;;;   - when attempting to access a slot of an instance
1209 ;;;
1210 ;;; It is not called by class-of, wrapper-of, or any of the low-level
1211 ;;; instance access macros.
1212 ;;;
1213 ;;; Of course these times when it is called are an internal
1214 ;;; implementation detail of PCL and are not part of the documented
1215 ;;; description of when the obsolete instance update happens. The
1216 ;;; documented description is as it appears in 88-002R.
1217 ;;;
1218 ;;; This has to return the new wrapper, so it counts on all the
1219 ;;; methods on obsolete-instance-trap-internal to return the new
1220 ;;; wrapper. It also does a little internal error checking to make
1221 ;;; sure that the traps are only happening when they should, and that
1222 ;;; the trap methods are computing appropriate new wrappers.
1223
1224 ;;; OBSOLETE-INSTANCE-TRAP might be called on structure instances
1225 ;;; after a structure is redefined. In most cases,
1226 ;;; OBSOLETE-INSTANCE-TRAP will not be able to fix the old instance,
1227 ;;; so it must signal an error. The hard part of this is that the
1228 ;;; error system and debugger might cause OBSOLETE-INSTANCE-TRAP to be
1229 ;;; called again, so in that case, we have to return some reasonable
1230 ;;; wrapper, instead.
1231
1232 (defvar *in-obsolete-instance-trap* nil)
1233 (defvar *the-wrapper-of-structure-object*
1234   (class-wrapper (find-class 'structure-object)))
1235
1236 (define-condition obsolete-structure (error)
1237   ((datum :reader obsolete-structure-datum :initarg :datum))
1238   (:report
1239    (lambda (condition stream)
1240      ;; Don't try to print the structure, since it probably won't work.
1241      (format stream
1242              "~@<obsolete structure error for a structure of type ~2I~_~S~:>"
1243              (type-of (obsolete-structure-datum condition))))))
1244
1245 (defun obsolete-instance-trap (owrapper nwrapper instance)
1246   (if (not (pcl-instance-p instance))
1247       (if *in-obsolete-instance-trap*
1248           *the-wrapper-of-structure-object*
1249            (let ((*in-obsolete-instance-trap* t))
1250              (error 'obsolete-structure :datum instance)))
1251       (let* ((class (wrapper-class* nwrapper))
1252              (copy (allocate-instance class)) ;??? allocate-instance ???
1253              (olayout (wrapper-instance-slots-layout owrapper))
1254              (nlayout (wrapper-instance-slots-layout nwrapper))
1255              (oslots (get-slots instance))
1256              (nslots (get-slots copy))
1257              (oclass-slots (wrapper-class-slots owrapper))
1258              (added ())
1259              (discarded ())
1260              (plist ()))
1261         ;; local  --> local     transfer
1262         ;; local  --> shared       discard
1263         ;; local  -->  --         discard
1264         ;; shared --> local     transfer
1265         ;; shared --> shared       discard
1266         ;; shared -->  --         discard
1267         ;;  --    --> local     add
1268         ;;  --    --> shared    --
1269
1270         ;; Go through all the old local slots.
1271         (let ((opos 0))
1272           (dolist (name olayout)
1273             (let ((npos (posq name nlayout)))
1274               (if npos
1275                   (setf (clos-slots-ref nslots npos)
1276                         (clos-slots-ref oslots opos))
1277                   (progn
1278                     (push name discarded)
1279                     (unless (eq (clos-slots-ref oslots opos) +slot-unbound+)
1280                       (setf (getf plist name) (clos-slots-ref oslots opos))))))
1281             (incf opos)))
1282
1283         ;; Go through all the old shared slots.
1284         (dolist (oclass-slot-and-val oclass-slots)
1285           (let ((name (car oclass-slot-and-val))
1286                 (val (cdr oclass-slot-and-val)))
1287             (let ((npos (posq name nlayout)))
1288               (if npos
1289                   (setf (clos-slots-ref nslots npos) (cdr oclass-slot-and-val))
1290                   (progn (push name discarded)
1291                          (unless (eq val +slot-unbound+)
1292                            (setf (getf plist name) val)))))))
1293
1294         ;; Go through all the new local slots to compute the added slots.
1295         (dolist (nlocal nlayout)
1296           (unless (or (memq nlocal olayout)
1297                       (assq nlocal oclass-slots))
1298             (push nlocal added)))
1299
1300         (swap-wrappers-and-slots instance copy)
1301
1302         (update-instance-for-redefined-class instance
1303                                              added
1304                                              discarded
1305                                              plist)
1306         nwrapper)))
1307 \f
1308 (defun change-class-internal (instance new-class initargs)
1309   (let* ((old-class (class-of instance))
1310          (copy (allocate-instance new-class))
1311          (new-wrapper (get-wrapper copy))
1312          (old-wrapper (class-wrapper old-class))
1313          (old-layout (wrapper-instance-slots-layout old-wrapper))
1314          (new-layout (wrapper-instance-slots-layout new-wrapper))
1315          (old-slots (get-slots instance))
1316          (new-slots (get-slots copy))
1317          (old-class-slots (wrapper-class-slots old-wrapper)))
1318
1319     ;; "The values of local slots specified by both the class CTO and
1320     ;; CFROM are retained. If such a local slot was unbound, it
1321     ;; remains unbound."
1322     (let ((new-position 0))
1323       (dolist (new-slot new-layout)
1324         (let ((old-position (posq new-slot old-layout)))
1325           (when old-position
1326             (setf (clos-slots-ref new-slots new-position)
1327                   (clos-slots-ref old-slots old-position))))
1328         (incf new-position)))
1329
1330     ;; "The values of slots specified as shared in the class CFROM and
1331     ;; as local in the class CTO are retained."
1332     (dolist (slot-and-val old-class-slots)
1333       (let ((position (posq (car slot-and-val) new-layout)))
1334         (when position
1335           (setf (clos-slots-ref new-slots position) (cdr slot-and-val)))))
1336
1337     ;; Make the copy point to the old instance's storage, and make the
1338     ;; old instance point to the new storage.
1339     (swap-wrappers-and-slots instance copy)
1340
1341     (apply #'update-instance-for-different-class copy instance initargs)
1342     instance))
1343
1344 (defmethod change-class ((instance standard-object)
1345                          (new-class standard-class)
1346                          &rest initargs)
1347   (change-class-internal instance new-class initargs))
1348
1349 (defmethod change-class ((instance funcallable-standard-object)
1350                          (new-class funcallable-standard-class)
1351                          &rest initargs)
1352   (change-class-internal instance new-class initargs))
1353
1354 (defmethod change-class ((instance standard-object)
1355                          (new-class funcallable-standard-class)
1356                          &rest initargs)
1357   (declare (ignore initargs))
1358   (error "You can't change the class of ~S to ~S~@
1359           because it isn't already an instance with metaclass ~S."
1360          instance new-class 'standard-class))
1361
1362 (defmethod change-class ((instance funcallable-standard-object)
1363                          (new-class standard-class)
1364                          &rest initargs)
1365   (declare (ignore initargs))
1366   (error "You can't change the class of ~S to ~S~@
1367           because it isn't already an instance with metaclass ~S."
1368          instance new-class 'funcallable-standard-class))
1369
1370 (defmethod change-class ((instance t) (new-class-name symbol) &rest initargs)
1371   (apply #'change-class instance (find-class new-class-name) initargs))
1372 \f
1373 ;;;; The metaclass BUILT-IN-CLASS
1374 ;;;;
1375 ;;;; This metaclass is something of a weird creature. By this point, all
1376 ;;;; instances of it which will exist have been created, and no instance
1377 ;;;; is ever created by calling MAKE-INSTANCE.
1378 ;;;;
1379 ;;;; But, there are other parts of the protocol we must follow and those
1380 ;;;; definitions appear here.
1381
1382 (defmethod shared-initialize :before
1383            ((class built-in-class) slot-names &rest initargs)
1384   (declare (ignore slot-names initargs))
1385   (error "attempt to initialize or reinitialize a built in class"))
1386
1387 (defmethod class-direct-slots       ((class built-in-class)) ())
1388 (defmethod class-slots             ((class built-in-class)) ())
1389 (defmethod class-direct-default-initargs ((class built-in-class)) ())
1390 (defmethod class-default-initargs       ((class built-in-class)) ())
1391
1392 (defmethod validate-superclass ((c class) (s built-in-class))
1393   (or (eq s *the-class-t*)
1394       (eq s *the-class-stream*)))
1395 \f
1396 ;;; Some necessary methods for FORWARD-REFERENCED-CLASS
1397 (defmethod class-direct-slots ((class forward-referenced-class)) ())
1398 (defmethod class-direct-default-initargs ((class forward-referenced-class)) ())
1399 (macrolet ((def (method)
1400              `(defmethod ,method ((class forward-referenced-class))
1401                 (error "~@<~I~S was called on a forward referenced class:~2I~_~S~:>"
1402                        ',method class))))
1403   (def class-default-initargs)
1404   (def class-precedence-list)
1405   (def class-slots))
1406
1407 (defmethod validate-superclass ((c slot-class)
1408                                 (f forward-referenced-class))
1409   t)
1410 \f
1411 (defmethod add-dependent ((metaobject dependent-update-mixin) dependent)
1412   (pushnew dependent (plist-value metaobject 'dependents)))
1413
1414 (defmethod remove-dependent ((metaobject dependent-update-mixin) dependent)
1415   (setf (plist-value metaobject 'dependents)
1416         (delete dependent (plist-value metaobject 'dependents))))
1417
1418 (defmethod map-dependents ((metaobject dependent-update-mixin) function)
1419   (dolist (dependent (plist-value metaobject 'dependents))
1420     (funcall function dependent)))
1421