lookup_interned_symbol uses get_handle_by_hash
[bpt/guile.git] / libguile / symbols.c
CommitLineData
05588a1a 1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5 21#ifdef HAVE_CONFIG_H
cf007485
RB
22# include <config.h>
23#endif
0f2d19dd 24
a0599745
MD
25#include "libguile/_scm.h"
26#include "libguile/chars.h"
27#include "libguile/eval.h"
ba393257 28#include "libguile/hash.h"
fb43bf74 29#include "libguile/smob.h"
a0599745
MD
30#include "libguile/variable.h"
31#include "libguile/alist.h"
7e73eaee 32#include "libguile/fluids.h"
a0599745
MD
33#include "libguile/strings.h"
34#include "libguile/vectors.h"
00ffa0e7 35#include "libguile/hashtab.h"
a0599745 36#include "libguile/weaks.h"
eb8db440 37#include "libguile/modules.h"
1206efbe
MV
38#include "libguile/read.h"
39#include "libguile/srfi-13.h"
a0599745
MD
40
41#include "libguile/validate.h"
42#include "libguile/symbols.h"
0f2d19dd 43
22fc179a
HWN
44#include "libguile/private-options.h"
45
46
95b88819
GH
47#ifdef HAVE_STRING_H
48#include <string.h>
49#endif
50
0f2d19dd
JB
51\f
52
0f979f3f
DH
53static SCM symbols;
54
a4c91488
MD
55#ifdef GUILE_DEBUG
56SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
57 (),
58 "Return the system symbol obarray.")
59#define FUNC_NAME s_scm_sys_symbols
60{
61 return symbols;
62}
63#undef FUNC_NAME
64#endif
65
0f979f3f
DH
66\f
67
0f2d19dd
JB
68/* {Symbols}
69 */
70
c35738c1
MD
71unsigned long
72scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
73{
3ee86942 74 return scm_i_symbol_hash (obj) % n;
c35738c1 75}
1cc91f1b 76
e0c83bf5
AW
77struct string_lookup_data
78{
17072fd2 79 SCM string;
e0c83bf5
AW
80 unsigned long string_hash;
81};
82
17072fd2
AW
83static int
84string_lookup_predicate_fn (SCM sym, void *closure)
e0c83bf5
AW
85{
86 struct string_lookup_data *data = closure;
87
17072fd2
AW
88 if (scm_i_symbol_hash (sym) == data->string_hash
89 && scm_i_symbol_length (sym) == scm_i_string_length (data->string))
fd0a5bbc 90 {
17072fd2
AW
91 size_t n = scm_i_symbol_length (sym);
92 while (n--)
93 if (scm_i_symbol_ref (sym, n) != scm_i_string_ref (data->string, n))
94 return 0;
95 return 1;
fd0a5bbc 96 }
17072fd2
AW
97 else
98 return 0;
e0c83bf5 99}
488b10b5 100
e0c83bf5
AW
101static SCM
102lookup_interned_symbol (SCM name, unsigned long raw_hash)
103{
104 struct string_lookup_data data;
105 SCM handle;
106
17072fd2 107 data.string = name;
e0c83bf5
AW
108 data.string_hash = raw_hash;
109
110 /* Strictly speaking, we should take a lock here. But instead we rely
111 on the fact that if this fails, we do take the lock on the
17072fd2
AW
112 intern_symbol path; and since nothing deletes from the hash table
113 except GC, we should be OK. */
114 handle = scm_hash_fn_get_handle_by_hash (symbols, raw_hash,
115 string_lookup_predicate_fn,
116 &data);
e0c83bf5
AW
117
118 if (scm_is_true (handle))
119 return SCM_CAR (handle);
120 else
121 return SCM_BOOL_F;
fd0a5bbc 122}
3ee86942 123
e0c83bf5
AW
124static unsigned long
125symbol_lookup_hash_fn (SCM obj, unsigned long max, void *closure)
126{
127 return scm_i_symbol_hash (obj) % max;
128}
129
130static SCM
131symbol_lookup_assoc_fn (SCM obj, SCM alist, void *closure)
05588a1a 132{
e0c83bf5
AW
133 for (; !scm_is_null (alist); alist = SCM_CDR (alist))
134 {
135 SCM sym = SCM_CAAR (alist);
136
137 if (scm_i_symbol_hash (sym) == scm_i_symbol_hash (obj)
138 && scm_is_true (scm_string_equal_p (scm_symbol_to_string (sym),
139 scm_symbol_to_string (obj))))
140 return SCM_CAR (alist);
141 }
142
143 return SCM_BOOL_F;
144}
145
146static scm_i_pthread_mutex_t intern_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
05588a1a 147
e0c83bf5
AW
148/* Intern SYMBOL, an uninterned symbol. Might return a different
149 symbol, if another one was interned at the same time. */
150static SCM
151intern_symbol (SCM symbol)
152{
153 SCM handle;
05588a1a 154
e0c83bf5
AW
155 scm_i_pthread_mutex_lock (&intern_lock);
156 handle = scm_hash_fn_create_handle_x (symbols, symbol, SCM_UNDEFINED,
157 symbol_lookup_hash_fn,
158 symbol_lookup_assoc_fn,
159 NULL);
160 scm_i_pthread_mutex_unlock (&intern_lock);
05588a1a 161
e0c83bf5 162 return SCM_CAR (handle);
05588a1a
LC
163}
164
fd0a5bbc 165static SCM
e23106d5 166scm_i_str2symbol (SCM str)
fd0a5bbc
HWN
167{
168 SCM symbol;
e23106d5 169 size_t raw_hash = scm_i_string_hash (str);
b52e071b 170
e23106d5 171 symbol = lookup_interned_symbol (str, raw_hash);
e0c83bf5
AW
172 if (scm_is_true (symbol))
173 return symbol;
174 else
05588a1a
LC
175 {
176 /* The symbol was not found, create it. */
177 symbol = scm_i_make_symbol (str, 0, raw_hash,
178 scm_cons (SCM_BOOL_F, SCM_EOL));
e0c83bf5 179 return intern_symbol (symbol);
05588a1a 180 }
b52e071b
DH
181}
182
fd0a5bbc 183
3ee86942 184static SCM
e23106d5 185scm_i_str2uninterned_symbol (SCM str)
ac48757b 186{
e23106d5 187 size_t raw_hash = scm_i_string_hash (str);
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);
e23106d5 222 return scm_i_str2uninterned_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);
e23106d5 284 return scm_i_str2symbol (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{
e23106d5 391 return scm_from_locale_symboln (sym, -1);
af68e5e5 392}
af68e5e5 393
3ee86942
MV
394SCM
395scm_from_locale_symboln (const char *sym, size_t len)
396{
e23106d5
MG
397 SCM str = scm_from_locale_stringn (sym, len);
398 return scm_i_str2symbol (str);
fd0a5bbc
HWN
399}
400
401SCM
402scm_take_locale_symboln (char *sym, size_t len)
403{
e23106d5 404 SCM str;
fd0a5bbc 405
e23106d5
MG
406 str = scm_take_locale_stringn (sym, len);
407 return scm_i_str2symbol (str);
fd0a5bbc
HWN
408}
409
410SCM
411scm_take_locale_symbol (char *sym)
412{
413 return scm_take_locale_symboln (sym, (size_t)-1);
3ee86942 414}
af68e5e5 415
ad5cbc47
AW
416SCM
417scm_from_latin1_symbol (const char *sym)
418{
419 return scm_from_latin1_symboln (sym, -1);
420}
421
422SCM
423scm_from_latin1_symboln (const char *sym, size_t len)
424{
425 SCM str = scm_from_latin1_stringn (sym, len);
426 return scm_i_str2symbol (str);
427}
428
429SCM
430scm_from_utf8_symbol (const char *sym)
431{
432 return scm_from_utf8_symboln (sym, -1);
433}
434
435SCM
436scm_from_utf8_symboln (const char *sym, size_t len)
437{
438 SCM str = scm_from_utf8_stringn (sym, len);
439 return scm_i_str2symbol (str);
440}
441
0f979f3f
DH
442void
443scm_symbols_prehistory ()
444{
e11e83f3 445 symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
0f979f3f
DH
446}
447
448
0f2d19dd
JB
449void
450scm_init_symbols ()
0f2d19dd 451{
a0599745 452#include "libguile/symbols.x"
0f2d19dd 453}
89e00824
ML
454
455/*
456 Local Variables:
457 c-file-style: "gnu"
458 End:
459*/