0.9.2.43:
[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 %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 (defun boundp (symbol)
27   #!+sb-doc
28   "Return non-NIL if SYMBOL is bound to a value."
29   (boundp symbol))
30
31 (defun set (symbol new-value)
32   #!+sb-doc
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))
37
38 (defun %set-symbol-value (symbol new-value)
39   (%set-symbol-value symbol new-value))
40
41 (defun makunbound (symbol)
42   #!+sb-doc
43   "Make SYMBOL unbound, removing any value it may currently have."
44   (with-single-package-locked-error (:symbol symbol "unbinding the symbol ~A")
45     (set symbol
46          (%primitive sb!c:make-other-immediate-type
47                      0
48                      sb!vm:unbound-marker-widetag))
49     symbol))
50
51 ;;; Return the built-in hash value for SYMBOL.
52 (defun symbol-hash (symbol)
53   (symbol-hash symbol))
54
55 (defun symbol-function (symbol)
56   #!+sb-doc
57   "Return SYMBOL's current function definition. Settable with SETF."
58   (%coerce-name-to-fun symbol))
59
60 (defun (setf symbol-function) (new-value symbol)
61   (declare (type symbol symbol) (type function new-value))
62   (with-single-package-locked-error
63       (:symbol symbol "setting the symbol-function of ~A")
64     (setf (%coerce-name-to-fun symbol) new-value)))
65
66 (defun symbol-plist (symbol)
67   #!+sb-doc
68   "Return SYMBOL's property list."
69   (symbol-plist symbol))
70
71 (defun %set-symbol-plist (symbol new-value)
72   (setf (symbol-plist symbol) new-value))
73
74 (defun symbol-name (symbol)
75   #!+sb-doc
76   "Return SYMBOL's name as a string."
77   (symbol-name symbol))
78
79 (defun symbol-package (symbol)
80   #!+sb-doc
81   "Return the package SYMBOL was interned in, or NIL if none."
82   (symbol-package symbol))
83
84 (defun %set-symbol-package (symbol package)
85   (declare (type symbol symbol))
86   (%set-symbol-package symbol package))
87
88 (defun make-symbol (string)
89   #!+sb-doc
90   "Make and return a new symbol with the STRING as its print name."
91   (make-symbol string))
92
93 (defun get (symbol indicator &optional (default nil))
94   #!+sb-doc
95   "Look on the property list of SYMBOL for the specified INDICATOR. If this
96   is found, return the associated value, else return DEFAULT."
97   (do ((pl (symbol-plist symbol) (cddr pl)))
98       ((atom pl) default)
99     (cond ((atom (cdr pl))
100            (error "~S has an odd number of items in its property list."
101                    symbol))
102           ((eq (car pl) indicator)
103            (return (cadr pl))))))
104
105 (defun %put (symbol indicator value)
106   #!+sb-doc
107   "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
108   Returns VALUE."
109   (do ((pl (symbol-plist symbol) (cddr pl)))
110       ((endp pl)
111        (setf (symbol-plist symbol)
112              (list* indicator value (symbol-plist symbol)))
113        value)
114     (cond ((endp (cdr pl))
115            (error "~S has an odd number of items in its property list."
116                   symbol))
117           ((eq (car pl) indicator)
118            (rplaca (cdr pl) value)
119            (return value)))))
120
121 (defun remprop (symbol indicator)
122   #!+sb-doc
123   "Look on property list of SYMBOL for property with specified
124   INDICATOR. If found, splice this indicator and its value out of
125   the plist, and return the tail of the original list starting with
126   INDICATOR. If not found, return () with no side effects.
127
128   NOTE: The ANSI specification requires REMPROP to return true (not false)
129   or false (the symbol NIL). Portable code should not rely on any other value."
130   (do ((pl (symbol-plist symbol) (cddr pl))
131        (prev nil pl))
132       ((atom pl) nil)
133     (cond ((atom (cdr pl))
134            (error "~S has an odd number of items in its property list."
135                   symbol))
136           ((eq (car pl) indicator)
137            (cond (prev (rplacd (cdr prev) (cddr pl)))
138                  (t
139                   (setf (symbol-plist symbol) (cddr pl))))
140            (return pl)))))
141
142 (defun getf (place indicator &optional (default ()))
143   #!+sb-doc
144   "Search the property list stored in Place for an indicator EQ to INDICATOR.
145   If one is found, return the corresponding value, else return DEFAULT."
146   (do ((plist place (cddr plist)))
147       ((null plist) default)
148     (cond ((atom (cdr plist))
149            (error 'simple-type-error
150                   :format-control "malformed property list: ~S."
151                   :format-arguments (list place)
152                   :datum (cdr plist)
153                   :expected-type 'cons))
154           ((eq (car plist) indicator)
155            (return (cadr plist))))))
156
157 (defun %putf (place property new-value)
158   (declare (type list place))
159   (do ((plist place (cddr plist)))
160       ((endp plist) (list* property new-value place))
161     (declare (type list plist))
162     (when (eq (car plist) property)
163       (setf (cadr plist) new-value)
164       (return place))))
165
166 (defun get-properties (place indicator-list)
167   #!+sb-doc
168   "Like GETF, except that INDICATOR-LIST is a list of indicators which will
169   be looked for in the property list stored in PLACE. Three values are
170   returned, see manual for details."
171   (do ((plist place (cddr plist)))
172       ((null plist) (values nil nil nil))
173     (cond ((atom (cdr plist))
174            (error 'simple-type-error
175                   :format-control "malformed property list: ~S."
176                   :format-arguments (list place)
177                   :datum (cdr plist)
178                   :expected-type 'cons))
179           ((memq (car plist) indicator-list)
180            (return (values (car plist) (cadr plist) plist))))))
181
182 (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol)
183   #!+sb-doc
184   "Make and return a new uninterned symbol with the same print name
185   as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound
186   nor fbound and has no properties, else it has a copy of SYMBOL's
187   function, value and property list."
188   (declare (type symbol symbol))
189   (setq new-symbol (make-symbol (symbol-name symbol)))
190   (when copy-props
191     (%set-symbol-value new-symbol
192                        (%primitive sb!c:fast-symbol-value symbol))
193     (setf (symbol-plist new-symbol)
194           (copy-list (symbol-plist symbol)))
195     (when (fboundp symbol)
196       (setf (symbol-function new-symbol) (symbol-function symbol))))
197   new-symbol)
198
199 ;;; FIXME: This declaration should be redundant.
200 (declaim (special *keyword-package*))
201
202 (defun keywordp (object)
203   #!+sb-doc
204   "Return true if Object is a symbol in the \"KEYWORD\" package."
205   (and (symbolp object)
206        (eq (symbol-package object) *keyword-package*)))
207 \f
208 ;;;; GENSYM and friends
209
210 (defvar *gensym-counter* 0
211   #!+sb-doc
212   "counter for generating unique GENSYM symbols")
213 (declaim (type unsigned-byte *gensym-counter*))
214
215 (defun gensym (&optional (thing "G"))
216   #!+sb-doc
217   "Creates a new uninterned symbol whose name is a prefix string (defaults
218    to \"G\"), followed by a decimal number. Thing, when supplied, will
219    alter the prefix if it is a string, or be used for the decimal number
220    if it is a number, of this symbol. The default value of the number is
221    the current value of *gensym-counter* which is incremented each time
222    it is used."
223   (let ((old *gensym-counter*))
224     (unless (numberp thing)
225       (let ((new (etypecase old
226                    (index (1+ old))
227                    (unsigned-byte (1+ old)))))
228         (declare (optimize (speed 3) (safety 0)(inhibit-warnings 3)))
229         (setq *gensym-counter* new)))
230     (multiple-value-bind (prefix int)
231         (etypecase thing
232           (simple-string (values thing old))
233           (fixnum (values "G" thing))
234           (string (values (coerce thing 'simple-string) old)))
235       (declare (simple-string prefix))
236       (make-symbol
237        (concatenate 'simple-string prefix
238                     (the simple-string
239                          (quick-integer-to-string int)))))))
240
241 (defvar *gentemp-counter* 0)
242 (declaim (type unsigned-byte *gentemp-counter*))
243
244 (defun gentemp (&optional (prefix "T") (package (sane-package)))
245   #!+sb-doc
246   "Creates a new symbol interned in package PACKAGE with the given PREFIX."
247   (declare (type string prefix))
248   (loop
249     (let ((*print-base* 10)
250           (*print-radix* nil)
251           (*print-pretty* nil)
252           (new-pname (format nil "~A~D" prefix (incf *gentemp-counter*))))
253       (multiple-value-bind (symbol existsp) (find-symbol new-pname package)
254         (declare (ignore symbol))
255         (unless existsp (return (values (intern new-pname package))))))))