1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
4 ;;;; This software is part of the SBCL system. See the README file for
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.
13 (in-package "SB!KERNEL")
15 (defun muffle-warning-p (warning)
16 (declare (special *muffled-warnings
*))
17 (typep warning
*muffled-warnings
*))
19 ;; Host lisp does not need a value for this, so start it out as NIL.
20 (defglobal **initial-handler-clusters
** nil
)
21 (setq **initial-handler-clusters
**
22 `(((,(find-classoid-cell 'warning
:create t
)
24 ,(named-lambda "MAYBE-MUFFLE" (warning)
25 (when (muffle-warning-p warning
)
26 (muffle-warning warning
)))))))
28 ;;; Each cluster is an alist of the form
30 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
32 ;;; where TYPE-TESTN are functions of one argument which test a given
33 ;;; condition instance for the type required by the corresponding
34 ;;; HANDLERN. HANDLERN are function designators.
36 ;;; Newly established handlers are added at the beginning of the
37 ;;; list. Elements to the left of the alist take precedence over
38 ;;; elements to the right.
40 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
42 (defvar *handler-clusters
* **initial-handler-clusters
**)
44 ;;; a list of lists of currently active RESTART instances. maintained
46 ;;; This variable is proclaimed to be "eventually" always-bound,
47 ;;; meaning in the loaded code. If DEFVAR were used, the compiler knows that
48 ;;; BOUNDP will be T, and therefore a code deletion note should be issused
49 ;;; for the initialization expression (UNLESS (BOUNDP x) <initform>).
50 ;;; Technically it's not even right to use DEFVAR because it works only
51 ;;; if the initializer is really NIL, since that is what %DEFVAR will assign
52 ;;; on account of the fact that an initializer was supplied at all.
53 (defparameter *restart-clusters
* '())
55 (defmethod print-object ((restart restart
) stream
)
57 (print-unreadable-object (restart stream
:type t
:identity t
)
58 (prin1 (restart-name restart
) stream
))
59 (restart-report restart stream
)))
62 (setf (fdocumentation 'restart-name
'function
)
63 "Return the name of the given restart object.")
65 (defun restart-report (restart stream
)
66 (if (restart-report-function restart
)
67 (funcall (truly-the function
(restart-report-function restart
))
69 (prin1 (or (restart-name restart
)
73 (defvar *restart-test-stack
* nil
)
75 ;; Call FUNCTION with all restarts in the current dynamic environment,
76 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
77 ;; restarts are processed)
78 ;; 2) and for which the restart test returns non-NIL for CONDITION.
79 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
80 (defun map-restarts (function &optional condition
(call-test-p t
))
81 (declare (function function
))
82 (let ((stack *restart-test-stack
*))
83 (dolist (restart-cluster *restart-clusters
*)
84 (dolist (restart restart-cluster
)
85 (when (and (or (not condition
)
86 (null (restart-associated-conditions restart
))
87 (memq condition
(restart-associated-conditions restart
)))
88 ;; A call to COMPUTE-RESTARTS -- from an error,
89 ;; from user code, whatever -- inside the test
90 ;; function would cause infinite recursion here, so
91 ;; we disable each restart using
92 ;; *restart-test-stack* for the duration of the
94 (not (memq restart stack
))
96 (let ((*restart-test-stack
* (cons restart stack
)))
97 (declare (truly-dynamic-extent *restart-test-stack
*))
98 (funcall (restart-test-function restart
) condition
))))
99 (funcall function restart
))))))
101 (defun compute-restarts (&optional condition
)
103 "Return a list of all the currently active restarts ordered from most recently
104 established to less recently established. If CONDITION is specified, then only
105 restarts associated with CONDITION (or with no condition) will be returned."
107 (map-restarts (lambda (restart) (result restart
)) condition
)
110 (defun %find-restart
(identifier condition
&optional
(call-test-p t
))
111 (flet ((eq-restart-p (restart)
112 (when (eq identifier restart
)
113 (return-from %find-restart restart
)))
114 (named-restart-p (restart)
115 (when (eq identifier
(restart-name restart
))
116 (return-from %find-restart restart
))))
117 ;; KLUDGE: can the compiler infer this dx automatically?
118 (declare (truly-dynamic-extent #'eq-restart-p
#'named-restart-p
))
119 (if (typep identifier
'restart
)
120 ;; The code under #+previous-... below breaks the abstraction
121 ;; introduced by MAP-RESTARTS, but is about twice as
122 ;; fast as #+equivalent-... . Also, it is a common case due to
124 ;; (INVOKE-RESTART RESTART)
125 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
126 ;; -> (FIND-RESTART RESTART)
128 ;; However, both #+previous-... and #+equivalent-... may be
129 ;; wrong altogether because of
130 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
131 ;; The behavior expected in that report can be achieved by the
132 ;; following line (which is, of course, the slowest of all
134 (map-restarts #'eq-restart-p condition call-test-p
)
136 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
137 (map-restarts #'eq-restart-p nil nil
)
139 #+previous-behavior--fastest-but-see-bug-774410
140 (and (find-if (lambda (cluster) (find identifier cluster
)) *restart-clusters
*)
143 (map-restarts #'named-restart-p condition call-test-p
))))
145 (defun find-restart (identifier &optional condition
)
147 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
148 then the innermost applicable restart with that name is returned. If IDENTIFIER
149 is a restart, it is returned if it is currently active. Otherwise NIL is
150 returned. If CONDITION is specified and not NIL, then only restarts associated
151 with that condition (or with no condition) will be returned."
152 ;; Calls MAP-RESTARTS such that restart test functions are
154 (%find-restart identifier condition
))
156 ;;; helper for the various functions which are ANSI-spec'ed to do
157 ;;; something with a restart or signal CONTROL-ERROR if there is none
158 (defun find-restart-or-control-error (identifier &optional condition
(call-test-p t
))
159 (declare (optimize allow-non-returning-tail-call
))
160 (or (%find-restart identifier condition call-test-p
)
161 (error 'simple-control-error
162 :format-control
"No restart ~S is active~@[ for ~S~]."
163 :format-arguments
(list identifier condition
))))
165 (defun invoke-restart (restart &rest values
)
167 "Calls the function associated with the given restart, passing any given
168 arguments. If the argument restart is not a restart or a currently active
169 non-nil restart name, then a CONTROL-ERROR is signalled."
170 (/show
"entering INVOKE-RESTART" restart
)
171 ;; The following code calls MAP-RESTARTS (through
172 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
173 ;; test functions are respected when RESTART is a symbol, but not
174 ;; when RESTART is a RESTART instance.
176 ;; Without disabling test functions for the RESTART instance case,
177 ;; the following problem would arise:
181 ;; ((some-condition (lambda (c)
182 ;; (invoke-restart (find-restart 'foo c)) ; a)
183 ;; (invoke-restart 'foo) ; b)
185 ;; (signal 'some-condition))
187 ;; :test (lambda (c) (typep c 'some-condition))))
189 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
190 ;; cannot supply the condition instance needed by the test. In case
191 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
192 ;; condition instance. As a result, the restart would be impossible
194 (let ((real-restart (find-restart-or-control-error
195 restart nil
(symbolp restart
))))
196 (apply (restart-function real-restart
) values
)))
198 (defun interactive-restart-arguments (real-restart)
199 (let ((interactive-function (restart-interactive-function real-restart
)))
200 (if interactive-function
201 (funcall interactive-function
)
204 (defun invoke-restart-interactively (restart)
206 "Calls the function associated with the given restart, prompting for any
207 necessary arguments. If the argument restart is not a restart or a
208 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
209 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
210 ;; see comment in INVOKE-RESTART.
211 (let* ((real-restart (find-restart-or-control-error
212 restart nil
(symbolp restart
)))
213 (args (interactive-restart-arguments real-restart
)))
214 (apply (restart-function real-restart
) args
)))
216 ;;; To reduce expansion size of RESTART-CASE
217 (defun with-simple-condition-restarts (function cerror-arg datum
&rest arguments
)
218 (let ((condition (apply #'coerce-to-condition datum
220 (warn 'simple-warning
)
221 (signal 'simple-condition
)
225 (with-condition-restarts condition
(car *restart-clusters
*)
226 (if (eq function
'cerror
)
227 (cerror cerror-arg condition
)
228 (funcall function condition
)))))
231 (defun assert-error (assertion &optional args-and-values places datum
&rest arguments
)
232 (let ((cond (if datum
233 (apply #'coerce-to-condition
234 datum
'simple-error
'error arguments
)
237 :format-control
"~@<The assertion ~S failed~:[.~:; ~
238 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
239 :format-arguments
(list assertion args-and-values
)))))
243 :report
(lambda (stream)
244 (format stream
"Retry assertion")
246 (format stream
" with new value~P for ~{~S~^, ~}."
247 (length places
) places
)
248 (format stream
".")))
251 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
252 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
253 ;;; and by CHECK-TYPE.
254 (defun read-evaluated-form (&optional
(prompt-control nil promptp
)
256 (apply #'format
*query-io
*
257 (if promptp prompt-control
"~&Type a form to be evaluated: ")
259 (finish-output *query-io
*)
260 (list (eval (read *query-io
*))))
262 (defun check-type-error (place place-value type
&optional type-string
)
269 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
270 :format-arguments
(list place place-value type-string type
))))
271 (restart-case (error condition
)
273 :report
(lambda (stream)
274 (format stream
"Supply a new value for ~S." place
))
275 :interactive read-evaluated-form
278 (defun etypecase-failure (value keys
)
279 (declare (optimize allow-non-returning-tail-call
))
283 :expected-type
`(or ,@keys
)
284 :possibilities keys
))
286 (defun ecase-failure (value keys
)
287 (declare (optimize allow-non-returning-tail-call
))
291 :expected-type
`(member ,@keys
)
292 :possibilities keys
))
294 (defun case-body-error (name keyform keyform-value expected-type keys
)
299 :expected-type expected-type
302 :report
(lambda (stream)
303 (format stream
"Supply a new value for ~S." keyform
))
304 :interactive read-evaluated-form