Use SB!IMPL as the implementation package for PARSE-BODY
[sbcl.git] / src / code / early-classoid.lisp
blob0e163432b34ac89495e064346b3aee37988d3572
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 (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 ;; the name of the primary standard keyword constructor, or NIL if none
34 (default-constructor nil :type symbol)
35 ;; all the explicit :CONSTRUCTOR specs, with name defaulted
36 (constructors () :type list)
37 ;; name of copying function
38 (copier-name nil :type symbol)
39 ;; name of type predicate
40 (predicate-name nil :type symbol)
41 ;; the arguments to the :INCLUDE option, or NIL if no included
42 ;; structure
43 (include nil :type list)
44 ;; properties used to define structure-like classes with an
45 ;; arbitrary superclass and that may not have STRUCTURE-CLASS as the
46 ;; metaclass. Syntax is:
47 ;; (superclass-name metaclass-name metaclass-constructor)
48 (alternate-metaclass nil :type list)
49 ;; a list of DEFSTRUCT-SLOT-DESCRIPTION objects for all slots
50 ;; (including included ones)
51 (slots () :type list)
52 ;; a list of (NAME . INDEX) pairs for accessors of included structures
53 (inherited-accessor-alist () :type list)
54 ;; number of elements we've allocated (See also RAW-LENGTH, which is not
55 ;; included in LENGTH.)
56 (length 0 :type index)
57 ;; General kind of implementation.
58 (type 'structure :type (member structure vector list
59 funcallable-structure))
61 ;; The next three slots are for :TYPE'd structures (which aren't
62 ;; classes, DD-CLASS-P = NIL)
64 ;; vector element type
65 (element-type t)
66 ;; T if :NAMED was explicitly specified, NIL otherwise
67 (named nil :type boolean)
68 ;; any INITIAL-OFFSET option on this direct type
69 (offset nil :type (or index null))
71 ;; which :PRINT-mumble option was given, if either was.
72 (print-option nil :type (member nil :print-function :print-object))
73 ;; the argument to the PRINT-FUNCTION or PRINT-OBJECT option.
74 ;; NIL if the option was given with no argument.
75 (printer-fname nil :type (or cons symbol))
77 ;; The number of untagged slots at the end.
78 #!-interleaved-raw-slots (raw-length 0 :type index)
79 ;; the value of the :PURE option, or :UNSPECIFIED. This is only
80 ;; meaningful if DD-CLASS-P = T.
81 (pure :unspecified :type (member t nil :unspecified)))
82 #!-sb-fluid (declaim (freeze-type defstruct-description))
84 ;;;; basic LAYOUT stuff
86 ;;; Note: This bound is set somewhat less than MOST-POSITIVE-FIXNUM
87 ;;; in order to guarantee that several hash values can be added without
88 ;;; overflowing into a bignum.
89 (defconstant layout-clos-hash-limit (1+ (ash sb!xc:most-positive-fixnum -3))
90 #!+sb-doc
91 "the exclusive upper bound on LAYOUT-CLOS-HASH values")
92 ;; This must be DEF!TYPE and not just DEFTYPE because access to slots
93 ;; of a layout occur "before" the structure definition is made in the
94 ;; run-the-xc pass, and the source-transform of a slot accessor
95 ;; wraps (TRULY-THE <type> ...) around %INSTANCE-REF,
96 ;; so <type> had best be defined at that point.
97 (def!type layout-clos-hash () `(integer 0 ,layout-clos-hash-limit))
98 (declaim (ftype (sfunction () layout-clos-hash) random-layout-clos-hash))
100 ;;; The LAYOUT structure is pointed to by the first cell of instance
101 ;;; (or structure) objects. It represents what we need to know for
102 ;;; type checking and garbage collection. Whenever a class is
103 ;;; incompatibly redefined, a new layout is allocated. If two object's
104 ;;; layouts are EQ, then they are exactly the same type.
106 ;;; *** IMPORTANT ***
108 ;;; If you change the slots of LAYOUT, you need to alter genesis as
109 ;;; well, since the initialization of layout slots is hardcoded there.
111 ;;; FIXME: ...it would be better to automate this, of course...
112 (def!struct (layout
113 ;; KLUDGE: A special hack keeps this from being
114 ;; called when building code for the
115 ;; cross-compiler. See comments at the DEFUN for
116 ;; this. -- WHN 19990914
117 (:make-load-form-fun #-sb-xc-host ignore-it
118 ;; KLUDGE: DEF!STRUCT at #+SB-XC-HOST
119 ;; time controls both the
120 ;; build-the-cross-compiler behavior
121 ;; and the run-the-cross-compiler
122 ;; behavior. The value below only
123 ;; works for build-the-cross-compiler.
124 ;; There's a special hack in
125 ;; EMIT-MAKE-LOAD-FORM which gives
126 ;; effectively IGNORE-IT behavior for
127 ;; LAYOUT at run-the-cross-compiler
128 ;; time. It would be cleaner to
129 ;; actually have an IGNORE-IT value
130 ;; stored, but it's hard to see how to
131 ;; do that concisely with the current
132 ;; DEF!STRUCT setup. -- WHN 19990930
133 #+sb-xc-host
134 make-load-form-for-layout))
135 ;; a pseudo-random hash value for use by CLOS.
136 (clos-hash (random-layout-clos-hash) :type layout-clos-hash)
137 ;; the class that this is a layout for
138 (classoid (missing-arg) :type classoid)
139 ;; The value of this slot can be:
140 ;; * :UNINITIALIZED if not initialized yet;
141 ;; * NIL if this is the up-to-date layout for a class; or
142 ;; * T if this layout has been invalidated (by being replaced by
143 ;; a new, more-up-to-date LAYOUT).
144 ;; * something else (probably a list) if the class is a PCL wrapper
145 ;; and PCL has made it invalid and made a note to itself about it
146 (invalid :uninitialized :type (or cons (member nil t :uninitialized)))
147 ;; the layouts for all classes we inherit. If hierarchical, i.e. if
148 ;; DEPTHOID >= 0, then these are ordered by ORDER-LAYOUT-INHERITS
149 ;; (least to most specific), so that each inherited layout appears
150 ;; at its expected depth, i.e. at its LAYOUT-DEPTHOID value.
152 ;; Remaining elements are filled by the non-hierarchical layouts or,
153 ;; if they would otherwise be empty, by copies of succeeding layouts.
154 (inherits #() :type simple-vector)
155 ;; If inheritance is not hierarchical, this is -1. If inheritance is
156 ;; hierarchical, this is the inheritance depth, i.e. (LENGTH INHERITS).
157 ;; Note:
158 ;; (1) This turns out to be a handy encoding for arithmetically
159 ;; comparing deepness; it is generally useful to do a bare numeric
160 ;; comparison of these depthoid values, and we hardly ever need to
161 ;; test whether the values are negative or not.
162 ;; (2) This was called INHERITANCE-DEPTH in classic CMU CL. It was
163 ;; renamed because some of us find it confusing to call something
164 ;; a depth when it isn't quite.
165 (depthoid -1 :type layout-depthoid)
166 ;; the number of top level descriptor cells in each instance
167 (length 0 :type index)
168 ;; If this layout has some kind of compiler meta-info, then this is
169 ;; it. If a structure, then we store the DEFSTRUCT-DESCRIPTION here.
170 (info nil :type (or null defstruct-description))
171 ;; This is true if objects of this class are never modified to
172 ;; contain dynamic pointers in their slots or constant-like
173 ;; substructure (and hence can be copied into read-only space by
174 ;; PURIFY).
176 ;; This slot is known to the C runtime support code.
177 (pure nil :type (member t nil 0))
178 ;; Number of raw words at the end.
179 ;; This slot is known to the C runtime support code.
180 ;; It counts the number of untagged cells, not user-visible slots.
181 ;; e.g. on 32-bit machines, each (COMPLEX DOUBLE-FLOAT) counts as 4.
182 #!-interleaved-raw-slots (n-untagged-slots 0 :type index)
183 ;; Metadata
184 #!+interleaved-raw-slots (untagged-bitmap 0 :type unsigned-byte)
185 #!+interleaved-raw-slots (equalp-tests #() :type simple-vector)
186 ;; Definition location
187 (source-location nil)
188 ;; If this layout is for an object of metatype STANDARD-CLASS,
189 ;; these are the EFFECTIVE-SLOT-DEFINITION metaobjects.
190 (slot-list nil :type list)
191 ;; Information about slots in the class to PCL: this provides fast
192 ;; access to slot-definitions and locations by name, etc.
193 ;; See MAKE-SLOT-TABLE in pcl/slots-boot.lisp for further details.
194 (slot-table #(1 nil) :type simple-vector)
195 ;; True IFF the layout belongs to a standand-instance or a
196 ;; standard-funcallable-instance.
197 ;; Old comment was:
198 ;; FIXME: If we unify wrappers and layouts this can go away, since
199 ;; it is only used in SB-PCL::EMIT-FETCH-WRAPPERS, which can then
200 ;; use INSTANCE-SLOTS-LAYOUT instead (if there is are no slot
201 ;; layouts, there are no slots for it to pull.)
202 ;; But while that's conceivable, it still seems advantageous to have
203 ;; a single bit that decides whether something is STANDARD-OBJECT.
204 (%for-std-class-b 0 :type bit :read-only t))
205 (declaim (freeze-type layout))
207 ;;; The CLASSOID structure is a supertype of all classoid types. A
208 ;;; CLASSOID is also a CTYPE structure as recognized by the type
209 ;;; system. (FIXME: It's also a type specifier, though this might go
210 ;;; away as with the merger of SB-PCL:CLASS and CL:CLASS it's no
211 ;;; longer necessary)
212 (def!struct (classoid
213 (:make-load-form-fun classoid-make-load-form-fun)
214 (:include ctype
215 (class-info (type-class-or-lose 'classoid)))
216 (:constructor nil)
217 #-no-ansi-print-object
218 (:print-object
219 (lambda (class stream)
220 (let ((name (classoid-name class)))
221 (print-unreadable-object (class stream
222 :type t
223 :identity (not name))
224 (format stream
225 ;; FIXME: Make sure that this prints
226 ;; reasonably for anonymous classes.
227 "~:[anonymous~;~:*~S~]~@[ (~(~A~))~]"
228 name
229 (classoid-state class))))))
230 #-sb-xc-host (:pure nil))
231 ;; the value to be returned by CLASSOID-NAME.
232 (name nil :type symbol)
233 ;; the current layout for this class, or NIL if none assigned yet
234 (layout nil :type (or layout null))
235 ;; How sure are we that this class won't be redefined?
236 ;; :READ-ONLY = We are committed to not changing the effective
237 ;; slots or superclasses.
238 ;; :SEALED = We can't even add subclasses.
239 ;; NIL = Anything could happen.
240 (state nil :type (member nil :read-only :sealed))
241 ;; direct superclasses of this class. Always NIL for CLOS classes.
242 (direct-superclasses () :type list)
243 ;; representation of all of the subclasses (direct or indirect) of
244 ;; this class. This is NIL if no subclasses or not initalized yet;
245 ;; otherwise, it's an EQ hash-table mapping CLASSOID objects to the
246 ;; subclass layout that was in effect at the time the subclass was
247 ;; created.
248 (subclasses nil :type (or null hash-table))
249 ;; the PCL class (= CL:CLASS, but with a view to future flexibility
250 ;; we don't just call it the CLASS slot) object for this class, or
251 ;; NIL if none assigned yet
252 (pcl-class nil))
254 ;;;; object types to represent classes
256 ;;; An UNDEFINED-CLASSOID is a cookie we make up to stick in forward
257 ;;; referenced layouts. Users should never see them.
258 (def!struct (undefined-classoid
259 (:include classoid)
260 (:constructor make-undefined-classoid (name))))
262 ;;; BUILT-IN-CLASS is used to represent the standard classes that
263 ;;; aren't defined with DEFSTRUCT and other specially implemented
264 ;;; primitive types whose only attribute is their name.
266 ;;; Some BUILT-IN-CLASSes have a TRANSLATION, which means that they
267 ;;; are effectively DEFTYPE'd to some other type (usually a union of
268 ;;; other classes or a "primitive" type such as NUMBER, ARRAY, etc.)
269 ;;; This translation is done when type specifiers are parsed. Type
270 ;;; system operations (union, subtypep, etc.) should never encounter
271 ;;; translated classes, only their translation.
272 (def!struct (built-in-classoid (:include classoid)
273 (:constructor make-built-in-classoid))
274 ;; the type we translate to on parsing. If NIL, then this class
275 ;; stands on its own; or it can be set to :INITIALIZING for a period
276 ;; during cold-load.
277 (translation nil :type (or ctype (member nil :initializing))))
279 (def!struct (condition-classoid (:include classoid)
280 (:constructor make-condition-classoid))
281 ;; list of CONDITION-SLOT structures for the direct slots of this
282 ;; class
283 (slots nil :type list)
284 ;; list of CONDITION-SLOT structures for all of the effective class
285 ;; slots of this class
286 (class-slots nil :type list)
287 ;; report function or NIL
288 (report nil :type (or function null))
289 ;; list of specifications of the form
291 ;; (INITARG INITFORM THUNK)
293 ;; where THUNK, when called without arguments, returns the value for
294 ;; INITARG.
295 (direct-default-initargs () :type list)
296 ;; class precedence list as a list of CLASS objects, with all
297 ;; non-CONDITION classes removed
298 (cpl () :type list)
299 ;; a list of all the effective instance allocation slots of this
300 ;; class that have a non-constant initform or default-initarg.
301 ;; Values for these slots must be computed in the dynamic
302 ;; environment of MAKE-CONDITION.
303 (hairy-slots nil :type list))
305 ;;;; classoid namespace
307 ;;; We use an indirection to allow forward referencing of class
308 ;;; definitions with load-time resolution.
309 (def!struct (classoid-cell
310 (:constructor make-classoid-cell (name &optional classoid))
311 (:make-load-form-fun (lambda (c)
312 `(find-classoid-cell
313 ',(classoid-cell-name c)
314 :create t)))
315 #-no-ansi-print-object
316 (:print-object (lambda (s stream)
317 (print-unreadable-object (s stream :type t)
318 (prin1 (classoid-cell-name s) stream)))))
319 ;; Name of class we expect to find.
320 (name nil :type symbol :read-only t)
321 ;; Classoid or NIL if not yet defined.
322 (classoid nil :type (or classoid null))
323 ;; PCL class, if any
324 (pcl-class nil))
325 (declaim (freeze-type classoid-cell))
327 ;;; This would be a logical place to define FIND-CLASSOID-CELL,
328 ;;; but since 'globaldb' occurs later in the build order,
329 ;;; you'd have to go out of your way to declare INFO notinline.