1 ;;;; type-related stuff which exists only in the target SBCL runtime
3 ;;;; This software is part of the SBCL system. See the README file for
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.
12 (in-package "SB!KERNEL")
14 (!begin-collecting-cold-init-forms)
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.)
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)
29 "Return T iff OBJECT is of type TYPE."
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.
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
40 (defun ctypep (obj type)
41 (declare (type ctype type))
49 (values (%typep obj type) t))
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)
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))))))
65 (values (functionp obj) t))
69 (values (alien-typep obj (alien-type-type-alien-type type)) t))
71 ;; Now the tricky stuff.
72 (let* ((hairy-spec (hairy-type-specifier type))
73 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
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)))))))
84 (multiple-value-bind (res win)
85 (ctypep obj (specifier-type (cadr hairy-spec)))
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)))
100 ((and (symbolp fun) (fboundp fun))
101 (values (not (null (funcall fun obj))) t))
103 (values nil nil))))))))))
105 ;;; LAYOUT-OF -- Exported
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
113 #!-sb-fluid (declaim (inline layout-of))
115 (declare (optimize (speed 3) (safety 0)))
116 (cond ((typep x 'instance) (%instance-layout x))
117 ((funcallable-instance-p x) (%funcallable-instance-layout 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..
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)))))
131 #!-sb-fluid (declaim (inline sb!xc:class-of))
132 (defun sb!xc:class-of (object)
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)))
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)
143 (byte-function (byte-function-type fun))
144 (byte-closure (byte-function-type (byte-closure-function fun)))
146 (specifier-type (%function-type (%closure-function fun)))))))
148 ;;;; miscellaneous interfaces
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))))
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))
172 :init-wrapper !cold-init-forms)
176 (if (funcallable-instance-p x)
178 (extract-function-type x)))
180 (make-member-type :members (list x)))
182 (let* ((num (if (complexp x) (realpart x) x))
183 (res (make-numeric-type
184 :class (etypecase num
188 :format (if (floatp num)
189 (float-format-name num)
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))))
197 (setf (numeric-type-low res) num)
198 (setf (numeric-type-high res) num)))
201 (let ((etype (specifier-type (array-element-type x))))
202 (make-array-type :dimensions (array-dimensions x)
203 :complexp (not (typep x 'simple-array))
205 :specialized-element-type etype)))
207 (make-cons-type *universal-type* *universal-type*))
209 (sb!xc:class-of x))))
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*)
214 (!defun-from-collected-cold-init-forms !target-type-cold-init)