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.)
22 (defun typep (object type)
24 "Return T iff OBJECT is of type TYPE."
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.
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.
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))
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 (funcall (etypecase type
61 (intersection-type #'every/type)
62 (union-type #'any/type))
65 (compound-type-types type)))
67 (values (functionp obj) t))
71 (values (alien-typep obj (alien-type-type-alien-type type)) t))
73 ;; Now the tricky stuff.
74 (let* ((hairy-spec (hairy-type-specifier type))
75 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
80 (dolist (spec (cdr hairy-spec) (values t t))
81 (multiple-value-bind (res win)
82 (ctypep obj (specifier-type spec))
83 (unless win (return (values nil nil)))
84 (unless res (return (values nil t)))))))
86 (multiple-value-bind (res win)
87 (ctypep obj (specifier-type (cadr hairy-spec)))
92 (let ((predicate-name (second hairy-spec)))
93 (declare (type symbol predicate-name)) ; by ANSI spec of SATISFIES
94 (if (fboundp predicate-name)
95 (values (not (null (funcall predicate-name obj))) t)
96 (values nil nil)))))))))
98 ;;; Return the layout for an object. This is the basic operation for
99 ;;; finding out the "type" of an object, and is used for generic
100 ;;; function dispatch. The standard doesn't seem to say as much as it
101 ;;; should about what this returns for built-in objects. For example,
102 ;;; it seems that we must return NULL rather than LIST when X is NIL
103 ;;; so that GF's can specialize on NULL.
104 #!-sb-fluid (declaim (inline layout-of))
106 (declare (optimize (speed 3) (safety 0)))
107 (cond ((typep x 'instance) (%instance-layout x))
108 ((funcallable-instance-p x) (%funcallable-instance-layout x))
110 ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
111 ;; I (WHN 19990209) replaced this with an expression evaluated at
112 ;; run time in order to make it easier to build the cross-compiler.
113 ;; If it doesn't work, something else will be needed..
115 ;; KLUDGE: In order to really make this run at run time
116 ;; (instead of doing some weird broken thing at cold load
117 ;; time), we need to suppress a DEFTRANSFORM.. -- WHN 19991004
118 (declare (notinline sb!xc:find-class))
119 (class-layout (sb!xc:find-class 'null))))
120 (t (svref *built-in-class-codes* (get-type x)))))
122 #!-sb-fluid (declaim (inline sb!xc:class-of))
123 (defun sb!xc:class-of (object)
125 "Return the class of the supplied object, which may be any Lisp object, not
126 just a CLOS STANDARD-OBJECT."
127 (layout-class (layout-of object)))
129 ;;; Pull the type specifier out of a function object.
130 (defun extract-fun-type (fun)
131 (specifier-type (%simple-fun-type (%closure-fun fun))))
133 ;;;; miscellaneous interfaces
135 ;;; Clear memoization of all type system operations that can be
136 ;;; altered by type definition/redefinition.
137 (defun clear-type-caches ()
138 (when *type-system-initialized*
139 (dolist (sym '(values-specifier-type-cache-clear
140 values-type-union-cache-clear
141 type-union2-cache-clear
142 values-subtypep-cache-clear
143 csubtypep-cache-clear
144 type-intersection2-cache-clear
145 values-type-intersection-cache-clear))
146 (funcall (symbol-function sym))))
149 ;;; Like TYPE-OF, only we return a CTYPE structure instead of a type
150 ;;; specifier, and we try to return the type most useful for type
151 ;;; checking, rather than trying to come up with the one that the user
152 ;;; might find most informative.
153 (declaim (ftype (function (t) ctype) ctype-of))
154 (defun-cached (ctype-of
155 :hash-function (lambda (x) (logand (sxhash x) #x1FF))
157 :init-wrapper !cold-init-forms)
161 (if (funcallable-instance-p x)
163 (extract-fun-type x)))
165 (make-member-type :members (list x)))
169 (let ((etype (specifier-type (array-element-type x))))
170 (make-array-type :dimensions (array-dimensions x)
171 :complexp (not (typep x 'simple-array))
173 :specialized-element-type etype)))
175 (make-cons-type *universal-type* *universal-type*))
177 (sb!xc:class-of x))))
179 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
180 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
182 (!defun-from-collected-cold-init-forms !target-type-cold-init)