Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / symbols.c
... / ...
CommitLineData
1/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19\f
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include "libguile/_scm.h"
25#include "libguile/chars.h"
26#include "libguile/eval.h"
27#include "libguile/hash.h"
28#include "libguile/smob.h"
29#include "libguile/variable.h"
30#include "libguile/alist.h"
31#include "libguile/fluids.h"
32#include "libguile/strings.h"
33#include "libguile/vectors.h"
34#include "libguile/hashtab.h"
35#include "libguile/weaks.h"
36#include "libguile/modules.h"
37#include "libguile/read.h"
38#include "libguile/srfi-13.h"
39
40#include "libguile/validate.h"
41#include "libguile/symbols.h"
42
43#include "libguile/private-options.h"
44
45
46#ifdef HAVE_STRING_H
47#include <string.h>
48#endif
49
50\f
51
52static SCM symbols;
53
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
65\f
66
67/* {Symbols}
68 */
69
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 *
77 * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
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{
87 return scm_i_symbol_hash (obj) % n;
88}
89
90static SCM
91lookup_interned_symbol (const char *name, size_t len,
92 unsigned long raw_hash)
93{
94 /* Try to find the symbol in the symbols table */
95 SCM result = SCM_BOOL_F;
96 SCM bucket, elt, previous_elt;
97 unsigned long hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
98
99 bucket = SCM_HASHTABLE_BUCKET (symbols, hash);
100 for (elt = bucket, previous_elt = SCM_BOOL_F;
101 !scm_is_null (elt);
102 previous_elt = elt, elt = SCM_CDR (elt))
103 {
104 SCM pair, sym;
105
106 pair = SCM_CAR (elt);
107 if (!scm_is_pair (pair))
108 abort ();
109
110 if (SCM_WEAK_PAIR_CAR_DELETED_P (pair))
111 {
112 /* PAIR is a weak pair whose key got nullified: remove it from
113 BUCKET. */
114 /* FIXME: Since this is done lazily, i.e., only when a new symbol
115 is to be inserted in a bucket containing deleted symbols, the
116 number of items in the hash table may remain erroneous for some
117 time, thus precluding proper rehashing. */
118 if (previous_elt != SCM_BOOL_F)
119 SCM_SETCDR (previous_elt, SCM_CDR (elt));
120 else
121 bucket = SCM_CDR (elt);
122
123 SCM_HASHTABLE_DECREMENT (symbols);
124 continue;
125 }
126
127 sym = SCM_CAR (pair);
128
129 if (scm_i_symbol_hash (sym) == raw_hash
130 && scm_i_symbol_length (sym) == len)
131 {
132 const char *chrs = scm_i_symbol_chars (sym);
133 size_t i = len;
134
135 while (i != 0)
136 {
137 --i;
138 if (name[i] != chrs[i])
139 goto next_symbol;
140 }
141
142 /* We found it. */
143 result = sym;
144 break;
145 }
146 next_symbol:
147 ;
148 }
149
150 if (SCM_HASHTABLE_N_ITEMS (symbols) < SCM_HASHTABLE_LOWER (symbols))
151 /* We removed many symbols in this pass so trigger a rehashing. */
152 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "lookup_interned_symbol");
153
154 return result;
155}
156
157static SCM
158scm_i_c_mem2symbol (const char *name, size_t len)
159{
160 SCM symbol;
161 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
162 size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
163
164 symbol = lookup_interned_symbol (name, len, raw_hash);
165 if (symbol != SCM_BOOL_F)
166 return symbol;
167
168 {
169 /* The symbol was not found - create it. */
170 SCM symbol = scm_i_c_make_symbol (name, len, 0, raw_hash,
171 scm_cons (SCM_BOOL_F, SCM_EOL));
172
173 SCM slot = SCM_HASHTABLE_BUCKET (symbols, hash);
174 SCM cell = scm_weak_car_pair (symbol, SCM_UNDEFINED);
175 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
176 SCM_HASHTABLE_INCREMENT (symbols);
177 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
178 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
179
180 return symbol;
181 }
182}
183
184static SCM
185scm_i_mem2symbol (SCM str)
186{
187 SCM symbol;
188 const char *name = scm_i_string_chars (str);
189 size_t len = scm_i_string_length (str);
190 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
191 size_t hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
192
193 symbol = lookup_interned_symbol (name, len, raw_hash);
194 if (symbol != SCM_BOOL_F)
195 return symbol;
196
197 {
198 /* The symbol was not found - create it. */
199 SCM symbol = scm_i_make_symbol (str, 0, raw_hash,
200 scm_cons (SCM_BOOL_F, SCM_EOL));
201
202 SCM slot = SCM_HASHTABLE_BUCKET (symbols, hash);
203 SCM cell = scm_weak_car_pair (symbol, SCM_UNDEFINED);
204 SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
205 SCM_HASHTABLE_INCREMENT (symbols);
206 if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
207 scm_i_rehash (symbols, scm_i_hash_symbol, 0, "scm_mem2symbol");
208
209 return symbol;
210 }
211}
212
213
214static SCM
215scm_i_mem2uninterned_symbol (SCM str)
216{
217 const char *name = scm_i_string_chars (str);
218 size_t len = scm_i_string_length (str);
219 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
220
221 return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
222 raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
223}
224
225SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
226 (SCM obj),
227 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
228 "@code{#f}.")
229#define FUNC_NAME s_scm_symbol_p
230{
231 return scm_from_bool (scm_is_symbol (obj));
232}
233#undef FUNC_NAME
234
235SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
236 (SCM symbol),
237 "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
238 "@code{#f}.")
239#define FUNC_NAME s_scm_symbol_interned_p
240{
241 SCM_VALIDATE_SYMBOL (1, symbol);
242 return scm_from_bool (scm_i_symbol_is_interned (symbol));
243}
244#undef FUNC_NAME
245
246SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
247 (SCM name),
248 "Return a new uninterned symbol with the name @var{name}. "
249 "The returned symbol is guaranteed to be unique and future "
250 "calls to @code{string->symbol} will not return it.")
251#define FUNC_NAME s_scm_make_symbol
252{
253 SCM_VALIDATE_STRING (1, name);
254 return scm_i_mem2uninterned_symbol (name);
255}
256#undef FUNC_NAME
257
258SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
259 (SCM s),
260 "Return the name of @var{symbol} as a string. If the symbol was\n"
261 "part of an object returned as the value of a literal expression\n"
262 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
263 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
264 "and its name contains alphabetic characters, then the string\n"
265 "returned will contain characters in the implementation's\n"
266 "preferred standard case---some implementations will prefer\n"
267 "upper case, others lower case. If the symbol was returned by\n"
268 "@code{string->symbol}, the case of characters in the string\n"
269 "returned will be the same as the case in the string that was\n"
270 "passed to @code{string->symbol}. It is an error to apply\n"
271 "mutation procedures like @code{string-set!} to strings returned\n"
272 "by this procedure.\n"
273 "\n"
274 "The following examples assume that the implementation's\n"
275 "standard case is lower case:\n"
276 "\n"
277 "@lisp\n"
278 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
279 "(symbol->string 'Martin) @result{} \"martin\"\n"
280 "(symbol->string\n"
281 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
282 "@end lisp")
283#define FUNC_NAME s_scm_symbol_to_string
284{
285 SCM_VALIDATE_SYMBOL (1, s);
286 return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
287}
288#undef FUNC_NAME
289
290
291SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
292 (SCM string),
293 "Return the symbol whose name is @var{string}. This procedure\n"
294 "can create symbols with names containing special characters or\n"
295 "letters in the non-standard case, but it is usually a bad idea\n"
296 "to create such symbols because in some implementations of\n"
297 "Scheme they cannot be read as themselves. See\n"
298 "@code{symbol->string}.\n"
299 "\n"
300 "The following examples assume that the implementation's\n"
301 "standard case is lower case:\n"
302 "\n"
303 "@lisp\n"
304 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
305 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
306 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
307 "(eq? 'JollyWog\n"
308 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
309 "(string=? \"K. Harper, M.D.\"\n"
310 " (symbol->string\n"
311 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
312 "@end lisp")
313#define FUNC_NAME s_scm_string_to_symbol
314{
315 SCM_VALIDATE_STRING (1, string);
316 return scm_i_mem2symbol (string);
317}
318#undef FUNC_NAME
319
320SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
321 (SCM str),
322 "Return the symbol whose name is @var{str}. @var{str} is\n"
323 "converted to lowercase before the conversion is done, if Guile\n"
324 "is currently reading symbols case-insensitively.")
325#define FUNC_NAME s_scm_string_ci_to_symbol
326{
327 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
328 ? scm_string_downcase(str)
329 : str);
330}
331#undef FUNC_NAME
332
333#define MAX_PREFIX_LENGTH 30
334
335SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
336 (SCM prefix),
337 "Create a new symbol with a name constructed from a prefix and\n"
338 "a counter value. The string @var{prefix} can be specified as\n"
339 "an optional argument. Default prefix is @code{ g}. The counter\n"
340 "is increased by 1 at each call. There is no provision for\n"
341 "resetting the counter.")
342#define FUNC_NAME s_scm_gensym
343{
344 static int gensym_counter = 0;
345
346 SCM suffix, name;
347 int n, n_digits;
348 char buf[SCM_INTBUFLEN];
349
350 if (SCM_UNBNDP (prefix))
351 prefix = scm_from_locale_string (" g");
352
353 /* mutex in case another thread looks and incs at the exact same moment */
354 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
355 n = gensym_counter++;
356 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
357
358 n_digits = scm_iint2str (n, 10, buf);
359 suffix = scm_from_locale_stringn (buf, n_digits);
360 name = scm_string_append (scm_list_2 (prefix, suffix));
361 return scm_string_to_symbol (name);
362}
363#undef FUNC_NAME
364
365SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
366 (SCM symbol),
367 "Return a hash value for @var{symbol}.")
368#define FUNC_NAME s_scm_symbol_hash
369{
370 SCM_VALIDATE_SYMBOL (1, symbol);
371 return scm_from_ulong (scm_i_symbol_hash (symbol));
372}
373#undef FUNC_NAME
374
375SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
376 (SCM s),
377 "Return the contents of @var{symbol}'s @dfn{function slot}.")
378#define FUNC_NAME s_scm_symbol_fref
379{
380 SCM_VALIDATE_SYMBOL (1, s);
381 return SCM_CAR (SCM_CELL_OBJECT_3 (s));
382}
383#undef FUNC_NAME
384
385
386SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
387 (SCM s),
388 "Return the @dfn{property list} currently associated with @var{symbol}.")
389#define FUNC_NAME s_scm_symbol_pref
390{
391 SCM_VALIDATE_SYMBOL (1, s);
392 return SCM_CDR (SCM_CELL_OBJECT_3 (s));
393}
394#undef FUNC_NAME
395
396
397SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
398 (SCM s, SCM val),
399 "Change the binding of @var{symbol}'s function slot.")
400#define FUNC_NAME s_scm_symbol_fset_x
401{
402 SCM_VALIDATE_SYMBOL (1, s);
403 SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
404 return SCM_UNSPECIFIED;
405}
406#undef FUNC_NAME
407
408
409SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
410 (SCM s, SCM val),
411 "Change the binding of @var{symbol}'s property slot.")
412#define FUNC_NAME s_scm_symbol_pset_x
413{
414 SCM_VALIDATE_SYMBOL (1, s);
415 SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
416 return SCM_UNSPECIFIED;
417}
418#undef FUNC_NAME
419
420SCM
421scm_from_locale_symbol (const char *sym)
422{
423 return scm_i_c_mem2symbol (sym, strlen (sym));
424}
425
426SCM
427scm_from_locale_symboln (const char *sym, size_t len)
428{
429 return scm_i_c_mem2symbol (sym, len);
430}
431
432SCM
433scm_take_locale_symboln (char *sym, size_t len)
434{
435 SCM res;
436 unsigned long raw_hash;
437
438 if (len == (size_t)-1)
439 len = strlen (sym);
440 else
441 {
442 /* Ensure STR is null terminated. A realloc for 1 extra byte should
443 often be satisfied from the alignment padding after the block, with
444 no actual data movement. */
445 sym = scm_realloc (sym, len+1);
446 sym[len] = '\0';
447 }
448
449 raw_hash = scm_string_hash ((unsigned char *)sym, len);
450 res = lookup_interned_symbol (sym, len, raw_hash);
451 if (res != SCM_BOOL_F)
452 {
453 free (sym);
454 return res;
455 }
456
457 res = scm_i_c_take_symbol (sym, len, 0, raw_hash,
458 scm_cons (SCM_BOOL_F, SCM_EOL));
459
460 return res;
461}
462
463SCM
464scm_take_locale_symbol (char *sym)
465{
466 return scm_take_locale_symboln (sym, (size_t)-1);
467}
468
469void
470scm_symbols_prehistory ()
471{
472 symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
473}
474
475
476void
477scm_init_symbols ()
478{
479#include "libguile/symbols.x"
480}
481
482/*
483 Local Variables:
484 c-file-style: "gnu"
485 End:
486*/