1.0.14.26: tweak COND slightly
[sbcl.git] / src / code / defboot.lisp
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.
11
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.
20
21 (in-package "SB!IMPL")
22
23 \f
24 ;;;; IN-PACKAGE
25
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)))))
30 \f
31 ;;;; MULTIPLE-VALUE-FOO
32
33 (defun list-of-symbols-p (x)
34   (and (listp x)
35        (every #'symbolp x)))
36
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 (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)))
51
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)))
62
63 (defmacro-mundanely multiple-value-list (value-form)
64   `(multiple-value-call #'list ,value-form))
65 \f
66 ;;;; various conditional constructs
67
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 "COND clause is not a list: ~S" clause)
76             (let ((test (first clause))
77                   (forms (rest clause)))
78               (if (endp forms)
79                   (let ((n-result (gensym)))
80                     `(let ((,n-result ,test))
81                        (if ,n-result
82                            ,n-result
83                            (cond ,@more))))
84                   (if (eq t test)
85                       `(progn ,@forms)
86                       `(if ,test
87                            (progn ,@forms)
88                            ,(when more `(cond ,@more))))))))))
89
90 (defmacro-mundanely when (test &body forms)
91   #!+sb-doc
92   "If the first argument is true, the rest of the forms are
93 evaluated as a PROGN."
94   `(if ,test (progn ,@forms) nil))
95
96 (defmacro-mundanely unless (test &body forms)
97   #!+sb-doc
98   "If the first argument is not true, the rest of the forms are
99 evaluated as a PROGN."
100   `(if ,test nil (progn ,@forms)))
101
102 (defmacro-mundanely and (&rest forms)
103   (cond ((endp forms) t)
104         ((endp (rest forms)) (first forms))
105         (t
106          `(if ,(first forms)
107               (and ,@(rest forms))
108               nil))))
109
110 (defmacro-mundanely or (&rest forms)
111   (cond ((endp forms) nil)
112         ((endp (rest forms)) (first forms))
113         (t
114          (let ((n-result (gensym)))
115            `(let ((,n-result ,(first forms)))
116               (if ,n-result
117                   ,n-result
118                   (or ,@(rest forms))))))))
119 \f
120 ;;;; various sequencing constructs
121
122 (flet ((prog-expansion-from-let (varlist body-decls let)
123          (multiple-value-bind (body decls)
124              (parse-body body-decls :doc-string-allowed nil)
125            `(block nil
126               (,let ,varlist
127                 ,@decls
128                 (tagbody ,@body))))))
129   (defmacro-mundanely prog (varlist &body body-decls)
130     (prog-expansion-from-let varlist body-decls 'let))
131   (defmacro-mundanely prog* (varlist &body body-decls)
132     (prog-expansion-from-let varlist body-decls 'let*)))
133
134 (defmacro-mundanely prog1 (result &body body)
135   (let ((n-result (gensym)))
136     `(let ((,n-result ,result))
137        ,@body
138        ,n-result)))
139
140 (defmacro-mundanely prog2 (form1 result &body body)
141   `(prog1 (progn ,form1 ,result) ,@body))
142 \f
143 ;;;; DEFUN
144
145 ;;; Should we save the inline expansion of the function named NAME?
146 (defun inline-fun-name-p (name)
147   (or
148    ;; the normal reason for saving the inline expansion
149    (info :function :inlinep name)
150    ;; another reason for saving the inline expansion: If the
151    ;; ANSI-recommended idiom
152    ;;   (DECLAIM (INLINE FOO))
153    ;;   (DEFUN FOO ..)
154    ;;   (DECLAIM (NOTINLINE FOO))
155    ;; has been used, and then we later do another
156    ;;   (DEFUN FOO ..)
157    ;; without a preceding
158    ;;   (DECLAIM (INLINE FOO))
159    ;; what should we do with the old inline expansion when we see the
160    ;; new DEFUN? Overwriting it with the new definition seems like
161    ;; the only unsurprising choice.
162    (info :function :inline-expansion-designator name)))
163
164 (defmacro-mundanely defun (&environment env name args &body body)
165   "Define a function at top level."
166   #+sb-xc-host
167   (unless (symbol-package (fun-name-block-name name))
168     (warn "DEFUN of uninterned function name ~S (tricky for GENESIS)" name))
169   (multiple-value-bind (forms decls doc) (parse-body body)
170     (let* (;; stuff shared between LAMBDA and INLINE-LAMBDA and NAMED-LAMBDA
171            (lambda-guts `(,args
172                           ,@decls
173                           (block ,(fun-name-block-name name)
174                             ,@forms)))
175            (lambda `(lambda ,@lambda-guts))
176            #-sb-xc-host
177            (named-lambda `(named-lambda ,name ,@lambda-guts))
178            (inline-lambda
179             (when (inline-fun-name-p name)
180               ;; we want to attempt to inline, so complain if we can't
181               (or (sb!c:maybe-inline-syntactic-closure lambda env)
182                   (progn
183                     (#+sb-xc-host warn
184                      #-sb-xc-host sb!c:maybe-compiler-notify
185                      "lexical environment too hairy, can't inline DEFUN ~S"
186                      name)
187                     nil)))))
188       `(progn
189          ;; In cross-compilation of toplevel DEFUNs, we arrange for
190          ;; the LAMBDA to be statically linked by GENESIS.
191          ;;
192          ;; It may seem strangely inconsistent not to use NAMED-LAMBDA
193          ;; here instead of LAMBDA. The reason is historical:
194          ;; COLD-FSET was written before NAMED-LAMBDA, and has special
195          ;; logic of its own to notify the compiler about NAME.
196          #+sb-xc-host
197          (cold-fset ,name ,lambda)
198
199          (eval-when (:compile-toplevel)
200            (sb!c:%compiler-defun ',name ',inline-lambda t))
201          (eval-when (:load-toplevel :execute)
202            (%defun ',name
203                    ;; In normal compilation (not for cold load) this is
204                    ;; where the compiled LAMBDA first appears. In
205                    ;; cross-compilation, we manipulate the
206                    ;; previously-statically-linked LAMBDA here.
207                    #-sb-xc-host ,named-lambda
208                    #+sb-xc-host (fdefinition ',name)
209                    ,doc
210                    ',inline-lambda
211                    (sb!c:source-location)))))))
212
213 #-sb-xc-host
214 (defun %defun (name def doc inline-lambda source-location)
215   (declare (ignore source-location))
216   (declare (type function def))
217   (declare (type (or null simple-string) doc))
218   (aver (legal-fun-name-p name)) ; should've been checked by DEFMACRO DEFUN
219   (sb!c:%compiler-defun name inline-lambda nil)
220   (when (fboundp name)
221     (/show0 "redefining NAME in %DEFUN")
222     (style-warn "redefining ~S in DEFUN" name))
223   (setf (sb!xc:fdefinition name) def)
224
225   (sb!c::note-name-defined name :function)
226
227   ;; FIXME: I want to do this here (and fix bug 137), but until the
228   ;; breathtaking CMU CL function name architecture is converted into
229   ;; something sane, (1) doing so doesn't really fix the bug, and
230   ;; (2) doing probably isn't even really safe.
231   #+nil (setf (%fun-name def) name)
232
233   (when doc
234     (setf (fdocumentation name 'function) doc)
235     #!+sb-eval
236     (when (typep def 'sb!eval:interpreted-function)
237       (setf (sb!eval:interpreted-function-documentation def)
238             doc)))
239   name)
240 \f
241 ;;;; DEFVAR and DEFPARAMETER
242
243 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
244   #!+sb-doc
245   "Define a global variable at top level. Declare the variable
246   SPECIAL and, optionally, initialize it. If the variable already has a
247   value, the old value is not clobbered. The third argument is an optional
248   documentation string for the variable."
249   `(progn
250      (eval-when (:compile-toplevel)
251        (%compiler-defvar ',var))
252      (eval-when (:load-toplevel :execute)
253        (%defvar ',var (unless (boundp ',var) ,val)
254                 ',valp ,doc ',docp
255                 (sb!c:source-location)))))
256
257 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
258   #!+sb-doc
259   "Define a parameter that is not normally changed by the program,
260   but that may be changed without causing an error. Declare the
261   variable special and sets its value to VAL, overwriting any
262   previous value. The third argument is an optional documentation
263   string for the parameter."
264   `(progn
265      (eval-when (:compile-toplevel)
266        (%compiler-defvar ',var))
267      (eval-when (:load-toplevel :execute)
268        (%defparameter ',var ,val ,doc ',docp (sb!c:source-location)))))
269
270 (defun %compiler-defvar (var)
271   (sb!xc:proclaim `(special ,var)))
272
273 #-sb-xc-host
274 (defun %defvar (var val valp doc docp source-location)
275   (%compiler-defvar var)
276   (when valp
277     (unless (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)
284
285 #-sb-xc-host
286 (defun %defparameter (var val doc docp source-location)
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)
294 \f
295 ;;;; iteration constructs
296
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 laternate exit mechanism."
323   (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
324
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 ((numberp count)
334         `(do ((,var 0 (1+ ,var)))
335              ((>= ,var ,count) ,result)
336            (declare (type unsigned-byte ,var))
337            ,@body))
338         (t
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)))))
346
347 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
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       `(block nil
360          (let ((,n-list ,list))
361            (tagbody
362               ,start
363               (unless (endp ,n-list)
364                 (let ((,var (car ,n-list)))
365                   ,@decls
366                   (setq ,n-list (cdr ,n-list))
367                   (tagbody ,@forms))
368                 (go ,start))))
369          ,(if result
370               `(let ((,var nil))
371                  ;; Filter out TYPE declarations (VAR gets bound to NIL,
372                  ;; and might have a conflicting type declaration) and
373                  ;; IGNORE (VAR might be ignored in the loop body, but
374                  ;; it's used in the result form).
375                  ,@(filter-dolist-declarations decls)
376                  ,var
377                  ,result)
378                nil)))))
379 \f
380 ;;;; conditions, handlers, restarts
381
382 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
383 ;;; macros in the compiler before the DEFVARs are compiled.
384 (sb!xc:proclaim
385  '(special *handler-clusters* *restart-clusters* *condition-restarts*))
386
387 (defmacro-mundanely with-condition-restarts
388     (condition-form restarts-form &body body)
389   #!+sb-doc
390   "Evaluates the BODY in a dynamic environment where the restarts in the list
391    RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
392    This allows FIND-RESTART, etc., to recognize restarts that are not related
393    to the error currently being debugged. See also RESTART-CASE."
394   (let ((n-cond (gensym)))
395     `(let ((*condition-restarts*
396             (cons (let ((,n-cond ,condition-form))
397                     (cons ,n-cond
398                           (append ,restarts-form
399                                   (cdr (assoc ,n-cond *condition-restarts*)))))
400                   *condition-restarts*)))
401        ,@body)))
402
403 (defmacro-mundanely restart-bind (bindings &body forms)
404   #!+sb-doc
405   "Executes forms in a dynamic context where the given restart bindings are
406    in effect. Users probably want to use RESTART-CASE. When clauses contain
407    the same restart name, FIND-RESTART will find the first such clause."
408   `(let ((*restart-clusters*
409           (cons (list
410                  ,@(mapcar (lambda (binding)
411                              (unless (or (car binding)
412                                          (member :report-function
413                                                  binding
414                                                  :test #'eq))
415                                (warn "Unnamed restart does not have a ~
416                                       report function: ~S"
417                                      binding))
418                              `(make-restart :name ',(car binding)
419                                             :function ,(cadr binding)
420                                             ,@(cddr binding)))
421                            bindings))
422                 *restart-clusters*)))
423      ,@forms))
424
425 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
426 ;;; appropriate. Gross, but it's what the book seems to say...
427 (defun munge-restart-case-expression (expression env)
428   (let ((exp (sb!xc:macroexpand expression env)))
429     (if (consp exp)
430         (let* ((name (car exp))
431                (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
432           (if (member name '(signal error cerror warn))
433               (once-only ((n-cond `(coerce-to-condition
434                                     ,(first args)
435                                     (list ,@(rest args))
436                                     ',(case name
437                                         (warn 'simple-warning)
438                                         (signal 'simple-condition)
439                                         (t 'simple-error))
440                                     ',name)))
441                 `(with-condition-restarts
442                      ,n-cond
443                      (car *restart-clusters*)
444                    ,(if (eq name 'cerror)
445                         `(cerror ,(second exp) ,n-cond)
446                         `(,name ,n-cond))))
447               expression))
448         expression)))
449
450 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
451 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
452 (defmacro-mundanely restart-case (expression &body clauses &environment env)
453   #!+sb-doc
454   "(RESTART-CASE form
455    {(case-name arg-list {keyword value}* body)}*)
456    The form is evaluated in a dynamic context where the clauses have special
457    meanings as points to which control may be transferred (see INVOKE-RESTART).
458    When clauses contain the same case-name, FIND-RESTART will find the first
459    such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
460    macroexpands into such) then the signalled condition will be associated with
461    the new restarts."
462   (flet ((transform-keywords (&key report interactive test)
463            (let ((result '()))
464              (when report
465                (setq result (list* (if (stringp report)
466                                        `#'(lambda (stream)
467                                             (write-string ,report stream))
468                                        `#',report)
469                                    :report-function
470                                    result)))
471              (when interactive
472                (setq result (list* `#',interactive
473                                    :interactive-function
474                                    result)))
475              (when test
476                (setq result (list* `#',test :test-function result)))
477              (nreverse result)))
478          (parse-keyword-pairs (list keys)
479            (do ((l list (cddr l))
480                 (k '() (list* (cadr l) (car l) k)))
481                ((or (null l) (not (member (car l) keys)))
482                 (values (nreverse k) l)))))
483     (let ((block-tag (gensym))
484           (temp-var (gensym))
485           (data
486            (macrolet (;; KLUDGE: This started as an old DEFMACRO
487                       ;; WITH-KEYWORD-PAIRS general utility, which was used
488                       ;; only in this one place in the code. It was translated
489                       ;; literally into this MACROLET in order to avoid some
490                       ;; cross-compilation bootstrap problems. It would almost
491                       ;; certainly be clearer, and it would certainly be more
492                       ;; concise, to do a more idiomatic translation, merging
493                       ;; this with the TRANSFORM-KEYWORDS logic above.
494                       ;;   -- WHN 19990925
495                       (with-keyword-pairs ((names expression) &body forms)
496                         (let ((temp (member '&rest names)))
497                           (unless (= (length temp) 2)
498                             (error "&REST keyword is ~:[missing~;misplaced~]."
499                                    temp))
500                           (let* ((key-vars (ldiff names temp))
501                                  (keywords (mapcar #'keywordicate key-vars))
502                                  (key-var (gensym))
503                                  (rest-var (cadr temp)))
504                             `(multiple-value-bind (,key-var ,rest-var)
505                                  (parse-keyword-pairs ,expression ',keywords)
506                                (let ,(mapcar (lambda (var keyword)
507                                                `(,var (getf ,key-var
508                                                             ,keyword)))
509                                              key-vars keywords)
510                                  ,@forms))))))
511              (mapcar (lambda (clause)
512                        (with-keyword-pairs ((report interactive test
513                                                     &rest forms)
514                                             (cddr clause))
515                          (list (car clause) ;name=0
516                                (gensym) ;tag=1
517                                (transform-keywords :report report ;keywords=2
518                                                    :interactive interactive
519                                                    :test test)
520                                (cadr clause) ;bvl=3
521                                forms))) ;body=4
522                    clauses))))
523       `(block ,block-tag
524          (let ((,temp-var nil))
525            (tagbody
526             (restart-bind
527                 ,(mapcar (lambda (datum)
528                            (let ((name (nth 0 datum))
529                                  (tag  (nth 1 datum))
530                                  (keys (nth 2 datum)))
531                              `(,name #'(lambda (&rest temp)
532                                          (setq ,temp-var temp)
533                                          (go ,tag))
534                                      ,@keys)))
535                          data)
536               (return-from ,block-tag
537                            ,(munge-restart-case-expression expression env)))
538             ,@(mapcan (lambda (datum)
539                         (let ((tag  (nth 1 datum))
540                               (bvl  (nth 3 datum))
541                               (body (nth 4 datum)))
542                           (list tag
543                                 `(return-from ,block-tag
544                                    (apply (lambda ,bvl ,@body)
545                                           ,temp-var)))))
546                       data)))))))
547
548 (defmacro-mundanely with-simple-restart ((restart-name format-string
549                                                        &rest format-arguments)
550                                          &body forms)
551   #!+sb-doc
552   "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
553    body)
554    If restart-name is not invoked, then all values returned by forms are
555    returned. If control is transferred to this restart, it immediately
556    returns the values NIL and T."
557   `(restart-case
558        ;; If there's just one body form, then don't use PROGN. This allows
559        ;; RESTART-CASE to "see" calls to ERROR, etc.
560        ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
561      (,restart-name ()
562         :report (lambda (stream)
563                   (format stream ,format-string ,@format-arguments))
564       (values nil t))))
565
566 (defmacro-mundanely handler-bind (bindings &body forms)
567   #!+sb-doc
568   "(HANDLER-BIND ( {(type handler)}* )  body)
569    Executes body in a dynamic context where the given handler bindings are
570    in effect. Each handler must take the condition being signalled as an
571    argument. The bindings are searched first to last in the event of a
572    signalled condition."
573   (let ((member-if (member-if (lambda (x)
574                                 (not (proper-list-of-length-p x 2)))
575                               bindings)))
576     (when member-if
577       (error "ill-formed handler binding: ~S" (first member-if))))
578   `(let ((*handler-clusters*
579           (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
580                                 bindings))
581                 *handler-clusters*)))
582      (multiple-value-prog1
583          (progn
584            ,@forms)
585        ;; Wait for any float exceptions.
586        #!+x86 (float-wait))))
587
588 (defmacro-mundanely handler-case (form &rest cases)
589   "(HANDLER-CASE form
590    { (type ([var]) body) }* )
591    Execute FORM in a context with handlers established for the condition
592    types. A peculiar property allows type to be :NO-ERROR. If such a clause
593    occurs, and form returns normally, all its values are passed to this clause
594    as if by MULTIPLE-VALUE-CALL.  The :NO-ERROR clause accepts more than one
595    var specification."
596   ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
597   ;; and names for the subexpressions would make it easier to
598   ;; understand the code below.
599   (let ((no-error-clause (assoc ':no-error cases)))
600     (if no-error-clause
601         (let ((normal-return (make-symbol "normal-return"))
602               (error-return  (make-symbol "error-return")))
603           `(block ,error-return
604              (multiple-value-call (lambda ,@(cdr no-error-clause))
605                (block ,normal-return
606                  (return-from ,error-return
607                    (handler-case (return-from ,normal-return ,form)
608                      ,@(remove no-error-clause cases)))))))
609         (let ((tag (gensym))
610               (var (gensym))
611               (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
612                                        cases)))
613           `(block ,tag
614              (let ((,var nil))
615                (declare (ignorable ,var))
616                (tagbody
617                 (handler-bind
618                     ,(mapcar (lambda (annotated-case)
619                                (list (cadr annotated-case)
620                                      `(lambda (temp)
621                                         ,(if (caddr annotated-case)
622                                              `(setq ,var temp)
623                                              '(declare (ignore temp)))
624                                         (go ,(car annotated-case)))))
625                              annotated-cases)
626                   (return-from ,tag
627                     #!-x86 ,form
628                     #!+x86 (multiple-value-prog1 ,form
629                              ;; Need to catch FP errors here!
630                              (float-wait))))
631                 ,@(mapcan
632                    (lambda (annotated-case)
633                      (list (car annotated-case)
634                            (let ((body (cdddr annotated-case)))
635                              `(return-from
636                                   ,tag
637                                 ,(cond ((caddr annotated-case)
638                                         `(let ((,(caaddr annotated-case)
639                                                 ,var))
640                                            ,@body))
641                                        (t
642                                         `(locally ,@body)))))))
643                    annotated-cases))))))))
644 \f
645 ;;;; miscellaneous
646
647 (defmacro-mundanely return (&optional (value nil))
648   `(return-from nil ,value))
649
650 (defmacro-mundanely psetq (&rest pairs)
651   #!+sb-doc
652   "PSETQ {var value}*
653    Set the variables to the values, like SETQ, except that assignments
654    happen in parallel, i.e. no assignments take place until all the
655    forms have been evaluated."
656   ;; Given the possibility of symbol-macros, we delegate to PSETF
657   ;; which knows how to deal with them, after checking that syntax is
658   ;; compatible with PSETQ.
659   (do ((pair pairs (cddr pair)))
660       ((endp pair) `(psetf ,@pairs))
661     (unless (symbolp (car pair))
662       (error 'simple-program-error
663              :format-control "variable ~S in PSETQ is not a SYMBOL"
664              :format-arguments (list (car pair))))))
665
666 (defmacro-mundanely lambda (&whole whole args &body body)
667   (declare (ignore args body))
668   `#',whole)
669
670 (defmacro-mundanely named-lambda (&whole whole name args &body body)
671   (declare (ignore name args body))
672   `#',whole)
673
674 (defmacro-mundanely lambda-with-lexenv (&whole whole
675                                         declarations macros symbol-macros
676                                         &body body)
677   (declare (ignore declarations macros symbol-macros body))
678   `#',whole)
679
680 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
681 ;;; when cross-compiling.  It's not critical for behaviour, but is
682 ;;; aesthetically pleasing, except inasmuch as there's this list of
683 ;;; magic functions here.  -- CSR, 2003-04-01
684 #+sb-xc-host
685 (sb!xc:proclaim '(ftype (function * *)
686                         ;; functions appearing in fundamental defining
687                         ;; macro expansions:
688                         %compiler-deftype
689                         %compiler-defvar
690                         %defun
691                         %defsetf
692                         %defparameter
693                         %defvar
694                         sb!c:%compiler-defun
695                         sb!c::%define-symbol-macro
696                         sb!c::%defconstant
697                         sb!c::%define-compiler-macro
698                         sb!c::%defmacro
699                         sb!kernel::%compiler-defstruct
700                         sb!kernel::%compiler-define-condition
701                         sb!kernel::%defstruct
702                         sb!kernel::%define-condition
703                         ;; miscellaneous functions commonly appearing
704                         ;; as a result of macro expansions or compiler
705                         ;; transformations:
706                         sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
707                         sb!kernel::arg-count-error ; PARSE-DEFMACRO
708                         ))