0.8.0.37:
[sbcl.git] / src / code / target-error.lisp
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!KERNEL")
14 \f
15 ;;; a list of lists of restarts
16 (defvar *restart-clusters* '())
17
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts* ())
21
22 (defvar *handler-clusters* nil)
23
24 (defstruct (restart (:copier nil) (:predicate nil))
25   (name (missing-arg) :type symbol :read-only t)
26   (function (missing-arg) :type function)
27   (report-function nil :type (or null function))
28   (interactive-function nil :type (or null function))
29   (test-function (lambda (cond) (declare (ignore cond)) t) :type function))
30 (def!method print-object ((restart restart) stream)
31   (if *print-escape*
32       (print-unreadable-object (restart stream :type t :identity t)
33         (prin1 (restart-name restart) stream))
34       (restart-report restart stream)))
35
36 (defun compute-restarts (&optional condition)
37   #!+sb-doc
38   "Return a list of all the currently active restarts ordered from most
39    recently established to less recently established. If CONDITION is
40    specified, then only restarts associated with CONDITION (or with no
41    condition) will be returned."
42   (let ((associated ())
43         (other ()))
44     (dolist (alist *condition-restarts*)
45       (if (eq (car alist) condition)
46           (setq associated (cdr alist))
47           (setq other (append (cdr alist) other))))
48     (collect ((res))
49       (dolist (restart-cluster *restart-clusters*)
50         (dolist (restart restart-cluster)
51           (when (and (or (not condition)
52                          (member restart associated)
53                          (not (member restart other)))
54                      (funcall (restart-test-function restart)
55                               condition))
56             (res restart))))
57       (res))))
58
59 #!+sb-doc
60 (setf (fdocumentation 'restart-name 'function)
61       "Return the name of the given restart object.")
62
63 (defun restart-report (restart stream)
64   (funcall (or (restart-report-function restart)
65                (let ((name (restart-name restart)))
66                  (lambda (stream)
67                    (if name (format stream "~S" name)
68                        (format stream "~S" restart)))))
69            stream))
70
71 (defun find-restart (name &optional condition)
72   #!+sb-doc
73   "Return the first restart named NAME. If NAME names a restart, the restart
74    is returned if it is currently active. If no such restart is found, NIL is
75    returned. It is an error to supply NIL as a name. If CONDITION is specified
76    and not NIL, then only restarts associated with that condition (or with no
77    condition) will be returned."
78   (let ((restarts (compute-restarts condition)))
79     (declare (type list restarts))
80     (find-if (lambda (x)
81                (or (eq x name)
82                    (eq (restart-name x) name)))
83              restarts)))
84
85 (defun find-restart-or-lose (restart-designator)
86   (let ((real-restart (find-restart restart-designator)))
87     (unless real-restart
88       (error 'simple-control-error
89              :format-control "Restart ~S is not active."
90              :format-arguments (list restart-designator)))
91     real-restart))
92
93 (defun invoke-restart (restart &rest values)
94   #!+sb-doc
95   "Calls the function associated with the given restart, passing any given
96    arguments. If the argument restart is not a restart or a currently active
97    non-nil restart name, then a control-error is signalled."
98   (/show "entering INVOKE-RESTART" restart)
99   (let ((real-restart (find-restart-or-lose restart)))
100     (apply (restart-function real-restart) values)))
101
102 (defun interactive-restart-arguments (real-restart)
103   (let ((interactive-function (restart-interactive-function real-restart)))
104     (if interactive-function
105         (funcall interactive-function)
106         '())))
107
108 (defun invoke-restart-interactively (restart)
109   #!+sb-doc
110   "Calls the function associated with the given restart, prompting for any
111    necessary arguments. If the argument restart is not a restart or a
112    currently active non-nil restart name, then a control-error is signalled."
113   (let* ((real-restart (find-restart-or-lose restart))
114          (args (interactive-restart-arguments real-restart)))
115     (apply (restart-function real-restart) args)))
116 \f
117 \f
118
119
120
121 ;;;; helper functions for restartable error handling which couldn't be
122 ;;;; defined 'til now 'cause they use the RESTART-CASE macro
123
124 (defun assert-error (assertion places datum &rest arguments)
125   (let ((cond (if datum
126                 (coerce-to-condition datum
127                                                     arguments
128                                                     'simple-error
129                                                     'error)
130                 (make-condition 'simple-error
131                                 :format-control "The assertion ~S failed."
132                                 :format-arguments (list assertion)))))
133     (restart-case
134         (error cond)
135       (continue ()
136                 :report (lambda (stream)
137                           (format stream "Retry assertion")
138                           (if places
139                               (format stream
140                                       " with new value~P for ~{~S~^, ~}."
141                                       (length places)
142                                       places)
143                               (format stream ".")))
144                 nil))))
145
146 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
147 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
148 ;;; and by CHECK-TYPE.
149 (defun read-evaluated-form ()
150   (format *query-io* "~&Type a form to be evaluated:~%")
151   (list (eval (read *query-io*))))
152
153 (defun check-type-error (place place-value type type-string)
154   (let ((cond (if type-string
155                   (make-condition 'simple-type-error
156                                   :datum place
157                                   :expected-type type
158                                   :format-control
159                                   "The value of ~S is ~S, which is not ~A."
160                                   :format-arguments (list place
161                                                           place-value
162                                                           type-string))
163                   (make-condition 'simple-type-error
164                                   :datum place
165                                   :expected-type type
166                                   :format-control
167                           "The value of ~S is ~S, which is not of type ~S."
168                                   :format-arguments (list place
169                                                           place-value
170                                                           type)))))
171     (restart-case (error cond)
172       (store-value (value)
173         :report (lambda (stream)
174                   (format stream "Supply a new value for ~S." place))
175         :interactive read-evaluated-form
176         value))))
177
178 (defun case-body-error (name keyform keyform-value expected-type keys)
179   (restart-case
180       (error 'case-failure
181              :name name
182              :datum keyform-value
183              :expected-type expected-type
184              :possibilities keys)
185     (store-value (value)
186       :report (lambda (stream)
187                 (format stream "Supply a new value for ~S." keyform))
188       :interactive read-evaluated-form
189       value)))