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