0.6.12.37;
[sbcl.git] / src / compiler / compiler-error.lisp
1 ;;;; the bare essentials of compiler error handling
2 ;;;;
3 ;;;; (Logically, this might belong in early-c.lisp, since it's stuff
4 ;;;; which might as well be visible to all compiler code. However,
5 ;;;; physically its DEFINE-CONDITION forms depend on the condition
6 ;;;; system being set up before it can be cold loaded, so we keep it
7 ;;;; in this separate, loaded-later file instead of in early-c.lisp.)
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 (in-package "SB!C")
19
20 ;;;; error-handling definitions which are easy to define early and
21 ;;;; which are nice to have visible everywhere
22
23 ;;; a function that is called to unwind out of COMPILER-ERROR
24 (declaim (type (function () nil) *compiler-error-bailout*))
25 (defvar *compiler-error-bailout*
26   (lambda () (error "COMPILER-ERROR with no bailout")))
27
28 ;;; We have a separate COMPILER-ERROR condition to allow us to
29 ;;; distinguish internal compiler errors from user errors.
30 ;;; Non-compiler errors put us in the debugger.
31 (define-condition compiler-error (simple-error) ())
32
33 ;;; Signal the appropriate condition. COMPILER-ERROR calls the bailout
34 ;;; function so that it never returns (but compilation continues).
35 ;;; COMPILER-ABORT falls through to the default error handling, so
36 ;;; compilation terminates. 
37 (declaim (ftype (function (string &rest t) nil) compiler-error compiler-abort))
38 (declaim (ftype (function (string &rest t) (values))
39                 compiler-warning compiler-style-warning))
40 (defun compiler-abort (format-string &rest format-args)
41   (error 'compiler-error
42          :format-control format-string
43          :format-arguments format-args))
44 (defun compiler-error (format-string &rest format-args)
45   (cerror "Replace form with call to ERROR."
46           'compiler-error
47           :format-control format-string
48           :format-arguments format-args)
49   (funcall *compiler-error-bailout*)
50   ;; FIXME: It might be nice to define a BUG or OOPS function for "shouldn't
51   ;; happen" cases like this.
52   (error "internal error, control returned from *COMPILER-ERROR-BAILOUT*"))
53 (defun compiler-warning (format-string &rest format-args)
54   (apply #'warn format-string format-args)
55   (values))
56 (defun compiler-style-warning (format-string &rest format-args)
57   (apply #'style-warn format-string format-args)
58   (values))
59
60 ;;; the condition of COMPILE-FILE being unable to READ from the
61 ;;; source file
62 ;;;
63 ;;; This is not a COMPILER-ERROR, since we don't try to recover from
64 ;;; it and keep chugging along, but instead immediately bail out of
65 ;;; the entire COMPILE-FILE.
66 ;;;
67 ;;; (The old CMU CL code did try to recover from this condition, but
68 ;;; the code for doing that was messy and didn't always work right.
69 ;;; Since in Common Lisp the simple act of reading and compiling code
70 ;;; (even without ever loading the compiled result) can have side
71 ;;; effects, it's a little scary to go on reading code when you're
72 ;;; deeply confused, so we violate what'd otherwise be good compiler
73 ;;; practice by not trying to recover from this error and bailing out
74 ;;; instead.)
75 (define-condition input-error-in-compile-file (error)
76   (;; the original error which was trapped to produce this condition
77    (error :reader input-error-in-compile-file-error
78           :initarg :error)
79    ;; the position where the bad READ began, or NIL if unavailable,
80    ;; redundant, or irrelevant
81    (position :reader input-error-in-compile-file-position
82              :initarg :position
83              :initform nil))
84   (:report
85    (lambda (condition stream)
86      (format stream
87              "~@<~S failure in ~S~@[ at character ~D~]: ~2I~_~A~:>"
88              'read
89              'compile-file
90              (input-error-in-compile-file-position condition)
91              (input-error-in-compile-file-error condition)))))