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