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