we have read-evaluated-form, kill read-replacement-character and -string
[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 (defun muffle-warning-p (warning)
23   (declare (special *muffled-warnings*))
24   (typep warning *muffled-warnings*))
25
26 (defun initial-handler-clusters ()
27   `(((warning . ,#'(lambda (warning)
28                      (when (muffle-warning-p warning)
29                        (muffle-warning warning)))))))
30
31 (defvar *handler-clusters* (initial-handler-clusters))
32
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)
40   (if *print-escape*
41       (print-unreadable-object (restart stream :type t :identity t)
42         (prin1 (restart-name restart) stream))
43       (restart-report restart stream)))
44
45 (defvar *restart-test-stack* nil)
46
47 (defun compute-restarts (&optional condition)
48   #!+sb-doc
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."
52   (let ((associated ())
53         (other ()))
54     (dolist (alist *condition-restarts*)
55       (if (eq (car alist) condition)
56           (setq associated (cdr alist))
57           (setq other (append (cdr alist) other))))
58     (collect ((res))
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)))
74              (res restart)))))
75       (res))))
76
77 #!+sb-doc
78 (setf (fdocumentation 'restart-name 'function)
79       "Return the name of the given restart object.")
80
81 (defun restart-report (restart stream)
82   (funcall (or (restart-report-function restart)
83                (let ((name (restart-name restart)))
84                  (lambda (stream)
85                    (if name (format stream "~S" name)
86                        (format stream "~S" restart)))))
87            stream))
88
89 (defun find-restart (identifier &optional condition)
90   #!+sb-doc
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."
96   ;; see comment above
97   (if (typep identifier 'restart)
98       (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
99            identifier)
100       (find identifier (compute-restarts condition) :key #'restart-name)))
101
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))))
109
110 (defun invoke-restart (restart &rest values)
111   #!+sb-doc
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)))
118
119 (defun interactive-restart-arguments (real-restart)
120   (let ((interactive-function (restart-interactive-function real-restart)))
121     (if interactive-function
122         (funcall interactive-function)
123         '())))
124
125 (defun invoke-restart-interactively (restart)
126   #!+sb-doc
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)))
133 \f
134 (defun assert-error (assertion places datum &rest arguments)
135   (let ((cond (if datum
136                 (coerce-to-condition datum
137                                                     arguments
138                                                     'simple-error
139                                                     'error)
140                 (make-condition 'simple-error
141                                 :format-control "The assertion ~S failed."
142                                 :format-arguments (list assertion)))))
143     (restart-case
144         (error cond)
145       (continue ()
146                 :report (lambda (stream)
147                           (format stream "Retry assertion")
148                           (if places
149                               (format stream
150                                       " with new value~P for ~{~S~^, ~}."
151                                       (length places)
152                                       places)
153                               (format stream ".")))
154                 nil))))
155
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 (&optional (prompt-control nil promptp)
160                             &rest prompt-args)
161   (apply #'format *query-io*
162          (if promptp prompt-control "~&Type a form to be evaluated: ")
163          prompt-args)
164   (finish-output *query-io*)
165   (list (eval (read *query-io*))))
166
167 (defun check-type-error (place place-value type type-string)
168   (let ((condition
169          (make-condition
170           'simple-type-error
171           :datum place-value
172           :expected-type type
173           :format-control
174           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
175           :format-arguments (list place place-value type-string type))))
176     (restart-case (error condition)
177       (store-value (value)
178         :report (lambda (stream)
179                   (format stream "Supply a new value for ~S." place))
180         :interactive read-evaluated-form
181         value))))
182
183 (defun case-failure (name value keys)
184   (error 'case-failure
185          :name name
186          :datum value
187          :expected-type (if (eq name 'ecase)
188                             `(member ,@keys)
189                             `(or ,@keys))
190          :possibilities keys))
191
192 (defun case-body-error (name keyform keyform-value expected-type keys)
193   (restart-case
194       (error 'case-failure
195              :name name
196              :datum keyform-value
197              :expected-type expected-type
198              :possibilities keys)
199     (store-value (value)
200       :report (lambda (stream)
201                 (format stream "Supply a new value for ~S." keyform))
202       :interactive read-evaluated-form
203       value)))