0.7.3.18:
[sbcl.git] / src / compiler / defconstant.lisp
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.
9
10 (in-package "SB!IMPL")
11
12 (def!macro sb!xc:defconstant (name value &optional documentation)
13   #!+sb-doc
14   "Define a global constant, saying that the value is constant and may be
15   compiled into code. If the variable already has a value, and this is not
16   EQL to the new value, the code is not portable (undefined behavior). The
17   third argument is an optional documentation string for the variable."
18   `(eval-when (:compile-toplevel :load-toplevel :execute)
19      (sb!c::%defconstant ',name ,value ',documentation)))
20
21 ;;; the guts of DEFCONSTANT
22 (defun sb!c::%defconstant (name value doc)
23   (unless (symbolp name)
24     (error "The constant name is not a symbol: ~S" name))
25   (about-to-modify-symbol-value name)
26   (when (looks-like-name-of-special-var-p name)
27     (style-warn "defining ~S as a constant, even though the name follows~@
28 the usual naming convention (names like *FOO*) for special variables"
29                 name))
30   (let ((kind (info :variable :kind name)))
31     (case kind
32       (:constant
33        ;; Note: This behavior (discouraging any non-EQL modification)
34        ;; is unpopular, but it is specified by ANSI (i.e. ANSI says a
35        ;; non-EQL change has undefined consequences). If people really
36        ;; want bindings which are constant in some sense other than
37        ;; EQL, I suggest either just using DEFVAR (which is usually
38        ;; appropriate, despite the un-mnemonic name), or defining
39        ;; something like the DEFCONSTANT-EQX macro used in SBCL (which
40        ;; is occasionally more appropriate). -- WHN 2001-12-21
41        (unless (eql value
42                     (info :variable :constant-value name))
43          (cerror "Go ahead and change the value."
44                  "The constant ~S is being redefined."
45                  name)))
46       (:global
47        ;; (This is OK -- undefined variables are of this kind. So we
48        ;; don't warn or error or anything, just fall through.)
49        )
50       (t (warn "redefining ~(~A~) ~S to be a constant" kind name))))
51   (when doc
52     (setf (fdocumentation name 'variable) doc))
53   #-sb-xc-host
54   (setf (symbol-value name) value)
55   #+sb-xc-host
56   (progn
57                  ;; Redefining our cross-compilation host's CL symbols
58                  ;; would be poor form.
59                  ;;
60                  ;; FIXME: Having to check this and then not treat it
61                  ;; as a fatal error seems like a symptom of things
62                  ;; being pretty broken. It's also a problem in and of
63                  ;; itself, since it makes it too easy for cases of
64                  ;; using the cross-compilation host Lisp's CL
65                  ;; constant values in the target Lisp to slip by. I
66                  ;; got backed into this because the cross-compiler
67                  ;; translates DEFCONSTANT SB!XC:FOO into DEFCONSTANT
68                  ;; CL:FOO. It would be good to unscrew the
69                  ;; cross-compilation package hacks so that that
70                  ;; translation doesn't happen. Perhaps:
71                  ;;   * Replace SB-XC with SB-CL. SB-CL exports all the 
72                  ;;     symbols which ANSI requires to be exported from CL.
73                  ;;   * Make a nickname SB!CL which behaves like SB!XC.
74                  ;;   * Go through the loaded-on-the-host code making
75                  ;;     every target definition be in SB-CL. E.g.
76                  ;;     DEFMACRO-MUNDANELY DEFCONSTANT becomes
77                  ;;     DEFMACRO-MUNDANELY SB!CL:DEFCONSTANT.
78                  ;;   * Make IN-TARGET-COMPILATION-MODE do 
79                  ;;     UNUSE-PACKAGE CL and USE-PACKAGE SB-CL in each
80                  ;;     of the target packages (then undo it on exit).
81                  ;;   * Make the cross-compiler's implementation of
82                  ;;     EVAL-WHEN (:COMPILE-TOPLEVEL) do UNCROSS.
83                  ;;     (This may not require any change.)
84                  ;;   * Hack GENESIS as necessary so that it outputs
85                  ;;     SB-CL stuff as COMMON-LISP stuff.
86                  ;;   * Now the code here can assert that the symbol
87                  ;;     being defined isn't in the cross-compilation
88                  ;;     host's CL package.
89                  (unless (eql (find-symbol (symbol-name name) :cl) name)
90                    ;; KLUDGE: In the cross-compiler, we use the
91                    ;; cross-compilation host's DEFCONSTANT macro
92                    ;; instead of just (SETF SYMBOL-VALUE), in order to
93                    ;; get whatever blessing the cross-compilation host
94                    ;; may expect for a global (SETF SYMBOL-VALUE).
95                    ;; (CMU CL, at least around 2.4.19, generated full
96                    ;; WARNINGs for code -- e.g. DEFTYPE expanders --
97                    ;; which referred to symbols which had been set by
98                    ;; (SETF SYMBOL-VALUE). I doubt such warnings are
99                    ;; ANSI-compliant, but I'm not sure, so I've
100                    ;; written this in a way that CMU CL will tolerate
101                    ;; and which ought to work elsewhere too.) -- WHN
102                    ;; 2001-03-24
103                    (eval `(defconstant ,name ',value))))
104
105   (setf (info :variable :kind name) :constant
106         (info :variable :constant-value name) value)
107   name)