e44501f2806baedcb6c10e1b84a4f56400f8edba
[sbcl.git] / src / code / cross-type.lisp
1 ;;;; cross-compiler-only versions of TYPEP, TYPE-OF, and related functions
2
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.
11
12 (in-package "SB!KERNEL")
13
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)))
18
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)
27             ))
28   (:report (lambda (c s)
29              (format
30               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)))))
34
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             )))
43
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)))
67
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)
83                  (t
84                   'integer)))
85           ((some (lambda (type) (subtypep raw-result type))
86                  '(array character list symbol))
87            raw-result)
88           (t
89            (error "can't handle TYPE-OF ~S in cross-compilation" object)))))
90
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))
99
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.
122            ;;
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 'class)
149              (bug "We don't support CROSS-TYPEP of CLASS type specifiers"))
150             ((and (symbolp target-type)
151                   (find-classoid target-type nil)
152                   (sb!xc:subtypep target-type 'cl:structure-object)
153                   (typep host-object '(or symbol number list character)))
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             (;; easy cases of arrays and vectors
160              (target-type-is-in
161               '(array simple-string simple-vector string vector))
162              (values (typep host-object target-type) t))
163             (;; general cases of vectors
164              (and (not (unknown-type-p (values-specifier-type target-type)))
165                   (sb!xc:subtypep target-type 'cl:vector))
166              (if (vectorp host-object)
167                  (warn-and-give-up) ; general-case vectors being way too hard
168                  (values nil t))) ; but "obviously not a vector" being easy
169             (;; general cases of arrays
170              (and (not (unknown-type-p (values-specifier-type target-type)))
171                   (sb!xc:subtypep target-type 'cl:array))
172              (if (arrayp host-object)
173                  (warn-and-give-up) ; general-case arrays being way too hard
174                  (values nil t))) ; but "obviously not an array" being easy
175             ((target-type-is-in '(*))
176              ;; KLUDGE: SBCL has * as an explicit wild type. While
177              ;; this is sort of logical (because (e.g. (ARRAY * 1)) is
178              ;; a valid type) it's not ANSI: looking at the ANSI
179              ;; definitions of complex types like like ARRAY shows
180              ;; that they consider * different from other type names.
181              ;; Someday we should probably get rid of this non-ANSIism
182              ;; in base SBCL, but until we do, we might as well here
183              ;; in the cross compiler. And in order to make sure that
184              ;; we don't continue doing it after we someday patch
185              ;; SBCL's type system so that * is no longer a type, we
186              ;; make this assertion. -- WHN 2001-08-08
187              (aver (typep (values-specifier-type '*) 'named-type))
188              (values t t))
189             (;; Many simple types are guaranteed to correspond exactly
190              ;; between any host ANSI Common Lisp and the target
191              ;; Common Lisp. (Some array types are too, but they
192              ;; were picked off earlier.)
193              (target-type-is-in
194               '(atom bit character complex cons float function integer keyword
195                 list nil null number rational real signed-byte symbol t
196                 unsigned-byte))
197              (values (typep host-object target-type) t))
198             (;; Floating point types are guaranteed to correspond,
199              ;; too, but less exactly.
200              (target-type-is-in
201               '(single-float double-float))
202              (cond ((floatp host-object)
203                     (warn-about-possible-float-info-loss)
204                     (values (typep host-object target-type) t))
205                    (t
206                     (values nil t))))
207             (;; Complexes suffer the same kind of problems as arrays
208              (and (not (unknown-type-p (values-specifier-type target-type)))
209                   (sb!xc:subtypep target-type 'cl:complex))
210              (if (complexp host-object)
211                  (warn-and-give-up) ; general-case complexes being way too hard
212                  (values nil t))) ; but "obviously not a complex" being easy
213             ;; Some types require translation between the cross-compilation
214             ;; host Common Lisp and the target SBCL.
215             ((target-type-is-in '(classoid))
216              (values (typep host-object 'classoid) t))
217             ((target-type-is-in '(fixnum))
218              (values (fixnump host-object) t))
219             ;; Some types are too hard to handle in the positive
220             ;; case, but at least we can be confident in a large
221             ;; fraction of the negative cases..
222             ((target-type-is-in
223               '(base-string simple-base-string simple-string))
224              (if (stringp host-object)
225                  (warn-and-give-up)
226                  (values nil t)))
227             ((target-type-is-in '(character base-char))
228              (cond ((typep host-object 'standard-char)
229                     (values t t))
230                    ((not (characterp host-object))
231                     (values nil t))
232                    (t
233                     (warn-and-give-up))))
234             ((target-type-is-in '(stream instance))
235              ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE
236              ;; is implemented as a STRUCTURE-OBJECT, so they'll fall
237              ;; through the tests above. We don't want to assume too
238              ;; much about them here, but at least we know enough
239              ;; about them to say that neither T nor NIL nor indeed
240              ;; any other symbol in the cross-compilation host is one.
241              ;; That knowledge suffices to answer so many of the
242              ;; questions that the cross-compiler asks that it's well
243              ;; worth special-casing it here.
244              (if (symbolp host-object)
245                  (values nil t)
246                  (warn-and-give-up)))
247             ;; various hacks for composite types..
248             ((consp target-type)
249              (let ((first (first target-type))
250                    (rest (rest target-type)))
251                (case first
252                  ;; Many complex types are guaranteed to correspond exactly
253                  ;; between any host ANSI Common Lisp and the target SBCL.
254                  ((integer member mod rational real signed-byte unsigned-byte)
255                   (values (typep host-object target-type) t))
256                  ;; Floating point types are guaranteed to correspond,
257                  ;; too, but less exactly.
258                  ((single-float double-float)
259                   (cond ((floatp host-object)
260                          (warn-about-possible-float-info-loss)
261                          (values (typep host-object target-type) t))
262                         (t
263                          (values nil t))))
264                  ;; Some complex types have translations that are less
265                  ;; trivial.
266                  (and (every/type #'cross-typep host-object rest))
267                  (or  (any/type   #'cross-typep host-object rest))
268                  ;; If we want to work with the KEYWORD type, we need
269                  ;; to grok (SATISFIES KEYWORDP).
270                  (satisfies
271                   (destructuring-bind (predicate-name) rest
272                     (if (and (in-cl-package-p predicate-name)
273                              (fboundp predicate-name))
274                         ;; Many predicates like KEYWORDP, ODDP, PACKAGEP,
275                         ;; and NULL correspond between host and target.
276                         ;; But we still need to handle errors, because
277                         ;; the code which calls us may not understand
278                         ;; that a type is unreachable. (E.g. when compiling
279                         ;; (AND STRING (SATISFIES ARRAY-HAS-FILL-POINTER-P))
280                         ;; CTYPEP may be called on the SATISFIES expression
281                         ;; even for non-STRINGs.)
282                         (multiple-value-bind (result error?)
283                             (ignore-errors (funcall predicate-name
284                                                     host-object))
285                           (if error?
286                               (values nil nil)
287                               (values result t)))
288                         ;; For symbols not in the CL package, it's not
289                         ;; in general clear how things correspond
290                         ;; between host and target, so we punt.
291                         (warn-and-give-up))))
292                  ;; Some complex types are too hard to handle in the
293                  ;; positive case, but at least we can be confident in
294                  ;; a large fraction of the negative cases..
295                  ((base-string simple-base-string simple-string)
296                   (if (stringp host-object)
297                       (warn-and-give-up)
298                       (values nil t)))
299                  ((vector simple-vector)
300                   (if (vectorp host-object)
301                       (warn-and-give-up)
302                       (values nil t)))
303                  ((array simple-array)
304                   (if (arrayp host-object)
305                       (warn-and-give-up)
306                       (values nil t)))
307                  (function
308                   (if (functionp host-object)
309                       (warn-and-give-up)
310                       (values nil t)))
311                  ;; And the Common Lisp type system is complicated,
312                  ;; and we don't try to implement everything.
313                  (otherwise (warn-and-give-up)))))
314             ;; And the Common Lisp type system is complicated, and
315             ;; we don't try to implement everything.
316             (t
317              (warn-and-give-up))))))
318
319 ;;; This is an incomplete TYPEP which runs at cross-compile time to
320 ;;; tell whether OBJECT is the host Lisp representation of a target
321 ;;; SBCL type specified by TARGET-TYPE-SPEC. It need make no pretense
322 ;;; to completeness, since it need only handle the cases which arise
323 ;;; when building SBCL itself, e.g. testing that range limits FOO and
324 ;;; BAR in (INTEGER FOO BAR) are INTEGERs.
325 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
326   (declare (ignore env))
327   (aver (null env-p)) ; 'cause we're too lazy to think about it
328   (multiple-value-bind (opinion certain-p)
329       (cross-typep host-object target-type-spec)
330     ;; A program that calls TYPEP doesn't want uncertainty and
331     ;; probably can't handle it.
332     (if certain-p
333         opinion
334         (error "uncertain in SB!XC:TYPEP ~S ~S"
335                host-object
336                target-type-spec))))
337
338 ;;; This is an incomplete, portable implementation for use at
339 ;;; cross-compile time only.
340 (defun ctypep (obj ctype)
341   (check-type ctype ctype)
342   (let (;; the Common Lisp type specifier corresponding to CTYPE
343         (type (type-specifier ctype)))
344     (check-type type (or symbol cons))
345     (cross-typep obj type)))
346
347 (defun ctype-of (x)
348   (typecase x
349     (function
350      (if (typep x 'generic-function)
351          ;; Since at cross-compile time we build a CLOS-free bootstrap
352          ;; version of SBCL, it's unclear how to explain to it what a
353          ;; generic function is.
354          (error "not implemented: cross CTYPE-OF generic function")
355          ;; There's no ANSI way to find out what the function is
356          ;; declared to be, so we just return the CTYPE for the
357          ;; most-general function.
358          *universal-fun-type*))
359     (symbol
360      (make-member-type :members (list x)))
361     (number
362      (ctype-of-number x))
363     (array
364      (let ((etype (specifier-type (array-element-type x))))
365        (make-array-type :dimensions (array-dimensions x)
366                         :complexp (not (typep x 'simple-array))
367                         :element-type etype
368                         :specialized-element-type etype)))
369     (cons (specifier-type 'cons))
370     (character
371      (cond ((typep x 'standard-char)
372             ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
373             ;; CHARACTER.)
374             (find-classoid 'base-char))
375            ((not (characterp x))
376             nil)
377            (t
378             ;; Beyond this, there seems to be no portable correspondence.
379             (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
380     (structure!object
381      (find-classoid (uncross (class-name (class-of x)))))
382     (t
383      ;; There might be more cases which we could handle with
384      ;; sufficient effort; since all we *need* to handle are enough
385      ;; cases for bootstrapping, we don't try to be complete here,. If
386      ;; future maintainers make the bootstrap code more complicated,
387      ;; they can also add new cases here to handle it. -- WHN 2000-11-11
388      (error "can't handle ~S in cross CTYPE-OF" x))))