PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/rel-2.0.4/Source/Modules/guile.cxx

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

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