fix style-warnings for condition slot-accessors used in :REPORT
[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 ;;;; miscellaneous support utilities
17
18 ;;; Signalling an error when trying to print an error condition is
19 ;;; generally a PITA, so whatever the failure encountered when
20 ;;; wondering about FILE-POSITION within a condition printer, 'tis
21 ;;; better silently to give up than to try to complain.
22 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
23   ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
24   ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
25   ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
26   ;; has been closed so that FILE-POSITION is a nonsense question. So
27   ;; my (WHN) impression is that the conservative approach is to
28   ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
29   ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
30   ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
31   ;; time an error was reported.)
32   (if posp
33       (ignore-errors (file-position stream pos))
34       (ignore-errors (file-position stream))))
35 \f
36 ;;;; the CONDITION class
37
38 (/show0 "condition.lisp 20")
39
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41
42 (/show0 "condition.lisp 24")
43
44 (def!struct (condition-classoid (:include classoid)
45                                 (:constructor make-condition-classoid))
46   ;; list of CONDITION-SLOT structures for the direct slots of this
47   ;; class
48   (slots nil :type list)
49   ;; list of CONDITION-SLOT structures for all of the effective class
50   ;; slots of this class
51   (class-slots nil :type list)
52   ;; report function or NIL
53   (report nil :type (or function null))
54   ;; list of alternating initargs and initforms
55   (default-initargs () :type list)
56   ;; class precedence list as a list of CLASS objects, with all
57   ;; non-CONDITION classes removed
58   (cpl () :type list)
59   ;; a list of all the effective instance allocation slots of this
60   ;; class that have a non-constant initform or default-initarg.
61   ;; Values for these slots must be computed in the dynamic
62   ;; environment of MAKE-CONDITION.
63   (hairy-slots nil :type list))
64
65 (/show0 "condition.lisp 49")
66
67 ) ; EVAL-WHEN
68
69 (!defstruct-with-alternate-metaclass condition
70   :slot-names (actual-initargs assigned-slots)
71   :boa-constructor %make-condition-object
72   :superclass-name t
73   :metaclass-name condition-classoid
74   :metaclass-constructor make-condition-classoid
75   :dd-type structure)
76
77 (defun make-condition-object (actual-initargs)
78   (%make-condition-object actual-initargs nil))
79
80 (defstruct (condition-slot (:copier nil))
81   (name (missing-arg) :type symbol)
82   ;; list of all applicable initargs
83   (initargs (missing-arg) :type list)
84   ;; names of reader and writer functions
85   (readers (missing-arg) :type list)
86   (writers (missing-arg) :type list)
87   ;; true if :INITFORM was specified
88   (initform-p (missing-arg) :type (member t nil))
89   ;; If this is a function, call it with no args. Otherwise, it's the
90   ;; actual value.
91   (initform (missing-arg) :type t)
92   ;; allocation of this slot, or NIL until defaulted
93   (allocation nil :type (member :instance :class nil))
94   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
95   (cell nil :type (or cons null))
96   ;; slot documentation
97   (documentation nil :type (or string null)))
98
99 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
100 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
101 ;;; have themselves listed in their CPLs. This behavior is inherited
102 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
103 ;;; figured out whether it's right. -- WHN 19990612
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105   (/show0 "condition.lisp 103")
106   (let ((condition-class (locally
107                            ;; KLUDGE: There's a DEFTRANSFORM
108                            ;; FIND-CLASSOID for constant class names
109                            ;; which creates fast but
110                            ;; non-cold-loadable, non-compact code. In
111                            ;; this context, we'd rather have compact,
112                            ;; cold-loadable code. -- WHN 19990928
113                            (declare (notinline find-classoid))
114                            (find-classoid 'condition))))
115     (setf (condition-classoid-cpl condition-class)
116           (list condition-class)))
117   (/show0 "condition.lisp 103"))
118
119 (setf (condition-classoid-report (locally
120                                    ;; KLUDGE: There's a DEFTRANSFORM
121                                    ;; FIND-CLASSOID for constant class
122                                    ;; names which creates fast but
123                                    ;; non-cold-loadable, non-compact
124                                    ;; code. In this context, we'd
125                                    ;; rather have compact,
126                                    ;; cold-loadable code. -- WHN
127                                    ;; 19990928
128                                    (declare (notinline find-classoid))
129                                    (find-classoid 'condition)))
130       (lambda (cond stream)
131         (format stream "Condition ~S was signalled." (type-of cond))))
132
133 (eval-when (:compile-toplevel :load-toplevel :execute)
134
135 (defun find-condition-layout (name parent-types)
136   (let* ((cpl (remove-duplicates
137                (reverse
138                 (reduce #'append
139                         (mapcar (lambda (x)
140                                   (condition-classoid-cpl
141                                    (find-classoid x)))
142                                 parent-types)))))
143          (cond-layout (info :type :compiler-layout 'condition))
144          (olayout (info :type :compiler-layout name))
145          ;; FIXME: Does this do the right thing in case of multiple
146          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
147          ;; it obvious what ANSI intends to be done in the case of
148          ;; multiple inheritance, so it's not actually clear what the
149          ;; right thing is..
150          (new-inherits
151           (order-layout-inherits (concatenate 'simple-vector
152                                               (layout-inherits cond-layout)
153                                               (mapcar #'classoid-layout cpl)))))
154     (if (and olayout
155              (not (mismatch (layout-inherits olayout) new-inherits)))
156         olayout
157         (make-layout :classoid (make-undefined-classoid name)
158                      :inherits new-inherits
159                      :depthoid -1
160                      :length (layout-length cond-layout)))))
161
162 ) ; EVAL-WHEN
163
164 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
165 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
166 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
167 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
168 ;;;   a condition type C is equivalent to:
169 ;;;     (defmethod print-object ((x c) stream)
170 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
171 ;;; The current code doesn't seem to quite match that.
172 (def!method print-object ((x condition) stream)
173   (if *print-escape*
174       (if (and (typep x 'simple-condition) (slot-value x 'format-control))
175           (print-unreadable-object (x stream :type t :identity t)
176             (write (simple-condition-format-control x)
177                    :stream stream
178                    :lines 1))
179           (print-unreadable-object (x stream :type t :identity t)))
180       ;; KLUDGE: A comment from CMU CL here said
181       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
182       ;;   superclasses in define-condition call!
183       (dolist (class (condition-classoid-cpl (classoid-of x))
184                      (error "no REPORT? shouldn't happen!"))
185         (let ((report (condition-classoid-report class)))
186           (when report
187             (return (funcall report x stream)))))))
188 \f
189 ;;;; slots of CONDITION objects
190
191 (defvar *empty-condition-slot* '(empty))
192
193 (defun find-slot-default (class slot)
194   (let ((initargs (condition-slot-initargs slot))
195         (cpl (condition-classoid-cpl class)))
196     (dolist (class cpl)
197       (let ((default-initargs (condition-classoid-default-initargs class)))
198         (dolist (initarg initargs)
199           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
200             (unless (eq val *empty-condition-slot*)
201               (return-from find-slot-default
202                            (if (functionp val)
203                                (funcall val)
204                                val)))))))
205
206     (if (condition-slot-initform-p slot)
207         (let ((initform (condition-slot-initform slot)))
208           (if (functionp initform)
209               (funcall initform)
210               initform))
211         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
212
213 (defun find-condition-class-slot (condition-class slot-name)
214   (dolist (sclass
215            (condition-classoid-cpl condition-class)
216            (error "There is no slot named ~S in ~S."
217                   slot-name condition-class))
218     (dolist (slot (condition-classoid-slots sclass))
219       (when (eq (condition-slot-name slot) slot-name)
220         (return-from find-condition-class-slot slot)))))
221
222 (defun condition-writer-function (condition new-value name)
223   (dolist (cslot (condition-classoid-class-slots
224                   (layout-classoid (%instance-layout condition)))
225                  (setf (getf (condition-assigned-slots condition) name)
226                        new-value))
227     (when (eq (condition-slot-name cslot) name)
228       (return (setf (car (condition-slot-cell cslot)) new-value)))))
229
230 (defun condition-reader-function (condition name)
231   (let ((class (layout-classoid (%instance-layout condition))))
232     (dolist (cslot (condition-classoid-class-slots class))
233       (when (eq (condition-slot-name cslot) name)
234         (return-from condition-reader-function
235                      (car (condition-slot-cell cslot)))))
236     (let ((val (getf (condition-assigned-slots condition) name
237                      *empty-condition-slot*)))
238       (if (eq val *empty-condition-slot*)
239           (let ((actual-initargs (condition-actual-initargs condition))
240                 (slot (find-condition-class-slot class name)))
241             (unless slot
242               (error "missing slot ~S of ~S" name condition))
243             (do ((initargs actual-initargs (cddr initargs)))
244                 ((endp initargs)
245                  (setf (getf (condition-assigned-slots condition) name)
246                        (find-slot-default class slot)))
247               (when (member (car initargs) (condition-slot-initargs slot))
248                 (return-from condition-reader-function
249                   (setf (getf (condition-assigned-slots condition)
250                               name)
251                         (cadr initargs))))))
252           val))))
253 \f
254 ;;;; MAKE-CONDITION
255
256 (defun make-condition (type &rest args)
257   #!+sb-doc
258   "Make an instance of a condition object using the specified initargs."
259   ;; Note: ANSI specifies no exceptional situations in this function.
260   ;; signalling simple-type-error would not be wrong.
261   (let* ((type (or (and (symbolp type) (find-classoid type nil))
262                     type))
263          (class (typecase type
264                   (condition-classoid type)
265                   (class
266                    ;; Punt to CLOS.
267                    (return-from make-condition (apply #'make-instance type args)))
268                   (classoid
269                    (error 'simple-type-error
270                           :datum type
271                           :expected-type 'condition-class
272                           :format-control "~S is not a condition class."
273                           :format-arguments (list type)))
274                   (t
275                    (error 'simple-type-error
276                           :datum type
277                           :expected-type 'condition-class
278                           :format-control "Bad type argument:~%  ~S"
279                           :format-arguments (list type)))))
280          (res (make-condition-object args)))
281     (setf (%instance-layout res) (classoid-layout class))
282     ;; Set any class slots with initargs present in this call.
283     (dolist (cslot (condition-classoid-class-slots class))
284       (dolist (initarg (condition-slot-initargs cslot))
285         (let ((val (getf args initarg *empty-condition-slot*)))
286           (unless (eq val *empty-condition-slot*)
287             (setf (car (condition-slot-cell cslot)) val)))))
288     ;; Default any slots with non-constant defaults now.
289     (dolist (hslot (condition-classoid-hairy-slots class))
290       (when (dolist (initarg (condition-slot-initargs hslot) t)
291               (unless (eq (getf args initarg *empty-condition-slot*)
292                           *empty-condition-slot*)
293                 (return nil)))
294         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
295               (find-slot-default class hslot))))
296     res))
297 \f
298 ;;;; DEFINE-CONDITION
299
300 (eval-when (:compile-toplevel :load-toplevel :execute)
301 (defun %compiler-define-condition (name direct-supers layout
302                                    all-readers all-writers)
303   (with-single-package-locked-error
304       (:symbol name "defining ~A as a condition")
305     (sb!xc:proclaim `(ftype (function (t) t) ,@all-readers))
306     (sb!xc:proclaim `(ftype (function (t t) t) ,@all-writers))
307     (multiple-value-bind (class old-layout)
308         (insured-find-classoid name
309                                #'condition-classoid-p
310                                #'make-condition-classoid)
311       (setf (layout-classoid layout) class)
312       (setf (classoid-direct-superclasses class)
313             (mapcar #'find-classoid direct-supers))
314       (cond ((not old-layout)
315              (register-layout layout))
316             ((not *type-system-initialized*)
317              (setf (layout-classoid old-layout) class)
318              (setq layout old-layout)
319              (unless (eq (classoid-layout class) layout)
320                (register-layout layout)))
321             ((redefine-layout-warning "current"
322                                       old-layout
323                                       "new"
324                                       (layout-length layout)
325                                       (layout-inherits layout)
326                                       (layout-depthoid layout)
327                                       (layout-n-untagged-slots layout))
328              (register-layout layout :invalidate t))
329             ((not (classoid-layout class))
330              (register-layout layout)))
331
332       (setf (layout-info layout)
333             (locally
334                 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
335                 ;; names which creates fast but non-cold-loadable, non-compact
336                 ;; code. In this context, we'd rather have compact, cold-loadable
337                 ;; code. -- WHN 19990928
338                 (declare (notinline find-classoid))
339               (layout-info (classoid-layout (find-classoid 'condition)))))
340
341       (setf (find-classoid name) class)
342
343       ;; Initialize CPL slot.
344       (setf (condition-classoid-cpl class)
345             (remove-if-not #'condition-classoid-p
346                            (std-compute-class-precedence-list class)))))
347   (values))
348 ) ; EVAL-WHEN
349
350 ;;; Compute the effective slots of CLASS, copying inherited slots and
351 ;;; destructively modifying direct slots.
352 ;;;
353 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
354 ;;; direct slots. Presumably it follows from the semantics of
355 ;;; inheritance and redefinition of conditions, but finding the cite
356 ;;; and documenting it here would be good. (Or, if this is not in fact
357 ;;; ANSI-compliant, fixing it would also be good.:-)
358 (defun compute-effective-slots (class)
359   (collect ((res (copy-list (condition-classoid-slots class))))
360     (dolist (sclass (cdr (condition-classoid-cpl class)))
361       (dolist (sslot (condition-classoid-slots sclass))
362         (let ((found (find (condition-slot-name sslot) (res)
363                            :key #'condition-slot-name)))
364           (cond (found
365                  (setf (condition-slot-initargs found)
366                        (union (condition-slot-initargs found)
367                               (condition-slot-initargs sslot)))
368                  (unless (condition-slot-initform-p found)
369                    (setf (condition-slot-initform-p found)
370                          (condition-slot-initform-p sslot))
371                    (setf (condition-slot-initform found)
372                          (condition-slot-initform sslot)))
373                  (unless (condition-slot-allocation found)
374                    (setf (condition-slot-allocation found)
375                          (condition-slot-allocation sslot))))
376                 (t
377                  (res (copy-structure sslot)))))))
378     (res)))
379
380 ;;; Early definitions of slot accessor creators.
381 ;;;
382 ;;; Slot accessors must be generic functions, but ANSI does not seem
383 ;;; to specify any of them, and we cannot support it before end of
384 ;;; warm init. So we use ordinary functions inside SBCL, and switch to
385 ;;; GFs only at the end of building.
386 (declaim (notinline install-condition-slot-reader
387                     install-condition-slot-writer))
388 (defun install-condition-slot-reader (name condition slot-name)
389   (declare (ignore condition))
390   (setf (fdefinition name)
391         (lambda (condition)
392           (condition-reader-function condition slot-name))))
393 (defun install-condition-slot-writer (name condition slot-name)
394   (declare (ignore condition))
395   (setf (fdefinition name)
396         (lambda (new-value condition)
397           (condition-writer-function condition new-value slot-name))))
398
399 (defvar *define-condition-hooks* nil)
400
401 (defun %set-condition-report (name report)
402   (setf (condition-classoid-report (find-classoid name))
403         report))
404
405 (defun %define-condition (name parent-types layout slots documentation
406                           default-initargs all-readers all-writers
407                           source-location)
408   (with-single-package-locked-error
409       (:symbol name "defining ~A as a condition")
410     (%compiler-define-condition name parent-types layout all-readers all-writers)
411     (sb!c:with-source-location (source-location)
412       (setf (layout-source-location layout)
413             source-location))
414     (let ((class (find-classoid name)))
415       (setf (condition-classoid-slots class) slots)
416       (setf (condition-classoid-default-initargs class) default-initargs)
417       (setf (fdocumentation name 'type) documentation)
418
419       (dolist (slot slots)
420
421         ;; Set up reader and writer functions.
422         (let ((slot-name (condition-slot-name slot)))
423           (dolist (reader (condition-slot-readers slot))
424             (install-condition-slot-reader reader name slot-name))
425           (dolist (writer (condition-slot-writers slot))
426             (install-condition-slot-writer writer name slot-name))))
427
428       ;; Compute effective slots and set up the class and hairy slots
429       ;; (subsets of the effective slots.)
430       (let ((eslots (compute-effective-slots class))
431             (e-def-initargs
432              (reduce #'append
433                      (mapcar #'condition-classoid-default-initargs
434                            (condition-classoid-cpl class)))))
435         (dolist (slot eslots)
436           (ecase (condition-slot-allocation slot)
437             (:class
438              (unless (condition-slot-cell slot)
439                (setf (condition-slot-cell slot)
440                      (list (if (condition-slot-initform-p slot)
441                                (let ((initform (condition-slot-initform slot)))
442                                  (if (functionp initform)
443                                      (funcall initform)
444                                      initform))
445                                *empty-condition-slot*))))
446              (push slot (condition-classoid-class-slots class)))
447             ((:instance nil)
448              (setf (condition-slot-allocation slot) :instance)
449              (when (or (functionp (condition-slot-initform slot))
450                        (dolist (initarg (condition-slot-initargs slot) nil)
451                          (when (functionp (getf e-def-initargs initarg))
452                            (return t))))
453                (push slot (condition-classoid-hairy-slots class)))))))
454       (when (boundp '*define-condition-hooks*)
455         (dolist (fun *define-condition-hooks*)
456           (funcall fun class))))
457     name))
458
459 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
460                                  &body options)
461   #!+sb-doc
462   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
463    Define NAME as a condition type. This new type inherits slots and its
464    report function from the specified PARENT-TYPEs. A slot spec is a list of:
465      (slot-name :reader <rname> :initarg <iname> {Option Value}*
466
467    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
468    and :TYPE and the overall options :DEFAULT-INITARGS and
469    [type] :DOCUMENTATION are also allowed.
470
471    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
472    a string or a two-argument lambda or function name. If a function, the
473    function is called with the condition and stream to report the condition.
474    If a string, the string is printed.
475
476    Condition types are classes, but (as allowed by ANSI and not as described in
477    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
478    SLOT-VALUE may not be used on condition objects."
479   (let* ((parent-types (or parent-types '(condition)))
480          (layout (find-condition-layout name parent-types))
481          (documentation nil)
482          (report nil)
483          (default-initargs ()))
484     (collect ((slots)
485               (all-readers nil append)
486               (all-writers nil append))
487       (dolist (spec slot-specs)
488         (when (keywordp spec)
489           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
490                 spec))
491         (let* ((spec (if (consp spec) spec (list spec)))
492                (slot-name (first spec))
493                (allocation :instance)
494                (initform-p nil)
495                documentation
496                initform)
497           (collect ((initargs)
498                     (readers)
499                     (writers))
500             (do ((options (rest spec) (cddr options)))
501                 ((null options))
502               (unless (and (consp options) (consp (cdr options)))
503                 (error "malformed condition slot spec:~%  ~S." spec))
504               (let ((arg (second options)))
505                 (case (first options)
506                   (:reader (readers arg))
507                   (:writer (writers arg))
508                   (:accessor
509                    (readers arg)
510                    (writers `(setf ,arg)))
511                   (:initform
512                    (when initform-p
513                      (error "more than one :INITFORM in ~S" spec))
514                    (setq initform-p t)
515                    (setq initform arg))
516                   (:initarg (initargs arg))
517                   (:allocation
518                    (setq allocation arg))
519                   (:documentation
520                    (when documentation
521                      (error "more than one :DOCUMENTATION in ~S" spec))
522                    (unless (stringp arg)
523                      (error "slot :DOCUMENTATION argument is not a string: ~S"
524                             arg))
525                    (setq documentation arg))
526                   (:type)
527                   (t
528                    (error "unknown slot option:~%  ~S" (first options))))))
529
530             (all-readers (readers))
531             (all-writers (writers))
532             (slots `(make-condition-slot
533                      :name ',slot-name
534                      :initargs ',(initargs)
535                      :readers ',(readers)
536                      :writers ',(writers)
537                      :initform-p ',initform-p
538                      :documentation ',documentation
539                      :initform
540                      ,(if (sb!xc:constantp initform)
541                           `',(constant-form-value initform)
542                           `#'(lambda () ,initform)))))))
543
544       (dolist (option options)
545         (unless (consp option)
546           (error "bad option:~%  ~S" option))
547         (case (first option)
548           (:documentation (setq documentation (second option)))
549           (:report
550            (let ((arg (second option)))
551              (setq report
552                    (if (stringp arg)
553                        `#'(lambda (condition stream)
554                             (declare (ignore condition))
555                             (write-string ,arg stream))
556                        `#'(lambda (condition stream)
557                             (funcall #',arg condition stream))))))
558           (:default-initargs
559            (do ((initargs (rest option) (cddr initargs)))
560                ((endp initargs))
561              (let ((val (second initargs)))
562                (setq default-initargs
563                      (list* `',(first initargs)
564                             (if (sb!xc:constantp val)
565                                 `',(constant-form-value val)
566                                 `#'(lambda () ,val))
567                             default-initargs)))))
568           (t
569            (error "unknown option: ~S" (first option)))))
570
571       `(progn
572          (eval-when (:compile-toplevel)
573            (%compiler-define-condition ',name ',parent-types ',layout
574                                        ',(all-readers) ',(all-writers)))
575          (eval-when (:load-toplevel :execute)
576            (%define-condition ',name
577                               ',parent-types
578                               ',layout
579                               (list ,@(slots))
580                               ,documentation
581                               (list ,@default-initargs)
582                               ',(all-readers)
583                               ',(all-writers)
584                               (sb!c:source-location))
585            ;; This needs to be after %DEFINE-CONDITION in case :REPORT
586            ;; is a lambda referring to condition slot accessors:
587            ;; they're not proclaimed as functions before it has run if
588            ;; we're under EVAL or loaded as source.
589            (%set-condition-report ',name ,report))))))
590 \f
591 ;;;; various CONDITIONs specified by ANSI
592
593 (define-condition serious-condition (condition) ())
594
595 (define-condition error (serious-condition) ())
596
597 (define-condition warning (condition) ())
598 (define-condition style-warning (warning) ())
599
600 (defun simple-condition-printer (condition stream)
601   (let ((control (simple-condition-format-control condition)))
602     (if control
603         (apply #'format stream
604                control
605                (simple-condition-format-arguments condition))
606         (error "No format-control for ~S" condition))))
607
608 (define-condition simple-condition ()
609   ((format-control :reader simple-condition-format-control
610                    :initarg :format-control
611                    :initform nil
612                    :type format-control)
613    (format-arguments :reader simple-condition-format-arguments
614                      :initarg :format-arguments
615                      :initform nil
616                      :type list))
617   (:report simple-condition-printer))
618
619 (define-condition simple-warning (simple-condition warning) ())
620
621 (define-condition simple-error (simple-condition error) ())
622
623 (define-condition storage-condition (serious-condition) ())
624
625 (define-condition type-error (error)
626   ((datum :reader type-error-datum :initarg :datum)
627    (expected-type :reader type-error-expected-type :initarg :expected-type))
628   (:report
629    (lambda (condition stream)
630      (format stream
631              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
632              (type-error-datum condition)
633              (type-error-expected-type condition)))))
634
635 (def!method print-object ((condition type-error) stream)
636   (if *print-escape*
637       (flet ((maybe-string (thing)
638                (ignore-errors
639                  (write-to-string thing :lines 1 :readably nil :array nil :pretty t))))
640         (let ((type (maybe-string (type-error-expected-type condition)))
641               (datum (maybe-string (type-error-datum condition))))
642           (if (and type datum)
643               (print-unreadable-object (condition stream :type t)
644                 (format stream "~@<expected-type: ~A ~_datum: ~A~:@>" type datum))
645               (call-next-method))))
646       (call-next-method)))
647
648 ;;; not specified by ANSI, but too useful not to have around.
649 (define-condition simple-style-warning (simple-condition style-warning) ())
650 (define-condition simple-type-error (simple-condition type-error) ())
651
652 ;; Can't have a function called SIMPLE-TYPE-ERROR or TYPE-ERROR...
653 (declaim (ftype (sfunction (t t t &rest t) nil) bad-type))
654 (defun bad-type (datum type control &rest arguments)
655   (error 'simple-type-error
656          :datum datum
657          :expected-type type
658          :format-control control
659          :format-arguments arguments))
660
661 (define-condition program-error (error) ())
662 (define-condition parse-error   (error) ())
663 (define-condition control-error (error) ())
664 (define-condition stream-error  (error)
665   ((stream :reader stream-error-stream :initarg :stream)))
666
667 (define-condition end-of-file (stream-error) ()
668   (:report
669    (lambda (condition stream)
670      (format stream
671              "end of file on ~S"
672              (stream-error-stream condition)))))
673
674 (define-condition closed-stream-error (stream-error) ()
675   (:report
676    (lambda (condition stream)
677      (format stream "~S is closed" (stream-error-stream condition)))))
678
679 (define-condition file-error (error)
680   ((pathname :reader file-error-pathname :initarg :pathname))
681   (:report
682    (lambda (condition stream)
683      (format stream "error on file ~S" (file-error-pathname condition)))))
684
685 (define-condition package-error (error)
686   ((package :reader package-error-package :initarg :package)))
687
688 (define-condition cell-error (error)
689   ((name :reader cell-error-name :initarg :name)))
690
691 (def!method print-object ((condition cell-error) stream)
692   (if (and *print-escape* (slot-boundp condition 'name))
693       (print-unreadable-object (condition stream :type t :identity t)
694         (princ (cell-error-name condition) stream))
695       (call-next-method)))
696
697 (define-condition unbound-variable (cell-error) ()
698   (:report
699    (lambda (condition stream)
700      (format stream
701              "The variable ~S is unbound."
702              (cell-error-name condition)))))
703
704 (define-condition undefined-function (cell-error) ()
705   (:report
706    (lambda (condition stream)
707      (format stream
708              "The function ~/sb-impl::print-symbol-with-prefix/ is undefined."
709              (cell-error-name condition)))))
710
711 (define-condition special-form-function (undefined-function) ()
712   (:report
713    (lambda (condition stream)
714      (format stream
715              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
716              (cell-error-name condition)))))
717
718 (define-condition arithmetic-error (error)
719   ((operation :reader arithmetic-error-operation
720               :initarg :operation
721               :initform nil)
722    (operands :reader arithmetic-error-operands
723              :initarg :operands))
724   (:report (lambda (condition stream)
725              (format stream
726                      "arithmetic error ~S signalled"
727                      (type-of condition))
728              (when (arithmetic-error-operation condition)
729                (format stream
730                        "~%Operation was ~S, operands ~S."
731                        (arithmetic-error-operation condition)
732                        (arithmetic-error-operands condition))))))
733
734 (define-condition division-by-zero         (arithmetic-error) ())
735 (define-condition floating-point-overflow  (arithmetic-error) ())
736 (define-condition floating-point-underflow (arithmetic-error) ())
737 (define-condition floating-point-inexact   (arithmetic-error) ())
738 (define-condition floating-point-invalid-operation (arithmetic-error) ())
739
740 (define-condition print-not-readable (error)
741   ((object :reader print-not-readable-object :initarg :object))
742   (:report
743    (lambda (condition stream)
744      (let ((obj (print-not-readable-object condition))
745            (*print-array* nil))
746        (format stream "~S cannot be printed readably." obj)))))
747
748 (define-condition reader-error (parse-error stream-error) ()
749   (:report (lambda (condition stream)
750              (%report-reader-error condition stream))))
751
752 ;;; a READER-ERROR whose REPORTing is controlled by FORMAT-CONTROL and
753 ;;; FORMAT-ARGS (the usual case for READER-ERRORs signalled from
754 ;;; within SBCL itself)
755 ;;;
756 ;;; (Inheriting CL:SIMPLE-CONDITION here isn't quite consistent with
757 ;;; the letter of the ANSI spec: this is not a condition signalled by
758 ;;; SIGNAL when a format-control is supplied by the function's first
759 ;;; argument. It seems to me (WHN) to be basically in the spirit of
760 ;;; the spec, but if not, it'd be straightforward to do our own
761 ;;; DEFINE-CONDITION SB-INT:SIMPLISTIC-CONDITION with
762 ;;; FORMAT-CONTROL and FORMAT-ARGS slots, and use that condition in
763 ;;; place of CL:SIMPLE-CONDITION here.)
764 (define-condition simple-reader-error (reader-error simple-condition)
765   ()
766   (:report (lambda (condition stream)
767              (%report-reader-error condition stream :simple t))))
768
769 ;;; base REPORTing of a READER-ERROR
770 ;;;
771 ;;; When SIMPLE, we expect and use SIMPLE-CONDITION-ish FORMAT-CONTROL
772 ;;; and FORMAT-ARGS slots.
773 (defun %report-reader-error (condition stream &key simple)
774   (let* ((error-stream (stream-error-stream condition))
775          (pos (file-position-or-nil-for-error error-stream)))
776     (when (and pos (plusp pos))
777       ;; FILE-POSITION is the next character -- error is at the previous one.
778       (decf pos))
779     (let (lineno colno)
780       (when (and pos
781                  (< pos sb!xc:array-dimension-limit)
782                  ;; KLUDGE: lseek() (which is what FILE-POSITION
783                  ;; reduces to on file-streams) is undefined on
784                  ;; "some devices", which in practice means that it
785                  ;; can claim to succeed on /dev/stdin on Darwin
786                  ;; and Solaris.  This is obviously bad news,
787                  ;; because the READ-SEQUENCE below will then
788                  ;; block, not complete, and the report will never
789                  ;; be printed.  As a workaround, we exclude
790                  ;; interactive streams from this attempt to report
791                  ;; positions.  -- CSR, 2003-08-21
792                  (not (interactive-stream-p error-stream))
793                  (file-position error-stream :start))
794         (let ((string
795                (make-string pos
796                             :element-type (stream-element-type
797                                            error-stream))))
798           (when (= pos (read-sequence string error-stream))
799             ;; Lines count from 1, columns from 0. It's stupid and traditional.
800             (setq lineno (1+ (count #\Newline string))
801                   colno (- pos (or (position #\Newline string :from-end t) 0)))))
802         (file-position-or-nil-for-error error-stream pos))
803       (pprint-logical-block (stream nil)
804         (if simple
805             (apply #'format stream
806                    (simple-condition-format-control condition)
807                    (simple-condition-format-arguments condition))
808             (prin1 (class-name (class-of condition)) stream))
809         (format stream "~2I~@[~_~_~:{~:(~A~): ~S~:^, ~:_~}~]~_~_Stream: ~S"
810                 (remove-if-not #'second
811                                (list (list :line lineno)
812                                      (list :column colno)
813                                      (list :file-position pos)))
814                 error-stream)))))
815 \f
816 ;;;; special SBCL extension conditions
817
818 ;;; an error apparently caused by a bug in SBCL itself
819 ;;;
820 ;;; Note that we don't make any serious effort to use this condition
821 ;;; for *all* errors in SBCL itself. E.g. type errors and array
822 ;;; indexing errors can occur in functions called from SBCL code, and
823 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
824 ;;; because the signalling code has no good way to know that the
825 ;;; underlying problem is a bug in SBCL. But in the fairly common case
826 ;;; that the signalling code does know that it's found a bug in SBCL,
827 ;;; this condition is appropriate, reusing boilerplate and helping
828 ;;; users to recognize it as an SBCL bug.
829 (define-condition bug (simple-error)
830   ()
831   (:report
832    (lambda (condition stream)
833      (format stream
834              "~@<  ~? ~:@_~?~:>"
835              (simple-condition-format-control condition)
836              (simple-condition-format-arguments condition)
837              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
838               SBCL might have been corrupted by bad user code, e.g. by an ~
839               undefined Lisp operation like ~S, or by stray pointers from ~
840               alien code or from unsafe Lisp code; or there might be a bug ~
841               in the OS or hardware that SBCL is running on.) If it seems to ~
842               be a bug in SBCL itself, the maintainers would like to know ~
843               about it. Bug reports are welcome on the SBCL ~
844               mailing lists, which you can find at ~
845               <http://sbcl.sourceforge.net/>.~:@>"
846              '((fmakunbound 'compile))))))
847
848 (define-condition simple-storage-condition (storage-condition simple-condition)
849   ())
850
851 ;;; a condition for use in stubs for operations which aren't supported
852 ;;; on some platforms
853 ;;;
854 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
855 ;;;   #-(or freebsd linux)
856 ;;;   (defun load-foreign (&rest rest)
857 ;;;     (error 'unsupported-operator :name 'load-foreign))
858 ;;;   #+(or freebsd linux)
859 ;;;   (defun load-foreign ... actual definition ...)
860 ;;; By signalling a standard condition in this case, we make it
861 ;;; possible for test code to distinguish between (1) intentionally
862 ;;; unimplemented and (2) unintentionally just screwed up somehow.
863 ;;; (Before this condition was defined, test code tried to deal with
864 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
865 ;;; sbcl-0.7.0, a package screwup left the definition of
866 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
867 ;;; architectures where it was supposed to be supported, and the
868 ;;; regression tests cheerfully passed because they assumed that
869 ;;; unFBOUNDPness meant they were running on an system which didn't
870 ;;; support the extension.)
871 (define-condition unsupported-operator (simple-error) ())
872 \f
873 ;;; (:ansi-cl :function remove)
874 ;;; (:ansi-cl :section (a b c))
875 ;;; (:ansi-cl :glossary "similar")
876 ;;;
877 ;;; (:sbcl :node "...")
878 ;;; (:sbcl :variable *ed-functions*)
879 ;;;
880 ;;; FIXME: this is not the right place for this.
881 (defun print-reference (reference stream)
882   (ecase (car reference)
883     (:amop
884      (format stream "AMOP")
885      (format stream ", ")
886      (destructuring-bind (type data) (cdr reference)
887        (ecase type
888          (:readers "Readers for ~:(~A~) Metaobjects"
889                    (substitute #\  #\- (symbol-name data)))
890          (:initialization
891           (format stream "Initialization of ~:(~A~) Metaobjects"
892                   (substitute #\  #\- (symbol-name data))))
893          (:generic-function (format stream "Generic Function ~S" data))
894          (:function (format stream "Function ~S" data))
895          (:section (format stream "Section ~{~D~^.~}" data)))))
896     (:ansi-cl
897      (format stream "The ANSI Standard")
898      (format stream ", ")
899      (destructuring-bind (type data) (cdr reference)
900        (ecase type
901          (:function (format stream "Function ~S" data))
902          (:special-operator (format stream "Special Operator ~S" data))
903          (:macro (format stream "Macro ~S" data))
904          (:section (format stream "Section ~{~D~^.~}" data))
905          (:glossary (format stream "Glossary entry for ~S" data))
906          (:issue (format stream "writeup for Issue ~A" data)))))
907     (:sbcl
908      (format stream "The SBCL Manual")
909      (format stream ", ")
910      (destructuring-bind (type data) (cdr reference)
911        (ecase type
912          (:node (format stream "Node ~S" data))
913          (:variable (format stream "Variable ~S" data))
914          (:function (format stream "Function ~S" data)))))
915     ;; FIXME: other documents (e.g. CLIM, Franz documentation :-)
916     ))
917 (define-condition reference-condition ()
918   ((references :initarg :references :reader reference-condition-references)))
919 (defvar *print-condition-references* t)
920 (def!method print-object :around ((o reference-condition) s)
921   (call-next-method)
922   (unless (or *print-escape* *print-readably*)
923     (when (and *print-condition-references*
924                (reference-condition-references o))
925       (format s "~&See also:~%")
926       (pprint-logical-block (s nil :per-line-prefix "  ")
927         (do* ((rs (reference-condition-references o) (cdr rs))
928               (r (car rs) (car rs)))
929              ((null rs))
930           (print-reference r s)
931           (unless (null (cdr rs))
932             (terpri s)))))))
933
934 (define-condition simple-reference-error (reference-condition simple-error)
935   ())
936
937 (define-condition simple-reference-warning (reference-condition simple-warning)
938   ())
939
940 (define-condition arguments-out-of-domain-error
941     (arithmetic-error reference-condition)
942   ())
943
944 (define-condition duplicate-definition (reference-condition warning)
945   ((name :initarg :name :reader duplicate-definition-name))
946   (:report (lambda (c s)
947              (format s "~@<Duplicate definition for ~S found in ~
948                         one file.~@:>"
949                      (duplicate-definition-name c))))
950   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
951
952 (define-condition constant-modified (reference-condition warning)
953   ((fun-name :initarg :fun-name :reader constant-modified-fun-name))
954   (:report (lambda (c s)
955              (format s "~@<Destructive function ~S called on ~
956                         constant data.~@:>"
957                      (constant-modified-fun-name c))))
958   (:default-initargs :references (list '(:ansi-cl :special-operator quote)
959                                        '(:ansi-cl :section (3 2 2 3)))))
960
961 (define-condition package-at-variance (reference-condition simple-warning)
962   ()
963   (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
964
965 (define-condition defconstant-uneql (reference-condition error)
966   ((name :initarg :name :reader defconstant-uneql-name)
967    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
968    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
969   (:report
970    (lambda (condition stream)
971      (format stream
972              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
973              (defconstant-uneql-name condition)
974              (defconstant-uneql-old-value condition)
975              (defconstant-uneql-new-value condition))))
976   (:default-initargs :references (list '(:ansi-cl :macro defconstant)
977                                        '(:sbcl :node "Idiosyncrasies"))))
978
979 (define-condition array-initial-element-mismatch
980     (reference-condition simple-warning)
981   ()
982   (:default-initargs
983       :references (list
984                    '(:ansi-cl :function make-array)
985                    '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
986
987 (define-condition type-warning (reference-condition simple-warning)
988   ()
989   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
990 (define-condition type-style-warning (reference-condition simple-style-warning)
991   ()
992   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
993
994 (define-condition local-argument-mismatch (reference-condition simple-warning)
995   ()
996   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
997
998 (define-condition format-args-mismatch (reference-condition)
999   ()
1000   (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
1001
1002 (define-condition format-too-few-args-warning
1003     (format-args-mismatch simple-warning)
1004   ())
1005 (define-condition format-too-many-args-warning
1006     (format-args-mismatch simple-style-warning)
1007   ())
1008
1009 (define-condition implicit-generic-function-warning (style-warning)
1010   ((name :initarg :name :reader implicit-generic-function-name))
1011   (:report
1012    (lambda (condition stream)
1013      (format stream "~@<Implicitly creating new generic function ~S.~:@>"
1014              (implicit-generic-function-name condition)))))
1015
1016 (define-condition extension-failure (reference-condition simple-error)
1017   ())
1018
1019 (define-condition structure-initarg-not-keyword
1020     (reference-condition simple-style-warning)
1021   ()
1022   (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
1023
1024 #!+sb-package-locks
1025 (progn
1026
1027 (define-condition package-lock-violation (package-error
1028                                           reference-condition
1029                                           simple-condition)
1030   ((current-package :initform *package*
1031                     :reader package-lock-violation-in-package))
1032   (:report
1033    (lambda (condition stream)
1034      (let ((control (simple-condition-format-control condition))
1035            (error-package (package-name (package-error-package condition)))
1036            (current-package (package-name (package-lock-violation-in-package condition))))
1037        (if control
1038            (apply #'format stream
1039                   (format nil "~~@<Lock on package ~A violated when ~A while in package ~A.~~:@>"
1040                           error-package
1041                           control
1042                           current-package)
1043                   (simple-condition-format-arguments condition))
1044            (format stream "~@<Lock on package ~A violated while in package ~A.~:@>"
1045                    error-package
1046                    current-package)))))
1047   ;; no :default-initargs -- reference-stuff provided by the
1048   ;; signalling form in target-package.lisp
1049   #!+sb-doc
1050   (:documentation
1051    "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
1052 when a package-lock is violated."))
1053
1054 (define-condition package-locked-error (package-lock-violation) ()
1055   #!+sb-doc
1056   (:documentation
1057    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1058 signalled when an operation on a package violates a package lock."))
1059
1060 (define-condition symbol-package-locked-error (package-lock-violation)
1061   ((symbol :initarg :symbol :reader package-locked-error-symbol))
1062   #!+sb-doc
1063   (:documentation
1064    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1065 signalled when an operation on a symbol violates a package lock. The
1066 symbol that caused the violation is accessed by the function
1067 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
1068
1069 ) ; progn
1070
1071 (define-condition undefined-alien-error (cell-error) ()
1072   (:report
1073    (lambda (condition stream)
1074      (if (slot-boundp condition 'name)
1075          (format stream "Undefined alien: ~S" (cell-error-name condition))
1076          (format stream "Undefined alien symbol.")))))
1077
1078 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
1079   (:report
1080    (lambda (condition stream)
1081      (declare (ignore condition))
1082      (format stream "Attempt to access an undefined alien variable."))))
1083
1084 (define-condition undefined-alien-function-error (undefined-alien-error) ()
1085   (:report
1086    (lambda (condition stream)
1087      (declare (ignore condition))
1088      (format stream "Attempt to call an undefined alien function."))))
1089
1090 \f
1091 ;;;; various other (not specified by ANSI) CONDITIONs
1092 ;;;;
1093 ;;;; These might logically belong in other files; they're here, after
1094 ;;;; setup of CONDITION machinery, only because that makes it easier to
1095 ;;;; get cold init to work.
1096
1097 ;;; OAOOM warning: see cross-condition.lisp
1098 (define-condition encapsulated-condition (condition)
1099   ((condition :initarg :condition :reader encapsulated-condition)))
1100
1101 ;;; KLUDGE: a condition for floating point errors when we can't or
1102 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1103 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1104 ;;; know how but the old code was broken by the conversion to POSIX
1105 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1106 ;;;
1107 ;;; FIXME: Perhaps this should also be a base class for all
1108 ;;; floating point exceptions?
1109 (define-condition floating-point-exception (arithmetic-error)
1110   ((flags :initarg :traps
1111           :initform nil
1112           :reader floating-point-exception-traps))
1113   (:report (lambda (condition stream)
1114              (format stream
1115                      "An arithmetic error ~S was signalled.~%"
1116                      (type-of condition))
1117              (let ((traps (floating-point-exception-traps condition)))
1118                (if traps
1119                    (format stream
1120                            "Trapping conditions are: ~%~{ ~S~^~}~%"
1121                            traps)
1122                    (write-line
1123                     "No traps are enabled? How can this be?"
1124                     stream))))))
1125
1126 (define-condition invalid-array-index-error (type-error)
1127   ((array :initarg :array :reader invalid-array-index-error-array)
1128    (axis :initarg :axis :reader invalid-array-index-error-axis))
1129   (:report
1130    (lambda (condition stream)
1131      (let ((array (invalid-array-index-error-array condition)))
1132        (format stream "Index ~W out of bounds for ~@[axis ~W of ~]~S, ~
1133                        should be nonnegative and <~W."
1134                (type-error-datum condition)
1135                (when (> (array-rank array) 1)
1136                  (invalid-array-index-error-axis condition))
1137                (type-of array)
1138                ;; Extract the bound from (INTEGER 0 (BOUND))
1139                (caaddr (type-error-expected-type condition)))))))
1140
1141 (define-condition invalid-array-error (reference-condition type-error) ()
1142   (:report
1143    (lambda (condition stream)
1144      (let ((*print-array* nil))
1145        (format stream
1146                "~@<Displaced array originally of type ~S has been invalidated ~
1147                 due its displaced-to array ~S having become too small to hold ~
1148                 it: the displaced array's dimensions have all been set to zero ~
1149                 to trap accesses to it.~:@>"
1150                (type-error-expected-type condition)
1151                (array-displacement (type-error-datum condition))))))
1152   (:default-initargs
1153       :references
1154       (list '(:ansi-cl :function adjust-array))))
1155
1156 (define-condition index-too-large-error (type-error)
1157   ()
1158   (:report
1159    (lambda (condition stream)
1160      (format stream
1161              "The index ~S is too large."
1162              (type-error-datum condition)))))
1163
1164 (define-condition bounding-indices-bad-error (reference-condition type-error)
1165   ((object :reader bounding-indices-bad-object :initarg :object))
1166   (:report
1167    (lambda (condition stream)
1168      (let* ((datum (type-error-datum condition))
1169             (start (car datum))
1170             (end (cdr datum))
1171             (object (bounding-indices-bad-object condition)))
1172        (etypecase object
1173          (sequence
1174           (format stream
1175                   "The bounding indices ~S and ~S are bad ~
1176                    for a sequence of length ~S."
1177                   start end (length object)))
1178          (array
1179           ;; from WITH-ARRAY-DATA
1180           (format stream
1181                   "The START and END parameters ~S and ~S are ~
1182                    bad for an array of total size ~S."
1183                   start end (array-total-size object)))))))
1184   (:default-initargs
1185       :references
1186       (list '(:ansi-cl :glossary "bounding index designator")
1187             '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1188
1189 (define-condition nil-array-accessed-error (reference-condition type-error)
1190   ()
1191   (:report (lambda (condition stream)
1192              (declare (ignore condition))
1193              (format stream
1194                      "An attempt to access an array of element-type ~
1195                       NIL was made.  Congratulations!")))
1196   (:default-initargs
1197       :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1198                         '(:ansi-cl :section (15 1 2 1))
1199                         '(:ansi-cl :section (15 1 2 2)))))
1200
1201 (define-condition namestring-parse-error (parse-error)
1202   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1203    (args :reader namestring-parse-error-args :initarg :args :initform nil)
1204    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1205    (offset :reader namestring-parse-error-offset :initarg :offset))
1206   (:report
1207    (lambda (condition stream)
1208      (format stream
1209              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
1210              (namestring-parse-error-complaint condition)
1211              (namestring-parse-error-args condition)
1212              (namestring-parse-error-namestring condition)
1213              (namestring-parse-error-offset condition)))))
1214
1215 (define-condition simple-package-error (simple-condition package-error) ())
1216
1217 (define-condition simple-reader-package-error (simple-reader-error) ())
1218
1219 (define-condition reader-eof-error (end-of-file)
1220   ((context :reader reader-eof-error-context :initarg :context))
1221   (:report
1222    (lambda (condition stream)
1223      (format stream
1224              "unexpected end of file on ~S ~A"
1225              (stream-error-stream condition)
1226              (reader-eof-error-context condition)))))
1227
1228 (define-condition reader-impossible-number-error (simple-reader-error)
1229   ((error :reader reader-impossible-number-error-error :initarg :error))
1230   (:report
1231    (lambda (condition stream)
1232      (let ((error-stream (stream-error-stream condition)))
1233        (format stream
1234                "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1235                (file-position-or-nil-for-error error-stream) error-stream
1236                (simple-condition-format-control condition)
1237                (simple-condition-format-arguments condition)
1238                (reader-impossible-number-error-error condition))))))
1239
1240 (define-condition standard-readtable-modified-error (reference-condition error)
1241   ((operation :initarg :operation :reader standard-readtable-modified-operation))
1242   (:report (lambda (condition stream)
1243              (format stream "~S would modify the standard readtable."
1244                      (standard-readtable-modified-operation condition))))
1245   (:default-initargs :references `((:ansi-cl :section (2 1 1 2))
1246                                    (:ansi-cl :glossary "standard readtable"))))
1247
1248 (define-condition standard-pprint-dispatch-table-modified-error
1249     (reference-condition error)
1250   ((operation :initarg :operation
1251               :reader standard-pprint-dispatch-table-modified-operation))
1252   (:report (lambda (condition stream)
1253              (format stream "~S would modify the standard pprint dispatch table."
1254                      (standard-pprint-dispatch-table-modified-operation
1255                       condition))))
1256   (:default-initargs
1257       :references `((:ansi-cl :glossary "standard pprint dispatch table"))))
1258
1259 (define-condition timeout (serious-condition)
1260   ((seconds :initarg :seconds :initform nil :reader timeout-seconds))
1261   (:report (lambda (condition stream)
1262              (format stream "Timeout occurred~@[ after ~A seconds~]."
1263                      (timeout-seconds condition)))))
1264
1265 (define-condition io-timeout (stream-error timeout)
1266   ((direction :reader io-timeout-direction :initarg :direction))
1267   (:report
1268    (lambda (condition stream)
1269      (declare (type stream stream))
1270      (format stream
1271              "I/O timeout while doing ~(~A~) on ~S."
1272              (io-timeout-direction condition)
1273              (stream-error-stream condition)))))
1274
1275 (define-condition deadline-timeout (timeout) ()
1276   (:report (lambda (condition stream)
1277              (format stream "A deadline was reached after ~A seconds."
1278                      (timeout-seconds condition)))))
1279
1280 (define-condition declaration-type-conflict-error (reference-condition
1281                                                    simple-error)
1282   ()
1283   (:default-initargs
1284       :format-control "symbol ~S cannot be both the name of a type and the name of a declaration"
1285     :references (list '(:ansi-cl :section (3 8 21)))))
1286
1287 ;;; Single stepping conditions
1288
1289 (define-condition step-condition ()
1290   ((form :initarg :form :reader step-condition-form))
1291
1292   #!+sb-doc
1293   (:documentation "Common base class of single-stepping conditions.
1294 STEP-CONDITION-FORM holds a string representation of the form being
1295 stepped."))
1296
1297 #!+sb-doc
1298 (setf (fdocumentation 'step-condition-form 'function)
1299       "Form associated with the STEP-CONDITION.")
1300
1301 (define-condition step-form-condition (step-condition)
1302   ((args :initarg :args :reader step-condition-args))
1303   (:report
1304    (lambda (condition stream)
1305      (let ((*print-circle* t)
1306            (*print-pretty* t)
1307            (*print-readably* nil))
1308        (format stream
1309                  "Evaluating call:~%~<  ~@;~A~:>~%~
1310                   ~:[With arguments:~%~{  ~S~%~}~;With unknown arguments~]~%"
1311                (list (step-condition-form condition))
1312                (eq (step-condition-args condition) :unknown)
1313                (step-condition-args condition)))))
1314   #!+sb-doc
1315   (:documentation "Condition signalled by code compiled with
1316 single-stepping information when about to execute a form.
1317 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1318 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1319 holds the source-path to the original form within that file or NIL.
1320 Associated with this condition are always the restarts STEP-INTO,
1321 STEP-NEXT, and STEP-CONTINUE."))
1322
1323 (define-condition step-result-condition (step-condition)
1324   ((result :initarg :result :reader step-condition-result)))
1325
1326 #!+sb-doc
1327 (setf (fdocumentation 'step-condition-result 'function)
1328       "Return values associated with STEP-VALUES-CONDITION as a list,
1329 or the variable value associated with STEP-VARIABLE-CONDITION.")
1330
1331 (define-condition step-values-condition (step-result-condition)
1332   ()
1333   #!+sb-doc
1334   (:documentation "Condition signalled by code compiled with
1335 single-stepping information after executing a form.
1336 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1337 the values returned by the form as a list. No associated restarts."))
1338
1339 (define-condition step-finished-condition (step-condition)
1340   ()
1341   (:report
1342    (lambda (condition stream)
1343      (declare (ignore condition))
1344      (format stream "Returning from STEP")))
1345   #!+sb-doc
1346   (:documentation "Condition signaled when STEP returns."))
1347 \f
1348 ;;; A knob for muffling warnings, mostly for use while loading files.
1349 (defvar *muffled-warnings* 'uninteresting-redefinition
1350   "A type that ought to specify a subtype of WARNING.  Whenever a
1351 warning is signaled, if the warning if of this type and is not
1352 handled by any other handler, it will be muffled.")
1353 \f
1354 ;;; Various STYLE-WARNING signaled in the system.
1355 ;; For the moment, we're only getting into the details for function
1356 ;; redefinitions, but other redefinitions could be done later
1357 ;; (e.g. methods).
1358 (define-condition redefinition-warning (style-warning)
1359   ((name
1360     :initarg :name
1361     :reader redefinition-warning-name)
1362    (new-location
1363     :initarg :new-location
1364     :reader redefinition-warning-new-location)))
1365
1366 (define-condition function-redefinition-warning (redefinition-warning)
1367   ((new-function
1368     :initarg :new-function
1369     :reader function-redefinition-warning-new-function)))
1370
1371 (define-condition redefinition-with-defun (function-redefinition-warning)
1372   ()
1373   (:report (lambda (warning stream)
1374              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1375                              in DEFUN"
1376                      (redefinition-warning-name warning)))))
1377
1378 (define-condition redefinition-with-defmacro (function-redefinition-warning)
1379   ()
1380   (:report (lambda (warning stream)
1381              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1382                              in DEFMACRO"
1383                      (redefinition-warning-name warning)))))
1384
1385 (define-condition redefinition-with-defgeneric (redefinition-warning)
1386   ()
1387   (:report (lambda (warning stream)
1388              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1389                              in DEFGENERIC"
1390                      (redefinition-warning-name warning)))))
1391
1392 (define-condition redefinition-with-defmethod (redefinition-warning)
1393   ((qualifiers :initarg :qualifiers
1394                :reader redefinition-with-defmethod-qualifiers)
1395    (specializers :initarg :specializers
1396                  :reader redefinition-with-defmethod-specializers)
1397    (new-location :initarg :new-location
1398                  :reader redefinition-with-defmethod-new-location)
1399    (old-method :initarg :old-method
1400                :reader redefinition-with-defmethod-old-method))
1401   (:report (lambda (warning stream)
1402              (format stream "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1403                      (redefinition-warning-name warning)
1404                      (redefinition-with-defmethod-qualifiers warning)
1405                      (redefinition-with-defmethod-specializers warning)))))
1406
1407 ;;;; Deciding which redefinitions are "interesting".
1408
1409 (defun function-file-namestring (function)
1410   #!+sb-eval
1411   (when (typep function 'sb!eval:interpreted-function)
1412     (return-from function-file-namestring
1413       (sb!c:definition-source-location-namestring
1414           (sb!eval:interpreted-function-source-location function))))
1415   (let* ((fun (sb!kernel:%fun-fun function))
1416          (code (sb!kernel:fun-code-header fun))
1417          (debug-info (sb!kernel:%code-debug-info code))
1418          (debug-source (when debug-info
1419                          (sb!c::debug-info-source debug-info)))
1420          (namestring (when debug-source
1421                        (sb!c::debug-source-namestring debug-source))))
1422     namestring))
1423
1424 (defun interesting-function-redefinition-warning-p (warning old)
1425   (let ((new (function-redefinition-warning-new-function warning))
1426         (source-location (redefinition-warning-new-location warning)))
1427     (or
1428      ;; Compiled->Interpreted is interesting.
1429      (and (typep old 'compiled-function)
1430           (typep new '(not compiled-function)))
1431      ;; FIN->Regular is interesting.
1432      (and (typep old 'funcallable-instance)
1433           (typep new '(not funcallable-instance)))
1434      ;; Different file or unknown location is interesting.
1435      (let* ((old-namestring (function-file-namestring old))
1436             (new-namestring
1437              (or (function-file-namestring new)
1438                  (when source-location
1439                    (sb!c::definition-source-location-namestring source-location)))))
1440        (and (or (not old-namestring)
1441                 (not new-namestring)
1442                 (not (string= old-namestring new-namestring))))))))
1443
1444 (defun uninteresting-ordinary-function-redefinition-p (warning)
1445   (and
1446    ;; There's garbage in various places when the first DEFUN runs in
1447    ;; cold-init.
1448    sb!kernel::*cold-init-complete-p*
1449    (typep warning 'redefinition-with-defun)
1450    ;; Shared logic.
1451    (let ((name (redefinition-warning-name warning)))
1452      (not (interesting-function-redefinition-warning-p
1453            warning (or (fdefinition name) (macro-function name)))))))
1454
1455 (defun uninteresting-macro-redefinition-p (warning)
1456   (and
1457    (typep warning 'redefinition-with-defmacro)
1458    ;; Shared logic.
1459    (let ((name (redefinition-warning-name warning)))
1460      (not (interesting-function-redefinition-warning-p
1461            warning (or (macro-function name) (fdefinition name)))))))
1462
1463 (defun uninteresting-generic-function-redefinition-p (warning)
1464   (and
1465    (typep warning 'redefinition-with-defgeneric)
1466    ;; Can't use the shared logic above, since GF's don't get a "new"
1467    ;; definition -- rather the FIN-FUNCTION is set.
1468    (let* ((name (redefinition-warning-name warning))
1469           (old (fdefinition name))
1470           (old-location (when (typep old 'generic-function)
1471                           (sb!pcl::definition-source old)))
1472           (old-namestring (when old-location
1473                             (sb!c:definition-source-location-namestring old-location)))
1474           (new-location (redefinition-warning-new-location warning))
1475           (new-namestring (when new-location
1476                            (sb!c:definition-source-location-namestring new-location))))
1477      (and old-namestring
1478           new-namestring
1479           (string= old-namestring new-namestring)))))
1480
1481 (defun uninteresting-method-redefinition-p (warning)
1482   (and
1483    (typep warning 'redefinition-with-defmethod)
1484    ;; Can't use the shared logic above, since GF's don't get a "new"
1485    ;; definition -- rather the FIN-FUNCTION is set.
1486    (let* ((old-method (redefinition-with-defmethod-old-method warning))
1487           (old-location (sb!pcl::definition-source old-method))
1488           (old-namestring (when old-location
1489                             (sb!c:definition-source-location-namestring old-location)))
1490           (new-location (redefinition-warning-new-location warning))
1491           (new-namestring (when new-location
1492                             (sb!c:definition-source-location-namestring new-location))))
1493          (and new-namestring
1494               old-namestring
1495               (string= new-namestring old-namestring)))))
1496
1497 (deftype uninteresting-redefinition ()
1498   '(or (satisfies uninteresting-ordinary-function-redefinition-p)
1499        (satisfies uninteresting-macro-redefinition-p)
1500        (satisfies uninteresting-generic-function-redefinition-p)
1501        (satisfies uninteresting-method-redefinition-p)))
1502
1503 (define-condition redefinition-with-deftransform (redefinition-warning)
1504   ((transform :initarg :transform
1505               :reader redefinition-with-deftransform-transform))
1506   (:report (lambda (warning stream)
1507              (format stream "Overwriting ~S"
1508                      (redefinition-with-deftransform-transform warning)))))
1509 \f
1510 ;;; Various other STYLE-WARNINGS
1511 (define-condition dubious-asterisks-around-variable-name
1512     (style-warning simple-condition)
1513   ()
1514   (:report (lambda (warning stream)
1515              (format stream "~@?, even though the name follows~@
1516 the usual naming convention (names like *FOO*) for special variables"
1517                      (simple-condition-format-control warning)
1518                      (simple-condition-format-arguments warning)))))
1519
1520 (define-condition asterisks-around-lexical-variable-name
1521     (dubious-asterisks-around-variable-name)
1522   ())
1523
1524 (define-condition asterisks-around-constant-variable-name
1525     (dubious-asterisks-around-variable-name)
1526   ())
1527
1528 ;; We call this UNDEFINED-ALIEN-STYLE-WARNING because there are some
1529 ;; subclasses of ERROR above having to do with undefined aliens.
1530 (define-condition undefined-alien-style-warning (style-warning)
1531   ((symbol :initarg :symbol :reader undefined-alien-symbol))
1532   (:report (lambda (warning stream)
1533              (format stream "Undefined alien: ~S"
1534                      (undefined-alien-symbol warning)))))
1535
1536 #!+sb-eval
1537 (define-condition lexical-environment-too-complex (style-warning)
1538   ((form :initarg :form :reader lexical-environment-too-complex-form)
1539    (lexenv :initarg :lexenv :reader lexical-environment-too-complex-lexenv))
1540   (:report (lambda (warning stream)
1541              (format stream
1542                      "~@<Native lexical environment too complex for ~
1543                          SB-EVAL to evaluate ~S, falling back to ~
1544                          SIMPLE-EVAL-IN-LEXENV.  Lexenv: ~S~:@>"
1545                      (lexical-environment-too-complex-form warning)
1546                      (lexical-environment-too-complex-lexenv warning)))))
1547
1548 ;; Although this has -ERROR- in the name, it's just a STYLE-WARNING.
1549 (define-condition character-decoding-error-in-comment (style-warning)
1550   ((stream :initarg :stream :reader decoding-error-in-comment-stream)
1551    (position :initarg :position :reader decoding-error-in-comment-position))
1552   (:report (lambda (warning stream)
1553              (format stream
1554                       "Character decoding error in a ~A-comment at ~
1555                       position ~A reading source stream ~A, ~
1556                       resyncing."
1557                       (decoding-error-in-comment-macro warning)
1558                       (decoding-error-in-comment-position warning)
1559                       (decoding-error-in-comment-stream warning)))))
1560
1561 (define-condition character-decoding-error-in-macro-char-comment
1562     (character-decoding-error-in-comment)
1563   ((char :initform #\; :initarg :char
1564          :reader character-decoding-error-in-macro-char-comment-char)))
1565
1566 (define-condition character-decoding-error-in-dispatch-macro-char-comment
1567     (character-decoding-error-in-comment)
1568   ;; ANSI doesn't give a way for a reader function invoked by a
1569   ;; dispatch macro character to determine which dispatch character
1570   ;; was used, so if a user wants to signal one of these from a custom
1571   ;; comment reader, he'll have to supply the :DISP-CHAR himself.
1572   ((disp-char :initform #\# :initarg :disp-char
1573               :reader character-decoding-error-in-macro-char-comment-disp-char)
1574    (sub-char :initarg :sub-char
1575              :reader character-decoding-error-in-macro-char-comment-sub-char)))
1576
1577 (defun decoding-error-in-comment-macro (warning)
1578   (etypecase warning
1579     (character-decoding-error-in-macro-char-comment
1580      (character-decoding-error-in-macro-char-comment-char warning))
1581     (character-decoding-error-in-dispatch-macro-char-comment
1582      (format
1583       nil "~C~C"
1584       (character-decoding-error-in-macro-char-comment-disp-char warning)
1585       (character-decoding-error-in-macro-char-comment-sub-char warning)))))
1586
1587 (define-condition deprecated-eval-when-situations (style-warning)
1588   ((situations :initarg :situations
1589                :reader deprecated-eval-when-situations-situations))
1590   (:report (lambda (warning stream)
1591              (format stream "using deprecated EVAL-WHEN situation names~{ ~S~}"
1592                      (deprecated-eval-when-situations-situations warning)))))
1593
1594 (define-condition proclamation-mismatch (style-warning)
1595   ((name :initarg :name :reader proclamation-mismatch-name)
1596    (old :initarg :old :reader proclamation-mismatch-old)
1597    (new :initarg :new :reader proclamation-mismatch-new)))
1598
1599 (define-condition type-proclamation-mismatch (proclamation-mismatch)
1600   ()
1601   (:report (lambda (warning stream)
1602              (format stream
1603                      "The new TYPE proclamation~% ~S for ~S does not ~
1604                      match the old TYPE proclamation ~S"
1605                      (proclamation-mismatch-new warning)
1606                      (proclamation-mismatch-name warning)
1607                      (proclamation-mismatch-old warning)))))
1608
1609 (define-condition ftype-proclamation-mismatch (proclamation-mismatch)
1610   ()
1611   (:report (lambda (warning stream)
1612              (format stream
1613                      "The new FTYPE proclamation~% ~S for ~S does not ~
1614                      match the old FTYPE proclamation ~S"
1615                      (proclamation-mismatch-new warning)
1616                      (proclamation-mismatch-name warning)
1617                      (proclamation-mismatch-old warning)))))
1618 \f
1619 ;;;; deprecation conditions
1620
1621 (define-condition deprecation-condition ()
1622   ((name :initarg :name :reader deprecated-name)
1623    (replacement :initarg :replacement :reader deprecated-name-replacement)
1624    (since :initarg :since :reader deprecated-since)
1625    (runtime-error :initarg :runtime-error :reader deprecated-name-runtime-error)))
1626
1627 (def!method print-object ((condition deprecation-condition) stream)
1628   (let ((*package* (find-package :keyword)))
1629     (if *print-escape*
1630         (print-unreadable-object (condition stream :type t)
1631           (format stream "~S is deprecated~@[, use ~S~]"
1632                   (deprecated-name condition)
1633                   (deprecated-name-replacement condition)))
1634         (format stream "~@<~S has been deprecated as of SBCL ~A~
1635                         ~@[, use ~S instead~].~:@>"
1636                 (deprecated-name condition)
1637                 (deprecated-since condition)
1638                 (deprecated-name-replacement condition)))))
1639
1640 (define-condition early-deprecation-warning (style-warning deprecation-condition)
1641   ())
1642
1643 (def!method print-object :after ((warning early-deprecation-warning) stream)
1644   (unless *print-escape*
1645     (let ((*package* (find-package :keyword)))
1646       (format stream "~%~@<~:@_In future SBCL versions ~S will signal a full warning ~
1647                       at compile-time.~:@>"
1648               (deprecated-name warning)))))
1649
1650 (define-condition late-deprecation-warning (warning deprecation-condition)
1651   ())
1652
1653 (def!method print-object :after ((warning late-deprecation-warning) stream)
1654   (unless *print-escape*
1655     (when (deprecated-name-runtime-error warning)
1656       (let ((*package* (find-package :keyword)))
1657         (format stream "~%~@<~:@_In future SBCL versions ~S will signal a runtime error.~:@>"
1658                 (deprecated-name warning))))))
1659
1660 (define-condition final-deprecation-warning (warning deprecation-condition)
1661   ())
1662
1663 (def!method print-object :after ((warning final-deprecation-warning) stream)
1664   (unless *print-escape*
1665     (when (deprecated-name-runtime-error warning)
1666       (let ((*package* (find-package :keyword)))
1667         (format stream "~%~@<~:@_An error will be signaled at runtime for ~S.~:@>"
1668                 (deprecated-name warning))))))
1669
1670 (define-condition deprecation-error (error deprecation-condition)
1671   ())
1672 \f
1673 ;;;; restart definitions
1674
1675 (define-condition abort-failure (control-error) ()
1676   (:report
1677    "An ABORT restart was found that failed to transfer control dynamically."))
1678
1679 (defun abort (&optional condition)
1680   #!+sb-doc
1681   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1682    none exists."
1683   (invoke-restart (find-restart-or-control-error 'abort condition))
1684   ;; ABORT signals an error in case there was a restart named ABORT
1685   ;; that did not transfer control dynamically. This could happen with
1686   ;; RESTART-BIND.
1687   (error 'abort-failure))
1688
1689 (defun muffle-warning (&optional condition)
1690   #!+sb-doc
1691   "Transfer control to a restart named MUFFLE-WARNING, signalling a
1692    CONTROL-ERROR if none exists."
1693   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1694
1695 (defun try-restart (name condition &rest arguments)
1696   (let ((restart (find-restart name condition)))
1697     (when restart
1698       (apply #'invoke-restart restart arguments))))
1699
1700 (macrolet ((define-nil-returning-restart (name args doc)
1701              #!-sb-doc (declare (ignore doc))
1702              `(defun ,name (,@args &optional condition)
1703                 #!+sb-doc ,doc
1704                 (try-restart ',name condition ,@args))))
1705   (define-nil-returning-restart continue ()
1706     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1707   (define-nil-returning-restart store-value (value)
1708     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
1709    none exists.")
1710   (define-nil-returning-restart use-value (value)
1711     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
1712    none exists."))
1713
1714 ;;; single-stepping restarts
1715
1716 (macrolet ((def (name doc)
1717                #!-sb-doc (declare (ignore doc))
1718                `(defun ,name (condition)
1719                  #!+sb-doc ,doc
1720                  (invoke-restart (find-restart-or-control-error ',name condition)))))
1721   (def step-continue
1722       "Transfers control to the STEP-CONTINUE restart associated with
1723 the condition, continuing execution without stepping. Signals a
1724 CONTROL-ERROR if the restart does not exist.")
1725   (def step-next
1726       "Transfers control to the STEP-NEXT restart associated with the
1727 condition, executing the current form without stepping and continuing
1728 stepping with the next form. Signals CONTROL-ERROR is the restart does
1729 not exists.")
1730   (def step-into
1731       "Transfers control to the STEP-INTO restart associated with the
1732 condition, stepping into the current form. Signals a CONTROL-ERROR is
1733 the restart does not exist."))
1734
1735 (/show0 "condition.lisp end of file")
1736