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