Remove get2 variant of get.
[sbcl.git] / src / code / symbol.lisp
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.
6
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.
15
16 (in-package "SB!IMPL")
17
18 (declaim (maybe-inline get get3 %put getf remprop %putf get-properties keywordp))
19
20 (defun symbol-value (symbol)
21   #!+sb-doc
22   "Return SYMBOL's current bound value."
23   (declare (optimize (safety 1)))
24   (symbol-value symbol))
25
26 #-sb-xc-host
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)))
30       (when (symbolp name)
31         (check-deprecated-variable name))))
32   form)
33
34 (defun boundp (symbol)
35   #!+sb-doc
36   "Return non-NIL if SYMBOL is bound to a value."
37   (boundp symbol))
38
39 (defun set (symbol new-value)
40   #!+sb-doc
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))
45
46 (defun %set-symbol-value (symbol new-value)
47   (%set-symbol-value symbol new-value))
48
49 (defun symbol-global-value (symbol)
50   #!+sb-doc
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))
56
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))
60
61 (declaim (inline %makunbound))
62 (defun %makunbound (symbol)
63   (%set-symbol-value symbol (%primitive sb!c:make-unbound-marker)))
64
65 (defun makunbound (symbol)
66   #!+sb-doc
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)
72     (%makunbound symbol)
73     symbol))
74
75 ;;; Return the built-in hash value for SYMBOL.
76 (defun symbol-hash (symbol)
77   (symbol-hash symbol))
78
79 (defun symbol-function (symbol)
80   #!+sb-doc
81   "Return SYMBOL's current function definition. Settable with SETF."
82   (%coerce-name-to-fun symbol))
83
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)))
89
90 (defun symbol-plist (symbol)
91   #!+sb-doc
92   "Return SYMBOL's property list."
93   (symbol-plist symbol))
94
95 (defun %set-symbol-plist (symbol new-value)
96   (setf (symbol-plist symbol) new-value))
97
98 (defun symbol-name (symbol)
99   #!+sb-doc
100   "Return SYMBOL's name as a string."
101   (symbol-name symbol))
102
103 (defun symbol-package (symbol)
104   #!+sb-doc
105   "Return the package SYMBOL was interned in, or NIL if none."
106   (symbol-package symbol))
107
108 (defun %set-symbol-package (symbol package)
109   (declare (type symbol symbol))
110   (%set-symbol-package symbol package))
111
112 (defun make-symbol (string)
113   #!+sb-doc
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)
117                     string
118                     (subseq string 0))))
119
120 (defun get (symbol indicator &optional (default nil))
121   #!+sb-doc
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))
125
126 (defun get3 (symbol indicator default)
127   (let (cdr-pl)
128     (do ((pl (symbol-plist symbol) (cdr cdr-pl)))
129         ((atom pl) default)
130       (setq cdr-pl (cdr pl))
131       (cond ((atom cdr-pl)
132              (error "~S has an odd number of items in its property list."
133                     symbol))
134             ((eq (car pl) indicator)
135              (return (car cdr-pl)))))))
136
137 (defun %put (symbol indicator value)
138   #!+sb-doc
139   "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
140   Returns VALUE."
141   (do ((pl (symbol-plist symbol) (cddr pl)))
142       ((endp pl)
143        (setf (symbol-plist symbol)
144              (list* indicator value (symbol-plist symbol)))
145        value)
146     (cond ((endp (cdr pl))
147            (error "~S has an odd number of items in its property list."
148                   symbol))
149           ((eq (car pl) indicator)
150            (rplaca (cdr pl) value)
151            (return value)))))
152
153 (defun remprop (symbol indicator)
154   #!+sb-doc
155   "Look on property list of SYMBOL for property with specified
156   INDICATOR. If found, splice this indicator and its value out of
157   the plist, and return the tail of the original list starting with
158   INDICATOR. If not found, return () with no side effects.
159
160   NOTE: The ANSI specification requires REMPROP to return true (not false)
161   or false (the symbol NIL). Portable code should not rely on any other value."
162   (do ((pl (symbol-plist symbol) (cddr pl))
163        (prev nil pl))
164       ((atom pl) nil)
165     (cond ((atom (cdr pl))
166            (error "~S has an odd number of items in its property list."
167                   symbol))
168           ((eq (car pl) indicator)
169            (cond (prev (rplacd (cdr prev) (cddr pl)))
170                  (t
171                   (setf (symbol-plist symbol) (cddr pl))))
172            (return pl)))))
173
174 (defun getf (place indicator &optional (default ()))
175   #!+sb-doc
176   "Search the property list stored in PLACE for an indicator EQ to INDICATOR.
177   If one is found, return the corresponding value, else return DEFAULT."
178   (do ((plist place (cddr plist)))
179       ((null plist) default)
180     (cond ((atom (cdr plist))
181            (error 'simple-type-error
182                   :format-control "malformed property list: ~S."
183                   :format-arguments (list place)
184                   :datum (cdr plist)
185                   :expected-type 'cons))
186           ((eq (car plist) indicator)
187            (return (cadr plist))))))
188
189 (defun %putf (place property new-value)
190   (declare (type list place))
191   (do ((plist place (cddr plist)))
192       ((endp plist) (list* property new-value place))
193     (declare (type list plist))
194     (when (eq (car plist) property)
195       (setf (cadr plist) new-value)
196       (return place))))
197
198 (defun get-properties (place indicator-list)
199   #!+sb-doc
200   "Like GETF, except that INDICATOR-LIST is a list of indicators which will
201   be looked for in the property list stored in PLACE. Three values are
202   returned, see manual for details."
203   (do ((plist place (cddr plist)))
204       ((null plist) (values nil nil nil))
205     (cond ((atom (cdr plist))
206            (error 'simple-type-error
207                   :format-control "malformed property list: ~S."
208                   :format-arguments (list place)
209                   :datum (cdr plist)
210                   :expected-type 'cons))
211           ((memq (car plist) indicator-list)
212            (return (values (car plist) (cadr plist) plist))))))
213
214 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
215   #!+sb-doc
216   "Make and return a new uninterned symbol with the same print name
217   as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
218   nor fbound and has no properties, else it has a copy of SYMBOL's
219   function, value and property list."
220   (declare (type symbol symbol))
221   (setq new-symbol (make-symbol (symbol-name symbol)))
222   (when copy-props
223     (%set-symbol-value new-symbol
224                        (%primitive sb!c:fast-symbol-value symbol))
225     (setf (symbol-plist new-symbol)
226           (copy-list (symbol-plist symbol)))
227     (when (fboundp symbol)
228       (setf (symbol-function new-symbol) (symbol-function symbol))))
229   new-symbol)
230
231 ;;; FIXME: This declaration should be redundant.
232 (declaim (special *keyword-package*))
233
234 (defun keywordp (object)
235   #!+sb-doc
236   "Return true if Object is a symbol in the \"KEYWORD\" package."
237   (and (symbolp object)
238        (eq (symbol-package object) *keyword-package*)))
239 \f
240 ;;;; GENSYM and friends
241
242 (defun %make-symbol-name (prefix counter)
243   (with-output-to-string (s)
244     (write-string prefix s)
245     (%output-integer-in-base counter 10 s)))
246
247 (defvar *gensym-counter* 0
248   #!+sb-doc
249   "counter for generating unique GENSYM symbols")
250 (declaim (type unsigned-byte *gensym-counter*))
251
252 (defun gensym (&optional (thing "G"))
253   #!+sb-doc
254   "Creates a new uninterned symbol whose name is a prefix string (defaults
255    to \"G\"), followed by a decimal number. Thing, when supplied, will
256    alter the prefix if it is a string, or be used for the decimal number
257    if it is a number, of this symbol. The default value of the number is
258    the current value of *gensym-counter* which is incremented each time
259    it is used."
260   (let ((old *gensym-counter*))
261     (unless (numberp thing)
262       (let ((new (etypecase old
263                    (index (1+ old))
264                    (unsigned-byte (1+ old)))))
265         (declare (optimize (speed 3) (safety 0) (inhibit-warnings 3)))
266         (setq *gensym-counter* new)))
267     (multiple-value-bind (prefix int)
268         (etypecase thing
269           (simple-string (values thing old))
270           (unsigned-byte (values "G" thing))
271           (string (values (coerce thing 'simple-string) old)))
272       (declare (simple-string prefix))
273       (make-symbol (%make-symbol-name prefix int)))))
274
275 (defvar *gentemp-counter* 0)
276 (declaim (type unsigned-byte *gentemp-counter*))
277
278 (defun gentemp (&optional (prefix "T") (package (sane-package)))
279   #!+sb-doc
280   "Creates a new symbol interned in package PACKAGE with the given PREFIX."
281   (declare (type string prefix))
282   (loop for name = (%make-symbol-name prefix (incf *gentemp-counter*))
283         while (nth-value 1 (find-symbol name package))
284         finally (return (values (intern name package)))))
285
286 ;;; This function is to be called just before a change which would affect the
287 ;;; symbol value. We don't absolutely have to call this function before such
288 ;;; changes, since such changes to constants are given as undefined behavior,
289 ;;; it's nice to do so. To circumvent this you need code like this:
290 ;;;
291 ;;;   (defvar foo)
292 ;;;   (defun set-foo (x) (setq foo x))
293 ;;;   (defconstant foo 42)
294 ;;;   (set-foo 13)
295 ;;;   foo => 13, (constantp 'foo) => t
296 ;;;
297 ;;; ...in which case you frankly deserve to lose.
298 (defun about-to-modify-symbol-value (symbol action &optional (new-value nil valuep) bind)
299   (declare (symbol symbol))
300   (flet ((describe-action ()
301            (ecase action
302              (set "set SYMBOL-VALUE of ~S")
303              (progv "bind ~S")
304              (compare-and-swap "compare-and-swap SYMBOL-VALUE of ~S")
305              (defconstant "define ~S as a constant")
306              (makunbound "make ~S unbound"))))
307     (let ((kind (info :variable :kind symbol)))
308       (multiple-value-bind (what continue)
309           (cond ((eq :constant kind)
310                  (cond ((eq symbol t)
311                         (values "Veritas aeterna. (can't ~@?)" nil))
312                        ((eq symbol nil)
313                         (values "Nihil ex nihil. (can't ~@?)" nil))
314                        ((keywordp symbol)
315                         (values "Can't ~@?." nil))
316                        (t
317                         (values "Constant modification: attempt to ~@?." t))))
318                 ((and bind (eq :global kind))
319                  (values "Can't ~@? (global variable)." nil)))
320         (when what
321           (if continue
322               (cerror "Modify the constant." what (describe-action) symbol)
323               (error what (describe-action) symbol)))
324         (when valuep
325           ;; :VARIABLE :TYPE is in the db only if it is declared, so no need to
326           ;; check.
327           (let ((type (info :variable :type symbol)))
328             (unless (sb!kernel::%%typep new-value type nil)
329               (let ((spec (type-specifier type)))
330                 (error 'simple-type-error
331                        :format-control "~@<Cannot ~@? to ~S, not of type ~S.~:@>"
332                        :format-arguments (list (describe-action) symbol new-value spec)
333                        :datum new-value
334                        :expected-type spec))))))))
335   (values))