rework procedure-callers to stay correct as callees are redefined
[bpt/guile.git] / libguile / symbols.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
dbb605f5 20#ifdef HAVE_CONFIG_H
cf007485
RB
21# include <config.h>
22#endif
0f2d19dd 23
a0599745
MD
24#include "libguile/_scm.h"
25#include "libguile/chars.h"
26#include "libguile/eval.h"
ba393257 27#include "libguile/hash.h"
fb43bf74 28#include "libguile/smob.h"
a0599745
MD
29#include "libguile/variable.h"
30#include "libguile/alist.h"
7e73eaee 31#include "libguile/fluids.h"
a0599745
MD
32#include "libguile/strings.h"
33#include "libguile/vectors.h"
00ffa0e7 34#include "libguile/hashtab.h"
a0599745 35#include "libguile/weaks.h"
eb8db440 36#include "libguile/modules.h"
1206efbe
MV
37#include "libguile/read.h"
38#include "libguile/srfi-13.h"
a0599745
MD
39
40#include "libguile/validate.h"
41#include "libguile/symbols.h"
0f2d19dd 42
22fc179a
HWN
43#include "libguile/private-options.h"
44
45
95b88819
GH
46#ifdef HAVE_STRING_H
47#include <string.h>
48#endif
49
0f2d19dd
JB
50\f
51
0f979f3f
DH
52static SCM symbols;
53
a4c91488
MD
54#ifdef GUILE_DEBUG
55SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
56 (),
57 "Return the system symbol obarray.")
58#define FUNC_NAME s_scm_sys_symbols
59{
60 return symbols;
61}
62#undef FUNC_NAME
63#endif
64
0f979f3f
DH
65\f
66
0f2d19dd
JB
67/* {Symbols}
68 */
69
c35738c1
MD
70/* In order to optimize reading speed, this function breaks part of
71 * the hashtable abstraction. The optimizations are:
72 *
73 * 1. The argument string can be compared directly to symbol objects
74 * without first creating an SCM string object. (This would have
75 * been necessary if we had used the hashtable API in hashtab.h.)
76 *
3ee86942 77 * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
c35738c1
MD
78 * to speed up lookup.
79 *
80 * Both optimizations might be possible without breaking the
81 * abstraction if the API in hashtab.c is improved.
82 */
83
84unsigned long
85scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
86{
3ee86942 87 return scm_i_symbol_hash (obj) % n;
c35738c1 88}
1cc91f1b 89
3ee86942 90static SCM
fd0a5bbc
HWN
91lookup_interned_symbol (const char *name, size_t len,
92 unsigned long raw_hash)
b52e071b 93{
fd0a5bbc
HWN
94 /* Try to find the symbol in the symbols table */
95 SCM l;
96 unsigned long hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
97
98 for (l = SCM_HASHTABLE_BUCKET (symbols, hash);
99 !scm_is_null (l);
100 l = SCM_CDR (l))
101 {
102 SCM sym = SCM_CAAR (l);
103 if (scm_i_symbol_hash (sym) == raw_hash
104 && scm_i_symbol_length (sym) == len)
105 {
106 const char *chrs = scm_i_symbol_chars (sym);
107 size_t i = len;
108
109 while (i != 0)
110 {
111 --i;
112 if (name[i] != chrs[i])
113 goto next_symbol;
114 }
115
116 return sym;
117 }
118 next_symbol:
119 ;
120 }
121
122 return SCM_BOOL_F;
123}
3ee86942 124
fd0a5bbc
HWN
125static SCM
126scm_i_c_mem2symbol (const char *name, size_t len)
127{
128 SCM symbol;
6869328b 129 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
c35738c1 130 size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
b52e071b 131
fd0a5bbc
HWN
132 symbol = lookup_interned_symbol (name, len, raw_hash);
133 if (symbol != SCM_BOOL_F)
134 return symbol;
135
b52e071b 136 {
fd0a5bbc
HWN
137 /* The symbol was not found - create it. */
138 SCM symbol = scm_i_c_make_symbol (name, len, 0, raw_hash,
139 scm_cons (SCM_BOOL_F, SCM_EOL));
140
141 SCM slot = SCM_HASHTABLE_BUCKET (symbols, hash);
142 SCM cell = scm_cons (symbol, SCM_UNDEFINED);
143 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
144 SCM_HASHTABLE_INCREMENT (symbols);
145 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
146 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
147
148 return symbol;
b52e071b 149 }
fd0a5bbc
HWN
150}
151
152static SCM
153scm_i_mem2symbol (SCM str)
154{
155 SCM symbol;
156 const char *name = scm_i_string_chars (str);
157 size_t len = scm_i_string_length (str);
158 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
159 size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
160
161 symbol = lookup_interned_symbol (name, len, raw_hash);
162 if (symbol != SCM_BOOL_F)
163 return symbol;
b52e071b
DH
164
165 {
166 /* The symbol was not found - create it. */
6869328b 167 SCM symbol = scm_i_make_symbol (str, 0, raw_hash,
3ee86942 168 scm_cons (SCM_BOOL_F, SCM_EOL));
b52e071b 169
4057a3e0 170 SCM slot = SCM_HASHTABLE_BUCKET (symbols, hash);
c8a1bdc4 171 SCM cell = scm_cons (symbol, SCM_UNDEFINED);
c35738c1
MD
172 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
173 SCM_HASHTABLE_INCREMENT (symbols);
174 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
175 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
b52e071b
DH
176
177 return symbol;
178 }
179}
180
fd0a5bbc 181
3ee86942
MV
182static SCM
183scm_i_mem2uninterned_symbol (SCM str)
ac48757b 184{
3ee86942
MV
185 const char *name = scm_i_string_chars (str);
186 size_t len = scm_i_string_length (str);
6869328b 187 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
3ee86942 188
6869328b
MV
189 return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
190 raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
b52e071b
DH
191}
192
3b3b36dd 193SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
8e93e199 194 (SCM obj),
1e6808ea
MG
195 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
196 "@code{#f}.")
1bbd0b84 197#define FUNC_NAME s_scm_symbol_p
0f2d19dd 198{
3ee86942 199 return scm_from_bool (scm_is_symbol (obj));
0f2d19dd 200}
1bbd0b84 201#undef FUNC_NAME
0f2d19dd 202
ac48757b
MV
203SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
204 (SCM symbol),
205 "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
206 "@code{#f}.")
207#define FUNC_NAME s_scm_symbol_interned_p
208{
209 SCM_VALIDATE_SYMBOL (1, symbol);
3ee86942 210 return scm_from_bool (scm_i_symbol_is_interned (symbol));
ac48757b
MV
211}
212#undef FUNC_NAME
213
214SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
215 (SCM name),
216 "Return a new uninterned symbol with the name @var{name}. "
217 "The returned symbol is guaranteed to be unique and future "
d58d5bfc 218 "calls to @code{string->symbol} will not return it.")
ac48757b
MV
219#define FUNC_NAME s_scm_make_symbol
220{
ac48757b 221 SCM_VALIDATE_STRING (1, name);
3ee86942 222 return scm_i_mem2uninterned_symbol (name);
ac48757b
MV
223}
224#undef FUNC_NAME
225
3b3b36dd 226SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
1bbd0b84 227 (SCM s),
1e6808ea
MG
228 "Return the name of @var{symbol} as a string. If the symbol was\n"
229 "part of an object returned as the value of a literal expression\n"
7a095584 230 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
1e6808ea
MG
231 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
232 "and its name contains alphabetic characters, then the string\n"
233 "returned will contain characters in the implementation's\n"
234 "preferred standard case---some implementations will prefer\n"
235 "upper case, others lower case. If the symbol was returned by\n"
236 "@code{string->symbol}, the case of characters in the string\n"
237 "returned will be the same as the case in the string that was\n"
238 "passed to @code{string->symbol}. It is an error to apply\n"
239 "mutation procedures like @code{string-set!} to strings returned\n"
240 "by this procedure.\n"
241 "\n"
942e5b91 242 "The following examples assume that the implementation's\n"
1e6808ea
MG
243 "standard case is lower case:\n"
244 "\n"
942e5b91 245 "@lisp\n"
1e6808ea
MG
246 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
247 "(symbol->string 'Martin) @result{} \"martin\"\n"
5ffe9968 248 "(symbol->string\n"
942e5b91
MG
249 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
250 "@end lisp")
1bbd0b84 251#define FUNC_NAME s_scm_symbol_to_string
0f2d19dd 252{
28b06554 253 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 254 return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
0f2d19dd 255}
1bbd0b84 256#undef FUNC_NAME
0f2d19dd
JB
257
258
3b3b36dd 259SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
1e6808ea
MG
260 (SCM string),
261 "Return the symbol whose name is @var{string}. This procedure\n"
942e5b91
MG
262 "can create symbols with names containing special characters or\n"
263 "letters in the non-standard case, but it is usually a bad idea\n"
1e6808ea
MG
264 "to create such symbols because in some implementations of\n"
265 "Scheme they cannot be read as themselves. See\n"
266 "@code{symbol->string}.\n"
267 "\n"
942e5b91 268 "The following examples assume that the implementation's\n"
1e6808ea
MG
269 "standard case is lower case:\n"
270 "\n"
942e5b91
MG
271 "@lisp\n"
272 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
273 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
274 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
275 "(eq? 'JollyWog\n"
276 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
277 "(string=? \"K. Harper, M.D.\"\n"
278 " (symbol->string\n"
279 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
280 "@end lisp")
1bbd0b84 281#define FUNC_NAME s_scm_string_to_symbol
0f2d19dd 282{
1e6808ea 283 SCM_VALIDATE_STRING (1, string);
3ee86942 284 return scm_i_mem2symbol (string);
0f2d19dd 285}
1bbd0b84 286#undef FUNC_NAME
0f2d19dd 287
1206efbe
MV
288SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
289 (SCM str),
290 "Return the symbol whose name is @var{str}. @var{str} is\n"
291 "converted to lowercase before the conversion is done, if Guile\n"
292 "is currently reading symbols case-insensitively.")
293#define FUNC_NAME s_scm_string_ci_to_symbol
294{
295 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
296 ? scm_string_downcase(str)
297 : str);
298}
299#undef FUNC_NAME
300
86d31dfe 301#define MAX_PREFIX_LENGTH 30
0f2d19dd 302
86d31dfe
MV
303SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
304 (SCM prefix),
305 "Create a new symbol with a name constructed from a prefix and\n"
306 "a counter value. The string @var{prefix} can be specified as\n"
68dc153d 307 "an optional argument. Default prefix is @code{ g}. The counter\n"
86d31dfe
MV
308 "is increased by 1 at each call. There is no provision for\n"
309 "resetting the counter.")
310#define FUNC_NAME s_scm_gensym
0f2d19dd 311{
7426a638 312 static int gensym_counter = 0;
3ee86942
MV
313
314 SCM suffix, name;
315 int n, n_digits;
316 char buf[SCM_INTBUFLEN];
7426a638 317
86d31dfe 318 if (SCM_UNBNDP (prefix))
3ee86942
MV
319 prefix = scm_from_locale_string (" g");
320
321 /* mutex in case another thread looks and incs at the exact same moment */
9de87eea 322 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
3ee86942 323 n = gensym_counter++;
9de87eea 324 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
3ee86942
MV
325
326 n_digits = scm_iint2str (n, 10, buf);
327 suffix = scm_from_locale_stringn (buf, n_digits);
328 name = scm_string_append (scm_list_2 (prefix, suffix));
329 return scm_string_to_symbol (name);
0f2d19dd 330}
1bbd0b84 331#undef FUNC_NAME
0f2d19dd 332
86d31dfe
MV
333SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
334 (SCM symbol),
335 "Return a hash value for @var{symbol}.")
336#define FUNC_NAME s_scm_symbol_hash
0f2d19dd 337{
86d31dfe 338 SCM_VALIDATE_SYMBOL (1, symbol);
3ee86942 339 return scm_from_ulong (scm_i_symbol_hash (symbol));
0f2d19dd 340}
1bbd0b84 341#undef FUNC_NAME
0f2d19dd 342
3b3b36dd 343SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
1bbd0b84 344 (SCM s),
b380b885 345 "Return the contents of @var{symbol}'s @dfn{function slot}.")
1bbd0b84 346#define FUNC_NAME s_scm_symbol_fref
0f2d19dd 347{
34d19ef6 348 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 349 return SCM_CAR (SCM_CELL_OBJECT_3 (s));
0f2d19dd 350}
1bbd0b84 351#undef FUNC_NAME
0f2d19dd
JB
352
353
3b3b36dd 354SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
1bbd0b84 355 (SCM s),
b380b885 356 "Return the @dfn{property list} currently associated with @var{symbol}.")
1bbd0b84 357#define FUNC_NAME s_scm_symbol_pref
0f2d19dd 358{
34d19ef6 359 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 360 return SCM_CDR (SCM_CELL_OBJECT_3 (s));
0f2d19dd 361}
1bbd0b84 362#undef FUNC_NAME
0f2d19dd
JB
363
364
3b3b36dd 365SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
1bbd0b84 366 (SCM s, SCM val),
b380b885 367 "Change the binding of @var{symbol}'s function slot.")
1bbd0b84 368#define FUNC_NAME s_scm_symbol_fset_x
0f2d19dd 369{
34d19ef6 370 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 371 SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
0f2d19dd
JB
372 return SCM_UNSPECIFIED;
373}
1bbd0b84 374#undef FUNC_NAME
0f2d19dd
JB
375
376
3b3b36dd 377SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
1bbd0b84 378 (SCM s, SCM val),
b380b885 379 "Change the binding of @var{symbol}'s property slot.")
1bbd0b84 380#define FUNC_NAME s_scm_symbol_pset_x
0f2d19dd 381{
34d19ef6 382 SCM_VALIDATE_SYMBOL (1, s);
3ee86942 383 SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
0f2d19dd
JB
384 return SCM_UNSPECIFIED;
385}
1bbd0b84 386#undef FUNC_NAME
0f2d19dd 387
3ee86942
MV
388SCM
389scm_from_locale_symbol (const char *sym)
af68e5e5 390{
fd0a5bbc 391 return scm_i_c_mem2symbol (sym, strlen (sym));
af68e5e5 392}
af68e5e5 393
3ee86942
MV
394SCM
395scm_from_locale_symboln (const char *sym, size_t len)
396{
fd0a5bbc
HWN
397 return scm_i_c_mem2symbol (sym, len);
398}
399
400SCM
401scm_take_locale_symboln (char *sym, size_t len)
402{
403 SCM res;
404 unsigned long raw_hash;
405
406 if (len == (size_t)-1)
407 len = strlen (sym);
408 else
409 {
410 /* Ensure STR is null terminated. A realloc for 1 extra byte should
411 often be satisfied from the alignment padding after the block, with
412 no actual data movement. */
413 sym = scm_realloc (sym, len+1);
414 sym[len] = '\0';
415 }
416
417 raw_hash = scm_string_hash ((unsigned char *)sym, len);
418 res = lookup_interned_symbol (sym, len, raw_hash);
419 if (res != SCM_BOOL_F)
420 {
421 free (sym);
422 return res;
423 }
424
425 res = scm_i_c_take_symbol (sym, len, 0, raw_hash,
426 scm_cons (SCM_BOOL_F, SCM_EOL));
427
428 return res;
429}
430
431SCM
432scm_take_locale_symbol (char *sym)
433{
434 return scm_take_locale_symboln (sym, (size_t)-1);
3ee86942 435}
af68e5e5 436
0f979f3f
DH
437void
438scm_symbols_prehistory ()
439{
e11e83f3 440 symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
0f979f3f
DH
441 scm_permanent_object (symbols);
442}
443
444
0f2d19dd
JB
445void
446scm_init_symbols ()
0f2d19dd 447{
a0599745 448#include "libguile/symbols.x"
0f2d19dd 449}
89e00824
ML
450
451/*
452 Local Variables:
453 c-file-style: "gnu"
454 End:
455*/