1.0.0.22: Extensible sequences. (EXPERIMENTAL: Do Not Use As Food)
[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     #!+sb-eval
229     (when (typep def 'sb!eval:interpreted-function)
230       (setf (sb!eval:interpreted-function-documentation def)
231             doc)))
232   name)
233 \f
234 ;;;; DEFVAR and DEFPARAMETER
235
236 (defmacro-mundanely defvar (var &optional (val nil valp) (doc nil docp))
237   #!+sb-doc
238   "Define a global variable at top level. Declare the variable
239   SPECIAL and, optionally, initialize it. If the variable already has a
240   value, the old value is not clobbered. The third argument is an optional
241   documentation string for the variable."
242   `(progn
243      (eval-when (:compile-toplevel)
244        (%compiler-defvar ',var))
245      (eval-when (:load-toplevel :execute)
246        (%defvar ',var (unless (boundp ',var) ,val)
247                 ',valp ,doc ',docp
248                 (sb!c:source-location)))))
249
250 (defmacro-mundanely defparameter (var val &optional (doc nil docp))
251   #!+sb-doc
252   "Define a parameter that is not normally changed by the program,
253   but that may be changed without causing an error. Declare the
254   variable special and sets its value to VAL, overwriting any
255   previous value. The third argument is an optional documentation
256   string for the parameter."
257   `(progn
258      (eval-when (:compile-toplevel)
259        (%compiler-defvar ',var))
260      (eval-when (:load-toplevel :execute)
261        (%defparameter ',var ,val ,doc ',docp (sb!c:source-location)))))
262
263 (defun %compiler-defvar (var)
264   (sb!xc:proclaim `(special ,var)))
265
266 #-sb-xc-host
267 (defun %defvar (var val valp doc docp source-location)
268   (%compiler-defvar var)
269   (when valp
270     (unless (boundp var)
271       (set var val)))
272   (when docp
273     (setf (fdocumentation var 'variable) doc))
274   (sb!c:with-source-location (source-location)
275     (setf (info :source-location :variable var) source-location))
276   var)
277
278 #-sb-xc-host
279 (defun %defparameter (var val doc docp source-location)
280   (%compiler-defvar var)
281   (set var val)
282   (when docp
283     (setf (fdocumentation var 'variable) doc))
284   (sb!c:with-source-location (source-location)
285     (setf (info :source-location :variable var) source-location))
286   var)
287 \f
288 ;;;; iteration constructs
289
290 ;;; (These macros are defined in terms of a function FROB-DO-BODY which
291 ;;; is also used by SB!INT:DO-ANONYMOUS. Since these macros should not
292 ;;; be loaded on the cross-compilation host, but SB!INT:DO-ANONYMOUS
293 ;;; and FROB-DO-BODY should be, these macros can't conveniently be in
294 ;;; the same file as FROB-DO-BODY.)
295 (defmacro-mundanely do (varlist endlist &body body)
296   #!+sb-doc
297   "DO ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
298   Iteration construct. Each Var is initialized in parallel to the value of the
299   specified Init form. On subsequent iterations, the Vars are assigned the
300   value of the Step form (if any) in parallel. The Test is evaluated before
301   each evaluation of the body Forms. When the Test is true, the Exit-Forms
302   are evaluated as a PROGN, with the result being the value of the DO. A block
303   named NIL is established around the entire expansion, allowing RETURN to be
304   used as an alternate exit mechanism."
305   (frob-do-body varlist endlist body 'let 'psetq 'do nil))
306 (defmacro-mundanely do* (varlist endlist &body body)
307   #!+sb-doc
308   "DO* ({(Var [Init] [Step])}*) (Test Exit-Form*) Declaration* Form*
309   Iteration construct. Each Var is initialized sequentially (like LET*) to the
310   value of the specified Init form. On subsequent iterations, the Vars are
311   sequentially assigned the value of the Step form (if any). The Test is
312   evaluated before each evaluation of the body Forms. When the Test is true,
313   the Exit-Forms are evaluated as a PROGN, with the result being the value
314   of the DO. A block named NIL is established around the entire expansion,
315   allowing RETURN to be used as an laternate exit mechanism."
316   (frob-do-body varlist endlist body 'let* 'setq 'do* nil))
317
318 ;;; DOTIMES and DOLIST could be defined more concisely using
319 ;;; destructuring macro lambda lists or DESTRUCTURING-BIND, but then
320 ;;; it'd be tricky to use them before those things were defined.
321 ;;; They're used enough times before destructuring mechanisms are
322 ;;; defined that it looks as though it's worth just implementing them
323 ;;; ASAP, at the cost of being unable to use the standard
324 ;;; destructuring mechanisms.
325 (defmacro-mundanely dotimes ((var count &optional (result nil)) &body body)
326   (cond ((numberp count)
327          `(do ((,var 0 (1+ ,var)))
328               ((>= ,var ,count) ,result)
329             (declare (type unsigned-byte ,var))
330             ,@body))
331         (t (let ((v1 (gensym)))
332              `(do ((,var 0 (1+ ,var)) (,v1 ,count))
333                   ((>= ,var ,v1) ,result)
334                 (declare (type unsigned-byte ,var))
335                 ,@body)))))
336
337 (defmacro-mundanely dolist ((var list &optional (result nil)) &body body)
338   ;; We repeatedly bind the var instead of setting it so that we never
339   ;; have to give the var an arbitrary value such as NIL (which might
340   ;; conflict with a declaration). If there is a result form, we
341   ;; introduce a gratuitous binding of the variable to NIL without the
342   ;; declarations, then evaluate the result form in that
343   ;; environment. We spuriously reference the gratuitous variable,
344   ;; since we don't want to use IGNORABLE on what might be a special
345   ;; var.
346   (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
347     (let ((n-list (gensym "N-LIST"))
348           (start (gensym "START")))
349       `(block nil
350          (let ((,n-list ,list))
351            (tagbody
352               ,start
353               (unless (endp ,n-list)
354                 (let ((,var (car ,n-list)))
355                   ,@decls
356                   (setq ,n-list (cdr ,n-list))
357                   (tagbody ,@forms))
358                 (go ,start))))
359          ,(if result
360               `(let ((,var nil))
361                  ;; Filter out TYPE declarations (VAR gets bound to NIL,
362                  ;; and might have a conflicting type declaration) and
363                  ;; IGNORE (VAR might be ignored in the loop body, but
364                  ;; it's used in the result form).
365                  ,@(filter-dolist-declarations decls)
366                  ,var
367                  ,result)
368                nil)))))
369 \f
370 ;;;; conditions, handlers, restarts
371
372 ;;; KLUDGE: we PROCLAIM these special here so that we can use restart
373 ;;; macros in the compiler before the DEFVARs are compiled.
374 (sb!xc:proclaim
375  '(special *handler-clusters* *restart-clusters* *condition-restarts*))
376
377 (defmacro-mundanely with-condition-restarts
378     (condition-form restarts-form &body body)
379   #!+sb-doc
380   "Evaluates the BODY in a dynamic environment where the restarts in the list
381    RESTARTS-FORM are associated with the condition returned by CONDITION-FORM.
382    This allows FIND-RESTART, etc., to recognize restarts that are not related
383    to the error currently being debugged. See also RESTART-CASE."
384   (let ((n-cond (gensym)))
385     `(let ((*condition-restarts*
386             (cons (let ((,n-cond ,condition-form))
387                     (cons ,n-cond
388                           (append ,restarts-form
389                                   (cdr (assoc ,n-cond *condition-restarts*)))))
390                   *condition-restarts*)))
391        ,@body)))
392
393 (defmacro-mundanely restart-bind (bindings &body forms)
394   #!+sb-doc
395   "Executes forms in a dynamic context where the given restart bindings are
396    in effect. Users probably want to use RESTART-CASE. When clauses contain
397    the same restart name, FIND-RESTART will find the first such clause."
398   `(let ((*restart-clusters*
399           (cons (list
400                  ,@(mapcar (lambda (binding)
401                              (unless (or (car binding)
402                                          (member :report-function
403                                                  binding
404                                                  :test #'eq))
405                                (warn "Unnamed restart does not have a ~
406                                       report function: ~S"
407                                      binding))
408                              `(make-restart :name ',(car binding)
409                                             :function ,(cadr binding)
410                                             ,@(cddr binding)))
411                            bindings))
412                 *restart-clusters*)))
413      ,@forms))
414
415 ;;; Wrap the RESTART-CASE expression in a WITH-CONDITION-RESTARTS if
416 ;;; appropriate. Gross, but it's what the book seems to say...
417 (defun munge-restart-case-expression (expression env)
418   (let ((exp (sb!xc:macroexpand expression env)))
419     (if (consp exp)
420         (let* ((name (car exp))
421                (args (if (eq name 'cerror) (cddr exp) (cdr exp))))
422           (if (member name '(signal error cerror warn))
423               (once-only ((n-cond `(coerce-to-condition
424                                     ,(first args)
425                                     (list ,@(rest args))
426                                     ',(case name
427                                         (warn 'simple-warning)
428                                         (signal 'simple-condition)
429                                         (t 'simple-error))
430                                     ',name)))
431                 `(with-condition-restarts
432                      ,n-cond
433                      (car *restart-clusters*)
434                    ,(if (eq name 'cerror)
435                         `(cerror ,(second exp) ,n-cond)
436                         `(,name ,n-cond))))
437               expression))
438         expression)))
439
440 ;;; FIXME: I did a fair amount of rearrangement of this code in order to
441 ;;; get WITH-KEYWORD-PAIRS to work cleanly. This code should be tested..
442 (defmacro-mundanely restart-case (expression &body clauses &environment env)
443   #!+sb-doc
444   "(RESTART-CASE form
445    {(case-name arg-list {keyword value}* body)}*)
446    The form is evaluated in a dynamic context where the clauses have special
447    meanings as points to which control may be transferred (see INVOKE-RESTART).
448    When clauses contain the same case-name, FIND-RESTART will find the first
449    such clause. If Expression is a call to SIGNAL, ERROR, CERROR or WARN (or
450    macroexpands into such) then the signalled condition will be associated with
451    the new restarts."
452   (flet ((transform-keywords (&key report interactive test)
453            (let ((result '()))
454              (when report
455                (setq result (list* (if (stringp report)
456                                        `#'(lambda (stream)
457                                             (write-string ,report stream))
458                                        `#',report)
459                                    :report-function
460                                    result)))
461              (when interactive
462                (setq result (list* `#',interactive
463                                    :interactive-function
464                                    result)))
465              (when test
466                (setq result (list* `#',test :test-function result)))
467              (nreverse result)))
468          (parse-keyword-pairs (list keys)
469            (do ((l list (cddr l))
470                 (k '() (list* (cadr l) (car l) k)))
471                ((or (null l) (not (member (car l) keys)))
472                 (values (nreverse k) l)))))
473     (let ((block-tag (gensym))
474           (temp-var (gensym))
475           (data
476            (macrolet (;; KLUDGE: This started as an old DEFMACRO
477                       ;; WITH-KEYWORD-PAIRS general utility, which was used
478                       ;; only in this one place in the code. It was translated
479                       ;; literally into this MACROLET in order to avoid some
480                       ;; cross-compilation bootstrap problems. It would almost
481                       ;; certainly be clearer, and it would certainly be more
482                       ;; concise, to do a more idiomatic translation, merging
483                       ;; this with the TRANSFORM-KEYWORDS logic above.
484                       ;;   -- WHN 19990925
485                       (with-keyword-pairs ((names expression) &body forms)
486                         (let ((temp (member '&rest names)))
487                           (unless (= (length temp) 2)
488                             (error "&REST keyword is ~:[missing~;misplaced~]."
489                                    temp))
490                           (let* ((key-vars (ldiff names temp))
491                                  (keywords (mapcar #'keywordicate key-vars))
492                                  (key-var (gensym))
493                                  (rest-var (cadr temp)))
494                             `(multiple-value-bind (,key-var ,rest-var)
495                                  (parse-keyword-pairs ,expression ',keywords)
496                                (let ,(mapcar (lambda (var keyword)
497                                                `(,var (getf ,key-var
498                                                             ,keyword)))
499                                              key-vars keywords)
500                                  ,@forms))))))
501              (mapcar (lambda (clause)
502                        (with-keyword-pairs ((report interactive test
503                                                     &rest forms)
504                                             (cddr clause))
505                          (list (car clause) ;name=0
506                                (gensym) ;tag=1
507                                (transform-keywords :report report ;keywords=2
508                                                    :interactive interactive
509                                                    :test test)
510                                (cadr clause) ;bvl=3
511                                forms))) ;body=4
512                    clauses))))
513       `(block ,block-tag
514          (let ((,temp-var nil))
515            (tagbody
516             (restart-bind
517                 ,(mapcar (lambda (datum)
518                            (let ((name (nth 0 datum))
519                                  (tag  (nth 1 datum))
520                                  (keys (nth 2 datum)))
521                              `(,name #'(lambda (&rest temp)
522                                          (setq ,temp-var temp)
523                                          (go ,tag))
524                                      ,@keys)))
525                          data)
526               (return-from ,block-tag
527                            ,(munge-restart-case-expression expression env)))
528             ,@(mapcan (lambda (datum)
529                         (let ((tag  (nth 1 datum))
530                               (bvl  (nth 3 datum))
531                               (body (nth 4 datum)))
532                           (list tag
533                                 `(return-from ,block-tag
534                                    (apply (lambda ,bvl ,@body)
535                                           ,temp-var)))))
536                       data)))))))
537
538 (defmacro-mundanely with-simple-restart ((restart-name format-string
539                                                        &rest format-arguments)
540                                          &body forms)
541   #!+sb-doc
542   "(WITH-SIMPLE-RESTART (restart-name format-string format-arguments)
543    body)
544    If restart-name is not invoked, then all values returned by forms are
545    returned. If control is transferred to this restart, it immediately
546    returns the values NIL and T."
547   `(restart-case
548        ;; If there's just one body form, then don't use PROGN. This allows
549        ;; RESTART-CASE to "see" calls to ERROR, etc.
550        ,(if (= (length forms) 1) (car forms) `(progn ,@forms))
551      (,restart-name ()
552         :report (lambda (stream)
553                   (format stream ,format-string ,@format-arguments))
554       (values nil t))))
555
556 (defmacro-mundanely handler-bind (bindings &body forms)
557   #!+sb-doc
558   "(HANDLER-BIND ( {(type handler)}* )  body)
559    Executes body in a dynamic context where the given handler bindings are
560    in effect. Each handler must take the condition being signalled as an
561    argument. The bindings are searched first to last in the event of a
562    signalled condition."
563   (let ((member-if (member-if (lambda (x)
564                                 (not (proper-list-of-length-p x 2)))
565                               bindings)))
566     (when member-if
567       (error "ill-formed handler binding: ~S" (first member-if))))
568   `(let ((*handler-clusters*
569           (cons (list ,@(mapcar (lambda (x) `(cons ',(car x) ,(cadr x)))
570                                 bindings))
571                 *handler-clusters*)))
572      (multiple-value-prog1
573          (progn
574            ,@forms)
575        ;; Wait for any float exceptions.
576        #!+x86 (float-wait))))
577
578 (defmacro-mundanely handler-case (form &rest cases)
579   "(HANDLER-CASE form
580    { (type ([var]) body) }* )
581    Execute FORM in a context with handlers established for the condition
582    types. A peculiar property allows type to be :NO-ERROR. If such a clause
583    occurs, and form returns normally, all its values are passed to this clause
584    as if by MULTIPLE-VALUE-CALL.  The :NO-ERROR clause accepts more than one
585    var specification."
586   ;; FIXME: Replacing CADR, CDDDR and friends with DESTRUCTURING-BIND
587   ;; and names for the subexpressions would make it easier to
588   ;; understand the code below.
589   (let ((no-error-clause (assoc ':no-error cases)))
590     (if no-error-clause
591         (let ((normal-return (make-symbol "normal-return"))
592               (error-return  (make-symbol "error-return")))
593           `(block ,error-return
594              (multiple-value-call (lambda ,@(cdr no-error-clause))
595                (block ,normal-return
596                  (return-from ,error-return
597                    (handler-case (return-from ,normal-return ,form)
598                      ,@(remove no-error-clause cases)))))))
599         (let ((tag (gensym))
600               (var (gensym))
601               (annotated-cases (mapcar (lambda (case) (cons (gensym) case))
602                                        cases)))
603           `(block ,tag
604              (let ((,var nil))
605                (declare (ignorable ,var))
606                (tagbody
607                 (handler-bind
608                     ,(mapcar (lambda (annotated-case)
609                                (list (cadr annotated-case)
610                                      `(lambda (temp)
611                                         ,(if (caddr annotated-case)
612                                              `(setq ,var temp)
613                                              '(declare (ignore temp)))
614                                         (go ,(car annotated-case)))))
615                              annotated-cases)
616                   (return-from ,tag
617                     #!-x86 ,form
618                     #!+x86 (multiple-value-prog1 ,form
619                              ;; Need to catch FP errors here!
620                              (float-wait))))
621                 ,@(mapcan
622                    (lambda (annotated-case)
623                      (list (car annotated-case)
624                            (let ((body (cdddr annotated-case)))
625                              `(return-from
626                                   ,tag
627                                 ,(cond ((caddr annotated-case)
628                                         `(let ((,(caaddr annotated-case)
629                                                 ,var))
630                                            ,@body))
631                                        (t
632                                         `(locally ,@body)))))))
633                    annotated-cases))))))))
634 \f
635 ;;;; miscellaneous
636
637 (defmacro-mundanely return (&optional (value nil))
638   `(return-from nil ,value))
639
640 (defmacro-mundanely psetq (&rest pairs)
641   #!+sb-doc
642   "PSETQ {var value}*
643    Set the variables to the values, like SETQ, except that assignments
644    happen in parallel, i.e. no assignments take place until all the
645    forms have been evaluated."
646   ;; Given the possibility of symbol-macros, we delegate to PSETF
647   ;; which knows how to deal with them, after checking that syntax is
648   ;; compatible with PSETQ.
649   (do ((pair pairs (cddr pair)))
650       ((endp pair) `(psetf ,@pairs))
651     (unless (symbolp (car pair))
652       (error 'simple-program-error
653              :format-control "variable ~S in PSETQ is not a SYMBOL"
654              :format-arguments (list (car pair))))))
655
656 (defmacro-mundanely lambda (&whole whole args &body body)
657   (declare (ignore args body))
658   `#',whole)
659
660 (defmacro-mundanely named-lambda (&whole whole name args &body body)
661   (declare (ignore name args body))
662   `#',whole)
663
664 (defmacro-mundanely lambda-with-lexenv (&whole whole
665                                         declarations macros symbol-macros
666                                         &body body)
667   (declare (ignore declarations macros symbol-macros body))
668   `#',whole)
669
670 ;;; this eliminates a whole bundle of unknown function STYLE-WARNINGs
671 ;;; when cross-compiling.  It's not critical for behaviour, but is
672 ;;; aesthetically pleasing, except inasmuch as there's this list of
673 ;;; magic functions here.  -- CSR, 2003-04-01
674 #+sb-xc-host
675 (sb!xc:proclaim '(ftype (function * *)
676                         ;; functions appearing in fundamental defining
677                         ;; macro expansions:
678                         %compiler-deftype
679                         %compiler-defvar
680                         %defun
681                         %defsetf
682                         %defparameter
683                         %defvar
684                         sb!c:%compiler-defun
685                         sb!c::%define-symbol-macro
686                         sb!c::%defconstant
687                         sb!c::%define-compiler-macro
688                         sb!c::%defmacro
689                         sb!kernel::%compiler-defstruct
690                         sb!kernel::%compiler-define-condition
691                         sb!kernel::%defstruct
692                         sb!kernel::%define-condition
693                         ;; miscellaneous functions commonly appearing
694                         ;; as a result of macro expansions or compiler
695                         ;; transformations:
696                         sb!int:find-undeleted-package-or-lose ; IN-PACKAGE
697                         sb!kernel::arg-count-error ; PARSE-DEFMACRO
698                         ))