0.8alpha.0.8:
[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     (unknown-type
121      ;; dunno how to do this ANSIly -- WHN 19990413
122      #+sb-xc-host (error "stub: %%TYPEP UNKNOWN-TYPE in xcompilation host")
123      ;; Parse it again to make sure it's really undefined.
124      (let ((reparse (specifier-type (unknown-type-specifier type))))
125        (if (typep reparse 'unknown-type)
126            (error "unknown type specifier: ~S"
127                   (unknown-type-specifier reparse))
128            (%%typep object reparse))))
129     (negation-type
130      (not (%%typep object (negation-type-type type))))
131     (hairy-type
132      ;; Now the tricky stuff.
133      (let* ((hairy-spec (hairy-type-specifier type))
134             (symbol (car hairy-spec)))
135        (ecase symbol
136          (and
137           (every (lambda (spec) (%%typep object (specifier-type spec)))
138                  (rest hairy-spec)))
139          ;; Note: it should be safe to skip OR here, because union
140          ;; types can always be represented as UNION-TYPE in general
141          ;; or other CTYPEs in special cases; we never need to use
142          ;; HAIRY-TYPE for them.
143          (not
144           (unless (proper-list-of-length-p hairy-spec 2)
145             (error "invalid type specifier: ~S" hairy-spec))
146           (not (%%typep object (specifier-type (cadr hairy-spec)))))
147          (satisfies
148           (unless (proper-list-of-length-p hairy-spec 2)
149             (error "invalid type specifier: ~S" hairy-spec))
150           (values (funcall (symbol-function (cadr hairy-spec)) object))))))
151     (alien-type-type
152      (sb!alien-internals:alien-typep object (alien-type-type-alien-type type)))
153     (fun-type
154      (error "Function types are not a legal argument to TYPEP:~%  ~S"
155             (type-specifier type)))))
156
157 ;;; Do a type test from a class cell, allowing forward reference and
158 ;;; redefinition.
159 (defun classoid-cell-typep (obj-layout cell object)
160   (let ((classoid (classoid-cell-classoid cell)))
161     (unless classoid
162       (error "The class ~S has not yet been defined."
163              (classoid-cell-name cell)))
164     (classoid-typep obj-layout classoid object)))
165
166 ;;; Test whether OBJ-LAYOUT is from an instance of CLASSOID.
167 (defun classoid-typep (obj-layout classoid object)
168   (declare (optimize speed))
169   (when (layout-invalid obj-layout)
170     (if (and (typep (classoid-of object) 'standard-classoid) object)
171         (setq obj-layout (sb!pcl::check-wrapper-validity object))
172         (error "TYPEP was called on an obsolete object (was class ~S)."
173                (classoid-proper-name (layout-classoid obj-layout)))))
174   (let ((layout (classoid-layout classoid))
175         (obj-inherits (layout-inherits obj-layout)))
176     (when (layout-invalid layout)
177       (error "The class ~S is currently invalid." classoid))
178     (or (eq obj-layout layout)
179         (dotimes (i (length obj-inherits) nil)
180           (when (eq (svref obj-inherits i) layout)
181             (return t))))))
182
183 ;;; This implementation is a placeholder to use until PCL is set up,
184 ;;; at which time it will be overwritten by a real implementation.
185 (defun sb!pcl::check-wrapper-validity (object)
186   object)