0.pre8.18:
[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     (let ((val (getf (condition-assigned-slots condition) name
210                      *empty-condition-slot*)))
211       (if (eq val *empty-condition-slot*)
212           (let ((actual-initargs (condition-actual-initargs condition))
213                 (slot (find-condition-class-slot class name)))
214             (unless slot
215               (error "missing slot ~S of ~S" name condition))
216             (do ((initargs actual-initargs (cddr initargs)))
217                 ((endp initargs)
218                  (setf (getf (condition-assigned-slots condition) name)
219                        (find-slot-default class slot)))
220               (when (member (car initargs) (condition-slot-initargs slot))
221                 (return-from condition-reader-function
222                   (setf (getf (condition-assigned-slots condition)
223                               name)
224                         (cadr initargs))))))
225           val))))
226 \f
227 ;;;; MAKE-CONDITION
228
229 (defun make-condition (thing &rest args)
230   #!+sb-doc
231   "Make an instance of a condition object using the specified initargs."
232   ;; Note: ANSI specifies no exceptional situations in this function.
233   ;; signalling simple-type-error would not be wrong.
234   (let* ((thing (if (symbolp thing)
235                     (find-classoid thing)
236                     thing))
237          (class (typecase thing
238                   (condition-classoid thing)
239                   (classoid
240                    (error 'simple-type-error
241                           :datum thing
242                           :expected-type 'condition-class
243                           :format-control "~S is not a condition class."
244                           :format-arguments (list thing)))
245                   (t
246                    (error 'simple-type-error
247                           :datum thing
248                           :expected-type 'condition-class
249                           :format-control "bad thing for class argument:~%  ~S"
250                           :format-arguments (list thing)))))
251          (res (make-condition-object args)))
252     (setf (%instance-layout res) (classoid-layout class))
253     ;; Set any class slots with initargs present in this call.
254     (dolist (cslot (condition-classoid-class-slots class))
255       (dolist (initarg (condition-slot-initargs cslot))
256         (let ((val (getf args initarg *empty-condition-slot*)))
257           (unless (eq val *empty-condition-slot*)
258             (setf (car (condition-slot-cell cslot)) val)))))
259     ;; Default any slots with non-constant defaults now.
260     (dolist (hslot (condition-classoid-hairy-slots class))
261       (when (dolist (initarg (condition-slot-initargs hslot) t)
262               (unless (eq (getf args initarg *empty-condition-slot*)
263                           *empty-condition-slot*)
264                 (return nil)))
265         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
266               (find-slot-default class hslot))))
267
268     res))
269 \f
270 ;;;; DEFINE-CONDITION
271
272 (eval-when (:compile-toplevel :load-toplevel :execute)
273 (defun %compiler-define-condition (name direct-supers layout)
274   (multiple-value-bind (class old-layout)
275       (insured-find-classoid name
276                              #'condition-classoid-p
277                              #'make-condition-classoid)
278     (setf (layout-classoid layout) class)
279     (setf (classoid-direct-superclasses class)
280           (mapcar #'find-classoid direct-supers))
281     (cond ((not old-layout)
282            (register-layout layout))
283           ((not *type-system-initialized*)
284            (setf (layout-classoid old-layout) class)
285            (setq layout old-layout)
286            (unless (eq (classoid-layout class) layout)
287              (register-layout layout)))
288           ((redefine-layout-warning "current"
289                                     old-layout
290                                     "new"
291                                     (layout-length layout)
292                                     (layout-inherits layout)
293                                     (layout-depthoid layout))
294            (register-layout layout :invalidate t))
295           ((not (classoid-layout class))
296            (register-layout layout)))
297
298     (setf (layout-info layout)
299           (locally
300             ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
301             ;; names which creates fast but non-cold-loadable, non-compact
302             ;; code. In this context, we'd rather have compact, cold-loadable
303             ;; code. -- WHN 19990928
304             (declare (notinline find-classoid))
305             (layout-info (classoid-layout (find-classoid 'condition)))))
306
307     (setf (find-classoid name) class)
308
309     ;; Initialize CPL slot.
310     (setf (condition-classoid-cpl class)
311           (remove-if-not #'condition-classoid-p 
312                          (std-compute-class-precedence-list class))))
313   (values))
314
315 ) ; EVAL-WHEN
316
317 ;;; Compute the effective slots of CLASS, copying inherited slots and
318 ;;; destructively modifying direct slots.
319 ;;;
320 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
321 ;;; direct slots. Presumably it follows from the semantics of
322 ;;; inheritance and redefinition of conditions, but finding the cite
323 ;;; and documenting it here would be good. (Or, if this is not in fact
324 ;;; ANSI-compliant, fixing it would also be good.:-)
325 (defun compute-effective-slots (class)
326   (collect ((res (copy-list (condition-classoid-slots class))))
327     (dolist (sclass (condition-classoid-cpl class))
328       (dolist (sslot (condition-classoid-slots sclass))
329         (let ((found (find (condition-slot-name sslot) (res))))
330           (cond (found
331                  (setf (condition-slot-initargs found)
332                        (union (condition-slot-initargs found)
333                               (condition-slot-initargs sslot)))
334                  (unless (condition-slot-initform-p found)
335                    (setf (condition-slot-initform-p found)
336                          (condition-slot-initform-p sslot))
337                    (setf (condition-slot-initform found)
338                          (condition-slot-initform sslot)))
339                  (unless (condition-slot-allocation found)
340                    (setf (condition-slot-allocation found)
341                          (condition-slot-allocation sslot))))
342                 (t
343                  (res (copy-structure sslot)))))))
344     (res)))
345
346 (defun %define-condition (name slots documentation report default-initargs)
347   (let ((class (find-classoid name)))
348     (setf (condition-classoid-slots class) slots)
349     (setf (condition-classoid-report class) report)
350     (setf (condition-classoid-default-initargs class) default-initargs)
351     (setf (fdocumentation name 'type) documentation)
352
353     (dolist (slot slots)
354
355       ;; Set up reader and writer functions.
356       (let ((name (condition-slot-name slot)))
357         (dolist (reader (condition-slot-readers slot))
358           (setf (fdefinition reader)
359                 (lambda (condition)
360                   (condition-reader-function condition name))))
361         (dolist (writer (condition-slot-writers slot))
362           (setf (fdefinition writer)
363                 (lambda (new-value condition)
364                   (condition-writer-function condition new-value name))))))
365
366     ;; Compute effective slots and set up the class and hairy slots
367     ;; (subsets of the effective slots.)
368     (let ((eslots (compute-effective-slots class))
369           (e-def-initargs
370            (reduce #'append
371                    (mapcar #'condition-classoid-default-initargs
372                            (condition-classoid-cpl class)))))
373       (dolist (slot eslots)
374         (ecase (condition-slot-allocation slot)
375           (:class
376            (unless (condition-slot-cell slot)
377              (setf (condition-slot-cell slot)
378                    (list (if (condition-slot-initform-p slot)
379                              (let ((initform (condition-slot-initform slot)))
380                                (if (functionp initform)
381                                    (funcall initform)
382                                    initform))
383                              *empty-condition-slot*))))
384            (push slot (condition-classoid-class-slots class)))
385           ((:instance nil)
386            (setf (condition-slot-allocation slot) :instance)
387            (when (or (functionp (condition-slot-initform slot))
388                      (dolist (initarg (condition-slot-initargs slot) nil)
389                        (when (functionp (getf e-def-initargs initarg))
390                          (return t))))
391              (push slot (condition-classoid-hairy-slots class))))))))
392   name)
393
394 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
395                                  &body options)
396   #!+sb-doc
397   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
398    Define NAME as a condition type. This new type inherits slots and its
399    report function from the specified PARENT-TYPEs. A slot spec is a list of:
400      (slot-name :reader <rname> :initarg <iname> {Option Value}*
401
402    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
403    and :TYPE and the overall options :DEFAULT-INITARGS and
404    [type] :DOCUMENTATION are also allowed.
405
406    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
407    a string or a two-argument lambda or function name. If a function, the
408    function is called with the condition and stream to report the condition.
409    If a string, the string is printed.
410
411    Condition types are classes, but (as allowed by ANSI and not as described in
412    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
413    SLOT-VALUE may not be used on condition objects."
414   (let* ((parent-types (or parent-types '(condition)))
415          (layout (find-condition-layout name parent-types))
416          (documentation nil)
417          (report nil)
418          (default-initargs ()))
419     (collect ((slots)
420               (all-readers nil append)
421               (all-writers nil append))
422       (dolist (spec slot-specs)
423         (when (keywordp spec)
424           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
425                 spec))
426         (let* ((spec (if (consp spec) spec (list spec)))
427                (slot-name (first spec))
428                (allocation :instance)
429                (initform-p nil)
430                initform)
431           (collect ((initargs)
432                     (readers)
433                     (writers))
434             (do ((options (rest spec) (cddr options)))
435                 ((null options))
436               (unless (and (consp options) (consp (cdr options)))
437                 (error "malformed condition slot spec:~%  ~S." spec))
438               (let ((arg (second options)))
439                 (case (first options)
440                   (:reader (readers arg))
441                   (:writer (writers arg))
442                   (:accessor
443                    (readers arg)
444                    (writers `(setf ,arg)))
445                   (:initform
446                    (when initform-p
447                      (error "more than one :INITFORM in ~S" spec))
448                    (setq initform-p t)
449                    (setq initform arg))
450                   (:initarg (initargs arg))
451                   (:allocation
452                    (setq allocation arg))
453                   (:type)
454                   (t
455                    (error "unknown slot option:~%  ~S" (first options))))))
456
457             (all-readers (readers))
458             (all-writers (writers))
459             (slots `(make-condition-slot
460                      :name ',slot-name
461                      :initargs ',(initargs)
462                      :readers ',(readers)
463                      :writers ',(writers)
464                      :initform-p ',initform-p
465                      :initform
466                      ,(if (constantp initform)
467                           `',(eval initform)
468                           `#'(lambda () ,initform)))))))
469
470       (dolist (option options)
471         (unless (consp option)
472           (error "bad option:~%  ~S" option))
473         (case (first option)
474           (:documentation (setq documentation (second option)))
475           (:report
476            (let ((arg (second option)))
477              (setq report
478                    (if (stringp arg)
479                        `#'(lambda (condition stream)
480                           (declare (ignore condition))
481                           (write-string ,arg stream))
482                        `#'(lambda (condition stream)
483                           (funcall #',arg condition stream))))))
484           (:default-initargs
485            (do ((initargs (rest option) (cddr initargs)))
486                ((endp initargs))
487              (let ((val (second initargs)))
488                (setq default-initargs
489                      (list* `',(first initargs)
490                             (if (constantp val)
491                                 `',(eval val)
492                                 `#'(lambda () ,val))
493                             default-initargs)))))
494           (t
495            (error "unknown option: ~S" (first option)))))
496
497       `(progn
498          (eval-when (:compile-toplevel :load-toplevel :execute)
499            (%compiler-define-condition ',name ',parent-types ',layout))
500
501          (declaim (ftype (function (t) t) ,@(all-readers)))
502          (declaim (ftype (function (t t) t) ,@(all-writers)))
503
504          (%define-condition ',name
505                             (list ,@(slots))
506                             ,documentation
507                             ,report
508                             (list ,@default-initargs))))))
509 \f
510 ;;;; DESCRIBE on CONDITIONs
511
512 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
513 ;;; eventually (once we get CLOS up and running so that we can define
514 ;;; methods)
515 (defun describe-condition (condition stream)
516   (format stream
517           "~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>"
518           condition
519           (type-of condition)
520           (concatenate 'list
521                        (condition-actual-initargs condition)
522                        (condition-assigned-slots condition))))
523 \f
524 ;;;; various CONDITIONs specified by ANSI
525
526 (define-condition serious-condition (condition) ())
527
528 (define-condition error (serious-condition) ())
529
530 (define-condition warning (condition) ())
531 (define-condition style-warning (warning) ())
532
533 (defun simple-condition-printer (condition stream)
534   (apply #'format
535          stream
536          (simple-condition-format-control condition)
537          (simple-condition-format-arguments condition)))
538
539 (define-condition simple-condition ()
540   ((format-control :reader simple-condition-format-control
541                    :initarg :format-control)
542    (format-arguments :reader simple-condition-format-arguments
543                      :initarg :format-arguments
544                      :initform '()))
545   (:report simple-condition-printer))
546
547 (define-condition simple-warning (simple-condition warning) ())
548
549 (define-condition simple-error (simple-condition error) ())
550
551 (define-condition storage-condition (serious-condition) ())
552
553 (define-condition type-error (error)
554   ((datum :reader type-error-datum :initarg :datum)
555    (expected-type :reader type-error-expected-type :initarg :expected-type))
556   (:report
557    (lambda (condition stream)
558      (format stream
559              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
560              (type-error-datum condition)
561              (type-error-expected-type condition)))))
562
563 (define-condition simple-type-error (simple-condition type-error) ())
564
565 (define-condition program-error (error) ())
566 (define-condition parse-error   (error) ())
567 (define-condition control-error (error) ())
568 (define-condition stream-error  (error)
569   ((stream :reader stream-error-stream :initarg :stream)))
570
571 (define-condition end-of-file (stream-error) ()
572   (:report
573    (lambda (condition stream)
574      (format stream
575              "end of file on ~S"
576              (stream-error-stream condition)))))
577
578 (define-condition file-error (error)
579   ((pathname :reader file-error-pathname :initarg :pathname))
580   (:report
581    (lambda (condition stream)
582      (format stream "error on file ~S" (file-error-pathname condition)))))
583
584 (define-condition package-error (error)
585   ((package :reader package-error-package :initarg :package)))
586
587 (define-condition cell-error (error)
588   ((name :reader cell-error-name :initarg :name)))
589
590 (define-condition unbound-variable (cell-error) ()
591   (:report
592    (lambda (condition stream)
593      (format stream
594              "The variable ~S is unbound."
595              (cell-error-name condition)))))
596
597 (define-condition undefined-function (cell-error) ()
598   (:report
599    (lambda (condition stream)
600      (format stream
601              "The function ~S is undefined."
602              (cell-error-name condition)))))
603
604 (define-condition special-form-function (undefined-function) ()
605   (:report
606    (lambda (condition stream)
607      (format stream
608              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
609              (cell-error-name condition)))))
610
611 (define-condition arithmetic-error (error)
612   ((operation :reader arithmetic-error-operation
613               :initarg :operation
614               :initform nil)
615    (operands :reader arithmetic-error-operands
616              :initarg :operands))
617   (:report (lambda (condition stream)
618              (format stream
619                      "arithmetic error ~S signalled"
620                      (type-of condition))
621              (when (arithmetic-error-operation condition)
622                (format stream
623                        "~%Operation was ~S, operands ~S."
624                        (arithmetic-error-operation condition)
625                        (arithmetic-error-operands condition))))))
626
627 (define-condition division-by-zero         (arithmetic-error) ())
628 (define-condition floating-point-overflow  (arithmetic-error) ())
629 (define-condition floating-point-underflow (arithmetic-error) ())
630 (define-condition floating-point-inexact   (arithmetic-error) ())
631 (define-condition floating-point-invalid-operation (arithmetic-error) ())
632
633 (define-condition print-not-readable (error)
634   ((object :reader print-not-readable-object :initarg :object))
635   (:report
636    (lambda (condition stream)
637      (let ((obj (print-not-readable-object condition))
638            (*print-array* nil))
639        (format stream "~S cannot be printed readably." obj)))))
640
641 (define-condition reader-error (parse-error stream-error)
642   ((format-control
643     :reader reader-error-format-control
644     :initarg :format-control)
645    (format-arguments
646     :reader reader-error-format-arguments
647     :initarg :format-arguments
648     :initform '()))
649   (:report
650    (lambda (condition stream)
651      (let ((error-stream (stream-error-stream condition)))
652        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?"
653                (file-position error-stream) error-stream
654                (reader-error-format-control condition)
655                (reader-error-format-arguments condition))))))
656 \f
657 ;;;; various other (not specified by ANSI) CONDITIONs
658 ;;;;
659 ;;;; These might logically belong in other files; they're here, after
660 ;;;; setup of CONDITION machinery, only because that makes it easier to
661 ;;;; get cold init to work.
662
663 ;;; KLUDGE: a condition for floating point errors when we can't or
664 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
665 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
666 ;;; know how but the old code was broken by the conversion to POSIX
667 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
668 ;;;
669 ;;; FIXME: Perhaps this should also be a base class for all
670 ;;; floating point exceptions?
671 (define-condition floating-point-exception (arithmetic-error)
672   ((flags :initarg :traps
673           :initform nil
674           :reader floating-point-exception-traps))
675   (:report (lambda (condition stream)
676              (format stream
677                      "An arithmetic error ~S was signalled.~%"
678                      (type-of condition))
679              (let ((traps (floating-point-exception-traps condition)))
680                (if traps
681                    (format stream
682                            "Trapping conditions are: ~%~{ ~S~^~}~%"
683                            traps)
684                    (write-line
685                     "No traps are enabled? How can this be?"
686                     stream))))))
687
688 (define-condition index-too-large-error (type-error)
689   ()
690   (:report
691    (lambda (condition stream)
692      (format stream
693              "The index ~S is too large."
694              (type-error-datum condition)))))
695
696 (define-condition bounding-indices-bad-error (type-error)
697   ((object :reader bounding-indices-bad-object :initarg :object))
698   (:report
699    (lambda (condition stream)
700      (let* ((datum (type-error-datum condition))
701             (start (car datum))
702             (end (cdr datum))
703             (object (bounding-indices-bad-object condition)))
704        (etypecase object
705          (sequence
706           (format stream
707                   "The bounding indices ~S and ~S are bad for a sequence of length ~S."
708                   start end (length object)))
709          (array
710           ;; from WITH-ARRAY-DATA
711           (format stream
712                   "The START and END parameters ~S and ~S are bad for an array of total size ~S."
713                   start end (array-total-size object))))))))
714
715 (define-condition nil-array-accessed-error (type-error)
716   ()
717   (:report (lambda (condition stream)
718              (format stream
719                      "An attempt to access an array of element-type ~
720                       NIL was made.  Congratulations!"))))
721
722 (define-condition io-timeout (stream-error)
723   ((direction :reader io-timeout-direction :initarg :direction))
724   (:report
725    (lambda (condition stream)
726      (declare (type stream stream))
727      (format stream
728              "I/O timeout ~(~A~)ing ~S"
729              (io-timeout-direction condition)
730              (stream-error-stream condition)))))
731
732 (define-condition namestring-parse-error (parse-error)
733   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
734    (args :reader namestring-parse-error-args :initarg :args :initform nil)
735    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
736    (offset :reader namestring-parse-error-offset :initarg :offset))
737   (:report
738    (lambda (condition stream)
739      (format stream
740              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
741              (namestring-parse-error-complaint condition)
742              (namestring-parse-error-args condition)
743              (namestring-parse-error-namestring condition)
744              (namestring-parse-error-offset condition)))))
745
746 (define-condition simple-package-error (simple-condition package-error) ())
747
748 (define-condition reader-package-error (reader-error) ())
749
750 (define-condition reader-eof-error (end-of-file)
751   ((context :reader reader-eof-error-context :initarg :context))
752   (:report
753    (lambda (condition stream)
754      (format stream
755              "unexpected end of file on ~S ~A"
756              (stream-error-stream condition)
757              (reader-eof-error-context condition)))))
758
759 (define-condition reader-impossible-number-error (reader-error)
760   ((error :reader reader-impossible-number-error-error :initarg :error))
761   (:report
762    (lambda (condition stream)
763      (let ((error-stream (stream-error-stream condition)))
764        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
765                (file-position error-stream) error-stream
766                (reader-error-format-control condition)
767                (reader-error-format-arguments condition)
768                (reader-impossible-number-error-error condition))))))
769
770 ;;; should this inherit from error?  good question
771 (define-condition timeout (error) ())
772
773
774 \f
775 ;;;; special SBCL extension conditions
776
777 ;;; an error apparently caused by a bug in SBCL itself
778 ;;;
779 ;;; Note that we don't make any serious effort to use this condition
780 ;;; for *all* errors in SBCL itself. E.g. type errors and array
781 ;;; indexing errors can occur in functions called from SBCL code, and
782 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
783 ;;; because the signalling code has no good way to know that the
784 ;;; underlying problem is a bug in SBCL. But in the fairly common case
785 ;;; that the signalling code does know that it's found a bug in SBCL,
786 ;;; this condition is appropriate, reusing boilerplate and helping
787 ;;; users to recognize it as an SBCL bug.
788 (define-condition bug (simple-error)
789   ()
790   (:report
791    (lambda (condition stream)
792      (format stream
793              "~@<  ~? ~:@_~?~:>"
794              (simple-condition-format-control condition)
795              (simple-condition-format-arguments condition)
796              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
797               SBCL might have been corrupted by bad user code, e.g. by an ~
798               undefined Lisp operation like ~S, or by stray pointers from ~
799               alien code or from unsafe Lisp code; or there might be a bug ~
800               in the OS or hardware that SBCL is running on.) If it seems to ~
801               be a bug in SBCL itself, the maintainers would like to know ~
802               about it. Bug reports are welcome on the SBCL ~
803               mailing lists, which you can find at ~
804               <http://sbcl.sourceforge.net/>.~:@>"
805              '((fmakunbound 'compile))))))
806 (defun bug (format-control &rest format-arguments)
807   (error 'bug
808          :format-control format-control
809          :format-arguments format-arguments))
810
811 ;;; a condition for use in stubs for operations which aren't supported
812 ;;; on some platforms
813 ;;;
814 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
815 ;;;   #-(or freebsd linux)
816 ;;;   (defun load-foreign (&rest rest)
817 ;;;     (error 'unsupported-operator :name 'load-foreign))
818 ;;;   #+(or freebsd linux)
819 ;;;   (defun load-foreign ... actual definition ...)
820 ;;; By signalling a standard condition in this case, we make it
821 ;;; possible for test code to distinguish between (1) intentionally
822 ;;; unimplemented and (2) unintentionally just screwed up somehow.
823 ;;; (Before this condition was defined, test code tried to deal with 
824 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
825 ;;; sbcl-0.7.0, a a package screwup left the definition of
826 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
827 ;;; architectures where it was supposed to be supported, and the
828 ;;; regression tests cheerfully passed because they assumed that
829 ;;; unFBOUNDPness meant they were running on an system which didn't
830 ;;; support the extension.)
831 (define-condition unsupported-operator (cell-error) ()
832   (:report
833    (lambda (condition stream)
834      (format stream
835              "unsupported on this platform (OS, CPU, whatever): ~S"
836              (cell-error-name condition)))))
837 \f
838 ;;;; restart definitions
839
840 (define-condition abort-failure (control-error) ()
841   (:report
842    "An ABORT restart was found that failed to transfer control dynamically."))
843
844 (defun abort (&optional condition)
845   #!+sb-doc
846   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
847    none exists."
848   (invoke-restart (find-restart 'abort condition))
849   ;; ABORT signals an error in case there was a restart named ABORT
850   ;; that did not transfer control dynamically. This could happen with
851   ;; RESTART-BIND.
852   (error 'abort-failure))
853
854 (defun muffle-warning (&optional condition)
855   #!+sb-doc
856   "Transfer control to a restart named MUFFLE-WARNING, signalling a
857    CONTROL-ERROR if none exists."
858   (invoke-restart (find-restart 'muffle-warning condition)))
859
860 (macrolet ((define-nil-returning-restart (name args doc)
861              #!-sb-doc (declare (ignore doc))
862              `(defun ,name (,@args &optional condition)
863                 #!+sb-doc ,doc
864                 ;; FIXME: Perhaps this shared logic should be pulled out into
865                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
866                 (let ((restart (find-restart ',name condition)))
867                   (when restart
868                     (invoke-restart restart ,@args))))))
869   (define-nil-returning-restart continue ()
870     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
871   (define-nil-returning-restart store-value (value)
872     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
873    none exists.")
874   (define-nil-returning-restart use-value (value)
875     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
876    none exists."))
877
878 (/show0 "condition.lisp end of file")
879