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 (defglobal **initial-handler-clusters
**
20 `(((,(lambda (condition)
21 (typep condition
'warning
))
24 (when (muffle-warning-p warning
)
25 (muffle-warning warning
)))))))
27 ;;; Each cluster is an alist of the form
29 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
31 ;;; where TYPE-TESTN are functions of one argument which test a given
32 ;;; condition instance for the type required by the corresponding
33 ;;; HANDLERN. HANDLERN are function designators.
35 ;;; Newly established handlers are added at the beginning of the
36 ;;; list. Elements to the left of the alist take precedence over
37 ;;; elements to the right.
39 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
41 (defvar *handler-clusters
* **initial-handler-clusters
**)
43 ;;; a list of lists of currently active RESTART instances. maintained
45 (defvar *restart-clusters
* '())
47 (def!method print-object
((restart restart
) stream
)
49 (print-unreadable-object (restart stream
:type t
:identity t
)
50 (prin1 (restart-name restart
) stream
))
51 (restart-report restart stream
)))
54 (setf (fdocumentation 'restart-name
'function
)
55 "Return the name of the given restart object.")
57 (defun restart-report (restart stream
)
58 (if (restart-report-function restart
)
59 (funcall (truly-the function
(restart-report-function restart
))
61 (prin1 (or (restart-name restart
)
65 (defvar *restart-test-stack
* nil
)
67 ;; Call FUNCTION with all restarts in the current dynamic environment,
68 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
69 ;; restarts are processed)
70 ;; 2) and for which the restart test returns non-NIL for CONDITION.
71 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
72 (defun map-restarts (function &optional condition
(call-test-p t
))
73 (declare (function function
))
74 (let ((stack *restart-test-stack
*))
75 (dolist (restart-cluster *restart-clusters
*)
76 (dolist (restart restart-cluster
)
77 (when (and (or (not condition
)
78 (null (restart-associated-conditions restart
))
79 (memq condition
(restart-associated-conditions restart
)))
80 ;; A call to COMPUTE-RESTARTS -- from an error,
81 ;; from user code, whatever -- inside the test
82 ;; function would cause infinite recursion here, so
83 ;; we disable each restart using
84 ;; *restart-test-stack* for the duration of the
86 (not (memq restart stack
))
88 (let ((*restart-test-stack
* (cons restart stack
)))
89 (declare (truly-dynamic-extent *restart-test-stack
*))
90 (funcall (restart-test-function restart
) condition
))))
91 (funcall function restart
))))))
93 (defun compute-restarts (&optional condition
)
95 "Return a list of all the currently active restarts ordered from most recently
96 established to less recently established. If CONDITION is specified, then only
97 restarts associated with CONDITION (or with no condition) will be returned."
99 (map-restarts (lambda (restart) (result restart
)) condition
)
102 (defun %find-restart
(identifier condition
&optional
(call-test-p t
))
103 (flet ((eq-restart-p (restart)
104 (when (eq identifier restart
)
105 (return-from %find-restart restart
)))
106 (named-restart-p (restart)
107 (when (eq identifier
(restart-name restart
))
108 (return-from %find-restart restart
))))
109 ;; KLUDGE: can the compiler infer this dx automatically?
110 (declare (truly-dynamic-extent #'eq-restart-p
#'named-restart-p
))
111 (if (typep identifier
'restart
)
112 ;; The code under #+previous-... below breaks the abstraction
113 ;; introduced by MAP-RESTARTS, but is about twice as
114 ;; fast as #+equivalent-... . Also, it is a common case due to
116 ;; (INVOKE-RESTART RESTART)
117 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
118 ;; -> (FIND-RESTART RESTART)
120 ;; However, both #+previous-... and #+equivalent-... may be
121 ;; wrong altogether because of
122 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
123 ;; The behavior expected in that report can be achieved by the
124 ;; following line (which is, of course, the slowest of all
126 (map-restarts #'eq-restart-p condition call-test-p
)
128 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
129 (map-restarts #'eq-restart-p nil nil
)
131 #+previous-behavior--fastest-but-see-bug-774410
132 (and (find-if (lambda (cluster) (find identifier cluster
)) *restart-clusters
*)
135 (map-restarts #'named-restart-p condition call-test-p
))))
137 (defun find-restart (identifier &optional condition
)
139 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
140 then the innermost applicable restart with that name is returned. If IDENTIFIER
141 is a restart, it is returned if it is currently active. Otherwise NIL is
142 returned. If CONDITION is specified and not NIL, then only restarts associated
143 with that condition (or with no condition) will be returned."
144 ;; Calls MAP-RESTARTS such that restart test functions are
146 (%find-restart identifier condition
))
148 ;;; helper for the various functions which are ANSI-spec'ed to do
149 ;;; something with a restart or signal CONTROL-ERROR if there is none
150 (defun find-restart-or-control-error (identifier &optional condition
(call-test-p t
))
151 (or (%find-restart identifier condition call-test-p
)
152 (error 'simple-control-error
153 :format-control
"No restart ~S is active~@[ for ~S~]."
154 :format-arguments
(list identifier condition
))))
156 (defun invoke-restart (restart &rest values
)
158 "Calls the function associated with the given restart, passing any given
159 arguments. If the argument restart is not a restart or a currently active
160 non-nil restart name, then a CONTROL-ERROR is signalled."
161 (/show
"entering INVOKE-RESTART" restart
)
162 ;; The following code calls MAP-RESTARTS (through
163 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
164 ;; test functions are respected when RESTART is a symbol, but not
165 ;; when RESTART is a RESTART instance.
167 ;; Without disabling test functions for the RESTART instance case,
168 ;; the following problem would arise:
172 ;; ((some-condition (lambda (c)
173 ;; (invoke-restart (find-restart 'foo c)) ; a)
174 ;; (invoke-restart 'foo) ; b)
176 ;; (signal 'some-condition))
178 ;; :test (lambda (c) (typep c 'some-condition))))
180 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
181 ;; cannot supply the condition instance needed by the test. In case
182 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
183 ;; condition instance. As a result, the restart would be impossible
185 (let ((real-restart (find-restart-or-control-error
186 restart nil
(symbolp restart
))))
187 (apply (restart-function real-restart
) values
)))
189 (defun interactive-restart-arguments (real-restart)
190 (let ((interactive-function (restart-interactive-function real-restart
)))
191 (if interactive-function
192 (funcall interactive-function
)
195 (defun invoke-restart-interactively (restart)
197 "Calls the function associated with the given restart, prompting for any
198 necessary arguments. If the argument restart is not a restart or a
199 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
200 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
201 ;; see comment in INVOKE-RESTART.
202 (let* ((real-restart (find-restart-or-control-error
203 restart nil
(symbolp restart
)))
204 (args (interactive-restart-arguments real-restart
)))
205 (apply (restart-function real-restart
) args
)))
208 (defun assert-error (assertion args-and-values places datum
&rest arguments
)
209 (let ((cond (if datum
211 datum arguments
'simple-error
'error
)
214 :format-control
"~@<The assertion ~S failed~:[.~:; ~
215 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
216 :format-arguments
(list assertion args-and-values
)))))
220 :report
(lambda (stream)
221 (format stream
"Retry assertion")
223 (format stream
" with new value~P for ~{~S~^, ~}."
224 (length places
) places
)
225 (format stream
".")))
228 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
229 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
230 ;;; and by CHECK-TYPE.
231 (defun read-evaluated-form (&optional
(prompt-control nil promptp
)
233 (apply #'format
*query-io
*
234 (if promptp prompt-control
"~&Type a form to be evaluated: ")
236 (finish-output *query-io
*)
237 (list (eval (read *query-io
*))))
239 (defun check-type-error (place place-value type type-string
)
246 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
247 :format-arguments
(list place place-value type-string type
))))
248 (restart-case (error condition
)
250 :report
(lambda (stream)
251 (format stream
"Supply a new value for ~S." place
))
252 :interactive read-evaluated-form
255 (defun case-failure (name value keys
)
259 :expected-type
(if (eq name
'ecase
)
262 :possibilities keys
))
264 (defun case-body-error (name keyform keyform-value expected-type keys
)
269 :expected-type expected-type
272 :report
(lambda (stream)
273 (format stream
"Supply a new value for ~S." keyform
))
274 :interactive read-evaluated-form