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