1 ;;;; COERCE and related code
3 ;;;; This software is part of the SBCL system. See the README file for
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!IMPL")
14 (macrolet ((def (name result access src-type &optional typep)
15 `(defun ,name (object ,@(if typep '(type) ()))
16 (declare (type ,(ecase src-type
19 (:sequence 'sequence)) object))
20 (do* ((index 0 (1+ index))
21 (length (length object))
24 ((>= index length) result)
25 (declare (fixnum length index))
26 (declare (type vector result))
27 (setf (,access result index)
29 (:list '(pop in-object))
30 (:vector '(aref in-object index))
31 (:sequence '(elt in-object index))))))))
33 (def list-to-vector* (make-sequence type length)
36 (def vector-to-vector* (make-sequence type length)
39 (def sequence-to-vector* (make-sequence type length)
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))))))
53 (defvar *offending-datum*); FIXME: Remove after debugging COERCE.
55 ;;; These are used both by the full DEFUN function and by various
56 ;;; optimization transforms in the constant-OUTPUT-TYPE-SPEC case.
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.)
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))))
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)))
84 (error 'simple-type-error
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
96 :format-control "~S can't be coerced to a function."
97 :format-arguments (list object)))))))
99 (defun coerce-to-list (object)
101 (vector (vector-to-list* object))))
103 (defun coerce-to-vector (object output-type-spec)
105 (list (list-to-vector* object output-type-spec))
106 (vector (vector-to-vector* object output-type-spec))))
108 ;;; old working version
109 (defun coerce (object output-type-spec)
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)
118 :expected-type output-type-spec)))
119 (let ((type (specifier-type output-type-spec)))
121 ((%typep object output-type-spec)
123 ((eq type *empty-type*)
125 ((csubtypep type (specifier-type 'character))
129 ((csubtypep type (specifier-type 'single-float))
130 (let ((res (%single-float object)))
131 (unless (typep res output-type-spec)
134 ((csubtypep type (specifier-type 'double-float))
135 (let ((res (%double-float object)))
136 (unless (typep res output-type-spec)
140 ((csubtypep type (specifier-type 'long-float))
141 (let ((res (%long-float object)))
142 (unless (typep res output-type-spec)
145 ((csubtypep type (specifier-type 'float))
146 (let ((res (%single-float object)))
147 (unless (typep res output-type-spec)
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))))
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,
174 (complex (%single-float object)))
175 ((csubtypep type (specifier-type 'complex))
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)
188 ((csubtypep type (specifier-type 'list))
191 ((type= type (specifier-type 'list))
192 (vector-to-list* object))
193 ((type= type (specifier-type 'null))
194 (if (= (length object) 0)
196 (sequence-type-length-mismatch-error type
199 (multiple-value-bind (min exactp)
200 (sb!kernel::cons-type-length-info type)
201 (let ((length (length object)))
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)
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)
217 (sequence-type-length-mismatch-error type
220 (multiple-value-bind (min exactp)
221 (sb!kernel::cons-type-length-info type)
222 (let ((length (length object)))
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))))
232 ((csubtypep type (specifier-type 'vector))
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))
241 ((and (csubtypep type (specifier-type 'sequence))
242 (find-class output-type-spec nil))
243 (let ((class (find-class output-type-spec)))
244 (sb!sequence:make-sequence-like
245 (sb!mop:class-prototype class)
246 (length object) :initial-contents object)))
247 ((csubtypep type (specifier-type 'function))
248 (when (and (legal-fun-name-p object)
249 (not (fboundp object)))
250 (error 'simple-type-error
252 ;; FIXME: SATISFIES FBOUNDP is a kinda bizarre broken
253 ;; type specifier, since the set of values it describes
254 ;; isn't in general constant in time. Maybe we could
255 ;; find a better way of expressing this error? (Maybe
256 ;; with the UNDEFINED-FUNCTION condition?)
257 :expected-type '(satisfies fboundp)
258 :format-control "~S isn't fbound."
259 :format-arguments (list object)))
260 (when (and (symbolp object)
261 (sb!xc:macro-function object))
262 (error 'simple-type-error
264 :expected-type '(not (satisfies sb!xc:macro-function))
265 :format-control "~S is a macro."
266 :format-arguments (list object)))
267 (when (and (symbolp object)
268 (special-operator-p object))
269 (error 'simple-type-error
271 :expected-type '(not (satisfies special-operator-p))
272 :format-control "~S is a special operator."
273 :format-arguments (list object)))
278 ;;; new version, which seems as though it should be better, but which
279 ;;; does not yet work
281 (defun coerce (object output-type-spec)
283 "Coerces the Object to an object of type Output-Type-Spec."
284 (flet ((coerce-error ()
285 (error 'simple-type-error
286 :format-control "~S can't be converted to type ~S."
287 :format-arguments (list object output-type-spec)))
288 (check-result (result)
289 #!+high-security (aver (typep result output-type-spec))
291 (let ((type (specifier-type output-type-spec)))
293 ((%typep object output-type-spec)
295 ((eq type *empty-type*)
297 ((csubtypep type (specifier-type 'character))
299 ((csubtypep type (specifier-type 'function))
300 (coerce-to-fun object))
304 ((csubtypep type (specifier-type 'single-float))
305 (%single-float object))
306 ((csubtypep type (specifier-type 'double-float))
307 (%double-float object))
309 ((csubtypep type (specifier-type 'long-float))
310 (%long-float object))
311 ((csubtypep type (specifier-type 'float))
312 (%single-float object))
313 ((csubtypep type (specifier-type '(complex single-float)))
314 (complex (%single-float (realpart object))
315 (%single-float (imagpart object))))
316 ((csubtypep type (specifier-type '(complex double-float)))
317 (complex (%double-float (realpart object))
318 (%double-float (imagpart object))))
320 ((csubtypep type (specifier-type '(complex long-float)))
321 (complex (%long-float (realpart object))
322 (%long-float (imagpart object))))
323 ((csubtypep type (specifier-type 'complex))
327 ;; If RES has the wrong type, that means that rule of
328 ;; canonical representation for complex rationals was
329 ;; invoked. According to the ANSI spec, (COERCE 7/2
330 ;; 'COMPLEX) returns 7/2. Thus, if the object was a
331 ;; rational, there is no error here.
332 (unless (or (typep res output-type-spec) (rationalp object))
335 ((csubtypep type (specifier-type 'list))
336 (coerce-to-list object))
337 ((csubtypep type (specifier-type 'string))
338 (check-result (coerce-to-simple-string object)))
339 ((csubtypep type (specifier-type 'bit-vector))
340 (check-result (coerce-to-bit-vector object)))
341 ((csubtypep type (specifier-type 'vector))
342 (check-result (coerce-to-vector object output-type-spec)))