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