Changed license terms to the plain LGPL thru-out.
[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_CELL_WORD_1 (obj)) = srcprops_freelist;
75 srcprops_freelist = (scm_t_srcprops *) SCM_CELL_WORD_1 (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_mallocated += n;
118 mem->next = srcprops_chunklist;
119 srcprops_chunklist = mem;
120 ptr = &mem->srcprops[0];
121 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
122 *(scm_t_srcprops **)&ptr[i] = &ptr[i + 1];
123 *(scm_t_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
124 srcprops_freelist = (scm_t_srcprops *) &ptr[1];
125 }
126 ptr->pos = SRCPROPMAKPOS (line, col);
127 ptr->fname = filename;
128 ptr->copy = copy;
129 ptr->plist = plist;
130 SCM_ALLOW_INTS;
131 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
132 }
133
134
135 SCM
136 scm_srcprops_to_plist (SCM obj)
137 {
138 SCM plist = SRCPROPPLIST (obj);
139 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
140 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
141 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
142 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
143 plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
144 plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
145 plist = scm_acons (scm_sym_breakpoint, SCM_BOOL (SRCPROPBRK (obj)), plist);
146 return plist;
147 }
148
149 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
150 (SCM obj),
151 "Return the source property association list of @var{obj}.")
152 #define FUNC_NAME s_scm_source_properties
153 {
154 SCM p;
155 SCM_VALIDATE_NIM (1, obj);
156 if (SCM_MEMOIZEDP (obj))
157 obj = SCM_MEMOIZED_EXP (obj);
158 else if (!SCM_CONSP (obj))
159 SCM_WRONG_TYPE_ARG (1, obj);
160 p = scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F);
161 if (SRCPROPSP (p))
162 return scm_srcprops_to_plist (p);
163 return SCM_EOL;
164 }
165 #undef FUNC_NAME
166
167 /* Perhaps this procedure should look through an alist
168 and try to make a srcprops-object...? */
169 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
170 (SCM obj, SCM plist),
171 "Install the association list @var{plist} as the source property\n"
172 "list for @var{obj}.")
173 #define FUNC_NAME s_scm_set_source_properties_x
174 {
175 SCM handle;
176 SCM_VALIDATE_NIM (1, obj);
177 if (SCM_MEMOIZEDP (obj))
178 obj = SCM_MEMOIZED_EXP (obj);
179 else if (!SCM_CONSP (obj))
180 SCM_WRONG_TYPE_ARG(1, obj);
181 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
182 SCM_SETCDR (handle, plist);
183 return plist;
184 }
185 #undef FUNC_NAME
186
187 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
188 (SCM obj, SCM key),
189 "Return the source property specified by @var{key} from\n"
190 "@var{obj}'s source property list.")
191 #define FUNC_NAME s_scm_source_property
192 {
193 SCM p;
194 SCM_VALIDATE_NIM (1, obj);
195 if (SCM_MEMOIZEDP (obj))
196 obj = SCM_MEMOIZED_EXP (obj);
197 else if (!SCM_CONSP (obj))
198 SCM_WRONG_TYPE_ARG (1, obj);
199 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
200 if (!SRCPROPSP (p))
201 goto plist;
202 if (SCM_EQ_P (scm_sym_breakpoint, key)) p = SCM_BOOL (SRCPROPBRK (p));
203 else if (SCM_EQ_P (scm_sym_line, key)) p = SCM_MAKINUM (SRCPROPLINE (p));
204 else if (SCM_EQ_P (scm_sym_column, key)) p = SCM_MAKINUM (SRCPROPCOL (p));
205 else if (SCM_EQ_P (scm_sym_filename, key)) p = SRCPROPFNAME (p);
206 else if (SCM_EQ_P (scm_sym_copy, key)) p = SRCPROPCOPY (p);
207 else
208 {
209 p = SRCPROPPLIST (p);
210 plist:
211 p = scm_assoc (key, p);
212 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
213 }
214 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
215 }
216 #undef FUNC_NAME
217
218 SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
219 (SCM obj, SCM key, SCM datum),
220 "Set the source property of object @var{obj}, which is specified by\n"
221 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
222 #define FUNC_NAME s_scm_set_source_property_x
223 {
224 scm_whash_handle h;
225 SCM p;
226 SCM_VALIDATE_NIM (1, obj);
227 if (SCM_MEMOIZEDP (obj))
228 obj = SCM_MEMOIZED_EXP (obj);
229 else if (!SCM_CONSP (obj))
230 SCM_WRONG_TYPE_ARG (1, obj);
231 h = scm_whash_get_handle (scm_source_whash, obj);
232 if (SCM_WHASHFOUNDP (h))
233 p = SCM_WHASHREF (scm_source_whash, h);
234 else
235 {
236 h = scm_whash_create_handle (scm_source_whash, obj);
237 p = SCM_EOL;
238 }
239 if (SCM_EQ_P (scm_sym_breakpoint, key))
240 {
241 if (SRCPROPSP (p))
242 {
243 if (SCM_FALSEP (datum))
244 CLEARSRCPROPBRK (p);
245 else
246 SETSRCPROPBRK (p);
247 }
248 else
249 {
250 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
251 SCM_WHASHSET (scm_source_whash, h, sp);
252 if (SCM_FALSEP (datum))
253 CLEARSRCPROPBRK (sp);
254 else
255 SETSRCPROPBRK (sp);
256 }
257 }
258 else if (SCM_EQ_P (scm_sym_line, key))
259 {
260 SCM_VALIDATE_INUM (3, datum);
261 if (SRCPROPSP (p))
262 SETSRCPROPLINE (p, SCM_INUM (datum));
263 else
264 SCM_WHASHSET (scm_source_whash, h,
265 scm_make_srcprops (SCM_INUM (datum), 0,
266 SCM_UNDEFINED, SCM_UNDEFINED, p));
267 }
268 else if (SCM_EQ_P (scm_sym_column, key))
269 {
270 SCM_VALIDATE_INUM (3, datum);
271 if (SRCPROPSP (p))
272 SETSRCPROPCOL (p, SCM_INUM (datum));
273 else
274 SCM_WHASHSET (scm_source_whash, h,
275 scm_make_srcprops (0, SCM_INUM (datum),
276 SCM_UNDEFINED, SCM_UNDEFINED, p));
277 }
278 else if (SCM_EQ_P (scm_sym_filename, key))
279 {
280 if (SRCPROPSP (p))
281 SRCPROPFNAME (p) = datum;
282 else
283 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
284 }
285 else if (SCM_EQ_P (scm_sym_copy, key))
286 {
287 if (SRCPROPSP (p))
288 SRCPROPCOPY (p) = datum;
289 else
290 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
291 }
292 else
293 {
294 if (SRCPROPSP (p))
295 SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p));
296 else
297 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
298 }
299 return SCM_UNSPECIFIED;
300 }
301 #undef FUNC_NAME
302
303
304 void
305 scm_init_srcprop ()
306 {
307 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
308 scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
309 scm_set_smob_free (scm_tc16_srcprops, srcprops_free);
310 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
311
312 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
313 scm_c_define ("source-whash", scm_source_whash);
314
315 #include "libguile/srcprop.x"
316 }
317
318 void
319 scm_finish_srcprop ()
320 {
321 register scm_t_srcprops_chunk *ptr = srcprops_chunklist, *next;
322 while (ptr)
323 {
324 next = ptr->next;
325 free ((char *) ptr);
326 scm_mallocated -= sizeof (scm_t_srcprops_chunk)
327 + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
328 ptr = next;
329 }
330 }
331
332 /*
333 Local Variables:
334 c-file-style: "gnu"
335 End:
336 */