Plug up leaky abstraction that (%INSTANCE-REF struct 0) is a LAYOUT.
[sbcl.git] / src / code / pred.lisp
blobed7394da724fb66900067769b092ca00d9da046c
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 (if (and (> (length inherits) depthoid)
73 (eq (svref inherits depthoid) slayout))
75 (eq layout slayout)))))
77 ;;; Is X a SEQUENCE? Harder than just (OR VECTOR LIST)
78 (defun sequencep (x)
79 (declare (inline extended-sequence-p))
80 (or (listp x) (vectorp x) (extended-sequence-p x)))
82 ;;;; primitive predicates. These must be supported directly by the
83 ;;;; compiler.
85 (defun not (object)
86 #!+sb-doc
87 "Return T if X is NIL, otherwise return NIL."
88 (not object))
90 ;;; All the primitive type predicate wrappers share a parallel form..
91 (macrolet ((def-type-predicate-wrapper (pred)
92 (let* ((name (symbol-name pred))
93 (stem (string-left-trim "%" (string-right-trim "P-" name)))
94 (article (if (position (schar name 0) "AEIOU") "an" "a")))
95 `(defun ,pred (object)
96 #!+sb-doc
97 ,(format nil
98 "Return true if OBJECT is ~A ~A, and NIL otherwise."
99 article
100 stem)
101 ;; (falling through to low-level implementation)
102 (,pred object)))))
103 (def-type-predicate-wrapper array-header-p)
104 (def-type-predicate-wrapper arrayp)
105 (def-type-predicate-wrapper atom)
106 ;; Testing for BASE-CHAR-P is usually redundant on #-sb-unicode,
107 ;; remove it there completely so that #-sb-unicode build will
108 ;; break when it's used.
109 #!+sb-unicode (def-type-predicate-wrapper base-char-p)
110 (def-type-predicate-wrapper base-string-p)
111 #!+sb-unicode (def-type-predicate-wrapper character-string-p)
112 (def-type-predicate-wrapper bignump)
113 (def-type-predicate-wrapper bit-vector-p)
114 (def-type-predicate-wrapper characterp)
115 (def-type-predicate-wrapper code-component-p)
116 (def-type-predicate-wrapper consp)
117 (def-type-predicate-wrapper compiled-function-p)
118 (def-type-predicate-wrapper complexp)
119 (def-type-predicate-wrapper complex-double-float-p)
120 (def-type-predicate-wrapper complex-float-p)
121 #!+long-float (def-type-predicate-wrapper complex-long-float-p)
122 (def-type-predicate-wrapper complex-rational-p)
123 (def-type-predicate-wrapper complex-single-float-p)
124 ;; (COMPLEX-VECTOR-P is not included here since it's awkward to express
125 ;; the type it tests for in the Common Lisp type system, and since it's
126 ;; only used in the implementation of a few specialized things.)
127 (def-type-predicate-wrapper double-float-p)
128 (def-type-predicate-wrapper extended-char-p)
129 (def-type-predicate-wrapper fdefn-p)
130 (def-type-predicate-wrapper fixnump)
131 (def-type-predicate-wrapper floatp)
132 (def-type-predicate-wrapper functionp)
133 (def-type-predicate-wrapper integerp)
134 (def-type-predicate-wrapper listp)
135 (def-type-predicate-wrapper long-float-p)
136 (def-type-predicate-wrapper lra-p)
137 (def-type-predicate-wrapper null)
138 (def-type-predicate-wrapper numberp)
139 (def-type-predicate-wrapper rationalp)
140 (def-type-predicate-wrapper ratiop)
141 (def-type-predicate-wrapper realp)
142 (def-type-predicate-wrapper short-float-p)
143 (def-type-predicate-wrapper single-float-p)
144 #!+sb-simd-pack (def-type-predicate-wrapper simd-pack-p)
145 (def-type-predicate-wrapper %instancep)
146 (def-type-predicate-wrapper symbolp)
147 (def-type-predicate-wrapper %other-pointer-p)
148 (def-type-predicate-wrapper system-area-pointer-p)
149 (def-type-predicate-wrapper weak-pointer-p)
150 #!+#.(cl:if (cl:= 32 sb!vm:n-word-bits) '(and) '(or))
151 (progn
152 (def-type-predicate-wrapper unsigned-byte-32-p)
153 (def-type-predicate-wrapper signed-byte-32-p))
154 #!+#.(cl:if (cl:= 64 sb!vm:n-word-bits) '(and) '(or))
155 (progn
156 (def-type-predicate-wrapper unsigned-byte-64-p)
157 (def-type-predicate-wrapper signed-byte-64-p))
158 ;; Specialized array types
159 (macrolet ((saetp-defs ()
160 `(progn
161 ,@(map 'list
162 (lambda (saetp)
163 `(def-type-predicate-wrapper
164 ,(symbolicate (sb!vm:saetp-primitive-type-name saetp) "-P")))
165 sb!vm:*specialized-array-element-type-properties*))))
166 (saetp-defs))
167 ;; Other array types
168 (def-type-predicate-wrapper simple-array-p)
169 (def-type-predicate-wrapper simple-rank-1-array-*-p)
170 (def-type-predicate-wrapper simple-string-p)
171 (def-type-predicate-wrapper stringp)
172 (def-type-predicate-wrapper vectorp)
173 (def-type-predicate-wrapper vector-nil-p))
175 #!+(or x86 x86-64 arm)
176 (defun fixnum-mod-p (x limit)
177 (and (fixnump x)
178 (<= 0 x limit)))
180 #!+(or x86 x86-64 ppc)
181 (defun %other-pointer-subtype-p (x choices)
182 (and (%other-pointer-p x)
183 (member (%other-pointer-widetag x) choices)
186 ;;; Return the specifier for the type of object. This is not simply
187 ;;; (TYPE-SPECIFIER (CTYPE-OF OBJECT)) because CTYPE-OF has different
188 ;;; goals than TYPE-OF. In particular, speed is more important than
189 ;;; precision here, and it is not permitted to return member types,
190 ;;; negation, union, or intersection types.
191 (defun type-of (object)
192 #!+sb-doc
193 "Return the type of OBJECT."
194 (typecase object
195 (fixnum
196 (cond
197 ((<= 0 object 1) 'bit)
198 ((< object 0) 'fixnum)
199 (t '(integer 0 #.sb!xc:most-positive-fixnum))))
200 (integer
201 (if (>= object 0)
202 '(integer #.(1+ sb!xc:most-positive-fixnum))
203 'bignum))
204 (standard-char 'standard-char)
205 (base-char 'base-char)
206 (extended-char 'extended-char)
207 ((member t) 'boolean)
208 (keyword 'keyword)
209 ((or array complex #!+sb-simd-pack simd-pack)
210 (let ((sb!kernel::*unparse-allow-negation* nil))
211 (declare (special sb!kernel::*unparse-allow-negation*)) ; forward ref
212 (type-specifier (ctype-of object))))
214 (let* ((classoid (layout-classoid (layout-of object)))
215 (name (classoid-name classoid)))
216 (if (%instancep object)
217 (case name
218 (sb!alien-internals:alien-value
219 `(alien
220 ,(sb!alien-internals:unparse-alien-type
221 (sb!alien-internals:alien-value-type object))))
223 (let ((pname (classoid-proper-name classoid)))
224 (if (classoid-p pname)
225 (classoid-pcl-class pname)
226 pname))))
227 name)))))
229 ;;;; equality predicates
231 ;;; This is real simple, 'cause the compiler takes care of it.
232 (defun eq (obj1 obj2)
233 #!+sb-doc
234 "Return T if OBJ1 and OBJ2 are the same object, otherwise NIL."
235 (eq obj1 obj2))
237 (declaim (inline %eql))
238 (defun %eql (obj1 obj2)
239 #!+sb-doc
240 "Return T if OBJ1 and OBJ2 represent the same object, otherwise NIL."
241 (or (eq obj1 obj2)
242 (if (or (typep obj2 'fixnum)
243 (not (typep obj2 'number)))
245 (macrolet ((foo (&rest stuff)
246 `(typecase obj2
247 ,@(mapcar (lambda (foo)
248 (let ((type (car foo))
249 (fn (cadr foo)))
250 `(,type
251 (and (typep obj1 ',type)
252 (,fn obj1 obj2)))))
253 stuff))))
254 (foo
255 (single-float eql)
256 (double-float eql)
257 #!+long-float
258 (long-float eql)
259 (bignum
260 (lambda (x y)
261 (zerop (bignum-compare x y))))
262 (ratio
263 (lambda (x y)
264 (and (eql (numerator x) (numerator y))
265 (eql (denominator x) (denominator y)))))
266 (complex
267 (lambda (x y)
268 (and (eql (realpart x) (realpart y))
269 (eql (imagpart x) (imagpart y))))))))))
271 (defun eql (x y)
272 (%eql x y))
274 (defun bit-vector-= (x y)
275 (declare (type bit-vector x y))
276 (cond ((eq x y))
277 ((and (simple-bit-vector-p x)
278 (simple-bit-vector-p y))
279 (bit-vector-= x y)) ; DEFTRANSFORM
281 (and (= (length x) (length y))
282 (do ((i 0 (1+ i))
283 (length (length x)))
284 ((= i length) t)
285 (declare (fixnum i))
286 (unless (= (bit x i) (bit y i))
287 (return nil)))))))
289 (defun equal (x y)
290 #!+sb-doc
291 "Return T if X and Y are EQL or if they are structured components whose
292 elements are EQUAL. Strings and bit-vectors are EQUAL if they are the same
293 length and have identical components. Other arrays must be EQ to be EQUAL."
294 ;; Non-tail self-recursion implemented with a local auxiliary function
295 ;; is a lot faster than doing it the straightforward way (at least
296 ;; on x86oids) due to calling convention differences. -- JES, 2005-12-30
297 (labels ((equal-aux (x y)
298 (cond ((%eql x y)
300 ((consp x)
301 (and (consp y)
302 (equal-aux (car x) (car y))
303 (equal-aux (cdr x) (cdr y))))
304 ((stringp x)
305 (and (stringp y) (string= x y)))
306 ((pathnamep x)
307 (and (pathnamep y) (pathname= x y)))
308 ((bit-vector-p x)
309 (and (bit-vector-p y)
310 (bit-vector-= x y)))
311 (t nil))))
312 ;; Use MAYBE-INLINE to get the inline expansion only once (instead
313 ;; of 200 times with INLINE). -- JES, 2005-12-30
314 (declare (maybe-inline equal-aux))
315 (equal-aux x y)))
317 ;;; EQUALP comparison of HASH-TABLE values
318 (defun hash-table-equalp (x y)
319 (declare (type hash-table x y))
320 (or (eq x y)
321 (and (hash-table-p y)
322 (eql (hash-table-count x) (hash-table-count y))
323 (eql (hash-table-test x) (hash-table-test y))
324 (block comparison-of-entries
325 (maphash (lambda (key x-value)
326 (multiple-value-bind (y-value y-value-p)
327 (gethash key y)
328 (unless (and y-value-p (equalp x-value y-value))
329 (return-from comparison-of-entries nil))))
331 t))))
333 (defun equalp (x y)
334 #+nil ; KLUDGE: If doc string, should be accurate: Talk about structures
335 ; and HASH-TABLEs.
336 "This is like EQUAL, except more liberal in several respects.
337 Numbers may be of different types, as long as the values are identical
338 after coercion. Characters may differ in alphabetic case. Vectors and
339 arrays must have identical dimensions and EQUALP elements, but may differ
340 in their type restriction."
341 (cond ((eq x y) t)
342 ((characterp x) (and (characterp y) (char-equal x y)))
343 ((numberp x) (and (numberp y) (= x y)))
344 ((consp x)
345 (and (consp y)
346 (equalp (car x) (car y))
347 (equalp (cdr x) (cdr y))))
348 ((pathnamep x)
349 (and (pathnamep y) (pathname= x y)))
350 ((hash-table-p x)
351 (and (hash-table-p y)
352 (hash-table-equalp x y)))
353 ((%instancep x)
354 (let ((layout-x (%instance-layout x)))
355 (and (%instancep y)
356 (eq layout-x (%instance-layout y))
357 ;; TODO: store one bit in the layout indicating whether EQUALP
358 ;; should scan slots. (basically a STRUCTURE-CLASSOID-P bit)
359 (structure-classoid-p (layout-classoid layout-x))
360 (macrolet ((slot-ref-equalp ()
361 `(let ((x-el (%instance-ref x i))
362 (y-el (%instance-ref y i)))
363 (or (eq x-el y-el) (equalp x-el y-el)))))
364 #!-interleaved-raw-slots
365 (let ((raw-len (layout-n-untagged-slots layout-x))
366 (total-len (layout-length layout-x)))
367 (and (dotimes (i (- total-len raw-len) t)
368 (unless (slot-ref-equalp)
369 (return nil)))
370 (or (zerop raw-len)
371 (raw-instance-slots-equalp layout-x x y))))
372 #!+interleaved-raw-slots
373 (let ((metadata (layout-untagged-bitmap layout-x)))
374 (if (zerop metadata)
375 (loop for i of-type index from sb!vm:instance-data-start
376 below (layout-length layout-x)
377 always (slot-ref-equalp))
378 (let ((comparators (layout-equalp-tests layout-x)))
379 (unless (= (length comparators)
380 (- (layout-length layout-x) sb!vm:instance-data-start))
381 (bug "EQUALP got incomplete instance layout"))
382 ;; See remark at the source code for %TARGET-DEFSTRUCT
383 ;; explaining how to use the vector of comparators.
384 (loop for i of-type index from sb!vm:instance-data-start
385 below (layout-length layout-x)
386 for test = (data-vector-ref
387 comparators (- i sb!vm:instance-data-start))
388 always (cond ((eql test 0) (slot-ref-equalp))
389 ((functionp test)
390 (funcall test i x y))
391 (t))))))))))
392 ((vectorp x)
393 (let ((length (length x)))
394 (and (vectorp y)
395 (= length (length y))
396 (dotimes (i length t)
397 (let ((x-el (aref x i))
398 (y-el (aref y i)))
399 (unless (or (eq x-el y-el)
400 (equalp x-el y-el))
401 (return nil)))))))
402 ((arrayp x)
403 (and (arrayp y)
404 (= (array-rank x) (array-rank y))
405 (dotimes (axis (array-rank x) t)
406 (unless (= (array-dimension x axis)
407 (array-dimension y axis))
408 (return nil)))
409 (dotimes (index (array-total-size x) t)
410 (let ((x-el (row-major-aref x index))
411 (y-el (row-major-aref y index)))
412 (unless (or (eq x-el y-el)
413 (equalp x-el y-el))
414 (return nil))))))
415 (t nil)))
417 (/show0 "about to do test cases in pred.lisp")
418 #!+sb-test
419 (let ((test-cases `((0.0 ,(load-time-value (make-unportable-float :single-float-negative-zero)) t)
420 (0.0 1.0 nil)
421 (#c(1 0) #c(1.0 0.0) t)
422 (#c(0 1) #c(0.0 1.0) t)
423 (#c(1.1 0.0) #c(11/10 0) nil) ; due to roundoff error
424 ("Hello" "hello" t)
425 ("Hello" #(#\h #\E #\l #\l #\o) t)
426 ("Hello" "goodbye" nil))))
427 (/show0 "TEST-CASES bound in pred.lisp")
428 (dolist (test-case test-cases)
429 (/show0 "about to do a TEST-CASE in pred.lisp")
430 (destructuring-bind (x y expected-result) test-case
431 (let* ((result (equalp x y))
432 (bresult (if result 1 0))
433 (expected-bresult (if expected-result 1 0)))
434 (unless (= bresult expected-bresult)
435 (/show0 "failing test in pred.lisp")
436 (error "failed test (EQUALP ~S ~S)" x y))))))
437 (/show0 "done with test cases in pred.lisp")