0.6.8.9:
[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   (about-to-modify variable)
39   (%set-symbol-value variable new-value))
40
41 (defun %set-symbol-value (symbol new-value)
42   (%set-symbol-value symbol new-value))
43
44 (defun makunbound (variable)
45   #!+sb-doc
46   "VARIABLE must evaluate to a symbol. This symbol is made unbound,
47   removing any value it may currently have."
48   (set variable
49        (%primitive sb!c:make-other-immediate-type 0 sb!vm:unbound-marker-type))
50   variable)
51
52 (defun symbol-hash (symbol)
53   #!+sb-doc
54   "Return the built-in hash value for symbol."
55   (symbol-hash symbol))
56
57 (defun symbol-function (variable)
58   #!+sb-doc
59   "VARIABLE must evaluate to a symbol. This symbol's current definition
60    is returned. Settable with SETF."
61   (raw-definition variable))
62
63 (defun fset (symbol new-value)
64   (declare (type symbol symbol) (type function new-value))
65   (setf (raw-definition symbol) new-value))
66
67 (defun symbol-plist (variable)
68   #!+sb-doc
69   "Return the property list of a symbol."
70   (symbol-plist variable))
71
72 (defun %set-symbol-plist (symbol new-value)
73   (setf (symbol-plist symbol) new-value))
74
75 (defun symbol-name (variable)
76   #!+sb-doc
77   "Return the print name of a symbol."
78   (symbol-name variable))
79
80 (defun symbol-package (variable)
81   #!+sb-doc
82   "Return the package a symbol is interned in, or NIL if none."
83   (symbol-package variable))
84
85 (defun %set-symbol-package (symbol package)
86   (declare (type symbol symbol))
87   (%set-symbol-package symbol package))
88
89 (defun make-symbol (string)
90   #!+sb-doc
91   "Make and return a new symbol with the STRING as its print name."
92   #!-gengc (make-symbol string)
93   #!+gengc (%make-symbol (random most-positive-fixnum) string))
94
95 (defun get (symbol indicator &optional (default nil))
96   #!+sb-doc
97   "Look on the property list of SYMBOL for the specified INDICATOR. If this
98   is found, return the associated value, else return DEFAULT."
99   (do ((pl (symbol-plist symbol) (cddr pl)))
100       ((atom pl) default)
101     (cond ((atom (cdr pl))
102            (error "~S has an odd number of items in its property list."
103                    symbol))
104           ((eq (car pl) indicator)
105            (return (cadr pl))))))
106
107 (defun %put (symbol indicator value)
108   #!+sb-doc
109   "The VALUE is added as a property of SYMBOL under the specified INDICATOR.
110   Returns VALUE."
111   (do ((pl (symbol-plist symbol) (cddr pl)))
112       ((endp pl)
113        (setf (symbol-plist symbol)
114              (list* indicator value (symbol-plist symbol)))
115        value)
116     (cond ((endp (cdr pl))
117            (error "~S has an odd number of items in its property list."
118                   symbol))
119           ((eq (car pl) indicator)
120            (rplaca (cdr pl) value)
121            (return value)))))
122
123 (defun remprop (symbol indicator)
124   #!+sb-doc
125   "Look on property list of SYMBOL for property with specified
126   INDICATOR. If found, splice this indicator and its value out of
127   the plist, and return the tail of the original list starting with
128   INDICATOR. If not found, return () with no side effects.
129
130   NOTE: The ANSI specification requires REMPROP to return true (not false)
131   or false (the symbol NIL). Portable code should not rely on any other value."
132   (do ((pl (symbol-plist symbol) (cddr pl))
133        (prev nil pl))
134       ((atom pl) nil)
135     (cond ((atom (cdr pl))
136            (error "~S has an odd number of items in its property list."
137                   symbol))
138           ((eq (car pl) indicator)
139            (cond (prev (rplacd (cdr prev) (cddr pl)))
140                  (t
141                   (setf (symbol-plist symbol) (cddr pl))))
142            (return pl)))))
143
144 (defun getf (place indicator &optional (default ()))
145   #!+sb-doc
146   "Searches the property list stored in Place for an indicator EQ to Indicator.
147   If one is found, the corresponding value is returned, else the Default is
148   returned."
149   (do ((plist place (cddr plist)))
150       ((null plist) default)
151     (cond ((atom (cdr plist))
152            (error "~S is a malformed property list."
153                   place))
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 "~S is a malformed proprty list."
175                   place))
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   "Returns 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 *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))))))))