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