Commit | Line | Data |
---|---|---|
d1c4720c | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009, 2010, 2011 Free Software Foundation |
575888bd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
575888bd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
575888bd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
575888bd | 24 | |
e6e2e95a MD |
25 | #include <errno.h> |
26 | ||
a0599745 | 27 | #include "libguile/_scm.h" |
4e047c3e | 28 | #include "libguile/async.h" |
a0599745 MD |
29 | #include "libguile/smob.h" |
30 | #include "libguile/alist.h" | |
31 | #include "libguile/debug.h" | |
32 | #include "libguile/hashtab.h" | |
33 | #include "libguile/hash.h" | |
34 | #include "libguile/ports.h" | |
35 | #include "libguile/root.h" | |
36 | #include "libguile/weaks.h" | |
42e6668b | 37 | #include "libguile/gc.h" |
575888bd | 38 | |
a0599745 MD |
39 | #include "libguile/validate.h" |
40 | #include "libguile/srcprop.h" | |
26c8cc14 AW |
41 | #include "libguile/private-options.h" |
42 | ||
575888bd MD |
43 | \f |
44 | /* {Source Properties} | |
45 | * | |
46 | * Properties of source list expressions. | |
8cbcaaa0 | 47 | * Four of these have special meaning: |
575888bd MD |
48 | * |
49 | * filename string The name of the source file. | |
50 | * copy list A copy of the list expression. | |
51 | * line integer The source code line number. | |
52 | * column integer The source code column number. | |
575888bd MD |
53 | * |
54 | * Most properties above can be set by the reader. | |
55 | * | |
56 | */ | |
57 | ||
85db4a2c DH |
58 | SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename"); |
59 | SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy"); | |
60 | SCM_GLOBAL_SYMBOL (scm_sym_line, "line"); | |
61 | SCM_GLOBAL_SYMBOL (scm_sym_column, "column"); | |
26c8cc14 | 62 | static SCM scm_source_whash; |
575888bd | 63 | |
575888bd | 64 | |
1cc91f1b | 65 | |
b0763985 | 66 | /* |
d00a0704 HWN |
67 | * Source properties are stored as double cells with the |
68 | * following layout: | |
b0763985 | 69 | |
d00a0704 HWN |
70 | * car = tag |
71 | * cbr = pos | |
72 | * ccr = copy | |
d5ed380e | 73 | * cdr = alist |
d00a0704 | 74 | */ |
b0763985 HWN |
75 | |
76 | #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p))) | |
9e9e54eb | 77 | #define SRCPROPPOS(p) (SCM_SMOB_DATA(p)) |
b0763985 HWN |
78 | #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12) |
79 | #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL) | |
9e9e54eb AW |
80 | #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p)) |
81 | #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p)) | |
b0763985 | 82 | #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c)) |
9e9e54eb | 83 | #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c))) |
b0763985 HWN |
84 | #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p)) |
85 | #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c) | |
9e9e54eb AW |
86 | #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c)) |
87 | #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l)) | |
b0763985 HWN |
88 | |
89 | ||
67a96734 NJ |
90 | static SCM scm_srcprops_to_alist (SCM obj); |
91 | ||
b0763985 | 92 | |
92c2555f | 93 | scm_t_bits scm_tc16_srcprops; |
575888bd | 94 | |
575888bd | 95 | static int |
e841c3e0 | 96 | srcprops_print (SCM obj, SCM port, scm_print_state *pstate) |
575888bd | 97 | { |
19402679 | 98 | int writingp = SCM_WRITINGP (pstate); |
b7f3516f | 99 | scm_puts ("#<srcprops ", port); |
19402679 | 100 | SCM_SET_WRITINGP (pstate, 1); |
67a96734 | 101 | scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate); |
19402679 | 102 | SCM_SET_WRITINGP (pstate, writingp); |
b7f3516f | 103 | scm_putc ('>', port); |
575888bd MD |
104 | return 1; |
105 | } | |
106 | ||
1cc91f1b | 107 | |
b0763985 | 108 | /* |
67a96734 | 109 | * We remember the last file name settings, so we can share that alist |
d00a0704 | 110 | * entry. This works because scm_set_source_property_x does not use |
67a96734 | 111 | * assoc-set! for modifying the alist. |
d00a0704 HWN |
112 | * |
113 | * This variable contains a protected cons, whose cdr is the cached | |
67a96734 | 114 | * alist |
b0763985 | 115 | */ |
67a96734 | 116 | static SCM scm_last_alist_filename; |
b0763985 | 117 | |
575888bd | 118 | SCM |
67a96734 | 119 | scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist) |
575888bd | 120 | { |
b0763985 | 121 | if (!SCM_UNBNDP (filename)) |
575888bd | 122 | { |
67a96734 | 123 | SCM old_alist = alist; |
42e6668b | 124 | |
b0763985 HWN |
125 | /* |
126 | have to extract the acons, and operate on that, for | |
127 | thread safety. | |
128 | */ | |
67a96734 | 129 | SCM last_acons = SCM_CDR (scm_last_alist_filename); |
393baa8a AW |
130 | if (scm_is_null (old_alist) |
131 | && scm_is_eq (SCM_CDAR (last_acons), filename)) | |
b0763985 | 132 | { |
67a96734 | 133 | alist = last_acons; |
b0763985 HWN |
134 | } |
135 | else | |
136 | { | |
67a96734 | 137 | alist = scm_acons (scm_sym_filename, filename, alist); |
393baa8a | 138 | if (scm_is_null (old_alist)) |
67a96734 | 139 | SCM_SETCDR (scm_last_alist_filename, alist); |
b0763985 | 140 | } |
575888bd | 141 | } |
b0763985 HWN |
142 | |
143 | SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops, | |
144 | SRCPROPMAKPOS (line, col), | |
b2b33168 AW |
145 | SCM_UNPACK (copy), |
146 | SCM_UNPACK (alist)); | |
575888bd MD |
147 | } |
148 | ||
1cc91f1b | 149 | |
67a96734 NJ |
150 | static SCM |
151 | scm_srcprops_to_alist (SCM obj) | |
575888bd | 152 | { |
67a96734 | 153 | SCM alist = SRCPROPALIST (obj); |
575888bd | 154 | if (!SCM_UNBNDP (SRCPROPCOPY (obj))) |
67a96734 NJ |
155 | alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist); |
156 | alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist); | |
157 | alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist); | |
67a96734 | 158 | return alist; |
575888bd MD |
159 | } |
160 | ||
a1ec6916 | 161 | SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0, |
1bbd0b84 | 162 | (SCM obj), |
e3239868 | 163 | "Return the source property association list of @var{obj}.") |
1bbd0b84 | 164 | #define FUNC_NAME s_scm_source_properties |
575888bd MD |
165 | { |
166 | SCM p; | |
34d19ef6 | 167 | SCM_VALIDATE_NIM (1, obj); |
f5003c13 | 168 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
230d095f | 169 | if (SRCPROPSP (p)) |
67a96734 | 170 | return scm_srcprops_to_alist (p); |
f5003c13 KR |
171 | else |
172 | /* list from set-source-properties!, or SCM_EOL for not found */ | |
173 | return p; | |
575888bd | 174 | } |
1bbd0b84 | 175 | #undef FUNC_NAME |
575888bd MD |
176 | |
177 | /* Perhaps this procedure should look through an alist | |
178 | and try to make a srcprops-object...? */ | |
a1ec6916 | 179 | SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0, |
67a96734 NJ |
180 | (SCM obj, SCM alist), |
181 | "Install the association list @var{alist} as the source property\n" | |
e3239868 | 182 | "list for @var{obj}.") |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_set_source_properties_x |
575888bd | 184 | { |
34d19ef6 | 185 | SCM_VALIDATE_NIM (1, obj); |
d1c4720c | 186 | scm_hashq_set_x (scm_source_whash, obj, alist); |
67a96734 | 187 | return alist; |
575888bd | 188 | } |
1bbd0b84 | 189 | #undef FUNC_NAME |
575888bd | 190 | |
26c8cc14 AW |
191 | int |
192 | scm_i_has_source_properties (SCM obj) | |
193 | #define FUNC_NAME "%set-source-properties" | |
194 | { | |
195 | SCM_VALIDATE_NIM (1, obj); | |
196 | ||
197 | return scm_is_true (scm_hashq_ref (scm_source_whash, obj, SCM_BOOL_F)); | |
198 | } | |
199 | #undef FUNC_NAME | |
200 | ||
201 | ||
202 | void | |
203 | scm_i_set_source_properties_x (SCM obj, long line, int col, SCM fname) | |
204 | #define FUNC_NAME "%set-source-properties" | |
205 | { | |
206 | SCM_VALIDATE_NIM (1, obj); | |
207 | ||
208 | scm_hashq_set_x (scm_source_whash, obj, | |
209 | scm_make_srcprops (line, col, fname, | |
210 | SCM_COPY_SOURCE_P | |
211 | ? scm_copy_tree (obj) | |
212 | : SCM_UNDEFINED, | |
213 | SCM_EOL)); | |
214 | } | |
215 | #undef FUNC_NAME | |
216 | ||
a1ec6916 | 217 | SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0, |
1bbd0b84 | 218 | (SCM obj, SCM key), |
e3239868 DH |
219 | "Return the source property specified by @var{key} from\n" |
220 | "@var{obj}'s source property list.") | |
1bbd0b84 | 221 | #define FUNC_NAME s_scm_source_property |
575888bd MD |
222 | { |
223 | SCM p; | |
34d19ef6 | 224 | SCM_VALIDATE_NIM (1, obj); |
575888bd | 225 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
24933780 | 226 | if (!SRCPROPSP (p)) |
67a96734 | 227 | goto alist; |
8cbcaaa0 AW |
228 | if (scm_is_eq (scm_sym_line, key)) |
229 | p = scm_from_int (SRCPROPLINE (p)); | |
230 | else if (scm_is_eq (scm_sym_column, key)) | |
231 | p = scm_from_int (SRCPROPCOL (p)); | |
232 | else if (scm_is_eq (scm_sym_copy, key)) | |
233 | p = SRCPROPCOPY (p); | |
575888bd MD |
234 | else |
235 | { | |
67a96734 NJ |
236 | p = SRCPROPALIST (p); |
237 | alist: | |
575888bd MD |
238 | p = scm_assoc (key, p); |
239 | return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F); | |
240 | } | |
241 | return SCM_UNBNDP (p) ? SCM_BOOL_F : p; | |
242 | } | |
1bbd0b84 | 243 | #undef FUNC_NAME |
575888bd | 244 | |
a1ec6916 | 245 | SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0, |
1bbd0b84 | 246 | (SCM obj, SCM key, SCM datum), |
e3239868 DH |
247 | "Set the source property of object @var{obj}, which is specified by\n" |
248 | "@var{key} to @var{datum}. Normally, the key will be a symbol.") | |
1bbd0b84 | 249 | #define FUNC_NAME s_scm_set_source_property_x |
575888bd | 250 | { |
575888bd | 251 | SCM p; |
34d19ef6 | 252 | SCM_VALIDATE_NIM (1, obj); |
d1c4720c | 253 | p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL); |
8cbcaaa0 AW |
254 | |
255 | if (scm_is_eq (scm_sym_line, key)) | |
575888bd | 256 | { |
0c95b57d | 257 | if (SRCPROPSP (p)) |
a55c2b68 | 258 | SETSRCPROPLINE (p, scm_to_int (datum)); |
575888bd | 259 | else |
d1c4720c AW |
260 | scm_hashq_set_x (scm_source_whash, obj, |
261 | scm_make_srcprops (scm_to_int (datum), 0, | |
262 | SCM_UNDEFINED, SCM_UNDEFINED, p)); | |
575888bd | 263 | } |
bc36d050 | 264 | else if (scm_is_eq (scm_sym_column, key)) |
575888bd | 265 | { |
0c95b57d | 266 | if (SRCPROPSP (p)) |
a55c2b68 | 267 | SETSRCPROPCOL (p, scm_to_int (datum)); |
575888bd | 268 | else |
d1c4720c AW |
269 | scm_hashq_set_x (scm_source_whash, obj, |
270 | scm_make_srcprops (0, scm_to_int (datum), | |
271 | SCM_UNDEFINED, SCM_UNDEFINED, p)); | |
575888bd | 272 | } |
bc36d050 | 273 | else if (scm_is_eq (scm_sym_copy, key)) |
575888bd | 274 | { |
0c95b57d | 275 | if (SRCPROPSP (p)) |
80237dcc | 276 | SETSRCPROPCOPY (p, datum); |
575888bd | 277 | else |
d1c4720c AW |
278 | scm_hashq_set_x (scm_source_whash, obj, |
279 | scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p)); | |
575888bd MD |
280 | } |
281 | else | |
58d233cc NJ |
282 | { |
283 | if (SRCPROPSP (p)) | |
67a96734 | 284 | SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p))); |
58d233cc | 285 | else |
d1c4720c AW |
286 | scm_hashq_set_x (scm_source_whash, obj, |
287 | scm_acons (key, datum, p)); | |
58d233cc | 288 | } |
575888bd MD |
289 | return SCM_UNSPECIFIED; |
290 | } | |
1bbd0b84 | 291 | #undef FUNC_NAME |
575888bd | 292 | |
1cc91f1b | 293 | |
0f458a37 AW |
294 | SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0, |
295 | (SCM xorig, SCM x, SCM y), | |
296 | "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n" | |
297 | "Any source properties associated with @var{xorig} are also associated\n" | |
298 | "with the new pair.") | |
299 | #define FUNC_NAME s_scm_cons_source | |
300 | { | |
301 | SCM p, z; | |
302 | z = scm_cons (x, y); | |
303 | /* Copy source properties possibly associated with xorig. */ | |
d1c4720c | 304 | p = scm_hashq_ref (scm_source_whash, xorig, SCM_BOOL_F); |
0f458a37 | 305 | if (scm_is_true (p)) |
d1c4720c | 306 | scm_hashq_set_x (scm_source_whash, z, p); |
0f458a37 AW |
307 | return z; |
308 | } | |
309 | #undef FUNC_NAME | |
310 | ||
311 | ||
575888bd MD |
312 | void |
313 | scm_init_srcprop () | |
575888bd | 314 | { |
e841c3e0 | 315 | scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0); |
e841c3e0 KN |
316 | scm_set_smob_print (scm_tc16_srcprops, srcprops_print); |
317 | ||
e11e83f3 | 318 | scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047)); |
86d31dfe | 319 | scm_c_define ("source-whash", scm_source_whash); |
85db4a2c | 320 | |
f39448c5 AW |
321 | scm_last_alist_filename = scm_cons (SCM_EOL, |
322 | scm_acons (SCM_EOL, SCM_EOL, SCM_EOL)); | |
b0763985 | 323 | |
a0599745 | 324 | #include "libguile/srcprop.x" |
575888bd MD |
325 | } |
326 | ||
89e00824 ML |
327 | |
328 | /* | |
329 | Local Variables: | |
330 | c-file-style: "gnu" | |
331 | End: | |
332 | */ |