| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: CLOS -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2001, Juan Jose Garcia-Ripoll |
| 6 |
;;;; Copyright (c) 1992, Giuseppe Attardi. |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
;;; |
| 12 |
;;; conditions.lsp |
| 13 |
;;; |
| 14 |
;;; Originally written by Kent M. Pitman of Symbolics, Inc. and |
| 15 |
;;; distributed without any copyright. |
| 16 |
;;; This is Version 18. |
| 17 |
;;; |
| 18 |
;;; KMP's disclaimer: |
| 19 |
;;; |
| 20 |
;;; This is a sample implementation. It is not in any way intended as the |
| 21 |
;;; definition of any aspect of the condition system. It is simply an existence |
| 22 |
;;; proof that the condition system can be implemented. |
| 23 |
;;; |
| 24 |
|
| 25 |
(in-package "SYSTEM") |
| 26 |
|
| 27 |
;;; ---------------------------------------------------------------------- |
| 28 |
;;; Unique Ids |
| 29 |
|
| 30 |
(defmacro unique-id (obj) |
| 31 |
"Generates a unique integer ID for its argument." |
| 32 |
`(sys:pointer ,obj)) |
| 33 |
|
| 34 |
|
| 35 |
;;; Restarts |
| 36 |
|
| 37 |
(defparameter *restart-clusters* ()) |
| 38 |
(defparameter *condition-restarts* ()) |
| 39 |
|
| 40 |
;;; do we need copy-list if *restart-clusters* has only one element? Beppe |
| 41 |
(defun compute-restarts (&optional condition) |
| 42 |
(let* ((assoc-restart ()) |
| 43 |
(other ()) |
| 44 |
(output ())) |
| 45 |
(when condition |
| 46 |
(dolist (i *condition-restarts*) |
| 47 |
(if (eq (first i) condition) |
| 48 |
(setq assoc-restart (append (rest i) assoc-restart)) |
| 49 |
(setq other (append (rest i) other))))) |
| 50 |
(dolist (restart-cluster *restart-clusters*) |
| 51 |
(dolist (restart restart-cluster) |
| 52 |
(when (and (or (not condition) |
| 53 |
(member restart assoc-restart) |
| 54 |
(not (member restart other))) |
| 55 |
(funcall (restart-test-function restart) condition)) |
| 56 |
(push restart output)))) |
| 57 |
(nreverse output))) |
| 58 |
|
| 59 |
(defun restart-print (restart stream depth) |
| 60 |
(declare (ignore depth)) |
| 61 |
(if *print-escape* |
| 62 |
(format stream "#<~s.~d>" (type-of restart) (unique-id restart)) |
| 63 |
(restart-report restart stream)) |
| 64 |
restart) |
| 65 |
|
| 66 |
(defstruct (restart (:PRINT-FUNCTION restart-print)) |
| 67 |
name |
| 68 |
function |
| 69 |
report-function |
| 70 |
interactive-function |
| 71 |
(test-function (constantly t))) |
| 72 |
|
| 73 |
(defun restart-report (restart stream) |
| 74 |
(declare (si::c-local)) |
| 75 |
(let ((fn (restart-report-function restart))) |
| 76 |
(if fn |
| 77 |
(funcall fn stream) |
| 78 |
(format stream "~s" (or (restart-name restart) restart))))) |
| 79 |
|
| 80 |
(defun bind-simple-restarts (tag names) |
| 81 |
(flet ((simple-restart-function (tag code) |
| 82 |
#'(lambda (&rest args) (throw tag (values code args))))) |
| 83 |
(cons (loop for i from 1 |
| 84 |
for n in (if (atom names) (list names) names) |
| 85 |
for f = (simple-restart-function tag i) |
| 86 |
collect (let ((v i)) |
| 87 |
(make-restart :name n |
| 88 |
:function f))) |
| 89 |
*restart-clusters*))) |
| 90 |
|
| 91 |
(defun bind-simple-handlers (tag names) |
| 92 |
(flet ((simple-handler-function (tag code) |
| 93 |
#'(lambda (c) (throw tag (values code c))))) |
| 94 |
(cons (loop for i from 1 |
| 95 |
for n in (if (atom names) (list names) names) |
| 96 |
for f = (simple-handler-function tag i) |
| 97 |
collect (cons n f)) |
| 98 |
*handler-clusters*))) |
| 99 |
|
| 100 |
(defmacro restart-bind (bindings &body forms) |
| 101 |
`(let ((*restart-clusters* |
| 102 |
(cons (list ,@(mapcar #'(lambda (binding) |
| 103 |
`(make-restart |
| 104 |
:NAME ',(car binding) |
| 105 |
:FUNCTION ,(cadr binding) |
| 106 |
,@(cddr binding))) |
| 107 |
bindings)) |
| 108 |
*restart-clusters*))) |
| 109 |
,@forms)) |
| 110 |
|
| 111 |
(defun find-restart (name &optional condition) |
| 112 |
(dolist (restart (compute-restarts condition)) |
| 113 |
(when (or (eq restart name) (eq (restart-name restart) name)) |
| 114 |
(return-from find-restart restart)))) |
| 115 |
|
| 116 |
(defun find-restart-never-fail (restart &optional condition) |
| 117 |
(declare (si::c-local)) |
| 118 |
(or (find-restart restart condition) |
| 119 |
(signal-simple-error 'control-error nil |
| 120 |
"Restart ~S is not active." |
| 121 |
(list restart)))) |
| 122 |
|
| 123 |
(defun invoke-restart (restart &rest values) |
| 124 |
(let ((real-restart (find-restart-never-fail restart))) |
| 125 |
(apply (restart-function real-restart) values))) |
| 126 |
|
| 127 |
(defun invoke-restart-interactively (restart) |
| 128 |
(let ((real-restart (find-restart-never-fail restart))) |
| 129 |
(apply (restart-function real-restart) |
| 130 |
(let ((interactive-function |
| 131 |
(restart-interactive-function real-restart))) |
| 132 |
(if interactive-function |
| 133 |
(funcall interactive-function) |
| 134 |
'()))))) |
| 135 |
|
| 136 |
|
| 137 |
(defmacro restart-case (expression &body clauses &environment env) |
| 138 |
(flet ((process-clause (clause) |
| 139 |
(do ((name (pop clause)) |
| 140 |
(args (pop clause)) |
| 141 |
(opts '(:test :report :interactive)) |
| 142 |
(keys '()) |
| 143 |
(forms clause (cddr forms))) |
| 144 |
((or (null forms) (not (member (car forms) opts))) |
| 145 |
;; name=0, tag=1, keys=2, bvl=3, body=4 |
| 146 |
(list name (gensym) keys args forms)) |
| 147 |
(let ((key (first forms)) |
| 148 |
(val (second forms))) |
| 149 |
(setf opts (remove key opts) |
| 150 |
keys (ecase key |
| 151 |
(:test |
| 152 |
(list* :test-function `(function ,val) |
| 153 |
keys)) |
| 154 |
(:interactive |
| 155 |
(list* :interactive-function `(function ,val) |
| 156 |
keys)) |
| 157 |
(:report |
| 158 |
(list* :report-function |
| 159 |
(if (stringp val) |
| 160 |
`(lambda (stream) |
| 161 |
(write-string ,val stream)) |
| 162 |
`(function ,val)) |
| 163 |
keys)))))))) |
| 164 |
(let*((block-tag (gensym)) |
| 165 |
(temp-var (gensym)) |
| 166 |
(data (mapcar #'process-clause clauses))) |
| 167 |
(let ((expression2 (macroexpand expression env))) |
| 168 |
(when (consp expression2) |
| 169 |
(let* ((condition-form nil) |
| 170 |
(condition-var (gensym)) |
| 171 |
(name (first expression2))) |
| 172 |
(case name |
| 173 |
(SIGNAL |
| 174 |
(setq condition-form `(coerce-to-condition ,(second expression2) |
| 175 |
(list ,@ (cddr expression2)) |
| 176 |
'simple-condition 'signal))) |
| 177 |
(ERROR |
| 178 |
(setq condition-form `(coerce-to-condition ,(second expression2) |
| 179 |
(list ,@(cddr expression2)) |
| 180 |
'simple-error 'ERROR))) |
| 181 |
(CERROR |
| 182 |
(setq condition-form `(coerce-to-condition ,(third expression2) |
| 183 |
(list ,@(cdddr expression2)) |
| 184 |
'SIMPLE-ERROR 'CERROR))) |
| 185 |
(WARN |
| 186 |
(setq condition-form `(coerce-to-condition ,(second expression2) |
| 187 |
(list ,@(cddr expression2)) |
| 188 |
'SIMPLE-WARNING 'WARN)))) |
| 189 |
(when condition-form |
| 190 |
(setq expression |
| 191 |
`(let ((,condition-var ,condition-form)) |
| 192 |
(with-condition-restarts ,condition-var |
| 193 |
(first *restart-clusters*) |
| 194 |
,(if (eq name 'CERROR) |
| 195 |
`(cerror ,(second expression2) ,condition-var) |
| 196 |
(list name condition-var))))))))) |
| 197 |
`(block ,block-tag |
| 198 |
(let ((,temp-var nil)) |
| 199 |
(tagbody |
| 200 |
(restart-bind |
| 201 |
,(mapcar #'(lambda (datum) |
| 202 |
(let*((name (nth 0 datum)) |
| 203 |
(tag (nth 1 datum)) |
| 204 |
(keys (nth 2 datum))) |
| 205 |
`(,name #'(lambda (&rest temp) |
| 206 |
(setq ,temp-var temp) |
| 207 |
(go ,tag)) |
| 208 |
,@keys))) |
| 209 |
data) |
| 210 |
(return-from ,block-tag ,expression)) |
| 211 |
,@(mapcan #'(lambda (datum) |
| 212 |
(let*((tag (nth 1 datum)) |
| 213 |
(bvl (nth 3 datum)) |
| 214 |
(body (nth 4 datum))) |
| 215 |
(list tag |
| 216 |
`(return-from ,block-tag |
| 217 |
(apply #'(lambda ,bvl ,@body) |
| 218 |
,temp-var))))) |
| 219 |
data))))))) |
| 220 |
|
| 221 |
(defmacro with-simple-restart ((restart-name format-control |
| 222 |
&rest format-arguments) |
| 223 |
&body forms) |
| 224 |
`(restart-case (progn ,@forms) |
| 225 |
(,restart-name () |
| 226 |
:REPORT (lambda (stream) |
| 227 |
(format stream ,format-control ,@format-arguments)) |
| 228 |
(values nil t)))) |
| 229 |
|
| 230 |
(defmacro with-condition-restarts (condition restarts &body forms) |
| 231 |
`(let ((*condition-restarts* (cons (cons ,condition ,restarts) |
| 232 |
*condition-restarts*))) |
| 233 |
,@forms)) |
| 234 |
|
| 235 |
|
| 236 |
;;; ---------------------------------------------------------------------- |
| 237 |
;;; Condition Data Type |
| 238 |
|
| 239 |
(defclass condition () |
| 240 |
((report-function :allocation :class :initform nil))) |
| 241 |
|
| 242 |
(defmethod print-object ((c condition) stream) |
| 243 |
(if *print-escape* |
| 244 |
(call-next-method) |
| 245 |
(let ((reporter (slot-value c 'report-function))) |
| 246 |
(cond ((stringp reporter) |
| 247 |
(write-string reporter stream)) |
| 248 |
((null reporter) |
| 249 |
(call-next-method)) |
| 250 |
(t |
| 251 |
(funcall reporter c stream)))))) |
| 252 |
|
| 253 |
(defmacro define-condition (name parent-list slot-specs &rest options) |
| 254 |
;; CAUTION: ANSI states the equivalence between :REPORT and a method. |
| 255 |
;; Does this mean that CALL-NEXT-METHOD should be available? SBCL does |
| 256 |
;; not do it this way, and so don't we. |
| 257 |
(let* ((class-options nil)) |
| 258 |
(dolist (option options) |
| 259 |
(case (car option) |
| 260 |
((:DEFAULT-INITARGS :DOCUMENTATION) |
| 261 |
(push option class-options)) |
| 262 |
(:REPORT |
| 263 |
(let ((report-function (cadr option))) |
| 264 |
(push `(report-function :initform ,(if (symbolp report-function) |
| 265 |
(list 'quote report-function) |
| 266 |
report-function)) |
| 267 |
slot-specs))) |
| 268 |
(otherwise (cerror "Ignore this DEFINE-CONDITION option." |
| 269 |
"Invalid DEFINE-CONDITION option: ~S" option)))) |
| 270 |
`(PROGN |
| 271 |
(DEFCLASS ,name ,(or parent-list '(CONDITION)) ,slot-specs ,@class-options) |
| 272 |
',NAME))) |
| 273 |
|
| 274 |
(defun find-subclasses-of-type (type class) |
| 275 |
;; Find all subclasses of CLASS that are subtypes of the given TYPE. |
| 276 |
(declare (si::c-local)) |
| 277 |
(if (subtypep class type) |
| 278 |
(list class) |
| 279 |
(loop for c in (clos::class-direct-subclasses class) |
| 280 |
nconc (find-subclasses-of-type type c)))) |
| 281 |
|
| 282 |
(defun make-condition (type &rest slot-initializations) |
| 283 |
(let ((class (or (and (symbolp type) (find-class type nil)) |
| 284 |
(first (last (sort (find-subclasses-of-type type (find-class 'condition)) |
| 285 |
#'si::subclassp)))))) |
| 286 |
(unless class |
| 287 |
(error 'SIMPLE-TYPE-ERROR |
| 288 |
:DATUM type |
| 289 |
:EXPECTED-TYPE 'CONDITION |
| 290 |
:FORMAT-CONTROL "Not a condition type: ~S" |
| 291 |
:FORMAT-ARGUMENTS (list type))) |
| 292 |
(apply #'make-instance class slot-initializations))) |
| 293 |
|
| 294 |
#| For the moment, do not redefine these. Beppe. |
| 295 |
(eval-when (eval compile load) |
| 296 |
|
| 297 |
(defun accumulate-cases (macro-name cases list-is-atom-p) |
| 298 |
(do ((c cases (cdr c)) |
| 299 |
(l '())) |
| 300 |
((null c) (nreverse l)) |
| 301 |
(let ((keys (caar c))) |
| 302 |
(cond ((atom keys) |
| 303 |
(cond ((null keys)) |
| 304 |
((member keys '(OTHERWISE T)) |
| 305 |
(error "OTHERWISE is not allowed in ~S expressions." |
| 306 |
macro-name)) |
| 307 |
(t (push keys l)))) |
| 308 |
(list-is-atom-p |
| 309 |
(push keys l)) |
| 310 |
(t (setq l (append keys l))))))) |
| 311 |
|
| 312 |
);nehw-lave |
| 313 |
|
| 314 |
(defmacro ecase (keyform &rest cases) |
| 315 |
(let ((keys (accumulate-cases 'ECASE cases nil))) |
| 316 |
`(case ,keyform |
| 317 |
,@cases |
| 318 |
(otherwise |
| 319 |
(error 'CASE-FAILURE :name 'ECASE |
| 320 |
:datum ,keyform |
| 321 |
:expected-type '(MEMBER ,@keys) |
| 322 |
:possibilities ',keys)))))) |
| 323 |
|
| 324 |
(defmacro ccase (keyplace &rest cases) |
| 325 |
(let ((keys (accumulate-cases 'CCASE cases nil)) |
| 326 |
(tag1 (gensym)) |
| 327 |
(tag2 (gensym))) |
| 328 |
`(block ,tag1 |
| 329 |
(tagbody ,tag2 |
| 330 |
(return-from ,tag1 |
| 331 |
(case ,keyplace |
| 332 |
,@cases |
| 333 |
(otherwise |
| 334 |
(restart-case (error 'CASE-FAILURE |
| 335 |
:name 'CCASE |
| 336 |
:datum ,keyplace |
| 337 |
:expected-type '(MEMBER ,@keys) |
| 338 |
:possibilities ',keys) |
| 339 |
(store-value (value) |
| 340 |
:report (lambda (stream) |
| 341 |
(format stream "Supply a new value of ~S." |
| 342 |
',keyplace)) |
| 343 |
:interactive read-evaluated-form |
| 344 |
(setf ,keyplace value) |
| 345 |
(go ,tag2)))))))))) |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
(defmacro etypecase (keyform &rest cases) |
| 350 |
(let ((types (accumulate-cases 'ETYPECASE cases t))) |
| 351 |
`(typecase ,keyform |
| 352 |
,@cases |
| 353 |
(otherwise |
| 354 |
(error 'CASE-FAILURE :name 'ETYPECASE |
| 355 |
:datum ,keyform |
| 356 |
:expected-type '(OR ,@types) |
| 357 |
:possibilities ',types))))) |
| 358 |
|
| 359 |
(defmacro ctypecase (keyplace &rest cases) |
| 360 |
(let ((types (accumulate-cases 'CTYPECASE cases t)) |
| 361 |
(tag1 (gensym)) |
| 362 |
(tag2 (gensym))) |
| 363 |
`(block ,tag1 |
| 364 |
(tagbody ,tag2 |
| 365 |
(return-from ,tag1 |
| 366 |
(typecase ,keyplace |
| 367 |
,@cases |
| 368 |
(otherwise |
| 369 |
(restart-case (error 'CASE-FAILURE |
| 370 |
:name 'CTYPECASE |
| 371 |
:datum ,keyplace |
| 372 |
:expected-type '(OR ,@types) |
| 373 |
:possibilities ',types) |
| 374 |
(store-value (value) |
| 375 |
:REPORT (lambda (stream) |
| 376 |
(format stream "Supply a new value of ~S." |
| 377 |
',keyplace)) |
| 378 |
:INTERACTIVE read-evaluated-form |
| 379 |
(setf ,keyplace value) |
| 380 |
(go ,tag2)))))))))) |
| 381 |
|
| 382 |
|# |
| 383 |
|
| 384 |
|
| 385 |
(defparameter *handler-clusters* nil) |
| 386 |
|
| 387 |
(defmacro handler-bind (bindings &body forms) |
| 388 |
(unless (every #'(lambda (x) (and (listp x) (= (length x) 2))) bindings) |
| 389 |
(error "Ill-formed handler bindings.")) |
| 390 |
`(let ((*handler-clusters* |
| 391 |
(cons (list ,@(mapcar #'(lambda (x) `(cons ',(car x) ,(cadr x))) |
| 392 |
bindings)) |
| 393 |
*handler-clusters*))) |
| 394 |
,@forms)) |
| 395 |
|
| 396 |
(defun signal (datum &rest arguments) |
| 397 |
(let* ((condition |
| 398 |
(coerce-to-condition datum arguments 'SIMPLE-CONDITION 'SIGNAL)) |
| 399 |
(*handler-clusters* *handler-clusters*)) |
| 400 |
(when (typep condition *break-on-signals*) |
| 401 |
(break "~A~%Break entered because of *BREAK-ON-SIGNALS*." condition)) |
| 402 |
(loop (unless *handler-clusters* (return)) |
| 403 |
(let ((cluster (pop *handler-clusters*))) |
| 404 |
(dolist (handler cluster) |
| 405 |
(when (typep condition (car handler)) |
| 406 |
(funcall (cdr handler) condition))))) |
| 407 |
nil)) |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
;;; COERCE-TO-CONDITION |
| 412 |
;;; Internal routine used in ERROR, CERROR, BREAK, and WARN for parsing the |
| 413 |
;;; hairy argument conventions into a single argument that's directly usable |
| 414 |
;;; by all the other routines. |
| 415 |
|
| 416 |
(defun coerce-to-condition (datum arguments default-type function-name) |
| 417 |
(cond ((typep datum 'CONDITION) |
| 418 |
(when arguments |
| 419 |
(cerror "Ignore the additional arguments." |
| 420 |
'SIMPLE-TYPE-ERROR |
| 421 |
:DATUM arguments |
| 422 |
:EXPECTED-TYPE 'NULL |
| 423 |
:FORMAT-CONTROL "You may not supply additional arguments ~ |
| 424 |
when giving ~S to ~S." |
| 425 |
:FORMAT-ARGUMENTS (list datum function-name))) |
| 426 |
datum) |
| 427 |
((symbolp datum) ;roughly, (subtypep datum 'CONDITION) |
| 428 |
(apply #'make-condition datum arguments)) |
| 429 |
((or (stringp datum) (functionp datum)) |
| 430 |
(make-condition default-type |
| 431 |
:FORMAT-CONTROL datum |
| 432 |
:FORMAT-ARGUMENTS arguments)) |
| 433 |
(t |
| 434 |
(error 'SIMPLE-TYPE-ERROR |
| 435 |
:DATUM datum |
| 436 |
:EXPECTED-TYPE '(OR SYMBOL STRING) |
| 437 |
:FORMAT-CONTROL "Bad argument to ~S: ~S" |
| 438 |
:FORMAT-ARGUMENTS (list function-name datum))))) |
| 439 |
|
| 440 |
(defun break (&optional (format-control "Break") &rest format-arguments) |
| 441 |
"Enters a break loop. The execution of the program can be resumed by typing |
| 442 |
:CONTINUE at the break loop. Type :HELP to see the break-loop commands list. |
| 443 |
If FORMAT-STRING is non-NIL, it is used as the format string to be output to |
| 444 |
*ERROR-OUTPUT* before entering the break loop. ARGs are arguments to the |
| 445 |
format string." |
| 446 |
(let ((*debugger-hook* nil)) |
| 447 |
(with-simple-restart (continue "Return from BREAK.") |
| 448 |
(invoke-debugger |
| 449 |
(make-condition 'SIMPLE-CONDITION |
| 450 |
:FORMAT-CONTROL format-control |
| 451 |
:FORMAT-ARGUMENTS format-arguments)))) |
| 452 |
nil) |
| 453 |
|
| 454 |
(defun warn (datum &rest arguments) |
| 455 |
"Args: (format-string &rest args) |
| 456 |
Formats FORMAT-STRING and ARGs to *ERROR-OUTPUT* as a warning message. Enters |
| 457 |
a break level if the value of *BREAK-ON-WARNINGS* is non-NIL. Otherwise, |
| 458 |
returns with NIL." |
| 459 |
(let ((condition |
| 460 |
(coerce-to-condition datum arguments 'SIMPLE-WARNING 'WARN))) |
| 461 |
(check-type condition warning "a warning condition") |
| 462 |
(restart-case (signal condition) |
| 463 |
(muffle-warning () |
| 464 |
:REPORT "Skip warning." |
| 465 |
(return-from warn nil))) |
| 466 |
(format *error-output* "~&;;; Warning: ~A~%" condition) |
| 467 |
nil)) |
| 468 |
|
| 469 |
|
| 470 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 471 |
;;; ALL CONDITIONS |
| 472 |
;;; |
| 473 |
;;; Instead of compiling each condition definition, we store them in a |
| 474 |
;;; list and evaluate them at run time. Besides, there are multiple |
| 475 |
;;; SIMPLE-* conditions which inherit from SIMPLE-ERROR and which are |
| 476 |
;;; only created when the error is signaled. |
| 477 |
;;; |
| 478 |
|
| 479 |
(define-condition warning () ()) |
| 480 |
|
| 481 |
(define-condition serious-condition () ()) |
| 482 |
|
| 483 |
(define-condition error (serious-condition) ()) |
| 484 |
|
| 485 |
(defgeneric simple-condition-format-control (instance) |
| 486 |
(:method (instance) |
| 487 |
(error 'type-error :datum instance :expected-type 'simple-condition))) |
| 488 |
|
| 489 |
(defgeneric simple-condition-format-arguments (instance) |
| 490 |
(:method (instance) |
| 491 |
(error 'type-error :datum instance :expected-type 'simple-condition))) |
| 492 |
|
| 493 |
(define-condition simple-condition () |
| 494 |
((format-control :INITARG :FORMAT-CONTROL :INITFORM "" |
| 495 |
:ACCESSOR simple-condition-format-control) |
| 496 |
(format-arguments :INITARG :FORMAT-ARGUMENTS :INITFORM NIL |
| 497 |
:ACCESSOR simple-condition-format-arguments)) |
| 498 |
(:REPORT |
| 499 |
(lambda (condition stream) |
| 500 |
(format stream "~?" (simple-condition-format-control condition) |
| 501 |
(simple-condition-format-arguments condition))))) |
| 502 |
|
| 503 |
(define-condition simple-warning (simple-condition warning) ()) |
| 504 |
|
| 505 |
(define-condition style-warning (warning) ()) |
| 506 |
|
| 507 |
(define-condition simple-style-warning (style-warning simple-condition) ()) |
| 508 |
|
| 509 |
(define-condition simple-error (simple-condition error) ()) |
| 510 |
|
| 511 |
(define-condition storage-condition (serious-condition) ()) |
| 512 |
|
| 513 |
(define-condition ext:segmentation-violation (storage-condition) |
| 514 |
() |
| 515 |
(:REPORT "Detected access to an invalid or protected memory address.")) |
| 516 |
|
| 517 |
(define-condition ext:stack-overflow (storage-condition) |
| 518 |
((size :initarg :size :initform 0 :reader ext:stack-overflow-size) |
| 519 |
(type :initarg :type :initform nil :reader ext:stack-overflow-type)) |
| 520 |
(:REPORT |
| 521 |
(lambda (condition stream) |
| 522 |
(let* ((type (ext::stack-overflow-type condition)) |
| 523 |
(size (ext::stack-overflow-size condition))) |
| 524 |
(if size |
| 525 |
(format stream "~A overflow at size ~D. Stack can probably be resized. |
| 526 |
Proceed with caution." |
| 527 |
type size) |
| 528 |
(format stream "~A stack overflow. Stack cannot grow any further. Either exit |
| 529 |
or return to an outer frame, undoing all the function calls so far." |
| 530 |
type)))))) |
| 531 |
|
| 532 |
(define-condition ext:storage-exhausted (storage-condition) () |
| 533 |
(:REPORT "Memory limit reached. Please jump to an outer pointer, quit program and enlarge the |
| 534 |
memory limits before executing the program again.")) |
| 535 |
|
| 536 |
(define-condition ext:illegal-instruction (serious-condition) |
| 537 |
() |
| 538 |
(:REPORT "Illegal instruction.")) |
| 539 |
|
| 540 |
(define-condition ext:timeout (serious-condition) |
| 541 |
((value :initarg :value :initform nil)) |
| 542 |
(:report (lambda (condition stream) |
| 543 |
(format stream "Timeout occurred~@[ after ~A second~:P~]." |
| 544 |
(slot-value condition 'value)))) |
| 545 |
(:documentation |
| 546 |
"Signaled when an operation does not complete within an allotted time budget.")) |
| 547 |
|
| 548 |
(define-condition ext:unix-signal-received () |
| 549 |
((code :type fixnum |
| 550 |
:initform 0 |
| 551 |
:initarg :code |
| 552 |
:accessor ext:unix-signal-received-code)) |
| 553 |
(:report (lambda (condition stream) |
| 554 |
(format stream "Serious signal ~D caught." |
| 555 |
(ext:unix-signal-received-code condition))))) |
| 556 |
|
| 557 |
(defgeneric type-error-datum (instance) |
| 558 |
(:method (instance) |
| 559 |
(error 'type-error :datum instance :expected-type 'type-error))) |
| 560 |
|
| 561 |
(defgeneric type-error-expected-type (instance) |
| 562 |
(:method (instance) |
| 563 |
(error 'type-error :datum instance :expected-type 'type-error))) |
| 564 |
|
| 565 |
(define-condition type-error (error) |
| 566 |
((datum :INITARG :DATUM :READER type-error-datum) |
| 567 |
(expected-type :INITARG :EXPECTED-TYPE :READER type-error-expected-type)) |
| 568 |
(:REPORT |
| 569 |
(lambda (condition stream) |
| 570 |
(format stream "~S is not of type ~S." |
| 571 |
(type-error-datum condition) |
| 572 |
(type-error-expected-type condition))))) |
| 573 |
|
| 574 |
(define-condition simple-type-error (simple-condition type-error) ()) |
| 575 |
|
| 576 |
(define-condition case-failure (type-error) |
| 577 |
((name :INITARG :NAME :READER case-failure-name) |
| 578 |
(possibilities :INITARG :POSSIBILITIES :READER case-failure-possibilities)) |
| 579 |
(:REPORT |
| 580 |
(lambda (condition stream) |
| 581 |
(format stream "~S fell through ~S expression.~%Wanted one of ~:S." |
| 582 |
(type-error-datum condition) |
| 583 |
(case-failure-name condition) |
| 584 |
(case-failure-possibilities condition))))) |
| 585 |
|
| 586 |
(define-condition program-error (error) ()) |
| 587 |
|
| 588 |
(define-condition control-error (error) ()) |
| 589 |
|
| 590 |
(defgeneric stream-error-stream (instance) |
| 591 |
(:method (instance) |
| 592 |
(error 'type-error :datum instance :expected-type 'stream-error))) |
| 593 |
|
| 594 |
(define-condition stream-error (error) |
| 595 |
((stream :initarg :stream :reader stream-error-stream))) |
| 596 |
|
| 597 |
(define-condition end-of-file (stream-error) |
| 598 |
() |
| 599 |
(:REPORT (lambda (condition stream) |
| 600 |
(format stream "Unexpected end of file on ~S." |
| 601 |
(stream-error-stream condition))))) |
| 602 |
|
| 603 |
(defgeneric file-error-pathname (instance) |
| 604 |
(:method (instance) |
| 605 |
(error 'type-error :datum instance :expected-type 'file-error))) |
| 606 |
|
| 607 |
(define-condition file-error (error) |
| 608 |
((pathname :INITARG :PATHNAME :READER file-error-pathname)) |
| 609 |
(:REPORT (lambda (condition stream) |
| 610 |
(format stream "Filesystem error with pathname ~S.~%Either |
| 611 |
1) the file does not exist, or |
| 612 |
2) we are not allowed to access the file, or |
| 613 |
3) the pathname points to a broken symbolic link." |
| 614 |
(file-error-pathname condition))))) |
| 615 |
|
| 616 |
(defgeneric package-error-package (instance) |
| 617 |
(:method (instance) |
| 618 |
(error 'type-error :datum instance :expected-type 'package-error))) |
| 619 |
|
| 620 |
(define-condition package-error (error) |
| 621 |
((package :INITARG :PACKAGE :READER package-error-package))) |
| 622 |
|
| 623 |
(defgeneric cell-error-name (instance) |
| 624 |
(:method (instance) |
| 625 |
(error 'type-error :datum instance :expected-type 'cell-error))) |
| 626 |
|
| 627 |
(define-condition cell-error (error) |
| 628 |
((name :INITARG :NAME :READER cell-error-name))) |
| 629 |
|
| 630 |
(define-condition unbound-variable (cell-error) |
| 631 |
() |
| 632 |
(:REPORT (lambda (condition stream) |
| 633 |
(format stream "The variable ~S is unbound." |
| 634 |
(cell-error-name condition))))) |
| 635 |
|
| 636 |
(defgeneric unbound-slot-instance (instance) |
| 637 |
(:method (instance) |
| 638 |
(error 'type-error :datum instance :expected-type 'unbound-slot))) |
| 639 |
|
| 640 |
(define-condition unbound-slot (cell-error) |
| 641 |
((instance :INITARG :INSTANCE :READER unbound-slot-instance)) |
| 642 |
(:REPORT (lambda (condition stream) |
| 643 |
(format stream "The slot ~S in the object ~S is unbound." |
| 644 |
(cell-error-name condition) |
| 645 |
(unbound-slot-instance condition))))) |
| 646 |
|
| 647 |
(define-condition undefined-function (cell-error) |
| 648 |
() |
| 649 |
(:REPORT (lambda (condition stream) |
| 650 |
(format stream "The function ~S is undefined." |
| 651 |
(cell-error-name condition))))) |
| 652 |
|
| 653 |
(defgeneric arithmetic-error-operation (instance) |
| 654 |
(:method (instance) |
| 655 |
(error 'type-error :datum instance :expected-type 'arithmetic-error))) |
| 656 |
|
| 657 |
(defgeneric arithmetic-error-operands (instance) |
| 658 |
(:method (instance) |
| 659 |
(error 'type-error :datum instance :expected-type 'arithmetic-error))) |
| 660 |
|
| 661 |
(define-condition arithmetic-error (error) |
| 662 |
((operation :INITARG :OPERATION :READER arithmetic-error-operation) |
| 663 |
(operands :INITARG :OPERANDS :INITFORM '() :READER arithmetic-error-operands))) |
| 664 |
|
| 665 |
(define-condition division-by-zero (arithmetic-error) ()) |
| 666 |
|
| 667 |
(define-condition floating-point-overflow (arithmetic-error) ()) |
| 668 |
|
| 669 |
(define-condition floating-point-underflow (arithmetic-error) ()) |
| 670 |
|
| 671 |
(define-condition floating-point-inexact (arithmetic-error) ()) |
| 672 |
|
| 673 |
(define-condition floating-point-invalid-operation (arithmetic-error) ()) |
| 674 |
|
| 675 |
(define-condition abort-failure (control-error) () |
| 676 |
(:REPORT "Abort failed.")) |
| 677 |
|
| 678 |
(defgeneric print-not-readable-object (instance) |
| 679 |
(:method (instance) |
| 680 |
(error 'type-error :datum instance :expected-type 'print-not-readable))) |
| 681 |
|
| 682 |
(define-condition print-not-readable (error) |
| 683 |
((object :INITARG :OBJECT :READER print-not-readable-object)) |
| 684 |
(:REPORT (lambda (condition stream) |
| 685 |
(format stream "Cannot print object ~A readably." |
| 686 |
(print-not-readable-object condition))))) |
| 687 |
|
| 688 |
(define-condition parse-error (error) ()) |
| 689 |
|
| 690 |
(define-condition reader-error (parse-error stream-error) ()) |
| 691 |
|
| 692 |
(define-condition format-error (simple-error) |
| 693 |
((format-control :initarg :complaint) |
| 694 |
(format-arguments :initarg :arguments) |
| 695 |
(control-string :reader format-error-control-string |
| 696 |
:initarg :control-string |
| 697 |
#+cmu-format :initform |
| 698 |
#+cmu-format *default-format-error-control-string*) |
| 699 |
(offset :reader format-error-offset :initarg :offset |
| 700 |
#+cmu-format :initform |
| 701 |
#+cmu-format *default-format-error-offset*) |
| 702 |
(print-banner :reader format-error-print-banner :initarg :print-banner |
| 703 |
:initform t)) |
| 704 |
(:report (lambda (condition stream) |
| 705 |
(cl:format stream |
| 706 |
"~:[~;Error in format: ~]~ |
| 707 |
~?~@[~% ~A~% ~V@T^~]" |
| 708 |
(format-error-print-banner condition) |
| 709 |
(simple-condition-format-control condition) |
| 710 |
(simple-condition-format-arguments condition) |
| 711 |
(format-error-control-string condition) |
| 712 |
(format-error-offset condition))))) |
| 713 |
|
| 714 |
(define-condition ext:interactive-interrupt (serious-condition) |
| 715 |
() |
| 716 |
(:report "Console interrupt.")) |
| 717 |
|
| 718 |
|
| 719 |
(defun signal-simple-error (base-condition continue-message format-control format-args |
| 720 |
&rest args) |
| 721 |
(let ((simple-error-name (intern (concatenate 'string "SIMPLE-" (string base-condition)) |
| 722 |
(find-package "SI")))) |
| 723 |
(unless (find-class simple-error-name nil) |
| 724 |
(eval `(defclass ,simple-error-name (simple-error ,base-condition) ()))) |
| 725 |
(if continue-message |
| 726 |
(apply #'cerror continue-message simple-error-name :format-control format-control |
| 727 |
:format-arguments format-args args) |
| 728 |
(apply #'error simple-error-name :format-control format-control |
| 729 |
:format-arguments format-args args)))) |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
(defmacro handler-case (form &rest cases) |
| 734 |
(let ((no-error-clause (assoc ':NO-ERROR cases))) |
| 735 |
(if no-error-clause |
| 736 |
(let* ((normal-return (make-symbol "NORMAL-RETURN")) |
| 737 |
(error-return (make-symbol "ERROR-RETURN"))) |
| 738 |
`(block ,error-return |
| 739 |
(multiple-value-call #'(lambda ,@(cdr no-error-clause)) |
| 740 |
(block ,normal-return |
| 741 |
(return-from ,error-return |
| 742 |
(handler-case (return-from ,normal-return ,form) |
| 743 |
,@(remove no-error-clause cases))))))) |
| 744 |
(let* ((tag (gensym)) |
| 745 |
(var (gensym)) |
| 746 |
(annotated-cases (mapcar #'(lambda (case) (cons (gensym) case)) |
| 747 |
cases))) |
| 748 |
`(block ,tag |
| 749 |
(let ((,var nil)) |
| 750 |
(declare (ignorable ,var)) |
| 751 |
(tagbody |
| 752 |
(handler-bind ,(mapcar #'(lambda (annotated-case) |
| 753 |
(list (cadr annotated-case) |
| 754 |
`#'(lambda (temp) |
| 755 |
(declare (ignorable temp)) |
| 756 |
,@(if (caddr annotated-case) |
| 757 |
`((setq ,var temp))) |
| 758 |
(go ,(car annotated-case))))) |
| 759 |
annotated-cases) |
| 760 |
(return-from ,tag ,form)) |
| 761 |
,@(mapcan #'(lambda (annotated-case) |
| 762 |
(list (car annotated-case) |
| 763 |
(let ((body (cdddr annotated-case))) |
| 764 |
`(return-from ,tag |
| 765 |
,(if (caddr annotated-case) |
| 766 |
`(let ((,(caaddr annotated-case) |
| 767 |
,var)) |
| 768 |
,@body) |
| 769 |
;; We must allow declarations! |
| 770 |
`(locally ,@body)))))) |
| 771 |
annotated-cases)))))))) |
| 772 |
|
| 773 |
(defmacro ignore-errors (&rest forms) |
| 774 |
`(handler-case (progn ,@forms) |
| 775 |
(error (condition) (values nil condition)))) |
| 776 |
|
| 777 |
(defun abort (&optional c) |
| 778 |
(invoke-restart (find-restart-never-fail 'ABORT c)) |
| 779 |
(error 'ABORT-FAILURE)) |
| 780 |
|
| 781 |
(defun continue (&optional c) |
| 782 |
(let ((restart (find-restart 'CONTINUE c))) |
| 783 |
(and restart (invoke-restart restart)))) |
| 784 |
|
| 785 |
(defun muffle-warning (&optional c) |
| 786 |
(invoke-restart (find-restart-never-fail 'MUFFLE-WARNING c))) |
| 787 |
|
| 788 |
(defun store-value (value &optional c) |
| 789 |
(let ((restart (find-restart 'STORE-VALUE c))) |
| 790 |
(and restart (invoke-restart restart value)))) |
| 791 |
|
| 792 |
(defun use-value (value &optional c) |
| 793 |
(let ((restart (find-restart 'USE-VALUE c))) |
| 794 |
(and restart (invoke-restart restart value)))) |
| 795 |
|
| 796 |
(defun assert-report (names stream) |
| 797 |
(declare (si::c-local)) |
| 798 |
(format stream "Retry assertion") |
| 799 |
(if names |
| 800 |
(format stream " with new value~P for ~{~S~^, ~}." |
| 801 |
(length names) names) |
| 802 |
(format stream "."))) |
| 803 |
|
| 804 |
(defun assert-prompt (name value) |
| 805 |
(declare (si::c-local)) |
| 806 |
(if (y-or-n-p "The old value of ~S is ~S.~ |
| 807 |
~%Do you want to supply a new value? " |
| 808 |
name value) |
| 809 |
(flet ((read-it () (eval (read *query-io*)))) |
| 810 |
(format *query-io* "~&Type a form to be evaluated:~%") |
| 811 |
(if (symbolp name) ;Help user debug lexical variables |
| 812 |
(progv (list name) (list value) (read-it)) |
| 813 |
(read-it))) |
| 814 |
value)) |
| 815 |
|
| 816 |
(defun assert-failure (test-form &optional place-names values |
| 817 |
&rest arguments) |
| 818 |
(unless arguments |
| 819 |
(setf arguments (list 'SIMPLE-TYPE-ERROR |
| 820 |
:DATUM test-form |
| 821 |
:EXPECTED-TYPE nil ; This needs some work in revision |
| 822 |
:FORMAT-CONTROL "The assertion ~S failed" |
| 823 |
:FORMAT-ARGUMENTS (list test-form)))) |
| 824 |
(restart-case (error (si::coerce-to-condition (first arguments) |
| 825 |
(rest arguments) |
| 826 |
'simple-error |
| 827 |
'assert)) |
| 828 |
(continue () |
| 829 |
:REPORT (lambda (stream) (assert-report place-names stream)) |
| 830 |
(return-from assert-failure |
| 831 |
(values-list (loop for place-name in place-names |
| 832 |
for value in values |
| 833 |
collect (assert-prompt place-name value))))))) |
| 834 |
|
| 835 |
;;; ---------------------------------------------------------------------- |
| 836 |
;;; ECL's interface to the toplevel and debugger |
| 837 |
|
| 838 |
(defun sys::universal-error-handler (continue-string datum args) |
| 839 |
"Args: (error-name continuable-p function-name |
| 840 |
continue-format-string error-format-string |
| 841 |
&rest args) |
| 842 |
ECL specific. |
| 843 |
Starts the error handler of ECL. |
| 844 |
When an error is detected, ECL calls this function with the specified |
| 845 |
arguments. To change the error handler of ECL, redefine this function. |
| 846 |
ERROR-NAME is the name of the error. CONTINUABLE-P is T for a continuable |
| 847 |
error and NIL for a fatal error. FUNCTION-NAME is the name of the function |
| 848 |
that caused the error. CONTINUE-FORMAT-STRING and ERROR-FORMAT-STRING are the |
| 849 |
format strings of the error message. ARGS are the arguments to the format |
| 850 |
strings." |
| 851 |
(declare (inline apply) ;; So as not to get bogus frames in debugger |
| 852 |
#-ecl-min |
| 853 |
(c::policy-debug-ihs-frame)) |
| 854 |
(let ((condition (coerce-to-condition datum args 'simple-error 'error))) |
| 855 |
(cond |
| 856 |
((eq t continue-string) |
| 857 |
; from CEerror; mostly allocation errors |
| 858 |
(with-simple-restart (ignore "Ignore the error, and try the operation again") |
| 859 |
(signal condition) |
| 860 |
(invoke-debugger condition))) |
| 861 |
((stringp continue-string) |
| 862 |
(with-simple-restart |
| 863 |
(continue "~A" (format nil "~?" continue-string args)) |
| 864 |
(signal condition) |
| 865 |
(invoke-debugger condition))) |
| 866 |
((and continue-string (symbolp continue-string)) |
| 867 |
; from CEerror |
| 868 |
(with-simple-restart (accept "Accept the error, returning NIL") |
| 869 |
(multiple-value-bind (rv used-restart) |
| 870 |
(with-simple-restart (ignore "Ignore the error, and try the operation again") |
| 871 |
(multiple-value-bind (rv used-restart) |
| 872 |
(with-simple-restart (continue "Continue, using ~S" continue-string) |
| 873 |
(signal condition) |
| 874 |
(invoke-debugger condition)) |
| 875 |
(if used-restart continue-string rv))) |
| 876 |
(if used-restart t rv)))) |
| 877 |
(t |
| 878 |
(progn |
| 879 |
(signal condition) |
| 880 |
(invoke-debugger condition)))))) |
| 881 |
|
| 882 |
(defun sys::tpl-continue-command (&rest any) |
| 883 |
(apply #'invoke-restart 'continue any)) |