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