Some changes to generated proxy .c and .h file.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10522 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2008-06-07 20:30:42 +00:00
commit d828791f56
4 changed files with 92 additions and 24 deletions

View file

@ -1,7 +1,7 @@
/* File : example.c */
/* A global variable */
/* double Foo = 3.0; */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {

View file

@ -1,9 +1,8 @@
/* File : example.i */
%module example
%feature("prepend") {
printf("Testing...\n");
}
%inline %{
extern double Foo;
extern int gcd(int x, int y);
%}

View file

@ -1,8 +1,12 @@
#include <stdio.h>
#include "example_proxy.h"
int main(int argc, char **argv) {
int a = 35;
int b = 15;
printf("GCD(%d, %d)=%d", a, b, gcd(a, b));
printf("Foo is %f\n", Foo);
printf("GCD(%d, %d)=%d\n", a, b, gcd(a, b));
return 0;
}

View file

@ -19,8 +19,10 @@ class C:public Language {
File *f_wrappers;
File *f_init;
File *f_shadow_c;
File *f_shadow_h;
String *f_shadow;
String *f_shadow_code;
String *f_shadow_header;
bool shadow_flag;
@ -62,6 +64,27 @@ public:
}
}
void emitBanner(File *f) {
Printf(f, "/* This file was automatically generated by SWIG (http://www.swig.org).\n");
Printf(f, " * Version %s\n", Swig_package_version());
Printf(f, " * \n");
Printf(f, " * Don't modify this file, modify the SWIG interface instead.\n");
Printf(f, " */\n\n");
}
void emitMingwLinkFix(File *f) {
Printf(f, "#ifndef __GNUC__\n");
Printf(f, "# define __DLL_IMPORT __declspec(dllimport)\n");
Printf(f, "#else\n");
Printf(f, "# define __DLL_IMPORT __attribute__((dllimport)) extern\n");
Printf(f, "#endif\n\n");
Printf(f, "#if defined (BUILD_ddd_DLL) || !defined (__WIN32__)\n");
Printf(f, "# define DLL_IMPORT extern\n");
Printf(f, "#else\n");
Printf(f, "# define DLL_IMPORT __DLL_IMPORT\n");
Printf(f, "#endif\n\n");
}
/* ---------------------------------------------------------------------
* top()
* --------------------------------------------------------------------- */
@ -80,24 +103,31 @@ public:
f_header = NewString("");
f_wrappers = NewString("");
/* generate shadow file if enabled */
/* generate shadow files if enabled */
if (shadow_flag) {
f_shadow = NewString("");
f_shadow_code = NewString("");
f_shadow_header = NewString("");
/* create shadow file with appropriate name */
String *shadow_filename = NewStringf("%s%s_proxy.c", SWIG_output_directory(), Char(module));
if ((f_shadow_c = NewFile(shadow_filename, "w")) == 0) {
FileErrorDisplay(shadow_filename);
String *shadow_code_filename = NewStringf("%s%s_proxy.c", SWIG_output_directory(), Char(module));
if ((f_shadow_c = NewFile(shadow_code_filename, "w")) == 0) {
FileErrorDisplay(shadow_code_filename);
SWIG_exit(EXIT_FAILURE);
}
Swig_register_filebyname("shadow", f_shadow);
String *shadow_header_filename = NewStringf("%s%s_proxy.h", SWIG_output_directory(), Char(module));
if ((f_shadow_h = NewFile(shadow_header_filename, "w")) == 0) {
FileErrorDisplay(shadow_header_filename);
SWIG_exit(EXIT_FAILURE);
}
Printf(f_shadow, "/* This file was automatically generated by SWIG (http://www.swig.org).\n");
Printf(f_shadow, " * Version %s\n", Swig_package_version());
Printf(f_shadow, " * \n");
Printf(f_shadow, " * Don't modify this file, modify the SWIG interface instead.\n");
Printf(f_shadow, " */\n\n");
Swig_register_filebyname("shadow_code", f_shadow_code);
Swig_register_filebyname("shadow_header", f_shadow_header);
emitBanner(f_shadow_code);
emitBanner(f_shadow_header);
emitMingwLinkFix(f_shadow_header);
Printf(f_shadow_code, "#include \"%s\"\n\n", shadow_header_filename);
}
Swig_register_filebyname("header", f_header);
@ -110,9 +140,12 @@ public:
/* finalize generating shadow file */
if (shadow_flag) {
Printv(f_shadow_c, f_shadow, "\n", NIL);
Printv(f_shadow_c, f_shadow_code, "\n", NIL);
Printv(f_shadow_h, f_shadow_header, "\n", NIL);
Close(f_shadow_c);
Delete(f_shadow);
Close(f_shadow_h);
Delete(f_shadow_code);
Delete(f_shadow_header);
}
/* write all to file */
@ -130,6 +163,19 @@ public:
return SWIG_OK;
}
virtual int globalvariableHandler(Node *n) {
if (shadow_flag) {
Printv(f_shadow_header, "DLL_IMPORT ", Getattr(n, "type"), " ", Getattr(n, "name"), NIL);
String *defval = NewString("");
defval = Getattr(n, "value");
if (defval) {
Printv(f_shadow_header, " = ", defval, NIL);
}
Printf(f_shadow_header, ";\n");
}
return SWIG_OK;
}
virtual int functionWrapper(Node *n) {
String *name = Getattr(n, "sym:name");
SwigType *type = Getattr(n, "type");
@ -148,6 +194,7 @@ public:
for (p = parms; p; ) {
np = nextSibling(p);
Printv(wrapper->def, Getattr(p, "type"), " ", Getattr(p, "lname"), np ? ", " : "", NIL);
Printf(stdout, "%s\n", Getattr(p, "tmap:argout"));
p = np;
}
Printv(wrapper->def, ") {", NIL);
@ -166,7 +213,6 @@ public:
if (shadow_flag) {
String *proto = ParmList_str(parms);
String *arg_names = NewString("");
Printv(f_shadow, type, " ", name, "(", proto, ") {\n", NIL);
Parm *p, *np;
for (p = parms; p; ) {
@ -175,6 +221,9 @@ public:
p = np;
}
Printv(f_shadow_code, "extern ", type, " _wrap_", name, "(", proto, ");\n", NIL);
Printv(f_shadow_code, type, " ", name, "(", proto, ") {\n", NIL);
/* handle 'prepend' feature */
String *prepend_str = Getattr(n, "feature:prepend");
if (prepend_str) {
@ -183,11 +232,27 @@ public:
Delitem(prepend_str, 0);
Delitem(prepend_str, DOH_END);
}
Printv(f_shadow, prepend_str, "\n", NIL);
Printv(f_shadow_code, prepend_str, "\n", NIL);
}
Printv(f_shadow, " return ", wname, "(", arg_names, ");\n", NIL);
Printv(f_shadow, "}\n", NIL);
/* call to the wrapper function */
Printv(f_shadow_code, " return ", wname, "(", arg_names, ");\n", NIL);
/* handle 'append' feature */
String *append_str = Getattr(n, "feature:append");
if (append_str) {
char *t = Char(append_str);
if (*t == '{') {
Delitem(append_str, 0);
Delitem(append_str, DOH_END);
}
Printv(f_shadow_code, append_str, "\n", NIL);
}
Printv(f_shadow_code, "}\n", NIL);
/* add function declaration to the proxy header file */
Printv(f_shadow_header, type, " ", name, "(", proto, ");\n");
}
Delete(wname);