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 ;;; a list of lists of restarts
16 (defvar *restart-clusters* '())
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts* ())
22 (defun muffle-warning-p (warning)
23 (declare (special *muffled-warnings*))
24 (typep warning *muffled-warnings*))
26 (defun initial-handler-clusters ()
27 `(((warning . ,#'(lambda (warning)
28 (when (muffle-warning-p warning)
29 (muffle-warning warning)))))))
31 (defvar *handler-clusters* (initial-handler-clusters))
33 (defstruct (restart (:copier nil) (:predicate nil))
34 (name (missing-arg) :type symbol :read-only t)
35 (function (missing-arg) :type function)
36 (report-function nil :type (or null function))
37 (interactive-function nil :type (or null function))
38 (test-function (lambda (cond) (declare (ignore cond)) t) :type function))
39 (def!method print-object ((restart restart) stream)
41 (print-unreadable-object (restart stream :type t :identity t)
42 (prin1 (restart-name restart) stream))
43 (restart-report restart stream)))
45 (defvar *restart-test-stack* nil)
47 (defun compute-restarts (&optional condition)
49 "Return a list of all the currently active restarts ordered from most recently
50 established to less recently established. If CONDITION is specified, then only
51 restarts associated with CONDITION (or with no condition) will be returned."
54 (dolist (alist *condition-restarts*)
55 (if (eq (car alist) condition)
56 (setq associated (cdr alist))
57 (setq other (append (cdr alist) other))))
59 (let ((stack *restart-test-stack*))
60 (dolist (restart-cluster *restart-clusters*)
61 (dolist (restart restart-cluster)
62 (when (and (or (not condition)
63 (memq restart associated)
64 (not (memq restart other)))
65 ;; A call to COMPUTE-RESTARTS -- from an error, from
66 ;; user code, whatever -- inside the test function
67 ;; would cause infinite recursion here, so we disable
68 ;; each restart using *restart-test-stack* for the
69 ;; duraction of the test call.
70 (not (memq restart stack))
71 (let ((*restart-test-stack* (cons restart stack)))
72 (declare (truly-dynamic-extent *restart-test-stack*))
73 (funcall (restart-test-function restart) condition)))
78 (setf (fdocumentation 'restart-name 'function)
79 "Return the name of the given restart object.")
81 (defun restart-report (restart stream)
82 (funcall (or (restart-report-function restart)
83 (let ((name (restart-name restart)))
85 (if name (format stream "~S" name)
86 (format stream "~S" restart)))))
89 (defun find-restart (identifier &optional condition)
91 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
92 then the innermost applicable restart with that name is returned. If IDENTIFIER
93 is a restart, it is returned if it is currently active. Otherwise NIL is
94 returned. If CONDITION is specified and not NIL, then only restarts associated
95 with that condition (or with no condition) will be returned."
97 (if (typep identifier 'restart)
98 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
100 (find identifier (compute-restarts condition) :key #'restart-name)))
102 ;;; helper for the various functions which are ANSI-spec'ed to do
103 ;;; something with a restart or signal CONTROL-ERROR if there is none
104 (defun find-restart-or-control-error (identifier &optional condition)
105 (or (find-restart identifier condition)
106 (error 'simple-control-error
107 :format-control "No restart ~S is active~@[ for ~S~]."
108 :format-arguments (list identifier condition))))
110 (defun invoke-restart (restart &rest values)
112 "Calls the function associated with the given restart, passing any given
113 arguments. If the argument restart is not a restart or a currently active
114 non-nil restart name, then a CONTROL-ERROR is signalled."
115 (/show "entering INVOKE-RESTART" restart)
116 (let ((real-restart (find-restart-or-control-error restart)))
117 (apply (restart-function real-restart) values)))
119 (defun interactive-restart-arguments (real-restart)
120 (let ((interactive-function (restart-interactive-function real-restart)))
121 (if interactive-function
122 (funcall interactive-function)
125 (defun invoke-restart-interactively (restart)
127 "Calls the function associated with the given restart, prompting for any
128 necessary arguments. If the argument restart is not a restart or a
129 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
130 (let* ((real-restart (find-restart-or-control-error restart))
131 (args (interactive-restart-arguments real-restart)))
132 (apply (restart-function real-restart) args)))
134 (defun assert-error (assertion places datum &rest arguments)
135 (let ((cond (if datum
136 (coerce-to-condition datum
140 (make-condition 'simple-error
141 :format-control "The assertion ~S failed."
142 :format-arguments (list assertion)))))
146 :report (lambda (stream)
147 (format stream "Retry assertion")
150 " with new value~P for ~{~S~^, ~}."
153 (format stream ".")))
156 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
157 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
158 ;;; and by CHECK-TYPE.
159 (defun read-evaluated-form ()
160 (format *query-io* "~&Type a form to be evaluated:~%")
161 (list (eval (read *query-io*))))
163 (defun check-type-error (place place-value type type-string)
170 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
171 :format-arguments (list place place-value type-string type))))
172 (restart-case (error condition)
174 :report (lambda (stream)
175 (format stream "Supply a new value for ~S." place))
176 :interactive read-evaluated-form
179 (defun case-failure (name value keys)
183 :expected-type (if (eq name 'ecase)
186 :possibilities keys))
188 (defun case-body-error (name keyform keyform-value expected-type keys)
193 :expected-type expected-type
196 :report (lambda (stream)
197 (format stream "Supply a new value for ~S." keyform))
198 :interactive read-evaluated-form