Make 'primordial-extensions' very primordial.
[sbcl.git] / src / code / early-classoid.lisp
bloba50258b635d9836fc0f7aac90cb124bc256f81a7
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 ;;;; DEFSTRUCT-DESCRIPTION
18 ;;; The DEFSTRUCT-DESCRIPTION structure holds compile-time information
19 ;;; about a structure type.
20 ;;; It is defined prior to LAYOUT because a LAYOUT-INFO slot
21 ;;; is declared to hold a DEFSTRUCT-DESCRIPTION.
22 (def!struct (defstruct-description
23 (:conc-name dd-)
24 (:make-load-form-fun just-dump-it-normally)
25 #-sb-xc-host (:pure t)
26 (:constructor make-defstruct-description (null-lexenv-p name)))
27 ;; name of the structure
28 (name (missing-arg) :type symbol :read-only t)
29 ;; documentation on the structure
30 (doc nil :type (or string null))
31 ;; prefix for slot names. If NIL, none.
32 (conc-name nil :type (or string null))
33 ;; All the :CONSTRUCTOR specs and posssibly an implied constructor,
34 ;; keyword constructors first, then BOA constructors. NIL if none.
35 (constructors () :type list)
36 ;; True if the DEFSTRUCT appeared in a null lexical environment.
37 (null-lexenv-p nil :type boolean :read-only t) ; the safe default is NIL
38 ;; name of copying function
39 (copier-name nil :type symbol)
40 ;; name of type predicate
41 (predicate-name nil :type symbol)
42 ;; the arguments to the :INCLUDE option, or NIL if no included
43 ;; structure
44 (include nil :type list)
45 ;; properties used to define structure-like classes with an
46 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
47 ;; metaclass. Syntax is:
48 ;; (superclass-name metaclass-name metaclass-constructor)
49 (alternate-metaclass nil :type list)
50 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
51 ;; (including included ones)
52 (slots () :type list)
53 ;; a list of (NAME . INDEX) pairs for accessors of included structures
54 (inherited-accessor-alist () :type list)
55 ;; number of elements we've allocated (See also RAW-LENGTH, which is not
56 ;; included in LENGTH.)
57 (length 0 :type index)
58 ;; General kind of implementation.
59 (type 'structure :type (member structure vector list
60 funcallable-structure))
62 ;; The next three slots are for :TYPE'd structures (which aren't
63 ;; classes, DD-CLASS-P = NIL)
65 ;; vector element type
66 (element-type t)
67 ;; T if :NAMED was explicitly specified, NIL otherwise
68 (named nil :type boolean)
69 ;; any INITIAL-OFFSET option on this direct type
70 (offset nil :type (or index null))
72 ;; which :PRINT-mumble option was given, if either was.
73 (print-option nil :type (member nil :print-function :print-object))
74 ;; the argument to the PRINT-FUNCTION or PRINT-OBJECT option.
75 ;; NIL if the option was given with no argument.
76 (printer-fname nil :type (or cons symbol))
78 ;; The number of untagged slots at the end.
79 #!-interleaved-raw-slots (raw-length 0 :type index)
80 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
81 ;; meaningful if DD-CLASS-P = T.
82 (pure :unspecified :type (member t nil :unspecified)))
83 #!-sb-fluid (declaim (freeze-type defstruct-description))
85 ;;;; basic LAYOUT stuff
87 ;;; Note: This bound is set somewhat less than MOST-POSITIVE-FIXNUM
88 ;;; in order to guarantee that several hash values can be added without
89 ;;; overflowing into a bignum.
90 (defconstant layout-clos-hash-limit (1+ (ash sb!xc:most-positive-fixnum -3))
91 #!+sb-doc
92 "the exclusive upper bound on LAYOUT-CLOS-HASH values")
93 ;; This must be DEF!TYPE and not just DEFTYPE because access to slots
94 ;; of a layout occur "before" the structure definition is made in the
95 ;; run-the-xc pass, and the source-transform of a slot accessor
96 ;; wraps (TRULY-THE <type> ...) around %INSTANCE-REF,
97 ;; so <type> had best be defined at that point.
98 (def!type layout-clos-hash () `(integer 0 ,layout-clos-hash-limit))
99 (declaim (ftype (sfunction () layout-clos-hash) random-layout-clos-hash))
101 ;;; The LAYOUT structure is pointed to by the first cell of instance
102 ;;; (or structure) objects. It represents what we need to know for
103 ;;; type checking and garbage collection. Whenever a class is
104 ;;; incompatibly redefined, a new layout is allocated. If two object's
105 ;;; layouts are EQ, then they are exactly the same type.
107 ;;; *** IMPORTANT ***
109 ;;; If you change the slots of LAYOUT, you need to alter genesis as
110 ;;; well, since the initialization of layout slots is hardcoded there.
112 ;;; FIXME: ...it would be better to automate this, of course...
113 (def!struct (layout
114 ;; KLUDGE: A special hack keeps this from being
115 ;; called when building code for the
116 ;; cross-compiler. See comments at the DEFUN for
117 ;; this. -- WHN 19990914
118 (:make-load-form-fun #-sb-xc-host ignore-it
119 ;; KLUDGE: DEF!STRUCT at #+SB-XC-HOST
120 ;; time controls both the
121 ;; build-the-cross-compiler behavior
122 ;; and the run-the-cross-compiler
123 ;; behavior. The value below only
124 ;; works for build-the-cross-compiler.
125 ;; There's a special hack in
126 ;; EMIT-MAKE-LOAD-FORM which gives
127 ;; effectively IGNORE-IT behavior for
128 ;; LAYOUT at run-the-cross-compiler
129 ;; time. It would be cleaner to
130 ;; actually have an IGNORE-IT value
131 ;; stored, but it's hard to see how to
132 ;; do that concisely with the current
133 ;; DEF!STRUCT setup. -- WHN 19990930
134 #+sb-xc-host
135 make-load-form-for-layout))
136 ;; a pseudo-random hash value for use by CLOS.
137 (clos-hash (random-layout-clos-hash) :type layout-clos-hash)
138 ;; the class that this is a layout for
139 (classoid (missing-arg) :type classoid)
140 ;; The value of this slot can be:
141 ;; * :UNINITIALIZED if not initialized yet;
142 ;; * NIL if this is the up-to-date layout for a class; or
143 ;; * T if this layout has been invalidated (by being replaced by
144 ;; a new, more-up-to-date LAYOUT).
145 ;; * something else (probably a list) if the class is a PCL wrapper
146 ;; and PCL has made it invalid and made a note to itself about it
147 (invalid :uninitialized :type (or cons (member nil t :uninitialized)))
148 ;; the layouts for all classes we inherit. If hierarchical, i.e. if
149 ;; DEPTHOID >= 0, then these are ordered by ORDER-LAYOUT-INHERITS
150 ;; (least to most specific), so that each inherited layout appears
151 ;; at its expected depth, i.e. at its LAYOUT-DEPTHOID value.
153 ;; Remaining elements are filled by the non-hierarchical layouts or,
154 ;; if they would otherwise be empty, by copies of succeeding layouts.
155 (inherits #() :type simple-vector)
156 ;; If inheritance is not hierarchical, this is -1. If inheritance is
157 ;; hierarchical, this is the inheritance depth, i.e. (LENGTH INHERITS).
158 ;; Note:
159 ;; (1) This turns out to be a handy encoding for arithmetically
160 ;; comparing deepness; it is generally useful to do a bare numeric
161 ;; comparison of these depthoid values, and we hardly ever need to
162 ;; test whether the values are negative or not.
163 ;; (2) This was called INHERITANCE-DEPTH in classic CMU CL. It was
164 ;; renamed because some of us find it confusing to call something
165 ;; a depth when it isn't quite.
166 (depthoid -1 :type layout-depthoid)
167 ;; the number of top level descriptor cells in each instance
168 (length 0 :type index)
169 ;; If this layout has some kind of compiler meta-info, then this is
170 ;; it. If a structure, then we store the DEFSTRUCT-DESCRIPTION here.
171 (info nil :type (or null defstruct-description))
172 ;; This is true if objects of this class are never modified to
173 ;; contain dynamic pointers in their slots or constant-like
174 ;; substructure (and hence can be copied into read-only space by
175 ;; PURIFY).
177 ;; This slot is known to the C runtime support code.
178 (pure nil :type (member t nil 0))
179 ;; Number of raw words at the end.
180 ;; This slot is known to the C runtime support code.
181 ;; It counts the number of untagged cells, not user-visible slots.
182 ;; e.g. on 32-bit machines, each (COMPLEX DOUBLE-FLOAT) counts as 4.
183 #!-interleaved-raw-slots (n-untagged-slots 0 :type index)
184 ;; Metadata
185 #!+interleaved-raw-slots (untagged-bitmap 0 :type unsigned-byte)
186 #!+interleaved-raw-slots (equalp-tests #() :type simple-vector)
187 ;; Definition location
188 (source-location nil)
189 ;; If this layout is for an object of metatype STANDARD-CLASS,
190 ;; these are the EFFECTIVE-SLOT-DEFINITION metaobjects.
191 (slot-list nil :type list)
192 ;; Information about slots in the class to PCL: this provides fast
193 ;; access to slot-definitions and locations by name, etc.
194 ;; See MAKE-SLOT-TABLE in pcl/slots-boot.lisp for further details.
195 (slot-table #(1 nil) :type simple-vector)
196 ;; True IFF the layout belongs to a standand-instance or a
197 ;; standard-funcallable-instance.
198 ;; Old comment was:
199 ;; FIXME: If we unify wrappers and layouts this can go away, since
200 ;; it is only used in SB-PCL::EMIT-FETCH-WRAPPERS, which can then
201 ;; use INSTANCE-SLOTS-LAYOUT instead (if there is are no slot
202 ;; layouts, there are no slots for it to pull.)
203 ;; But while that's conceivable, it still seems advantageous to have
204 ;; a single bit that decides whether something is STANDARD-OBJECT.
205 (%for-std-class-b 0 :type bit :read-only t))
206 (declaim (freeze-type layout))
208 ;;; The CLASSOID structure is a supertype of all classoid types. A
209 ;;; CLASSOID is also a CTYPE structure as recognized by the type
210 ;;; system. (FIXME: It's also a type specifier, though this might go
211 ;;; away as with the merger of SB-PCL:CLASS and CL:CLASS it's no
212 ;;; longer necessary)
213 (def!struct (classoid
214 (:make-load-form-fun classoid-make-load-form-fun)
215 (:include ctype
216 (class-info (type-class-or-lose 'classoid)))
217 (:constructor nil)
218 #-no-ansi-print-object
219 (:print-object
220 (lambda (class stream)
221 (let ((name (classoid-name class)))
222 (print-unreadable-object (class stream
223 :type t
224 :identity (not name))
225 (format stream
226 ;; FIXME: Make sure that this prints
227 ;; reasonably for anonymous classes.
228 "~:[anonymous~;~:*~S~]~@[ (~(~A~))~]"
229 name
230 (classoid-state class))))))
231 #-sb-xc-host (:pure nil))
232 ;; the value to be returned by CLASSOID-NAME.
233 (name nil :type symbol)
234 ;; the current layout for this class, or NIL if none assigned yet
235 (layout nil :type (or layout null))
236 ;; How sure are we that this class won't be redefined?
237 ;; :READ-ONLY = We are committed to not changing the effective
238 ;; slots or superclasses.
239 ;; :SEALED = We can't even add subclasses.
240 ;; NIL = Anything could happen.
241 (state nil :type (member nil :read-only :sealed))
242 ;; direct superclasses of this class. Always NIL for CLOS classes.
243 (direct-superclasses () :type list)
244 ;; representation of all of the subclasses (direct or indirect) of
245 ;; this class. This is NIL if no subclasses or not initalized yet;
246 ;; otherwise, it's an EQ hash-table mapping CLASSOID objects to the
247 ;; subclass layout that was in effect at the time the subclass was
248 ;; created.
249 (subclasses nil :type (or null hash-table))
250 ;; the PCL class (= CL:CLASS, but with a view to future flexibility
251 ;; we don't just call it the CLASS slot) object for this class, or
252 ;; NIL if none assigned yet
253 (pcl-class nil))
255 ;;;; object types to represent classes
257 ;;; An UNDEFINED-CLASSOID is a cookie we make up to stick in forward
258 ;;; referenced layouts. Users should never see them.
259 (def!struct (undefined-classoid
260 (:include classoid)
261 (:constructor make-undefined-classoid (name))))
263 ;;; BUILT-IN-CLASS is used to represent the standard classes that
264 ;;; aren't defined with DEFSTRUCT and other specially implemented
265 ;;; primitive types whose only attribute is their name.
267 ;;; Some BUILT-IN-CLASSes have a TRANSLATION, which means that they
268 ;;; are effectively DEFTYPE'd to some other type (usually a union of
269 ;;; other classes or a "primitive" type such as NUMBER, ARRAY, etc.)
270 ;;; This translation is done when type specifiers are parsed. Type
271 ;;; system operations (union, subtypep, etc.) should never encounter
272 ;;; translated classes, only their translation.
273 (def!struct (built-in-classoid (:include classoid)
274 (:constructor make-built-in-classoid))
275 ;; the type we translate to on parsing. If NIL, then this class
276 ;; stands on its own; or it can be set to :INITIALIZING for a period
277 ;; during cold-load.
278 (translation nil :type (or ctype (member nil :initializing))))
280 (def!struct (condition-classoid (:include classoid)
281 (:constructor make-condition-classoid))
282 ;; list of CONDITION-SLOT structures for the direct slots of this
283 ;; class
284 (slots nil :type list)
285 ;; list of CONDITION-SLOT structures for all of the effective class
286 ;; slots of this class
287 (class-slots nil :type list)
288 ;; report function or NIL
289 (report nil :type (or function null))
290 ;; list of specifications of the form
292 ;; (INITARG INITFORM THUNK)
294 ;; where THUNK, when called without arguments, returns the value for
295 ;; INITARG.
296 (direct-default-initargs () :type list)
297 ;; class precedence list as a list of CLASS objects, with all
298 ;; non-CONDITION classes removed
299 (cpl () :type list)
300 ;; a list of all the effective instance allocation slots of this
301 ;; class that have a non-constant initform or default-initarg.
302 ;; Values for these slots must be computed in the dynamic
303 ;; environment of MAKE-CONDITION.
304 (hairy-slots nil :type list))
306 ;;;; classoid namespace
308 ;;; We use an indirection to allow forward referencing of class
309 ;;; definitions with load-time resolution.
310 (def!struct (classoid-cell
311 (:constructor make-classoid-cell (name &optional classoid))
312 (:make-load-form-fun (lambda (c)
313 `(find-classoid-cell
314 ',(classoid-cell-name c)
315 :create t)))
316 #-no-ansi-print-object
317 (:print-object (lambda (s stream)
318 (print-unreadable-object (s stream :type t)
319 (prin1 (classoid-cell-name s) stream)))))
320 ;; Name of class we expect to find.
321 (name nil :type symbol :read-only t)
322 ;; Classoid or NIL if not yet defined.
323 (classoid nil :type (or classoid null))
324 ;; PCL class, if any
325 (pcl-class nil))
326 (declaim (freeze-type classoid-cell))
328 (defun find-classoid-cell (name &key create)
329 (let ((real-name (uncross name)))
330 (cond ((info :type :classoid-cell real-name))
331 (create
332 (get-info-value-initializing :type :classoid-cell real-name
333 (make-classoid-cell real-name))))))
335 ;;; Return the classoid with the specified NAME. If ERRORP is false,
336 ;;; then NIL is returned when no such class exists.
337 (defun find-classoid (name &optional (errorp t))
338 (declare (type symbol name))
339 (let ((cell (find-classoid-cell name)))
340 (cond ((and cell (classoid-cell-classoid cell)))
341 (errorp
342 (error 'simple-type-error
343 :datum nil
344 :expected-type 'class
345 :format-control "Class not yet defined: ~S"
346 :format-arguments (list name))))))
348 ;;;; PCL stuff
350 ;;; the CLASSOID that we use to represent type information for
351 ;;; STANDARD-CLASS and FUNCALLABLE-STANDARD-CLASS. The type system
352 ;;; side does not need to distinguish between STANDARD-CLASS and
353 ;;; FUNCALLABLE-STANDARD-CLASS.
354 (def!struct (standard-classoid (:include classoid)
355 (:constructor make-standard-classoid)))
356 ;;; a metaclass for classes which aren't standardlike but will never
357 ;;; change either.
358 (def!struct (static-classoid (:include classoid)
359 (:constructor make-static-classoid)))
361 (declaim (freeze-type built-in-classoid condition-classoid
362 standard-classoid static-classoid))
364 (in-package "SB!C")
366 ;;; layout for this type being used by the compiler
367 (define-info-type (:type :compiler-layout)
368 :type-spec (or layout null)
369 :default (lambda (name)
370 (let ((class (find-classoid name nil)))
371 (when class (classoid-layout class)))))
373 ;;; The type specifier for this function, or a DEFSTRUCT-DESCRIPTION
374 ;;; or the symbol :GENERIC-FUNTION.
375 ;;; If a DD, it must contain a constructor whose name is
376 ;;; the one being sought in globaldb, which is used to derive the type.
377 ;;; If :GENERIC-FUNCTION, the info is recomputed from existing methods
378 ;;; and stored back into globaldb.
379 (define-info-type (:function :type)
380 :type-spec (or ctype defstruct-description (member :generic-function))
381 :default #'ftype-from-fdefn)
383 ;;; This is sorta semantically equivalent to SXHASH, but better-behaved for
384 ;;; legal function names. It performs more work by not cutting off as soon
385 ;;; in the CDR direction, thereby improving the distribution of method names.
386 ;;; More work here equates to less work in the global hashtable.
387 ;;; To wit: (eq (sxhash '(foo a b c bar)) (sxhash '(foo a b c d))) => T
388 ;;; but the corresponding globaldb-sxhashoids differ.
389 ;;; This is no longer inline because for the cases where it is needed -
390 ;;; names which are not just symbols or (SETF F) - an extra call has no impact.
391 (defun globaldb-sxhashoid (name)
392 ;; We can't use MIX because it's in 'target-sxhash',
393 ;; so use the host's sxhash, but ensure that the result is a target fixnum.
394 ;; (And we can't define this in 'globaldb' because that's too early.)
395 #+sb-xc-host (logand (sxhash name) sb!xc:most-positive-fixnum)
396 #-sb-xc-host
397 (locally
398 (declare (optimize (safety 0))) ; after the argc check
399 ;; TRAVERSE will walk across more cons cells than RECURSE will descend.
400 ;; That's why this isn't just one self-recursive function.
401 (labels ((traverse (accumulator x length-limit)
402 (declare (fixnum length-limit))
403 (cond ((atom x) (sb!int:mix (sxhash x) accumulator))
404 ((zerop length-limit) accumulator)
405 (t (traverse (sb!int:mix (recurse (car x) 4) accumulator)
406 (cdr x) (1- length-limit)))))
407 (recurse (x depthoid) ; depthoid = a blend of level and length
408 (declare (fixnum depthoid))
409 (cond ((atom x) (sxhash x))
410 ((zerop depthoid)
411 #.(logand sb!xc:most-positive-fixnum #36Rglobaldbsxhashoid))
412 (t (sb!int:mix (recurse (car x) (1- depthoid))
413 (recurse (cdr x) (1- depthoid)))))))
414 (traverse 0 name 10))))
416 ;;; A random place for this :-(
417 #+sb-xc-host (setq *info-environment* (make-info-hashtable))