1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 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
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "libguile/_scm.h"
24 #include "libguile/smob.h"
25 #include "libguile/alist.h"
26 #include "libguile/debug.h"
27 #include "libguile/hashtab.h"
28 #include "libguile/hash.h"
29 #include "libguile/ports.h"
30 #include "libguile/root.h"
31 #include "libguile/weaks.h"
33 #include "libguile/validate.h"
34 #include "libguile/srcprop.h"
36 /* {Source Properties}
38 * Properties of source list expressions.
39 * Five of these have special meaning and optimized storage:
41 * filename string The name of the source file.
42 * copy list A copy of the list expression.
43 * line integer The source code line number.
44 * column integer The source code column number.
45 * breakpoint boolean Sets a breakpoint on this form.
47 * Most properties above can be set by the reader.
51 SCM_GLOBAL_SYMBOL (scm_sym_filename
, "filename");
52 SCM_GLOBAL_SYMBOL (scm_sym_copy
, "copy");
53 SCM_GLOBAL_SYMBOL (scm_sym_line
, "line");
54 SCM_GLOBAL_SYMBOL (scm_sym_column
, "column");
55 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint
, "breakpoint");
57 scm_t_bits scm_tc16_srcprops
;
58 static scm_t_srcprops_chunk
*srcprops_chunklist
= 0;
59 static scm_t_srcprops
*srcprops_freelist
= 0;
63 srcprops_mark (SCM obj
)
65 scm_gc_mark (SRCPROPFNAME (obj
));
66 scm_gc_mark (SRCPROPCOPY (obj
));
67 return SRCPROPPLIST (obj
);
72 srcprops_free (SCM obj
)
74 *((scm_t_srcprops
**) SCM_SMOB_DATA (obj
)) = srcprops_freelist
;
75 srcprops_freelist
= (scm_t_srcprops
*) SCM_SMOB_DATA (obj
);
76 return 0; /* srcprops_chunks are not freed until leaving guile */
81 srcprops_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
83 int writingp
= SCM_WRITINGP (pstate
);
84 scm_puts ("#<srcprops ", port
);
85 SCM_SET_WRITINGP (pstate
, 1);
86 scm_iprin1 (scm_srcprops_to_plist (obj
), port
, pstate
);
87 SCM_SET_WRITINGP (pstate
, writingp
);
94 scm_c_source_property_breakpoint_p (SCM form
)
96 SCM obj
= scm_whash_lookup (scm_source_whash
, form
);
97 return SRCPROPSP (obj
) && SRCPROPBRK (obj
);
102 scm_make_srcprops (long line
, int col
, SCM filename
, SCM copy
, SCM plist
)
104 register scm_t_srcprops
*ptr
;
106 if ((ptr
= srcprops_freelist
) != NULL
)
107 srcprops_freelist
= *(scm_t_srcprops
**)ptr
;
111 scm_t_srcprops_chunk
*mem
;
112 size_t n
= sizeof (scm_t_srcprops_chunk
)
113 + sizeof (scm_t_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
114 SCM_SYSCALL (mem
= (scm_t_srcprops_chunk
*) scm_malloc (n
));
116 scm_memory_error ("srcprops");
117 scm_gc_register_collectable_memory (mem
, n
, "srcprops");
119 mem
->next
= srcprops_chunklist
;
120 srcprops_chunklist
= mem
;
121 ptr
= &mem
->srcprops
[0];
122 for (i
= 1; i
< SRCPROPS_CHUNKSIZE
- 1; ++i
)
123 *(scm_t_srcprops
**)&ptr
[i
] = &ptr
[i
+ 1];
124 *(scm_t_srcprops
**)&ptr
[SRCPROPS_CHUNKSIZE
- 1] = 0;
125 srcprops_freelist
= (scm_t_srcprops
*) &ptr
[1];
127 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
128 ptr
->fname
= filename
;
132 SCM_RETURN_NEWSMOB (scm_tc16_srcprops
, ptr
);
137 scm_srcprops_to_plist (SCM obj
)
139 SCM plist
= SRCPROPPLIST (obj
);
140 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
141 plist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), plist
);
142 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
143 plist
= scm_acons (scm_sym_filename
, SRCPROPFNAME (obj
), plist
);
144 plist
= scm_acons (scm_sym_column
, scm_from_int (SRCPROPCOL (obj
)), plist
);
145 plist
= scm_acons (scm_sym_line
, scm_from_int (SRCPROPLINE (obj
)), plist
);
146 plist
= scm_acons (scm_sym_breakpoint
, scm_from_bool (SRCPROPBRK (obj
)), plist
);
150 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
152 "Return the source property association list of @var{obj}.")
153 #define FUNC_NAME s_scm_source_properties
156 SCM_VALIDATE_NIM (1, obj
);
157 if (SCM_MEMOIZEDP (obj
))
158 obj
= SCM_MEMOIZED_EXP (obj
);
159 else if (!scm_is_pair (obj
))
160 SCM_WRONG_TYPE_ARG (1, obj
);
161 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
163 return scm_srcprops_to_plist (p
);
165 /* list from set-source-properties!, or SCM_EOL for not found */
170 /* Perhaps this procedure should look through an alist
171 and try to make a srcprops-object...? */
172 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
173 (SCM obj
, SCM plist
),
174 "Install the association list @var{plist} as the source property\n"
175 "list for @var{obj}.")
176 #define FUNC_NAME s_scm_set_source_properties_x
179 SCM_VALIDATE_NIM (1, obj
);
180 if (SCM_MEMOIZEDP (obj
))
181 obj
= SCM_MEMOIZED_EXP (obj
);
182 else if (!scm_is_pair (obj
))
183 SCM_WRONG_TYPE_ARG(1, obj
);
184 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
185 SCM_SETCDR (handle
, plist
);
190 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
192 "Return the source property specified by @var{key} from\n"
193 "@var{obj}'s source property list.")
194 #define FUNC_NAME s_scm_source_property
197 SCM_VALIDATE_NIM (1, obj
);
198 if (SCM_MEMOIZEDP (obj
))
199 obj
= SCM_MEMOIZED_EXP (obj
);
200 else if (!scm_is_pair (obj
))
201 SCM_WRONG_TYPE_ARG (1, obj
);
202 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
205 if (scm_is_eq (scm_sym_breakpoint
, key
)) p
= scm_from_bool (SRCPROPBRK (p
));
206 else if (scm_is_eq (scm_sym_line
, key
)) p
= scm_from_int (SRCPROPLINE (p
));
207 else if (scm_is_eq (scm_sym_column
, key
)) p
= scm_from_int (SRCPROPCOL (p
));
208 else if (scm_is_eq (scm_sym_filename
, key
)) p
= SRCPROPFNAME (p
);
209 else if (scm_is_eq (scm_sym_copy
, key
)) p
= SRCPROPCOPY (p
);
212 p
= SRCPROPPLIST (p
);
214 p
= scm_assoc (key
, p
);
215 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
217 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
221 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
222 (SCM obj
, SCM key
, SCM datum
),
223 "Set the source property of object @var{obj}, which is specified by\n"
224 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
225 #define FUNC_NAME s_scm_set_source_property_x
229 SCM_VALIDATE_NIM (1, obj
);
230 if (SCM_MEMOIZEDP (obj
))
231 obj
= SCM_MEMOIZED_EXP (obj
);
232 else if (!scm_is_pair (obj
))
233 SCM_WRONG_TYPE_ARG (1, obj
);
234 h
= scm_whash_get_handle (scm_source_whash
, obj
);
235 if (SCM_WHASHFOUNDP (h
))
236 p
= SCM_WHASHREF (scm_source_whash
, h
);
239 h
= scm_whash_create_handle (scm_source_whash
, obj
);
242 if (scm_is_eq (scm_sym_breakpoint
, key
))
246 if (scm_is_false (datum
))
253 SCM sp
= scm_make_srcprops (0, 0, SCM_UNDEFINED
, SCM_UNDEFINED
, p
);
254 SCM_WHASHSET (scm_source_whash
, h
, sp
);
255 if (scm_is_false (datum
))
256 CLEARSRCPROPBRK (sp
);
261 else if (scm_is_eq (scm_sym_line
, key
))
264 SETSRCPROPLINE (p
, scm_to_int (datum
));
266 SCM_WHASHSET (scm_source_whash
, h
,
267 scm_make_srcprops (scm_to_int (datum
), 0,
268 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
270 else if (scm_is_eq (scm_sym_column
, key
))
273 SETSRCPROPCOL (p
, scm_to_int (datum
));
275 SCM_WHASHSET (scm_source_whash
, h
,
276 scm_make_srcprops (0, scm_to_int (datum
),
277 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
279 else if (scm_is_eq (scm_sym_filename
, key
))
282 SRCPROPFNAME (p
) = datum
;
284 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
286 else if (scm_is_eq (scm_sym_copy
, key
))
289 SRCPROPCOPY (p
) = datum
;
291 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
296 SRCPROPPLIST (p
) = scm_acons (key
, datum
, SRCPROPPLIST (p
));
298 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
300 return SCM_UNSPECIFIED
;
308 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
309 scm_set_smob_mark (scm_tc16_srcprops
, srcprops_mark
);
310 scm_set_smob_free (scm_tc16_srcprops
, srcprops_free
);
311 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
313 scm_source_whash
= scm_make_weak_key_hash_table (scm_from_int (2047));
314 scm_c_define ("source-whash", scm_source_whash
);
316 #include "libguile/srcprop.x"
320 scm_finish_srcprop ()
322 register scm_t_srcprops_chunk
*ptr
= srcprops_chunklist
, *next
;
323 size_t n
= sizeof (scm_t_srcprops_chunk
)
324 + sizeof (scm_t_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
328 scm_gc_unregister_collectable_memory (ptr
, n
, "srcprops");