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