PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Source/Modules/mzscheme.cxx

#
C++ | 827 lines | 564 code | 148 blank | 115 comment | 165 complexity | dfbda15d6615a3a764dc7f8307aff343 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /******************************************************************************
  2. * Simplified Wrapper and Interface Generator (SWIG)
  3. *
  4. * Author : David Beazley
  5. *
  6. * Department of Computer Science
  7. * University of Chicago
  8. * 1100 E 58th Street
  9. * Chicago, IL 60637
  10. * beazley@cs.uchicago.edu
  11. *
  12. * Please read the file LICENSE for the copyright and terms by which SWIG
  13. * can be used and distributed.
  14. *****************************************************************************/
  15. char cvsroot_mzscheme_cxx[] = "$Header$";
  16. /***********************************************************************
  17. * $Header$
  18. *
  19. * mzscheme.cxx
  20. *
  21. * Definitions for adding functions to Mzscheme 101
  22. ***********************************************************************/
  23. #include "swigmod.h"
  24. #include <ctype.h>
  25. static const char *usage = (char*)"\
  26. Mzscheme Options (available with -mzscheme)\n\
  27. -prefix <name> - Set a prefix <name> to be prepended to all names\n\
  28. -declaremodule - Create extension that declares a module\n\
  29. -noinit - Do not emit scheme_initialize, scheme_reload,\n\
  30. scheme_module_name functions\n";
  31. static String *fieldnames_tab = 0;
  32. static String *convert_tab = 0;
  33. static String *convert_proto_tab = 0;
  34. static String *struct_name = 0;
  35. static String *mangled_struct_name = 0;
  36. static char *prefix=0;
  37. static bool declaremodule = false;
  38. static bool noinit = false;
  39. static String *module=0;
  40. static char *mzscheme_path=(char*)"mzscheme";
  41. static String *init_func_def = 0;
  42. static File *f_runtime = 0;
  43. static File *f_header = 0;
  44. static File *f_wrappers = 0;
  45. static File *f_init = 0;
  46. // Used for garbage collection
  47. static int exporting_destructor = 0;
  48. static String *swigtype_ptr = 0;
  49. static String *cls_swigtype = 0;
  50. class MZSCHEME : public Language {
  51. public:
  52. /* ------------------------------------------------------------
  53. * main()
  54. * ------------------------------------------------------------ */
  55. virtual void main (int argc, char *argv[]) {
  56. int i;
  57. SWIG_library_directory(mzscheme_path);
  58. // Look for certain command line options
  59. for (i = 1; i < argc; i++) {
  60. if (argv[i]) {
  61. if (strcmp (argv[i], "-help") == 0) {
  62. fputs (usage, stdout);
  63. SWIG_exit (0);
  64. } else if (strcmp (argv[i], "-prefix") == 0) {
  65. if (argv[i + 1]) {
  66. prefix = new char[strlen(argv[i + 1]) + 2];
  67. strcpy(prefix, argv[i + 1]);
  68. Swig_mark_arg (i);
  69. Swig_mark_arg (i + 1);
  70. i++;
  71. } else {
  72. Swig_arg_error();
  73. }
  74. } else if (strcmp (argv[i], "-declaremodule") == 0) {
  75. declaremodule = true;
  76. Swig_mark_arg (i);
  77. } else if (strcmp (argv[i], "-noinit") == 0) {
  78. noinit = true;
  79. Swig_mark_arg (i);
  80. }
  81. }
  82. }
  83. // If a prefix has been specified make sure it ends in a '_'
  84. if (prefix) {
  85. if (prefix[strlen (prefix)] != '_') {
  86. prefix[strlen (prefix) + 1] = 0;
  87. prefix[strlen (prefix)] = '_';
  88. }
  89. } else
  90. prefix = (char*)"swig_";
  91. // Add a symbol for this module
  92. Preprocessor_define ("SWIGMZSCHEME 1",0);
  93. // Set name of typemaps
  94. SWIG_typemap_lang("mzscheme");
  95. // Read in default typemaps */
  96. SWIG_config_file("mzscheme.swg");
  97. allow_overloading();
  98. }
  99. /* ------------------------------------------------------------
  100. * top()
  101. * ------------------------------------------------------------ */
  102. virtual int top(Node *n) {
  103. /* Initialize all of the output files */
  104. String *outfile = Getattr(n,"outfile");
  105. f_runtime = NewFile(outfile,"w");
  106. if (!f_runtime) {
  107. FileErrorDisplay(outfile);
  108. SWIG_exit(EXIT_FAILURE);
  109. }
  110. f_init = NewString("");
  111. f_header = NewString("");
  112. f_wrappers = NewString("");
  113. /* Register file targets with the SWIG file handler */
  114. Swig_register_filebyname("header",f_header);
  115. Swig_register_filebyname("wrapper",f_wrappers);
  116. Swig_register_filebyname("runtime",f_runtime);
  117. init_func_def = NewString("");
  118. Swig_register_filebyname("init",init_func_def);
  119. Printf(f_runtime, "/* -*- buffer-read-only: t -*- vi: set ro: */\n");
  120. Swig_banner (f_runtime);
  121. module = Getattr(n,"name");
  122. Language::top(n);
  123. SwigType_emit_type_table (f_runtime, f_wrappers);
  124. if (!noinit) {
  125. if (declaremodule) {
  126. Printf(f_init, "#define SWIG_MZSCHEME_CREATE_MENV(env) scheme_primitive_module(scheme_intern_symbol(\"%s\"), env)\n", module);
  127. }
  128. else {
  129. Printf(f_init,"#define SWIG_MZSCHEME_CREATE_MENV(env) (env)\n");
  130. }
  131. Printf(f_init, "%s\n", Char(init_func_def));
  132. if (declaremodule) {
  133. Printf(f_init, "\tscheme_finish_primitive_module(menv);\n");
  134. }
  135. Printf (f_init, "\treturn scheme_void;\n}\n");
  136. Printf(f_init, "Scheme_Object *scheme_initialize(Scheme_Env *env) {\n");
  137. Printf(f_init, "\treturn scheme_reload(env);\n");
  138. Printf (f_init, "}\n");
  139. Printf(f_init,"Scheme_Object *scheme_module_name(void) {\n");
  140. if (declaremodule) {
  141. Printf(f_init, " return scheme_intern_symbol((char*)\"%s\");\n", module);
  142. } else {
  143. Printf(f_init," return scheme_make_symbol((char*)\"%s\");\n", module);
  144. }
  145. Printf(f_init,"}\n");
  146. }
  147. /* Close all of the files */
  148. Dump(f_header,f_runtime);
  149. Dump(f_wrappers,f_runtime);
  150. Wrapper_pretty_print(f_init,f_runtime);
  151. Delete(f_header);
  152. Delete(f_wrappers);
  153. Delete(f_init);
  154. Close(f_runtime);
  155. Delete(f_runtime);
  156. return SWIG_OK;
  157. }
  158. /* ------------------------------------------------------------
  159. * functionWrapper()
  160. * Create a function declaration and register it with the interpreter.
  161. * ------------------------------------------------------------ */
  162. void throw_unhandled_mzscheme_type_error (SwigType *d)
  163. {
  164. Swig_warning(WARN_TYPEMAP_UNDEF, input_file, line_number,
  165. "Unable to handle type %s.\n", SwigType_str(d,0));
  166. }
  167. /* Return true iff T is a pointer type */
  168. int
  169. is_a_pointer (SwigType *t)
  170. {
  171. return SwigType_ispointer(SwigType_typedef_resolve_all(t));
  172. }
  173. virtual int functionWrapper(Node *n) {
  174. char *iname = GetChar(n,"sym:name");
  175. SwigType *d = Getattr(n,"type");
  176. ParmList *l = Getattr(n,"parms");
  177. Parm *p;
  178. Wrapper *f = NewWrapper();
  179. String *proc_name = NewString("");
  180. String *source = NewString("");
  181. String *target = NewString("");
  182. String *arg = NewString("");
  183. String *cleanup = NewString("");
  184. String *outarg = NewString("");
  185. String *build = NewString("");
  186. String *tm;
  187. int argout_set = 0;
  188. int i = 0;
  189. int numargs;
  190. int numreq;
  191. String *overname = 0;
  192. // Make a wrapper name for this
  193. String *wname = Swig_name_wrapper(iname);
  194. if (Getattr(n,"sym:overloaded")) {
  195. overname = Getattr(n,"sym:overname");
  196. } else {
  197. if (!addSymbol(iname,n)) return SWIG_ERROR;
  198. }
  199. if (overname) {
  200. Append(wname, overname);
  201. }
  202. Setattr(n,"wrap:name",wname);
  203. // Build the name for Scheme.
  204. Printv(proc_name, iname,NIL);
  205. Replaceall(proc_name, "_", "-");
  206. // writing the function wrapper function
  207. Printv(f->def, "static Scheme_Object *", wname, " (", NIL);
  208. Printv(f->def, "int argc, Scheme_Object **argv", NIL);
  209. Printv(f->def, ")\n{", NIL);
  210. /* Define the scheme name in C. This define is used by several
  211. macros. */
  212. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
  213. // Declare return variable and arguments
  214. // number of parameters
  215. // they are called arg0, arg1, ...
  216. // the return value is called result
  217. emit_args(d, l, f);
  218. /* Attach the standard typemaps */
  219. emit_attach_parmmaps(l,f);
  220. Setattr(n,"wrap:parms",l);
  221. numargs = emit_num_arguments(l);
  222. numreq = emit_num_required(l);
  223. // adds local variables
  224. Wrapper_add_local(f, "lenv", "int lenv = 1");
  225. Wrapper_add_local(f, "values", "Scheme_Object *values[MAXVALUES]");
  226. // Now write code to extract the parameters (this is super ugly)
  227. for (i = 0, p = l; i < numargs; i++) {
  228. /* Skip ignored arguments */
  229. while (checkAttribute(p,"tmap:in:numinputs","0")) {
  230. p = Getattr(p,"tmap:in:next");
  231. }
  232. SwigType *pt = Getattr(p,"type");
  233. String *ln = Getattr(p,"lname");
  234. // Produce names of source and target
  235. Clear(source);
  236. Clear(target);
  237. Clear(arg);
  238. Printf(source, "argv[%d]", i);
  239. Printf(target, "%s",ln);
  240. Printv(arg, Getattr(p,"name"),NIL);
  241. if (i >= numreq) {
  242. Printf(f->code,"if (argc > %d) {\n",i);
  243. }
  244. // Handle parameter types.
  245. if ((tm = Getattr(p,"tmap:in"))) {
  246. Replaceall(tm,"$source",source);
  247. Replaceall(tm,"$target",target);
  248. Replaceall(tm,"$input",source);
  249. Setattr(p,"emit:input",source);
  250. Printv(f->code, tm, "\n", NIL);
  251. p = Getattr(p,"tmap:in:next");
  252. } else {
  253. // no typemap found
  254. // check if typedef and resolve
  255. throw_unhandled_mzscheme_type_error (pt);
  256. p = nextSibling(p);
  257. }
  258. if (i >= numreq) {
  259. Printf(f->code,"}\n");
  260. }
  261. }
  262. /* Insert constraint checking code */
  263. for (p = l; p;) {
  264. if ((tm = Getattr(p,"tmap:check"))) {
  265. Replaceall(tm,"$target",Getattr(p,"lname"));
  266. Printv(f->code,tm,"\n",NIL);
  267. p = Getattr(p,"tmap:check:next");
  268. } else {
  269. p = nextSibling(p);
  270. }
  271. }
  272. // Pass output arguments back to the caller.
  273. for (p = l; p;) {
  274. if ((tm = Getattr(p,"tmap:argout"))) {
  275. Replaceall(tm,"$source",Getattr(p,"emit:input")); /* Deprecated */
  276. Replaceall(tm,"$target",Getattr(p,"lname")); /* Deprecated */
  277. Replaceall(tm,"$arg",Getattr(p,"emit:input"));
  278. Replaceall(tm,"$input",Getattr(p,"emit:input"));
  279. Printv(outarg,tm,"\n",NIL);
  280. p = Getattr(p,"tmap:argout:next");
  281. argout_set = 1;
  282. } else {
  283. p = nextSibling(p);
  284. }
  285. }
  286. // Free up any memory allocated for the arguments.
  287. /* Insert cleanup code */
  288. for (p = l; p;) {
  289. if ((tm = Getattr(p,"tmap:freearg"))) {
  290. Replaceall(tm,"$target",Getattr(p,"lname"));
  291. Printv(cleanup,tm,"\n",NIL);
  292. p = Getattr(p,"tmap:freearg:next");
  293. } else {
  294. p = nextSibling(p);
  295. }
  296. }
  297. // Now write code to make the function call
  298. emit_action(n,f);
  299. // Now have return value, figure out what to do with it.
  300. if ((tm = Swig_typemap_lookup_new("out",n,"result",0))) {
  301. Replaceall(tm,"$source","result");
  302. Replaceall(tm,"$target","values[0]");
  303. Replaceall(tm,"$result","values[0]");
  304. if (GetFlag(n, "feature:new"))
  305. Replaceall(tm, "$owner", "1");
  306. else
  307. Replaceall(tm, "$owner", "0");
  308. Printv(f->code, tm, "\n",NIL);
  309. } else {
  310. throw_unhandled_mzscheme_type_error (d);
  311. }
  312. // Dump the argument output code
  313. Printv(f->code, Char(outarg),NIL);
  314. // Dump the argument cleanup code
  315. Printv(f->code, Char(cleanup),NIL);
  316. // Look for any remaining cleanup
  317. if (GetFlag(n,"feature:new")) {
  318. if ((tm = Swig_typemap_lookup_new("newfree",n,"result",0))) {
  319. Replaceall(tm,"$source","result");
  320. Printv(f->code, tm, "\n",NIL);
  321. }
  322. }
  323. // Free any memory allocated by the function being wrapped..
  324. if ((tm = Swig_typemap_lookup_new("ret",n,"result",0))) {
  325. Replaceall(tm,"$source","result");
  326. Printv(f->code, tm, "\n",NIL);
  327. }
  328. // Wrap things up (in a manner of speaking)
  329. Printv(f->code, tab4, "return SWIG_MzScheme_PackageValues(lenv, values);\n", NIL);
  330. Printf(f->code, "#undef FUNC_NAME\n");
  331. Printv(f->code, "}\n",NIL);
  332. Wrapper_print(f, f_wrappers);
  333. if (!Getattr(n,"sym:overloaded")) {
  334. // Now register the function
  335. char temp[256];
  336. sprintf(temp, "%d", numargs);
  337. if (exporting_destructor) {
  338. Printf(init_func_def, "SWIG_TypeClientData(SWIGTYPE%s, (void *) %s);\n", swigtype_ptr, wname);
  339. } else {
  340. Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n",
  341. proc_name, wname, proc_name, numreq, numargs);
  342. }
  343. } else {
  344. if (!Getattr(n,"sym:nextSibling")) {
  345. /* Emit overloading dispatch function */
  346. int maxargs;
  347. String *dispatch = Swig_overload_dispatch(n,"return %s(argc,argv);",&maxargs);
  348. /* Generate a dispatch wrapper for all overloaded functions */
  349. Wrapper *df = NewWrapper();
  350. String *dname = Swig_name_wrapper(iname);
  351. Printv(df->def,
  352. "static Scheme_Object *\n", dname,
  353. "(int argc, Scheme_Object **argv) {",
  354. NIL);
  355. Printv(df->code,dispatch,"\n",NIL);
  356. Printf(df->code,"scheme_signal_error(\"No matching function for overloaded '%s'\");\n", iname);
  357. Printv(df->code,"}\n",NIL);
  358. Wrapper_print(df,f_wrappers);
  359. Printf(init_func_def, "scheme_add_global(\"%s\", scheme_make_prim_w_arity(%s,\"%s\",%d,%d),menv);\n",
  360. proc_name, dname, proc_name, 0, maxargs);
  361. DelWrapper(df);
  362. Delete(dispatch);
  363. Delete(dname);
  364. }
  365. }
  366. Delete(proc_name);
  367. Delete(source);
  368. Delete(target);
  369. Delete(arg);
  370. Delete(outarg);
  371. Delete(cleanup);
  372. Delete(build);
  373. DelWrapper(f);
  374. return SWIG_OK;
  375. }
  376. /* ------------------------------------------------------------
  377. * variableWrapper()
  378. *
  379. * Create a link to a C variable.
  380. * This creates a single function _wrap_swig_var_varname().
  381. * This function takes a single optional argument. If supplied, it means
  382. * we are setting this variable to some value. If omitted, it means we are
  383. * simply evaluating this variable. Either way, we return the variables
  384. * value.
  385. * ------------------------------------------------------------ */
  386. virtual int variableWrapper(Node *n) {
  387. char *name = GetChar(n,"name");
  388. char *iname = GetChar(n,"sym:name");
  389. SwigType *t = Getattr(n,"type");
  390. String *proc_name = NewString("");
  391. char var_name[256];
  392. String *tm;
  393. String *tm2 = NewString("");;
  394. String *argnum = NewString("0");
  395. String *arg = NewString("argv[0]");
  396. Wrapper *f;
  397. if (!addSymbol(iname,n)) return SWIG_ERROR;
  398. f = NewWrapper();
  399. // evaluation function names
  400. strcpy(var_name, Char(Swig_name_wrapper(iname)));
  401. // Build the name for scheme.
  402. Printv(proc_name, iname,NIL);
  403. Replaceall(proc_name, "_", "-");
  404. if ((SwigType_type(t) != T_USER) || (is_a_pointer(t))) {
  405. Printf (f->def, "static Scheme_Object *%s(int argc, Scheme_Object** argv) {\n", var_name);
  406. Printv(f->def, "#define FUNC_NAME \"", proc_name, "\"", NIL);
  407. Wrapper_add_local (f, "swig_result", "Scheme_Object *swig_result");
  408. if (!GetFlag(n,"feature:immutable")) {
  409. /* Check for a setting of the variable value */
  410. Printf (f->code, "if (argc) {\n");
  411. if ((tm = Swig_typemap_lookup_new("varin",n,name,0))) {
  412. Replaceall(tm,"$source","argv[0]");
  413. Replaceall(tm,"$target",name);
  414. Replaceall(tm,"$input","argv[0]");
  415. Printv(f->code, tm, "\n",NIL);
  416. }
  417. else {
  418. throw_unhandled_mzscheme_type_error (t);
  419. }
  420. Printf (f->code, "}\n");
  421. }
  422. // Now return the value of the variable (regardless
  423. // of evaluating or setting)
  424. if ((tm = Swig_typemap_lookup_new("varout",n,name,0))) {
  425. Replaceall(tm,"$source",name);
  426. Replaceall(tm,"$target","swig_result");
  427. Replaceall(tm,"$result","swig_result");
  428. Printf (f->code, "%s\n", tm);
  429. }
  430. else {
  431. throw_unhandled_mzscheme_type_error (t);
  432. }
  433. Printf (f->code, "\nreturn swig_result;\n");
  434. Printf (f->code, "#undef FUNC_NAME\n");
  435. Printf (f->code, "}\n");
  436. Wrapper_print (f, f_wrappers);
  437. // Now add symbol to the MzScheme interpreter
  438. Printv(init_func_def,
  439. "scheme_add_global(\"",
  440. proc_name,
  441. "\", scheme_make_prim_w_arity(",
  442. var_name,
  443. ", \"",
  444. proc_name,
  445. "\", ",
  446. "0",
  447. ", ",
  448. "1",
  449. "), menv);\n",NIL);
  450. } else {
  451. Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number,
  452. "Unsupported variable type %s (ignored).\n", SwigType_str(t,0));
  453. }
  454. Delete(proc_name);
  455. Delete(argnum);
  456. Delete(arg);
  457. Delete(tm2);
  458. DelWrapper(f);
  459. return SWIG_OK;
  460. }
  461. /* ------------------------------------------------------------
  462. * constantWrapper()
  463. * ------------------------------------------------------------ */
  464. virtual int constantWrapper(Node *n) {
  465. char *name = GetChar(n,"name");
  466. char *iname = GetChar(n,"sym:name");
  467. SwigType *type = Getattr(n,"type");
  468. String *value = Getattr(n,"value");
  469. String *var_name = NewString("");
  470. String *proc_name = NewString("");
  471. String *rvalue = NewString("");
  472. String *temp = NewString("");
  473. String *tm;
  474. // Make a static variable;
  475. Printf (var_name, "_wrap_const_%s", Swig_name_mangle(Getattr(n,"sym:name")));
  476. // Build the name for scheme.
  477. Printv(proc_name, iname,NIL);
  478. Replaceall(proc_name, "_", "-");
  479. if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) {
  480. Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number,
  481. "Unsupported constant value.\n");
  482. return SWIG_NOWRAP;
  483. }
  484. // See if there's a typemap
  485. Printv(rvalue, value,NIL);
  486. if ((SwigType_type(type) == T_CHAR) && (is_a_pointer(type) == 1)) {
  487. temp = Copy(rvalue);
  488. Clear(rvalue);
  489. Printv(rvalue, "\"", temp, "\"",NIL);
  490. }
  491. if ((SwigType_type(type) == T_CHAR) && (is_a_pointer(type) == 0)) {
  492. Delete(temp);
  493. temp = Copy(rvalue);
  494. Clear(rvalue);
  495. Printv(rvalue, "'", temp, "'",NIL);
  496. }
  497. if ((tm = Swig_typemap_lookup_new("constant",n,name,0))) {
  498. Replaceall(tm,"$source",rvalue);
  499. Replaceall(tm,"$value",rvalue);
  500. Replaceall(tm,"$target",name);
  501. Printf (f_init, "%s\n", tm);
  502. } else {
  503. // Create variable and assign it a value
  504. Printf (f_header, "static %s = ", SwigType_lstr(type,var_name));
  505. if ((SwigType_type(type) == T_STRING)) {
  506. Printf (f_header, "\"%s\";\n", value);
  507. } else if (SwigType_type(type) == T_CHAR) {
  508. Printf (f_header, "\'%s\';\n", value);
  509. } else {
  510. Printf (f_header, "%s;\n", value);
  511. }
  512. // Now create a variable declaration
  513. {
  514. /* Hack alert: will cleanup later -- Dave */
  515. Node *n = NewHash();
  516. Setattr(n,"name",var_name);
  517. Setattr(n,"sym:name",iname);
  518. Setattr(n,"type", type);
  519. variableWrapper(n);
  520. Delete(n);
  521. }
  522. }
  523. Delete(proc_name);
  524. Delete(rvalue);
  525. Delete(temp);
  526. return SWIG_OK;
  527. }
  528. virtual int destructorHandler(Node *n) {
  529. exporting_destructor = true;
  530. Language::destructorHandler(n);
  531. exporting_destructor = false;
  532. return SWIG_OK;
  533. }
  534. /* ------------------------------------------------------------
  535. * classHandler()
  536. * ------------------------------------------------------------ */
  537. virtual int classHandler(Node *n) {
  538. String *mangled_classname = 0;
  539. String *real_classname = 0;
  540. String *scm_structname = NewString("");
  541. SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "classtype"));
  542. SwigType *t = NewStringf("p.%s", Getattr(n, "name"));
  543. swigtype_ptr = SwigType_manglestr(t);
  544. Delete(t);
  545. cls_swigtype = SwigType_manglestr(Getattr(n, "name"));
  546. fieldnames_tab = NewString("");
  547. convert_tab = NewString("");
  548. convert_proto_tab = NewString("");
  549. struct_name = Getattr(n,"sym:name");
  550. mangled_struct_name = Swig_name_mangle(Getattr(n,"sym:name"));
  551. Printv(scm_structname, struct_name, NIL);
  552. Replaceall(scm_structname, "_", "-");
  553. real_classname = Getattr(n,"name");
  554. mangled_classname = Swig_name_mangle(real_classname);
  555. Printv(fieldnames_tab, "static const char *_swig_struct_",
  556. cls_swigtype, "_field_names[] = { \n", NIL);
  557. Printv(convert_proto_tab, "static Scheme_Object *_swig_convert_struct_",
  558. cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ");\n", NIL);
  559. Printv(convert_tab, "static Scheme_Object *_swig_convert_struct_",
  560. cls_swigtype, "(", SwigType_str(ctype_ptr, "ptr"), ")\n {\n",
  561. NIL);
  562. Printv(convert_tab,
  563. tab4, "Scheme_Object *obj;\n",
  564. tab4, "Scheme_Object *fields[_swig_struct_", cls_swigtype,
  565. "_field_names_cnt];\n",
  566. tab4, "int i = 0;\n\n", NIL);
  567. /* Generate normal wrappers */
  568. Language::classHandler(n);
  569. Printv(convert_tab, tab4, "obj = scheme_make_struct_instance(",
  570. "_swig_struct_type_", cls_swigtype, ", i, fields);\n", NIL);
  571. Printv(convert_tab, tab4, "return obj;\n}\n\n", NIL);
  572. Printv(fieldnames_tab, "};\n", NIL);
  573. Printv(f_header, "static Scheme_Object *_swig_struct_type_",
  574. cls_swigtype, ";\n", NIL);
  575. Printv(f_header, fieldnames_tab, NIL);
  576. Printv(f_header, "#define _swig_struct_", cls_swigtype,
  577. "_field_names_cnt (sizeof(_swig_struct_", cls_swigtype,
  578. "_field_names)/sizeof(char*))\n", NIL);
  579. Printv(f_header, convert_proto_tab, NIL);
  580. Printv(f_wrappers, convert_tab, NIL);
  581. Printv(init_func_def, "_swig_struct_type_", cls_swigtype,
  582. " = SWIG_MzScheme_new_scheme_struct(menv, \"", scm_structname, "\", ",
  583. "_swig_struct_", cls_swigtype, "_field_names_cnt,",
  584. "(char**) _swig_struct_", cls_swigtype, "_field_names);\n",
  585. NIL);
  586. Delete(mangled_classname);
  587. Delete(swigtype_ptr);
  588. swigtype_ptr = 0;
  589. Delete(fieldnames_tab);
  590. Delete(convert_tab);
  591. Delete(ctype_ptr);
  592. Delete(convert_proto_tab);
  593. struct_name = 0;
  594. mangled_struct_name = 0;
  595. Delete(cls_swigtype);
  596. cls_swigtype = 0;
  597. return SWIG_OK;
  598. }
  599. /* ------------------------------------------------------------
  600. * membervariableHandler()
  601. * ------------------------------------------------------------ */
  602. virtual int membervariableHandler(Node *n) {
  603. Language::membervariableHandler(n);
  604. if (!is_smart_pointer()) {
  605. String *symname = Getattr(n, "sym:name");
  606. String *name = Getattr(n, "name");
  607. SwigType *type = Getattr(n, "type");
  608. String *swigtype = SwigType_manglestr(Getattr(n, "type"));
  609. String *tm = 0;
  610. String *access_mem = NewString("");
  611. SwigType *ctype_ptr = NewStringf("p.%s", Getattr(n, "type"));
  612. Printv(fieldnames_tab, tab4, "\"", symname, "\",\n", NIL);
  613. Printv(access_mem, "(ptr)->", name, NIL);
  614. if ((SwigType_type(type) == T_USER) && (!is_a_pointer(type))) {
  615. Printv(convert_tab, tab4, "fields[i++] = ", NIL);
  616. Printv(convert_tab, "_swig_convert_struct_", swigtype,
  617. "((", SwigType_str(ctype_ptr, ""), ")&((ptr)->",
  618. name, "));\n", NIL);
  619. } else if ((tm = Swig_typemap_lookup_new("varout",n,access_mem,0))) {
  620. Replaceall(tm,"$result","fields[i++]");
  621. Printv(convert_tab, tm, "\n", NIL);
  622. } else
  623. Swig_warning(WARN_TYPEMAP_VAR_UNDEF, input_file, line_number,
  624. "Unsupported member variable type %s (ignored).\n",
  625. SwigType_str(type,0));
  626. Delete(access_mem);
  627. }
  628. return SWIG_OK;
  629. }
  630. /* ------------------------------------------------------------
  631. * validIdentifer()
  632. * ------------------------------------------------------------ */
  633. virtual int validIdentifier(String *s) {
  634. char *c = Char(s);
  635. /* Check whether we have an R5RS identifier.*/
  636. /* <identifier> --> <initial> <subsequent>* | <peculiar identifier> */
  637. /* <initial> --> <letter> | <special initial> */
  638. if (!(isalpha(*c) || (*c == '!') || (*c == '$') || (*c == '%')
  639. || (*c == '&') || (*c == '*') || (*c == '/') || (*c == ':')
  640. || (*c == '<') || (*c == '=') || (*c == '>') || (*c == '?')
  641. || (*c == '^') || (*c == '_') || (*c == '~'))) {
  642. /* <peculiar identifier> --> + | - | ... */
  643. if ((strcmp(c, "+") == 0)
  644. || strcmp(c, "-") == 0
  645. || strcmp(c, "...") == 0) return 1;
  646. else return 0;
  647. }
  648. /* <subsequent> --> <initial> | <digit> | <special subsequent> */
  649. while (*c) {
  650. if (!(isalnum(*c) || (*c == '!') || (*c == '$') || (*c == '%')
  651. || (*c == '&') || (*c == '*') || (*c == '/') || (*c == ':')
  652. || (*c == '<') || (*c == '=') || (*c == '>') || (*c == '?')
  653. || (*c == '^') || (*c == '_') || (*c == '~') || (*c == '+')
  654. || (*c == '-') || (*c == '.') || (*c == '@'))) return 0;
  655. c++;
  656. }
  657. return 1;
  658. }
  659. String *runtimeCode() {
  660. String *s = Swig_include_sys("mzrun.swg");
  661. if (!s) {
  662. Printf(stderr, "*** Unable to open 'mzrun.swg'\n");
  663. s = NewString("");
  664. }
  665. return s;
  666. }
  667. String *defaultExternalRuntimeFilename() {
  668. return NewString("swigmzrun.h");
  669. }
  670. };
  671. /* -----------------------------------------------------------------------------
  672. * swig_mzscheme() - Instantiate module
  673. * ----------------------------------------------------------------------------- */
  674. static Language * new_swig_mzscheme() {
  675. return new MZSCHEME();
  676. }
  677. extern "C" Language * swig_mzscheme(void) {
  678. return new_swig_mzscheme();
  679. }