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