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 '(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 ;; Some types require translation between the cross-compilation
215 ;; host Common Lisp and the target SBCL.
216 ((target-type-is-in '(sb!xc:class))
217 (values (typep host-object 'sb!xc:class) t))
218 ((target-type-is-in '(fixnum))
219 (values (fixnump host-object) t))
220 ;; Some types are too hard to handle in the positive
221 ;; case, but at least we can be confident in a large
222 ;; fraction of the negative cases..
224 '(base-string simple-base-string simple-string))
225 (if (stringp host-object)
228 ((target-type-is-in '(character base-char))
229 (cond ((typep host-object 'standard-char)
231 ((not (characterp host-object))
234 (warn-and-give-up))))
235 ((target-type-is-in '(stream instance))
236 ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE
237 ;; is implemented as a STRUCTURE-OBJECT, so they'll fall
238 ;; through the tests above. We don't want to assume too
239 ;; much about them here, but at least we know enough
240 ;; about them to say that neither T nor NIL nor indeed
241 ;; any other symbol in the cross-compilation host is one.
242 ;; That knowledge suffices to answer so many of the
243 ;; questions that the cross-compiler asks that it's well
244 ;; worth special-casing it here.
245 (if (symbolp host-object)
248 ;; various hacks for composite types..
250 (let ((first (first target-type))
251 (rest (rest target-type)))
253 ;; Many complex types are guaranteed to correspond exactly
254 ;; between any host ANSI Common Lisp and the target SBCL.
255 ((integer member mod rational real signed-byte unsigned-byte)
256 (values (typep host-object target-type) t))
257 ;; Floating point types are guaranteed to correspond,
258 ;; too, but less exactly.
259 ((single-float double-float)
260 (cond ((floatp host-object)
261 (warn-about-possible-float-info-loss)
262 (values (typep host-object target-type) t))
265 ;; Some complex types have translations that are less
267 (and (every/type #'cross-typep host-object rest))
268 (or (any/type #'cross-typep host-object rest))
269 ;; If we want to work with the KEYWORD type, we need
270 ;; to grok (SATISFIES KEYWORDP).
272 (destructuring-bind (predicate-name) rest
273 (if (and (in-cl-package-p predicate-name)
274 (fboundp predicate-name))
275 ;; Many things like KEYWORDP, ODDP, PACKAGEP,
276 ;; and NULL correspond between host and target.
277 (values (not (null (funcall predicate-name
280 ;; For symbols not in the CL package, it's not
281 ;; in general clear how things correspond
282 ;; between host and target, so we punt.
283 (warn-and-give-up))))
284 ;; Some complex types are too hard to handle in the
285 ;; positive case, but at least we can be confident in
286 ;; a large fraction of the negative cases..
287 ((base-string simple-base-string simple-string)
288 (if (stringp host-object)
291 ((vector simple-vector)
292 (if (vectorp host-object)
295 ((array simple-array)
296 (if (arrayp host-object)
300 (if (functionp host-object)
303 ;; And the Common Lisp type system is complicated,
304 ;; and we don't try to implement everything.
305 (otherwise (warn-and-give-up)))))
306 ;; And the Common Lisp type system is complicated, and
307 ;; we don't try to implement everything.
309 (warn-and-give-up))))))
311 ;;; This is an incomplete TYPEP which runs at cross-compile time to
312 ;;; tell whether OBJECT is the host Lisp representation of a target
313 ;;; SBCL type specified by TARGET-TYPE-SPEC. It need make no pretense
314 ;;; to completeness, since it need only handle the cases which arise
315 ;;; when building SBCL itself, e.g. testing that range limits FOO and
316 ;;; BAR in (INTEGER FOO BAR) are INTEGERs.
317 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
318 (declare (ignore env))
319 (aver (null env-p)) ; 'cause we're too lazy to think about it
320 (multiple-value-bind (opinion certain-p)
321 (cross-typep host-object target-type-spec)
322 ;; A program that calls TYPEP doesn't want uncertainty and
323 ;; probably can't handle it.
326 (error "uncertain in SB!XC:TYPEP ~S ~S"
330 ;;; This is an incomplete, portable implementation for use at
331 ;;; cross-compile time only.
332 (defun ctypep (obj ctype)
333 (check-type ctype ctype)
334 (let (;; the Common Lisp type specifier corresponding to CTYPE
335 (type (type-specifier ctype)))
336 (check-type type (or symbol cons))
337 (cross-typep obj type)))
342 (if (typep x 'generic-function)
343 ;; Since at cross-compile time we build a CLOS-free bootstrap
344 ;; version of SBCL, it's unclear how to explain to it what a
345 ;; generic function is.
346 (error "not implemented: cross CTYPE-OF generic function")
347 ;; There's no ANSI way to find out what the function is
348 ;; declared to be, so we just return the CTYPE for the
349 ;; most-general function.
350 *universal-fun-type*))
352 (make-member-type :members (list x)))
356 (let ((etype (specifier-type (array-element-type x))))
357 (make-array-type :dimensions (array-dimensions x)
358 :complexp (not (typep x 'simple-array))
360 :specialized-element-type etype)))
361 (cons (specifier-type 'cons))
363 (cond ((typep x 'standard-char)
364 ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
366 (sb!xc:find-class 'base-char))
367 ((not (characterp x))
370 ;; Beyond this, there seems to be no portable correspondence.
371 (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
373 (sb!xc:find-class (uncross (class-name (class-of x)))))
375 ;; There might be more cases which we could handle with
376 ;; sufficient effort; since all we *need* to handle are enough
377 ;; cases for bootstrapping, we don't try to be complete here,. If
378 ;; future maintainers make the bootstrap code more complicated,
379 ;; they can also add new cases here to handle it. -- WHN 2000-11-11
380 (error "can't handle ~S in cross CTYPE-OF" x))))