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