Mark SB-DEBUG:BACKTRACE[-AS-LIST] as deprecated
[sbcl.git] / src / code / symbol.lisp
blob141932a15958f1d2d5154c02c398b74c8ad0d383
1 ;;;; code to manipulate symbols (but not packages, which are handled
2 ;;;; elsewhere)
3 ;;;;
4 ;;;; Many of these definitions are trivial interpreter entries to
5 ;;;; functions open-coded by the compiler.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!IMPL")
18 (declaim (maybe-inline get get3 %put getf remprop %putf get-properties keywordp))
20 ;;; Used by [GLOBAL-]SYMBOL-VALUE compiler-macros:
21 ;;;
22 ;;; When SYMBOL is constant, check whether it names a deprecated
23 ;;; variable, potentially signaling a {EARLY,LATE}-DEPRECATION-WARNING
24 ;;; in the process. Furthermore, if the deprecation state is :FINAL,
25 ;;; replace FORM by SYMBOL, causing the symbol-macro on SYMBOL to
26 ;;; expand into a call to DEPRECATION-ERROR.
27 ;;;
28 ;;; See SB-IMPL:SETUP-VARIABLE-IN-FINAL-DEPRECATION.
29 #-sb-xc-host
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31 (defun maybe-handle-deprecated-global-variable (symbol env)
32 (when (sb!xc:constantp symbol env)
33 (let ((name (constant-form-value symbol env)))
34 (when (symbolp name)
35 (case (deprecated-thing-p 'variable name)
36 ((:early :late)
37 (check-deprecated-thing 'variable name)
38 nil)
39 ;; In this case, there is a symbol-macro for NAME that
40 ;; will signal the FINAL-DEPRECATION-WARNING when
41 ;; ir1converted and the DEPRECATION-ERROR at runtime.
42 (:final
43 name)))))))
45 (defun symbol-value (symbol)
46 #!+sb-doc
47 "Return SYMBOL's current bound value."
48 (declare (optimize (safety 1)))
49 (symbol-value symbol))
51 #-sb-xc-host
52 (define-compiler-macro symbol-value (&whole form symbol &environment env)
53 (or (maybe-handle-deprecated-global-variable symbol env) form))
55 (defun boundp (symbol)
56 #!+sb-doc
57 "Return non-NIL if SYMBOL is bound to a value."
58 (boundp symbol))
60 (defun set (symbol new-value)
61 #!+sb-doc
62 "Set SYMBOL's value cell to NEW-VALUE."
63 (declare (type symbol symbol))
64 (about-to-modify-symbol-value symbol 'set new-value)
65 (%set-symbol-value symbol new-value))
67 (defun %set-symbol-value (symbol new-value)
68 (%set-symbol-value symbol new-value))
70 (defun symbol-global-value (symbol)
71 #!+sb-doc
72 "Return the SYMBOL's current global value. Identical to SYMBOL-VALUE,
73 in single-threaded builds: in multithreaded builds bound values are
74 distinct from the global value. Can also be SETF."
75 (declare (optimize (safety 1)))
76 (symbol-global-value symbol))
78 #-sb-xc-host
79 (define-compiler-macro symbol-global-value (&whole form symbol
80 &environment env)
81 (or (maybe-handle-deprecated-global-variable symbol env) form))
83 (defun set-symbol-global-value (symbol new-value)
84 (about-to-modify-symbol-value symbol 'set new-value)
85 (%set-symbol-global-value symbol new-value))
87 (declaim (inline %makunbound))
88 (defun %makunbound (symbol)
89 (%set-symbol-value symbol (%primitive sb!c:make-unbound-marker)))
91 (defun makunbound (symbol)
92 #!+sb-doc
93 "Make SYMBOL unbound, removing any value it may currently have."
94 (with-single-package-locked-error (:symbol symbol "unbinding the symbol ~A")
95 ;; :EVENTUALLY is allowed for :always-bound here, as it has no bearing
96 (when (eq (info :variable :always-bound symbol) :always-bound)
97 (error "Can't make ~A variable unbound: ~S" 'always-bound symbol))
98 (about-to-modify-symbol-value symbol 'makunbound)
99 (%makunbound symbol)
100 symbol))
102 ;; Compute a symbol's hash. Also used by FIND-SYMBOL which requires that a hash
103 ;; be a pure function of the name and not a semi-opaque property of the symbol.
104 ;; The hash of all symbols named "NIL" must be the same, so not to pessimize
105 ;; FIND-SYMBOL by special-casing the finding of CL:NIL with an extra "or"
106 ;; in the hash-equality test. i.e. We can't recognize that CL:NIL was the
107 ;; object sought (having an exceptional hash) until it has been found.
108 (defun compute-symbol-hash (string length)
109 (declare (simple-string string) (index length))
110 (if (and (= length 3)
111 (locally
112 ;; SXHASH-SUBSTRING is unsafe, so this is too. but do we know that
113 ;; length is ok, or is it an accident that it can scan too far?
114 (declare (optimize (safety 0)))
115 (string-dispatch (simple-base-string (simple-array character (*)))
116 string
117 (and (char= (schar string 0) #\N)
118 (char= (schar string 1) #\I)
119 (char= (schar string 2) #\L)))))
120 ;; FIXME: hardwire this. See similar comment at
121 ;; (deftransform sxhash ((x) (symbol))
122 (return-from compute-symbol-hash (symbol-hash nil)))
123 ;; And make a symbol's hash not the same as (sxhash name) in general.
124 (let ((sxhash (logand (lognot (%sxhash-simple-substring string length))
125 sb!xc:most-positive-fixnum)))
126 (if (zerop sxhash) #x55AA sxhash))) ; arbitrary substitute for 0
128 ;; Return SYMBOL's hash, a strictly positive fixnum, computing it if not stored.
129 ;; The inlined code for (SXHASH symbol) only calls ENSURE-SYMBOL-HASH if
130 ;; needed, however this is ok to call even if the hash is already nonzero.
131 (defun ensure-symbol-hash (symbol)
132 (let ((hash (symbol-hash symbol)))
133 (if (zerop hash)
134 (let ((name (symbol-name symbol)))
135 (%set-symbol-hash symbol (compute-symbol-hash name (length name))))
136 hash)))
138 ;;; Interpreter stub: Return whatever is in the SYMBOL-HASH slot of SYMBOL.
139 (defun symbol-hash (symbol)
140 (symbol-hash symbol))
142 (defun symbol-function (symbol)
143 #!+sb-doc
144 "Return SYMBOL's current function definition. Settable with SETF."
145 (%coerce-name-to-fun symbol symbol-fdefn))
147 ;; I think there are two bugs here.
148 ;; Per CLHS "SETF may be used with symbol-function to replace a global
149 ;; function definition when the symbol's function definition
150 ;; does not represent a special operator."
151 ;; 1. This should fail:
152 ;; * (in-package CL) ; circumvent package lock
153 ;; * (setf (symbol-function 'if) #'cons) => #<FUNCTION CONS>
154 ;; 2. (SETF (SYMBOL-FUNCTION 'I-ONCE-WAS-A-MACRO) #'CONS)
155 ;; should _probably_ make I-ONCE-WAS-A-MACRO not a macro
156 (defun (setf symbol-function) (new-value symbol)
157 (declare (type symbol symbol) (type function new-value))
158 ;; (SYMBOL-FUNCTION symbol) == (FDEFINITION symbol) according to the writeup
159 ;; on SYMBOL-FUNCTION. It doesn't say that SETF behaves the same, but let's
160 ;; assume it does, and that we can't assign our macro/special guard funs.
161 (err-if-unacceptable-function new-value '(setf symbol-function))
162 (with-single-package-locked-error
163 (:symbol symbol "setting the symbol-function of ~A")
164 ;; This code is a little "surprising" in that it is not just a limited
165 ;; case of (SETF FDEFINITION), but instead a different thing.
166 ;; I really think the code paths should be reconciled.
167 ;; e.g. what's up with *USER-HASH-TABLE-TESTS* being checked
168 ;; in %SET-FDEFINITION but not here?
169 (maybe-clobber-ftype symbol)
170 (let ((fdefn (find-or-create-fdefn symbol)))
171 (setf (fdefn-fun fdefn) new-value))))
173 ;;; Accessors for the dual-purpose info/plist slot
175 ;; A symbol's INFO slot is always in one of three states:
176 ;; 1. NIL ; the initial state
177 ;; 2. #(data ....) ; globaldb used the slot
178 ;; 3. (PLIST . NIL) or (PLIST . #(data ...)) ; plist was touched,
179 ;; and also possibly globaldb used the slot
181 ;; State 1 transitions to state 2 by assigning globaldb data,
182 ;; or to state 3 via ({SETF|CAS} SYMBOL-PLIST).
183 ;; (SETF INFO) by itself will never cause 1 -> 3 transition.
184 ;; State 2 transitions to state 3 via ({SETF|CAS} SYMBOL-PLIST).
185 ;; There are *no* other permissible state transitions.
187 (defun symbol-info (symbol)
188 (symbol-info symbol))
190 ;; An "interpreter stub" for an operation that is only implemented for
191 ;; the benefit of platforms without compare-and-swap-vops.
192 (defun (setf symbol-info) (new-info symbol)
193 (setf (symbol-info symbol) new-info))
195 ;; Atomically update SYMBOL's info/plist slot to contain a new info vector.
196 ;; The vector is computed by calling UPDATE-FN on the old vector,
197 ;; repeatedly as necessary, until no conflict happens with other updaters.
198 ;; The function may choose to abort the update by returning NIL.
199 (defun update-symbol-info (symbol update-fn)
200 (declare (symbol symbol)
201 (type (function (t) t) update-fn))
202 (prog ((info-holder (symbol-info symbol))
203 (current-vect))
204 outer-restart
205 ;; Do not use SYMBOL-INFO-VECTOR - this must not perform a slot read again.
206 (setq current-vect (if (listp info-holder) (cdr info-holder) info-holder))
207 inner-restart
208 ;; KLUDGE: The "#." on +nil-packed-infos+ is due to slightly crippled
209 ;; fops in genesis's fasload. Anonymizing the constant works around the
210 ;; issue, at the expense of an extra copy of the empty info vector.
211 (let ((new-vect (funcall update-fn
212 (or current-vect #.+nil-packed-infos+))))
213 (unless (simple-vector-p new-vect)
214 (aver (null new-vect))
215 (return)) ; nothing to do
216 (if (consp info-holder) ; State 3: exchange the CDR
217 (let ((old (%compare-and-swap-cdr info-holder current-vect new-vect)))
218 (when (eq old current-vect) (return t)) ; win
219 (setq current-vect old) ; Don't touch holder- it's still a cons
220 (go inner-restart)))
221 ;; State 1 or 2: info-holder is NIL or a vector.
222 ;; Exchange the contents of the info slot. Type-inference derives
223 ;; SIMPLE-VECTOR-P on the args to CAS, so no extra checking.
224 (let ((old (%compare-and-swap-symbol-info symbol info-holder new-vect)))
225 (when (eq old info-holder) (return t)) ; win
226 ;; Check whether we're in state 2 or 3 now.
227 ;; Impossible to be in state 1: nobody ever puts NIL in the slot.
228 ;; Up above, we bailed out if the update-fn returned NIL.
229 (setq info-holder old)
230 (go outer-restart)))))
232 (eval-when (:compile-toplevel)
233 ;; If we're in state 1 or state 3, we can take (CAR (SYMBOL-INFO S))
234 ;; to get the property list. If we're in state 2, this same access
235 ;; gets the fixnum which is the VECTOR-LENGTH of the info vector.
236 ;; So all we have to do is turn any fixnum to NIL, and we have a plist.
237 ;; Ensure that this pun stays working.
238 (assert (= (- (* sb!vm:n-word-bytes sb!vm:cons-car-slot)
239 sb!vm:list-pointer-lowtag)
240 (- (* sb!vm:n-word-bytes sb!vm:vector-length-slot)
241 sb!vm:other-pointer-lowtag))))
243 (defun symbol-plist (symbol)
244 #!+sb-doc
245 "Return SYMBOL's property list."
246 #!+symbol-info-vops
247 (symbol-plist symbol) ; VOP translates it
248 #!-symbol-info-vops
249 (let ((list (car (truly-the list (symbol-info symbol))))) ; a white lie
250 ;; Just ensure the result is not a fixnum, and we're done.
251 (if (fixnump list) nil list)))
253 (declaim (ftype (sfunction (symbol t) cons) %ensure-plist-holder)
254 (inline %ensure-plist-holder))
256 ;; When a plist update (setf or cas) is first performed on a symbol,
257 ;; a one-time allocation of an extra cons is done which creates two
258 ;; "slots" from one: a slot for the info-vector and a slot for the plist.
259 ;; This avoids complications in the implementation of the user-facing
260 ;; (CAS SYMBOL-PLIST) function, which should not have to be aware of
261 ;; competition from globaldb mutators even if no other threads attempt
262 ;; to manipulate the plist per se.
264 ;; Given a SYMBOL and its current INFO of type (OR LIST SIMPLE-VECTOR)
265 ;; ensure that SYMBOL's current info is a cons, and return that.
266 ;; If racing with multiple threads, at most one thread will install the cons.
267 (defun %ensure-plist-holder (symbol info)
268 ;; Invoked only when SYMBOL is known to be a symbol.
269 (declare (optimize (safety 0)))
270 (if (consp info) ; it's fine to call this with a cell already installed
271 info ; all done
272 (let (newcell)
273 ;; The pointer from the new cons to the old info must be persisted
274 ;; to memory before the symbol's info slot points to the cons.
275 ;; [x86oid doesn't need the barrier, others might]
276 (sb!thread:barrier (:write)
277 (setq newcell (cons nil info)))
278 (loop (let ((old (%compare-and-swap-symbol-info symbol info newcell)))
279 (cond ((eq old info) (return newcell)) ; win
280 ((consp old) (return old))) ; somebody else made a cons!
281 (setq info old)
282 (sb!thread:barrier (:write) ; Retry using same newcell
283 (rplacd newcell info)))))))
285 (declaim (inline %compare-and-swap-symbol-plist
286 %set-symbol-plist))
288 (defun %compare-and-swap-symbol-plist (symbol old new)
289 ;; This is the entry point into which (CAS SYMBOL-PLIST) is transformed.
290 ;; If SYMBOL's info cell is a cons, we can do (CAS CAR). Otherwise punt.
291 (declare (symbol symbol) (list old new))
292 (let ((cell (symbol-info symbol)))
293 (if (consp cell)
294 (%compare-and-swap-car cell old new)
295 (%%compare-and-swap-symbol-plist symbol old new))))
297 (defun %%compare-and-swap-symbol-plist (symbol old new)
298 ;; This is just the second half of a partially-inline function, to avoid
299 ;; code bloat in the exceptional case. Type assertions should have been
300 ;; done - or not, per policy - by the caller of %COMPARE-AND-SWAP-SYMBOL-PLIST
301 ;; so now use TRULY-THE to avoid further type checking.
302 (%compare-and-swap-car (%ensure-plist-holder (truly-the symbol symbol)
303 (symbol-info symbol))
304 old new))
306 (defun %set-symbol-plist (symbol new-value)
307 ;; This is the entry point into which (SETF SYMBOL-PLIST) is transformed.
308 ;; If SYMBOL's info cell is a cons, we can do (SETF CAR). Otherwise punt.
309 (declare (symbol symbol) (list new-value))
310 (let ((cell (symbol-info symbol)))
311 (if (consp cell)
312 (setf (car cell) new-value)
313 (%%set-symbol-plist symbol new-value))))
315 (defun %%set-symbol-plist (symbol new-value)
316 ;; Same considerations as for %%COMPARE-AND-SWAP-SYMBOL-PLIST,
317 ;; with a slight efficiency hack: if the symbol has no plist holder cell
318 ;; and the NEW-VALUE is NIL, try to avoid creating a holder cell.
319 ;; Yet we must write something, because omitting a memory operation
320 ;; could have a subtle effect in the presence of multi-threading.
321 (let ((info (symbol-info (truly-the symbol symbol))))
322 (when (and (not new-value) (atom info)) ; try to treat this as a no-op
323 (let ((old (%compare-and-swap-symbol-info symbol info info)))
324 (if (eq old info) ; good enough
325 (return-from %%set-symbol-plist new-value) ; = nil
326 (setq info old))))
327 (setf (car (%ensure-plist-holder symbol info)) new-value)))
329 ;;; End of Info/Plist slot manipulation
331 (defun symbol-name (symbol)
332 #!+sb-doc
333 "Return SYMBOL's name as a string."
334 (symbol-name symbol))
336 (defun symbol-package (symbol)
337 #!+sb-doc
338 "Return the package SYMBOL was interned in, or NIL if none."
339 (symbol-package symbol))
341 (defun %set-symbol-package (symbol package)
342 (declare (type symbol symbol))
343 (%set-symbol-package symbol package))
345 (defun make-symbol (string)
346 #!+sb-doc
347 "Make and return a new symbol with the STRING as its print name."
348 (declare (type string string))
349 (%make-symbol (if (simple-string-p string)
350 string
351 (subseq string 0))))
353 (defun get (symbol indicator &optional (default nil))
354 #!+sb-doc
355 "Look on the property list of SYMBOL for the specified INDICATOR. If this
356 is found, return the associated value, else return DEFAULT."
357 (get3 symbol indicator default))
359 (defun get3 (symbol indicator default)
360 (let (cdr-pl)
361 (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
362 ((atom pl) default)
363 (setq cdr-pl (cdr pl))
364 (cond ((atom cdr-pl)
365 (error "~S has an odd number of items in its property list."
366 symbol))
367 ((eq (car pl) indicator)
368 (return (car cdr-pl)))))))
370 (defun %put (symbol indicator value)
371 #!+sb-doc
372 "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
373 Returns VALUE."
374 (do ((pl (symbol-plist symbol) (cddr pl)))
375 ((endp pl)
376 (setf (symbol-plist symbol)
377 (list* indicator value (symbol-plist symbol)))
378 value)
379 (cond ((endp (cdr pl))
380 (error "~S has an odd number of items in its property list."
381 symbol))
382 ((eq (car pl) indicator)
383 (rplaca (cdr pl) value)
384 (return value)))))
386 (defun remprop (symbol indicator)
387 #!+sb-doc
388 "Look on property list of SYMBOL for property with specified
389 INDICATOR. If found, splice this indicator and its value out of
390 the plist, and return the tail of the original list starting with
391 INDICATOR. If not found, return () with no side effects.
393 NOTE: The ANSI specification requires REMPROP to return true (not false)
394 or false (the symbol NIL). Portable code should not rely on any other value."
395 (do ((pl (symbol-plist symbol) (cddr pl))
396 (prev nil pl))
397 ((atom pl) nil)
398 (cond ((atom (cdr pl))
399 (error "~S has an odd number of items in its property list."
400 symbol))
401 ((eq (car pl) indicator)
402 (cond (prev (rplacd (cdr prev) (cddr pl)))
404 (setf (symbol-plist symbol) (cddr pl))))
405 (return pl)))))
407 (defun getf (place indicator &optional (default ()))
408 #!+sb-doc
409 "Search the property list stored in PLACE for an indicator EQ to INDICATOR.
410 If one is found, return the corresponding value, else return DEFAULT."
411 (do ((plist place (cddr plist)))
412 ((null plist) default)
413 (cond ((atom (cdr plist))
414 (error 'simple-type-error
415 :format-control "malformed property list: ~S."
416 :format-arguments (list place)
417 :datum (cdr plist)
418 :expected-type 'cons))
419 ((eq (car plist) indicator)
420 (return (cadr plist))))))
422 (defun %putf (place property new-value)
423 (declare (type list place))
424 (do ((plist place (cddr plist)))
425 ((endp plist) (list* property new-value place))
426 (declare (type list plist))
427 (when (eq (car plist) property)
428 (setf (cadr plist) new-value)
429 (return place))))
431 (defun get-properties (place indicator-list)
432 #!+sb-doc
433 "Like GETF, except that INDICATOR-LIST is a list of indicators which will
434 be looked for in the property list stored in PLACE. Three values are
435 returned, see manual for details."
436 (do ((plist place (cddr plist)))
437 ((null plist) (values nil nil nil))
438 (cond ((atom (cdr plist))
439 (error 'simple-type-error
440 :format-control "malformed property list: ~S."
441 :format-arguments (list place)
442 :datum (cdr plist)
443 :expected-type 'cons))
444 ((memq (car plist) indicator-list)
445 (return (values (car plist) (cadr plist) plist))))))
447 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
448 #!+sb-doc
449 "Make and return a new uninterned symbol with the same print name
450 as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
451 nor fbound and has no properties, else it has a copy of SYMBOL's
452 function, value and property list."
453 (declare (type symbol symbol))
454 (setq new-symbol (make-symbol (symbol-name symbol)))
455 (when copy-props
456 (%set-symbol-value new-symbol
457 (%primitive sb!c:fast-symbol-value symbol))
458 (setf (symbol-plist new-symbol)
459 (copy-list (symbol-plist symbol)))
460 (when (fboundp symbol)
461 (setf (symbol-function new-symbol) (symbol-function symbol))))
462 new-symbol)
464 (defun keywordp (object)
465 #!+sb-doc
466 "Return true if Object is a symbol in the \"KEYWORD\" package."
467 (and (symbolp object)
468 (eq (symbol-package object) *keyword-package*)))
470 ;;;; GENSYM and friends
472 (defun %make-symbol-name (prefix counter)
473 (declare (string prefix))
474 (if (typep counter '(and fixnum unsigned-byte))
475 (let ((s ""))
476 (declare (simple-string s))
477 (labels ((recurse (depth n)
478 (multiple-value-bind (q r) (truncate n 10)
479 (if (plusp q)
480 (recurse (1+ depth) q)
481 (let ((et (if (or (base-string-p prefix)
482 (every #'base-char-p prefix))
483 'base-char 'character)))
484 (setq s (make-string (+ (length prefix) depth)
485 :element-type et))
486 (replace s prefix)))
487 (setf (char s (- (length s) depth))
488 (code-char (+ (char-code #\0) r)))
489 s)))
490 (recurse 1 counter)))
491 (with-simple-output-to-string (s)
492 (write-string prefix s)
493 (%output-integer-in-base counter 10 s))))
495 (defvar *gensym-counter* 0
496 #!+sb-doc
497 "counter for generating unique GENSYM symbols")
498 (declaim (type unsigned-byte *gensym-counter*))
500 (defun gensym (&optional (thing "G"))
501 #!+sb-doc
502 "Creates a new uninterned symbol whose name is a prefix string (defaults
503 to \"G\"), followed by a decimal number. Thing, when supplied, will
504 alter the prefix if it is a string, or be used for the decimal number
505 if it is a number, of this symbol. The default value of the number is
506 the current value of *gensym-counter* which is incremented each time
507 it is used."
508 (multiple-value-bind (prefix int)
509 (if (integerp thing)
510 (values "G" thing)
511 (values thing (let ((old *gensym-counter*))
512 (setq *gensym-counter* (1+ old))
513 old)))
514 (make-symbol (%make-symbol-name prefix int))))
516 (defvar *gentemp-counter* 0)
517 (declaim (type unsigned-byte *gentemp-counter*))
519 (defun gentemp (&optional (prefix "T") (package (sane-package)))
520 #!+sb-doc
521 "Creates a new symbol interned in package PACKAGE with the given PREFIX."
522 (declare (type string prefix))
523 (loop for name = (%make-symbol-name prefix (incf *gentemp-counter*))
524 while (nth-value 1 (find-symbol name package))
525 finally (return (values (intern name package)))))
527 ;;; This function is to be called just before a change which would affect the
528 ;;; symbol value. We don't absolutely have to call this function before such
529 ;;; changes, since such changes to constants are given as undefined behavior,
530 ;;; it's nice to do so. To circumvent this you need code like this:
532 ;;; (defvar foo)
533 ;;; (defun set-foo (x) (setq foo x))
534 ;;; (defconstant foo 42)
535 ;;; (set-foo 13)
536 ;;; foo => 13, (constantp 'foo) => t
538 ;;; ...in which case you frankly deserve to lose.
539 (defun about-to-modify-symbol-value (symbol action &optional (new-value nil valuep) bind)
540 (declare (symbol symbol))
541 (flet ((describe-action ()
542 (ecase action
543 (set "set SYMBOL-VALUE of ~S")
544 (progv "bind ~S")
545 (compare-and-swap "compare-and-swap SYMBOL-VALUE of ~S")
546 (defconstant "define ~S as a constant")
547 (makunbound "make ~S unbound"))))
548 (let ((kind (info :variable :kind symbol)))
549 (multiple-value-bind (what continue)
550 (cond ((eq :constant kind)
551 (cond ((eq symbol t)
552 (values "Veritas aeterna. (can't ~@?)" nil))
553 ((eq symbol nil)
554 (values "Nihil ex nihil. (can't ~@?)" nil))
555 ((keywordp symbol)
556 (values "Can't ~@?." nil))
558 (values "Constant modification: attempt to ~@?." t))))
559 ((and bind (eq :global kind))
560 (values "Can't ~@? (global variable)." nil)))
561 (when what
562 (if continue
563 (cerror "Modify the constant." what (describe-action) symbol)
564 (error what (describe-action) symbol)))
565 (when valuep
566 ;; :VARIABLE :TYPE is in the db only if it is declared, so no need to
567 ;; check.
568 (let ((type (info :variable :type symbol)))
569 (unless (sb!kernel::%%typep new-value type nil)
570 (let ((spec (type-specifier type)))
571 (error 'simple-type-error
572 :format-control "~@<Cannot ~@? to ~S, not of type ~S.~:@>"
573 :format-arguments (list (describe-action) symbol new-value spec)
574 :datum new-value
575 :expected-type spec)))))))
576 nil))