* backtrace.c, debug.c, eval.c, eval.h, gsubr.c, procprop.h,
[bpt/guile.git] / libguile / srcprop.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997,1998 Free Software Foundation
575888bd
MD
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
575888bd
MD
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
82892bed 43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
575888bd
MD
44\f
45
46#include <stdio.h>
47#include "_scm.h"
20e6290e 48#include "smob.h"
56366edf 49#include "genio.h"
20e6290e
JB
50#include "alist.h"
51#include "debug.h"
52#include "hashtab.h"
5c5549cb 53#include "hash.h"
20e6290e 54#include "weaks.h"
575888bd 55
20e6290e 56#include "srcprop.h"
575888bd
MD
57\f
58/* {Source Properties}
59 *
60 * Properties of source list expressions.
61 * Five of these have special meaning and optimized storage:
62 *
63 * filename string The name of the source file.
64 * copy list A copy of the list expression.
65 * line integer The source code line number.
66 * column integer The source code column number.
67 * breakpoint boolean Sets a breakpoint on this form.
68 *
69 * Most properties above can be set by the reader.
70 *
71 */
72
92e5aa0e
MD
73SCM scm_sym_filename;
74SCM scm_sym_copy;
75SCM scm_sym_line;
76SCM scm_sym_column;
77SCM scm_sym_breakpoint;
575888bd 78
5c5549cb 79long scm_tc16_srcprops;
575888bd
MD
80static scm_srcprops_chunk *srcprops_chunklist = 0;
81static scm_srcprops *srcprops_freelist = 0;
82
1cc91f1b
JB
83
84static SCM marksrcprops SCM_P ((SCM obj));
85
575888bd
MD
86static SCM
87marksrcprops (obj)
88 SCM obj;
575888bd 89{
575888bd
MD
90 scm_gc_mark (SRCPROPFNAME (obj));
91 scm_gc_mark (SRCPROPCOPY (obj));
92 return SRCPROPPLIST (obj);
93}
94
1cc91f1b
JB
95
96static scm_sizet freesrcprops SCM_P ((SCM obj));
97
575888bd
MD
98static scm_sizet
99freesrcprops (obj)
100 SCM obj;
575888bd
MD
101{
102 *((scm_srcprops **) SCM_CDR (obj)) = srcprops_freelist;
103 srcprops_freelist = (scm_srcprops *) SCM_CDR (obj);
104 return 0; /* srcprops_chunks are not freed until leaving guile */
105}
106
1cc91f1b
JB
107
108static int prinsrcprops SCM_P ((SCM obj, SCM port, scm_print_state *pstate));
109
575888bd 110static int
19402679 111prinsrcprops (obj, port, pstate)
575888bd
MD
112 SCM obj;
113 SCM port;
19402679 114 scm_print_state *pstate;
575888bd 115{
19402679 116 int writingp = SCM_WRITINGP (pstate);
b7f3516f 117 scm_puts ("#<srcprops ", port);
19402679
MD
118 SCM_SET_WRITINGP (pstate, 1);
119 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
120 SCM_SET_WRITINGP (pstate, writingp);
b7f3516f 121 scm_putc ('>', port);
575888bd
MD
122 return 1;
123}
124
1cc91f1b 125
575888bd 126SCM
5c5549cb 127scm_make_srcprops (line, col, filename, copy, plist)
575888bd
MD
128 int line;
129 int col;
130 SCM filename;
131 SCM copy;
132 SCM plist;
575888bd 133{
575888bd
MD
134 register scm_srcprops *ptr;
135 SCM_DEFER_INTS;
136 if ((ptr = srcprops_freelist) != NULL)
137 srcprops_freelist = *(scm_srcprops **)ptr;
138 else
139 {
140 int i;
141 scm_srcprops_chunk *mem;
142 scm_sizet n = sizeof (scm_srcprops_chunk)
143 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
144 SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n));
145 SCM_ASSERT (mem, SCM_UNDEFINED, SCM_NALLOC, "srcprops");
146 scm_mallocated += n;
147 mem->next = srcprops_chunklist;
148 srcprops_chunklist = mem;
149 ptr = &mem->srcprops[0];
150 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
151 *(scm_srcprops **)&ptr[i] = &ptr[i + 1];
152 *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
153 srcprops_freelist = (scm_srcprops *) &ptr[1];
154 }
575888bd
MD
155 ptr->pos = SRCPROPMAKPOS (line, col);
156 ptr->fname = filename;
157 ptr->copy = copy;
158 ptr->plist = plist;
23a62151 159 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
575888bd
MD
160}
161
1cc91f1b 162
575888bd
MD
163SCM
164scm_srcprops_to_plist (obj)
165 SCM obj;
575888bd
MD
166{
167 SCM plist = SRCPROPPLIST (obj);
168 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
92e5aa0e 169 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
575888bd 170 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
92e5aa0e
MD
171 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
172 plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
173 plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
174 plist = scm_acons (scm_sym_breakpoint, SRCPROPBRK (obj), plist);
575888bd
MD
175 return plist;
176}
177
178SCM_PROC (s_source_properties, "source-properties", 1, 0, 0, scm_source_properties);
1cc91f1b 179
575888bd
MD
180SCM
181scm_source_properties (obj)
182 SCM obj;
575888bd
MD
183{
184 SCM p;
a4645b97 185 SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_source_properties);
575888bd 186 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
187 obj = SCM_MEMOIZED_EXP (obj);
188#ifndef SCM_RECKLESS
189 else if (SCM_NCONSP (obj))
190 scm_wrong_type_arg (s_source_properties, 1, obj);
191#endif
575888bd
MD
192 p = scm_hashq_ref (scm_source_whash, obj, (SCM) NULL);
193 if (p != (SCM) NULL && SRCPROPSP (p))
194 return scm_srcprops_to_plist (p);
195 return SCM_EOL;
196}
197
198/* Perhaps this procedure should look through an alist
199 and try to make a srcprops-object...? */
200SCM_PROC (s_set_source_properties_x, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x);
1cc91f1b 201
575888bd
MD
202SCM
203scm_set_source_properties_x (obj, plist)
204 SCM obj;
205 SCM plist;
575888bd
MD
206{
207 SCM handle;
a4645b97 208 SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_set_source_properties_x);
575888bd 209 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
210 obj = SCM_MEMOIZED_EXP (obj);
211#ifndef SCM_RECKLESS
212 else if (SCM_NCONSP (obj))
213 scm_wrong_type_arg (s_set_source_properties_x, 1, obj);
214#endif
575888bd
MD
215 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
216 SCM_SETCDR (handle, plist);
217 return plist;
218}
219
220SCM_PROC (s_source_property, "source-property", 2, 0, 0, scm_source_property);
1cc91f1b 221
575888bd
MD
222SCM
223scm_source_property (obj, key)
224 SCM obj;
225 SCM key;
575888bd
MD
226{
227 SCM p;
a4645b97 228 SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_source_property);
575888bd 229 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
230 obj = SCM_MEMOIZED_EXP (obj);
231#ifndef SCM_RECKLESS
232 else if (SCM_NCONSP (obj))
233 scm_wrong_type_arg (s_source_property, 1, obj);
234#endif
575888bd
MD
235 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
236 if (SCM_IMP (p) || !SRCPROPSP (p))
237 goto plist;
92e5aa0e
MD
238 if (scm_sym_breakpoint == key) p = SRCPROPBRK (p);
239 else if (scm_sym_line == key) p = SCM_MAKINUM (SRCPROPLINE (p));
240 else if (scm_sym_column == key) p = SCM_MAKINUM (SRCPROPCOL (p));
241 else if (scm_sym_filename == key) p = SRCPROPFNAME (p);
242 else if (scm_sym_copy == key) p = SRCPROPCOPY (p);
575888bd
MD
243 else
244 {
245 p = SRCPROPPLIST (p);
246 plist:
247 p = scm_assoc (key, p);
248 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
249 }
250 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
251}
252
253SCM_PROC (s_set_source_property_x, "set-source-property!", 3, 0, 0, scm_set_source_property_x);
1cc91f1b 254
575888bd
MD
255SCM
256scm_set_source_property_x (obj, key, datum)
257 SCM obj;
258 SCM key;
259 SCM datum;
575888bd
MD
260{
261 scm_whash_handle h;
262 SCM p;
a4645b97 263 SCM_ASSERT (SCM_NIMP (obj), obj, SCM_ARG1, s_set_source_property_x);
575888bd 264 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
265 obj = SCM_MEMOIZED_EXP (obj);
266#ifndef SCM_RECKLESS
267 else if (SCM_NCONSP (obj))
268 scm_wrong_type_arg (s_set_source_property_x, 1, obj);
269#endif
575888bd
MD
270 h = scm_whash_get_handle (scm_source_whash, obj);
271 if (SCM_WHASHFOUNDP (h))
272 p = SCM_WHASHREF (scm_source_whash, h);
273 else
274 {
275 h = scm_whash_create_handle (scm_source_whash, obj);
276 p = SCM_EOL;
277 }
92e5aa0e 278 if (scm_sym_breakpoint == key)
cda139a7
MD
279 {
280 if (SCM_FALSEP (datum))
281 CLEARSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
282 ? p
283 : SCM_WHASHSET (scm_source_whash, h,
284 scm_make_srcprops (0,
285 0,
286 SCM_UNDEFINED,
287 SCM_UNDEFINED,
288 p)));
289 else
290 SETSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p)
575888bd
MD
291 ? p
292 : SCM_WHASHSET (scm_source_whash, h,
cda139a7
MD
293 scm_make_srcprops (0,
294 0,
295 SCM_UNDEFINED,
296 SCM_UNDEFINED,
297 p)));
298 }
92e5aa0e 299 else if (scm_sym_line == key)
575888bd 300 {
a9dbb9fd
MD
301 SCM_ASSERT (SCM_INUMP (datum),
302 datum, SCM_ARG3, s_set_source_property_x);
575888bd 303 if (SCM_NIMP (p) && SRCPROPSP (p))
a9dbb9fd 304 SETSRCPROPLINE (p, SCM_INUM (datum));
575888bd
MD
305 else
306 SCM_WHASHSET (scm_source_whash, h,
a9dbb9fd
MD
307 scm_make_srcprops (SCM_INUM (datum), 0,
308 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 309 }
92e5aa0e 310 else if (scm_sym_column == key)
575888bd 311 {
a9dbb9fd
MD
312 SCM_ASSERT (SCM_INUMP (datum),
313 datum, SCM_ARG3, s_set_source_property_x);
575888bd 314 if (SCM_NIMP (p) && SRCPROPSP (p))
a9dbb9fd 315 SETSRCPROPCOL (p, SCM_INUM (datum));
575888bd
MD
316 else
317 SCM_WHASHSET (scm_source_whash, h,
a9dbb9fd
MD
318 scm_make_srcprops (0, SCM_INUM (datum),
319 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 320 }
92e5aa0e 321 else if (scm_sym_filename == key)
575888bd
MD
322 {
323 if (SCM_NIMP (p) && SRCPROPSP (p))
324 SRCPROPFNAME (p) = datum;
325 else
5c5549cb 326 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
575888bd 327 }
92e5aa0e 328 else if (scm_sym_filename == key)
575888bd
MD
329 {
330 if (SCM_NIMP (p) && SRCPROPSP (p))
331 SRCPROPCOPY (p) = datum;
332 else
5c5549cb 333 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
334 }
335 else
336 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
337 return SCM_UNSPECIFIED;
338}
339
1cc91f1b 340
575888bd
MD
341void
342scm_init_srcprop ()
575888bd 343{
23a62151
MD
344 scm_tc16_srcprops = scm_make_smob_type_mfpe ("srcprops", 0,
345 marksrcprops, freesrcprops, prinsrcprops, NULL);
5c5549cb 346 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
575888bd 347
92e5aa0e
MD
348 scm_sym_filename = SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED));
349 scm_sym_copy = SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED));
350 scm_sym_line = SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED));
351 scm_sym_column = SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED));
352 scm_sym_breakpoint = SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED));
575888bd
MD
353
354 scm_sysintern ("source-whash", scm_source_whash);
355#include "srcprop.x"
356}
357
358void
359scm_finish_srcprop ()
360{
361 register scm_srcprops_chunk *ptr = srcprops_chunklist, *next;
362 while (ptr)
363 {
364 next = ptr->next;
365 free ((char *) ptr);
366 scm_mallocated -= sizeof (scm_srcprops_chunk)
367 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
5c5549cb 368 ptr = next;
575888bd
MD
369 }
370}