symbols.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2003, 2004,
  2. * 2006, 2009, 2011 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/chars.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/hash.h"
  26. #include "libguile/smob.h"
  27. #include "libguile/variable.h"
  28. #include "libguile/alist.h"
  29. #include "libguile/fluids.h"
  30. #include "libguile/strings.h"
  31. #include "libguile/vectors.h"
  32. #include "libguile/hashtab.h"
  33. #include "libguile/weaks.h"
  34. #include "libguile/modules.h"
  35. #include "libguile/read.h"
  36. #include "libguile/srfi-13.h"
  37. #include "libguile/validate.h"
  38. #include "libguile/symbols.h"
  39. #include "libguile/private-options.h"
  40. #ifdef HAVE_STRING_H
  41. #include <string.h>
  42. #endif
  43. static SCM symbols;
  44. #ifdef GUILE_DEBUG
  45. SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
  46. (),
  47. "Return the system symbol obarray.")
  48. #define FUNC_NAME s_scm_sys_symbols
  49. {
  50. return symbols;
  51. }
  52. #undef FUNC_NAME
  53. #endif
  54. /* {Symbols}
  55. */
  56. unsigned long
  57. scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
  58. {
  59. return scm_i_symbol_hash (obj) % n;
  60. }
  61. struct string_lookup_data
  62. {
  63. SCM string;
  64. unsigned long string_hash;
  65. };
  66. static int
  67. string_lookup_predicate_fn (SCM sym, void *closure)
  68. {
  69. struct string_lookup_data *data = closure;
  70. if (scm_i_symbol_hash (sym) == data->string_hash
  71. && scm_i_symbol_length (sym) == scm_i_string_length (data->string))
  72. {
  73. size_t n = scm_i_symbol_length (sym);
  74. while (n--)
  75. if (scm_i_symbol_ref (sym, n) != scm_i_string_ref (data->string, n))
  76. return 0;
  77. return 1;
  78. }
  79. else
  80. return 0;
  81. }
  82. static SCM
  83. lookup_interned_symbol (SCM name, unsigned long raw_hash)
  84. {
  85. struct string_lookup_data data;
  86. SCM handle;
  87. data.string = name;
  88. data.string_hash = raw_hash;
  89. /* Strictly speaking, we should take a lock here. But instead we rely
  90. on the fact that if this fails, we do take the lock on the
  91. intern_symbol path; and since nothing deletes from the hash table
  92. except GC, we should be OK. */
  93. handle = scm_hash_fn_get_handle_by_hash (symbols, raw_hash,
  94. string_lookup_predicate_fn,
  95. &data);
  96. if (scm_is_true (handle))
  97. return SCM_CAR (handle);
  98. else
  99. return SCM_BOOL_F;
  100. }
  101. struct latin1_lookup_data
  102. {
  103. const char *str;
  104. size_t len;
  105. unsigned long string_hash;
  106. };
  107. static int
  108. latin1_lookup_predicate_fn (SCM sym, void *closure)
  109. {
  110. struct latin1_lookup_data *data = closure;
  111. return scm_i_symbol_hash (sym) == data->string_hash
  112. && scm_i_is_narrow_symbol (sym)
  113. && scm_i_symbol_length (sym) == data->len
  114. && strncmp (scm_i_symbol_chars (sym), data->str, data->len) == 0;
  115. }
  116. static SCM
  117. lookup_interned_latin1_symbol (const char *str, size_t len,
  118. unsigned long raw_hash)
  119. {
  120. struct latin1_lookup_data data;
  121. SCM handle;
  122. data.str = str;
  123. data.len = len;
  124. data.string_hash = raw_hash;
  125. /* Strictly speaking, we should take a lock here. But instead we rely
  126. on the fact that if this fails, we do take the lock on the
  127. intern_symbol path; and since nothing deletes from the hash table
  128. except GC, we should be OK. */
  129. handle = scm_hash_fn_get_handle_by_hash (symbols, raw_hash,
  130. latin1_lookup_predicate_fn,
  131. &data);
  132. if (scm_is_true (handle))
  133. return SCM_CAR (handle);
  134. else
  135. return SCM_BOOL_F;
  136. }
  137. static unsigned long
  138. symbol_lookup_hash_fn (SCM obj, unsigned long max, void *closure)
  139. {
  140. return scm_i_symbol_hash (obj) % max;
  141. }
  142. static SCM
  143. symbol_lookup_assoc_fn (SCM obj, SCM alist, void *closure)
  144. {
  145. for (; !scm_is_null (alist); alist = SCM_CDR (alist))
  146. {
  147. SCM sym = SCM_CAAR (alist);
  148. if (scm_i_symbol_hash (sym) == scm_i_symbol_hash (obj)
  149. && scm_is_true (scm_string_equal_p (scm_symbol_to_string (sym),
  150. scm_symbol_to_string (obj))))
  151. return SCM_CAR (alist);
  152. }
  153. return SCM_BOOL_F;
  154. }
  155. static scm_i_pthread_mutex_t intern_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  156. /* Intern SYMBOL, an uninterned symbol. Might return a different
  157. symbol, if another one was interned at the same time. */
  158. static SCM
  159. intern_symbol (SCM symbol)
  160. {
  161. SCM handle;
  162. scm_i_pthread_mutex_lock (&intern_lock);
  163. handle = scm_hash_fn_create_handle_x (symbols, symbol, SCM_UNDEFINED,
  164. symbol_lookup_hash_fn,
  165. symbol_lookup_assoc_fn,
  166. NULL);
  167. scm_i_pthread_mutex_unlock (&intern_lock);
  168. return SCM_CAR (handle);
  169. }
  170. static SCM
  171. scm_i_str2symbol (SCM str)
  172. {
  173. SCM symbol;
  174. size_t raw_hash = scm_i_string_hash (str);
  175. symbol = lookup_interned_symbol (str, raw_hash);
  176. if (scm_is_true (symbol))
  177. return symbol;
  178. else
  179. {
  180. /* The symbol was not found, create it. */
  181. symbol = scm_i_make_symbol (str, 0, raw_hash,
  182. scm_cons (SCM_BOOL_F, SCM_EOL));
  183. return intern_symbol (symbol);
  184. }
  185. }
  186. static SCM
  187. scm_i_str2uninterned_symbol (SCM str)
  188. {
  189. size_t raw_hash = scm_i_string_hash (str);
  190. return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
  191. raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
  192. }
  193. SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
  194. (SCM obj),
  195. "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
  196. "@code{#f}.")
  197. #define FUNC_NAME s_scm_symbol_p
  198. {
  199. return scm_from_bool (scm_is_symbol (obj));
  200. }
  201. #undef FUNC_NAME
  202. SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
  203. (SCM symbol),
  204. "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
  205. "@code{#f}.")
  206. #define FUNC_NAME s_scm_symbol_interned_p
  207. {
  208. SCM_VALIDATE_SYMBOL (1, symbol);
  209. return scm_from_bool (scm_i_symbol_is_interned (symbol));
  210. }
  211. #undef FUNC_NAME
  212. SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
  213. (SCM name),
  214. "Return a new uninterned symbol with the name @var{name}. "
  215. "The returned symbol is guaranteed to be unique and future "
  216. "calls to @code{string->symbol} will not return it.")
  217. #define FUNC_NAME s_scm_make_symbol
  218. {
  219. SCM_VALIDATE_STRING (1, name);
  220. return scm_i_str2uninterned_symbol (name);
  221. }
  222. #undef FUNC_NAME
  223. SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
  224. (SCM s),
  225. "Return the name of @var{symbol} as a string. If the symbol was\n"
  226. "part of an object returned as the value of a literal expression\n"
  227. "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
  228. "Report on Scheme}) or by a call to the @code{read} procedure,\n"
  229. "and its name contains alphabetic characters, then the string\n"
  230. "returned will contain characters in the implementation's\n"
  231. "preferred standard case---some implementations will prefer\n"
  232. "upper case, others lower case. If the symbol was returned by\n"
  233. "@code{string->symbol}, the case of characters in the string\n"
  234. "returned will be the same as the case in the string that was\n"
  235. "passed to @code{string->symbol}. It is an error to apply\n"
  236. "mutation procedures like @code{string-set!} to strings returned\n"
  237. "by this procedure.\n"
  238. "\n"
  239. "The following examples assume that the implementation's\n"
  240. "standard case is lower case:\n"
  241. "\n"
  242. "@lisp\n"
  243. "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
  244. "(symbol->string 'Martin) @result{} \"martin\"\n"
  245. "(symbol->string\n"
  246. " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
  247. "@end lisp")
  248. #define FUNC_NAME s_scm_symbol_to_string
  249. {
  250. SCM_VALIDATE_SYMBOL (1, s);
  251. return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
  252. }
  253. #undef FUNC_NAME
  254. SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
  255. (SCM string),
  256. "Return the symbol whose name is @var{string}. This procedure\n"
  257. "can create symbols with names containing special characters or\n"
  258. "letters in the non-standard case, but it is usually a bad idea\n"
  259. "to create such symbols because in some implementations of\n"
  260. "Scheme they cannot be read as themselves. See\n"
  261. "@code{symbol->string}.\n"
  262. "\n"
  263. "The following examples assume that the implementation's\n"
  264. "standard case is lower case:\n"
  265. "\n"
  266. "@lisp\n"
  267. "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
  268. "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
  269. "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
  270. "(eq? 'JollyWog\n"
  271. " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
  272. "(string=? \"K. Harper, M.D.\"\n"
  273. " (symbol->string\n"
  274. " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
  275. "@end lisp")
  276. #define FUNC_NAME s_scm_string_to_symbol
  277. {
  278. SCM_VALIDATE_STRING (1, string);
  279. return scm_i_str2symbol (string);
  280. }
  281. #undef FUNC_NAME
  282. SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
  283. (SCM str),
  284. "Return the symbol whose name is @var{str}. @var{str} is\n"
  285. "converted to lowercase before the conversion is done, if Guile\n"
  286. "is currently reading symbols case-insensitively.")
  287. #define FUNC_NAME s_scm_string_ci_to_symbol
  288. {
  289. return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
  290. ? scm_string_downcase(str)
  291. : str);
  292. }
  293. #undef FUNC_NAME
  294. /* The default prefix for `gensym'd symbols. */
  295. static SCM default_gensym_prefix;
  296. #define MAX_PREFIX_LENGTH 30
  297. SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
  298. (SCM prefix),
  299. "Create a new symbol with a name constructed from a prefix and\n"
  300. "a counter value. The string @var{prefix} can be specified as\n"
  301. "an optional argument. Default prefix is @code{ g}. The counter\n"
  302. "is increased by 1 at each call. There is no provision for\n"
  303. "resetting the counter.")
  304. #define FUNC_NAME s_scm_gensym
  305. {
  306. static int gensym_counter = 0;
  307. SCM suffix, name;
  308. int n, n_digits;
  309. char buf[SCM_INTBUFLEN];
  310. if (SCM_UNBNDP (prefix))
  311. prefix = default_gensym_prefix;
  312. /* mutex in case another thread looks and incs at the exact same moment */
  313. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  314. n = gensym_counter++;
  315. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  316. n_digits = scm_iint2str (n, 10, buf);
  317. suffix = scm_from_latin1_stringn (buf, n_digits);
  318. name = scm_string_append (scm_list_2 (prefix, suffix));
  319. return scm_string_to_symbol (name);
  320. }
  321. #undef FUNC_NAME
  322. SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
  323. (SCM symbol),
  324. "Return a hash value for @var{symbol}.")
  325. #define FUNC_NAME s_scm_symbol_hash
  326. {
  327. SCM_VALIDATE_SYMBOL (1, symbol);
  328. return scm_from_ulong (scm_i_symbol_hash (symbol));
  329. }
  330. #undef FUNC_NAME
  331. SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
  332. (SCM s),
  333. "Return the contents of @var{symbol}'s @dfn{function slot}.")
  334. #define FUNC_NAME s_scm_symbol_fref
  335. {
  336. SCM_VALIDATE_SYMBOL (1, s);
  337. return SCM_CAR (SCM_CELL_OBJECT_3 (s));
  338. }
  339. #undef FUNC_NAME
  340. SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
  341. (SCM s),
  342. "Return the @dfn{property list} currently associated with @var{symbol}.")
  343. #define FUNC_NAME s_scm_symbol_pref
  344. {
  345. SCM_VALIDATE_SYMBOL (1, s);
  346. return SCM_CDR (SCM_CELL_OBJECT_3 (s));
  347. }
  348. #undef FUNC_NAME
  349. SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
  350. (SCM s, SCM val),
  351. "Change the binding of @var{symbol}'s function slot.")
  352. #define FUNC_NAME s_scm_symbol_fset_x
  353. {
  354. SCM_VALIDATE_SYMBOL (1, s);
  355. SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
  356. return SCM_UNSPECIFIED;
  357. }
  358. #undef FUNC_NAME
  359. SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
  360. (SCM s, SCM val),
  361. "Change the binding of @var{symbol}'s property slot.")
  362. #define FUNC_NAME s_scm_symbol_pset_x
  363. {
  364. SCM_VALIDATE_SYMBOL (1, s);
  365. SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
  366. return SCM_UNSPECIFIED;
  367. }
  368. #undef FUNC_NAME
  369. SCM
  370. scm_from_locale_symbol (const char *sym)
  371. {
  372. return scm_from_locale_symboln (sym, -1);
  373. }
  374. SCM
  375. scm_from_locale_symboln (const char *sym, size_t len)
  376. {
  377. SCM str = scm_from_locale_stringn (sym, len);
  378. return scm_i_str2symbol (str);
  379. }
  380. SCM
  381. scm_take_locale_symboln (char *sym, size_t len)
  382. {
  383. SCM str;
  384. str = scm_take_locale_stringn (sym, len);
  385. return scm_i_str2symbol (str);
  386. }
  387. SCM
  388. scm_take_locale_symbol (char *sym)
  389. {
  390. return scm_take_locale_symboln (sym, (size_t)-1);
  391. }
  392. SCM
  393. scm_from_latin1_symbol (const char *sym)
  394. {
  395. return scm_from_latin1_symboln (sym, -1);
  396. }
  397. SCM
  398. scm_from_latin1_symboln (const char *sym, size_t len)
  399. {
  400. unsigned long hash;
  401. SCM ret;
  402. if (len == (size_t) -1)
  403. len = strlen (sym);
  404. hash = scm_i_latin1_string_hash (sym, len);
  405. ret = lookup_interned_latin1_symbol (sym, len, hash);
  406. if (scm_is_false (ret))
  407. {
  408. SCM str = scm_from_latin1_stringn (sym, len);
  409. ret = scm_i_str2symbol (str);
  410. }
  411. return ret;
  412. }
  413. SCM
  414. scm_from_utf8_symbol (const char *sym)
  415. {
  416. return scm_from_utf8_symboln (sym, -1);
  417. }
  418. SCM
  419. scm_from_utf8_symboln (const char *sym, size_t len)
  420. {
  421. SCM str = scm_from_utf8_stringn (sym, len);
  422. return scm_i_str2symbol (str);
  423. }
  424. void
  425. scm_symbols_prehistory ()
  426. {
  427. symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
  428. }
  429. void
  430. scm_init_symbols ()
  431. {
  432. #include "libguile/symbols.x"
  433. default_gensym_prefix = scm_from_latin1_string (" g");
  434. }
  435. /*
  436. Local Variables:
  437. c-file-style: "gnu"
  438. End:
  439. */