Fix scoping of forward class declarations nested within a class (for C++). Also fix %template and resolution of template parameters that are typedefs.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12764 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-07-26 19:34:23 +00:00
commit 7d038d4bd7
11 changed files with 442 additions and 54 deletions

View file

@ -5,6 +5,33 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.5 (in progress)
===========================
2011-07-26: wsfulton
Fix scoping of forward class declarations nested within a class (for C++). Previously the symbol
was incorrectly put into the outer namespace, eg
namespace std {
template<class Key, class T> struct map {
class iterator;
}
}
iterator was scoped as std::iterator, but now it is correctly std::map<Key, T>::iterator;
Also fixed is %template and template parameters that are a typedef when the template contains
default template parameters, eg:
namespace Std {
template<class Key, class T, class C = int> struct Map {
typedef Key key_type;
typedef T mapped_type;
}
}
tyepdef double DOUBLE;
%typemap(MM) Std::Map<int, DOUBLE>;
All symbols within Map will be resolved correctly, eg key_type and mapped_type no matter if the
wrapped code uses Std::Map<int, double> or std::Map<int, DOUBLE> or Std::Map<int, double, int>
2011-07-20 szager
[python] Fix closure for tp_call slot.

View file

@ -383,6 +383,7 @@ CPP_TEST_CASES += \
template_tbase_template \
template_template_parameters \
template_typedef \
template_typedef_class_template \
template_typedef_cplx \
template_typedef_cplx2 \
template_typedef_cplx3 \
@ -392,6 +393,9 @@ CPP_TEST_CASES += \
template_typedef_ns \
template_typedef_ptr \
template_typedef_rec \
template_typemaps \
template_typemaps_typedef \
template_typemaps_typedef2 \
template_using \
template_virtual \
template_whitespace \

View file

@ -0,0 +1,37 @@
from template_typemaps_typedef2 import *
m1 = MultimapIntA()
dummy_pair = m1.make_dummy_pair()
val = m1.typemap_test(dummy_pair).val
if val != 1234:
raise RuntimeError, "typemaps not working"
m2 = MultimapAInt()
# TODO: typemaps and specializations not quite working as expected. T needs expanding, but at least the right typemap is being picked up.
#dummy_pair = m2.make_dummy_pair()
#val = m2.typemap_test(dummy_pair)
#print val
#if val != 4321:
# raise RuntimeError, "typemaps not working"
if typedef_test1(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test1 not working"
if typedef_test2(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test2 not working"
if typedef_test3(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test3 not working"
if typedef_test4(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test4 not working"
if typedef_test5(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test5 not working"
if typedef_test6(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test6 not working"

View file

@ -0,0 +1,37 @@
from template_typemaps_typedef import *
m1 = MultimapIntA()
dummy_pair = m1.make_dummy_pair()
val = m1.typemap_test(dummy_pair).val
if val != 1234:
raise RuntimeError, "typemaps not working"
m2 = MultimapAInt()
# TODO: typemaps and specializations not quite working as expected. T needs expanding, but at least the right typemap is being picked up.
#dummy_pair = m2.make_dummy_pair()
#val = m2.typemap_test(dummy_pair)
#print val
#if val != 4321:
# raise RuntimeError, "typemaps not working"
if typedef_test1(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test1 not working"
if typedef_test2(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test2 not working"
if typedef_test3(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test3 not working"
if typedef_test4(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test4 not working"
if typedef_test5(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test5 not working"
if typedef_test6(dummy_pair).val != 1234:
raise RuntimeError, "typedef_test6 not working"

View file

@ -0,0 +1,57 @@
%module template_typedef_class_template
%inline %{
namespace Standard {
template <class T, class U > struct Pair {
T first;
U second;
};
}
%}
// previously these typemaps were erroneously being used as iterator was not correctly scoped in Multimap
%typemap(out) Standard::Pair<Standard::iterator, Standard::iterator> "_this_will_not_compile_iterator_"
%typemap(out) Standard::Pair<Standard::const_iterator, Standard::const_iterator> "_this_will_not_compile_const_iterator_"
%{
namespace Standard {
template<class Key, class T> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator {};
class const_iterator {};
// test usage of a typedef of a nested class in a template
Standard::Pair<iterator,iterator> equal_range_1(const key_type& kt1) {}
Standard::Pair<const_iterator,const_iterator> equal_range_2(const key_type& kt2) const {}
};
}
%}
namespace Standard {
template<class Key, class T> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator;
class const_iterator;
// test usage of a typedef of a nested class in a template
Standard::Pair<iterator,iterator> equal_range_1(const key_type& kt1) {}
Standard::Pair<const_iterator,const_iterator> equal_range_2(const key_type& kt2) const {}
};
}
%inline %{
struct A {
int val;
A(int v = 0): val(v) {}
};
%}
%template(PairA) Standard::Pair<int, A*>;
%template(MultimapA) Standard::Multimap<int, A*>;

View file

@ -0,0 +1,109 @@
%module template_typemaps_typedef
// Similar to template_typedef_class_template
// Testing typemaps of a typedef of a nested class in a template and where the template uses default parameters
%inline %{
namespace Standard {
template <class T, class U > struct Pair {
T first;
U second;
};
}
%}
%{
namespace Standard {
template<class Key, class T, class J = int> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator {
public:
mapped_type mm;
iterator(mapped_type m = mapped_type()) : mm(m) {}
};
mapped_type typemap_test(Standard::Pair<iterator,iterator> pp) { return pp.second.mm; }
Standard::Pair<iterator,iterator>* make_dummy_pair() { return new Standard::Pair<iterator, iterator>(); }
};
}
%}
namespace Standard {
template<class Key, class T, class J = int> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator;
%typemap(in) Standard::Pair<iterator,iterator> "$1 = default_general< Key, T >();"
mapped_type typemap_test(Standard::Pair<iterator,iterator> pii1);
Standard::Pair<iterator,iterator>* make_dummy_pair();
};
}
// specialization
namespace Standard {
template<> class Multimap<A, int> {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator;
// Note uses a different function to the non-specialized version
%typemap(in) Standard::Pair<iterator,iterator> "$1 = default_A_int< A, int >();"
mapped_type typemap_test(Standard::Pair<iterator,iterator> pii2);
Standard::Pair<iterator,iterator>* make_dummy_pair();
};
}
%inline %{
struct A {
int val;
A(int v = 0): val(v) {}
};
%}
%{
// For < int, A >
template<typename Key, typename T> Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_general() {
Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_value;
default_value.second.mm = A(1234);
return default_value;
}
// For < A, int >
template<typename Key, typename T> Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_A_int() {
Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_value;
default_value.second.mm = 4321;
return default_value;
}
%}
%inline %{
typedef A AA;
namespace Space {
typedef AA AB;
}
%}
%template(PairIntA) Standard::Pair<int, A>;
%template(MultimapIntA) Standard::Multimap<int, A>;
%template(PairAInt) Standard::Pair<A, int>;
%template(MultimapAInt) Standard::Multimap<A, int>;
%inline %{
// Extend the test with some typedefs in the template parameters
Standard::Multimap< int, AA >::mapped_type typedef_test1(Standard::Pair< Standard::Multimap< int, AA >::iterator, Standard::Multimap< int, AA >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, A >::mapped_type typedef_test2(Standard::Pair< Standard::Multimap< int, A >::iterator, Standard::Multimap< int, A >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, AA, int >::mapped_type typedef_test3(Standard::Pair< Standard::Multimap< int, AA, int >::iterator, Standard::Multimap< int, AA, int >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, A , int >::mapped_type typedef_test4(Standard::Pair< Standard::Multimap< int, A , int >::iterator, Standard::Multimap< int, A , int >::iterator > pp) { return pp.second.mm; }
using namespace Space;
Standard::Multimap< int, AB >::mapped_type typedef_test5(Standard::Pair< Standard::Multimap< int, AB >::iterator, Standard::Multimap< int, AB >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, AB, int >::mapped_type typedef_test6(Standard::Pair< Standard::Multimap< int, AB, int >::iterator, Standard::Multimap< int, AB, int >::iterator > pp) { return pp.second.mm; }
%}

View file

@ -0,0 +1,111 @@
%module template_typemaps_typedef2
// Identical to template_typemaps_typedef, except for %template
// Similar to template_typedef_class_template
// Testing typemaps of a typedef of a nested class in a template and where the template uses default parameters
%inline %{
namespace Standard {
template <class T, class U > struct Pair {
T first;
U second;
};
}
%}
%{
namespace Standard {
template<class Key, class T, class J = int> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator {
public:
mapped_type mm;
iterator(mapped_type m = mapped_type()) : mm(m) {}
};
mapped_type typemap_test(Standard::Pair<iterator,iterator> pp) { return pp.second.mm; }
Standard::Pair<iterator,iterator>* make_dummy_pair() { return new Standard::Pair<iterator, iterator>(); }
};
}
%}
namespace Standard {
template<class Key, class T, class J = int> class Multimap {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator;
%typemap(in) Standard::Pair<iterator,iterator> "$1 = default_general< Key, T >();"
mapped_type typemap_test(Standard::Pair<iterator,iterator> pii1);
Standard::Pair<iterator,iterator>* make_dummy_pair();
};
}
// specialization
namespace Standard {
template<> class Multimap<A, int> {
public:
typedef Key key_type;
typedef T mapped_type;
class iterator;
// Note uses a different function to the non-specialized version
%typemap(in) Standard::Pair<iterator,iterator> "$1 = default_A_int< A, int >();"
mapped_type typemap_test(Standard::Pair<iterator,iterator> pii2);
Standard::Pair<iterator,iterator>* make_dummy_pair();
};
}
%inline %{
struct A {
int val;
A(int v = 0): val(v) {}
};
%}
%{
// For < int, A >
template<typename Key, typename T> Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_general() {
Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_value;
default_value.second.mm = A(1234);
return default_value;
}
// For < A, int >
template<typename Key, typename T> Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_A_int() {
Standard::Pair< typename Standard::Multimap< Key, T >::iterator, typename Standard::Multimap< Key, T >::iterator > default_value;
default_value.second.mm = 4321;
return default_value;
}
%}
%inline %{
typedef A AA;
namespace Space {
typedef AA AB;
}
%}
%template(PairIntA) Standard::Pair<int, Space::AB>;
%template(MultimapIntA) Standard::Multimap<int, Space::AB>;
%template(PairAInt) Standard::Pair<Space::AB, int>;
%template(MultimapAInt) Standard::Multimap<Space::AB, int>;
%inline %{
// Extend the test with some typedefs in the template parameters
Standard::Multimap< int, AA >::mapped_type typedef_test1(Standard::Pair< Standard::Multimap< int, AA >::iterator, Standard::Multimap< int, AA >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, A >::mapped_type typedef_test2(Standard::Pair< Standard::Multimap< int, A >::iterator, Standard::Multimap< int, A >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, AA, int >::mapped_type typedef_test3(Standard::Pair< Standard::Multimap< int, AA, int >::iterator, Standard::Multimap< int, AA, int >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, A , int >::mapped_type typedef_test4(Standard::Pair< Standard::Multimap< int, A , int >::iterator, Standard::Multimap< int, A , int >::iterator > pp) { return pp.second.mm; }
using namespace Space;
Standard::Multimap< int, AB >::mapped_type typedef_test5(Standard::Pair< Standard::Multimap< int, AB >::iterator, Standard::Multimap< int, AB >::iterator > pp) { return pp.second.mm; }
Standard::Multimap< int, AB, int >::mapped_type typedef_test6(Standard::Pair< Standard::Multimap< int, AB, int >::iterator, Standard::Multimap< int, AB, int >::iterator > pp) { return pp.second.mm; }
%}

View file

@ -539,18 +539,10 @@ class TypePass:private Dispatcher {
virtual int classforwardDeclaration(Node *n) {
/* Temporary hack. Can't do inside a class because it breaks
C nested structure wrapping */
/* Can't do inside a C struct because it breaks C nested structure wrapping */
if ((!inclass) || (CPlusPlus)) {
String *name = Getattr(n, "name");
String *nname;
SwigType_typedef_class(name);
if (nsname) {
nname = NewStringf("%s::%s", nsname, name);
Setattr(n, "name", nname);
}
}
return SWIG_OK;
}

View file

@ -223,6 +223,9 @@ static void symbol_print_symbols(const char *symboltabletype) {
while (it.key) {
String *symname = it.key;
Printf(stdout, " %s\n", symname);
/*
Printf(stdout, " %s - %p (%s)\n", symname, it.item, Getattr(it.item, "name"));
*/
it = Next(it);
}
}

View file

@ -91,9 +91,11 @@ static Hash *get_typemap(int tm_scope, const SwigType *type) {
static void set_typemap(int tm_scope, const SwigType *type, Hash *tm) {
SwigType *hashtype = 0;
if (SwigType_istemplate(type)) {
String *ty = Swig_symbol_template_deftype(type, 0);
SwigType *rty = SwigType_typedef_resolve_all(type);
String *ty = Swig_symbol_template_deftype(rty, 0);
String *tyq = Swig_symbol_type_qualify(ty, 0);
hashtype = SwigType_remove_global_scope_prefix(tyq);
Delete(rty);
Delete(tyq);
Delete(ty);
} else {
@ -2013,12 +2015,13 @@ static void replace_embedded_typemap(String *s, ParmList *parm_sublist, Wrapper
void Swig_typemap_debug() {
int ts;
int nesting_level = 2;
Printf(stdout, "---[ typemaps ]--------------------------------------------------------------\n");
ts = tm_scope;
while (ts >= 0) {
Printf(stdout, "::: scope %d\n\n", ts);
Printf(stdout, "%s\n", typemaps[ts]);
Swig_print(typemaps[ts], nesting_level);
ts--;
}
Printf(stdout, "-----------------------------------------------------------------------------\n");

View file

@ -425,8 +425,6 @@ static Typetab *SwigType_find_scope(Typetab *s, const SwigType *nameprefix) {
return 0;
Setmark(s, 1);
/* Printf(stdout,"find_scope: %x(%s) '%s'\n", s, Getattr(s,"name"), nameprefix); */
if (SwigType_istemplate(nameprefix)) {
nnameprefix = SwigType_typedef_resolve_all(nameprefix);
nameprefix = nnameprefix;
@ -542,6 +540,53 @@ static SwigType *_typedef_resolve(Typetab *s, String *base, int look_parent) {
return type;
}
/* -----------------------------------------------------------------------------
* template_parameters_resolve()
*
* For use with templates only. The template parameters are resolved. If none
* of the parameters can be resolved, zero is returned.
* ----------------------------------------------------------------------------- */
static String *template_parameters_resolve(const String *base) {
List *tparms;
String *suffix;
String *type;
int i, sz;
int rep = 0;
type = SwigType_templateprefix(base);
suffix = SwigType_templatesuffix(base);
Append(type, "<(");
tparms = SwigType_parmlist(base);
sz = Len(tparms);
for (i = 0; i < sz; i++) {
SwigType *tpr;
SwigType *tp = Getitem(tparms, i);
if (!rep) {
tpr = SwigType_typedef_resolve(tp);
} else {
tpr = 0;
}
if (tpr) {
Append(type, tpr);
Delete(tpr);
rep = 1;
} else {
Append(type, tp);
}
if ((i + 1) < sz)
Append(type, ",");
}
Append(type, ")>");
Append(type, suffix);
Delete(suffix);
Delete(tparms);
if (!rep) {
Delete(type);
type = 0;
}
return type;
}
static SwigType *typedef_resolve(Typetab *s, String *base) {
return _typedef_resolve(s, base, 1);
}
@ -562,12 +607,6 @@ SwigType *SwigType_typedef_resolve(const SwigType *t) {
String *nameprefix = 0;
int newtype = 0;
/*
if (!noscope) {
noscope = NewStringEmpty();
}
*/
resolved_scope = 0;
#ifdef SWIG_TYPEDEF_RESOLVE_CACHE
@ -611,6 +650,9 @@ SwigType *SwigType_typedef_resolve(const SwigType *t) {
#endif
if (nameprefix) {
/* Name had a prefix on it. See if we can locate the proper scope for it */
String *rnameprefix = template_parameters_resolve(nameprefix);
nameprefix = rnameprefix ? Copy(rnameprefix) : nameprefix;
Delete(rnameprefix);
s = SwigType_find_scope(s, nameprefix);
/* Couldn't locate a scope for the type. */
@ -677,42 +719,8 @@ SwigType *SwigType_typedef_resolve(const SwigType *t) {
template arguments one by one to see if they can be resolved. */
if (!type && SwigType_istemplate(base)) {
List *tparms;
String *suffix;
int i, sz;
int rep = 0;
type = SwigType_templateprefix(base);
newtype = 1;
suffix = SwigType_templatesuffix(base);
Append(type, "<(");
tparms = SwigType_parmlist(base);
sz = Len(tparms);
for (i = 0; i < sz; i++) {
SwigType *tpr;
SwigType *tp = Getitem(tparms, i);
if (!rep) {
tpr = SwigType_typedef_resolve(tp);
} else {
tpr = 0;
}
if (tpr) {
Append(type, tpr);
Delete(tpr);
rep = 1;
} else {
Append(type, tp);
}
if ((i + 1) < sz)
Append(type, ",");
}
Append(type, ")>");
Append(type, suffix);
Delete(suffix);
Delete(tparms);
if (!rep) {
Delete(type);
type = 0;
}
type = template_parameters_resolve(base);
}
if (namebase)
Delete(namebase);