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