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