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