PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/talby-perl5-improvements/Source/Swig/cwrap.c

#
C | 1526 lines | 1067 code | 163 blank | 296 comment | 216 complexity | 689bd6c58a443309c59a55165d2c737f MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * This file is part of SWIG, which is licensed as a whole under version 3
  3. * (or any later version) of the GNU General Public License. Some additional
  4. * terms also apply to certain portions of SWIG. The full details of the SWIG
  5. * license and copyrights can be found in the LICENSE and COPYRIGHT files
  6. * included with the SWIG source code as distributed by the SWIG developers
  7. * and at http://www.swig.org/legal.html.
  8. *
  9. * cwrap.c
  10. *
  11. * This file defines a variety of wrapping rules for C/C++ handling including
  12. * the naming of local variables, calling conventions, and so forth.
  13. * ----------------------------------------------------------------------------- */
  14. char cvsroot_cwrap_c[] = "$Id: cwrap.c 12890 2012-01-01 03:52:06Z talby $";
  15. #include "swig.h"
  16. extern int cparse_cplusplus;
  17. static const char *cresult_variable_name = "result";
  18. static Parm *nonvoid_parms(Parm *p) {
  19. if (p) {
  20. SwigType *t = Getattr(p, "type");
  21. if (SwigType_type(t) == T_VOID)
  22. return 0;
  23. }
  24. return p;
  25. }
  26. /* -----------------------------------------------------------------------------
  27. * Swig_cresult_name_set()
  28. *
  29. * Change the name of the variable used to hold the return value from C/C++ wrapper functions
  30. * from the default "result".
  31. * ----------------------------------------------------------------------------- */
  32. void Swig_cresult_name_set(const char *new_name) {
  33. cresult_variable_name = new_name;
  34. }
  35. /* -----------------------------------------------------------------------------
  36. * Swig_cresult_name()
  37. *
  38. * Get the name of the variable used to hold the return value from C/C++ wrapper functions
  39. * ----------------------------------------------------------------------------- */
  40. const char *Swig_cresult_name(void) {
  41. return cresult_variable_name;
  42. }
  43. /* -----------------------------------------------------------------------------
  44. * Swig_cparm_name()
  45. *
  46. * Generates a name for the ith argument in an argument list
  47. * ----------------------------------------------------------------------------- */
  48. String *Swig_cparm_name(Parm *p, int i) {
  49. String *name = NewStringf("arg%d", i + 1);
  50. if (p) {
  51. Setattr(p, "lname", name);
  52. }
  53. return name;
  54. }
  55. /* -----------------------------------------------------------------------------
  56. * Swig_clocal()
  57. *
  58. * Creates a string that declares a C local variable type. Converts references
  59. * and user defined types to pointers.
  60. * ----------------------------------------------------------------------------- */
  61. static String *Swig_clocal(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr value) {
  62. String *decl;
  63. decl = NewStringEmpty();
  64. switch (SwigType_type(t)) {
  65. case T_REFERENCE:
  66. if (value) {
  67. String *lstrname = SwigType_lstr(t, name);
  68. String *lstr = SwigType_lstr(t, 0);
  69. Printf(decl, "%s = (%s) &%s_defvalue", lstrname, lstr, name);
  70. Delete(lstrname);
  71. Delete(lstr);
  72. } else {
  73. String *lstrname = SwigType_lstr(t, name);
  74. Printf(decl, "%s = 0", lstrname);
  75. Delete(lstrname);
  76. }
  77. break;
  78. case T_VOID:
  79. break;
  80. case T_VARARGS:
  81. Printf(decl, "void *%s = 0", name);
  82. break;
  83. default:
  84. if (value) {
  85. String *lcaststr = SwigType_lcaststr(t, value);
  86. String *lstr = SwigType_lstr(t, 0);
  87. String *lstrn = SwigType_lstr(t, name);
  88. Printf(decl, "%s = (%s) %s", lstrn, lstr, lcaststr);
  89. Delete(lcaststr);
  90. Delete(lstr);
  91. Delete(lstrn);
  92. } else {
  93. String *lstrname = SwigType_lstr(t, name);
  94. Append(decl, lstrname);
  95. Delete(lstrname);
  96. }
  97. }
  98. return decl;
  99. }
  100. /* -----------------------------------------------------------------------------
  101. * Swig_wrapped_var_convert()
  102. *
  103. * Converts a member variable for use in the get and set wrapper methods.
  104. * This function only converts user defined types to pointers.
  105. * ----------------------------------------------------------------------------- */
  106. String *Swig_wrapped_var_type(SwigType *t, int varcref) {
  107. SwigType *ty;
  108. if (!Strstr(t, "enum $unnamed")) {
  109. ty = Copy(t);
  110. } else {
  111. /* Change the type for unnamed enum instance variables */
  112. ty = NewString("int");
  113. }
  114. if (SwigType_isclass(t)) {
  115. if (varcref) {
  116. if (cparse_cplusplus) {
  117. if (!SwigType_isconst(ty))
  118. SwigType_add_qualifier(ty, "const");
  119. SwigType_add_reference(ty);
  120. } else {
  121. return Copy(ty);
  122. }
  123. } else {
  124. SwigType_add_pointer(ty);
  125. }
  126. }
  127. return ty;
  128. }
  129. String *Swig_wrapped_member_var_type(SwigType *t, int varcref) {
  130. SwigType *ty;
  131. if (!Strstr(t, "enum $unnamed")) {
  132. ty = Copy(t);
  133. } else {
  134. /* Change the type for unnamed enum instance variables */
  135. ty = NewString("int");
  136. }
  137. if (SwigType_isclass(t)) {
  138. if (varcref) {
  139. if (cparse_cplusplus) {
  140. if (!SwigType_isconst(ty))
  141. SwigType_add_qualifier(ty, "const");
  142. SwigType_add_reference(ty);
  143. } else {
  144. return Copy(ty);
  145. }
  146. } else {
  147. SwigType_add_pointer(ty);
  148. }
  149. }
  150. return ty;
  151. }
  152. static String *Swig_wrapped_var_deref(SwigType *t, const_String_or_char_ptr name, int varcref) {
  153. if (SwigType_isclass(t)) {
  154. if (varcref) {
  155. if (cparse_cplusplus) {
  156. return NewStringf("*%s", name);
  157. } else {
  158. return NewStringf("%s", name);
  159. }
  160. } else {
  161. return NewStringf("*%s", name);
  162. }
  163. } else {
  164. return SwigType_rcaststr(t, name);
  165. }
  166. }
  167. static String *Swig_wrapped_var_assign(SwigType *t, const_String_or_char_ptr name, int varcref) {
  168. if (SwigType_isclass(t)) {
  169. if (varcref) {
  170. return NewStringf("%s", name);
  171. } else {
  172. return NewStringf("&%s", name);
  173. }
  174. } else {
  175. return SwigType_lcaststr(t, name);
  176. }
  177. }
  178. /* -----------------------------------------------------------------------------
  179. * Swig_cargs()
  180. *
  181. * Emit all of the local variables for a list of parameters. Returns the
  182. * number of parameters.
  183. * Default values for the local variables are only emitted if the compact default
  184. * argument behaviour is required.
  185. * ----------------------------------------------------------------------------- */
  186. int Swig_cargs(Wrapper *w, ParmList *p) {
  187. int i = 0;
  188. int compactdefargs = ParmList_is_compactdefargs(p);
  189. while (p != 0) {
  190. String *lname = Swig_cparm_name(p, i);
  191. SwigType *pt = Getattr(p, "type");
  192. if ((SwigType_type(pt) != T_VOID)) {
  193. String *local = 0;
  194. String *type = Getattr(p, "type");
  195. /* default values only emitted if in compact default args mode */
  196. String *pvalue = (compactdefargs) ? Getattr(p, "value") : 0;
  197. /* When using compactdefaultargs, the code generated initialises a variable via a constructor call that accepts the
  198. * default value as a parameter. The default constructor is not called and therefore SwigValueWrapper is not needed. */
  199. SwigType *altty = pvalue ? 0 : SwigType_alttype(type, 0);
  200. int tycode = SwigType_type(type);
  201. if (tycode == T_REFERENCE) {
  202. if (pvalue) {
  203. SwigType *tvalue;
  204. String *defname, *defvalue, *rvalue, *qvalue;
  205. rvalue = SwigType_typedef_resolve_all(pvalue);
  206. qvalue = SwigType_typedef_qualified(rvalue);
  207. defname = NewStringf("%s_defvalue", lname);
  208. tvalue = Copy(type);
  209. SwigType_del_reference(tvalue);
  210. tycode = SwigType_type(tvalue);
  211. if (tycode != T_USER) {
  212. /* plain primitive type, we copy the def value */
  213. String *lstr = SwigType_lstr(tvalue, defname);
  214. defvalue = NewStringf("%s = %s", lstr, qvalue);
  215. Delete(lstr);
  216. } else {
  217. /* user type, we copy the reference value */
  218. String *str = SwigType_str(type, defname);
  219. defvalue = NewStringf("%s = %s", str, qvalue);
  220. Delete(str);
  221. }
  222. Wrapper_add_localv(w, defname, defvalue, NIL);
  223. Delete(tvalue);
  224. Delete(rvalue);
  225. Delete(qvalue);
  226. Delete(defname);
  227. Delete(defvalue);
  228. }
  229. } else if (!pvalue && ((tycode == T_POINTER) || (tycode == T_STRING))) {
  230. pvalue = (String *) "0";
  231. }
  232. if (!altty) {
  233. local = Swig_clocal(pt, lname, pvalue);
  234. } else {
  235. local = Swig_clocal(altty, lname, pvalue);
  236. Delete(altty);
  237. }
  238. Wrapper_add_localv(w, lname, local, NIL);
  239. Delete(local);
  240. i++;
  241. }
  242. Delete(lname);
  243. p = nextSibling(p);
  244. }
  245. return (i);
  246. }
  247. /* -----------------------------------------------------------------------------
  248. * Swig_cresult()
  249. *
  250. * This function generates the C code needed to set the result of a C
  251. * function call.
  252. * ----------------------------------------------------------------------------- */
  253. String *Swig_cresult(SwigType *t, const_String_or_char_ptr name, const_String_or_char_ptr decl) {
  254. String *fcall;
  255. fcall = NewStringEmpty();
  256. switch (SwigType_type(t)) {
  257. case T_VOID:
  258. break;
  259. case T_REFERENCE:
  260. {
  261. String *lstr = SwigType_lstr(t, 0);
  262. Printf(fcall, "%s = (%s) &", name, lstr);
  263. Delete(lstr);
  264. }
  265. break;
  266. case T_USER:
  267. Printf(fcall, "%s = ", name);
  268. break;
  269. default:
  270. /* Normal return value */
  271. {
  272. String *lstr = SwigType_lstr(t, 0);
  273. Printf(fcall, "%s = (%s)", name, lstr);
  274. Delete(lstr);
  275. }
  276. break;
  277. }
  278. /* Now print out function call */
  279. Append(fcall, decl);
  280. /* A sick hack */
  281. {
  282. char *c = Char(decl) + Len(decl) - 1;
  283. if (!((*c == ';') || (*c == '}')))
  284. Append(fcall, ";");
  285. }
  286. return fcall;
  287. }
  288. /* -----------------------------------------------------------------------------
  289. * Swig_cfunction_call()
  290. *
  291. * Creates a string that calls a C function using the local variable rules
  292. * defined above.
  293. *
  294. * name(arg0, arg1, arg2, ... argn)
  295. *
  296. * ----------------------------------------------------------------------------- */
  297. String *Swig_cfunction_call(const_String_or_char_ptr name, ParmList *parms) {
  298. String *func;
  299. int i = 0;
  300. int comma = 0;
  301. Parm *p = parms;
  302. String *nname;
  303. func = NewStringEmpty();
  304. nname = SwigType_namestr(name);
  305. /*
  306. SWIGTEMPLATEDISAMBIGUATOR is compiler dependent (swiglabels.swg),
  307. - SUN Studio 9 requires 'template',
  308. - gcc-3.4 forbids the use of 'template'.
  309. the rest seems not caring very much,
  310. */
  311. if (SwigType_istemplate(name)) {
  312. String *prefix = Swig_scopename_prefix(nname);
  313. if (!prefix || Len(prefix) == 0) {
  314. Printf(func, "%s(", nname);
  315. } else {
  316. String *last = Swig_scopename_last(nname);
  317. Printf(func, "%s::SWIGTEMPLATEDISAMBIGUATOR %s(", prefix, last);
  318. Delete(last);
  319. }
  320. Delete(prefix);
  321. } else {
  322. Printf(func, "%s(", nname);
  323. }
  324. Delete(nname);
  325. if (p && Getattr(p, "arg:classref")) {
  326. i++;
  327. p = nextSibling(p);
  328. }
  329. while (p) {
  330. SwigType *pt = Getattr(p, "type");
  331. if ((SwigType_type(pt) != T_VOID)) {
  332. SwigType *rpt = SwigType_typedef_resolve_all(pt);
  333. String *pname = Swig_cparm_name(p, i);
  334. String *rcaststr = SwigType_rcaststr(rpt, pname);
  335. if (comma) {
  336. Printv(func, ",", rcaststr, NIL);
  337. } else {
  338. Append(func, rcaststr);
  339. }
  340. Delete(rpt);
  341. Delete(pname);
  342. Delete(rcaststr);
  343. comma = 1;
  344. i++;
  345. }
  346. p = nextSibling(p);
  347. }
  348. Append(func, ")");
  349. return func;
  350. }
  351. /* -----------------------------------------------------------------------------
  352. * Swig_cmethod_call()
  353. *
  354. * Generates a string that calls a C++ method from a list of parameters.
  355. *
  356. * arg0->name(arg1, arg2, arg3, ..., argn)
  357. *
  358. * self is an argument that defines how to handle the first argument. Normally,
  359. * it should be set to "this->". With C++ proxy classes enabled, it could be
  360. * set to "(*this)->" or some similar sequence.
  361. * ----------------------------------------------------------------------------- */
  362. static String *Swig_cmethod_call(const_String_or_char_ptr name, ParmList *parms, const_String_or_char_ptr self, String *explicit_qualifier, SwigType *director_type) {
  363. String *func, *nname;
  364. int i = 0;
  365. Parm *p = parms;
  366. SwigType *pt;
  367. int comma = 0;
  368. func = NewStringEmpty();
  369. if (!p)
  370. return func;
  371. if (!self)
  372. self = (char *) "(this)->";
  373. Append(func, self);
  374. if (SwigType_istemplate(name) && (strncmp(Char(name), "operator ", 9) == 0)) {
  375. /* fix for template + operators and compilers like gcc 3.3.5 */
  376. String *tprefix = SwigType_templateprefix(name);
  377. nname = tprefix;
  378. } else {
  379. nname = SwigType_namestr(name);
  380. }
  381. if (director_type) {
  382. const char *pname = "darg";
  383. String *rcaststr = SwigType_rcaststr(director_type, pname);
  384. Replaceall(func, "this", rcaststr);
  385. Delete(rcaststr);
  386. } else {
  387. pt = Getattr(p, "type");
  388. /* If the method is invoked through a dereferenced pointer, we don't add any casts
  389. (needed for smart pointers). Otherwise, we cast to the appropriate type */
  390. if (Strstr(func, "*this")) {
  391. String *pname = Swig_cparm_name(p, 0);
  392. Replaceall(func, "this", pname);
  393. Delete(pname);
  394. } else {
  395. String *pname = Swig_cparm_name(p, 0);
  396. String *rcaststr = SwigType_rcaststr(pt, pname);
  397. Replaceall(func, "this", rcaststr);
  398. Delete(rcaststr);
  399. Delete(pname);
  400. }
  401. /*
  402. SWIGTEMPLATEDESIMBUAGATOR is compiler dependent (swiglabels.swg),
  403. - SUN Studio 9 requires 'template',
  404. - gcc-3.4 forbids the use of 'template' (correctly implementing the ISO C++ standard)
  405. the others don't seem to care,
  406. */
  407. if (SwigType_istemplate(name))
  408. Printf(func, "SWIGTEMPLATEDISAMBIGUATOR ");
  409. if (explicit_qualifier) {
  410. Printv(func, explicit_qualifier, "::", NIL);
  411. }
  412. }
  413. Printf(func, "%s(", nname);
  414. i++;
  415. p = nextSibling(p);
  416. while (p) {
  417. pt = Getattr(p, "type");
  418. if ((SwigType_type(pt) != T_VOID)) {
  419. String *pname = Swig_cparm_name(p, i);
  420. String *rcaststr = SwigType_rcaststr(pt, pname);
  421. if (comma)
  422. Append(func, ",");
  423. Append(func, rcaststr);
  424. Delete(rcaststr);
  425. Delete(pname);
  426. comma = 1;
  427. i++;
  428. }
  429. p = nextSibling(p);
  430. }
  431. Append(func, ")");
  432. Delete(nname);
  433. return func;
  434. }
  435. /* -----------------------------------------------------------------------------
  436. * Swig_cconstructor_call()
  437. *
  438. * Creates a string that calls a C constructor function.
  439. *
  440. * calloc(1,sizeof(name));
  441. * ----------------------------------------------------------------------------- */
  442. String *Swig_cconstructor_call(const_String_or_char_ptr name) {
  443. DOH *func;
  444. func = NewStringEmpty();
  445. Printf(func, "calloc(1, sizeof(%s))", name);
  446. return func;
  447. }
  448. /* -----------------------------------------------------------------------------
  449. * Swig_cppconstructor_call()
  450. *
  451. * Creates a string that calls a C function using the local variable rules
  452. * defined above.
  453. *
  454. * name(arg0, arg1, arg2, ... argn)
  455. *
  456. * ----------------------------------------------------------------------------- */
  457. String *Swig_cppconstructor_base_call(const_String_or_char_ptr name, ParmList *parms, int skip_self) {
  458. String *func;
  459. String *nname;
  460. int i = 0;
  461. int comma = 0;
  462. Parm *p = parms;
  463. SwigType *pt;
  464. if (skip_self) {
  465. if (p)
  466. p = nextSibling(p);
  467. i++;
  468. }
  469. nname = SwigType_namestr(name);
  470. func = NewStringEmpty();
  471. Printf(func, "new %s(", nname);
  472. while (p) {
  473. pt = Getattr(p, "type");
  474. if ((SwigType_type(pt) != T_VOID)) {
  475. String *rcaststr = 0;
  476. String *pname = 0;
  477. if (comma)
  478. Append(func, ",");
  479. if (!Getattr(p, "arg:byname")) {
  480. pname = Swig_cparm_name(p, i);
  481. i++;
  482. } else {
  483. pname = Getattr(p, "value");
  484. if (pname)
  485. pname = Copy(pname);
  486. else
  487. pname = Copy(Getattr(p, "name"));
  488. }
  489. rcaststr = SwigType_rcaststr(pt, pname);
  490. Append(func, rcaststr);
  491. Delete(rcaststr);
  492. comma = 1;
  493. Delete(pname);
  494. }
  495. p = nextSibling(p);
  496. }
  497. Append(func, ")");
  498. Delete(nname);
  499. return func;
  500. }
  501. String *Swig_cppconstructor_call(const_String_or_char_ptr name, ParmList *parms) {
  502. return Swig_cppconstructor_base_call(name, parms, 0);
  503. }
  504. String *Swig_cppconstructor_nodirector_call(const_String_or_char_ptr name, ParmList *parms) {
  505. return Swig_cppconstructor_base_call(name, parms, 1);
  506. }
  507. String *Swig_cppconstructor_director_call(const_String_or_char_ptr name, ParmList *parms) {
  508. return Swig_cppconstructor_base_call(name, parms, 0);
  509. }
  510. /* -----------------------------------------------------------------------------
  511. * recursive_flag_search()
  512. *
  513. * This function searches for the class attribute 'attr' in the class
  514. * 'n' or recursively in its bases.
  515. *
  516. * If you define SWIG_FAST_REC_SEARCH, the method will set the found
  517. * 'attr' in the target class 'n'. If not, the method will set the
  518. * 'noattr' one. This prevents of having to navigate the entire
  519. * hierarchy tree everytime, so, it is an O(1) method... or something
  520. * like that. However, it populates all the parsed classes with the
  521. * 'attr' and/or 'noattr' attributes.
  522. *
  523. * If you undefine the SWIG_FAST_REC_SEARCH no attribute will be set
  524. * while searching. This could be slower for large projects with very
  525. * large hierarchy trees... or maybe not. But it will be cleaner.
  526. *
  527. * Maybe later a swig option can be added to switch at runtime.
  528. *
  529. * ----------------------------------------------------------------------------- */
  530. /* #define SWIG_FAST_REC_SEARCH 1 */
  531. static String *recursive_flag_search(Node *n, const String *attr, const String *noattr) {
  532. String *f = 0;
  533. n = Swig_methodclass(n);
  534. if (GetFlag(n, noattr)) {
  535. return 0;
  536. }
  537. f = GetFlagAttr(n, attr);
  538. if (f) {
  539. return f;
  540. } else {
  541. List *bl = Getattr(n, "bases");
  542. if (bl) {
  543. Iterator bi;
  544. for (bi = First(bl); bi.item; bi = Next(bi)) {
  545. f = recursive_flag_search(bi.item, attr, noattr);
  546. if (f) {
  547. #ifdef SWIG_FAST_REC_SEARCH
  548. SetFlagAttr(n, attr, f);
  549. #endif
  550. return f;
  551. }
  552. }
  553. }
  554. }
  555. #ifdef SWIG_FAST_REC_SEARCH
  556. SetFlag(n, noattr);
  557. #endif
  558. return 0;
  559. }
  560. /* -----------------------------------------------------------------------------
  561. * Swig_unref_call()
  562. *
  563. * Find the "feature:unref" call, if any.
  564. * ----------------------------------------------------------------------------- */
  565. String *Swig_unref_call(Node *n) {
  566. String *unref = recursive_flag_search(n, "feature:unref", "feature:nounref");
  567. if (unref) {
  568. String *pname = Swig_cparm_name(0, 0);
  569. unref = NewString(unref);
  570. Replaceall(unref, "$this", pname);
  571. Replaceall(unref, "$self", pname);
  572. Delete(pname);
  573. }
  574. return unref;
  575. }
  576. /* -----------------------------------------------------------------------------
  577. * Swig_ref_call()
  578. *
  579. * Find the "feature:ref" call, if any.
  580. * ----------------------------------------------------------------------------- */
  581. String *Swig_ref_call(Node *n, const String *lname) {
  582. String *ref = recursive_flag_search(n, "feature:ref", "feature:noref");
  583. if (ref) {
  584. ref = NewString(ref);
  585. Replaceall(ref, "$this", lname);
  586. Replaceall(ref, "$self", lname);
  587. }
  588. return ref;
  589. }
  590. /* -----------------------------------------------------------------------------
  591. * Swig_cdestructor_call()
  592. *
  593. * Creates a string that calls a C destructor function.
  594. *
  595. * free((char *) arg0);
  596. * ----------------------------------------------------------------------------- */
  597. String *Swig_cdestructor_call(Node *n) {
  598. Node *cn = Swig_methodclass(n);
  599. String *unref = Swig_unref_call(cn);
  600. if (unref) {
  601. return unref;
  602. } else {
  603. String *pname = Swig_cparm_name(0, 0);
  604. String *call = NewStringf("free((char *) %s);", pname);
  605. Delete(pname);
  606. return call;
  607. }
  608. }
  609. /* -----------------------------------------------------------------------------
  610. * Swig_cppdestructor_call()
  611. *
  612. * Creates a string that calls a C destructor function.
  613. *
  614. * delete arg1;
  615. * ----------------------------------------------------------------------------- */
  616. String *Swig_cppdestructor_call(Node *n) {
  617. Node *cn = Swig_methodclass(n);
  618. String *unref = Swig_unref_call(cn);
  619. if (unref) {
  620. return unref;
  621. } else {
  622. String *pname = Swig_cparm_name(0, 0);
  623. String *call = NewStringf("delete %s;", pname);
  624. Delete(pname);
  625. return call;
  626. }
  627. }
  628. /* -----------------------------------------------------------------------------
  629. * Swig_cmemberset_call()
  630. *
  631. * Generates a string that sets the name of a member in a C++ class or C struct.
  632. *
  633. * arg0->name = arg1
  634. *
  635. * ----------------------------------------------------------------------------- */
  636. String *Swig_cmemberset_call(const_String_or_char_ptr name, SwigType *type, String *self, int varcref) {
  637. String *func;
  638. String *pname0 = Swig_cparm_name(0, 0);
  639. String *pname1 = Swig_cparm_name(0, 1);
  640. func = NewStringEmpty();
  641. if (!self)
  642. self = NewString("(this)->");
  643. else
  644. self = NewString(self);
  645. Replaceall(self, "this", pname0);
  646. if (SwigType_type(type) != T_ARRAY) {
  647. if (!Strstr(type, "enum $unnamed")) {
  648. String *dref = Swig_wrapped_var_deref(type, pname1, varcref);
  649. Printf(func, "if (%s) %s%s = %s", pname0, self, name, dref);
  650. Delete(dref);
  651. } else {
  652. Printf(func, "if (%s && sizeof(int) == sizeof(%s%s)) *(int*)(void*)&(%s%s) = %s", pname0, self, name, self, name, pname1);
  653. }
  654. }
  655. Delete(self);
  656. Delete(pname0);
  657. Delete(pname1);
  658. return (func);
  659. }
  660. /* -----------------------------------------------------------------------------
  661. * Swig_cmemberget_call()
  662. *
  663. * Generates a string that sets the name of a member in a C++ class or C struct.
  664. *
  665. * arg0->name
  666. *
  667. * ----------------------------------------------------------------------------- */
  668. String *Swig_cmemberget_call(const_String_or_char_ptr name, SwigType *t, String *self, int varcref) {
  669. String *func;
  670. String *call;
  671. String *pname0 = Swig_cparm_name(0, 0);
  672. if (!self)
  673. self = NewString("(this)->");
  674. else
  675. self = NewString(self);
  676. Replaceall(self, "this", pname0);
  677. func = NewStringEmpty();
  678. call = Swig_wrapped_var_assign(t, "", varcref);
  679. Printf(func, "%s (%s%s)", call, self, name);
  680. Delete(self);
  681. Delete(call);
  682. Delete(pname0);
  683. return func;
  684. }
  685. /* -----------------------------------------------------------------------------
  686. * extension_code()
  687. *
  688. * Generates an extension function (a function defined in %extend)
  689. *
  690. * return_type function_name(parms) code
  691. *
  692. * ----------------------------------------------------------------------------- */
  693. static String *extension_code(const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) {
  694. String *parms_str = cplusplus ? ParmList_str_defaultargs(parms) : ParmList_str(parms);
  695. String *sig = NewStringf("%s(%s)", function_name, parms_str);
  696. String *rt_sig = SwigType_str(return_type, sig);
  697. String *body = NewStringf("SWIGINTERN %s", rt_sig);
  698. Printv(body, code, "\n", NIL);
  699. if (self)
  700. Replaceall(body, "$self", self);
  701. Delete(parms_str);
  702. Delete(sig);
  703. Delete(rt_sig);
  704. return body;
  705. }
  706. /* -----------------------------------------------------------------------------
  707. * Swig_add_extension_code()
  708. *
  709. * Generates an extension function (a function defined in %extend) and
  710. * adds it to the "wrap:code" attribute of a node
  711. *
  712. * See also extension_code()
  713. *
  714. * ----------------------------------------------------------------------------- */
  715. int Swig_add_extension_code(Node *n, const String *function_name, ParmList *parms, SwigType *return_type, const String *code, int cplusplus, const String *self) {
  716. String *body;
  717. if (parms && Getattr(parms, "arg:classref")) parms = nextSibling(parms);
  718. body = extension_code(function_name, parms, return_type, code, cplusplus, self);
  719. Setattr(n, "wrap:code", body);
  720. Delete(body);
  721. return SWIG_OK;
  722. }
  723. /* -----------------------------------------------------------------------------
  724. * Swig_MethodToFunction(Node *n)
  725. *
  726. * Converts a C++ method node to a function accessor function.
  727. * ----------------------------------------------------------------------------- */
  728. int Swig_MethodToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int flags, SwigType *director_type, int is_director) {
  729. String *name;
  730. ParmList *parms;
  731. SwigType *type;
  732. Parm *p;
  733. String *self = 0;
  734. int is_smart_pointer_overload = 0;
  735. String *qualifier = Getattr(n, "qualifier");
  736. /* If smart pointer without const overload or mutable method, change self dereferencing */
  737. if (flags & CWRAP_SMART_POINTER) {
  738. if (flags & CWRAP_SMART_POINTER_OVERLOAD) {
  739. if (qualifier && strncmp(Char(qualifier), "q(const)", 8) == 0) {
  740. self = NewString("(*(this))->");
  741. is_smart_pointer_overload = 1;
  742. }
  743. else if (Cmp(Getattr(n, "storage"), "static") == 0) {
  744. String *cname = Getattr(n, "classname") ? Getattr(n, "classname") : classname;
  745. String *ctname = SwigType_namestr(cname);
  746. self = NewStringf("(*(%s const *)this)->", ctname);
  747. is_smart_pointer_overload = 1;
  748. Delete(ctname);
  749. }
  750. else {
  751. self = NewString("(*this)->");
  752. }
  753. } else {
  754. self = NewString("(*this)->");
  755. }
  756. }
  757. /* If node is a member template expansion, we don't allow added code */
  758. if (Getattr(n, "templatetype"))
  759. flags &= ~(CWRAP_EXTEND);
  760. name = Getattr(n, "name");
  761. parms = CopyParmList(nonvoid_parms(Getattr(n, "parms")));
  762. type = NewString(classname);
  763. if (qualifier) {
  764. SwigType_push(type, qualifier);
  765. }
  766. SwigType_add_pointer(type);
  767. p = NewParm(type, "self", n);
  768. Setattr(p, "self", "1");
  769. Setattr(p, "hidden","1");
  770. /*
  771. Disable the 'this' ownership in 'self' to manage inplace
  772. operations like:
  773. A& A::operator+=(int i) { ...; return *this;}
  774. Here the 'self' parameter ownership needs to be disabled since
  775. there could be two objects sharing the same 'this' pointer: the
  776. input and the result one. And worse, the pointer could be deleted
  777. in one of the objects (input), leaving the other (output) with
  778. just a seg. fault to happen.
  779. To avoid the previous problem, use
  780. %feature("self:disown") *::operator+=;
  781. %feature("new") *::operator+=;
  782. These two lines just transfer the ownership of the 'this' pointer
  783. from the input to the output wrapping object.
  784. This happens in python, but may also happen in other target
  785. languages.
  786. */
  787. if (GetFlag(n, "feature:self:disown")) {
  788. Setattr(p, "wrap:disown", "1");
  789. }
  790. set_nextSibling(p, parms);
  791. Delete(type);
  792. /* Generate action code for the access */
  793. if (!(flags & CWRAP_EXTEND)) {
  794. String *explicit_qualifier = 0;
  795. String *call = 0;
  796. String *cres = 0;
  797. String *explicitcall_name = 0;
  798. int pure_virtual = !(Cmp(Getattr(n, "storage"), "virtual")) && !(Cmp(Getattr(n, "value"), "0"));
  799. /* Call the explicit method rather than allow for a polymorphic call */
  800. if ((flags & CWRAP_DIRECTOR_TWO_CALLS) || (flags & CWRAP_DIRECTOR_ONE_CALL)) {
  801. String *access = Getattr(n, "access");
  802. if (access && (Cmp(access, "protected") == 0)) {
  803. /* If protected access (can only be if a director method) then call the extra public accessor method (language module must provide this) */
  804. String *explicit_qualifier_tmp = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
  805. explicitcall_name = NewStringf("%sSwigPublic", name);
  806. explicit_qualifier = NewStringf("SwigDirector_%s", explicit_qualifier_tmp);
  807. Delete(explicit_qualifier_tmp);
  808. } else {
  809. explicit_qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
  810. }
  811. }
  812. call = Swig_cmethod_call(explicitcall_name ? explicitcall_name : name, p, self, explicit_qualifier, director_type);
  813. cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call);
  814. if (pure_virtual && is_director && (flags & CWRAP_DIRECTOR_TWO_CALLS)) {
  815. String *qualifier = SwigType_namestr(Getattr(Getattr(parentNode(n), "typescope"), "qname"));
  816. Delete(cres);
  817. cres = NewStringf("Swig::DirectorPureVirtualException::raise(\"%s::%s\");", qualifier, name);
  818. Delete(qualifier);
  819. }
  820. if (flags & CWRAP_DIRECTOR_TWO_CALLS) {
  821. /* Create two method calls, one to call the explicit method, the other a normal polymorphic function call */
  822. String *cres_both_calls = NewStringf("");
  823. String *call_extra = Swig_cmethod_call(name, p, self, 0, director_type);
  824. String *cres_extra = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call_extra);
  825. Printv(cres_both_calls, "if (upcall) {\n", cres, "\n", "} else {", cres_extra, "\n}", NIL);
  826. Setattr(n, "wrap:action", cres_both_calls);
  827. Delete(cres_extra);
  828. Delete(call_extra);
  829. Delete(cres_both_calls);
  830. } else {
  831. Setattr(n, "wrap:action", cres);
  832. }
  833. Delete(explicitcall_name);
  834. Delete(call);
  835. Delete(cres);
  836. Delete(explicit_qualifier);
  837. } else {
  838. /* Methods with default arguments are wrapped with additional methods for each default argument,
  839. * however, only one extra %extend method is generated. */
  840. String *defaultargs = Getattr(n, "defaultargs");
  841. String *code = Getattr(n, "code");
  842. String *cname = Getattr(n, "classname") ? Getattr(n, "classname") : classname;
  843. String *membername = Swig_name_member(nspace, cname, name);
  844. String *mangled = Swig_name_mangle(membername);
  845. int is_smart_pointer = flags & CWRAP_SMART_POINTER;
  846. type = Getattr(n, "type");
  847. /* Check if the method is overloaded. If so, and it has code attached, we append an extra suffix
  848. to avoid a name-clash in the generated wrappers. This allows overloaded methods to be defined
  849. in C. */
  850. if (Getattr(n, "sym:overloaded") && code) {
  851. Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname"));
  852. }
  853. /* See if there is any code that we need to emit */
  854. if (!defaultargs && code && !is_smart_pointer) {
  855. Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self");
  856. }
  857. if (is_smart_pointer) {
  858. int i = 0;
  859. Parm *pp = p;
  860. String *func = NewStringf("%s(", mangled);
  861. String *cres;
  862. if (Cmp(Getattr(n, "storage"), "static") != 0) {
  863. String *pname = Swig_cparm_name(pp, i);
  864. String *ctname = SwigType_namestr(cname);
  865. String *fadd = 0;
  866. if (is_smart_pointer_overload) {
  867. String *nclassname = SwigType_namestr(classname);
  868. fadd = NewStringf("(%s const *)((%s const *)%s)->operator ->()", ctname, nclassname, pname);
  869. Delete(nclassname);
  870. }
  871. else {
  872. fadd = NewStringf("(%s*)(%s)->operator ->()", ctname, pname);
  873. }
  874. Append(func, fadd);
  875. Delete(ctname);
  876. Delete(fadd);
  877. Delete(pname);
  878. pp = nextSibling(pp);
  879. if (pp)
  880. Append(func, ",");
  881. } else {
  882. pp = nextSibling(pp);
  883. }
  884. ++i;
  885. while (pp) {
  886. SwigType *pt = Getattr(pp, "type");
  887. if ((SwigType_type(pt) != T_VOID)) {
  888. String *pname = Swig_cparm_name(pp, i++);
  889. String *rcaststr = SwigType_rcaststr(pt, pname);
  890. Append(func, rcaststr);
  891. Delete(rcaststr);
  892. Delete(pname);
  893. pp = nextSibling(pp);
  894. if (pp)
  895. Append(func, ",");
  896. }
  897. }
  898. Append(func, ")");
  899. cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), func);
  900. Setattr(n, "wrap:action", cres);
  901. Delete(cres);
  902. } else {
  903. String *call = Swig_cfunction_call(mangled, p);
  904. String *cres = Swig_cresult(Getattr(n, "type"), Swig_cresult_name(), call);
  905. Setattr(n, "wrap:action", cres);
  906. Delete(call);
  907. Delete(cres);
  908. }
  909. Delete(membername);
  910. Delete(mangled);
  911. }
  912. Setattr(n, "parms", p);
  913. Delete(p);
  914. Delete(self);
  915. Delete(parms);
  916. return SWIG_OK;
  917. }
  918. /* -----------------------------------------------------------------------------
  919. * Swig_methodclass()
  920. *
  921. * This function returns the class node for a given method or class.
  922. * ----------------------------------------------------------------------------- */
  923. Node *Swig_methodclass(Node *n) {
  924. Node *nodetype = nodeType(n);
  925. if (Cmp(nodetype, "class") == 0)
  926. return n;
  927. return GetFlag(n, "feature:extend") ? parentNode(parentNode(n)) : parentNode(n);
  928. }
  929. int Swig_directorclass(Node *n) {
  930. Node *classNode = Swig_methodclass(n);
  931. assert(classNode != 0);
  932. return (Getattr(classNode, "vtable") != 0);
  933. }
  934. Node *Swig_directormap(Node *module, String *type) {
  935. int is_void = !Cmp(type, "void");
  936. if (!is_void && module) {
  937. /* ?? follow the inheritance hierarchy? */
  938. String *base = SwigType_base(type);
  939. Node *directormap = Getattr(module, "wrap:directormap");
  940. if (directormap)
  941. return Getattr(directormap, base);
  942. }
  943. return 0;
  944. }
  945. /* -----------------------------------------------------------------------------
  946. * Swig_ConstructorToFunction()
  947. *
  948. * This function creates a C wrapper for a C constructor function.
  949. * ----------------------------------------------------------------------------- */
  950. int Swig_ConstructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, String *none_comparison, String *director_ctor, int cplus, int flags) {
  951. ParmList *parms;
  952. Parm *prefix_args;
  953. Parm *p;
  954. ParmList *directorparms;
  955. SwigType *type;
  956. int use_director;
  957. use_director = Swig_directorclass(n);
  958. parms = CopyParmList(nonvoid_parms(Getattr(n, "parms")));
  959. /* Prepend the list of prefix_args (if any) */
  960. prefix_args = Getattr(n, "director:prefix_args");
  961. if (prefix_args != NIL) {
  962. Parm *p2, *p3;
  963. directorparms = CopyParmList(prefix_args);
  964. for (p = directorparms; nextSibling(p); p = nextSibling(p));
  965. for (p2 = parms; p2; p2 = nextSibling(p2)) {
  966. p3 = CopyParm(p2);
  967. set_nextSibling(p, p3);
  968. Delete(p3);
  969. p = p3;
  970. }
  971. } else
  972. directorparms = parms;
  973. type = NewString(classname);
  974. SwigType_add_pointer(type);
  975. if (flags & CWRAP_EXTEND) {
  976. /* Constructors with default arguments are wrapped with additional constructor methods for each default argument,
  977. * however, only one extra %extend method is generated. */
  978. String *call;
  979. String *cres;
  980. String *defaultargs = Getattr(n, "defaultargs");
  981. String *code = Getattr(n, "code");
  982. String *membername = Swig_name_construct(nspace, classname);
  983. String *mangled = Swig_name_mangle(membername);
  984. /* Check if the constructor is overloaded. If so, and it has code attached, we append an extra suffix
  985. to avoid a name-clash in the generated wrappers. This allows overloaded constructors to be defined
  986. in C. */
  987. if (Getattr(n, "sym:overloaded") && code) {
  988. Append(mangled, Getattr(defaultargs ? defaultargs : n, "sym:overname"));
  989. }
  990. /* See if there is any code that we need to emit */
  991. if (!defaultargs && code) {
  992. Swig_add_extension_code(n, mangled, parms, type, code, cparse_cplusplus, "self");
  993. }
  994. call = Swig_cfunction_call(mangled, parms);
  995. cres = Swig_cresult(type, Swig_cresult_name(), call);
  996. Setattr(n, "wrap:action", cres);
  997. Delete(cres);
  998. Delete(call);
  999. Delete(membername);
  1000. Delete(mangled);
  1001. } else {
  1002. if (cplus) {
  1003. /* if a C++ director class exists, create it rather than the original class */
  1004. if (use_director) {
  1005. Node *parent = Swig_methodclass(n);
  1006. int abstract = Getattr(parent, "abstract") != 0;
  1007. String *name = Getattr(parent, "sym:name");
  1008. String *directorname = NewStringf("SwigDirector_%s", name);
  1009. String *action = NewStringEmpty();
  1010. String *tmp_none_comparison = Copy(none_comparison);
  1011. String *director_call;
  1012. String *nodirector_call;
  1013. Replaceall(tmp_none_comparison, "$arg", "arg1");
  1014. director_call = Swig_cppconstructor_director_call(directorname, directorparms);
  1015. nodirector_call = Swig_cppconstructor_nodirector_call(classname, parms);
  1016. if (abstract) {
  1017. /* whether or not the abstract class has been subclassed in python,
  1018. * create a director instance (there's no way to create a normal
  1019. * instance). if any of the pure virtual methods haven't been
  1020. * implemented in the target language, calls to those methods will
  1021. * generate Swig::DirectorPureVirtualException exceptions.
  1022. */
  1023. String *cres = Swig_cresult(type, Swig_cresult_name(), director_call);
  1024. Append(action, cres);
  1025. Delete(cres);
  1026. } else {
  1027. /* (scottm): The code for creating a new director is now a string
  1028. template that gets passed in via the director_ctor argument.
  1029. $comparison : an 'if' comparison from none_comparison
  1030. $director_new: Call new for director class
  1031. $nondirector_new: Call new for non-director class
  1032. */
  1033. String *cres;
  1034. Append(action, director_ctor);
  1035. Replaceall(action, "$comparison", tmp_none_comparison);
  1036. cres = Swig_cresult(type, Swig_cresult_name(), director_call);
  1037. Replaceall(action, "$director_new", cres);
  1038. Delete(cres);
  1039. cres = Swig_cresult(type, Swig_cresult_name(), nodirector_call);
  1040. Replaceall(action, "$nondirector_new", cres);
  1041. Delete(cres);
  1042. }
  1043. Setattr(n, "wrap:action", action);
  1044. Delete(tmp_none_comparison);
  1045. Delete(action);
  1046. Delete(directorname);
  1047. } else {
  1048. String *call;
  1049. if (parms && Getattr(parms, "arg:classref"))
  1050. call = Swig_cppconstructor_nodirector_call(classname, parms);
  1051. else
  1052. call = Swig_cppconstructor_call(classname, parms);
  1053. String *cres = Swig_cresult(type, Swig_cresult_name(), call);
  1054. Setattr(n, "wrap:action", cres);
  1055. Delete(cres);
  1056. Delete(call);
  1057. }
  1058. } else {
  1059. String *call = Swig_cconstructor_call(classname);
  1060. String *cres = Swig_cresult(type, Swig_cresult_name(), call);
  1061. Setattr(n, "wrap:action", cres);
  1062. Delete(cres);
  1063. Delete(call);
  1064. }
  1065. }
  1066. Setattr(n, "type", type);
  1067. Setattr(n, "parms", parms);
  1068. Delete(type);
  1069. if (directorparms != parms)
  1070. Delete(directorparms);
  1071. Delete(parms);
  1072. return SWIG_OK;
  1073. }
  1074. /* -----------------------------------------------------------------------------
  1075. * Swig_DestructorToFunction()
  1076. *
  1077. * This function creates a C wrapper for a destructor function.
  1078. * ----------------------------------------------------------------------------- */
  1079. int Swig_DestructorToFunction(Node *n, const_String_or_char_ptr nspace, String *classname, int cplus, int flags) {
  1080. SwigType *type;
  1081. Parm *p;
  1082. type = NewString(classname);
  1083. SwigType_add_pointer(type);
  1084. p = NewParm(type, "self", n);
  1085. Setattr(p, "self", "1");
  1086. Setattr(p, "hidden", "1");
  1087. Setattr(p, "wrap:disown", "1");
  1088. Delete(type);
  1089. type = NewString("void");
  1090. if (flags & CWRAP_EXTEND) {
  1091. String *cres;
  1092. String *call;
  1093. String *membername, *mangled, *code;
  1094. membername = Swig_name_destroy(nspace, classname);
  1095. mangled = Swig_name_mangle(membername);
  1096. code = Getattr(n, "code");
  1097. if (code) {
  1098. Swig_add_extension_code(n, mangled, p, type, code, cparse_cplusplus, "self");
  1099. }
  1100. call = Swig_cfunction_call(mangled, p);
  1101. cres = NewStringf("%s;", call);
  1102. Setattr(n, "wrap:action", cres);
  1103. Delete(membername);
  1104. Delete(mangled);
  1105. Delete(call);
  1106. Delete(cres);
  1107. } else {
  1108. if (cplus) {
  1109. String *call = Swig_cppdestructor_call(n);
  1110. String *cres = NewStringf("%s", call);
  1111. Setattr(n, "wrap:action", cres);
  1112. Delete(call);
  1113. Delete(cres);
  1114. } else {
  1115. String *call = Swig_cdestructor_call(n);
  1116. String *cres = NewStringf("%s", call);
  1117. Setattr(n, "wrap:action", cres);
  1118. Delete(call);
  1119. Delete(cres);
  1120. }
  1121. }
  1122. Setattr(n, "type", type);
  1123. Setattr(n, "parms", p);
  1124. Delete(type);
  1125. Delete(p);
  1126. return SWIG_OK;
  1127. }
  1128. /* -----------------------------------------------------------------------------
  1129. * Swig_MembersetToFunction()
  1130. *
  1131. * This function creates a C wrapper for setting a structure member.
  1132. * ----------------------------------------------------------------------------- */
  1133. int Swig_MembersetToFunction(Node *n, String *classname, int flags) {
  1134. String *name;
  1135. ParmList *parms;
  1136. Parm *p;
  1137. SwigType *t;
  1138. SwigType *ty;
  1139. SwigType *type;
  1140. SwigType *void_type = NewString("void");
  1141. String *self = 0;
  1142. int varcref = flags & CWRAP_NATURAL_VAR;
  1143. if (flags & CWRAP_SMART_POINTER) {
  1144. self = NewString("(*this)->");
  1145. }
  1146. if (flags & CWRAP_ALL_PROTECTED_ACCESS) {
  1147. self = NewStringf("darg->");
  1148. }
  1149. name = Getattr(n, "name");
  1150. type = Getattr(n, "type");
  1151. t = NewString(classname);
  1152. SwigType_add_pointer(t);
  1153. parms = NewParm(t, "self", n);
  1154. Setattr(parms, "self", "1");
  1155. Setattr(parms, "hidden","1");
  1156. Delete(t);
  1157. ty = Swig_wrapped_member_var_type(type, varcref);
  1158. p = NewParm(ty, name, n);
  1159. Setattr(parms, "hidden", "1");
  1160. set_nextSibling(parms, p);
  1161. /* If the type is a pointer or reference. We mark it with a special wrap:disown attribute */
  1162. if (SwigType_check_decl(type, "p.")) {
  1163. Setattr(p, "wrap:disown", "1");
  1164. }
  1165. Delete(p);
  1166. if (flags & CWRAP_EXTEND) {
  1167. String *call;
  1168. String *cres;
  1169. String *code = Getattr(n, "code");
  1170. String *sname = Swig_name_set(0, name);
  1171. String *membername = Swig_name_member(0, classname, sname);
  1172. String *mangled = Swig_name_mangle(membername);
  1173. if (code) {
  1174. /* I don't think this ever gets run - WSF */
  1175. Swig_add_extension_code(n, mangled, parms, void_type, code, cparse_cplusplus, "self");
  1176. }
  1177. call = Swig_cfunction_call(mangled, parms);
  1178. cres = NewStringf("%s;", call);
  1179. Setattr(n, "wrap:action", cres);
  1180. Delete(cres);
  1181. Delete(call);
  1182. Delete(mangled);
  1183. Delete(membername);
  1184. Delete(sname);
  1185. } else {
  1186. String *call = Swig_cmemberset_call(name, type, self, varcref);
  1187. String *cres = NewStringf("%s;", call);
  1188. Setattr(n, "wrap:action", cres);
  1189. Delete(call);
  1190. Delete(cres);
  1191. }
  1192. Setattr(n, "type", void_type);
  1193. Setattr(n, "parms", parms);
  1194. Delete(parms);
  1195. Delete(ty);
  1196. Delete(void_type);
  1197. Delete(self);
  1198. return SWIG_OK;
  1199. }
  1200. /* -----------------------------------------------------------------------------
  1201. * Swig_MembergetToFunction()
  1202. *
  1203. * This function creates a C wrapper for getting a structure member.
  1204. * ----------------------------------------------------------------------------- */
  1205. int Swig_MembergetToFunction(Node *n, String *classname, int flags) {
  1206. String *name;
  1207. ParmList *parms;
  1208. SwigType *t;
  1209. SwigType *ty;
  1210. SwigType *type;
  1211. String *self = 0;
  1212. int varcref = flags & CWRAP_NATURAL_VAR;
  1213. if (flags & CWRAP_SMART_POINTER) {
  1214. if (checkAttribute(n, "storage", "static")) {
  1215. Node *sn = Getattr(n, "cplus:staticbase");
  1216. String *base = Getattr(sn, "name");
  1217. self = NewStringf("%s::", base);
  1218. } else if (flags & CWRAP_SMART_POINTER_OVERLOAD) {
  1219. String *nclassname = SwigType_namestr(classname);
  1220. self = NewStringf("(*(%s const *)this)->", nclassname);
  1221. Delete(nclassname);
  1222. } else {
  1223. self = NewString("(*this)->");
  1224. }
  1225. }
  1226. if (flags & CWRAP_ALL_PROTECTED_ACCESS) {
  1227. self = NewStringf("darg->");
  1228. }
  1229. name = Getattr(n, "name");
  1230. type = Getattr(n, "type");
  1231. t = NewString(classname);
  1232. SwigType_add_pointer(t);
  1233. parms = NewParm(t, "self", n);
  1234. Setattr(parms, "self", "1");
  1235. Setattr(parms, "hidden","1");
  1236. Delete(t);
  1237. ty = Swig_wrapped_member_var_type(type, varcref);
  1238. if (flags & CWRAP_EXTEND) {
  1239. String *call;
  1240. String *cres;
  1241. String *code = Getattr(n, "code");
  1242. String *gname = Swig_name_get(0, name);
  1243. String *membername = Swig_name_member(0, classname, gname);
  1244. String *mangled = Swig_name_mangle(membername);
  1245. if (code) {
  1246. /* I don't think this ever gets run - WSF */
  1247. Swig_add_extension_code(n, mangled, parms, ty, code, cparse_cplusplus, "self");
  1248. }
  1249. call = Swig_cfunction_call(mangled, parms);
  1250. cres = Swig_cresult(ty, Swig_cresult_name(), call);
  1251. Setattr(n, "wrap:action", cres);
  1252. Delete(cres);
  1253. Delete(call);
  1254. Delete(mangled);
  1255. Delete(membername);
  1256. Delete(gname);
  1257. } else {
  1258. String *call = Swig_cmemberget_call(name, type, self, varcref);
  1259. String *cres = Swig_cresult(ty, Swig_cresult_name(), call);
  1260. Setattr(n, "wrap:action", cres);
  1261. Delete(call);
  1262. Delete(cres);
  1263. }
  1264. Setattr(n, "type", ty);
  1265. Setattr(n, "parms", parms);
  1266. Delete(parms);
  1267. Delete(ty);
  1268. return SWIG_OK;
  1269. }
  1270. /* -----------------------------------------------------------------------------
  1271. * Swig_VarsetToFunction()
  1272. *
  1273. * This function creates a C wrapper for setting a global variable or static member
  1274. * variable.
  1275. * ----------------------------------------------------------------------------- */
  1276. int Swig_VarsetToFunction(Node *n, int flags) {
  1277. String *name, *nname;
  1278. ParmList *parms;
  1279. SwigType *type, *ty;
  1280. int varcref = flags & CWRAP_NATURAL_VAR;
  1281. name = Getattr(n, "name");
  1282. type = Getattr(n, "type");
  1283. nname = SwigType_namestr(name);
  1284. ty = Swig_wrapped_var_type(type, varcref);
  1285. parms = NewParm(ty, name, n);
  1286. if (flags & CWRAP_EXTEND) {
  1287. String *sname = Swig_name_set(0, name);
  1288. String *mangled = Swig_name_mangle(sname);
  1289. String *call = Swig_cfunction_call(mangled, parms);
  1290. String *cres = NewStringf("%s;", call);
  1291. Setattr(n, "wrap:action", cres);
  1292. Delete(cres);
  1293. Delete(call);
  1294. Delete(mangled);
  1295. Delete(sname);
  1296. } else {
  1297. if (!Strstr(type, "enum $unnamed")) {
  1298. String *pname = Swig_cparm_name(0, 0);
  1299. String *dref = Swig_wrapped_var_deref(type, pname, varcref);
  1300. String *call = NewStringf("%s = %s;", nname, dref);
  1301. Setattr(n, "wrap:action", call);
  1302. Delete(call);
  1303. Delete(dref);
  1304. Delete(pname);
  1305. } else {
  1306. String *pname = Swig_cparm_name(0, 0);
  1307. String *call = NewStringf("if (sizeof(int) == sizeof(%s)) *(int*)(void*)&(%s) = %s;", nname, nname, pname);
  1308. Setattr(n, "wrap:action", call);
  1309. Delete(pname);
  1310. Delete(call);
  1311. }
  1312. }
  1313. Setattr(n, "type", "void");
  1314. Setattr(n, "parms", parms);
  1315. Delete(parms);
  1316. Delete(ty);
  1317. Delete(nname);
  1318. return SWIG_OK;
  1319. }
  1320. /* -----------------------------------------------------------------------------
  1321. * Swig_VargetToFunction()
  1322. *
  1323. * This function creates a C wrapper for getting a global variable or static member
  1324. * variable.
  1325. * ----------------------------------------------------------------------------- */
  1326. int Swig_VargetToFunction(Node *n, int flags) {
  1327. String *cres, *call;
  1328. String *name;
  1329. SwigType *type;
  1330. SwigType *ty = 0;
  1331. int varcref = flags & CWRAP_NATURAL_VAR;
  1332. name = Getattr(n, "name");
  1333. type = Getattr(n, "type");
  1334. ty = Swig_wrapped_var_type(type, varcref);
  1335. if (flags & CWRAP_EXTEND) {
  1336. String *sname = Swig_name_get(0, name);
  1337. String *mangled = Swig_name_mangle(sname);
  1338. call = Swig_cfunction_call(mangled, 0);
  1339. cres = Swig_cresult(ty, Swig_cresult_name(), call);
  1340. Setattr(n, "wrap:action", cres);
  1341. Delete(mangled);
  1342. Delete(sname);
  1343. } else {
  1344. String *nname = SwigType_namestr(name);
  1345. call = Swig_wrapped_var_assign(type, nname, varcref);
  1346. cres = Swig_cresult(ty, Swig_cresult_name(), call);
  1347. Setattr(n, "wrap:action", cres);
  1348. Delete(nname);
  1349. }
  1350. Setattr(n, "type", ty);
  1351. Delattr(n, "parms");
  1352. Delete(cres);
  1353. Delete(call);
  1354. Delete(ty);
  1355. return SWIG_OK;
  1356. }