0.6.11.13:
[sbcl.git] / src / code / early-type.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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.
9
10 (in-package "SB!KERNEL")
11
12 (!begin-collecting-cold-init-forms)
13
14 ;;; Has the type system been properly initialized? (I.e. is it OK to
15 ;;; use it?)
16 (defvar *type-system-initialized* #+sb-xc-host nil) ; (set in cold load)
17
18 ;;; Use experimental type functionality?
19 ;;;
20 ;;; REMOVEME: Eventually the new type functionality should be stable
21 ;;; enough that nothing depends on this, and we can remove it again.
22 (defvar *xtype?*)
23 (!cold-init-forms (setf *xtype?* nil))
24 \f
25 ;;; Return the type structure corresponding to a type specifier. We
26 ;;; pick off structure types as a special case.
27 ;;;
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.
35                                 (the fixnum
36                                      (logand (the fixnum (sxhash x))
37                                              #x3FF)))
38                :hash-bits 10
39                :init-wrapper !cold-init-forms)
40               ((orig eq))
41   (let ((u (uncross orig)))
42     (or (info :type :builtin u)
43         (let ((spec (type-expand u)))
44           (cond
45            ((and (not (eq spec u))
46                  (info :type :builtin spec)))
47            ((eq (info :type :kind spec) :instance)
48             (sb!xc:find-class spec))
49            ((typep spec 'class)
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.
53             #+sb-xc-host (error
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)
57                 spec))
58            (t
59             (let* (;; FIXME: This automatic promotion of FOO-style
60                    ;; specs to (FOO)-style specs violates the ANSI
61                    ;; standard. Unfortunately, we can't fix the
62                    ;; problem just by removing it, since then things
63                    ;; downstream should break. But at some point we
64                    ;; should fix this and the things downstream too.
65                    (lspec (if (atom spec) (list spec) spec))
66                    (fun (info :type :translator (car lspec))))
67               (cond (fun
68                      (funcall fun lspec))
69                     ((or (and (consp spec) (symbolp (car spec)))
70                          (symbolp spec))
71                      (when *type-system-initialized*
72                        (signal 'parse-unknown-type :specifier spec))
73                      ;; (The RETURN-FROM here inhibits caching.)
74                      (return-from values-specifier-type
75                        (make-unknown-type :specifier spec)))
76                     (t
77                      (error "bad thing to be a type specifier: ~S"
78                             spec))))))))))
79
80 ;;; Like VALUES-SPECIFIER-TYPE, except that we guarantee to never
81 ;;; return a VALUES type.
82 (defun specifier-type (x)
83   (let ((res (values-specifier-type x)))
84     (when (values-type-p res)
85       (error "VALUES type illegal in this context:~%  ~S" x))
86     res))
87
88 ;;; Similar to MACROEXPAND, but expands DEFTYPEs. We don't bother
89 ;;; returning a second value.
90 (defun type-expand (form)
91   (let ((def (cond ((symbolp form)
92                     (info :type :expander form))
93                    ((and (consp form) (symbolp (car form)))
94                     (info :type :expander (car form)))
95                    (t nil))))
96     (if def
97         (type-expand (funcall def (if (consp form) form (list form))))
98         form)))
99
100 ;;; A HAIRY-TYPE represents anything too weird to be described
101 ;;; reasonably or to be useful, such as NOT, SATISFIES, unknown types,
102 ;;; and unreasonably complicated types involving AND. We just remember
103 ;;; the original type spec.
104 (defstruct (hairy-type (:include ctype
105                                  (class-info (type-class-or-lose 'hairy))
106                                  (enumerable t))
107                        (:copier nil)
108                        #!+cmu (:pure nil))
109   ;; the Common Lisp type-specifier
110   (specifier nil :type t))
111
112 (!define-type-class hairy)
113
114 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet
115 ;;; defined). We make this distinction since we don't want to complain
116 ;;; about types that are hairy but defined.
117 (defstruct (unknown-type (:include hairy-type)
118                          (:copier nil)))
119
120 ;;; ARGS-TYPE objects are used both to represent VALUES types and
121 ;;; to represent FUNCTION types.
122 (defstruct (args-type (:include ctype)
123                       (:constructor nil)
124                       (:copier nil))
125   ;; Lists of the type for each required and optional argument.
126   (required nil :type list)
127   (optional nil :type list)
128   ;; The type for the rest arg. NIL if there is no rest arg.
129   (rest nil :type (or ctype null))
130   ;; true if &KEY arguments are specified
131   (keyp nil :type boolean)
132   ;; list of KEY-INFO structures describing the &KEY arguments
133   (keywords nil :type list)
134   ;; true if other &KEY arguments are allowed
135   (allowp nil :type boolean))
136
137 (defstruct (values-type
138             (:include args-type
139                       (class-info (type-class-or-lose 'values)))
140             (:copier nil)))
141
142 (!define-type-class values)
143
144 (defstruct (function-type
145             (:include args-type
146                       (class-info (type-class-or-lose 'function))))
147   ;; True if the arguments are unrestrictive, i.e. *.
148   (wild-args nil :type boolean)
149   ;; Type describing the return values. This is a values type
150   ;; when multiple values were specified for the return.
151   (returns (required-argument) :type ctype))
152
153 ;;; The CONSTANT-TYPE structure represents a use of the
154 ;;; CONSTANT-ARGUMENT "type specifier", which is only meaningful in
155 ;;; function argument type specifiers used within the compiler. (It
156 ;;; represents something that the compiler knows to be a constant.)
157 (defstruct (constant-type
158             (:include ctype
159                       (class-info (type-class-or-lose 'constant)))
160             (:copier nil))
161   ;; The type which the argument must be a constant instance of for this type
162   ;; specifier to win.
163   (type (required-argument) :type ctype))
164
165 ;;; The NAMED-TYPE is used to represent *, T and NIL. These types must be
166 ;;; super- or sub-types of all types, not just classes and * and NIL aren't
167 ;;; classes anyway, so it wouldn't make much sense to make them built-in
168 ;;; classes.
169 (defstruct (named-type (:include ctype
170                                  (class-info (type-class-or-lose 'named)))
171                        (:copier nil))
172   (name nil :type symbol))
173
174 ;;; a list of all the float "formats" (i.e. internal representations;
175 ;;; nothing to do with #'FORMAT), in order of decreasing precision
176 (eval-when (:compile-toplevel :load-toplevel :execute)
177   (defparameter *float-formats*
178     '(long-float double-float single-float short-float)))
179
180 ;;; The type of a float format.
181 (deftype float-format () `(member ,@*float-formats*))
182
183 ;;; A NUMERIC-TYPE represents any numeric type, including things
184 ;;; such as FIXNUM.
185 (defstruct (numeric-type (:include ctype
186                                    (class-info (type-class-or-lose
187                                                 'number)))
188                          #!+negative-zero-is-not-zero
189                          (:constructor %make-numeric-type))
190   ;; the kind of numeric type we have, or NIL if not specified (just
191   ;; NUMBER or COMPLEX)
192   ;;
193   ;; KLUDGE: A slot named CLASS for a non-CLASS value is bad.
194   ;; Especially when a CLASS value *is* stored in another slot (called
195   ;; CLASS-INFO:-). Perhaps this should be called CLASS-NAME? Also
196   ;; weird that comment above says "Numeric-Type is used to represent
197   ;; all numeric types" but this slot doesn't allow COMPLEX as an
198   ;; option.. how does this fall into "not specified" NIL case above?
199   (class nil :type (member integer rational float nil))
200   ;; "format" for a float type (i.e. type specifier for a CPU
201   ;; representation of floating point, e.g. 'SINGLE-FLOAT -- nothing
202   ;; to do with #'FORMAT), or NIL if not specified or not a float.
203   ;; Formats which don't exist in a given implementation don't appear
204   ;; here.
205   (format nil
206           ;; FIXME: suppressed because of cold init problems under
207           ;; hacked type system in sbcl-0.6.11.13, should be restored
208           #+nil :type #+nil (or float-format null))
209   ;; Is this a complex numeric type?  Null if unknown (only in NUMBER).
210   ;;
211   ;; FIXME: I'm bewildered by FOO-P names for things not intended to
212   ;; interpreted as truth values. Perhaps rename this COMPLEXNESS?
213   (complexp :real :type (member :real :complex nil))
214   ;; The upper and lower bounds on the value, or NIL if there is no
215   ;; bound. If a list of a number, the bound is exclusive. Integer
216   ;; types never have exclusive bounds.
217   (low nil :type (or number cons null))
218   (high nil :type (or number cons null)))
219
220 ;;; An ARRAY-TYPE is used to represent any array type, including
221 ;;; things such as SIMPLE-STRING.
222 (defstruct (array-type (:include ctype
223                                  (class-info (type-class-or-lose 'array)))
224                        (:copier nil))
225   ;; the dimensions of the array, or * if unspecified. If a dimension
226   ;; is unspecified, it is *.
227   (dimensions '* :type (or list (member *)))
228   ;; Is this not a simple array type? (:MAYBE means that we don't know.)
229   (complexp :maybe :type (member t nil :maybe))
230   ;; the element type as originally specified
231   (element-type (required-argument) :type ctype)
232   ;; the element type as it is specialized in this implementation
233   (specialized-element-type *wild-type* :type ctype))
234
235 ;;; A MEMBER-TYPE represent a use of the MEMBER type specifier. We
236 ;;; bother with this at this level because MEMBER types are fairly
237 ;;; important and union and intersection are well defined.
238 (defstruct (member-type (:include ctype
239                                   (class-info (type-class-or-lose 'member))
240                                   (enumerable t))
241                         (:copier nil)
242                         #-sb-xc-host (:pure nil))
243   ;; the things in the set, with no duplications
244   (members nil :type list))
245
246 ;;; A COMPOUND-TYPE is a type defined out of a set of types, the
247 ;;; common parent of UNION-TYPE and INTERSECTION-TYPE.
248 (defstruct (compound-type (:include ctype)
249                           (:constructor nil)
250                           (:copier nil))
251   (types nil
252          ;; FIXME: This type declaration was suppresed as a temporary
253          ;; hack to work around sbcl-0.6.11.13 cold init problems.
254          ;; Restore it.
255          #+nil :type #+nil list 
256          :read-only t))
257
258 ;;; A UNION-TYPE represents a use of the OR type specifier which we
259 ;;; couldn't canonicalize to something simpler. Canonical form:
260 ;;;   1. All possible pairwise simplifications (using the UNION2 type
261 ;;;      methods) have been performed. Thus e.g. there is never more
262 ;;;      than one MEMBER-TYPE component. FIXME: As of sbcl-0.6.11.13,
263 ;;;      this hadn't been fully implemented yet.
264 ;;;   2. There are never any UNION-TYPE components.
265 (defstruct (union-type (:include compound-type
266                                  (class-info (type-class-or-lose 'union)))
267                        (:constructor %make-union-type (enumerable types))
268                        (:copier nil)))
269
270 ;;; An INTERSECTION-TYPE represents a use of the AND type specifier
271 ;;; which we couldn't canonicalize to something simpler. Canonical form:
272 ;;;   1. All possible pairwise simplifications (using the INTERSECTION2
273 ;;;      type methods) have been performed. Thus e.g. there is never more
274 ;;;      than one MEMBER-TYPE component.
275 ;;;   2. There are never any INTERSECTION-TYPE components: we've
276 ;;;      flattened everything into a single INTERSECTION-TYPE object.
277 ;;;   3. There are never any UNION-TYPE components. Either we should
278 ;;;      use the distributive rule to rearrange things so that
279 ;;;      unions contain intersections and not vice versa, or we
280 ;;;      should just punt to using a HAIRY-TYPE.
281 (defstruct (intersection-type (:include compound-type
282                                         (class-info (type-class-or-lose
283                                                      'intersection)))
284                               (:constructor %make-intersection-type
285                                             (enumerable types))
286                               (:copier nil)))
287
288 ;;; Return TYPE converted to canonical form for a situation where the
289 ;;; "type" '* (which SBCL still represents as a type even though ANSI
290 ;;; CL defines it as a related but different kind of placeholder) is
291 ;;; equivalent to type T.
292 (defun type-*-to-t (type)
293   (if (type= type *wild-type*)
294       *universal-type*
295       type))
296
297 ;;; A CONS-TYPE is used to represent a CONS type.
298 (defstruct (cons-type (:include ctype (:class-info (type-class-or-lose 'cons)))
299                       (:constructor
300                        ;; ANSI says that for CAR and CDR subtype
301                        ;; specifiers '* is equivalent to T. In order
302                        ;; to avoid special cases in SUBTYPEP and
303                        ;; possibly elsewhere, we slam all CONS-TYPE
304                        ;; objects into canonical form w.r.t. this
305                        ;; equivalence at creation time.
306                        make-cons-type (car-raw-type
307                                        cdr-raw-type
308                                        &aux
309                                        (car-type (type-*-to-t car-raw-type))
310                                        (cdr-type (type-*-to-t cdr-raw-type))))
311                       (:copier nil))
312   ;; the CAR and CDR element types (to support ANSI (CONS FOO BAR) types)
313   ;;
314   ;; FIXME: Most or all other type structure slots could also be :READ-ONLY.
315   (car-type (required-argument) :type ctype :read-only t)
316   (cdr-type (required-argument) :type ctype :read-only t))
317
318 ;;; Note that the type NAME has been (re)defined, updating the
319 ;;; undefined warnings and VALUES-SPECIFIER-TYPE cache.
320 (defun %note-type-defined (name)
321   (declare (symbol name))
322   (note-name-defined name :type)
323   (when (boundp 'sb!kernel::*values-specifier-type-cache-vector*)
324     (values-specifier-type-cache-clear))
325   (values))
326
327 ;;; Is X a fixnum in the target Lisp?
328 ;;;
329 ;;; KLUDGE: not clear this really belongs in early-type.lisp, but where?
330 (defun target-fixnump (x)
331   (and (integerp x)
332        (<= sb!vm:*target-most-negative-fixnum*
333            x
334            sb!vm:*target-most-positive-fixnum*)))
335
336 (!defun-from-collected-cold-init-forms !early-type-cold-init)