Move SFUNCTION type earlier, use it more.
[sbcl.git] / src / compiler / defconstant.lisp
blob4ff3163889eeb2fc865f285fd4a08feb9f912eca
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!IMPL")
12 ;;; We need a mechanism different from the usual SYMBOL-VALUE during cross-
13 ;;; compilation so we don't clobber the host Lisp's manifest constants.
14 ;;; This was formerly done using the globaldb, so emulate exactly the
15 ;;; calls to UNCROSS in (SETF INFO) and INFO because that works just fine.
16 ;;; Also, as noted in 'constantp.lisp'
17 ;;; "KLUDGE: superficially, this might look good enough..."
18 ;;; we should enforce that we not wrongly look at a host constant where
19 ;;; a target constant is intended. This specialized accessor, in lieu of INFO
20 ;;; facilitates implementing something like that, though in fact ir1tran is
21 ;;; already able to generate some warnings about host constant usage.
22 #+sb-xc-host
23 (progn
24 (defun (setf sb!c::xc-constant-value) (newval sym)
25 (setf (get (uncross sym) :sb-xc-constant-val) newval))
26 (defun sb!c::xc-constant-value (sym) ; return 2 values as does (INFO ...)
27 (multiple-value-bind (indicator value foundp)
28 (get-properties (symbol-plist (uncross sym)) '(:sb-xc-constant-val))
29 (declare (ignore indicator))
30 (values value (not (null foundp))))))
32 (declaim (ftype (sfunction (symbol t &optional t t) null)
33 about-to-modify-symbol-value))
34 ;;; the guts of DEFCONSTANT
36 (defun sb!c::%defconstant (name value source-location &optional (doc nil docp))
37 #+sb-xc-host (declare (ignore doc docp))
38 (unless (symbolp name)
39 (error "The constant name is not a symbol: ~S" name))
40 (with-single-package-locked-error (:symbol name
41 "defining ~s as a constant")
42 (when (looks-like-name-of-special-var-p name)
43 (style-warn 'asterisks-around-constant-variable-name
44 :format-control "Defining ~S as a constant"
45 :format-arguments (list name)))
46 (when source-location
47 (setf (info :source-location :constant name) source-location))
48 (let ((kind (info :variable :kind name)))
49 (case kind
50 (:constant
51 ;; Note: This behavior (discouraging any non-EQL modification)
52 ;; is unpopular, but it is specified by ANSI (i.e. ANSI says a
53 ;; non-EQL change has undefined consequences). If people really
54 ;; want bindings which are constant in some sense other than
55 ;; EQL, I suggest either just using DEFVAR (which is usually
56 ;; appropriate, despite the un-mnemonic name), or defining
57 ;; something like the DEFCONSTANT-EQX macro used in SBCL (which
58 ;; is occasionally more appropriate). -- WHN 2001-12-21
59 (if (boundp name)
60 (if (typep name '(or boolean keyword))
61 ;; Non-continuable error.
62 (about-to-modify-symbol-value name 'defconstant)
63 (let ((old (symbol-value name)))
64 (unless (eql value old)
65 (multiple-value-bind (ignore aborted)
66 (with-simple-restart (abort "Keep the old value.")
67 (cerror "Go ahead and change the value."
68 'defconstant-uneql
69 :name name
70 :old-value old
71 :new-value value))
72 (declare (ignore ignore))
73 (when aborted
74 (return-from sb!c::%defconstant name))))))
75 (warn "redefining a MAKUNBOUND constant: ~S" name)))
76 (:unknown
77 ;; (This is OK -- undefined variables are of this kind. So we
78 ;; don't warn or error or anything, just fall through.)
80 (t (warn "redefining ~(~A~) ~S to be a constant" kind name)))))
81 ;; We ought to be consistent in treating any change of :VARIABLE :KIND
82 ;; as a continuable error. The above CASE expression pre-dates the
83 ;; existence of symbol-macros (I believe), but at a bare minimum,
84 ;; INFO should return NIL for its second value if requesting the
85 ;; :macro-expansion of something that is getting defined as constant.
86 (clear-info :variable :macro-expansion name)
87 (clear-info :source-location :symbol-macro name)
88 #-sb-xc-host
89 (progn
90 (when docp
91 (setf (fdocumentation name 'variable) doc))
92 (%set-symbol-value name value))
93 #+sb-xc-host
94 (progn
95 ;; Redefining our cross-compilation host's CL symbols would be poor form.
97 ;; FIXME: Having to check this and then not treat it as a fatal error
98 ;; seems like a symptom of things being pretty broken. It's also a problem
99 ;; in and of itself, since it makes it too easy for cases of using the
100 ;; cross-compilation host Lisp's CL constant values in the target Lisp to
101 ;; slip by. I got backed into this because the cross-compiler translates
102 ;; DEFCONSTANT SB!XC:FOO into DEFCONSTANT CL:FOO. It would be good to
103 ;; unscrew the cross-compilation package hacks so that that translation
104 ;; doesn't happen. Perhaps: * Replace SB-XC with SB-CL. SB-CL exports all
105 ;; the symbols which ANSI requires to be exported from CL. * Make a
106 ;; nickname SB!CL which behaves like SB!XC. * Go through the
107 ;; loaded-on-the-host code making every target definition be in SB-CL.
108 ;; E.g. SB!XC:DEFMACRO DEFCONSTANT becomes SB!XC:DEFMACRO SB!CL:DEFCONSTANT.
109 ;; * Make IN-TARGET-COMPILATION-MODE do UNUSE-PACKAGE CL and
110 ;; USE-PACKAGE SB-CL in each of the target packages (then undo it
111 ;; on exit). * Make the cross-compiler's implementation of EVAL-WHEN
112 ;; (:COMPILE-TOPLEVEL) do UNCROSS. (This may not require any change.) *
113 ;; Hack GENESIS as necessary so that it outputs SB-CL stuff as COMMON-LISP
114 ;; stuff. * Now the code here can assert that the symbol being defined
115 ;; isn't in the cross-compilation host's CL package.
116 (unless (eql (find-symbol (symbol-name name) :cl) name)
117 ;; KLUDGE: In the cross-compiler, we use the cross-compilation host's
118 ;; DEFCONSTANT macro instead of just (SETF SYMBOL-VALUE), in order to
119 ;; get whatever blessing the cross-compilation host may expect for a
120 ;; global (SETF SYMBOL-VALUE). (CMU CL, at least around 2.4.19,
121 ;; generated full WARNINGs for code -- e.g. DEFTYPE expanders -- which
122 ;; referred to symbols which had been set by (SETF SYMBOL-VALUE). I
123 ;; doubt such warnings are ANSI-compliant, but I'm not sure, so I've
124 ;; written this in a way that CMU CL will tolerate and which ought to
125 ;; work elsewhere too.) -- WHN 2001-03-24
126 (eval `(defconstant ,name ',value)))
127 ;; It would certainly be awesome if this was only needed for symbols
128 ;; in CL. Unfortunately, that is not the case. Maybe some are moved
129 ;; back in CL later on?
130 (setf (sb!c::xc-constant-value name) value))
131 (setf (info :variable :kind name) :constant)
132 name)