PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/Modules/perl5.cxx

#
C++ | 1769 lines | 1236 code | 252 blank | 281 comment | 343 complexity | 7f59f4904151c9d776700a11da856e8d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. * perl5.cxx
  10. *
  11. * Perl5 language module for SWIG.
  12. * ------------------------------------------------------------------------- */
  13. char cvsroot_perl5_cxx[] = "$Id: perl5.cxx 12830 2011-10-30 21:51:50Z wsfulton $";
  14. #include "swigmod.h"
  15. #include "cparse.h"
  16. static int treduce = SWIG_cparse_template_reduce(0);
  17. #include <ctype.h>
  18. static const char *usage = (char *) "\
  19. Perl5 Options (available with -perl5)\n\
  20. -compat - Compatibility mode\n\
  21. -const - Wrap constants as constants and not variables (implies -proxy)\n\
  22. -cppcast - Enable C++ casting operators\n\
  23. -nocppcast - Disable C++ casting operators, useful for generating bugs\n\
  24. -nopm - Do not generate the .pm file\n\
  25. -noproxy - Don't create proxy classes\n\
  26. -proxy - Create proxy classes\n\
  27. -static - Omit code related to dynamic loading\n\
  28. \n";
  29. static int compat = 0;
  30. static int no_pmfile = 0;
  31. static int export_all = 0;
  32. /*
  33. * pmfile
  34. * set by the -pm flag, overrides the name of the .pm file
  35. */
  36. static String *pmfile = 0;
  37. /*
  38. * module
  39. * set by the %module directive, e.g. "Xerces". It will determine
  40. * the name of the .pm file, and the dynamic library, and the name
  41. * used by any module wanting to %import the module.
  42. */
  43. static String *module = 0;
  44. /*
  45. * namespace_module
  46. * the fully namespace qualified name of the module. It will be used
  47. * to set the package namespace in the .pm file, as well as the name
  48. * of the initialization methods in the glue library. This will be
  49. * the same as module, above, unless the %module directive is given
  50. * the 'package' option, e.g. %module(package="Foo::Bar") "baz"
  51. */
  52. static String *namespace_module = 0;
  53. /*
  54. * cmodule
  55. * the namespace of the internal glue code, set to the value of
  56. * module with a 'c' appended
  57. */
  58. static String *cmodule = 0;
  59. /*
  60. * dest_package
  61. * an optional namespace to put all classes into. Specified by using
  62. * the %module(package="Foo::Bar") "baz" syntax
  63. */
  64. static String *dest_package = 0;
  65. static String *command_tab = 0;
  66. static String *constant_tab = 0;
  67. static String *variable_tab = 0;
  68. static File *f_begin = 0;
  69. static File *f_runtime = 0;
  70. static File *f_header = 0;
  71. static File *f_wrappers = 0;
  72. static File *f_init = 0;
  73. static File *f_pm = 0;
  74. static String *pm; /* Package initialization code */
  75. static String *magic; /* Magic variable wrappers */
  76. static int staticoption = 0;
  77. // controlling verbose output
  78. static int verbose = 0;
  79. /* The following variables are used to manage Perl5 classes */
  80. static int blessed = 1; /* Enable object oriented features */
  81. static int do_constants = 0; /* Constant wrapping */
  82. static List *classlist = 0; /* List of classes */
  83. static int have_constructor = 0;
  84. static int have_destructor = 0;
  85. static int have_data_members = 0;
  86. static String *class_name = 0; /* Name of the class (what Perl thinks it is) */
  87. static String *real_classname = 0; /* Real name of C/C++ class */
  88. static String *fullclassname = 0;
  89. static String *pcode = 0; /* Perl code associated with each class */
  90. /* static String *blessedmembers = 0; *//* Member data associated with each class */
  91. static int member_func = 0; /* Set to 1 when wrapping a member function */
  92. static String *func_stubs = 0; /* Function stubs */
  93. static String *const_stubs = 0; /* Constant stubs */
  94. static int num_consts = 0; /* Number of constants */
  95. static String *var_stubs = 0; /* Variable stubs */
  96. static String *exported = 0; /* Exported symbols */
  97. static String *pragma_include = 0;
  98. static String *additional_perl_code = 0; /* Additional Perl code from %perlcode %{ ... %} */
  99. static Hash *operators = 0;
  100. static int have_operators = 0;
  101. class PERL5:public Language {
  102. public:
  103. PERL5():Language () {
  104. Clear(argc_template_string);
  105. Printv(argc_template_string, "items", NIL);
  106. Clear(argv_template_string);
  107. Printv(argv_template_string, "ST(%d)", NIL);
  108. }
  109. /* Test to see if a type corresponds to something wrapped with a shadow class */
  110. Node *is_shadow(SwigType *t) {
  111. Node *n;
  112. n = classLookup(t);
  113. /* Printf(stdout,"'%s' --> '%x'\n", t, n); */
  114. if (n) {
  115. if (!Getattr(n, "perl5:proxy")) {
  116. setclassname(n);
  117. }
  118. return Getattr(n, "perl5:proxy");
  119. }
  120. return 0;
  121. }
  122. /* ------------------------------------------------------------
  123. * main()
  124. * ------------------------------------------------------------ */
  125. virtual void main(int argc, char *argv[]) {
  126. int i = 1;
  127. int cppcast = 1;
  128. SWIG_library_directory("perl5");
  129. for (i = 1; i < argc; i++) {
  130. if (argv[i]) {
  131. if (strcmp(argv[i], "-package") == 0) {
  132. Printv(stderr,
  133. "*** -package is no longer supported\n*** use the directive '%module A::B::C' in your interface file instead\n*** see the Perl section in the manual for details.\n", NIL);
  134. SWIG_exit(EXIT_FAILURE);
  135. } else if (strcmp(argv[i], "-interface") == 0) {
  136. Printv(stderr,
  137. "*** -interface is no longer supported\n*** use the directive '%module A::B::C' in your interface file instead\n*** see the Perl section in the manual for details.\n", NIL);
  138. SWIG_exit(EXIT_FAILURE);
  139. } else if (strcmp(argv[i], "-exportall") == 0) {
  140. export_all = 1;
  141. Swig_mark_arg(i);
  142. } else if (strcmp(argv[i], "-static") == 0) {
  143. staticoption = 1;
  144. Swig_mark_arg(i);
  145. } else if ((strcmp(argv[i], "-shadow") == 0) || ((strcmp(argv[i], "-proxy") == 0))) {
  146. blessed = 1;
  147. Swig_mark_arg(i);
  148. } else if ((strcmp(argv[i], "-noproxy") == 0)) {
  149. blessed = 0;
  150. Swig_mark_arg(i);
  151. } else if (strcmp(argv[i], "-const") == 0) {
  152. do_constants = 1;
  153. blessed = 1;
  154. Swig_mark_arg(i);
  155. } else if (strcmp(argv[i], "-nopm") == 0) {
  156. no_pmfile = 1;
  157. Swig_mark_arg(i);
  158. } else if (strcmp(argv[i], "-pm") == 0) {
  159. Swig_mark_arg(i);
  160. i++;
  161. pmfile = NewString(argv[i]);
  162. Swig_mark_arg(i);
  163. } else if (strcmp(argv[i],"-v") == 0) {
  164. Swig_mark_arg(i);
  165. verbose++;
  166. } else if (strcmp(argv[i], "-cppcast") == 0) {
  167. cppcast = 1;
  168. Swig_mark_arg(i);
  169. } else if (strcmp(argv[i], "-nocppcast") == 0) {
  170. cppcast = 0;
  171. Swig_mark_arg(i);
  172. } else if (strcmp(argv[i], "-compat") == 0) {
  173. compat = 1;
  174. Swig_mark_arg(i);
  175. } else if (strcmp(argv[i], "-help") == 0) {
  176. fputs(usage, stdout);
  177. }
  178. }
  179. }
  180. if (cppcast) {
  181. Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0);
  182. }
  183. Preprocessor_define("SWIGPERL 1", 0);
  184. // SWIGPERL5 is deprecated, and no longer documented.
  185. Preprocessor_define("SWIGPERL5 1", 0);
  186. SWIG_typemap_lang("perl5");
  187. SWIG_config_file("perl5.swg");
  188. allow_overloading();
  189. }
  190. /* ------------------------------------------------------------
  191. * top()
  192. * ------------------------------------------------------------ */
  193. virtual int top(Node *n) {
  194. /* Initialize all of the output files */
  195. String *outfile = Getattr(n, "outfile");
  196. f_begin = NewFile(outfile, "w", SWIG_output_files());
  197. if (!f_begin) {
  198. FileErrorDisplay(outfile);
  199. SWIG_exit(EXIT_FAILURE);
  200. }
  201. f_runtime = NewString("");
  202. f_init = NewString("");
  203. f_header = NewString("");
  204. f_wrappers = NewString("");
  205. /* Register file targets with the SWIG file handler */
  206. Swig_register_filebyname("header", f_header);
  207. Swig_register_filebyname("wrapper", f_wrappers);
  208. Swig_register_filebyname("begin", f_begin);
  209. Swig_register_filebyname("runtime", f_runtime);
  210. Swig_register_filebyname("init", f_init);
  211. classlist = NewList();
  212. pm = NewString("");
  213. func_stubs = NewString("");
  214. var_stubs = NewString("");
  215. const_stubs = NewString("");
  216. exported = NewString("");
  217. magic = NewString("");
  218. pragma_include = NewString("");
  219. additional_perl_code = NewString("");
  220. command_tab = NewString("static swig_command_info swig_commands[] = {\n");
  221. constant_tab = NewString("static swig_constant_info swig_constants[] = {\n");
  222. variable_tab = NewString("static swig_variable_info swig_variables[] = {\n");
  223. Swig_banner(f_begin);
  224. Printf(f_runtime, "\n");
  225. Printf(f_runtime, "#define SWIGPERL\n");
  226. Printf(f_runtime, "#define SWIG_CASTRANK_MODE\n");
  227. Printf(f_runtime, "\n");
  228. // Is the imported module in another package? (IOW, does it use the
  229. // %module(package="name") option and it's different than the package
  230. // of this module.)
  231. Node *mod = Getattr(n, "module");
  232. Node *options = Getattr(mod, "options");
  233. module = Copy(Getattr(n,"name"));
  234. if (verbose > 0) {
  235. fprintf(stdout, "top: using module: %s\n", Char(module));
  236. }
  237. dest_package = options ? Getattr(options, "package") : 0;
  238. if (dest_package) {
  239. namespace_module = Copy(dest_package);
  240. if (verbose > 0) {
  241. fprintf(stdout, "top: Found package: %s\n",Char(dest_package));
  242. }
  243. } else {
  244. namespace_module = Copy(module);
  245. if (verbose > 0) {
  246. fprintf(stdout, "top: No package found\n");
  247. }
  248. }
  249. String *underscore_module = Copy(module);
  250. Replaceall(underscore_module,":","_");
  251. if (verbose > 0) {
  252. fprintf(stdout, "top: using namespace_module: %s\n", Char(namespace_module));
  253. }
  254. /* If we're in blessed mode, change the package name to "packagec" */
  255. if (blessed) {
  256. cmodule = NewStringf("%sc",namespace_module);
  257. } else {
  258. cmodule = NewString(namespace_module);
  259. }
  260. /* Create a .pm file
  261. * Need to strip off any prefixes that might be found in
  262. * the module name */
  263. if (no_pmfile) {
  264. f_pm = NewString(0);
  265. } else {
  266. if (!pmfile) {
  267. char *m = Char(module) + Len(module);
  268. while (m != Char(module)) {
  269. if (*m == ':') {
  270. m++;
  271. break;
  272. }
  273. m--;
  274. }
  275. pmfile = NewStringf("%s.pm", m);
  276. }
  277. String *filen = NewStringf("%s%s", SWIG_output_directory(), pmfile);
  278. if ((f_pm = NewFile(filen, "w", SWIG_output_files())) == 0) {
  279. FileErrorDisplay(filen);
  280. SWIG_exit(EXIT_FAILURE);
  281. }
  282. Delete(filen);
  283. filen = NULL;
  284. Swig_register_filebyname("pm", f_pm);
  285. Swig_register_filebyname("perl", f_pm);
  286. }
  287. {
  288. String *boot_name = NewStringf("boot_%s", underscore_module);
  289. Printf(f_header,"#define SWIG_init %s\n\n", boot_name);
  290. Printf(f_header,"#define SWIG_name \"%s::%s\"\n", cmodule, boot_name);
  291. Printf(f_header,"#define SWIG_prefix \"%s::\"\n", cmodule);
  292. Delete(boot_name);
  293. }
  294. Swig_banner_target_lang(f_pm, "#");
  295. Printf(f_pm, "\n");
  296. Printf(f_pm, "package %s;\n", module);
  297. /*
  298. * If the package option has been given we are placing our
  299. * symbols into some other packages namespace, so we do not
  300. * mess with @ISA or require for that package
  301. */
  302. if (dest_package) {
  303. Printf(f_pm,"use base qw(DynaLoader);\n");
  304. } else {
  305. Printf(f_pm,"use base qw(Exporter);\n");
  306. if (!staticoption) {
  307. Printf(f_pm,"use base qw(DynaLoader);\n");
  308. }
  309. }
  310. /* Start creating magic code */
  311. Printv(magic,
  312. "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n",
  313. "#ifdef PERL_OBJECT\n",
  314. "#define MAGIC_CLASS _wrap_", underscore_module, "_var::\n",
  315. "class _wrap_", underscore_module, "_var : public CPerlObj {\n",
  316. "public:\n",
  317. "#else\n",
  318. "#define MAGIC_CLASS\n",
  319. "#endif\n",
  320. "SWIGCLASS_STATIC int swig_magic_readonly(pTHX_ SV *SWIGUNUSEDPARM(sv), MAGIC *SWIGUNUSEDPARM(mg)) {\n",
  321. tab4, "MAGIC_PPERL\n", tab4, "croak(\"Value is read-only.\");\n", tab4, "return 0;\n", "}\n", NIL);
  322. Printf(f_wrappers, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
  323. /* emit wrappers */
  324. Language::top(n);
  325. String *base = NewString("");
  326. /* Dump out variable wrappers */
  327. Printv(magic, "\n\n#ifdef PERL_OBJECT\n", "};\n", "#endif\n", NIL);
  328. Printv(magic, "\n#ifdef __cplusplus\n}\n#endif\n", NIL);
  329. Printf(f_header, "%s\n", magic);
  330. String *type_table = NewString("");
  331. /* Patch the type table to reflect the names used by shadow classes */
  332. if (blessed) {
  333. Iterator cls;
  334. for (cls = First(classlist); cls.item; cls = Next(cls)) {
  335. String *pname = Getattr(cls.item, "perl5:proxy");
  336. if (pname) {
  337. SwigType *type = Getattr(cls.item, "classtypeobj");
  338. if (!type)
  339. continue; /* If unnamed class, no type will be found */
  340. type = Copy(type);
  341. SwigType_add_pointer(type);
  342. String *mangled = SwigType_manglestr(type);
  343. SwigType_remember_mangleddata(mangled, NewStringf("\"%s\"", pname));
  344. Delete(type);
  345. Delete(mangled);
  346. }
  347. }
  348. }
  349. SwigType_emit_type_table(f_runtime, type_table);
  350. Printf(f_wrappers, "%s", type_table);
  351. Delete(type_table);
  352. Printf(constant_tab, "{0,0,0,0,0,0}\n};\n");
  353. Printv(f_wrappers, constant_tab, NIL);
  354. Printf(f_wrappers, "#ifdef __cplusplus\n}\n#endif\n");
  355. Printf(f_init, "\t ST(0) = &PL_sv_yes;\n");
  356. Printf(f_init, "\t XSRETURN(1);\n");
  357. Printf(f_init, "}\n");
  358. /* Finish off tables */
  359. Printf(variable_tab, "{0,0,0,0}\n};\n");
  360. Printv(f_wrappers, variable_tab, NIL);
  361. Printf(command_tab, "{0,0}\n};\n");
  362. Printv(f_wrappers, command_tab, NIL);
  363. Printf(f_pm, "package %s;\n", cmodule);
  364. if (!staticoption) {
  365. Printf(f_pm,"bootstrap %s;\n", module);
  366. } else {
  367. Printf(f_pm,"package %s;\n", cmodule);
  368. Printf(f_pm,"boot_%s();\n", underscore_module);
  369. }
  370. Printf(f_pm, "package %s;\n", module);
  371. /*
  372. * If the package option has been given we are placing our
  373. * symbols into some other packages namespace, so we do not
  374. * mess with @EXPORT
  375. */
  376. if (!dest_package) {
  377. Printf(f_pm,"@EXPORT = qw(%s);\n", exported);
  378. }
  379. Printf(f_pm, "%s", pragma_include);
  380. if (blessed) {
  381. /*
  382. * These methods will be duplicated if package
  383. * has been specified, so we do not output them
  384. */
  385. if (!dest_package) {
  386. Printv(base, "\n# ---------- BASE METHODS -------------\n\n", "package ", namespace_module, ";\n\n", NIL);
  387. /* Write out the TIE method */
  388. Printv(base, "sub TIEHASH {\n", tab4, "my ($classname,$obj) = @_;\n", tab4, "return bless $obj, $classname;\n", "}\n\n", NIL);
  389. /* Output a CLEAR method. This is just a place-holder, but by providing it we
  390. * can make declarations such as
  391. * %$u = ( x => 2, y=>3, z =>4 );
  392. *
  393. * Where x,y,z are the members of some C/C++ object. */
  394. Printf(base, "sub CLEAR { }\n\n");
  395. /* Output default firstkey/nextkey methods */
  396. Printf(base, "sub FIRSTKEY { }\n\n");
  397. Printf(base, "sub NEXTKEY { }\n\n");
  398. /* Output a FETCH method. This is actually common to all classes */
  399. Printv(base,
  400. "sub FETCH {\n",
  401. tab4, "my ($self,$field) = @_;\n", tab4, "my $member_func = \"swig_${field}_get\";\n", tab4, "$self->$member_func();\n", "}\n\n", NIL);
  402. /* Output a STORE method. This is also common to all classes (might move to base class) */
  403. Printv(base,
  404. "sub STORE {\n",
  405. tab4, "my ($self,$field,$newval) = @_;\n",
  406. tab4, "my $member_func = \"swig_${field}_set\";\n", tab4, "$self->$member_func($newval);\n", "}\n\n", NIL);
  407. /* Output a 'this' method */
  408. Printv(base, "sub this {\n", tab4, "my $ptr = shift;\n", tab4, "return tied(%$ptr);\n", "}\n\n", NIL);
  409. Printf(f_pm, "%s", base);
  410. }
  411. /* Emit function stubs for stand-alone functions */
  412. Printf(f_pm, "\n# ------- FUNCTION WRAPPERS --------\n\n");
  413. Printf(f_pm, "package %s;\n\n", namespace_module);
  414. Printf(f_pm, "%s", func_stubs);
  415. /* Emit package code for different classes */
  416. Printf(f_pm, "%s", pm);
  417. if (num_consts > 0) {
  418. /* Emit constant stubs */
  419. Printf(f_pm, "\n# ------- CONSTANT STUBS -------\n\n");
  420. Printf(f_pm, "package %s;\n\n", namespace_module);
  421. Printf(f_pm, "%s", const_stubs);
  422. }
  423. /* Emit variable stubs */
  424. Printf(f_pm, "\n# ------- VARIABLE STUBS --------\n\n");
  425. Printf(f_pm, "package %s;\n\n", namespace_module);
  426. Printf(f_pm, "%s", var_stubs);
  427. }
  428. /* Add additional Perl code at the end */
  429. Printf(f_pm, "%s", additional_perl_code);
  430. Printf(f_pm, "1;\n");
  431. Close(f_pm);
  432. Delete(f_pm);
  433. Delete(base);
  434. Delete(dest_package);
  435. Delete(underscore_module);
  436. /* Close all of the files */
  437. Dump(f_runtime, f_begin);
  438. Dump(f_header, f_begin);
  439. Dump(f_wrappers, f_begin);
  440. Wrapper_pretty_print(f_init, f_begin);
  441. Delete(f_header);
  442. Delete(f_wrappers);
  443. Delete(f_init);
  444. Close(f_begin);
  445. Delete(f_runtime);
  446. Delete(f_begin);
  447. return SWIG_OK;
  448. }
  449. /* ------------------------------------------------------------
  450. * importDirective(Node *n)
  451. * ------------------------------------------------------------ */
  452. virtual int importDirective(Node *n) {
  453. if (blessed) {
  454. String *modname = Getattr(n, "module");
  455. if (modname) {
  456. Printf(f_pm, "require %s;\n", modname);
  457. }
  458. }
  459. return Language::importDirective(n);
  460. }
  461. /* ------------------------------------------------------------
  462. * functionWrapper()
  463. * ------------------------------------------------------------ */
  464. virtual int functionWrapper(Node *n) {
  465. String *name = Getattr(n, "name");
  466. String *iname = Getattr(n, "sym:name");
  467. SwigType *d = Getattr(n, "type");
  468. ParmList *l = Getattr(n, "parms");
  469. String *overname = 0;
  470. Parm *p;
  471. int i;
  472. Wrapper *f;
  473. char source[256], temp[256];
  474. String *tm;
  475. String *cleanup, *outarg;
  476. int num_saved = 0;
  477. int num_arguments, num_required;
  478. int varargs = 0;
  479. if (Getattr(n, "sym:overloaded")) {
  480. overname = Getattr(n, "sym:overname");
  481. } else {
  482. if (!addSymbol(iname, n))
  483. return SWIG_ERROR;
  484. }
  485. f = NewWrapper();
  486. cleanup = NewString("");
  487. outarg = NewString("");
  488. String *wname = Swig_name_wrapper(iname);
  489. if (overname) {
  490. Append(wname, overname);
  491. }
  492. Setattr(n, "wrap:name", wname);
  493. Printv(f->def, "XS(", wname, ") {\n", "{\n", /* scope to destroy C++ objects before croaking */
  494. NIL);
  495. emit_parameter_variables(l, f);
  496. emit_attach_parmmaps(l, f);
  497. Setattr(n, "wrap:parms", l);
  498. num_arguments = emit_num_arguments(l);
  499. num_required = emit_num_required(l);
  500. varargs = emit_isvarargs(l);
  501. Wrapper_add_local(f, "argvi", "int argvi = 0");
  502. /* Check the number of arguments */
  503. if (!varargs) {
  504. Printf(f->code, " if ((items < %d) || (items > %d)) {\n", num_required, num_arguments);
  505. } else {
  506. Printf(f->code, " if (items < %d) {\n", num_required);
  507. }
  508. Printf(f->code, " SWIG_croak(\"Usage: %s\");\n", usage_func(Char(iname), d, l));
  509. Printf(f->code, "}\n");
  510. /* Write code to extract parameters. */
  511. i = 0;
  512. for (i = 0, p = l; i < num_arguments; i++) {
  513. /* Skip ignored arguments */
  514. while (checkAttribute(p, "tmap:in:numinputs", "0")) {
  515. p = Getattr(p, "tmap:in:next");
  516. }
  517. SwigType *pt = Getattr(p, "type");
  518. /* Produce string representation of source and target arguments */
  519. sprintf(source, "ST(%d)", i);
  520. String *target = Getattr(p, "lname");
  521. if (i >= num_required) {
  522. Printf(f->code, " if (items > %d) {\n", i);
  523. }
  524. if ((tm = Getattr(p, "tmap:in"))) {
  525. Replaceall(tm, "$target", target);
  526. Replaceall(tm, "$source", source);
  527. Replaceall(tm, "$input", source);
  528. Setattr(p, "emit:input", source); /* Save input location */
  529. if (Getattr(p, "wrap:disown") || (Getattr(p, "tmap:in:disown"))) {
  530. Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN");
  531. } else {
  532. Replaceall(tm, "$disown", "0");
  533. }
  534. Printf(f->code, "%s\n", tm);
  535. p = Getattr(p, "tmap:in:next");
  536. } else {
  537. Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(pt, 0));
  538. p = nextSibling(p);
  539. }
  540. if (i >= num_required) {
  541. Printf(f->code, " }\n");
  542. }
  543. }
  544. if (varargs) {
  545. if (p && (tm = Getattr(p, "tmap:in"))) {
  546. sprintf(source, "ST(%d)", i);
  547. Replaceall(tm, "$input", source);
  548. Setattr(p, "emit:input", source);
  549. Printf(f->code, "if (items >= %d) {\n", i);
  550. Printv(f->code, tm, "\n", NIL);
  551. Printf(f->code, "}\n");
  552. }
  553. }
  554. /* Insert constraint checking code */
  555. for (p = l; p;) {
  556. if ((tm = Getattr(p, "tmap:check"))) {
  557. Replaceall(tm, "$target", Getattr(p, "lname"));
  558. Printv(f->code, tm, "\n", NIL);
  559. p = Getattr(p, "tmap:check:next");
  560. } else {
  561. p = nextSibling(p);
  562. }
  563. }
  564. /* Insert cleanup code */
  565. for (i = 0, p = l; p; i++) {
  566. if ((tm = Getattr(p, "tmap:freearg"))) {
  567. Replaceall(tm, "$source", Getattr(p, "lname"));
  568. Replaceall(tm, "$arg", Getattr(p, "emit:input"));
  569. Replaceall(tm, "$input", Getattr(p, "emit:input"));
  570. Printv(cleanup, tm, "\n", NIL);
  571. p = Getattr(p, "tmap:freearg:next");
  572. } else {
  573. p = nextSibling(p);
  574. }
  575. }
  576. /* Insert argument output code */
  577. num_saved = 0;
  578. for (i = 0, p = l; p; i++) {
  579. if ((tm = Getattr(p, "tmap:argout"))) {
  580. SwigType *t = Getattr(p, "type");
  581. Replaceall(tm, "$source", Getattr(p, "lname"));
  582. Replaceall(tm, "$target", "ST(argvi)");
  583. Replaceall(tm, "$result", "ST(argvi)");
  584. if (is_shadow(t)) {
  585. Replaceall(tm, "$shadow", "SWIG_SHADOW");
  586. } else {
  587. Replaceall(tm, "$shadow", "0");
  588. }
  589. String *in = Getattr(p, "emit:input");
  590. if (in) {
  591. sprintf(temp, "_saved[%d]", num_saved);
  592. Replaceall(tm, "$arg", temp);
  593. Replaceall(tm, "$input", temp);
  594. Printf(f->code, "_saved[%d] = %s;\n", num_saved, in);
  595. num_saved++;
  596. }
  597. Printv(outarg, tm, "\n", NIL);
  598. p = Getattr(p, "tmap:argout:next");
  599. } else {
  600. p = nextSibling(p);
  601. }
  602. }
  603. /* If there were any saved arguments, emit a local variable for them */
  604. if (num_saved) {
  605. sprintf(temp, "_saved[%d]", num_saved);
  606. Wrapper_add_localv(f, "_saved", "SV *", temp, NIL);
  607. }
  608. /* Now write code to make the function call */
  609. Swig_director_emit_dynamic_cast(n, f);
  610. String *actioncode = emit_action(n);
  611. if ((tm = Swig_typemap_lookup_out("out", n, Swig_cresult_name(), f, actioncode))) {
  612. SwigType *t = Getattr(n, "type");
  613. Replaceall(tm, "$source", Swig_cresult_name());
  614. Replaceall(tm, "$target", "ST(argvi)");
  615. Replaceall(tm, "$result", "ST(argvi)");
  616. if (is_shadow(t)) {
  617. Replaceall(tm, "$shadow", "SWIG_SHADOW");
  618. } else {
  619. Replaceall(tm, "$shadow", "0");
  620. }
  621. if (GetFlag(n, "feature:new")) {
  622. Replaceall(tm, "$owner", "SWIG_OWNER");
  623. } else {
  624. Replaceall(tm, "$owner", "0");
  625. }
  626. Printf(f->code, "%s\n", tm);
  627. } else {
  628. Swig_warning(WARN_TYPEMAP_OUT_UNDEF, input_file, line_number, "Unable to use return type %s in function %s.\n", SwigType_str(d, 0), name);
  629. }
  630. emit_return_variable(n, d, f);
  631. /* If there were any output args, take care of them. */
  632. Printv(f->code, outarg, NIL);
  633. /* If there was any cleanup, do that. */
  634. Printv(f->code, cleanup, NIL);
  635. if (GetFlag(n, "feature:new")) {
  636. if ((tm = Swig_typemap_lookup("newfree", n, Swig_cresult_name(), 0))) {
  637. Replaceall(tm, "$source", Swig_cresult_name());
  638. Printf(f->code, "%s\n", tm);
  639. }
  640. }
  641. if ((tm = Swig_typemap_lookup("ret", n, Swig_cresult_name(), 0))) {
  642. Replaceall(tm, "$source", Swig_cresult_name());
  643. Printf(f->code, "%s\n", tm);
  644. }
  645. Printv(f->code, "XSRETURN(argvi);\n", "fail:\n", cleanup, "SWIG_croak_null();\n" "}\n" "}\n", NIL);
  646. /* Add the dXSARGS last */
  647. Wrapper_add_local(f, "dXSARGS", "dXSARGS");
  648. /* Substitute the cleanup code */
  649. Replaceall(f->code, "$cleanup", cleanup);
  650. Replaceall(f->code, "$symname", iname);
  651. /* Dump the wrapper function */
  652. Wrapper_print(f, f_wrappers);
  653. /* Now register the function */
  654. if (!Getattr(n, "sym:overloaded")) {
  655. Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, iname, wname);
  656. } else if (!Getattr(n, "sym:nextSibling")) {
  657. /* Generate overloaded dispatch function */
  658. int maxargs;
  659. String *dispatch = Swig_overload_dispatch_cast(n, "PUSHMARK(MARK); SWIG_CALLXS(%s); return;", &maxargs);
  660. /* Generate a dispatch wrapper for all overloaded functions */
  661. Wrapper *df = NewWrapper();
  662. String *dname = Swig_name_wrapper(iname);
  663. Printv(df->def, "XS(", dname, ") {\n", NIL);
  664. Wrapper_add_local(df, "dXSARGS", "dXSARGS");
  665. Printv(df->code, dispatch, "\n", NIL);
  666. Printf(df->code, "croak(\"No matching function for overloaded '%s'\");\n", iname);
  667. Printf(df->code, "XSRETURN(0);\n");
  668. Printv(df->code, "}\n", NIL);
  669. Wrapper_print(df, f_wrappers);
  670. Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, iname, dname);
  671. DelWrapper(df);
  672. Delete(dispatch);
  673. Delete(dname);
  674. }
  675. if (!Getattr(n, "sym:nextSibling")) {
  676. if (export_all) {
  677. Printf(exported, "%s ", iname);
  678. }
  679. /* --------------------------------------------------------------------
  680. * Create a stub for this function, provided it's not a member function
  681. * -------------------------------------------------------------------- */
  682. if ((blessed) && (!member_func)) {
  683. Printv(func_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL);
  684. }
  685. }
  686. Delete(cleanup);
  687. Delete(outarg);
  688. DelWrapper(f);
  689. return SWIG_OK;
  690. }
  691. /* ------------------------------------------------------------
  692. * variableWrapper()
  693. * ------------------------------------------------------------ */
  694. virtual int variableWrapper(Node *n) {
  695. String *name = Getattr(n, "name");
  696. String *iname = Getattr(n, "sym:name");
  697. SwigType *t = Getattr(n, "type");
  698. Wrapper *getf, *setf;
  699. String *tm;
  700. String *getname = Swig_name_get(NSPACE_TODO, iname);
  701. String *setname = Swig_name_set(NSPACE_TODO, iname);
  702. String *get_name = Swig_name_wrapper(getname);
  703. String *set_name = Swig_name_wrapper(setname);
  704. if (!addSymbol(iname, n))
  705. return SWIG_ERROR;
  706. getf = NewWrapper();
  707. setf = NewWrapper();
  708. /* Create a Perl function for setting the variable value */
  709. if (!GetFlag(n, "feature:immutable")) {
  710. Setattr(n, "wrap:name", set_name);
  711. Printf(setf->def, "SWIGCLASS_STATIC int %s(pTHX_ SV* sv, MAGIC * SWIGUNUSEDPARM(mg)) {\n", set_name);
  712. Printv(setf->code, tab4, "MAGIC_PPERL\n", NIL);
  713. /* Check for a few typemaps */
  714. tm = Swig_typemap_lookup("varin", n, name, 0);
  715. if (tm) {
  716. Replaceall(tm, "$source", "sv");
  717. Replaceall(tm, "$target", name);
  718. Replaceall(tm, "$input", "sv");
  719. /* Printf(setf->code,"%s\n", tm); */
  720. emit_action_code(n, setf->code, tm);
  721. } else {
  722. Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0));
  723. return SWIG_NOWRAP;
  724. }
  725. Printf(setf->code, "fail:\n");
  726. Printf(setf->code, " return 1;\n}\n");
  727. Replaceall(setf->code, "$symname", iname);
  728. Wrapper_print(setf, magic);
  729. }
  730. /* Now write a function to evaluate the variable */
  731. Setattr(n, "wrap:name", get_name);
  732. int addfail = 0;
  733. Printf(getf->def, "SWIGCLASS_STATIC int %s(pTHX_ SV *sv, MAGIC *SWIGUNUSEDPARM(mg)) {\n", get_name);
  734. Printv(getf->code, tab4, "MAGIC_PPERL\n", NIL);
  735. if ((tm = Swig_typemap_lookup("varout", n, name, 0))) {
  736. Replaceall(tm, "$target", "sv");
  737. Replaceall(tm, "$result", "sv");
  738. Replaceall(tm, "$source", name);
  739. if (is_shadow(t)) {
  740. Replaceall(tm, "$shadow", "SWIG_SHADOW");
  741. } else {
  742. Replaceall(tm, "$shadow", "0");
  743. }
  744. /* Printf(getf->code,"%s\n", tm); */
  745. addfail = emit_action_code(n, getf->code, tm);
  746. } else {
  747. Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
  748. DelWrapper(setf);
  749. DelWrapper(getf);
  750. return SWIG_NOWRAP;
  751. }
  752. Printf(getf->code, " return 1;\n");
  753. if (addfail) {
  754. Append(getf->code, "fail:\n");
  755. Append(getf->code, " return 0;\n");
  756. }
  757. Append(getf->code, "}\n");
  758. Replaceall(getf->code, "$symname", iname);
  759. Wrapper_print(getf, magic);
  760. String *tt = Getattr(n, "tmap:varout:type");
  761. if (tt) {
  762. String *tm = NewStringf("&SWIGTYPE%s", SwigType_manglestr(t));
  763. if (Replaceall(tt, "$1_descriptor", tm)) {
  764. SwigType_remember(t);
  765. }
  766. Delete(tm);
  767. SwigType *st = Copy(t);
  768. SwigType_add_pointer(st);
  769. tm = NewStringf("&SWIGTYPE%s", SwigType_manglestr(st));
  770. if (Replaceall(tt, "$&1_descriptor", tm)) {
  771. SwigType_remember(st);
  772. }
  773. Delete(tm);
  774. Delete(st);
  775. } else {
  776. tt = (String *) "0";
  777. }
  778. /* Now add symbol to the PERL interpreter */
  779. if (GetFlag(n, "feature:immutable")) {
  780. Printv(variable_tab, tab4, "{ \"", cmodule, "::", iname, "\", MAGIC_CLASS swig_magic_readonly, MAGIC_CLASS ", get_name, ",", tt, " },\n", NIL);
  781. } else {
  782. Printv(variable_tab, tab4, "{ \"", cmodule, "::", iname, "\", MAGIC_CLASS ", set_name, ", MAGIC_CLASS ", get_name, ",", tt, " },\n", NIL);
  783. }
  784. /* If we're blessed, try to figure out what to do with the variable
  785. 1. If it's a Perl object of some sort, create a tied-hash
  786. around it.
  787. 2. Otherwise, just hack Perl's symbol table */
  788. if (blessed) {
  789. if (is_shadow(t)) {
  790. Printv(var_stubs,
  791. "\nmy %__", iname, "_hash;\n",
  792. "tie %__", iname, "_hash,\"", is_shadow(t), "\", $",
  793. cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(t), ";\n", NIL);
  794. } else {
  795. Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL);
  796. }
  797. }
  798. if (export_all)
  799. Printf(exported, "$%s ", iname);
  800. DelWrapper(setf);
  801. DelWrapper(getf);
  802. Delete(getname);
  803. Delete(setname);
  804. Delete(set_name);
  805. Delete(get_name);
  806. return SWIG_OK;
  807. }
  808. /* ------------------------------------------------------------
  809. * constantWrapper()
  810. * ------------------------------------------------------------ */
  811. virtual int constantWrapper(Node *n) {
  812. String *name = Getattr(n, "name");
  813. String *iname = Getattr(n, "sym:name");
  814. SwigType *type = Getattr(n, "type");
  815. String *rawval = Getattr(n, "rawval");
  816. String *value = rawval ? rawval : Getattr(n, "value");
  817. String *tm;
  818. if (!addSymbol(iname, n))
  819. return SWIG_ERROR;
  820. /* Special hook for member pointer */
  821. if (SwigType_type(type) == T_MPOINTER) {
  822. String *wname = Swig_name_wrapper(iname);
  823. Printf(f_wrappers, "static %s = %s;\n", SwigType_str(type, wname), value);
  824. value = Char(wname);
  825. }
  826. if ((tm = Swig_typemap_lookup("consttab", n, name, 0))) {
  827. Replaceall(tm, "$source", value);
  828. Replaceall(tm, "$target", name);
  829. Replaceall(tm, "$value", value);
  830. if (is_shadow(type)) {
  831. Replaceall(tm, "$shadow", "SWIG_SHADOW");
  832. } else {
  833. Replaceall(tm, "$shadow", "0");
  834. }
  835. Printf(constant_tab, "%s,\n", tm);
  836. } else if ((tm = Swig_typemap_lookup("constcode", n, name, 0))) {
  837. Replaceall(tm, "$source", value);
  838. Replaceall(tm, "$target", name);
  839. Replaceall(tm, "$value", value);
  840. if (is_shadow(type)) {
  841. Replaceall(tm, "$shadow", "SWIG_SHADOW");
  842. } else {
  843. Replaceall(tm, "$shadow", "0");
  844. }
  845. Printf(f_init, "%s\n", tm);
  846. } else {
  847. Swig_warning(WARN_TYPEMAP_CONST_UNDEF, input_file, line_number, "Unsupported constant value.\n");
  848. return SWIG_NOWRAP;
  849. }
  850. if (blessed) {
  851. if (is_shadow(type)) {
  852. Printv(var_stubs,
  853. "\nmy %__", iname, "_hash;\n",
  854. "tie %__", iname, "_hash,\"", is_shadow(type), "\", $",
  855. cmodule, "::", iname, ";\n", "$", iname, "= \\%__", iname, "_hash;\n", "bless $", iname, ", ", is_shadow(type), ";\n", NIL);
  856. } else if (do_constants) {
  857. Printv(const_stubs, "sub ", name, " () { $", cmodule, "::", name, " }\n", NIL);
  858. num_consts++;
  859. } else {
  860. Printv(var_stubs, "*", iname, " = *", cmodule, "::", iname, ";\n", NIL);
  861. }
  862. }
  863. if (export_all) {
  864. if (do_constants && !is_shadow(type)) {
  865. Printf(exported, "%s ", name);
  866. } else {
  867. Printf(exported, "$%s ", iname);
  868. }
  869. }
  870. return SWIG_OK;
  871. }
  872. /* ------------------------------------------------------------
  873. * usage_func()
  874. * ------------------------------------------------------------ */
  875. char *usage_func(char *iname, SwigType *, ParmList *l) {
  876. static String *temp = 0;
  877. Parm *p;
  878. int i;
  879. if (!temp)
  880. temp = NewString("");
  881. Clear(temp);
  882. Printf(temp, "%s(", iname);
  883. /* Now go through and print parameters */
  884. p = l;
  885. i = 0;
  886. while (p != 0) {
  887. SwigType *pt = Getattr(p, "type");
  888. String *pn = Getattr(p, "name");
  889. if (!checkAttribute(p,"tmap:in:numinputs","0")) {
  890. /* If parameter has been named, use that. Otherwise, just print a type */
  891. if (SwigType_type(pt) != T_VOID) {
  892. if (Len(pn) > 0) {
  893. Printf(temp, "%s", pn);
  894. } else {
  895. Printf(temp, "%s", SwigType_str(pt, 0));
  896. }
  897. }
  898. i++;
  899. p = nextSibling(p);
  900. if (p)
  901. if (!checkAttribute(p,"tmap:in:numinputs","0"))
  902. Putc(',', temp);
  903. } else {
  904. p = nextSibling(p);
  905. if (p)
  906. if ((i > 0) && (!checkAttribute(p,"tmap:in:numinputs","0")))
  907. Putc(',', temp);
  908. }
  909. }
  910. Printf(temp, ");");
  911. return Char(temp);
  912. }
  913. /* ------------------------------------------------------------
  914. * nativeWrapper()
  915. * ------------------------------------------------------------ */
  916. virtual int nativeWrapper(Node *n) {
  917. String *name = Getattr(n, "sym:name");
  918. String *funcname = Getattr(n, "wrap:name");
  919. if (!addSymbol(funcname, n))
  920. return SWIG_ERROR;
  921. Printf(command_tab, "{\"%s::%s\", %s},\n", cmodule, name, funcname);
  922. if (export_all)
  923. Printf(exported, "%s ", name);
  924. if (blessed) {
  925. Printv(func_stubs, "*", name, " = *", cmodule, "::", name, ";\n", NIL);
  926. }
  927. return SWIG_OK;
  928. }
  929. /* ----------------------------------------------------------------------------
  930. * OBJECT-ORIENTED FEATURES
  931. *
  932. * These extensions provide a more object-oriented interface to C++
  933. * classes and structures. The code here is based on extensions
  934. * provided by David Fletcher and Gary Holt.
  935. *
  936. * I have generalized these extensions to make them more general purpose
  937. * and to resolve object-ownership problems.
  938. *
  939. * The approach here is very similar to the Python module :
  940. * 1. All of the original methods are placed into a single
  941. * package like before except that a 'c' is appended to the
  942. * package name.
  943. *
  944. * 2. All methods and function calls are wrapped with a new
  945. * perl function. While possibly inefficient this allows
  946. * us to catch complex function arguments (which are hard to
  947. * track otherwise).
  948. *
  949. * 3. Classes are represented as tied-hashes in a manner similar
  950. * to Gary Holt's extension. This allows us to access
  951. * member data.
  952. *
  953. * 4. Stand-alone (global) C functions are modified to take
  954. * tied hashes as arguments for complex datatypes (if
  955. * appropriate).
  956. *
  957. * 5. Global variables involving a class/struct is encapsulated
  958. * in a tied hash.
  959. *
  960. * ------------------------------------------------------------------------- */
  961. void setclassname(Node *n) {
  962. String *symname = Getattr(n, "sym:name");
  963. String *fullname;
  964. String *actualpackage;
  965. Node *clsmodule = Getattr(n, "module");
  966. if (!clsmodule) {
  967. /* imported module does not define a module name. Oh well */
  968. return;
  969. }
  970. /* Do some work on the class name */
  971. if (verbose > 0) {
  972. String *modulename = Getattr(clsmodule, "name");
  973. fprintf(stdout, "setclassname: Found sym:name: %s\n", Char(symname));
  974. fprintf(stdout, "setclassname: Found module: %s\n", Char(modulename));
  975. fprintf(stdout, "setclassname: No package found\n");
  976. }
  977. if (dest_package) {
  978. fullname = NewStringf("%s::%s", namespace_module, symname);
  979. } else {
  980. actualpackage = Getattr(clsmodule,"name");
  981. if (verbose > 0) {
  982. fprintf(stdout, "setclassname: Found actualpackage: %s\n", Char(actualpackage));
  983. }
  984. if ((!compat) && (!Strchr(symname,':'))) {
  985. fullname = NewStringf("%s::%s",actualpackage,symname);
  986. } else {
  987. fullname = NewString(symname);
  988. }
  989. }
  990. if (verbose > 0) {
  991. fprintf(stdout, "setclassname: setting proxy: %s\n", Char(fullname));
  992. }
  993. Setattr(n, "perl5:proxy", fullname);
  994. }
  995. /* ------------------------------------------------------------
  996. * classDeclaration()
  997. * ------------------------------------------------------------ */
  998. virtual int classDeclaration(Node *n) {
  999. /* Do some work on the class name */
  1000. if (!Getattr(n, "feature:onlychildren")) {
  1001. if (blessed) {
  1002. setclassname(n);
  1003. Append(classlist, n);
  1004. }
  1005. }
  1006. return Language::classDeclaration(n);
  1007. }
  1008. /* ------------------------------------------------------------
  1009. * classHandler()
  1010. * ------------------------------------------------------------ */
  1011. virtual int classHandler(Node *n) {
  1012. if (blessed) {
  1013. have_constructor = 0;
  1014. have_operators = 0;
  1015. have_destructor = 0;
  1016. have_data_members = 0;
  1017. operators = NewHash();
  1018. class_name = Getattr(n, "sym:name");
  1019. if (!addSymbol(class_name, n))
  1020. return SWIG_ERROR;
  1021. /* Use the fully qualified name of the Perl class */
  1022. if (!compat) {
  1023. fullclassname = NewStringf("%s::%s", namespace_module, class_name);
  1024. } else {
  1025. fullclassname = NewString(class_name);
  1026. }
  1027. real_classname = Getattr(n, "name");
  1028. pcode = NewString("");
  1029. // blessedmembers = NewString("");
  1030. }
  1031. /* Emit all of the members */
  1032. Language::classHandler(n);
  1033. /* Finish the rest of the class */
  1034. if (blessed) {
  1035. /* Generate a client-data entry */
  1036. SwigType *ct = NewStringf("p.%s", real_classname);
  1037. Printv(f_init, "SWIG_TypeClientData(SWIGTYPE", SwigType_manglestr(ct), ", (void*) \"", fullclassname, "\");\n", NIL);
  1038. SwigType_remember(ct);
  1039. Delete(ct);
  1040. Printv(pm, "\n############# Class : ", fullclassname, " ##############\n", "\npackage ", fullclassname, ";\n", NIL);
  1041. if (have_operators) {
  1042. Printf(pm, "use overload\n");
  1043. Iterator ki;
  1044. for (ki = First(operators); ki.key; ki = Next(ki)) {
  1045. char *name = Char(ki.key);
  1046. // fprintf(stderr,"found name: <%s>\n", name);
  1047. if (strstr(name, "__eq__")) {
  1048. Printv(pm, tab4, "\"==\" => sub { $_[0]->__eq__($_[1])},\n",NIL);
  1049. } else if (strstr(name, "__ne__")) {
  1050. Printv(pm, tab4, "\"!=\" => sub { $_[0]->__ne__($_[1])},\n",NIL);
  1051. // there are no tests for this in operator_overload_runme.pl
  1052. // it is likely to be broken
  1053. // } else if (strstr(name, "__assign__")) {
  1054. // Printv(pm, tab4, "\"=\" => sub { $_[0]->__assign__($_[1])},\n",NIL);
  1055. } else if (strstr(name, "__str__")) {
  1056. Printv(pm, tab4, "'\"\"' => sub { $_[0]->__str__()},\n",NIL);
  1057. } else if (strstr(name, "__plusplus__")) {
  1058. Printv(pm, tab4, "\"++\" => sub { $_[0]->__plusplus__()},\n",NIL);
  1059. } else if (strstr(name, "__minmin__")) {
  1060. Printv(pm, tab4, "\"--\" => sub { $_[0]->__minmin__()},\n",NIL);
  1061. } else if (strstr(name, "__add__")) {
  1062. Printv(pm, tab4, "\"+\" => sub { $_[0]->__add__($_[1])},\n",NIL);
  1063. } else if (strstr(name, "__sub__")) {
  1064. Printv(pm, tab4, "\"-\" => sub { if( not $_[2] ) { $_[0]->__sub__($_[1]) }\n",NIL);
  1065. Printv(pm, tab8, "elsif( $_[0]->can('__rsub__') ) { $_[0]->__rsub__($_[1]) }\n",NIL);
  1066. Printv(pm, tab8, "else { die(\"reverse subtraction not supported\") }\n",NIL);
  1067. Printv(pm, tab8, "},\n",NIL);
  1068. } else if (strstr(name, "__mul__")) {
  1069. Printv(pm, tab4, "\"*\" => sub { $_[0]->__mul__($_[1])},\n",NIL);
  1070. } else if (strstr(name, "__div__")) {
  1071. Printv(pm, tab4, "\"/\" => sub { $_[0]->__div__($_[1])},\n",NIL);
  1072. } else if (strstr(name, "__mod__")) {
  1073. Printv(pm, tab4, "\"%\" => sub { $_[0]->__mod__($_[1])},\n",NIL);
  1074. // there are no tests for this in operator_overload_runme.pl
  1075. // it is likely to be broken
  1076. // } else if (strstr(name, "__and__")) {
  1077. // Printv(pm, tab4, "\"&\" => sub { $_[0]->__and__($_[1])},\n",NIL);
  1078. // there are no tests for this in operator_overload_runme.pl
  1079. // it is likely to be broken
  1080. // } else if (strstr(name, "__or__")) {
  1081. // Printv(pm, tab4, "\"|\" => sub { $_[0]->__or__($_[1])},\n",NIL);
  1082. } else if (strstr(name, "__gt__")) {
  1083. Printv(pm, tab4, "\">\" => sub { $_[0]->__gt__($_[1])},\n",NIL);
  1084. } else if (strstr(name, "__ge__")) {
  1085. Printv(pm, tab4, "\">=\" => sub { $_[0]->__ge__($_[1])},\n",NIL);
  1086. } else if (strstr(name, "__not__")) {
  1087. Printv(pm, tab4, "\"!\" => sub { $_[0]->__not__()},\n",NIL);
  1088. } else if (strstr(name, "__lt__")) {
  1089. Printv(pm, tab4, "\"<\" => sub { $_[0]->__lt__($_[1])},\n",NIL);
  1090. } else if (strstr(name, "__le__")) {
  1091. Printv(pm, tab4, "\"<=\" => sub { $_[0]->__le__($_[1])},\n",NIL);
  1092. } else if (strstr(name, "__pluseq__")) {
  1093. Printv(pm, tab4, "\"+=\" => sub { $_[0]->__pluseq__($_[1])},\n",NIL);
  1094. } else if (strstr(name, "__mineq__")) {
  1095. Printv(pm, tab4, "\"-=\" => sub { $_[0]->__mineq__($_[1])},\n",NIL);
  1096. } else if (strstr(name, "__neg__")) {
  1097. Printv(pm, tab4, "\"neg\" => sub { $_[0]->__neg__()},\n",NIL);
  1098. } else {
  1099. fprintf(stderr,"Unknown operator: %s\n", name);
  1100. }
  1101. }
  1102. Printv(pm, tab4,
  1103. "\"=\" => sub { my $class = ref($_[0]); $class->new($_[0]) },\n", NIL);
  1104. Printv(pm, tab4, "\"fallback\" => 1;\n", NIL);
  1105. }
  1106. // make use strict happy
  1107. Printv(pm, "use vars qw(@ISA %OWNER %ITERATORS %BLESSEDMEMBERS);\n", NIL);
  1108. /* If we are inheriting from a base class, set that up */
  1109. Printv(pm, "@ISA = qw(", NIL);
  1110. /* Handle inheritance */
  1111. List *baselist = Getattr(n, "bases");
  1112. if (baselist && Len(baselist)) {
  1113. Iterator b;
  1114. b = First(baselist);
  1115. while (b.item) {
  1116. String *bname = Getattr(b.item, "perl5:proxy");
  1117. if (!bname) {
  1118. b = Next(b);
  1119. continue;
  1120. }
  1121. Printv(pm, " ", bname, NIL);
  1122. b = Next(b);
  1123. }
  1124. }
  1125. /* Module comes last */
  1126. if (!compat || Cmp(namespace_module, fullclassname)) {
  1127. Printv(pm, " ", namespace_module, NIL);
  1128. }
  1129. Printf(pm, " );\n");
  1130. /* Dump out a hash table containing the pointers that we own */
  1131. Printf(pm, "%%OWNER = ();\n");
  1132. if (have_data_members || have_destructor)
  1133. Printf(pm, "%%ITERATORS = ();\n");
  1134. /* Dump out the package methods */
  1135. Printv(pm, pcode, NIL);
  1136. Delete(pcode);
  1137. /* Output methods for managing ownership */
  1138. Printv(pm,
  1139. "sub DISOWN {\n",
  1140. tab4, "my $self = shift;\n",
  1141. tab4, "my $ptr = tied(%$self);\n",
  1142. tab4, "delete $OWNER{$ptr};\n",
  1143. "}\n\n", "sub ACQUIRE {\n", tab4, "my $self = shift;\n", tab4, "my $ptr = tied(%$self);\n", tab4, "$OWNER{$ptr} = 1;\n", "}\n\n", NIL);
  1144. /* Only output the following methods if a class has member data */
  1145. Delete(operators);
  1146. operators = 0;
  1147. }
  1148. return SWIG_OK;
  1149. }
  1150. /* ------------------------------------------------------------
  1151. * memberfunctionHandler()
  1152. * ------------------------------------------------------------ */
  1153. virtual int memberfunctionHandler(Node *n) {
  1154. String *symname = Getattr(n, "sym:name");
  1155. member_func = 1;
  1156. Language::memberfunctionHandler(n);
  1157. member_func = 0;
  1158. if ((blessed) && (!Getattr(n, "sym:nextSibling"))) {
  1159. if (Strstr(symname, "__eq__")) {
  1160. DohSetInt(operators, "__eq__", 1);
  1161. have_operators = 1;
  1162. } else if (Strstr(symname, "__ne__")) {
  1163. DohSetInt(operators, "__ne__", 1);
  1164. have_operators = 1;
  1165. } else if (Strstr(symname, "__assign__")) {
  1166. DohSetInt(operators, "__assign__", 1);
  1167. have_operators = 1;
  1168. } else if (Strstr(symname, "__str__")) {
  1169. DohSetInt(operators, "__str__", 1);
  1170. have_operators = 1;
  1171. } else if (Strstr(symname, "__add__")) {
  1172. DohSetInt(operators, "__add__", 1);
  1173. have_operators = 1;
  1174. } else if (Strstr(symname, "__sub__")) {
  1175. DohSetInt(operators, "__sub__", 1);
  1176. have_operators = 1;
  1177. } else if (Strstr(symname, "__mul__")) {
  1178. DohSetInt(operators, "__mul__", 1);
  1179. have_operators = 1;
  1180. } else if (Strstr(symname, "__div__")) {
  1181. DohSetInt(operators, "__div__", 1);
  1182. have_operators = 1;
  1183. } else if (Strstr(symname, "__mod__")) {
  1184. DohSetInt(operators, "__mod__", 1);
  1185. have_operators = 1;
  1186. } else if (Strstr(symname, "__and__")) {
  1187. DohSetInt(operators, "__and__", 1);
  1188. have_operators = 1;
  1189. } else if (Strstr(symname, "__or__")) {
  1190. DohSetInt(operators, "__or__", 1);
  1191. have_operators = 1;
  1192. } else if (Strstr(symname, "__not__")) {
  1193. DohSetInt(operators, "__not__", 1);
  1194. have_operators = 1;
  1195. } else if (Strstr(symname, "__gt__")) {
  1196. DohSetInt(operators, "__gt__", 1);
  1197. have_operators = 1;
  1198. } else if (Strstr(symname, "__ge__")) {
  1199. DohSetInt(operators, "__ge__", 1);
  1200. have_operators = 1;
  1201. } else if (Strstr(symname, "__lt__")) {
  1202. DohSetInt(operators, "__lt__", 1);
  1203. have_operators = 1;
  1204. } else if (Strstr(symname, "__le__")) {
  1205. DohSetInt(operators, "__le__", 1);
  1206. have_operators = 1;
  1207. } else if (Strstr(symname, "__neg__")) {
  1208. DohSetInt(operators, "__neg__", 1);
  1209. have_operators = 1;
  1210. } else if (Strstr(symname, "__plusplus__")) {
  1211. DohSetInt(operators, "__plusplus__", 1);
  1212. have_operators = 1;
  1213. } else if (Strstr(symname, "__minmin__")) {
  1214. DohSetInt(operators, "__minmin__", 1);
  1215. have_operators = 1;
  1216. } else if (Strstr(symname, "__mineq__")) {
  1217. DohSetInt(operators, "__mineq__", 1);
  1218. have_operators = 1;
  1219. } else if (Strstr(symname, "__pluseq__")) {
  1220. DohSetInt(operators, "__pluseq__", 1);
  1221. have_operators = 1;
  1222. }
  1223. if (Getattr(n, "feature:shadow")) {
  1224. String *plcode = perlcode(Getattr(n, "feature:shadow"), 0);
  1225. String *plaction = NewStringf("%s::%s", cmodule, Swig_name_member(NSPACE_TODO, class_name, symname));
  1226. Replaceall(plcode, "$action", plaction);
  1227. Delete(plaction);
  1228. Printv(pcode, plcode, NIL);
  1229. } else {
  1230. Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL);
  1231. }
  1232. }
  1233. return SWIG_OK;
  1234. }
  1235. /* ------------------------------------------------------------
  1236. * membervariableHandler()
  1237. *
  1238. * Adds an instance member.
  1239. * ----------------------------------------------------------------------------- */
  1240. virtual int membervariableHandler(Node *n) {
  1241. String *symname = Getattr(n, "sym:name");
  1242. /* SwigType *t = Getattr(n,"type"); */
  1243. /* Emit a pair of get/set functions for the variable */
  1244. member_func = 1;
  1245. Language::membervariableHandler(n);
  1246. member_func = 0;
  1247. if (blessed) {
  1248. Printv(pcode, "*swig_", symname, "_get = *", cmodule, "::", Swig_name_get(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)), ";\n", NIL);
  1249. Printv(pcode, "*swig_", symname, "_set = *", cmodule, "::", Swig_name_set(NSPACE_TODO, Swig_name_member(NSPACE_TODO, class_name, symname)), ";\n", NIL);
  1250. /* Now we need to generate a little Perl code for this */
  1251. /* if (is_shadow(t)) {
  1252. *//* This is a Perl object that we have already seen. Add an
  1253. entry to the members list *//*
  1254. Printv(blessedmembers,
  1255. tab4, symname, " => '", is_shadow(t), "',\n",
  1256. NIL);
  1257. }
  1258. */
  1259. }
  1260. have_data_members++;
  1261. return SWIG_OK;
  1262. }
  1263. /* ------------------------------------------------------------
  1264. * constructorDeclaration()
  1265. *
  1266. * Emits a blessed constructor for our class. In addition to our construct
  1267. * we manage a Perl hash table containing all of the pointers created by
  1268. * the constructor. This prevents us from accidentally trying to free
  1269. * something that wasn't necessarily allocated by malloc or new
  1270. * ------------------------------------------------------------ */
  1271. virtual int constructorHandler(Node *n) {
  1272. String *symname = Getattr(n, "sym:name");
  1273. member_func = 1;
  1274. Language::constructorHandler(n);
  1275. if ((blessed) && (!Getattr(n, "sym:nextSibling"))) {
  1276. if (Getattr(n, "feature:shadow")) {
  1277. String *plcode = perlcode(Getattr(n, "feature:shadow"), 0);
  1278. String *plaction = NewStringf("%s::%s", module, Swig_name_member(NSPACE_TODO, class_name, symname));
  1279. Replaceall(plcode, "$action", plaction);
  1280. Delete(plaction);
  1281. Printv(pcode, plcode, NIL);
  1282. } else {
  1283. if ((Cmp(symname, class_name) == 0)) {
  1284. /* Emit a blessed constructor */
  1285. Printf(pcode, "sub new {\n");
  1286. } else {
  1287. /* Constructor doesn't match classname so we'll just use the normal name */
  1288. Printv(pcode, "sub ", Swig_name_construct(NSPACE_TODO, symname), " {\n", NIL);
  1289. }
  1290. Printv(pcode,
  1291. tab4, "my $pkg = shift;\n",
  1292. tab4, "my $self = ", cmodule, "::", Swig_name_construct(NSPACE_TODO, symname), "(@_);\n", tab4, "bless $self, $pkg if defined($self);\n", "}\n\n", NIL);
  1293. have_constructor = 1;
  1294. }
  1295. }
  1296. member_func = 0;
  1297. return SWIG_OK;
  1298. }
  1299. /* ------------------------------------------------------------
  1300. * destructorHandler()
  1301. * ------------------------------------------------------------ */
  1302. virtual int destructorHandler(Node *n) {
  1303. String *symname = Getattr(n, "sym:name");
  1304. member_func = 1;
  1305. Language::destructorHandler(n);
  1306. if (blessed) {
  1307. if (Getattr(n, "feature:shadow")) {
  1308. String *plcode = perlcode(Getattr(n, "feature:shadow"), 0);
  1309. String *plaction = NewStringf("%s::%s", module, Swig_name_member(NSPACE_TODO, class_name, symname));
  1310. Replaceall(plcode, "$action", plaction);
  1311. Delete(plaction);
  1312. Printv(pcode, plcode, NIL);
  1313. } else {
  1314. Printv(pcode,
  1315. "sub DESTROY {\n",
  1316. tab4, "return unless $_[0]->isa('HASH');\n",
  1317. tab4, "my $self = tied(%{$_[0]});\n",
  1318. tab4, "return unless defined $self;\n",
  1319. tab4, "delete $ITERATORS{$self};\n",
  1320. tab4, "if (exists $OWNER{$self}) {\n",
  1321. tab8, cmodule, "::", Swig_name_destroy(NSPACE_TODO, symname), "($self);\n", tab8, "delete $OWNER{$self};\n", tab4, "}\n}\n\n", NIL);
  1322. have_destructor = 1;
  1323. }
  1324. }
  1325. member_func = 0;
  1326. return SWIG_OK;
  1327. }
  1328. /* ------------------------------------------------------------
  1329. * staticmemberfunctionHandler()
  1330. * ------------------------------------------------------------ */
  1331. virtual int staticmemberfunctionHandler(Node *n) {
  1332. member_func = 1;
  1333. Language::staticmemberfunctionHandler(n);
  1334. member_func = 0;
  1335. if ((blessed) && (!Getattr(n, "sym:nextSibling"))) {
  1336. String *symname = Getattr(n, "sym:name");
  1337. Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL);
  1338. }
  1339. return SWIG_OK;
  1340. }
  1341. /* ------------------------------------------------------------
  1342. * staticmembervariableHandler()
  1343. * ------------------------------------------------------------ */
  1344. virtual int staticmembervariableHandler(Node *n) {
  1345. Language::staticmembervariableHandler(n);
  1346. if (blessed) {
  1347. String *symname = Getattr(n, "sym:name");
  1348. Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL);
  1349. }
  1350. return SWIG_OK;
  1351. }
  1352. /* ------------------------------------------------------------
  1353. * memberconstantHandler()
  1354. * ------------------------------------------------------------ */
  1355. virtual int memberconstantHandler(Node *n) {
  1356. String *symname = Getattr(n, "sym:name");
  1357. int oldblessed = blessed;
  1358. /* Create a normal constant */
  1359. blessed = 0;
  1360. Language::memberconstantHandler(n);
  1361. blessed = oldblessed;
  1362. if (blessed) {
  1363. Printv(pcode, "*", symname, " = *", cmodule, "::", Swig_name_member(NSPACE_TODO, class_name, symname), ";\n", NIL);
  1364. }
  1365. return SWIG_OK;
  1366. }
  1367. /* ------------------------------------------------------------
  1368. * pragma()
  1369. *
  1370. * Pragma directive.
  1371. *
  1372. * %pragma(perl5) code="String" # Includes a string in the .pm file
  1373. * %pragma(perl5) include="file.pl" # Includes a file in the .pm file
  1374. * ------------------------------------------------------------ */
  1375. virtual int pragmaDirective(Node *n) {
  1376. String *lang;
  1377. String *code;
  1378. String *value;
  1379. if (!ImportMode) {
  1380. lang = Getattr(n, "lang");
  1381. code = Getattr(n, "name");
  1382. value = Getattr(n, "value");
  1383. if (Strcmp(lang, "perl5") == 0) {
  1384. if (Strcmp(code, "code") == 0) {
  1385. /* Dump the value string into the .pm file */
  1386. if (value) {
  1387. Printf(pragma_include, "%s\n", value);
  1388. }
  1389. } else if (Strcmp(code, "include") == 0) {
  1390. /* Include a file into the .pm file */
  1391. if (value) {
  1392. FILE *f = Swig_include_open(value);
  1393. if (!f) {
  1394. Swig_error(input_file, line_number, "Unable to locate file %s\n", value);
  1395. } else {
  1396. char buffer[4096];
  1397. while (fgets(buffer, 4095, f)) {
  1398. Printf(pragma_include, "%s", buffer);
  1399. }
  1400. }
  1401. fclose(f);
  1402. }
  1403. } else {
  1404. Swig_error(input_file, line_number, "Unrecognized pragma.\n");
  1405. }
  1406. }
  1407. }
  1408. return Language::pragmaDirective(n);
  1409. }
  1410. /* ------------------------------------------------------------
  1411. * perlcode() - Output perlcode code into the shadow file
  1412. * ------------------------------------------------------------ */
  1413. String *perlcode(String *code, const String *indent) {
  1414. String *out = NewString("");
  1415. String *temp;
  1416. char *t;
  1417. if (!indent)
  1418. indent = "";
  1419. temp = NewString(code);
  1420. t = Char(temp);
  1421. if (*t == '{') {
  1422. Delitem(temp, 0);
  1423. Delitem(temp, DOH_END);
  1424. }
  1425. /* Split the input text into lines */
  1426. List *clist = DohSplitLines(temp);
  1427. Delete(temp);
  1428. int initial = 0;
  1429. String *s = 0;
  1430. Iterator si;
  1431. /* Get the initial indentation */
  1432. for (si = First(clist); si.item; si = Next(si)) {
  1433. s = si.item;
  1434. if (Len(s)) {
  1435. char *c = Char(s);
  1436. while (*c) {
  1437. if (!isspace(*c))
  1438. break;
  1439. initial++;
  1440. c++;
  1441. }
  1442. if (*c && !isspace(*c))
  1443. break;
  1444. else {
  1445. initial = 0;
  1446. }
  1447. }
  1448. }
  1449. while (si.item) {
  1450. s = si.item;
  1451. if (Len(s) > initial) {
  1452. char *c = Char(s);
  1453. c += initial;
  1454. Printv(out, indent, c, "\n", NIL);
  1455. } else {
  1456. Printv(out, "\n", NIL);
  1457. }
  1458. si = Next(si);
  1459. }
  1460. Delete(clist);
  1461. return out;
  1462. }
  1463. /* ------------------------------------------------------------
  1464. * insertDirective()
  1465. *
  1466. * Hook for %insert directive.
  1467. * ------------------------------------------------------------ */
  1468. virtual int insertDirective(Node *n) {
  1469. String *code = Getattr(n, "code");
  1470. String *section = Getattr(n, "section");
  1471. if ((!ImportMode) && (Cmp(section, "perl") == 0)) {
  1472. Printv(additional_perl_code, code, NIL);
  1473. } else {
  1474. Language::insertDirective(n);
  1475. }
  1476. return SWIG_OK;
  1477. }
  1478. String *runtimeCode() {
  1479. String *s = NewString("");
  1480. String *shead = Swig_include_sys("perlhead.swg");
  1481. if (!shead) {
  1482. Printf(stderr, "*** Unable to open 'perlhead.swg'\n");
  1483. } else {
  1484. Append(s, shead);
  1485. Delete(shead);
  1486. }
  1487. String *serrors = Swig_include_sys("perlerrors.swg");
  1488. if (!serrors) {
  1489. Printf(stderr, "*** Unable to open 'perlerrors.swg'\n");
  1490. } else {
  1491. Append(s, serrors);
  1492. Delete(serrors);
  1493. }
  1494. String *srun = Swig_include_sys("perlrun.swg");
  1495. if (!srun) {
  1496. Printf(stderr, "*** Unable to open 'perlrun.swg'\n");
  1497. } else {
  1498. Append(s, srun);
  1499. Delete(srun);
  1500. }
  1501. return s;
  1502. }
  1503. String *defaultExternalRuntimeFilename() {
  1504. return NewString("swigperlrun.h");
  1505. }
  1506. };
  1507. /* -----------------------------------------------------------------------------
  1508. * swig_perl5() - Instantiate module
  1509. * ----------------------------------------------------------------------------- */
  1510. static Language *new_swig_perl5() {
  1511. return new PERL5();
  1512. }
  1513. extern "C" Language *swig_perl5(void) {
  1514. return new_swig_perl5();
  1515. }