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 %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)
36 (%set-symbol-value symbol new-value))
38 (defun %set-symbol-value (symbol new-value)
39 (%set-symbol-value symbol new-value))
41 (defun makunbound (symbol)
43 "Make SYMBOL unbound, removing any value it may currently have."
45 (%primitive sb!c:make-other-immediate-type
47 sb!vm:unbound-marker-widetag))
50 ;;; Return the built-in hash value for SYMBOL.
52 ;;; only backends for which a SYMBOL-HASH vop exists. In the past,
53 ;;; when the MIPS backend supported (or nearly did) a generational
54 ;;; (non-conservative) garbage collector, this read (OR X86 MIPS).
55 ;;; Having excised the vestigial support for GENGC, this now only
56 ;;; applies for the x86 port, but if someone were to rework the GENGC
57 ;;; support, this might change again. -- CSR, 2002-08-26
59 (defun symbol-hash (symbol)
62 ;;; Compute the hash value for SYMBOL.
64 (defun symbol-hash (symbol)
65 (%sxhash-simple-string (symbol-name symbol)))
67 (defun symbol-function (symbol)
69 "Return SYMBOL's current function definition. Settable with SETF."
70 (%coerce-name-to-fun symbol))
72 (defun (setf symbol-function) (new-value symbol)
73 (declare (type symbol symbol) (type function new-value))
74 (setf (%coerce-name-to-fun symbol) new-value))
76 (defun symbol-plist (symbol)
78 "Return SYMBOL's property list."
79 (symbol-plist symbol))
81 (defun %set-symbol-plist (symbol new-value)
82 (setf (symbol-plist symbol) new-value))
84 (defun symbol-name (symbol)
86 "Return SYMBOL's name as a string."
89 (defun symbol-package (symbol)
91 "Return the package SYMBOL was interned in, or NIL if none."
92 (symbol-package symbol))
94 (defun %set-symbol-package (symbol package)
95 (declare (type symbol symbol))
96 (%set-symbol-package symbol package))
98 (defun make-symbol (string)
100 "Make and return a new symbol with the STRING as its print name."
101 (make-symbol string))
103 (defun get (symbol indicator &optional (default nil))
105 "Look on the property list of SYMBOL for the specified INDICATOR. If this
106 is found, return the associated value, else return DEFAULT."
107 (do ((pl (symbol-plist symbol) (cddr pl)))
109 (cond ((atom (cdr pl))
110 (error "~S has an odd number of items in its property list."
112 ((eq (car pl) indicator)
113 (return (cadr pl))))))
115 (defun %put (symbol indicator value)
117 "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
119 (do ((pl (symbol-plist symbol) (cddr pl)))
121 (setf (symbol-plist symbol)
122 (list* indicator value (symbol-plist symbol)))
124 (cond ((endp (cdr pl))
125 (error "~S has an odd number of items in its property list."
127 ((eq (car pl) indicator)
128 (rplaca (cdr pl) value)
131 (defun remprop (symbol indicator)
133 "Look on property list of SYMBOL for property with specified
134 INDICATOR. If found, splice this indicator and its value out of
135 the plist, and return the tail of the original list starting with
136 INDICATOR. If not found, return () with no side effects.
138 NOTE: The ANSI specification requires REMPROP to return true (not false)
139 or false (the symbol NIL). Portable code should not rely on any other value."
140 (do ((pl (symbol-plist symbol) (cddr pl))
143 (cond ((atom (cdr pl))
144 (error "~S has an odd number of items in its property list."
146 ((eq (car pl) indicator)
147 (cond (prev (rplacd (cdr prev) (cddr pl)))
149 (setf (symbol-plist symbol) (cddr pl))))
152 (defun getf (place indicator &optional (default ()))
154 "Search the property list stored in Place for an indicator EQ to INDICATOR.
155 If one is found, return the corresponding value, else return DEFAULT."
156 (do ((plist place (cddr plist)))
157 ((null plist) default)
158 (cond ((atom (cdr plist))
159 (error 'simple-type-error
160 :format-control "malformed property list: ~S."
161 :format-arguments (list place)
163 :expected-type 'cons))
164 ((eq (car plist) indicator)
165 (return (cadr plist))))))
167 (defun %putf (place property new-value)
168 (declare (type list place))
169 (do ((plist place (cddr plist)))
170 ((endp plist) (list* property new-value place))
171 (declare (type list plist))
172 (when (eq (car plist) property)
173 (setf (cadr plist) new-value)
176 (defun get-properties (place indicator-list)
178 "Like GETF, except that INDICATOR-LIST is a list of indicators which will
179 be looked for in the property list stored in PLACE. Three values are
180 returned, see manual for details."
181 (do ((plist place (cddr plist)))
182 ((null plist) (values nil nil nil))
183 (cond ((atom (cdr plist))
184 (error 'simple-type-error
185 :format-control "malformed property list: ~S."
186 :format-arguments (list place)
188 :expected-type 'cons))
189 ((memq (car plist) indicator-list)
190 (return (values (car plist) (cadr plist) plist))))))
192 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
194 "Make and return a new uninterned symbol with the same print name
195 as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
196 nor fbound and has no properties, else it has a copy of SYMBOL's
197 function, value and property list."
198 (declare (type symbol symbol))
199 (setq new-symbol (make-symbol (symbol-name symbol)))
201 (%set-symbol-value new-symbol
202 (%primitive sb!c:fast-symbol-value symbol))
203 (setf (symbol-plist new-symbol)
204 (copy-list (symbol-plist symbol)))
205 (when (fboundp symbol)
206 (setf (symbol-function new-symbol) (symbol-function symbol))))
209 ;;; FIXME: This declaration should be redundant.
210 (declaim (special *keyword-package*))
212 (defun keywordp (object)
214 "Return true if Object is a symbol in the \"KEYWORD\" package."
215 (and (symbolp object)
216 (eq (symbol-package object) *keyword-package*)))
218 ;;;; GENSYM and friends
220 (defvar *gensym-counter* 0
222 "counter for generating unique GENSYM symbols")
223 (declaim (type unsigned-byte *gensym-counter*))
225 (defun gensym (&optional (thing "G"))
227 "Creates a new uninterned symbol whose name is a prefix string (defaults
228 to \"G\"), followed by a decimal number. Thing, when supplied, will
229 alter the prefix if it is a string, or be used for the decimal number
230 if it is a number, of this symbol. The default value of the number is
231 the current value of *gensym-counter* which is incremented each time
233 (let ((old *gensym-counter*))
234 (unless (numberp thing)
235 (let ((new (etypecase old
237 (unsigned-byte (1+ old)))))
238 (declare (optimize (speed 3) (safety 0)(inhibit-warnings 3)))
239 (setq *gensym-counter* new)))
240 (multiple-value-bind (prefix int)
242 (simple-string (values thing old))
243 (fixnum (values "G" thing))
244 (string (values (coerce thing 'simple-string) old)))
245 (declare (simple-string prefix))
247 (concatenate 'simple-string prefix
249 (quick-integer-to-string int)))))))
251 (defvar *gentemp-counter* 0)
252 (declaim (type unsigned-byte *gentemp-counter*))
254 (defun gentemp (&optional (prefix "T") (package (sane-package)))
256 "Creates a new symbol interned in package PACKAGE with the given PREFIX."
257 (declare (type string prefix))
259 (let ((*print-base* 10)
262 (new-pname (format nil "~A~D" prefix (incf *gentemp-counter*))))
263 (multiple-value-bind (symbol existsp) (find-symbol new-pname package)
264 (declare (ignore symbol))
265 (unless existsp (return (values (intern new-pname package))))))))