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 ;;; Return the type structure corresponding to a type specifier. We
19 ;;; pick off structure types as a special case.
21 ;;; Note: VALUES-SPECIFIER-TYPE-CACHE-CLEAR must be called whenever a
22 ;;; type is defined (or redefined).
23 (defun-cached (values-specifier-type
24 :hash-function (lambda (x)
25 ;; FIXME: the THE FIXNUM stuff is
26 ;; redundant in SBCL (or modern CMU
27 ;; CL) because of type inference.
29 (logand (the fixnum (sxhash x))
32 :init-wrapper !cold-init-forms)
34 (let ((u (uncross orig)))
35 (or (info :type :builtin u)
36 (let ((spec (type-expand u)))
38 ((and (not (eq spec u))
39 (info :type :builtin spec)))
40 ((eq (info :type :kind spec) :instance)
41 (sb!xc:find-class spec))
43 ;; There doesn't seem to be any way to translate
44 ;; (TYPEP SPEC 'BUILT-IN-CLASS) into something which can be
45 ;; executed on the host Common Lisp at cross-compilation time.
47 "stub: (TYPEP SPEC 'BUILT-IN-CLASS) on xc host")
48 (if (typep spec 'built-in-class)
49 (or (built-in-class-translation spec) spec)
52 (let* ((lspec (if (atom spec) (list spec) spec))
53 (fun (info :type :translator (car lspec))))
54 (cond (fun (funcall fun lspec))
55 ((or (and (consp spec) (symbolp (car spec)))
57 (when *type-system-initialized*
58 (signal 'parse-unknown-type :specifier spec))
59 ;; (The RETURN-FROM here inhibits caching.)
60 (return-from values-specifier-type
61 (make-unknown-type :specifier spec)))
63 (error "bad thing to be a type specifier: ~S"
66 ;;; Like VALUES-SPECIFIER-TYPE, except that we guarantee to never
67 ;;; return a VALUES type.
68 (defun specifier-type (x)
69 (let ((res (values-specifier-type x)))
70 (when (values-type-p res)
71 (error "VALUES type illegal in this context:~% ~S" x))
74 ;;; Similar to MACROEXPAND, but expands DEFTYPEs. We don't bother
75 ;;; returning a second value.
76 (defun type-expand (form)
77 (let ((def (cond ((symbolp form)
78 (info :type :expander form))
79 ((and (consp form) (symbolp (car form)))
80 (info :type :expander (car form)))
83 (type-expand (funcall def (if (consp form) form (list form))))
86 ;;; A HAIRY-TYPE represents anything too weird to be described
87 ;;; reasonably or to be useful, such as AND, NOT and SATISFIES and
88 ;;; unknown types. We just remember the original type spec.
89 (defstruct (hairy-type (:include ctype
90 (class-info (type-class-or-lose 'hairy))
93 ;; the Common Lisp type-specifier
94 (specifier nil :type t))
96 (define-type-class hairy)
98 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet
99 ;;; defined). We make this distinction since we don't want to complain
100 ;;; about types that are hairy but defined.
101 (defstruct (unknown-type (:include hairy-type)))
103 ;;; ARGS-TYPE objects are used both to represent VALUES types and
104 ;;; to represent FUNCTION types.
105 (defstruct (args-type (:include ctype)
107 ;; Lists of the type for each required and optional argument.
108 (required nil :type list)
109 (optional nil :type list)
110 ;; The type for the rest arg. NIL if there is no rest arg.
111 (rest nil :type (or ctype null))
112 ;; True if keyword arguments are specified.
113 (keyp nil :type boolean)
114 ;; List of key-info structures describing the keyword arguments.
115 (keywords nil :type list)
116 ;; True if other keywords are allowed.
117 (allowp nil :type boolean))
119 (defstruct (values-type
121 (class-info (type-class-or-lose 'values)))))
123 (define-type-class values)
125 (defstruct (function-type
127 (class-info (type-class-or-lose 'function))))
128 ;; True if the arguments are unrestrictive, i.e. *.
129 (wild-args nil :type boolean)
130 ;; Type describing the return values. This is a values type
131 ;; when multiple values were specified for the return.
132 (returns (required-argument) :type ctype))
134 ;;; The CONSTANT-TYPE structure represents a use of the
135 ;;; CONSTANT-ARGUMENT "type specifier", which is only meaningful in
136 ;;; function argument type specifiers used within the compiler. (It
137 ;;; represents something that the compiler knows to be a constant.)
138 (defstruct (constant-type
140 (class-info (type-class-or-lose 'constant))))
141 ;; The type which the argument must be a constant instance of for this type
143 (type (required-argument) :type ctype))
145 ;;; The NAMED-TYPE is used to represent *, T and NIL. These types must be
146 ;;; super or sub types of all types, not just classes and * & NIL aren't
147 ;;; classes anyway, so it wouldn't make much sense to make them built-in
149 (defstruct (named-type (:include ctype
150 (class-info (type-class-or-lose 'named))))
151 (name nil :type symbol))
153 ;;; The Numeric-Type is used to represent all numeric types, including things
155 (defstruct (numeric-type (:include ctype
156 (class-info (type-class-or-lose
158 #!+negative-zero-is-not-zero
159 (:constructor %make-numeric-type))
160 ;; The kind of numeric type we have. NIL if not specified (just NUMBER or
163 ;; KLUDGE: A slot named CLASS for a non-CLASS value is bad.
164 ;; Especially when a CLASS value *is* stored in another slot (called
165 ;; CLASS-INFO:-). Perhaps this should be called CLASS-NAME? Also
166 ;; weird that comment above says "Numeric-Type is used to represent
167 ;; all numeric types" but this slot doesn't allow COMPLEX as an
168 ;; option.. how does this fall into "not specified" NIL case above?
169 (class nil :type (member integer rational float nil))
170 ;; Format for a float type. NIL if not specified or not a float. Formats
171 ;; which don't exist in a given implementation don't appear here.
172 (format nil :type (or float-format null))
173 ;; Is this a complex numeric type? Null if unknown (only in NUMBER.)
175 ;; FIXME: I'm bewildered by FOO-P names for things not intended to
176 ;; interpreted as truth values. Perhaps rename this COMPLEXNESS?
177 (complexp :real :type (member :real :complex nil))
178 ;; The upper and lower bounds on the value, or NIL if there is no
179 ;; bound. If a list of a number, the bound is exclusive. Integer
180 ;; types never have exclusive bounds.
181 (low nil :type (or number cons null))
182 (high nil :type (or number cons null)))
184 ;;; The Array-Type is used to represent all array types, including
185 ;;; things such as SIMPLE-STRING.
186 (defstruct (array-type (:include ctype
187 (class-info (type-class-or-lose 'array))))
188 ;; The dimensions of the array. * if unspecified. If a dimension is
189 ;; unspecified, it is *.
190 (dimensions '* :type (or list (member *)))
191 ;; Is this not a simple array type? (:MAYBE means that we don't know.)
192 (complexp :maybe :type (member t nil :maybe))
193 ;; The element type as originally specified.
194 (element-type (required-argument) :type ctype)
195 ;; The element type as it is specialized in this implementation.
196 (specialized-element-type *wild-type* :type ctype))
198 ;;; The Member-Type represents uses of the MEMBER type specifier. We
199 ;;; bother with this at this level because MEMBER types are fairly
200 ;;; important and union and intersection are well defined.
201 (defstruct (member-type (:include ctype
202 (class-info (type-class-or-lose 'member))
204 #-sb-xc-host (:pure nil))
205 ;; The things in the set, with no duplications.
206 (members nil :type list))
208 ;;; A UNION-TYPE represents a use of the OR type specifier which can't
209 ;;; be canonicalized to something simpler. Canonical form:
210 ;;; 1. There is never more than one Member-Type component.
211 ;;; 2. There are never any Union-Type components.
212 (defstruct (union-type (:include ctype
213 (class-info (type-class-or-lose 'union)))
214 (:constructor %make-union-type (enumerable types)))
215 ;; The types in the union.
216 (types nil :type list))
218 ;;; Note that the type Name has been (re)defined, updating the
219 ;;; undefined warnings and VALUES-SPECIFIER-TYPE cache.
220 (defun %note-type-defined (name)
221 (declare (symbol name))
222 (note-name-defined name :type)
223 (when (boundp 'sb!kernel::*values-specifier-type-cache-vector*)
224 (values-specifier-type-cache-clear))
227 ;;; Is X a fixnum in the target Lisp?
229 ;;; KLUDGE: not clear this really belongs in early-type.lisp, but where?
230 (defun target-fixnump (x)
232 (<= sb!vm:*target-most-negative-fixnum*
234 sb!vm:*target-most-positive-fixnum*)))
236 (!defun-from-collected-cold-init-forms !early-type-cold-init)