Modified parameter handling using typemaps. 'Reference' example. Visibility hint now applies only to the global functions.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10603 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
cdd920a6ca
commit
f84342a301
13 changed files with 205 additions and 36 deletions
|
|
@ -1105,11 +1105,9 @@ c: $(SRCS)
|
||||||
$(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES)
|
$(CC) -c $(CCSHARED) $(CFLAGS) $(ISRCS) $(SRCS) $(INCLUDES)
|
||||||
$(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(CLIBPREFIX)$(TARGET)$(SO)
|
$(LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(CLIBPREFIX)$(TARGET)$(SO)
|
||||||
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(TARGET)
|
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(TARGET)
|
||||||
LD_LIBRARY_PATH=`pwd`:LD_LIBRARY_PATH; export LD_LIBRARY_PATH
|
|
||||||
|
|
||||||
c_cpp: $(SRCS)
|
c_cpp: $(SRCS)
|
||||||
$(SWIG) -c++ -c $(SWIGOPT) $(INTERFACE)
|
$(SWIG) -c++ -c $(SWIGOPT) $(INTERFACE)
|
||||||
$(CXX) -c $(CCSHARED) $(CXXFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES)
|
$(CXX) -c $(CCSHARED) $(CXXFLAGS) $(ICXXSRCS) $(SRCS) $(CXXSRCS) $(INCLUDES)
|
||||||
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(CLIBPREFIX)$(TARGET)$(SO)
|
$(CXXSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(CLIBPREFIX)$(TARGET)$(SO)
|
||||||
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(TARGET)
|
$(CC) $(MAIN) $(TARGET)_proxy.c -L. -l$(TARGET)
|
||||||
LD_LIBRARY_PATH=`pwd`:LD_LIBRARY_PATH; export LD_LIBRARY_PATH
|
|
||||||
|
|
|
||||||
|
|
@ -10,5 +10,5 @@ all::
|
||||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c_cpp
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c_cpp
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.out *.so *.a
|
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
|
||||||
|
|
||||||
|
|
|
||||||
14
Examples/c/reference/Makefile
Normal file
14
Examples/c/reference/Makefile
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
TOP = ../..
|
||||||
|
SWIG = $(TOP)/../preinst-swig -debug-module 4
|
||||||
|
CXXSRCS = example.cxx
|
||||||
|
TARGET = example
|
||||||
|
INTERFACE = example.i
|
||||||
|
MAIN = main.c
|
||||||
|
|
||||||
|
all::
|
||||||
|
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||||
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c_cpp
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
|
||||||
|
|
||||||
19
Examples/c/reference/example.cxx
Normal file
19
Examples/c/reference/example.cxx
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "example.h"
|
||||||
|
|
||||||
|
void foo_by_val(Bar foo) {
|
||||||
|
foo.set(123);
|
||||||
|
printf("inside foo_by_val: %d\n", foo.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo_by_ref(Bar& foo) {
|
||||||
|
foo.set(123);
|
||||||
|
printf("inside foo_by_ref: %d\n", foo.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo_by_ptr(Bar* foo) {
|
||||||
|
foo->set(123);
|
||||||
|
printf("inside foo_by_ptr: %d\n", foo->get());
|
||||||
|
}
|
||||||
|
|
||||||
16
Examples/c/reference/example.h
Normal file
16
Examples/c/reference/example.h
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
private:
|
||||||
|
int x;
|
||||||
|
public:
|
||||||
|
Bar() : x(0) {}
|
||||||
|
~Bar() {}
|
||||||
|
void set(int x) { this->x = x; }
|
||||||
|
int get() { return x; }
|
||||||
|
};
|
||||||
|
|
||||||
|
/*void foo_by_val(Bar bar);
|
||||||
|
void foo_by_ref(Bar& bar);*/
|
||||||
|
void foo_by_ptr(Bar* bar);
|
||||||
|
|
||||||
8
Examples/c/reference/example.i
Normal file
8
Examples/c/reference/example.i
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
12
Examples/c/reference/main.c
Normal file
12
Examples/c/reference/main.c
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "example_proxy.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
Bar * bar = new_Bar();
|
||||||
|
foo_by_ptr(bar);
|
||||||
|
|
||||||
|
delete_Bar(bar);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
TOP = ../..
|
TOP = ../..
|
||||||
SWIG = $(TOP)/../preinst-swig
|
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
|
||||||
SRCS = example.c
|
SRCS = example.c
|
||||||
TARGET = example
|
TARGET = example
|
||||||
INTERFACE = example.i
|
INTERFACE = example.i
|
||||||
|
|
@ -10,5 +10,5 @@ all::
|
||||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c
|
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.so *.out *.a *~
|
rm -f *.o *.so *.out *.a *.exe *.dll *_wrap* *_proxy* *~
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,5 +22,3 @@ int gcd(int x, int y) {
|
||||||
}
|
}
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,10 @@
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
%typemap(in) double {
|
||||||
|
/* hello */
|
||||||
|
}
|
||||||
|
|
||||||
/* Let's just grab the original header file here */
|
/* Let's just grab the original header file here */
|
||||||
%include "example.h"
|
%include "example.h"
|
||||||
|
|
||||||
|
|
|
||||||
22
Lib/c/c.swg
22
Lib/c/c.swg
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* -----------------------------------------------------------------------------
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* c.swg
|
||||||
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
%typemap(ctype) short "short"
|
||||||
|
%typemap(ctype) int "int"
|
||||||
|
%typemap(ctype) long "long"
|
||||||
|
%typemap(ctype) char "char"
|
||||||
|
%typemap(ctype) float "float"
|
||||||
|
%typemap(ctype) double "double"
|
||||||
|
%typemap(ctype) bool "_Bool"
|
||||||
|
%typemap(ctype) SWIGTYPE "struct $*1_typeObj *"
|
||||||
|
|
||||||
|
%typemap(in) short, int, long, char, float, double, bool "$1 = $input;"
|
||||||
|
|
||||||
|
%typemap(in) SWIGTYPE {
|
||||||
|
$1 = ($1_type) $input->obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -252,6 +252,10 @@
|
||||||
|
|
||||||
/* please leave 870-889 free for Php */
|
/* please leave 870-889 free for Php */
|
||||||
|
|
||||||
|
#define WARN_C_TYPEMAP_CTYPE_UNDEF 890
|
||||||
|
|
||||||
|
/* please leave 890-909 free for C */
|
||||||
|
|
||||||
|
|
||||||
/* Feel free to claim any number in this space that's not currently being used. Just make sure you
|
/* Feel free to claim any number in this space that's not currently being used. Just make sure you
|
||||||
add an entry here */
|
add an entry here */
|
||||||
|
|
|
||||||
|
|
@ -72,12 +72,12 @@ public:
|
||||||
void emitSwigProtectSymbols(File *f) {
|
void emitSwigProtectSymbols(File *f) {
|
||||||
Printf(f, "#ifndef SWIGPROTECT\n");
|
Printf(f, "#ifndef SWIGPROTECT\n");
|
||||||
Printf(f, "# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n");
|
Printf(f, "# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)\n");
|
||||||
Printf(f, "# define SWIGPROTECT //\n");
|
Printf(f, "# define SWIGPROTECT(x)\n");
|
||||||
Printf(f, "# else\n");
|
Printf(f, "# else\n");
|
||||||
Printf(f, "# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)\n");
|
Printf(f, "# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)\n");
|
||||||
Printf(f, "# define SWIGPROTECT __attribute__ ((visibility(\"protected\")))\n");
|
Printf(f, "# define SWIGPROTECT(x) __attribute__ ((visibility(\"protected\"))) x\n");
|
||||||
Printf(f, "# else\n");
|
Printf(f, "# else\n");
|
||||||
Printf(f, "# define SWIGPROTECT //\n");
|
Printf(f, "# define SWIGPROTECT(x)\n");
|
||||||
Printf(f, "# endif\n");
|
Printf(f, "# endif\n");
|
||||||
Printf(f, "# endif\n");
|
Printf(f, "# endif\n");
|
||||||
Printf(f, "#endif\n\n");
|
Printf(f, "#endif\n\n");
|
||||||
|
|
@ -226,6 +226,26 @@ public:
|
||||||
return SWIG_OK;
|
return SWIG_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -----------------------------------------------------------------------
|
||||||
|
* globalfunctionHandler()
|
||||||
|
* ------------------------------------------------------------------------ */
|
||||||
|
|
||||||
|
virtual int globalfunctionHandler(Node *n ) {
|
||||||
|
String* vis_hint = NewString("");
|
||||||
|
String* return_type_str = SwigType_str(Getattr(n, "type"), 0);
|
||||||
|
String* name = Getattr(n, "sym:name");
|
||||||
|
ParmList* parms = Getattr(n, "parms");
|
||||||
|
|
||||||
|
Language::globalfunctionHandler(n);
|
||||||
|
|
||||||
|
// add visibility hint for the compiler (do not override this symbol)
|
||||||
|
Printv(vis_hint, "SWIGPROTECT(", return_type_str, " ", name, "(", ParmList_str(parms), ");)\n\n", NIL);
|
||||||
|
Printv(f_wrappers, vis_hint, NIL);
|
||||||
|
|
||||||
|
Delete(vis_hint);
|
||||||
|
return SWIG_OK;
|
||||||
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* functionWrapper()
|
* functionWrapper()
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
@ -236,6 +256,8 @@ public:
|
||||||
String *return_type_str = SwigType_str(return_type, 0);
|
String *return_type_str = SwigType_str(return_type, 0);
|
||||||
String *arg_names = NewString("");
|
String *arg_names = NewString("");
|
||||||
ParmList *parms = Getattr(n, "parms");
|
ParmList *parms = Getattr(n, "parms");
|
||||||
|
Parm *p;
|
||||||
|
String* tm;
|
||||||
|
|
||||||
// create new function wrapper object
|
// create new function wrapper object
|
||||||
Wrapper *wrapper = NewWrapper();
|
Wrapper *wrapper = NewWrapper();
|
||||||
|
|
@ -250,16 +272,66 @@ public:
|
||||||
// attach the standard typemaps
|
// attach the standard typemaps
|
||||||
emit_attach_parmmaps(parms, wrapper);
|
emit_attach_parmmaps(parms, wrapper);
|
||||||
|
|
||||||
// prepare parameter list
|
// attach 'ctype' typemaps
|
||||||
Parm *p, *np;
|
Swig_typemap_attach_parms("ctype", parms, wrapper);
|
||||||
|
|
||||||
|
Setattr(n, "wrap:parms", parms);
|
||||||
|
|
||||||
|
// emit variables for holding parameters
|
||||||
|
emit_parameter_variables(parms, wrapper);
|
||||||
|
|
||||||
|
// prepare function definition
|
||||||
|
int gencomma = 0;
|
||||||
for (p = parms; p; ) {
|
for (p = parms; p; ) {
|
||||||
np = nextSibling(p);
|
|
||||||
SwigType *type = Getattr(p, "type");
|
while (checkAttribute(p, "tmap:in:numinputs", "0")) {
|
||||||
Printv(wrapper->def, SwigType_str(type, 0), " ", Getattr(p, "lname"), np ? ", " : "", NIL);
|
p = Getattr(p, "tmap:in:next");
|
||||||
Printv(arg_names, Getattr(p, "name"), np ? ", " : "", NIL);
|
}
|
||||||
p = np;
|
|
||||||
|
SwigType* type = Getattr(p, "type");
|
||||||
|
String* lname = Getattr(p, "lname");
|
||||||
|
String* c_parm_type = NewString("");
|
||||||
|
String* arg_name = NewString("");
|
||||||
|
|
||||||
|
Printf(arg_name, "c%s", lname);
|
||||||
|
|
||||||
|
if ((tm = Getattr(p, "tmap:ctype"))) {
|
||||||
|
if (Cmp(Getattr(p, "c:immutable"), "1") == 0) {
|
||||||
|
Printv(c_parm_type, SwigType_str(type, 0), NIL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Printv(c_parm_type, tm, NIL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(type, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
Printv(arg_names, gencomma ? ", " : "", Getattr(p, "name"), NIL);
|
||||||
|
Printv(wrapper->def, gencomma ? ", " : "", c_parm_type, " ", arg_name, NIL);
|
||||||
|
gencomma = 1;
|
||||||
|
|
||||||
|
if ((tm = Getattr(p, "tmap:in"))) {
|
||||||
|
if (Cmp(Getattr(p, "c:immutable"), "1") == 0) {
|
||||||
|
// FIXME
|
||||||
|
Printv(wrapper->code, lname, " = ", arg_name, ";\n", NIL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Replaceall(tm, "$input", arg_name);
|
||||||
|
Setattr(p, "emit:input", arg_name);
|
||||||
|
Printf(wrapper->code, "%s\n", tm);
|
||||||
|
}
|
||||||
|
p = Getattr(p, "tmap:in:next");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(type, 0));
|
||||||
|
p = nextSibling(p);
|
||||||
|
}
|
||||||
|
Delete(arg_name);
|
||||||
|
Delete(c_parm_type);
|
||||||
}
|
}
|
||||||
Printv(wrapper->def, ") {", NIL);
|
|
||||||
|
Printf(wrapper->def, ") {");
|
||||||
|
|
||||||
// declare wrapper function local variables
|
// declare wrapper function local variables
|
||||||
emit_return_variable(n, return_type, wrapper);
|
emit_return_variable(n, return_type, wrapper);
|
||||||
|
|
@ -277,19 +349,24 @@ public:
|
||||||
if (shadow_flag) {
|
if (shadow_flag) {
|
||||||
// use shadow-type for parameter if supplied
|
// use shadow-type for parameter if supplied
|
||||||
String* proto;
|
String* proto;
|
||||||
String* stype = Getattr(parms, "stype");
|
if (parms) {
|
||||||
if (stype) {
|
String* stype = Getattr(parms, "c:stype");
|
||||||
Swig_save("temp", parms, "type", NIL);
|
if (stype) {
|
||||||
Setattr(parms, "type", stype);
|
Swig_save("temp", parms, "type", NIL);
|
||||||
proto = ParmList_str(parms);
|
Setattr(parms, "type", stype);
|
||||||
Swig_restore(parms);
|
proto = ParmList_str(parms);
|
||||||
|
Swig_restore(parms);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
proto = ParmList_str(parms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
proto = ParmList_str(parms);
|
proto = empty_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
// use shadow-type for return type if supplied
|
// use shadow-type for return type if supplied
|
||||||
SwigType* shadow_type = Getattr(n, "stype");
|
SwigType* shadow_type = Getattr(n, "c:stype");
|
||||||
if (shadow_type) {
|
if (shadow_type) {
|
||||||
return_type_str = SwigType_str(shadow_type, 0);
|
return_type_str = SwigType_str(shadow_type, 0);
|
||||||
}
|
}
|
||||||
|
|
@ -329,12 +406,6 @@ public:
|
||||||
Printv(f_shadow_header, return_type_str, " ", name, "(", proto, ");\n");
|
Printv(f_shadow_header, return_type_str, " ", name, "(", proto, ");\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// add visibility hint for the compiler
|
|
||||||
String* vis_hint = NewString("");
|
|
||||||
Printv(vis_hint, "SWIGPROTECT ", return_type_str, " ", name, "(", ParmList_str(parms), ");\n", NIL);
|
|
||||||
Printv(f_init, vis_hint, NIL);
|
|
||||||
Delete(vis_hint);
|
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
Delete(arg_names);
|
Delete(arg_names);
|
||||||
Delete(wname);
|
Delete(wname);
|
||||||
|
|
@ -395,7 +466,8 @@ public:
|
||||||
Parm* p = NewParm(ctype, "self");
|
Parm* p = NewParm(ctype, "self");
|
||||||
stype = Copy(classname);
|
stype = Copy(classname);
|
||||||
SwigType_add_pointer(stype);
|
SwigType_add_pointer(stype);
|
||||||
Setattr(p, "stype", stype);
|
Setattr(p, "c:stype", stype);
|
||||||
|
Setattr(p, "c:immutable", "1");
|
||||||
if (parms)
|
if (parms)
|
||||||
set_nextSibling(p, parms);
|
set_nextSibling(p, parms);
|
||||||
Setattr(n, "parms", p);
|
Setattr(n, "parms", p);
|
||||||
|
|
@ -461,7 +533,8 @@ public:
|
||||||
Parm* p = NewParm(ctype, "self");
|
Parm* p = NewParm(ctype, "self");
|
||||||
stype = Copy(classname);
|
stype = Copy(classname);
|
||||||
SwigType_add_pointer(stype);
|
SwigType_add_pointer(stype);
|
||||||
Setattr(p, "stype", stype);
|
Setattr(p, "c:stype", stype);
|
||||||
|
Setattr(p, "c:immutable", "1");
|
||||||
Setattr(p, "lname", "arg1");
|
Setattr(p, "lname", "arg1");
|
||||||
|
|
||||||
// create second argument
|
// create second argument
|
||||||
|
|
@ -533,7 +606,7 @@ public:
|
||||||
Setattr(n, "type", ctype);
|
Setattr(n, "type", ctype);
|
||||||
stype = Copy(name);
|
stype = Copy(name);
|
||||||
SwigType_add_pointer(stype);
|
SwigType_add_pointer(stype);
|
||||||
Setattr(n, "stype", stype);
|
Setattr(n, "c:stype", stype);
|
||||||
|
|
||||||
// generate action code
|
// generate action code
|
||||||
Printv(code, "result = (", sobj_name, "*) malloc(sizeof(", sobj_name, "));\n", NIL);
|
Printv(code, "result = (", sobj_name, "*) malloc(sizeof(", sobj_name, "));\n", NIL);
|
||||||
|
|
@ -576,7 +649,8 @@ public:
|
||||||
Setattr(p, "lname", "arg1");
|
Setattr(p, "lname", "arg1");
|
||||||
stype = Copy(name);
|
stype = Copy(name);
|
||||||
SwigType_add_pointer(stype);
|
SwigType_add_pointer(stype);
|
||||||
Setattr(p, "stype", stype);
|
Setattr(p, "c:stype", stype);
|
||||||
|
Setattr(p, "c:immutable", "1");
|
||||||
Setattr(n, "parms", p);
|
Setattr(n, "parms", p);
|
||||||
Setattr(n, "type", "void");
|
Setattr(n, "type", "void");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue