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
)
22 ;;; Each cluster is an alist of the form
24 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
26 ;;; where TYPE-TESTN are functions of one argument which test a given
27 ;;; condition instance for the type required by the corresponding
28 ;;; HANDLERN. HANDLERN are function designators.
30 ;;; Newly established handlers are added at the beginning of the
31 ;;; list. Elements to the left of the alist take precedence over
32 ;;; elements to the right.
34 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
36 #!+sb-thread
(!define-thread-local
*handler-clusters
* **initial-handler-clusters
**)
37 #!-sb-thread
(defvar *handler-clusters
* **initial-handler-clusters
**)
39 ;;; a list of lists of currently active RESTART instances. maintained
41 (!define-thread-local
*restart-clusters
* nil
)
43 (defun !target-error-cold-init
()
44 (setq **initial-handler-clusters
**
45 `(((,(find-classoid-cell 'warning
:create t
)
47 ,(named-lambda "MAYBE-MUFFLE" (warning)
48 (when (muffle-warning-p warning
)
49 (muffle-warning warning
)))))))
50 ;;; If multithreaded, *HANDLER-CLUSTERS* is #<unbound> at this point.
51 ;;; This SETQ assigns to TLS since the value is not no-tls-value-marker.
52 (setq *handler-clusters
* **initial-handler-clusters
**))
54 (defmethod print-object ((restart restart
) stream
)
56 (print-unreadable-object (restart stream
:type t
:identity t
)
57 (prin1 (restart-name restart
) stream
))
58 (restart-report restart stream
)))
60 (setf (fdocumentation 'restart-name
'function
)
61 "Return the name of the given restart object.")
63 (defun restart-report (restart stream
)
64 (if (restart-report-function restart
)
65 (funcall (truly-the function
(restart-report-function restart
))
67 (prin1 (or (restart-name restart
)
71 (defvar *restart-test-stack
* nil
)
73 ;; Call FUNCTION with all restarts in the current dynamic environment,
74 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
75 ;; restarts are processed)
76 ;; 2) and for which the restart test returns non-NIL for CONDITION.
77 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
78 (defun map-restarts (function &optional condition
(call-test-p t
))
79 (declare (function function
))
80 (let ((stack *restart-test-stack
*))
81 (dolist (restart-cluster *restart-clusters
*)
82 (dolist (restart restart-cluster
)
83 (when (and (or (not condition
)
84 (null (restart-associated-conditions restart
))
85 (memq condition
(restart-associated-conditions restart
)))
86 ;; A call to COMPUTE-RESTARTS -- from an error,
87 ;; from user code, whatever -- inside the test
88 ;; function would cause infinite recursion here, so
89 ;; we disable each restart using
90 ;; *restart-test-stack* for the duration of the
92 (not (memq restart stack
))
94 (let ((*restart-test-stack
* (cons restart stack
)))
95 (declare (truly-dynamic-extent *restart-test-stack
*))
96 (funcall (restart-test-function restart
) condition
))))
97 (funcall function restart
))))))
99 (defun compute-restarts (&optional condition
)
100 "Return a list of all the currently active restarts ordered from most recently
101 established to less recently established. If CONDITION is specified, then only
102 restarts associated with CONDITION (or with no condition) will be returned."
104 (map-restarts (lambda (restart) (result restart
)) condition
)
107 (defun %find-restart
(identifier condition
&optional
(call-test-p t
))
108 (flet ((eq-restart-p (restart)
109 (when (eq identifier restart
)
110 (return-from %find-restart restart
)))
111 (named-restart-p (restart)
112 (when (eq identifier
(restart-name restart
))
113 (return-from %find-restart restart
))))
114 ;; KLUDGE: can the compiler infer this dx automatically?
115 (declare (truly-dynamic-extent #'eq-restart-p
#'named-restart-p
))
116 (if (typep identifier
'restart
)
117 ;; The code under #+previous-... below breaks the abstraction
118 ;; introduced by MAP-RESTARTS, but is about twice as
119 ;; fast as #+equivalent-... . Also, it is a common case due to
121 ;; (INVOKE-RESTART RESTART)
122 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
123 ;; -> (FIND-RESTART RESTART)
125 ;; However, both #+previous-... and #+equivalent-... may be
126 ;; wrong altogether because of
127 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
128 ;; The behavior expected in that report can be achieved by the
129 ;; following line (which is, of course, the slowest of all
131 (map-restarts #'eq-restart-p condition call-test-p
)
133 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
134 (map-restarts #'eq-restart-p nil nil
)
136 #+previous-behavior--fastest-but-see-bug-774410
137 (and (find-if (lambda (cluster) (find identifier cluster
)) *restart-clusters
*)
140 (map-restarts #'named-restart-p condition call-test-p
))))
142 (defun find-restart (identifier &optional condition
)
143 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
144 then the innermost applicable restart with that name is returned. If IDENTIFIER
145 is a restart, it is returned if it is currently active. Otherwise NIL is
146 returned. If CONDITION is specified and not NIL, then only restarts associated
147 with that condition (or with no condition) will be returned."
148 ;; Calls MAP-RESTARTS such that restart test functions are
150 (%find-restart identifier condition
))
152 ;;; helper for the various functions which are ANSI-spec'ed to do
153 ;;; something with a restart or signal CONTROL-ERROR if there is none
154 (defun find-restart-or-control-error (identifier &optional condition
(call-test-p t
))
155 (declare (optimize allow-non-returning-tail-call
))
156 (or (%find-restart identifier condition call-test-p
)
157 (error 'simple-control-error
158 :format-control
"No restart ~S is active~@[ for ~S~]."
159 :format-arguments
(list identifier condition
))))
161 (defun invoke-restart (restart &rest values
)
162 "Calls the function associated with the given restart, passing any given
163 arguments. If the argument restart is not a restart or a currently active
164 non-nil restart name, then a CONTROL-ERROR is signalled."
165 (/show
"entering INVOKE-RESTART" restart
)
166 ;; The following code calls MAP-RESTARTS (through
167 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
168 ;; test functions are respected when RESTART is a symbol, but not
169 ;; when RESTART is a RESTART instance.
171 ;; Without disabling test functions for the RESTART instance case,
172 ;; the following problem would arise:
176 ;; ((some-condition (lambda (c)
177 ;; (invoke-restart (find-restart 'foo c)) ; a)
178 ;; (invoke-restart 'foo) ; b)
180 ;; (signal 'some-condition))
182 ;; :test (lambda (c) (typep c 'some-condition))))
184 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
185 ;; cannot supply the condition instance needed by the test. In case
186 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
187 ;; condition instance. As a result, the restart would be impossible
189 (let ((real-restart (find-restart-or-control-error
190 restart nil
(symbolp restart
))))
191 (apply (restart-function real-restart
) values
)))
193 (defun interactive-restart-arguments (real-restart)
194 (let ((interactive-function (restart-interactive-function real-restart
)))
195 (if interactive-function
196 (funcall interactive-function
)
199 (defun invoke-restart-interactively (restart)
200 "Calls the function associated with the given restart, prompting for any
201 necessary arguments. If the argument restart is not a restart or a
202 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
203 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
204 ;; see comment in INVOKE-RESTART.
205 (let* ((real-restart (find-restart-or-control-error
206 restart nil
(symbolp restart
)))
207 (args (interactive-restart-arguments real-restart
)))
208 (apply (restart-function real-restart
) args
)))
210 ;;; To reduce expansion size of RESTART-CASE
211 (defun with-simple-condition-restarts (function cerror-arg datum
&rest arguments
)
212 (let ((sb!debug
:*stack-top-hint
* (or sb
!debug
:*stack-top-hint
*
213 'with-simple-condition-restarts
))
214 (condition (apply #'coerce-to-condition datum
216 (warn 'simple-warning
)
217 (signal 'simple-condition
)
221 (with-condition-restarts condition
(car *restart-clusters
*)
222 (if (eq function
'cerror
)
223 (cerror cerror-arg condition
)
224 (funcall function condition
)))))
227 (defun assert-error (assertion &optional args-and-values places datum
&rest arguments
)
228 (let ((cond (if datum
229 (apply #'coerce-to-condition
230 datum
'simple-error
'error arguments
)
233 :format-control
"~@<The assertion ~S failed~:[.~:; ~
234 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
235 :format-arguments
(list assertion args-and-values
)))))
239 :report
(lambda (stream)
240 (format stream
"Retry assertion")
242 (format stream
" with new value~P for ~{~S~^, ~}."
243 (length places
) places
)
244 (format stream
".")))
247 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
248 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
249 ;;; and by CHECK-TYPE.
250 (defun read-evaluated-form (&optional
(prompt-control nil promptp
)
252 (apply #'format
*query-io
*
253 (if promptp prompt-control
"~&Type a form to be evaluated: ")
255 (finish-output *query-io
*)
256 (list (eval (read *query-io
*))))
258 ;;; Same as above but returns multiple values
259 (defun mv-read-evaluated-form (&optional
(prompt-control nil promptp
)
261 (apply #'format
*query-io
*
262 (if promptp prompt-control
"~&Type a form to be evaluated: ")
264 (finish-output *query-io
*)
265 (multiple-value-list (eval (read *query-io
*))))
267 (defun check-type-error (place place-value type
&optional type-string
)
274 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
275 :format-arguments
(list place place-value type-string type
))))
276 (restart-case (error condition
)
278 :report
(lambda (stream)
279 (format stream
"Supply a new value for ~S." place
))
280 :interactive read-evaluated-form
283 (defun etypecase-failure (value keys
)
284 (declare (optimize allow-non-returning-tail-call
))
288 :expected-type
`(or ,@keys
)
289 :possibilities keys
))
291 (defun ecase-failure (value keys
)
292 (declare (optimize allow-non-returning-tail-call
))
296 :expected-type
`(member ,@keys
)
297 :possibilities keys
))
299 (defun case-body-error (name keyform keyform-value expected-type keys
)
304 :expected-type expected-type
307 :report
(lambda (stream)
308 (format stream
"Supply a new value for ~S." keyform
))
309 :interactive read-evaluated-form