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)))
54 (funcall (restart-test-function restart)
60 (setf (fdocumentation 'restart-name 'function)
61 "Return the name of the given restart object.")
63 (defun restart-report (restart stream)
64 (funcall (or (restart-report-function restart)
65 (let ((name (restart-name restart)))
67 (if name (format stream "~S" name)
68 (format stream "~S" restart)))))
71 (defun find-restart (name &optional condition)
73 "Return the first restart named NAME. If NAME names a restart, the restart
74 is returned if it is currently active. If no such restart is found, NIL is
75 returned. It is an error to supply NIL as a name. If CONDITION is specified
76 and not NIL, then only restarts associated with that condition (or with no
77 condition) will be returned."
78 (let ((restarts (compute-restarts condition)))
79 (declare (type list restarts))
82 (eq (restart-name x) name)))
85 (defun find-restart-or-lose (restart-designator)
86 (let ((real-restart (find-restart restart-designator)))
88 (error 'simple-control-error
89 :format-control "Restart ~S is not active."
90 :format-arguments (list restart-designator)))
93 (defun invoke-restart (restart &rest values)
95 "Calls the function associated with the given restart, passing any given
96 arguments. If the argument restart is not a restart or a currently active
97 non-nil restart name, then a control-error is signalled."
98 (/show "entering INVOKE-RESTART" restart)
99 (let ((real-restart (find-restart-or-lose restart)))
100 (apply (restart-function real-restart) values)))
102 (defun interactive-restart-arguments (real-restart)
103 (let ((interactive-function (restart-interactive-function real-restart)))
104 (if interactive-function
105 (funcall interactive-function)
108 (defun invoke-restart-interactively (restart)
110 "Calls the function associated with the given restart, prompting for any
111 necessary arguments. If the argument restart is not a restart or a
112 currently active non-nil restart name, then a control-error is signalled."
113 (let* ((real-restart (find-restart-or-lose restart))
114 (args (interactive-restart-arguments real-restart)))
115 (apply (restart-function real-restart) args)))
117 (defun assert-error (assertion places datum &rest arguments)
118 (let ((cond (if datum
119 (coerce-to-condition datum
123 (make-condition 'simple-error
124 :format-control "The assertion ~S failed."
125 :format-arguments (list assertion)))))
129 :report (lambda (stream)
130 (format stream "Retry assertion")
133 " with new value~P for ~{~S~^, ~}."
136 (format stream ".")))
139 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
140 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
141 ;;; and by CHECK-TYPE.
142 (defun read-evaluated-form ()
143 (format *query-io* "~&Type a form to be evaluated:~%")
144 (list (eval (read *query-io*))))
146 (defun check-type-error (place place-value type type-string)
147 (let ((cond (if type-string
148 (make-condition 'simple-type-error
152 "The value of ~S is ~S, which is not ~A."
153 :format-arguments (list place
156 (make-condition 'simple-type-error
160 "The value of ~S is ~S, which is not of type ~S."
161 :format-arguments (list place
164 (restart-case (error cond)
166 :report (lambda (stream)
167 (format stream "Supply a new value for ~S." place))
168 :interactive read-evaluated-form
171 (defun case-body-error (name keyform keyform-value expected-type keys)
176 :expected-type expected-type
179 :report (lambda (stream)
180 (format stream "Supply a new value for ~S." keyform))
181 :interactive read-evaluated-form