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