1 ;;;; lots of basic macros for the target SBCL
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")
17 ;;;; ASSERT and CHECK-TYPE
19 ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how
20 ;;; closures are compiled. RESTART-CASE has forms with closures that
21 ;;; the compiler causes to be generated at the top of any function
22 ;;; using RESTART-CASE, regardless of whether they are needed. Thus if
23 ;;; we just wrapped a RESTART-CASE around the call to ERROR, we'd have
24 ;;; to do a significant amount of work at runtime allocating and
25 ;;; deallocating the closures regardless of whether they were ever
28 ;;; ASSERT-ERROR isn't defined until a later file because it uses the
29 ;;; macro RESTART-CASE, which isn't defined until a later file.
30 (defmacro-mundanely assert (test-form &optional places datum &rest arguments)
32 "Signals an error if the value of test-form is nil. Continuing from this
33 error using the CONTINUE restart will allow the user to alter the value of
34 some locations known to SETF, starting over with test-form. Returns nil."
36 (assert-error ',test-form ',places ,datum ,@arguments)
37 ,@(mapcar #'(lambda (place)
38 `(setf ,place (assert-prompt ',place ,place)))
41 (defun assert-prompt (name value)
42 (cond ((y-or-n-p "The old value of ~S is ~S.~
43 ~%Do you want to supply a new value? "
45 (format *query-io* "~&Type a form to be evaluated:~%")
46 (flet ((read-it () (eval (read *query-io*))))
47 (if (symbolp name) ;help user debug lexical variables
48 (progv (list name) (list value) (read-it))
52 ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because
53 ;;; of how closures are compiled. RESTART-CASE has forms with closures
54 ;;; that the compiler causes to be generated at the top of any
55 ;;; function using RESTART-CASE, regardless of whether they are
56 ;;; needed. Because it would be nice if CHECK-TYPE were cheap to use,
57 ;;; and some things (e.g., READ-CHAR) can't afford this excessive
58 ;;; consing, we bend backwards a little.
60 ;;; FIXME: In reality, this restart cruft is needed hardly anywhere in
61 ;;; the system. Write NEED and NEED-TYPE to replace ASSERT and
62 ;;; CHECK-TYPE inside the system.
64 ;;; CHECK-TYPE-ERROR isn't defined until a later file because it uses
65 ;;; the macro RESTART-CASE, which isn't defined until a later file.
66 (defmacro-mundanely check-type (place type &optional type-string)
68 "Signals a restartable error of type TYPE-ERROR if the value of PLACE is
69 not of the specified type. If an error is signalled and the restart is
72 STORE-VALUE is invoked. It will store into PLACE and start over."
73 (let ((place-value (gensym)))
74 `(do ((,place-value ,place))
75 ((typep ,place-value ',type))
77 (check-type-error ',place ,place-value ',type ,type-string)))))
79 #!+high-security-support
80 (defmacro-mundanely check-type-var (place type-var &optional type-string)
82 "Signals an error of type type-error if the contents of place are not of the
83 specified type to which the type-var evaluates. If an error is signaled,
84 this can only return if STORE-VALUE is invoked. It will store into place
86 (let ((place-value (gensym))
87 (type-value (gensym)))
88 `(do ((,place-value ,place)
89 (,type-value ,type-var))
90 ((typep ,place-value ,type-value))
92 (check-type-error ',place ,place-value ,type-value ,type-string)))))
96 (defmacro-mundanely defconstant (var val &optional doc)
98 "For defining global constants at top level. The DEFCONSTANT says that the
99 value is constant and may be compiled into code. If the variable already has
100 a value, and this is not equal to the init, an error is signalled. The third
101 argument is an optional documentation string for the variable."
102 `(sb!c::%defconstant ',var ,val ',doc))
104 ;;; These are like the other %MUMBLEs except that we currently
105 ;;; actually do something interesting at load time, namely checking
106 ;;; whether the constant is being redefined.
107 (defun sb!c::%defconstant (name value doc)
108 (sb!c::%%defconstant name value doc))
109 #+sb-xc-host (sb!xc:proclaim '(ftype function sb!c::%%defconstant)) ; to avoid
110 ; undefined function warnings
111 (defun sb!c::%%defconstant (name value doc)
113 (setf (fdocumentation name 'variable) doc))
115 (unless (equalp (symbol-value name) value)
116 (cerror "Go ahead and change the value."
117 "The constant ~S is being redefined."
119 (setf (symbol-value name) value)
120 (setf (info :variable :kind name) :constant)
121 (clear-info :variable :constant-value name)
124 ;;;; DEFINE-COMPILER-MACRO
126 ;;; FIXME: The logic here for handling compiler macros named (SETF
127 ;;; FOO) was added after the fork from SBCL, is not well tested, and
128 ;;; may conflict with subtleties of the ANSI standard. E.g. section
129 ;;; "3.2.2.1 Compiler Macros" says that creating a lexical binding for
130 ;;; a function name shadows a compiler macro, and it's not clear that
131 ;;; that works with this version. It should be tested.
132 (defmacro-mundanely define-compiler-macro (name lambda-list &body body)
134 "Define a compiler-macro for NAME."
135 (let ((whole (gensym "WHOLE-"))
136 (environment (gensym "ENV-")))
137 (multiple-value-bind (body local-decs doc)
138 (parse-defmacro lambda-list whole body name 'define-compiler-macro
139 :environment environment)
140 (let ((def `(lambda (,whole ,environment)
142 (block ,(function-name-block-name name)
144 `(sb!c::%define-compiler-macro ',name #',def ',lambda-list ,doc)))))
145 (defun sb!c::%define-compiler-macro (name definition lambda-list doc)
146 ;; FIXME: Why does this have to be an interpreted function? Shouldn't
148 (assert (sb!eval:interpreted-function-p definition))
149 (setf (sb!eval:interpreted-function-name definition)
150 (format nil "DEFINE-COMPILER-MACRO ~S" name))
151 (setf (sb!eval:interpreted-function-arglist definition) lambda-list)
152 (sb!c::%%define-compiler-macro name definition doc))
153 (defun sb!c::%%define-compiler-macro (name definition doc)
154 (setf (sb!xc:compiler-macro-function name) definition)
155 ;; FIXME: Add support for (SETF FDOCUMENTATION) when object is a list
156 ;; and type is COMPILER-MACRO. (Until then, we have to discard any
157 ;; compiler macro documentation for (SETF FOO).)
159 (setf (fdocumentation name 'compiler-macro) doc))
162 ;;;; CASE, TYPECASE, and friends
164 (eval-when (:compile-toplevel :load-toplevel :execute)
166 ;;; CASE-BODY (interface)
168 ;;; CASE-BODY returns code for all the standard "case" macros. Name is
169 ;;; the macro name, and keyform is the thing to case on. Multi-p
170 ;;; indicates whether a branch may fire off a list of keys; otherwise,
171 ;;; a key that is a list is interpreted in some way as a single key.
172 ;;; When multi-p, test is applied to the value of keyform and each key
173 ;;; for a given branch; otherwise, test is applied to the value of
174 ;;; keyform and the entire first element, instead of each part, of the
175 ;;; case branch. When errorp, no t or otherwise branch is permitted,
176 ;;; and an ERROR form is generated. When proceedp, it is an error to
177 ;;; omit errorp, and the ERROR form generated is executed within a
178 ;;; RESTART-CASE allowing keyform to be set and retested.
179 (defun case-body (name keyform cases multi-p test errorp proceedp needcasesp)
180 (unless (or cases (not needcasesp))
181 (warn "no clauses in ~S" name))
182 (let ((keyform-value (gensym))
187 (error "~S -- Bad clause in ~S." case name))
188 ((memq (car case) '(t otherwise))
190 (error 'simple-program-error
191 :format-control "No default clause is allowed in ~S: ~S"
192 :format-arguments (list name case))
193 (push `(t nil ,@(rest case)) clauses)))
194 ((and multi-p (listp (first case)))
195 (setf keys (append (first case) keys))
196 (push `((or ,@(mapcar #'(lambda (key)
197 `(,test ,keyform-value ',key))
202 (push (first case) keys)
203 (push `((,test ,keyform-value
204 ',(first case)) nil ,@(rest case)) clauses))))
205 (case-body-aux name keyform keyform-value clauses keys errorp proceedp
206 `(,(if multi-p 'member 'or) ,@keys))))
208 ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled
209 ;;; all the cases. Note: it is not necessary that the resulting code
210 ;;; signal case-failure conditions, but that's what KMP's prototype
211 ;;; code did. We call CASE-BODY-ERROR, because of how closures are
212 ;;; compiled. RESTART-CASE has forms with closures that the compiler
213 ;;; causes to be generated at the top of any function using the case
214 ;;; macros, regardless of whether they are needed.
216 ;;; The CASE-BODY-ERROR function is defined later, when the
217 ;;; RESTART-CASE macro has been defined.
218 (defun case-body-aux (name keyform keyform-value clauses keys
219 errorp proceedp expected-type)
221 (let ((block (gensym))
223 `(let ((,keyform-value ,keyform))
229 (cond ,@(nreverse clauses)
234 ',name ',keyform ,keyform-value
235 ',expected-type ',keys)))
237 `(let ((,keyform-value ,keyform))
238 (declare (ignorable ,keyform-value)) ; e.g. (CASE KEY (T))
242 `((t (error 'sb!conditions::case-failure
244 :datum ,keyform-value
245 :expected-type ',expected-type
246 :possibilities ',keys))))))))
249 (defmacro-mundanely case (keyform &body cases)
251 "CASE Keyform {({(Key*) | Key} Form*)}*
252 Evaluates the Forms in the first clause with a Key EQL to the value of
253 Keyform. If a singleton key is T then the clause is a default clause."
254 (case-body 'case keyform cases t 'eql nil nil nil))
256 (defmacro-mundanely ccase (keyform &body cases)
258 "CCASE Keyform {({(Key*) | Key} Form*)}*
259 Evaluates the Forms in the first clause with a Key EQL to the value of
260 Keyform. If none of the keys matches then a correctable error is
262 (case-body 'ccase keyform cases t 'eql t t t))
264 (defmacro-mundanely ecase (keyform &body cases)
266 "ECASE Keyform {({(Key*) | Key} Form*)}*
267 Evaluates the Forms in the first clause with a Key EQL to the value of
268 Keyform. If none of the keys matches then an error is signalled."
269 (case-body 'ecase keyform cases t 'eql t nil t))
271 (defmacro-mundanely typecase (keyform &body cases)
273 "TYPECASE Keyform {(Type Form*)}*
274 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
276 (case-body 'typecase keyform cases nil 'typep nil nil nil))
278 (defmacro-mundanely ctypecase (keyform &body cases)
280 "CTYPECASE Keyform {(Type Form*)}*
281 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
282 is true. If no form is satisfied then a correctable error is signalled."
283 (case-body 'ctypecase keyform cases nil 'typep t t t))
285 (defmacro-mundanely etypecase (keyform &body cases)
287 "ETYPECASE Keyform {(Type Form*)}*
288 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
289 is true. If no form is satisfied then an error is signalled."
290 (case-body 'etypecase keyform cases nil 'typep t nil t))
292 ;;;; WITH-FOO i/o-related macros
294 (defmacro-mundanely with-open-stream ((var stream) &body forms-decls)
295 (multiple-value-bind (forms decls) (parse-body forms-decls nil)
296 (let ((abortp (gensym)))
297 `(let ((,var ,stream)
301 (multiple-value-prog1
305 (close ,var :abort ,abortp)))))))
307 (defmacro-mundanely with-open-file ((stream filespec &rest options)
309 `(with-open-stream (,stream (open ,filespec ,@options))
312 (defmacro-mundanely with-input-from-string ((var string &key index start end)
314 (multiple-value-bind (forms decls) (parse-body forms-decls nil)
315 ;; The ONCE-ONLY inhibits compiler note for unreachable code when
317 (once-only ((string string))
320 `(make-string-input-stream ,string ,(or start 0)))
323 (make-string-input-stream ,string
326 (make-string-input-stream ,string
329 `(make-string-input-stream ,string
337 `((setf ,index (string-input-stream-current ,var)))))))))
339 (defmacro-mundanely with-output-to-string ((var &optional string)
341 (multiple-value-bind (forms decls) (parse-body forms-decls nil)
343 `(let ((,var (make-fill-pointer-output-stream ,string)))
348 `(let ((,var (make-string-output-stream)))
353 (get-output-stream-string ,var)))))
355 ;;;; miscellaneous macros
357 (defmacro-mundanely nth-value (n form)
359 "Evaluates FORM and returns the Nth value (zero based). This involves no
360 consing when N is a trivial constant integer."
362 (let ((dummy-list nil)
363 (keeper (gensym "KEEPER-")))
364 ;; We build DUMMY-LIST, a list of variables to bind to useless
365 ;; values, then we explicitly IGNORE those bindings and return
366 ;; KEEPER, the only thing we're really interested in right now.
368 (push (gensym "IGNORE-") dummy-list))
369 `(multiple-value-bind (,@dummy-list ,keeper) ,form
370 (declare (ignore ,@dummy-list))
373 `(case (the fixnum ,n)
374 (0 (nth-value 0 ,form))
375 (1 (nth-value 1 ,form))
376 (2 (nth-value 2 ,form))
377 (t (nth (the fixnum ,n) (multiple-value-list ,form)))))))
379 (defmacro-mundanely declaim (&rest specs)
381 "DECLAIM Declaration*
382 Do a declaration or declarations for the global environment."
384 `(eval-when (:compile-toplevel :load-toplevel :execute)
385 ,@(mapcar #'(lambda (x)
386 `(sb!xc:proclaim ',x))
388 ;; KLUDGE: The definition above doesn't work in the cross-compiler,
389 ;; because UNCROSS translates SB!XC:PROCLAIM into CL:PROCLAIM before
390 ;; the form gets executed. Instead, we have to explicitly do the
391 ;; proclamation at macroexpansion time. -- WHN ca. 19990810
393 ;; FIXME: Maybe we don't need this special treatment any more now
394 ;; that we're using DEFMACRO-MUNDANELY instead of DEFMACRO?
396 (mapcar #'sb!xc:proclaim specs)
398 ,@(mapcar #'(lambda (x)
399 `(sb!xc:proclaim ',x))
402 (defmacro-mundanely print-unreadable-object ((object stream
405 `(%print-unreadable-object ,object ,stream ,type ,identity
407 `#'(lambda () ,@body)