* Introduce SCM_UNUSED and mark unused function parameters.
[bpt/guile.git] / libguile / symbols.c
CommitLineData
e81d98ec 1/* Copyright (C) 1995,1996,1997,1998,2000,2001 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
0f2d19dd
JB
45\f
46
a0599745
MD
47#include "libguile/_scm.h"
48#include "libguile/chars.h"
49#include "libguile/eval.h"
ba393257 50#include "libguile/hash.h"
fb43bf74 51#include "libguile/smob.h"
a0599745
MD
52#include "libguile/variable.h"
53#include "libguile/alist.h"
7e73eaee 54#include "libguile/fluids.h"
a0599745
MD
55#include "libguile/strings.h"
56#include "libguile/vectors.h"
00ffa0e7 57#include "libguile/hashtab.h"
a0599745 58#include "libguile/weaks.h"
eb8db440 59#include "libguile/modules.h"
a0599745
MD
60
61#include "libguile/validate.h"
62#include "libguile/symbols.h"
0f2d19dd 63
95b88819
GH
64#ifdef HAVE_STRING_H
65#include <string.h>
66#endif
67
0f2d19dd
JB
68\f
69
0f979f3f
DH
70static SCM symbols;
71
a4c91488
MD
72#ifdef GUILE_DEBUG
73SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
74 (),
75 "Return the system symbol obarray.")
76#define FUNC_NAME s_scm_sys_symbols
77{
78 return symbols;
79}
80#undef FUNC_NAME
81#endif
82
0f979f3f
DH
83\f
84
0f2d19dd
JB
85/* {Symbols}
86 */
87
1cc91f1b 88
b52e071b 89SCM
1be6b49c 90scm_mem2symbol (const char *name, size_t len)
b52e071b 91{
1be6b49c
ML
92 size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
93 size_t hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
b52e071b
DH
94
95 {
0f979f3f 96 /* Try to find the symbol in the symbols table */
b52e071b
DH
97
98 SCM l;
99
0f979f3f 100 for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
b52e071b 101 {
a7a59ea9 102 SCM sym = SCM_CAAR (l);
25c507d9
MV
103 if (SCM_SYMBOL_HASH (sym) == raw_hash
104 && SCM_SYMBOL_LENGTH (sym) == len)
b52e071b
DH
105 {
106 char *chrs = SCM_SYMBOL_CHARS (sym);
1be6b49c 107 size_t i = len;
b52e071b
DH
108
109 while (i != 0)
110 {
111 --i;
112 if (name[i] != chrs[i])
113 goto next_symbol;
114 }
115
116 return sym;
117 }
118 next_symbol:
8d5a2737 119 ;
b52e071b
DH
120 }
121 }
122
123 {
124 /* The symbol was not found - create it. */
125
126 SCM symbol;
a7a59ea9 127 SCM cell;
b52e071b
DH
128 SCM slot;
129
130 SCM_NEWCELL2 (symbol);
25c507d9 131 SCM_SET_SYMBOL_CHARS (symbol, scm_must_strndup (name, len));
b52e071b
DH
132 SCM_SET_SYMBOL_HASH (symbol, raw_hash);
133 SCM_SET_PROP_SLOTS (symbol, scm_cons (SCM_BOOL_F, SCM_EOL));
134 SCM_SET_SYMBOL_LENGTH (symbol, (long) len);
135
0f979f3f 136 slot = SCM_VELTS (symbols) [hash];
a7a59ea9
MV
137 cell = scm_cons (symbol, SCM_UNDEFINED);
138 SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
b52e071b
DH
139
140 return symbol;
141 }
142}
143
144
145SCM
146scm_str2symbol (const char *str)
147{
148 return scm_mem2symbol (str, strlen (str));
149}
150
3b3b36dd 151SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
8e93e199 152 (SCM obj),
1e6808ea
MG
153 "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
154 "@code{#f}.")
1bbd0b84 155#define FUNC_NAME s_scm_symbol_p
0f2d19dd 156{
8e93e199 157 return SCM_BOOL (SCM_SYMBOLP (obj));
0f2d19dd 158}
1bbd0b84 159#undef FUNC_NAME
0f2d19dd 160
3b3b36dd 161SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
1bbd0b84 162 (SCM s),
1e6808ea
MG
163 "Return the name of @var{symbol} as a string. If the symbol was\n"
164 "part of an object returned as the value of a literal expression\n"
7a095584 165 "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
1e6808ea
MG
166 "Report on Scheme}) or by a call to the @code{read} procedure,\n"
167 "and its name contains alphabetic characters, then the string\n"
168 "returned will contain characters in the implementation's\n"
169 "preferred standard case---some implementations will prefer\n"
170 "upper case, others lower case. If the symbol was returned by\n"
171 "@code{string->symbol}, the case of characters in the string\n"
172 "returned will be the same as the case in the string that was\n"
173 "passed to @code{string->symbol}. It is an error to apply\n"
174 "mutation procedures like @code{string-set!} to strings returned\n"
175 "by this procedure.\n"
176 "\n"
942e5b91 177 "The following examples assume that the implementation's\n"
1e6808ea
MG
178 "standard case is lower case:\n"
179 "\n"
942e5b91 180 "@lisp\n"
1e6808ea
MG
181 "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
182 "(symbol->string 'Martin) @result{} \"martin\"\n"
5ffe9968 183 "(symbol->string\n"
942e5b91
MG
184 " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
185 "@end lisp")
1bbd0b84 186#define FUNC_NAME s_scm_symbol_to_string
0f2d19dd 187{
28b06554 188 SCM_VALIDATE_SYMBOL (1, s);
9fd38a3d 189 return scm_makfromstr (SCM_SYMBOL_CHARS (s), SCM_SYMBOL_LENGTH (s), 0);
0f2d19dd 190}
1bbd0b84 191#undef FUNC_NAME
0f2d19dd
JB
192
193
3b3b36dd 194SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
1e6808ea
MG
195 (SCM string),
196 "Return the symbol whose name is @var{string}. This procedure\n"
942e5b91
MG
197 "can create symbols with names containing special characters or\n"
198 "letters in the non-standard case, but it is usually a bad idea\n"
1e6808ea
MG
199 "to create such symbols because in some implementations of\n"
200 "Scheme they cannot be read as themselves. See\n"
201 "@code{symbol->string}.\n"
202 "\n"
942e5b91 203 "The following examples assume that the implementation's\n"
1e6808ea
MG
204 "standard case is lower case:\n"
205 "\n"
942e5b91
MG
206 "@lisp\n"
207 "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
208 "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
209 "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
210 "(eq? 'JollyWog\n"
211 " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
212 "(string=? \"K. Harper, M.D.\"\n"
213 " (symbol->string\n"
214 " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
215 "@end lisp")
1bbd0b84 216#define FUNC_NAME s_scm_string_to_symbol
0f2d19dd 217{
1e6808ea
MG
218 SCM_VALIDATE_STRING (1, string);
219 return scm_mem2symbol (SCM_STRING_CHARS (string),
220 SCM_STRING_LENGTH (string));
0f2d19dd 221}
1bbd0b84 222#undef FUNC_NAME
0f2d19dd 223
86d31dfe 224#define MAX_PREFIX_LENGTH 30
0f2d19dd 225
86d31dfe 226static int gensym_counter;
0f2d19dd 227
86d31dfe
MV
228SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
229 (SCM prefix),
230 "Create a new symbol with a name constructed from a prefix and\n"
231 "a counter value. The string @var{prefix} can be specified as\n"
232 "an optional argument. Default prefix is @code{g}. The counter\n"
233 "is increased by 1 at each call. There is no provision for\n"
234 "resetting the counter.")
235#define FUNC_NAME s_scm_gensym
0f2d19dd 236{
86d31dfe
MV
237 char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
238 char *name = buf;
1be6b49c 239 size_t len;
86d31dfe
MV
240 if (SCM_UNBNDP (prefix))
241 {
242 name[0] = 'g';
243 len = 1;
244 }
245 else
246 {
247 SCM_VALIDATE_STRING (1, prefix);
248 len = SCM_STRING_LENGTH (prefix);
249 if (len > MAX_PREFIX_LENGTH)
8d09eb04
MG
250 name = SCM_MUST_MALLOC (len + SCM_INTBUFLEN);
251 memcpy (name, SCM_STRING_CHARS (prefix), len);
86d31dfe 252 }
49bc24fe 253 {
86d31dfe
MV
254 int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
255 SCM res = scm_mem2symbol (name, len + n_digits);
256 if (name != buf)
257 scm_must_free (name);
258 return res;
49bc24fe 259 }
0f2d19dd 260}
1bbd0b84 261#undef FUNC_NAME
0f2d19dd 262
86d31dfe
MV
263SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
264 (SCM symbol),
265 "Return a hash value for @var{symbol}.")
266#define FUNC_NAME s_scm_symbol_hash
0f2d19dd 267{
86d31dfe
MV
268 SCM_VALIDATE_SYMBOL (1, symbol);
269 return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol));
0f2d19dd 270}
1bbd0b84 271#undef FUNC_NAME
0f2d19dd 272
3b3b36dd 273SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
1bbd0b84 274 (SCM s),
b380b885 275 "Return the contents of @var{symbol}'s @dfn{function slot}.")
1bbd0b84 276#define FUNC_NAME s_scm_symbol_fref
0f2d19dd 277{
3b3b36dd 278 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd
JB
279 return SCM_SYMBOL_FUNC (s);
280}
1bbd0b84 281#undef FUNC_NAME
0f2d19dd
JB
282
283
3b3b36dd 284SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
1bbd0b84 285 (SCM s),
b380b885 286 "Return the @dfn{property list} currently associated with @var{symbol}.")
1bbd0b84 287#define FUNC_NAME s_scm_symbol_pref
0f2d19dd 288{
3b3b36dd 289 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd
JB
290 return SCM_SYMBOL_PROPS (s);
291}
1bbd0b84 292#undef FUNC_NAME
0f2d19dd
JB
293
294
3b3b36dd 295SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
1bbd0b84 296 (SCM s, SCM val),
b380b885 297 "Change the binding of @var{symbol}'s function slot.")
1bbd0b84 298#define FUNC_NAME s_scm_symbol_fset_x
0f2d19dd 299{
3b3b36dd 300 SCM_VALIDATE_SYMBOL (1,s);
cf551a2b 301 SCM_SET_SYMBOL_FUNC (s, val);
0f2d19dd
JB
302 return SCM_UNSPECIFIED;
303}
1bbd0b84 304#undef FUNC_NAME
0f2d19dd
JB
305
306
3b3b36dd 307SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
1bbd0b84 308 (SCM s, SCM val),
b380b885 309 "Change the binding of @var{symbol}'s property slot.")
1bbd0b84 310#define FUNC_NAME s_scm_symbol_pset_x
0f2d19dd 311{
3b3b36dd 312 SCM_VALIDATE_SYMBOL (1,s);
0f2d19dd 313 SCM_DEFER_INTS;
cf551a2b 314 SCM_SET_SYMBOL_PROPS (s, val);
0f2d19dd
JB
315 SCM_ALLOW_INTS;
316 return SCM_UNSPECIFIED;
317}
1bbd0b84 318#undef FUNC_NAME
0f2d19dd 319
0f979f3f
DH
320void
321scm_symbols_prehistory ()
322{
a4c91488 323 symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (1009));
0f979f3f
DH
324 scm_permanent_object (symbols);
325}
326
327
0f2d19dd
JB
328void
329scm_init_symbols ()
0f2d19dd 330{
1ff4df7a 331 gensym_counter = 0;
8dc9439f 332#ifndef SCM_MAGIC_SNARFER
a0599745 333#include "libguile/symbols.x"
8dc9439f 334#endif
86d31dfe
MV
335#if SCM_ENABLE_VCELLS
336 scm_init_symbols_deprecated ();
337#endif
0f2d19dd 338}
89e00824
ML
339
340/*
341 Local Variables:
342 c-file-style: "gnu"
343 End:
344*/