* __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
1cc91f1b
JB
80
81static SCM marksrcprops SCM_P ((SCM obj));
82
575888bd
MD
83static SCM
84marksrcprops (obj)
85 SCM obj;
575888bd
MD
86{
87 SCM_SETGC8MARK (obj);
88 scm_gc_mark (SRCPROPFNAME (obj));
89 scm_gc_mark (SRCPROPCOPY (obj));
90 return SRCPROPPLIST (obj);
91}
92
1cc91f1b
JB
93
94static scm_sizet freesrcprops SCM_P ((SCM obj));
95
575888bd
MD
96static scm_sizet
97freesrcprops (obj)
98 SCM obj;
575888bd
MD
99{
100 *((scm_srcprops **) SCM_CDR (obj)) = srcprops_freelist;
101 srcprops_freelist = (scm_srcprops *) SCM_CDR (obj);
102 return 0; /* srcprops_chunks are not freed until leaving guile */
103}
104
1cc91f1b
JB
105
106static int prinsrcprops SCM_P ((SCM obj, SCM port, scm_print_state *pstate));
107
575888bd 108static int
19402679 109prinsrcprops (obj, port, pstate)
575888bd
MD
110 SCM obj;
111 SCM port;
19402679 112 scm_print_state *pstate;
575888bd 113{
19402679 114 int writingp = SCM_WRITINGP (pstate);
575888bd 115 scm_gen_puts (scm_regular_string, "#<srcprops ", port);
19402679
MD
116 SCM_SET_WRITINGP (pstate, 1);
117 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
118 SCM_SET_WRITINGP (pstate, writingp);
575888bd
MD
119 scm_gen_putc ('>', port);
120 return 1;
121}
122
123static scm_smobfuns srcpropssmob =
124{marksrcprops, freesrcprops, prinsrcprops, 0};
125
1cc91f1b 126
575888bd 127SCM
5c5549cb 128scm_make_srcprops (line, col, filename, copy, plist)
575888bd
MD
129 int line;
130 int col;
131 SCM filename;
132 SCM copy;
133 SCM plist;
575888bd
MD
134{
135 register SCM ans;
136 register scm_srcprops *ptr;
137 SCM_DEFER_INTS;
138 if ((ptr = srcprops_freelist) != NULL)
139 srcprops_freelist = *(scm_srcprops **)ptr;
140 else
141 {
142 int i;
143 scm_srcprops_chunk *mem;
144 scm_sizet n = sizeof (scm_srcprops_chunk)
145 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
146 SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n));
147 SCM_ASSERT (mem, SCM_UNDEFINED, SCM_NALLOC, "srcprops");
148 scm_mallocated += n;
149 mem->next = srcprops_chunklist;
150 srcprops_chunklist = mem;
151 ptr = &mem->srcprops[0];
152 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
153 *(scm_srcprops **)&ptr[i] = &ptr[i + 1];
154 *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
155 srcprops_freelist = (scm_srcprops *) &ptr[1];
156 }
157 SCM_NEWCELL (ans);
5c5549cb 158 SCM_CAR (ans) = scm_tc16_srcprops;
575888bd
MD
159 ptr->pos = SRCPROPMAKPOS (line, col);
160 ptr->fname = filename;
161 ptr->copy = copy;
162 ptr->plist = plist;
163 SCM_CDR (ans) = (SCM) ptr;
164 SCM_ALLOW_INTS;
165 return ans;
166}
167
1cc91f1b 168
575888bd
MD
169SCM
170scm_srcprops_to_plist (obj)
171 SCM obj;
575888bd
MD
172{
173 SCM plist = SRCPROPPLIST (obj);
174 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
175 plist = scm_acons (scm_i_copy, SRCPROPCOPY (obj), plist);
176 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
177 plist = scm_acons (scm_i_filename, SRCPROPFNAME (obj), plist);
178 plist = scm_acons (scm_i_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
179 plist = scm_acons (scm_i_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
180 plist = scm_acons (scm_i_breakpoint, SRCPROPBRK (obj), plist);
181 return plist;
182}
183
184SCM_PROC (s_source_properties, "source-properties", 1, 0, 0, scm_source_properties);
1cc91f1b 185
575888bd
MD
186SCM
187scm_source_properties (obj)
188 SCM obj;
575888bd
MD
189{
190 SCM p;
191 if (SCM_MEMOIZEDP (obj))
192 obj = SCM_MEMOEXP (obj);
193 p = scm_hashq_ref (scm_source_whash, obj, (SCM) NULL);
194 if (p != (SCM) NULL && SRCPROPSP (p))
195 return scm_srcprops_to_plist (p);
196 return SCM_EOL;
197}
198
199/* Perhaps this procedure should look through an alist
200 and try to make a srcprops-object...? */
201SCM_PROC (s_set_source_properties_x, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x);
1cc91f1b 202
575888bd
MD
203SCM
204scm_set_source_properties_x (obj, plist)
205 SCM obj;
206 SCM plist;
575888bd
MD
207{
208 SCM handle;
209 if (SCM_MEMOIZEDP (obj))
210 obj = SCM_MEMOEXP (obj);
211 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
212 SCM_SETCDR (handle, plist);
213 return plist;
214}
215
216SCM_PROC (s_source_property, "source-property", 2, 0, 0, scm_source_property);
1cc91f1b 217
575888bd
MD
218SCM
219scm_source_property (obj, key)
220 SCM obj;
221 SCM key;
575888bd
MD
222{
223 SCM p;
224 if (SCM_MEMOIZEDP (obj))
225 obj = SCM_MEMOEXP (obj);
226 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
227 if (SCM_IMP (p) || !SRCPROPSP (p))
228 goto plist;
229 if (scm_i_breakpoint == key) p = SRCPROPBRK (p);
230 else if (scm_i_line == key) p = SCM_MAKINUM (SRCPROPLINE (p));
231 else if (scm_i_column == key) p = SCM_MAKINUM (SRCPROPCOL (p));
232 else if (scm_i_filename == key) p = SRCPROPFNAME (p);
233 else if (scm_i_copy == key) p = SRCPROPCOPY (p);
234 else
235 {
236 p = SRCPROPPLIST (p);
237 plist:
238 p = scm_assoc (key, p);
239 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
240 }
241 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
242}
243
244SCM_PROC (s_set_source_property_x, "set-source-property!", 3, 0, 0, scm_set_source_property_x);
1cc91f1b 245
575888bd
MD
246SCM
247scm_set_source_property_x (obj, key, datum)
248 SCM obj;
249 SCM key;
250 SCM datum;
575888bd
MD
251{
252 scm_whash_handle h;
253 SCM p;
254 if (SCM_MEMOIZEDP (obj))
255 obj = SCM_MEMOEXP (obj);
256 h = scm_whash_get_handle (scm_source_whash, obj);
257 if (SCM_WHASHFOUNDP (h))
258 p = SCM_WHASHREF (scm_source_whash, h);
259 else
260 {
261 h = scm_whash_create_handle (scm_source_whash, obj);
262 p = SCM_EOL;
263 }
264 if (scm_i_breakpoint == key)
265 if (SCM_FALSEP (datum))
266 CLEARSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
267 ? p
268 : SCM_WHASHSET (scm_source_whash, h,
5c5549cb 269 scm_make_srcprops (0, 0, SCM_UNDEFINED,
575888bd
MD
270 SCM_UNDEFINED, p)));
271 else
272 SETSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
273 ? p
274 : SCM_WHASHSET (scm_source_whash, h,
5c5549cb 275 scm_make_srcprops (0, 0, SCM_UNDEFINED,
575888bd
MD
276 SCM_UNDEFINED, p)));
277 else if (scm_i_line == key)
278 {
279 if (SCM_NIMP (p) && SRCPROPSP (p))
280 SETSRCPROPLINE (p, datum);
281 else
282 SCM_WHASHSET (scm_source_whash, h,
5c5549cb 283 scm_make_srcprops (datum, 0, SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd
MD
284 }
285 else if (scm_i_column == key)
286 {
287 if (SCM_NIMP (p) && SRCPROPSP (p))
288 SETSRCPROPCOL (p, datum);
289 else
290 SCM_WHASHSET (scm_source_whash, h,
5c5549cb 291 scm_make_srcprops (0, datum, SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd
MD
292 }
293 else if (scm_i_filename == key)
294 {
295 if (SCM_NIMP (p) && SRCPROPSP (p))
296 SRCPROPFNAME (p) = datum;
297 else
5c5549cb 298 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
575888bd
MD
299 }
300 else if (scm_i_filename == key)
301 {
302 if (SCM_NIMP (p) && SRCPROPSP (p))
303 SRCPROPCOPY (p) = datum;
304 else
5c5549cb 305 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
306 }
307 else
308 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
309 return SCM_UNSPECIFIED;
310}
311
1cc91f1b 312
575888bd
MD
313void
314scm_init_srcprop ()
575888bd 315{
5c5549cb
MD
316 scm_tc16_srcprops = scm_newsmob (&srcpropssmob);
317 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
575888bd
MD
318
319 scm_i_filename = SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED));
320 scm_i_copy = SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED));
321 scm_i_line = SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED));
322 scm_i_column = SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED));
323 scm_i_breakpoint = SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED));
324
325 scm_sysintern ("source-whash", scm_source_whash);
326#include "srcprop.x"
327}
328
329void
330scm_finish_srcprop ()
331{
332 register scm_srcprops_chunk *ptr = srcprops_chunklist, *next;
333 while (ptr)
334 {
335 next = ptr->next;
336 free ((char *) ptr);
337 scm_mallocated -= sizeof (scm_srcprops_chunk)
338 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
5c5549cb 339 ptr = next;
575888bd
MD
340 }
341}