0.pre8.7
[sbcl.git] / src / code / condition.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
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15 \f
16 ;;;; the CONDITION class
17
18 (/show0 "condition.lisp 20")
19
20 (eval-when (:compile-toplevel :load-toplevel :execute)
21
22 (/show0 "condition.lisp 24")
23
24 (def!struct (condition-classoid (:include slot-classoid)
25                                 (:constructor make-condition-classoid))
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 (/show0 "condition.lisp 49")
46
47 ) ; EVAL-WHEN
48
49 (!defstruct-with-alternate-metaclass condition
50   :slot-names (actual-initargs assigned-slots)
51   :boa-constructor %make-condition-object
52   :superclass-name instance
53   :metaclass-name condition-classoid
54   :metaclass-constructor make-condition-classoid
55   :dd-type structure)
56
57 (defun make-condition-object (actual-initargs)
58   (%make-condition-object actual-initargs nil))
59
60 (defstruct (condition-slot (:copier nil))
61   (name (missing-arg) :type symbol)
62   ;; list of all applicable initargs
63   (initargs (missing-arg) :type list)
64   ;; names of reader and writer functions
65   (readers (missing-arg) :type list)
66   (writers (missing-arg) :type list)
67   ;; true if :INITFORM was specified
68   (initform-p (missing-arg) :type (member t nil))
69   ;; If this is a function, call it with no args. Otherwise, it's the
70   ;; actual value.
71   (initform (missing-arg) :type t)
72   ;; allocation of this slot, or NIL until defaulted
73   (allocation nil :type (member :instance :class nil))
74   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
75   (cell nil :type (or cons null)))
76
77 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
78 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
79 ;;; have themselves listed in their CPLs. This behavior is inherited
80 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
81 ;;; figured out whether it's right. -- WHN 19990612
82 (eval-when (:compile-toplevel :load-toplevel :execute)
83   (/show0 "condition.lisp 103")
84   (let ((condition-class (locally
85                            ;; KLUDGE: There's a DEFTRANSFORM
86                            ;; FIND-CLASSOID for constant class names
87                            ;; which creates fast but
88                            ;; non-cold-loadable, non-compact code. In
89                            ;; this context, we'd rather have compact,
90                            ;; cold-loadable code. -- WHN 19990928
91                            (declare (notinline find-classoid))
92                            (find-classoid 'condition))))
93     (setf (condition-classoid-cpl condition-class)
94           (list condition-class)))
95   (/show0 "condition.lisp 103"))
96
97 (setf (condition-classoid-report (locally
98                                    ;; KLUDGE: There's a DEFTRANSFORM
99                                    ;; FIND-CLASSOID for constant class
100                                    ;; names which creates fast but
101                                    ;; non-cold-loadable, non-compact
102                                    ;; code. In this context, we'd
103                                    ;; rather have compact,
104                                    ;; cold-loadable code. -- WHN
105                                    ;; 19990928
106                                    (declare (notinline find-classoid))
107                                    (find-classoid 'condition)))
108       (lambda (cond stream)
109         (format stream "Condition ~S was signalled." (type-of cond))))
110
111 (eval-when (:compile-toplevel :load-toplevel :execute)
112
113 (defun find-condition-layout (name parent-types)
114   (let* ((cpl (remove-duplicates
115                (reverse
116                 (reduce #'append
117                         (mapcar (lambda (x)
118                                   (condition-classoid-cpl
119                                    (find-classoid x)))
120                                 parent-types)))))
121          (cond-layout (info :type :compiler-layout 'condition))
122          (olayout (info :type :compiler-layout name))
123          ;; FIXME: Does this do the right thing in case of multiple
124          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
125          ;; it obvious what ANSI intends to be done in the case of
126          ;; multiple inheritance, so it's not actually clear what the
127          ;; right thing is..
128          (new-inherits
129           (order-layout-inherits (concatenate 'simple-vector
130                                               (layout-inherits cond-layout)
131                                               (mapcar #'classoid-layout cpl)))))
132     (if (and olayout
133              (not (mismatch (layout-inherits olayout) new-inherits)))
134         olayout
135         (make-layout :classoid (make-undefined-classoid name)
136                      :inherits new-inherits
137                      :depthoid -1
138                      :length (layout-length cond-layout)))))
139
140 ) ; EVAL-WHEN
141
142 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
143 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
144 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
145 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
146 ;;;   a condition type C is equivalent to:
147 ;;;     (defmethod print-object ((x c) stream)
148 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
149 ;;; The current code doesn't seem to quite match that.
150 (def!method print-object ((x condition) stream)
151   (if *print-escape*
152       (print-unreadable-object (x stream :type t :identity t))
153       ;; KLUDGE: A comment from CMU CL here said
154       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
155       ;;   superclasses in define-condition call!
156       (dolist (class (condition-classoid-cpl (classoid-of x))
157                      (error "no REPORT? shouldn't happen!"))
158         (let ((report (condition-classoid-report class)))
159           (when report
160             (return (funcall report x stream)))))))
161 \f
162 ;;;; slots of CONDITION objects
163
164 (defvar *empty-condition-slot* '(empty))
165
166 (defun find-slot-default (class slot)
167   (let ((initargs (condition-slot-initargs slot))
168         (cpl (condition-classoid-cpl class)))
169     (dolist (class cpl)
170       (let ((default-initargs (condition-classoid-default-initargs class)))
171         (dolist (initarg initargs)
172           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
173             (unless (eq val *empty-condition-slot*)
174               (return-from find-slot-default
175                            (if (functionp val)
176                                (funcall val)
177                                val)))))))
178
179     (if (condition-slot-initform-p slot)
180         (let ((initform (condition-slot-initform slot)))
181           (if (functionp initform)
182               (funcall initform)
183               initform))
184         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
185
186 (defun find-condition-class-slot (condition-class slot-name)
187   (dolist (sclass
188            (condition-classoid-cpl condition-class)
189            (error "There is no slot named ~S in ~S."
190                   slot-name condition-class))
191     (dolist (slot (condition-classoid-slots sclass))
192       (when (eq (condition-slot-name slot) slot-name)
193         (return-from find-condition-class-slot slot)))))
194
195 (defun condition-writer-function (condition new-value name)
196   (dolist (cslot (condition-classoid-class-slots
197                   (layout-classoid (%instance-layout condition)))
198                  (setf (getf (condition-assigned-slots condition) name)
199                        new-value))
200     (when (eq (condition-slot-name cslot) name)
201       (return (setf (car (condition-slot-cell cslot)) new-value)))))
202
203 (defun condition-reader-function (condition name)
204   (let ((class (layout-classoid (%instance-layout condition))))
205     (dolist (cslot (condition-classoid-class-slots class))
206       (when (eq (condition-slot-name cslot) name)
207         (return-from condition-reader-function
208                      (car (condition-slot-cell cslot)))))
209
210     (let ((val (getf (condition-assigned-slots condition) name
211                      *empty-condition-slot*)))
212       (if (eq val *empty-condition-slot*)
213           (let ((actual-initargs (condition-actual-initargs condition))
214                 (slot (find-condition-class-slot class name)))
215             (unless slot
216               (error "missing slot ~S of ~S" name condition))
217             (dolist (initarg (condition-slot-initargs slot))
218               (let ((val (getf actual-initargs
219                                initarg
220                                *empty-condition-slot*)))
221                 (unless (eq val *empty-condition-slot*)
222                   (return-from condition-reader-function
223                                (setf (getf (condition-assigned-slots condition)
224                                            name)
225                                      val)))))
226             (setf (getf (condition-assigned-slots condition) name)
227                   (find-slot-default class slot)))
228           val))))
229 \f
230 ;;;; MAKE-CONDITION
231
232 (defun make-condition (thing &rest args)
233   #!+sb-doc
234   "Make an instance of a condition object using the specified initargs."
235   ;; Note: ANSI specifies no exceptional situations in this function.
236   ;; signalling simple-type-error would not be wrong.
237   (let* ((thing (if (symbolp thing)
238                     (find-classoid thing)
239                     thing))
240          (class (typecase thing
241                   (condition-classoid thing)
242                   (classoid
243                    (error 'simple-type-error
244                           :datum thing
245                           :expected-type 'condition-class
246                           :format-control "~S is not a condition class."
247                           :format-arguments (list thing)))
248                   (t
249                    (error 'simple-type-error
250                           :datum thing
251                           :expected-type 'condition-class
252                           :format-control "bad thing for class argument:~%  ~S"
253                           :format-arguments (list thing)))))
254          (res (make-condition-object args)))
255     (setf (%instance-layout res) (classoid-layout class))
256     ;; Set any class slots with initargs present in this call.
257     (dolist (cslot (condition-classoid-class-slots class))
258       (dolist (initarg (condition-slot-initargs cslot))
259         (let ((val (getf args initarg *empty-condition-slot*)))
260           (unless (eq val *empty-condition-slot*)
261             (setf (car (condition-slot-cell cslot)) val)))))
262     ;; Default any slots with non-constant defaults now.
263     (dolist (hslot (condition-classoid-hairy-slots class))
264       (when (dolist (initarg (condition-slot-initargs hslot) t)
265               (unless (eq (getf args initarg *empty-condition-slot*)
266                           *empty-condition-slot*)
267                 (return nil)))
268         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
269               (find-slot-default class hslot))))
270
271     res))
272 \f
273 ;;;; DEFINE-CONDITION
274
275 (eval-when (:compile-toplevel :load-toplevel :execute)
276 (defun %compiler-define-condition (name direct-supers layout)
277   (multiple-value-bind (class old-layout)
278       (insured-find-classoid name
279                              #'condition-classoid-p
280                              #'make-condition-classoid)
281     (setf (layout-classoid layout) class)
282     (setf (classoid-direct-superclasses class)
283           (mapcar #'find-classoid direct-supers))
284     (cond ((not old-layout)
285            (register-layout layout))
286           ((not *type-system-initialized*)
287            (setf (layout-classoid old-layout) class)
288            (setq layout old-layout)
289            (unless (eq (classoid-layout class) layout)
290              (register-layout layout)))
291           ((redefine-layout-warning "current"
292                                     old-layout
293                                     "new"
294                                     (layout-length layout)
295                                     (layout-inherits layout)
296                                     (layout-depthoid layout))
297            (register-layout layout :invalidate t))
298           ((not (classoid-layout class))
299            (register-layout layout)))
300
301     (setf (layout-info layout)
302           (locally
303             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
304             ;; names which creates fast but non-cold-loadable, non-compact
305             ;; code. In this context, we'd rather have compact, cold-loadable
306             ;; code. -- WHN 19990928
307             (declare (notinline find-classoid))
308             (layout-info (classoid-layout (find-classoid 'condition)))))
309
310     (setf (find-classoid name) class)
311
312     ;; Initialize CPL slot.
313     (setf (condition-classoid-cpl class)
314           (remove-if-not #'condition-classoid-p 
315                          (std-compute-class-precedence-list class))))
316   (values))
317
318 ) ; EVAL-WHEN
319
320 ;;; Compute the effective slots of CLASS, copying inherited slots and
321 ;;; destructively modifying direct slots.
322 ;;;
323 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
324 ;;; direct slots. Presumably it follows from the semantics of
325 ;;; inheritance and redefinition of conditions, but finding the cite
326 ;;; and documenting it here would be good. (Or, if this is not in fact
327 ;;; ANSI-compliant, fixing it would also be good.:-)
328 (defun compute-effective-slots (class)
329   (collect ((res (copy-list (condition-classoid-slots class))))
330     (dolist (sclass (condition-classoid-cpl class))
331       (dolist (sslot (condition-classoid-slots sclass))
332         (let ((found (find (condition-slot-name sslot) (res))))
333           (cond (found
334                  (setf (condition-slot-initargs found)
335                        (union (condition-slot-initargs found)
336                               (condition-slot-initargs sslot)))
337                  (unless (condition-slot-initform-p found)
338                    (setf (condition-slot-initform-p found)
339                          (condition-slot-initform-p sslot))
340                    (setf (condition-slot-initform found)
341                          (condition-slot-initform sslot)))
342                  (unless (condition-slot-allocation found)
343                    (setf (condition-slot-allocation found)
344                          (condition-slot-allocation sslot))))
345                 (t
346                  (res (copy-structure sslot)))))))
347     (res)))
348
349 (defun %define-condition (name slots documentation report default-initargs)
350   (let ((class (find-classoid name)))
351     (setf (condition-classoid-slots class) slots)
352     (setf (condition-classoid-report class) report)
353     (setf (condition-classoid-default-initargs class) default-initargs)
354     (setf (fdocumentation name 'type) documentation)
355
356     (dolist (slot slots)
357
358       ;; Set up reader and writer functions.
359       (let ((name (condition-slot-name slot)))
360         (dolist (reader (condition-slot-readers slot))
361           (setf (fdefinition reader)
362                 (lambda (condition)
363                   (condition-reader-function condition name))))
364         (dolist (writer (condition-slot-writers slot))
365           (setf (fdefinition writer)
366                 (lambda (new-value condition)
367                   (condition-writer-function condition new-value name))))))
368
369     ;; Compute effective slots and set up the class and hairy slots
370     ;; (subsets of the effective slots.)
371     (let ((eslots (compute-effective-slots class))
372           (e-def-initargs
373            (reduce #'append
374                    (mapcar #'condition-classoid-default-initargs
375                            (condition-classoid-cpl class)))))
376       (dolist (slot eslots)
377         (ecase (condition-slot-allocation slot)
378           (:class
379            (unless (condition-slot-cell slot)
380              (setf (condition-slot-cell slot)
381                    (list (if (condition-slot-initform-p slot)
382                              (let ((initform (condition-slot-initform slot)))
383                                (if (functionp initform)
384                                    (funcall initform)
385                                    initform))
386                              *empty-condition-slot*))))
387            (push slot (condition-classoid-class-slots class)))
388           ((:instance nil)
389            (setf (condition-slot-allocation slot) :instance)
390            (when (or (functionp (condition-slot-initform slot))
391                      (dolist (initarg (condition-slot-initargs slot) nil)
392                        (when (functionp (getf e-def-initargs initarg))
393                          (return t))))
394              (push slot (condition-classoid-hairy-slots class))))))))
395   name)
396
397 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
398                                  &body options)
399   #!+sb-doc
400   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
401    Define NAME as a condition type. This new type inherits slots and its
402    report function from the specified PARENT-TYPEs. A slot spec is a list of:
403      (slot-name :reader <rname> :initarg <iname> {Option Value}*
404
405    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
406    and :TYPE and the overall options :DEFAULT-INITARGS and
407    [type] :DOCUMENTATION are also allowed.
408
409    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
410    a string or a two-argument lambda or function name. If a function, the
411    function is called with the condition and stream to report the condition.
412    If a string, the string is printed.
413
414    Condition types are classes, but (as allowed by ANSI and not as described in
415    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
416    SLOT-VALUE may not be used on condition objects."
417   (let* ((parent-types (or parent-types '(condition)))
418          (layout (find-condition-layout name parent-types))
419          (documentation nil)
420          (report nil)
421          (default-initargs ()))
422     (collect ((slots)
423               (all-readers nil append)
424               (all-writers nil append))
425       (dolist (spec slot-specs)
426         (when (keywordp spec)
427           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
428                 spec))
429         (let* ((spec (if (consp spec) spec (list spec)))
430                (slot-name (first spec))
431                (allocation :instance)
432                (initform-p nil)
433                initform)
434           (collect ((initargs)
435                     (readers)
436                     (writers))
437             (do ((options (rest spec) (cddr options)))
438                 ((null options))
439               (unless (and (consp options) (consp (cdr options)))
440                 (error "malformed condition slot spec:~%  ~S." spec))
441               (let ((arg (second options)))
442                 (case (first options)
443                   (:reader (readers arg))
444                   (:writer (writers arg))
445                   (:accessor
446                    (readers arg)
447                    (writers `(setf ,arg)))
448                   (:initform
449                    (when initform-p
450                      (error "more than one :INITFORM in ~S" spec))
451                    (setq initform-p t)
452                    (setq initform arg))
453                   (:initarg (initargs arg))
454                   (:allocation
455                    (setq allocation arg))
456                   (:type)
457                   (t
458                    (error "unknown slot option:~%  ~S" (first options))))))
459
460             (all-readers (readers))
461             (all-writers (writers))
462             (slots `(make-condition-slot
463                      :name ',slot-name
464                      :initargs ',(initargs)
465                      :readers ',(readers)
466                      :writers ',(writers)
467                      :initform-p ',initform-p
468                      :initform
469                      ,(if (constantp initform)
470                           `',(eval initform)
471                           `#'(lambda () ,initform)))))))
472
473       (dolist (option options)
474         (unless (consp option)
475           (error "bad option:~%  ~S" option))
476         (case (first option)
477           (:documentation (setq documentation (second option)))
478           (:report
479            (let ((arg (second option)))
480              (setq report
481                    (if (stringp arg)
482                        `#'(lambda (condition stream)
483                           (declare (ignore condition))
484                           (write-string ,arg stream))
485                        `#'(lambda (condition stream)
486                           (funcall #',arg condition stream))))))
487           (:default-initargs
488            (do ((initargs (rest option) (cddr initargs)))
489                ((endp initargs))
490              (let ((val (second initargs)))
491                (setq default-initargs
492                      (list* `',(first initargs)
493                             (if (constantp val)
494                                 `',(eval val)
495                                 `#'(lambda () ,val))
496                             default-initargs)))))
497           (t
498            (error "unknown option: ~S" (first option)))))
499
500       `(progn
501          (eval-when (:compile-toplevel :load-toplevel :execute)
502            (%compiler-define-condition ',name ',parent-types ',layout))
503
504          (declaim (ftype (function (t) t) ,@(all-readers)))
505          (declaim (ftype (function (t t) t) ,@(all-writers)))
506
507          (%define-condition ',name
508                             (list ,@(slots))
509                             ,documentation
510                             ,report
511                             (list ,@default-initargs))))))
512 \f
513 ;;;; DESCRIBE on CONDITIONs
514
515 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
516 ;;; eventually (once we get CLOS up and running so that we can define
517 ;;; methods)
518 (defun describe-condition (condition stream)
519   (format stream
520           "~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>"
521           condition
522           (type-of condition)
523           (concatenate 'list
524                        (condition-actual-initargs condition)
525                        (condition-assigned-slots condition))))
526 \f
527 ;;;; various CONDITIONs specified by ANSI
528
529 (define-condition serious-condition (condition) ())
530
531 (define-condition error (serious-condition) ())
532
533 (define-condition warning (condition) ())
534 (define-condition style-warning (warning) ())
535
536 (defun simple-condition-printer (condition stream)
537   (apply #'format
538          stream
539          (simple-condition-format-control condition)
540          (simple-condition-format-arguments condition)))
541
542 (define-condition simple-condition ()
543   ((format-control :reader simple-condition-format-control
544                    :initarg :format-control)
545    (format-arguments :reader simple-condition-format-arguments
546                      :initarg :format-arguments
547                      :initform '()))
548   (:report simple-condition-printer))
549
550 (define-condition simple-warning (simple-condition warning) ())
551
552 (define-condition simple-error (simple-condition error) ())
553
554 (define-condition storage-condition (serious-condition) ())
555
556 (define-condition type-error (error)
557   ((datum :reader type-error-datum :initarg :datum)
558    (expected-type :reader type-error-expected-type :initarg :expected-type))
559   (:report
560    (lambda (condition stream)
561      (format stream
562              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
563              (type-error-datum condition)
564              (type-error-expected-type condition)))))
565
566 (define-condition simple-type-error (simple-condition type-error) ())
567
568 (define-condition program-error (error) ())
569 (define-condition parse-error   (error) ())
570 (define-condition control-error (error) ())
571 (define-condition stream-error  (error)
572   ((stream :reader stream-error-stream :initarg :stream)))
573
574 (define-condition end-of-file (stream-error) ()
575   (:report
576    (lambda (condition stream)
577      (format stream
578              "end of file on ~S"
579              (stream-error-stream condition)))))
580
581 (define-condition file-error (error)
582   ((pathname :reader file-error-pathname :initarg :pathname))
583   (:report
584    (lambda (condition stream)
585      (format stream "error on file ~S" (file-error-pathname condition)))))
586
587 (define-condition package-error (error)
588   ((package :reader package-error-package :initarg :package)))
589
590 (define-condition cell-error (error)
591   ((name :reader cell-error-name :initarg :name)))
592
593 (define-condition unbound-variable (cell-error) ()
594   (:report
595    (lambda (condition stream)
596      (format stream
597              "The variable ~S is unbound."
598              (cell-error-name condition)))))
599
600 (define-condition undefined-function (cell-error) ()
601   (:report
602    (lambda (condition stream)
603      (format stream
604              "The function ~S is undefined."
605              (cell-error-name condition)))))
606
607 (define-condition special-form-function (undefined-function) ()
608   (:report
609    (lambda (condition stream)
610      (format stream
611              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
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 ~W ~]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           :initform nil
677           :reader floating-point-exception-traps))
678   (:report (lambda (condition stream)
679              (format stream
680                      "An arithmetic error ~S was signalled.~%"
681                      (type-of condition))
682              (let ((traps (floating-point-exception-traps condition)))
683                (if traps
684                    (format stream
685                            "Trapping conditions are: ~%~{ ~S~^~}~%"
686                            traps)
687                    (write-line
688                     "No traps are enabled? How can this be?"
689                     stream))))))
690
691 (define-condition index-too-large-error (type-error)
692   ()
693   (:report
694    (lambda (condition stream)
695      (format stream
696              "The index ~S is too large."
697              (type-error-datum condition)))))
698
699 (define-condition bounding-indices-bad-error (type-error)
700   ((object :reader bounding-indices-bad-object :initarg :object))
701   (:report
702    (lambda (condition stream)
703      (let* ((datum (type-error-datum condition))
704             (start (car datum))
705             (end (cdr datum))
706             (object (bounding-indices-bad-object condition)))
707        (etypecase object
708          (sequence
709           (format stream
710                   "The bounding indices ~S and ~S are bad for a sequence of length ~S."
711                   start end (length object)))
712          (array
713           ;; from WITH-ARRAY-DATA
714           (format stream
715                   "The START and END parameters ~S and ~S are bad for an array of total size ~S."
716                   start end (array-total-size object))))))))
717
718 (define-condition nil-array-accessed-error (type-error)
719   ()
720   (:report (lambda (condition stream)
721              (format stream
722                      "An attempt to access an array of element-type ~
723                       NIL was made.  Congratulations!"))))
724
725 (define-condition io-timeout (stream-error)
726   ((direction :reader io-timeout-direction :initarg :direction))
727   (:report
728    (lambda (condition stream)
729      (declare (type stream stream))
730      (format stream
731              "I/O timeout ~(~A~)ing ~S"
732              (io-timeout-direction condition)
733              (stream-error-stream condition)))))
734
735 (define-condition namestring-parse-error (parse-error)
736   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
737    (args :reader namestring-parse-error-args :initarg :args :initform nil)
738    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
739    (offset :reader namestring-parse-error-offset :initarg :offset))
740   (:report
741    (lambda (condition stream)
742      (format stream
743              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
744              (namestring-parse-error-complaint condition)
745              (namestring-parse-error-args condition)
746              (namestring-parse-error-namestring condition)
747              (namestring-parse-error-offset condition)))))
748
749 (define-condition simple-package-error (simple-condition package-error) ())
750
751 (define-condition reader-package-error (reader-error) ())
752
753 (define-condition reader-eof-error (end-of-file)
754   ((context :reader reader-eof-error-context :initarg :context))
755   (:report
756    (lambda (condition stream)
757      (format stream
758              "unexpected end of file on ~S ~A"
759              (stream-error-stream condition)
760              (reader-eof-error-context condition)))))
761
762 (define-condition reader-impossible-number-error (reader-error)
763   ((error :reader reader-impossible-number-error-error :initarg :error))
764   (:report
765    (lambda (condition stream)
766      (let ((error-stream (stream-error-stream condition)))
767        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
768                (file-position error-stream) error-stream
769                (reader-error-format-control condition)
770                (reader-error-format-arguments condition)
771                (reader-impossible-number-error-error condition))))))
772
773 ;;; should this inherit from error?  good question
774 (define-condition timeout (error) ())
775
776
777 \f
778 ;;;; special SBCL extension conditions
779
780 ;;; an error apparently caused by a bug in SBCL itself
781 ;;;
782 ;;; Note that we don't make any serious effort to use this condition
783 ;;; for *all* errors in SBCL itself. E.g. type errors and array
784 ;;; indexing errors can occur in functions called from SBCL code, and
785 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
786 ;;; because the signalling code has no good way to know that the
787 ;;; underlying problem is a bug in SBCL. But in the fairly common case
788 ;;; that the signalling code does know that it's found a bug in SBCL,
789 ;;; this condition is appropriate, reusing boilerplate and helping
790 ;;; users to recognize it as an SBCL bug.
791 (define-condition bug (simple-error)
792   ()
793   (:report
794    (lambda (condition stream)
795      (format stream
796              "~@<  ~? ~:@_~?~:>"
797              (simple-condition-format-control condition)
798              (simple-condition-format-arguments condition)
799              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
800               SBCL might have been corrupted by bad user code, e.g. by an ~
801               undefined Lisp operation like ~S, or by stray pointers from ~
802               alien code or from unsafe Lisp code; or there might be a bug ~
803               in the OS or hardware that SBCL is running on.) If it seems to ~
804               be a bug in SBCL itself, the maintainers would like to know ~
805               about it. Bug reports are welcome on the SBCL ~
806               mailing lists, which you can find at ~
807               <http://sbcl.sourceforge.net/>.~:@>"
808              '((fmakunbound 'compile))))))
809 (defun bug (format-control &rest format-arguments)
810   (error 'bug
811          :format-control format-control
812          :format-arguments format-arguments))
813
814 ;;; a condition for use in stubs for operations which aren't supported
815 ;;; on some platforms
816 ;;;
817 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
818 ;;;   #-(or freebsd linux)
819 ;;;   (defun load-foreign (&rest rest)
820 ;;;     (error 'unsupported-operator :name 'load-foreign))
821 ;;;   #+(or freebsd linux)
822 ;;;   (defun load-foreign ... actual definition ...)
823 ;;; By signalling a standard condition in this case, we make it
824 ;;; possible for test code to distinguish between (1) intentionally
825 ;;; unimplemented and (2) unintentionally just screwed up somehow.
826 ;;; (Before this condition was defined, test code tried to deal with 
827 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
828 ;;; sbcl-0.7.0, a a package screwup left the definition of
829 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
830 ;;; architectures where it was supposed to be supported, and the
831 ;;; regression tests cheerfully passed because they assumed that
832 ;;; unFBOUNDPness meant they were running on an system which didn't
833 ;;; support the extension.)
834 (define-condition unsupported-operator (cell-error) ()
835   (:report
836    (lambda (condition stream)
837      (format stream
838              "unsupported on this platform (OS, CPU, whatever): ~S"
839              (cell-error-name condition)))))
840 \f
841 ;;;; restart definitions
842
843 (define-condition abort-failure (control-error) ()
844   (:report
845    "An ABORT restart was found that failed to transfer control dynamically."))
846
847 (defun abort (&optional condition)
848   #!+sb-doc
849   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
850    none exists."
851   (invoke-restart (find-restart 'abort condition))
852   ;; ABORT signals an error in case there was a restart named ABORT
853   ;; that did not transfer control dynamically. This could happen with
854   ;; RESTART-BIND.
855   (error 'abort-failure))
856
857 (defun muffle-warning (&optional condition)
858   #!+sb-doc
859   "Transfer control to a restart named MUFFLE-WARNING, signalling a
860    CONTROL-ERROR if none exists."
861   (invoke-restart (find-restart 'muffle-warning condition)))
862
863 (macrolet ((define-nil-returning-restart (name args doc)
864              #!-sb-doc (declare (ignore doc))
865              `(defun ,name (,@args &optional condition)
866                 #!+sb-doc ,doc
867                 ;; FIXME: Perhaps this shared logic should be pulled out into
868                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
869                 (when (find-restart ',name condition)
870                   (invoke-restart ',name ,@args)))))
871   (define-nil-returning-restart continue ()
872     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
873   (define-nil-returning-restart store-value (value)
874     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
875    none exists.")
876   (define-nil-returning-restart use-value (value)
877     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
878    none exists."))
879
880 (/show0 "condition.lisp end of file")
881