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