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