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