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