1 ;;;; code to manipulate symbols (but not packages, which are handled
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
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 get2 get3 %put getf remprop %putf get-properties keywordp))
20 (defun symbol-value (symbol)
22 "Return SYMBOL's current bound value."
23 (declare (optimize (safety 1)))
24 (symbol-value symbol))
27 (define-compiler-macro symbol-value (&whole form symbol &environment env)
28 (when (sb!xc:constantp symbol env)
29 (let ((name (constant-form-value symbol env)))
31 (check-deprecated-variable name))))
34 (defun boundp (symbol)
36 "Return non-NIL if SYMBOL is bound to a value."
39 (defun set (symbol new-value)
41 "Set SYMBOL's value cell to NEW-VALUE."
42 (declare (type symbol symbol))
43 (about-to-modify-symbol-value symbol 'set new-value)
44 (%set-symbol-value symbol new-value))
46 (defun %set-symbol-value (symbol new-value)
47 (%set-symbol-value symbol new-value))
49 (defun symbol-global-value (symbol)
51 "Return the SYMBOL's current global value. Identical to SYMBOL-VALUE,
52 in single-threaded builds: in multithreaded builds bound values are
53 distinct from the global value. Can also be SETF."
54 (declare (optimize (safety 1)))
55 (symbol-global-value symbol))
57 (defun set-symbol-global-value (symbol new-value)
58 (about-to-modify-symbol-value symbol 'set new-value)
59 (sb!kernel:%set-symbol-global-value symbol new-value))
61 (declaim (inline %makunbound))
62 (defun %makunbound (symbol)
63 (%set-symbol-value symbol (%primitive sb!c:make-unbound-marker)))
65 (defun makunbound (symbol)
67 "Make SYMBOL unbound, removing any value it may currently have."
68 (with-single-package-locked-error (:symbol symbol "unbinding the symbol ~A")
69 (when (and (info :variable :always-bound symbol))
70 (error "Can't make ~A variable unbound: ~S" 'always-bound symbol))
71 (about-to-modify-symbol-value symbol 'makunbound)
75 ;;; Return the built-in hash value for SYMBOL.
76 (defun symbol-hash (symbol)
79 (defun symbol-function (symbol)
81 "Return SYMBOL's current function definition. Settable with SETF."
82 (%coerce-name-to-fun symbol))
84 (defun (setf symbol-function) (new-value symbol)
85 (declare (type symbol symbol) (type function new-value))
86 (with-single-package-locked-error
87 (:symbol symbol "setting the symbol-function of ~A")
88 (setf (%coerce-name-to-fun symbol) new-value)))
90 (defun symbol-plist (symbol)
92 "Return SYMBOL's property list."
93 (symbol-plist symbol))
95 (defun %set-symbol-plist (symbol new-value)
96 (setf (symbol-plist symbol) new-value))
98 (defun symbol-name (symbol)
100 "Return SYMBOL's name as a string."
101 (symbol-name symbol))
103 (defun symbol-package (symbol)
105 "Return the package SYMBOL was interned in, or NIL if none."
106 (symbol-package symbol))
108 (defun %set-symbol-package (symbol package)
109 (declare (type symbol symbol))
110 (%set-symbol-package symbol package))
112 (defun make-symbol (string)
114 "Make and return a new symbol with the STRING as its print name."
115 (declare (type string string))
116 (%make-symbol (if (simple-string-p string)
120 (defun get (symbol indicator &optional (default nil))
122 "Look on the property list of SYMBOL for the specified INDICATOR. If this
123 is found, return the associated value, else return DEFAULT."
124 (get3 symbol indicator default))
126 (defun get2 (symbol indicator)
127 (get3 symbol indicator nil))
130 (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
132 (setf cdr-pl (cdr pl))
134 (error "~S has an odd number of items in its property list."
136 ((eq (car pl) indicator)
137 (return (car cdr-pl)))))))
140 (defun get3 (symbol indicator default)
142 (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
144 (setq cdr-pl (cdr pl))
146 (error "~S has an odd number of items in its property list."
148 ((eq (car pl) indicator)
149 (return (car cdr-pl)))))))
151 (defun %put (symbol indicator value)
153 "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
155 (do ((pl (symbol-plist symbol) (cddr pl)))
157 (setf (symbol-plist symbol)
158 (list* indicator value (symbol-plist symbol)))
160 (cond ((endp (cdr pl))
161 (error "~S has an odd number of items in its property list."
163 ((eq (car pl) indicator)
164 (rplaca (cdr pl) value)
167 (defun remprop (symbol indicator)
169 "Look on property list of SYMBOL for property with specified
170 INDICATOR. If found, splice this indicator and its value out of
171 the plist, and return the tail of the original list starting with
172 INDICATOR. If not found, return () with no side effects.
174 NOTE: The ANSI specification requires REMPROP to return true (not false)
175 or false (the symbol NIL). Portable code should not rely on any other value."
176 (do ((pl (symbol-plist symbol) (cddr pl))
179 (cond ((atom (cdr pl))
180 (error "~S has an odd number of items in its property list."
182 ((eq (car pl) indicator)
183 (cond (prev (rplacd (cdr prev) (cddr pl)))
185 (setf (symbol-plist symbol) (cddr pl))))
188 (defun getf (place indicator &optional (default ()))
190 "Search the property list stored in PLACE for an indicator EQ to INDICATOR.
191 If one is found, return the corresponding value, else return DEFAULT."
192 (do ((plist place (cddr plist)))
193 ((null plist) default)
194 (cond ((atom (cdr plist))
195 (error 'simple-type-error
196 :format-control "malformed property list: ~S."
197 :format-arguments (list place)
199 :expected-type 'cons))
200 ((eq (car plist) indicator)
201 (return (cadr plist))))))
203 (defun %putf (place property new-value)
204 (declare (type list place))
205 (do ((plist place (cddr plist)))
206 ((endp plist) (list* property new-value place))
207 (declare (type list plist))
208 (when (eq (car plist) property)
209 (setf (cadr plist) new-value)
212 (defun get-properties (place indicator-list)
214 "Like GETF, except that INDICATOR-LIST is a list of indicators which will
215 be looked for in the property list stored in PLACE. Three values are
216 returned, see manual for details."
217 (do ((plist place (cddr plist)))
218 ((null plist) (values nil nil nil))
219 (cond ((atom (cdr plist))
220 (error 'simple-type-error
221 :format-control "malformed property list: ~S."
222 :format-arguments (list place)
224 :expected-type 'cons))
225 ((memq (car plist) indicator-list)
226 (return (values (car plist) (cadr plist) plist))))))
228 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
230 "Make and return a new uninterned symbol with the same print name
231 as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
232 nor fbound and has no properties, else it has a copy of SYMBOL's
233 function, value and property list."
234 (declare (type symbol symbol))
235 (setq new-symbol (make-symbol (symbol-name symbol)))
237 (%set-symbol-value new-symbol
238 (%primitive sb!c:fast-symbol-value symbol))
239 (setf (symbol-plist new-symbol)
240 (copy-list (symbol-plist symbol)))
241 (when (fboundp symbol)
242 (setf (symbol-function new-symbol) (symbol-function symbol))))
245 ;;; FIXME: This declaration should be redundant.
246 (declaim (special *keyword-package*))
248 (defun keywordp (object)
250 "Return true if Object is a symbol in the \"KEYWORD\" package."
251 (and (symbolp object)
252 (eq (symbol-package object) *keyword-package*)))
254 ;;;; GENSYM and friends
256 (defun %make-symbol-name (prefix counter)
257 (with-output-to-string (s)
258 (write-string prefix s)
259 (%output-integer-in-base counter 10 s)))
261 (defvar *gensym-counter* 0
263 "counter for generating unique GENSYM symbols")
264 (declaim (type unsigned-byte *gensym-counter*))
266 (defun gensym (&optional (thing "G"))
268 "Creates a new uninterned symbol whose name is a prefix string (defaults
269 to \"G\"), followed by a decimal number. Thing, when supplied, will
270 alter the prefix if it is a string, or be used for the decimal number
271 if it is a number, of this symbol. The default value of the number is
272 the current value of *gensym-counter* which is incremented each time
274 (let ((old *gensym-counter*))
275 (unless (numberp thing)
276 (let ((new (etypecase old
278 (unsigned-byte (1+ old)))))
279 (declare (optimize (speed 3) (safety 0) (inhibit-warnings 3)))
280 (setq *gensym-counter* new)))
281 (multiple-value-bind (prefix int)
283 (simple-string (values thing old))
284 (unsigned-byte (values "G" thing))
285 (string (values (coerce thing 'simple-string) old)))
286 (declare (simple-string prefix))
287 (make-symbol (%make-symbol-name prefix int)))))
289 (defvar *gentemp-counter* 0)
290 (declaim (type unsigned-byte *gentemp-counter*))
292 (defun gentemp (&optional (prefix "T") (package (sane-package)))
294 "Creates a new symbol interned in package PACKAGE with the given PREFIX."
295 (declare (type string prefix))
296 (loop for name = (%make-symbol-name prefix (incf *gentemp-counter*))
297 while (nth-value 1 (find-symbol name package))
298 finally (return (values (intern name package)))))
300 ;;; This function is to be called just before a change which would affect the
301 ;;; symbol value. We don't absolutely have to call this function before such
302 ;;; changes, since such changes to constants are given as undefined behavior,
303 ;;; it's nice to do so. To circumvent this you need code like this:
306 ;;; (defun set-foo (x) (setq foo x))
307 ;;; (defconstant foo 42)
309 ;;; foo => 13, (constantp 'foo) => t
311 ;;; ...in which case you frankly deserve to lose.
312 (defun about-to-modify-symbol-value (symbol action &optional (new-value nil valuep) bind)
313 (declare (symbol symbol))
314 (flet ((describe-action ()
316 (set "set SYMBOL-VALUE of ~S")
318 (compare-and-swap "compare-and-swap SYMBOL-VALUE of ~S")
319 (defconstant "define ~S as a constant")
320 (makunbound "make ~S unbound"))))
321 (let ((kind (info :variable :kind symbol)))
322 (multiple-value-bind (what continue)
323 (cond ((eq :constant kind)
325 (values "Veritas aeterna. (can't ~@?)" nil))
327 (values "Nihil ex nihil. (can't ~@?)" nil))
329 (values "Can't ~@?." nil))
331 (values "Constant modification: attempt to ~@?." t))))
332 ((and bind (eq :global kind))
333 (values "Can't ~@? (global variable)." nil)))
336 (cerror "Modify the constant." what (describe-action) symbol)
337 (error what (describe-action) symbol)))
339 ;; :VARIABLE :TYPE is in the db only if it is declared, so no need to
341 (let ((type (info :variable :type symbol)))
342 (unless (sb!kernel::%%typep new-value type nil)
343 (let ((spec (type-specifier type)))
344 (error 'simple-type-error
345 :format-control "~@<Cannot ~@? to ~S, not of type ~S.~:@>"
346 :format-arguments (list (describe-action) symbol new-value spec)
348 :expected-type spec))))))))