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