* __scm.h, alist.c, alist.h, append.c, append.h, appinit.c,
[bpt/guile.git] / libguile / srcprop.c
CommitLineData
575888bd
MD
1/* Copyright (C) 1995,1996 Mikael Djurfeldt
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, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 *
41 * The author can be reached at djurfeldt@nada.kth.se
42 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN
43 */
44\f
45
46#include <stdio.h>
47#include "_scm.h"
20e6290e
JB
48#include "smob.h"
49#include "alist.h"
50#include "debug.h"
51#include "hashtab.h"
5c5549cb 52#include "hash.h"
20e6290e 53#include "weaks.h"
575888bd 54
20e6290e 55#include "srcprop.h"
575888bd
MD
56\f
57/* {Source Properties}
58 *
59 * Properties of source list expressions.
60 * Five of these have special meaning and optimized storage:
61 *
62 * filename string The name of the source file.
63 * copy list A copy of the list expression.
64 * line integer The source code line number.
65 * column integer The source code column number.
66 * breakpoint boolean Sets a breakpoint on this form.
67 *
68 * Most properties above can be set by the reader.
69 *
70 */
71
72SCM scm_i_copy;
73static SCM scm_i_breakpoint, scm_i_line, scm_i_column;
74static SCM scm_i_filename;
75
5c5549cb 76long scm_tc16_srcprops;
575888bd
MD
77static scm_srcprops_chunk *srcprops_chunklist = 0;
78static scm_srcprops *srcprops_freelist = 0;
79
80#ifdef __STDC__
81static SCM
82marksrcprops (SCM obj)
83#else
84static SCM
85marksrcprops (obj)
86 SCM obj;
87#endif
88{
89 SCM_SETGC8MARK (obj);
90 scm_gc_mark (SRCPROPFNAME (obj));
91 scm_gc_mark (SRCPROPCOPY (obj));
92 return SRCPROPPLIST (obj);
93}
94
95#ifdef __STDC__
96static scm_sizet
97freesrcprops (SCM obj)
98#else
99static scm_sizet
100freesrcprops (obj)
101 SCM obj;
102#endif
103{
104 *((scm_srcprops **) SCM_CDR (obj)) = srcprops_freelist;
105 srcprops_freelist = (scm_srcprops *) SCM_CDR (obj);
106 return 0; /* srcprops_chunks are not freed until leaving guile */
107}
108
109#ifdef __STDC__
110static int
19402679 111prinsrcprops (SCM obj, SCM port, scm_print_state *pstate)
575888bd
MD
112#else
113static int
19402679 114prinsrcprops (obj, port, pstate)
575888bd
MD
115 SCM obj;
116 SCM port;
19402679 117 scm_print_state *pstate;
575888bd
MD
118#endif
119{
19402679 120 int writingp = SCM_WRITINGP (pstate);
575888bd 121 scm_gen_puts (scm_regular_string, "#<srcprops ", port);
19402679
MD
122 SCM_SET_WRITINGP (pstate, 1);
123 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
124 SCM_SET_WRITINGP (pstate, writingp);
575888bd
MD
125 scm_gen_putc ('>', port);
126 return 1;
127}
128
129static scm_smobfuns srcpropssmob =
130{marksrcprops, freesrcprops, prinsrcprops, 0};
131
132#ifdef __STDC__
133SCM
5c5549cb 134scm_make_srcprops (int line, int col, SCM filename, SCM copy, SCM plist)
575888bd
MD
135#else
136SCM
5c5549cb 137scm_make_srcprops (line, col, filename, copy, plist)
575888bd
MD
138 int line;
139 int col;
140 SCM filename;
141 SCM copy;
142 SCM plist;
143#endif
144{
145 register SCM ans;
146 register scm_srcprops *ptr;
147 SCM_DEFER_INTS;
148 if ((ptr = srcprops_freelist) != NULL)
149 srcprops_freelist = *(scm_srcprops **)ptr;
150 else
151 {
152 int i;
153 scm_srcprops_chunk *mem;
154 scm_sizet n = sizeof (scm_srcprops_chunk)
155 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
156 SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n));
157 SCM_ASSERT (mem, SCM_UNDEFINED, SCM_NALLOC, "srcprops");
158 scm_mallocated += n;
159 mem->next = srcprops_chunklist;
160 srcprops_chunklist = mem;
161 ptr = &mem->srcprops[0];
162 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
163 *(scm_srcprops **)&ptr[i] = &ptr[i + 1];
164 *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
165 srcprops_freelist = (scm_srcprops *) &ptr[1];
166 }
167 SCM_NEWCELL (ans);
5c5549cb 168 SCM_CAR (ans) = scm_tc16_srcprops;
575888bd
MD
169 ptr->pos = SRCPROPMAKPOS (line, col);
170 ptr->fname = filename;
171 ptr->copy = copy;
172 ptr->plist = plist;
173 SCM_CDR (ans) = (SCM) ptr;
174 SCM_ALLOW_INTS;
175 return ans;
176}
177
178#ifdef __STDC__
179SCM
180scm_srcprops_to_plist (SCM obj)
181#else
182SCM
183scm_srcprops_to_plist (obj)
184 SCM obj;
185#endif
186{
187 SCM plist = SRCPROPPLIST (obj);
188 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
189 plist = scm_acons (scm_i_copy, SRCPROPCOPY (obj), plist);
190 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
191 plist = scm_acons (scm_i_filename, SRCPROPFNAME (obj), plist);
192 plist = scm_acons (scm_i_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
193 plist = scm_acons (scm_i_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
194 plist = scm_acons (scm_i_breakpoint, SRCPROPBRK (obj), plist);
195 return plist;
196}
197
198SCM_PROC (s_source_properties, "source-properties", 1, 0, 0, scm_source_properties);
199#ifdef __STDC__
200SCM
201scm_source_properties (SCM obj)
202#else
203SCM
204scm_source_properties (obj)
205 SCM obj;
206#endif
207{
208 SCM p;
209 if (SCM_MEMOIZEDP (obj))
210 obj = SCM_MEMOEXP (obj);
211 p = scm_hashq_ref (scm_source_whash, obj, (SCM) NULL);
212 if (p != (SCM) NULL && SRCPROPSP (p))
213 return scm_srcprops_to_plist (p);
214 return SCM_EOL;
215}
216
217/* Perhaps this procedure should look through an alist
218 and try to make a srcprops-object...? */
219SCM_PROC (s_set_source_properties_x, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x);
220#ifdef __STDC__
221SCM
222scm_set_source_properties_x (SCM obj, SCM plist)
223#else
224SCM
225scm_set_source_properties_x (obj, plist)
226 SCM obj;
227 SCM plist;
228#endif
229{
230 SCM handle;
231 if (SCM_MEMOIZEDP (obj))
232 obj = SCM_MEMOEXP (obj);
233 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
234 SCM_SETCDR (handle, plist);
235 return plist;
236}
237
238SCM_PROC (s_source_property, "source-property", 2, 0, 0, scm_source_property);
239#ifdef __STDC__
240SCM
241scm_source_property (SCM obj, SCM key)
242#else
243SCM
244scm_source_property (obj, key)
245 SCM obj;
246 SCM key;
247#endif
248{
249 SCM p;
250 if (SCM_MEMOIZEDP (obj))
251 obj = SCM_MEMOEXP (obj);
252 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
253 if (SCM_IMP (p) || !SRCPROPSP (p))
254 goto plist;
255 if (scm_i_breakpoint == key) p = SRCPROPBRK (p);
256 else if (scm_i_line == key) p = SCM_MAKINUM (SRCPROPLINE (p));
257 else if (scm_i_column == key) p = SCM_MAKINUM (SRCPROPCOL (p));
258 else if (scm_i_filename == key) p = SRCPROPFNAME (p);
259 else if (scm_i_copy == key) p = SRCPROPCOPY (p);
260 else
261 {
262 p = SRCPROPPLIST (p);
263 plist:
264 p = scm_assoc (key, p);
265 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
266 }
267 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
268}
269
270SCM_PROC (s_set_source_property_x, "set-source-property!", 3, 0, 0, scm_set_source_property_x);
271#ifdef __STDC__
272SCM
273scm_set_source_property_x (SCM obj, SCM key, SCM datum)
274#else
275SCM
276scm_set_source_property_x (obj, key, datum)
277 SCM obj;
278 SCM key;
279 SCM datum;
280#endif
281{
282 scm_whash_handle h;
283 SCM p;
284 if (SCM_MEMOIZEDP (obj))
285 obj = SCM_MEMOEXP (obj);
286 h = scm_whash_get_handle (scm_source_whash, obj);
287 if (SCM_WHASHFOUNDP (h))
288 p = SCM_WHASHREF (scm_source_whash, h);
289 else
290 {
291 h = scm_whash_create_handle (scm_source_whash, obj);
292 p = SCM_EOL;
293 }
294 if (scm_i_breakpoint == key)
295 if (SCM_FALSEP (datum))
296 CLEARSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
297 ? p
298 : SCM_WHASHSET (scm_source_whash, h,
5c5549cb 299 scm_make_srcprops (0, 0, SCM_UNDEFINED,
575888bd
MD
300 SCM_UNDEFINED, p)));
301 else
302 SETSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
303 ? p
304 : SCM_WHASHSET (scm_source_whash, h,
5c5549cb 305 scm_make_srcprops (0, 0, SCM_UNDEFINED,
575888bd
MD
306 SCM_UNDEFINED, p)));
307 else if (scm_i_line == key)
308 {
309 if (SCM_NIMP (p) && SRCPROPSP (p))
310 SETSRCPROPLINE (p, datum);
311 else
312 SCM_WHASHSET (scm_source_whash, h,
5c5549cb 313 scm_make_srcprops (datum, 0, SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd
MD
314 }
315 else if (scm_i_column == key)
316 {
317 if (SCM_NIMP (p) && SRCPROPSP (p))
318 SETSRCPROPCOL (p, datum);
319 else
320 SCM_WHASHSET (scm_source_whash, h,
5c5549cb 321 scm_make_srcprops (0, datum, SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd
MD
322 }
323 else if (scm_i_filename == key)
324 {
325 if (SCM_NIMP (p) && SRCPROPSP (p))
326 SRCPROPFNAME (p) = datum;
327 else
5c5549cb 328 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
575888bd
MD
329 }
330 else if (scm_i_filename == key)
331 {
332 if (SCM_NIMP (p) && SRCPROPSP (p))
333 SRCPROPCOPY (p) = datum;
334 else
5c5549cb 335 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
336 }
337 else
338 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
339 return SCM_UNSPECIFIED;
340}
341
342#ifdef __STDC__
343void
344scm_init_srcprop (void)
345#else
346void
347scm_init_srcprop ()
348#endif
349{
5c5549cb
MD
350 scm_tc16_srcprops = scm_newsmob (&srcpropssmob);
351 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
575888bd
MD
352
353 scm_i_filename = SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED));
354 scm_i_copy = SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED));
355 scm_i_line = SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED));
356 scm_i_column = SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED));
357 scm_i_breakpoint = SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED));
358
359 scm_sysintern ("source-whash", scm_source_whash);
360#include "srcprop.x"
361}
362
363void
364scm_finish_srcprop ()
365{
366 register scm_srcprops_chunk *ptr = srcprops_chunklist, *next;
367 while (ptr)
368 {
369 next = ptr->next;
370 free ((char *) ptr);
371 scm_mallocated -= sizeof (scm_srcprops_chunk)
372 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
5c5549cb 373 ptr = next;
575888bd
MD
374 }
375}