PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/mkoeppe-1-3-mzscheme/SWIG/Source/Modules1.1/guile.cxx

#
C++ | 1052 lines | 747 code | 137 blank | 168 comment | 175 complexity | dc41423329ec08932118fe5c3b6a9618 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /******************************************************************************
  2. * Simplified Wrapper and Interface Generator (SWIG)
  3. *
  4. * Author : David Beazley
  5. *
  6. * Department of Computer Science
  7. * University of Chicago
  8. * 1100 E 58th Street
  9. * Chicago, IL 60637
  10. * beazley@cs.uchicago.edu
  11. *
  12. * Please read the file LICENSE for the copyright and terms by which SWIG
  13. * can be used and distributed.
  14. *****************************************************************************/
  15. static char cvsroot[] = "$Header$";
  16. /***********************************************************************
  17. * $Header$
  18. *
  19. * guile.cxx
  20. *
  21. * Definitions for adding functions to Guile
  22. ***********************************************************************/
  23. #include "mod11.h"
  24. #include "guile.h"
  25. #ifndef MACSWIG
  26. #include "swigconfig.h"
  27. #endif
  28. static char *guile_usage = (char*)"\
  29. Guile Options (available with -guile)\n\
  30. -ldflags - Print runtime libraries to link with\n\
  31. -module name - Set name of module [default \"swig\"]\n\
  32. -prefix name - Use NAME as prefix [default \"gswig_\"]\n\
  33. -package name - Set the path of the module [default NULL]\n\
  34. -linkage lstyle - Use linkage protocol LSTYLE [default `module']\n\
  35. -procdoc file - Output procedure documentation to FILE\n\
  36. -procdocformat format - Output procedure documentation in FORMAT;\n\
  37. one of `guile-1.4', `plain', `texinfo'\n\
  38. -scmstub file - Output Scheme FILE with module declaration and\n\
  39. exports; only with `passive' and `simple' linkage\n\
  40. \n\
  41. When unspecified, the default LSTYLE is `simple'. For native Guile\n\
  42. module linking (for Guile versions >=1.5.0), use `module'. Other\n\
  43. LSTYLE values are: `passive' for passive linking (no C-level\n\
  44. module-handling code), `ltdlmod' for Guile's old dynamic module\n\
  45. convention (versions <= 1.4), or `hobbit' for hobbit modules.\n\
  46. \n";
  47. // ---------------------------------------------------------------------
  48. // GUILE ()
  49. // ---------------------------------------------------------------------
  50. GUILE::GUILE ()
  51. {
  52. // Set global vars
  53. typemap_lang = (char*)"guile";
  54. // Set class vars
  55. prefix = (char*)"gswig_";
  56. module = NULL;
  57. package = NULL;
  58. linkage = GUILE_LSTYLE_SIMPLE;
  59. procdoc = NULL;
  60. scmstub = NULL;
  61. docformat = GUILE_1_4;
  62. emit_setters = 0;
  63. struct_member = 0;
  64. before_return = NULL;
  65. exported_symbols = NewString("");
  66. scmtext = NewString("");
  67. Swig_register_filebyname("scheme", scmtext);
  68. }
  69. // ---------------------------------------------------------------------
  70. // GUILE::parse_args(int argc, char *argv[])
  71. //
  72. // Parse arguments.
  73. // ---------------------------------------------------------------------
  74. void
  75. GUILE::parse_args (int argc, char *argv[])
  76. {
  77. int i, orig_len;
  78. sprintf (LibDir, "%s", "guile");
  79. // Look for certain command line options
  80. for (i = 1; i < argc; i++) {
  81. if (argv[i]) {
  82. if (strcmp (argv[i], "-help") == 0) {
  83. fputs (guile_usage, stderr);
  84. SWIG_exit (EXIT_SUCCESS);
  85. }
  86. else if (strcmp (argv[i], "-prefix") == 0) {
  87. if (argv[i + 1]) {
  88. prefix = new char[strlen (argv[i + 1]) + 2];
  89. strcpy (prefix, argv[i + 1]);
  90. Swig_mark_arg (i);
  91. Swig_mark_arg (i + 1);
  92. i++;
  93. } else {
  94. Swig_arg_error();
  95. }
  96. }
  97. else if (strcmp (argv[i], "-package") == 0) {
  98. if (argv[i + 1]) {
  99. package = new char[strlen (argv[i + 1]) + 2];
  100. strcpy (package, argv [i + 1]);
  101. Swig_mark_arg (i);
  102. Swig_mark_arg (i + 1);
  103. i++;
  104. } else {
  105. Swig_arg_error();
  106. }
  107. }
  108. else if (strcmp (argv[i], "-module") == 0) {
  109. if (argv[i + 1]) {
  110. set_module (argv[i + 1]);
  111. Swig_mark_arg (i);
  112. Swig_mark_arg (i + 1);
  113. ++i;
  114. } else {
  115. Swig_arg_error();
  116. }
  117. }
  118. else if (strcmp (argv[i], "-ldflags") == 0) {
  119. printf("%s\n", SWIG_GUILE_RUNTIME);
  120. SWIG_exit (EXIT_SUCCESS);
  121. }
  122. else if (strcmp (argv[i], "-Linkage") == 0
  123. || strcmp (argv[i], "-linkage") == 0) {
  124. if (argv[i + 1]) {
  125. if (0 == strcmp (argv[i + 1], "ltdlmod"))
  126. linkage = GUILE_LSTYLE_LTDLMOD_1_4;
  127. else if (0 == strcmp (argv[i + 1], "hobbit"))
  128. linkage = GUILE_LSTYLE_HOBBIT;
  129. else if (0 == strcmp (argv[i + 1], "simple"))
  130. linkage = GUILE_LSTYLE_SIMPLE;
  131. else if (0 == strcmp (argv[i + 1], "passive"))
  132. linkage = GUILE_LSTYLE_PASSIVE;
  133. else if (0 == strcmp (argv[i + 1], "module"))
  134. linkage = GUILE_LSTYLE_MODULE;
  135. else
  136. Swig_arg_error ();
  137. Swig_mark_arg (i);
  138. Swig_mark_arg (i + 1);
  139. i++;
  140. } else {
  141. Swig_arg_error();
  142. }
  143. }
  144. else if (strcmp (argv[i], "-procdoc") == 0) {
  145. if (argv[i + 1]) {
  146. procdoc = NewFile(argv[i + 1], (char *) "w");
  147. Swig_mark_arg (i);
  148. Swig_mark_arg (i + 1);
  149. i++;
  150. } else {
  151. Swig_arg_error();
  152. }
  153. }
  154. else if (strcmp (argv[i], "-procdocformat") == 0) {
  155. if (strcmp(argv[i+1], "guile-1.4") == 0)
  156. docformat = GUILE_1_4;
  157. else if (strcmp(argv[i+1], "plain") == 0)
  158. docformat = PLAIN;
  159. else if (strcmp(argv[i+1], "texinfo") == 0)
  160. docformat = TEXINFO;
  161. else Swig_arg_error();
  162. Swig_mark_arg(i);
  163. Swig_mark_arg(i+1);
  164. i++;
  165. }
  166. else if (strcmp (argv[i], "-emit-setters") == 0) {
  167. emit_setters = 1;
  168. Swig_mark_arg (i);
  169. }
  170. else if (strcmp (argv[i], "-scmstub") == 0) {
  171. if (argv[i + 1]) {
  172. scmstub = NewFile(argv[i + 1], (char *) "w");
  173. Swig_mark_arg (i);
  174. Swig_mark_arg (i + 1);
  175. i++;
  176. } else {
  177. Swig_arg_error();
  178. }
  179. }
  180. }
  181. }
  182. // Make sure `prefix' ends in an underscore
  183. orig_len = strlen (prefix);
  184. if (prefix[orig_len - 1] != '_') {
  185. prefix[1 + orig_len] = 0;
  186. prefix[orig_len] = '_';
  187. }
  188. /* Add a symbol for this module */
  189. Preprocessor_define ((void *) "SWIGGUILE",0);
  190. /* Read in default typemaps */
  191. SWIG_config_file("guile.i");
  192. }
  193. // --------------------------------------------------------------------
  194. // GUILE::parse()
  195. //
  196. // Parse the input file
  197. // --------------------------------------------------------------------
  198. void
  199. GUILE::parse ()
  200. {
  201. // Print out GUILE specific headers
  202. headers();
  203. // Run the parser
  204. yyparse();
  205. }
  206. // ---------------------------------------------------------------------
  207. // GUILE::set_module(char *mod_name)
  208. //
  209. // Sets the module name.
  210. // Does nothing if it's already set (so it can be overridden as a command
  211. // line option).
  212. //
  213. //----------------------------------------------------------------------
  214. void
  215. GUILE::set_module (char *mod_name)
  216. {
  217. if (module) return;
  218. module = new char [strlen (mod_name) + 1];
  219. strcpy (module, mod_name);
  220. }
  221. // ---------------------------------------------------------------------
  222. // GUILE::set_init(char *iname)
  223. //
  224. // Sets the initialization function name.
  225. // Does nothing if it's already set
  226. //
  227. //----------------------------------------------------------------------
  228. void
  229. GUILE::set_init (char *iname)
  230. {
  231. abort (); // for now -ttn
  232. set_module (iname);
  233. }
  234. // ---------------------------------------------------------------------
  235. // GUILE::headers(void)
  236. //
  237. // Generate the appropriate header files for GUILE interface.
  238. // ----------------------------------------------------------------------
  239. void
  240. GUILE::headers (void)
  241. {
  242. Printf(f_runtime, "/* -*- buffer-read-only: t -*- vi: set ro: */\n");
  243. Swig_banner (f_runtime);
  244. Printf (f_runtime, "/* Implementation : GUILE */\n\n");
  245. /* Write out directives and declarations */
  246. if (NoInclude) {
  247. Printf(f_runtime, "#define SWIG_NOINCLUDE\n");
  248. }
  249. }
  250. // --------------------------------------------------------------------
  251. // GUILE::initialize()
  252. //
  253. // Output initialization code that registers functions with the
  254. // interface.
  255. // ---------------------------------------------------------------------
  256. void
  257. GUILE::initialize (void)
  258. {
  259. if (CPlusPlus) {
  260. Printf(f_runtime, "extern \"C\" {\n\n");
  261. }
  262. switch (linkage) {
  263. case GUILE_LSTYLE_SIMPLE:
  264. /* Simple linkage; we have to export the SWIG_init function. The user can
  265. rename the function by a #define. */
  266. Printf (f_runtime, "extern void\nSWIG_init (void)\n;\n");
  267. Printf (f_init, "extern void\nSWIG_init (void)\n{\n");
  268. break;
  269. default:
  270. /* Other linkage; we make the SWIG_init function static */
  271. Printf (f_runtime, "static void\nSWIG_init (void)\n;\n");
  272. Printf (f_init, "static void\nSWIG_init (void)\n{\n");
  273. break;
  274. }
  275. Printf (f_init, "\tSWIG_Guile_Init();\n");
  276. if (CPlusPlus) {
  277. Printf(f_runtime, "\n}\n");
  278. }
  279. }
  280. void
  281. GUILE::emit_linkage (char *module_name)
  282. {
  283. DOHString *module_func = NewString("");
  284. if (CPlusPlus) {
  285. Printf(f_init, "extern \"C\" {\n\n");
  286. }
  287. Printv(module_func,module_name,0);
  288. Replace(module_func,"-", "_", DOH_REPLACE_ANY);
  289. switch (linkage) {
  290. case GUILE_LSTYLE_SIMPLE:
  291. Printf (f_init, "\n/* Linkage: simple */\n");
  292. break;
  293. case GUILE_LSTYLE_PASSIVE:
  294. Printf (f_init, "\n/* Linkage: passive */\n");
  295. Replace(module_func,"/", "_", DOH_REPLACE_ANY);
  296. Insert(module_func,0, "scm_init_");
  297. Append(module_func,"_module");
  298. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  299. Printf (f_init, " SWIG_init();\n");
  300. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  301. Printf (f_init, "}\n");
  302. break;
  303. case GUILE_LSTYLE_LTDLMOD_1_4:
  304. Printf (f_init, "\n/* Linkage: ltdlmod */\n");
  305. Replace(module_func,"/", "_", DOH_REPLACE_ANY);
  306. Insert(module_func,0, "scm_init_");
  307. Append(module_func,"_module");
  308. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  309. {
  310. DOHString *mod = NewString(module_name);
  311. Replace(mod,"/", " ", DOH_REPLACE_ANY);
  312. Printf (f_init, " scm_register_module_xxx (\"%s\", (void *) SWIG_init);\n",
  313. mod);
  314. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  315. Delete(mod);
  316. }
  317. Printf (f_init, "}\n");
  318. break;
  319. case GUILE_LSTYLE_MODULE:
  320. Printf (f_init, "\n/* Linkage: module */\n");
  321. Replace(module_func,"/", "_", DOH_REPLACE_ANY);
  322. Insert(module_func,0, "scm_init_");
  323. Append(module_func,"_module");
  324. Printf (f_init, "static void SWIG_init_helper(void *data)\n");
  325. Printf (f_init, "{\n SWIG_init();\n");
  326. if (Len(exported_symbols) > 0)
  327. Printf (f_init, " scm_c_export(%sNULL);",
  328. exported_symbols);
  329. Printf (f_init, "\n}\n\n");
  330. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  331. {
  332. DOHString *mod = NewString(module_name);
  333. Replace(mod,"/", " ", DOH_REPLACE_ANY);
  334. Printf(f_init, " SCM module = scm_c_define_module(\"%s\",\n", mod);
  335. Printf(f_init, " SWIG_init_helper, NULL);\n");
  336. Printf(f_init, " return SCM_UNSPECIFIED;\n");
  337. Delete(mod);
  338. }
  339. Printf (f_init, "}\n");
  340. break;
  341. case GUILE_LSTYLE_HOBBIT:
  342. Printf (f_init, "\n/* Linkage: hobbit */\n");
  343. Replace(module_func,"/", "_slash_", DOH_REPLACE_ANY);
  344. Insert(module_func,0, "scm_init_");
  345. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  346. {
  347. DOHString *mod = NewString(module_name);
  348. Replace(mod,"/", " ", DOH_REPLACE_ANY);
  349. Printf (f_init, " scm_register_module_xxx (\"%s\", (void *) SWIG_init);\n",
  350. mod);
  351. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  352. Delete(mod);
  353. }
  354. Printf (f_init, "}\n");
  355. break;
  356. default:
  357. abort(); // for now
  358. }
  359. if (scmstub) {
  360. /* Emit Scheme stub if requested */
  361. DOHString *mod = NewString(module_name);
  362. Replace(mod, "/", " ", DOH_REPLACE_ANY);
  363. Printf (scmstub, ";;; -*- buffer-read-only: t -*- vi: set ro: */\n");
  364. Printf (scmstub, ";;; Automatically generated by SWIG; do not edit.\n\n");
  365. if (linkage == GUILE_LSTYLE_SIMPLE
  366. || linkage == GUILE_LSTYLE_PASSIVE)
  367. Printf (scmstub, "(define-module (%s))\n\n", mod);
  368. Delete(mod);
  369. Printf (scmstub, "%s", scmtext);
  370. if ((linkage == GUILE_LSTYLE_SIMPLE
  371. || linkage == GUILE_LSTYLE_PASSIVE)
  372. && Len(exported_symbols) > 0) {
  373. DOHString *ex = NewString(exported_symbols);
  374. Replace(ex, ", ", "\n ", DOH_REPLACE_ANY);
  375. Replace(ex, "\"", "", DOH_REPLACE_ANY);
  376. Chop(ex);
  377. Printf(scmstub, "\n(export %s)\n", ex);
  378. Delete(ex);
  379. }
  380. }
  381. Delete(module_func);
  382. if (CPlusPlus) {
  383. Printf(f_init, "\n}\n");
  384. }
  385. }
  386. // ---------------------------------------------------------------------
  387. // GUILE::close(void)
  388. //
  389. // Wrap things up. Close initialization function.
  390. // ---------------------------------------------------------------------
  391. void
  392. GUILE::close (void)
  393. {
  394. SwigType_emit_type_table (f_runtime, f_wrappers);
  395. Printf (f_init, "SWIG_Guile_RegisterTypes(swig_types, swig_types_initial);\n");
  396. Printf (f_init, "}\n\n");
  397. char module_name[256];
  398. if (!module)
  399. sprintf(module_name, "swig");
  400. else {
  401. if (package)
  402. sprintf(module_name,"%s/%s", package,module);
  403. else
  404. strcpy(module_name,module);
  405. }
  406. emit_linkage (module_name);
  407. if (procdoc) {
  408. Delete(procdoc);
  409. procdoc = NULL;
  410. }
  411. if (scmstub) {
  412. Delete(scmstub);
  413. scmstub = NULL;
  414. }
  415. }
  416. /* Return true iff T is a pointer type */
  417. static int
  418. is_a_pointer (SwigType *t)
  419. {
  420. return SwigType_ispointer(SwigType_typedef_resolve_all(t));
  421. }
  422. /* Same as Swig_typemap_lookup but fall back to `int' when `enum' is
  423. requested -- enum handling is somewhat broken in the 1.1 parser.
  424. But we don't want to change it now since it is deprecated. */
  425. static char *
  426. guile_typemap_lookup(const char *op, SwigType *type, const String_or_char *pname, String_or_char *source,
  427. String_or_char *target, Wrapper *f)
  428. {
  429. char *tm;
  430. tm = Swig_typemap_lookup((char*) op, type, (char*)pname, source, target, f);
  431. if (!tm) {
  432. SwigType *base = SwigType_typedef_resolve_all(type);
  433. if (strncmp(Char(base), "enum ", 5)==0)
  434. tm = Swig_typemap_lookup((char*) op, NewSwigType(T_INT), (char*)pname, source, target, f);
  435. }
  436. return tm;
  437. }
  438. /* Lookup a typemap, replace all relevant parameters and write it to
  439. the given generalized file. Return 0 if no typemap found. */
  440. static int
  441. guile_do_typemap(DOHFile *file, const char *op,
  442. SwigType *type, const String_or_char *arg,
  443. String_or_char *source, String_or_char *target,
  444. int argnum, DOHString *name, Wrapper *f,
  445. int nonewline_p)
  446. {
  447. char *tm;
  448. if ((tm = guile_typemap_lookup(op, type, arg,
  449. source, target, f))) {
  450. String *s = NewString(tm);
  451. char argnum_s[10];
  452. sprintf(argnum_s, "%d", argnum);
  453. Replace(s,"$argnum", argnum_s, DOH_REPLACE_ANY);
  454. Replace(s,"$arg", arg, DOH_REPLACE_ANY);
  455. Replace(s,"$name", name, DOH_REPLACE_ANY);
  456. if (nonewline_p)
  457. Printv(file, s, 0);
  458. else Printv(file, s, "\n", 0);
  459. Delete(s);
  460. return 1;
  461. }
  462. else return 0;
  463. }
  464. /* Lookup a documentation typemap, replace all relevant parameters and
  465. write it to the given generalized file, providing a sensible
  466. default value. */
  467. static void
  468. guile_do_doc_typemap(DOHFile *file, const char *op,
  469. SwigType *type, const String_or_char *arg,
  470. int argnum, DOHString *name, Wrapper *f)
  471. {
  472. if (!guile_do_typemap(file, op, type, arg,
  473. NULL, NULL, argnum, name, f, 1)) {
  474. /* FIXME: Can't we provide this default via a typemap as well? */
  475. String *s = NewString(SwigType_str(type, 0));
  476. Chop(s);
  477. if (arg) Printf(file, "(%s <%s>)", arg, s);
  478. else Printf(file, "<%s>", s);
  479. Delete(s);
  480. }
  481. }
  482. /* Report an error handling the given type. */
  483. static void
  484. throw_unhandled_guile_type_error (SwigType *d)
  485. {
  486. Printf (stderr, "%s : Line %d. Unable to handle type %s.\n",input_file, line_number, SwigType_str(d,0));
  487. error_count++;
  488. }
  489. /* Write out procedure documentation */
  490. void
  491. GUILE::write_doc(const String *proc_name,
  492. const String *signature,
  493. const String *doc)
  494. {
  495. switch (docformat) {
  496. case GUILE_1_4:
  497. Printv(procdoc, "\f\n", 0);
  498. Printv(procdoc, "(", signature, ")\n", 0);
  499. Printv(procdoc, doc, "\n", 0);
  500. break;
  501. case PLAIN:
  502. Printv(procdoc, "\f", proc_name, "\n\n", 0);
  503. Printv(procdoc, "(", signature, ")\n", 0);
  504. Printv(procdoc, doc, "\n\n", 0);
  505. break;
  506. case TEXINFO:
  507. Printv(procdoc, "\f", proc_name, "\n", 0);
  508. Printv(procdoc, "@deffn primitive ", signature, "\n", 0);
  509. Printv(procdoc, doc, "\n", 0);
  510. Printv(procdoc, "@end deffn\n\n", 0);
  511. break;
  512. }
  513. }
  514. // ----------------------------------------------------------------------
  515. // GUILE::create_function(char *name, char *iname, SwigType *d,
  516. // ParmList *l)
  517. //
  518. // Create a function declaration and register it with the interpreter.
  519. // ----------------------------------------------------------------------
  520. void
  521. GUILE::create_function (char *name, char *iname, SwigType *d, ParmList *l)
  522. {
  523. Parm *p;
  524. DOHString *proc_name = 0;
  525. char source[256], target[256], wname[256];
  526. Wrapper *f = NewWrapper();;
  527. String *cleanup = NewString("");
  528. String *outarg = NewString("");
  529. String *signature = NewString("");
  530. String *returns = NewString("");
  531. int returns_list = 0;
  532. String *tmp = NewString("");
  533. int i;
  534. int numargs = 0;
  535. int numopt = 0;
  536. // Make a wrapper name for this
  537. strcpy(wname, Char(Swig_name_wrapper(name)));
  538. // Build the name for scheme.
  539. proc_name = NewString(iname);
  540. Replace(proc_name,"_", "-", DOH_REPLACE_ANY);
  541. /* Emit locals etc. into f->code; figure out which args to ignore */
  542. emit_args (d, l, f);
  543. /* Declare return variable */
  544. Wrapper_add_local (f,"gswig_result", "SCM gswig_result");
  545. Wrapper_add_local (f,"gswig_list_p", "int gswig_list_p = 0");
  546. if (procdoc)
  547. guile_do_doc_typemap(returns, "outdoc", d, NULL,
  548. 0, proc_name, f);
  549. /* Open prototype and signature */
  550. Printv(f->def, "static SCM\n", wname," (", 0);
  551. Printv(signature, proc_name, 0);
  552. /* Now write code to extract the parameters */
  553. for (p = l, i = 0; p; p=Getnext(p), i++) {
  554. SwigType *pt = Gettype(p);
  555. String *pn = Getname(p);
  556. int opt_p = (Getvalue(p)
  557. || Swig_typemap_search((char*)"default",pt,pn));
  558. // Produce names of source and target
  559. sprintf(source,"s_%d",i);
  560. sprintf(target,"%s", Char(Getlname(p)));
  561. // Handle parameter types.
  562. if (Getignore(p))
  563. Printv(f->code, "/* ", pn, " ignored... */\n", 0);
  564. else {
  565. if (numargs!=0) Printf(f->def,", ");
  566. Printf(f->def,"SCM s_%d", i);
  567. if (opt_p) {
  568. numopt++;
  569. Printf(f->code," if (s_%d != GH_NOT_PASSED) {\n", i);
  570. }
  571. ++numargs;
  572. if (guile_do_typemap(f->code, "in", pt, pn,
  573. source, target, numargs, proc_name, f, 0)) {
  574. /* nothing to do */
  575. }
  576. else {
  577. throw_unhandled_guile_type_error (pt);
  578. }
  579. if (procdoc) {
  580. /* Add to signature */
  581. Printf(signature, " ");
  582. guile_do_doc_typemap(signature, "indoc", pt, pn,
  583. numargs, proc_name, f);
  584. }
  585. if (opt_p)
  586. Printf(f->code," }\n");
  587. }
  588. /* Check if there are any constraints. */
  589. guile_do_typemap(f->code, "check", pt, pn,
  590. source, target, numargs, proc_name, f, 0);
  591. /* Pass output arguments back to the caller. */
  592. guile_do_typemap(outarg, "argout", pt, pn,
  593. source, target, numargs, proc_name, f, 0);
  594. if (procdoc) {
  595. /* Document output arguments */
  596. Clear(tmp);
  597. if (guile_do_typemap(tmp, "argoutdoc", pt, pn,
  598. source, target, numargs, proc_name, f, 1)) {
  599. if (Len(returns) == 0) { /* unspecified -> singleton */
  600. Printv(returns, tmp, 0);
  601. }
  602. else { /* append to list */
  603. Printv(returns, " ", tmp, 0);
  604. returns_list = 1;
  605. }
  606. }
  607. }
  608. // free up any memory allocated for the arguments.
  609. guile_do_typemap(cleanup, "freearg", pt, pn,
  610. source, target, numargs, proc_name, f, 0);
  611. }
  612. /* Close prototype */
  613. Printf(f->def, ")\n{\n");
  614. /* Define the scheme name in C. This define is used by several Guile
  615. macros. */
  616. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", 0);
  617. // Now write code to make the function call
  618. Printv(f->code, tab4, "gh_defer_ints();\n", 0);
  619. emit_func_call (name, d, l, f);
  620. Printv(f->code, tab4, "gh_allow_ints();\n", 0);
  621. // Now have return value, figure out what to do with it.
  622. if (guile_do_typemap(f->code, "out", d, name,
  623. (char*)"result", (char*)"gswig_result",
  624. 0, proc_name, f, 0)) {
  625. /* nothing */
  626. }
  627. else {
  628. throw_unhandled_guile_type_error (d);
  629. }
  630. // Dump the argument output code
  631. Printv(f->code,outarg,0);
  632. // Dump the argument cleanup code
  633. Printv(f->code,cleanup,0);
  634. // Look for any remaining cleanup
  635. if (NewObject) {
  636. guile_do_typemap(f->code, "newfree", d, iname,
  637. (char*)"result", (char*)"", 0, proc_name, f, 0);
  638. }
  639. // Free any memory allocated by the function being wrapped..
  640. guile_do_typemap(f->code, "ret", d, name,
  641. (char*)"result", (char*)"", 0, proc_name, f, 0);
  642. // Wrap things up (in a manner of speaking)
  643. if (before_return)
  644. Printv(f->code, before_return, "\n", 0);
  645. Printv(f->code, "return gswig_result;\n", 0);
  646. // Undefine the scheme name
  647. Printf(f->code, "#undef FUNC_NAME\n");
  648. Printf(f->code, "}\n");
  649. Wrapper_print (f, f_wrappers);
  650. if (numargs > 10) {
  651. int i;
  652. /* gh_new_procedure would complain: too many args */
  653. /* Build a wrapper wrapper */
  654. Printv(f_wrappers, "static SCM\n", wname,"_rest (SCM rest)\n", 0);
  655. Printv(f_wrappers, "{\n", 0);
  656. Printf(f_wrappers, "SCM arg[%d];\n", numargs);
  657. Printf(f_wrappers, "SWIG_Guile_GetArgs (arg, rest, %d, %d, \"%s\");\n",
  658. numargs-numopt, numopt, proc_name);
  659. Printv(f_wrappers, "return ", wname, "(", 0);
  660. Printv(f_wrappers, "arg[0]", 0);
  661. for (i = 1; i<numargs; i++)
  662. Printf(f_wrappers, ", arg[%d]", i);
  663. Printv(f_wrappers, ");\n", 0);
  664. Printv(f_wrappers, "}\n", 0);
  665. /* Register it */
  666. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s_rest, 0, 0, 1);\n",
  667. proc_name, wname, numargs-numopt, numopt);
  668. }
  669. else if (emit_setters && struct_member && strlen(Char(proc_name))>3) {
  670. int len = Len(proc_name);
  671. const char *pc = Char(proc_name);
  672. /* MEMBER-set and MEMBER-get functions. */
  673. int is_setter = (pc[len - 3] == 's');
  674. if (is_setter) {
  675. Printf(f_init, "SCM setter = ");
  676. struct_member = 2; /* have a setter */
  677. }
  678. else Printf(f_init, "SCM getter = ");
  679. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s, %d, %d, 0);\n",
  680. proc_name, wname, numargs-numopt, numopt);
  681. if (!is_setter) {
  682. /* Strip off "-get" */
  683. char *pws_name = (char*) malloc(sizeof(char) * (len - 3));
  684. strncpy(pws_name, pc, len - 3);
  685. pws_name[len - 4] = 0;
  686. if (struct_member==2) {
  687. /* There was a setter, so create a procedure with setter */
  688. Printf (f_init, "gh_define(\"%s\", "
  689. "scm_make_procedure_with_setter(getter, setter));\n",
  690. pws_name);
  691. }
  692. else {
  693. /* There was no setter, so make an alias to the getter */
  694. Printf (f_init, "gh_define(\"%s\", getter);\n",
  695. pws_name);
  696. }
  697. Printf (exported_symbols, "\"%s\", ", pws_name);
  698. free(pws_name);
  699. }
  700. }
  701. else {
  702. /* Register the function */
  703. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s, %d, %d, 0);\n",
  704. proc_name, wname, numargs-numopt, numopt);
  705. }
  706. Printf (exported_symbols, "\"%s\", ", proc_name);
  707. if (procdoc) {
  708. String *returns_text = NewString("");
  709. Printv(returns_text, "Returns ", 0);
  710. if (Len(returns)==0) Printv(returns_text, "unspecified", 0);
  711. else if (returns_list) Printv(returns_text, "list (", returns, ")", 0);
  712. else Printv(returns_text, returns, 0);
  713. write_doc(proc_name, signature, returns_text);
  714. Delete(returns_text);
  715. }
  716. Delete(proc_name);
  717. Delete(outarg);
  718. Delete(cleanup);
  719. Delete(signature);
  720. Delete(returns);
  721. Delete(tmp);
  722. DelWrapper(f);
  723. }
  724. // -----------------------------------------------------------------------
  725. // GUILE::link_variable(char *name, char *iname, SwigType *d)
  726. //
  727. // Create a link to a C variable.
  728. // This creates a single function PREFIX_var_VARNAME().
  729. // This function takes a single optional argument. If supplied, it means
  730. // we are setting this variable to some value. If omitted, it means we are
  731. // simply evaluating this variable. Either way, we return the variables
  732. // value.
  733. // -----------------------------------------------------------------------
  734. void
  735. GUILE::link_variable (char *name, char *iname, SwigType *t)
  736. {
  737. DOHString *proc_name;
  738. char var_name[256];
  739. char *tm;
  740. Wrapper *f;
  741. f = NewWrapper();
  742. // evaluation function names
  743. strcpy(var_name, Char(Swig_name_wrapper(name)));
  744. // Build the name for scheme.
  745. proc_name = NewString(iname);
  746. Replace(proc_name,"_", "-",DOH_REPLACE_ANY);
  747. if ((SwigType_type(t) != T_USER) || (is_a_pointer(t))) {
  748. Printf (f->def, "static SCM\n%s(SCM s_0)\n{\n", var_name);
  749. /* Define the scheme name in C. This define is used by several Guile
  750. macros. */
  751. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", 0);
  752. Wrapper_add_local (f, "gswig_result", "SCM gswig_result");
  753. if (!(Status & STAT_READONLY)) {
  754. /* Check for a setting of the variable value */
  755. Printf (f->code, "if (s_0 != GH_NOT_PASSED) {\n");
  756. if (guile_do_typemap(f->code, "varin",
  757. t, name, (char*) "s_0", name, 1, name, f, 0)) {
  758. /* nothing */
  759. }
  760. else {
  761. throw_unhandled_guile_type_error (t);
  762. }
  763. Printf (f->code, "}\n");
  764. }
  765. // Now return the value of the variable (regardless
  766. // of evaluating or setting)
  767. if (guile_do_typemap (f->code, "varout",
  768. t, name, name, (char*)"gswig_result",
  769. 0, name, f, 1)) {
  770. /* nothing */
  771. }
  772. else {
  773. throw_unhandled_guile_type_error (t);
  774. }
  775. Printf (f->code, "\nreturn gswig_result;\n");
  776. Printf (f->code, "#undef FUNC_NAME\n");
  777. Printf (f->code, "}\n");
  778. Wrapper_print (f, f_wrappers);
  779. // Now add symbol to the Guile interpreter
  780. if (!emit_setters
  781. || Status & STAT_READONLY) {
  782. /* Read-only variables become a simple procedure returning the
  783. value; read-write variables become a simple procedure with
  784. an optional argument. */
  785. Printf (f_init, "\t gh_new_procedure(\"%s\", (swig_guile_proc) %s, 0, %d, 0);\n",
  786. proc_name, var_name, (Status & STAT_READONLY) ? 0 : 1);
  787. }
  788. else {
  789. /* Read/write variables become a procedure with setter. */
  790. Printf (f_init, "\t{ SCM p = gh_new_procedure(\"%s\", (swig_guile_proc) %s, 0, 1, 0);\n",
  791. proc_name, var_name);
  792. Printf (f_init, "\t gh_define(\"%s\", "
  793. "scm_make_procedure_with_setter(p, p)); }\n",
  794. proc_name);
  795. }
  796. Printf (exported_symbols, "\"%s\", ", proc_name);
  797. if (procdoc) {
  798. /* Compute documentation */
  799. String *signature = NewString("");
  800. String *doc = NewString("");
  801. if (Status & STAT_READONLY) {
  802. Printv(signature, proc_name, 0);
  803. Printv(doc, "Returns constant ", 0);
  804. guile_do_doc_typemap(doc, "varoutdoc", t, NULL,
  805. 0, proc_name, f);
  806. }
  807. else {
  808. Printv(signature, proc_name,
  809. " #:optional ", 0);
  810. guile_do_doc_typemap(signature, "varindoc", t, "new-value",
  811. 1, proc_name, f);
  812. Printv(doc, "If NEW-VALUE is provided, "
  813. "set C variable to this value.\n", 0);
  814. Printv(doc, "Returns variable value ", 0);
  815. guile_do_doc_typemap(doc, "varoutdoc", t, NULL,
  816. 0, proc_name, f);
  817. }
  818. write_doc(proc_name, signature, doc);
  819. Delete(signature);
  820. Delete(doc);
  821. }
  822. } else {
  823. Printf (stderr, "%s : Line %d. ** Warning. Unable to link with "
  824. " type %s (ignored).\n",
  825. input_file, line_number, SwigType_str(t,0));
  826. }
  827. Delete(proc_name);
  828. DelWrapper(f);
  829. }
  830. // -----------------------------------------------------------------------
  831. // GUILE::declare_const(char *name, char *iname, SwigType *type, char *value)
  832. //
  833. // We create a read-only variable.
  834. // ------------------------------------------------------------------------
  835. void
  836. GUILE::declare_const (char *name, char *iname, SwigType *type, char *value)
  837. {
  838. int OldStatus = Status; // Save old status flags
  839. DOHString *proc_name;
  840. char var_name[256];
  841. DOHString *rvalue;
  842. char *tm;
  843. Wrapper *f;
  844. SwigType *nctype;
  845. f = NewWrapper();
  846. Status = STAT_READONLY; // Enable readonly mode.
  847. // Make a static variable;
  848. sprintf (var_name, "%sconst_%s", prefix, name);
  849. // Strip const qualifier from type if present
  850. nctype = NewString(type);
  851. if (SwigType_isconst(nctype)) {
  852. Delete(SwigType_pop(nctype));
  853. }
  854. // Build the name for scheme.
  855. proc_name = NewString(iname);
  856. Replace(proc_name,"_", "-", DOH_REPLACE_ANY);
  857. if ((SwigType_type(nctype) == T_USER) && (!is_a_pointer(nctype))) {
  858. Printf (stderr, "%s : Line %d. Unsupported constant value.\n",
  859. input_file, line_number);
  860. return;
  861. }
  862. // See if there's a typemap
  863. if (SwigType_type(nctype) == T_STRING) {
  864. rvalue = NewStringf("\"%s\"", value);
  865. } else if (SwigType_type(nctype) == T_CHAR) {
  866. rvalue = NewStringf("\'%s\'", value);
  867. } else {
  868. rvalue = NewString(value);
  869. }
  870. if (guile_do_typemap(f_header, "const", nctype, name,
  871. Char(rvalue), name, 0, name, f, 0)) {
  872. /* nothing */
  873. } else {
  874. // Create variable and assign it a value
  875. Printf (f_header, "static %s %s = %s;\n", SwigType_lstr(nctype,0),
  876. var_name, rvalue);
  877. }
  878. // Now create a variable declaration
  879. link_variable (var_name, iname, nctype);
  880. Status = OldStatus;
  881. Delete(nctype);
  882. Delete(proc_name);
  883. Delete(rvalue);
  884. DelWrapper(f);
  885. }
  886. void GUILE::cpp_variable(char *name, char *iname, SwigType *t)
  887. {
  888. if (emit_setters) {
  889. struct_member = 1;
  890. Printf(f_init, "{\n");
  891. Language::cpp_variable(name, iname, t);
  892. Printf(f_init, "}\n");
  893. struct_member = 0;
  894. }
  895. else {
  896. /* Only emit traditional VAR-get and VAR-set procedures */
  897. Language::cpp_variable(name, iname, t);
  898. }
  899. }
  900. void GUILE::pragma(char *lang, char *cmd, char *value)
  901. {
  902. if (strcmp(lang,(char*)"guile") == 0) {
  903. if (strcmp(cmd, (char*)"beforereturn")==0) {
  904. if (before_return)
  905. Delete(before_return);
  906. before_return = value ? NewString(value) : NULL;
  907. }
  908. }
  909. }
  910. void
  911. GUILE::import_start(char *modname) {
  912. }
  913. void
  914. GUILE::import_end() {
  915. }