Use SB!IMPL as the implementation package for PARSE-BODY
[sbcl.git] / src / code / defboot.lisp
blob3ae03d35e3c67aee69485961b2b2efea05185269
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 ,@(when doc (list doc))
189 ,@decls
190 (block ,(fun-name-block-name name)
191 ,@forms)))
192 (lambda `(lambda ,@lambda-guts))
193 (named-lambda `(named-lambda ,name ,@lambda-guts))
194 (inline-lambda
195 (when (inline-fun-name-p name)
196 ;; we want to attempt to inline, so complain if we can't
197 (or (sb!c:maybe-inline-syntactic-closure lambda env)
198 (progn
199 (#+sb-xc-host warn
200 #-sb-xc-host sb!c:maybe-compiler-notify
201 "lexical environment too hairy, can't inline DEFUN ~S"
202 name)
203 nil)))))
204 `(progn
205 (eval-when (:compile-toplevel)
206 (sb!c:%compiler-defun ',name ',inline-lambda t))
207 ,@(when (typep name '(cons (eql setf)))
208 `((eval-when (:compile-toplevel :execute)
209 (sb!c::warn-if-setf-macro ',name))))
210 (%defun ',name ,named-lambda (sb!c:source-location)
211 ,@(and inline-lambda
212 `(',inline-lambda)))))))
214 #-sb-xc-host
215 (progn (defun %defun (name def source-location &optional inline-lambda)
216 (declare (type function def))
217 ;; should've been checked by DEFMACRO DEFUN
218 (aver (legal-fun-name-p name))
219 (sb!c:%compiler-defun name inline-lambda nil)
220 (when (fboundp name)
221 (warn 'redefinition-with-defun
222 :name name :new-function def :new-location source-location))
223 (setf (sb!xc:fdefinition name) def)
224 ;; %COMPILER-DEFUN doesn't do this except at compile-time, when it
225 ;; also checks package locks. By doing this here we let (SETF
226 ;; FDEFINITION) do the load-time package lock checking before
227 ;; we frob any existing inline expansions.
228 (sb!c::%set-inline-expansion name nil inline-lambda)
229 (sb!c::note-name-defined name :function)
230 name)
231 ;; During cold-init we don't touch the fdefinition.
232 (defun !%quietly-defun (name inline-lambda)
233 (sb!c:%compiler-defun name nil nil) ; makes :WHERE-FROM = :DEFINED
234 (sb!c::%set-inline-expansion name nil inline-lambda)
235 ;; and no need to call NOTE-NAME-DEFINED. It would do nothing.
238 ;;;; DEFVAR and DEFPARAMETER
240 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
241 #!+sb-doc
242 "Define a special variable at top level. Declare the variable
243 SPECIAL and, optionally, initialize it. If the variable already has a
244 value, the old value is not clobbered. The third argument is an optional
245 documentation string for the variable."
246 `(progn
247 (eval-when (:compile-toplevel)
248 (%compiler-defvar ',var))
249 (%defvar ',var
250 (sb!c:source-location)
251 ,@(and valp
252 `((unless (boundp ',var) ,val)))
253 ,@(and docp
254 `(,doc)))))
256 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
257 #!+sb-doc
258 "Define a parameter that is not normally changed by the program,
259 but that may be changed without causing an error. Declare the
260 variable special and sets its value to VAL, overwriting any
261 previous value. The third argument is an optional documentation
262 string for the parameter."
263 `(progn
264 (eval-when (:compile-toplevel)
265 (%compiler-defvar ',var))
266 (%defparameter ',var ,val (sb!c:source-location)
267 ,@(and docp
268 `(,doc)))))
270 (defun %compiler-defvar (var)
271 (sb!xc:proclaim `(special ,var)))
273 #-sb-xc-host
274 (defun %defvar (var source-location &optional (val nil valp) (doc nil docp))
275 (%compiler-defvar var)
276 (when (and valp
277 (not (boundp var)))
278 (set var val))
279 (when docp
280 (setf (fdocumentation var 'variable) doc))
281 (sb!c:with-source-location (source-location)
282 (setf (info :source-location :variable var) source-location))
283 var)
285 #-sb-xc-host
286 (defun %defparameter (var val source-location &optional (doc nil docp))
287 (%compiler-defvar var)
288 (set var val)
289 (when docp
290 (setf (fdocumentation var 'variable) doc))
291 (sb!c:with-source-location (source-location)
292 (setf (info :source-location :variable var) source-location))
293 var)
295 ;;;; iteration constructs
297 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
298 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
299 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
300 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
301 ;;; the same file as FROB-DO-BODY.)
302 (defmacro-mundanely do (varlist endlist &body body)
303 #!+sb-doc
304 "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
305 Iteration construct. Each Var is initialized in parallel to the value of the
306 specified Init form. On subsequent iterations, the Vars are assigned the
307 value of the Step form (if any) in parallel. The Test is evaluated before
308 each evaluation of the body Forms. When the Test is true, the Exit-Forms
309 are evaluated as a PROGN, with the result being the value of the DO. A block
310 named NIL is established around the entire expansion, allowing RETURN to be
311 used as an alternate exit mechanism."
312 (frob-do-body varlist endlist body 'let 'psetq 'do nil))
313 (defmacro-mundanely do* (varlist endlist &body body)
314 #!+sb-doc
315 "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
316 Iteration construct. Each Var is initialized sequentially (like LET*) to the
317 value of the specified Init form. On subsequent iterations, the Vars are
318 sequentially assigned the value of the Step form (if any). The Test is
319 evaluated before each evaluation of the body Forms. When the Test is true,
320 the Exit-Forms are evaluated as a PROGN, with the result being the value
321 of the DO. A block named NIL is established around the entire expansion,
322 allowing RETURN to be used as an alternate exit mechanism."
323 (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
325 ;;; DOTIMES and DOLIST could be defined more concisely using
326 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
327 ;;; it'd be tricky to use them before those things were defined.
328 ;;; They're used enough times before destructuring mechanisms are
329 ;;; defined that it looks as though it's worth just implementing them
330 ;;; ASAP, at the cost of being unable to use the standard
331 ;;; destructuring mechanisms.
332 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
333 (cond ((integerp count)
334 `(do ((,var 0 (1+ ,var)))
335 ((>= ,var ,count) ,result)
336 (declare (type unsigned-byte ,var))
337 ,@body))
339 (let ((c (gensym "COUNT")))
340 `(do ((,var 0 (1+ ,var))
341 (,c ,count))
342 ((>= ,var ,c) ,result)
343 (declare (type unsigned-byte ,var)
344 (type integer ,c))
345 ,@body)))))
347 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body &environment env)
348 ;; We repeatedly bind the var instead of setting it so that we never
349 ;; have to give the var an arbitrary value such as NIL (which might
350 ;; conflict with a declaration). If there is a result form, we
351 ;; introduce a gratuitous binding of the variable to NIL without the
352 ;; declarations, then evaluate the result form in that
353 ;; environment. We spuriously reference the gratuitous variable,
354 ;; since we don't want to use IGNORABLE on what might be a special
355 ;; var.
356 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
357 (let* ((n-list (gensym "N-LIST"))
358 (start (gensym "START")))
359 (multiple-value-bind (clist members clist-ok)
360 (cond ((sb!xc:constantp list env)
361 (let ((value (constant-form-value list env)))
362 (multiple-value-bind (all dot) (list-members value :max-length 20)
363 (when (eql dot t)
364 ;; Full warning is too much: the user may terminate the loop
365 ;; early enough. Contents are still right, though.
366 (style-warn "Dotted list ~S in DOLIST." value))
367 (if (eql dot :maybe)
368 (values value nil nil)
369 (values value all t)))))
370 ((and (consp list) (eq 'list (car list))
371 (every (lambda (arg) (sb!xc:constantp arg env)) (cdr list)))
372 (let ((values (mapcar (lambda (arg) (constant-form-value arg env)) (cdr list))))
373 (values values values t)))
375 (values nil nil nil)))
376 `(block nil
377 (let ((,n-list ,(if clist-ok (list 'quote clist) list)))
378 (tagbody
379 ,start
380 (unless (endp ,n-list)
381 (let ((,var ,(if clist-ok
382 `(truly-the (member ,@members) (car ,n-list))
383 `(car ,n-list))))
384 ,@decls
385 (setq ,n-list (cdr ,n-list))
386 (tagbody ,@forms))
387 (go ,start))))
388 ,(if result
389 `(let ((,var nil))
390 ;; Filter out TYPE declarations (VAR gets bound to NIL,
391 ;; and might have a conflicting type declaration) and
392 ;; IGNORE (VAR might be ignored in the loop body, but
393 ;; it's used in the result form).
394 ,@(filter-dolist-declarations decls)
395 ,var
396 ,result)
397 nil))))))
399 ;;;; conditions, handlers, restarts
401 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
402 ;;; macros in the compiler before the DEFVARs are compiled.
404 ;;; For an explanation of these data structures, see DEFVARs in
405 ;;; target-error.lisp.
406 (sb!xc:proclaim '(special *handler-clusters* *restart-clusters*))
408 ;;; Generated code need not check for unbound-marker in *HANDLER-CLUSTERS*
409 ;;; (resp *RESTART-). To elicit this we must poke at the info db.
410 ;;; SB!XC:PROCLAIM SPECIAL doesn't advise the host Lisp that *HANDLER-CLUSTERS*
411 ;;; is special and so it rightfully complains about a SETQ of the variable.
412 ;;; But I must SETQ if proclaming ALWAYS-BOUND because the xc asks the host
413 ;;; whether it's currently bound.
414 ;;; But the DEFVARs are in target-error. So it's one hack or another.
415 (setf (info :variable :always-bound '*handler-clusters*)
416 #+sb-xc :always-bound #-sb-xc :eventually)
417 (setf (info :variable :always-bound '*restart-clusters*)
418 #+sb-xc :always-bound #-sb-xc :eventually)
420 (defmacro-mundanely with-condition-restarts
421 (condition-form restarts-form &body body)
422 #!+sb-doc
423 "Evaluates the BODY in a dynamic environment where the restarts in the list
424 RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
425 This allows FIND-RESTART, etc., to recognize restarts that are not related
426 to the error currently being debugged. See also RESTART-CASE."
427 (once-only ((restarts restarts-form))
428 (with-unique-names (restart)
429 ;; FIXME: check the need for interrupt-safety.
430 `(unwind-protect
431 (progn
432 (dolist (,restart ,restarts)
433 (push ,condition-form
434 (restart-associated-conditions ,restart)))
435 ,@body)
436 (dolist (,restart ,restarts)
437 (pop (restart-associated-conditions ,restart)))))))
439 (defmacro-mundanely restart-bind (bindings &body forms)
440 #!+sb-doc
441 "(RESTART-BIND ({(case-name function {keyword value}*)}*) forms)
442 Executes forms in a dynamic context where the given bindings are in
443 effect. Users probably want to use RESTART-CASE. A case-name of NIL
444 indicates an anonymous restart. When bindings contain the same
445 restart name, FIND-RESTART will find the first such binding."
446 (flet ((parse-binding (binding)
447 (unless (>= (length binding) 2)
448 (error "ill-formed restart binding: ~S" binding))
449 (destructuring-bind (name function
450 &key interactive-function
451 test-function
452 report-function)
453 binding
454 (unless (or name report-function)
455 (warn "Unnamed restart does not have a report function: ~
456 ~S" binding))
457 `(make-restart ',name ,function
458 ,@(and (or report-function
459 interactive-function
460 test-function)
461 `(,report-function))
462 ,@(and (or interactive-function
463 test-function)
464 `(,interactive-function))
465 ,@(and test-function
466 `(,test-function))))))
467 `(let ((*restart-clusters*
468 (cons (list ,@(mapcar #'parse-binding bindings))
469 *restart-clusters*)))
470 (declare (truly-dynamic-extent *restart-clusters*))
471 ,@forms)))
473 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
474 ;;; appropriate. Gross, but it's what the book seems to say...
475 (defun munge-restart-case-expression (expression env)
476 (let ((exp (%macroexpand expression env)))
477 (if (consp exp)
478 (let* ((name (car exp))
479 (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
480 (if (member name '(signal error cerror warn))
481 (once-only ((n-cond `(coerce-to-condition
482 ,(first args)
483 (list ,@(rest args))
484 ',(case name
485 (warn 'simple-warning)
486 (signal 'simple-condition)
487 (t 'simple-error))
488 ',name)))
489 `(with-condition-restarts
490 ,n-cond
491 (car *restart-clusters*)
492 ,(if (eq name 'cerror)
493 `(cerror ,(second exp) ,n-cond)
494 `(,name ,n-cond))))
495 expression))
496 expression)))
498 (defmacro-mundanely restart-case (expression &body clauses &environment env)
499 #!+sb-doc
500 "(RESTART-CASE form {(case-name arg-list {keyword value}* body)}*)
501 The form is evaluated in a dynamic context where the clauses have
502 special meanings as points to which control may be transferred (see
503 INVOKE-RESTART). When clauses contain the same case-name,
504 FIND-RESTART will find the first such clause. If form is a call to
505 SIGNAL, ERROR, CERROR or WARN (or macroexpands into such) then the
506 signalled condition will be associated with the new restarts."
507 ;; PARSE-CLAUSE (which uses PARSE-KEYWORDS-AND-BODY) is used to
508 ;; parse all clauses into lists of the form
510 ;; (NAME TAG KEYWORDS LAMBDA-LIST BODY)
512 ;; where KEYWORDS are suitable keywords for use in HANDLER-BIND
513 ;; bindings. These lists are then passed to
514 ;; * MAKE-BINDING which generates bindings for the respective NAME
515 ;; for HANDLER-BIND
516 ;; * MAKE-APPLY-AND-RETURN which generates TAGBODY entries executing
517 ;; the respective BODY.
518 (let ((block-tag (sb!xc:gensym "BLOCK"))
519 (temp-var (gensym)))
520 (labels ((parse-keywords-and-body (keywords-and-body)
521 (do ((form keywords-and-body (cddr form))
522 (result '())) (nil)
523 (destructuring-bind (&optional key (arg nil argp) &rest rest)
524 form
525 (declare (ignore rest))
526 (setq result
527 (append
528 (cond
529 ((and (eq key :report) argp)
530 (list :report-function
531 (if (stringp arg)
532 `#'(lambda (stream)
533 (write-string ,arg stream))
534 `#',arg)))
535 ((and (eq key :interactive) argp)
536 (list :interactive-function `#',arg))
537 ((and (eq key :test) argp)
538 (list :test-function `#',arg))
540 (return (values result form))))
541 result)))))
542 (parse-clause (clause)
543 (unless (and (listp clause) (>= (length clause) 2)
544 (listp (second clause)))
545 (error "ill-formed ~S clause, no lambda-list:~% ~S"
546 'restart-case clause))
547 (destructuring-bind (name lambda-list &body body) clause
548 (multiple-value-bind (keywords body)
549 (parse-keywords-and-body body)
550 (list name (sb!xc:gensym "TAG") keywords lambda-list body))))
551 (make-binding (clause-data)
552 (destructuring-bind (name tag keywords lambda-list body) clause-data
553 (declare (ignore body))
554 `(,name
555 (lambda ,(cond ((null lambda-list)
557 ((and (null (cdr lambda-list))
558 (not (member (car lambda-list)
559 '(&optional &key &aux))))
560 '(temp))
562 '(&rest temp)))
563 ,@(when lambda-list `((setq ,temp-var temp)))
564 (locally (declare (optimize (safety 0)))
565 (go ,tag)))
566 ,@keywords)))
567 (make-apply-and-return (clause-data)
568 (destructuring-bind (name tag keywords lambda-list body) clause-data
569 (declare (ignore name keywords))
570 `(,tag (return-from ,block-tag
571 ,(cond ((null lambda-list)
572 `(progn ,@body))
573 ((and (null (cdr lambda-list))
574 (not (member (car lambda-list)
575 '(&optional &key &aux))))
576 `(funcall (lambda ,lambda-list ,@body) ,temp-var))
578 `(apply (lambda ,lambda-list ,@body) ,temp-var))))))))
579 (let ((clauses-data (mapcar #'parse-clause clauses)))
580 `(block ,block-tag
581 (let ((,temp-var nil))
582 (declare (ignorable ,temp-var))
583 (tagbody
584 (restart-bind
585 ,(mapcar #'make-binding clauses-data)
586 (return-from ,block-tag
587 ,(munge-restart-case-expression expression env)))
588 ,@(mapcan #'make-apply-and-return clauses-data))))))))
590 (defmacro-mundanely with-simple-restart ((restart-name format-string
591 &rest format-arguments)
592 &body forms)
593 #!+sb-doc
594 "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
595 body)
596 If restart-name is not invoked, then all values returned by forms are
597 returned. If control is transferred to this restart, it immediately
598 returns the values NIL and T."
599 (let ((stream (sb!xc:gensym "STREAM")))
600 `(restart-case
601 ;; If there's just one body form, then don't use PROGN. This allows
602 ;; RESTART-CASE to "see" calls to ERROR, etc.
603 ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
604 (,restart-name ()
605 :report (lambda (,stream)
606 (declare (type stream ,stream))
607 (format ,stream ,format-string ,@format-arguments))
608 (values nil t)))))
610 (defmacro-mundanely %handler-bind (bindings form)
611 ;; As an optimization, this looks at the handler parts of BINDINGS
612 ;; and turns handlers of the forms (lambda ...) and (function
613 ;; (lambda ...)) into local, dynamic-extent functions.
615 ;; Type specifiers in BINDINGS are translated into local,
616 ;; dynamic-extent functions to allow TYPEP optimizations.
617 (let ((local-functions '())
618 (cluster-entries '()))
619 (labels ((local-function (lambda-form &optional name)
620 (let ((name (sb!xc:gensym name)))
621 (push `(,name ,@(rest lambda-form)) local-functions)
622 name))
623 (entry-form (type handler)
624 (let ((name (local-function
625 `(lambda (condition)
626 (typep condition ',type))
627 "TYPEP")))
628 `(cons (function ,name) ,handler)))
629 (local-function-handler (type lambda-form)
630 (let ((name (local-function lambda-form "HANDLER")))
631 (push (entry-form type `(function ,name)) cluster-entries)))
632 (process-binding (binding)
633 (unless (proper-list-of-length-p binding 2)
634 (error "ill-formed handler binding: ~S" binding))
635 (destructuring-bind (type handler) binding
636 (typecase handler
637 ((cons (eql lambda) t)
638 (local-function-handler type handler))
639 ((cons (eql function) (cons (cons (eql lambda)) null))
640 (local-function-handler type (second handler)))
642 (push (apply #'entry-form binding) cluster-entries))))))
643 (cond
644 ((not bindings)
645 form)
647 (mapc #'process-binding bindings)
648 `(dx-flet (,@(reverse local-functions))
649 (let ((*handler-clusters*
650 (list* (list ,@(nreverse cluster-entries)) *handler-clusters*)))
651 #!+stack-allocatable-fixed-objects
652 (declare (truly-dynamic-extent *handler-clusters*))
653 (progn ,form))))))))
655 (defmacro-mundanely handler-bind (bindings &body forms)
656 #!+sb-doc
657 "(HANDLER-BIND ( {(type handler)}* ) body)
659 Executes body in a dynamic context where the given handler bindings are in
660 effect. Each handler must take the condition being signalled as an argument.
661 The bindings are searched first to last in the event of a signalled
662 condition."
663 `(%handler-bind ,bindings
664 #!-x86 (progn ,@forms)
665 ;; Need to catch FP errors here!
666 #!+x86 (multiple-value-prog1 (progn ,@forms) (float-wait))))
668 (defmacro-mundanely handler-case (form &rest cases)
669 #!+sb-doc
670 "(HANDLER-CASE form { (type ([var]) body) }* )
672 Execute FORM in a context with handlers established for the condition types. A
673 peculiar property allows type to be :NO-ERROR. If such a clause occurs, and
674 form returns normally, all its values are passed to this clause as if by
675 MULTIPLE-VALUE-CALL. The :NO-ERROR clause accepts more than one var
676 specification."
677 (let ((no-error-clause (assoc ':no-error cases)))
678 (if no-error-clause
679 (let ((normal-return (make-symbol "normal-return"))
680 (error-return (make-symbol "error-return")))
681 `(block ,error-return
682 (multiple-value-call (lambda ,@(cdr no-error-clause))
683 (block ,normal-return
684 (return-from ,error-return
685 (handler-case (return-from ,normal-return ,form)
686 ,@(remove no-error-clause cases)))))))
687 (let* ((local-funs nil)
688 (annotated-cases
689 (mapcar (lambda (case)
690 (with-unique-names (tag fun)
691 (destructuring-bind (type ll &body body) case
692 (push `(,fun ,ll ,@body) local-funs)
693 (list tag type ll fun))))
694 cases)))
695 (with-unique-names (block cell form-fun)
696 `(dx-flet ((,form-fun ()
697 #!-x86 ,form
698 ;; Need to catch FP errors here!
699 #!+x86 (multiple-value-prog1 ,form (float-wait)))
700 ,@(reverse local-funs))
701 (declare (optimize (sb!c::check-tag-existence 0)))
702 (block ,block
703 ;; KLUDGE: We use a dx CONS cell instead of just assigning to
704 ;; the variable directly, so that we can stack allocate
705 ;; robustly: dx value cells don't work quite right, and it is
706 ;; possible to construct user code that should loop
707 ;; indefinitely, but instead eats up some stack each time
708 ;; around.
709 (dx-let ((,cell (cons :condition nil)))
710 (declare (ignorable ,cell))
711 (tagbody
712 (%handler-bind
713 ,(mapcar (lambda (annotated-case)
714 (destructuring-bind (tag type ll fun-name) annotated-case
715 (declare (ignore fun-name))
716 (list type
717 `(lambda (temp)
718 ,(if ll
719 `(setf (cdr ,cell) temp)
720 '(declare (ignore temp)))
721 (go ,tag)))))
722 annotated-cases)
723 (return-from ,block (,form-fun)))
724 ,@(mapcan
725 (lambda (annotated-case)
726 (destructuring-bind (tag type ll fun-name) annotated-case
727 (declare (ignore type))
728 (list tag
729 `(return-from ,block
730 ,(if ll
731 `(,fun-name (cdr ,cell))
732 `(,fun-name))))))
733 annotated-cases))))))))))
735 ;;;; miscellaneous
737 (defmacro-mundanely return (&optional (value nil))
738 `(return-from nil ,value))
740 (defmacro-mundanely lambda (&whole whole args &body body)
741 (declare (ignore args body))
742 `#',whole)
744 (defmacro-mundanely named-lambda (&whole whole name args &body body)
745 (declare (ignore name args body))
746 `#',whole)
748 (defmacro-mundanely lambda-with-lexenv (&whole whole
749 declarations macros symbol-macros
750 &body body)
751 (declare (ignore declarations macros symbol-macros body))
752 `#',whole)