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!KERNEL")
14 ;;; Is X a fixnum in the target Lisp?
17 (<= sb
!xc
:most-negative-fixnum x sb
!xc
:most-positive-fixnum
)))
19 ;;; (This was a useful warning when trying to get bootstrapping
20 ;;; to work, but it's mostly irrelevant noise now that the system
22 (define-condition cross-type-style-warning
(style-warning)
24 :reader cross-type-style-warning-call
)
25 (message :reader cross-type-style-warning-message
26 #+cmu
:initarg
#+cmu
:message
; (to stop bogus non-STYLE WARNING)
28 (:report
(lambda (c s
)
31 "cross-compilation-time type ambiguity (should be OK) in ~S:~%~A"
32 (cross-type-style-warning-call c
)
33 (cross-type-style-warning-message c
)))))
35 ;;; This warning is issued when giving up on a type calculation where a
36 ;;; conservative answer is acceptable. Since a conservative answer is
37 ;;; acceptable, the only downside is lost optimization opportunities.
38 (define-condition cross-type-giving-up-conservatively
39 (cross-type-style-warning)
40 ((message :initform
"giving up conservatively"
41 #+cmu
:reader
#+cmu
#.
(gensym) ; (to stop bogus non-STYLE WARNING)
44 ;;; This warning refers to the flexibility in the ANSI spec with
45 ;;; regard to run-time distinctions between floating point types.
46 ;;; (E.g. the cross-compilation host might not even distinguish
47 ;;; between SINGLE-FLOAT and DOUBLE-FLOAT, so a DOUBLE-FLOAT number
48 ;;; would test positive as SINGLE-FLOAT.) If the target SBCL does make
49 ;;; this distinction, then information is lost. It's not too hard to
50 ;;; contrive situations where this would be a problem. In practice we
51 ;;; don't tend to run into them because all widely used Common Lisp
52 ;;; environments do recognize the distinction between SINGLE-FLOAT and
53 ;;; DOUBLE-FLOAT, and we don't really need the other distinctions
54 ;;; (e.g. between SHORT-FLOAT and SINGLE-FLOAT), so we call
55 ;;; WARN-POSSIBLE-CROSS-TYPE-FLOAT-INFO-LOSS to test at runtime
56 ;;; whether we need to worry about this at all, and not warn unless we
57 ;;; do. If we *do* have to worry about this at runtime, my (WHN
58 ;;; 19990808) guess is that the system will break in multiple places,
59 ;;; so this is a real WARNING, not just a STYLE-WARNING.
61 ;;; KLUDGE: If we ever try to support LONG-FLOAT or SHORT-FLOAT, this
62 ;;; situation will get a lot more complicated.
63 (defun warn-possible-cross-type-float-info-loss (call)
64 (when (or (subtypep 'single-float
'double-float
)
65 (subtypep 'double-float
'single-float
))
66 (warn "possible floating point information loss in ~S" call
)))
68 (defun sb!xc
:type-of
(object)
69 (labels (;; FIXME: This function is a no-op now that we no longer
70 ;; have a distinct package T%CL to translate
71 ;; for-the-target-Lisp CL symbols to, and should go away
73 (translate (expr) expr
))
74 (let ((raw-result (type-of object
)))
75 (cond ((or (subtypep raw-result
'float
)
76 (subtypep raw-result
'complex
))
77 (warn-possible-cross-type-float-info-loss
78 `(sb!xc
:type-of
,object
))
79 (translate raw-result
))
80 ((subtypep raw-result
'integer
)
81 (cond ((<= 0 object
1)
87 ((some (lambda (type) (subtypep raw-result type
))
88 '(array character list symbol
))
89 (translate raw-result
))
91 (error "can't handle TYPE-OF ~S in cross-compilation"))))))
93 ;;; Is SYMBOL in the CL package? Note that we're testing this on the
94 ;;; cross-compilation host, which could do things any old way. In
95 ;;; particular, it might be in the CL package even though
96 ;;; SYMBOL-PACKAGE is not (FIND-PACKAGE :CL). So we test things
98 (defun in-cl-package-p (symbol)
99 (eql (find-symbol (symbol-name symbol
) :cl
)
102 ;;; This is like TYPEP, except that it asks whether HOST-OBJECT would
103 ;;; be of TARGET-TYPE when instantiated on the target SBCL. Since this
104 ;;; is hard to determine in some cases, and since in other cases we
105 ;;; just haven't bothered to try, it needs to return two values, just
106 ;;; like SUBTYPEP: the first value for its conservative opinion (never
107 ;;; T unless it's certain) and the second value to tell whether it's
109 (defun cross-typep (host-object raw-target-type
)
110 (let ((target-type (type-expand raw-target-type
)))
111 (flet ((warn-and-give-up ()
112 ;; We don't have to keep track of this as long as system
113 ;; performance is acceptable, since giving up
114 ;; conservatively is a safe way out.
116 (warn 'cross-type-giving-up-conservatively
117 :call
`(cross-typep ,host-object
,raw-target-type
))
119 (warn-about-possible-float-info-loss ()
120 (warn-possible-cross-type-float-info-loss
121 `(cross-typep ,host-object
,raw-target-type
)))
122 ;; a convenient idiom for making more matches to special cases:
123 ;; Test both forms of target type for membership in LIST.
125 ;; (In order to avoid having to use too much deep knowledge
126 ;; of types, it's sometimes convenient to test RAW-TARGET-TYPE
127 ;; as well as the expanded type, since we can get matches with
128 ;; just EQL. E.g. SIMPLE-STRING can be matched with EQL, while
129 ;; safely matching its expansion,
130 ;; (OR (SIMPLE-ARRAY CHARACTER (*)) (SIMPLE-BASE-STRING *))
131 ;; would require logic clever enough to know that, e.g., OR is
133 (target-type-is-in (list)
134 (or (member raw-target-type list
)
135 (member target-type list
))))
136 (cond (;; Handle various SBCL-specific types which can't exist on
137 ;; the ANSI cross-compilation host. KLUDGE: This code will
138 ;; need to be tweaked by hand if the names of these types
140 (if (consp target-type
)
141 (member (car target-type
)
144 '(system-area-pointer
146 sb
!alien-internals
:alien-value
)))
148 (;; special case when TARGET-TYPE isn't a type spec, but
149 ;; instead a CLASS object
150 (typep target-type
'sb
!xc
::structure-class
)
151 ;; SBCL-specific types which have an analogue specially
152 ;; created on the host system
153 (if (sb!xc
:subtypep
(sb!xc
:class-name target-type
)
154 'sb
!kernel
::structure
!object
)
155 (values (typep host-object
(sb!xc
:class-name target-type
)) t
)
157 ((and (symbolp target-type
)
158 (find-class target-type nil
)
159 (subtypep target-type
'sb
!kernel
::structure
!object
))
160 (values (typep host-object target-type
) t
))
161 ((and (symbolp target-type
)
162 (sb!xc
:find-class target-type nil
)
163 (sb!xc
:subtypep target-type
'cl
:structure-object
)
164 (typep host-object
'(or symbol number list character
)))
166 (;; easy cases of arrays and vectors
168 '(array simple-string simple-vector string vector
))
169 (values (typep host-object target-type
) t
))
170 (;; general cases of vectors
171 (and (not (unknown-type-p (values-specifier-type target-type
)))
172 (sb!xc
:subtypep target-type
'cl
:vector
))
173 (if (vectorp host-object
)
174 (warn-and-give-up) ; general-case vectors being way too hard
175 (values nil t
))) ; but "obviously not a vector" being easy
176 (;; general cases of arrays
177 (and (not (unknown-type-p (values-specifier-type target-type
)))
178 (sb!xc
:subtypep target-type
'cl
:array
))
179 (if (arrayp host-object
)
180 (warn-and-give-up) ; general-case arrays being way too hard
181 (values nil t
))) ; but "obviously not an array" being easy
182 ((target-type-is-in '(*))
183 ;; KLUDGE: SBCL has * as an explicit wild type. While
184 ;; this is sort of logical (because (e.g. (ARRAY * 1)) is
185 ;; a valid type) it's not ANSI: looking at the ANSI
186 ;; definitions of complex types like like ARRAY shows
187 ;; that they consider * different from other type names.
188 ;; Someday we should probably get rid of this non-ANSIism
189 ;; in base SBCL, but until we do, we might as well here
190 ;; in the cross compiler. And in order to make sure that
191 ;; we don't continue doing it after we someday patch
192 ;; SBCL's type system so that * is no longer a type, we
193 ;; make this assertion. -- WHN 2001-08-08
194 (aver (typep (specifier-type '*) 'named-type
))
196 (;; Many simple types are guaranteed to correspond exactly
197 ;; between any host ANSI Common Lisp and the target
198 ;; Common Lisp. (Some array types are too, but they
199 ;; were picked off earlier.)
201 '(atom bit character complex cons float function integer keyword
202 list nil null number rational real signed-byte symbol t
204 (values (typep host-object target-type
) t
))
205 (;; Floating point types are guaranteed to correspond,
206 ;; too, but less exactly.
208 '(single-float double-float
))
209 (cond ((floatp host-object
)
210 (warn-about-possible-float-info-loss)
211 (values (typep host-object target-type
) t
))
214 (;; Complexes suffer the same kind of problems as arrays
215 (and (not (unknown-type-p (values-specifier-type target-type
)))
216 (sb!xc
:subtypep target-type
'cl
:complex
))
217 (if (complexp host-object
)
218 (warn-and-give-up) ; general-case complexes being way too hard
219 (values nil t
))) ; but "obviously not a complex" being easy
220 ;; Some types require translation between the cross-compilation
221 ;; host Common Lisp and the target SBCL.
222 ((target-type-is-in '(sb!xc
:class
))
223 (values (typep host-object
'sb
!xc
:class
) t
))
224 ((target-type-is-in '(fixnum))
225 (values (fixnump host-object
) t
))
226 ;; Some types are too hard to handle in the positive
227 ;; case, but at least we can be confident in a large
228 ;; fraction of the negative cases..
230 '(base-string simple-base-string simple-string
))
231 (if (stringp host-object
)
234 ((target-type-is-in '(character base-char
))
235 (cond ((typep host-object
'standard-char
)
237 ((not (characterp host-object
))
240 (warn-and-give-up))))
241 ((target-type-is-in '(stream instance
))
242 ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE
243 ;; is implemented as a STRUCTURE-OBJECT, so they'll fall
244 ;; through the tests above. We don't want to assume too
245 ;; much about them here, but at least we know enough
246 ;; about them to say that neither T nor NIL nor indeed
247 ;; any other symbol in the cross-compilation host is one.
248 ;; That knowledge suffices to answer so many of the
249 ;; questions that the cross-compiler asks that it's well
250 ;; worth special-casing it here.
251 (if (symbolp host-object
)
254 ;; various hacks for composite types..
256 (let ((first (first target-type
))
257 (rest (rest target-type
)))
259 ;; Many complex types are guaranteed to correspond exactly
260 ;; between any host ANSI Common Lisp and the target SBCL.
261 ((integer member mod rational real signed-byte unsigned-byte
)
262 (values (typep host-object target-type
) t
))
263 ;; Floating point types are guaranteed to correspond,
264 ;; too, but less exactly.
265 ((single-float double-float
)
266 (cond ((floatp host-object
)
267 (warn-about-possible-float-info-loss)
268 (values (typep host-object target-type
) t
))
271 ;; Some complex types have translations that are less
273 (and (every/type
#'cross-typep host-object rest
))
274 (or (any/type
#'cross-typep host-object rest
))
275 ;; If we want to work with the KEYWORD type, we need
276 ;; to grok (SATISFIES KEYWORDP).
278 (destructuring-bind (predicate-name) rest
279 (if (and (in-cl-package-p predicate-name
)
280 (fboundp predicate-name
))
281 ;; Many things like KEYWORDP, ODDP, PACKAGEP,
282 ;; and NULL correspond between host and target.
283 (values (not (null (funcall predicate-name
286 ;; For symbols not in the CL package, it's not
287 ;; in general clear how things correspond
288 ;; between host and target, so we punt.
289 (warn-and-give-up))))
290 ;; Some complex types are too hard to handle in the
291 ;; positive case, but at least we can be confident in
292 ;; a large fraction of the negative cases..
293 ((base-string simple-base-string simple-string
)
294 (if (stringp host-object
)
297 ((vector simple-vector
)
298 (if (vectorp host-object
)
301 ((array simple-array
)
302 (if (arrayp host-object
)
306 (if (functionp host-object
)
309 ;; And the Common Lisp type system is complicated,
310 ;; and we don't try to implement everything.
311 (otherwise (warn-and-give-up)))))
312 ;; And the Common Lisp type system is complicated, and
313 ;; we don't try to implement everything.
315 (warn-and-give-up))))))
317 ;;; This is an incomplete TYPEP which runs at cross-compile time to
318 ;;; tell whether OBJECT is the host Lisp representation of a target
319 ;;; SBCL type specified by TARGET-TYPE-SPEC. It need make no pretense
320 ;;; to completeness, since it need only handle the cases which arise
321 ;;; when building SBCL itself, e.g. testing that range limits FOO and
322 ;;; BAR in (INTEGER FOO BAR) are INTEGERs.
323 (defun sb!xc
:typep
(host-object target-type-spec
&optional
(env nil env-p
))
324 (declare (ignore env
))
325 (aver (null env-p
)) ; 'cause we're too lazy to think about it
326 (multiple-value-bind (opinion certain-p
)
327 (cross-typep host-object target-type-spec
)
328 ;; A program that calls TYPEP doesn't want uncertainty and
329 ;; probably can't handle it.
332 (error "uncertain in SB!XC:TYPEP ~S ~S"
336 ;;; This is an incomplete, portable implementation for use at
337 ;;; cross-compile time only.
338 (defun ctypep (obj ctype
)
339 (check-type ctype ctype
)
340 (let (;; the Common Lisp type specifier corresponding to CTYPE
341 (type (type-specifier ctype
)))
342 (check-type type
(or symbol cons
))
343 (cross-typep obj type
)))
348 (if (typep x
'generic-function
)
349 ;; Since at cross-compile time we build a CLOS-free bootstrap
350 ;; version of SBCL, it's unclear how to explain to it what a
351 ;; generic function is.
352 (error "not implemented: cross CTYPE-OF generic function")
353 ;; There's no ANSI way to find out what the function is
354 ;; declared to be, so we just return the CTYPE for the
355 ;; most-general function.
356 *universal-fun-type
*))
358 (make-member-type :members
(list x
)))
362 (let ((etype (specifier-type (array-element-type x
))))
363 (make-array-type :dimensions
(array-dimensions x
)
364 :complexp
(not (typep x
'simple-array
))
366 :specialized-element-type etype
)))
367 (cons (specifier-type 'cons
))
369 (cond ((typep x
'standard-char
)
370 ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
372 (sb!xc
:find-class
'base-char
))
373 ((not (characterp x
))
376 ;; Beyond this, there seems to be no portable correspondence.
377 (error "can't map host Lisp CHARACTER ~S to target Lisp" x
))))
379 (sb!xc
:find-class
(uncross (class-name (class-of x
)))))
381 ;; There might be more cases which we could handle with
382 ;; sufficient effort; since all we *need* to handle are enough
383 ;; cases for bootstrapping, we don't try to be complete here,. If
384 ;; future maintainers make the bootstrap code more complicated,
385 ;; they can also add new cases here to handle it. -- WHN 2000-11-11
386 (error "can't handle ~S in cross CTYPE-OF" x
))))