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