libguile/Makefile.am (snarfcppopts): Remove CFLAGS
[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,
7 * 2004, 2006, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25 \f
26
27 /* Macros for snarfing initialization actions from C source. */
28
29 /* Casting to a function that can take any number of arguments. */
30 #define SCM_FUNC_CAST_ARBITRARY_ARGS scm_t_subr
31
32
33 #ifdef SCM_ALIGNED
34 /* We support static allocation of some `SCM' objects. */
35 # define SCM_SUPPORT_STATIC_ALLOCATION
36 #endif
37
38 /* C preprocessor token concatenation. */
39 #define scm_i_paste(x, y) x ## y
40 #define scm_i_paste3(a, b, c) a ## b ## c
41
42
43 \f
44 /* Generic macros to be used in user macro definitions.
45 *
46 * For example, in order to define a macro which creates ints and
47 * initializes them to the result of foo (), do:
48 *
49 * #define SCM_FOO(NAME) \
50 * SCM_SNARF_HERE (int NAME) \
51 * SCM_SNARF_INIT (NAME = foo ())
52 *
53 * The SCM_SNARF_INIT text goes into the corresponding .x file
54 * up through the first occurrence of SCM_SNARF_DOC_START on that
55 * line, if any.
56 *
57 * Some debugging options can cause the preprocessor to echo #define
58 * directives to its output. Keeping the snarfing markers on separate
59 * lines prevents guile-snarf from inadvertently snarfing the definition
60 * of SCM_SNARF_INIT if those options are in effect.
61 */
62
63 #ifdef SCM_MAGIC_SNARF_INITS
64 # define SCM_SNARF_HERE(X)
65 # define SCM_SNARF_INIT_PREFIX ^^
66 # define SCM_SNARF_INIT(X) SCM_SNARF_INIT_PREFIX X ^:^
67 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
68 #else
69 # ifdef SCM_MAGIC_SNARF_DOCS
70 # define SCM_SNARF_HERE(X)
71 # define SCM_SNARF_INIT(X)
72 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING) \
73 ^^ { \
74 cname CNAME ^^ \
75 fname FNAME ^^ \
76 type TYPE ^^ \
77 location __FILE__ __LINE__ ^^ \
78 arglist ARGLIST ^^ \
79 argsig REQ OPT VAR ^^ \
80 DOCSTRING ^^ }
81 # else
82 # define SCM_SNARF_HERE(X) X
83 # define SCM_SNARF_INIT(X)
84 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
85 # endif
86 #endif
87
88 #define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
89 SCM_SNARF_HERE(\
90 SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
91 SCM FNAME ARGLIST\
92 )\
93 SCM_SNARF_INIT(\
94 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
95 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
96 )\
97 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
98
99 /* Always use the generic subr case. */
100 #define SCM_DEFINE SCM_DEFINE_GSUBR
101
102
103 #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
104 SCM_SNARF_HERE(\
105 SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
106 static SCM g_ ## FNAME; \
107 SCM FNAME ARGLIST\
108 )\
109 SCM_SNARF_INIT(\
110 g_ ## FNAME = SCM_PACK (0); \
111 scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
112 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
113 &g_ ## FNAME); \
114 )\
115 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
116
117 #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
118 SCM_SNARF_HERE(\
119 SCM_UNUSED static const char s_ ## FNAME [] = PRIMNAME; \
120 SCM FNAME ARGLIST\
121 )\
122 SCM_SNARF_INIT(\
123 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
124 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
125 scm_c_export (s_ ## FNAME, NULL); \
126 )\
127 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
128
129 #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
130 SCM_SNARF_HERE(SCM_UNUSED static const char RANAME[]=STR) \
131 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
132 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
133
134 #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
135 SCM_SNARF_HERE(SCM_UNUSED 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 SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
139 "implemented by the C function \"" #CFN "\"")
140
141 #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
142 SCM_SNARF_HERE(\
143 SCM_UNUSED static const char RANAME[]=STR;\
144 static SCM GF \
145 )SCM_SNARF_INIT(\
146 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
147 scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
148 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
149 )
150
151 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
152
153 # define SCM_SYMBOL(c_name, scheme_name) \
154 SCM_SNARF_HERE( \
155 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
156 static SCM c_name) \
157 SCM_SNARF_INIT( \
158 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
159 )
160
161 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
162 SCM_SNARF_HERE( \
163 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
164 SCM c_name) \
165 SCM_SNARF_INIT( \
166 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
167 )
168
169 #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
170
171 # define SCM_SYMBOL(c_name, scheme_name) \
172 SCM_SNARF_HERE(static SCM c_name) \
173 SCM_SNARF_INIT(c_name = scm_from_utf8_symbol (scheme_name))
174
175 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
176 SCM_SNARF_HERE(SCM c_name) \
177 SCM_SNARF_INIT(c_name = scm_from_utf8_symbol (scheme_name))
178
179 #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
180
181 #define SCM_KEYWORD(c_name, scheme_name) \
182 SCM_SNARF_HERE(static SCM c_name) \
183 SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
184
185 #define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
186 SCM_SNARF_HERE(SCM c_name) \
187 SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
188
189 #define SCM_VARIABLE(c_name, scheme_name) \
190 SCM_SNARF_HERE(static SCM c_name) \
191 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
192
193 #define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
194 SCM_SNARF_HERE(SCM c_name) \
195 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
196
197 #define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
198 SCM_SNARF_HERE(static SCM c_name) \
199 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
200
201 #define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
202 SCM_SNARF_HERE(SCM c_name) \
203 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
204
205 #define SCM_MUTEX(c_name) \
206 SCM_SNARF_HERE(static scm_t_mutex c_name) \
207 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
208
209 #define SCM_GLOBAL_MUTEX(c_name) \
210 SCM_SNARF_HERE(scm_t_mutex c_name) \
211 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
212
213 #define SCM_REC_MUTEX(c_name) \
214 SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
215 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
216
217 #define SCM_GLOBAL_REC_MUTEX(c_name) \
218 SCM_SNARF_HERE(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_SMOB(tag, scheme_name, size) \
222 SCM_SNARF_HERE(static scm_t_bits tag) \
223 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
224
225 #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
226 SCM_SNARF_HERE(scm_t_bits tag) \
227 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
228
229 #define SCM_SMOB_MARK(tag, c_name, arg) \
230 SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
231 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
232
233 #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
234 SCM_SNARF_HERE(SCM c_name(SCM arg)) \
235 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
236
237 #define SCM_SMOB_FREE(tag, c_name, arg) \
238 SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
239 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
240
241 #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
242 SCM_SNARF_HERE(size_t c_name(SCM arg)) \
243 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
244
245 #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
246 SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
247 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
248
249 #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
250 SCM_SNARF_HERE(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_SMOB_EQUALP(tag, c_name, obj1, obj2) \
254 SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
255 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
256
257 #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
258 SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
259 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
260
261 #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
262 SCM_SNARF_HERE(static SCM c_name arglist) \
263 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
264
265 #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
266 SCM_SNARF_HERE(SCM c_name arglist) \
267 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
268
269 \f
270 /* Low-level snarfing for static memory allocation. */
271
272 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
273
274 #define SCM_IMMUTABLE_CELL(c_name, car, cdr) \
275 static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
276 c_name ## _raw_scell = \
277 { \
278 SCM_PACK (car), \
279 SCM_PACK (cdr) \
280 }; \
281 static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_scell)
282
283 #define SCM_IMMUTABLE_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
284 static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
285 c_name ## _raw_cell [2] = \
286 { \
287 { SCM_PACK (car), SCM_PACK (cbr) }, \
288 { SCM_PACK (ccr), SCM_PACK (cdr) } \
289 }; \
290 static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_cell)
291
292 #define SCM_STATIC_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
293 static SCM_ALIGNED (8) SCM_UNUSED scm_t_cell \
294 c_name ## _raw_cell [2] = \
295 { \
296 { SCM_PACK (car), SCM_PACK (cbr) }, \
297 { SCM_PACK (ccr), SCM_PACK (cdr) } \
298 }; \
299 static SCM_UNUSED SCM c_name = SCM_PACK (& c_name ## _raw_cell)
300
301 #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
302 static SCM_UNUSED const \
303 struct \
304 { \
305 scm_t_bits word_0; \
306 scm_t_bits word_1; \
307 const char buffer[sizeof (contents)]; \
308 } \
309 c_name = \
310 { \
311 scm_tc7_stringbuf | SCM_I_STRINGBUF_F_SHARED, \
312 sizeof (contents) - 1, \
313 contents \
314 }
315
316 #define SCM_IMMUTABLE_STRING(c_name, contents) \
317 SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
318 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
319 scm_tc7_ro_string, \
320 (scm_t_bits) &scm_i_paste (c_name, \
321 _stringbuf), \
322 (scm_t_bits) 0, \
323 (scm_t_bits) (sizeof (contents) - 1))
324
325 #define SCM_IMMUTABLE_POINTER(c_name, ptr) \
326 SCM_IMMUTABLE_CELL (c_name, scm_tc7_pointer, ptr)
327
328 #endif /* SCM_SUPPORT_STATIC_ALLOCATION */
329
330 \f
331 /* Documentation. */
332
333 #ifdef SCM_MAGIC_SNARF_DOCS
334 #undef SCM_ASSERT
335 #define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
336 #endif /* SCM_MAGIC_SNARF_DOCS */
337
338 #endif /* SCM_SNARF_H */
339
340 /*
341 Local Variables:
342 c-file-style: "gnu"
343 End:
344 */