symbols.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2009 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. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/chars.h"
  22. #include "libguile/eval.h"
  23. #include "libguile/hash.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/variable.h"
  26. #include "libguile/alist.h"
  27. #include "libguile/fluids.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/vectors.h"
  30. #include "libguile/hashtab.h"
  31. #include "libguile/weaks.h"
  32. #include "libguile/modules.h"
  33. #include "libguile/read.h"
  34. #include "libguile/srfi-13.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/symbols.h"
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40. static SCM symbols;
  41. #ifdef GUILE_DEBUG
  42. SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
  43. (),
  44. "Return the system symbol obarray.")
  45. #define FUNC_NAME s_scm_sys_symbols
  46. {
  47. return symbols;
  48. }
  49. #undef FUNC_NAME
  50. #endif
  51. /* {Symbols}
  52. */
  53. /* In order to optimize reading speed, this function breaks part of
  54. * the hashtable abstraction. The optimizations are:
  55. *
  56. * 1. The argument string can be compared directly to symbol objects
  57. * without first creating an SCM string object. (This would have
  58. * been necessary if we had used the hashtable API in hashtab.h.)
  59. *
  60. * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
  61. * to speed up lookup.
  62. *
  63. * Both optimizations might be possible without breaking the
  64. * abstraction if the API in hashtab.c is improved.
  65. */
  66. unsigned long
  67. scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
  68. {
  69. return scm_i_symbol_hash (obj) % n;
  70. }
  71. static SCM
  72. lookup_interned_symbol (const char *name, size_t len,
  73. unsigned long raw_hash)
  74. {
  75. /* Try to find the symbol in the symbols table */
  76. SCM l;
  77. unsigned long hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (symbols);
  78. for (l = SCM_HASHTABLE_BUCKET (symbols, hash);
  79. !scm_is_null (l);
  80. l = SCM_CDR (l))
  81. {
  82. SCM sym = SCM_CAAR (l);
  83. if (scm_i_symbol_hash (sym) == raw_hash
  84. && scm_i_symbol_length (sym) == len)
  85. {
  86. const char *chrs = scm_i_symbol_chars (sym);
  87. size_t i = len;
  88. while (i != 0)
  89. {
  90. --i;
  91. if (name[i] != chrs[i])
  92. goto next_symbol;
  93. }
  94. return sym;
  95. }
  96. next_symbol:
  97. ;
  98. }
  99. return SCM_BOOL_F;
  100. }
  101. /* Intern SYMBOL, an uninterned symbol. */
  102. static void
  103. intern_symbol (SCM symbol)
  104. {
  105. SCM slot, cell;
  106. unsigned long hash;
  107. hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (symbols);
  108. slot = SCM_HASHTABLE_BUCKET (symbols, hash);
  109. cell = scm_cons (symbol, SCM_UNDEFINED);
  110. SCM_SET_HASHTABLE_BUCKET (symbols, hash, scm_cons (cell, slot));
  111. SCM_HASHTABLE_INCREMENT (symbols);
  112. if (SCM_HASHTABLE_N_ITEMS (symbols) > SCM_HASHTABLE_UPPER (symbols))
  113. scm_i_rehash (symbols, scm_i_hash_symbol, 0, "intern_symbol");
  114. }
  115. static SCM
  116. scm_i_c_mem2symbol (const char *name, size_t len)
  117. {
  118. SCM symbol;
  119. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  120. symbol = lookup_interned_symbol (name, len, raw_hash);
  121. if (scm_is_false (symbol))
  122. {
  123. /* The symbol was not found, create it. */
  124. symbol = scm_i_c_make_symbol (name, len, 0, raw_hash,
  125. scm_cons (SCM_BOOL_F, SCM_EOL));
  126. intern_symbol (symbol);
  127. }
  128. return symbol;
  129. }
  130. static SCM
  131. scm_i_mem2symbol (SCM str)
  132. {
  133. SCM symbol;
  134. const char *name = scm_i_string_chars (str);
  135. size_t len = scm_i_string_length (str);
  136. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  137. symbol = lookup_interned_symbol (name, len, raw_hash);
  138. if (scm_is_false (symbol))
  139. {
  140. /* The symbol was not found, create it. */
  141. symbol = scm_i_make_symbol (str, 0, raw_hash,
  142. scm_cons (SCM_BOOL_F, SCM_EOL));
  143. intern_symbol (symbol);
  144. }
  145. return symbol;
  146. }
  147. static SCM
  148. scm_i_mem2uninterned_symbol (SCM str)
  149. {
  150. const char *name = scm_i_string_chars (str);
  151. size_t len = scm_i_string_length (str);
  152. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  153. return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
  154. raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
  155. }
  156. SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
  157. (SCM obj),
  158. "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
  159. "@code{#f}.")
  160. #define FUNC_NAME s_scm_symbol_p
  161. {
  162. return scm_from_bool (scm_is_symbol (obj));
  163. }
  164. #undef FUNC_NAME
  165. SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
  166. (SCM symbol),
  167. "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
  168. "@code{#f}.")
  169. #define FUNC_NAME s_scm_symbol_interned_p
  170. {
  171. SCM_VALIDATE_SYMBOL (1, symbol);
  172. return scm_from_bool (scm_i_symbol_is_interned (symbol));
  173. }
  174. #undef FUNC_NAME
  175. SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
  176. (SCM name),
  177. "Return a new uninterned symbol with the name @var{name}. "
  178. "The returned symbol is guaranteed to be unique and future "
  179. "calls to @code{string->symbol} will not return it.")
  180. #define FUNC_NAME s_scm_make_symbol
  181. {
  182. SCM_VALIDATE_STRING (1, name);
  183. return scm_i_mem2uninterned_symbol (name);
  184. }
  185. #undef FUNC_NAME
  186. SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
  187. (SCM s),
  188. "Return the name of @var{symbol} as a string. If the symbol was\n"
  189. "part of an object returned as the value of a literal expression\n"
  190. "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
  191. "Report on Scheme}) or by a call to the @code{read} procedure,\n"
  192. "and its name contains alphabetic characters, then the string\n"
  193. "returned will contain characters in the implementation's\n"
  194. "preferred standard case---some implementations will prefer\n"
  195. "upper case, others lower case. If the symbol was returned by\n"
  196. "@code{string->symbol}, the case of characters in the string\n"
  197. "returned will be the same as the case in the string that was\n"
  198. "passed to @code{string->symbol}. It is an error to apply\n"
  199. "mutation procedures like @code{string-set!} to strings returned\n"
  200. "by this procedure.\n"
  201. "\n"
  202. "The following examples assume that the implementation's\n"
  203. "standard case is lower case:\n"
  204. "\n"
  205. "@lisp\n"
  206. "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
  207. "(symbol->string 'Martin) @result{} \"martin\"\n"
  208. "(symbol->string\n"
  209. " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
  210. "@end lisp")
  211. #define FUNC_NAME s_scm_symbol_to_string
  212. {
  213. SCM_VALIDATE_SYMBOL (1, s);
  214. return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
  215. }
  216. #undef FUNC_NAME
  217. SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
  218. (SCM string),
  219. "Return the symbol whose name is @var{string}. This procedure\n"
  220. "can create symbols with names containing special characters or\n"
  221. "letters in the non-standard case, but it is usually a bad idea\n"
  222. "to create such symbols because in some implementations of\n"
  223. "Scheme they cannot be read as themselves. See\n"
  224. "@code{symbol->string}.\n"
  225. "\n"
  226. "The following examples assume that the implementation's\n"
  227. "standard case is lower case:\n"
  228. "\n"
  229. "@lisp\n"
  230. "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
  231. "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
  232. "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
  233. "(eq? 'JollyWog\n"
  234. " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
  235. "(string=? \"K. Harper, M.D.\"\n"
  236. " (symbol->string\n"
  237. " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
  238. "@end lisp")
  239. #define FUNC_NAME s_scm_string_to_symbol
  240. {
  241. SCM_VALIDATE_STRING (1, string);
  242. return scm_i_mem2symbol (string);
  243. }
  244. #undef FUNC_NAME
  245. SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
  246. (SCM str),
  247. "Return the symbol whose name is @var{str}. @var{str} is\n"
  248. "converted to lowercase before the conversion is done, if Guile\n"
  249. "is currently reading symbols case-insensitively.")
  250. #define FUNC_NAME s_scm_string_ci_to_symbol
  251. {
  252. return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
  253. ? scm_string_downcase(str)
  254. : str);
  255. }
  256. #undef FUNC_NAME
  257. #define MAX_PREFIX_LENGTH 30
  258. SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
  259. (SCM prefix),
  260. "Create a new symbol with a name constructed from a prefix and\n"
  261. "a counter value. The string @var{prefix} can be specified as\n"
  262. "an optional argument. Default prefix is @code{ g}. The counter\n"
  263. "is increased by 1 at each call. There is no provision for\n"
  264. "resetting the counter.")
  265. #define FUNC_NAME s_scm_gensym
  266. {
  267. static int gensym_counter = 0;
  268. SCM suffix, name;
  269. int n, n_digits;
  270. char buf[SCM_INTBUFLEN];
  271. if (SCM_UNBNDP (prefix))
  272. prefix = scm_from_locale_string (" g");
  273. /* mutex in case another thread looks and incs at the exact same moment */
  274. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  275. n = gensym_counter++;
  276. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  277. n_digits = scm_iint2str (n, 10, buf);
  278. suffix = scm_from_locale_stringn (buf, n_digits);
  279. name = scm_string_append (scm_list_2 (prefix, suffix));
  280. return scm_string_to_symbol (name);
  281. }
  282. #undef FUNC_NAME
  283. SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
  284. (SCM symbol),
  285. "Return a hash value for @var{symbol}.")
  286. #define FUNC_NAME s_scm_symbol_hash
  287. {
  288. SCM_VALIDATE_SYMBOL (1, symbol);
  289. return scm_from_ulong (scm_i_symbol_hash (symbol));
  290. }
  291. #undef FUNC_NAME
  292. SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
  293. (SCM s),
  294. "Return the contents of @var{symbol}'s @dfn{function slot}.")
  295. #define FUNC_NAME s_scm_symbol_fref
  296. {
  297. SCM_VALIDATE_SYMBOL (1, s);
  298. return SCM_CAR (SCM_CELL_OBJECT_3 (s));
  299. }
  300. #undef FUNC_NAME
  301. SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
  302. (SCM s),
  303. "Return the @dfn{property list} currently associated with @var{symbol}.")
  304. #define FUNC_NAME s_scm_symbol_pref
  305. {
  306. SCM_VALIDATE_SYMBOL (1, s);
  307. return SCM_CDR (SCM_CELL_OBJECT_3 (s));
  308. }
  309. #undef FUNC_NAME
  310. SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
  311. (SCM s, SCM val),
  312. "Change the binding of @var{symbol}'s function slot.")
  313. #define FUNC_NAME s_scm_symbol_fset_x
  314. {
  315. SCM_VALIDATE_SYMBOL (1, s);
  316. SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
  317. return SCM_UNSPECIFIED;
  318. }
  319. #undef FUNC_NAME
  320. SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
  321. (SCM s, SCM val),
  322. "Change the binding of @var{symbol}'s property slot.")
  323. #define FUNC_NAME s_scm_symbol_pset_x
  324. {
  325. SCM_VALIDATE_SYMBOL (1, s);
  326. SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
  327. return SCM_UNSPECIFIED;
  328. }
  329. #undef FUNC_NAME
  330. SCM
  331. scm_from_locale_symbol (const char *sym)
  332. {
  333. return scm_i_c_mem2symbol (sym, strlen (sym));
  334. }
  335. SCM
  336. scm_from_locale_symboln (const char *sym, size_t len)
  337. {
  338. return scm_i_c_mem2symbol (sym, len);
  339. }
  340. SCM
  341. scm_take_locale_symboln (char *sym, size_t len)
  342. {
  343. SCM res;
  344. unsigned long raw_hash;
  345. if (len == (size_t)-1)
  346. len = strlen (sym);
  347. else
  348. {
  349. /* Ensure STR is null terminated. A realloc for 1 extra byte should
  350. often be satisfied from the alignment padding after the block, with
  351. no actual data movement. */
  352. sym = scm_realloc (sym, len+1);
  353. sym[len] = '\0';
  354. }
  355. raw_hash = scm_string_hash ((unsigned char *)sym, len);
  356. res = lookup_interned_symbol (sym, len, raw_hash);
  357. if (scm_is_false (res))
  358. {
  359. res = scm_i_c_take_symbol (sym, len, 0, raw_hash,
  360. scm_cons (SCM_BOOL_F, SCM_EOL));
  361. intern_symbol (res);
  362. }
  363. else
  364. free (sym);
  365. return res;
  366. }
  367. SCM
  368. scm_take_locale_symbol (char *sym)
  369. {
  370. return scm_take_locale_symboln (sym, (size_t)-1);
  371. }
  372. void
  373. scm_symbols_prehistory ()
  374. {
  375. symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
  376. scm_permanent_object (symbols);
  377. }
  378. void
  379. scm_init_symbols ()
  380. {
  381. #include "libguile/symbols.x"
  382. }
  383. /*
  384. Local Variables:
  385. c-file-style: "gnu"
  386. End:
  387. */