a43b3c4582ba51522876d71e448c27c367e03eb6
[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!IMPL")
13
14 ;;; Is X a fixnum in the target Lisp?
15 (defun fixnump (x)
16   (and (integerp x)
17        (<= sb!vm:*target-most-negative-fixnum*
18            x
19            sb!vm:*target-most-positive-fixnum*)))
20
21 ;;; (This was a useful warning when trying to get bootstrapping
22 ;;; to work, but it's mostly irrelevant noise now that the system
23 ;;; works.)
24 (define-condition cross-type-style-warning (style-warning)
25   ((call :initarg :call
26          :reader cross-type-style-warning-call)
27    (message :reader cross-type-style-warning-message
28             #+cmu :initarg #+cmu :message ; to stop bogus non-STYLE WARNING
29             ))
30   (:report (lambda (c s)
31              (format
32               s
33               "cross-compilation-time type ambiguity (should be OK) in ~S:~%~A"
34               (cross-type-style-warning-call c)
35               (cross-type-style-warning-message c)))))
36
37 ;;; This warning is issued when giving up on a type calculation where a
38 ;;; conservative answer is acceptable. Since a conservative answer is
39 ;;; acceptable, the only downside is lost optimization opportunities.
40 (define-condition cross-type-giving-up-conservatively
41     (cross-type-style-warning)
42   ((message :initform "giving up conservatively"
43             #+cmu :reader #+cmu #.(gensym) ; (to stop bogus non-STYLE WARNING)
44             )))
45
46 ;;; This warning refers to the flexibility in the ANSI spec with
47 ;;; regard to run-time distinctions between floating point types.
48 ;;; (E.g. the cross-compilation host might not even distinguish
49 ;;; between SINGLE-FLOAT and DOUBLE-FLOAT, so a DOUBLE-FLOAT number
50 ;;; would test positive as SINGLE-FLOAT.) If the target SBCL does make
51 ;;; this distinction, then information is lost. It's not too hard to
52 ;;; contrive situations where this would be a problem. In practice we
53 ;;; don't tend to run into them because all widely used Common Lisp
54 ;;; environments do recognize the distinction between SINGLE-FLOAT and
55 ;;; DOUBLE-FLOAT, and we don't really need the other distinctions
56 ;;; (e.g. between SHORT-FLOAT and SINGLE-FLOAT), so we call
57 ;;; WARN-POSSIBLE-CROSS-TYPE-FLOAT-INFO-LOSS to test at runtime
58 ;;; whether we need to worry about this at all, and not warn unless we
59 ;;; do. If we *do* have to worry about this at runtime, my (WHN
60 ;;; 19990808) guess is that the system will break in multiple places,
61 ;;; so this is a real WARNING, not just a STYLE-WARNING.
62 ;;;
63 ;;; KLUDGE: If we ever try to support LONG-FLOAT or SHORT-FLOAT, this
64 ;;; situation will get a lot more complicated.
65 (defun warn-possible-cross-type-float-info-loss (call)
66   (when (or (subtypep 'single-float 'double-float)
67             (subtypep 'double-float 'single-float))
68     (warn "possible floating point information loss in ~S" call)))
69
70 (defun sb!xc:type-of (object)
71   (labels (;; FIXME: This function is a no-op now that we no longer
72            ;; have a distinct package T%CL to translate
73            ;; for-the-target-Lisp CL symbols to, and should go away
74            ;; completely.
75            (translate (expr) expr))
76     (let ((raw-result (type-of object)))
77       (cond ((or (subtypep raw-result 'float)
78                  (subtypep raw-result 'complex))
79              (warn-possible-cross-type-float-info-loss
80               `(sb!xc:type-of ,object))
81              (translate raw-result))
82             ((subtypep raw-result 'integer)
83              (cond ((<= 0 object 1)
84                     'bit)
85                    ((fixnump object)
86                     'fixnum)
87                    (t
88                     'integer)))
89             ((some (lambda (type) (subtypep raw-result type))
90                    '(array character list symbol))
91              (translate raw-result))
92             (t
93              (error "can't handle TYPE-OF ~S in cross-compilation"))))))
94
95 ;;; Like TYPEP, but asks whether HOST-OBJECT would be of TARGET-TYPE
96 ;;; when instantiated on the target SBCL. Since this is hard to decide
97 ;;; in some cases, and since in other cases we just haven't bothered
98 ;;; to try, it needs to return two values, just like SUBTYPEP: the
99 ;;; first value for its conservative opinion (never T unless it's
100 ;;; certain) and the second value to tell whether it's certain.
101 (defun cross-typep (host-object target-type)
102   (flet ((warn-and-give-up ()
103            ;; We don't have to keep track of this as long as system performance
104            ;; is acceptable, since giving up conservatively is a safe way out.
105            #+nil
106            (warn 'cross-type-giving-up-conservatively
107                  :call `(cross-typep ,host-object ,target-type))
108            (values nil nil))
109          (warn-about-possible-float-info-loss ()
110            (warn-possible-cross-type-float-info-loss
111             `(cross-typep ,host-object ,target-type))))
112     (cond (;; Handle various SBCL-specific types which can't exist on the
113            ;; ANSI cross-compilation host. KLUDGE: This code will need to be
114            ;; tweaked by hand if the names of these types ever change, ugh!
115            (if (consp target-type)
116                (member (car target-type)
117                        '(sb!alien:alien))
118                (member target-type
119                        '(system-area-pointer
120                          funcallable-instance
121                          sb!alien-internals:alien-value)))
122            (values nil t))
123           (;; special case when TARGET-TYPE isn't a type spec, but instead
124            ;; a CLASS object
125            (typep target-type 'sb!xc::structure-class)
126            ;; SBCL-specific types which have an analogue specially created
127            ;; on the host system
128            (if (sb!xc:subtypep (sb!xc:class-name target-type)
129                                'sb!kernel::structure!object)
130                (values (typep host-object (sb!xc:class-name target-type)) t)
131                (values nil t)))
132           ((and (symbolp target-type)
133                 (find-class target-type nil)
134                 (subtypep target-type 'sb!kernel::structure!object))
135            (values (typep host-object target-type) t))
136           ((and (symbolp target-type)
137                 (sb!xc:find-class target-type nil)
138                 (sb!xc:subtypep target-type 'cl:structure-object)
139                 (typep host-object '(or symbol number list character)))
140            (values nil t))
141           (;; easy cases of arrays and vectors
142            (member target-type
143                    '(array simple-string simple-vector string vector))
144            (values (typep host-object target-type) t))
145           (;; general cases of vectors
146            (and (not (unknown-type-p (values-specifier-type target-type)))
147                 (sb!xc:subtypep target-type 'cl:vector))
148            (if (vectorp host-object)
149                (warn-and-give-up) ; general case of vectors being way too hard
150                (values nil t))) ; but "obviously not a vector" being easy
151           (;; general cases of arrays
152            (and (not (unknown-type-p (values-specifier-type target-type)))
153                 (sb!xc:subtypep target-type 'cl:array))
154            (if (arrayp host-object)
155                (warn-and-give-up) ; general case of arrays being way too hard
156                (values nil t))) ; but "obviously not an array" being easy
157           ((consp target-type)
158            (let ((first (first target-type))
159                  (rest (rest target-type)))
160              (case first
161                ;; Many complex types are guaranteed to correspond exactly
162                ;; between any host ANSI Common Lisp and the target SBCL.
163                ((integer member mod rational real signed-byte unsigned-byte)
164                 (values (typep host-object target-type) t))
165                ;; Floating point types are guaranteed to correspond, too, but
166                ;; less exactly.
167                ((single-float double-float)
168                 (cond ((floatp host-object)
169                        (warn-about-possible-float-info-loss)
170                        (values (typep host-object target-type) t))
171                       (t
172                        (values nil t))))
173                ;; Some complex types have translations that are less trivial.
174                (and
175                 ;; Note: This could be implemented as a real test, just the way
176                 ;; that OR is; I just haven't bothered. -- WHN 19990706
177                 (warn-and-give-up))
178                (or (let ((opinion nil)
179                          (certain-p t))
180                      (dolist (i rest)
181                        (multiple-value-bind (sub-opinion sub-certain-p)
182                            (cross-typep host-object i)
183                          (cond (sub-opinion (setf opinion t
184                                                   certain-p t)
185                                             (return))
186                                ((not sub-certain-p) (setf certain-p nil))))
187                        (if certain-p
188                            (values opinion t)
189                            (warn-and-give-up)))))
190                ;; Some complex types are too hard to handle in the positive
191                ;; case, but at least we can be confident in a large fraction of
192                ;; the negative cases..
193                ((base-string simple-base-string simple-string)
194                 (if (stringp host-object)
195                     (warn-and-give-up)
196                     (values nil t)))
197                ((vector simple-vector)
198                 (if (vectorp host-object)
199                     (warn-and-give-up)
200                     (values nil t)))
201                ((array simple-array)
202                 (if (arrayp host-object)
203                     (warn-and-give-up)
204                     (values nil t)))
205                (function
206                 (if (functionp host-object)
207                     (warn-and-give-up)
208                     (values nil t)))
209                ;; And the Common Lisp type system is complicated, and we don't
210                ;; try to implement everything.
211                (otherwise (warn-and-give-up)))))
212           (t
213            (case target-type
214              ((*)
215               ;; KLUDGE: SBCL has * as an explicit wild type. While this is
216               ;; sort of logical (because (e.g. (ARRAY * 1)) is a valid type)
217               ;; it's not ANSI: looking at the ANSI definitions of complex
218               ;; types like like ARRAY shows that they consider * different
219               ;; from other type names. Someday we should probably get rid of
220               ;; this non-ANSIism in base SBCL, but until we do, we might as
221               ;; well here in the cross compiler. And in order to make sure
222               ;; that we don't continue doing it after we someday patch SBCL's
223               ;; type system so that * is no longer a type, we make this
224               ;; assertion:
225               (assert (typep (specifier-type '*) 'named-type))
226               (values t t))
227              ;; Many simple types are guaranteed to correspond exactly
228              ;; between any host ANSI Common Lisp and the target
229              ;; Common Lisp. (Some array types are too, but they
230              ;; were picked off earlier.)
231              ((bit character complex cons float function integer list nil
232                null number rational real signed-byte symbol t unsigned-byte)
233               (values (typep host-object target-type) t))
234              ;; Floating point types are guaranteed to correspond, too, but
235              ;; less exactly.
236              ((single-float double-float)
237               (cond ((floatp host-object)
238                      (warn-about-possible-float-info-loss)
239                      (values (typep host-object target-type) t))
240                     (t
241                      (values nil t))))
242              ;; Some types require translation between the cross-compilation
243              ;; host Common Lisp and the target SBCL.
244              (sb!xc:class (values (typep host-object 'sb!xc:class) t))
245              (fixnum (values (fixnump host-object) t))
246              ;; Some types are too hard to handle in the positive case, but at
247              ;; least we can be confident in a large fraction of the negative
248              ;; cases..
249              ((base-string simple-base-string simple-string)
250               (if (stringp host-object)
251                   (warn-and-give-up)
252                   (values nil t)))
253              ((character base-char)
254               (cond ((typep host-object 'standard-char)
255                      (values t t))
256                     ((not (characterp host-object))
257                      (values nil t))
258                     (t
259                      (warn-and-give-up))))
260              ((stream instance)
261               ;; Neither target CL:STREAM nor target SB!KERNEL:INSTANCE is
262               ;; implemented as a STRUCTURE-OBJECT, so they'll fall through the
263               ;; tests above. We don't want to assume too much about them here,
264               ;; but at least we know enough about them to say that neither T
265               ;; nor NIL nor indeed any other symbol in the cross-compilation
266               ;; host is one. That knowledge suffices to answer so many of the
267               ;; questions that the cross-compiler asks that it's well worth
268               ;; special-casing it here.
269               (if (symbolp host-object)
270                   (values nil t)
271                   (warn-and-give-up)))
272              ;; And the Common Lisp type system is complicated, and we don't
273              ;; try to implement everything.
274              (otherwise (warn-and-give-up)))))))
275
276 ;;; An incomplete TYPEP which runs at cross-compile time to tell whether OBJECT
277 ;;; is the host Lisp representation of a target SBCL type specified by
278 ;;; TARGET-TYPE-SPEC. It need make no pretense to completeness, since it
279 ;;; need only handle the cases which arise when building SBCL itself, e.g.
280 ;;; testing that range limits FOO and BAR in (INTEGER FOO BAR) are INTEGERs.
281 (defun sb!xc:typep (host-object target-type-spec &optional (env nil env-p))
282   (declare (ignore env))
283   (assert (null env-p)) ; 'cause we're too lazy to think about it
284   (multiple-value-bind (opinion certain-p)
285       (cross-typep host-object target-type-spec)
286     ;; A program that calls TYPEP doesn't want uncertainty and probably
287     ;; can't handle it.
288     (if certain-p
289         opinion
290         (error "uncertain in SB!XC:TYPEP ~S ~S"
291                host-object
292                target-type-spec))))
293
294 ;;; This implementation is an incomplete, portable version for use at
295 ;;; cross-compile time only.
296 (defun ctypep (obj ctype)
297   (check-type ctype ctype)
298   (let (;; the Common Lisp type specifier corresponding to CTYPE
299         (type (type-specifier ctype)))
300     (check-type type (or symbol cons))
301     (cross-typep obj type)))
302
303 (defparameter *universal-function-type*
304   (make-function-type :wild-args t
305                       :returns *wild-type*))
306
307 (defun ctype-of (x)
308   (typecase x
309     (function
310      (if (typep x 'generic-function)
311          ;; Since at cross-compile time we build a CLOS-free bootstrap
312          ;; version of SBCL, it's unclear how to explain to it what a
313          ;; generic function is.
314          (error "not implemented: cross CTYPE-OF generic function")
315          ;; There's no ANSI way to find out what the function is
316          ;; declared to be, so we just return the CTYPE for the
317          ;; most-general function.
318          *universal-function-type*))
319     (symbol
320      (make-member-type :members (list x)))
321     (number
322      (let* ((num (if (complexp x) (realpart x) x))
323             (res (make-numeric-type
324                   :class (etypecase num
325                            (integer 'integer)
326                            (rational 'rational)
327                            (float 'float))
328                   :format (if (floatp num)
329                               (float-format-name num)
330                               nil))))
331        (cond ((complexp x)
332               (setf (numeric-type-complexp res) :complex)
333               (let ((imag (imagpart x)))
334                 (setf (numeric-type-low res) (min num imag))
335                 (setf (numeric-type-high res) (max num imag))))
336              (t
337               (setf (numeric-type-low res) num)
338               (setf (numeric-type-high res) num)))
339        res))
340     (array
341      (let ((etype (specifier-type (array-element-type x))))
342        (make-array-type :dimensions (array-dimensions x)
343                         :complexp (not (typep x 'simple-array))
344                         :element-type etype
345                         :specialized-element-type etype)))
346     (cons (specifier-type 'cons))
347     (character
348      (cond ((typep x 'standard-char)
349             ;; (Note that SBCL doesn't distinguish between BASE-CHAR and
350             ;; CHARACTER.)
351             (sb!xc:find-class 'base-char))
352            ((not (characterp x))
353             nil)
354            (t
355             ;; Beyond this, there seems to be no portable correspondence.
356             (error "can't map host Lisp CHARACTER ~S to target Lisp" x))))
357     (structure!object
358      (sb!xc:find-class (uncross (class-name (class-of x)))))
359     (t
360      ;; There might be more cases which we could handle with
361      ;; sufficient effort; since all we *need* to handle are enough
362      ;; cases for bootstrapping, we don't try to be complete here,. If
363      ;; future maintainers make the bootstrap code more complicated,
364      ;; they can also add new cases here to handle it. -- WHN 2000-11-11
365      (error "can't handle ~S in cross CTYPE-OF" x))))