1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009, 2010, 2011 Free Software Foundation
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * 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.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "libguile/_scm.h"
28 #include "libguile/async.h"
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"
37 #include "libguile/gc.h"
39 #include "libguile/validate.h"
40 #include "libguile/srcprop.h"
41 #include "libguile/private-options.h"
44 /* {Source Properties}
46 * Properties of source list expressions.
47 * Four of these have special meaning:
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.
54 * Most properties above can be set by the reader.
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");
63 static SCM scm_source_whash
;
64 static scm_i_pthread_mutex_t source_lock
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
68 * Source properties are stored as double cells with the
77 #define SRCPROPSP(p) (SCM_SMOB_PREDICATE (scm_tc16_srcprops, (p)))
78 #define SRCPROPPOS(p) (SCM_SMOB_DATA(p))
79 #define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
80 #define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
81 #define SRCPROPCOPY(p) (SCM_SMOB_OBJECT_2(p))
82 #define SRCPROPALIST(p) (SCM_SMOB_OBJECT_3(p))
83 #define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
84 #define SETSRCPROPPOS(p, l, c) (SCM_SET_SMOB_DATA_1 (p, SRCPROPMAKPOS (l, c)))
85 #define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
86 #define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
87 #define SETSRCPROPCOPY(p, c) (SCM_SET_SMOB_OBJECT_2 (p, c))
88 #define SETSRCPROPALIST(p, l) (SCM_SET_SMOB_OBJECT_3 (p, l))
91 static SCM
scm_srcprops_to_alist (SCM obj
);
94 scm_t_bits scm_tc16_srcprops
;
97 srcprops_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
99 int writingp
= SCM_WRITINGP (pstate
);
100 scm_puts ("#<srcprops ", port
);
101 SCM_SET_WRITINGP (pstate
, 1);
102 scm_iprin1 (scm_srcprops_to_alist (obj
), port
, pstate
);
103 SCM_SET_WRITINGP (pstate
, writingp
);
104 scm_putc ('>', port
);
110 * We remember the last file name settings, so we can share that alist
111 * entry. This works because scm_set_source_property_x does not use
112 * assoc-set! for modifying the alist.
114 * This variable contains a protected cons, whose cdr is the cached
117 static SCM scm_last_alist_filename
;
120 scm_make_srcprops (long line
, int col
, SCM filename
, SCM copy
, SCM alist
)
122 if (!SCM_UNBNDP (filename
))
124 SCM old_alist
= alist
;
127 have to extract the acons, and operate on that, for
130 SCM last_acons
= SCM_CDR (scm_last_alist_filename
);
131 if (scm_is_null (old_alist
)
132 && scm_is_eq (SCM_CDAR (last_acons
), filename
))
138 alist
= scm_acons (scm_sym_filename
, filename
, alist
);
139 if (scm_is_null (old_alist
))
140 SCM_SETCDR (scm_last_alist_filename
, alist
);
144 SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops
,
145 SRCPROPMAKPOS (line
, col
),
152 scm_srcprops_to_alist (SCM obj
)
154 SCM alist
= SRCPROPALIST (obj
);
155 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
156 alist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), alist
);
157 alist
= scm_acons (scm_sym_column
, scm_from_int (SRCPROPCOL (obj
)), alist
);
158 alist
= scm_acons (scm_sym_line
, scm_from_int (SRCPROPLINE (obj
)), alist
);
162 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
164 "Return the source property association list of @var{obj}.")
165 #define FUNC_NAME s_scm_source_properties
168 SCM_VALIDATE_NIM (1, obj
);
170 scm_i_pthread_mutex_lock (&source_lock
);
171 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
172 scm_i_pthread_mutex_unlock (&source_lock
);
175 return scm_srcprops_to_alist (p
);
177 /* list from set-source-properties!, or SCM_EOL for not found */
182 /* Perhaps this procedure should look through an alist
183 and try to make a srcprops-object...? */
184 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
185 (SCM obj
, SCM alist
),
186 "Install the association list @var{alist} as the source property\n"
187 "list for @var{obj}.")
188 #define FUNC_NAME s_scm_set_source_properties_x
190 SCM_VALIDATE_NIM (1, obj
);
192 scm_i_pthread_mutex_lock (&source_lock
);
193 scm_hashq_set_x (scm_source_whash
, obj
, alist
);
194 scm_i_pthread_mutex_unlock (&source_lock
);
201 scm_i_has_source_properties (SCM obj
)
202 #define FUNC_NAME "%set-source-properties"
206 SCM_VALIDATE_NIM (1, obj
);
208 scm_i_pthread_mutex_lock (&source_lock
);
209 ret
= scm_is_true (scm_hashq_ref (scm_source_whash
, obj
, SCM_BOOL_F
));
210 scm_i_pthread_mutex_unlock (&source_lock
);
218 scm_i_set_source_properties_x (SCM obj
, long line
, int col
, SCM fname
)
219 #define FUNC_NAME "%set-source-properties"
221 SCM_VALIDATE_NIM (1, obj
);
223 scm_i_pthread_mutex_lock (&source_lock
);
224 scm_hashq_set_x (scm_source_whash
, obj
,
225 scm_make_srcprops (line
, col
, fname
,
227 ? scm_copy_tree (obj
)
230 scm_i_pthread_mutex_unlock (&source_lock
);
234 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
236 "Return the source property specified by @var{key} from\n"
237 "@var{obj}'s source property list.")
238 #define FUNC_NAME s_scm_source_property
241 SCM_VALIDATE_NIM (1, obj
);
243 scm_i_pthread_mutex_lock (&source_lock
);
244 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
245 scm_i_pthread_mutex_unlock (&source_lock
);
249 if (scm_is_eq (scm_sym_line
, key
))
250 p
= scm_from_int (SRCPROPLINE (p
));
251 else if (scm_is_eq (scm_sym_column
, key
))
252 p
= scm_from_int (SRCPROPCOL (p
));
253 else if (scm_is_eq (scm_sym_copy
, key
))
257 p
= SRCPROPALIST (p
);
259 p
= scm_assoc (key
, p
);
260 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
262 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
266 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
267 (SCM obj
, SCM key
, SCM datum
),
268 "Set the source property of object @var{obj}, which is specified by\n"
269 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
270 #define FUNC_NAME s_scm_set_source_property_x
273 SCM_VALIDATE_NIM (1, obj
);
275 scm_i_pthread_mutex_lock (&source_lock
);
276 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
278 if (scm_is_eq (scm_sym_line
, key
))
281 SETSRCPROPLINE (p
, scm_to_int (datum
));
283 scm_hashq_set_x (scm_source_whash
, obj
,
284 scm_make_srcprops (scm_to_int (datum
), 0,
285 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
287 else if (scm_is_eq (scm_sym_column
, key
))
290 SETSRCPROPCOL (p
, scm_to_int (datum
));
292 scm_hashq_set_x (scm_source_whash
, obj
,
293 scm_make_srcprops (0, scm_to_int (datum
),
294 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
296 else if (scm_is_eq (scm_sym_copy
, key
))
299 SETSRCPROPCOPY (p
, datum
);
301 scm_hashq_set_x (scm_source_whash
, obj
,
302 scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
307 SETSRCPROPALIST (p
, scm_acons (key
, datum
, SRCPROPALIST (p
)));
309 scm_hashq_set_x (scm_source_whash
, obj
,
310 scm_acons (key
, datum
, p
));
312 scm_i_pthread_mutex_unlock (&source_lock
);
314 return SCM_UNSPECIFIED
;
319 SCM_DEFINE (scm_cons_source
, "cons-source", 3, 0, 0,
320 (SCM xorig
, SCM x
, SCM y
),
321 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
322 "Any source properties associated with @var{xorig} are also associated\n"
323 "with the new pair.")
324 #define FUNC_NAME s_scm_cons_source
328 scm_i_pthread_mutex_lock (&source_lock
);
329 /* Copy source properties possibly associated with xorig. */
330 p
= scm_hashq_ref (scm_source_whash
, xorig
, SCM_BOOL_F
);
332 scm_hashq_set_x (scm_source_whash
, z
, p
);
333 scm_i_pthread_mutex_unlock (&source_lock
);
342 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
343 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
345 scm_source_whash
= scm_make_weak_key_hash_table (scm_from_int (2047));
346 scm_c_define ("source-whash", scm_source_whash
);
348 scm_last_alist_filename
= scm_cons (SCM_EOL
,
349 scm_acons (SCM_EOL
, SCM_EOL
, SCM_EOL
));
351 #include "libguile/srcprop.x"