Merge from mvo-vcell-cleanup-1-branch.
[bpt/guile.git] / libguile / srcprop.c
CommitLineData
216eedfc 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 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 */
1bbd0b84
GB
44
45/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
46 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
47
575888bd
MD
48\f
49
e6e2e95a
MD
50#include <errno.h>
51
a0599745
MD
52#include "libguile/_scm.h"
53#include "libguile/smob.h"
54#include "libguile/alist.h"
55#include "libguile/debug.h"
56#include "libguile/hashtab.h"
57#include "libguile/hash.h"
58#include "libguile/ports.h"
59#include "libguile/root.h"
60#include "libguile/weaks.h"
575888bd 61
a0599745
MD
62#include "libguile/validate.h"
63#include "libguile/srcprop.h"
575888bd
MD
64\f
65/* {Source Properties}
66 *
67 * Properties of source list expressions.
68 * Five of these have special meaning and optimized storage:
69 *
70 * filename string The name of the source file.
71 * copy list A copy of the list expression.
72 * line integer The source code line number.
73 * column integer The source code column number.
74 * breakpoint boolean Sets a breakpoint on this form.
75 *
76 * Most properties above can be set by the reader.
77 *
78 */
79
85db4a2c
DH
80SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
81SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
82SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
83SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
84SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
575888bd 85
e841c3e0 86scm_bits_t scm_tc16_srcprops;
575888bd
MD
87static scm_srcprops_chunk *srcprops_chunklist = 0;
88static scm_srcprops *srcprops_freelist = 0;
89
1cc91f1b 90
575888bd 91static SCM
e841c3e0 92srcprops_mark (SCM obj)
575888bd 93{
575888bd
MD
94 scm_gc_mark (SRCPROPFNAME (obj));
95 scm_gc_mark (SRCPROPCOPY (obj));
96 return SRCPROPPLIST (obj);
97}
98
1cc91f1b 99
575888bd 100static scm_sizet
e841c3e0 101srcprops_free (SCM obj)
575888bd 102{
54778cd3
DH
103 *((scm_srcprops **) SCM_CELL_WORD_1 (obj)) = srcprops_freelist;
104 srcprops_freelist = (scm_srcprops *) SCM_CELL_WORD_1 (obj);
575888bd
MD
105 return 0; /* srcprops_chunks are not freed until leaving guile */
106}
107
1cc91f1b 108
575888bd 109static int
e841c3e0 110srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
575888bd 111{
19402679 112 int writingp = SCM_WRITINGP (pstate);
b7f3516f 113 scm_puts ("#<srcprops ", port);
19402679
MD
114 SCM_SET_WRITINGP (pstate, 1);
115 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
116 SCM_SET_WRITINGP (pstate, writingp);
b7f3516f 117 scm_putc ('>', port);
575888bd
MD
118 return 1;
119}
120
1cc91f1b 121
575888bd 122SCM
1bbd0b84 123scm_make_srcprops (int line, int col, SCM filename, SCM copy, SCM plist)
575888bd 124{
575888bd
MD
125 register scm_srcprops *ptr;
126 SCM_DEFER_INTS;
127 if ((ptr = srcprops_freelist) != NULL)
128 srcprops_freelist = *(scm_srcprops **)ptr;
129 else
130 {
131 int i;
132 scm_srcprops_chunk *mem;
133 scm_sizet n = sizeof (scm_srcprops_chunk)
134 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
135 SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n));
2500356c
DH
136 if (mem == NULL)
137 scm_memory_error ("srcprops");
575888bd
MD
138 scm_mallocated += n;
139 mem->next = srcprops_chunklist;
140 srcprops_chunklist = mem;
141 ptr = &mem->srcprops[0];
142 for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i)
143 *(scm_srcprops **)&ptr[i] = &ptr[i + 1];
144 *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0;
145 srcprops_freelist = (scm_srcprops *) &ptr[1];
146 }
575888bd
MD
147 ptr->pos = SRCPROPMAKPOS (line, col);
148 ptr->fname = filename;
149 ptr->copy = copy;
150 ptr->plist = plist;
216eedfc 151 SCM_ALLOW_INTS;
23a62151 152 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
575888bd
MD
153}
154
1cc91f1b 155
575888bd 156SCM
1bbd0b84 157scm_srcprops_to_plist (SCM obj)
575888bd
MD
158{
159 SCM plist = SRCPROPPLIST (obj);
160 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
92e5aa0e 161 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
575888bd 162 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
92e5aa0e
MD
163 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
164 plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
165 plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
166 plist = scm_acons (scm_sym_breakpoint, SRCPROPBRK (obj), plist);
575888bd
MD
167 return plist;
168}
169
a1ec6916 170SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
1bbd0b84 171 (SCM obj),
e3239868 172 "Return the source property association list of @var{obj}.")
1bbd0b84 173#define FUNC_NAME s_scm_source_properties
575888bd
MD
174{
175 SCM p;
6b5a304f 176 SCM_VALIDATE_NIM (1,obj);
575888bd 177 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
178 obj = SCM_MEMOIZED_EXP (obj);
179#ifndef SCM_RECKLESS
180 else if (SCM_NCONSP (obj))
1bbd0b84 181 SCM_WRONG_TYPE_ARG (1, obj);
a4645b97 182#endif
230d095f
DH
183 p = scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F);
184 if (SRCPROPSP (p))
575888bd
MD
185 return scm_srcprops_to_plist (p);
186 return SCM_EOL;
187}
1bbd0b84 188#undef FUNC_NAME
575888bd
MD
189
190/* Perhaps this procedure should look through an alist
191 and try to make a srcprops-object...? */
a1ec6916 192SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
1bbd0b84 193 (SCM obj, SCM plist),
e3239868
DH
194 "Install the association list @var{plist} as the source property\n"
195 "list for @var{obj}.")
1bbd0b84 196#define FUNC_NAME s_scm_set_source_properties_x
575888bd
MD
197{
198 SCM handle;
6b5a304f 199 SCM_VALIDATE_NIM (1,obj);
575888bd 200 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
201 obj = SCM_MEMOIZED_EXP (obj);
202#ifndef SCM_RECKLESS
203 else if (SCM_NCONSP (obj))
1bbd0b84 204 SCM_WRONG_TYPE_ARG(1, obj);
a4645b97 205#endif
575888bd
MD
206 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
207 SCM_SETCDR (handle, plist);
208 return plist;
209}
1bbd0b84 210#undef FUNC_NAME
575888bd 211
a1ec6916 212SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
1bbd0b84 213 (SCM obj, SCM key),
e3239868
DH
214 "Return the source property specified by @var{key} from\n"
215 "@var{obj}'s source property list.")
1bbd0b84 216#define FUNC_NAME s_scm_source_property
575888bd
MD
217{
218 SCM p;
6b5a304f 219 SCM_VALIDATE_NIM (1,obj);
575888bd 220 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
221 obj = SCM_MEMOIZED_EXP (obj);
222#ifndef SCM_RECKLESS
09a56810 223 else if (SCM_NECONSP (obj))
1bbd0b84 224 SCM_WRONG_TYPE_ARG (1, obj);
a4645b97 225#endif
575888bd
MD
226 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
227 if (SCM_IMP (p) || !SRCPROPSP (p))
228 goto plist;
54778cd3
DH
229 if (SCM_EQ_P (scm_sym_breakpoint, key)) p = SRCPROPBRK (p);
230 else if (SCM_EQ_P (scm_sym_line, key)) p = SCM_MAKINUM (SRCPROPLINE (p));
231 else if (SCM_EQ_P (scm_sym_column, key)) p = SCM_MAKINUM (SRCPROPCOL (p));
232 else if (SCM_EQ_P (scm_sym_filename, key)) p = SRCPROPFNAME (p);
233 else if (SCM_EQ_P (scm_sym_copy, key)) p = SRCPROPCOPY (p);
575888bd
MD
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}
1bbd0b84 243#undef FUNC_NAME
575888bd 244
a1ec6916 245SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
1bbd0b84 246 (SCM obj, SCM key, SCM datum),
e3239868
DH
247 "Set the source property of object @var{obj}, which is specified by\n"
248 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
1bbd0b84 249#define FUNC_NAME s_scm_set_source_property_x
575888bd
MD
250{
251 scm_whash_handle h;
252 SCM p;
6b5a304f 253 SCM_VALIDATE_NIM (1,obj);
575888bd 254 if (SCM_MEMOIZEDP (obj))
a4645b97
MD
255 obj = SCM_MEMOIZED_EXP (obj);
256#ifndef SCM_RECKLESS
257 else if (SCM_NCONSP (obj))
1bbd0b84 258 SCM_WRONG_TYPE_ARG (1, obj);
a4645b97 259#endif
575888bd
MD
260 h = scm_whash_get_handle (scm_source_whash, obj);
261 if (SCM_WHASHFOUNDP (h))
262 p = SCM_WHASHREF (scm_source_whash, h);
263 else
264 {
265 h = scm_whash_create_handle (scm_source_whash, obj);
266 p = SCM_EOL;
267 }
54778cd3 268 if (SCM_EQ_P (scm_sym_breakpoint, key))
cda139a7 269 {
62850ef3
DH
270 if (SRCPROPSP (p))
271 {
272 if (SCM_FALSEP (datum))
273 CLEARSRCPROPBRK (p);
274 else
275 SETSRCPROPBRK (p);
276 }
cda139a7 277 else
62850ef3
DH
278 {
279 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
280 SCM_WHASHSET (scm_source_whash, h, sp);
281 if (SCM_FALSEP (datum))
282 CLEARSRCPROPBRK (sp);
283 else
284 SETSRCPROPBRK (sp);
285 }
cda139a7 286 }
54778cd3 287 else if (SCM_EQ_P (scm_sym_line, key))
575888bd 288 {
3b3b36dd 289 SCM_VALIDATE_INUM (3,datum);
0c95b57d 290 if (SRCPROPSP (p))
a9dbb9fd 291 SETSRCPROPLINE (p, SCM_INUM (datum));
575888bd
MD
292 else
293 SCM_WHASHSET (scm_source_whash, h,
a9dbb9fd
MD
294 scm_make_srcprops (SCM_INUM (datum), 0,
295 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 296 }
54778cd3 297 else if (SCM_EQ_P (scm_sym_column, key))
575888bd 298 {
3b3b36dd 299 SCM_VALIDATE_INUM (3,datum);
0c95b57d 300 if (SRCPROPSP (p))
a9dbb9fd 301 SETSRCPROPCOL (p, SCM_INUM (datum));
575888bd
MD
302 else
303 SCM_WHASHSET (scm_source_whash, h,
a9dbb9fd
MD
304 scm_make_srcprops (0, SCM_INUM (datum),
305 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 306 }
54778cd3 307 else if (SCM_EQ_P (scm_sym_filename, key))
575888bd 308 {
0c95b57d 309 if (SRCPROPSP (p))
575888bd
MD
310 SRCPROPFNAME (p) = datum;
311 else
5c5549cb 312 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
575888bd 313 }
0419a528 314 else if (SCM_EQ_P (scm_sym_copy, key))
575888bd 315 {
0c95b57d 316 if (SRCPROPSP (p))
575888bd
MD
317 SRCPROPCOPY (p) = datum;
318 else
5c5549cb 319 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
320 }
321 else
322 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
323 return SCM_UNSPECIFIED;
324}
1bbd0b84 325#undef FUNC_NAME
575888bd 326
1cc91f1b 327
575888bd
MD
328void
329scm_init_srcprop ()
575888bd 330{
e841c3e0
KN
331 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
332 scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
333 scm_set_smob_free (scm_tc16_srcprops, srcprops_free);
334 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
335
5c5549cb 336 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
86d31dfe 337 scm_c_define ("source-whash", scm_source_whash);
85db4a2c 338
8dc9439f 339#ifndef SCM_MAGIC_SNARFER
a0599745 340#include "libguile/srcprop.x"
8dc9439f 341#endif
575888bd
MD
342}
343
344void
345scm_finish_srcprop ()
346{
347 register scm_srcprops_chunk *ptr = srcprops_chunklist, *next;
348 while (ptr)
349 {
350 next = ptr->next;
351 free ((char *) ptr);
352 scm_mallocated -= sizeof (scm_srcprops_chunk)
353 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
5c5549cb 354 ptr = next;
575888bd
MD
355 }
356}
89e00824
ML
357
358/*
359 Local Variables:
360 c-file-style: "gnu"
361 End:
362*/