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