1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!KERNEL")
12 (!begin-collecting-cold-init-forms)
14 ;;; Has the type system been properly initialized? (I.e. is it OK to
16 (defvar *type-system-initialized* #+sb-xc-host nil) ; (set in cold load)
18 ;;; Use experimental type functionality?
20 ;;; REMOVEME: Eventually the new type functionality should be stable
21 ;;; enough that nothing depends on this, and we can remove it again.
23 (!cold-init-forms (setf *xtype?* nil))
25 ;;; Return the type structure corresponding to a type specifier. We
26 ;;; pick off structure types as a special case.
28 ;;; Note: VALUES-SPECIFIER-TYPE-CACHE-CLEAR must be called whenever a
29 ;;; type is defined (or redefined).
30 (defun-cached (values-specifier-type
31 :hash-function (lambda (x)
32 ;; FIXME: the THE FIXNUM stuff is
33 ;; redundant in SBCL (or modern CMU
34 ;; CL) because of type inference.
36 (logand (the fixnum (sxhash x))
39 :init-wrapper !cold-init-forms)
41 (let ((u (uncross orig)))
42 (or (info :type :builtin u)
43 (let ((spec (type-expand u)))
45 ((and (not (eq spec u))
46 (info :type :builtin spec)))
47 ((eq (info :type :kind spec) :instance)
48 (sb!xc:find-class spec))
50 ;; There doesn't seem to be any way to translate
51 ;; (TYPEP SPEC 'BUILT-IN-CLASS) into something which can be
52 ;; executed on the host Common Lisp at cross-compilation time.
54 "stub: (TYPEP SPEC 'BUILT-IN-CLASS) on xc host")
55 (if (typep spec 'built-in-class)
56 (or (built-in-class-translation spec) spec)
60 (lspec (if (atom spec) (list spec) spec))
61 (fun (info :type :translator (car lspec))))
62 (cond (fun (funcall fun lspec))
63 ((or (and (consp spec) (symbolp (car spec)))
65 (when *type-system-initialized*
66 (signal 'parse-unknown-type :specifier spec))
67 ;; (The RETURN-FROM here inhibits caching.)
68 (return-from values-specifier-type
69 (make-unknown-type :specifier spec)))
71 (error "bad thing to be a type specifier: ~S"
74 ;;; Like VALUES-SPECIFIER-TYPE, except that we guarantee to never
75 ;;; return a VALUES type.
76 (defun specifier-type (x)
77 (let ((res (values-specifier-type x)))
78 (when (values-type-p res)
79 (error "VALUES type illegal in this context:~% ~S" x))
82 ;;; Similar to MACROEXPAND, but expands DEFTYPEs. We don't bother
83 ;;; returning a second value.
84 (defun type-expand (form)
85 (let ((def (cond ((symbolp form)
86 (info :type :expander form))
87 ((and (consp form) (symbolp (car form)))
88 (info :type :expander (car form)))
91 (type-expand (funcall def (if (consp form) form (list form))))
94 ;;; A HAIRY-TYPE represents anything too weird to be described
95 ;;; reasonably or to be useful, such as NOT, SATISFIES, unknown types,
96 ;;; and unreasonably complicated types involving AND. We just remember
97 ;;; the original type spec.
98 (defstruct (hairy-type (:include ctype
99 (class-info (type-class-or-lose 'hairy))
103 ;; the Common Lisp type-specifier
104 (specifier nil :type t))
106 (!define-type-class hairy)
108 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet
109 ;;; defined). We make this distinction since we don't want to complain
110 ;;; about types that are hairy but defined.
111 (defstruct (unknown-type (:include hairy-type)
114 ;;; ARGS-TYPE objects are used both to represent VALUES types and
115 ;;; to represent FUNCTION types.
116 (defstruct (args-type (:include ctype)
119 ;; Lists of the type for each required and optional argument.
120 (required nil :type list)
121 (optional nil :type list)
122 ;; The type for the rest arg. NIL if there is no rest arg.
123 (rest nil :type (or ctype null))
124 ;; True if keyword arguments are specified.
125 (keyp nil :type boolean)
126 ;; List of key-info structures describing the keyword arguments.
127 (keywords nil :type list)
128 ;; True if other keywords are allowed.
129 (allowp nil :type boolean))
131 (defstruct (values-type
133 (class-info (type-class-or-lose 'values)))
136 (!define-type-class values)
138 (defstruct (function-type
140 (class-info (type-class-or-lose 'function))))
141 ;; True if the arguments are unrestrictive, i.e. *.
142 (wild-args nil :type boolean)
143 ;; Type describing the return values. This is a values type
144 ;; when multiple values were specified for the return.
145 (returns (required-argument) :type ctype))
147 ;;; The CONSTANT-TYPE structure represents a use of the
148 ;;; CONSTANT-ARGUMENT "type specifier", which is only meaningful in
149 ;;; function argument type specifiers used within the compiler. (It
150 ;;; represents something that the compiler knows to be a constant.)
151 (defstruct (constant-type
153 (class-info (type-class-or-lose 'constant)))
155 ;; The type which the argument must be a constant instance of for this type
157 (type (required-argument) :type ctype))
159 ;;; The NAMED-TYPE is used to represent *, T and NIL. These types must be
160 ;;; super- or sub-types of all types, not just classes and * and NIL aren't
161 ;;; classes anyway, so it wouldn't make much sense to make them built-in
163 (defstruct (named-type (:include ctype
164 (class-info (type-class-or-lose 'named)))
166 (name nil :type symbol))
168 ;;; A NUMERIC-TYPE represents any numeric type, including things
170 (defstruct (numeric-type (:include ctype
171 (class-info (type-class-or-lose
173 #!+negative-zero-is-not-zero
174 (:constructor %make-numeric-type))
175 ;; The kind of numeric type we have. NIL if not specified (just NUMBER or
178 ;; KLUDGE: A slot named CLASS for a non-CLASS value is bad.
179 ;; Especially when a CLASS value *is* stored in another slot (called
180 ;; CLASS-INFO:-). Perhaps this should be called CLASS-NAME? Also
181 ;; weird that comment above says "Numeric-Type is used to represent
182 ;; all numeric types" but this slot doesn't allow COMPLEX as an
183 ;; option.. how does this fall into "not specified" NIL case above?
184 (class nil :type (member integer rational float nil))
185 ;; Format for a float type. NIL if not specified or not a float. Formats
186 ;; which don't exist in a given implementation don't appear here.
187 (format nil :type (or float-format null))
188 ;; Is this a complex numeric type? Null if unknown (only in NUMBER.)
190 ;; FIXME: I'm bewildered by FOO-P names for things not intended to
191 ;; interpreted as truth values. Perhaps rename this COMPLEXNESS?
192 (complexp :real :type (member :real :complex nil))
193 ;; The upper and lower bounds on the value, or NIL if there is no
194 ;; bound. If a list of a number, the bound is exclusive. Integer
195 ;; types never have exclusive bounds.
196 (low nil :type (or number cons null))
197 (high nil :type (or number cons null)))
199 ;;; The Array-Type is used to represent all array types, including
200 ;;; things such as SIMPLE-STRING.
201 (defstruct (array-type (:include ctype
202 (class-info (type-class-or-lose 'array)))
204 ;; the dimensions of the array, or * if unspecified. If a dimension
205 ;; is unspecified, it is *.
206 (dimensions '* :type (or list (member *)))
207 ;; Is this not a simple array type? (:MAYBE means that we don't know.)
208 (complexp :maybe :type (member t nil :maybe))
209 ;; the element type as originally specified
210 (element-type (required-argument) :type ctype)
211 ;; the element type as it is specialized in this implementation
212 (specialized-element-type *wild-type* :type ctype))
214 ;;; A MEMBER-TYPE represent a use of the MEMBER type specifier. We
215 ;;; bother with this at this level because MEMBER types are fairly
216 ;;; important and union and intersection are well defined.
217 (defstruct (member-type (:include ctype
218 (class-info (type-class-or-lose 'member))
221 #-sb-xc-host (:pure nil))
222 ;; the things in the set, with no duplications
223 (members nil :type list))
225 ;;; A COMPOUND-TYPE is a type defined out of a set of types,
226 ;;; the common parent of UNION-TYPE and INTERSECTION-TYPE.
227 (defstruct (compound-type (:include ctype)
230 (types nil :type list :read-only t))
232 ;;; A UNION-TYPE represents a use of the OR type specifier which can't
233 ;;; be canonicalized to something simpler. Canonical form:
234 ;;; 1. There is never more than one MEMBER-TYPE component.
235 ;;; 2. There are never any UNION-TYPE components.
236 (defstruct (union-type (:include compound-type
237 (class-info (type-class-or-lose 'union)))
238 (:constructor %make-union-type (enumerable types))
241 ;;; An INTERSECTION-TYPE represents a use of the AND type specifier
242 ;;; which can't be canonicalized to something simpler. Canonical form:
243 ;;; 1. There is never more than one MEMBER-TYPE component.
244 ;;; 2. There are never any INTERSECTION-TYPE or UNION-TYPE components.
245 (defstruct (intersection-type (:include compound-type
246 (class-info (type-class-or-lose
248 (:constructor %make-intersection-type
252 ;;; Return TYPE converted to canonical form for a situation where the
253 ;;; "type" '* (which SBCL still represents as a type even though ANSI
254 ;;; CL defines it as a related but different kind of placeholder) is
255 ;;; equivalent to type T.
256 (defun type-*-to-t (type)
257 (if (type= type *wild-type*)
261 ;;; A CONS-TYPE is used to represent a CONS type.
262 (defstruct (cons-type (:include ctype
263 (:class-info (type-class-or-lose 'cons)))
265 ;; ANSI says that for CAR and CDR subtype
266 ;; specifiers '* is equivalent to T. In order
267 ;; to avoid special cases in SUBTYPEP and
268 ;; possibly elsewhere, we slam all CONS-TYPE
269 ;; objects into canonical form w.r.t. this
270 ;; equivalence at creation time.
271 make-cons-type (car-raw-type
274 (car-type (type-*-to-t car-raw-type))
275 (cdr-type (type-*-to-t cdr-raw-type))))
277 ;; the CAR and CDR element types (to support ANSI (CONS FOO BAR) types)
279 ;; FIXME: Most or all other type structure slots could also be :READ-ONLY.
280 (car-type (required-argument) :type ctype :read-only t)
281 (cdr-type (required-argument) :type ctype :read-only t))
283 ;;; Note that the type NAME has been (re)defined, updating the
284 ;;; undefined warnings and VALUES-SPECIFIER-TYPE cache.
285 (defun %note-type-defined (name)
286 (declare (symbol name))
287 (note-name-defined name :type)
288 (when (boundp 'sb!kernel::*values-specifier-type-cache-vector*)
289 (values-specifier-type-cache-clear))
292 ;;; Is X a fixnum in the target Lisp?
294 ;;; KLUDGE: not clear this really belongs in early-type.lisp, but where?
295 (defun target-fixnump (x)
297 (<= sb!vm:*target-most-negative-fixnum*
299 sb!vm:*target-most-positive-fixnum*)))
301 (!defun-from-collected-cold-init-forms !early-type-cold-init)