/tags/rel-1-3-30rc1-b4beautify/SWIG/Source/Modules/directors.cxx

# · C++ · 248 lines · 188 code · 17 blank · 43 comment · 55 complexity · 2e1158ef6c93a61254cc535b6207ef6b MD5 · raw file

  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. * directors.cxx
  6. *
  7. * Director support functions.
  8. * Not all of these may be necessary, and some may duplicate existing functionality
  9. * in SWIG. --MR
  10. * ----------------------------------------------------------------------------- */
  11. char cvsroot_directors_cxx[] = "$Header";
  12. #include "swigmod.h"
  13. /* Swig_csuperclass_call()
  14. *
  15. * Generates a fully qualified method call, including the full parameter list.
  16. * e.g. "base::method(i, j)"
  17. *
  18. */
  19. String *Swig_csuperclass_call(String* base, String* method, ParmList* l) {
  20. String *call = NewString("");
  21. int arg_idx = 0;
  22. Parm *p;
  23. if (base) {
  24. Printf(call, "%s::", base);
  25. }
  26. Printf(call, "%s(", method);
  27. for (p=l; p; p = nextSibling(p)) {
  28. String *pname = Getattr(p, "name");
  29. if (!pname && Cmp(Getattr(p,"type"), "void")) {
  30. pname = NewString("");
  31. Printf(pname, "arg%d", arg_idx++);
  32. }
  33. if (p != l) Printf(call, ", ");
  34. Printv(call, pname, NIL);
  35. }
  36. Printf(call, ")");
  37. return call;
  38. }
  39. /* Swig_class_declaration()
  40. *
  41. * Generate the start of a class/struct declaration.
  42. * e.g. "class myclass"
  43. *
  44. */
  45. String *Swig_class_declaration(Node *n, String *name) {
  46. if (!name) {
  47. name = Getattr(n, "sym:name");
  48. }
  49. String *result = NewString("");
  50. String *kind = Getattr(n, "kind");
  51. Printf(result, "%s %s", kind, name);
  52. return result;
  53. }
  54. String *Swig_class_name(Node *n) {
  55. String *name;
  56. name = Copy(Getattr(n, "sym:name"));
  57. return name;
  58. }
  59. /* Swig_director_declaration()
  60. *
  61. * Generate the full director class declaration, complete with base classes.
  62. * e.g. "class SwigDirector_myclass : public myclass, public Swig::Director {"
  63. *
  64. */
  65. String *Swig_director_declaration(Node *n) {
  66. String* classname = Swig_class_name(n);
  67. String *directorname = NewStringf("SwigDirector_%s", classname);
  68. String *base = Getattr(n, "classtype");
  69. String *declaration = Swig_class_declaration(n, directorname);
  70. Printf(declaration, " : public %s, public Swig::Director {\n", base);
  71. Delete(classname);
  72. Delete(directorname);
  73. return declaration;
  74. }
  75. String *Swig_method_call(String_or_char *name, ParmList *parms) {
  76. String *func;
  77. int i = 0;
  78. int comma = 0;
  79. Parm *p = parms;
  80. SwigType *pt;
  81. String *nname;
  82. func = NewString("");
  83. nname = SwigType_namestr(name);
  84. Printf(func,"%s(", nname);
  85. while (p) {
  86. String *pname;
  87. pt = Getattr(p,"type");
  88. if ((SwigType_type(pt) != T_VOID)) {
  89. if (comma) Printf(func,",");
  90. pname = Getattr(p, "name");
  91. Printf(func,"%s", pname);
  92. comma = 1;
  93. i++;
  94. }
  95. p = nextSibling(p);
  96. }
  97. Printf(func,")");
  98. return func;
  99. }
  100. /* Swig_method_decl
  101. *
  102. * Misnamed and misappropriated! Taken from SWIG's type string manipulation utilities
  103. * and modified to generate full (or partial) type qualifiers for method declarations,
  104. * local variable declarations, and return value casting. More importantly, it merges
  105. * parameter type information with actual parameter names to produce a complete method
  106. * declaration that fully mirrors the original method declaration.
  107. *
  108. * There is almost certainly a saner way to do this.
  109. *
  110. * This function needs to be cleaned up and possibly split into several smaller
  111. * functions. For instance, attaching default names to parameters should be done in a
  112. * separate function.
  113. *
  114. */
  115. String *Swig_method_decl(SwigType *s, const String_or_char *id, List *args, int strip, int values) {
  116. String *result;
  117. List *elements;
  118. String *element = 0, *nextelement;
  119. int is_const = 0;
  120. int nelements, i;
  121. int is_func = 0;
  122. int arg_idx = 0;
  123. if (id) {
  124. result = NewString(Char(id));
  125. } else {
  126. result = NewString("");
  127. }
  128. elements = SwigType_split(s);
  129. nelements = Len(elements);
  130. if (nelements > 0) {
  131. element = Getitem(elements, 0);
  132. }
  133. for (i = 0; i < nelements; i++) {
  134. if (i < (nelements - 1)) {
  135. nextelement = Getitem(elements, i+1);
  136. } else {
  137. nextelement = 0;
  138. }
  139. if (SwigType_isqualifier(element)) {
  140. int skip = 0;
  141. DOH *q = 0;
  142. if (!strip) {
  143. q = SwigType_parm(element);
  144. if (!Cmp(q, "const")) {
  145. is_const = 1;
  146. is_func = SwigType_isfunction(nextelement);
  147. if (is_func) skip = 1;
  148. skip = 1;
  149. }
  150. if (!skip) {
  151. Insert(result,0," ");
  152. Insert(result,0,q);
  153. }
  154. Delete(q);
  155. }
  156. } else if (SwigType_ispointer(element)) {
  157. Insert(result,0,"*");
  158. if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
  159. Insert(result,0,"(");
  160. Append(result,")");
  161. }
  162. } else if (SwigType_ismemberpointer(element)) {
  163. String *q;
  164. q = SwigType_parm(element);
  165. Insert(result,0,"::*");
  166. Insert(result,0,q);
  167. if ((nextelement) && ((SwigType_isfunction(nextelement) || (SwigType_isarray(nextelement))))) {
  168. Insert(result,0,"(");
  169. Append(result,")");
  170. }
  171. Delete(q);
  172. }
  173. else if (SwigType_isreference(element)) {
  174. Insert(result,0,"&");
  175. } else if (SwigType_isarray(element)) {
  176. DOH *size;
  177. Append(result,"[");
  178. size = SwigType_parm(element);
  179. Append(result,size);
  180. Append(result,"]");
  181. Delete(size);
  182. } else if (SwigType_isfunction(element)) {
  183. Parm *parm;
  184. String *p;
  185. Append(result,"(");
  186. parm = args;
  187. while (parm != 0) {
  188. String *type = Getattr(parm, "type");
  189. String* name = Getattr(parm, "name");
  190. if (!name && Cmp(type, "void")) {
  191. name = NewString("");
  192. Printf(name, "arg%d", arg_idx++);
  193. Setattr(parm, "name", name);
  194. }
  195. if (!name) {
  196. name = NewString("");
  197. }
  198. p = SwigType_str(type, name);
  199. Append(result,p);
  200. String* value = Getattr(parm, "value");
  201. if (values && (value != 0)) {
  202. Printf(result, " = %s", value);
  203. }
  204. parm = nextSibling(parm);
  205. if (parm != 0) Append(result,", ");
  206. }
  207. Append(result,")");
  208. } else {
  209. if (Strcmp(element,"v(...)") == 0) {
  210. Insert(result,0,"...");
  211. } else {
  212. String *bs = SwigType_namestr(element);
  213. Insert(result,0," ");
  214. Insert(result,0,bs);
  215. Delete(bs);
  216. }
  217. }
  218. element = nextelement;
  219. }
  220. Delete(elements);
  221. if (is_const) {
  222. if (is_func) {
  223. Append(result, " ");
  224. Append(result, "const");
  225. } else {
  226. Insert(result, 0, "const ");
  227. }
  228. }
  229. Chop(result);
  230. return result;
  231. }