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