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")
14 ;;;; ASSERT and CHECK-TYPE
16 ;;; ASSERT is written this way, to call ASSERT-ERROR, because of how
17 ;;; closures are compiled. RESTART-CASE has forms with closures that
18 ;;; the compiler causes to be generated at the top of any function
19 ;;; using RESTART-CASE, regardless of whether they are needed. Thus if
20 ;;; we just wrapped a RESTART-CASE around the call to ERROR, we'd have
21 ;;; to do a significant amount of work at runtime allocating and
22 ;;; deallocating the closures regardless of whether they were ever
25 ;;; ASSERT-ERROR isn't defined until a later file because it uses the
26 ;;; macro RESTART-CASE, which isn't defined until a later file.
27 (defmacro-mundanely assert (test-form &optional places datum &rest arguments)
29 "Signals an error if the value of test-form is nil. Continuing from this
30 error using the CONTINUE restart will allow the user to alter the value of
31 some locations known to SETF, starting over with test-form. Returns NIL."
33 (assert-error ',test-form ',places ,datum ,@arguments)
34 ,@(mapcar (lambda (place)
35 `(setf ,place (assert-prompt ',place ,place)))
38 (defun assert-prompt (name value)
39 (cond ((y-or-n-p "The old value of ~S is ~S.~
40 ~%Do you want to supply a new value? "
42 (format *query-io* "~&Type a form to be evaluated:~%")
43 (flet ((read-it () (eval (read *query-io*))))
44 (if (symbolp name) ;help user debug lexical variables
45 (progv (list name) (list value) (read-it))
49 ;;; CHECK-TYPE is written this way, to call CHECK-TYPE-ERROR, because
50 ;;; of how closures are compiled. RESTART-CASE has forms with closures
51 ;;; that the compiler causes to be generated at the top of any
52 ;;; function using RESTART-CASE, regardless of whether they are
53 ;;; needed. Because it would be nice if CHECK-TYPE were cheap to use,
54 ;;; and some things (e.g., READ-CHAR) can't afford this excessive
55 ;;; consing, we bend backwards a little.
57 ;;; CHECK-TYPE-ERROR isn't defined until a later file because it uses
58 ;;; the macro RESTART-CASE, which isn't defined until a later file.
59 (defmacro-mundanely check-type (place type &optional type-string)
61 "Signal a restartable error of type TYPE-ERROR if the value of PLACE is
62 not of the specified type. If an error is signalled and the restart is
63 used to return, this can only return if the STORE-VALUE restart is
64 invoked. In that case it will store into PLACE and start over."
65 (let ((place-value (gensym)))
66 `(do ((,place-value ,place ,place))
67 ((typep ,place-value ',type))
69 (check-type-error ',place ,place-value ',type ,type-string)))))
71 ;;;; DEFINE-SYMBOL-MACRO
73 (defmacro-mundanely define-symbol-macro (name expansion)
74 `(eval-when (:compile-toplevel :load-toplevel :execute)
75 (sb!c::%define-symbol-macro ',name ',expansion)))
77 (defun sb!c::%define-symbol-macro (name expansion)
78 (unless (symbolp name)
79 (error 'simple-type-error :datum name :expected-type 'symbol
80 :format-control "Symbol macro name is not a symbol: ~S."
81 :format-arguments (list name)))
82 (with-single-package-locked-error
83 (:symbol name "defining ~A as a symbol-macro"))
84 (ecase (info :variable :kind name)
86 (setf (info :variable :kind name) :macro)
87 (setf (info :variable :macro-expansion name) expansion))
89 (error 'simple-program-error
90 :format-control "Symbol macro name already declared special: ~S."
91 :format-arguments (list name)))
93 (error 'simple-program-error
94 :format-control "Symbol macro name already declared constant: ~S."
95 :format-arguments (list name))))
98 ;;;; DEFINE-COMPILER-MACRO
100 (defmacro-mundanely define-compiler-macro (name lambda-list &body body)
102 "Define a compiler-macro for NAME."
103 (legal-fun-name-or-type-error name)
105 ;; It's fairly clear that the user intends the compiler macro to
106 ;; expand when he does (SETF (FOO ...) X). And that's even a
107 ;; useful and reasonable thing to want. Unfortunately,
108 ;; (SETF (FOO ...) X) macroexpands into (FUNCALL (SETF FOO) X ...),
109 ;; and it's not at all clear that it's valid to expand a FUNCALL form,
110 ;; and the ANSI standard doesn't seem to say anything else which
111 ;; would justify us expanding the compiler macro the way the user
112 ;; wants. So instead we rely on 3.2.2.1.3 "When Compiler Macros Are
113 ;; Used" which says they never have to be used, so by ignoring such
114 ;; macros we're erring on the safe side. But any user who does
115 ;; (DEFINE-COMPILER-MACRO (SETF FOO) ...) could easily be surprised
116 ;; by this way of complying with a rather screwy aspect of the ANSI
117 ;; spec, so at least we can warn him...
118 (sb!c::compiler-style-warn
119 "defining compiler macro of (SETF ...), which will not be expanded"))
120 (when (and (symbolp name) (special-operator-p name))
121 (error 'simple-program-error
122 :format-control "cannot define a compiler-macro for a special operator: ~S"
123 :format-arguments (list name)))
124 (with-unique-names (whole environment)
125 (multiple-value-bind (body local-decs doc)
126 (parse-defmacro lambda-list whole body name 'define-compiler-macro
127 :environment environment)
128 (let ((def `(lambda (,whole ,environment)
131 (debug-name (sb!c::debug-name 'compiler-macro-function name)))
132 `(eval-when (:compile-toplevel :load-toplevel :execute)
133 (sb!c::%define-compiler-macro ',name
139 ;;; FIXME: This will look remarkably similar to those who have already
140 ;;; seen the code for %DEFMACRO in src/code/defmacro.lisp. Various
141 ;;; bits of logic should be shared (notably arglist setting).
144 `(eval-when (,@times)
145 (defun sb!c::%define-compiler-macro
146 (name definition lambda-list doc debug-name)
148 '((declare (ignore lambda-list debug-name))))
149 ;; FIXME: warn about incompatible lambda list with
150 ;; respect to parent function?
151 (setf (sb!xc:compiler-macro-function name) definition)
152 ;; FIXME: Add support for (SETF FDOCUMENTATION) when
153 ;; object is a list and type is COMPILER-MACRO. (Until
154 ;; then, we have to discard any compiler macro
155 ;; documentation for (SETF FOO).)
157 (setf (fdocumentation name 'compiler-macro) doc))
159 `(case (widetag-of definition)
160 (#.sb!vm:closure-header-widetag
161 (setf (%simple-fun-arglist (%closure-fun definition))
163 (%simple-fun-name (%closure-fun definition))
165 (#.sb!vm:simple-fun-header-widetag
166 (setf (%simple-fun-arglist definition) lambda-list
167 (%simple-fun-name definition) debug-name))))
170 (def (:load-toplevel :execute) #-sb-xc-host t #+sb-xc-host nil)
171 #-sb-xc (def (:compile-toplevel) nil)))
173 ;;;; CASE, TYPECASE, and friends
175 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
177 ;;; CASE-BODY returns code for all the standard "case" macros. NAME is
178 ;;; the macro name, and KEYFORM is the thing to case on. MULTI-P
179 ;;; indicates whether a branch may fire off a list of keys; otherwise,
180 ;;; a key that is a list is interpreted in some way as a single key.
181 ;;; When MULTI-P, TEST is applied to the value of KEYFORM and each key
182 ;;; for a given branch; otherwise, TEST is applied to the value of
183 ;;; KEYFORM and the entire first element, instead of each part, of the
184 ;;; case branch. When ERRORP, no OTHERWISE-CLAUSEs are recognized,
185 ;;; and an ERROR form is generated where control falls off the end
186 ;;; of the ordinary clauses. When PROCEEDP, it is an error to
187 ;;; omit ERRORP, and the ERROR form generated is executed within a
188 ;;; RESTART-CASE allowing KEYFORM to be set and retested.
189 (defun case-body (name keyform cases multi-p test errorp proceedp needcasesp)
190 (unless (or cases (not needcasesp))
191 (warn "no clauses in ~S" name))
192 (let ((keyform-value (gensym))
195 (do* ((cases cases (cdr cases))
196 (case (car cases) (car cases)))
198 (unless (list-of-length-at-least-p case 1)
199 (error "~S -- bad clause in ~S" case name))
200 (destructuring-bind (keyoid &rest forms) case
201 (cond (;; an OTHERWISE-CLAUSE
203 ;; By the way... The old code here tried gave
204 ;; STYLE-WARNINGs for normal-clauses which looked as
205 ;; though they might've been intended to be
206 ;; otherwise-clauses. As Tony Martinez reported on
207 ;; sbcl-devel 2004-11-09 there are sometimes good
208 ;; reasons to write clauses like that; and as I noticed
209 ;; when trying to understand the old code so I could
210 ;; understand his patch, trying to guess which clauses
211 ;; don't have good reasons is fundamentally kind of a
212 ;; mess. SBCL does issue style warnings rather
213 ;; enthusiastically, and I have often justified that by
214 ;; arguing that we're doing that to detect issues which
215 ;; are tedious for programmers to detect for by
216 ;; proofreading (like small typoes in long symbol
217 ;; names, or duplicate function definitions in large
218 ;; files). This doesn't seem to be an issue like that,
219 ;; and I can't think of a comparably good justification
220 ;; for giving STYLE-WARNINGs for legal code here, so
221 ;; now we just hope the programmer knows what he's
222 ;; doing. -- WHN 2004-11-20
223 (and (not errorp) ; possible only in CASE or TYPECASE,
224 ; not in [EC]CASE or [EC]TYPECASE
225 (memq keyoid '(t otherwise))
227 (push `(t nil ,@forms) clauses))
228 ((and multi-p (listp keyoid))
229 (setf keys (append keyoid keys))
230 (push `((or ,@(mapcar (lambda (key)
231 `(,test ,keyform-value ',key))
238 (push `((,test ,keyform-value ',keyoid)
242 (case-body-aux name keyform keyform-value clauses keys errorp proceedp
243 `(,(if multi-p 'member 'or) ,@keys))))
245 ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled
246 ;;; all the cases. Note: it is not necessary that the resulting code
247 ;;; signal case-failure conditions, but that's what KMP's prototype
248 ;;; code did. We call CASE-BODY-ERROR, because of how closures are
249 ;;; compiled. RESTART-CASE has forms with closures that the compiler
250 ;;; causes to be generated at the top of any function using the case
251 ;;; macros, regardless of whether they are needed.
253 ;;; The CASE-BODY-ERROR function is defined later, when the
254 ;;; RESTART-CASE macro has been defined.
255 (defun case-body-aux (name keyform keyform-value clauses keys
256 errorp proceedp expected-type)
258 (let ((block (gensym))
260 `(let ((,keyform-value ,keyform))
266 (cond ,@(nreverse clauses)
271 ',name ',keyform ,keyform-value
272 ',expected-type ',keys)))
274 `(let ((,keyform-value ,keyform))
275 (declare (ignorable ,keyform-value)) ; e.g. (CASE KEY (T))
279 `((t (error 'case-failure
281 :datum ,keyform-value
282 :expected-type ',expected-type
283 :possibilities ',keys))))))))
286 (defmacro-mundanely case (keyform &body cases)
288 "CASE Keyform {({(Key*) | Key} Form*)}*
289 Evaluates the Forms in the first clause with a Key EQL to the value of
290 Keyform. If a singleton key is T then the clause is a default clause."
291 (case-body 'case keyform cases t 'eql nil nil nil))
293 (defmacro-mundanely ccase (keyform &body cases)
295 "CCASE Keyform {({(Key*) | Key} Form*)}*
296 Evaluates the Forms in the first clause with a Key EQL to the value of
297 Keyform. If none of the keys matches then a correctable error is
299 (case-body 'ccase keyform cases t 'eql t t t))
301 (defmacro-mundanely ecase (keyform &body cases)
303 "ECASE Keyform {({(Key*) | Key} Form*)}*
304 Evaluates the Forms in the first clause with a Key EQL to the value of
305 Keyform. If none of the keys matches then an error is signalled."
306 (case-body 'ecase keyform cases t 'eql t nil t))
308 (defmacro-mundanely typecase (keyform &body cases)
310 "TYPECASE Keyform {(Type Form*)}*
311 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
313 (case-body 'typecase keyform cases nil 'typep nil nil nil))
315 (defmacro-mundanely ctypecase (keyform &body cases)
317 "CTYPECASE Keyform {(Type Form*)}*
318 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
319 is true. If no form is satisfied then a correctable error is signalled."
320 (case-body 'ctypecase keyform cases nil 'typep t t t))
322 (defmacro-mundanely etypecase (keyform &body cases)
324 "ETYPECASE Keyform {(Type Form*)}*
325 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
326 is true. If no form is satisfied then an error is signalled."
327 (case-body 'etypecase keyform cases nil 'typep t nil t))
329 ;;;; WITH-FOO i/o-related macros
331 (defmacro-mundanely with-open-stream ((var stream) &body forms-decls)
332 (multiple-value-bind (forms decls)
333 (parse-body forms-decls :doc-string-allowed nil)
334 (let ((abortp (gensym)))
335 `(let ((,var ,stream)
339 (multiple-value-prog1
343 (close ,var :abort ,abortp)))))))
345 (defmacro-mundanely with-open-file ((stream filespec &rest options)
347 `(with-open-stream (,stream (open ,filespec ,@options))
350 (defmacro-mundanely with-input-from-string ((var string &key index start end)
352 (multiple-value-bind (forms decls)
353 (parse-body forms-decls :doc-string-allowed nil)
354 ;; The ONCE-ONLY inhibits compiler note for unreachable code when
356 (once-only ((string string))
359 `(make-string-input-stream ,string ,(or start 0)))
362 (make-string-input-stream ,string
365 (make-string-input-stream ,string
368 `(make-string-input-stream ,string
372 (multiple-value-prog1
377 `((setf ,index (string-input-stream-current ,var)))))))))
379 (defmacro-mundanely with-output-to-string
380 ((var &optional string &key (element-type ''character))
382 (multiple-value-bind (forms decls)
383 (parse-body forms-decls :doc-string-allowed nil)
385 (let ((element-type-var (gensym)))
386 `(let ((,var (make-fill-pointer-output-stream ,string))
387 ;; ELEMENT-TYPE isn't currently used for anything
388 ;; (see FILL-POINTER-OUTPUT-STREAM FIXME in stream.lisp),
389 ;; but it still has to be evaluated for side-effects.
390 (,element-type-var ,element-type))
391 (declare (ignore ,element-type-var))
396 `(let ((,var (make-string-output-stream :element-type ,element-type)))
401 (get-output-stream-string ,var)))))
403 ;;;; miscellaneous macros
405 (defmacro-mundanely nth-value (n form)
407 "Evaluate FORM and return the Nth value (zero based). This involves no
408 consing when N is a trivial constant integer."
409 ;; FIXME: The above is true, if slightly misleading. The
410 ;; MULTIPLE-VALUE-BIND idiom [ as opposed to MULTIPLE-VALUE-CALL
411 ;; (LAMBDA (&REST VALUES) (NTH N VALUES)) ] does indeed not cons at
412 ;; runtime. However, for large N (say N = 200), COMPILE on such a
413 ;; form will take longer than can be described as adequate, as the
414 ;; optional dispatch mechanism for the M-V-B gets increasingly
417 (let ((dummy-list nil)
418 (keeper (gensym "KEEPER-")))
419 ;; We build DUMMY-LIST, a list of variables to bind to useless
420 ;; values, then we explicitly IGNORE those bindings and return
421 ;; KEEPER, the only thing we're really interested in right now.
423 (push (gensym "IGNORE-") dummy-list))
424 `(multiple-value-bind (,@dummy-list ,keeper) ,form
425 (declare (ignore ,@dummy-list))
428 `(case (the fixnum ,n)
429 (0 (nth-value 0 ,form))
430 (1 (nth-value 1 ,form))
431 (2 (nth-value 2 ,form))
432 (t (nth (the fixnum ,n) (multiple-value-list ,form)))))))
434 (defmacro-mundanely declaim (&rest specs)
436 "DECLAIM Declaration*
437 Do a declaration or declarations for the global environment."
438 `(eval-when (:compile-toplevel :load-toplevel :execute)
439 ,@(mapcar (lambda (spec) `(sb!xc:proclaim ',spec))
442 (defmacro-mundanely print-unreadable-object ((object stream &key type identity)
444 "Output OBJECT to STREAM with \"#<\" prefix, \">\" suffix, optionally
445 with object-type prefix and object-identity suffix, and executing the
446 code in BODY to provide possible further output."
447 `(%print-unreadable-object ,object ,stream ,type ,identity
452 (defmacro-mundanely ignore-errors (&rest forms)
454 "Execute FORMS handling ERROR conditions, returning the result of the last
455 form, or (VALUES NIL the-ERROR-that-was-caught) if an ERROR was handled."
456 `(handler-case (progn ,@forms)
457 (error (condition) (values nil condition))))