91ecea18ab584cd53becaf738c81322183b7839a
[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 class.
25   (slots nil :type list)
26   ;; List of CONDITION-SLOT structures for all of the effective class slots of
27   ;; this class.
28   (class-slots nil :type list)
29   ;; Report function or NIL.
30   (report nil :type (or function null))
31   ;; List of alternating initargs and initforms.
32   (default-initargs () :type list)
33   ;; CPL as a list of class objects, with all non-condition classes removed.
34   (cpl () :type list)
35   ;; A list of all the effective instance allocation slots of this class that
36   ;; have a non-constant initform or default-initarg. Values for these slots
37   ;; must be computed in the dynamic environment of MAKE-CONDITION.
38   (hairy-slots nil :type list))
39
40 (defun make-condition-class (&rest rest)
41   (apply #'bare-make-condition-class
42          (rename-keyword-args '((:name :%name)) rest)))
43
44 ) ; EVAL-WHEN
45
46 (defstruct (condition
47             (:constructor make-condition-object (actual-initargs))
48             (:alternate-metaclass instance
49                                   condition-class
50                                   make-condition-class)
51             (:copier nil))
52
53   (function-name nil)
54   ;; Actual initargs supplied to MAKE-CONDITION.
55   (actual-initargs (required-argument) :type list)
56   ;; Plist mapping slot names to any values that were assigned or defaulted
57   ;; after creation.
58   (assigned-slots () :type list))
59
60 (defstruct condition-slot
61   (name (required-argument) :type symbol)
62   ;; List of all applicable initargs.
63   (initargs (required-argument) :type list)
64   ;; Names of reader and writer functions.
65   (readers (required-argument) :type list)
66   (writers (required-argument) :type list)
67   ;; True if :INITFORM was specified.
68   (initform-p (required-argument) :type (member t nil))
69   ;; If a function, call it with no args. Otherwise, the actual value.
70   (initform (required-argument) :type t)
71   ;; Allocation of this slot. Nil only until defaulted.
72   (allocation nil :type (member :instance :class nil))
73   ;; If :class allocation, a cons whose car holds the value.
74   (cell nil :type (or cons null)))
75
76 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
77   ;; the appropriate initialization value for the CPL slot of a CONDITION,
78   ;; calculated by looking at the INHERITS information in the LAYOUT
79   ;; of the CONDITION
80   (defun condition-class-cpl-from-layout (condition)
81     (declare (type condition condition))
82     (let* ((class (sb!xc:find-class condition))
83            (layout (class-layout class))
84            (superset (map 'list #'identity (layout-inherits layout))))
85       (delete-if (lambda (superclass)
86                    (not (typep superclass 'condition-class)))
87                  superset))))
88
89 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed in its
90 ;;; CPL, while other classes derived from CONDITION-CLASS don't have themselves
91 ;;; listed in their CPLs. This behavior is inherited from CMU CL, and didn't
92 ;;; seem to be explained there, and I haven't figured out whether it's right.
93 ;;; -- WHN 19990612
94 (eval-when (:compile-toplevel :load-toplevel :execute)
95   (let ((condition-class (locally
96                            ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for
97                            ;; constant class names which creates fast but
98                            ;; non-cold-loadable, non-compact code. In this
99                            ;; context, we'd rather have compact, cold-loadable
100                            ;; code. -- WHN 19990928
101                            (declare (notinline sb!xc:find-class))
102                            (sb!xc:find-class 'condition))))
103     (setf (condition-class-cpl condition-class)
104           (list condition-class))))
105
106 (setf (condition-class-report (locally
107                                 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM
108                                 ;; for constant class names which creates fast
109                                 ;; but non-cold-loadable, non-compact code. In
110                                 ;; this context, we'd rather have compact,
111                                 ;; cold-loadable code. -- WHN 19990928
112                                 (declare (notinline sb!xc:find-class))
113                                 (find-class 'condition)))
114       #'(lambda (cond stream)
115           (format stream "Condition ~S was signalled." (type-of cond))))
116
117 (eval-when (:compile-toplevel :load-toplevel :execute)
118
119 (defun find-condition-layout (name parent-types)
120   (let* ((cpl (remove-duplicates
121                (reverse
122                 (reduce #'append
123                         (mapcar #'(lambda (x)
124                                     (condition-class-cpl
125                                      (sb!xc:find-class x)))
126                                 parent-types)))))
127          (cond-layout (info :type :compiler-layout 'condition))
128          (olayout (info :type :compiler-layout name))
129          (new-inherits
130           (concatenate 'simple-vector
131                        (layout-inherits cond-layout)
132                        (mapcar #'class-layout cpl))))
133     (if (and olayout
134              (not (mismatch (layout-inherits olayout) new-inherits)))
135         olayout
136         (make-layout :class (make-undefined-class name)
137                      :inherits new-inherits
138                      :depthoid -1
139                      :length (layout-length cond-layout)))))
140
141 ) ; EVAL-WHEN
142
143 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
144 ;;;   Condition reporting is mediated through the print-object method for
145 ;;;   the condition type in question, with *print-escape* always being nil.
146 ;;;   Specifying (:report report-name) in the definition of a condition
147 ;;;   type C is equivalent to:
148 ;;;     (defmethod print-object ((x c) stream)
149 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
150 ;;; The current code doesn't seem to quite match that.
151 (def!method print-object ((x condition) stream)
152   (if *print-escape*
153       (print-unreadable-object (x stream :type t :identity t))
154       ;; KLUDGE: A comment from CMU CL here said
155       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
156       ;;   superclasses in define-condition call!
157       (dolist (class (condition-class-cpl (sb!xc:class-of x))
158                      (error "no REPORT? shouldn't happen!"))
159         (let ((report (condition-class-report class)))
160           (when report
161             (return (funcall report x stream)))))))
162 \f
163 ;;;; slots of CONDITION objects
164
165 (defvar *empty-slot* '(empty))
166
167 (defun find-slot-default (class slot)
168   (let ((initargs (condition-slot-initargs slot))
169         (cpl (condition-class-cpl class)))
170     (dolist (class cpl)
171       (let ((default-initargs (condition-class-default-initargs class)))
172         (dolist (initarg initargs)
173           (let ((val (getf default-initargs initarg *empty-slot*)))
174             (unless (eq val *empty-slot*)
175               (return-from find-slot-default
176                            (if (functionp val)
177                                (funcall val)
178                                val)))))))
179
180     (if (condition-slot-initform-p slot)
181         (let ((initform (condition-slot-initform slot)))
182           (if (functionp initform)
183               (funcall initform)
184               initform))
185         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
186
187 (defun find-slot (classes name)
188   (dolist (sclass classes nil)
189     (dolist (slot (condition-class-slots sclass))
190       (when (eq (condition-slot-name slot) name)
191         (return-from find-slot slot)))))
192
193 (defun condition-writer-function (condition new-value name)
194   (dolist (cslot (condition-class-class-slots
195                   (layout-class (%instance-layout condition)))
196                  (setf (getf (condition-assigned-slots condition) name)
197                        new-value))
198     (when (eq (condition-slot-name cslot) name)
199       (return (setf (car (condition-slot-cell cslot)) new-value)))))
200
201 (defun condition-reader-function (condition name)
202   (let ((class (layout-class (%instance-layout condition))))
203     (dolist (cslot (condition-class-class-slots class))
204       (when (eq (condition-slot-name cslot) name)
205         (return-from condition-reader-function
206                      (car (condition-slot-cell cslot)))))
207
208     (let ((val (getf (condition-assigned-slots condition) name
209                      *empty-slot*)))
210       (if (eq val *empty-slot*)
211           (let ((actual-initargs (condition-actual-initargs condition))
212                 (slot (find-slot (condition-class-cpl class) name)))
213             (dolist (initarg (condition-slot-initargs slot))
214               (let ((val (getf actual-initargs initarg *empty-slot*)))
215                 (unless (eq val *empty-slot*)
216                   (return-from condition-reader-function
217                                (setf (getf (condition-assigned-slots condition)
218                                            name)
219                                      val)))))
220             (setf (getf (condition-assigned-slots condition) name)
221                   (find-slot-default class slot)))
222           val))))
223 \f
224 ;;;; MAKE-CONDITION
225
226 (defun make-condition (thing &rest args)
227   #!+sb-doc
228   "Make an instance of a condition object using the specified initargs."
229   ;; Note: ANSI specifies no exceptional situations in this function.
230   ;; signalling simple-type-error would not be wrong.
231   (let* ((thing (if (symbolp thing)
232                     (sb!xc:find-class thing)
233                     thing))
234          (class (typecase thing
235                   (condition-class thing)
236                   (class
237                    (error 'simple-type-error
238                           :datum thing
239                           :expected-type 'condition-class
240                           :format-control "~S is not a condition class."
241                           :format-arguments (list thing)))
242                   (t
243                    (error 'simple-type-error
244                           :datum thing
245                           :expected-type 'condition-class
246                           :format-control "bad thing for class arg:~%  ~S"
247                           :format-arguments (list thing)))))
248          (res (make-condition-object args)))
249     (setf (%instance-layout res) (class-layout class))
250     ;; Set any class slots with initargs present in this call.
251     (dolist (cslot (condition-class-class-slots class))
252       (dolist (initarg (condition-slot-initargs cslot))
253         (let ((val (getf args initarg *empty-slot*)))
254           (unless (eq val *empty-slot*)
255             (setf (car (condition-slot-cell cslot)) val)))))
256     ;; Default any slots with non-constant defaults now.
257     (dolist (hslot (condition-class-hairy-slots class))
258       (when (dolist (initarg (condition-slot-initargs hslot) t)
259               (unless (eq (getf args initarg *empty-slot*) *empty-slot*)
260                 (return nil)))
261         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
262               (find-slot-default class hslot))))
263
264     res))
265 \f
266 ;;;; DEFINE-CONDITION
267
268 (eval-when (:compile-toplevel :load-toplevel :execute)
269 (defun %compiler-define-condition (name direct-supers layout)
270   (multiple-value-bind (class old-layout)
271       (insured-find-class name #'condition-class-p #'make-condition-class)
272     (setf (layout-class layout) class)
273     (setf (class-direct-superclasses class)
274           (mapcar #'sb!xc:find-class direct-supers))
275     (cond ((not old-layout)
276            (register-layout layout))
277           ((not *type-system-initialized*)
278            (setf (layout-class old-layout) class)
279            (setq layout old-layout)
280            (unless (eq (class-layout class) layout)
281              (register-layout layout)))
282           ((redefine-layout-warning "current"
283                                     old-layout
284                                     "new"
285                                     (layout-length layout)
286                                     (layout-inherits layout)
287                                     (layout-depthoid layout))
288            (register-layout layout :invalidate t))
289           ((not (class-layout class))
290            (register-layout layout)))
291
292     (setf (layout-info layout)
293           (locally
294             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
295             ;; names which creates fast but non-cold-loadable, non-compact
296             ;; code. In this context, we'd rather have compact, cold-loadable
297             ;; code. -- WHN 19990928
298             (declare (notinline sb!xc:find-class))
299             (layout-info (class-layout (sb!xc:find-class 'condition)))))
300
301     (setf (sb!xc:find-class name) class)
302
303     ;; Initialize CPL slot from layout.
304     (collect ((cpl))
305       (cpl class)
306       (let ((inherits (layout-inherits layout)))
307         (do ((i (1- (length inherits)) (1- i)))
308             ((minusp i))
309           (let ((super (sb!xc:find-class
310                         (sb!xc:class-name
311                          (layout-class (svref inherits i))))))
312             (when (typep super 'condition-class)
313               (cpl super)))))
314       (setf (condition-class-cpl class) (cpl))))
315
316   (values))
317
318 ) ; EVAL-WHEN
319
320 ;;; Compute the effective slots of class, copying inherited slots and
321 ;;; side-effecting direct slots.
322 (defun compute-effective-slots (class)
323   (collect ((res (copy-list (condition-class-slots class))))
324     (dolist (sclass (condition-class-cpl class))
325       (dolist (sslot (condition-class-slots sclass))
326         (let ((found (find (condition-slot-name sslot) (res)
327                            :test #'eq)))
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 (subsets of
365     ;; 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-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 ;;;; various CONDITIONs specified by ANSI
513
514 (define-condition serious-condition (condition)())
515
516 (define-condition error (serious-condition) ())
517
518 (define-condition warning (condition) ())
519 (define-condition style-warning (warning) ())
520
521 (defun simple-condition-printer (condition stream)
522   ;; FIXME: Why use APPLY instead of an ordinary form? To stop the optimizer
523   ;; from doing something?
524   (apply #'format stream (simple-condition-format-control condition)
525                          (simple-condition-format-arguments condition)))
526
527 (define-condition simple-condition ()
528   ((format-control :reader simple-condition-format-control
529                    :initarg :format-control)
530    (format-arguments :reader simple-condition-format-arguments
531                      :initarg :format-arguments
532                      :initform '()))
533   (:report simple-condition-printer))
534
535 (define-condition simple-warning (simple-condition warning) ())
536
537 (defun print-simple-error (condition stream)
538   (format stream
539           "~&~@<error in function ~S: ~3I~:_~?~:>"
540           (condition-function-name condition)
541           (simple-condition-format-control condition)
542           (simple-condition-format-arguments condition)))
543
544 (define-condition simple-error (simple-condition error) ()
545   ;; This is the condition type used by ERROR and CERROR when
546   ;; a format-control string is supplied as the first argument.
547   (:report print-simple-error))
548
549 (define-condition storage-condition (serious-condition) ())
550
551 ;;; FIXME: Should we really be reporting CONDITION-FUNCTION-NAME data on an
552 ;;; ad hoc basis, for some conditions and not others? Why not standardize
553 ;;; it somehow? perhaps by making the debugger report it?
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              "~@<TYPE-ERROR in ~S: ~3I~:_~S is not of type ~S~:>."
562              (condition-function-name condition)
563              (type-error-datum condition)
564              (type-error-expected-type condition)))))
565
566 (define-condition program-error (error) ())
567 (define-condition parse-error   (error) ())
568 (define-condition control-error (error) ())
569 (define-condition stream-error  (error)
570   ((stream :reader stream-error-stream :initarg :stream)))
571
572 (define-condition end-of-file (stream-error) ()
573   (:report
574    (lambda (condition stream)
575      (format stream
576              "END-OF-FILE on ~S"
577              (stream-error-stream condition)))))
578
579 (define-condition file-error (error)
580   ((pathname :reader file-error-pathname :initarg :pathname))
581   (:report
582    (lambda (condition stream)
583      (format stream
584              "~&~@<FILE-ERROR in function ~S: ~3i~:_~?~:>"
585              (condition-function-name condition)
586              (serious-condition-format-control condition)
587              (serious-condition-format-arguments condition)))))
588
589 (define-condition package-error (error)
590   ((package :reader package-error-package :initarg :package)))
591
592 (define-condition cell-error (error)
593   ((name :reader cell-error-name :initarg :name)))
594
595 (define-condition unbound-variable (cell-error) ()
596   (:report
597    (lambda (condition stream)
598      (format stream
599              "error in ~S: The variable ~S is unbound."
600              (condition-function-name condition)
601              (cell-error-name condition)))))
602
603 (define-condition undefined-function (cell-error) ()
604   (:report
605    (lambda (condition stream)
606      (format stream
607              "error in ~S: The function ~S is undefined."
608              (condition-function-name condition)
609              (cell-error-name condition)))))
610
611 (define-condition arithmetic-error (error)
612   ((operation :reader arithmetic-error-operation
613               :initarg :operation
614               :initform nil)
615    (operands :reader arithmetic-error-operands
616              :initarg :operands))
617   (:report (lambda (condition stream)
618              (format stream
619                      "arithmetic error ~S signalled"
620                      (type-of condition))
621              (when (arithmetic-error-operation condition)
622                (format stream
623                        "~%Operation was ~S, operands ~S."
624                        (arithmetic-error-operation condition)
625                        (arithmetic-error-operands condition))))))
626
627 (define-condition division-by-zero       (arithmetic-error) ())
628 (define-condition floating-point-overflow  (arithmetic-error) ())
629 (define-condition floating-point-underflow (arithmetic-error) ())
630 (define-condition floating-point-inexact   (arithmetic-error) ())
631 (define-condition floating-point-invalid-operation   (arithmetic-error) ())
632
633 (define-condition print-not-readable (error)
634   ((object :reader print-not-readable-object :initarg :object))
635   (:report
636    (lambda (condition stream)
637      (let ((obj (print-not-readable-object condition))
638            (*print-array* nil))
639        (format stream "~S cannot be printed readably." obj)))))
640
641 (define-condition reader-error (parse-error stream-error)
642   ((format-control
643     :reader reader-error-format-control
644     :initarg :format-control)
645    (format-arguments
646     :reader reader-error-format-arguments
647     :initarg :format-arguments
648     :initform '()))
649   (:report
650    (lambda (condition stream)
651      (let ((error-stream (stream-error-stream condition)))
652        (format stream "READER-ERROR ~@[at ~D ~]on ~S:~%~?"
653                (file-position error-stream) error-stream
654                (reader-error-format-control condition)
655                (reader-error-format-arguments condition))))))
656 \f
657 ;;;; various other (not specified by ANSI) CONDITIONs
658 ;;;;
659 ;;;; These might logically belong in other files; they're here, after
660 ;;;; setup of CONDITION machinery, only because that makes it easier to
661 ;;;; get cold init to work.
662
663 ;;; KLUDGE: a condition for floating point errors when we can't or
664 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
665 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
666 ;;; know how but the old code was broken by the conversion to POSIX
667 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
668 ;;;
669 ;;; FIXME: Perhaps this should also be a base class for all
670 ;;; floating point exceptions?
671 (define-condition floating-point-exception (arithmetic-error)
672   ((flags :initarg :traps
673           :reader floating-point-exception-traps))
674   (:report (lambda (condition stream)
675              (format stream
676                      "An arithmetic error ~S was signalled.~%"
677                      (type-of condition))
678              (let ((traps (floating-point-exception-traps condition)))
679                (if traps
680                    (format stream
681                            "Trapping conditions are: ~%~{ ~S~^~}~%"
682                            traps)
683                    (write-line
684                     "No traps are enabled? How can this be?"
685                     stream))))))
686
687 (define-condition index-too-large-error (type-error)
688   ()
689   (:report
690    (lambda (condition stream)
691      (format stream
692              "error in ~S: ~S: index too large"
693              (condition-function-name condition)
694              (type-error-datum condition)))))
695
696 (define-condition io-timeout (stream-error)
697   ((direction :reader io-timeout-direction :initarg :direction))
698   (:report
699    (lambda (condition stream)
700      (declare (type stream stream))
701      (format stream
702              "IO-TIMEOUT ~(~A~)ing ~S"
703              (io-timeout-direction condition)
704              (stream-error-stream condition)))))
705
706 (define-condition namestring-parse-error (parse-error)
707   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
708    (arguments :reader namestring-parse-error-arguments :initarg :arguments
709               :initform nil)
710    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
711    (offset :reader namestring-parse-error-offset :initarg :offset))
712   (:report %print-namestring-parse-error))
713
714 (define-condition simple-package-error (simple-condition package-error) ())
715
716 (define-condition reader-package-error (reader-error) ())
717
718 (define-condition reader-eof-error (end-of-file)
719   ((context :reader reader-eof-error-context :initarg :context))
720   (:report
721    (lambda (condition stream)
722      (format stream
723              "unexpected EOF on ~S ~A"
724              (stream-error-stream condition)
725              (reader-eof-error-context condition)))))
726 \f
727 ;;;; restart definitions
728
729 (define-condition abort-failure (control-error) ()
730   (:report
731    "An ABORT restart was found that failed to transfer control dynamically."))
732
733 (defun abort (&optional condition)
734   #!+sb-doc
735   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
736    none exists."
737   (invoke-restart (find-restart 'abort condition))
738   ;; ABORT signals an error in case there was a restart named abort that did
739   ;; not transfer control dynamically. This could happen with RESTART-BIND.
740   (error 'abort-failure))
741
742 (defun muffle-warning (&optional condition)
743   #!+sb-doc
744   "Transfer control to a restart named MUFFLE-WARNING, signalling a
745    CONTROL-ERROR if none exists."
746   (invoke-restart (find-restart 'muffle-warning condition)))
747
748 (macrolet ((define-nil-returning-restart (name args doc)
749              #!-sb-doc (declare (ignore doc))
750              `(defun ,name (,@args &optional condition)
751                 #!+sb-doc ,doc
752                 ;; FIXME: Perhaps this shared logic should be pulled out into
753                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
754                 (when (find-restart ',name condition)
755                   (invoke-restart ',name ,@args)))))
756   (define-nil-returning-restart continue ()
757     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
758   (define-nil-returning-restart store-value (value)
759     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
760    none exists.")
761   (define-nil-returning-restart use-value (value)
762     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
763    none exists."))