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