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 (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
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
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
273 :expected-type '(not (satisfies special-operator-p))
274 :format-control "~S is a special operator."
275 :format-arguments (list object)))
280 ;;; new version, which seems as though it should be better, but which
281 ;;; does not yet work
283 (defun coerce (object output-type-spec)
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))
293 (let ((type (specifier-type output-type-spec)))
295 ((%typep object output-type-spec)
297 ((eq type *empty-type*)
299 ((csubtypep type (specifier-type 'character))
301 ((csubtypep type (specifier-type 'function))
302 (coerce-to-fun object))
306 ((csubtypep type (specifier-type 'single-float))
307 (%single-float object))
308 ((csubtypep type (specifier-type 'double-float))
309 (%double-float object))
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))))
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))
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))
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)))