1 ;;;; This file contains the definition of non-CLASS types (e.g.
2 ;;;; subtypes of interesting BUILT-IN-CLASSes) and the interfaces to
3 ;;;; the type system. Common Lisp type specifiers are parsed into a
4 ;;;; somewhat canonical internal type representation that supports
5 ;;;; type union, intersection, etc. (Except that ALIEN types have
8 ;;;; This software is part of the SBCL system. See the README file for
11 ;;;; This software is derived from the CMU CL system, which was
12 ;;;; written at Carnegie Mellon University and released into the
13 ;;;; public domain. The software is in the public domain and is
14 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
15 ;;;; files for more information.
17 (in-package "SB!KERNEL")
19 (!begin-collecting-cold-init-forms)
21 ;;; ### Remaining incorrectnesses:
23 ;;; TYPE-UNION (and the OR type) doesn't properly canonicalize an
24 ;;; exhaustive partition or coalesce contiguous ranges of numeric
27 ;;; There are all sorts of nasty problems with open bounds on FLOAT
28 ;;; types (and probably FLOAT types in general.)
30 ;;; RATIO and BIGNUM are not recognized as numeric types.
32 ;;; FIXME: It seems to me that this should be set to NIL by default,
33 ;;; and perhaps not even optionally set to T.
34 (defvar *use-implementation-types* t
36 "*USE-IMPLEMENTATION-TYPES* is a semi-public flag which determines how
37 restrictive we are in determining type membership. If two types are the
38 same in the implementation, then we will consider them them the same when
39 this switch is on. When it is off, we try to be as restrictive as the
40 language allows, allowing us to detect more errors. Currently, this only
41 affects array types.")
43 (!cold-init-forms (setq *use-implementation-types* t))
45 ;;; These functions are used as method for types which need a complex
46 ;;; subtypep method to handle some superclasses, but cover a subtree
47 ;;; of the type graph (i.e. there is no simple way for any other type
48 ;;; class to be a subtype.) There are always still complex ways,
49 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
50 ;;; chance to run, instead of immediately returning NIL, T.
51 (defun delegate-complex-subtypep-arg2 (type1 type2)
53 (type-class-complex-subtypep-arg1
54 (type-class-info type1))))
56 (funcall subtypep-arg1 type1 type2)
58 (defun delegate-complex-intersection (type1 type2)
59 (let ((method (type-class-complex-intersection (type-class-info type1))))
60 (if (and method (not (eq method #'delegate-complex-intersection)))
61 (funcall method type2 type1)
62 (vanilla-intersection type1 type2))))
64 ;;; This is used by DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
65 ;;; method. INFO is a list of conses (SUPERCLASS-CLASS .
66 ;;; {GUARD-TYPE-SPECIFIER | NIL}). This will never be called with a
67 ;;; hairy type as TYPE2, since the hairy type TYPE2 method gets first
70 ;;; FIXME: Declare this as INLINE, since it's only used in one place.
71 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info)
73 (and (sb!xc:typep type2 'sb!xc:class)
75 (when (or (not (cdr x))
76 (csubtypep type1 (specifier-type (cdr x))))
78 (or (eq type2 (car x))
79 (let ((inherits (layout-inherits (class-layout (car x)))))
80 (dotimes (i (length inherits) nil)
81 (when (eq type2 (layout-class (svref inherits i)))
85 ;;; This function takes a list of specs, each of the form
86 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
87 ;;; Consider one spec (with no guard): any instance of the named
88 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
89 ;;; its superclasses. If there are multiple specs, then some will have
90 ;;; guards. We choose the first spec whose guard is a supertype of
91 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
94 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
96 ;;; WHEN controls when the forms are executed.
97 (defmacro define-superclasses (type-class-name specs when)
98 (let ((type-class (gensym "TYPE-CLASS-"))
99 (info (gensym "INFO")))
101 (let ((,type-class (type-class-or-lose ',type-class-name))
102 (,info (mapcar (lambda (spec)
104 (super &optional guard)
106 (cons (sb!xc:find-class super) guard)))
108 (setf (type-class-complex-subtypep-arg1 ,type-class)
109 (lambda (type1 type2)
110 (has-superclasses-complex-subtypep-arg1 type1 type2 ,info)))
111 (setf (type-class-complex-subtypep-arg2 ,type-class)
112 #'delegate-complex-subtypep-arg2)
113 (setf (type-class-complex-intersection ,type-class)
114 #'delegate-complex-intersection)))))
116 ;;;; FUNCTION and VALUES types
118 ;;;; Pretty much all of the general type operations are illegal on
119 ;;;; VALUES types, since we can't discriminate using them, do
120 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
121 ;;;; operations, but are generally considered to be equivalent to
122 ;;;; FUNCTION. These really aren't true types in any type theoretic
123 ;;;; sense, but we still parse them into CTYPE structures for two
126 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
127 ;;;; tell whether a type is a function or values type without
129 ;;;; -- Many of the places that can be annotated with real types can
130 ;;;; also be annotated with function or values types.
132 ;;; the description of a keyword argument
133 (defstruct (key-info #-sb-xc-host (:pure t))
135 (name (required-argument) :type keyword)
136 ;; the type of the argument value
137 (type (required-argument) :type ctype))
139 (define-type-method (values :simple-subtypep :complex-subtypep-arg1)
141 (declare (ignore type2))
142 (error "Subtypep is illegal on this type:~% ~S" (type-specifier type1)))
144 (define-type-method (values :complex-subtypep-arg2)
146 (declare (ignore type1))
147 (error "Subtypep is illegal on this type:~% ~S" (type-specifier type2)))
149 (define-type-method (values :unparse) (type)
150 (cons 'values (unparse-args-types type)))
152 ;;; Return true if LIST1 and LIST2 have the same elements in the same
153 ;;; positions according to TYPE=. We return NIL, NIL if there is an
154 ;;; uncertain comparison.
155 (defun type=-list (list1 list2)
156 (declare (list list1 list2))
157 (do ((types1 list1 (cdr types1))
158 (types2 list2 (cdr types2)))
159 ((or (null types1) (null types2))
160 (if (or types1 types2)
163 (multiple-value-bind (val win)
164 (type= (first types1) (first types2))
166 (return (values nil nil)))
168 (return (values nil t))))))
170 (define-type-method (values :simple-=) (type1 type2)
171 (let ((rest1 (args-type-rest type1))
172 (rest2 (args-type-rest type2)))
173 (cond ((or (args-type-keyp type1) (args-type-keyp type2)
174 (args-type-allowp type1) (args-type-allowp type2))
176 ((and rest1 rest2 (type/= rest1 rest2))
181 (multiple-value-bind (req-val req-win)
182 (type=-list (values-type-required type1)
183 (values-type-required type2))
184 (multiple-value-bind (opt-val opt-win)
185 (type=-list (values-type-optional type1)
186 (values-type-optional type2))
187 (values (and req-val opt-val) (and req-win opt-win))))))))
189 (define-type-class function)
191 ;;; a flag that we can bind to cause complex function types to be
192 ;;; unparsed as FUNCTION. This is useful when we want a type that we
193 ;;; can pass to TYPEP.
194 (defvar *unparse-function-type-simplify*)
195 (!cold-init-forms (setq *unparse-function-type-simplify* nil))
197 (define-type-method (function :unparse) (type)
198 (if *unparse-function-type-simplify*
201 (if (function-type-wild-args type)
203 (unparse-args-types type))
205 (function-type-returns type)))))
207 ;;; Since all function types are equivalent to FUNCTION, they are all
208 ;;; subtypes of each other.
209 (define-type-method (function :simple-subtypep) (type1 type2)
210 (declare (ignore type1 type2))
213 (define-superclasses function ((function)) !cold-init-forms)
215 ;;; The union or intersection of two FUNCTION types is FUNCTION.
216 (define-type-method (function :simple-union) (type1 type2)
217 (declare (ignore type1 type2))
218 (specifier-type 'function))
219 (define-type-method (function :simple-intersection) (type1 type2)
220 (declare (ignore type1 type2))
221 (values (specifier-type 'function) t))
223 ;;; ### Not very real, but good enough for redefining transforms
224 ;;; according to type:
225 (define-type-method (function :simple-=) (type1 type2)
226 (values (equalp type1 type2) t))
228 (define-type-class constant :inherits values)
230 (define-type-method (constant :unparse) (type)
231 `(constant-argument ,(type-specifier (constant-type-type type))))
233 (define-type-method (constant :simple-=) (type1 type2)
234 (type= (constant-type-type type1) (constant-type-type type2)))
236 (def-type-translator constant-argument (type)
237 (make-constant-type :type (specifier-type type)))
239 ;;; Given a LAMBDA-LIST-like values type specification and an ARGS-TYPE
240 ;;; structure, fill in the slots in the structure accordingly. This is
241 ;;; used for both FUNCTION and VALUES types.
242 (declaim (ftype (function (list args-type) (values)) parse-args-types))
243 (defun parse-args-types (lambda-list result)
244 (multiple-value-bind (required optional restp rest keyp keys allowp aux)
245 (parse-lambda-list lambda-list)
247 (error "&Aux in a FUNCTION or VALUES type: ~S." lambda-list))
248 (setf (args-type-required result) (mapcar #'specifier-type required))
249 (setf (args-type-optional result) (mapcar #'specifier-type optional))
250 (setf (args-type-rest result) (if restp (specifier-type rest) nil))
251 (setf (args-type-keyp result) keyp)
252 (collect ((key-info))
254 (unless (proper-list-of-length-p key 2)
255 (error "Keyword type description is not a two-list: ~S." key))
256 (let ((kwd (first key)))
257 (when (find kwd (key-info) :key #'key-info-name)
258 (error "Repeated keyword ~S in lambda list: ~S." kwd lambda-list))
259 (key-info (make-key-info :name kwd
260 :type (specifier-type (second key))))))
261 (setf (args-type-keywords result) (key-info)))
262 (setf (args-type-allowp result) allowp)
265 ;;; Return the lambda-list-like type specification corresponding
267 (declaim (ftype (function (args-type) list) unparse-args-types))
268 (defun unparse-args-types (type)
271 (dolist (arg (args-type-required type))
272 (result (type-specifier arg)))
274 (when (args-type-optional type)
276 (dolist (arg (args-type-optional type))
277 (result (type-specifier arg))))
279 (when (args-type-rest type)
281 (result (type-specifier (args-type-rest type))))
283 (when (args-type-keyp type)
285 (dolist (key (args-type-keywords type))
286 (result (list (key-info-name key)
287 (type-specifier (key-info-type key))))))
289 (when (args-type-allowp type)
290 (result '&allow-other-keys))
294 (def-type-translator function (&optional (args '*) (result '*))
295 (let ((res (make-function-type
296 :returns (values-specifier-type result))))
298 (setf (function-type-wild-args res) t)
299 (parse-args-types args res))
302 (def-type-translator values (&rest values)
303 (let ((res (make-values-type)))
304 (parse-args-types values res)
307 ;;;; VALUES types interfaces
309 ;;;; We provide a few special operations that can be meaningfully used
310 ;;;; on VALUES types (as well as on any other type).
312 ;;; Return the type of the first value indicated by Type. This is used
313 ;;; by people who don't want to have to deal with values types.
315 ;;; MNA: fix-instance-typep-call patch
316 #!-sb-fluid (declaim (freeze-type values-type))
317 ; (inline single-value-type))
318 (defun single-value-type (type)
319 (declare (type ctype type))
320 (cond ((values-type-p type)
321 (or (car (args-type-required type))
322 (if (args-type-optional type)
323 (type-union (car (args-type-optional type)) (specifier-type 'null)))
324 (args-type-rest type)
325 (specifier-type 'null)))
326 ((eq type *wild-type*)
331 ;;; Return the minmum number of arguments that a function can be
332 ;;; called with, and the maximum number or NIL. If not a function
333 ;;; type, return NIL, NIL.
334 (defun function-type-nargs (type)
335 (declare (type ctype type))
336 (if (function-type-p type)
337 (let ((fixed (length (args-type-required type))))
338 (if (or (args-type-rest type)
339 (args-type-keyp type)
340 (args-type-allowp type))
342 (values fixed (+ fixed (length (args-type-optional type))))))
345 ;;; Determine if Type corresponds to a definite number of values. The
346 ;;; first value is a list of the types for each value, and the second
347 ;;; value is the number of values. If the number of values is not
348 ;;; fixed, then return NIL and :Unknown.
349 (defun values-types (type)
350 (declare (type ctype type))
351 (cond ((eq type *wild-type*)
352 (values nil :unknown))
353 ((not (values-type-p type))
354 (values (list type) 1))
355 ((or (args-type-optional type)
356 (args-type-rest type)
357 (args-type-keyp type)
358 (args-type-allowp type))
359 (values nil :unknown))
361 (let ((req (args-type-required type)))
362 (values (mapcar #'single-value-type req) (length req))))))
364 ;;; Return two values:
365 ;;; MNA: fix-instance-typep-call patch
366 ;;; 1. A list of all the positional (fixed and optional) types.
367 ;;; 2. The &REST type (if any). If keywords allowed, *UNIVERSAL-TYPE*.
368 ;;; If no keywords or &REST, then the DEFAULT-TYPE.
369 (defun values-type-types (type &optional (default-type *empty-type*))
370 (declare (type values-type type))
371 (values (append (args-type-required type)
372 (args-type-optional type))
373 (cond ((args-type-keyp type) *universal-type*)
374 ((args-type-rest type))
376 ;; MNA: fix-instance-typep-call patch
379 ;;; Return a list of OPERATION applied to the types in TYPES1 and
380 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
381 ;;; than TYPES2. The second value is T if OPERATION always returned a
382 ;;; true second value.
383 (defun fixed-values-op (types1 types2 rest2 operation)
384 (declare (list types1 types2) (type ctype rest2) (type function operation))
386 (values (mapcar #'(lambda (t1 t2)
387 (multiple-value-bind (res win)
388 (funcall operation t1 t2)
394 (make-list (- (length types1) (length types2))
395 :initial-element rest2)))
398 ;;; If Type isn't a values type, then make it into one:
399 ;;; <type> ==> (values type &rest t)
400 (defun coerce-to-values (type)
401 (declare (type ctype type))
402 (if (values-type-p type)
404 (make-values-type :required (list type) :rest *universal-type*)))
406 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
407 ;;; type, including VALUES types. With VALUES types such as:
410 ;;; we compute the more useful result
411 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
412 ;;; rather than the precise result
413 ;;; (<operation> (values a0 a1) (values b0 b1))
414 ;;; This has the virtue of always keeping the VALUES type specifier
415 ;;; outermost, and retains all of the information that is really
416 ;;; useful for static type analysis. We want to know what is always
417 ;;; true of each value independently. It is worthless to know that IF
418 ;;; the first value is B0 then the second will be B1.
420 ;;; If the VALUES count signatures differ, then we produce a result with
421 ;;; the required VALUE count chosen by NREQ when applied to the number
422 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
423 ;;; &REST T (anyone who uses keyword values deserves to lose.)
425 ;;; The second value is true if the result is definitely empty or if
426 ;;; OPERATION returned true as its second value each time we called
427 ;;; it. Since we approximate the intersection of VALUES types, the
428 ;;; second value being true doesn't mean the result is exact.
429 ;;; MNA: fix-instance-typep-call patch
430 (defun args-type-op (type1 type2 operation nreq default-type)
431 ;;; MNA: fix-instance-typep-call patch
432 (declare (type ctype type1 type2 default-type)
433 (type function operation nreq))
434 (if (or (values-type-p type1) (values-type-p type2))
435 (let ((type1 (coerce-to-values type1))
436 (type2 (coerce-to-values type2)))
437 (multiple-value-bind (types1 rest1)
438 ;;; MNA: fix-instance-typep-call patch
439 (values-type-types type1 default-type)
440 (multiple-value-bind (types2 rest2)
441 ;;; MNA: fix-instance-typep-call patch
442 (values-type-types type2 default-type)
443 (multiple-value-bind (rest rest-exact)
444 (funcall operation rest1 rest2)
445 (multiple-value-bind (res res-exact)
446 (if (< (length types1) (length types2))
447 (fixed-values-op types2 types1 rest1 operation)
448 (fixed-values-op types1 types2 rest2 operation))
449 (let* ((req (funcall nreq
450 (length (args-type-required type1))
451 (length (args-type-required type2))))
452 (required (subseq res 0 req))
453 (opt (subseq res req))
454 (opt-last (position rest opt :test-not #'type=
456 (if (find *empty-type* required :test #'type=)
457 (values *empty-type* t)
458 (values (make-values-type
460 :optional (if opt-last
461 (subseq opt 0 (1+ opt-last))
463 ;; MNA fix-instance-typep-call patch
464 :rest (if (eq rest default-type) nil rest))
465 (and rest-exact res-exact)))))))))
466 (funcall operation type1 type2)))
468 ;;; Do a union or intersection operation on types that might be values
469 ;;; types. The result is optimized for utility rather than exactness,
470 ;;; but it is guaranteed that it will be no smaller (more restrictive)
471 ;;; than the precise result.
473 ;;; The return convention seems to be analogous to
474 ;;; TYPES-INTERSECT. -- WHN 19990910.
475 (defun-cached (values-type-union :hash-function type-cache-hash
478 :init-wrapper !cold-init-forms)
479 ((type1 eq) (type2 eq))
480 (declare (type ctype type1 type2))
481 (cond ((or (eq type1 *wild-type*) (eq type2 *wild-type*)) *wild-type*)
482 ((eq type1 *empty-type*) type2)
483 ((eq type2 *empty-type*) type1)
485 ;;; MNA: fix-instance-typep-call patch
486 (values (args-type-op type1 type2 #'type-union #'min *empty-type*)))))
488 (defun-cached (values-type-intersection :hash-function type-cache-hash
491 :default (values nil :empty)
492 :init-wrapper !cold-init-forms)
493 ((type1 eq) (type2 eq))
494 (declare (type ctype type1 type2))
495 (cond ((eq type1 *wild-type*) (values type2 t))
496 ((eq type2 *wild-type*) (values type1 t))
498 (args-type-op type1 type2 #'type-intersection #'max (specifier-type 'null)))))
500 ;;; This is like TYPES-INTERSECT, except that it sort of works on
501 ;;; VALUES types. Note that due to the semantics of
502 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
503 ;;; there isn't really any intersection (?).
505 ;;; The return convention seems to be analogous to
506 ;;; TYPES-INTERSECT. -- WHN 19990910.
507 (defun values-types-intersect (type1 type2)
508 (cond ((or (eq type1 *empty-type*) (eq type2 *empty-type*))
510 ((or (values-type-p type1) (values-type-p type2))
511 (multiple-value-bind (res win) (values-type-intersection type1 type2)
512 (values (not (eq res *empty-type*))
515 (types-intersect type1 type2))))
517 ;;; a SUBTYPEP-like operation that can be used on any types, including
519 (defun-cached (values-subtypep :hash-function type-cache-hash
522 :default (values nil :empty)
523 :init-wrapper !cold-init-forms)
524 ((type1 eq) (type2 eq))
525 (declare (type ctype type1 type2))
526 (cond ((eq type2 *wild-type*) (values t t))
527 ((eq type1 *wild-type*)
528 (values (eq type2 *universal-type*) t))
529 ((not (values-types-intersect type1 type2))
532 (if (or (values-type-p type1) (values-type-p type2))
533 (let ((type1 (coerce-to-values type1))
534 (type2 (coerce-to-values type2)))
535 (multiple-value-bind (types1 rest1) (values-type-types type1)
536 (multiple-value-bind (types2 rest2) (values-type-types type2)
537 (cond ((< (length (values-type-required type1))
538 (length (values-type-required type2)))
540 ((< (length types1) (length types2))
542 ((or (values-type-keyp type1)
543 (values-type-keyp type2))
546 (do ((t1 types1 (rest t1))
547 (t2 types2 (rest t2)))
549 (csubtypep rest1 rest2))
550 (multiple-value-bind (res win-p)
551 (csubtypep (first t1) (first t2))
553 (return (values nil nil)))
555 (return (values nil t))))))))))
556 (csubtypep type1 type2)))))
558 ;;;; type method interfaces
560 ;;; like SUBTYPEP, only works on CTYPE structures
561 (defun-cached (csubtypep :hash-function type-cache-hash
564 :default (values nil :empty)
565 :init-wrapper !cold-init-forms)
566 ((type1 eq) (type2 eq))
567 (declare (type ctype type1 type2))
568 (cond ((or (eq type1 type2)
569 (eq type1 *empty-type*)
570 (eq type2 *wild-type*))
572 ((or (eq type1 *wild-type*)
573 (eq type2 *empty-type*))
576 (invoke-type-method :simple-subtypep :complex-subtypep-arg2
578 :complex-arg1 :complex-subtypep-arg1))))
580 ;;; Just parse the type specifiers and call CSUBTYPE.
581 (defun sb!xc:subtypep (type1 type2)
583 "Return two values indicating the relationship between type1 and type2.
584 If values are T and T, type1 definitely is a subtype of type2.
585 If values are NIL and T, type1 definitely is not a subtype of type2.
586 If values are NIL and NIL, it couldn't be determined."
587 (csubtypep (specifier-type type1) (specifier-type type2)))
589 ;;; If two types are definitely equivalent, return true. The second
590 ;;; value indicates whether the first value is definitely correct.
591 ;;; This should only fail in the presence of HAIRY types.
592 (defun-cached (type= :hash-function type-cache-hash
595 :default (values nil :empty)
596 :init-wrapper !cold-init-forms)
597 ((type1 eq) (type2 eq))
598 (declare (type ctype type1 type2))
601 (invoke-type-method :simple-= :complex-= type1 type2)))
603 ;;; Not exactly the negation of TYPE=, since when the relationship is
604 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
605 ;;; the conservative assumption is =.
606 (defun type/= (type1 type2)
607 (declare (type ctype type1 type2))
608 (multiple-value-bind (res win) (type= type1 type2)
613 ;;; Find a type which includes both types. Any inexactness is
614 ;;; represented by the fuzzy element types; we return a single value
615 ;;; that is precise to the best of our knowledge. This result is
616 ;;; simplified into the canonical form, thus is not a UNION type
617 ;;; unless there is no other way to represent the result.
618 (defun-cached (type-union :hash-function type-cache-hash
620 :init-wrapper !cold-init-forms)
621 ((type1 eq) (type2 eq))
622 (declare (type ctype type1 type2))
625 (let ((res (invoke-type-method :simple-union :complex-union
628 (cond ((eq res :vanilla)
629 (or (vanilla-union type1 type2)
630 (make-union-type (list type1 type2))))
633 (make-union-type (list type1 type2)))))))
635 ;;; Return as restrictive a type as we can discover that is no more
636 ;;; restrictive than the intersection of Type1 and Type2. The second
637 ;;; value is true if the result is exact. At worst, we randomly return
638 ;;; one of the arguments as the first value (trying not to return a
640 (defun-cached (type-intersection :hash-function type-cache-hash
643 :default (values nil :empty)
644 :init-wrapper !cold-init-forms)
645 ((type1 eq) (type2 eq))
646 (declare (type ctype type1 type2))
649 (invoke-type-method :simple-intersection :complex-intersection
651 :default (values *empty-type* t))))
653 ;;; The first value is true unless the types don't intersect. The
654 ;;; second value is true if the first value is definitely correct. NIL
655 ;;; is considered to intersect with any type. If T is a subtype of
656 ;;; either type, then we also return T, T. This way we consider hairy
657 ;;; types to intersect with T.
658 (defun types-intersect (type1 type2)
659 (declare (type ctype type1 type2))
660 (if (or (eq type1 *empty-type*) (eq type2 *empty-type*))
662 (multiple-value-bind (val winp) (type-intersection type1 type2)
664 (if (or (csubtypep *universal-type* type1)
665 (csubtypep *universal-type* type2))
668 ((eq val *empty-type*) (values nil t))
671 ;;; Return a Common Lisp type specifier corresponding to the TYPE
673 (defun type-specifier (type)
674 (declare (type ctype type))
675 (funcall (type-class-unparse (type-class-info type)) type))
677 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
678 ;;; early-type.lisp by WHN ca. 19990201.)
680 ;;; Take a list of type specifiers, compute the translation and define
681 ;;; it as a builtin type.
682 (declaim (ftype (function (list) (values)) precompute-types))
683 (defun precompute-types (specs)
685 (let ((res (specifier-type spec)))
686 (unless (unknown-type-p res)
687 (setf (info :type :builtin spec) res)
688 (setf (info :type :kind spec) :primitive))))
693 (define-type-class named)
696 (defvar *empty-type*)
697 (defvar *universal-type*)
700 (macrolet ((frob (name var)
702 (setq ,var (make-named-type :name ',name))
703 (setf (info :type :kind ',name) :primitive)
704 (setf (info :type :builtin ',name) ,var))))
705 ;; KLUDGE: In ANSI, * isn't really the name of a type, it's just a
706 ;; special symbol which can be stuck in some places where an
707 ;; ordinary type can go, e.g. (ARRAY * 1) instead of (ARRAY T 1).
708 ;; At some point, in order to become more standard, we should
709 ;; convert all the classic CMU CL legacy *s and *WILD-TYPE*s into
710 ;; Ts and *UNIVERSAL-TYPE*s.
712 (frob nil *empty-type*)
713 (frob t *universal-type*)))
715 (define-type-method (named :simple-=) (type1 type2)
716 (values (eq type1 type2) t))
718 (define-type-method (named :simple-subtypep) (type1 type2)
719 (values (or (eq type1 *empty-type*) (eq type2 *wild-type*)) t))
721 (define-type-method (named :complex-subtypep-arg1) (type1 type2)
722 (assert (not (hairy-type-p type2)))
723 (values (eq type1 *empty-type*) t))
725 (define-type-method (named :complex-subtypep-arg2) (type1 type2)
726 (if (hairy-type-p type1)
728 (values (not (eq type2 *empty-type*)) t)))
730 (define-type-method (named :complex-intersection) (type1 type2)
731 (vanilla-intersection type1 type2))
733 (define-type-method (named :unparse) (x)
736 ;;;; hairy and unknown types
738 (define-type-method (hairy :unparse) (x) (hairy-type-specifier x))
740 (define-type-method (hairy :simple-subtypep) (type1 type2)
741 (let ((hairy-spec1 (hairy-type-specifier type1))
742 (hairy-spec2 (hairy-type-specifier type2)))
743 (cond ((and (consp hairy-spec1) (eq (car hairy-spec1) 'not)
744 (consp hairy-spec2) (eq (car hairy-spec2) 'not))
745 (csubtypep (specifier-type (cadr hairy-spec2))
746 (specifier-type (cadr hairy-spec1))))
747 ((equal hairy-spec1 hairy-spec2)
752 (define-type-method (hairy :complex-subtypep-arg2) (type1 type2)
753 (let ((hairy-spec (hairy-type-specifier type2)))
754 (cond ((and (consp hairy-spec) (eq (car hairy-spec) 'not))
755 (multiple-value-bind (val win)
756 (type-intersection type1 (specifier-type (cadr hairy-spec)))
758 (values (eq val *empty-type*) t)
763 (define-type-method (hairy :complex-subtypep-arg1 :complex-=) (type1 type2)
764 (declare (ignore type1 type2))
767 (define-type-method (hairy :simple-intersection :complex-intersection)
769 (declare (ignore type2))
772 (define-type-method (hairy :complex-union) (type1 type2)
773 (make-union-type (list type1 type2)))
775 (define-type-method (hairy :simple-=) (type1 type2)
776 (if (equal (hairy-type-specifier type1)
777 (hairy-type-specifier type2))
781 (def-type-translator not (&whole whole type)
782 (declare (ignore type))
783 (make-hairy-type :specifier whole))
785 (def-type-translator satisfies (&whole whole fun)
786 (declare (ignore fun))
787 (make-hairy-type :specifier whole))
791 ;;; A list of all the float formats, in order of decreasing precision.
792 (eval-when (:compile-toplevel :load-toplevel :execute)
793 (defparameter *float-formats*
794 '(long-float double-float single-float short-float)))
796 ;;; The type of a float format.
797 (deftype float-format () `(member ,@*float-formats*))
799 #!+negative-zero-is-not-zero
800 (defun make-numeric-type (&key class format (complexp :real) low high
802 (flet ((canonicalise-low-bound (x)
803 ;; Canonicalise a low bound of (-0.0) to 0.0.
804 (if (and (consp x) (floatp (car x)) (zerop (car x))
805 (minusp (float-sign (car x))))
808 (canonicalise-high-bound (x)
809 ;; Canonicalise a high bound of (+0.0) to -0.0.
810 (if (and (consp x) (floatp (car x)) (zerop (car x))
811 (plusp (float-sign (car x))))
814 (%make-numeric-type :class class
817 :low (canonicalise-low-bound low)
818 :high (canonicalise-high-bound high)
819 :enumerable enumerable)))
821 (define-type-class number)
823 (define-type-method (number :simple-=) (type1 type2)
825 (and (eq (numeric-type-class type1) (numeric-type-class type2))
826 (eq (numeric-type-format type1) (numeric-type-format type2))
827 (eq (numeric-type-complexp type1) (numeric-type-complexp type2))
828 (equal (numeric-type-low type1) (numeric-type-low type2))
829 (equal (numeric-type-high type1) (numeric-type-high type2)))
832 (define-type-method (number :unparse) (type)
833 (let* ((complexp (numeric-type-complexp type))
834 (low (numeric-type-low type))
835 (high (numeric-type-high type))
836 (base (case (numeric-type-class type)
839 (float (or (numeric-type-format type) 'float))
842 (cond ((and (eq base 'integer) high low)
843 (let ((high-count (logcount high))
844 (high-length (integer-length high)))
846 (cond ((= high 0) '(integer 0 0))
848 ((and (= high-count high-length)
850 `(unsigned-byte ,high-length))
853 ((and (= low sb!vm:*target-most-negative-fixnum*)
854 (= high sb!vm:*target-most-positive-fixnum*))
856 ((and (= low (lognot high))
857 (= high-count high-length)
859 `(signed-byte ,(1+ high-length)))
861 `(integer ,low ,high)))))
862 (high `(,base ,(or low '*) ,high))
864 (if (and (eq base 'integer) (= low 0))
872 (if (eq base+bounds 'real)
874 `(complex ,base+bounds)))
876 (assert (eq base+bounds 'real))
879 ;;; Return true if X is "less than or equal" to Y, taking open bounds
880 ;;; into consideration. CLOSED is the predicate used to test the bound
881 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
882 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
883 ;;; the sense that if it is infinite (NIL), then the test succeeds,
884 ;;; whereas if X is infinite, then the test fails (unless Y is also
887 ;;; This is for comparing bounds of the same kind, e.g. upper and
888 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
889 #!-negative-zero-is-not-zero
890 (defmacro numeric-bound-test (x y closed open)
895 (,closed (car ,x) (car ,y))
896 (,closed (car ,x) ,y)))
902 #!+negative-zero-is-not-zero
903 (defmacro numeric-bound-test-zero (op x y)
904 `(if (and (zerop ,x) (zerop ,y) (floatp ,x) (floatp ,y))
905 (,op (float-sign ,x) (float-sign ,y))
908 #!+negative-zero-is-not-zero
909 (defmacro numeric-bound-test (x y closed open)
914 (numeric-bound-test-zero ,closed (car ,x) (car ,y))
915 (numeric-bound-test-zero ,closed (car ,x) ,y)))
918 (numeric-bound-test-zero ,open ,x (car ,y))
919 (numeric-bound-test-zero ,closed ,x ,y)))))
921 ;;; This is used to compare upper and lower bounds. This is different
922 ;;; from the same-bound case:
923 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
924 ;;; return true if *either* arg is NIL.
925 ;;; -- an open inner bound is "greater" and also squeezes the interval,
926 ;;; causing us to use the OPEN test for those cases as well.
927 #!-negative-zero-is-not-zero
928 (defmacro numeric-bound-test* (x y closed open)
933 (,open (car ,x) (car ,y))
934 (,open (car ,x) ,y)))
940 #!+negative-zero-is-not-zero
941 (defmacro numeric-bound-test* (x y closed open)
946 (numeric-bound-test-zero ,open (car ,x) (car ,y))
947 (numeric-bound-test-zero ,open (car ,x) ,y)))
950 (numeric-bound-test-zero ,open ,x (car ,y))
951 (numeric-bound-test-zero ,closed ,x ,y)))))
953 ;;; Return whichever of the numeric bounds X and Y is "maximal"
954 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
955 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
956 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
957 ;;; otherwise we return the other arg.
958 (defmacro numeric-bound-max (x y closed open max-p)
961 `(cond ((not ,n-x) ,(if max-p nil n-y))
962 ((not ,n-y) ,(if max-p nil n-x))
965 (if (,closed (car ,n-x) (car ,n-y)) ,n-x ,n-y)
966 (if (,open (car ,n-x) ,n-y) ,n-x ,n-y)))
969 (if (,open (car ,n-y) ,n-x) ,n-y ,n-x)
970 (if (,closed ,n-y ,n-x) ,n-y ,n-x))))))
972 (define-type-method (number :simple-subtypep) (type1 type2)
973 (let ((class1 (numeric-type-class type1))
974 (class2 (numeric-type-class type2))
975 (complexp2 (numeric-type-complexp type2))
976 (format2 (numeric-type-format type2))
977 (low1 (numeric-type-low type1))
978 (high1 (numeric-type-high type1))
979 (low2 (numeric-type-low type2))
980 (high2 (numeric-type-high type2)))
981 ;; If one is complex and the other isn't, they are disjoint.
982 (cond ((not (or (eq (numeric-type-complexp type1) complexp2)
985 ;; If the classes are specified and different, the types are
986 ;; disjoint unless type2 is rational and type1 is integer.
987 ((not (or (eq class1 class2)
989 (and (eq class1 'integer)
990 (eq class2 'rational))))
992 ;; If the float formats are specified and different, the types
994 ((not (or (eq (numeric-type-format type1) format2)
998 ((and (numeric-bound-test low1 low2 >= >)
999 (numeric-bound-test high1 high2 <= <))
1004 (define-superclasses number ((generic-number)) !cold-init-forms)
1006 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1007 ;;; then return true, otherwise NIL.
1008 (defun numeric-types-adjacent (low high)
1009 (let ((low-bound (numeric-type-high low))
1010 (high-bound (numeric-type-low high)))
1011 (cond ((not (and low-bound high-bound)) nil)
1012 ((and (consp low-bound) (consp high-bound)) nil)
1014 #!-negative-zero-is-not-zero
1015 (let ((low-value (car low-bound)))
1016 (or (eql low-value high-bound)
1017 (and (eql low-value -0f0) (eql high-bound 0f0))
1018 (and (eql low-value 0f0) (eql high-bound -0f0))
1019 (and (eql low-value -0d0) (eql high-bound 0d0))
1020 (and (eql low-value 0d0) (eql high-bound -0d0))))
1021 #!+negative-zero-is-not-zero
1022 (eql (car low-bound) high-bound))
1024 #!-negative-zero-is-not-zero
1025 (let ((high-value (car high-bound)))
1026 (or (eql high-value low-bound)
1027 (and (eql high-value -0f0) (eql low-bound 0f0))
1028 (and (eql high-value 0f0) (eql low-bound -0f0))
1029 (and (eql high-value -0d0) (eql low-bound 0d0))
1030 (and (eql high-value 0d0) (eql low-bound -0d0))))
1031 #!+negative-zero-is-not-zero
1032 (eql (car high-bound) low-bound))
1033 #!+negative-zero-is-not-zero
1034 ((or (and (eql low-bound -0f0) (eql high-bound 0f0))
1035 (and (eql low-bound -0d0) (eql high-bound 0d0))))
1036 ((and (eq (numeric-type-class low) 'integer)
1037 (eq (numeric-type-class high) 'integer))
1038 (eql (1+ low-bound) high-bound))
1042 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1044 ;;; ### Note: we give up early, so keep from dropping lots of information on
1045 ;;; the floor by returning overly general types.
1046 (define-type-method (number :simple-union) (type1 type2)
1047 (declare (type numeric-type type1 type2))
1048 (cond ((csubtypep type1 type2) type2)
1049 ((csubtypep type2 type1) type1)
1051 (let ((class1 (numeric-type-class type1))
1052 (format1 (numeric-type-format type1))
1053 (complexp1 (numeric-type-complexp type1))
1054 (class2 (numeric-type-class type2))
1055 (format2 (numeric-type-format type2))
1056 (complexp2 (numeric-type-complexp type2)))
1057 (when (and (eq class1 class2)
1058 (eq format1 format2)
1059 (eq complexp1 complexp2)
1060 (or (numeric-types-intersect type1 type2)
1061 (numeric-types-adjacent type1 type2)
1062 (numeric-types-adjacent type2 type1)))
1067 :low (numeric-bound-max (numeric-type-low type1)
1068 (numeric-type-low type2)
1070 :high (numeric-bound-max (numeric-type-high type1)
1071 (numeric-type-high type2)
1075 (setf (info :type :kind 'number) :primitive)
1076 (setf (info :type :builtin 'number)
1077 (make-numeric-type :complexp nil)))
1079 (def-type-translator complex (&optional (spec '*))
1081 (make-numeric-type :complexp :complex)
1082 (let ((type (specifier-type spec)))
1083 (unless (numeric-type-p type)
1084 (error "Component type for Complex is not numeric: ~S." spec))
1085 (when (eq (numeric-type-complexp type) :complex)
1086 (error "Component type for Complex is complex: ~S." spec))
1087 (let ((res (copy-numeric-type type)))
1088 (setf (numeric-type-complexp res) :complex)
1091 ;;; If X is *, return NIL, otherwise return the bound, which must be a
1092 ;;; member of TYPE or a one-element list of a member of TYPE.
1093 #!-sb-fluid (declaim (inline canonicalized-bound))
1094 (defun canonicalized-bound (bound type)
1095 (cond ((eq bound '*) nil)
1096 ((or (sb!xc:typep bound type)
1098 (sb!xc:typep (car bound) type)
1099 (null (cdr bound))))
1102 (error "Bound is not ~S, a ~S or a list of a ~S: ~S"
1108 (def-type-translator integer (&optional (low '*) (high '*))
1109 (let* ((l (canonicalized-bound low 'integer))
1110 (lb (if (consp l) (1+ (car l)) l))
1111 (h (canonicalized-bound high 'integer))
1112 (hb (if (consp h) (1- (car h)) h)))
1113 (when (and hb lb (< hb lb))
1114 (error "Lower bound ~S is greater than upper bound ~S." l h))
1115 (make-numeric-type :class 'integer
1117 :enumerable (not (null (and l h)))
1121 (defmacro def-bounded-type (type class format)
1122 `(def-type-translator ,type (&optional (low '*) (high '*))
1123 (let ((lb (canonicalized-bound low ',type))
1124 (hb (canonicalized-bound high ',type)))
1125 (unless (numeric-bound-test* lb hb <= <)
1126 (error "Lower bound ~S is not less than upper bound ~S." low high))
1127 (make-numeric-type :class ',class :format ',format :low lb :high hb))))
1129 (def-bounded-type rational rational nil)
1130 (def-bounded-type float float nil)
1131 (def-bounded-type real nil nil)
1133 (defmacro define-float-format (f)
1134 `(def-bounded-type ,f float ,f))
1136 (define-float-format short-float)
1137 (define-float-format single-float)
1138 (define-float-format double-float)
1139 (define-float-format long-float)
1141 (defun numeric-types-intersect (type1 type2)
1142 (declare (type numeric-type type1 type2))
1143 (let* ((class1 (numeric-type-class type1))
1144 (class2 (numeric-type-class type2))
1145 (complexp1 (numeric-type-complexp type1))
1146 (complexp2 (numeric-type-complexp type2))
1147 (format1 (numeric-type-format type1))
1148 (format2 (numeric-type-format type2))
1149 (low1 (numeric-type-low type1))
1150 (high1 (numeric-type-high type1))
1151 (low2 (numeric-type-low type2))
1152 (high2 (numeric-type-high type2)))
1153 ;; If one is complex and the other isn't, then they are disjoint.
1154 (cond ((not (or (eq complexp1 complexp2)
1155 (null complexp1) (null complexp2)))
1157 ;; If either type is a float, then the other must either be
1158 ;; specified to be a float or unspecified. Otherwise, they
1160 ((and (eq class1 'float)
1161 (not (member class2 '(float nil)))) nil)
1162 ((and (eq class2 'float)
1163 (not (member class1 '(float nil)))) nil)
1164 ;; If the float formats are specified and different, the
1165 ;; types are disjoint.
1166 ((not (or (eq format1 format2) (null format1) (null format2)))
1169 ;; Check the bounds. This is a bit odd because we must
1170 ;; always have the outer bound of the interval as the
1172 (if (numeric-bound-test high1 high2 <= <)
1173 (or (and (numeric-bound-test low1 low2 >= >)
1174 (numeric-bound-test* low1 high2 <= <))
1175 (and (numeric-bound-test low2 low1 >= >)
1176 (numeric-bound-test* low2 high1 <= <)))
1177 (or (and (numeric-bound-test* low2 high1 <= <)
1178 (numeric-bound-test low2 low1 >= >))
1179 (and (numeric-bound-test high2 high1 <= <)
1180 (numeric-bound-test* high2 low1 >= >))))))))
1182 ;;; Take the numeric bound X and convert it into something that can be
1183 ;;; used as a bound in a numeric type with the specified CLASS and
1184 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
1185 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
1187 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
1188 ;;; the appropriate type number. X may only be a float when CLASS is
1191 ;;; ### Note: it is possible for the coercion to a float to overflow
1192 ;;; or underflow. This happens when the bound doesn't fit in the
1193 ;;; specified format. In this case, we should really return the
1194 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
1195 ;;; of desired format. But these conditions aren't currently signalled
1196 ;;; in any useful way.
1198 ;;; Also, when converting an open rational bound into a float we
1199 ;;; should probably convert it to a closed bound of the closest float
1200 ;;; in the specified format. KLUDGE: In general, open float bounds are
1201 ;;; screwed up. -- (comment from original CMU CL)
1202 (defun round-numeric-bound (x class format up-p)
1204 (let ((cx (if (consp x) (car x) x)))
1208 (if (and (consp x) (integerp cx))
1209 (if up-p (1+ cx) (1- cx))
1210 (if up-p (ceiling cx) (floor cx))))
1212 (let ((res (if format (coerce cx format) (float cx))))
1213 (if (consp x) (list res) res)))))
1216 ;;; Handle the case of TYPE-INTERSECTION on two numeric types. We use
1217 ;;; TYPES-INTERSECT to throw out the case of types with no
1218 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
1219 ;;; TYPE2's attribute, which must be at least as restrictive. If the
1220 ;;; types intersect, then the only attributes that can be specified
1221 ;;; and different are the class and the bounds.
1223 ;;; When the class differs, we use the more restrictive class. The
1224 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
1227 ;;; We make the result lower (upper) bound the maximum (minimum) of
1228 ;;; the argument lower (upper) bounds. We convert the bounds into the
1229 ;;; appropriate numeric type before maximizing. This avoids possible
1230 ;;; confusion due to mixed-type comparisons (but I think the result is
1232 (define-type-method (number :simple-intersection) (type1 type2)
1233 (declare (type numeric-type type1 type2))
1234 (if (numeric-types-intersect type1 type2)
1235 (let* ((class1 (numeric-type-class type1))
1236 (class2 (numeric-type-class type2))
1237 (class (ecase class1
1239 ((integer float) class1)
1240 (rational (if (eq class2 'integer)
1243 (format (or (numeric-type-format type1)
1244 (numeric-type-format type2))))
1249 :complexp (or (numeric-type-complexp type1)
1250 (numeric-type-complexp type2))
1251 :low (numeric-bound-max
1252 (round-numeric-bound (numeric-type-low type1)
1254 (round-numeric-bound (numeric-type-low type2)
1257 :high (numeric-bound-max
1258 (round-numeric-bound (numeric-type-high type1)
1260 (round-numeric-bound (numeric-type-high type2)
1264 (values *empty-type* t)))
1266 ;;; Given two float formats, return the one with more precision. If
1267 ;;; either one is null, return NIL.
1268 (defun float-format-max (f1 f2)
1270 (dolist (f *float-formats* (error "bad float format: ~S" f1))
1271 (when (or (eq f f1) (eq f f2))
1274 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
1275 ;;; the rules of numeric contagion. This is always NUMBER, some float
1276 ;;; format (possibly complex) or RATIONAL. Due to rational
1277 ;;; canonicalization, there isn't much we can do here with integers or
1278 ;;; rational complex numbers.
1280 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
1281 ;;; is useful mainly for allowing types that are technically numbers,
1282 ;;; but not a NUMERIC-TYPE.
1283 (defun numeric-contagion (type1 type2)
1284 (if (and (numeric-type-p type1) (numeric-type-p type2))
1285 (let ((class1 (numeric-type-class type1))
1286 (class2 (numeric-type-class type2))
1287 (format1 (numeric-type-format type1))
1288 (format2 (numeric-type-format type2))
1289 (complexp1 (numeric-type-complexp type1))
1290 (complexp2 (numeric-type-complexp type2)))
1291 (cond ((or (null complexp1)
1293 (specifier-type 'number))
1297 :format (ecase class2
1298 (float (float-format-max format1 format2))
1299 ((integer rational) format1)
1301 ;; A double-float with any real number is a
1304 (if (eq format1 'double-float)
1307 ;; A long-float with any real number is a
1310 (if (eq format1 'long-float)
1313 :complexp (if (or (eq complexp1 :complex)
1314 (eq complexp2 :complex))
1317 ((eq class2 'float) (numeric-contagion type2 type1))
1318 ((and (eq complexp1 :real) (eq complexp2 :real))
1320 :class (and class1 class2 'rational)
1323 (specifier-type 'number))))
1324 (specifier-type 'number)))
1328 (define-type-class array)
1330 ;;; What this does depends on the setting of the
1331 ;;; *USE-IMPLEMENTATION-TYPES* switch. If true, return the specialized
1332 ;;; element type, otherwise return the original element type.
1333 (defun specialized-element-type-maybe (type)
1334 (declare (type array-type type))
1335 (if *use-implementation-types*
1336 (array-type-specialized-element-type type)
1337 (array-type-element-type type)))
1339 (define-type-method (array :simple-=) (type1 type2)
1340 (values (and (equal (array-type-dimensions type1)
1341 (array-type-dimensions type2))
1342 (eq (array-type-complexp type1)
1343 (array-type-complexp type2))
1344 (type= (specialized-element-type-maybe type1)
1345 (specialized-element-type-maybe type2)))
1348 (define-type-method (array :unparse) (type)
1349 (let ((dims (array-type-dimensions type))
1350 (eltype (type-specifier (array-type-element-type type)))
1351 (complexp (array-type-complexp type)))
1354 (if complexp 'array 'simple-array)
1355 (if complexp `(array ,eltype) `(simple-array ,eltype))))
1356 ((= (length dims) 1)
1358 (if (eq (car dims) '*)
1361 (base-char 'base-string)
1364 (t `(vector ,eltype)))
1366 (bit `(bit-vector ,(car dims)))
1367 (base-char `(base-string ,(car dims)))
1368 (character `(string ,(car dims)))
1369 (t `(vector ,eltype ,(car dims)))))
1370 (if (eq (car dims) '*)
1372 (bit 'simple-bit-vector)
1373 (base-char 'simple-base-string)
1374 (character 'simple-string)
1375 ((t) 'simple-vector)
1376 (t `(simple-array ,eltype (*))))
1378 (bit `(simple-bit-vector ,(car dims)))
1379 (base-char `(simple-base-string ,(car dims)))
1380 (character `(simple-string ,(car dims)))
1381 ((t) `(simple-vector ,(car dims)))
1382 (t `(simple-array ,eltype ,dims))))))
1385 `(array ,eltype ,dims)
1386 `(simple-array ,eltype ,dims))))))
1388 (define-type-method (array :simple-subtypep) (type1 type2)
1389 (let ((dims1 (array-type-dimensions type1))
1390 (dims2 (array-type-dimensions type2))
1391 (complexp2 (array-type-complexp type2)))
1392 ;; See whether dimensions are compatible.
1393 (cond ((not (or (eq dims2 '*)
1394 (and (not (eq dims1 '*))
1395 ;; (sbcl-0.6.4 has trouble figuring out that
1396 ;; DIMS1 and DIMS2 must be lists at this
1397 ;; point, and knowing that is important to
1398 ;; compiling EVERY efficiently.)
1399 (= (length (the list dims1))
1400 (length (the list dims2)))
1401 (every (lambda (x y)
1402 (or (eq y '*) (eql x y)))
1404 (the list dims2)))))
1406 ;; See whether complexpness is compatible.
1407 ((not (or (eq complexp2 :maybe)
1408 (eq (array-type-complexp type1) complexp2)))
1410 ;; If the TYPE2 eltype is wild, we win. Otherwise, the types
1411 ;; must be identical.
1412 ((or (eq (array-type-element-type type2) *wild-type*)
1413 (type= (specialized-element-type-maybe type1)
1414 (specialized-element-type-maybe type2)))
1419 (define-superclasses array
1425 (defun array-types-intersect (type1 type2)
1426 (declare (type array-type type1 type2))
1427 (let ((dims1 (array-type-dimensions type1))
1428 (dims2 (array-type-dimensions type2))
1429 (complexp1 (array-type-complexp type1))
1430 (complexp2 (array-type-complexp type2)))
1431 ;; See whether dimensions are compatible.
1432 (cond ((not (or (eq dims1 '*) (eq dims2 '*)
1433 (and (= (length dims1) (length dims2))
1434 (every #'(lambda (x y)
1435 (or (eq x '*) (eq y '*) (= x y)))
1438 ;; See whether complexpness is compatible.
1439 ((not (or (eq complexp1 :maybe)
1440 (eq complexp2 :maybe)
1441 (eq complexp1 complexp2)))
1443 ;; If either element type is wild, then they intersect.
1444 ;; Otherwise, the types must be identical.
1445 ((or (eq (array-type-element-type type1) *wild-type*)
1446 (eq (array-type-element-type type2) *wild-type*)
1447 (type= (specialized-element-type-maybe type1)
1448 (specialized-element-type-maybe type2)))
1454 (define-type-method (array :simple-intersection) (type1 type2)
1455 (declare (type array-type type1 type2))
1456 (if (array-types-intersect type1 type2)
1457 (let ((dims1 (array-type-dimensions type1))
1458 (dims2 (array-type-dimensions type2))
1459 (complexp1 (array-type-complexp type1))
1460 (complexp2 (array-type-complexp type2))
1461 (eltype1 (array-type-element-type type1))
1462 (eltype2 (array-type-element-type type2)))
1464 (specialize-array-type
1466 :dimensions (cond ((eq dims1 '*) dims2)
1467 ((eq dims2 '*) dims1)
1469 (mapcar (lambda (x y) (if (eq x '*) y x))
1471 :complexp (if (eq complexp1 :maybe) complexp2 complexp1)
1472 :element-type (if (eq eltype1 *wild-type*) eltype2 eltype1)))
1474 (values *empty-type* t)))
1476 ;;; Check a supplied dimension list to determine whether it is legal,
1477 ;;; and return it in canonical form (as either '* or a list).
1478 (defun canonical-array-dimensions (dims)
1483 (error "Arrays can't have a negative number of dimensions: ~S" dims))
1484 (when (>= dims sb!xc:array-rank-limit)
1485 (error "array type with too many dimensions: ~S" dims))
1486 (make-list dims :initial-element '*))
1488 (when (>= (length dims) sb!xc:array-rank-limit)
1489 (error "array type with too many dimensions: ~S" dims))
1492 (unless (and (integerp dim)
1494 (< dim sb!xc:array-dimension-limit))
1495 (error "bad dimension in array type: ~S" dim))))
1498 (error "Array dimensions is not a list, integer or *:~% ~S" dims))))
1502 (define-type-class member)
1504 (define-type-method (member :unparse) (type)
1505 (let ((members (member-type-members type)))
1506 (if (equal members '(nil))
1508 `(member ,@members))))
1510 (define-type-method (member :simple-subtypep) (type1 type2)
1511 (values (subsetp (member-type-members type1) (member-type-members type2))
1514 (define-type-method (member :complex-subtypep-arg1) (type1 type2)
1516 (values (every-type-op ctypep type2 (member-type-members type1)
1520 ;;; We punt if the odd type is enumerable and intersects with the
1521 ;;; MEMBER type. If not enumerable, then it is definitely not a
1522 ;;; subtype of the MEMBER type.
1523 (define-type-method (member :complex-subtypep-arg2) (type1 type2)
1524 (cond ((not (type-enumerable type1)) (values nil t))
1525 ((types-intersect type1 type2) (values nil nil))
1529 (define-type-method (member :simple-intersection) (type1 type2)
1530 (let ((mem1 (member-type-members type1))
1531 (mem2 (member-type-members type2)))
1532 (values (cond ((subsetp mem1 mem2) type1)
1533 ((subsetp mem2 mem1) type2)
1535 (let ((res (intersection mem1 mem2)))
1537 (make-member-type :members res)
1541 (define-type-method (member :complex-intersection) (type1 type2)
1543 (collect ((members))
1544 (let ((mem2 (member-type-members type2)))
1545 (dolist (member mem2)
1546 (multiple-value-bind (val win) (ctypep member type1)
1548 (return-from PUNT (values type2 nil)))
1549 (when val (members member))))
1551 (values (cond ((subsetp mem2 (members)) type2)
1552 ((null (members)) *empty-type*)
1554 (make-member-type :members (members))))
1557 ;;; We don't need a :COMPLEX-UNION, since the only interesting case is a union
1558 ;;; type, and the member/union interaction is handled by the union type
1560 (define-type-method (member :simple-union) (type1 type2)
1561 (let ((mem1 (member-type-members type1))
1562 (mem2 (member-type-members type2)))
1563 (cond ((subsetp mem1 mem2) type2)
1564 ((subsetp mem2 mem1) type1)
1566 (make-member-type :members (union mem1 mem2))))))
1568 (define-type-method (member :simple-=) (type1 type2)
1569 (let ((mem1 (member-type-members type1))
1570 (mem2 (member-type-members type2)))
1571 (values (and (subsetp mem1 mem2) (subsetp mem2 mem1))
1574 (define-type-method (member :complex-=) (type1 type2)
1575 (if (type-enumerable type1)
1576 (multiple-value-bind (val win) (csubtypep type2 type1)
1577 (if (or val (not win))
1582 (def-type-translator member (&rest members)
1584 (make-member-type :members (remove-duplicates members))
1589 ;;; Make a union type from the specifier types, setting ENUMERABLE in
1590 ;;; the result if all are enumerable.
1591 (defun make-union-type (types)
1592 (declare (list types))
1593 (%make-union-type (every #'type-enumerable types) types))
1595 (define-type-class union)
1597 ;;; If LIST, then return that, otherwise the OR of the component types.
1598 (define-type-method (union :unparse) (type)
1599 (declare (type ctype type))
1600 (if (type= type (specifier-type 'list))
1602 `(or ,@(mapcar #'type-specifier (union-type-types type)))))
1604 ;;; Two union types are equal if every type in one is equal to some
1605 ;;; type in the other.
1606 (define-type-method (union :simple-=) (type1 type2)
1608 (let ((types1 (union-type-types type1))
1609 (types2 (union-type-types type2)))
1610 (values (and (dolist (type1 types1 t)
1611 (unless (any-type-op type= type1 types2)
1613 (dolist (type2 types2 t)
1614 (unless (any-type-op type= type2 types1)
1618 ;;; Similarly, a union type is a subtype of another if every element
1619 ;;; of TYPE1 is a subtype of some element of TYPE2.
1620 (define-type-method (union :simple-subtypep) (type1 type2)
1622 (let ((types2 (union-type-types type2)))
1623 (values (dolist (type1 (union-type-types type1) t)
1624 (unless (any-type-op csubtypep type1 types2)
1628 (define-type-method (union :complex-subtypep-arg1) (type1 type2)
1630 (values (every-type-op csubtypep type2 (union-type-types type1)
1634 (define-type-method (union :complex-subtypep-arg2) (type1 type2)
1636 (values (any-type-op csubtypep type1 (union-type-types type2)) t)))
1638 (define-type-method (union :complex-union) (type1 type2)
1639 (let* ((class1 (type-class-info type1)))
1641 (let ((this-type type1))
1642 (dolist (type (union-type-types type2)
1644 (make-union-type (cons this-type (res)))
1646 (cond ((eq (type-class-info type) class1)
1647 (let ((union (funcall (type-class-simple-union class1)
1650 (setq this-type union)
1652 ((csubtypep type this-type))
1653 ((csubtypep type1 type) (return type2))
1657 ;;; For the union of union types, we let the :COMPLEX-UNION method do
1659 (define-type-method (union :simple-union) (type1 type2)
1661 (dolist (t2 (union-type-types type2) res)
1662 (setq res (type-union res t2)))))
1664 (define-type-method (union :simple-intersection :complex-intersection)
1666 (let ((res *empty-type*)
1668 (dolist (type (union-type-types type2) (values res win))
1669 (multiple-value-bind (int w) (type-intersection type1 type)
1670 (setq res (type-union res int))
1671 (unless w (setq win nil))))))
1673 (def-type-translator or (&rest types)
1674 (reduce #'type-union
1675 (mapcar #'specifier-type types)
1676 :initial-value *empty-type*))
1678 ;;; We don't actually have intersection types, since the result of
1679 ;;; reasonable type intersections is always describable as a union of
1680 ;;; simple types. If something is too hairy to fit this mold, then we
1681 ;;; make a hairy type.
1682 (def-type-translator and (&whole spec &rest types)
1683 (let ((res *wild-type*))
1684 (dolist (type types res)
1685 (let ((ctype (specifier-type type)))
1686 (multiple-value-bind (int win) (type-intersection res ctype)
1688 (return (make-hairy-type :specifier spec)))
1691 ;;; Return the type that describes all objects that are in X but not
1692 ;;; in Y. If we can't determine this type, then return NIL.
1694 ;;; For now, we only are clever dealing with union and member types.
1695 ;;; If either type is not a union type, then we pretend that it is a
1696 ;;; union of just one type. What we do is remove from X all the types
1697 ;;; that are a subtype any type in Y. If any type in X intersects with
1698 ;;; a type in Y but is not a subtype, then we give up.
1700 ;;; We must also special-case any member type that appears in the
1701 ;;; union. We remove from X's members all objects that are TYPEP to Y.
1702 ;;; If Y has any members, we must be careful that none of those
1703 ;;; members are CTYPEP to any of Y's non-member types. We give up in
1704 ;;; this case, since to compute that difference we would have to break
1705 ;;; the type from X into some collection of types that represents the
1706 ;;; type without that particular element. This seems too hairy to be
1707 ;;; worthwhile, given its low utility.
1708 (defun type-difference (x y)
1709 (let ((x-types (if (union-type-p x) (union-type-types x) (list x)))
1710 (y-types (if (union-type-p y) (union-type-types y) (list y))))
1712 (dolist (x-type x-types)
1713 (if (member-type-p x-type)
1714 (collect ((members))
1715 (dolist (mem (member-type-members x-type))
1716 (multiple-value-bind (val win) (ctypep mem y)
1717 (unless win (return-from type-difference nil))
1721 (res (make-member-type :members (members)))))
1722 (dolist (y-type y-types (res x-type))
1723 (multiple-value-bind (val win) (csubtypep x-type y-type)
1724 (unless win (return-from type-difference nil))
1726 (when (types-intersect x-type y-type)
1727 (return-from type-difference nil))))))
1729 (let ((y-mem (find-if #'member-type-p y-types)))
1731 (let ((members (member-type-members y-mem)))
1732 (dolist (x-type x-types)
1733 (unless (member-type-p x-type)
1734 (dolist (member members)
1735 (multiple-value-bind (val win) (ctypep member x-type)
1736 (when (or (not win) val)
1737 (return-from type-difference nil)))))))))
1739 (cond ((null (res)) *empty-type*)
1740 ((null (rest (res))) (first (res)))
1742 (make-union-type (res)))))))
1744 (def-type-translator array (&optional (element-type '*)
1746 (specialize-array-type
1747 (make-array-type :dimensions (canonical-array-dimensions dimensions)
1748 :element-type (specifier-type element-type))))
1750 (def-type-translator simple-array (&optional (element-type '*)
1752 (specialize-array-type
1753 (make-array-type :dimensions (canonical-array-dimensions dimensions)
1754 :element-type (specifier-type element-type)
1757 (!defun-from-collected-cold-init-forms !late-type-cold-init)