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