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