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