1 ;;;; stuff originally from CMU CL's error.lisp which can or should
2 ;;;; come late (mostly related to the CONDITION class itself)
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
14 (in-package "SB!KERNEL")
16 ;;;; miscellaneous support utilities
18 ;;; Signalling an error when trying to print an error condition is
19 ;;; generally a PITA, so whatever the failure encountered when
20 ;;; wondering about FILE-POSITION within a condition printer, 'tis
21 ;;; better silently to give up than to try to complain.
22 (defun file-position-or-nil-for-error (stream &optional (pos nil posp))
23 ;; Arguably FILE-POSITION shouldn't be signalling errors at all; but
24 ;; "NIL if this cannot be determined" in the ANSI spec doesn't seem
25 ;; absolutely unambiguously to prohibit errors when, e.g., STREAM
26 ;; has been closed so that FILE-POSITION is a nonsense question. So
27 ;; my (WHN) impression is that the conservative approach is to
28 ;; IGNORE-ERRORS. (I encountered this failure from within a homebrew
29 ;; defsystemish operation where the ERROR-STREAM had been CL:CLOSEd,
30 ;; I think by nonlocally exiting through a WITH-OPEN-FILE, by the
31 ;; time an error was reported.)
33 (ignore-errors (file-position stream pos))
34 (ignore-errors (file-position stream))))
36 ;;;; the CONDITION class
38 (/show0 "condition.lisp 20")
40 (eval-when (:compile-toplevel :load-toplevel :execute)
42 (/show0 "condition.lisp 24")
44 (def!struct (condition-classoid (:include slot-classoid)
45 (:constructor make-condition-classoid))
46 ;; list of CONDITION-SLOT structures for the direct slots of this
48 (slots nil :type list)
49 ;; list of CONDITION-SLOT structures for all of the effective class
50 ;; slots of this class
51 (class-slots nil :type list)
52 ;; report function or NIL
53 (report nil :type (or function null))
54 ;; list of alternating initargs and initforms
55 (default-initargs () :type list)
56 ;; class precedence list as a list of CLASS objects, with all
57 ;; non-CONDITION classes removed
59 ;; a list of all the effective instance allocation slots of this
60 ;; class that have a non-constant initform or default-initarg.
61 ;; Values for these slots must be computed in the dynamic
62 ;; environment of MAKE-CONDITION.
63 (hairy-slots nil :type list))
65 (/show0 "condition.lisp 49")
69 (!defstruct-with-alternate-metaclass condition
70 :slot-names (actual-initargs assigned-slots)
71 :boa-constructor %make-condition-object
72 :superclass-name instance
73 :metaclass-name condition-classoid
74 :metaclass-constructor make-condition-classoid
77 (defun make-condition-object (actual-initargs)
78 (%make-condition-object actual-initargs nil))
80 (defstruct (condition-slot (:copier nil))
81 (name (missing-arg) :type symbol)
82 ;; list of all applicable initargs
83 (initargs (missing-arg) :type list)
84 ;; names of reader and writer functions
85 (readers (missing-arg) :type list)
86 (writers (missing-arg) :type list)
87 ;; true if :INITFORM was specified
88 (initform-p (missing-arg) :type (member t nil))
89 ;; If this is a function, call it with no args. Otherwise, it's the
91 (initform (missing-arg) :type t)
92 ;; allocation of this slot, or NIL until defaulted
93 (allocation nil :type (member :instance :class nil))
94 ;; If ALLOCATION is :CLASS, this is a cons whose car holds the value.
95 (cell nil :type (or cons null))
97 (documentation nil :type (or string null)))
99 ;;; KLUDGE: It's not clear to me why CONDITION-CLASS has itself listed
100 ;;; in its CPL, while other classes derived from CONDITION-CLASS don't
101 ;;; have themselves listed in their CPLs. This behavior is inherited
102 ;;; from CMU CL, and didn't seem to be explained there, and I haven't
103 ;;; figured out whether it's right. -- WHN 19990612
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105 (/show0 "condition.lisp 103")
106 (let ((condition-class (locally
107 ;; KLUDGE: There's a DEFTRANSFORM
108 ;; FIND-CLASSOID for constant class names
109 ;; which creates fast but
110 ;; non-cold-loadable, non-compact code. In
111 ;; this context, we'd rather have compact,
112 ;; cold-loadable code. -- WHN 19990928
113 (declare (notinline find-classoid))
114 (find-classoid 'condition))))
115 (setf (condition-classoid-cpl condition-class)
116 (list condition-class)))
117 (/show0 "condition.lisp 103"))
119 (setf (condition-classoid-report (locally
120 ;; KLUDGE: There's a DEFTRANSFORM
121 ;; FIND-CLASSOID for constant class
122 ;; names which creates fast but
123 ;; non-cold-loadable, non-compact
124 ;; code. In this context, we'd
125 ;; rather have compact,
126 ;; cold-loadable code. -- WHN
128 (declare (notinline find-classoid))
129 (find-classoid 'condition)))
130 (lambda (cond stream)
131 (format stream "Condition ~S was signalled." (type-of cond))))
133 (eval-when (:compile-toplevel :load-toplevel :execute)
135 (defun find-condition-layout (name parent-types)
136 (let* ((cpl (remove-duplicates
140 (condition-classoid-cpl
143 (cond-layout (info :type :compiler-layout 'condition))
144 (olayout (info :type :compiler-layout name))
145 ;; FIXME: Does this do the right thing in case of multiple
146 ;; inheritance? A quick look at DEFINE-CONDITION didn't make
147 ;; it obvious what ANSI intends to be done in the case of
148 ;; multiple inheritance, so it's not actually clear what the
151 (order-layout-inherits (concatenate 'simple-vector
152 (layout-inherits cond-layout)
153 (mapcar #'classoid-layout cpl)))))
155 (not (mismatch (layout-inherits olayout) new-inherits)))
157 (make-layout :classoid (make-undefined-classoid name)
158 :inherits new-inherits
160 :length (layout-length cond-layout)))))
164 ;;; FIXME: ANSI's definition of DEFINE-CONDITION says
165 ;;; Condition reporting is mediated through the PRINT-OBJECT method
166 ;;; for the condition type in question, with *PRINT-ESCAPE* always
167 ;;; being nil. Specifying (:REPORT REPORT-NAME) in the definition of
168 ;;; a condition type C is equivalent to:
169 ;;; (defmethod print-object ((x c) stream)
170 ;;; (if *print-escape* (call-next-method) (report-name x stream)))
171 ;;; The current code doesn't seem to quite match that.
172 (def!method print-object ((x condition) stream)
174 (print-unreadable-object (x stream :type t :identity t))
175 ;; KLUDGE: A comment from CMU CL here said
176 ;; 7/13/98 BUG? CPL is not sorted and results here depend on order of
177 ;; superclasses in define-condition call!
178 (dolist (class (condition-classoid-cpl (classoid-of x))
179 (error "no REPORT? shouldn't happen!"))
180 (let ((report (condition-classoid-report class)))
182 (return (funcall report x stream)))))))
184 ;;;; slots of CONDITION objects
186 (defvar *empty-condition-slot* '(empty))
188 (defun find-slot-default (class slot)
189 (let ((initargs (condition-slot-initargs slot))
190 (cpl (condition-classoid-cpl class)))
192 (let ((default-initargs (condition-classoid-default-initargs class)))
193 (dolist (initarg initargs)
194 (let ((val (getf default-initargs initarg *empty-condition-slot*)))
195 (unless (eq val *empty-condition-slot*)
196 (return-from find-slot-default
201 (if (condition-slot-initform-p slot)
202 (let ((initform (condition-slot-initform slot)))
203 (if (functionp initform)
206 (error "unbound condition slot: ~S" (condition-slot-name slot)))))
208 (defun find-condition-class-slot (condition-class slot-name)
210 (condition-classoid-cpl condition-class)
211 (error "There is no slot named ~S in ~S."
212 slot-name condition-class))
213 (dolist (slot (condition-classoid-slots sclass))
214 (when (eq (condition-slot-name slot) slot-name)
215 (return-from find-condition-class-slot slot)))))
217 (defun condition-writer-function (condition new-value name)
218 (dolist (cslot (condition-classoid-class-slots
219 (layout-classoid (%instance-layout condition)))
220 (setf (getf (condition-assigned-slots condition) name)
222 (when (eq (condition-slot-name cslot) name)
223 (return (setf (car (condition-slot-cell cslot)) new-value)))))
225 (defun condition-reader-function (condition name)
226 (let ((class (layout-classoid (%instance-layout condition))))
227 (dolist (cslot (condition-classoid-class-slots class))
228 (when (eq (condition-slot-name cslot) name)
229 (return-from condition-reader-function
230 (car (condition-slot-cell cslot)))))
231 (let ((val (getf (condition-assigned-slots condition) name
232 *empty-condition-slot*)))
233 (if (eq val *empty-condition-slot*)
234 (let ((actual-initargs (condition-actual-initargs condition))
235 (slot (find-condition-class-slot class name)))
237 (error "missing slot ~S of ~S" name condition))
238 (do ((initargs actual-initargs (cddr initargs)))
240 (setf (getf (condition-assigned-slots condition) name)
241 (find-slot-default class slot)))
242 (when (member (car initargs) (condition-slot-initargs slot))
243 (return-from condition-reader-function
244 (setf (getf (condition-assigned-slots condition)
251 (defun make-condition (thing &rest args)
253 "Make an instance of a condition object using the specified initargs."
254 ;; Note: ANSI specifies no exceptional situations in this function.
255 ;; signalling simple-type-error would not be wrong.
256 (let* ((thing (or (and (symbolp thing) (find-classoid thing nil))
258 (class (typecase thing
259 (condition-classoid thing)
261 (error 'simple-type-error
263 :expected-type 'condition-class
264 :format-control "~S is not a condition class."
265 :format-arguments (list thing)))
267 (error 'simple-type-error
269 :expected-type 'condition-class
270 :format-control "bad thing for class argument:~% ~S"
271 :format-arguments (list thing)))))
272 (res (make-condition-object args)))
273 (setf (%instance-layout res) (classoid-layout class))
274 ;; Set any class slots with initargs present in this call.
275 (dolist (cslot (condition-classoid-class-slots class))
276 (dolist (initarg (condition-slot-initargs cslot))
277 (let ((val (getf args initarg *empty-condition-slot*)))
278 (unless (eq val *empty-condition-slot*)
279 (setf (car (condition-slot-cell cslot)) val)))))
280 ;; Default any slots with non-constant defaults now.
281 (dolist (hslot (condition-classoid-hairy-slots class))
282 (when (dolist (initarg (condition-slot-initargs hslot) t)
283 (unless (eq (getf args initarg *empty-condition-slot*)
284 *empty-condition-slot*)
286 (setf (getf (condition-assigned-slots res) (condition-slot-name hslot))
287 (find-slot-default class hslot))))
290 ;;;; DEFINE-CONDITION
292 (eval-when (:compile-toplevel :load-toplevel :execute)
293 (defun %compiler-define-condition (name direct-supers layout
294 all-readers all-writers)
295 (with-single-package-locked-error
296 (:symbol name "defining ~A as a condition")
297 (sb!xc:proclaim `(ftype (function (t) t) ,@all-readers))
298 (sb!xc:proclaim `(ftype (function (t t) t) ,@all-writers))
299 (multiple-value-bind (class old-layout)
300 (insured-find-classoid name
301 #'condition-classoid-p
302 #'make-condition-classoid)
303 (setf (layout-classoid layout) class)
304 (setf (classoid-direct-superclasses class)
305 (mapcar #'find-classoid direct-supers))
306 (cond ((not old-layout)
307 (register-layout layout))
308 ((not *type-system-initialized*)
309 (setf (layout-classoid old-layout) class)
310 (setq layout old-layout)
311 (unless (eq (classoid-layout class) layout)
312 (register-layout layout)))
313 ((redefine-layout-warning "current"
316 (layout-length layout)
317 (layout-inherits layout)
318 (layout-depthoid layout))
319 (register-layout layout :invalidate t))
320 ((not (classoid-layout class))
321 (register-layout layout)))
323 (setf (layout-info layout)
325 ;; KLUDGE: There's a FIND-CLASS DEFTRANSFORM for constant class
326 ;; names which creates fast but non-cold-loadable, non-compact
327 ;; code. In this context, we'd rather have compact, cold-loadable
328 ;; code. -- WHN 19990928
329 (declare (notinline find-classoid))
330 (layout-info (classoid-layout (find-classoid 'condition)))))
332 (setf (find-classoid name) class)
334 ;; Initialize CPL slot.
335 (setf (condition-classoid-cpl class)
336 (remove-if-not #'condition-classoid-p
337 (std-compute-class-precedence-list class)))))
341 ;;; Compute the effective slots of CLASS, copying inherited slots and
342 ;;; destructively modifying direct slots.
344 ;;; FIXME: It'd be nice to explain why it's OK to destructively modify
345 ;;; direct slots. Presumably it follows from the semantics of
346 ;;; inheritance and redefinition of conditions, but finding the cite
347 ;;; and documenting it here would be good. (Or, if this is not in fact
348 ;;; ANSI-compliant, fixing it would also be good.:-)
349 (defun compute-effective-slots (class)
350 (collect ((res (copy-list (condition-classoid-slots class))))
351 (dolist (sclass (cdr (condition-classoid-cpl class)))
352 (dolist (sslot (condition-classoid-slots sclass))
353 (let ((found (find (condition-slot-name sslot) (res)
354 :key #'condition-slot-name)))
356 (setf (condition-slot-initargs found)
357 (union (condition-slot-initargs found)
358 (condition-slot-initargs sslot)))
359 (unless (condition-slot-initform-p found)
360 (setf (condition-slot-initform-p found)
361 (condition-slot-initform-p sslot))
362 (setf (condition-slot-initform found)
363 (condition-slot-initform sslot)))
364 (unless (condition-slot-allocation found)
365 (setf (condition-slot-allocation found)
366 (condition-slot-allocation sslot))))
368 (res (copy-structure sslot)))))))
371 ;;; Early definitions of slot accessor creators.
373 ;;; Slot accessors must be generic functions, but ANSI does not seem
374 ;;; to specify any of them, and we cannot support it before end of
375 ;;; warm init. So we use ordinary functions inside SBCL, and switch to
376 ;;; GFs only at the end of building.
377 (declaim (notinline install-condition-slot-reader
378 install-condition-slot-writer))
379 (defun install-condition-slot-reader (name condition slot-name)
380 (declare (ignore condition))
381 (setf (fdefinition name)
383 (condition-reader-function condition slot-name))))
384 (defun install-condition-slot-writer (name condition slot-name)
385 (declare (ignore condition))
386 (setf (fdefinition name)
387 (lambda (new-value condition)
388 (condition-writer-function condition new-value slot-name))))
390 (defun %define-condition (name parent-types layout slots documentation
391 report default-initargs all-readers all-writers)
392 (with-single-package-locked-error
393 (:symbol name "defining ~A as a condition")
394 (%compiler-define-condition name parent-types layout all-readers all-writers)
395 (let ((class (find-classoid name)))
396 (setf (condition-classoid-slots class) slots)
397 (setf (condition-classoid-report class) report)
398 (setf (condition-classoid-default-initargs class) default-initargs)
399 (setf (fdocumentation name 'type) documentation)
403 ;; Set up reader and writer functions.
404 (let ((slot-name (condition-slot-name slot)))
405 (dolist (reader (condition-slot-readers slot))
406 (install-condition-slot-reader reader name slot-name))
407 (dolist (writer (condition-slot-writers slot))
408 (install-condition-slot-writer writer name slot-name))))
410 ;; Compute effective slots and set up the class and hairy slots
411 ;; (subsets of the effective slots.)
412 (let ((eslots (compute-effective-slots class))
415 (mapcar #'condition-classoid-default-initargs
416 (condition-classoid-cpl class)))))
417 (dolist (slot eslots)
418 (ecase (condition-slot-allocation slot)
420 (unless (condition-slot-cell slot)
421 (setf (condition-slot-cell slot)
422 (list (if (condition-slot-initform-p slot)
423 (let ((initform (condition-slot-initform slot)))
424 (if (functionp initform)
427 *empty-condition-slot*))))
428 (push slot (condition-classoid-class-slots class)))
430 (setf (condition-slot-allocation slot) :instance)
431 (when (or (functionp (condition-slot-initform slot))
432 (dolist (initarg (condition-slot-initargs slot) nil)
433 (when (functionp (getf e-def-initargs initarg))
435 (push slot (condition-classoid-hairy-slots class))))))))
438 (defmacro define-condition (name (&rest parent-types) (&rest slot-specs)
441 "DEFINE-CONDITION Name (Parent-Type*) (Slot-Spec*) Option*
442 Define NAME as a condition type. This new type inherits slots and its
443 report function from the specified PARENT-TYPEs. A slot spec is a list of:
444 (slot-name :reader <rname> :initarg <iname> {Option Value}*
446 The DEFINE-CLASS slot options :ALLOCATION, :INITFORM, [slot] :DOCUMENTATION
447 and :TYPE and the overall options :DEFAULT-INITARGS and
448 [type] :DOCUMENTATION are also allowed.
450 The :REPORT option is peculiar to DEFINE-CONDITION. Its argument is either
451 a string or a two-argument lambda or function name. If a function, the
452 function is called with the condition and stream to report the condition.
453 If a string, the string is printed.
455 Condition types are classes, but (as allowed by ANSI and not as described in
456 CLtL2) are neither STANDARD-OBJECTs nor STRUCTURE-OBJECTs. WITH-SLOTS and
457 SLOT-VALUE may not be used on condition objects."
458 (let* ((parent-types (or parent-types '(condition)))
459 (layout (find-condition-layout name parent-types))
462 (default-initargs ()))
464 (all-readers nil append)
465 (all-writers nil append))
466 (dolist (spec slot-specs)
467 (when (keywordp spec)
468 (warn "Keyword slot name indicates probable syntax error:~% ~S"
470 (let* ((spec (if (consp spec) spec (list spec)))
471 (slot-name (first spec))
472 (allocation :instance)
479 (do ((options (rest spec) (cddr options)))
481 (unless (and (consp options) (consp (cdr options)))
482 (error "malformed condition slot spec:~% ~S." spec))
483 (let ((arg (second options)))
484 (case (first options)
485 (:reader (readers arg))
486 (:writer (writers arg))
489 (writers `(setf ,arg)))
492 (error "more than one :INITFORM in ~S" spec))
495 (:initarg (initargs arg))
497 (setq allocation arg))
500 (error "more than one :DOCUMENTATION in ~S" spec))
501 (unless (stringp arg)
502 (error "slot :DOCUMENTATION argument is not a string: ~S"
504 (setq documentation arg))
507 (error "unknown slot option:~% ~S" (first options))))))
509 (all-readers (readers))
510 (all-writers (writers))
511 (slots `(make-condition-slot
513 :initargs ',(initargs)
516 :initform-p ',initform-p
517 :documentation ',documentation
519 ,(if (constantp initform)
521 `#'(lambda () ,initform)))))))
523 (dolist (option options)
524 (unless (consp option)
525 (error "bad option:~% ~S" option))
527 (:documentation (setq documentation (second option)))
529 (let ((arg (second option)))
532 `#'(lambda (condition stream)
533 (declare (ignore condition))
534 (write-string ,arg stream))
535 `#'(lambda (condition stream)
536 (funcall #',arg condition stream))))))
538 (do ((initargs (rest option) (cddr initargs)))
540 (let ((val (second initargs)))
541 (setq default-initargs
542 (list* `',(first initargs)
546 default-initargs)))))
548 (error "unknown option: ~S" (first option)))))
551 (eval-when (:compile-toplevel)
552 (%compiler-define-condition ',name ',parent-types ',layout
553 ',(all-readers) ',(all-writers)))
554 (eval-when (:load-toplevel :execute)
555 (%define-condition ',name
561 (list ,@default-initargs)
563 ',(all-writers)))))))
565 ;;;; DESCRIBE on CONDITIONs
567 ;;; a function to be used as the guts of DESCRIBE-OBJECT (CONDITION T)
568 ;;; eventually (once we get CLOS up and running so that we can define
570 (defun describe-condition (condition stream)
572 "~&~@<~S ~_is a ~S. ~_Its slot values are ~_~S.~:>~%"
576 (condition-actual-initargs condition)
577 (condition-assigned-slots condition))))
579 ;;;; various CONDITIONs specified by ANSI
581 (define-condition serious-condition (condition) ())
583 (define-condition error (serious-condition) ())
585 (define-condition warning (condition) ())
586 (define-condition style-warning (warning) ())
588 (defun simple-condition-printer (condition stream)
591 (simple-condition-format-control condition)
592 (simple-condition-format-arguments condition)))
594 (define-condition simple-condition ()
595 ((format-control :reader simple-condition-format-control
596 :initarg :format-control
597 :type format-control)
598 (format-arguments :reader simple-condition-format-arguments
599 :initarg :format-arguments
602 (:report simple-condition-printer))
604 (define-condition simple-warning (simple-condition warning) ())
606 (define-condition simple-error (simple-condition error) ())
608 ;;; not specified by ANSI, but too useful not to have around.
609 (define-condition simple-style-warning (simple-condition style-warning) ())
611 (define-condition storage-condition (serious-condition) ())
613 (define-condition type-error (error)
614 ((datum :reader type-error-datum :initarg :datum)
615 (expected-type :reader type-error-expected-type :initarg :expected-type))
617 (lambda (condition stream)
619 "~@<The value ~2I~:_~S ~I~_is not of type ~2I~_~S.~:>"
620 (type-error-datum condition)
621 (type-error-expected-type condition)))))
623 (define-condition simple-type-error (simple-condition type-error) ())
625 (define-condition program-error (error) ())
626 (define-condition parse-error (error) ())
627 (define-condition control-error (error) ())
628 (define-condition stream-error (error)
629 ((stream :reader stream-error-stream :initarg :stream)))
631 (define-condition end-of-file (stream-error) ()
633 (lambda (condition stream)
636 (stream-error-stream condition)))))
638 (define-condition file-error (error)
639 ((pathname :reader file-error-pathname :initarg :pathname))
641 (lambda (condition stream)
642 (format stream "error on file ~S" (file-error-pathname condition)))))
644 (define-condition package-error (error)
645 ((package :reader package-error-package :initarg :package)))
647 (define-condition cell-error (error)
648 ((name :reader cell-error-name :initarg :name)))
650 (define-condition unbound-variable (cell-error) ()
652 (lambda (condition stream)
654 "The variable ~S is unbound."
655 (cell-error-name condition)))))
657 (define-condition undefined-function (cell-error) ()
659 (lambda (condition stream)
661 "The function ~S is undefined."
662 (cell-error-name condition)))))
664 (define-condition special-form-function (undefined-function) ()
666 (lambda (condition stream)
668 "Cannot FUNCALL the SYMBOL-FUNCTION of special operator ~S."
669 (cell-error-name condition)))))
671 (define-condition arithmetic-error (error)
672 ((operation :reader arithmetic-error-operation
675 (operands :reader arithmetic-error-operands
677 (:report (lambda (condition stream)
679 "arithmetic error ~S signalled"
681 (when (arithmetic-error-operation condition)
683 "~%Operation was ~S, operands ~S."
684 (arithmetic-error-operation condition)
685 (arithmetic-error-operands condition))))))
687 (define-condition division-by-zero (arithmetic-error) ())
688 (define-condition floating-point-overflow (arithmetic-error) ())
689 (define-condition floating-point-underflow (arithmetic-error) ())
690 (define-condition floating-point-inexact (arithmetic-error) ())
691 (define-condition floating-point-invalid-operation (arithmetic-error) ())
693 (define-condition print-not-readable (error)
694 ((object :reader print-not-readable-object :initarg :object))
696 (lambda (condition stream)
697 (let ((obj (print-not-readable-object condition))
699 (format stream "~S cannot be printed readably." obj)))))
701 (define-condition reader-error (parse-error stream-error)
703 :reader reader-error-format-control
704 :initarg :format-control)
706 :reader reader-error-format-arguments
707 :initarg :format-arguments
710 (lambda (condition stream)
711 (let* ((error-stream (stream-error-stream condition))
712 (pos (file-position-or-nil-for-error error-stream)))
715 (< pos sb!xc:array-dimension-limit)
716 ;; KLUDGE: lseek() (which is what FILE-POSITION
717 ;; reduces to on file-streams) is undefined on
718 ;; "some devices", which in practice means that it
719 ;; can claim to succeed on /dev/stdin on Darwin
720 ;; and Solaris. This is obviously bad news,
721 ;; because the READ-SEQUENCE below will then
722 ;; block, not complete, and the report will never
723 ;; be printed. As a workaround, we exclude
724 ;; interactive streams from this attempt to report
725 ;; positions. -- CSR, 2003-08-21
726 (not (interactive-stream-p error-stream))
727 (file-position error-stream :start))
730 :element-type (stream-element-type
732 (when (= pos (read-sequence string error-stream))
733 (setq lineno (1+ (count #\Newline string))
735 (or (position #\Newline string :from-end t) -1)
737 (file-position-or-nil-for-error error-stream pos))
739 "READER-ERROR ~@[at ~W ~]~
740 ~@[(line ~W~]~@[, column ~W) ~]~
742 pos lineno colno error-stream
743 (reader-error-format-control condition)
744 (reader-error-format-arguments condition)))))))
746 ;;;; special SBCL extension conditions
748 ;;; an error apparently caused by a bug in SBCL itself
750 ;;; Note that we don't make any serious effort to use this condition
751 ;;; for *all* errors in SBCL itself. E.g. type errors and array
752 ;;; indexing errors can occur in functions called from SBCL code, and
753 ;;; will just end up as ordinary TYPE-ERROR or invalid index error,
754 ;;; because the signalling code has no good way to know that the
755 ;;; underlying problem is a bug in SBCL. But in the fairly common case
756 ;;; that the signalling code does know that it's found a bug in SBCL,
757 ;;; this condition is appropriate, reusing boilerplate and helping
758 ;;; users to recognize it as an SBCL bug.
759 (define-condition bug (simple-error)
762 (lambda (condition stream)
765 (simple-condition-format-control condition)
766 (simple-condition-format-arguments condition)
767 "~@<This is probably a bug in SBCL itself. (Alternatively, ~
768 SBCL might have been corrupted by bad user code, e.g. by an ~
769 undefined Lisp operation like ~S, or by stray pointers from ~
770 alien code or from unsafe Lisp code; or there might be a bug ~
771 in the OS or hardware that SBCL is running on.) If it seems to ~
772 be a bug in SBCL itself, the maintainers would like to know ~
773 about it. Bug reports are welcome on the SBCL ~
774 mailing lists, which you can find at ~
775 <http://sbcl.sourceforge.net/>.~:@>"
776 '((fmakunbound 'compile))))))
778 ;;; a condition for use in stubs for operations which aren't supported
779 ;;; on some platforms
781 ;;; E.g. in sbcl-0.7.0.5, it might be appropriate to do something like
782 ;;; #-(or freebsd linux)
783 ;;; (defun load-foreign (&rest rest)
784 ;;; (error 'unsupported-operator :name 'load-foreign))
785 ;;; #+(or freebsd linux)
786 ;;; (defun load-foreign ... actual definition ...)
787 ;;; By signalling a standard condition in this case, we make it
788 ;;; possible for test code to distinguish between (1) intentionally
789 ;;; unimplemented and (2) unintentionally just screwed up somehow.
790 ;;; (Before this condition was defined, test code tried to deal with
791 ;;; this by checking for FBOUNDP, but that didn't work reliably. In
792 ;;; sbcl-0.7.0, a a package screwup left the definition of
793 ;;; LOAD-FOREIGN in the wrong package, so it was unFBOUNDP even on
794 ;;; architectures where it was supposed to be supported, and the
795 ;;; regression tests cheerfully passed because they assumed that
796 ;;; unFBOUNDPness meant they were running on an system which didn't
797 ;;; support the extension.)
798 (define-condition unsupported-operator (simple-error) ())
801 ;;; (:ansi-cl :function remove)
802 ;;; (:ansi-cl :section (a b c))
803 ;;; (:ansi-cl :glossary "similar")
805 ;;; (:sbcl :node "...")
806 ;;; (:sbcl :variable *ed-functions*)
808 ;;; FIXME: this is not the right place for this.
809 (defun print-reference (reference stream)
810 (ecase (car reference)
812 (format stream "AMOP")
814 (destructuring-bind (type data) (cdr reference)
816 (:generic-function (format stream "Generic Function ~S" data))
817 (:section (format stream "Section ~{~D~^.~}" data)))))
819 (format stream "The ANSI Standard")
821 (destructuring-bind (type data) (cdr reference)
823 (:function (format stream "Function ~S" data))
824 (:special-operator (format stream "Special Operator ~S" data))
825 (:macro (format stream "Macro ~S" data))
826 (:section (format stream "Section ~{~D~^.~}" data))
827 (:glossary (format stream "Glossary entry for ~S" data))
828 (:issue (format stream "writeup for Issue ~A" data)))))
830 (format stream "The SBCL Manual")
832 (destructuring-bind (type data) (cdr reference)
834 (:node (format stream "Node ~S" data))
835 (:variable (format stream "Variable ~S" data))
836 (:function (format stream "Function ~S" data)))))
837 ;; FIXME: other documents (e.g. CLIM, Franz documentation :-)
839 (define-condition reference-condition ()
840 ((references :initarg :references :reader reference-condition-references)))
841 (defvar *print-condition-references* t)
842 (def!method print-object :around ((o reference-condition) s)
844 (unless (or *print-escape* *print-readably*)
845 (when (and *print-condition-references*
846 (reference-condition-references o))
847 (format s "~&See also:~%")
848 (pprint-logical-block (s nil :per-line-prefix " ")
849 (do* ((rs (reference-condition-references o) (cdr rs))
850 (r (car rs) (car rs)))
852 (print-reference r s)
853 (unless (null (cdr rs))
856 (define-condition duplicate-definition (reference-condition warning)
857 ((name :initarg :name :reader duplicate-definition-name))
858 (:report (lambda (c s)
859 (format s "~@<Duplicate definition for ~S found in ~
861 (duplicate-definition-name c))))
862 (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
864 (define-condition package-at-variance (reference-condition simple-warning)
866 (:default-initargs :references (list '(:ansi-cl :macro defpackage))))
868 (define-condition defconstant-uneql (reference-condition error)
869 ((name :initarg :name :reader defconstant-uneql-name)
870 (old-value :initarg :old-value :reader defconstant-uneql-old-value)
871 (new-value :initarg :new-value :reader defconstant-uneql-new-value))
873 (lambda (condition stream)
875 "~@<The constant ~S is being redefined (from ~S to ~S)~@:>"
876 (defconstant-uneql-name condition)
877 (defconstant-uneql-old-value condition)
878 (defconstant-uneql-new-value condition))))
879 (:default-initargs :references (list '(:ansi-cl :macro defconstant)
880 '(:sbcl :node "Idiosyncrasies"))))
882 (define-condition array-initial-element-mismatch
883 (reference-condition simple-warning)
887 '(:ansi-cl :function make-array)
888 '(:ansi-cl :function sb!xc:upgraded-array-element-type))))
890 (define-condition displaced-to-array-too-small-error
891 (reference-condition simple-error)
894 :references (list '(:ansi-cl :function adjust-array))))
896 (define-condition type-warning (reference-condition simple-warning)
898 (:default-initargs :references (list '(:sbcl :node "Handling of Types"))))
900 (define-condition local-argument-mismatch (reference-condition simple-warning)
902 (:default-initargs :references (list '(:ansi-cl :section (3 2 2 3)))))
904 (define-condition format-args-mismatch (reference-condition)
906 (:default-initargs :references (list '(:ansi-cl :section (22 3 10 2)))))
908 (define-condition format-too-few-args-warning
909 (format-args-mismatch simple-warning)
911 (define-condition format-too-many-args-warning
912 (format-args-mismatch simple-style-warning)
915 (define-condition extension-failure (reference-condition simple-error)
918 (define-condition structure-initarg-not-keyword
919 (reference-condition simple-style-warning)
921 (:default-initargs :references (list '(:ansi-cl :section (2 4 8 13)))))
926 (define-condition package-lock-violation (reference-condition package-error)
927 ((format-control :initform nil :initarg :format-control
928 :reader package-error-format-control)
929 (format-arguments :initform nil :initarg :format-arguments
930 :reader package-error-format-arguments))
932 (lambda (condition stream)
933 (let ((control (package-error-format-control condition)))
935 (apply #'format stream
936 (format nil "~~@<Lock on package ~A violated when ~A.~~:@>"
937 (package-name (package-error-package condition))
939 (package-error-format-arguments condition))
940 (format stream "~@<Lock on package ~A violated.~:@>"
941 (package-name (package-error-package condition)))))))
942 ;; no :default-initargs -- reference-stuff provided by the
943 ;; signalling form in target-package.lisp
946 "Subtype of CL:PACKAGE-ERROR. A subtype of this error is signalled
947 when a package-lock is violated."))
949 (define-condition package-locked-error (package-lock-violation) ()
952 "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
953 signalled when an operation on a package violates a package lock."))
955 (define-condition symbol-package-locked-error (package-lock-violation)
956 ((symbol :initarg :symbol :reader package-locked-error-symbol))
959 "Subtype of SB-EXT:PACKAGE-LOCK-VIOLATION. An error of this type is
960 signalled when an operation on a symbol violates a package lock. The
961 symbol that caused the violation is accessed by the function
962 SB-EXT:PACKAGE-LOCKED-ERROR-SYMBOL."))
966 (define-condition undefined-alien-error (error) ())
968 (define-condition undefined-alien-variable-error (undefined-alien-error) ()
970 (lambda (condition stream)
971 (declare (ignore condition))
972 (format stream "Attempt to access an undefined alien variable."))))
974 (define-condition undefined-alien-function-error (undefined-alien-error) ()
976 (lambda (condition stream)
977 (declare (ignore condition))
978 (format stream "Attempt to call an undefined alien function."))))
981 ;;;; various other (not specified by ANSI) CONDITIONs
983 ;;;; These might logically belong in other files; they're here, after
984 ;;;; setup of CONDITION machinery, only because that makes it easier to
985 ;;;; get cold init to work.
987 ;;; OAOOM warning: see cross-condition.lisp
988 (define-condition encapsulated-condition (condition)
989 ((condition :initarg :condition :reader encapsulated-condition)))
991 (define-condition values-type-error (type-error)
994 (lambda (condition stream)
996 "~@<The values set ~2I~:_[~{~S~^ ~}] ~I~_is not of type ~2I~_~S.~:>"
997 (type-error-datum condition)
998 (type-error-expected-type condition)))))
1000 ;;; KLUDGE: a condition for floating point errors when we can't or
1001 ;;; won't figure out what type they are. (In FreeBSD and OpenBSD we
1002 ;;; don't know how, at least as of sbcl-0.6.7; in Linux we probably
1003 ;;; know how but the old code was broken by the conversion to POSIX
1004 ;;; signal handling and hasn't been fixed as of sbcl-0.6.7.)
1006 ;;; FIXME: Perhaps this should also be a base class for all
1007 ;;; floating point exceptions?
1008 (define-condition floating-point-exception (arithmetic-error)
1009 ((flags :initarg :traps
1011 :reader floating-point-exception-traps))
1012 (:report (lambda (condition stream)
1014 "An arithmetic error ~S was signalled.~%"
1015 (type-of condition))
1016 (let ((traps (floating-point-exception-traps condition)))
1019 "Trapping conditions are: ~%~{ ~S~^~}~%"
1022 "No traps are enabled? How can this be?"
1025 (define-condition index-too-large-error (type-error)
1028 (lambda (condition stream)
1030 "The index ~S is too large."
1031 (type-error-datum condition)))))
1033 (define-condition bounding-indices-bad-error (reference-condition type-error)
1034 ((object :reader bounding-indices-bad-object :initarg :object))
1036 (lambda (condition stream)
1037 (let* ((datum (type-error-datum condition))
1040 (object (bounding-indices-bad-object condition)))
1044 "The bounding indices ~S and ~S are bad ~
1045 for a sequence of length ~S."
1046 start end (length object)))
1048 ;; from WITH-ARRAY-DATA
1050 "The START and END parameters ~S and ~S are ~
1051 bad for an array of total size ~S."
1052 start end (array-total-size object)))))))
1055 (list '(:ansi-cl :glossary "bounding index designator")
1056 '(:ansi-cl :issue "SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR"))))
1058 (define-condition nil-array-accessed-error (reference-condition type-error)
1060 (:report (lambda (condition stream)
1061 (declare (ignore condition))
1063 "An attempt to access an array of element-type ~
1064 NIL was made. Congratulations!")))
1066 :references (list '(:ansi-cl :function sb!xc:upgraded-array-element-type)
1067 '(:ansi-cl :section (15 1 2 1))
1068 '(:ansi-cl :section (15 1 2 2)))))
1070 (define-condition io-timeout (stream-error)
1071 ((direction :reader io-timeout-direction :initarg :direction))
1073 (lambda (condition stream)
1074 (declare (type stream stream))
1076 "I/O timeout ~(~A~)ing ~S"
1077 (io-timeout-direction condition)
1078 (stream-error-stream condition)))))
1080 (define-condition namestring-parse-error (parse-error)
1081 ((complaint :reader namestring-parse-error-complaint :initarg :complaint)
1082 (args :reader namestring-parse-error-args :initarg :args :initform nil)
1083 (namestring :reader namestring-parse-error-namestring :initarg :namestring)
1084 (offset :reader namestring-parse-error-offset :initarg :offset))
1086 (lambda (condition stream)
1088 "parse error in namestring: ~?~% ~A~% ~V@T^"
1089 (namestring-parse-error-complaint condition)
1090 (namestring-parse-error-args condition)
1091 (namestring-parse-error-namestring condition)
1092 (namestring-parse-error-offset condition)))))
1094 (define-condition simple-package-error (simple-condition package-error) ())
1096 (define-condition reader-package-error (reader-error) ())
1098 (define-condition reader-eof-error (end-of-file)
1099 ((context :reader reader-eof-error-context :initarg :context))
1101 (lambda (condition stream)
1103 "unexpected end of file on ~S ~A"
1104 (stream-error-stream condition)
1105 (reader-eof-error-context condition)))))
1107 (define-condition reader-impossible-number-error (reader-error)
1108 ((error :reader reader-impossible-number-error-error :initarg :error))
1110 (lambda (condition stream)
1111 (let ((error-stream (stream-error-stream condition)))
1112 (format stream "READER-ERROR ~@[at ~W ~]on ~S:~%~?~%Original error: ~A"
1113 (file-position-or-nil-for-error error-stream) error-stream
1114 (reader-error-format-control condition)
1115 (reader-error-format-arguments condition)
1116 (reader-impossible-number-error-error condition))))))
1118 (define-condition timeout (serious-condition) ())
1120 ;;; Single stepping conditions
1122 (define-condition step-condition ()
1123 ((form :initarg :form :reader step-condition-form))
1125 (:documentation "Common base class of single-stepping conditions.
1126 STEP-CONDITION-FORM holds a string representation of the form being
1130 (setf (fdocumentation 'step-condition-form 'function)
1131 "Form associated with the STEP-CONDITION.")
1133 (define-condition step-form-condition (step-condition)
1134 ((source-path :initarg :source-path :reader step-condition-source-path)
1135 (pathname :initarg :pathname :reader step-condition-pathname))
1137 (:documentation "Condition signalled by code compiled with
1138 single-stepping information when about to execute a form.
1139 STEP-CONDITION-FORM holds the form, STEP-CONDITION-PATHNAME holds the
1140 pathname of the original file or NIL, and STEP-CONDITION-SOURCE-PATH
1141 holds the source-path to the original form within that file or NIL.
1142 Associated with this condition are always the restarts STEP-INTO,
1143 STEP-NEXT, and STEP-CONTINUE."))
1146 (setf (fdocumentation 'step-condition-source-path 'function)
1147 "Source-path of the original form associated with the
1148 STEP-FORM-CONDITION or NIL."
1149 (fdocumentation 'step-condition-pathname 'function)
1150 "Pathname of the original source-file associated with the
1151 STEP-FORM-CONDITION or NIL.")
1153 (define-condition step-result-condition (step-condition)
1154 ((result :initarg :result :reader step-condition-result)))
1157 (setf (fdocumentation 'step-condition-result 'function)
1158 "Return values associated with STEP-VALUES-CONDITION as a list,
1159 or the variable value associated with STEP-VARIABLE-CONDITION.")
1161 (define-condition step-values-condition (step-result-condition)
1164 (:documentation "Condition signalled by code compiled with
1165 single-stepping information after executing a form.
1166 STEP-CONDITION-FORM holds the form, and STEP-CONDITION-RESULT holds
1167 the values returned by the form as a list. No associated restarts."))
1169 (define-condition step-variable-condition (step-result-condition)
1172 (:documentation "Condition signalled by code compiled with
1173 single-stepping information when referencing a variable.
1174 STEP-CONDITION-FORM hold the symbol, and STEP-CONDITION-RESULT holds
1175 the value of the variable. No associated restarts."))
1178 ;;;; restart definitions
1180 (define-condition abort-failure (control-error) ()
1182 "An ABORT restart was found that failed to transfer control dynamically."))
1184 (defun abort (&optional condition)
1186 "Transfer control to a restart named ABORT, signalling a CONTROL-ERROR if
1188 (invoke-restart (find-restart-or-control-error 'abort condition))
1189 ;; ABORT signals an error in case there was a restart named ABORT
1190 ;; that did not transfer control dynamically. This could happen with
1192 (error 'abort-failure))
1194 (defun muffle-warning (&optional condition)
1196 "Transfer control to a restart named MUFFLE-WARNING, signalling a
1197 CONTROL-ERROR if none exists."
1198 (invoke-restart (find-restart-or-control-error 'muffle-warning condition)))
1200 (macrolet ((define-nil-returning-restart (name args doc)
1201 #!-sb-doc (declare (ignore doc))
1202 `(defun ,name (,@args &optional condition)
1204 ;; FIXME: Perhaps this shared logic should be pulled out into
1205 ;; FLET MAYBE-INVOKE-RESTART? See whether it shrinks code..
1206 (let ((restart (find-restart ',name condition)))
1208 (invoke-restart restart ,@args))))))
1209 (define-nil-returning-restart continue ()
1210 "Transfer control to a restart named CONTINUE, or return NIL if none exists.")
1211 (define-nil-returning-restart store-value (value)
1212 "Transfer control and VALUE to a restart named STORE-VALUE, or return NIL if
1214 (define-nil-returning-restart use-value (value)
1215 "Transfer control and VALUE to a restart named USE-VALUE, or return NIL if
1218 ;;; single-stepping restarts
1220 (macrolet ((def (name doc)
1221 #!-sb-doc (declare (ignore doc))
1222 `(defun ,name (condition)
1224 (invoke-restart (find-restart-or-control-error ',name condition)))))
1226 "Transfers control to the STEP-CONTINUE restart associated with
1227 the condition, continuing execution without stepping. Signals a
1228 CONTROL-ERROR if the restart does not exist.")
1230 "Transfers control to the STEP-NEXT restart associated with the
1231 condition, executing the current form without stepping and continuing
1232 stepping with the next form. Signals CONTROL-ERROR is the restart does
1235 "Transfers control to the STEP-INTO restart associated with the
1236 condition, stepping into the current form. Signals a CONTROL-ERROR is
1237 the restart does not exist."))
1239 (/show0 "condition.lisp end of file")