PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-7/SWIG/Source/Modules1.1/perl5.cxx

#
C++ | 1982 lines | 1378 code | 308 blank | 296 comment | 241 complexity | c80ede32496224f043084a4634042c8a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. *
  11. * Copyright (C) 1999-2000. The University of Chicago
  12. * See the file LICENSE for information on usage and redistribution.
  13. * ----------------------------------------------------------------------------- */
  14. /* DB: I had to take some features related to package naming out of this to
  15. get the new type system to work. These need to be put back in at some point. */
  16. static char cvsroot[] = "$Header$";
  17. #include "mod11.h"
  18. #include "perl5.h"
  19. #ifndef MACSWIG
  20. #include "swigconfig.h"
  21. #endif
  22. static char *usage = (char*)"\
  23. Perl5 Options (available with -perl5)\n\
  24. -ldflags - Print runtime libraries to link with\n\
  25. -module name - Set module name\n\
  26. -interface name - Set interface name\n\
  27. -package name - Set package prefix\n\
  28. -static - Omit code related to dynamic loading.\n\
  29. -shadow - Create shadow classes.\n\
  30. -const - Wrap constants as constants and not variables (implies -shadow).\n\
  31. -compat - Compatibility mode.\n\n";
  32. static String *import_file = 0;
  33. static List *import_stack = 0;
  34. static String *smodule = 0;
  35. static int compat = 0;
  36. static int export_all = 0;
  37. static String *package = 0;
  38. static String *module = 0;
  39. static String *interface = 0;
  40. static String *cmodule = 0;
  41. static String *vinit = 0;
  42. static FILE *f_pm = 0;
  43. static String *pm; /* Package initialization code */
  44. static String *magic; /* Magic variable wrappers */
  45. static int is_static = 0;
  46. /* The following variables are used to manage Perl5 classes */
  47. static int blessed = 0; /* Enable object oriented features */
  48. static int do_constants = 0; /* Constant wrapping */
  49. static Hash *classes = 0; /* A hash table for storing the classes we've seen so far */
  50. static int have_constructor = 0;
  51. static int have_destructor= 0;
  52. static int have_data_members = 0;
  53. static String *class_name = 0; /* Name of the class (what Perl thinks it is) */
  54. static String *class_type = 0; /* Type of class "struct", "class", "union" */
  55. static String *real_classname = 0; /* Real name of C/C++ class */
  56. static String *base_class = 0; /* Base class (if using inheritance) */
  57. static int class_renamed = 0;
  58. static String *fullclassname = 0;
  59. static String *pcode = 0; /* Perl code associated with each class */
  60. static String *blessedmembers = 0; /* Member data associated with each class */
  61. static int member_func = 0; /* Set to 1 when wrapping a member function */
  62. static String *realpackage = 0; /* Name of real module */
  63. static String *func_stubs = 0; /* Function stubs */
  64. static String *const_stubs = 0; /* Constant stubs */
  65. static int num_consts = 0; /* Number of constants */
  66. static String *var_stubs = 0; /* Variable stubs */
  67. static String *member_keys = 0; /* Keys for all member data */
  68. static String *exported = 0; /* Exported symbols */
  69. static String *pragma_include = 0;
  70. /* Test to see if a type corresponds to something wrapped with a shadow class */
  71. static DOH *is_shadow(SwigType *t) {
  72. DOH *r;
  73. SwigType *lt = Swig_clocal_type(t);
  74. r = Getattr(classes,lt);
  75. Delete(lt);
  76. return r;
  77. }
  78. /* -----------------------------------------------------------------------------
  79. * PERL5::parse_args()
  80. * ----------------------------------------------------------------------------- */
  81. void
  82. PERL5::parse_args(int argc, char *argv[]) {
  83. int i = 1;
  84. cmodule = NewString("");
  85. strcpy(LibDir,"perl5");
  86. for (i = 1; i < argc; i++) {
  87. if (argv[i]) {
  88. if(strcmp(argv[i],"-package") == 0) {
  89. if (argv[i+1]) {
  90. package = NewString(argv[i+1]);
  91. Swig_mark_arg(i);
  92. Swig_mark_arg(i+1);
  93. i++;
  94. } else {
  95. Swig_arg_error();
  96. }
  97. } else if(strcmp(argv[i],"-interface") == 0) {
  98. if (argv[i+1]) {
  99. interface = NewString(argv[i+1]);
  100. Swig_mark_arg(i);
  101. Swig_mark_arg(i+1);
  102. i++;
  103. } else {
  104. Swig_arg_error();
  105. }
  106. } else if (strcmp(argv[i],"-module") == 0) {
  107. if (argv[i+1]) {
  108. module = NewString(argv[i+1]);
  109. Append(cmodule,module);
  110. Replace(cmodule,":","_",DOH_REPLACE_ANY);
  111. Swig_mark_arg(i);
  112. Swig_mark_arg(i+1);
  113. i++;
  114. } else {
  115. Swig_arg_error();
  116. }
  117. } else if (strcmp(argv[i],"-exportall") == 0) {
  118. export_all = 1;
  119. Swig_mark_arg(i);
  120. } else if (strcmp(argv[i],"-static") == 0) {
  121. is_static = 1;
  122. Swig_mark_arg(i);
  123. } else if (strcmp(argv[i],"-shadow") == 0) {
  124. blessed = 1;
  125. Swig_mark_arg(i);
  126. } else if (strcmp(argv[i],"-const") == 0) {
  127. do_constants = 1;
  128. blessed = 1;
  129. Swig_mark_arg(i);
  130. } else if (strcmp(argv[i],"-compat") == 0) {
  131. compat = 1;
  132. Swig_mark_arg(i);
  133. } else if (strcmp(argv[i],"-help") == 0) {
  134. fputs(usage,stderr);
  135. } else if (strcmp (argv[i], "-ldflags") == 0) {
  136. printf("%s\n", SWIG_PERL_RUNTIME);
  137. SWIG_exit (EXIT_SUCCESS);
  138. }
  139. }
  140. }
  141. Preprocessor_define((void *) "SWIGPERL 1", 0);
  142. Preprocessor_define((void *) "SWIGPERL5 1", 0);
  143. typemap_lang = (char*)"perl5";
  144. }
  145. /* -----------------------------------------------------------------------------
  146. * PERL5::parse()
  147. * ----------------------------------------------------------------------------- */
  148. void
  149. PERL5::parse() {
  150. classes = NewHash();
  151. vinit = NewString("");
  152. pm = NewString("");
  153. func_stubs = NewString("");
  154. var_stubs = NewString("");
  155. const_stubs = NewString("");
  156. exported = NewString("");
  157. magic = NewString("");
  158. pragma_include = NewString("");
  159. import_stack = NewList();
  160. Swig_banner(f_runtime);
  161. if (NoInclude) {
  162. Printf(f_runtime,"#define SWIG_NOINCLUDE\n");
  163. }
  164. if (Swig_insert_file("common.swg", f_runtime) == -1) {
  165. Printf(stderr,"SWIG : Fatal error. Unable to locate 'common.swg' in SWIG library.\n");
  166. SWIG_exit (EXIT_FAILURE);
  167. }
  168. if (Swig_insert_file("perl5.swg", f_runtime) == -1) {
  169. Printf(stderr,"SWIG : Fatal error. Unable to locate 'perl5.swg' in SWIG library.\n");
  170. SWIG_exit (EXIT_FAILURE);
  171. }
  172. yyparse();
  173. }
  174. /* -----------------------------------------------------------------------------
  175. * PERL5::import_start(char *modname)
  176. * ----------------------------------------------------------------------------- */
  177. void
  178. PERL5::import_start(char *modname) {
  179. if (blessed) {
  180. Printf(f_pm,"require %s;\n", modname);
  181. }
  182. /* Save the old module */
  183. if (import_file) {
  184. Append(import_stack,import_file);
  185. }
  186. import_file = NewString(modname);
  187. }
  188. void
  189. PERL5::import_end() {
  190. Delete(import_file);
  191. if (Len(import_stack)) {
  192. import_file = Copy(Getitem(import_stack,Len(import_stack)-1));
  193. Delitem(import_stack,Len(import_stack)-1);
  194. } else {
  195. import_file = 0;
  196. }
  197. }
  198. /* -----------------------------------------------------------------------------
  199. * PERL5::set_module()
  200. * ----------------------------------------------------------------------------- */
  201. void
  202. PERL5::set_module(char *mod_name) {
  203. if (module) return;
  204. module = NewString(mod_name);
  205. /* Create a C module name and put it in 'cmodule' */
  206. Clear(cmodule);
  207. Append(cmodule,module);
  208. Replace(cmodule,":","_",DOH_REPLACE_ANY);
  209. }
  210. /* -----------------------------------------------------------------------------
  211. * PERL5::initialize()
  212. * ----------------------------------------------------------------------------- */
  213. void
  214. PERL5::initialize()
  215. {
  216. char filen[256];
  217. if (!module){
  218. Printf(stderr,"*** Error. No module name specified.\n");
  219. SWIG_exit (EXIT_FAILURE);
  220. }
  221. if (!package) {
  222. package = NewString(module);
  223. }
  224. /* If we're in blessed mode, change the package name to "packagec" */
  225. if (blessed) {
  226. realpackage = package;
  227. package = interface ? interface : NewStringf("%sc",package);
  228. } else {
  229. realpackage = NewString(package);
  230. }
  231. /* Create a .pm file
  232. * Need to strip off any prefixes that might be found in
  233. * the module name */
  234. {
  235. char *m = Char(module) + Len(module);
  236. while (m != Char(module)) {
  237. if (*m == ':') {
  238. m++;
  239. break;
  240. }
  241. m--;
  242. }
  243. sprintf(filen,"%s%s.pm", output_dir,m);
  244. if ((f_pm = fopen(filen,"w")) == 0) {
  245. Printf(stderr,"Unable to open %s\n", filen);
  246. SWIG_exit (EXIT_FAILURE);
  247. }
  248. }
  249. if (!blessed) {
  250. smodule = NewString(module);
  251. } else if (is_static) {
  252. smodule = NewStringf("%sc",module);
  253. Append(cmodule,"c");
  254. Append(cmodule,"c");
  255. } else {
  256. smodule = NewString(module);
  257. }
  258. {
  259. String *tmp = NewString(realpackage);
  260. Replace(tmp,":","_", DOH_REPLACE_ANY);
  261. Printf(f_header,"#define SWIG_init boot_%s\n\n", tmp);
  262. Printf(f_header,"#define SWIG_name \"%s::boot_%s\"\n", package, tmp);
  263. Delete(tmp);
  264. }
  265. Printf(f_header,"#define SWIG_varinit \"%s::var_%s_init();\"\n", package, cmodule);
  266. Printf(f_header,"#ifdef __cplusplus\n");
  267. Printf(f_header,"extern \"C\"\n");
  268. Printf(f_header,"#endif\n");
  269. Printf(f_header,"#ifndef PERL_OBJECT\n");
  270. Printf(f_header,"XS(SWIG_init);\n");
  271. Printf(f_header,"#else\n");
  272. Printf(f_header,"SWIGEXPORT(void) SWIG_init (CV *cv, CPerlObj *);\n");
  273. Printf(f_header,"#endif\n");
  274. Printf(f_init,"#ifdef __cplusplus\n");
  275. Printf(f_init,"extern \"C\"\n");
  276. Printf(f_init,"#endif\n");
  277. Printf(f_init,"XS(SWIG_init) {\n");
  278. Printf(f_init,"\t dXSARGS;\n");
  279. Printf(f_init,"\t int i;\n");
  280. Printf(f_init,"\t char *file = (char *) __FILE__;\n");
  281. Printv(f_init,
  282. "for (i = 0; swig_types_initial[i]; i++) {\n",
  283. "swig_types[i] = SWIG_TypeRegister(swig_types_initial[i]);\n",
  284. "}\n", 0);
  285. Printf(f_init,"\t newXS((char *) \"%s::var_%s_init\", _wrap_perl5_%s_var_init, file);\n",package,cmodule, cmodule);
  286. Printv(vinit,
  287. "XS(_wrap_perl5_", cmodule, "_var_init) {\n",
  288. tab4, "dXSARGS;\n",
  289. tab4, "SV *sv;\n",
  290. 0);
  291. Printf(f_pm,"# This file was automatically generated by SWIG\n");
  292. Printf(f_pm,"package %s;\n",realpackage);
  293. Printf(f_pm,"require Exporter;\n");
  294. if (!is_static) {
  295. Printf(f_pm,"require DynaLoader;\n");
  296. Printf(f_pm,"@ISA = qw(Exporter DynaLoader);\n");
  297. } else {
  298. Printf(f_pm,"@ISA = qw(Exporter);\n");
  299. }
  300. /* Start creating magic code */
  301. Printv(magic,
  302. "#ifdef PERL_OBJECT\n",
  303. "#define MAGIC_CLASS _wrap_", module, "_var::\n",
  304. "class _wrap_", module, "_var : public CPerlObj {\n",
  305. "public:\n",
  306. "#else\n",
  307. "#define MAGIC_CLASS\n",
  308. "#endif\n",
  309. "SWIGCLASS_STATIC int swig_magic_readonly(SV *sv, MAGIC *mg) {\n",
  310. tab4, "MAGIC_PPERL\n",
  311. tab4, "sv = sv; mg = mg;\n",
  312. tab4, "croak(\"Value is read-only.\");\n",
  313. tab4, "return 0;\n",
  314. "}\n",
  315. 0);
  316. }
  317. /* -----------------------------------------------------------------------------
  318. * PERL5::close()
  319. * ----------------------------------------------------------------------------- */
  320. void
  321. PERL5::close(void) {
  322. String *base = NewString("");
  323. /* Dump out variable wrappers */
  324. Printv(magic,
  325. "\n\n#ifdef PERL_OBJECT\n",
  326. "};\n",
  327. "#endif\n",
  328. 0);
  329. Printf(f_header,"%s\n", magic);
  330. String *type_table = NewString("");
  331. SwigType_emit_type_table(f_runtime,type_table);
  332. /* Patch the type table to reflect the names used by shadow classes */
  333. if (blessed) {
  334. SwigType *type;
  335. for (type = Firstkey(classes); type; type = Nextkey(classes)) {
  336. String *mangle = NewStringf("\"%s\"", SwigType_manglestr(type));
  337. String *rep = NewStringf("\"%s\"", Getattr(classes,type));
  338. Replace(type_table,mangle,rep,DOH_REPLACE_ANY);
  339. Delete(mangle);
  340. Delete(rep);
  341. }
  342. }
  343. Printf(f_wrappers,"%s",type_table);
  344. Delete(type_table);
  345. /* Printf(stdout,"::: Perl shadow :::\n\n%s",classes); */
  346. Printf(f_init,"\t ST(0) = &PL_sv_yes;\n");
  347. Printf(f_init,"\t XSRETURN(1);\n");
  348. Printf(f_init,"}\n");
  349. Printv(vinit,tab4, "XSRETURN(1);\n", "}\n", 0);
  350. Printf(f_wrappers,"%s", vinit);
  351. Printf(f_pm,"package %s;\n", package);
  352. if (!is_static) {
  353. Printf(f_pm,"bootstrap %s;\n", realpackage);
  354. } else {
  355. String *tmp = NewString(realpackage);
  356. Replace(tmp,":","_",DOH_REPLACE_ANY);
  357. Printf(f_pm,"boot_%s();\n", tmp);
  358. Delete(tmp);
  359. }
  360. Printf(f_pm,"var_%s_init();\n", cmodule);
  361. Printf(f_pm,"%s",pragma_include);
  362. Printf(f_pm,"package %s;\n", realpackage);
  363. Printf(f_pm,"@EXPORT = qw( %s);\n",exported);
  364. if (blessed) {
  365. Printv(base,
  366. "\n# ---------- BASE METHODS -------------\n\n",
  367. "package ", realpackage, ";\n\n",
  368. 0);
  369. /* Write out the TIE method */
  370. Printv(base,
  371. "sub TIEHASH {\n",
  372. tab4, "my ($classname,$obj) = @_;\n",
  373. tab4, "return bless $obj, $classname;\n",
  374. "}\n\n",
  375. 0);
  376. /* Output a CLEAR method. This is just a place-holder, but by providing it we
  377. * can make declarations such as
  378. * %$u = ( x => 2, y=>3, z =>4 );
  379. *
  380. * Where x,y,z are the members of some C/C++ object. */
  381. Printf(base,"sub CLEAR { }\n\n");
  382. /* Output default firstkey/nextkey methods */
  383. Printf(base, "sub FIRSTKEY { }\n\n");
  384. Printf(base, "sub NEXTKEY { }\n\n");
  385. /* Output a 'this' method */
  386. Printv(base,
  387. "sub this {\n",
  388. tab4, "my $ptr = shift;\n",
  389. tab4, "return tied(%$ptr);\n",
  390. "}\n\n",
  391. 0);
  392. Printf(f_pm,"%s",base);
  393. /* Emit function stubs for stand-alone functions */
  394. Printf(f_pm,"\n# ------- FUNCTION WRAPPERS --------\n\n");
  395. Printf(f_pm,"package %s;\n\n",realpackage);
  396. Printf(f_pm,"%s",func_stubs);
  397. /* Emit package code for different classes */
  398. Printf(f_pm,"%s",pm);
  399. if (num_consts > 0) {
  400. /* Emit constant stubs */
  401. Printf(f_pm,"\n# ------- CONSTANT STUBS -------\n\n");
  402. Printf(f_pm,"package %s;\n\n",realpackage);
  403. Printf(f_pm,"%s",const_stubs);
  404. }
  405. /* Emit variable stubs */
  406. Printf(f_pm,"\n# ------- VARIABLE STUBS --------\n\n");
  407. Printf(f_pm,"package %s;\n\n",realpackage);
  408. Printf(f_pm,"%s",var_stubs);
  409. }
  410. Printf(f_pm,"1;\n");
  411. fclose(f_pm);
  412. Delete(base);
  413. }
  414. /* -----------------------------------------------------------------------------
  415. * get_pointer()
  416. * ----------------------------------------------------------------------------- */
  417. static void
  418. get_pointer(char *iname, char *srcname, char *src, char *dest,
  419. SwigType *t, String *f, char *ret) {
  420. SwigType_remember(t);
  421. SwigType *lt = Swig_clocal_type(t);
  422. Printv(f, "if (SWIG_ConvertPtr(", src, ",(void **) &", dest, ",", 0);
  423. /* If we're passing a void pointer, we give the pointer conversion a NULL
  424. pointer, otherwise pass in the expected type. */
  425. if (Cmp(lt,"p.void") == 0) {
  426. Printf(f, " 0 ) < 0) {\n");
  427. } else {
  428. Printv(f, "SWIGTYPE", SwigType_manglestr(t), ") < 0) {\n",0);
  429. }
  430. Printv(f,
  431. "croak(\"Type error in ", srcname, " of ", iname,". Expected %s\", SWIGTYPE",
  432. SwigType_manglestr(t), "->name);\n",
  433. ret, ";\n",
  434. "}\n",
  435. 0);
  436. Delete(lt);
  437. }
  438. /* -----------------------------------------------------------------------------
  439. * PERL5::create_command()
  440. * ----------------------------------------------------------------------------- */
  441. void
  442. PERL5::create_command(char *cname, char *iname) {
  443. Printf(f_init,"\t newXS((char *) \"%s::%s\", %s, file);\n", package, iname, Swig_name_wrapper(cname));
  444. if (export_all) {
  445. Printf(exported,"%s ",iname);
  446. }
  447. }
  448. /* -----------------------------------------------------------------------------
  449. * PERL5::create_function()
  450. * ----------------------------------------------------------------------------- */
  451. void
  452. PERL5::create_function(char *name, char *iname, SwigType *d, ParmList *l)
  453. {
  454. Parm *p;
  455. int pcount,i,j;
  456. Wrapper *f;
  457. char source[256],target[256],temp[256], argnum[32];
  458. char *tm;
  459. String *cleanup, *outarg;
  460. int numopt = 0;
  461. int need_save, num_saved = 0;
  462. f = NewWrapper();
  463. cleanup = NewString("");
  464. outarg = NewString("");
  465. Printv(f->def, "XS(", Swig_name_wrapper(iname), ") {\n", 0);
  466. pcount = emit_args(d, l, f);
  467. numopt = check_numopt(l);
  468. Wrapper_add_local(f,"argvi","int argvi = 0");
  469. /* Check the number of arguments */
  470. Printf(f->code," if ((items < %d) || (items > %d)) \n", pcount-numopt, ParmList_numarg(l));
  471. Printf(f->code," croak(\"Usage: %s\");\n", usage_func(iname,d,l));
  472. /* Write code to extract parameters. */
  473. i = 0;
  474. j = 0;
  475. for (p = l; p; p = Getnext(p)) {
  476. SwigType *pt = Gettype(p);
  477. String *pn = Getname(p);
  478. /* Produce string representation of source and target arguments */
  479. sprintf(source,"ST(%d)",j);
  480. sprintf(target,"%s", Char(Getlname(p)));
  481. sprintf(argnum,"%d",j+1);
  482. /* Check to see if this argument is being ignored */
  483. if (!Getignore(p)) {
  484. /* Check for optional argument */
  485. if (j>= (pcount-numopt))
  486. Printf(f->code," if (items > %d) {\n", j);
  487. if ((tm = Swig_typemap_lookup((char*)"in",pt,pn,source,target,f))) {
  488. Printf(f->code,"%s\n",tm);
  489. Replace(f->code,"$argnum",argnum,DOH_REPLACE_ANY);
  490. Replace(f->code,"$arg",source,DOH_REPLACE_ANY);
  491. } else {
  492. switch(SwigType_type(pt)) {
  493. case T_BOOL:
  494. case T_INT :
  495. case T_SHORT :
  496. case T_LONG :
  497. case T_SCHAR:
  498. case T_UINT:
  499. case T_USHORT:
  500. case T_ULONG:
  501. case T_UCHAR:
  502. Printf(f->code," %s = (%s)SvIV(ST(%d));\n", target, SwigType_lstr(pt,0),j);
  503. break;
  504. case T_CHAR :
  505. Printf(f->code," %s = (char) *SvPV(ST(%d),PL_na);\n", target, j);
  506. break;
  507. case T_DOUBLE :
  508. case T_FLOAT :
  509. Printf(f->code," %s = (%s)SvNV(ST(%d));\n", target, SwigType_lstr(pt,0), j);
  510. break;
  511. case T_VOID :
  512. break;
  513. case T_USER:
  514. SwigType_add_pointer(pt);
  515. sprintf(temp,"argument %d", i+1);
  516. get_pointer(iname, temp, source, target, pt, f->code, (char *)"XSRETURN(1)");
  517. SwigType_del_pointer(pt);
  518. break;
  519. case T_STRING:
  520. Printf(f->code," if (! SvOK((SV*) ST(%d))) { %s = 0; }\n", j, target);
  521. Printf(f->code," else { %s = (char *) SvPV(ST(%d),PL_na); }\n", target,j);
  522. break;
  523. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  524. sprintf(temp,"argument %d", i+1);
  525. get_pointer(iname,temp,source,target, pt, f->code, (char*)"XSRETURN(1)");
  526. break;
  527. default :
  528. Printf(stderr,"%s : Line %d. Unable to use type %s as a function argument.\n",input_file, line_number, SwigType_str(pt,0));
  529. break;
  530. }
  531. }
  532. /* The source is going to be an array of saved values. */
  533. sprintf(temp,"_saved[%d]",num_saved);
  534. if (j>= (pcount-numopt))
  535. Printf(f->code," } \n");
  536. j++;
  537. } else {
  538. temp[0] = 0;
  539. }
  540. /* Check if there is any constraint code */
  541. if ((tm = Swig_typemap_lookup((char*)"check",pt,pn,source,target,0))) {
  542. Printf(f->code,"%s\n", tm);
  543. Replace(f->code,"$argnum",argnum, DOH_REPLACE_ANY);
  544. }
  545. need_save = 0;
  546. if ((tm = Swig_typemap_lookup((char*)"freearg",pt,pn,target,temp,0))) {
  547. Printf(cleanup,"%s\n", tm);
  548. Replace(cleanup,"$argnum",argnum,DOH_REPLACE_ANY);
  549. Replace(cleanup,"$arg",temp,DOH_REPLACE_ANY);
  550. need_save = 1;
  551. }
  552. if ((tm = Swig_typemap_lookup((char*)"argout",pt,pn,target,(char*)"ST(argvi)",0))) {
  553. String *tempstr = NewString(tm);
  554. Replace(tempstr,"$argnum",argnum, DOH_REPLACE_ANY);
  555. Replace(tempstr,"$arg",temp, DOH_REPLACE_ANY);
  556. Printf(outarg,"%s\n", tempstr);
  557. Delete(tempstr);
  558. need_save = 1;
  559. }
  560. /* If we need a saved variable, we need to emit to emit some code for that
  561. This only applies if the argument actually existed (not ignore) */
  562. if ((need_save) && (!Getignore(p))) {
  563. Printv(f->code, tab4, temp, " = ", source, ";\n", 0);
  564. num_saved++;
  565. }
  566. i++;
  567. }
  568. /* If there were any saved arguments, emit a local variable for them */
  569. if (num_saved) {
  570. sprintf(temp,"_saved[%d]",num_saved);
  571. Wrapper_add_localv(f,"_saved","SV *",temp,0);
  572. }
  573. /* Now write code to make the function call */
  574. emit_func_call(name,d,l,f);
  575. if ((tm = Swig_typemap_lookup((char*)"out",d,iname,(char*)"result",(char*)"ST(argvi)",0))) {
  576. Printf(f->code, "%s\n", tm);
  577. } else {
  578. if (SwigType_type(d) != T_VOID) {
  579. Printf(f->code," ST(argvi) = sv_newmortal();\n");
  580. switch (SwigType_type(d)) {
  581. case T_INT: case T_BOOL: case T_UINT:
  582. case T_SHORT: case T_USHORT:
  583. case T_LONG : case T_ULONG:
  584. case T_SCHAR: case T_UCHAR :
  585. Printf(f->code," sv_setiv(ST(argvi++),(IV) result);\n");
  586. break;
  587. case T_DOUBLE :
  588. case T_FLOAT :
  589. Printf(f->code," sv_setnv(ST(argvi++), (double) result);\n");
  590. break;
  591. case T_CHAR :
  592. Wrapper_add_local(f,"_ctemp", "char ctemp[2]");
  593. Printv(f->code,
  594. tab4, "ctemp[0] = result;\n",
  595. tab4, "ctemp[1] = 0;\n",
  596. tab4, "sv_setpv((SV*)ST(argvi++),ctemp);\n",
  597. 0);
  598. break;
  599. case T_USER:
  600. SwigType_add_pointer(d);
  601. SwigType_remember(d);
  602. Printv(f->code,
  603. tab4, "SWIG_MakePtr(ST(argvi++), (void *) result, SWIGTYPE", SwigType_manglestr(d),");\n", 0);
  604. SwigType_del_pointer(d);
  605. break;
  606. case T_STRING:
  607. Printf(f->code," sv_setpv((SV*)ST(argvi++),(char *) result);\n");
  608. break;
  609. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  610. SwigType_remember(d);
  611. Printv(f->code, tab4, "SWIG_MakePtr(ST(argvi++), (void *) result, SWIGTYPE", SwigType_manglestr(d), ");\n", 0);
  612. break;
  613. default :
  614. Printf(stderr,"%s: Line %d. Unable to use return type %s in function %s.\n", input_file, line_number, SwigType_str(d,0), name);
  615. break;
  616. }
  617. }
  618. }
  619. /* If there were any output args, take care of them. */
  620. Printv(f->code,outarg,0);
  621. /* If there was any cleanup, do that. */
  622. Printv(f->code,cleanup,0);
  623. if (NewObject) {
  624. if ((tm = Swig_typemap_lookup((char*)"newfree",d,iname,(char*)"result",(char*)"",0))) {
  625. Printf(f->code,"%s\n",tm);
  626. }
  627. }
  628. if ((tm = Swig_typemap_lookup((char*)"ret",d,iname,(char*)"result",(char*)"",0))) {
  629. Printf(f->code,"%s\n", tm);
  630. }
  631. Printf(f->code," XSRETURN(argvi);\n}\n");
  632. /* Add the dXSARGS last */
  633. Wrapper_add_local(f,"dXSARGS","dXSARGS");
  634. /* Substitute the cleanup code */
  635. Replace(f->code,"$cleanup",cleanup,DOH_REPLACE_ANY);
  636. Replace(f->code,"$name",iname,DOH_REPLACE_ANY);
  637. /* Dump the wrapper function */
  638. Wrapper_print(f,f_wrappers);
  639. /* Now register the function */
  640. Printf(f_init,"\t newXS((char *) \"%s::%s\", %s, file);\n", package, iname, Swig_name_wrapper(iname));
  641. if (export_all) {
  642. Printf(exported,"%s ", iname);
  643. }
  644. /* --------------------------------------------------------------------
  645. * Create a stub for this function, provided it's not a member function
  646. *
  647. * Really we only need to create a stub if this function involves
  648. * complex datatypes. If it does, we'll make a small wrapper to
  649. * process the arguments. If it doesn't, we'll just make a symbol
  650. * table entry.
  651. * -------------------------------------------------------------------- */
  652. if ((blessed) && (!member_func)) {
  653. int need_stub = 0;
  654. String *func = NewString("");
  655. /* We'll make a stub since we may need it anyways */
  656. Printv(func, "sub ", iname, " {\n",
  657. tab4, "my @args = @_;\n",
  658. 0);
  659. /* Now we have to go through and patch up the argument list. If any
  660. * arguments to our function correspond to other Perl objects, we
  661. * need to extract them from a tied-hash table object.*/
  662. Parm *p = l;
  663. int i = 0;
  664. while(p) {
  665. SwigType *pt = Gettype(p);
  666. if (!Getignore(p)) {
  667. /* Look up the datatype name here */
  668. char sourceNtarget[256];
  669. sprintf(sourceNtarget,"$args[%d]",i);
  670. if ((tm = Swig_typemap_lookup((char*)"perl5in",pt,(char*)"",sourceNtarget,sourceNtarget,0))) {
  671. Printf(func,"%s\n", tm);
  672. } else if (is_shadow(pt)) {
  673. /*
  674. if (i >= (pcount - numopt))
  675. Printf(func," if (scalar(@args) >= %d) {\n ", i);
  676. Printf(func," $args[%d] = tied(%%{$args[%d]});\n", i, i);
  677. if (i >= (pcount - numopt))
  678. Printf(func," }\n");
  679. need_stub = 1;
  680. */
  681. }
  682. i++;
  683. }
  684. p = Getnext(p);
  685. }
  686. Printv(func, tab4, "my $result = ", package, "::", iname, "(@args);\n", 0);
  687. /* Now check to see what kind of return result was found.
  688. * If this function is returning a result by 'value', SWIG did an
  689. * implicit malloc/new. We'll mark the object like it was created
  690. * in Perl so we can garbage collect it. */
  691. if ((tm = Swig_typemap_lookup((char*)"perl5out",d,(char*)"",name,(char*)"sv",0))) {
  692. Printv(func,
  693. tm, "\n",
  694. tab4, "return $result;\n",
  695. "}\n",
  696. 0);
  697. need_stub = 1;
  698. } else if (is_shadow(d)) {
  699. Printv(func, tab4, "return undef if (!defined($result));\n", 0);
  700. /* If we're returning an object by value, put it's reference
  701. into our local hash table */
  702. if ((!SwigType_ispointer(d)) || NewObject) {
  703. Printv(func, tab4, "$", is_shadow(d), "::OWNER{$result} = 1;\n", 0);
  704. }
  705. /* We're returning a Perl "object" of some kind. Turn it into a tied hash */
  706. Printv(func,
  707. tab4, "my %resulthash;\n",
  708. tab4, "tie %resulthash, ref($result), $result;\n",
  709. tab4, "return bless \\%resulthash, ref($result);\n",
  710. "}\n",
  711. 0);
  712. need_stub = 1;
  713. } else {
  714. /* Hmmm. This doesn't appear to be anything I know about */
  715. Printv(func, tab4, "return $result;\n", "}\n", 0);
  716. }
  717. /* Now check if we needed the stub. If so, emit it, otherwise
  718. * Emit code to hack Perl's symbol table instead */
  719. if (need_stub) {
  720. Printf(func_stubs,"%s",func);
  721. } else {
  722. Printv(func_stubs,"*", iname, " = *", package, "::", iname, ";\n", 0);
  723. }
  724. Delete(func);
  725. }
  726. Delete(cleanup);
  727. Delete(outarg);
  728. DelWrapper(f);
  729. }
  730. /* -----------------------------------------------------------------------------
  731. * PERL5::link_variable()
  732. * ----------------------------------------------------------------------------- */
  733. void PERL5::link_variable(char *name, char *iname, SwigType *t)
  734. {
  735. char set_name[256];
  736. char val_name[256];
  737. Wrapper *getf, *setf;
  738. char *tm;
  739. int setable = 1;
  740. sprintf(set_name,"_wrap_set_%s",iname);
  741. sprintf(val_name,"_wrap_val_%s",iname);
  742. getf = NewWrapper();
  743. setf = NewWrapper();
  744. /* Create a new scalar that we will attach magic to */
  745. Printv(vinit, tab4, "sv = perl_get_sv(\"", package, "::", iname, "\",TRUE | 0x2);\n", 0);
  746. /* Create a Perl function for setting the variable value */
  747. if (!(Status & STAT_READONLY)) {
  748. Printf(setf->def,"SWIGCLASS_STATIC int %s(SV* sv, MAGIC *mg) {\n", set_name);
  749. Printv(setf->code,
  750. tab4, "MAGIC_PPERL\n",
  751. tab4, "mg = mg;\n",
  752. 0);
  753. /* Check for a few typemaps */
  754. if ((tm = Swig_typemap_lookup((char*)"varin",t,(char*)"",(char*)"sv",name,0))) {
  755. Printf(setf->code,"%s\n", tm);
  756. } else if ((tm = Swig_typemap_lookup((char*)"in",t,(char*)"",(char*)"sv",name,0))) {
  757. Printf(setf->code,"%s\n", tm);
  758. } else {
  759. switch(SwigType_type(t)) {
  760. case T_INT : case T_BOOL: case T_UINT:
  761. case T_SHORT : case T_USHORT:
  762. case T_LONG : case T_ULONG:
  763. case T_UCHAR: case T_SCHAR:
  764. Printv(setf->code,tab4, name, " = (", SwigType_str(t,0), ") SvIV(sv);\n", 0);
  765. break;
  766. case T_DOUBLE :
  767. case T_FLOAT :
  768. Printv(setf->code, tab4, name, " = (", SwigType_str(t,0), ") SvNV(sv);\n", 0);
  769. break;
  770. case T_CHAR :
  771. Printv(setf->code, tab4, name, " = (char) *SvPV(sv,PL_na);\n", 0);
  772. break;
  773. case T_USER:
  774. SwigType_add_pointer(t);
  775. Wrapper_add_local(setf,"_temp", "void *_temp");
  776. get_pointer(iname,(char*)"value",(char*)"sv",(char*)"_temp", t, setf->code, (char*)"return(1)");
  777. Printv(setf->code, tab4, name, " = *((", SwigType_str(t,0), ") _temp);\n", 0);
  778. SwigType_del_pointer(t);
  779. break;
  780. case T_STRING:
  781. Wrapper_add_local(setf,"_a","char *_a");
  782. Printf(setf->code," _a = (char *) SvPV(sv,PL_na);\n");
  783. if (CPlusPlus)
  784. Printv(setf->code,
  785. tab4, "if (", name, ") delete [] ", name, ";\n",
  786. tab4, name, " = new char[strlen(_a)+1];\n",
  787. 0);
  788. else
  789. Printv(setf->code,
  790. tab4, "if (", name, ") free((char*)", name, ");\n",
  791. tab4, name, " = (char *) malloc(strlen(_a)+1);\n",
  792. 0);
  793. Printv(setf->code,"strcpy((char*)", name, ",_a);\n", 0);
  794. break;
  795. case T_ARRAY:
  796. {
  797. SwigType *aop;
  798. SwigType *ta = Copy(t);
  799. aop = SwigType_pop(ta);
  800. if (SwigType_type(ta) == T_CHAR) {
  801. String *dim = SwigType_array_getdim(aop,0);
  802. if (dim && Len(dim)) {
  803. Printf(setf->code, "strncpy(%s,(char*) SvPV(sv,PL_na), %s);\n", name,dim);
  804. setable = 1;
  805. } else {
  806. setable = 0;
  807. }
  808. } else {
  809. setable = 0;
  810. }
  811. Delete(ta);
  812. Delete(aop);
  813. }
  814. break;
  815. case T_POINTER: case T_REFERENCE:
  816. Wrapper_add_local(setf,"_temp","void *_temp");
  817. get_pointer(iname,(char*)"value",(char*)"sv",(char*)"_temp", t, setf->code, (char*)"return(1)");
  818. Printv(setf->code,tab4, name, " = (", SwigType_str(t,0), ") _temp;\n", 0);
  819. break;
  820. default :
  821. Printf(stderr,"%s : Line %d. Unable to link with datatype %s (ignored).\n", input_file, line_number, SwigType_str(t,0));
  822. return;
  823. }
  824. }
  825. Printf(setf->code," return 1;\n}\n");
  826. Replace(setf->code,"$name",iname, DOH_REPLACE_ANY);
  827. Wrapper_print(setf,magic);
  828. }
  829. /* Now write a function to evaluate the variable */
  830. Printf(getf->def,"SWIGCLASS_STATIC int %s(SV *sv, MAGIC *mg) {\n", val_name);
  831. Printv(getf->code,
  832. tab4, "MAGIC_PPERL\n",
  833. tab4, "mg = mg;\n",
  834. 0);
  835. if ((tm = Swig_typemap_lookup((char*)"varout",t,(char*)"",name, (char*)"sv",0))) {
  836. Printf(getf->code,"%s\n", tm);
  837. } else if ((tm = Swig_typemap_lookup((char*)"out",t,(char*)"",name,(char*)"sv",0))) {
  838. Printf(getf->code,"%s\n", tm);
  839. } else {
  840. switch(SwigType_type(t)) {
  841. case T_INT : case T_BOOL: case T_UINT:
  842. case T_SHORT : case T_USHORT:
  843. case T_LONG : case T_ULONG:
  844. case T_UCHAR: case T_SCHAR:
  845. Printv(getf->code,tab4, "sv_setiv(sv, (IV) ", name, ");\n", 0);
  846. Printv(vinit, tab4, "sv_setiv(sv,(IV)", name, ");\n",0);
  847. break;
  848. case T_DOUBLE :
  849. case T_FLOAT :
  850. Printv(getf->code, tab4,"sv_setnv(sv, (double) ", name, ");\n", 0);
  851. Printv(vinit, tab4, "sv_setnv(sv,(double)", name, ");\n",0);
  852. break;
  853. case T_CHAR :
  854. Wrapper_add_local(getf,"_ptemp","char _ptemp[2]");
  855. Printv(getf->code,
  856. tab4, "_ptemp[0] = ", name, ";\n",
  857. tab4, "_ptemp[1] = 0;\n",
  858. tab4, "sv_setpv((SV*) sv, _ptemp);\n",
  859. 0);
  860. break;
  861. case T_USER:
  862. SwigType_add_pointer(t);
  863. Printv(getf->code,
  864. tab4, "rsv = SvRV(sv);\n",
  865. tab4, "sv_setiv(rsv,(IV) &", name, ");\n",
  866. 0);
  867. Wrapper_add_local(getf,"rsv","SV *rsv");
  868. Printv(vinit, tab4, "SWIG_MakePtr(sv, (void *) &", name, ",SWIGTYPE", SwigType_manglestr(t), ");\n",0);
  869. SwigType_del_pointer(t);
  870. break;
  871. case T_STRING:
  872. Printv(getf->code, tab4, "sv_setpv((SV*) sv, ", name, ");\n", 0);
  873. break;
  874. case T_ARRAY:
  875. {
  876. SwigType *aop;
  877. SwigType *ta = Copy(t);
  878. aop = SwigType_pop(ta);
  879. if (SwigType_type(ta) == T_CHAR) {
  880. Printv(getf->code, "sv_setpv((SV*)sv, ", name, ");\n", 0);
  881. Delete(ta);
  882. Delete(aop);
  883. break;
  884. }
  885. Delete(ta);
  886. Delete(aop);
  887. }
  888. /* No break here is intentional */
  889. case T_POINTER: case T_REFERENCE:
  890. Printv(getf->code,
  891. tab4, "rsv = SvRV(sv);\n",
  892. tab4, "sv_setiv(rsv,(IV) ", name, ");\n",
  893. 0);
  894. Wrapper_add_local(getf,"rsv","SV *rsv");
  895. Printv(vinit, tab4, "SWIG_MakePtr(sv,(void *) 1, SWIGTYPE", SwigType_manglestr(t), ");\n",0);
  896. break;
  897. default :
  898. break;
  899. }
  900. }
  901. Printf(getf->code," return 1;\n}\n");
  902. Replace(getf->code,"$name",iname, DOH_REPLACE_ANY);
  903. Wrapper_print(getf,magic);
  904. /* Now add symbol to the PERL interpreter */
  905. if ((Status & STAT_READONLY) || (!setable)) {
  906. Printv(vinit, tab4, "swig_create_magic(sv,\"", package, "::", iname, "\",MAGIC_CAST MAGIC_CLASS swig_magic_readonly, MAGIC_CAST MAGIC_CLASS ", val_name, ");\n",0);
  907. } else {
  908. Printv(vinit, tab4, "swig_create_magic(sv,\"", package, "::", iname, "\", MAGIC_CAST MAGIC_CLASS ", set_name, ", MAGIC_CAST MAGIC_CLASS ", val_name, ");\n",0);
  909. }
  910. /* If we're blessed, try to figure out what to do with the variable
  911. 1. If it's a Perl object of some sort, create a tied-hash
  912. around it.
  913. 2. Otherwise, just hack Perl's symbol table */
  914. if (blessed) {
  915. if (is_shadow(t)) {
  916. Printv(var_stubs,
  917. "\nmy %__", iname, "_hash;\n",
  918. "tie %__", iname, "_hash,\"", is_shadow(t), "\", $",
  919. package, "::", iname, ";\n",
  920. "$", iname, "= \\%__", iname, "_hash;\n",
  921. "bless $", iname, ", ", is_shadow(t), ";\n",
  922. 0);
  923. } else {
  924. Printv(var_stubs, "*", iname, " = *", package, "::", iname, ";\n", 0);
  925. }
  926. }
  927. if (export_all)
  928. Printf(exported,"$%s ", name);
  929. DelWrapper(setf);
  930. DelWrapper(getf);
  931. }
  932. /* -----------------------------------------------------------------------------
  933. * PERL5::declare_const()
  934. * ----------------------------------------------------------------------------- */
  935. /* Functions used to create constants */
  936. static const char *setiv = "#ifndef PERL_OBJECT\
  937. \n#define swig_setiv(a,b) _swig_setiv(a,b)\
  938. \nstatic void _swig_setiv(char *name, long value) { \
  939. \n#else\
  940. \n#define swig_setiv(a,b) _swig_setiv(pPerl,a,b)\
  941. \nstatic void _swig_setiv(CPerlObj *pPerl, char *name, long value) { \
  942. \n#endif\
  943. \n SV *sv; \
  944. \n sv = perl_get_sv(name,TRUE | 0x2);\
  945. \n sv_setiv(sv, (IV) value);\
  946. \n SvREADONLY_on(sv);\
  947. \n}\n";
  948. static const char *setnv = "#ifndef PERL_OBJECT\
  949. \n#define swig_setnv(a,b) _swig_setnv(a,b)\
  950. \nstatic void _swig_setnv(char *name, double value) { \
  951. \n#else\
  952. \n#define swig_setnv(a,b) _swig_setnv(pPerl,a,b)\
  953. \nstatic void _swig_setnv(CPerlObj *pPerl, char *name, double value) { \
  954. \n#endif\
  955. \n SV *sv; \
  956. \n sv = perl_get_sv(name,TRUE | 0x2);\
  957. \n sv_setnv(sv, value);\
  958. \n SvREADONLY_on(sv);\
  959. \n}\n";
  960. static const char *setpv = "#ifndef PERL_OBJECT\
  961. \n#define swig_setpv(a,b) _swig_setpv(a,b)\
  962. \nstatic void _swig_setpv(char *name, char *value) { \
  963. \n#else\
  964. \n#define swig_setpv(a,b) _swig_setpv(pPerl,a,b)\
  965. \nstatic void _swig_setpv(CPerlObj *pPerl, char *name, char *value) { \
  966. \n#endif\
  967. \n SV *sv; \
  968. \n sv = perl_get_sv(name,TRUE | 0x2);\
  969. \n sv_setpv(sv, value);\
  970. \n SvREADONLY_on(sv);\
  971. \n}\n";
  972. static const char *setrv = "#ifndef PERL_OBJECT\
  973. \n#define swig_setrv(a,b,c) _swig_setrv(a,b,c)\
  974. \nstatic void _swig_setrv(char *name, void *value, char *type) { \
  975. \n#else\
  976. \n#define swig_setrv(a,b,c) _swig_setrv(pPerl,a,b,c)\
  977. \nstatic void _swig_setrv(CPerlObj *pPerl, char *name, void *value, char *type) { \
  978. \n#endif\
  979. \n SV *sv; \
  980. \n sv = perl_get_sv(name,TRUE | 0x2);\
  981. \n sv_setref_pv(sv, type, value);\
  982. \n SvREADONLY_on(sv);\
  983. \n}\n";
  984. void
  985. PERL5::declare_const(char *name, char *iname, SwigType *type, char *value)
  986. {
  987. char *tm;
  988. static int have_int_func = 0;
  989. static int have_double_func = 0;
  990. static int have_char_func = 0;
  991. static int have_ref_func = 0;
  992. if ((tm = Swig_typemap_lookup((char*)"const",type,name,value,name,0))) {
  993. Printf(f_init,"%s\n",tm);
  994. } else {
  995. switch(SwigType_type(type)) {
  996. case T_INT: case T_UINT: case T_BOOL:
  997. case T_SHORT: case T_USHORT:
  998. case T_LONG: case T_ULONG:
  999. case T_SCHAR: case T_UCHAR:
  1000. if (!have_int_func) {
  1001. Printf(f_header,"%s\n",setiv);
  1002. have_int_func = 1;
  1003. }
  1004. Printv(vinit, tab4, "swig_setiv(\"", package, "::", iname, "\", (long) ", value, ");\n",0);
  1005. break;
  1006. case T_DOUBLE:
  1007. case T_FLOAT:
  1008. if (!have_double_func) {
  1009. Printf(f_header,"%s\n",setnv);
  1010. have_double_func = 1;
  1011. }
  1012. Printv(vinit, tab4, "swig_setnv(\"", package, "::", iname, "\", (double) (", value, "));\n",0);
  1013. break;
  1014. case T_CHAR :
  1015. if (!have_char_func) {
  1016. Printf(f_header,"%s\n",setpv);
  1017. have_char_func = 1;
  1018. }
  1019. Printf(vinit," swig_setpv(\"%s::%s\",\"%s\");\n", package, iname, value);
  1020. break;
  1021. case T_STRING:
  1022. if (!have_char_func) {
  1023. Printf(f_header,"%s\n",setpv);
  1024. have_char_func = 1;
  1025. }
  1026. Printf(vinit," swig_setpv(\"%s::%s\",\"%s\");\n", package, iname, value);
  1027. break;
  1028. case T_POINTER: case T_ARRAY: case T_REFERENCE:
  1029. if (!have_ref_func) {
  1030. Printf(f_header,"%s\n",setrv);
  1031. have_ref_func = 1;
  1032. }
  1033. Printv(vinit, tab4, "swig_setrv(\"", package, "::", iname, "\", (void *) ", value, ", \"",
  1034. SwigType_manglestr(type), "\");\n", 0);
  1035. break;
  1036. default:
  1037. Printf(stderr,"%s : Line %d. Unsupported constant value.\n", input_file, line_number);
  1038. break;
  1039. }
  1040. }
  1041. if (blessed) {
  1042. if (is_shadow(type)) {
  1043. Printv(var_stubs,
  1044. "\nmy %__", iname, "_hash;\n",
  1045. "tie %__", iname, "_hash,\"", is_shadow(type), "\", $",
  1046. package, "::", iname, ";\n",
  1047. "$", iname, "= \\%__", iname, "_hash;\n",
  1048. "bless $", iname, ", ", is_shadow(type), ";\n",
  1049. 0);
  1050. } else if (do_constants) {
  1051. Printv(const_stubs,"sub ", name, " { $",
  1052. package, "::", name, " }\n", 0);
  1053. num_consts++;
  1054. } else {
  1055. Printv(var_stubs, "*",iname," = *", package, "::", iname, ";\n", 0);
  1056. }
  1057. }
  1058. if (export_all) {
  1059. if (do_constants && !is_shadow(type)) {
  1060. Printf(exported,"%s ",name);
  1061. } else {
  1062. Printf(exported,"$%s ",iname);
  1063. }
  1064. }
  1065. }
  1066. /* -----------------------------------------------------------------------------
  1067. * PERL5::usage_func()
  1068. * ----------------------------------------------------------------------------- */
  1069. char *
  1070. PERL5::usage_func(char *iname, SwigType *, ParmList *l) {
  1071. static String *temp = 0;
  1072. Parm *p;
  1073. int i;
  1074. if (!temp) temp = NewString("");
  1075. Clear(temp);
  1076. Printf(temp,"%s(",iname);
  1077. /* Now go through and print parameters */
  1078. p = l;
  1079. i = 0;
  1080. while (p != 0) {
  1081. SwigType *pt = Gettype(p);
  1082. String *pn = Getname(p);
  1083. if (!Getignore(p)) {
  1084. /* If parameter has been named, use that. Otherwise, just print a type */
  1085. if (SwigType_type(pt) != T_VOID) {
  1086. if (Len(pn) > 0) {
  1087. Printf(temp,"%s",pn);
  1088. } else {
  1089. Printf(temp,"%s",SwigType_str(pt,0));
  1090. }
  1091. }
  1092. i++;
  1093. p = Getnext(p);
  1094. if (p)
  1095. if (!Getignore(p))
  1096. Putc(',',temp);
  1097. } else {
  1098. p = Getnext(p);
  1099. if (p)
  1100. if ((i>0) && (!Getignore(p)))
  1101. Putc(',',temp);
  1102. }
  1103. }
  1104. Printf(temp,");");
  1105. return Char(temp);
  1106. }
  1107. /* -----------------------------------------------------------------------------
  1108. * PERL5::add_native()
  1109. * ----------------------------------------------------------------------------- */
  1110. void
  1111. PERL5::add_native(char *name, char *funcname, SwigType *, ParmList *) {
  1112. Printf(f_init,"\t newXS((char *) \"%s::%s\", %s, file);\n", package,name, funcname);
  1113. if (export_all)
  1114. Printf(exported,"%s ",name);
  1115. if (blessed) {
  1116. Printv(func_stubs,"*", name, " = *", package, "::", name, ";\n", 0);
  1117. }
  1118. }
  1119. /****************************************************************************
  1120. *** OBJECT-ORIENTED FEATURES
  1121. ****************************************************************************
  1122. *** These extensions provide a more object-oriented interface to C++
  1123. *** classes and structures. The code here is based on extensions
  1124. *** provided by David Fletcher and Gary Holt.
  1125. ***
  1126. *** I have generalized these extensions to make them more general purpose
  1127. *** and to resolve object-ownership problems.
  1128. ***
  1129. *** The approach here is very similar to the Python module :
  1130. *** 1. All of the original methods are placed into a single
  1131. *** package like before except that a 'c' is appended to the
  1132. *** package name.
  1133. ***
  1134. *** 2. All methods and function calls are wrapped with a new
  1135. *** perl function. While possibly inefficient this allows
  1136. *** us to catch complex function arguments (which are hard to
  1137. *** track otherwise).
  1138. ***
  1139. *** 3. Classes are represented as tied-hashes in a manner similar
  1140. *** to Gary Holt's extension. This allows us to access
  1141. *** member data.
  1142. ***
  1143. *** 4. Stand-alone (global) C functions are modified to take
  1144. *** tied hashes as arguments for complex datatypes (if
  1145. *** appropriate).
  1146. ***
  1147. *** 5. Global variables involving a class/struct is encapsulated
  1148. *** in a tied hash.
  1149. ***
  1150. *** 6. Object ownership is maintained by having a hash table
  1151. *** within in each package called "this". It is unlikely
  1152. *** that C++ program will use this so it's a somewhat
  1153. *** safe variable name.
  1154. ***
  1155. ****************************************************************************/
  1156. /* -----------------------------------------------------------------------------
  1157. * PERL5::cpp_open_class()
  1158. * ----------------------------------------------------------------------------- */
  1159. void
  1160. PERL5::cpp_open_class(char *classname, char *rname, char *ctype, int strip) {
  1161. this->Language::cpp_open_class(classname, rname, ctype, strip);
  1162. if (blessed) {
  1163. have_constructor = 0;
  1164. have_destructor = 0;
  1165. have_data_members = 0;
  1166. Delete(class_name); class_name = 0;
  1167. Delete(class_type); class_type =0;
  1168. Delete(real_classname); real_classname = 0;
  1169. Delete(base_class); base_class = 0;
  1170. Delete(fullclassname); fullclassname = 0;
  1171. /* If the class is being renamed to something else, use the renaming */
  1172. if (rname) {
  1173. class_name = NewString(rname);
  1174. class_renamed = 1;
  1175. } else {
  1176. class_name = NewString(classname);
  1177. class_renamed = 0;
  1178. }
  1179. /* Use the fully qualified name of the Perl class */
  1180. if (!compat) {
  1181. fullclassname = NewStringf("%s::%s",realpackage,class_name);
  1182. } else {
  1183. fullclassname = NewString(class_name);
  1184. }
  1185. real_classname = NewString(classname);
  1186. if (base_class) Delete(base_class);
  1187. base_class = 0;
  1188. class_type = NewString(ctype);
  1189. pcode = NewString("");
  1190. blessedmembers = NewString("");
  1191. member_keys = NewString("");
  1192. /* Add some symbols to the hash tables */
  1193. cpp_class_decl(Char(classname),Char(class_name),Char(ctype));
  1194. }
  1195. }
  1196. /* -----------------------------------------------------------------------------
  1197. * PERL5::cpp_close_class()
  1198. * ----------------------------------------------------------------------------- */
  1199. void
  1200. PERL5::cpp_close_class() {
  1201. if (blessed) {
  1202. Printv(pm,
  1203. "\n############# Class : ", fullclassname, " ##############\n",
  1204. "\npackage ", fullclassname, ";\n",
  1205. 0);
  1206. /* If we are inheriting from a base class, set that up */
  1207. Printv(pm, "@ISA = qw( ",realpackage, 0);
  1208. /* Removed to fix SF Bug #449398
  1209. if (Cmp(class_name,realpackage))
  1210. Printv(pm, "@ISA = qw( ",realpackage, 0);
  1211. else
  1212. Printv(pm, "@ISA = qw( ", 0);
  1213. */
  1214. if (base_class) {
  1215. Printv(pm, " ", base_class, 0);
  1216. }
  1217. Printf(pm, " );\n");
  1218. /* Dump out a hash table containing the pointers that we own */
  1219. Printf(pm, "%%OWNER = ();\n");
  1220. if (have_data_members) {
  1221. Printv(pm,
  1222. "%BLESSEDMEMBERS = (\n", blessedmembers, ");\n\n",
  1223. 0);
  1224. }
  1225. if (have_data_members || have_destructor)
  1226. Printf(pm, "%%ITERATORS = ();\n");
  1227. /* Dump out the package methods */
  1228. Printv(pm,pcode,0);
  1229. Delete(pcode);
  1230. /* Output methods for managing ownership */
  1231. Printv(pm,
  1232. "sub DISOWN {\n",
  1233. tab4, "my $self = shift;\n",
  1234. tab4, "my $ptr = tied(%$self);\n",
  1235. tab4, "delete $OWNER{$ptr};\n",
  1236. tab4, "};\n\n",
  1237. "sub ACQUIRE {\n",
  1238. tab4, "my $self = shift;\n",
  1239. tab4, "my $ptr = tied(%$self);\n",
  1240. tab4, "$OWNER{$ptr} = 1;\n",
  1241. tab4, "};\n\n",
  1242. 0);
  1243. /* Only output the following methods if a class has member data */
  1244. if (have_data_members) {
  1245. /* Output a FETCH method. This is actually common to all classes */
  1246. Printv(pm,
  1247. "sub FETCH {\n",
  1248. tab4, "my ($self,$field) = @_;\n",
  1249. tab4, "my $member_func = \"swig_${field}_get\";\n",
  1250. tab4, "my $val = $self->$member_func();\n",
  1251. tab4, "if (exists $BLESSEDMEMBERS{$field}) {\n",
  1252. tab8, "return undef if (!defined($val));\n",
  1253. tab8, "my %retval;\n",
  1254. tab8, "tie %retval,$BLESSEDMEMBERS{$field},$val;\n",
  1255. tab8, "return bless \\%retval, $BLESSEDMEMBERS{$field};\n",
  1256. tab4, "}\n",
  1257. tab4, "return $val;\n",
  1258. "}\n\n",
  1259. 0);
  1260. /* Output a STORE method. This is also common to all classes (might move to base class) */
  1261. Printv(pm,
  1262. "sub STORE {\n",
  1263. tab4, "my ($self,$field,$newval) = @_;\n",
  1264. tab4, "my $member_func = \"swig_${field}_set\";\n",
  1265. tab4, "if (exists $BLESSEDMEMBERS{$field}) {\n",
  1266. tab8, "$self->$member_func(tied(%{$newval}));\n",
  1267. tab4, "} else {\n",
  1268. tab8, "$self->$member_func($newval);\n",
  1269. tab4, "}\n",
  1270. "}\n\n",
  1271. 0);
  1272. #ifdef OLD
  1273. /* Output a FIRSTKEY method. This is to allow iteration over a structure's keys. */
  1274. Printv(pm,
  1275. "sub FIRSTKEY {\n",
  1276. tab4, "my $self = shift;\n",
  1277. tab4, "$ITERATORS{$self} = [", member_keys, "];\n",
  1278. tab4, "my $first = shift @{$ITERATORS{$self}};\n",
  1279. tab4, "return $first;\n",
  1280. "}\n\n",
  1281. 0);
  1282. /* Output a NEXTKEY method. This is the iterator so that each and keys works */
  1283. Printv(pm,
  1284. "sub NEXTKEY {\n",
  1285. tab4, "my $self = shift;\n",
  1286. tab4, "$nelem = scalar @{$ITERATORS{$self}};\n",
  1287. tab4, "if ($nelem > 0) {\n",
  1288. tab8, "my $member = shift @{$ITERATORS{$self}};\n",
  1289. tab8, "return $member;\n",
  1290. tab4, "} else {\n",
  1291. tab8, "$ITERATORS{$self} = [", member_keys, "];\n",
  1292. tab8, "return ();\n",
  1293. tab4, "}\n",
  1294. "}\n\n",
  1295. 0);
  1296. #endif
  1297. }
  1298. }
  1299. }
  1300. /* -----------------------------------------------------------------------------
  1301. * PERL5::cpp_member_func()
  1302. * ----------------------------------------------------------------------------- */
  1303. void
  1304. PERL5::cpp_member_func(char *name, char *iname, SwigType *t, ParmList *l) {
  1305. String *func;
  1306. char *realname;
  1307. Parm *p;
  1308. int i;
  1309. String *cname;
  1310. int pcount, numopt;
  1311. char *tm;
  1312. int need_wrapper = 0;
  1313. member_func = 1;
  1314. this->Language::cpp_member_func(name,iname,t,l);
  1315. member_func = 0;
  1316. if (blessed) {
  1317. func = NewString("");
  1318. cname = NewString("perl5:");
  1319. /* Now emit a Perl wrapper function around our member function, we might need
  1320. to patch up some arguments along the way */
  1321. if (!iname)
  1322. realname = name;
  1323. else
  1324. realname = iname;
  1325. Printf(cname,"%s::%s",class_name,realname);
  1326. Printv(func,
  1327. "sub ", realname, " {\n",
  1328. tab4, "my @args = @_;\n",
  1329. 0);
  1330. /* Now we have to go through and patch up the argument list. If any
  1331. arguments to our function correspond to other Perl objects, we
  1332. need to extract them from a tied-hash table object. */
  1333. p = l;
  1334. pcount = ParmList_len(l);
  1335. numopt = check_numopt(l);
  1336. i = 1;
  1337. while(p) {
  1338. SwigType *pt = Gettype(p);
  1339. if (!Getignore(p)) {
  1340. char sourceNtarget[512];
  1341. sprintf(sourceNtarget, "$args[%d]", i);
  1342. if ((tm = Swig_typemap_lookup((char*)"perl5in",pt,(char*)"",sourceNtarget,sourceNtarget,0))) {
  1343. Printf(func,"%s\n",tm);
  1344. need_wrapper = 1;
  1345. }
  1346. i++;
  1347. }
  1348. p = Getnext(p);
  1349. }
  1350. /* Okay. We've made argument adjustments, now call into the package */
  1351. Printv(func,
  1352. tab4, "my $result = ", package, "::", Swig_name_member(class_name,realname),
  1353. "(@args);\n",
  1354. 0);
  1355. /* Now check to see what kind of return result was found.
  1356. * If this function is returning a result by 'value', SWIG did an
  1357. * implicit malloc/new. We'll mark the object like it was created
  1358. * in Perl so we can garbage collect it. */
  1359. if ((tm = Swig_typemap_lookup((char*)"perl5out",t,(char*)"",name,(char*)"sv",0))) {
  1360. Printv(func,
  1361. tm, "\n",
  1362. tab4,"return $result;\n",
  1363. "}\n",
  1364. 0);
  1365. need_wrapper = 1;
  1366. } else if (is_shadow(t)) {
  1367. Printv(func,tab4, "return undef if (!defined($result));\n", 0);
  1368. /* If we're returning an object by value, put it's reference
  1369. into our local hash table */
  1370. if (!SwigType_ispointer(t) || NewObject) {
  1371. Printv(func, tab4, "$", is_shadow(t), "::OWNER{$result} = 1; \n", 0);
  1372. }
  1373. /* We're returning a Perl "object" of some kind. Turn it into
  1374. a tied hash */
  1375. Printv(func,
  1376. tab4, "my %resulthash;\n",
  1377. tab4, "tie %resulthash, ref($result), $result;\n",
  1378. tab4, "return bless \\%resulthash, ref($result);\n",
  1379. "}\n",
  1380. 0);
  1381. need_wrapper = 1;
  1382. } else {
  1383. /* Hmmm. This doesn't appear to be anything I know about so just
  1384. return it unmodified */
  1385. Printv(func, tab4,"return $result;\n", "}\n", 0);
  1386. }
  1387. if (need_wrapper) {
  1388. Printv(pcode,func,0);
  1389. } else {
  1390. Printv(pcode,"*",realname," = *", package, "::", Swig_name_member(class_name,realname), ";\n", 0);
  1391. }
  1392. Delete(func);
  1393. Delete(cname);
  1394. }
  1395. }
  1396. /* -----------------------------------------------------------------------------
  1397. * PERL5::cpp_variable()
  1398. *
  1399. * Adds an instance member. This is a little hairy because data members are
  1400. * really added with a tied-hash table that is attached to the object.
  1401. *
  1402. * On the low level, we will emit a pair of get/set functions to retrieve
  1403. * values just like before. These will then be encapsulated in a FETCH/STORE
  1404. * method associated with the tied-hash.
  1405. *
  1406. * In the event that a member is an object that we have already wrapped, then
  1407. * we need to retrieve the data a tied-hash as opposed to what SWIG normally
  1408. * returns. To determine this, we build an internal hash called 'BLESSEDMEMBERS'
  1409. * that contains the names and types of tied data members. If a member name
  1410. * is in the list, we tie it, otherwise, we just return the normal SWIG value.
  1411. * ----------------------------------------------------------------------------- */
  1412. void PERL5::cpp_variable(char *name, char *iname, SwigType *t) {
  1413. char *realname;
  1414. /* Emit a pair of get/set functions for the variable */
  1415. member_func = 1;
  1416. this->Language::cpp_variable(name, iname, t);
  1417. member_func = 0;
  1418. if (blessed) {
  1419. if (iname) realname = iname;
  1420. else realname = name;
  1421. /* Store name of key for future reference */
  1422. Printf(member_keys,"'%s', ", realname);
  1423. Printv(pcode,"*swig_", realname, "_get = *", package, "::", Swig_name_get(Swig_name_member(class_name,realname)), ";\n", 0);
  1424. Printv(pcode,"*swig_", realname, "_set = *", package, "::", Swig_name_set(Swig_name_member(class_name,realname)), ";\n", 0);
  1425. /* Now we need to generate a little Perl code for this */
  1426. if (is_shadow(t)) {
  1427. /* This is a Perl object that we have already seen. Add an
  1428. entry to the members list*/
  1429. Printv(blessedmembers,
  1430. tab4, realname, " => '", is_shadow(t), "',\n",
  1431. 0);
  1432. }
  1433. }
  1434. have_data_members++;
  1435. }
  1436. /* -----------------------------------------------------------------------------
  1437. * PERL5::cpp_constructor()
  1438. *
  1439. * Emits a blessed constructor for our class. In addition to our construct
  1440. * we manage a Perl hash table containing all of the pointers created by
  1441. * the constructor. This prevents us from accidentally trying to free
  1442. * something that wasn't necessarily allocated by malloc or new
  1443. * ----------------------------------------------------------------------------- */
  1444. void
  1445. PERL5::cpp_constructor(char *name, char *iname, ParmList *l) {
  1446. Parm *p;
  1447. int i;
  1448. String *realname;
  1449. /* Emit an old-style constructor for this class */
  1450. member_func = 1;
  1451. this->Language::cpp_constructor(name, iname, l);
  1452. if (blessed) {
  1453. if (iname)
  1454. realname = iname;
  1455. else {
  1456. if (class_renamed) realname = class_name;
  1457. else realname = class_name;
  1458. }
  1459. if ((Cmp(realname,class_name) == 0)) {
  1460. /* Emit a blessed constructor */
  1461. Printf(pcode, "sub new {\n");
  1462. } else {
  1463. /* Constructor doesn't match classname so we'll just use the normal name */
  1464. Printv(pcode, "sub ", Swig_name_construct(realname), " () {\n", 0);
  1465. }
  1466. Printv(pcode, tab4, "my $pkg = shift;\n",
  1467. tab4, "my @args = @_;\n", 0);
  1468. /* We are going to need to patch up arguments here if necessary
  1469. * Now we have to go through and patch up the argument list. If any
  1470. * arguments to our function correspond to other Perl objects, we
  1471. * need to extract them from a tied-hash table object. */
  1472. p = l;
  1473. i = 0;
  1474. while(p) {
  1475. SwigType *pt = Gettype(p);
  1476. if (is_shadow(pt)) {
  1477. /* Yep. This smells alot like an object, patch up the arguments */
  1478. Printf(pcode, " $args[%d] = tied(%%{$args[%d]});\n", i, i);
  1479. }
  1480. p = Getnext(p);
  1481. i++;
  1482. }
  1483. Printv(pcode,
  1484. tab4, "my $self = ", package, "::", Swig_name_construct(realname), "(@args);\n",
  1485. tab4, "return undef if (!defined($self));\n",
  1486. tab4, "bless $self, \"", fullclassname, "\";\n",
  1487. tab4, "$OWNER{$self} = 1;\n",
  1488. tab4, "my %retval;\n",
  1489. tab4, "tie %retval, \"", fullclassname, "\", $self;\n",
  1490. tab4, "return bless \\%retval, $pkg;\n",
  1491. "}\n\n",
  1492. 0);
  1493. have_constructor = 1;
  1494. }
  1495. member_func = 0;
  1496. }
  1497. /* -----------------------------------------------------------------------------
  1498. * PERL5::cpp_destructor()
  1499. * ----------------------------------------------------------------------------- */
  1500. void
  1501. PERL5::cpp_destructor(char *name, char *newname) {
  1502. String *realname;
  1503. member_func = 1;
  1504. this->Language::cpp_destructor(name, newname);
  1505. if (blessed) {
  1506. if (newname) realname = newname;
  1507. else {
  1508. if (class_renamed) realname = class_name;
  1509. else realname = name;
  1510. }
  1511. /* Emit a destructor for this object*/
  1512. Printv(pcode,
  1513. "sub DESTROY {\n",
  1514. tab4, "return unless $_[0]->isa('HASH');\n",
  1515. tab4, "my $self = tied(%{$_[0]});\n",
  1516. tab4, "delete $ITERATORS{$self};\n",
  1517. tab4, "if (exists $OWNER{$self}) {\n",
  1518. tab8, package, "::", Swig_name_destroy(realname), "($self);\n",
  1519. tab8, "delete $OWNER{$self};\n",
  1520. tab4, "}\n}\n\n",
  1521. 0);
  1522. have_destructor = 1;
  1523. }
  1524. member_func = 0;
  1525. }
  1526. /* -----------------------------------------------------------------------------
  1527. * PERL5::cpp_static_func()
  1528. * ----------------------------------------------------------------------------- */
  1529. void
  1530. PERL5::cpp_static_func(char *name, char *iname, SwigType *t, ParmList *l) {
  1531. this->Language::cpp_static_func(name,iname,t,l);
  1532. if (blessed) {
  1533. char *realname;
  1534. if (iname) realname = name;
  1535. else realname = iname;
  1536. Printv(pcode, "*", realname, " = *", package, "::", Swig_name_member(class_name,realname), ";\n", 0);
  1537. }
  1538. }
  1539. /* -----------------------------------------------------------------------------
  1540. * PERL5::cpp_static_var()
  1541. * ----------------------------------------------------------------------------- */
  1542. void
  1543. PERL5::cpp_static_var(char *name, char *iname, SwigType *t) {
  1544. this->Language::cpp_static_var(name,iname,t);
  1545. if (blessed) {
  1546. char *realname;
  1547. if (iname) realname = name;
  1548. else realname = iname;
  1549. Printv(pcode, "*", realname, " = *", package, "::", Swig_name_member(class_name,realname), ";\n", 0);
  1550. }
  1551. }
  1552. /* -----------------------------------------------------------------------------
  1553. * PERL5::cpp_inherit()
  1554. * ------------------------------------------------------------------------------ */
  1555. void
  1556. PERL5::cpp_inherit(char **baseclass, int) {
  1557. char *bc;
  1558. int i = 0, have_first = 0;
  1559. if (!blessed) {
  1560. this->Language::cpp_inherit(baseclass);
  1561. return;
  1562. }
  1563. /* Inherit variables and constants from base classes, but not
  1564. functions (since Perl can handle that okay). */
  1565. this->Language::cpp_inherit(baseclass, INHERIT_CONST | INHERIT_VAR);
  1566. /* Now tell the Perl5 module that we're inheriting from base classes */
  1567. base_class = NewString("");
  1568. while (baseclass[i]) {
  1569. /* See if this is a class we know about */
  1570. String *b = NewString(baseclass[i]);
  1571. bc = Char(is_shadow(b));
  1572. Delete(b);
  1573. if (bc) {
  1574. if (have_first) Putc(' ', base_class);
  1575. Printf(base_class,bc);
  1576. have_first = 1;
  1577. }
  1578. i++;
  1579. }
  1580. if (!have_first) {
  1581. Delete(base_class);
  1582. base_class = 0;
  1583. }
  1584. }
  1585. /* -----------------------------------------------------------------------------
  1586. * PERL5::cpp_declare_const()
  1587. * ----------------------------------------------------------------------------- */
  1588. void
  1589. PERL5::cpp_declare_const(char *name, char *iname, SwigType *type, char *value) {
  1590. String *realname;
  1591. int oldblessed = blessed;
  1592. /* Create a normal constant */
  1593. blessed = 0;
  1594. this->Language::cpp_declare_const(name, iname, type, value);
  1595. blessed = oldblessed;
  1596. if (blessed) {
  1597. if (!iname)
  1598. realname = name;
  1599. else
  1600. realname = iname;
  1601. /* Create a symbol table entry for it */
  1602. Printv(pcode, "*", realname, " = *", package, "::", Swig_name_member(class_name,realname), ";\n", 0);
  1603. }
  1604. }
  1605. /* -----------------------------------------------------------------------------
  1606. * PERL5::cpp_class_decl()
  1607. * ----------------------------------------------------------------------------- */
  1608. void
  1609. PERL5::cpp_class_decl(char *name, char *rename, char *type) {
  1610. String *stype;
  1611. String *fullname;
  1612. String *actualpackage;
  1613. actualpackage = import_file ? import_file : realpackage;
  1614. if (blessed) {
  1615. stype = NewString(name);
  1616. SwigType_add_pointer(stype);
  1617. if ((!compat) && (!strchr(rename,':'))) {
  1618. fullname = NewStringf("%s::%s",actualpackage,rename);
  1619. } else {
  1620. fullname = NewString(rename);
  1621. }
  1622. Setattr(classes,stype,fullname);
  1623. Delete(stype);
  1624. if (strlen(type) > 0) {
  1625. stype = NewStringf("%s %s",type,name);
  1626. SwigType_add_pointer(stype);
  1627. Setattr(classes,stype,fullname);
  1628. Delete(stype);
  1629. }
  1630. }
  1631. }
  1632. /* -----------------------------------------------------------------------------
  1633. * PERL5::add_typedef()
  1634. * ----------------------------------------------------------------------------- */
  1635. void
  1636. PERL5::add_typedef(SwigType *t, char *name) {
  1637. if (!blessed) return;
  1638. if (is_shadow(t)) {
  1639. cpp_class_decl(name,Char(is_shadow(t)), (char *) "");
  1640. }
  1641. }
  1642. /* -----------------------------------------------------------------------------
  1643. * PERL5::pragma()
  1644. *
  1645. * Pragma directive.
  1646. *
  1647. * %pragma(perl5) code="String" # Includes a string in the .pm file
  1648. * %pragma(perl5) include="file.pl" # Includes a file in the .pm file
  1649. * ----------------------------------------------------------------------------- */
  1650. void PERL5::pragma(char *lang, char *code, char *value) {
  1651. if (strcmp(lang,"perl5") == 0) {
  1652. if (strcmp(code,"code") == 0) {
  1653. /* Dump the value string into the .pm file */
  1654. if (value) {
  1655. Printf(pragma_include, "%s\n", value);
  1656. }
  1657. } else if (strcmp(code,"include") == 0) {
  1658. /* Include a file into the .pm file */
  1659. if (value) {
  1660. FILE *f = Swig_open(value);
  1661. if (!f) {
  1662. Printf(stderr,"%s : Line %d. Unable to locate file %s\n", input_file, line_number,value);
  1663. } else {
  1664. char buffer[4096];
  1665. while (fgets(buffer,4095,f)) {
  1666. Printf(pragma_include,"%s",buffer);
  1667. }
  1668. }
  1669. }
  1670. } else {
  1671. Printf(stderr,"%s : Line %d. Unrecognized pragma.\n", input_file,line_number);
  1672. }
  1673. }
  1674. }