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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libguile/_scm.h"
24 #include "libguile/async.h"
25 #include "libguile/smob.h"
26 #include "libguile/alist.h"
27 #include "libguile/debug.h"
28 #include "libguile/hashtab.h"
29 #include "libguile/hash.h"
30 #include "libguile/ports.h"
31 #include "libguile/root.h"
32 #include "libguile/weaks.h"
34 #include "libguile/validate.h"
35 #include "libguile/srcprop.h"
37 /* {Source Properties}
39 * Properties of source list expressions.
40 * Five of these have special meaning and optimized storage:
42 * filename string The name of the source file.
43 * copy list A copy of the list expression.
44 * line integer The source code line number.
45 * column integer The source code column number.
46 * breakpoint boolean Sets a breakpoint on this form.
48 * Most properties above can be set by the reader.
52 SCM_GLOBAL_SYMBOL (scm_sym_filename
, "filename");
53 SCM_GLOBAL_SYMBOL (scm_sym_copy
, "copy");
54 SCM_GLOBAL_SYMBOL (scm_sym_line
, "line");
55 SCM_GLOBAL_SYMBOL (scm_sym_column
, "column");
56 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint
, "breakpoint");
58 scm_t_bits scm_tc16_srcprops
;
59 static scm_t_srcprops_chunk
*srcprops_chunklist
= 0;
60 static scm_t_srcprops
*srcprops_freelist
= 0;
64 srcprops_mark (SCM obj
)
66 scm_gc_mark (SRCPROPFNAME (obj
));
67 scm_gc_mark (SRCPROPCOPY (obj
));
68 return SRCPROPPLIST (obj
);
73 srcprops_free (SCM obj
)
75 *((scm_t_srcprops
**) SCM_SMOB_DATA (obj
)) = srcprops_freelist
;
76 srcprops_freelist
= (scm_t_srcprops
*) SCM_SMOB_DATA (obj
);
77 return 0; /* srcprops_chunks are not freed until leaving guile */
82 srcprops_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
84 int writingp
= SCM_WRITINGP (pstate
);
85 scm_puts ("#<srcprops ", port
);
86 SCM_SET_WRITINGP (pstate
, 1);
87 scm_iprin1 (scm_srcprops_to_plist (obj
), port
, pstate
);
88 SCM_SET_WRITINGP (pstate
, writingp
);
95 scm_c_source_property_breakpoint_p (SCM form
)
97 SCM obj
= scm_whash_lookup (scm_source_whash
, form
);
98 return SRCPROPSP (obj
) && SRCPROPBRK (obj
);
103 scm_make_srcprops (long line
, int col
, SCM filename
, SCM copy
, SCM plist
)
105 register scm_t_srcprops
*ptr
;
106 SCM_CRITICAL_SECTION_START
;
107 if ((ptr
= srcprops_freelist
) != NULL
)
108 srcprops_freelist
= *(scm_t_srcprops
**)ptr
;
112 scm_t_srcprops_chunk
*mem
;
113 size_t n
= sizeof (scm_t_srcprops_chunk
)
114 + sizeof (scm_t_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
115 SCM_SYSCALL (mem
= (scm_t_srcprops_chunk
*) scm_malloc (n
));
117 scm_memory_error ("srcprops");
118 scm_gc_register_collectable_memory (mem
, n
, "srcprops");
120 mem
->next
= srcprops_chunklist
;
121 srcprops_chunklist
= mem
;
122 ptr
= &mem
->srcprops
[0];
123 for (i
= 1; i
< SRCPROPS_CHUNKSIZE
- 1; ++i
)
124 *(scm_t_srcprops
**)&ptr
[i
] = &ptr
[i
+ 1];
125 *(scm_t_srcprops
**)&ptr
[SRCPROPS_CHUNKSIZE
- 1] = 0;
126 srcprops_freelist
= (scm_t_srcprops
*) &ptr
[1];
128 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
129 ptr
->fname
= filename
;
132 SCM_CRITICAL_SECTION_END
;
133 SCM_RETURN_NEWSMOB (scm_tc16_srcprops
, ptr
);
138 scm_srcprops_to_plist (SCM obj
)
140 SCM plist
= SRCPROPPLIST (obj
);
141 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
142 plist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), plist
);
143 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
144 plist
= scm_acons (scm_sym_filename
, SRCPROPFNAME (obj
), plist
);
145 plist
= scm_acons (scm_sym_column
, scm_from_int (SRCPROPCOL (obj
)), plist
);
146 plist
= scm_acons (scm_sym_line
, scm_from_int (SRCPROPLINE (obj
)), plist
);
147 plist
= scm_acons (scm_sym_breakpoint
, scm_from_bool (SRCPROPBRK (obj
)), plist
);
151 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
153 "Return the source property association list of @var{obj}.")
154 #define FUNC_NAME s_scm_source_properties
157 SCM_VALIDATE_NIM (1, obj
);
158 if (SCM_MEMOIZEDP (obj
))
159 obj
= SCM_MEMOIZED_EXP (obj
);
160 else if (!scm_is_pair (obj
))
161 SCM_WRONG_TYPE_ARG (1, obj
);
162 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
164 return scm_srcprops_to_plist (p
);
166 /* list from set-source-properties!, or SCM_EOL for not found */
171 /* Perhaps this procedure should look through an alist
172 and try to make a srcprops-object...? */
173 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
174 (SCM obj
, SCM plist
),
175 "Install the association list @var{plist} as the source property\n"
176 "list for @var{obj}.")
177 #define FUNC_NAME s_scm_set_source_properties_x
180 SCM_VALIDATE_NIM (1, obj
);
181 if (SCM_MEMOIZEDP (obj
))
182 obj
= SCM_MEMOIZED_EXP (obj
);
183 else if (!scm_is_pair (obj
))
184 SCM_WRONG_TYPE_ARG(1, obj
);
185 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
186 SCM_SETCDR (handle
, plist
);
191 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
193 "Return the source property specified by @var{key} from\n"
194 "@var{obj}'s source property list.")
195 #define FUNC_NAME s_scm_source_property
198 SCM_VALIDATE_NIM (1, obj
);
199 if (SCM_MEMOIZEDP (obj
))
200 obj
= SCM_MEMOIZED_EXP (obj
);
201 else if (!scm_is_pair (obj
))
202 SCM_WRONG_TYPE_ARG (1, obj
);
203 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
206 if (scm_is_eq (scm_sym_breakpoint
, key
)) p
= scm_from_bool (SRCPROPBRK (p
));
207 else if (scm_is_eq (scm_sym_line
, key
)) p
= scm_from_int (SRCPROPLINE (p
));
208 else if (scm_is_eq (scm_sym_column
, key
)) p
= scm_from_int (SRCPROPCOL (p
));
209 else if (scm_is_eq (scm_sym_filename
, key
)) p
= SRCPROPFNAME (p
);
210 else if (scm_is_eq (scm_sym_copy
, key
)) p
= SRCPROPCOPY (p
);
213 p
= SRCPROPPLIST (p
);
215 p
= scm_assoc (key
, p
);
216 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
218 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
222 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
223 (SCM obj
, SCM key
, SCM datum
),
224 "Set the source property of object @var{obj}, which is specified by\n"
225 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
226 #define FUNC_NAME s_scm_set_source_property_x
230 SCM_VALIDATE_NIM (1, obj
);
231 if (SCM_MEMOIZEDP (obj
))
232 obj
= SCM_MEMOIZED_EXP (obj
);
233 else if (!scm_is_pair (obj
))
234 SCM_WRONG_TYPE_ARG (1, obj
);
235 h
= scm_whash_get_handle (scm_source_whash
, obj
);
236 if (SCM_WHASHFOUNDP (h
))
237 p
= SCM_WHASHREF (scm_source_whash
, h
);
240 h
= scm_whash_create_handle (scm_source_whash
, obj
);
243 if (scm_is_eq (scm_sym_breakpoint
, key
))
247 if (scm_is_false (datum
))
254 SCM sp
= scm_make_srcprops (0, 0, SCM_UNDEFINED
, SCM_UNDEFINED
, p
);
255 SCM_WHASHSET (scm_source_whash
, h
, sp
);
256 if (scm_is_false (datum
))
257 CLEARSRCPROPBRK (sp
);
262 else if (scm_is_eq (scm_sym_line
, key
))
265 SETSRCPROPLINE (p
, scm_to_int (datum
));
267 SCM_WHASHSET (scm_source_whash
, h
,
268 scm_make_srcprops (scm_to_int (datum
), 0,
269 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
271 else if (scm_is_eq (scm_sym_column
, key
))
274 SETSRCPROPCOL (p
, scm_to_int (datum
));
276 SCM_WHASHSET (scm_source_whash
, h
,
277 scm_make_srcprops (0, scm_to_int (datum
),
278 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
280 else if (scm_is_eq (scm_sym_filename
, key
))
283 SRCPROPFNAME (p
) = datum
;
285 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
287 else if (scm_is_eq (scm_sym_copy
, key
))
290 SRCPROPCOPY (p
) = datum
;
292 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
297 SRCPROPPLIST (p
) = scm_acons (key
, datum
, SRCPROPPLIST (p
));
299 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
301 return SCM_UNSPECIFIED
;
309 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
310 scm_set_smob_mark (scm_tc16_srcprops
, srcprops_mark
);
311 scm_set_smob_free (scm_tc16_srcprops
, srcprops_free
);
312 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
314 scm_source_whash
= scm_make_weak_key_hash_table (scm_from_int (2047));
315 scm_c_define ("source-whash", scm_source_whash
);
317 #include "libguile/srcprop.x"
321 scm_finish_srcprop ()
323 register scm_t_srcprops_chunk
*ptr
= srcprops_chunklist
, *next
;
324 size_t n
= sizeof (scm_t_srcprops_chunk
)
325 + sizeof (scm_t_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);
329 scm_gc_unregister_collectable_memory (ptr
, n
, "srcprops");