signal errors for bad initialization of slot definitions
[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   ;; the initform if :INITFORM was specified, otherwise NIL
72   (initform nil :type t)
73   ;; if this is a function, call it with no args to get the initform value
74   (initfunction (missing-arg) :type t)
75   ;; allocation of this slot, or NIL until defaulted
76   (allocation nil :type (member :instance :class nil))
77   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value
78   (cell nil :type (or cons null))
79   ;; slot documentation
80   (documentation nil :type (or string null)))
81
82 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
83 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
84 ;;; have themselves listed in their CPLs. This behavior is inherited
85 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
86 ;;; figured out whether it's right. -- WHN 19990612
87 (eval-when (:compile-toplevel :load-toplevel :execute)
88   (/show0 "condition.lisp 103")
89   (let ((condition-class (locally
90                            ;; KLUDGE: There's a DEFTRANSFORM
91                            ;; FIND-CLASSOID for constant class names
92                            ;; which creates fast but
93                            ;; non-cold-loadable, non-compact code. In
94                            ;; this context, we'd rather have compact,
95                            ;; cold-loadable code. -- WHN 19990928
96                            (declare (notinline find-classoid))
97                            (find-classoid 'condition))))
98     (setf (condition-classoid-cpl condition-class)
99           (list condition-class)))
100   (/show0 "condition.lisp 103"))
101
102 (setf (condition-classoid-report (locally
103                                    ;; KLUDGE: There's a DEFTRANSFORM
104                                    ;; FIND-CLASSOID for constant class
105                                    ;; names which creates fast but
106                                    ;; non-cold-loadable, non-compact
107                                    ;; code. In this context, we'd
108                                    ;; rather have compact,
109                                    ;; cold-loadable code. -- WHN
110                                    ;; 19990928
111                                    (declare (notinline find-classoid))
112                                    (find-classoid 'condition)))
113       (lambda (cond stream)
114         (format stream "Condition ~S was signalled." (type-of cond))))
115
116 (eval-when (:compile-toplevel :load-toplevel :execute)
117
118 (defun find-condition-layout (name parent-types)
119   (let* ((cpl (remove-duplicates
120                (reverse
121                 (reduce #'append
122                         (mapcar (lambda (x)
123                                   (condition-classoid-cpl
124                                    (find-classoid x)))
125                                 parent-types)))))
126          (cond-layout (info :type :compiler-layout 'condition))
127          (olayout (info :type :compiler-layout name))
128          ;; FIXME: Does this do the right thing in case of multiple
129          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
130          ;; it obvious what ANSI intends to be done in the case of
131          ;; multiple inheritance, so it's not actually clear what the
132          ;; right thing is..
133          (new-inherits
134           (order-layout-inherits (concatenate 'simple-vector
135                                               (layout-inherits cond-layout)
136                                               (mapcar #'classoid-layout cpl)))))
137     (if (and olayout
138              (not (mismatch (layout-inherits olayout) new-inherits)))
139         olayout
140         (make-layout :classoid (make-undefined-classoid name)
141                      :inherits new-inherits
142                      :depthoid -1
143                      :length (layout-length cond-layout)))))
144
145 ) ; EVAL-WHEN
146
147 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
148 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
149 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
150 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
151 ;;;   a condition type C is equivalent to:
152 ;;;     (defmethod print-object ((x c) stream)
153 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
154 ;;; The current code doesn't seem to quite match that.
155 (def!method print-object ((x condition) stream)
156   (if *print-escape*
157       (if (and (typep x 'simple-condition) (slot-value x 'format-control))
158           (print-unreadable-object (x stream :type t :identity t)
159             (write (simple-condition-format-control x)
160                    :stream stream
161                    :lines 1))
162           (print-unreadable-object (x stream :type t :identity t)))
163       ;; KLUDGE: A comment from CMU CL here said
164       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
165       ;;   superclasses in define-condition call!
166       (dolist (class (condition-classoid-cpl (classoid-of x))
167                      (error "no REPORT? shouldn't happen!"))
168         (let ((report (condition-classoid-report class)))
169           (when report
170             (return (funcall report x stream)))))))
171 \f
172 ;;;; slots of CONDITION objects
173
174 (defvar *empty-condition-slot* '(empty))
175
176 (defun find-slot-default (class slot)
177   (let ((initargs (condition-slot-initargs slot))
178         (cpl (condition-classoid-cpl class)))
179     ;; When CLASS or a superclass has a default initarg for SLOT, use
180     ;; that.
181     (dolist (class cpl)
182       (let ((direct-default-initargs
183               (condition-classoid-direct-default-initargs class)))
184         (dolist (initarg initargs)
185           (let ((initfunction (third (assoc initarg direct-default-initargs))))
186             (when initfunction
187               (return-from find-slot-default (funcall initfunction)))))))
188
189     ;; Otherwise use the initform of SLOT, if there is one.
190     (if (condition-slot-initform-p slot)
191         (let ((initfun (condition-slot-initfunction slot)))
192           (aver (functionp initfun))
193           (funcall initfun))
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                    (setf (condition-slot-initfunction sslot)
369                          (condition-slot-initfunction found)))
370                  (unless (condition-slot-allocation found)
371                    (setf (condition-slot-allocation found)
372                          (condition-slot-allocation sslot))))
373                 (t
374                  (res (copy-structure sslot)))))))
375     (res)))
376
377 ;;; Early definitions of slot accessor creators.
378 ;;;
379 ;;; Slot accessors must be generic functions, but ANSI does not seem
380 ;;; to specify any of them, and we cannot support it before end of
381 ;;; warm init. So we use ordinary functions inside SBCL, and switch to
382 ;;; GFs only at the end of building.
383 (declaim (notinline install-condition-slot-reader
384                     install-condition-slot-writer))
385 (defun install-condition-slot-reader (name condition slot-name)
386   (declare (ignore condition))
387   (setf (fdefinition name)
388         (lambda (condition)
389           (condition-reader-function condition slot-name))))
390 (defun install-condition-slot-writer (name condition slot-name)
391   (declare (ignore condition))
392   (setf (fdefinition name)
393         (lambda (new-value condition)
394           (condition-writer-function condition new-value slot-name))))
395
396 (defvar *define-condition-hooks* nil)
397
398 (defun %set-condition-report (name report)
399   (setf (condition-classoid-report (find-classoid name))
400         report))
401
402 (defun %define-condition (name parent-types layout slots documentation
403                           direct-default-initargs all-readers all-writers
404                           source-location)
405   (with-single-package-locked-error
406       (:symbol name "defining ~A as a condition")
407     (%compiler-define-condition name parent-types layout all-readers all-writers)
408     (sb!c:with-source-location (source-location)
409       (setf (layout-source-location layout)
410             source-location))
411     (let ((class (find-classoid name)))
412       (setf (condition-classoid-slots class) slots
413             (condition-classoid-direct-default-initargs class) direct-default-initargs
414             (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       (setf (condition-classoid-hairy-slots class) '())
428       (let ((eslots (compute-effective-slots class))
429             (e-def-initargs
430              (reduce #'append
431                      (mapcar #'condition-classoid-direct-default-initargs
432                              (condition-classoid-cpl class)))))
433         (dolist (slot eslots)
434           (ecase (condition-slot-allocation slot)
435             (:class
436              (unless (condition-slot-cell slot)
437                (setf (condition-slot-cell slot)
438                      (list (if (condition-slot-initform-p slot)
439                                (let ((initfun (condition-slot-initfunction slot)))
440                                  (aver (functionp initfun))
441                                  (funcall initfun))
442                                *empty-condition-slot*))))
443              (push slot (condition-classoid-class-slots class)))
444             ((:instance nil)
445              (setf (condition-slot-allocation slot) :instance)
446              ;; FIXME: isn't this "always hairy"?
447              (when (or (functionp (condition-slot-initfunction slot))
448                        (dolist (initarg (condition-slot-initargs slot) nil)
449                          (when (functionp (third (assoc initarg e-def-initargs)))
450                            (return t))))
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 `',initform)
538                      :initfunction ,(when initform-p
539                                       `#'(lambda () ,initform))
540                      :allocation ',allocation)))))
541
542       (dolist (option options)
543         (unless (consp option)
544           (error "bad option:~%  ~S" option))
545         (case (first option)
546           (:documentation (setq documentation (second option)))
547           (:report
548            (let ((arg (second option)))
549              (setq report
550                    (if (stringp arg)
551                        `#'(lambda (condition stream)
552                             (declare (ignore condition))
553                             (write-string ,arg stream))
554                        `#'(lambda (condition stream)
555                             (funcall #',arg condition stream))))))
556           (:default-initargs
557            (doplist (initarg initform) (rest option)
558              (push ``(,',initarg ,',initform ,#'(lambda () ,initform))
559                    direct-default-initargs)))
560           (t
561            (error "unknown option: ~S" (first option)))))
562
563       `(progn
564          (eval-when (:compile-toplevel)
565            (%compiler-define-condition ',name ',parent-types ',layout
566                                        ',(all-readers) ',(all-writers)))
567          (eval-when (:load-toplevel :execute)
568            (%define-condition ',name
569                               ',parent-types
570                               ',layout
571                               (list ,@(slots))
572                               ,documentation
573                               (list ,@direct-default-initargs)
574                               ',(all-readers)
575                               ',(all-writers)
576                               (sb!c:source-location))
577            ;; This needs to be after %DEFINE-CONDITION in case :REPORT
578            ;; is a lambda referring to condition slot accessors:
579            ;; they're not proclaimed as functions before it has run if
580            ;; we're under EVAL or loaded as source.
581            (%set-condition-report ',name ,report)
582            ',name)))))
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 (and *print-escape*
630            (slot-boundp condition 'expected-type)
631            (slot-boundp condition 'datum))
632       (flet ((maybe-string (thing)
633                (ignore-errors
634                  (write-to-string thing :lines 1 :readably nil :array nil :pretty t))))
635         (let ((type (maybe-string (type-error-expected-type condition)))
636               (datum (maybe-string (type-error-datum condition))))
637           (if (and type datum)
638               (print-unreadable-object (condition stream :type t)
639                 (format stream "~@<expected-type: ~A ~_datum: ~A~:@>" type datum))
640               (call-next-method))))
641       (call-next-method)))
642
643 ;;; not specified by ANSI, but too useful not to have around.
644 (define-condition simple-style-warning (simple-condition style-warning) ())
645 (define-condition simple-type-error (simple-condition type-error) ())
646
647 ;; Can't have a function called SIMPLE-TYPE-ERROR or TYPE-ERROR...
648 (declaim (ftype (sfunction (t t t &rest t) nil) bad-type))
649 (defun bad-type (datum type control &rest arguments)
650   (error 'simple-type-error
651          :datum datum
652          :expected-type type
653          :format-control control
654          :format-arguments arguments))
655
656 (define-condition program-error (error) ())
657 (define-condition parse-error   (error) ())
658 (define-condition control-error (error) ())
659 (define-condition stream-error  (error)
660   ((stream :reader stream-error-stream :initarg :stream)))
661
662 (define-condition end-of-file (stream-error) ()
663   (:report
664    (lambda (condition stream)
665      (format stream
666              "end of file on ~S"
667              (stream-error-stream condition)))))
668
669 (define-condition closed-stream-error (stream-error) ()
670   (:report
671    (lambda (condition stream)
672      (format stream "~S is closed" (stream-error-stream condition)))))
673
674 (define-condition file-error (error)
675   ((pathname :reader file-error-pathname :initarg :pathname))
676   (:report
677    (lambda (condition stream)
678      (format stream "error on file ~S" (file-error-pathname condition)))))
679
680 (define-condition package-error (error)
681   ((package :reader package-error-package :initarg :package)))
682
683 (define-condition cell-error (error)
684   ((name :reader cell-error-name :initarg :name)))
685
686 (def!method print-object ((condition cell-error) stream)
687   (if (and *print-escape* (slot-boundp condition 'name))
688       (print-unreadable-object (condition stream :type t :identity t)
689         (princ (cell-error-name condition) stream))
690       (call-next-method)))
691
692 (define-condition unbound-variable (cell-error) ()
693   (:report
694    (lambda (condition stream)
695      (format stream
696              "The variable ~S is unbound."
697              (cell-error-name condition)))))
698
699 (define-condition undefined-function (cell-error) ()
700   (:report
701    (lambda (condition stream)
702      (let ((*package* (find-package :keyword)))
703        (format stream
704                "The function ~S is undefined."
705                (cell-error-name condition))))))
706
707 (define-condition special-form-function (undefined-function) ()
708   (:report
709    (lambda (condition stream)
710      (format stream
711              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
712              (cell-error-name condition)))))
713
714 (define-condition arithmetic-error (error)
715   ((operation :reader arithmetic-error-operation
716               :initarg :operation
717               :initform nil)
718    (operands :reader arithmetic-error-operands
719              :initarg :operands))
720   (:report (lambda (condition stream)
721              (format stream
722                      "arithmetic error ~S signalled"
723                      (type-of condition))
724              (when (arithmetic-error-operation condition)
725                (format stream
726                        "~%Operation was ~S, operands ~S."
727                        (arithmetic-error-operation condition)
728                        (arithmetic-error-operands condition))))))
729
730 (define-condition division-by-zero         (arithmetic-error) ())
731 (define-condition floating-point-overflow  (arithmetic-error) ())
732 (define-condition floating-point-underflow (arithmetic-error) ())
733 (define-condition floating-point-inexact   (arithmetic-error) ())
734 (define-condition floating-point-invalid-operation (arithmetic-error) ())
735
736 (define-condition print-not-readable (error)
737   ((object :reader print-not-readable-object :initarg :object))
738   (:report
739    (lambda (condition stream)
740      (let ((obj (print-not-readable-object condition))
741            (*print-array* nil))
742        (format stream "~S cannot be printed readably." obj)))))
743
744 (define-condition reader-error (parse-error stream-error) ()
745   (:report (lambda (condition stream)
746              (%report-reader-error condition stream))))
747
748 ;;; a READER-ERROR whose REPORTing is controlled by FORMAT-CONTROL and
749 ;;; FORMAT-ARGS (the usual case for READER-ERRORs signalled from
750 ;;; within SBCL itself)
751 ;;;
752 ;;; (Inheriting CL:SIMPLE-CONDITION here isn't quite consistent with
753 ;;; the letter of the ANSI spec: this is not a condition signalled by
754 ;;; SIGNAL when a format-control is supplied by the function's first
755 ;;; argument. It seems to me (WHN) to be basically in the spirit of
756 ;;; the spec, but if not, it'd be straightforward to do our own
757 ;;; DEFINE-CONDITION SB-INT:SIMPLISTIC-CONDITION with
758 ;;; FORMAT-CONTROL and FORMAT-ARGS slots, and use that condition in
759 ;;; place of CL:SIMPLE-CONDITION here.)
760 (define-condition simple-reader-error (reader-error simple-condition)
761   ()
762   (:report (lambda (condition stream)
763              (%report-reader-error condition stream :simple t))))
764
765 ;;; base REPORTing of a READER-ERROR
766 ;;;
767 ;;; When SIMPLE, we expect and use SIMPLE-CONDITION-ish FORMAT-CONTROL
768 ;;; and FORMAT-ARGS slots.
769 (defun %report-reader-error (condition stream &key simple position)
770   (let ((error-stream (stream-error-stream condition)))
771     (pprint-logical-block (stream nil)
772       (if simple
773           (apply #'format stream
774                  (simple-condition-format-control condition)
775                  (simple-condition-format-arguments condition))
776           (prin1 (class-name (class-of condition)) stream))
777       (format stream "~2I~@[~_~_~:{~:(~A~): ~S~:^, ~:_~}~]~_~_Stream: ~S"
778               (stream-error-position-info error-stream position)
779               error-stream))))
780 \f
781 ;;;; special SBCL extension conditions
782
783 ;;; an error apparently caused by a bug in SBCL itself
784 ;;;
785 ;;; Note that we don't make any serious effort to use this condition
786 ;;; for *all* errors in SBCL itself. E.g. type errors and array
787 ;;; indexing errors can occur in functions called from SBCL code, and
788 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
789 ;;; because the signalling code has no good way to know that the
790 ;;; underlying problem is a bug in SBCL. But in the fairly common case
791 ;;; that the signalling code does know that it's found a bug in SBCL,
792 ;;; this condition is appropriate, reusing boilerplate and helping
793 ;;; users to recognize it as an SBCL bug.
794 (define-condition bug (simple-error)
795   ()
796   (:report
797    (lambda (condition stream)
798      (format stream
799              "~@<  ~? ~:@_~?~:>"
800              (simple-condition-format-control condition)
801              (simple-condition-format-arguments condition)
802              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
803               SBCL might have been corrupted by bad user code, e.g. by an ~
804               undefined Lisp operation like ~S, or by stray pointers from ~
805               alien code or from unsafe Lisp code; or there might be a bug ~
806               in the OS or hardware that SBCL is running on.) If it seems to ~
807               be a bug in SBCL itself, the maintainers would like to know ~
808               about it. Bug reports are welcome on the SBCL ~
809               mailing lists, which you can find at ~
810               <http://sbcl.sourceforge.net/>.~:@>"
811              '((fmakunbound 'compile))))))
812
813 (define-condition simple-storage-condition (storage-condition simple-condition)
814   ())
815
816 ;;; a condition for use in stubs for operations which aren't supported
817 ;;; on some platforms
818 ;;;
819 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
820 ;;;   #-(or freebsd linux)
821 ;;;   (defun load-foreign (&rest rest)
822 ;;;     (error 'unsupported-operator :name 'load-foreign))
823 ;;;   #+(or freebsd linux)
824 ;;;   (defun load-foreign ... actual definition ...)
825 ;;; By signalling a standard condition in this case, we make it
826 ;;; possible for test code to distinguish between (1) intentionally
827 ;;; unimplemented and (2) unintentionally just screwed up somehow.
828 ;;; (Before this condition was defined, test code tried to deal with
829 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
830 ;;; sbcl-0.7.0, a package screwup left the definition of
831 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
832 ;;; architectures where it was supposed to be supported, and the
833 ;;; regression tests cheerfully passed because they assumed that
834 ;;; unFBOUNDPness meant they were running on an system which didn't
835 ;;; support the extension.)
836 (define-condition unsupported-operator (simple-error) ())
837 \f
838 ;;; (:ansi-cl :function remove)
839 ;;; (:ansi-cl :section (a b c))
840 ;;; (:ansi-cl :glossary "similar")
841 ;;;
842 ;;; (:sbcl :node "...")
843 ;;; (:sbcl :variable *ed-functions*)
844 ;;;
845 ;;; FIXME: this is not the right place for this.
846 (defun print-reference (reference stream)
847   (ecase (car reference)
848     (:amop
849      (format stream "AMOP")
850      (format stream ", ")
851      (destructuring-bind (type data) (cdr reference)
852        (ecase type
853          (:readers "Readers for ~:(~A~) Metaobjects"
854                    (substitute #\  #\- (symbol-name data)))
855          (:initialization
856           (format stream "Initialization of ~:(~A~) Metaobjects"
857                   (substitute #\  #\- (symbol-name data))))
858          (:generic-function (format stream "Generic Function ~S" data))
859          (:function (format stream "Function ~S" data))
860          (:section (format stream "Section ~{~D~^.~}" data)))))
861     (:ansi-cl
862      (format stream "The ANSI Standard")
863      (format stream ", ")
864      (destructuring-bind (type data) (cdr reference)
865        (ecase type
866          (:function (format stream "Function ~S" data))
867          (:special-operator (format stream "Special Operator ~S" data))
868          (:macro (format stream "Macro ~S" data))
869          (:section (format stream "Section ~{~D~^.~}" data))
870          (:glossary (format stream "Glossary entry for ~S" data))
871          (:issue (format stream "writeup for Issue ~A" data)))))
872     (:sbcl
873      (format stream "The SBCL Manual")
874      (format stream ", ")
875      (destructuring-bind (type data) (cdr reference)
876        (ecase type
877          (:node (format stream "Node ~S" data))
878          (:variable (format stream "Variable ~S" data))
879          (:function (format stream "Function ~S" data)))))
880     ;; FIXME: other documents (e.g. CLIM, Franz documentation :-)
881     ))
882 (define-condition reference-condition ()
883   ((references :initarg :references :reader reference-condition-references)))
884 (defvar *print-condition-references* t)
885 (def!method print-object :around ((o reference-condition) s)
886   (call-next-method)
887   (unless (or *print-escape* *print-readably*)
888     (when (and *print-condition-references*
889                (reference-condition-references o))
890       (format s "~&See also:~%")
891       (pprint-logical-block (s nil :per-line-prefix "  ")
892         (do* ((rs (reference-condition-references o) (cdr rs))
893               (r (car rs) (car rs)))
894              ((null rs))
895           (print-reference r s)
896           (unless (null (cdr rs))
897             (terpri s)))))))
898
899 (define-condition simple-reference-error (reference-condition simple-error)
900   ())
901
902 (define-condition simple-reference-warning (reference-condition simple-warning)
903   ())
904
905 (define-condition arguments-out-of-domain-error
906     (arithmetic-error reference-condition)
907   ())
908
909 (define-condition duplicate-definition (reference-condition warning)
910   ((name :initarg :name :reader duplicate-definition-name))
911   (:report (lambda (c s)
912              (format s "~@<Duplicate definition for ~S found in ~
913                         one file.~@:>"
914                      (duplicate-definition-name c))))
915   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
916
917 (define-condition constant-modified (reference-condition warning)
918   ((fun-name :initarg :fun-name :reader constant-modified-fun-name))
919   (:report (lambda (c s)
920              (format s "~@<Destructive function ~S called on ~
921                         constant data.~@:>"
922                      (constant-modified-fun-name c))))
923   (:default-initargs :references (list '(:ansi-cl :special-operator quote)
924                                        '(:ansi-cl :section (3 2 2 3)))))
925
926 (define-condition package-at-variance (reference-condition simple-warning)
927   ()
928   (:default-initargs :references (list '(:ansi-cl :macro defpackage)
929                                        '(:sbcl :variable *on-package-variance*))))
930
931 (define-condition package-at-variance-error (reference-condition simple-condition
932                                              package-error)
933   ()
934   (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
935
936 (define-condition defconstant-uneql (reference-condition error)
937   ((name :initarg :name :reader defconstant-uneql-name)
938    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
939    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
940   (:report
941    (lambda (condition stream)
942      (format stream
943              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
944              (defconstant-uneql-name condition)
945              (defconstant-uneql-old-value condition)
946              (defconstant-uneql-new-value condition))))
947   (:default-initargs :references (list '(:ansi-cl :macro defconstant)
948                                        '(:sbcl :node "Idiosyncrasies"))))
949
950 (define-condition array-initial-element-mismatch
951     (reference-condition simple-warning)
952   ()
953   (:default-initargs
954       :references (list
955                    '(:ansi-cl :function make-array)
956                    '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
957
958 (define-condition type-warning (reference-condition simple-warning)
959   ()
960   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
961 (define-condition type-style-warning (reference-condition simple-style-warning)
962   ()
963   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
964
965 (define-condition local-argument-mismatch (reference-condition simple-warning)
966   ()
967   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
968
969 (define-condition format-args-mismatch (reference-condition)
970   ()
971   (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
972
973 (define-condition format-too-few-args-warning
974     (format-args-mismatch simple-warning)
975   ())
976 (define-condition format-too-many-args-warning
977     (format-args-mismatch simple-style-warning)
978   ())
979
980 (define-condition implicit-generic-function-warning (style-warning)
981   ((name :initarg :name :reader implicit-generic-function-name))
982   (:report
983    (lambda (condition stream)
984      (format stream "~@<Implicitly creating new generic function ~
985                      ~/sb-impl::print-symbol-with-prefix/.~:@>"
986              (implicit-generic-function-name condition)))))
987
988 (define-condition extension-failure (reference-condition simple-error)
989   ())
990
991 (define-condition structure-initarg-not-keyword
992     (reference-condition simple-style-warning)
993   ()
994   (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
995
996 #!+sb-package-locks
997 (progn
998
999 (define-condition package-lock-violation (package-error
1000                                           reference-condition
1001                                           simple-condition)
1002   ((current-package :initform *package*
1003                     :reader package-lock-violation-in-package))
1004   (:report
1005    (lambda (condition stream)
1006      (let ((control (simple-condition-format-control condition))
1007            (error-package (package-name (package-error-package condition)))
1008            (current-package (package-name (package-lock-violation-in-package condition))))
1009        (if control
1010            (apply #'format stream
1011                   (format nil "~~@<Lock on package ~A violated when ~A while in package ~A.~~:@>"
1012                           error-package
1013                           control
1014                           current-package)
1015                   (simple-condition-format-arguments condition))
1016            (format stream "~@<Lock on package ~A violated while in package ~A.~:@>"
1017                    error-package
1018                    current-package)))))
1019   ;; no :default-initargs -- reference-stuff provided by the
1020   ;; signalling form in target-package.lisp
1021   #!+sb-doc
1022   (:documentation
1023    "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
1024 when a package-lock is violated."))
1025
1026 (define-condition package-locked-error (package-lock-violation) ()
1027   #!+sb-doc
1028   (:documentation
1029    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1030 signalled when an operation on a package violates a package lock."))
1031
1032 (define-condition symbol-package-locked-error (package-lock-violation)
1033   ((symbol :initarg :symbol :reader package-locked-error-symbol))
1034   #!+sb-doc
1035   (:documentation
1036    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
1037 signalled when an operation on a symbol violates a package lock. The
1038 symbol that caused the violation is accessed by the function
1039 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
1040
1041 ) ; progn
1042
1043 (define-condition undefined-alien-error (cell-error) ()
1044   (:report
1045    (lambda (condition stream)
1046      (if (slot-boundp condition 'name)
1047          (format stream "Undefined alien: ~S" (cell-error-name condition))
1048          (format stream "Undefined alien symbol.")))))
1049
1050 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
1051   (:report
1052    (lambda (condition stream)
1053      (declare (ignore condition))
1054      (format stream "Attempt to access an undefined alien variable."))))
1055
1056 (define-condition undefined-alien-function-error (undefined-alien-error) ()
1057   (:report
1058    (lambda (condition stream)
1059      (if (and (slot-boundp condition 'name)
1060               (cell-error-name condition))
1061          (format stream "The alien function ~s is undefined."
1062                  (cell-error-name condition))
1063          (format stream "Attempt to call an undefined alien function.")))))
1064
1065 \f
1066 ;;;; various other (not specified by ANSI) CONDITIONs
1067 ;;;;
1068 ;;;; These might logically belong in other files; they're here, after
1069 ;;;; setup of CONDITION machinery, only because that makes it easier to
1070 ;;;; get cold init to work.
1071
1072 ;;; OAOOM warning: see cross-condition.lisp
1073 (define-condition encapsulated-condition (condition)
1074   ((condition :initarg :condition :reader encapsulated-condition)))
1075
1076 ;;; KLUDGE: a condition for floating point errors when we can't or
1077 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1078 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1079 ;;; know how but the old code was broken by the conversion to POSIX
1080 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1081 ;;;
1082 ;;; FIXME: Perhaps this should also be a base class for all
1083 ;;; floating point exceptions?
1084 (define-condition floating-point-exception (arithmetic-error)
1085   ((flags :initarg :traps
1086           :initform nil
1087           :reader floating-point-exception-traps))
1088   (:report (lambda (condition stream)
1089              (format stream
1090                      "An arithmetic error ~S was signalled.~%"
1091                      (type-of condition))
1092              (let ((traps (floating-point-exception-traps condition)))
1093                (if traps
1094                    (format stream
1095                            "Trapping conditions are: ~%~{ ~S~^~}~%"
1096                            traps)
1097                    (write-line
1098                     "No traps are enabled? How can this be?"
1099                     stream))))))
1100
1101 (define-condition invalid-array-index-error (type-error)
1102   ((array :initarg :array :reader invalid-array-index-error-array)
1103    (axis :initarg :axis :reader invalid-array-index-error-axis))
1104   (:report
1105    (lambda (condition stream)
1106      (let ((array (invalid-array-index-error-array condition)))
1107        (format stream "Index ~W out of bounds for ~@[axis ~W of ~]~S, ~
1108                        should be nonnegative and <~W."
1109                (type-error-datum condition)
1110                (when (> (array-rank array) 1)
1111                  (invalid-array-index-error-axis condition))
1112                (type-of array)
1113                ;; Extract the bound from (INTEGER 0 (BOUND))
1114                (caaddr (type-error-expected-type condition)))))))
1115
1116 (define-condition invalid-array-error (reference-condition type-error) ()
1117   (:report
1118    (lambda (condition stream)
1119      (let ((*print-array* nil))
1120        (format stream
1121                "~@<Displaced array originally of type ~S has been invalidated ~
1122                 due its displaced-to array ~S having become too small to hold ~
1123                 it: the displaced array's dimensions have all been set to zero ~
1124                 to trap accesses to it.~:@>"
1125                (type-error-expected-type condition)
1126                (array-displacement (type-error-datum condition))))))
1127   (:default-initargs
1128       :references
1129       (list '(:ansi-cl :function adjust-array))))
1130
1131 (define-condition index-too-large-error (type-error)
1132   ()
1133   (:report
1134    (lambda (condition stream)
1135      (format stream
1136              "The index ~S is too large."
1137              (type-error-datum condition)))))
1138
1139 (define-condition bounding-indices-bad-error (reference-condition type-error)
1140   ((object :reader bounding-indices-bad-object :initarg :object))
1141   (:report
1142    (lambda (condition stream)
1143      (let* ((datum (type-error-datum condition))
1144             (start (car datum))
1145             (end (cdr datum))
1146             (object (bounding-indices-bad-object condition)))
1147        (etypecase object
1148          (sequence
1149           (format stream
1150                   "The bounding indices ~S and ~S are bad ~
1151                    for a sequence of length ~S."
1152                   start end (length object)))
1153          (array
1154           ;; from WITH-ARRAY-DATA
1155           (format stream
1156                   "The START and END parameters ~S and ~S are ~
1157                    bad for an array of total size ~S."
1158                   start end (array-total-size object)))))))
1159   (:default-initargs
1160       :references
1161       (list '(:ansi-cl :glossary "bounding index designator")
1162             '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1163
1164 (define-condition nil-array-accessed-error (reference-condition type-error)
1165   ()
1166   (:report (lambda (condition stream)
1167              (declare (ignore condition))
1168              (format stream
1169                      "An attempt to access an array of element-type ~
1170                       NIL was made.  Congratulations!")))
1171   (:default-initargs
1172       :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1173                         '(:ansi-cl :section (15 1 2 1))
1174                         '(:ansi-cl :section (15 1 2 2)))))
1175
1176 (define-condition namestring-parse-error (parse-error)
1177   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1178    (args :reader namestring-parse-error-args :initarg :args :initform nil)
1179    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1180    (offset :reader namestring-parse-error-offset :initarg :offset))
1181   (:report
1182    (lambda (condition stream)
1183      (format stream
1184              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
1185              (namestring-parse-error-complaint condition)
1186              (namestring-parse-error-args condition)
1187              (namestring-parse-error-namestring condition)
1188              (namestring-parse-error-offset condition)))))
1189
1190 (define-condition simple-package-error (simple-condition package-error) ())
1191
1192 (define-condition simple-reader-package-error (simple-reader-error package-error) ())
1193
1194 (define-condition reader-eof-error (end-of-file)
1195   ((context :reader reader-eof-error-context :initarg :context))
1196   (:report
1197    (lambda (condition stream)
1198      (format stream
1199              "unexpected end of file on ~S ~A"
1200              (stream-error-stream condition)
1201              (reader-eof-error-context condition)))))
1202
1203 (define-condition reader-impossible-number-error (simple-reader-error)
1204   ((error :reader reader-impossible-number-error-error :initarg :error))
1205   (:report
1206    (lambda (condition stream)
1207      (let ((error-stream (stream-error-stream condition)))
1208        (format stream
1209                "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1210                (file-position-or-nil-for-error error-stream) error-stream
1211                (simple-condition-format-control condition)
1212                (simple-condition-format-arguments condition)
1213                (reader-impossible-number-error-error condition))))))
1214
1215 (define-condition standard-readtable-modified-error (reference-condition error)
1216   ((operation :initarg :operation :reader standard-readtable-modified-operation))
1217   (:report (lambda (condition stream)
1218              (format stream "~S would modify the standard readtable."
1219                      (standard-readtable-modified-operation condition))))
1220   (:default-initargs :references `((:ansi-cl :section (2 1 1 2))
1221                                    (:ansi-cl :glossary "standard readtable"))))
1222
1223 (define-condition standard-pprint-dispatch-table-modified-error
1224     (reference-condition error)
1225   ((operation :initarg :operation
1226               :reader standard-pprint-dispatch-table-modified-operation))
1227   (:report (lambda (condition stream)
1228              (format stream "~S would modify the standard pprint dispatch table."
1229                      (standard-pprint-dispatch-table-modified-operation
1230                       condition))))
1231   (:default-initargs
1232       :references `((:ansi-cl :glossary "standard pprint dispatch table"))))
1233
1234 (define-condition timeout (serious-condition)
1235   ((seconds :initarg :seconds :initform nil :reader timeout-seconds))
1236   (:report (lambda (condition stream)
1237              (format stream "Timeout occurred~@[ after ~A seconds~]."
1238                      (timeout-seconds condition)))))
1239
1240 (define-condition io-timeout (stream-error timeout)
1241   ((direction :reader io-timeout-direction :initarg :direction))
1242   (:report
1243    (lambda (condition stream)
1244      (declare (type stream stream))
1245      (format stream
1246              "I/O timeout while doing ~(~A~) on ~S."
1247              (io-timeout-direction condition)
1248              (stream-error-stream condition)))))
1249
1250 (define-condition deadline-timeout (timeout) ()
1251   (:report (lambda (condition stream)
1252              (format stream "A deadline was reached after ~A seconds."
1253                      (timeout-seconds condition)))))
1254
1255 (define-condition declaration-type-conflict-error (reference-condition
1256                                                    simple-error)
1257   ()
1258   (:default-initargs
1259       :format-control "symbol ~S cannot be both the name of a type and the name of a declaration"
1260     :references (list '(:ansi-cl :section (3 8 21)))))
1261
1262 ;;; Single stepping conditions
1263
1264 (define-condition step-condition ()
1265   ((form :initarg :form :reader step-condition-form))
1266
1267   #!+sb-doc
1268   (:documentation "Common base class of single-stepping conditions.
1269 STEP-CONDITION-FORM holds a string representation of the form being
1270 stepped."))
1271
1272 #!+sb-doc
1273 (setf (fdocumentation 'step-condition-form 'function)
1274       "Form associated with the STEP-CONDITION.")
1275
1276 (define-condition step-form-condition (step-condition)
1277   ((args :initarg :args :reader step-condition-args))
1278   (:report
1279    (lambda (condition stream)
1280      (let ((*print-circle* t)
1281            (*print-pretty* t)
1282            (*print-readably* nil))
1283        (format stream
1284                  "Evaluating call:~%~<  ~@;~A~:>~%~
1285                   ~:[With arguments:~%~{  ~S~%~}~;With unknown arguments~]~%"
1286                (list (step-condition-form condition))
1287                (eq (step-condition-args condition) :unknown)
1288                (step-condition-args condition)))))
1289   #!+sb-doc
1290   (:documentation "Condition signalled by code compiled with
1291 single-stepping information when about to execute a form.
1292 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1293 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1294 holds the source-path to the original form within that file or NIL.
1295 Associated with this condition are always the restarts STEP-INTO,
1296 STEP-NEXT, and STEP-CONTINUE."))
1297
1298 (define-condition step-result-condition (step-condition)
1299   ((result :initarg :result :reader step-condition-result)))
1300
1301 #!+sb-doc
1302 (setf (fdocumentation 'step-condition-result 'function)
1303       "Return values associated with STEP-VALUES-CONDITION as a list,
1304 or the variable value associated with STEP-VARIABLE-CONDITION.")
1305
1306 (define-condition step-values-condition (step-result-condition)
1307   ()
1308   #!+sb-doc
1309   (:documentation "Condition signalled by code compiled with
1310 single-stepping information after executing a form.
1311 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1312 the values returned by the form as a list. No associated restarts."))
1313
1314 (define-condition step-finished-condition (step-condition)
1315   ()
1316   (:report
1317    (lambda (condition stream)
1318      (declare (ignore condition))
1319      (format stream "Returning from STEP")))
1320   #!+sb-doc
1321   (:documentation "Condition signaled when STEP returns."))
1322 \f
1323 ;;; A knob for muffling warnings, mostly for use while loading files.
1324 (defvar *muffled-warnings* 'uninteresting-redefinition
1325   "A type that ought to specify a subtype of WARNING.  Whenever a
1326 warning is signaled, if the warning if of this type and is not
1327 handled by any other handler, it will be muffled.")
1328 \f
1329 ;;; Various STYLE-WARNING signaled in the system.
1330 ;; For the moment, we're only getting into the details for function
1331 ;; redefinitions, but other redefinitions could be done later
1332 ;; (e.g. methods).
1333 (define-condition redefinition-warning (style-warning)
1334   ((name
1335     :initarg :name
1336     :reader redefinition-warning-name)
1337    (new-location
1338     :initarg :new-location
1339     :reader redefinition-warning-new-location)))
1340
1341 (define-condition function-redefinition-warning (redefinition-warning)
1342   ((new-function
1343     :initarg :new-function
1344     :reader function-redefinition-warning-new-function)))
1345
1346 (define-condition redefinition-with-defun (function-redefinition-warning)
1347   ()
1348   (:report (lambda (warning stream)
1349              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1350                              in DEFUN"
1351                      (redefinition-warning-name warning)))))
1352
1353 (define-condition redefinition-with-defmacro (function-redefinition-warning)
1354   ()
1355   (:report (lambda (warning stream)
1356              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1357                              in DEFMACRO"
1358                      (redefinition-warning-name warning)))))
1359
1360 (define-condition redefinition-with-defgeneric (redefinition-warning)
1361   ()
1362   (:report (lambda (warning stream)
1363              (format stream "redefining ~/sb-impl::print-symbol-with-prefix/ ~
1364                              in DEFGENERIC"
1365                      (redefinition-warning-name warning)))))
1366
1367 (define-condition redefinition-with-defmethod (redefinition-warning)
1368   ((qualifiers :initarg :qualifiers
1369                :reader redefinition-with-defmethod-qualifiers)
1370    (specializers :initarg :specializers
1371                  :reader redefinition-with-defmethod-specializers)
1372    (new-location :initarg :new-location
1373                  :reader redefinition-with-defmethod-new-location)
1374    (old-method :initarg :old-method
1375                :reader redefinition-with-defmethod-old-method))
1376   (:report (lambda (warning stream)
1377              (format stream "redefining ~S~{ ~S~} ~S in DEFMETHOD"
1378                      (redefinition-warning-name warning)
1379                      (redefinition-with-defmethod-qualifiers warning)
1380                      (redefinition-with-defmethod-specializers warning)))))
1381
1382 ;;;; Deciding which redefinitions are "interesting".
1383
1384 (defun function-file-namestring (function)
1385   #!+sb-eval
1386   (when (typep function 'sb!eval:interpreted-function)
1387     (return-from function-file-namestring
1388       (sb!c:definition-source-location-namestring
1389           (sb!eval:interpreted-function-source-location function))))
1390   (let* ((fun (sb!kernel:%fun-fun function))
1391          (code (sb!kernel:fun-code-header fun))
1392          (debug-info (sb!kernel:%code-debug-info code))
1393          (debug-source (when debug-info
1394                          (sb!c::debug-info-source debug-info)))
1395          (namestring (when debug-source
1396                        (sb!c::debug-source-namestring debug-source))))
1397     namestring))
1398
1399 (defun interesting-function-redefinition-warning-p (warning old)
1400   (let ((new (function-redefinition-warning-new-function warning))
1401         (source-location (redefinition-warning-new-location warning)))
1402     (or
1403      ;; compiled->interpreted is interesting.
1404      (and (typep old 'compiled-function)
1405           (typep new '(not compiled-function)))
1406      ;; fin->regular is interesting except for interpreted->compiled.
1407      (and (typep old '(and funcallable-instance
1408                            #!+sb-eval (not sb!eval:interpreted-function)))
1409           (typep new '(not funcallable-instance)))
1410      ;; different file or unknown location is interesting.
1411      (let* ((old-namestring (function-file-namestring old))
1412             (new-namestring
1413              (or (function-file-namestring new)
1414                  (when source-location
1415                    (sb!c::definition-source-location-namestring source-location)))))
1416        (and (or (not old-namestring)
1417                 (not new-namestring)
1418                 (not (string= old-namestring new-namestring))))))))
1419
1420 (defun uninteresting-ordinary-function-redefinition-p (warning)
1421   (and
1422    ;; There's garbage in various places when the first DEFUN runs in
1423    ;; cold-init.
1424    sb!kernel::*cold-init-complete-p*
1425    (typep warning 'redefinition-with-defun)
1426    ;; Shared logic.
1427    (let ((name (redefinition-warning-name warning)))
1428      (not (interesting-function-redefinition-warning-p
1429            warning (or (fdefinition name) (macro-function name)))))))
1430
1431 (defun uninteresting-macro-redefinition-p (warning)
1432   (and
1433    (typep warning 'redefinition-with-defmacro)
1434    ;; Shared logic.
1435    (let ((name (redefinition-warning-name warning)))
1436      (not (interesting-function-redefinition-warning-p
1437            warning (or (macro-function name) (fdefinition name)))))))
1438
1439 (defun uninteresting-generic-function-redefinition-p (warning)
1440   (and
1441    (typep warning 'redefinition-with-defgeneric)
1442    ;; Can't use the shared logic above, since GF's don't get a "new"
1443    ;; definition -- rather the FIN-FUNCTION is set.
1444    (let* ((name (redefinition-warning-name warning))
1445           (old (fdefinition name))
1446           (old-location (when (typep old 'generic-function)
1447                           (sb!pcl::definition-source old)))
1448           (old-namestring (when old-location
1449                             (sb!c:definition-source-location-namestring old-location)))
1450           (new-location (redefinition-warning-new-location warning))
1451           (new-namestring (when new-location
1452                            (sb!c:definition-source-location-namestring new-location))))
1453      (and old-namestring
1454           new-namestring
1455           (string= old-namestring new-namestring)))))
1456
1457 (defun uninteresting-method-redefinition-p (warning)
1458   (and
1459    (typep warning 'redefinition-with-defmethod)
1460    ;; Can't use the shared logic above, since GF's don't get a "new"
1461    ;; definition -- rather the FIN-FUNCTION is set.
1462    (let* ((old-method (redefinition-with-defmethod-old-method warning))
1463           (old-location (sb!pcl::definition-source old-method))
1464           (old-namestring (when old-location
1465                             (sb!c:definition-source-location-namestring old-location)))
1466           (new-location (redefinition-warning-new-location warning))
1467           (new-namestring (when new-location
1468                             (sb!c:definition-source-location-namestring new-location))))
1469          (and new-namestring
1470               old-namestring
1471               (string= new-namestring old-namestring)))))
1472
1473 (deftype uninteresting-redefinition ()
1474   '(or (satisfies uninteresting-ordinary-function-redefinition-p)
1475        (satisfies uninteresting-macro-redefinition-p)
1476        (satisfies uninteresting-generic-function-redefinition-p)
1477        (satisfies uninteresting-method-redefinition-p)))
1478
1479 (define-condition redefinition-with-deftransform (redefinition-warning)
1480   ((transform :initarg :transform
1481               :reader redefinition-with-deftransform-transform))
1482   (:report (lambda (warning stream)
1483              (format stream "Overwriting ~S"
1484                      (redefinition-with-deftransform-transform warning)))))
1485 \f
1486 ;;; Various other STYLE-WARNINGS
1487 (define-condition dubious-asterisks-around-variable-name
1488     (style-warning simple-condition)
1489   ()
1490   (:report (lambda (warning stream)
1491              (format stream "~@?, even though the name follows~@
1492 the usual naming convention (names like *FOO*) for special variables"
1493                      (simple-condition-format-control warning)
1494                      (simple-condition-format-arguments warning)))))
1495
1496 (define-condition asterisks-around-lexical-variable-name
1497     (dubious-asterisks-around-variable-name)
1498   ())
1499
1500 (define-condition asterisks-around-constant-variable-name
1501     (dubious-asterisks-around-variable-name)
1502   ())
1503
1504 ;; We call this UNDEFINED-ALIEN-STYLE-WARNING because there are some
1505 ;; subclasses of ERROR above having to do with undefined aliens.
1506 (define-condition undefined-alien-style-warning (style-warning)
1507   ((symbol :initarg :symbol :reader undefined-alien-symbol))
1508   (:report (lambda (warning stream)
1509              (format stream "Undefined alien: ~S"
1510                      (undefined-alien-symbol warning)))))
1511
1512 #!+sb-eval
1513 (define-condition lexical-environment-too-complex (style-warning)
1514   ((form :initarg :form :reader lexical-environment-too-complex-form)
1515    (lexenv :initarg :lexenv :reader lexical-environment-too-complex-lexenv))
1516   (:report (lambda (warning stream)
1517              (format stream
1518                      "~@<Native lexical environment too complex for ~
1519                          SB-EVAL to evaluate ~S, falling back to ~
1520                          SIMPLE-EVAL-IN-LEXENV.  Lexenv: ~S~:@>"
1521                      (lexical-environment-too-complex-form warning)
1522                      (lexical-environment-too-complex-lexenv warning)))))
1523
1524 ;; Although this has -ERROR- in the name, it's just a STYLE-WARNING.
1525 (define-condition character-decoding-error-in-comment (style-warning)
1526   ((stream :initarg :stream :reader decoding-error-in-comment-stream)
1527    (position :initarg :position :reader decoding-error-in-comment-position))
1528   (:report (lambda (warning stream)
1529              (format stream
1530                       "Character decoding error in a ~A-comment at ~
1531                       position ~A reading source stream ~A, ~
1532                       resyncing."
1533                       (decoding-error-in-comment-macro warning)
1534                       (decoding-error-in-comment-position warning)
1535                       (decoding-error-in-comment-stream warning)))))
1536
1537 (define-condition character-decoding-error-in-macro-char-comment
1538     (character-decoding-error-in-comment)
1539   ((char :initform #\; :initarg :char
1540          :reader character-decoding-error-in-macro-char-comment-char)))
1541
1542 (define-condition character-decoding-error-in-dispatch-macro-char-comment
1543     (character-decoding-error-in-comment)
1544   ;; ANSI doesn't give a way for a reader function invoked by a
1545   ;; dispatch macro character to determine which dispatch character
1546   ;; was used, so if a user wants to signal one of these from a custom
1547   ;; comment reader, he'll have to supply the :DISP-CHAR himself.
1548   ((disp-char :initform #\# :initarg :disp-char
1549               :reader character-decoding-error-in-macro-char-comment-disp-char)
1550    (sub-char :initarg :sub-char
1551              :reader character-decoding-error-in-macro-char-comment-sub-char)))
1552
1553 (defun decoding-error-in-comment-macro (warning)
1554   (etypecase warning
1555     (character-decoding-error-in-macro-char-comment
1556      (character-decoding-error-in-macro-char-comment-char warning))
1557     (character-decoding-error-in-dispatch-macro-char-comment
1558      (format
1559       nil "~C~C"
1560       (character-decoding-error-in-macro-char-comment-disp-char warning)
1561       (character-decoding-error-in-macro-char-comment-sub-char warning)))))
1562
1563 (define-condition deprecated-eval-when-situations (style-warning)
1564   ((situations :initarg :situations
1565                :reader deprecated-eval-when-situations-situations))
1566   (:report (lambda (warning stream)
1567              (format stream "using deprecated EVAL-WHEN situation names~{ ~S~}"
1568                      (deprecated-eval-when-situations-situations warning)))))
1569
1570 (define-condition proclamation-mismatch (style-warning)
1571   ((name :initarg :name :reader proclamation-mismatch-name)
1572    (old :initarg :old :reader proclamation-mismatch-old)
1573    (new :initarg :new :reader proclamation-mismatch-new)))
1574
1575 (define-condition type-proclamation-mismatch (proclamation-mismatch)
1576   ()
1577   (:report (lambda (warning stream)
1578              (format stream
1579                      "The new TYPE proclamation~% ~S for ~S does not ~
1580                      match the old TYPE proclamation ~S"
1581                      (proclamation-mismatch-new warning)
1582                      (proclamation-mismatch-name warning)
1583                      (proclamation-mismatch-old warning)))))
1584
1585 (define-condition ftype-proclamation-mismatch (proclamation-mismatch)
1586   ()
1587   (:report (lambda (warning stream)
1588              (format stream
1589                      "The new FTYPE proclamation~% ~S for ~S does not ~
1590                      match the old FTYPE proclamation ~S"
1591                      (proclamation-mismatch-new warning)
1592                      (proclamation-mismatch-name warning)
1593                      (proclamation-mismatch-old warning)))))
1594 \f
1595 ;;;; deprecation conditions
1596
1597 (define-condition deprecation-condition ()
1598   ((name :initarg :name :reader deprecated-name)
1599    (replacements :initarg :replacements :reader deprecated-name-replacements)
1600    (since :initarg :since :reader deprecated-since)
1601    (runtime-error :initarg :runtime-error :reader deprecated-name-runtime-error)))
1602
1603 (def!method print-object ((condition deprecation-condition) stream)
1604   (let ((*package* (find-package :keyword)))
1605     (if *print-escape*
1606         (print-unreadable-object (condition stream :type t)
1607           (apply #'format
1608                  stream "~S is deprecated.~
1609                          ~#[~; Use ~S instead.~; ~
1610                                Use ~S or ~S instead.~:; ~
1611                                Use~@{~#[~; or~] ~S~^,~} instead.~]"
1612                   (deprecated-name condition)
1613                   (deprecated-name-replacements condition)))
1614         (apply #'format
1615                stream "~@<~S has been deprecated as of SBCL ~A.~
1616                        ~#[~; Use ~S instead.~; ~
1617                              Use ~S or ~S instead.~:; ~
1618                              Use~@{~#[~; or~] ~S~^,~:_~} instead.~]~:@>"
1619                 (deprecated-name condition)
1620                 (deprecated-since condition)
1621                 (deprecated-name-replacements condition)))))
1622
1623 (define-condition early-deprecation-warning (style-warning deprecation-condition)
1624   ())
1625
1626 (def!method print-object :after ((warning early-deprecation-warning) stream)
1627   (unless *print-escape*
1628     (let ((*package* (find-package :keyword)))
1629       (format stream "~%~@<~:@_In future SBCL versions ~S will signal a full warning ~
1630                       at compile-time.~:@>"
1631               (deprecated-name warning)))))
1632
1633 (define-condition late-deprecation-warning (warning deprecation-condition)
1634   ())
1635
1636 (def!method print-object :after ((warning late-deprecation-warning) stream)
1637   (unless *print-escape*
1638     (when (deprecated-name-runtime-error warning)
1639       (let ((*package* (find-package :keyword)))
1640         (format stream "~%~@<~:@_In future SBCL versions ~S will signal a runtime error.~:@>"
1641                 (deprecated-name warning))))))
1642
1643 (define-condition final-deprecation-warning (warning deprecation-condition)
1644   ())
1645
1646 (def!method print-object :after ((warning final-deprecation-warning) stream)
1647   (unless *print-escape*
1648     (when (deprecated-name-runtime-error warning)
1649       (let ((*package* (find-package :keyword)))
1650         (format stream "~%~@<~:@_An error will be signaled at runtime for ~S.~:@>"
1651                 (deprecated-name warning))))))
1652
1653 (define-condition deprecation-error (error deprecation-condition)
1654   ())
1655 \f
1656 ;;;; restart definitions
1657
1658 (define-condition abort-failure (control-error) ()
1659   (:report
1660    "An ABORT restart was found that failed to transfer control dynamically."))
1661
1662 (defun abort (&optional condition)
1663   #!+sb-doc
1664   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1665    none exists."
1666   (invoke-restart (find-restart-or-control-error 'abort condition))
1667   ;; ABORT signals an error in case there was a restart named ABORT
1668   ;; that did not transfer control dynamically. This could happen with
1669   ;; RESTART-BIND.
1670   (error 'abort-failure))
1671
1672 (defun muffle-warning (&optional condition)
1673   #!+sb-doc
1674   "Transfer control to a restart named MUFFLE-WARNING, signalling a
1675    CONTROL-ERROR if none exists."
1676   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1677
1678 (defun try-restart (name condition &rest arguments)
1679   (let ((restart (find-restart name condition)))
1680     (when restart
1681       (apply #'invoke-restart restart arguments))))
1682
1683 (macrolet ((define-nil-returning-restart (name args doc)
1684              #!-sb-doc (declare (ignore doc))
1685              `(defun ,name (,@args &optional condition)
1686                 #!+sb-doc ,doc
1687                 (try-restart ',name condition ,@args))))
1688   (define-nil-returning-restart continue ()
1689     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1690   (define-nil-returning-restart store-value (value)
1691     "Transfer control and VALUE to a restart named STORE-VALUE, or
1692 return NIL if none exists.")
1693   (define-nil-returning-restart use-value (value)
1694     "Transfer control and VALUE to a restart named USE-VALUE, or
1695 return NIL if none exists.")
1696   (define-nil-returning-restart print-unreadably ()
1697     "Transfer control to a restart named SB-EXT:PRINT-UNREADABLY, or
1698 return NIL if none exists."))
1699
1700 ;;; single-stepping restarts
1701
1702 (macrolet ((def (name doc)
1703                #!-sb-doc (declare (ignore doc))
1704                `(defun ,name (condition)
1705                  #!+sb-doc ,doc
1706                  (invoke-restart (find-restart-or-control-error ',name condition)))))
1707   (def step-continue
1708       "Transfers control to the STEP-CONTINUE restart associated with
1709 the condition, continuing execution without stepping. Signals a
1710 CONTROL-ERROR if the restart does not exist.")
1711   (def step-next
1712       "Transfers control to the STEP-NEXT restart associated with the
1713 condition, executing the current form without stepping and continuing
1714 stepping with the next form. Signals CONTROL-ERROR is the restart does
1715 not exists.")
1716   (def step-into
1717       "Transfers control to the STEP-INTO restart associated with the
1718 condition, stepping into the current form. Signals a CONTROL-ERROR is
1719 the restart does not exist."))
1720
1721 ;;; Compiler macro magic
1722
1723 (define-condition compiler-macro-keyword-problem ()
1724   ((argument :initarg :argument :reader compiler-macro-keyword-argument))
1725   (:report (lambda (condition stream)
1726              (format stream "~@<Argument ~S in keyword position is not ~
1727                              a self-evaluating symbol, preventing compiler-macro ~
1728                              expansion.~@:>"
1729                      (compiler-macro-keyword-argument condition)))))
1730
1731 (/show0 "condition.lisp end of file")