* tags.h: Update tag system docs.
[bpt/guile.git] / libguile / srcprop.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice.
41 *
42 * The author can be reached at djurfeldt@nada.kth.se
43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
44
45
46 \f
47
48 #include <errno.h>
49
50 #include "libguile/_scm.h"
51 #include "libguile/smob.h"
52 #include "libguile/alist.h"
53 #include "libguile/debug.h"
54 #include "libguile/hashtab.h"
55 #include "libguile/hash.h"
56 #include "libguile/ports.h"
57 #include "libguile/root.h"
58 #include "libguile/weaks.h"
59
60 #include "libguile/validate.h"
61 #include "libguile/srcprop.h"
62 \f
63 /* {Source Properties}
64 *
65 * Properties of source list expressions.
66 * Five of these have special meaning and optimized storage:
67 *
68 * filename string The name of the source file.
69 * copy list A copy of the list expression.
70 * line integer The source code line number.
71 * column integer The source code column number.
72 * breakpoint boolean Sets a breakpoint on this form.
73 *
74 * Most properties above can be set by the reader.
75 *
76 */
77
78 SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
79 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
80 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
81 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
82 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
83
84 scm_t_bits scm_tc16_srcprops;
85 static scm_t_srcprops_chunk *srcprops_chunklist = 0;
86 static scm_t_srcprops *srcprops_freelist = 0;
87
88
89 static SCM
90 srcprops_mark (SCM obj)
91 {
92 scm_gc_mark (SRCPROPFNAME (obj));
93 scm_gc_mark (SRCPROPCOPY (obj));
94 return SRCPROPPLIST (obj);
95 }
96
97
98 static size_t
99 srcprops_free (SCM obj)
100 {
101 *((scm_t_srcprops **) SCM_CELL_WORD_1 (obj)) = srcprops_freelist;
102 srcprops_freelist = (scm_t_srcprops *) SCM_CELL_WORD_1 (obj);
103 return 0; /* srcprops_chunks are not freed until leaving guile */
104 }
105
106
107 static int
108 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
109 {
110 int writingp = SCM_WRITINGP (pstate);
111 scm_puts ("#<srcprops ", port);
112 SCM_SET_WRITINGP (pstate, 1);
113 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
114 SCM_SET_WRITINGP (pstate, writingp);
115 scm_putc ('>', port);
116 return 1;
117 }
118
119
120 SCM
121 scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist)
122 {
123 register scm_t_srcprops *ptr;
124 SCM_DEFER_INTS;
125 if ((ptr = srcprops_freelist) != NULL)
126 srcprops_freelist = *(scm_t_srcprops **)ptr;
127 else
128 {
129 size_t i;
130 scm_t_srcprops_chunk *mem;
131 size_t n = sizeof (scm_t_srcprops_chunk)
132 + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
133 SCM_SYSCALL (mem = (scm_t_srcprops_chunk *) malloc (n));
134 if (mem == NULL)
135 scm_memory_error ("srcprops");
136 scm_mallocated += n;
137 mem->next = srcprops_chunklist;
138 srcprops_chunklist = mem;
139 ptr = &mem->srcprops[0];
140 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
141 *(scm_t_srcprops **)&ptr[i] = &ptr[i + 1];
142 *(scm_t_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
143 srcprops_freelist = (scm_t_srcprops *) &ptr[1];
144 }
145 ptr->pos = SRCPROPMAKPOS (line, col);
146 ptr->fname = filename;
147 ptr->copy = copy;
148 ptr->plist = plist;
149 SCM_ALLOW_INTS;
150 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
151 }
152
153
154 SCM
155 scm_srcprops_to_plist (SCM obj)
156 {
157 SCM plist = SRCPROPPLIST (obj);
158 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
159 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
160 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
161 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
162 plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
163 plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
164 plist = scm_acons (scm_sym_breakpoint, SRCPROPBRK (obj), plist);
165 return plist;
166 }
167
168 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
169 (SCM obj),
170 "Return the source property association list of @var{obj}.")
171 #define FUNC_NAME s_scm_source_properties
172 {
173 SCM p;
174 SCM_VALIDATE_NIM (1,obj);
175 if (SCM_MEMOIZEDP (obj))
176 obj = SCM_MEMOIZED_EXP (obj);
177 #ifndef SCM_RECKLESS
178 else if (SCM_NCONSP (obj))
179 SCM_WRONG_TYPE_ARG (1, obj);
180 #endif
181 p = scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F);
182 if (SRCPROPSP (p))
183 return scm_srcprops_to_plist (p);
184 return SCM_EOL;
185 }
186 #undef FUNC_NAME
187
188 /* Perhaps this procedure should look through an alist
189 and try to make a srcprops-object...? */
190 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
191 (SCM obj, SCM plist),
192 "Install the association list @var{plist} as the source property\n"
193 "list for @var{obj}.")
194 #define FUNC_NAME s_scm_set_source_properties_x
195 {
196 SCM handle;
197 SCM_VALIDATE_NIM (1,obj);
198 if (SCM_MEMOIZEDP (obj))
199 obj = SCM_MEMOIZED_EXP (obj);
200 #ifndef SCM_RECKLESS
201 else if (SCM_NCONSP (obj))
202 SCM_WRONG_TYPE_ARG(1, obj);
203 #endif
204 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
205 SCM_SETCDR (handle, plist);
206 return plist;
207 }
208 #undef FUNC_NAME
209
210 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
211 (SCM obj, SCM key),
212 "Return the source property specified by @var{key} from\n"
213 "@var{obj}'s source property list.")
214 #define FUNC_NAME s_scm_source_property
215 {
216 SCM p;
217 SCM_VALIDATE_NIM (1,obj);
218 if (SCM_MEMOIZEDP (obj))
219 obj = SCM_MEMOIZED_EXP (obj);
220 #ifndef SCM_RECKLESS
221 else if (SCM_NCONSP (obj))
222 SCM_WRONG_TYPE_ARG (1, obj);
223 #endif
224 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
225 if (SCM_IMP (p) || !SRCPROPSP (p))
226 goto plist;
227 if (SCM_EQ_P (scm_sym_breakpoint, key)) p = SRCPROPBRK (p);
228 else if (SCM_EQ_P (scm_sym_line, key)) p = SCM_MAKINUM (SRCPROPLINE (p));
229 else if (SCM_EQ_P (scm_sym_column, key)) p = SCM_MAKINUM (SRCPROPCOL (p));
230 else if (SCM_EQ_P (scm_sym_filename, key)) p = SRCPROPFNAME (p);
231 else if (SCM_EQ_P (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 #ifndef SCM_RECKLESS
255 else if (SCM_NCONSP (obj))
256 SCM_WRONG_TYPE_ARG (1, obj);
257 #endif
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_EQ_P (scm_sym_breakpoint, key))
267 {
268 if (SRCPROPSP (p))
269 {
270 if (SCM_FALSEP (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_FALSEP (datum))
280 CLEARSRCPROPBRK (sp);
281 else
282 SETSRCPROPBRK (sp);
283 }
284 }
285 else if (SCM_EQ_P (scm_sym_line, key))
286 {
287 SCM_VALIDATE_INUM (3,datum);
288 if (SRCPROPSP (p))
289 SETSRCPROPLINE (p, SCM_INUM (datum));
290 else
291 SCM_WHASHSET (scm_source_whash, h,
292 scm_make_srcprops (SCM_INUM (datum), 0,
293 SCM_UNDEFINED, SCM_UNDEFINED, p));
294 }
295 else if (SCM_EQ_P (scm_sym_column, key))
296 {
297 SCM_VALIDATE_INUM (3,datum);
298 if (SRCPROPSP (p))
299 SETSRCPROPCOL (p, SCM_INUM (datum));
300 else
301 SCM_WHASHSET (scm_source_whash, h,
302 scm_make_srcprops (0, SCM_INUM (datum),
303 SCM_UNDEFINED, SCM_UNDEFINED, p));
304 }
305 else if (SCM_EQ_P (scm_sym_filename, key))
306 {
307 if (SRCPROPSP (p))
308 SRCPROPFNAME (p) = datum;
309 else
310 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
311 }
312 else if (SCM_EQ_P (scm_sym_copy, key))
313 {
314 if (SRCPROPSP (p))
315 SRCPROPCOPY (p) = datum;
316 else
317 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
318 }
319 else
320 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
321 return SCM_UNSPECIFIED;
322 }
323 #undef FUNC_NAME
324
325
326 void
327 scm_init_srcprop ()
328 {
329 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
330 scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
331 scm_set_smob_free (scm_tc16_srcprops, srcprops_free);
332 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
333
334 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
335 scm_c_define ("source-whash", scm_source_whash);
336
337 #ifndef SCM_MAGIC_SNARFER
338 #include "libguile/srcprop.x"
339 #endif
340 }
341
342 void
343 scm_finish_srcprop ()
344 {
345 register scm_t_srcprops_chunk *ptr = srcprops_chunklist, *next;
346 while (ptr)
347 {
348 next = ptr->next;
349 free ((char *) ptr);
350 scm_mallocated -= sizeof (scm_t_srcprops_chunk)
351 + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
352 ptr = next;
353 }
354 }
355
356 /*
357 Local Variables:
358 c-file-style: "gnu"
359 End:
360 */