0.6.8.15:
[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 ;;;
23 ;;; KLUDGE: In classic CMU CL this was wrapped in a (DECLAIM (START-BLOCK
24 ;;; TYPEP %TYPEP CLASS-CELL-TYPEP)) to make calls efficient. Once I straighten
25 ;;; out bootstrapping and cross-compiling issues it'd likely be a good idea to
26 ;;; do this again. -- WHN 19990413
27 (defun typep (object type)
28   #!+sb-doc
29   "Return T iff OBJECT is of type TYPE."
30   (%typep object type))
31
32 ;;; If TYPE is a type that we can do a compile-time test on, then
33 ;;; return whether the object is of that type as the first value and
34 ;;; second value true. Otherwise return NIL, NIL.
35 ;;;
36 ;;; We give up on unknown types and pick off FUNCTION and UNION types.
37 ;;; For structure types, we require that the type be defined in both
38 ;;; the current and compiler environments, and that the INCLUDES be
39 ;;; the same.
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     (union-type
60      (dolist (mem (union-type-types type) (values nil t))
61        (multiple-value-bind (val win) (ctypep obj mem)
62          (unless win (return (values nil nil)))
63          (when val (return (values t t))))))
64     (function-type
65      (values (functionp obj) t))
66     (unknown-type
67      (values nil nil))
68     (alien-type-type
69      (values (alien-typep obj (alien-type-type-alien-type type)) t))
70     (hairy-type
71      ;; Now the tricky stuff.
72      (let* ((hairy-spec (hairy-type-specifier type))
73             (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
74        (ecase symbol
75          (and
76           (if (atom hairy-spec)
77               (values t t)
78               (dolist (spec (cdr hairy-spec) (values t t))
79                 (multiple-value-bind (res win)
80                     (ctypep obj (specifier-type spec))
81                   (unless win (return (values nil nil)))
82                   (unless res (return (values nil t)))))))
83          (not
84           (multiple-value-bind (res win)
85               (ctypep obj (specifier-type (cadr hairy-spec)))
86             (if win
87                 (values (not res) t)
88                 (values nil nil))))
89          (satisfies
90           ;; KLUDGE: This stuff might well blow up if we tried to execute it
91           ;; when cross-compiling. But since for the foreseeable future the
92           ;; only code we'll try to cross-compile is SBCL itself, and SBCL is
93           ;; built without using SATISFIES types, it's arguably not important
94           ;; to worry about this. -- WHN 19990210.
95           (let ((fun (second hairy-spec)))
96             (cond ((and (consp fun)
97                         (eq (car fun) 'lambda))
98                    (values (not (null (funcall (coerce fun 'function) obj)))
99                            t))
100                   ((and (symbolp fun) (fboundp fun))
101                    (values (not (null (funcall fun obj))) t))
102                   (t
103                    (values nil nil))))))))))
104 \f
105 ;;; LAYOUT-OF  --  Exported
106 ;;;
107 ;;;    Return the layout for an object. This is the basic operation for
108 ;;; finding out the "type" of an object, and is used for generic function
109 ;;; dispatch. The standard doesn't seem to say as much as it should about what
110 ;;; this returns for built-in objects. For example, it seems that we must
111 ;;; return NULL rather than LIST when X is NIL so that GF's can specialize on
112 ;;; NULL.
113 #!-sb-fluid (declaim (inline layout-of))
114 (defun layout-of (x)
115   (declare (optimize (speed 3) (safety 0)))
116   (cond ((typep x 'instance) (%instance-layout x))
117         ((funcallable-instance-p x) (%funcallable-instance-layout x))
118         ((null x)
119          ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
120          ;; I (WHN 19990209) replaced this with an expression evaluated at
121          ;; run time in order to make it easier to build the cross-compiler.
122          ;; If it doesn't work, something else will be needed..
123          (locally
124            ;; KLUDGE: In order to really make it run at run time (instead of
125            ;; doing some weird broken thing at cold load time),
126            ;; we need to suppress a DEFTRANSFORM.. -- WHN 19991004
127            (declare (notinline sb!xc:find-class))
128            (class-layout (sb!xc:find-class 'null))))
129         (t (svref *built-in-class-codes* (get-type x)))))
130
131 #!-sb-fluid (declaim (inline sb!xc:class-of))
132 (defun sb!xc:class-of (object)
133   #!+sb-doc
134   "Return the class of the supplied object, which may be any Lisp object, not
135    just a CLOS STANDARD-OBJECT."
136   (layout-class (layout-of object)))
137
138 ;;; Pull the type specifier out of a function object.
139 (defun extract-function-type (fun)
140   (if (sb!eval:interpreted-function-p fun)
141       (sb!eval:interpreted-function-type fun)
142       (typecase fun
143         (byte-function (byte-function-type fun))
144         (byte-closure (byte-function-type (byte-closure-function fun)))
145         (t
146          (specifier-type (%function-type (%closure-function fun)))))))
147 \f
148 ;;;; miscellaneous interfaces
149
150 ;;; Clear memoization of all type system operations that can be
151 ;;; altered by type definition/redefinition.
152 (defun clear-type-caches ()
153   (when *type-system-initialized*
154     (dolist (sym '(values-specifier-type-cache-clear
155                    values-type-union-cache-clear
156                    type-union-cache-clear
157                    values-subtypep-cache-clear
158                    csubtypep-cache-clear
159                    type-intersection-cache-clear
160                    values-type-intersection-cache-clear))
161       (funcall (symbol-function sym))))
162   (values))
163
164 ;;; Like TYPE-OF, only we return a CTYPE structure instead of a type
165 ;;; specifier, and we try to return the type most useful for type
166 ;;; checking, rather than trying to come up with the one that the user
167 ;;; might find most informative.
168 (declaim (ftype (function (t) ctype) ctype-of))
169 (defun-cached (ctype-of
170                :hash-function (lambda (x) (logand (sxhash x) #x1FF))
171                :hash-bits 9
172                :init-wrapper !cold-init-forms)
173               ((x eq))
174   (typecase x
175     (function
176      (if (funcallable-instance-p x)
177          (sb!xc:class-of x)
178          (extract-function-type x)))
179     (symbol
180      (make-member-type :members (list x)))
181     (number
182      (let* ((num (if (complexp x) (realpart x) x))
183             (res (make-numeric-type
184                   :class (etypecase num
185                            (integer 'integer)
186                            (rational 'rational)
187                            (float 'float))
188                   :format (if (floatp num)
189                               (float-format-name num)
190                               nil))))
191        (cond ((complexp x)
192               (setf (numeric-type-complexp res) :complex)
193               (let ((imag (imagpart x)))
194                 (setf (numeric-type-low res) (min num imag))
195                 (setf (numeric-type-high res) (max num imag))))
196              (t
197               (setf (numeric-type-low res) num)
198               (setf (numeric-type-high res) num)))
199        res))
200     (array
201      (let ((etype (specifier-type (array-element-type x))))
202        (make-array-type :dimensions (array-dimensions x)
203                         :complexp (not (typep x 'simple-array))
204                         :element-type etype
205                         :specialized-element-type etype)))
206     (cons
207      (make-cons-type *universal-type* *universal-type*))
208     (t
209      (sb!xc:class-of x))))
210
211 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
212 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
213 \f
214 (!defun-from-collected-cold-init-forms !target-type-cold-init)