Initial revision
[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
18 (sb!int:file-comment
19   "$Header$")
20 \f
21 ;;;; the CONDITION class
22
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24
25 (def!struct (condition-class (:include slot-class)
26                              (:constructor bare-make-condition-class))
27   ;; List of CONDITION-SLOT structures for the direct slots of this class.
28   (slots nil :type list)
29   ;; List of CONDITION-SLOT structures for all of the effective class slots of
30   ;; 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   ;; CPL as a list of class objects, with all non-condition classes removed.
37   (cpl () :type list)
38   ;; A list of all the effective instance allocation slots of this class that
39   ;; have a non-constant initform or default-initarg. Values for these slots
40   ;; must be computed in the dynamic 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 defaulted
60   ;; 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 a function, call it with no args. Otherwise, the actual value.
73   (initform (required-argument) :type t)
74   ;; Allocation of this slot. Nil only until defaulted.
75   (allocation nil :type (member :instance :class nil))
76   ;; If :class allocation, a cons whose car holds the value.
77   (cell nil :type (or cons null)))
78
79 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
80   ;; the appropriate initialization value for the CPL slot of a CONDITION,
81   ;; calculated by looking at the INHERITS information in the LAYOUT
82   ;; of the CONDITION
83   (defun condition-class-cpl-from-layout (condition)
84     (declare (type condition condition))
85     (let* ((class (sb!xc:find-class condition))
86            (layout (class-layout class))
87            (superset (map 'list #'identity (layout-inherits layout))))
88       (delete-if (lambda (superclass)
89                    (not (typep superclass 'condition-class)))
90                  superset))))
91
92 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed in its
93 ;;; CPL, while other classes derived from CONDITION-CLASS don't have themselves
94 ;;; listed in their CPLs. This behavior is inherited from CMU CL, and didn't
95 ;;; seem to be explained there, and I haven't figured out whether it's right.
96 ;;; -- WHN 19990612
97 (eval-when (:compile-toplevel :load-toplevel :execute)
98   (let ((condition-class (locally
99                            ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for
100                            ;; constant class names which creates fast but
101                            ;; non-cold-loadable, non-compact code. In this
102                            ;; context, we'd rather have compact, cold-loadable
103                            ;; code. -- WHN 19990928
104                            (declare (notinline sb!xc:find-class))
105                            (sb!xc:find-class 'condition))))
106     (setf (condition-class-cpl condition-class)
107           (list condition-class))))
108
109 (setf (condition-class-report (locally
110                                 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM
111                                 ;; for constant class names which creates fast
112                                 ;; but non-cold-loadable, non-compact code. In
113                                 ;; this context, we'd rather have compact,
114                                 ;; cold-loadable code. -- WHN 19990928
115                                 (declare (notinline sb!xc:find-class))
116                                 (find-class 'condition)))
117       #'(lambda (cond stream)
118           (format stream "Condition ~S was signalled." (type-of cond))))
119
120 (eval-when (:compile-toplevel :load-toplevel :execute)
121
122 (defun find-condition-layout (name parent-types)
123   (let* ((cpl (remove-duplicates
124                (reverse
125                 (reduce #'append
126                         (mapcar #'(lambda (x)
127                                     (condition-class-cpl
128                                      (sb!xc:find-class x)))
129                                 parent-types)))))
130          (cond-layout (info :type :compiler-layout 'condition))
131          (olayout (info :type :compiler-layout name))
132          (new-inherits
133           (concatenate 'simple-vector
134                        (layout-inherits cond-layout)
135                        (mapcar #'class-layout cpl))))
136     (if (and olayout
137              (not (mismatch (layout-inherits olayout) new-inherits)))
138         olayout
139         (make-layout :class (make-undefined-class name)
140                      :inherits new-inherits
141                      :depthoid -1
142                      :length (layout-length cond-layout)))))
143
144 ) ; EVAL-WHEN
145
146 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
147 ;;;   Condition reporting is mediated through the print-object method for
148 ;;;   the condition type in question, with *print-escape* always being nil.
149 ;;;   Specifying (:report report-name) in the definition of a condition
150 ;;;   type C is equivalent to:
151 ;;;     (defmethod print-object ((x c) stream)
152 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
153 ;;; The current code doesn't seem to quite match that.
154 (def!method print-object ((x condition) stream)
155   (if *print-escape*
156       (print-unreadable-object (x stream :type t :identity t))
157       ;; KLUDGE: A comment from CMU CL here said
158       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
159       ;;   superclasses in define-condition call!
160       (dolist (class (condition-class-cpl (sb!xc:class-of x))
161                      (error "no REPORT? shouldn't happen!"))
162         (let ((report (condition-class-report class)))
163           (when report
164             (return (funcall report x stream)))))))
165 \f
166 ;;;; slots of CONDITION objects
167
168 (defvar *empty-slot* '(empty))
169
170 (defun find-slot-default (class slot)
171   (let ((initargs (condition-slot-initargs slot))
172         (cpl (condition-class-cpl class)))
173     (dolist (class cpl)
174       (let ((default-initargs (condition-class-default-initargs class)))
175         (dolist (initarg initargs)
176           (let ((val (getf default-initargs initarg *empty-slot*)))
177             (unless (eq val *empty-slot*)
178               (return-from find-slot-default
179                            (if (functionp val)
180                                (funcall val)
181                                val)))))))
182
183     (if (condition-slot-initform-p slot)
184         (let ((initform (condition-slot-initform slot)))
185           (if (functionp initform)
186               (funcall initform)
187               initform))
188         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
189
190 (defun find-slot (classes name)
191   (dolist (sclass classes nil)
192     (dolist (slot (condition-class-slots sclass))
193       (when (eq (condition-slot-name slot) name)
194         (return-from find-slot slot)))))
195
196 (defun condition-writer-function (condition new-value name)
197   (dolist (cslot (condition-class-class-slots
198                   (layout-class (%instance-layout condition)))
199                  (setf (getf (condition-assigned-slots condition) name)
200                        new-value))
201     (when (eq (condition-slot-name cslot) name)
202       (return (setf (car (condition-slot-cell cslot)) new-value)))))
203
204 (defun condition-reader-function (condition name)
205   (let ((class (layout-class (%instance-layout condition))))
206     (dolist (cslot (condition-class-class-slots class))
207       (when (eq (condition-slot-name cslot) name)
208         (return-from condition-reader-function
209                      (car (condition-slot-cell cslot)))))
210
211     (let ((val (getf (condition-assigned-slots condition) name
212                      *empty-slot*)))
213       (if (eq val *empty-slot*)
214           (let ((actual-initargs (condition-actual-initargs condition))
215                 (slot (find-slot (condition-class-cpl class) name)))
216             (dolist (initarg (condition-slot-initargs slot))
217               (let ((val (getf actual-initargs initarg *empty-slot*)))
218                 (unless (eq val *empty-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 arg:~%  ~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-slot*)))
257           (unless (eq val *empty-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-slot*) *empty-slot*)
263                 (return nil)))
264         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
265               (find-slot-default class hslot))))
266
267     res))
268 \f
269 ;;;; DEFINE-CONDITION
270
271 (eval-when (:compile-toplevel :load-toplevel :execute)
272 (defun %compiler-define-condition (name direct-supers layout)
273   (multiple-value-bind (class old-layout)
274       (insured-find-class name #'condition-class-p #'make-condition-class)
275     (setf (layout-class layout) class)
276     (setf (class-direct-superclasses class)
277           (mapcar #'sb!xc:find-class direct-supers))
278     (cond ((not old-layout)
279            (register-layout layout))
280           ((not *type-system-initialized*)
281            (setf (layout-class old-layout) class)
282            (setq layout old-layout)
283            (unless (eq (class-layout class) layout)
284              (register-layout layout)))
285           ((redefine-layout-warning "current"
286                                     old-layout
287                                     "new"
288                                     (layout-length layout)
289                                     (layout-inherits layout)
290                                     (layout-depthoid layout))
291            (register-layout layout :invalidate t))
292           ((not (class-layout class))
293            (register-layout layout)))
294
295     (setf (layout-info layout)
296           (locally
297             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
298             ;; names which creates fast but non-cold-loadable, non-compact
299             ;; code. In this context, we'd rather have compact, cold-loadable
300             ;; code. -- WHN 19990928
301             (declare (notinline sb!xc:find-class))
302             (layout-info (class-layout (sb!xc:find-class 'condition)))))
303
304     (setf (sb!xc:find-class name) class)
305
306     ;; Initialize CPL slot from layout.
307     (collect ((cpl))
308       (cpl class)
309       (let ((inherits (layout-inherits layout)))
310         (do ((i (1- (length inherits)) (1- i)))
311             ((minusp i))
312           (let ((super (sb!xc:find-class
313                         (sb!xc:class-name
314                          (layout-class (svref inherits i))))))
315             (when (typep super 'condition-class)
316               (cpl super)))))
317       (setf (condition-class-cpl class) (cpl))))
318
319   (values))
320
321 ) ; EVAL-WHEN
322
323 ;;; Compute the effective slots of class, copying inherited slots and
324 ;;; side-effecting direct slots.
325 (defun compute-effective-slots (class)
326   (collect ((res (copy-list (condition-class-slots class))))
327     (dolist (sclass (condition-class-cpl class))
328       (dolist (sslot (condition-class-slots sclass))
329         (let ((found (find (condition-slot-name sslot) (res)
330                            :test #'eq)))
331           (cond (found
332                  (setf (condition-slot-initargs found)
333                        (union (condition-slot-initargs found)
334                               (condition-slot-initargs sslot)))
335                  (unless (condition-slot-initform-p found)
336                    (setf (condition-slot-initform-p found)
337                          (condition-slot-initform-p sslot))
338                    (setf (condition-slot-initform found)
339                          (condition-slot-initform sslot)))
340                  (unless (condition-slot-allocation found)
341                    (setf (condition-slot-allocation found)
342                          (condition-slot-allocation sslot))))
343                 (t
344                  (res (copy-structure sslot)))))))
345     (res)))
346
347 (defun %define-condition (name slots documentation report default-initargs)
348   (let ((class (sb!xc:find-class name)))
349     (setf (condition-class-slots class) slots)
350     (setf (condition-class-report class) report)
351     (setf (condition-class-default-initargs class) default-initargs)
352     (setf (fdocumentation name 'type) documentation)
353
354     (dolist (slot slots)
355
356       ;; Set up reader and writer functions.
357       (let ((name (condition-slot-name slot)))
358         (dolist (reader (condition-slot-readers slot))
359           (setf (fdefinition reader)
360                 #'(lambda (condition)
361                     (condition-reader-function condition name))))
362         (dolist (writer (condition-slot-writers slot))
363           (setf (fdefinition writer)
364                 #'(lambda (new-value condition)
365                     (condition-writer-function condition new-value name))))))
366
367     ;; Compute effective slots and set up the class and hairy slots (subsets of
368     ;; the effective slots.)
369     (let ((eslots (compute-effective-slots class))
370           (e-def-initargs
371            (reduce #'append
372                    (mapcar #'condition-class-default-initargs
373                            (condition-class-cpl class)))))
374       (dolist (slot eslots)
375         (ecase (condition-slot-allocation slot)
376           (:class
377            (unless (condition-slot-cell slot)
378              (setf (condition-slot-cell slot)
379                    (list (if (condition-slot-initform-p slot)
380                              (let ((initform (condition-slot-initform slot)))
381                                (if (functionp initform)
382                                    (funcall initform)
383                                    initform))
384                              *empty-slot*))))
385            (push slot (condition-class-class-slots class)))
386           ((:instance nil)
387            (setf (condition-slot-allocation slot) :instance)
388            (when (or (functionp (condition-slot-initform slot))
389                      (dolist (initarg (condition-slot-initargs slot) nil)
390                        (when (functionp (getf e-def-initargs initarg))
391                          (return t))))
392              (push slot (condition-class-hairy-slots class))))))))
393   name)
394
395 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
396                                  &body options)
397   #!+sb-doc
398   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
399    Define NAME as a condition type. This new type inherits slots and its
400    report function from the specified PARENT-TYPEs. A slot spec is a list of:
401      (slot-name :reader <rname> :initarg <iname> {Option Value}*
402
403    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
404    and :TYPE and the overall options :DEFAULT-INITARGS and
405    [type] :DOCUMENTATION are also allowed.
406
407    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
408    a string or a two-argument lambda or function name. If a function, the
409    function is called with the condition and stream to report the condition.
410    If a string, the string is printed.
411
412    Condition types are classes, but (as allowed by ANSI and not as described in
413    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
414    SLOT-VALUE may not be used on condition objects."
415   (let* ((parent-types (or parent-types '(condition)))
416          (layout (find-condition-layout name parent-types))
417          (documentation nil)
418          (report nil)
419          (default-initargs ()))
420     (collect ((slots)
421               (all-readers nil append)
422               (all-writers nil append))
423       (dolist (spec slot-specs)
424         (when (keywordp spec)
425           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
426                 spec))
427         (let* ((spec (if (consp spec) spec (list spec)))
428                (slot-name (first spec))
429                (allocation :instance)
430                (initform-p nil)
431                initform)
432           (collect ((initargs)
433                     (readers)
434                     (writers))
435             (do ((options (rest spec) (cddr options)))
436                 ((null options))
437               (unless (and (consp options) (consp (cdr options)))
438                 (error "malformed condition slot spec:~%  ~S." spec))
439               (let ((arg (second options)))
440                 (case (first options)
441                   (:reader (readers arg))
442                   (:writer (writers arg))
443                   (:accessor
444                    (readers arg)
445                    (writers `(setf ,arg)))
446                   (:initform
447                    (when initform-p
448                      (error "more than one :INITFORM in ~S" spec))
449                    (setq initform-p t)
450                    (setq initform arg))
451                   (:initarg (initargs arg))
452                   (:allocation
453                    (setq allocation arg))
454                   (:type)
455                   (t
456                    (error "unknown slot option:~%  ~S" (first options))))))
457
458             (all-readers (readers))
459             (all-writers (writers))
460             (slots `(make-condition-slot
461                      :name ',slot-name
462                      :initargs ',(initargs)
463                      :readers ',(readers)
464                      :writers ',(writers)
465                      :initform-p ',initform-p
466                      :initform
467                      ,(if (constantp initform)
468                           `',(eval initform)
469                           `#'(lambda () ,initform)))))))
470
471       (dolist (option options)
472         (unless (consp option)
473           (error "bad option:~%  ~S" option))
474         (case (first option)
475           (:documentation (setq documentation (second option)))
476           (:report
477            (let ((arg (second option)))
478              (setq report
479                    (if (stringp arg)
480                        `#'(lambda (condition stream)
481                             (declare (ignore condition))
482                             (write-string ,arg stream))
483                        `#'(lambda (condition stream)
484                             (funcall #',arg condition stream))))))
485           (:default-initargs
486            (do ((initargs (rest option) (cddr initargs)))
487                ((endp initargs))
488              (let ((val (second initargs)))
489                (setq default-initargs
490                      (list* `',(first initargs)
491                             (if (constantp val)
492                                 `',(eval val)
493                                 `#'(lambda () ,val))
494                             default-initargs)))))
495           (t
496            (error "unknown option: ~S" (first option)))))
497
498       (when (all-writers)
499         (warn "Condition slot setters probably not allowed in ANSI CL:~%  ~S"
500               (all-writers)))
501
502       `(progn
503          (eval-when (:compile-toplevel :load-toplevel :execute)
504            (%compiler-define-condition ',name ',parent-types ',layout))
505
506          (declaim (ftype (function (t) t) ,@(all-readers)))
507          (declaim (ftype (function (t t) t) ,@(all-writers)))
508
509          (%define-condition ',name
510                             (list ,@(slots))
511                             ,documentation
512                             ,report
513                             (list ,@default-initargs))))))
514 \f
515 ;;;; various CONDITIONs specified by ANSI
516
517 (define-condition serious-condition (condition)())
518
519 (define-condition error (serious-condition) ())
520
521 (define-condition warning (condition) ())
522 (define-condition style-warning (warning) ())
523
524 (defun simple-condition-printer (condition stream)
525   ;; FIXME: Why use APPLY instead of an ordinary form? To stop the optimizer
526   ;; from doing something?
527   (apply #'format stream (simple-condition-format-control condition)
528                          (simple-condition-format-arguments condition)))
529
530 (define-condition simple-condition ()
531   ((format-control :reader simple-condition-format-control
532                    :initarg :format-control)
533    (format-arguments :reader simple-condition-format-arguments
534                      :initarg :format-arguments
535                      :initform '()))
536   (:report simple-condition-printer))
537
538 (define-condition simple-warning (simple-condition warning) ())
539
540 (defun print-simple-error (condition stream)
541   (format stream
542           "~&~@<error in function ~S: ~3I~:_~?~:>"
543           (condition-function-name condition)
544           (simple-condition-format-control condition)
545           (simple-condition-format-arguments condition)))
546
547 (define-condition simple-error (simple-condition error) ()
548   ;; This is the condition type used by ERROR and CERROR when
549   ;; a format-control string is supplied as the first argument.
550   (:report print-simple-error))
551
552 (define-condition storage-condition (serious-condition) ())
553
554 ;;; FIXME: Should we really be reporting CONDITION-FUNCTION-NAME data on an
555 ;;; ad hoc basis, for some conditions and not others? Why not standardize
556 ;;; it somehow? perhaps by making the debugger report it?
557
558 (define-condition type-error (error)
559   ((datum :reader type-error-datum :initarg :datum)
560    (expected-type :reader type-error-expected-type :initarg :expected-type))
561   (:report
562    (lambda (condition stream)
563      (format stream
564              "~@<TYPE-ERROR in ~S: ~3I~:_~S is not of type ~S~:>."
565              (condition-function-name condition)
566              (type-error-datum condition)
567              (type-error-expected-type condition)))))
568
569 (define-condition program-error (error) ())
570 (define-condition parse-error   (error) ())
571 (define-condition control-error (error) ())
572 (define-condition stream-error  (error)
573   ((stream :reader stream-error-stream :initarg :stream)))
574
575 (define-condition end-of-file (stream-error) ()
576   (:report
577    (lambda (condition stream)
578      (format stream
579              "END-OF-FILE on ~S"
580              (stream-error-stream condition)))))
581
582 (define-condition file-error (error)
583   ((pathname :reader file-error-pathname :initarg :pathname))
584   (:report
585    (lambda (condition stream)
586      (format stream
587              "~&~@<FILE-ERROR in function ~S: ~3i~:_~?~:>"
588              (condition-function-name condition)
589              (serious-condition-format-control condition)
590              (serious-condition-format-arguments condition)))))
591
592 (define-condition package-error (error)
593   ((package :reader package-error-package :initarg :package)))
594
595 (define-condition cell-error (error)
596   ((name :reader cell-error-name :initarg :name)))
597
598 (define-condition unbound-variable (cell-error) ()
599   (:report
600    (lambda (condition stream)
601      (format stream
602              "error in ~S: The variable ~S is unbound."
603              (condition-function-name condition)
604              (cell-error-name condition)))))
605
606 (define-condition undefined-function (cell-error) ()
607   (:report
608    (lambda (condition stream)
609      (format stream
610              "error in ~S: The function ~S is undefined."
611              (condition-function-name condition)
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           :reader floating-point-exception-traps))
677   (:report (lambda (condition stream)
678              (format stream
679                      "An arithmetic error ~S was signalled.~%"
680                      (type-of condition))
681              (let ((traps (floating-point-exception-traps condition)))
682                (if traps
683                    (format stream
684                            "Trapping conditions are: ~%~{ ~S~^~}~%"
685                            traps)
686                    (write-line
687                     "No traps are enabled? How can this be?"
688                     stream))))))
689
690 (define-condition index-too-large-error (type-error)
691   ()
692   (:report
693    (lambda (condition stream)
694      (format stream
695              "error in ~S: ~S: index too large"
696              (condition-function-name condition)
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              "IO-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 %print-namestring-parse-error))
716
717 (define-condition simple-package-error (simple-condition package-error) ())
718
719 (define-condition reader-package-error (reader-error) ())
720
721 (define-condition reader-eof-error (end-of-file)
722   ((context :reader reader-eof-error-context :initarg :context))
723   (:report
724    (lambda (condition stream)
725      (format stream
726              "unexpected EOF on ~S ~A"
727              (stream-error-stream condition)
728              (reader-eof-error-context condition)))))
729 \f
730 ;;;; restart definitions
731
732 (define-condition abort-failure (control-error) ()
733   (:report
734    "An ABORT restart was found that failed to transfer control dynamically."))
735
736 (defun abort (&optional condition)
737   #!+sb-doc
738   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
739    none exists."
740   (invoke-restart (find-restart 'abort condition))
741   ;; ABORT signals an error in case there was a restart named abort that did
742   ;; not transfer control dynamically. This could happen with RESTART-BIND.
743   (error 'abort-failure))
744
745 (defun muffle-warning (&optional condition)
746   #!+sb-doc
747   "Transfer control to a restart named MUFFLE-WARNING, signalling a
748    CONTROL-ERROR if none exists."
749   (invoke-restart (find-restart 'muffle-warning condition)))
750
751 (macrolet ((define-nil-returning-restart (name args doc)
752              #!-sb-doc (declare (ignore doc))
753              `(defun ,name (,@args &optional condition)
754                 #!+sb-doc ,doc
755                 ;; FIXME: Perhaps this shared logic should be pulled out into
756                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
757                 (when (find-restart ',name condition)
758                   (invoke-restart ',name ,@args)))))
759   (define-nil-returning-restart continue ()
760     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
761   (define-nil-returning-restart store-value (value)
762     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
763    none exists.")
764   (define-nil-returning-restart use-value (value)
765     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
766    none exists."))