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