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