Make some RESTART slots read-only
[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
15 (defun muffle-warning-p (warning)
16   (declare (special *muffled-warnings*))
17   (typep warning *muffled-warnings*))
18
19 (defun initial-handler-clusters ()
20   `(((warning . ,#'(lambda (warning)
21                      (when (muffle-warning-p warning)
22                        (muffle-warning warning)))))))
23
24 ;;; an alist with elements of the form
25 ;;;
26 ;;;  (CONDITION . (HANDLER1 HANDLER2 ...))
27 ;;;
28 ;;; Recently established handlers are added at the beginning of the
29 ;;; list. Elements to the left of the alist take precedence over
30 ;;; elements to the right.
31 (defvar *handler-clusters* (initial-handler-clusters))
32
33 ;;; a list of lists of currently active RESTART instances. maintained
34 ;;; by RESTART-BIND.
35 (defvar *restart-clusters* '())
36
37 ;;; an ALIST with elements of the form
38 ;;;
39 ;;;   (CONDITION . (RESTART1 RESTART2 ...))
40 ;;;
41 ;;; which records the restarts currently associated with
42 ;;; conditions. maintained by WITH-CONDITION-RESTARTS.
43 (defvar *condition-restarts* ())
44
45 (defstruct (restart (:copier nil) (:predicate nil))
46   (name (missing-arg) :type symbol :read-only t)
47   (function (missing-arg) :type function :read-only t)
48   (report-function nil :type (or null function) :read-only t)
49   (interactive-function nil :type (or null function) :read-only t)
50   (test-function (lambda (cond) (declare (ignore cond)) t) :type function :read-only t))
51
52 (def!method print-object ((restart restart) stream)
53   (if *print-escape*
54       (print-unreadable-object (restart stream :type t :identity t)
55         (prin1 (restart-name restart) stream))
56       (restart-report restart stream)))
57
58 #!+sb-doc
59 (setf (fdocumentation 'restart-name 'function)
60       "Return the name of the given restart object.")
61
62 (defun restart-report (restart stream)
63   (funcall (or (restart-report-function restart)
64                (lambda (stream)
65                  (format stream "~S" (or (restart-name restart)
66                                          restart))))
67            stream))
68
69 (defvar *restart-test-stack* nil)
70
71 (defun compute-restarts (&optional condition)
72   #!+sb-doc
73   "Return a list of all the currently active restarts ordered from most recently
74 established to less recently established. If CONDITION is specified, then only
75 restarts associated with CONDITION (or with no condition) will be returned."
76   (let ((associated ())
77         (other ()))
78     (dolist (alist *condition-restarts*)
79       (if (eq (car alist) condition)
80           (setq associated (cdr alist))
81           (setq other (append (cdr alist) other))))
82     (collect ((res))
83       (let ((stack *restart-test-stack*))
84         (dolist (restart-cluster *restart-clusters*)
85           (dolist (restart restart-cluster)
86             (when (and (or (not condition)
87                            (memq restart associated)
88                            (not (memq restart other)))
89                        ;; A call to COMPUTE-RESTARTS -- from an error, from
90                        ;; user code, whatever -- inside the test function
91                        ;; would cause infinite recursion here, so we disable
92                        ;; each restart using *restart-test-stack* for the
93                        ;; duraction of the test call.
94                        (not (memq restart stack))
95                        (let ((*restart-test-stack* (cons restart stack)))
96                          (declare (truly-dynamic-extent *restart-test-stack*))
97                          (funcall (restart-test-function restart) condition)))
98              (res restart)))))
99       (res))))
100
101 (defun find-restart (identifier &optional condition)
102   #!+sb-doc
103   "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
104 then the innermost applicable restart with that name is returned. If IDENTIFIER
105 is a restart, it is returned if it is currently active. Otherwise NIL is
106 returned. If CONDITION is specified and not NIL, then only restarts associated
107 with that condition (or with no condition) will be returned."
108   ;; see comment above
109   (if (typep identifier 'restart)
110       (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
111            identifier)
112       (find identifier (compute-restarts condition) :key #'restart-name)))
113
114 ;;; helper for the various functions which are ANSI-spec'ed to do
115 ;;; something with a restart or signal CONTROL-ERROR if there is none
116 (defun find-restart-or-control-error (identifier &optional condition)
117   (or (find-restart identifier condition)
118       (error 'simple-control-error
119              :format-control "No restart ~S is active~@[ for ~S~]."
120              :format-arguments (list identifier condition))))
121
122 (defun invoke-restart (restart &rest values)
123   #!+sb-doc
124   "Calls the function associated with the given restart, passing any given
125    arguments. If the argument restart is not a restart or a currently active
126    non-nil restart name, then a CONTROL-ERROR is signalled."
127   (/show "entering INVOKE-RESTART" restart)
128   (let ((real-restart (find-restart-or-control-error restart)))
129     (apply (restart-function real-restart) values)))
130
131 (defun interactive-restart-arguments (real-restart)
132   (let ((interactive-function (restart-interactive-function real-restart)))
133     (if interactive-function
134         (funcall interactive-function)
135         '())))
136
137 (defun invoke-restart-interactively (restart)
138   #!+sb-doc
139   "Calls the function associated with the given restart, prompting for any
140    necessary arguments. If the argument restart is not a restart or a
141    currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
142   (let* ((real-restart (find-restart-or-control-error restart))
143          (args (interactive-restart-arguments real-restart)))
144     (apply (restart-function real-restart) args)))
145 \f
146 (defun assert-error (assertion args-and-values places datum &rest arguments)
147   (let ((cond (if datum
148                   (coerce-to-condition
149                    datum arguments 'simple-error 'error)
150                   (make-condition
151                    'simple-error
152                    :format-control "~@<The assertion ~S failed~:[.~:; ~
153                                     with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
154                    :format-arguments (list assertion args-and-values)))))
155     (restart-case
156         (error cond)
157       (continue ()
158         :report (lambda (stream)
159                   (format stream "Retry assertion")
160                   (if places
161                       (format stream " with new value~P for ~{~S~^, ~}."
162                               (length places) places)
163                       (format stream ".")))
164         nil))))
165
166 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
167 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
168 ;;; and by CHECK-TYPE.
169 (defun read-evaluated-form (&optional (prompt-control nil promptp)
170                             &rest prompt-args)
171   (apply #'format *query-io*
172          (if promptp prompt-control "~&Type a form to be evaluated: ")
173          prompt-args)
174   (finish-output *query-io*)
175   (list (eval (read *query-io*))))
176
177 (defun check-type-error (place place-value type type-string)
178   (let ((condition
179          (make-condition
180           'simple-type-error
181           :datum place-value
182           :expected-type type
183           :format-control
184           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
185           :format-arguments (list place place-value type-string type))))
186     (restart-case (error condition)
187       (store-value (value)
188         :report (lambda (stream)
189                   (format stream "Supply a new value for ~S." place))
190         :interactive read-evaluated-form
191         value))))
192
193 (defun case-failure (name value keys)
194   (error 'case-failure
195          :name name
196          :datum value
197          :expected-type (if (eq name 'ecase)
198                             `(member ,@keys)
199                             `(or ,@keys))
200          :possibilities keys))
201
202 (defun case-body-error (name keyform keyform-value expected-type keys)
203   (restart-case
204       (error 'case-failure
205              :name name
206              :datum keyform-value
207              :expected-type expected-type
208              :possibilities keys)
209     (store-value (value)
210       :report (lambda (stream)
211                 (format stream "Supply a new value for ~S." keyform))
212       :interactive read-evaluated-form
213       value)))