PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-1-3-28/SWIG/Source/Modules/guile.cxx

#
C++ | 1826 lines | 1410 code | 210 blank | 206 comment | 429 complexity | 167afd17878d65bc88a9fce53beba686 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  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. char cvsroot_guile_cxx[] = "$Header$";
  16. /***********************************************************************
  17. * $Header$
  18. *
  19. * guile.cxx
  20. *
  21. * Definitions for adding functions to Guile
  22. ***********************************************************************/
  23. /***********************************************************************
  24. * GOOPS Support added by John Lenz <jelenz@wisc.edu> in June, 2003
  25. * Base code copied from chicken module, writen by Jonah Beckford
  26. ***********************************************************************/
  27. #include "swigmod.h"
  28. #include <ctype.h>
  29. // Note string broken in half for compilers that can't handle long strings
  30. static const char *guile_usage = (char*)"\
  31. Guile Options (available with -guile)\n\
  32. -prefix <name> - Use <name> as prefix [default \"gswig_\"]\n\
  33. -package <name> - Set the path of the module to <name>\n\
  34. (default NULL)\n\
  35. -emitsetters - Emit procedures-with-setters for variables\n\
  36. and structure slots.\n\
  37. -onlysetters - Don't emit traditional getter and setter\n\
  38. procedures for structure slots,\n\
  39. only emit procedures-with-setters.\n\
  40. -procdoc <file> - Output procedure documentation to <file>\n\
  41. -procdocformat <format> - Output procedure documentation in <format>;\n\
  42. one of `guile-1.4', `plain', `texinfo'\n\
  43. -linkage <lstyle> - Use linkage protocol <lstyle> (default `simple')\n\
  44. Use `module' for native Guile module linking\n\
  45. (requires Guile >= 1.5.0). Use `passive' for\n\
  46. passive linking (no C-level module-handling code),\n\
  47. `ltdlmod' for Guile's old dynamic module\n\
  48. convention (Guile <= 1.4), or `hobbit' for hobbit\n\
  49. modules.\n\
  50. -scmstub - Output Scheme file with module declaration and\n\
  51. exports; only with `passive' and `simple' linkage\n\
  52. -gh - Use the gh_ Guile API. (Guile <= 1.8) \n\
  53. -scm - Use the scm Guile API. (Guile >= 1.6, default) \n\
  54. -proxy - Export GOOPS class definitions\n\
  55. -emitslotaccessors - Emit accessor methods for all GOOPS slots\n" "\
  56. -primsuffix <suffix> - Name appended to primitive module when exporting\n\
  57. GOOPS classes. (default = \"primitive\")\n\
  58. -goopsprefix <prefix> - Prepend <prefix> to all goops identifiers\n\
  59. -useclassprefix - Prepend the class name to all goops identifiers\n\
  60. -exportprimitive - Add the (export ...) code from scmstub into the\n\
  61. GOOPS file.\n";
  62. static File *f_runtime = 0;
  63. static File *f_header = 0;
  64. static File *f_wrappers = 0;
  65. static File *f_init = 0;
  66. static char *prefix = (char *) "gswig_";
  67. static char *module = 0;
  68. static char *package = 0;
  69. static enum {
  70. GUILE_LSTYLE_SIMPLE, // call `SWIG_init()'
  71. GUILE_LSTYLE_PASSIVE, // passive linking (no module code)
  72. GUILE_LSTYLE_MODULE, // native guile module linking (Guile >= 1.4.1)
  73. GUILE_LSTYLE_LTDLMOD_1_4, // old (Guile <= 1.4) dynamic module convention
  74. GUILE_LSTYLE_HOBBIT // use (hobbit4d link)
  75. } linkage = GUILE_LSTYLE_SIMPLE;
  76. static File *procdoc = 0;
  77. static bool scmstub = false;
  78. static String *scmtext;
  79. static bool goops = false;
  80. static String *goopstext;
  81. static String *goopscode;
  82. static String *goopsexport;
  83. static enum {
  84. GUILE_1_4,
  85. PLAIN,
  86. TEXINFO
  87. } docformat = GUILE_1_4;
  88. static int emit_setters = 0;
  89. static int only_setters = 0;
  90. static int emit_slot_accessors = 0;
  91. static int struct_member = 0;
  92. static String *beforereturn = 0;
  93. static String *return_nothing_doc = 0;
  94. static String *return_one_doc = 0;
  95. static String *return_multi_doc = 0;
  96. static String *exported_symbols = 0;
  97. static int use_scm_interface = 1;
  98. static int exporting_destructor = 0;
  99. static String *swigtype_ptr = 0;
  100. /* GOOPS stuff */
  101. static String *primsuffix = 0;
  102. static String *class_name = 0;
  103. static String *short_class_name = 0;
  104. static String *goops_class_methods;
  105. static int in_class = 0;
  106. static int have_constructor = 0;
  107. static int useclassprefix = 0; // -useclassprefix argument
  108. static String *goopsprefix = 0; // -goopsprefix argument
  109. static int primRenamer = 0; // if (use-modules ((...) :renamer ...) is exported to GOOPS file
  110. static int exportprimitive = 0; // -exportprimitive argument
  111. static String *memberfunction_name = 0;
  112. extern "C" {
  113. static int has_classname(Node *class_node)
  114. {
  115. return Getattr(class_node, "guile:goopsclassname") != NULL;
  116. }
  117. }
  118. class GUILE : public Language {
  119. public:
  120. /* ------------------------------------------------------------
  121. * main()
  122. * ------------------------------------------------------------ */
  123. virtual void main (int argc, char *argv[]) {
  124. int i, orig_len;
  125. SWIG_library_directory("guile");
  126. SWIG_typemap_lang("guile");
  127. // Look for certain command line options
  128. for (i = 1; i < argc; i++) {
  129. if (argv[i]) {
  130. if (strcmp (argv[i], "-help") == 0) {
  131. fputs (guile_usage, stdout);
  132. SWIG_exit (EXIT_SUCCESS);
  133. }
  134. else if (strcmp (argv[i], "-prefix") == 0) {
  135. if (argv[i + 1]) {
  136. prefix = new char[strlen (argv[i + 1]) + 2];
  137. strcpy (prefix, argv[i + 1]);
  138. Swig_mark_arg (i);
  139. Swig_mark_arg (i + 1);
  140. i++;
  141. } else {
  142. Swig_arg_error();
  143. }
  144. }
  145. else if (strcmp (argv[i], "-package") == 0) {
  146. if (argv[i + 1]) {
  147. package = new char[strlen (argv[i + 1]) + 2];
  148. strcpy (package, argv [i + 1]);
  149. Swig_mark_arg (i);
  150. Swig_mark_arg (i + 1);
  151. i++;
  152. } else {
  153. Swig_arg_error();
  154. }
  155. }
  156. else if (strcmp (argv[i], "-Linkage") == 0
  157. || strcmp (argv[i], "-linkage") == 0) {
  158. if (argv[i + 1]) {
  159. if (0 == strcmp (argv[i + 1], "ltdlmod"))
  160. linkage = GUILE_LSTYLE_LTDLMOD_1_4;
  161. else if (0 == strcmp (argv[i + 1], "hobbit"))
  162. linkage = GUILE_LSTYLE_HOBBIT;
  163. else if (0 == strcmp (argv[i + 1], "simple"))
  164. linkage = GUILE_LSTYLE_SIMPLE;
  165. else if (0 == strcmp (argv[i + 1], "passive"))
  166. linkage = GUILE_LSTYLE_PASSIVE;
  167. else if (0 == strcmp (argv[i + 1], "module"))
  168. linkage = GUILE_LSTYLE_MODULE;
  169. else
  170. Swig_arg_error ();
  171. Swig_mark_arg (i);
  172. Swig_mark_arg (i + 1);
  173. i++;
  174. } else {
  175. Swig_arg_error();
  176. }
  177. }
  178. else if (strcmp (argv[i], "-procdoc") == 0) {
  179. if (argv[i + 1]) {
  180. procdoc = NewFile(argv[i + 1], (char *) "w");
  181. if (!procdoc) {
  182. FileErrorDisplay(argv[i + 1]);
  183. SWIG_exit(EXIT_FAILURE);
  184. }
  185. Swig_mark_arg (i);
  186. Swig_mark_arg (i + 1);
  187. i++;
  188. } else {
  189. Swig_arg_error();
  190. }
  191. }
  192. else if (strcmp (argv[i], "-procdocformat") == 0) {
  193. if (strcmp(argv[i+1], "guile-1.4") == 0)
  194. docformat = GUILE_1_4;
  195. else if (strcmp(argv[i+1], "plain") == 0)
  196. docformat = PLAIN;
  197. else if (strcmp(argv[i+1], "texinfo") == 0)
  198. docformat = TEXINFO;
  199. else Swig_arg_error();
  200. Swig_mark_arg(i);
  201. Swig_mark_arg(i+1);
  202. i++;
  203. }
  204. else if (strcmp (argv[i], "-emit-setters") == 0
  205. || strcmp (argv[i], "-emitsetters") == 0) {
  206. emit_setters = 1;
  207. Swig_mark_arg (i);
  208. }
  209. else if (strcmp (argv[i], "-only-setters") == 0
  210. || strcmp (argv[i], "-onlysetters") == 0) {
  211. emit_setters = 1;
  212. only_setters = 1;
  213. Swig_mark_arg (i);
  214. }
  215. else if (strcmp (argv[i], "-emit-slot-accessors") == 0
  216. || strcmp (argv[i], "-emitslotaccessors") == 0) {
  217. emit_slot_accessors = 1;
  218. Swig_mark_arg (i);
  219. }
  220. else if (strcmp (argv[i], "-scmstub") == 0) {
  221. scmstub = true;
  222. Swig_mark_arg(i);
  223. }
  224. else if ((strcmp(argv[i],"-shadow") == 0) || ((strcmp(argv[i],"-proxy") == 0))) {
  225. goops = true;
  226. Swig_mark_arg(i);
  227. }
  228. else if (strcmp(argv[i], "-gh") == 0) {
  229. use_scm_interface = 0;
  230. Swig_mark_arg(i);
  231. }
  232. else if (strcmp(argv[i], "-scm") == 0) {
  233. use_scm_interface = 1;
  234. Swig_mark_arg(i);
  235. }
  236. else if (strcmp(argv[i], "-primsuffix") == 0) {
  237. if (argv[i+1]) {
  238. primsuffix = NewString(argv[i+1]);
  239. Swig_mark_arg (i);
  240. Swig_mark_arg (i + 1);
  241. i++;
  242. } else {
  243. Swig_arg_error();
  244. }
  245. }
  246. else if (strcmp(argv[i], "-goopsprefix") == 0) {
  247. if (argv[i+1]) {
  248. goopsprefix = NewString(argv[i+1]);
  249. Swig_mark_arg (i);
  250. Swig_mark_arg (i + 1);
  251. i++;
  252. } else {
  253. Swig_arg_error();
  254. }
  255. }
  256. else if (strcmp(argv[i], "-useclassprefix") == 0) {
  257. useclassprefix = 1;
  258. Swig_mark_arg(i);
  259. }
  260. else if (strcmp(argv[i], "-exportprimitive") == 0) {
  261. exportprimitive = 1;
  262. // should use Swig_warning() here?
  263. Swig_mark_arg(i);
  264. }
  265. }
  266. }
  267. // set default value for primsuffix
  268. if (primsuffix == NULL)
  269. primsuffix = NewString("primitive");
  270. //goops support can only be enabled if passive or module linkage is used
  271. if (goops) {
  272. if (linkage != GUILE_LSTYLE_PASSIVE && linkage != GUILE_LSTYLE_MODULE) {
  273. Printf(stderr, "guile: GOOPS support requires passive or module linkage\n");
  274. exit(1);
  275. }
  276. }
  277. if (goops) {
  278. // -proxy implies -emit-setters
  279. emit_setters = 1;
  280. }
  281. if ((linkage == GUILE_LSTYLE_PASSIVE && scmstub) || linkage == GUILE_LSTYLE_MODULE)
  282. primRenamer = 1;
  283. if (exportprimitive && primRenamer) {
  284. // should use Swig_warning() ?
  285. Printf(stderr,
  286. "guile: Warning: -exportprimitive only makes sense with passive linkage without a scmstub.\n");
  287. }
  288. // Make sure `prefix' ends in an underscore
  289. orig_len = strlen (prefix);
  290. if (prefix[orig_len - 1] != '_') {
  291. prefix[1 + orig_len] = 0;
  292. prefix[orig_len] = '_';
  293. }
  294. /* Add a symbol for this module */
  295. Preprocessor_define ("SWIGGUILE 1",0);
  296. /* Read in default typemaps */
  297. if (use_scm_interface)
  298. SWIG_config_file("guile_scm.swg");
  299. else
  300. SWIG_config_file("guile_gh.swg");
  301. allow_overloading();
  302. }
  303. /* ------------------------------------------------------------
  304. * top()
  305. * ------------------------------------------------------------ */
  306. virtual int top(Node *n) {
  307. /* Initialize all of the output files */
  308. String *outfile = Getattr(n,"outfile");
  309. f_runtime = NewFile(outfile,"w");
  310. if (!f_runtime) {
  311. FileErrorDisplay(outfile);
  312. SWIG_exit(EXIT_FAILURE);
  313. }
  314. f_init = NewString("");
  315. f_header = NewString("");
  316. f_wrappers = NewString("");
  317. /* Register file targets with the SWIG file handler */
  318. Swig_register_filebyname("header",f_header);
  319. Swig_register_filebyname("wrapper",f_wrappers);
  320. Swig_register_filebyname("runtime",f_runtime);
  321. Swig_register_filebyname("init",f_init);
  322. scmtext = NewString("");
  323. Swig_register_filebyname("scheme", scmtext);
  324. exported_symbols = NewString("");
  325. goopstext = NewString("");
  326. Swig_register_filebyname("goops", goopstext);
  327. goopscode = NewString("");
  328. goopsexport = NewString("");
  329. Printf(f_runtime, "/* -*- buffer-read-only: t -*- vi: set ro: */\n");
  330. Swig_banner (f_runtime);
  331. Printf (f_runtime, "/* Implementation : GUILE */\n\n");
  332. if (!use_scm_interface) {
  333. if (SwigRuntime == 1)
  334. Printf(f_runtime, "#define SWIG_GLOBAL\n");
  335. if (SwigRuntime == 2)
  336. Printf(f_runtime, "#define SWIG_NOINCLUDE\n");
  337. }
  338. /* Write out directives and declarations */
  339. module = Swig_copy_string(Char(Getattr(n,"name")));
  340. switch (linkage) {
  341. case GUILE_LSTYLE_SIMPLE:
  342. /* Simple linkage; we have to export the SWIG_init function. The user can
  343. rename the function by a #define. */
  344. Printf (f_runtime, "#define SWIG_GUILE_INIT_STATIC extern\n");
  345. break;
  346. default:
  347. /* Other linkage; we make the SWIG_init function static */
  348. Printf (f_runtime, "#define SWIG_GUILE_INIT_STATIC static\n");
  349. break;
  350. }
  351. if (CPlusPlus) {
  352. Printf(f_runtime, "extern \"C\" {\n\n");
  353. }
  354. Printf (f_runtime, "SWIG_GUILE_INIT_STATIC void\nSWIG_init (void);\n");
  355. if (CPlusPlus) {
  356. Printf(f_runtime, "\n}\n");
  357. }
  358. Language::top(n);
  359. /* Close module */
  360. Printf(f_wrappers,"#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
  361. SwigType_emit_type_table (f_runtime, f_wrappers);
  362. Printf (f_init, "}\n\n");
  363. Printf (f_init, "#ifdef __cplusplus\n}\n#endif\n");
  364. String *module_name = NewString("");
  365. if (!module)
  366. Printv(module_name, "swig", NIL);
  367. else {
  368. if (package)
  369. Printf(module_name,"%s/%s", package, module);
  370. else
  371. Printv(module_name,module,NIL);
  372. }
  373. emit_linkage (module_name);
  374. Delete(module_name);
  375. if (procdoc) {
  376. Delete(procdoc);
  377. procdoc = NULL;
  378. }
  379. Delete(goopscode);
  380. Delete(goopsexport);
  381. Delete(goopstext);
  382. /* Close all of the files */
  383. Dump(f_header,f_runtime);
  384. Dump(f_wrappers,f_runtime);
  385. Wrapper_pretty_print(f_init,f_runtime);
  386. Delete(f_header);
  387. Delete(f_wrappers);
  388. Delete(f_init);
  389. Close(f_runtime);
  390. Delete(f_runtime);
  391. return SWIG_OK;
  392. }
  393. void emit_linkage (String *module_name) {
  394. String *module_func = NewString("");
  395. if (CPlusPlus) {
  396. Printf(f_init, "extern \"C\" {\n\n");
  397. }
  398. Printv(module_func,module_name,NIL);
  399. if (goops)
  400. Replaceall(module_func,"-", "_");
  401. switch (linkage) {
  402. case GUILE_LSTYLE_SIMPLE:
  403. Printf (f_init, "\n/* Linkage: simple */\n");
  404. break;
  405. case GUILE_LSTYLE_PASSIVE:
  406. Printf (f_init, "\n/* Linkage: passive */\n");
  407. Replaceall(module_func,"/", "_");
  408. Insert(module_func,0, "scm_init_");
  409. Append(module_func,"_module");
  410. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  411. Printf (f_init, " SWIG_init();\n");
  412. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  413. Printf (f_init, "}\n");
  414. break;
  415. case GUILE_LSTYLE_LTDLMOD_1_4:
  416. Printf (f_init, "\n/* Linkage: ltdlmod */\n");
  417. Replaceall(module_func,"/", "_");
  418. Insert(module_func,0, "scm_init_");
  419. Append(module_func,"_module");
  420. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  421. {
  422. String *mod = NewString(module_name);
  423. Replaceall(mod,"/", " ");
  424. Printf (f_init, " scm_register_module_xxx (\"%s\", (void *) SWIG_init);\n",
  425. mod);
  426. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  427. Delete(mod);
  428. }
  429. Printf (f_init, "}\n");
  430. break;
  431. case GUILE_LSTYLE_MODULE:
  432. Printf (f_init, "\n/* Linkage: module */\n");
  433. Replaceall(module_func,"/", "_");
  434. Insert(module_func,0, "scm_init_");
  435. Append(module_func,"_module");
  436. Printf (f_init, "static void SWIG_init_helper(void *data)\n");
  437. Printf (f_init, "{\n SWIG_init();\n");
  438. if (Len(exported_symbols) > 0)
  439. Printf (f_init, " scm_c_export(%sNULL);",
  440. exported_symbols);
  441. Printf (f_init, "\n}\n\n");
  442. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  443. {
  444. String *mod = NewString(module_name);
  445. if (goops)
  446. Printv(mod,"-",primsuffix,NIL);
  447. Replaceall(mod,"/", " ");
  448. Printf(f_init, " SCM module = scm_c_define_module(\"%s\",\n", mod);
  449. Printf(f_init, " SWIG_init_helper, NULL);\n");
  450. Printf(f_init, " return SCM_UNSPECIFIED;\n");
  451. Delete(mod);
  452. }
  453. Printf (f_init, "}\n");
  454. break;
  455. case GUILE_LSTYLE_HOBBIT:
  456. Printf (f_init, "\n/* Linkage: hobbit */\n");
  457. Replaceall(module_func,"/", "_slash_");
  458. Insert(module_func,0, "scm_init_");
  459. Printf (f_init, "SCM\n%s (void)\n{\n", module_func);
  460. {
  461. String *mod = NewString(module_name);
  462. Replaceall(mod,"/", " ");
  463. Printf (f_init, " scm_register_module_xxx (\"%s\", (void *) SWIG_init);\n",
  464. mod);
  465. Printf (f_init, " return SCM_UNSPECIFIED;\n");
  466. Delete(mod);
  467. }
  468. Printf (f_init, "}\n");
  469. break;
  470. default:
  471. abort(); // for now
  472. }
  473. if (scmstub) {
  474. /* Emit Scheme stub if requested */
  475. String *primitive_name = NewString(module_name);
  476. if (goops)
  477. Printv(primitive_name,"-",primsuffix,NIL);
  478. String *mod = NewString(primitive_name);
  479. Replaceall(mod, "/", " ");
  480. String *fname = NewStringf("%s%s.scm",
  481. SWIG_output_directory(),
  482. primitive_name);
  483. Delete(primitive_name);
  484. File *scmstubfile = NewFile(fname, (char *) "w");
  485. if (!scmstubfile) {
  486. FileErrorDisplay(fname);
  487. SWIG_exit(EXIT_FAILURE);
  488. }
  489. Delete(fname);
  490. Printf (scmstubfile, ";;; -*- buffer-read-only: t -*- vi: set ro: */\n");
  491. Printf (scmstubfile, ";;; Automatically generated by SWIG; do not edit.\n\n");
  492. if (linkage == GUILE_LSTYLE_SIMPLE
  493. || linkage == GUILE_LSTYLE_PASSIVE)
  494. Printf (scmstubfile, "(define-module (%s))\n\n", mod);
  495. Delete(mod);
  496. Printf (scmstubfile, "%s", scmtext);
  497. if ((linkage == GUILE_LSTYLE_SIMPLE
  498. || linkage == GUILE_LSTYLE_PASSIVE)
  499. && Len(exported_symbols) > 0) {
  500. String *ex = NewString(exported_symbols);
  501. Replaceall(ex, ", ", "\n ");
  502. Replaceall(ex, "\"", "");
  503. Chop(ex);
  504. Printf(scmstubfile, "\n(export %s)\n", ex);
  505. Delete(ex);
  506. }
  507. Delete(scmstubfile);
  508. }
  509. if (goops) {
  510. String *mod = NewString(module_name);
  511. Replaceall(mod, "/", " ");
  512. String *fname = NewStringf("%s%s.scm", SWIG_output_directory(),
  513. module_name);
  514. File *goopsfile = NewFile(fname, (char *)"w");
  515. if (!goopsfile) {
  516. FileErrorDisplay(fname);
  517. SWIG_exit(EXIT_FAILURE);
  518. }
  519. Delete(fname);
  520. Printf (goopsfile, ";;; -*- buffer-read-only: t -*- vi: set ro: */\n");
  521. Printf (goopsfile, ";;; Automatically generated by SWIG; do not edit.\n\n");
  522. Printf (goopsfile, "(define-module (%s))\n", mod);
  523. Printf (goopsfile, "%s\n", goopstext);
  524. Printf (goopsfile, "(use-modules (oop goops) (Swig common))\n");
  525. if (primRenamer) {
  526. Printf (goopsfile, "(use-modules ((%s-%s) :renamer (symbol-prefix-proc 'primitive:)))\n",
  527. mod, primsuffix);
  528. }
  529. Printf (goopsfile, "%s\n(export %s)", goopscode, goopsexport);
  530. if (exportprimitive) {
  531. String *ex = NewString(exported_symbols);
  532. Replaceall(ex, ", ", "\n ");
  533. Replaceall(ex, "\"", "");
  534. Chop(ex);
  535. Printf(goopsfile, "\n(export %s)", ex);
  536. Delete(ex);
  537. }
  538. Delete(mod);
  539. Delete(goopsfile);
  540. }
  541. Delete(module_func);
  542. if (CPlusPlus) {
  543. Printf(f_init, "\n}\n");
  544. }
  545. }
  546. /* Return true iff T is a pointer type */
  547. int is_a_pointer (SwigType *t) {
  548. return SwigType_ispointer(SwigType_typedef_resolve_all(t));
  549. }
  550. /* Report an error handling the given type. */
  551. void throw_unhandled_guile_type_error (SwigType *d) {
  552. Swig_warning(WARN_TYPEMAP_UNDEF, input_file, line_number,
  553. "Unable to handle type %s.\n", SwigType_str(d,0));
  554. }
  555. /* Write out procedure documentation */
  556. void write_doc(const String *proc_name,
  557. const String *signature,
  558. const String *doc,
  559. const String *signature2 = NULL) {
  560. switch (docformat) {
  561. case GUILE_1_4:
  562. Printv(procdoc, "\f\n", NIL);
  563. Printv(procdoc, "(", signature, ")\n", NIL);
  564. if (signature2)
  565. Printv(procdoc, "(", signature2, ")\n", NIL);
  566. Printv(procdoc, doc, "\n", NIL);
  567. break;
  568. case PLAIN:
  569. Printv(procdoc, "\f", proc_name, "\n\n", NIL);
  570. Printv(procdoc, "(", signature, ")\n", NIL);
  571. if (signature2)
  572. Printv(procdoc, "(", signature2, ")\n", NIL);
  573. Printv(procdoc, doc, "\n\n", NIL);
  574. break;
  575. case TEXINFO:
  576. Printv(procdoc, "\f", proc_name, "\n", NIL);
  577. Printv(procdoc, "@deffn primitive ", signature, "\n", NIL);
  578. if (signature2)
  579. Printv(procdoc, "@deffnx primitive ", signature2, "\n", NIL);
  580. Printv(procdoc, doc, "\n", NIL);
  581. Printv(procdoc, "@end deffn\n\n", NIL);
  582. break;
  583. }
  584. }
  585. /* returns false if the typemap is an empty string */
  586. bool handle_documentation_typemap(String *output,
  587. const String *maybe_delimiter,
  588. Parm *p,
  589. const String *typemap,
  590. const String *default_doc,
  591. const String *name = NULL)
  592. {
  593. String *tmp = NewString("");
  594. String *tm;
  595. if (!(tm = Getattr(p, typemap))) {
  596. Printf(tmp, "%s", default_doc);
  597. tm = tmp;
  598. }
  599. bool result = (Len(tm) > 0);
  600. if (maybe_delimiter && Len(output) > 0 && Len(tm) > 0) {
  601. Printv(output, maybe_delimiter, NIL);
  602. }
  603. const String *pn = (name == NULL) ? (const String *)Getattr(p,"name") : name;
  604. String *pt = Getattr(p,"type");
  605. Replaceall(tm, "$name", pn); // legacy for $parmname
  606. Replaceall(tm, "$type", SwigType_str(pt,0));
  607. /* $NAME is like $name, but marked-up as a variable. */
  608. String *ARGNAME = NewString("");
  609. if (docformat == TEXINFO)
  610. Printf(ARGNAME, "@var{%s}", pn);
  611. else Printf(ARGNAME, "%(upper)s", pn);
  612. Replaceall(tm, "$NAME", ARGNAME);
  613. Replaceall(tm, "$PARMNAME", ARGNAME);
  614. Printv(output,tm,NIL);
  615. Delete(tmp);
  616. return result;
  617. }
  618. /* ------------------------------------------------------------
  619. * functionWrapper()
  620. * Create a function declaration and register it with the interpreter.
  621. * ------------------------------------------------------------ */
  622. virtual int functionWrapper(Node *n) {
  623. String *iname = Getattr(n,"sym:name");
  624. SwigType *d = Getattr(n,"type");
  625. ParmList *l = Getattr(n,"parms");
  626. Parm *p;
  627. String *proc_name = 0;
  628. char source[256], target[256];
  629. Wrapper *f = NewWrapper();;
  630. String *cleanup = NewString("");
  631. String *outarg = NewString("");
  632. String *signature = NewString("");
  633. String *doc_body = NewString("");
  634. String *returns = NewString("");
  635. String *method_signature = NewString("");
  636. String *primitive_args = NewString("");
  637. Hash *scheme_arg_names = NewHash();
  638. int num_results = 1;
  639. String *tmp = NewString("");
  640. String *tm;
  641. int i;
  642. int numargs = 0;
  643. int numreq = 0;
  644. String *overname = 0;
  645. int args_passed_as_array = 0;
  646. int scheme_argnum = 0;
  647. bool any_specialized_arg = false;
  648. // Make a wrapper name for this
  649. String *wname = Swig_name_wrapper(iname);
  650. if (Getattr(n,"sym:overloaded")) {
  651. overname = Getattr(n,"sym:overname");
  652. args_passed_as_array = 1;
  653. } else {
  654. if (!addSymbol(iname,n)) return SWIG_ERROR;
  655. }
  656. if (overname) {
  657. Append(wname, overname);
  658. }
  659. Setattr(n,"wrap:name",wname);
  660. // Build the name for scheme.
  661. proc_name = NewString(iname);
  662. Replaceall(proc_name,"_", "-");
  663. /* Emit locals etc. into f->code; figure out which args to ignore */
  664. emit_args (d, l, f);
  665. /* Attach the standard typemaps */
  666. emit_attach_parmmaps(l,f);
  667. Setattr(n,"wrap:parms",l);
  668. /* Get number of required and total arguments */
  669. numargs = emit_num_arguments(l);
  670. numreq = emit_num_required(l);
  671. /* Declare return variable */
  672. Wrapper_add_local (f,"gswig_result", "SCM gswig_result");
  673. Wrapper_add_local (f,"gswig_list_p", "int gswig_list_p = 0");
  674. /* Get the output typemap so we can start generating documentation. Don't
  675. worry, the returned string is saved as 'tmap:out' */
  676. Swig_typemap_lookup_new("out",n,"result",0);
  677. if ((tm = Getattr(n,"tmap:out:doc"))) {
  678. Printv(returns,tm,NIL);
  679. if (Len(tm) > 0) num_results = 1;
  680. else num_results = 0;
  681. } else {
  682. String *s = SwigType_str(d,0);
  683. Chop(s);
  684. Printf(returns,"<%s>",s);
  685. Delete(s);
  686. num_results = 1;
  687. }
  688. /* Open prototype and signature */
  689. Printv(f->def, "static SCM\n", wname," (", NIL);
  690. if (args_passed_as_array) {
  691. Printv(f->def, "int argc, SCM *argv", NIL);
  692. }
  693. Printv(signature, proc_name, NIL);
  694. /* Now write code to extract the parameters */
  695. for (i = 0, p = l; i < numargs; i++) {
  696. while (checkAttribute(p,"tmap:in:numinputs","0")) {
  697. p = Getattr(p,"tmap:in:next");
  698. }
  699. SwigType *pt = Getattr(p,"type");
  700. String *ln = Getattr(p,"lname");
  701. int opt_p = (i >= numreq);
  702. // Produce names of source and target
  703. if (args_passed_as_array)
  704. sprintf(source, "argv[%d]", i);
  705. else
  706. sprintf(source,"s_%d",i);
  707. sprintf(target,"%s", Char(ln));
  708. if (!args_passed_as_array) {
  709. if (i!=0) Printf(f->def,", ");
  710. Printf(f->def,"SCM s_%d", i);
  711. }
  712. if (opt_p) {
  713. Printf(f->code," if (%s != SCM_UNDEFINED) {\n", source);
  714. }
  715. if ((tm = Getattr(p,"tmap:in"))) {
  716. Replaceall(tm,"$source",source);
  717. Replaceall(tm,"$target",target);
  718. Replaceall(tm,"$input",source);
  719. Setattr(p,"emit:input", source);
  720. Printv(f->code,tm,"\n",NIL);
  721. SwigType *pb = SwigType_typedef_resolve_all(SwigType_base(pt));
  722. SwigType *pn = Getattr(p,"name");
  723. String *argname;
  724. scheme_argnum++;
  725. if (pn && !Getattr(scheme_arg_names, pn))
  726. argname = pn;
  727. else {
  728. /* Anonymous arg or re-used argument name -- choose a name that cannot clash */
  729. argname = NewStringf("%%arg%d", scheme_argnum);
  730. }
  731. if (procdoc) {
  732. if (i == numreq) {
  733. /* First optional argument */
  734. Printf(signature, " #:optional");
  735. }
  736. /* Add to signature (arglist) */
  737. handle_documentation_typemap(signature, " ", p, "tmap:in:arglist",
  738. "$name", argname);
  739. /* Document the type of the arg in the documentation body */
  740. handle_documentation_typemap(doc_body, ", ", p, "tmap:in:doc",
  741. "$NAME is of type <$type>", argname);
  742. }
  743. if (goops) {
  744. if (i < numreq) {
  745. if (strcmp("void", Char(pt)) != 0) {
  746. Node *class_node = Swig_symbol_clookup_check(pb, Getattr(n, "sym:symtab"),
  747. has_classname);
  748. String *goopsclassname = (class_node == NULL) ? NULL :
  749. Getattr(class_node, "guile:goopsclassname");
  750. /* do input conversion */
  751. if (goopsclassname) {
  752. Printv(method_signature, " (", argname, " ", goopsclassname, ")", NIL);
  753. any_specialized_arg = true;
  754. } else {
  755. Printv(method_signature, " ", argname, NIL);
  756. }
  757. Printv(primitive_args, " ", argname, NIL);
  758. Setattr(scheme_arg_names, argname, p);
  759. }
  760. }
  761. }
  762. if (!pn) {
  763. Delete(argname);
  764. }
  765. p = Getattr(p,"tmap:in:next");
  766. } else {
  767. throw_unhandled_guile_type_error (pt);
  768. p = nextSibling(p);
  769. }
  770. if (opt_p)
  771. Printf(f->code," }\n");
  772. }
  773. if (Len(doc_body) > 0)
  774. Printf(doc_body, ".\n");
  775. /* Insert constraint checking code */
  776. for (p = l; p;) {
  777. if ((tm = Getattr(p,"tmap:check"))) {
  778. Replaceall(tm,"$target",Getattr(p,"lname"));
  779. Printv(f->code,tm,"\n",NIL);
  780. p = Getattr(p,"tmap:check:next");
  781. } else {
  782. p = nextSibling(p);
  783. }
  784. }
  785. /* Pass output arguments back to the caller. */
  786. /* Insert argument output code */
  787. for (p = l; p;) {
  788. if ((tm = Getattr(p,"tmap:argout"))) {
  789. Replaceall(tm,"$source",Getattr(p,"lname"));
  790. Replaceall(tm,"$target",Getattr(p,"lname"));
  791. Replaceall(tm,"$arg",Getattr(p,"emit:input"));
  792. Replaceall(tm,"$input",Getattr(p,"emit:input"));
  793. Printv(outarg,tm,"\n",NIL);
  794. if (procdoc) {
  795. if (handle_documentation_typemap(returns, ", ",
  796. p, "tmap:argout:doc",
  797. "$NAME (of type $type)")) {
  798. /* A documentation typemap that is not the empty string
  799. indicates that a value is returned to Scheme. */
  800. num_results++;
  801. }
  802. }
  803. p = Getattr(p,"tmap:argout:next");
  804. } else {
  805. p = nextSibling(p);
  806. }
  807. }
  808. /* Insert cleanup code */
  809. for (p = l; p;) {
  810. if ((tm = Getattr(p,"tmap:freearg"))) {
  811. Replaceall(tm,"$target",Getattr(p,"lname"));
  812. Replaceall(tm,"$input",Getattr(p,"emit:input"));
  813. Printv(cleanup,tm,"\n",NIL);
  814. p = Getattr(p,"tmap:freearg:next");
  815. } else {
  816. p = nextSibling(p);
  817. }
  818. }
  819. if (use_scm_interface && exporting_destructor) {
  820. /* Mark the destructor's argument as destroyed. */
  821. String *tm = NewString("SWIG_Guile_MarkPointerDestroyed($input);");
  822. Replaceall(tm,"$input",Getattr(l,"emit:input"));
  823. Printv(cleanup, tm, "\n", NIL);
  824. Delete(tm);
  825. }
  826. /* Close prototype */
  827. Printf(f->def, ")\n{\n");
  828. /* Define the scheme name in C. This define is used by several Guile
  829. macros. */
  830. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
  831. // Now write code to make the function call
  832. if (!use_scm_interface)
  833. Printv(f->code, tab4, "gh_defer_ints();\n", NIL);
  834. emit_action(n,f);
  835. if (!use_scm_interface)
  836. Printv(f->code, tab4, "gh_allow_ints();\n", NIL);
  837. // Now have return value, figure out what to do with it.
  838. if ((tm = Getattr(n,"tmap:out"))) {
  839. Replaceall(tm,"$result","gswig_result");
  840. Replaceall(tm,"$target","gswig_result");
  841. Replaceall(tm,"$source","result");
  842. if (GetFlag(n, "feature:new"))
  843. Replaceall(tm, "$owner", "1");
  844. else
  845. Replaceall(tm, "$owner", "0");
  846. Printv(f->code,tm,"\n",NIL);
  847. }
  848. else {
  849. throw_unhandled_guile_type_error (d);
  850. }
  851. // Dump the argument output code
  852. Printv(f->code,outarg,NIL);
  853. // Dump the argument cleanup code
  854. Printv(f->code,cleanup,NIL);
  855. // Look for any remaining cleanup
  856. if (GetFlag(n,"feature:new")) {
  857. if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
  858. Replaceall(tm,"$source","result");
  859. Printv(f->code,tm,"\n",NIL);
  860. }
  861. }
  862. // Free any memory allocated by the function being wrapped..
  863. if ((tm = Swig_typemap_lookup_new("ret",n,"result",0))) {
  864. Replaceall(tm,"$source","result");
  865. Printv(f->code,tm,"\n",NIL);
  866. }
  867. // Wrap things up (in a manner of speaking)
  868. if (beforereturn)
  869. Printv(f->code, beforereturn, "\n", NIL);
  870. Printv(f->code, "return gswig_result;\n", NIL);
  871. // Undefine the scheme name
  872. Printf(f->code, "#undef FUNC_NAME\n");
  873. Printf(f->code, "}\n");
  874. Wrapper_print (f, f_wrappers);
  875. if (!Getattr(n, "sym:overloaded")) {
  876. if (numargs > 10) {
  877. int i;
  878. /* gh_new_procedure would complain: too many args */
  879. /* Build a wrapper wrapper */
  880. Printv(f_wrappers, "static SCM\n", wname,"_rest (SCM rest)\n", NIL);
  881. Printv(f_wrappers, "{\n", NIL);
  882. Printf(f_wrappers, "SCM arg[%d];\n", numargs);
  883. Printf(f_wrappers, "SWIG_Guile_GetArgs (arg, rest, %d, %d, \"%s\");\n",
  884. numreq, numargs-numreq, proc_name);
  885. Printv(f_wrappers, "return ", wname, "(", NIL);
  886. Printv(f_wrappers, "arg[0]", NIL);
  887. for (i = 1; i<numargs; i++)
  888. Printf(f_wrappers, ", arg[%d]", i);
  889. Printv(f_wrappers, ");\n", NIL);
  890. Printv(f_wrappers, "}\n", NIL);
  891. /* Register it */
  892. if (use_scm_interface) {
  893. Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, 0, 1, (swig_guile_proc) %s_rest);\n",
  894. proc_name, wname);
  895. } else {
  896. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s_rest, 0, 0, 1);\n",
  897. proc_name, wname, numreq, numargs-numreq);
  898. }
  899. }
  900. else if (emit_setters && struct_member && strlen(Char(proc_name))>3) {
  901. int len = Len(proc_name);
  902. const char *pc = Char(proc_name);
  903. /* MEMBER-set and MEMBER-get functions. */
  904. int is_setter = (pc[len - 3] == 's');
  905. if (is_setter) {
  906. Printf(f_init, "SCM setter = ");
  907. struct_member = 2; /* have a setter */
  908. }
  909. else Printf(f_init, "SCM getter = ");
  910. if (use_scm_interface) {
  911. /* GOOPS support uses the MEMBER-set and MEMBER-get functions,
  912. so ignore only_setters in this case. */
  913. if (only_setters && !goops)
  914. Printf(f_init, "scm_c_make_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n",
  915. proc_name, numreq, numargs-numreq, wname);
  916. else
  917. Printf(f_init, "scm_c_define_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n",
  918. proc_name, numreq, numargs-numreq, wname);
  919. } else {
  920. if (only_setters && !goops)
  921. Printf(f_init, "scm_make_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n",
  922. proc_name, numreq, numargs-numreq, wname);
  923. else
  924. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s, %d, %d, 0);\n",
  925. proc_name, wname, numreq, numargs-numreq);
  926. }
  927. if (!is_setter) {
  928. /* Strip off "-get" */
  929. char *pws_name = (char*) malloc(sizeof(char) * (len - 3));
  930. strncpy(pws_name, pc, len - 3);
  931. pws_name[len - 4] = 0;
  932. if (struct_member==2) {
  933. /* There was a setter, so create a procedure with setter */
  934. if (use_scm_interface) {
  935. Printf(f_init, "scm_c_define");
  936. } else {
  937. Printf(f_init, "gh_define");
  938. }
  939. Printf (f_init, "(\"%s\", "
  940. "scm_make_procedure_with_setter(getter, setter));\n",
  941. pws_name);
  942. }
  943. else {
  944. /* There was no setter, so make an alias to the getter */
  945. if (use_scm_interface) {
  946. Printf(f_init, "scm_c_define");
  947. } else {
  948. Printf(f_init, "gh_define");
  949. }
  950. Printf (f_init, "(\"%s\", getter);\n",
  951. pws_name);
  952. }
  953. Printf (exported_symbols, "\"%s\", ", pws_name);
  954. free(pws_name);
  955. }
  956. }
  957. else {
  958. /* Register the function */
  959. if (use_scm_interface) {
  960. if (exporting_destructor) {
  961. Printf(f_init,
  962. "((swig_guile_clientdata *)(SWIGTYPE%s->clientdata))->destroy = (guile_destructor) %s;\n",
  963. swigtype_ptr, wname);
  964. //Printf(f_init, "SWIG_TypeClientData(SWIGTYPE%s, (void *) %s);\n", swigtype_ptr, wname);
  965. }
  966. Printf(f_init, "scm_c_define_gsubr(\"%s\", %d, %d, 0, (swig_guile_proc) %s);\n",
  967. proc_name, numreq, numargs-numreq, wname);
  968. } else {
  969. Printf (f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s, %d, %d, 0);\n",
  970. proc_name, wname, numreq, numargs-numreq);
  971. }
  972. }
  973. }
  974. else { /* overloaded function; don't export the single methods */
  975. if (!Getattr(n,"sym:nextSibling")) {
  976. /* Emit overloading dispatch function */
  977. int maxargs;
  978. String *dispatch = Swig_overload_dispatch(n,"return %s(argc,argv);",&maxargs);
  979. /* Generate a dispatch wrapper for all overloaded functions */
  980. Wrapper *df = NewWrapper();
  981. String *dname = Swig_name_wrapper(iname);
  982. Printv(df->def,
  983. "static SCM\n", dname,
  984. "(SCM rest)\n{\n",
  985. NIL);
  986. Printf(df->code, "#define FUNC_NAME \"%s\"\n", proc_name);
  987. Printf(df->code, "SCM argv[%d];\n", maxargs);
  988. Printf(df->code, "int argc = SWIG_Guile_GetArgs (argv, rest, %d, %d, \"%s\");\n",
  989. 0, maxargs, proc_name);
  990. Printv(df->code,dispatch,"\n",NIL);
  991. Printf(df->code,"scm_misc_error(\"%s\", \"No matching method for generic function `%s'\", SCM_EOL);\n", proc_name, iname);
  992. Printf(df->code, "#undef FUNC_NAME\n");
  993. Printv(df->code,"}\n",NIL);
  994. Wrapper_print(df,f_wrappers);
  995. if (use_scm_interface) {
  996. Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, 0, 1, (swig_guile_proc) %s);\n",
  997. proc_name, dname);
  998. } else {
  999. Printf(f_init, "gh_new_procedure(\"%s\", (swig_guile_proc) %s, 0, 0, 1);\n",
  1000. proc_name, dname);
  1001. }
  1002. DelWrapper(df);
  1003. Delete(dispatch);
  1004. Delete(dname);
  1005. }
  1006. }
  1007. Printf (exported_symbols, "\"%s\", ", proc_name);
  1008. if (!in_class || memberfunction_name) {
  1009. // export wrapper into goops file
  1010. String *method_def = NewString("");
  1011. String *goops_name;
  1012. if (in_class)
  1013. goops_name = NewString(memberfunction_name);
  1014. else
  1015. goops_name = goopsNameMapping(proc_name, (char *)"");
  1016. String *primitive_name = NewString("");
  1017. if (primRenamer)
  1018. Printv(primitive_name, "primitive:", proc_name, NIL);
  1019. else
  1020. Printv(primitive_name, proc_name, NIL);
  1021. Replaceall(method_signature, "_", "-");
  1022. Replaceall(primitive_args, "_", "-");
  1023. if (!any_specialized_arg) {
  1024. /* If there would not be any specialized argument in
  1025. the method declaration, we simply re-export the
  1026. function. This is a performance optimization. */
  1027. Printv(method_def, "(define ", goops_name, " ",
  1028. primitive_name, ")\n", NIL);
  1029. }
  1030. else if (numreq == numargs) {
  1031. Printv(method_def, "(define-method (", goops_name, method_signature, ")\n", NIL);
  1032. Printv(method_def, " (", primitive_name, primitive_args, "))\n", NIL);
  1033. }
  1034. else {
  1035. /* Handle optional args. For the rest argument, use a name
  1036. that cannot clash.*/
  1037. Printv(method_def, "(define-method (", goops_name, method_signature, " . %args)\n", NIL);
  1038. Printv(method_def, " (apply ", primitive_name, primitive_args, " %args))\n", NIL);
  1039. }
  1040. if (in_class) {
  1041. /* Defer method definition till end of class definition. */
  1042. Printv(goops_class_methods, method_def, NIL);
  1043. }
  1044. else {
  1045. Printv(goopscode, method_def, NIL);
  1046. }
  1047. Printf(goopsexport, "%s ", goops_name);
  1048. Delete(primitive_name);
  1049. Delete(goops_name);
  1050. Delete(method_def);
  1051. }
  1052. if (procdoc) {
  1053. String *returns_text = NewString("");
  1054. if (num_results == 0) Printv(returns_text, return_nothing_doc, NIL);
  1055. else if (num_results == 1) Printv(returns_text, return_one_doc, NIL);
  1056. else Printv(returns_text, return_multi_doc, NIL);
  1057. /* Substitute documentation variables */
  1058. static const char *numbers[] = {"zero", "one", "two", "three",
  1059. "four", "five", "six", "seven",
  1060. "eight", "nine", "ten", "eleven",
  1061. "twelve"};
  1062. if (num_results <= 12)
  1063. Replaceall(returns_text, "$num_values", numbers[num_results]);
  1064. else {
  1065. String *num_results_str = NewStringf("%d", num_results);
  1066. Replaceall(returns_text, "$num_values", num_results_str);
  1067. Delete(num_results_str);
  1068. }
  1069. Replaceall(returns_text, "$values", returns);
  1070. Printf(doc_body, "\n%s", returns_text);
  1071. write_doc(proc_name, signature, doc_body);
  1072. Delete(returns_text);
  1073. }
  1074. Delete(proc_name);
  1075. Delete(outarg);
  1076. Delete(cleanup);
  1077. Delete(signature);
  1078. Delete(method_signature);
  1079. Delete(primitive_args);
  1080. Delete(doc_body);
  1081. Delete(returns);
  1082. Delete(tmp);
  1083. Delete(scheme_arg_names);
  1084. DelWrapper(f);
  1085. return SWIG_OK;
  1086. }
  1087. /* ------------------------------------------------------------
  1088. * variableWrapper()
  1089. *
  1090. * Create a link to a C variable.
  1091. * This creates a single function PREFIX_var_VARNAME().
  1092. * This function takes a single optional argument. If supplied, it means
  1093. * we are setting this variable to some value. If omitted, it means we are
  1094. * simply evaluating this variable. Either way, we return the variables
  1095. * value.
  1096. * ------------------------------------------------------------ */
  1097. virtual int variableWrapper(Node *n) {
  1098. char *name = GetChar(n,"name");
  1099. char *iname = GetChar(n,"sym:name");
  1100. SwigType *t = Getattr(n,"type");
  1101. String *proc_name;
  1102. char var_name[256];
  1103. Wrapper *f;
  1104. String *tm;
  1105. if (!addSymbol(iname,n)) return SWIG_ERROR;
  1106. f = NewWrapper();
  1107. // evaluation function names
  1108. strcpy(var_name, Char(Swig_name_wrapper(iname)));
  1109. // Build the name for scheme.
  1110. proc_name = NewString(iname);
  1111. Replaceall(proc_name,"_", "-");
  1112. if (1 || (SwigType_type(t) != T_USER) || (is_a_pointer(t))) {
  1113. Printf (f->def, "static SCM\n%s(SCM s_0)\n{\n", var_name);
  1114. /* Define the scheme name in C. This define is used by several Guile
  1115. macros. */
  1116. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
  1117. Wrapper_add_local (f, "gswig_result", "SCM gswig_result");
  1118. if (!GetFlag(n,"feature:immutable")) {
  1119. /* Check for a setting of the variable value */
  1120. Printf (f->code, "if (s_0 != SCM_UNDEFINED) {\n");
  1121. if ((tm = Swig_typemap_lookup_new("varin",n,name,0))) {
  1122. Replaceall(tm,"$source","s_0");
  1123. Replaceall(tm,"$input","s_0");
  1124. Replaceall(tm,"$target",name);
  1125. /* Printv(f->code,tm,"\n",NIL); */
  1126. emit_action_code(n, f, tm);
  1127. }
  1128. else {
  1129. throw_unhandled_guile_type_error (t);
  1130. }
  1131. Printf (f->code, "}\n");
  1132. }
  1133. // Now return the value of the variable (regardless
  1134. // of evaluating or setting)
  1135. if ((tm = Swig_typemap_lookup_new("varout",n,name,0))) {
  1136. Replaceall(tm,"$source",name);
  1137. Replaceall(tm,"$target","gswig_result");
  1138. Replaceall(tm,"$result", "gswig_result");
  1139. /* Printv(f->code,tm,"\n",NIL); */
  1140. emit_action_code(n, f, tm);
  1141. }
  1142. else {
  1143. throw_unhandled_guile_type_error (t);
  1144. }
  1145. Printf (f->code, "\nreturn gswig_result;\n");
  1146. Printf (f->code, "#undef FUNC_NAME\n");
  1147. Printf (f->code, "}\n");
  1148. Wrapper_print (f, f_wrappers);
  1149. // Now add symbol to the Guile interpreter
  1150. if (!emit_setters
  1151. || GetFlag(n,"feature:immutable")) {
  1152. /* Read-only variables become a simple procedure returning the
  1153. value; read-write variables become a simple procedure with
  1154. an optional argument. */
  1155. if (use_scm_interface) {
  1156. Printf(f_init, "scm_c_define_gsubr(\"%s\", 0, %d, 0, (swig_guile_proc) %s);\n",
  1157. proc_name, !GetFlag(n, "feature:immutable"), var_name);
  1158. } else {
  1159. Printf (f_init, "\t gh_new_procedure(\"%s\", (swig_guile_proc) %s, 0, %d, 0);\n",
  1160. proc_name, var_name, !GetFlag(n,"feature:immutable"));
  1161. }
  1162. }
  1163. else {
  1164. /* Read/write variables become a procedure with setter. */
  1165. if (use_scm_interface) {
  1166. Printf(f_init, "{ SCM p = scm_c_define_gsubr(\"%s\", 0, 1, 0, (swig_guile_proc) %s);\n",
  1167. proc_name, var_name);
  1168. Printf(f_init, "scm_c_define");
  1169. } else {
  1170. Printf (f_init, "\t{ SCM p = gh_new_procedure(\"%s\", (swig_guile_proc) %s, 0, 1, 0);\n",
  1171. proc_name, var_name);
  1172. Printf(f_init, "gh_define");
  1173. }
  1174. Printf (f_init, "(\"%s\", "
  1175. "scm_make_procedure_with_setter(p, p)); }\n",
  1176. proc_name);
  1177. }
  1178. Printf (exported_symbols, "\"%s\", ", proc_name);
  1179. // export wrapper into goops file
  1180. if (!in_class) { // only if the variable is not part of a class
  1181. String *class_name = SwigType_typedef_resolve_all(SwigType_base(t));
  1182. String *goops_name = goopsNameMapping(proc_name, (char*)"");
  1183. String *primitive_name = NewString("");
  1184. if (primRenamer)
  1185. Printv(primitive_name, "primitive:", NIL);
  1186. Printv(primitive_name, proc_name, NIL);
  1187. /* Simply re-export the procedure */
  1188. Printv(goopscode, "(define ", goops_name, " ", primitive_name, ")\n", NIL);
  1189. Printf(goopsexport, "%s ", goops_name);
  1190. Delete(primitive_name);
  1191. Delete(class_name);
  1192. Delete(goops_name);
  1193. }
  1194. if (procdoc) {
  1195. /* Compute documentation */
  1196. String *signature = NewString("");
  1197. String *signature2 = NULL;
  1198. String *doc = NewString("");
  1199. if (GetFlag(n,"feature:immutable")) {
  1200. Printv(signature, proc_name, NIL);
  1201. Printv(doc, "Returns constant ", NIL);
  1202. if ((tm = Getattr(n,"tmap:varout:doc"))) {
  1203. Printv(doc,tm,NIL);
  1204. } else {
  1205. String *s = SwigType_str(t,0);
  1206. Chop(s);
  1207. Printf(doc,"<%s>",s);
  1208. Delete(s);
  1209. }
  1210. }
  1211. else if (emit_setters) {
  1212. Printv(signature, proc_name, NIL);
  1213. signature2 = NewString("");
  1214. Printv(signature2, "set! (", proc_name, ") ", NIL);
  1215. handle_documentation_typemap(signature2, NIL, n, "tmap:varin:arglist",
  1216. "new-value");
  1217. Printv(doc, "Get or set the value of the C variable, \n", NIL);
  1218. Printv(doc, "which is of type ", NIL);
  1219. handle_documentation_typemap(doc, NIL, n, "tmap:varout:doc",
  1220. "$1_type");
  1221. Printv(doc, ".");
  1222. }
  1223. else {
  1224. Printv(signature, proc_name,
  1225. " #:optional ", NIL);
  1226. if ((tm = Getattr(n,"tmap:varin:doc"))) {
  1227. Printv(signature,tm,NIL);
  1228. } else {
  1229. String *s = SwigType_str(t,0);
  1230. Chop(s);
  1231. Printf(signature,"new-value <%s>",s);
  1232. Delete(s);
  1233. }
  1234. Printv(doc, "If NEW-VALUE is provided, "
  1235. "set C variable to this value.\n", NIL);
  1236. Printv(doc, "Returns variable value ", NIL);
  1237. if ((tm = Getattr(n,"tmap:varout:doc"))) {
  1238. Printv(doc,tm,NIL);
  1239. } else {
  1240. String *s = SwigType_str(t,0);
  1241. Chop(s);
  1242. Printf(doc,"<%s>",s);
  1243. Delete(s);
  1244. }
  1245. }
  1246. write_doc(proc_name, signature, doc, signature2);
  1247. Delete(signature);
  1248. if (signature2) Delete(signature2);
  1249. Delete(doc);
  1250. }
  1251. } else {
  1252. Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number,
  1253. "Unsupported variable type %s (ignored).\n", SwigType_str(t,0));
  1254. }
  1255. Delete(proc_name);
  1256. DelWrapper(f);
  1257. return SWIG_OK;
  1258. }
  1259. /* ------------------------------------------------------------
  1260. * constantWrapper()
  1261. *
  1262. * We create a read-only variable.
  1263. * ------------------------------------------------------------ */
  1264. virtual int constantWrapper(Node *n) {
  1265. char *name = GetChar(n,"name");
  1266. char *iname = GetChar(n,"sym:name");
  1267. SwigType *type = Getattr(n,"type");
  1268. String *value = Getattr(n,"value");
  1269. String *proc_name;
  1270. char var_name[256];
  1271. String *rvalue;
  1272. Wrapper *f;
  1273. SwigType *nctype;
  1274. String *tm;
  1275. f = NewWrapper();
  1276. // Make a static variable;
  1277. sprintf (var_name, "%sconst_%s", prefix, iname);
  1278. // Strip const qualifier from type if present
  1279. nctype = NewString(type);
  1280. if (SwigType_isconst(nctype)) {
  1281. Delete(SwigType_pop(nctype));
  1282. }
  1283. // Build the name for scheme.
  1284. proc_name = NewString(iname);
  1285. Replaceall(proc_name,"_", "-");
  1286. if ((SwigType_type(nctype) == T_USER) && (!is_a_pointer(nctype))) {
  1287. Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number,
  1288. "Unsupported constant value.\n");
  1289. return SWIG_NOWRAP;
  1290. }
  1291. // See if there's a typemap
  1292. if (SwigType_type(nctype) == T_STRING) {
  1293. rvalue = NewStringf("\"%s\"", value);
  1294. } else if (SwigType_type(nctype) == T_CHAR) {
  1295. rvalue = NewStringf("\'%s\'", value);
  1296. } else {
  1297. rvalue = NewString(value);
  1298. }
  1299. if ((tm = Swig_typemap_lookup_new("constant",n,name,0))) {
  1300. Replaceall(tm,"$source",rvalue);
  1301. Replaceall(tm,"$value",rvalue);
  1302. Replaceall(tm,"$target",name);
  1303. Printv(f_header,tm,"\n",NIL);
  1304. } else {
  1305. // Create variable and assign it a value
  1306. Printf (f_header, "static %s = %s;\n", SwigType_lstr(nctype,var_name),
  1307. rvalue);
  1308. }
  1309. {
  1310. /* Hack alert: will cleanup later -- Dave */
  1311. Node *n = NewHash();
  1312. Setattr(n,"name",var_name);
  1313. Setattr(n,"sym:name",iname);
  1314. Setattr(n,"type", nctype);
  1315. SetFlag(n,"feature:immutable");
  1316. variableWrapper(n);
  1317. Delete(n);
  1318. }
  1319. Delete(nctype);
  1320. Delete(proc_name);
  1321. Delete(rvalue);
  1322. DelWrapper(f);
  1323. return SWIG_OK;
  1324. }
  1325. /* ------------------------------------------------------------
  1326. * classDeclaration()
  1327. * ------------------------------------------------------------ */
  1328. virtual int classDeclaration(Node *n) {
  1329. String *class_name = NewStringf("<%s>", Getattr(n, "sym:name"));
  1330. Setattr(n, "guile:goopsclassname", class_name);
  1331. return Language::classDeclaration(n);
  1332. }
  1333. /* ------------------------------------------------------------
  1334. * classHandler()
  1335. * ------------------------------------------------------------ */
  1336. virtual int classHandler(Node *n) {
  1337. /* Create new strings for building up a wrapper function */
  1338. have_constructor = 0;
  1339. class_name = NewString("");
  1340. short_class_name = NewString("");
  1341. Printv(class_name, "<", Getattr(n,"sym:name"), ">", NIL);
  1342. Printv(short_class_name, Getattr(n,"sym:name"), NIL);
  1343. Replaceall(class_name, "_", "-");
  1344. Replaceall(short_class_name, "_", "-");
  1345. if (!addSymbol(class_name,n)) return SWIG_ERROR;
  1346. /* Handle inheritance */
  1347. String *base_class = NewString("<");
  1348. List *baselist = Getattr(n,"bases");
  1349. if (baselist && Len(baselist)) {
  1350. Iterator i = First(baselist);
  1351. while (i.item) {
  1352. Printv(base_class,Getattr(i.item, "sym:name"),NIL);
  1353. i = Next(i);
  1354. if (i.item) {
  1355. Printf(base_class, "> <");
  1356. }
  1357. }
  1358. }
  1359. Printf(base_class, ">");
  1360. Replaceall(base_class, "_", "-");
  1361. Printv(goopscode,"(define-class ", class_name, " ", NIL);
  1362. Printf(goopsexport, "%s ", class_name);
  1363. if (Len(base_class) > 2) {
  1364. Printv(goopscode,"(", base_class, ")\n", NIL);
  1365. } else {
  1366. Printv(goopscode,"(<swig>)\n", NIL);
  1367. }
  1368. SwigType *ct = NewStringf("p.%s", Getattr(n, "name"));
  1369. swigtype_ptr = SwigType_manglestr(ct);
  1370. String *mangled_classname = Swig_name_mangle(Getattr(n, "sym:name"));
  1371. /* Export clientdata structure */
  1372. if (use_scm_interface) {
  1373. Printf(f_runtime, "static swig_guile_clientdata _swig_guile_clientdata%s = { NULL, SCM_EOL };\n",
  1374. mangled_classname);
  1375. Printv(f_init, "SWIG_TypeClientData(SWIGTYPE", swigtype_ptr,
  1376. ", (void *) &_swig_guile_clientdata", mangled_classname, ");\n", NIL);
  1377. SwigType_remember(ct);
  1378. }
  1379. Delete(ct);
  1380. /* Emit all of the members */
  1381. goops_class_methods = NewString("");
  1382. in_class = 1;
  1383. Language::classHandler(n);
  1384. in_class = 0;
  1385. Printv(goopscode," #:metaclass <swig-metaclass>\n",NIL);
  1386. if (have_c

Large files files are truncated, but you can click here to view the full file