1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2006 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"
33 #include "libguile/gc.h"
35 #include "libguile/validate.h"
36 #include "libguile/srcprop.h"
38 /* {Source Properties}
40 * Properties of source list expressions.
41 * Five of these have special meaning and optimized storage:
43 * filename string The name of the source file.
44 * copy list A copy of the list expression.
45 * line integer The source code line number.
46 * column integer The source code column number.
47 * breakpoint boolean Sets a breakpoint on this form.
49 * Most properties above can be set by the reader.
53 SCM_GLOBAL_SYMBOL (scm_sym_filename
, "filename");
54 SCM_GLOBAL_SYMBOL (scm_sym_copy
, "copy");
55 SCM_GLOBAL_SYMBOL (scm_sym_line
, "line");
56 SCM_GLOBAL_SYMBOL (scm_sym_column
, "column");
57 SCM_GLOBAL_SYMBOL (scm_sym_breakpoint
, "breakpoint");
59 scm_t_bits scm_tc16_srcprops
;
63 srcprops_print (SCM obj
, SCM port
, scm_print_state
*pstate
)
65 int writingp
= SCM_WRITINGP (pstate
);
66 scm_puts ("#<srcprops ", port
);
67 SCM_SET_WRITINGP (pstate
, 1);
68 scm_iprin1 (scm_srcprops_to_plist (obj
), port
, pstate
);
69 SCM_SET_WRITINGP (pstate
, writingp
);
76 scm_c_source_property_breakpoint_p (SCM form
)
78 SCM obj
= scm_whash_lookup (scm_source_whash
, form
);
79 return SRCPROPSP (obj
) && SRCPROPBRK (obj
);
84 scm_make_srcprops (long line
, int col
, SCM filename
, SCM copy
, SCM plist
)
86 register scm_t_srcprops
*ptr
;
88 ptr
= scm_gc_malloc (sizeof (*ptr
), "srcprop");
90 ptr
->pos
= SRCPROPMAKPOS (line
, col
);
91 ptr
->fname
= filename
;
95 SCM_RETURN_NEWSMOB (scm_tc16_srcprops
, ptr
);
100 scm_srcprops_to_plist (SCM obj
)
102 SCM plist
= SRCPROPPLIST (obj
);
103 if (!SCM_UNBNDP (SRCPROPCOPY (obj
)))
104 plist
= scm_acons (scm_sym_copy
, SRCPROPCOPY (obj
), plist
);
105 if (!SCM_UNBNDP (SRCPROPFNAME (obj
)))
106 plist
= scm_acons (scm_sym_filename
, SRCPROPFNAME (obj
), plist
);
107 plist
= scm_acons (scm_sym_column
, scm_from_int (SRCPROPCOL (obj
)), plist
);
108 plist
= scm_acons (scm_sym_line
, scm_from_int (SRCPROPLINE (obj
)), plist
);
109 plist
= scm_acons (scm_sym_breakpoint
, scm_from_bool (SRCPROPBRK (obj
)), plist
);
113 SCM_DEFINE (scm_source_properties
, "source-properties", 1, 0, 0,
115 "Return the source property association list of @var{obj}.")
116 #define FUNC_NAME s_scm_source_properties
119 SCM_VALIDATE_NIM (1, obj
);
120 if (SCM_MEMOIZEDP (obj
))
121 obj
= SCM_MEMOIZED_EXP (obj
);
122 else if (!scm_is_pair (obj
))
123 SCM_WRONG_TYPE_ARG (1, obj
);
124 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
126 return scm_srcprops_to_plist (p
);
128 /* list from set-source-properties!, or SCM_EOL for not found */
133 /* Perhaps this procedure should look through an alist
134 and try to make a srcprops-object...? */
135 SCM_DEFINE (scm_set_source_properties_x
, "set-source-properties!", 2, 0, 0,
136 (SCM obj
, SCM plist
),
137 "Install the association list @var{plist} as the source property\n"
138 "list for @var{obj}.")
139 #define FUNC_NAME s_scm_set_source_properties_x
142 SCM_VALIDATE_NIM (1, obj
);
143 if (SCM_MEMOIZEDP (obj
))
144 obj
= SCM_MEMOIZED_EXP (obj
);
145 else if (!scm_is_pair (obj
))
146 SCM_WRONG_TYPE_ARG(1, obj
);
147 handle
= scm_hashq_create_handle_x (scm_source_whash
, obj
, plist
);
153 SCM_DEFINE (scm_source_property
, "source-property", 2, 0, 0,
155 "Return the source property specified by @var{key} from\n"
156 "@var{obj}'s source property list.")
157 #define FUNC_NAME s_scm_source_property
160 SCM_VALIDATE_NIM (1, obj
);
161 if (SCM_MEMOIZEDP (obj
))
162 obj
= SCM_MEMOIZED_EXP (obj
);
163 else if (!scm_is_pair (obj
))
164 SCM_WRONG_TYPE_ARG (1, obj
);
165 p
= scm_hashq_ref (scm_source_whash
, obj
, SCM_EOL
);
168 if (scm_is_eq (scm_sym_breakpoint
, key
)) p
= scm_from_bool (SRCPROPBRK (p
));
169 else if (scm_is_eq (scm_sym_line
, key
)) p
= scm_from_int (SRCPROPLINE (p
));
170 else if (scm_is_eq (scm_sym_column
, key
)) p
= scm_from_int (SRCPROPCOL (p
));
171 else if (scm_is_eq (scm_sym_filename
, key
)) p
= SRCPROPFNAME (p
);
172 else if (scm_is_eq (scm_sym_copy
, key
)) p
= SRCPROPCOPY (p
);
175 p
= SRCPROPPLIST (p
);
177 p
= scm_assoc (key
, p
);
178 return (SCM_NIMP (p
) ? SCM_CDR (p
) : SCM_BOOL_F
);
180 return SCM_UNBNDP (p
) ? SCM_BOOL_F
: p
;
184 SCM_DEFINE (scm_set_source_property_x
, "set-source-property!", 3, 0, 0,
185 (SCM obj
, SCM key
, SCM datum
),
186 "Set the source property of object @var{obj}, which is specified by\n"
187 "@var{key} to @var{datum}. Normally, the key will be a symbol.")
188 #define FUNC_NAME s_scm_set_source_property_x
192 SCM_VALIDATE_NIM (1, obj
);
193 if (SCM_MEMOIZEDP (obj
))
194 obj
= SCM_MEMOIZED_EXP (obj
);
195 else if (!scm_is_pair (obj
))
196 SCM_WRONG_TYPE_ARG (1, obj
);
197 h
= scm_whash_get_handle (scm_source_whash
, obj
);
198 if (SCM_WHASHFOUNDP (h
))
199 p
= SCM_WHASHREF (scm_source_whash
, h
);
202 h
= scm_whash_create_handle (scm_source_whash
, obj
);
205 if (scm_is_eq (scm_sym_breakpoint
, key
))
209 if (scm_is_false (datum
))
216 SCM sp
= scm_make_srcprops (0, 0, SCM_UNDEFINED
, SCM_UNDEFINED
, p
);
217 SCM_WHASHSET (scm_source_whash
, h
, sp
);
218 if (scm_is_false (datum
))
219 CLEARSRCPROPBRK (sp
);
224 else if (scm_is_eq (scm_sym_line
, key
))
227 SETSRCPROPLINE (p
, scm_to_int (datum
));
229 SCM_WHASHSET (scm_source_whash
, h
,
230 scm_make_srcprops (scm_to_int (datum
), 0,
231 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
233 else if (scm_is_eq (scm_sym_column
, key
))
236 SETSRCPROPCOL (p
, scm_to_int (datum
));
238 SCM_WHASHSET (scm_source_whash
, h
,
239 scm_make_srcprops (0, scm_to_int (datum
),
240 SCM_UNDEFINED
, SCM_UNDEFINED
, p
));
242 else if (scm_is_eq (scm_sym_filename
, key
))
245 SRCPROPFNAME (p
) = datum
;
247 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, datum
, SCM_UNDEFINED
, p
));
249 else if (scm_is_eq (scm_sym_copy
, key
))
252 SRCPROPCOPY (p
) = datum
;
254 SCM_WHASHSET (scm_source_whash
, h
, scm_make_srcprops (0, 0, SCM_UNDEFINED
, datum
, p
));
259 SRCPROPPLIST (p
) = scm_acons (key
, datum
, SRCPROPPLIST (p
));
261 SCM_WHASHSET (scm_source_whash
, h
, scm_acons (key
, datum
, p
));
263 return SCM_UNSPECIFIED
;
271 scm_tc16_srcprops
= scm_make_smob_type ("srcprops", 0);
272 scm_set_smob_print (scm_tc16_srcprops
, srcprops_print
);
274 scm_source_whash
= scm_make_weak_key_hash_table (scm_from_int (2047));
275 scm_c_define ("source-whash", scm_source_whash
);
277 #include "libguile/srcprop.x"
281 scm_finish_srcprop ()