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