Make INFO's compiler-macro more forgiving.
[sbcl.git] / src / code / defboot.lisp
blob52e5980adc50108ad11dd30285f0f34c4edcf214
1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
3 ;;;;
4 ;;;; KLUDGE: The bootstrapping aspect of this is now obsolete. It was
5 ;;;; originally intended that this file file would be loaded into a
6 ;;;; Lisp image which had Common Lisp primitives defined, and DEFMACRO
7 ;;;; defined, and little else. Since then that approach has been
8 ;;;; dropped and this file has been modified somewhat to make it work
9 ;;;; more cleanly when used to predefine macros at
10 ;;;; build-the-cross-compiler time.
12 ;;;; This software is part of the SBCL system. See the README file for
13 ;;;; more information.
14 ;;;;
15 ;;;; This software is derived from the CMU CL system, which was
16 ;;;; written at Carnegie Mellon University and released into the
17 ;;;; public domain. The software is in the public domain and is
18 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
19 ;;;; files for more information.
21 (in-package "SB!IMPL")
24 ;;;; IN-PACKAGE
26 (defmacro-mundanely in-package (string-designator)
27 (let ((string (string string-designator)))
28 `(eval-when (:compile-toplevel :load-toplevel :execute)
29 (setq *package* (find-undeleted-package-or-lose ,string)))))
31 ;;;; MULTIPLE-VALUE-FOO
33 (defun list-of-symbols-p (x)
34 (and (listp x)
35 (every #'symbolp x)))
37 (defmacro-mundanely multiple-value-bind (vars value-form &body body)
38 (if (list-of-symbols-p vars)
39 ;; It's unclear why it would be important to special-case the LENGTH=1 case
40 ;; at this level, but the CMU CL code did it, so.. -- WHN 19990411
41 (if (= (length vars) 1)
42 `(let ((,(car vars) ,value-form))
43 ,@body)
44 (let ((ignore (sb!xc:gensym)))
45 `(multiple-value-call #'(lambda (&optional ,@(mapcar #'list vars)
46 &rest ,ignore)
47 (declare (ignore ,ignore))
48 ,@body)
49 ,value-form)))
50 (error "Vars is not a list of symbols: ~S" vars)))
52 (defmacro-mundanely multiple-value-setq (vars value-form)
53 (unless (list-of-symbols-p vars)
54 (error "Vars is not a list of symbols: ~S" vars))
55 ;; MULTIPLE-VALUE-SETQ is required to always return just the primary
56 ;; value of the value-from, even if there are no vars. (SETF VALUES)
57 ;; in turn is required to return as many values as there are
58 ;; value-places, hence this:
59 (if vars
60 `(values (setf (values ,@vars) ,value-form))
61 `(values ,value-form)))
63 (defmacro-mundanely multiple-value-list (value-form)
64 `(multiple-value-call #'list ,value-form))
66 ;;;; various conditional constructs
68 ;;; COND defined in terms of IF
69 (defmacro-mundanely cond (&rest clauses)
70 (if (endp clauses)
71 nil
72 (let ((clause (first clauses))
73 (more (rest clauses)))
74 (if (atom clause)
75 (error 'simple-type-error
76 :format-control "COND clause is not a ~S: ~S"
77 :format-arguments (list 'cons clause)
78 :expected-type 'cons
79 :datum clause)
80 (let ((test (first clause))
81 (forms (rest clause)))
82 (if (endp forms)
83 (let ((n-result (gensym)))
84 `(let ((,n-result ,test))
85 (if ,n-result
86 ,n-result
87 (cond ,@more))))
88 (if (eq t test)
89 ;; THE to preserve non-toplevelness for FOO in
90 ;; (COND (T (FOO)))
91 ;; FIXME: this hides all other possible stylistic issues,
92 ;; not the least of which is a code deletion note,
93 ;; if there are forms following the one whose head is T.
94 ;; This is not usually the SBCL preferred way.
95 `(the t (progn ,@forms))
96 `(if ,test
97 (progn ,@forms)
98 ,(when more `(cond ,@more))))))))))
100 (defmacro-mundanely when (test &body forms)
101 #!+sb-doc
102 "If the first argument is true, the rest of the forms are
103 evaluated as a PROGN."
104 `(if ,test (progn ,@forms) nil))
106 (defmacro-mundanely unless (test &body forms)
107 #!+sb-doc
108 "If the first argument is not true, the rest of the forms are
109 evaluated as a PROGN."
110 `(if ,test nil (progn ,@forms)))
112 (defmacro-mundanely and (&rest forms)
113 (cond ((endp forms) t)
114 ((endp (rest forms))
115 ;; Preserve non-toplevelness of the form!
116 `(the t ,(first forms)))
118 `(if ,(first forms)
119 (and ,@(rest forms))
120 nil))))
122 (defmacro-mundanely or (&rest forms)
123 (cond ((endp forms) nil)
124 ((endp (rest forms))
125 ;; Preserve non-toplevelness of the form!
126 `(the t ,(first forms)))
128 (let ((n-result (gensym)))
129 `(let ((,n-result ,(first forms)))
130 (if ,n-result
131 ,n-result
132 (or ,@(rest forms))))))))
134 ;;;; various sequencing constructs
136 (flet ((prog-expansion-from-let (varlist body-decls let)
137 (multiple-value-bind (body decls)
138 (parse-body body-decls :doc-string-allowed nil)
139 `(block nil
140 (,let ,varlist
141 ,@decls
142 (tagbody ,@body))))))
143 (defmacro-mundanely prog (varlist &body body-decls)
144 (prog-expansion-from-let varlist body-decls 'let))
145 (defmacro-mundanely prog* (varlist &body body-decls)
146 (prog-expansion-from-let varlist body-decls 'let*)))
148 (defmacro-mundanely prog1 (result &body body)
149 (let ((n-result (gensym)))
150 `(let ((,n-result ,result))
151 ,@body
152 ,n-result)))
154 (defmacro-mundanely prog2 (form1 result &body body)
155 `(prog1 (progn ,form1 ,result) ,@body))
157 ;;;; DEFUN
159 ;;; Should we save the inline expansion of the function named NAME?
160 (defun inline-fun-name-p (name)
162 ;; the normal reason for saving the inline expansion
163 (let ((inlinep (info :function :inlinep name)))
164 (member inlinep '(:inline :maybe-inline)))
165 ;; another reason for saving the inline expansion: If the
166 ;; ANSI-recommended idiom
167 ;; (DECLAIM (INLINE FOO))
168 ;; (DEFUN FOO ..)
169 ;; (DECLAIM (NOTINLINE FOO))
170 ;; has been used, and then we later do another
171 ;; (DEFUN FOO ..)
172 ;; without a preceding
173 ;; (DECLAIM (INLINE FOO))
174 ;; what should we do with the old inline expansion when we see the
175 ;; new DEFUN? Overwriting it with the new definition seems like
176 ;; the only unsurprising choice.
177 (info :function :inline-expansion-designator name)))
179 (defmacro-mundanely defun (&environment env name args &body body)
180 #!+sb-doc
181 "Define a function at top level."
182 #+sb-xc-host
183 (unless (symbol-package (fun-name-block-name name))
184 (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
185 (multiple-value-bind (forms decls doc) (parse-body body)
186 (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
187 (lambda-guts `(,args
188 ,@decls
189 (block ,(fun-name-block-name name)
190 ,@forms)))
191 (lambda `(lambda ,@lambda-guts))
192 (named-lambda `(named-lambda ,name ,@lambda-guts))
193 (inline-lambda
194 (when (inline-fun-name-p name)
195 ;; we want to attempt to inline, so complain if we can't
196 (or (sb!c:maybe-inline-syntactic-closure lambda env)
197 (progn
198 (#+sb-xc-host warn
199 #-sb-xc-host sb!c:maybe-compiler-notify
200 "lexical environment too hairy, can't inline DEFUN ~S"
201 name)
202 nil)))))
203 `(progn
204 (eval-when (:compile-toplevel)
205 (sb!c:%compiler-defun ',name ',inline-lambda t))
206 ,@(when (typep name '(cons (eql setf)))
207 `((eval-when (:compile-toplevel :execute)
208 (sb!c::warn-if-setf-macro ',name))))
209 (%defun ',name ,named-lambda ,doc ',inline-lambda
210 (sb!c:source-location))))))
212 #-sb-xc-host
213 (progn (defun %defun (name def doc inline-lambda source-location)
214 (declare (type function def))
215 (declare (type (or null simple-string) doc))
216 ;; should've been checked by DEFMACRO DEFUN
217 (aver (legal-fun-name-p name))
218 (sb!c:%compiler-defun name inline-lambda nil)
219 (when (fboundp name)
220 (warn 'redefinition-with-defun
221 :name name :new-function def :new-location source-location))
222 (setf (sb!xc:fdefinition name) def)
223 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
224 ;; also checks package locks. By doing this here we let (SETF
225 ;; FDEFINITION) do the load-time package lock checking before
226 ;; we frob any existing inline expansions.
227 (sb!c::%set-inline-expansion name nil inline-lambda)
228 (sb!c::note-name-defined name :function)
229 (when doc
230 (setf (%fun-doc def) doc))
231 name)
232 ;; During cold-init we don't touch the fdefinition.
233 (defun !%quietly-defun (name doc inline-lambda)
234 (sb!c:%compiler-defun name nil nil) ; makes :WHERE-FROM = :DEFINED
235 (sb!c::%set-inline-expansion name nil inline-lambda)
236 ;; and no need to call NOTE-NAME-DEFINED. It would do nothing.
237 (when doc
238 (setf (%fun-doc (fdefinition name)) doc))))
240 ;;;; DEFVAR and DEFPARAMETER
242 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
243 #!+sb-doc
244 "Define a special variable at top level. Declare the variable
245 SPECIAL and, optionally, initialize it. If the variable already has a
246 value, the old value is not clobbered. The third argument is an optional
247 documentation string for the variable."
248 `(progn
249 (eval-when (:compile-toplevel)
250 (%compiler-defvar ',var))
251 (%defvar ',var (unless (boundp ',var) ,val)
252 ',valp ,doc ',docp
253 (sb!c:source-location))))
255 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
256 #!+sb-doc
257 "Define a parameter that is not normally changed by the program,
258 but that may be changed without causing an error. Declare the
259 variable special and sets its value to VAL, overwriting any
260 previous value. The third argument is an optional documentation
261 string for the parameter."
262 `(progn
263 (eval-when (:compile-toplevel)
264 (%compiler-defvar ',var))
265 (%defparameter ',var ,val ,doc ',docp (sb!c:source-location))))
267 (defun %compiler-defvar (var)
268 (sb!xc:proclaim `(special ,var)))
270 #-sb-xc-host
271 (defun %defvar (var val valp doc docp source-location)
272 (%compiler-defvar var)
273 (when valp
274 (unless (boundp var)
275 (set var val)))
276 (when docp
277 (setf (fdocumentation var 'variable) doc))
278 (sb!c:with-source-location (source-location)
279 (setf (info :source-location :variable var) source-location))
280 var)
282 #-sb-xc-host
283 (defun %defparameter (var val doc docp source-location)
284 (%compiler-defvar var)
285 (set var val)
286 (when docp
287 (setf (fdocumentation var 'variable) doc))
288 (sb!c:with-source-location (source-location)
289 (setf (info :source-location :variable var) source-location))
290 var)
292 ;;;; iteration constructs
294 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
295 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
296 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
297 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
298 ;;; the same file as FROB-DO-BODY.)
299 (defmacro-mundanely do (varlist endlist &body body)
300 #!+sb-doc
301 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
302 Iteration construct. Each Var is initialized in parallel to the value of the
303 specified Init form. On subsequent iterations, the Vars are assigned the
304 value of the Step form (if any) in parallel. The Test is evaluated before
305 each evaluation of the body Forms. When the Test is true, the Exit-Forms
306 are evaluated as a PROGN, with the result being the value of the DO. A block
307 named NIL is established around the entire expansion, allowing RETURN to be
308 used as an alternate exit mechanism."
309 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
310 (defmacro-mundanely do* (varlist endlist &body body)
311 #!+sb-doc
312 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
313 Iteration construct. Each Var is initialized sequentially (like LET*) to the
314 value of the specified Init form. On subsequent iterations, the Vars are
315 sequentially assigned the value of the Step form (if any). The Test is
316 evaluated before each evaluation of the body Forms. When the Test is true,
317 the Exit-Forms are evaluated as a PROGN, with the result being the value
318 of the DO. A block named NIL is established around the entire expansion,
319 allowing RETURN to be used as an alternate exit mechanism."
320 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
322 ;;; DOTIMES and DOLIST could be defined more concisely using
323 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
324 ;;; it'd be tricky to use them before those things were defined.
325 ;;; They're used enough times before destructuring mechanisms are
326 ;;; defined that it looks as though it's worth just implementing them
327 ;;; ASAP, at the cost of being unable to use the standard
328 ;;; destructuring mechanisms.
329 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
330 (cond ((integerp count)
331 `(do ((,var 0 (1+ ,var)))
332 ((>= ,var ,count) ,result)
333 (declare (type unsigned-byte ,var))
334 ,@body))
336 (let ((c (gensym "COUNT")))
337 `(do ((,var 0 (1+ ,var))
338 (,c ,count))
339 ((>= ,var ,c) ,result)
340 (declare (type unsigned-byte ,var)
341 (type integer ,c))
342 ,@body)))))
344 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body &environment env)
345 ;; We repeatedly bind the var instead of setting it so that we never
346 ;; have to give the var an arbitrary value such as NIL (which might
347 ;; conflict with a declaration). If there is a result form, we
348 ;; introduce a gratuitous binding of the variable to NIL without the
349 ;; declarations, then evaluate the result form in that
350 ;; environment. We spuriously reference the gratuitous variable,
351 ;; since we don't want to use IGNORABLE on what might be a special
352 ;; var.
353 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
354 (let* ((n-list (gensym "N-LIST"))
355 (start (gensym "START")))
356 (multiple-value-bind (clist members clist-ok)
357 (cond ((sb!xc:constantp list env)
358 (let ((value (constant-form-value list env)))
359 (multiple-value-bind (all dot) (list-members value :max-length 20)
360 (when (eql dot t)
361 ;; Full warning is too much: the user may terminate the loop
362 ;; early enough. Contents are still right, though.
363 (style-warn "Dotted list ~S in DOLIST." value))
364 (if (eql dot :maybe)
365 (values value nil nil)
366 (values value all t)))))
367 ((and (consp list) (eq 'list (car list))
368 (every (lambda (arg) (sb!xc:constantp arg env)) (cdr list)))
369 (let ((values (mapcar (lambda (arg) (constant-form-value arg env)) (cdr list))))
370 (values values values t)))
372 (values nil nil nil)))
373 `(block nil
374 (let ((,n-list ,(if clist-ok (list 'quote clist) list)))
375 (tagbody
376 ,start
377 (unless (endp ,n-list)
378 (let ((,var ,(if clist-ok
379 `(truly-the (member ,@members) (car ,n-list))
380 `(car ,n-list))))
381 ,@decls
382 (setq ,n-list (cdr ,n-list))
383 (tagbody ,@forms))
384 (go ,start))))
385 ,(if result
386 `(let ((,var nil))
387 ;; Filter out TYPE declarations (VAR gets bound to NIL,
388 ;; and might have a conflicting type declaration) and
389 ;; IGNORE (VAR might be ignored in the loop body, but
390 ;; it's used in the result form).
391 ,@(filter-dolist-declarations decls)
392 ,var
393 ,result)
394 nil))))))
396 ;;;; conditions, handlers, restarts
398 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
399 ;;; macros in the compiler before the DEFVARs are compiled.
401 ;;; For an explanation of these data structures, see DEFVARs in
402 ;;; target-error.lisp.
403 (sb!xc:proclaim '(special *handler-clusters* *restart-clusters*))
405 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
406 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
407 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
408 ;;; is special and so it rightfully complains about a SETQ of the variable.
409 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
410 ;;; whether it's currently bound.
411 ;;; But the DEFVARs are in target-error. So it's one hack or another.
412 (setf (info :variable :always-bound '*handler-clusters*)
413 #+sb-xc :always-bound #-sb-xc :eventually)
414 (setf (info :variable :always-bound '*restart-clusters*)
415 #+sb-xc :always-bound #-sb-xc :eventually)
417 (defmacro-mundanely with-condition-restarts
418 (condition-form restarts-form &body body)
419 #!+sb-doc
420 "Evaluates the BODY in a dynamic environment where the restarts in the list
421 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
422 This allows FIND-RESTART, etc., to recognize restarts that are not related
423 to the error currently being debugged. See also RESTART-CASE."
424 (once-only ((restarts restarts-form))
425 (with-unique-names (restart)
426 ;; FIXME: check the need for interrupt-safety.
427 `(unwind-protect
428 (progn
429 (dolist (,restart ,restarts)
430 (push ,condition-form
431 (restart-associated-conditions ,restart)))
432 ,@body)
433 (dolist (,restart ,restarts)
434 (pop (restart-associated-conditions ,restart)))))))
436 (defmacro-mundanely restart-bind (bindings &body forms)
437 #!+sb-doc
438 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
439 Executes forms in a dynamic context where the given bindings are in
440 effect. Users probably want to use RESTART-CASE. A case-name of NIL
441 indicates an anonymous restart. When bindings contain the same
442 restart name, FIND-RESTART will find the first such binding."
443 (flet ((parse-binding (binding)
444 (unless (>= (length binding) 2)
445 (error "ill-formed restart binding: ~S" binding))
446 (destructuring-bind (name function
447 &key interactive-function
448 test-function
449 report-function)
450 binding
451 (unless (or name report-function)
452 (warn "Unnamed restart does not have a report function: ~
453 ~S" binding))
454 `(make-restart ',name ,function
455 ,@(and (or report-function
456 interactive-function
457 test-function)
458 `(,report-function))
459 ,@(and (or interactive-function
460 test-function)
461 `(,interactive-function))
462 ,@(and test-function
463 `(,test-function))))))
464 `(let ((*restart-clusters*
465 (cons (list ,@(mapcar #'parse-binding bindings))
466 *restart-clusters*)))
467 (declare (truly-dynamic-extent *restart-clusters*))
468 ,@forms)))
470 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
471 ;;; appropriate. Gross, but it's what the book seems to say...
472 (defun munge-restart-case-expression (expression env)
473 (let ((exp (%macroexpand expression env)))
474 (if (consp exp)
475 (let* ((name (car exp))
476 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
477 (if (member name '(signal error cerror warn))
478 (once-only ((n-cond `(coerce-to-condition
479 ,(first args)
480 (list ,@(rest args))
481 ',(case name
482 (warn 'simple-warning)
483 (signal 'simple-condition)
484 (t 'simple-error))
485 ',name)))
486 `(with-condition-restarts
487 ,n-cond
488 (car *restart-clusters*)
489 ,(if (eq name 'cerror)
490 `(cerror ,(second exp) ,n-cond)
491 `(,name ,n-cond))))
492 expression))
493 expression)))
495 (defmacro-mundanely restart-case (expression &body clauses &environment env)
496 #!+sb-doc
497 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
498 The form is evaluated in a dynamic context where the clauses have
499 special meanings as points to which control may be transferred (see
500 INVOKE-RESTART). When clauses contain the same case-name,
501 FIND-RESTART will find the first such clause. If form is a call to
502 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
503 signalled condition will be associated with the new restarts."
504 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
505 ;; parse all clauses into lists of the form
507 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
509 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
510 ;; bindings. These lists are then passed to
511 ;; * MAKE-BINDING which generates bindings for the respective NAME
512 ;; for HANDLER-BIND
513 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
514 ;; the respective BODY.
515 (let ((block-tag (sb!xc:gensym "BLOCK"))
516 (temp-var (gensym)))
517 (labels ((parse-keywords-and-body (keywords-and-body)
518 (do ((form keywords-and-body (cddr form))
519 (result '())) (nil)
520 (destructuring-bind (&optional key (arg nil argp) &rest rest)
521 form
522 (declare (ignore rest))
523 (setq result
524 (append
525 (cond
526 ((and (eq key :report) argp)
527 (list :report-function
528 (if (stringp arg)
529 `#'(lambda (stream)
530 (write-string ,arg stream))
531 `#',arg)))
532 ((and (eq key :interactive) argp)
533 (list :interactive-function `#',arg))
534 ((and (eq key :test) argp)
535 (list :test-function `#',arg))
537 (return (values result form))))
538 result)))))
539 (parse-clause (clause)
540 (unless (and (listp clause) (>= (length clause) 2)
541 (listp (second clause)))
542 (error "ill-formed ~S clause, no lambda-list:~% ~S"
543 'restart-case clause))
544 (destructuring-bind (name lambda-list &body body) clause
545 (multiple-value-bind (keywords body)
546 (parse-keywords-and-body body)
547 (list name (sb!xc:gensym "TAG") keywords lambda-list body))))
548 (make-binding (clause-data)
549 (destructuring-bind (name tag keywords lambda-list body) clause-data
550 (declare (ignore body))
551 `(,name
552 (lambda ,(cond ((null lambda-list)
554 ((and (null (cdr lambda-list))
555 (not (member (car lambda-list)
556 '(&optional &key &aux))))
557 '(temp))
559 '(&rest temp)))
560 ,@(when lambda-list `((setq ,temp-var temp)))
561 (locally (declare (optimize (safety 0)))
562 (go ,tag)))
563 ,@keywords)))
564 (make-apply-and-return (clause-data)
565 (destructuring-bind (name tag keywords lambda-list body) clause-data
566 (declare (ignore name keywords))
567 `(,tag (return-from ,block-tag
568 ,(cond ((null lambda-list)
569 `(progn ,@body))
570 ((and (null (cdr lambda-list))
571 (not (member (car lambda-list)
572 '(&optional &key &aux))))
573 `(funcall (lambda ,lambda-list ,@body) ,temp-var))
575 `(apply (lambda ,lambda-list ,@body) ,temp-var))))))))
576 (let ((clauses-data (mapcar #'parse-clause clauses)))
577 `(block ,block-tag
578 (let ((,temp-var nil))
579 (declare (ignorable ,temp-var))
580 (tagbody
581 (restart-bind
582 ,(mapcar #'make-binding clauses-data)
583 (return-from ,block-tag
584 ,(munge-restart-case-expression expression env)))
585 ,@(mapcan #'make-apply-and-return clauses-data))))))))
587 (defmacro-mundanely with-simple-restart ((restart-name format-string
588 &rest format-arguments)
589 &body forms)
590 #!+sb-doc
591 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
592 body)
593 If restart-name is not invoked, then all values returned by forms are
594 returned. If control is transferred to this restart, it immediately
595 returns the values NIL and T."
596 (let ((stream (sb!xc:gensym "STREAM")))
597 `(restart-case
598 ;; If there's just one body form, then don't use PROGN. This allows
599 ;; RESTART-CASE to "see" calls to ERROR, etc.
600 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
601 (,restart-name ()
602 :report (lambda (,stream)
603 (declare (type stream ,stream))
604 (format ,stream ,format-string ,@format-arguments))
605 (values nil t)))))
607 (defmacro-mundanely %handler-bind (bindings form)
608 ;; As an optimization, this looks at the handler parts of BINDINGS
609 ;; and turns handlers of the forms (lambda ...) and (function
610 ;; (lambda ...)) into local, dynamic-extent functions.
612 ;; Type specifiers in BINDINGS are translated into local,
613 ;; dynamic-extent functions to allow TYPEP optimizations.
614 (let ((local-functions '())
615 (cluster-entries '()))
616 (labels ((local-function (lambda-form &optional name)
617 (let ((name (sb!xc:gensym name)))
618 (push `(,name ,@(rest lambda-form)) local-functions)
619 name))
620 (entry-form (type handler)
621 (let ((name (local-function
622 `(lambda (condition)
623 (typep condition ',type))
624 "TYPEP")))
625 `(cons (function ,name) ,handler)))
626 (local-function-handler (type lambda-form)
627 (let ((name (local-function lambda-form "HANDLER")))
628 (push (entry-form type `(function ,name)) cluster-entries)))
629 (process-binding (binding)
630 (unless (proper-list-of-length-p binding 2)
631 (error "ill-formed handler binding: ~S" binding))
632 (destructuring-bind (type handler) binding
633 (typecase handler
634 ((cons (eql lambda) t)
635 (local-function-handler type handler))
636 ((cons (eql function)
637 (cons (cons (eql lambda) t) t))
638 (local-function-handler type (second handler)))
640 (push (apply #'entry-form binding) cluster-entries))))))
641 (cond
642 ((not bindings)
643 form)
645 (mapc #'process-binding bindings)
646 `(dx-flet (,@(reverse local-functions))
647 (let ((*handler-clusters*
648 (list* (list ,@(nreverse cluster-entries)) *handler-clusters*)))
649 #!+stack-allocatable-fixed-objects
650 (declare (truly-dynamic-extent *handler-clusters*))
651 (progn ,form))))))))
653 (defmacro-mundanely handler-bind (bindings &body forms)
654 #!+sb-doc
655 "(HANDLER-BIND ( {(type handler)}* ) body)
657 Executes body in a dynamic context where the given handler bindings are in
658 effect. Each handler must take the condition being signalled as an argument.
659 The bindings are searched first to last in the event of a signalled
660 condition."
661 `(%handler-bind ,bindings
662 #!-x86 (progn ,@forms)
663 ;; Need to catch FP errors here!
664 #!+x86 (multiple-value-prog1 (progn ,@forms) (float-wait))))
666 (defmacro-mundanely handler-case (form &rest cases)
667 #!+sb-doc
668 "(HANDLER-CASE form { (type ([var]) body) }* )
670 Execute FORM in a context with handlers established for the condition types. A
671 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
672 form returns normally, all its values are passed to this clause as if by
673 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
674 specification."
675 (let ((no-error-clause (assoc ':no-error cases)))
676 (if no-error-clause
677 (let ((normal-return (make-symbol "normal-return"))
678 (error-return (make-symbol "error-return")))
679 `(block ,error-return
680 (multiple-value-call (lambda ,@(cdr no-error-clause))
681 (block ,normal-return
682 (return-from ,error-return
683 (handler-case (return-from ,normal-return ,form)
684 ,@(remove no-error-clause cases)))))))
685 (let* ((local-funs nil)
686 (annotated-cases
687 (mapcar (lambda (case)
688 (with-unique-names (tag fun)
689 (destructuring-bind (type ll &body body) case
690 (push `(,fun ,ll ,@body) local-funs)
691 (list tag type ll fun))))
692 cases)))
693 (with-unique-names (block cell form-fun)
694 `(dx-flet ((,form-fun ()
695 #!-x86 ,form
696 ;; Need to catch FP errors here!
697 #!+x86 (multiple-value-prog1 ,form (float-wait)))
698 ,@(reverse local-funs))
699 (declare (optimize (sb!c::check-tag-existence 0)))
700 (block ,block
701 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
702 ;; the variable directly, so that we can stack allocate
703 ;; robustly: dx value cells don't work quite right, and it is
704 ;; possible to construct user code that should loop
705 ;; indefinitely, but instead eats up some stack each time
706 ;; around.
707 (dx-let ((,cell (cons :condition nil)))
708 (declare (ignorable ,cell))
709 (tagbody
710 (%handler-bind
711 ,(mapcar (lambda (annotated-case)
712 (destructuring-bind (tag type ll fun-name) annotated-case
713 (declare (ignore fun-name))
714 (list type
715 `(lambda (temp)
716 ,(if ll
717 `(setf (cdr ,cell) temp)
718 '(declare (ignore temp)))
719 (go ,tag)))))
720 annotated-cases)
721 (return-from ,block (,form-fun)))
722 ,@(mapcan
723 (lambda (annotated-case)
724 (destructuring-bind (tag type ll fun-name) annotated-case
725 (declare (ignore type))
726 (list tag
727 `(return-from ,block
728 ,(if ll
729 `(,fun-name (cdr ,cell))
730 `(,fun-name))))))
731 annotated-cases))))))))))
733 ;;;; miscellaneous
735 (defmacro-mundanely return (&optional (value nil))
736 `(return-from nil ,value))
738 (defmacro-mundanely psetq (&rest pairs)
739 #!+sb-doc
740 "PSETQ {var value}*
741 Set the variables to the values, like SETQ, except that assignments
742 happen in parallel, i.e. no assignments take place until all the
743 forms have been evaluated."
744 ;; Given the possibility of symbol-macros, we delegate to PSETF
745 ;; which knows how to deal with them, after checking that syntax is
746 ;; compatible with PSETQ.
747 (do ((pair pairs (cddr pair)))
748 ((endp pair) `(psetf ,@pairs))
749 (unless (symbolp (car pair))
750 (error 'simple-program-error
751 :format-control "variable ~S in PSETQ is not a SYMBOL"
752 :format-arguments (list (car pair))))))
754 (defmacro-mundanely lambda (&whole whole args &body body)
755 (declare (ignore args body))
756 `#',whole)
758 (defmacro-mundanely named-lambda (&whole whole name args &body body)
759 (declare (ignore name args body))
760 `#',whole)
762 (defmacro-mundanely lambda-with-lexenv (&whole whole
763 declarations macros symbol-macros
764 &body body)
765 (declare (ignore declarations macros symbol-macros body))
766 `#',whole)