Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / cross-type.lisp
blob18d4727dd1b6f8ef10011643556e01c2d60155fc
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
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.
12 (in-package "SB!KERNEL")
14 ;;; Is X a fixnum in the target Lisp?
15 (defun fixnump (x)
16 (and (integerp x)
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
21 ;;; works.)
22 (define-condition cross-type-style-warning (style-warning)
23 ((call :initarg :call
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)
29 (format
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)
42 )))
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.
60 ;;;
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 (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 raw-result)
75 ((subtypep raw-result 'integer)
76 (cond ((<= 0 object 1)
77 'bit)
78 (;; We can't rely on the host's opinion of whether
79 ;; it's a FIXNUM, but instead test against target
80 ;; MOST-fooITIVE-FIXNUM limits.
81 (fixnump object)
82 'fixnum)
84 'integer)))
85 ((subtypep raw-result 'simple-string)
86 `(simple-base-string ,(length object)))
87 ((subtypep raw-result 'string) 'base-string)
88 ((some (lambda (type) (subtypep raw-result type))
89 '(array character list symbol))
90 raw-result)
92 (error "can't handle TYPE-OF ~S in cross-compilation" object)))))
94 ;;; Is SYMBOL in the CL package? Note that we're testing this on the
95 ;;; cross-compilation host, which could do things any old way. In
96 ;;; particular, it might be in the CL package even though
97 ;;; SYMBOL-PACKAGE is not (FIND-PACKAGE :CL). So we test things
98 ;;; another way.
99 (defun in-cl-package-p (symbol)
100 (eql (find-symbol (symbol-name symbol) :cl)
101 symbol))
103 ;; Return T if SYMBOL is a predicate acceptable for use in a SATISFIES type
104 ;; specifier. We assume that anything in CL: is allowed (see explanation at
105 ;; call point), and beyond that, anything we define has to be expressly listed
106 ;; here, for fear of later unexpected confusion.
107 (defun acceptable-cross-typep-pred (symbol)
108 (and (fboundp symbol)
109 (or (in-cl-package-p symbol)
110 ;; KLUDGE: rather than extensible list of predicates that match
111 ;; in behavior between the host and target lisp, hardcode a few.
112 (memq symbol '(sb!vm:static-symbol-p
113 sb!vm::wired-tls-symbol-p)))))
115 ;;; This is like TYPEP, except that it asks whether HOST-OBJECT would
116 ;;; be of TARGET-TYPE when instantiated on the target SBCL. Since this
117 ;;; is hard to determine in some cases, and since in other cases we
118 ;;; just haven't bothered to try, it needs to return two values, just
119 ;;; like SUBTYPEP: the first value for its conservative opinion (never
120 ;;; T unless it's certain) and the second value to tell whether it's
121 ;;; certain.
122 (defun cross-typep (host-object raw-target-type)
123 (let ((target-type (typexpand raw-target-type)))
124 (flet ((warn-and-give-up ()
125 ;; We don't have to keep track of this as long as system
126 ;; performance is acceptable, since giving up
127 ;; conservatively is a safe way out.
128 #+nil
129 (warn 'cross-type-giving-up-conservatively
130 :call `(cross-typep ,host-object ,raw-target-type))
131 (values nil nil))
132 (warn-about-possible-float-info-loss ()
133 (warn-possible-cross-type-float-info-loss
134 `(cross-typep ,host-object ,raw-target-type)))
135 ;; a convenient idiom for making more matches to special cases:
136 ;; Test both forms of target type for membership in LIST.
138 ;; (In order to avoid having to use too much deep knowledge
139 ;; of types, it's sometimes convenient to test RAW-TARGET-TYPE
140 ;; as well as the expanded type, since we can get matches with
141 ;; just EQL. E.g. SIMPLE-STRING can be matched with EQL, while
142 ;; safely matching its expansion,
143 ;; (OR (SIMPLE-ARRAY CHARACTER (*)) (SIMPLE-BASE-STRING *))
144 ;; would require logic clever enough to know that, e.g., OR is
145 ;; commutative.)
146 (target-type-is-in (list)
147 (or (member raw-target-type list)
148 (member target-type list))))
149 (cond (;; Handle various SBCL-specific types which can't exist on
150 ;; the ANSI cross-compilation host. KLUDGE: This code will
151 ;; need to be tweaked by hand if the names of these types
152 ;; ever change, ugh!
153 (if (consp target-type)
154 (member (car target-type)
155 '(alien))
156 (member target-type
157 '(system-area-pointer
158 sb!alien-internals:alien-value)))
159 (values nil t))
160 (;; special case when TARGET-TYPE isn't a type spec, but
161 ;; instead a CLASS object.
162 (typep target-type 'class)
163 (bug "We don't support CROSS-TYPEP of CLASS type specifiers"))
164 ((and (symbolp target-type)
165 (find-classoid target-type nil)
166 (sb!xc:subtypep target-type 'cl:structure-object)
167 (typep host-object '(or symbol number list character)))
168 (values nil t))
169 ((and (symbolp target-type)
170 (find-class target-type nil)
171 (subtypep target-type 'structure!object))
172 (values (typep host-object target-type) t))
173 (;; easy cases of arrays and vectors
174 (target-type-is-in
175 '(array simple-string simple-vector string vector))
176 (values (typep host-object target-type) t))
177 (;; sequence is not guaranteed to be an exhaustive
178 ;; partition, but it includes at least lists and vectors.
179 (target-type-is-in '(sequence))
180 (if (or (vectorp host-object) (listp host-object))
181 (values t t)
182 (if (typep host-object target-type)
183 (warn-and-give-up)
184 (values nil t))))
185 (;; general cases of vectors
186 (and (not (hairy-type-p (values-specifier-type target-type)))
187 (sb!xc:subtypep target-type 'cl:vector))
188 (if (vectorp host-object)
189 (warn-and-give-up) ; general-case vectors being way too hard
190 (values nil t))) ; but "obviously not a vector" being easy
191 (;; general cases of arrays
192 (and (not (hairy-type-p (values-specifier-type target-type)))
193 (sb!xc:subtypep target-type 'cl:array))
194 (if (arrayp host-object)
195 (warn-and-give-up) ; general-case arrays being way too hard
196 (values nil t))) ; but "obviously not an array" being easy
197 ((target-type-is-in '(*))
198 ;; KLUDGE: SBCL has * as an explicit wild type. While
199 ;; this is sort of logical (because (e.g. (ARRAY * 1)) is
200 ;; a valid type) it's not ANSI: looking at the ANSI
201 ;; definitions of complex types like like ARRAY shows
202 ;; that they consider * different from other type names.
203 ;; Someday we should probably get rid of this non-ANSIism
204 ;; in base SBCL, but until we do, we might as well here
205 ;; in the cross compiler. And in order to make sure that
206 ;; we don't continue doing it after we someday patch
207 ;; SBCL's type system so that * is no longer a type, we
208 ;; make this assertion. -- WHN 2001-08-08
209 (aver (typep (values-specifier-type '*) 'named-type))
210 (values t t))
211 (;; Many simple types are guaranteed to correspond exactly
212 ;; between any host ANSI Common Lisp and the target
213 ;; Common Lisp. (Some array types are too, but they
214 ;; were picked off earlier.)
215 (target-type-is-in
216 '(atom bit character complex cons float function integer keyword
217 list nil null number rational real signed-byte symbol t
218 unsigned-byte))
219 (values (typep host-object target-type) t))
220 (;; Floating point types are guaranteed to correspond,
221 ;; too, but less exactly.
222 (target-type-is-in
223 '(single-float double-float))
224 (cond ((floatp host-object)
225 (warn-about-possible-float-info-loss)
226 (values (typep host-object target-type) t))
228 (values nil t))))
229 (;; Complexes suffer the same kind of problems as arrays.
230 ;; Our dumping logic is based on contents, however, so
231 ;; reasoning about them should be safe
232 (and (not (hairy-type-p (values-specifier-type target-type)))
233 (sb!xc:subtypep target-type 'cl:complex))
234 (if (complexp host-object)
235 (let ((re (realpart host-object))
236 (im (imagpart host-object)))
237 (if (or (and (eq target-type 'complex)
238 (typep re 'rational) (typep im 'rational))
239 (and (equal target-type '(cl:complex single-float))
240 (typep re 'single-float) (typep im 'single-float))
241 (and (equal target-type '(cl:complex double-float))
242 (typep re 'double-float) (typep im 'double-float)))
243 (values t t)
244 (progn
245 ;; We won't know how to dump it either.
246 (warn "Host complex too complex: ~S" host-object)
247 (warn-and-give-up))))
248 (values nil t)))
249 ;; Some types require translation between the cross-compilation
250 ;; host Common Lisp and the target SBCL.
251 ((target-type-is-in '(classoid))
252 (values (typep host-object 'classoid) t))
253 ((target-type-is-in '(fixnum))
254 (values (fixnump host-object) t))
255 ((target-type-is-in '(bignum))
256 (values (and (integerp host-object) (not (fixnump host-object)))
258 ;; Some types are too hard to handle in the positive
259 ;; case, but at least we can be confident in a large
260 ;; fraction of the negative cases..
261 ((target-type-is-in
262 '(base-string simple-base-string simple-string))
263 (if (stringp host-object)
264 (warn-and-give-up)
265 (values nil t)))
266 ((target-type-is-in '(character base-char standard-char))
267 (cond ((typep host-object 'standard-char)
268 (values t t))
269 ((not (characterp host-object))
270 (values nil t))
272 (warn-and-give-up))))
273 ((target-type-is-in '(stream instance))
274 ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE
275 ;; is implemented as a STRUCTURE-OBJECT, so they'll fall
276 ;; through the tests above. We don't want to assume too
277 ;; much about them here, but at least we know enough
278 ;; about them to say that neither T nor NIL nor indeed
279 ;; any other symbol in the cross-compilation host is one.
280 ;; That knowledge suffices to answer so many of the
281 ;; questions that the cross-compiler asks that it's well
282 ;; worth special-casing it here.
283 (if (symbolp host-object)
284 (values nil t)
285 (warn-and-give-up)))
286 ;; various hacks for composite types..
287 ((consp target-type)
288 (let ((first (first target-type))
289 (rest (rest target-type)))
290 (case first
291 ;; Many complex types are guaranteed to correspond exactly
292 ;; between any host ANSI Common Lisp and the target SBCL.
293 ((integer member mod rational real signed-byte unsigned-byte)
294 (values (typep host-object target-type) t))
295 ;; Floating point types are guaranteed to correspond,
296 ;; too, but less exactly.
297 ((single-float double-float)
298 (cond ((floatp host-object)
299 (warn-about-possible-float-info-loss)
300 (values (typep host-object target-type) t))
302 (values nil t))))
303 ;; Some complex types have translations that are less
304 ;; trivial.
305 (and (every/type #'cross-typep host-object rest))
306 (or (any/type #'cross-typep host-object rest))
307 (not
308 (multiple-value-bind (value surep)
309 (cross-typep host-object (car rest))
310 (if surep
311 (values (not value) t)
312 (warn-and-give-up))))
313 ;; If we want to work with the KEYWORD type, we need
314 ;; to grok (SATISFIES KEYWORDP).
315 (satisfies
316 (destructuring-bind (predicate-name) rest
317 (if (acceptable-cross-typep-pred predicate-name)
318 ;; Many predicates like KEYWORDP, ODDP, PACKAGEP,
319 ;; and NULL correspond between host and target.
320 ;; But we still need to handle errors, because
321 ;; the code which calls us may not understand
322 ;; that a type is unreachable. (E.g. when compiling
323 ;; (AND STRING (SATISFIES ARRAY-HAS-FILL-POINTER-P))
324 ;; CTYPEP may be called on the SATISFIES expression
325 ;; even for non-STRINGs.)
326 (multiple-value-bind (result error?)
327 (ignore-errors (funcall predicate-name
328 host-object))
329 (if error?
330 (values nil nil)
331 (values result t)))
332 ;; For symbols not in the CL package, it's not
333 ;; in general clear how things correspond
334 ;; between host and target, so we punt.
335 (warn-and-give-up))))
336 ;; Some complex types are too hard to handle in the
337 ;; positive case, but at least we can be confident in
338 ;; a large fraction of the negative cases..
339 ((base-string simple-base-string simple-string)
340 (if (stringp host-object)
341 (warn-and-give-up)
342 (values nil t)))
343 ((vector simple-vector)
344 (if (vectorp host-object)
345 (warn-and-give-up)
346 (values nil t)))
347 ((array simple-array)
348 (if (arrayp host-object)
349 (warn-and-give-up)
350 (values nil t)))
351 (function
352 (if (functionp host-object)
353 (warn-and-give-up)
354 (values nil t)))
355 ;; And the Common Lisp type system is complicated,
356 ;; and we don't try to implement everything.
357 (otherwise (warn-and-give-up)))))
358 ;; And the Common Lisp type system is complicated, and
359 ;; we don't try to implement everything.
361 (warn-and-give-up))))))
363 ;;; This is an incomplete TYPEP which runs at cross-compile time to
364 ;;; tell whether OBJECT is the host Lisp representation of a target
365 ;;; SBCL type specified by TARGET-TYPE-SPEC. It need make no pretense
366 ;;; to completeness, since it need only handle the cases which arise
367 ;;; when building SBCL itself, e.g. testing that range limits FOO and
368 ;;; BAR in (INTEGER FOO BAR) are INTEGERs.
369 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
370 (declare (ignore env))
371 (aver (null env-p)) ; 'cause we're too lazy to think about it
372 (multiple-value-bind (opinion certain-p)
373 (cross-typep host-object target-type-spec)
374 ;; A program that calls TYPEP doesn't want uncertainty and
375 ;; probably can't handle it.
376 (if certain-p
377 opinion
378 (error "uncertain in SB!XC:TYPEP ~S ~S"
379 host-object
380 target-type-spec))))
382 ;;; This is an incomplete, portable implementation for use at
383 ;;; cross-compile time only.
384 (defun ctypep (obj ctype)
385 (check-type ctype ctype)
386 ;; There is at least one possible endless recursion in the
387 ;; cross-compiler type system: (SUBTYPEP NULL (OR UNKOWN0 UNKNOWN1)
388 ;; runs out of stack. The right way would probably be to not
389 ;; implement CTYPEP in terms of TYPE-SPECIFIER (:UNPARSE, that may
390 ;; call TYPE=, that in turn may call CTYPEP). Until then, pick a few
391 ;; cherries off.
392 (cond ((member-type-p ctype)
393 (if (member-type-member-p obj ctype)
394 (values t t)
395 (values nil t)))
396 ((union-type-p ctype)
397 (any/type #'ctypep obj (union-type-types ctype)))
398 ((array-type-p ctype)
399 ;; This is essentially just the ARRAY-TYPE case of %%TYPEP
400 ;; using !SPECIALIZED-ARRAY-ELEMENT-TYPE, not ARRAY-ELEMENT-TYPE.
401 (if (and (arrayp obj)
402 (case (array-type-complexp ctype)
403 ((t) (not (typep obj 'simple-array)))
404 ((nil) (typep obj 'simple-array)))
405 (or (eq (array-type-element-type ctype) *wild-type*)
406 (type= (specifier-type
407 (!specialized-array-element-type obj))
408 (array-type-specialized-element-type ctype)))
409 (or (eq (array-type-dimensions ctype) '*)
410 (and (= (length (array-type-dimensions ctype))
411 (array-rank obj)))
412 (every (lambda (required actual)
413 (or (eq required '*) (eql required actual)))
414 (array-type-dimensions ctype)
415 (array-dimensions obj))))
416 (values t t)
417 (values nil t)))
419 (let ( ;; the Common Lisp type specifier corresponding to CTYPE
420 (type (type-specifier ctype)))
421 (check-type type (or symbol cons))
422 (cross-typep obj type)))))
424 (defun ctype-of (x)
425 (typecase x
426 (function
427 (if (typep x 'generic-function)
428 ;; Since at cross-compile time we build a CLOS-free bootstrap
429 ;; version of SBCL, it's unclear how to explain to it what a
430 ;; generic function is.
431 (error "not implemented: cross CTYPE-OF generic function")
432 ;; There's no ANSI way to find out what the function is
433 ;; declared to be, so we just return the CTYPE for the
434 ;; most-general function.
435 *universal-fun-type*))
436 (symbol
437 (make-member-type :members (list x)))
438 (number
439 (ctype-of-number x))
440 (array
441 ;; It is critical not to inquire of the host for the array's element type.
442 (let ((etype (specifier-type (!specialized-array-element-type x))))
443 (make-array-type (array-dimensions x)
444 ;; complexp relies on the host implementation,
445 ;; but in practice any array for which we need to
446 ;; call ctype-of will be a simple-array.
447 :complexp (not (typep x 'simple-array))
448 :element-type etype
449 :specialized-element-type etype)))
450 (cons (specifier-type 'cons))
451 (character
452 (cond ((typep x 'standard-char)
453 ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
454 ;; CHARACTER.)
455 (specifier-type 'base-char))
456 ((not (characterp x))
457 nil)
459 ;; Beyond this, there seems to be no portable correspondence.
460 (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
461 (structure!object
462 (find-classoid (uncross (class-name (class-of x)))))
464 ;; There might be more cases which we could handle with
465 ;; sufficient effort; since all we *need* to handle are enough
466 ;; cases for bootstrapping, we don't try to be complete here,. If
467 ;; future maintainers make the bootstrap code more complicated,
468 ;; they can also add new cases here to handle it. -- WHN 2000-11-11
469 (error "can't handle ~S in cross CTYPE-OF" x))))