0.9.1.38:
[sbcl.git] / src / code / condition.lisp
1 ;;;; stuff originally from CMU CL's error.lisp which can or should
2 ;;;; come late (mostly related to the CONDITION class itself)
3 ;;;;
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15 \f
16 ;;;; miscellaneous support utilities
17
18 ;;; Signalling an error when trying to print an error condition is
19 ;;; generally a PITA, so whatever the failure encountered when
20 ;;; wondering about FILE-POSITION within a condition printer, 'tis
21 ;;; better silently to give up than to try to complain. 
22 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
23   ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
24   ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
25   ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
26   ;; has been closed so that FILE-POSITION is a nonsense question. So
27   ;; my (WHN) impression is that the conservative approach is to
28   ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
29   ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
30   ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
31   ;; time an error was reported.)
32   (if posp
33       (ignore-errors (file-position stream pos))
34       (ignore-errors (file-position stream))))
35 \f
36 ;;;; the CONDITION class
37
38 (/show0 "condition.lisp 20")
39
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41
42 (/show0 "condition.lisp 24")
43
44 (def!struct (condition-classoid (:include slot-classoid)
45                                 (:constructor make-condition-classoid))
46   ;; list of CONDITION-SLOT structures for the direct slots of this
47   ;; class
48   (slots nil :type list)
49   ;; list of CONDITION-SLOT structures for all of the effective class
50   ;; slots of this class
51   (class-slots nil :type list)
52   ;; report function or NIL
53   (report nil :type (or function null))
54   ;; list of alternating initargs and initforms
55   (default-initargs () :type list)
56   ;; class precedence list as a list of CLASS objects, with all
57   ;; non-CONDITION classes removed
58   (cpl () :type list)
59   ;; a list of all the effective instance allocation slots of this
60   ;; class that have a non-constant initform or default-initarg.
61   ;; Values for these slots must be computed in the dynamic
62   ;; environment of MAKE-CONDITION.
63   (hairy-slots nil :type list))
64
65 (/show0 "condition.lisp 49")
66
67 ) ; EVAL-WHEN
68
69 (!defstruct-with-alternate-metaclass condition
70   :slot-names (actual-initargs assigned-slots)
71   :boa-constructor %make-condition-object
72   :superclass-name instance
73   :metaclass-name condition-classoid
74   :metaclass-constructor make-condition-classoid
75   :dd-type structure)
76
77 (defun make-condition-object (actual-initargs)
78   (%make-condition-object actual-initargs nil))
79
80 (defstruct (condition-slot (:copier nil))
81   (name (missing-arg) :type symbol)
82   ;; list of all applicable initargs
83   (initargs (missing-arg) :type list)
84   ;; names of reader and writer functions
85   (readers (missing-arg) :type list)
86   (writers (missing-arg) :type list)
87   ;; true if :INITFORM was specified
88   (initform-p (missing-arg) :type (member t nil))
89   ;; If this is a function, call it with no args. Otherwise, it's the
90   ;; actual value.
91   (initform (missing-arg) :type t)
92   ;; allocation of this slot, or NIL until defaulted
93   (allocation nil :type (member :instance :class nil))
94   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
95   (cell nil :type (or cons null))
96   ;; slot documentation
97   (documentation nil :type (or string null)))
98
99 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
100 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
101 ;;; have themselves listed in their CPLs. This behavior is inherited
102 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
103 ;;; figured out whether it's right. -- WHN 19990612
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105   (/show0 "condition.lisp 103")
106   (let ((condition-class (locally
107                            ;; KLUDGE: There's a DEFTRANSFORM
108                            ;; FIND-CLASSOID for constant class names
109                            ;; which creates fast but
110                            ;; non-cold-loadable, non-compact code. In
111                            ;; this context, we'd rather have compact,
112                            ;; cold-loadable code. -- WHN 19990928
113                            (declare (notinline find-classoid))
114                            (find-classoid 'condition))))
115     (setf (condition-classoid-cpl condition-class)
116           (list condition-class)))
117   (/show0 "condition.lisp 103"))
118
119 (setf (condition-classoid-report (locally
120                                    ;; KLUDGE: There's a DEFTRANSFORM
121                                    ;; FIND-CLASSOID for constant class
122                                    ;; names which creates fast but
123                                    ;; non-cold-loadable, non-compact
124                                    ;; code. In this context, we'd
125                                    ;; rather have compact,
126                                    ;; cold-loadable code. -- WHN
127                                    ;; 19990928
128                                    (declare (notinline find-classoid))
129                                    (find-classoid 'condition)))
130       (lambda (cond stream)
131         (format stream "Condition ~S was signalled." (type-of cond))))
132
133 (eval-when (:compile-toplevel :load-toplevel :execute)
134
135 (defun find-condition-layout (name parent-types)
136   (let* ((cpl (remove-duplicates
137                (reverse
138                 (reduce #'append
139                         (mapcar (lambda (x)
140                                   (condition-classoid-cpl
141                                    (find-classoid x)))
142                                 parent-types)))))
143          (cond-layout (info :type :compiler-layout 'condition))
144          (olayout (info :type :compiler-layout name))
145          ;; FIXME: Does this do the right thing in case of multiple
146          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
147          ;; it obvious what ANSI intends to be done in the case of
148          ;; multiple inheritance, so it's not actually clear what the
149          ;; right thing is..
150          (new-inherits
151           (order-layout-inherits (concatenate 'simple-vector
152                                               (layout-inherits cond-layout)
153                                               (mapcar #'classoid-layout cpl)))))
154     (if (and olayout
155              (not (mismatch (layout-inherits olayout) new-inherits)))
156         olayout
157         (make-layout :classoid (make-undefined-classoid name)
158                      :inherits new-inherits
159                      :depthoid -1
160                      :length (layout-length cond-layout)))))
161
162 ) ; EVAL-WHEN
163
164 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
165 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
166 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
167 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
168 ;;;   a condition type C is equivalent to:
169 ;;;     (defmethod print-object ((x c) stream)
170 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
171 ;;; The current code doesn't seem to quite match that.
172 (def!method print-object ((x condition) stream)
173   (if *print-escape*
174       (print-unreadable-object (x stream :type t :identity t))
175       ;; KLUDGE: A comment from CMU CL here said
176       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
177       ;;   superclasses in define-condition call!
178       (dolist (class (condition-classoid-cpl (classoid-of x))
179                      (error "no REPORT? shouldn't happen!"))
180         (let ((report (condition-classoid-report class)))
181           (when report
182             (return (funcall report x stream)))))))
183 \f
184 ;;;; slots of CONDITION objects
185
186 (defvar *empty-condition-slot* '(empty))
187
188 (defun find-slot-default (class slot)
189   (let ((initargs (condition-slot-initargs slot))
190         (cpl (condition-classoid-cpl class)))
191     (dolist (class cpl)
192       (let ((default-initargs (condition-classoid-default-initargs class)))
193         (dolist (initarg initargs)
194           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
195             (unless (eq val *empty-condition-slot*)
196               (return-from find-slot-default
197                            (if (functionp val)
198                                (funcall val)
199                                val)))))))
200
201     (if (condition-slot-initform-p slot)
202         (let ((initform (condition-slot-initform slot)))
203           (if (functionp initform)
204               (funcall initform)
205               initform))
206         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
207
208 (defun find-condition-class-slot (condition-class slot-name)
209   (dolist (sclass
210            (condition-classoid-cpl condition-class)
211            (error "There is no slot named ~S in ~S."
212                   slot-name condition-class))
213     (dolist (slot (condition-classoid-slots sclass))
214       (when (eq (condition-slot-name slot) slot-name)
215         (return-from find-condition-class-slot slot)))))
216
217 (defun condition-writer-function (condition new-value name)
218   (dolist (cslot (condition-classoid-class-slots
219                   (layout-classoid (%instance-layout condition)))
220                  (setf (getf (condition-assigned-slots condition) name)
221                        new-value))
222     (when (eq (condition-slot-name cslot) name)
223       (return (setf (car (condition-slot-cell cslot)) new-value)))))
224
225 (defun condition-reader-function (condition name)
226   (let ((class (layout-classoid (%instance-layout condition))))
227     (dolist (cslot (condition-classoid-class-slots class))
228       (when (eq (condition-slot-name cslot) name)
229         (return-from condition-reader-function
230                      (car (condition-slot-cell cslot)))))
231     (let ((val (getf (condition-assigned-slots condition) name
232                      *empty-condition-slot*)))
233       (if (eq val *empty-condition-slot*)
234           (let ((actual-initargs (condition-actual-initargs condition))
235                 (slot (find-condition-class-slot class name)))
236             (unless slot
237               (error "missing slot ~S of ~S" name condition))
238             (do ((initargs actual-initargs (cddr initargs)))
239                 ((endp initargs)
240                  (setf (getf (condition-assigned-slots condition) name)
241                        (find-slot-default class slot)))
242               (when (member (car initargs) (condition-slot-initargs slot))
243                 (return-from condition-reader-function
244                   (setf (getf (condition-assigned-slots condition)
245                               name)
246                         (cadr initargs))))))
247           val))))
248 \f
249 ;;;; MAKE-CONDITION
250
251 (defun make-condition (thing &rest args)
252   #!+sb-doc
253   "Make an instance of a condition object using the specified initargs."
254   ;; Note: ANSI specifies no exceptional situations in this function.
255   ;; signalling simple-type-error would not be wrong.
256   (let* ((thing (or (and (symbolp thing) (find-classoid thing nil))
257                     thing))
258          (class (typecase thing
259                   (condition-classoid thing)
260                   (classoid
261                    (error 'simple-type-error
262                           :datum thing
263                           :expected-type 'condition-class
264                           :format-control "~S is not a condition class."
265                           :format-arguments (list thing)))
266                   (t
267                    (error 'simple-type-error
268                           :datum thing
269                           :expected-type 'condition-class
270                           :format-control "bad thing for class argument:~%  ~S"
271                           :format-arguments (list thing)))))
272          (res (make-condition-object args)))
273     (setf (%instance-layout res) (classoid-layout class))
274     ;; Set any class slots with initargs present in this call.
275     (dolist (cslot (condition-classoid-class-slots class))
276       (dolist (initarg (condition-slot-initargs cslot))
277         (let ((val (getf args initarg *empty-condition-slot*)))
278           (unless (eq val *empty-condition-slot*)
279             (setf (car (condition-slot-cell cslot)) val)))))
280     ;; Default any slots with non-constant defaults now.
281     (dolist (hslot (condition-classoid-hairy-slots class))
282       (when (dolist (initarg (condition-slot-initargs hslot) t)
283               (unless (eq (getf args initarg *empty-condition-slot*)
284                           *empty-condition-slot*)
285                 (return nil)))
286         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
287               (find-slot-default class hslot))))
288     res))
289 \f
290 ;;;; DEFINE-CONDITION
291
292 (eval-when (:compile-toplevel :load-toplevel :execute)
293 (defun %compiler-define-condition (name direct-supers layout
294                                    all-readers all-writers)
295   (with-single-package-locked-error 
296       (:symbol name "defining ~A as a condition")
297     (sb!xc:proclaim `(ftype (function (t) t) ,@all-readers))
298     (sb!xc:proclaim `(ftype (function (t t) t) ,@all-writers))
299     (multiple-value-bind (class old-layout)
300         (insured-find-classoid name
301                                #'condition-classoid-p
302                                #'make-condition-classoid)
303       (setf (layout-classoid layout) class)
304       (setf (classoid-direct-superclasses class)
305             (mapcar #'find-classoid direct-supers))
306       (cond ((not old-layout)
307              (register-layout layout))
308             ((not *type-system-initialized*)
309              (setf (layout-classoid old-layout) class)
310              (setq layout old-layout)
311              (unless (eq (classoid-layout class) layout)
312                (register-layout layout)))
313             ((redefine-layout-warning "current"
314                                       old-layout
315                                       "new"
316                                       (layout-length layout)
317                                       (layout-inherits layout)
318                                       (layout-depthoid layout)
319                                       (layout-n-untagged-slots layout))
320              (register-layout layout :invalidate t))
321             ((not (classoid-layout class))
322              (register-layout layout)))
323       
324       (setf (layout-info layout)
325             (locally
326                 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
327                 ;; names which creates fast but non-cold-loadable, non-compact
328                 ;; code. In this context, we'd rather have compact, cold-loadable
329                 ;; code. -- WHN 19990928
330                 (declare (notinline find-classoid))
331               (layout-info (classoid-layout (find-classoid 'condition)))))
332       
333       (setf (find-classoid name) class)
334       
335       ;; Initialize CPL slot.
336       (setf (condition-classoid-cpl class)
337             (remove-if-not #'condition-classoid-p 
338                            (std-compute-class-precedence-list class)))))
339   (values))
340 ) ; EVAL-WHEN
341
342 ;;; Compute the effective slots of CLASS, copying inherited slots and
343 ;;; destructively modifying direct slots.
344 ;;;
345 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
346 ;;; direct slots. Presumably it follows from the semantics of
347 ;;; inheritance and redefinition of conditions, but finding the cite
348 ;;; and documenting it here would be good. (Or, if this is not in fact
349 ;;; ANSI-compliant, fixing it would also be good.:-)
350 (defun compute-effective-slots (class)
351   (collect ((res (copy-list (condition-classoid-slots class))))
352     (dolist (sclass (cdr (condition-classoid-cpl class)))
353       (dolist (sslot (condition-classoid-slots sclass))
354         (let ((found (find (condition-slot-name sslot) (res)
355                            :key #'condition-slot-name)))
356           (cond (found
357                  (setf (condition-slot-initargs found)
358                        (union (condition-slot-initargs found)
359                               (condition-slot-initargs sslot)))
360                  (unless (condition-slot-initform-p found)
361                    (setf (condition-slot-initform-p found)
362                          (condition-slot-initform-p sslot))
363                    (setf (condition-slot-initform found)
364                          (condition-slot-initform sslot)))
365                  (unless (condition-slot-allocation found)
366                    (setf (condition-slot-allocation found)
367                          (condition-slot-allocation sslot))))
368                 (t
369                  (res (copy-structure sslot)))))))
370     (res)))
371
372 ;;; Early definitions of slot accessor creators.
373 ;;;
374 ;;; Slot accessors must be generic functions, but ANSI does not seem
375 ;;; to specify any of them, and we cannot support it before end of
376 ;;; warm init. So we use ordinary functions inside SBCL, and switch to
377 ;;; GFs only at the end of building.
378 (declaim (notinline install-condition-slot-reader
379                     install-condition-slot-writer))
380 (defun install-condition-slot-reader (name condition slot-name)
381   (declare (ignore condition))
382   (setf (fdefinition name)
383         (lambda (condition)
384           (condition-reader-function condition slot-name))))
385 (defun install-condition-slot-writer (name condition slot-name)
386   (declare (ignore condition))
387   (setf (fdefinition name)
388         (lambda (new-value condition)
389           (condition-writer-function condition new-value slot-name))))
390
391 (defun %define-condition (name parent-types layout slots documentation
392                           report default-initargs all-readers all-writers)
393   (with-single-package-locked-error 
394       (:symbol name "defining ~A as a condition")
395     (%compiler-define-condition name parent-types layout all-readers all-writers)
396     (let ((class (find-classoid name)))
397       (setf (condition-classoid-slots class) slots)
398       (setf (condition-classoid-report class) report)
399       (setf (condition-classoid-default-initargs class) default-initargs)
400       (setf (fdocumentation name 'type) documentation)
401       
402       (dolist (slot slots)
403         
404         ;; Set up reader and writer functions.
405         (let ((slot-name (condition-slot-name slot)))
406           (dolist (reader (condition-slot-readers slot))
407             (install-condition-slot-reader reader name slot-name))
408           (dolist (writer (condition-slot-writers slot))
409             (install-condition-slot-writer writer name slot-name))))
410       
411       ;; Compute effective slots and set up the class and hairy slots
412       ;; (subsets of the effective slots.)
413       (let ((eslots (compute-effective-slots class))
414             (e-def-initargs
415              (reduce #'append
416                      (mapcar #'condition-classoid-default-initargs
417                            (condition-classoid-cpl class)))))
418         (dolist (slot eslots)
419           (ecase (condition-slot-allocation slot)
420             (:class
421              (unless (condition-slot-cell slot)
422                (setf (condition-slot-cell slot)
423                      (list (if (condition-slot-initform-p slot)
424                                (let ((initform (condition-slot-initform slot)))
425                                  (if (functionp initform)
426                                      (funcall initform)
427                                      initform))
428                                *empty-condition-slot*))))
429              (push slot (condition-classoid-class-slots class)))
430             ((:instance nil)
431              (setf (condition-slot-allocation slot) :instance)
432              (when (or (functionp (condition-slot-initform slot))
433                        (dolist (initarg (condition-slot-initargs slot) nil)
434                          (when (functionp (getf e-def-initargs initarg))
435                            (return t))))
436                (push slot (condition-classoid-hairy-slots class))))))))
437     name))
438
439 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
440                                  &body options)
441   #!+sb-doc
442   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
443    Define NAME as a condition type. This new type inherits slots and its
444    report function from the specified PARENT-TYPEs. A slot spec is a list of:
445      (slot-name :reader <rname> :initarg <iname> {Option Value}*
446
447    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
448    and :TYPE and the overall options :DEFAULT-INITARGS and
449    [type] :DOCUMENTATION are also allowed.
450
451    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
452    a string or a two-argument lambda or function name. If a function, the
453    function is called with the condition and stream to report the condition.
454    If a string, the string is printed.
455
456    Condition types are classes, but (as allowed by ANSI and not as described in
457    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
458    SLOT-VALUE may not be used on condition objects."
459   (let* ((parent-types (or parent-types '(condition)))
460          (layout (find-condition-layout name parent-types))
461          (documentation nil)
462          (report nil)
463          (default-initargs ()))
464     (collect ((slots)
465               (all-readers nil append)
466               (all-writers nil append))
467       (dolist (spec slot-specs)
468         (when (keywordp spec)
469           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
470                 spec))
471         (let* ((spec (if (consp spec) spec (list spec)))
472                (slot-name (first spec))
473                (allocation :instance)
474                (initform-p nil)
475                documentation
476                initform)
477           (collect ((initargs)
478                     (readers)
479                     (writers))
480             (do ((options (rest spec) (cddr options)))
481                 ((null options))
482               (unless (and (consp options) (consp (cdr options)))
483                 (error "malformed condition slot spec:~%  ~S." spec))
484               (let ((arg (second options)))
485                 (case (first options)
486                   (:reader (readers arg))
487                   (:writer (writers arg))
488                   (:accessor
489                    (readers arg)
490                    (writers `(setf ,arg)))
491                   (:initform
492                    (when initform-p
493                      (error "more than one :INITFORM in ~S" spec))
494                    (setq initform-p t)
495                    (setq initform arg))
496                   (:initarg (initargs arg))
497                   (:allocation
498                    (setq allocation arg))
499                   (:documentation
500                    (when documentation
501                      (error "more than one :DOCUMENTATION in ~S" spec))
502                    (unless (stringp arg)
503                      (error "slot :DOCUMENTATION argument is not a string: ~S"
504                             arg))
505                    (setq documentation arg))
506                   (:type)
507                   (t
508                    (error "unknown slot option:~%  ~S" (first options))))))
509
510             (all-readers (readers))
511             (all-writers (writers))
512             (slots `(make-condition-slot
513                      :name ',slot-name
514                      :initargs ',(initargs)
515                      :readers ',(readers)
516                      :writers ',(writers)
517                      :initform-p ',initform-p
518                      :documentation ',documentation
519                      :initform
520                      ,(if (constantp initform)
521                           `',(eval initform)
522                           `#'(lambda () ,initform)))))))
523
524       (dolist (option options)
525         (unless (consp option)
526           (error "bad option:~%  ~S" option))
527         (case (first option)
528           (:documentation (setq documentation (second option)))
529           (:report
530            (let ((arg (second option)))
531              (setq report
532                    (if (stringp arg)
533                        `#'(lambda (condition stream)
534                           (declare (ignore condition))
535                           (write-string ,arg stream))
536                        `#'(lambda (condition stream)
537                           (funcall #',arg condition stream))))))
538           (:default-initargs
539            (do ((initargs (rest option) (cddr initargs)))
540                ((endp initargs))
541              (let ((val (second initargs)))
542                (setq default-initargs
543                      (list* `',(first initargs)
544                             (if (constantp val)
545                                 `',(eval val)
546                                 `#'(lambda () ,val))
547                             default-initargs)))))
548           (t
549            (error "unknown option: ~S" (first option)))))
550
551       `(progn
552          (eval-when (:compile-toplevel)
553            (%compiler-define-condition ',name ',parent-types ',layout
554                                        ',(all-readers) ',(all-writers)))
555          (eval-when (:load-toplevel :execute)
556            (%define-condition ',name
557                               ',parent-types
558                               ',layout
559                               (list ,@(slots))
560                               ,documentation
561                               ,report
562                               (list ,@default-initargs)
563                               ',(all-readers)
564                               ',(all-writers)))))))
565 \f
566 ;;;; DESCRIBE on CONDITIONs
567
568 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
569 ;;; eventually (once we get CLOS up and running so that we can define
570 ;;; methods)
571 (defun describe-condition (condition stream)
572   (format stream
573           "~&~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>~%"
574           condition
575           (type-of condition)
576           (concatenate 'list
577                        (condition-actual-initargs condition)
578                        (condition-assigned-slots condition))))
579 \f
580 ;;;; various CONDITIONs specified by ANSI
581
582 (define-condition serious-condition (condition) ())
583
584 (define-condition error (serious-condition) ())
585
586 (define-condition warning (condition) ())
587 (define-condition style-warning (warning) ())
588
589 (defun simple-condition-printer (condition stream)
590   (apply #'format
591          stream
592          (simple-condition-format-control condition)
593          (simple-condition-format-arguments condition)))
594
595 (define-condition simple-condition ()
596   ((format-control :reader simple-condition-format-control
597                    :initarg :format-control
598                    :type format-control)
599    (format-arguments :reader simple-condition-format-arguments
600                      :initarg :format-arguments
601                      :initform '()
602                      :type list))
603   (:report simple-condition-printer))
604
605 (define-condition simple-warning (simple-condition warning) ())
606
607 (define-condition simple-error (simple-condition error) ())
608
609 ;;; not specified by ANSI, but too useful not to have around.
610 (define-condition simple-style-warning (simple-condition style-warning) ())
611
612 (define-condition storage-condition (serious-condition) ())
613
614 (define-condition type-error (error)
615   ((datum :reader type-error-datum :initarg :datum)
616    (expected-type :reader type-error-expected-type :initarg :expected-type))
617   (:report
618    (lambda (condition stream)
619      (format stream
620              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
621              (type-error-datum condition)
622              (type-error-expected-type condition)))))
623
624 (define-condition simple-type-error (simple-condition type-error) ())
625
626 (define-condition program-error (error) ())
627 (define-condition parse-error   (error) ())
628 (define-condition control-error (error) ())
629 (define-condition stream-error  (error)
630   ((stream :reader stream-error-stream :initarg :stream)))
631
632 (define-condition end-of-file (stream-error) ()
633   (:report
634    (lambda (condition stream)
635      (format stream
636              "end of file on ~S"
637              (stream-error-stream condition)))))
638
639 (define-condition file-error (error)
640   ((pathname :reader file-error-pathname :initarg :pathname))
641   (:report
642    (lambda (condition stream)
643      (format stream "error on file ~S" (file-error-pathname condition)))))
644
645 (define-condition package-error (error)
646   ((package :reader package-error-package :initarg :package)))
647
648 (define-condition cell-error (error)
649   ((name :reader cell-error-name :initarg :name)))
650
651 (define-condition unbound-variable (cell-error) ()
652   (:report
653    (lambda (condition stream)
654      (format stream
655              "The variable ~S is unbound."
656              (cell-error-name condition)))))
657
658 (define-condition undefined-function (cell-error) ()
659   (:report
660    (lambda (condition stream)
661      (format stream
662              "The function ~S is undefined."
663              (cell-error-name condition)))))
664
665 (define-condition special-form-function (undefined-function) ()
666   (:report
667    (lambda (condition stream)
668      (format stream
669              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
670              (cell-error-name condition)))))
671
672 (define-condition arithmetic-error (error)
673   ((operation :reader arithmetic-error-operation
674               :initarg :operation
675               :initform nil)
676    (operands :reader arithmetic-error-operands
677              :initarg :operands))
678   (:report (lambda (condition stream)
679              (format stream
680                      "arithmetic error ~S signalled"
681                      (type-of condition))
682              (when (arithmetic-error-operation condition)
683                (format stream
684                        "~%Operation was ~S, operands ~S."
685                        (arithmetic-error-operation condition)
686                        (arithmetic-error-operands condition))))))
687
688 (define-condition division-by-zero         (arithmetic-error) ())
689 (define-condition floating-point-overflow  (arithmetic-error) ())
690 (define-condition floating-point-underflow (arithmetic-error) ())
691 (define-condition floating-point-inexact   (arithmetic-error) ())
692 (define-condition floating-point-invalid-operation (arithmetic-error) ())
693
694 (define-condition print-not-readable (error)
695   ((object :reader print-not-readable-object :initarg :object))
696   (:report
697    (lambda (condition stream)
698      (let ((obj (print-not-readable-object condition))
699            (*print-array* nil))
700        (format stream "~S cannot be printed readably." obj)))))
701
702 (define-condition reader-error (parse-error stream-error)
703   ((format-control
704     :reader reader-error-format-control
705     :initarg :format-control)
706    (format-arguments
707     :reader reader-error-format-arguments
708     :initarg :format-arguments
709     :initform '()))
710   (:report
711    (lambda (condition stream)
712      (let* ((error-stream (stream-error-stream condition))
713             (pos (file-position-or-nil-for-error error-stream)))
714        (let (lineno colno)
715          (when (and pos
716                     (< pos sb!xc:array-dimension-limit)
717                     ;; KLUDGE: lseek() (which is what FILE-POSITION
718                     ;; reduces to on file-streams) is undefined on
719                     ;; "some devices", which in practice means that it
720                     ;; can claim to succeed on /dev/stdin on Darwin
721                     ;; and Solaris.  This is obviously bad news,
722                     ;; because the READ-SEQUENCE below will then
723                     ;; block, not complete, and the report will never
724                     ;; be printed.  As a workaround, we exclude
725                     ;; interactive streams from this attempt to report
726                     ;; positions.  -- CSR, 2003-08-21
727                     (not (interactive-stream-p error-stream))
728                     (file-position error-stream :start))
729            (let ((string
730                   (make-string pos
731                                :element-type (stream-element-type
732                                               error-stream))))
733              (when (= pos (read-sequence string error-stream))
734                (setq lineno (1+ (count #\Newline string))
735                      colno (- pos
736                               (or (position #\Newline string :from-end t) -1)
737                               1))))
738            (file-position-or-nil-for-error error-stream pos))
739          (format stream
740                  "READER-ERROR ~@[at ~W ~]~
741                   ~@[(line ~W~]~@[, column ~W) ~]~
742                   on ~S:~%~?"
743                  pos lineno colno error-stream
744                  (reader-error-format-control condition)
745                  (reader-error-format-arguments condition)))))))
746 \f
747 ;;;; special SBCL extension conditions
748
749 ;;; an error apparently caused by a bug in SBCL itself
750 ;;;
751 ;;; Note that we don't make any serious effort to use this condition
752 ;;; for *all* errors in SBCL itself. E.g. type errors and array
753 ;;; indexing errors can occur in functions called from SBCL code, and
754 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
755 ;;; because the signalling code has no good way to know that the
756 ;;; underlying problem is a bug in SBCL. But in the fairly common case
757 ;;; that the signalling code does know that it's found a bug in SBCL,
758 ;;; this condition is appropriate, reusing boilerplate and helping
759 ;;; users to recognize it as an SBCL bug.
760 (define-condition bug (simple-error)
761   ()
762   (:report
763    (lambda (condition stream)
764      (format stream
765              "~@<  ~? ~:@_~?~:>"
766              (simple-condition-format-control condition)
767              (simple-condition-format-arguments condition)
768              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
769               SBCL might have been corrupted by bad user code, e.g. by an ~
770               undefined Lisp operation like ~S, or by stray pointers from ~
771               alien code or from unsafe Lisp code; or there might be a bug ~
772               in the OS or hardware that SBCL is running on.) If it seems to ~
773               be a bug in SBCL itself, the maintainers would like to know ~
774               about it. Bug reports are welcome on the SBCL ~
775               mailing lists, which you can find at ~
776               <http://sbcl.sourceforge.net/>.~:@>"
777              '((fmakunbound 'compile))))))
778
779 (define-condition simple-storage-condition (storage-condition simple-condition) ())
780
781 ;;; a condition for use in stubs for operations which aren't supported
782 ;;; on some platforms
783 ;;;
784 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
785 ;;;   #-(or freebsd linux)
786 ;;;   (defun load-foreign (&rest rest)
787 ;;;     (error 'unsupported-operator :name 'load-foreign))
788 ;;;   #+(or freebsd linux)
789 ;;;   (defun load-foreign ... actual definition ...)
790 ;;; By signalling a standard condition in this case, we make it
791 ;;; possible for test code to distinguish between (1) intentionally
792 ;;; unimplemented and (2) unintentionally just screwed up somehow.
793 ;;; (Before this condition was defined, test code tried to deal with 
794 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
795 ;;; sbcl-0.7.0, a a package screwup left the definition of
796 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
797 ;;; architectures where it was supposed to be supported, and the
798 ;;; regression tests cheerfully passed because they assumed that
799 ;;; unFBOUNDPness meant they were running on an system which didn't
800 ;;; support the extension.)
801 (define-condition unsupported-operator (simple-error) ())
802
803 \f
804 ;;; (:ansi-cl :function remove)
805 ;;; (:ansi-cl :section (a b c))
806 ;;; (:ansi-cl :glossary "similar")
807 ;;;
808 ;;; (:sbcl :node "...")
809 ;;; (:sbcl :variable *ed-functions*)
810 ;;;
811 ;;; FIXME: this is not the right place for this.
812 (defun print-reference (reference stream)
813   (ecase (car reference)
814     (:amop
815      (format stream "AMOP")
816      (format stream ", ")
817      (destructuring-bind (type data) (cdr reference)
818        (ecase type
819          (:generic-function (format stream "Generic Function ~S" data))
820          (:section (format stream "Section ~{~D~^.~}" data)))))
821     (:ansi-cl
822      (format stream "The ANSI Standard")
823      (format stream ", ")
824      (destructuring-bind (type data) (cdr reference)
825        (ecase type
826          (:function (format stream "Function ~S" data))
827          (:special-operator (format stream "Special Operator ~S" data))
828          (:macro (format stream "Macro ~S" data))
829          (:section (format stream "Section ~{~D~^.~}" data))
830          (:glossary (format stream "Glossary entry for ~S" data))
831          (:issue (format stream "writeup for Issue ~A" data)))))
832     (:sbcl
833      (format stream "The SBCL Manual")
834      (format stream ", ")
835      (destructuring-bind (type data) (cdr reference)
836        (ecase type
837          (:node (format stream "Node ~S" data))
838          (:variable (format stream "Variable ~S" data))
839          (:function (format stream "Function ~S" data)))))
840     ;; FIXME: other documents (e.g. CLIM, Franz documentation :-)
841     ))
842 (define-condition reference-condition ()
843   ((references :initarg :references :reader reference-condition-references)))
844 (defvar *print-condition-references* t)
845 (def!method print-object :around ((o reference-condition) s)
846   (call-next-method)
847   (unless (or *print-escape* *print-readably*)
848     (when (and *print-condition-references*
849                (reference-condition-references o))
850       (format s "~&See also:~%")
851       (pprint-logical-block (s nil :per-line-prefix "  ")
852         (do* ((rs (reference-condition-references o) (cdr rs))
853               (r (car rs) (car rs)))
854              ((null rs))
855           (print-reference r s)
856           (unless (null (cdr rs))
857             (terpri s)))))))
858
859 (define-condition duplicate-definition (reference-condition warning)
860   ((name :initarg :name :reader duplicate-definition-name))
861   (:report (lambda (c s)
862              (format s "~@<Duplicate definition for ~S found in ~
863                         one file.~@:>"
864                      (duplicate-definition-name c))))
865   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
866
867 (define-condition package-at-variance (reference-condition simple-warning) 
868   ()
869   (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
870
871 (define-condition defconstant-uneql (reference-condition error)
872   ((name :initarg :name :reader defconstant-uneql-name)
873    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
874    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
875   (:report
876    (lambda (condition stream)
877      (format stream
878              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
879              (defconstant-uneql-name condition)
880              (defconstant-uneql-old-value condition)
881              (defconstant-uneql-new-value condition))))
882   (:default-initargs :references (list '(:ansi-cl :macro defconstant)
883                                        '(:sbcl :node "Idiosyncrasies"))))
884
885 (define-condition array-initial-element-mismatch 
886     (reference-condition simple-warning)
887   ()
888   (:default-initargs 
889       :references (list 
890                    '(:ansi-cl :function make-array) 
891                    '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
892
893 (define-condition displaced-to-array-too-small-error
894     (reference-condition simple-error)
895   ()
896   (:default-initargs
897       :references (list '(:ansi-cl :function adjust-array))))
898
899 (define-condition type-warning (reference-condition simple-warning)
900   ()
901   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
902
903 (define-condition local-argument-mismatch (reference-condition simple-warning)
904   ()
905   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
906
907 (define-condition format-args-mismatch (reference-condition)
908   ()
909   (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
910
911 (define-condition format-too-few-args-warning 
912     (format-args-mismatch simple-warning)
913   ())
914 (define-condition format-too-many-args-warning
915     (format-args-mismatch simple-style-warning)
916   ())
917
918 (define-condition extension-failure (reference-condition simple-error)
919   ())
920
921 (define-condition structure-initarg-not-keyword
922     (reference-condition simple-style-warning)
923   ()
924   (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
925
926 #!+sb-package-locks
927 (progn
928
929 (define-condition package-lock-violation (reference-condition package-error)
930   ((format-control :initform nil :initarg :format-control 
931                    :reader package-error-format-control)
932    (format-arguments :initform nil :initarg :format-arguments
933                      :reader package-error-format-arguments))
934   (:report 
935    (lambda (condition stream)
936      (let ((control (package-error-format-control condition)))
937        (if control
938            (apply #'format stream
939                   (format nil "~~@<Lock on package ~A violated when ~A.~~:@>"
940                           (package-name (package-error-package condition))
941                           control)
942                   (package-error-format-arguments condition))
943            (format stream "~@<Lock on package ~A violated.~:@>"
944                    (package-name (package-error-package condition)))))))
945   ;; no :default-initargs -- reference-stuff provided by the
946   ;; signalling form in target-package.lisp
947   #!+sb-doc
948   (:documentation
949    "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
950 when a package-lock is violated."))
951
952 (define-condition package-locked-error (package-lock-violation) ()
953   #!+sb-doc
954   (:documentation
955    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
956 signalled when an operation on a package violates a package lock."))
957
958 (define-condition symbol-package-locked-error (package-lock-violation)
959   ((symbol :initarg :symbol :reader package-locked-error-symbol))
960   #!+sb-doc
961   (:documentation
962    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
963 signalled when an operation on a symbol violates a package lock. The
964 symbol that caused the violation is accessed by the function
965 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
966
967 ) ; progn
968
969 (define-condition undefined-alien-error (error) ())
970
971 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
972   (:report
973    (lambda (condition stream)
974      (declare (ignore condition))
975      (format stream "Attempt to access an undefined alien variable."))))
976
977 (define-condition undefined-alien-function-error (undefined-alien-error) ()
978   (:report
979    (lambda (condition stream)
980      (declare (ignore condition))
981      (format stream "Attempt to call an undefined alien function."))))
982
983 \f
984 ;;;; various other (not specified by ANSI) CONDITIONs
985 ;;;;
986 ;;;; These might logically belong in other files; they're here, after
987 ;;;; setup of CONDITION machinery, only because that makes it easier to
988 ;;;; get cold init to work.
989
990 ;;; OAOOM warning: see cross-condition.lisp
991 (define-condition encapsulated-condition (condition)
992   ((condition :initarg :condition :reader encapsulated-condition)))
993
994 (define-condition values-type-error (type-error)
995   ()
996   (:report
997    (lambda (condition stream)
998      (format stream
999              "~@<The values set ~2I~:_[~{~S~^ ~}] ~I~_is not of type ~2I~_~S.~:>"
1000              (type-error-datum condition)
1001              (type-error-expected-type condition)))))
1002
1003 ;;; KLUDGE: a condition for floating point errors when we can't or
1004 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1005 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1006 ;;; know how but the old code was broken by the conversion to POSIX
1007 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1008 ;;;
1009 ;;; FIXME: Perhaps this should also be a base class for all
1010 ;;; floating point exceptions?
1011 (define-condition floating-point-exception (arithmetic-error)
1012   ((flags :initarg :traps
1013           :initform nil
1014           :reader floating-point-exception-traps))
1015   (:report (lambda (condition stream)
1016              (format stream
1017                      "An arithmetic error ~S was signalled.~%"
1018                      (type-of condition))
1019              (let ((traps (floating-point-exception-traps condition)))
1020                (if traps
1021                    (format stream
1022                            "Trapping conditions are: ~%~{ ~S~^~}~%"
1023                            traps)
1024                    (write-line
1025                     "No traps are enabled? How can this be?"
1026                     stream))))))
1027
1028 (define-condition index-too-large-error (type-error)
1029   ()
1030   (:report
1031    (lambda (condition stream)
1032      (format stream
1033              "The index ~S is too large."
1034              (type-error-datum condition)))))
1035
1036 (define-condition bounding-indices-bad-error (reference-condition type-error)
1037   ((object :reader bounding-indices-bad-object :initarg :object))
1038   (:report
1039    (lambda (condition stream)
1040      (let* ((datum (type-error-datum condition))
1041             (start (car datum))
1042             (end (cdr datum))
1043             (object (bounding-indices-bad-object condition)))
1044        (etypecase object
1045          (sequence
1046           (format stream
1047                   "The bounding indices ~S and ~S are bad ~
1048                    for a sequence of length ~S."
1049                   start end (length object)))
1050          (array
1051           ;; from WITH-ARRAY-DATA
1052           (format stream
1053                   "The START and END parameters ~S and ~S are ~
1054                    bad for an array of total size ~S."
1055                   start end (array-total-size object)))))))
1056   (:default-initargs 
1057       :references 
1058       (list '(:ansi-cl :glossary "bounding index designator")
1059             '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1060
1061 (define-condition nil-array-accessed-error (reference-condition type-error)
1062   ()
1063   (:report (lambda (condition stream)
1064              (declare (ignore condition))
1065              (format stream
1066                      "An attempt to access an array of element-type ~
1067                       NIL was made.  Congratulations!")))
1068   (:default-initargs
1069       :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1070                         '(:ansi-cl :section (15 1 2 1))
1071                         '(:ansi-cl :section (15 1 2 2)))))
1072
1073 (define-condition io-timeout (stream-error)
1074   ((direction :reader io-timeout-direction :initarg :direction))
1075   (:report
1076    (lambda (condition stream)
1077      (declare (type stream stream))
1078      (format stream
1079              "I/O timeout ~(~A~)ing ~S"
1080              (io-timeout-direction condition)
1081              (stream-error-stream condition)))))
1082
1083 (define-condition namestring-parse-error (parse-error)
1084   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1085    (args :reader namestring-parse-error-args :initarg :args :initform nil)
1086    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1087    (offset :reader namestring-parse-error-offset :initarg :offset))
1088   (:report
1089    (lambda (condition stream)
1090      (format stream
1091              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
1092              (namestring-parse-error-complaint condition)
1093              (namestring-parse-error-args condition)
1094              (namestring-parse-error-namestring condition)
1095              (namestring-parse-error-offset condition)))))
1096
1097 (define-condition simple-package-error (simple-condition package-error) ())
1098
1099 (define-condition reader-package-error (reader-error) ())
1100
1101 (define-condition reader-eof-error (end-of-file)
1102   ((context :reader reader-eof-error-context :initarg :context))
1103   (:report
1104    (lambda (condition stream)
1105      (format stream
1106              "unexpected end of file on ~S ~A"
1107              (stream-error-stream condition)
1108              (reader-eof-error-context condition)))))
1109
1110 (define-condition reader-impossible-number-error (reader-error)
1111   ((error :reader reader-impossible-number-error-error :initarg :error))
1112   (:report
1113    (lambda (condition stream)
1114      (let ((error-stream (stream-error-stream condition)))
1115        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1116                (file-position-or-nil-for-error error-stream) error-stream
1117                (reader-error-format-control condition)
1118                (reader-error-format-arguments condition)
1119                (reader-impossible-number-error-error condition))))))
1120
1121 (define-condition timeout (serious-condition) ())
1122
1123 (define-condition declaration-type-conflict-error (reference-condition
1124                                                    simple-error)
1125   ()
1126   (:default-initargs
1127       :format-control "symbol ~S cannot be both the name of a type and the name of a declaration"
1128     :references (list '(:ansi-cl :section (3 8 21)))))
1129
1130 ;;; Single stepping conditions
1131
1132 (define-condition step-condition ()
1133   ((form :initarg :form :reader step-condition-form))
1134   #!+sb-doc
1135   (:documentation "Common base class of single-stepping conditions.
1136 STEP-CONDITION-FORM holds a string representation of the form being
1137 stepped."))
1138
1139 #!+sb-doc
1140 (setf (fdocumentation 'step-condition-form 'function)
1141       "Form associated with the STEP-CONDITION.")
1142
1143 (define-condition step-form-condition (step-condition)
1144   ((source-path :initarg :source-path :reader step-condition-source-path)
1145    (pathname :initarg :pathname :reader step-condition-pathname))
1146   #!+sb-doc
1147   (:documentation "Condition signalled by code compiled with
1148 single-stepping information when about to execute a form.
1149 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1150 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1151 holds the source-path to the original form within that file or NIL.
1152 Associated with this condition are always the restarts STEP-INTO,
1153 STEP-NEXT, and STEP-CONTINUE."))
1154
1155 #!+sb-doc
1156 (setf (fdocumentation 'step-condition-source-path 'function)
1157       "Source-path of the original form associated with the
1158 STEP-FORM-CONDITION or NIL."
1159       (fdocumentation 'step-condition-pathname 'function)
1160       "Pathname of the original source-file associated with the
1161 STEP-FORM-CONDITION or NIL.")
1162
1163 (define-condition step-result-condition (step-condition)
1164   ((result :initarg :result :reader step-condition-result)))
1165
1166 #!+sb-doc
1167 (setf (fdocumentation 'step-condition-result 'function)
1168       "Return values associated with STEP-VALUES-CONDITION as a list,
1169 or the variable value associated with STEP-VARIABLE-CONDITION.")
1170
1171 (define-condition step-values-condition (step-result-condition)
1172   ()
1173   #!+sb-doc
1174   (:documentation "Condition signalled by code compiled with
1175 single-stepping information after executing a form.
1176 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1177 the values returned by the form as a list. No associated restarts."))
1178
1179 (define-condition step-variable-condition (step-result-condition)
1180   ()
1181   #!+sb-doc
1182   (:documentation "Condition signalled by code compiled with
1183 single-stepping information when referencing a variable.
1184 STEP-CONDITION-FORM hold the symbol, and STEP-CONDITION-RESULT holds
1185 the value of the variable. No associated restarts."))
1186
1187 \f
1188 ;;;; restart definitions
1189
1190 (define-condition abort-failure (control-error) ()
1191   (:report
1192    "An ABORT restart was found that failed to transfer control dynamically."))
1193
1194 (defun abort (&optional condition)
1195   #!+sb-doc
1196   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1197    none exists."
1198   (invoke-restart (find-restart-or-control-error 'abort condition))
1199   ;; ABORT signals an error in case there was a restart named ABORT
1200   ;; that did not transfer control dynamically. This could happen with
1201   ;; RESTART-BIND.
1202   (error 'abort-failure))
1203
1204 (defun muffle-warning (&optional condition)
1205   #!+sb-doc
1206   "Transfer control to a restart named MUFFLE-WARNING, signalling a
1207    CONTROL-ERROR if none exists."
1208   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1209
1210 (macrolet ((define-nil-returning-restart (name args doc)
1211              #!-sb-doc (declare (ignore doc))
1212              `(defun ,name (,@args &optional condition)
1213                 #!+sb-doc ,doc
1214                 ;; FIXME: Perhaps this shared logic should be pulled out into
1215                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
1216                 (let ((restart (find-restart ',name condition)))
1217                   (when restart
1218                     (invoke-restart restart ,@args))))))
1219   (define-nil-returning-restart continue ()
1220     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1221   (define-nil-returning-restart store-value (value)
1222     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
1223    none exists.")
1224   (define-nil-returning-restart use-value (value)
1225     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
1226    none exists."))
1227
1228 ;;; single-stepping restarts
1229
1230 (macrolet ((def (name doc)
1231                #!-sb-doc (declare (ignore doc))
1232                `(defun ,name (condition)
1233                  #!+sb-doc ,doc
1234                  (invoke-restart (find-restart-or-control-error ',name condition)))))
1235   (def step-continue
1236       "Transfers control to the STEP-CONTINUE restart associated with
1237 the condition, continuing execution without stepping. Signals a
1238 CONTROL-ERROR if the restart does not exist.")
1239   (def step-next
1240       "Transfers control to the STEP-NEXT restart associated with the
1241 condition, executing the current form without stepping and continuing
1242 stepping with the next form. Signals CONTROL-ERROR is the restart does
1243 not exists.")
1244   (def step-into
1245       "Transfers control to the STEP-INTO restart associated with the
1246 condition, stepping into the current form. Signals a CONTROL-ERROR is
1247 the restart does not exist."))
1248
1249 (/show0 "condition.lisp end of file")
1250