0.9.5.83: almost fix COMPUTE-RESTARTS & FIND-RESTART
[sbcl.git] / src / code / target-error.lisp
1 ;;;; that part of the condition system which can or should come early
2 ;;;; (mostly macro-related)
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!KERNEL")
14 \f
15 ;;; a list of lists of restarts
16 (defvar *restart-clusters* '())
17
18 ;;; an ALIST (condition . restarts) which records the restarts currently
19 ;;; associated with Condition
20 (defvar *condition-restarts* ())
21
22 (defvar *handler-clusters* nil)
23
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)
31   (if *print-escape*
32       (print-unreadable-object (restart stream :type t :identity t)
33         (prin1 (restart-name restart) stream))
34       (restart-report restart stream)))
35
36 (defun compute-restarts (&optional condition)
37   #!+sb-doc
38   "Return a list of all the currently active restarts ordered from most recently
39 established to less recently established. If CONDITION is specified, then only
40 restarts associated with CONDITION (or with no condition) will be returned."
41   (let ((associated ())
42         (other ()))
43     (dolist (alist *condition-restarts*)
44       (if (eq (car alist) condition)
45           (setq associated (cdr alist))
46           (setq other (append (cdr alist) other))))
47     (collect ((res))
48       (dolist (restart-cluster *restart-clusters*)
49         (dolist (restart restart-cluster)
50           (when (and (or (not condition)
51                          (member restart associated)
52                          (not (member restart other)))
53                      (funcall (restart-test-function restart) condition))
54             (res restart))))
55       (res))))
56
57 #!+sb-doc
58 (setf (fdocumentation 'restart-name 'function)
59       "Return the name of the given restart object.")
60
61 (defun restart-report (restart stream)
62   (funcall (or (restart-report-function restart)
63                (let ((name (restart-name restart)))
64                  (lambda (stream)
65                    (if name (format stream "~S" name)
66                        (format stream "~S" restart)))))
67            stream))
68
69 (defun find-restart (identifier &optional condition)
70   #!+sb-doc
71   "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
72 then the innermost applicable restart with that name is returned. If IDENTIFIER
73 is a restart, it is returned if it is currently active. Otherwise NIL is
74 returned. If CONDITION is specified and not NIL, then only restarts associated
75 with that condition (or with no condition) will be returned."
76   ;; see comment above
77   (if (typep identifier 'restart)
78       (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
79            identifier)
80       (find identifier (compute-restarts condition) :key #'restart-name)))
81
82 ;;; helper for the various functions which are ANSI-spec'ed to do
83 ;;; something with a restart or signal CONTROL-ERROR if there is none
84 (defun find-restart-or-control-error (identifier &optional condition)
85   (or (find-restart identifier condition)
86       (error 'simple-control-error
87              :format-control "No restart ~S is active~@[ for ~S~]."
88              :format-arguments (list identifier condition))))
89
90 (defun invoke-restart (restart &rest values)
91   #!+sb-doc
92   "Calls the function associated with the given restart, passing any given
93    arguments. If the argument restart is not a restart or a currently active
94    non-nil restart name, then a CONTROL-ERROR is signalled."
95   (/show "entering INVOKE-RESTART" restart)
96   (let ((real-restart (find-restart-or-control-error restart)))
97     (apply (restart-function real-restart) values)))
98
99 (defun interactive-restart-arguments (real-restart)
100   (let ((interactive-function (restart-interactive-function real-restart)))
101     (if interactive-function
102         (funcall interactive-function)
103         '())))
104
105 (defun invoke-restart-interactively (restart)
106   #!+sb-doc
107   "Calls the function associated with the given restart, prompting for any
108    necessary arguments. If the argument restart is not a restart or a
109    currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
110   (let* ((real-restart (find-restart-or-control-error restart))
111          (args (interactive-restart-arguments real-restart)))
112     (apply (restart-function real-restart) args)))
113 \f
114 (defun assert-error (assertion places datum &rest arguments)
115   (let ((cond (if datum
116                 (coerce-to-condition datum
117                                                     arguments
118                                                     'simple-error
119                                                     'error)
120                 (make-condition 'simple-error
121                                 :format-control "The assertion ~S failed."
122                                 :format-arguments (list assertion)))))
123     (restart-case
124         (error cond)
125       (continue ()
126                 :report (lambda (stream)
127                           (format stream "Retry assertion")
128                           (if places
129                               (format stream
130                                       " with new value~P for ~{~S~^, ~}."
131                                       (length places)
132                                       places)
133                               (format stream ".")))
134                 nil))))
135
136 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
137 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
138 ;;; and by CHECK-TYPE.
139 (defun read-evaluated-form ()
140   (format *query-io* "~&Type a form to be evaluated:~%")
141   (list (eval (read *query-io*))))
142
143 (defun check-type-error (place place-value type type-string)
144   (let ((condition
145          (make-condition
146           'simple-type-error
147           :datum place-value
148           :expected-type type
149           :format-control
150           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
151           :format-arguments (list place place-value type-string type))))
152     (restart-case (error condition)
153       (store-value (value)
154         :report (lambda (stream)
155                   (format stream "Supply a new value for ~S." place))
156         :interactive read-evaluated-form
157         value))))
158
159 (defun case-body-error (name keyform keyform-value expected-type keys)
160   (restart-case
161       (error 'case-failure
162              :name name
163              :datum keyform-value
164              :expected-type expected-type
165              :possibilities keys)
166     (store-value (value)
167       :report (lambda (stream)
168                 (format stream "Supply a new value for ~S." keyform))
169       :interactive read-evaluated-form
170       value)))