Merge commit '29776e85da637ec4d44b2b2822d6934a50c0084b' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / srcprop.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006 Free Software Foundation
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
18
19 \f
20
21 #include <errno.h>
22
23 #include "libguile/_scm.h"
24 #include "libguile/async.h"
25 #include "libguile/smob.h"
26 #include "libguile/alist.h"
27 #include "libguile/debug.h"
28 #include "libguile/hashtab.h"
29 #include "libguile/hash.h"
30 #include "libguile/ports.h"
31 #include "libguile/root.h"
32 #include "libguile/weaks.h"
33 #include "libguile/gc.h"
34
35 #include "libguile/validate.h"
36 #include "libguile/srcprop.h"
37 \f
38 /* {Source Properties}
39 *
40 * Properties of source list expressions.
41 * Five of these have special meaning:
42 *
43 * filename string The name of the source file.
44 * copy list A copy of the list expression.
45 * line integer The source code line number.
46 * column integer The source code column number.
47 * breakpoint boolean Sets a breakpoint on this form.
48 *
49 * Most properties above can be set by the reader.
50 *
51 */
52
53 SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
54 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
55 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
56 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
57 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
58
59
60
61 /*
62 * Source properties are stored as double cells with the
63 * following layout:
64
65 * car = tag
66 * cbr = pos
67 * ccr = copy
68 * cdr = plist
69 */
70
71 #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
72 #define SRCPROPBRK(p) (SCM_SMOB_FLAGS (p) & SCM_SOURCE_PROPERTY_FLAG_BREAK)
73 #define SRCPROPPOS(p) (SCM_CELL_WORD(p,1))
74 #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
75 #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
76 #define SRCPROPCOPY(p) (SCM_CELL_OBJECT(p,2))
77 #define SRCPROPPLIST(p) (SCM_CELL_OBJECT_3(p))
78 #define SETSRCPROPBRK(p) \
79 (SCM_SET_SMOB_FLAGS ((p), \
80 SCM_SMOB_FLAGS (p) | SCM_SOURCE_PROPERTY_FLAG_BREAK))
81 #define CLEARSRCPROPBRK(p) \
82 (SCM_SET_SMOB_FLAGS ((p), \
83 SCM_SMOB_FLAGS (p) & ~SCM_SOURCE_PROPERTY_FLAG_BREAK))
84 #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
85 #define SETSRCPROPPOS(p, l, c) (SCM_SET_CELL_WORD(p,1, SRCPROPMAKPOS (l, c)))
86 #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
87 #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
88
89
90
91 scm_t_bits scm_tc16_srcprops;
92
93 static SCM
94 srcprops_mark (SCM obj)
95 {
96 scm_gc_mark (SRCPROPCOPY (obj));
97 return SRCPROPPLIST (obj);
98 }
99
100 static int
101 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
102 {
103 int writingp = SCM_WRITINGP (pstate);
104 scm_puts ("#<srcprops ", port);
105 SCM_SET_WRITINGP (pstate, 1);
106 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
107 SCM_SET_WRITINGP (pstate, writingp);
108 scm_putc ('>', port);
109 return 1;
110 }
111
112
113 int
114 scm_c_source_property_breakpoint_p (SCM form)
115 {
116 SCM obj = scm_whash_lookup (scm_source_whash, form);
117 return SRCPROPSP (obj) && SRCPROPBRK (obj);
118 }
119
120
121 /*
122 * We remember the last file name settings, so we can share that plist
123 * entry. This works because scm_set_source_property_x does not use
124 * assoc-set! for modifying the plist.
125 *
126 * This variable contains a protected cons, whose cdr is the cached
127 * plist
128 */
129 static SCM scm_last_plist_filename;
130
131 SCM
132 scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist)
133 {
134 if (!SCM_UNBNDP (filename))
135 {
136 SCM old_plist = plist;
137
138 /*
139 have to extract the acons, and operate on that, for
140 thread safety.
141 */
142 SCM last_acons = SCM_CDR (scm_last_plist_filename);
143 if (old_plist == SCM_EOL
144 && SCM_CDAR (last_acons) == filename)
145 {
146 plist = last_acons;
147 }
148 else
149 {
150 plist = scm_acons (scm_sym_filename, filename, plist);
151 if (old_plist == SCM_EOL)
152 SCM_SETCDR (scm_last_plist_filename, plist);
153 }
154 }
155
156 SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
157 SRCPROPMAKPOS (line, col),
158 copy,
159 plist);
160 }
161
162
163 SCM
164 scm_srcprops_to_plist (SCM obj)
165 {
166 SCM plist = SRCPROPPLIST (obj);
167 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
168 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
169 plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist);
170 plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist);
171 plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist);
172 return plist;
173 }
174
175 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
176 (SCM obj),
177 "Return the source property association list of @var{obj}.")
178 #define FUNC_NAME s_scm_source_properties
179 {
180 SCM p;
181 SCM_VALIDATE_NIM (1, obj);
182 if (SCM_MEMOIZEDP (obj))
183 obj = SCM_MEMOIZED_EXP (obj);
184 else if (!scm_is_pair (obj))
185 SCM_WRONG_TYPE_ARG (1, obj);
186 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
187 if (SRCPROPSP (p))
188 return scm_srcprops_to_plist (p);
189 else
190 /* list from set-source-properties!, or SCM_EOL for not found */
191 return p;
192 }
193 #undef FUNC_NAME
194
195 /* Perhaps this procedure should look through an alist
196 and try to make a srcprops-object...? */
197 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
198 (SCM obj, SCM plist),
199 "Install the association list @var{plist} as the source property\n"
200 "list for @var{obj}.")
201 #define FUNC_NAME s_scm_set_source_properties_x
202 {
203 SCM handle;
204 SCM_VALIDATE_NIM (1, obj);
205 if (SCM_MEMOIZEDP (obj))
206 obj = SCM_MEMOIZED_EXP (obj);
207 else if (!scm_is_pair (obj))
208 SCM_WRONG_TYPE_ARG(1, obj);
209 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
210
211 return plist;
212 }
213 #undef FUNC_NAME
214
215 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
216 (SCM obj, SCM key),
217 "Return the source property specified by @var{key} from\n"
218 "@var{obj}'s source property list.")
219 #define FUNC_NAME s_scm_source_property
220 {
221 SCM p;
222 SCM_VALIDATE_NIM (1, obj);
223 if (SCM_MEMOIZEDP (obj))
224 obj = SCM_MEMOIZED_EXP (obj);
225 else if (!scm_is_pair (obj))
226 SCM_WRONG_TYPE_ARG (1, obj);
227 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
228 if (!SRCPROPSP (p))
229 goto plist;
230 if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p));
231 else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p));
232 else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p));
233 else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p);
234 else
235 {
236 p = SRCPROPPLIST (p);
237 plist:
238 p = scm_assoc (key, p);
239 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
240 }
241 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
242 }
243 #undef FUNC_NAME
244
245 SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
246 (SCM obj, SCM key, SCM datum),
247 "Set the source property of object @var{obj}, which is specified by\n"
248 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
249 #define FUNC_NAME s_scm_set_source_property_x
250 {
251 scm_whash_handle h;
252 SCM p;
253 SCM_VALIDATE_NIM (1, obj);
254 if (SCM_MEMOIZEDP (obj))
255 obj = SCM_MEMOIZED_EXP (obj);
256 else if (!scm_is_pair (obj))
257 SCM_WRONG_TYPE_ARG (1, obj);
258 h = scm_whash_get_handle (scm_source_whash, obj);
259 if (SCM_WHASHFOUNDP (h))
260 p = SCM_WHASHREF (scm_source_whash, h);
261 else
262 {
263 h = scm_whash_create_handle (scm_source_whash, obj);
264 p = SCM_EOL;
265 }
266 if (scm_is_eq (scm_sym_breakpoint, key))
267 {
268 if (SRCPROPSP (p))
269 {
270 if (scm_is_false (datum))
271 CLEARSRCPROPBRK (p);
272 else
273 SETSRCPROPBRK (p);
274 }
275 else
276 {
277 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
278 SCM_WHASHSET (scm_source_whash, h, sp);
279 if (scm_is_false (datum))
280 CLEARSRCPROPBRK (sp);
281 else
282 SETSRCPROPBRK (sp);
283 }
284 }
285 else if (scm_is_eq (scm_sym_line, key))
286 {
287 if (SRCPROPSP (p))
288 SETSRCPROPLINE (p, scm_to_int (datum));
289 else
290 SCM_WHASHSET (scm_source_whash, h,
291 scm_make_srcprops (scm_to_int (datum), 0,
292 SCM_UNDEFINED, SCM_UNDEFINED, p));
293 }
294 else if (scm_is_eq (scm_sym_column, key))
295 {
296 if (SRCPROPSP (p))
297 SETSRCPROPCOL (p, scm_to_int (datum));
298 else
299 SCM_WHASHSET (scm_source_whash, h,
300 scm_make_srcprops (0, scm_to_int (datum),
301 SCM_UNDEFINED, SCM_UNDEFINED, p));
302 }
303 else if (scm_is_eq (scm_sym_copy, key))
304 {
305 if (SRCPROPSP (p))
306 SRCPROPCOPY (p) = datum;
307 else
308 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
309 }
310 else
311 {
312 if (SRCPROPSP (p))
313 SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p));
314 else
315 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
316 }
317 return SCM_UNSPECIFIED;
318 }
319 #undef FUNC_NAME
320
321
322 void
323 scm_init_srcprop ()
324 {
325 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
326 scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
327 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
328
329 scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
330 scm_c_define ("source-whash", scm_source_whash);
331
332 scm_last_plist_filename
333 = scm_permanent_object (scm_cons (SCM_EOL,
334 scm_acons (SCM_EOL, SCM_EOL, SCM_EOL)));
335
336 #include "libguile/srcprop.x"
337 }
338
339
340 /*
341 Local Variables:
342 c-file-style: "gnu"
343 End:
344 */