0.pre7.136:
[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 ;;;; FIXME: should perhaps be called condition.lisp, or moved into
5 ;;;; classes.lisp
6
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
15
16 (in-package "SB!KERNEL")
17 \f
18 ;;;; the CONDITION class
19
20 (/show0 "condition.lisp 20")
21
22 (eval-when (:compile-toplevel :load-toplevel :execute)
23
24 (/show0 "condition.lisp 24")
25
26 (def!struct (condition-class (:include slot-class)
27                              (:constructor bare-make-condition-class))
28   ;; list of CONDITION-SLOT structures for the direct slots of this
29   ;; class
30   (slots nil :type list)
31   ;; list of CONDITION-SLOT structures for all of the effective class
32   ;; slots of this class
33   (class-slots nil :type list)
34   ;; report function or NIL
35   (report nil :type (or function null))
36   ;; list of alternating initargs and initforms
37   (default-initargs () :type list)
38   ;; class precedence list as a list of CLASS objects, with all
39   ;; non-CONDITION classes removed
40   (cpl () :type list)
41   ;; a list of all the effective instance allocation slots of this
42   ;; class that have a non-constant initform or default-initarg.
43   ;; Values for these slots must be computed in the dynamic
44   ;; environment of MAKE-CONDITION.
45   (hairy-slots nil :type list))
46
47 (/show0 "condition.lisp 49")
48
49 (defun make-condition-class (&rest rest)
50   (apply #'bare-make-condition-class
51          (rename-key-args '((:name :%name)) rest)))
52
53 (/show0 "condition.lisp 53")
54
55 ) ; EVAL-WHEN
56
57 (!defstruct-with-alternate-metaclass condition
58   :slot-names (actual-initargs assigned-slots)
59   :boa-constructor %make-condition-object
60   :superclass-name instance
61   :metaclass-name condition-class
62   :metaclass-constructor make-condition-class
63   :dd-type structure)
64
65 (defun make-condition-object (actual-initargs)
66   (%make-condition-object actual-initargs nil))
67
68 (defstruct (condition-slot (:copier nil))
69   (name (missing-arg) :type symbol)
70   ;; list of all applicable initargs
71   (initargs (missing-arg) :type list)
72   ;; names of reader and writer functions
73   (readers (missing-arg) :type list)
74   (writers (missing-arg) :type list)
75   ;; true if :INITFORM was specified
76   (initform-p (missing-arg) :type (member t nil))
77   ;; If this is a function, call it with no args. Otherwise, it's the
78   ;; actual value.
79   (initform (missing-arg) :type t)
80   ;; allocation of this slot, or NIL until defaulted
81   (allocation nil :type (member :instance :class nil))
82   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
83   (cell nil :type (or cons null)))
84
85 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
86 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
87 ;;; have themselves listed in their CPLs. This behavior is inherited
88 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
89 ;;; figured out whether it's right. -- WHN 19990612
90 (eval-when (:compile-toplevel :load-toplevel :execute)
91   (/show0 "condition.lisp 103")
92   (let ((condition-class (locally
93                            ;; KLUDGE: There's a DEFTRANSFORM FIND-CLASS for
94                            ;; constant class names which creates fast but
95                            ;; non-cold-loadable, non-compact code. In this
96                            ;; context, we'd rather have compact, cold-loadable
97                            ;; code. -- WHN 19990928
98                            (declare (notinline sb!xc:find-class))
99                            (sb!xc:find-class 'condition))))
100     (setf (condition-class-cpl condition-class)
101           (list condition-class)))
102   (/show0 "condition.lisp 103"))
103
104 (setf (condition-class-report (locally
105                                 ;; KLUDGE: There's a DEFTRANSFORM FIND-CLASS 
106                                 ;; for constant class names which creates fast
107                                 ;; but non-cold-loadable, non-compact code. In
108                                 ;; this context, we'd rather have compact,
109                                 ;; cold-loadable code. -- WHN 19990928
110                                 (declare (notinline sb!xc:find-class))
111                                 (find-class 'condition)))
112       (lambda (cond stream)
113         (format stream "Condition ~S was signalled." (type-of cond))))
114
115 (eval-when (:compile-toplevel :load-toplevel :execute)
116
117 (defun find-condition-layout (name parent-types)
118   (let* ((cpl (remove-duplicates
119                (reverse
120                 (reduce #'append
121                         (mapcar (lambda (x)
122                                   (condition-class-cpl
123                                    (sb!xc:find-class x)))
124                                 parent-types)))))
125          (cond-layout (info :type :compiler-layout 'condition))
126          (olayout (info :type :compiler-layout name))
127          ;; FIXME: Does this do the right thing in case of multiple
128          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
129          ;; it obvious what ANSI intends to be done in the case of
130          ;; multiple inheritance, so it's not actually clear what the
131          ;; right thing is..
132          (new-inherits
133           (order-layout-inherits (concatenate 'simple-vector
134                                               (layout-inherits cond-layout)
135                                               (mapcar #'class-layout cpl)))))
136     (if (and olayout
137              (not (mismatch (layout-inherits olayout) new-inherits)))
138         olayout
139         (make-layout :class (make-undefined-class name)
140                      :inherits new-inherits
141                      :depthoid -1
142                      :length (layout-length cond-layout)))))
143
144 ) ; EVAL-WHEN
145
146 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
147 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
148 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
149 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
150 ;;;   a condition type C is equivalent to:
151 ;;;     (defmethod print-object ((x c) stream)
152 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
153 ;;; The current code doesn't seem to quite match that.
154 (def!method print-object ((x condition) stream)
155   (if *print-escape*
156       (print-unreadable-object (x stream :type t :identity t))
157       ;; KLUDGE: A comment from CMU CL here said
158       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
159       ;;   superclasses in define-condition call!
160       (dolist (class (condition-class-cpl (sb!xc:class-of x))
161                      (error "no REPORT? shouldn't happen!"))
162         (let ((report (condition-class-report class)))
163           (when report
164             (return (funcall report x stream)))))))
165 \f
166 ;;;; slots of CONDITION objects
167
168 (defvar *empty-condition-slot* '(empty))
169
170 (defun find-slot-default (class slot)
171   (let ((initargs (condition-slot-initargs slot))
172         (cpl (condition-class-cpl class)))
173     (dolist (class cpl)
174       (let ((default-initargs (condition-class-default-initargs class)))
175         (dolist (initarg initargs)
176           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
177             (unless (eq val *empty-condition-slot*)
178               (return-from find-slot-default
179                            (if (functionp val)
180                                (funcall val)
181                                val)))))))
182
183     (if (condition-slot-initform-p slot)
184         (let ((initform (condition-slot-initform slot)))
185           (if (functionp initform)
186               (funcall initform)
187               initform))
188         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
189
190 (defun find-condition-class-slot (condition-class slot-name)
191   (dolist (sclass
192            (condition-class-cpl condition-class)
193            (error "There is no slot named ~S in ~S."
194                   slot-name condition-class))
195     (dolist (slot (condition-class-slots sclass))
196       (when (eq (condition-slot-name slot) slot-name)
197         (return-from find-condition-class-slot slot)))))
198
199 (defun condition-writer-function (condition new-value name)
200   (dolist (cslot (condition-class-class-slots
201                   (layout-class (%instance-layout condition)))
202                  (setf (getf (condition-assigned-slots condition) name)
203                        new-value))
204     (when (eq (condition-slot-name cslot) name)
205       (return (setf (car (condition-slot-cell cslot)) new-value)))))
206
207 (defun condition-reader-function (condition name)
208   (let ((class (layout-class (%instance-layout condition))))
209     (dolist (cslot (condition-class-class-slots class))
210       (when (eq (condition-slot-name cslot) name)
211         (return-from condition-reader-function
212                      (car (condition-slot-cell cslot)))))
213
214     (let ((val (getf (condition-assigned-slots condition) name
215                      *empty-condition-slot*)))
216       (if (eq val *empty-condition-slot*)
217           (let ((actual-initargs (condition-actual-initargs condition))
218                 (slot (find-condition-class-slot class name)))
219             (unless slot
220               (error "missing slot ~S of ~S" name condition))
221             (dolist (initarg (condition-slot-initargs slot))
222               (let ((val (getf actual-initargs
223                                initarg
224                                *empty-condition-slot*)))
225                 (unless (eq val *empty-condition-slot*)
226                   (return-from condition-reader-function
227                                (setf (getf (condition-assigned-slots condition)
228                                            name)
229                                      val)))))
230             (setf (getf (condition-assigned-slots condition) name)
231                   (find-slot-default class slot)))
232           val))))
233 \f
234 ;;;; MAKE-CONDITION
235
236 (defun make-condition (thing &rest args)
237   #!+sb-doc
238   "Make an instance of a condition object using the specified initargs."
239   ;; Note: ANSI specifies no exceptional situations in this function.
240   ;; signalling simple-type-error would not be wrong.
241   (let* ((thing (if (symbolp thing)
242                     (sb!xc:find-class thing)
243                     thing))
244          (class (typecase thing
245                   (condition-class thing)
246                   (class
247                    (error 'simple-type-error
248                           :datum thing
249                           :expected-type 'condition-class
250                           :format-control "~S is not a condition class."
251                           :format-arguments (list thing)))
252                   (t
253                    (error 'simple-type-error
254                           :datum thing
255                           :expected-type 'condition-class
256                           :format-control "bad thing for class argument:~%  ~S"
257                           :format-arguments (list thing)))))
258          (res (make-condition-object args)))
259     (setf (%instance-layout res) (class-layout class))
260     ;; Set any class slots with initargs present in this call.
261     (dolist (cslot (condition-class-class-slots class))
262       (dolist (initarg (condition-slot-initargs cslot))
263         (let ((val (getf args initarg *empty-condition-slot*)))
264           (unless (eq val *empty-condition-slot*)
265             (setf (car (condition-slot-cell cslot)) val)))))
266     ;; Default any slots with non-constant defaults now.
267     (dolist (hslot (condition-class-hairy-slots class))
268       (when (dolist (initarg (condition-slot-initargs hslot) t)
269               (unless (eq (getf args initarg *empty-condition-slot*)
270                           *empty-condition-slot*)
271                 (return nil)))
272         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
273               (find-slot-default class hslot))))
274
275     res))
276 \f
277 ;;;; DEFINE-CONDITION
278
279 (eval-when (:compile-toplevel :load-toplevel :execute)
280 (defun %compiler-define-condition (name direct-supers layout)
281   (multiple-value-bind (class old-layout)
282       (insured-find-class name #'condition-class-p #'make-condition-class)
283     (setf (layout-class layout) class)
284     (setf (class-direct-superclasses class)
285           (mapcar #'sb!xc:find-class direct-supers))
286     (cond ((not old-layout)
287            (register-layout layout))
288           ((not *type-system-initialized*)
289            (setf (layout-class old-layout) class)
290            (setq layout old-layout)
291            (unless (eq (class-layout class) layout)
292              (register-layout layout)))
293           ((redefine-layout-warning "current"
294                                     old-layout
295                                     "new"
296                                     (layout-length layout)
297                                     (layout-inherits layout)
298                                     (layout-depthoid layout))
299            (register-layout layout :invalidate t))
300           ((not (class-layout class))
301            (register-layout layout)))
302
303     (setf (layout-info layout)
304           (locally
305             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
306             ;; names which creates fast but non-cold-loadable, non-compact
307             ;; code. In this context, we'd rather have compact, cold-loadable
308             ;; code. -- WHN 19990928
309             (declare (notinline sb!xc:find-class))
310             (layout-info (class-layout (sb!xc:find-class 'condition)))))
311
312     (setf (sb!xc:find-class name) class)
313
314     ;; Initialize CPL slot.
315     (setf (condition-class-cpl class)
316           (remove-if-not #'condition-class-p 
317                          (std-compute-class-precedence-list class))))
318   (values))
319
320 ) ; EVAL-WHEN
321
322 ;;; Compute the effective slots of CLASS, copying inherited slots and
323 ;;; destructively modifying direct slots.
324 ;;;
325 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
326 ;;; direct slots. Presumably it follows from the semantics of
327 ;;; inheritance and redefinition of conditions, but finding the cite
328 ;;; and documenting it here would be good. (Or, if this is not in fact
329 ;;; ANSI-compliant, fixing it would also be good.:-)
330 (defun compute-effective-slots (class)
331   (collect ((res (copy-list (condition-class-slots class))))
332     (dolist (sclass (condition-class-cpl class))
333       (dolist (sslot (condition-class-slots sclass))
334         (let ((found (find (condition-slot-name sslot) (res))))
335           (cond (found
336                  (setf (condition-slot-initargs found)
337                        (union (condition-slot-initargs found)
338                               (condition-slot-initargs sslot)))
339                  (unless (condition-slot-initform-p found)
340                    (setf (condition-slot-initform-p found)
341                          (condition-slot-initform-p sslot))
342                    (setf (condition-slot-initform found)
343                          (condition-slot-initform sslot)))
344                  (unless (condition-slot-allocation found)
345                    (setf (condition-slot-allocation found)
346                          (condition-slot-allocation sslot))))
347                 (t
348                  (res (copy-structure sslot)))))))
349     (res)))
350
351 (defun %define-condition (name slots documentation report default-initargs)
352   (let ((class (sb!xc:find-class name)))
353     (setf (condition-class-slots class) slots)
354     (setf (condition-class-report class) report)
355     (setf (condition-class-default-initargs class) default-initargs)
356     (setf (fdocumentation name 'type) documentation)
357
358     (dolist (slot slots)
359
360       ;; Set up reader and writer functions.
361       (let ((name (condition-slot-name slot)))
362         (dolist (reader (condition-slot-readers slot))
363           (setf (fdefinition reader)
364                 (lambda (condition)
365                   (condition-reader-function condition name))))
366         (dolist (writer (condition-slot-writers slot))
367           (setf (fdefinition writer)
368                 (lambda (new-value condition)
369                   (condition-writer-function condition new-value name))))))
370
371     ;; Compute effective slots and set up the class and hairy slots
372     ;; (subsets of the effective slots.)
373     (let ((eslots (compute-effective-slots class))
374           (e-def-initargs
375            (reduce #'append
376                    (mapcar #'condition-class-default-initargs
377                            (condition-class-cpl class)))))
378       (dolist (slot eslots)
379         (ecase (condition-slot-allocation slot)
380           (:class
381            (unless (condition-slot-cell slot)
382              (setf (condition-slot-cell slot)
383                    (list (if (condition-slot-initform-p slot)
384                              (let ((initform (condition-slot-initform slot)))
385                                (if (functionp initform)
386                                    (funcall initform)
387                                    initform))
388                              *empty-condition-slot*))))
389            (push slot (condition-class-class-slots class)))
390           ((:instance nil)
391            (setf (condition-slot-allocation slot) :instance)
392            (when (or (functionp (condition-slot-initform slot))
393                      (dolist (initarg (condition-slot-initargs slot) nil)
394                        (when (functionp (getf e-def-initargs initarg))
395                          (return t))))
396              (push slot (condition-class-hairy-slots class))))))))
397   name)
398
399 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
400                                  &body options)
401   #!+sb-doc
402   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
403    Define NAME as a condition type. This new type inherits slots and its
404    report function from the specified PARENT-TYPEs. A slot spec is a list of:
405      (slot-name :reader <rname> :initarg <iname> {Option Value}*
406
407    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
408    and :TYPE and the overall options :DEFAULT-INITARGS and
409    [type] :DOCUMENTATION are also allowed.
410
411    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
412    a string or a two-argument lambda or function name. If a function, the
413    function is called with the condition and stream to report the condition.
414    If a string, the string is printed.
415
416    Condition types are classes, but (as allowed by ANSI and not as described in
417    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
418    SLOT-VALUE may not be used on condition objects."
419   (let* ((parent-types (or parent-types '(condition)))
420          (layout (find-condition-layout name parent-types))
421          (documentation nil)
422          (report nil)
423          (default-initargs ()))
424     (collect ((slots)
425               (all-readers nil append)
426               (all-writers nil append))
427       (dolist (spec slot-specs)
428         (when (keywordp spec)
429           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
430                 spec))
431         (let* ((spec (if (consp spec) spec (list spec)))
432                (slot-name (first spec))
433                (allocation :instance)
434                (initform-p nil)
435                initform)
436           (collect ((initargs)
437                     (readers)
438                     (writers))
439             (do ((options (rest spec) (cddr options)))
440                 ((null options))
441               (unless (and (consp options) (consp (cdr options)))
442                 (error "malformed condition slot spec:~%  ~S." spec))
443               (let ((arg (second options)))
444                 (case (first options)
445                   (:reader (readers arg))
446                   (:writer (writers arg))
447                   (:accessor
448                    (readers arg)
449                    (writers `(setf ,arg)))
450                   (:initform
451                    (when initform-p
452                      (error "more than one :INITFORM in ~S" spec))
453                    (setq initform-p t)
454                    (setq initform arg))
455                   (:initarg (initargs arg))
456                   (:allocation
457                    (setq allocation arg))
458                   (:type)
459                   (t
460                    (error "unknown slot option:~%  ~S" (first options))))))
461
462             (all-readers (readers))
463             (all-writers (writers))
464             (slots `(make-condition-slot
465                      :name ',slot-name
466                      :initargs ',(initargs)
467                      :readers ',(readers)
468                      :writers ',(writers)
469                      :initform-p ',initform-p
470                      :initform
471                      ,(if (constantp initform)
472                           `',(eval initform)
473                           `#'(lambda () ,initform)))))))
474
475       (dolist (option options)
476         (unless (consp option)
477           (error "bad option:~%  ~S" option))
478         (case (first option)
479           (:documentation (setq documentation (second option)))
480           (:report
481            (let ((arg (second option)))
482              (setq report
483                    (if (stringp arg)
484                        `#'(lambda (condition stream)
485                           (declare (ignore condition))
486                           (write-string ,arg stream))
487                        `#'(lambda (condition stream)
488                           (funcall #',arg condition stream))))))
489           (:default-initargs
490            (do ((initargs (rest option) (cddr initargs)))
491                ((endp initargs))
492              (let ((val (second initargs)))
493                (setq default-initargs
494                      (list* `',(first initargs)
495                             (if (constantp val)
496                                 `',(eval val)
497                                 `#'(lambda () ,val))
498                             default-initargs)))))
499           (t
500            (error "unknown option: ~S" (first option)))))
501
502       (when (all-writers)
503         (warn "Condition slot setters probably not allowed in ANSI CL:~%  ~S"
504               (all-writers)))
505
506       `(progn
507          (eval-when (:compile-toplevel :load-toplevel :execute)
508            (%compiler-define-condition ',name ',parent-types ',layout))
509
510          (declaim (ftype (function (t) t) ,@(all-readers)))
511          (declaim (ftype (function (t t) t) ,@(all-writers)))
512
513          (%define-condition ',name
514                             (list ,@(slots))
515                             ,documentation
516                             ,report
517                             (list ,@default-initargs))))))
518 \f
519 ;;;; DESCRIBE on CONDITIONs
520
521 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
522 ;;; eventually (once we get CLOS up and running so that we can define
523 ;;; methods)
524 (defun describe-condition (condition stream)
525   (format stream
526           "~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>"
527           condition
528           (type-of condition)
529           (concatenate 'list
530                        (condition-actual-initargs condition)
531                        (condition-assigned-slots condition))))
532 \f
533 ;;;; various CONDITIONs specified by ANSI
534
535 (define-condition serious-condition (condition) ())
536
537 (define-condition error (serious-condition) ())
538
539 (define-condition warning (condition) ())
540 (define-condition style-warning (warning) ())
541
542 (defun simple-condition-printer (condition stream)
543   (apply #'format
544          stream
545          (simple-condition-format-control condition)
546          (simple-condition-format-arguments condition)))
547
548 (define-condition simple-condition ()
549   ((format-control :reader simple-condition-format-control
550                    :initarg :format-control)
551    (format-arguments :reader simple-condition-format-arguments
552                      :initarg :format-arguments
553                      :initform '()))
554   (:report simple-condition-printer))
555
556 (define-condition simple-warning (simple-condition warning) ())
557
558 (define-condition simple-error (simple-condition error) ())
559
560 (define-condition storage-condition (serious-condition) ())
561
562 (define-condition type-error (error)
563   ((datum :reader type-error-datum :initarg :datum)
564    (expected-type :reader type-error-expected-type :initarg :expected-type))
565   (:report
566    (lambda (condition stream)
567      (format stream
568              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
569              (type-error-datum condition)
570              (type-error-expected-type condition)))))
571
572 (define-condition simple-type-error (simple-condition type-error) ())
573
574 (define-condition program-error (error) ())
575 (define-condition parse-error   (error) ())
576 (define-condition control-error (error) ())
577 (define-condition stream-error  (error)
578   ((stream :reader stream-error-stream :initarg :stream)))
579
580 (define-condition end-of-file (stream-error) ()
581   (:report
582    (lambda (condition stream)
583      (format stream
584              "end of file on ~S"
585              (stream-error-stream condition)))))
586
587 (define-condition file-error (error)
588   ((pathname :reader file-error-pathname :initarg :pathname))
589   (:report
590    (lambda (condition stream)
591      (format stream
592              "~@<error on file ~_~S: ~2I~:_~?~:>"
593              (file-error-pathname condition)
594              ;; FIXME: ANSI's FILE-ERROR doesn't have FORMAT-CONTROL and 
595              ;; FORMAT-ARGUMENTS, and the inheritance here doesn't seem
596              ;; to give us FORMAT-CONTROL or FORMAT-ARGUMENTS either.
597              ;; So how does this work?
598              (serious-condition-format-control condition)
599              (serious-condition-format-arguments condition)))))
600
601 (define-condition package-error (error)
602   ((package :reader package-error-package :initarg :package)))
603
604 (define-condition cell-error (error)
605   ((name :reader cell-error-name :initarg :name)))
606
607 (define-condition unbound-variable (cell-error) ()
608   (:report
609    (lambda (condition stream)
610      (format stream
611              "The variable ~S is unbound."
612              (cell-error-name condition)))))
613
614 (define-condition undefined-function (cell-error) ()
615   (:report
616    (lambda (condition stream)
617      (format stream
618              "The function ~S is undefined."
619              (cell-error-name condition)))))
620
621 (define-condition arithmetic-error (error)
622   ((operation :reader arithmetic-error-operation
623               :initarg :operation
624               :initform nil)
625    (operands :reader arithmetic-error-operands
626              :initarg :operands))
627   (:report (lambda (condition stream)
628              (format stream
629                      "arithmetic error ~S signalled"
630                      (type-of condition))
631              (when (arithmetic-error-operation condition)
632                (format stream
633                        "~%Operation was ~S, operands ~S."
634                        (arithmetic-error-operation condition)
635                        (arithmetic-error-operands condition))))))
636
637 (define-condition division-by-zero         (arithmetic-error) ())
638 (define-condition floating-point-overflow  (arithmetic-error) ())
639 (define-condition floating-point-underflow (arithmetic-error) ())
640 (define-condition floating-point-inexact   (arithmetic-error) ())
641 (define-condition floating-point-invalid-operation (arithmetic-error) ())
642
643 (define-condition print-not-readable (error)
644   ((object :reader print-not-readable-object :initarg :object))
645   (:report
646    (lambda (condition stream)
647      (let ((obj (print-not-readable-object condition))
648            (*print-array* nil))
649        (format stream "~S cannot be printed readably." obj)))))
650
651 (define-condition reader-error (parse-error stream-error)
652   ((format-control
653     :reader reader-error-format-control
654     :initarg :format-control)
655    (format-arguments
656     :reader reader-error-format-arguments
657     :initarg :format-arguments
658     :initform '()))
659   (:report
660    (lambda (condition stream)
661      (let ((error-stream (stream-error-stream condition)))
662        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?"
663                (file-position error-stream) error-stream
664                (reader-error-format-control condition)
665                (reader-error-format-arguments condition))))))
666 \f
667 ;;;; various other (not specified by ANSI) CONDITIONs
668 ;;;;
669 ;;;; These might logically belong in other files; they're here, after
670 ;;;; setup of CONDITION machinery, only because that makes it easier to
671 ;;;; get cold init to work.
672
673 ;;; KLUDGE: a condition for floating point errors when we can't or
674 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
675 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
676 ;;; know how but the old code was broken by the conversion to POSIX
677 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
678 ;;;
679 ;;; FIXME: Perhaps this should also be a base class for all
680 ;;; floating point exceptions?
681 (define-condition floating-point-exception (arithmetic-error)
682   ((flags :initarg :traps
683           :initform nil
684           :reader floating-point-exception-traps))
685   (:report (lambda (condition stream)
686              (format stream
687                      "An arithmetic error ~S was signalled.~%"
688                      (type-of condition))
689              (let ((traps (floating-point-exception-traps condition)))
690                (if traps
691                    (format stream
692                            "Trapping conditions are: ~%~{ ~S~^~}~%"
693                            traps)
694                    (write-line
695                     "No traps are enabled? How can this be?"
696                     stream))))))
697
698 (define-condition index-too-large-error (type-error)
699   ()
700   (:report
701    (lambda (condition stream)
702      (format stream
703              "The index ~S is too large."
704              (type-error-datum condition)))))
705
706 (define-condition io-timeout (stream-error)
707   ((direction :reader io-timeout-direction :initarg :direction))
708   (:report
709    (lambda (condition stream)
710      (declare (type stream stream))
711      (format stream
712              "I/O timeout ~(~A~)ing ~S"
713              (io-timeout-direction condition)
714              (stream-error-stream condition)))))
715
716 (define-condition namestring-parse-error (parse-error)
717   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
718    (args :reader namestring-parse-error-args :initarg :args :initform nil)
719    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
720    (offset :reader namestring-parse-error-offset :initarg :offset))
721   (:report
722    (lambda (condition stream)
723      (format stream
724              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
725              (namestring-parse-error-complaint condition)
726              (namestring-parse-error-args condition)
727              (namestring-parse-error-namestring condition)
728              (namestring-parse-error-offset condition)))))
729
730 (define-condition simple-package-error (simple-condition package-error) ())
731
732 (define-condition reader-package-error (reader-error) ())
733
734 (define-condition reader-eof-error (end-of-file)
735   ((context :reader reader-eof-error-context :initarg :context))
736   (:report
737    (lambda (condition stream)
738      (format stream
739              "unexpected end of file on ~S ~A"
740              (stream-error-stream condition)
741              (reader-eof-error-context condition)))))
742 \f
743 ;;;; restart definitions
744
745 (define-condition abort-failure (control-error) ()
746   (:report
747    "An ABORT restart was found that failed to transfer control dynamically."))
748
749 (defun abort (&optional condition)
750   #!+sb-doc
751   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
752    none exists."
753   (invoke-restart (find-restart 'abort condition))
754   ;; ABORT signals an error in case there was a restart named ABORT
755   ;; that did not transfer control dynamically. This could happen with
756   ;; RESTART-BIND.
757   (error 'abort-failure))
758
759 (defun muffle-warning (&optional condition)
760   #!+sb-doc
761   "Transfer control to a restart named MUFFLE-WARNING, signalling a
762    CONTROL-ERROR if none exists."
763   (invoke-restart (find-restart 'muffle-warning condition)))
764
765 (macrolet ((define-nil-returning-restart (name args doc)
766              #!-sb-doc (declare (ignore doc))
767              `(defun ,name (,@args &optional condition)
768                 #!+sb-doc ,doc
769                 ;; FIXME: Perhaps this shared logic should be pulled out into
770                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
771                 (when (find-restart ',name condition)
772                   (invoke-restart ',name ,@args)))))
773   (define-nil-returning-restart continue ()
774     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
775   (define-nil-returning-restart store-value (value)
776     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
777    none exists.")
778   (define-nil-returning-restart use-value (value)
779     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
780    none exists."))
781
782 (/show0 "condition.lisp end of file")
783