ca3c349108e1ab98978d637cf01cc86d692f9109
[sbcl.git] / src / code / typep.lisp
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.
9
10 (in-package "SB!KERNEL")
11
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))
26
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        ((nil) nil)))
43     (numeric-type
44      (and (numberp object)
45           (let (;; I think this works because of an invariant of the
46                 ;; two components of a COMPLEX are always coerced to
47                 ;; be the same, e.g. (COMPLEX 1.0 3/2) => #C(1.0 1.5).
48                 ;; Dunno why that holds, though -- ANSI? Python
49                 ;; tradition? marsh faerie spirits? -- WHN 2001-10-27
50                 (num (if (complexp object)
51                          (realpart object)
52                          object)))
53             (ecase (numeric-type-class type)
54               (integer (integerp num))
55               (rational (rationalp num))
56               (float
57                (ecase (numeric-type-format type)
58                  (short-float (typep num 'short-float))
59                  (single-float (typep num 'single-float))
60                  (double-float (typep num 'double-float))
61                  (long-float (typep num 'long-float))
62                  ((nil) (floatp num))))
63               ((nil) t)))
64           (flet ((bound-test (val)
65                    (let ((low (numeric-type-low type))
66                          (high (numeric-type-high type)))
67                      (and (cond ((null low) t)
68                                 ((listp low) (> val (car low)))
69                                 (t (>= val low)))
70                           (cond ((null high) t)
71                                 ((listp high) (< val (car high)))
72                                 (t (<= val high)))))))
73             (ecase (numeric-type-complexp type)
74               ((nil) t)
75               (:complex
76                (and (complexp object)
77                     (bound-test (realpart object))
78                     (bound-test (imagpart object))))
79               (:real
80                (and (not (complexp object))
81                     (bound-test object)))))))
82     (array-type
83      (and (arrayp object)
84           (ecase (array-type-complexp type)
85             ((t) (not (typep object 'simple-array)))
86             ((nil) (typep object 'simple-array))
87             ((:maybe) t))
88           (or (eq (array-type-dimensions type) '*)
89               (do ((want (array-type-dimensions type) (cdr want))
90                    (got (array-dimensions object) (cdr got)))
91                   ((and (null want) (null got)) t)
92                 (unless (and want got
93                              (or (eq (car want) '*)
94                                  (= (car want) (car got))))
95                   (return nil))))
96           (if (unknown-type-p (array-type-element-type type))
97               ;; better to fail this way than to get bogosities like
98               ;;   (TYPEP (MAKE-ARRAY 11) '(ARRAY SOME-UNDEFINED-TYPE)) => T
99               (error "~@<unknown element type in array type: ~2I~_~S~:>"
100                      (type-specifier type))
101               t)
102           (or (eq (array-type-element-type type) *wild-type*)
103               (values (type= (array-type-specialized-element-type type)
104                              (specifier-type (array-element-type
105                                               object)))))))
106     (member-type
107      (if (member object (member-type-members type)) t))
108     (classoid
109      #+sb-xc-host (ctypep object type)
110      #-sb-xc-host (classoid-typep (layout-of object) type object))
111     (union-type
112      (some (lambda (union-type-type) (%%typep object union-type-type))
113            (union-type-types type)))
114     (intersection-type
115      (every (lambda (intersection-type-type)
116               (%%typep object intersection-type-type))
117             (intersection-type-types type)))
118     (cons-type
119      (and (consp object)
120           (%%typep (car object) (cons-type-car-type type))
121           (%%typep (cdr object) (cons-type-cdr-type type))))
122     (character-set-type
123      (and (characterp object)
124          (let ((code (char-code object))
125                (pairs (character-set-type-pairs type)))
126            (dolist (pair pairs nil)
127              (destructuring-bind (low . high) pair
128                (when (<= low code high)
129                  (return t)))))))
130     (unknown-type
131      ;; dunno how to do this ANSIly -- WHN 19990413
132      #+sb-xc-host (error "stub: %%TYPEP UNKNOWN-TYPE in xcompilation host")
133      ;; Parse it again to make sure it's really undefined.
134      (let ((reparse (specifier-type (unknown-type-specifier type))))
135        (if (typep reparse 'unknown-type)
136            (error "unknown type specifier: ~S"
137                   (unknown-type-specifier reparse))
138            (%%typep object reparse))))
139     (negation-type
140      (not (%%typep object (negation-type-type type))))
141     (hairy-type
142      ;; Now the tricky stuff.
143      (let* ((hairy-spec (hairy-type-specifier type))
144             (symbol (car hairy-spec)))
145        (ecase symbol
146          (and
147           (every (lambda (spec) (%%typep object (specifier-type spec)))
148                  (rest hairy-spec)))
149          ;; Note: it should be safe to skip OR here, because union
150          ;; types can always be represented as UNION-TYPE in general
151          ;; or other CTYPEs in special cases; we never need to use
152          ;; HAIRY-TYPE for them.
153          (not
154           (unless (proper-list-of-length-p hairy-spec 2)
155             (error "invalid type specifier: ~S" hairy-spec))
156           (not (%%typep object (specifier-type (cadr hairy-spec)))))
157          (satisfies
158           (unless (proper-list-of-length-p hairy-spec 2)
159             (error "invalid type specifier: ~S" hairy-spec))
160           (values (funcall (symbol-function (cadr hairy-spec)) object))))))
161     (alien-type-type
162      (sb!alien-internals:alien-typep object (alien-type-type-alien-type type)))
163     (fun-type
164      (error "Function types are not a legal argument to TYPEP:~%  ~S"
165             (type-specifier type)))))
166
167 ;;; Do a type test from a class cell, allowing forward reference and
168 ;;; redefinition.
169 (defun classoid-cell-typep (obj-layout cell object)
170   (let ((classoid (classoid-cell-classoid cell)))
171     (unless classoid
172       (error "The class ~S has not yet been defined."
173              (classoid-cell-name cell)))
174     (classoid-typep obj-layout classoid object)))
175
176 ;;; Test whether OBJ-LAYOUT is from an instance of CLASSOID.
177 (defun classoid-typep (obj-layout classoid object)
178   (declare (optimize speed))
179   (multiple-value-bind (obj-layout layout)
180       (do ((layout (classoid-layout classoid) (classoid-layout classoid))
181            (i 0 (+ i 1))
182            (obj-layout obj-layout))
183           ((and (not (layout-invalid obj-layout))
184                 (not (layout-invalid layout)))
185            (values obj-layout layout))
186         (aver (< i 2))
187         (when (layout-invalid obj-layout)
188           (setq obj-layout (update-object-layout-or-invalid object layout)))
189         (ensure-classoid-valid classoid layout))
190     (let ((obj-inherits (layout-inherits obj-layout)))
191       (or (eq obj-layout layout)
192           (dotimes (i (length obj-inherits) nil)
193             (when (eq (svref obj-inherits i) layout)
194               (return t)))))))