Experimenting with global function wrapping, generating C proxy file, 'prepend' feature.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10512 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2008-05-30 16:17:24 +00:00
commit a36f790271
3 changed files with 226 additions and 161 deletions

View file

@ -1102,6 +1102,6 @@ c: $(SRCS)
$(SWIG) -c $(SWIGOPT) $(INTERFACE) $(SWIG) -c $(SWIGOPT) $(INTERFACE)
$(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES) $(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES)
$(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(LIBPREFIX)_$(TARGET)$(SO) $(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(LIBPREFIX)_$(TARGET)$(SO)
$(CC) $(EXEC) -L. -l$(LIBPREFIX)_$(TARGET) $(CC) $(EXEC) $(TARGET)_proxy.c -L. -l$(LIBPREFIX)_$(TARGET)

View file

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

View file

@ -1,158 +1,220 @@
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* See the LICENSE file for information on copyright, usage and redistribution * See the LICENSE file for information on copyright, usage and redistribution
* of SWIG, and the README file for authors - http://www.swig.org/release.html. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
* *
* c.cxx * c.cxx
* *
* C language module for SWIG. * C language module for SWIG.
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
char cvsroot_c_cxx[] = "$Id: c.cxx 10452 2008-05-15 21:16:48Z wsfulton $"; char cvsroot_c_cxx[] = "$Id$";
#include "swigmod.h" #include "swigmod.h"
class C:public Language { class C:public Language {
static const char *usage; static const char *usage;
File *f_runtime; File *f_runtime;
File *f_runtime_h; File *f_header;
File *f_header; File *f_wrappers;
File *f_wrappers; File *f_init;
File *f_init; File *f_shadow_c;
public: String *f_shadow;
/* ----------------------------------------------------------------------------- bool shadow_flag;
* C()
* ----------------------------------------------------------------------------- */ public:
C() { /* -----------------------------------------------------------------------------
} * C()
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* main() C() : shadow_flag(true) {
* ------------------------------------------------------------ */ }
virtual void main(int argc, char *argv[]) { /* ------------------------------------------------------------
* main()
SWIG_library_directory("c"); * ------------------------------------------------------------ */
// Add a symbol to the parser for conditional compilation virtual void main(int argc, char *argv[]) {
Preprocessor_define("SWIGC 1", 0);
SWIG_library_directory("c");
// Add typemap definitions
SWIG_typemap_lang("c"); // Add a symbol to the parser for conditional compilation
SWIG_config_file("c.swg"); Preprocessor_define("SWIGC 1", 0);
// Look for certain command line options // Add typemap definitions
for (int i = 1; i < argc; i++) { SWIG_typemap_lang("c");
if (argv[i]) { SWIG_config_file("c.swg");
if (strcmp(argv[i], "-help") == 0) {
Printf(stdout, "%s\n", usage); // Look for certain command line options
} for (int i = 1; i < argc; i++) {
} if (argv[i]) {
} if (strcmp(argv[i], "-help") == 0) {
} Printf(stdout, "%s\n", usage);
} else if ((strcmp(argv[i], "-shadow") == 0) || (strcmp(argv[i], "-proxy") == 0)) {
/* --------------------------------------------------------------------- shadow_flag = true;
* top() } else if (strcmp(argv[i], "-noproxy") == 0) {
* --------------------------------------------------------------------- */ shadow_flag = false;
}
virtual int top(Node *n) { }
Printf(stdout, "top called\n"); }
}
String *module = Getattr(n, "name");
String *outfile = Getattr(n, "outfile"); /* ---------------------------------------------------------------------
* top()
/* initialize I/O */ * --------------------------------------------------------------------- */
f_runtime = NewFile(outfile, "w");
if (!f_runtime) { virtual int top(Node *n) {
FileErrorDisplay(outfile); String *module = Getattr(n, "name");
SWIG_exit(EXIT_FAILURE); String *outfile = Getattr(n, "outfile");
}
f_init = NewString(""); /* initialize I/O */
f_header = NewString(""); f_runtime = NewFile(outfile, "w");
f_wrappers = NewString(""); if (!f_runtime) {
FileErrorDisplay(outfile);
Swig_register_filebyname("header", f_header); SWIG_exit(EXIT_FAILURE);
Swig_register_filebyname("wrappers", f_wrappers); }
Swig_register_filebyname("runtime", f_runtime); f_init = NewString("");
Swig_register_filebyname("init", f_init); f_header = NewString("");
f_wrappers = NewString("");
/* emit code for children */
Language::top(n); /* generate shadow file if enabled */
if (shadow_flag) {
/* write all to file */ f_shadow = NewString("");
Dump(f_header, f_runtime);
Dump(f_wrappers, f_runtime); /* create shadow file with appropriate name */
Wrapper_pretty_print(f_init, f_runtime); String *shadow_filename = NewStringf("%s%s_proxy.c", SWIG_output_directory(), Char(module));
if ((f_shadow_c = NewFile(shadow_filename, "w")) == 0) {
/* cleanup */ FileErrorDisplay(shadow_filename);
Delete(f_header); SWIG_exit(EXIT_FAILURE);
Delete(f_wrappers); }
Delete(f_init);
Close(f_runtime); Swig_register_filebyname("shadow", f_shadow);
Delete(f_runtime);
Printf(f_shadow, "/* This file was automatically generated by SWIG (http://www.swig.org).\n");
return SWIG_OK; 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");
virtual int functionWrapper(Node *n) { Printf(f_shadow, " */\n\n");
Printf(stdout, "creating function wrapper\n"); }
String *name = Getattr(n, "sym:name");
String *type = Getattr(n, "type"); Swig_register_filebyname("header", f_header);
ParmList *parms = Getattr(n, "parms"); Swig_register_filebyname("wrappers", f_wrappers);
String *wrapname = Getattr(n, "wrap:name"); Swig_register_filebyname("runtime", f_runtime);
Swig_register_filebyname("init", f_init);
Wrapper *wrapper = NewWrapper();
String *wname = Swig_name_wrapper(name); /* emit code for children */
Language::top(n);
Setattr(n, "wrap:name", wname);
/* finalize generating shadow file */
String *parse_args = NewString(""); if (shadow_flag) {
Parm *parm; Printv(f_shadow_c, f_shadow, "\n", NIL);
for (parm = parms; parm; ) { Close(f_shadow_c);
Append(parse_args, Getattr(parm, "type")); Delete(f_shadow);
Append(parse_args, " "); }
Append(parse_args, Getattr(parm, "lname"));
parm = nextSibling(parm); /* write all to file */
if (parm) { Dump(f_header, f_runtime);
Append(parse_args, ", "); Dump(f_wrappers, f_runtime);
} Wrapper_pretty_print(f_init, f_runtime);
}
/* cleanup */
Printv(wrapper->def, type, " ", wname, "(", parse_args, ") {", NIL); Delete(f_header);
Printv(wrapper->code, Getattr(n, "type"), " result;\n", NIL); Delete(f_wrappers);
Printv(wrapper->code, Getattr(n, "wrap:action"), "\n", NIL); Delete(f_init);
Printv(wrapper->code, "return result;\n}\n", NIL); Close(f_runtime);
Delete(f_runtime);
Wrapper_print(wrapper, f_wrappers);
return SWIG_OK;
Delete(wname); }
DelWrapper(wrapper);
virtual int functionWrapper(Node *n) {
return SWIG_OK; String *name = Getattr(n, "sym:name");
} SwigType *type = Getattr(n, "type");
ParmList *parms = Getattr(n, "parms");
}; /* class C */
/* create new wrapper name */
/* ----------------------------------------------------------------------------- Wrapper *wrapper = NewWrapper();
* swig_c() - Instantiate module String *wname = Swig_name_wrapper(name);
* ----------------------------------------------------------------------------- */ Setattr(n, "wrap:name", wname);
static Language *new_swig_c() { /* create wrapper function prototype */
return new C(); Printv(wrapper->def, type, " ", wname, "(", NIL);
}
/* prepare parameter list */
extern "C" Language *swig_c(void) { Parm *p, *np;
return new_swig_c(); for (p = parms; p; ) {
} np = nextSibling(p);
Printv(wrapper->def, Getattr(p, "type"), " ", Getattr(p, "lname"), np ? ", " : "", NIL);
/* ----------------------------------------------------------------------------- p = np;
* Static member variables }
* ----------------------------------------------------------------------------- */ Printv(wrapper->def, ") {", NIL);
const char *C::usage = (char *) "\ /* declare wrapper function local variables */
C Options (available with -c)\n\ emit_return_variable(n, type, wrapper);
\n";
/* emit action code */
String *action = emit_action(n);
Append(wrapper->code, action);
Append(wrapper->code, "return result;\n}\n");
Wrapper_print(wrapper, f_wrappers);
/* take care of shadow function */
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; ) {
np = nextSibling(p);
Printv(arg_names, Getattr(p, "name"), np ? ", " : "", NIL);
p = np;
}
/* handle 'prepend' feature */
String *prepend_str = Getattr(n, "feature:prepend");
if (prepend_str) {
char *t = Char(prepend_str);
if (*t == '{') {
Delitem(prepend_str, 0);
Delitem(prepend_str, DOH_END);
}
Printv(f_shadow, prepend_str, "\n", NIL);
}
Printv(f_shadow, " return ", wname, "(", arg_names, ");\n", NIL);
Printv(f_shadow, "}\n", NIL);
}
Delete(wname);
DelWrapper(wrapper);
return SWIG_OK;
}
}; /* class C */
/* -----------------------------------------------------------------------------
* swig_c() - Instantiate module
* ----------------------------------------------------------------------------- */
static Language *new_swig_c() {
return new C();
}
extern "C" Language *swig_c(void) {
return new_swig_c();
}
/* -----------------------------------------------------------------------------
* Static member variables
* ----------------------------------------------------------------------------- */
const char *C::usage = (char *) "\
C Options (available with -c)\n\
\n";