Commit | Line | Data |
---|---|---|
575888bd MD |
1 | /* Copyright (C) 1995,1996 Mikael Djurfeldt |
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, 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. | |
40 | * | |
41 | * The author can be reached at djurfeldt@nada.kth.se | |
42 | * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN | |
43 | */ | |
44 | \f | |
45 | ||
46 | #include <stdio.h> | |
47 | #include "_scm.h" | |
20e6290e JB |
48 | #include "smob.h" |
49 | #include "alist.h" | |
50 | #include "debug.h" | |
51 | #include "hashtab.h" | |
5c5549cb | 52 | #include "hash.h" |
20e6290e | 53 | #include "weaks.h" |
575888bd | 54 | |
20e6290e | 55 | #include "srcprop.h" |
575888bd MD |
56 | \f |
57 | /* {Source Properties} | |
58 | * | |
59 | * Properties of source list expressions. | |
60 | * Five of these have special meaning and optimized storage: | |
61 | * | |
62 | * filename string The name of the source file. | |
63 | * copy list A copy of the list expression. | |
64 | * line integer The source code line number. | |
65 | * column integer The source code column number. | |
66 | * breakpoint boolean Sets a breakpoint on this form. | |
67 | * | |
68 | * Most properties above can be set by the reader. | |
69 | * | |
70 | */ | |
71 | ||
72 | SCM scm_i_copy; | |
73 | static SCM scm_i_breakpoint, scm_i_line, scm_i_column; | |
74 | static SCM scm_i_filename; | |
75 | ||
5c5549cb | 76 | long scm_tc16_srcprops; |
575888bd MD |
77 | static scm_srcprops_chunk *srcprops_chunklist = 0; |
78 | static scm_srcprops *srcprops_freelist = 0; | |
79 | ||
80 | #ifdef __STDC__ | |
81 | static SCM | |
82 | marksrcprops (SCM obj) | |
83 | #else | |
84 | static SCM | |
85 | marksrcprops (obj) | |
86 | SCM obj; | |
87 | #endif | |
88 | { | |
89 | SCM_SETGC8MARK (obj); | |
90 | scm_gc_mark (SRCPROPFNAME (obj)); | |
91 | scm_gc_mark (SRCPROPCOPY (obj)); | |
92 | return SRCPROPPLIST (obj); | |
93 | } | |
94 | ||
95 | #ifdef __STDC__ | |
96 | static scm_sizet | |
97 | freesrcprops (SCM obj) | |
98 | #else | |
99 | static scm_sizet | |
100 | freesrcprops (obj) | |
101 | SCM obj; | |
102 | #endif | |
103 | { | |
104 | *((scm_srcprops **) SCM_CDR (obj)) = srcprops_freelist; | |
105 | srcprops_freelist = (scm_srcprops *) SCM_CDR (obj); | |
106 | return 0; /* srcprops_chunks are not freed until leaving guile */ | |
107 | } | |
108 | ||
109 | #ifdef __STDC__ | |
110 | static int | |
111 | prinsrcprops (SCM obj, SCM port, int writing) | |
112 | #else | |
113 | static int | |
114 | prinsrcprops (obj, port, writing) | |
115 | SCM obj; | |
116 | SCM port; | |
117 | int writing; | |
118 | #endif | |
119 | { | |
120 | scm_gen_puts (scm_regular_string, "#<srcprops ", port); | |
121 | scm_iprin1 (scm_srcprops_to_plist (obj), port, 1); | |
122 | scm_gen_putc ('>', port); | |
123 | return 1; | |
124 | } | |
125 | ||
126 | static scm_smobfuns srcpropssmob = | |
127 | {marksrcprops, freesrcprops, prinsrcprops, 0}; | |
128 | ||
129 | #ifdef __STDC__ | |
130 | SCM | |
5c5549cb | 131 | scm_make_srcprops (int line, int col, SCM filename, SCM copy, SCM plist) |
575888bd MD |
132 | #else |
133 | SCM | |
5c5549cb | 134 | scm_make_srcprops (line, col, filename, copy, plist) |
575888bd MD |
135 | int line; |
136 | int col; | |
137 | SCM filename; | |
138 | SCM copy; | |
139 | SCM plist; | |
140 | #endif | |
141 | { | |
142 | register SCM ans; | |
143 | register scm_srcprops *ptr; | |
144 | SCM_DEFER_INTS; | |
145 | if ((ptr = srcprops_freelist) != NULL) | |
146 | srcprops_freelist = *(scm_srcprops **)ptr; | |
147 | else | |
148 | { | |
149 | int i; | |
150 | scm_srcprops_chunk *mem; | |
151 | scm_sizet n = sizeof (scm_srcprops_chunk) | |
152 | + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1); | |
153 | SCM_SYSCALL (mem = (scm_srcprops_chunk *) malloc (n)); | |
154 | SCM_ASSERT (mem, SCM_UNDEFINED, SCM_NALLOC, "srcprops"); | |
155 | scm_mallocated += n; | |
156 | mem->next = srcprops_chunklist; | |
157 | srcprops_chunklist = mem; | |
158 | ptr = &mem->srcprops[0]; | |
159 | for (i = 1; i < SRCPROPS_CHUNKSIZE - 1; ++i) | |
160 | *(scm_srcprops **)&ptr[i] = &ptr[i + 1]; | |
161 | *(scm_srcprops **)&ptr[SRCPROPS_CHUNKSIZE - 1] = 0; | |
162 | srcprops_freelist = (scm_srcprops *) &ptr[1]; | |
163 | } | |
164 | SCM_NEWCELL (ans); | |
5c5549cb | 165 | SCM_CAR (ans) = scm_tc16_srcprops; |
575888bd MD |
166 | ptr->pos = SRCPROPMAKPOS (line, col); |
167 | ptr->fname = filename; | |
168 | ptr->copy = copy; | |
169 | ptr->plist = plist; | |
170 | SCM_CDR (ans) = (SCM) ptr; | |
171 | SCM_ALLOW_INTS; | |
172 | return ans; | |
173 | } | |
174 | ||
175 | #ifdef __STDC__ | |
176 | SCM | |
177 | scm_srcprops_to_plist (SCM obj) | |
178 | #else | |
179 | SCM | |
180 | scm_srcprops_to_plist (obj) | |
181 | SCM obj; | |
182 | #endif | |
183 | { | |
184 | SCM plist = SRCPROPPLIST (obj); | |
185 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) | |
186 | plist = scm_acons (scm_i_copy, SRCPROPCOPY (obj), plist); | |
187 | if (!SCM_UNBNDP (SRCPROPFNAME (obj))) | |
188 | plist = scm_acons (scm_i_filename, SRCPROPFNAME (obj), plist); | |
189 | plist = scm_acons (scm_i_column, SCM_MAKINUM (SRCPROPCOL (obj)), plist); | |
190 | plist = scm_acons (scm_i_line, SCM_MAKINUM (SRCPROPLINE (obj)), plist); | |
191 | plist = scm_acons (scm_i_breakpoint, SRCPROPBRK (obj), plist); | |
192 | return plist; | |
193 | } | |
194 | ||
195 | SCM_PROC (s_source_properties, "source-properties", 1, 0, 0, scm_source_properties); | |
196 | #ifdef __STDC__ | |
197 | SCM | |
198 | scm_source_properties (SCM obj) | |
199 | #else | |
200 | SCM | |
201 | scm_source_properties (obj) | |
202 | SCM obj; | |
203 | #endif | |
204 | { | |
205 | SCM p; | |
206 | if (SCM_MEMOIZEDP (obj)) | |
207 | obj = SCM_MEMOEXP (obj); | |
208 | p = scm_hashq_ref (scm_source_whash, obj, (SCM) NULL); | |
209 | if (p != (SCM) NULL && SRCPROPSP (p)) | |
210 | return scm_srcprops_to_plist (p); | |
211 | return SCM_EOL; | |
212 | } | |
213 | ||
214 | /* Perhaps this procedure should look through an alist | |
215 | and try to make a srcprops-object...? */ | |
216 | SCM_PROC (s_set_source_properties_x, "set-source-properties!", 2, 0, 0, scm_set_source_properties_x); | |
217 | #ifdef __STDC__ | |
218 | SCM | |
219 | scm_set_source_properties_x (SCM obj, SCM plist) | |
220 | #else | |
221 | SCM | |
222 | scm_set_source_properties_x (obj, plist) | |
223 | SCM obj; | |
224 | SCM plist; | |
225 | #endif | |
226 | { | |
227 | SCM handle; | |
228 | if (SCM_MEMOIZEDP (obj)) | |
229 | obj = SCM_MEMOEXP (obj); | |
230 | handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist); | |
231 | SCM_SETCDR (handle, plist); | |
232 | return plist; | |
233 | } | |
234 | ||
235 | SCM_PROC (s_source_property, "source-property", 2, 0, 0, scm_source_property); | |
236 | #ifdef __STDC__ | |
237 | SCM | |
238 | scm_source_property (SCM obj, SCM key) | |
239 | #else | |
240 | SCM | |
241 | scm_source_property (obj, key) | |
242 | SCM obj; | |
243 | SCM key; | |
244 | #endif | |
245 | { | |
246 | SCM p; | |
247 | if (SCM_MEMOIZEDP (obj)) | |
248 | obj = SCM_MEMOEXP (obj); | |
249 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); | |
250 | if (SCM_IMP (p) || !SRCPROPSP (p)) | |
251 | goto plist; | |
252 | if (scm_i_breakpoint == key) p = SRCPROPBRK (p); | |
253 | else if (scm_i_line == key) p = SCM_MAKINUM (SRCPROPLINE (p)); | |
254 | else if (scm_i_column == key) p = SCM_MAKINUM (SRCPROPCOL (p)); | |
255 | else if (scm_i_filename == key) p = SRCPROPFNAME (p); | |
256 | else if (scm_i_copy == key) p = SRCPROPCOPY (p); | |
257 | else | |
258 | { | |
259 | p = SRCPROPPLIST (p); | |
260 | plist: | |
261 | p = scm_assoc (key, p); | |
262 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); | |
263 | } | |
264 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; | |
265 | } | |
266 | ||
267 | SCM_PROC (s_set_source_property_x, "set-source-property!", 3, 0, 0, scm_set_source_property_x); | |
268 | #ifdef __STDC__ | |
269 | SCM | |
270 | scm_set_source_property_x (SCM obj, SCM key, SCM datum) | |
271 | #else | |
272 | SCM | |
273 | scm_set_source_property_x (obj, key, datum) | |
274 | SCM obj; | |
275 | SCM key; | |
276 | SCM datum; | |
277 | #endif | |
278 | { | |
279 | scm_whash_handle h; | |
280 | SCM p; | |
281 | if (SCM_MEMOIZEDP (obj)) | |
282 | obj = SCM_MEMOEXP (obj); | |
283 | h = scm_whash_get_handle (scm_source_whash, obj); | |
284 | if (SCM_WHASHFOUNDP (h)) | |
285 | p = SCM_WHASHREF (scm_source_whash, h); | |
286 | else | |
287 | { | |
288 | h = scm_whash_create_handle (scm_source_whash, obj); | |
289 | p = SCM_EOL; | |
290 | } | |
291 | if (scm_i_breakpoint == key) | |
292 | if (SCM_FALSEP (datum)) | |
293 | CLEARSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p) | |
294 | ? p | |
295 | : SCM_WHASHSET (scm_source_whash, h, | |
5c5549cb | 296 | scm_make_srcprops (0, 0, SCM_UNDEFINED, |
575888bd MD |
297 | SCM_UNDEFINED, p))); |
298 | else | |
299 | SETSRCPROPBRK (SCM_NIMP (p) && SRCPROPSP (p) | |
300 | ? p | |
301 | : SCM_WHASHSET (scm_source_whash, h, | |
5c5549cb | 302 | scm_make_srcprops (0, 0, SCM_UNDEFINED, |
575888bd MD |
303 | SCM_UNDEFINED, p))); |
304 | else if (scm_i_line == key) | |
305 | { | |
306 | if (SCM_NIMP (p) && SRCPROPSP (p)) | |
307 | SETSRCPROPLINE (p, datum); | |
308 | else | |
309 | SCM_WHASHSET (scm_source_whash, h, | |
5c5549cb | 310 | scm_make_srcprops (datum, 0, SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd MD |
311 | } |
312 | else if (scm_i_column == key) | |
313 | { | |
314 | if (SCM_NIMP (p) && SRCPROPSP (p)) | |
315 | SETSRCPROPCOL (p, datum); | |
316 | else | |
317 | SCM_WHASHSET (scm_source_whash, h, | |
5c5549cb | 318 | scm_make_srcprops (0, datum, SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd MD |
319 | } |
320 | else if (scm_i_filename == key) | |
321 | { | |
322 | if (SCM_NIMP (p) && SRCPROPSP (p)) | |
323 | SRCPROPFNAME (p) = datum; | |
324 | else | |
5c5549cb | 325 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p)); |
575888bd MD |
326 | } |
327 | else if (scm_i_filename == key) | |
328 | { | |
329 | if (SCM_NIMP (p) && SRCPROPSP (p)) | |
330 | SRCPROPCOPY (p) = datum; | |
331 | else | |
5c5549cb | 332 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
575888bd MD |
333 | } |
334 | else | |
335 | SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p)); | |
336 | return SCM_UNSPECIFIED; | |
337 | } | |
338 | ||
339 | #ifdef __STDC__ | |
340 | void | |
341 | scm_init_srcprop (void) | |
342 | #else | |
343 | void | |
344 | scm_init_srcprop () | |
345 | #endif | |
346 | { | |
5c5549cb MD |
347 | scm_tc16_srcprops = scm_newsmob (&srcpropssmob); |
348 | scm_source_whash = scm_make_weak_key_hash_table (SCM_MAKINUM (2047)); | |
575888bd MD |
349 | |
350 | scm_i_filename = SCM_CAR (scm_sysintern ("filename", SCM_UNDEFINED)); | |
351 | scm_i_copy = SCM_CAR (scm_sysintern ("copy", SCM_UNDEFINED)); | |
352 | scm_i_line = SCM_CAR (scm_sysintern ("line", SCM_UNDEFINED)); | |
353 | scm_i_column = SCM_CAR (scm_sysintern ("column", SCM_UNDEFINED)); | |
354 | scm_i_breakpoint = SCM_CAR (scm_sysintern ("breakpoint", SCM_UNDEFINED)); | |
355 | ||
356 | scm_sysintern ("source-whash", scm_source_whash); | |
357 | #include "srcprop.x" | |
358 | } | |
359 | ||
360 | void | |
361 | scm_finish_srcprop () | |
362 | { | |
363 | register scm_srcprops_chunk *ptr = srcprops_chunklist, *next; | |
364 | while (ptr) | |
365 | { | |
366 | next = ptr->next; | |
367 | free ((char *) ptr); | |
368 | scm_mallocated -= sizeof (scm_srcprops_chunk) | |
369 | + sizeof (scm_srcprops) * (SRCPROPS_CHUNKSIZE - 1); | |
5c5549cb | 370 | ptr = next; |
575888bd MD |
371 | } |
372 | } |