generate implicit copyctor, add -nocopyctor, and clarify the -nodefault, -nodefaultctor, -nodefautldtor options

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8030 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2005-12-22 06:26:26 +00:00
commit 723281a823
8 changed files with 261 additions and 61 deletions

View file

@ -0,0 +1,69 @@
%module constructor_copy
%nocopyctor Foo8;
%nocopyctor Bar<double>;
%inline %{
struct Foo1 {
int x;
Foo1(int _x = 2) : x(_x)
{
}
};
struct Foo2 {
Foo2() { }
};
struct Foo3 {
Foo3() { }
Foo3(const Foo3& ) { }
};
struct Foo4 {
Foo4() { }
protected:
Foo4(const Foo4& ) { }
};
struct Foo4a {
Foo4a() { }
private:
Foo4a(const Foo4a& ) { }
};
struct Foo5 : Foo4 {
};
struct Foo6 : Foo4 {
Foo6(const Foo6& ) { }
};
struct Foo7 : Foo5 {
};
struct Foo8 {
};
template <class T>
class Bar
{
public:
int x;
Bar(int _x = 0) : x(_x)
{
}
};
%}
%template(Bari) Bar<int>;
%template(Bard) Bar<double>;

View file

@ -0,0 +1,38 @@
from constructor_copy import *
f1 = Foo1(3);
f11 = Foo1(f1);
if f1.x != f11.x:
raise RuntimeError
f8 = Foo8()
try:
f81 = Foo8(f8)
good = 0
except:
good = 1
if not good:
raise RuntimeError
bi = Bari(5)
bc = Bari(bi)
if (bi.x != bc.x):
raise RuntimeError
bd = Bard(5)
try:
bc = Bard(bd)
good = 0
except:
good = 1
if not good:
raise RuntimeError

View file

@ -47,18 +47,28 @@
#define %clearimmutable %feature("immutable","")
#define %mutable %clearimmutable
/* Generation of default constructors */
/* Generation of default constructors/destructors (old form, don't use) */
#define %nodefault %feature("nodefault","1")
#define %default %feature("nodefault","0")
#define %clearnodefault %feature("nodefault","")
#define %makedefault %cleardefault
/* Disable the generation of implicit/default destructor */
/* Disable the generation of implicit default constructor */
#define %nodefaultctor %feature("nodefaultctor","1")
#define %defaultctor %feature("nodefaultctor","0")
#define %clearnodefaultctor %feature("nodefaultctor","")
/* Disable the generation of implicit default destructor */
#define %nodefaultdtor %feature("nodefaultdtor","1")
#define %defaultdtor %feature("nodefaultdtor","0")
#define %clearnodefaultdtor %feature("nodefaultdtor","")
/* Force the old nodefault behavior */
/* Disable the generation of copy constructor */
#define %nocopyctor %feature("nocopyctor","1")
#define %copyctor %feature("nocopyctor","0")
#define %clearnocopyctor %feature("nocopyctor","")
/* Force the old nodefault behavior, ie disable both constructor and destructor */
#define %oldnodefault %feature("oldnodefault","1")
#define %nooldnodefault %feature("oldnodefault","0")
#define %clearoldnodefault %feature("oldnodefault","")

View file

@ -44,6 +44,7 @@
#define WARN_DEPRECATED_OPTC 120
#define WARN_DEPRECATED_NAME 121
#define WARN_DEPRECATED_NOEXTERN 122
#define WARN_DEPRECATED_NODEFAULT 123
/* -- Preprocessor -- */

View file

@ -570,8 +570,7 @@ public:
/* If class is abstract. No default constructor. Sorry */
if (Getattr(n,"abstract")) {
Delattr(n,"allocate:default_constructor");
}
if (!Getattr(n,"allocate:default_constructor")) {
} else if (!Getattr(n,"allocate:default_constructor")) {
/* Check base classes */
List *bases = Getattr(n,"allbases");
int allows_default = 1;
@ -587,7 +586,26 @@ public:
Setattr(n,"allocate:default_constructor","1");
}
}
}
if (!Getattr(n,"allocate:has_copy_constructor")) {
if (!Getattr(n,"allocate:copy_constructor") && !Getattr(n,"abstract")) {
/* Check base classes */
List *bases = Getattr(n,"allbases");
int allows_copy = 1;
for (int i = 0; i < Len(bases); i++) {
Node *n = Getitem(bases,i);
/* If base class does not allow copy constructor, we don't allow it either */
if (!Getattr(n,"allocate:copy_constructor") && (!Getattr(n,"allocate:copy_base_constructor"))) {
allows_copy = 0;
}
}
if (allows_copy) {
Setattr(n,"allocate:copy_constructor","1");
}
}
}
if (!Getattr(n,"allocate:has_destructor")) {
/* No destructor was defined. We need to check a few things here too */
List *bases = Getattr(n,"allbases");
@ -797,24 +815,36 @@ public:
/* See if this is a copy constructor */
if (parms && (ParmList_numrequired(parms) == 1)) {
/* Look for a few cases. X(const X &), X(X &), X(X *) */
int copy_constructor = 0;
String *cc = NewStringf("r.q(const).%s", Getattr(inclass,"name"));
if (Strcmp(cc,Getattr(parms,"type")) == 0) {
Setattr(n,"copy_constructor","1");
copy_constructor = 1;
} else {
Delete(cc);
cc = NewStringf("r.%s", Getattr(inclass,"name"));
if (Strcmp(cc,Getattr(parms,"type")) == 0) {
copy_constructor = 1;
} else {
Delete(cc);
cc = NewStringf("p.%s", Getattr(inclass,"name"));
String *ty = SwigType_strip_qualifiers(Getattr(parms,"type"));
if (Strcmp(cc,ty) == 0) {
copy_constructor = 1;
}
Delete(cc);
Delete(ty);
}
}
Delete(cc);
cc = NewStringf("r.%s", Getattr(inclass,"name"));
if (Strcmp(cc,Getattr(parms,"type")) == 0) {
if (copy_constructor) {
Setattr(n,"copy_constructor","1");
Setattr(inclass,"allocate:has_copy_constructor","1");
if (cplus_mode == PUBLIC) {
Setattr(inclass,"allocate:copy_constructor","1");
} else if (cplus_mode == PROTECTED) {
Setattr(inclass,"allocate:copy_base_constructor","1");
}
}
Delete(cc);
cc = NewStringf("p.%s", Getattr(inclass,"name"));
String *ty = SwigType_strip_qualifiers(Getattr(parms,"type"));
if (Strcmp(cc,ty) == 0) {
Setattr(n,"copy_constructor","1");
}
Delete(cc);
Delete(ty);
}
return SWIG_OK;
}

View file

@ -351,6 +351,8 @@ void swig_pragma(char *lang, char *name, char *value) {
if ((strcmp(name,"make_default") == 0) || ((strcmp(name,"makedefault") == 0))) {
GenerateDefault = 1;
} else if ((strcmp(name,"no_default") == 0) || ((strcmp(name,"nodefault") == 0))) {
Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1,
"dangerous, use %nodefaultctor, %nodefaultdtor or %nocopyctor instead.\n");
GenerateDefault = 0;
} else if (strcmp(name,"attributefunction") == 0) {
String *nvalue = NewString(value);
@ -2026,26 +2028,43 @@ int Language::classDeclaration(Node *n) {
/* ----------------------------------------------------------------------
* Language::classHandler()
* ---------------------------------------------------------------------- */
static Node *makeCopyConstructor(Node *n)
{
Node *cn = NewHash();
set_nodeType(cn,"constructor");
String *name = Getattr(n,"name");
String *symname = Getattr(n,"sym:name");
String *cc = NewStringf("r.q(const).%s", name);
String *decl = NewStringf("f(%s).",cc);
Parm *p = NewParm(cc,"");
Setattr(cn,"name",name);
Setattr(cn,"sym:name",symname);
SetFlag(cn,"feature:new");
Setattr(cn,"decl",decl);
Setattr(cn,"parentNode",n);
Setattr(cn,"parms",p);
Setattr(cn,"copy_constructor","1");
Setattr(cn,"allocate:copy_constructor","1");
Symtab *oldscope = Swig_symbol_setscope(Getattr(n,"symtab"));
Swig_symbol_add(symname, cn);
Swig_symbol_setscope(oldscope);
return cn;
}
static Node *makeConstructor(Node *n)
{
Node *cn = Copy(n);
Node *cn = NewHash();
set_nodeType(cn,"constructor");
String *name = Getattr(n,"name");
String *rname = 0;
String *dname = NewStringf("%s::",name);
if (SwigType_istemplate(name)) {
rname = SwigType_templateprefix(name);
name = rname;
}
String *lname = Swig_scopename_last(name);
Append(dname,lname);
String *symname = Getattr(n,"sym:name");
Setattr(cn,"name",name);
Setattr(cn,"sym:name",symname);
SetFlag(cn,"feature:new");
Setattr(cn,"decl","f().");
Swig_features_get(Swig_cparse_features(), 0, dname, Getattr(cn,"decl"), cn);
Delete(rname);
Delete(dname);
Delete(lname);
Setattr(cn,"parentNode",n);
Symtab *oldscope = Swig_symbol_setscope(Getattr(n,"symtab"));
Swig_symbol_add(symname, cn);
Swig_symbol_setscope(oldscope);
return cn;
}
@ -2076,7 +2095,27 @@ int Language::classHandler(Node *n) {
bool hasDirector = Swig_directorclass(n) ? true : false;
int odefault = (GenerateDefault && !GetFlag(n,"feature:nodefault"));
if (!ImportMode && (!GetFlag(n,"feature:nocopyctor")) && odefault) {
if (!Getattr(n,"has_copy_constructor") && !Getattr(n,"allocate:has_copy_constructor")
&& (Getattr(n,"allocate:copy_constructor"))) {
if (!Abstract) {
Node *cn = makeCopyConstructor(CurrentClass);
appendChild(n,cn);
}
}
}
cplus_mode = PUBLIC;
if (!ImportMode && !GetFlag(n,"feature:nodefaultctor") && odefault) {
if (!Getattr(n,"has_constructor") && !Getattr(n,"allocate:has_constructor")
&& (Getattr(n,"allocate:default_constructor"))) {
/* Note: will need to change this to support different kinds of classes */
if (!Abstract) {
Node *cn = makeConstructor(CurrentClass);
appendChild(n,cn);
}
}
}
/* Emit all of the class members */
emit_children(n);
@ -2093,19 +2132,9 @@ int Language::classHandler(Node *n) {
SmartPointer = 0;
}
cplus_mode = PUBLIC;
if (!ImportMode && (GenerateDefault && !GetFlag(n,"feature:nodefault"))) {
if (!Getattr(n,"has_constructor") && !Getattr(n,"allocate:has_constructor") && (Getattr(n,"allocate:default_constructor"))) {
/* Note: will need to change this to support different kinds of classes */
if (!Abstract) {
Node *cn = makeConstructor(CurrentClass);
constructorHandler(cn);
Delete(cn);
}
}
}
if (!ImportMode) {
if (!Getattr(n,"has_destructor") && (!Getattr(n,"allocate:has_destructor")) && (Getattr(n,"allocate:default_destructor"))) {
if (!ImportMode && !GetFlag(n,"feature:nodefaultdtor") && odefault) {
if (!Getattr(n,"has_destructor") && (!Getattr(n,"allocate:has_destructor"))
&& (Getattr(n,"allocate:default_destructor"))) {
Node *cn = makeDestructor(CurrentClass);
destructorHandler(cn);
Delete(cn);
@ -2165,6 +2194,7 @@ int Language::constructorDeclaration(Node *n) {
String *name = Getattr(n,"name");
String *symname = Getattr(n,"sym:name");
if (!symname) return SWIG_NOWRAP;
if (!CurrentClass) return SWIG_NOWRAP;
if (ImportMode) return SWIG_NOWRAP;
@ -2242,6 +2272,7 @@ int Language::constructorDeclaration(Node *n) {
}
}
Setattr(CurrentClass,"has_constructor","1");
Swig_restore(n);
return SWIG_OK;
}
@ -2321,6 +2352,7 @@ Language::copyconstructorHandler(Node *n) {
Swig_ConstructorToFunction(n,ClassType, none_comparison, director_ctor,
CPlusPlus, Getattr(n,"template") ? 0 : Extend);
Setattr(n,"sym:name", mrename);
Printf(stderr,"Hello\n");
functionWrapper(n);
Delete(mrename);
Swig_restore(n);

View file

@ -83,7 +83,10 @@ static const char *usage2 = (const char*)"\
-makedefault - Create default constructors/destructors (the default)\n\
-module <name> - Set module name to <name>\n\
-nocontract - Turn off contract checking \n\
-nodefault - Do not generate constructors/destructors\n\
-nodefault - Do not generate constructors, destructor or copy constructors (dangerous, don't use it)\n\
-nodefaultctor - Do not generate implicit default constructors\n\
-nodefaultdctor - Do not generate implicit default destructors (dangerous, use %nodefaultdtor instead)\n\
-nocopyctor - Do not generate implicit copy constructors\n\
-nodirprot - Do not wrap director protected members\n\
-noexcept - Do not wrap exception specifiers\n\
-addextern - Add extra extern declarations\n\
@ -241,7 +244,20 @@ void SWIG_config_cppext(const char *ext) {
cpp_extension = (char *) ext;
}
void SWIG_getfeatures(const char *c)
void SWIG_setfeature(const char *cfeature, const char *cvalue)
{
Hash *features_hash = Swig_cparse_features();
String *name = NewString("");
String *fname = NewString(cfeature);
String *fvalue = NewString(cvalue);
Swig_feature_set(features_hash,name,0,fname,fvalue,0);
Delete(name);
Delete(fname);
Delete(fvalue);
}
void SWIG_setfeatures(const char *c)
{
char feature[64];
char *fb = feature;
@ -397,16 +413,9 @@ void SWIG_getoptions(int argc, char *argv[])
Wrapper_fast_dispatch_mode_set(0);
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-directors") == 0) {
Hash *features_hash = Swig_cparse_features();
String *name = NewString("");
String *fname = NewString("feature:director");
String *fvalue = NewString("1");
Swig_feature_set(features_hash,name,0,fname,fvalue,0);
SWIG_setfeature("feature:director","1");
Wrapper_director_mode_set(1);
Swig_mark_arg(i);
Delete(name);
Delete(fname);
Delete(fvalue);
} else if (strcmp(argv[i],"-dirprot") == 0) {
Wrapper_director_protected_mode_set(1);
Swig_mark_arg(i);
@ -438,6 +447,17 @@ void SWIG_getoptions(int argc, char *argv[])
Swig_mark_arg(i);
} else if ((strcmp(argv[i],"-no_default") == 0) || (strcmp(argv[i],"-nodefault") == 0)) {
GenerateDefault = 0;
Swig_warning(WARN_DEPRECATED_NODEFAULT, "SWIG",1,
"dangerous, use -nodefaultctor, -nodefaultdtor or -nocopyctor instead.\n");
Swig_mark_arg(i);
} else if ((strcmp(argv[i],"-nodefaultctor") == 0)) {
SWIG_setfeature("feature:nodefaultctor","1");
Swig_mark_arg(i);
} else if ((strcmp(argv[i],"-nodefaultdtor") == 0)) {
SWIG_setfeature("feature:nodefaultdtor","1");
Swig_mark_arg(i);
} else if ((strcmp(argv[i],"-nocopyctor") == 0)) {
SWIG_setfeature("feature:nocopyctor","1");
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-noexcept") == 0) {
NoExcept = 1;
@ -510,7 +530,7 @@ void SWIG_getoptions(int argc, char *argv[])
} else if (strcmp(argv[i],"-features") == 0) {
Swig_mark_arg(i);
if (argv[i+1]) {
SWIG_getfeatures(argv[i+1]);
SWIG_setfeatures(argv[i+1]);
Swig_mark_arg(i+1);
} else {
Swig_arg_error();

View file

@ -234,10 +234,10 @@ public:
} else if (strcmp(argv[i],"-apply") == 0) {
apply = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-new_repr") == 0) {
} else if ((strcmp(argv[i],"-new_repr") == 0) || (strcmp(argv[i],"-newrepr") == 0)) {
new_repr = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-old_repr") == 0) {
} else if ((strcmp(argv[i],"-old_repr") == 0) || (strcmp(argv[i],"-oldrepr") == 0)) {
new_repr = 0;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-classptr") == 0) {
@ -325,7 +325,7 @@ public:
} else if (strcmp(argv[i],"-noh") == 0) {
no_header_file = 1;
Swig_mark_arg(i);
} else if (strcmp(argv[i],"-new_vwm") == 0) {
} else if ((strcmp(argv[i],"-new_vwm") == 0) || (strcmp(argv[i],"-newvwm") == 0)) {
/* Turn on new value wrapper mpde */
Swig_value_wrapper_mode(1);
no_header_file = 1;