Fix make-array transforms.
[sbcl.git] / src / code / coerce.lisp
1 ;;;; COERCE and related code
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 (macrolet ((def (name result access src-type &optional typep)
15              `(defun ,name (object ,@(if typep '(type) ()))
16                 (declare (type ,(ecase src-type
17                                        (:list 'list)
18                                        (:vector 'vector)
19                                        (:sequence 'sequence)) object))
20                 (do* ((index 0 (1+ index))
21                       (length (length object))
22                       (result ,result)
23                       (in-object object))
24                      ((>= index length) result)
25                   (declare (fixnum length index))
26                   (declare (type vector result))
27                   (setf (,access result index)
28                         ,(ecase src-type
29                            (:list '(pop in-object))
30                            (:vector '(aref in-object index))
31                            (:sequence '(elt in-object index))))))))
32
33   (def list-to-vector* (make-sequence type length)
34     aref :list t)
35
36   (def vector-to-vector* (make-sequence type length)
37     aref :vector t)
38
39   (def sequence-to-vector* (make-sequence type length)
40     aref :sequence t))
41
42 (defun vector-to-list* (object)
43   (declare (type vector object))
44   (let ((result (list nil))
45         (length (length object)))
46     (declare (fixnum length))
47     (do ((index 0 (1+ index))
48          (splice result (cdr splice)))
49         ((>= index length) (cdr result))
50       (declare (fixnum index))
51       (rplacd splice (list (aref object index))))))
52
53 (defvar *offending-datum*); FIXME: Remove after debugging COERCE.
54
55 ;;; These are used both by the full DEFUN function and by various
56 ;;; optimization transforms in the constant-OUTPUT-TYPE-SPEC case.
57 ;;;
58 ;;; Most of them are INLINE so that they can be optimized when the
59 ;;; argument type is known. It might be better to do this with
60 ;;; DEFTRANSFORMs, though.
61 (declaim (inline coerce-to-list))
62 (declaim (inline coerce-to-vector))
63 (defun coerce-to-fun (object)
64   ;; (Unlike the other COERCE-TO-FOOs, this one isn't inline, because
65   ;; it's so big and because optimizing away the outer ETYPECASE
66   ;; doesn't seem to buy us that much anyway.)
67   (etypecase object
68     (symbol
69      ;; ANSI lets us return ordinary errors (non-TYPE-ERRORs) here.
70      (cond ((macro-function object)
71             (error "~S names a macro." object))
72            ((special-operator-p object)
73             (error "~S is a special operator." object))
74            (t (fdefinition object))))
75     (list
76      (case (first object)
77        ((setf)
78         (fdefinition object))
79        ((lambda)
80         ;; FIXME: If we go to a compiler-only implementation, this can
81         ;; become COMPILE instead of EVAL, which seems nicer to me.
82         (eval `(function ,object)))
83        (t
84         (error 'simple-type-error
85                :datum object
86                :expected-type '(or symbol
87                                    ;; KLUDGE: ANSI wants us to
88                                    ;; return a TYPE-ERROR here, and
89                                    ;; a TYPE-ERROR is supposed to
90                                    ;; describe the expected type,
91                                    ;; but it's not obvious how to
92                                    ;; describe the coerceable cons
93                                    ;; types, so we punt and just say
94                                    ;; CONS. -- WHN 20000503
95                                    cons)
96                :format-control "~S can't be coerced to a function."
97                :format-arguments (list object)))))))
98
99 (defun coerce-to-list (object)
100   (etypecase object
101     (vector (vector-to-list* object))))
102
103 (defun coerce-to-vector (object output-type-spec)
104   (etypecase object
105     (list (list-to-vector* object output-type-spec))
106     (vector (vector-to-vector* object output-type-spec))))
107
108 ;;; old working version
109 (defun coerce (object output-type-spec)
110   #!+sb-doc
111   "Coerce the Object to an object of type Output-Type-Spec."
112   (flet ((coerce-error ()
113            (/show0 "entering COERCE-ERROR")
114            (error 'simple-type-error
115                   :format-control "~S can't be converted to type ~S."
116                   :format-arguments (list object output-type-spec)
117                   :datum object
118                   :expected-type output-type-spec)))
119     (let ((type (specifier-type output-type-spec)))
120       (cond
121         ((%typep object output-type-spec)
122          object)
123         ((eq type *empty-type*)
124          (coerce-error))
125         ((type= type (specifier-type 'character))
126          (character object))
127         ((numberp object)
128          (cond
129            ((csubtypep type (specifier-type 'single-float))
130             (let ((res (%single-float object)))
131               (unless (typep res output-type-spec)
132                 (coerce-error))
133               res))
134            ((csubtypep type (specifier-type 'double-float))
135             (let ((res (%double-float object)))
136               (unless (typep res output-type-spec)
137                 (coerce-error))
138               res))
139            #!+long-float
140            ((csubtypep type (specifier-type 'long-float))
141             (let ((res (%long-float object)))
142               (unless (typep res output-type-spec)
143                 (coerce-error))
144               res))
145            ((csubtypep type (specifier-type 'float))
146             (let ((res (%single-float object)))
147               (unless (typep res output-type-spec)
148                 (coerce-error))
149               res))
150            (t
151             (let ((res
152                    (cond
153                      ((csubtypep type (specifier-type '(complex single-float)))
154                       (complex (%single-float (realpart object))
155                                (%single-float (imagpart object))))
156                      ((csubtypep type (specifier-type '(complex double-float)))
157                       (complex (%double-float (realpart object))
158                                (%double-float (imagpart object))))
159                      #!+long-float
160                      ((csubtypep type (specifier-type '(complex long-float)))
161                       (complex (%long-float (realpart object))
162                                (%long-float (imagpart object))))
163                      ((csubtypep type (specifier-type '(complex float)))
164                       (complex (%single-float (realpart object))
165                                (%single-float (imagpart object))))
166                      ((and (typep object 'rational)
167                            (csubtypep type (specifier-type '(complex float))))
168                       ;; Perhaps somewhat surprisingly, ANSI specifies
169                       ;; that (COERCE FOO 'FLOAT) is a SINGLE-FLOAT,
170                       ;; not dispatching on
171                       ;; *READ-DEFAULT-FLOAT-FORMAT*.  By analogy, we
172                       ;; do the same for complex numbers. -- CSR,
173                       ;; 2002-08-06
174                       (complex (%single-float object)))
175                      ((csubtypep type (specifier-type 'complex))
176                       (complex object))
177                      (t
178                       (coerce-error)))))
179               ;; If RES has the wrong type, that means that rule of
180               ;; canonical representation for complex rationals was
181               ;; invoked. According to the Hyperspec, (coerce 7/2
182               ;; 'complex) returns 7/2. Thus, if the object was a
183               ;; rational, there is no error here.
184               (unless (or (typep res output-type-spec)
185                           (rationalp object))
186                 (coerce-error))
187               res))))
188         ((csubtypep type (specifier-type 'list))
189          (if (vectorp object)
190              (cond
191                ((type= type (specifier-type 'list))
192                 (vector-to-list* object))
193                ((type= type (specifier-type 'null))
194                 (if (= (length object) 0)
195                     'nil
196                     (sequence-type-length-mismatch-error type
197                                                          (length object))))
198                ((cons-type-p type)
199                 (multiple-value-bind (min exactp)
200                     (sb!kernel::cons-type-length-info type)
201                   (let ((length (length object)))
202                     (if exactp
203                         (unless (= length min)
204                           (sequence-type-length-mismatch-error type length))
205                         (unless (>= length min)
206                           (sequence-type-length-mismatch-error type length)))
207                     (vector-to-list* object))))
208                (t (sequence-type-too-hairy (type-specifier type))))
209              (if (sequencep object)
210                  (cond
211                    ((type= type (specifier-type 'list))
212                     (sb!sequence:make-sequence-like
213                      nil (length object) :initial-contents object))
214                    ((type= type (specifier-type 'null))
215                     (if (= (length object) 0)
216                         'nil
217                         (sequence-type-length-mismatch-error type
218                                                              (length object))))
219                    ((cons-type-p type)
220                     (multiple-value-bind (min exactp)
221                         (sb!kernel::cons-type-length-info type)
222                       (let ((length (length object)))
223                         (if exactp
224                             (unless (= length min)
225                               (sequence-type-length-mismatch-error type length))
226                             (unless (>= length min)
227                               (sequence-type-length-mismatch-error type length)))
228                         (sb!sequence:make-sequence-like
229                          nil length :initial-contents object))))
230                    (t (sequence-type-too-hairy (type-specifier type))))
231                  (coerce-error))))
232         ((csubtypep type (specifier-type 'vector))
233          (typecase object
234            ;; FOO-TO-VECTOR* go through MAKE-SEQUENCE, so length
235            ;; errors are caught there. -- CSR, 2002-10-18
236            (list (list-to-vector* object output-type-spec))
237            (vector (vector-to-vector* object output-type-spec))
238            (sequence (sequence-to-vector* object output-type-spec))
239            (t
240             (coerce-error))))
241         ((and (csubtypep type (specifier-type 'sequence))
242               (find-class output-type-spec nil))
243          (let ((class (find-class output-type-spec)))
244            (unless (sb!mop:class-finalized-p class)
245              (sb!mop:finalize-inheritance class))
246            (sb!sequence:make-sequence-like
247             (sb!mop:class-prototype class)
248             (length object) :initial-contents object)))
249         ((csubtypep type (specifier-type 'function))
250          (when (and (legal-fun-name-p object)
251                     (not (fboundp object)))
252            (error 'simple-type-error
253                   :datum object
254                   ;; FIXME: SATISFIES FBOUNDP is a kinda bizarre broken
255                   ;; type specifier, since the set of values it describes
256                   ;; isn't in general constant in time. Maybe we could
257                   ;; find a better way of expressing this error? (Maybe
258                   ;; with the UNDEFINED-FUNCTION condition?)
259                   :expected-type '(satisfies fboundp)
260                :format-control "~S isn't fbound."
261                :format-arguments (list object)))
262          (when (and (symbolp object)
263                     (sb!xc:macro-function object))
264            (error 'simple-type-error
265                   :datum object
266                   :expected-type '(not (satisfies sb!xc:macro-function))
267                   :format-control "~S is a macro."
268                   :format-arguments (list object)))
269          (when (and (symbolp object)
270                     (special-operator-p object))
271            (error 'simple-type-error
272                   :datum object
273                   :expected-type '(not (satisfies special-operator-p))
274                   :format-control "~S is a special operator."
275                   :format-arguments (list object)))
276          (eval `#',object))
277         (t
278          (coerce-error))))))
279
280 ;;; new version, which seems as though it should be better, but which
281 ;;; does not yet work
282 #+nil
283 (defun coerce (object output-type-spec)
284   #!+sb-doc
285   "Coerces the Object to an object of type Output-Type-Spec."
286   (flet ((coerce-error ()
287            (error 'simple-type-error
288                   :format-control "~S can't be converted to type ~S."
289                   :format-arguments (list object output-type-spec)))
290          (check-result (result)
291            #!+high-security (aver (typep result output-type-spec))
292            result))
293     (let ((type (specifier-type output-type-spec)))
294       (cond
295         ((%typep object output-type-spec)
296          object)
297         ((eq type *empty-type*)
298          (coerce-error))
299         ((csubtypep type (specifier-type 'character))
300          (character object))
301         ((csubtypep type (specifier-type 'function))
302          (coerce-to-fun object))
303         ((numberp object)
304          (let ((res
305                 (cond
306                   ((csubtypep type (specifier-type 'single-float))
307                    (%single-float object))
308                   ((csubtypep type (specifier-type 'double-float))
309                    (%double-float object))
310                   #!+long-float
311                   ((csubtypep type (specifier-type 'long-float))
312                    (%long-float object))
313                   ((csubtypep type (specifier-type 'float))
314                    (%single-float object))
315                   ((csubtypep type (specifier-type '(complex single-float)))
316                    (complex (%single-float (realpart object))
317                             (%single-float (imagpart object))))
318                   ((csubtypep type (specifier-type '(complex double-float)))
319                    (complex (%double-float (realpart object))
320                             (%double-float (imagpart object))))
321                   #!+long-float
322                   ((csubtypep type (specifier-type '(complex long-float)))
323                    (complex (%long-float (realpart object))
324                             (%long-float (imagpart object))))
325                   ((csubtypep type (specifier-type 'complex))
326                    (complex object))
327                   (t
328                    (coerce-error)))))
329            ;; If RES has the wrong type, that means that rule of
330            ;; canonical representation for complex rationals was
331            ;; invoked. According to the ANSI spec, (COERCE 7/2
332            ;; 'COMPLEX) returns 7/2. Thus, if the object was a
333            ;; rational, there is no error here.
334            (unless (or (typep res output-type-spec) (rationalp object))
335              (coerce-error))
336            res))
337         ((csubtypep type (specifier-type 'list))
338          (coerce-to-list object))
339         ((csubtypep type (specifier-type 'string))
340          (check-result (coerce-to-simple-string object)))
341         ((csubtypep type (specifier-type 'bit-vector))
342          (check-result (coerce-to-bit-vector object)))
343         ((csubtypep type (specifier-type 'vector))
344          (check-result (coerce-to-vector object output-type-spec)))
345         (t
346          (coerce-error))))))