be8c31901e0a5d51a4d7e34d0a9f4efb168f3172
[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 args-and-values places datum &rest arguments)
135   (let ((cond (if datum
136                   (coerce-to-condition
137                    datum arguments 'simple-error 'error)
138                   (make-condition
139                    'simple-error
140                    :format-control "~@<The assertion ~S failed~:[.~:; ~
141                                     with ~:*~{~{~S = ~S~}~^, ~}.~]~:@>"
142                    :format-arguments (list assertion args-and-values)))))
143     (restart-case
144         (error cond)
145       (continue ()
146         :report (lambda (stream)
147                   (format stream "Retry assertion")
148                   (if places
149                       (format stream " with new value~P for ~{~S~^, ~}."
150                               (length places) places)
151                       (format stream ".")))
152         nil))))
153
154 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
155 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
156 ;;; and by CHECK-TYPE.
157 (defun read-evaluated-form (&optional (prompt-control nil promptp)
158                             &rest prompt-args)
159   (apply #'format *query-io*
160          (if promptp prompt-control "~&Type a form to be evaluated: ")
161          prompt-args)
162   (finish-output *query-io*)
163   (list (eval (read *query-io*))))
164
165 (defun check-type-error (place place-value type type-string)
166   (let ((condition
167          (make-condition
168           'simple-type-error
169           :datum place-value
170           :expected-type type
171           :format-control
172           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
173           :format-arguments (list place place-value type-string type))))
174     (restart-case (error condition)
175       (store-value (value)
176         :report (lambda (stream)
177                   (format stream "Supply a new value for ~S." place))
178         :interactive read-evaluated-form
179         value))))
180
181 (defun case-failure (name value keys)
182   (error 'case-failure
183          :name name
184          :datum value
185          :expected-type (if (eq name 'ecase)
186                             `(member ,@keys)
187                             `(or ,@keys))
188          :possibilities keys))
189
190 (defun case-body-error (name keyform keyform-value expected-type keys)
191   (restart-case
192       (error 'case-failure
193              :name name
194              :datum keyform-value
195              :expected-type expected-type
196              :possibilities keys)
197     (store-value (value)
198       :report (lambda (stream)
199                 (format stream "Supply a new value for ~S." keyform))
200       :interactive read-evaluated-form
201       value)))