Some support for overloading, simplified wrapping of pure C functions, some improvements in argument passing typemaps.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10653 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
125f362852
commit
f585c69d02
4 changed files with 346 additions and 183 deletions
|
|
@ -4,6 +4,13 @@ class A {
|
|||
public:
|
||||
int i;
|
||||
A(int i) : i(i) {}
|
||||
void foo() {
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
B(int i) : A(i) {}
|
||||
};
|
||||
|
||||
class MyClass {
|
||||
|
|
@ -18,7 +25,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void bar(int x, int* px, int* xs[5]) {
|
||||
void bar(int x, int* px, int** xs) {
|
||||
printf("x = %d\n", x);
|
||||
printf("*px = %d\n", *px);
|
||||
int i = 0;
|
||||
|
|
@ -28,5 +35,18 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void foo_1(A& a, A& b) {
|
||||
printf("a.i = %d\n", a.i);
|
||||
printf("b.i = %d\n", b.i);
|
||||
a.i++;
|
||||
b.i++;
|
||||
}
|
||||
|
||||
void foo_2(int& x, double& d) {
|
||||
printf("x = %d d = %f\n", x, d);
|
||||
x++;
|
||||
d++;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@
|
|||
#include "example_proxy.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
MyClass* mc = new_MyClass();
|
||||
A* a = new_A(123), *b = new_A(234), *as[5];
|
||||
int i, j = 321, *js[5];
|
||||
double d = 55.0;
|
||||
|
||||
for (i = 0; i < 4; ++i) {
|
||||
as[i] = new_A(20 + i);
|
||||
|
|
@ -18,9 +18,18 @@ int main(int argc, char** argv) {
|
|||
js[i] = 0;
|
||||
|
||||
MyClass_bar(mc, j, &j, js);
|
||||
MyClass_foo(mc, *a, b, as);
|
||||
MyClass_foo(mc, a, b, as);
|
||||
MyClass_foo_1(mc, a, b);
|
||||
MyClass_foo_1(mc, a, b);
|
||||
|
||||
MyClass_foo_2(mc, &j, &d);
|
||||
MyClass_foo_2(mc, &j, &d);
|
||||
|
||||
delete_A(a);
|
||||
delete_A(b);
|
||||
delete_MyClass(mc);
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
delete_A(as[i]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,25 +12,27 @@
|
|||
|
||||
%typemap(ctype) void, short, int, long, char, float, double "$1_type"
|
||||
%typemap(ctype) void*, short*, int*, long*, char*, float*, double* "$1_type"
|
||||
%typemap(ctype) short&, int&, long&, char&, float&, double& "$1_basetype *"
|
||||
%typemap(ctype) const short, const int, const long, const char, const float, const double "$1_basetype"
|
||||
%typemap(ctype) bool "_Bool"
|
||||
%typemap(ctype) SWIGTYPE "$1_type"
|
||||
%typemap(ctype) SWIGTYPE * "$1_type"
|
||||
%typemap(ctype) SWIGTYPE & "$1_type"
|
||||
%typemap(ctype) SWIGTYPE * [ANY] "$1_basetype*"
|
||||
%typemap(ctype) SWIGCLASSTYPE "struct Obj$1_type"
|
||||
%typemap(ctype) SWIGCLASSTYPE "struct Obj$1_type *"
|
||||
%typemap(ctype) SWIGCLASSTYPE * "struct Obj$1_type"
|
||||
%typemap(ctype) SWIGCLASSTYPE & "struct Obj$1_type"
|
||||
%typemap(ctype) SWIGCLASSTYPE & "struct Obj$*1_ltype *"
|
||||
%typemap(ctype) SWIGCLASSTYPE * [ANY] "struct Obj$1_basetype*"
|
||||
|
||||
%typemap(in) short, int, long, char, float, double, bool "$1 = ($1_basetype) $input;"
|
||||
%typemap(in) void*, short*, int*, long*, char*, float*, double*, bool* "$1 = ($1_basetype *) $input;"
|
||||
%typemap(in) short&, int&, long&, char&, float&, double&, bool& "$1 = ($1_basetype *) $input;"
|
||||
%typemap(in) SWIGTYPE "$1 = ($1_type) $input;"
|
||||
%typemap(in) SWIGTYPE * "$1 = ($1_type) $input;"
|
||||
%typemap(in) SWIGTYPE & "$1 = ($1_type) $input;"
|
||||
%typemap(in) SWIGTYPE * [ANY] "$1 = ($1_basetype*) $input;"
|
||||
%typemap(in) SWIGCLASSTYPE {
|
||||
$1 = * ($1_type *) ($input.obj);
|
||||
$1 = * ($1_type *) ($input->obj);
|
||||
}
|
||||
|
||||
%typemap(in) SWIGCLASSTYPE * {
|
||||
|
|
|
|||
|
|
@ -73,6 +73,11 @@ public:
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!CPlusPlus)
|
||||
runtime_flag = false;
|
||||
|
||||
allow_overloading();
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
|
|
@ -114,7 +119,6 @@ public:
|
|||
Printf(f_header, "}\n\n");
|
||||
}
|
||||
|
||||
|
||||
// generate shadow files if enabled
|
||||
if (shadow_flag) {
|
||||
f_shadow_code_init = NewString("");
|
||||
|
|
@ -192,7 +196,7 @@ public:
|
|||
virtual int globalvariableHandler(Node *n) {
|
||||
SwigType *type = Getattr(n, "type");
|
||||
String *type_str = SwigType_str(type, 0);
|
||||
// FIXME
|
||||
//if (!CPlusPlus)
|
||||
// Printv(f_wrappers, "SWIGEXPORTC ", type_str, " ", Getattr(n, "name"), ";\n", NIL);
|
||||
if (shadow_flag) {
|
||||
Printv(f_shadow_header, "SWIGIMPORT ", type_str, " ", Getattr(n, "name"), ";\n", NIL);
|
||||
|
|
@ -230,6 +234,54 @@ public:
|
|||
return SWIG_OK;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* prepend_feature()
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
||||
String* prepend_feature(Node *n) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
return (prepend_str ? prepend_str : empty_string);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* append_feature()
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
||||
String* append_feature(Node *n) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
return (append_str ? append_str : empty_string);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* getTypeSymbol()
|
||||
*
|
||||
* incomplete for now...
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
||||
const char* getTypeSymbol(String* type) {
|
||||
char* c = Char(type);
|
||||
if (strcmp(c, "int") == 0)
|
||||
return "i";
|
||||
if (strcmp(c, "double") == 0)
|
||||
return "d";
|
||||
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------
|
||||
* functionWrapper()
|
||||
* ---------------------------------------------------------------------- */
|
||||
|
|
@ -238,18 +290,67 @@ public:
|
|||
String *name = Getattr(n, "sym:name");
|
||||
SwigType *type = Getattr(n, "type");
|
||||
SwigType *return_type = NewString("");
|
||||
String *wname;
|
||||
String *arg_names = NewString("");
|
||||
ParmList *parms = Getattr(n, "parms");
|
||||
Parm *p;
|
||||
String *tm;
|
||||
String *proto = NewString("");
|
||||
bool is_void_return;
|
||||
String *over_suffix = NewString("");
|
||||
int gencomma;
|
||||
bool is_void_return = (SwigType_type(Getattr(n, "type")) == T_VOID);
|
||||
|
||||
// create new function wrapper object
|
||||
Wrapper *wrapper = NewWrapper();
|
||||
|
||||
if (!CPlusPlus) {
|
||||
// this is C function, we don't apply typemaps to it
|
||||
|
||||
// create new wrapper name
|
||||
String *wname = Swig_name_wrapper(name);
|
||||
wname = Swig_name_wrapper(name);
|
||||
Setattr(n, "wrap:name", wname);
|
||||
|
||||
// create function call
|
||||
arg_names = Swig_cfunction_call(empty_string, parms);
|
||||
if (arg_names) {
|
||||
Delitem(arg_names, 0);
|
||||
Delitem(arg_names, DOH_END);
|
||||
}
|
||||
return_type = SwigType_str(Getattr(n, "type"), 0);
|
||||
|
||||
// emit wrapper prototype and code
|
||||
gencomma = 0;
|
||||
for (p = parms; p; p = nextSibling(p)) {
|
||||
Printv(proto, gencomma ? ", " : "", SwigType_str(Getattr(p, "type"), 0), " ", Getattr(p, "lname"), NIL);
|
||||
gencomma = 1;
|
||||
}
|
||||
Printv(wrapper->def, return_type, " ", wname, "(", proto, ") {\n", NIL);
|
||||
Append(wrapper->code, prepend_feature(n));
|
||||
if (!is_void_return) {
|
||||
Printv(wrapper->code, return_type, " result;\n", NIL);
|
||||
Printf(wrapper->code, "result = ");
|
||||
}
|
||||
Printv(wrapper->code, name, "(", arg_names, ");\n", NIL);
|
||||
Append(wrapper->code, append_feature(n));
|
||||
if (!is_void_return)
|
||||
Printf(wrapper->code, "return result;\n");
|
||||
Printf(wrapper->code, "}");
|
||||
}
|
||||
else {
|
||||
// mangle name if functions is overloaded
|
||||
if (Getattr(n, "sym:overloaded")) {
|
||||
if (!Getattr(n, "copy_constructor")) {
|
||||
if (parms)
|
||||
Append(over_suffix, "_");
|
||||
for (p = parms; p; p = nextSibling(p)) {
|
||||
Append(over_suffix, getTypeSymbol(Getattr(p, "type")));
|
||||
}
|
||||
Append(name, over_suffix);
|
||||
}
|
||||
}
|
||||
|
||||
// create new wrapper name
|
||||
wname = Swig_name_wrapper(name);
|
||||
Setattr(n, "wrap:name", wname);
|
||||
|
||||
// attach the standard typemaps
|
||||
|
|
@ -276,9 +377,6 @@ public:
|
|||
// emit variables for holding parameters
|
||||
emit_parameter_variables(parms, wrapper);
|
||||
|
||||
// is the function void?
|
||||
is_void_return = (SwigType_type(Getattr(n, "type")) == T_VOID);
|
||||
|
||||
// add variable for holding result of original function
|
||||
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
|
||||
if (SwigType_isconst(type))
|
||||
|
|
@ -290,7 +388,7 @@ public:
|
|||
Printv(wrapper->def, "SWIGEXPORTC ", return_type, " ", wname, "(", NIL);
|
||||
|
||||
// prepare function definition
|
||||
int gencomma = 0;
|
||||
gencomma = 0;
|
||||
for (p = parms; p; ) {
|
||||
|
||||
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
|
||||
|
|
@ -383,7 +481,7 @@ public:
|
|||
Append(wrapper->code, "return result;\n");
|
||||
|
||||
Append(wrapper->code, "}\n");
|
||||
Wrapper_print(wrapper, f_wrappers);
|
||||
}
|
||||
|
||||
// take care of shadow function
|
||||
if (shadow_flag) {
|
||||
|
|
@ -397,38 +495,17 @@ public:
|
|||
Printv(f_shadow_code_init, "extern ", return_type, " ", wname, "(", proto, ");\n", NIL);
|
||||
Printv(f_shadow_code_body, return_type, " ", name, "(", proto, ") {\n", NIL);
|
||||
|
||||
// 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_code_body, prepend_str, "\n", NIL);
|
||||
}
|
||||
|
||||
// call to the wrapper function
|
||||
Printv(f_shadow_code_body, " 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_body, append_str, "\n", NIL);
|
||||
}
|
||||
|
||||
Printv(f_shadow_code_body, "}\n", NIL);
|
||||
Printv(f_shadow_code_body, " return ", wname, "(", arg_names, ");\n}\n", NIL);
|
||||
|
||||
// add function declaration to the proxy header file
|
||||
Printv(f_shadow_header, return_type, " ", name, "(", proto, ");\n");
|
||||
}
|
||||
|
||||
Wrapper_print(wrapper, f_wrappers);
|
||||
|
||||
// cleanup
|
||||
Delete(over_suffix);
|
||||
Delete(proto);
|
||||
Delete(arg_names);
|
||||
Delete(wname);
|
||||
|
|
@ -471,6 +548,21 @@ public:
|
|||
return new_node;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* copy_node()
|
||||
*
|
||||
* tests if given name already exists in one of child nodes of n
|
||||
* --------------------------------------------------------------------- */
|
||||
|
||||
bool is_in(String* name, Node* n) {
|
||||
Hash* h;
|
||||
for (h = firstChild(n); h; h = nextSibling(h)) {
|
||||
if (Cmp(name, Getattr(h, "name")) == 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* classHandler()
|
||||
* --------------------------------------------------------------------- */
|
||||
|
|
@ -481,24 +573,36 @@ public:
|
|||
List* baselist = Getattr(n, "bases");
|
||||
Replaceall(name, "::", "_");
|
||||
|
||||
if (CPlusPlus) {
|
||||
// inheritance support: attach all members from base classes to this class
|
||||
if (baselist) {
|
||||
Iterator i;
|
||||
for (i = First(baselist); i.item; i = Next(i)) {
|
||||
// look for member variables (TODO: support for other constructs)
|
||||
// look for member variables and functions
|
||||
Node* node;
|
||||
for (node = firstChild(i.item); node; node = nextSibling(node)) {
|
||||
if ((Cmp(Getattr(node, "kind"), "variable") == 0)
|
||||
&& (Cmp(Getattr(node, "access"), "private") != 0)
|
||||
|| (Cmp(Getattr(node, "kind"), "function") == 0)) {
|
||||
if ((Cmp(Getattr(node, "access"), "public") == 0)
|
||||
&& (Cmp(Getattr(node, "storage"), "static") != 0)) {
|
||||
if (!is_in(Getattr(node, "name"), n)) {
|
||||
Node* new_node = copy_node(node);
|
||||
Setattr(new_node, "sym:name", Getattr(new_node, "name"));
|
||||
set_nodeType(new_node, "cdecl");
|
||||
// make sure there are no copyied object-structs params
|
||||
ParmList* all_parms = Getattr(new_node, "parms"), * parms;
|
||||
if (Getattr(all_parms, "c:objstruct")) {
|
||||
parms = Copy(nextSibling(all_parms));
|
||||
Delete(all_parms);
|
||||
Setattr(new_node, "parms", parms);
|
||||
}
|
||||
appendChild(n, new_node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// emit "class"-struct definition
|
||||
Printv(sobj, "struct Obj", name, " {\n void* obj;\n", NIL);
|
||||
|
|
@ -511,14 +615,35 @@ public:
|
|||
Printv(f_header, "const char* __typename_", name, " = \"", name, "\";\n\n", NIL);
|
||||
|
||||
// declare it in the proxy header
|
||||
if (shadow_flag) {
|
||||
if (shadow_flag)
|
||||
Printv(f_shadow_header, "typedef ", sobj, " ", name, ";\n\n", NIL);
|
||||
}
|
||||
|
||||
Delete(sobj);
|
||||
Delete(name);
|
||||
return Language::classHandler(n);
|
||||
}
|
||||
else if (Cmp(Getattr(n, "kind"), "struct") == 0) {
|
||||
// this is C struct, just declare it in proxy
|
||||
if (shadow_flag) {
|
||||
Printv(f_shadow_header, "struct ", name, " {\n", NIL);
|
||||
Node* node;
|
||||
for (node = firstChild(n); node; node = nextSibling(node)) {
|
||||
String* kind = Getattr(node, "kind");
|
||||
if ((Cmp(kind, "variable") == 0) || (Cmp(kind, "function") == 0)) {
|
||||
String* type = NewString("");
|
||||
Printv(type, Getattr(node, "decl"), Getattr(node, "type"), NIL);
|
||||
Printv(f_shadow_header, " ", SwigType_str(type, 0), " ", Getattr(node, "name"), ";\n", NIL);
|
||||
Delete(type);
|
||||
}
|
||||
}
|
||||
Append(f_shadow_header, "};\n\n");
|
||||
}
|
||||
|
||||
Delete(sobj);
|
||||
Delete(name);
|
||||
}
|
||||
return SWIG_OK;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------
|
||||
* staticmemberfunctionHandler()
|
||||
|
|
@ -594,7 +719,9 @@ public:
|
|||
Append(arg_lnames, Swig_cfunction_call(empty_string, parms));
|
||||
|
||||
// omit first argument in method call
|
||||
String* arg_call_lnames = Strstr(arg_lnames, "arg2");
|
||||
String* arg_call_lnames = Strstr(arg_lnames, "*arg2");
|
||||
if (!arg_call_lnames)
|
||||
arg_call_lnames = Strstr(arg_lnames, "arg2");
|
||||
if (!arg_call_lnames)
|
||||
arg_call_lnames = empty_string;
|
||||
else
|
||||
|
|
@ -795,6 +922,9 @@ public:
|
|||
* --------------------------------------------------------------------- */
|
||||
|
||||
virtual int constructorHandler(Node* n) {
|
||||
if (Getattr(n, "copy_constructor"))
|
||||
return copyconstructorHandler(n);
|
||||
|
||||
String* classname = Getattr(parentNode(n), "name");
|
||||
String* newclassname = Copy(classname);
|
||||
String* sobj_name = NewString("");
|
||||
|
|
@ -822,6 +952,7 @@ public:
|
|||
|
||||
// modify the constructor name
|
||||
Printv(constr_name, "new_", newclassname, NIL);
|
||||
Setattr(n, "name", constr_name);
|
||||
Setattr(n, "sym:name", constr_name);
|
||||
|
||||
// generate action code
|
||||
|
|
@ -873,6 +1004,7 @@ public:
|
|||
|
||||
// modify the constructor name
|
||||
Printv(constr_name, "copy_", newclassname, NIL);
|
||||
Setattr(n, "name", constr_name);
|
||||
Setattr(n, "sym:name", constr_name);
|
||||
|
||||
// generate action code
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue