0.8.1.12:
[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   ;; slot documentation
77   (documentation nil :type (or string null)))
78
79 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
80 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
81 ;;; have themselves listed in their CPLs. This behavior is inherited
82 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
83 ;;; figured out whether it's right. -- WHN 19990612
84 (eval-when (:compile-toplevel :load-toplevel :execute)
85   (/show0 "condition.lisp 103")
86   (let ((condition-class (locally
87                            ;; KLUDGE: There's a DEFTRANSFORM
88                            ;; FIND-CLASSOID for constant class names
89                            ;; which creates fast but
90                            ;; non-cold-loadable, non-compact code. In
91                            ;; this context, we'd rather have compact,
92                            ;; cold-loadable code. -- WHN 19990928
93                            (declare (notinline find-classoid))
94                            (find-classoid 'condition))))
95     (setf (condition-classoid-cpl condition-class)
96           (list condition-class)))
97   (/show0 "condition.lisp 103"))
98
99 (setf (condition-classoid-report (locally
100                                    ;; KLUDGE: There's a DEFTRANSFORM
101                                    ;; FIND-CLASSOID for constant class
102                                    ;; names which creates fast but
103                                    ;; non-cold-loadable, non-compact
104                                    ;; code. In this context, we'd
105                                    ;; rather have compact,
106                                    ;; cold-loadable code. -- WHN
107                                    ;; 19990928
108                                    (declare (notinline find-classoid))
109                                    (find-classoid 'condition)))
110       (lambda (cond stream)
111         (format stream "Condition ~S was signalled." (type-of cond))))
112
113 (eval-when (:compile-toplevel :load-toplevel :execute)
114
115 (defun find-condition-layout (name parent-types)
116   (let* ((cpl (remove-duplicates
117                (reverse
118                 (reduce #'append
119                         (mapcar (lambda (x)
120                                   (condition-classoid-cpl
121                                    (find-classoid x)))
122                                 parent-types)))))
123          (cond-layout (info :type :compiler-layout 'condition))
124          (olayout (info :type :compiler-layout name))
125          ;; FIXME: Does this do the right thing in case of multiple
126          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
127          ;; it obvious what ANSI intends to be done in the case of
128          ;; multiple inheritance, so it's not actually clear what the
129          ;; right thing is..
130          (new-inherits
131           (order-layout-inherits (concatenate 'simple-vector
132                                               (layout-inherits cond-layout)
133                                               (mapcar #'classoid-layout cpl)))))
134     (if (and olayout
135              (not (mismatch (layout-inherits olayout) new-inherits)))
136         olayout
137         (make-layout :classoid (make-undefined-classoid name)
138                      :inherits new-inherits
139                      :depthoid -1
140                      :length (layout-length cond-layout)))))
141
142 ) ; EVAL-WHEN
143
144 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
145 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
146 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
147 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
148 ;;;   a condition type C is equivalent to:
149 ;;;     (defmethod print-object ((x c) stream)
150 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
151 ;;; The current code doesn't seem to quite match that.
152 (def!method print-object ((x condition) stream)
153   (if *print-escape*
154       (print-unreadable-object (x stream :type t :identity t))
155       ;; KLUDGE: A comment from CMU CL here said
156       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
157       ;;   superclasses in define-condition call!
158       (dolist (class (condition-classoid-cpl (classoid-of x))
159                      (error "no REPORT? shouldn't happen!"))
160         (let ((report (condition-classoid-report class)))
161           (when report
162             (return (funcall report x stream)))))))
163 \f
164 ;;;; slots of CONDITION objects
165
166 (defvar *empty-condition-slot* '(empty))
167
168 (defun find-slot-default (class slot)
169   (let ((initargs (condition-slot-initargs slot))
170         (cpl (condition-classoid-cpl class)))
171     (dolist (class cpl)
172       (let ((default-initargs (condition-classoid-default-initargs class)))
173         (dolist (initarg initargs)
174           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
175             (unless (eq val *empty-condition-slot*)
176               (return-from find-slot-default
177                            (if (functionp val)
178                                (funcall val)
179                                val)))))))
180
181     (if (condition-slot-initform-p slot)
182         (let ((initform (condition-slot-initform slot)))
183           (if (functionp initform)
184               (funcall initform)
185               initform))
186         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
187
188 (defun find-condition-class-slot (condition-class slot-name)
189   (dolist (sclass
190            (condition-classoid-cpl condition-class)
191            (error "There is no slot named ~S in ~S."
192                   slot-name condition-class))
193     (dolist (slot (condition-classoid-slots sclass))
194       (when (eq (condition-slot-name slot) slot-name)
195         (return-from find-condition-class-slot slot)))))
196
197 (defun condition-writer-function (condition new-value name)
198   (dolist (cslot (condition-classoid-class-slots
199                   (layout-classoid (%instance-layout condition)))
200                  (setf (getf (condition-assigned-slots condition) name)
201                        new-value))
202     (when (eq (condition-slot-name cslot) name)
203       (return (setf (car (condition-slot-cell cslot)) new-value)))))
204
205 (defun condition-reader-function (condition name)
206   (let ((class (layout-classoid (%instance-layout condition))))
207     (dolist (cslot (condition-classoid-class-slots class))
208       (when (eq (condition-slot-name cslot) name)
209         (return-from condition-reader-function
210                      (car (condition-slot-cell cslot)))))
211     (let ((val (getf (condition-assigned-slots condition) name
212                      *empty-condition-slot*)))
213       (if (eq val *empty-condition-slot*)
214           (let ((actual-initargs (condition-actual-initargs condition))
215                 (slot (find-condition-class-slot class name)))
216             (unless slot
217               (error "missing slot ~S of ~S" name condition))
218             (do ((initargs actual-initargs (cddr initargs)))
219                 ((endp initargs)
220                  (setf (getf (condition-assigned-slots condition) name)
221                        (find-slot-default class slot)))
222               (when (member (car initargs) (condition-slot-initargs slot))
223                 (return-from condition-reader-function
224                   (setf (getf (condition-assigned-slots condition)
225                               name)
226                         (cadr initargs))))))
227           val))))
228 \f
229 ;;;; MAKE-CONDITION
230
231 (defun make-condition (thing &rest args)
232   #!+sb-doc
233   "Make an instance of a condition object using the specified initargs."
234   ;; Note: ANSI specifies no exceptional situations in this function.
235   ;; signalling simple-type-error would not be wrong.
236   (let* ((thing (if (symbolp thing)
237                     (find-classoid thing)
238                     thing))
239          (class (typecase thing
240                   (condition-classoid thing)
241                   (classoid
242                    (error 'simple-type-error
243                           :datum thing
244                           :expected-type 'condition-class
245                           :format-control "~S is not a condition class."
246                           :format-arguments (list thing)))
247                   (t
248                    (error 'simple-type-error
249                           :datum thing
250                           :expected-type 'condition-class
251                           :format-control "bad thing for class argument:~%  ~S"
252                           :format-arguments (list thing)))))
253          (res (make-condition-object args)))
254     (setf (%instance-layout res) (classoid-layout class))
255     ;; Set any class slots with initargs present in this call.
256     (dolist (cslot (condition-classoid-class-slots class))
257       (dolist (initarg (condition-slot-initargs cslot))
258         (let ((val (getf args initarg *empty-condition-slot*)))
259           (unless (eq val *empty-condition-slot*)
260             (setf (car (condition-slot-cell cslot)) val)))))
261     ;; Default any slots with non-constant defaults now.
262     (dolist (hslot (condition-classoid-hairy-slots class))
263       (when (dolist (initarg (condition-slot-initargs hslot) t)
264               (unless (eq (getf args initarg *empty-condition-slot*)
265                           *empty-condition-slot*)
266                 (return nil)))
267         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
268               (find-slot-default class hslot))))
269
270     res))
271 \f
272 ;;;; DEFINE-CONDITION
273
274 (eval-when (:compile-toplevel :load-toplevel :execute)
275 (defun %compiler-define-condition (name direct-supers layout)
276   (multiple-value-bind (class old-layout)
277       (insured-find-classoid name
278                              #'condition-classoid-p
279                              #'make-condition-classoid)
280     (setf (layout-classoid layout) class)
281     (setf (classoid-direct-superclasses class)
282           (mapcar #'find-classoid direct-supers))
283     (cond ((not old-layout)
284            (register-layout layout))
285           ((not *type-system-initialized*)
286            (setf (layout-classoid old-layout) class)
287            (setq layout old-layout)
288            (unless (eq (classoid-layout class) layout)
289              (register-layout layout)))
290           ((redefine-layout-warning "current"
291                                     old-layout
292                                     "new"
293                                     (layout-length layout)
294                                     (layout-inherits layout)
295                                     (layout-depthoid layout))
296            (register-layout layout :invalidate t))
297           ((not (classoid-layout class))
298            (register-layout layout)))
299
300     (setf (layout-info layout)
301           (locally
302             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
303             ;; names which creates fast but non-cold-loadable, non-compact
304             ;; code. In this context, we'd rather have compact, cold-loadable
305             ;; code. -- WHN 19990928
306             (declare (notinline find-classoid))
307             (layout-info (classoid-layout (find-classoid 'condition)))))
308
309     (setf (find-classoid name) class)
310
311     ;; Initialize CPL slot.
312     (setf (condition-classoid-cpl class)
313           (remove-if-not #'condition-classoid-p 
314                          (std-compute-class-precedence-list class))))
315   (values))
316
317 ) ; EVAL-WHEN
318
319 ;;; Compute the effective slots of CLASS, copying inherited slots and
320 ;;; destructively modifying direct slots.
321 ;;;
322 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
323 ;;; direct slots. Presumably it follows from the semantics of
324 ;;; inheritance and redefinition of conditions, but finding the cite
325 ;;; and documenting it here would be good. (Or, if this is not in fact
326 ;;; ANSI-compliant, fixing it would also be good.:-)
327 (defun compute-effective-slots (class)
328   (collect ((res (copy-list (condition-classoid-slots class))))
329     (dolist (sclass (cdr (condition-classoid-cpl class)))
330       (dolist (sslot (condition-classoid-slots sclass))
331         (let ((found (find (condition-slot-name sslot) (res)
332                            :key #'condition-slot-name)))
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                documentation
434                initform)
435           (collect ((initargs)
436                     (readers)
437                     (writers))
438             (do ((options (rest spec) (cddr options)))
439                 ((null options))
440               (unless (and (consp options) (consp (cdr options)))
441                 (error "malformed condition slot spec:~%  ~S." spec))
442               (let ((arg (second options)))
443                 (case (first options)
444                   (:reader (readers arg))
445                   (:writer (writers arg))
446                   (:accessor
447                    (readers arg)
448                    (writers `(setf ,arg)))
449                   (:initform
450                    (when initform-p
451                      (error "more than one :INITFORM in ~S" spec))
452                    (setq initform-p t)
453                    (setq initform arg))
454                   (:initarg (initargs arg))
455                   (:allocation
456                    (setq allocation arg))
457                   (:documentation
458                    (when documentation
459                      (error "more than one :DOCUMENTATION in ~S" spec))
460                    (unless (stringp arg)
461                      (error "slot :DOCUMENTATION argument is not a string: ~S"
462                             arg))
463                    (setq documentation arg))
464                   (:type)
465                   (t
466                    (error "unknown slot option:~%  ~S" (first options))))))
467
468             (all-readers (readers))
469             (all-writers (writers))
470             (slots `(make-condition-slot
471                      :name ',slot-name
472                      :initargs ',(initargs)
473                      :readers ',(readers)
474                      :writers ',(writers)
475                      :initform-p ',initform-p
476                      :documentation ',documentation
477                      :initform
478                      ,(if (constantp initform)
479                           `',(eval initform)
480                           `#'(lambda () ,initform)))))))
481
482       (dolist (option options)
483         (unless (consp option)
484           (error "bad option:~%  ~S" option))
485         (case (first option)
486           (:documentation (setq documentation (second option)))
487           (:report
488            (let ((arg (second option)))
489              (setq report
490                    (if (stringp arg)
491                        `#'(lambda (condition stream)
492                           (declare (ignore condition))
493                           (write-string ,arg stream))
494                        `#'(lambda (condition stream)
495                           (funcall #',arg condition stream))))))
496           (:default-initargs
497            (do ((initargs (rest option) (cddr initargs)))
498                ((endp initargs))
499              (let ((val (second initargs)))
500                (setq default-initargs
501                      (list* `',(first initargs)
502                             (if (constantp val)
503                                 `',(eval val)
504                                 `#'(lambda () ,val))
505                             default-initargs)))))
506           (t
507            (error "unknown option: ~S" (first option)))))
508
509       `(progn
510          (eval-when (:compile-toplevel :load-toplevel :execute)
511            (%compiler-define-condition ',name ',parent-types ',layout))
512
513          (declaim (ftype (function (t) t) ,@(all-readers)))
514          (declaim (ftype (function (t t) t) ,@(all-writers)))
515
516          (%define-condition ',name
517                             (list ,@(slots))
518                             ,documentation
519                             ,report
520                             (list ,@default-initargs))))))
521 \f
522 ;;;; DESCRIBE on CONDITIONs
523
524 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
525 ;;; eventually (once we get CLOS up and running so that we can define
526 ;;; methods)
527 (defun describe-condition (condition stream)
528   (format stream
529           "~&~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>~%"
530           condition
531           (type-of condition)
532           (concatenate 'list
533                        (condition-actual-initargs condition)
534                        (condition-assigned-slots condition))))
535 \f
536 ;;;; various CONDITIONs specified by ANSI
537
538 (define-condition serious-condition (condition) ())
539
540 (define-condition error (serious-condition) ())
541
542 (define-condition warning (condition) ())
543 (define-condition style-warning (warning) ())
544
545 (defun simple-condition-printer (condition stream)
546   (apply #'format
547          stream
548          (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 (define-condition simple-error (simple-condition error) ())
562
563 (define-condition storage-condition (serious-condition) ())
564
565 (define-condition type-error (error)
566   ((datum :reader type-error-datum :initarg :datum)
567    (expected-type :reader type-error-expected-type :initarg :expected-type))
568   (:report
569    (lambda (condition stream)
570      (format stream
571              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
572              (type-error-datum condition)
573              (type-error-expected-type condition)))))
574
575 (define-condition simple-type-error (simple-condition type-error) ())
576
577 (define-condition program-error (error) ())
578 (define-condition parse-error   (error) ())
579 (define-condition control-error (error) ())
580 (define-condition stream-error  (error)
581   ((stream :reader stream-error-stream :initarg :stream)))
582
583 (define-condition end-of-file (stream-error) ()
584   (:report
585    (lambda (condition stream)
586      (format stream
587              "end of file on ~S"
588              (stream-error-stream condition)))))
589
590 (define-condition file-error (error)
591   ((pathname :reader file-error-pathname :initarg :pathname))
592   (:report
593    (lambda (condition stream)
594      (format stream "error on file ~S" (file-error-pathname condition)))))
595
596 (define-condition package-error (error)
597   ((package :reader package-error-package :initarg :package)))
598
599 (define-condition cell-error (error)
600   ((name :reader cell-error-name :initarg :name)))
601
602 (define-condition unbound-variable (cell-error) ()
603   (:report
604    (lambda (condition stream)
605      (format stream
606              "The variable ~S is unbound."
607              (cell-error-name condition)))))
608
609 (define-condition undefined-function (cell-error) ()
610   (:report
611    (lambda (condition stream)
612      (format stream
613              "The function ~S is undefined."
614              (cell-error-name condition)))))
615
616 (define-condition special-form-function (undefined-function) ()
617   (:report
618    (lambda (condition stream)
619      (format stream
620              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
621              (cell-error-name condition)))))
622
623 (define-condition arithmetic-error (error)
624   ((operation :reader arithmetic-error-operation
625               :initarg :operation
626               :initform nil)
627    (operands :reader arithmetic-error-operands
628              :initarg :operands))
629   (:report (lambda (condition stream)
630              (format stream
631                      "arithmetic error ~S signalled"
632                      (type-of condition))
633              (when (arithmetic-error-operation condition)
634                (format stream
635                        "~%Operation was ~S, operands ~S."
636                        (arithmetic-error-operation condition)
637                        (arithmetic-error-operands condition))))))
638
639 (define-condition division-by-zero         (arithmetic-error) ())
640 (define-condition floating-point-overflow  (arithmetic-error) ())
641 (define-condition floating-point-underflow (arithmetic-error) ())
642 (define-condition floating-point-inexact   (arithmetic-error) ())
643 (define-condition floating-point-invalid-operation (arithmetic-error) ())
644
645 (define-condition print-not-readable (error)
646   ((object :reader print-not-readable-object :initarg :object))
647   (:report
648    (lambda (condition stream)
649      (let ((obj (print-not-readable-object condition))
650            (*print-array* nil))
651        (format stream "~S cannot be printed readably." obj)))))
652
653 (define-condition reader-error (parse-error stream-error)
654   ((format-control
655     :reader reader-error-format-control
656     :initarg :format-control)
657    (format-arguments
658     :reader reader-error-format-arguments
659     :initarg :format-arguments
660     :initform '()))
661   (:report
662    (lambda (condition stream)
663      (let ((error-stream (stream-error-stream condition)))
664        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?"
665                (file-position error-stream) error-stream
666                (reader-error-format-control condition)
667                (reader-error-format-arguments condition))))))
668 \f
669 ;;;; various other (not specified by ANSI) CONDITIONs
670 ;;;;
671 ;;;; These might logically belong in other files; they're here, after
672 ;;;; setup of CONDITION machinery, only because that makes it easier to
673 ;;;; get cold init to work.
674
675 (define-condition values-type-error (type-error)
676   ()
677   (:report
678    (lambda (condition stream)
679      (format stream
680              "~@<The values set ~2I~:_[~{~S~^ ~}] ~I~_is not of type ~2I~_~S.~:>"
681              (type-error-datum condition)
682              (type-error-expected-type condition)))))
683
684 ;;; KLUDGE: a condition for floating point errors when we can't or
685 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
686 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
687 ;;; know how but the old code was broken by the conversion to POSIX
688 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
689 ;;;
690 ;;; FIXME: Perhaps this should also be a base class for all
691 ;;; floating point exceptions?
692 (define-condition floating-point-exception (arithmetic-error)
693   ((flags :initarg :traps
694           :initform nil
695           :reader floating-point-exception-traps))
696   (:report (lambda (condition stream)
697              (format stream
698                      "An arithmetic error ~S was signalled.~%"
699                      (type-of condition))
700              (let ((traps (floating-point-exception-traps condition)))
701                (if traps
702                    (format stream
703                            "Trapping conditions are: ~%~{ ~S~^~}~%"
704                            traps)
705                    (write-line
706                     "No traps are enabled? How can this be?"
707                     stream))))))
708
709 (define-condition index-too-large-error (type-error)
710   ()
711   (:report
712    (lambda (condition stream)
713      (format stream
714              "The index ~S is too large."
715              (type-error-datum condition)))))
716
717 (define-condition bounding-indices-bad-error (type-error)
718   ((object :reader bounding-indices-bad-object :initarg :object))
719   (:report
720    (lambda (condition stream)
721      (let* ((datum (type-error-datum condition))
722             (start (car datum))
723             (end (cdr datum))
724             (object (bounding-indices-bad-object condition)))
725        (etypecase object
726          (sequence
727           (format stream
728                   "The bounding indices ~S and ~S are bad for a sequence of length ~S."
729                   start end (length object)))
730          (array
731           ;; from WITH-ARRAY-DATA
732           (format stream
733                   "The START and END parameters ~S and ~S are bad for an array of total size ~S."
734                   start end (array-total-size object))))))))
735
736 (define-condition nil-array-accessed-error (type-error)
737   ()
738   (:report (lambda (condition stream)
739              (format stream
740                      "An attempt to access an array of element-type ~
741                       NIL was made.  Congratulations!"))))
742
743 (define-condition io-timeout (stream-error)
744   ((direction :reader io-timeout-direction :initarg :direction))
745   (:report
746    (lambda (condition stream)
747      (declare (type stream stream))
748      (format stream
749              "I/O timeout ~(~A~)ing ~S"
750              (io-timeout-direction condition)
751              (stream-error-stream condition)))))
752
753 (define-condition namestring-parse-error (parse-error)
754   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
755    (args :reader namestring-parse-error-args :initarg :args :initform nil)
756    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
757    (offset :reader namestring-parse-error-offset :initarg :offset))
758   (:report
759    (lambda (condition stream)
760      (format stream
761              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
762              (namestring-parse-error-complaint condition)
763              (namestring-parse-error-args condition)
764              (namestring-parse-error-namestring condition)
765              (namestring-parse-error-offset condition)))))
766
767 (define-condition simple-package-error (simple-condition package-error) ())
768
769 (define-condition reader-package-error (reader-error) ())
770
771 (define-condition reader-eof-error (end-of-file)
772   ((context :reader reader-eof-error-context :initarg :context))
773   (:report
774    (lambda (condition stream)
775      (format stream
776              "unexpected end of file on ~S ~A"
777              (stream-error-stream condition)
778              (reader-eof-error-context condition)))))
779
780 (define-condition reader-impossible-number-error (reader-error)
781   ((error :reader reader-impossible-number-error-error :initarg :error))
782   (:report
783    (lambda (condition stream)
784      (let ((error-stream (stream-error-stream condition)))
785        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
786                (file-position error-stream) error-stream
787                (reader-error-format-control condition)
788                (reader-error-format-arguments condition)
789                (reader-impossible-number-error-error condition))))))
790
791 (define-condition sb!ext::timeout (serious-condition) ())
792
793 (define-condition defconstant-uneql (error)
794   ((name :initarg :name :reader defconstant-uneql-name)
795    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
796    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
797   (:report
798    (lambda (condition stream)
799      (format stream
800              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
801              (defconstant-uneql-name condition)
802              (defconstant-uneql-old-value condition)
803              (defconstant-uneql-new-value condition)))))
804 \f
805 ;;;; special SBCL extension conditions
806
807 ;;; an error apparently caused by a bug in SBCL itself
808 ;;;
809 ;;; Note that we don't make any serious effort to use this condition
810 ;;; for *all* errors in SBCL itself. E.g. type errors and array
811 ;;; indexing errors can occur in functions called from SBCL code, and
812 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
813 ;;; because the signalling code has no good way to know that the
814 ;;; underlying problem is a bug in SBCL. But in the fairly common case
815 ;;; that the signalling code does know that it's found a bug in SBCL,
816 ;;; this condition is appropriate, reusing boilerplate and helping
817 ;;; users to recognize it as an SBCL bug.
818 (define-condition bug (simple-error)
819   ()
820   (:report
821    (lambda (condition stream)
822      (format stream
823              "~@<  ~? ~:@_~?~:>"
824              (simple-condition-format-control condition)
825              (simple-condition-format-arguments condition)
826              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
827               SBCL might have been corrupted by bad user code, e.g. by an ~
828               undefined Lisp operation like ~S, or by stray pointers from ~
829               alien code or from unsafe Lisp code; or there might be a bug ~
830               in the OS or hardware that SBCL is running on.) If it seems to ~
831               be a bug in SBCL itself, the maintainers would like to know ~
832               about it. Bug reports are welcome on the SBCL ~
833               mailing lists, which you can find at ~
834               <http://sbcl.sourceforge.net/>.~:@>"
835              '((fmakunbound 'compile))))))
836 (defun bug (format-control &rest format-arguments)
837   (error 'bug
838          :format-control format-control
839          :format-arguments format-arguments))
840
841 ;;; a condition for use in stubs for operations which aren't supported
842 ;;; on some platforms
843 ;;;
844 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
845 ;;;   #-(or freebsd linux)
846 ;;;   (defun load-foreign (&rest rest)
847 ;;;     (error 'unsupported-operator :name 'load-foreign))
848 ;;;   #+(or freebsd linux)
849 ;;;   (defun load-foreign ... actual definition ...)
850 ;;; By signalling a standard condition in this case, we make it
851 ;;; possible for test code to distinguish between (1) intentionally
852 ;;; unimplemented and (2) unintentionally just screwed up somehow.
853 ;;; (Before this condition was defined, test code tried to deal with 
854 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
855 ;;; sbcl-0.7.0, a a package screwup left the definition of
856 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
857 ;;; architectures where it was supposed to be supported, and the
858 ;;; regression tests cheerfully passed because they assumed that
859 ;;; unFBOUNDPness meant they were running on an system which didn't
860 ;;; support the extension.)
861 (define-condition unsupported-operator (cell-error) ()
862   (:report
863    (lambda (condition stream)
864      (format stream
865              "unsupported on this platform (OS, CPU, whatever): ~S"
866              (cell-error-name condition)))))
867 \f
868 ;;;; restart definitions
869
870 (define-condition abort-failure (control-error) ()
871   (:report
872    "An ABORT restart was found that failed to transfer control dynamically."))
873
874 (defun abort (&optional condition)
875   #!+sb-doc
876   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
877    none exists."
878   (invoke-restart (find-restart-or-control-error 'abort condition))
879   ;; ABORT signals an error in case there was a restart named ABORT
880   ;; that did not transfer control dynamically. This could happen with
881   ;; RESTART-BIND.
882   (error 'abort-failure))
883
884 (defun muffle-warning (&optional condition)
885   #!+sb-doc
886   "Transfer control to a restart named MUFFLE-WARNING, signalling a
887    CONTROL-ERROR if none exists."
888   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
889
890 (macrolet ((define-nil-returning-restart (name args doc)
891              #!-sb-doc (declare (ignore doc))
892              `(defun ,name (,@args &optional condition)
893                 #!+sb-doc ,doc
894                 ;; FIXME: Perhaps this shared logic should be pulled out into
895                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
896                 (let ((restart (find-restart ',name condition)))
897                   (when restart
898                     (invoke-restart restart ,@args))))))
899   (define-nil-returning-restart continue ()
900     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
901   (define-nil-returning-restart store-value (value)
902     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
903    none exists.")
904   (define-nil-returning-restart use-value (value)
905     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
906    none exists."))
907
908 (/show0 "condition.lisp end of file")
909