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
))
39 (values (%typep obj type
) t
))
41 (if (if (csubtypep type
(specifier-type 'function
))
42 (funcallable-instance-p obj
)
44 (if (eq (classoid-layout type
)
45 (info :type
:compiler-layout
(classoid-name type
)))
46 (values (sb!xc
:typep obj type
) t
)
50 (funcall (etypecase type
51 (intersection-type #'every
/type
)
52 (union-type #'any
/type
))
55 (compound-type-types type
)))
57 (values (functionp obj
) t
))
61 (values (alien-typep obj
(alien-type-type-alien-type type
)) t
))
63 (multiple-value-bind (res win
)
64 (ctypep obj
(negation-type-type 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
)))
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
)))))))
82 (multiple-value-bind (res win
)
83 (ctypep obj
(specifier-type (cadr hairy-spec
)))
88 ;; If the SATISFIES function is not foldable, we cannot answer!
89 (let* ((form `(,(second hairy-spec
) ',obj
)))
90 (multiple-value-bind (ok result
)
91 (sb!c
::constant-function-call-p form nil nil
)
92 (values (not (null result
)) ok
)))))))))
94 ;;; Return the layout for an object. This is the basic operation for
95 ;;; finding out the "type" of an object, and is used for generic
96 ;;; function dispatch. The standard doesn't seem to say as much as it
97 ;;; should about what this returns for built-in objects. For example,
98 ;;; it seems that we must return NULL rather than LIST when X is NIL
99 ;;; so that GF's can specialize on NULL.
100 #!-sb-fluid
(declaim (inline layout-of
))
102 (declare (optimize (speed 3) (safety 0)))
103 (cond ((%instancep x
) (%instance-layout x
))
104 ((funcallable-instance-p x
) (%funcallable-instance-layout x
))
106 ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
107 ;; I (WHN 19990209) replaced this with an expression evaluated at
108 ;; run time in order to make it easier to build the cross-compiler.
110 ;; KLUDGE: Since there's a DEFTRANSFORM for FIND-CLASSOID on
111 ;; constant names which creates non-cold-loadable code, we
112 ;; can't just use (CLASSOID-LAYOUT (FIND-CLASSOID 'NULL))
113 ;; here. The original (WHN 19991004) solution was to locally
114 ;; notinline FIND-CLASSOID. However, the full call to
115 ;; FIND-CLASSOID caused suboptimal register allocation in PCL
116 ;; dfuns. So instead we now use a special variable which is
117 ;; initialized during cold init. -- JES, 2006-07-04
118 *null-classoid-layout
*)
119 (t (svref *built-in-class-codes
* (widetag-of x
)))))
121 #!-sb-fluid
(declaim (inline classoid-of
))
122 (defun classoid-of (object)
124 "Return the class of the supplied object, which may be any Lisp object, not
125 just a CLOS STANDARD-OBJECT."
126 (layout-classoid (layout-of object
)))
128 ;;;; miscellaneous interfaces
130 ;;; Clear memoization of all type system operations that can be
131 ;;; altered by type definition/redefinition.
133 ;;; FIXME: This should be autogenerated.
134 (defun clear-type-caches ()
135 (declare (special *type-system-initialized
*))
136 (when *type-system-initialized
*
137 (dolist (sym '(values-specifier-type-cache-clear
138 values-type-union-cache-clear
139 type-union2-cache-clear
140 values-subtypep-cache-clear
141 csubtypep-cache-clear
142 type-intersection2-cache-clear
143 values-type-intersection-cache-clear
145 (funcall (the function
(symbol-function sym
)))))
148 ;;; This is like TYPE-OF, only we return a CTYPE structure instead of
149 ;;; a type specifier, and we try to return the type most useful for
150 ;;; type checking, rather than trying to come up with the one that the
151 ;;; user might find most informative.
152 (declaim (ftype (function (t) ctype
) ctype-of
))
153 (defun-cached (ctype-of
154 :hash-function
(lambda (x) (logand (sxhash x
) #x1FF
))
156 :init-wrapper
!cold-init-forms
)
160 (if (funcallable-instance-p x
)
162 (specifier-type (sb!impl
::%fun-type x
))))
164 (make-member-type :members
(list x
)))
168 (let ((etype (specifier-type (array-element-type x
))))
169 (make-array-type :dimensions
(array-dimensions x
)
170 :complexp
(not (typep x
'simple-array
))
172 :specialized-element-type etype
)))
174 (make-cons-type *universal-type
* *universal-type
*))
176 (specifier-type 'character
))
180 (!defun-from-collected-cold-init-forms
!target-type-cold-init
)