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. | |
b0763985 | 40 | * Five of these have special meaning: |
575888bd MD |
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 | |
575888bd | 58 | |
1cc91f1b | 59 | |
b0763985 HWN |
60 | /* |
61 | layout: | |
62 | ||
63 | car = tag | |
64 | cbr = pos | |
65 | ccr = copy | |
66 | cdr = plist | |
67 | */ | |
68 | ||
69 | #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p))) | |
70 | #define SRCPROPBRK(p) (SCM_SMOB_FLAGS (p) & SCM_SOURCE_PROPERTY_FLAG_BREAK) | |
71 | #define SRCPROPPOS(p) (SCM_CELL_WORD(p,1)) | |
72 | #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12) | |
73 | #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL) | |
74 | #define SRCPROPCOPY(p) (SCM_CELL_OBJECT(p,2)) | |
75 | #define SRCPROPPLIST(p) (SCM_CELL_OBJECT_3(p)) | |
76 | #define SETSRCPROPBRK(p) \ | |
77 | (SCM_SET_SMOB_FLAGS ((p), \ | |
78 | SCM_SMOB_FLAGS (p) | SCM_SOURCE_PROPERTY_FLAG_BREAK)) | |
79 | #define CLEARSRCPROPBRK(p) \ | |
80 | (SCM_SET_SMOB_FLAGS ((p), \ | |
81 | SCM_SMOB_FLAGS (p) & ~SCM_SOURCE_PROPERTY_FLAG_BREAK)) | |
82 | #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c)) | |
83 | #define SETSRCPROPPOS(p, l, c) (SCM_SET_CELL_WORD(p,1, SRCPROPMAKPOS (l, c))) | |
84 | #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p)) | |
85 | #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c) | |
86 | ||
87 | ||
88 | ||
89 | scm_t_bits scm_tc16_srcprops; | |
90 | ||
575888bd | 91 | static SCM |
e841c3e0 | 92 | srcprops_mark (SCM obj) |
575888bd | 93 | { |
575888bd MD |
94 | scm_gc_mark (SRCPROPCOPY (obj)); |
95 | return SRCPROPPLIST (obj); | |
96 | } | |
97 | ||
575888bd | 98 | static int |
e841c3e0 | 99 | srcprops_print (SCM obj, SCM port, scm_print_state *pstate) |
575888bd | 100 | { |
19402679 | 101 | int writingp = SCM_WRITINGP (pstate); |
b7f3516f | 102 | scm_puts ("#<srcprops ", port); |
19402679 | 103 | SCM_SET_WRITINGP (pstate, 1); |
7862b07e | 104 | scm_iprin1 (scm_srcprops_to_plist (obj), port, pstate); |
19402679 | 105 | SCM_SET_WRITINGP (pstate, writingp); |
b7f3516f | 106 | scm_putc ('>', port); |
575888bd MD |
107 | return 1; |
108 | } | |
109 | ||
1cc91f1b | 110 | |
bc76d628 DH |
111 | int |
112 | scm_c_source_property_breakpoint_p (SCM form) | |
113 | { | |
114 | SCM obj = scm_whash_lookup (scm_source_whash, form); | |
115 | return SRCPROPSP (obj) && SRCPROPBRK (obj); | |
116 | } | |
117 | ||
118 | ||
b0763985 HWN |
119 | /* |
120 | A protected cells whose cdr contains the last plist | |
121 | used if plist contains only the filename. | |
122 | ||
123 | This works because scm_set_source_property_x does | |
124 | not use assoc-set! for modifying the plist. | |
125 | */ | |
126 | static SCM scm_last_plist_filename; | |
127 | ||
575888bd | 128 | SCM |
1be6b49c | 129 | scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM plist) |
575888bd | 130 | { |
b0763985 | 131 | if (!SCM_UNBNDP (filename)) |
575888bd | 132 | { |
b0763985 HWN |
133 | SCM old_plist = plist; |
134 | ||
135 | /* | |
136 | have to extract the acons, and operate on that, for | |
137 | thread safety. | |
138 | */ | |
139 | SCM last_acons = SCM_CDR (scm_last_plist_filename); | |
140 | if (old_plist == SCM_EOL | |
141 | && SCM_CDAR (last_acons) == filename) | |
142 | { | |
143 | plist = last_acons; | |
144 | } | |
145 | else | |
146 | { | |
147 | plist = scm_acons (scm_sym_filename, filename, plist); | |
148 | if (old_plist == SCM_EOL) | |
149 | SCM_SETCDR (scm_last_plist_filename, plist); | |
150 | } | |
575888bd | 151 | } |
b0763985 HWN |
152 | |
153 | SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops, | |
154 | SRCPROPMAKPOS (line, col), | |
155 | copy, | |
156 | plist); | |
575888bd MD |
157 | } |
158 | ||
1cc91f1b | 159 | |
575888bd | 160 | SCM |
7862b07e | 161 | scm_srcprops_to_plist (SCM obj) |
575888bd MD |
162 | { |
163 | SCM plist = SRCPROPPLIST (obj); | |
164 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) | |
92e5aa0e | 165 | plist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), plist); |
e11e83f3 MV |
166 | plist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), plist); |
167 | plist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), plist); | |
7888309b | 168 | plist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), plist); |
575888bd MD |
169 | return plist; |
170 | } | |
171 | ||
a1ec6916 | 172 | SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, |
1bbd0b84 | 173 | (SCM obj), |
e3239868 | 174 | "Return the source property association list of @var{obj}.") |
1bbd0b84 | 175 | #define FUNC_NAME s_scm_source_properties |
575888bd MD |
176 | { |
177 | SCM p; | |
34d19ef6 | 178 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 179 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 180 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 181 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 182 | SCM_WRONG_TYPE_ARG (1, obj); |
f5003c13 | 183 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
230d095f | 184 | if (SRCPROPSP (p)) |
7862b07e | 185 | return scm_srcprops_to_plist (p); |
f5003c13 KR |
186 | else |
187 | /* list from set-source-properties!, or SCM_EOL for not found */ | |
188 | return p; | |
575888bd | 189 | } |
1bbd0b84 | 190 | #undef FUNC_NAME |
575888bd MD |
191 | |
192 | /* Perhaps this procedure should look through an alist | |
193 | and try to make a srcprops-object...? */ | |
a1ec6916 | 194 | SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0, |
1bbd0b84 | 195 | (SCM obj, SCM plist), |
e3239868 DH |
196 | "Install the association list @var{plist} as the source property\n" |
197 | "list for @var{obj}.") | |
1bbd0b84 | 198 | #define FUNC_NAME s_scm_set_source_properties_x |
575888bd MD |
199 | { |
200 | SCM handle; | |
34d19ef6 | 201 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 202 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 203 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 204 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 205 | SCM_WRONG_TYPE_ARG(1, obj); |
575888bd MD |
206 | handle = scm_hashq_create_handle_x (scm_source_whash, obj, plist); |
207 | SCM_SETCDR (handle, plist); | |
208 | return plist; | |
209 | } | |
1bbd0b84 | 210 | #undef FUNC_NAME |
575888bd | 211 | |
a1ec6916 | 212 | SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0, |
1bbd0b84 | 213 | (SCM obj, SCM key), |
e3239868 DH |
214 | "Return the source property specified by @var{key} from\n" |
215 | "@var{obj}'s source property list.") | |
1bbd0b84 | 216 | #define FUNC_NAME s_scm_source_property |
575888bd MD |
217 | { |
218 | SCM p; | |
34d19ef6 | 219 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 220 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 221 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 222 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 223 | SCM_WRONG_TYPE_ARG (1, obj); |
575888bd | 224 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
24933780 | 225 | if (!SRCPROPSP (p)) |
575888bd | 226 | goto plist; |
bc36d050 MV |
227 | if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p)); |
228 | else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p)); | |
229 | else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p)); | |
bc36d050 | 230 | else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p); |
575888bd MD |
231 | else |
232 | { | |
233 | p = SRCPROPPLIST (p); | |
234 | plist: | |
235 | p = scm_assoc (key, p); | |
236 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); | |
237 | } | |
238 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; | |
239 | } | |
1bbd0b84 | 240 | #undef FUNC_NAME |
575888bd | 241 | |
a1ec6916 | 242 | SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0, |
1bbd0b84 | 243 | (SCM obj, SCM key, SCM datum), |
e3239868 DH |
244 | "Set the source property of object @var{obj}, which is specified by\n" |
245 | "@var{key} to @var{datum}. Normally, the key will be a symbol.") | |
1bbd0b84 | 246 | #define FUNC_NAME s_scm_set_source_property_x |
575888bd MD |
247 | { |
248 | scm_whash_handle h; | |
249 | SCM p; | |
34d19ef6 | 250 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 251 | if (SCM_MEMOIZEDP (obj)) |
a4645b97 | 252 | obj = SCM_MEMOIZED_EXP (obj); |
d2e53ed6 | 253 | else if (!scm_is_pair (obj)) |
1bbd0b84 | 254 | SCM_WRONG_TYPE_ARG (1, obj); |
575888bd MD |
255 | h = scm_whash_get_handle (scm_source_whash, obj); |
256 | if (SCM_WHASHFOUNDP (h)) | |
257 | p = SCM_WHASHREF (scm_source_whash, h); | |
258 | else | |
259 | { | |
260 | h = scm_whash_create_handle (scm_source_whash, obj); | |
261 | p = SCM_EOL; | |
262 | } | |
bc36d050 | 263 | if (scm_is_eq (scm_sym_breakpoint, key)) |
cda139a7 | 264 | { |
62850ef3 DH |
265 | if (SRCPROPSP (p)) |
266 | { | |
7888309b | 267 | if (scm_is_false (datum)) |
62850ef3 DH |
268 | CLEARSRCPROPBRK (p); |
269 | else | |
270 | SETSRCPROPBRK (p); | |
271 | } | |
cda139a7 | 272 | else |
62850ef3 DH |
273 | { |
274 | SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p); | |
275 | SCM_WHASHSET (scm_source_whash, h, sp); | |
7888309b | 276 | if (scm_is_false (datum)) |
62850ef3 DH |
277 | CLEARSRCPROPBRK (sp); |
278 | else | |
279 | SETSRCPROPBRK (sp); | |
280 | } | |
cda139a7 | 281 | } |
bc36d050 | 282 | else if (scm_is_eq (scm_sym_line, key)) |
575888bd | 283 | { |
0c95b57d | 284 | if (SRCPROPSP (p)) |
a55c2b68 | 285 | SETSRCPROPLINE (p, scm_to_int (datum)); |
575888bd MD |
286 | else |
287 | SCM_WHASHSET (scm_source_whash, h, | |
a55c2b68 | 288 | scm_make_srcprops (scm_to_int (datum), 0, |
a9dbb9fd | 289 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd | 290 | } |
bc36d050 | 291 | else if (scm_is_eq (scm_sym_column, key)) |
575888bd | 292 | { |
0c95b57d | 293 | if (SRCPROPSP (p)) |
a55c2b68 | 294 | SETSRCPROPCOL (p, scm_to_int (datum)); |
575888bd MD |
295 | else |
296 | SCM_WHASHSET (scm_source_whash, h, | |
a55c2b68 | 297 | scm_make_srcprops (0, scm_to_int (datum), |
a9dbb9fd | 298 | SCM_UNDEFINED, SCM_UNDEFINED, p)); |
575888bd | 299 | } |
bc36d050 | 300 | else if (scm_is_eq (scm_sym_copy, key)) |
575888bd | 301 | { |
0c95b57d | 302 | if (SRCPROPSP (p)) |
575888bd MD |
303 | SRCPROPCOPY (p) = datum; |
304 | else | |
5c5549cb | 305 | SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); |
575888bd MD |
306 | } |
307 | else | |
58d233cc NJ |
308 | { |
309 | if (SRCPROPSP (p)) | |
310 | SRCPROPPLIST (p) = scm_acons (key, datum, SRCPROPPLIST (p)); | |
311 | else | |
312 | SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p)); | |
313 | } | |
575888bd MD |
314 | return SCM_UNSPECIFIED; |
315 | } | |
1bbd0b84 | 316 | #undef FUNC_NAME |
575888bd | 317 | |
1cc91f1b | 318 | |
575888bd MD |
319 | void |
320 | scm_init_srcprop () | |
575888bd | 321 | { |
e841c3e0 KN |
322 | scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); |
323 | scm_set_smob_mark (scm_tc16_srcprops, srcprops_mark); | |
e841c3e0 KN |
324 | scm_set_smob_print (scm_tc16_srcprops, srcprops_print); |
325 | ||
e11e83f3 | 326 | scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047)); |
86d31dfe | 327 | scm_c_define ("source-whash", scm_source_whash); |
85db4a2c | 328 | |
b0763985 HWN |
329 | scm_last_plist_filename |
330 | = scm_permanent_object (scm_cons (SCM_EOL, | |
331 | scm_acons (SCM_EOL, SCM_EOL, SCM_EOL))); | |
332 | ||
a0599745 | 333 | #include "libguile/srcprop.x" |
575888bd MD |
334 | } |
335 | ||
89e00824 ML |
336 | |
337 | /* | |
338 | Local Variables: | |
339 | c-file-style: "gnu" | |
340 | End: | |
341 | */ |