Merge branch 'boehm-demers-weiser-gc' into bdw-gc-static-alloc
[bpt/guile.git] / libguile / snarf.h
CommitLineData
5b4215a8
JB
1/* classes: h_files */
2
b29058ff
DH
3#ifndef SCM_SNARF_H
4#define SCM_SNARF_H
5b4215a8 5
c6054fea 6/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
b29058ff 7 *
73be1d9e 8 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
b29058ff 12 *
53befeb7
NJ
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
b29058ff 17 *
73be1d9e
MV
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
53befeb7
NJ
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
73be1d9e 22 */
1bbd0b84 23
5b4215a8
JB
24\f
25
b29058ff
DH
26/* Macros for snarfing initialization actions from C source. */
27
4b4d0898 28#if defined(__cplusplus) || defined(GUILE_CPLUSPLUS_SNARF)
e1fbffa9
MV
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
bfe19b46 35#else
4b4d0898 36#define SCM_FUNC_CAST_ARBITRARY_ARGS SCM (*)()
bfe19b46
MD
37#endif
38
c6054fea
LC
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
42#endif
43
b68095f0
LC
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
47
c6054fea
LC
48
49\f
73d8385e
MD
50/* Generic macros to be used in user macro definitions.
51 *
52 * For example, in order to define a macro which creates ints and
53 * initializes them to the result of foo (), do:
54 *
55 * #define SCM_FOO(NAME) \
94a70859
MD
56 * SCM_SNARF_HERE (int NAME) \
57 * SCM_SNARF_INIT (NAME = foo ())
1e76143f 58 *
94a70859 59 * The SCM_SNARF_INIT text goes into the corresponding .x file
3c6d9d71
DH
60 * up through the first occurrence of SCM_SNARF_DOC_START on that
61 * line, if any.
73d8385e
MD
62 */
63
c99f9605
ML
64#ifdef SCM_MAGIC_SNARF_INITS
65# define SCM_SNARF_HERE(X)
09e804ff 66# define SCM_SNARF_INIT(X) ^^ X ^:^
cecb4a5e 67# define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
1e76143f 68#else
c99f9605 69# ifdef SCM_MAGIC_SNARF_DOCS
94a70859 70# define SCM_SNARF_HERE(X)
c99f9605 71# define SCM_SNARF_INIT(X)
cecb4a5e 72# define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING) \
ac13d9d2 73^^ { \
cecb4a5e 74cname CNAME ^^ \
ac13d9d2
ML
75fname FNAME ^^ \
76type TYPE ^^ \
77location __FILE__ __LINE__ ^^ \
78arglist ARGLIST ^^ \
79argsig REQ OPT VAR ^^ \
80DOCSTRING ^^ }
c99f9605
ML
81# else
82# define SCM_SNARF_HERE(X) X
83# define SCM_SNARF_INIT(X)
cecb4a5e 84# define SCM_SNARF_DOCS(TYPE, CNAME, FNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
c99f9605 85# endif
1e76143f 86#endif
73d8385e 87
46f9baf4 88#define SCM_DEFINE_GSUBR(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
94a70859 89SCM_SNARF_HERE(\
4b4d0898
GB
90static const char s_ ## FNAME [] = PRIMNAME; \
91SCM FNAME ARGLIST\
92)\
94a70859 93SCM_SNARF_INIT(\
9a441ddb
MV
94scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
95 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
2fdcf8bd 96)\
cecb4a5e 97SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
1bbd0b84 98
46f9baf4
LC
99#ifdef SCM_SUPPORT_STATIC_ALLOCATION
100
f0eb5ae6
LC
101/* Static subr allocation. */
102#define SCM_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
46f9baf4
LC
103SCM_SYMBOL (scm_i_paste (FNAME, __name), PRIMNAME); \
104SCM_SNARF_HERE( \
105 static const char scm_i_paste (s_, FNAME) [] = PRIMNAME; \
106 SCM_IMMUTABLE_SUBR (scm_i_paste (FNAME, __subr), \
107 scm_i_paste (FNAME, __name), \
108 REQ, OPT, VAR, &FNAME); \
109 SCM FNAME ARGLIST \
110) \
111SCM_SNARF_INIT( \
112 /* Initialize the procedure name (an interned symbol). */ \
113 scm_i_paste (FNAME, __subr_meta_info)[0] = scm_i_paste (FNAME, __name); \
114 \
115 /* Define the subr. */ \
116 scm_c_define (scm_i_paste (s_, FNAME), scm_i_paste (FNAME, __subr)); \
117) \
118SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
119
46f9baf4
LC
120#else /* !SCM_SUPPORT_STATIC_ALLOCATION */
121
122/* Always use the generic subr case. */
123#define SCM_DEFINE SCM_DEFINE_GSUBR
124
125#endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
126
127
a48d60b1
MD
128#define SCM_PRIMITIVE_GENERIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
129SCM_SNARF_HERE(\
130static const char s_ ## FNAME [] = PRIMNAME; \
131static SCM g_ ## FNAME; \
132SCM FNAME ARGLIST\
133)\
134SCM_SNARF_INIT(\
135g_ ## FNAME = SCM_PACK (0); \
136scm_c_define_gsubr_with_generic (s_ ## FNAME, REQ, OPT, VAR, \
137 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME, \
138 &g_ ## FNAME); \
139)\
140SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
141
5527702a
MV
142#define SCM_DEFINE_PUBLIC(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
143SCM_SNARF_HERE(\
144static const char s_ ## FNAME [] = PRIMNAME; \
145SCM FNAME ARGLIST\
146)\
147SCM_SNARF_INIT(\
148scm_c_define_gsubr (s_ ## FNAME, REQ, OPT, VAR, \
149 (SCM_FUNC_CAST_ARBITRARY_ARGS) FNAME); \
150scm_c_export (s_ ## FNAME, NULL); \
151)\
152SCM_SNARF_DOCS(primitive, FNAME, PRIMNAME, ARGLIST, REQ, OPT, VAR, DOCSTRING)
153
c3ee7520 154#define SCM_DEFINE1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
94a70859 155SCM_SNARF_HERE(\
4b4d0898
GB
156static const char s_ ## FNAME [] = PRIMNAME; \
157SCM FNAME ARGLIST\
2fdcf8bd 158)\
9a441ddb 159SCM_SNARF_INIT(scm_c_define_subr (s_ ## FNAME, TYPE, FNAME); ) \
cecb4a5e 160SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
1bbd0b84 161
a48d60b1
MD
162#define SCM_PRIMITIVE_GENERIC_1(FNAME, PRIMNAME, TYPE, ARGLIST, DOCSTRING) \
163SCM_SNARF_HERE(\
164static const char s_ ## FNAME [] = PRIMNAME; \
165static SCM g_ ## FNAME; \
166SCM FNAME ARGLIST\
167)\
168SCM_SNARF_INIT(\
169g_ ## FNAME = SCM_PACK (0); \
170scm_c_define_subr_with_generic (s_ ## FNAME, TYPE, FNAME, &g_ ## FNAME); \
171)\
172SCM_SNARF_DOCS(1, FNAME, PRIMNAME, ARGLIST, 2, 0, 0, DOCSTRING)
173
9b1594fd 174#define SCM_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
94a70859 175SCM_SNARF_HERE(static const char RANAME[]=STR) \
9a441ddb
MV
176SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
177 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN))
9b1594fd 178
1bbd0b84 179#define SCM_REGISTER_PROC(RANAME, STR, REQ, OPT, VAR, CFN) \
94a70859 180SCM_SNARF_HERE(static const char RANAME[]=STR) \
9a441ddb
MV
181SCM_SNARF_INIT(scm_c_define_gsubr (RANAME, REQ, OPT, VAR, \
182 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN);) \
cecb4a5e 183SCM_SNARF_DOCS(register, CFN, STR, (), REQ, OPT, VAR, \
c99f9605 184 "implemented by the C function \"" #CFN "\"")
3dc81fba 185
9de33deb 186#define SCM_GPROC(RANAME, STR, REQ, OPT, VAR, CFN, GF) \
94a70859 187SCM_SNARF_HERE(\
4b4d0898 188static const char RANAME[]=STR;\
54778cd3 189static SCM GF \
94a70859 190)SCM_SNARF_INIT(\
54778cd3 191GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
9a441ddb
MV
192scm_c_define_gsubr_with_generic (RANAME, REQ, OPT, VAR, \
193 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
4b4d0898 194)
3dc81fba 195
4b4d0898 196#define SCM_PROC1(RANAME, STR, TYPE, CFN) \
94a70859
MD
197SCM_SNARF_HERE(static const char RANAME[]=STR) \
198SCM_SNARF_INIT(\
9a441ddb 199scm_c_define_subr (RANAME, TYPE, (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN) \
4b4d0898 200)
9b1594fd 201
9b1594fd 202
4b4d0898 203#define SCM_GPROC1(RANAME, STR, TYPE, CFN, GF) \
94a70859 204SCM_SNARF_HERE(\
4b4d0898 205static const char RANAME[]=STR; \
54778cd3 206static SCM GF \
94a70859 207)SCM_SNARF_INIT(\
54778cd3 208GF = SCM_PACK (0); /* Dirk:FIXME:: Can we safely use #f instead of 0? */ \
9a441ddb
MV
209scm_c_define_subr_with_generic (RANAME, TYPE, \
210 (SCM_FUNC_CAST_ARBITRARY_ARGS) CFN, &GF) \
4b4d0898 211)
5b4215a8 212
b8229a3b 213#define SCM_SYNTAX(RANAME, STR, TYPE, CFN) \
94a70859
MD
214SCM_SNARF_HERE(static const char RANAME[]=STR)\
215SCM_SNARF_INIT(scm_make_synt (RANAME, TYPE, CFN))
b8229a3b 216
c6054fea
LC
217#ifdef SCM_SUPPORT_STATIC_ALLOCATION
218
219# define SCM_SYMBOL(c_name, scheme_name) \
220SCM_SNARF_HERE( \
b68095f0 221 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
c6054fea 222 static SCM c_name) \
b68095f0
LC
223SCM_SNARF_INIT( \
224 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
225)
c6054fea
LC
226
227# define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
228SCM_SNARF_HERE( \
b68095f0 229 SCM_IMMUTABLE_STRING (scm_i_paste (c_name, _string), scheme_name); \
c6054fea 230 SCM c_name) \
b68095f0
LC
231SCM_SNARF_INIT( \
232 c_name = scm_string_to_symbol (scm_i_paste (c_name, _string)) \
233)
c6054fea
LC
234
235#else /* !SCM_SUPPORT_STATIC_ALLOCATION */
236
237# define SCM_SYMBOL(c_name, scheme_name) \
238SCM_SNARF_HERE(static SCM c_name) \
cc95e00a 239SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
5b4215a8 240
c6054fea
LC
241# define SCM_GLOBAL_SYMBOL(c_name, scheme_name) \
242SCM_SNARF_HERE(SCM c_name) \
cc95e00a 243SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_symbol (scheme_name)))
78f9f47b 244
c6054fea
LC
245#endif /* !SCM_SUPPORT_STATIC_ALLOCATION */
246
37b83f68 247#define SCM_KEYWORD(c_name, scheme_name) \
94a70859 248SCM_SNARF_HERE(static SCM c_name) \
ee4140fb 249SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
37b83f68 250
37b83f68 251#define SCM_GLOBAL_KEYWORD(c_name, scheme_name) \
94a70859 252SCM_SNARF_HERE(SCM c_name) \
ee4140fb 253SCM_SNARF_INIT(c_name = scm_permanent_object (scm_from_locale_keyword (scheme_name)))
5b4215a8 254
86d31dfe
MV
255#define SCM_VARIABLE(c_name, scheme_name) \
256SCM_SNARF_HERE(static SCM c_name) \
257SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
258
259#define SCM_GLOBAL_VARIABLE(c_name, scheme_name) \
260SCM_SNARF_HERE(SCM c_name) \
261SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, SCM_BOOL_F));)
262
263#define SCM_VARIABLE_INIT(c_name, scheme_name, init_val) \
264SCM_SNARF_HERE(static SCM c_name) \
265SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
266
267#define SCM_GLOBAL_VARIABLE_INIT(c_name, scheme_name, init_val) \
268SCM_SNARF_HERE(SCM c_name) \
269SCM_SNARF_INIT(c_name = scm_permanent_object (scm_c_define (scheme_name, init_val));)
270
28d52ebb
MD
271#define SCM_MUTEX(c_name) \
272SCM_SNARF_HERE(static scm_t_mutex c_name) \
273SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
9bc4701c 274
28d52ebb
MD
275#define SCM_GLOBAL_MUTEX(c_name) \
276SCM_SNARF_HERE(scm_t_mutex c_name) \
277SCM_SNARF_INIT(scm_i_plugin_mutex_init (&c_name, &scm_i_plugin_mutex))
9bc4701c 278
28d52ebb
MD
279#define SCM_REC_MUTEX(c_name) \
280SCM_SNARF_HERE(static scm_t_rec_mutex c_name) \
281SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
9bc4701c 282
28d52ebb
MD
283#define SCM_GLOBAL_REC_MUTEX(c_name) \
284SCM_SNARF_HERE(scm_t_rec_mutex c_name) \
0b6843b1 285SCM_SNARF_INIT(scm_i_plugin_rec_mutex_init (&c_name, &scm_i_plugin_rec_mutex))
9bc4701c 286
94e38a65
MV
287#define SCM_SMOB(tag, scheme_name, size) \
288SCM_SNARF_HERE(static scm_t_bits tag) \
289SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
290
291#define SCM_GLOBAL_SMOB(tag, scheme_name, size) \
292SCM_SNARF_HERE(scm_t_bits tag) \
293SCM_SNARF_INIT((tag)=scm_make_smob_type((scheme_name), (size));)
294
295#define SCM_SMOB_MARK(tag, c_name, arg) \
296SCM_SNARF_HERE(static SCM c_name(SCM arg)) \
297SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
298
299#define SCM_GLOBAL_SMOB_MARK(tag, c_name, arg) \
300SCM_SNARF_HERE(SCM c_name(SCM arg)) \
301SCM_SNARF_INIT(scm_set_smob_mark((tag), (c_name));)
302
303#define SCM_SMOB_FREE(tag, c_name, arg) \
304SCM_SNARF_HERE(static size_t c_name(SCM arg)) \
305SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
306
307#define SCM_GLOBAL_SMOB_FREE(tag, c_name, arg) \
308SCM_SNARF_HERE(size_t c_name(SCM arg)) \
309SCM_SNARF_INIT(scm_set_smob_free((tag), (c_name));)
310
311#define SCM_SMOB_PRINT(tag, c_name, obj, port, pstate) \
312SCM_SNARF_HERE(static int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
313SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
314
315#define SCM_GLOBAL_SMOB_PRINT(tag, c_name, obj, port, pstate) \
316SCM_SNARF_HERE(int c_name(SCM obj, SCM port, scm_print_state* pstate)) \
317SCM_SNARF_INIT(scm_set_smob_print((tag), (c_name));)
318
319#define SCM_SMOB_EQUALP(tag, c_name, obj1, obj2) \
320SCM_SNARF_HERE(static SCM c_name(SCM obj1, SCM obj2)) \
321SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
322
323#define SCM_GLOBAL_SMOB_EQUALP(tag, c_name, obj1, obj2) \
324SCM_SNARF_HERE(SCM c_name(SCM obj1, SCM obj2)) \
325SCM_SNARF_INIT(scm_set_smob_equalp((tag), (c_name));)
326
327#define SCM_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
328SCM_SNARF_HERE(static SCM c_name arglist) \
329SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
330
331#define SCM_GLOBAL_SMOB_APPLY(tag, c_name, req, opt, rest, arglist) \
332SCM_SNARF_HERE(SCM c_name arglist) \
333SCM_SNARF_INIT(scm_set_smob_apply((tag), (c_name), (req), (opt), (rest));)
334
c6054fea
LC
335\f
336/* Low-level snarfing for static memory allocation. */
337
338#ifdef SCM_SUPPORT_STATIC_ALLOCATION
339
340#define SCM_IMMUTABLE_DOUBLE_CELL(c_name, car, cbr, ccr, cdr) \
341 static SCM_ALIGNED (8) SCM_UNUSED const scm_t_cell \
342 c_name ## _raw_cell [2] = \
343 { \
344 { SCM_PACK (car), SCM_PACK (cbr) }, \
345 { SCM_PACK (ccr), SCM_PACK (cdr) } \
346 }; \
347 static SCM_UNUSED const SCM c_name = SCM_PACK (& c_name ## _raw_cell)
348
5f236208
LC
349#define SCM_IMMUTABLE_STRINGBUF(c_name, contents) \
350 static SCM_UNUSED const \
351 struct \
352 { \
353 scm_t_bits word_0; \
354 scm_t_bits word_1; \
355 const char buffer[sizeof (contents)]; \
356 } \
357 c_name = \
358 { \
359 scm_tc7_stringbuf | SCM_I_STRINGBUF_F_SHARED, \
360 sizeof (contents) - 1, \
361 contents \
362 }
c6054fea
LC
363
364#define SCM_IMMUTABLE_STRING(c_name, contents) \
b68095f0 365 SCM_IMMUTABLE_STRINGBUF (scm_i_paste (c_name, _stringbuf), contents); \
c6054fea
LC
366 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
367 scm_tc7_ro_string, \
b68095f0 368 (scm_t_bits) &scm_i_paste (c_name, \
5f236208 369 _stringbuf), \
c6054fea
LC
370 (scm_t_bits) 0, \
371 (scm_t_bits) sizeof (contents) - 1)
372
46f9baf4
LC
373#define SCM_IMMUTABLE_SUBR(c_name, name, req, opt, rest, fcn) \
374 static SCM_UNUSED SCM scm_i_paste (c_name, _meta_info)[2] = \
375 { \
376 SCM_BOOL_F, /* The name, initialized at run-time. */ \
377 SCM_EOL /* The procedure properties. */ \
378 }; \
379 SCM_IMMUTABLE_DOUBLE_CELL (c_name, \
380 SCM_SUBR_ARITY_TO_TYPE (req, opt, rest), \
381 (scm_t_bits) fcn, \
382 (scm_t_bits) 0 /* no generic */, \
383 (scm_t_bits) & scm_i_paste (c_name, _meta_info));
384
c6054fea
LC
385#endif /* SCM_SUPPORT_STATIC_ALLOCATION */
386
387\f
388/* Documentation. */
94e38a65 389
c99f9605 390#ifdef SCM_MAGIC_SNARF_DOCS
4b4d0898 391#undef SCM_ASSERT
ac13d9d2 392#define SCM_ASSERT(_cond, _arg, _pos, _subr) ^^ argpos _arg _pos __LINE__ ^^
c99f9605 393#endif /* SCM_MAGIC_SNARF_DOCS */
2e07d033 394
b29058ff 395#endif /* SCM_SNARF_H */
89e00824
ML
396
397/*
398 Local Variables:
399 c-file-style: "gnu"
400 End:
401*/