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