Enforce consistency between DEFINE-COLD-FOP and DEFINE-FOP.
[sbcl.git] / src / compiler / defconstant.lisp
blob9a907078c7a2a3fc2f3c85f94cf87f9ce2f86e17
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 (def!macro sb!xc:defconstant (name value &optional documentation)
33 #!+sb-doc
34 "Define a global constant, saying that the value is constant and may be
35 compiled into code. If the variable already has a value, and this is not
36 EQL to the new value, the code is not portable (undefined behavior). The
37 third argument is an optional documentation string for the variable."
38 `(eval-when (:compile-toplevel :load-toplevel :execute)
39 (sb!c::%defconstant ',name ,value ',documentation
40 (sb!c:source-location))))
42 ;;; the guts of DEFCONSTANT
43 (defun sb!c::%defconstant (name value doc source-location)
44 (unless (symbolp name)
45 (error "The constant name is not a symbol: ~S" name))
46 (when (looks-like-name-of-special-var-p name)
47 (style-warn 'asterisks-around-constant-variable-name
48 :format-control "defining ~S as a constant"
49 :format-arguments (list name)))
50 (sb!c:with-source-location (source-location)
51 (setf (info :source-location :constant name) source-location))
52 (let ((kind (info :variable :kind name)))
53 (case kind
54 (:constant
55 ;; Note: This behavior (discouraging any non-EQL modification)
56 ;; is unpopular, but it is specified by ANSI (i.e. ANSI says a
57 ;; non-EQL change has undefined consequences). If people really
58 ;; want bindings which are constant in some sense other than
59 ;; EQL, I suggest either just using DEFVAR (which is usually
60 ;; appropriate, despite the un-mnemonic name), or defining
61 ;; something like the DEFCONSTANT-EQX macro used in SBCL (which
62 ;; is occasionally more appropriate). -- WHN 2001-12-21
63 (if (boundp name)
64 (if (typep name '(or boolean keyword))
65 ;; Non-continuable error.
66 (about-to-modify-symbol-value name 'defconstant)
67 (let ((old (symbol-value name)))
68 (unless (eql value old)
69 (multiple-value-bind (ignore aborted)
70 (with-simple-restart (abort "Keep the old value.")
71 (cerror "Go ahead and change the value."
72 'defconstant-uneql
73 :name name
74 :old-value old
75 :new-value value))
76 (declare (ignore ignore))
77 (when aborted
78 (return-from sb!c::%defconstant name))))))
79 (warn "redefining a MAKUNBOUND constant: ~S" name)))
80 (:unknown
81 ;; (This is OK -- undefined variables are of this kind. So we
82 ;; don't warn or error or anything, just fall through.)
84 (t (warn "redefining ~(~A~) ~S to be a constant" kind name))))
85 ;; We ought to be consistent in treating any change of :VARIABLE :KIND
86 ;; as a continuable error. The above CASE expression pre-dates the
87 ;; existence of symbol-macros (I believe), but at a bare minimum,
88 ;; INFO should return NIL for its second value if requesting the
89 ;; :macro-expansion of something that is getting defined as constant.
90 (clear-info :variable :macro-expansion name)
91 (clear-info :source-location :symbol-macro name)
92 (when doc
93 (setf (fdocumentation name 'variable) doc))
94 #-sb-xc-host
95 (%set-symbol-value name value)
96 #+sb-xc-host
97 (progn
98 ;; Redefining our cross-compilation host's CL symbols would be poor form.
100 ;; FIXME: Having to check this and then not treat it as a fatal error
101 ;; seems like a symptom of things being pretty broken. It's also a problem
102 ;; in and of itself, since it makes it too easy for cases of using the
103 ;; cross-compilation host Lisp's CL constant values in the target Lisp to
104 ;; slip by. I got backed into this because the cross-compiler translates
105 ;; DEFCONSTANT SB!XC:FOO into DEFCONSTANT CL:FOO. It would be good to
106 ;; unscrew the cross-compilation package hacks so that that translation
107 ;; doesn't happen. Perhaps: * Replace SB-XC with SB-CL. SB-CL exports all
108 ;; the symbols which ANSI requires to be exported from CL. * Make a
109 ;; nickname SB!CL which behaves like SB!XC. * Go through the
110 ;; loaded-on-the-host code making every target definition be in SB-CL.
111 ;; E.g. DEFMACRO-MUNDANELY DEFCONSTANT becomes DEFMACRO-MUNDANELY
112 ;; SB!CL:DEFCONSTANT. * Make IN-TARGET-COMPILATION-MODE do UNUSE-PACKAGE
113 ;; CL and USE-PACKAGE SB-CL in each of the target packages (then undo it
114 ;; on exit). * Make the cross-compiler's implementation of EVAL-WHEN
115 ;; (:COMPILE-TOPLEVEL) do UNCROSS. (This may not require any change.) *
116 ;; Hack GENESIS as necessary so that it outputs SB-CL stuff as COMMON-LISP
117 ;; stuff. * Now the code here can assert that the symbol being defined
118 ;; isn't in the cross-compilation host's CL package.
119 (unless (eql (find-symbol (symbol-name name) :cl) name)
120 ;; KLUDGE: In the cross-compiler, we use the cross-compilation host's
121 ;; DEFCONSTANT macro instead of just (SETF SYMBOL-VALUE), in order to
122 ;; get whatever blessing the cross-compilation host may expect for a
123 ;; global (SETF SYMBOL-VALUE). (CMU CL, at least around 2.4.19,
124 ;; generated full WARNINGs for code -- e.g. DEFTYPE expanders -- which
125 ;; referred to symbols which had been set by (SETF SYMBOL-VALUE). I
126 ;; doubt such warnings are ANSI-compliant, but I'm not sure, so I've
127 ;; written this in a way that CMU CL will tolerate and which ought to
128 ;; work elsewhere too.) -- WHN 2001-03-24
129 (eval `(defconstant ,name ',value)))
130 ;; It would certainly be awesome if this was only needed for symbols
131 ;; in CL. Unfortunately, that is not the case. Maybe some are moved
132 ;; back in CL later on?
133 (setf (sb!c::xc-constant-value name) value))
134 (setf (info :variable :kind name) :constant)
135 name)