* _scm.h: Removed #include <errno.h>.
[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 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
46 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
47
48 \f
49
50 #include <errno.h>
51
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"
61
62 #include "libguile/validate.h"
63 #include "libguile/srcprop.h"
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
80 SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
81 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
82 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
83 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
84 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
85
86 scm_bits_t scm_tc16_srcprops;
87 static scm_srcprops_chunk *srcprops_chunklist = 0;
88 static scm_srcprops *srcprops_freelist = 0;
89
90
91 static SCM
92 srcprops_mark (SCM obj)
93 {
94 scm_gc_mark (SRCPROPFNAME (obj));
95 scm_gc_mark (SRCPROPCOPY (obj));
96 return SRCPROPPLIST (obj);
97 }
98
99
100 static scm_sizet
101 srcprops_free (SCM obj)
102 {
103 *((scm_srcprops **) SCM_CELL_WORD_1 (obj)) = srcprops_freelist;
104 srcprops_freelist = (scm_srcprops *) SCM_CELL_WORD_1 (obj);
105 return 0; /* srcprops_chunks are not freed until leaving guile */
106 }
107
108
109 static int
110 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
111 {
112 int writingp = SCM_WRITINGP (pstate);
113 scm_puts ("#<srcprops ", port);
114 SCM_SET_WRITINGP (pstate, 1);
115 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
116 SCM_SET_WRITINGP (pstate, writingp);
117 scm_putc ('>', port);
118 return 1;
119 }
120
121
122 SCM
123 scm_make_srcprops (int line, int col, SCM filename, SCM copy, SCM plist)
124 {
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));
136 if (mem == NULL)
137 scm_memory_error ("srcprops");
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 }
147 ptr->pos = SRCPROPMAKPOS (line, col);
148 ptr->fname = filename;
149 ptr->copy = copy;
150 ptr->plist = plist;
151 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
152 }
153
154
155 SCM
156 scm_srcprops_to_plist (SCM obj)
157 {
158 SCM plist = SRCPROPPLIST (obj);
159 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
160 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
161 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
162 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
163 plist = scm_acons (scm_sym_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist);
164 plist = scm_acons (scm_sym_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist);
165 plist = scm_acons (scm_sym_breakpoint, SRCPROPBRK (obj), plist);
166 return plist;
167 }
168
169 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
170 (SCM obj),
171 "Return the source property association list of @var{obj}.")
172 #define FUNC_NAME s_scm_source_properties
173 {
174 SCM p;
175 SCM_VALIDATE_NIM (1,obj);
176 if (SCM_MEMOIZEDP (obj))
177 obj = SCM_MEMOIZED_EXP (obj);
178 #ifndef SCM_RECKLESS
179 else if (SCM_NCONSP (obj))
180 SCM_WRONG_TYPE_ARG (1, obj);
181 #endif
182 p = scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F);
183 if (SRCPROPSP (p))
184 return scm_srcprops_to_plist (p);
185 return SCM_EOL;
186 }
187 #undef FUNC_NAME
188
189 /* Perhaps this procedure should look through an alist
190 and try to make a srcprops-object...? */
191 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
192 (SCM obj, SCM plist),
193 "Install the association list @var{plist} as the source property\n"
194 "list for @var{obj}.")
195 #define FUNC_NAME s_scm_set_source_properties_x
196 {
197 SCM handle;
198 SCM_VALIDATE_NIM (1,obj);
199 if (SCM_MEMOIZEDP (obj))
200 obj = SCM_MEMOIZED_EXP (obj);
201 #ifndef SCM_RECKLESS
202 else if (SCM_NCONSP (obj))
203 SCM_WRONG_TYPE_ARG(1, obj);
204 #endif
205 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
206 SCM_SETCDR (handle, plist);
207 return plist;
208 }
209 #undef FUNC_NAME
210
211 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
212 (SCM obj, SCM key),
213 "Return the source property specified by @var{key} from\n"
214 "@var{obj}'s source property list.")
215 #define FUNC_NAME s_scm_source_property
216 {
217 SCM p;
218 SCM_VALIDATE_NIM (1,obj);
219 if (SCM_MEMOIZEDP (obj))
220 obj = SCM_MEMOIZED_EXP (obj);
221 #ifndef SCM_RECKLESS
222 else if (SCM_NECONSP (obj))
223 SCM_WRONG_TYPE_ARG (1, obj);
224 #endif
225 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
226 if (SCM_IMP (p) || !SRCPROPSP (p))
227 goto plist;
228 if (SCM_EQ_P (scm_sym_breakpoint, key)) p = SRCPROPBRK (p);
229 else if (SCM_EQ_P (scm_sym_line, key)) p = SCM_MAKINUM (SRCPROPLINE (p));
230 else if (SCM_EQ_P (scm_sym_column, key)) p = SCM_MAKINUM (SRCPROPCOL (p));
231 else if (SCM_EQ_P (scm_sym_filename, key)) p = SRCPROPFNAME (p);
232 else if (SCM_EQ_P (scm_sym_copy, key)) p = SRCPROPCOPY (p);
233 else
234 {
235 p = SRCPROPPLIST (p);
236 plist:
237 p = scm_assoc (key, p);
238 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
239 }
240 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
241 }
242 #undef FUNC_NAME
243
244 SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
245 (SCM obj, SCM key, SCM datum),
246 "Set the source property of object @var{obj}, which is specified by\n"
247 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
248 #define FUNC_NAME s_scm_set_source_property_x
249 {
250 scm_whash_handle h;
251 SCM p;
252 SCM_VALIDATE_NIM (1,obj);
253 if (SCM_MEMOIZEDP (obj))
254 obj = SCM_MEMOIZED_EXP (obj);
255 #ifndef SCM_RECKLESS
256 else if (SCM_NCONSP (obj))
257 SCM_WRONG_TYPE_ARG (1, obj);
258 #endif
259 h = scm_whash_get_handle (scm_source_whash, obj);
260 if (SCM_WHASHFOUNDP (h))
261 p = SCM_WHASHREF (scm_source_whash, h);
262 else
263 {
264 h = scm_whash_create_handle (scm_source_whash, obj);
265 p = SCM_EOL;
266 }
267 if (SCM_EQ_P (scm_sym_breakpoint, key))
268 {
269 if (SRCPROPSP (p))
270 {
271 if (SCM_FALSEP (datum))
272 CLEARSRCPROPBRK (p);
273 else
274 SETSRCPROPBRK (p);
275 }
276 else
277 {
278 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
279 SCM_WHASHSET (scm_source_whash, h, sp);
280 if (SCM_FALSEP (datum))
281 CLEARSRCPROPBRK (sp);
282 else
283 SETSRCPROPBRK (sp);
284 }
285 }
286 else if (SCM_EQ_P (scm_sym_line, key))
287 {
288 SCM_VALIDATE_INUM (3,datum);
289 if (SRCPROPSP (p))
290 SETSRCPROPLINE (p, SCM_INUM (datum));
291 else
292 SCM_WHASHSET (scm_source_whash, h,
293 scm_make_srcprops (SCM_INUM (datum), 0,
294 SCM_UNDEFINED, SCM_UNDEFINED, p));
295 }
296 else if (SCM_EQ_P (scm_sym_column, key))
297 {
298 SCM_VALIDATE_INUM (3,datum);
299 if (SRCPROPSP (p))
300 SETSRCPROPCOL (p, SCM_INUM (datum));
301 else
302 SCM_WHASHSET (scm_source_whash, h,
303 scm_make_srcprops (0, SCM_INUM (datum),
304 SCM_UNDEFINED, SCM_UNDEFINED, p));
305 }
306 else if (SCM_EQ_P (scm_sym_filename, key))
307 {
308 if (SRCPROPSP (p))
309 SRCPROPFNAME (p) = datum;
310 else
311 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
312 }
313 else if (SCM_EQ_P (scm_sym_copy, key))
314 {
315 if (SRCPROPSP (p))
316 SRCPROPCOPY (p) = datum;
317 else
318 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
319 }
320 else
321 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
322 return SCM_UNSPECIFIED;
323 }
324 #undef FUNC_NAME
325
326
327 void
328 scm_init_srcprop ()
329 {
330 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
331 scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark);
332 scm_set_smob_free (scm_tc16_srcprops, srcprops_free);
333 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
334
335 scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
336 scm_sysintern ("source-whash", scm_source_whash);
337
338 #ifndef SCM_MAGIC_SNARFER
339 #include "libguile/srcprop.x"
340 #endif
341 }
342
343 void
344 scm_finish_srcprop ()
345 {
346 register scm_srcprops_chunk *ptr = srcprops_chunklist, *next;
347 while (ptr)
348 {
349 next = ptr->next;
350 free ((char *) ptr);
351 scm_mallocated -= sizeof (scm_srcprops_chunk)
352 + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1);
353 ptr = next;
354 }
355 }
356
357 /*
358 Local Variables:
359 c-file-style: "gnu"
360 End:
361 */