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

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

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

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