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