Commit | Line | Data |
---|---|---|
2b829bbb | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006 Free Software Foundation |
575888bd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
575888bd | 7 | * |
73be1d9e MV |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
575888bd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
92205699 | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
73be1d9e | 16 | */ |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
575888bd MD |
19 | \f |
20 | ||
e6e2e95a MD |
21 | #include <errno.h> |
22 | ||
a0599745 | 23 | #include "libguile/_scm.h" |
4e047c3e | 24 | #include "libguile/async.h" |
a0599745 MD |
25 | #include "libguile/smob.h" |
26 | #include "libguile/alist.h" | |
27 | #include "libguile/debug.h" | |
28 | #include "libguile/hashtab.h" | |
29 | #include "libguile/hash.h" | |
30 | #include "libguile/ports.h" | |
31 | #include "libguile/root.h" | |
32 | #include "libguile/weaks.h" | |
575888bd | 33 | |
a0599745 MD |
34 | #include "libguile/validate.h" |
35 | #include "libguile/srcprop.h" | |
575888bd MD |
36 | \f |
37 | /* {Source Properties} | |
38 | * | |
39 | * Properties of source list expressions. | |
40 | * Five of these have special meaning and optimized storage: | |
41 | * | |
42 | * filename string The name of the source file. | |
43 | * copy list A copy of the list expression. | |
44 | * line integer The source code line number. | |
45 | * column integer The source code column number. | |
46 | * breakpoint boolean Sets a breakpoint on this form. | |
47 | * | |
48 | * Most properties above can be set by the reader. | |
49 | * | |
50 | */ | |
51 | ||
85db4a2c DH |
52 | SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename"); |
53 | SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy"); | |
54 | SCM_GLOBAL_SYMBOL (scm_sym_line, "line"); | |
55 | SCM_GLOBAL_SYMBOL (scm_sym_column, "column"); | |
56 | SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint"); | |
575888bd | 57 | |
92c2555f MV |
58 | scm_t_bits scm_tc16_srcprops; |
59 | static scm_t_srcprops_chunk *srcprops_chunklist = 0; | |
60 | static scm_t_srcprops *srcprops_freelist = 0; | |
575888bd | 61 | |
1cc91f1b | 62 | |
575888bd | 63 | static SCM |
e841c3e0 | 64 | srcprops_mark (SCM obj) |
575888bd | 65 | { |
575888bd MD |
66 | scm_gc_mark (SRCPROPFNAME (obj)); |
67 | scm_gc_mark (SRCPROPCOPY (obj)); | |
68 | return SRCPROPPLIST (obj); | |
69 | } | |
70 | ||
1cc91f1b | 71 | |
1be6b49c | 72 | static size_t |
e841c3e0 | 73 | srcprops_free (SCM obj) |
575888bd | 74 | { |
f5710d53 MV |
75 | *((scm_t_srcprops **) SCM_SMOB_DATA (obj)) = srcprops_freelist; |
76 | srcprops_freelist = (scm_t_srcprops *) SCM_SMOB_DATA (obj); | |
575888bd MD |
77 | return 0; /* srcprops_chunks are not freed until leaving guile */ |
78 | } | |
79 | ||
1cc91f1b | 80 | |
575888bd | 81 | static int |
e841c3e0 | 82 | srcprops_print (SCM obj, SCM port, scm_print_state *pstate) |
575888bd | 83 | { |
19402679 | 84 | int writingp = SCM_WRITINGP (pstate); |
b7f3516f | 85 | scm_puts ("#<srcprops ", port); |
19402679 | 86 | SCM_SET_WRITINGP (pstate, 1); |
7862b07e | 87 | scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate); |
19402679 | 88 | SCM_SET_WRITINGP (pstate, writingp); |
b7f3516f | 89 | scm_putc ('>', port); |
575888bd MD |
90 | return 1; |
91 | } | |
92 | ||
1cc91f1b | 93 | |
bc76d628 DH |
94 | int |
95 | scm_c_source_property_breakpoint_p (SCM form) | |
96 | { | |
97 | SCM obj = scm_whash_lookup (scm_source_whash, form); | |
98 | return SRCPROPSP (obj) && SRCPROPBRK (obj); | |
99 | } | |
100 | ||
101 | ||
575888bd | 102 | SCM |
1be6b49c | 103 | scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist) |
575888bd | 104 | { |
92c2555f | 105 | register scm_t_srcprops *ptr; |
9de87eea | 106 | SCM_CRITICAL_SECTION_START; |
575888bd | 107 | if ((ptr = srcprops_freelist) != NULL) |
92c2555f | 108 | srcprops_freelist = *(scm_t_srcprops **)ptr; |
575888bd MD |
109 | else |
110 | { | |
1be6b49c | 111 | size_t i; |
92c2555f MV |
112 | scm_t_srcprops_chunk *mem; |
113 | size_t n = sizeof (scm_t_srcprops_chunk) | |
114 | + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1); | |
67329a9e | 115 | SCM_SYSCALL (mem = (scm_t_srcprops_chunk *) scm_malloc (n)); |
2500356c DH |
116 | if (mem == NULL) |
117 | scm_memory_error ("srcprops"); | |
0068984b HWN |
118 | scm_gc_register_collectable_memory (mem, n, "srcprops"); |
119 | ||
575888bd MD |
120 | mem->next = srcprops_chunklist; |
121 | srcprops_chunklist = mem; | |
122 | ptr = &mem->srcprops[0]; | |
123 | for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i) | |
92c2555f MV |
124 | *(scm_t_srcprops **)&ptr[i] = &ptr[i + 1]; |
125 | *(scm_t_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0; | |
126 | srcprops_freelist = (scm_t_srcprops *) &ptr[1]; | |
575888bd | 127 | } |
575888bd MD |
128 | ptr->pos = SRCPROPMAKPOS (line, col); |
129 | ptr->fname = filename; | |
130 | ptr->copy = copy; | |
131 | ptr->plist = plist; | |
9de87eea | 132 | SCM_CRITICAL_SECTION_END; |
23a62151 | 133 | SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr); |
575888bd MD |
134 | } |
135 | ||
1cc91f1b | 136 | |
575888bd | 137 | SCM |
7862b07e | 138 | scm_srcprops_to_plist (SCM obj) |
575888bd MD |
139 | { |
140 | SCM plist = SRCPROPPLIST (obj); | |
141 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) | |
92e5aa0e | 142 | plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist); |
575888bd | 143 | if (!SCM_UNBNDP (SRCPROPFNAME (obj))) |
92e5aa0e | 144 | plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist); |
e11e83f3 MV |
145 | plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist); |
146 | plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist); | |
7888309b | 147 | plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist); |
575888bd MD |
148 | return plist; |
149 | } | |
150 | ||
a1ec6916 | 151 | SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, |
1bbd0b84 | 152 | (SCM obj), |
e3239868 | 153 | "Return the source property association list of @var{obj}.") |
1bbd0b84 | 154 | #define FUNC_NAME s_scm_source_properties |
575888bd MD |
155 | { |
156 | SCM p; | |
34d19ef6 | 157 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 158 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 159 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 160 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 161 | SCM_WRONG_TYPE_ARG (1, obj); |
f5003c13 | 162 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
230d095f | 163 | if (SRCPROPSP (p)) |
7862b07e | 164 | return scm_srcprops_to_plist (p); |
f5003c13 KR |
165 | else |
166 | /* list from set-source-properties!, or SCM_EOL for not found */ | |
167 | return p; | |
575888bd | 168 | } |
1bbd0b84 | 169 | #undef FUNC_NAME |
575888bd MD |
170 | |
171 | /* Perhaps this procedure should look through an alist | |
172 | and try to make a srcprops-object...? */ | |
a1ec6916 | 173 | SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0, |
1bbd0b84 | 174 | (SCM obj, SCM plist), |
e3239868 DH |
175 | "Install the association list @var{plist} as the source property\n" |
176 | "list for @var{obj}.") | |
1bbd0b84 | 177 | #define FUNC_NAME s_scm_set_source_properties_x |
575888bd MD |
178 | { |
179 | SCM handle; | |
34d19ef6 | 180 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 181 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 182 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 183 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 184 | SCM_WRONG_TYPE_ARG(1, obj); |
575888bd MD |
185 | handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist); |
186 | SCM_SETCDR (handle, plist); | |
187 | return plist; | |
188 | } | |
1bbd0b84 | 189 | #undef FUNC_NAME |
575888bd | 190 | |
a1ec6916 | 191 | SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0, |
1bbd0b84 | 192 | (SCM obj, SCM key), |
e3239868 DH |
193 | "Return the source property specified by @var{key} from\n" |
194 | "@var{obj}'s source property list.") | |
1bbd0b84 | 195 | #define FUNC_NAME s_scm_source_property |
575888bd MD |
196 | { |
197 | SCM p; | |
34d19ef6 | 198 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 199 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 200 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 201 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 202 | SCM_WRONG_TYPE_ARG (1, obj); |
575888bd | 203 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
24933780 | 204 | if (!SRCPROPSP (p)) |
575888bd | 205 | goto plist; |
bc36d050 MV |
206 | if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p)); |
207 | else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p)); | |
208 | else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p)); | |
209 | else if (scm_is_eq (scm_sym_filename, key)) p = SRCPROPFNAME (p); | |
210 | else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p); | |
575888bd MD |
211 | else |
212 | { | |
213 | p = SRCPROPPLIST (p); | |
214 | plist: | |
215 | p = scm_assoc (key, p); | |
216 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); | |
217 | } | |
218 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; | |
219 | } | |
1bbd0b84 | 220 | #undef FUNC_NAME |
575888bd | 221 | |
a1ec6916 | 222 | SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0, |
1bbd0b84 | 223 | (SCM obj, SCM key, SCM datum), |
e3239868 DH |
224 | "Set the source property of object @var{obj}, which is specified by\n" |
225 | "@var{key} to @var{datum}. Normally, the key will be a symbol.") | |
1bbd0b84 | 226 | #define FUNC_NAME s_scm_set_source_property_x |
575888bd MD |
227 | { |
228 | scm_whash_handle h; | |
229 | SCM p; | |
34d19ef6 | 230 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 231 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 232 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 233 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 234 | SCM_WRONG_TYPE_ARG (1, obj); |
575888bd MD |
235 | h = scm_whash_get_handle (scm_source_whash, obj); |
236 | if (SCM_WHASHFOUNDP (h)) | |
237 | p = SCM_WHASHREF (scm_source_whash, h); | |
238 | else | |
239 | { | |
240 | h = scm_whash_create_handle (scm_source_whash, obj); | |
241 | p = SCM_EOL; | |
242 | } | |
bc36d050 | 243 | if (scm_is_eq (scm_sym_breakpoint, key)) |
cda139a7 | 244 | { |
62850ef3 DH |
245 | if (SRCPROPSP (p)) |
246 | { | |
7888309b | 247 | if (scm_is_false (datum)) |
62850ef3 DH |
248 | CLEARSRCPROPBRK (p); |
249 | else | |
250 | SETSRCPROPBRK (p); | |
251 | } | |
cda139a7 | 252 | else |
62850ef3 DH |
253 | { |
254 | SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p); | |
255 | SCM_WHASHSET (scm_source_whash, h, sp); | |
7888309b | 256 | if (scm_is_false (datum)) |
62850ef3 DH |
257 | CLEARSRCPROPBRK (sp); |
258 | else | |
259 | SETSRCPROPBRK (sp); | |
260 | } | |
cda139a7 | 261 | } |
bc36d050 | 262 | else if (scm_is_eq (scm_sym_line, key)) |
575888bd | 263 | { |
0c95b57d | 264 | if (SRCPROPSP (p)) |
a55c2b68 | 265 | SETSRCPROPLINE (p, scm_to_int (datum)); |
575888bd MD |
266 | else |
267 | SCM_WHASHSET (scm_source_whash, h, | |
a55c2b68 | 268 | scm_make_srcprops (scm_to_int (datum), 0, |
a9dbb9fd | 269 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd | 270 | } |
bc36d050 | 271 | else if (scm_is_eq (scm_sym_column, key)) |
575888bd | 272 | { |
0c95b57d | 273 | if (SRCPROPSP (p)) |
a55c2b68 | 274 | SETSRCPROPCOL (p, scm_to_int (datum)); |
575888bd MD |
275 | else |
276 | SCM_WHASHSET (scm_source_whash, h, | |
a55c2b68 | 277 | scm_make_srcprops (0, scm_to_int (datum), |
a9dbb9fd | 278 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd | 279 | } |
bc36d050 | 280 | else if (scm_is_eq (scm_sym_filename, key)) |
575888bd | 281 | { |
0c95b57d | 282 | if (SRCPROPSP (p)) |
575888bd MD |
283 | SRCPROPFNAME (p) = datum; |
284 | else | |
5c5549cb | 285 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p)); |
575888bd | 286 | } |
bc36d050 | 287 | else if (scm_is_eq (scm_sym_copy, key)) |
575888bd | 288 | { |
0c95b57d | 289 | if (SRCPROPSP (p)) |
575888bd MD |
290 | SRCPROPCOPY (p) = datum; |
291 | else | |
5c5549cb | 292 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
575888bd MD |
293 | } |
294 | else | |
58d233cc NJ |
295 | { |
296 | if (SRCPROPSP (p)) | |
297 | SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p)); | |
298 | else | |
299 | SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p)); | |
300 | } | |
575888bd MD |
301 | return SCM_UNSPECIFIED; |
302 | } | |
1bbd0b84 | 303 | #undef FUNC_NAME |
575888bd | 304 | |
1cc91f1b | 305 | |
575888bd MD |
306 | void |
307 | scm_init_srcprop () | |
575888bd | 308 | { |
e841c3e0 KN |
309 | scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); |
310 | scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark); | |
311 | scm_set_smob_free (scm_tc16_srcprops, srcprops_free); | |
312 | scm_set_smob_print (scm_tc16_srcprops, srcprops_print); | |
313 | ||
e11e83f3 | 314 | scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047)); |
86d31dfe | 315 | scm_c_define ("source-whash", scm_source_whash); |
85db4a2c | 316 | |
a0599745 | 317 | #include "libguile/srcprop.x" |
575888bd MD |
318 | } |
319 | ||
320 | void | |
321 | scm_finish_srcprop () | |
322 | { | |
92c2555f | 323 | register scm_t_srcprops_chunk *ptr = srcprops_chunklist, *next; |
0068984b HWN |
324 | size_t n= sizeof (scm_t_srcprops_chunk) |
325 | + sizeof (scm_t_srcprops) * (SRCPROPS_CHUNKSIZE - 1); | |
575888bd MD |
326 | while (ptr) |
327 | { | |
328 | next = ptr->next; | |
0068984b | 329 | scm_gc_unregister_collectable_memory (ptr, n, "srcprops"); |
575888bd | 330 | free ((char *) ptr); |
5c5549cb | 331 | ptr = next; |
575888bd MD |
332 | } |
333 | } | |
89e00824 ML |
334 | |
335 | /* | |
336 | Local Variables: | |
337 | c-file-style: "gnu" | |
338 | End: | |
339 | */ |