0.6.8.15:
[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 \f
18 ;;; Return the type structure corresponding to a type specifier. We
19 ;;; pick off structure types as a special case.
20 ;;;
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.
28                                 (the fixnum
29                                      (logand (the fixnum (sxhash x))
30                                              #x3FF)))
31                :hash-bits 10
32                :init-wrapper !cold-init-forms)
33               ((orig eq))
34   (let ((u (uncross orig)))
35     (or (info :type :builtin u)
36         (let ((spec (type-expand u)))
37           (cond
38            ((and (not (eq spec u))
39                  (info :type :builtin spec)))
40            ((eq (info :type :kind spec) :instance)
41             (sb!xc:find-class spec))
42            ((typep spec 'class)
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.
46             #+sb-xc-host (error
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)
50                 spec))
51            (t
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)))
56                          (symbolp 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)))
62                     (t
63                      (error "bad thing to be a type specifier: ~S"
64                             spec))))))))))
65
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))
72     res))
73
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)))
81                    (t nil))))
82     (if def
83         (type-expand (funcall def (if (consp form) form (list form))))
84         form)))
85
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))
91                                  (enumerable t))
92                        #!+cmu (:pure nil))
93   ;; the Common Lisp type-specifier
94   (specifier nil :type t))
95
96 (define-type-class hairy)
97
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)))
102
103 ;;; ARGS-TYPE objects are used both to represent VALUES types and
104 ;;; to represent FUNCTION types.
105 (defstruct (args-type (:include ctype)
106                       (:constructor nil))
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))
118
119 (defstruct (values-type
120             (:include args-type
121                       (class-info (type-class-or-lose 'values)))))
122
123 (define-type-class values)
124
125 (defstruct (function-type
126             (:include args-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))
133
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
139             (:include ctype
140                       (class-info (type-class-or-lose 'constant))))
141   ;; The type which the argument must be a constant instance of for this type
142   ;; specifier to win.
143   (type (required-argument) :type ctype))
144
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
148 ;;; classes.
149 (defstruct (named-type (:include ctype
150                                  (class-info (type-class-or-lose 'named))))
151   (name nil :type symbol))
152
153 ;;; The Numeric-Type is used to represent all numeric types, including things
154 ;;; such as FIXNUM.
155 (defstruct (numeric-type (:include ctype
156                                    (class-info (type-class-or-lose
157                                                 'number)))
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
161   ;; COMPLEX).
162   ;;
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.)
174   ;;
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)))
183
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))
197
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))
203                                   (enumerable t))
204                         #-sb-xc-host (:pure nil))
205   ;; The things in the set, with no duplications.
206   (members nil :type list))
207
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))
217
218 ;;; Return TYPE converted to canonical form for a situation where the
219 ;;; type '* is equivalent to type T.
220 (defun type-*-to-t (type)
221   (if (type= type *wild-type*)
222       *universal-type*
223       type))
224
225 ;;; A CONS-TYPE is used to represent a CONS type.
226 (defstruct (cons-type (:include ctype
227                                 (:class-info (type-class-or-lose 'cons)))
228                       (:constructor
229                        ;; ANSI says that for CAR and CDR subtype
230                        ;; specifiers '* is equivalent to T. In order
231                        ;; to avoid special cases in SUBTYPEP and
232                        ;; possibly elsewhere, we slam all CONS-TYPE
233                        ;; objects into canonical form w.r.t. this
234                        ;; equivalence at creation time.
235                        make-cons-type (car-raw-type
236                                        cdr-raw-type
237                                        &aux
238                                        (car-type (type-*-to-t car-raw-type))
239                                        (cdr-type (type-*-to-t cdr-raw-type)))))
240   ;; the CAR and CDR element types (to support ANSI (CONS FOO BAR) types)
241   ;;
242   ;; FIXME: Most or all other type structure slots could also be :READ-ONLY.
243   (car-type (required-argument) :type ctype :read-only t)
244   (cdr-type (required-argument) :type ctype :read-only t))
245
246 ;;; Note that the type NAME has been (re)defined, updating the
247 ;;; undefined warnings and VALUES-SPECIFIER-TYPE cache.
248 (defun %note-type-defined (name)
249   (declare (symbol name))
250   (note-name-defined name :type)
251   (when (boundp 'sb!kernel::*values-specifier-type-cache-vector*)
252     (values-specifier-type-cache-clear))
253   (values))
254
255 ;;; Is X a fixnum in the target Lisp?
256 ;;;
257 ;;; KLUDGE: not clear this really belongs in early-type.lisp, but where?
258 (defun target-fixnump (x)
259   (and (integerp x)
260        (<= sb!vm:*target-most-negative-fixnum*
261            x
262            sb!vm:*target-most-positive-fixnum*)))
263
264 (!defun-from-collected-cold-init-forms !early-type-cold-init)