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