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 ;;;; representations of types
20 ;;; A HAIRY-TYPE represents anything too weird to be described
21 ;;; reasonably or to be useful, such as NOT, SATISFIES, unknown types,
22 ;;; and unreasonably complicated types involving AND. We just remember
23 ;;; the original type spec.
24 (defstruct (hairy-type (:include ctype
25 (class-info (type-class-or-lose 'hairy))
27 (might-contain-other-types-p t))
30 ;; the Common Lisp type-specifier of the type we represent
31 (specifier nil :type t))
33 (!define-type-class hairy)
35 ;;; An UNKNOWN-TYPE is a type not known to the type system (not yet
36 ;;; defined). We make this distinction since we don't want to complain
37 ;;; about types that are hairy but defined.
38 (defstruct (unknown-type (:include hairy-type)
41 (defstruct (negation-type (:include ctype
42 (class-info (type-class-or-lose 'negation))
43 ;; FIXME: is this right? It's
44 ;; what they had before, anyway
46 (might-contain-other-types-p t))
49 (type (missing-arg) :type ctype))
51 (!define-type-class negation)
53 ;;; ARGS-TYPE objects are used both to represent VALUES types and
54 ;;; to represent FUNCTION types.
55 (defstruct (args-type (:include ctype)
58 ;; Lists of the type for each required and optional argument.
59 (required nil :type list)
60 (optional nil :type list)
61 ;; The type for the rest arg. NIL if there is no &REST arg.
62 (rest nil :type (or ctype null))
63 ;; true if &KEY arguments are specified
64 (keyp nil :type boolean)
65 ;; list of KEY-INFO structures describing the &KEY arguments
66 (keywords nil :type list)
67 ;; true if other &KEY arguments are allowed
68 (allowp nil :type boolean))
70 (defun canonicalize-args-type-args (required optional rest)
71 (when (eq rest *empty-type*)
74 (loop with last-not-rest = nil
77 do (cond ((eq opt *empty-type*)
78 (return (values required (subseq optional i) rest)))
80 (setq last-not-rest i)))
81 finally (return (values required
83 (subseq optional 0 (1+ last-not-rest))
87 (defun args-types (lambda-list-like-thing)
89 (required optional restp rest keyp keys allowp auxp aux
90 morep more-context more-count llk-p)
91 (parse-lambda-list-like-thing lambda-list-like-thing)
92 (declare (ignore aux morep more-context more-count))
94 (error "&AUX in a FUNCTION or VALUES type: ~S." lambda-list-like-thing))
95 (let ((required (mapcar #'single-value-specifier-type required))
96 (optional (mapcar #'single-value-specifier-type optional))
97 (rest (when restp (single-value-specifier-type rest)))
101 (unless (proper-list-of-length-p key 2)
102 (error "Keyword type description is not a two-list: ~S." key))
103 (let ((kwd (first key)))
104 (when (find kwd (key-info) :key #'key-info-name)
105 (error "~@<repeated keyword ~S in lambda list: ~2I~_~S~:>"
106 kwd lambda-list-like-thing))
110 :type (single-value-specifier-type (second key))))))
112 (multiple-value-bind (required optional rest)
113 (canonicalize-args-type-args required optional rest)
114 (values required optional rest keyp keywords allowp llk-p)))))
116 (defstruct (values-type
118 (class-info (type-class-or-lose 'values)))
119 (:constructor %make-values-type)
122 (defun-cached (make-values-type-cached
124 :hash-function (lambda (req opt rest allowp)
126 (type-list-cache-hash req)
127 (type-list-cache-hash opt)
129 (type-hash-value rest)
133 ((required equal-but-no-car-recursion)
134 (optional equal-but-no-car-recursion)
137 (%make-values-type :required required
142 ;;; FIXME: ANSI VALUES has a short form (without lambda list
143 ;;; keywords), which should be translated into a long one.
144 (defun make-values-type (&key (args nil argsp)
145 required optional rest allowp)
149 (multiple-value-bind (required optional rest keyp keywords allowp
152 (declare (ignore keywords))
154 (error "&KEY appeared in a VALUES type specifier ~S."
157 (make-values-type :required required
161 (make-short-values-type required))))
162 (multiple-value-bind (required optional rest)
163 (canonicalize-args-type-args required optional rest)
164 (cond ((and (null required)
166 (eq rest *universal-type*))
168 ((memq *empty-type* required)
170 (t (make-values-type-cached required optional
173 (!define-type-class values)
175 ;;; (SPECIFIER-TYPE 'FUNCTION) and its subtypes
176 (defstruct (fun-type (:include args-type
177 (class-info (type-class-or-lose 'function)))
179 %make-fun-type (&key required optional rest
183 &aux (rest (if (eq rest *empty-type*)
186 ;; true if the arguments are unrestrictive, i.e. *
187 (wild-args nil :type boolean)
188 ;; type describing the return values. This is a values type
189 ;; when multiple values were specified for the return.
190 (returns (missing-arg) :type ctype))
191 (defun make-fun-type (&rest initargs
192 &key (args nil argsp) returns &allow-other-keys)
195 (if (eq returns *wild-type*)
196 (specifier-type 'function)
197 (%make-fun-type :wild-args t :returns returns))
198 (multiple-value-bind (required optional rest keyp keywords allowp)
200 (if (and (null required)
202 (eq rest *universal-type*)
204 (if (eq returns *wild-type*)
205 (specifier-type 'function)
206 (%make-fun-type :wild-args t :returns returns))
207 (%make-fun-type :required required
214 ;; FIXME: are we really sure that we won't make something that
215 ;; looks like a completely wild function here?
216 (apply #'%make-fun-type initargs)))
218 ;;; The CONSTANT-TYPE structure represents a use of the CONSTANT-ARG
219 ;;; "type specifier", which is only meaningful in function argument
220 ;;; type specifiers used within the compiler. (It represents something
221 ;;; that the compiler knows to be a constant.)
222 (defstruct (constant-type
224 (class-info (type-class-or-lose 'constant)))
226 ;; The type which the argument must be a constant instance of for this type
228 (type (missing-arg) :type ctype))
230 ;;; The NAMED-TYPE is used to represent *, T and NIL. These types must
231 ;;; be super- or sub-types of all types, not just classes and * and
232 ;;; NIL aren't classes anyway, so it wouldn't make much sense to make
233 ;;; them built-in classes.
234 (defstruct (named-type (:include ctype
235 (class-info (type-class-or-lose 'named)))
237 (name nil :type symbol))
239 ;;; a list of all the float "formats" (i.e. internal representations;
240 ;;; nothing to do with #'FORMAT), in order of decreasing precision
241 (eval-when (:compile-toplevel :load-toplevel :execute)
242 (defparameter *float-formats*
243 '(long-float double-float single-float short-float)))
245 ;;; The type of a float format.
246 (deftype float-format () `(member ,@*float-formats*))
248 ;;; A NUMERIC-TYPE represents any numeric type, including things
250 (defstruct (numeric-type (:include ctype
251 (class-info (type-class-or-lose 'number)))
252 (:constructor %make-numeric-type)
254 ;; the kind of numeric type we have, or NIL if not specified (just
255 ;; NUMBER or COMPLEX)
257 ;; KLUDGE: A slot named CLASS for a non-CLASS value is bad.
258 ;; Especially when a CLASS value *is* stored in another slot (called
259 ;; CLASS-INFO:-). Perhaps this should be called CLASS-NAME? Also
260 ;; weird that comment above says "Numeric-Type is used to represent
261 ;; all numeric types" but this slot doesn't allow COMPLEX as an
262 ;; option.. how does this fall into "not specified" NIL case above?
263 ;; Perhaps someday we can switch to CLOS and make NUMERIC-TYPE
264 ;; be an abstract base class and INTEGER-TYPE, RATIONAL-TYPE, and
265 ;; whatnot be concrete subclasses..
266 (class nil :type (member integer rational float nil) :read-only t)
267 ;; "format" for a float type (i.e. type specifier for a CPU
268 ;; representation of floating point, e.g. 'SINGLE-FLOAT -- nothing
269 ;; to do with #'FORMAT), or NIL if not specified or not a float.
270 ;; Formats which don't exist in a given implementation don't appear
272 (format nil :type (or float-format null) :read-only t)
273 ;; Is this a complex numeric type? Null if unknown (only in NUMBER).
275 ;; FIXME: I'm bewildered by FOO-P names for things not intended to
276 ;; interpreted as truth values. Perhaps rename this COMPLEXNESS?
277 (complexp :real :type (member :real :complex nil) :read-only t)
278 ;; The upper and lower bounds on the value, or NIL if there is no
279 ;; bound. If a list of a number, the bound is exclusive. Integer
280 ;; types never have exclusive bounds, i.e. they may have them on
281 ;; input, but they're canonicalized to inclusive bounds before we
283 (low nil :type (or number cons null) :read-only t)
284 (high nil :type (or number cons null) :read-only t))
286 ;;; Impose canonicalization rules for NUMERIC-TYPE. Note that in some
287 ;;; cases, despite the name, we return *EMPTY-TYPE* instead of a
289 (defun make-numeric-type (&key class format (complexp :real) low high
291 ;; if interval is empty
294 (if (or (consp low) (consp high)) ; if either bound is exclusive
295 (>= (type-bound-number low) (type-bound-number high))
298 (multiple-value-bind (canonical-low canonical-high)
301 ;; INTEGER types always have their LOW and HIGH bounds
302 ;; represented as inclusive, not exclusive values.
303 (values (if (consp low)
304 (1+ (type-bound-number low))
307 (1- (type-bound-number high))
310 ;; no canonicalization necessary
312 (when (and (eq class 'rational)
313 (integerp canonical-low)
314 (integerp canonical-high)
315 (= canonical-low canonical-high))
316 (setf class 'integer))
317 (%make-numeric-type :class class
322 :enumerable enumerable))))
324 (defun modified-numeric-type (base
326 (class (numeric-type-class base))
327 (format (numeric-type-format base))
328 (complexp (numeric-type-complexp base))
329 (low (numeric-type-low base))
330 (high (numeric-type-high base))
331 (enumerable (numeric-type-enumerable base)))
332 (make-numeric-type :class class
337 :enumerable enumerable))
339 ;;; An ARRAY-TYPE is used to represent any array type, including
340 ;;; things such as SIMPLE-STRING.
341 (defstruct (array-type (:include ctype
342 (class-info (type-class-or-lose 'array)))
343 (:constructor %make-array-type)
345 ;; the dimensions of the array, or * if unspecified. If a dimension
346 ;; is unspecified, it is *.
347 (dimensions '* :type (or list (member *)))
348 ;; Is this not a simple array type? (:MAYBE means that we don't know.)
349 (complexp :maybe :type (member t nil :maybe))
350 ;; the element type as originally specified
351 (element-type (missing-arg) :type ctype)
352 ;; the element type as it is specialized in this implementation
353 (specialized-element-type *wild-type* :type ctype))
354 (define-cached-synonym make-array-type)
356 ;;; A MEMBER-TYPE represent a use of the MEMBER type specifier. We
357 ;;; bother with this at this level because MEMBER types are fairly
358 ;;; important and union and intersection are well defined.
359 (defstruct (member-type (:include ctype
360 (class-info (type-class-or-lose 'member))
363 (:constructor %make-member-type (members))
364 #-sb-xc-host (:pure nil))
365 ;; the things in the set, with no duplications
366 (members nil :type list))
367 (defun make-member-type (&key members)
368 (declare (type list members))
369 ;; make sure that we've removed duplicates
370 (aver (= (length members) (length (remove-duplicates members))))
371 ;; if we have a pair of zeros (e.g. 0.0d0 and -0.0d0), then we can
372 ;; canonicalize to (DOUBLE-FLOAT 0.0d0 0.0d0), because numeric
373 ;; ranges are compared by arithmetic operators (while MEMBERship is
374 ;; compared by EQL). -- CSR, 2003-04-23
375 (let ((singlep (subsetp `(,(load-time-value (make-unportable-float :single-float-negative-zero)) 0.0f0) members))
376 (doublep (subsetp `(,(load-time-value (make-unportable-float :double-float-negative-zero)) 0.0d0) members))
378 (longp (subsetp `(,(load-time-value (make-unportable-float :long-float-negative-zero)) 0.0l0) members)))
379 (if (or singlep doublep #!+long-float longp)
382 (push (ctype-of 0.0f0) union-types)
383 (setf members (set-difference members `(,(load-time-value (make-unportable-float :single-float-negative-zero)) 0.0f0))))
385 (push (ctype-of 0.0d0) union-types)
386 (setf members (set-difference members `(,(load-time-value (make-unportable-float :double-float-negative-zero)) 0.0d0))))
389 (push (ctype-of 0.0l0) union-types)
390 (setf members (set-difference members `(,(load-time-value (make-unportable-float :long-float-negative-zero)) 0.0l0))))
391 (aver (not (null union-types)))
395 (cons (%make-member-type members)
397 (%make-member-type members))))
399 ;;; A COMPOUND-TYPE is a type defined out of a set of types, the
400 ;;; common parent of UNION-TYPE and INTERSECTION-TYPE.
401 (defstruct (compound-type (:include ctype
402 (might-contain-other-types-p t))
405 (types nil :type list :read-only t))
407 ;;; A UNION-TYPE represents a use of the OR type specifier which we
408 ;;; couldn't canonicalize to something simpler. Canonical form:
409 ;;; 1. All possible pairwise simplifications (using the UNION2 type
410 ;;; methods) have been performed. Thus e.g. there is never more
411 ;;; than one MEMBER-TYPE component. FIXME: As of sbcl-0.6.11.13,
412 ;;; this hadn't been fully implemented yet.
413 ;;; 2. There are never any UNION-TYPE components.
414 (defstruct (union-type (:include compound-type
415 (class-info (type-class-or-lose 'union)))
416 (:constructor %make-union-type (enumerable types))
418 (define-cached-synonym make-union-type)
420 ;;; An INTERSECTION-TYPE represents a use of the AND type specifier
421 ;;; which we couldn't canonicalize to something simpler. Canonical form:
422 ;;; 1. All possible pairwise simplifications (using the INTERSECTION2
423 ;;; type methods) have been performed. Thus e.g. there is never more
424 ;;; than one MEMBER-TYPE component.
425 ;;; 2. There are never any INTERSECTION-TYPE components: we've
426 ;;; flattened everything into a single INTERSECTION-TYPE object.
427 ;;; 3. There are never any UNION-TYPE components. Either we should
428 ;;; use the distributive rule to rearrange things so that
429 ;;; unions contain intersections and not vice versa, or we
430 ;;; should just punt to using a HAIRY-TYPE.
431 (defstruct (intersection-type (:include compound-type
432 (class-info (type-class-or-lose
434 (:constructor %make-intersection-type
438 ;;; Return TYPE converted to canonical form for a situation where the
439 ;;; "type" '* (which SBCL still represents as a type even though ANSI
440 ;;; CL defines it as a related but different kind of placeholder) is
441 ;;; equivalent to type T.
442 (defun type-*-to-t (type)
443 (if (type= type *wild-type*)
447 ;;; A CONS-TYPE is used to represent a CONS type.
448 (defstruct (cons-type (:include ctype (class-info (type-class-or-lose 'cons)))
450 %make-cons-type (car-type
453 ;; the CAR and CDR element types (to support ANSI (CONS FOO BAR) types)
455 ;; FIXME: Most or all other type structure slots could also be :READ-ONLY.
456 (car-type (missing-arg) :type ctype :read-only t)
457 (cdr-type (missing-arg) :type ctype :read-only t))
458 (defun make-cons-type (car-type cdr-type)
459 (aver (not (or (eq car-type *wild-type*)
460 (eq cdr-type *wild-type*))))
461 (if (or (eq car-type *empty-type*)
462 (eq cdr-type *empty-type*))
464 (%make-cons-type car-type cdr-type)))
468 ;;; Return the type structure corresponding to a type specifier. We
469 ;;; pick off structure types as a special case.
471 ;;; Note: VALUES-SPECIFIER-TYPE-CACHE-CLEAR must be called whenever a
472 ;;; type is defined (or redefined).
473 (defun-cached (values-specifier-type
474 :hash-function (lambda (x)
475 (logand (sxhash x) #x3FF))
477 :init-wrapper !cold-init-forms)
478 ((orig equal-but-no-car-recursion))
479 (let ((u (uncross orig)))
480 (or (info :type :builtin u)
481 (let ((spec (type-expand u)))
483 ((and (not (eq spec u))
484 (info :type :builtin spec)))
485 ((eq (info :type :kind spec) :instance)
486 (find-classoid spec))
487 ((typep spec 'classoid)
488 ;; There doesn't seem to be any way to translate
489 ;; (TYPEP SPEC 'BUILT-IN-CLASS) into something which can be
490 ;; executed on the host Common Lisp at cross-compilation time.
492 "stub: (TYPEP SPEC 'BUILT-IN-CLASS) on xc host")
493 (if (typep spec 'built-in-classoid)
494 (or (built-in-classoid-translation spec) spec)
497 (when (and (atom spec)
498 (member spec '(and or not member eql satisfies values)))
499 (error "The symbol ~S is not valid as a type specifier." spec))
500 (let* ((lspec (if (atom spec) (list spec) spec))
501 (fun (info :type :translator (car lspec))))
504 ((or (and (consp spec) (symbolp (car spec)))
506 (when (and *type-system-initialized*
507 (not (eq (info :type :kind spec)
508 :forthcoming-defclass-type)))
509 (signal 'parse-unknown-type :specifier spec))
510 ;; (The RETURN-FROM here inhibits caching.)
511 (return-from values-specifier-type
512 (make-unknown-type :specifier spec)))
514 (error "bad thing to be a type specifier: ~S"
517 ;;; This is like VALUES-SPECIFIER-TYPE, except that we guarantee to
518 ;;; never return a VALUES type.
519 (defun specifier-type (x)
520 (let ((res (values-specifier-type x)))
521 (when (or (values-type-p res)
522 ;; bootstrap magic :-(
523 (and (named-type-p res)
524 (eq (named-type-name res) '*)))
525 (error "VALUES type illegal in this context:~% ~S" x))
528 (defun single-value-specifier-type (x)
533 ;;; Similar to MACROEXPAND, but expands DEFTYPEs. We don't bother
534 ;;; returning a second value.
535 (defun type-expand (form)
536 (let ((def (cond ((symbolp form)
537 (info :type :expander form))
538 ((and (consp form) (symbolp (car form)))
539 (info :type :expander (car form)))
542 (type-expand (funcall def (if (consp form) form (list form))))
545 ;;; Note that the type NAME has been (re)defined, updating the
546 ;;; undefined warnings and VALUES-SPECIFIER-TYPE cache.
547 (defun %note-type-defined (name)
548 (declare (symbol name))
549 (note-name-defined name :type)
550 (when (boundp 'sb!kernel::*values-specifier-type-cache-vector*)
551 (values-specifier-type-cache-clear))
555 (!defun-from-collected-cold-init-forms !early-type-cold-init)