Use SB!IMPL as the implementation package for PARSE-BODY
[sbcl.git] / src / code / target-error.lisp
blob635c72e19adab752d8a23a2dbc144675e99e3e36
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
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.
13 (in-package "SB!KERNEL")
15 (defun muffle-warning-p (warning)
16 (declare (special *muffled-warnings*))
17 (typep warning *muffled-warnings*))
19 ;; Host lisp does not need a value for this, so start it out as NIL.
20 (defglobal **initial-handler-clusters** nil)
21 (setq **initial-handler-clusters**
22 `(((,(lambda (condition)
23 (typep condition 'warning))
25 ,(lambda (warning)
26 (when (muffle-warning-p warning)
27 (muffle-warning warning)))))))
29 ;;; Each cluster is an alist of the form
30 ;;;
31 ;;; ((TYPE-TEST1 . HANDLER1) (TYPE-TEST2 . HANDLER2) ...)
32 ;;;
33 ;;; where TYPE-TESTN are functions of one argument which test a given
34 ;;; condition instance for the type required by the corresponding
35 ;;; HANDLERN. HANDLERN are function designators.
36 ;;;
37 ;;; Newly established handlers are added at the beginning of the
38 ;;; list. Elements to the left of the alist take precedence over
39 ;;; elements to the right.
40 ;;;
41 ;;; Lists to which *HANDLER-CLUSTERS* is bound generally have dynamic
42 ;;; extent.
43 (defvar *handler-clusters* **initial-handler-clusters**)
45 ;;; a list of lists of currently active RESTART instances. maintained
46 ;;; by RESTART-BIND.
47 ;;; This variable is proclaimed to be "eventually" always-bound,
48 ;;; meaning in the loaded code. If DEFVAR were used, the compiler knows that
49 ;;; BOUNDP will be T, and therefore a code deletion note should be issused
50 ;;; for the initialization expression (UNLESS (BOUNDP x) <initform>).
51 ;;; Technically it's not even right to use DEFVAR because it works only
52 ;;; if the initializer is really NIL, since that is what %DEFVAR will assign
53 ;;; on account of the fact that an initializer was supplied at all.
54 (defparameter *restart-clusters* '())
56 (def!method print-object ((restart restart) stream)
57 (if *print-escape*
58 (print-unreadable-object (restart stream :type t :identity t)
59 (prin1 (restart-name restart) stream))
60 (restart-report restart stream)))
62 #!+sb-doc
63 (setf (fdocumentation 'restart-name 'function)
64 "Return the name of the given restart object.")
66 (defun restart-report (restart stream)
67 (if (restart-report-function restart)
68 (funcall (truly-the function (restart-report-function restart))
69 stream)
70 (prin1 (or (restart-name restart)
71 restart)
72 stream)))
74 (defvar *restart-test-stack* nil)
76 ;; Call FUNCTION with all restarts in the current dynamic environment,
77 ;; 1) that are associated to CONDITION (when CONDITION is NIL, all
78 ;; restarts are processed)
79 ;; 2) and for which the restart test returns non-NIL for CONDITION.
80 ;; When CALL-TEST-P is non-NIL, all restarts are processed.
81 (defun map-restarts (function &optional condition (call-test-p t))
82 (declare (function function))
83 (let ((stack *restart-test-stack*))
84 (dolist (restart-cluster *restart-clusters*)
85 (dolist (restart restart-cluster)
86 (when (and (or (not condition)
87 (null (restart-associated-conditions restart))
88 (memq condition (restart-associated-conditions restart)))
89 ;; A call to COMPUTE-RESTARTS -- from an error,
90 ;; from user code, whatever -- inside the test
91 ;; function would cause infinite recursion here, so
92 ;; we disable each restart using
93 ;; *restart-test-stack* for the duration of the
94 ;; test call.
95 (not (memq restart stack))
96 (or (not call-test-p)
97 (let ((*restart-test-stack* (cons restart stack)))
98 (declare (truly-dynamic-extent *restart-test-stack*))
99 (funcall (restart-test-function restart) condition))))
100 (funcall function restart))))))
102 (defun compute-restarts (&optional condition)
103 #!+sb-doc
104 "Return a list of all the currently active restarts ordered from most recently
105 established to less recently established. If CONDITION is specified, then only
106 restarts associated with CONDITION (or with no condition) will be returned."
107 (collect ((result))
108 (map-restarts (lambda (restart) (result restart)) condition)
109 (result)))
111 (defun %find-restart (identifier condition &optional (call-test-p t))
112 (flet ((eq-restart-p (restart)
113 (when (eq identifier restart)
114 (return-from %find-restart restart)))
115 (named-restart-p (restart)
116 (when (eq identifier (restart-name restart))
117 (return-from %find-restart restart))))
118 ;; KLUDGE: can the compiler infer this dx automatically?
119 (declare (truly-dynamic-extent #'eq-restart-p #'named-restart-p))
120 (if (typep identifier 'restart)
121 ;; The code under #+previous-... below breaks the abstraction
122 ;; introduced by MAP-RESTARTS, but is about twice as
123 ;; fast as #+equivalent-... . Also, it is a common case due to
125 ;; (INVOKE-RESTART RESTART)
126 ;; -> (FIND-RESTART-OR-CONTROL-ERROR RESTART)
127 ;; -> (FIND-RESTART RESTART)
129 ;; However, both #+previous-... and #+equivalent-... may be
130 ;; wrong altogether because of
131 ;; https://bugs.launchpad.net/sbcl/+bug/774410:
132 ;; The behavior expected in that report can be achieved by the
133 ;; following line (which is, of course, the slowest of all
134 ;; possibilities):
135 (map-restarts #'eq-restart-p condition call-test-p)
137 #+equivalent-to-previous-sbcl-behavior--faster-but-see-bug-774410
138 (map-restarts #'eq-restart-p nil nil)
140 #+previous-behavior--fastest-but-see-bug-774410
141 (and (find-if (lambda (cluster) (find identifier cluster)) *restart-clusters*)
142 identifier)
144 (map-restarts #'named-restart-p condition call-test-p))))
146 (defun find-restart (identifier &optional condition)
147 #!+sb-doc
148 "Return the first restart identified by IDENTIFIER. If IDENTIFIER is a symbol,
149 then the innermost applicable restart with that name is returned. If IDENTIFIER
150 is a restart, it is returned if it is currently active. Otherwise NIL is
151 returned. If CONDITION is specified and not NIL, then only restarts associated
152 with that condition (or with no condition) will be returned."
153 ;; Calls MAP-RESTARTS such that restart test functions are
154 ;; respected.
155 (%find-restart identifier condition))
157 ;;; helper for the various functions which are ANSI-spec'ed to do
158 ;;; something with a restart or signal CONTROL-ERROR if there is none
159 (defun find-restart-or-control-error (identifier &optional condition (call-test-p t))
160 (or (%find-restart identifier condition call-test-p)
161 (error 'simple-control-error
162 :format-control "No restart ~S is active~@[ for ~S~]."
163 :format-arguments (list identifier condition))))
165 (defun invoke-restart (restart &rest values)
166 #!+sb-doc
167 "Calls the function associated with the given restart, passing any given
168 arguments. If the argument restart is not a restart or a currently active
169 non-nil restart name, then a CONTROL-ERROR is signalled."
170 (/show "entering INVOKE-RESTART" restart)
171 ;; The following code calls MAP-RESTARTS (through
172 ;; FIND-RESTART-OR-CONTROL-ERROR -> %FIND-RESTART) such that restart
173 ;; test functions are respected when RESTART is a symbol, but not
174 ;; when RESTART is a RESTART instance.
176 ;; Without disabling test functions for the RESTART instance case,
177 ;; the following problem would arise:
179 ;; (restart-case
180 ;; (handler-bind
181 ;; ((some-condition (lambda (c)
182 ;; (invoke-restart (find-restart 'foo c)) ; a)
183 ;; (invoke-restart 'foo) ; b)
184 ;; )))
185 ;; (signal 'some-condition))
186 ;; (foo ()
187 ;; :test (lambda (c) (typep c 'some-condition))))
189 ;; In case a), INVOKE-RESTART receives the RESTART instance, but
190 ;; cannot supply the condition instance needed by the test. In case
191 ;; b) INVOKE-RESTART calls FIND-RESTART, but again cannot supply the
192 ;; condition instance. As a result, the restart would be impossible
193 ;; the invoke.
194 (let ((real-restart (find-restart-or-control-error
195 restart nil (symbolp restart))))
196 (apply (restart-function real-restart) values)))
198 (defun interactive-restart-arguments (real-restart)
199 (let ((interactive-function (restart-interactive-function real-restart)))
200 (if interactive-function
201 (funcall interactive-function)
202 '())))
204 (defun invoke-restart-interactively (restart)
205 #!+sb-doc
206 "Calls the function associated with the given restart, prompting for any
207 necessary arguments. If the argument restart is not a restart or a
208 currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
209 ;; For an explanation of the call to FIND-RESTART-OR-CONTROL-ERROR,
210 ;; see comment in INVOKE-RESTART.
211 (let* ((real-restart (find-restart-or-control-error
212 restart nil (symbolp restart)))
213 (args (interactive-restart-arguments real-restart)))
214 (apply (restart-function real-restart) args)))
217 (defun assert-error (assertion args-and-values places datum &rest arguments)
218 (let ((cond (if datum
219 (coerce-to-condition
220 datum arguments 'simple-error 'error)
221 (make-condition
222 'simple-error
223 :format-control "~@<The assertion ~S failed~:[.~:; ~
224 with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
225 :format-arguments (list assertion args-and-values)))))
226 (restart-case
227 (error cond)
228 (continue ()
229 :report (lambda (stream)
230 (format stream "Retry assertion")
231 (if places
232 (format stream " with new value~P for ~{~S~^, ~}."
233 (length places) places)
234 (format stream ".")))
235 nil))))
237 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
238 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
239 ;;; and by CHECK-TYPE.
240 (defun read-evaluated-form (&optional (prompt-control nil promptp)
241 &rest prompt-args)
242 (apply #'format *query-io*
243 (if promptp prompt-control "~&Type a form to be evaluated: ")
244 prompt-args)
245 (finish-output *query-io*)
246 (list (eval (read *query-io*))))
248 (defun check-type-error (place place-value type type-string)
249 (let ((condition
250 (make-condition
251 'simple-type-error
252 :datum place-value
253 :expected-type type
254 :format-control
255 "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
256 :format-arguments (list place place-value type-string type))))
257 (restart-case (error condition)
258 (store-value (value)
259 :report (lambda (stream)
260 (format stream "Supply a new value for ~S." place))
261 :interactive read-evaluated-form
262 value))))
264 (defun case-failure (name value keys)
265 (error 'case-failure
266 :name name
267 :datum value
268 :expected-type (if (eq name 'ecase)
269 `(member ,@keys)
270 `(or ,@keys))
271 :possibilities keys))
273 (defun case-body-error (name keyform keyform-value expected-type keys)
274 (restart-case
275 (error 'case-failure
276 :name name
277 :datum keyform-value
278 :expected-type expected-type
279 :possibilities keys)
280 (store-value (value)
281 :report (lambda (stream)
282 (format stream "Supply a new value for ~S." keyform))
283 :interactive read-evaluated-form
284 value)))