/branches/gsoc2009-ashishs99/Source/Swig/parms.c

# · C · 244 lines · 148 code · 29 blank · 67 comment · 24 complexity · c96d3b491beebf4fe242abe2be6199f4 MD5 · raw file

  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. * parms.c
  10. *
  11. * Parameter list class.
  12. * ----------------------------------------------------------------------------- */
  13. char cvsroot_parms_c[] = "$Id: parms.c 12107 2010-06-09 13:12:33Z ashishs99 $";
  14. #include "swig.h"
  15. /* ------------------------------------------------------------------------
  16. * NewParm()
  17. *
  18. * Create a new parameter from datatype 'type' and name 'name' copying
  19. * the file and line number from the Node file_line_node.
  20. * ------------------------------------------------------------------------ */
  21. Parm *NewParm(SwigType *type, const_String_or_char_ptr name, Node *file_line_node) {
  22. Parm *p = NewParmWithoutFileLineInfo(type, name);
  23. Setfile(p, Getfile(file_line_node));
  24. Setline(p, Getline(file_line_node));
  25. return p;
  26. }
  27. /* ------------------------------------------------------------------------
  28. * NewParmWithoutFileLineInfo()
  29. *
  30. * Create a new parameter from datatype 'type' and name 'name' without any
  31. * file / line numbering information.
  32. * ------------------------------------------------------------------------ */
  33. Parm *NewParmWithoutFileLineInfo(SwigType *type, const_String_or_char_ptr name) {
  34. Parm *p = NewHash();
  35. set_nodeType(p, "parm");
  36. if (type) {
  37. SwigType *ntype = Copy(type);
  38. Setattr(p, "type", ntype);
  39. Delete(ntype);
  40. }
  41. Setattr(p, "name", name);
  42. return p;
  43. }
  44. /* ------------------------------------------------------------------------
  45. * CopyParm()
  46. * ------------------------------------------------------------------------ */
  47. Parm *CopyParm(Parm *p) {
  48. Parm *np = NewHash();
  49. Iterator ki;
  50. for (ki = First(p); ki.key; ki = Next(ki)) {
  51. if (DohIsString(ki.item)) {
  52. DOH *c = Copy(ki.item);
  53. Setattr(np,ki.key,c);
  54. Delete(c);
  55. }
  56. }
  57. Setfile(np, Getfile(p));
  58. Setline(np, Getline(p));
  59. return np;
  60. }
  61. /* ------------------------------------------------------------------
  62. * CopyParmListMax()
  63. * CopyParmList()
  64. * ------------------------------------------------------------------ */
  65. ParmList *CopyParmListMax(ParmList *p, int count) {
  66. Parm *np;
  67. Parm *pp = 0;
  68. Parm *fp = 0;
  69. if (!p)
  70. return 0;
  71. while (p) {
  72. if (count == 0) break;
  73. np = CopyParm(p);
  74. if (pp) {
  75. set_nextSibling(pp, np);
  76. Delete(np);
  77. } else {
  78. fp = np;
  79. }
  80. pp = np;
  81. p = nextSibling(p);
  82. count--;
  83. }
  84. return fp;
  85. }
  86. ParmList *CopyParmList(ParmList *p) {
  87. return CopyParmListMax(p,-1);
  88. }
  89. /* -----------------------------------------------------------------------------
  90. * int ParmList_numrequired(). Return number of required arguments
  91. * ----------------------------------------------------------------------------- */
  92. int ParmList_numrequired(ParmList *p) {
  93. int i = 0;
  94. while (p) {
  95. SwigType *t = Getattr(p, "type");
  96. String *value = Getattr(p, "value");
  97. if (value)
  98. return i;
  99. if (!(SwigType_type(t) == T_VOID))
  100. i++;
  101. else
  102. break;
  103. p = nextSibling(p);
  104. }
  105. return i;
  106. }
  107. /* -----------------------------------------------------------------------------
  108. * int ParmList_len()
  109. * ----------------------------------------------------------------------------- */
  110. int ParmList_len(ParmList *p) {
  111. int i = 0;
  112. while (p) {
  113. i++;
  114. p = nextSibling(p);
  115. }
  116. return i;
  117. }
  118. /* ---------------------------------------------------------------------
  119. * get_empty_type()
  120. * ---------------------------------------------------------------------- */
  121. static SwigType *get_empty_type() {
  122. return NewStringEmpty();
  123. }
  124. /* ---------------------------------------------------------------------
  125. * ParmList_str()
  126. *
  127. * Generates a string of parameters
  128. * ---------------------------------------------------------------------- */
  129. String *ParmList_str(ParmList *p) {
  130. String *out = NewStringEmpty();
  131. while (p) {
  132. String *type = Getattr(p, "type");
  133. String *pstr = SwigType_str(type ? type : get_empty_type(), Getattr(p, "name"));
  134. Append(out, pstr);
  135. p = nextSibling(p);
  136. if (p) {
  137. Append(out, ",");
  138. }
  139. Delete(pstr);
  140. }
  141. return out;
  142. }
  143. /* ---------------------------------------------------------------------
  144. * ParmList_str_defaultargs()
  145. *
  146. * Generates a string of parameters including default arguments
  147. * ---------------------------------------------------------------------- */
  148. String *ParmList_str_defaultargs(ParmList *p) {
  149. String *out = NewStringEmpty();
  150. while (p) {
  151. String *value = Getattr(p, "value");
  152. String *type = Getattr(p, "type");
  153. String *pstr = SwigType_str(type ? type : get_empty_type(), Getattr(p, "name"));
  154. Append(out, pstr);
  155. if (value) {
  156. Printf(out, "=%s", value);
  157. }
  158. p = nextSibling(p);
  159. if (p) {
  160. Append(out, ",");
  161. }
  162. Delete(pstr);
  163. }
  164. return out;
  165. }
  166. /* -----------------------------------------------------------------------------
  167. * ParmList_str_multibrackets()
  168. *
  169. * Generates a string of parameters including default arguments adding brackets
  170. * if more than one parameter
  171. * ----------------------------------------------------------------------------- */
  172. String *ParmList_str_multibrackets(ParmList *p) {
  173. String *out;
  174. String *parm_str = ParmList_str_defaultargs(p);
  175. if (ParmList_len(p) > 1)
  176. out = NewStringf("(%s)", parm_str);
  177. else
  178. out = NewStringf("%s", parm_str);
  179. Delete(parm_str);
  180. return out;
  181. }
  182. /* ---------------------------------------------------------------------
  183. * ParmList_protostr()
  184. *
  185. * Generate a prototype string.
  186. * ---------------------------------------------------------------------- */
  187. String *ParmList_protostr(ParmList *p) {
  188. String *out = NewStringEmpty();
  189. while (p) {
  190. String *type = Getattr(p, "type");
  191. String *pstr = SwigType_str(type ? type : get_empty_type(), 0);
  192. Append(out, pstr);
  193. p = nextSibling(p);
  194. if (p) {
  195. Append(out, ",");
  196. }
  197. Delete(pstr);
  198. }
  199. return out;
  200. }
  201. /* ---------------------------------------------------------------------
  202. * ParmList_has_defaultargs()
  203. *
  204. * Returns 1 if the parameter list passed in is has one or more default
  205. * arguments. Otherwise returns 0.
  206. * ---------------------------------------------------------------------- */
  207. int ParmList_has_defaultargs(ParmList *p) {
  208. while (p) {
  209. if (Getattr(p, "value")) {
  210. return 1;
  211. }
  212. p = nextSibling(p);
  213. }
  214. return 0;
  215. }