6 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
26 /* Macros for snarfing initialization actions from C source. */
28 #if defined(__cplusplus) || defined(GUILE_CPLUSPLUS_SNARF)
30 /* This used to be "SCM (*)(...)" but GCC on RedHat 7.1 doesn't seem
33 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
36 #define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
39 #if (defined SCM_ALIGNED) && (SCM_DEBUG_TYPING_STRICTNESS <= 1)
40 /* We support static allocation of some `SCM' objects. */
41 # define SCM_SUPPORT_STATIC_ALLOCATION
44 /* C preprocessor token concatenation. */
45 #define scm_i_paste(x, y) x ## y
46 #define scm_i_paste3(a, b, c) a ## b ## c
50 /* Generic macros to be used in user macro definitions.
52 * For example, in order to define a macro which creates ints and
53 * initializes them to the result of foo (), do:
55 * #define SCM_FOO(NAME) \
56 * SCM_SNARF_HERE (int NAME) \
57 * SCM_SNARF_INIT (NAME = foo ())
59 * The SCM_SNARF_INIT text goes into the corresponding .x file
60 * up through the first occurrence of SCM_SNARF_DOC_START on that
64 #ifdef SCM_MAGIC_SNARF_INITS
65 # define SCM_SNARF_HERE(X)
66 # define SCM_SNARF_INIT(X) ^^ X ^:^
67 # define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
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) \
77 location __FILE__ __LINE__ ^^ \
79 argsig REQ OPT VAR ^^ \
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)
88 #define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
90 static const char s_ ## FNAME [] = PRIMNAME; \
94 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
95 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
97 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
99 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
101 /* Static subr allocation. */
102 #define SCM_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
103 SCM_SYMBOL (scm_i_paste (FNAME, __name), PRIMNAME); \
105 static const char scm_i_paste (s_, FNAME) [] = PRIMNAME; \
106 SCM_API SCM FNAME ARGLIST; \
107 SCM_IMMUTABLE_SUBR (scm_i_paste (FNAME, __subr), \
108 scm_i_paste (FNAME, __name), \
109 REQ, OPT, VAR, &FNAME); \
113 /* Initialize the procedure name (an interned symbol). */ \
114 scm_i_paste (FNAME, __subr_meta_info)[0] = scm_i_paste (FNAME, __name); \
116 /* Define the subr. */ \
117 scm_c_define (scm_i_paste (s_, FNAME), scm_i_paste (FNAME, __subr)); \
119 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
121 #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
123 /* Always use the generic subr case. */
124 #define SCM_DEFINE SCM_DEFINE_GSUBR
126 #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
129 #define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
131 static const char s_ ## FNAME [] = PRIMNAME; \
132 static SCM g_ ## FNAME; \
136 g_ ## FNAME = SCM_PACK (0); \
137 scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
138 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
141 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
143 #define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
145 static const char s_ ## FNAME [] = PRIMNAME; \
149 scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
150 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
151 scm_c_export (s_ ## FNAME, NULL); \
153 SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
155 #define SCM_DEFINE1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
157 static const char s_ ## FNAME [] = PRIMNAME; \
160 SCM_SNARF_INIT(scm_c_define_subr (s_ ## FNAME, TYPE, FNAME); ) \
161 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
163 #define SCM_PRIMITIVE_GENERIC_1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
165 static const char s_ ## FNAME [] = PRIMNAME; \
166 static SCM g_ ## FNAME; \
170 g_ ## FNAME = SCM_PACK (0); \
171 scm_c_define_subr_with_generic (s_ ## FNAME, TYPE, FNAME, &g_ ## FNAME); \
173 SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
175 #define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
176 SCM_SNARF_HERE(static const char RANAME[]=STR) \
177 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
178 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
180 #define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
181 SCM_SNARF_HERE(static const char RANAME[]=STR) \
182 SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
183 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
184 SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
185 "implemented by the C function \"" #CFN "\"")
187 #define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
189 static const char RANAME[]=STR;\
192 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
193 scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
194 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
197 #define SCM_PROC1(RANAME, STR, TYPE, CFN) \
198 SCM_SNARF_HERE(static const char RANAME[]=STR) \
200 scm_c_define_subr (RANAME, TYPE, (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN) \
204 #define SCM_GPROC1(RANAME, STR, TYPE, CFN, GF) \
206 static const char RANAME[]=STR; \
209 GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
210 scm_c_define_subr_with_generic (RANAME, TYPE, \
211 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
214 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
216 # define SCM_SYMBOL(c_name, scheme_name) \
218 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
221 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
224 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
226 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
229 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
232 #else /* !SCM_SUPPORT_STATIC_ALLOCATION */
234 # define SCM_SYMBOL(c_name, scheme_name) \
235 SCM_SNARF_HERE(static SCM c_name) \
236 SCM_SNARF_INIT(c_name = scm_from_locale_symbol (scheme_name))
238 # define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
239 SCM_SNARF_HERE(SCM c_name) \
240 SCM_SNARF_INIT(c_name = scm_from_locale_symbol (scheme_name))
242 #endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
244 #define SCM_KEYWORD(c_name, scheme_name) \
245 SCM_SNARF_HERE(static SCM c_name) \
246 SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
248 #define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
249 SCM_SNARF_HERE(SCM c_name) \
250 SCM_SNARF_INIT(c_name = scm_from_locale_keyword (scheme_name))
252 #define SCM_VARIABLE(c_name, scheme_name) \
253 SCM_SNARF_HERE(static SCM c_name) \
254 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
256 #define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
257 SCM_SNARF_HERE(SCM c_name) \
258 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, SCM_BOOL_F);)
260 #define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
261 SCM_SNARF_HERE(static SCM c_name) \
262 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
264 #define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
265 SCM_SNARF_HERE(SCM c_name) \
266 SCM_SNARF_INIT(c_name = scm_c_define (scheme_name, init_val);)
268 #define SCM_MUTEX(c_name) \
269 SCM_SNARF_HERE(static scm_t_mutex c_name) \
270 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
272 #define SCM_GLOBAL_MUTEX(c_name) \
273 SCM_SNARF_HERE(scm_t_mutex c_name) \
274 SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
276 #define SCM_REC_MUTEX(c_name) \
277 SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
278 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
280 #define SCM_GLOBAL_REC_MUTEX(c_name) \
281 SCM_SNARF_HERE(scm_t_rec_mutex c_name) \
282 SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
284 #define SCM_SMOB(tag, scheme_name, size) \
285 SCM_SNARF_HERE(static scm_t_bits tag) \
286 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
288 #define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
289 SCM_SNARF_HERE(scm_t_bits tag) \
290 SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
292 #define SCM_SMOB_MARK(tag, c_name, arg) \
293 SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
294 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
296 #define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
297 SCM_SNARF_HERE(SCM c_name(SCM arg)) \
298 SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
300 #define SCM_SMOB_FREE(tag, c_name, arg) \
301 SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
302 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
304 #define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
305 SCM_SNARF_HERE(size_t c_name(SCM arg)) \
306 SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
308 #define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
309 SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
310 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
312 #define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
313 SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
314 SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
316 #define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
317 SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
318 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
320 #define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
321 SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
322 SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
324 #define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
325 SCM_SNARF_HERE(static SCM c_name arglist) \
326 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
328 #define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
329 SCM_SNARF_HERE(SCM c_name arglist) \
330 SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
333 /* Low-level snarfing for static memory allocation. */
335 #ifdef SCM_SUPPORT_STATIC_ALLOCATION
337 #define SCM_IMMUTABLE_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
338 static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
339 c_name ## _raw_cell [2] = \
341 { SCM_PACK (car), SCM_PACK (cbr) }, \
342 { SCM_PACK (ccr), SCM_PACK (cdr) } \
344 static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_cell)
346 #define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
347 static SCM_UNUSED const \
352 const char buffer[sizeof (contents)]; \
356 scm_tc7_stringbuf | SCM_I_STRINGBUF_F_SHARED, \
357 sizeof (contents) - 1, \
361 #define SCM_IMMUTABLE_STRING(c_name, contents) \
362 SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
363 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
365 (scm_t_bits) &scm_i_paste (c_name, \
368 (scm_t_bits) sizeof (contents) - 1)
370 #define SCM_IMMUTABLE_SUBR(c_name, name, req, opt, rest, fcn) \
371 static SCM_UNUSED SCM scm_i_paste (c_name, _meta_info)[2] = \
373 SCM_BOOL_F, /* The name, initialized at run-time. */ \
374 SCM_EOL /* The procedure properties. */ \
376 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
377 SCM_SUBR_ARITY_TO_TYPE (req, opt, rest), \
379 (scm_t_bits) 0 /* no generic */, \
380 (scm_t_bits) & scm_i_paste (c_name, _meta_info));
382 #endif /* SCM_SUPPORT_STATIC_ALLOCATION */
387 #ifdef SCM_MAGIC_SNARF_DOCS
389 #define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
390 #endif /* SCM_MAGIC_SNARF_DOCS */
392 #endif /* SCM_SNARF_H */