Optimize 'string-hash'.
[bpt/guile.git] / libguile / srcprop.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2006,
2 * 2008, 2009, 2010, 2011, 2012 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
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <errno.h>
27
28 #include "libguile/_scm.h"
29 #include "libguile/async.h"
30 #include "libguile/smob.h"
31 #include "libguile/alist.h"
32 #include "libguile/debug.h"
33 #include "libguile/hashtab.h"
34 #include "libguile/hash.h"
35 #include "libguile/ports.h"
36 #include "libguile/root.h"
37 #include "libguile/weaks.h"
38 #include "libguile/gc.h"
39
40 #include "libguile/validate.h"
41 #include "libguile/srcprop.h"
42 #include "libguile/private-options.h"
43
44 \f
45 /* {Source Properties}
46 *
47 * Properties of source list expressions.
48 * Four of these have special meaning:
49 *
50 * filename string The name of the source file.
51 * copy list A copy of the list expression.
52 * line integer The source code line number.
53 * column integer The source code column number.
54 *
55 * Most properties above can be set by the reader.
56 *
57 */
58
59 SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
60 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
61 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
62 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
63
64 static SCM scm_source_whash;
65 static scm_i_pthread_mutex_t source_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
66
67
68 /*
69 * Source properties are stored as double cells with the
70 * following layout:
71
72 * car = tag
73 * cbr = pos
74 * ccr = copy
75 * cdr = alist
76 */
77
78 #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
79 #define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
80 #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
81 #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
82 #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
83 #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
84 #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
85 #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
86 #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
87 #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
88 #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
89 #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
90
91
92 static SCM scm_srcprops_to_alist (SCM obj);
93
94
95 scm_t_bits scm_tc16_srcprops;
96
97
98 static int
99 supports_source_props (SCM obj)
100 {
101 return SCM_NIMP (obj) && !scm_is_symbol (obj) && !scm_is_keyword (obj);
102 }
103
104
105 static int
106 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
107 {
108 int writingp = SCM_WRITINGP (pstate);
109 scm_puts ("#<srcprops ", port);
110 SCM_SET_WRITINGP (pstate, 1);
111 scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
112 SCM_SET_WRITINGP (pstate, writingp);
113 scm_putc ('>', port);
114 return 1;
115 }
116
117
118 /*
119 * We remember the last file name settings, so we can share that alist
120 * entry. This works because scm_set_source_property_x does not use
121 * assoc-set! for modifying the alist.
122 *
123 * This variable contains a protected cons, whose cdr is the cached
124 * alist
125 */
126 static SCM scm_last_alist_filename;
127
128 SCM
129 scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
130 {
131 if (!SCM_UNBNDP (filename))
132 {
133 SCM old_alist = alist;
134
135 /*
136 have to extract the acons, and operate on that, for
137 thread safety.
138 */
139 SCM last_acons = SCM_CDR (scm_last_alist_filename);
140 if (scm_is_null (old_alist)
141 && scm_is_eq (SCM_CDAR (last_acons), filename))
142 {
143 alist = last_acons;
144 }
145 else
146 {
147 alist = scm_acons (scm_sym_filename, filename, alist);
148 if (scm_is_null (old_alist))
149 SCM_SETCDR (scm_last_alist_filename, alist);
150 }
151 }
152
153 SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
154 SRCPROPMAKPOS (line, col),
155 SCM_UNPACK (copy),
156 SCM_UNPACK (alist));
157 }
158
159
160 static SCM
161 scm_srcprops_to_alist (SCM obj)
162 {
163 SCM alist = SRCPROPALIST (obj);
164 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
165 alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
166 alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
167 alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
168 return alist;
169 }
170
171 SCM_DEFINE (scm_supports_source_properties_p, "supports-source-properties?", 1, 0, 0,
172 (SCM obj),
173 "Return #t if @var{obj} supports adding source properties,\n"
174 "otherwise return #f.")
175 #define FUNC_NAME s_scm_supports_source_properties_p
176 {
177 return scm_from_bool (supports_source_props (obj));
178 }
179 #undef FUNC_NAME
180
181 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
182 (SCM obj),
183 "Return the source property association list of @var{obj}.")
184 #define FUNC_NAME s_scm_source_properties
185 {
186 if (SCM_IMP (obj))
187 return SCM_EOL;
188 else
189 {
190 SCM p;
191
192 scm_i_pthread_mutex_lock (&source_lock);
193 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
194 scm_i_pthread_mutex_unlock (&source_lock);
195
196 if (SRCPROPSP (p))
197 return scm_srcprops_to_alist (p);
198 else
199 /* list from set-source-properties!, or SCM_EOL for not found */
200 return p;
201 }
202 }
203 #undef FUNC_NAME
204
205 /* Perhaps this procedure should look through an alist
206 and try to make a srcprops-object...? */
207 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
208 (SCM obj, SCM alist),
209 "Install the association list @var{alist} as the source property\n"
210 "list for @var{obj}.")
211 #define FUNC_NAME s_scm_set_source_properties_x
212 {
213 SCM_VALIDATE_NIM (1, obj);
214
215 scm_i_pthread_mutex_lock (&source_lock);
216 scm_hashq_set_x (scm_source_whash, obj, alist);
217 scm_i_pthread_mutex_unlock (&source_lock);
218
219 return alist;
220 }
221 #undef FUNC_NAME
222
223 int
224 scm_i_has_source_properties (SCM obj)
225 #define FUNC_NAME "%set-source-properties"
226 {
227 if (SCM_IMP (obj))
228 return 0;
229 else
230 {
231 int ret;
232
233 scm_i_pthread_mutex_lock (&source_lock);
234 ret = scm_is_true (scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F));
235 scm_i_pthread_mutex_unlock (&source_lock);
236
237 return ret;
238 }
239 }
240 #undef FUNC_NAME
241
242
243 void
244 scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
245 #define FUNC_NAME "%set-source-properties"
246 {
247 SCM_VALIDATE_NIM (1, obj);
248
249 scm_i_pthread_mutex_lock (&source_lock);
250 scm_hashq_set_x (scm_source_whash, obj,
251 scm_make_srcprops (line, col, fname,
252 SCM_COPY_SOURCE_P
253 ? scm_copy_tree (obj)
254 : SCM_UNDEFINED,
255 SCM_EOL));
256 scm_i_pthread_mutex_unlock (&source_lock);
257 }
258 #undef FUNC_NAME
259
260 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
261 (SCM obj, SCM key),
262 "Return the source property specified by @var{key} from\n"
263 "@var{obj}'s source property list.")
264 #define FUNC_NAME s_scm_source_property
265 {
266 if (SCM_IMP (obj))
267 return SCM_BOOL_F;
268 else
269 {
270 SCM p;
271
272 scm_i_pthread_mutex_lock (&source_lock);
273 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
274 scm_i_pthread_mutex_unlock (&source_lock);
275
276 if (!SRCPROPSP (p))
277 goto alist;
278 if (scm_is_eq (scm_sym_line, key))
279 p = scm_from_int (SRCPROPLINE (p));
280 else if (scm_is_eq (scm_sym_column, key))
281 p = scm_from_int (SRCPROPCOL (p));
282 else if (scm_is_eq (scm_sym_copy, key))
283 p = SRCPROPCOPY (p);
284 else
285 {
286 p = SRCPROPALIST (p);
287 alist:
288 p = scm_assoc (key, p);
289 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
290 }
291 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
292 }
293 }
294 #undef FUNC_NAME
295
296 SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
297 (SCM obj, SCM key, SCM datum),
298 "Set the source property of object @var{obj}, which is specified by\n"
299 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
300 #define FUNC_NAME s_scm_set_source_property_x
301 {
302 SCM p;
303 SCM_VALIDATE_NIM (1, obj);
304
305 scm_i_pthread_mutex_lock (&source_lock);
306 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
307
308 if (scm_is_eq (scm_sym_line, key))
309 {
310 if (SRCPROPSP (p))
311 SETSRCPROPLINE (p, scm_to_int (datum));
312 else
313 scm_hashq_set_x (scm_source_whash, obj,
314 scm_make_srcprops (scm_to_int (datum), 0,
315 SCM_UNDEFINED, SCM_UNDEFINED, p));
316 }
317 else if (scm_is_eq (scm_sym_column, key))
318 {
319 if (SRCPROPSP (p))
320 SETSRCPROPCOL (p, scm_to_int (datum));
321 else
322 scm_hashq_set_x (scm_source_whash, obj,
323 scm_make_srcprops (0, scm_to_int (datum),
324 SCM_UNDEFINED, SCM_UNDEFINED, p));
325 }
326 else if (scm_is_eq (scm_sym_copy, key))
327 {
328 if (SRCPROPSP (p))
329 SETSRCPROPCOPY (p, datum);
330 else
331 scm_hashq_set_x (scm_source_whash, obj,
332 scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
333 }
334 else
335 {
336 if (SRCPROPSP (p))
337 SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
338 else
339 scm_hashq_set_x (scm_source_whash, obj,
340 scm_acons (key, datum, p));
341 }
342 scm_i_pthread_mutex_unlock (&source_lock);
343
344 return SCM_UNSPECIFIED;
345 }
346 #undef FUNC_NAME
347
348
349 SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
350 (SCM xorig, SCM x, SCM y),
351 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
352 "Any source properties associated with @var{xorig} are also associated\n"
353 "with the new pair.")
354 #define FUNC_NAME s_scm_cons_source
355 {
356 SCM p, z;
357 z = scm_cons (x, y);
358 scm_i_pthread_mutex_lock (&source_lock);
359 /* Copy source properties possibly associated with xorig. */
360 p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F);
361 if (scm_is_true (p))
362 scm_hashq_set_x (scm_source_whash, z, p);
363 scm_i_pthread_mutex_unlock (&source_lock);
364 return z;
365 }
366 #undef FUNC_NAME
367
368
369 void
370 scm_init_srcprop ()
371 {
372 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
373 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
374
375 scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
376 scm_c_define ("source-whash", scm_source_whash);
377
378 scm_last_alist_filename = scm_cons (SCM_EOL,
379 scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
380
381 #include "libguile/srcprop.x"
382 }
383
384
385 /*
386 Local Variables:
387 c-file-style: "gnu"
388 End:
389 */