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