PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C++ | 427 lines | 296 code | 99 blank | 32 comment | 54 complexity | 6dd967fe7cec77ce38a3eaa24eafc446 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. * uffi.cxx
  6. *
  7. * Uffi language module for SWIG.
  8. * ----------------------------------------------------------------------------- */
  9. // TODO: remove remnants of lisptype
  10. char cvsroot_uffi_cxx[] = "$Header$";
  11. #include "swigmod.h"
  12. class UFFI : public Language {
  13. public:
  14. virtual void main(int argc, char *argv[]);
  15. virtual int top(Node *n);
  16. virtual int functionWrapper(Node *n);
  17. virtual int constantWrapper(Node *n);
  18. virtual int classHandler(Node *n);
  19. virtual int membervariableHandler(Node *n);
  20. };
  21. static File *f_cl=0;
  22. static File *f_null=0;
  23. static struct {
  24. int count;
  25. String **entries;
  26. } defined_foreign_types;
  27. static const char *identifier_converter="identifier-convert-null";
  28. static int any_varargs(ParmList *pl) {
  29. Parm *p;
  30. for(p=pl; p; p=nextSibling(p)) {
  31. if (SwigType_isvarargs(Getattr(p, "type")))
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. /* utilities */
  37. /* returns new string w/ parens stripped */
  38. static String *strip_parens(String *string) {
  39. char *s=Char(string), *p;
  40. int len=Len(string);
  41. String *res;
  42. if (len==0 || s[0] != '(' || s[len-1] != ')') {
  43. return NewString(string);
  44. }
  45. p=(char *)malloc(len-2+1);
  46. if (!p) {
  47. Printf(stderr, "Malloc failed\n");
  48. SWIG_exit(EXIT_FAILURE);
  49. }
  50. strncpy(p, s+1, len-1);
  51. p[len-2]=0; /* null terminate */
  52. res=NewString(p);
  53. free(p);
  54. return res;
  55. }
  56. static String *convert_literal(String *num_param, String *type) {
  57. String *num=strip_parens(num_param), *res;
  58. char *s=Char(num);
  59. /* Make sure doubles use 'd' instead of 'e' */
  60. if (!Strcmp(type, "double")) {
  61. String *updated=Copy(num);
  62. if (Replace(updated, "e", "d", DOH_REPLACE_ANY) > 1) {
  63. Printf(stderr, "Weird!! number %s looks invalid.\n", num);
  64. SWIG_exit(EXIT_FAILURE);
  65. }
  66. Delete(num);
  67. return updated;
  68. }
  69. if (SwigType_type(type) == T_CHAR) {
  70. /* Use CL syntax for character literals */
  71. return NewStringf("#\\%s", num_param);
  72. }
  73. else if (SwigType_type(type) == T_STRING) {
  74. /* Use CL syntax for string literals */
  75. return NewStringf("\"%s\"", num_param);
  76. }
  77. if (Len(num) < 2 || s[0] != '0') {
  78. return num;
  79. }
  80. /* octal or hex */
  81. res=NewStringf("#%c%s",
  82. s[1] == 'x' ? 'x' : 'o',
  83. s+2);
  84. Delete(num);
  85. return res;
  86. }
  87. static void add_defined_foreign_type(String *type) {
  88. if (!defined_foreign_types.count) {
  89. /* Make fresh */
  90. defined_foreign_types.count=1;
  91. defined_foreign_types.entries=(String **)malloc(sizeof(String *));
  92. } else {
  93. /* make room */
  94. defined_foreign_types.count++;
  95. defined_foreign_types.entries=(String **)
  96. realloc(defined_foreign_types.entries,
  97. defined_foreign_types.count*sizeof(String *));
  98. }
  99. if (!defined_foreign_types.entries) {
  100. Printf(stderr, "Out of memory\n");
  101. SWIG_exit(EXIT_FAILURE);
  102. }
  103. /* Fill in the new data */
  104. defined_foreign_types.entries[defined_foreign_types.count-1]=
  105. Copy(type);
  106. }
  107. static String *get_ffi_type(SwigType *ty, const String_or_char *name) {
  108. Hash *typemap = Swig_typemap_search("ffitype", ty, name, 0);
  109. if (typemap) {
  110. String *typespec = Getattr(typemap, "code");
  111. return NewString(typespec);
  112. }
  113. else {
  114. SwigType *tr=SwigType_typedef_resolve_all(ty);
  115. char *type_reduced=Char(tr);
  116. int i;
  117. //Printf(stdout,"convert_type %s\n", ty);
  118. if (SwigType_isconst(tr)) {
  119. SwigType_pop(tr);
  120. type_reduced=Char(tr);
  121. }
  122. if (SwigType_ispointer(type_reduced) || SwigType_isarray(ty) ||
  123. !strncmp(type_reduced, "p.f", 3)) {
  124. return NewString(":pointer-void");
  125. }
  126. for(i=0; i<defined_foreign_types.count; i++) {
  127. if (!Strcmp(ty, defined_foreign_types.entries[i])) {
  128. return NewStringf("#.(%s \"%s\" :type :type)",
  129. identifier_converter,
  130. ty);
  131. }
  132. }
  133. if (!Strncmp(type_reduced, "enum ", 5)) {
  134. return NewString(":int");
  135. }
  136. Printf(stderr, "Unsupported data type: %s (was: %s)\n", type_reduced, ty);
  137. SWIG_exit(EXIT_FAILURE);
  138. }
  139. return 0;
  140. }
  141. static String *get_lisp_type(SwigType *ty, const String_or_char *name)
  142. {
  143. Hash *typemap = Swig_typemap_search("lisptype", ty, name, 0);
  144. if (typemap) {
  145. String *typespec = Getattr(typemap, "code");
  146. return NewString(typespec);
  147. }
  148. else {
  149. return NewString("");
  150. }
  151. }
  152. void UFFI :: main(int argc, char *argv[]) {
  153. int i;
  154. SWIG_library_directory("uffi");
  155. SWIG_config_file("uffi.swg");
  156. for(i=1; i<argc; i++) {
  157. if (!strcmp(argv[i], "-identifier-converter")) {
  158. char *conv=argv[i+1];
  159. if (!conv)
  160. Swig_arg_error();
  161. Swig_mark_arg(i);
  162. Swig_mark_arg(i+1);
  163. i++;
  164. /* check for built-ins */
  165. if (!strcmp(conv, "lispify")) {
  166. identifier_converter="identifier-convert-lispify";
  167. } else if (!strcmp(conv, "null")) {
  168. identifier_converter="identifier-convert-null";
  169. } else {
  170. /* Must be user defined */
  171. char *idconv = new char[strlen(conv)+1];
  172. strcpy(idconv, conv);
  173. identifier_converter=idconv;
  174. }
  175. }
  176. if (!strcmp(argv[i], "-help")) {
  177. fprintf(stdout, "UFFI Options (available with -uffi)\n");
  178. fprintf(stdout,
  179. " -identifier-converter <type or funcname>\n"
  180. "\tSpecifies the type of conversion to do on C identifiers to convert\n"
  181. "\tthem to symbols. There are two built-in converters: 'null' and\n"
  182. "\t 'lispify'. The default is 'null'. If you supply a name other\n"
  183. "\tthan one of the built-ins, then a function by that name will be\n"
  184. "\tcalled to convert identifiers to symbols.\n");
  185. }
  186. }
  187. }
  188. int UFFI :: top(Node *n) {
  189. String *module=Getattr(n, "name");
  190. String *output_filename=NewString("");
  191. String *devnull=NewString("/dev/null");
  192. f_null=NewFile(devnull, "w+");
  193. if (!f_null) {
  194. FileErrorDisplay(devnull);
  195. SWIG_exit(EXIT_FAILURE);
  196. }
  197. Delete(devnull);
  198. Printf(output_filename, "%s%s.cl", SWIG_output_directory(), module);
  199. f_cl=NewFile(output_filename, "w");
  200. if (!f_cl) {
  201. FileErrorDisplay(output_filename);
  202. SWIG_exit(EXIT_FAILURE);
  203. }
  204. Swig_register_filebyname("header",f_null);
  205. Swig_register_filebyname("runtime",f_null);
  206. Swig_register_filebyname("wrapper", f_cl);
  207. Printf(f_cl, ";; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; package: %s -*-\n;; This is an automatically generated file. Make changes in\n;; the definition file, not here.\n\n(defpackage :%s\n (:use :common-lisp :uffi))\n\n(in-package :%s)\n", module, module, module);
  208. Printf(f_cl, "(eval-when (compile load eval)\n (defparameter *swig-identifier-converter* '%s))\n", identifier_converter);
  209. Language::top(n);
  210. Close(f_cl);
  211. Delete(f_cl); // Delete the handle, not the file
  212. Close(f_null);
  213. Delete(f_null);
  214. return SWIG_OK;
  215. }
  216. int UFFI :: functionWrapper(Node *n) {
  217. String *funcname=Getattr(n, "sym:name");
  218. ParmList *pl=Getattr(n, "parms");
  219. Parm *p;
  220. int argnum=0, first=1, varargs=0;
  221. //Language::functionWrapper(n);
  222. Printf(f_cl, "(swig-defun \"%s\"\n", funcname);
  223. Printf(f_cl, " (");
  224. /* Special cases */
  225. if (ParmList_len(pl) == 0) {
  226. Printf(f_cl, ":void");
  227. } else if (any_varargs(pl)) {
  228. Printf(f_cl, "#| varargs |#");
  229. varargs=1;
  230. } else {
  231. for (p=pl; p; p=nextSibling(p), argnum++) {
  232. String *argname=Getattr(p, "name");
  233. SwigType *argtype=Getattr(p, "type");
  234. String *ffitype=get_ffi_type(argtype, argname);
  235. String *lisptype=get_lisp_type(argtype, argname);
  236. int tempargname=0;
  237. if (!argname) {
  238. argname=NewStringf("arg%d", argnum);
  239. tempargname=1;
  240. }
  241. if (!first) {
  242. Printf(f_cl, "\n ");
  243. }
  244. Printf(f_cl, "(%s %s %s)", argname, ffitype, lisptype);
  245. first=0;
  246. Delete(ffitype);
  247. Delete(lisptype);
  248. if (tempargname)
  249. Delete(argname);
  250. }
  251. }
  252. Printf(f_cl, ")\n"); /* finish arg list */
  253. Printf(f_cl, " :returning %s\n"
  254. //" :strings-convert t\n"
  255. //" :call-direct %s\n"
  256. //" :optimize-for-space t"
  257. ")\n",
  258. get_ffi_type(Getattr(n, "type"), "result")
  259. //,varargs ? "nil" : "t"
  260. );
  261. return SWIG_OK;
  262. }
  263. int UFFI :: constantWrapper(Node *n) {
  264. String *type=Getattr(n, "type");
  265. String *converted_value=convert_literal(Getattr(n, "value"), type);
  266. String *name=Getattr(n, "sym:name");
  267. #if 0
  268. Printf(stdout, "constant %s is of type %s. value: %s\n",
  269. name, type, converted_value);
  270. #endif
  271. Printf(f_cl, "(swig-defconstant \"%s\" %s)\n",
  272. name, converted_value);
  273. Delete(converted_value);
  274. return SWIG_OK;
  275. }
  276. // Includes structs
  277. int UFFI :: classHandler(Node *n) {
  278. String *name=Getattr(n, "sym:name");
  279. String *kind = Getattr(n,"kind");
  280. Node *c;
  281. if (Strcmp(kind, "struct")) {
  282. Printf(stderr, "Don't know how to deal with %s kind of class yet.\n",
  283. kind);
  284. Printf(stderr, " (name: %s)\n", name);
  285. SWIG_exit(EXIT_FAILURE);
  286. }
  287. Printf(f_cl,
  288. "(swig-def-struct \"%s\"\n \n",
  289. name);
  290. for (c=firstChild(n); c; c=nextSibling(c)) {
  291. SwigType *type=Getattr(c, "type");
  292. SwigType *decl=Getattr(c, "decl");
  293. type = Copy(type);
  294. SwigType_push(type, decl);
  295. String *lisp_type;
  296. if (Strcmp(nodeType(c), "cdecl")) {
  297. Printf(stderr, "Structure %s has a slot that we can't deal with.\n",
  298. name);
  299. Printf(stderr, "nodeType: %s, name: %s, type: %s\n",
  300. nodeType(c),
  301. Getattr(c, "name"),
  302. Getattr(c, "type"));
  303. SWIG_exit(EXIT_FAILURE);
  304. }
  305. /* Printf(stdout, "Converting %s in %s\n", type, name); */
  306. lisp_type=get_ffi_type(type, Getattr(c, "sym:name"));
  307. Printf(f_cl,
  308. " (#.(%s \"%s\" :type :slot) %s)\n",
  309. identifier_converter,
  310. Getattr(c, "sym:name"),
  311. lisp_type);
  312. Delete(lisp_type);
  313. }
  314. // Language::classHandler(n);
  315. Printf(f_cl, " )\n");
  316. /* Add this structure to the known lisp types */
  317. //Printf(stdout, "Adding %s foreign type\n", name);
  318. add_defined_foreign_type(name);
  319. return SWIG_OK;
  320. }
  321. int UFFI :: membervariableHandler(Node *n)
  322. {
  323. Language::membervariableHandler(n);
  324. return SWIG_OK;
  325. }
  326. extern "C" Language *swig_uffi(void) {
  327. return new UFFI();
  328. }