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