0.7.8.56:
[sbcl/lichteblau.git] / src / code / cross-type.lisp
blobe4031af9dbc84f53f2f96bd02d80328c5d5fa8ba
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 ((some (lambda (type) (subtypep raw-result type))
86 '(array character list symbol))
87 raw-result)
89 (error "can't handle TYPE-OF ~S in cross-compilation")))))
91 ;;; Is SYMBOL in the CL package? Note that we're testing this on the
92 ;;; cross-compilation host, which could do things any old way. In
93 ;;; particular, it might be in the CL package even though
94 ;;; SYMBOL-PACKAGE is not (FIND-PACKAGE :CL). So we test things
95 ;;; another way.
96 (defun in-cl-package-p (symbol)
97 (eql (find-symbol (symbol-name symbol) :cl)
98 symbol))
100 ;;; This is like TYPEP, except that it asks whether HOST-OBJECT would
101 ;;; be of TARGET-TYPE when instantiated on the target SBCL. Since this
102 ;;; is hard to determine in some cases, and since in other cases we
103 ;;; just haven't bothered to try, it needs to return two values, just
104 ;;; like SUBTYPEP: the first value for its conservative opinion (never
105 ;;; T unless it's certain) and the second value to tell whether it's
106 ;;; certain.
107 (defun cross-typep (host-object raw-target-type)
108 (let ((target-type (type-expand raw-target-type)))
109 (flet ((warn-and-give-up ()
110 ;; We don't have to keep track of this as long as system
111 ;; performance is acceptable, since giving up
112 ;; conservatively is a safe way out.
113 #+nil
114 (warn 'cross-type-giving-up-conservatively
115 :call `(cross-typep ,host-object ,raw-target-type))
116 (values nil nil))
117 (warn-about-possible-float-info-loss ()
118 (warn-possible-cross-type-float-info-loss
119 `(cross-typep ,host-object ,raw-target-type)))
120 ;; a convenient idiom for making more matches to special cases:
121 ;; Test both forms of target type for membership in LIST.
123 ;; (In order to avoid having to use too much deep knowledge
124 ;; of types, it's sometimes convenient to test RAW-TARGET-TYPE
125 ;; as well as the expanded type, since we can get matches with
126 ;; just EQL. E.g. SIMPLE-STRING can be matched with EQL, while
127 ;; safely matching its expansion,
128 ;; (OR (SIMPLE-ARRAY CHARACTER (*)) (SIMPLE-BASE-STRING *))
129 ;; would require logic clever enough to know that, e.g., OR is
130 ;; commutative.)
131 (target-type-is-in (list)
132 (or (member raw-target-type list)
133 (member target-type list))))
134 (cond (;; Handle various SBCL-specific types which can't exist on
135 ;; the ANSI cross-compilation host. KLUDGE: This code will
136 ;; need to be tweaked by hand if the names of these types
137 ;; ever change, ugh!
138 (if (consp target-type)
139 (member (car target-type)
140 '(sb!alien:alien))
141 (member target-type
142 '(system-area-pointer
143 funcallable-instance
144 sb!alien-internals:alien-value)))
145 (values nil t))
146 (;; special case when TARGET-TYPE isn't a type spec, but
147 ;; instead a CLASS object
148 (typep target-type 'sb!xc::structure-class)
149 ;; SBCL-specific types which have an analogue specially
150 ;; created on the host system
151 (if (sb!xc:subtypep (sb!xc:class-name target-type)
152 'sb!kernel::structure!object)
153 (values (typep host-object (sb!xc:class-name target-type)) t)
154 (values nil t)))
155 ((and (symbolp target-type)
156 (find-class target-type nil)
157 (subtypep target-type 'sb!kernel::structure!object))
158 (values (typep host-object target-type) t))
159 ((and (symbolp target-type)
160 (sb!xc:find-class target-type nil)
161 (sb!xc:subtypep target-type 'cl:structure-object)
162 (typep host-object '(or symbol number list character)))
163 (values nil t))
164 (;; easy cases of arrays and vectors
165 (target-type-is-in
166 '(array simple-string simple-vector string vector))
167 (values (typep host-object target-type) t))
168 (;; general cases of vectors
169 (and (not (unknown-type-p (values-specifier-type target-type)))
170 (sb!xc:subtypep target-type 'cl:vector))
171 (if (vectorp host-object)
172 (warn-and-give-up) ; general-case vectors being way too hard
173 (values nil t))) ; but "obviously not a vector" being easy
174 (;; general cases of arrays
175 (and (not (unknown-type-p (values-specifier-type target-type)))
176 (sb!xc:subtypep target-type 'cl:array))
177 (if (arrayp host-object)
178 (warn-and-give-up) ; general-case arrays being way too hard
179 (values nil t))) ; but "obviously not an array" being easy
180 ((target-type-is-in '(*))
181 ;; KLUDGE: SBCL has * as an explicit wild type. While
182 ;; this is sort of logical (because (e.g. (ARRAY * 1)) is
183 ;; a valid type) it's not ANSI: looking at the ANSI
184 ;; definitions of complex types like like ARRAY shows
185 ;; that they consider * different from other type names.
186 ;; Someday we should probably get rid of this non-ANSIism
187 ;; in base SBCL, but until we do, we might as well here
188 ;; in the cross compiler. And in order to make sure that
189 ;; we don't continue doing it after we someday patch
190 ;; SBCL's type system so that * is no longer a type, we
191 ;; make this assertion. -- WHN 2001-08-08
192 (aver (typep (specifier-type '*) 'named-type))
193 (values t t))
194 (;; Many simple types are guaranteed to correspond exactly
195 ;; between any host ANSI Common Lisp and the target
196 ;; Common Lisp. (Some array types are too, but they
197 ;; were picked off earlier.)
198 (target-type-is-in
199 '(atom bit character complex cons float function integer keyword
200 list nil null number rational real signed-byte symbol t
201 unsigned-byte))
202 (values (typep host-object target-type) t))
203 (;; Floating point types are guaranteed to correspond,
204 ;; too, but less exactly.
205 (target-type-is-in
206 '(single-float double-float))
207 (cond ((floatp host-object)
208 (warn-about-possible-float-info-loss)
209 (values (typep host-object target-type) t))
211 (values nil t))))
212 (;; Complexes suffer the same kind of problems as arrays
213 (and (not (unknown-type-p (values-specifier-type target-type)))
214 (sb!xc:subtypep target-type 'cl:complex))
215 (if (complexp host-object)
216 (warn-and-give-up) ; general-case complexes being way too hard
217 (values nil t))) ; but "obviously not a complex" being easy
218 ;; Some types require translation between the cross-compilation
219 ;; host Common Lisp and the target SBCL.
220 ((target-type-is-in '(sb!xc:class))
221 (values (typep host-object 'sb!xc:class) t))
222 ((target-type-is-in '(fixnum))
223 (values (fixnump host-object) t))
224 ;; Some types are too hard to handle in the positive
225 ;; case, but at least we can be confident in a large
226 ;; fraction of the negative cases..
227 ((target-type-is-in
228 '(base-string simple-base-string simple-string))
229 (if (stringp host-object)
230 (warn-and-give-up)
231 (values nil t)))
232 ((target-type-is-in '(character base-char))
233 (cond ((typep host-object 'standard-char)
234 (values t t))
235 ((not (characterp host-object))
236 (values nil t))
238 (warn-and-give-up))))
239 ((target-type-is-in '(stream instance))
240 ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE
241 ;; is implemented as a STRUCTURE-OBJECT, so they'll fall
242 ;; through the tests above. We don't want to assume too
243 ;; much about them here, but at least we know enough
244 ;; about them to say that neither T nor NIL nor indeed
245 ;; any other symbol in the cross-compilation host is one.
246 ;; That knowledge suffices to answer so many of the
247 ;; questions that the cross-compiler asks that it's well
248 ;; worth special-casing it here.
249 (if (symbolp host-object)
250 (values nil t)
251 (warn-and-give-up)))
252 ;; various hacks for composite types..
253 ((consp target-type)
254 (let ((first (first target-type))
255 (rest (rest target-type)))
256 (case first
257 ;; Many complex types are guaranteed to correspond exactly
258 ;; between any host ANSI Common Lisp and the target SBCL.
259 ((integer member mod rational real signed-byte unsigned-byte)
260 (values (typep host-object target-type) t))
261 ;; Floating point types are guaranteed to correspond,
262 ;; too, but less exactly.
263 ((single-float double-float)
264 (cond ((floatp host-object)
265 (warn-about-possible-float-info-loss)
266 (values (typep host-object target-type) t))
268 (values nil t))))
269 ;; Some complex types have translations that are less
270 ;; trivial.
271 (and (every/type #'cross-typep host-object rest))
272 (or (any/type #'cross-typep host-object rest))
273 ;; If we want to work with the KEYWORD type, we need
274 ;; to grok (SATISFIES KEYWORDP).
275 (satisfies
276 (destructuring-bind (predicate-name) rest
277 (if (and (in-cl-package-p predicate-name)
278 (fboundp predicate-name))
279 ;; Many predicates like KEYWORDP, ODDP, PACKAGEP,
280 ;; and NULL correspond between host and target.
281 ;; But we still need to handle errors, because
282 ;; the code which calls us may not understand
283 ;; that a type is unreachable. (E.g. when compiling
284 ;; (AND STRING (SATISFIES ARRAY-HAS-FILL-POINTER-P))
285 ;; CTYPEP may be called on the SATISFIES expression
286 ;; even for non-STRINGs.)
287 (multiple-value-bind (result error?)
288 (ignore-errors (funcall predicate-name
289 host-object))
290 (if error?
291 (values nil nil)
292 (values result t)))
293 ;; For symbols not in the CL package, it's not
294 ;; in general clear how things correspond
295 ;; between host and target, so we punt.
296 (warn-and-give-up))))
297 ;; Some complex types are too hard to handle in the
298 ;; positive case, but at least we can be confident in
299 ;; a large fraction of the negative cases..
300 ((base-string simple-base-string simple-string)
301 (if (stringp host-object)
302 (warn-and-give-up)
303 (values nil t)))
304 ((vector simple-vector)
305 (if (vectorp host-object)
306 (warn-and-give-up)
307 (values nil t)))
308 ((array simple-array)
309 (if (arrayp host-object)
310 (warn-and-give-up)
311 (values nil t)))
312 (function
313 (if (functionp host-object)
314 (warn-and-give-up)
315 (values nil t)))
316 ;; And the Common Lisp type system is complicated,
317 ;; and we don't try to implement everything.
318 (otherwise (warn-and-give-up)))))
319 ;; And the Common Lisp type system is complicated, and
320 ;; we don't try to implement everything.
322 (warn-and-give-up))))))
324 ;;; This is an incomplete TYPEP which runs at cross-compile time to
325 ;;; tell whether OBJECT is the host Lisp representation of a target
326 ;;; SBCL type specified by TARGET-TYPE-SPEC. It need make no pretense
327 ;;; to completeness, since it need only handle the cases which arise
328 ;;; when building SBCL itself, e.g. testing that range limits FOO and
329 ;;; BAR in (INTEGER FOO BAR) are INTEGERs.
330 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
331 (declare (ignore env))
332 (aver (null env-p)) ; 'cause we're too lazy to think about it
333 (multiple-value-bind (opinion certain-p)
334 (cross-typep host-object target-type-spec)
335 ;; A program that calls TYPEP doesn't want uncertainty and
336 ;; probably can't handle it.
337 (if certain-p
338 opinion
339 (error "uncertain in SB!XC:TYPEP ~S ~S"
340 host-object
341 target-type-spec))))
343 ;;; This is an incomplete, portable implementation for use at
344 ;;; cross-compile time only.
345 (defun ctypep (obj ctype)
346 (check-type ctype ctype)
347 (let (;; the Common Lisp type specifier corresponding to CTYPE
348 (type (type-specifier ctype)))
349 (check-type type (or symbol cons))
350 (cross-typep obj type)))
352 (defun ctype-of (x)
353 (typecase x
354 (function
355 (if (typep x 'generic-function)
356 ;; Since at cross-compile time we build a CLOS-free bootstrap
357 ;; version of SBCL, it's unclear how to explain to it what a
358 ;; generic function is.
359 (error "not implemented: cross CTYPE-OF generic function")
360 ;; There's no ANSI way to find out what the function is
361 ;; declared to be, so we just return the CTYPE for the
362 ;; most-general function.
363 *universal-fun-type*))
364 (symbol
365 (make-member-type :members (list x)))
366 (number
367 (ctype-of-number x))
368 (array
369 (let ((etype (specifier-type (array-element-type x))))
370 (make-array-type :dimensions (array-dimensions x)
371 :complexp (not (typep x 'simple-array))
372 :element-type etype
373 :specialized-element-type etype)))
374 (cons (specifier-type 'cons))
375 (character
376 (cond ((typep x 'standard-char)
377 ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
378 ;; CHARACTER.)
379 (sb!xc:find-class 'base-char))
380 ((not (characterp x))
381 nil)
383 ;; Beyond this, there seems to be no portable correspondence.
384 (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
385 (structure!object
386 (sb!xc:find-class (uncross (class-name (class-of x)))))
388 ;; There might be more cases which we could handle with
389 ;; sufficient effort; since all we *need* to handle are enough
390 ;; cases for bootstrapping, we don't try to be complete here,. If
391 ;; future maintainers make the bootstrap code more complicated,
392 ;; they can also add new cases here to handle it. -- WHN 2000-11-11
393 (error "can't handle ~S in cross CTYPE-OF" x))))