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