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-frob (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
23 ((= index length) result)
24 (declare (fixnum length index))
25 (setf (,access result index)
27 (:list '(pop in-object))
28 (:vector '(aref in-object index))))))))
30 (def-frob list-to-simple-string* (make-string length) schar :list)
32 (def-frob list-to-bit-vector* (make-array length :element-type '(mod 2))
35 (def-frob list-to-vector* (make-sequence-of-type type length)
38 (def-frob vector-to-vector* (make-sequence-of-type type length)
41 (def-frob vector-to-simple-string* (make-string length) schar :vector)
43 (def-frob vector-to-bit-vector* (make-array length :element-type '(mod 2))
46 (defun vector-to-list* (object)
47 (let ((result (list nil))
48 (length (length object)))
49 (declare (fixnum length))
50 (do ((index 0 (1+ index))
51 (splice result (cdr splice)))
52 ((= index length) (cdr result))
53 (declare (fixnum index))
54 (rplacd splice (list (aref object index))))))
56 (defun string-to-simple-string* (object)
57 (if (simple-string-p object)
59 (with-array-data ((data object)
61 (end (length object)))
62 (declare (simple-string data))
63 (subseq data start end))))
65 (defun bit-vector-to-simple-bit-vector* (object)
66 (if (simple-bit-vector-p object)
68 (with-array-data ((data object)
70 (end (length object)))
71 (declare (simple-bit-vector data))
72 (subseq data start end))))
74 (defvar *offending-datum*); FIXME: Remove after debugging COERCE.
76 ;;; These are used both by the full DEFUN function and by various
77 ;;; optimization transforms in the constant-OUTPUT-TYPE-SPEC case.
79 ;;; Most of them are INLINE so that they can be optimized when the
80 ;;; argument type is known. It might be better to do this with
81 ;;; DEFTRANSFORMs, though.
82 (declaim (inline coerce-to-list))
83 (declaim (inline coerce-to-simple-string coerce-to-bit-vector coerce-to-vector))
84 (defun coerce-to-function (object)
85 ;; (Unlike the other COERCE-TO-FOOs, this one isn't inline, because
86 ;; it's so big and because optimizing away the outer ETYPECASE
87 ;; doesn't seem to buy us that much anyway.)
90 ;; ANSI lets us return ordinary errors (non-TYPE-ERRORs) here.
91 (cond ((macro-function object)
92 (error "~S names a macro." object))
93 ((special-operator-p object)
94 (error "~S is a special operator." object))
95 (t (fdefinition object))))
100 ((lambda instance-lambda)
101 ;; FIXME: If we go to a compiler-only implementation, this can
102 ;; become COMPILE instead of EVAL, which seems nicer to me.
103 (eval `(function ,object)))
105 (error 'simple-type-error
107 :expected-type '(or symbol
108 ;; KLUDGE: ANSI wants us to
109 ;; return a TYPE-ERROR here, and
110 ;; a TYPE-ERROR is supposed to
111 ;; describe the expected type,
112 ;; but it's not obvious how to
113 ;; describe the coerceable cons
114 ;; types, so we punt and just say
115 ;; CONS. -- WHN 20000503
117 :format-control "~S can't be coerced to a function."
118 :format-arguments (list object)))))))
119 (defun coerce-to-list (object)
121 (vector (vector-to-list* object))))
122 (defun coerce-to-simple-string (object)
124 (list (list-to-simple-string* object))
125 (string (string-to-simple-string* object))
126 (vector (vector-to-simple-string* object))))
127 (defun coerce-to-bit-vector (object)
129 (list (list-to-bit-vector* object))
130 (vector (vector-to-bit-vector* object))))
131 (defun coerce-to-vector (object output-type-spec)
133 (list (list-to-vector* object output-type-spec))
134 (vector (vector-to-vector* object output-type-spec))))
136 ;;; old working version
137 (defun coerce (object output-type-spec)
139 "Coerces the Object to an object of type Output-Type-Spec."
140 (flet ((coerce-error ()
141 (/show0 "entering COERCE-ERROR")
142 (error 'simple-type-error
143 :format-control "~S can't be converted to type ~S."
144 :format-arguments (list object output-type-spec)))
145 (check-result (result)
147 (check-type-var result output-type-spec)
149 (let ((type (specifier-type output-type-spec)))
151 ((%typep object output-type-spec)
153 ((eq type *empty-type*)
155 ((csubtypep type (specifier-type 'character))
157 ((csubtypep type (specifier-type 'function))
159 (when (and (or (symbolp object)
161 (= (length object) 2)
162 (eq (car object) 'setf)))
163 (not (fboundp object)))
164 (error 'simple-type-error
166 :expected-type '(satisfies fboundp)
167 :format-control "~S isn't fbound."
168 :format-arguments (list object)))
170 (when (and (symbolp object)
171 (sb!xc:macro-function object))
172 (error 'simple-type-error
174 :expected-type '(not (satisfies sb!xc:macro-function))
175 :format-control "~S is a macro."
176 :format-arguments (list object)))
178 (when (and (symbolp object)
179 (special-operator-p object))
180 (error 'simple-type-error
182 :expected-type '(not (satisfies special-operator-p))
183 :format-control "~S is a special operator."
184 :format-arguments (list object)))
189 ((csubtypep type (specifier-type 'single-float))
190 (%single-float object))
191 ((csubtypep type (specifier-type 'double-float))
192 (%double-float object))
194 ((csubtypep type (specifier-type 'long-float))
195 (%long-float object))
196 ((csubtypep type (specifier-type 'float))
197 (%single-float object))
198 ((csubtypep type (specifier-type '(complex single-float)))
199 (complex (%single-float (realpart object))
200 (%single-float (imagpart object))))
201 ((csubtypep type (specifier-type '(complex double-float)))
202 (complex (%double-float (realpart object))
203 (%double-float (imagpart object))))
205 ((csubtypep type (specifier-type '(complex long-float)))
206 (complex (%long-float (realpart object))
207 (%long-float (imagpart object))))
208 ((csubtypep type (specifier-type 'complex))
212 ;; If RES has the wrong type, that means that rule of canonical
213 ;; representation for complex rationals was invoked. According to
214 ;; the Hyperspec, (coerce 7/2 'complex) returns 7/2. Thus, if the
215 ;; object was a rational, there is no error here.
216 (unless (or (typep res output-type-spec) (rationalp object))
219 ((csubtypep type (specifier-type 'list))
221 (vector-to-list* object)
223 ((csubtypep type (specifier-type 'string))
226 (list (list-to-simple-string* object))
227 (string (string-to-simple-string* object))
228 (vector (vector-to-simple-string* object))
231 ((csubtypep type (specifier-type 'bit-vector))
234 (list (list-to-bit-vector* object))
235 (vector (vector-to-bit-vector* object))
238 ((csubtypep type (specifier-type 'vector))
241 (list (list-to-vector* object output-type-spec))
242 (vector (vector-to-vector* object output-type-spec))
248 ;;; new version, which seems as though it should be better, but which
249 ;;; does not yet work
251 (defun coerce (object output-type-spec)
253 "Coerces the Object to an object of type Output-Type-Spec."
254 (flet ((coerce-error ()
255 (error 'simple-type-error
256 :format-control "~S can't be converted to type ~S."
257 :format-arguments (list object output-type-spec)))
258 (check-result (result)
260 (check-type-var result output-type-spec)
262 (let ((type (specifier-type output-type-spec)))
264 ((%typep object output-type-spec)
266 ((eq type *empty-type*)
268 ((csubtypep type (specifier-type 'character))
270 ((csubtypep type (specifier-type 'function))
271 (coerce-to-function object))
275 ((csubtypep type (specifier-type 'single-float))
276 (%single-float object))
277 ((csubtypep type (specifier-type 'double-float))
278 (%double-float object))
280 ((csubtypep type (specifier-type 'long-float))
281 (%long-float object))
282 ((csubtypep type (specifier-type 'float))
283 (%single-float object))
284 ((csubtypep type (specifier-type '(complex single-float)))
285 (complex (%single-float (realpart object))
286 (%single-float (imagpart object))))
287 ((csubtypep type (specifier-type '(complex double-float)))
288 (complex (%double-float (realpart object))
289 (%double-float (imagpart object))))
291 ((csubtypep type (specifier-type '(complex long-float)))
292 (complex (%long-float (realpart object))
293 (%long-float (imagpart object))))
294 ((csubtypep type (specifier-type 'complex))
298 ;; If RES has the wrong type, that means that rule of
299 ;; canonical representation for complex rationals was
300 ;; invoked. According to the ANSI spec, (COERCE 7/2
301 ;; 'COMPLEX) returns 7/2. Thus, if the object was a
302 ;; rational, there is no error here.
303 (unless (or (typep res output-type-spec) (rationalp object))
306 ((csubtypep type (specifier-type 'list))
307 (coerce-to-list object))
308 ((csubtypep type (specifier-type 'string))
309 (check-result (coerce-to-simple-string object)))
310 ((csubtypep type (specifier-type 'bit-vector))
311 (check-result (coerce-to-bit-vector object)))
312 ((csubtypep type (specifier-type 'vector))
313 (check-result (coerce-to-vector object output-type-spec)))