/tags/ttn-post-xml-patch/SWIG/Source/Modules1.1/lang.cxx

# · C++ · 475 lines · 273 code · 89 blank · 113 comment · 43 complexity · 6fb1bf434bbbec02ff79743f9efc6e97 MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * lang.cxx
  3. *
  4. * Language base class functions. Default C++ handling is also implemented here.
  5. *
  6. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  7. *
  8. * Copyright (C) 1998-2000. The University of Chicago
  9. * Copyright (C) 1995-1998. The University of Utah and The Regents of the
  10. * University of California.
  11. *
  12. * See the file LICENSE for information on usage and redistribution.
  13. * ----------------------------------------------------------------------------- */
  14. static char cvsroot[] = "$Header$";
  15. #include "swig11.h"
  16. /* -----------------------------------------------------------------
  17. * Language::create_command()
  18. * ----------------------------------------------------------------- */
  19. void Language::create_command(String *, String *) {
  20. Printf(stderr,"SWIG Warning. No command creation procedure defined.\n");
  21. Printf(stderr,"C++ inheritance may not work correctly.\n");
  22. }
  23. /* -----------------------------------------------------------------
  24. * Language::nativefunction()
  25. * ----------------------------------------------------------------- */
  26. void
  27. Language::nativefunction(DOH *node) {
  28. Printf(stderr,"%s:%d. Adding native function %s not supported (ignored).\n", Getfile(node), Getline(node), Getattr(node,"scriptname"));
  29. }
  30. /* The following strings are used during code generation and contain various
  31. permutations of the class name:
  32. ClassName - This is the name of the class as it appears in C/C++
  33. source code. It does not include a type specifier
  34. such as "struct" or "class"
  35. ClassRename - If non-NULL, the class has been renamed.
  36. ClassType - The type of the class (struct, class, union)
  37. ClassFullname - The full name of the class including its type specifier.
  38. For example, "class Foo" or "struct Vector".
  39. ClassPrefix - The class prefix as used in the scripting language
  40. interface. This is either ClassName or ClassRename.
  41. */
  42. static String *ClassName = 0;
  43. static String *ClassRename = 0;
  44. static String *ClassType = 0;
  45. static String *ClassFullname = 0;
  46. static String *ClassPrefix = 0;
  47. /* -----------------------------------------------------------------------------
  48. * Language::cpp_open_class()
  49. * ----------------------------------------------------------------------------- */
  50. void Language::cpp_open_class(DOH *node) {
  51. String *classname;
  52. String *classrename;
  53. String *ctype;
  54. String *altname;
  55. int strip = CPlusPlus;
  56. classname = Getname(node);
  57. classrename = Getattr(node,"scriptname");
  58. ctype = Getattr(node,"classtype");
  59. altname = Getattr(node,"altname");
  60. if (altname) {
  61. strip = 1;
  62. classname = altname; /* Use the alt name instead of the class name */
  63. }
  64. if (strip) SetInt(node,"strip",1);
  65. /* Copy the class name */
  66. if (ClassName) Delete(ClassName);
  67. ClassName = NewString(classname);
  68. /* Copy the class renaming */
  69. if (ClassRename) Delete(ClassRename);
  70. ClassRename = NewString(classrename);
  71. if (ClassType) Delete(ClassType);
  72. ClassType = strip ? 0 : NewString(ctype);
  73. if (ClassFullname) Delete(ClassFullname);
  74. ClassFullname = ClassType ? NewStringf("%s %s", ClassType, ClassName) : NewString(ClassName);
  75. ClassPrefix = ClassRename;
  76. }
  77. /* -----------------------------------------------------------------------------
  78. * Language::cpp_close_class()
  79. * ----------------------------------------------------------------------------- */
  80. void Language::cpp_close_class() {
  81. /* Doesn't really do anything */
  82. }
  83. /* -----------------------------------------------------------------------------
  84. * Language::cpp_memberfunction()
  85. * ----------------------------------------------------------------------------- */
  86. void Language::cpp_memberfunction(DOH *node) {
  87. String *name;
  88. String *iname;
  89. String *ccode;
  90. String *script_name;
  91. SwigType *type;
  92. ParmList *parms;
  93. Wrapper *w;
  94. name = Getattr(node,"name");
  95. iname = Getattr(node,"scriptname");
  96. type = Getattr(node,"type");
  97. parms = Getattr(node,"parms");
  98. ccode = Getattr(node,"code");
  99. /* Generate the C wrapper function name and interpreter name of this function*/
  100. /* Create the actual function name */
  101. script_name = Swig_name_member(ClassPrefix, iname ? iname : name);
  102. /* Now do a symbol table lookup on it */
  103. /* Create the C wrapper function for this */
  104. w = Swig_cmethod_wrapper(ClassFullname, name, type, parms, ccode);
  105. if (AddMethods && ccode) {
  106. /* Dump the C wrappers */
  107. Printf(f_wrappers,"%s",w);
  108. } else if (!AddMethods) {
  109. /* Just create a string that does what we want */
  110. emit_set_action(Swig_cmethod_call(name, Getparms(w)));
  111. }
  112. Setattr(w,"scriptname",script_name);
  113. lang->function(w);
  114. Delete(w);
  115. }
  116. /* -----------------------------------------------------------------------------
  117. * Language::cpp_constructor()
  118. * ----------------------------------------------------------------------------- */
  119. void Language::cpp_constructor(DOH *node) {
  120. String *name;
  121. String *iname;
  122. String *cname;
  123. String *ccode;
  124. ParmList *parms;
  125. Wrapper *w;
  126. name = Getattr(node,"name");
  127. iname = Getattr(node,"scriptname");
  128. parms = Getattr(node,"parms");
  129. ccode = Getattr(node,"code");
  130. if ((Cmp(name,ClassName))) {
  131. Printf(stderr,"%s:%d. Function %s must have a return type.\n",
  132. Getfile(node), Getline(node), name);
  133. return;
  134. }
  135. cname = Swig_name_construct(iname ? iname : ClassPrefix);
  136. /* Add this function to the SWIG symbol table */
  137. if (CPlusPlus) {
  138. w = Swig_cppconstructor_wrapper(ClassFullname, parms, ccode);
  139. } else {
  140. w = Swig_cconstructor_wrapper(ClassFullname, parms, ccode);
  141. }
  142. Setattr(w,"scriptname", cname);
  143. if (!AddMethods) {
  144. if (CPlusPlus) {
  145. emit_set_action(Swig_cppconstructor_call(ClassFullname, parms));
  146. } else {
  147. emit_set_action(Swig_cconstructor_call(ClassFullname));
  148. }
  149. } else {
  150. if (ccode) {
  151. Printf(f_wrappers,"%s",w);
  152. }
  153. }
  154. lang->function(w);
  155. Delete(w);
  156. /* Call our default method */
  157. /* cplus_emit_constructor(Char(ClassName), Char(ClassType), Char(ClassRename), name, iname, l, AddMethods); */
  158. }
  159. /* -----------------------------------------------------------------------------
  160. * Language::cpp_destructor()
  161. * ----------------------------------------------------------------------------- */
  162. void Language::cpp_destructor(DOH *node) {
  163. String *name;
  164. String *iname;
  165. String *cname;
  166. String *ccode;
  167. Wrapper *w;
  168. name = Getattr(node,"name");
  169. iname = Getattr(node,"scriptname");
  170. cname = Swig_name_destroy(ClassRename ? ClassRename : ClassName);
  171. ccode = Getattr(node,"code");
  172. /* Add this function to the SWIG symbol table */
  173. if (CPlusPlus) {
  174. w = Swig_cppdestructor_wrapper(ClassFullname,ccode);
  175. } else {
  176. w = Swig_cdestructor_wrapper(ClassFullname, ccode);
  177. }
  178. Setattr(w,"scriptname",cname);
  179. if (AddMethods && ccode) {
  180. Printf(f_wrappers,"%s", w);
  181. lang->function(w);
  182. } else if (AddMethods) {
  183. lang->function(w);
  184. } else {
  185. if (CPlusPlus)
  186. emit_set_action(Swig_cppdestructor_call());
  187. else
  188. emit_set_action(Swig_cdestructor_call());
  189. lang->function(w);
  190. }
  191. Delete(w);
  192. }
  193. /* -----------------------------------------------------------------------------
  194. * Language::cpp_inherit()
  195. * ----------------------------------------------------------------------------- */
  196. void Language::cpp_inherit(List *bases) {
  197. if (!bases) return;
  198. /*
  199. while (baseclass[i]) {
  200. // cplus_inherit_members(baseclass[i],mode);
  201. i++;
  202. }
  203. */
  204. }
  205. /* -----------------------------------------------------------------------------
  206. * Language::cpp_variable()
  207. * ----------------------------------------------------------------------------- */
  208. void Language::cpp_variable(DOH *node) {
  209. String *name;
  210. String *iname;
  211. String *cname;
  212. String *setcode, *getcode;
  213. SwigType *type;
  214. Wrapper *w;
  215. name = Getattr(node,"name");
  216. iname = Getattr(node,"scriptname");
  217. type = Getattr(node,"type");
  218. setcode = Getattr(node,"setcode");
  219. getcode = Getattr(node,"getcode");
  220. /* Set the class prefix */
  221. cname = Swig_name_get(Swig_name_member(ClassPrefix, iname ? iname : name));
  222. /* Check the symbol table */
  223. /* Create a function to set the value of the variable */
  224. if (!ReadOnly) {
  225. w = Swig_cmemberset_wrapper(ClassFullname, name, type, setcode);
  226. Setattr(w,"scriptname",Swig_name_set(Swig_name_member(ClassPrefix,iname ? iname : name)));
  227. if (AddMethods && setcode) {
  228. Printf(f_wrappers,"%s",w);
  229. } else if (!AddMethods) {
  230. /* Check for a member in typemap here */
  231. String *target = NewStringf("%s->%s", Swig_cparm_name(0,0),name);
  232. char *tm = Swig_typemap_lookup("memberin",type,name,Swig_cparm_name(0,1),target,0);
  233. if (!tm)
  234. emit_set_action(Swig_cmemberset_call(name,type));
  235. else
  236. emit_set_action(tm);
  237. Delete(target);
  238. }
  239. lang->function(w);
  240. Delete(w);
  241. }
  242. w = Swig_cmemberget_wrapper(ClassFullname, name, type, getcode);
  243. Setattr(w,"scriptname", Swig_name_get(Swig_name_member(ClassPrefix, iname ? iname : name)));
  244. if (AddMethods && getcode) {
  245. Printf(f_wrappers,"%s",w);
  246. } else if (!AddMethods) {
  247. emit_set_action(Swig_cmemberget_call(name,type));
  248. }
  249. lang->function(w);
  250. Delete(w);
  251. }
  252. /* -----------------------------------------------------------------------------
  253. * Language::cpp_static_func()
  254. * ----------------------------------------------------------------------------- */
  255. void Language::cpp_staticfunction(DOH *node) {
  256. String *name;
  257. String *iname;
  258. SwigType *type;
  259. ParmList *parms;
  260. String *ccode;
  261. String *script_name;
  262. String *real_name;
  263. DOH *nnode;
  264. Wrapper *w;
  265. name = Getattr(node,"name");
  266. iname = Getattr(node,"scriptname");
  267. type = Getattr(node,"type");
  268. parms = Getattr(node,"parms");
  269. ccode = Getattr(node,"code");
  270. /* Set the member function name */
  271. script_name = Swig_name_member(ClassPrefix,iname ? iname : name);
  272. /* Now do a symbol table lookup on it : */
  273. /* Figure out the name of the function */
  274. if (AddMethods) {
  275. real_name = Swig_name_member(ClassName, name);
  276. if (!ccode) {
  277. nnode = Copy(node);
  278. Setattr(nnode,"name", real_name);
  279. Setattr(nnode,"scriptname", script_name);
  280. lang->function(nnode);
  281. Delete(nnode);
  282. } else {
  283. w = Swig_cfunction_wrapper(real_name,type,parms,ccode);
  284. Printf(f_wrappers,"%s",w);
  285. Setattr(w,"scriptname",script_name);
  286. lang->function(w);
  287. Delete(w);
  288. }
  289. } else {
  290. nnode = Copy(node);
  291. real_name = NewStringf("%s::%s", ClassName, name);
  292. Setattr(nnode,"name", real_name);
  293. Setattr(nnode,"scriptname", script_name);
  294. lang->function(nnode);
  295. Delete(nnode);
  296. Delete(real_name);
  297. }
  298. }
  299. /* -----------------------------------------------------------------------------
  300. * Language::cpp_constant()
  301. * ----------------------------------------------------------------------------- */
  302. void Language::cpp_constant(DOH *node)
  303. {
  304. String *name;
  305. String *iname;
  306. String *value;
  307. SwigType *type;
  308. String *cname;
  309. String *mname;
  310. String *new_value;
  311. name = Getattr(node,"name");
  312. iname = Getattr(node,"scriptname");
  313. value = Getattr(node,"value");
  314. type = Getattr(node,"type");
  315. /* Set the constant name */
  316. cname = Swig_name_member(ClassPrefix, iname ? iname : name);
  317. /* Now do a symbol table lookup on it : */
  318. /* Form correct C++ name */
  319. mname = NewStringf("%s::%s", ClassName,name);
  320. /* Declare a constant */
  321. if (!value) {
  322. new_value = NewStringf("%s::%s", ClassName, name);
  323. } else {
  324. new_value = NewString(value);
  325. }
  326. Hash *n;
  327. n = NewHash();
  328. Setattr(n,"name",cname);
  329. Setattr(n,"scriptname",cname);
  330. Setattr(n,"type",type);
  331. Setattr(n,"value",new_value);
  332. lang->constant(n);
  333. Delete(n);
  334. Delete(new_value);
  335. Delete(mname);
  336. }
  337. /* -----------------------------------------------------------------------------
  338. * Language::cpp_staticvariable()
  339. * ----------------------------------------------------------------------------- */
  340. void Language::cpp_staticvariable(DOH *node) {
  341. char *name, *iname;
  342. SwigType *t;
  343. char *cname;
  344. char mname[256];
  345. name = GetChar(node,"name");
  346. iname = GetChar(node,"scriptname");
  347. t = Getattr(node,"type");
  348. /* Create the variable name */
  349. cname = Char(Swig_name_member(ClassPrefix, iname ? iname : name));
  350. /* Now do a symbol table lookup on it : */
  351. /* Form correct C++ name */
  352. sprintf(mname,"%s::%s",Char(ClassName),name);
  353. /* Link with this variable */
  354. Hash *n = NewHash();
  355. Setattr(n,"name",mname);
  356. Setattr(n,"scriptname", cname);
  357. Setattr(n,"type",t);
  358. lang->variable(n);
  359. Delete(n);
  360. }
  361. /* -----------------------------------------------------------------------------
  362. * Language::cpp_class_decl()
  363. * ----------------------------------------------------------------------------- */
  364. void Language::cpp_class_decl(DOH *) {
  365. /* Does nothing by default */
  366. }
  367. /* -----------------------------------------------------------------------------
  368. * Language::add_typedef()
  369. * ----------------------------------------------------------------------------- */
  370. void Language::add_typedef(SwigType *, String *) {
  371. /* Does nothing by default */
  372. }
  373. /* -----------------------------------------------------------------------------
  374. * Language::pragma()
  375. * ----------------------------------------------------------------------------- */
  376. void Language::pragma(DOH *node) {
  377. /* Does nothing by default */
  378. }
  379. /* -----------------------------------------------------------------------------
  380. * Language::import()
  381. * ----------------------------------------------------------------------------- */
  382. void Language::import(String *) {
  383. /* Does nothing by default */
  384. }