snarf.h 12 KB

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