1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!KERNEL")
12 ;;; (Note that when cross-compiling, SB!XC:TYPEP is interpreted as a
13 ;;; test that the host Lisp object OBJECT translates to a target SBCL
14 ;;; type TYPE. This behavior is needed e.g. to test for the validity
15 ;;; of numeric subtype bounds read when cross-compiling.)
16 (defun typep (object type
&optional environment
)
18 "Is OBJECT of type TYPE?"
19 (declare (ignore environment
))
20 ;; Actually interpreting types at runtime is done by %TYPEP. The
21 ;; cost of the extra function call here should be negligible
22 ;; compared to the cost of interpreting types. (And the compiler
23 ;; tries hard to optimize away the interpretation of types at
24 ;; runtime, and when it succeeds, we never get here anyway.)
27 ;;; the actual TYPEP engine. The compiler only generates calls to this
28 ;;; function when it can't figure out anything more intelligent to do.
29 (defun %typep
(object specifier
)
31 (if (ctype-p specifier
)
33 (specifier-type specifier
))))
34 (defun %%typep
(object type
)
35 (declare (type ctype type
))
38 (ecase (named-type-name type
)
40 ((instance) (%instancep object
))
41 ((funcallable-instance) (funcallable-instance-p object
))
42 ((extended-sequence) (extended-sequence-p object
))
46 (let (;; I think this works because of an invariant of the
47 ;; two components of a COMPLEX are always coerced to
48 ;; be the same, e.g. (COMPLEX 1.0 3/2) => #C(1.0 1.5).
49 ;; Dunno why that holds, though -- ANSI? Python
50 ;; tradition? marsh faerie spirits? -- WHN 2001-10-27
51 (num (if (complexp object
)
54 (ecase (numeric-type-class type
)
55 (integer (integerp num
))
56 (rational (rationalp num
))
58 (ecase (numeric-type-format type
)
59 (short-float (typep num
'short-float
))
60 (single-float (typep num
'single-float
))
61 (double-float (typep num
'double-float
))
62 (long-float (typep num
'long-float
))
63 ((nil) (floatp num
))))
65 (flet ((bound-test (val)
66 (let ((low (numeric-type-low type
))
67 (high (numeric-type-high type
)))
68 (and (cond ((null low
) t
)
69 ((listp low
) (> val
(car low
)))
72 ((listp high
) (< val
(car high
)))
73 (t (<= val high
)))))))
74 (ecase (numeric-type-complexp type
)
77 (and (complexp object
)
78 (bound-test (realpart object
))
79 (bound-test (imagpart object
))))
81 (and (not (complexp object
))
82 (bound-test object
)))))))
85 (ecase (array-type-complexp type
)
86 ((t) (not (typep object
'simple-array
)))
87 ((nil) (typep object
'simple-array
))
89 (or (eq (array-type-dimensions type
) '*)
90 (do ((want (array-type-dimensions type
) (cdr want
))
91 (got (array-dimensions object
) (cdr got
)))
92 ((and (null want
) (null got
)) t
)
94 (or (eq (car want
) '*)
95 (= (car want
) (car got
))))
97 (if (unknown-type-p (array-type-element-type type
))
98 ;; better to fail this way than to get bogosities like
99 ;; (TYPEP (MAKE-ARRAY 11) '(ARRAY SOME-UNDEFINED-TYPE)) => T
100 (error "~@<unknown element type in array type: ~2I~_~S~:>"
101 (type-specifier type
))
103 (or (eq (array-type-element-type type
) *wild-type
*)
104 (values (type= (array-type-specialized-element-type type
)
105 (specifier-type (array-element-type
108 (when (member-type-member-p object type
)
111 #+sb-xc-host
(ctypep object type
)
112 #-sb-xc-host
(classoid-typep (layout-of object
) type object
))
114 (some (lambda (union-type-type) (%%typep object union-type-type
))
115 (union-type-types type
)))
117 (every (lambda (intersection-type-type)
118 (%%typep object intersection-type-type
))
119 (intersection-type-types type
)))
122 (%%typep
(car object
) (cons-type-car-type type
))
123 (%%typep
(cdr object
) (cons-type-cdr-type type
))))
125 (and (characterp object
)
126 (let ((code (char-code object
))
127 (pairs (character-set-type-pairs type
)))
128 (dolist (pair pairs nil
)
129 (destructuring-bind (low . high
) pair
130 (when (<= low code high
)
133 ;; dunno how to do this ANSIly -- WHN 19990413
134 #+sb-xc-host
(error "stub: %%TYPEP UNKNOWN-TYPE in xcompilation host")
135 ;; Parse it again to make sure it's really undefined.
136 (let ((reparse (specifier-type (unknown-type-specifier type
))))
137 (if (typep reparse
'unknown-type
)
138 (error "unknown type specifier: ~S"
139 (unknown-type-specifier reparse
))
140 (%%typep object reparse
))))
142 (not (%%typep object
(negation-type-type type
))))
144 ;; Now the tricky stuff.
145 (let* ((hairy-spec (hairy-type-specifier type
))
146 (symbol (car hairy-spec
)))
149 (every (lambda (spec) (%%typep object
(specifier-type spec
)))
151 ;; Note: it should be safe to skip OR here, because union
152 ;; types can always be represented as UNION-TYPE in general
153 ;; or other CTYPEs in special cases; we never need to use
154 ;; HAIRY-TYPE for them.
156 (unless (proper-list-of-length-p hairy-spec
2)
157 (error "invalid type specifier: ~S" hairy-spec
))
158 (not (%%typep object
(specifier-type (cadr hairy-spec
)))))
160 (unless (proper-list-of-length-p hairy-spec
2)
161 (error "invalid type specifier: ~S" hairy-spec
))
162 (values (funcall (symbol-function (cadr hairy-spec
)) object
))))))
164 (sb!alien-internals
:alien-typep object
(alien-type-type-alien-type type
)))
166 (error "Function types are not a legal argument to TYPEP:~% ~S"
167 (type-specifier type
)))))
169 ;;; Do a type test from a class cell, allowing forward reference and
171 (defun classoid-cell-typep (obj-layout cell object
)
172 (let ((classoid (classoid-cell-classoid cell
)))
174 (error "The class ~S has not yet been defined."
175 (classoid-cell-name cell
)))
176 (classoid-typep obj-layout classoid object
)))
178 ;;; Test whether OBJ-LAYOUT is from an instance of CLASSOID.
179 (defun classoid-typep (obj-layout classoid object
)
180 (declare (optimize speed
))
181 (multiple-value-bind (obj-layout layout
)
182 (do ((layout (classoid-layout classoid
) (classoid-layout classoid
))
184 (obj-layout obj-layout
))
185 ((and (not (layout-invalid obj-layout
))
186 (not (layout-invalid layout
)))
187 (values obj-layout layout
))
189 (when (layout-invalid obj-layout
)
190 (setq obj-layout
(update-object-layout-or-invalid object layout
)))
191 (ensure-classoid-valid classoid layout
))
192 (let ((obj-inherits (layout-inherits obj-layout
)))
193 (or (eq obj-layout layout
)
194 (dotimes (i (length obj-inherits
) nil
)
195 (when (eq (svref obj-inherits i
) layout
)