use the new public metatables from struct.c
[bpt/guile.git] / libguile / srcprop.c
CommitLineData
ecdfc95d 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006, 2008, 2009 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.
b0763985 45 * Five 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.
51 * breakpoint boolean Sets a breakpoint on this form.
52 *
53 * Most properties above can be set by the reader.
54 *
55 */
56
85db4a2c
DH
57SCM_GLOBAL_SYMBOL (scm_sym_filename, "filename");
58SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
59SCM_GLOBAL_SYMBOL (scm_sym_line, "line");
60SCM_GLOBAL_SYMBOL (scm_sym_column, "column");
61SCM_GLOBAL_SYMBOL (scm_sym_breakpoint, "breakpoint");
e7efe8e7 62SCM 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)))
77#define SRCPROPBRK(p) (SCM_SMOB_FLAGS (p) & SCM_SOURCE_PROPERTY_FLAG_BREAK)
78#define SRCPROPPOS(p) (SCM_CELL_WORD(p,1))
79#define SRCPROPLINE(p) (SRCPROPPOS(p) >> 12)
80#define SRCPROPCOL(p) (SRCPROPPOS(p) & 0x0fffL)
81#define SRCPROPCOPY(p) (SCM_CELL_OBJECT(p,2))
67a96734 82#define SRCPROPALIST(p) (SCM_CELL_OBJECT_3(p))
b0763985
HWN
83#define SETSRCPROPBRK(p) \
84 (SCM_SET_SMOB_FLAGS ((p), \
85 SCM_SMOB_FLAGS (p) | SCM_SOURCE_PROPERTY_FLAG_BREAK))
86#define CLEARSRCPROPBRK(p) \
87 (SCM_SET_SMOB_FLAGS ((p), \
88 SCM_SMOB_FLAGS (p) & ~SCM_SOURCE_PROPERTY_FLAG_BREAK))
89#define SRCPROPMAKPOS(l, c) (((l) << 12) + (c))
90#define SETSRCPROPPOS(p, l, c) (SCM_SET_CELL_WORD(p,1, SRCPROPMAKPOS (l, c)))
91#define SETSRCPROPLINE(p, l) SETSRCPROPPOS (p, l, SRCPROPCOL (p))
92#define SETSRCPROPCOL(p, c) SETSRCPROPPOS (p, SRCPROPLINE (p), c)
80237dcc 93#define SETSRCPROPCOPY(p, c) (SCM_SET_CELL_WORD(p, 2, c))
67a96734 94#define SETSRCPROPALIST(p, l) (SCM_SET_CELL_WORD(p, 3, l))
b0763985
HWN
95
96
67a96734
NJ
97static SCM scm_srcprops_to_alist (SCM obj);
98
b0763985 99
92c2555f 100scm_t_bits scm_tc16_srcprops;
575888bd 101
575888bd 102static int
e841c3e0 103srcprops_print (SCM obj, SCM port, scm_print_state *pstate)
575888bd 104{
19402679 105 int writingp = SCM_WRITINGP (pstate);
b7f3516f 106 scm_puts ("#<srcprops ", port);
19402679 107 SCM_SET_WRITINGP (pstate, 1);
67a96734 108 scm_iprin1 (scm_srcprops_to_alist (obj), port, pstate);
19402679 109 SCM_SET_WRITINGP (pstate, writingp);
b7f3516f 110 scm_putc ('>', port);
575888bd
MD
111 return 1;
112}
113
1cc91f1b 114
bc76d628
DH
115int
116scm_c_source_property_breakpoint_p (SCM form)
117{
118 SCM obj = scm_whash_lookup (scm_source_whash, form);
119 return SRCPROPSP (obj) && SRCPROPBRK (obj);
120}
121
122
b0763985 123/*
67a96734 124 * We remember the last file name settings, so we can share that alist
d00a0704 125 * entry. This works because scm_set_source_property_x does not use
67a96734 126 * assoc-set! for modifying the alist.
d00a0704
HWN
127 *
128 * This variable contains a protected cons, whose cdr is the cached
67a96734 129 * alist
b0763985 130 */
67a96734 131static SCM scm_last_alist_filename;
b0763985 132
575888bd 133SCM
67a96734 134scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
575888bd 135{
b0763985 136 if (!SCM_UNBNDP (filename))
575888bd 137 {
67a96734 138 SCM old_alist = alist;
42e6668b 139
b0763985
HWN
140 /*
141 have to extract the acons, and operate on that, for
142 thread safety.
143 */
67a96734
NJ
144 SCM last_acons = SCM_CDR (scm_last_alist_filename);
145 if (old_alist == SCM_EOL
b0763985
HWN
146 && SCM_CDAR (last_acons) == filename)
147 {
67a96734 148 alist = last_acons;
b0763985
HWN
149 }
150 else
151 {
67a96734
NJ
152 alist = scm_acons (scm_sym_filename, filename, alist);
153 if (old_alist == SCM_EOL)
154 SCM_SETCDR (scm_last_alist_filename, alist);
b0763985 155 }
575888bd 156 }
b0763985
HWN
157
158 SCM_RETURN_NEWSMOB3 (scm_tc16_srcprops,
159 SRCPROPMAKPOS (line, col),
160 copy,
67a96734 161 alist);
575888bd
MD
162}
163
1cc91f1b 164
67a96734
NJ
165static SCM
166scm_srcprops_to_alist (SCM obj)
575888bd 167{
67a96734 168 SCM alist = SRCPROPALIST (obj);
575888bd 169 if (!SCM_UNBNDP (SRCPROPCOPY (obj)))
67a96734
NJ
170 alist = scm_acons (scm_sym_copy, SRCPROPCOPY (obj), alist);
171 alist = scm_acons (scm_sym_column, scm_from_int (SRCPROPCOL (obj)), alist);
172 alist = scm_acons (scm_sym_line, scm_from_int (SRCPROPLINE (obj)), alist);
173 alist = scm_acons (scm_sym_breakpoint, scm_from_bool (SRCPROPBRK (obj)), alist);
174 return alist;
575888bd
MD
175}
176
a1ec6916 177SCM_DEFINE (scm_source_properties, "source-properties", 1, 0, 0,
1bbd0b84 178 (SCM obj),
e3239868 179 "Return the source property association list of @var{obj}.")
1bbd0b84 180#define FUNC_NAME s_scm_source_properties
575888bd
MD
181{
182 SCM p;
34d19ef6 183 SCM_VALIDATE_NIM (1, obj);
f5003c13 184 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
230d095f 185 if (SRCPROPSP (p))
67a96734 186 return scm_srcprops_to_alist (p);
f5003c13
KR
187 else
188 /* list from set-source-properties!, or SCM_EOL for not found */
189 return p;
575888bd 190}
1bbd0b84 191#undef FUNC_NAME
575888bd
MD
192
193/* Perhaps this procedure should look through an alist
194 and try to make a srcprops-object...? */
a1ec6916 195SCM_DEFINE (scm_set_source_properties_x, "set-source-properties!", 2, 0, 0,
67a96734
NJ
196 (SCM obj, SCM alist),
197 "Install the association list @var{alist} as the source property\n"
e3239868 198 "list for @var{obj}.")
1bbd0b84 199#define FUNC_NAME s_scm_set_source_properties_x
575888bd
MD
200{
201 SCM handle;
34d19ef6 202 SCM_VALIDATE_NIM (1, obj);
67a96734
NJ
203 handle = scm_hashq_create_handle_x (scm_source_whash, obj, alist);
204 SCM_SETCDR (handle, alist);
205 return alist;
575888bd 206}
1bbd0b84 207#undef FUNC_NAME
575888bd 208
a1ec6916 209SCM_DEFINE (scm_source_property, "source-property", 2, 0, 0,
1bbd0b84 210 (SCM obj, SCM key),
e3239868
DH
211 "Return the source property specified by @var{key} from\n"
212 "@var{obj}'s source property list.")
1bbd0b84 213#define FUNC_NAME s_scm_source_property
575888bd
MD
214{
215 SCM p;
34d19ef6 216 SCM_VALIDATE_NIM (1, obj);
575888bd 217 p = scm_hashq_ref (scm_source_whash, obj, SCM_EOL);
24933780 218 if (!SRCPROPSP (p))
67a96734 219 goto alist;
bc36d050
MV
220 if (scm_is_eq (scm_sym_breakpoint, key)) p = scm_from_bool (SRCPROPBRK (p));
221 else if (scm_is_eq (scm_sym_line, key)) p = scm_from_int (SRCPROPLINE (p));
222 else if (scm_is_eq (scm_sym_column, key)) p = scm_from_int (SRCPROPCOL (p));
bc36d050 223 else if (scm_is_eq (scm_sym_copy, key)) p = SRCPROPCOPY (p);
575888bd
MD
224 else
225 {
67a96734
NJ
226 p = SRCPROPALIST (p);
227 alist:
575888bd
MD
228 p = scm_assoc (key, p);
229 return (SCM_NIMP (p) ? SCM_CDR (p) : SCM_BOOL_F);
230 }
231 return SCM_UNBNDP (p) ? SCM_BOOL_F : p;
232}
1bbd0b84 233#undef FUNC_NAME
575888bd 234
a1ec6916 235SCM_DEFINE (scm_set_source_property_x, "set-source-property!", 3, 0, 0,
1bbd0b84 236 (SCM obj, SCM key, SCM datum),
e3239868
DH
237 "Set the source property of object @var{obj}, which is specified by\n"
238 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
1bbd0b84 239#define FUNC_NAME s_scm_set_source_property_x
575888bd
MD
240{
241 scm_whash_handle h;
242 SCM p;
34d19ef6 243 SCM_VALIDATE_NIM (1, obj);
575888bd
MD
244 h = scm_whash_get_handle (scm_source_whash, obj);
245 if (SCM_WHASHFOUNDP (h))
246 p = SCM_WHASHREF (scm_source_whash, h);
247 else
248 {
249 h = scm_whash_create_handle (scm_source_whash, obj);
250 p = SCM_EOL;
251 }
bc36d050 252 if (scm_is_eq (scm_sym_breakpoint, key))
cda139a7 253 {
62850ef3
DH
254 if (SRCPROPSP (p))
255 {
7888309b 256 if (scm_is_false (datum))
62850ef3
DH
257 CLEARSRCPROPBRK (p);
258 else
259 SETSRCPROPBRK (p);
260 }
cda139a7 261 else
62850ef3
DH
262 {
263 SCM sp = scm_make_srcprops (0, 0, SCM_UNDEFINED, SCM_UNDEFINED, p);
264 SCM_WHASHSET (scm_source_whash, h, sp);
7888309b 265 if (scm_is_false (datum))
62850ef3
DH
266 CLEARSRCPROPBRK (sp);
267 else
268 SETSRCPROPBRK (sp);
269 }
cda139a7 270 }
bc36d050 271 else if (scm_is_eq (scm_sym_line, key))
575888bd 272 {
0c95b57d 273 if (SRCPROPSP (p))
a55c2b68 274 SETSRCPROPLINE (p, scm_to_int (datum));
575888bd
MD
275 else
276 SCM_WHASHSET (scm_source_whash, h,
a55c2b68 277 scm_make_srcprops (scm_to_int (datum), 0,
a9dbb9fd 278 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 279 }
bc36d050 280 else if (scm_is_eq (scm_sym_column, key))
575888bd 281 {
0c95b57d 282 if (SRCPROPSP (p))
a55c2b68 283 SETSRCPROPCOL (p, scm_to_int (datum));
575888bd
MD
284 else
285 SCM_WHASHSET (scm_source_whash, h,
a55c2b68 286 scm_make_srcprops (0, scm_to_int (datum),
a9dbb9fd 287 SCM_UNDEFINED, SCM_UNDEFINED, p));
575888bd 288 }
bc36d050 289 else if (scm_is_eq (scm_sym_copy, key))
575888bd 290 {
0c95b57d 291 if (SRCPROPSP (p))
80237dcc 292 SETSRCPROPCOPY (p, datum);
575888bd 293 else
5c5549cb 294 SCM_WHASHSET (scm_source_whash, h, scm_make_srcprops (0, 0, SCM_UNDEFINED, datum, p));
575888bd
MD
295 }
296 else
58d233cc
NJ
297 {
298 if (SRCPROPSP (p))
67a96734 299 SETSRCPROPALIST (p, scm_acons (key, datum, SRCPROPALIST (p)));
58d233cc
NJ
300 else
301 SCM_WHASHSET (scm_source_whash, h, scm_acons (key, datum, p));
302 }
575888bd
MD
303 return SCM_UNSPECIFIED;
304}
1bbd0b84 305#undef FUNC_NAME
575888bd 306
1cc91f1b 307
0f458a37
AW
308SCM_DEFINE (scm_cons_source, "cons-source", 3, 0, 0,
309 (SCM xorig, SCM x, SCM y),
310 "Create and return a new pair whose car and cdr are @var{x} and @var{y}.\n"
311 "Any source properties associated with @var{xorig} are also associated\n"
312 "with the new pair.")
313#define FUNC_NAME s_scm_cons_source
314{
315 SCM p, z;
316 z = scm_cons (x, y);
317 /* Copy source properties possibly associated with xorig. */
318 p = scm_whash_lookup (scm_source_whash, xorig);
319 if (scm_is_true (p))
320 scm_whash_insert (scm_source_whash, z, p);
321 return z;
322}
323#undef FUNC_NAME
324
325
575888bd
MD
326void
327scm_init_srcprop ()
575888bd 328{
e841c3e0 329 scm_tc16_srcprops = scm_make_smob_type ("srcprops", 0);
e841c3e0
KN
330 scm_set_smob_print (scm_tc16_srcprops, srcprops_print);
331
e11e83f3 332 scm_source_whash = scm_make_weak_key_hash_table (scm_from_int (2047));
86d31dfe 333 scm_c_define ("source-whash", scm_source_whash);
85db4a2c 334
f39448c5
AW
335 scm_last_alist_filename = scm_cons (SCM_EOL,
336 scm_acons (SCM_EOL, SCM_EOL, SCM_EOL));
b0763985 337
a0599745 338#include "libguile/srcprop.x"
575888bd
MD
339}
340
89e00824
ML
341
342/*
343 Local Variables:
344 c-file-style: "gnu"
345 End:
346*/