chicken.swg 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /* -----------------------------------------------------------------------------
  2. * chicken.swg
  3. *
  4. * CHICKEN configuration module.
  5. * ----------------------------------------------------------------------------- */
  6. /* chicken.h has to appear first. */
  7. %insert(runtime) %{
  8. #include <assert.h>
  9. #include <chicken.h>
  10. %}
  11. %insert(runtime) "swigrun.swg"; // Common C API type-checking code
  12. %insert(runtime) "chickenrun.swg"; // CHICKEN run-time code
  13. /* -----------------------------------------------------------------------------
  14. * standard typemaps
  15. * ----------------------------------------------------------------------------- */
  16. /*
  17. CHICKEN: C
  18. ----------
  19. fixnum: int, short, unsigned int, unsigned short, unsigned char,
  20. signed char
  21. char: char
  22. bool: bool
  23. flonum: float, double, long, long long, unsigned long, unsigned long
  24. long
  25. */
  26. /* --- Primitive types --- */
  27. %define SIMPLE_TYPEMAP(type_, from_scheme, to_scheme, checker, convtype, storage_)
  28. %typemap(in) type_
  29. %{ if (!checker ($input)) {
  30. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument #$argnum is not of type 'type_'");
  31. }
  32. $1 = ($1_ltype) from_scheme ($input); %}
  33. /* Const primitive references. Passed by value */
  34. %typemap(in) const type_ & ($*1_ltype temp)
  35. %{ if (!checker ($input)) {
  36. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument #$argnum is not of type 'type_'");
  37. }
  38. temp = ($*1_ltype) from_scheme ($input);
  39. $1 = &temp; %}
  40. /* --- Variable input --- */
  41. %typemap(varin) type_
  42. %{ if (!checker ($input)) {
  43. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Cannot use '$1_ltype' for variable '$name' of type 'type_'");
  44. }
  45. $1 = ($1_ltype) from_scheme ($input); %}
  46. #if "storage_" == "0"
  47. %typemap(out) type_
  48. %{
  49. $result = to_scheme (convtype ($1));
  50. %}
  51. /* References to primitive types. Return by value */
  52. %typemap(out) const type_ &
  53. %{
  54. $result = to_scheme (convtype (*$1));
  55. %}
  56. /* --- Variable output --- */
  57. %typemap(varout) type_
  58. %{
  59. $result = to_scheme (convtype ($varname));
  60. %}
  61. %typemap(throws) type_
  62. %{
  63. SWIG_Chicken_ThrowException(to_scheme ( convtype ($1)));
  64. %}
  65. #else
  66. %typemap(out) type_
  67. %{
  68. {
  69. C_word *space = C_alloc(storage_);
  70. $result = to_scheme (&space, convtype ($1));
  71. }
  72. %}
  73. /* References to primitive types. Return by value */
  74. %typemap(out) const type_ &
  75. %{
  76. {
  77. C_word *space = C_alloc(storage_);
  78. $result = to_scheme (&space, convtype (*$1));
  79. }
  80. %}
  81. /* --- Variable output --- */
  82. %typemap(varout) type_
  83. %{
  84. {
  85. C_word *space = C_alloc(storage_);
  86. $result = to_scheme (&space, convtype ($varname));
  87. }
  88. %}
  89. %typemap(throws) type_
  90. %{
  91. {
  92. C_word *space = C_alloc(storage_);
  93. SWIG_Chicken_ThrowException(to_scheme (&space, convtype ($1)));
  94. }
  95. %}
  96. #endif
  97. /* --- Constants --- */
  98. %typemap(constcode) type_
  99. "static const $1_type $result = $value;"
  100. %enddef
  101. SIMPLE_TYPEMAP(int, C_num_to_int, C_fix, C_swig_is_number, (int), 0);
  102. //SIMPLE_TYPEMAP(enum SWIGTYPE, C_unfix, C_fix, C_swig_is_fixnum, (int), 0);
  103. SIMPLE_TYPEMAP(short, C_num_to_int, C_fix, C_swig_is_number, (int), 0);
  104. SIMPLE_TYPEMAP(long, C_num_to_long, C_long_to_num, C_swig_is_long, (long), C_SIZEOF_FLONUM);
  105. SIMPLE_TYPEMAP(long long, C_num_to_long, C_long_to_num, C_swig_is_long, (long), C_SIZEOF_FLONUM);
  106. SIMPLE_TYPEMAP(unsigned int, C_num_to_unsigned_int, C_unsigned_int_to_num, C_swig_is_number, (unsigned int), C_SIZEOF_FLONUM);
  107. SIMPLE_TYPEMAP(unsigned short, C_num_to_unsigned_int, C_fix, C_swig_is_number, (unsigned int), 0);
  108. SIMPLE_TYPEMAP(unsigned long, C_num_to_unsigned_long, C_unsigned_long_to_num, C_swig_is_long, (unsigned long), C_SIZEOF_FLONUM);
  109. SIMPLE_TYPEMAP(unsigned long long, C_num_to_unsigned_long, C_unsigned_long_to_num, C_swig_is_long, (unsigned long), C_SIZEOF_FLONUM);
  110. SIMPLE_TYPEMAP(unsigned char, C_character_code, C_make_character, C_swig_is_char, (unsigned int), 0);
  111. SIMPLE_TYPEMAP(signed char, C_character_code, C_make_character, C_swig_is_char, (int), 0);
  112. SIMPLE_TYPEMAP(char, C_character_code, C_make_character, C_swig_is_char, (char), 0);
  113. SIMPLE_TYPEMAP(bool, C_truep, C_mk_bool, C_swig_is_bool, (bool), 0);
  114. SIMPLE_TYPEMAP(float, C_c_double, C_flonum, C_swig_is_number, (double), C_SIZEOF_FLONUM);
  115. SIMPLE_TYPEMAP(double, C_c_double, C_flonum, C_swig_is_number, (double), C_SIZEOF_FLONUM);
  116. /* enum SWIGTYPE */
  117. %apply int { enum SWIGTYPE };
  118. %apply const int& { const enum SWIGTYPE& };
  119. %apply const int& { const enum SWIGTYPE&& };
  120. %typemap(varin) enum SWIGTYPE
  121. {
  122. if (!C_swig_is_fixnum($input) && sizeof(int) != sizeof($1)) {
  123. swig_barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, "enum variable '$name' can not be set");
  124. }
  125. *((int *)(void *)&$1) = C_unfix($input);
  126. }
  127. /* --- Input arguments --- */
  128. /* Strings */
  129. %typemap(in) char *
  130. { if ($input == C_SCHEME_FALSE) {
  131. $1 = NULL;
  132. }
  133. else {
  134. if (!C_swig_is_string ($input)) {
  135. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument #$argnum is not of type 'char *'");
  136. }
  137. $1 = ($ltype) SWIG_MakeString ($input);
  138. }
  139. }
  140. %typemap(freearg) char * "if ($1 != NULL) { free ($1); }"
  141. /* Pointers, references, and arrays */
  142. %typemap(in,closcode="(slot-ref $input 'swig-this)") SWIGTYPE *, SWIGTYPE [], SWIGTYPE &, SWIGTYPE && {
  143. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, $disown);
  144. }
  145. %typemap(in,closcode="(slot-ref $input 'swig-this)") SWIGTYPE *DISOWN {
  146. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, SWIG_POINTER_DISOWN);
  147. }
  148. /* Void pointer. Accepts any kind of pointer */
  149. %typemap(in) void * {
  150. $1 = ($1_ltype)SWIG_MustGetPtr($input, NULL, $argnum, 0);
  151. }
  152. %typemap(varin,closcode="(slot-ref $input 'swig-this)") SWIGTYPE * {
  153. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, SWIG_POINTER_DISOWN);
  154. }
  155. %typemap(varin,closcode="(slot-ref $input 'swig-this)") SWIGTYPE & {
  156. $1 = *(($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0));
  157. }
  158. %typemap(varin,closcode="(slot-ref $input 'swig-this)") SWIGTYPE && {
  159. $1 = *(($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0));
  160. }
  161. %typemap(varin) SWIGTYPE [] {
  162. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, "Type error");
  163. }
  164. %typemap(varin) SWIGTYPE [ANY] {
  165. void *temp;
  166. int ii;
  167. $1_basetype *b = 0;
  168. temp = SWIG_MustGetPtr($input, $1_descriptor, 1, 0);
  169. b = ($1_basetype *) $1;
  170. for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii);
  171. }
  172. %typemap(varin) void * {
  173. $1 = SWIG_MustGetPtr($input, NULL, 1, 0);
  174. }
  175. %typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE [] {
  176. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  177. $result = SWIG_NewPointerObj($1, $descriptor, $owner);
  178. }
  179. %typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC {
  180. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  181. swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,(void **) &$1);
  182. $result = SWIG_NewPointerObj($1, ty, $owner);
  183. }
  184. %typemap(varout) SWIGTYPE *, SWIGTYPE [] {
  185. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  186. $result = SWIG_NewPointerObj($varname, $descriptor, 0);
  187. }
  188. %typemap(varout) SWIGTYPE & {
  189. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  190. $result = SWIG_NewPointerObj((void *) &$varname, $1_descriptor, 0);
  191. }
  192. %typemap(varout) SWIGTYPE && {
  193. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  194. $result = SWIG_NewPointerObj((void *) &$varname, $1_descriptor, 0);
  195. }
  196. /* special typemaps for class pointers */
  197. %typemap(in) SWIGTYPE (CLASS::*) {
  198. char err_msg[256];
  199. if (C_swig_is_pair($input)) {
  200. /* try and convert pointer object */
  201. void *result;
  202. if (!SWIG_ConvertPtr(C_block_item($input,1), &result, $descriptor, 0)) {
  203. C_word ptr = C_block_item($input,0);
  204. if (C_swig_is_string(ptr)) {
  205. SWIG_UnpackData(C_c_string(ptr), (void *) &$1, sizeof($type));
  206. } else {
  207. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", $argnum, ($descriptor->str ? $descriptor->str : $descriptor->name));
  208. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  209. }
  210. } else {
  211. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", $argnum, ($descriptor->str ? $descriptor->str : $descriptor->name));
  212. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  213. }
  214. } else {
  215. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", $argnum, ($descriptor->str ? $descriptor->str : $descriptor->name));
  216. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  217. }
  218. }
  219. %typemap(out) SWIGTYPE (CLASS::*) {
  220. size_t ptr_size = sizeof($type);
  221. C_word *known_space = C_alloc(C_SIZEOF_PAIR + C_SIZEOF_STRING(2*ptr_size) + C_SIZEOF_SWIG_POINTER);
  222. char *temp = (char *)malloc(2*ptr_size);
  223. C_word ptr = SWIG_NewPointerObj((void *) known_space, $descriptor, 0);
  224. SWIG_PackData(temp, (void *) &$1, ptr_size);
  225. $result = C_pair(&known_space, C_string(&known_space, 2*ptr_size, temp), ptr);
  226. free(temp);
  227. }
  228. %typemap(varin) SWIGTYPE (CLASS::*) {
  229. char err_msg[256];
  230. if (C_swig_is_pair($input)) {
  231. /* try and convert pointer object */
  232. void *result;
  233. if (!SWIG_ConvertPtr(C_block_item($input,1), &result, $descriptor, 0)) {
  234. C_word ptr = C_block_item($input,0);
  235. if (C_swig_is_string(ptr)) {
  236. SWIG_UnpackData(C_c_string(ptr), (void *) &$1, sizeof($type));
  237. } else {
  238. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", 1, ($descriptor->str ? $descriptor->str : $descriptor->name));
  239. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  240. }
  241. } else {
  242. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", 1, ($descriptor->str ? $descriptor->str : $descriptor->name));
  243. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  244. }
  245. } else {
  246. snprintf(err_msg, sizeof(err_msg), "Type error in argument #%i: expected %s", 1, ($descriptor->str ? $descriptor->str : $descriptor->name));
  247. SWIG_Chicken_Barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, err_msg);
  248. }
  249. }
  250. %typemap(varout) SWIGTYPE (CLASS::*) {
  251. size_t ptr_size = sizeof($type);
  252. C_word *known_space = C_alloc(C_SIZEOF_PAIR + C_SIZEOF_STRING(2*ptr_size) + C_SIZEOF_SWIG_POINTER);
  253. char *temp = (char *)malloc(2*ptr_size);
  254. C_word ptr = SWIG_NewPointerObj((void *) known_space, $descriptor, 0);
  255. SWIG_PackData(temp, (void *) &$varname, ptr_size);
  256. $result = C_pair(&known_space, C_string(&known_space, 2*ptr_size, temp), ptr);
  257. free(temp);
  258. }
  259. /* Pass-by-value */
  260. %typemap(in,closcode="(slot-ref $input 'swig-this)") SWIGTYPE($&1_ltype argp) {
  261. argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, $argnum, 0);
  262. $1 = *argp;
  263. }
  264. %typemap(varin,closcode="(slot-ref $input 'swig-this)") SWIGTYPE {
  265. $&1_ltype argp;
  266. argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, 1, 0);
  267. $1 = *argp;
  268. }
  269. %typemap(out) SWIGTYPE
  270. #ifdef __cplusplus
  271. {
  272. $&1_ltype resultptr;
  273. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  274. resultptr = new $1_ltype((const $1_ltype &) $1);
  275. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 1);
  276. }
  277. #else
  278. {
  279. $&1_ltype resultptr;
  280. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  281. resultptr = ($&1_ltype) malloc(sizeof($1_type));
  282. memmove(resultptr, &$1, sizeof($1_type));
  283. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 1);
  284. }
  285. #endif
  286. %typemap(varout) SWIGTYPE
  287. #ifdef __cplusplus
  288. {
  289. $&1_ltype resultptr;
  290. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  291. resultptr = new $1_ltype((const $1_ltype&) $1);
  292. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 0);
  293. }
  294. #else
  295. {
  296. $&1_ltype resultptr;
  297. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  298. resultptr = ($&1_ltype) malloc(sizeof($1_type));
  299. memmove(resultptr, &$1, sizeof($1_type));
  300. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 0);
  301. }
  302. #endif
  303. /* --- Output values --- */
  304. /* Strings */
  305. %typemap(out)
  306. char *
  307. { char *s = (char*) $1;
  308. if ($1 == NULL) {
  309. $result = C_SCHEME_FALSE;
  310. }
  311. else {
  312. int string_len = strlen ((char *) ($1));
  313. C_word *string_space = C_alloc (C_SIZEOF_STRING (string_len));
  314. $result = C_string (&string_space, string_len, s);
  315. }
  316. }
  317. %typemap(varout)
  318. char *
  319. { char *s = (char*) $varname;
  320. if ($varname == NULL) {
  321. $result = C_SCHEME_FALSE;
  322. }
  323. else {
  324. int string_len = strlen ($varname);
  325. C_word *string_space = C_alloc (C_SIZEOF_STRING (string_len));
  326. $result = C_string (&string_space, string_len, s);
  327. }
  328. }
  329. %typemap(throws) char *
  330. {
  331. if ($1 == NULL) {
  332. SWIG_Chicken_ThrowException(C_SCHEME_FALSE);
  333. } else {
  334. int string_len = strlen($1);
  335. C_word *string_space = C_alloc(C_SIZEOF_STRING(string_len));
  336. SWIG_Chicken_ThrowException(C_string(&string_space, string_len, (char *) $1));
  337. }
  338. }
  339. /* Void */
  340. %typemap(out) void
  341. %{
  342. $result = C_SCHEME_UNDEFINED;
  343. %}
  344. /* Special typemap for character array return values */
  345. %typemap(out)
  346. char [ANY], const char [ANY]
  347. %{ if ($1 == NULL) {
  348. $result = C_SCHEME_FALSE;
  349. }
  350. else {
  351. const int string_len = strlen ($1);
  352. C_word *string_space = C_alloc (C_SIZEOF_STRING (string_len));
  353. $result = C_string (&string_space, string_len, $1);
  354. } %}
  355. /* Primitive types--return by value */
  356. /* --- Variable input --- */
  357. /* A string */
  358. #ifdef __cplusplus
  359. %typemap(varin) char * {
  360. if ($input == C_SCHEME_FALSE) {
  361. $1 = NULL;
  362. }
  363. else if (!C_swig_is_string ($input)) {
  364. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "C variable '$name ($1_ltype)'");
  365. }
  366. else {
  367. char *temp = C_c_string ($input);
  368. int len = C_header_size ($input);
  369. if ($1) delete [] $1;
  370. $1 = ($type) new char[len+1];
  371. strncpy((char*)$1, temp, len);
  372. ((char*)$1) [len] = 0;
  373. }
  374. }
  375. %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * {
  376. if ($input == C_SCHEME_FALSE) {
  377. $1 = NULL;
  378. }
  379. else if (!C_swig_is_string ($input)) {
  380. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "C variable '$name ($1_ltype)'");
  381. }
  382. else {
  383. char *temp = C_c_string ($input);
  384. int len = C_header_size ($input);
  385. $1 = ($type) new char[len+1];
  386. strncpy((char*)$1,temp,len);
  387. ((char*)$1) [len] = 0;
  388. }
  389. }
  390. #else
  391. %typemap(varin) char * {
  392. if ($input == C_SCHEME_FALSE) {
  393. $1 = NULL;
  394. }
  395. else if (!C_swig_is_string ($input)) {
  396. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "C variable '$name ($1_ltype)'");
  397. }
  398. else {
  399. char *temp = C_c_string ($input);
  400. int len = C_header_size ($input);
  401. if ($1) free((char*) $1);
  402. $1 = ($type) malloc(len+1);
  403. strncpy((char*)$1,temp,len);
  404. ((char*)$1) [len] = 0;
  405. }
  406. }
  407. %typemap(varin,warning="451:Setting const char * variable may leak memory") const char * {
  408. if ($input == C_SCHEME_FALSE) {
  409. $1 = NULL;
  410. }
  411. else if (!C_swig_is_string ($input)) {
  412. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "C variable '$name ($1_ltype)'");
  413. }
  414. else {
  415. char *temp = C_c_string ($input);
  416. int len = C_header_size ($input);
  417. $1 = ($type) malloc(len+1);
  418. strncpy((char*)$1,temp,len);
  419. ((char*)$1) [len] = 0;
  420. }
  421. }
  422. #endif
  423. %typemap(varin) char [] {
  424. swig_barf(SWIG_BARF1_BAD_ARGUMENT_TYPE, "C/C++ variable '$name' is read-only");
  425. }
  426. /* Special case for string array variables */
  427. %typemap(varin) char [ANY] {
  428. if ($input == C_SCHEME_FALSE) {
  429. memset($1,0,$1_dim0*sizeof(char));
  430. }
  431. else if (!C_swig_is_string ($input)) {
  432. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "C variable '$name ($1_ltype)'");
  433. }
  434. else {
  435. char *temp = C_c_string ($input);
  436. strncpy($1,temp,$1_dim0*sizeof(char));
  437. }
  438. }
  439. /* --- Variable output --- */
  440. /* Void */
  441. %typemap(varout) void "$result = C_SCHEME_UNDEFINED;";
  442. /* Special typemap for character array return values */
  443. %typemap(varout) char [ANY], const char [ANY]
  444. %{ if ($varname == NULL) {
  445. $result = C_SCHEME_FALSE;
  446. }
  447. else {
  448. const int string_len = strlen ($varname);
  449. C_word *string_space = C_alloc (C_SIZEOF_STRING (string_len));
  450. $result = C_string (&string_space, string_len, (char *) $varname);
  451. }
  452. %}
  453. /* --- Constants --- */
  454. %typemap(constcode) char *
  455. "static const char *$result = $value;"
  456. %typemap(constcode) SWIGTYPE *, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE []
  457. "static const void *$result = (void*) $value;"
  458. /* ------------------------------------------------------------
  459. * String & length
  460. * ------------------------------------------------------------ */
  461. %typemap(in) (char *STRING, int LENGTH), (char *STRING, size_t LENGTH) {
  462. if ($input == C_SCHEME_FALSE) {
  463. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Cannot use a null/#f string for a char*, int arguments");
  464. }
  465. else if (C_swig_is_string ($input)) {
  466. $1 = ($1_ltype) C_c_string ($input);
  467. $2 = ($2_ltype) C_header_size ($input);
  468. }
  469. else {
  470. swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument #$argnum is not of type 'string'");
  471. }
  472. }
  473. /* ------------------------------------------------------------
  474. * CHICKEN types
  475. * ------------------------------------------------------------ */
  476. %typemap(in) C_word "$1 = $input;";
  477. %typemap(out) C_word "$result = $1;";
  478. /* ------------------------------------------------------------
  479. * Typechecking rules
  480. * ------------------------------------------------------------ */
  481. %typecheck(SWIG_TYPECHECK_INTEGER)
  482. bool, const bool &
  483. {
  484. $1 = C_swig_is_bool ($input);
  485. }
  486. %typecheck(SWIG_TYPECHECK_INTEGER)
  487. int, short,
  488. unsigned int, unsigned short,
  489. signed char, unsigned char,
  490. const int &, const short &,
  491. const unsigned int &, const unsigned short &,
  492. enum SWIGTYPE
  493. {
  494. $1 = C_swig_is_fixnum ($input);
  495. }
  496. %typecheck(SWIG_TYPECHECK_INTEGER)
  497. long,
  498. unsigned long,
  499. long long, unsigned long long,
  500. const long &,
  501. const unsigned long &,
  502. const long long &, const unsigned long long &
  503. {
  504. $1 = (C_swig_is_bool ($input) ||
  505. C_swig_is_fixnum ($input) ||
  506. C_swig_is_flonum ($input)) ? 1 : 0;
  507. }
  508. %typecheck(SWIG_TYPECHECK_DOUBLE)
  509. float, double,
  510. const float &, const double &
  511. {
  512. $1 = C_swig_is_flonum ($input);
  513. }
  514. %typecheck(SWIG_TYPECHECK_CHAR) char {
  515. $1 = C_swig_is_string ($input);
  516. }
  517. %typecheck(SWIG_TYPECHECK_STRING) char * {
  518. $1 = C_swig_is_string ($input);
  519. }
  520. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE &&, SWIGTYPE [] {
  521. void *ptr;
  522. $1 = !SWIG_ConvertPtr($input, &ptr, $1_descriptor, 0);
  523. }
  524. %typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
  525. void *ptr;
  526. $1 = !SWIG_ConvertPtr($input, &ptr, 0, 0);
  527. }
  528. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE &
  529. {
  530. void *ptr = 0;
  531. if (SWIG_ConvertPtr($input, &ptr, $descriptor, 0)) {
  532. /* error */
  533. $1 = 0;
  534. } else {
  535. $1 = (ptr != 0);
  536. }
  537. }
  538. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE &&
  539. {
  540. void *ptr = 0;
  541. if (SWIG_ConvertPtr($input, &ptr, $descriptor, 0)) {
  542. /* error */
  543. $1 = 0;
  544. } else {
  545. $1 = (ptr != 0);
  546. }
  547. }
  548. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE
  549. {
  550. void *ptr = 0;
  551. if (SWIG_ConvertPtr($input, &ptr, $&descriptor, 0)) {
  552. /* error */
  553. $1 = 0;
  554. } else {
  555. $1 = (ptr != 0);
  556. }
  557. }
  558. /* ------------------------------------------------------------
  559. * Exception handling
  560. * ------------------------------------------------------------ */
  561. /* ------------------------------------------------------------
  562. * --- Exception handling ---
  563. * ------------------------------------------------------------ */
  564. %typemap(throws) SWIGTYPE {
  565. $&ltype temp = new $ltype($1);
  566. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  567. C_word ptr = SWIG_NewPointerObj(temp, $&descriptor,1);
  568. SWIG_Chicken_ThrowException(ptr);
  569. }
  570. %typemap(throws) SWIGTYPE * {
  571. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  572. C_word ptr = SWIG_NewPointerObj((void *) $1, $descriptor, 0);
  573. SWIG_Chicken_ThrowException(ptr);
  574. }
  575. %typemap(throws) SWIGTYPE [ANY] {
  576. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  577. C_word ptr = SWIG_NewPointerObj((void *) $1, $descriptor, 0);
  578. SWIG_Chicken_ThrowException(ptr);
  579. }
  580. %typemap(throws) SWIGTYPE & {
  581. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  582. C_word ptr = SWIG_NewPointerObj((void *)&($1),$descriptor,0);
  583. SWIG_Chicken_ThrowException(ptr);
  584. }
  585. %typemap(throws) SWIGTYPE && {
  586. C_word *known_space = C_alloc(C_SIZEOF_SWIG_POINTER);
  587. C_word ptr = SWIG_NewPointerObj((void *)&($1),$descriptor,0);
  588. SWIG_Chicken_ThrowException(ptr);
  589. }
  590. /* ------------------------------------------------------------
  591. * ANSI C typemaps
  592. * ------------------------------------------------------------ */
  593. %apply unsigned long { size_t };
  594. /* ------------------------------------------------------------
  595. * Various
  596. * ------------------------------------------------------------ */
  597. /* Array reference typemaps */
  598. %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
  599. %apply SWIGTYPE && { SWIGTYPE ((&&)[ANY]) }
  600. /* const pointers */
  601. %apply SWIGTYPE * { SWIGTYPE *const }
  602. /* ------------------------------------------------------------
  603. * Overloaded operator support
  604. * ------------------------------------------------------------ */
  605. #ifdef __cplusplus
  606. %rename(__add__) *::operator+;
  607. %rename(__pos__) *::operator+();
  608. %rename(__pos__) *::operator+() const;
  609. %rename(__sub__) *::operator-;
  610. %rename(__neg__) *::operator-();
  611. %rename(__neg__) *::operator-() const;
  612. %rename(__mul__) *::operator*;
  613. %rename(__div__) *::operator/;
  614. %rename(__mod__) *::operator%;
  615. %rename(__lshift__) *::operator<<;
  616. %rename(__rshift__) *::operator>>;
  617. %rename(__and__) *::operator&;
  618. %rename(__or__) *::operator|;
  619. %rename(__xor__) *::operator^;
  620. %rename(__invert__) *::operator~;
  621. %rename(__iadd__) *::operator+=;
  622. %rename(__isub__) *::operator-=;
  623. %rename(__imul__) *::operator*=;
  624. %rename(__idiv__) *::operator/=;
  625. %rename(__imod__) *::operator%=;
  626. %rename(__ilshift__) *::operator<<=;
  627. %rename(__irshift__) *::operator>>=;
  628. %rename(__iand__) *::operator&=;
  629. %rename(__ior__) *::operator|=;
  630. %rename(__ixor__) *::operator^=;
  631. %rename(__lt__) *::operator<;
  632. %rename(__le__) *::operator<=;
  633. %rename(__gt__) *::operator>;
  634. %rename(__ge__) *::operator>=;
  635. %rename(__eq__) *::operator==;
  636. %rename(__ne__) *::operator!=;
  637. /* Special cases */
  638. %rename(__call__) *::operator();
  639. #endif
  640. /* Warnings for certain CHICKEN keywords */
  641. %include <chickenkw.swg>
  642. /* TinyCLOS <--> Low-level CHICKEN */
  643. %typemap("clos_in") SIMPLE_CLOS_OBJECT * "(slot-ref $input (quote this))"
  644. %typemap("clos_out") SIMPLE_CLOS_OBJECT * "(make $class (quote this) $1)"
  645. %insert(header) %{
  646. #ifdef __cplusplus
  647. extern "C" {
  648. #endif
  649. /* Chicken initialization function */
  650. SWIGEXPORT void SWIG_init(C_word, C_word, C_word) C_noret;
  651. #ifdef __cplusplus
  652. }
  653. #endif
  654. %}
  655. %insert(closprefix) "swigclosprefix.scm"
  656. %insert(init) "swiginit.swg"
  657. %insert(init) %{
  658. /* CHICKEN initialization function */
  659. #ifdef __cplusplus
  660. extern "C" {
  661. #endif
  662. SWIGEXPORT void SWIG_init(C_word argc, C_word closure, C_word continuation) {
  663. int i;
  664. C_word sym;
  665. C_word tmp;
  666. C_word *a;
  667. C_word ret;
  668. C_word *return_vec;
  669. SWIG_InitializeModule(0);
  670. SWIG_PropagateClientData();
  671. ret = C_SCHEME_TRUE;
  672. #if $veclength
  673. return_vec = C_alloc(C_SIZEOF_VECTOR($veclength));
  674. ret = (C_word) return_vec;
  675. *(return_vec++) = C_VECTOR_TYPE | $veclength;
  676. #endif
  677. a = C_alloc(2*$nummethods$symsize);
  678. %}