1 /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006 Free Software Foundation, Inc.
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.
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.
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
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"
40 #include "libguile/validate.h"
41 #include "libguile/symbols.h"
52 SCM_DEFINE (scm_sys_symbols
, "%symbols", 0, 0, 0,
54 "Return the system symbol obarray.")
55 #define FUNC_NAME s_scm_sys_symbols
67 /* In order to optimize reading speed, this function breaks part of
68 * the hashtable abstraction. The optimizations are:
70 * 1. The argument string can be compared directly to symbol objects
71 * without first creating an SCM string object. (This would have
72 * been necessary if we had used the hashtable API in hashtab.h.)
74 * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
77 * Both optimizations might be possible without breaking the
78 * abstraction if the API in hashtab.c is improved.
82 scm_i_hash_symbol (SCM obj
, unsigned long n
, void *closure
)
84 return scm_i_symbol_hash (obj
) % n
;
88 lookup_interned_symbol (const char *name
, size_t len
,
89 unsigned long raw_hash
)
91 /* Try to find the symbol in the symbols table */
92 SCM result
= SCM_BOOL_F
;
93 SCM bucket
, elt
, previous_elt
;
94 unsigned long hash
= raw_hash
% SCM_HASHTABLE_N_BUCKETS (symbols
);
96 bucket
= SCM_HASHTABLE_BUCKET (symbols
, hash
);
97 for (elt
= bucket
, previous_elt
= SCM_BOOL_F
;
99 previous_elt
= elt
, elt
= SCM_CDR (elt
))
103 pair
= SCM_CAR (elt
);
104 if (!scm_is_pair (pair
))
107 if (SCM_WEAK_PAIR_CAR_DELETED_P (pair
))
109 /* PAIR is a weak pair whose key got nullified: remove it from
111 /* FIXME: Since this is done lazily, i.e., only when a new symbol
112 is to be inserted in a bucket containing deleted symbols, the
113 number of items in the hash table may remain erroneous for some
114 time, thus precluding proper rehashing. */
115 if (previous_elt
!= SCM_BOOL_F
)
116 SCM_SETCDR (previous_elt
, SCM_CDR (elt
));
118 bucket
= SCM_CDR (elt
);
120 SCM_HASHTABLE_DECREMENT (symbols
);
124 sym
= SCM_CAR (pair
);
126 if (scm_i_symbol_hash (sym
) == raw_hash
127 && scm_i_symbol_length (sym
) == len
)
129 const char *chrs
= scm_i_symbol_chars (sym
);
135 if (name
[i
] != chrs
[i
])
147 if (SCM_HASHTABLE_N_ITEMS (symbols
) < SCM_HASHTABLE_LOWER (symbols
))
148 /* We removed many symbols in this pass so trigger a rehashing. */
149 scm_i_rehash (symbols
, scm_i_hash_symbol
, 0, "lookup_interned_symbol");
155 scm_i_c_mem2symbol (const char *name
, size_t len
)
158 size_t raw_hash
= scm_string_hash ((const unsigned char *) name
, len
);
159 size_t hash
= raw_hash
% SCM_HASHTABLE_N_BUCKETS (symbols
);
161 symbol
= lookup_interned_symbol (name
, len
, raw_hash
);
162 if (symbol
!= SCM_BOOL_F
)
166 /* The symbol was not found - create it. */
167 SCM symbol
= scm_i_c_make_symbol (name
, len
, 0, raw_hash
,
168 scm_cons (SCM_BOOL_F
, SCM_EOL
));
170 SCM slot
= SCM_HASHTABLE_BUCKET (symbols
, hash
);
171 SCM cell
= scm_weak_car_pair (symbol
, SCM_UNDEFINED
);
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");
182 scm_i_mem2symbol (SCM str
)
185 const char *name
= scm_i_string_chars (str
);
186 size_t len
= scm_i_string_length (str
);
187 size_t raw_hash
= scm_string_hash ((const unsigned char *) name
, len
);
188 size_t hash
= raw_hash
% SCM_HASHTABLE_N_BUCKETS (symbols
);
190 symbol
= lookup_interned_symbol (name
, len
, raw_hash
);
191 if (symbol
!= SCM_BOOL_F
)
195 /* The symbol was not found - create it. */
196 SCM symbol
= scm_i_make_symbol (str
, 0, raw_hash
,
197 scm_cons (SCM_BOOL_F
, SCM_EOL
));
199 SCM slot
= SCM_HASHTABLE_BUCKET (symbols
, hash
);
200 SCM cell
= scm_weak_car_pair (symbol
, SCM_UNDEFINED
);
201 SCM_SET_HASHTABLE_BUCKET (symbols
, hash
, scm_cons (cell
, slot
));
202 SCM_HASHTABLE_INCREMENT (symbols
);
203 if (SCM_HASHTABLE_N_ITEMS (symbols
) > SCM_HASHTABLE_UPPER (symbols
))
204 scm_i_rehash (symbols
, scm_i_hash_symbol
, 0, "scm_mem2symbol");
212 scm_i_mem2uninterned_symbol (SCM str
)
214 const char *name
= scm_i_string_chars (str
);
215 size_t len
= scm_i_string_length (str
);
216 size_t raw_hash
= scm_string_hash ((const unsigned char *) name
, len
);
218 return scm_i_make_symbol (str
, SCM_I_F_SYMBOL_UNINTERNED
,
219 raw_hash
, scm_cons (SCM_BOOL_F
, SCM_EOL
));
222 SCM_DEFINE (scm_symbol_p
, "symbol?", 1, 0, 0,
224 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
226 #define FUNC_NAME s_scm_symbol_p
228 return scm_from_bool (scm_is_symbol (obj
));
232 SCM_DEFINE (scm_symbol_interned_p
, "symbol-interned?", 1, 0, 0,
234 "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
236 #define FUNC_NAME s_scm_symbol_interned_p
238 SCM_VALIDATE_SYMBOL (1, symbol
);
239 return scm_from_bool (scm_i_symbol_is_interned (symbol
));
243 SCM_DEFINE (scm_make_symbol
, "make-symbol", 1, 0, 0,
245 "Return a new uninterned symbol with the name @var{name}. "
246 "The returned symbol is guaranteed to be unique and future "
247 "calls to @code{string->symbol} will not return it.")
248 #define FUNC_NAME s_scm_make_symbol
250 SCM_VALIDATE_STRING (1, name
);
251 return scm_i_mem2uninterned_symbol (name
);
255 SCM_DEFINE (scm_symbol_to_string
, "symbol->string", 1, 0, 0,
257 "Return the name of @var{symbol} as a string. If the symbol was\n"
258 "part of an object returned as the value of a literal expression\n"
259 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
260 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
261 "and its name contains alphabetic characters, then the string\n"
262 "returned will contain characters in the implementation's\n"
263 "preferred standard case---some implementations will prefer\n"
264 "upper case, others lower case. If the symbol was returned by\n"
265 "@code{string->symbol}, the case of characters in the string\n"
266 "returned will be the same as the case in the string that was\n"
267 "passed to @code{string->symbol}. It is an error to apply\n"
268 "mutation procedures like @code{string-set!} to strings returned\n"
269 "by this procedure.\n"
271 "The following examples assume that the implementation's\n"
272 "standard case is lower case:\n"
275 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
276 "(symbol->string 'Martin) @result{} \"martin\"\n"
278 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
280 #define FUNC_NAME s_scm_symbol_to_string
282 SCM_VALIDATE_SYMBOL (1, s
);
283 return scm_i_symbol_substring (s
, 0, scm_i_symbol_length (s
));
288 SCM_DEFINE (scm_string_to_symbol
, "string->symbol", 1, 0, 0,
290 "Return the symbol whose name is @var{string}. This procedure\n"
291 "can create symbols with names containing special characters or\n"
292 "letters in the non-standard case, but it is usually a bad idea\n"
293 "to create such symbols because in some implementations of\n"
294 "Scheme they cannot be read as themselves. See\n"
295 "@code{symbol->string}.\n"
297 "The following examples assume that the implementation's\n"
298 "standard case is lower case:\n"
301 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
302 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
303 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
305 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
306 "(string=? \"K. Harper, M.D.\"\n"
308 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
310 #define FUNC_NAME s_scm_string_to_symbol
312 SCM_VALIDATE_STRING (1, string
);
313 return scm_i_mem2symbol (string
);
317 SCM_DEFINE (scm_string_ci_to_symbol
, "string-ci->symbol", 1, 0, 0,
319 "Return the symbol whose name is @var{str}. @var{str} is\n"
320 "converted to lowercase before the conversion is done, if Guile\n"
321 "is currently reading symbols case-insensitively.")
322 #define FUNC_NAME s_scm_string_ci_to_symbol
324 return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
325 ? scm_string_downcase(str
)
330 #define MAX_PREFIX_LENGTH 30
332 SCM_DEFINE (scm_gensym
, "gensym", 0, 1, 0,
334 "Create a new symbol with a name constructed from a prefix and\n"
335 "a counter value. The string @var{prefix} can be specified as\n"
336 "an optional argument. Default prefix is @code{ g}. The counter\n"
337 "is increased by 1 at each call. There is no provision for\n"
338 "resetting the counter.")
339 #define FUNC_NAME s_scm_gensym
341 static int gensym_counter
= 0;
345 char buf
[SCM_INTBUFLEN
];
347 if (SCM_UNBNDP (prefix
))
348 prefix
= scm_from_locale_string (" g");
350 /* mutex in case another thread looks and incs at the exact same moment */
351 scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex
);
352 n
= gensym_counter
++;
353 scm_i_pthread_mutex_unlock (&scm_i_misc_mutex
);
355 n_digits
= scm_iint2str (n
, 10, buf
);
356 suffix
= scm_from_locale_stringn (buf
, n_digits
);
357 name
= scm_string_append (scm_list_2 (prefix
, suffix
));
358 return scm_string_to_symbol (name
);
362 SCM_DEFINE (scm_symbol_hash
, "symbol-hash", 1, 0, 0,
364 "Return a hash value for @var{symbol}.")
365 #define FUNC_NAME s_scm_symbol_hash
367 SCM_VALIDATE_SYMBOL (1, symbol
);
368 return scm_from_ulong (scm_i_symbol_hash (symbol
));
372 SCM_DEFINE (scm_symbol_fref
, "symbol-fref", 1, 0, 0,
374 "Return the contents of @var{symbol}'s @dfn{function slot}.")
375 #define FUNC_NAME s_scm_symbol_fref
377 SCM_VALIDATE_SYMBOL (1, s
);
378 return SCM_CAR (SCM_CELL_OBJECT_3 (s
));
383 SCM_DEFINE (scm_symbol_pref
, "symbol-pref", 1, 0, 0,
385 "Return the @dfn{property list} currently associated with @var{symbol}.")
386 #define FUNC_NAME s_scm_symbol_pref
388 SCM_VALIDATE_SYMBOL (1, s
);
389 return SCM_CDR (SCM_CELL_OBJECT_3 (s
));
394 SCM_DEFINE (scm_symbol_fset_x
, "symbol-fset!", 2, 0, 0,
396 "Change the binding of @var{symbol}'s function slot.")
397 #define FUNC_NAME s_scm_symbol_fset_x
399 SCM_VALIDATE_SYMBOL (1, s
);
400 SCM_SETCAR (SCM_CELL_OBJECT_3 (s
), val
);
401 return SCM_UNSPECIFIED
;
406 SCM_DEFINE (scm_symbol_pset_x
, "symbol-pset!", 2, 0, 0,
408 "Change the binding of @var{symbol}'s property slot.")
409 #define FUNC_NAME s_scm_symbol_pset_x
411 SCM_VALIDATE_SYMBOL (1, s
);
412 SCM_SETCDR (SCM_CELL_OBJECT_3 (s
), val
);
413 return SCM_UNSPECIFIED
;
418 scm_from_locale_symbol (const char *sym
)
420 return scm_i_c_mem2symbol (sym
, strlen (sym
));
424 scm_from_locale_symboln (const char *sym
, size_t len
)
426 return scm_i_c_mem2symbol (sym
, len
);
430 scm_take_locale_symboln (char *sym
, size_t len
)
433 unsigned long raw_hash
;
435 if (len
== (size_t)-1)
439 /* Ensure STR is null terminated. A realloc for 1 extra byte should
440 often be satisfied from the alignment padding after the block, with
441 no actual data movement. */
442 sym
= scm_realloc (sym
, len
+1);
446 raw_hash
= scm_string_hash ((unsigned char *)sym
, len
);
447 res
= lookup_interned_symbol (sym
, len
, raw_hash
);
448 if (res
!= SCM_BOOL_F
)
454 res
= scm_i_c_take_symbol (sym
, len
, 0, raw_hash
,
455 scm_cons (SCM_BOOL_F
, SCM_EOL
));
461 scm_take_locale_symbol (char *sym
)
463 return scm_take_locale_symboln (sym
, (size_t)-1);
467 scm_symbols_prehistory ()
469 symbols
= scm_make_weak_key_hash_table (scm_from_int (2139));
476 #include "libguile/symbols.x"