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