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_CELL_WORD_1 (obj
)) = srcprops_freelist
;
75 srcprops_freelist
= (scm_t_srcprops
*) SCM_CELL_WORD_1 (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");
118 mem
->next
= srcprops_chunklist
;
119 srcprops_chunklist
= mem
;
120 ptr
= &mem
->srcprops
[0];
121 for (i
= 1; i
< SRCPROPS_CHUNKSIZE
- 1; ++i
)
122 *(scm_t_srcprops
**)&ptr
[i
] = &ptr
[i
+ 1];
123 *(scm_t_srcprops
**)&ptr
[SRCPROPS_CHUNKSIZE
- 1] = 0;
124 srcprops_freelist
= (scm_t_srcprops
*) &ptr
[1];
126 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
127 ptr
->fname
= filename
;
131 SCM_RETURN_NEWSMOB (scm_tc16_srcprops
, ptr
);
136 scm_srcprops_to_plist (SCM obj
)
138 SCM plist
= SRCPROPPLIST (obj
);
139 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
140 plist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), plist
);
141 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
142 plist
= scm_acons (scm_sym_filename
, SRCPROPFNAME (obj
), plist
);
143 plist
= scm_acons (scm_sym_column
, SCM_MAKINUM (SRCPROPCOL (obj
)), plist
);
144 plist
= scm_acons (scm_sym_line
, SCM_MAKINUM (SRCPROPLINE (obj
)), plist
);
145 plist
= scm_acons (scm_sym_breakpoint
, SCM_BOOL (SRCPROPBRK (obj
)), plist
);
149 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
151 "Return the source property association list of @var{obj}.")
152 #define FUNC_NAME s_scm_source_properties
155 SCM_VALIDATE_NIM (1, obj
);
156 if (SCM_MEMOIZEDP (obj
))
157 obj
= SCM_MEMOIZED_EXP (obj
);
158 else if (!SCM_CONSP (obj
))
159 SCM_WRONG_TYPE_ARG (1, obj
);
160 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_BOOL_F
);
162 return scm_srcprops_to_plist (p
);
167 /* Perhaps this procedure should look through an alist
168 and try to make a srcprops-object...? */
169 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
170 (SCM obj
, SCM plist
),
171 "Install the association list @var{plist} as the source property\n"
172 "list for @var{obj}.")
173 #define FUNC_NAME s_scm_set_source_properties_x
176 SCM_VALIDATE_NIM (1, obj
);
177 if (SCM_MEMOIZEDP (obj
))
178 obj
= SCM_MEMOIZED_EXP (obj
);
179 else if (!SCM_CONSP (obj
))
180 SCM_WRONG_TYPE_ARG(1, obj
);
181 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
182 SCM_SETCDR (handle
, plist
);
187 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
189 "Return the source property specified by @var{key} from\n"
190 "@var{obj}'s source property list.")
191 #define FUNC_NAME s_scm_source_property
194 SCM_VALIDATE_NIM (1, obj
);
195 if (SCM_MEMOIZEDP (obj
))
196 obj
= SCM_MEMOIZED_EXP (obj
);
197 else if (!SCM_CONSP (obj
))
198 SCM_WRONG_TYPE_ARG (1, obj
);
199 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
202 if (SCM_EQ_P (scm_sym_breakpoint
, key
)) p
= SCM_BOOL (SRCPROPBRK (p
));
203 else if (SCM_EQ_P (scm_sym_line
, key
)) p
= SCM_MAKINUM (SRCPROPLINE (p
));
204 else if (SCM_EQ_P (scm_sym_column
, key
)) p
= SCM_MAKINUM (SRCPROPCOL (p
));
205 else if (SCM_EQ_P (scm_sym_filename
, key
)) p
= SRCPROPFNAME (p
);
206 else if (SCM_EQ_P (scm_sym_copy
, key
)) p
= SRCPROPCOPY (p
);
209 p
= SRCPROPPLIST (p
);
211 p
= scm_assoc (key
, p
);
212 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
214 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
218 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
219 (SCM obj
, SCM key
, SCM datum
),
220 "Set the source property of object @var{obj}, which is specified by\n"
221 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
222 #define FUNC_NAME s_scm_set_source_property_x
226 SCM_VALIDATE_NIM (1, obj
);
227 if (SCM_MEMOIZEDP (obj
))
228 obj
= SCM_MEMOIZED_EXP (obj
);
229 else if (!SCM_CONSP (obj
))
230 SCM_WRONG_TYPE_ARG (1, obj
);
231 h
= scm_whash_get_handle (scm_source_whash
, obj
);
232 if (SCM_WHASHFOUNDP (h
))
233 p
= SCM_WHASHREF (scm_source_whash
, h
);
236 h
= scm_whash_create_handle (scm_source_whash
, obj
);
239 if (SCM_EQ_P (scm_sym_breakpoint
, key
))
243 if (SCM_FALSEP (datum
))
250 SCM sp
= scm_make_srcprops (0, 0, SCM_UNDEFINED
, SCM_UNDEFINED
, p
);
251 SCM_WHASHSET (scm_source_whash
, h
, sp
);
252 if (SCM_FALSEP (datum
))
253 CLEARSRCPROPBRK (sp
);
258 else if (SCM_EQ_P (scm_sym_line
, key
))
260 SCM_VALIDATE_INUM (3, datum
);
262 SETSRCPROPLINE (p
, SCM_INUM (datum
));
264 SCM_WHASHSET (scm_source_whash
, h
,
265 scm_make_srcprops (SCM_INUM (datum
), 0,
266 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
268 else if (SCM_EQ_P (scm_sym_column
, key
))
270 SCM_VALIDATE_INUM (3, datum
);
272 SETSRCPROPCOL (p
, SCM_INUM (datum
));
274 SCM_WHASHSET (scm_source_whash
, h
,
275 scm_make_srcprops (0, SCM_INUM (datum
),
276 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
278 else if (SCM_EQ_P (scm_sym_filename
, key
))
281 SRCPROPFNAME (p
) = datum
;
283 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
285 else if (SCM_EQ_P (scm_sym_copy
, key
))
288 SRCPROPCOPY (p
) = datum
;
290 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
295 SRCPROPPLIST (p
) = scm_acons (key
, datum
, SRCPROPPLIST (p
));
297 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
299 return SCM_UNSPECIFIED
;
307 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
308 scm_set_smob_mark (scm_tc16_srcprops
, srcprops_mark
);
309 scm_set_smob_free (scm_tc16_srcprops
, srcprops_free
);
310 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
312 scm_source_whash
= scm_make_weak_key_hash_table (SCM_MAKINUM (2047));
313 scm_c_define ("source-whash", scm_source_whash
);
315 #include "libguile/srcprop.x"
319 scm_finish_srcprop ()
321 register scm_t_srcprops_chunk
*ptr
= srcprops_chunklist
, *next
;
326 scm_mallocated
-= sizeof (scm_t_srcprops_chunk
)
327 + sizeof (scm_t_srcprops
) * (SRCPROPS_CHUNKSIZE
- 1);