PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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

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