1 /* Copyright (C) 1995,1996,1997,1998 Free Software Foundation
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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.
42 * The author can be reached at djurfeldt@nada.kth.se
43 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
58 /* {Source Properties}
60 * Properties of source list expressions.
61 * Five of these have special meaning and optimized storage:
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.
69 * Most properties above can be set by the reader.
79 long scm_tc16_srcprops
;
80 static scm_srcprops_chunk
*srcprops_chunklist
= 0;
81 static scm_srcprops
*srcprops_freelist
= 0;
84 static SCM marksrcprops
SCM_P ((SCM obj
));
90 scm_gc_mark (SRCPROPFNAME (obj
));
91 scm_gc_mark (SRCPROPCOPY (obj
));
92 return SRCPROPPLIST (obj
);
96 static scm_sizet freesrcprops
SCM_P ((SCM obj
));
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 */
108 static int prinsrcprops
SCM_P ((SCM obj
, SCM port
, scm_print_state
*pstate
));
111 prinsrcprops (obj
, port
, pstate
)
114 scm_print_state
*pstate
;
116 int writingp
= SCM_WRITINGP (pstate
);
117 scm_puts ("#<srcprops ", port
);
118 SCM_SET_WRITINGP (pstate
, 1);
119 scm_iprin1 (scm_srcprops_to_plist (obj
), port
, pstate
);
120 SCM_SET_WRITINGP (pstate
, writingp
);
121 scm_putc ('>', port
);
125 static scm_smobfuns srcpropssmob
=
126 {marksrcprops
, freesrcprops
, prinsrcprops
, 0};
130 scm_make_srcprops (line
, col
, filename
, copy
, plist
)
138 register scm_srcprops
*ptr
;
140 if ((ptr
= srcprops_freelist
) != NULL
)
141 srcprops_freelist
= *(scm_srcprops
**)ptr
;
145 scm_srcprops_chunk
*mem
;
146 scm_sizet n
= sizeof (scm_srcprops_chunk
)
147 + sizeof (scm_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
148 SCM_SYSCALL (mem
= (scm_srcprops_chunk
*) malloc (n
));
149 SCM_ASSERT (mem
, SCM_UNDEFINED
, SCM_NALLOC
, "srcprops");
151 mem
->next
= srcprops_chunklist
;
152 srcprops_chunklist
= mem
;
153 ptr
= &mem
->srcprops
[0];
154 for (i
= 1; i
< SRCPROPS_CHUNKSIZE
- 1; ++i
)
155 *(scm_srcprops
**)&ptr
[i
] = &ptr
[i
+ 1];
156 *(scm_srcprops
**)&ptr
[SRCPROPS_CHUNKSIZE
- 1] = 0;
157 srcprops_freelist
= (scm_srcprops
*) &ptr
[1];
160 SCM_SETCAR (ans
, scm_tc16_srcprops
);
161 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
162 ptr
->fname
= filename
;
165 SCM_SETCDR (ans
, (SCM
) ptr
);
172 scm_srcprops_to_plist (obj
)
175 SCM plist
= SRCPROPPLIST (obj
);
176 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
177 plist
= scm_acons (scm_i_copy
, SRCPROPCOPY (obj
), plist
);
178 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
179 plist
= scm_acons (scm_i_filename
, SRCPROPFNAME (obj
), plist
);
180 plist
= scm_acons (scm_i_column
, SCM_MAKINUM (SRCPROPCOL (obj
)), plist
);
181 plist
= scm_acons (scm_i_line
, SCM_MAKINUM (SRCPROPLINE (obj
)), plist
);
182 plist
= scm_acons (scm_i_breakpoint
, SRCPROPBRK (obj
), plist
);
186 SCM_PROC (s_source_properties
, "source-properties", 1, 0, 0, scm_source_properties
);
189 scm_source_properties (obj
)
193 SCM_ASSERT (SCM_NIMP (obj
), obj
, SCM_ARG1
, s_source_properties
);
194 if (SCM_MEMOIZEDP (obj
))
195 obj
= SCM_MEMOIZED_EXP (obj
);
197 else if (SCM_NCONSP (obj
))
198 scm_wrong_type_arg (s_source_properties
, 1, obj
);
200 p
= scm_hashq_ref (scm_source_whash
, obj
, (SCM
) NULL
);
201 if (p
!= (SCM
) NULL
&& SRCPROPSP (p
))
202 return scm_srcprops_to_plist (p
);
206 /* Perhaps this procedure should look through an alist
207 and try to make a srcprops-object...? */
208 SCM_PROC (s_set_source_properties_x
, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x
);
211 scm_set_source_properties_x (obj
, plist
)
216 SCM_ASSERT (SCM_NIMP (obj
), obj
, SCM_ARG1
, s_set_source_properties_x
);
217 if (SCM_MEMOIZEDP (obj
))
218 obj
= SCM_MEMOIZED_EXP (obj
);
220 else if (SCM_NCONSP (obj
))
221 scm_wrong_type_arg (s_set_source_properties_x
, 1, obj
);
223 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
224 SCM_SETCDR (handle
, plist
);
228 SCM_PROC (s_source_property
, "source-property", 2, 0, 0, scm_source_property
);
231 scm_source_property (obj
, key
)
236 SCM_ASSERT (SCM_NIMP (obj
), obj
, SCM_ARG1
, s_source_property
);
237 if (SCM_MEMOIZEDP (obj
))
238 obj
= SCM_MEMOIZED_EXP (obj
);
240 else if (SCM_NCONSP (obj
))
241 scm_wrong_type_arg (s_source_property
, 1, obj
);
243 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
244 if (SCM_IMP (p
) || !SRCPROPSP (p
))
246 if (scm_i_breakpoint
== key
) p
= SRCPROPBRK (p
);
247 else if (scm_i_line
== key
) p
= SCM_MAKINUM (SRCPROPLINE (p
));
248 else if (scm_i_column
== key
) p
= SCM_MAKINUM (SRCPROPCOL (p
));
249 else if (scm_i_filename
== key
) p
= SRCPROPFNAME (p
);
250 else if (scm_i_copy
== key
) p
= SRCPROPCOPY (p
);
253 p
= SRCPROPPLIST (p
);
255 p
= scm_assoc (key
, p
);
256 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
258 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
261 SCM_PROC (s_set_source_property_x
, "set-source-property!", 3, 0, 0, scm_set_source_property_x
);
264 scm_set_source_property_x (obj
, key
, datum
)
271 SCM_ASSERT (SCM_NIMP (obj
), obj
, SCM_ARG1
, s_set_source_property_x
);
272 if (SCM_MEMOIZEDP (obj
))
273 obj
= SCM_MEMOIZED_EXP (obj
);
275 else if (SCM_NCONSP (obj
))
276 scm_wrong_type_arg (s_set_source_property_x
, 1, obj
);
278 h
= scm_whash_get_handle (scm_source_whash
, obj
);
279 if (SCM_WHASHFOUNDP (h
))
280 p
= SCM_WHASHREF (scm_source_whash
, h
);
283 h
= scm_whash_create_handle (scm_source_whash
, obj
);
286 if (scm_i_breakpoint
== key
)
288 if (SCM_FALSEP (datum
))
289 CLEARSRCPROPBRK (SCM_NIMP (p
) && SRCPROPSP (p
)
291 : SCM_WHASHSET (scm_source_whash
, h
,
292 scm_make_srcprops (0,
298 SETSRCPROPBRK (SCM_NIMP (p
) && SRCPROPSP (p
)
300 : SCM_WHASHSET (scm_source_whash
, h
,
301 scm_make_srcprops (0,
307 else if (scm_i_line
== key
)
309 SCM_ASSERT (SCM_INUMP (datum
),
310 datum
, SCM_ARG3
, s_set_source_property_x
);
311 if (SCM_NIMP (p
) && SRCPROPSP (p
))
312 SETSRCPROPLINE (p
, SCM_INUM (datum
));
314 SCM_WHASHSET (scm_source_whash
, h
,
315 scm_make_srcprops (SCM_INUM (datum
), 0,
316 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
318 else if (scm_i_column
== key
)
320 SCM_ASSERT (SCM_INUMP (datum
),
321 datum
, SCM_ARG3
, s_set_source_property_x
);
322 if (SCM_NIMP (p
) && SRCPROPSP (p
))
323 SETSRCPROPCOL (p
, SCM_INUM (datum
));
325 SCM_WHASHSET (scm_source_whash
, h
,
326 scm_make_srcprops (0, SCM_INUM (datum
),
327 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
329 else if (scm_i_filename
== key
)
331 if (SCM_NIMP (p
) && SRCPROPSP (p
))
332 SRCPROPFNAME (p
) = datum
;
334 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
336 else if (scm_i_filename
== key
)
338 if (SCM_NIMP (p
) && SRCPROPSP (p
))
339 SRCPROPCOPY (p
) = datum
;
341 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
344 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
345 return SCM_UNSPECIFIED
;
352 scm_tc16_srcprops
= scm_newsmob (&srcpropssmob
);
353 scm_source_whash
= scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
355 scm_i_filename
= SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED
));
356 scm_i_copy
= SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED
));
357 scm_i_line
= SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED
));
358 scm_i_column
= SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED
));
359 scm_i_breakpoint
= SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED
));
361 scm_sysintern ("source-whash", scm_source_whash
);
366 scm_finish_srcprop ()
368 register scm_srcprops_chunk
*ptr
= srcprops_chunklist
, *next
;
373 scm_mallocated
-= sizeof (scm_srcprops_chunk
)
374 + sizeof (scm_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);