1 ;;;; cross-compiler-only versions of TYPEP, TYPE-OF, and related functions
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!IMPL")
14 ;;; (This was a useful warning when trying to get bootstrapping
15 ;;; to work, but it's mostly irrelevant noise now that the system
17 (define-condition cross-type-style-warning (style-warning)
19 :reader cross-type-style-warning-call)
20 (message :reader cross-type-style-warning-message
21 #+cmu :initarg #+cmu :message ; to stop bogus non-STYLE WARNING
23 (:report (lambda (c s)
26 "cross-compilation-time type ambiguity (should be OK) in ~S:~%~A"
27 (cross-type-style-warning-call c)
28 (cross-type-style-warning-message c)))))
30 ;;; This warning is issued when giving up on a type calculation where a
31 ;;; conservative answer is acceptable. Since a conservative answer is
32 ;;; acceptable, the only downside is lost optimization opportunities.
33 (define-condition cross-type-giving-up-conservatively
34 (cross-type-style-warning)
35 ((message :initform "giving up conservatively"
36 #+cmu :reader #+cmu #.(gensym) ; (to stop bogus non-STYLE WARNING)
39 ;;; This warning refers to the flexibility in the ANSI spec with
40 ;;; regard to run-time distinctions between floating point types.
41 ;;; (E.g. the cross-compilation host might not even distinguish
42 ;;; between SINGLE-FLOAT and DOUBLE-FLOAT, so a DOUBLE-FLOAT number
43 ;;; would test positive as SINGLE-FLOAT.) If the target SBCL does make
44 ;;; this distinction, then information is lost. It's not too hard to
45 ;;; contrive situations where this would be a problem. In practice we
46 ;;; don't tend to run into them because all widely used Common Lisp
47 ;;; environments do recognize the distinction between SINGLE-FLOAT and
48 ;;; DOUBLE-FLOAT, and we don't really need the other distinctions
49 ;;; (e.g. between SHORT-FLOAT and SINGLE-FLOAT), so we call
50 ;;; WARN-POSSIBLE-CROSS-TYPE-FLOAT-INFO-LOSS to test at runtime
51 ;;; whether we need to worry about this at all, and not warn unless we
52 ;;; do. If we *do* have to worry about this at runtime, my (WHN
53 ;;; 19990808) guess is that the system will break in multiple places,
54 ;;; so this is a real WARNING, not just a STYLE-WARNING.
56 ;;; KLUDGE: If we ever try to support LONG-FLOAT or SHORT-FLOAT, this
57 ;;; situation will get a lot more complicated.
58 (defun warn-possible-cross-type-float-info-loss (call)
59 (when (or (subtypep 'single-float 'double-float)
60 (subtypep 'double-float 'single-float))
61 (warn "possible floating point information loss in ~S" call)))
63 (defun sb!xc:type-of (object)
64 (labels (;; FIXME: This function is a no-op now that we no longer
65 ;; have a distinct package T%CL to translate
66 ;; for-the-target-Lisp CL symbols to, and should go away
68 (translate (expr) expr))
69 (let ((raw-result (type-of object)))
70 (cond ((or (subtypep raw-result 'float)
71 (subtypep raw-result 'complex))
72 (warn-possible-cross-type-float-info-loss
73 `(sb!xc:type-of ,object))
74 (translate raw-result))
75 ((subtypep raw-result 'integer)
76 (cond ((<= 0 object 1)
78 ((target-fixnump object)
82 ((some (lambda (type) (subtypep raw-result type))
83 '(array character list symbol))
84 (translate raw-result))
86 (error "can't handle TYPE-OF ~S in cross-compilation"))))))
88 ;;; Like TYPEP, but asks whether HOST-OBJECT would be of TARGET-TYPE
89 ;;; when instantiated on the target SBCL. Since this is hard to decide
90 ;;; in some cases, and since in other cases we just haven't bothered
91 ;;; to try, it needs to return two values, just like SUBTYPEP: the
92 ;;; first value for its conservative opinion (never T unless it's
93 ;;; certain) and the second value to tell whether it's certain.
94 (defun cross-typep (host-object target-type)
95 (flet ((warn-and-give-up ()
96 ;; We don't have to keep track of this as long as system performance
97 ;; is acceptable, since giving up conservatively is a safe way out.
99 (warn 'cross-type-giving-up-conservatively
100 :call `(cross-typep ,host-object ,target-type))
102 (warn-about-possible-float-info-loss ()
103 (warn-possible-cross-type-float-info-loss
104 `(cross-typep ,host-object ,target-type))))
105 (cond (;; Handle various SBCL-specific types which can't exist on the
106 ;; ANSI cross-compilation host. KLUDGE: This code will need to be
107 ;; tweaked by hand if the names of these types ever change, ugh!
108 (if (consp target-type)
109 (member (car target-type)
112 '(system-area-pointer
114 sb!alien-internals:alien-value)))
116 (;; special case when TARGET-TYPE isn't a type spec, but instead
118 (typep target-type 'sb!xc::structure-class)
119 ;; SBCL-specific types which have an analogue specially created
120 ;; on the host system
121 (if (sb!xc:subtypep (sb!xc:class-name target-type)
122 'sb!kernel::structure!object)
123 (values (typep host-object (sb!xc:class-name target-type)) t)
125 ((and (symbolp target-type)
126 (find-class target-type nil)
127 (subtypep target-type 'sb!kernel::structure!object))
128 (values (typep host-object target-type) t))
129 ((and (symbolp target-type)
130 (sb!xc:find-class target-type nil)
131 (sb!xc:subtypep target-type 'cl:structure-object)
132 (typep host-object '(or symbol number list character)))
134 (;; easy cases of arrays and vectors
136 '(array simple-string simple-vector string vector))
137 (values (typep host-object target-type) t))
138 (;; general cases of vectors
139 (and (not (unknown-type-p (values-specifier-type target-type)))
140 (sb!xc:subtypep target-type 'cl:vector))
141 (if (vectorp host-object)
142 (warn-and-give-up) ; general case of vectors being way too hard
143 (values nil t))) ; but "obviously not a vector" being easy
144 (;; general cases of arrays
145 (and (not (unknown-type-p (values-specifier-type target-type)))
146 (sb!xc:subtypep target-type 'cl:array))
147 (if (arrayp host-object)
148 (warn-and-give-up) ; general case of arrays being way too hard
149 (values nil t))) ; but "obviously not an array" being easy
151 (let ((first (first target-type))
152 (rest (rest target-type)))
154 ;; Many complex types are guaranteed to correspond exactly
155 ;; between any host ANSI Common Lisp and the target SBCL.
156 ((integer member mod rational real signed-byte unsigned-byte)
157 (values (typep host-object target-type) t))
158 ;; Floating point types are guaranteed to correspond, too, but
160 ((single-float double-float)
161 (cond ((floatp host-object)
162 (warn-about-possible-float-info-loss)
163 (values (typep host-object target-type) t))
166 ;; Some complex types have translations that are less trivial.
168 ;; Note: This could be implemented as a real test, just the way
169 ;; that OR is; I just haven't bothered. -- WHN 19990706
171 (or (let ((opinion nil)
174 (multiple-value-bind (sub-opinion sub-certain-p)
175 (cross-typep host-object i)
176 (cond (sub-opinion (setf opinion t
179 ((not sub-certain-p) (setf certain-p nil))))
182 (warn-and-give-up)))))
183 ;; Some complex types are too hard to handle in the positive
184 ;; case, but at least we can be confident in a large fraction of
185 ;; the negative cases..
186 ((base-string simple-base-string simple-string)
187 (if (stringp host-object)
190 ((vector simple-vector)
191 (if (vectorp host-object)
194 ((array simple-array)
195 (if (arrayp host-object)
199 (if (functionp host-object)
202 ;; And the Common Lisp type system is complicated, and we don't
203 ;; try to implement everything.
204 (otherwise (warn-and-give-up)))))
208 ;; KLUDGE: SBCL has * as an explicit wild type. While this is
209 ;; sort of logical (because (e.g. (ARRAY * 1)) is a valid type)
210 ;; it's not ANSI: looking at the ANSI definitions of complex
211 ;; types like like ARRAY shows that they consider * different
212 ;; from other type names. Someday we should probably get rid of
213 ;; this non-ANSIism in base SBCL, but until we do, we might as
214 ;; well here in the cross compiler. And in order to make sure
215 ;; that we don't continue doing it after we someday patch SBCL's
216 ;; type system so that * is no longer a type, we make this
218 (assert (typep (specifier-type '*) 'named-type))
220 ;; Many simple types are guaranteed to correspond exactly
221 ;; between any host ANSI Common Lisp and the target
222 ;; Common Lisp. (Some array types are too, but they
223 ;; were picked off earlier.)
224 ((bit character complex cons float function integer list nil
225 null number rational real signed-byte symbol t unsigned-byte)
226 (values (typep host-object target-type) t))
227 ;; Floating point types are guaranteed to correspond, too, but
229 ((single-float double-float)
230 (cond ((floatp host-object)
231 (warn-about-possible-float-info-loss)
232 (values (typep host-object target-type) t))
235 ;; Some types require translation between the cross-compilation
236 ;; host Common Lisp and the target SBCL.
237 (sb!xc:class (values (typep host-object 'sb!xc:class) t))
238 (fixnum (values (target-fixnump host-object) t))
239 ;; Some types are too hard to handle in the positive case, but at
240 ;; least we can be confident in a large fraction of the negative
242 ((base-string simple-base-string simple-string)
243 (if (stringp host-object)
246 ((character base-char)
247 (cond ((typep host-object 'standard-char)
249 ((not (characterp host-object))
252 (warn-and-give-up))))
254 ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE is
255 ;; implemented as a STRUCTURE-OBJECT, so they'll fall through the
256 ;; tests above. We don't want to assume too much about them here,
257 ;; but at least we know enough about them to say that neither T
258 ;; nor NIL nor indeed any other symbol in the cross-compilation
259 ;; host is one. That knowledge suffices to answer so many of the
260 ;; questions that the cross-compiler asks that it's well worth
261 ;; special-casing it here.
262 (if (symbolp host-object)
265 ;; And the Common Lisp type system is complicated, and we don't
266 ;; try to implement everything.
267 (otherwise (warn-and-give-up)))))))
269 ;;; An incomplete TYPEP which runs at cross-compile time to tell whether OBJECT
270 ;;; is the host Lisp representation of a target SBCL type specified by
271 ;;; TARGET-TYPE-SPEC. It need make no pretense to completeness, since it
272 ;;; need only handle the cases which arise when building SBCL itself, e.g.
273 ;;; testing that range limits FOO and BAR in (INTEGER FOO BAR) are INTEGERs.
274 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
275 (declare (ignore env))
276 (assert (null env-p)) ; 'cause we're too lazy to think about it
277 (multiple-value-bind (opinion certain-p)
278 (cross-typep host-object target-type-spec)
279 ;; A program that calls TYPEP doesn't want uncertainty and probably
283 (error "uncertain in SB!XC:TYPEP ~S ~S"
287 ;;; This implementation is an incomplete, portable version for use at
288 ;;; cross-compile time only.
289 (defun ctypep (obj ctype)
290 (check-type ctype ctype)
291 (let (;; the Common Lisp type specifier corresponding to CTYPE
292 (type (type-specifier ctype)))
293 (check-type type (or symbol cons))
294 (cross-typep obj type)))
296 (defparameter *universal-function-type*
297 (make-function-type :wild-args t
298 :returns *wild-type*))
303 (if (typep x 'generic-function)
304 ;; Since at cross-compile time we build a CLOS-free bootstrap
305 ;; version of SBCL, it's unclear how to explain to it what a
306 ;; generic function is.
307 (error "not implemented: cross CTYPE-OF generic function")
308 ;; There's no ANSI way to find out what the function is
309 ;; declared to be, so we just return the CTYPE for the
310 ;; most-general function.
311 *universal-function-type*))
313 (make-member-type :members (list x)))
315 (let* ((num (if (complexp x) (realpart x) x))
316 (res (make-numeric-type
317 :class (etypecase num
321 :format (if (floatp num)
322 (float-format-name num)
325 (setf (numeric-type-complexp res) :complex)
326 (let ((imag (imagpart x)))
327 (setf (numeric-type-low res) (min num imag))
328 (setf (numeric-type-high res) (max num imag))))
330 (setf (numeric-type-low res) num)
331 (setf (numeric-type-high res) num)))
334 (let ((etype (specifier-type (array-element-type x))))
335 (make-array-type :dimensions (array-dimensions x)
336 :complexp (not (typep x 'simple-array))
338 :specialized-element-type etype)))
339 (cons (specifier-type 'cons))
341 (cond ((typep x 'standard-char)
342 ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
344 (sb!xc:find-class 'base-char))
345 ((not (characterp x))
348 ;; Beyond this, there seems to be no portable correspondence.
349 (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
351 (sb!xc:find-class (uncross (class-name (class-of x)))))
353 ;; There might be more cases which we could handle with
354 ;; sufficient effort; since all we *need* to handle are enough
355 ;; cases for bootstrapping, we don't try to be complete here,. If
356 ;; future maintainers make the bootstrap code more complicated,
357 ;; they can also add new cases here to handle it. -- WHN 2000-11-11
358 (error "can't handle ~S in cross CTYPE-OF" x))))