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
62 "Signal a restartable error of type TYPE-ERROR if the value of PLACE
63 is not of the specified type. If an error is signalled and the restart
64 is used to return, this can only return if the STORE-VALUE restart is
65 invoked. In that case it will store into PLACE and start over."
66 ;; KLUDGE: We use a simpler form of expansion if PLACE is just a
67 ;; variable to work around Python's blind spot in type derivation.
68 ;; For more complex places getting the type derived should not
69 ;; matter so much anyhow.
70 (let ((expanded (sb!xc:macroexpand place env)))
71 (if (symbolp expanded)
73 ((typep ,place ',type))
74 (setf ,place (check-type-error ',place ,place ',type ,type-string)))
75 (let ((value (gensym)))
76 `(do ((,value ,place ,place))
77 ((typep ,value ',type))
79 (check-type-error ',place ,value ',type ,type-string)))))))
81 ;;;; DEFINE-SYMBOL-MACRO
83 (defmacro-mundanely define-symbol-macro (name expansion)
84 `(eval-when (:compile-toplevel :load-toplevel :execute)
85 (sb!c::%define-symbol-macro ',name ',expansion (sb!c:source-location))))
87 (defun sb!c::%define-symbol-macro (name expansion source-location)
88 (unless (symbolp name)
89 (error 'simple-type-error :datum name :expected-type 'symbol
90 :format-control "Symbol macro name is not a symbol: ~S."
91 :format-arguments (list name)))
92 (with-single-package-locked-error
93 (:symbol name "defining ~A as a symbol-macro"))
94 (sb!c:with-source-location (source-location)
95 (setf (info :source-location :symbol-macro name) source-location))
96 (ecase (info :variable :kind name)
98 (setf (info :variable :kind name) :macro)
99 (setf (info :variable :macro-expansion name) expansion))
101 (error 'simple-program-error
102 :format-control "Symbol macro name already declared special: ~S."
103 :format-arguments (list name)))
105 (error 'simple-program-error
106 :format-control "Symbol macro name already declared constant: ~S."
107 :format-arguments (list name))))
110 ;;;; DEFINE-COMPILER-MACRO
112 (defmacro-mundanely define-compiler-macro (name lambda-list &body body)
114 "Define a compiler-macro for NAME."
115 (legal-fun-name-or-type-error name)
116 (when (and (symbolp name) (special-operator-p name))
117 (error 'simple-program-error
118 :format-control "cannot define a compiler-macro for a special operator: ~S"
119 :format-arguments (list name)))
120 (with-unique-names (whole environment)
121 (multiple-value-bind (body local-decs doc)
122 (parse-defmacro lambda-list whole body name 'define-compiler-macro
123 :environment environment)
124 (let ((def `(lambda (,whole ,environment)
127 (debug-name (sb!c::debug-name 'compiler-macro-function name)))
128 `(eval-when (:compile-toplevel :load-toplevel :execute)
129 (sb!c::%define-compiler-macro ',name
135 ;;; FIXME: This will look remarkably similar to those who have already
136 ;;; seen the code for %DEFMACRO in src/code/defmacro.lisp. Various
137 ;;; bits of logic should be shared (notably arglist setting).
140 `(eval-when (,@times)
141 (defun sb!c::%define-compiler-macro
142 (name definition lambda-list doc debug-name)
144 '((declare (ignore lambda-list debug-name))))
145 ;; FIXME: warn about incompatible lambda list with
146 ;; respect to parent function?
147 (setf (sb!xc:compiler-macro-function name) definition)
148 ;; FIXME: Add support for (SETF FDOCUMENTATION) when
149 ;; object is a list and type is COMPILER-MACRO. (Until
150 ;; then, we have to discard any compiler macro
151 ;; documentation for (SETF FOO).)
153 (setf (fdocumentation name 'compiler-macro) doc))
155 `(case (widetag-of definition)
156 (#.sb!vm:closure-header-widetag
157 (setf (%simple-fun-arglist (%closure-fun definition))
159 (%simple-fun-name (%closure-fun definition))
161 (#.sb!vm:simple-fun-header-widetag
162 (setf (%simple-fun-arglist definition) lambda-list
163 (%simple-fun-name definition) debug-name))))
166 (def (:load-toplevel :execute) #-sb-xc-host t #+sb-xc-host nil)
167 #-sb-xc (def (:compile-toplevel) nil)))
169 ;;;; CASE, TYPECASE, and friends
171 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
173 (define-condition duplicate-case-key-warning (style-warning)
175 :reader case-warning-key)
176 (case-kind :initarg :case-kind
177 :reader case-warning-case-kind)
178 (occurrences :initarg :occurrences
180 :reader duplicate-case-key-warning-occurrences))
182 (lambda (condition stream)
184 "Duplicate key ~S in ~S form, ~
185 occurring in~{~#[~; and~]~{ the ~:R clause:~%~< ~S~:>~}~^,~}."
186 (case-warning-key condition)
187 (case-warning-case-kind condition)
188 (duplicate-case-key-warning-occurrences condition)))))
190 ;;; CASE-BODY returns code for all the standard "case" macros. NAME is
191 ;;; the macro name, and KEYFORM is the thing to case on. MULTI-P
192 ;;; indicates whether a branch may fire off a list of keys; otherwise,
193 ;;; a key that is a list is interpreted in some way as a single key.
194 ;;; When MULTI-P, TEST is applied to the value of KEYFORM and each key
195 ;;; for a given branch; otherwise, TEST is applied to the value of
196 ;;; KEYFORM and the entire first element, instead of each part, of the
197 ;;; case branch. When ERRORP, no OTHERWISE-CLAUSEs are recognized,
198 ;;; and an ERROR form is generated where control falls off the end
199 ;;; of the ordinary clauses. When PROCEEDP, it is an error to
200 ;;; omit ERRORP, and the ERROR form generated is executed within a
201 ;;; RESTART-CASE allowing KEYFORM to be set and retested.
202 (defun case-body (name keyform cases multi-p test errorp proceedp needcasesp)
203 (unless (or cases (not needcasesp))
204 (warn "no clauses in ~S" name))
205 (let ((keyform-value (gensym))
208 (keys-seen (make-hash-table :test #'eql)))
209 (do* ((cases cases (cdr cases))
210 (case (car cases) (car cases))
211 (case-position 1 (1+ case-position)))
213 (flet ((check-clause (case-keys)
214 (loop for k in case-keys
215 for existing = (gethash k keys-seen)
217 (let ((sb!c::*current-path*
218 (when (boundp 'sb!c::*source-paths*)
219 (or (gethash case sb!c::*source-paths*)
220 sb!c::*current-path*))))
221 (warn 'duplicate-case-key-warning
224 :occurrences `(,existing (,case-position (,case)))))))
225 (let ((record (list case-position (list case))))
226 (dolist (k case-keys)
227 (setf (gethash k keys-seen) record)))))
228 (unless (list-of-length-at-least-p case 1)
229 (error "~S -- bad clause in ~S" case name))
230 (destructuring-bind (keyoid &rest forms) case
231 (cond (;; an OTHERWISE-CLAUSE
233 ;; By the way... The old code here tried gave
234 ;; STYLE-WARNINGs for normal-clauses which looked as
235 ;; though they might've been intended to be
236 ;; otherwise-clauses. As Tony Martinez reported on
237 ;; sbcl-devel 2004-11-09 there are sometimes good
238 ;; reasons to write clauses like that; and as I noticed
239 ;; when trying to understand the old code so I could
240 ;; understand his patch, trying to guess which clauses
241 ;; don't have good reasons is fundamentally kind of a
242 ;; mess. SBCL does issue style warnings rather
243 ;; enthusiastically, and I have often justified that by
244 ;; arguing that we're doing that to detect issues which
245 ;; are tedious for programmers to detect for by
246 ;; proofreading (like small typoes in long symbol
247 ;; names, or duplicate function definitions in large
248 ;; files). This doesn't seem to be an issue like that,
249 ;; and I can't think of a comparably good justification
250 ;; for giving STYLE-WARNINGs for legal code here, so
251 ;; now we just hope the programmer knows what he's
252 ;; doing. -- WHN 2004-11-20
253 (and (not errorp) ; possible only in CASE or TYPECASE,
254 ; not in [EC]CASE or [EC]TYPECASE
255 (memq keyoid '(t otherwise))
257 (push `(t nil ,@forms) clauses))
258 ((and multi-p (listp keyoid))
259 (setf keys (append keyoid keys))
260 (check-clause keyoid)
261 (push `((or ,@(mapcar (lambda (key)
262 `(,test ,keyform-value ',key))
269 (check-clause (list keyoid))
270 (push `((,test ,keyform-value ',keyoid)
274 (case-body-aux name keyform keyform-value clauses keys errorp proceedp
275 `(,(if multi-p 'member 'or) ,@keys))))
277 ;;; CASE-BODY-AUX provides the expansion once CASE-BODY has groveled
278 ;;; all the cases. Note: it is not necessary that the resulting code
279 ;;; signal case-failure conditions, but that's what KMP's prototype
280 ;;; code did. We call CASE-BODY-ERROR, because of how closures are
281 ;;; compiled. RESTART-CASE has forms with closures that the compiler
282 ;;; causes to be generated at the top of any function using the case
283 ;;; macros, regardless of whether they are needed.
285 ;;; The CASE-BODY-ERROR function is defined later, when the
286 ;;; RESTART-CASE macro has been defined.
287 (defun case-body-aux (name keyform keyform-value clauses keys
288 errorp proceedp expected-type)
290 (let ((block (gensym))
292 `(let ((,keyform-value ,keyform))
298 (cond ,@(nreverse clauses)
303 ',name ',keyform ,keyform-value
304 ',expected-type ',keys)))
306 `(let ((,keyform-value ,keyform))
307 (declare (ignorable ,keyform-value)) ; e.g. (CASE KEY (T))
311 `((t (error 'case-failure
313 :datum ,keyform-value
314 :expected-type ',expected-type
315 :possibilities ',keys))))))))
318 (defmacro-mundanely case (keyform &body cases)
320 "CASE Keyform {({(Key*) | Key} Form*)}*
321 Evaluates the Forms in the first clause with a Key EQL to the value of
322 Keyform. If a singleton key is T then the clause is a default clause."
323 (case-body 'case keyform cases t 'eql nil nil nil))
325 (defmacro-mundanely ccase (keyform &body cases)
327 "CCASE Keyform {({(Key*) | Key} Form*)}*
328 Evaluates the Forms in the first clause with a Key EQL to the value of
329 Keyform. If none of the keys matches then a correctable error is
331 (case-body 'ccase keyform cases t 'eql t t t))
333 (defmacro-mundanely ecase (keyform &body cases)
335 "ECASE Keyform {({(Key*) | Key} Form*)}*
336 Evaluates the Forms in the first clause with a Key EQL to the value of
337 Keyform. If none of the keys matches then an error is signalled."
338 (case-body 'ecase keyform cases t 'eql t nil t))
340 (defmacro-mundanely typecase (keyform &body cases)
342 "TYPECASE Keyform {(Type Form*)}*
343 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
345 (case-body 'typecase keyform cases nil 'typep nil nil nil))
347 (defmacro-mundanely ctypecase (keyform &body cases)
349 "CTYPECASE Keyform {(Type Form*)}*
350 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
351 is true. If no form is satisfied then a correctable error is signalled."
352 (case-body 'ctypecase keyform cases nil 'typep t t t))
354 (defmacro-mundanely etypecase (keyform &body cases)
356 "ETYPECASE Keyform {(Type Form*)}*
357 Evaluates the Forms in the first clause for which TYPEP of Keyform and Type
358 is true. If no form is satisfied then an error is signalled."
359 (case-body 'etypecase keyform cases nil 'typep t nil t))
361 ;;;; WITH-FOO i/o-related macros
363 (defmacro-mundanely with-open-stream ((var stream) &body forms-decls)
364 (multiple-value-bind (forms decls)
365 (parse-body forms-decls :doc-string-allowed nil)
366 (let ((abortp (gensym)))
367 `(let ((,var ,stream)
371 (multiple-value-prog1
375 (close ,var :abort ,abortp)))))))
377 (defmacro-mundanely with-open-file ((stream filespec &rest options)
379 `(with-open-stream (,stream (open ,filespec ,@options))
382 (defmacro-mundanely with-input-from-string ((var string &key index start end)
384 (multiple-value-bind (forms decls)
385 (parse-body forms-decls :doc-string-allowed nil)
386 ;; The ONCE-ONLY inhibits compiler note for unreachable code when
388 (once-only ((string string))
391 `(make-string-input-stream ,string ,(or start 0)))
394 (make-string-input-stream ,string
397 (make-string-input-stream ,string
400 `(make-string-input-stream ,string
404 (multiple-value-prog1
409 `((setf ,index (string-input-stream-current ,var)))))))))
411 (defmacro-mundanely with-output-to-string
412 ((var &optional string &key (element-type ''character))
414 (multiple-value-bind (forms decls)
415 (parse-body forms-decls :doc-string-allowed nil)
417 (let ((element-type-var (gensym)))
418 `(let ((,var (make-fill-pointer-output-stream ,string))
419 ;; ELEMENT-TYPE isn't currently used for anything
420 ;; (see FILL-POINTER-OUTPUT-STREAM FIXME in stream.lisp),
421 ;; but it still has to be evaluated for side-effects.
422 (,element-type-var ,element-type))
423 (declare (ignore ,element-type-var))
428 `(let ((,var (make-string-output-stream :element-type ,element-type)))
433 (get-output-stream-string ,var)))))
435 ;;;; miscellaneous macros
437 (defmacro-mundanely nth-value (n form)
439 "Evaluate FORM and return the Nth value (zero based). This involves no
440 consing when N is a trivial constant integer."
441 ;; FIXME: The above is true, if slightly misleading. The
442 ;; MULTIPLE-VALUE-BIND idiom [ as opposed to MULTIPLE-VALUE-CALL
443 ;; (LAMBDA (&REST VALUES) (NTH N VALUES)) ] does indeed not cons at
444 ;; runtime. However, for large N (say N = 200), COMPILE on such a
445 ;; form will take longer than can be described as adequate, as the
446 ;; optional dispatch mechanism for the M-V-B gets increasingly
449 (let ((dummy-list nil)
450 (keeper (gensym "KEEPER-")))
451 ;; We build DUMMY-LIST, a list of variables to bind to useless
452 ;; values, then we explicitly IGNORE those bindings and return
453 ;; KEEPER, the only thing we're really interested in right now.
455 (push (gensym "IGNORE-") dummy-list))
456 `(multiple-value-bind (,@dummy-list ,keeper) ,form
457 (declare (ignore ,@dummy-list))
460 `(case (the fixnum ,n)
461 (0 (nth-value 0 ,form))
462 (1 (nth-value 1 ,form))
463 (2 (nth-value 2 ,form))
464 (t (nth (the fixnum ,n) (multiple-value-list ,form)))))))
466 (defmacro-mundanely declaim (&rest specs)
468 "DECLAIM Declaration*
469 Do a declaration or declarations for the global environment."
470 `(eval-when (:compile-toplevel :load-toplevel :execute)
471 ,@(mapcar (lambda (spec) `(sb!xc:proclaim ',spec))
474 (defmacro-mundanely print-unreadable-object ((object stream &key type identity)
476 "Output OBJECT to STREAM with \"#<\" prefix, \">\" suffix, optionally
477 with object-type prefix and object-identity suffix, and executing the
478 code in BODY to provide possible further output."
479 `(%print-unreadable-object ,object ,stream ,type ,identity
484 (defmacro-mundanely ignore-errors (&rest forms)
486 "Execute FORMS handling ERROR conditions, returning the result of the last
487 form, or (VALUES NIL the-ERROR-that-was-caught) if an ERROR was handled."
488 `(handler-case (progn ,@forms)
489 (error (condition) (values nil condition))))