/tags/rel-1-3-24/SWIG/Source/Modules/perl5.cxx

# · C++ · 1541 lines · 1068 code · 232 blank · 241 comment · 242 complexity · c01155f1158bf9ced2d89b968b62127e MD5 · raw file

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