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

/Source/Swig/cwrap.c

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