0.9.2.43:
[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 (defvar *handler-clusters* nil)
23
24 (defstruct (restart (:copier nil) (:predicate nil))
25   (name (missing-arg) :type symbol :read-only t)
26   (function (missing-arg) :type function)
27   (report-function nil :type (or null function))
28   (interactive-function nil :type (or null function))
29   (test-function (lambda (cond) (declare (ignore cond)) t) :type function))
30 (def!method print-object ((restart restart) stream)
31   (if *print-escape*
32       (print-unreadable-object (restart stream :type t :identity t)
33         (prin1 (restart-name restart) stream))
34       (restart-report restart stream)))
35
36 (defun compute-restarts (&optional condition)
37   #!+sb-doc
38   "Return a list of all the currently active restarts ordered from most
39    recently established to less recently established. If CONDITION is
40    specified, then only restarts associated with CONDITION (or with no
41    condition) will be returned."
42   (let ((associated ())
43         (other ()))
44     (dolist (alist *condition-restarts*)
45       (if (eq (car alist) condition)
46           (setq associated (cdr alist))
47           (setq other (append (cdr alist) other))))
48     (collect ((res))
49       (dolist (restart-cluster *restart-clusters*)
50         (dolist (restart restart-cluster)
51           (when (and (or (not condition)
52                          (member restart associated)
53                          (not (member restart other)))
54                      (funcall (restart-test-function restart)
55                               condition))
56             (res restart))))
57       (res))))
58
59 #!+sb-doc
60 (setf (fdocumentation 'restart-name 'function)
61       "Return the name of the given restart object.")
62
63 (defun restart-report (restart stream)
64   (funcall (or (restart-report-function restart)
65                (let ((name (restart-name restart)))
66                  (lambda (stream)
67                    (if name (format stream "~S" name)
68                        (format stream "~S" restart)))))
69            stream))
70
71 (defun find-restart (name &optional condition)
72   #!+sb-doc
73   "Return the first restart named NAME. If NAME names a restart, the restart
74    is returned if it is currently active. If no such restart is found, NIL is
75    returned. It is an error to supply NIL as a name. If CONDITION is specified
76    and not NIL, then only restarts associated with that condition (or with no
77    condition) will be returned."
78   (let ((restarts (compute-restarts condition)))
79     (declare (type list restarts))
80     (find-if (lambda (x)
81                (or (eq x name)
82                    (eq (restart-name x) name)))
83              restarts)))
84
85 ;;; helper for the various functions which are ANSI-spec'ed to do
86 ;;; something with a restart or signal CONTROL-ERROR if there is none
87 (defun find-restart-or-control-error (identifier &optional condition)
88   (or (find-restart identifier condition)
89       (error 'simple-control-error
90              :format-control "No restart ~S is active~@[ for ~S~]."
91              :format-arguments (list identifier condition))))
92
93 (defun invoke-restart (restart &rest values)
94   #!+sb-doc
95   "Calls the function associated with the given restart, passing any given
96    arguments. If the argument restart is not a restart or a currently active
97    non-nil restart name, then a CONTROL-ERROR is signalled."
98   (/show "entering INVOKE-RESTART" restart)
99   (let ((real-restart (find-restart-or-control-error restart)))
100     (apply (restart-function real-restart) values)))
101
102 (defun interactive-restart-arguments (real-restart)
103   (let ((interactive-function (restart-interactive-function real-restart)))
104     (if interactive-function
105         (funcall interactive-function)
106         '())))
107
108 (defun invoke-restart-interactively (restart)
109   #!+sb-doc
110   "Calls the function associated with the given restart, prompting for any
111    necessary arguments. If the argument restart is not a restart or a
112    currently active non-NIL restart name, then a CONTROL-ERROR is signalled."
113   (let* ((real-restart (find-restart-or-control-error restart))
114          (args (interactive-restart-arguments real-restart)))
115     (apply (restart-function real-restart) args)))
116 \f
117 (defun assert-error (assertion places datum &rest arguments)
118   (let ((cond (if datum
119                 (coerce-to-condition datum
120                                                     arguments
121                                                     'simple-error
122                                                     'error)
123                 (make-condition 'simple-error
124                                 :format-control "The assertion ~S failed."
125                                 :format-arguments (list assertion)))))
126     (restart-case
127         (error cond)
128       (continue ()
129                 :report (lambda (stream)
130                           (format stream "Retry assertion")
131                           (if places
132                               (format stream
133                                       " with new value~P for ~{~S~^, ~}."
134                                       (length places)
135                                       places)
136                               (format stream ".")))
137                 nil))))
138
139 ;;; READ-EVALUATED-FORM is used as the interactive method for restart cases
140 ;;; setup by the Common Lisp "casing" (e.g., CCASE and CTYPECASE) macros
141 ;;; and by CHECK-TYPE.
142 (defun read-evaluated-form ()
143   (format *query-io* "~&Type a form to be evaluated:~%")
144   (list (eval (read *query-io*))))
145
146 (defun check-type-error (place place-value type type-string)
147   (let ((condition
148          (make-condition
149           'simple-type-error
150           :datum place-value
151           :expected-type type
152           :format-control
153           "The value of ~S is ~S, which is not ~:[of type ~S~;~:*~A~]."
154           :format-arguments (list place place-value type-string type))))
155     (restart-case (error condition)
156       (store-value (value)
157         :report (lambda (stream)
158                   (format stream "Supply a new value for ~S." place))
159         :interactive read-evaluated-form
160         value))))
161
162 (defun case-body-error (name keyform keyform-value expected-type keys)
163   (restart-case
164       (error 'case-failure
165              :name name
166              :datum keyform-value
167              :expected-type expected-type
168              :possibilities keys)
169     (store-value (value)
170       :report (lambda (stream)
171                 (format stream "Supply a new value for ~S." keyform))
172       :interactive read-evaluated-form
173       value)))