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