1.0.6.34: AVER that the GF lock is held during SET-DFUN
[sbcl/simd.git] / src / code / typep.lisp
blobf52c5e705ed5de9267f2b78ddbc4c43893158273
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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)
17 #!+sb-doc
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.)
25 (%typep object 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 (%%typep object
31 (if (ctype-p specifier)
32 specifier
33 (specifier-type specifier))))
34 (defun %%typep (object type)
35 (declare (type ctype type))
36 (etypecase type
37 (named-type
38 (ecase (named-type-name type)
39 ((* t) t)
40 ((instance) (%instancep object))
41 ((funcallable-instance) (funcallable-instance-p object))
42 ((extended-sequence) (extended-sequence-p object))
43 ((nil) nil)))
44 (numeric-type
45 (and (numberp 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)
52 (realpart object)
53 object)))
54 (ecase (numeric-type-class type)
55 (integer (integerp num))
56 (rational (rationalp num))
57 (float
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))))
64 ((nil) t)))
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)))
70 (t (>= val low)))
71 (cond ((null high) t)
72 ((listp high) (< val (car high)))
73 (t (<= val high)))))))
74 (ecase (numeric-type-complexp type)
75 ((nil) t)
76 (:complex
77 (and (complexp object)
78 (bound-test (realpart object))
79 (bound-test (imagpart object))))
80 (:real
81 (and (not (complexp object))
82 (bound-test object)))))))
83 (array-type
84 (and (arrayp object)
85 (ecase (array-type-complexp type)
86 ((t) (not (typep object 'simple-array)))
87 ((nil) (typep object 'simple-array))
88 ((:maybe) t))
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)
93 (unless (and want got
94 (or (eq (car want) '*)
95 (= (car want) (car got))))
96 (return nil))))
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
106 object)))))))
107 (member-type
108 (if (member object (member-type-members type)) t))
109 (classoid
110 #+sb-xc-host (ctypep object type)
111 #-sb-xc-host (classoid-typep (layout-of object) type object))
112 (union-type
113 (some (lambda (union-type-type) (%%typep object union-type-type))
114 (union-type-types type)))
115 (intersection-type
116 (every (lambda (intersection-type-type)
117 (%%typep object intersection-type-type))
118 (intersection-type-types type)))
119 (cons-type
120 (and (consp object)
121 (%%typep (car object) (cons-type-car-type type))
122 (%%typep (cdr object) (cons-type-cdr-type type))))
123 (character-set-type
124 (and (characterp object)
125 (let ((code (char-code object))
126 (pairs (character-set-type-pairs type)))
127 (dolist (pair pairs nil)
128 (destructuring-bind (low . high) pair
129 (when (<= low code high)
130 (return t)))))))
131 (unknown-type
132 ;; dunno how to do this ANSIly -- WHN 19990413
133 #+sb-xc-host (error "stub: %%TYPEP UNKNOWN-TYPE in xcompilation host")
134 ;; Parse it again to make sure it's really undefined.
135 (let ((reparse (specifier-type (unknown-type-specifier type))))
136 (if (typep reparse 'unknown-type)
137 (error "unknown type specifier: ~S"
138 (unknown-type-specifier reparse))
139 (%%typep object reparse))))
140 (negation-type
141 (not (%%typep object (negation-type-type type))))
142 (hairy-type
143 ;; Now the tricky stuff.
144 (let* ((hairy-spec (hairy-type-specifier type))
145 (symbol (car hairy-spec)))
146 (ecase symbol
147 (and
148 (every (lambda (spec) (%%typep object (specifier-type spec)))
149 (rest hairy-spec)))
150 ;; Note: it should be safe to skip OR here, because union
151 ;; types can always be represented as UNION-TYPE in general
152 ;; or other CTYPEs in special cases; we never need to use
153 ;; HAIRY-TYPE for them.
154 (not
155 (unless (proper-list-of-length-p hairy-spec 2)
156 (error "invalid type specifier: ~S" hairy-spec))
157 (not (%%typep object (specifier-type (cadr hairy-spec)))))
158 (satisfies
159 (unless (proper-list-of-length-p hairy-spec 2)
160 (error "invalid type specifier: ~S" hairy-spec))
161 (values (funcall (symbol-function (cadr hairy-spec)) object))))))
162 (alien-type-type
163 (sb!alien-internals:alien-typep object (alien-type-type-alien-type type)))
164 (fun-type
165 (error "Function types are not a legal argument to TYPEP:~% ~S"
166 (type-specifier type)))))
168 ;;; Do a type test from a class cell, allowing forward reference and
169 ;;; redefinition.
170 (defun classoid-cell-typep (obj-layout cell object)
171 (let ((classoid (classoid-cell-classoid cell)))
172 (unless classoid
173 (error "The class ~S has not yet been defined."
174 (classoid-cell-name cell)))
175 (classoid-typep obj-layout classoid object)))
177 ;;; Test whether OBJ-LAYOUT is from an instance of CLASSOID.
178 (defun classoid-typep (obj-layout classoid object)
179 (declare (optimize speed))
180 (multiple-value-bind (obj-layout layout)
181 (do ((layout (classoid-layout classoid) (classoid-layout classoid))
182 (i 0 (+ i 1))
183 (obj-layout obj-layout))
184 ((and (not (layout-invalid obj-layout))
185 (not (layout-invalid layout)))
186 (values obj-layout layout))
187 (aver (< i 2))
188 (when (layout-invalid obj-layout)
189 (setq obj-layout (update-object-layout-or-invalid object layout)))
190 (ensure-classoid-valid classoid layout))
191 (let ((obj-inherits (layout-inherits obj-layout)))
192 (or (eq obj-layout layout)
193 (dotimes (i (length obj-inherits) nil)
194 (when (eq (svref obj-inherits i) layout)
195 (return t)))))))