reimplement hashtab.c's weak hash tables in terms of weak-table.c
[bpt/guile.git] / libguile / srcprop.c
CommitLineData
d1c4720c 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009, 2010, 2011 Free Software Foundation
575888bd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
575888bd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
575888bd 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
575888bd 20\f
dbb605f5
LC
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
575888bd 24
e6e2e95a
MD
25#include <errno.h>
26
a0599745 27#include "libguile/_scm.h"
4e047c3e 28#include "libguile/async.h"
a0599745
MD
29#include "libguile/smob.h"
30#include "libguile/alist.h"
31#include "libguile/debug.h"
32#include "libguile/hashtab.h"
33#include "libguile/hash.h"
34#include "libguile/ports.h"
35#include "libguile/root.h"
36#include "libguile/weaks.h"
42e6668b 37#include "libguile/gc.h"
575888bd 38
a0599745
MD
39#include "libguile/validate.h"
40#include "libguile/srcprop.h"
26c8cc14
AW
41#include "libguile/private-options.h"
42
575888bd
MD
43\f
44/* {Source Properties}
45 *
46 * Properties of source list expressions.
8cbcaaa0 47 * Four of these have special meaning:
575888bd
MD
48 *
49 * filename string The name of the source file.
50 * copy list A copy of the list expression.
51 * line integer The source code line number.
52 * column integer The source code column number.
575888bd
MD
53 *
54 * Most properties above can be set by the reader.
55 *
56 */
57
85db4a2c
DH
58SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
59SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
60SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
61SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
575888bd 62
2f045fc1
AW
63static SCM scm_source_whash;
64static scm_i_pthread_mutex_t source_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER;
575888bd 65
1cc91f1b 66
b0763985 67/*
d00a0704
HWN
68 * Source properties are stored as double cells with the
69 * following layout:
b0763985 70
d00a0704
HWN
71 * car = tag
72 * cbr = pos
73 * ccr = copy
d5ed380e 74 * cdr = alist
d00a0704 75 */
b0763985
HWN
76
77#define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
9e9e54eb 78#define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
b0763985
HWN
79#define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
80#define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
9e9e54eb
AW
81#define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
82#define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
b0763985 83#define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
9e9e54eb 84#define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
b0763985
HWN
85#define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
86#define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
9e9e54eb
AW
87#define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
88#define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
b0763985
HWN
89
90
67a96734
NJ
91static SCM scm_srcprops_to_alist (SCM obj);
92
b0763985 93
92c2555f 94scm_t_bits scm_tc16_srcprops;
575888bd 95
575888bd 96static int
e841c3e0 97srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
575888bd 98{
19402679 99 int writingp = SCM_WRITINGP (pstate);
b7f3516f 100 scm_puts ("#<srcprops ", port);
19402679 101 SCM_SET_WRITINGP (pstate, 1);
67a96734 102 scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
19402679 103 SCM_SET_WRITINGP (pstate, writingp);
b7f3516f 104 scm_putc ('>', port);
575888bd
MD
105 return 1;
106}
107
1cc91f1b 108
b0763985 109/*
67a96734 110 * We remember the last file name settings, so we can share that alist
d00a0704 111 * entry. This works because scm_set_source_property_x does not use
67a96734 112 * assoc-set! for modifying the alist.
d00a0704
HWN
113 *
114 * This variable contains a protected cons, whose cdr is the cached
67a96734 115 * alist
b0763985 116 */
67a96734 117static SCM scm_last_alist_filename;
b0763985 118
575888bd 119SCM
67a96734 120scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
575888bd 121{
b0763985 122 if (!SCM_UNBNDP (filename))
575888bd 123 {
67a96734 124 SCM old_alist = alist;
42e6668b 125
b0763985
HWN
126 /*
127 have to extract the acons, and operate on that, for
128 thread safety.
129 */
67a96734 130 SCM last_acons = SCM_CDR (scm_last_alist_filename);
393baa8a
AW
131 if (scm_is_null (old_alist)
132 && scm_is_eq (SCM_CDAR (last_acons), filename))
b0763985 133 {
67a96734 134 alist = last_acons;
b0763985
HWN
135 }
136 else
137 {
67a96734 138 alist = scm_acons (scm_sym_filename, filename, alist);
393baa8a 139 if (scm_is_null (old_alist))
67a96734 140 SCM_SETCDR (scm_last_alist_filename, alist);
b0763985 141 }
575888bd 142 }
b0763985
HWN
143
144 SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
145 SRCPROPMAKPOS (line, col),
b2b33168
AW
146 SCM_UNPACK (copy),
147 SCM_UNPACK (alist));
575888bd
MD
148}
149
1cc91f1b 150
67a96734
NJ
151static SCM
152scm_srcprops_to_alist (SCM obj)
575888bd 153{
67a96734 154 SCM alist = SRCPROPALIST (obj);
575888bd 155 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
67a96734
NJ
156 alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
157 alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
158 alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
67a96734 159 return alist;
575888bd
MD
160}
161
a1ec6916 162SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
1bbd0b84 163 (SCM obj),
e3239868 164 "Return the source property association list of @var{obj}.")
1bbd0b84 165#define FUNC_NAME s_scm_source_properties
575888bd
MD
166{
167 SCM p;
34d19ef6 168 SCM_VALIDATE_NIM (1, obj);
2f045fc1
AW
169
170 scm_i_pthread_mutex_lock (&source_lock);
171 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
172 scm_i_pthread_mutex_unlock (&source_lock);
173
230d095f 174 if (SRCPROPSP (p))
67a96734 175 return scm_srcprops_to_alist (p);
f5003c13
KR
176 else
177 /* list from set-source-properties!, or SCM_EOL for not found */
178 return p;
575888bd 179}
1bbd0b84 180#undef FUNC_NAME
575888bd
MD
181
182/* Perhaps this procedure should look through an alist
183 and try to make a srcprops-object...? */
a1ec6916 184SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
67a96734
NJ
185 (SCM obj, SCM alist),
186 "Install the association list @var{alist} as the source property\n"
e3239868 187 "list for @var{obj}.")
1bbd0b84 188#define FUNC_NAME s_scm_set_source_properties_x
575888bd 189{
34d19ef6 190 SCM_VALIDATE_NIM (1, obj);
2f045fc1
AW
191
192 scm_i_pthread_mutex_lock (&source_lock);
d1c4720c 193 scm_hashq_set_x (scm_source_whash, obj, alist);
2f045fc1
AW
194 scm_i_pthread_mutex_unlock (&source_lock);
195
67a96734 196 return alist;
575888bd 197}
1bbd0b84 198#undef FUNC_NAME
575888bd 199
26c8cc14
AW
200int
201scm_i_has_source_properties (SCM obj)
202#define FUNC_NAME "%set-source-properties"
203{
2f045fc1
AW
204 int ret;
205
26c8cc14
AW
206 SCM_VALIDATE_NIM (1, obj);
207
2f045fc1
AW
208 scm_i_pthread_mutex_lock (&source_lock);
209 ret = scm_is_true (scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F));
210 scm_i_pthread_mutex_unlock (&source_lock);
211
212 return ret;
26c8cc14
AW
213}
214#undef FUNC_NAME
215
216
217void
218scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname)
219#define FUNC_NAME "%set-source-properties"
220{
221 SCM_VALIDATE_NIM (1, obj);
222
2f045fc1 223 scm_i_pthread_mutex_lock (&source_lock);
26c8cc14
AW
224 scm_hashq_set_x (scm_source_whash, obj,
225 scm_make_srcprops (line, col, fname,
226 SCM_COPY_SOURCE_P
227 ? scm_copy_tree (obj)
228 : SCM_UNDEFINED,
229 SCM_EOL));
2f045fc1 230 scm_i_pthread_mutex_unlock (&source_lock);
26c8cc14
AW
231}
232#undef FUNC_NAME
233
a1ec6916 234SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
1bbd0b84 235 (SCM obj, SCM key),
e3239868
DH
236 "Return the source property specified by @var{key} from\n"
237 "@var{obj}'s source property list.")
1bbd0b84 238#define FUNC_NAME s_scm_source_property
575888bd
MD
239{
240 SCM p;
34d19ef6 241 SCM_VALIDATE_NIM (1, obj);
2f045fc1
AW
242
243 scm_i_pthread_mutex_lock (&source_lock);
575888bd 244 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
2f045fc1
AW
245 scm_i_pthread_mutex_unlock (&source_lock);
246
24933780 247 if (!SRCPROPSP (p))
67a96734 248 goto alist;
8cbcaaa0
AW
249 if (scm_is_eq (scm_sym_line, key))
250 p = scm_from_int (SRCPROPLINE (p));
251 else if (scm_is_eq (scm_sym_column, key))
252 p = scm_from_int (SRCPROPCOL (p));
253 else if (scm_is_eq (scm_sym_copy, key))
254 p = SRCPROPCOPY (p);
575888bd
MD
255 else
256 {
67a96734
NJ
257 p = SRCPROPALIST (p);
258 alist:
575888bd
MD
259 p = scm_assoc (key, p);
260 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
261 }
262 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
263}
1bbd0b84 264#undef FUNC_NAME
575888bd 265
a1ec6916 266SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
1bbd0b84 267 (SCM obj, SCM key, SCM datum),
e3239868
DH
268 "Set the source property of object @var{obj}, which is specified by\n"
269 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
1bbd0b84 270#define FUNC_NAME s_scm_set_source_property_x
575888bd 271{
575888bd 272 SCM p;
34d19ef6 273 SCM_VALIDATE_NIM (1, obj);
2f045fc1
AW
274
275 scm_i_pthread_mutex_lock (&source_lock);
d1c4720c 276 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
8cbcaaa0
AW
277
278 if (scm_is_eq (scm_sym_line, key))
575888bd 279 {
0c95b57d 280 if (SRCPROPSP (p))
a55c2b68 281 SETSRCPROPLINE (p, scm_to_int (datum));
575888bd 282 else
d1c4720c
AW
283 scm_hashq_set_x (scm_source_whash, obj,
284 scm_make_srcprops (scm_to_int (datum), 0,
285 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 286 }
bc36d050 287 else if (scm_is_eq (scm_sym_column, key))
575888bd 288 {
0c95b57d 289 if (SRCPROPSP (p))
a55c2b68 290 SETSRCPROPCOL (p, scm_to_int (datum));
575888bd 291 else
d1c4720c
AW
292 scm_hashq_set_x (scm_source_whash, obj,
293 scm_make_srcprops (0, scm_to_int (datum),
294 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 295 }
bc36d050 296 else if (scm_is_eq (scm_sym_copy, key))
575888bd 297 {
0c95b57d 298 if (SRCPROPSP (p))
80237dcc 299 SETSRCPROPCOPY (p, datum);
575888bd 300 else
d1c4720c
AW
301 scm_hashq_set_x (scm_source_whash, obj,
302 scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
303 }
304 else
58d233cc
NJ
305 {
306 if (SRCPROPSP (p))
67a96734 307 SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
58d233cc 308 else
d1c4720c
AW
309 scm_hashq_set_x (scm_source_whash, obj,
310 scm_acons (key, datum, p));
58d233cc 311 }
2f045fc1
AW
312 scm_i_pthread_mutex_unlock (&source_lock);
313
575888bd
MD
314 return SCM_UNSPECIFIED;
315}
1bbd0b84 316#undef FUNC_NAME
575888bd 317
1cc91f1b 318
0f458a37
AW
319SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
320 (SCM xorig, SCM x, SCM y),
321 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
322 "Any source properties associated with @var{xorig} are also associated\n"
323 "with the new pair.")
324#define FUNC_NAME s_scm_cons_source
325{
326 SCM p, z;
327 z = scm_cons (x, y);
2f045fc1 328 scm_i_pthread_mutex_lock (&source_lock);
0f458a37 329 /* Copy source properties possibly associated with xorig. */
d1c4720c 330 p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F);
0f458a37 331 if (scm_is_true (p))
d1c4720c 332 scm_hashq_set_x (scm_source_whash, z, p);
2f045fc1 333 scm_i_pthread_mutex_unlock (&source_lock);
0f458a37
AW
334 return z;
335}
336#undef FUNC_NAME
337
338
575888bd
MD
339void
340scm_init_srcprop ()
575888bd 341{
e841c3e0 342 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
e841c3e0
KN
343 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
344
e11e83f3 345 scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
86d31dfe 346 scm_c_define ("source-whash", scm_source_whash);
85db4a2c 347
f39448c5
AW
348 scm_last_alist_filename = scm_cons (SCM_EOL,
349 scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
b0763985 350
a0599745 351#include "libguile/srcprop.x"
575888bd
MD
352}
353
89e00824
ML
354
355/*
356 Local Variables:
357 c-file-style: "gnu"
358 End:
359*/