1.0.5.9: experimental semi-synchronous deadlines
[sbcl.git] / src / code / condition.lisp
1 ;;;; stuff originally from CMU CL's error.lisp which can or should
2 ;;;; come late (mostly related to the CONDITION class itself)
3 ;;;;
4
5 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; more information.
7 ;;;;
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
13
14 (in-package "SB!KERNEL")
15 \f
16 ;;;; miscellaneous support utilities
17
18 ;;; Signalling an error when trying to print an error condition is
19 ;;; generally a PITA, so whatever the failure encountered when
20 ;;; wondering about FILE-POSITION within a condition printer, 'tis
21 ;;; better silently to give up than to try to complain.
22 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
23   ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
24   ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
25   ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
26   ;; has been closed so that FILE-POSITION is a nonsense question. So
27   ;; my (WHN) impression is that the conservative approach is to
28   ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
29   ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
30   ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
31   ;; time an error was reported.)
32   (if posp
33       (ignore-errors (file-position stream pos))
34       (ignore-errors (file-position stream))))
35 \f
36 ;;;; the CONDITION class
37
38 (/show0 "condition.lisp 20")
39
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41
42 (/show0 "condition.lisp 24")
43
44 (def!struct (condition-classoid (:include classoid)
45                                 (:constructor make-condition-classoid))
46   ;; list of CONDITION-SLOT structures for the direct slots of this
47   ;; class
48   (slots nil :type list)
49   ;; list of CONDITION-SLOT structures for all of the effective class
50   ;; slots of this class
51   (class-slots nil :type list)
52   ;; report function or NIL
53   (report nil :type (or function null))
54   ;; list of alternating initargs and initforms
55   (default-initargs () :type list)
56   ;; class precedence list as a list of CLASS objects, with all
57   ;; non-CONDITION classes removed
58   (cpl () :type list)
59   ;; a list of all the effective instance allocation slots of this
60   ;; class that have a non-constant initform or default-initarg.
61   ;; Values for these slots must be computed in the dynamic
62   ;; environment of MAKE-CONDITION.
63   (hairy-slots nil :type list))
64
65 (/show0 "condition.lisp 49")
66
67 ) ; EVAL-WHEN
68
69 (!defstruct-with-alternate-metaclass condition
70   :slot-names (actual-initargs assigned-slots)
71   :boa-constructor %make-condition-object
72   :superclass-name t
73   :metaclass-name condition-classoid
74   :metaclass-constructor make-condition-classoid
75   :dd-type structure)
76
77 (defun make-condition-object (actual-initargs)
78   (%make-condition-object actual-initargs nil))
79
80 (defstruct (condition-slot (:copier nil))
81   (name (missing-arg) :type symbol)
82   ;; list of all applicable initargs
83   (initargs (missing-arg) :type list)
84   ;; names of reader and writer functions
85   (readers (missing-arg) :type list)
86   (writers (missing-arg) :type list)
87   ;; true if :INITFORM was specified
88   (initform-p (missing-arg) :type (member t nil))
89   ;; If this is a function, call it with no args. Otherwise, it's the
90   ;; actual value.
91   (initform (missing-arg) :type t)
92   ;; allocation of this slot, or NIL until defaulted
93   (allocation nil :type (member :instance :class nil))
94   ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
95   (cell nil :type (or cons null))
96   ;; slot documentation
97   (documentation nil :type (or string null)))
98
99 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
100 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
101 ;;; have themselves listed in their CPLs. This behavior is inherited
102 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
103 ;;; figured out whether it's right. -- WHN 19990612
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105   (/show0 "condition.lisp 103")
106   (let ((condition-class (locally
107                            ;; KLUDGE: There's a DEFTRANSFORM
108                            ;; FIND-CLASSOID for constant class names
109                            ;; which creates fast but
110                            ;; non-cold-loadable, non-compact code. In
111                            ;; this context, we'd rather have compact,
112                            ;; cold-loadable code. -- WHN 19990928
113                            (declare (notinline find-classoid))
114                            (find-classoid 'condition))))
115     (setf (condition-classoid-cpl condition-class)
116           (list condition-class)))
117   (/show0 "condition.lisp 103"))
118
119 (setf (condition-classoid-report (locally
120                                    ;; KLUDGE: There's a DEFTRANSFORM
121                                    ;; FIND-CLASSOID for constant class
122                                    ;; names which creates fast but
123                                    ;; non-cold-loadable, non-compact
124                                    ;; code. In this context, we'd
125                                    ;; rather have compact,
126                                    ;; cold-loadable code. -- WHN
127                                    ;; 19990928
128                                    (declare (notinline find-classoid))
129                                    (find-classoid 'condition)))
130       (lambda (cond stream)
131         (format stream "Condition ~S was signalled." (type-of cond))))
132
133 (eval-when (:compile-toplevel :load-toplevel :execute)
134
135 (defun find-condition-layout (name parent-types)
136   (let* ((cpl (remove-duplicates
137                (reverse
138                 (reduce #'append
139                         (mapcar (lambda (x)
140                                   (condition-classoid-cpl
141                                    (find-classoid x)))
142                                 parent-types)))))
143          (cond-layout (info :type :compiler-layout 'condition))
144          (olayout (info :type :compiler-layout name))
145          ;; FIXME: Does this do the right thing in case of multiple
146          ;; inheritance? A quick look at DEFINE-CONDITION didn't make
147          ;; it obvious what ANSI intends to be done in the case of
148          ;; multiple inheritance, so it's not actually clear what the
149          ;; right thing is..
150          (new-inherits
151           (order-layout-inherits (concatenate 'simple-vector
152                                               (layout-inherits cond-layout)
153                                               (mapcar #'classoid-layout cpl)))))
154     (if (and olayout
155              (not (mismatch (layout-inherits olayout) new-inherits)))
156         olayout
157         (make-layout :classoid (make-undefined-classoid name)
158                      :inherits new-inherits
159                      :depthoid -1
160                      :length (layout-length cond-layout)))))
161
162 ) ; EVAL-WHEN
163
164 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
165 ;;;   Condition reporting is mediated through the PRINT-OBJECT method
166 ;;;   for the condition type in question, with *PRINT-ESCAPE* always
167 ;;;   being nil. Specifying (:REPORT REPORT-NAME) in the definition of
168 ;;;   a condition type C is equivalent to:
169 ;;;     (defmethod print-object ((x c) stream)
170 ;;;       (if *print-escape* (call-next-method) (report-name x stream)))
171 ;;; The current code doesn't seem to quite match that.
172 (def!method print-object ((x condition) stream)
173   (if *print-escape*
174       (print-unreadable-object (x stream :type t :identity t))
175       ;; KLUDGE: A comment from CMU CL here said
176       ;;   7/13/98 BUG? CPL is not sorted and results here depend on order of
177       ;;   superclasses in define-condition call!
178       (dolist (class (condition-classoid-cpl (classoid-of x))
179                      (error "no REPORT? shouldn't happen!"))
180         (let ((report (condition-classoid-report class)))
181           (when report
182             (return (funcall report x stream)))))))
183 \f
184 ;;;; slots of CONDITION objects
185
186 (defvar *empty-condition-slot* '(empty))
187
188 (defun find-slot-default (class slot)
189   (let ((initargs (condition-slot-initargs slot))
190         (cpl (condition-classoid-cpl class)))
191     (dolist (class cpl)
192       (let ((default-initargs (condition-classoid-default-initargs class)))
193         (dolist (initarg initargs)
194           (let ((val (getf default-initargs initarg *empty-condition-slot*)))
195             (unless (eq val *empty-condition-slot*)
196               (return-from find-slot-default
197                            (if (functionp val)
198                                (funcall val)
199                                val)))))))
200
201     (if (condition-slot-initform-p slot)
202         (let ((initform (condition-slot-initform slot)))
203           (if (functionp initform)
204               (funcall initform)
205               initform))
206         (error "unbound condition slot: ~S" (condition-slot-name slot)))))
207
208 (defun find-condition-class-slot (condition-class slot-name)
209   (dolist (sclass
210            (condition-classoid-cpl condition-class)
211            (error "There is no slot named ~S in ~S."
212                   slot-name condition-class))
213     (dolist (slot (condition-classoid-slots sclass))
214       (when (eq (condition-slot-name slot) slot-name)
215         (return-from find-condition-class-slot slot)))))
216
217 (defun condition-writer-function (condition new-value name)
218   (dolist (cslot (condition-classoid-class-slots
219                   (layout-classoid (%instance-layout condition)))
220                  (setf (getf (condition-assigned-slots condition) name)
221                        new-value))
222     (when (eq (condition-slot-name cslot) name)
223       (return (setf (car (condition-slot-cell cslot)) new-value)))))
224
225 (defun condition-reader-function (condition name)
226   (let ((class (layout-classoid (%instance-layout condition))))
227     (dolist (cslot (condition-classoid-class-slots class))
228       (when (eq (condition-slot-name cslot) name)
229         (return-from condition-reader-function
230                      (car (condition-slot-cell cslot)))))
231     (let ((val (getf (condition-assigned-slots condition) name
232                      *empty-condition-slot*)))
233       (if (eq val *empty-condition-slot*)
234           (let ((actual-initargs (condition-actual-initargs condition))
235                 (slot (find-condition-class-slot class name)))
236             (unless slot
237               (error "missing slot ~S of ~S" name condition))
238             (do ((initargs actual-initargs (cddr initargs)))
239                 ((endp initargs)
240                  (setf (getf (condition-assigned-slots condition) name)
241                        (find-slot-default class slot)))
242               (when (member (car initargs) (condition-slot-initargs slot))
243                 (return-from condition-reader-function
244                   (setf (getf (condition-assigned-slots condition)
245                               name)
246                         (cadr initargs))))))
247           val))))
248 \f
249 ;;;; MAKE-CONDITION
250
251 (defun make-condition (type &rest args)
252   #!+sb-doc
253   "Make an instance of a condition object using the specified initargs."
254   ;; Note: ANSI specifies no exceptional situations in this function.
255   ;; signalling simple-type-error would not be wrong.
256   (let* ((type (or (and (symbolp type) (find-classoid type nil))
257                     type))
258          (class (typecase type
259                   (condition-classoid type)
260                   (class
261                    ;; Punt to CLOS.
262                    (return-from make-condition (apply #'make-instance type args)))
263                   (classoid
264                    (error 'simple-type-error
265                           :datum type
266                           :expected-type 'condition-class
267                           :format-control "~S is not a condition class."
268                           :format-arguments (list type)))
269                   (t
270                    (error 'simple-type-error
271                           :datum type
272                           :expected-type 'condition-class
273                           :format-control "Bad type argument:~%  ~S"
274                           :format-arguments (list type)))))
275          (res (make-condition-object args)))
276     (setf (%instance-layout res) (classoid-layout class))
277     ;; Set any class slots with initargs present in this call.
278     (dolist (cslot (condition-classoid-class-slots class))
279       (dolist (initarg (condition-slot-initargs cslot))
280         (let ((val (getf args initarg *empty-condition-slot*)))
281           (unless (eq val *empty-condition-slot*)
282             (setf (car (condition-slot-cell cslot)) val)))))
283     ;; Default any slots with non-constant defaults now.
284     (dolist (hslot (condition-classoid-hairy-slots class))
285       (when (dolist (initarg (condition-slot-initargs hslot) t)
286               (unless (eq (getf args initarg *empty-condition-slot*)
287                           *empty-condition-slot*)
288                 (return nil)))
289         (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
290               (find-slot-default class hslot))))
291     res))
292 \f
293 ;;;; DEFINE-CONDITION
294
295 (eval-when (:compile-toplevel :load-toplevel :execute)
296 (defun %compiler-define-condition (name direct-supers layout
297                                    all-readers all-writers)
298   (with-single-package-locked-error
299       (:symbol name "defining ~A as a condition")
300     (sb!xc:proclaim `(ftype (function (t) t) ,@all-readers))
301     (sb!xc:proclaim `(ftype (function (t t) t) ,@all-writers))
302     (multiple-value-bind (class old-layout)
303         (insured-find-classoid name
304                                #'condition-classoid-p
305                                #'make-condition-classoid)
306       (setf (layout-classoid layout) class)
307       (setf (classoid-direct-superclasses class)
308             (mapcar #'find-classoid direct-supers))
309       (cond ((not old-layout)
310              (register-layout layout))
311             ((not *type-system-initialized*)
312              (setf (layout-classoid old-layout) class)
313              (setq layout old-layout)
314              (unless (eq (classoid-layout class) layout)
315                (register-layout layout)))
316             ((redefine-layout-warning "current"
317                                       old-layout
318                                       "new"
319                                       (layout-length layout)
320                                       (layout-inherits layout)
321                                       (layout-depthoid layout)
322                                       (layout-n-untagged-slots layout))
323              (register-layout layout :invalidate t))
324             ((not (classoid-layout class))
325              (register-layout layout)))
326
327       (setf (layout-info layout)
328             (locally
329                 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
330                 ;; names which creates fast but non-cold-loadable, non-compact
331                 ;; code. In this context, we'd rather have compact, cold-loadable
332                 ;; code. -- WHN 19990928
333                 (declare (notinline find-classoid))
334               (layout-info (classoid-layout (find-classoid 'condition)))))
335
336       (setf (find-classoid name) class)
337
338       ;; Initialize CPL slot.
339       (setf (condition-classoid-cpl class)
340             (remove-if-not #'condition-classoid-p
341                            (std-compute-class-precedence-list class)))))
342   (values))
343 ) ; EVAL-WHEN
344
345 ;;; Compute the effective slots of CLASS, copying inherited slots and
346 ;;; destructively modifying direct slots.
347 ;;;
348 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
349 ;;; direct slots. Presumably it follows from the semantics of
350 ;;; inheritance and redefinition of conditions, but finding the cite
351 ;;; and documenting it here would be good. (Or, if this is not in fact
352 ;;; ANSI-compliant, fixing it would also be good.:-)
353 (defun compute-effective-slots (class)
354   (collect ((res (copy-list (condition-classoid-slots class))))
355     (dolist (sclass (cdr (condition-classoid-cpl class)))
356       (dolist (sslot (condition-classoid-slots sclass))
357         (let ((found (find (condition-slot-name sslot) (res)
358                            :key #'condition-slot-name)))
359           (cond (found
360                  (setf (condition-slot-initargs found)
361                        (union (condition-slot-initargs found)
362                               (condition-slot-initargs sslot)))
363                  (unless (condition-slot-initform-p found)
364                    (setf (condition-slot-initform-p found)
365                          (condition-slot-initform-p sslot))
366                    (setf (condition-slot-initform found)
367                          (condition-slot-initform sslot)))
368                  (unless (condition-slot-allocation found)
369                    (setf (condition-slot-allocation found)
370                          (condition-slot-allocation sslot))))
371                 (t
372                  (res (copy-structure sslot)))))))
373     (res)))
374
375 ;;; Early definitions of slot accessor creators.
376 ;;;
377 ;;; Slot accessors must be generic functions, but ANSI does not seem
378 ;;; to specify any of them, and we cannot support it before end of
379 ;;; warm init. So we use ordinary functions inside SBCL, and switch to
380 ;;; GFs only at the end of building.
381 (declaim (notinline install-condition-slot-reader
382                     install-condition-slot-writer))
383 (defun install-condition-slot-reader (name condition slot-name)
384   (declare (ignore condition))
385   (setf (fdefinition name)
386         (lambda (condition)
387           (condition-reader-function condition slot-name))))
388 (defun install-condition-slot-writer (name condition slot-name)
389   (declare (ignore condition))
390   (setf (fdefinition name)
391         (lambda (new-value condition)
392           (condition-writer-function condition new-value slot-name))))
393
394 (defvar *define-condition-hooks* nil)
395
396 (defun %define-condition (name parent-types layout slots documentation
397                           report default-initargs all-readers all-writers
398                           source-location)
399   (with-single-package-locked-error
400       (:symbol name "defining ~A as a condition")
401     (%compiler-define-condition name parent-types layout all-readers all-writers)
402     (sb!c:with-source-location (source-location)
403       (setf (layout-source-location layout)
404             source-location))
405     (let ((class (find-classoid name)))
406       (setf (condition-classoid-slots class) slots)
407       (setf (condition-classoid-report class) report)
408       (setf (condition-classoid-default-initargs class) default-initargs)
409       (setf (fdocumentation name 'type) documentation)
410
411       (dolist (slot slots)
412
413         ;; Set up reader and writer functions.
414         (let ((slot-name (condition-slot-name slot)))
415           (dolist (reader (condition-slot-readers slot))
416             (install-condition-slot-reader reader name slot-name))
417           (dolist (writer (condition-slot-writers slot))
418             (install-condition-slot-writer writer name slot-name))))
419
420       ;; Compute effective slots and set up the class and hairy slots
421       ;; (subsets of the effective slots.)
422       (let ((eslots (compute-effective-slots class))
423             (e-def-initargs
424              (reduce #'append
425                      (mapcar #'condition-classoid-default-initargs
426                            (condition-classoid-cpl class)))))
427         (dolist (slot eslots)
428           (ecase (condition-slot-allocation slot)
429             (:class
430              (unless (condition-slot-cell slot)
431                (setf (condition-slot-cell slot)
432                      (list (if (condition-slot-initform-p slot)
433                                (let ((initform (condition-slot-initform slot)))
434                                  (if (functionp initform)
435                                      (funcall initform)
436                                      initform))
437                                *empty-condition-slot*))))
438              (push slot (condition-classoid-class-slots class)))
439             ((:instance nil)
440              (setf (condition-slot-allocation slot) :instance)
441              (when (or (functionp (condition-slot-initform slot))
442                        (dolist (initarg (condition-slot-initargs slot) nil)
443                          (when (functionp (getf e-def-initargs initarg))
444                            (return t))))
445                (push slot (condition-classoid-hairy-slots class)))))))
446       (when (boundp '*define-condition-hooks*)
447         (dolist (fun *define-condition-hooks*)
448           (funcall fun class))))
449     name))
450
451 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
452                                  &body options)
453   #!+sb-doc
454   "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
455    Define NAME as a condition type. This new type inherits slots and its
456    report function from the specified PARENT-TYPEs. A slot spec is a list of:
457      (slot-name :reader <rname> :initarg <iname> {Option Value}*
458
459    The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
460    and :TYPE and the overall options :DEFAULT-INITARGS and
461    [type] :DOCUMENTATION are also allowed.
462
463    The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
464    a string or a two-argument lambda or function name. If a function, the
465    function is called with the condition and stream to report the condition.
466    If a string, the string is printed.
467
468    Condition types are classes, but (as allowed by ANSI and not as described in
469    CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
470    SLOT-VALUE may not be used on condition objects."
471   (let* ((parent-types (or parent-types '(condition)))
472          (layout (find-condition-layout name parent-types))
473          (documentation nil)
474          (report nil)
475          (default-initargs ()))
476     (collect ((slots)
477               (all-readers nil append)
478               (all-writers nil append))
479       (dolist (spec slot-specs)
480         (when (keywordp spec)
481           (warn "Keyword slot name indicates probable syntax error:~%  ~S"
482                 spec))
483         (let* ((spec (if (consp spec) spec (list spec)))
484                (slot-name (first spec))
485                (allocation :instance)
486                (initform-p nil)
487                documentation
488                initform)
489           (collect ((initargs)
490                     (readers)
491                     (writers))
492             (do ((options (rest spec) (cddr options)))
493                 ((null options))
494               (unless (and (consp options) (consp (cdr options)))
495                 (error "malformed condition slot spec:~%  ~S." spec))
496               (let ((arg (second options)))
497                 (case (first options)
498                   (:reader (readers arg))
499                   (:writer (writers arg))
500                   (:accessor
501                    (readers arg)
502                    (writers `(setf ,arg)))
503                   (:initform
504                    (when initform-p
505                      (error "more than one :INITFORM in ~S" spec))
506                    (setq initform-p t)
507                    (setq initform arg))
508                   (:initarg (initargs arg))
509                   (:allocation
510                    (setq allocation arg))
511                   (:documentation
512                    (when documentation
513                      (error "more than one :DOCUMENTATION in ~S" spec))
514                    (unless (stringp arg)
515                      (error "slot :DOCUMENTATION argument is not a string: ~S"
516                             arg))
517                    (setq documentation arg))
518                   (:type)
519                   (t
520                    (error "unknown slot option:~%  ~S" (first options))))))
521
522             (all-readers (readers))
523             (all-writers (writers))
524             (slots `(make-condition-slot
525                      :name ',slot-name
526                      :initargs ',(initargs)
527                      :readers ',(readers)
528                      :writers ',(writers)
529                      :initform-p ',initform-p
530                      :documentation ',documentation
531                      :initform
532                      ,(if (sb!xc:constantp initform)
533                           `',(constant-form-value initform)
534                           `#'(lambda () ,initform)))))))
535
536       (dolist (option options)
537         (unless (consp option)
538           (error "bad option:~%  ~S" option))
539         (case (first option)
540           (:documentation (setq documentation (second option)))
541           (:report
542            (let ((arg (second option)))
543              (setq report
544                    (if (stringp arg)
545                        `#'(lambda (condition stream)
546                           (declare (ignore condition))
547                           (write-string ,arg stream))
548                        `#'(lambda (condition stream)
549                           (funcall #',arg condition stream))))))
550           (:default-initargs
551            (do ((initargs (rest option) (cddr initargs)))
552                ((endp initargs))
553              (let ((val (second initargs)))
554                (setq default-initargs
555                      (list* `',(first initargs)
556                             (if (sb!xc:constantp val)
557                                 `',(constant-form-value val)
558                                 `#'(lambda () ,val))
559                             default-initargs)))))
560           (t
561            (error "unknown option: ~S" (first option)))))
562
563       `(progn
564          (eval-when (:compile-toplevel)
565            (%compiler-define-condition ',name ',parent-types ',layout
566                                        ',(all-readers) ',(all-writers)))
567          (eval-when (:load-toplevel :execute)
568            (%define-condition ',name
569                               ',parent-types
570                               ',layout
571                               (list ,@(slots))
572                               ,documentation
573                               ,report
574                               (list ,@default-initargs)
575                               ',(all-readers)
576                               ',(all-writers)
577                               (sb!c:source-location)))))))
578 \f
579 ;;;; DESCRIBE on CONDITIONs
580
581 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
582 ;;; eventually (once we get CLOS up and running so that we can define
583 ;;; methods)
584 (defun describe-condition (condition stream)
585   (format stream
586           "~&~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>~%"
587           condition
588           (type-of condition)
589           (concatenate 'list
590                        (condition-actual-initargs condition)
591                        (condition-assigned-slots condition))))
592 \f
593 ;;;; various CONDITIONs specified by ANSI
594
595 (define-condition serious-condition (condition) ())
596
597 (define-condition error (serious-condition) ())
598
599 (define-condition warning (condition) ())
600 (define-condition style-warning (warning) ())
601
602 (defun simple-condition-printer (condition stream)
603   (apply #'format
604          stream
605          (simple-condition-format-control condition)
606          (simple-condition-format-arguments condition)))
607
608 (define-condition simple-condition ()
609   ((format-control :reader simple-condition-format-control
610                    :initarg :format-control
611                    :type format-control)
612    (format-arguments :reader simple-condition-format-arguments
613                      :initarg :format-arguments
614                      :initform '()
615                      :type list))
616   (:report simple-condition-printer))
617
618 (define-condition simple-warning (simple-condition warning) ())
619
620 (define-condition simple-error (simple-condition error) ())
621
622 ;;; not specified by ANSI, but too useful not to have around.
623 (define-condition simple-style-warning (simple-condition style-warning) ())
624
625 (define-condition storage-condition (serious-condition) ())
626
627 (define-condition type-error (error)
628   ((datum :reader type-error-datum :initarg :datum)
629    (expected-type :reader type-error-expected-type :initarg :expected-type))
630   (:report
631    (lambda (condition stream)
632      (format stream
633              "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
634              (type-error-datum condition)
635              (type-error-expected-type condition)))))
636
637 (define-condition simple-type-error (simple-condition type-error) ())
638
639 (define-condition program-error (error) ())
640 (define-condition parse-error   (error) ())
641 (define-condition control-error (error) ())
642 (define-condition stream-error  (error)
643   ((stream :reader stream-error-stream :initarg :stream)))
644
645 (define-condition end-of-file (stream-error) ()
646   (:report
647    (lambda (condition stream)
648      (format stream
649              "end of file on ~S"
650              (stream-error-stream condition)))))
651
652 (define-condition file-error (error)
653   ((pathname :reader file-error-pathname :initarg :pathname))
654   (:report
655    (lambda (condition stream)
656      (format stream "error on file ~S" (file-error-pathname condition)))))
657
658 (define-condition package-error (error)
659   ((package :reader package-error-package :initarg :package)))
660
661 (define-condition cell-error (error)
662   ((name :reader cell-error-name :initarg :name)))
663
664 (def!method print-object ((condition cell-error) stream)
665   (if (and *print-escape* (slot-boundp condition 'name))
666       (print-unreadable-object (condition stream :type t :identity t)
667         (princ (cell-error-name condition) stream))
668       (call-next-method)))
669
670 (define-condition unbound-variable (cell-error) ()
671   (:report
672    (lambda (condition stream)
673      (format stream
674              "The variable ~S is unbound."
675              (cell-error-name condition)))))
676
677 (define-condition undefined-function (cell-error) ()
678   (:report
679    (lambda (condition stream)
680      (format stream
681              "The function ~S is undefined."
682              (cell-error-name condition)))))
683
684 (define-condition special-form-function (undefined-function) ()
685   (:report
686    (lambda (condition stream)
687      (format stream
688              "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
689              (cell-error-name condition)))))
690
691 (define-condition arithmetic-error (error)
692   ((operation :reader arithmetic-error-operation
693               :initarg :operation
694               :initform nil)
695    (operands :reader arithmetic-error-operands
696              :initarg :operands))
697   (:report (lambda (condition stream)
698              (format stream
699                      "arithmetic error ~S signalled"
700                      (type-of condition))
701              (when (arithmetic-error-operation condition)
702                (format stream
703                        "~%Operation was ~S, operands ~S."
704                        (arithmetic-error-operation condition)
705                        (arithmetic-error-operands condition))))))
706
707 (define-condition division-by-zero         (arithmetic-error) ())
708 (define-condition floating-point-overflow  (arithmetic-error) ())
709 (define-condition floating-point-underflow (arithmetic-error) ())
710 (define-condition floating-point-inexact   (arithmetic-error) ())
711 (define-condition floating-point-invalid-operation (arithmetic-error) ())
712
713 (define-condition print-not-readable (error)
714   ((object :reader print-not-readable-object :initarg :object))
715   (:report
716    (lambda (condition stream)
717      (let ((obj (print-not-readable-object condition))
718            (*print-array* nil))
719        (format stream "~S cannot be printed readably." obj)))))
720
721 (define-condition reader-error (parse-error stream-error)
722   ((format-control
723     :reader reader-error-format-control
724     :initarg :format-control)
725    (format-arguments
726     :reader reader-error-format-arguments
727     :initarg :format-arguments
728     :initform '()))
729   (:report
730    (lambda (condition stream)
731      (let* ((error-stream (stream-error-stream condition))
732             (pos (file-position-or-nil-for-error error-stream)))
733        (let (lineno colno)
734          (when (and pos
735                     (< pos sb!xc:array-dimension-limit)
736                     ;; KLUDGE: lseek() (which is what FILE-POSITION
737                     ;; reduces to on file-streams) is undefined on
738                     ;; "some devices", which in practice means that it
739                     ;; can claim to succeed on /dev/stdin on Darwin
740                     ;; and Solaris.  This is obviously bad news,
741                     ;; because the READ-SEQUENCE below will then
742                     ;; block, not complete, and the report will never
743                     ;; be printed.  As a workaround, we exclude
744                     ;; interactive streams from this attempt to report
745                     ;; positions.  -- CSR, 2003-08-21
746                     (not (interactive-stream-p error-stream))
747                     (file-position error-stream :start))
748            (let ((string
749                   (make-string pos
750                                :element-type (stream-element-type
751                                               error-stream))))
752              (when (= pos (read-sequence string error-stream))
753                (setq lineno (1+ (count #\Newline string))
754                      colno (- pos
755                               (or (position #\Newline string :from-end t) -1)
756                               1))))
757            (file-position-or-nil-for-error error-stream pos))
758          (format stream
759                  "READER-ERROR ~@[at ~W ~]~
760                   ~@[(line ~W~]~@[, column ~W) ~]~
761                   on ~S:~%~?"
762                  pos lineno colno error-stream
763                  (reader-error-format-control condition)
764                  (reader-error-format-arguments condition)))))))
765 \f
766 ;;;; special SBCL extension conditions
767
768 ;;; an error apparently caused by a bug in SBCL itself
769 ;;;
770 ;;; Note that we don't make any serious effort to use this condition
771 ;;; for *all* errors in SBCL itself. E.g. type errors and array
772 ;;; indexing errors can occur in functions called from SBCL code, and
773 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
774 ;;; because the signalling code has no good way to know that the
775 ;;; underlying problem is a bug in SBCL. But in the fairly common case
776 ;;; that the signalling code does know that it's found a bug in SBCL,
777 ;;; this condition is appropriate, reusing boilerplate and helping
778 ;;; users to recognize it as an SBCL bug.
779 (define-condition bug (simple-error)
780   ()
781   (:report
782    (lambda (condition stream)
783      (format stream
784              "~@<  ~? ~:@_~?~:>"
785              (simple-condition-format-control condition)
786              (simple-condition-format-arguments condition)
787              "~@<This is probably a bug in SBCL itself. (Alternatively, ~
788               SBCL might have been corrupted by bad user code, e.g. by an ~
789               undefined Lisp operation like ~S, or by stray pointers from ~
790               alien code or from unsafe Lisp code; or there might be a bug ~
791               in the OS or hardware that SBCL is running on.) If it seems to ~
792               be a bug in SBCL itself, the maintainers would like to know ~
793               about it. Bug reports are welcome on the SBCL ~
794               mailing lists, which you can find at ~
795               <http://sbcl.sourceforge.net/>.~:@>"
796              '((fmakunbound 'compile))))))
797
798 (define-condition simple-storage-condition (storage-condition simple-condition) ())
799
800 ;;; a condition for use in stubs for operations which aren't supported
801 ;;; on some platforms
802 ;;;
803 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
804 ;;;   #-(or freebsd linux)
805 ;;;   (defun load-foreign (&rest rest)
806 ;;;     (error 'unsupported-operator :name 'load-foreign))
807 ;;;   #+(or freebsd linux)
808 ;;;   (defun load-foreign ... actual definition ...)
809 ;;; By signalling a standard condition in this case, we make it
810 ;;; possible for test code to distinguish between (1) intentionally
811 ;;; unimplemented and (2) unintentionally just screwed up somehow.
812 ;;; (Before this condition was defined, test code tried to deal with
813 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
814 ;;; sbcl-0.7.0, a a package screwup left the definition of
815 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
816 ;;; architectures where it was supposed to be supported, and the
817 ;;; regression tests cheerfully passed because they assumed that
818 ;;; unFBOUNDPness meant they were running on an system which didn't
819 ;;; support the extension.)
820 (define-condition unsupported-operator (simple-error) ())
821
822 \f
823 ;;; (:ansi-cl :function remove)
824 ;;; (:ansi-cl :section (a b c))
825 ;;; (:ansi-cl :glossary "similar")
826 ;;;
827 ;;; (:sbcl :node "...")
828 ;;; (:sbcl :variable *ed-functions*)
829 ;;;
830 ;;; FIXME: this is not the right place for this.
831 (defun print-reference (reference stream)
832   (ecase (car reference)
833     (:amop
834      (format stream "AMOP")
835      (format stream ", ")
836      (destructuring-bind (type data) (cdr reference)
837        (ecase type
838          (:readers "Readers for ~:(~A~) Metaobjects"
839                    (substitute #\  #\- (symbol-name data)))
840          (:initialization
841           (format stream "Initialization of ~:(~A~) Metaobjects"
842                   (substitute #\  #\- (symbol-name data))))
843          (:generic-function (format stream "Generic Function ~S" data))
844          (:function (format stream "Function ~S" data))
845          (:section (format stream "Section ~{~D~^.~}" data)))))
846     (:ansi-cl
847      (format stream "The ANSI Standard")
848      (format stream ", ")
849      (destructuring-bind (type data) (cdr reference)
850        (ecase type
851          (:function (format stream "Function ~S" data))
852          (:special-operator (format stream "Special Operator ~S" data))
853          (:macro (format stream "Macro ~S" data))
854          (:section (format stream "Section ~{~D~^.~}" data))
855          (:glossary (format stream "Glossary entry for ~S" data))
856          (:issue (format stream "writeup for Issue ~A" data)))))
857     (:sbcl
858      (format stream "The SBCL Manual")
859      (format stream ", ")
860      (destructuring-bind (type data) (cdr reference)
861        (ecase type
862          (:node (format stream "Node ~S" data))
863          (:variable (format stream "Variable ~S" data))
864          (:function (format stream "Function ~S" data)))))
865     ;; FIXME: other documents (e.g. CLIM, Franz documentation :-)
866     ))
867 (define-condition reference-condition ()
868   ((references :initarg :references :reader reference-condition-references)))
869 (defvar *print-condition-references* t)
870 (def!method print-object :around ((o reference-condition) s)
871   (call-next-method)
872   (unless (or *print-escape* *print-readably*)
873     (when (and *print-condition-references*
874                (reference-condition-references o))
875       (format s "~&See also:~%")
876       (pprint-logical-block (s nil :per-line-prefix "  ")
877         (do* ((rs (reference-condition-references o) (cdr rs))
878               (r (car rs) (car rs)))
879              ((null rs))
880           (print-reference r s)
881           (unless (null (cdr rs))
882             (terpri s)))))))
883
884 (define-condition duplicate-definition (reference-condition warning)
885   ((name :initarg :name :reader duplicate-definition-name))
886   (:report (lambda (c s)
887              (format s "~@<Duplicate definition for ~S found in ~
888                         one file.~@:>"
889                      (duplicate-definition-name c))))
890   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
891
892 (define-condition constant-modified (reference-condition warning)
893   ((fun-name :initarg :fun-name :reader constant-modified-fun-name))
894   (:report (lambda (c s)
895              (format s "~@<Destructive function ~S called on ~
896                         constant data.~@:>"
897                      (constant-modified-fun-name c))))
898   (:default-initargs :references (list '(:ansi-cl :special-operator quote)
899                                        '(:ansi-cl :section (3 2 2 3)))))
900
901 (define-condition package-at-variance (reference-condition simple-warning)
902   ()
903   (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
904
905 (define-condition defconstant-uneql (reference-condition error)
906   ((name :initarg :name :reader defconstant-uneql-name)
907    (old-value :initarg :old-value :reader defconstant-uneql-old-value)
908    (new-value :initarg :new-value :reader defconstant-uneql-new-value))
909   (:report
910    (lambda (condition stream)
911      (format stream
912              "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
913              (defconstant-uneql-name condition)
914              (defconstant-uneql-old-value condition)
915              (defconstant-uneql-new-value condition))))
916   (:default-initargs :references (list '(:ansi-cl :macro defconstant)
917                                        '(:sbcl :node "Idiosyncrasies"))))
918
919 (define-condition array-initial-element-mismatch
920     (reference-condition simple-warning)
921   ()
922   (:default-initargs
923       :references (list
924                    '(:ansi-cl :function make-array)
925                    '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
926
927 (define-condition displaced-to-array-too-small-error
928     (reference-condition simple-error)
929   ()
930   (:default-initargs
931       :references (list '(:ansi-cl :function adjust-array))))
932
933 (define-condition type-warning (reference-condition simple-warning)
934   ()
935   (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
936
937 (define-condition local-argument-mismatch (reference-condition simple-warning)
938   ()
939   (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
940
941 (define-condition format-args-mismatch (reference-condition)
942   ()
943   (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
944
945 (define-condition format-too-few-args-warning
946     (format-args-mismatch simple-warning)
947   ())
948 (define-condition format-too-many-args-warning
949     (format-args-mismatch simple-style-warning)
950   ())
951
952 (define-condition extension-failure (reference-condition simple-error)
953   ())
954
955 (define-condition structure-initarg-not-keyword
956     (reference-condition simple-style-warning)
957   ()
958   (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
959
960 #!+sb-package-locks
961 (progn
962
963 (define-condition package-lock-violation (reference-condition package-error)
964   ((format-control :initform nil :initarg :format-control
965                    :reader package-error-format-control)
966    (format-arguments :initform nil :initarg :format-arguments
967                      :reader package-error-format-arguments))
968   (:report
969    (lambda (condition stream)
970      (let ((control (package-error-format-control condition)))
971        (if control
972            (apply #'format stream
973                   (format nil "~~@<Lock on package ~A violated when ~A.~~:@>"
974                           (package-name (package-error-package condition))
975                           control)
976                   (package-error-format-arguments condition))
977            (format stream "~@<Lock on package ~A violated.~:@>"
978                    (package-name (package-error-package condition)))))))
979   ;; no :default-initargs -- reference-stuff provided by the
980   ;; signalling form in target-package.lisp
981   #!+sb-doc
982   (:documentation
983    "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
984 when a package-lock is violated."))
985
986 (define-condition package-locked-error (package-lock-violation) ()
987   #!+sb-doc
988   (:documentation
989    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
990 signalled when an operation on a package violates a package lock."))
991
992 (define-condition symbol-package-locked-error (package-lock-violation)
993   ((symbol :initarg :symbol :reader package-locked-error-symbol))
994   #!+sb-doc
995   (:documentation
996    "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
997 signalled when an operation on a symbol violates a package lock. The
998 symbol that caused the violation is accessed by the function
999 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
1000
1001 ) ; progn
1002
1003 (define-condition undefined-alien-error (cell-error) ()
1004   (:report
1005    (lambda (condition stream)
1006      (if (slot-boundp condition 'name)
1007          (format stream "Undefined alien: ~S" (cell-error-name condition))
1008          (format stream "Undefined alien symbol.")))))
1009
1010 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
1011   (:report
1012    (lambda (condition stream)
1013      (declare (ignore condition))
1014      (format stream "Attempt to access an undefined alien variable."))))
1015
1016 (define-condition undefined-alien-function-error (undefined-alien-error) ()
1017   (:report
1018    (lambda (condition stream)
1019      (declare (ignore condition))
1020      (format stream "Attempt to call an undefined alien function."))))
1021
1022 \f
1023 ;;;; various other (not specified by ANSI) CONDITIONs
1024 ;;;;
1025 ;;;; These might logically belong in other files; they're here, after
1026 ;;;; setup of CONDITION machinery, only because that makes it easier to
1027 ;;;; get cold init to work.
1028
1029 ;;; OAOOM warning: see cross-condition.lisp
1030 (define-condition encapsulated-condition (condition)
1031   ((condition :initarg :condition :reader encapsulated-condition)))
1032
1033 (define-condition values-type-error (type-error)
1034   ()
1035   (:report
1036    (lambda (condition stream)
1037      (format stream
1038              "~@<The values set ~2I~:_[~{~S~^ ~}] ~I~_is not of type ~2I~_~S.~:>"
1039              (type-error-datum condition)
1040              (type-error-expected-type condition)))))
1041
1042 ;;; KLUDGE: a condition for floating point errors when we can't or
1043 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1044 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1045 ;;; know how but the old code was broken by the conversion to POSIX
1046 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1047 ;;;
1048 ;;; FIXME: Perhaps this should also be a base class for all
1049 ;;; floating point exceptions?
1050 (define-condition floating-point-exception (arithmetic-error)
1051   ((flags :initarg :traps
1052           :initform nil
1053           :reader floating-point-exception-traps))
1054   (:report (lambda (condition stream)
1055              (format stream
1056                      "An arithmetic error ~S was signalled.~%"
1057                      (type-of condition))
1058              (let ((traps (floating-point-exception-traps condition)))
1059                (if traps
1060                    (format stream
1061                            "Trapping conditions are: ~%~{ ~S~^~}~%"
1062                            traps)
1063                    (write-line
1064                     "No traps are enabled? How can this be?"
1065                     stream))))))
1066
1067 (define-condition index-too-large-error (type-error)
1068   ()
1069   (:report
1070    (lambda (condition stream)
1071      (format stream
1072              "The index ~S is too large."
1073              (type-error-datum condition)))))
1074
1075 (define-condition bounding-indices-bad-error (reference-condition type-error)
1076   ((object :reader bounding-indices-bad-object :initarg :object))
1077   (:report
1078    (lambda (condition stream)
1079      (let* ((datum (type-error-datum condition))
1080             (start (car datum))
1081             (end (cdr datum))
1082             (object (bounding-indices-bad-object condition)))
1083        (etypecase object
1084          (sequence
1085           (format stream
1086                   "The bounding indices ~S and ~S are bad ~
1087                    for a sequence of length ~S."
1088                   start end (length object)))
1089          (array
1090           ;; from WITH-ARRAY-DATA
1091           (format stream
1092                   "The START and END parameters ~S and ~S are ~
1093                    bad for an array of total size ~S."
1094                   start end (array-total-size object)))))))
1095   (:default-initargs
1096       :references
1097       (list '(:ansi-cl :glossary "bounding index designator")
1098             '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1099
1100 (define-condition nil-array-accessed-error (reference-condition type-error)
1101   ()
1102   (:report (lambda (condition stream)
1103              (declare (ignore condition))
1104              (format stream
1105                      "An attempt to access an array of element-type ~
1106                       NIL was made.  Congratulations!")))
1107   (:default-initargs
1108       :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1109                         '(:ansi-cl :section (15 1 2 1))
1110                         '(:ansi-cl :section (15 1 2 2)))))
1111
1112 (define-condition namestring-parse-error (parse-error)
1113   ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1114    (args :reader namestring-parse-error-args :initarg :args :initform nil)
1115    (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1116    (offset :reader namestring-parse-error-offset :initarg :offset))
1117   (:report
1118    (lambda (condition stream)
1119      (format stream
1120              "parse error in namestring: ~?~%  ~A~%  ~V@T^"
1121              (namestring-parse-error-complaint condition)
1122              (namestring-parse-error-args condition)
1123              (namestring-parse-error-namestring condition)
1124              (namestring-parse-error-offset condition)))))
1125
1126 (define-condition simple-package-error (simple-condition package-error) ())
1127
1128 (define-condition reader-package-error (reader-error) ())
1129
1130 (define-condition reader-eof-error (end-of-file)
1131   ((context :reader reader-eof-error-context :initarg :context))
1132   (:report
1133    (lambda (condition stream)
1134      (format stream
1135              "unexpected end of file on ~S ~A"
1136              (stream-error-stream condition)
1137              (reader-eof-error-context condition)))))
1138
1139 (define-condition reader-impossible-number-error (reader-error)
1140   ((error :reader reader-impossible-number-error-error :initarg :error))
1141   (:report
1142    (lambda (condition stream)
1143      (let ((error-stream (stream-error-stream condition)))
1144        (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1145                (file-position-or-nil-for-error error-stream) error-stream
1146                (reader-error-format-control condition)
1147                (reader-error-format-arguments condition)
1148                (reader-impossible-number-error-error condition))))))
1149
1150 (define-condition timeout (serious-condition)
1151   ((seconds :initarg :seconds :initform nil :reader timeout-seconds))
1152   (:report (lambda (condition stream)
1153              (format stream "Timeout occurred~@[ after ~A seconds~]."
1154                      (timeout-seconds condition)))))
1155
1156 (define-condition io-timeout (stream-error timeout)
1157   ((direction :reader io-timeout-direction :initarg :direction))
1158   (:report
1159    (lambda (condition stream)
1160      (declare (type stream stream))
1161      (format stream
1162              "I/O timeout ~(~A~)ing ~S."
1163              (io-timeout-direction condition)
1164              (stream-error-stream condition)))))
1165
1166 (define-condition deadline-timeout (timeout) ()
1167   (:report (lambda (condition stream)
1168              (format stream "A deadline was reached after ~A seconds."
1169                      (timeout-seconds condition)))))
1170
1171 (define-condition declaration-type-conflict-error (reference-condition
1172                                                    simple-error)
1173   ()
1174   (:default-initargs
1175       :format-control "symbol ~S cannot be both the name of a type and the name of a declaration"
1176     :references (list '(:ansi-cl :section (3 8 21)))))
1177
1178 ;;; Single stepping conditions
1179
1180 (define-condition step-condition ()
1181   ((form :initarg :form :reader step-condition-form))
1182
1183   #!+sb-doc
1184   (:documentation "Common base class of single-stepping conditions.
1185 STEP-CONDITION-FORM holds a string representation of the form being
1186 stepped."))
1187
1188 #!+sb-doc
1189 (setf (fdocumentation 'step-condition-form 'function)
1190       "Form associated with the STEP-CONDITION.")
1191
1192 (define-condition step-form-condition (step-condition)
1193   ((args :initarg :args :reader step-condition-args))
1194   (:report
1195    (lambda (condition stream)
1196      (let ((*print-circle* t)
1197            (*print-pretty* t)
1198            (*print-readably* nil))
1199        (format stream
1200                  "Evaluating call:~%~<  ~@;~A~:>~%~
1201                   ~:[With arguments:~%~{  ~S~%~}~;With unknown arguments~]~%"
1202                (list (step-condition-form condition))
1203                (eq (step-condition-args condition) :unknown)
1204                (step-condition-args condition)))))
1205   #!+sb-doc
1206   (:documentation "Condition signalled by code compiled with
1207 single-stepping information when about to execute a form.
1208 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1209 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1210 holds the source-path to the original form within that file or NIL.
1211 Associated with this condition are always the restarts STEP-INTO,
1212 STEP-NEXT, and STEP-CONTINUE."))
1213
1214 #!+sb-doc
1215 (setf (fdocumentation 'step-condition-source-path 'function)
1216       "Source-path of the original form associated with the
1217 STEP-FORM-CONDITION or NIL."
1218       (fdocumentation 'step-condition-pathname 'function)
1219       "Pathname of the original source-file associated with the
1220 STEP-FORM-CONDITION or NIL.")
1221
1222 (define-condition step-result-condition (step-condition)
1223   ((result :initarg :result :reader step-condition-result)))
1224
1225 #!+sb-doc
1226 (setf (fdocumentation 'step-condition-result 'function)
1227       "Return values associated with STEP-VALUES-CONDITION as a list,
1228 or the variable value associated with STEP-VARIABLE-CONDITION.")
1229
1230 (define-condition step-values-condition (step-result-condition)
1231   ()
1232   #!+sb-doc
1233   (:documentation "Condition signalled by code compiled with
1234 single-stepping information after executing a form.
1235 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1236 the values returned by the form as a list. No associated restarts."))
1237
1238 (define-condition step-finished-condition (step-condition)
1239   ()
1240   (:report
1241    (lambda (condition stream)
1242      (declare (ignore condition))
1243      (format stream "Returning from STEP")))
1244   #!+sb-doc
1245   (:documentation "Condition signaled when STEP returns."))
1246
1247 \f
1248 ;;;; restart definitions
1249
1250 (define-condition abort-failure (control-error) ()
1251   (:report
1252    "An ABORT restart was found that failed to transfer control dynamically."))
1253
1254 (defun abort (&optional condition)
1255   #!+sb-doc
1256   "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1257    none exists."
1258   (invoke-restart (find-restart-or-control-error 'abort condition))
1259   ;; ABORT signals an error in case there was a restart named ABORT
1260   ;; that did not transfer control dynamically. This could happen with
1261   ;; RESTART-BIND.
1262   (error 'abort-failure))
1263
1264 (defun muffle-warning (&optional condition)
1265   #!+sb-doc
1266   "Transfer control to a restart named MUFFLE-WARNING, signalling a
1267    CONTROL-ERROR if none exists."
1268   (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1269
1270 (macrolet ((define-nil-returning-restart (name args doc)
1271              #!-sb-doc (declare (ignore doc))
1272              `(defun ,name (,@args &optional condition)
1273                 #!+sb-doc ,doc
1274                 ;; FIXME: Perhaps this shared logic should be pulled out into
1275                 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
1276                 (let ((restart (find-restart ',name condition)))
1277                   (when restart
1278                     (invoke-restart restart ,@args))))))
1279   (define-nil-returning-restart continue ()
1280     "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1281   (define-nil-returning-restart store-value (value)
1282     "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
1283    none exists.")
1284   (define-nil-returning-restart use-value (value)
1285     "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
1286    none exists."))
1287
1288 ;;; single-stepping restarts
1289
1290 (macrolet ((def (name doc)
1291                #!-sb-doc (declare (ignore doc))
1292                `(defun ,name (condition)
1293                  #!+sb-doc ,doc
1294                  (invoke-restart (find-restart-or-control-error ',name condition)))))
1295   (def step-continue
1296       "Transfers control to the STEP-CONTINUE restart associated with
1297 the condition, continuing execution without stepping. Signals a
1298 CONTROL-ERROR if the restart does not exist.")
1299   (def step-next
1300       "Transfers control to the STEP-NEXT restart associated with the
1301 condition, executing the current form without stepping and continuing
1302 stepping with the next form. Signals CONTROL-ERROR is the restart does
1303 not exists.")
1304   (def step-into
1305       "Transfers control to the STEP-INTO restart associated with the
1306 condition, stepping into the current form. Signals a CONTROL-ERROR is
1307 the restart does not exist."))
1308
1309 (/show0 "condition.lisp end of file")
1310