Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / snarf.h
1 /* classes: h_files */
2
3 #ifndef SCM_SNARF_H
4 #define SCM_SNARF_H
5
6 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 /* Macros for snarfing initialization actions from C source. */
27
28 #if defined(__cplusplus) || defined(GUILE_CPLUSPLUS_SNARF)
29
30 /* This used to be "SCM (*)(...)" but GCC on RedHat 7.1 doesn't seem
31 to like it.
32 */
33 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
34
35 #else
36 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
37 #endif
38
39 /* Generic macros to be used in user macro definitions.
40 *
41 * For example, in order to define a macro which creates ints and
42 * initializes them to the result of foo (), do:
43 *
44 * #define SCM_FOO(NAME) \
45 * SCM_SNARF_HERE (int NAME) \
46 * SCM_SNARF_INIT (NAME = foo ())
47 *
48 * The SCM_SNARF_INIT text goes into the corresponding .x file
49 * up through the first occurrence of SCM_SNARF_DOC_START on that
50 * line, if any.
51 */
52
53 #ifdef SCM_MAGIC_SNARF_INITS
54 # define SCM_SNARF_HERE(X)
55 # define SCM_SNARF_INIT(X) ^^ X ^:^
56 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
57 #else
58 # ifdef SCM_MAGIC_SNARF_DOCS
59 # define SCM_SNARF_HERE(X)
60 # define SCM_SNARF_INIT(X)
61 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING) \
62 ^^ { \
63 cname CNAME ^^ \
64 fname FNAME ^^ \
65 type TYPE ^^ \
66 location __FILE__ __LINE__ ^^ \
67 arglist ARGLIST ^^ \
68 argsig REQ OPT VAR ^^ \
69 DOCSTRING ^^ }
70 # else
71 # define SCM_SNARF_HERE(X) X
72 # define SCM_SNARF_INIT(X)
73 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
74 # endif
75 #endif
76
77 #define SCM_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
78 SCM_SNARF_HERE(\
79 static const char s_ ## FNAME [] = PRIMNAME; \
80 SCM FNAME ARGLIST\
81 )\
82 SCM_SNARF_INIT(\
83 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
84 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
85 )\
86 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
87
88 #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
89 SCM_SNARF_HERE(\
90 static const char s_ ## FNAME [] = PRIMNAME; \
91 static SCM g_ ## FNAME; \
92 SCM FNAME ARGLIST\
93 )\
94 SCM_SNARF_INIT(\
95 g_ ## FNAME = SCM_PACK (0); \
96 scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
97 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
98 &g_ ## FNAME); \
99 )\
100 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
101
102 #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
103 SCM_SNARF_HERE(\
104 static const char s_ ## FNAME [] = PRIMNAME; \
105 SCM FNAME ARGLIST\
106 )\
107 SCM_SNARF_INIT(\
108 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
109 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
110 scm_c_export (s_ ## FNAME, NULL); \
111 )\
112 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
113
114 #define SCM_DEFINE1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
115 SCM_SNARF_HERE(\
116 static const char s_ ## FNAME [] = PRIMNAME; \
117 SCM FNAME ARGLIST\
118 )\
119 SCM_SNARF_INIT(scm_c_define_subr (s_ ## FNAME, TYPE, FNAME); ) \
120 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
121
122 #define SCM_PRIMITIVE_GENERIC_1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
123 SCM_SNARF_HERE(\
124 static const char s_ ## FNAME [] = PRIMNAME; \
125 static SCM g_ ## FNAME; \
126 SCM FNAME ARGLIST\
127 )\
128 SCM_SNARF_INIT(\
129 g_ ## FNAME = SCM_PACK (0); \
130 scm_c_define_subr_with_generic (s_ ## FNAME, TYPE, FNAME, &g_ ## FNAME); \
131 )\
132 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
133
134 #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
135 SCM_SNARF_HERE(static const char RANAME[]=STR) \
136 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
137 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
138
139 #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
140 SCM_SNARF_HERE(static const char RANAME[]=STR) \
141 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
142 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
143 SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
144 "implemented by the C function \"" #CFN "\"")
145
146 #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
147 SCM_SNARF_HERE(\
148 static const char RANAME[]=STR;\
149 static SCM GF \
150 )SCM_SNARF_INIT(\
151 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
152 scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
153 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
154 )
155
156 #define SCM_PROC1(RANAME, STR, TYPE, CFN) \
157 SCM_SNARF_HERE(static const char RANAME[]=STR) \
158 SCM_SNARF_INIT(\
159 scm_c_define_subr (RANAME, TYPE, (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN) \
160 )
161
162
163 #define SCM_GPROC1(RANAME, STR, TYPE, CFN, GF) \
164 SCM_SNARF_HERE(\
165 static const char RANAME[]=STR; \
166 static SCM GF \
167 )SCM_SNARF_INIT(\
168 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
169 scm_c_define_subr_with_generic (RANAME, TYPE, \
170 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
171 )
172
173 #define SCM_SYNTAX(RANAME, STR, TYPE, CFN) \
174 SCM_SNARF_HERE(static const char RANAME[]=STR)\
175 SCM_SNARF_INIT(scm_make_synt (RANAME, TYPE, CFN))
176
177 #define SCM_SYMBOL(c_name, scheme_name) \
178 SCM_SNARF_HERE(static SCM c_name) \
179 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
180
181 #define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
182 SCM_SNARF_HERE(SCM c_name) \
183 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
184
185 #define SCM_KEYWORD(c_name, scheme_name) \
186 SCM_SNARF_HERE(static SCM c_name) \
187 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
188
189 #define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
190 SCM_SNARF_HERE(SCM c_name) \
191 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
192
193 #define SCM_VARIABLE(c_name, scheme_name) \
194 SCM_SNARF_HERE(static SCM c_name) \
195 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
196
197 #define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
198 SCM_SNARF_HERE(SCM c_name) \
199 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
200
201 #define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
202 SCM_SNARF_HERE(static SCM c_name) \
203 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
204
205 #define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
206 SCM_SNARF_HERE(SCM c_name) \
207 SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
208
209 #define SCM_MUTEX(c_name) \
210 SCM_SNARF_HERE(static scm_t_mutex c_name) \
211 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
212
213 #define SCM_GLOBAL_MUTEX(c_name) \
214 SCM_SNARF_HERE(scm_t_mutex c_name) \
215 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
216
217 #define SCM_REC_MUTEX(c_name) \
218 SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
219 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
220
221 #define SCM_GLOBAL_REC_MUTEX(c_name) \
222 SCM_SNARF_HERE(scm_t_rec_mutex c_name) \
223 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
224
225 #define SCM_SMOB(tag, scheme_name, size) \
226 SCM_SNARF_HERE(static scm_t_bits tag) \
227 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
228
229 #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
230 SCM_SNARF_HERE(scm_t_bits tag) \
231 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
232
233 #define SCM_SMOB_MARK(tag, c_name, arg) \
234 SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
235 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
236
237 #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
238 SCM_SNARF_HERE(SCM c_name(SCM arg)) \
239 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
240
241 #define SCM_SMOB_FREE(tag, c_name, arg) \
242 SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
243 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
244
245 #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
246 SCM_SNARF_HERE(size_t c_name(SCM arg)) \
247 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
248
249 #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
250 SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
251 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
252
253 #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
254 SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
255 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
256
257 #define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
258 SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
259 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
260
261 #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
262 SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
263 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
264
265 #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
266 SCM_SNARF_HERE(static SCM c_name arglist) \
267 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
268
269 #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
270 SCM_SNARF_HERE(SCM c_name arglist) \
271 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
272
273
274 #ifdef SCM_MAGIC_SNARF_DOCS
275 #undef SCM_ASSERT
276 #define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
277 #endif /* SCM_MAGIC_SNARF_DOCS */
278
279 #endif /* SCM_SNARF_H */
280
281 /*
282 Local Variables:
283 c-file-style: "gnu"
284 End:
285 */