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