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 (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 list-to-vector* (make-sequence type length)
33 (def vector-to-vector* (make-sequence type length)
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))))))
46 (defvar *offending-datum*); FIXME: Remove after debugging COERCE.
48 ;;; These are used both by the full DEFUN function and by various
49 ;;; optimization transforms in the constant-OUTPUT-TYPE-SPEC case.
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.)
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))))
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)))
77 (error 'simple-type-error
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
89 :format-control "~S can't be coerced to a function."
90 :format-arguments (list object)))))))
92 (defun coerce-to-list (object)
94 (vector (vector-to-list* object))))
96 (defun coerce-to-vector (object output-type-spec)
98 (list (list-to-vector* object output-type-spec))
99 (vector (vector-to-vector* object output-type-spec))))
101 ;;; old working version
102 (defun coerce (object output-type-spec)
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 (let ((type (specifier-type output-type-spec)))
112 ((%typep object output-type-spec)
114 ((eq type *empty-type*)
116 ((csubtypep type (specifier-type 'character))
118 ((csubtypep type (specifier-type 'function))
120 (when (and (or (symbolp object)
122 (= (length object) 2)
123 (eq (car object) 'setf)))
124 (not (fboundp object)))
125 (error 'simple-type-error
127 ;; FIXME: SATISFIES FBOUNDP is a kinda bizarre broken
128 ;; type specifier, since the set of values it describes
129 ;; isn't in general constant in time. Maybe we could
130 ;; find a better way of expressing this error? (Maybe
131 ;; with the UNDEFINED-FUNCTION condition?)
132 :expected-type '(satisfies fboundp)
133 :format-control "~S isn't fbound."
134 :format-arguments (list object)))
136 (when (and (symbolp object)
137 (sb!xc:macro-function object))
138 (error 'simple-type-error
140 :expected-type '(not (satisfies sb!xc:macro-function))
141 :format-control "~S is a macro."
142 :format-arguments (list object)))
144 (when (and (symbolp object)
145 (special-operator-p object))
146 (error 'simple-type-error
148 :expected-type '(not (satisfies special-operator-p))
149 :format-control "~S is a special operator."
150 :format-arguments (list object)))
155 ((csubtypep type (specifier-type 'single-float))
156 (%single-float object))
157 ((csubtypep type (specifier-type 'double-float))
158 (%double-float object))
160 ((csubtypep type (specifier-type 'long-float))
161 (%long-float object))
162 ((csubtypep type (specifier-type 'float))
163 (%single-float object))
164 ((csubtypep type (specifier-type '(complex single-float)))
165 (complex (%single-float (realpart object))
166 (%single-float (imagpart object))))
167 ((csubtypep type (specifier-type '(complex double-float)))
168 (complex (%double-float (realpart object))
169 (%double-float (imagpart object))))
171 ((csubtypep type (specifier-type '(complex long-float)))
172 (complex (%long-float (realpart object))
173 (%long-float (imagpart object))))
174 ((and (typep object 'rational)
175 (csubtypep type (specifier-type '(complex float))))
176 ;; Perhaps somewhat surprisingly, ANSI specifies
177 ;; that (COERCE FOO 'FLOAT) is a SINGLE-FLOAT, not
178 ;; dispatching on *READ-DEFAULT-FLOAT-FORMAT*. By
179 ;; analogy, we do the same for complex numbers. --
181 (complex (%single-float object)))
182 ((csubtypep type (specifier-type 'complex))
186 ;; If RES has the wrong type, that means that rule of canonical
187 ;; representation for complex rationals was invoked. According to
188 ;; the Hyperspec, (coerce 7/2 'complex) returns 7/2. Thus, if the
189 ;; object was a rational, there is no error here.
190 (unless (or (typep res output-type-spec) (rationalp object))
193 ((csubtypep type (specifier-type 'list))
195 (vector-to-list* object)
197 ((csubtypep type (specifier-type 'vector))
199 (list (list-to-vector* object output-type-spec))
200 (vector (vector-to-vector* object output-type-spec))
206 ;;; new version, which seems as though it should be better, but which
207 ;;; does not yet work
209 (defun coerce (object output-type-spec)
211 "Coerces the Object to an object of type Output-Type-Spec."
212 (flet ((coerce-error ()
213 (error 'simple-type-error
214 :format-control "~S can't be converted to type ~S."
215 :format-arguments (list object output-type-spec)))
216 (check-result (result)
217 #!+high-security (aver (typep result output-type-spec))
219 (let ((type (specifier-type output-type-spec)))
221 ((%typep object output-type-spec)
223 ((eq type *empty-type*)
225 ((csubtypep type (specifier-type 'character))
227 ((csubtypep type (specifier-type 'function))
228 (coerce-to-fun object))
232 ((csubtypep type (specifier-type 'single-float))
233 (%single-float object))
234 ((csubtypep type (specifier-type 'double-float))
235 (%double-float object))
237 ((csubtypep type (specifier-type 'long-float))
238 (%long-float object))
239 ((csubtypep type (specifier-type 'float))
240 (%single-float object))
241 ((csubtypep type (specifier-type '(complex single-float)))
242 (complex (%single-float (realpart object))
243 (%single-float (imagpart object))))
244 ((csubtypep type (specifier-type '(complex double-float)))
245 (complex (%double-float (realpart object))
246 (%double-float (imagpart object))))
248 ((csubtypep type (specifier-type '(complex long-float)))
249 (complex (%long-float (realpart object))
250 (%long-float (imagpart object))))
251 ((csubtypep type (specifier-type 'complex))
255 ;; If RES has the wrong type, that means that rule of
256 ;; canonical representation for complex rationals was
257 ;; invoked. According to the ANSI spec, (COERCE 7/2
258 ;; 'COMPLEX) returns 7/2. Thus, if the object was a
259 ;; rational, there is no error here.
260 (unless (or (typep res output-type-spec) (rationalp object))
263 ((csubtypep type (specifier-type 'list))
264 (coerce-to-list object))
265 ((csubtypep type (specifier-type 'string))
266 (check-result (coerce-to-simple-string object)))
267 ((csubtypep type (specifier-type 'bit-vector))
268 (check-result (coerce-to-bit-vector object)))
269 ((csubtypep type (specifier-type 'vector))
270 (check-result (coerce-to-vector object output-type-spec)))