Merge commit 'f30e1bdf97ae8b2b2918da585f887a4d3a23a347' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / srcprop.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006 Free Software Foundation
2 *
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.
7 *
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.
12 *
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18
19 \f
20
21 #include <errno.h>
22
23 #include "libguile/_scm.h"
24 #include "libguile/async.h"
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"
33 #include "libguile/gc.h"
34
35 #include "libguile/validate.h"
36 #include "libguile/srcprop.h"
37 \f
38 /* {Source Properties}
39 *
40 * Properties of source list expressions.
41 * Five of these have special meaning and optimized storage:
42 *
43 * filename string The name of the source file.
44 * copy list A copy of the list expression.
45 * line integer The source code line number.
46 * column integer The source code column number.
47 * breakpoint boolean Sets a breakpoint on this form.
48 *
49 * Most properties above can be set by the reader.
50 *
51 */
52
53 SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
54 SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
55 SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
56 SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
57 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
58
59 scm_t_bits scm_tc16_srcprops;
60
61
62 static int
63 srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
64 {
65 int writingp = SCM_WRITINGP (pstate);
66 scm_puts ("#<srcprops ", port);
67 SCM_SET_WRITINGP (pstate, 1);
68 scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate);
69 SCM_SET_WRITINGP (pstate, writingp);
70 scm_putc ('>', port);
71 return 1;
72 }
73
74
75 int
76 scm_c_source_property_breakpoint_p (SCM form)
77 {
78 SCM obj = scm_whash_lookup (scm_source_whash, form);
79 return SRCPROPSP (obj) && SRCPROPBRK (obj);
80 }
81
82
83 SCM
84 scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist)
85 {
86 register scm_t_srcprops *ptr;
87
88 ptr = scm_gc_malloc (sizeof (*ptr), "srcprop");
89
90 ptr->pos = SRCPROPMAKPOS (line, col);
91 ptr->fname = filename;
92 ptr->copy = copy;
93 ptr->plist = plist;
94
95 SCM_RETURN_NEWSMOB (scm_tc16_srcprops, ptr);
96 }
97
98
99 SCM
100 scm_srcprops_to_plist (SCM obj)
101 {
102 SCM plist = SRCPROPPLIST (obj);
103 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
104 plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist);
105 if (!SCM_UNBNDP (SRCPROPFNAME (obj)))
106 plist = scm_acons (scm_sym_filename, SRCPROPFNAME (obj), plist);
107 plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist);
108 plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist);
109 plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist);
110 return plist;
111 }
112
113 SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
114 (SCM obj),
115 "Return the source property association list of @var{obj}.")
116 #define FUNC_NAME s_scm_source_properties
117 {
118 SCM p;
119 SCM_VALIDATE_NIM (1, obj);
120 if (SCM_MEMOIZEDP (obj))
121 obj = SCM_MEMOIZED_EXP (obj);
122 else if (!scm_is_pair (obj))
123 SCM_WRONG_TYPE_ARG (1, obj);
124 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
125 if (SRCPROPSP (p))
126 return scm_srcprops_to_plist (p);
127 else
128 /* list from set-source-properties!, or SCM_EOL for not found */
129 return p;
130 }
131 #undef FUNC_NAME
132
133 /* Perhaps this procedure should look through an alist
134 and try to make a srcprops-object...? */
135 SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
136 (SCM obj, SCM plist),
137 "Install the association list @var{plist} as the source property\n"
138 "list for @var{obj}.")
139 #define FUNC_NAME s_scm_set_source_properties_x
140 {
141 SCM handle;
142 SCM_VALIDATE_NIM (1, obj);
143 if (SCM_MEMOIZEDP (obj))
144 obj = SCM_MEMOIZED_EXP (obj);
145 else if (!scm_is_pair (obj))
146 SCM_WRONG_TYPE_ARG(1, obj);
147 handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist);
148
149 return plist;
150 }
151 #undef FUNC_NAME
152
153 SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
154 (SCM obj, SCM key),
155 "Return the source property specified by @var{key} from\n"
156 "@var{obj}'s source property list.")
157 #define FUNC_NAME s_scm_source_property
158 {
159 SCM p;
160 SCM_VALIDATE_NIM (1, obj);
161 if (SCM_MEMOIZEDP (obj))
162 obj = SCM_MEMOIZED_EXP (obj);
163 else if (!scm_is_pair (obj))
164 SCM_WRONG_TYPE_ARG (1, obj);
165 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
166 if (!SRCPROPSP (p))
167 goto plist;
168 if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p));
169 else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p));
170 else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p));
171 else if (scm_is_eq (scm_sym_filename, key)) p = SRCPROPFNAME (p);
172 else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p);
173 else
174 {
175 p = SRCPROPPLIST (p);
176 plist:
177 p = scm_assoc (key, p);
178 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
179 }
180 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
181 }
182 #undef FUNC_NAME
183
184 SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
185 (SCM obj, SCM key, SCM datum),
186 "Set the source property of object @var{obj}, which is specified by\n"
187 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
188 #define FUNC_NAME s_scm_set_source_property_x
189 {
190 scm_whash_handle h;
191 SCM p;
192 SCM_VALIDATE_NIM (1, obj);
193 if (SCM_MEMOIZEDP (obj))
194 obj = SCM_MEMOIZED_EXP (obj);
195 else if (!scm_is_pair (obj))
196 SCM_WRONG_TYPE_ARG (1, obj);
197 h = scm_whash_get_handle (scm_source_whash, obj);
198 if (SCM_WHASHFOUNDP (h))
199 p = SCM_WHASHREF (scm_source_whash, h);
200 else
201 {
202 h = scm_whash_create_handle (scm_source_whash, obj);
203 p = SCM_EOL;
204 }
205 if (scm_is_eq (scm_sym_breakpoint, key))
206 {
207 if (SRCPROPSP (p))
208 {
209 if (scm_is_false (datum))
210 CLEARSRCPROPBRK (p);
211 else
212 SETSRCPROPBRK (p);
213 }
214 else
215 {
216 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
217 SCM_WHASHSET (scm_source_whash, h, sp);
218 if (scm_is_false (datum))
219 CLEARSRCPROPBRK (sp);
220 else
221 SETSRCPROPBRK (sp);
222 }
223 }
224 else if (scm_is_eq (scm_sym_line, key))
225 {
226 if (SRCPROPSP (p))
227 SETSRCPROPLINE (p, scm_to_int (datum));
228 else
229 SCM_WHASHSET (scm_source_whash, h,
230 scm_make_srcprops (scm_to_int (datum), 0,
231 SCM_UNDEFINED, SCM_UNDEFINED, p));
232 }
233 else if (scm_is_eq (scm_sym_column, key))
234 {
235 if (SRCPROPSP (p))
236 SETSRCPROPCOL (p, scm_to_int (datum));
237 else
238 SCM_WHASHSET (scm_source_whash, h,
239 scm_make_srcprops (0, scm_to_int (datum),
240 SCM_UNDEFINED, SCM_UNDEFINED, p));
241 }
242 else if (scm_is_eq (scm_sym_filename, key))
243 {
244 if (SRCPROPSP (p))
245 SRCPROPFNAME (p) = datum;
246 else
247 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, datum, SCM_UNDEFINED, p));
248 }
249 else if (scm_is_eq (scm_sym_copy, key))
250 {
251 if (SRCPROPSP (p))
252 SRCPROPCOPY (p) = datum;
253 else
254 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
255 }
256 else
257 {
258 if (SRCPROPSP (p))
259 SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p));
260 else
261 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
262 }
263 return SCM_UNSPECIFIED;
264 }
265 #undef FUNC_NAME
266
267
268 void
269 scm_init_srcprop ()
270 {
271 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
272 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
273
274 scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
275 scm_c_define ("source-whash", scm_source_whash);
276
277 #include "libguile/srcprop.x"
278 }
279
280 void
281 scm_finish_srcprop ()
282 {
283 /* Nothing to do. */
284 }
285
286 /*
287 Local Variables:
288 c-file-style: "gnu"
289 End:
290 */