0.6.11.26:
[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 ;;; Just call %TYPEP.
17 ;;;
18 ;;; Note that when cross-compiling, SB!XC:TYPEP is interpreted as
19 ;;; a test that the host Lisp object OBJECT translates to a target SBCL
20 ;;; type TYPE. (This behavior is needed e.g. to test for the validity of
21 ;;; numeric subtype bounds read when cross-compiling.)
22 (defun typep (object type)
23   #!+sb-doc
24   "Return T iff OBJECT is of type TYPE."
25   (%typep object type))
26
27 ;;; If TYPE is a type that we can do a compile-time test on, then
28 ;;; return whether the object is of that type as the first value and
29 ;;; second value true. Otherwise return NIL, NIL.
30 ;;;
31 ;;; We give up on unknown types and pick off FUNCTION- and COMPOUND-
32 ;;; types. For STRUCTURE- types, we require that the type be defined
33 ;;; in both the current and compiler environments, and that the
34 ;;; INCLUDES be the same.
35 ;;;
36 ;;; KLUDGE: This should probably be a type method instead of a big
37 ;;; ETYPECASE. But then the type method system should probably be CLOS
38 ;;; too, and until that happens wedging more stuff into it might be
39 ;;; messy. So I've left it a big ETYPECASE. -- 2001-03-16
40 (defun ctypep (obj type)
41   (declare (type ctype type))
42   (etypecase type
43     ((or numeric-type
44          named-type
45          member-type
46          array-type
47          sb!xc:built-in-class
48          cons-type)
49      (values (%typep obj type) t))
50     (sb!xc:class
51      (if (if (csubtypep type (specifier-type 'funcallable-instance))
52              (funcallable-instance-p obj)
53              (typep obj 'instance))
54          (if (eq (class-layout type)
55                  (info :type :compiler-layout (sb!xc:class-name type)))
56              (values (sb!xc:typep obj type) t)
57              (values nil nil))
58          (values nil t)))
59     (compound-type
60      (funcall (etypecase type
61                 (intersection-type #'every/type)
62                 (union-type #'any/type))
63               #'ctypep
64               obj
65               (compound-type-types type)))
66     (function-type
67      (values (functionp obj) t))
68     (unknown-type
69      (values nil nil))
70     (alien-type-type
71      (values (alien-typep obj (alien-type-type-alien-type type)) t))
72     (hairy-type
73      ;; Now the tricky stuff.
74      (let* ((hairy-spec (hairy-type-specifier type))
75             (symbol (if (consp hairy-spec) (car hairy-spec) hairy-spec)))
76        (ecase symbol
77          (and
78           (if (atom hairy-spec)
79               (values t t)
80               (dolist (spec (cdr hairy-spec) (values t t))
81                 (multiple-value-bind (res win)
82                     (ctypep obj (specifier-type spec))
83                   (unless win (return (values nil nil)))
84                   (unless res (return (values nil t)))))))
85          (not
86           (multiple-value-bind (res win)
87               (ctypep obj (specifier-type (cadr hairy-spec)))
88             (if win
89                 (values (not res) t)
90                 (values nil nil))))
91          (satisfies
92           (let ((predicate-name (second hairy-spec)))
93             (declare (type symbol predicate-name)) ; by ANSI spec of SATISFIES
94             (if (fboundp predicate-name)
95                 (values (not (null (funcall predicate-name obj))) t)
96                 (values nil nil)))))))))
97 \f
98 ;;; Return the layout for an object. This is the basic operation for
99 ;;; finding out the "type" of an object, and is used for generic
100 ;;; function dispatch. The standard doesn't seem to say as much as it
101 ;;; should about what this returns for built-in objects. For example,
102 ;;; it seems that we must return NULL rather than LIST when X is NIL
103 ;;; so that GF's can specialize on NULL.
104 #!-sb-fluid (declaim (inline layout-of))
105 (defun layout-of (x)
106   (declare (optimize (speed 3) (safety 0)))
107   (cond ((typep x 'instance) (%instance-layout x))
108         ((funcallable-instance-p x) (%funcallable-instance-layout x))
109         ((null x)
110          ;; Note: was #.((CLASS-LAYOUT (SB!XC:FIND-CLASS 'NULL))).
111          ;; I (WHN 19990209) replaced this with an expression evaluated at
112          ;; run time in order to make it easier to build the cross-compiler.
113          ;; If it doesn't work, something else will be needed..
114          (locally
115            ;; KLUDGE: In order to really make this run at run time
116            ;; (instead of doing some weird broken thing at cold load
117            ;; time), we need to suppress a DEFTRANSFORM.. -- WHN 19991004
118            (declare (notinline sb!xc:find-class))
119            (class-layout (sb!xc:find-class 'null))))
120         (t (svref *built-in-class-codes* (get-type x)))))
121
122 #!-sb-fluid (declaim (inline sb!xc:class-of))
123 (defun sb!xc:class-of (object)
124   #!+sb-doc
125   "Return the class of the supplied object, which may be any Lisp object, not
126    just a CLOS STANDARD-OBJECT."
127   (layout-class (layout-of object)))
128
129 ;;; Pull the type specifier out of a function object.
130 (defun extract-function-type (fun)
131   (if (sb!eval:interpreted-function-p fun)
132       (sb!eval:interpreted-function-type fun)
133       (typecase fun
134         (byte-function (byte-function-type fun))
135         (byte-closure (byte-function-type (byte-closure-function fun)))
136         (t
137          (specifier-type (%function-type (%closure-function fun)))))))
138 \f
139 ;;;; miscellaneous interfaces
140
141 ;;; Clear memoization of all type system operations that can be
142 ;;; altered by type definition/redefinition.
143 (defun clear-type-caches ()
144   (when *type-system-initialized*
145     (dolist (sym '(values-specifier-type-cache-clear
146                    values-type-union-cache-clear
147                    type-union2-cache-clear
148                    values-subtypep-cache-clear
149                    csubtypep-cache-clear
150                    type-intersection2-cache-clear
151                    values-type-intersection-cache-clear))
152       (funcall (symbol-function sym))))
153   (values))
154
155 ;;; Like TYPE-OF, only we return a CTYPE structure instead of a type
156 ;;; specifier, and we try to return the type most useful for type
157 ;;; checking, rather than trying to come up with the one that the user
158 ;;; might find most informative.
159 (declaim (ftype (function (t) ctype) ctype-of))
160 (defun-cached (ctype-of
161                :hash-function (lambda (x) (logand (sxhash x) #x1FF))
162                :hash-bits 9
163                :init-wrapper !cold-init-forms)
164               ((x eq))
165   (typecase x
166     (function
167      (if (funcallable-instance-p x)
168          (sb!xc:class-of x)
169          (extract-function-type x)))
170     (symbol
171      (make-member-type :members (list x)))
172     (number
173      (ctype-of-number x))
174     (array
175      (let ((etype (specifier-type (array-element-type x))))
176        (make-array-type :dimensions (array-dimensions x)
177                         :complexp (not (typep x 'simple-array))
178                         :element-type etype
179                         :specialized-element-type etype)))
180     (cons
181      (make-cons-type *universal-type* *universal-type*))
182     (t
183      (sb!xc:class-of x))))
184
185 ;;; Clear this cache on GC so that we don't hold onto too much garbage.
186 (pushnew 'ctype-of-cache-clear *before-gc-hooks*)
187 \f
188 (!defun-from-collected-cold-init-forms !target-type-cold-init)