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