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