1.0.9.11: even faster SLOT-VALUE &co
[sbcl.git] / src / code / class.lisp
1 ;;;; This file contains structures and functions for the maintenance of
2 ;;;; basic information about defined types. Different object systems
3 ;;;; can be supported simultaneously.
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15
16 (!begin-collecting-cold-init-forms)
17 \f
18 ;;;; the CLASSOID structure
19
20 ;;; The CLASSOID structure is a supertype of all classoid types.  A
21 ;;; CLASSOID is also a CTYPE structure as recognized by the type
22 ;;; system.  (FIXME: It's also a type specifier, though this might go
23 ;;; away as with the merger of SB-PCL:CLASS and CL:CLASS it's no
24 ;;; longer necessary)
25 (def!struct (classoid
26              (:make-load-form-fun classoid-make-load-form-fun)
27              (:include ctype
28                        (class-info (type-class-or-lose 'classoid)))
29              (:constructor nil)
30              #-no-ansi-print-object
31              (:print-object
32               (lambda (class stream)
33                 (let ((name (classoid-name class)))
34                   (print-unreadable-object (class stream
35                                                   :type t
36                                                   :identity (not name))
37                     (format stream
38                             ;; FIXME: Make sure that this prints
39                             ;; reasonably for anonymous classes.
40                             "~:[anonymous~;~:*~S~]~@[ (~(~A~))~]"
41                             name
42                             (classoid-state class))))))
43              #-sb-xc-host (:pure nil))
44   ;; the value to be returned by CLASSOID-NAME.
45   (name nil :type symbol)
46   ;; the current layout for this class, or NIL if none assigned yet
47   (layout nil :type (or layout null))
48   ;; How sure are we that this class won't be redefined?
49   ;;   :READ-ONLY = We are committed to not changing the effective
50   ;;                slots or superclasses.
51   ;;   :SEALED    = We can't even add subclasses.
52   ;;   NIL        = Anything could happen.
53   (state nil :type (member nil :read-only :sealed))
54   ;; direct superclasses of this class
55   (direct-superclasses () :type list)
56   ;; representation of all of the subclasses (direct or indirect) of
57   ;; this class. This is NIL if no subclasses or not initalized yet;
58   ;; otherwise, it's an EQ hash-table mapping CLASSOID objects to the
59   ;; subclass layout that was in effect at the time the subclass was
60   ;; created.
61   (subclasses nil :type (or null hash-table))
62   ;; the PCL class (= CL:CLASS, but with a view to future flexibility
63   ;; we don't just call it the CLASS slot) object for this class, or
64   ;; NIL if none assigned yet
65   (pcl-class nil))
66
67 (defun classoid-make-load-form-fun (class)
68   (/show "entering CLASSOID-MAKE-LOAD-FORM-FUN" class)
69   (let ((name (classoid-name class)))
70     (unless (and name (eq (find-classoid name nil) class))
71       (/show "anonymous/undefined class case")
72       (error "can't use anonymous or undefined class as constant:~%  ~S"
73              class))
74     `(locally
75        ;; KLUDGE: There's a FIND-CLASSOID DEFTRANSFORM for constant
76        ;; class names which creates fast but non-cold-loadable,
77        ;; non-compact code. In this context, we'd rather have compact,
78        ;; cold-loadable code. -- WHN 19990928
79        (declare (notinline find-classoid))
80        (find-classoid ',name))))
81 \f
82 ;;;; basic LAYOUT stuff
83
84 ;;; Note: This bound is set somewhat less than MOST-POSITIVE-FIXNUM
85 ;;; in order to guarantee that several hash values can be added without
86 ;;; overflowing into a bignum.
87 (def!constant layout-clos-hash-limit (1+ (ash sb!xc:most-positive-fixnum -3))
88   #!+sb-doc
89   "the exclusive upper bound on LAYOUT-CLOS-HASH values")
90 (def!type layout-clos-hash () '(integer 0 #.layout-clos-hash-limit))
91
92 ;;; a list of conses, initialized by genesis
93 ;;;
94 ;;; In each cons, the car is the symbol naming the layout, and the
95 ;;; cdr is the layout itself.
96 (defvar *!initial-layouts*)
97
98 ;;; a table mapping class names to layouts for classes we have
99 ;;; referenced but not yet loaded. This is initialized from an alist
100 ;;; created by genesis describing the layouts that genesis created at
101 ;;; cold-load time.
102 (defvar *forward-referenced-layouts*)
103 (!cold-init-forms
104   (setq *forward-referenced-layouts* (make-hash-table :test 'equal))
105   #-sb-xc-host (progn
106                  (/show0 "processing *!INITIAL-LAYOUTS*")
107                  (dolist (x *!initial-layouts*)
108                    (setf (gethash (car x) *forward-referenced-layouts*)
109                          (cdr x)))
110                  (/show0 "done processing *!INITIAL-LAYOUTS*")))
111
112 ;;; The LAYOUT structure is pointed to by the first cell of instance
113 ;;; (or structure) objects. It represents what we need to know for
114 ;;; type checking and garbage collection. Whenever a class is
115 ;;; incompatibly redefined, a new layout is allocated. If two object's
116 ;;; layouts are EQ, then they are exactly the same type.
117 (def!struct (layout
118              ;; KLUDGE: A special hack keeps this from being
119              ;; called when building code for the
120              ;; cross-compiler. See comments at the DEFUN for
121              ;; this. -- WHN 19990914
122              (:make-load-form-fun #-sb-xc-host ignore-it
123                                   ;; KLUDGE: DEF!STRUCT at #+SB-XC-HOST
124                                   ;; time controls both the
125                                   ;; build-the-cross-compiler behavior
126                                   ;; and the run-the-cross-compiler
127                                   ;; behavior. The value below only
128                                   ;; works for build-the-cross-compiler.
129                                   ;; There's a special hack in
130                                   ;; EMIT-MAKE-LOAD-FORM which gives
131                                   ;; effectively IGNORE-IT behavior for
132                                   ;; LAYOUT at run-the-cross-compiler
133                                   ;; time. It would be cleaner to
134                                   ;; actually have an IGNORE-IT value
135                                   ;; stored, but it's hard to see how to
136                                   ;; do that concisely with the current
137                                   ;; DEF!STRUCT setup. -- WHN 19990930
138                                   #+sb-xc-host
139                                   make-load-form-for-layout))
140   ;; a pseudo-random hash value for use by CLOS.  KLUDGE: The fact
141   ;; that this slot is at offset 1 is known to GENESIS.
142   (clos-hash (random-layout-clos-hash) :type layout-clos-hash)
143   ;; the class that this is a layout for
144   (classoid (missing-arg) :type classoid)
145   ;; The value of this slot can be:
146   ;;   * :UNINITIALIZED if not initialized yet;
147   ;;   * NIL if this is the up-to-date layout for a class; or
148   ;;   * T if this layout has been invalidated (by being replaced by
149   ;;     a new, more-up-to-date LAYOUT).
150   ;;   * something else (probably a list) if the class is a PCL wrapper
151   ;;     and PCL has made it invalid and made a note to itself about it
152   (invalid :uninitialized :type (or cons (member nil t :uninitialized)))
153   ;; the layouts for all classes we inherit. If hierarchical, i.e. if
154   ;; DEPTHOID >= 0, then these are ordered by ORDER-LAYOUT-INHERITS
155   ;; (least to most specific), so that each inherited layout appears
156   ;; at its expected depth, i.e. at its LAYOUT-DEPTHOID value.
157   ;;
158   ;; Remaining elements are filled by the non-hierarchical layouts or,
159   ;; if they would otherwise be empty, by copies of succeeding layouts.
160   (inherits #() :type simple-vector)
161   ;; If inheritance is not hierarchical, this is -1. If inheritance is
162   ;; hierarchical, this is the inheritance depth, i.e. (LENGTH INHERITS).
163   ;; Note:
164   ;;  (1) This turns out to be a handy encoding for arithmetically
165   ;;      comparing deepness; it is generally useful to do a bare numeric
166   ;;      comparison of these depthoid values, and we hardly ever need to
167   ;;      test whether the values are negative or not.
168   ;;  (2) This was called INHERITANCE-DEPTH in classic CMU CL. It was
169   ;;      renamed because some of us find it confusing to call something
170   ;;      a depth when it isn't quite.
171   (depthoid -1 :type layout-depthoid)
172   ;; the number of top level descriptor cells in each instance
173   (length 0 :type index)
174   ;; If this layout has some kind of compiler meta-info, then this is
175   ;; it. If a structure, then we store the DEFSTRUCT-DESCRIPTION here.
176   (info nil)
177   ;; This is true if objects of this class are never modified to
178   ;; contain dynamic pointers in their slots or constant-like
179   ;; substructure (and hence can be copied into read-only space by
180   ;; PURIFY).
181   ;;
182   ;; This slot is known to the C runtime support code.
183   (pure nil :type (member t nil 0))
184   ;; Number of raw words at the end.
185   ;; This slot is known to the C runtime support code.
186   (n-untagged-slots 0 :type index)
187   ;; Definition location
188   (source-location nil)
189   ;; Information about slots in the class to PCL: this provides fast
190   ;; access to slot-definitions and locations by name, etc.
191   (slot-table #(nil) :type simple-vector)
192   ;; True IFF the layout belongs to a standand-instance or a
193   ;; standard-funcallable-instance -- that is, true only if the layout
194   ;; is really a wrapper.
195   ;;
196   ;; FIXME: If we unify wrappers and layouts this can go away, since
197   ;; it is only used in SB-PCL::EMIT-FETCH-WRAPPERS, which can then
198   ;; use INSTANCE-SLOTS-LAYOUT instead (if there is are no slot
199   ;; layouts, there are no slots for it to pull.)
200   (for-std-class-p nil :type boolean :read-only t))
201
202 (def!method print-object ((layout layout) stream)
203   (print-unreadable-object (layout stream :type t :identity t)
204     (format stream
205             "for ~S~@[, INVALID=~S~]"
206             (layout-proper-name layout)
207             (layout-invalid layout))))
208
209 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
210   (defun layout-proper-name (layout)
211     (classoid-proper-name (layout-classoid layout))))
212 \f
213 ;;;; support for the hash values used by CLOS when working with LAYOUTs
214
215 ;;; a generator for random values suitable for the CLOS-HASH slots of
216 ;;; LAYOUTs. We use our own RANDOM-STATE here because we'd like
217 ;;; pseudo-random values to come the same way in the target even when
218 ;;; we make minor changes to the system, in order to reduce the
219 ;;; mysteriousness of possible CLOS bugs.
220 (defvar *layout-clos-hash-random-state*)
221 (defun random-layout-clos-hash ()
222   ;; FIXME: I'm not sure why this expression is (1+ (RANDOM FOO)),
223   ;; returning a strictly positive value. I copied it verbatim from
224   ;; CMU CL INITIALIZE-LAYOUT-HASH, so presumably it works, but I
225   ;; dunno whether the hash values are really supposed to be 1-based.
226   ;; They're declared as INDEX.. Or is this a hack to try to avoid
227   ;; having to use bignum arithmetic? Or what? An explanation would be
228   ;; nice.
229   ;;
230   ;; an explanation is provided in Kiczales and Rodriguez, "Efficient
231   ;; Method Dispatch in PCL", 1990.  -- CSR, 2005-11-30
232   (1+ (random (1- layout-clos-hash-limit)
233               (if (boundp '*layout-clos-hash-random-state*)
234                   *layout-clos-hash-random-state*
235                   (setf *layout-clos-hash-random-state*
236                         (make-random-state))))))
237 \f
238 ;;; If we can't find any existing layout, then we create a new one
239 ;;; storing it in *FORWARD-REFERENCED-LAYOUTS*. In classic CMU CL, we
240 ;;; used to immediately check for compatibility, but for
241 ;;; cross-compilability reasons (i.e. convenience of using this
242 ;;; function in a MAKE-LOAD-FORM expression) that functionality has
243 ;;; been split off into INIT-OR-CHECK-LAYOUT.
244 (declaim (ftype (function (symbol) layout) find-layout))
245 (defun find-layout (name)
246   (let ((classoid (find-classoid name nil)))
247     (or (and classoid (classoid-layout classoid))
248         (gethash name *forward-referenced-layouts*)
249         (setf (gethash name *forward-referenced-layouts*)
250               (make-layout :classoid (or classoid
251                                          (make-undefined-classoid name)))))))
252
253 ;;; If LAYOUT is uninitialized, initialize it with CLASSOID, LENGTH,
254 ;;; INHERITS, and DEPTHOID, otherwise require that it be consistent
255 ;;; with CLASSOID, LENGTH, INHERITS, and DEPTHOID.
256 ;;;
257 ;;; UNDEFINED-CLASS values are interpreted specially as "we don't know
258 ;;; anything about the class", so if LAYOUT is initialized, any
259 ;;; preexisting class slot value is OK, and if it's not initialized,
260 ;;; its class slot value is set to an UNDEFINED-CLASS. -- FIXME: This
261 ;;; is no longer true, :UNINITIALIZED used instead.
262 (declaim (ftype (function (layout classoid index simple-vector layout-depthoid
263                                   index)
264                           layout)
265                 init-or-check-layout))
266 (defun init-or-check-layout
267     (layout classoid length inherits depthoid nuntagged)
268   (cond ((eq (layout-invalid layout) :uninitialized)
269          ;; There was no layout before, we just created one which
270          ;; we'll now initialize with our information.
271          (setf (layout-length layout) length
272                (layout-inherits layout) inherits
273                (layout-depthoid layout) depthoid
274                (layout-n-untagged-slots layout) nuntagged
275                (layout-classoid layout) classoid
276                (layout-invalid layout) nil))
277         ;; FIXME: Now that LAYOUTs are born :UNINITIALIZED, maybe this
278         ;; clause is not needed?
279         ((not *type-system-initialized*)
280          (setf (layout-classoid layout) classoid))
281         (t
282          ;; There was an old layout already initialized with old
283          ;; information, and we'll now check that old information
284          ;; which was known with certainty is consistent with current
285          ;; information which is known with certainty.
286          (check-layout layout classoid length inherits depthoid nuntagged)))
287   layout)
288
289 ;;; In code for the target Lisp, we don't use dump LAYOUTs using the
290 ;;; standard load form mechanism, we use special fops instead, in
291 ;;; order to make cold load come out right. But when we're building
292 ;;; the cross-compiler, we can't do that because we don't have access
293 ;;; to special non-ANSI low-level things like special fops, and we
294 ;;; don't need to do that anyway because our code isn't going to be
295 ;;; cold loaded, so we use the ordinary load form system.
296 ;;;
297 ;;; KLUDGE: A special hack causes this not to be called when we are
298 ;;; building code for the target Lisp. It would be tidier to just not
299 ;;; have it in place when we're building the target Lisp, but it
300 ;;; wasn't clear how to do that without rethinking DEF!STRUCT quite a
301 ;;; bit, so I punted. -- WHN 19990914
302 #+sb-xc-host
303 (defun make-load-form-for-layout (layout &optional env)
304   (declare (type layout layout))
305   (declare (ignore env))
306   (when (layout-invalid layout)
307     (compiler-error "can't dump reference to obsolete class: ~S"
308                     (layout-classoid layout)))
309   (let ((name (classoid-name (layout-classoid layout))))
310     (unless name
311       (compiler-error "can't dump anonymous LAYOUT: ~S" layout))
312     ;; Since LAYOUT refers to a class which refers back to the LAYOUT,
313     ;; we have to do this in two stages, like the TREE-WITH-PARENT
314     ;; example in the MAKE-LOAD-FORM entry in the ANSI spec.
315     (values
316      ;; "creation" form (which actually doesn't create a new LAYOUT if
317      ;; there's a preexisting one with this name)
318      `(find-layout ',name)
319      ;; "initialization" form (which actually doesn't initialize
320      ;; preexisting LAYOUTs, just checks that they're consistent).
321      `(init-or-check-layout ',layout
322                             ',(layout-classoid layout)
323                             ',(layout-length layout)
324                             ',(layout-inherits layout)
325                             ',(layout-depthoid layout)
326                             ',(layout-n-untagged-slots layout)))))
327
328 ;;; If LAYOUT's slot values differ from the specified slot values in
329 ;;; any interesting way, then give a warning and return T.
330 (declaim (ftype (function (simple-string
331                            layout
332                            simple-string
333                            index
334                            simple-vector
335                            layout-depthoid
336                            index))
337                 redefine-layout-warning))
338 (defun redefine-layout-warning (old-context old-layout
339                                 context length inherits depthoid nuntagged)
340   (declare (type layout old-layout) (type simple-string old-context context))
341   (let ((name (layout-proper-name old-layout)))
342     (or (let ((old-inherits (layout-inherits old-layout)))
343           (or (when (mismatch old-inherits
344                               inherits
345                               :key #'layout-proper-name)
346                 (warn "change in superclasses of class ~S:~%  ~
347                        ~A superclasses: ~S~%  ~
348                        ~A superclasses: ~S"
349                       name
350                       old-context
351                       (map 'list #'layout-proper-name old-inherits)
352                       context
353                       (map 'list #'layout-proper-name inherits))
354                 t)
355               (let ((diff (mismatch old-inherits inherits)))
356                 (when diff
357                   (warn
358                    "in class ~S:~%  ~
359                     ~:(~A~) definition of superclass ~S is incompatible with~%  ~
360                     ~A definition."
361                    name
362                    old-context
363                    (layout-proper-name (svref old-inherits diff))
364                    context)
365                   t))))
366         (let ((old-length (layout-length old-layout)))
367           (unless (= old-length length)
368             (warn "change in instance length of class ~S:~%  ~
369                    ~A length: ~W~%  ~
370                    ~A length: ~W"
371                   name
372                   old-context old-length
373                   context length)
374             t))
375         (let ((old-nuntagged (layout-n-untagged-slots old-layout)))
376           (unless (= old-nuntagged nuntagged)
377             (warn "change in instance layout of class ~S:~%  ~
378                    ~A untagged slots: ~W~%  ~
379                    ~A untagged slots: ~W"
380                   name
381                   old-context old-nuntagged
382                   context nuntagged)
383             t))
384         (unless (= (layout-depthoid old-layout) depthoid)
385           (warn "change in the inheritance structure of class ~S~%  ~
386                  between the ~A definition and the ~A definition"
387                 name old-context context)
388           t))))
389
390 ;;; Require that LAYOUT data be consistent with CLASS, LENGTH,
391 ;;; INHERITS, and DEPTHOID.
392 (declaim (ftype (function
393                  (layout classoid index simple-vector layout-depthoid index))
394                 check-layout))
395 (defun check-layout (layout classoid length inherits depthoid nuntagged)
396   (aver (eq (layout-classoid layout) classoid))
397   (when (redefine-layout-warning "current" layout
398                                  "compile time" length inherits depthoid
399                                  nuntagged)
400     ;; Classic CMU CL had more options here. There are several reasons
401     ;; why they might want more options which are less appropriate for
402     ;; us: (1) It's hard to fit the classic CMU CL flexible approach
403     ;; into the ANSI-style MAKE-LOAD-FORM system, and having a
404     ;; non-MAKE-LOAD-FORM-style system is painful when we're trying to
405     ;; make the cross-compiler run under vanilla ANSI Common Lisp. (2)
406     ;; We have CLOS now, and if you want to be able to flexibly
407     ;; redefine classes without restarting the system, it'd make sense
408     ;; to use that, so supporting complexity in order to allow
409     ;; modifying DEFSTRUCTs without restarting the system is a low
410     ;; priority. (3) We now have the ability to rebuild the SBCL
411     ;; system from scratch, so we no longer need this functionality in
412     ;; order to maintain the SBCL system by modifying running images.
413     (error "The class ~S was not changed, and there's no guarantee that~@
414             the loaded code (which expected another layout) will work."
415            (layout-proper-name layout)))
416   (values))
417
418 ;;; a common idiom (the same as CMU CL FIND-LAYOUT) rolled up into a
419 ;;; single function call
420 ;;;
421 ;;; Used by the loader to forward-reference layouts for classes whose
422 ;;; definitions may not have been loaded yet. This allows type tests
423 ;;; to be loaded when the type definition hasn't been loaded yet.
424 (declaim (ftype (function (symbol index simple-vector layout-depthoid index)
425                           layout)
426                 find-and-init-or-check-layout))
427 (defun find-and-init-or-check-layout (name length inherits depthoid nuntagged)
428   (let ((layout (find-layout name)))
429     (init-or-check-layout layout
430                           (or (find-classoid name nil)
431                               (layout-classoid layout))
432                           length
433                           inherits
434                           depthoid
435                           nuntagged)))
436
437 ;;; Record LAYOUT as the layout for its class, adding it as a subtype
438 ;;; of all superclasses. This is the operation that "installs" a
439 ;;; layout for a class in the type system, clobbering any old layout.
440 ;;; However, this does not modify the class namespace; that is a
441 ;;; separate operation (think anonymous classes.)
442 ;;; -- If INVALIDATE, then all the layouts for any old definition
443 ;;;    and subclasses are invalidated, and the SUBCLASSES slot is cleared.
444 ;;; -- If DESTRUCT-LAYOUT, then this is some old layout, and is to be
445 ;;;    destructively modified to hold the same type information.
446 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
447 (defun register-layout (layout &key (invalidate t) destruct-layout)
448   (declare (type layout layout) (type (or layout null) destruct-layout))
449   (let* ((classoid (layout-classoid layout))
450          (classoid-layout (classoid-layout classoid))
451          (subclasses (classoid-subclasses classoid)))
452
453     ;; Attempting to register ourselves with a temporary undefined
454     ;; class placeholder is almost certainly a programmer error. (I
455     ;; should know, I did it.) -- WHN 19990927
456     (aver (not (undefined-classoid-p classoid)))
457
458     ;; This assertion dates from classic CMU CL. The rationale is
459     ;; probably that calling REGISTER-LAYOUT more than once for the
460     ;; same LAYOUT is almost certainly a programmer error.
461     (aver (not (eq classoid-layout layout)))
462
463     ;; Figure out what classes are affected by the change, and issue
464     ;; appropriate warnings and invalidations.
465     (when classoid-layout
466       (modify-classoid classoid)
467       (when subclasses
468         (dohash (subclass subclass-layout subclasses)
469           (modify-classoid subclass)
470           (when invalidate
471             (invalidate-layout subclass-layout))))
472       (when invalidate
473         (invalidate-layout classoid-layout)
474         (setf (classoid-subclasses classoid) nil)))
475
476     (if destruct-layout
477         (setf (layout-invalid destruct-layout) nil
478               (layout-inherits destruct-layout) (layout-inherits layout)
479               (layout-depthoid destruct-layout)(layout-depthoid layout)
480               (layout-length destruct-layout) (layout-length layout)
481               (layout-n-untagged-slots destruct-layout) (layout-n-untagged-slots layout)
482               (layout-info destruct-layout) (layout-info layout)
483               (classoid-layout classoid) destruct-layout)
484         (setf (layout-invalid layout) nil
485               (classoid-layout classoid) layout))
486
487     (dovector (super-layout (layout-inherits layout))
488       (let* ((super (layout-classoid super-layout))
489              (subclasses (or (classoid-subclasses super)
490                              (setf (classoid-subclasses super)
491                                    (make-hash-table :test 'eq)))))
492         (when (and (eq (classoid-state super) :sealed)
493                    (not (gethash classoid subclasses)))
494           (warn "unsealing sealed class ~S in order to subclass it"
495                 (classoid-name super))
496           (setf (classoid-state super) :read-only))
497         (setf (gethash classoid subclasses)
498               (or destruct-layout layout)))))
499
500   (values))
501 ); EVAL-WHEN
502
503 ;;; Arrange the inherited layouts to appear at their expected depth,
504 ;;; ensuring that hierarchical type tests succeed. Layouts with
505 ;;; DEPTHOID >= 0 (i.e. hierarchical classes) are placed first,
506 ;;; at exactly that index in the INHERITS vector. Then, non-hierarchical
507 ;;; layouts are placed in remaining elements. Then, any still-empty
508 ;;; elements are filled with their successors, ensuring that each
509 ;;; element contains a valid layout.
510 ;;;
511 ;;; This reordering may destroy CPL ordering, so the inherits should
512 ;;; not be read as being in CPL order.
513 (defun order-layout-inherits (layouts)
514   (declare (simple-vector layouts))
515   (let ((length (length layouts))
516         (max-depth -1))
517     (dotimes (i length)
518       (let ((depth (layout-depthoid (svref layouts i))))
519         (when (> depth max-depth)
520           (setf max-depth depth))))
521     (let* ((new-length (max (1+ max-depth) length))
522            ;; KLUDGE: 0 here is the "uninitialized" element.  We need
523            ;; to specify it explicitly for portability purposes, as
524            ;; elements can be read before being set [ see below, "(EQL
525            ;; OLD-LAYOUT 0)" ].  -- CSR, 2002-04-20
526            (inherits (make-array new-length :initial-element 0)))
527       (dotimes (i length)
528         (let* ((layout (svref layouts i))
529                (depth (layout-depthoid layout)))
530           (unless (eql depth -1)
531             (let ((old-layout (svref inherits depth)))
532               (unless (or (eql old-layout 0) (eq old-layout layout))
533                 (error "layout depth conflict: ~S~%" layouts)))
534             (setf (svref inherits depth) layout))))
535       (do ((i 0 (1+ i))
536            (j 0))
537           ((>= i length))
538         (declare (type index i j))
539         (let* ((layout (svref layouts i))
540                (depth (layout-depthoid layout)))
541           (when (eql depth -1)
542             (loop (when (eql (svref inherits j) 0)
543                     (return))
544                   (incf j))
545             (setf (svref inherits j) layout))))
546       (do ((i (1- new-length) (1- i)))
547           ((< i 0))
548         (declare (type fixnum i))
549         (when (eql (svref inherits i) 0)
550           (setf (svref inherits i) (svref inherits (1+ i)))))
551       inherits)))
552 \f
553 ;;;; class precedence lists
554
555 ;;; Topologically sort the list of objects to meet a set of ordering
556 ;;; constraints given by pairs (A . B) constraining A to precede B.
557 ;;; When there are multiple objects to choose, the tie-breaker
558 ;;; function is called with both the list of object to choose from and
559 ;;; the reverse ordering built so far.
560 (defun topological-sort (objects constraints tie-breaker)
561   (declare (list objects constraints)
562            (function tie-breaker))
563   (let ((obj-info (make-hash-table :size (length objects)))
564         (free-objs nil)
565         (result nil))
566     (dolist (constraint constraints)
567       (let ((obj1 (car constraint))
568             (obj2 (cdr constraint)))
569         (let ((info2 (gethash obj2 obj-info)))
570           (if info2
571               (incf (first info2))
572               (setf (gethash obj2 obj-info) (list 1))))
573         (let ((info1 (gethash obj1 obj-info)))
574           (if info1
575               (push obj2 (rest info1))
576               (setf (gethash obj1 obj-info) (list 0 obj2))))))
577     (dolist (obj objects)
578       (let ((info (gethash obj obj-info)))
579         (when (or (not info) (zerop (first info)))
580           (push obj free-objs))))
581     (loop
582      (flet ((next-result (obj)
583               (push obj result)
584               (dolist (successor (rest (gethash obj obj-info)))
585                 (let* ((successor-info (gethash successor obj-info))
586                        (count (1- (first successor-info))))
587                   (setf (first successor-info) count)
588                   (when (zerop count)
589                     (push successor free-objs))))))
590        (cond ((endp free-objs)
591               (dohash (obj info obj-info)
592                 (unless (zerop (first info))
593                   (error "Topological sort failed due to constraint on ~S."
594                          obj)))
595               (return (nreverse result)))
596              ((endp (rest free-objs))
597               (next-result (pop free-objs)))
598              (t
599               (let ((obj (funcall tie-breaker free-objs result)))
600                 (setf free-objs (remove obj free-objs))
601                 (next-result obj))))))))
602
603
604 ;;; standard class precedence list computation
605 (defun std-compute-class-precedence-list (class)
606   (let ((classes nil)
607         (constraints nil))
608     (labels ((note-class (class)
609                (unless (member class classes)
610                  (push class classes)
611                  (let ((superclasses (classoid-direct-superclasses class)))
612                    (do ((prev class)
613                         (rest superclasses (rest rest)))
614                        ((endp rest))
615                      (let ((next (first rest)))
616                        (push (cons prev next) constraints)
617                        (setf prev next)))
618                    (dolist (class superclasses)
619                      (note-class class)))))
620              (std-cpl-tie-breaker (free-classes rev-cpl)
621                (dolist (class rev-cpl (first free-classes))
622                  (let* ((superclasses (classoid-direct-superclasses class))
623                         (intersection (intersection free-classes
624                                                     superclasses)))
625                    (when intersection
626                      (return (first intersection)))))))
627       (note-class class)
628       (topological-sort classes constraints #'std-cpl-tie-breaker))))
629 \f
630 ;;;; object types to represent classes
631
632 ;;; An UNDEFINED-CLASSOID is a cookie we make up to stick in forward
633 ;;; referenced layouts. Users should never see them.
634 (def!struct (undefined-classoid
635              (:include classoid)
636              (:constructor make-undefined-classoid (name))))
637
638 ;;; BUILT-IN-CLASS is used to represent the standard classes that
639 ;;; aren't defined with DEFSTRUCT and other specially implemented
640 ;;; primitive types whose only attribute is their name.
641 ;;;
642 ;;; Some BUILT-IN-CLASSes have a TRANSLATION, which means that they
643 ;;; are effectively DEFTYPE'd to some other type (usually a union of
644 ;;; other classes or a "primitive" type such as NUMBER, ARRAY, etc.)
645 ;;; This translation is done when type specifiers are parsed. Type
646 ;;; system operations (union, subtypep, etc.) should never encounter
647 ;;; translated classes, only their translation.
648 (def!struct (built-in-classoid (:include classoid)
649                                (:constructor make-built-in-classoid))
650   ;; the type we translate to on parsing. If NIL, then this class
651   ;; stands on its own; or it can be set to :INITIALIZING for a period
652   ;; during cold-load.
653   (translation nil :type (or ctype (member nil :initializing))))
654
655 ;;; STRUCTURE-CLASS represents what we need to know about structure
656 ;;; classes. Non-structure "typed" defstructs are a special case, and
657 ;;; don't have a corresponding class.
658 (def!struct (structure-classoid (:include classoid)
659                                 (:constructor make-structure-classoid))
660   ;; If true, a default keyword constructor for this structure.
661   (constructor nil :type (or function null)))
662 \f
663 ;;;; classoid namespace
664
665 ;;; We use an indirection to allow forward referencing of class
666 ;;; definitions with load-time resolution.
667 (def!struct (classoid-cell
668              (:constructor make-classoid-cell (name &optional classoid))
669              (:make-load-form-fun (lambda (c)
670                                     `(find-classoid-cell
671                                       ',(classoid-cell-name c))))
672              #-no-ansi-print-object
673              (:print-object (lambda (s stream)
674                               (print-unreadable-object (s stream :type t)
675                                 (prin1 (classoid-cell-name s) stream)))))
676   ;; Name of class we expect to find.
677   (name nil :type symbol :read-only t)
678   ;; Class or NIL if not yet defined.
679   (classoid nil :type (or classoid null)))
680 (defun find-classoid-cell (name)
681   (or (info :type :classoid name)
682       (setf (info :type :classoid name)
683             (make-classoid-cell name))))
684
685 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
686 (defun find-classoid (name &optional (errorp t) environment)
687   #!+sb-doc
688   "Return the class with the specified NAME. If ERRORP is false, then
689 NIL is returned when no such class exists."
690   (declare (type symbol name) (ignore environment))
691   (let ((res (classoid-cell-classoid (find-classoid-cell name))))
692     (if (or res (not errorp))
693         res
694         (error 'simple-type-error
695                :datum nil
696                :expected-type 'class
697                :format-control "class not yet defined:~%  ~S"
698                :format-arguments (list name)))))
699 (defun (setf find-classoid) (new-value name)
700   #-sb-xc (declare (type (or null classoid) new-value))
701   (cond
702     ((null new-value)
703      (ecase (info :type :kind name)
704        ((nil))
705        (:defined)
706        (:primitive
707         (error "attempt to redefine :PRIMITIVE type: ~S" name))
708        ((:forthcoming-defclass-type :instance)
709         (setf (info :type :kind name) nil
710               (info :type :classoid name) nil
711               (info :type :documentation name) nil
712               (info :type :compiler-layout name) nil))))
713     (t
714      (ecase (info :type :kind name)
715        ((nil))
716        (:forthcoming-defclass-type
717         ;; XXX Currently, nothing needs to be done in this
718         ;; case. Later, when PCL is integrated tighter into SBCL, this
719         ;; might need more work.
720         nil)
721        (:instance
722         ;; KLUDGE: The reason these clauses aren't directly parallel
723         ;; is that we need to use the internal CLASSOID structure
724         ;; ourselves, because we don't have CLASSes to work with until
725         ;; PCL is built.  In the host, CLASSes have an approximately
726         ;; one-to-one correspondence with the target CLASSOIDs (as
727         ;; well as with the target CLASSes, modulo potential
728         ;; differences with respect to conditions).
729         #+sb-xc-host
730         (let ((old (class-of (find-classoid name)))
731               (new (class-of new-value)))
732           (unless (eq old new)
733             (bug "trying to change the metaclass of ~S from ~S to ~S in the ~
734                   cross-compiler."
735                  name (class-name old) (class-name new))))
736         #-sb-xc-host
737         (let ((old (classoid-of (find-classoid name)))
738               (new (classoid-of new-value)))
739           (unless (eq old new)
740             (warn "changing meta-class of ~S from ~S to ~S"
741                   name (classoid-name old) (classoid-name new)))))
742        (:primitive
743         (error "illegal to redefine standard type ~S" name))
744        (:defined
745            (warn "redefining DEFTYPE type to be a class: ~S" name)
746            (setf (info :type :expander name) nil)))
747
748      (remhash name *forward-referenced-layouts*)
749      (%note-type-defined name)
750      ;; we need to handle things like
751      ;;   (setf (find-class 'foo) (find-class 'integer))
752      ;; and
753      ;;   (setf (find-class 'integer) (find-class 'integer))
754      (cond
755        ((built-in-classoid-p new-value)
756         (setf (info :type :kind name) (or (info :type :kind name) :defined))
757         (let ((translation (built-in-classoid-translation new-value)))
758           (when translation
759             (setf (info :type :translator name)
760                   (lambda (c) (declare (ignore c)) translation)))))
761        (t (setf (info :type :kind name) :instance)))
762      (setf (classoid-cell-classoid (find-classoid-cell name)) new-value)
763      (unless (eq (info :type :compiler-layout name)
764                  (classoid-layout new-value))
765        (setf (info :type :compiler-layout name) (classoid-layout new-value)))))
766   new-value)
767 ) ; EVAL-WHEN
768
769 ;;; Called when we are about to define NAME as a class meeting some
770 ;;; predicate (such as a meta-class type test.) The first result is
771 ;;; always of the desired class. The second result is any existing
772 ;;; LAYOUT for this name.
773 (defun insured-find-classoid (name predicate constructor)
774   (declare (type function predicate constructor))
775   (let* ((old (find-classoid name nil))
776          (res (if (and old (funcall predicate old))
777                   old
778                   (funcall constructor :name name)))
779          (found (or (gethash name *forward-referenced-layouts*)
780                     (when old (classoid-layout old)))))
781     (when found
782       (setf (layout-classoid found) res))
783     (values res found)))
784
785 ;;; If the class has a proper name, return the name, otherwise return
786 ;;; the class.
787 (defun classoid-proper-name (class)
788   #-sb-xc (declare (type classoid class))
789   (let ((name (classoid-name class)))
790     (if (and name (eq (find-classoid name nil) class))
791         name
792         class)))
793 \f
794 ;;;; CLASS type operations
795
796 (!define-type-class classoid)
797
798 ;;; We might be passed classoids with invalid layouts; in any pairwise
799 ;;; class comparison, we must ensure that both are valid before
800 ;;; proceeding.
801 (defun ensure-classoid-valid (classoid layout)
802   (aver (eq classoid (layout-classoid layout)))
803   (when (layout-invalid layout)
804     (if (typep classoid 'standard-classoid)
805         (let ((class (classoid-pcl-class classoid)))
806           (cond
807             ((sb!pcl:class-finalized-p class)
808              (sb!pcl::force-cache-flushes class))
809             ((sb!pcl::class-has-a-forward-referenced-superclass-p class)
810              (error "Invalid, unfinalizeable class ~S (classoid ~S)."
811                     class classoid))
812             (t (sb!pcl:finalize-inheritance class))))
813         (error "Don't know how to ensure validity of ~S (not ~
814                 a STANDARD-CLASSOID)." classoid))))
815
816 (defun ensure-both-classoids-valid (class1 class2)
817   (do ((layout1 (classoid-layout class1) (classoid-layout class1))
818        (layout2 (classoid-layout class2) (classoid-layout class2))
819        (i 0 (+ i 1)))
820       ((and (not (layout-invalid layout1)) (not (layout-invalid layout2))))
821     (aver (< i 2))
822     (ensure-classoid-valid class1 layout1)
823     (ensure-classoid-valid class2 layout2)))
824
825 (defun update-object-layout-or-invalid (object layout)
826   (if (typep (classoid-of object) 'standard-classoid)
827       (sb!pcl::check-wrapper-validity object)
828       (sb!c::%layout-invalid-error object layout)))
829
830 ;;; Simple methods for TYPE= and SUBTYPEP should never be called when
831 ;;; the two classes are equal, since there are EQ checks in those
832 ;;; operations.
833 (!define-type-method (classoid :simple-=) (type1 type2)
834   (aver (not (eq type1 type2)))
835   (values nil t))
836
837 (!define-type-method (classoid :simple-subtypep) (class1 class2)
838   (aver (not (eq class1 class2)))
839   (ensure-both-classoids-valid class1 class2)
840   (let ((subclasses (classoid-subclasses class2)))
841     (if (and subclasses (gethash class1 subclasses))
842         (values t t)
843         (values nil t))))
844
845 ;;; When finding the intersection of a sealed class and some other
846 ;;; class (not hierarchically related) the intersection is the union
847 ;;; of the currently shared subclasses.
848 (defun sealed-class-intersection2 (sealed other)
849   (declare (type classoid sealed other))
850   (let ((s-sub (classoid-subclasses sealed))
851         (o-sub (classoid-subclasses other)))
852     (if (and s-sub o-sub)
853         (collect ((res *empty-type* type-union))
854           (dohash (subclass layout s-sub)
855             (declare (ignore layout))
856             (when (gethash subclass o-sub)
857               (res (specifier-type subclass))))
858           (res))
859         *empty-type*)))
860
861 (!define-type-method (classoid :simple-intersection2) (class1 class2)
862   (declare (type classoid class1 class2))
863   (ensure-both-classoids-valid class1 class2)
864   (cond ((eq class1 class2)
865          class1)
866         ;; If one is a subclass of the other, then that is the
867         ;; intersection.
868         ((let ((subclasses (classoid-subclasses class2)))
869            (and subclasses (gethash class1 subclasses)))
870          class1)
871         ((let ((subclasses (classoid-subclasses class1)))
872            (and subclasses (gethash class2 subclasses)))
873          class2)
874         ;; Otherwise, we can't in general be sure that the
875         ;; intersection is empty, since a subclass of both might be
876         ;; defined. But we can eliminate it for some special cases.
877         ((or (structure-classoid-p class1)
878              (structure-classoid-p class2))
879          ;; No subclass of both can be defined.
880          *empty-type*)
881         ((eq (classoid-state class1) :sealed)
882          ;; checking whether a subclass of both can be defined:
883          (sealed-class-intersection2 class1 class2))
884         ((eq (classoid-state class2) :sealed)
885          ;; checking whether a subclass of both can be defined:
886          (sealed-class-intersection2 class2 class1))
887         (t
888          ;; uncertain, since a subclass of both might be defined
889          nil)))
890
891 ;;; KLUDGE: we need this to deal with the special-case INSTANCE and
892 ;;; FUNCALLABLE-INSTANCE types (which used to be CLASSOIDs until CSR
893 ;;; discovered that this was incompatible with the MOP class
894 ;;; hierarchy).  See NAMED :COMPLEX-SUBTYPEP-ARG2
895 (defvar *non-instance-classoid-types*
896   '(symbol system-area-pointer weak-pointer code-component
897     lra fdefn random-class))
898
899 ;;; KLUDGE: we need this because of the need to represent
900 ;;; intersections of two classes, even when empty at a given time, as
901 ;;; uncanonicalized intersections because of the possibility of later
902 ;;; defining a subclass of both classes.  The necessity for changing
903 ;;; the default return value from SUBTYPEP to NIL, T if no alternate
904 ;;; method is present comes about because, unlike the other places we
905 ;;; use INVOKE-COMPLEX-SUBTYPEP-ARG1-METHOD, in HAIRY methods and the
906 ;;; like, classes are in their own hierarchy with no possibility of
907 ;;; mixtures with other type classes.
908 (!define-type-method (classoid :complex-subtypep-arg2) (type1 class2)
909   (if (and (intersection-type-p type1)
910            (> (count-if #'classoid-p (intersection-type-types type1)) 1))
911       (values nil nil)
912       (invoke-complex-subtypep-arg1-method type1 class2 nil t)))
913
914 (!define-type-method (classoid :negate) (type)
915   (make-negation-type :type type))
916
917 (!define-type-method (classoid :unparse) (type)
918   (classoid-proper-name type))
919 \f
920 ;;;; PCL stuff
921
922 ;;; the CLASSOID that we use to represent type information for
923 ;;; STANDARD-CLASS and FUNCALLABLE-STANDARD-CLASS.  The type system
924 ;;; side does not need to distinguish between STANDARD-CLASS and
925 ;;; FUNCALLABLE-STANDARD-CLASS.
926 (def!struct (standard-classoid (:include classoid)
927                                (:constructor make-standard-classoid)))
928 ;;; a metaclass for classes which aren't standardlike but will never
929 ;;; change either.
930 (def!struct (static-classoid (:include classoid)
931                              (:constructor make-static-classoid)))
932 \f
933 ;;;; built-in classes
934
935 ;;; The BUILT-IN-CLASSES list is a data structure which configures the
936 ;;; creation of all the built-in classes. It contains all the info
937 ;;; that we need to maintain the mapping between classes, compile-time
938 ;;; types and run-time type codes. These options are defined:
939 ;;;
940 ;;; :TRANSLATION (default none)
941 ;;;     When this class is "parsed" as a type specifier, it is
942 ;;;     translated into the specified internal type representation,
943 ;;;     rather than being left as a class. This is used for types
944 ;;;     which we want to canonicalize to some other kind of type
945 ;;;     object because in general we want to be able to include more
946 ;;;     information than just the class (e.g. for numeric types.)
947 ;;;
948 ;;; :ENUMERABLE (default NIL)
949 ;;;     The value of the :ENUMERABLE slot in the created class.
950 ;;;     Meaningless in translated classes.
951 ;;;
952 ;;; :STATE (default :SEALED)
953 ;;;     The value of CLASS-STATE which we want on completion,
954 ;;;     indicating whether subclasses can be created at run-time.
955 ;;;
956 ;;; :HIERARCHICAL-P (default T unless any of the inherits are non-hierarchical)
957 ;;;     True if we can assign this class a unique inheritance depth.
958 ;;;
959 ;;; :CODES (default none)
960 ;;;     Run-time type codes which should be translated back to this
961 ;;;     class by CLASS-OF. Unspecified for abstract classes.
962 ;;;
963 ;;; :INHERITS (default this class and T)
964 ;;;     The class-precedence list for this class, with this class and
965 ;;;     T implicit.
966 ;;;
967 ;;; :DIRECT-SUPERCLASSES (default to head of CPL)
968 ;;;     List of the direct superclasses of this class.
969 ;;;
970 ;;; FIXME: This doesn't seem to be needed after cold init (and so can
971 ;;; probably be uninterned at the end of cold init).
972 (defvar *built-in-classes*)
973 (!cold-init-forms
974   (/show0 "setting *BUILT-IN-CLASSES*")
975   (setq
976    *built-in-classes*
977    '((t :state :read-only :translation t)
978      (character :enumerable t
979                 :codes (#.sb!vm:character-widetag)
980                 :translation (character-set)
981                 :prototype-form (code-char 42))
982      (symbol :codes (#.sb!vm:symbol-header-widetag)
983              :prototype-form '#:mu)
984
985      (system-area-pointer :codes (#.sb!vm:sap-widetag)
986                           :prototype-form (sb!sys:int-sap 42))
987      (weak-pointer :codes (#.sb!vm:weak-pointer-widetag)
988       :prototype-form (sb!ext:make-weak-pointer (find-package "CL")))
989      (code-component :codes (#.sb!vm:code-header-widetag))
990      (lra :codes (#.sb!vm:return-pc-header-widetag))
991      (fdefn :codes (#.sb!vm:fdefn-widetag)
992             :prototype-form (sb!kernel:make-fdefn "42"))
993      (random-class) ; used for unknown type codes
994
995      (function
996       :codes (#.sb!vm:closure-header-widetag
997               #.sb!vm:simple-fun-header-widetag)
998       :state :read-only
999       :prototype-form (function (lambda () 42)))
1000
1001      (number :translation number)
1002      (complex
1003       :translation complex
1004       :inherits (number)
1005       :codes (#.sb!vm:complex-widetag)
1006       :prototype-form (complex 42 42))
1007      (complex-single-float
1008       :translation (complex single-float)
1009       :inherits (complex number)
1010       :codes (#.sb!vm:complex-single-float-widetag)
1011       :prototype-form (complex 42f0 42f0))
1012      (complex-double-float
1013       :translation (complex double-float)
1014       :inherits (complex number)
1015       :codes (#.sb!vm:complex-double-float-widetag)
1016       :prototype-form (complex 42d0 42d0))
1017      #!+long-float
1018      (complex-long-float
1019       :translation (complex long-float)
1020       :inherits (complex number)
1021       :codes (#.sb!vm:complex-long-float-widetag)
1022       :prototype-form (complex 42l0 42l0))
1023      (real :translation real :inherits (number))
1024      (float
1025       :translation float
1026       :inherits (real number))
1027      (single-float
1028       :translation single-float
1029       :inherits (float real number)
1030       :codes (#.sb!vm:single-float-widetag)
1031       :prototype-form 42f0)
1032      (double-float
1033       :translation double-float
1034       :inherits (float real number)
1035       :codes (#.sb!vm:double-float-widetag)
1036       :prototype-form 42d0)
1037      #!+long-float
1038      (long-float
1039       :translation long-float
1040       :inherits (float real number)
1041       :codes (#.sb!vm:long-float-widetag)
1042       :prototype-form 42l0)
1043      (rational
1044       :translation rational
1045       :inherits (real number))
1046      (ratio
1047       :translation (and rational (not integer))
1048       :inherits (rational real number)
1049       :codes (#.sb!vm:ratio-widetag)
1050       :prototype-form 1/42)
1051      (integer
1052       :translation integer
1053       :inherits (rational real number))
1054      (fixnum
1055       :translation (integer #.sb!xc:most-negative-fixnum
1056                     #.sb!xc:most-positive-fixnum)
1057       :inherits (integer rational real number)
1058       :codes (#.sb!vm:even-fixnum-lowtag #.sb!vm:odd-fixnum-lowtag)
1059       :prototype-form 42)
1060      (bignum
1061       :translation (and integer (not fixnum))
1062       :inherits (integer rational real number)
1063       :codes (#.sb!vm:bignum-widetag)
1064       :prototype-form (expt 2 #.(* sb!vm:n-word-bits (/ 3 2))))
1065
1066      (array :translation array :codes (#.sb!vm:complex-array-widetag)
1067             :hierarchical-p nil
1068             :prototype-form (make-array nil :adjustable t))
1069      (simple-array
1070       :translation simple-array :codes (#.sb!vm:simple-array-widetag)
1071       :inherits (array)
1072       :prototype-form (make-array nil))
1073      (sequence
1074       :translation (or cons (member nil) vector extended-sequence)
1075       :state :read-only
1076       :depth 2)
1077      (vector
1078       :translation vector :codes (#.sb!vm:complex-vector-widetag)
1079       :direct-superclasses (array sequence)
1080       :inherits (array sequence))
1081      (simple-vector
1082       :translation simple-vector :codes (#.sb!vm:simple-vector-widetag)
1083       :direct-superclasses (vector simple-array)
1084       :inherits (vector simple-array array sequence)
1085       :prototype-form (make-array 0))
1086      (bit-vector
1087       :translation bit-vector :codes (#.sb!vm:complex-bit-vector-widetag)
1088       :inherits (vector array sequence)
1089       :prototype-form (make-array 0 :element-type 'bit :fill-pointer t))
1090      (simple-bit-vector
1091       :translation simple-bit-vector :codes (#.sb!vm:simple-bit-vector-widetag)
1092       :direct-superclasses (bit-vector simple-array)
1093       :inherits (bit-vector vector simple-array
1094                  array sequence)
1095       :prototype-form (make-array 0 :element-type 'bit))
1096      (simple-array-unsigned-byte-2
1097       :translation (simple-array (unsigned-byte 2) (*))
1098       :codes (#.sb!vm:simple-array-unsigned-byte-2-widetag)
1099       :direct-superclasses (vector simple-array)
1100       :inherits (vector simple-array array sequence)
1101       :prototype-form (make-array 0 :element-type '(unsigned-byte 2)))
1102      (simple-array-unsigned-byte-4
1103       :translation (simple-array (unsigned-byte 4) (*))
1104       :codes (#.sb!vm:simple-array-unsigned-byte-4-widetag)
1105       :direct-superclasses (vector simple-array)
1106       :inherits (vector simple-array array sequence)
1107       :prototype-form (make-array 0 :element-type '(unsigned-byte 4)))
1108      (simple-array-unsigned-byte-7
1109       :translation (simple-array (unsigned-byte 7) (*))
1110       :codes (#.sb!vm:simple-array-unsigned-byte-7-widetag)
1111       :direct-superclasses (vector simple-array)
1112       :inherits (vector simple-array array sequence)
1113       :prototype-form (make-array 0 :element-type '(unsigned-byte 7)))
1114      (simple-array-unsigned-byte-8
1115       :translation (simple-array (unsigned-byte 8) (*))
1116       :codes (#.sb!vm:simple-array-unsigned-byte-8-widetag)
1117       :direct-superclasses (vector simple-array)
1118       :inherits (vector simple-array array sequence)
1119       :prototype-form (make-array 0 :element-type '(unsigned-byte 8)))
1120      (simple-array-unsigned-byte-15
1121       :translation (simple-array (unsigned-byte 15) (*))
1122       :codes (#.sb!vm:simple-array-unsigned-byte-15-widetag)
1123       :direct-superclasses (vector simple-array)
1124       :inherits (vector simple-array array sequence)
1125       :prototype-form (make-array 0 :element-type '(unsigned-byte 15)))
1126      (simple-array-unsigned-byte-16
1127       :translation (simple-array (unsigned-byte 16) (*))
1128       :codes (#.sb!vm:simple-array-unsigned-byte-16-widetag)
1129       :direct-superclasses (vector simple-array)
1130       :inherits (vector simple-array array sequence)
1131       :prototype-form (make-array 0 :element-type '(unsigned-byte 16)))
1132      #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
1133      (simple-array-unsigned-byte-29
1134       :translation (simple-array (unsigned-byte 29) (*))
1135       :codes (#.sb!vm:simple-array-unsigned-byte-29-widetag)
1136       :direct-superclasses (vector simple-array)
1137       :inherits (vector simple-array array sequence)
1138       :prototype-form (make-array 0 :element-type '(unsigned-byte 29)))
1139      (simple-array-unsigned-byte-31
1140       :translation (simple-array (unsigned-byte 31) (*))
1141       :codes (#.sb!vm:simple-array-unsigned-byte-31-widetag)
1142       :direct-superclasses (vector simple-array)
1143       :inherits (vector simple-array array sequence)
1144       :prototype-form (make-array 0 :element-type '(unsigned-byte 31)))
1145      (simple-array-unsigned-byte-32
1146       :translation (simple-array (unsigned-byte 32) (*))
1147       :codes (#.sb!vm:simple-array-unsigned-byte-32-widetag)
1148       :direct-superclasses (vector simple-array)
1149       :inherits (vector simple-array array sequence)
1150       :prototype-form (make-array 0 :element-type '(unsigned-byte 32)))
1151      #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1152      (simple-array-unsigned-byte-60
1153       :translation (simple-array (unsigned-byte 60) (*))
1154       :codes (#.sb!vm:simple-array-unsigned-byte-60-widetag)
1155       :direct-superclasses (vector simple-array)
1156       :inherits (vector simple-array array sequence)
1157       :prototype-form (make-array 0 :element-type '(unsigned-byte 60)))
1158      #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1159      (simple-array-unsigned-byte-63
1160       :translation (simple-array (unsigned-byte 63) (*))
1161       :codes (#.sb!vm:simple-array-unsigned-byte-63-widetag)
1162       :direct-superclasses (vector simple-array)
1163       :inherits (vector simple-array array sequence)
1164       :prototype-form (make-array 0 :element-type '(unsigned-byte 63)))
1165      #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1166      (simple-array-unsigned-byte-64
1167       :translation (simple-array (unsigned-byte 64) (*))
1168       :codes (#.sb!vm:simple-array-unsigned-byte-64-widetag)
1169       :direct-superclasses (vector simple-array)
1170       :inherits (vector simple-array array sequence)
1171       :prototype-form (make-array 0 :element-type '(unsigned-byte 64)))
1172      (simple-array-signed-byte-8
1173       :translation (simple-array (signed-byte 8) (*))
1174       :codes (#.sb!vm:simple-array-signed-byte-8-widetag)
1175       :direct-superclasses (vector simple-array)
1176       :inherits (vector simple-array array sequence)
1177       :prototype-form (make-array 0 :element-type '(signed-byte 8)))
1178      (simple-array-signed-byte-16
1179       :translation (simple-array (signed-byte 16) (*))
1180       :codes (#.sb!vm:simple-array-signed-byte-16-widetag)
1181       :direct-superclasses (vector simple-array)
1182       :inherits (vector simple-array array sequence)
1183       :prototype-form (make-array 0 :element-type '(signed-byte 16)))
1184      #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
1185      (simple-array-signed-byte-30
1186       :translation (simple-array (signed-byte 30) (*))
1187       :codes (#.sb!vm:simple-array-signed-byte-30-widetag)
1188       :direct-superclasses (vector simple-array)
1189       :inherits (vector simple-array array sequence)
1190       :prototype-form (make-array 0 :element-type '(signed-byte 30)))
1191      (simple-array-signed-byte-32
1192       :translation (simple-array (signed-byte 32) (*))
1193       :codes (#.sb!vm:simple-array-signed-byte-32-widetag)
1194       :direct-superclasses (vector simple-array)
1195       :inherits (vector simple-array array sequence)
1196       :prototype-form (make-array 0 :element-type '(signed-byte 32)))
1197      #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1198      (simple-array-signed-byte-61
1199       :translation (simple-array (signed-byte 61) (*))
1200       :codes (#.sb!vm:simple-array-signed-byte-61-widetag)
1201       :direct-superclasses (vector simple-array)
1202       :inherits (vector simple-array array sequence)
1203       :prototype-form (make-array 0 :element-type '(signed-byte 61)))
1204      #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1205      (simple-array-signed-byte-64
1206       :translation (simple-array (signed-byte 64) (*))
1207       :codes (#.sb!vm:simple-array-signed-byte-64-widetag)
1208       :direct-superclasses (vector simple-array)
1209       :inherits (vector simple-array array sequence)
1210       :prototype-form (make-array 0 :element-type '(signed-byte 64)))
1211      (simple-array-single-float
1212       :translation (simple-array single-float (*))
1213       :codes (#.sb!vm:simple-array-single-float-widetag)
1214       :direct-superclasses (vector simple-array)
1215       :inherits (vector simple-array array sequence)
1216       :prototype-form (make-array 0 :element-type 'single-float))
1217      (simple-array-double-float
1218       :translation (simple-array double-float (*))
1219       :codes (#.sb!vm:simple-array-double-float-widetag)
1220       :direct-superclasses (vector simple-array)
1221       :inherits (vector simple-array array sequence)
1222       :prototype-form (make-array 0 :element-type 'double-float))
1223      #!+long-float
1224      (simple-array-long-float
1225       :translation (simple-array long-float (*))
1226       :codes (#.sb!vm:simple-array-long-float-widetag)
1227       :direct-superclasses (vector simple-array)
1228       :inherits (vector simple-array array sequence)
1229       :prototype-form (make-array 0 :element-type 'long-float))
1230      (simple-array-complex-single-float
1231       :translation (simple-array (complex single-float) (*))
1232       :codes (#.sb!vm:simple-array-complex-single-float-widetag)
1233       :direct-superclasses (vector simple-array)
1234       :inherits (vector simple-array array sequence)
1235       :prototype-form (make-array 0 :element-type '(complex single-float)))
1236      (simple-array-complex-double-float
1237       :translation (simple-array (complex double-float) (*))
1238       :codes (#.sb!vm:simple-array-complex-double-float-widetag)
1239       :direct-superclasses (vector simple-array)
1240       :inherits (vector simple-array array sequence)
1241       :prototype-form (make-array 0 :element-type '(complex double-float)))
1242      #!+long-float
1243      (simple-array-complex-long-float
1244       :translation (simple-array (complex long-float) (*))
1245       :codes (#.sb!vm:simple-array-complex-long-float-widetag)
1246       :direct-superclasses (vector simple-array)
1247       :inherits (vector simple-array array sequence)
1248       :prototype-form (make-array 0 :element-type '(complex long-float)))
1249      (string
1250       :translation string
1251       :direct-superclasses (vector)
1252       :inherits (vector array sequence))
1253      (simple-string
1254       :translation simple-string
1255       :direct-superclasses (string simple-array)
1256       :inherits (string vector simple-array array sequence))
1257      (vector-nil
1258       :translation (vector nil)
1259       :codes (#.sb!vm:complex-vector-nil-widetag)
1260       :direct-superclasses (string)
1261       :inherits (string vector array sequence)
1262       :prototype-form (make-array 0 :element-type 'nil :fill-pointer t))
1263      (simple-array-nil
1264       :translation (simple-array nil (*))
1265       :codes (#.sb!vm:simple-array-nil-widetag)
1266       :direct-superclasses (vector-nil simple-string)
1267       :inherits (vector-nil simple-string string vector simple-array
1268                  array sequence)
1269       :prototype-form (make-array 0 :element-type 'nil))
1270      (base-string
1271       :translation base-string
1272       :codes (#.sb!vm:complex-base-string-widetag)
1273       :direct-superclasses (string)
1274       :inherits (string vector array sequence)
1275       :prototype-form (make-array 0 :element-type 'base-char :fill-pointer t))
1276      (simple-base-string
1277       :translation simple-base-string
1278       :codes (#.sb!vm:simple-base-string-widetag)
1279       :direct-superclasses (base-string simple-string)
1280       :inherits (base-string simple-string string vector simple-array
1281                  array sequence)
1282       :prototype-form (make-array 0 :element-type 'base-char))
1283      #!+sb-unicode
1284      (character-string
1285       :translation (vector character)
1286       :codes (#.sb!vm:complex-character-string-widetag)
1287       :direct-superclasses (string)
1288       :inherits (string vector array sequence)
1289       :prototype-form (make-array 0 :element-type 'character :fill-pointer t))
1290      #!+sb-unicode
1291      (simple-character-string
1292       :translation (simple-array character (*))
1293       :codes (#.sb!vm:simple-character-string-widetag)
1294       :direct-superclasses (character-string simple-string)
1295       :inherits (character-string simple-string string vector simple-array
1296                  array sequence)
1297       :prototype-form (make-array 0 :element-type 'character))
1298      (list
1299       :translation (or cons (member nil))
1300       :inherits (sequence))
1301      (cons
1302       :codes (#.sb!vm:list-pointer-lowtag)
1303       :translation cons
1304       :inherits (list sequence)
1305       :prototype-form (cons nil nil))
1306      (null
1307       :translation (member nil)
1308       :inherits (symbol list sequence)
1309       :direct-superclasses (symbol list)
1310       :prototype-form 'nil)
1311      (stream
1312       :state :read-only
1313       :depth 2)
1314      (file-stream
1315       :state :read-only
1316       :depth 4
1317       :inherits (stream))
1318      (string-stream
1319       :state :read-only
1320       :depth 4
1321       :inherits (stream)))))
1322
1323 ;;; See also src/code/class-init.lisp where we finish setting up the
1324 ;;; translations for built-in types.
1325 (!cold-init-forms
1326   (dolist (x *built-in-classes*)
1327     #-sb-xc-host (/show0 "at head of loop over *BUILT-IN-CLASSES*")
1328     (destructuring-bind
1329         (name &key
1330               (translation nil trans-p)
1331               inherits
1332               codes
1333               enumerable
1334               state
1335               depth
1336               prototype-form
1337               (hierarchical-p t) ; might be modified below
1338               (direct-superclasses (if inherits
1339                                      (list (car inherits))
1340                                      '(t))))
1341         x
1342       (declare (ignore codes state translation prototype-form))
1343       (let ((inherits-list (if (eq name t)
1344                                ()
1345                                (cons t (reverse inherits))))
1346             (classoid (make-built-in-classoid
1347                        :enumerable enumerable
1348                        :name name
1349                        :translation (if trans-p :initializing nil)
1350                        :direct-superclasses
1351                        (if (eq name t)
1352                            nil
1353                            (mapcar #'find-classoid direct-superclasses)))))
1354         (setf (info :type :kind name) #+sb-xc-host :defined #-sb-xc-host :primitive
1355               (classoid-cell-classoid (find-classoid-cell name)) classoid)
1356         (unless trans-p
1357           (setf (info :type :builtin name) classoid))
1358         (let* ((inherits-vector
1359                 (map 'simple-vector
1360                      (lambda (x)
1361                        (let ((super-layout
1362                               (classoid-layout (find-classoid x))))
1363                          (when (minusp (layout-depthoid super-layout))
1364                            (setf hierarchical-p nil))
1365                          super-layout))
1366                      inherits-list))
1367                (depthoid (if hierarchical-p
1368                            (or depth (length inherits-vector))
1369                            -1)))
1370           (register-layout
1371            (find-and-init-or-check-layout name
1372                                           0
1373                                           inherits-vector
1374                                           depthoid
1375                                           0)
1376            :invalidate nil)))))
1377   (/show0 "done with loop over *BUILT-IN-CLASSES*"))
1378
1379 ;;; Define temporary PCL STANDARD-CLASSes. These will be set up
1380 ;;; correctly and the Lisp layout replaced by a PCL wrapper after PCL
1381 ;;; is loaded and the class defined.
1382 (!cold-init-forms
1383   (/show0 "about to define temporary STANDARD-CLASSes")
1384   (dolist (x '(;; Why is STREAM duplicated in this list? Because, when
1385                ;; the inherits-vector of FUNDAMENTAL-STREAM is set up,
1386                ;; a vector containing the elements of the list below,
1387                ;; i.e. '(T STREAM STREAM), is created, and
1388                ;; this is what the function ORDER-LAYOUT-INHERITS
1389                ;; would do, too.
1390                ;;
1391                ;; So, the purpose is to guarantee a valid layout for
1392                ;; the FUNDAMENTAL-STREAM class, matching what
1393                ;; ORDER-LAYOUT-INHERITS would do.
1394                ;; ORDER-LAYOUT-INHERITS would place STREAM at index 2
1395                ;; in the INHERITS(-VECTOR). Index 1 would not be
1396                ;; filled, so STREAM is duplicated there (as
1397                ;; ORDER-LAYOUTS-INHERITS would do). Maybe the
1398                ;; duplicate definition could be removed (removing a
1399                ;; STREAM element), because FUNDAMENTAL-STREAM is
1400                ;; redefined after PCL is set up, anyway. But to play
1401                ;; it safely, we define the class with a valid INHERITS
1402                ;; vector.
1403                (fundamental-stream (t stream stream))))
1404     (/show0 "defining temporary STANDARD-CLASS")
1405     (let* ((name (first x))
1406            (inherits-list (second x))
1407            (classoid (make-standard-classoid :name name))
1408            (classoid-cell (find-classoid-cell name)))
1409       ;; Needed to open-code the MAP, below
1410       (declare (type list inherits-list))
1411       (setf (classoid-cell-classoid classoid-cell) classoid
1412             (info :type :classoid name) classoid-cell
1413             (info :type :kind name) :instance)
1414       (let ((inherits (map 'simple-vector
1415                            (lambda (x)
1416                              (classoid-layout (find-classoid x)))
1417                            inherits-list)))
1418         #-sb-xc-host (/show0 "INHERITS=..") #-sb-xc-host (/hexstr inherits)
1419         (register-layout (find-and-init-or-check-layout name 0 inherits -1 0)
1420                          :invalidate nil))))
1421   (/show0 "done defining temporary STANDARD-CLASSes"))
1422
1423 ;;; Now that we have set up the class heterarchy, seal the sealed
1424 ;;; classes. This must be done after the subclasses have been set up.
1425 (!cold-init-forms
1426   (dolist (x *built-in-classes*)
1427     (destructuring-bind (name &key (state :sealed) &allow-other-keys) x
1428       (setf (classoid-state (find-classoid name)) state))))
1429 \f
1430 ;;;; class definition/redefinition
1431
1432 ;;; This is to be called whenever we are altering a class.
1433 (defun modify-classoid (classoid)
1434   (clear-type-caches)
1435   (when (member (classoid-state classoid) '(:read-only :frozen))
1436     ;; FIXME: This should probably be CERROR.
1437     (warn "making ~(~A~) class ~S writable"
1438           (classoid-state classoid)
1439           (classoid-name classoid))
1440     (setf (classoid-state classoid) nil)))
1441
1442 ;;; Mark LAYOUT as invalid. Setting DEPTHOID -1 helps cause unsafe
1443 ;;; structure type tests to fail. Remove class from all superclasses
1444 ;;; too (might not be registered, so might not be in subclasses of the
1445 ;;; nominal superclasses.)  We set the layout-clos-hash slots to 0 to
1446 ;;; invalidate the wrappers for specialized dispatch functions, which
1447 ;;; use those slots as indexes into tables.
1448 (defun invalidate-layout (layout)
1449   (declare (type layout layout))
1450   (setf (layout-invalid layout) t
1451         (layout-depthoid layout) -1)
1452   (setf (layout-clos-hash layout) 0)
1453   (let ((inherits (layout-inherits layout))
1454         (classoid (layout-classoid layout)))
1455     (modify-classoid classoid)
1456     (dovector (super inherits)
1457       (let ((subs (classoid-subclasses (layout-classoid super))))
1458         (when subs
1459           (remhash classoid subs)))))
1460   (values))
1461 \f
1462 ;;;; cold loading initializations
1463
1464 ;;; FIXME: It would be good to arrange for this to be called when the
1465 ;;; cross-compiler is being built, not just when the target Lisp is
1466 ;;; being cold loaded. Perhaps this could be moved to its own file
1467 ;;; late in the build-order.lisp-expr sequence, and be put in
1468 ;;; !COLD-INIT-FORMS there?
1469 (defun !class-finalize ()
1470   (dohash (name layout *forward-referenced-layouts*)
1471     (let ((class (find-classoid name nil)))
1472       (cond ((not class)
1473              (setf (layout-classoid layout) (make-undefined-classoid name)))
1474             ((eq (classoid-layout class) layout)
1475              (remhash name *forward-referenced-layouts*))
1476             (t
1477              ;; FIXME: ERROR?
1478              (warn "something strange with forward layout for ~S:~%  ~S"
1479                    name
1480                    layout))))))
1481
1482 (!cold-init-forms
1483   #-sb-xc-host (/show0 "about to set *BUILT-IN-CLASS-CODES*")
1484   (setq *built-in-class-codes*
1485         (let* ((initial-element
1486                 (locally
1487                   ;; KLUDGE: There's a FIND-CLASSOID DEFTRANSFORM for
1488                   ;; constant class names which creates fast but
1489                   ;; non-cold-loadable, non-compact code. In this
1490                   ;; context, we'd rather have compact, cold-loadable
1491                   ;; code. -- WHN 19990928
1492                   (declare (notinline find-classoid))
1493                   (classoid-layout (find-classoid 'random-class))))
1494                (res (make-array 256 :initial-element initial-element)))
1495           (dolist (x *built-in-classes* res)
1496             (destructuring-bind (name &key codes &allow-other-keys)
1497                                 x
1498               (let ((layout (classoid-layout (find-classoid name))))
1499                 (dolist (code codes)
1500                   (setf (svref res code) layout)))))))
1501   (setq *null-classoid-layout*
1502         ;; KLUDGE: we use (LET () ...) instead of a LOCALLY here to
1503         ;; work around a bug in the LOCALLY handling in the fopcompiler
1504         ;; (present in 0.9.13-0.9.14.18). -- JES, 2006-07-16
1505         (let ()
1506           (declare (notinline find-classoid))
1507           (classoid-layout (find-classoid 'null))))
1508   #-sb-xc-host (/show0 "done setting *BUILT-IN-CLASS-CODES*"))
1509 \f
1510 (!defun-from-collected-cold-init-forms !classes-cold-init)