faa04474d8eeb6a46231f38e6ffc77e38fc4ff82
[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                                        '(:sbcl :variable *on-package-variance*))))
917
918 (define-condition package-at-variance-error (reference-condition simple-condition
919                                              package-error)
920   ()
921   (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
922
923 (define-condition defconstant-uneql (reference-condition error)
924   ((name :initarg :name :reader defconstant-uneql-name)
925    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
926    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
927   (:report
928    (lambda (condition stream)
929      (format stream
930              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
931              (defconstant-uneql-name condition)
932              (defconstant-uneql-old-value condition)
933              (defconstant-uneql-new-value condition))))
934   (:default-initargs :references (list '(:ansi-cl :macro defconstant)
935                                        '(:sbcl :node "Idiosyncrasies"))))
936
937 (define-condition array-initial-element-mismatch
938     (reference-condition simple-warning)
939   ()
940   (:default-initargs
941       :references (list
942                    '(:ansi-cl :function make-array)
943                    '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
944
945 (define-condition type-warning (reference-condition simple-warning)
946   ()
947   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
948 (define-condition type-style-warning (reference-condition simple-style-warning)
949   ()
950   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
951
952 (define-condition local-argument-mismatch (reference-condition simple-warning)
953   ()
954   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
955
956 (define-condition format-args-mismatch (reference-condition)
957   ()
958   (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
959
960 (define-condition format-too-few-args-warning
961     (format-args-mismatch simple-warning)
962   ())
963 (define-condition format-too-many-args-warning
964     (format-args-mismatch simple-style-warning)
965   ())
966
967 (define-condition implicit-generic-function-warning (style-warning)
968   ((name :initarg :name :reader implicit-generic-function-name))
969   (:report
970    (lambda (condition stream)
971      (let ((*package* (find-package :keyword)))
972        (format stream "~@<Implicitly creating new generic function ~S.~:@>"
973                (implicit-generic-function-name condition))))))
974
975 (define-condition extension-failure (reference-condition simple-error)
976   ())
977
978 (define-condition structure-initarg-not-keyword
979     (reference-condition simple-style-warning)
980   ()
981   (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
982
983 #!+sb-package-locks
984 (progn
985
986 (define-condition package-lock-violation (package-error
987                                           reference-condition
988                                           simple-condition)
989   ((current-package :initform *package*
990                     :reader package-lock-violation-in-package))
991   (:report
992    (lambda (condition stream)
993      (let ((control (simple-condition-format-control condition))
994            (error-package (package-name (package-error-package condition)))
995            (current-package (package-name (package-lock-violation-in-package condition))))
996        (if control
997            (apply #'format stream
998                   (format nil "~~@<Lock on package ~A violated when ~A while in package ~A.~~:@>"
999                           error-package
1000                           control
1001                           current-package)
1002                   (simple-condition-format-arguments condition))
1003            (format stream "~@<Lock on package ~A violated while in package ~A.~:@>"
1004                    error-package
1005                    current-package)))))
1006   ;; no :default-initargs -- reference-stuff provided by the
1007   ;; signalling form in target-package.lisp
1008   #!+sb-doc
1009   (:documentation
1010    "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
1011 when a package-lock is violated."))
1012
1013 (define-condition package-locked-error (package-lock-violation) ()
1014   #!+sb-doc
1015   (:documentation
1016    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1017 signalled when an operation on a package violates a package lock."))
1018
1019 (define-condition symbol-package-locked-error (package-lock-violation)
1020   ((symbol :initarg :symbol :reader package-locked-error-symbol))
1021   #!+sb-doc
1022   (:documentation
1023    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1024 signalled when an operation on a symbol violates a package lock. The
1025 symbol that caused the violation is accessed by the function
1026 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
1027
1028 ) ; progn
1029
1030 (define-condition undefined-alien-error (cell-error) ()
1031   (:report
1032    (lambda (condition stream)
1033      (if (slot-boundp condition 'name)
1034          (format stream "Undefined alien: ~S" (cell-error-name condition))
1035          (format stream "Undefined alien symbol.")))))
1036
1037 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
1038   (:report
1039    (lambda (condition stream)
1040      (declare (ignore condition))
1041      (format stream "Attempt to access an undefined alien variable."))))
1042
1043 (define-condition undefined-alien-function-error (undefined-alien-error) ()
1044   (:report
1045    (lambda (condition stream)
1046      (declare (ignore condition))
1047      (format stream "Attempt to call an undefined alien function."))))
1048
1049 \f
1050 ;;;; various other (not specified by ANSI) CONDITIONs
1051 ;;;;
1052 ;;;; These might logically belong in other files; they're here, after
1053 ;;;; setup of CONDITION machinery, only because that makes it easier to
1054 ;;;; get cold init to work.
1055
1056 ;;; OAOOM warning: see cross-condition.lisp
1057 (define-condition encapsulated-condition (condition)
1058   ((condition :initarg :condition :reader encapsulated-condition)))
1059
1060 ;;; KLUDGE: a condition for floating point errors when we can't or
1061 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1062 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1063 ;;; know how but the old code was broken by the conversion to POSIX
1064 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1065 ;;;
1066 ;;; FIXME: Perhaps this should also be a base class for all
1067 ;;; floating point exceptions?
1068 (define-condition floating-point-exception (arithmetic-error)
1069   ((flags :initarg :traps
1070           :initform nil
1071           :reader floating-point-exception-traps))
1072   (:report (lambda (condition stream)
1073              (format stream
1074                      "An arithmetic error ~S was signalled.~%"
1075                      (type-of condition))
1076              (let ((traps (floating-point-exception-traps condition)))
1077                (if traps
1078                    (format stream
1079                            "Trapping conditions are: ~%~{ ~S~^~}~%"
1080                            traps)
1081                    (write-line
1082                     "No traps are enabled? How can this be?"
1083                     stream))))))
1084
1085 (define-condition invalid-array-index-error (type-error)
1086   ((array :initarg :array :reader invalid-array-index-error-array)
1087    (axis :initarg :axis :reader invalid-array-index-error-axis))
1088   (:report
1089    (lambda (condition stream)
1090      (let ((array (invalid-array-index-error-array condition)))
1091        (format stream "Index ~W out of bounds for ~@[axis ~W of ~]~S, ~
1092                        should be nonnegative and <~W."
1093                (type-error-datum condition)
1094                (when (> (array-rank array) 1)
1095                  (invalid-array-index-error-axis condition))
1096                (type-of array)
1097                ;; Extract the bound from (INTEGER 0 (BOUND))
1098                (caaddr (type-error-expected-type condition)))))))
1099
1100 (define-condition invalid-array-error (reference-condition type-error) ()
1101   (:report
1102    (lambda (condition stream)
1103      (let ((*print-array* nil))
1104        (format stream
1105                "~@<Displaced array originally of type ~S has been invalidated ~
1106                 due its displaced-to array ~S having become too small to hold ~
1107                 it: the displaced array's dimensions have all been set to zero ~
1108                 to trap accesses to it.~:@>"
1109                (type-error-expected-type condition)
1110                (array-displacement (type-error-datum condition))))))
1111   (:default-initargs
1112       :references
1113       (list '(:ansi-cl :function adjust-array))))
1114
1115 (define-condition index-too-large-error (type-error)
1116   ()
1117   (:report
1118    (lambda (condition stream)
1119      (format stream
1120              "The index ~S is too large."
1121              (type-error-datum condition)))))
1122
1123 (define-condition bounding-indices-bad-error (reference-condition type-error)
1124   ((object :reader bounding-indices-bad-object :initarg :object))
1125   (:report
1126    (lambda (condition stream)
1127      (let* ((datum (type-error-datum condition))
1128             (start (car datum))
1129             (end (cdr datum))
1130             (object (bounding-indices-bad-object condition)))
1131        (etypecase object
1132          (sequence
1133           (format stream
1134                   "The bounding indices ~S and ~S are bad ~
1135                    for a sequence of length ~S."
1136                   start end (length object)))
1137          (array
1138           ;; from WITH-ARRAY-DATA
1139           (format stream
1140                   "The START and END parameters ~S and ~S are ~
1141                    bad for an array of total size ~S."
1142                   start end (array-total-size object)))))))
1143   (:default-initargs
1144       :references
1145       (list '(:ansi-cl :glossary "bounding index designator")
1146             '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1147
1148 (define-condition nil-array-accessed-error (reference-condition type-error)
1149   ()
1150   (:report (lambda (condition stream)
1151              (declare (ignore condition))
1152              (format stream
1153                      "An attempt to access an array of element-type ~
1154                       NIL was made.  Congratulations!")))
1155   (:default-initargs
1156       :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1157                         '(:ansi-cl :section (15 1 2 1))
1158                         '(:ansi-cl :section (15 1 2 2)))))
1159
1160 (define-condition namestring-parse-error (parse-error)
1161   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1162    (args :reader namestring-parse-error-args :initarg :args :initform nil)
1163    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1164    (offset :reader namestring-parse-error-offset :initarg :offset))
1165   (:report
1166    (lambda (condition stream)
1167      (format stream
1168              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
1169              (namestring-parse-error-complaint condition)
1170              (namestring-parse-error-args condition)
1171              (namestring-parse-error-namestring condition)
1172              (namestring-parse-error-offset condition)))))
1173
1174 (define-condition simple-package-error (simple-condition package-error) ())
1175
1176 (define-condition simple-reader-package-error (simple-reader-error package-error) ())
1177
1178 (define-condition reader-eof-error (end-of-file)
1179   ((context :reader reader-eof-error-context :initarg :context))
1180   (:report
1181    (lambda (condition stream)
1182      (format stream
1183              "unexpected end of file on ~S ~A"
1184              (stream-error-stream condition)
1185              (reader-eof-error-context condition)))))
1186
1187 (define-condition reader-impossible-number-error (simple-reader-error)
1188   ((error :reader reader-impossible-number-error-error :initarg :error))
1189   (:report
1190    (lambda (condition stream)
1191      (let ((error-stream (stream-error-stream condition)))
1192        (format stream
1193                "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1194                (file-position-or-nil-for-error error-stream) error-stream
1195                (simple-condition-format-control condition)
1196                (simple-condition-format-arguments condition)
1197                (reader-impossible-number-error-error condition))))))
1198
1199 (define-condition standard-readtable-modified-error (reference-condition error)
1200   ((operation :initarg :operation :reader standard-readtable-modified-operation))
1201   (:report (lambda (condition stream)
1202              (format stream "~S would modify the standard readtable."
1203                      (standard-readtable-modified-operation condition))))
1204   (:default-initargs :references `((:ansi-cl :section (2 1 1 2))
1205                                    (:ansi-cl :glossary "standard readtable"))))
1206
1207 (define-condition standard-pprint-dispatch-table-modified-error
1208     (reference-condition error)
1209   ((operation :initarg :operation
1210               :reader standard-pprint-dispatch-table-modified-operation))
1211   (:report (lambda (condition stream)
1212              (format stream "~S would modify the standard pprint dispatch table."
1213                      (standard-pprint-dispatch-table-modified-operation
1214                       condition))))
1215   (:default-initargs
1216       :references `((:ansi-cl :glossary "standard pprint dispatch table"))))
1217
1218 (define-condition timeout (serious-condition)
1219   ((seconds :initarg :seconds :initform nil :reader timeout-seconds))
1220   (:report (lambda (condition stream)
1221              (format stream "Timeout occurred~@[ after ~A seconds~]."
1222                      (timeout-seconds condition)))))
1223
1224 (define-condition io-timeout (stream-error timeout)
1225   ((direction :reader io-timeout-direction :initarg :direction))
1226   (:report
1227    (lambda (condition stream)
1228      (declare (type stream stream))
1229      (format stream
1230              "I/O timeout while doing ~(~A~) on ~S."
1231              (io-timeout-direction condition)
1232              (stream-error-stream condition)))))
1233
1234 (define-condition deadline-timeout (timeout) ()
1235   (:report (lambda (condition stream)
1236              (format stream "A deadline was reached after ~A seconds."
1237                      (timeout-seconds condition)))))
1238
1239 (define-condition declaration-type-conflict-error (reference-condition
1240                                                    simple-error)
1241   ()
1242   (:default-initargs
1243       :format-control "symbol ~S cannot be both the name of a type and the name of a declaration"
1244     :references (list '(:ansi-cl :section (3 8 21)))))
1245
1246 ;;; Single stepping conditions
1247
1248 (define-condition step-condition ()
1249   ((form :initarg :form :reader step-condition-form))
1250
1251   #!+sb-doc
1252   (:documentation "Common base class of single-stepping conditions.
1253 STEP-CONDITION-FORM holds a string representation of the form being
1254 stepped."))
1255
1256 #!+sb-doc
1257 (setf (fdocumentation 'step-condition-form 'function)
1258       "Form associated with the STEP-CONDITION.")
1259
1260 (define-condition step-form-condition (step-condition)
1261   ((args :initarg :args :reader step-condition-args))
1262   (:report
1263    (lambda (condition stream)
1264      (let ((*print-circle* t)
1265            (*print-pretty* t)
1266            (*print-readably* nil))
1267        (format stream
1268                  "Evaluating call:~%~<  ~@;~A~:>~%~
1269                   ~:[With arguments:~%~{  ~S~%~}~;With unknown arguments~]~%"
1270                (list (step-condition-form condition))
1271                (eq (step-condition-args condition) :unknown)
1272                (step-condition-args condition)))))
1273   #!+sb-doc
1274   (:documentation "Condition signalled by code compiled with
1275 single-stepping information when about to execute a form.
1276 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1277 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1278 holds the source-path to the original form within that file or NIL.
1279 Associated with this condition are always the restarts STEP-INTO,
1280 STEP-NEXT, and STEP-CONTINUE."))
1281
1282 (define-condition step-result-condition (step-condition)
1283   ((result :initarg :result :reader step-condition-result)))
1284
1285 #!+sb-doc
1286 (setf (fdocumentation 'step-condition-result 'function)
1287       "Return values associated with STEP-VALUES-CONDITION as a list,
1288 or the variable value associated with STEP-VARIABLE-CONDITION.")
1289
1290 (define-condition step-values-condition (step-result-condition)
1291   ()
1292   #!+sb-doc
1293   (:documentation "Condition signalled by code compiled with
1294 single-stepping information after executing a form.
1295 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1296 the values returned by the form as a list. No associated restarts."))
1297
1298 (define-condition step-finished-condition (step-condition)
1299   ()
1300   (:report
1301    (lambda (condition stream)
1302      (declare (ignore condition))
1303      (format stream "Returning from STEP")))
1304   #!+sb-doc
1305   (:documentation "Condition signaled when STEP returns."))
1306 \f
1307 ;;; A knob for muffling warnings, mostly for use while loading files.
1308 (defvar *muffled-warnings* 'uninteresting-redefinition
1309   "A type that ought to specify a subtype of WARNING.  Whenever a
1310 warning is signaled, if the warning if of this type and is not
1311 handled by any other handler, it will be muffled.")
1312 \f
1313 ;;; Various STYLE-WARNING signaled in the system.
1314 ;; For the moment, we're only getting into the details for function
1315 ;; redefinitions, but other redefinitions could be done later
1316 ;; (e.g. methods).
1317 (define-condition redefinition-warning (style-warning)
1318   ((name
1319     :initarg :name
1320     :reader redefinition-warning-name)
1321    (new-location
1322     :initarg :new-location
1323     :reader redefinition-warning-new-location)))
1324
1325 (define-condition function-redefinition-warning (redefinition-warning)
1326   ((new-function
1327     :initarg :new-function
1328     :reader function-redefinition-warning-new-function)))
1329
1330 (define-condition redefinition-with-defun (function-redefinition-warning)
1331   ()
1332   (:report (lambda (warning stream)
1333              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1334                              in DEFUN"
1335                      (redefinition-warning-name warning)))))
1336
1337 (define-condition redefinition-with-defmacro (function-redefinition-warning)
1338   ()
1339   (:report (lambda (warning stream)
1340              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1341                              in DEFMACRO"
1342                      (redefinition-warning-name warning)))))
1343
1344 (define-condition redefinition-with-defgeneric (redefinition-warning)
1345   ()
1346   (:report (lambda (warning stream)
1347              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1348                              in DEFGENERIC"
1349                      (redefinition-warning-name warning)))))
1350
1351 (define-condition redefinition-with-defmethod (redefinition-warning)
1352   ((qualifiers :initarg :qualifiers
1353                :reader redefinition-with-defmethod-qualifiers)
1354    (specializers :initarg :specializers
1355                  :reader redefinition-with-defmethod-specializers)
1356    (new-location :initarg :new-location
1357                  :reader redefinition-with-defmethod-new-location)
1358    (old-method :initarg :old-method
1359                :reader redefinition-with-defmethod-old-method))
1360   (:report (lambda (warning stream)
1361              (format stream "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1362                      (redefinition-warning-name warning)
1363                      (redefinition-with-defmethod-qualifiers warning)
1364                      (redefinition-with-defmethod-specializers warning)))))
1365
1366 ;;;; Deciding which redefinitions are "interesting".
1367
1368 (defun function-file-namestring (function)
1369   #!+sb-eval
1370   (when (typep function 'sb!eval:interpreted-function)
1371     (return-from function-file-namestring
1372       (sb!c:definition-source-location-namestring
1373           (sb!eval:interpreted-function-source-location function))))
1374   (let* ((fun (sb!kernel:%fun-fun function))
1375          (code (sb!kernel:fun-code-header fun))
1376          (debug-info (sb!kernel:%code-debug-info code))
1377          (debug-source (when debug-info
1378                          (sb!c::debug-info-source debug-info)))
1379          (namestring (when debug-source
1380                        (sb!c::debug-source-namestring debug-source))))
1381     namestring))
1382
1383 (defun interesting-function-redefinition-warning-p (warning old)
1384   (let ((new (function-redefinition-warning-new-function warning))
1385         (source-location (redefinition-warning-new-location warning)))
1386     (or
1387      ;; Compiled->Interpreted is interesting.
1388      (and (typep old 'compiled-function)
1389           (typep new '(not compiled-function)))
1390      ;; FIN->Regular is interesting.
1391      (and (typep old 'funcallable-instance)
1392           (typep new '(not funcallable-instance)))
1393      ;; Different file or unknown location is interesting.
1394      (let* ((old-namestring (function-file-namestring old))
1395             (new-namestring
1396              (or (function-file-namestring new)
1397                  (when source-location
1398                    (sb!c::definition-source-location-namestring source-location)))))
1399        (and (or (not old-namestring)
1400                 (not new-namestring)
1401                 (not (string= old-namestring new-namestring))))))))
1402
1403 (defun uninteresting-ordinary-function-redefinition-p (warning)
1404   (and
1405    ;; There's garbage in various places when the first DEFUN runs in
1406    ;; cold-init.
1407    sb!kernel::*cold-init-complete-p*
1408    (typep warning 'redefinition-with-defun)
1409    ;; Shared logic.
1410    (let ((name (redefinition-warning-name warning)))
1411      (not (interesting-function-redefinition-warning-p
1412            warning (or (fdefinition name) (macro-function name)))))))
1413
1414 (defun uninteresting-macro-redefinition-p (warning)
1415   (and
1416    (typep warning 'redefinition-with-defmacro)
1417    ;; Shared logic.
1418    (let ((name (redefinition-warning-name warning)))
1419      (not (interesting-function-redefinition-warning-p
1420            warning (or (macro-function name) (fdefinition name)))))))
1421
1422 (defun uninteresting-generic-function-redefinition-p (warning)
1423   (and
1424    (typep warning 'redefinition-with-defgeneric)
1425    ;; Can't use the shared logic above, since GF's don't get a "new"
1426    ;; definition -- rather the FIN-FUNCTION is set.
1427    (let* ((name (redefinition-warning-name warning))
1428           (old (fdefinition name))
1429           (old-location (when (typep old 'generic-function)
1430                           (sb!pcl::definition-source old)))
1431           (old-namestring (when old-location
1432                             (sb!c:definition-source-location-namestring old-location)))
1433           (new-location (redefinition-warning-new-location warning))
1434           (new-namestring (when new-location
1435                            (sb!c:definition-source-location-namestring new-location))))
1436      (and old-namestring
1437           new-namestring
1438           (string= old-namestring new-namestring)))))
1439
1440 (defun uninteresting-method-redefinition-p (warning)
1441   (and
1442    (typep warning 'redefinition-with-defmethod)
1443    ;; Can't use the shared logic above, since GF's don't get a "new"
1444    ;; definition -- rather the FIN-FUNCTION is set.
1445    (let* ((old-method (redefinition-with-defmethod-old-method warning))
1446           (old-location (sb!pcl::definition-source old-method))
1447           (old-namestring (when old-location
1448                             (sb!c:definition-source-location-namestring old-location)))
1449           (new-location (redefinition-warning-new-location warning))
1450           (new-namestring (when new-location
1451                             (sb!c:definition-source-location-namestring new-location))))
1452          (and new-namestring
1453               old-namestring
1454               (string= new-namestring old-namestring)))))
1455
1456 (deftype uninteresting-redefinition ()
1457   '(or (satisfies uninteresting-ordinary-function-redefinition-p)
1458        (satisfies uninteresting-macro-redefinition-p)
1459        (satisfies uninteresting-generic-function-redefinition-p)
1460        (satisfies uninteresting-method-redefinition-p)))
1461
1462 (define-condition redefinition-with-deftransform (redefinition-warning)
1463   ((transform :initarg :transform
1464               :reader redefinition-with-deftransform-transform))
1465   (:report (lambda (warning stream)
1466              (format stream "Overwriting ~S"
1467                      (redefinition-with-deftransform-transform warning)))))
1468 \f
1469 ;;; Various other STYLE-WARNINGS
1470 (define-condition dubious-asterisks-around-variable-name
1471     (style-warning simple-condition)
1472   ()
1473   (:report (lambda (warning stream)
1474              (format stream "~@?, even though the name follows~@
1475 the usual naming convention (names like *FOO*) for special variables"
1476                      (simple-condition-format-control warning)
1477                      (simple-condition-format-arguments warning)))))
1478
1479 (define-condition asterisks-around-lexical-variable-name
1480     (dubious-asterisks-around-variable-name)
1481   ())
1482
1483 (define-condition asterisks-around-constant-variable-name
1484     (dubious-asterisks-around-variable-name)
1485   ())
1486
1487 ;; We call this UNDEFINED-ALIEN-STYLE-WARNING because there are some
1488 ;; subclasses of ERROR above having to do with undefined aliens.
1489 (define-condition undefined-alien-style-warning (style-warning)
1490   ((symbol :initarg :symbol :reader undefined-alien-symbol))
1491   (:report (lambda (warning stream)
1492              (format stream "Undefined alien: ~S"
1493                      (undefined-alien-symbol warning)))))
1494
1495 #!+sb-eval
1496 (define-condition lexical-environment-too-complex (style-warning)
1497   ((form :initarg :form :reader lexical-environment-too-complex-form)
1498    (lexenv :initarg :lexenv :reader lexical-environment-too-complex-lexenv))
1499   (:report (lambda (warning stream)
1500              (format stream
1501                      "~@<Native lexical environment too complex for ~
1502                          SB-EVAL to evaluate ~S, falling back to ~
1503                          SIMPLE-EVAL-IN-LEXENV.  Lexenv: ~S~:@>"
1504                      (lexical-environment-too-complex-form warning)
1505                      (lexical-environment-too-complex-lexenv warning)))))
1506
1507 ;; Although this has -ERROR- in the name, it's just a STYLE-WARNING.
1508 (define-condition character-decoding-error-in-comment (style-warning)
1509   ((stream :initarg :stream :reader decoding-error-in-comment-stream)
1510    (position :initarg :position :reader decoding-error-in-comment-position))
1511   (:report (lambda (warning stream)
1512              (format stream
1513                       "Character decoding error in a ~A-comment at ~
1514                       position ~A reading source stream ~A, ~
1515                       resyncing."
1516                       (decoding-error-in-comment-macro warning)
1517                       (decoding-error-in-comment-position warning)
1518                       (decoding-error-in-comment-stream warning)))))
1519
1520 (define-condition character-decoding-error-in-macro-char-comment
1521     (character-decoding-error-in-comment)
1522   ((char :initform #\; :initarg :char
1523          :reader character-decoding-error-in-macro-char-comment-char)))
1524
1525 (define-condition character-decoding-error-in-dispatch-macro-char-comment
1526     (character-decoding-error-in-comment)
1527   ;; ANSI doesn't give a way for a reader function invoked by a
1528   ;; dispatch macro character to determine which dispatch character
1529   ;; was used, so if a user wants to signal one of these from a custom
1530   ;; comment reader, he'll have to supply the :DISP-CHAR himself.
1531   ((disp-char :initform #\# :initarg :disp-char
1532               :reader character-decoding-error-in-macro-char-comment-disp-char)
1533    (sub-char :initarg :sub-char
1534              :reader character-decoding-error-in-macro-char-comment-sub-char)))
1535
1536 (defun decoding-error-in-comment-macro (warning)
1537   (etypecase warning
1538     (character-decoding-error-in-macro-char-comment
1539      (character-decoding-error-in-macro-char-comment-char warning))
1540     (character-decoding-error-in-dispatch-macro-char-comment
1541      (format
1542       nil "~C~C"
1543       (character-decoding-error-in-macro-char-comment-disp-char warning)
1544       (character-decoding-error-in-macro-char-comment-sub-char warning)))))
1545
1546 (define-condition deprecated-eval-when-situations (style-warning)
1547   ((situations :initarg :situations
1548                :reader deprecated-eval-when-situations-situations))
1549   (:report (lambda (warning stream)
1550              (format stream "using deprecated EVAL-WHEN situation names~{ ~S~}"
1551                      (deprecated-eval-when-situations-situations warning)))))
1552
1553 (define-condition proclamation-mismatch (style-warning)
1554   ((name :initarg :name :reader proclamation-mismatch-name)
1555    (old :initarg :old :reader proclamation-mismatch-old)
1556    (new :initarg :new :reader proclamation-mismatch-new)))
1557
1558 (define-condition type-proclamation-mismatch (proclamation-mismatch)
1559   ()
1560   (:report (lambda (warning stream)
1561              (format stream
1562                      "The new TYPE proclamation~% ~S for ~S does not ~
1563                      match the old TYPE proclamation ~S"
1564                      (proclamation-mismatch-new warning)
1565                      (proclamation-mismatch-name warning)
1566                      (proclamation-mismatch-old warning)))))
1567
1568 (define-condition ftype-proclamation-mismatch (proclamation-mismatch)
1569   ()
1570   (:report (lambda (warning stream)
1571              (format stream
1572                      "The new FTYPE proclamation~% ~S for ~S does not ~
1573                      match the old FTYPE proclamation ~S"
1574                      (proclamation-mismatch-new warning)
1575                      (proclamation-mismatch-name warning)
1576                      (proclamation-mismatch-old warning)))))
1577 \f
1578 ;;;; deprecation conditions
1579
1580 (define-condition deprecation-condition ()
1581   ((name :initarg :name :reader deprecated-name)
1582    (replacements :initarg :replacements :reader deprecated-name-replacements)
1583    (since :initarg :since :reader deprecated-since)
1584    (runtime-error :initarg :runtime-error :reader deprecated-name-runtime-error)))
1585
1586 (def!method print-object ((condition deprecation-condition) stream)
1587   (let ((*package* (find-package :keyword)))
1588     (if *print-escape*
1589         (print-unreadable-object (condition stream :type t)
1590           (apply #'format
1591                  stream "~S is deprecated.~
1592                          ~#[~; Use ~S instead.~; ~
1593                                Use ~S or ~S instead.~:; ~
1594                                Use~@{~#[~; or~] ~S~^,~} instead.~]"
1595                   (deprecated-name condition)
1596                   (deprecated-name-replacements condition)))
1597         (apply #'format
1598                stream "~@<~S has been deprecated as of SBCL ~A.~
1599                        ~#[~; Use ~S instead.~; ~
1600                              Use ~S or ~S instead.~:; ~
1601                              Use~@{~#[~; or~] ~S~^,~:_~} instead.~]~:@>"
1602                 (deprecated-name condition)
1603                 (deprecated-since condition)
1604                 (deprecated-name-replacements condition)))))
1605
1606 (define-condition early-deprecation-warning (style-warning deprecation-condition)
1607   ())
1608
1609 (def!method print-object :after ((warning early-deprecation-warning) stream)
1610   (unless *print-escape*
1611     (let ((*package* (find-package :keyword)))
1612       (format stream "~%~@<~:@_In future SBCL versions ~S will signal a full warning ~
1613                       at compile-time.~:@>"
1614               (deprecated-name warning)))))
1615
1616 (define-condition late-deprecation-warning (warning deprecation-condition)
1617   ())
1618
1619 (def!method print-object :after ((warning late-deprecation-warning) stream)
1620   (unless *print-escape*
1621     (when (deprecated-name-runtime-error warning)
1622       (let ((*package* (find-package :keyword)))
1623         (format stream "~%~@<~:@_In future SBCL versions ~S will signal a runtime error.~:@>"
1624                 (deprecated-name warning))))))
1625
1626 (define-condition final-deprecation-warning (warning deprecation-condition)
1627   ())
1628
1629 (def!method print-object :after ((warning final-deprecation-warning) stream)
1630   (unless *print-escape*
1631     (when (deprecated-name-runtime-error warning)
1632       (let ((*package* (find-package :keyword)))
1633         (format stream "~%~@<~:@_An error will be signaled at runtime for ~S.~:@>"
1634                 (deprecated-name warning))))))
1635
1636 (define-condition deprecation-error (error deprecation-condition)
1637   ())
1638 \f
1639 ;;;; restart definitions
1640
1641 (define-condition abort-failure (control-error) ()
1642   (:report
1643    "An ABORT restart was found that failed to transfer control dynamically."))
1644
1645 (defun abort (&optional condition)
1646   #!+sb-doc
1647   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1648    none exists."
1649   (invoke-restart (find-restart-or-control-error 'abort condition))
1650   ;; ABORT signals an error in case there was a restart named ABORT
1651   ;; that did not transfer control dynamically. This could happen with
1652   ;; RESTART-BIND.
1653   (error 'abort-failure))
1654
1655 (defun muffle-warning (&optional condition)
1656   #!+sb-doc
1657   "Transfer control to a restart named MUFFLE-WARNING, signalling a
1658    CONTROL-ERROR if none exists."
1659   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1660
1661 (defun try-restart (name condition &rest arguments)
1662   (let ((restart (find-restart name condition)))
1663     (when restart
1664       (apply #'invoke-restart restart arguments))))
1665
1666 (macrolet ((define-nil-returning-restart (name args doc)
1667              #!-sb-doc (declare (ignore doc))
1668              `(defun ,name (,@args &optional condition)
1669                 #!+sb-doc ,doc
1670                 (try-restart ',name condition ,@args))))
1671   (define-nil-returning-restart continue ()
1672     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1673   (define-nil-returning-restart store-value (value)
1674     "Transfer control and VALUE to a restart named STORE-VALUE, or
1675 return NIL if none exists.")
1676   (define-nil-returning-restart use-value (value)
1677     "Transfer control and VALUE to a restart named USE-VALUE, or
1678 return NIL if none exists.")
1679   (define-nil-returning-restart print-unreadably ()
1680     "Transfer control to a restart named SB-EXT:PRINT-UNREADABLY, or
1681 return NIL if none exists."))
1682
1683 ;;; single-stepping restarts
1684
1685 (macrolet ((def (name doc)
1686                #!-sb-doc (declare (ignore doc))
1687                `(defun ,name (condition)
1688                  #!+sb-doc ,doc
1689                  (invoke-restart (find-restart-or-control-error ',name condition)))))
1690   (def step-continue
1691       "Transfers control to the STEP-CONTINUE restart associated with
1692 the condition, continuing execution without stepping. Signals a
1693 CONTROL-ERROR if the restart does not exist.")
1694   (def step-next
1695       "Transfers control to the STEP-NEXT restart associated with the
1696 condition, executing the current form without stepping and continuing
1697 stepping with the next form. Signals CONTROL-ERROR is the restart does
1698 not exists.")
1699   (def step-into
1700       "Transfers control to the STEP-INTO restart associated with the
1701 condition, stepping into the current form. Signals a CONTROL-ERROR is
1702 the restart does not exist."))
1703
1704 ;;; Compiler macro magic
1705
1706 (define-condition compiler-macro-keyword-problem ()
1707   ((argument :initarg :argument :reader compiler-macro-keyword-argument))
1708   (:report (lambda (condition stream)
1709              (format stream "~@<Argument ~S in keyword position is not ~
1710                              a self-evaluating symbol, preventing compiler-macro ~
1711                              expansion.~@:>"
1712                      (compiler-macro-keyword-argument condition)))))
1713
1714 (/show0 "condition.lisp end of file")