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