20945cffa00ed29c1ce3bc22282b0e0d0244c1e5
[sbcl.git] / src / code / target-type.lisp
1 ;;;; type-related stuff which exists only in the target SBCL runtime
2
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.
11
12 (in-package "SB!KERNEL")
13
14 (!begin-collecting-cold-init-forms)
15 \f
16 ;;; Just call %TYPEP.
17 ;;;
18 ;;; Note that when cross-compiling, SB!XC:TYPEP is interpreted as
19 ;;; a test that the host Lisp object OBJECT translates to a target SBCL
20 ;;; type TYPE. (This behavior is needed e.g. to test for the validity of
21 ;;; numeric subtype bounds read when cross-compiling.)
22 (defun typep (object type)
23   #!+sb-doc
24   "Return T iff OBJECT is of type TYPE."
25   (%typep object type))
26
27 ;;; If TYPE is a type that we can do a compile-time test on, then
28 ;;; return whether the object is of that type as the first value and
29 ;;; second value true. Otherwise return NIL, NIL.
30 ;;;
31 ;;; We give up on unknown types and pick off FUNCTION- and COMPOUND-
32 ;;; types. For STRUCTURE- types, we require that the type be defined
33 ;;; in both the current and compiler environments, and that the
34 ;;; INCLUDES be the same.
35 ;;;
36 ;;; KLUDGE: This should probably be a type method instead of a big
37 ;;; ETYPECASE. But then the type method system should probably be CLOS
38 ;;; too, and until that happens wedging more stuff into it might be
39 ;;; messy. So I've left it a big ETYPECASE. -- 2001-03-16
40 (defun ctypep (obj type)
41   (declare (type ctype type))
42   (etypecase type
43     ((or numeric-type
44          named-type
45          member-type
46          array-type
47          sb!xc:built-in-class
48          cons-type)
49      (values (%typep obj type) t))
50     (sb!xc:class
51      (if (if (csubtypep type (specifier-type 'funcallable-instance))
52              (funcallable-instance-p obj)
53              (typep obj 'instance))
54          (if (eq (class-layout type)
55                  (info :type :compiler-layout (sb!xc:class-name type)))
56              (values (sb!xc:typep obj type) t)
57              (values nil nil))
58          (values nil t)))
59     (compound-type
60      ;; REMOVEME: old version
61      #|
62      (let ((certain? t))
63        (etypecase type
64          (union-type
65           (dolist (mem (union-type-types type) (values nil certain?))
66             (multiple-value-bind (val win) (ctypep obj mem)
67               (if win
68                   (when val (return (values t t)))
69                   (setf certain? nil)))))
70          (intersection-type
71           (dolist (mem (intersection-type-types type)
72                        (if certain? (values t t) (values nil nil)))
73             (multiple-value-bind (val win) (ctypep obj mem)
74               (if win
75                   (unless val (return (values nil t)))
76                   (setf certain? nil)))))))
77      |#
78      (let ((types (compound-type-types type)))
79        (etypecase type
80          (intersection-type (every/type #'ctypep obj types))
81          (union-type        (any/type   #'ctypep obj types)))))
82     (function-type
83      (values (functionp obj) t))
84     (unknown-type
85      (values nil nil))
86     (alien-type-type
87      (values (alien-typep obj (alien-type-type-alien-type type)) t))
88     (hairy-type
89      ;; Now the tricky stuff.
90      (let* ((hairy-spec (hairy-type-specifier type))
91             (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
92        (ecase symbol
93          (and
94           (if (atom hairy-spec)
95               (values t t)
96               (dolist (spec (cdr hairy-spec) (values t t))
97                 (multiple-value-bind (res win)
98                     (ctypep obj (specifier-type spec))
99                   (unless win (return (values nil nil)))
100                   (unless res (return (values nil t)))))))
101          (not
102           (multiple-value-bind (res win)
103               (ctypep obj (specifier-type (cadr hairy-spec)))
104             (if win
105                 (values (not res) t)
106                 (values nil nil))))
107          (satisfies
108           (let ((predicate-name (second hairy-spec)))
109             (declare (type symbol predicate-name)) ; by ANSI spec of SATISFIES
110             (if (fboundp predicate-name)
111                 (values (not (null (funcall predicate-name obj))) t)
112                 (values nil nil)))))))))
113 \f
114 ;;; Return the layout for an object. This is the basic operation for
115 ;;; finding out the "type" of an object, and is used for generic
116 ;;; function dispatch. The standard doesn't seem to say as much as it
117 ;;; should about what this returns for built-in objects. For example,
118 ;;; it seems that we must return NULL rather than LIST when X is NIL
119 ;;; so that GF's can specialize on NULL.
120 #!-sb-fluid (declaim (inline layout-of))
121 (defun layout-of (x)
122   (declare (optimize (speed 3) (safety 0)))
123   (cond ((typep x 'instance) (%instance-layout x))
124         ((funcallable-instance-p x) (%funcallable-instance-layout x))
125         ((null x)
126          ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
127          ;; I (WHN 19990209) replaced this with an expression evaluated at
128          ;; run time in order to make it easier to build the cross-compiler.
129          ;; If it doesn't work, something else will be needed..
130          (locally
131            ;; KLUDGE: In order to really make it run at run time (instead of
132            ;; doing some weird broken thing at cold load time),
133            ;; we need to suppress a DEFTRANSFORM.. -- WHN 19991004
134            (declare (notinline sb!xc:find-class))
135            (class-layout (sb!xc:find-class 'null))))
136         (t (svref *built-in-class-codes* (get-type x)))))
137
138 #!-sb-fluid (declaim (inline sb!xc:class-of))
139 (defun sb!xc:class-of (object)
140   #!+sb-doc
141   "Return the class of the supplied object, which may be any Lisp object, not
142    just a CLOS STANDARD-OBJECT."
143   (layout-class (layout-of object)))
144
145 ;;; Pull the type specifier out of a function object.
146 (defun extract-function-type (fun)
147   (if (sb!eval:interpreted-function-p fun)
148       (sb!eval:interpreted-function-type fun)
149       (typecase fun
150         (byte-function (byte-function-type fun))
151         (byte-closure (byte-function-type (byte-closure-function fun)))
152         (t
153          (specifier-type (%function-type (%closure-function fun)))))))
154 \f
155 ;;;; miscellaneous interfaces
156
157 ;;; Clear memoization of all type system operations that can be
158 ;;; altered by type definition/redefinition.
159 (defun clear-type-caches ()
160   (when *type-system-initialized*
161     (dolist (sym '(values-specifier-type-cache-clear
162                    values-type-union-cache-clear
163                    type-union2-cache-clear
164                    values-subtypep-cache-clear
165                    csubtypep-cache-clear
166                    type-intersection2-cache-clear
167                    values-type-intersection-cache-clear))
168       (funcall (symbol-function sym))))
169   (values))
170
171 ;;; Like TYPE-OF, only we return a CTYPE structure instead of a type
172 ;;; specifier, and we try to return the type most useful for type
173 ;;; checking, rather than trying to come up with the one that the user
174 ;;; might find most informative.
175 (declaim (ftype (function (t) ctype) ctype-of))
176 (defun-cached (ctype-of
177                :hash-function (lambda (x) (logand (sxhash x) #x1FF))
178                :hash-bits 9
179                :init-wrapper !cold-init-forms)
180               ((x eq))
181   (typecase x
182     (function
183      (if (funcallable-instance-p x)
184          (sb!xc:class-of x)
185          (extract-function-type x)))
186     (symbol
187      (make-member-type :members (list x)))
188     (number
189      (let* ((num (if (complexp x) (realpart x) x))
190             (res (make-numeric-type
191                   :class (etypecase num
192                            (integer 'integer)
193                            (rational 'rational)
194                            (float 'float))
195                   :format (if (floatp num)
196                               (float-format-name num)
197                               nil))))
198        (cond ((complexp x)
199               (setf (numeric-type-complexp res) :complex)
200               (let ((imag (imagpart x)))
201                 (setf (numeric-type-low res) (min num imag))
202                 (setf (numeric-type-high res) (max num imag))))
203              (t
204               (setf (numeric-type-low res) num)
205               (setf (numeric-type-high res) num)))
206        res))
207     (array
208      (let ((etype (specifier-type (array-element-type x))))
209        (make-array-type :dimensions (array-dimensions x)
210                         :complexp (not (typep x 'simple-array))
211                         :element-type etype
212                         :specialized-element-type etype)))
213     (cons
214      (make-cons-type *universal-type* *universal-type*))
215     (t
216      (sb!xc:class-of x))))
217
218 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
219 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
220 \f
221 (!defun-from-collected-cold-init-forms !target-type-cold-init)