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 (/show0
"late-type.lisp 19")
21 (!begin-collecting-cold-init-forms
)
23 ;;; ### Remaining incorrectnesses:
25 ;;; There are all sorts of nasty problems with open bounds on FLOAT
26 ;;; types (and probably FLOAT types in general.)
28 ;;; This condition is signalled whenever we make a UNKNOWN-TYPE so that
29 ;;; compiler warnings can be emitted as appropriate.
30 (define-condition parse-unknown-type
(condition)
31 ((specifier :reader parse-unknown-type-specifier
:initarg
:specifier
))
33 :specifier
(missing-arg)))
35 ;;; This condition is signalled whenever we encounter a type (DEFTYPE,
36 ;;; structure, condition, class) that has been marked as deprecated.
37 (define-condition parse-deprecated-type
(condition)
38 ((specifier :reader parse-deprecated-type-specifier
:initarg
:specifier
))
40 :specifier
(missing-arg)))
42 ;;; These functions are used as method for types which need a complex
43 ;;; subtypep method to handle some superclasses, but cover a subtree
44 ;;; of the type graph (i.e. there is no simple way for any other type
45 ;;; class to be a subtype.) There are always still complex ways,
46 ;;; namely UNION and MEMBER types, so we must give TYPE1's method a
47 ;;; chance to run, instead of immediately returning NIL, T.
48 (defun delegate-complex-subtypep-arg2 (type1 type2
)
50 (type-class-complex-subtypep-arg1 (type-class-info type1
))))
52 (funcall subtypep-arg1 type1 type2
)
54 (defun delegate-complex-intersection2 (type1 type2
)
55 (let ((method (type-class-complex-intersection2 (type-class-info type1
))))
56 (if (and method
(not (eq method
#'delegate-complex-intersection2
)))
57 (funcall method type2 type1
)
58 (hierarchical-intersection2 type1 type2
))))
60 (defun contains-unknown-type-p (ctype)
63 (compound-type (some #'contains-unknown-type-p
(compound-type-types ctype
)))
64 (negation-type (contains-unknown-type-p (negation-type-type ctype
)))
65 (cons-type (or (contains-unknown-type-p (cons-type-car-type ctype
))
66 (contains-unknown-type-p (cons-type-cdr-type ctype
))))
67 (array-type (contains-unknown-type-p (array-type-element-type ctype
)))
69 (or (some #'contains-unknown-type-p
(args-type-required ctype
))
70 (some #'contains-unknown-type-p
(args-type-optional ctype
))
71 (acond ((args-type-rest ctype
) (contains-unknown-type-p it
)))
72 (some (lambda (x) (contains-unknown-type-p (key-info-type x
)))
73 (args-type-keywords ctype
))
74 (and (fun-type-p ctype
)
75 (contains-unknown-type-p (fun-type-returns ctype
)))))))
77 ;; Similar to (NOT CONTAINS-UNKNOWN-TYPE-P), but report that (SATISFIES F)
78 ;; is not a testable type unless F is currently bound.
79 (defun testable-type-p (ctype)
81 (unknown-type nil
) ; must precede HAIRY because an unknown is HAIRY
83 (let ((spec (hairy-type-specifier ctype
)))
84 ;; Anything other than (SATISFIES ...) is testable
85 ;; because there's no reason to suppose that it isn't.
86 (or (neq (car spec
) 'satisfies
) (fboundp (cadr spec
)))))
87 (compound-type (every #'testable-type-p
(compound-type-types ctype
)))
88 (negation-type (testable-type-p (negation-type-type ctype
)))
89 (cons-type (and (testable-type-p (cons-type-car-type ctype
))
90 (testable-type-p (cons-type-cdr-type ctype
))))
91 ;; This case could be too strict. I think an array type is testable
92 ;; if the upgraded type is testable. Probably nobody cares though.
93 (array-type (testable-type-p (array-type-element-type ctype
)))
96 ;;; This is used by !DEFINE-SUPERCLASSES to define the SUBTYPE-ARG1
97 ;;; method. INFO is a list of conses
98 ;;; (SUPERCLASS-CLASS . {GUARD-TYPE-SPECIFIER | NIL}).
99 (defun has-superclasses-complex-subtypep-arg1 (type1 type2 info
)
100 ;; If TYPE2 might be concealing something related to our class
102 (if (type-might-contain-other-types-p type2
)
103 ;; too confusing, gotta punt
105 ;; ordinary case expected by old CMU CL code, where the taxonomy
106 ;; of TYPE2's representation accurately reflects the taxonomy of
107 ;; the underlying set
109 ;; FIXME: This old CMU CL code probably deserves a comment
110 ;; explaining to us mere mortals how it works...
111 (and (sb!xc
:typep type2
'classoid
)
113 (let ((guard (cdr x
)))
114 (when (or (not guard
)
115 (csubtypep type1
(if (%instancep guard
)
118 (specifier-type guard
)))))
120 (or (eq type2
(car x
))
121 (let ((inherits (layout-inherits
122 (classoid-layout (car x
)))))
123 (dotimes (i (length inherits
) nil
)
124 (when (eq type2
(layout-classoid (svref inherits i
)))
128 ;;; This function takes a list of specs, each of the form
129 ;;; (SUPERCLASS-NAME &OPTIONAL GUARD).
130 ;;; Consider one spec (with no guard): any instance of the named
131 ;;; TYPE-CLASS is also a subtype of the named superclass and of any of
132 ;;; its superclasses. If there are multiple specs, then some will have
133 ;;; guards. We choose the first spec whose guard is a supertype of
134 ;;; TYPE1 and use its superclass. In effect, a sequence of guards
137 ;;; G0,(and G1 (not G0)), (and G2 (not (or G0 G1))).
139 ;;; WHEN controls when the forms are executed.
140 (defmacro !define-superclasses
(type-class-name specs progn-oid
)
141 (let ((defun-name (symbolicate type-class-name
"-COMPLEX-SUBTYPEP-ARG1")))
143 (defun ,defun-name
(type1 type2
)
144 (has-superclasses-complex-subtypep-arg1
147 (list ,@(mapcar (lambda (spec)
148 (destructuring-bind (super &optional guard
) spec
149 `(cons (find-classoid ',super
) ',guard
)))
150 specs
)) #-sb-xc-host t
)))
152 (let ((type-class (type-class-or-lose ',type-class-name
)))
153 (setf (type-class-complex-subtypep-arg1 type-class
) #',defun-name
)
154 (setf (type-class-complex-subtypep-arg2 type-class
)
155 #'delegate-complex-subtypep-arg2
)
156 (setf (type-class-complex-intersection2 type-class
)
157 #'delegate-complex-intersection2
))))))
159 ;;;; FUNCTION and VALUES types
161 ;;;; Pretty much all of the general type operations are illegal on
162 ;;;; VALUES types, since we can't discriminate using them, do
163 ;;;; SUBTYPEP, etc. FUNCTION types are acceptable to the normal type
164 ;;;; operations, but are generally considered to be equivalent to
165 ;;;; FUNCTION. These really aren't true types in any type theoretic
166 ;;;; sense, but we still parse them into CTYPE structures for two
169 ;;;; -- Parsing and unparsing work the same way, and indeed we can't
170 ;;;; tell whether a type is a function or values type without
172 ;;;; -- Many of the places that can be annotated with real types can
173 ;;;; also be annotated with function or values types.
175 (!define-type-method
(values :simple-subtypep
:complex-subtypep-arg1
)
177 (declare (ignore type2
))
178 ;; FIXME: should be TYPE-ERROR, here and in next method
179 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type1
)))
181 (!define-type-method
(values :complex-subtypep-arg2
)
183 (declare (ignore type1
))
184 (error "SUBTYPEP is illegal on this type:~% ~S" (type-specifier type2
)))
186 (!define-type-method
(values :negate
) (type)
187 (error "NOT VALUES too confusing on ~S" (type-specifier type
)))
189 (!define-type-method
(values :unparse
) (type)
191 (let ((unparsed (unparse-args-types type
)))
192 (if (or (values-type-optional type
)
193 (values-type-rest type
)
194 (values-type-allowp type
))
196 (nconc unparsed
'(&optional
))))))
198 ;;; Return true if LIST1 and LIST2 have the same elements in the same
199 ;;; positions according to TYPE=. We return NIL, NIL if there is an
200 ;;; uncertain comparison.
201 (defun type=-list
(list1 list2
)
202 (declare (list list1 list2
))
203 (do ((types1 list1
(cdr types1
))
204 (types2 list2
(cdr types2
)))
205 ((or (null types1
) (null types2
))
206 (if (or types1 types2
)
209 (multiple-value-bind (val win
)
210 (type= (first types1
) (first types2
))
212 (return (values nil nil
)))
214 (return (values nil t
))))))
216 (!define-type-method
(values :simple-
=) (type1 type2
)
217 (type=-args type1 type2
))
219 ;;; a flag that we can bind to cause complex function types to be
220 ;;; unparsed as FUNCTION. This is useful when we want a type that we
221 ;;; can pass to TYPEP.
222 (!defvar
*unparse-fun-type-simplify
* nil
)
223 ;;; A flag to prevent TYPE-OF calls by user applications from returning
224 ;;; (NOT x). TYPE-SPECIFIER usually allows it to preserve information.
225 (!defvar
*unparse-allow-negation
* t
)
227 (!define-type-method
(function :negate
) (type) (make-negation-type type
))
229 (!define-type-method
(function :unparse
) (type)
230 (if *unparse-fun-type-simplify
*
233 (if (fun-type-wild-args type
)
235 (unparse-args-types type
))
237 (fun-type-returns type
)))))
239 ;;; The meaning of this is a little confused. On the one hand, all
240 ;;; function objects are represented the same way regardless of the
241 ;;; arglists and return values, and apps don't get to ask things like
242 ;;; (TYPEP #'FOO (FUNCTION (FIXNUM) *)) in any meaningful way. On the
243 ;;; other hand, Python wants to reason about function types. So...
244 (!define-type-method
(function :simple-subtypep
) (type1 type2
)
245 (flet ((fun-type-simple-p (type)
246 (not (or (fun-type-rest type
)
247 (fun-type-keyp type
))))
248 (every-csubtypep (types1 types2
)
252 do
(multiple-value-bind (res sure-p
)
254 (unless res
(return (values res sure-p
))))
255 finally
(return (values t t
)))))
256 (and/type
(values-subtypep (fun-type-returns type1
)
257 (fun-type-returns type2
))
258 (cond ((fun-type-wild-args type2
) (values t t
))
259 ((fun-type-wild-args type1
)
260 (cond ((fun-type-keyp type2
) (values nil nil
))
261 ((not (fun-type-rest type2
)) (values nil t
))
262 ((not (null (fun-type-required type2
)))
264 (t (and/type
(type= *universal-type
*
265 (fun-type-rest type2
))
270 ((not (and (fun-type-simple-p type1
)
271 (fun-type-simple-p type2
)))
273 (t (multiple-value-bind (min1 max1
) (fun-type-nargs type1
)
274 (multiple-value-bind (min2 max2
) (fun-type-nargs type2
)
275 (cond ((or (> max1 max2
) (< min1 min2
))
277 ((and (= min1 min2
) (= max1 max2
))
278 (and/type
(every-csubtypep
279 (fun-type-required type1
)
280 (fun-type-required type2
))
282 (fun-type-optional type1
)
283 (fun-type-optional type2
))))
286 (fun-type-required type1
)
287 (fun-type-optional type1
))
289 (fun-type-required type2
)
290 (fun-type-optional type2
))))))))))))
292 (!define-superclasses function
((function)) !cold-init-forms
)
294 ;;; The union or intersection of two FUNCTION types is FUNCTION.
295 (!define-type-method
(function :simple-union2
) (type1 type2
)
296 (declare (ignore type1 type2
))
297 (specifier-type 'function
))
298 (!define-type-method
(function :simple-intersection2
) (type1 type2
)
299 (let ((ftype (specifier-type 'function
)))
300 (cond ((eq type1 ftype
) type2
)
301 ((eq type2 ftype
) type1
)
302 (t (let ((rtype (values-type-intersection (fun-type-returns type1
)
303 (fun-type-returns type2
))))
304 (flet ((change-returns (ftype rtype
)
305 (declare (type fun-type ftype
) (type ctype rtype
))
306 (make-fun-type :required
(fun-type-required ftype
)
307 :optional
(fun-type-optional ftype
)
308 :keyp
(fun-type-keyp ftype
)
309 :keywords
(fun-type-keywords ftype
)
310 :allowp
(fun-type-allowp ftype
)
313 ((fun-type-wild-args type1
)
314 (if (fun-type-wild-args type2
)
315 (make-fun-type :wild-args t
317 (change-returns type2 rtype
)))
318 ((fun-type-wild-args type2
)
319 (change-returns type1 rtype
))
320 (t (multiple-value-bind (req opt rest
)
321 (args-type-op type1 type2
#'type-intersection
#'max
)
322 (make-fun-type :required req
326 :allowp
(and (fun-type-allowp type1
)
327 (fun-type-allowp type2
))
328 :returns rtype
))))))))))
330 ;;; The union or intersection of a subclass of FUNCTION with a
331 ;;; FUNCTION type is somewhat complicated.
332 (!define-type-method
(function :complex-intersection2
) (type1 type2
)
334 ((type= type1
(specifier-type 'function
)) type2
)
335 ((csubtypep type1
(specifier-type 'function
)) nil
)
336 (t :call-other-method
)))
337 (!define-type-method
(function :complex-union2
) (type1 type2
)
338 (declare (ignore type2
))
339 ;; TYPE2 is a FUNCTION type. If TYPE1 is a classoid type naming
340 ;; FUNCTION, then it is the union of the two; otherwise, there is no
343 ((type= type1
(specifier-type 'function
)) type1
)
346 (!define-type-method
(function :simple-
=) (type1 type2
)
347 (macrolet ((compare (comparator field
)
348 (let ((reader (symbolicate '#:fun-type- field
)))
349 `(,comparator
(,reader type1
) (,reader type2
)))))
350 (and/type
(compare type
= returns
)
351 (cond ((neq (fun-type-wild-args type1
) (fun-type-wild-args type2
))
353 ((eq (fun-type-wild-args type1
) t
)
355 (t (type=-args type1 type2
))))))
357 (!define-type-class constant
:inherits values
)
359 (!define-type-method
(constant :negate
) (type)
360 (error "NOT CONSTANT too confusing on ~S" (type-specifier type
)))
362 (!define-type-method
(constant :unparse
) (type)
363 `(constant-arg ,(type-specifier (constant-type-type type
))))
365 (!define-type-method
(constant :simple-
=) (type1 type2
)
366 (type= (constant-type-type type1
) (constant-type-type type2
)))
368 (!def-type-translator constant-arg
((:context context
) type
)
369 (make-constant-type :type
(single-value-specifier-type-r context type
)))
371 ;;; Return the lambda-list-like type specification corresponding
373 (declaim (ftype (function (args-type) list
) unparse-args-types
))
374 (defun unparse-args-types (type)
377 (dolist (arg (args-type-required type
))
378 (result (type-specifier arg
)))
380 (when (args-type-optional type
)
382 (dolist (arg (args-type-optional type
))
383 (result (type-specifier arg
))))
385 (when (args-type-rest type
)
387 (result (type-specifier (args-type-rest type
))))
389 (when (args-type-keyp type
)
391 (dolist (key (args-type-keywords type
))
392 (result (list (key-info-name key
)
393 (type-specifier (key-info-type key
))))))
395 (when (args-type-allowp type
)
396 (result '&allow-other-keys
))
400 (!def-type-translator function
((:context context
)
401 &optional
(args '*) (result '*))
402 (let ((result (coerce-to-values (values-specifier-type-r context result
))))
404 (if (eq result
*wild-type
*)
405 (specifier-type 'function
)
406 (make-fun-type :wild-args t
:returns result
))
407 (multiple-value-bind (llks required optional rest keywords
)
408 (parse-args-types context args
:function-type
)
409 (if (and (null required
)
411 (eq rest
*universal-type
*)
412 (not (ll-kwds-keyp llks
)))
413 (if (eq result
*wild-type
*)
414 (specifier-type 'function
)
415 (make-fun-type :wild-args t
:returns result
))
416 (make-fun-type :required required
419 :keyp
(ll-kwds-keyp llks
)
421 :allowp
(ll-kwds-allowp llks
)
422 :returns result
))))))
424 (!def-type-translator values
:list
((:context context
) &rest values
)
427 (multiple-value-bind (llks required optional rest
)
428 (parse-args-types context values
:values-type
)
430 (make-values-type :required required
:optional optional
:rest rest
)
431 (make-short-values-type required
)))))
433 ;;;; VALUES types interfaces
435 ;;;; We provide a few special operations that can be meaningfully used
436 ;;;; on VALUES types (as well as on any other type).
438 ;;; Return the minimum number of values possibly matching VALUES type
440 (defun values-type-min-value-count (type)
443 (ecase (named-type-name type
)
447 (length (values-type-required type
)))))
449 ;;; Return the maximum number of values possibly matching VALUES type
451 (defun values-type-max-value-count (type)
454 (ecase (named-type-name type
)
455 ((t *) call-arguments-limit
)
458 (if (values-type-rest type
)
460 (+ (length (values-type-optional type
))
461 (length (values-type-required type
)))))))
463 (defun values-type-may-be-single-value-p (type)
464 (<= (values-type-min-value-count type
)
466 (values-type-max-value-count type
)))
468 ;;; VALUES type with a single value.
469 (defun type-single-value-p (type)
470 (and (%values-type-p type
)
471 (not (values-type-rest type
))
472 (null (values-type-optional type
))
473 (singleton-p (values-type-required type
))))
475 ;;; Return the type of the first value indicated by TYPE. This is used
476 ;;; by people who don't want to have to deal with VALUES types.
477 #!-sb-fluid
(declaim (freeze-type values-type
))
478 ; (inline single-value-type))
479 (defun single-value-type (type)
480 (declare (type ctype type
))
481 (cond ((eq type
*wild-type
*)
483 ((eq type
*empty-type
*)
485 ((not (values-type-p type
))
487 ((car (args-type-required type
)))
488 (t (type-union (specifier-type 'null
)
489 (or (car (args-type-optional type
))
490 (args-type-rest type
)
491 (specifier-type 'null
))))))
493 ;;; Return the minimum number of arguments that a function can be
494 ;;; called with, and the maximum number or NIL. If not a function
495 ;;; type, return NIL, NIL.
496 (defun fun-type-nargs (type)
497 (declare (type ctype type
))
498 (if (and (fun-type-p type
) (not (fun-type-wild-args type
)))
499 (let ((fixed (length (args-type-required type
))))
500 (if (or (args-type-rest type
)
501 (args-type-keyp type
)
502 (args-type-allowp type
))
504 (values fixed
(+ fixed
(length (args-type-optional type
))))))
507 ;;; Determine whether TYPE corresponds to a definite number of values.
508 ;;; The first value is a list of the types for each value, and the
509 ;;; second value is the number of values. If the number of values is
510 ;;; not fixed, then return NIL and :UNKNOWN.
511 (defun values-types (type)
512 (declare (type ctype type
))
513 (cond ((or (eq type
*wild-type
*) (eq type
*empty-type
*))
514 (values nil
:unknown
))
515 ((or (args-type-optional type
)
516 (args-type-rest type
))
517 (values nil
:unknown
))
519 (let ((req (args-type-required type
)))
520 (values req
(length req
))))))
522 ;;; Return two values:
523 ;;; 1. A list of all the positional (fixed and optional) types.
524 ;;; 2. The &REST type (if any). If no &REST, then the DEFAULT-TYPE.
525 (defun values-type-types (type &optional
(default-type *empty-type
*))
526 (declare (type ctype type
))
527 (if (eq type
*wild-type
*)
528 (values nil
*universal-type
*)
529 (values (append (args-type-required type
)
530 (args-type-optional type
))
531 (or (args-type-rest type
)
534 ;;; types of values in (the <type> (values o_1 ... o_n))
535 (defun values-type-out (type count
)
536 (declare (type ctype type
) (type unsigned-byte count
))
537 (if (eq type
*wild-type
*)
538 (make-list count
:initial-element
*universal-type
*)
540 (flet ((process-types (types)
541 (loop for type in types
545 (process-types (values-type-required type
))
546 (process-types (values-type-optional type
))
547 (let ((rest (values-type-rest type
)))
553 ;;; types of variable in (m-v-bind (v_1 ... v_n) (the <type> ...
554 (defun values-type-in (type count
)
555 (declare (type ctype type
) (type unsigned-byte count
))
556 (if (eq type
*wild-type
*)
557 (make-list count
:initial-element
*universal-type
*)
559 (let ((null-type (specifier-type 'null
)))
560 (loop for type in
(values-type-required type
)
564 (loop for type in
(values-type-optional type
)
567 do
(res (type-union type null-type
)))
569 (loop with rest
= (acond ((values-type-rest type
)
570 (type-union it null-type
))
576 ;;; Return a list of OPERATION applied to the types in TYPES1 and
577 ;;; TYPES2, padding with REST2 as needed. TYPES1 must not be shorter
578 ;;; than TYPES2. The second value is T if OPERATION always returned a
579 ;;; true second value.
580 (defun fixed-values-op (types1 types2 rest2 operation
)
581 (declare (list types1 types2
) (type ctype rest2
) (type function operation
))
583 (values (mapcar (lambda (t1 t2
)
584 (multiple-value-bind (res win
)
585 (funcall operation t1 t2
)
591 (make-list (- (length types1
) (length types2
))
592 :initial-element rest2
)))
595 ;;; If TYPE isn't a values type, then make it into one.
596 (defun-cached (%coerce-to-values
:hash-bits
8 :hash-function
#'type-hash-value
)
598 (cond ((multiple-value-bind (res sure
)
599 (csubtypep (specifier-type 'null
) type
)
600 (and (not res
) sure
))
601 ;; FIXME: What should we do with (NOT SURE)?
602 (make-values-type :required
(list type
) :rest
*universal-type
*))
604 (make-values-type :optional
(list type
) :rest
*universal-type
*))))
606 (defun coerce-to-values (type)
607 (declare (type ctype type
))
608 (cond ((or (eq type
*universal-type
*)
609 (eq type
*wild-type
*))
611 ((values-type-p type
)
613 (t (%coerce-to-values type
))))
615 ;;; Return type, corresponding to ANSI short form of VALUES type
617 (defun make-short-values-type (types)
618 (declare (list types
))
619 (let ((last-required (position-if
621 (not/type
(csubtypep (specifier-type 'null
) type
)))
625 (make-values-type :required
(subseq types
0 (1+ last-required
))
626 :optional
(subseq types
(1+ last-required
))
627 :rest
*universal-type
*)
628 (make-values-type :optional types
:rest
*universal-type
*))))
630 (defun make-single-value-type (type)
631 (make-values-type :required
(list type
)))
633 ;;; Do the specified OPERATION on TYPE1 and TYPE2, which may be any
634 ;;; type, including VALUES types. With VALUES types such as:
637 ;;; we compute the more useful result
638 ;;; (VALUES (<operation> a0 b0) (<operation> a1 b1))
639 ;;; rather than the precise result
640 ;;; (<operation> (values a0 a1) (values b0 b1))
641 ;;; This has the virtue of always keeping the VALUES type specifier
642 ;;; outermost, and retains all of the information that is really
643 ;;; useful for static type analysis. We want to know what is always
644 ;;; true of each value independently. It is worthless to know that if
645 ;;; the first value is B0 then the second will be B1.
647 ;;; If the VALUES count signatures differ, then we produce a result with
648 ;;; the required VALUE count chosen by NREQ when applied to the number
649 ;;; of required values in TYPE1 and TYPE2. Any &KEY values become
650 ;;; &REST T (anyone who uses keyword values deserves to lose.)
652 ;;; The second value is true if the result is definitely empty or if
653 ;;; OPERATION returned true as its second value each time we called
654 ;;; it. Since we approximate the intersection of VALUES types, the
655 ;;; second value being true doesn't mean the result is exact.
656 (defun args-type-op (type1 type2 operation nreq
)
657 (declare (type ctype type1 type2
)
658 (type function operation nreq
))
659 (when (eq type1 type2
)
661 (multiple-value-bind (types1 rest1
)
662 (values-type-types type1
)
663 (multiple-value-bind (types2 rest2
)
664 (values-type-types type2
)
665 (multiple-value-bind (rest rest-exact
)
666 (funcall operation rest1 rest2
)
667 (multiple-value-bind (res res-exact
)
668 (if (< (length types1
) (length types2
))
669 (fixed-values-op types2 types1 rest1 operation
)
670 (fixed-values-op types1 types2 rest2 operation
))
671 (let* ((req (funcall nreq
672 (length (args-type-required type1
))
673 (length (args-type-required type2
))))
674 (required (subseq res
0 req
))
675 (opt (subseq res req
)))
676 (values required opt rest
677 (and rest-exact res-exact
))))))))
679 (defun values-type-op (type1 type2 operation nreq
)
680 (multiple-value-bind (required optional rest exactp
)
681 (args-type-op type1 type2 operation nreq
)
682 (values (make-values-type :required required
687 (defun compare-key-args (type1 type2
)
688 (let ((keys1 (args-type-keywords type1
))
689 (keys2 (args-type-keywords type2
)))
690 (and (= (length keys1
) (length keys2
))
691 (eq (args-type-allowp type1
)
692 (args-type-allowp type2
))
693 (loop for key1 in keys1
694 for match
= (find (key-info-name key1
)
695 keys2
:key
#'key-info-name
)
697 (type= (key-info-type key1
)
698 (key-info-type match
)))))))
700 (defun type=-args
(type1 type2
)
701 (macrolet ((compare (comparator field
)
702 (let ((reader (symbolicate '#:args-type- field
)))
703 `(,comparator
(,reader type1
) (,reader type2
)))))
705 (cond ((null (args-type-rest type1
))
706 (values (null (args-type-rest type2
)) t
))
707 ((null (args-type-rest type2
))
710 (compare type
= rest
)))
711 (and/type
(and/type
(compare type
=-list required
)
712 (compare type
=-list optional
))
713 (if (or (args-type-keyp type1
) (args-type-keyp type2
))
714 (values (compare-key-args type1 type2
) t
)
717 ;;; Do a union or intersection operation on types that might be values
718 ;;; types. The result is optimized for utility rather than exactness,
719 ;;; but it is guaranteed that it will be no smaller (more restrictive)
720 ;;; than the precise result.
722 ;;; The return convention seems to be analogous to
723 ;;; TYPES-EQUAL-OR-INTERSECT. -- WHN 19990910.
724 (defun-cached (values-type-union :hash-function
#'type-cache-hash
726 ((type1 eq
) (type2 eq
))
727 (declare (type ctype type1 type2
))
728 (cond ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*)) *wild-type
*)
729 ((eq type1
*empty-type
*) type2
)
730 ((eq type2
*empty-type
*) type1
)
732 (values (values-type-op type1 type2
#'type-union
#'min
)))))
734 (defun-cached (values-type-intersection :hash-function
#'type-cache-hash
736 ((type1 eq
) (type2 eq
))
737 (declare (type ctype type1 type2
))
738 (cond ((eq type1
*wild-type
*)
739 (coerce-to-values type2
))
740 ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*))
742 ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
744 ((and (not (values-type-p type2
))
745 (values-type-required type1
))
746 (let ((req1 (values-type-required type1
)))
747 (make-values-type :required
(cons (type-intersection (first req1
) type2
)
749 :optional
(values-type-optional type1
)
750 :rest
(values-type-rest type1
)
751 :allowp
(values-type-allowp type1
))))
753 (values (values-type-op type1
(coerce-to-values type2
)
757 ;;; This is like TYPES-EQUAL-OR-INTERSECT, except that it sort of
758 ;;; works on VALUES types. Note that due to the semantics of
759 ;;; VALUES-TYPE-INTERSECTION, this might return (VALUES T T) when
760 ;;; there isn't really any intersection.
761 (defun values-types-equal-or-intersect (type1 type2
)
762 (cond ((or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
764 ((or (eq type1
*wild-type
*) (eq type2
*wild-type
*))
767 (let ((res (values-type-intersection type1 type2
)))
768 (values (not (eq res
*empty-type
*))
771 ;;; a SUBTYPEP-like operation that can be used on any types, including
773 (defun-cached (values-subtypep :hash-function
#'type-cache-hash
776 ((type1 eq
) (type2 eq
))
777 (declare (type ctype type1 type2
))
778 (cond ((or (eq type2
*wild-type
*) (eq type2
*universal-type
*)
779 (eq type1
*empty-type
*))
781 ((eq type1
*wild-type
*)
782 (values (eq type2
*wild-type
*) t
))
783 ((or (eq type2
*empty-type
*)
784 (not (values-types-equal-or-intersect type1 type2
)))
786 ((and (not (values-type-p type2
))
787 (values-type-required type1
))
788 (csubtypep (first (values-type-required type1
))
790 (t (setq type2
(coerce-to-values type2
))
791 (multiple-value-bind (types1 rest1
) (values-type-types type1
)
792 (multiple-value-bind (types2 rest2
) (values-type-types type2
)
793 (cond ((< (length (values-type-required type1
))
794 (length (values-type-required type2
)))
796 ((< (length types1
) (length types2
))
799 (do ((t1 types1
(rest t1
))
800 (t2 types2
(rest t2
)))
802 (csubtypep rest1 rest2
))
803 (multiple-value-bind (res win-p
)
804 (csubtypep (first t1
) (first t2
))
806 (return (values nil nil
)))
808 (return (values nil t
))))))))))))
810 ;;;; type method interfaces
812 ;;; like SUBTYPEP, only works on CTYPE structures
813 (defun-cached (csubtypep :hash-function
#'type-cache-hash
817 ((type1 eq
) (type2 eq
))
818 (declare (type ctype type1 type2
))
819 (cond ((or (eq type1 type2
)
820 (eq type1
*empty-type
*)
821 (eq type2
*universal-type
*))
824 ((eq type1
*universal-type
*)
828 (!invoke-type-method
:simple-subtypep
:complex-subtypep-arg2
830 :complex-arg1
:complex-subtypep-arg1
)))))
832 ;;; Just parse the type specifiers and call CSUBTYPE.
833 (defun sb!xc
:subtypep
(type1 type2
&optional environment
)
835 "Return two values indicating the relationship between type1 and type2.
836 If values are T and T, type1 definitely is a subtype of type2.
837 If values are NIL and T, type1 definitely is not a subtype of type2.
838 If values are NIL and NIL, it couldn't be determined."
839 (declare (type lexenv-designator environment
) (ignore environment
))
840 (declare (explicit-check))
841 (csubtypep (specifier-type type1
) (specifier-type type2
)))
843 ;;; If two types are definitely equivalent, return true. The second
844 ;;; value indicates whether the first value is definitely correct.
845 ;;; This should only fail in the presence of HAIRY types.
846 (defun-cached (type= :hash-function
#'type-cache-hash
850 ((type1 eq
) (type2 eq
))
851 (declare (type ctype type1 type2
))
852 (cond ((eq type1 type2
)
854 ;; If args are not EQ, but both allow TYPE= optimization,
855 ;; and at least one is interned, then return no and certainty.
856 ;; Most of the interned CTYPEs admit this optimization,
857 ;; NUMERIC and MEMBER types do as well.
858 ((and (minusp (logior (type-hash-value type1
) (type-hash-value type2
)))
859 (logtest (logand (type-hash-value type1
) (type-hash-value type2
))
860 +type-admits-type
=-optimization
+))
863 (memoize (!invoke-type-method
:simple-
= :complex-
= type1 type2
)))))
865 ;;; Not exactly the negation of TYPE=, since when the relationship is
866 ;;; uncertain, we still return NIL, NIL. This is useful in cases where
867 ;;; the conservative assumption is =.
868 (defun type/= (type1 type2
)
869 (declare (type ctype type1 type2
))
870 (multiple-value-bind (res win
) (type= type1 type2
)
875 ;;; the type method dispatch case of TYPE-UNION2
876 (defun %type-union2
(type1 type2
)
877 ;; As in %TYPE-INTERSECTION2, it seems to be a good idea to give
878 ;; both argument orders a chance at COMPLEX-INTERSECTION2. Unlike
879 ;; %TYPE-INTERSECTION2, though, I don't have a specific case which
880 ;; demonstrates this is actually necessary. Also unlike
881 ;; %TYPE-INTERSECTION2, there seems to be no need to distinguish
882 ;; between not finding a method and having a method return NIL.
884 (!invoke-type-method
:simple-union2
:complex-union2
887 (declare (inline 1way
))
888 (or (1way type1 type2
)
889 (1way type2 type1
))))
891 ;;; Find a type which includes both types. Any inexactness is
892 ;;; represented by the fuzzy element types; we return a single value
893 ;;; that is precise to the best of our knowledge. This result is
894 ;;; simplified into the canonical form, thus is not a UNION-TYPE
895 ;;; unless we find no other way to represent the result.
896 (defun-cached (type-union2 :hash-function
#'type-cache-hash
899 ((type1 eq
) (type2 eq
))
900 ;; KLUDGE: This was generated from TYPE-INTERSECTION2 by Ye Olde Cut And
901 ;; Paste technique of programming. If it stays around (as opposed to
902 ;; e.g. fading away in favor of some CLOS solution) the shared logic
903 ;; should probably become shared code. -- WHN 2001-03-16
904 (declare (type ctype type1 type2
))
910 ;; CSUBTYPEP for array-types answers questions about the
911 ;; specialized type, yet for union we want to take the
912 ;; expressed type in account too.
913 ((and (not (and (array-type-p type1
) (array-type-p type2
)))
914 (or (setf t2
(csubtypep type1 type2
))
915 (csubtypep type2 type1
)))
917 ((or (union-type-p type1
)
918 (union-type-p type2
))
919 ;; Unions of UNION-TYPE should have the UNION-TYPE-TYPES
920 ;; values broken out and united separately. The full TYPE-UNION
921 ;; function knows how to do this, so let it handle it.
922 (type-union type1 type2
))
924 ;; the ordinary case: we dispatch to type methods
925 (%type-union2 type1 type2
)))))))
927 ;;; the type method dispatch case of TYPE-INTERSECTION2
928 (defun %type-intersection2
(type1 type2
)
929 ;; We want to give both argument orders a chance at
930 ;; COMPLEX-INTERSECTION2. Without that, the old CMU CL type
931 ;; methods could give noncommutative results, e.g.
932 ;; (TYPE-INTERSECTION2 *EMPTY-TYPE* SOME-HAIRY-TYPE)
934 ;; (TYPE-INTERSECTION2 SOME-HAIRY-TYPE *EMPTY-TYPE*)
935 ;; => #<NAMED-TYPE NIL>, T
936 ;; We also need to distinguish between the case where we found a
937 ;; type method, and it returned NIL, and the case where we fell
938 ;; through without finding any type method. An example of the first
939 ;; case is the intersection of a HAIRY-TYPE with some ordinary type.
940 ;; An example of the second case is the intersection of two
941 ;; completely-unrelated types, e.g. CONS and NUMBER, or SYMBOL and
944 ;; (Why yes, CLOS probably *would* be nicer..)
946 (!invoke-type-method
:simple-intersection2
:complex-intersection2
948 :default
:call-other-method
)))
949 (declare (inline 1way
))
950 (let ((xy (1way type1 type2
)))
951 (or (and (not (eql xy
:call-other-method
)) xy
)
952 (let ((yx (1way type2 type1
)))
953 (or (and (not (eql yx
:call-other-method
)) yx
)
954 (cond ((and (eql xy
:call-other-method
)
955 (eql yx
:call-other-method
))
960 (defun-cached (type-intersection2 :hash-function
#'type-cache-hash
964 ((type1 eq
) (type2 eq
))
965 (declare (type ctype type1 type2
))
967 ;; FIXME: For some reason, this doesn't catch e.g. type1 =
968 ;; type2 = (SPECIFIER-TYPE
969 ;; 'SOME-UNKNOWN-TYPE). Investigate. - CSR, 2002-04-10
973 ((or (intersection-type-p type1
)
974 (intersection-type-p type2
))
975 ;; Intersections of INTERSECTION-TYPE should have the
976 ;; INTERSECTION-TYPE-TYPES values broken out and intersected
977 ;; separately. The full TYPE-INTERSECTION function knows how
978 ;; to do that, so let it handle it.
979 (type-intersection type1 type2
))
981 ;; the ordinary case: we dispatch to type methods
982 (%type-intersection2 type1 type2
))))))
984 ;;; Return as restrictive and simple a type as we can discover that is
985 ;;; no more restrictive than the intersection of TYPE1 and TYPE2. At
986 ;;; worst, we arbitrarily return one of the arguments as the first
987 ;;; value (trying not to return a hairy type).
988 (defun type-approx-intersection2 (type1 type2
)
989 (cond ((type-intersection2 type1 type2
))
990 ((hairy-type-p type1
) type2
)
993 ;;; a test useful for checking whether a derived type matches a
996 ;;; The first value is true unless the types don't intersect and
997 ;;; aren't equal. The second value is true if the first value is
998 ;;; definitely correct. NIL is considered to intersect with any type.
999 ;;; If T is a subtype of either type, then we also return T, T. This
1000 ;;; way we recognize that hairy types might intersect with T.
1002 ;;; Well now given the statement above that this is "useful for ..."
1003 ;;; a particular thing, I see how treating *empty-type* magically could
1004 ;;; be useful, however given all the _other_ calls to this function within
1005 ;;; this file, it seems suboptimal, because logically it is wrong.
1006 (defun types-equal-or-intersect (type1 type2
)
1007 (declare (type ctype type1 type2
))
1008 (if (or (eq type1
*empty-type
*) (eq type2
*empty-type
*))
1010 (let ((intersection2 (type-intersection2 type1 type2
)))
1011 (cond ((not intersection2
)
1012 (if (or (csubtypep *universal-type
* type1
)
1013 (csubtypep *universal-type
* type2
))
1016 ((eq intersection2
*empty-type
*) (values nil t
))
1017 (t (values t t
))))))
1019 ;;; Return a Common Lisp type specifier corresponding to the TYPE
1021 (defun type-specifier (type)
1022 (declare (type ctype type
))
1023 (funcall (type-class-unparse (type-class-info type
)) type
))
1025 ;;; Don't try to define a print method until it's actually gonna work!
1026 ;;; (Otherwise this would be near the DEFSTRUCT)
1027 (defmethod print-object ((ctype ctype
) stream
)
1028 (print-unreadable-object (ctype stream
:type t
)
1029 (prin1 (type-specifier ctype
) stream
)))
1032 ;;; Just dump it as a specifier. (We'll convert it back upon loading.)
1033 (defmethod make-load-form ((type ctype
) &optional env
)
1034 (declare (ignore env
))
1035 `(specifier-type ',(type-specifier type
)))
1037 (defun-cached (type-negation :hash-function
#'type-hash-value
1041 (declare (type ctype type
))
1042 (funcall (type-class-negate (type-class-info type
)) type
))
1044 (defun-cached (type-singleton-p :hash-function
#'type-hash-value
1048 (declare (type ctype type
))
1049 (let ((function (type-class-singleton-p (type-class-info type
))))
1051 (funcall function type
)
1054 ;;; (VALUES-SPECIFIER-TYPE and SPECIFIER-TYPE moved from here to
1055 ;;; early-type.lisp by WHN ca. 19990201.)
1057 ;;; Take a list of type specifiers, computing the translation of each
1058 ;;; specifier and defining it as a builtin type.
1059 ;;; Seee the comments in 'type-init' for why this is a slightly
1060 ;;; screwy way to go about it.
1061 (declaim (ftype (function (list) (values)) !precompute-types
))
1062 (defun !precompute-types
(specs)
1063 (dolist (spec specs
)
1064 (let ((res (handler-bind
1065 ((parse-unknown-type
1067 (declare (ignore c
))
1068 ;; We can handle conditions at this point,
1069 ;; but win32 can not perform i/o here because
1070 ;; !MAKE-COLD-STDERR-STREAM has no implementation.
1072 (progn (write-string "//caught: parse-unknown ")
1075 (specifier-type spec
))))
1076 (unless (unknown-type-p res
)
1077 (setf (info :type
:builtin spec
) res
)
1078 (setf (info :type
:kind spec
) :primitive
))))
1081 ;;;; general TYPE-UNION and TYPE-INTERSECTION operations
1083 ;;;; These are fully general operations on CTYPEs: they'll always
1084 ;;;; return a CTYPE representing the result.
1086 ;;; shared logic for unions and intersections: Return a list of
1087 ;;; types representing the same types as INPUT-TYPES, but with
1088 ;;; COMPOUND-TYPEs satisfying %COMPOUND-TYPE-P broken up into their
1089 ;;; component types, and with any SIMPLY2 simplifications applied.
1091 ((def (name compound-type-p simplify2
)
1092 `(defun ,name
(types)
1094 (multiple-value-bind (first rest
)
1095 (if (,compound-type-p
(car types
))
1096 (values (car (compound-type-types (car types
)))
1097 (append (cdr (compound-type-types (car types
)))
1099 (values (car types
) (cdr types
)))
1100 (let ((rest (,name rest
)) u
)
1101 (dolist (r rest
(cons first rest
))
1102 (when (setq u
(,simplify2 first r
))
1103 (return (,name
(nsubstitute u r rest
)))))))))))
1104 (def simplify-intersections intersection-type-p type-intersection2
)
1105 (def simplify-unions union-type-p type-union2
))
1107 (defun maybe-distribute-one-union (union-type types
)
1108 (let* ((intersection (apply #'type-intersection types
))
1109 (union (mapcar (lambda (x) (type-intersection x intersection
))
1110 (union-type-types union-type
))))
1111 (if (notany (lambda (x) (or (hairy-type-p x
)
1112 (intersection-type-p x
)))
1117 (defun type-intersection (&rest input-types
)
1118 (%type-intersection input-types
))
1119 (defun-cached (%type-intersection
:hash-bits
10 :hash-function
#'type-list-cache-hash
)
1120 ((input-types equal
))
1121 (let ((simplified-types (simplify-intersections input-types
)))
1122 (declare (type list simplified-types
))
1123 ;; We want to have a canonical representation of types (or failing
1124 ;; that, punt to HAIRY-TYPE). Canonical representation would have
1125 ;; intersections inside unions but not vice versa, since you can
1126 ;; always achieve that by the distributive rule. But we don't want
1127 ;; to just apply the distributive rule, since it would be too easy
1128 ;; to end up with unreasonably huge type expressions. So instead
1129 ;; we try to generate a simple type by distributing the union; if
1130 ;; the type can't be made simple, we punt to HAIRY-TYPE.
1131 (if (and (cdr simplified-types
) (some #'union-type-p simplified-types
))
1132 (let* ((first-union (find-if #'union-type-p simplified-types
))
1133 (other-types (coerce (remove first-union simplified-types
)
1135 (distributed (maybe-distribute-one-union first-union
1138 (apply #'type-union distributed
)
1139 (%make-hairy-type
`(and ,@(map 'list
#'type-specifier
1140 simplified-types
)))))
1142 ((null simplified-types
) *universal-type
*)
1143 ((null (cdr simplified-types
)) (car simplified-types
))
1144 (t (%make-intersection-type
1145 (some #'type-enumerable simplified-types
)
1146 simplified-types
))))))
1148 (defun type-union (&rest input-types
)
1149 (%type-union input-types
))
1150 (defun-cached (%type-union
:hash-bits
8 :hash-function
#'type-list-cache-hash
)
1151 ((input-types equal
))
1152 (let ((simplified-types (simplify-unions input-types
)))
1154 ((null simplified-types
) *empty-type
*)
1155 ((null (cdr simplified-types
)) (car simplified-types
))
1157 (every #'type-enumerable simplified-types
)
1158 simplified-types
)))))
1162 (!define-type-method
(named :simple-
=) (type1 type2
)
1163 ;;(aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1164 (values (eq type1 type2
) t
))
1166 (defun cons-type-might-be-empty-type (type)
1167 (declare (type cons-type type
))
1168 (let ((car-type (cons-type-car-type type
))
1169 (cdr-type (cons-type-cdr-type type
)))
1171 (if (cons-type-p car-type
)
1172 (cons-type-might-be-empty-type car-type
)
1173 (multiple-value-bind (yes surep
)
1174 (type= car-type
*empty-type
*)
1177 (if (cons-type-p cdr-type
)
1178 (cons-type-might-be-empty-type cdr-type
)
1179 (multiple-value-bind (yes surep
)
1180 (type= cdr-type
*empty-type
*)
1184 (defun cons-type-length-info (type)
1185 (declare (type cons-type type
))
1186 (do ((min 1 (1+ min
))
1187 (cdr (cons-type-cdr-type type
) (cons-type-cdr-type cdr
)))
1188 ((not (cons-type-p cdr
))
1190 ((csubtypep cdr
(specifier-type 'null
))
1192 ((csubtypep *universal-type
* cdr
)
1194 ((type/= (type-intersection (specifier-type 'cons
) cdr
) *empty-type
*)
1196 ((type/= (type-intersection (specifier-type 'null
) cdr
) *empty-type
*)
1198 (t (values min
:maybe
))))
1201 (!define-type-method
(named :complex-
=) (type1 type2
)
1203 ((and (eq type2
*empty-type
*)
1204 (or (and (intersection-type-p type1
)
1205 ;; not allowed to be unsure on these... FIXME: keep
1206 ;; the list of CL types that are intersection types
1207 ;; once and only once.
1208 (not (or (type= type1
(specifier-type 'ratio
))
1209 (type= type1
(specifier-type 'keyword
)))))
1210 (and (cons-type-p type1
)
1211 (cons-type-might-be-empty-type type1
))))
1212 ;; things like (AND (EQL 0) (SATISFIES ODDP)) or (AND FUNCTION
1213 ;; STREAM) can get here. In general, we can't really tell
1214 ;; whether these are equal to NIL or not, so
1216 ((type-might-contain-other-types-p type1
)
1217 (invoke-complex-=-other-method type1 type2
))
1218 (t (values nil t
))))
1220 (!define-type-method
(named :simple-subtypep
) (type1 type2
)
1221 (aver (not (eq type1
*wild-type
*))) ; * isn't really a type.
1222 (aver (not (eq type1 type2
)))
1223 (values (or (eq type1
*empty-type
*)
1224 (eq type2
*wild-type
*)
1225 (eq type2
*universal-type
*)) t
))
1227 (!define-type-method
(named :complex-subtypep-arg1
) (type1 type2
)
1228 ;; This AVER causes problems if we write accurate methods for the
1229 ;; union (and possibly intersection) types which then delegate to
1230 ;; us; while a user shouldn't get here, because of the odd status of
1231 ;; *wild-type* a type-intersection executed by the compiler can. -
1234 ;; (aver (not (eq type1 *wild-type*))) ; * isn't really a type.
1235 (cond ((eq type1
*empty-type
*)
1237 (;; When TYPE2 might be the universal type in disguise
1238 (type-might-contain-other-types-p type2
)
1239 ;; Now that the UNION and HAIRY COMPLEX-SUBTYPEP-ARG2 methods
1240 ;; can delegate to us (more or less as CALL-NEXT-METHOD) when
1241 ;; they're uncertain, we can't just barf on COMPOUND-TYPE and
1242 ;; HAIRY-TYPEs as we used to. Instead we deal with the
1243 ;; problem (where at least part of the problem is cases like
1244 ;; (SUBTYPEP T '(SATISFIES FOO))
1246 ;; (SUBTYPEP T '(AND (SATISFIES FOO) (SATISFIES BAR)))
1247 ;; where the second type is a hairy type like SATISFIES, or
1248 ;; is a compound type which might contain a hairy type) by
1249 ;; returning uncertainty.
1251 ((eq type1
*funcallable-instance-type
*)
1252 (values (eq type2
(specifier-type 'function
)) t
))
1254 ;; This case would have been picked off by the SIMPLE-SUBTYPEP
1255 ;; method, and so shouldn't appear here.
1256 (aver (not (named-type-p type2
)))
1257 ;; Since TYPE2 is not EQ *UNIVERSAL-TYPE* and is not another
1258 ;; named type in disguise, TYPE2 is not a superset of TYPE1.
1261 (!define-type-method
(named :complex-subtypep-arg2
) (type1 type2
)
1262 (aver (not (eq type2
*wild-type
*))) ; * isn't really a type.
1263 (cond ((eq type2
*universal-type
*)
1265 ;; some CONS types can conceal danger
1266 ((and (cons-type-p type1
) (cons-type-might-be-empty-type type1
))
1268 ((type-might-contain-other-types-p type1
)
1269 ;; those types can be other types in disguise. So we'd
1271 (invoke-complex-subtypep-arg1-method type1 type2
))
1272 ((and (or (eq type2
*instance-type
*)
1273 (eq type2
*funcallable-instance-type
*))
1274 (member-type-p type1
))
1275 ;; member types can be subtypep INSTANCE and
1276 ;; FUNCALLABLE-INSTANCE in surprising ways.
1277 (invoke-complex-subtypep-arg1-method type1 type2
))
1278 ((and (eq type2
*extended-sequence-type
*) (classoid-p type1
))
1279 (values (if (classoid-inherits-from type1
'sequence
) t nil
) t
))
1280 ((and (eq type2
*instance-type
*) (classoid-p type1
))
1282 ((classoid-non-instance-p type1
)
1284 ((classoid-inherits-from type1
'function
)
1286 ((eq type1
(find-classoid 'function
))
1288 ((or (structure-classoid-p type1
)
1289 (condition-classoid-p type1
))
1291 (t (values nil nil
))))
1292 ((and (eq type2
*funcallable-instance-type
*) (classoid-p type1
))
1293 (if (and (not (classoid-non-instance-p type1
))
1294 (classoid-inherits-from type1
'function
))
1298 ;; FIXME: This seems to rely on there only being 4 or 5
1299 ;; NAMED-TYPE values, and the exclusion of various
1300 ;; possibilities above. It would be good to explain it and/or
1301 ;; rewrite it so that it's clearer.
1304 (!define-type-method
(named :simple-intersection2
) (type1 type2
)
1306 ((and (eq type1
*extended-sequence-type
*)
1307 (or (eq type2
*instance-type
*)
1308 (eq type2
*funcallable-instance-type
*)))
1310 ((and (or (eq type1
*instance-type
*)
1311 (eq type1
*funcallable-instance-type
*))
1312 (eq type2
*extended-sequence-type
*))
1315 (hierarchical-intersection2 type1 type2
))))
1317 (!define-type-method
(named :complex-intersection2
) (type1 type2
)
1318 ;; FIXME: This assertion failed when I added it in sbcl-0.6.11.13.
1319 ;; Perhaps when bug 85 is fixed it can be reenabled.
1320 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1321 (flet ((empty-unless-hairy (type)
1322 (unless (or (type-might-contain-other-types-p type
)
1323 (member-type-p type
))
1326 ((eq type2
*extended-sequence-type
*)
1328 ((or structure-classoid condition-classoid
) *empty-type
*)
1330 ((classoid-non-instance-p type1
) *empty-type
*)
1331 ((classoid-inherits-from type1
'sequence
) type1
)))
1332 (t (empty-unless-hairy type1
))))
1333 ((eq type2
*instance-type
*)
1335 ((or structure-classoid condition-classoid
) type1
)
1336 (classoid (when (or (classoid-non-instance-p type1
)
1337 (eq type1
(find-classoid 'function
))
1338 (classoid-inherits-from type1
'function
))
1340 (t (empty-unless-hairy type1
))))
1341 ((eq type2
*funcallable-instance-type
*)
1343 ((or structure-classoid condition-classoid
) *empty-type
*)
1346 ((classoid-non-instance-p type1
) *empty-type
*)
1347 ((classoid-inherits-from type1
'function
) type1
)
1348 ((type= type1
(find-classoid 'function
)) type2
)))
1350 (t (empty-unless-hairy type1
))))
1351 (t (hierarchical-intersection2 type1 type2
)))))
1353 (!define-type-method
(named :complex-union2
) (type1 type2
)
1354 ;; Perhaps when bug 85 is fixed this can be reenabled.
1355 ;;(aver (not (eq type2 *wild-type*))) ; * isn't really a type.
1357 ((eq type2
*extended-sequence-type
*)
1358 (cond ((not (classoid-p type1
)) nil
)
1359 ((and (not (classoid-non-instance-p type1
))
1360 (classoid-inherits-from type1
'sequence
))
1362 ((eq type2
*instance-type
*)
1363 (cond ((not (classoid-p type1
)) nil
)
1364 ((and (not (classoid-non-instance-p type1
))
1365 (not (classoid-inherits-from type1
'function
)))
1367 ((eq type2
*funcallable-instance-type
*)
1368 (cond ((not (classoid-p type1
)) nil
)
1369 ((classoid-non-instance-p type1
) nil
)
1370 ((not (classoid-inherits-from type1
'function
)) nil
)
1371 ((eq type1
(specifier-type 'function
)) type1
)
1373 (t (hierarchical-union2 type1 type2
))))
1375 (!define-type-method
(named :negate
) (x)
1376 (aver (not (eq x
*wild-type
*)))
1378 ((eq x
*universal-type
*) *empty-type
*)
1379 ((eq x
*empty-type
*) *universal-type
*)
1380 ((or (eq x
*instance-type
*)
1381 (eq x
*funcallable-instance-type
*)
1382 (eq x
*extended-sequence-type
*))
1383 (make-negation-type x
))
1384 (t (bug "NAMED type unexpected: ~S" x
))))
1386 (!define-type-method
(named :unparse
) (x)
1387 (named-type-name x
))
1389 ;;;; hairy and unknown types
1390 ;;;; DEFINE-TYPE-CLASS HAIRY is in 'early-type'
1392 (!define-type-method
(hairy :negate
) (x) (make-negation-type x
))
1394 (!define-type-method
(hairy :unparse
) (x)
1395 (hairy-type-specifier x
))
1397 (!define-type-method
(hairy :simple-subtypep
) (type1 type2
)
1398 (let ((hairy-spec1 (hairy-type-specifier type1
))
1399 (hairy-spec2 (hairy-type-specifier type2
)))
1400 (cond ((equal-but-no-car-recursion hairy-spec1 hairy-spec2
)
1402 ((maybe-reparse-specifier! type1
)
1403 (csubtypep type1 type2
))
1404 ((maybe-reparse-specifier! type2
)
1405 (csubtypep type1 type2
))
1407 (values nil nil
)))))
1409 (!define-type-method
(hairy :complex-subtypep-arg2
) (type1 type2
)
1410 (if (maybe-reparse-specifier! type2
)
1411 (csubtypep type1 type2
)
1412 (let ((specifier (hairy-type-specifier type2
)))
1413 (cond ((and (consp specifier
) (eql (car specifier
) 'satisfies
))
1414 (case (cadr specifier
)
1415 ((keywordp) (if (type= type1
(specifier-type 'symbol
))
1417 (invoke-complex-subtypep-arg1-method type1 type2
)))
1418 (t (invoke-complex-subtypep-arg1-method type1 type2
))))
1420 (invoke-complex-subtypep-arg1-method type1 type2
))))))
1422 (!define-type-method
(hairy :complex-subtypep-arg1
) (type1 type2
)
1423 (if (maybe-reparse-specifier! type1
)
1424 (csubtypep type1 type2
)
1427 (!define-type-method
(hairy :complex-
=) (type1 type2
)
1428 (if (maybe-reparse-specifier! type2
)
1432 (!define-type-method
(hairy :simple-intersection2
:complex-intersection2
)
1434 (acond ((type= type1 type2
)
1436 ((eq type2
(literal-ctype *satisfies-keywordp-type
*))
1437 ;; (AND (MEMBER A) (SATISFIES KEYWORDP)) is possibly non-empty
1438 ;; if A is re-homed as :A. However as a special case that really
1439 ;; does occur, (AND (MEMBER NIL) (SATISFIES KEYWORDP))
1440 ;; is empty because of the illegality of changing NIL's package.
1441 (if (eq type1
(specifier-type 'null
))
1443 (multiple-value-bind (answer certain
)
1444 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1445 (and (not answer
) certain
*empty-type
*))))
1446 ((eq type2
(literal-ctype *fun-name-type
*))
1447 (multiple-value-bind (answer certain
)
1448 (types-equal-or-intersect type1
(specifier-type 'symbol
))
1451 (multiple-value-bind (answer certain
)
1452 (types-equal-or-intersect type1
(specifier-type 'cons
))
1453 (and (not answer
) certain
*empty-type
*)))))
1454 ((and (typep (hairy-type-specifier type2
) '(cons (eql satisfies
)))
1455 (info :function
:predicate-truth-constraint
1456 (cadr (hairy-type-specifier type2
))))
1457 (multiple-value-bind (answer certain
)
1458 (types-equal-or-intersect type1
(specifier-type it
))
1459 (and (not answer
) certain
*empty-type
*)))))
1461 (!define-type-method
(hairy :simple-union2
)
1463 (if (type= type1 type2
)
1467 (!define-type-method
(hairy :simple-
=) (type1 type2
)
1468 (if (equal-but-no-car-recursion (hairy-type-specifier type1
)
1469 (hairy-type-specifier type2
))
1473 (!def-type-translator satisfies
:list
(&whole whole predicate-name
)
1474 (unless (symbolp predicate-name
)
1475 (error 'simple-type-error
1476 :datum predicate-name
1477 :expected-type
'symbol
1478 :format-control
"The SATISFIES predicate name is not a symbol: ~S"
1479 :format-arguments
(list predicate-name
)))
1480 (case predicate-name
1481 (keywordp (literal-ctype *satisfies-keywordp-type
*))
1482 (legal-fun-name-p (literal-ctype *fun-name-type
*))
1483 (t (%make-hairy-type whole
))))
1487 (!define-type-method
(negation :negate
) (x)
1488 (negation-type-type x
))
1490 (!define-type-method
(negation :unparse
) (x)
1491 (if (type= (negation-type-type x
) (specifier-type 'cons
))
1493 `(not ,(type-specifier (negation-type-type x
)))))
1495 (!define-type-method
(negation :simple-subtypep
) (type1 type2
)
1496 (csubtypep (negation-type-type type2
) (negation-type-type type1
)))
1498 (!define-type-method
(negation :complex-subtypep-arg2
) (type1 type2
)
1499 (let* ((complement-type2 (negation-type-type type2
))
1500 (intersection2 (type-intersection2 type1
1503 ;; FIXME: if uncertain, maybe try arg1?
1504 (type= intersection2
*empty-type
*)
1505 (invoke-complex-subtypep-arg1-method type1 type2
))))
1507 (!define-type-method
(negation :complex-subtypep-arg1
) (type1 type2
)
1508 ;; "Incrementally extended heuristic algorithms tend inexorably toward the
1509 ;; incomprehensible." -- http://www.unlambda.com/~james/lambda/lambda.txt
1511 ;; You may not believe this. I couldn't either. But then I sat down
1512 ;; and drew lots of Venn diagrams. Comments involving a and b refer
1513 ;; to the call (subtypep '(not a) 'b) -- CSR, 2002-02-27.
1515 ;; (Several logical truths in this block are true as long as
1516 ;; b/=T. As of sbcl-0.7.1.28, it seems impossible to construct a
1517 ;; case with b=T where we actually reach this type method, but
1518 ;; we'll test for and exclude this case anyway, since future
1519 ;; maintenance might make it possible for it to end up in this
1521 (multiple-value-bind (equal certain
)
1522 (type= type2
*universal-type
*)
1524 (return (values nil nil
)))
1526 (return (values t t
))))
1527 (let ((complement-type1 (negation-type-type type1
)))
1528 ;; Do the special cases first, in order to give us a chance if
1529 ;; subtype/supertype relationships are hairy.
1530 (multiple-value-bind (equal certain
)
1531 (type= complement-type1 type2
)
1532 ;; If a = b, ~a is not a subtype of b (unless b=T, which was
1535 (return (values nil nil
)))
1537 (return (values nil t
))))
1538 ;; KLUDGE: ANSI requires that the SUBTYPEP result between any
1539 ;; two built-in atomic type specifiers never be uncertain. This
1540 ;; is hard to do cleanly for the built-in types whose
1541 ;; definitions include (NOT FOO), i.e. CONS and RATIO. However,
1542 ;; we can do it with this hack, which uses our global knowledge
1543 ;; that our implementation of the type system uses disjoint
1544 ;; implementation types to represent disjoint sets (except when
1545 ;; types are contained in other types). (This is a KLUDGE
1546 ;; because it's fragile. Various changes in internal
1547 ;; representation in the type system could make it start
1548 ;; confidently returning incorrect results.) -- WHN 2002-03-08
1549 (unless (or (type-might-contain-other-types-p complement-type1
)
1550 (type-might-contain-other-types-p type2
))
1551 ;; Because of the way our types which don't contain other
1552 ;; types are disjoint subsets of the space of possible values,
1553 ;; (SUBTYPEP '(NOT AA) 'B)=NIL when AA and B are simple (and B
1554 ;; is not T, as checked above).
1555 (return (values nil t
)))
1556 ;; The old (TYPE= TYPE1 TYPE2) branch would never be taken, as
1557 ;; TYPE1 and TYPE2 will only be equal if they're both NOT types,
1558 ;; and then the :SIMPLE-SUBTYPEP method would be used instead.
1559 ;; But a CSUBTYPEP relationship might still hold:
1560 (multiple-value-bind (equal certain
)
1561 (csubtypep complement-type1 type2
)
1562 ;; If a is a subtype of b, ~a is not a subtype of b (unless
1563 ;; b=T, which was excluded above).
1565 (return (values nil nil
)))
1567 (return (values nil t
))))
1568 (multiple-value-bind (equal certain
)
1569 (csubtypep type2 complement-type1
)
1570 ;; If b is a subtype of a, ~a is not a subtype of b. (FIXME:
1571 ;; That's not true if a=T. Do we know at this point that a is
1574 (return (values nil nil
)))
1576 (return (values nil t
))))
1577 ;; old CSR comment ca. 0.7.2, now obsoleted by the SIMPLE-CTYPE?
1578 ;; KLUDGE case above: Other cases here would rely on being able
1579 ;; to catch all possible cases, which the fragility of this type
1580 ;; system doesn't inspire me; for instance, if a is type= to ~b,
1581 ;; then we want T, T; if this is not the case and the types are
1582 ;; disjoint (have an intersection of *empty-type*) then we want
1583 ;; NIL, T; else if the union of a and b is the *universal-type*
1584 ;; then we want T, T. So currently we still claim to be unsure
1585 ;; about e.g. (subtypep '(not fixnum) 'single-float).
1587 ;; OTOH we might still get here:
1590 (!define-type-method
(negation :complex-
=) (type1 type2
)
1591 ;; (NOT FOO) isn't equivalent to anything that's not a negation
1592 ;; type, except possibly a type that might contain it in disguise.
1593 (declare (ignore type2
))
1594 (if (type-might-contain-other-types-p type1
)
1598 (!define-type-method
(negation :simple-intersection2
) (type1 type2
)
1599 (let ((not1 (negation-type-type type1
))
1600 (not2 (negation-type-type type2
)))
1602 ((csubtypep not1 not2
) type2
)
1603 ((csubtypep not2 not1
) type1
)
1604 ;; Why no analagous clause to the disjoint in the SIMPLE-UNION2
1605 ;; method, below? The clause would read
1607 ;; ((EQ (TYPE-UNION NOT1 NOT2) *UNIVERSAL-TYPE*) *EMPTY-TYPE*)
1609 ;; but with proper canonicalization of negation types, there's
1610 ;; no way of constructing two negation types with union of their
1611 ;; negations being the universal type.
1613 (aver (not (eq (type-union not1 not2
) *universal-type
*)))
1616 (defun maybe-complex-array-refinement (type1 type2
)
1617 (let* ((ntype (negation-type-type type2
))
1618 (ndims (array-type-dimensions ntype
))
1619 (ncomplexp (array-type-complexp ntype
))
1620 (nseltype (array-type-specialized-element-type ntype
))
1621 (neltype (array-type-element-type ntype
)))
1622 (if (and (eql ndims
'*) (null ncomplexp
)
1623 (eq neltype
*wild-type
*) (eq nseltype
*wild-type
*))
1624 (make-array-type (array-type-dimensions type1
)
1626 :element-type
(array-type-element-type type1
)
1627 :specialized-element-type
(array-type-specialized-element-type type1
)))))
1629 (!define-type-method
(negation :complex-intersection2
) (type1 type2
)
1631 ((csubtypep type1
(negation-type-type type2
)) *empty-type
*)
1632 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1634 ((and (array-type-p type1
) (array-type-p (negation-type-type type2
)))
1635 (maybe-complex-array-refinement type1 type2
))
1638 (!define-type-method
(negation :simple-union2
) (type1 type2
)
1639 (let ((not1 (negation-type-type type1
))
1640 (not2 (negation-type-type type2
)))
1642 ((csubtypep not1 not2
) type1
)
1643 ((csubtypep not2 not1
) type2
)
1644 ((eq (type-intersection not1 not2
) *empty-type
*)
1648 (!define-type-method
(negation :complex-union2
) (type1 type2
)
1650 ((csubtypep (negation-type-type type2
) type1
) *universal-type
*)
1651 ((eq (type-intersection type1
(negation-type-type type2
)) *empty-type
*)
1655 (!define-type-method
(negation :simple-
=) (type1 type2
)
1656 (type= (negation-type-type type1
) (negation-type-type type2
)))
1658 (!def-type-translator not
:list
((:context context
) typespec
)
1659 (type-negation (specifier-type-r context typespec
)))
1663 (declaim (inline numeric-type-equal
))
1664 (defun numeric-type-equal (type1 type2
)
1665 (and (eq (numeric-type-class type1
) (numeric-type-class type2
))
1666 (eq (numeric-type-format type1
) (numeric-type-format type2
))
1667 (eq (numeric-type-complexp type1
) (numeric-type-complexp type2
))))
1669 (!define-type-method
(number :simple-
=) (type1 type2
)
1671 (and (numeric-type-equal type1 type2
)
1672 (equalp (numeric-type-low type1
) (numeric-type-low type2
))
1673 (equalp (numeric-type-high type1
) (numeric-type-high type2
)))
1676 (!define-type-method
(number :negate
) (type)
1677 (if (and (null (numeric-type-low type
)) (null (numeric-type-high type
)))
1678 (make-negation-type type
)
1680 (make-negation-type (modified-numeric-type type
:low nil
:high nil
))
1682 ((null (numeric-type-low type
))
1683 (modified-numeric-type
1685 :low
(let ((h (numeric-type-high type
)))
1686 (if (consp h
) (car h
) (list h
)))
1688 ((null (numeric-type-high type
))
1689 (modified-numeric-type
1692 :high
(let ((l (numeric-type-low type
)))
1693 (if (consp l
) (car l
) (list l
)))))
1695 (modified-numeric-type
1698 :high
(let ((l (numeric-type-low type
)))
1699 (if (consp l
) (car l
) (list l
))))
1700 (modified-numeric-type
1702 :low
(let ((h (numeric-type-high type
)))
1703 (if (consp h
) (car h
) (list h
)))
1706 (!define-type-method
(number :unparse
) (type)
1707 (let* ((complexp (numeric-type-complexp type
))
1708 (low (numeric-type-low type
))
1709 (high (numeric-type-high type
))
1710 (base (case (numeric-type-class type
)
1712 (rational 'rational
)
1713 (float (or (numeric-type-format type
) 'float
))
1716 (cond ((and (eq base
'integer
) high low
)
1717 (let ((high-count (logcount high
))
1718 (high-length (integer-length high
)))
1720 (cond ((= high
0) '(integer 0 0))
1722 ((and (= high-count high-length
)
1723 (plusp high-length
))
1724 `(unsigned-byte ,high-length
))
1726 `(mod ,(1+ high
)))))
1727 ((and (= low sb
!xc
:most-negative-fixnum
)
1728 (= high sb
!xc
:most-positive-fixnum
))
1730 ((and (= low
(lognot high
))
1731 (= high-count high-length
)
1733 `(signed-byte ,(1+ high-length
)))
1735 `(integer ,low
,high
)))))
1736 (high `(,base
,(or low
'*) ,high
))
1738 (if (and (eq base
'integer
) (= low
0))
1746 (aver (neq base
+bounds
'real
))
1747 `(complex ,base
+bounds
))
1749 (aver (eq base
+bounds
'real
))
1752 (!define-type-method
(number :singleton-p
) (type)
1753 (let ((low (numeric-type-low type
))
1754 (high (numeric-type-high type
)))
1757 (eql (numeric-type-complexp type
) :real
)
1758 (member (numeric-type-class type
) '(integer rational
1759 #-sb-xc-host float
)))
1760 (values t
(numeric-type-low type
))
1763 ;;; Return true if X is "less than or equal" to Y, taking open bounds
1764 ;;; into consideration. CLOSED is the predicate used to test the bound
1765 ;;; on a closed interval (e.g. <=), and OPEN is the predicate used on
1766 ;;; open bounds (e.g. <). Y is considered to be the outside bound, in
1767 ;;; the sense that if it is infinite (NIL), then the test succeeds,
1768 ;;; whereas if X is infinite, then the test fails (unless Y is also
1771 ;;; This is for comparing bounds of the same kind, e.g. upper and
1772 ;;; upper. Use NUMERIC-BOUND-TEST* for different kinds of bounds.
1773 (defmacro numeric-bound-test
(x y closed open
)
1778 (,closed
(car ,x
) (car ,y
))
1779 (,closed
(car ,x
) ,y
)))
1785 ;;; This is used to compare upper and lower bounds. This is different
1786 ;;; from the same-bound case:
1787 ;;; -- Since X = NIL is -infinity, whereas y = NIL is +infinity, we
1788 ;;; return true if *either* arg is NIL.
1789 ;;; -- an open inner bound is "greater" and also squeezes the interval,
1790 ;;; causing us to use the OPEN test for those cases as well.
1791 (defmacro numeric-bound-test
* (x y closed open
)
1796 (,open
(car ,x
) (car ,y
))
1797 (,open
(car ,x
) ,y
)))
1803 ;;; Return whichever of the numeric bounds X and Y is "maximal"
1804 ;;; according to the predicates CLOSED (e.g. >=) and OPEN (e.g. >).
1805 ;;; This is only meaningful for maximizing like bounds, i.e. upper and
1806 ;;; upper. If MAX-P is true, then we return NIL if X or Y is NIL,
1807 ;;; otherwise we return the other arg.
1808 (defmacro numeric-bound-max
(x y closed open max-p
)
1811 `(cond ((not ,n-x
) ,(if max-p nil n-y
))
1812 ((not ,n-y
) ,(if max-p nil n-x
))
1815 (if (,closed
(car ,n-x
) (car ,n-y
)) ,n-x
,n-y
)
1816 (if (,open
(car ,n-x
) ,n-y
) ,n-x
,n-y
)))
1819 (if (,open
(car ,n-y
) ,n-x
) ,n-y
,n-x
)
1820 (if (,closed
,n-y
,n-x
) ,n-y
,n-x
))))))
1822 (!define-type-method
(number :simple-subtypep
) (type1 type2
)
1823 (let ((class1 (numeric-type-class type1
))
1824 (class2 (numeric-type-class type2
))
1825 (complexp2 (numeric-type-complexp type2
))
1826 (format2 (numeric-type-format type2
))
1827 (low1 (numeric-type-low type1
))
1828 (high1 (numeric-type-high type1
))
1829 (low2 (numeric-type-low type2
))
1830 (high2 (numeric-type-high type2
)))
1831 ;; If one is complex and the other isn't, they are disjoint.
1832 (cond ((not (or (eq (numeric-type-complexp type1
) complexp2
)
1835 ;; If the classes are specified and different, the types are
1836 ;; disjoint unless type2 is RATIONAL and type1 is INTEGER.
1837 ;; [ or type1 is INTEGER and type2 is of the form (RATIONAL
1838 ;; X X) for integral X, but this is dealt with in the
1839 ;; canonicalization inside MAKE-NUMERIC-TYPE ]
1840 ((not (or (eq class1 class2
)
1842 (and (eq class1
'integer
) (eq class2
'rational
))))
1844 ;; If the float formats are specified and different, the types
1846 ((not (or (eq (numeric-type-format type1
) format2
)
1849 ;; Check the bounds.
1850 ((and (numeric-bound-test low1 low2
>= >)
1851 (numeric-bound-test high1 high2
<= <))
1856 (!define-superclasses number
((number)) !cold-init-forms
)
1858 ;;; If the high bound of LOW is adjacent to the low bound of HIGH,
1859 ;;; then return true, otherwise NIL.
1860 (defun numeric-types-adjacent (low high
)
1861 (let ((low-bound (numeric-type-high low
))
1862 (high-bound (numeric-type-low high
)))
1863 (cond ((not (and low-bound high-bound
)) nil
)
1864 ((and (consp low-bound
) (consp high-bound
)) nil
)
1866 (let ((low-value (car low-bound
)))
1867 (or (eql low-value high-bound
)
1869 (load-time-value (make-unportable-float
1870 :single-float-negative-zero
) t
))
1871 (eql high-bound
0f0
))
1872 (and (eql low-value
0f0
)
1874 (load-time-value (make-unportable-float
1875 :single-float-negative-zero
) t
)))
1877 (load-time-value (make-unportable-float
1878 :double-float-negative-zero
) t
))
1879 (eql high-bound
0d0
))
1880 (and (eql low-value
0d0
)
1882 (load-time-value (make-unportable-float
1883 :double-float-negative-zero
) t
))))))
1885 (let ((high-value (car high-bound
)))
1886 (or (eql high-value low-bound
)
1887 (and (eql high-value
1888 (load-time-value (make-unportable-float
1889 :single-float-negative-zero
) t
))
1890 (eql low-bound
0f0
))
1891 (and (eql high-value
0f0
)
1893 (load-time-value (make-unportable-float
1894 :single-float-negative-zero
) t
)))
1895 (and (eql high-value
1896 (load-time-value (make-unportable-float
1897 :double-float-negative-zero
) t
))
1898 (eql low-bound
0d0
))
1899 (and (eql high-value
0d0
)
1901 (load-time-value (make-unportable-float
1902 :double-float-negative-zero
) t
))))))
1903 ((and (eq (numeric-type-class low
) 'integer
)
1904 (eq (numeric-type-class high
) 'integer
))
1905 (eql (1+ low-bound
) high-bound
))
1909 ;;; Return a numeric type that is a supertype for both TYPE1 and TYPE2.
1911 ;;; Binding *APPROXIMATE-NUMERIC-UNIONS* to T allows merging non-adjacent
1912 ;;; numeric types, eg (OR (INTEGER 0 12) (INTEGER 20 128)) => (INTEGER 0 128),
1913 ;;; the compiler does this occasionally during type-derivation to avoid
1914 ;;; creating absurdly complex unions of numeric types.
1915 (defvar *approximate-numeric-unions
* nil
)
1917 (!define-type-method
(number :simple-union2
) (type1 type2
)
1918 (declare (type numeric-type type1 type2
))
1919 (cond ((csubtypep type1 type2
) type2
)
1920 ((csubtypep type2 type1
) type1
)
1922 (let ((class1 (numeric-type-class type1
))
1923 (format1 (numeric-type-format type1
))
1924 (complexp1 (numeric-type-complexp type1
))
1925 (class2 (numeric-type-class type2
))
1926 (format2 (numeric-type-format type2
))
1927 (complexp2 (numeric-type-complexp type2
)))
1929 ((and (eq class1 class2
)
1930 (eq format1 format2
)
1931 (eq complexp1 complexp2
)
1932 (or *approximate-numeric-unions
*
1933 (numeric-types-intersect type1 type2
)
1934 (numeric-types-adjacent type1 type2
)
1935 (numeric-types-adjacent type2 type1
)))
1940 :low
(numeric-bound-max (numeric-type-low type1
)
1941 (numeric-type-low type2
)
1943 :high
(numeric-bound-max (numeric-type-high type1
)
1944 (numeric-type-high type2
)
1946 ;; FIXME: These two clauses are almost identical, and the
1947 ;; consequents are in fact identical in every respect.
1948 ((and (eq class1
'rational
)
1949 (eq class2
'integer
)
1950 (eq format1 format2
)
1951 (eq complexp1 complexp2
)
1952 (integerp (numeric-type-low type2
))
1953 (integerp (numeric-type-high type2
))
1954 (= (numeric-type-low type2
) (numeric-type-high type2
))
1955 (or *approximate-numeric-unions
*
1956 (numeric-types-adjacent type1 type2
)
1957 (numeric-types-adjacent type2 type1
)))
1962 :low
(numeric-bound-max (numeric-type-low type1
)
1963 (numeric-type-low type2
)
1965 :high
(numeric-bound-max (numeric-type-high type1
)
1966 (numeric-type-high type2
)
1968 ((and (eq class1
'integer
)
1969 (eq class2
'rational
)
1970 (eq format1 format2
)
1971 (eq complexp1 complexp2
)
1972 (integerp (numeric-type-low type1
))
1973 (integerp (numeric-type-high type1
))
1974 (= (numeric-type-low type1
) (numeric-type-high type1
))
1975 (or *approximate-numeric-unions
*
1976 (numeric-types-adjacent type1 type2
)
1977 (numeric-types-adjacent type2 type1
)))
1982 :low
(numeric-bound-max (numeric-type-low type1
)
1983 (numeric-type-low type2
)
1985 :high
(numeric-bound-max (numeric-type-high type1
)
1986 (numeric-type-high type2
)
1991 (!cold-init-forms
;; is !PRECOMPUTE-TYPES not doing the right thing?
1992 (setf (info :type
:kind
'number
) :primitive
)
1993 (setf (info :type
:builtin
'number
)
1994 (make-numeric-type :complexp nil
)))
1996 (!def-type-translator complex
((:context context
) &optional
(typespec '*))
1997 (if (eq typespec
'*)
1998 (specifier-type '(complex real
))
1999 (labels ((not-numeric ()
2000 (error "The component type for COMPLEX is not numeric: ~S"
2003 (error "The component type for COMPLEX is not a subtype of REAL: ~S"
2005 (complex1 (component-type)
2006 (unless (numeric-type-p component-type
)
2008 (when (eq (numeric-type-complexp component-type
) :complex
)
2010 (if (csubtypep component-type
(specifier-type '(eql 0)))
2012 (modified-numeric-type component-type
2013 :complexp
:complex
)))
2016 ((eq ctype
*empty-type
*) *empty-type
*)
2017 ((eq ctype
*universal-type
*) (not-real))
2018 ((typep ctype
'numeric-type
) (complex1 ctype
))
2019 ((typep ctype
'union-type
)
2021 (mapcar #'do-complex
(union-type-types ctype
))))
2022 ((typep ctype
'member-type
)
2024 (mapcar-member-type-members
2025 (lambda (x) (do-complex (ctype-of x
)))
2027 ((and (typep ctype
'intersection-type
)
2028 ;; FIXME: This is very much a
2029 ;; not-quite-worst-effort, but we are required to do
2030 ;; something here because of our representation of
2031 ;; RATIO as (AND RATIONAL (NOT INTEGER)): we must
2032 ;; allow users to ask about (COMPLEX RATIO). This
2033 ;; will of course fail to work right on such types
2034 ;; as (AND INTEGER (SATISFIES ZEROP))...
2035 (let ((numbers (remove-if-not
2037 (intersection-type-types ctype
))))
2039 (null (cdr numbers
))
2040 (eq (numeric-type-complexp (car numbers
)) :real
)
2041 (complex1 (car numbers
))))))
2043 (multiple-value-bind (subtypep certainly
)
2044 (csubtypep ctype
(specifier-type 'real
))
2045 (if (and (not subtypep
) certainly
)
2047 ;; ANSI just says that TYPESPEC is any subtype of
2048 ;; type REAL, not necessarily a NUMERIC-TYPE. In
2049 ;; particular, at this point TYPESPEC could legally
2050 ;; be a hairy type like (AND NUMBER (SATISFIES
2051 ;; REALP) (SATISFIES ZEROP)), in which case we fall
2052 ;; through the logic above and end up here,
2054 ;; FIXME: (COMPLEX NUMBER) is not rejected but should
2055 ;; be, as NUMBER is clearly not a subtype of real.
2056 (bug "~@<(known bug #145): The type ~S is too hairy to be ~
2057 used for a COMPLEX component.~:@>"
2059 (let ((ctype (specifier-type-r context typespec
)))
2060 (do-complex ctype
)))))
2062 ;;; If X is *, return NIL, otherwise return the bound, which must be a
2063 ;;; member of TYPE or a one-element list of a member of TYPE.
2064 ;;; This is not necessarily the canonical bound. An integer bound
2065 ;;; should always be an atom, which we'll enforce later if needed.
2066 #!-sb-fluid
(declaim (inline valid-bound
))
2067 (defun valid-bound (bound type
)
2068 (cond ((eq bound
'*) nil
)
2069 ((sb!xc
:typep
(if (singleton-p bound
) (car bound
) bound
) type
) bound
)
2071 (error "Bound is not * or ~A ~S or list of one ~:*~S: ~S"
2072 (if (eq type
'integer
) "an" "a") type bound
))))
2074 (!def-type-translator integer
(&optional
(low '*) (high '*))
2075 (let ((lb (valid-bound low
'integer
))
2076 (hb (valid-bound high
'integer
)))
2077 (make-numeric-type :class
'integer
:complexp
:real
2078 :enumerable
(not (null (and lb hb
)))
2081 (defmacro !def-bounded-type
(type class format
)
2082 `(!def-type-translator
,type
(&optional
(low '*) (high '*))
2083 (let ((lb (valid-bound low
',type
))
2084 (hb (valid-bound high
',type
)))
2085 (make-numeric-type :class
',class
:format
',format
2086 :low lb
:high hb
))))
2088 (!def-bounded-type rational rational nil
)
2090 ;;; Unlike CMU CL, we represent the types FLOAT and REAL as
2091 ;;; UNION-TYPEs of more primitive types, in order to make
2092 ;;; type representation more unique, avoiding problems in the
2093 ;;; simplification of things like
2094 ;;; (subtypep '(or (single-float -1.0 1.0) (single-float 0.1))
2095 ;;; '(or (real -1 7) (single-float 0.1) (single-float -1.0 1.0)))
2096 ;;; When we allowed REAL to remain as a separate NUMERIC-TYPE,
2097 ;;; it was too easy for the first argument to be simplified to
2098 ;;; '(SINGLE-FLOAT -1.0), and for the second argument to be simplified
2099 ;;; to '(OR (REAL -1 7) (SINGLE-FLOAT 0.1)) and then for the
2100 ;;; SUBTYPEP to fail (returning NIL,T instead of T,T) because
2101 ;;; the first argument can't be seen to be a subtype of any of the
2102 ;;; terms in the second argument.
2104 ;;; The old CMU CL way was:
2105 ;;; (!def-bounded-type float float nil)
2106 ;;; (!def-bounded-type real nil nil)
2108 ;;; FIXME: If this new way works for a while with no weird new
2109 ;;; problems, we can go back and rip out support for separate FLOAT
2110 ;;; and REAL flavors of NUMERIC-TYPE. The new way was added in
2111 ;;; sbcl-0.6.11.22, 2001-03-21.
2113 ;;; FIXME: It's probably necessary to do something to fix the
2114 ;;; analogous problem with INTEGER and RATIONAL types. Perhaps
2115 ;;; bounded RATIONAL types should be represented as (OR RATIO INTEGER).
2116 (defun coerce-bound (bound type upperp inner-coerce-bound-fun
)
2117 (declare (type function inner-coerce-bound-fun
))
2120 (funcall inner-coerce-bound-fun bound type upperp
)))
2121 (defun inner-coerce-real-bound (bound type upperp
)
2122 #+sb-xc-host
(declare (ignore upperp
))
2123 (let #+sb-xc-host
()
2125 ((nl (load-time-value (symbol-value 'sb
!xc
:most-negative-long-float
) t
))
2126 (pl (load-time-value (symbol-value 'sb
!xc
:most-positive-long-float
) t
)))
2127 (let ((nbound (if (consp bound
) (car bound
) bound
))
2128 (consp (consp bound
)))
2132 (list (rational nbound
))
2136 ((floatp nbound
) bound
)
2138 ;; Coerce to the widest float format available, to avoid
2139 ;; unnecessary loss of precision, but don't coerce
2140 ;; unrepresentable numbers, except on the host where we
2141 ;; shouldn't be making these types (but KLUDGE: can't even
2142 ;; assert portably that we're not).
2146 (when (< nbound nl
) (return-from inner-coerce-real-bound nl
)))
2148 (when (> nbound pl
) (return-from inner-coerce-real-bound pl
))))
2149 (let ((result (coerce nbound
'long-float
)))
2150 (if consp
(list result
) result
)))))))))
2151 (defun inner-coerce-float-bound (bound type upperp
)
2152 #+sb-xc-host
(declare (ignore upperp
))
2153 (let #+sb-xc-host
()
2155 ((nd (load-time-value (symbol-value 'sb
!xc
:most-negative-double-float
) t
))
2156 (pd (load-time-value (symbol-value 'sb
!xc
:most-positive-double-float
) t
))
2157 (ns (load-time-value (symbol-value 'sb
!xc
:most-negative-single-float
) t
))
2158 (ps (load-time-value (symbol-value 'sb
!xc
:most-positive-single-float
) t
)))
2159 (let ((nbound (if (consp bound
) (car bound
) bound
))
2160 (consp (consp bound
)))
2164 ((typep nbound
'single-float
) bound
)
2169 (when (< nbound ns
) (return-from inner-coerce-float-bound ns
)))
2171 (when (> nbound ps
) (return-from inner-coerce-float-bound ps
))))
2172 (let ((result (coerce nbound
'single-float
)))
2173 (if consp
(list result
) result
)))))
2176 ((typep nbound
'double-float
) bound
)
2181 (when (< nbound nd
) (return-from inner-coerce-float-bound nd
)))
2183 (when (> nbound pd
) (return-from inner-coerce-float-bound pd
))))
2184 (let ((result (coerce nbound
'double-float
)))
2185 (if consp
(list result
) result
)))))))))
2186 (defun coerced-real-bound (bound type upperp
)
2187 (coerce-bound bound type upperp
#'inner-coerce-real-bound
))
2188 (defun coerced-float-bound (bound type upperp
)
2189 (coerce-bound bound type upperp
#'inner-coerce-float-bound
))
2190 (!def-type-translator real
(&optional
(low '*) (high '*))
2191 (specifier-type `(or (float ,(coerced-real-bound low
'float nil
)
2192 ,(coerced-real-bound high
'float t
))
2193 (rational ,(coerced-real-bound low
'rational nil
)
2194 ,(coerced-real-bound high
'rational t
)))))
2195 (!def-type-translator float
(&optional
(low '*) (high '*))
2197 `(or (single-float ,(coerced-float-bound low
'single-float nil
)
2198 ,(coerced-float-bound high
'single-float t
))
2199 (double-float ,(coerced-float-bound low
'double-float nil
)
2200 ,(coerced-float-bound high
'double-float t
))
2201 #!+long-float
,(error "stub: no long float support yet"))))
2203 (macrolet ((define-float-format (f) `(!def-bounded-type
,f float
,f
)))
2204 (define-float-format single-float
)
2205 (define-float-format double-float
))
2207 (defun numeric-types-intersect (type1 type2
)
2208 (declare (type numeric-type type1 type2
))
2209 (let* ((class1 (numeric-type-class type1
))
2210 (class2 (numeric-type-class type2
))
2211 (complexp1 (numeric-type-complexp type1
))
2212 (complexp2 (numeric-type-complexp type2
))
2213 (format1 (numeric-type-format type1
))
2214 (format2 (numeric-type-format type2
))
2215 (low1 (numeric-type-low type1
))
2216 (high1 (numeric-type-high type1
))
2217 (low2 (numeric-type-low type2
))
2218 (high2 (numeric-type-high type2
)))
2219 ;; If one is complex and the other isn't, then they are disjoint.
2220 (cond ((not (or (eq complexp1 complexp2
)
2221 (null complexp1
) (null complexp2
)))
2223 ;; If either type is a float, then the other must either be
2224 ;; specified to be a float or unspecified. Otherwise, they
2226 ((and (eq class1
'float
)
2227 (not (member class2
'(float nil
)))) nil
)
2228 ((and (eq class2
'float
)
2229 (not (member class1
'(float nil
)))) nil
)
2230 ;; If the float formats are specified and different, the
2231 ;; types are disjoint.
2232 ((not (or (eq format1 format2
) (null format1
) (null format2
)))
2235 ;; Check the bounds. This is a bit odd because we must
2236 ;; always have the outer bound of the interval as the
2238 (if (numeric-bound-test high1 high2
<= <)
2239 (or (and (numeric-bound-test low1 low2
>= >)
2240 (numeric-bound-test* low1 high2
<= <))
2241 (and (numeric-bound-test low2 low1
>= >)
2242 (numeric-bound-test* low2 high1
<= <)))
2243 (or (and (numeric-bound-test* low2 high1
<= <)
2244 (numeric-bound-test low2 low1
>= >))
2245 (and (numeric-bound-test high2 high1
<= <)
2246 (numeric-bound-test* high2 low1
>= >))))))))
2248 ;;; Take the numeric bound X and convert it into something that can be
2249 ;;; used as a bound in a numeric type with the specified CLASS and
2250 ;;; FORMAT. If UP-P is true, then we round up as needed, otherwise we
2251 ;;; round down. UP-P true implies that X is a lower bound, i.e. (N) > N.
2253 ;;; This is used by NUMERIC-TYPE-INTERSECTION to mash the bound into
2254 ;;; the appropriate type number. X may only be a float when CLASS is
2257 ;;; ### Note: it is possible for the coercion to a float to overflow
2258 ;;; or underflow. This happens when the bound doesn't fit in the
2259 ;;; specified format. In this case, we should really return the
2260 ;;; appropriate {Most | Least}-{Positive | Negative}-XXX-Float float
2261 ;;; of desired format. But these conditions aren't currently signalled
2262 ;;; in any useful way.
2264 ;;; Also, when converting an open rational bound into a float we
2265 ;;; should probably convert it to a closed bound of the closest float
2266 ;;; in the specified format. KLUDGE: In general, open float bounds are
2267 ;;; screwed up. -- (comment from original CMU CL)
2268 (defun round-numeric-bound (x class format up-p
)
2270 (let ((cx (if (consp x
) (car x
) x
)))
2274 (if (and (consp x
) (integerp cx
))
2275 (if up-p
(1+ cx
) (1- cx
))
2276 (if up-p
(ceiling cx
) (floor cx
))))
2280 ((and format
(subtypep format
'double-float
))
2281 (if (<= most-negative-double-float cx most-positive-double-float
)
2285 (if (<= most-negative-single-float cx most-positive-single-float
)
2287 (coerce cx
(or format
'single-float
))
2289 (if (consp x
) (list res
) res
)))))
2292 ;;; Handle the case of type intersection on two numeric types. We use
2293 ;;; TYPES-EQUAL-OR-INTERSECT to throw out the case of types with no
2294 ;;; intersection. If an attribute in TYPE1 is unspecified, then we use
2295 ;;; TYPE2's attribute, which must be at least as restrictive. If the
2296 ;;; types intersect, then the only attributes that can be specified
2297 ;;; and different are the class and the bounds.
2299 ;;; When the class differs, we use the more restrictive class. The
2300 ;;; only interesting case is RATIONAL/INTEGER, since RATIONAL includes
2303 ;;; We make the result lower (upper) bound the maximum (minimum) of
2304 ;;; the argument lower (upper) bounds. We convert the bounds into the
2305 ;;; appropriate numeric type before maximizing. This avoids possible
2306 ;;; confusion due to mixed-type comparisons (but I think the result is
2308 (!define-type-method
(number :simple-intersection2
) (type1 type2
)
2309 (declare (type numeric-type type1 type2
))
2310 (if (numeric-types-intersect type1 type2
)
2311 (let* ((class1 (numeric-type-class type1
))
2312 (class2 (numeric-type-class type2
))
2313 (class (ecase class1
2315 ((integer float
) class1
)
2316 (rational (if (eq class2
'integer
)
2319 (format (or (numeric-type-format type1
)
2320 (numeric-type-format type2
))))
2324 :complexp
(or (numeric-type-complexp type1
)
2325 (numeric-type-complexp type2
))
2326 :low
(numeric-bound-max
2327 (round-numeric-bound (numeric-type-low type1
)
2329 (round-numeric-bound (numeric-type-low type2
)
2332 :high
(numeric-bound-max
2333 (round-numeric-bound (numeric-type-high type1
)
2335 (round-numeric-bound (numeric-type-high type2
)
2340 ;;; Given two float formats, return the one with more precision. If
2341 ;;; either one is null, return NIL.
2342 (defun float-format-max (f1 f2
)
2344 (dolist (f *float-formats
* (error "bad float format: ~S" f1
))
2345 (when (or (eq f f1
) (eq f f2
))
2348 ;;; Return the result of an operation on TYPE1 and TYPE2 according to
2349 ;;; the rules of numeric contagion. This is always NUMBER, some float
2350 ;;; format (possibly complex) or RATIONAL. Due to rational
2351 ;;; canonicalization, there isn't much we can do here with integers or
2352 ;;; rational complex numbers.
2354 ;;; If either argument is not a NUMERIC-TYPE, then return NUMBER. This
2355 ;;; is useful mainly for allowing types that are technically numbers,
2356 ;;; but not a NUMERIC-TYPE.
2357 (defun numeric-contagion (type1 type2
)
2358 (if (and (numeric-type-p type1
) (numeric-type-p type2
))
2359 (let ((class1 (numeric-type-class type1
))
2360 (class2 (numeric-type-class type2
))
2361 (format1 (numeric-type-format type1
))
2362 (format2 (numeric-type-format type2
))
2363 (complexp1 (numeric-type-complexp type1
))
2364 (complexp2 (numeric-type-complexp type2
)))
2365 (cond ((or (null complexp1
)
2367 (specifier-type 'number
))
2371 :format
(ecase class2
2372 (float (float-format-max format1 format2
))
2373 ((integer rational
) format1
)
2375 ;; A double-float with any real number is a
2378 (if (eq format1
'double-float
)
2381 ;; A long-float with any real number is a
2384 (if (eq format1
'long-float
)
2387 :complexp
(if (or (eq complexp1
:complex
)
2388 (eq complexp2
:complex
))
2391 ((eq class2
'float
) (numeric-contagion type2 type1
))
2392 ((and (eq complexp1
:real
) (eq complexp2
:real
))
2394 :class
(and class1 class2
'rational
)
2397 (specifier-type 'number
))))
2398 (specifier-type 'number
)))
2402 (!define-type-method
(array :simple-
=) (type1 type2
)
2403 (cond ((not (and (equal (array-type-dimensions type1
)
2404 (array-type-dimensions type2
))
2405 (eq (array-type-complexp type1
)
2406 (array-type-complexp type2
))))
2408 ((or (unknown-type-p (array-type-element-type type1
))
2409 (unknown-type-p (array-type-element-type type2
)))
2410 (type= (array-type-element-type type1
)
2411 (array-type-element-type type2
)))
2413 (values (type= (array-type-specialized-element-type type1
)
2414 (array-type-specialized-element-type type2
))
2417 (!define-type-method
(array :negate
) (type)
2418 ;; FIXME (and hint to PFD): we're vulnerable here to attacks of the
2419 ;; form "are (AND ARRAY (NOT (ARRAY T))) and (OR (ARRAY BIT) (ARRAY
2420 ;; NIL) (ARRAY CHAR) ...) equivalent?" -- CSR, 2003-12-10
2421 ;; A symptom of the aforementioned is that the following are not TYPE=
2422 ;; (AND (VECTOR T) (NOT SIMPLE-ARRAY)) ; an ARRAY-TYPE
2423 ;; (AND (VECTOR T) (NOT SIMPLE-VECTOR)) ; an INTERSECTION-TYPE
2424 ;; even though (VECTOR T) makes it so that the (NOT) clause in each can
2425 ;; only provide one additional bit of information: that the vector
2426 ;; is complex as opposed to simple. The rank and element-type are fixed.
2427 (if (and (eq (array-type-dimensions type
) '*)
2428 (eq (array-type-complexp type
) 't
)
2429 (eq (array-type-element-type type
) *wild-type
*))
2430 ;; (NOT <hairy-array>) = either SIMPLE-ARRAY or (NOT ARRAY).
2431 ;; This is deliberately asymmetric - trying to say that NOT simple-array
2432 ;; equals hairy-array leads to infinite recursion.
2433 (type-union (make-array-type '* :complexp nil
2434 :element-type
*wild-type
*)
2436 (make-array-type '* :element-type
*wild-type
*)))
2437 (make-negation-type type
)))
2439 (!define-type-method
(array :unparse
) (type)
2440 (let* ((dims (array-type-dimensions type
))
2441 ;; Compare the specialised element type and the
2442 ;; derived element type. If the derived type
2443 ;; is so small that it jumps to a smaller upgraded
2444 ;; element type, use the specialised element type.
2446 ;; This protects from unparsing
2447 ;; (and (vector (or bit symbol))
2448 ;; (vector (or bit character)))
2449 ;; i.e., the intersection of two T array types,
2451 (stype (array-type-specialized-element-type type
))
2452 (dtype (array-type-element-type type
))
2453 (utype (%upgraded-array-element-type dtype
))
2454 (eltype (type-specifier (if (type= stype utype
)
2457 (complexp (array-type-complexp type
)))
2458 (if (and (eq complexp t
) (not *unparse-allow-negation
*))
2459 (setq complexp
:maybe
))
2463 ((t) '(and array
(not simple-array
)))
2465 ((nil) 'simple-array
))
2467 ((t) `(and (array ,eltype
) (not simple-array
)))
2468 ((:maybe
) `(array ,eltype
))
2469 ((nil) `(simple-array ,eltype
)))))
2470 ((= (length dims
) 1)
2473 (if (eq (car dims
) '*)
2476 ((base-char #!-sb-unicode character
) 'base-string
)
2478 (t `(vector ,eltype
)))
2480 (bit `(bit-vector ,(car dims
)))
2481 ((base-char #!-sb-unicode character
)
2482 `(base-string ,(car dims
)))
2483 (t `(vector ,eltype
,(car dims
)))))))
2484 (if (eql complexp
:maybe
)
2486 `(and ,answer
(not simple-array
))))
2487 (if (eq (car dims
) '*)
2489 (bit 'simple-bit-vector
)
2490 ((base-char #!-sb-unicode character
) 'simple-base-string
)
2491 ((t) 'simple-vector
)
2492 (t `(simple-array ,eltype
(*))))
2494 (bit `(simple-bit-vector ,(car dims
)))
2495 ((base-char #!-sb-unicode character
)
2496 `(simple-base-string ,(car dims
)))
2497 ((t) `(simple-vector ,(car dims
)))
2498 (t `(simple-array ,eltype
,dims
))))))
2501 ((t) `(and (array ,eltype
,dims
) (not simple-array
)))
2502 ((:maybe
) `(array ,eltype
,dims
))
2503 ((nil) `(simple-array ,eltype
,dims
)))))))
2505 (!define-type-method
(array :simple-subtypep
) (type1 type2
)
2506 (let ((dims1 (array-type-dimensions type1
))
2507 (dims2 (array-type-dimensions type2
))
2508 (complexp2 (array-type-complexp type2
)))
2509 (cond (;; not subtypep unless dimensions are compatible
2510 (not (or (eq dims2
'*)
2511 (and (not (eq dims1
'*))
2512 ;; (sbcl-0.6.4 has trouble figuring out that
2513 ;; DIMS1 and DIMS2 must be lists at this
2514 ;; point, and knowing that is important to
2515 ;; compiling EVERY efficiently.)
2516 (= (length (the list dims1
))
2517 (length (the list dims2
)))
2518 (every (lambda (x y
)
2519 (or (eq y
'*) (eql x y
)))
2521 (the list dims2
)))))
2523 ;; not subtypep unless complexness is compatible
2524 ((not (or (eq complexp2
:maybe
)
2525 (eq (array-type-complexp type1
) complexp2
)))
2527 ;; Since we didn't fail any of the tests above, we win
2528 ;; if the TYPE2 element type is wild.
2529 ((eq (array-type-element-type type2
) *wild-type
*)
2531 (;; Since we didn't match any of the special cases above, if
2532 ;; either element type is unknown we can only give a good
2533 ;; answer if they are the same.
2534 (or (unknown-type-p (array-type-element-type type1
))
2535 (unknown-type-p (array-type-element-type type2
)))
2536 (if (type= (array-type-element-type type1
)
2537 (array-type-element-type type2
))
2540 (;; Otherwise, the subtype relationship holds iff the
2541 ;; types are equal, and they're equal iff the specialized
2542 ;; element types are identical.
2544 (values (type= (array-type-specialized-element-type type1
)
2545 (array-type-specialized-element-type type2
))
2548 (!define-superclasses array
((vector vector
) (array)) !cold-init-forms
)
2550 (defun array-types-intersect (type1 type2
)
2551 (declare (type array-type type1 type2
))
2552 (let ((dims1 (array-type-dimensions type1
))
2553 (dims2 (array-type-dimensions type2
))
2554 (complexp1 (array-type-complexp type1
))
2555 (complexp2 (array-type-complexp type2
)))
2556 ;; See whether dimensions are compatible.
2557 (cond ((not (or (eq dims1
'*) (eq dims2
'*)
2558 (and (= (length dims1
) (length dims2
))
2559 (every (lambda (x y
)
2560 (or (eq x
'*) (eq y
'*) (= x y
)))
2563 ;; See whether complexpness is compatible.
2564 ((not (or (eq complexp1
:maybe
)
2565 (eq complexp2
:maybe
)
2566 (eq complexp1 complexp2
)))
2570 ;; If either element type is wild, then they intersect.
2571 ;; Otherwise, the types must be identical.
2573 ;; FIXME: There seems to have been a fair amount of
2574 ;; confusion about the distinction between requested element
2575 ;; type and specialized element type; here is one of
2576 ;; them. If we request an array to hold objects of an
2577 ;; unknown type, we can do no better than represent that
2578 ;; type as an array specialized on wild-type. We keep the
2579 ;; requested element-type in the -ELEMENT-TYPE slot, and
2580 ;; *WILD-TYPE* in the -SPECIALIZED-ELEMENT-TYPE. So, here,
2581 ;; we must test for the SPECIALIZED slot being *WILD-TYPE*,
2582 ;; not just the ELEMENT-TYPE slot. Maybe the return value
2583 ;; in that specific case should be T, NIL? Or maybe this
2584 ;; function should really be called
2585 ;; ARRAY-TYPES-COULD-POSSIBLY-INTERSECT? In any case, this
2586 ;; was responsible for bug #123, and this whole issue could
2587 ;; do with a rethink and/or a rewrite. -- CSR, 2002-08-21
2588 ((or (eq (array-type-specialized-element-type type1
) *wild-type
*)
2589 (eq (array-type-specialized-element-type type2
) *wild-type
*)
2590 (type= (array-type-specialized-element-type type1
)
2591 (array-type-specialized-element-type type2
)))
2597 (defun unite-array-types-complexp (type1 type2
)
2598 (let ((complexp1 (array-type-complexp type1
))
2599 (complexp2 (array-type-complexp type2
)))
2601 ((eq complexp1 complexp2
)
2602 ;; both types are the same complexp-ity
2603 (values complexp1 t
))
2604 ((eq complexp1
:maybe
)
2605 ;; type1 is wild-complexp
2606 (values :maybe type1
))
2607 ((eq complexp2
:maybe
)
2608 ;; type2 is wild-complexp
2609 (values :maybe type2
))
2611 ;; both types partition the complexp-space
2612 (values :maybe nil
)))))
2614 (defun unite-array-types-dimensions (type1 type2
)
2615 (let ((dims1 (array-type-dimensions type1
))
2616 (dims2 (array-type-dimensions type2
)))
2617 (cond ((equal dims1 dims2
)
2618 ;; both types are same dimensionality
2621 ;; type1 is wild-dimensions
2624 ;; type2 is wild-dimensions
2626 ((not (= (length dims1
) (length dims2
)))
2627 ;; types have different number of dimensions
2628 (values :incompatible nil
))
2630 ;; we need to check on a per-dimension basis
2631 (let* ((supertype1 t
)
2634 (result (mapcar (lambda (dim1 dim2
)
2639 (setf supertype2 nil
)
2642 (setf supertype1 nil
)
2645 (setf compatible nil
))))
2648 ((or (not compatible
)
2649 (and (not supertype1
)
2651 (values :incompatible nil
))
2652 ((and supertype1 supertype2
)
2653 (values result supertype1
))
2655 (values result
(if supertype1 type1 type2
)))))))))
2657 (defun unite-array-types-element-types (type1 type2
)
2658 ;; FIXME: We'd love to be able to unite the full set of specialized
2659 ;; array element types up to *wild-type*, but :simple-union2 is
2660 ;; performed pairwise, so we don't have a good hook for it and our
2661 ;; representation doesn't allow us to easily detect the situation
2663 ;; But see SIMPLIFY-ARRAY-UNIONS which is able to do something like that.
2664 (let* ((eltype1 (array-type-element-type type1
))
2665 (eltype2 (array-type-element-type type2
))
2666 (stype1 (array-type-specialized-element-type type1
))
2667 (stype2 (array-type-specialized-element-type type2
))
2668 (wild1 (eq eltype1
*wild-type
*))
2669 (wild2 (eq eltype2
*wild-type
*)))
2671 ((type= eltype1 eltype2
)
2672 (values eltype1 stype1 t
))
2674 (values eltype1 stype1 type1
))
2676 (values eltype2 stype2 type2
))
2677 ((not (type= stype1 stype2
))
2678 ;; non-wild types that don't share UAET don't unite
2679 (values :incompatible nil nil
))
2680 ((csubtypep eltype1 eltype2
)
2681 (values eltype2 stype2 type2
))
2682 ((csubtypep eltype2 eltype1
)
2683 (values eltype1 stype1 type1
))
2685 (values :incompatible nil nil
)))))
2687 (defun unite-array-types-supertypes-compatible-p (&rest supertypes
)
2688 ;; supertypes are compatible if they are all T, if there is a single
2689 ;; NIL and all the rest are T, or if all non-T supertypes are the
2690 ;; same and not NIL.
2691 (let ((interesting-supertypes
2692 (remove t supertypes
)))
2693 (or (not interesting-supertypes
)
2694 (equal interesting-supertypes
'(nil))
2695 ;; supertypes are (OR BOOLEAN ARRAY-TYPE), so...
2696 (typep (remove-duplicates interesting-supertypes
)
2697 '(cons array-type null
)))))
2699 (!define-type-method
(array :simple-union2
) (type1 type2
)
2700 (multiple-value-bind
2701 (result-eltype result-stype eltype-supertype
)
2702 (unite-array-types-element-types type1 type2
)
2703 (multiple-value-bind
2704 (result-complexp complexp-supertype
)
2705 (unite-array-types-complexp type1 type2
)
2706 (multiple-value-bind
2707 (result-dimensions dimensions-supertype
)
2708 (unite-array-types-dimensions type1 type2
)
2709 (when (and (not (eq result-dimensions
:incompatible
))
2710 (not (eq result-eltype
:incompatible
))
2711 (unite-array-types-supertypes-compatible-p
2712 eltype-supertype complexp-supertype dimensions-supertype
))
2713 (make-array-type result-dimensions
2714 :complexp result-complexp
2715 :element-type result-eltype
2716 :specialized-element-type result-stype
))))))
2718 (!define-type-method
(array :simple-intersection2
) (type1 type2
)
2719 (declare (type array-type type1 type2
))
2720 (if (array-types-intersect type1 type2
)
2721 (let ((dims1 (array-type-dimensions type1
))
2722 (dims2 (array-type-dimensions type2
))
2723 (complexp1 (array-type-complexp type1
))
2724 (complexp2 (array-type-complexp type2
))
2725 (eltype1 (array-type-element-type type1
))
2726 (eltype2 (array-type-element-type type2
))
2727 (stype1 (array-type-specialized-element-type type1
))
2728 (stype2 (array-type-specialized-element-type type2
)))
2729 (make-array-type (cond ((eq dims1
'*) dims2
)
2730 ((eq dims2
'*) dims1
)
2732 (mapcar (lambda (x y
) (if (eq x
'*) y x
))
2734 :complexp
(if (eq complexp1
:maybe
) complexp2 complexp1
)
2736 ((eq eltype1
*wild-type
*) eltype2
)
2737 ((eq eltype2
*wild-type
*) eltype1
)
2738 (t (type-intersection eltype1 eltype2
)))
2739 :specialized-element-type
(cond
2740 ((eq stype1
*wild-type
*) stype2
)
2741 ((eq stype2
*wild-type
*) stype1
)
2743 (aver (type= stype1 stype2
))
2747 ;;; Check a supplied dimension list to determine whether it is legal,
2748 ;;; and return it in canonical form (as either '* or a list).
2749 (defun canonical-array-dimensions (dims)
2754 (error "Arrays can't have a negative number of dimensions: ~S" dims
))
2755 (when (>= dims sb
!xc
:array-rank-limit
)
2756 (error "array type with too many dimensions: ~S" dims
))
2757 (make-list dims
:initial-element
'*))
2759 (when (>= (length dims
) sb
!xc
:array-rank-limit
)
2760 (error "array type with too many dimensions: ~S" dims
))
2763 (unless (and (integerp dim
)
2765 (< dim sb
!xc
:array-dimension-limit
))
2766 (error "bad dimension in array type: ~S" dim
))))
2769 (error "Array dimensions is not a list, integer or *:~% ~S" dims
))))
2773 (!define-type-method
(member :negate
) (type)
2774 (let ((xset (member-type-xset type
))
2775 (fp-zeroes (member-type-fp-zeroes type
)))
2777 ;; Hairy case, which needs to do a bit of float type
2778 ;; canonicalization.
2779 (apply #'type-intersection
2780 (if (xset-empty-p xset
)
2782 (make-negation-type (make-member-type xset nil
)))
2785 (let* ((opposite (neg-fp-zero x
))
2786 (type (ctype-of opposite
)))
2789 (modified-numeric-type type
:low nil
:high nil
))
2790 (modified-numeric-type type
:low nil
:high
(list opposite
))
2791 (make-eql-type opposite
)
2792 (modified-numeric-type type
:low
(list opposite
) :high nil
))))
2795 (make-negation-type type
))))
2797 (!define-type-method
(member :unparse
) (type)
2798 (cond ((eq type
(specifier-type 'null
)) 'null
) ; NULL type is EQ-comparable
2799 ((eq type
(specifier-type 'boolean
)) 'boolean
) ; so is BOOLEAN
2800 (t `(member ,@(member-type-members type
)))))
2802 (!define-type-method
(member :singleton-p
) (type)
2803 (if (eql 1 (member-type-size type
))
2804 (values t
(first (member-type-members type
)))
2807 (!define-type-method
(member :simple-subtypep
) (type1 type2
)
2808 (values (and (xset-subset-p (member-type-xset type1
)
2809 (member-type-xset type2
))
2810 (subsetp (member-type-fp-zeroes type1
)
2811 (member-type-fp-zeroes type2
)))
2814 (!define-type-method
(member :complex-subtypep-arg1
) (type1 type2
)
2816 (mapc-member-type-members
2818 (multiple-value-bind (ok surep
) (ctypep elt type2
)
2820 (return-from punt
(values nil nil
)))
2822 (return-from punt
(values nil t
)))))
2826 ;;; We punt if the odd type is enumerable and intersects with the
2827 ;;; MEMBER type. If not enumerable, then it is definitely not a
2828 ;;; subtype of the MEMBER type.
2829 (!define-type-method
(member :complex-subtypep-arg2
) (type1 type2
)
2830 (cond ((not (type-enumerable type1
)) (values nil t
))
2831 ((types-equal-or-intersect type1 type2
)
2832 (invoke-complex-subtypep-arg1-method type1 type2
))
2833 (t (values nil t
))))
2835 (!define-type-method
(member :simple-intersection2
) (type1 type2
)
2836 (make-member-type (xset-intersection (member-type-xset type1
)
2837 (member-type-xset type2
))
2838 (intersection (member-type-fp-zeroes type1
)
2839 (member-type-fp-zeroes type2
))))
2841 (!define-type-method
(member :complex-intersection2
) (type1 type2
)
2843 (let ((xset (alloc-xset))
2845 (mapc-member-type-members
2847 (multiple-value-bind (ok sure
) (ctypep member type1
)
2849 (return-from punt nil
))
2851 (if (fp-zero-p member
)
2852 (pushnew member fp-zeroes
)
2853 (add-to-xset member xset
)))))
2855 (if (and (xset-empty-p xset
) (not fp-zeroes
))
2857 (make-member-type xset fp-zeroes
)))))
2859 ;;; We don't need a :COMPLEX-UNION2, since the only interesting case is
2860 ;;; a union type, and the member/union interaction is handled by the
2861 ;;; union type method.
2862 (!define-type-method
(member :simple-union2
) (type1 type2
)
2863 (make-member-type (xset-union (member-type-xset type1
)
2864 (member-type-xset type2
))
2865 (union (member-type-fp-zeroes type1
)
2866 (member-type-fp-zeroes type2
))))
2868 (!define-type-method
(member :simple-
=) (type1 type2
)
2869 (let ((xset1 (member-type-xset type1
))
2870 (xset2 (member-type-xset type2
))
2871 (l1 (member-type-fp-zeroes type1
))
2872 (l2 (member-type-fp-zeroes type2
)))
2873 (values (and (eql (xset-count xset1
) (xset-count xset2
))
2874 (xset-subset-p xset1 xset2
)
2875 (xset-subset-p xset2 xset1
)
2880 (!define-type-method
(member :complex-
=) (type1 type2
)
2881 (if (type-enumerable type1
)
2882 (multiple-value-bind (val win
) (csubtypep type2 type1
)
2883 (if (or val
(not win
))
2888 (!def-type-translator member
:list
(&rest members
)
2890 (let (ms numbers char-codes
)
2891 (dolist (m (remove-duplicates members
))
2893 (character (push (sb!xc
:char-code m
) char-codes
))
2894 (real (if (and (floatp m
) (zerop m
))
2896 (push (ctype-of m
) numbers
)))
2899 (member-type-from-list ms
)
2900 (make-character-set-type (mapcar (lambda (x) (cons x x
))
2901 (sort char-codes
#'<)))
2902 (nreverse numbers
)))
2905 ;;;; intersection types
2907 ;;;; Until version 0.6.10.6, SBCL followed the original CMU CL approach
2908 ;;;; of punting on all AND types, not just the unreasonably complicated
2909 ;;;; ones. The change was motivated by trying to get the KEYWORD type
2910 ;;;; to behave sensibly:
2911 ;;;; ;; reasonable definition
2912 ;;;; (DEFTYPE KEYWORD () '(AND SYMBOL (SATISFIES KEYWORDP)))
2913 ;;;; ;; reasonable behavior
2914 ;;;; (AVER (SUBTYPEP 'KEYWORD 'SYMBOL))
2915 ;;;; Without understanding a little about the semantics of AND, we'd
2916 ;;;; get (SUBTYPEP 'KEYWORD 'SYMBOL)=>NIL,NIL and, for entirely
2917 ;;;; parallel reasons, (SUBTYPEP 'RATIO 'NUMBER)=>NIL,NIL. That's
2920 ;;;; We still follow the example of CMU CL to some extent, by punting
2921 ;;;; (to the opaque HAIRY-TYPE) on sufficiently complicated types
2924 (!define-type-class intersection
2925 :enumerable
#'compound-type-enumerable
2926 :might-contain-other-types t
)
2928 (!define-type-method
(intersection :negate
) (type)
2930 (mapcar #'type-negation
(intersection-type-types type
))))
2932 ;;; A few intersection types have special names. The others just get
2933 ;;; mechanically unparsed.
2934 (!define-type-method
(intersection :unparse
) (type)
2935 (declare (type ctype type
))
2936 (or (find type
'(ratio keyword compiled-function
) :key
#'specifier-type
:test
#'type
=)
2937 `(and ,@(mapcar #'type-specifier
(intersection-type-types type
)))))
2939 ;;; shared machinery for type equality: true if every type in the set
2940 ;;; TYPES1 matches a type in the set TYPES2 and vice versa
2941 (defun type=-set
(types1 types2
)
2942 (flet ((type<=-set
(x y
)
2943 (declare (type list x y
))
2944 (every/type
(lambda (x y-element
)
2945 (any/type
#'type
= y-element x
))
2947 (and/type
(type<=-set types1 types2
)
2948 (type<=-set types2 types1
))))
2950 ;;; Two intersection types are equal if their subtypes are equal sets.
2952 ;;; FIXME: Might it be better to use
2953 ;;; (AND (SUBTYPEP X Y) (SUBTYPEP Y X))
2954 ;;; instead, since SUBTYPEP is the usual relationship that we care
2955 ;;; most about, so it would be good to leverage any ingenuity there
2956 ;;; in this more obscure method?
2957 (!define-type-method
(intersection :simple-
=) (type1 type2
)
2958 (type=-set
(intersection-type-types type1
)
2959 (intersection-type-types type2
)))
2961 (defun %intersection-complex-subtypep-arg1
(type1 type2
)
2962 (type= type1
(type-intersection type1 type2
)))
2964 (defun %intersection-simple-subtypep
(type1 type2
)
2965 (every/type
#'%intersection-complex-subtypep-arg1
2967 (intersection-type-types type2
)))
2969 (!define-type-method
(intersection :simple-subtypep
) (type1 type2
)
2970 (%intersection-simple-subtypep type1 type2
))
2972 (!define-type-method
(intersection :complex-subtypep-arg1
) (type1 type2
)
2973 (%intersection-complex-subtypep-arg1 type1 type2
))
2975 (defun %intersection-complex-subtypep-arg2
(type1 type2
)
2976 (every/type
#'csubtypep type1
(intersection-type-types type2
)))
2978 (!define-type-method
(intersection :complex-subtypep-arg2
) (type1 type2
)
2979 (%intersection-complex-subtypep-arg2 type1 type2
))
2981 ;;; FIXME: This will look eeriely familiar to readers of the UNION
2982 ;;; :SIMPLE-INTERSECTION2 :COMPLEX-INTERSECTION2 method. That's
2983 ;;; because it was generated by cut'n'paste methods. Given that
2984 ;;; intersections and unions have all sorts of symmetries known to
2985 ;;; mathematics, it shouldn't be beyond the ken of some programmers to
2986 ;;; reflect those symmetries in code in a way that ties them together
2987 ;;; more strongly than having two independent near-copies :-/
2988 (!define-type-method
(intersection :simple-union2
:complex-union2
)
2990 ;; Within this method, type2 is guaranteed to be an intersection
2992 (aver (intersection-type-p type2
))
2993 ;; Make sure to call only the applicable methods...
2994 (cond ((and (intersection-type-p type1
)
2995 (%intersection-simple-subtypep type1 type2
)) type2
)
2996 ((and (intersection-type-p type1
)
2997 (%intersection-simple-subtypep type2 type1
)) type1
)
2998 ((and (not (intersection-type-p type1
))
2999 (%intersection-complex-subtypep-arg2 type1 type2
))
3001 ((and (not (intersection-type-p type1
))
3002 (%intersection-complex-subtypep-arg1 type2 type1
))
3004 ;; KLUDGE: This special (and somewhat hairy) magic is required
3005 ;; to deal with the RATIONAL/INTEGER special case. The UNION
3006 ;; of (INTEGER * -1) and (AND (RATIONAL * -1/2) (NOT INTEGER))
3007 ;; should be (RATIONAL * -1/2) -- CSR, 2003-02-28
3008 ((and (csubtypep type2
(specifier-type 'ratio
))
3009 (numeric-type-p type1
)
3010 (csubtypep type1
(specifier-type 'integer
))
3015 :low
(if (null (numeric-type-low type1
))
3017 (list (1- (numeric-type-low type1
))))
3018 :high
(if (null (numeric-type-high type1
))
3020 (list (1+ (numeric-type-high type1
)))))))
3021 (let* ((intersected (intersection-type-types type2
))
3022 (remaining (remove (specifier-type '(not integer
))
3025 (and (not (equal intersected remaining
))
3026 (type-union type1
(apply #'type-intersection remaining
)))))
3028 (let ((accumulator *universal-type
*))
3029 (do ((t2s (intersection-type-types type2
) (cdr t2s
)))
3030 ((null t2s
) accumulator
)
3031 (let ((union (type-union type1
(car t2s
))))
3032 (when (union-type-p union
)
3033 ;; we have to give up here -- there are all sorts of
3034 ;; ordering worries, but it's better than before.
3035 ;; Doing exactly the same as in the UNION
3036 ;; :SIMPLE/:COMPLEX-INTERSECTION2 method causes stack
3037 ;; overflow with the mutual recursion never bottoming
3039 (if (and (eq accumulator
*universal-type
*)
3041 ;; KLUDGE: if we get here, we have a partially
3042 ;; simplified result. While this isn't by any
3043 ;; means a universal simplification, including
3044 ;; this logic here means that we can get (OR
3045 ;; KEYWORD (NOT KEYWORD)) canonicalized to T.
3049 (type-intersection accumulator union
))))))))
3051 (!def-type-translator and
:list
((:context context
) &rest type-specifiers
)
3052 (apply #'type-intersection
3053 (mapcar (lambda (x) (specifier-type-r context x
))
3058 (!define-type-class union
3059 :enumerable
#'compound-type-enumerable
3060 :might-contain-other-types t
)
3062 (!define-type-method
(union :negate
) (type)
3063 (declare (type ctype type
))
3064 (apply #'type-intersection
3065 (mapcar #'type-negation
(union-type-types type
))))
3067 ;;; Unlike ARRAY-TYPE-DIMENSIONS this handles union types, which
3068 ;;; includes the type STRING.
3069 (defun ctype-array-dimensions (type)
3070 (labels ((process-compound-type (types)
3072 (dolist (type types
)
3073 (unless (or (hairy-type-p type
)
3074 (negation-type-p type
))
3075 (let ((current-dimensions (determine type
)))
3076 (cond ((eq current-dimensions
'*)
3077 (return-from ctype-array-dimensions
'*))
3079 (not (equal current-dimensions dimensions
)))
3080 (if (= (length dimensions
)
3081 (length current-dimensions
))
3083 (loop for dimension in dimensions
3084 for current-dimension in current-dimensions
3085 collect
(if (eql dimension current-dimension
)
3088 (return-from ctype-array-dimensions
'*)))
3091 (setf dimensions current-dimensions
))))))
3096 (array-type-dimensions type
))
3098 (process-compound-type (union-type-types type
)))
3100 (process-compound-type
3101 (mapcar #'ctype-of
(member-type-members type
))))
3103 (process-compound-type (intersection-type-types type
))))))
3106 (defun ctype-array-specialized-element-types (type)
3108 (labels ((process-compound-type (types)
3109 (loop for type in types
3110 unless
(or (hairy-type-p type
)
3111 (negation-type-p type
))
3112 do
(determine type
)))
3116 (when (eq (array-type-specialized-element-type type
) *wild-type
*)
3117 (return-from ctype-array-specialized-element-types
3119 (pushnew (array-type-specialized-element-type type
)
3120 types
:test
#'type
=))
3122 (process-compound-type (union-type-types type
)))
3124 (process-compound-type (intersection-type-types type
)))
3126 (process-compound-type
3127 (mapcar #'ctype-of
(member-type-members type
)))))))
3131 (defun unparse-string-type (ctype string-type
)
3132 (let ((string-ctype (specifier-type string-type
)))
3133 (and (union-type-p ctype
)
3134 (csubtypep ctype string-ctype
)
3135 (let ((types (copy-list (union-type-types string-ctype
))))
3136 (and (loop for type in
(union-type-types ctype
)
3137 for matching
= (and (array-type-p type
)
3141 do
(setf types
(delete matching types
)))
3143 (let ((dimensions (ctype-array-dimensions ctype
)))
3144 (cond ((and (singleton-p dimensions
)
3145 (integerp (car dimensions
)))
3146 `(,string-type
,@dimensions
)))))))
3148 ;;; The LIST, FLOAT and REAL types have special names. Other union
3149 ;;; types just get mechanically unparsed.
3150 (!define-type-method
(union :unparse
) (type)
3151 (declare (type ctype type
))
3153 ((type= type
(specifier-type 'list
)) 'list
)
3154 ((type= type
(specifier-type 'float
)) 'float
)
3155 ((type= type
(specifier-type 'real
)) 'real
)
3156 ((type= type
(specifier-type 'sequence
)) 'sequence
)
3157 ((type= type
(specifier-type 'bignum
)) 'bignum
)
3158 ((type= type
(specifier-type 'simple-string
)) 'simple-string
)
3159 ((type= type
(specifier-type 'string
)) 'string
)
3160 ((unparse-string-type type
'simple-string
))
3161 ((unparse-string-type type
'string
))
3162 ((type= type
(specifier-type 'complex
)) 'complex
)
3163 (t `(or ,@(mapcar #'type-specifier
(union-type-types type
))))))
3165 ;;; Two union types are equal if they are each subtypes of each
3166 ;;; other. We need to be this clever because our complex subtypep
3167 ;;; methods are now more accurate; we don't get infinite recursion
3168 ;;; because the simple-subtypep method delegates to complex-subtypep
3169 ;;; of the individual types of type1. - CSR, 2002-04-09
3171 ;;; Previous comment, now obsolete, but worth keeping around because
3172 ;;; it is true, though too strong a condition:
3174 ;;; Two union types are equal if their subtypes are equal sets.
3175 (!define-type-method
(union :simple-
=) (type1 type2
)
3176 (multiple-value-bind (subtype certain?
)
3177 (csubtypep type1 type2
)
3179 (csubtypep type2 type1
)
3180 ;; we might as well become as certain as possible.
3183 (multiple-value-bind (subtype certain?
)
3184 (csubtypep type2 type1
)
3185 (declare (ignore subtype
))
3186 (values nil certain?
))))))
3188 (!define-type-method
(union :complex-
=) (type1 type2
)
3189 (declare (ignore type1
))
3190 (if (some #'type-might-contain-other-types-p
3191 (union-type-types type2
))
3195 ;;; Similarly, a union type is a subtype of another if and only if
3196 ;;; every element of TYPE1 is a subtype of TYPE2.
3197 (defun union-simple-subtypep (type1 type2
)
3198 (every/type
(swapped-args-fun #'union-complex-subtypep-arg2
)
3200 (union-type-types type1
)))
3202 (!define-type-method
(union :simple-subtypep
) (type1 type2
)
3203 (union-simple-subtypep type1 type2
))
3205 (defun union-complex-subtypep-arg1 (type1 type2
)
3206 (every/type
(swapped-args-fun #'csubtypep
)
3208 (union-type-types type1
)))
3210 (!define-type-method
(union :complex-subtypep-arg1
) (type1 type2
)
3211 (union-complex-subtypep-arg1 type1 type2
))
3213 (defun union-complex-subtypep-arg2 (type1 type2
)
3214 ;; At this stage, we know that type2 is a union type and type1
3215 ;; isn't. We might as well check this, though:
3216 (aver (union-type-p type2
))
3217 (aver (not (union-type-p type1
)))
3218 ;; was: (any/type #'csubtypep type1 (union-type-types type2)), which
3219 ;; turns out to be too restrictive, causing bug 91.
3221 ;; the following reimplementation might look dodgy. It is dodgy. It
3222 ;; depends on the union :complex-= method not doing very much work
3223 ;; -- certainly, not using subtypep. Reasoning:
3225 ;; A is a subset of (B1 u B2)
3226 ;; <=> A n (B1 u B2) = A
3227 ;; <=> (A n B1) u (A n B2) = A
3229 ;; But, we have to be careful not to delegate this type= to
3230 ;; something that could invoke subtypep, which might get us back
3231 ;; here -> stack explosion. We therefore ensure that the second type
3232 ;; (which is the one that's dispatched on) is either a union type
3233 ;; (where we've ensured that the complex-= method will not call
3234 ;; subtypep) or something with no union types involved, in which
3235 ;; case we'll never come back here.
3237 ;; If we don't do this, then e.g.
3238 ;; (SUBTYPEP '(MEMBER 3) '(OR (SATISFIES FOO) (SATISFIES BAR)))
3239 ;; would loop infinitely, as the member :complex-= method is
3240 ;; implemented in terms of subtypep.
3242 ;; Ouch. - CSR, 2002-04-10
3243 (multiple-value-bind (sub-value sub-certain?
)
3246 (mapcar (lambda (x) (type-intersection type1 x
))
3247 (union-type-types type2
))))
3249 (values sub-value sub-certain?
)
3250 ;; The ANY/TYPE expression above is a sufficient condition for
3251 ;; subsetness, but not a necessary one, so we might get a more
3252 ;; certain answer by this CALL-NEXT-METHOD-ish step when the
3253 ;; ANY/TYPE expression is uncertain.
3254 (invoke-complex-subtypep-arg1-method type1 type2
))))
3256 (!define-type-method
(union :complex-subtypep-arg2
) (type1 type2
)
3257 (union-complex-subtypep-arg2 type1 type2
))
3259 (!define-type-method
(union :simple-intersection2
:complex-intersection2
)
3261 ;; The CSUBTYPEP clauses here let us simplify e.g.
3262 ;; (TYPE-INTERSECTION2 (SPECIFIER-TYPE 'LIST)
3263 ;; (SPECIFIER-TYPE '(OR LIST VECTOR)))
3264 ;; (where LIST is (OR CONS NULL)).
3266 ;; The tests are more or less (CSUBTYPEP TYPE1 TYPE2) and vice
3267 ;; versa, but it's important that we pre-expand them into
3268 ;; specialized operations on individual elements of
3269 ;; UNION-TYPE-TYPES, instead of using the ordinary call to
3270 ;; CSUBTYPEP, in order to avoid possibly invoking any methods which
3271 ;; might in turn invoke (TYPE-INTERSECTION2 TYPE1 TYPE2) and thus
3272 ;; cause infinite recursion.
3274 ;; Within this method, type2 is guaranteed to be a union type:
3275 (aver (union-type-p type2
))
3276 ;; Make sure to call only the applicable methods...
3277 (cond ((and (union-type-p type1
)
3278 (union-simple-subtypep type1 type2
)) type1
)
3279 ((and (union-type-p type1
)
3280 (union-simple-subtypep type2 type1
)) type2
)
3281 ((and (not (union-type-p type1
))
3282 (union-complex-subtypep-arg2 type1 type2
))
3284 ((and (not (union-type-p type1
))
3285 (union-complex-subtypep-arg1 type2 type1
))
3288 ;; KLUDGE: This code accumulates a sequence of TYPE-UNION2
3289 ;; operations in a particular order, and gives up if any of
3290 ;; the sub-unions turn out not to be simple. In other cases
3291 ;; ca. sbcl-0.6.11.15, that approach to taking a union was a
3292 ;; bad idea, since it can overlook simplifications which
3293 ;; might occur if the terms were accumulated in a different
3294 ;; order. It's possible that that will be a problem here too.
3295 ;; However, I can't think of a good example to demonstrate
3296 ;; it, and without an example to demonstrate it I can't write
3297 ;; test cases, and without test cases I don't want to
3298 ;; complicate the code to address what's still a hypothetical
3299 ;; problem. So I punted. -- WHN 2001-03-20
3300 (let ((accumulator *empty-type
*))
3301 (dolist (t2 (union-type-types type2
) accumulator
)
3303 (type-union accumulator
3304 (type-intersection type1 t2
))))))))
3306 (!def-type-translator or
:list
((:context context
) &rest type-specifiers
)
3307 (let ((type (apply #'type-union
3308 (mapcar (lambda (x) (specifier-type-r context x
))
3310 (if (union-type-p type
)
3311 (sb!kernel
::simplify-array-unions type
)
3316 (!def-type-translator cons
((:context context
)
3317 &optional
(car-type-spec '*) (cdr-type-spec '*))
3318 (let ((car-type (single-value-specifier-type-r context car-type-spec
))
3319 (cdr-type (single-value-specifier-type-r context cdr-type-spec
)))
3320 (make-cons-type car-type cdr-type
)))
3322 (!define-type-method
(cons :negate
) (type)
3323 (if (and (eq (cons-type-car-type type
) *universal-type
*)
3324 (eq (cons-type-cdr-type type
) *universal-type
*))
3325 (make-negation-type type
)
3327 (make-negation-type (specifier-type 'cons
))
3329 ((and (not (eq (cons-type-car-type type
) *universal-type
*))
3330 (not (eq (cons-type-cdr-type type
) *universal-type
*)))
3333 (type-negation (cons-type-car-type type
))
3337 (type-negation (cons-type-cdr-type type
)))))
3338 ((not (eq (cons-type-car-type type
) *universal-type
*))
3340 (type-negation (cons-type-car-type type
))
3342 ((not (eq (cons-type-cdr-type type
) *universal-type
*))
3345 (type-negation (cons-type-cdr-type type
))))
3346 (t (bug "Weird CONS type ~S" type
))))))
3348 (!define-type-method
(cons :unparse
) (type)
3349 (if (eq type
(specifier-type 'cons
))
3351 `(cons ,(type-specifier (cons-type-car-type type
))
3352 ,(type-specifier (cons-type-cdr-type type
)))))
3354 (!define-type-method
(cons :simple-
=) (type1 type2
)
3355 (declare (type cons-type type1 type2
))
3356 (multiple-value-bind (car-match car-win
)
3357 (type= (cons-type-car-type type1
) (cons-type-car-type type2
))
3358 (multiple-value-bind (cdr-match cdr-win
)
3359 (type= (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3360 (cond ((and car-match cdr-match
)
3361 (aver (and car-win cdr-win
))
3365 ;; FIXME: Ideally we would like to detect and handle
3366 ;; (CONS UNKNOWN INTEGER) (CONS UNKNOWN SYMBOL) => NIL, T
3367 ;; but just returning a secondary true on (and car-win cdr-win)
3368 ;; unfortunately breaks other things. --NS 2006-08-16
3369 (and (or (and (not car-match
) car-win
)
3370 (and (not cdr-match
) cdr-win
))
3371 (not (and (cons-type-might-be-empty-type type1
)
3372 (cons-type-might-be-empty-type type2
))))))))))
3374 (!define-type-method
(cons :simple-subtypep
) (type1 type2
)
3375 (declare (type cons-type type1 type2
))
3376 (multiple-value-bind (val-car win-car
)
3377 (csubtypep (cons-type-car-type type1
) (cons-type-car-type type2
))
3378 (multiple-value-bind (val-cdr win-cdr
)
3379 (csubtypep (cons-type-cdr-type type1
) (cons-type-cdr-type type2
))
3380 (if (and val-car val-cdr
)
3381 (values t
(and win-car win-cdr
))
3382 (values nil
(or (and (not val-car
) win-car
)
3383 (and (not val-cdr
) win-cdr
)))))))
3385 ;;; Give up if a precise type is not possible, to avoid returning
3386 ;;; overly general types.
3387 (!define-type-method
(cons :simple-union2
) (type1 type2
)
3388 (declare (type cons-type type1 type2
))
3389 (let ((car-type1 (cons-type-car-type type1
))
3390 (car-type2 (cons-type-car-type type2
))
3391 (cdr-type1 (cons-type-cdr-type type1
))
3392 (cdr-type2 (cons-type-cdr-type type2
))
3395 ;; UGH. -- CSR, 2003-02-24
3396 (macrolet ((frob-car (car1 car2 cdr1 cdr2
3397 &optional
(not1 nil not1p
))
3399 (make-cons-type ,car1
(type-union ,cdr1
,cdr2
))
3401 (type-intersection ,car2
3404 `(type-negation ,car1
)))
3406 (cond ((type= car-type1 car-type2
)
3407 (make-cons-type car-type1
3408 (type-union cdr-type1 cdr-type2
)))
3409 ((type= cdr-type1 cdr-type2
)
3410 (make-cons-type (type-union car-type1 car-type2
)
3412 ((csubtypep car-type1 car-type2
)
3413 (frob-car car-type1 car-type2 cdr-type1 cdr-type2
))
3414 ((csubtypep car-type2 car-type1
)
3415 (frob-car car-type2 car-type1 cdr-type2 cdr-type1
))
3416 ;; more general case of the above, but harder to compute
3418 (setf car-not1
(type-negation car-type1
))
3419 (multiple-value-bind (yes win
)
3420 (csubtypep car-type2 car-not1
)
3421 (and (not yes
) win
)))
3422 (frob-car car-type1 car-type2 cdr-type1 cdr-type2 car-not1
))
3424 (setf car-not2
(type-negation car-type2
))
3425 (multiple-value-bind (yes win
)
3426 (csubtypep car-type1 car-not2
)
3427 (and (not yes
) win
)))
3428 (frob-car car-type2 car-type1 cdr-type2 cdr-type1 car-not2
))
3429 ;; Don't put these in -- consider the effect of taking the
3430 ;; union of (CONS (INTEGER 0 2) (INTEGER 5 7)) and
3431 ;; (CONS (INTEGER 0 3) (INTEGER 5 6)).
3433 ((csubtypep cdr-type1 cdr-type2
)
3434 (frob-cdr car-type1 car-type2 cdr-type1 cdr-type2
))
3436 ((csubtypep cdr-type2 cdr-type1
)
3437 (frob-cdr car-type2 car-type1 cdr-type2 cdr-type1
))))))
3439 (!define-type-method
(cons :simple-intersection2
) (type1 type2
)
3440 (declare (type cons-type type1 type2
))
3441 (let ((car-int2 (type-intersection2 (cons-type-car-type type1
)
3442 (cons-type-car-type type2
)))
3443 (cdr-int2 (type-intersection2 (cons-type-cdr-type type1
)
3444 (cons-type-cdr-type type2
))))
3446 ((and car-int2 cdr-int2
) (make-cons-type car-int2 cdr-int2
))
3447 (car-int2 (make-cons-type car-int2
3449 (cons-type-cdr-type type1
)
3450 (cons-type-cdr-type type2
))))
3451 (cdr-int2 (make-cons-type
3452 (type-intersection (cons-type-car-type type1
)
3453 (cons-type-car-type type2
))
3456 (!define-superclasses cons
((cons)) !cold-init-forms
)
3458 ;;;; CHARACTER-SET types
3460 (!def-type-translator character-set
3461 (&optional
(pairs '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))
3462 (make-character-set-type pairs
))
3464 (!define-type-method
(character-set :negate
) (type)
3465 (let ((pairs (character-set-type-pairs type
)))
3466 (if (and (= (length pairs
) 1)
3468 (= (cdar pairs
) (1- sb
!xc
:char-code-limit
)))
3469 (make-negation-type type
)
3470 (let ((not-character
3472 (make-character-set-type
3473 '((0 .
#.
(1- sb
!xc
:char-code-limit
)))))))
3476 (make-character-set-type
3478 (when (> (caar pairs
) 0)
3479 (push (cons 0 (1- (caar pairs
))) not-pairs
))
3480 (do* ((tail pairs
(cdr tail
))
3481 (high1 (cdar tail
) (cdar tail
))
3482 (low2 (caadr tail
) (caadr tail
)))
3484 (when (< (cdar tail
) (1- sb
!xc
:char-code-limit
))
3485 (push (cons (1+ (cdar tail
))
3486 (1- sb
!xc
:char-code-limit
))
3488 (nreverse not-pairs
))
3489 (push (cons (1+ high1
) (1- low2
)) not-pairs
)))))))))
3491 (!define-type-method
(character-set :unparse
) (type)
3493 ((eq type
(specifier-type 'character
)) 'character
)
3494 ((eq type
(specifier-type 'base-char
)) 'base-char
)
3495 ((eq type
(specifier-type 'extended-char
)) 'extended-char
)
3496 ;; standard-char is not an interned type
3497 ((type= type
(specifier-type 'standard-char
)) 'standard-char
)
3499 ;; Unparse into either MEMBER or CHARACTER-SET. We use MEMBER if there
3500 ;; are at most as many characters as there are character code ranges.
3501 ;; (basically saying to use MEMBER if each range is one character)
3502 (let* ((pairs (character-set-type-pairs type
))
3503 (count (length pairs
))
3504 (chars (loop named outer
3505 for
(low . high
) in pairs
3506 nconc
(loop for code from low upto high
3507 collect
(sb!xc
:code-char code
)
3508 when
(minusp (decf count
))
3509 do
(return-from outer t
)))))
3511 `(character-set ,pairs
)
3512 `(member ,@chars
))))))
3514 (!define-type-method
(character-set :singleton-p
) (type)
3515 (let* ((pairs (character-set-type-pairs type
))
3516 (pair (first pairs
)))
3517 (if (and (typep pairs
'(cons t null
))
3518 (eql (car pair
) (cdr pair
)))
3519 (values t
(code-char (car pair
)))
3522 (!define-type-method
(character-set :simple-
=) (type1 type2
)
3523 (let ((pairs1 (character-set-type-pairs type1
))
3524 (pairs2 (character-set-type-pairs type2
)))
3525 (values (equal pairs1 pairs2
) t
)))
3527 (!define-type-method
(character-set :simple-subtypep
) (type1 type2
)
3529 (dolist (pair (character-set-type-pairs type1
) t
)
3530 (unless (position pair
(character-set-type-pairs type2
)
3531 :test
(lambda (x y
) (and (>= (car x
) (car y
))
3532 (<= (cdr x
) (cdr y
)))))
3536 (!define-type-method
(character-set :simple-union2
) (type1 type2
)
3537 ;; KLUDGE: the canonizing in the MAKE-CHARACTER-SET-TYPE function
3538 ;; actually does the union for us. It might be a little fragile to
3540 (make-character-set-type
3542 (copy-alist (character-set-type-pairs type1
))
3543 (copy-alist (character-set-type-pairs type2
))
3546 (!define-type-method
(character-set :simple-intersection2
) (type1 type2
)
3547 ;; KLUDGE: brute force.
3550 (dolist (pair1 (character-set-type-pairs type1
)
3551 (make-character-set-type
3552 (sort pairs
#'< :key
#'car
)))
3553 (dolist (pair2 (character-set-type-pairs type2
))
3555 ((<= (car pair1
) (car pair2
) (cdr pair1
))
3556 (push (cons (car pair2
) (min (cdr pair1
) (cdr pair2
))) pairs
))
3557 ((<= (car pair2
) (car pair1
) (cdr pair2
))
3558 (push (cons (car pair1
) (min (cdr pair1
) (cdr pair2
))) pairs
))))))
3560 (make-character-set-type
3561 (intersect-type-pairs
3562 (character-set-type-pairs type1
)
3563 (character-set-type-pairs type2
))))
3566 ;;; Intersect two ordered lists of pairs
3567 ;;; Each list is of the form ((start1 . end1) ... (startn . endn)),
3568 ;;; where start1 <= end1 < start2 <= end2 < ... < startn <= endn.
3569 ;;; Each pair represents the integer interval start..end.
3571 (defun intersect-type-pairs (alist1 alist2
)
3572 (if (and alist1 alist2
)
3574 (pair1 (pop alist1
))
3575 (pair2 (pop alist2
)))
3577 (when (> (car pair1
) (car pair2
))
3578 (rotatef pair1 pair2
)
3579 (rotatef alist1 alist2
))
3580 (let ((pair1-cdr (cdr pair1
)))
3582 ((> (car pair2
) pair1-cdr
)
3583 ;; No over lap -- discard pair1
3584 (unless alist1
(return))
3585 (setq pair1
(pop alist1
)))
3586 ((<= (cdr pair2
) pair1-cdr
)
3587 (push (cons (car pair2
) (cdr pair2
)) res
)
3589 ((= (cdr pair2
) pair1-cdr
)
3590 (unless alist1
(return))
3591 (unless alist2
(return))
3592 (setq pair1
(pop alist1
)
3593 pair2
(pop alist2
)))
3594 (t ;; (< (cdr pair2) pair1-cdr)
3595 (unless alist2
(return))
3596 (setq pair1
(cons (1+ (cdr pair2
)) pair1-cdr
))
3597 (setq pair2
(pop alist2
)))))
3598 (t ;; (> (cdr pair2) (cdr pair1))
3599 (push (cons (car pair2
) pair1-cdr
) res
)
3600 (unless alist1
(return))
3601 (setq pair2
(cons (1+ pair1-cdr
) (cdr pair2
)))
3602 (setq pair1
(pop alist1
))))))
3607 ;;; Return the type that describes all objects that are in X but not
3608 ;;; in Y. If we can't determine this type, then return NIL.
3610 ;;; For now, we only are clever dealing with union and member types.
3611 ;;; If either type is not a union type, then we pretend that it is a
3612 ;;; union of just one type. What we do is remove from X all the types
3613 ;;; that are a subtype any type in Y. If any type in X intersects with
3614 ;;; a type in Y but is not a subtype, then we give up.
3616 ;;; We must also special-case any member type that appears in the
3617 ;;; union. We remove from X's members all objects that are TYPEP to Y.
3618 ;;; If Y has any members, we must be careful that none of those
3619 ;;; members are CTYPEP to any of Y's non-member types. We give up in
3620 ;;; this case, since to compute that difference we would have to break
3621 ;;; the type from X into some collection of types that represents the
3622 ;;; type without that particular element. This seems too hairy to be
3623 ;;; worthwhile, given its low utility.
3624 (defun type-difference (x y
)
3625 (if (and (numeric-type-p x
) (numeric-type-p y
))
3626 ;; Numeric types are easy. Are there any others we should handle like this?
3627 (type-intersection x
(type-negation y
))
3628 (let ((x-types (if (union-type-p x
) (union-type-types x
) (list x
)))
3629 (y-types (if (union-type-p y
) (union-type-types y
) (list y
))))
3631 (dolist (x-type x-types
)
3632 (if (member-type-p x-type
)
3633 (let ((xset (alloc-xset))
3635 (mapc-member-type-members
3637 (multiple-value-bind (ok sure
) (ctypep elt y
)
3639 (return-from type-difference nil
))
3642 (pushnew elt fp-zeroes
)
3643 (add-to-xset elt xset
)))))
3645 (unless (and (xset-empty-p xset
) (not fp-zeroes
))
3646 (res (make-member-type xset fp-zeroes
))))
3647 (dolist (y-type y-types
(res x-type
))
3648 (multiple-value-bind (val win
) (csubtypep x-type y-type
)
3649 (unless win
(return-from type-difference nil
))
3651 (when (types-equal-or-intersect x-type y-type
)
3652 (return-from type-difference nil
))))))
3653 (let ((y-mem (find-if #'member-type-p y-types
)))
3655 (dolist (x-type x-types
)
3656 (unless (member-type-p x-type
)
3657 (mapc-member-type-members
3659 (multiple-value-bind (ok sure
) (ctypep member x-type
)
3660 (when (or (not sure
) ok
)
3661 (return-from type-difference nil
))))
3663 (apply #'type-union
(res))))))
3665 (!def-type-translator array
((:context context
)
3666 &optional
(element-type '*)
3668 (let ((eltype (if (eq element-type
'*)
3670 (specifier-type-r context element-type
))))
3671 (make-array-type (canonical-array-dimensions dimensions
)
3673 :element-type eltype
3674 :specialized-element-type
(%upgraded-array-element-type
3677 (!def-type-translator simple-array
((:context context
)
3678 &optional
(element-type '*)
3680 (let ((eltype (if (eq element-type
'*)
3682 (specifier-type-r context element-type
))))
3683 (make-array-type (canonical-array-dimensions dimensions
)
3685 :element-type eltype
3686 :specialized-element-type
(%upgraded-array-element-type
3689 ;;;; SIMD-PACK types
3692 (!define-type-class simd-pack
:enumerable nil
3693 :might-contain-other-types nil
)
3695 ;; Though this involves a recursive call to parser, parsing context need not
3696 ;; be passed down, because an unknown-type condition is an immediate failure.
3697 (!def-type-translator simd-pack
(&optional
(element-type-spec '*))
3698 (if (eql element-type-spec
'*)
3699 (%make-simd-pack-type
*simd-pack-element-types
*)
3700 (make-simd-pack-type (single-value-specifier-type element-type-spec
))))
3702 (!define-type-method
(simd-pack :negate
) (type)
3703 (let ((remaining (set-difference *simd-pack-element-types
*
3704 (simd-pack-type-element-type type
)))
3705 (not-simd-pack (make-negation-type (specifier-type 'simd-pack
))))
3707 (type-union not-simd-pack
(%make-simd-pack-type remaining
))
3710 (!define-type-method
(simd-pack :unparse
) (type)
3711 (let ((eltypes (simd-pack-type-element-type type
)))
3712 (cond ((equal eltypes
*simd-pack-element-types
*)
3714 ((= 1 (length eltypes
))
3715 `(simd-pack ,(first eltypes
)))
3717 `(or ,@(mapcar (lambda (eltype)
3718 `(simd-pack ,eltype
))
3721 (!define-type-method
(simd-pack :simple-
=) (type1 type2
)
3722 (declare (type simd-pack-type type1 type2
))
3723 (null (set-exclusive-or (simd-pack-type-element-type type1
)
3724 (simd-pack-type-element-type type2
))))
3726 (!define-type-method
(simd-pack :simple-subtypep
) (type1 type2
)
3727 (declare (type simd-pack-type type1 type2
))
3728 (subsetp (simd-pack-type-element-type type1
)
3729 (simd-pack-type-element-type type2
)))
3731 (!define-type-method
(simd-pack :simple-union2
) (type1 type2
)
3732 (declare (type simd-pack-type type1 type2
))
3733 (%make-simd-pack-type
(union (simd-pack-type-element-type type1
)
3734 (simd-pack-type-element-type type2
))))
3736 (!define-type-method
(simd-pack :simple-intersection2
) (type1 type2
)
3737 (declare (type simd-pack-type type1 type2
))
3738 (let ((intersection (intersection (simd-pack-type-element-type type1
)
3739 (simd-pack-type-element-type type2
))))
3741 (%make-simd-pack-type intersection
)
3744 (!define-superclasses simd-pack
((simd-pack)) !cold-init-forms
))
3746 ;;;; utilities shared between cross-compiler and target system
3748 ;;; Does the type derived from compilation of an actual function
3749 ;;; definition satisfy declarations of a function's type?
3750 (defun defined-ftype-matches-declared-ftype-p (defined-ftype declared-ftype
)
3751 (declare (type ctype defined-ftype declared-ftype
))
3752 (flet ((is-built-in-class-function-p (ctype)
3753 (and (built-in-classoid-p ctype
)
3754 (eq (built-in-classoid-name ctype
) 'function
))))
3755 (cond (;; DECLARED-FTYPE could certainly be #<BUILT-IN-CLASS FUNCTION>;
3756 ;; that's what happens when we (DECLAIM (FTYPE FUNCTION FOO)).
3757 (is-built-in-class-function-p declared-ftype
)
3758 ;; In that case, any definition satisfies the declaration.
3760 (;; It's not clear whether or how DEFINED-FTYPE might be
3761 ;; #<BUILT-IN-CLASS FUNCTION>, but it's not obviously
3762 ;; invalid, so let's handle that case too, just in case.
3763 (is-built-in-class-function-p defined-ftype
)
3764 ;; No matter what DECLARED-FTYPE might be, we can't prove
3765 ;; that an object of type FUNCTION doesn't satisfy it, so
3766 ;; we return success no matter what.
3768 (;; Otherwise both of them must be FUN-TYPE objects.
3770 ;; FIXME: For now we only check compatibility of the return
3771 ;; type, not argument types, and we don't even check the
3772 ;; return type very precisely (as per bug 94a). It would be
3773 ;; good to do a better job. Perhaps to check the
3774 ;; compatibility of the arguments, we should (1) redo
3775 ;; VALUES-TYPES-EQUAL-OR-INTERSECT as
3776 ;; ARGS-TYPES-EQUAL-OR-INTERSECT, and then (2) apply it to
3777 ;; the ARGS-TYPE slices of the FUN-TYPEs. (ARGS-TYPE
3778 ;; is a base class both of VALUES-TYPE and of FUN-TYPE.)
3779 (values-types-equal-or-intersect
3780 (fun-type-returns defined-ftype
)
3781 (fun-type-returns declared-ftype
))))))
3783 ;;; This messy case of CTYPE for NUMBER is shared between the
3784 ;;; cross-compiler and the target system.
3785 (defun ctype-of-number (x)
3786 (let ((num (if (complexp x
) (realpart x
) x
)))
3787 (multiple-value-bind (complexp low high
)
3789 (let ((imag (imagpart x
)))
3790 (values :complex
(min num imag
) (max num imag
)))
3791 (values :real num num
))
3792 (make-numeric-type :class
(etypecase num
3793 (integer (if (complexp x
)
3794 (if (integerp (imagpart x
))
3798 (rational 'rational
)
3800 :format
(and (floatp num
) (float-format-name num
))
3805 ;;; The following function is a generic driver for approximating
3806 ;;; set-valued functions over types. Putting this here because it'll
3807 ;;; probably be useful for a lot of type analyses.
3809 ;;; Let f be a function from values of type X to Y, e.g., ARRAY-RANK.
3811 ;;; We compute an over or under-approximation of the set
3813 ;;; F(TYPE) = { f(x) : x in TYPE /\ x in X } \subseteq Y
3815 ;;; via set-valued approximations of f, OVER and UNDER.
3817 ;;; These functions must have the property that
3818 ;;; Forall TYPE, OVER(TYPE) \superseteq F(TYPE) and
3819 ;;; Forall TYPE, UNDER(TYPE) \subseteq F(TYPE)
3821 ;;; The driver is also parameterised over the finite set
3824 ;;; Union, intersection and difference are binary functions to compute
3825 ;;; set union, intersection and difference. Top and bottom are the
3826 ;;; concrete representations for the universe and empty sets; we never
3827 ;;; call the set functions on top or bottom, so it's safe to use
3828 ;;; special values there.
3832 ;;; TYPE: the ctype for which we wish to approximate F(TYPE)
3833 ;;; OVERAPPROXIMATE: true if we wish to overapproximate, nil otherwise.
3834 ;;; You usually want T.
3835 ;;; UNION/INTERSECTION/DIFFERENCE: implementations of finite set operations.
3836 ;;; Conform to cl::(union/intersection/set-difference). Passing NIL will
3837 ;;; disable some cleverness and result in quicker computation of coarser
3838 ;;; approximations. However, passing difference without union and intersection
3839 ;;; will probably not end well.
3840 ;;; TOP/BOTTOM: concrete representation of the universe and empty set. Finite
3841 ;;; set operations are never called on TOP/BOTTOM, so it's safe to use special
3843 ;;; OVER/UNDER: the set-valued approximations of F.
3845 ;;; Implementation details.
3847 ;;; It's a straightforward walk down the type.
3848 ;;; Union types -> take the union of children, intersection ->
3849 ;;; intersect. There is some complication for negation types: we must
3850 ;;; not only negate the result, but also flip from overapproximating
3851 ;;; to underapproximating in the children (or vice versa).
3853 ;;; We represent sets as a pair of (negate-p finite-set) in order to
3854 ;;; support negation types.
3856 (declaim (inline generic-abstract-type-function
))
3857 (defun generic-abstract-type-function
3858 (type overapproximate
3859 union intersection difference
3862 (labels ((union* (x y
)
3863 ;; wrappers to avoid calling union/intersection on
3865 (cond ((or (eql x top
)
3871 (funcall union x y
))))
3872 (intersection* (x y
)
3873 (cond ((or (eql x bottom
)
3879 (funcall intersection x y
))))
3880 (unite (not-x-p x not-y-p y
)
3881 ;; if we only have one negated set, it's x.
3883 (rotatef not-x-p not-y-p
)
3885 (cond ((and not-x-p not-y-p
)
3886 ;; -x \/ -y = -(x /\ y)
3887 (normalize t
(intersection* x y
)))
3889 ;; -x \/ y = -(x \ y)
3899 (funcall difference x y
)))))
3901 (values nil
(union* x y
)))))
3902 (intersect (not-x-p x not-y-p y
)
3904 (rotatef not-x-p not-y-p
)
3906 (cond ((and not-x-p not-y-p
)
3907 ;; -x /\ -y = -(x \/ y)
3908 (normalize t
(union* x y
)))
3911 (cond ((or (eql x top
) (eql y bottom
))
3912 (values nil bottom
))
3918 (values nil
(funcall difference y x
)))))
3920 (values nil
(intersection* x y
)))))
3921 (normalize (not-x-p x
)
3922 ;; catch some easy cases of redundant negation.
3923 (cond ((not not-x-p
)
3931 (default (overapproximate)
3933 (if overapproximate top bottom
))
3934 (walk-union (types overapproximate
)
3935 ;; Only do this if union is provided.
3937 (return-from walk-union
(default overapproximate
)))
3938 ;; Reduce/union from bottom.
3939 (let ((not-acc-p nil
)
3941 (dolist (type types
(values not-acc-p acc
))
3942 (multiple-value-bind (not x
)
3943 (walk type overapproximate
)
3944 (setf (values not-acc-p acc
)
3945 (unite not-acc-p acc not x
)))
3946 ;; Early exit on top set.
3947 (when (and (eql acc top
)
3949 (return (values nil top
))))))
3950 (walk-intersection (types overapproximate
)
3951 ;; Skip if we don't know how to intersect sets
3952 (unless intersection
3953 (return-from walk-intersection
(default overapproximate
)))
3954 ;; Reduce/intersection from top
3955 (let ((not-acc-p nil
)
3957 (dolist (type types
(values not-acc-p acc
))
3958 (multiple-value-bind (not x
)
3959 (walk type overapproximate
)
3960 (setf (values not-acc-p acc
)
3961 (intersect not-acc-p acc not x
)))
3962 (when (and (eql acc bottom
)
3964 (return (values nil bottom
))))))
3965 (walk-negate (type overapproximate
)
3966 ;; Don't introduce negated types if we don't know how to
3969 (return-from walk-negate
(default overapproximate
)))
3970 (multiple-value-bind (not x
)
3971 (walk type
(not overapproximate
))
3972 (normalize (not not
) x
)))
3973 (walk (type overapproximate
)
3976 (walk-union (union-type-types type
) overapproximate
))
3977 ((cons (member or union
))
3978 (walk-union (rest type
) overapproximate
))
3980 (walk-intersection (intersection-type-types type
) overapproximate
))
3981 ((cons (member and intersection
))
3982 (walk-intersection (rest type
) overapproximate
))
3984 (walk-negate (negation-type-type type
) overapproximate
))
3986 (walk-negate (second type
) overapproximate
))
3994 (funcall under type
)
3995 (default nil
))))))))
3996 (multiple-value-call #'normalize
(walk type overapproximate
))))
3997 (declaim (notinline generic-abstract-type-function
))
3999 ;;; Standard list representation of sets. Use CL:* for the universe.
4000 (defun list-abstract-type-function (type over
&key under
(overapproximate t
))
4001 (declare (inline generic-abstract-type-function
))
4002 (generic-abstract-type-function
4003 type overapproximate
4004 #'union
#'intersection
#'set-difference
4008 (!defun-from-collected-cold-init-forms
!late-type-cold-init
)
4010 (/show0
"late-type.lisp end of file")