expand.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #ifndef SCM_EXPAND_H
  2. #define SCM_EXPAND_H
  3. /* Copyright 2010, 2011, 2013, 2014, 2018 Free Software Foundation, Inc.
  4. This file is part of Guile.
  5. Guile is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. Guile is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with Guile. If not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include "libguile/struct.h"
  17. #ifdef BUILDING_LIBGUILE
  18. /* All private for now. Ask if you want to use this. Surely this should be
  19. auto-generated by something; for now I wrangle it with keyboard macros. */
  20. typedef enum
  21. {
  22. SCM_EXPANDED_VOID,
  23. SCM_EXPANDED_CONST,
  24. SCM_EXPANDED_PRIMITIVE_REF,
  25. SCM_EXPANDED_LEXICAL_REF,
  26. SCM_EXPANDED_LEXICAL_SET,
  27. SCM_EXPANDED_MODULE_REF,
  28. SCM_EXPANDED_MODULE_SET,
  29. SCM_EXPANDED_TOPLEVEL_REF,
  30. SCM_EXPANDED_TOPLEVEL_SET,
  31. SCM_EXPANDED_TOPLEVEL_DEFINE,
  32. SCM_EXPANDED_CONDITIONAL,
  33. SCM_EXPANDED_CALL,
  34. SCM_EXPANDED_PRIMCALL,
  35. SCM_EXPANDED_SEQ,
  36. SCM_EXPANDED_LAMBDA,
  37. SCM_EXPANDED_LAMBDA_CASE,
  38. SCM_EXPANDED_LET,
  39. SCM_EXPANDED_LETREC,
  40. SCM_NUM_EXPANDED_TYPES,
  41. } scm_t_expanded_type;
  42. /* {Expanded Source}
  43. */
  44. SCM_INTERNAL SCM scm_exp_vtable_vtable;
  45. enum
  46. {
  47. SCM_EXPANDED_TYPE_NAME = scm_vtable_offset_user,
  48. SCM_EXPANDED_TYPE_CODE,
  49. SCM_EXPANDED_TYPE_FIELDS,
  50. };
  51. #define SCM_EXPANDED_P(x) \
  52. (SCM_STRUCTP (x) \
  53. && (scm_is_eq (SCM_STRUCT_VTABLE (SCM_STRUCT_VTABLE (x)), scm_exp_vtable_vtable)))
  54. #define SCM_EXPANDED_REF(x,type,field) \
  55. (scm_struct_ref (x, SCM_I_MAKINUM (SCM_EXPANDED_##type##_##field)))
  56. #define SCM_EXPANDED_TYPE(x) \
  57. SCM_STRUCT_DATA_REF (SCM_STRUCT_VTABLE (x), SCM_EXPANDED_TYPE_CODE)
  58. #define SCM_EXPANDED_VOID_TYPE_NAME "void"
  59. #define SCM_EXPANDED_VOID_FIELD_NAMES \
  60. { "src" }
  61. enum
  62. {
  63. SCM_EXPANDED_VOID_SRC,
  64. SCM_NUM_EXPANDED_VOID_FIELDS,
  65. };
  66. #define SCM_MAKE_EXPANDED_VOID(src) \
  67. scm_c_make_struct (exp_vtables[SCM_EXPANDED_VOID], 0, SCM_NUM_EXPANDED_VOID_FIELDS, SCM_UNPACK (src))
  68. #define SCM_EXPANDED_CONST_TYPE_NAME "const"
  69. #define SCM_EXPANDED_CONST_FIELD_NAMES \
  70. { "src", "exp", }
  71. enum
  72. {
  73. SCM_EXPANDED_CONST_SRC,
  74. SCM_EXPANDED_CONST_EXP,
  75. SCM_NUM_EXPANDED_CONST_FIELDS,
  76. };
  77. #define SCM_MAKE_EXPANDED_CONST(src, exp) \
  78. scm_c_make_struct (exp_vtables[SCM_EXPANDED_CONST], 0, SCM_NUM_EXPANDED_CONST_FIELDS, SCM_UNPACK (src), SCM_UNPACK (exp))
  79. #define SCM_EXPANDED_PRIMITIVE_REF_TYPE_NAME "primitive-ref"
  80. #define SCM_EXPANDED_PRIMITIVE_REF_FIELD_NAMES \
  81. { "src", "name", }
  82. enum
  83. {
  84. SCM_EXPANDED_PRIMITIVE_REF_SRC,
  85. SCM_EXPANDED_PRIMITIVE_REF_NAME,
  86. SCM_NUM_EXPANDED_PRIMITIVE_REF_FIELDS,
  87. };
  88. #define SCM_MAKE_EXPANDED_PRIMITIVE_REF(src, name) \
  89. scm_c_make_struct (exp_vtables[SCM_EXPANDED_PRIMITIVE_REF], 0, SCM_NUM_EXPANDED_PRIMITIVE_REF_FIELDS, SCM_UNPACK (src), SCM_UNPACK (name))
  90. #define SCM_EXPANDED_LEXICAL_REF_TYPE_NAME "lexical-ref"
  91. #define SCM_EXPANDED_LEXICAL_REF_FIELD_NAMES \
  92. { "src", "name", "gensym", }
  93. enum
  94. {
  95. SCM_EXPANDED_LEXICAL_REF_SRC,
  96. SCM_EXPANDED_LEXICAL_REF_NAME,
  97. SCM_EXPANDED_LEXICAL_REF_GENSYM,
  98. SCM_NUM_EXPANDED_LEXICAL_REF_FIELDS,
  99. };
  100. #define SCM_MAKE_EXPANDED_LEXICAL_REF(src, name, gensym) \
  101. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LEXICAL_REF], 0, SCM_NUM_EXPANDED_LEXICAL_REF_FIELDS, SCM_UNPACK (src), SCM_UNPACK (name), SCM_UNPACK (gensym))
  102. #define SCM_EXPANDED_LEXICAL_SET_TYPE_NAME "lexical-set"
  103. #define SCM_EXPANDED_LEXICAL_SET_FIELD_NAMES \
  104. { "src", "name", "gensym", "exp", }
  105. enum
  106. {
  107. SCM_EXPANDED_LEXICAL_SET_SRC,
  108. SCM_EXPANDED_LEXICAL_SET_NAME,
  109. SCM_EXPANDED_LEXICAL_SET_GENSYM,
  110. SCM_EXPANDED_LEXICAL_SET_EXP,
  111. SCM_NUM_EXPANDED_LEXICAL_SET_FIELDS,
  112. };
  113. #define SCM_MAKE_EXPANDED_LEXICAL_SET(src, name, gensym, exp) \
  114. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LEXICAL_SET], 0, SCM_NUM_EXPANDED_LEXICAL_SET_FIELDS, SCM_UNPACK (src), SCM_UNPACK (name), SCM_UNPACK (gensym), SCM_UNPACK (exp))
  115. #define SCM_EXPANDED_MODULE_REF_TYPE_NAME "module-ref"
  116. #define SCM_EXPANDED_MODULE_REF_FIELD_NAMES \
  117. { "src", "mod", "name", "public?", }
  118. enum
  119. {
  120. SCM_EXPANDED_MODULE_REF_SRC,
  121. SCM_EXPANDED_MODULE_REF_MOD,
  122. SCM_EXPANDED_MODULE_REF_NAME,
  123. SCM_EXPANDED_MODULE_REF_PUBLIC,
  124. SCM_NUM_EXPANDED_MODULE_REF_FIELDS,
  125. };
  126. #define SCM_MAKE_EXPANDED_MODULE_REF(src, mod, name, public) \
  127. scm_c_make_struct (exp_vtables[SCM_EXPANDED_MODULE_REF], 0, SCM_NUM_EXPANDED_MODULE_REF_FIELDS, SCM_UNPACK (src), SCM_UNPACK (mod), SCM_UNPACK (name), SCM_UNPACK (public))
  128. #define SCM_EXPANDED_MODULE_SET_TYPE_NAME "module-set"
  129. #define SCM_EXPANDED_MODULE_SET_FIELD_NAMES \
  130. { "src", "mod", "name", "public?", "exp", }
  131. enum
  132. {
  133. SCM_EXPANDED_MODULE_SET_SRC,
  134. SCM_EXPANDED_MODULE_SET_MOD,
  135. SCM_EXPANDED_MODULE_SET_NAME,
  136. SCM_EXPANDED_MODULE_SET_PUBLIC,
  137. SCM_EXPANDED_MODULE_SET_EXP,
  138. SCM_NUM_EXPANDED_MODULE_SET_FIELDS,
  139. };
  140. #define SCM_MAKE_EXPANDED_MODULE_SET(src, mod, name, public, exp) \
  141. scm_c_make_struct (exp_vtables[SCM_EXPANDED_MODULE_SET], 0, SCM_NUM_EXPANDED_MODULE_SET_FIELDS, SCM_UNPACK (src), SCM_UNPACK (mod), SCM_UNPACK (name), SCM_UNPACK (public), SCM_UNPACK (exp))
  142. #define SCM_EXPANDED_TOPLEVEL_REF_TYPE_NAME "toplevel-ref"
  143. #define SCM_EXPANDED_TOPLEVEL_REF_FIELD_NAMES \
  144. { "src", "mod", "name", }
  145. enum
  146. {
  147. SCM_EXPANDED_TOPLEVEL_REF_SRC,
  148. SCM_EXPANDED_TOPLEVEL_REF_MOD,
  149. SCM_EXPANDED_TOPLEVEL_REF_NAME,
  150. SCM_NUM_EXPANDED_TOPLEVEL_REF_FIELDS,
  151. };
  152. #define SCM_MAKE_EXPANDED_TOPLEVEL_REF(src, mod, name) \
  153. scm_c_make_struct (exp_vtables[SCM_EXPANDED_TOPLEVEL_REF], 0, SCM_NUM_EXPANDED_TOPLEVEL_REF_FIELDS, SCM_UNPACK (src), SCM_UNPACK (mod), SCM_UNPACK (name))
  154. #define SCM_EXPANDED_TOPLEVEL_SET_TYPE_NAME "toplevel-set"
  155. #define SCM_EXPANDED_TOPLEVEL_SET_FIELD_NAMES \
  156. { "src", "mod", "name", "exp", }
  157. enum
  158. {
  159. SCM_EXPANDED_TOPLEVEL_SET_SRC,
  160. SCM_EXPANDED_TOPLEVEL_SET_MOD,
  161. SCM_EXPANDED_TOPLEVEL_SET_NAME,
  162. SCM_EXPANDED_TOPLEVEL_SET_EXP,
  163. SCM_NUM_EXPANDED_TOPLEVEL_SET_FIELDS,
  164. };
  165. #define SCM_MAKE_EXPANDED_TOPLEVEL_SET(src, mod, name, exp) \
  166. scm_c_make_struct (exp_vtables[SCM_EXPANDED_TOPLEVEL_SET], 0, SCM_NUM_EXPANDED_TOPLEVEL_SET_FIELDS, SCM_UNPACK (src), SCM_UNPACK (mod), SCM_UNPACK (name), SCM_UNPACK (exp))
  167. #define SCM_EXPANDED_TOPLEVEL_DEFINE_TYPE_NAME "toplevel-define"
  168. #define SCM_EXPANDED_TOPLEVEL_DEFINE_FIELD_NAMES \
  169. { "src", "mod", "name", "exp", }
  170. enum
  171. {
  172. SCM_EXPANDED_TOPLEVEL_DEFINE_SRC,
  173. SCM_EXPANDED_TOPLEVEL_DEFINE_MOD,
  174. SCM_EXPANDED_TOPLEVEL_DEFINE_NAME,
  175. SCM_EXPANDED_TOPLEVEL_DEFINE_EXP,
  176. SCM_NUM_EXPANDED_TOPLEVEL_DEFINE_FIELDS,
  177. };
  178. #define SCM_MAKE_EXPANDED_TOPLEVEL_DEFINE(src, mod, name, exp) \
  179. scm_c_make_struct (exp_vtables[SCM_EXPANDED_TOPLEVEL_DEFINE], 0, SCM_NUM_EXPANDED_TOPLEVEL_DEFINE_FIELDS, SCM_UNPACK (src), SCM_UNPACK (mod), SCM_UNPACK (name), SCM_UNPACK (exp))
  180. #define SCM_EXPANDED_CONDITIONAL_TYPE_NAME "conditional"
  181. #define SCM_EXPANDED_CONDITIONAL_FIELD_NAMES \
  182. { "src", "test", "consequent", "alternate", }
  183. enum
  184. {
  185. SCM_EXPANDED_CONDITIONAL_SRC,
  186. SCM_EXPANDED_CONDITIONAL_TEST,
  187. SCM_EXPANDED_CONDITIONAL_CONSEQUENT,
  188. SCM_EXPANDED_CONDITIONAL_ALTERNATE,
  189. SCM_NUM_EXPANDED_CONDITIONAL_FIELDS,
  190. };
  191. #define SCM_MAKE_EXPANDED_CONDITIONAL(src, test, consequent, alternate) \
  192. scm_c_make_struct (exp_vtables[SCM_EXPANDED_CONDITIONAL], 0, SCM_NUM_EXPANDED_CONDITIONAL_FIELDS, SCM_UNPACK (src), SCM_UNPACK (test), SCM_UNPACK (consequent), SCM_UNPACK (alternate))
  193. #define SCM_EXPANDED_CALL_TYPE_NAME "call"
  194. #define SCM_EXPANDED_CALL_FIELD_NAMES \
  195. { "src", "proc", "args", }
  196. enum
  197. {
  198. SCM_EXPANDED_CALL_SRC,
  199. SCM_EXPANDED_CALL_PROC,
  200. SCM_EXPANDED_CALL_ARGS,
  201. SCM_NUM_EXPANDED_CALL_FIELDS,
  202. };
  203. #define SCM_MAKE_EXPANDED_CALL(src, proc, args) \
  204. scm_c_make_struct (exp_vtables[SCM_EXPANDED_CALL], 0, SCM_NUM_EXPANDED_CALL_FIELDS, SCM_UNPACK (src), SCM_UNPACK (proc), SCM_UNPACK (args))
  205. #define SCM_EXPANDED_PRIMCALL_TYPE_NAME "primcall"
  206. #define SCM_EXPANDED_PRIMCALL_FIELD_NAMES \
  207. { "src", "name", "args", }
  208. enum
  209. {
  210. SCM_EXPANDED_PRIMCALL_SRC,
  211. SCM_EXPANDED_PRIMCALL_NAME,
  212. SCM_EXPANDED_PRIMCALL_ARGS,
  213. SCM_NUM_EXPANDED_PRIMCALL_FIELDS,
  214. };
  215. #define SCM_MAKE_EXPANDED_PRIMCALL(src, name, args) \
  216. scm_c_make_struct (exp_vtables[SCM_EXPANDED_PRIMCALL], 0, SCM_NUM_EXPANDED_PRIMCALL_FIELDS, SCM_UNPACK (src), SCM_UNPACK (name), SCM_UNPACK (args))
  217. #define SCM_EXPANDED_SEQ_TYPE_NAME "seq"
  218. #define SCM_EXPANDED_SEQ_FIELD_NAMES \
  219. { "src", "head", "tail", }
  220. enum
  221. {
  222. SCM_EXPANDED_SEQ_SRC,
  223. SCM_EXPANDED_SEQ_HEAD,
  224. SCM_EXPANDED_SEQ_TAIL,
  225. SCM_NUM_EXPANDED_SEQ_FIELDS,
  226. };
  227. #define SCM_MAKE_EXPANDED_SEQ(src, head, tail) \
  228. scm_c_make_struct (exp_vtables[SCM_EXPANDED_SEQ], 0, SCM_NUM_EXPANDED_SEQ_FIELDS, SCM_UNPACK (src), SCM_UNPACK (head), SCM_UNPACK (tail))
  229. #define SCM_EXPANDED_LAMBDA_TYPE_NAME "lambda"
  230. #define SCM_EXPANDED_LAMBDA_FIELD_NAMES \
  231. { "src", "meta", "body", }
  232. enum
  233. {
  234. SCM_EXPANDED_LAMBDA_SRC,
  235. SCM_EXPANDED_LAMBDA_META,
  236. SCM_EXPANDED_LAMBDA_BODY,
  237. SCM_NUM_EXPANDED_LAMBDA_FIELDS,
  238. };
  239. #define SCM_MAKE_EXPANDED_LAMBDA(src, meta, body) \
  240. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LAMBDA], 0, SCM_NUM_EXPANDED_LAMBDA_FIELDS, SCM_UNPACK (src), SCM_UNPACK (meta), SCM_UNPACK (body))
  241. #define SCM_EXPANDED_LAMBDA_CASE_TYPE_NAME "lambda-case"
  242. #define SCM_EXPANDED_LAMBDA_CASE_FIELD_NAMES \
  243. { "src", "req", "opt", "rest", "kw", "inits", "gensyms", "body", "alternate", }
  244. enum
  245. {
  246. SCM_EXPANDED_LAMBDA_CASE_SRC,
  247. SCM_EXPANDED_LAMBDA_CASE_REQ,
  248. SCM_EXPANDED_LAMBDA_CASE_OPT,
  249. SCM_EXPANDED_LAMBDA_CASE_REST,
  250. SCM_EXPANDED_LAMBDA_CASE_KW,
  251. SCM_EXPANDED_LAMBDA_CASE_INITS,
  252. SCM_EXPANDED_LAMBDA_CASE_GENSYMS,
  253. SCM_EXPANDED_LAMBDA_CASE_BODY,
  254. SCM_EXPANDED_LAMBDA_CASE_ALTERNATE,
  255. SCM_NUM_EXPANDED_LAMBDA_CASE_FIELDS,
  256. };
  257. #define SCM_MAKE_EXPANDED_LAMBDA_CASE(src, req, opt, rest, kw, inits, gensyms, body, alternate) \
  258. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LAMBDA_CASE], 0, SCM_NUM_EXPANDED_LAMBDA_CASE_FIELDS, SCM_UNPACK (src), SCM_UNPACK (req), SCM_UNPACK (opt), SCM_UNPACK (rest), SCM_UNPACK (kw), SCM_UNPACK (inits), SCM_UNPACK (gensyms), SCM_UNPACK (body), SCM_UNPACK (alternate))
  259. #define SCM_EXPANDED_LET_TYPE_NAME "let"
  260. #define SCM_EXPANDED_LET_FIELD_NAMES \
  261. { "src", "names", "gensyms", "vals", "body", }
  262. enum
  263. {
  264. SCM_EXPANDED_LET_SRC,
  265. SCM_EXPANDED_LET_NAMES,
  266. SCM_EXPANDED_LET_GENSYMS,
  267. SCM_EXPANDED_LET_VALS,
  268. SCM_EXPANDED_LET_BODY,
  269. SCM_NUM_EXPANDED_LET_FIELDS,
  270. };
  271. #define SCM_MAKE_EXPANDED_LET(src, names, gensyms, vals, body) \
  272. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LET], 0, SCM_NUM_EXPANDED_LET_FIELDS, SCM_UNPACK (src), SCM_UNPACK (names), SCM_UNPACK (gensyms), SCM_UNPACK (vals), SCM_UNPACK (body))
  273. #define SCM_EXPANDED_LETREC_TYPE_NAME "letrec"
  274. #define SCM_EXPANDED_LETREC_FIELD_NAMES \
  275. { "src", "in-order?", "names", "gensyms", "vals", "body", }
  276. enum
  277. {
  278. SCM_EXPANDED_LETREC_SRC,
  279. SCM_EXPANDED_LETREC_IN_ORDER_P,
  280. SCM_EXPANDED_LETREC_NAMES,
  281. SCM_EXPANDED_LETREC_GENSYMS,
  282. SCM_EXPANDED_LETREC_VALS,
  283. SCM_EXPANDED_LETREC_BODY,
  284. SCM_NUM_EXPANDED_LETREC_FIELDS,
  285. };
  286. #define SCM_MAKE_EXPANDED_LETREC(src, in_order_p, names, gensyms, vals, body) \
  287. scm_c_make_struct (exp_vtables[SCM_EXPANDED_LETREC], 0, SCM_NUM_EXPANDED_LETREC_FIELDS, SCM_UNPACK (src), SCM_UNPACK (in_order_p), SCM_UNPACK (names), SCM_UNPACK (gensyms), SCM_UNPACK (vals), SCM_UNPACK (body))
  288. #endif /* BUILDING_LIBGUILE */
  289. SCM_INTERNAL SCM scm_macroexpand (SCM exp);
  290. SCM_INTERNAL SCM scm_macroexpanded_p (SCM exp);
  291. SCM_INTERNAL SCM scm_convert_assignment (SCM exp);
  292. SCM_INTERNAL void scm_init_expand (void);
  293. #endif /* SCM_EXPAND_H */