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))
26 (defun boundp (symbol)
28 "Return non-NIL if SYMBOL is bound to a value."
31 (defun set (symbol new-value)
33 "Set SYMBOL's value cell to NEW-VALUE."
34 (declare (type symbol symbol))
35 (about-to-modify-symbol-value symbol 'set new-value)
36 (%set-symbol-value symbol new-value))
38 (defun %set-symbol-value (symbol new-value)
39 (%set-symbol-value symbol new-value))
41 (defun symbol-global-value (symbol)
43 "Return the SYMBOL's current global value. Identical to SYMBOL-VALUE,
44 in single-threaded builds: in multithreaded builds bound values are
45 distinct from the global value. Can also be SETF."
46 (declare (optimize (safety 1)))
47 (symbol-global-value symbol))
49 (defun set-symbol-global-value (symbol new-value)
50 (about-to-modify-symbol-value symbol 'set new-value)
51 (sb!kernel:%set-symbol-global-value symbol new-value))
53 (declaim (inline %makunbound))
54 (defun %makunbound (symbol)
55 (%set-symbol-value symbol (%primitive sb!c:make-other-immediate-type
56 0 sb!vm:unbound-marker-widetag)))
58 (defun makunbound (symbol)
60 "Make SYMBOL unbound, removing any value it may currently have."
61 (with-single-package-locked-error (:symbol symbol "unbinding the symbol ~A")
62 (when (and (info :variable :always-bound symbol))
63 (error "Can't make ~A variable unbound: ~S" 'always-bound symbol))
64 (about-to-modify-symbol-value symbol 'makunbound)
68 ;;; Return the built-in hash value for SYMBOL.
69 (defun symbol-hash (symbol)
72 (defun symbol-function (symbol)
74 "Return SYMBOL's current function definition. Settable with SETF."
75 (%coerce-name-to-fun symbol))
77 (defun (setf symbol-function) (new-value symbol)
78 (declare (type symbol symbol) (type function new-value))
79 (with-single-package-locked-error
80 (:symbol symbol "setting the symbol-function of ~A")
81 (setf (%coerce-name-to-fun symbol) new-value)))
83 (defun symbol-plist (symbol)
85 "Return SYMBOL's property list."
86 (symbol-plist symbol))
88 (defun %set-symbol-plist (symbol new-value)
89 (setf (symbol-plist symbol) new-value))
91 (defun symbol-name (symbol)
93 "Return SYMBOL's name as a string."
96 (defun symbol-package (symbol)
98 "Return the package SYMBOL was interned in, or NIL if none."
99 (symbol-package symbol))
101 (defun %set-symbol-package (symbol package)
102 (declare (type symbol symbol))
103 (%set-symbol-package symbol package))
105 (defun make-symbol (string)
107 "Make and return a new symbol with the STRING as its print name."
108 (declare (type string string))
109 (%make-symbol (if (simple-string-p string)
113 (defun get (symbol indicator &optional (default nil))
115 "Look on the property list of SYMBOL for the specified INDICATOR. If this
116 is found, return the associated value, else return DEFAULT."
117 (get3 symbol indicator default))
119 (defun get2 (symbol indicator)
120 (get3 symbol indicator nil))
123 (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
125 (setf cdr-pl (cdr pl))
127 (error "~S has an odd number of items in its property list."
129 ((eq (car pl) indicator)
130 (return (car cdr-pl)))))))
133 (defun get3 (symbol indicator default)
135 (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
137 (setq cdr-pl (cdr pl))
139 (error "~S has an odd number of items in its property list."
141 ((eq (car pl) indicator)
142 (return (car cdr-pl)))))))
144 (defun %put (symbol indicator value)
146 "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
148 (do ((pl (symbol-plist symbol) (cddr pl)))
150 (setf (symbol-plist symbol)
151 (list* indicator value (symbol-plist symbol)))
153 (cond ((endp (cdr pl))
154 (error "~S has an odd number of items in its property list."
156 ((eq (car pl) indicator)
157 (rplaca (cdr pl) value)
160 (defun remprop (symbol indicator)
162 "Look on property list of SYMBOL for property with specified
163 INDICATOR. If found, splice this indicator and its value out of
164 the plist, and return the tail of the original list starting with
165 INDICATOR. If not found, return () with no side effects.
167 NOTE: The ANSI specification requires REMPROP to return true (not false)
168 or false (the symbol NIL). Portable code should not rely on any other value."
169 (do ((pl (symbol-plist symbol) (cddr pl))
172 (cond ((atom (cdr pl))
173 (error "~S has an odd number of items in its property list."
175 ((eq (car pl) indicator)
176 (cond (prev (rplacd (cdr prev) (cddr pl)))
178 (setf (symbol-plist symbol) (cddr pl))))
181 (defun getf (place indicator &optional (default ()))
183 "Search the property list stored in Place for an indicator EQ to INDICATOR.
184 If one is found, return the corresponding value, else return DEFAULT."
185 (do ((plist place (cddr plist)))
186 ((null plist) default)
187 (cond ((atom (cdr plist))
188 (error 'simple-type-error
189 :format-control "malformed property list: ~S."
190 :format-arguments (list place)
192 :expected-type 'cons))
193 ((eq (car plist) indicator)
194 (return (cadr plist))))))
196 (defun %putf (place property new-value)
197 (declare (type list place))
198 (do ((plist place (cddr plist)))
199 ((endp plist) (list* property new-value place))
200 (declare (type list plist))
201 (when (eq (car plist) property)
202 (setf (cadr plist) new-value)
205 (defun get-properties (place indicator-list)
207 "Like GETF, except that INDICATOR-LIST is a list of indicators which will
208 be looked for in the property list stored in PLACE. Three values are
209 returned, see manual for details."
210 (do ((plist place (cddr plist)))
211 ((null plist) (values nil nil nil))
212 (cond ((atom (cdr plist))
213 (error 'simple-type-error
214 :format-control "malformed property list: ~S."
215 :format-arguments (list place)
217 :expected-type 'cons))
218 ((memq (car plist) indicator-list)
219 (return (values (car plist) (cadr plist) plist))))))
221 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
223 "Make and return a new uninterned symbol with the same print name
224 as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
225 nor fbound and has no properties, else it has a copy of SYMBOL's
226 function, value and property list."
227 (declare (type symbol symbol))
228 (setq new-symbol (make-symbol (symbol-name symbol)))
230 (%set-symbol-value new-symbol
231 (%primitive sb!c:fast-symbol-value symbol))
232 (setf (symbol-plist new-symbol)
233 (copy-list (symbol-plist symbol)))
234 (when (fboundp symbol)
235 (setf (symbol-function new-symbol) (symbol-function symbol))))
238 ;;; FIXME: This declaration should be redundant.
239 (declaim (special *keyword-package*))
241 (defun keywordp (object)
243 "Return true if Object is a symbol in the \"KEYWORD\" package."
244 (and (symbolp object)
245 (eq (symbol-package object) *keyword-package*)))
247 ;;;; GENSYM and friends
249 (defun %make-symbol-name (prefix counter)
250 (with-output-to-string (s)
251 (write-string prefix s)
252 (%output-integer-in-base counter 10 s)))
254 (defvar *gensym-counter* 0
256 "counter for generating unique GENSYM symbols")
257 (declaim (type unsigned-byte *gensym-counter*))
259 (defun gensym (&optional (thing "G"))
261 "Creates a new uninterned symbol whose name is a prefix string (defaults
262 to \"G\"), followed by a decimal number. Thing, when supplied, will
263 alter the prefix if it is a string, or be used for the decimal number
264 if it is a number, of this symbol. The default value of the number is
265 the current value of *gensym-counter* which is incremented each time
267 (let ((old *gensym-counter*))
268 (unless (numberp thing)
269 (let ((new (etypecase old
271 (unsigned-byte (1+ old)))))
272 (declare (optimize (speed 3) (safety 0) (inhibit-warnings 3)))
273 (setq *gensym-counter* new)))
274 (multiple-value-bind (prefix int)
276 (simple-string (values thing old))
277 (fixnum (values "G" thing))
278 (string (values (coerce thing 'simple-string) old)))
279 (declare (simple-string prefix))
280 (make-symbol (%make-symbol-name prefix int)))))
282 (defvar *gentemp-counter* 0)
283 (declaim (type unsigned-byte *gentemp-counter*))
285 (defun gentemp (&optional (prefix "T") (package (sane-package)))
287 "Creates a new symbol interned in package PACKAGE with the given PREFIX."
288 (declare (type string prefix))
289 (loop for name = (%make-symbol-name prefix (incf *gentemp-counter*))
290 while (nth-value 1 (find-symbol name package))
291 finally (return (values (intern name package)))))