Make globaldb's mapping from a CLOS specializer to its CTYPE transparent.
[sbcl.git] / src / code / class.lisp
blob8cc95a6513862b829bfacf906eddbb8aab1b2967
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)))
704 (defun make-structure-classoid (&key name)
705 (mark-ctype-interned (%make-structure-classoid :name name)))
707 ;;;; classoid namespace
709 ;;; We use an indirection to allow forward referencing of class
710 ;;; definitions with load-time resolution.
711 (def!struct (classoid-cell
712 (:constructor make-classoid-cell (name &optional classoid))
713 (:make-load-form-fun (lambda (c)
714 `(find-classoid-cell
715 ',(classoid-cell-name c)
716 :create t)))
717 #-no-ansi-print-object
718 (:print-object (lambda (s stream)
719 (print-unreadable-object (s stream :type t)
720 (prin1 (classoid-cell-name s) stream)))))
721 ;; Name of class we expect to find.
722 (name nil :type symbol :read-only t)
723 ;; Classoid or NIL if not yet defined.
724 (classoid nil :type (or classoid null))
725 ;; PCL class, if any
726 (pcl-class nil))
727 (declaim (freeze-type classoid-cell))
729 (defun find-classoid-cell (name &key create)
730 (let ((real-name (uncross name)))
731 (cond ((info :type :classoid-cell real-name))
732 (create
733 (get-info-value-initializing :type :classoid-cell real-name
734 (make-classoid-cell real-name))))))
736 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
738 ;; Return the classoid with the specified NAME. If ERRORP is false,
739 ;; then NIL is returned when no such class exists."
740 (defun find-classoid (name &optional (errorp t))
741 (declare (type symbol name))
742 (let ((cell (find-classoid-cell name)))
743 (cond ((and cell (classoid-cell-classoid cell)))
744 (errorp
745 (error 'simple-type-error
746 :datum nil
747 :expected-type 'class
748 :format-control "Class not yet defined: ~S"
749 :format-arguments (list name))))))
751 (defun (setf find-classoid) (new-value name)
752 #-sb-xc (declare (type (or null classoid) new-value))
753 (aver new-value)
754 (let ((table *forward-referenced-layouts*))
755 (with-world-lock ()
756 (let ((cell (find-classoid-cell name :create t)))
757 (ecase (info :type :kind name)
758 ((nil))
759 (:forthcoming-defclass-type
760 ;; FIXME: Currently, nothing needs to be done in this case.
761 ;; Later, when PCL is integrated tighter into SBCL, this
762 ;; might need more work.
763 nil)
764 (:instance
765 (aver cell)
766 (let ((old-value (classoid-cell-classoid cell)))
767 (aver old-value)
768 ;; KLUDGE: The reason these clauses aren't directly
769 ;; parallel is that we need to use the internal
770 ;; CLASSOID structure ourselves, because we don't
771 ;; have CLASSes to work with until PCL is built. In
772 ;; the host, CLASSes have an approximately
773 ;; one-to-one correspondence with the target
774 ;; CLASSOIDs (as well as with the target CLASSes,
775 ;; modulo potential differences with respect to
776 ;; conditions).
777 #+sb-xc-host
778 (let ((old (class-of old-value))
779 (new (class-of new-value)))
780 (unless (eq old new)
781 (bug "Trying to change the metaclass of ~S from ~S to ~S in the ~
782 cross-compiler."
783 name (class-name old) (class-name new))))
784 #-sb-xc-host
785 (let ((old (classoid-of old-value))
786 (new (classoid-of new-value)))
787 (unless (eq old new)
788 (warn "Changing meta-class of ~S from ~S to ~S."
789 name (classoid-name old) (classoid-name new))))))
790 (:primitive
791 (error "Cannot redefine standard type ~S." name))
792 (:defined
793 (warn "redefining DEFTYPE type to be a class: ~
794 ~/sb-impl::print-symbol-with-prefix/" name)
795 (clear-info :type :expander name)
796 (clear-info :type :lambda-list name)
797 (clear-info :type :source-location name)))
799 (remhash name table)
800 (%note-type-defined name)
801 ;; we need to handle things like
802 ;; (setf (find-class 'foo) (find-class 'integer))
803 ;; and
804 ;; (setf (find-class 'integer) (find-class 'integer))
805 (cond ((built-in-classoid-p new-value)
806 (setf (info :type :kind name)
807 (or (info :type :kind name) :defined))
808 (let ((translation (built-in-classoid-translation new-value)))
809 (when translation
810 (setf (info :type :translator name) translation))))
812 (setf (info :type :kind name) :instance)))
813 (setf (classoid-cell-classoid cell) new-value)
814 (unless (eq (info :type :compiler-layout name)
815 (classoid-layout new-value))
816 (setf (info :type :compiler-layout name)
817 (classoid-layout new-value))))))
818 new-value)
820 (defun %clear-classoid (name cell)
821 (ecase (info :type :kind name)
822 ((nil))
823 (:defined)
824 (:primitive
825 (error "Attempt to remove :PRIMITIVE type: ~S" name))
826 ((:forthcoming-defclass-type :instance)
827 (when cell
828 ;; Note: We cannot remove the classoid cell from the table,
829 ;; since compiled code may refer directly to the cell, and
830 ;; getting a different cell for a classoid with the same name
831 ;; just would not do.
833 ;; Remove the proper name of the classoid, if this was it.
834 (let* ((classoid (classoid-cell-classoid cell))
835 (proper-name (classoid-name classoid)))
836 (when (eq proper-name name)
837 (setf (classoid-name classoid) nil)))
839 ;; Clear the cell.
840 (setf (classoid-cell-classoid cell) nil
841 (classoid-cell-pcl-class cell) nil))
842 (clear-info :type :kind name)
843 (clear-info :type :documentation name)
844 (clear-info :type :compiler-layout name)))))
846 ;;; Called when we are about to define NAME as a class meeting some
847 ;;; predicate (such as a meta-class type test.) The first result is
848 ;;; always of the desired class. The second result is any existing
849 ;;; LAYOUT for this name.
851 ;;; Again, this should be compiler-only, but easier to make this
852 ;;; thread-safe.
853 (defun insured-find-classoid (name predicate constructor)
854 (declare (type function predicate constructor))
855 (let ((table *forward-referenced-layouts*))
856 (with-locked-system-table (table)
857 (let* ((old (find-classoid name nil))
858 (res (if (and old (funcall predicate old))
860 (funcall constructor :name name)))
861 (found (or (gethash name table)
862 (when old (classoid-layout old)))))
863 (when found
864 (setf (layout-classoid found) res))
865 (values res found)))))
867 ;;; If the classoid has a proper name, return the name, otherwise return
868 ;;; the classoid.
869 (defun classoid-proper-name (classoid)
870 #-sb-xc (declare (type classoid classoid))
871 (let ((name (classoid-name classoid)))
872 (if (and name (eq (find-classoid name nil) classoid))
873 name
874 classoid)))
876 ;;;; CLASS type operations
878 ;; referenced right away by !DEFINE-TYPE-CLASS.
879 (eval-when (:compile-toplevel :load-toplevel :execute)
880 ;; Actually this definition makes very little sense because
881 ;; (TYPE-ENUMERABLE (FIND-CLASSOID 'CHARACTER)) => T
882 ;; but (TYPE-ENUMERABLE (SPECIFIER-TYPE 'CHARACTER)) => NIL.
883 ;; You should never see the CLASSOID used as a type though,
884 ;; at least not from parsing and set operations.
885 ;; On a related note, (TYPE-ENUMERABLE (FIND-CLASSOID 'NULL))
886 ;; should probably be T, but you'll never see that type either.
887 ;; Perhaps a better definition of this function would be
888 ;; (if (classoid-translation x) (bug "enumerable-p classoid?") nil)
889 (defun classoid-enumerable-p (x) (eq (classoid-name x) 'character)))
890 (!define-type-class classoid :enumerable #'classoid-enumerable-p
891 :might-contain-other-types nil)
893 ;;; We might be passed classoids with invalid layouts; in any pairwise
894 ;;; class comparison, we must ensure that both are valid before
895 ;;; proceeding.
896 (defun %ensure-classoid-valid (classoid layout error-context)
897 (aver (eq classoid (layout-classoid layout)))
898 (or (not (layout-invalid layout))
899 (if (typep classoid 'standard-classoid)
900 (let ((class (classoid-pcl-class classoid)))
901 (cond
902 ((sb!pcl:class-finalized-p class)
903 (sb!pcl::%force-cache-flushes class)
905 ((sb!pcl::class-has-a-forward-referenced-superclass-p class)
906 (when error-context
907 (bug "~@<Invalid class ~S with forward-referenced superclass ~
908 ~S in ~A.~%~:@>"
909 class
910 (sb!pcl::class-has-a-forward-referenced-superclass-p class)
911 error-context))
912 nil)
914 (sb!pcl:finalize-inheritance class)
915 t)))
916 (bug "~@<Don't know how to ensure validity of ~S (not a STANDARD-CLASSOID) ~
917 for ~A.~%~:@>"
918 classoid (or error-context 'subtypep)))))
920 (defun %ensure-both-classoids-valid (class1 class2 &optional errorp)
921 (do ((layout1 (classoid-layout class1) (classoid-layout class1))
922 (layout2 (classoid-layout class2) (classoid-layout class2))
923 (i 0 (+ i 1)))
924 ((and (not (layout-invalid layout1)) (not (layout-invalid layout2)))
926 (aver (< i 2))
927 (unless (and (%ensure-classoid-valid class1 layout1 errorp)
928 (%ensure-classoid-valid class2 layout2 errorp))
929 (return-from %ensure-both-classoids-valid nil))))
931 (defun update-object-layout-or-invalid (object layout)
932 ;; FIXME: explain why this isn't (LAYOUT-FOR-STD-CLASS-P LAYOUT).
933 (if (layout-for-std-class-p (layout-of object))
934 (sb!pcl::check-wrapper-validity object)
935 (sb!c::%layout-invalid-error object layout)))
937 ;;; Simple methods for TYPE= and SUBTYPEP should never be called when
938 ;;; the two classes are equal, since there are EQ checks in those
939 ;;; operations.
940 (!define-type-method (classoid :simple-=) (type1 type2)
941 (aver (not (eq type1 type2)))
942 (values nil t))
944 (!define-type-method (classoid :simple-subtypep) (class1 class2)
945 (aver (not (eq class1 class2)))
946 (with-world-lock ()
947 (if (%ensure-both-classoids-valid class1 class2)
948 (let ((subclasses2 (classoid-subclasses class2)))
949 (if (and subclasses2 (gethash class1 subclasses2))
950 (values t t)
951 (if (and (typep class1 'standard-classoid)
952 (typep class2 'standard-classoid)
953 (or (sb!pcl::class-has-a-forward-referenced-superclass-p
954 (classoid-pcl-class class1))
955 (sb!pcl::class-has-a-forward-referenced-superclass-p
956 (classoid-pcl-class class2))))
957 ;; If there's a forward-referenced class involved we don't know for sure.
958 ;; (There are cases which we /could/ figure out, but that doesn't seem
959 ;; to be required or important, really.)
960 (values nil nil)
961 (values nil t))))
962 (values nil nil))))
964 ;;; When finding the intersection of a sealed class and some other
965 ;;; class (not hierarchically related) the intersection is the union
966 ;;; of the currently shared subclasses.
967 (defun sealed-class-intersection2 (sealed other)
968 (declare (type classoid sealed other))
969 (let ((s-sub (classoid-subclasses sealed))
970 (o-sub (classoid-subclasses other)))
971 (if (and s-sub o-sub)
972 (collect ((res *empty-type* type-union))
973 (dohash ((subclass layout) s-sub :locked t)
974 (declare (ignore layout))
975 (when (gethash subclass o-sub)
976 (res (specifier-type subclass))))
977 (res))
978 *empty-type*)))
980 (!define-type-method (classoid :simple-intersection2) (class1 class2)
981 (declare (type classoid class1 class2))
982 (with-world-lock ()
983 (%ensure-both-classoids-valid class1 class2 "type intersection")
984 (cond ((eq class1 class2)
985 class1)
986 ;; If one is a subclass of the other, then that is the
987 ;; intersection.
988 ((let ((subclasses (classoid-subclasses class2)))
989 (and subclasses (gethash class1 subclasses)))
990 class1)
991 ((let ((subclasses (classoid-subclasses class1)))
992 (and subclasses (gethash class2 subclasses)))
993 class2)
994 ;; Otherwise, we can't in general be sure that the
995 ;; intersection is empty, since a subclass of both might be
996 ;; defined. But we can eliminate it for some special cases.
997 ((or (structure-classoid-p class1)
998 (structure-classoid-p class2))
999 ;; No subclass of both can be defined.
1000 *empty-type*)
1001 ((eq (classoid-state class1) :sealed)
1002 ;; checking whether a subclass of both can be defined:
1003 (sealed-class-intersection2 class1 class2))
1004 ((eq (classoid-state class2) :sealed)
1005 ;; checking whether a subclass of both can be defined:
1006 (sealed-class-intersection2 class2 class1))
1008 ;; uncertain, since a subclass of both might be defined
1009 nil))))
1011 ;;; KLUDGE: we need this to deal with the special-case INSTANCE and
1012 ;;; FUNCALLABLE-INSTANCE types (which used to be CLASSOIDs until CSR
1013 ;;; discovered that this was incompatible with the MOP class
1014 ;;; hierarchy). See NAMED :COMPLEX-SUBTYPEP-ARG2
1015 (defvar *non-instance-classoid-types*
1016 '(symbol system-area-pointer weak-pointer code-component
1017 lra fdefn random-class))
1019 ;;; KLUDGE: we need this because of the need to represent
1020 ;;; intersections of two classes, even when empty at a given time, as
1021 ;;; uncanonicalized intersections because of the possibility of later
1022 ;;; defining a subclass of both classes. The necessity for changing
1023 ;;; the default return value from SUBTYPEP to NIL, T if no alternate
1024 ;;; method is present comes about because, unlike the other places we
1025 ;;; use INVOKE-COMPLEX-SUBTYPEP-ARG1-METHOD, in HAIRY methods and the
1026 ;;; like, classes are in their own hierarchy with no possibility of
1027 ;;; mixtures with other type classes.
1028 (!define-type-method (classoid :complex-subtypep-arg2) (type1 class2)
1029 (if (and (intersection-type-p type1)
1030 (> (count-if #'classoid-p (intersection-type-types type1)) 1))
1031 (values nil nil)
1032 (invoke-complex-subtypep-arg1-method type1 class2 nil t)))
1034 (!define-type-method (classoid :negate) (type)
1035 (make-negation-type :type type))
1037 (!define-type-method (classoid :unparse) (type)
1038 (classoid-proper-name type))
1040 ;;;; PCL stuff
1042 ;;; the CLASSOID that we use to represent type information for
1043 ;;; STANDARD-CLASS and FUNCALLABLE-STANDARD-CLASS. The type system
1044 ;;; side does not need to distinguish between STANDARD-CLASS and
1045 ;;; FUNCALLABLE-STANDARD-CLASS.
1046 (def!struct (standard-classoid (:include classoid)
1047 (:constructor make-standard-classoid)))
1048 ;;; a metaclass for classes which aren't standardlike but will never
1049 ;;; change either.
1050 (def!struct (static-classoid (:include classoid)
1051 (:constructor make-static-classoid)))
1053 ;;;; built-in classes
1055 ;;; The BUILT-IN-CLASSES list is a data structure which configures the
1056 ;;; creation of all the built-in classes. It contains all the info
1057 ;;; that we need to maintain the mapping between classes, compile-time
1058 ;;; types and run-time type codes. These options are defined:
1060 ;;; :TRANSLATION (default none)
1061 ;;; When this class is "parsed" as a type specifier, it is
1062 ;;; translated into the specified internal type representation,
1063 ;;; rather than being left as a class. This is used for types
1064 ;;; which we want to canonicalize to some other kind of type
1065 ;;; object because in general we want to be able to include more
1066 ;;; information than just the class (e.g. for numeric types.)
1068 ;;; :STATE (default :SEALED)
1069 ;;; The value of CLASS-STATE which we want on completion,
1070 ;;; indicating whether subclasses can be created at run-time.
1072 ;;; :HIERARCHICAL-P (default T unless any of the inherits are non-hierarchical)
1073 ;;; True if we can assign this class a unique inheritance depth.
1075 ;;; :CODES (default none)
1076 ;;; Run-time type codes which should be translated back to this
1077 ;;; class by CLASS-OF. Unspecified for abstract classes.
1079 ;;; :INHERITS (default this class and T)
1080 ;;; The class-precedence list for this class, with this class and
1081 ;;; T implicit.
1083 ;;; :DIRECT-SUPERCLASSES (default to head of CPL)
1084 ;;; List of the direct superclasses of this class.
1086 ;;; NB: not to be confused with SB-PCL::*BUILT-IN-CLASSES*
1087 (!defvar *!built-in-classes*
1088 ;; To me these data would look nicer with commas instead of "#."
1089 '((t :state :read-only :translation t)
1090 (character :codes (#.sb!vm:character-widetag)
1091 :translation (character-set)
1092 :prototype-form (code-char 42))
1093 (symbol :codes (#.sb!vm:symbol-header-widetag)
1094 :prototype-form '#:mu)
1096 (system-area-pointer :codes (#.sb!vm:sap-widetag)
1097 :prototype-form (int-sap 42))
1098 (weak-pointer :codes (#.sb!vm:weak-pointer-widetag)
1099 :prototype-form (make-weak-pointer (find-package "CL")))
1100 (code-component :codes (#.sb!vm:code-header-widetag))
1101 ;; should this be #!-(or x86 x86-64) ?
1102 (lra :codes (#.sb!vm:return-pc-header-widetag))
1103 (fdefn :codes (#.sb!vm:fdefn-widetag)
1104 :prototype-form (make-fdefn "42"))
1105 (random-class) ; used for unknown type codes
1107 (function
1108 :codes (#.sb!vm:closure-header-widetag
1109 #.sb!vm:simple-fun-header-widetag)
1110 :state :read-only
1111 :prototype-form (function (lambda () 42)))
1113 (number :translation number)
1114 (complex
1115 :translation complex
1116 :inherits (number)
1117 :codes (#.sb!vm:complex-widetag)
1118 :prototype-form (complex 42 42))
1119 (complex-single-float
1120 :translation (complex single-float)
1121 :inherits (complex number)
1122 :codes (#.sb!vm:complex-single-float-widetag)
1123 :prototype-form (complex 42f0 42f0))
1124 (complex-double-float
1125 :translation (complex double-float)
1126 :inherits (complex number)
1127 :codes (#.sb!vm:complex-double-float-widetag)
1128 :prototype-form (complex 42d0 42d0))
1129 #!+long-float
1130 (complex-long-float
1131 :translation (complex long-float)
1132 :inherits (complex number)
1133 :codes (#.sb!vm:complex-long-float-widetag)
1134 :prototype-form (complex 42l0 42l0))
1135 #!+sb-simd-pack
1136 (simd-pack
1137 :translation simd-pack
1138 :codes (#.sb!vm:simd-pack-widetag)
1139 :prototype-form (%make-simd-pack-ub64 42 42))
1140 (real :translation real :inherits (number))
1141 (float
1142 :translation float
1143 :inherits (real number))
1144 (single-float
1145 :translation single-float
1146 :inherits (float real number)
1147 :codes (#.sb!vm:single-float-widetag)
1148 :prototype-form 42f0)
1149 (double-float
1150 :translation double-float
1151 :inherits (float real number)
1152 :codes (#.sb!vm:double-float-widetag)
1153 :prototype-form 42d0)
1154 #!+long-float
1155 (long-float
1156 :translation long-float
1157 :inherits (float real number)
1158 :codes (#.sb!vm:long-float-widetag)
1159 :prototype-form 42l0)
1160 (rational
1161 :translation rational
1162 :inherits (real number))
1163 (ratio
1164 :translation (and rational (not integer))
1165 :inherits (rational real number)
1166 :codes (#.sb!vm:ratio-widetag)
1167 :prototype-form 1/42)
1168 (integer
1169 :translation integer
1170 :inherits (rational real number))
1171 (fixnum
1172 :translation (integer #.sb!xc:most-negative-fixnum
1173 #.sb!xc:most-positive-fixnum)
1174 :inherits (integer rational real number)
1175 :codes #.(mapcar #'symbol-value sb!vm::fixnum-lowtags)
1176 :prototype-form 42)
1177 (bignum
1178 :translation (and integer (not fixnum))
1179 :inherits (integer rational real number)
1180 :codes (#.sb!vm:bignum-widetag)
1181 :prototype-form (expt 2 #.(* sb!vm:n-word-bits (/ 3 2))))
1183 (array :translation array :codes (#.sb!vm:complex-array-widetag)
1184 :hierarchical-p nil
1185 :prototype-form (make-array nil :adjustable t))
1186 (simple-array
1187 :translation simple-array :codes (#.sb!vm:simple-array-widetag)
1188 :inherits (array)
1189 :prototype-form (make-array nil))
1190 (sequence
1191 :translation (or cons (member nil) vector extended-sequence)
1192 :state :read-only
1193 :depth 2)
1194 (vector
1195 :translation vector :codes (#.sb!vm:complex-vector-widetag)
1196 :direct-superclasses (array sequence)
1197 :inherits (array sequence))
1198 (simple-vector
1199 :translation simple-vector :codes (#.sb!vm:simple-vector-widetag)
1200 :direct-superclasses (vector simple-array)
1201 :inherits (vector simple-array array sequence)
1202 :prototype-form (make-array 0))
1203 (bit-vector
1204 :translation bit-vector :codes (#.sb!vm:complex-bit-vector-widetag)
1205 :inherits (vector array sequence)
1206 :prototype-form (make-array 0 :element-type 'bit :fill-pointer t))
1207 (simple-bit-vector
1208 :translation simple-bit-vector :codes (#.sb!vm:simple-bit-vector-widetag)
1209 :direct-superclasses (bit-vector simple-array)
1210 :inherits (bit-vector vector simple-array
1211 array sequence)
1212 :prototype-form (make-array 0 :element-type 'bit))
1213 (simple-array-unsigned-byte-2
1214 :translation (simple-array (unsigned-byte 2) (*))
1215 :codes (#.sb!vm:simple-array-unsigned-byte-2-widetag)
1216 :direct-superclasses (vector simple-array)
1217 :inherits (vector simple-array array sequence)
1218 :prototype-form (make-array 0 :element-type '(unsigned-byte 2)))
1219 (simple-array-unsigned-byte-4
1220 :translation (simple-array (unsigned-byte 4) (*))
1221 :codes (#.sb!vm:simple-array-unsigned-byte-4-widetag)
1222 :direct-superclasses (vector simple-array)
1223 :inherits (vector simple-array array sequence)
1224 :prototype-form (make-array 0 :element-type '(unsigned-byte 4)))
1225 (simple-array-unsigned-byte-7
1226 :translation (simple-array (unsigned-byte 7) (*))
1227 :codes (#.sb!vm:simple-array-unsigned-byte-7-widetag)
1228 :direct-superclasses (vector simple-array)
1229 :inherits (vector simple-array array sequence)
1230 :prototype-form (make-array 0 :element-type '(unsigned-byte 7)))
1231 (simple-array-unsigned-byte-8
1232 :translation (simple-array (unsigned-byte 8) (*))
1233 :codes (#.sb!vm:simple-array-unsigned-byte-8-widetag)
1234 :direct-superclasses (vector simple-array)
1235 :inherits (vector simple-array array sequence)
1236 :prototype-form (make-array 0 :element-type '(unsigned-byte 8)))
1237 (simple-array-unsigned-byte-15
1238 :translation (simple-array (unsigned-byte 15) (*))
1239 :codes (#.sb!vm:simple-array-unsigned-byte-15-widetag)
1240 :direct-superclasses (vector simple-array)
1241 :inherits (vector simple-array array sequence)
1242 :prototype-form (make-array 0 :element-type '(unsigned-byte 15)))
1243 (simple-array-unsigned-byte-16
1244 :translation (simple-array (unsigned-byte 16) (*))
1245 :codes (#.sb!vm:simple-array-unsigned-byte-16-widetag)
1246 :direct-superclasses (vector simple-array)
1247 :inherits (vector simple-array array sequence)
1248 :prototype-form (make-array 0 :element-type '(unsigned-byte 16)))
1250 (simple-array-unsigned-fixnum
1251 :translation (simple-array (unsigned-byte #.sb!vm:n-positive-fixnum-bits) (*))
1252 :codes (#.sb!vm:simple-array-unsigned-fixnum-widetag)
1253 :direct-superclasses (vector simple-array)
1254 :inherits (vector simple-array array sequence)
1255 :prototype-form (make-array 0
1256 :element-type '(unsigned-byte #.sb!vm:n-positive-fixnum-bits)))
1258 (simple-array-unsigned-byte-31
1259 :translation (simple-array (unsigned-byte 31) (*))
1260 :codes (#.sb!vm:simple-array-unsigned-byte-31-widetag)
1261 :direct-superclasses (vector simple-array)
1262 :inherits (vector simple-array array sequence)
1263 :prototype-form (make-array 0 :element-type '(unsigned-byte 31)))
1264 (simple-array-unsigned-byte-32
1265 :translation (simple-array (unsigned-byte 32) (*))
1266 :codes (#.sb!vm:simple-array-unsigned-byte-32-widetag)
1267 :direct-superclasses (vector simple-array)
1268 :inherits (vector simple-array array sequence)
1269 :prototype-form (make-array 0 :element-type '(unsigned-byte 32)))
1270 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1271 (simple-array-unsigned-byte-63
1272 :translation (simple-array (unsigned-byte 63) (*))
1273 :codes (#.sb!vm:simple-array-unsigned-byte-63-widetag)
1274 :direct-superclasses (vector simple-array)
1275 :inherits (vector simple-array array sequence)
1276 :prototype-form (make-array 0 :element-type '(unsigned-byte 63)))
1277 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1278 (simple-array-unsigned-byte-64
1279 :translation (simple-array (unsigned-byte 64) (*))
1280 :codes (#.sb!vm:simple-array-unsigned-byte-64-widetag)
1281 :direct-superclasses (vector simple-array)
1282 :inherits (vector simple-array array sequence)
1283 :prototype-form (make-array 0 :element-type '(unsigned-byte 64)))
1284 (simple-array-signed-byte-8
1285 :translation (simple-array (signed-byte 8) (*))
1286 :codes (#.sb!vm:simple-array-signed-byte-8-widetag)
1287 :direct-superclasses (vector simple-array)
1288 :inherits (vector simple-array array sequence)
1289 :prototype-form (make-array 0 :element-type '(signed-byte 8)))
1290 (simple-array-signed-byte-16
1291 :translation (simple-array (signed-byte 16) (*))
1292 :codes (#.sb!vm:simple-array-signed-byte-16-widetag)
1293 :direct-superclasses (vector simple-array)
1294 :inherits (vector simple-array array sequence)
1295 :prototype-form (make-array 0 :element-type '(signed-byte 16)))
1297 (simple-array-fixnum
1298 :translation (simple-array (signed-byte #.sb!vm:n-fixnum-bits)
1299 (*))
1300 :codes (#.sb!vm:simple-array-fixnum-widetag)
1301 :direct-superclasses (vector simple-array)
1302 :inherits (vector simple-array array sequence)
1303 :prototype-form (make-array 0
1304 :element-type
1305 '(signed-byte #.sb!vm:n-fixnum-bits)))
1307 (simple-array-signed-byte-32
1308 :translation (simple-array (signed-byte 32) (*))
1309 :codes (#.sb!vm:simple-array-signed-byte-32-widetag)
1310 :direct-superclasses (vector simple-array)
1311 :inherits (vector simple-array array sequence)
1312 :prototype-form (make-array 0 :element-type '(signed-byte 32)))
1313 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
1314 (simple-array-signed-byte-64
1315 :translation (simple-array (signed-byte 64) (*))
1316 :codes (#.sb!vm:simple-array-signed-byte-64-widetag)
1317 :direct-superclasses (vector simple-array)
1318 :inherits (vector simple-array array sequence)
1319 :prototype-form (make-array 0 :element-type '(signed-byte 64)))
1320 (simple-array-single-float
1321 :translation (simple-array single-float (*))
1322 :codes (#.sb!vm:simple-array-single-float-widetag)
1323 :direct-superclasses (vector simple-array)
1324 :inherits (vector simple-array array sequence)
1325 :prototype-form (make-array 0 :element-type 'single-float))
1326 (simple-array-double-float
1327 :translation (simple-array double-float (*))
1328 :codes (#.sb!vm:simple-array-double-float-widetag)
1329 :direct-superclasses (vector simple-array)
1330 :inherits (vector simple-array array sequence)
1331 :prototype-form (make-array 0 :element-type 'double-float))
1332 #!+long-float
1333 (simple-array-long-float
1334 :translation (simple-array long-float (*))
1335 :codes (#.sb!vm:simple-array-long-float-widetag)
1336 :direct-superclasses (vector simple-array)
1337 :inherits (vector simple-array array sequence)
1338 :prototype-form (make-array 0 :element-type 'long-float))
1339 (simple-array-complex-single-float
1340 :translation (simple-array (complex single-float) (*))
1341 :codes (#.sb!vm:simple-array-complex-single-float-widetag)
1342 :direct-superclasses (vector simple-array)
1343 :inherits (vector simple-array array sequence)
1344 :prototype-form (make-array 0 :element-type '(complex single-float)))
1345 (simple-array-complex-double-float
1346 :translation (simple-array (complex double-float) (*))
1347 :codes (#.sb!vm:simple-array-complex-double-float-widetag)
1348 :direct-superclasses (vector simple-array)
1349 :inherits (vector simple-array array sequence)
1350 :prototype-form (make-array 0 :element-type '(complex double-float)))
1351 #!+long-float
1352 (simple-array-complex-long-float
1353 :translation (simple-array (complex long-float) (*))
1354 :codes (#.sb!vm:simple-array-complex-long-float-widetag)
1355 :direct-superclasses (vector simple-array)
1356 :inherits (vector simple-array array sequence)
1357 :prototype-form (make-array 0 :element-type '(complex long-float)))
1358 (string
1359 :translation string
1360 :direct-superclasses (vector)
1361 :inherits (vector array sequence))
1362 (simple-string
1363 :translation simple-string
1364 :direct-superclasses (string simple-array)
1365 :inherits (string vector simple-array array sequence))
1366 (vector-nil
1367 :translation (vector nil)
1368 :codes (#.sb!vm:complex-vector-nil-widetag)
1369 :direct-superclasses (string)
1370 :inherits (string vector array sequence)
1371 :prototype-form (make-array 0 :element-type 'nil :fill-pointer t))
1372 (simple-array-nil
1373 :translation (simple-array nil (*))
1374 :codes (#.sb!vm:simple-array-nil-widetag)
1375 :direct-superclasses (vector-nil simple-string)
1376 :inherits (vector-nil simple-string string vector simple-array
1377 array sequence)
1378 :prototype-form (make-array 0 :element-type 'nil))
1379 (base-string
1380 :translation base-string
1381 :codes (#.sb!vm:complex-base-string-widetag)
1382 :direct-superclasses (string)
1383 :inherits (string vector array sequence)
1384 :prototype-form (make-array 0 :element-type 'base-char :fill-pointer t))
1385 (simple-base-string
1386 :translation simple-base-string
1387 :codes (#.sb!vm:simple-base-string-widetag)
1388 :direct-superclasses (base-string simple-string)
1389 :inherits (base-string simple-string string vector simple-array
1390 array sequence)
1391 :prototype-form (make-array 0 :element-type 'base-char))
1392 #!+sb-unicode
1393 (character-string
1394 :translation (vector character)
1395 :codes (#.sb!vm:complex-character-string-widetag)
1396 :direct-superclasses (string)
1397 :inherits (string vector array sequence)
1398 :prototype-form (make-array 0 :element-type 'character :fill-pointer t))
1399 #!+sb-unicode
1400 (simple-character-string
1401 :translation (simple-array character (*))
1402 :codes (#.sb!vm:simple-character-string-widetag)
1403 :direct-superclasses (character-string simple-string)
1404 :inherits (character-string simple-string string vector simple-array
1405 array sequence)
1406 :prototype-form (make-array 0 :element-type 'character))
1407 (list
1408 :translation (or cons (member nil))
1409 :inherits (sequence))
1410 (cons
1411 :codes (#.sb!vm:list-pointer-lowtag)
1412 :translation cons
1413 :inherits (list sequence)
1414 :prototype-form (cons nil nil))
1415 (null
1416 :translation (member nil)
1417 :inherits (symbol list sequence)
1418 :direct-superclasses (symbol list)
1419 :prototype-form 'nil)
1420 (stream
1421 :state :read-only
1422 :depth 2)
1423 (file-stream
1424 :state :read-only
1425 :depth 4
1426 :inherits (stream))
1427 (string-stream
1428 :state :read-only
1429 :depth 4
1430 :inherits (stream))))
1432 ;;; See also src/code/class-init.lisp where we finish setting up the
1433 ;;; translations for built-in types.
1434 (!cold-init-forms
1435 (dolist (x *!built-in-classes*)
1436 #-sb-xc-host (/show0 "at head of loop over *!BUILT-IN-CLASSES*")
1437 (destructuring-bind
1438 (name &key
1439 (translation nil trans-p)
1440 inherits
1441 codes
1442 state
1443 depth
1444 prototype-form
1445 (hierarchical-p t) ; might be modified below
1446 (direct-superclasses (if inherits
1447 (list (car inherits))
1448 '(t))))
1450 (declare (ignore codes state translation prototype-form))
1451 (let ((inherits-list (if (eq name t)
1453 (cons t (reverse inherits))))
1454 (classoid (make-built-in-classoid
1455 :name name
1456 :translation (if trans-p :initializing nil)
1457 :direct-superclasses
1458 (if (eq name t)
1460 (mapcar #'find-classoid direct-superclasses)))))
1461 (mark-ctype-interned classoid)
1462 (setf (info :type :kind name) #+sb-xc-host :defined #-sb-xc-host :primitive
1463 (classoid-cell-classoid (find-classoid-cell name :create t)) classoid)
1464 (unless trans-p
1465 (setf (info :type :builtin name) classoid))
1466 (let* ((inherits-vector
1467 (map 'simple-vector
1468 (lambda (x)
1469 (let ((super-layout
1470 (classoid-layout (find-classoid x))))
1471 (when (minusp (layout-depthoid super-layout))
1472 (setf hierarchical-p nil))
1473 super-layout))
1474 inherits-list))
1475 (depthoid (if hierarchical-p
1476 (or depth (length inherits-vector))
1477 -1)))
1478 (register-layout
1479 (find-and-init-or-check-layout name
1481 inherits-vector
1482 depthoid
1484 :invalidate nil)))))
1485 (/show0 "done with loop over *!BUILT-IN-CLASSES*"))
1487 ;;; Define temporary PCL STANDARD-CLASSes. These will be set up
1488 ;;; correctly and the Lisp layout replaced by a PCL wrapper after PCL
1489 ;;; is loaded and the class defined.
1490 (!cold-init-forms
1491 (/show0 "about to define temporary STANDARD-CLASSes")
1492 (dolist (x '(;; Why is STREAM duplicated in this list? Because, when
1493 ;; the inherits-vector of FUNDAMENTAL-STREAM is set up,
1494 ;; a vector containing the elements of the list below,
1495 ;; i.e. '(T STREAM STREAM), is created, and
1496 ;; this is what the function ORDER-LAYOUT-INHERITS
1497 ;; would do, too.
1499 ;; So, the purpose is to guarantee a valid layout for
1500 ;; the FUNDAMENTAL-STREAM class, matching what
1501 ;; ORDER-LAYOUT-INHERITS would do.
1502 ;; ORDER-LAYOUT-INHERITS would place STREAM at index 2
1503 ;; in the INHERITS(-VECTOR). Index 1 would not be
1504 ;; filled, so STREAM is duplicated there (as
1505 ;; ORDER-LAYOUTS-INHERITS would do). Maybe the
1506 ;; duplicate definition could be removed (removing a
1507 ;; STREAM element), because FUNDAMENTAL-STREAM is
1508 ;; redefined after PCL is set up, anyway. But to play
1509 ;; it safely, we define the class with a valid INHERITS
1510 ;; vector.
1511 (fundamental-stream (t stream stream))))
1512 (/show0 "defining temporary STANDARD-CLASS")
1513 (let* ((name (first x))
1514 (inherits-list (second x))
1515 (classoid (make-standard-classoid :name name))
1516 (classoid-cell (find-classoid-cell name :create t)))
1517 ;; Needed to open-code the MAP, below
1518 (declare (type list inherits-list))
1519 (setf (classoid-cell-classoid classoid-cell) classoid
1520 (info :type :kind name) :instance)
1521 (let ((inherits (map 'simple-vector
1522 (lambda (x)
1523 (classoid-layout (find-classoid x)))
1524 inherits-list)))
1525 #-sb-xc-host (/show0 "INHERITS=..") #-sb-xc-host (/hexstr inherits)
1526 (register-layout (find-and-init-or-check-layout name 0 inherits -1 0)
1527 :invalidate nil))))
1528 (/show0 "done defining temporary STANDARD-CLASSes"))
1530 ;;; Now that we have set up the class heterarchy, seal the sealed
1531 ;;; classes. This must be done after the subclasses have been set up.
1532 (!cold-init-forms
1533 (dolist (x *!built-in-classes*)
1534 (destructuring-bind (name &key (state :sealed) &allow-other-keys) x
1535 (setf (classoid-state (find-classoid name)) state))))
1537 ;;;; class definition/redefinition
1539 ;;; This is to be called whenever we are altering a class.
1540 (defun %modify-classoid (classoid)
1541 (clear-type-caches)
1542 (when (member (classoid-state classoid) '(:read-only :frozen))
1543 ;; FIXME: This should probably be CERROR.
1544 (warn "making ~(~A~) class ~S writable"
1545 (classoid-state classoid)
1546 (classoid-name classoid))
1547 (setf (classoid-state classoid) nil)))
1549 ;;; Mark LAYOUT as invalid. Setting DEPTHOID -1 helps cause unsafe
1550 ;;; structure type tests to fail. Remove class from all superclasses
1551 ;;; too (might not be registered, so might not be in subclasses of the
1552 ;;; nominal superclasses.) We set the layout-clos-hash slots to 0 to
1553 ;;; invalidate the wrappers for specialized dispatch functions, which
1554 ;;; use those slots as indexes into tables.
1555 (defun %invalidate-layout (layout)
1556 (declare (type layout layout))
1557 (setf (layout-invalid layout) t
1558 (layout-depthoid layout) -1)
1559 (setf (layout-clos-hash layout) 0)
1560 (let ((inherits (layout-inherits layout))
1561 (classoid (layout-classoid layout)))
1562 (%modify-classoid classoid)
1563 (dovector (super inherits)
1564 (let ((subs (classoid-subclasses (layout-classoid super))))
1565 (when subs
1566 (remhash classoid subs)))))
1567 (values))
1569 ;;;; cold loading initializations
1571 ;;; FIXME: It would be good to arrange for this to be called when the
1572 ;;; cross-compiler is being built, not just when the target Lisp is
1573 ;;; being cold loaded. Perhaps this could be moved to its own file
1574 ;;; late in the build-order.lisp-expr sequence, and be put in
1575 ;;; !COLD-INIT-FORMS there?
1576 (defun !class-finalize ()
1577 (dohash ((name layout) *forward-referenced-layouts*)
1578 (let ((class (find-classoid name nil)))
1579 (cond ((not class)
1580 (setf (layout-classoid layout) (make-undefined-classoid name)))
1581 ((eq (classoid-layout class) layout)
1582 (remhash name *forward-referenced-layouts*))
1584 (error "Something strange with forward layout for ~S:~% ~S"
1585 name layout))))))
1587 (!cold-init-forms
1588 #-sb-xc-host (/show0 "about to set *BUILT-IN-CLASS-CODES*")
1589 (setq **built-in-class-codes**
1590 (let* ((initial-element
1591 (locally
1592 ;; KLUDGE: There's a FIND-CLASSOID DEFTRANSFORM for
1593 ;; constant class names which creates fast but
1594 ;; non-cold-loadable, non-compact code. In this
1595 ;; context, we'd rather have compact, cold-loadable
1596 ;; code. -- WHN 19990928
1597 (declare (notinline find-classoid))
1598 (classoid-layout (find-classoid 'random-class))))
1599 (res (make-array 256 :initial-element initial-element)))
1600 (dolist (x *!built-in-classes* res)
1601 (destructuring-bind (name &key codes &allow-other-keys)
1603 (let ((layout (classoid-layout (find-classoid name))))
1604 (dolist (code codes)
1605 (setf (svref res code) layout)))))))
1606 #-sb-xc-host (/show0 "done setting *BUILT-IN-CLASS-CODES*"))
1608 (!defun-from-collected-cold-init-forms !classes-cold-init)