Fixes for the family of %interface macros for overloaded methods

When C++ methods are not able to be overloaded in a derived class,
such as when they differ by just const, or the target language
parameters types are identical even when the C++ parameter types
are different, SWIG will ignore one of the overloaded methods with
a warning. A %ignore is required to explicitly ignore one of the
overloaded methods to avoid the warning message. Methods added
in the derived classes due to one of the %interface macros are now
similarly ignored/not added to the derived class.

The adding of additional methods into the parse tree is now more
robust and complete resulting in support for %feature and %rename
for the added methods.

Closes #1277
This commit is contained in:
William S Fulton 2022-03-12 12:46:59 +00:00
commit b6ece11fc1
12 changed files with 259 additions and 15 deletions

View file

@ -308,6 +308,7 @@ CPP_TEST_CASES += \
multiple_inheritance_abstract \
multiple_inheritance_interfaces \
multiple_inheritance_nspace \
multiple_inheritance_overload \
multiple_inheritance_shared_ptr \
name_cxx \
name_warnings \

View file

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using multiple_inheritance_overloadNamespace;
public class multiple_inheritance_overload_runme {
public static void check(bool fail, String msg) {
if (fail)
throw new Exception(msg);
}
public static void Main() {
int i = 0;
Base b1 = new Derived();
check(b1.Method(i) != 0, "b1.Method failed");
check(b1.MethodForRenaming(i) != 0, "b1.MethodForRenaming failed");
check(b1.MethodForRenamingConst(i) != 1, "b1.MethodForRenamingConst failed");
check(b1.MethodWarningSuppressed(i) != 0, "b1.MethodWarningSuppressed failed");
check(b1.NotVirtualMethod(i) != 0, "b1.NotVirtualMethod failed");
check(b1.SimilarOverloadedMethod(i) != 0, "b1.NotVirtualMethod failed");
Derived d1 = new Derived();
check(d1.Method(i) != 0, "d1.Method failed");
check(d1.MethodForRenaming(i) != 0, "d1.MethodForRenaming failed");
check(d1.MethodForRenamingConst(i) != 1, "d1.MethodForRenamingConst failed");
check(d1.MethodWarningSuppressed(i) != 0, "d1.MethodWarningSuppressed failed");
check(d1.NotVirtualMethod(i) != 0, "d1.NotVirtualMethod failed");
check(d1.SimilarOverloadedMethod(i) != 0, "d1.NotVirtualMethod failed");
check(d1.AnotherMethod(i) != 0, "d1.AnotherMethod failed");
Base db1 = BaseSwigImpl.inout(d1);
check(db1.Method(i) != 0, "db1.Method failed");
check(db1.MethodForRenaming(i) != 0, "db1.MethodForRenaming failed");
check(db1.MethodForRenamingConst(i) != 1, "db1.MethodForRenamingConst failed");
check(db1.MethodWarningSuppressed(i) != 0, "db1.MethodWarningSuppressed failed");
check(db1.NotVirtualMethod(i) != 0, "db1.NotVirtualMethod failed");
check(db1.SimilarOverloadedMethod(i) != 0, "db1.NotVirtualMethod failed");
MoreDerived m1 = new MoreDerived();
check(m1.Method(i) != 0, "m1.Method failed");
check(m1.MethodForRenaming(i) != 0, "m1.MethodForRenaming failed");
check(m1.MethodForRenamingConst(i) != 1, "m1.MethodForRenamingConst failed");
check(m1.MethodWarningSuppressed(i) != 0, "m1.MethodWarningSuppressed failed");
check(m1.NotVirtualMethod(i) != 0, "m1.NotVirtualMethod failed");
check(m1.SimilarOverloadedMethod(i) != 0, "m1.NotVirtualMethod failed");
check(m1.AnotherMethod(i) != 0, "m1.AnotherMethod failed");
Base mb2 = BaseSwigImpl.inout(m1);
check(mb2.Method(i) != 0, "mb2.Method failed");
check(mb2.MethodForRenaming(i) != 0, "mb2.MethodForRenaming failed");
check(mb2.MethodForRenamingConst(i) != 1, "mb2.MethodForRenamingConst failed");
check(mb2.MethodWarningSuppressed(i) != 0, "mb2.MethodWarningSuppressed failed");
check(mb2.NotVirtualMethod(i) != 0, "mb2.NotVirtualMethod failed");
check(mb2.SimilarOverloadedMethod(i) != 0, "mb2.NotVirtualMethod failed");
}
}

View file

@ -0,0 +1,65 @@
import multiple_inheritance_overload.*;
public class multiple_inheritance_overload_runme {
static {
try {
System.loadLibrary("multiple_inheritance_overload");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
public static void check(boolean fail, String msg) {
if (fail)
throw new RuntimeException(msg);
}
public static void main(String argv[]) {
int i = 0;
Base b1 = new Derived();
check(b1.Method(i) != 0, "b1.Method failed");
check(b1.MethodForRenaming(i) != 0, "b1.MethodForRenaming failed");
check(b1.MethodForRenamingConst(i) != 1, "b1.MethodForRenamingConst failed");
check(b1.MethodWarningSuppressed(i) != 0, "b1.MethodWarningSuppressed failed");
check(b1.NotVirtualMethod(i) != 0, "b1.NotVirtualMethod failed");
check(b1.SimilarOverloadedMethod(i) != 0, "b1.NotVirtualMethod failed");
Derived d1 = new Derived();
check(d1.Method(i) != 0, "d1.Method failed");
check(d1.MethodForRenaming(i) != 0, "d1.MethodForRenaming failed");
check(d1.MethodForRenamingConst(i) != 1, "d1.MethodForRenamingConst failed");
check(d1.MethodWarningSuppressed(i) != 0, "d1.MethodWarningSuppressed failed");
check(d1.NotVirtualMethod(i) != 0, "d1.NotVirtualMethod failed");
check(d1.SimilarOverloadedMethod(i) != 0, "d1.NotVirtualMethod failed");
check(d1.AnotherMethod(i) != 0, "d1.AnotherMethod failed");
Base db1 = BaseSwigImpl.inout(d1);
check(db1.Method(i) != 0, "db1.Method failed");
check(db1.MethodForRenaming(i) != 0, "db1.MethodForRenaming failed");
check(db1.MethodForRenamingConst(i) != 1, "db1.MethodForRenamingConst failed");
check(db1.MethodWarningSuppressed(i) != 0, "db1.MethodWarningSuppressed failed");
check(db1.NotVirtualMethod(i) != 0, "db1.NotVirtualMethod failed");
check(db1.SimilarOverloadedMethod(i) != 0, "db1.NotVirtualMethod failed");
MoreDerived m1 = new MoreDerived();
check(m1.Method(i) != 0, "m1.Method failed");
check(m1.MethodForRenaming(i) != 0, "m1.MethodForRenaming failed");
check(m1.MethodForRenamingConst(i) != 1, "m1.MethodForRenamingConst failed");
check(m1.MethodWarningSuppressed(i) != 0, "m1.MethodWarningSuppressed failed");
check(m1.NotVirtualMethod(i) != 0, "m1.NotVirtualMethod failed");
check(m1.SimilarOverloadedMethod(i) != 0, "m1.NotVirtualMethod failed");
check(m1.AnotherMethod(i) != 0, "m1.AnotherMethod failed");
Base mb2 = BaseSwigImpl.inout(m1);
check(mb2.Method(i) != 0, "mb2.Method failed");
check(mb2.MethodForRenaming(i) != 0, "mb2.MethodForRenaming failed");
check(mb2.MethodForRenamingConst(i) != 1, "mb2.MethodForRenamingConst failed");
check(mb2.MethodWarningSuppressed(i) != 0, "mb2.MethodWarningSuppressed failed");
check(mb2.NotVirtualMethod(i) != 0, "mb2.NotVirtualMethod failed");
check(mb2.SimilarOverloadedMethod(i) != 0, "mb2.NotVirtualMethod failed");
}
}

View file

@ -5,7 +5,7 @@
SWIGWARN_PHP_MULTIPLE_INHERITANCE); /* languages not supporting multiple inheritance */
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%include "swiginterface.i"
%include <swiginterface.i>
%interface_impl(Space::ABase1)
%interface_impl(Space::CBase1)
%interface_impl(Space::CBase2)

View file

@ -4,7 +4,7 @@
SWIGWARN_PHP_MULTIPLE_INHERITANCE); /* languages not supporting multiple inheritance */
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%include "swiginterface.i"
%include <swiginterface.i>
%interface_custom("A", "IA", IA)
%interface_custom("B", "IB", IB)
%interface_custom("%(strip:[I])s", "I%s", IC) // same as %interface_custom("C", "IC", IC)

View file

@ -10,7 +10,7 @@
#endif
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%include "swiginterface.i"
%include <swiginterface.i>
%interface(Space::ABase1)
%interface(Space::CBase1)
%interface(Space::CBase2)

View file

@ -0,0 +1,67 @@
%module(ruby_minherit="1") multiple_inheritance_overload
%warnfilter(SWIGWARN_D_MULTIPLE_INHERITANCE,
SWIGWARN_PHP_MULTIPLE_INHERITANCE); /* languages not supporting multiple inheritance */
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%include <swiginterface.i>
%interface_impl(Space::Base);
%interface_impl(Space::AnotherBase);
#endif
%ignore AnotherSpace::AnotherBase::AnotherMethod(int i) const;
%ignore Space::Base::Method(int i) const;
%ignore Space::Base::NotVirtualMethod(int i) const;
%ignore Space::Base::SimilarOverloadedMethod(unsigned short i);
%rename(MethodForRenamingConst) Space::Base::MethodForRenaming(int i) const;
// Different overloaded warning filters needed for scripting languages (eg Python) and for statically typed languages (eg C#).
%warnfilter(509, 516) Space::Base::MethodWarningSuppressed(int i) const;
%inline %{
namespace AnotherSpace {
class AnotherBase {
public:
virtual int AnotherMethod(int i) { return 0; }
virtual int AnotherMethod(int i) const { return 1; }
virtual ~AnotherBase() {}
};
}
namespace Space {
class Base
{
public:
virtual int Method(int i) { return 0; }
virtual int Method(int i) const { return 1; }
virtual int MethodForRenaming(int i) { return 0; }
virtual int MethodForRenaming(int i) const { return 1; }
virtual int MethodWarningSuppressed(int i) { return 0; }
virtual int MethodWarningSuppressed(int i) const { return 1; }
int NotVirtualMethod(int i) { return 0; }
int NotVirtualMethod(int i) const { return 1; }
typedef int Integer;
// int and unsigned short are wrapped with a Java int and so would be automatically ignored with a warning
virtual int SimilarOverloadedMethod(Integer i) { return 0; }
virtual int SimilarOverloadedMethod(unsigned short i) { return 1; }
virtual ~Base() {}
static Base *inout(Base *p) { return p; }
};
class Derived : public Base, public AnotherSpace::AnotherBase
{
public:
int member_var;
};
class MoreDerived : public Derived {
};
}
namespace OtherSpace {
class OtherDerived : public Space::Base
{
};
}
%}

View file

@ -48,7 +48,7 @@
%shared_ptr(Space::Bottom2)
%shared_ptr(Space::Bottom3)
%include "swiginterface.i"
%include <swiginterface.i>
SWIG_SHARED_PTR_INTERFACE_TYPEMAPS(, Space::ABase1)
SWIG_SHARED_PTR_INTERFACE_TYPEMAPS(, Space::CBase1)
SWIG_SHARED_PTR_INTERFACE_TYPEMAPS(, Space::CBase2)

View file

@ -5,7 +5,7 @@
SWIGWARN_PHP_MULTIPLE_INHERITANCE); /* languages not supporting multiple inheritance or %interface */
#if defined(SWIGJAVA) || defined(SWIGCSHARP)
%include "swiginterface.i"
%include <swiginterface.i>
%interface(IA)
#endif