Fixed some issues with member variables handling

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@11155 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2009-03-18 15:04:20 +00:00
commit 5002c69cd6
9 changed files with 115 additions and 61 deletions

View file

@ -1098,17 +1098,19 @@ r_clean:
# ---------------------------------------------------------------- # ----------------------------------------------------------------
CLIBPREFIX = lib CLIBPREFIX = lib
C_LDSHARED = @C_LDSHARED@
CXX_LDSHARED = @CXX_LDSHARED@
C_SO = @C_SO@
c: $(SRCS) 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 $(CLIBPREFIX)$(TARGET)$(SO) $(C_LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(CLIBPREFIX)$(TARGET)$(C_SO)
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) $(CXX_LDSHARED) $(CFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(CLIBPREFIX)$(TARGET)$(C_SO)
c_compile: $(RUNME) $(PROXY) c_compile: $(RUNME) $(PROXY)
$(CC) $(RUNME) $(PROXY) -L. -l$(TARGET) -o $(RUNME:.c=) $(CC) $(RUNME) $(PROXY) -L. -l$(TARGET) -o $(RUNME:.c=)

View file

@ -24,6 +24,6 @@ memchk:
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
clean: clean:
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~ runme rm -f *.o *.out *.so *.a *.dll *.dylib *.exe *_wrap* *_proxy* *~ runme
check: all check: all

View file

@ -24,6 +24,6 @@ memchk:
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
clean: clean:
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~ runme rm -f *.o *.out *.so *.a *.dll *.dylib *.exe *_wrap* *_proxy* *~ runme
check: all check: all

View file

@ -24,6 +24,6 @@ memchk:
env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme env LD_LIBRARY_PATH=. $(MEMTOOL) ./runme
clean: clean:
rm -f *.o *.so *.out *.a *.exe *.dll *_wrap* *_proxy* *~ runme rm -f *.o *.so *.out *.a *.exe *.dll *.dylib *_wrap* *_proxy* *~ runme
check: all check: all

View file

@ -12,13 +12,12 @@ top_builddir = @top_builddir@/..
C_TEST_CASES = C_TEST_CASES =
CPP_TEST_CASES = \ CPP_TEST_CASES = cast_operator \
cast_operator \ char_strings \
char_strings \ exception_order \
exception_order \ exception_partial_info \
exception_partial_info \ enums \
enums \ enum_plus
enum_plus \
include $(srcdir)/../common.mk include $(srcdir)/../common.mk

View file

@ -15,6 +15,7 @@
#include <setjmp.h> #include <setjmp.h>
#define SWIG_STR(x) #x #define SWIG_STR(x) #x
#define SWIG_contract_assert(expr, msg) if(!(expr)) { printf("%s\n", msg); SWIG_exit(0); } else
%} %}
// typemaps for function parameters // typemaps for function parameters
@ -34,7 +35,7 @@
%typemap(ctype) short *&, int *&, long *&, char *&, float *&, double *& "$1_basetype **" %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) const short *&, const int *&, const long *&, const char *&, const float *&, const double *& "$1_basetype **"
%typemap(ctype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$1_basetype *" %typemap(ctype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$1_basetype *"
%typemap(ctype) void * [ANY], short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY] "$1_basetype **" %typemap(ctype) void * [ANY], short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY] "$1_basetype *"
%typemap(ctype) void [ANY][ANY], short [ANY][ANY], int [ANY][ANY], long [ANY][ANY], char [ANY][ANY], float [ANY][ANY], double [ANY][ANY] "$1_basetype **" %typemap(ctype) void [ANY][ANY], short [ANY][ANY], int [ANY][ANY], long [ANY][ANY], char [ANY][ANY], float [ANY][ANY], double [ANY][ANY] "$1_basetype **"
%typemap(ctype) SWIGTYPE "SwigObj *" %typemap(ctype) SWIGTYPE "SwigObj *"
%typemap(ctype) SWIGTYPE * "SwigObj *" %typemap(ctype) SWIGTYPE * "SwigObj *"
@ -96,10 +97,13 @@
$1 = ($1_ltype) 0; $1 = ($1_ltype) 0;
} }
/*
* unsupported yet
%typemap(freearg) SWIGTYPE * [ANY], SWIGTYPE * [ANY][ANY], SWIGTYPE **, SWIGTYPE *** { %typemap(freearg) SWIGTYPE * [ANY], SWIGTYPE * [ANY][ANY], SWIGTYPE **, SWIGTYPE *** {
if (arg2) if ($input)
free(arg2); free($input);
} }
*/
%typemap(in) SWIGTYPE * [ANY][ANY], SWIGTYPE *** { %typemap(in) SWIGTYPE * [ANY][ANY], SWIGTYPE *** {
if ($input) { if ($input) {
@ -143,9 +147,11 @@
%typemap(couttype) short *&, int *&, long *&, char *&, float *&, double *& "$1_basetype **" %typemap(couttype) short *&, int *&, long *&, char *&, float *&, double *& "$1_basetype **"
%typemap(couttype) const short *&, const int *&, const long *&, const char *&, const float *&, const double *& "$1_basetype **" %typemap(couttype) const short *&, const int *&, const long *&, const char *&, const float *&, const double *& "$1_basetype **"
%typemap(couttype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$1_basetype *" %typemap(couttype) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$1_basetype *"
%typemap(couttype) short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY] "$1_basetype *"
%typemap(couttype) SWIGTYPE "SwigObj *" %typemap(couttype) SWIGTYPE "SwigObj *"
%typemap(couttype) SWIGTYPE * "SwigObj *" %typemap(couttype) SWIGTYPE * "SwigObj *"
%typemap(couttype) SWIGTYPE & "SwigObj *" %typemap(couttype) SWIGTYPE & "SwigObj *"
%typemap(couttype) SWIGTYPE [ANY] "SwigObj **"
%typemap(couttype) SWIGTYPE * [ANY], SWIGTYPE ** "SwigObj **" %typemap(couttype) SWIGTYPE * [ANY], SWIGTYPE ** "SwigObj **"
%typemap(couttype) SWIGTYPE * [ANY][ANY], SWIGTYPE *** "SwigObj ***" %typemap(couttype) SWIGTYPE * [ANY][ANY], SWIGTYPE *** "SwigObj ***"
%typemap(couttype) enum SWIGTYPE "$1_type" %typemap(couttype) enum SWIGTYPE "$1_type"
@ -167,6 +173,7 @@
%typemap(out) short *&, int *&, long *&, char *&, float *&, double *& "$result = $1;" %typemap(out) short *&, int *&, long *&, char *&, float *&, double *& "$result = $1;"
%typemap(out) const short *&, const int *&, const long *&, const char *&, const float *&, const double *& "$result = $1;" %typemap(out) const short *&, const int *&, const long *&, const char *&, const float *&, const double *& "$result = $1;"
%typemap(out) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$result = $1;" %typemap(out) short [ANY], int [ANY], long [ANY], char [ANY], float [ANY], double [ANY] "$result = $1;"
%typemap(out) short * [ANY], int * [ANY], long * [ANY], char * [ANY], float * [ANY], double * [ANY] "$result = $1;"
%typemap(out) void "" %typemap(out) void ""
%typemap(out, fragment="stdbool_inc") bool, bool *, const bool, const bool * "$result = $1;" %typemap(out, fragment="stdbool_inc") bool, bool *, const bool, const bool * "$result = $1;"
@ -175,31 +182,41 @@
%typemap(out) enum SWIGTYPE "$result = ($1_type) $1;" %typemap(out) enum SWIGTYPE "$result = ($1_type) $1;"
%typemap(out) SWIGTYPE { %typemap(out) SWIGTYPE {
$result = SWIG_create_object(SWIG_STR($1_type)); $result = SWIG_temporary;
$result->obj = (void*) &$1; $result->obj = (void*) &$1;
} }
%typemap(out) SWIGTYPE *, SWIGTYPE & { %typemap(out) SWIGTYPE *, SWIGTYPE & {
$result = SWIG_create_object(SWIG_STR($1_basetype)); $result = SWIG_temporary;
$result->obj = (void*) $1; $result->obj = (void*) $1;
} }
%typemap(out) SWIGTYPE * [ANY], SWIGTYPE ** { %typemap(out) SWIGTYPE * [ANY], SWIGTYPE ** {
static SwigObj **_temp = 0;
if ($1) { if ($1) {
$result = (SwigObj**) malloc($1_dim0 * sizeof(SwigObj*));
size_t i = 0; size_t i = 0;
for ( ; i < $1_dim0; ++i) if (_temp) {
for ( ; i < $1_dim0; ++i)
SWIG_destroy_object(_temp[i]);
free(_temp);
}
_temp = (SwigObj**) malloc($1_dim0 * sizeof(SwigObj*));
for (i = 0 ; i < $1_dim0; ++i) {
if ($1[i]) { if ($1[i]) {
$result[i] = SWIG_create_object(SWIG_STR($1_ltype)); _temp[i] = SWIG_create_object(SWIG_STR($1_ltype));
$result[i]->obj = (void*) $1[i]; _temp[i]->obj = (void*) $1[i];
} }
else else
$result[i] = (SwigObj*) 0; _temp[i] = (SwigObj*) 0;
}
$result = _temp;
} }
else else
$result = (SwigObj**) 0; $result = (SwigObj**) 0;
} }
/*
* 2+ dim arrays - not supported yet
%typemap(out) SWIGTYPE * [ANY][ANY], SWIGTYPE *** { %typemap(out) SWIGTYPE * [ANY][ANY], SWIGTYPE *** {
if ($1) { if ($1) {
$result = (SwigObj***) malloc($1_dim0 * $1_dim1 * sizeof(SwigObj*)); $result = (SwigObj***) malloc($1_dim0 * $1_dim1 * sizeof(SwigObj*));
@ -217,7 +234,7 @@
} }
else else
$result = (SwigObj***) 0; $result = (SwigObj***) 0;
} }*/
#ifdef SWIG_C_EXCEPT #ifdef SWIG_C_EXCEPT
@ -228,6 +245,11 @@ typedef struct {
} SwigObj; } SwigObj;
%} %}
%include "cexcept.swg" %include "cexcept.swg"
#ifdef SWIG_CPPMODE
%insert("runtime") %{
SwigObj *SWIG_temporary = (SwigObj *) malloc(sizeof(SwigObj));
%}
#endif
#else #else
%insert("runtime") %{ %insert("runtime") %{
typedef struct { typedef struct {
@ -247,6 +269,12 @@ SWIGEXPORTC int SWIG_exit(int code) {
#endif #endif
%} %}
#ifdef SWIG_CPPMODE
%insert("runtime") %{
SwigObj *SWIG_temporary = (SwigObj *) malloc(sizeof(SwigObj));
%}
#endif
%insert("proxy_header") %{ %insert("proxy_header") %{
typedef struct { typedef struct {
void *obj; void *obj;

View file

@ -7,7 +7,7 @@
// this is used instead of default SWIGEXPORT symbol // this is used instead of default SWIGEXPORT symbol
#ifndef SWIGEXPORTC #ifndef SWIGEXPORTC
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__APPLE__)
# define SWIGEXPORTC # define SWIGEXPORTC
# else # else
# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) # if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
@ -19,7 +19,7 @@
#endif #endif
#ifndef SWIGPROTECT #ifndef SWIGPROTECT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) # if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__APPLE__)
# define SWIGPROTECT(x) # define SWIGPROTECT(x)
# else # else
# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) # if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)

View file

@ -78,14 +78,15 @@ public:
} }
} }
if (!CPlusPlus) if (!CPlusPlus)
except_flag = false; except_flag = false;
// add a symbol to the parser for conditional compilation // add a symbol to the parser for conditional compilation
Preprocessor_define("SWIGC 1", 0); Preprocessor_define("SWIGC 1", 0);
Preprocessor_define("SWIG_C_RUNTME 1", 0);
if (except_flag) if (except_flag)
Preprocessor_define("SWIG_C_EXCEPT 1", 0); Preprocessor_define("SWIG_C_EXCEPT 1", 0);
if (CPlusPlus)
Preprocessor_define("SWIG_CPPMODE 1", 0);
SWIG_library_directory("c"); SWIG_library_directory("c");
@ -199,7 +200,7 @@ public:
} }
Swig_register_filebyname("header", f_header); Swig_register_filebyname("header", f_header);
Swig_register_filebyname("wrappers", f_wrappers); Swig_register_filebyname("wrapper", f_wrappers);
Swig_register_filebyname("runtime", f_runtime); Swig_register_filebyname("runtime", f_runtime);
Swig_register_filebyname("init", f_init); Swig_register_filebyname("init", f_init);
@ -425,7 +426,7 @@ ready:
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
* functionWrapper() * functionWrapper()
* ---------------------------------------------------------------------- */ * ---------------------------------------------------------------------- */
virtual int functionWrapper(Node *n) { virtual int functionWrapper(Node *n) {
String *name = Getattr(n, "sym:name"); String *name = Getattr(n, "sym:name");
SwigType *type = Getattr(n, "type"); SwigType *type = Getattr(n, "type");
@ -544,13 +545,13 @@ ready:
Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No couttype typemap defined for %s\n", SwigType_str(type, 0)); Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No couttype typemap defined for %s\n", SwigType_str(type, 0));
} }
// add variable for holding result of original function // add variable for holding result of original function 'cppresult'
bool return_object = false; bool return_object = false;
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) { if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
SwigType *tdtype = SwigType_typedef_resolve(type); SwigType *tdtype = SwigType_typedef_resolve(type);
if (tdtype) if (tdtype)
type = tdtype; type = tdtype;
if (SwigType_isenum(type)) { if (SwigType_isenum(type)) {
Wrapper_add_localv(wrapper, "cppresult", "int", "cppresult", NIL); Wrapper_add_localv(wrapper, "cppresult", "int", "cppresult", NIL);
} }
@ -575,12 +576,15 @@ ready:
SwigType_add_reference(type); SwigType_add_reference(type);
} }
else if (SwigType_isarray(type)) { else if (SwigType_isarray(type)) {
return_var_type = SwigType_base(type); return_var_type = SwigType_base(type);
SwigType *atype = Copy(type); SwigType *atype = Copy(type);
do { do {
SwigType_del_array(atype); SwigType_del_array(atype);
SwigType_add_pointer(return_var_type); SwigType_add_pointer(return_var_type);
} while (SwigType_isarray(atype)); } while (SwigType_isarray(atype));
if (SwigType_ispointer(atype))
SwigType_add_pointer(return_var_type);
Delete(atype); Delete(atype);
} }
else { else {
@ -611,6 +615,7 @@ ready:
SwigType_add_pointer(return_var_type); SwigType_add_pointer(return_var_type);
} }
else { else {
SwigType_add_pointer(type);
return_var_type = type; return_var_type = type;
} }
Wrapper_add_localv(wrapper, "cppresult", SwigType_str(return_var_type, 0), "cppresult", NIL); Wrapper_add_localv(wrapper, "cppresult", SwigType_str(return_var_type, 0), "cppresult", NIL);
@ -687,7 +692,10 @@ ready:
gencomma = 1; gencomma = 1;
// apply typemaps for input parameter // apply typemaps for input parameter
if ((tm = Getattr(p, "tmap:in"))) { if (Cmp(nodeType(n), "destructor") == 0) {
p = Getattr(p, "tmap:in:next");
}
else if ((tm = Getattr(p, "tmap:in"))) {
if (dont_apply_tmap) { if (dont_apply_tmap) {
Printv(wrapper->code, lname, " = ", arg_name, ";\n", NIL); Printv(wrapper->code, lname, " = ", arg_name, ";\n", NIL);
} }
@ -702,6 +710,7 @@ ready:
Swig_warning(WARN_TYPEMAP_IN_UNDEF, input_file, line_number, "Unable to use type %s as a function argument.\n", SwigType_str(type, 0)); 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); p = nextSibling(p);
} }
Delete(arg_name); Delete(arg_name);
Delete(proxy_parm_type); Delete(proxy_parm_type);
@ -710,12 +719,14 @@ ready:
Printf(wrapper->def, ") {"); Printf(wrapper->def, ") {");
// emit variables for holding parameters if (Cmp(nodeType(n), "destructor") != 0) {
emit_parameter_variables(parms, wrapper); // emit variables for holding parameters
emit_parameter_variables(parms, wrapper);
// emit variable for holding function return value
emit_return_variable(n, return_type, wrapper); // emit variable for holding function return value
emit_return_variable(n, return_type, wrapper);
}
// insert constraint checking // insert constraint checking
for (p = parms; p; ) { for (p = parms; p; ) {
if ((tm = Getattr(p, "tmap:check"))) { if ((tm = Getattr(p, "tmap:check"))) {
@ -1043,7 +1054,11 @@ ready:
String *code = NewString(""); String *code = NewString("");
// create code for 'get' function // create code for 'get' function
Printv(code, "result = ", classname, "::", name, ";\n", NIL); Printv(code, "result = $mod", classname, "::", name, ";\n", NIL);
if (!SwigType_isbuiltin(SwigType_base(type)) && !SwigType_ispointer(type))
Replaceall(code, "$mod", "&");
else
Replaceall(code, "$mod", "");
wrap_get_variable(n, classname, newclassname, name, code); wrap_get_variable(n, classname, newclassname, name, code);
// create parameter for 'set' function // create parameter for 'set' function
@ -1066,7 +1081,11 @@ ready:
} }
else else
code = NewString(""); code = NewString("");
Printv(code, classname, "::", name, " = arg1;\n", NIL); Printv(code, classname, "::", name, " = $mod arg1;\nresult = arg1;\n", NIL);
if (!SwigType_isbuiltin(SwigType_base(type)) && !SwigType_ispointer(type))
Replaceall(code, "$mod", "*");
else
Replaceall(code, "$mod", "");
wrap_set_variable(n, classname, newclassname, name, code); wrap_set_variable(n, classname, newclassname, name, code);
} }
@ -1082,26 +1101,14 @@ ready:
* --------------------------------------------------------------------- */ * --------------------------------------------------------------------- */
virtual int membervariableHandler(Node *n) { virtual int membervariableHandler(Node *n) {
SwigType *type = Copy(Getattr(n, "type")); SwigType *type = Getattr(n, "type");
SwigType *tdtype; SwigType *btype = SwigType_base(type);
tdtype = SwigType_typedef_resolve(type); if (SwigType_isarray(type) && !SwigType_isbuiltin(btype)) {
if (tdtype) // this hack applies to member objects array (not ptrs.)
type = tdtype; SwigType_add_pointer(btype);
SwigType_add_array(btype, NewStringf("%s", SwigType_array_getdim(type, 0)));
int array_count = 0; Setattr(n, "type", btype);
while (SwigType_isarray(type)) {
SwigType_del_array(type);
array_count++;
} }
if (type) {
if (!SwigType_ispointer(type) && !SwigType_isreference(type))
Setattr(n, "c:retval", "1");
}
while (array_count) {
SwigType_add_pointer(type);
array_count--;
}
Delete(type);
return Language::membervariableHandler(n); return Language::membervariableHandler(n);
} }
@ -1289,7 +1296,7 @@ ready:
// create action code // create action code
if (except_flag) { if (except_flag) {
Printf(code, "SWIG_remove_registry_entry(carg1);\n"); Printf(code, "SWIG_remove_registry_entry(carg1);\n");
Printf(code, "SWIG_destroy_object(arg1);\n"); Printf(code, "SWIG_destroy_object(carg1);");
} }
else { else {
Printv(code, "if (carg1->obj)\ndelete (", classname, " *) (carg1->obj);\n", NIL); Printv(code, "if (carg1->obj)\ndelete (", classname, " *) (carg1->obj);\n", NIL);

View file

@ -1815,6 +1815,24 @@ fi
AC_SUBST(RBIN) AC_SUBST(RBIN)
# C module on Mac OS X tweaks
case $host in
*-*-darwin*)
C_LDSHARED="cc -dynamiclib"
CXX_LDSHARED="g++ -dynamiclib"
C_SO=".dylib"
;;
*)
C_LDSHARED=$(LDSHARED)
CXX_LDSHARED="$(CXXSHARED)"
C_SO=$(SO)
;;
esac
AC_SUBST(C_LDSHARED)
AC_SUBST(CXX_LDSHARED)
AC_SUBST(C_SO)
#---------------------------------------------------------------- #----------------------------------------------------------------
# Determine which languages to use for examples/test-suite # Determine which languages to use for examples/test-suite
#---------------------------------------------------------------- #----------------------------------------------------------------