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 constructor access src-type
&optional explicit-check
)
15 `(defun ,name
(object type
)
16 (declare (type ,src-type object
))
17 ,@(when explicit-check
`((declare (explicit-check))))
18 (do* ((index 0 (1+ index
))
19 (length (length object
))
22 ((>= index length
) result
)
23 (declare (fixnum length index
))
24 (declare (type vector result
))
25 (setf (,access result index
)
27 (list '(pop in-object
))
28 (vector '(aref in-object index
))
29 (sequence '(elt in-object index
))))))))
31 (def list-to-vector
* (make-sequence type length
) aref list t
)
33 (def vector-to-vector
* (make-sequence type length
) aref vector t
)
35 (def sequence-to-vector
* (make-sequence type length
) aref sequence
))
37 (defun vector-to-list* (object)
38 (declare (type vector object
))
39 (dx-let ((result (list nil
)))
40 (let ((splice result
))
41 (do-vector-data (elt object
(cdr result
))
42 (let ((cell (list elt
)))
43 (setf (cdr splice
) cell splice cell
))))))
45 (defun sequence-to-list (sequence)
46 (declare (type sequence sequence
))
47 (dx-let ((result (list nil
)))
48 (let ((splice result
))
49 (sb!sequence
:dosequence
(elt sequence
(cdr result
))
50 (let ((cell (list elt
)))
51 (setf (cdr splice
) cell splice cell
))))))
53 ;;; These are used both by the full DEFUN function and by various
54 ;;; optimization transforms in the constant-OUTPUT-TYPE-SPEC case.
56 ;;; Most of them are INLINE so that they can be optimized when the
57 ;;; argument type is known. It might be better to do this with
58 ;;; DEFTRANSFORMs, though.
59 (declaim (inline coerce-to-list
))
60 (declaim (inline coerce-to-vector
))
62 (defun coerce-symbol-to-fun (symbol)
63 ;; FIXME? I would think to use SYMBOL-FUNCTION here which does not strip off
64 ;; encapsulations. But Stas wrote FDEFINITION so ...
65 ;; [Also note, we won't encapsulate a macro or special-form, so this
66 ;; introspective technique to decide what kind something is works either way]
67 (let ((def (fdefinition symbol
)))
68 (if (macro/special-guard-fun-p def
)
69 (error (ecase (car (%fun-name def
))
70 (:macro
"~S names a macro.")
71 (:special
"~S names a special operator."))
75 (defun coerce-to-fun (object)
76 ;; (Unlike the other COERCE-TO-FOOs, this one isn't inline, because
77 ;; it's so big and because optimizing away the outer ETYPECASE
78 ;; doesn't seem to buy us that much anyway.)
82 (coerce-symbol-to-fun object
))
90 (error 'simple-type-error
92 :expected-type
'(or symbol
93 ;; KLUDGE: ANSI wants us to
94 ;; return a TYPE-ERROR here, and
95 ;; a TYPE-ERROR is supposed to
96 ;; describe the expected type,
97 ;; but it's not obvious how to
98 ;; describe the coerceable cons
99 ;; types, so we punt and just say
100 ;; CONS. -- WHN 20000503
102 :format-control
"~S can't be coerced to a function."
103 :format-arguments
(list object
)))))))
105 (defun coerce-to-list (object)
108 (vector-to-list* object
)
109 (sequence-to-list object
)))
111 (defun coerce-to-vector (object output-type-spec
)
113 (list (list-to-vector* object output-type-spec
))
114 (vector (vector-to-vector* object output-type-spec
))))
116 ;;; old working version
117 (defun coerce (object output-type-spec
)
119 "Coerce the Object to an object of type Output-Type-Spec."
120 (declare (explicit-check))
121 (flet ((coerce-error ()
122 (error 'simple-type-error
123 :format-control
"~S can't be converted to type ~S."
124 :format-arguments
(list object output-type-spec
)
126 :expected-type output-type-spec
)))
127 (let ((type (specifier-type output-type-spec
)))
129 ((%%typep object type
)
131 ((eq type
*empty-type
*)
133 ((type= type
(specifier-type 'character
))
137 ((csubtypep type
(specifier-type 'single-float
))
138 (let ((res (%single-float object
)))
139 (unless (typep res output-type-spec
)
142 ((csubtypep type
(specifier-type 'double-float
))
143 (let ((res (%double-float object
)))
144 (unless (typep res output-type-spec
)
148 ((csubtypep type
(specifier-type 'long-float
))
149 (let ((res (%long-float object
)))
150 (unless (typep res output-type-spec
)
153 ((csubtypep type
(specifier-type 'float
))
154 (let ((res (%single-float object
)))
155 (unless (typep res output-type-spec
)
161 ((csubtypep type
(specifier-type '(complex single-float
)))
162 (complex (%single-float
(realpart object
))
163 (%single-float
(imagpart object
))))
164 ((csubtypep type
(specifier-type '(complex double-float
)))
165 (complex (%double-float
(realpart object
))
166 (%double-float
(imagpart object
))))
168 ((csubtypep type
(specifier-type '(complex long-float
)))
169 (complex (%long-float
(realpart object
))
170 (%long-float
(imagpart object
))))
171 ((csubtypep type
(specifier-type '(complex float
)))
172 (complex (%single-float
(realpart object
))
173 (%single-float
(imagpart object
))))
174 ((and (typep object
'rational
) ; TODO jmoringe unreachable?
175 (csubtypep type
(specifier-type '(complex float
))))
176 ;; Perhaps somewhat surprisingly, ANSI specifies
177 ;; that (COERCE FOO 'FLOAT) is a SINGLE-FLOAT,
178 ;; not dispatching on
179 ;; *READ-DEFAULT-FLOAT-FORMAT*. By analogy, we
180 ;; do the same for complex numbers. -- CSR,
182 (complex (%single-float object
)))
183 ((csubtypep type
(specifier-type 'complex
))
187 ;; If RES has the wrong type, that means that rule of
188 ;; canonical representation for complex rationals was
189 ;; invoked. According to the Hyperspec, (coerce 7/2
190 ;; 'complex) returns 7/2. Thus, if the object was a
191 ;; rational, there is no error here.
192 (unless (or (typep res output-type-spec
)
196 ((csubtypep type
(specifier-type 'list
))
199 ((type= type
(specifier-type 'list
))
200 (vector-to-list* object
))
201 ((type= type
(specifier-type 'null
))
202 (if (= (length object
) 0)
204 (sequence-type-length-mismatch-error type
207 (multiple-value-bind (min exactp
)
208 (sb!kernel
::cons-type-length-info type
)
209 (let ((length (length object
)))
211 (unless (= length min
)
212 (sequence-type-length-mismatch-error type length
))
213 (unless (>= length min
)
214 (sequence-type-length-mismatch-error type length
)))
215 (vector-to-list* object
))))
216 (t (sequence-type-too-hairy (type-specifier type
))))
217 (if (sequencep object
)
219 ((type= type
(specifier-type 'list
))
220 (sb!sequence
:make-sequence-like
221 nil
(length object
) :initial-contents object
))
222 ((type= type
(specifier-type 'null
))
223 (if (= (length object
) 0)
225 (sequence-type-length-mismatch-error type
228 (multiple-value-bind (min exactp
)
229 (sb!kernel
::cons-type-length-info type
)
230 (let ((length (length object
)))
232 (unless (= length min
)
233 (sequence-type-length-mismatch-error type length
))
234 (unless (>= length min
)
235 (sequence-type-length-mismatch-error type length
)))
236 (sb!sequence
:make-sequence-like
237 nil length
:initial-contents object
))))
238 (t (sequence-type-too-hairy (type-specifier type
))))
240 ((csubtypep type
(specifier-type 'vector
))
242 ;; FOO-TO-VECTOR* go through MAKE-SEQUENCE, so length
243 ;; errors are caught there. -- CSR, 2002-10-18
244 (list (list-to-vector* object output-type-spec
))
245 (vector (vector-to-vector* object output-type-spec
))
246 (sequence (sequence-to-vector* object output-type-spec
))
249 ((and (csubtypep type
(specifier-type 'sequence
))
250 (find-class output-type-spec nil
))
251 (let ((prototype (sb!mop
:class-prototype
252 (sb!pcl
:ensure-class-finalized
253 (find-class output-type-spec
)))))
254 (sb!sequence
:make-sequence-like
255 prototype
(length object
) :initial-contents object
)))
256 ((csubtypep type
(specifier-type 'function
))
257 (coerce-to-fun object
))
261 ;;; new version, which seems as though it should be better, but which
262 ;;; does not yet work
264 (defun coerce (object output-type-spec
)
266 "Coerces the Object to an object of type Output-Type-Spec."
267 (flet ((coerce-error ()
268 (error 'simple-type-error
269 :format-control
"~S can't be converted to type ~S."
270 :format-arguments
(list object output-type-spec
)))
271 (check-result (result)
272 #!+high-security
(aver (typep result output-type-spec
))
274 (let ((type (specifier-type output-type-spec
)))
276 ((%%typep object type
)
278 ((eq type
*empty-type
*)
280 ((csubtypep type
(specifier-type 'character
))
282 ((csubtypep type
(specifier-type 'function
))
283 (coerce-to-fun object
))
287 ((csubtypep type
(specifier-type 'single-float
))
288 (%single-float object
))
289 ((csubtypep type
(specifier-type 'double-float
))
290 (%double-float object
))
292 ((csubtypep type
(specifier-type 'long-float
))
293 (%long-float object
))
294 ((csubtypep type
(specifier-type 'float
))
295 (%single-float object
))
296 ((csubtypep type
(specifier-type '(complex single-float
)))
297 (complex (%single-float
(realpart object
))
298 (%single-float
(imagpart object
))))
299 ((csubtypep type
(specifier-type '(complex double-float
)))
300 (complex (%double-float
(realpart object
))
301 (%double-float
(imagpart object
))))
303 ((csubtypep type
(specifier-type '(complex long-float
)))
304 (complex (%long-float
(realpart object
))
305 (%long-float
(imagpart object
))))
306 ((csubtypep type
(specifier-type 'complex
))
310 ;; If RES has the wrong type, that means that rule of
311 ;; canonical representation for complex rationals was
312 ;; invoked. According to the ANSI spec, (COERCE 7/2
313 ;; 'COMPLEX) returns 7/2. Thus, if the object was a
314 ;; rational, there is no error here.
315 (unless (or (typep res output-type-spec
) (rationalp object
))
318 ((csubtypep type
(specifier-type 'list
))
319 (coerce-to-list object
))
320 ((csubtypep type
(specifier-type 'string
))
321 (check-result (coerce-to-simple-string object
)))
322 ((csubtypep type
(specifier-type 'bit-vector
))
323 (check-result (coerce-to-bit-vector object
)))
324 ((csubtypep type
(specifier-type 'vector
))
325 (check-result (coerce-to-vector object output-type-spec
)))