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