Make INFO's compiler-macro more forgiving.
[sbcl.git] / src / code / pred.lisp
blob20977d811fbbcd0b2365e54a9598ccb231ba1041
1 ;;;; predicate functions (EQUAL and friends, and type predicates)
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;;; miscellaneous non-primitive predicates
16 #!-sb-fluid (declaim (inline streamp))
17 (defun streamp (stream)
18 (typep stream 'stream))
20 ;;; various (VECTOR FOO) type predicates, not implemented as simple
21 ;;; widetag tests
22 (macrolet
23 ((def ()
24 `(progn
25 ,@(loop for (name spec) in *vector-without-complex-typecode-infos*
26 collect `(defun ,name (x)
27 (or (typep x '(simple-array ,spec (*)))
28 (and (complex-vector-p x)
29 (do ((data (%array-data-vector x) (%array-data-vector data)))
30 ((not (array-header-p data)) (typep data '(simple-array ,spec (*))))))))))))
31 (def))
33 ;;; Is X an extended sequence?
34 ;; This is like the "hierarchical layout depths for other things"
35 ;; case of the TYPEP transform (cf 'typetran'). Ideally it would
36 ;; be preferable to share TYPEP's code rather than repeat it here.
37 (declaim (maybe-inline extended-sequence-p))
38 (defun extended-sequence-p (x)
39 (let* ((slayout #.(info :type :compiler-layout 'sequence))
40 (depthoid #.(layout-depthoid (info :type :compiler-layout 'sequence)))
41 (layout
42 ;; It is not an error to define a class that is both SEQUENCE and
43 ;; FUNCALLABLE-INSTANCE with metaclass FUNCALLABLE-STANDARD-CLASS
44 (cond ((%instancep x) (%instance-layout x))
45 ((funcallable-instance-p x) (%funcallable-instance-layout x))
46 (t (return-from extended-sequence-p nil)))))
47 (when (layout-invalid layout)
48 (setq layout (update-object-layout-or-invalid x slayout)))
49 ;; It's _nearly_ impossible to create an instance which is exactly
50 ;; of type SEQUENCE. To wit: (make-instance 'sequence) =>
51 ;; "Cannot allocate an instance of #<BUILT-IN-CLASS SEQUENCE>."
52 ;; We should not need to check for that, just the 'inherits' vector.
53 ;; However, bootstrap code does a sleazy thing, making an instance of
54 ;; the abstract base type which is impossible for user code to do.
55 ;; Preferably the prototype instance for SEQUENCE would be one that could
56 ;; exist, so it would be a STANDARD-OBJECT and SEQUENCE. But it's not.
57 ;; Hence we have to check for a layout that no code using the documented
58 ;; sequence API would ever see, just to get the boundary case right.
59 ;; Note also:
60 ;; - Some builtins use a prototype object that is strictly deeper than
61 ;; layout of the named class because it is indeed the case that no
62 ;; object's layout can ever be EQ to that of the ancestor.
63 ;; e.g. a fixnum as representative of class REAL
64 ;; - Some builtins actually fail (TYPEP (CLASS-PROTOTYPE X) X)
65 ;; but that's not an excuse for getting SEQUENCE wrong:
66 ;; (CLASS-PROTOTYPE (FIND-CLASS 'FLOAT)) => 42
67 ;; (CLASS-PROTOTYPE (FIND-CLASS 'VECTOR)) => 42
68 ;; (CLASS-PROTOTYPE (FIND-CLASS 'LIST)) => 42
69 ;; (CLASS-PROTOTYPE (FIND-CLASS 'STRING)) => 42
70 (let ((inherits (layout-inherits (truly-the layout layout))))
71 (declare (optimize (safety 0)))
72 (eq (if (> (length inherits) depthoid) (svref inherits depthoid) layout)
73 slayout))))
75 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
76 (defun sequencep (x)
77 (declare (inline extended-sequence-p))
78 (or (listp x) (vectorp x) (extended-sequence-p x)))
80 ;;;; primitive predicates. These must be supported directly by the
81 ;;;; compiler.
83 (defun not (object)
84 #!+sb-doc
85 "Return T if X is NIL, otherwise return NIL."
86 (not object))
88 ;;; All the primitive type predicate wrappers share a parallel form..
89 (macrolet ((def-type-predicate-wrapper (pred)
90 (let* ((name (symbol-name pred))
91 (stem (string-left-trim "%" (string-right-trim "P-" name)))
92 (article (if (position (schar name 0) "AEIOU") "an" "a")))
93 `(defun ,pred (object)
94 #!+sb-doc
95 ,(format nil
96 "Return true if OBJECT is ~A ~A, and NIL otherwise."
97 article
98 stem)
99 ;; (falling through to low-level implementation)
100 (,pred object)))))
101 (def-type-predicate-wrapper array-header-p)
102 (def-type-predicate-wrapper arrayp)
103 (def-type-predicate-wrapper atom)
104 ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
105 ;; remove it there completely so that #-sb-unicode build will
106 ;; break when it's used.
107 #!+sb-unicode (def-type-predicate-wrapper base-char-p)
108 (def-type-predicate-wrapper base-string-p)
109 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
110 (def-type-predicate-wrapper bignump)
111 (def-type-predicate-wrapper bit-vector-p)
112 (def-type-predicate-wrapper characterp)
113 (def-type-predicate-wrapper code-component-p)
114 (def-type-predicate-wrapper consp)
115 (def-type-predicate-wrapper compiled-function-p)
116 (def-type-predicate-wrapper complexp)
117 (def-type-predicate-wrapper complex-double-float-p)
118 (def-type-predicate-wrapper complex-float-p)
119 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
120 (def-type-predicate-wrapper complex-rational-p)
121 (def-type-predicate-wrapper complex-single-float-p)
122 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
123 ;; the type it tests for in the Common Lisp type system, and since it's
124 ;; only used in the implementation of a few specialized things.)
125 (def-type-predicate-wrapper double-float-p)
126 (def-type-predicate-wrapper extended-char-p)
127 (def-type-predicate-wrapper fdefn-p)
128 (def-type-predicate-wrapper fixnump)
129 (def-type-predicate-wrapper floatp)
130 (def-type-predicate-wrapper functionp)
131 (def-type-predicate-wrapper integerp)
132 (def-type-predicate-wrapper listp)
133 (def-type-predicate-wrapper long-float-p)
134 (def-type-predicate-wrapper lra-p)
135 (def-type-predicate-wrapper null)
136 (def-type-predicate-wrapper numberp)
137 (def-type-predicate-wrapper rationalp)
138 (def-type-predicate-wrapper ratiop)
139 (def-type-predicate-wrapper realp)
140 (def-type-predicate-wrapper short-float-p)
141 (def-type-predicate-wrapper single-float-p)
142 #!+sb-simd-pack (def-type-predicate-wrapper simd-pack-p)
143 (def-type-predicate-wrapper %instancep)
144 (def-type-predicate-wrapper symbolp)
145 (def-type-predicate-wrapper %other-pointer-p)
146 (def-type-predicate-wrapper system-area-pointer-p)
147 (def-type-predicate-wrapper weak-pointer-p)
148 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
149 (progn
150 (def-type-predicate-wrapper unsigned-byte-32-p)
151 (def-type-predicate-wrapper signed-byte-32-p))
152 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
153 (progn
154 (def-type-predicate-wrapper unsigned-byte-64-p)
155 (def-type-predicate-wrapper signed-byte-64-p))
156 ;; Specialized array types
157 (macrolet ((saetp-defs ()
158 `(progn
159 ,@(map 'list
160 (lambda (saetp)
161 `(def-type-predicate-wrapper
162 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
163 sb!vm:*specialized-array-element-type-properties*))))
164 (saetp-defs))
165 ;; Other array types
166 (def-type-predicate-wrapper simple-array-p)
167 (def-type-predicate-wrapper simple-rank-1-array-*-p)
168 (def-type-predicate-wrapper simple-string-p)
169 (def-type-predicate-wrapper stringp)
170 (def-type-predicate-wrapper vectorp)
171 (def-type-predicate-wrapper vector-nil-p))
173 #!+(or x86 x86-64 arm)
174 (defun fixnum-mod-p (x limit)
175 (and (fixnump x)
176 (<= 0 x limit)))
178 #!+(or x86 x86-64 ppc)
179 (defun %other-pointer-subtype-p (x choices)
180 (and (%other-pointer-p x)
181 (member (%other-pointer-widetag x) choices)
184 ;;; Return the specifier for the type of object. This is not simply
185 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
186 ;;; goals than TYPE-OF. In particular, speed is more important than
187 ;;; precision here, and it is not permitted to return member types,
188 ;;; negation, union, or intersection types.
189 (defun type-of (object)
190 #!+sb-doc
191 "Return the type of OBJECT."
192 (typecase object
193 (fixnum
194 (cond
195 ((<= 0 object 1) 'bit)
196 ((< object 0) 'fixnum)
197 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
198 (integer
199 (if (>= object 0)
200 '(integer #.(1+ sb!xc:most-positive-fixnum))
201 'bignum))
202 (standard-char 'standard-char)
203 (base-char 'base-char)
204 (extended-char 'extended-char)
205 ((member t) 'boolean)
206 (keyword 'keyword)
207 ((or array complex #!+sb-simd-pack simd-pack)
208 (let ((sb!kernel::*unparse-allow-negation* nil))
209 (declare (special sb!kernel::*unparse-allow-negation*)) ; forward ref
210 (type-specifier (ctype-of object))))
212 (let* ((classoid (layout-classoid (layout-of object)))
213 (name (classoid-name classoid)))
214 (if (%instancep object)
215 (case name
216 (sb!alien-internals:alien-value
217 `(alien
218 ,(sb!alien-internals:unparse-alien-type
219 (sb!alien-internals:alien-value-type object))))
221 (let ((pname (classoid-proper-name classoid)))
222 (if (classoid-p pname)
223 (classoid-pcl-class pname)
224 pname))))
225 name)))))
227 ;;;; equality predicates
229 ;;; This is real simple, 'cause the compiler takes care of it.
230 (defun eq (obj1 obj2)
231 #!+sb-doc
232 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
233 (eq obj1 obj2))
234 ;;; and this too, but it's only needed for backends on which
235 ;;; IR1 might potentially transform EQL into %EQL/INTEGER.
236 #!+integer-eql-vop
237 (defun %eql/integer (obj1 obj2)
238 (eql (the integer obj1) (the integer obj2)))
240 (declaim (inline %eql))
241 (defun %eql (obj1 obj2)
242 #!+sb-doc
243 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
244 (or (eq obj1 obj2)
245 (if (or (typep obj2 'fixnum)
246 (not (typep obj2 'number)))
248 ;; I would think that we could do slightly better here by testing that
249 ;; both objs are OTHER-POINTER-P with equal %OTHER-POINTER-WIDETAGs.
250 ;; Then dispatch on obj2 and elide the TYPEP on obj1 using TRULY-THE.
251 ;; Also would need to deal with immediate single-float for 64-bit.
252 (macrolet ((foo (&rest stuff)
253 `(typecase obj2
254 ,@(mapcar (lambda (foo)
255 (let ((type (car foo))
256 (fn (cadr foo)))
257 `(,type
258 (and (typep obj1 ',type)
259 (,fn obj1 obj2)))))
260 stuff))))
261 (foo
262 (single-float eql)
263 (double-float eql)
264 #!+long-float
265 (long-float eql)
266 (bignum
267 #!-integer-eql-vop (lambda (x y) (zerop (bignum-compare x y)))
268 #!+integer-eql-vop eql) ; will become %eql/integer
269 (ratio
270 (lambda (x y)
271 (and (eql (numerator x) (numerator y))
272 (eql (denominator x) (denominator y)))))
273 (complex
274 (lambda (x y)
275 (and (eql (realpart x) (realpart y))
276 (eql (imagpart x) (imagpart y))))))))))
278 (defun eql (x y)
279 (%eql x y))
281 (defun bit-vector-= (x y)
282 (declare (type bit-vector x y))
283 (cond ((eq x y))
284 ((and (simple-bit-vector-p x)
285 (simple-bit-vector-p y))
286 (bit-vector-= x y)) ; DEFTRANSFORM
288 (and (= (length x) (length y))
289 (do ((i 0 (1+ i))
290 (length (length x)))
291 ((= i length) t)
292 (declare (fixnum i))
293 (unless (= (bit x i) (bit y i))
294 (return nil)))))))
296 (defun equal (x y)
297 #!+sb-doc
298 "Return T if X and Y are EQL or if they are structured components whose
299 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
300 length and have identical components. Other arrays must be EQ to be EQUAL."
301 ;; Non-tail self-recursion implemented with a local auxiliary function
302 ;; is a lot faster than doing it the straightforward way (at least
303 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
304 (labels ((equal-aux (x y)
305 (cond ((%eql x y)
307 ((consp x)
308 (and (consp y)
309 (equal-aux (car x) (car y))
310 (equal-aux (cdr x) (cdr y))))
311 ((stringp x)
312 (and (stringp y) (string= x y)))
313 ((pathnamep x)
314 (and (pathnamep y) (pathname= x y)))
315 ((bit-vector-p x)
316 (and (bit-vector-p y)
317 (bit-vector-= x y)))
318 (t nil))))
319 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
320 ;; of 200 times with INLINE). -- JES, 2005-12-30
321 (declare (maybe-inline equal-aux))
322 (equal-aux x y)))
324 ;;; EQUALP comparison of HASH-TABLE values
325 (defun hash-table-equalp (x y)
326 (declare (type hash-table x y))
327 (or (eq x y)
328 (and (hash-table-p y)
329 (eql (hash-table-count x) (hash-table-count y))
330 (eql (hash-table-test x) (hash-table-test y))
331 (block comparison-of-entries
332 (maphash (lambda (key x-value)
333 (multiple-value-bind (y-value y-value-p)
334 (gethash key y)
335 (unless (and y-value-p (equalp x-value y-value))
336 (return-from comparison-of-entries nil))))
338 t))))
340 (defun equalp (x y)
341 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
342 ; and HASH-TABLEs.
343 "This is like EQUAL, except more liberal in several respects.
344 Numbers may be of different types, as long as the values are identical
345 after coercion. Characters may differ in alphabetic case. Vectors and
346 arrays must have identical dimensions and EQUALP elements, but may differ
347 in their type restriction."
348 (cond ((eq x y) t)
349 ((characterp x) (and (characterp y) (char-equal x y)))
350 ((numberp x) (and (numberp y) (= x y)))
351 ((consp x)
352 (and (consp y)
353 (equalp (car x) (car y))
354 (equalp (cdr x) (cdr y))))
355 ((pathnamep x)
356 (and (pathnamep y) (pathname= x y)))
357 ((hash-table-p x)
358 (and (hash-table-p y)
359 (hash-table-equalp x y)))
360 ((%instancep x)
361 (let ((layout-x (%instance-layout x)))
362 (and (%instancep y)
363 (eq layout-x (%instance-layout y))
364 ;; TODO: store one bit in the layout indicating whether EQUALP
365 ;; should scan slots. (basically a STRUCTURE-CLASSOID-P bit)
366 (structure-classoid-p (layout-classoid layout-x))
367 (macrolet ((slot-ref-equalp ()
368 `(let ((x-el (%instance-ref x i))
369 (y-el (%instance-ref y i)))
370 (or (eq x-el y-el) (equalp x-el y-el)))))
371 #!-interleaved-raw-slots
372 (let ((raw-len (layout-n-untagged-slots layout-x))
373 (total-len (layout-length layout-x)))
374 (and (dotimes (i (- total-len raw-len) t)
375 (unless (slot-ref-equalp)
376 (return nil)))
377 (or (zerop raw-len)
378 (raw-instance-slots-equalp layout-x x y))))
379 #!+interleaved-raw-slots
380 (let ((metadata (layout-untagged-bitmap layout-x)))
381 (if (zerop metadata)
382 (loop for i of-type index from sb!vm:instance-data-start
383 below (layout-length layout-x)
384 always (slot-ref-equalp))
385 (let ((comparators (layout-equalp-tests layout-x)))
386 (unless (= (length comparators)
387 (- (layout-length layout-x) sb!vm:instance-data-start))
388 (bug "EQUALP got incomplete instance layout"))
389 ;; See remark at the source code for %TARGET-DEFSTRUCT
390 ;; explaining how to use the vector of comparators.
391 (loop for i of-type index from sb!vm:instance-data-start
392 below (layout-length layout-x)
393 for test = (data-vector-ref
394 comparators (- i sb!vm:instance-data-start))
395 always (cond ((eql test 0) (slot-ref-equalp))
396 ((functionp test)
397 (funcall test i x y))
398 (t))))))))))
399 ((vectorp x)
400 (let ((length (length x)))
401 (and (vectorp y)
402 (= length (length y))
403 (dotimes (i length t)
404 (let ((x-el (aref x i))
405 (y-el (aref y i)))
406 (unless (or (eq x-el y-el)
407 (equalp x-el y-el))
408 (return nil)))))))
409 ((arrayp x)
410 (and (arrayp y)
411 (= (array-rank x) (array-rank y))
412 (dotimes (axis (array-rank x) t)
413 (unless (= (array-dimension x axis)
414 (array-dimension y axis))
415 (return nil)))
416 (dotimes (index (array-total-size x) t)
417 (let ((x-el (row-major-aref x index))
418 (y-el (row-major-aref y index)))
419 (unless (or (eq x-el y-el)
420 (equalp x-el y-el))
421 (return nil))))))
422 (t nil)))
424 (/show0 "about to do test cases in pred.lisp")
425 #!+sb-test
426 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
427 (0.0 1.0 nil)
428 (#c(1 0) #c(1.0 0.0) t)
429 (#c(0 1) #c(0.0 1.0) t)
430 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
431 ("Hello" "hello" t)
432 ("Hello" #(#\h #\E #\l #\l #\o) t)
433 ("Hello" "goodbye" nil))))
434 (/show0 "TEST-CASES bound in pred.lisp")
435 (dolist (test-case test-cases)
436 (/show0 "about to do a TEST-CASE in pred.lisp")
437 (destructuring-bind (x y expected-result) test-case
438 (let* ((result (equalp x y))
439 (bresult (if result 1 0))
440 (expected-bresult (if expected-result 1 0)))
441 (unless (= bresult expected-bresult)
442 (/show0 "failing test in pred.lisp")
443 (error "failed test (EQUALP ~S ~S)" x y))))))
444 (/show0 "done with test cases in pred.lisp")