1 ;;;; bootstrapping fundamental machinery (e.g. DEFUN, DEFCONSTANT,
2 ;;;; DEFVAR) from special forms and primitive functions
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.
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")
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)
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
))
44 (let ((ignore (sb!xc
:gensym
)))
45 `(multiple-value-call #'(lambda (&optional
,@(mapcar #'list vars
)
47 (declare (ignore ,ignore
))
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:
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
)
72 (let ((clause (first clauses
))
73 (more (rest clauses
)))
75 (error 'simple-type-error
76 :format-control
"COND clause is not a ~S: ~S"
77 :format-arguments
(list 'cons clause
)
80 (let ((test (first clause
))
81 (forms (rest clause
)))
83 (let ((n-result (gensym)))
84 `(let ((,n-result
,test
))
89 ;; THE to preserve non-toplevelness for FOO in
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
))
98 ,(when more
`(cond ,@more
))))))))))
100 (defmacro-mundanely when
(test &body forms
)
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
)
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
)
115 ;; Preserve non-toplevelness of the form!
116 `(the t
,(first forms
)))
122 (defmacro-mundanely or
(&rest forms
)
123 (cond ((endp forms
) nil
)
125 ;; Preserve non-toplevelness of the form!
126 `(the t
,(first forms
)))
128 (let ((n-result (gensym)))
129 `(let ((,n-result
,(first forms
)))
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
)
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
))
154 (defmacro-mundanely prog2
(form1 result
&body body
)
155 `(prog1 (progn ,form1
,result
) ,@body
))
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))
169 ;; (DECLAIM (NOTINLINE FOO))
170 ;; has been used, and then we later do another
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
)
181 "Define a function at top level."
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
189 (block ,(fun-name-block-name name
)
191 (lambda `(lambda ,@lambda-guts
))
192 (named-lambda `(named-lambda ,name
,@lambda-guts
))
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
)
199 #-sb-xc-host sb
!c
:maybe-compiler-notify
200 "lexical environment too hairy, can't inline DEFUN ~S"
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
))))))
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
)
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
)
230 (setf (%fun-doc def
) doc
))
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.
238 (setf (%fun-doc
(fdefinition name
)) doc
))))
240 ;;;; DEFVAR and DEFPARAMETER
242 (defmacro-mundanely defvar
(var &optional
(val nil valp
) (doc nil docp
))
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."
249 (eval-when (:compile-toplevel
)
250 (%compiler-defvar
',var
))
251 (%defvar
',var
(unless (boundp ',var
) ,val
)
253 (sb!c
:source-location
))))
255 (defmacro-mundanely defparameter
(var val
&optional
(doc nil docp
))
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."
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
)))
271 (defun %defvar
(var val valp doc docp source-location
)
272 (%compiler-defvar var
)
277 (setf (fdocumentation var
'variable
) doc
))
278 (sb!c
:with-source-location
(source-location)
279 (setf (info :source-location
:variable var
) source-location
))
283 (defun %defparameter
(var val doc docp source-location
)
284 (%compiler-defvar var
)
287 (setf (fdocumentation var
'variable
) doc
))
288 (sb!c
:with-source-location
(source-location)
289 (setf (info :source-location
:variable var
) source-location
))
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
)
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
)
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
))
336 (let ((c (gensym "COUNT")))
337 `(do ((,var
0 (1+ ,var
))
339 ((>= ,var
,c
) ,result
)
340 (declare (type unsigned-byte
,var
)
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
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)
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
))
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
)))
374 (let ((,n-list
,(if clist-ok
(list 'quote clist
) list
)))
377 (unless (endp ,n-list
)
378 (let ((,var
,(if clist-ok
379 `(truly-the (member ,@members
) (car ,n-list
))
382 (setq ,n-list
(cdr ,n-list
))
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
)
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
)
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.
429 (dolist (,restart
,restarts
)
430 (push ,condition-form
431 (restart-associated-conditions ,restart
)))
433 (dolist (,restart
,restarts
)
434 (pop (restart-associated-conditions ,restart
)))))))
436 (defmacro-mundanely restart-bind
(bindings &body forms
)
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
451 (unless (or name report-function
)
452 (warn "Unnamed restart does not have a report function: ~
454 `(make-restart ',name
,function
455 ,@(and (or report-function
459 ,@(and (or interactive-function
461 `(,interactive-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
*))
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
)))
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
482 (warn 'simple-warning
)
483 (signal 'simple-condition
)
486 `(with-condition-restarts
488 (car *restart-clusters
*)
489 ,(if (eq name
'cerror
)
490 `(cerror ,(second exp
) ,n-cond
)
495 (defmacro-mundanely restart-case
(expression &body clauses
&environment env
)
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
513 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
514 ;; the respective BODY.
515 (let ((block-tag (sb!xc
:gensym
"BLOCK"))
517 (labels ((parse-keywords-and-body (keywords-and-body)
518 (do ((form keywords-and-body
(cddr form
))
520 (destructuring-bind (&optional key
(arg nil argp
) &rest rest
)
522 (declare (ignore rest
))
526 ((and (eq key
:report
) argp
)
527 (list :report-function
530 (write-string ,arg stream
))
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
))))
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
))
552 (lambda ,(cond ((null lambda-list
)
554 ((and (null (cdr lambda-list
))
555 (not (member (car lambda-list
)
556 '(&optional
&key
&aux
))))
560 ,@(when lambda-list
`((setq ,temp-var temp
)))
561 (locally (declare (optimize (safety 0)))
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
)
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
)))
578 (let ((,temp-var nil
))
579 (declare (ignorable ,temp-var
))
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
)
591 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
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")))
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
))
602 :report
(lambda (,stream
)
603 (declare (type stream
,stream
))
604 (format ,stream
,format-string
,@format-arguments
))
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
)
620 (entry-form (type handler
)
621 (let ((name (local-function
623 (typep condition
',type
))
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
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
))))))
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
*))
653 (defmacro-mundanely handler-bind
(bindings &body forms
)
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
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
)
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
675 (let ((no-error-clause (assoc ':no-error cases
)))
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
)
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
))))
693 (with-unique-names (block cell form-fun
)
694 `(dx-flet ((,form-fun
()
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)))
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
707 (dx-let ((,cell
(cons :condition nil
)))
708 (declare (ignorable ,cell
))
711 ,(mapcar (lambda (annotated-case)
712 (destructuring-bind (tag type ll fun-name
) annotated-case
713 (declare (ignore fun-name
))
717 `(setf (cdr ,cell
) temp
)
718 '(declare (ignore temp
)))
721 (return-from ,block
(,form-fun
)))
723 (lambda (annotated-case)
724 (destructuring-bind (tag type ll fun-name
) annotated-case
725 (declare (ignore type
))
729 `(,fun-name
(cdr ,cell
))
731 annotated-cases
))))))))))
735 (defmacro-mundanely return
(&optional
(value nil
))
736 `(return-from nil
,value
))
738 (defmacro-mundanely psetq
(&rest pairs
)
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
))
758 (defmacro-mundanely named-lambda
(&whole whole name args
&body body
)
759 (declare (ignore name args body
))
762 (defmacro-mundanely lambda-with-lexenv
(&whole whole
763 declarations macros symbol-macros
765 (declare (ignore declarations macros symbol-macros body
))