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 (type lexenv-designator environment
) (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.)
25 (%%typep object
(specifier-type type
)))
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
)
30 ;; Checking CTYPE-P on the specifier, as used to be done, is not right.
31 ;; If the specifier were a CTYPE we shouldn't have gotten here.
32 (%%typep object
(specifier-type specifier
)))
34 (defun %%typep
(object type
&optional
(strict t
))
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 (or (eq (array-type-complexp type
) :maybe
)
86 (eq (not (simple-array-p object
))
87 (array-type-complexp type
)))
88 (let ((want (array-type-dimensions type
)))
90 (if (array-header-p object
)
91 (do ((rank (array-rank object
))
93 (want want
(cdr want
)))
94 ((= axis rank
) (null want
))
95 (let ((dim (car want
)))
96 (unless (or (eq dim
'*)
97 (eq dim
(%array-dimension object axis
)))
99 (let ((dim (car want
)))
100 (and (or (eq dim
'*) (eq dim
(length object
)))
101 (not (cdr want
)))))))
102 ;; FIXME: treatment of compound types involving unknown types
103 ;; is generally bogus throughout the system, e.g.
104 ;; (TYPEP MY-ARRAY '(ARRAY (OR BAD1 BAD2) *)) => T
105 ;; because (OR BAD1 BAD2) is not represented as an UNKNOWN-TYPE,
106 ;; and has specialized type '*.
107 ;; One way to fix this is that every CTYPE needs a bit to indicate
108 ;; whether any subpart of it is unknown, or else when parsing,
109 ;; we should always return an UNKNOWN if any subpart is unknown,
110 ;; or else any time we use a CTYPE, we do a deep traversal
111 ;; to detect embedded UNKNOWNs (which seems bad for performance).
112 (if (unknown-type-p (array-type-element-type type
))
113 ;; better to fail this way than to get bogosities like
114 ;; (TYPEP (MAKE-ARRAY 11) '(ARRAY SOME-UNDEFINED-TYPE)) => T
115 (error "~@<unknown element type in array type: ~2I~_~S~:>"
116 (type-specifier type
))
118 (or (eq (array-type-specialized-element-type type
) *wild-type
*)
119 (values (type= (array-type-specialized-element-type type
)
120 ;; FIXME: not the most efficient.
121 (specifier-type (array-element-type
124 (when (member-type-member-p object type
)
127 #+sb-xc-host
(ctypep object type
)
128 ;; It might be more efficient to check that OBJECT is either INSTANCEP
129 ;; or FUNCALLABLE-INSTANCE-P before making this call.
130 ;; But doing that would change the behavior if %%TYPEP were ever called
131 ;; with a built-in classoid whose members are not instances.
132 ;; e.g. (%%typep (find-fdefn 'car) (specifier-type 'fdefn))
133 ;; I'm not sure if that can happen.
134 #-sb-xc-host
(classoid-typep (layout-of object
) type object
))
136 (some (lambda (union-type-type) (%%typep object union-type-type strict
))
137 (union-type-types type
)))
139 (every (lambda (intersection-type-type)
140 (%%typep object intersection-type-type strict
))
141 (intersection-type-types type
)))
144 (%%typep
(car object
) (cons-type-car-type type
) strict
)
145 (%%typep
(cdr object
) (cons-type-cdr-type type
) strict
)))
148 (and (simd-pack-p object
)
149 (let* ((tag (%simd-pack-tag object
))
150 (name (nth tag
*simd-pack-element-types
*)))
151 (not (not (member name
(simd-pack-type-element-type type
)))))))
153 (and (characterp object
)
154 (let ((code (char-code object
))
155 (pairs (character-set-type-pairs type
)))
156 (dolist (pair pairs nil
)
157 (destructuring-bind (low . high
) pair
158 (when (<= low code high
)
161 ;; dunno how to do this ANSIly -- WHN 19990413
162 #+sb-xc-host
(error "stub: %%TYPEP UNKNOWN-TYPE in xcompilation host")
163 ;; Parse it again to make sure it's really undefined.
164 (let ((reparse (specifier-type (unknown-type-specifier type
))))
165 (if (typep reparse
'unknown-type
)
166 (error "unknown type specifier: ~S"
167 (unknown-type-specifier reparse
))
168 (%%typep object reparse strict
))))
170 (not (%%typep object
(negation-type-type type
) strict
)))
172 ;; Now the tricky stuff.
173 (let* ((hairy-spec (hairy-type-specifier type
))
174 (symbol (car hairy-spec
)))
177 (every (lambda (spec) (%%typep object
(specifier-type spec
) strict
))
179 ;; Note: it should be safe to skip OR here, because union
180 ;; types can always be represented as UNION-TYPE in general
181 ;; or other CTYPEs in special cases; we never need to use
182 ;; HAIRY-TYPE for them.
184 (unless (proper-list-of-length-p hairy-spec
2)
185 (error "invalid type specifier: ~S" hairy-spec
))
186 (not (%%typep object
(specifier-type (cadr hairy-spec
)) strict
)))
188 (unless (proper-list-of-length-p hairy-spec
2)
189 (error "invalid type specifier: ~S" hairy-spec
))
190 (values (funcall (symbol-function (cadr hairy-spec
)) object
))))))
192 (sb!alien-internals
:alien-typep object
(alien-type-type-alien-type type
)))
195 (error "Function types are not a legal argument to TYPEP:~% ~S"
196 (type-specifier type
))
197 (and (functionp object
)
198 (csubtypep (specifier-type (sb!impl
::%fun-type object
)) type
))))))
200 ;;; Do a type test from a class cell, allowing forward reference and
202 (defun classoid-cell-typep (obj-layout cell object
)
203 (let ((classoid (classoid-cell-classoid cell
)))
205 (error "The class ~S has not yet been defined."
206 (classoid-cell-name cell
)))
207 (classoid-typep obj-layout classoid object
)))
209 ;;; Test whether OBJ-LAYOUT is from an instance of CLASSOID.
210 (defun classoid-typep (obj-layout classoid object
)
211 (declare (optimize speed
))
212 ;; FIXME & KLUDGE: We could like to grab the *WORLD-LOCK* here (to ensure that
213 ;; class graph doesn't change while we're doing the typep test), but in
214 ;; pratice that causes trouble -- deadlocking against the compiler
215 ;; if compiler output (or macro, or compiler-macro expansion) causes
216 ;; another thread to do stuff. Not locking is a shoddy bandaid as it is remains
217 ;; easy to trigger the same problem using a different code path -- but in practice
218 ;; locking here makes Slime unusable with :SPAWN in post *WORLD-LOCK* world. So...
220 (multiple-value-bind (obj-layout layout
)
221 (do ((layout (classoid-layout classoid
) (classoid-layout classoid
))
223 (obj-layout obj-layout
))
224 ((and (not (layout-invalid obj-layout
))
225 (not (layout-invalid layout
)))
226 (values obj-layout layout
))
228 (when (layout-invalid obj-layout
)
229 (setq obj-layout
(update-object-layout-or-invalid object layout
)))
230 (%ensure-classoid-valid classoid layout
"typep"))
231 (let ((obj-inherits (layout-inherits obj-layout
)))
232 (or (eq obj-layout layout
)
233 (dotimes (i (length obj-inherits
) nil
)
234 (when (eq (svref obj-inherits i
) layout
)