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