0.7.8.7:
[sbcl/lichteblau.git] / src / code / target-type.lisp
blobd024ace63c366868eb133fcb6a922f0f8fe15260
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
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.
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.
19 ;;;
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.
24 ;;;
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))
31 (etypecase type
32 ((or numeric-type
33 named-type
34 member-type
35 array-type
36 sb!xc:built-in-class
37 cons-type)
38 (values (%typep obj type) t))
39 (sb!xc:class
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)
46 (values nil nil))
47 (values nil t)))
48 (compound-type
49 (funcall (etypecase type
50 (intersection-type #'every/type)
51 (union-type #'any/type))
52 #'ctypep
53 obj
54 (compound-type-types type)))
55 (fun-type
56 (values (functionp obj) t))
57 (unknown-type
58 (values nil nil))
59 (alien-type-type
60 (values (alien-typep obj (alien-type-type-alien-type type)) t))
61 (hairy-type
62 ;; Now the tricky stuff.
63 (let* ((hairy-spec (hairy-type-specifier type))
64 (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
65 (ecase symbol
66 (and
67 (if (atom hairy-spec)
68 (values t t)
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)))))))
74 (not
75 (multiple-value-bind (res win)
76 (ctypep obj (specifier-type (cadr hairy-spec)))
77 (if win
78 (values (not res) t)
79 (values nil nil))))
80 (satisfies
81 (let ((predicate-name (second hairy-spec)))
82 (declare (type symbol predicate-name)) ; by ANSI spec of SATISFIES
83 (if (fboundp predicate-name)
84 (let* (;; "Is OBJ of the SATISFIES type?" represented
85 ;; as a generalized boolean.
87 ;; (Why IGNORE-ERRORS? This code is used to try to
88 ;; check type relationships at compile time.
89 ;; Passing only-slightly-twisted types like
90 ;; (AND INTEGER (SATISFIES ODDP)) into the
91 ;; rather-significantly-twisted type dispatch
92 ;; system can easily give rise to oddities like
93 ;; calling predicates like ODDP on values they
94 ;; don't like. (E.g. on OBJ=#\NEWLINE when the
95 ;; above type is tested for TYPE= against
96 ;; STANDARD-CHAR, represented as a
97 ;; MEMBER-TYPE.) In such cases, NIL seems to be
98 ;; an appropriate answer to "is OBJ of the
99 ;; SATISFIES type?")
100 (gbool (ignore-errors (funcall predicate-name obj)))
101 ;; RAW coerced to a pure BOOLEAN value
102 (bool (not (not gbool))))
103 (values bool t))
104 (values nil nil)))))))))
106 ;;; Return the layout for an object. This is the basic operation for
107 ;;; finding out the "type" of an object, and is used for generic
108 ;;; function dispatch. The standard doesn't seem to say as much as it
109 ;;; should about what this returns for built-in objects. For example,
110 ;;; it seems that we must return NULL rather than LIST when X is NIL
111 ;;; so that GF's can specialize on NULL.
112 #!-sb-fluid (declaim (inline layout-of))
113 (defun layout-of (x)
114 (declare (optimize (speed 3) (safety 0)))
115 (cond ((typep x 'instance) (%instance-layout x))
116 ((funcallable-instance-p x) (%funcallable-instance-layout x))
117 ((null x)
118 ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
119 ;; I (WHN 19990209) replaced this with an expression evaluated at
120 ;; run time in order to make it easier to build the cross-compiler.
121 ;; If it doesn't work, something else will be needed..
122 (locally
123 ;; KLUDGE: In order to really make this run at run time
124 ;; (instead of doing some weird broken thing at cold load
125 ;; time), we need to suppress a DEFTRANSFORM.. -- WHN 19991004
126 (declare (notinline sb!xc:find-class))
127 (class-layout (sb!xc:find-class 'null))))
128 (t (svref *built-in-class-codes* (widetag-of x)))))
130 #!-sb-fluid (declaim (inline sb!xc:class-of))
131 (defun sb!xc:class-of (object)
132 #!+sb-doc
133 "Return the class of the supplied object, which may be any Lisp object, not
134 just a CLOS STANDARD-OBJECT."
135 (layout-class (layout-of object)))
137 ;;; Pull the type specifier out of a function object.
138 (defun extract-fun-type (fun)
139 (specifier-type (%simple-fun-type (%closure-fun fun))))
141 ;;;; miscellaneous interfaces
143 ;;; Clear memoization of all type system operations that can be
144 ;;; altered by type definition/redefinition.
145 (defun clear-type-caches ()
146 (when *type-system-initialized*
147 (dolist (sym '(values-specifier-type-cache-clear
148 values-type-union-cache-clear
149 type-union2-cache-clear
150 values-subtypep-cache-clear
151 csubtypep-cache-clear
152 type-intersection2-cache-clear
153 values-type-intersection-cache-clear))
154 (funcall (symbol-function sym))))
155 (values))
157 ;;; This is like TYPE-OF, only we return a CTYPE structure instead of
158 ;;; a type specifier, and we try to return the type most useful for
159 ;;; type checking, rather than trying to come up with the one that the
160 ;;; user might find most informative.
161 (declaim (ftype (function (t) ctype) ctype-of))
162 (defun-cached (ctype-of
163 :hash-function (lambda (x) (logand (sxhash x) #x1FF))
164 :hash-bits 9
165 :init-wrapper !cold-init-forms)
166 ((x eq))
167 (typecase x
168 (function
169 (if (funcallable-instance-p x)
170 (sb!xc:class-of x)
171 (extract-fun-type x)))
172 (symbol
173 (make-member-type :members (list x)))
174 (number
175 (ctype-of-number x))
176 (array
177 (let ((etype (specifier-type (array-element-type x))))
178 (make-array-type :dimensions (array-dimensions x)
179 :complexp (not (typep x 'simple-array))
180 :element-type etype
181 :specialized-element-type etype)))
182 (cons
183 (make-cons-type *universal-type* *universal-type*))
185 (sb!xc:class-of x))))
187 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
188 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
190 (!defun-from-collected-cold-init-forms !target-type-cold-init)