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 (defvar *handler-clusters* nil)
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)
32 (print-unreadable-object (restart stream :type t :identity t)
33 (prin1 (restart-name restart) stream))
34 (restart-report restart stream)))
36 (defun compute-restarts (&optional condition)
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."
44 (dolist (alist *condition-restarts*)
45 (if (eq (car alist) condition)
46 (setq associated (cdr alist))
47 (setq other (append (cdr alist) other))))
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)))
55 (funcall (restart-test-function restart)
61 (setf (fdocumentation 'restart-name 'function)
62 "Return the name of the given restart object.")
64 (defun restart-report (restart stream)
65 (funcall (or (restart-report-function restart)
66 (let ((name (restart-name restart)))
68 (if name (format stream "~S" name)
69 (format stream "~S" restart)))))
72 (defun find-restart (name &optional condition)
74 "Return the first restart named NAME. If NAME names a restart, the restart
75 is returned if it is currently active. If no such restart is found, NIL is
76 returned. It is an error to supply NIL as a name. If CONDITION is specified
77 and not NIL, then only restarts associated with that condition (or with no
78 condition) will be returned."
79 (let ((restarts (compute-restarts condition)))
80 (declare (type list restarts))
83 (eq (restart-name x) name)))
86 ;;; helper for the various functions which are ANSI-spec'ed to do
87 ;;; something with a restart or signal CONTROL-ERROR if there is none
88 (defun find-restart-or-control-error (identifier &optional condition)
89 (or (find-restart identifier condition)
90 (error 'simple-control-error
91 :format-control "No restart ~S is active~@[ for ~S~]."
92 :format-arguments (list identifier condition))))
94 (defun invoke-restart (restart &rest values)
96 "Calls the function associated with the given restart, passing any given
97 arguments. If the argument restart is not a restart or a currently active
98 non-nil restart name, then a CONTROL-ERROR is signalled."
99 (/show "entering INVOKE-RESTART" restart)
100 (let ((real-restart (find-restart-or-control-error restart)))
101 (apply (restart-function real-restart) values)))
103 (defun interactive-restart-arguments (real-restart)
104 (let ((interactive-function (restart-interactive-function real-restart)))
105 (if interactive-function
106 (funcall interactive-function)
109 (defun invoke-restart-interactively (restart)
111 "Calls the function associated with the given restart, prompting for any
112 necessary arguments. If the argument restart is not a restart or a
113 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
114 (let* ((real-restart (find-restart-or-control-error restart))
115 (args (interactive-restart-arguments real-restart)))
116 (apply (restart-function real-restart) args)))
118 (defun assert-error (assertion places datum &rest arguments)
119 (let ((cond (if datum
120 (coerce-to-condition datum
124 (make-condition 'simple-error
125 :format-control "The assertion ~S failed."
126 :format-arguments (list assertion)))))
130 :report (lambda (stream)
131 (format stream "Retry assertion")
134 " with new value~P for ~{~S~^, ~}."
137 (format stream ".")))
140 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
141 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
142 ;;; and by CHECK-TYPE.
143 (defun read-evaluated-form ()
144 (format *query-io* "~&Type a form to be evaluated:~%")
145 (list (eval (read *query-io*))))
147 (defun check-type-error (place place-value type type-string)
154 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
155 :format-arguments (list place place-value type-string type))))
156 (restart-case (error condition)
158 :report (lambda (stream)
159 (format stream "Supply a new value for ~S." place))
160 :interactive read-evaluated-form
163 (defun case-body-error (name keyform keyform-value expected-type keys)
168 :expected-type expected-type
171 :report (lambda (stream)
172 (format stream "Supply a new value for ~S." keyform))
173 :interactive read-evaluated-form