1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 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 */
45 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
46 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
51 #include "libguile/_scm.h"
52 #include "libguile/smob.h"
53 #include "libguile/alist.h"
54 #include "libguile/debug.h"
55 #include "libguile/hashtab.h"
56 #include "libguile/hash.h"
57 #include "libguile/ports.h"
58 #include "libguile/root.h"
59 #include "libguile/weaks.h"
61 #include "libguile/validate.h"
62 #include "libguile/srcprop.h"
64 /* {Source Properties}
66 * Properties of source list expressions.
67 * Five of these have special meaning and optimized storage:
69 * filename string The name of the source file.
70 * copy list A copy of the list expression.
71 * line integer The source code line number.
72 * column integer The source code column number.
73 * breakpoint boolean Sets a breakpoint on this form.
75 * Most properties above can be set by the reader.
79 SCM_GLOBAL_SYMBOL (scm_sym_filename
, "filename");
80 SCM_GLOBAL_SYMBOL (scm_sym_copy
, "copy");
81 SCM_GLOBAL_SYMBOL (scm_sym_line
, "line");
82 SCM_GLOBAL_SYMBOL (scm_sym_column
, "column");
83 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint
, "breakpoint");
85 scm_bits_t scm_tc16_srcprops
;
86 static scm_srcprops_chunk
*srcprops_chunklist
= 0;
87 static scm_srcprops
*srcprops_freelist
= 0;
91 srcprops_mark (SCM obj
)
93 scm_gc_mark (SRCPROPFNAME (obj
));
94 scm_gc_mark (SRCPROPCOPY (obj
));
95 return SRCPROPPLIST (obj
);
100 srcprops_free (SCM obj
)
102 *((scm_srcprops
**) SCM_CELL_WORD_1 (obj
)) = srcprops_freelist
;
103 srcprops_freelist
= (scm_srcprops
*) SCM_CELL_WORD_1 (obj
);
104 return 0; /* srcprops_chunks are not freed until leaving guile */
109 srcprops_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
111 int writingp
= SCM_WRITINGP (pstate
);
112 scm_puts ("#<srcprops ", port
);
113 SCM_SET_WRITINGP (pstate
, 1);
114 scm_iprin1 (scm_srcprops_to_plist (obj
), port
, pstate
);
115 SCM_SET_WRITINGP (pstate
, writingp
);
116 scm_putc ('>', port
);
122 scm_make_srcprops (int line
, int col
, SCM filename
, SCM copy
, SCM plist
)
124 register scm_srcprops
*ptr
;
126 if ((ptr
= srcprops_freelist
) != NULL
)
127 srcprops_freelist
= *(scm_srcprops
**)ptr
;
131 scm_srcprops_chunk
*mem
;
132 scm_sizet n
= sizeof (scm_srcprops_chunk
)
133 + sizeof (scm_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
134 SCM_SYSCALL (mem
= (scm_srcprops_chunk
*) malloc (n
));
136 scm_memory_error ("srcprops");
138 mem
->next
= srcprops_chunklist
;
139 srcprops_chunklist
= mem
;
140 ptr
= &mem
->srcprops
[0];
141 for (i
= 1; i
< SRCPROPS_CHUNKSIZE
- 1; ++i
)
142 *(scm_srcprops
**)&ptr
[i
] = &ptr
[i
+ 1];
143 *(scm_srcprops
**)&ptr
[SRCPROPS_CHUNKSIZE
- 1] = 0;
144 srcprops_freelist
= (scm_srcprops
*) &ptr
[1];
146 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
147 ptr
->fname
= filename
;
150 SCM_RETURN_NEWSMOB (scm_tc16_srcprops
, ptr
);
155 scm_srcprops_to_plist (SCM obj
)
157 SCM plist
= SRCPROPPLIST (obj
);
158 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
159 plist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), plist
);
160 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
161 plist
= scm_acons (scm_sym_filename
, SRCPROPFNAME (obj
), plist
);
162 plist
= scm_acons (scm_sym_column
, SCM_MAKINUM (SRCPROPCOL (obj
)), plist
);
163 plist
= scm_acons (scm_sym_line
, SCM_MAKINUM (SRCPROPLINE (obj
)), plist
);
164 plist
= scm_acons (scm_sym_breakpoint
, SRCPROPBRK (obj
), plist
);
168 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
171 #define FUNC_NAME s_scm_source_properties
174 SCM_VALIDATE_NIM (1,obj
);
175 if (SCM_MEMOIZEDP (obj
))
176 obj
= SCM_MEMOIZED_EXP (obj
);
178 else if (SCM_NCONSP (obj
))
179 SCM_WRONG_TYPE_ARG (1, obj
);
181 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_BOOL_F
);
183 return scm_srcprops_to_plist (p
);
188 /* Perhaps this procedure should look through an alist
189 and try to make a srcprops-object...? */
190 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
191 (SCM obj
, SCM plist
),
193 #define FUNC_NAME s_scm_set_source_properties_x
196 SCM_VALIDATE_NIM (1,obj
);
197 if (SCM_MEMOIZEDP (obj
))
198 obj
= SCM_MEMOIZED_EXP (obj
);
200 else if (SCM_NCONSP (obj
))
201 SCM_WRONG_TYPE_ARG(1, obj
);
203 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
204 SCM_SETCDR (handle
, plist
);
209 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
212 #define FUNC_NAME s_scm_source_property
215 SCM_VALIDATE_NIM (1,obj
);
216 if (SCM_MEMOIZEDP (obj
))
217 obj
= SCM_MEMOIZED_EXP (obj
);
219 else if (SCM_NECONSP (obj
))
220 SCM_WRONG_TYPE_ARG (1, obj
);
222 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
223 if (SCM_IMP (p
) || !SRCPROPSP (p
))
225 if (SCM_EQ_P (scm_sym_breakpoint
, key
)) p
= SRCPROPBRK (p
);
226 else if (SCM_EQ_P (scm_sym_line
, key
)) p
= SCM_MAKINUM (SRCPROPLINE (p
));
227 else if (SCM_EQ_P (scm_sym_column
, key
)) p
= SCM_MAKINUM (SRCPROPCOL (p
));
228 else if (SCM_EQ_P (scm_sym_filename
, key
)) p
= SRCPROPFNAME (p
);
229 else if (SCM_EQ_P (scm_sym_copy
, key
)) p
= SRCPROPCOPY (p
);
232 p
= SRCPROPPLIST (p
);
234 p
= scm_assoc (key
, p
);
235 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
237 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
241 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
242 (SCM obj
, SCM key
, SCM datum
),
244 #define FUNC_NAME s_scm_set_source_property_x
248 SCM_VALIDATE_NIM (1,obj
);
249 if (SCM_MEMOIZEDP (obj
))
250 obj
= SCM_MEMOIZED_EXP (obj
);
252 else if (SCM_NCONSP (obj
))
253 SCM_WRONG_TYPE_ARG (1, obj
);
255 h
= scm_whash_get_handle (scm_source_whash
, obj
);
256 if (SCM_WHASHFOUNDP (h
))
257 p
= SCM_WHASHREF (scm_source_whash
, h
);
260 h
= scm_whash_create_handle (scm_source_whash
, obj
);
263 if (SCM_EQ_P (scm_sym_breakpoint
, key
))
267 if (SCM_FALSEP (datum
))
274 SCM sp
= scm_make_srcprops (0, 0, SCM_UNDEFINED
, SCM_UNDEFINED
, p
);
275 SCM_WHASHSET (scm_source_whash
, h
, sp
);
276 if (SCM_FALSEP (datum
))
277 CLEARSRCPROPBRK (sp
);
282 else if (SCM_EQ_P (scm_sym_line
, key
))
284 SCM_VALIDATE_INUM (3,datum
);
286 SETSRCPROPLINE (p
, SCM_INUM (datum
));
288 SCM_WHASHSET (scm_source_whash
, h
,
289 scm_make_srcprops (SCM_INUM (datum
), 0,
290 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
292 else if (SCM_EQ_P (scm_sym_column
, key
))
294 SCM_VALIDATE_INUM (3,datum
);
296 SETSRCPROPCOL (p
, SCM_INUM (datum
));
298 SCM_WHASHSET (scm_source_whash
, h
,
299 scm_make_srcprops (0, SCM_INUM (datum
),
300 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
302 else if (SCM_EQ_P (scm_sym_filename
, key
))
305 SRCPROPFNAME (p
) = datum
;
307 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
309 else if (SCM_EQ_P (scm_sym_copy
, key
))
312 SRCPROPCOPY (p
) = datum
;
314 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
317 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
318 return SCM_UNSPECIFIED
;
326 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
327 scm_set_smob_mark (scm_tc16_srcprops
, srcprops_mark
);
328 scm_set_smob_free (scm_tc16_srcprops
, srcprops_free
);
329 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
331 scm_source_whash
= scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
332 scm_sysintern ("source-whash", scm_source_whash
);
334 #ifndef SCM_MAGIC_SNARFER
335 #include "libguile/srcprop.x"
340 scm_finish_srcprop ()
342 register scm_srcprops_chunk
*ptr
= srcprops_chunklist
, *next
;
347 scm_mallocated
-= sizeof (scm_srcprops_chunk
)
348 + sizeof (scm_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);