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