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)
16 ;;; If TYPE is a type that we can do a compile-time test on, then
17 ;;; return whether the object is of that type as the first value and
18 ;;; second value true. Otherwise return NIL, NIL.
20 ;;; We give up on unknown types and pick off FUNCTION- and COMPOUND-
21 ;;; types. For STRUCTURE- types, we require that the type be defined
22 ;;; in both the current and compiler environments, and that the
23 ;;; INCLUDES be the same.
25 ;;; KLUDGE: This should probably be a type method instead of a big
26 ;;; ETYPECASE. But then the type method system should probably be CLOS
27 ;;; too, and until that happens wedging more stuff into it might be
28 ;;; messy. So I've left it a big ETYPECASE. -- 2001-03-16
29 (defun ctypep (obj type)
30 (declare (type ctype type))
38 (values (%typep obj type) t))
40 (if (if (csubtypep type (specifier-type 'funcallable-instance))
41 (funcallable-instance-p obj)
42 (typep obj 'instance))
43 (if (eq (class-layout type)
44 (info :type :compiler-layout (sb!xc:class-name type)))
45 (values (sb!xc:typep obj type) t)
49 (funcall (etypecase type
50 (intersection-type #'every/type)
51 (union-type #'any/type))
54 (compound-type-types type)))
56 (values (functionp obj) t))
60 (values (alien-typep obj (alien-type-type-alien-type type)) t))
62 ;; Now the tricky stuff.
63 (let* ((hairy-spec (hairy-type-specifier type))
64 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
69 (dolist (spec (cdr hairy-spec) (values t t))
70 (multiple-value-bind (res win)
71 (ctypep obj (specifier-type spec))
72 (unless win (return (values nil nil)))
73 (unless res (return (values nil t)))))))
75 (multiple-value-bind (res win)
76 (ctypep obj (specifier-type (cadr hairy-spec)))
81 (let ((predicate-name (second hairy-spec)))
82 (declare (type symbol predicate-name)) ; by ANSI spec of SATISFIES
83 (if (fboundp predicate-name)
84 (values (not (null (funcall predicate-name obj))) t)
85 (values nil nil)))))))))
87 ;;; Return the layout for an object. This is the basic operation for
88 ;;; finding out the "type" of an object, and is used for generic
89 ;;; function dispatch. The standard doesn't seem to say as much as it
90 ;;; should about what this returns for built-in objects. For example,
91 ;;; it seems that we must return NULL rather than LIST when X is NIL
92 ;;; so that GF's can specialize on NULL.
93 #!-sb-fluid (declaim (inline layout-of))
95 (declare (optimize (speed 3) (safety 0)))
96 (cond ((typep x 'instance) (%instance-layout x))
97 ((funcallable-instance-p x) (%funcallable-instance-layout x))
99 ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
100 ;; I (WHN 19990209) replaced this with an expression evaluated at
101 ;; run time in order to make it easier to build the cross-compiler.
102 ;; If it doesn't work, something else will be needed..
104 ;; KLUDGE: In order to really make this run at run time
105 ;; (instead of doing some weird broken thing at cold load
106 ;; time), we need to suppress a DEFTRANSFORM.. -- WHN 19991004
107 (declare (notinline sb!xc:find-class))
108 (class-layout (sb!xc:find-class 'null))))
109 (t (svref *built-in-class-codes* (widetag-of x)))))
111 #!-sb-fluid (declaim (inline sb!xc:class-of))
112 (defun sb!xc:class-of (object)
114 "Return the class of the supplied object, which may be any Lisp object, not
115 just a CLOS STANDARD-OBJECT."
116 (layout-class (layout-of object)))
118 ;;; Pull the type specifier out of a function object.
119 (defun extract-fun-type (fun)
120 (specifier-type (%simple-fun-type (%closure-fun fun))))
122 ;;;; miscellaneous interfaces
124 ;;; Clear memoization of all type system operations that can be
125 ;;; altered by type definition/redefinition.
126 (defun clear-type-caches ()
127 (when *type-system-initialized*
128 (dolist (sym '(values-specifier-type-cache-clear
129 values-type-union-cache-clear
130 type-union2-cache-clear
131 values-subtypep-cache-clear
132 csubtypep-cache-clear
133 type-intersection2-cache-clear
134 values-type-intersection-cache-clear))
135 (funcall (symbol-function sym))))
138 ;;; Like TYPE-OF, only we return a CTYPE structure instead of a type
139 ;;; specifier, and we try to return the type most useful for type
140 ;;; checking, rather than trying to come up with the one that the user
141 ;;; might find most informative.
142 (declaim (ftype (function (t) ctype) ctype-of))
143 (defun-cached (ctype-of
144 :hash-function (lambda (x) (logand (sxhash x) #x1FF))
146 :init-wrapper !cold-init-forms)
150 (if (funcallable-instance-p x)
152 (extract-fun-type x)))
154 (make-member-type :members (list x)))
158 (let ((etype (specifier-type (array-element-type x))))
159 (make-array-type :dimensions (array-dimensions x)
160 :complexp (not (typep x 'simple-array))
162 :specialized-element-type etype)))
164 (make-cons-type *universal-type* *universal-type*))
166 (sb!xc:class-of x))))
168 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
169 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
171 (!defun-from-collected-cold-init-forms !target-type-cold-init)