0.8aplha.0.6:
[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 (cdr (condition-classoid-cpl class)))
328       (dolist (sslot (condition-classoid-slots sclass))
329         (let ((found (find (condition-slot-name sslot) (res)
330                            :key #'condition-slot-name)))
331           (cond (found
332                  (setf (condition-slot-initargs found)
333                        (union (condition-slot-initargs found)
334                               (condition-slot-initargs sslot)))
335                  (unless (condition-slot-initform-p found)
336                    (setf (condition-slot-initform-p found)
337                          (condition-slot-initform-p sslot))
338                    (setf (condition-slot-initform found)
339                          (condition-slot-initform sslot)))
340                  (unless (condition-slot-allocation found)
341                    (setf (condition-slot-allocation found)
342                          (condition-slot-allocation sslot))))
343                 (t
344                  (res (copy-structure sslot)))))))
345     (res)))
346
347 (defun %define-condition (name slots documentation report default-initargs)
348   (let ((class (find-classoid name)))
349     (setf (condition-classoid-slots class) slots)
350     (setf (condition-classoid-report class) report)
351     (setf (condition-classoid-default-initargs class) default-initargs)
352     (setf (fdocumentation name 'type) documentation)
353
354     (dolist (slot slots)
355
356       ;; Set up reader and writer functions.
357       (let ((name (condition-slot-name slot)))
358         (dolist (reader (condition-slot-readers slot))
359           (setf (fdefinition reader)
360                 (lambda (condition)
361                   (condition-reader-function condition name))))
362         (dolist (writer (condition-slot-writers slot))
363           (setf (fdefinition writer)
364                 (lambda (new-value condition)
365                   (condition-writer-function condition new-value name))))))
366
367     ;; Compute effective slots and set up the class and hairy slots
368     ;; (subsets of the effective slots.)
369     (let ((eslots (compute-effective-slots class))
370           (e-def-initargs
371            (reduce #'append
372                    (mapcar #'condition-classoid-default-initargs
373                            (condition-classoid-cpl class)))))
374       (dolist (slot eslots)
375         (ecase (condition-slot-allocation slot)
376           (:class
377            (unless (condition-slot-cell slot)
378              (setf (condition-slot-cell slot)
379                    (list (if (condition-slot-initform-p slot)
380                              (let ((initform (condition-slot-initform slot)))
381                                (if (functionp initform)
382                                    (funcall initform)
383                                    initform))
384                              *empty-condition-slot*))))
385            (push slot (condition-classoid-class-slots class)))
386           ((:instance nil)
387            (setf (condition-slot-allocation slot) :instance)
388            (when (or (functionp (condition-slot-initform slot))
389                      (dolist (initarg (condition-slot-initargs slot) nil)
390                        (when (functionp (getf e-def-initargs initarg))
391                          (return t))))
392              (push slot (condition-classoid-hairy-slots class))))))))
393   name)
394
395 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
396                                  &body options)
397   #!+sb-doc
398   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
399    Define NAME as a condition type. This new type inherits slots and its
400    report function from the specified PARENT-TYPEs. A slot spec is a list of:
401      (slot-name :reader <rname> :initarg <iname> {Option Value}*
402
403    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
404    and :TYPE and the overall options :DEFAULT-INITARGS and
405    [type] :DOCUMENTATION are also allowed.
406
407    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
408    a string or a two-argument lambda or function name. If a function, the
409    function is called with the condition and stream to report the condition.
410    If a string, the string is printed.
411
412    Condition types are classes, but (as allowed by ANSI and not as described in
413    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
414    SLOT-VALUE may not be used on condition objects."
415   (let* ((parent-types (or parent-types '(condition)))
416          (layout (find-condition-layout name parent-types))
417          (documentation nil)
418          (report nil)
419          (default-initargs ()))
420     (collect ((slots)
421               (all-readers nil append)
422               (all-writers nil append))
423       (dolist (spec slot-specs)
424         (when (keywordp spec)
425           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
426                 spec))
427         (let* ((spec (if (consp spec) spec (list spec)))
428                (slot-name (first spec))
429                (allocation :instance)
430                (initform-p nil)
431                initform)
432           (collect ((initargs)
433                     (readers)
434                     (writers))
435             (do ((options (rest spec) (cddr options)))
436                 ((null options))
437               (unless (and (consp options) (consp (cdr options)))
438                 (error "malformed condition slot spec:~%  ~S." spec))
439               (let ((arg (second options)))
440                 (case (first options)
441                   (:reader (readers arg))
442                   (:writer (writers arg))
443                   (:accessor
444                    (readers arg)
445                    (writers `(setf ,arg)))
446                   (:initform
447                    (when initform-p
448                      (error "more than one :INITFORM in ~S" spec))
449                    (setq initform-p t)
450                    (setq initform arg))
451                   (:initarg (initargs arg))
452                   (:allocation
453                    (setq allocation arg))
454                   (:type)
455                   (t
456                    (error "unknown slot option:~%  ~S" (first options))))))
457
458             (all-readers (readers))
459             (all-writers (writers))
460             (slots `(make-condition-slot
461                      :name ',slot-name
462                      :initargs ',(initargs)
463                      :readers ',(readers)
464                      :writers ',(writers)
465                      :initform-p ',initform-p
466                      :initform
467                      ,(if (constantp initform)
468                           `',(eval initform)
469                           `#'(lambda () ,initform)))))))
470
471       (dolist (option options)
472         (unless (consp option)
473           (error "bad option:~%  ~S" option))
474         (case (first option)
475           (:documentation (setq documentation (second option)))
476           (:report
477            (let ((arg (second option)))
478              (setq report
479                    (if (stringp arg)
480                        `#'(lambda (condition stream)
481                           (declare (ignore condition))
482                           (write-string ,arg stream))
483                        `#'(lambda (condition stream)
484                           (funcall #',arg condition stream))))))
485           (:default-initargs
486            (do ((initargs (rest option) (cddr initargs)))
487                ((endp initargs))
488              (let ((val (second initargs)))
489                (setq default-initargs
490                      (list* `',(first initargs)
491                             (if (constantp val)
492                                 `',(eval val)
493                                 `#'(lambda () ,val))
494                             default-initargs)))))
495           (t
496            (error "unknown option: ~S" (first option)))))
497
498       `(progn
499          (eval-when (:compile-toplevel :load-toplevel :execute)
500            (%compiler-define-condition ',name ',parent-types ',layout))
501
502          (declaim (ftype (function (t) t) ,@(all-readers)))
503          (declaim (ftype (function (t t) t) ,@(all-writers)))
504
505          (%define-condition ',name
506                             (list ,@(slots))
507                             ,documentation
508                             ,report
509                             (list ,@default-initargs))))))
510 \f
511 ;;;; DESCRIBE on CONDITIONs
512
513 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
514 ;;; eventually (once we get CLOS up and running so that we can define
515 ;;; methods)
516 (defun describe-condition (condition stream)
517   (format stream
518           "~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>"
519           condition
520           (type-of condition)
521           (concatenate 'list
522                        (condition-actual-initargs condition)
523                        (condition-assigned-slots condition))))
524 \f
525 ;;;; various CONDITIONs specified by ANSI
526
527 (define-condition serious-condition (condition) ())
528
529 (define-condition error (serious-condition) ())
530
531 (define-condition warning (condition) ())
532 (define-condition style-warning (warning) ())
533
534 (defun simple-condition-printer (condition stream)
535   (apply #'format
536          stream
537          (simple-condition-format-control condition)
538          (simple-condition-format-arguments condition)))
539
540 (define-condition simple-condition ()
541   ((format-control :reader simple-condition-format-control
542                    :initarg :format-control)
543    (format-arguments :reader simple-condition-format-arguments
544                      :initarg :format-arguments
545                      :initform '()))
546   (:report simple-condition-printer))
547
548 (define-condition simple-warning (simple-condition warning) ())
549
550 (define-condition simple-error (simple-condition error) ())
551
552 (define-condition storage-condition (serious-condition) ())
553
554 (define-condition type-error (error)
555   ((datum :reader type-error-datum :initarg :datum)
556    (expected-type :reader type-error-expected-type :initarg :expected-type))
557   (:report
558    (lambda (condition stream)
559      (format stream
560              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
561              (type-error-datum condition)
562              (type-error-expected-type condition)))))
563
564 (define-condition simple-type-error (simple-condition type-error) ())
565
566 (define-condition program-error (error) ())
567 (define-condition parse-error   (error) ())
568 (define-condition control-error (error) ())
569 (define-condition stream-error  (error)
570   ((stream :reader stream-error-stream :initarg :stream)))
571
572 (define-condition end-of-file (stream-error) ()
573   (:report
574    (lambda (condition stream)
575      (format stream
576              "end of file on ~S"
577              (stream-error-stream condition)))))
578
579 (define-condition file-error (error)
580   ((pathname :reader file-error-pathname :initarg :pathname))
581   (:report
582    (lambda (condition stream)
583      (format stream "error on file ~S" (file-error-pathname condition)))))
584
585 (define-condition package-error (error)
586   ((package :reader package-error-package :initarg :package)))
587
588 (define-condition cell-error (error)
589   ((name :reader cell-error-name :initarg :name)))
590
591 (define-condition unbound-variable (cell-error) ()
592   (:report
593    (lambda (condition stream)
594      (format stream
595              "The variable ~S is unbound."
596              (cell-error-name condition)))))
597
598 (define-condition undefined-function (cell-error) ()
599   (:report
600    (lambda (condition stream)
601      (format stream
602              "The function ~S is undefined."
603              (cell-error-name condition)))))
604
605 (define-condition special-form-function (undefined-function) ()
606   (:report
607    (lambda (condition stream)
608      (format stream
609              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
610              (cell-error-name condition)))))
611
612 (define-condition arithmetic-error (error)
613   ((operation :reader arithmetic-error-operation
614               :initarg :operation
615               :initform nil)
616    (operands :reader arithmetic-error-operands
617              :initarg :operands))
618   (:report (lambda (condition stream)
619              (format stream
620                      "arithmetic error ~S signalled"
621                      (type-of condition))
622              (when (arithmetic-error-operation condition)
623                (format stream
624                        "~%Operation was ~S, operands ~S."
625                        (arithmetic-error-operation condition)
626                        (arithmetic-error-operands condition))))))
627
628 (define-condition division-by-zero         (arithmetic-error) ())
629 (define-condition floating-point-overflow  (arithmetic-error) ())
630 (define-condition floating-point-underflow (arithmetic-error) ())
631 (define-condition floating-point-inexact   (arithmetic-error) ())
632 (define-condition floating-point-invalid-operation (arithmetic-error) ())
633
634 (define-condition print-not-readable (error)
635   ((object :reader print-not-readable-object :initarg :object))
636   (:report
637    (lambda (condition stream)
638      (let ((obj (print-not-readable-object condition))
639            (*print-array* nil))
640        (format stream "~S cannot be printed readably." obj)))))
641
642 (define-condition reader-error (parse-error stream-error)
643   ((format-control
644     :reader reader-error-format-control
645     :initarg :format-control)
646    (format-arguments
647     :reader reader-error-format-arguments
648     :initarg :format-arguments
649     :initform '()))
650   (:report
651    (lambda (condition stream)
652      (let ((error-stream (stream-error-stream condition)))
653        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?"
654                (file-position error-stream) error-stream
655                (reader-error-format-control condition)
656                (reader-error-format-arguments condition))))))
657 \f
658 ;;;; various other (not specified by ANSI) CONDITIONs
659 ;;;;
660 ;;;; These might logically belong in other files; they're here, after
661 ;;;; setup of CONDITION machinery, only because that makes it easier to
662 ;;;; get cold init to work.
663
664 ;;; KLUDGE: a condition for floating point errors when we can't or
665 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
666 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
667 ;;; know how but the old code was broken by the conversion to POSIX
668 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
669 ;;;
670 ;;; FIXME: Perhaps this should also be a base class for all
671 ;;; floating point exceptions?
672 (define-condition floating-point-exception (arithmetic-error)
673   ((flags :initarg :traps
674           :initform nil
675           :reader floating-point-exception-traps))
676   (:report (lambda (condition stream)
677              (format stream
678                      "An arithmetic error ~S was signalled.~%"
679                      (type-of condition))
680              (let ((traps (floating-point-exception-traps condition)))
681                (if traps
682                    (format stream
683                            "Trapping conditions are: ~%~{ ~S~^~}~%"
684                            traps)
685                    (write-line
686                     "No traps are enabled? How can this be?"
687                     stream))))))
688
689 (define-condition index-too-large-error (type-error)
690   ()
691   (:report
692    (lambda (condition stream)
693      (format stream
694              "The index ~S is too large."
695              (type-error-datum condition)))))
696
697 (define-condition bounding-indices-bad-error (type-error)
698   ((object :reader bounding-indices-bad-object :initarg :object))
699   (:report
700    (lambda (condition stream)
701      (let* ((datum (type-error-datum condition))
702             (start (car datum))
703             (end (cdr datum))
704             (object (bounding-indices-bad-object condition)))
705        (etypecase object
706          (sequence
707           (format stream
708                   "The bounding indices ~S and ~S are bad for a sequence of length ~S."
709                   start end (length object)))
710          (array
711           ;; from WITH-ARRAY-DATA
712           (format stream
713                   "The START and END parameters ~S and ~S are bad for an array of total size ~S."
714                   start end (array-total-size object))))))))
715
716 (define-condition nil-array-accessed-error (type-error)
717   ()
718   (:report (lambda (condition stream)
719              (format stream
720                      "An attempt to access an array of element-type ~
721                       NIL was made.  Congratulations!"))))
722
723 (define-condition io-timeout (stream-error)
724   ((direction :reader io-timeout-direction :initarg :direction))
725   (:report
726    (lambda (condition stream)
727      (declare (type stream stream))
728      (format stream
729              "I/O timeout ~(~A~)ing ~S"
730              (io-timeout-direction condition)
731              (stream-error-stream condition)))))
732
733 (define-condition namestring-parse-error (parse-error)
734   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
735    (args :reader namestring-parse-error-args :initarg :args :initform nil)
736    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
737    (offset :reader namestring-parse-error-offset :initarg :offset))
738   (:report
739    (lambda (condition stream)
740      (format stream
741              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
742              (namestring-parse-error-complaint condition)
743              (namestring-parse-error-args condition)
744              (namestring-parse-error-namestring condition)
745              (namestring-parse-error-offset condition)))))
746
747 (define-condition simple-package-error (simple-condition package-error) ())
748
749 (define-condition reader-package-error (reader-error) ())
750
751 (define-condition reader-eof-error (end-of-file)
752   ((context :reader reader-eof-error-context :initarg :context))
753   (:report
754    (lambda (condition stream)
755      (format stream
756              "unexpected end of file on ~S ~A"
757              (stream-error-stream condition)
758              (reader-eof-error-context condition)))))
759
760 (define-condition reader-impossible-number-error (reader-error)
761   ((error :reader reader-impossible-number-error-error :initarg :error))
762   (:report
763    (lambda (condition stream)
764      (let ((error-stream (stream-error-stream condition)))
765        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
766                (file-position error-stream) error-stream
767                (reader-error-format-control condition)
768                (reader-error-format-arguments condition)
769                (reader-impossible-number-error-error condition))))))
770
771 (define-condition sb!ext::timeout (serious-condition) ())
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