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